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 int of_n_addr_cells(struct device_node
*np
)
89 if (!of_property_read_u32(np
, "#address-cells", &cells
))
92 /* No #address-cells property for the root node */
93 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT
;
95 EXPORT_SYMBOL(of_n_addr_cells
);
97 int of_n_size_cells(struct device_node
*np
)
104 if (!of_property_read_u32(np
, "#size-cells", &cells
))
106 } while (np
->parent
);
107 /* No #size-cells property for the root node */
108 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT
;
110 EXPORT_SYMBOL(of_n_size_cells
);
113 int __weak
of_node_to_nid(struct device_node
*np
)
119 static struct device_node
**phandle_cache
;
120 static u32 phandle_cache_mask
;
123 * Assumptions behind phandle_cache implementation:
124 * - phandle property values are in a contiguous range of 1..n
126 * If the assumptions do not hold, then
127 * - the phandle lookup overhead reduction provided by the cache
128 * will likely be less
130 void of_populate_phandle_cache(void)
134 struct device_node
*np
;
137 raw_spin_lock_irqsave(&devtree_lock
, flags
);
139 kfree(phandle_cache
);
140 phandle_cache
= NULL
;
142 for_each_of_allnodes(np
)
143 if (np
->phandle
&& np
->phandle
!= OF_PHANDLE_ILLEGAL
)
149 cache_entries
= roundup_pow_of_two(phandles
);
150 phandle_cache_mask
= cache_entries
- 1;
152 phandle_cache
= kcalloc(cache_entries
, sizeof(*phandle_cache
),
157 for_each_of_allnodes(np
)
158 if (np
->phandle
&& np
->phandle
!= OF_PHANDLE_ILLEGAL
)
159 phandle_cache
[np
->phandle
& phandle_cache_mask
] = np
;
162 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
165 int of_free_phandle_cache(void)
169 raw_spin_lock_irqsave(&devtree_lock
, flags
);
171 kfree(phandle_cache
);
172 phandle_cache
= NULL
;
174 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
178 #if !defined(CONFIG_MODULES)
179 late_initcall_sync(of_free_phandle_cache
);
182 void __init
of_core_init(void)
184 struct device_node
*np
;
186 of_populate_phandle_cache();
188 /* Create the kset, and register existing nodes */
189 mutex_lock(&of_mutex
);
190 of_kset
= kset_create_and_add("devicetree", NULL
, firmware_kobj
);
192 mutex_unlock(&of_mutex
);
193 pr_err("failed to register existing nodes\n");
196 for_each_of_allnodes(np
)
197 __of_attach_node_sysfs(np
);
198 mutex_unlock(&of_mutex
);
200 /* Symlink in /proc as required by userspace ABI */
202 proc_symlink("device-tree", NULL
, "/sys/firmware/devicetree/base");
205 static struct property
*__of_find_property(const struct device_node
*np
,
206 const char *name
, int *lenp
)
213 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
214 if (of_prop_cmp(pp
->name
, name
) == 0) {
224 struct property
*of_find_property(const struct device_node
*np
,
231 raw_spin_lock_irqsave(&devtree_lock
, flags
);
232 pp
= __of_find_property(np
, name
, lenp
);
233 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
237 EXPORT_SYMBOL(of_find_property
);
239 struct device_node
*__of_find_all_nodes(struct device_node
*prev
)
241 struct device_node
*np
;
244 } else if (prev
->child
) {
247 /* Walk back up looking for a sibling, or the end of the structure */
249 while (np
->parent
&& !np
->sibling
)
251 np
= np
->sibling
; /* Might be null at the end of the tree */
257 * of_find_all_nodes - Get next node in global list
258 * @prev: Previous node or NULL to start iteration
259 * of_node_put() will be called on it
261 * Returns a node pointer with refcount incremented, use
262 * of_node_put() on it when done.
264 struct device_node
*of_find_all_nodes(struct device_node
*prev
)
266 struct device_node
*np
;
269 raw_spin_lock_irqsave(&devtree_lock
, flags
);
270 np
= __of_find_all_nodes(prev
);
273 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
276 EXPORT_SYMBOL(of_find_all_nodes
);
279 * Find a property with a given name for a given node
280 * and return the value.
282 const void *__of_get_property(const struct device_node
*np
,
283 const char *name
, int *lenp
)
285 struct property
*pp
= __of_find_property(np
, name
, lenp
);
287 return pp
? pp
->value
: NULL
;
291 * Find a property with a given name for a given node
292 * and return the value.
294 const void *of_get_property(const struct device_node
*np
, const char *name
,
297 struct property
*pp
= of_find_property(np
, name
, lenp
);
299 return pp
? pp
->value
: NULL
;
301 EXPORT_SYMBOL(of_get_property
);
304 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
306 * @cpu: logical cpu index of a core/thread
307 * @phys_id: physical identifier of a core/thread
309 * CPU logical to physical index mapping is architecture specific.
310 * However this __weak function provides a default match of physical
311 * id to logical cpu index. phys_id provided here is usually values read
312 * from the device tree which must match the hardware internal registers.
314 * Returns true if the physical identifier and the logical cpu index
315 * correspond to the same core/thread, false otherwise.
317 bool __weak
arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
319 return (u32
)phys_id
== cpu
;
323 * Checks if the given "prop_name" property holds the physical id of the
324 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
325 * NULL, local thread number within the core is returned in it.
327 static bool __of_find_n_match_cpu_property(struct device_node
*cpun
,
328 const char *prop_name
, int cpu
, unsigned int *thread
)
331 int ac
, prop_len
, tid
;
334 ac
= of_n_addr_cells(cpun
);
335 cell
= of_get_property(cpun
, prop_name
, &prop_len
);
336 if (!cell
&& !ac
&& arch_match_cpu_phys_id(cpu
, 0))
340 prop_len
/= sizeof(*cell
) * ac
;
341 for (tid
= 0; tid
< prop_len
; tid
++) {
342 hwid
= of_read_number(cell
, ac
);
343 if (arch_match_cpu_phys_id(cpu
, hwid
)) {
354 * arch_find_n_match_cpu_physical_id - See if the given device node is
355 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
356 * else false. If 'thread' is non-NULL, the local thread number within the
357 * core is returned in it.
359 bool __weak
arch_find_n_match_cpu_physical_id(struct device_node
*cpun
,
360 int cpu
, unsigned int *thread
)
362 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
363 * for thread ids on PowerPC. If it doesn't exist fallback to
364 * standard "reg" property.
366 if (IS_ENABLED(CONFIG_PPC
) &&
367 __of_find_n_match_cpu_property(cpun
,
368 "ibm,ppc-interrupt-server#s",
372 return __of_find_n_match_cpu_property(cpun
, "reg", cpu
, thread
);
376 * of_get_cpu_node - Get device node associated with the given logical CPU
378 * @cpu: CPU number(logical index) for which device node is required
379 * @thread: if not NULL, local thread number within the physical core is
382 * The main purpose of this function is to retrieve the device node for the
383 * given logical CPU index. It should be used to initialize the of_node in
384 * cpu device. Once of_node in cpu device is populated, all the further
385 * references can use that instead.
387 * CPU logical to physical index mapping is architecture specific and is built
388 * before booting secondary cores. This function uses arch_match_cpu_phys_id
389 * which can be overridden by architecture specific implementation.
391 * Returns a node pointer for the logical cpu with refcount incremented, use
392 * of_node_put() on it when done. Returns NULL if not found.
394 struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
)
396 struct device_node
*cpun
;
398 for_each_of_cpu_node(cpun
) {
399 if (arch_find_n_match_cpu_physical_id(cpun
, cpu
, thread
))
404 EXPORT_SYMBOL(of_get_cpu_node
);
407 * of_cpu_node_to_id: Get the logical CPU number for a given device_node
409 * @cpu_node: Pointer to the device_node for CPU.
411 * Returns the logical CPU number of the given CPU device_node.
412 * Returns -ENODEV if the CPU is not found.
414 int of_cpu_node_to_id(struct device_node
*cpu_node
)
418 struct device_node
*np
;
420 for_each_possible_cpu(cpu
) {
421 np
= of_cpu_device_node_get(cpu
);
422 found
= (cpu_node
== np
);
430 EXPORT_SYMBOL(of_cpu_node_to_id
);
433 * __of_device_is_compatible() - Check if the node matches given constraints
434 * @device: pointer to node
435 * @compat: required compatible string, NULL or "" for any match
436 * @type: required device_type value, NULL or "" for any match
437 * @name: required node name, NULL or "" for any match
439 * Checks if the given @compat, @type and @name strings match the
440 * properties of the given @device. A constraints can be skipped by
441 * passing NULL or an empty string as the constraint.
443 * Returns 0 for no match, and a positive integer on match. The return
444 * value is a relative score with larger values indicating better
445 * matches. The score is weighted for the most specific compatible value
446 * to get the highest score. Matching type is next, followed by matching
447 * name. Practically speaking, this results in the following priority
450 * 1. specific compatible && type && name
451 * 2. specific compatible && type
452 * 3. specific compatible && name
453 * 4. specific compatible
454 * 5. general compatible && type && name
455 * 6. general compatible && type
456 * 7. general compatible && name
457 * 8. general compatible
462 static int __of_device_is_compatible(const struct device_node
*device
,
463 const char *compat
, const char *type
, const char *name
)
465 struct property
*prop
;
467 int index
= 0, score
= 0;
469 /* Compatible match has highest priority */
470 if (compat
&& compat
[0]) {
471 prop
= __of_find_property(device
, "compatible", NULL
);
472 for (cp
= of_prop_next_string(prop
, NULL
); cp
;
473 cp
= of_prop_next_string(prop
, cp
), index
++) {
474 if (of_compat_cmp(cp
, compat
, strlen(compat
)) == 0) {
475 score
= INT_MAX
/2 - (index
<< 2);
483 /* Matching type is better than matching name */
484 if (type
&& type
[0]) {
485 if (!device
->type
|| of_node_cmp(type
, device
->type
))
490 /* Matching name is a bit better than not */
491 if (name
&& name
[0]) {
492 if (!device
->name
|| of_node_cmp(name
, device
->name
))
500 /** Checks if the given "compat" string matches one of the strings in
501 * the device's "compatible" property
503 int of_device_is_compatible(const struct device_node
*device
,
509 raw_spin_lock_irqsave(&devtree_lock
, flags
);
510 res
= __of_device_is_compatible(device
, compat
, NULL
, NULL
);
511 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
514 EXPORT_SYMBOL(of_device_is_compatible
);
516 /** Checks if the device is compatible with any of the entries in
517 * a NULL terminated array of strings. Returns the best match
520 int of_device_compatible_match(struct device_node
*device
,
521 const char *const *compat
)
523 unsigned int tmp
, score
= 0;
529 tmp
= of_device_is_compatible(device
, *compat
);
539 * of_machine_is_compatible - Test root of device tree for a given compatible value
540 * @compat: compatible string to look for in root node's compatible property.
542 * Returns a positive integer if the root node has the given value in its
543 * compatible property.
545 int of_machine_is_compatible(const char *compat
)
547 struct device_node
*root
;
550 root
= of_find_node_by_path("/");
552 rc
= of_device_is_compatible(root
, compat
);
557 EXPORT_SYMBOL(of_machine_is_compatible
);
560 * __of_device_is_available - check if a device is available for use
562 * @device: Node to check for availability, with locks already held
564 * Returns true if the status property is absent or set to "okay" or "ok",
567 static bool __of_device_is_available(const struct device_node
*device
)
575 status
= __of_get_property(device
, "status", &statlen
);
580 if (!strcmp(status
, "okay") || !strcmp(status
, "ok"))
588 * of_device_is_available - check if a device is available for use
590 * @device: Node to check for availability
592 * Returns true if the status property is absent or set to "okay" or "ok",
595 bool of_device_is_available(const struct device_node
*device
)
600 raw_spin_lock_irqsave(&devtree_lock
, flags
);
601 res
= __of_device_is_available(device
);
602 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
606 EXPORT_SYMBOL(of_device_is_available
);
609 * of_device_is_big_endian - check if a device has BE registers
611 * @device: Node to check for endianness
613 * Returns true if the device has a "big-endian" property, or if the kernel
614 * was compiled for BE *and* the device has a "native-endian" property.
615 * Returns false otherwise.
617 * Callers would nominally use ioread32be/iowrite32be if
618 * of_device_is_big_endian() == true, or readl/writel otherwise.
620 bool of_device_is_big_endian(const struct device_node
*device
)
622 if (of_property_read_bool(device
, "big-endian"))
624 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
) &&
625 of_property_read_bool(device
, "native-endian"))
629 EXPORT_SYMBOL(of_device_is_big_endian
);
632 * of_get_parent - Get a node's parent if any
633 * @node: Node to get parent
635 * Returns a node pointer with refcount incremented, use
636 * of_node_put() on it when done.
638 struct device_node
*of_get_parent(const struct device_node
*node
)
640 struct device_node
*np
;
646 raw_spin_lock_irqsave(&devtree_lock
, flags
);
647 np
= of_node_get(node
->parent
);
648 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
651 EXPORT_SYMBOL(of_get_parent
);
654 * of_get_next_parent - Iterate to a node's parent
655 * @node: Node to get parent of
657 * This is like of_get_parent() except that it drops the
658 * refcount on the passed node, making it suitable for iterating
659 * through a node's parents.
661 * Returns a node pointer with refcount incremented, use
662 * of_node_put() on it when done.
664 struct device_node
*of_get_next_parent(struct device_node
*node
)
666 struct device_node
*parent
;
672 raw_spin_lock_irqsave(&devtree_lock
, flags
);
673 parent
= of_node_get(node
->parent
);
675 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
678 EXPORT_SYMBOL(of_get_next_parent
);
680 static struct device_node
*__of_get_next_child(const struct device_node
*node
,
681 struct device_node
*prev
)
683 struct device_node
*next
;
688 next
= prev
? prev
->sibling
: node
->child
;
689 for (; next
; next
= next
->sibling
)
690 if (of_node_get(next
))
695 #define __for_each_child_of_node(parent, child) \
696 for (child = __of_get_next_child(parent, NULL); child != NULL; \
697 child = __of_get_next_child(parent, child))
700 * of_get_next_child - Iterate a node childs
702 * @prev: previous child of the parent node, or NULL to get first
704 * Returns a node pointer with refcount incremented, use of_node_put() on
705 * it when done. Returns NULL when prev is the last child. Decrements the
708 struct device_node
*of_get_next_child(const struct device_node
*node
,
709 struct device_node
*prev
)
711 struct device_node
*next
;
714 raw_spin_lock_irqsave(&devtree_lock
, flags
);
715 next
= __of_get_next_child(node
, prev
);
716 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
719 EXPORT_SYMBOL(of_get_next_child
);
722 * of_get_next_available_child - Find the next available child node
724 * @prev: previous child of the parent node, or NULL to get first
726 * This function is like of_get_next_child(), except that it
727 * automatically skips any disabled nodes (i.e. status = "disabled").
729 struct device_node
*of_get_next_available_child(const struct device_node
*node
,
730 struct device_node
*prev
)
732 struct device_node
*next
;
738 raw_spin_lock_irqsave(&devtree_lock
, flags
);
739 next
= prev
? prev
->sibling
: node
->child
;
740 for (; next
; next
= next
->sibling
) {
741 if (!__of_device_is_available(next
))
743 if (of_node_get(next
))
747 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
750 EXPORT_SYMBOL(of_get_next_available_child
);
753 * of_get_next_cpu_node - Iterate on cpu nodes
754 * @prev: previous child of the /cpus node, or NULL to get first
756 * Returns a cpu node pointer with refcount incremented, use of_node_put()
757 * on it when done. Returns NULL when prev is the last child. Decrements
758 * the refcount of prev.
760 struct device_node
*of_get_next_cpu_node(struct device_node
*prev
)
762 struct device_node
*next
= NULL
;
764 struct device_node
*node
;
767 node
= of_find_node_by_path("/cpus");
769 raw_spin_lock_irqsave(&devtree_lock
, flags
);
771 next
= prev
->sibling
;
776 for (; next
; next
= next
->sibling
) {
777 if (!(of_node_name_eq(next
, "cpu") ||
778 (next
->type
&& !of_node_cmp(next
->type
, "cpu"))))
780 if (of_node_get(next
))
784 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
787 EXPORT_SYMBOL(of_get_next_cpu_node
);
790 * of_get_compatible_child - Find compatible child node
791 * @parent: parent node
792 * @compatible: compatible string
794 * Lookup child node whose compatible property contains the given compatible
797 * Returns a node pointer with refcount incremented, use of_node_put() on it
798 * when done; or NULL if not found.
800 struct device_node
*of_get_compatible_child(const struct device_node
*parent
,
801 const char *compatible
)
803 struct device_node
*child
;
805 for_each_child_of_node(parent
, child
) {
806 if (of_device_is_compatible(child
, compatible
))
812 EXPORT_SYMBOL(of_get_compatible_child
);
815 * of_get_child_by_name - Find the child node by name for a given parent
817 * @name: child name to look for.
819 * This function looks for child node for given matching name
821 * Returns a node pointer if found, with refcount incremented, use
822 * of_node_put() on it when done.
823 * Returns NULL if node is not found.
825 struct device_node
*of_get_child_by_name(const struct device_node
*node
,
828 struct device_node
*child
;
830 for_each_child_of_node(node
, child
)
831 if (child
->name
&& (of_node_cmp(child
->name
, name
) == 0))
835 EXPORT_SYMBOL(of_get_child_by_name
);
837 struct device_node
*__of_find_node_by_path(struct device_node
*parent
,
840 struct device_node
*child
;
843 len
= strcspn(path
, "/:");
847 __for_each_child_of_node(parent
, child
) {
848 const char *name
= kbasename(child
->full_name
);
849 if (strncmp(path
, name
, len
) == 0 && (strlen(name
) == len
))
855 struct device_node
*__of_find_node_by_full_path(struct device_node
*node
,
858 const char *separator
= strchr(path
, ':');
860 while (node
&& *path
== '/') {
861 struct device_node
*tmp
= node
;
863 path
++; /* Increment past '/' delimiter */
864 node
= __of_find_node_by_path(node
, path
);
866 path
= strchrnul(path
, '/');
867 if (separator
&& separator
< path
)
874 * of_find_node_opts_by_path - Find a node matching a full OF path
875 * @path: Either the full path to match, or if the path does not
876 * start with '/', the name of a property of the /aliases
877 * node (an alias). In the case of an alias, the node
878 * matching the alias' value will be returned.
879 * @opts: Address of a pointer into which to store the start of
880 * an options string appended to the end of the path with
886 * foo/bar Valid alias + relative path
888 * Returns a node pointer with refcount incremented, use
889 * of_node_put() on it when done.
891 struct device_node
*of_find_node_opts_by_path(const char *path
, const char **opts
)
893 struct device_node
*np
= NULL
;
896 const char *separator
= strchr(path
, ':');
899 *opts
= separator
? separator
+ 1 : NULL
;
901 if (strcmp(path
, "/") == 0)
902 return of_node_get(of_root
);
904 /* The path could begin with an alias */
907 const char *p
= separator
;
910 p
= strchrnul(path
, '/');
913 /* of_aliases must not be NULL */
917 for_each_property_of_node(of_aliases
, pp
) {
918 if (strlen(pp
->name
) == len
&& !strncmp(pp
->name
, path
, len
)) {
919 np
= of_find_node_by_path(pp
->value
);
928 /* Step down the tree matching path components */
929 raw_spin_lock_irqsave(&devtree_lock
, flags
);
931 np
= of_node_get(of_root
);
932 np
= __of_find_node_by_full_path(np
, path
);
933 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
936 EXPORT_SYMBOL(of_find_node_opts_by_path
);
939 * of_find_node_by_name - Find a node by its "name" property
940 * @from: The node to start searching from or NULL; the node
941 * you pass will not be searched, only the next one
942 * will. Typically, you pass what the previous call
943 * returned. of_node_put() will be called on @from.
944 * @name: The name string to match against
946 * Returns a node pointer with refcount incremented, use
947 * of_node_put() on it when done.
949 struct device_node
*of_find_node_by_name(struct device_node
*from
,
952 struct device_node
*np
;
955 raw_spin_lock_irqsave(&devtree_lock
, flags
);
956 for_each_of_allnodes_from(from
, np
)
957 if (np
->name
&& (of_node_cmp(np
->name
, name
) == 0)
961 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
964 EXPORT_SYMBOL(of_find_node_by_name
);
967 * of_find_node_by_type - Find a node by its "device_type" property
968 * @from: The node to start searching from, or NULL to start searching
969 * the entire device tree. The node you pass will not be
970 * searched, only the next one will; typically, you pass
971 * what the previous call returned. of_node_put() will be
972 * called on from for you.
973 * @type: The type string to match against
975 * Returns a node pointer with refcount incremented, use
976 * of_node_put() on it when done.
978 struct device_node
*of_find_node_by_type(struct device_node
*from
,
981 struct device_node
*np
;
984 raw_spin_lock_irqsave(&devtree_lock
, flags
);
985 for_each_of_allnodes_from(from
, np
)
986 if (np
->type
&& (of_node_cmp(np
->type
, type
) == 0)
990 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
993 EXPORT_SYMBOL(of_find_node_by_type
);
996 * of_find_compatible_node - Find a node based on type and one of the
997 * tokens in its "compatible" property
998 * @from: The node to start searching from or NULL, the node
999 * you pass will not be searched, only the next one
1000 * will; typically, you pass what the previous call
1001 * returned. of_node_put() will be called on it
1002 * @type: The type string to match "device_type" or NULL to ignore
1003 * @compatible: The string to match to one of the tokens in the device
1004 * "compatible" list.
1006 * Returns a node pointer with refcount incremented, use
1007 * of_node_put() on it when done.
1009 struct device_node
*of_find_compatible_node(struct device_node
*from
,
1010 const char *type
, const char *compatible
)
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_device_is_compatible(np
, compatible
, type
, NULL
) &&
1021 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1024 EXPORT_SYMBOL(of_find_compatible_node
);
1027 * of_find_node_with_property - Find a node which has a property with
1029 * @from: The node to start searching from or NULL, the node
1030 * you pass will not be searched, only the next one
1031 * will; typically, you pass what the previous call
1032 * returned. of_node_put() will be called on it
1033 * @prop_name: The name of the property to look for.
1035 * Returns a node pointer with refcount incremented, use
1036 * of_node_put() on it when done.
1038 struct device_node
*of_find_node_with_property(struct device_node
*from
,
1039 const char *prop_name
)
1041 struct device_node
*np
;
1042 struct property
*pp
;
1043 unsigned long flags
;
1045 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1046 for_each_of_allnodes_from(from
, np
) {
1047 for (pp
= np
->properties
; pp
; pp
= pp
->next
) {
1048 if (of_prop_cmp(pp
->name
, prop_name
) == 0) {
1056 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1059 EXPORT_SYMBOL(of_find_node_with_property
);
1062 const struct of_device_id
*__of_match_node(const struct of_device_id
*matches
,
1063 const struct device_node
*node
)
1065 const struct of_device_id
*best_match
= NULL
;
1066 int score
, best_score
= 0;
1071 for (; matches
->name
[0] || matches
->type
[0] || matches
->compatible
[0]; matches
++) {
1072 score
= __of_device_is_compatible(node
, matches
->compatible
,
1073 matches
->type
, matches
->name
);
1074 if (score
> best_score
) {
1075 best_match
= matches
;
1084 * of_match_node - Tell if a device_node has a matching of_match structure
1085 * @matches: array of of device match structures to search in
1086 * @node: the of device structure to match against
1088 * Low level utility function used by device matching.
1090 const struct of_device_id
*of_match_node(const struct of_device_id
*matches
,
1091 const struct device_node
*node
)
1093 const struct of_device_id
*match
;
1094 unsigned long flags
;
1096 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1097 match
= __of_match_node(matches
, node
);
1098 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1101 EXPORT_SYMBOL(of_match_node
);
1104 * of_find_matching_node_and_match - Find a node based on an of_device_id
1106 * @from: The node to start searching from or NULL, the node
1107 * you pass will not be searched, only the next one
1108 * will; typically, you pass what the previous call
1109 * returned. of_node_put() will be called on it
1110 * @matches: array of of device match structures to search in
1111 * @match Updated to point at the matches entry which matched
1113 * Returns a node pointer with refcount incremented, use
1114 * of_node_put() on it when done.
1116 struct device_node
*of_find_matching_node_and_match(struct device_node
*from
,
1117 const struct of_device_id
*matches
,
1118 const struct of_device_id
**match
)
1120 struct device_node
*np
;
1121 const struct of_device_id
*m
;
1122 unsigned long flags
;
1127 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1128 for_each_of_allnodes_from(from
, np
) {
1129 m
= __of_match_node(matches
, np
);
1130 if (m
&& of_node_get(np
)) {
1137 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1140 EXPORT_SYMBOL(of_find_matching_node_and_match
);
1143 * of_modalias_node - Lookup appropriate modalias for a device node
1144 * @node: pointer to a device tree node
1145 * @modalias: Pointer to buffer that modalias value will be copied into
1146 * @len: Length of modalias value
1148 * Based on the value of the compatible property, this routine will attempt
1149 * to choose an appropriate modalias value for a particular device tree node.
1150 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1151 * from the first entry in the compatible list property.
1153 * This routine returns 0 on success, <0 on failure.
1155 int of_modalias_node(struct device_node
*node
, char *modalias
, int len
)
1157 const char *compatible
, *p
;
1160 compatible
= of_get_property(node
, "compatible", &cplen
);
1161 if (!compatible
|| strlen(compatible
) > cplen
)
1163 p
= strchr(compatible
, ',');
1164 strlcpy(modalias
, p
? p
+ 1 : compatible
, len
);
1167 EXPORT_SYMBOL_GPL(of_modalias_node
);
1170 * of_find_node_by_phandle - Find a node given a phandle
1171 * @handle: phandle of the node to find
1173 * Returns a node pointer with refcount incremented, use
1174 * of_node_put() on it when done.
1176 struct device_node
*of_find_node_by_phandle(phandle handle
)
1178 struct device_node
*np
= NULL
;
1179 unsigned long flags
;
1180 phandle masked_handle
;
1185 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1187 masked_handle
= handle
& phandle_cache_mask
;
1189 if (phandle_cache
) {
1190 if (phandle_cache
[masked_handle
] &&
1191 handle
== phandle_cache
[masked_handle
]->phandle
)
1192 np
= phandle_cache
[masked_handle
];
1196 for_each_of_allnodes(np
)
1197 if (np
->phandle
== handle
) {
1199 phandle_cache
[masked_handle
] = np
;
1205 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1208 EXPORT_SYMBOL(of_find_node_by_phandle
);
1210 void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
)
1213 printk("%s %pOF", msg
, args
->np
);
1214 for (i
= 0; i
< args
->args_count
; i
++) {
1215 const char delim
= i
? ',' : ':';
1217 pr_cont("%c%08x", delim
, args
->args
[i
]);
1222 int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
1223 const struct device_node
*np
,
1224 const char *list_name
,
1225 const char *cells_name
,
1231 memset(it
, 0, sizeof(*it
));
1233 list
= of_get_property(np
, list_name
, &size
);
1237 it
->cells_name
= cells_name
;
1238 it
->cell_count
= cell_count
;
1240 it
->list_end
= list
+ size
/ sizeof(*list
);
1241 it
->phandle_end
= list
;
1246 EXPORT_SYMBOL_GPL(of_phandle_iterator_init
);
1248 int of_phandle_iterator_next(struct of_phandle_iterator
*it
)
1253 of_node_put(it
->node
);
1257 if (!it
->cur
|| it
->phandle_end
>= it
->list_end
)
1260 it
->cur
= it
->phandle_end
;
1262 /* If phandle is 0, then it is an empty entry with no arguments. */
1263 it
->phandle
= be32_to_cpup(it
->cur
++);
1268 * Find the provider node and parse the #*-cells property to
1269 * determine the argument length.
1271 it
->node
= of_find_node_by_phandle(it
->phandle
);
1273 if (it
->cells_name
) {
1275 pr_err("%pOF: could not find phandle\n",
1280 if (of_property_read_u32(it
->node
, it
->cells_name
,
1282 pr_err("%pOF: could not get %s for %pOF\n",
1289 count
= it
->cell_count
;
1293 * Make sure that the arguments actually fit in the remaining
1294 * property data length
1296 if (it
->cur
+ count
> it
->list_end
) {
1297 pr_err("%pOF: arguments longer than property\n",
1303 it
->phandle_end
= it
->cur
+ count
;
1304 it
->cur_count
= count
;
1310 of_node_put(it
->node
);
1316 EXPORT_SYMBOL_GPL(of_phandle_iterator_next
);
1318 int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
1324 count
= it
->cur_count
;
1326 if (WARN_ON(size
< count
))
1329 for (i
= 0; i
< count
; i
++)
1330 args
[i
] = be32_to_cpup(it
->cur
++);
1335 static int __of_parse_phandle_with_args(const struct device_node
*np
,
1336 const char *list_name
,
1337 const char *cells_name
,
1338 int cell_count
, int index
,
1339 struct of_phandle_args
*out_args
)
1341 struct of_phandle_iterator it
;
1342 int rc
, cur_index
= 0;
1344 /* Loop over the phandles until all the requested entry is found */
1345 of_for_each_phandle(&it
, rc
, np
, list_name
, cells_name
, cell_count
) {
1347 * All of the error cases bail out of the loop, so at
1348 * this point, the parsing is successful. If the requested
1349 * index matches, then fill the out_args structure and return,
1350 * or return -ENOENT for an empty entry.
1353 if (cur_index
== index
) {
1360 c
= of_phandle_iterator_args(&it
,
1363 out_args
->np
= it
.node
;
1364 out_args
->args_count
= c
;
1366 of_node_put(it
.node
);
1369 /* Found it! return success */
1377 * Unlock node before returning result; will be one of:
1378 * -ENOENT : index is for empty phandle
1379 * -EINVAL : parsing error on data
1383 of_node_put(it
.node
);
1388 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1389 * @np: Pointer to device node holding phandle property
1390 * @phandle_name: Name of property holding a phandle value
1391 * @index: For properties holding a table of phandles, this is the index into
1394 * Returns the device_node pointer with refcount incremented. Use
1395 * of_node_put() on it when done.
1397 struct device_node
*of_parse_phandle(const struct device_node
*np
,
1398 const char *phandle_name
, int index
)
1400 struct of_phandle_args args
;
1405 if (__of_parse_phandle_with_args(np
, phandle_name
, NULL
, 0,
1411 EXPORT_SYMBOL(of_parse_phandle
);
1414 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1415 * @np: pointer to a device tree node containing a list
1416 * @list_name: property name that contains a list
1417 * @cells_name: property name that specifies phandles' arguments count
1418 * @index: index of a phandle to parse out
1419 * @out_args: optional pointer to output arguments structure (will be filled)
1421 * This function is useful to parse lists of phandles and their arguments.
1422 * Returns 0 on success and fills out_args, on error returns appropriate
1425 * Caller is responsible to call of_node_put() on the returned out_args->np
1431 * #list-cells = <2>;
1435 * #list-cells = <1>;
1439 * list = <&phandle1 1 2 &phandle2 3>;
1442 * To get a device_node of the `node2' node you may call this:
1443 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1445 int of_parse_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1446 const char *cells_name
, int index
,
1447 struct of_phandle_args
*out_args
)
1451 return __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0,
1454 EXPORT_SYMBOL(of_parse_phandle_with_args
);
1457 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1458 * @np: pointer to a device tree node containing a list
1459 * @list_name: property name that contains a list
1460 * @stem_name: stem of property names that specify phandles' arguments count
1461 * @index: index of a phandle to parse out
1462 * @out_args: optional pointer to output arguments structure (will be filled)
1464 * This function is useful to parse lists of phandles and their arguments.
1465 * Returns 0 on success and fills out_args, on error returns appropriate errno
1466 * value. The difference between this function and of_parse_phandle_with_args()
1467 * is that this API remaps a phandle if the node the phandle points to has
1468 * a <@stem_name>-map property.
1470 * Caller is responsible to call of_node_put() on the returned out_args->np
1476 * #list-cells = <2>;
1480 * #list-cells = <1>;
1484 * #list-cells = <1>;
1485 * list-map = <0 &phandle2 3>,
1487 * <2 &phandle1 5 1>;
1488 * list-map-mask = <0x3>;
1492 * list = <&phandle1 1 2 &phandle3 0>;
1495 * To get a device_node of the `node2' node you may call this:
1496 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1498 int of_parse_phandle_with_args_map(const struct device_node
*np
,
1499 const char *list_name
,
1500 const char *stem_name
,
1501 int index
, struct of_phandle_args
*out_args
)
1503 char *cells_name
, *map_name
= NULL
, *mask_name
= NULL
;
1504 char *pass_name
= NULL
;
1505 struct device_node
*cur
, *new = NULL
;
1506 const __be32
*map
, *mask
, *pass
;
1507 static const __be32 dummy_mask
[] = { [0 ... MAX_PHANDLE_ARGS
] = ~0 };
1508 static const __be32 dummy_pass
[] = { [0 ... MAX_PHANDLE_ARGS
] = 0 };
1509 __be32 initial_match_array
[MAX_PHANDLE_ARGS
];
1510 const __be32
*match_array
= initial_match_array
;
1511 int i
, ret
, map_len
, match
;
1512 u32 list_size
, new_size
;
1517 cells_name
= kasprintf(GFP_KERNEL
, "#%s-cells", stem_name
);
1522 map_name
= kasprintf(GFP_KERNEL
, "%s-map", stem_name
);
1526 mask_name
= kasprintf(GFP_KERNEL
, "%s-map-mask", stem_name
);
1530 pass_name
= kasprintf(GFP_KERNEL
, "%s-map-pass-thru", stem_name
);
1534 ret
= __of_parse_phandle_with_args(np
, list_name
, cells_name
, 0, index
,
1539 /* Get the #<list>-cells property */
1541 ret
= of_property_read_u32(cur
, cells_name
, &list_size
);
1545 /* Precalculate the match array - this simplifies match loop */
1546 for (i
= 0; i
< list_size
; i
++)
1547 initial_match_array
[i
] = cpu_to_be32(out_args
->args
[i
]);
1551 /* Get the <list>-map property */
1552 map
= of_get_property(cur
, map_name
, &map_len
);
1557 map_len
/= sizeof(u32
);
1559 /* Get the <list>-map-mask property (optional) */
1560 mask
= of_get_property(cur
, mask_name
, NULL
);
1563 /* Iterate through <list>-map property */
1565 while (map_len
> (list_size
+ 1) && !match
) {
1566 /* Compare specifiers */
1568 for (i
= 0; i
< list_size
; i
++, map_len
--)
1569 match
&= !((match_array
[i
] ^ *map
++) & mask
[i
]);
1572 new = of_find_node_by_phandle(be32_to_cpup(map
));
1576 /* Check if not found */
1580 if (!of_device_is_available(new))
1583 ret
= of_property_read_u32(new, cells_name
, &new_size
);
1587 /* Check for malformed properties */
1588 if (WARN_ON(new_size
> MAX_PHANDLE_ARGS
))
1590 if (map_len
< new_size
)
1593 /* Move forward by new node's #<list>-cells amount */
1595 map_len
-= new_size
;
1600 /* Get the <list>-map-pass-thru property (optional) */
1601 pass
= of_get_property(cur
, pass_name
, NULL
);
1606 * Successfully parsed a <list>-map translation; copy new
1607 * specifier into the out_args structure, keeping the
1608 * bits specified in <list>-map-pass-thru.
1610 match_array
= map
- new_size
;
1611 for (i
= 0; i
< new_size
; i
++) {
1612 __be32 val
= *(map
- new_size
+ i
);
1614 if (i
< list_size
) {
1616 val
|= cpu_to_be32(out_args
->args
[i
]) & pass
[i
];
1619 out_args
->args
[i
] = be32_to_cpu(val
);
1621 out_args
->args_count
= list_size
= new_size
;
1622 /* Iterate again with new provider */
1638 EXPORT_SYMBOL(of_parse_phandle_with_args_map
);
1641 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1642 * @np: pointer to a device tree node containing a list
1643 * @list_name: property name that contains a list
1644 * @cell_count: number of argument cells following the phandle
1645 * @index: index of a phandle to parse out
1646 * @out_args: optional pointer to output arguments structure (will be filled)
1648 * This function is useful to parse lists of phandles and their arguments.
1649 * Returns 0 on success and fills out_args, on error returns appropriate
1652 * Caller is responsible to call of_node_put() on the returned out_args->np
1664 * list = <&phandle1 0 2 &phandle2 2 3>;
1667 * To get a device_node of the `node2' node you may call this:
1668 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1670 int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
1671 const char *list_name
, int cell_count
,
1672 int index
, struct of_phandle_args
*out_args
)
1676 return __of_parse_phandle_with_args(np
, list_name
, NULL
, cell_count
,
1679 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args
);
1682 * of_count_phandle_with_args() - Find the number of phandles references in a property
1683 * @np: pointer to a device tree node containing a list
1684 * @list_name: property name that contains a list
1685 * @cells_name: property name that specifies phandles' arguments count
1687 * Returns the number of phandle + argument tuples within a property. It
1688 * is a typical pattern to encode a list of phandle and variable
1689 * arguments into a single property. The number of arguments is encoded
1690 * by a property in the phandle-target node. For example, a gpios
1691 * property would contain a list of GPIO specifies consisting of a
1692 * phandle and 1 or more arguments. The number of arguments are
1693 * determined by the #gpio-cells property in the node pointed to by the
1696 int of_count_phandle_with_args(const struct device_node
*np
, const char *list_name
,
1697 const char *cells_name
)
1699 struct of_phandle_iterator it
;
1700 int rc
, cur_index
= 0;
1702 rc
= of_phandle_iterator_init(&it
, np
, list_name
, cells_name
, 0);
1706 while ((rc
= of_phandle_iterator_next(&it
)) == 0)
1714 EXPORT_SYMBOL(of_count_phandle_with_args
);
1717 * __of_add_property - Add a property to a node without lock operations
1719 int __of_add_property(struct device_node
*np
, struct property
*prop
)
1721 struct property
**next
;
1724 next
= &np
->properties
;
1726 if (strcmp(prop
->name
, (*next
)->name
) == 0)
1727 /* duplicate ! don't insert it */
1730 next
= &(*next
)->next
;
1738 * of_add_property - Add a property to a node
1740 int of_add_property(struct device_node
*np
, struct property
*prop
)
1742 unsigned long flags
;
1745 mutex_lock(&of_mutex
);
1747 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1748 rc
= __of_add_property(np
, prop
);
1749 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1752 __of_add_property_sysfs(np
, prop
);
1754 mutex_unlock(&of_mutex
);
1757 of_property_notify(OF_RECONFIG_ADD_PROPERTY
, np
, prop
, NULL
);
1762 int __of_remove_property(struct device_node
*np
, struct property
*prop
)
1764 struct property
**next
;
1766 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1773 /* found the node */
1775 prop
->next
= np
->deadprops
;
1776 np
->deadprops
= prop
;
1782 * of_remove_property - Remove a property from a node.
1784 * Note that we don't actually remove it, since we have given out
1785 * who-knows-how-many pointers to the data using get-property.
1786 * Instead we just move the property to the "dead properties"
1787 * list, so it won't be found any more.
1789 int of_remove_property(struct device_node
*np
, struct property
*prop
)
1791 unsigned long flags
;
1797 mutex_lock(&of_mutex
);
1799 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1800 rc
= __of_remove_property(np
, prop
);
1801 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1804 __of_remove_property_sysfs(np
, prop
);
1806 mutex_unlock(&of_mutex
);
1809 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
, NULL
);
1814 int __of_update_property(struct device_node
*np
, struct property
*newprop
,
1815 struct property
**oldpropp
)
1817 struct property
**next
, *oldprop
;
1819 for (next
= &np
->properties
; *next
; next
= &(*next
)->next
) {
1820 if (of_prop_cmp((*next
)->name
, newprop
->name
) == 0)
1823 *oldpropp
= oldprop
= *next
;
1826 /* replace the node */
1827 newprop
->next
= oldprop
->next
;
1829 oldprop
->next
= np
->deadprops
;
1830 np
->deadprops
= oldprop
;
1833 newprop
->next
= NULL
;
1841 * of_update_property - Update a property in a node, if the property does
1842 * not exist, add it.
1844 * Note that we don't actually remove it, since we have given out
1845 * who-knows-how-many pointers to the data using get-property.
1846 * Instead we just move the property to the "dead properties" list,
1847 * and add the new property to the property list
1849 int of_update_property(struct device_node
*np
, struct property
*newprop
)
1851 struct property
*oldprop
;
1852 unsigned long flags
;
1858 mutex_lock(&of_mutex
);
1860 raw_spin_lock_irqsave(&devtree_lock
, flags
);
1861 rc
= __of_update_property(np
, newprop
, &oldprop
);
1862 raw_spin_unlock_irqrestore(&devtree_lock
, flags
);
1865 __of_update_property_sysfs(np
, newprop
, oldprop
);
1867 mutex_unlock(&of_mutex
);
1870 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY
, np
, newprop
, oldprop
);
1875 static void of_alias_add(struct alias_prop
*ap
, struct device_node
*np
,
1876 int id
, const char *stem
, int stem_len
)
1880 strncpy(ap
->stem
, stem
, stem_len
);
1881 ap
->stem
[stem_len
] = 0;
1882 list_add_tail(&ap
->link
, &aliases_lookup
);
1883 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1884 ap
->alias
, ap
->stem
, ap
->id
, np
);
1888 * of_alias_scan - Scan all properties of the 'aliases' node
1890 * The function scans all the properties of the 'aliases' node and populates
1891 * the global lookup table with the properties. It returns the
1892 * number of alias properties found, or an error code in case of failure.
1894 * @dt_alloc: An allocator that provides a virtual address to memory
1895 * for storing the resulting tree
1897 void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
))
1899 struct property
*pp
;
1901 of_aliases
= of_find_node_by_path("/aliases");
1902 of_chosen
= of_find_node_by_path("/chosen");
1903 if (of_chosen
== NULL
)
1904 of_chosen
= of_find_node_by_path("/chosen@0");
1907 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
1908 const char *name
= NULL
;
1910 if (of_property_read_string(of_chosen
, "stdout-path", &name
))
1911 of_property_read_string(of_chosen
, "linux,stdout-path",
1913 if (IS_ENABLED(CONFIG_PPC
) && !name
)
1914 of_property_read_string(of_aliases
, "stdout", &name
);
1916 of_stdout
= of_find_node_opts_by_path(name
, &of_stdout_options
);
1922 for_each_property_of_node(of_aliases
, pp
) {
1923 const char *start
= pp
->name
;
1924 const char *end
= start
+ strlen(start
);
1925 struct device_node
*np
;
1926 struct alias_prop
*ap
;
1929 /* Skip those we do not want to proceed */
1930 if (!strcmp(pp
->name
, "name") ||
1931 !strcmp(pp
->name
, "phandle") ||
1932 !strcmp(pp
->name
, "linux,phandle"))
1935 np
= of_find_node_by_path(pp
->value
);
1939 /* walk the alias backwards to extract the id and work out
1940 * the 'stem' string */
1941 while (isdigit(*(end
-1)) && end
> start
)
1945 if (kstrtoint(end
, 10, &id
) < 0)
1948 /* Allocate an alias_prop with enough space for the stem */
1949 ap
= dt_alloc(sizeof(*ap
) + len
+ 1, __alignof__(*ap
));
1952 memset(ap
, 0, sizeof(*ap
) + len
+ 1);
1954 of_alias_add(ap
, np
, id
, start
, len
);
1959 * of_alias_get_id - Get alias id for the given device_node
1960 * @np: Pointer to the given device_node
1961 * @stem: Alias stem of the given device_node
1963 * The function travels the lookup table to get the alias id for the given
1964 * device_node and alias stem. It returns the alias id if found.
1966 int of_alias_get_id(struct device_node
*np
, const char *stem
)
1968 struct alias_prop
*app
;
1971 mutex_lock(&of_mutex
);
1972 list_for_each_entry(app
, &aliases_lookup
, link
) {
1973 if (strcmp(app
->stem
, stem
) != 0)
1976 if (np
== app
->np
) {
1981 mutex_unlock(&of_mutex
);
1985 EXPORT_SYMBOL_GPL(of_alias_get_id
);
1988 * of_alias_get_alias_list - Get alias list for the given device driver
1989 * @matches: Array of OF device match structures to search in
1990 * @stem: Alias stem of the given device_node
1991 * @bitmap: Bitmap field pointer
1992 * @nbits: Maximum number of alias IDs which can be recorded in bitmap
1994 * The function travels the lookup table to record alias ids for the given
1995 * device match structures and alias stem.
1997 * Return: 0 or -ENOSYS when !CONFIG_OF or
1998 * -EOVERFLOW if alias ID is greater then allocated nbits
2000 int of_alias_get_alias_list(const struct of_device_id
*matches
,
2001 const char *stem
, unsigned long *bitmap
,
2004 struct alias_prop
*app
;
2007 /* Zero bitmap field to make sure that all the time it is clean */
2008 bitmap_zero(bitmap
, nbits
);
2010 mutex_lock(&of_mutex
);
2011 pr_debug("%s: Looking for stem: %s\n", __func__
, stem
);
2012 list_for_each_entry(app
, &aliases_lookup
, link
) {
2013 pr_debug("%s: stem: %s, id: %d\n",
2014 __func__
, app
->stem
, app
->id
);
2016 if (strcmp(app
->stem
, stem
) != 0) {
2017 pr_debug("%s: stem comparison didn't pass %s\n",
2018 __func__
, app
->stem
);
2022 if (of_match_node(matches
, app
->np
)) {
2023 pr_debug("%s: Allocated ID %d\n", __func__
, app
->id
);
2025 if (app
->id
>= nbits
) {
2026 pr_warn("%s: ID %d >= than bitmap field %d\n",
2027 __func__
, app
->id
, nbits
);
2030 set_bit(app
->id
, bitmap
);
2034 mutex_unlock(&of_mutex
);
2038 EXPORT_SYMBOL_GPL(of_alias_get_alias_list
);
2041 * of_alias_get_highest_id - Get highest alias id for the given stem
2042 * @stem: Alias stem to be examined
2044 * The function travels the lookup table to get the highest alias id for the
2045 * given alias stem. It returns the alias id if found.
2047 int of_alias_get_highest_id(const char *stem
)
2049 struct alias_prop
*app
;
2052 mutex_lock(&of_mutex
);
2053 list_for_each_entry(app
, &aliases_lookup
, link
) {
2054 if (strcmp(app
->stem
, stem
) != 0)
2060 mutex_unlock(&of_mutex
);
2064 EXPORT_SYMBOL_GPL(of_alias_get_highest_id
);
2067 * of_console_check() - Test and setup console for DT setup
2068 * @dn - Pointer to device node
2069 * @name - Name to use for preferred console without index. ex. "ttyS"
2070 * @index - Index to use for preferred console.
2072 * Check if the given device node matches the stdout-path property in the
2073 * /chosen node. If it does then register it as the preferred console and return
2074 * TRUE. Otherwise return FALSE.
2076 bool of_console_check(struct device_node
*dn
, char *name
, int index
)
2078 if (!dn
|| dn
!= of_stdout
|| console_set_on_cmdline
)
2082 * XXX: cast `options' to char pointer to suppress complication
2083 * warnings: printk, UART and console drivers expect char pointer.
2085 return !add_preferred_console(name
, index
, (char *)of_stdout_options
);
2087 EXPORT_SYMBOL_GPL(of_console_check
);
2090 * of_find_next_cache_node - Find a node's subsidiary cache
2091 * @np: node of type "cpu" or "cache"
2093 * Returns a node pointer with refcount incremented, use
2094 * of_node_put() on it when done. Caller should hold a reference
2097 struct device_node
*of_find_next_cache_node(const struct device_node
*np
)
2099 struct device_node
*child
, *cache_node
;
2101 cache_node
= of_parse_phandle(np
, "l2-cache", 0);
2103 cache_node
= of_parse_phandle(np
, "next-level-cache", 0);
2108 /* OF on pmac has nodes instead of properties named "l2-cache"
2109 * beneath CPU nodes.
2111 if (IS_ENABLED(CONFIG_PPC_PMAC
) && !strcmp(np
->type
, "cpu"))
2112 for_each_child_of_node(np
, child
)
2113 if (!strcmp(child
->type
, "cache"))
2120 * of_find_last_cache_level - Find the level at which the last cache is
2121 * present for the given logical cpu
2123 * @cpu: cpu number(logical index) for which the last cache level is needed
2125 * Returns the the level at which the last cache is present. It is exactly
2126 * same as the total number of cache levels for the given logical cpu.
2128 int of_find_last_cache_level(unsigned int cpu
)
2130 u32 cache_level
= 0;
2131 struct device_node
*prev
= NULL
, *np
= of_cpu_device_node_get(cpu
);
2136 np
= of_find_next_cache_node(np
);
2139 of_property_read_u32(prev
, "cache-level", &cache_level
);
2145 * of_map_rid - Translate a requester ID through a downstream mapping.
2146 * @np: root complex device node.
2147 * @rid: device requester ID to map.
2148 * @map_name: property name of the map to use.
2149 * @map_mask_name: optional property name of the mask to use.
2150 * @target: optional pointer to a target device node.
2151 * @id_out: optional pointer to receive the translated ID.
2153 * Given a device requester ID, look up the appropriate implementation-defined
2154 * platform ID and/or the target device which receives transactions on that
2155 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
2156 * @id_out may be NULL if only the other is required. If @target points to
2157 * a non-NULL device node pointer, only entries targeting that node will be
2158 * matched; if it points to a NULL value, it will receive the device node of
2159 * the first matching target phandle, with a reference held.
2161 * Return: 0 on success or a standard error code on failure.
2163 int of_map_rid(struct device_node
*np
, u32 rid
,
2164 const char *map_name
, const char *map_mask_name
,
2165 struct device_node
**target
, u32
*id_out
)
2167 u32 map_mask
, masked_rid
;
2169 const __be32
*map
= NULL
;
2171 if (!np
|| !map_name
|| (!target
&& !id_out
))
2174 map
= of_get_property(np
, map_name
, &map_len
);
2178 /* Otherwise, no map implies no translation */
2183 if (!map_len
|| map_len
% (4 * sizeof(*map
))) {
2184 pr_err("%pOF: Error: Bad %s length: %d\n", np
,
2189 /* The default is to select all bits. */
2190 map_mask
= 0xffffffff;
2193 * Can be overridden by "{iommu,msi}-map-mask" property.
2194 * If of_property_read_u32() fails, the default is used.
2197 of_property_read_u32(np
, map_mask_name
, &map_mask
);
2199 masked_rid
= map_mask
& rid
;
2200 for ( ; map_len
> 0; map_len
-= 4 * sizeof(*map
), map
+= 4) {
2201 struct device_node
*phandle_node
;
2202 u32 rid_base
= be32_to_cpup(map
+ 0);
2203 u32 phandle
= be32_to_cpup(map
+ 1);
2204 u32 out_base
= be32_to_cpup(map
+ 2);
2205 u32 rid_len
= be32_to_cpup(map
+ 3);
2207 if (rid_base
& ~map_mask
) {
2208 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
2209 np
, map_name
, map_name
,
2210 map_mask
, rid_base
);
2214 if (masked_rid
< rid_base
|| masked_rid
>= rid_base
+ rid_len
)
2217 phandle_node
= of_find_node_by_phandle(phandle
);
2223 of_node_put(phandle_node
);
2225 *target
= phandle_node
;
2227 if (*target
!= phandle_node
)
2232 *id_out
= masked_rid
- rid_base
+ out_base
;
2234 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
2235 np
, map_name
, map_mask
, rid_base
, out_base
,
2236 rid_len
, rid
, masked_rid
- rid_base
+ out_base
);
2240 pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
2241 np
, map_name
, rid
, target
&& *target
? *target
: NULL
);
2244 EXPORT_SYMBOL_GPL(of_map_rid
);