1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Procedures for creating, accessing and interpreting the device tree.
4 *
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
7 *
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
10 *
11 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 *
13 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
14 * Grant Likely.
15 */
16
17 #define pr_fmt(fmt) "OF: " fmt
18
19 #include <linux/console.h>
20 #include <linux/ctype.h>
21 #include <linux/cpu.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/of_graph.h>
26 #include <linux/spinlock.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/proc_fs.h>
30
31 #include "of_private.h"
32
33 LIST_HEAD(aliases_lookup);
34
35 struct device_node *of_root;
36 EXPORT_SYMBOL(of_root);
37 struct device_node *of_chosen;
38 EXPORT_SYMBOL(of_chosen);
39 struct device_node *of_aliases;
40 struct device_node *of_stdout;
41 static const char *of_stdout_options;
42
43 struct kset *of_kset;
44
45 /*
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.
50 */
51 DEFINE_MUTEX(of_mutex);
52
53 /* use when traversing tree through the child, sibling,
54 * or parent members of struct device_node.
55 */
56 DEFINE_RAW_SPINLOCK(devtree_lock);
57
of_node_name_eq(const struct device_node * np,const char * name)58 bool of_node_name_eq(const struct device_node *np, const char *name)
59 {
60 const char *node_name;
61 size_t len;
62
63 if (!np)
64 return false;
65
66 node_name = kbasename(np->full_name);
67 len = strchrnul(node_name, '@') - node_name;
68
69 return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
70 }
71 EXPORT_SYMBOL(of_node_name_eq);
72
of_node_name_prefix(const struct device_node * np,const char * prefix)73 bool of_node_name_prefix(const struct device_node *np, const char *prefix)
74 {
75 if (!np)
76 return false;
77
78 return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
79 }
80 EXPORT_SYMBOL(of_node_name_prefix);
81
__of_node_is_type(const struct device_node * np,const char * type)82 static bool __of_node_is_type(const struct device_node *np, const char *type)
83 {
84 const char *match = __of_get_property(np, "device_type", NULL);
85
86 return np && match && type && !strcmp(match, type);
87 }
88
of_bus_n_addr_cells(struct device_node * np)89 int of_bus_n_addr_cells(struct device_node *np)
90 {
91 u32 cells;
92
93 for (; np; np = np->parent)
94 if (!of_property_read_u32(np, "#address-cells", &cells))
95 return cells;
96
97 /* No #address-cells property for the root node */
98 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
99 }
100
of_n_addr_cells(struct device_node * np)101 int of_n_addr_cells(struct device_node *np)
102 {
103 if (np->parent)
104 np = np->parent;
105
106 return of_bus_n_addr_cells(np);
107 }
108 EXPORT_SYMBOL(of_n_addr_cells);
109
of_bus_n_size_cells(struct device_node * np)110 int of_bus_n_size_cells(struct device_node *np)
111 {
112 u32 cells;
113
114 for (; np; np = np->parent)
115 if (!of_property_read_u32(np, "#size-cells", &cells))
116 return cells;
117
118 /* No #size-cells property for the root node */
119 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
120 }
121
of_n_size_cells(struct device_node * np)122 int of_n_size_cells(struct device_node *np)
123 {
124 if (np->parent)
125 np = np->parent;
126
127 return of_bus_n_size_cells(np);
128 }
129 EXPORT_SYMBOL(of_n_size_cells);
130
131 #ifdef CONFIG_NUMA
of_node_to_nid(struct device_node * np)132 int __weak of_node_to_nid(struct device_node *np)
133 {
134 return NUMA_NO_NODE;
135 }
136 #endif
137
138 #define OF_PHANDLE_CACHE_BITS 7
139 #define OF_PHANDLE_CACHE_SZ BIT(OF_PHANDLE_CACHE_BITS)
140
141 static struct device_node *phandle_cache[OF_PHANDLE_CACHE_SZ];
142
of_phandle_cache_hash(phandle handle)143 static u32 of_phandle_cache_hash(phandle handle)
144 {
145 return hash_32(handle, OF_PHANDLE_CACHE_BITS);
146 }
147
148 /*
149 * Caller must hold devtree_lock.
150 */
__of_phandle_cache_inv_entry(phandle handle)151 void __of_phandle_cache_inv_entry(phandle handle)
152 {
153 u32 handle_hash;
154 struct device_node *np;
155
156 if (!handle)
157 return;
158
159 handle_hash = of_phandle_cache_hash(handle);
160
161 np = phandle_cache[handle_hash];
162 if (np && handle == np->phandle)
163 phandle_cache[handle_hash] = NULL;
164 }
165
of_core_init(void)166 void __init of_core_init(void)
167 {
168 struct device_node *np;
169
170 of_platform_register_reconfig_notifier();
171
172 /* Create the kset, and register existing nodes */
173 mutex_lock(&of_mutex);
174 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
175 if (!of_kset) {
176 mutex_unlock(&of_mutex);
177 pr_err("failed to register existing nodes\n");
178 return;
179 }
180 for_each_of_allnodes(np) {
181 __of_attach_node_sysfs(np);
182 if (np->phandle && !phandle_cache[of_phandle_cache_hash(np->phandle)])
183 phandle_cache[of_phandle_cache_hash(np->phandle)] = np;
184 }
185 mutex_unlock(&of_mutex);
186
187 /* Symlink in /proc as required by userspace ABI */
188 if (of_root)
189 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
190 }
191
__of_find_property(const struct device_node * np,const char * name,int * lenp)192 static struct property *__of_find_property(const struct device_node *np,
193 const char *name, int *lenp)
194 {
195 struct property *pp;
196
197 if (!np)
198 return NULL;
199
200 for (pp = np->properties; pp; pp = pp->next) {
201 if (of_prop_cmp(pp->name, name) == 0) {
202 if (lenp)
203 *lenp = pp->length;
204 break;
205 }
206 }
207
208 return pp;
209 }
210
of_find_property(const struct device_node * np,const char * name,int * lenp)211 struct property *of_find_property(const struct device_node *np,
212 const char *name,
213 int *lenp)
214 {
215 struct property *pp;
216 unsigned long flags;
217
218 raw_spin_lock_irqsave(&devtree_lock, flags);
219 pp = __of_find_property(np, name, lenp);
220 raw_spin_unlock_irqrestore(&devtree_lock, flags);
221
222 return pp;
223 }
224 EXPORT_SYMBOL(of_find_property);
225
__of_find_all_nodes(struct device_node * prev)226 struct device_node *__of_find_all_nodes(struct device_node *prev)
227 {
228 struct device_node *np;
229 if (!prev) {
230 np = of_root;
231 } else if (prev->child) {
232 np = prev->child;
233 } else {
234 /* Walk back up looking for a sibling, or the end of the structure */
235 np = prev;
236 while (np->parent && !np->sibling)
237 np = np->parent;
238 np = np->sibling; /* Might be null at the end of the tree */
239 }
240 return np;
241 }
242
243 /**
244 * of_find_all_nodes - Get next node in global list
245 * @prev: Previous node or NULL to start iteration
246 * of_node_put() will be called on it
247 *
248 * Return: A node pointer with refcount incremented, use
249 * of_node_put() on it when done.
250 */
of_find_all_nodes(struct device_node * prev)251 struct device_node *of_find_all_nodes(struct device_node *prev)
252 {
253 struct device_node *np;
254 unsigned long flags;
255
256 raw_spin_lock_irqsave(&devtree_lock, flags);
257 np = __of_find_all_nodes(prev);
258 of_node_get(np);
259 of_node_put(prev);
260 raw_spin_unlock_irqrestore(&devtree_lock, flags);
261 return np;
262 }
263 EXPORT_SYMBOL(of_find_all_nodes);
264
265 /*
266 * Find a property with a given name for a given node
267 * and return the value.
268 */
__of_get_property(const struct device_node * np,const char * name,int * lenp)269 const void *__of_get_property(const struct device_node *np,
270 const char *name, int *lenp)
271 {
272 struct property *pp = __of_find_property(np, name, lenp);
273
274 return pp ? pp->value : NULL;
275 }
276
277 /*
278 * Find a property with a given name for a given node
279 * and return the value.
280 */
of_get_property(const struct device_node * np,const char * name,int * lenp)281 const void *of_get_property(const struct device_node *np, const char *name,
282 int *lenp)
283 {
284 struct property *pp = of_find_property(np, name, lenp);
285
286 return pp ? pp->value : NULL;
287 }
288 EXPORT_SYMBOL(of_get_property);
289
290 /**
291 * __of_device_is_compatible() - Check if the node matches given constraints
292 * @device: pointer to node
293 * @compat: required compatible string, NULL or "" for any match
294 * @type: required device_type value, NULL or "" for any match
295 * @name: required node name, NULL or "" for any match
296 *
297 * Checks if the given @compat, @type and @name strings match the
298 * properties of the given @device. A constraints can be skipped by
299 * passing NULL or an empty string as the constraint.
300 *
301 * Returns 0 for no match, and a positive integer on match. The return
302 * value is a relative score with larger values indicating better
303 * matches. The score is weighted for the most specific compatible value
304 * to get the highest score. Matching type is next, followed by matching
305 * name. Practically speaking, this results in the following priority
306 * order for matches:
307 *
308 * 1. specific compatible && type && name
309 * 2. specific compatible && type
310 * 3. specific compatible && name
311 * 4. specific compatible
312 * 5. general compatible && type && name
313 * 6. general compatible && type
314 * 7. general compatible && name
315 * 8. general compatible
316 * 9. type && name
317 * 10. type
318 * 11. name
319 */
__of_device_is_compatible(const struct device_node * device,const char * compat,const char * type,const char * name)320 static int __of_device_is_compatible(const struct device_node *device,
321 const char *compat, const char *type, const char *name)
322 {
323 struct property *prop;
324 const char *cp;
325 int index = 0, score = 0;
326
327 /* Compatible match has highest priority */
328 if (compat && compat[0]) {
329 prop = __of_find_property(device, "compatible", NULL);
330 for (cp = of_prop_next_string(prop, NULL); cp;
331 cp = of_prop_next_string(prop, cp), index++) {
332 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
333 score = INT_MAX/2 - (index << 2);
334 break;
335 }
336 }
337 if (!score)
338 return 0;
339 }
340
341 /* Matching type is better than matching name */
342 if (type && type[0]) {
343 if (!__of_node_is_type(device, type))
344 return 0;
345 score += 2;
346 }
347
348 /* Matching name is a bit better than not */
349 if (name && name[0]) {
350 if (!of_node_name_eq(device, name))
351 return 0;
352 score++;
353 }
354
355 return score;
356 }
357
358 /** Checks if the given "compat" string matches one of the strings in
359 * the device's "compatible" property
360 */
of_device_is_compatible(const struct device_node * device,const char * compat)361 int of_device_is_compatible(const struct device_node *device,
362 const char *compat)
363 {
364 unsigned long flags;
365 int res;
366
367 raw_spin_lock_irqsave(&devtree_lock, flags);
368 res = __of_device_is_compatible(device, compat, NULL, NULL);
369 raw_spin_unlock_irqrestore(&devtree_lock, flags);
370 return res;
371 }
372 EXPORT_SYMBOL(of_device_is_compatible);
373
374 /** Checks if the device is compatible with any of the entries in
375 * a NULL terminated array of strings. Returns the best match
376 * score or 0.
377 */
of_device_compatible_match(const struct device_node * device,const char * const * compat)378 int of_device_compatible_match(const struct device_node *device,
379 const char *const *compat)
380 {
381 unsigned int tmp, score = 0;
382
383 if (!compat)
384 return 0;
385
386 while (*compat) {
387 tmp = of_device_is_compatible(device, *compat);
388 if (tmp > score)
389 score = tmp;
390 compat++;
391 }
392
393 return score;
394 }
395 EXPORT_SYMBOL_GPL(of_device_compatible_match);
396
397 /**
398 * of_machine_compatible_match - Test root of device tree against a compatible array
399 * @compats: NULL terminated array of compatible strings to look for in root node's compatible property.
400 *
401 * Returns true if the root node has any of the given compatible values in its
402 * compatible property.
403 */
of_machine_compatible_match(const char * const * compats)404 bool of_machine_compatible_match(const char *const *compats)
405 {
406 struct device_node *root;
407 int rc = 0;
408
409 root = of_find_node_by_path("/");
410 if (root) {
411 rc = of_device_compatible_match(root, compats);
412 of_node_put(root);
413 }
414
415 return rc != 0;
416 }
417 EXPORT_SYMBOL(of_machine_compatible_match);
418
419 /**
420 * __of_device_is_available - check if a device is available for use
421 *
422 * @device: Node to check for availability, with locks already held
423 *
424 * Return: True if the status property is absent or set to "okay" or "ok",
425 * false otherwise
426 */
__of_device_is_available(const struct device_node * device)427 static bool __of_device_is_available(const struct device_node *device)
428 {
429 const char *status;
430 int statlen;
431
432 if (!device)
433 return false;
434
435 status = __of_get_property(device, "status", &statlen);
436 if (status == NULL)
437 return true;
438
439 if (statlen > 0) {
440 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
441 return true;
442 }
443
444 return false;
445 }
446
447 /**
448 * of_device_is_available - check if a device is available for use
449 *
450 * @device: Node to check for availability
451 *
452 * Return: True if the status property is absent or set to "okay" or "ok",
453 * false otherwise
454 */
of_device_is_available(const struct device_node * device)455 bool of_device_is_available(const struct device_node *device)
456 {
457 unsigned long flags;
458 bool res;
459
460 raw_spin_lock_irqsave(&devtree_lock, flags);
461 res = __of_device_is_available(device);
462 raw_spin_unlock_irqrestore(&devtree_lock, flags);
463 return res;
464
465 }
466 EXPORT_SYMBOL(of_device_is_available);
467
468 /**
469 * __of_device_is_fail - check if a device has status "fail" or "fail-..."
470 *
471 * @device: Node to check status for, with locks already held
472 *
473 * Return: True if the status property is set to "fail" or "fail-..." (for any
474 * error code suffix), false otherwise
475 */
__of_device_is_fail(const struct device_node * device)476 static bool __of_device_is_fail(const struct device_node *device)
477 {
478 const char *status;
479
480 if (!device)
481 return false;
482
483 status = __of_get_property(device, "status", NULL);
484 if (status == NULL)
485 return false;
486
487 return !strcmp(status, "fail") || !strncmp(status, "fail-", 5);
488 }
489
490 /**
491 * of_device_is_big_endian - check if a device has BE registers
492 *
493 * @device: Node to check for endianness
494 *
495 * Return: True if the device has a "big-endian" property, or if the kernel
496 * was compiled for BE *and* the device has a "native-endian" property.
497 * Returns false otherwise.
498 *
499 * Callers would nominally use ioread32be/iowrite32be if
500 * of_device_is_big_endian() == true, or readl/writel otherwise.
501 */
of_device_is_big_endian(const struct device_node * device)502 bool of_device_is_big_endian(const struct device_node *device)
503 {
504 if (of_property_read_bool(device, "big-endian"))
505 return true;
506 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
507 of_property_read_bool(device, "native-endian"))
508 return true;
509 return false;
510 }
511 EXPORT_SYMBOL(of_device_is_big_endian);
512
513 /**
514 * of_get_parent - Get a node's parent if any
515 * @node: Node to get parent
516 *
517 * Return: A node pointer with refcount incremented, use
518 * of_node_put() on it when done.
519 */
of_get_parent(const struct device_node * node)520 struct device_node *of_get_parent(const struct device_node *node)
521 {
522 struct device_node *np;
523 unsigned long flags;
524
525 if (!node)
526 return NULL;
527
528 raw_spin_lock_irqsave(&devtree_lock, flags);
529 np = of_node_get(node->parent);
530 raw_spin_unlock_irqrestore(&devtree_lock, flags);
531 return np;
532 }
533 EXPORT_SYMBOL(of_get_parent);
534
535 /**
536 * of_get_next_parent - Iterate to a node's parent
537 * @node: Node to get parent of
538 *
539 * This is like of_get_parent() except that it drops the
540 * refcount on the passed node, making it suitable for iterating
541 * through a node's parents.
542 *
543 * Return: A node pointer with refcount incremented, use
544 * of_node_put() on it when done.
545 */
of_get_next_parent(struct device_node * node)546 struct device_node *of_get_next_parent(struct device_node *node)
547 {
548 struct device_node *parent;
549 unsigned long flags;
550
551 if (!node)
552 return NULL;
553
554 raw_spin_lock_irqsave(&devtree_lock, flags);
555 parent = of_node_get(node->parent);
556 of_node_put(node);
557 raw_spin_unlock_irqrestore(&devtree_lock, flags);
558 return parent;
559 }
560 EXPORT_SYMBOL(of_get_next_parent);
561
__of_get_next_child(const struct device_node * node,struct device_node * prev)562 static struct device_node *__of_get_next_child(const struct device_node *node,
563 struct device_node *prev)
564 {
565 struct device_node *next;
566
567 if (!node)
568 return NULL;
569
570 next = prev ? prev->sibling : node->child;
571 of_node_get(next);
572 of_node_put(prev);
573 return next;
574 }
575 #define __for_each_child_of_node(parent, child) \
576 for (child = __of_get_next_child(parent, NULL); child != NULL; \
577 child = __of_get_next_child(parent, child))
578
579 /**
580 * of_get_next_child - Iterate a node childs
581 * @node: parent node
582 * @prev: previous child of the parent node, or NULL to get first
583 *
584 * Return: A node pointer with refcount incremented, use of_node_put() on
585 * it when done. Returns NULL when prev is the last child. Decrements the
586 * refcount of prev.
587 */
of_get_next_child(const struct device_node * node,struct device_node * prev)588 struct device_node *of_get_next_child(const struct device_node *node,
589 struct device_node *prev)
590 {
591 struct device_node *next;
592 unsigned long flags;
593
594 raw_spin_lock_irqsave(&devtree_lock, flags);
595 next = __of_get_next_child(node, prev);
596 raw_spin_unlock_irqrestore(&devtree_lock, flags);
597 return next;
598 }
599 EXPORT_SYMBOL(of_get_next_child);
600
601 /**
602 * of_get_next_available_child - Find the next available child node
603 * @node: parent node
604 * @prev: previous child of the parent node, or NULL to get first
605 *
606 * This function is like of_get_next_child(), except that it
607 * automatically skips any disabled nodes (i.e. status = "disabled").
608 */
of_get_next_available_child(const struct device_node * node,struct device_node * prev)609 struct device_node *of_get_next_available_child(const struct device_node *node,
610 struct device_node *prev)
611 {
612 struct device_node *next;
613 unsigned long flags;
614
615 if (!node)
616 return NULL;
617
618 raw_spin_lock_irqsave(&devtree_lock, flags);
619 next = prev ? prev->sibling : node->child;
620 for (; next; next = next->sibling) {
621 if (!__of_device_is_available(next))
622 continue;
623 if (of_node_get(next))
624 break;
625 }
626 of_node_put(prev);
627 raw_spin_unlock_irqrestore(&devtree_lock, flags);
628 return next;
629 }
630 EXPORT_SYMBOL(of_get_next_available_child);
631
632 /**
633 * of_get_next_cpu_node - Iterate on cpu nodes
634 * @prev: previous child of the /cpus node, or NULL to get first
635 *
636 * Unusable CPUs (those with the status property set to "fail" or "fail-...")
637 * will be skipped.
638 *
639 * Return: A cpu node pointer with refcount incremented, use of_node_put()
640 * on it when done. Returns NULL when prev is the last child. Decrements
641 * the refcount of prev.
642 */
of_get_next_cpu_node(struct device_node * prev)643 struct device_node *of_get_next_cpu_node(struct device_node *prev)
644 {
645 struct device_node *next = NULL;
646 unsigned long flags;
647 struct device_node *node;
648
649 if (!prev)
650 node = of_find_node_by_path("/cpus");
651
652 raw_spin_lock_irqsave(&devtree_lock, flags);
653 if (prev)
654 next = prev->sibling;
655 else if (node) {
656 next = node->child;
657 of_node_put(node);
658 }
659 for (; next; next = next->sibling) {
660 if (__of_device_is_fail(next))
661 continue;
662 if (!(of_node_name_eq(next, "cpu") ||
663 __of_node_is_type(next, "cpu")))
664 continue;
665 if (of_node_get(next))
666 break;
667 }
668 of_node_put(prev);
669 raw_spin_unlock_irqrestore(&devtree_lock, flags);
670 return next;
671 }
672 EXPORT_SYMBOL(of_get_next_cpu_node);
673
674 /**
675 * of_get_compatible_child - Find compatible child node
676 * @parent: parent node
677 * @compatible: compatible string
678 *
679 * Lookup child node whose compatible property contains the given compatible
680 * string.
681 *
682 * Return: a node pointer with refcount incremented, use of_node_put() on it
683 * when done; or NULL if not found.
684 */
of_get_compatible_child(const struct device_node * parent,const char * compatible)685 struct device_node *of_get_compatible_child(const struct device_node *parent,
686 const char *compatible)
687 {
688 struct device_node *child;
689
690 for_each_child_of_node(parent, child) {
691 if (of_device_is_compatible(child, compatible))
692 break;
693 }
694
695 return child;
696 }
697 EXPORT_SYMBOL(of_get_compatible_child);
698
699 /**
700 * of_get_child_by_name - Find the child node by name for a given parent
701 * @node: parent node
702 * @name: child name to look for.
703 *
704 * This function looks for child node for given matching name
705 *
706 * Return: A node pointer if found, with refcount incremented, use
707 * of_node_put() on it when done.
708 * Returns NULL if node is not found.
709 */
of_get_child_by_name(const struct device_node * node,const char * name)710 struct device_node *of_get_child_by_name(const struct device_node *node,
711 const char *name)
712 {
713 struct device_node *child;
714
715 for_each_child_of_node(node, child)
716 if (of_node_name_eq(child, name))
717 break;
718 return child;
719 }
720 EXPORT_SYMBOL(of_get_child_by_name);
721
__of_find_node_by_path(struct device_node * parent,const char * path)722 struct device_node *__of_find_node_by_path(struct device_node *parent,
723 const char *path)
724 {
725 struct device_node *child;
726 int len;
727
728 len = strcspn(path, "/:");
729 if (!len)
730 return NULL;
731
732 __for_each_child_of_node(parent, child) {
733 const char *name = kbasename(child->full_name);
734 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
735 return child;
736 }
737 return NULL;
738 }
739
__of_find_node_by_full_path(struct device_node * node,const char * path)740 struct device_node *__of_find_node_by_full_path(struct device_node *node,
741 const char *path)
742 {
743 const char *separator = strchr(path, ':');
744
745 while (node && *path == '/') {
746 struct device_node *tmp = node;
747
748 path++; /* Increment past '/' delimiter */
749 node = __of_find_node_by_path(node, path);
750 of_node_put(tmp);
751 path = strchrnul(path, '/');
752 if (separator && separator < path)
753 break;
754 }
755 return node;
756 }
757
758 /**
759 * of_find_node_opts_by_path - Find a node matching a full OF path
760 * @path: Either the full path to match, or if the path does not
761 * start with '/', the name of a property of the /aliases
762 * node (an alias). In the case of an alias, the node
763 * matching the alias' value will be returned.
764 * @opts: Address of a pointer into which to store the start of
765 * an options string appended to the end of the path with
766 * a ':' separator.
767 *
768 * Valid paths:
769 * * /foo/bar Full path
770 * * foo Valid alias
771 * * foo/bar Valid alias + relative path
772 *
773 * Return: A node pointer with refcount incremented, use
774 * of_node_put() on it when done.
775 */
of_find_node_opts_by_path(const char * path,const char ** opts)776 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
777 {
778 struct device_node *np = NULL;
779 struct property *pp;
780 unsigned long flags;
781 const char *separator = strchr(path, ':');
782
783 if (opts)
784 *opts = separator ? separator + 1 : NULL;
785
786 if (strcmp(path, "/") == 0)
787 return of_node_get(of_root);
788
789 /* The path could begin with an alias */
790 if (*path != '/') {
791 int len;
792 const char *p = separator;
793
794 if (!p)
795 p = strchrnul(path, '/');
796 len = p - path;
797
798 /* of_aliases must not be NULL */
799 if (!of_aliases)
800 return NULL;
801
802 for_each_property_of_node(of_aliases, pp) {
803 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
804 np = of_find_node_by_path(pp->value);
805 break;
806 }
807 }
808 if (!np)
809 return NULL;
810 path = p;
811 }
812
813 /* Step down the tree matching path components */
814 raw_spin_lock_irqsave(&devtree_lock, flags);
815 if (!np)
816 np = of_node_get(of_root);
817 np = __of_find_node_by_full_path(np, path);
818 raw_spin_unlock_irqrestore(&devtree_lock, flags);
819 return np;
820 }
821 EXPORT_SYMBOL(of_find_node_opts_by_path);
822
823 /**
824 * of_find_node_by_name - Find a node by its "name" property
825 * @from: The node to start searching from or NULL; the node
826 * you pass will not be searched, only the next one
827 * will. Typically, you pass what the previous call
828 * returned. of_node_put() will be called on @from.
829 * @name: The name string to match against
830 *
831 * Return: A node pointer with refcount incremented, use
832 * of_node_put() on it when done.
833 */
of_find_node_by_name(struct device_node * from,const char * name)834 struct device_node *of_find_node_by_name(struct device_node *from,
835 const char *name)
836 {
837 struct device_node *np;
838 unsigned long flags;
839
840 raw_spin_lock_irqsave(&devtree_lock, flags);
841 for_each_of_allnodes_from(from, np)
842 if (of_node_name_eq(np, name) && of_node_get(np))
843 break;
844 of_node_put(from);
845 raw_spin_unlock_irqrestore(&devtree_lock, flags);
846 return np;
847 }
848 EXPORT_SYMBOL(of_find_node_by_name);
849
850 /**
851 * of_find_node_by_type - Find a node by its "device_type" property
852 * @from: The node to start searching from, or NULL to start searching
853 * the entire device tree. The node you pass will not be
854 * searched, only the next one will; typically, you pass
855 * what the previous call returned. of_node_put() will be
856 * called on from for you.
857 * @type: The type string to match against
858 *
859 * Return: A node pointer with refcount incremented, use
860 * of_node_put() on it when done.
861 */
of_find_node_by_type(struct device_node * from,const char * type)862 struct device_node *of_find_node_by_type(struct device_node *from,
863 const char *type)
864 {
865 struct device_node *np;
866 unsigned long flags;
867
868 raw_spin_lock_irqsave(&devtree_lock, flags);
869 for_each_of_allnodes_from(from, np)
870 if (__of_node_is_type(np, type) && of_node_get(np))
871 break;
872 of_node_put(from);
873 raw_spin_unlock_irqrestore(&devtree_lock, flags);
874 return np;
875 }
876 EXPORT_SYMBOL(of_find_node_by_type);
877
878 /**
879 * of_find_compatible_node - Find a node based on type and one of the
880 * tokens in its "compatible" property
881 * @from: The node to start searching from or NULL, the node
882 * you pass will not be searched, only the next one
883 * will; typically, you pass what the previous call
884 * returned. of_node_put() will be called on it
885 * @type: The type string to match "device_type" or NULL to ignore
886 * @compatible: The string to match to one of the tokens in the device
887 * "compatible" list.
888 *
889 * Return: A node pointer with refcount incremented, use
890 * of_node_put() on it when done.
891 */
of_find_compatible_node(struct device_node * from,const char * type,const char * compatible)892 struct device_node *of_find_compatible_node(struct device_node *from,
893 const char *type, const char *compatible)
894 {
895 struct device_node *np;
896 unsigned long flags;
897
898 raw_spin_lock_irqsave(&devtree_lock, flags);
899 for_each_of_allnodes_from(from, np)
900 if (__of_device_is_compatible(np, compatible, type, NULL) &&
901 of_node_get(np))
902 break;
903 of_node_put(from);
904 raw_spin_unlock_irqrestore(&devtree_lock, flags);
905 return np;
906 }
907 EXPORT_SYMBOL(of_find_compatible_node);
908
909 /**
910 * of_find_node_with_property - Find a node which has a property with
911 * the given name.
912 * @from: The node to start searching from or NULL, the node
913 * you pass will not be searched, only the next one
914 * will; typically, you pass what the previous call
915 * returned. of_node_put() will be called on it
916 * @prop_name: The name of the property to look for.
917 *
918 * Return: A node pointer with refcount incremented, use
919 * of_node_put() on it when done.
920 */
of_find_node_with_property(struct device_node * from,const char * prop_name)921 struct device_node *of_find_node_with_property(struct device_node *from,
922 const char *prop_name)
923 {
924 struct device_node *np;
925 struct property *pp;
926 unsigned long flags;
927
928 raw_spin_lock_irqsave(&devtree_lock, flags);
929 for_each_of_allnodes_from(from, np) {
930 for (pp = np->properties; pp; pp = pp->next) {
931 if (of_prop_cmp(pp->name, prop_name) == 0) {
932 of_node_get(np);
933 goto out;
934 }
935 }
936 }
937 out:
938 of_node_put(from);
939 raw_spin_unlock_irqrestore(&devtree_lock, flags);
940 return np;
941 }
942 EXPORT_SYMBOL(of_find_node_with_property);
943
944 static
__of_match_node(const struct of_device_id * matches,const struct device_node * node)945 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
946 const struct device_node *node)
947 {
948 const struct of_device_id *best_match = NULL;
949 int score, best_score = 0;
950
951 if (!matches)
952 return NULL;
953
954 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
955 score = __of_device_is_compatible(node, matches->compatible,
956 matches->type, matches->name);
957 if (score > best_score) {
958 best_match = matches;
959 best_score = score;
960 }
961 }
962
963 return best_match;
964 }
965
966 /**
967 * of_match_node - Tell if a device_node has a matching of_match structure
968 * @matches: array of of device match structures to search in
969 * @node: the of device structure to match against
970 *
971 * Low level utility function used by device matching.
972 */
of_match_node(const struct of_device_id * matches,const struct device_node * node)973 const struct of_device_id *of_match_node(const struct of_device_id *matches,
974 const struct device_node *node)
975 {
976 const struct of_device_id *match;
977 unsigned long flags;
978
979 raw_spin_lock_irqsave(&devtree_lock, flags);
980 match = __of_match_node(matches, node);
981 raw_spin_unlock_irqrestore(&devtree_lock, flags);
982 return match;
983 }
984 EXPORT_SYMBOL(of_match_node);
985
986 /**
987 * of_find_matching_node_and_match - Find a node based on an of_device_id
988 * match table.
989 * @from: The node to start searching from or NULL, the node
990 * you pass will not be searched, only the next one
991 * will; typically, you pass what the previous call
992 * returned. of_node_put() will be called on it
993 * @matches: array of of device match structures to search in
994 * @match: Updated to point at the matches entry which matched
995 *
996 * Return: A node pointer with refcount incremented, use
997 * of_node_put() on it when done.
998 */
of_find_matching_node_and_match(struct device_node * from,const struct of_device_id * matches,const struct of_device_id ** match)999 struct device_node *of_find_matching_node_and_match(struct device_node *from,
1000 const struct of_device_id *matches,
1001 const struct of_device_id **match)
1002 {
1003 struct device_node *np;
1004 const struct of_device_id *m;
1005 unsigned long flags;
1006
1007 if (match)
1008 *match = NULL;
1009
1010 raw_spin_lock_irqsave(&devtree_lock, flags);
1011 for_each_of_allnodes_from(from, np) {
1012 m = __of_match_node(matches, np);
1013 if (m && of_node_get(np)) {
1014 if (match)
1015 *match = m;
1016 break;
1017 }
1018 }
1019 of_node_put(from);
1020 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1021 return np;
1022 }
1023 EXPORT_SYMBOL(of_find_matching_node_and_match);
1024
1025 /**
1026 * of_alias_from_compatible - Lookup appropriate alias for a device node
1027 * depending on compatible
1028 * @node: pointer to a device tree node
1029 * @alias: Pointer to buffer that alias value will be copied into
1030 * @len: Length of alias value
1031 *
1032 * Based on the value of the compatible property, this routine will attempt
1033 * to choose an appropriate alias value for a particular device tree node.
1034 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1035 * from the first entry in the compatible list property.
1036 *
1037 * Note: The matching on just the "product" side of the compatible is a relic
1038 * from I2C and SPI. Please do not add any new user.
1039 *
1040 * Return: This routine returns 0 on success, <0 on failure.
1041 */
of_alias_from_compatible(const struct device_node * node,char * alias,int len)1042 int of_alias_from_compatible(const struct device_node *node, char *alias, int len)
1043 {
1044 const char *compatible, *p;
1045 int cplen;
1046
1047 compatible = of_get_property(node, "compatible", &cplen);
1048 if (!compatible || strlen(compatible) > cplen)
1049 return -ENODEV;
1050 p = strchr(compatible, ',');
1051 strscpy(alias, p ? p + 1 : compatible, len);
1052 return 0;
1053 }
1054 EXPORT_SYMBOL_GPL(of_alias_from_compatible);
1055
1056 /**
1057 * of_find_node_by_phandle - Find a node given a phandle
1058 * @handle: phandle of the node to find
1059 *
1060 * Return: A node pointer with refcount incremented, use
1061 * of_node_put() on it when done.
1062 */
of_find_node_by_phandle(phandle handle)1063 struct device_node *of_find_node_by_phandle(phandle handle)
1064 {
1065 struct device_node *np = NULL;
1066 unsigned long flags;
1067 u32 handle_hash;
1068
1069 if (!handle)
1070 return NULL;
1071
1072 handle_hash = of_phandle_cache_hash(handle);
1073
1074 raw_spin_lock_irqsave(&devtree_lock, flags);
1075
1076 if (phandle_cache[handle_hash] &&
1077 handle == phandle_cache[handle_hash]->phandle)
1078 np = phandle_cache[handle_hash];
1079
1080 if (!np) {
1081 for_each_of_allnodes(np)
1082 if (np->phandle == handle &&
1083 !of_node_check_flag(np, OF_DETACHED)) {
1084 phandle_cache[handle_hash] = np;
1085 break;
1086 }
1087 }
1088
1089 of_node_get(np);
1090 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1091 return np;
1092 }
1093 EXPORT_SYMBOL(of_find_node_by_phandle);
1094
of_print_phandle_args(const char * msg,const struct of_phandle_args * args)1095 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1096 {
1097 int i;
1098 printk("%s %pOF", msg, args->np);
1099 for (i = 0; i < args->args_count; i++) {
1100 const char delim = i ? ',' : ':';
1101
1102 pr_cont("%c%08x", delim, args->args[i]);
1103 }
1104 pr_cont("\n");
1105 }
1106
of_phandle_iterator_init(struct of_phandle_iterator * it,const struct device_node * np,const char * list_name,const char * cells_name,int cell_count)1107 int of_phandle_iterator_init(struct of_phandle_iterator *it,
1108 const struct device_node *np,
1109 const char *list_name,
1110 const char *cells_name,
1111 int cell_count)
1112 {
1113 const __be32 *list;
1114 int size;
1115
1116 memset(it, 0, sizeof(*it));
1117
1118 /*
1119 * one of cell_count or cells_name must be provided to determine the
1120 * argument length.
1121 */
1122 if (cell_count < 0 && !cells_name)
1123 return -EINVAL;
1124
1125 list = of_get_property(np, list_name, &size);
1126 if (!list)
1127 return -ENOENT;
1128
1129 it->cells_name = cells_name;
1130 it->cell_count = cell_count;
1131 it->parent = np;
1132 it->list_end = list + size / sizeof(*list);
1133 it->phandle_end = list;
1134 it->cur = list;
1135
1136 return 0;
1137 }
1138 EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
1139
of_phandle_iterator_next(struct of_phandle_iterator * it)1140 int of_phandle_iterator_next(struct of_phandle_iterator *it)
1141 {
1142 uint32_t count = 0;
1143
1144 if (it->node) {
1145 of_node_put(it->node);
1146 it->node = NULL;
1147 }
1148
1149 if (!it->cur || it->phandle_end >= it->list_end)
1150 return -ENOENT;
1151
1152 it->cur = it->phandle_end;
1153
1154 /* If phandle is 0, then it is an empty entry with no arguments. */
1155 it->phandle = be32_to_cpup(it->cur++);
1156
1157 if (it->phandle) {
1158
1159 /*
1160 * Find the provider node and parse the #*-cells property to
1161 * determine the argument length.
1162 */
1163 it->node = of_find_node_by_phandle(it->phandle);
1164
1165 if (it->cells_name) {
1166 if (!it->node) {
1167 pr_err("%pOF: could not find phandle %d\n",
1168 it->parent, it->phandle);
1169 goto err;
1170 }
1171
1172 if (of_property_read_u32(it->node, it->cells_name,
1173 &count)) {
1174 /*
1175 * If both cell_count and cells_name is given,
1176 * fall back to cell_count in absence
1177 * of the cells_name property
1178 */
1179 if (it->cell_count >= 0) {
1180 count = it->cell_count;
1181 } else {
1182 pr_err("%pOF: could not get %s for %pOF\n",
1183 it->parent,
1184 it->cells_name,
1185 it->node);
1186 goto err;
1187 }
1188 }
1189 } else {
1190 count = it->cell_count;
1191 }
1192
1193 /*
1194 * Make sure that the arguments actually fit in the remaining
1195 * property data length
1196 */
1197 if (it->cur + count > it->list_end) {
1198 if (it->cells_name)
1199 pr_err("%pOF: %s = %d found %td\n",
1200 it->parent, it->cells_name,
1201 count, it->list_end - it->cur);
1202 else
1203 pr_err("%pOF: phandle %s needs %d, found %td\n",
1204 it->parent, of_node_full_name(it->node),
1205 count, it->list_end - it->cur);
1206 goto err;
1207 }
1208 }
1209
1210 it->phandle_end = it->cur + count;
1211 it->cur_count = count;
1212
1213 return 0;
1214
1215 err:
1216 if (it->node) {
1217 of_node_put(it->node);
1218 it->node = NULL;
1219 }
1220
1221 return -EINVAL;
1222 }
1223 EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
1224
of_phandle_iterator_args(struct of_phandle_iterator * it,uint32_t * args,int size)1225 int of_phandle_iterator_args(struct of_phandle_iterator *it,
1226 uint32_t *args,
1227 int size)
1228 {
1229 int i, count;
1230
1231 count = it->cur_count;
1232
1233 if (WARN_ON(size < count))
1234 count = size;
1235
1236 for (i = 0; i < count; i++)
1237 args[i] = be32_to_cpup(it->cur++);
1238
1239 return count;
1240 }
1241
__of_parse_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name,int cell_count,int index,struct of_phandle_args * out_args)1242 int __of_parse_phandle_with_args(const struct device_node *np,
1243 const char *list_name,
1244 const char *cells_name,
1245 int cell_count, int index,
1246 struct of_phandle_args *out_args)
1247 {
1248 struct of_phandle_iterator it;
1249 int rc, cur_index = 0;
1250
1251 if (index < 0)
1252 return -EINVAL;
1253
1254 /* Loop over the phandles until all the requested entry is found */
1255 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
1256 /*
1257 * All of the error cases bail out of the loop, so at
1258 * this point, the parsing is successful. If the requested
1259 * index matches, then fill the out_args structure and return,
1260 * or return -ENOENT for an empty entry.
1261 */
1262 rc = -ENOENT;
1263 if (cur_index == index) {
1264 if (!it.phandle)
1265 goto err;
1266
1267 if (out_args) {
1268 int c;
1269
1270 c = of_phandle_iterator_args(&it,
1271 out_args->args,
1272 MAX_PHANDLE_ARGS);
1273 out_args->np = it.node;
1274 out_args->args_count = c;
1275 } else {
1276 of_node_put(it.node);
1277 }
1278
1279 /* Found it! return success */
1280 return 0;
1281 }
1282
1283 cur_index++;
1284 }
1285
1286 /*
1287 * Unlock node before returning result; will be one of:
1288 * -ENOENT : index is for empty phandle
1289 * -EINVAL : parsing error on data
1290 */
1291
1292 err:
1293 of_node_put(it.node);
1294 return rc;
1295 }
1296 EXPORT_SYMBOL(__of_parse_phandle_with_args);
1297
1298 /**
1299 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1300 * @np: pointer to a device tree node containing a list
1301 * @list_name: property name that contains a list
1302 * @stem_name: stem of property names that specify phandles' arguments count
1303 * @index: index of a phandle to parse out
1304 * @out_args: optional pointer to output arguments structure (will be filled)
1305 *
1306 * This function is useful to parse lists of phandles and their arguments.
1307 * Returns 0 on success and fills out_args, on error returns appropriate errno
1308 * value. The difference between this function and of_parse_phandle_with_args()
1309 * is that this API remaps a phandle if the node the phandle points to has
1310 * a <@stem_name>-map property.
1311 *
1312 * Caller is responsible to call of_node_put() on the returned out_args->np
1313 * pointer.
1314 *
1315 * Example::
1316 *
1317 * phandle1: node1 {
1318 * #list-cells = <2>;
1319 * };
1320 *
1321 * phandle2: node2 {
1322 * #list-cells = <1>;
1323 * };
1324 *
1325 * phandle3: node3 {
1326 * #list-cells = <1>;
1327 * list-map = <0 &phandle2 3>,
1328 * <1 &phandle2 2>,
1329 * <2 &phandle1 5 1>;
1330 * list-map-mask = <0x3>;
1331 * };
1332 *
1333 * node4 {
1334 * list = <&phandle1 1 2 &phandle3 0>;
1335 * };
1336 *
1337 * To get a device_node of the ``node2`` node you may call this:
1338 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1339 */
of_parse_phandle_with_args_map(const struct device_node * np,const char * list_name,const char * stem_name,int index,struct of_phandle_args * out_args)1340 int of_parse_phandle_with_args_map(const struct device_node *np,
1341 const char *list_name,
1342 const char *stem_name,
1343 int index, struct of_phandle_args *out_args)
1344 {
1345 char *cells_name, *map_name = NULL, *mask_name = NULL;
1346 char *pass_name = NULL;
1347 struct device_node *cur, *new = NULL;
1348 const __be32 *map, *mask, *pass;
1349 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
1350 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
1351 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1352 const __be32 *match_array = initial_match_array;
1353 int i, ret, map_len, match;
1354 u32 list_size, new_size;
1355
1356 if (index < 0)
1357 return -EINVAL;
1358
1359 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1360 if (!cells_name)
1361 return -ENOMEM;
1362
1363 ret = -ENOMEM;
1364 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1365 if (!map_name)
1366 goto free;
1367
1368 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1369 if (!mask_name)
1370 goto free;
1371
1372 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1373 if (!pass_name)
1374 goto free;
1375
1376 ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index,
1377 out_args);
1378 if (ret)
1379 goto free;
1380
1381 /* Get the #<list>-cells property */
1382 cur = out_args->np;
1383 ret = of_property_read_u32(cur, cells_name, &list_size);
1384 if (ret < 0)
1385 goto put;
1386
1387 /* Precalculate the match array - this simplifies match loop */
1388 for (i = 0; i < list_size; i++)
1389 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1390
1391 ret = -EINVAL;
1392 while (cur) {
1393 /* Get the <list>-map property */
1394 map = of_get_property(cur, map_name, &map_len);
1395 if (!map) {
1396 ret = 0;
1397 goto free;
1398 }
1399 map_len /= sizeof(u32);
1400
1401 /* Get the <list>-map-mask property (optional) */
1402 mask = of_get_property(cur, mask_name, NULL);
1403 if (!mask)
1404 mask = dummy_mask;
1405 /* Iterate through <list>-map property */
1406 match = 0;
1407 while (map_len > (list_size + 1) && !match) {
1408 /* Compare specifiers */
1409 match = 1;
1410 for (i = 0; i < list_size; i++, map_len--)
1411 match &= !((match_array[i] ^ *map++) & mask[i]);
1412
1413 of_node_put(new);
1414 new = of_find_node_by_phandle(be32_to_cpup(map));
1415 map++;
1416 map_len--;
1417
1418 /* Check if not found */
1419 if (!new)
1420 goto put;
1421
1422 if (!of_device_is_available(new))
1423 match = 0;
1424
1425 ret = of_property_read_u32(new, cells_name, &new_size);
1426 if (ret)
1427 goto put;
1428
1429 /* Check for malformed properties */
1430 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1431 goto put;
1432 if (map_len < new_size)
1433 goto put;
1434
1435 /* Move forward by new node's #<list>-cells amount */
1436 map += new_size;
1437 map_len -= new_size;
1438 }
1439 if (!match)
1440 goto put;
1441
1442 /* Get the <list>-map-pass-thru property (optional) */
1443 pass = of_get_property(cur, pass_name, NULL);
1444 if (!pass)
1445 pass = dummy_pass;
1446
1447 /*
1448 * Successfully parsed a <list>-map translation; copy new
1449 * specifier into the out_args structure, keeping the
1450 * bits specified in <list>-map-pass-thru.
1451 */
1452 match_array = map - new_size;
1453 for (i = 0; i < new_size; i++) {
1454 __be32 val = *(map - new_size + i);
1455
1456 if (i < list_size) {
1457 val &= ~pass[i];
1458 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1459 }
1460
1461 out_args->args[i] = be32_to_cpu(val);
1462 }
1463 out_args->args_count = list_size = new_size;
1464 /* Iterate again with new provider */
1465 out_args->np = new;
1466 of_node_put(cur);
1467 cur = new;
1468 new = NULL;
1469 }
1470 put:
1471 of_node_put(cur);
1472 of_node_put(new);
1473 free:
1474 kfree(mask_name);
1475 kfree(map_name);
1476 kfree(cells_name);
1477 kfree(pass_name);
1478
1479 return ret;
1480 }
1481 EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1482
1483 /**
1484 * of_count_phandle_with_args() - Find the number of phandles references in a property
1485 * @np: pointer to a device tree node containing a list
1486 * @list_name: property name that contains a list
1487 * @cells_name: property name that specifies phandles' arguments count
1488 *
1489 * Return: The number of phandle + argument tuples within a property. It
1490 * is a typical pattern to encode a list of phandle and variable
1491 * arguments into a single property. The number of arguments is encoded
1492 * by a property in the phandle-target node. For example, a gpios
1493 * property would contain a list of GPIO specifies consisting of a
1494 * phandle and 1 or more arguments. The number of arguments are
1495 * determined by the #gpio-cells property in the node pointed to by the
1496 * phandle.
1497 */
of_count_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name)1498 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1499 const char *cells_name)
1500 {
1501 struct of_phandle_iterator it;
1502 int rc, cur_index = 0;
1503
1504 /*
1505 * If cells_name is NULL we assume a cell count of 0. This makes
1506 * counting the phandles trivial as each 32bit word in the list is a
1507 * phandle and no arguments are to consider. So we don't iterate through
1508 * the list but just use the length to determine the phandle count.
1509 */
1510 if (!cells_name) {
1511 const __be32 *list;
1512 int size;
1513
1514 list = of_get_property(np, list_name, &size);
1515 if (!list)
1516 return -ENOENT;
1517
1518 return size / sizeof(*list);
1519 }
1520
1521 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1);
1522 if (rc)
1523 return rc;
1524
1525 while ((rc = of_phandle_iterator_next(&it)) == 0)
1526 cur_index += 1;
1527
1528 if (rc != -ENOENT)
1529 return rc;
1530
1531 return cur_index;
1532 }
1533 EXPORT_SYMBOL(of_count_phandle_with_args);
1534
__of_remove_property_from_list(struct property ** list,struct property * prop)1535 static struct property *__of_remove_property_from_list(struct property **list, struct property *prop)
1536 {
1537 struct property **next;
1538
1539 for (next = list; *next; next = &(*next)->next) {
1540 if (*next == prop) {
1541 *next = prop->next;
1542 prop->next = NULL;
1543 return prop;
1544 }
1545 }
1546 return NULL;
1547 }
1548
1549 /**
1550 * __of_add_property - Add a property to a node without lock operations
1551 * @np: Caller's Device Node
1552 * @prop: Property to add
1553 */
__of_add_property(struct device_node * np,struct property * prop)1554 int __of_add_property(struct device_node *np, struct property *prop)
1555 {
1556 int rc = 0;
1557 unsigned long flags;
1558 struct property **next;
1559
1560 raw_spin_lock_irqsave(&devtree_lock, flags);
1561
1562 __of_remove_property_from_list(&np->deadprops, prop);
1563
1564 prop->next = NULL;
1565 next = &np->properties;
1566 while (*next) {
1567 if (strcmp(prop->name, (*next)->name) == 0) {
1568 /* duplicate ! don't insert it */
1569 rc = -EEXIST;
1570 goto out_unlock;
1571 }
1572 next = &(*next)->next;
1573 }
1574 *next = prop;
1575
1576 out_unlock:
1577 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1578 if (rc)
1579 return rc;
1580
1581 __of_add_property_sysfs(np, prop);
1582 return 0;
1583 }
1584
1585 /**
1586 * of_add_property - Add a property to a node
1587 * @np: Caller's Device Node
1588 * @prop: Property to add
1589 */
of_add_property(struct device_node * np,struct property * prop)1590 int of_add_property(struct device_node *np, struct property *prop)
1591 {
1592 int rc;
1593
1594 mutex_lock(&of_mutex);
1595 rc = __of_add_property(np, prop);
1596 mutex_unlock(&of_mutex);
1597
1598 if (!rc)
1599 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1600
1601 return rc;
1602 }
1603 EXPORT_SYMBOL_GPL(of_add_property);
1604
__of_remove_property(struct device_node * np,struct property * prop)1605 int __of_remove_property(struct device_node *np, struct property *prop)
1606 {
1607 unsigned long flags;
1608 int rc = -ENODEV;
1609
1610 raw_spin_lock_irqsave(&devtree_lock, flags);
1611
1612 if (__of_remove_property_from_list(&np->properties, prop)) {
1613 /* Found the property, add it to deadprops list */
1614 prop->next = np->deadprops;
1615 np->deadprops = prop;
1616 rc = 0;
1617 }
1618
1619 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1620 if (rc)
1621 return rc;
1622
1623 __of_remove_property_sysfs(np, prop);
1624 return 0;
1625 }
1626
1627 /**
1628 * of_remove_property - Remove a property from a node.
1629 * @np: Caller's Device Node
1630 * @prop: Property to remove
1631 *
1632 * Note that we don't actually remove it, since we have given out
1633 * who-knows-how-many pointers to the data using get-property.
1634 * Instead we just move the property to the "dead properties"
1635 * list, so it won't be found any more.
1636 */
of_remove_property(struct device_node * np,struct property * prop)1637 int of_remove_property(struct device_node *np, struct property *prop)
1638 {
1639 int rc;
1640
1641 if (!prop)
1642 return -ENODEV;
1643
1644 mutex_lock(&of_mutex);
1645 rc = __of_remove_property(np, prop);
1646 mutex_unlock(&of_mutex);
1647
1648 if (!rc)
1649 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1650
1651 return rc;
1652 }
1653 EXPORT_SYMBOL_GPL(of_remove_property);
1654
__of_update_property(struct device_node * np,struct property * newprop,struct property ** oldpropp)1655 int __of_update_property(struct device_node *np, struct property *newprop,
1656 struct property **oldpropp)
1657 {
1658 struct property **next, *oldprop;
1659 unsigned long flags;
1660
1661 raw_spin_lock_irqsave(&devtree_lock, flags);
1662
1663 __of_remove_property_from_list(&np->deadprops, newprop);
1664
1665 for (next = &np->properties; *next; next = &(*next)->next) {
1666 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1667 break;
1668 }
1669 *oldpropp = oldprop = *next;
1670
1671 if (oldprop) {
1672 /* replace the node */
1673 newprop->next = oldprop->next;
1674 *next = newprop;
1675 oldprop->next = np->deadprops;
1676 np->deadprops = oldprop;
1677 } else {
1678 /* new node */
1679 newprop->next = NULL;
1680 *next = newprop;
1681 }
1682
1683 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1684
1685 __of_update_property_sysfs(np, newprop, oldprop);
1686
1687 return 0;
1688 }
1689
1690 /*
1691 * of_update_property - Update a property in a node, if the property does
1692 * not exist, add it.
1693 *
1694 * Note that we don't actually remove it, since we have given out
1695 * who-knows-how-many pointers to the data using get-property.
1696 * Instead we just move the property to the "dead properties" list,
1697 * and add the new property to the property list
1698 */
of_update_property(struct device_node * np,struct property * newprop)1699 int of_update_property(struct device_node *np, struct property *newprop)
1700 {
1701 struct property *oldprop;
1702 int rc;
1703
1704 if (!newprop->name)
1705 return -EINVAL;
1706
1707 mutex_lock(&of_mutex);
1708 rc = __of_update_property(np, newprop, &oldprop);
1709 mutex_unlock(&of_mutex);
1710
1711 if (!rc)
1712 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1713
1714 return rc;
1715 }
1716
of_alias_add(struct alias_prop * ap,struct device_node * np,int id,const char * stem,int stem_len)1717 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1718 int id, const char *stem, int stem_len)
1719 {
1720 ap->np = np;
1721 ap->id = id;
1722 strscpy(ap->stem, stem, stem_len + 1);
1723 list_add_tail(&ap->link, &aliases_lookup);
1724 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1725 ap->alias, ap->stem, ap->id, np);
1726 }
1727
1728 /**
1729 * of_alias_scan - Scan all properties of the 'aliases' node
1730 * @dt_alloc: An allocator that provides a virtual address to memory
1731 * for storing the resulting tree
1732 *
1733 * The function scans all the properties of the 'aliases' node and populates
1734 * the global lookup table with the properties. It returns the
1735 * number of alias properties found, or an error code in case of failure.
1736 */
of_alias_scan(void * (* dt_alloc)(u64 size,u64 align))1737 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1738 {
1739 struct property *pp;
1740
1741 of_aliases = of_find_node_by_path("/aliases");
1742 of_chosen = of_find_node_by_path("/chosen");
1743 if (of_chosen == NULL)
1744 of_chosen = of_find_node_by_path("/chosen@0");
1745
1746 if (of_chosen) {
1747 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
1748 const char *name = NULL;
1749
1750 if (of_property_read_string(of_chosen, "stdout-path", &name))
1751 of_property_read_string(of_chosen, "linux,stdout-path",
1752 &name);
1753 if (IS_ENABLED(CONFIG_PPC) && !name)
1754 of_property_read_string(of_aliases, "stdout", &name);
1755 if (name)
1756 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
1757 if (of_stdout)
1758 of_stdout->fwnode.flags |= FWNODE_FLAG_BEST_EFFORT;
1759 }
1760
1761 if (!of_aliases)
1762 return;
1763
1764 for_each_property_of_node(of_aliases, pp) {
1765 const char *start = pp->name;
1766 const char *end = start + strlen(start);
1767 struct device_node *np;
1768 struct alias_prop *ap;
1769 int id, len;
1770
1771 /* Skip those we do not want to proceed */
1772 if (!strcmp(pp->name, "name") ||
1773 !strcmp(pp->name, "phandle") ||
1774 !strcmp(pp->name, "linux,phandle"))
1775 continue;
1776
1777 np = of_find_node_by_path(pp->value);
1778 if (!np)
1779 continue;
1780
1781 /* walk the alias backwards to extract the id and work out
1782 * the 'stem' string */
1783 while (isdigit(*(end-1)) && end > start)
1784 end--;
1785 len = end - start;
1786
1787 if (kstrtoint(end, 10, &id) < 0)
1788 continue;
1789
1790 /* Allocate an alias_prop with enough space for the stem */
1791 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
1792 if (!ap)
1793 continue;
1794 memset(ap, 0, sizeof(*ap) + len + 1);
1795 ap->alias = start;
1796 of_alias_add(ap, np, id, start, len);
1797 }
1798 }
1799
1800 /**
1801 * of_alias_get_id - Get alias id for the given device_node
1802 * @np: Pointer to the given device_node
1803 * @stem: Alias stem of the given device_node
1804 *
1805 * The function travels the lookup table to get the alias id for the given
1806 * device_node and alias stem.
1807 *
1808 * Return: The alias id if found.
1809 */
of_alias_get_id(struct device_node * np,const char * stem)1810 int of_alias_get_id(struct device_node *np, const char *stem)
1811 {
1812 struct alias_prop *app;
1813 int id = -ENODEV;
1814
1815 mutex_lock(&of_mutex);
1816 list_for_each_entry(app, &aliases_lookup, link) {
1817 if (strcmp(app->stem, stem) != 0)
1818 continue;
1819
1820 if (np == app->np) {
1821 id = app->id;
1822 break;
1823 }
1824 }
1825 mutex_unlock(&of_mutex);
1826
1827 return id;
1828 }
1829 EXPORT_SYMBOL_GPL(of_alias_get_id);
1830
1831 /**
1832 * of_alias_get_highest_id - Get highest alias id for the given stem
1833 * @stem: Alias stem to be examined
1834 *
1835 * The function travels the lookup table to get the highest alias id for the
1836 * given alias stem. It returns the alias id if found.
1837 */
of_alias_get_highest_id(const char * stem)1838 int of_alias_get_highest_id(const char *stem)
1839 {
1840 struct alias_prop *app;
1841 int id = -ENODEV;
1842
1843 mutex_lock(&of_mutex);
1844 list_for_each_entry(app, &aliases_lookup, link) {
1845 if (strcmp(app->stem, stem) != 0)
1846 continue;
1847
1848 if (app->id > id)
1849 id = app->id;
1850 }
1851 mutex_unlock(&of_mutex);
1852
1853 return id;
1854 }
1855 EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
1856
1857 /**
1858 * of_console_check() - Test and setup console for DT setup
1859 * @dn: Pointer to device node
1860 * @name: Name to use for preferred console without index. ex. "ttyS"
1861 * @index: Index to use for preferred console.
1862 *
1863 * Check if the given device node matches the stdout-path property in the
1864 * /chosen node. If it does then register it as the preferred console.
1865 *
1866 * Return: TRUE if console successfully setup. Otherwise return FALSE.
1867 */
of_console_check(struct device_node * dn,char * name,int index)1868 bool of_console_check(struct device_node *dn, char *name, int index)
1869 {
1870 if (!dn || dn != of_stdout || console_set_on_cmdline)
1871 return false;
1872
1873 /*
1874 * XXX: cast `options' to char pointer to suppress complication
1875 * warnings: printk, UART and console drivers expect char pointer.
1876 */
1877 return !add_preferred_console(name, index, (char *)of_stdout_options);
1878 }
1879 EXPORT_SYMBOL_GPL(of_console_check);
1880
1881 /**
1882 * of_find_next_cache_node - Find a node's subsidiary cache
1883 * @np: node of type "cpu" or "cache"
1884 *
1885 * Return: A node pointer with refcount incremented, use
1886 * of_node_put() on it when done. Caller should hold a reference
1887 * to np.
1888 */
of_find_next_cache_node(const struct device_node * np)1889 struct device_node *of_find_next_cache_node(const struct device_node *np)
1890 {
1891 struct device_node *child, *cache_node;
1892
1893 cache_node = of_parse_phandle(np, "l2-cache", 0);
1894 if (!cache_node)
1895 cache_node = of_parse_phandle(np, "next-level-cache", 0);
1896
1897 if (cache_node)
1898 return cache_node;
1899
1900 /* OF on pmac has nodes instead of properties named "l2-cache"
1901 * beneath CPU nodes.
1902 */
1903 if (IS_ENABLED(CONFIG_PPC_PMAC) && of_node_is_type(np, "cpu"))
1904 for_each_child_of_node(np, child)
1905 if (of_node_is_type(child, "cache"))
1906 return child;
1907
1908 return NULL;
1909 }
1910
1911 /**
1912 * of_find_last_cache_level - Find the level at which the last cache is
1913 * present for the given logical cpu
1914 *
1915 * @cpu: cpu number(logical index) for which the last cache level is needed
1916 *
1917 * Return: The level at which the last cache is present. It is exactly
1918 * same as the total number of cache levels for the given logical cpu.
1919 */
of_find_last_cache_level(unsigned int cpu)1920 int of_find_last_cache_level(unsigned int cpu)
1921 {
1922 u32 cache_level = 0;
1923 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
1924
1925 while (np) {
1926 of_node_put(prev);
1927 prev = np;
1928 np = of_find_next_cache_node(np);
1929 }
1930
1931 of_property_read_u32(prev, "cache-level", &cache_level);
1932 of_node_put(prev);
1933
1934 return cache_level;
1935 }
1936
1937 /**
1938 * of_map_id - Translate an ID through a downstream mapping.
1939 * @np: root complex device node.
1940 * @id: device ID to map.
1941 * @map_name: property name of the map to use.
1942 * @map_mask_name: optional property name of the mask to use.
1943 * @target: optional pointer to a target device node.
1944 * @id_out: optional pointer to receive the translated ID.
1945 *
1946 * Given a device ID, look up the appropriate implementation-defined
1947 * platform ID and/or the target device which receives transactions on that
1948 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
1949 * @id_out may be NULL if only the other is required. If @target points to
1950 * a non-NULL device node pointer, only entries targeting that node will be
1951 * matched; if it points to a NULL value, it will receive the device node of
1952 * the first matching target phandle, with a reference held.
1953 *
1954 * Return: 0 on success or a standard error code on failure.
1955 */
of_map_id(struct device_node * np,u32 id,const char * map_name,const char * map_mask_name,struct device_node ** target,u32 * id_out)1956 int of_map_id(struct device_node *np, u32 id,
1957 const char *map_name, const char *map_mask_name,
1958 struct device_node **target, u32 *id_out)
1959 {
1960 u32 map_mask, masked_id;
1961 int map_len;
1962 const __be32 *map = NULL;
1963
1964 if (!np || !map_name || (!target && !id_out))
1965 return -EINVAL;
1966
1967 map = of_get_property(np, map_name, &map_len);
1968 if (!map) {
1969 if (target)
1970 return -ENODEV;
1971 /* Otherwise, no map implies no translation */
1972 *id_out = id;
1973 return 0;
1974 }
1975
1976 if (!map_len || map_len % (4 * sizeof(*map))) {
1977 pr_err("%pOF: Error: Bad %s length: %d\n", np,
1978 map_name, map_len);
1979 return -EINVAL;
1980 }
1981
1982 /* The default is to select all bits. */
1983 map_mask = 0xffffffff;
1984
1985 /*
1986 * Can be overridden by "{iommu,msi}-map-mask" property.
1987 * If of_property_read_u32() fails, the default is used.
1988 */
1989 if (map_mask_name)
1990 of_property_read_u32(np, map_mask_name, &map_mask);
1991
1992 masked_id = map_mask & id;
1993 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
1994 struct device_node *phandle_node;
1995 u32 id_base = be32_to_cpup(map + 0);
1996 u32 phandle = be32_to_cpup(map + 1);
1997 u32 out_base = be32_to_cpup(map + 2);
1998 u32 id_len = be32_to_cpup(map + 3);
1999
2000 if (id_base & ~map_mask) {
2001 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores id-base (0x%x)\n",
2002 np, map_name, map_name,
2003 map_mask, id_base);
2004 return -EFAULT;
2005 }
2006
2007 if (masked_id < id_base || masked_id >= id_base + id_len)
2008 continue;
2009
2010 phandle_node = of_find_node_by_phandle(phandle);
2011 if (!phandle_node)
2012 return -ENODEV;
2013
2014 if (target) {
2015 if (*target)
2016 of_node_put(phandle_node);
2017 else
2018 *target = phandle_node;
2019
2020 if (*target != phandle_node)
2021 continue;
2022 }
2023
2024 if (id_out)
2025 *id_out = masked_id - id_base + out_base;
2026
2027 pr_debug("%pOF: %s, using mask %08x, id-base: %08x, out-base: %08x, length: %08x, id: %08x -> %08x\n",
2028 np, map_name, map_mask, id_base, out_base,
2029 id_len, id, masked_id - id_base + out_base);
2030 return 0;
2031 }
2032
2033 pr_info("%pOF: no %s translation for id 0x%x on %pOF\n", np, map_name,
2034 id, target && *target ? *target : NULL);
2035
2036 /* Bypasses translation */
2037 if (id_out)
2038 *id_out = id;
2039 return 0;
2040 }
2041 EXPORT_SYMBOL_GPL(of_map_id);
2042