drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / of / base.c
blob7dc394255a0a14cd1aed02ec79c2f787a222b44c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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
14 * Grant Likely.
17 #define pr_fmt(fmt) "OF: " fmt
19 #include <linux/cleanup.h>
20 #include <linux/console.h>
21 #include <linux/ctype.h>
22 #include <linux/cpu.h>
23 #include <linux/module.h>
24 #include <linux/of.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 EXPORT_SYMBOL(of_chosen);
40 struct device_node *of_aliases;
41 struct device_node *of_stdout;
42 static const char *of_stdout_options;
44 struct kset *of_kset;
47 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
48 * This mutex must be held whenever modifications are being made to the
49 * device tree. The of_{attach,detach}_node() and
50 * of_{add,remove,update}_property() helpers make sure this happens.
52 DEFINE_MUTEX(of_mutex);
54 /* use when traversing tree through the child, sibling,
55 * or parent members of struct device_node.
57 DEFINE_RAW_SPINLOCK(devtree_lock);
59 bool of_node_name_eq(const struct device_node *np, const char *name)
61 const char *node_name;
62 size_t len;
64 if (!np)
65 return false;
67 node_name = kbasename(np->full_name);
68 len = strchrnul(node_name, '@') - node_name;
70 return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
72 EXPORT_SYMBOL(of_node_name_eq);
74 bool of_node_name_prefix(const struct device_node *np, const char *prefix)
76 if (!np)
77 return false;
79 return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
81 EXPORT_SYMBOL(of_node_name_prefix);
83 static bool __of_node_is_type(const struct device_node *np, const char *type)
85 const char *match = __of_get_property(np, "device_type", NULL);
87 return np && match && type && !strcmp(match, type);
90 #define EXCLUDED_DEFAULT_CELLS_PLATFORMS ( \
91 IS_ENABLED(CONFIG_SPARC) \
94 int of_bus_n_addr_cells(struct device_node *np)
96 u32 cells;
98 for (; np; np = np->parent) {
99 if (!of_property_read_u32(np, "#address-cells", &cells))
100 return cells;
102 * Default root value and walking parent nodes for "#address-cells"
103 * is deprecated. Any platforms which hit this warning should
104 * be added to the excluded list.
106 WARN_ONCE(!EXCLUDED_DEFAULT_CELLS_PLATFORMS,
107 "Missing '#address-cells' in %pOF\n", np);
109 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
112 int of_n_addr_cells(struct device_node *np)
114 if (np->parent)
115 np = np->parent;
117 return of_bus_n_addr_cells(np);
119 EXPORT_SYMBOL(of_n_addr_cells);
121 int of_bus_n_size_cells(struct device_node *np)
123 u32 cells;
125 for (; np; np = np->parent) {
126 if (!of_property_read_u32(np, "#size-cells", &cells))
127 return cells;
129 * Default root value and walking parent nodes for "#size-cells"
130 * is deprecated. Any platforms which hit this warning should
131 * be added to the excluded list.
133 WARN_ONCE(!EXCLUDED_DEFAULT_CELLS_PLATFORMS,
134 "Missing '#size-cells' in %pOF\n", np);
136 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
139 int of_n_size_cells(struct device_node *np)
141 if (np->parent)
142 np = np->parent;
144 return of_bus_n_size_cells(np);
146 EXPORT_SYMBOL(of_n_size_cells);
148 #ifdef CONFIG_NUMA
149 int __weak of_node_to_nid(struct device_node *np)
151 return NUMA_NO_NODE;
153 #endif
155 #define OF_PHANDLE_CACHE_BITS 7
156 #define OF_PHANDLE_CACHE_SZ BIT(OF_PHANDLE_CACHE_BITS)
158 static struct device_node *phandle_cache[OF_PHANDLE_CACHE_SZ];
160 static u32 of_phandle_cache_hash(phandle handle)
162 return hash_32(handle, OF_PHANDLE_CACHE_BITS);
166 * Caller must hold devtree_lock.
168 void __of_phandle_cache_inv_entry(phandle handle)
170 u32 handle_hash;
171 struct device_node *np;
173 if (!handle)
174 return;
176 handle_hash = of_phandle_cache_hash(handle);
178 np = phandle_cache[handle_hash];
179 if (np && handle == np->phandle)
180 phandle_cache[handle_hash] = NULL;
183 void __init of_core_init(void)
185 struct device_node *np;
187 of_platform_register_reconfig_notifier();
189 /* Create the kset, and register existing nodes */
190 mutex_lock(&of_mutex);
191 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
192 if (!of_kset) {
193 mutex_unlock(&of_mutex);
194 pr_err("failed to register existing nodes\n");
195 return;
197 for_each_of_allnodes(np) {
198 __of_attach_node_sysfs(np);
199 if (np->phandle && !phandle_cache[of_phandle_cache_hash(np->phandle)])
200 phandle_cache[of_phandle_cache_hash(np->phandle)] = np;
202 mutex_unlock(&of_mutex);
204 /* Symlink in /proc as required by userspace ABI */
205 if (of_root)
206 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
209 static struct property *__of_find_property(const struct device_node *np,
210 const char *name, int *lenp)
212 struct property *pp;
214 if (!np)
215 return NULL;
217 for (pp = np->properties; pp; pp = pp->next) {
218 if (of_prop_cmp(pp->name, name) == 0) {
219 if (lenp)
220 *lenp = pp->length;
221 break;
225 return pp;
228 struct property *of_find_property(const struct device_node *np,
229 const char *name,
230 int *lenp)
232 struct property *pp;
233 unsigned long flags;
235 raw_spin_lock_irqsave(&devtree_lock, flags);
236 pp = __of_find_property(np, name, lenp);
237 raw_spin_unlock_irqrestore(&devtree_lock, flags);
239 return pp;
241 EXPORT_SYMBOL(of_find_property);
243 struct device_node *__of_find_all_nodes(struct device_node *prev)
245 struct device_node *np;
246 if (!prev) {
247 np = of_root;
248 } else if (prev->child) {
249 np = prev->child;
250 } else {
251 /* Walk back up looking for a sibling, or the end of the structure */
252 np = prev;
253 while (np->parent && !np->sibling)
254 np = np->parent;
255 np = np->sibling; /* Might be null at the end of the tree */
257 return np;
261 * of_find_all_nodes - Get next node in global list
262 * @prev: Previous node or NULL to start iteration
263 * of_node_put() will be called on it
265 * Return: A node pointer with refcount incremented, use
266 * of_node_put() on it when done.
268 struct device_node *of_find_all_nodes(struct device_node *prev)
270 struct device_node *np;
271 unsigned long flags;
273 raw_spin_lock_irqsave(&devtree_lock, flags);
274 np = __of_find_all_nodes(prev);
275 of_node_get(np);
276 of_node_put(prev);
277 raw_spin_unlock_irqrestore(&devtree_lock, flags);
278 return np;
280 EXPORT_SYMBOL(of_find_all_nodes);
283 * Find a property with a given name for a given node
284 * and return the value.
286 const void *__of_get_property(const struct device_node *np,
287 const char *name, int *lenp)
289 const struct property *pp = __of_find_property(np, name, lenp);
291 return pp ? pp->value : NULL;
295 * Find a property with a given name for a given node
296 * and return the value.
298 const void *of_get_property(const struct device_node *np, const char *name,
299 int *lenp)
301 const struct property *pp = of_find_property(np, name, lenp);
303 return pp ? pp->value : NULL;
305 EXPORT_SYMBOL(of_get_property);
308 * __of_device_is_compatible() - Check if the node matches given constraints
309 * @device: pointer to node
310 * @compat: required compatible string, NULL or "" for any match
311 * @type: required device_type value, NULL or "" for any match
312 * @name: required node name, NULL or "" for any match
314 * Checks if the given @compat, @type and @name strings match the
315 * properties of the given @device. A constraints can be skipped by
316 * passing NULL or an empty string as the constraint.
318 * Returns 0 for no match, and a positive integer on match. The return
319 * value is a relative score with larger values indicating better
320 * matches. The score is weighted for the most specific compatible value
321 * to get the highest score. Matching type is next, followed by matching
322 * name. Practically speaking, this results in the following priority
323 * order for matches:
325 * 1. specific compatible && type && name
326 * 2. specific compatible && type
327 * 3. specific compatible && name
328 * 4. specific compatible
329 * 5. general compatible && type && name
330 * 6. general compatible && type
331 * 7. general compatible && name
332 * 8. general compatible
333 * 9. type && name
334 * 10. type
335 * 11. name
337 static int __of_device_is_compatible(const struct device_node *device,
338 const char *compat, const char *type, const char *name)
340 const struct property *prop;
341 const char *cp;
342 int index = 0, score = 0;
344 /* Compatible match has highest priority */
345 if (compat && compat[0]) {
346 prop = __of_find_property(device, "compatible", NULL);
347 for (cp = of_prop_next_string(prop, NULL); cp;
348 cp = of_prop_next_string(prop, cp), index++) {
349 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
350 score = INT_MAX/2 - (index << 2);
351 break;
354 if (!score)
355 return 0;
358 /* Matching type is better than matching name */
359 if (type && type[0]) {
360 if (!__of_node_is_type(device, type))
361 return 0;
362 score += 2;
365 /* Matching name is a bit better than not */
366 if (name && name[0]) {
367 if (!of_node_name_eq(device, name))
368 return 0;
369 score++;
372 return score;
375 /** Checks if the given "compat" string matches one of the strings in
376 * the device's "compatible" property
378 int of_device_is_compatible(const struct device_node *device,
379 const char *compat)
381 unsigned long flags;
382 int res;
384 raw_spin_lock_irqsave(&devtree_lock, flags);
385 res = __of_device_is_compatible(device, compat, NULL, NULL);
386 raw_spin_unlock_irqrestore(&devtree_lock, flags);
387 return res;
389 EXPORT_SYMBOL(of_device_is_compatible);
391 /** Checks if the device is compatible with any of the entries in
392 * a NULL terminated array of strings. Returns the best match
393 * score or 0.
395 int of_device_compatible_match(const struct device_node *device,
396 const char *const *compat)
398 unsigned int tmp, score = 0;
400 if (!compat)
401 return 0;
403 while (*compat) {
404 tmp = of_device_is_compatible(device, *compat);
405 if (tmp > score)
406 score = tmp;
407 compat++;
410 return score;
412 EXPORT_SYMBOL_GPL(of_device_compatible_match);
415 * of_machine_compatible_match - Test root of device tree against a compatible array
416 * @compats: NULL terminated array of compatible strings to look for in root node's compatible property.
418 * Returns true if the root node has any of the given compatible values in its
419 * compatible property.
421 bool of_machine_compatible_match(const char *const *compats)
423 struct device_node *root;
424 int rc = 0;
426 root = of_find_node_by_path("/");
427 if (root) {
428 rc = of_device_compatible_match(root, compats);
429 of_node_put(root);
432 return rc != 0;
434 EXPORT_SYMBOL(of_machine_compatible_match);
436 static bool __of_device_is_status(const struct device_node *device,
437 const char * const*strings)
439 const char *status;
440 int statlen;
442 if (!device)
443 return false;
445 status = __of_get_property(device, "status", &statlen);
446 if (status == NULL)
447 return false;
449 if (statlen > 0) {
450 while (*strings) {
451 unsigned int len = strlen(*strings);
453 if ((*strings)[len - 1] == '-') {
454 if (!strncmp(status, *strings, len))
455 return true;
456 } else {
457 if (!strcmp(status, *strings))
458 return true;
460 strings++;
464 return false;
468 * __of_device_is_available - check if a device is available for use
470 * @device: Node to check for availability, with locks already held
472 * Return: True if the status property is absent or set to "okay" or "ok",
473 * false otherwise
475 static bool __of_device_is_available(const struct device_node *device)
477 static const char * const ok[] = {"okay", "ok", NULL};
479 if (!device)
480 return false;
482 return !__of_get_property(device, "status", NULL) ||
483 __of_device_is_status(device, ok);
487 * __of_device_is_reserved - check if a device is reserved
489 * @device: Node to check for availability, with locks already held
491 * Return: True if the status property is set to "reserved", false otherwise
493 static bool __of_device_is_reserved(const struct device_node *device)
495 static const char * const reserved[] = {"reserved", NULL};
497 return __of_device_is_status(device, reserved);
501 * of_device_is_available - check if a device is available for use
503 * @device: Node to check for availability
505 * Return: True if the status property is absent or set to "okay" or "ok",
506 * false otherwise
508 bool of_device_is_available(const struct device_node *device)
510 unsigned long flags;
511 bool res;
513 raw_spin_lock_irqsave(&devtree_lock, flags);
514 res = __of_device_is_available(device);
515 raw_spin_unlock_irqrestore(&devtree_lock, flags);
516 return res;
519 EXPORT_SYMBOL(of_device_is_available);
522 * __of_device_is_fail - check if a device has status "fail" or "fail-..."
524 * @device: Node to check status for, with locks already held
526 * Return: True if the status property is set to "fail" or "fail-..." (for any
527 * error code suffix), false otherwise
529 static bool __of_device_is_fail(const struct device_node *device)
531 static const char * const fail[] = {"fail", "fail-", NULL};
533 return __of_device_is_status(device, fail);
537 * of_device_is_big_endian - check if a device has BE registers
539 * @device: Node to check for endianness
541 * Return: True if the device has a "big-endian" property, or if the kernel
542 * was compiled for BE *and* the device has a "native-endian" property.
543 * Returns false otherwise.
545 * Callers would nominally use ioread32be/iowrite32be if
546 * of_device_is_big_endian() == true, or readl/writel otherwise.
548 bool of_device_is_big_endian(const struct device_node *device)
550 if (of_property_read_bool(device, "big-endian"))
551 return true;
552 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
553 of_property_read_bool(device, "native-endian"))
554 return true;
555 return false;
557 EXPORT_SYMBOL(of_device_is_big_endian);
560 * of_get_parent - Get a node's parent if any
561 * @node: Node to get parent
563 * Return: A node pointer with refcount incremented, use
564 * of_node_put() on it when done.
566 struct device_node *of_get_parent(const struct device_node *node)
568 struct device_node *np;
569 unsigned long flags;
571 if (!node)
572 return NULL;
574 raw_spin_lock_irqsave(&devtree_lock, flags);
575 np = of_node_get(node->parent);
576 raw_spin_unlock_irqrestore(&devtree_lock, flags);
577 return np;
579 EXPORT_SYMBOL(of_get_parent);
582 * of_get_next_parent - Iterate to a node's parent
583 * @node: Node to get parent of
585 * This is like of_get_parent() except that it drops the
586 * refcount on the passed node, making it suitable for iterating
587 * through a node's parents.
589 * Return: A node pointer with refcount incremented, use
590 * of_node_put() on it when done.
592 struct device_node *of_get_next_parent(struct device_node *node)
594 struct device_node *parent;
595 unsigned long flags;
597 if (!node)
598 return NULL;
600 raw_spin_lock_irqsave(&devtree_lock, flags);
601 parent = of_node_get(node->parent);
602 of_node_put(node);
603 raw_spin_unlock_irqrestore(&devtree_lock, flags);
604 return parent;
606 EXPORT_SYMBOL(of_get_next_parent);
608 static struct device_node *__of_get_next_child(const struct device_node *node,
609 struct device_node *prev)
611 struct device_node *next;
613 if (!node)
614 return NULL;
616 next = prev ? prev->sibling : node->child;
617 of_node_get(next);
618 of_node_put(prev);
619 return next;
621 #define __for_each_child_of_node(parent, child) \
622 for (child = __of_get_next_child(parent, NULL); child != NULL; \
623 child = __of_get_next_child(parent, child))
626 * of_get_next_child - Iterate a node childs
627 * @node: parent node
628 * @prev: previous child of the parent node, or NULL to get first
630 * Return: A node pointer with refcount incremented, use of_node_put() on
631 * it when done. Returns NULL when prev is the last child. Decrements the
632 * refcount of prev.
634 struct device_node *of_get_next_child(const struct device_node *node,
635 struct device_node *prev)
637 struct device_node *next;
638 unsigned long flags;
640 raw_spin_lock_irqsave(&devtree_lock, flags);
641 next = __of_get_next_child(node, prev);
642 raw_spin_unlock_irqrestore(&devtree_lock, flags);
643 return next;
645 EXPORT_SYMBOL(of_get_next_child);
648 * of_get_next_child_with_prefix - Find the next child node with prefix
649 * @node: parent node
650 * @prev: previous child of the parent node, or NULL to get first
651 * @prefix: prefix that the node name should have
653 * This function is like of_get_next_child(), except that it automatically
654 * skips any nodes whose name doesn't have the given prefix.
656 * Return: A node pointer with refcount incremented, use
657 * of_node_put() on it when done.
659 struct device_node *of_get_next_child_with_prefix(const struct device_node *node,
660 struct device_node *prev,
661 const char *prefix)
663 struct device_node *next;
664 unsigned long flags;
666 if (!node)
667 return NULL;
669 raw_spin_lock_irqsave(&devtree_lock, flags);
670 next = prev ? prev->sibling : node->child;
671 for (; next; next = next->sibling) {
672 if (!of_node_name_prefix(next, prefix))
673 continue;
674 if (of_node_get(next))
675 break;
677 of_node_put(prev);
678 raw_spin_unlock_irqrestore(&devtree_lock, flags);
679 return next;
681 EXPORT_SYMBOL(of_get_next_child_with_prefix);
683 static struct device_node *of_get_next_status_child(const struct device_node *node,
684 struct device_node *prev,
685 bool (*checker)(const struct device_node *))
687 struct device_node *next;
688 unsigned long flags;
690 if (!node)
691 return NULL;
693 raw_spin_lock_irqsave(&devtree_lock, flags);
694 next = prev ? prev->sibling : node->child;
695 for (; next; next = next->sibling) {
696 if (!checker(next))
697 continue;
698 if (of_node_get(next))
699 break;
701 of_node_put(prev);
702 raw_spin_unlock_irqrestore(&devtree_lock, flags);
703 return next;
707 * of_get_next_available_child - Find the next available child node
708 * @node: parent node
709 * @prev: previous child of the parent node, or NULL to get first
711 * This function is like of_get_next_child(), except that it
712 * automatically skips any disabled nodes (i.e. status = "disabled").
714 struct device_node *of_get_next_available_child(const struct device_node *node,
715 struct device_node *prev)
717 return of_get_next_status_child(node, prev, __of_device_is_available);
719 EXPORT_SYMBOL(of_get_next_available_child);
722 * of_get_next_reserved_child - Find the next reserved child node
723 * @node: parent 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_reserved_child(const struct device_node *node,
730 struct device_node *prev)
732 return of_get_next_status_child(node, prev, __of_device_is_reserved);
734 EXPORT_SYMBOL(of_get_next_reserved_child);
737 * of_get_next_cpu_node - Iterate on cpu nodes
738 * @prev: previous child of the /cpus node, or NULL to get first
740 * Unusable CPUs (those with the status property set to "fail" or "fail-...")
741 * will be skipped.
743 * Return: A cpu node pointer with refcount incremented, use of_node_put()
744 * on it when done. Returns NULL when prev is the last child. Decrements
745 * the refcount of prev.
747 struct device_node *of_get_next_cpu_node(struct device_node *prev)
749 struct device_node *next = NULL;
750 unsigned long flags;
751 struct device_node *node;
753 if (!prev)
754 node = of_find_node_by_path("/cpus");
756 raw_spin_lock_irqsave(&devtree_lock, flags);
757 if (prev)
758 next = prev->sibling;
759 else if (node) {
760 next = node->child;
761 of_node_put(node);
763 for (; next; next = next->sibling) {
764 if (__of_device_is_fail(next))
765 continue;
766 if (!(of_node_name_eq(next, "cpu") ||
767 __of_node_is_type(next, "cpu")))
768 continue;
769 if (of_node_get(next))
770 break;
772 of_node_put(prev);
773 raw_spin_unlock_irqrestore(&devtree_lock, flags);
774 return next;
776 EXPORT_SYMBOL(of_get_next_cpu_node);
779 * of_get_compatible_child - Find compatible child node
780 * @parent: parent node
781 * @compatible: compatible string
783 * Lookup child node whose compatible property contains the given compatible
784 * string.
786 * Return: a node pointer with refcount incremented, use of_node_put() on it
787 * when done; or NULL if not found.
789 struct device_node *of_get_compatible_child(const struct device_node *parent,
790 const char *compatible)
792 struct device_node *child;
794 for_each_child_of_node(parent, child) {
795 if (of_device_is_compatible(child, compatible))
796 break;
799 return child;
801 EXPORT_SYMBOL(of_get_compatible_child);
804 * of_get_child_by_name - Find the child node by name for a given parent
805 * @node: parent node
806 * @name: child name to look for.
808 * This function looks for child node for given matching name
810 * Return: A node pointer if found, with refcount incremented, use
811 * of_node_put() on it when done.
812 * Returns NULL if node is not found.
814 struct device_node *of_get_child_by_name(const struct device_node *node,
815 const char *name)
817 struct device_node *child;
819 for_each_child_of_node(node, child)
820 if (of_node_name_eq(child, name))
821 break;
822 return child;
824 EXPORT_SYMBOL(of_get_child_by_name);
826 struct device_node *__of_find_node_by_path(const struct device_node *parent,
827 const char *path)
829 struct device_node *child;
830 int len;
832 len = strcspn(path, "/:");
833 if (!len)
834 return NULL;
836 __for_each_child_of_node(parent, child) {
837 const char *name = kbasename(child->full_name);
838 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
839 return child;
841 return NULL;
844 struct device_node *__of_find_node_by_full_path(struct device_node *node,
845 const char *path)
847 const char *separator = strchr(path, ':');
849 while (node && *path == '/') {
850 struct device_node *tmp = node;
852 path++; /* Increment past '/' delimiter */
853 node = __of_find_node_by_path(node, path);
854 of_node_put(tmp);
855 path = strchrnul(path, '/');
856 if (separator && separator < path)
857 break;
859 return node;
863 * of_find_node_opts_by_path - Find a node matching a full OF path
864 * @path: Either the full path to match, or if the path does not
865 * start with '/', the name of a property of the /aliases
866 * node (an alias). In the case of an alias, the node
867 * matching the alias' value will be returned.
868 * @opts: Address of a pointer into which to store the start of
869 * an options string appended to the end of the path with
870 * a ':' separator.
872 * Valid paths:
873 * * /foo/bar Full path
874 * * foo Valid alias
875 * * foo/bar Valid alias + relative path
877 * Return: A node pointer with refcount incremented, use
878 * of_node_put() on it when done.
880 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
882 struct device_node *np = NULL;
883 const struct property *pp;
884 unsigned long flags;
885 const char *separator = strchr(path, ':');
887 if (opts)
888 *opts = separator ? separator + 1 : NULL;
890 if (strcmp(path, "/") == 0)
891 return of_node_get(of_root);
893 /* The path could begin with an alias */
894 if (*path != '/') {
895 int len;
896 const char *p = separator;
898 if (!p)
899 p = strchrnul(path, '/');
900 len = p - path;
902 /* of_aliases must not be NULL */
903 if (!of_aliases)
904 return NULL;
906 for_each_property_of_node(of_aliases, pp) {
907 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
908 np = of_find_node_by_path(pp->value);
909 break;
912 if (!np)
913 return NULL;
914 path = p;
917 /* Step down the tree matching path components */
918 raw_spin_lock_irqsave(&devtree_lock, flags);
919 if (!np)
920 np = of_node_get(of_root);
921 np = __of_find_node_by_full_path(np, path);
922 raw_spin_unlock_irqrestore(&devtree_lock, flags);
923 return np;
925 EXPORT_SYMBOL(of_find_node_opts_by_path);
928 * of_find_node_by_name - Find a node by its "name" property
929 * @from: The node to start searching from or NULL; the node
930 * you pass will not be searched, only the next one
931 * will. Typically, you pass what the previous call
932 * returned. of_node_put() will be called on @from.
933 * @name: The name string to match against
935 * Return: A node pointer with refcount incremented, use
936 * of_node_put() on it when done.
938 struct device_node *of_find_node_by_name(struct device_node *from,
939 const char *name)
941 struct device_node *np;
942 unsigned long flags;
944 raw_spin_lock_irqsave(&devtree_lock, flags);
945 for_each_of_allnodes_from(from, np)
946 if (of_node_name_eq(np, name) && of_node_get(np))
947 break;
948 of_node_put(from);
949 raw_spin_unlock_irqrestore(&devtree_lock, flags);
950 return np;
952 EXPORT_SYMBOL(of_find_node_by_name);
955 * of_find_node_by_type - Find a node by its "device_type" property
956 * @from: The node to start searching from, or NULL to start searching
957 * the entire device tree. The node you pass will not be
958 * searched, only the next one will; typically, you pass
959 * what the previous call returned. of_node_put() will be
960 * called on from for you.
961 * @type: The type string to match against
963 * Return: A node pointer with refcount incremented, use
964 * of_node_put() on it when done.
966 struct device_node *of_find_node_by_type(struct device_node *from,
967 const char *type)
969 struct device_node *np;
970 unsigned long flags;
972 raw_spin_lock_irqsave(&devtree_lock, flags);
973 for_each_of_allnodes_from(from, np)
974 if (__of_node_is_type(np, type) && of_node_get(np))
975 break;
976 of_node_put(from);
977 raw_spin_unlock_irqrestore(&devtree_lock, flags);
978 return np;
980 EXPORT_SYMBOL(of_find_node_by_type);
983 * of_find_compatible_node - Find a node based on type and one of the
984 * tokens in its "compatible" property
985 * @from: The node to start searching from or NULL, the node
986 * you pass will not be searched, only the next one
987 * will; typically, you pass what the previous call
988 * returned. of_node_put() will be called on it
989 * @type: The type string to match "device_type" or NULL to ignore
990 * @compatible: The string to match to one of the tokens in the device
991 * "compatible" list.
993 * Return: A node pointer with refcount incremented, use
994 * of_node_put() on it when done.
996 struct device_node *of_find_compatible_node(struct device_node *from,
997 const char *type, const char *compatible)
999 struct device_node *np;
1000 unsigned long flags;
1002 raw_spin_lock_irqsave(&devtree_lock, flags);
1003 for_each_of_allnodes_from(from, np)
1004 if (__of_device_is_compatible(np, compatible, type, NULL) &&
1005 of_node_get(np))
1006 break;
1007 of_node_put(from);
1008 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1009 return np;
1011 EXPORT_SYMBOL(of_find_compatible_node);
1014 * of_find_node_with_property - Find a node which has a property with
1015 * the given name.
1016 * @from: The node to start searching from or NULL, the node
1017 * you pass will not be searched, only the next one
1018 * will; typically, you pass what the previous call
1019 * returned. of_node_put() will be called on it
1020 * @prop_name: The name of the property to look for.
1022 * Return: A node pointer with refcount incremented, use
1023 * of_node_put() on it when done.
1025 struct device_node *of_find_node_with_property(struct device_node *from,
1026 const char *prop_name)
1028 struct device_node *np;
1029 const struct property *pp;
1030 unsigned long flags;
1032 raw_spin_lock_irqsave(&devtree_lock, flags);
1033 for_each_of_allnodes_from(from, np) {
1034 for (pp = np->properties; pp; pp = pp->next) {
1035 if (of_prop_cmp(pp->name, prop_name) == 0) {
1036 of_node_get(np);
1037 goto out;
1041 out:
1042 of_node_put(from);
1043 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1044 return np;
1046 EXPORT_SYMBOL(of_find_node_with_property);
1048 static
1049 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1050 const struct device_node *node)
1052 const struct of_device_id *best_match = NULL;
1053 int score, best_score = 0;
1055 if (!matches)
1056 return NULL;
1058 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1059 score = __of_device_is_compatible(node, matches->compatible,
1060 matches->type, matches->name);
1061 if (score > best_score) {
1062 best_match = matches;
1063 best_score = score;
1067 return best_match;
1071 * of_match_node - Tell if a device_node has a matching of_match structure
1072 * @matches: array of of device match structures to search in
1073 * @node: the of device structure to match against
1075 * Low level utility function used by device matching.
1077 const struct of_device_id *of_match_node(const struct of_device_id *matches,
1078 const struct device_node *node)
1080 const struct of_device_id *match;
1081 unsigned long flags;
1083 raw_spin_lock_irqsave(&devtree_lock, flags);
1084 match = __of_match_node(matches, node);
1085 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1086 return match;
1088 EXPORT_SYMBOL(of_match_node);
1091 * of_find_matching_node_and_match - Find a node based on an of_device_id
1092 * match table.
1093 * @from: The node to start searching from or NULL, the node
1094 * you pass will not be searched, only the next one
1095 * will; typically, you pass what the previous call
1096 * returned. of_node_put() will be called on it
1097 * @matches: array of of device match structures to search in
1098 * @match: Updated to point at the matches entry which matched
1100 * Return: A node pointer with refcount incremented, use
1101 * of_node_put() on it when done.
1103 struct device_node *of_find_matching_node_and_match(struct device_node *from,
1104 const struct of_device_id *matches,
1105 const struct of_device_id **match)
1107 struct device_node *np;
1108 const struct of_device_id *m;
1109 unsigned long flags;
1111 if (match)
1112 *match = NULL;
1114 raw_spin_lock_irqsave(&devtree_lock, flags);
1115 for_each_of_allnodes_from(from, np) {
1116 m = __of_match_node(matches, np);
1117 if (m && of_node_get(np)) {
1118 if (match)
1119 *match = m;
1120 break;
1123 of_node_put(from);
1124 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1125 return np;
1127 EXPORT_SYMBOL(of_find_matching_node_and_match);
1130 * of_alias_from_compatible - Lookup appropriate alias for a device node
1131 * depending on compatible
1132 * @node: pointer to a device tree node
1133 * @alias: Pointer to buffer that alias value will be copied into
1134 * @len: Length of alias value
1136 * Based on the value of the compatible property, this routine will attempt
1137 * to choose an appropriate alias value for a particular device tree node.
1138 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1139 * from the first entry in the compatible list property.
1141 * Note: The matching on just the "product" side of the compatible is a relic
1142 * from I2C and SPI. Please do not add any new user.
1144 * Return: This routine returns 0 on success, <0 on failure.
1146 int of_alias_from_compatible(const struct device_node *node, char *alias, int len)
1148 const char *compatible, *p;
1149 int cplen;
1151 compatible = of_get_property(node, "compatible", &cplen);
1152 if (!compatible || strlen(compatible) > cplen)
1153 return -ENODEV;
1154 p = strchr(compatible, ',');
1155 strscpy(alias, p ? p + 1 : compatible, len);
1156 return 0;
1158 EXPORT_SYMBOL_GPL(of_alias_from_compatible);
1161 * of_find_node_by_phandle - Find a node given a phandle
1162 * @handle: phandle of the node to find
1164 * Return: A node pointer with refcount incremented, use
1165 * of_node_put() on it when done.
1167 struct device_node *of_find_node_by_phandle(phandle handle)
1169 struct device_node *np = NULL;
1170 unsigned long flags;
1171 u32 handle_hash;
1173 if (!handle)
1174 return NULL;
1176 handle_hash = of_phandle_cache_hash(handle);
1178 raw_spin_lock_irqsave(&devtree_lock, flags);
1180 if (phandle_cache[handle_hash] &&
1181 handle == phandle_cache[handle_hash]->phandle)
1182 np = phandle_cache[handle_hash];
1184 if (!np) {
1185 for_each_of_allnodes(np)
1186 if (np->phandle == handle &&
1187 !of_node_check_flag(np, OF_DETACHED)) {
1188 phandle_cache[handle_hash] = np;
1189 break;
1193 of_node_get(np);
1194 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1195 return np;
1197 EXPORT_SYMBOL(of_find_node_by_phandle);
1199 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1201 int i;
1202 printk("%s %pOF", msg, args->np);
1203 for (i = 0; i < args->args_count; i++) {
1204 const char delim = i ? ',' : ':';
1206 pr_cont("%c%08x", delim, args->args[i]);
1208 pr_cont("\n");
1211 int of_phandle_iterator_init(struct of_phandle_iterator *it,
1212 const struct device_node *np,
1213 const char *list_name,
1214 const char *cells_name,
1215 int cell_count)
1217 const __be32 *list;
1218 int size;
1220 memset(it, 0, sizeof(*it));
1223 * one of cell_count or cells_name must be provided to determine the
1224 * argument length.
1226 if (cell_count < 0 && !cells_name)
1227 return -EINVAL;
1229 list = of_get_property(np, list_name, &size);
1230 if (!list)
1231 return -ENOENT;
1233 it->cells_name = cells_name;
1234 it->cell_count = cell_count;
1235 it->parent = np;
1236 it->list_end = list + size / sizeof(*list);
1237 it->phandle_end = list;
1238 it->cur = list;
1240 return 0;
1242 EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
1244 int of_phandle_iterator_next(struct of_phandle_iterator *it)
1246 uint32_t count = 0;
1248 if (it->node) {
1249 of_node_put(it->node);
1250 it->node = NULL;
1253 if (!it->cur || it->phandle_end >= it->list_end)
1254 return -ENOENT;
1256 it->cur = it->phandle_end;
1258 /* If phandle is 0, then it is an empty entry with no arguments. */
1259 it->phandle = be32_to_cpup(it->cur++);
1261 if (it->phandle) {
1264 * Find the provider node and parse the #*-cells property to
1265 * determine the argument length.
1267 it->node = of_find_node_by_phandle(it->phandle);
1269 if (it->cells_name) {
1270 if (!it->node) {
1271 pr_err("%pOF: could not find phandle %d\n",
1272 it->parent, it->phandle);
1273 goto err;
1276 if (of_property_read_u32(it->node, it->cells_name,
1277 &count)) {
1279 * If both cell_count and cells_name is given,
1280 * fall back to cell_count in absence
1281 * of the cells_name property
1283 if (it->cell_count >= 0) {
1284 count = it->cell_count;
1285 } else {
1286 pr_err("%pOF: could not get %s for %pOF\n",
1287 it->parent,
1288 it->cells_name,
1289 it->node);
1290 goto err;
1293 } else {
1294 count = it->cell_count;
1298 * Make sure that the arguments actually fit in the remaining
1299 * property data length
1301 if (it->cur + count > it->list_end) {
1302 if (it->cells_name)
1303 pr_err("%pOF: %s = %d found %td\n",
1304 it->parent, it->cells_name,
1305 count, it->list_end - it->cur);
1306 else
1307 pr_err("%pOF: phandle %s needs %d, found %td\n",
1308 it->parent, of_node_full_name(it->node),
1309 count, it->list_end - it->cur);
1310 goto err;
1314 it->phandle_end = it->cur + count;
1315 it->cur_count = count;
1317 return 0;
1319 err:
1320 if (it->node) {
1321 of_node_put(it->node);
1322 it->node = NULL;
1325 return -EINVAL;
1327 EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
1329 int of_phandle_iterator_args(struct of_phandle_iterator *it,
1330 uint32_t *args,
1331 int size)
1333 int i, count;
1335 count = it->cur_count;
1337 if (WARN_ON(size < count))
1338 count = size;
1340 for (i = 0; i < count; i++)
1341 args[i] = be32_to_cpup(it->cur++);
1343 return count;
1346 int __of_parse_phandle_with_args(const struct device_node *np,
1347 const char *list_name,
1348 const char *cells_name,
1349 int cell_count, int index,
1350 struct of_phandle_args *out_args)
1352 struct of_phandle_iterator it;
1353 int rc, cur_index = 0;
1355 if (index < 0)
1356 return -EINVAL;
1358 /* Loop over the phandles until all the requested entry is found */
1359 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
1361 * All of the error cases bail out of the loop, so at
1362 * this point, the parsing is successful. If the requested
1363 * index matches, then fill the out_args structure and return,
1364 * or return -ENOENT for an empty entry.
1366 rc = -ENOENT;
1367 if (cur_index == index) {
1368 if (!it.phandle)
1369 goto err;
1371 if (out_args) {
1372 int c;
1374 c = of_phandle_iterator_args(&it,
1375 out_args->args,
1376 MAX_PHANDLE_ARGS);
1377 out_args->np = it.node;
1378 out_args->args_count = c;
1379 } else {
1380 of_node_put(it.node);
1383 /* Found it! return success */
1384 return 0;
1387 cur_index++;
1391 * Unlock node before returning result; will be one of:
1392 * -ENOENT : index is for empty phandle
1393 * -EINVAL : parsing error on data
1396 err:
1397 of_node_put(it.node);
1398 return rc;
1400 EXPORT_SYMBOL(__of_parse_phandle_with_args);
1403 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1404 * @np: pointer to a device tree node containing a list
1405 * @list_name: property name that contains a list
1406 * @stem_name: stem of property names that specify phandles' arguments count
1407 * @index: index of a phandle to parse out
1408 * @out_args: optional pointer to output arguments structure (will be filled)
1410 * This function is useful to parse lists of phandles and their arguments.
1411 * Returns 0 on success and fills out_args, on error returns appropriate errno
1412 * value. The difference between this function and of_parse_phandle_with_args()
1413 * is that this API remaps a phandle if the node the phandle points to has
1414 * a <@stem_name>-map property.
1416 * Caller is responsible to call of_node_put() on the returned out_args->np
1417 * pointer.
1419 * Example::
1421 * phandle1: node1 {
1422 * #list-cells = <2>;
1423 * };
1425 * phandle2: node2 {
1426 * #list-cells = <1>;
1427 * };
1429 * phandle3: node3 {
1430 * #list-cells = <1>;
1431 * list-map = <0 &phandle2 3>,
1432 * <1 &phandle2 2>,
1433 * <2 &phandle1 5 1>;
1434 * list-map-mask = <0x3>;
1435 * };
1437 * node4 {
1438 * list = <&phandle1 1 2 &phandle3 0>;
1439 * };
1441 * To get a device_node of the ``node2`` node you may call this:
1442 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1444 int of_parse_phandle_with_args_map(const struct device_node *np,
1445 const char *list_name,
1446 const char *stem_name,
1447 int index, struct of_phandle_args *out_args)
1449 char *cells_name __free(kfree) = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1450 char *map_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1451 char *mask_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1452 char *pass_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1453 struct device_node *cur, *new = NULL;
1454 const __be32 *map, *mask, *pass;
1455 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
1456 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(0) };
1457 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1458 const __be32 *match_array = initial_match_array;
1459 int i, ret, map_len, match;
1460 u32 list_size, new_size;
1462 if (index < 0)
1463 return -EINVAL;
1465 if (!cells_name || !map_name || !mask_name || !pass_name)
1466 return -ENOMEM;
1468 ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index,
1469 out_args);
1470 if (ret)
1471 return ret;
1473 /* Get the #<list>-cells property */
1474 cur = out_args->np;
1475 ret = of_property_read_u32(cur, cells_name, &list_size);
1476 if (ret < 0)
1477 goto put;
1479 /* Precalculate the match array - this simplifies match loop */
1480 for (i = 0; i < list_size; i++)
1481 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1483 ret = -EINVAL;
1484 while (cur) {
1485 /* Get the <list>-map property */
1486 map = of_get_property(cur, map_name, &map_len);
1487 if (!map) {
1488 return 0;
1490 map_len /= sizeof(u32);
1492 /* Get the <list>-map-mask property (optional) */
1493 mask = of_get_property(cur, mask_name, NULL);
1494 if (!mask)
1495 mask = dummy_mask;
1496 /* Iterate through <list>-map property */
1497 match = 0;
1498 while (map_len > (list_size + 1) && !match) {
1499 /* Compare specifiers */
1500 match = 1;
1501 for (i = 0; i < list_size; i++, map_len--)
1502 match &= !((match_array[i] ^ *map++) & mask[i]);
1504 of_node_put(new);
1505 new = of_find_node_by_phandle(be32_to_cpup(map));
1506 map++;
1507 map_len--;
1509 /* Check if not found */
1510 if (!new)
1511 goto put;
1513 if (!of_device_is_available(new))
1514 match = 0;
1516 ret = of_property_read_u32(new, cells_name, &new_size);
1517 if (ret)
1518 goto put;
1520 /* Check for malformed properties */
1521 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1522 goto put;
1523 if (map_len < new_size)
1524 goto put;
1526 /* Move forward by new node's #<list>-cells amount */
1527 map += new_size;
1528 map_len -= new_size;
1530 if (!match)
1531 goto put;
1533 /* Get the <list>-map-pass-thru property (optional) */
1534 pass = of_get_property(cur, pass_name, NULL);
1535 if (!pass)
1536 pass = dummy_pass;
1539 * Successfully parsed a <list>-map translation; copy new
1540 * specifier into the out_args structure, keeping the
1541 * bits specified in <list>-map-pass-thru.
1543 match_array = map - new_size;
1544 for (i = 0; i < new_size; i++) {
1545 __be32 val = *(map - new_size + i);
1547 if (i < list_size) {
1548 val &= ~pass[i];
1549 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1552 out_args->args[i] = be32_to_cpu(val);
1554 out_args->args_count = list_size = new_size;
1555 /* Iterate again with new provider */
1556 out_args->np = new;
1557 of_node_put(cur);
1558 cur = new;
1559 new = NULL;
1561 put:
1562 of_node_put(cur);
1563 of_node_put(new);
1564 return ret;
1566 EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1569 * of_count_phandle_with_args() - Find the number of phandles references in a property
1570 * @np: pointer to a device tree node containing a list
1571 * @list_name: property name that contains a list
1572 * @cells_name: property name that specifies phandles' arguments count
1574 * Return: The number of phandle + argument tuples within a property. It
1575 * is a typical pattern to encode a list of phandle and variable
1576 * arguments into a single property. The number of arguments is encoded
1577 * by a property in the phandle-target node. For example, a gpios
1578 * property would contain a list of GPIO specifies consisting of a
1579 * phandle and 1 or more arguments. The number of arguments are
1580 * determined by the #gpio-cells property in the node pointed to by the
1581 * phandle.
1583 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1584 const char *cells_name)
1586 struct of_phandle_iterator it;
1587 int rc, cur_index = 0;
1590 * If cells_name is NULL we assume a cell count of 0. This makes
1591 * counting the phandles trivial as each 32bit word in the list is a
1592 * phandle and no arguments are to consider. So we don't iterate through
1593 * the list but just use the length to determine the phandle count.
1595 if (!cells_name) {
1596 const __be32 *list;
1597 int size;
1599 list = of_get_property(np, list_name, &size);
1600 if (!list)
1601 return -ENOENT;
1603 return size / sizeof(*list);
1606 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1);
1607 if (rc)
1608 return rc;
1610 while ((rc = of_phandle_iterator_next(&it)) == 0)
1611 cur_index += 1;
1613 if (rc != -ENOENT)
1614 return rc;
1616 return cur_index;
1618 EXPORT_SYMBOL(of_count_phandle_with_args);
1620 static struct property *__of_remove_property_from_list(struct property **list, struct property *prop)
1622 struct property **next;
1624 for (next = list; *next; next = &(*next)->next) {
1625 if (*next == prop) {
1626 *next = prop->next;
1627 prop->next = NULL;
1628 return prop;
1631 return NULL;
1635 * __of_add_property - Add a property to a node without lock operations
1636 * @np: Caller's Device Node
1637 * @prop: Property to add
1639 int __of_add_property(struct device_node *np, struct property *prop)
1641 int rc = 0;
1642 unsigned long flags;
1643 struct property **next;
1645 raw_spin_lock_irqsave(&devtree_lock, flags);
1647 __of_remove_property_from_list(&np->deadprops, prop);
1649 prop->next = NULL;
1650 next = &np->properties;
1651 while (*next) {
1652 if (strcmp(prop->name, (*next)->name) == 0) {
1653 /* duplicate ! don't insert it */
1654 rc = -EEXIST;
1655 goto out_unlock;
1657 next = &(*next)->next;
1659 *next = prop;
1661 out_unlock:
1662 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1663 if (rc)
1664 return rc;
1666 __of_add_property_sysfs(np, prop);
1667 return 0;
1671 * of_add_property - Add a property to a node
1672 * @np: Caller's Device Node
1673 * @prop: Property to add
1675 int of_add_property(struct device_node *np, struct property *prop)
1677 int rc;
1679 mutex_lock(&of_mutex);
1680 rc = __of_add_property(np, prop);
1681 mutex_unlock(&of_mutex);
1683 if (!rc)
1684 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1686 return rc;
1688 EXPORT_SYMBOL_GPL(of_add_property);
1690 int __of_remove_property(struct device_node *np, struct property *prop)
1692 unsigned long flags;
1693 int rc = -ENODEV;
1695 raw_spin_lock_irqsave(&devtree_lock, flags);
1697 if (__of_remove_property_from_list(&np->properties, prop)) {
1698 /* Found the property, add it to deadprops list */
1699 prop->next = np->deadprops;
1700 np->deadprops = prop;
1701 rc = 0;
1704 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1705 if (rc)
1706 return rc;
1708 __of_remove_property_sysfs(np, prop);
1709 return 0;
1713 * of_remove_property - Remove a property from a node.
1714 * @np: Caller's Device Node
1715 * @prop: Property to remove
1717 * Note that we don't actually remove it, since we have given out
1718 * who-knows-how-many pointers to the data using get-property.
1719 * Instead we just move the property to the "dead properties"
1720 * list, so it won't be found any more.
1722 int of_remove_property(struct device_node *np, struct property *prop)
1724 int rc;
1726 if (!prop)
1727 return -ENODEV;
1729 mutex_lock(&of_mutex);
1730 rc = __of_remove_property(np, prop);
1731 mutex_unlock(&of_mutex);
1733 if (!rc)
1734 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1736 return rc;
1738 EXPORT_SYMBOL_GPL(of_remove_property);
1740 int __of_update_property(struct device_node *np, struct property *newprop,
1741 struct property **oldpropp)
1743 struct property **next, *oldprop;
1744 unsigned long flags;
1746 raw_spin_lock_irqsave(&devtree_lock, flags);
1748 __of_remove_property_from_list(&np->deadprops, newprop);
1750 for (next = &np->properties; *next; next = &(*next)->next) {
1751 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1752 break;
1754 *oldpropp = oldprop = *next;
1756 if (oldprop) {
1757 /* replace the node */
1758 newprop->next = oldprop->next;
1759 *next = newprop;
1760 oldprop->next = np->deadprops;
1761 np->deadprops = oldprop;
1762 } else {
1763 /* new node */
1764 newprop->next = NULL;
1765 *next = newprop;
1768 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1770 __of_update_property_sysfs(np, newprop, oldprop);
1772 return 0;
1776 * of_update_property - Update a property in a node, if the property does
1777 * not exist, add it.
1779 * Note that we don't actually remove it, since we have given out
1780 * who-knows-how-many pointers to the data using get-property.
1781 * Instead we just move the property to the "dead properties" list,
1782 * and add the new property to the property list
1784 int of_update_property(struct device_node *np, struct property *newprop)
1786 struct property *oldprop;
1787 int rc;
1789 if (!newprop->name)
1790 return -EINVAL;
1792 mutex_lock(&of_mutex);
1793 rc = __of_update_property(np, newprop, &oldprop);
1794 mutex_unlock(&of_mutex);
1796 if (!rc)
1797 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1799 return rc;
1802 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1803 int id, const char *stem, int stem_len)
1805 ap->np = np;
1806 ap->id = id;
1807 strscpy(ap->stem, stem, stem_len + 1);
1808 list_add_tail(&ap->link, &aliases_lookup);
1809 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1810 ap->alias, ap->stem, ap->id, np);
1814 * of_alias_scan - Scan all properties of the 'aliases' node
1815 * @dt_alloc: An allocator that provides a virtual address to memory
1816 * for storing the resulting tree
1818 * The function scans all the properties of the 'aliases' node and populates
1819 * the global lookup table with the properties. It returns the
1820 * number of alias properties found, or an error code in case of failure.
1822 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1824 const struct property *pp;
1826 of_aliases = of_find_node_by_path("/aliases");
1827 of_chosen = of_find_node_by_path("/chosen");
1828 if (of_chosen == NULL)
1829 of_chosen = of_find_node_by_path("/chosen@0");
1831 if (of_chosen) {
1832 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
1833 const char *name = NULL;
1835 if (of_property_read_string(of_chosen, "stdout-path", &name))
1836 of_property_read_string(of_chosen, "linux,stdout-path",
1837 &name);
1838 if (IS_ENABLED(CONFIG_PPC) && !name)
1839 of_property_read_string(of_aliases, "stdout", &name);
1840 if (name)
1841 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
1842 if (of_stdout)
1843 of_stdout->fwnode.flags |= FWNODE_FLAG_BEST_EFFORT;
1846 if (!of_aliases)
1847 return;
1849 for_each_property_of_node(of_aliases, pp) {
1850 const char *start = pp->name;
1851 const char *end = start + strlen(start);
1852 struct device_node *np;
1853 struct alias_prop *ap;
1854 int id, len;
1856 /* Skip those we do not want to proceed */
1857 if (!strcmp(pp->name, "name") ||
1858 !strcmp(pp->name, "phandle") ||
1859 !strcmp(pp->name, "linux,phandle"))
1860 continue;
1862 np = of_find_node_by_path(pp->value);
1863 if (!np)
1864 continue;
1866 /* walk the alias backwards to extract the id and work out
1867 * the 'stem' string */
1868 while (isdigit(*(end-1)) && end > start)
1869 end--;
1870 len = end - start;
1872 if (kstrtoint(end, 10, &id) < 0)
1873 continue;
1875 /* Allocate an alias_prop with enough space for the stem */
1876 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
1877 if (!ap)
1878 continue;
1879 memset(ap, 0, sizeof(*ap) + len + 1);
1880 ap->alias = start;
1881 of_alias_add(ap, np, id, start, len);
1886 * of_alias_get_id - Get alias id for the given device_node
1887 * @np: Pointer to the given device_node
1888 * @stem: Alias stem of the given device_node
1890 * The function travels the lookup table to get the alias id for the given
1891 * device_node and alias stem.
1893 * Return: The alias id if found.
1895 int of_alias_get_id(const struct device_node *np, const char *stem)
1897 struct alias_prop *app;
1898 int id = -ENODEV;
1900 mutex_lock(&of_mutex);
1901 list_for_each_entry(app, &aliases_lookup, link) {
1902 if (strcmp(app->stem, stem) != 0)
1903 continue;
1905 if (np == app->np) {
1906 id = app->id;
1907 break;
1910 mutex_unlock(&of_mutex);
1912 return id;
1914 EXPORT_SYMBOL_GPL(of_alias_get_id);
1917 * of_alias_get_highest_id - Get highest alias id for the given stem
1918 * @stem: Alias stem to be examined
1920 * The function travels the lookup table to get the highest alias id for the
1921 * given alias stem. It returns the alias id if found.
1923 int of_alias_get_highest_id(const char *stem)
1925 struct alias_prop *app;
1926 int id = -ENODEV;
1928 mutex_lock(&of_mutex);
1929 list_for_each_entry(app, &aliases_lookup, link) {
1930 if (strcmp(app->stem, stem) != 0)
1931 continue;
1933 if (app->id > id)
1934 id = app->id;
1936 mutex_unlock(&of_mutex);
1938 return id;
1940 EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
1943 * of_console_check() - Test and setup console for DT setup
1944 * @dn: Pointer to device node
1945 * @name: Name to use for preferred console without index. ex. "ttyS"
1946 * @index: Index to use for preferred console.
1948 * Check if the given device node matches the stdout-path property in the
1949 * /chosen node. If it does then register it as the preferred console.
1951 * Return: TRUE if console successfully setup. Otherwise return FALSE.
1953 bool of_console_check(const struct device_node *dn, char *name, int index)
1955 if (!dn || dn != of_stdout || console_set_on_cmdline)
1956 return false;
1959 * XXX: cast `options' to char pointer to suppress complication
1960 * warnings: printk, UART and console drivers expect char pointer.
1962 return !add_preferred_console(name, index, (char *)of_stdout_options);
1964 EXPORT_SYMBOL_GPL(of_console_check);
1967 * of_find_next_cache_node - Find a node's subsidiary cache
1968 * @np: node of type "cpu" or "cache"
1970 * Return: A node pointer with refcount incremented, use
1971 * of_node_put() on it when done. Caller should hold a reference
1972 * to np.
1974 struct device_node *of_find_next_cache_node(const struct device_node *np)
1976 struct device_node *child, *cache_node;
1978 cache_node = of_parse_phandle(np, "l2-cache", 0);
1979 if (!cache_node)
1980 cache_node = of_parse_phandle(np, "next-level-cache", 0);
1982 if (cache_node)
1983 return cache_node;
1985 /* OF on pmac has nodes instead of properties named "l2-cache"
1986 * beneath CPU nodes.
1988 if (IS_ENABLED(CONFIG_PPC_PMAC) && of_node_is_type(np, "cpu"))
1989 for_each_child_of_node(np, child)
1990 if (of_node_is_type(child, "cache"))
1991 return child;
1993 return NULL;
1997 * of_find_last_cache_level - Find the level at which the last cache is
1998 * present for the given logical cpu
2000 * @cpu: cpu number(logical index) for which the last cache level is needed
2002 * Return: The level at which the last cache is present. It is exactly
2003 * same as the total number of cache levels for the given logical cpu.
2005 int of_find_last_cache_level(unsigned int cpu)
2007 u32 cache_level = 0;
2008 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2010 while (np) {
2011 of_node_put(prev);
2012 prev = np;
2013 np = of_find_next_cache_node(np);
2016 of_property_read_u32(prev, "cache-level", &cache_level);
2017 of_node_put(prev);
2019 return cache_level;
2023 * of_map_id - Translate an ID through a downstream mapping.
2024 * @np: root complex device node.
2025 * @id: device ID to map.
2026 * @map_name: property name of the map to use.
2027 * @map_mask_name: optional property name of the mask to use.
2028 * @target: optional pointer to a target device node.
2029 * @id_out: optional pointer to receive the translated ID.
2031 * Given a device ID, look up the appropriate implementation-defined
2032 * platform ID and/or the target device which receives transactions on that
2033 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
2034 * @id_out may be NULL if only the other is required. If @target points to
2035 * a non-NULL device node pointer, only entries targeting that node will be
2036 * matched; if it points to a NULL value, it will receive the device node of
2037 * the first matching target phandle, with a reference held.
2039 * Return: 0 on success or a standard error code on failure.
2041 int of_map_id(const struct device_node *np, u32 id,
2042 const char *map_name, const char *map_mask_name,
2043 struct device_node **target, u32 *id_out)
2045 u32 map_mask, masked_id;
2046 int map_len;
2047 const __be32 *map = NULL;
2049 if (!np || !map_name || (!target && !id_out))
2050 return -EINVAL;
2052 map = of_get_property(np, map_name, &map_len);
2053 if (!map) {
2054 if (target)
2055 return -ENODEV;
2056 /* Otherwise, no map implies no translation */
2057 *id_out = id;
2058 return 0;
2061 if (!map_len || map_len % (4 * sizeof(*map))) {
2062 pr_err("%pOF: Error: Bad %s length: %d\n", np,
2063 map_name, map_len);
2064 return -EINVAL;
2067 /* The default is to select all bits. */
2068 map_mask = 0xffffffff;
2071 * Can be overridden by "{iommu,msi}-map-mask" property.
2072 * If of_property_read_u32() fails, the default is used.
2074 if (map_mask_name)
2075 of_property_read_u32(np, map_mask_name, &map_mask);
2077 masked_id = map_mask & id;
2078 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
2079 struct device_node *phandle_node;
2080 u32 id_base = be32_to_cpup(map + 0);
2081 u32 phandle = be32_to_cpup(map + 1);
2082 u32 out_base = be32_to_cpup(map + 2);
2083 u32 id_len = be32_to_cpup(map + 3);
2085 if (id_base & ~map_mask) {
2086 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores id-base (0x%x)\n",
2087 np, map_name, map_name,
2088 map_mask, id_base);
2089 return -EFAULT;
2092 if (masked_id < id_base || masked_id >= id_base + id_len)
2093 continue;
2095 phandle_node = of_find_node_by_phandle(phandle);
2096 if (!phandle_node)
2097 return -ENODEV;
2099 if (target) {
2100 if (*target)
2101 of_node_put(phandle_node);
2102 else
2103 *target = phandle_node;
2105 if (*target != phandle_node)
2106 continue;
2109 if (id_out)
2110 *id_out = masked_id - id_base + out_base;
2112 pr_debug("%pOF: %s, using mask %08x, id-base: %08x, out-base: %08x, length: %08x, id: %08x -> %08x\n",
2113 np, map_name, map_mask, id_base, out_base,
2114 id_len, id, masked_id - id_base + out_base);
2115 return 0;
2118 pr_info("%pOF: no %s translation for id 0x%x on %pOF\n", np, map_name,
2119 id, target && *target ? *target : NULL);
2121 /* Bypasses translation */
2122 if (id_out)
2123 *id_out = id;
2124 return 0;
2126 EXPORT_SYMBOL_GPL(of_map_id);