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_device_is_available(next))
782 if (of_node_get(next))
786 raw_spin_unlock_irqrestore(&devtree_lock, flags);
789 EXPORT_SYMBOL(of_get_next_cpu_node);
792 * of_get_compatible_child - Find compatible child node
793 * @parent: parent node
794 * @compatible: compatible string
796 * Lookup child node whose compatible property contains the given compatible
799 * Returns a node pointer with refcount incremented, use of_node_put() on it
800 * when done; or NULL if not found.
802 struct device_node *of_get_compatible_child(const struct device_node *parent,
803 const char *compatible)
805 struct device_node *child;
807 for_each_child_of_node(parent, child) {
808 if (of_device_is_compatible(child, compatible))
814 EXPORT_SYMBOL(of_get_compatible_child);
817 * of_get_child_by_name - Find the child node by name for a given parent
819 * @name: child name to look for.
821 * This function looks for child node for given matching name
823 * Returns a node pointer if found, with refcount incremented, use
824 * of_node_put() on it when done.
825 * Returns NULL if node is not found.
827 struct device_node *of_get_child_by_name(const struct device_node *node,
830 struct device_node *child;
832 for_each_child_of_node(node, child)
833 if (child->name && (of_node_cmp(child->name, name) == 0))
837 EXPORT_SYMBOL(of_get_child_by_name);
839 struct device_node *__of_find_node_by_path(struct device_node *parent,
842 struct device_node *child;
845 len = strcspn(path, "/:");
849 __for_each_child_of_node(parent, child) {
850 const char *name = kbasename(child->full_name);
851 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
857 struct device_node *__of_find_node_by_full_path(struct device_node *node,
860 const char *separator = strchr(path, ':');
862 while (node && *path == '/') {
863 struct device_node *tmp = node;
865 path++; /* Increment past '/' delimiter */
866 node = __of_find_node_by_path(node, path);
868 path = strchrnul(path, '/');
869 if (separator && separator < path)
876 * of_find_node_opts_by_path - Find a node matching a full OF path
877 * @path: Either the full path to match, or if the path does not
878 * start with '/', the name of a property of the /aliases
879 * node (an alias). In the case of an alias, the node
880 * matching the alias' value will be returned.
881 * @opts: Address of a pointer into which to store the start of
882 * an options string appended to the end of the path with
888 * foo/bar Valid alias + relative path
890 * Returns a node pointer with refcount incremented, use
891 * of_node_put() on it when done.
893 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
895 struct device_node *np = NULL;
898 const char *separator = strchr(path, ':');
901 *opts = separator ? separator + 1 : NULL;
903 if (strcmp(path, "/") == 0)
904 return of_node_get(of_root);
906 /* The path could begin with an alias */
909 const char *p = separator;
912 p = strchrnul(path, '/');
915 /* of_aliases must not be NULL */
919 for_each_property_of_node(of_aliases, pp) {
920 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
921 np = of_find_node_by_path(pp->value);
930 /* Step down the tree matching path components */
931 raw_spin_lock_irqsave(&devtree_lock, flags);
933 np = of_node_get(of_root);
934 np = __of_find_node_by_full_path(np, path);
935 raw_spin_unlock_irqrestore(&devtree_lock, flags);
938 EXPORT_SYMBOL(of_find_node_opts_by_path);
941 * of_find_node_by_name - Find a node by its "name" property
942 * @from: The node to start searching from or NULL; the node
943 * you pass will not be searched, only the next one
944 * will. Typically, you pass what the previous call
945 * returned. of_node_put() will be called on @from.
946 * @name: The name string to match against
948 * Returns a node pointer with refcount incremented, use
949 * of_node_put() on it when done.
951 struct device_node *of_find_node_by_name(struct device_node *from,
954 struct device_node *np;
957 raw_spin_lock_irqsave(&devtree_lock, flags);
958 for_each_of_allnodes_from(from, np)
959 if (np->name && (of_node_cmp(np->name, name) == 0)
963 raw_spin_unlock_irqrestore(&devtree_lock, flags);
966 EXPORT_SYMBOL(of_find_node_by_name);
969 * of_find_node_by_type - Find a node by its "device_type" property
970 * @from: The node to start searching from, or NULL to start searching
971 * the entire device tree. The node you pass will not be
972 * searched, only the next one will; typically, you pass
973 * what the previous call returned. of_node_put() will be
974 * called on from for you.
975 * @type: The type string to match against
977 * Returns a node pointer with refcount incremented, use
978 * of_node_put() on it when done.
980 struct device_node *of_find_node_by_type(struct device_node *from,
983 struct device_node *np;
986 raw_spin_lock_irqsave(&devtree_lock, flags);
987 for_each_of_allnodes_from(from, np)
988 if (np->type && (of_node_cmp(np->type, type) == 0)
992 raw_spin_unlock_irqrestore(&devtree_lock, flags);
995 EXPORT_SYMBOL(of_find_node_by_type);
998 * of_find_compatible_node - Find a node based on type and one of the
999 * tokens in its "compatible" 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 it
1004 * @type: The type string to match "device_type" or NULL to ignore
1005 * @compatible: The string to match to one of the tokens in the device
1006 * "compatible" list.
1008 * Returns a node pointer with refcount incremented, use
1009 * of_node_put() on it when done.
1011 struct device_node *of_find_compatible_node(struct device_node *from,
1012 const char *type, const char *compatible)
1014 struct device_node *np;
1015 unsigned long flags;
1017 raw_spin_lock_irqsave(&devtree_lock, flags);
1018 for_each_of_allnodes_from(from, np)
1019 if (__of_device_is_compatible(np, compatible, type, NULL) &&
1023 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1026 EXPORT_SYMBOL(of_find_compatible_node);
1029 * of_find_node_with_property - Find a node which has a property with
1031 * @from: The node to start searching from or NULL, the node
1032 * you pass will not be searched, only the next one
1033 * will; typically, you pass what the previous call
1034 * returned. of_node_put() will be called on it
1035 * @prop_name: The name of the property to look for.
1037 * Returns a node pointer with refcount incremented, use
1038 * of_node_put() on it when done.
1040 struct device_node *of_find_node_with_property(struct device_node *from,
1041 const char *prop_name)
1043 struct device_node *np;
1044 struct property *pp;
1045 unsigned long flags;
1047 raw_spin_lock_irqsave(&devtree_lock, flags);
1048 for_each_of_allnodes_from(from, np) {
1049 for (pp = np->properties; pp; pp = pp->next) {
1050 if (of_prop_cmp(pp->name, prop_name) == 0) {
1058 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1061 EXPORT_SYMBOL(of_find_node_with_property);
1064 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1065 const struct device_node *node)
1067 const struct of_device_id *best_match = NULL;
1068 int score, best_score = 0;
1073 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1074 score = __of_device_is_compatible(node, matches->compatible,
1075 matches->type, matches->name);
1076 if (score > best_score) {
1077 best_match = matches;
1086 * of_match_node - Tell if a device_node has a matching of_match structure
1087 * @matches: array of of device match structures to search in
1088 * @node: the of device structure to match against
1090 * Low level utility function used by device matching.
1092 const struct of_device_id *of_match_node(const struct of_device_id *matches,
1093 const struct device_node *node)
1095 const struct of_device_id *match;
1096 unsigned long flags;
1098 raw_spin_lock_irqsave(&devtree_lock, flags);
1099 match = __of_match_node(matches, node);
1100 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1103 EXPORT_SYMBOL(of_match_node);
1106 * of_find_matching_node_and_match - Find a node based on an of_device_id
1108 * @from: The node to start searching from or NULL, the node
1109 * you pass will not be searched, only the next one
1110 * will; typically, you pass what the previous call
1111 * returned. of_node_put() will be called on it
1112 * @matches: array of of device match structures to search in
1113 * @match Updated to point at the matches entry which matched
1115 * Returns a node pointer with refcount incremented, use
1116 * of_node_put() on it when done.
1118 struct device_node *of_find_matching_node_and_match(struct device_node *from,
1119 const struct of_device_id *matches,
1120 const struct of_device_id **match)
1122 struct device_node *np;
1123 const struct of_device_id *m;
1124 unsigned long flags;
1129 raw_spin_lock_irqsave(&devtree_lock, flags);
1130 for_each_of_allnodes_from(from, np) {
1131 m = __of_match_node(matches, np);
1132 if (m && of_node_get(np)) {
1139 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1142 EXPORT_SYMBOL(of_find_matching_node_and_match);
1145 * of_modalias_node - Lookup appropriate modalias for a device node
1146 * @node: pointer to a device tree node
1147 * @modalias: Pointer to buffer that modalias value will be copied into
1148 * @len: Length of modalias value
1150 * Based on the value of the compatible property, this routine will attempt
1151 * to choose an appropriate modalias value for a particular device tree node.
1152 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1153 * from the first entry in the compatible list property.
1155 * This routine returns 0 on success, <0 on failure.
1157 int of_modalias_node(struct device_node *node, char *modalias, int len)
1159 const char *compatible, *p;
1162 compatible = of_get_property(node, "compatible", &cplen);
1163 if (!compatible || strlen(compatible) > cplen)
1165 p = strchr(compatible, ',');
1166 strlcpy(modalias, p ? p + 1 : compatible, len);
1169 EXPORT_SYMBOL_GPL(of_modalias_node);
1172 * of_find_node_by_phandle - Find a node given a phandle
1173 * @handle: phandle of the node to find
1175 * Returns a node pointer with refcount incremented, use
1176 * of_node_put() on it when done.
1178 struct device_node *of_find_node_by_phandle(phandle handle)
1180 struct device_node *np = NULL;
1181 unsigned long flags;
1182 phandle masked_handle;
1187 raw_spin_lock_irqsave(&devtree_lock, flags);
1189 masked_handle = handle & phandle_cache_mask;
1191 if (phandle_cache) {
1192 if (phandle_cache[masked_handle] &&
1193 handle == phandle_cache[masked_handle]->phandle)
1194 np = phandle_cache[masked_handle];
1198 for_each_of_allnodes(np)
1199 if (np->phandle == handle) {
1201 phandle_cache[masked_handle] = np;
1207 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1210 EXPORT_SYMBOL(of_find_node_by_phandle);
1212 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1215 printk("%s %pOF", msg, args->np);
1216 for (i = 0; i < args->args_count; i++) {
1217 const char delim = i ? ',' : ':';
1219 pr_cont("%c%08x", delim, args->args[i]);
1224 int of_phandle_iterator_init(struct of_phandle_iterator *it,
1225 const struct device_node *np,
1226 const char *list_name,
1227 const char *cells_name,
1233 memset(it, 0, sizeof(*it));
1235 list = of_get_property(np, list_name, &size);
1239 it->cells_name = cells_name;
1240 it->cell_count = cell_count;
1242 it->list_end = list + size / sizeof(*list);
1243 it->phandle_end = list;
1248 EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
1250 int of_phandle_iterator_next(struct of_phandle_iterator *it)
1255 of_node_put(it->node);
1259 if (!it->cur || it->phandle_end >= it->list_end)
1262 it->cur = it->phandle_end;
1264 /* If phandle is 0, then it is an empty entry with no arguments. */
1265 it->phandle = be32_to_cpup(it->cur++);
1270 * Find the provider node and parse the #*-cells property to
1271 * determine the argument length.
1273 it->node = of_find_node_by_phandle(it->phandle);
1275 if (it->cells_name) {
1277 pr_err("%pOF: could not find phandle\n",
1282 if (of_property_read_u32(it->node, it->cells_name,
1284 pr_err("%pOF: could not get %s for %pOF\n",
1291 count = it->cell_count;
1295 * Make sure that the arguments actually fit in the remaining
1296 * property data length
1298 if (it->cur + count > it->list_end) {
1299 pr_err("%pOF: arguments longer than property\n",
1305 it->phandle_end = it->cur + count;
1306 it->cur_count = count;
1312 of_node_put(it->node);
1318 EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
1320 int of_phandle_iterator_args(struct of_phandle_iterator *it,
1326 count = it->cur_count;
1328 if (WARN_ON(size < count))
1331 for (i = 0; i < count; i++)
1332 args[i] = be32_to_cpup(it->cur++);
1337 static int __of_parse_phandle_with_args(const struct device_node *np,
1338 const char *list_name,
1339 const char *cells_name,
1340 int cell_count, int index,
1341 struct of_phandle_args *out_args)
1343 struct of_phandle_iterator it;
1344 int rc, cur_index = 0;
1346 /* Loop over the phandles until all the requested entry is found */
1347 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
1349 * All of the error cases bail out of the loop, so at
1350 * this point, the parsing is successful. If the requested
1351 * index matches, then fill the out_args structure and return,
1352 * or return -ENOENT for an empty entry.
1355 if (cur_index == index) {
1362 c = of_phandle_iterator_args(&it,
1365 out_args->np = it.node;
1366 out_args->args_count = c;
1368 of_node_put(it.node);
1371 /* Found it! return success */
1379 * Unlock node before returning result; will be one of:
1380 * -ENOENT : index is for empty phandle
1381 * -EINVAL : parsing error on data
1385 of_node_put(it.node);
1390 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1391 * @np: Pointer to device node holding phandle property
1392 * @phandle_name: Name of property holding a phandle value
1393 * @index: For properties holding a table of phandles, this is the index into
1396 * Returns the device_node pointer with refcount incremented. Use
1397 * of_node_put() on it when done.
1399 struct device_node *of_parse_phandle(const struct device_node *np,
1400 const char *phandle_name, int index)
1402 struct of_phandle_args args;
1407 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1413 EXPORT_SYMBOL(of_parse_phandle);
1416 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1417 * @np: pointer to a device tree node containing a list
1418 * @list_name: property name that contains a list
1419 * @cells_name: property name that specifies phandles' arguments count
1420 * @index: index of a phandle to parse out
1421 * @out_args: optional pointer to output arguments structure (will be filled)
1423 * This function is useful to parse lists of phandles and their arguments.
1424 * Returns 0 on success and fills out_args, on error returns appropriate
1427 * Caller is responsible to call of_node_put() on the returned out_args->np
1433 * #list-cells = <2>;
1437 * #list-cells = <1>;
1441 * list = <&phandle1 1 2 &phandle2 3>;
1444 * To get a device_node of the `node2' node you may call this:
1445 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1447 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1448 const char *cells_name, int index,
1449 struct of_phandle_args *out_args)
1453 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1456 EXPORT_SYMBOL(of_parse_phandle_with_args);
1459 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1460 * @np: pointer to a device tree node containing a list
1461 * @list_name: property name that contains a list
1462 * @stem_name: stem of property names that specify phandles' arguments count
1463 * @index: index of a phandle to parse out
1464 * @out_args: optional pointer to output arguments structure (will be filled)
1466 * This function is useful to parse lists of phandles and their arguments.
1467 * Returns 0 on success and fills out_args, on error returns appropriate errno
1468 * value. The difference between this function and of_parse_phandle_with_args()
1469 * is that this API remaps a phandle if the node the phandle points to has
1470 * a <@stem_name>-map property.
1472 * Caller is responsible to call of_node_put() on the returned out_args->np
1478 * #list-cells = <2>;
1482 * #list-cells = <1>;
1486 * #list-cells = <1>;
1487 * list-map = <0 &phandle2 3>,
1489 * <2 &phandle1 5 1>;
1490 * list-map-mask = <0x3>;
1494 * list = <&phandle1 1 2 &phandle3 0>;
1497 * To get a device_node of the `node2' node you may call this:
1498 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1500 int of_parse_phandle_with_args_map(const struct device_node *np,
1501 const char *list_name,
1502 const char *stem_name,
1503 int index, struct of_phandle_args *out_args)
1505 char *cells_name, *map_name = NULL, *mask_name = NULL;
1506 char *pass_name = NULL;
1507 struct device_node *cur, *new = NULL;
1508 const __be32 *map, *mask, *pass;
1509 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
1510 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
1511 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1512 const __be32 *match_array = initial_match_array;
1513 int i, ret, map_len, match;
1514 u32 list_size, new_size;
1519 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1524 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1528 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1532 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1536 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1541 /* Get the #<list>-cells property */
1543 ret = of_property_read_u32(cur, cells_name, &list_size);
1547 /* Precalculate the match array - this simplifies match loop */
1548 for (i = 0; i < list_size; i++)
1549 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1553 /* Get the <list>-map property */
1554 map = of_get_property(cur, map_name, &map_len);
1559 map_len /= sizeof(u32);
1561 /* Get the <list>-map-mask property (optional) */
1562 mask = of_get_property(cur, mask_name, NULL);
1565 /* Iterate through <list>-map property */
1567 while (map_len > (list_size + 1) && !match) {
1568 /* Compare specifiers */
1570 for (i = 0; i < list_size; i++, map_len--)
1571 match &= !((match_array[i] ^ *map++) & mask[i]);
1574 new = of_find_node_by_phandle(be32_to_cpup(map));
1578 /* Check if not found */
1582 if (!of_device_is_available(new))
1585 ret = of_property_read_u32(new, cells_name, &new_size);
1589 /* Check for malformed properties */
1590 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1592 if (map_len < new_size)
1595 /* Move forward by new node's #<list>-cells amount */
1597 map_len -= new_size;
1602 /* Get the <list>-map-pass-thru property (optional) */
1603 pass = of_get_property(cur, pass_name, NULL);
1608 * Successfully parsed a <list>-map translation; copy new
1609 * specifier into the out_args structure, keeping the
1610 * bits specified in <list>-map-pass-thru.
1612 match_array = map - new_size;
1613 for (i = 0; i < new_size; i++) {
1614 __be32 val = *(map - new_size + i);
1616 if (i < list_size) {
1618 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1621 out_args->args[i] = be32_to_cpu(val);
1623 out_args->args_count = list_size = new_size;
1624 /* Iterate again with new provider */
1640 EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1643 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1644 * @np: pointer to a device tree node containing a list
1645 * @list_name: property name that contains a list
1646 * @cell_count: number of argument cells following the phandle
1647 * @index: index of a phandle to parse out
1648 * @out_args: optional pointer to output arguments structure (will be filled)
1650 * This function is useful to parse lists of phandles and their arguments.
1651 * Returns 0 on success and fills out_args, on error returns appropriate
1654 * Caller is responsible to call of_node_put() on the returned out_args->np
1666 * list = <&phandle1 0 2 &phandle2 2 3>;
1669 * To get a device_node of the `node2' node you may call this:
1670 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1672 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1673 const char *list_name, int cell_count,
1674 int index, struct of_phandle_args *out_args)
1678 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1681 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1684 * of_count_phandle_with_args() - Find the number of phandles references in a property
1685 * @np: pointer to a device tree node containing a list
1686 * @list_name: property name that contains a list
1687 * @cells_name: property name that specifies phandles' arguments count
1689 * Returns the number of phandle + argument tuples within a property. It
1690 * is a typical pattern to encode a list of phandle and variable
1691 * arguments into a single property. The number of arguments is encoded
1692 * by a property in the phandle-target node. For example, a gpios
1693 * property would contain a list of GPIO specifies consisting of a
1694 * phandle and 1 or more arguments. The number of arguments are
1695 * determined by the #gpio-cells property in the node pointed to by the
1698 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1699 const char *cells_name)
1701 struct of_phandle_iterator it;
1702 int rc, cur_index = 0;
1704 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1708 while ((rc = of_phandle_iterator_next(&it)) == 0)
1716 EXPORT_SYMBOL(of_count_phandle_with_args);
1719 * __of_add_property - Add a property to a node without lock operations
1721 int __of_add_property(struct device_node *np, struct property *prop)
1723 struct property **next;
1726 next = &np->properties;
1728 if (strcmp(prop->name, (*next)->name) == 0)
1729 /* duplicate ! don't insert it */
1732 next = &(*next)->next;
1740 * of_add_property - Add a property to a node
1742 int of_add_property(struct device_node *np, struct property *prop)
1744 unsigned long flags;
1747 mutex_lock(&of_mutex);
1749 raw_spin_lock_irqsave(&devtree_lock, flags);
1750 rc = __of_add_property(np, prop);
1751 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1754 __of_add_property_sysfs(np, prop);
1756 mutex_unlock(&of_mutex);
1759 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1764 int __of_remove_property(struct device_node *np, struct property *prop)
1766 struct property **next;
1768 for (next = &np->properties; *next; next = &(*next)->next) {
1775 /* found the node */
1777 prop->next = np->deadprops;
1778 np->deadprops = prop;
1784 * of_remove_property - Remove a property from a node.
1786 * Note that we don't actually remove it, since we have given out
1787 * who-knows-how-many pointers to the data using get-property.
1788 * Instead we just move the property to the "dead properties"
1789 * list, so it won't be found any more.
1791 int of_remove_property(struct device_node *np, struct property *prop)
1793 unsigned long flags;
1799 mutex_lock(&of_mutex);
1801 raw_spin_lock_irqsave(&devtree_lock, flags);
1802 rc = __of_remove_property(np, prop);
1803 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1806 __of_remove_property_sysfs(np, prop);
1808 mutex_unlock(&of_mutex);
1811 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1816 int __of_update_property(struct device_node *np, struct property *newprop,
1817 struct property **oldpropp)
1819 struct property **next, *oldprop;
1821 for (next = &np->properties; *next; next = &(*next)->next) {
1822 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1825 *oldpropp = oldprop = *next;
1828 /* replace the node */
1829 newprop->next = oldprop->next;
1831 oldprop->next = np->deadprops;
1832 np->deadprops = oldprop;
1835 newprop->next = NULL;
1843 * of_update_property - Update a property in a node, if the property does
1844 * not exist, add it.
1846 * Note that we don't actually remove it, since we have given out
1847 * who-knows-how-many pointers to the data using get-property.
1848 * Instead we just move the property to the "dead properties" list,
1849 * and add the new property to the property list
1851 int of_update_property(struct device_node *np, struct property *newprop)
1853 struct property *oldprop;
1854 unsigned long flags;
1860 mutex_lock(&of_mutex);
1862 raw_spin_lock_irqsave(&devtree_lock, flags);
1863 rc = __of_update_property(np, newprop, &oldprop);
1864 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1867 __of_update_property_sysfs(np, newprop, oldprop);
1869 mutex_unlock(&of_mutex);
1872 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1877 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1878 int id, const char *stem, int stem_len)
1882 strncpy(ap->stem, stem, stem_len);
1883 ap->stem[stem_len] = 0;
1884 list_add_tail(&ap->link, &aliases_lookup);
1885 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1886 ap->alias, ap->stem, ap->id, np);
1890 * of_alias_scan - Scan all properties of the 'aliases' node
1892 * The function scans all the properties of the 'aliases' node and populates
1893 * the global lookup table with the properties. It returns the
1894 * number of alias properties found, or an error code in case of failure.
1896 * @dt_alloc: An allocator that provides a virtual address to memory
1897 * for storing the resulting tree
1899 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1901 struct property *pp;
1903 of_aliases = of_find_node_by_path("/aliases");
1904 of_chosen = of_find_node_by_path("/chosen");
1905 if (of_chosen == NULL)
1906 of_chosen = of_find_node_by_path("/chosen@0");
1909 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
1910 const char *name = NULL;
1912 if (of_property_read_string(of_chosen, "stdout-path", &name))
1913 of_property_read_string(of_chosen, "linux,stdout-path",
1915 if (IS_ENABLED(CONFIG_PPC) && !name)
1916 of_property_read_string(of_aliases, "stdout", &name);
1918 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
1924 for_each_property_of_node(of_aliases, pp) {
1925 const char *start = pp->name;
1926 const char *end = start + strlen(start);
1927 struct device_node *np;
1928 struct alias_prop *ap;
1931 /* Skip those we do not want to proceed */
1932 if (!strcmp(pp->name, "name") ||
1933 !strcmp(pp->name, "phandle") ||
1934 !strcmp(pp->name, "linux,phandle"))
1937 np = of_find_node_by_path(pp->value);
1941 /* walk the alias backwards to extract the id and work out
1942 * the 'stem' string */
1943 while (isdigit(*(end-1)) && end > start)
1947 if (kstrtoint(end, 10, &id) < 0)
1950 /* Allocate an alias_prop with enough space for the stem */
1951 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
1954 memset(ap, 0, sizeof(*ap) + len + 1);
1956 of_alias_add(ap, np, id, start, len);
1961 * of_alias_get_id - Get alias id for the given device_node
1962 * @np: Pointer to the given device_node
1963 * @stem: Alias stem of the given device_node
1965 * The function travels the lookup table to get the alias id for the given
1966 * device_node and alias stem. It returns the alias id if found.
1968 int of_alias_get_id(struct device_node *np, const char *stem)
1970 struct alias_prop *app;
1973 mutex_lock(&of_mutex);
1974 list_for_each_entry(app, &aliases_lookup, link) {
1975 if (strcmp(app->stem, stem) != 0)
1978 if (np == app->np) {
1983 mutex_unlock(&of_mutex);
1987 EXPORT_SYMBOL_GPL(of_alias_get_id);
1990 * of_alias_get_alias_list - Get alias list for the given device driver
1991 * @matches: Array of OF device match structures to search in
1992 * @stem: Alias stem of the given device_node
1993 * @bitmap: Bitmap field pointer
1994 * @nbits: Maximum number of alias IDs which can be recorded in bitmap
1996 * The function travels the lookup table to record alias ids for the given
1997 * device match structures and alias stem.
1999 * Return: 0 or -ENOSYS when !CONFIG_OF or
2000 * -EOVERFLOW if alias ID is greater then allocated nbits
2002 int of_alias_get_alias_list(const struct of_device_id *matches,
2003 const char *stem, unsigned long *bitmap,
2006 struct alias_prop *app;
2009 /* Zero bitmap field to make sure that all the time it is clean */
2010 bitmap_zero(bitmap, nbits);
2012 mutex_lock(&of_mutex);
2013 pr_debug("%s: Looking for stem: %s\n", __func__, stem);
2014 list_for_each_entry(app, &aliases_lookup, link) {
2015 pr_debug("%s: stem: %s, id: %d\n",
2016 __func__, app->stem, app->id);
2018 if (strcmp(app->stem, stem) != 0) {
2019 pr_debug("%s: stem comparison didn't pass %s\n",
2020 __func__, app->stem);
2024 if (of_match_node(matches, app->np)) {
2025 pr_debug("%s: Allocated ID %d\n", __func__, app->id);
2027 if (app->id >= nbits) {
2028 pr_warn("%s: ID %d >= than bitmap field %d\n",
2029 __func__, app->id, nbits);
2032 set_bit(app->id, bitmap);
2036 mutex_unlock(&of_mutex);
2040 EXPORT_SYMBOL_GPL(of_alias_get_alias_list);
2043 * of_alias_get_highest_id - Get highest alias id for the given stem
2044 * @stem: Alias stem to be examined
2046 * The function travels the lookup table to get the highest alias id for the
2047 * given alias stem. It returns the alias id if found.
2049 int of_alias_get_highest_id(const char *stem)
2051 struct alias_prop *app;
2054 mutex_lock(&of_mutex);
2055 list_for_each_entry(app, &aliases_lookup, link) {
2056 if (strcmp(app->stem, stem) != 0)
2062 mutex_unlock(&of_mutex);
2066 EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2069 * of_console_check() - Test and setup console for DT setup
2070 * @dn - Pointer to device node
2071 * @name - Name to use for preferred console without index. ex. "ttyS"
2072 * @index - Index to use for preferred console.
2074 * Check if the given device node matches the stdout-path property in the
2075 * /chosen node. If it does then register it as the preferred console and return
2076 * TRUE. Otherwise return FALSE.
2078 bool of_console_check(struct device_node *dn, char *name, int index)
2080 if (!dn || dn != of_stdout || console_set_on_cmdline)
2084 * XXX: cast `options' to char pointer to suppress complication
2085 * warnings: printk, UART and console drivers expect char pointer.
2087 return !add_preferred_console(name, index, (char *)of_stdout_options);
2089 EXPORT_SYMBOL_GPL(of_console_check);
2092 * of_find_next_cache_node - Find a node's subsidiary cache
2093 * @np: node of type "cpu" or "cache"
2095 * Returns a node pointer with refcount incremented, use
2096 * of_node_put() on it when done. Caller should hold a reference
2099 struct device_node *of_find_next_cache_node(const struct device_node *np)
2101 struct device_node *child, *cache_node;
2103 cache_node = of_parse_phandle(np, "l2-cache", 0);
2105 cache_node = of_parse_phandle(np, "next-level-cache", 0);
2110 /* OF on pmac has nodes instead of properties named "l2-cache"
2111 * beneath CPU nodes.
2113 if (IS_ENABLED(CONFIG_PPC_PMAC) && !strcmp(np->type, "cpu"))
2114 for_each_child_of_node(np, child)
2115 if (!strcmp(child->type, "cache"))
2122 * of_find_last_cache_level - Find the level at which the last cache is
2123 * present for the given logical cpu
2125 * @cpu: cpu number(logical index) for which the last cache level is needed
2127 * Returns the the level at which the last cache is present. It is exactly
2128 * same as the total number of cache levels for the given logical cpu.
2130 int of_find_last_cache_level(unsigned int cpu)
2132 u32 cache_level = 0;
2133 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2138 np = of_find_next_cache_node(np);
2141 of_property_read_u32(prev, "cache-level", &cache_level);
2147 * of_map_rid - Translate a requester ID through a downstream mapping.
2148 * @np: root complex device node.
2149 * @rid: device requester ID to map.
2150 * @map_name: property name of the map to use.
2151 * @map_mask_name: optional property name of the mask to use.
2152 * @target: optional pointer to a target device node.
2153 * @id_out: optional pointer to receive the translated ID.
2155 * Given a device requester ID, look up the appropriate implementation-defined
2156 * platform ID and/or the target device which receives transactions on that
2157 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
2158 * @id_out may be NULL if only the other is required. If @target points to
2159 * a non-NULL device node pointer, only entries targeting that node will be
2160 * matched; if it points to a NULL value, it will receive the device node of
2161 * the first matching target phandle, with a reference held.
2163 * Return: 0 on success or a standard error code on failure.
2165 int of_map_rid(struct device_node *np, u32 rid,
2166 const char *map_name, const char *map_mask_name,
2167 struct device_node **target, u32 *id_out)
2169 u32 map_mask, masked_rid;
2171 const __be32 *map = NULL;
2173 if (!np || !map_name || (!target && !id_out))
2176 map = of_get_property(np, map_name, &map_len);
2180 /* Otherwise, no map implies no translation */
2185 if (!map_len || map_len % (4 * sizeof(*map))) {
2186 pr_err("%pOF: Error: Bad %s length: %d\n", np,
2191 /* The default is to select all bits. */
2192 map_mask = 0xffffffff;
2195 * Can be overridden by "{iommu,msi}-map-mask" property.
2196 * If of_property_read_u32() fails, the default is used.
2199 of_property_read_u32(np, map_mask_name, &map_mask);
2201 masked_rid = map_mask & rid;
2202 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
2203 struct device_node *phandle_node;
2204 u32 rid_base = be32_to_cpup(map + 0);
2205 u32 phandle = be32_to_cpup(map + 1);
2206 u32 out_base = be32_to_cpup(map + 2);
2207 u32 rid_len = be32_to_cpup(map + 3);
2209 if (rid_base & ~map_mask) {
2210 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
2211 np, map_name, map_name,
2212 map_mask, rid_base);
2216 if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
2219 phandle_node = of_find_node_by_phandle(phandle);
2225 of_node_put(phandle_node);
2227 *target = phandle_node;
2229 if (*target != phandle_node)
2234 *id_out = masked_rid - rid_base + out_base;
2236 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
2237 np, map_name, map_mask, rid_base, out_base,
2238 rid_len, rid, masked_rid - rid_base + out_base);
2242 pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
2243 np, map_name, rid, target && *target ? *target : NULL);
2246 EXPORT_SYMBOL_GPL(of_map_rid);