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