1 // SPDX-License-Identifier: GPL-2.0+
3 * Procedures for creating, accessing and interpreting the device tree.
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
11 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
13 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
17 #define pr_fmt(fmt) "OF: " fmt
19 #include <linux/bitmap.h>
20 #include <linux/console.h>
21 #include <linux/ctype.h>
22 #include <linux/cpu.h>
23 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/of_graph.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/proc_fs.h>
32 #include "of_private.h"
34 LIST_HEAD(aliases_lookup
);
36 struct device_node
*of_root
;
37 EXPORT_SYMBOL(of_root
);
38 struct device_node
*of_chosen
;
39 struct device_node
*of_aliases
;
40 struct device_node
*of_stdout
;
41 static const char *of_stdout_options
;
46 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
47 * This mutex must be held whenever modifications are being made to the
48 * device tree. The of_{attach,detach}_node() and
49 * of_{add,remove,update}_property() helpers make sure this happens.
51 DEFINE_MUTEX(of_mutex
);
53 /* use when traversing tree through the child, sibling,
54 * or parent members of struct device_node.
56 DEFINE_RAW_SPINLOCK(devtree_lock
);
58 bool of_node_name_eq(const struct device_node
*np
, const char *name
)
60 const char *node_name
;
66 node_name
= kbasename(np
->full_name
);
67 len
= strchrnul(node_name
, '@') - node_name
;
69 return (strlen(name
) == len
) && (strncmp(node_name
, name
, len
) == 0);
71 EXPORT_SYMBOL(of_node_name_eq
);
73 bool of_node_name_prefix(const struct device_node
*np
, const char *prefix
)
78 return strncmp(kbasename(np
->full_name
), prefix
, strlen(prefix
)) == 0;
80 EXPORT_SYMBOL(of_node_name_prefix
);
82 static bool __of_node_is_type(const struct device_node
*np
, const char *type
)
84 const char *match
= __of_get_property(np
, "device_type", NULL
);
86 return np
&& match
&& type
&& !strcmp(match
, type
);
89 int of_bus_n_addr_cells(struct device_node
*np
)
93 for (; np
; np
= np
->parent
)
94 if (!of_property_read_u32(np
, "#address-cells", &cells
))
97 /* No #address-cells property for the root node */
98 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
101 int of_n_addr_cells(struct device_node
*np
)
106 return of_bus_n_addr_cells(np
);
108 EXPORT_SYMBOL(of_n_addr_cells
);
110 int of_bus_n_size_cells(struct device_node
*np
)
114 for (; np
; np
= np
->parent
)
115 if (!of_property_read_u32(np
, "#size-cells", &cells
))
118 /* No #size-cells property for the root node */
119 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
122 int of_n_size_cells(struct device_node
*np
)
127 return of_bus_n_size_cells(np
);
129 EXPORT_SYMBOL(of_n_size_cells
);
132 int __weak
of_node_to_nid(struct device_node
*np
)
139 * Assumptions behind phandle_cache implementation:
140 * - phandle property values are in a contiguous range of 1..n
142 * If the assumptions do not hold, then
143 * - the phandle lookup overhead reduction provided by the cache
144 * will likely be less
147 static struct device_node
**phandle_cache
;
148 static u32 phandle_cache_mask
;
151 * Caller must hold devtree_lock.
153 static void __of_free_phandle_cache(void)
155 u32 cache_entries
= phandle_cache_mask
+ 1;
161 for (k
= 0; k
< cache_entries
; k
++)
162 of_node_put(phandle_cache
[k
]);
164 kfree(phandle_cache
);
165 phandle_cache
= NULL
;
168 int of_free_phandle_cache(void)
172 raw_spin_lock_irqsave(&devtree_lock
, flags
);
174 __of_free_phandle_cache();
176 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
180 #if !defined(CONFIG_MODULES)
181 late_initcall_sync(of_free_phandle_cache
);
185 * Caller must hold devtree_lock.
187 void __of_free_phandle_cache_entry(phandle handle
)
189 phandle masked_handle
;
190 struct device_node
*np
;
195 masked_handle
= handle
& phandle_cache_mask
;
198 np
= phandle_cache
[masked_handle
];
199 if (np
&& handle
== np
->phandle
) {
201 phandle_cache
[masked_handle
] = NULL
;
206 void of_populate_phandle_cache(void)
210 struct device_node
*np
;
213 raw_spin_lock_irqsave(&devtree_lock
, flags
);
215 __of_free_phandle_cache();
217 for_each_of_allnodes(np
)
218 if (np
->phandle
&& np
->phandle
!= OF_PHANDLE_ILLEGAL
)
224 cache_entries
= roundup_pow_of_two(phandles
);
225 phandle_cache_mask
= cache_entries
- 1;
227 phandle_cache
= kcalloc(cache_entries
, sizeof(*phandle_cache
),
232 for_each_of_allnodes(np
)
233 if (np
->phandle
&& np
->phandle
!= OF_PHANDLE_ILLEGAL
) {
235 phandle_cache
[np
->phandle
& phandle_cache_mask
] = np
;
239 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
242 void __init
of_core_init(void)
244 struct device_node
*np
;
246 of_populate_phandle_cache();
248 /* Create the kset, and register existing nodes */
249 mutex_lock(&of_mutex
);
250 of_kset
= kset_create_and_add("devicetree", NULL
, firmware_kobj
);
252 mutex_unlock(&of_mutex
);
253 pr_err("failed to register existing nodes\n");
256 for_each_of_allnodes(np
)
257 __of_attach_node_sysfs(np
);
258 mutex_unlock(&of_mutex
);
260 /* Symlink in /proc as required by userspace ABI */
262 proc_symlink("device-tree", NULL
, "/sys/firmware/devicetree/base");
265 static struct property
*__of_find_property(const struct device_node
*np
,
266 const char *name
, int *lenp
)
273 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
274 if (of_prop_cmp(pp
->name
, name
) == 0) {
284 struct property
*of_find_property(const struct device_node
*np
,
291 raw_spin_lock_irqsave(&devtree_lock
, flags
);
292 pp
= __of_find_property(np
, name
, lenp
);
293 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
297 EXPORT_SYMBOL(of_find_property
);
299 struct device_node
*__of_find_all_nodes(struct device_node
*prev
)
301 struct device_node
*np
;
304 } else if (prev
->child
) {
307 /* Walk back up looking for a sibling, or the end of the structure */
309 while (np
->parent
&& !np
->sibling
)
311 np
= np
->sibling
; /* Might be null at the end of the tree */
317 * of_find_all_nodes - Get next node in global list
318 * @prev: Previous node or NULL to start iteration
319 * of_node_put() will be called on it
321 * Returns a node pointer with refcount incremented, use
322 * of_node_put() on it when done.
324 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
326 struct device_node
*np
;
329 raw_spin_lock_irqsave(&devtree_lock
, flags
);
330 np
= __of_find_all_nodes(prev
);
333 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
336 EXPORT_SYMBOL(of_find_all_nodes
);
339 * Find a property with a given name for a given node
340 * and return the value.
342 const void *__of_get_property(const struct device_node
*np
,
343 const char *name
, int *lenp
)
345 struct property
*pp
= __of_find_property(np
, name
, lenp
);
347 return pp
? pp
->value
: NULL
;
351 * Find a property with a given name for a given node
352 * and return the value.
354 const void *of_get_property(const struct device_node
*np
, const char *name
,
357 struct property
*pp
= of_find_property(np
, name
, lenp
);
359 return pp
? pp
->value
: NULL
;
361 EXPORT_SYMBOL(of_get_property
);
364 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
366 * @cpu: logical cpu index of a core/thread
367 * @phys_id: physical identifier of a core/thread
369 * CPU logical to physical index mapping is architecture specific.
370 * However this __weak function provides a default match of physical
371 * id to logical cpu index. phys_id provided here is usually values read
372 * from the device tree which must match the hardware internal registers.
374 * Returns true if the physical identifier and the logical cpu index
375 * correspond to the same core/thread, false otherwise.
377 bool __weak
arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
379 return (u32
)phys_id
== cpu
;
383 * Checks if the given "prop_name" property holds the physical id of the
384 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
385 * NULL, local thread number within the core is returned in it.
387 static bool __of_find_n_match_cpu_property(struct device_node
*cpun
,
388 const char *prop_name
, int cpu
, unsigned int *thread
)
391 int ac
, prop_len
, tid
;
394 ac
= of_n_addr_cells(cpun
);
395 cell
= of_get_property(cpun
, prop_name
, &prop_len
);
396 if (!cell
&& !ac
&& arch_match_cpu_phys_id(cpu
, 0))
400 prop_len
/= sizeof(*cell
) * ac
;
401 for (tid
= 0; tid
< prop_len
; tid
++) {
402 hwid
= of_read_number(cell
, ac
);
403 if (arch_match_cpu_phys_id(cpu
, hwid
)) {
414 * arch_find_n_match_cpu_physical_id - See if the given device node is
415 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
416 * else false. If 'thread' is non-NULL, the local thread number within the
417 * core is returned in it.
419 bool __weak
arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
420 int cpu
, unsigned int *thread
)
422 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
423 * for thread ids on PowerPC. If it doesn't exist fallback to
424 * standard "reg" property.
426 if (IS_ENABLED(CONFIG_PPC
) &&
427 __of_find_n_match_cpu_property(cpun
,
428 "ibm,ppc-interrupt-server#s",
432 return __of_find_n_match_cpu_property(cpun
, "reg", cpu
, thread
);
436 * of_get_cpu_node - Get device node associated with the given logical CPU
438 * @cpu: CPU number(logical index) for which device node is required
439 * @thread: if not NULL, local thread number within the physical core is
442 * The main purpose of this function is to retrieve the device node for the
443 * given logical CPU index. It should be used to initialize the of_node in
444 * cpu device. Once of_node in cpu device is populated, all the further
445 * references can use that instead.
447 * CPU logical to physical index mapping is architecture specific and is built
448 * before booting secondary cores. This function uses arch_match_cpu_phys_id
449 * which can be overridden by architecture specific implementation.
451 * Returns a node pointer for the logical cpu with refcount incremented, use
452 * of_node_put() on it when done. Returns NULL if not found.
454 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
456 struct device_node
*cpun
;
458 for_each_of_cpu_node(cpun
) {
459 if (arch_find_n_match_cpu_physical_id(cpun
, cpu
, thread
))
464 EXPORT_SYMBOL(of_get_cpu_node
);
467 * of_cpu_node_to_id: Get the logical CPU number for a given device_node
469 * @cpu_node: Pointer to the device_node for CPU.
471 * Returns the logical CPU number of the given CPU device_node.
472 * Returns -ENODEV if the CPU is not found.
474 int of_cpu_node_to_id(struct device_node
*cpu_node
)
478 struct device_node
*np
;
480 for_each_possible_cpu(cpu
) {
481 np
= of_cpu_device_node_get(cpu
);
482 found
= (cpu_node
== np
);
490 EXPORT_SYMBOL(of_cpu_node_to_id
);
493 * __of_device_is_compatible() - Check if the node matches given constraints
494 * @device: pointer to node
495 * @compat: required compatible string, NULL or "" for any match
496 * @type: required device_type value, NULL or "" for any match
497 * @name: required node name, NULL or "" for any match
499 * Checks if the given @compat, @type and @name strings match the
500 * properties of the given @device. A constraints can be skipped by
501 * passing NULL or an empty string as the constraint.
503 * Returns 0 for no match, and a positive integer on match. The return
504 * value is a relative score with larger values indicating better
505 * matches. The score is weighted for the most specific compatible value
506 * to get the highest score. Matching type is next, followed by matching
507 * name. Practically speaking, this results in the following priority
510 * 1. specific compatible && type && name
511 * 2. specific compatible && type
512 * 3. specific compatible && name
513 * 4. specific compatible
514 * 5. general compatible && type && name
515 * 6. general compatible && type
516 * 7. general compatible && name
517 * 8. general compatible
522 static int __of_device_is_compatible(const struct device_node
*device
,
523 const char *compat
, const char *type
, const char *name
)
525 struct property
*prop
;
527 int index
= 0, score
= 0;
529 /* Compatible match has highest priority */
530 if (compat
&& compat
[0]) {
531 prop
= __of_find_property(device
, "compatible", NULL
);
532 for (cp
= of_prop_next_string(prop
, NULL
); cp
;
533 cp
= of_prop_next_string(prop
, cp
), index
++) {
534 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0) {
535 score
= INT_MAX
/2 - (index
<< 2);
543 /* Matching type is better than matching name */
544 if (type
&& type
[0]) {
545 if (!__of_node_is_type(device
, type
))
550 /* Matching name is a bit better than not */
551 if (name
&& name
[0]) {
552 if (!of_node_name_eq(device
, name
))
560 /** Checks if the given "compat" string matches one of the strings in
561 * the device's "compatible" property
563 int of_device_is_compatible(const struct device_node
*device
,
569 raw_spin_lock_irqsave(&devtree_lock
, flags
);
570 res
= __of_device_is_compatible(device
, compat
, NULL
, NULL
);
571 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
574 EXPORT_SYMBOL(of_device_is_compatible
);
576 /** Checks if the device is compatible with any of the entries in
577 * a NULL terminated array of strings. Returns the best match
580 int of_device_compatible_match(struct device_node
*device
,
581 const char *const *compat
)
583 unsigned int tmp
, score
= 0;
589 tmp
= of_device_is_compatible(device
, *compat
);
599 * of_machine_is_compatible - Test root of device tree for a given compatible value
600 * @compat: compatible string to look for in root node's compatible property.
602 * Returns a positive integer if the root node has the given value in its
603 * compatible property.
605 int of_machine_is_compatible(const char *compat
)
607 struct device_node
*root
;
610 root
= of_find_node_by_path("/");
612 rc
= of_device_is_compatible(root
, compat
);
617 EXPORT_SYMBOL(of_machine_is_compatible
);
620 * __of_device_is_available - check if a device is available for use
622 * @device: Node to check for availability, with locks already held
624 * Returns true if the status property is absent or set to "okay" or "ok",
627 static bool __of_device_is_available(const struct device_node
*device
)
635 status
= __of_get_property(device
, "status", &statlen
);
640 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
648 * of_device_is_available - check if a device is available for use
650 * @device: Node to check for availability
652 * Returns true if the status property is absent or set to "okay" or "ok",
655 bool of_device_is_available(const struct device_node
*device
)
660 raw_spin_lock_irqsave(&devtree_lock
, flags
);
661 res
= __of_device_is_available(device
);
662 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
666 EXPORT_SYMBOL(of_device_is_available
);
669 * of_device_is_big_endian - check if a device has BE registers
671 * @device: Node to check for endianness
673 * Returns true if the device has a "big-endian" property, or if the kernel
674 * was compiled for BE *and* the device has a "native-endian" property.
675 * Returns false otherwise.
677 * Callers would nominally use ioread32be/iowrite32be if
678 * of_device_is_big_endian() == true, or readl/writel otherwise.
680 bool of_device_is_big_endian(const struct device_node
*device
)
682 if (of_property_read_bool(device
, "big-endian"))
684 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
685 of_property_read_bool(device
, "native-endian"))
689 EXPORT_SYMBOL(of_device_is_big_endian
);
692 * of_get_parent - Get a node's parent if any
693 * @node: Node to get parent
695 * Returns a node pointer with refcount incremented, use
696 * of_node_put() on it when done.
698 struct device_node
*of_get_parent(const struct device_node
*node
)
700 struct device_node
*np
;
706 raw_spin_lock_irqsave(&devtree_lock
, flags
);
707 np
= of_node_get(node
->parent
);
708 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
711 EXPORT_SYMBOL(of_get_parent
);
714 * of_get_next_parent - Iterate to a node's parent
715 * @node: Node to get parent of
717 * This is like of_get_parent() except that it drops the
718 * refcount on the passed node, making it suitable for iterating
719 * through a node's parents.
721 * Returns a node pointer with refcount incremented, use
722 * of_node_put() on it when done.
724 struct device_node
*of_get_next_parent(struct device_node
*node
)
726 struct device_node
*parent
;
732 raw_spin_lock_irqsave(&devtree_lock
, flags
);
733 parent
= of_node_get(node
->parent
);
735 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
738 EXPORT_SYMBOL(of_get_next_parent
);
740 static struct device_node
*__of_get_next_child(const struct device_node
*node
,
741 struct device_node
*prev
)
743 struct device_node
*next
;
748 next
= prev
? prev
->sibling
: node
->child
;
749 for (; next
; next
= next
->sibling
)
750 if (of_node_get(next
))
755 #define __for_each_child_of_node(parent, child) \
756 for (child = __of_get_next_child(parent, NULL); child != NULL; \
757 child = __of_get_next_child(parent, child))
760 * of_get_next_child - Iterate a node childs
762 * @prev: previous child of the parent node, or NULL to get first
764 * Returns a node pointer with refcount incremented, use of_node_put() on
765 * it when done. Returns NULL when prev is the last child. Decrements the
768 struct device_node
*of_get_next_child(const struct device_node
*node
,
769 struct device_node
*prev
)
771 struct device_node
*next
;
774 raw_spin_lock_irqsave(&devtree_lock
, flags
);
775 next
= __of_get_next_child(node
, prev
);
776 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
779 EXPORT_SYMBOL(of_get_next_child
);
782 * of_get_next_available_child - Find the next available child node
784 * @prev: previous child of the parent node, or NULL to get first
786 * This function is like of_get_next_child(), except that it
787 * automatically skips any disabled nodes (i.e. status = "disabled").
789 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
790 struct device_node
*prev
)
792 struct device_node
*next
;
798 raw_spin_lock_irqsave(&devtree_lock
, flags
);
799 next
= prev
? prev
->sibling
: node
->child
;
800 for (; next
; next
= next
->sibling
) {
801 if (!__of_device_is_available(next
))
803 if (of_node_get(next
))
807 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
810 EXPORT_SYMBOL(of_get_next_available_child
);
813 * of_get_next_cpu_node - Iterate on cpu nodes
814 * @prev: previous child of the /cpus node, or NULL to get first
816 * Returns a cpu node pointer with refcount incremented, use of_node_put()
817 * on it when done. Returns NULL when prev is the last child. Decrements
818 * the refcount of prev.
820 struct device_node
*of_get_next_cpu_node(struct device_node
*prev
)
822 struct device_node
*next
= NULL
;
824 struct device_node
*node
;
827 node
= of_find_node_by_path("/cpus");
829 raw_spin_lock_irqsave(&devtree_lock
, flags
);
831 next
= prev
->sibling
;
836 for (; next
; next
= next
->sibling
) {
837 if (!(of_node_name_eq(next
, "cpu") ||
838 __of_node_is_type(next
, "cpu")))
840 if (of_node_get(next
))
844 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
847 EXPORT_SYMBOL(of_get_next_cpu_node
);
850 * of_get_compatible_child - Find compatible child node
851 * @parent: parent node
852 * @compatible: compatible string
854 * Lookup child node whose compatible property contains the given compatible
857 * Returns a node pointer with refcount incremented, use of_node_put() on it
858 * when done; or NULL if not found.
860 struct device_node
*of_get_compatible_child(const struct device_node
*parent
,
861 const char *compatible
)
863 struct device_node
*child
;
865 for_each_child_of_node(parent
, child
) {
866 if (of_device_is_compatible(child
, compatible
))
872 EXPORT_SYMBOL(of_get_compatible_child
);
875 * of_get_child_by_name - Find the child node by name for a given parent
877 * @name: child name to look for.
879 * This function looks for child node for given matching name
881 * Returns a node pointer if found, with refcount incremented, use
882 * of_node_put() on it when done.
883 * Returns NULL if node is not found.
885 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
888 struct device_node
*child
;
890 for_each_child_of_node(node
, child
)
891 if (of_node_name_eq(child
, name
))
895 EXPORT_SYMBOL(of_get_child_by_name
);
897 struct device_node
*__of_find_node_by_path(struct device_node
*parent
,
900 struct device_node
*child
;
903 len
= strcspn(path
, "/:");
907 __for_each_child_of_node(parent
, child
) {
908 const char *name
= kbasename(child
->full_name
);
909 if (strncmp(path
, name
, len
) == 0 && (strlen(name
) == len
))
915 struct device_node
*__of_find_node_by_full_path(struct device_node
*node
,
918 const char *separator
= strchr(path
, ':');
920 while (node
&& *path
== '/') {
921 struct device_node
*tmp
= node
;
923 path
++; /* Increment past '/' delimiter */
924 node
= __of_find_node_by_path(node
, path
);
926 path
= strchrnul(path
, '/');
927 if (separator
&& separator
< path
)
934 * of_find_node_opts_by_path - Find a node matching a full OF path
935 * @path: Either the full path to match, or if the path does not
936 * start with '/', the name of a property of the /aliases
937 * node (an alias). In the case of an alias, the node
938 * matching the alias' value will be returned.
939 * @opts: Address of a pointer into which to store the start of
940 * an options string appended to the end of the path with
946 * foo/bar Valid alias + relative path
948 * Returns a node pointer with refcount incremented, use
949 * of_node_put() on it when done.
951 struct device_node
*of_find_node_opts_by_path(const char *path
, const char **opts
)
953 struct device_node
*np
= NULL
;
956 const char *separator
= strchr(path
, ':');
959 *opts
= separator
? separator
+ 1 : NULL
;
961 if (strcmp(path
, "/") == 0)
962 return of_node_get(of_root
);
964 /* The path could begin with an alias */
967 const char *p
= separator
;
970 p
= strchrnul(path
, '/');
973 /* of_aliases must not be NULL */
977 for_each_property_of_node(of_aliases
, pp
) {
978 if (strlen(pp
->name
) == len
&& !strncmp(pp
->name
, path
, len
)) {
979 np
= of_find_node_by_path(pp
->value
);
988 /* Step down the tree matching path components */
989 raw_spin_lock_irqsave(&devtree_lock
, flags
);
991 np
= of_node_get(of_root
);
992 np
= __of_find_node_by_full_path(np
, path
);
993 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
996 EXPORT_SYMBOL(of_find_node_opts_by_path
);
999 * of_find_node_by_name - Find a node by its "name" property
1000 * @from: The node to start searching from or NULL; the node
1001 * you pass will not be searched, only the next one
1002 * will. Typically, you pass what the previous call
1003 * returned. of_node_put() will be called on @from.
1004 * @name: The name string to match against
1006 * Returns a node pointer with refcount incremented, use
1007 * of_node_put() on it when done.
1009 struct device_node
*of_find_node_by_name(struct device_node
*from
,
1012 struct device_node
*np
;
1013 unsigned long flags
;
1015 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1016 for_each_of_allnodes_from(from
, np
)
1017 if (of_node_name_eq(np
, name
) && of_node_get(np
))
1020 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1023 EXPORT_SYMBOL(of_find_node_by_name
);
1026 * of_find_node_by_type - Find a node by its "device_type" property
1027 * @from: The node to start searching from, or NULL to start searching
1028 * the entire device tree. The node you pass will not be
1029 * searched, only the next one will; typically, you pass
1030 * what the previous call returned. of_node_put() will be
1031 * called on from for you.
1032 * @type: The type string to match against
1034 * Returns a node pointer with refcount incremented, use
1035 * of_node_put() on it when done.
1037 struct device_node
*of_find_node_by_type(struct device_node
*from
,
1040 struct device_node
*np
;
1041 unsigned long flags
;
1043 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1044 for_each_of_allnodes_from(from
, np
)
1045 if (__of_node_is_type(np
, type
) && of_node_get(np
))
1048 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1051 EXPORT_SYMBOL(of_find_node_by_type
);
1054 * of_find_compatible_node - Find a node based on type and one of the
1055 * tokens in its "compatible" property
1056 * @from: The node to start searching from or NULL, the node
1057 * you pass will not be searched, only the next one
1058 * will; typically, you pass what the previous call
1059 * returned. of_node_put() will be called on it
1060 * @type: The type string to match "device_type" or NULL to ignore
1061 * @compatible: The string to match to one of the tokens in the device
1062 * "compatible" list.
1064 * Returns a node pointer with refcount incremented, use
1065 * of_node_put() on it when done.
1067 struct device_node
*of_find_compatible_node(struct device_node
*from
,
1068 const char *type
, const char *compatible
)
1070 struct device_node
*np
;
1071 unsigned long flags
;
1073 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1074 for_each_of_allnodes_from(from
, np
)
1075 if (__of_device_is_compatible(np
, compatible
, type
, NULL
) &&
1079 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1082 EXPORT_SYMBOL(of_find_compatible_node
);
1085 * of_find_node_with_property - Find a node which has a property with
1087 * @from: The node to start searching from or NULL, the node
1088 * you pass will not be searched, only the next one
1089 * will; typically, you pass what the previous call
1090 * returned. of_node_put() will be called on it
1091 * @prop_name: The name of the property to look for.
1093 * Returns a node pointer with refcount incremented, use
1094 * of_node_put() on it when done.
1096 struct device_node
*of_find_node_with_property(struct device_node
*from
,
1097 const char *prop_name
)
1099 struct device_node
*np
;
1100 struct property
*pp
;
1101 unsigned long flags
;
1103 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1104 for_each_of_allnodes_from(from
, np
) {
1105 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
1106 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
1114 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1117 EXPORT_SYMBOL(of_find_node_with_property
);
1120 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
1121 const struct device_node
*node
)
1123 const struct of_device_id
*best_match
= NULL
;
1124 int score
, best_score
= 0;
1129 for (; matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]; matches
++) {
1130 score
= __of_device_is_compatible(node
, matches
->compatible
,
1131 matches
->type
, matches
->name
);
1132 if (score
> best_score
) {
1133 best_match
= matches
;
1142 * of_match_node - Tell if a device_node has a matching of_match structure
1143 * @matches: array of of device match structures to search in
1144 * @node: the of device structure to match against
1146 * Low level utility function used by device matching.
1148 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
1149 const struct device_node
*node
)
1151 const struct of_device_id
*match
;
1152 unsigned long flags
;
1154 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1155 match
= __of_match_node(matches
, node
);
1156 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1159 EXPORT_SYMBOL(of_match_node
);
1162 * of_find_matching_node_and_match - Find a node based on an of_device_id
1164 * @from: The node to start searching from or NULL, the node
1165 * you pass will not be searched, only the next one
1166 * will; typically, you pass what the previous call
1167 * returned. of_node_put() will be called on it
1168 * @matches: array of of device match structures to search in
1169 * @match Updated to point at the matches entry which matched
1171 * Returns a node pointer with refcount incremented, use
1172 * of_node_put() on it when done.
1174 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
1175 const struct of_device_id
*matches
,
1176 const struct of_device_id
**match
)
1178 struct device_node
*np
;
1179 const struct of_device_id
*m
;
1180 unsigned long flags
;
1185 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1186 for_each_of_allnodes_from(from
, np
) {
1187 m
= __of_match_node(matches
, np
);
1188 if (m
&& of_node_get(np
)) {
1195 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1198 EXPORT_SYMBOL(of_find_matching_node_and_match
);
1201 * of_modalias_node - Lookup appropriate modalias for a device node
1202 * @node: pointer to a device tree node
1203 * @modalias: Pointer to buffer that modalias value will be copied into
1204 * @len: Length of modalias value
1206 * Based on the value of the compatible property, this routine will attempt
1207 * to choose an appropriate modalias value for a particular device tree node.
1208 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1209 * from the first entry in the compatible list property.
1211 * This routine returns 0 on success, <0 on failure.
1213 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
1215 const char *compatible
, *p
;
1218 compatible
= of_get_property(node
, "compatible", &cplen
);
1219 if (!compatible
|| strlen(compatible
) > cplen
)
1221 p
= strchr(compatible
, ',');
1222 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
1225 EXPORT_SYMBOL_GPL(of_modalias_node
);
1228 * of_find_node_by_phandle - Find a node given a phandle
1229 * @handle: phandle of the node to find
1231 * Returns a node pointer with refcount incremented, use
1232 * of_node_put() on it when done.
1234 struct device_node
*of_find_node_by_phandle(phandle handle
)
1236 struct device_node
*np
= NULL
;
1237 unsigned long flags
;
1238 phandle masked_handle
;
1243 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1245 masked_handle
= handle
& phandle_cache_mask
;
1247 if (phandle_cache
) {
1248 if (phandle_cache
[masked_handle
] &&
1249 handle
== phandle_cache
[masked_handle
]->phandle
)
1250 np
= phandle_cache
[masked_handle
];
1251 if (np
&& of_node_check_flag(np
, OF_DETACHED
)) {
1252 WARN_ON(1); /* did not uncache np on node removal */
1254 phandle_cache
[masked_handle
] = NULL
;
1260 for_each_of_allnodes(np
)
1261 if (np
->phandle
== handle
&&
1262 !of_node_check_flag(np
, OF_DETACHED
)) {
1263 if (phandle_cache
) {
1264 /* will put when removed from cache */
1266 phandle_cache
[masked_handle
] = np
;
1273 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1276 EXPORT_SYMBOL(of_find_node_by_phandle
);
1278 void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
)
1281 printk("%s %pOF", msg
, args
->np
);
1282 for (i
= 0; i
< args
->args_count
; i
++) {
1283 const char delim
= i
? ',' : ':';
1285 pr_cont("%c%08x", delim
, args
->args
[i
]);
1290 int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
1291 const struct device_node
*np
,
1292 const char *list_name
,
1293 const char *cells_name
,
1299 memset(it
, 0, sizeof(*it
));
1302 * one of cell_count or cells_name must be provided to determine the
1305 if (cell_count
< 0 && !cells_name
)
1308 list
= of_get_property(np
, list_name
, &size
);
1312 it
->cells_name
= cells_name
;
1313 it
->cell_count
= cell_count
;
1315 it
->list_end
= list
+ size
/ sizeof(*list
);
1316 it
->phandle_end
= list
;
1321 EXPORT_SYMBOL_GPL(of_phandle_iterator_init
);
1323 int of_phandle_iterator_next(struct of_phandle_iterator
*it
)
1328 of_node_put(it
->node
);
1332 if (!it
->cur
|| it
->phandle_end
>= it
->list_end
)
1335 it
->cur
= it
->phandle_end
;
1337 /* If phandle is 0, then it is an empty entry with no arguments. */
1338 it
->phandle
= be32_to_cpup(it
->cur
++);
1343 * Find the provider node and parse the #*-cells property to
1344 * determine the argument length.
1346 it
->node
= of_find_node_by_phandle(it
->phandle
);
1348 if (it
->cells_name
) {
1350 pr_err("%pOF: could not find phandle\n",
1355 if (of_property_read_u32(it
->node
, it
->cells_name
,
1358 * If both cell_count and cells_name is given,
1359 * fall back to cell_count in absence
1360 * of the cells_name property
1362 if (it
->cell_count
>= 0) {
1363 count
= it
->cell_count
;
1365 pr_err("%pOF: could not get %s for %pOF\n",
1373 count
= it
->cell_count
;
1377 * Make sure that the arguments actually fit in the remaining
1378 * property data length
1380 if (it
->cur
+ count
> it
->list_end
) {
1381 pr_err("%pOF: %s = %d found %d\n",
1382 it
->parent
, it
->cells_name
,
1383 count
, it
->cell_count
);
1388 it
->phandle_end
= it
->cur
+ count
;
1389 it
->cur_count
= count
;
1395 of_node_put(it
->node
);
1401 EXPORT_SYMBOL_GPL(of_phandle_iterator_next
);
1403 int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
1409 count
= it
->cur_count
;
1411 if (WARN_ON(size
< count
))
1414 for (i
= 0; i
< count
; i
++)
1415 args
[i
] = be32_to_cpup(it
->cur
++);
1420 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1421 const char *list_name
,
1422 const char *cells_name
,
1423 int cell_count
, int index
,
1424 struct of_phandle_args
*out_args
)
1426 struct of_phandle_iterator it
;
1427 int rc
, cur_index
= 0;
1429 /* Loop over the phandles until all the requested entry is found */
1430 of_for_each_phandle(&it
, rc
, np
, list_name
, cells_name
, cell_count
) {
1432 * All of the error cases bail out of the loop, so at
1433 * this point, the parsing is successful. If the requested
1434 * index matches, then fill the out_args structure and return,
1435 * or return -ENOENT for an empty entry.
1438 if (cur_index
== index
) {
1445 c
= of_phandle_iterator_args(&it
,
1448 out_args
->np
= it
.node
;
1449 out_args
->args_count
= c
;
1451 of_node_put(it
.node
);
1454 /* Found it! return success */
1462 * Unlock node before returning result; will be one of:
1463 * -ENOENT : index is for empty phandle
1464 * -EINVAL : parsing error on data
1468 of_node_put(it
.node
);
1473 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1474 * @np: Pointer to device node holding phandle property
1475 * @phandle_name: Name of property holding a phandle value
1476 * @index: For properties holding a table of phandles, this is the index into
1479 * Returns the device_node pointer with refcount incremented. Use
1480 * of_node_put() on it when done.
1482 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1483 const char *phandle_name
, int index
)
1485 struct of_phandle_args args
;
1490 if (__of_parse_phandle_with_args(np
, phandle_name
, NULL
, 0,
1496 EXPORT_SYMBOL(of_parse_phandle
);
1499 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1500 * @np: pointer to a device tree node containing a list
1501 * @list_name: property name that contains a list
1502 * @cells_name: property name that specifies phandles' arguments count
1503 * @index: index of a phandle to parse out
1504 * @out_args: optional pointer to output arguments structure (will be filled)
1506 * This function is useful to parse lists of phandles and their arguments.
1507 * Returns 0 on success and fills out_args, on error returns appropriate
1510 * Caller is responsible to call of_node_put() on the returned out_args->np
1516 * #list-cells = <2>;
1520 * #list-cells = <1>;
1524 * list = <&phandle1 1 2 &phandle2 3>;
1527 * To get a device_node of the `node2' node you may call this:
1528 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1530 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1531 const char *cells_name
, int index
,
1532 struct of_phandle_args
*out_args
)
1534 int cell_count
= -1;
1539 /* If cells_name is NULL we assume a cell count of 0 */
1543 return __of_parse_phandle_with_args(np
, list_name
, cells_name
,
1544 cell_count
, index
, out_args
);
1546 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1549 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1550 * @np: pointer to a device tree node containing a list
1551 * @list_name: property name that contains a list
1552 * @stem_name: stem of property names that specify phandles' arguments count
1553 * @index: index of a phandle to parse out
1554 * @out_args: optional pointer to output arguments structure (will be filled)
1556 * This function is useful to parse lists of phandles and their arguments.
1557 * Returns 0 on success and fills out_args, on error returns appropriate errno
1558 * value. The difference between this function and of_parse_phandle_with_args()
1559 * is that this API remaps a phandle if the node the phandle points to has
1560 * a <@stem_name>-map property.
1562 * Caller is responsible to call of_node_put() on the returned out_args->np
1568 * #list-cells = <2>;
1572 * #list-cells = <1>;
1576 * #list-cells = <1>;
1577 * list-map = <0 &phandle2 3>,
1579 * <2 &phandle1 5 1>;
1580 * list-map-mask = <0x3>;
1584 * list = <&phandle1 1 2 &phandle3 0>;
1587 * To get a device_node of the `node2' node you may call this:
1588 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1590 int of_parse_phandle_with_args_map(const struct device_node
*np
,
1591 const char *list_name
,
1592 const char *stem_name
,
1593 int index
, struct of_phandle_args
*out_args
)
1595 char *cells_name
, *map_name
= NULL
, *mask_name
= NULL
;
1596 char *pass_name
= NULL
;
1597 struct device_node
*cur
, *new = NULL
;
1598 const __be32
*map
, *mask
, *pass
;
1599 static const __be32 dummy_mask
[] = { [0 ... MAX_PHANDLE_ARGS
] = ~0 };
1600 static const __be32 dummy_pass
[] = { [0 ... MAX_PHANDLE_ARGS
] = 0 };
1601 __be32 initial_match_array
[MAX_PHANDLE_ARGS
];
1602 const __be32
*match_array
= initial_match_array
;
1603 int i
, ret
, map_len
, match
;
1604 u32 list_size
, new_size
;
1609 cells_name
= kasprintf(GFP_KERNEL
, "#%s-cells", stem_name
);
1614 map_name
= kasprintf(GFP_KERNEL
, "%s-map", stem_name
);
1618 mask_name
= kasprintf(GFP_KERNEL
, "%s-map-mask", stem_name
);
1622 pass_name
= kasprintf(GFP_KERNEL
, "%s-map-pass-thru", stem_name
);
1626 ret
= __of_parse_phandle_with_args(np
, list_name
, cells_name
, -1, index
,
1631 /* Get the #<list>-cells property */
1633 ret
= of_property_read_u32(cur
, cells_name
, &list_size
);
1637 /* Precalculate the match array - this simplifies match loop */
1638 for (i
= 0; i
< list_size
; i
++)
1639 initial_match_array
[i
] = cpu_to_be32(out_args
->args
[i
]);
1643 /* Get the <list>-map property */
1644 map
= of_get_property(cur
, map_name
, &map_len
);
1649 map_len
/= sizeof(u32
);
1651 /* Get the <list>-map-mask property (optional) */
1652 mask
= of_get_property(cur
, mask_name
, NULL
);
1655 /* Iterate through <list>-map property */
1657 while (map_len
> (list_size
+ 1) && !match
) {
1658 /* Compare specifiers */
1660 for (i
= 0; i
< list_size
; i
++, map_len
--)
1661 match
&= !((match_array
[i
] ^ *map
++) & mask
[i
]);
1664 new = of_find_node_by_phandle(be32_to_cpup(map
));
1668 /* Check if not found */
1672 if (!of_device_is_available(new))
1675 ret
= of_property_read_u32(new, cells_name
, &new_size
);
1679 /* Check for malformed properties */
1680 if (WARN_ON(new_size
> MAX_PHANDLE_ARGS
))
1682 if (map_len
< new_size
)
1685 /* Move forward by new node's #<list>-cells amount */
1687 map_len
-= new_size
;
1692 /* Get the <list>-map-pass-thru property (optional) */
1693 pass
= of_get_property(cur
, pass_name
, NULL
);
1698 * Successfully parsed a <list>-map translation; copy new
1699 * specifier into the out_args structure, keeping the
1700 * bits specified in <list>-map-pass-thru.
1702 match_array
= map
- new_size
;
1703 for (i
= 0; i
< new_size
; i
++) {
1704 __be32 val
= *(map
- new_size
+ i
);
1706 if (i
< list_size
) {
1708 val
|= cpu_to_be32(out_args
->args
[i
]) & pass
[i
];
1711 out_args
->args
[i
] = be32_to_cpu(val
);
1713 out_args
->args_count
= list_size
= new_size
;
1714 /* Iterate again with new provider */
1730 EXPORT_SYMBOL(of_parse_phandle_with_args_map
);
1733 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1734 * @np: pointer to a device tree node containing a list
1735 * @list_name: property name that contains a list
1736 * @cell_count: number of argument cells following the phandle
1737 * @index: index of a phandle to parse out
1738 * @out_args: optional pointer to output arguments structure (will be filled)
1740 * This function is useful to parse lists of phandles and their arguments.
1741 * Returns 0 on success and fills out_args, on error returns appropriate
1744 * Caller is responsible to call of_node_put() on the returned out_args->np
1756 * list = <&phandle1 0 2 &phandle2 2 3>;
1759 * To get a device_node of the `node2' node you may call this:
1760 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1762 int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
1763 const char *list_name
, int cell_count
,
1764 int index
, struct of_phandle_args
*out_args
)
1768 return __of_parse_phandle_with_args(np
, list_name
, NULL
, cell_count
,
1771 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args
);
1774 * of_count_phandle_with_args() - Find the number of phandles references in a property
1775 * @np: pointer to a device tree node containing a list
1776 * @list_name: property name that contains a list
1777 * @cells_name: property name that specifies phandles' arguments count
1779 * Returns the number of phandle + argument tuples within a property. It
1780 * is a typical pattern to encode a list of phandle and variable
1781 * arguments into a single property. The number of arguments is encoded
1782 * by a property in the phandle-target node. For example, a gpios
1783 * property would contain a list of GPIO specifies consisting of a
1784 * phandle and 1 or more arguments. The number of arguments are
1785 * determined by the #gpio-cells property in the node pointed to by the
1788 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1789 const char *cells_name
)
1791 struct of_phandle_iterator it
;
1792 int rc
, cur_index
= 0;
1795 * If cells_name is NULL we assume a cell count of 0. This makes
1796 * counting the phandles trivial as each 32bit word in the list is a
1797 * phandle and no arguments are to consider. So we don't iterate through
1798 * the list but just use the length to determine the phandle count.
1804 list
= of_get_property(np
, list_name
, &size
);
1808 return size
/ sizeof(*list
);
1811 rc
= of_phandle_iterator_init(&it
, np
, list_name
, cells_name
, -1);
1815 while ((rc
= of_phandle_iterator_next(&it
)) == 0)
1823 EXPORT_SYMBOL(of_count_phandle_with_args
);
1826 * __of_add_property - Add a property to a node without lock operations
1828 int __of_add_property(struct device_node
*np
, struct property
*prop
)
1830 struct property
**next
;
1833 next
= &np
->properties
;
1835 if (strcmp(prop
->name
, (*next
)->name
) == 0)
1836 /* duplicate ! don't insert it */
1839 next
= &(*next
)->next
;
1847 * of_add_property - Add a property to a node
1849 int of_add_property(struct device_node
*np
, struct property
*prop
)
1851 unsigned long flags
;
1854 mutex_lock(&of_mutex
);
1856 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1857 rc
= __of_add_property(np
, prop
);
1858 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1861 __of_add_property_sysfs(np
, prop
);
1863 mutex_unlock(&of_mutex
);
1866 of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
, NULL
);
1871 int __of_remove_property(struct device_node
*np
, struct property
*prop
)
1873 struct property
**next
;
1875 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1882 /* found the node */
1884 prop
->next
= np
->deadprops
;
1885 np
->deadprops
= prop
;
1891 * of_remove_property - Remove a property from a node.
1893 * Note that we don't actually remove it, since we have given out
1894 * who-knows-how-many pointers to the data using get-property.
1895 * Instead we just move the property to the "dead properties"
1896 * list, so it won't be found any more.
1898 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1900 unsigned long flags
;
1906 mutex_lock(&of_mutex
);
1908 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1909 rc
= __of_remove_property(np
, prop
);
1910 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1913 __of_remove_property_sysfs(np
, prop
);
1915 mutex_unlock(&of_mutex
);
1918 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
, NULL
);
1923 int __of_update_property(struct device_node
*np
, struct property
*newprop
,
1924 struct property
**oldpropp
)
1926 struct property
**next
, *oldprop
;
1928 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1929 if (of_prop_cmp((*next
)->name
, newprop
->name
) == 0)
1932 *oldpropp
= oldprop
= *next
;
1935 /* replace the node */
1936 newprop
->next
= oldprop
->next
;
1938 oldprop
->next
= np
->deadprops
;
1939 np
->deadprops
= oldprop
;
1942 newprop
->next
= NULL
;
1950 * of_update_property - Update a property in a node, if the property does
1951 * not exist, add it.
1953 * Note that we don't actually remove it, since we have given out
1954 * who-knows-how-many pointers to the data using get-property.
1955 * Instead we just move the property to the "dead properties" list,
1956 * and add the new property to the property list
1958 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1960 struct property
*oldprop
;
1961 unsigned long flags
;
1967 mutex_lock(&of_mutex
);
1969 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1970 rc
= __of_update_property(np
, newprop
, &oldprop
);
1971 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1974 __of_update_property_sysfs(np
, newprop
, oldprop
);
1976 mutex_unlock(&of_mutex
);
1979 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
, oldprop
);
1984 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1985 int id
, const char *stem
, int stem_len
)
1989 strncpy(ap
->stem
, stem
, stem_len
);
1990 ap
->stem
[stem_len
] = 0;
1991 list_add_tail(&ap
->link
, &aliases_lookup
);
1992 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1993 ap
->alias
, ap
->stem
, ap
->id
, np
);
1997 * of_alias_scan - Scan all properties of the 'aliases' node
1999 * The function scans all the properties of the 'aliases' node and populates
2000 * the global lookup table with the properties. It returns the
2001 * number of alias properties found, or an error code in case of failure.
2003 * @dt_alloc: An allocator that provides a virtual address to memory
2004 * for storing the resulting tree
2006 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
2008 struct property
*pp
;
2010 of_aliases
= of_find_node_by_path("/aliases");
2011 of_chosen
= of_find_node_by_path("/chosen");
2012 if (of_chosen
== NULL
)
2013 of_chosen
= of_find_node_by_path("/chosen@0");
2016 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
2017 const char *name
= NULL
;
2019 if (of_property_read_string(of_chosen
, "stdout-path", &name
))
2020 of_property_read_string(of_chosen
, "linux,stdout-path",
2022 if (IS_ENABLED(CONFIG_PPC
) && !name
)
2023 of_property_read_string(of_aliases
, "stdout", &name
);
2025 of_stdout
= of_find_node_opts_by_path(name
, &of_stdout_options
);
2031 for_each_property_of_node(of_aliases
, pp
) {
2032 const char *start
= pp
->name
;
2033 const char *end
= start
+ strlen(start
);
2034 struct device_node
*np
;
2035 struct alias_prop
*ap
;
2038 /* Skip those we do not want to proceed */
2039 if (!strcmp(pp
->name
, "name") ||
2040 !strcmp(pp
->name
, "phandle") ||
2041 !strcmp(pp
->name
, "linux,phandle"))
2044 np
= of_find_node_by_path(pp
->value
);
2048 /* walk the alias backwards to extract the id and work out
2049 * the 'stem' string */
2050 while (isdigit(*(end
-1)) && end
> start
)
2054 if (kstrtoint(end
, 10, &id
) < 0)
2057 /* Allocate an alias_prop with enough space for the stem */
2058 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, __alignof__(*ap
));
2061 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
2063 of_alias_add(ap
, np
, id
, start
, len
);
2068 * of_alias_get_id - Get alias id for the given device_node
2069 * @np: Pointer to the given device_node
2070 * @stem: Alias stem of the given device_node
2072 * The function travels the lookup table to get the alias id for the given
2073 * device_node and alias stem. It returns the alias id if found.
2075 int of_alias_get_id(struct device_node
*np
, const char *stem
)
2077 struct alias_prop
*app
;
2080 mutex_lock(&of_mutex
);
2081 list_for_each_entry(app
, &aliases_lookup
, link
) {
2082 if (strcmp(app
->stem
, stem
) != 0)
2085 if (np
== app
->np
) {
2090 mutex_unlock(&of_mutex
);
2094 EXPORT_SYMBOL_GPL(of_alias_get_id
);
2097 * of_alias_get_alias_list - Get alias list for the given device driver
2098 * @matches: Array of OF device match structures to search in
2099 * @stem: Alias stem of the given device_node
2100 * @bitmap: Bitmap field pointer
2101 * @nbits: Maximum number of alias IDs which can be recorded in bitmap
2103 * The function travels the lookup table to record alias ids for the given
2104 * device match structures and alias stem.
2106 * Return: 0 or -ENOSYS when !CONFIG_OF or
2107 * -EOVERFLOW if alias ID is greater then allocated nbits
2109 int of_alias_get_alias_list(const struct of_device_id
*matches
,
2110 const char *stem
, unsigned long *bitmap
,
2113 struct alias_prop
*app
;
2116 /* Zero bitmap field to make sure that all the time it is clean */
2117 bitmap_zero(bitmap
, nbits
);
2119 mutex_lock(&of_mutex
);
2120 pr_debug("%s: Looking for stem: %s\n", __func__
, stem
);
2121 list_for_each_entry(app
, &aliases_lookup
, link
) {
2122 pr_debug("%s: stem: %s, id: %d\n",
2123 __func__
, app
->stem
, app
->id
);
2125 if (strcmp(app
->stem
, stem
) != 0) {
2126 pr_debug("%s: stem comparison didn't pass %s\n",
2127 __func__
, app
->stem
);
2131 if (of_match_node(matches
, app
->np
)) {
2132 pr_debug("%s: Allocated ID %d\n", __func__
, app
->id
);
2134 if (app
->id
>= nbits
) {
2135 pr_warn("%s: ID %d >= than bitmap field %d\n",
2136 __func__
, app
->id
, nbits
);
2139 set_bit(app
->id
, bitmap
);
2143 mutex_unlock(&of_mutex
);
2147 EXPORT_SYMBOL_GPL(of_alias_get_alias_list
);
2150 * of_alias_get_highest_id - Get highest alias id for the given stem
2151 * @stem: Alias stem to be examined
2153 * The function travels the lookup table to get the highest alias id for the
2154 * given alias stem. It returns the alias id if found.
2156 int of_alias_get_highest_id(const char *stem
)
2158 struct alias_prop
*app
;
2161 mutex_lock(&of_mutex
);
2162 list_for_each_entry(app
, &aliases_lookup
, link
) {
2163 if (strcmp(app
->stem
, stem
) != 0)
2169 mutex_unlock(&of_mutex
);
2173 EXPORT_SYMBOL_GPL(of_alias_get_highest_id
);
2176 * of_console_check() - Test and setup console for DT setup
2177 * @dn - Pointer to device node
2178 * @name - Name to use for preferred console without index. ex. "ttyS"
2179 * @index - Index to use for preferred console.
2181 * Check if the given device node matches the stdout-path property in the
2182 * /chosen node. If it does then register it as the preferred console and return
2183 * TRUE. Otherwise return FALSE.
2185 bool of_console_check(struct device_node
*dn
, char *name
, int index
)
2187 if (!dn
|| dn
!= of_stdout
|| console_set_on_cmdline
)
2191 * XXX: cast `options' to char pointer to suppress complication
2192 * warnings: printk, UART and console drivers expect char pointer.
2194 return !add_preferred_console(name
, index
, (char *)of_stdout_options
);
2196 EXPORT_SYMBOL_GPL(of_console_check
);
2199 * of_find_next_cache_node - Find a node's subsidiary cache
2200 * @np: node of type "cpu" or "cache"
2202 * Returns a node pointer with refcount incremented, use
2203 * of_node_put() on it when done. Caller should hold a reference
2206 struct device_node
*of_find_next_cache_node(const struct device_node
*np
)
2208 struct device_node
*child
, *cache_node
;
2210 cache_node
= of_parse_phandle(np
, "l2-cache", 0);
2212 cache_node
= of_parse_phandle(np
, "next-level-cache", 0);
2217 /* OF on pmac has nodes instead of properties named "l2-cache"
2218 * beneath CPU nodes.
2220 if (IS_ENABLED(CONFIG_PPC_PMAC
) && of_node_is_type(np
, "cpu"))
2221 for_each_child_of_node(np
, child
)
2222 if (of_node_is_type(child
, "cache"))
2229 * of_find_last_cache_level - Find the level at which the last cache is
2230 * present for the given logical cpu
2232 * @cpu: cpu number(logical index) for which the last cache level is needed
2234 * Returns the the level at which the last cache is present. It is exactly
2235 * same as the total number of cache levels for the given logical cpu.
2237 int of_find_last_cache_level(unsigned int cpu
)
2239 u32 cache_level
= 0;
2240 struct device_node
*prev
= NULL
, *np
= of_cpu_device_node_get(cpu
);
2245 np
= of_find_next_cache_node(np
);
2248 of_property_read_u32(prev
, "cache-level", &cache_level
);
2254 * of_map_rid - Translate a requester ID through a downstream mapping.
2255 * @np: root complex device node.
2256 * @rid: device requester ID to map.
2257 * @map_name: property name of the map to use.
2258 * @map_mask_name: optional property name of the mask to use.
2259 * @target: optional pointer to a target device node.
2260 * @id_out: optional pointer to receive the translated ID.
2262 * Given a device requester ID, look up the appropriate implementation-defined
2263 * platform ID and/or the target device which receives transactions on that
2264 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
2265 * @id_out may be NULL if only the other is required. If @target points to
2266 * a non-NULL device node pointer, only entries targeting that node will be
2267 * matched; if it points to a NULL value, it will receive the device node of
2268 * the first matching target phandle, with a reference held.
2270 * Return: 0 on success or a standard error code on failure.
2272 int of_map_rid(struct device_node
*np
, u32 rid
,
2273 const char *map_name
, const char *map_mask_name
,
2274 struct device_node
**target
, u32
*id_out
)
2276 u32 map_mask
, masked_rid
;
2278 const __be32
*map
= NULL
;
2280 if (!np
|| !map_name
|| (!target
&& !id_out
))
2283 map
= of_get_property(np
, map_name
, &map_len
);
2287 /* Otherwise, no map implies no translation */
2292 if (!map_len
|| map_len
% (4 * sizeof(*map
))) {
2293 pr_err("%pOF: Error: Bad %s length: %d\n", np
,
2298 /* The default is to select all bits. */
2299 map_mask
= 0xffffffff;
2302 * Can be overridden by "{iommu,msi}-map-mask" property.
2303 * If of_property_read_u32() fails, the default is used.
2306 of_property_read_u32(np
, map_mask_name
, &map_mask
);
2308 masked_rid
= map_mask
& rid
;
2309 for ( ; map_len
> 0; map_len
-= 4 * sizeof(*map
), map
+= 4) {
2310 struct device_node
*phandle_node
;
2311 u32 rid_base
= be32_to_cpup(map
+ 0);
2312 u32 phandle
= be32_to_cpup(map
+ 1);
2313 u32 out_base
= be32_to_cpup(map
+ 2);
2314 u32 rid_len
= be32_to_cpup(map
+ 3);
2316 if (rid_base
& ~map_mask
) {
2317 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
2318 np
, map_name
, map_name
,
2319 map_mask
, rid_base
);
2323 if (masked_rid
< rid_base
|| masked_rid
>= rid_base
+ rid_len
)
2326 phandle_node
= of_find_node_by_phandle(phandle
);
2332 of_node_put(phandle_node
);
2334 *target
= phandle_node
;
2336 if (*target
!= phandle_node
)
2341 *id_out
= masked_rid
- rid_base
+ out_base
;
2343 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
2344 np
, map_name
, map_mask
, rid_base
, out_base
,
2345 rid_len
, rid
, masked_rid
- rid_base
+ out_base
);
2349 pr_info("%pOF: no %s translation for rid 0x%x on %pOF\n", np
, map_name
,
2350 rid
, target
&& *target
? *target
: NULL
);
2352 /* Bypasses translation */
2357 EXPORT_SYMBOL_GPL(of_map_rid
);