xref: /linux/drivers/of/base.c (revision c1aac62f36c1e37ee81c9e09ee9ee733eef05dcb)
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras	August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11  *
12  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13  *  Grant Likely.
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License
17  *      as published by the Free Software Foundation; either version
18  *      2 of the License, or (at your option) any later version.
19  */
20 
21 #define pr_fmt(fmt)	"OF: " fmt
22 
23 #include <linux/console.h>
24 #include <linux/ctype.h>
25 #include <linux/cpu.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29 #include <linux/of_graph.h>
30 #include <linux/spinlock.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/proc_fs.h>
34 
35 #include "of_private.h"
36 
37 LIST_HEAD(aliases_lookup);
38 
39 struct device_node *of_root;
40 EXPORT_SYMBOL(of_root);
41 struct device_node *of_chosen;
42 struct device_node *of_aliases;
43 struct device_node *of_stdout;
44 static const char *of_stdout_options;
45 
46 struct kset *of_kset;
47 
48 /*
49  * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
50  * This mutex must be held whenever modifications are being made to the
51  * device tree. The of_{attach,detach}_node() and
52  * of_{add,remove,update}_property() helpers make sure this happens.
53  */
54 DEFINE_MUTEX(of_mutex);
55 
56 /* use when traversing tree through the child, sibling,
57  * or parent members of struct device_node.
58  */
59 DEFINE_RAW_SPINLOCK(devtree_lock);
60 
61 int of_n_addr_cells(struct device_node *np)
62 {
63 	const __be32 *ip;
64 
65 	do {
66 		if (np->parent)
67 			np = np->parent;
68 		ip = of_get_property(np, "#address-cells", NULL);
69 		if (ip)
70 			return be32_to_cpup(ip);
71 	} while (np->parent);
72 	/* No #address-cells property for the root node */
73 	return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
74 }
75 EXPORT_SYMBOL(of_n_addr_cells);
76 
77 int of_n_size_cells(struct device_node *np)
78 {
79 	const __be32 *ip;
80 
81 	do {
82 		if (np->parent)
83 			np = np->parent;
84 		ip = of_get_property(np, "#size-cells", NULL);
85 		if (ip)
86 			return be32_to_cpup(ip);
87 	} while (np->parent);
88 	/* No #size-cells property for the root node */
89 	return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
90 }
91 EXPORT_SYMBOL(of_n_size_cells);
92 
93 #ifdef CONFIG_NUMA
94 int __weak of_node_to_nid(struct device_node *np)
95 {
96 	return NUMA_NO_NODE;
97 }
98 #endif
99 
100 #ifndef CONFIG_OF_DYNAMIC
101 static void of_node_release(struct kobject *kobj)
102 {
103 	/* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
104 }
105 #endif /* CONFIG_OF_DYNAMIC */
106 
107 struct kobj_type of_node_ktype = {
108 	.release = of_node_release,
109 };
110 
111 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
112 				struct bin_attribute *bin_attr, char *buf,
113 				loff_t offset, size_t count)
114 {
115 	struct property *pp = container_of(bin_attr, struct property, attr);
116 	return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
117 }
118 
119 /* always return newly allocated name, caller must free after use */
120 static const char *safe_name(struct kobject *kobj, const char *orig_name)
121 {
122 	const char *name = orig_name;
123 	struct kernfs_node *kn;
124 	int i = 0;
125 
126 	/* don't be a hero. After 16 tries give up */
127 	while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
128 		sysfs_put(kn);
129 		if (name != orig_name)
130 			kfree(name);
131 		name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
132 	}
133 
134 	if (name == orig_name) {
135 		name = kstrdup(orig_name, GFP_KERNEL);
136 	} else {
137 		pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
138 			kobject_name(kobj), name);
139 	}
140 	return name;
141 }
142 
143 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
144 {
145 	int rc;
146 
147 	/* Important: Don't leak passwords */
148 	bool secure = strncmp(pp->name, "security-", 9) == 0;
149 
150 	if (!IS_ENABLED(CONFIG_SYSFS))
151 		return 0;
152 
153 	if (!of_kset || !of_node_is_attached(np))
154 		return 0;
155 
156 	sysfs_bin_attr_init(&pp->attr);
157 	pp->attr.attr.name = safe_name(&np->kobj, pp->name);
158 	pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
159 	pp->attr.size = secure ? 0 : pp->length;
160 	pp->attr.read = of_node_property_read;
161 
162 	rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
163 	WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
164 	return rc;
165 }
166 
167 int __of_attach_node_sysfs(struct device_node *np)
168 {
169 	const char *name;
170 	struct kobject *parent;
171 	struct property *pp;
172 	int rc;
173 
174 	if (!IS_ENABLED(CONFIG_SYSFS))
175 		return 0;
176 
177 	if (!of_kset)
178 		return 0;
179 
180 	np->kobj.kset = of_kset;
181 	if (!np->parent) {
182 		/* Nodes without parents are new top level trees */
183 		name = safe_name(&of_kset->kobj, "base");
184 		parent = NULL;
185 	} else {
186 		name = safe_name(&np->parent->kobj, kbasename(np->full_name));
187 		parent = &np->parent->kobj;
188 	}
189 	if (!name)
190 		return -ENOMEM;
191 	rc = kobject_add(&np->kobj, parent, "%s", name);
192 	kfree(name);
193 	if (rc)
194 		return rc;
195 
196 	for_each_property_of_node(np, pp)
197 		__of_add_property_sysfs(np, pp);
198 
199 	return 0;
200 }
201 
202 void __init of_core_init(void)
203 {
204 	struct device_node *np;
205 
206 	/* Create the kset, and register existing nodes */
207 	mutex_lock(&of_mutex);
208 	of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
209 	if (!of_kset) {
210 		mutex_unlock(&of_mutex);
211 		pr_err("failed to register existing nodes\n");
212 		return;
213 	}
214 	for_each_of_allnodes(np)
215 		__of_attach_node_sysfs(np);
216 	mutex_unlock(&of_mutex);
217 
218 	/* Symlink in /proc as required by userspace ABI */
219 	if (of_root)
220 		proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
221 }
222 
223 static struct property *__of_find_property(const struct device_node *np,
224 					   const char *name, int *lenp)
225 {
226 	struct property *pp;
227 
228 	if (!np)
229 		return NULL;
230 
231 	for (pp = np->properties; pp; pp = pp->next) {
232 		if (of_prop_cmp(pp->name, name) == 0) {
233 			if (lenp)
234 				*lenp = pp->length;
235 			break;
236 		}
237 	}
238 
239 	return pp;
240 }
241 
242 struct property *of_find_property(const struct device_node *np,
243 				  const char *name,
244 				  int *lenp)
245 {
246 	struct property *pp;
247 	unsigned long flags;
248 
249 	raw_spin_lock_irqsave(&devtree_lock, flags);
250 	pp = __of_find_property(np, name, lenp);
251 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
252 
253 	return pp;
254 }
255 EXPORT_SYMBOL(of_find_property);
256 
257 struct device_node *__of_find_all_nodes(struct device_node *prev)
258 {
259 	struct device_node *np;
260 	if (!prev) {
261 		np = of_root;
262 	} else if (prev->child) {
263 		np = prev->child;
264 	} else {
265 		/* Walk back up looking for a sibling, or the end of the structure */
266 		np = prev;
267 		while (np->parent && !np->sibling)
268 			np = np->parent;
269 		np = np->sibling; /* Might be null at the end of the tree */
270 	}
271 	return np;
272 }
273 
274 /**
275  * of_find_all_nodes - Get next node in global list
276  * @prev:	Previous node or NULL to start iteration
277  *		of_node_put() will be called on it
278  *
279  * Returns a node pointer with refcount incremented, use
280  * of_node_put() on it when done.
281  */
282 struct device_node *of_find_all_nodes(struct device_node *prev)
283 {
284 	struct device_node *np;
285 	unsigned long flags;
286 
287 	raw_spin_lock_irqsave(&devtree_lock, flags);
288 	np = __of_find_all_nodes(prev);
289 	of_node_get(np);
290 	of_node_put(prev);
291 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
292 	return np;
293 }
294 EXPORT_SYMBOL(of_find_all_nodes);
295 
296 /*
297  * Find a property with a given name for a given node
298  * and return the value.
299  */
300 const void *__of_get_property(const struct device_node *np,
301 			      const char *name, int *lenp)
302 {
303 	struct property *pp = __of_find_property(np, name, lenp);
304 
305 	return pp ? pp->value : NULL;
306 }
307 
308 /*
309  * Find a property with a given name for a given node
310  * and return the value.
311  */
312 const void *of_get_property(const struct device_node *np, const char *name,
313 			    int *lenp)
314 {
315 	struct property *pp = of_find_property(np, name, lenp);
316 
317 	return pp ? pp->value : NULL;
318 }
319 EXPORT_SYMBOL(of_get_property);
320 
321 /*
322  * arch_match_cpu_phys_id - Match the given logical CPU and physical id
323  *
324  * @cpu: logical cpu index of a core/thread
325  * @phys_id: physical identifier of a core/thread
326  *
327  * CPU logical to physical index mapping is architecture specific.
328  * However this __weak function provides a default match of physical
329  * id to logical cpu index. phys_id provided here is usually values read
330  * from the device tree which must match the hardware internal registers.
331  *
332  * Returns true if the physical identifier and the logical cpu index
333  * correspond to the same core/thread, false otherwise.
334  */
335 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
336 {
337 	return (u32)phys_id == cpu;
338 }
339 
340 /**
341  * Checks if the given "prop_name" property holds the physical id of the
342  * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
343  * NULL, local thread number within the core is returned in it.
344  */
345 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
346 			const char *prop_name, int cpu, unsigned int *thread)
347 {
348 	const __be32 *cell;
349 	int ac, prop_len, tid;
350 	u64 hwid;
351 
352 	ac = of_n_addr_cells(cpun);
353 	cell = of_get_property(cpun, prop_name, &prop_len);
354 	if (!cell || !ac)
355 		return false;
356 	prop_len /= sizeof(*cell) * ac;
357 	for (tid = 0; tid < prop_len; tid++) {
358 		hwid = of_read_number(cell, ac);
359 		if (arch_match_cpu_phys_id(cpu, hwid)) {
360 			if (thread)
361 				*thread = tid;
362 			return true;
363 		}
364 		cell += ac;
365 	}
366 	return false;
367 }
368 
369 /*
370  * arch_find_n_match_cpu_physical_id - See if the given device node is
371  * for the cpu corresponding to logical cpu 'cpu'.  Return true if so,
372  * else false.  If 'thread' is non-NULL, the local thread number within the
373  * core is returned in it.
374  */
375 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
376 					      int cpu, unsigned int *thread)
377 {
378 	/* Check for non-standard "ibm,ppc-interrupt-server#s" property
379 	 * for thread ids on PowerPC. If it doesn't exist fallback to
380 	 * standard "reg" property.
381 	 */
382 	if (IS_ENABLED(CONFIG_PPC) &&
383 	    __of_find_n_match_cpu_property(cpun,
384 					   "ibm,ppc-interrupt-server#s",
385 					   cpu, thread))
386 		return true;
387 
388 	return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
389 }
390 
391 /**
392  * of_get_cpu_node - Get device node associated with the given logical CPU
393  *
394  * @cpu: CPU number(logical index) for which device node is required
395  * @thread: if not NULL, local thread number within the physical core is
396  *          returned
397  *
398  * The main purpose of this function is to retrieve the device node for the
399  * given logical CPU index. It should be used to initialize the of_node in
400  * cpu device. Once of_node in cpu device is populated, all the further
401  * references can use that instead.
402  *
403  * CPU logical to physical index mapping is architecture specific and is built
404  * before booting secondary cores. This function uses arch_match_cpu_phys_id
405  * which can be overridden by architecture specific implementation.
406  *
407  * Returns a node pointer for the logical cpu with refcount incremented, use
408  * of_node_put() on it when done. Returns NULL if not found.
409  */
410 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
411 {
412 	struct device_node *cpun;
413 
414 	for_each_node_by_type(cpun, "cpu") {
415 		if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
416 			return cpun;
417 	}
418 	return NULL;
419 }
420 EXPORT_SYMBOL(of_get_cpu_node);
421 
422 /**
423  * __of_device_is_compatible() - Check if the node matches given constraints
424  * @device: pointer to node
425  * @compat: required compatible string, NULL or "" for any match
426  * @type: required device_type value, NULL or "" for any match
427  * @name: required node name, NULL or "" for any match
428  *
429  * Checks if the given @compat, @type and @name strings match the
430  * properties of the given @device. A constraints can be skipped by
431  * passing NULL or an empty string as the constraint.
432  *
433  * Returns 0 for no match, and a positive integer on match. The return
434  * value is a relative score with larger values indicating better
435  * matches. The score is weighted for the most specific compatible value
436  * to get the highest score. Matching type is next, followed by matching
437  * name. Practically speaking, this results in the following priority
438  * order for matches:
439  *
440  * 1. specific compatible && type && name
441  * 2. specific compatible && type
442  * 3. specific compatible && name
443  * 4. specific compatible
444  * 5. general compatible && type && name
445  * 6. general compatible && type
446  * 7. general compatible && name
447  * 8. general compatible
448  * 9. type && name
449  * 10. type
450  * 11. name
451  */
452 static int __of_device_is_compatible(const struct device_node *device,
453 				     const char *compat, const char *type, const char *name)
454 {
455 	struct property *prop;
456 	const char *cp;
457 	int index = 0, score = 0;
458 
459 	/* Compatible match has highest priority */
460 	if (compat && compat[0]) {
461 		prop = __of_find_property(device, "compatible", NULL);
462 		for (cp = of_prop_next_string(prop, NULL); cp;
463 		     cp = of_prop_next_string(prop, cp), index++) {
464 			if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
465 				score = INT_MAX/2 - (index << 2);
466 				break;
467 			}
468 		}
469 		if (!score)
470 			return 0;
471 	}
472 
473 	/* Matching type is better than matching name */
474 	if (type && type[0]) {
475 		if (!device->type || of_node_cmp(type, device->type))
476 			return 0;
477 		score += 2;
478 	}
479 
480 	/* Matching name is a bit better than not */
481 	if (name && name[0]) {
482 		if (!device->name || of_node_cmp(name, device->name))
483 			return 0;
484 		score++;
485 	}
486 
487 	return score;
488 }
489 
490 /** Checks if the given "compat" string matches one of the strings in
491  * the device's "compatible" property
492  */
493 int of_device_is_compatible(const struct device_node *device,
494 		const char *compat)
495 {
496 	unsigned long flags;
497 	int res;
498 
499 	raw_spin_lock_irqsave(&devtree_lock, flags);
500 	res = __of_device_is_compatible(device, compat, NULL, NULL);
501 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
502 	return res;
503 }
504 EXPORT_SYMBOL(of_device_is_compatible);
505 
506 /** Checks if the device is compatible with any of the entries in
507  *  a NULL terminated array of strings. Returns the best match
508  *  score or 0.
509  */
510 int of_device_compatible_match(struct device_node *device,
511 			       const char *const *compat)
512 {
513 	unsigned int tmp, score = 0;
514 
515 	if (!compat)
516 		return 0;
517 
518 	while (*compat) {
519 		tmp = of_device_is_compatible(device, *compat);
520 		if (tmp > score)
521 			score = tmp;
522 		compat++;
523 	}
524 
525 	return score;
526 }
527 
528 /**
529  * of_machine_is_compatible - Test root of device tree for a given compatible value
530  * @compat: compatible string to look for in root node's compatible property.
531  *
532  * Returns a positive integer if the root node has the given value in its
533  * compatible property.
534  */
535 int of_machine_is_compatible(const char *compat)
536 {
537 	struct device_node *root;
538 	int rc = 0;
539 
540 	root = of_find_node_by_path("/");
541 	if (root) {
542 		rc = of_device_is_compatible(root, compat);
543 		of_node_put(root);
544 	}
545 	return rc;
546 }
547 EXPORT_SYMBOL(of_machine_is_compatible);
548 
549 /**
550  *  __of_device_is_available - check if a device is available for use
551  *
552  *  @device: Node to check for availability, with locks already held
553  *
554  *  Returns true if the status property is absent or set to "okay" or "ok",
555  *  false otherwise
556  */
557 static bool __of_device_is_available(const struct device_node *device)
558 {
559 	const char *status;
560 	int statlen;
561 
562 	if (!device)
563 		return false;
564 
565 	status = __of_get_property(device, "status", &statlen);
566 	if (status == NULL)
567 		return true;
568 
569 	if (statlen > 0) {
570 		if (!strcmp(status, "okay") || !strcmp(status, "ok"))
571 			return true;
572 	}
573 
574 	return false;
575 }
576 
577 /**
578  *  of_device_is_available - check if a device is available for use
579  *
580  *  @device: Node to check for availability
581  *
582  *  Returns true if the status property is absent or set to "okay" or "ok",
583  *  false otherwise
584  */
585 bool of_device_is_available(const struct device_node *device)
586 {
587 	unsigned long flags;
588 	bool res;
589 
590 	raw_spin_lock_irqsave(&devtree_lock, flags);
591 	res = __of_device_is_available(device);
592 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
593 	return res;
594 
595 }
596 EXPORT_SYMBOL(of_device_is_available);
597 
598 /**
599  *  of_device_is_big_endian - check if a device has BE registers
600  *
601  *  @device: Node to check for endianness
602  *
603  *  Returns true if the device has a "big-endian" property, or if the kernel
604  *  was compiled for BE *and* the device has a "native-endian" property.
605  *  Returns false otherwise.
606  *
607  *  Callers would nominally use ioread32be/iowrite32be if
608  *  of_device_is_big_endian() == true, or readl/writel otherwise.
609  */
610 bool of_device_is_big_endian(const struct device_node *device)
611 {
612 	if (of_property_read_bool(device, "big-endian"))
613 		return true;
614 	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
615 	    of_property_read_bool(device, "native-endian"))
616 		return true;
617 	return false;
618 }
619 EXPORT_SYMBOL(of_device_is_big_endian);
620 
621 /**
622  *	of_get_parent - Get a node's parent if any
623  *	@node:	Node to get parent
624  *
625  *	Returns a node pointer with refcount incremented, use
626  *	of_node_put() on it when done.
627  */
628 struct device_node *of_get_parent(const struct device_node *node)
629 {
630 	struct device_node *np;
631 	unsigned long flags;
632 
633 	if (!node)
634 		return NULL;
635 
636 	raw_spin_lock_irqsave(&devtree_lock, flags);
637 	np = of_node_get(node->parent);
638 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
639 	return np;
640 }
641 EXPORT_SYMBOL(of_get_parent);
642 
643 /**
644  *	of_get_next_parent - Iterate to a node's parent
645  *	@node:	Node to get parent of
646  *
647  *	This is like of_get_parent() except that it drops the
648  *	refcount on the passed node, making it suitable for iterating
649  *	through a node's parents.
650  *
651  *	Returns a node pointer with refcount incremented, use
652  *	of_node_put() on it when done.
653  */
654 struct device_node *of_get_next_parent(struct device_node *node)
655 {
656 	struct device_node *parent;
657 	unsigned long flags;
658 
659 	if (!node)
660 		return NULL;
661 
662 	raw_spin_lock_irqsave(&devtree_lock, flags);
663 	parent = of_node_get(node->parent);
664 	of_node_put(node);
665 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
666 	return parent;
667 }
668 EXPORT_SYMBOL(of_get_next_parent);
669 
670 static struct device_node *__of_get_next_child(const struct device_node *node,
671 						struct device_node *prev)
672 {
673 	struct device_node *next;
674 
675 	if (!node)
676 		return NULL;
677 
678 	next = prev ? prev->sibling : node->child;
679 	for (; next; next = next->sibling)
680 		if (of_node_get(next))
681 			break;
682 	of_node_put(prev);
683 	return next;
684 }
685 #define __for_each_child_of_node(parent, child) \
686 	for (child = __of_get_next_child(parent, NULL); child != NULL; \
687 	     child = __of_get_next_child(parent, child))
688 
689 /**
690  *	of_get_next_child - Iterate a node childs
691  *	@node:	parent node
692  *	@prev:	previous child of the parent node, or NULL to get first
693  *
694  *	Returns a node pointer with refcount incremented, use of_node_put() on
695  *	it when done. Returns NULL when prev is the last child. Decrements the
696  *	refcount of prev.
697  */
698 struct device_node *of_get_next_child(const struct device_node *node,
699 	struct device_node *prev)
700 {
701 	struct device_node *next;
702 	unsigned long flags;
703 
704 	raw_spin_lock_irqsave(&devtree_lock, flags);
705 	next = __of_get_next_child(node, prev);
706 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
707 	return next;
708 }
709 EXPORT_SYMBOL(of_get_next_child);
710 
711 /**
712  *	of_get_next_available_child - Find the next available child node
713  *	@node:	parent node
714  *	@prev:	previous child of the parent node, or NULL to get first
715  *
716  *      This function is like of_get_next_child(), except that it
717  *      automatically skips any disabled nodes (i.e. status = "disabled").
718  */
719 struct device_node *of_get_next_available_child(const struct device_node *node,
720 	struct device_node *prev)
721 {
722 	struct device_node *next;
723 	unsigned long flags;
724 
725 	if (!node)
726 		return NULL;
727 
728 	raw_spin_lock_irqsave(&devtree_lock, flags);
729 	next = prev ? prev->sibling : node->child;
730 	for (; next; next = next->sibling) {
731 		if (!__of_device_is_available(next))
732 			continue;
733 		if (of_node_get(next))
734 			break;
735 	}
736 	of_node_put(prev);
737 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
738 	return next;
739 }
740 EXPORT_SYMBOL(of_get_next_available_child);
741 
742 /**
743  *	of_get_child_by_name - Find the child node by name for a given parent
744  *	@node:	parent node
745  *	@name:	child name to look for.
746  *
747  *      This function looks for child node for given matching name
748  *
749  *	Returns a node pointer if found, with refcount incremented, use
750  *	of_node_put() on it when done.
751  *	Returns NULL if node is not found.
752  */
753 struct device_node *of_get_child_by_name(const struct device_node *node,
754 				const char *name)
755 {
756 	struct device_node *child;
757 
758 	for_each_child_of_node(node, child)
759 		if (child->name && (of_node_cmp(child->name, name) == 0))
760 			break;
761 	return child;
762 }
763 EXPORT_SYMBOL(of_get_child_by_name);
764 
765 static struct device_node *__of_find_node_by_path(struct device_node *parent,
766 						const char *path)
767 {
768 	struct device_node *child;
769 	int len;
770 
771 	len = strcspn(path, "/:");
772 	if (!len)
773 		return NULL;
774 
775 	__for_each_child_of_node(parent, child) {
776 		const char *name = strrchr(child->full_name, '/');
777 		if (WARN(!name, "malformed device_node %s\n", child->full_name))
778 			continue;
779 		name++;
780 		if (strncmp(path, name, len) == 0 && (strlen(name) == len))
781 			return child;
782 	}
783 	return NULL;
784 }
785 
786 /**
787  *	of_find_node_opts_by_path - Find a node matching a full OF path
788  *	@path: Either the full path to match, or if the path does not
789  *	       start with '/', the name of a property of the /aliases
790  *	       node (an alias).  In the case of an alias, the node
791  *	       matching the alias' value will be returned.
792  *	@opts: Address of a pointer into which to store the start of
793  *	       an options string appended to the end of the path with
794  *	       a ':' separator.
795  *
796  *	Valid paths:
797  *		/foo/bar	Full path
798  *		foo		Valid alias
799  *		foo/bar		Valid alias + relative path
800  *
801  *	Returns a node pointer with refcount incremented, use
802  *	of_node_put() on it when done.
803  */
804 struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
805 {
806 	struct device_node *np = NULL;
807 	struct property *pp;
808 	unsigned long flags;
809 	const char *separator = strchr(path, ':');
810 
811 	if (opts)
812 		*opts = separator ? separator + 1 : NULL;
813 
814 	if (strcmp(path, "/") == 0)
815 		return of_node_get(of_root);
816 
817 	/* The path could begin with an alias */
818 	if (*path != '/') {
819 		int len;
820 		const char *p = separator;
821 
822 		if (!p)
823 			p = strchrnul(path, '/');
824 		len = p - path;
825 
826 		/* of_aliases must not be NULL */
827 		if (!of_aliases)
828 			return NULL;
829 
830 		for_each_property_of_node(of_aliases, pp) {
831 			if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
832 				np = of_find_node_by_path(pp->value);
833 				break;
834 			}
835 		}
836 		if (!np)
837 			return NULL;
838 		path = p;
839 	}
840 
841 	/* Step down the tree matching path components */
842 	raw_spin_lock_irqsave(&devtree_lock, flags);
843 	if (!np)
844 		np = of_node_get(of_root);
845 	while (np && *path == '/') {
846 		path++; /* Increment past '/' delimiter */
847 		np = __of_find_node_by_path(np, path);
848 		path = strchrnul(path, '/');
849 		if (separator && separator < path)
850 			break;
851 	}
852 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
853 	return np;
854 }
855 EXPORT_SYMBOL(of_find_node_opts_by_path);
856 
857 /**
858  *	of_find_node_by_name - Find a node by its "name" property
859  *	@from:	The node to start searching from or NULL, the node
860  *		you pass will not be searched, only the next one
861  *		will; typically, you pass what the previous call
862  *		returned. of_node_put() will be called on it
863  *	@name:	The name string to match against
864  *
865  *	Returns a node pointer with refcount incremented, use
866  *	of_node_put() on it when done.
867  */
868 struct device_node *of_find_node_by_name(struct device_node *from,
869 	const char *name)
870 {
871 	struct device_node *np;
872 	unsigned long flags;
873 
874 	raw_spin_lock_irqsave(&devtree_lock, flags);
875 	for_each_of_allnodes_from(from, np)
876 		if (np->name && (of_node_cmp(np->name, name) == 0)
877 		    && of_node_get(np))
878 			break;
879 	of_node_put(from);
880 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
881 	return np;
882 }
883 EXPORT_SYMBOL(of_find_node_by_name);
884 
885 /**
886  *	of_find_node_by_type - Find a node by its "device_type" property
887  *	@from:	The node to start searching from, or NULL to start searching
888  *		the entire device tree. The node you pass will not be
889  *		searched, only the next one will; typically, you pass
890  *		what the previous call returned. of_node_put() will be
891  *		called on from for you.
892  *	@type:	The type string to match against
893  *
894  *	Returns a node pointer with refcount incremented, use
895  *	of_node_put() on it when done.
896  */
897 struct device_node *of_find_node_by_type(struct device_node *from,
898 	const char *type)
899 {
900 	struct device_node *np;
901 	unsigned long flags;
902 
903 	raw_spin_lock_irqsave(&devtree_lock, flags);
904 	for_each_of_allnodes_from(from, np)
905 		if (np->type && (of_node_cmp(np->type, type) == 0)
906 		    && of_node_get(np))
907 			break;
908 	of_node_put(from);
909 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
910 	return np;
911 }
912 EXPORT_SYMBOL(of_find_node_by_type);
913 
914 /**
915  *	of_find_compatible_node - Find a node based on type and one of the
916  *                                tokens in its "compatible" property
917  *	@from:		The node to start searching from or NULL, the node
918  *			you pass will not be searched, only the next one
919  *			will; typically, you pass what the previous call
920  *			returned. of_node_put() will be called on it
921  *	@type:		The type string to match "device_type" or NULL to ignore
922  *	@compatible:	The string to match to one of the tokens in the device
923  *			"compatible" list.
924  *
925  *	Returns a node pointer with refcount incremented, use
926  *	of_node_put() on it when done.
927  */
928 struct device_node *of_find_compatible_node(struct device_node *from,
929 	const char *type, const char *compatible)
930 {
931 	struct device_node *np;
932 	unsigned long flags;
933 
934 	raw_spin_lock_irqsave(&devtree_lock, flags);
935 	for_each_of_allnodes_from(from, np)
936 		if (__of_device_is_compatible(np, compatible, type, NULL) &&
937 		    of_node_get(np))
938 			break;
939 	of_node_put(from);
940 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
941 	return np;
942 }
943 EXPORT_SYMBOL(of_find_compatible_node);
944 
945 /**
946  *	of_find_node_with_property - Find a node which has a property with
947  *                                   the given name.
948  *	@from:		The node to start searching from or NULL, the node
949  *			you pass will not be searched, only the next one
950  *			will; typically, you pass what the previous call
951  *			returned. of_node_put() will be called on it
952  *	@prop_name:	The name of the property to look for.
953  *
954  *	Returns a node pointer with refcount incremented, use
955  *	of_node_put() on it when done.
956  */
957 struct device_node *of_find_node_with_property(struct device_node *from,
958 	const char *prop_name)
959 {
960 	struct device_node *np;
961 	struct property *pp;
962 	unsigned long flags;
963 
964 	raw_spin_lock_irqsave(&devtree_lock, flags);
965 	for_each_of_allnodes_from(from, np) {
966 		for (pp = np->properties; pp; pp = pp->next) {
967 			if (of_prop_cmp(pp->name, prop_name) == 0) {
968 				of_node_get(np);
969 				goto out;
970 			}
971 		}
972 	}
973 out:
974 	of_node_put(from);
975 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
976 	return np;
977 }
978 EXPORT_SYMBOL(of_find_node_with_property);
979 
980 static
981 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
982 					   const struct device_node *node)
983 {
984 	const struct of_device_id *best_match = NULL;
985 	int score, best_score = 0;
986 
987 	if (!matches)
988 		return NULL;
989 
990 	for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
991 		score = __of_device_is_compatible(node, matches->compatible,
992 						  matches->type, matches->name);
993 		if (score > best_score) {
994 			best_match = matches;
995 			best_score = score;
996 		}
997 	}
998 
999 	return best_match;
1000 }
1001 
1002 /**
1003  * of_match_node - Tell if a device_node has a matching of_match structure
1004  *	@matches:	array of of device match structures to search in
1005  *	@node:		the of device structure to match against
1006  *
1007  *	Low level utility function used by device matching.
1008  */
1009 const struct of_device_id *of_match_node(const struct of_device_id *matches,
1010 					 const struct device_node *node)
1011 {
1012 	const struct of_device_id *match;
1013 	unsigned long flags;
1014 
1015 	raw_spin_lock_irqsave(&devtree_lock, flags);
1016 	match = __of_match_node(matches, node);
1017 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1018 	return match;
1019 }
1020 EXPORT_SYMBOL(of_match_node);
1021 
1022 /**
1023  *	of_find_matching_node_and_match - Find a node based on an of_device_id
1024  *					  match table.
1025  *	@from:		The node to start searching from or NULL, the node
1026  *			you pass will not be searched, only the next one
1027  *			will; typically, you pass what the previous call
1028  *			returned. of_node_put() will be called on it
1029  *	@matches:	array of of device match structures to search in
1030  *	@match		Updated to point at the matches entry which matched
1031  *
1032  *	Returns a node pointer with refcount incremented, use
1033  *	of_node_put() on it when done.
1034  */
1035 struct device_node *of_find_matching_node_and_match(struct device_node *from,
1036 					const struct of_device_id *matches,
1037 					const struct of_device_id **match)
1038 {
1039 	struct device_node *np;
1040 	const struct of_device_id *m;
1041 	unsigned long flags;
1042 
1043 	if (match)
1044 		*match = NULL;
1045 
1046 	raw_spin_lock_irqsave(&devtree_lock, flags);
1047 	for_each_of_allnodes_from(from, np) {
1048 		m = __of_match_node(matches, np);
1049 		if (m && of_node_get(np)) {
1050 			if (match)
1051 				*match = m;
1052 			break;
1053 		}
1054 	}
1055 	of_node_put(from);
1056 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1057 	return np;
1058 }
1059 EXPORT_SYMBOL(of_find_matching_node_and_match);
1060 
1061 /**
1062  * of_modalias_node - Lookup appropriate modalias for a device node
1063  * @node:	pointer to a device tree node
1064  * @modalias:	Pointer to buffer that modalias value will be copied into
1065  * @len:	Length of modalias value
1066  *
1067  * Based on the value of the compatible property, this routine will attempt
1068  * to choose an appropriate modalias value for a particular device tree node.
1069  * It does this by stripping the manufacturer prefix (as delimited by a ',')
1070  * from the first entry in the compatible list property.
1071  *
1072  * This routine returns 0 on success, <0 on failure.
1073  */
1074 int of_modalias_node(struct device_node *node, char *modalias, int len)
1075 {
1076 	const char *compatible, *p;
1077 	int cplen;
1078 
1079 	compatible = of_get_property(node, "compatible", &cplen);
1080 	if (!compatible || strlen(compatible) > cplen)
1081 		return -ENODEV;
1082 	p = strchr(compatible, ',');
1083 	strlcpy(modalias, p ? p + 1 : compatible, len);
1084 	return 0;
1085 }
1086 EXPORT_SYMBOL_GPL(of_modalias_node);
1087 
1088 /**
1089  * of_find_node_by_phandle - Find a node given a phandle
1090  * @handle:	phandle of the node to find
1091  *
1092  * Returns a node pointer with refcount incremented, use
1093  * of_node_put() on it when done.
1094  */
1095 struct device_node *of_find_node_by_phandle(phandle handle)
1096 {
1097 	struct device_node *np;
1098 	unsigned long flags;
1099 
1100 	if (!handle)
1101 		return NULL;
1102 
1103 	raw_spin_lock_irqsave(&devtree_lock, flags);
1104 	for_each_of_allnodes(np)
1105 		if (np->phandle == handle)
1106 			break;
1107 	of_node_get(np);
1108 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1109 	return np;
1110 }
1111 EXPORT_SYMBOL(of_find_node_by_phandle);
1112 
1113 /**
1114  * of_property_count_elems_of_size - Count the number of elements in a property
1115  *
1116  * @np:		device node from which the property value is to be read.
1117  * @propname:	name of the property to be searched.
1118  * @elem_size:	size of the individual element
1119  *
1120  * Search for a property in a device node and count the number of elements of
1121  * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1122  * property does not exist or its length does not match a multiple of elem_size
1123  * and -ENODATA if the property does not have a value.
1124  */
1125 int of_property_count_elems_of_size(const struct device_node *np,
1126 				const char *propname, int elem_size)
1127 {
1128 	struct property *prop = of_find_property(np, propname, NULL);
1129 
1130 	if (!prop)
1131 		return -EINVAL;
1132 	if (!prop->value)
1133 		return -ENODATA;
1134 
1135 	if (prop->length % elem_size != 0) {
1136 		pr_err("size of %s in node %s is not a multiple of %d\n",
1137 		       propname, np->full_name, elem_size);
1138 		return -EINVAL;
1139 	}
1140 
1141 	return prop->length / elem_size;
1142 }
1143 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1144 
1145 /**
1146  * of_find_property_value_of_size
1147  *
1148  * @np:		device node from which the property value is to be read.
1149  * @propname:	name of the property to be searched.
1150  * @min:	minimum allowed length of property value
1151  * @max:	maximum allowed length of property value (0 means unlimited)
1152  * @len:	if !=NULL, actual length is written to here
1153  *
1154  * Search for a property in a device node and valid the requested size.
1155  * Returns the property value on success, -EINVAL if the property does not
1156  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1157  * property data is too small or too large.
1158  *
1159  */
1160 static void *of_find_property_value_of_size(const struct device_node *np,
1161 			const char *propname, u32 min, u32 max, size_t *len)
1162 {
1163 	struct property *prop = of_find_property(np, propname, NULL);
1164 
1165 	if (!prop)
1166 		return ERR_PTR(-EINVAL);
1167 	if (!prop->value)
1168 		return ERR_PTR(-ENODATA);
1169 	if (prop->length < min)
1170 		return ERR_PTR(-EOVERFLOW);
1171 	if (max && prop->length > max)
1172 		return ERR_PTR(-EOVERFLOW);
1173 
1174 	if (len)
1175 		*len = prop->length;
1176 
1177 	return prop->value;
1178 }
1179 
1180 /**
1181  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1182  *
1183  * @np:		device node from which the property value is to be read.
1184  * @propname:	name of the property to be searched.
1185  * @index:	index of the u32 in the list of values
1186  * @out_value:	pointer to return value, modified only if no error.
1187  *
1188  * Search for a property in a device node and read nth 32-bit value from
1189  * it. Returns 0 on success, -EINVAL if the property does not exist,
1190  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1191  * property data isn't large enough.
1192  *
1193  * The out_value is modified only if a valid u32 value can be decoded.
1194  */
1195 int of_property_read_u32_index(const struct device_node *np,
1196 				       const char *propname,
1197 				       u32 index, u32 *out_value)
1198 {
1199 	const u32 *val = of_find_property_value_of_size(np, propname,
1200 					((index + 1) * sizeof(*out_value)),
1201 					0,
1202 					NULL);
1203 
1204 	if (IS_ERR(val))
1205 		return PTR_ERR(val);
1206 
1207 	*out_value = be32_to_cpup(((__be32 *)val) + index);
1208 	return 0;
1209 }
1210 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1211 
1212 /**
1213  * of_property_read_variable_u8_array - Find and read an array of u8 from a
1214  * property, with bounds on the minimum and maximum array size.
1215  *
1216  * @np:		device node from which the property value is to be read.
1217  * @propname:	name of the property to be searched.
1218  * @out_values:	pointer to return value, modified only if return value is 0.
1219  * @sz_min:	minimum number of array elements to read
1220  * @sz_max:	maximum number of array elements to read, if zero there is no
1221  *		upper limit on the number of elements in the dts entry but only
1222  *		sz_min will be read.
1223  *
1224  * Search for a property in a device node and read 8-bit value(s) from
1225  * it. Returns number of elements read on success, -EINVAL if the property
1226  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1227  * if the property data is smaller than sz_min or longer than sz_max.
1228  *
1229  * dts entry of array should be like:
1230  *	property = /bits/ 8 <0x50 0x60 0x70>;
1231  *
1232  * The out_values is modified only if a valid u8 value can be decoded.
1233  */
1234 int of_property_read_variable_u8_array(const struct device_node *np,
1235 					const char *propname, u8 *out_values,
1236 					size_t sz_min, size_t sz_max)
1237 {
1238 	size_t sz, count;
1239 	const u8 *val = of_find_property_value_of_size(np, propname,
1240 						(sz_min * sizeof(*out_values)),
1241 						(sz_max * sizeof(*out_values)),
1242 						&sz);
1243 
1244 	if (IS_ERR(val))
1245 		return PTR_ERR(val);
1246 
1247 	if (!sz_max)
1248 		sz = sz_min;
1249 	else
1250 		sz /= sizeof(*out_values);
1251 
1252 	count = sz;
1253 	while (count--)
1254 		*out_values++ = *val++;
1255 
1256 	return sz;
1257 }
1258 EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array);
1259 
1260 /**
1261  * of_property_read_variable_u16_array - Find and read an array of u16 from a
1262  * property, with bounds on the minimum and maximum array size.
1263  *
1264  * @np:		device node from which the property value is to be read.
1265  * @propname:	name of the property to be searched.
1266  * @out_values:	pointer to return value, modified only if return value is 0.
1267  * @sz_min:	minimum number of array elements to read
1268  * @sz_max:	maximum number of array elements to read, if zero there is no
1269  *		upper limit on the number of elements in the dts entry but only
1270  *		sz_min will be read.
1271  *
1272  * Search for a property in a device node and read 16-bit value(s) from
1273  * it. Returns number of elements read on success, -EINVAL if the property
1274  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1275  * if the property data is smaller than sz_min or longer than sz_max.
1276  *
1277  * dts entry of array should be like:
1278  *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
1279  *
1280  * The out_values is modified only if a valid u16 value can be decoded.
1281  */
1282 int of_property_read_variable_u16_array(const struct device_node *np,
1283 					const char *propname, u16 *out_values,
1284 					size_t sz_min, size_t sz_max)
1285 {
1286 	size_t sz, count;
1287 	const __be16 *val = of_find_property_value_of_size(np, propname,
1288 						(sz_min * sizeof(*out_values)),
1289 						(sz_max * sizeof(*out_values)),
1290 						&sz);
1291 
1292 	if (IS_ERR(val))
1293 		return PTR_ERR(val);
1294 
1295 	if (!sz_max)
1296 		sz = sz_min;
1297 	else
1298 		sz /= sizeof(*out_values);
1299 
1300 	count = sz;
1301 	while (count--)
1302 		*out_values++ = be16_to_cpup(val++);
1303 
1304 	return sz;
1305 }
1306 EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array);
1307 
1308 /**
1309  * of_property_read_variable_u32_array - Find and read an array of 32 bit
1310  * integers from a property, with bounds on the minimum and maximum array size.
1311  *
1312  * @np:		device node from which the property value is to be read.
1313  * @propname:	name of the property to be searched.
1314  * @out_values:	pointer to return value, modified only if return value is 0.
1315  * @sz_min:	minimum number of array elements to read
1316  * @sz_max:	maximum number of array elements to read, if zero there is no
1317  *		upper limit on the number of elements in the dts entry but only
1318  *		sz_min will be read.
1319  *
1320  * Search for a property in a device node and read 32-bit value(s) from
1321  * it. Returns number of elements read on success, -EINVAL if the property
1322  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1323  * if the property data is smaller than sz_min or longer than sz_max.
1324  *
1325  * The out_values is modified only if a valid u32 value can be decoded.
1326  */
1327 int of_property_read_variable_u32_array(const struct device_node *np,
1328 			       const char *propname, u32 *out_values,
1329 			       size_t sz_min, size_t sz_max)
1330 {
1331 	size_t sz, count;
1332 	const __be32 *val = of_find_property_value_of_size(np, propname,
1333 						(sz_min * sizeof(*out_values)),
1334 						(sz_max * sizeof(*out_values)),
1335 						&sz);
1336 
1337 	if (IS_ERR(val))
1338 		return PTR_ERR(val);
1339 
1340 	if (!sz_max)
1341 		sz = sz_min;
1342 	else
1343 		sz /= sizeof(*out_values);
1344 
1345 	count = sz;
1346 	while (count--)
1347 		*out_values++ = be32_to_cpup(val++);
1348 
1349 	return sz;
1350 }
1351 EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array);
1352 
1353 /**
1354  * of_property_read_u64 - Find and read a 64 bit integer from a property
1355  * @np:		device node from which the property value is to be read.
1356  * @propname:	name of the property to be searched.
1357  * @out_value:	pointer to return value, modified only if return value is 0.
1358  *
1359  * Search for a property in a device node and read a 64-bit value from
1360  * it. Returns 0 on success, -EINVAL if the property does not exist,
1361  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1362  * property data isn't large enough.
1363  *
1364  * The out_value is modified only if a valid u64 value can be decoded.
1365  */
1366 int of_property_read_u64(const struct device_node *np, const char *propname,
1367 			 u64 *out_value)
1368 {
1369 	const __be32 *val = of_find_property_value_of_size(np, propname,
1370 						sizeof(*out_value),
1371 						0,
1372 						NULL);
1373 
1374 	if (IS_ERR(val))
1375 		return PTR_ERR(val);
1376 
1377 	*out_value = of_read_number(val, 2);
1378 	return 0;
1379 }
1380 EXPORT_SYMBOL_GPL(of_property_read_u64);
1381 
1382 /**
1383  * of_property_read_variable_u64_array - Find and read an array of 64 bit
1384  * integers from a property, with bounds on the minimum and maximum array size.
1385  *
1386  * @np:		device node from which the property value is to be read.
1387  * @propname:	name of the property to be searched.
1388  * @out_values:	pointer to return value, modified only if return value is 0.
1389  * @sz_min:	minimum number of array elements to read
1390  * @sz_max:	maximum number of array elements to read, if zero there is no
1391  *		upper limit on the number of elements in the dts entry but only
1392  *		sz_min will be read.
1393  *
1394  * Search for a property in a device node and read 64-bit value(s) from
1395  * it. Returns number of elements read on success, -EINVAL if the property
1396  * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW
1397  * if the property data is smaller than sz_min or longer than sz_max.
1398  *
1399  * The out_values is modified only if a valid u64 value can be decoded.
1400  */
1401 int of_property_read_variable_u64_array(const struct device_node *np,
1402 			       const char *propname, u64 *out_values,
1403 			       size_t sz_min, size_t sz_max)
1404 {
1405 	size_t sz, count;
1406 	const __be32 *val = of_find_property_value_of_size(np, propname,
1407 						(sz_min * sizeof(*out_values)),
1408 						(sz_max * sizeof(*out_values)),
1409 						&sz);
1410 
1411 	if (IS_ERR(val))
1412 		return PTR_ERR(val);
1413 
1414 	if (!sz_max)
1415 		sz = sz_min;
1416 	else
1417 		sz /= sizeof(*out_values);
1418 
1419 	count = sz;
1420 	while (count--) {
1421 		*out_values++ = of_read_number(val, 2);
1422 		val += 2;
1423 	}
1424 
1425 	return sz;
1426 }
1427 EXPORT_SYMBOL_GPL(of_property_read_variable_u64_array);
1428 
1429 /**
1430  * of_property_read_string - Find and read a string from a property
1431  * @np:		device node from which the property value is to be read.
1432  * @propname:	name of the property to be searched.
1433  * @out_string:	pointer to null terminated return string, modified only if
1434  *		return value is 0.
1435  *
1436  * Search for a property in a device tree node and retrieve a null
1437  * terminated string value (pointer to data, not a copy). Returns 0 on
1438  * success, -EINVAL if the property does not exist, -ENODATA if property
1439  * does not have a value, and -EILSEQ if the string is not null-terminated
1440  * within the length of the property data.
1441  *
1442  * The out_string pointer is modified only if a valid string can be decoded.
1443  */
1444 int of_property_read_string(const struct device_node *np, const char *propname,
1445 				const char **out_string)
1446 {
1447 	const struct property *prop = of_find_property(np, propname, NULL);
1448 	if (!prop)
1449 		return -EINVAL;
1450 	if (!prop->value)
1451 		return -ENODATA;
1452 	if (strnlen(prop->value, prop->length) >= prop->length)
1453 		return -EILSEQ;
1454 	*out_string = prop->value;
1455 	return 0;
1456 }
1457 EXPORT_SYMBOL_GPL(of_property_read_string);
1458 
1459 /**
1460  * of_property_match_string() - Find string in a list and return index
1461  * @np: pointer to node containing string list property
1462  * @propname: string list property name
1463  * @string: pointer to string to search for in string list
1464  *
1465  * This function searches a string list property and returns the index
1466  * of a specific string value.
1467  */
1468 int of_property_match_string(const struct device_node *np, const char *propname,
1469 			     const char *string)
1470 {
1471 	const struct property *prop = of_find_property(np, propname, NULL);
1472 	size_t l;
1473 	int i;
1474 	const char *p, *end;
1475 
1476 	if (!prop)
1477 		return -EINVAL;
1478 	if (!prop->value)
1479 		return -ENODATA;
1480 
1481 	p = prop->value;
1482 	end = p + prop->length;
1483 
1484 	for (i = 0; p < end; i++, p += l) {
1485 		l = strnlen(p, end - p) + 1;
1486 		if (p + l > end)
1487 			return -EILSEQ;
1488 		pr_debug("comparing %s with %s\n", string, p);
1489 		if (strcmp(string, p) == 0)
1490 			return i; /* Found it; return index */
1491 	}
1492 	return -ENODATA;
1493 }
1494 EXPORT_SYMBOL_GPL(of_property_match_string);
1495 
1496 /**
1497  * of_property_read_string_helper() - Utility helper for parsing string properties
1498  * @np:		device node from which the property value is to be read.
1499  * @propname:	name of the property to be searched.
1500  * @out_strs:	output array of string pointers.
1501  * @sz:		number of array elements to read.
1502  * @skip:	Number of strings to skip over at beginning of list.
1503  *
1504  * Don't call this function directly. It is a utility helper for the
1505  * of_property_read_string*() family of functions.
1506  */
1507 int of_property_read_string_helper(const struct device_node *np,
1508 				   const char *propname, const char **out_strs,
1509 				   size_t sz, int skip)
1510 {
1511 	const struct property *prop = of_find_property(np, propname, NULL);
1512 	int l = 0, i = 0;
1513 	const char *p, *end;
1514 
1515 	if (!prop)
1516 		return -EINVAL;
1517 	if (!prop->value)
1518 		return -ENODATA;
1519 	p = prop->value;
1520 	end = p + prop->length;
1521 
1522 	for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1523 		l = strnlen(p, end - p) + 1;
1524 		if (p + l > end)
1525 			return -EILSEQ;
1526 		if (out_strs && i >= skip)
1527 			*out_strs++ = p;
1528 	}
1529 	i -= skip;
1530 	return i <= 0 ? -ENODATA : i;
1531 }
1532 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1533 
1534 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1535 {
1536 	int i;
1537 	printk("%s %s", msg, of_node_full_name(args->np));
1538 	for (i = 0; i < args->args_count; i++) {
1539 		const char delim = i ? ',' : ':';
1540 
1541 		pr_cont("%c%08x", delim, args->args[i]);
1542 	}
1543 	pr_cont("\n");
1544 }
1545 
1546 int of_phandle_iterator_init(struct of_phandle_iterator *it,
1547 		const struct device_node *np,
1548 		const char *list_name,
1549 		const char *cells_name,
1550 		int cell_count)
1551 {
1552 	const __be32 *list;
1553 	int size;
1554 
1555 	memset(it, 0, sizeof(*it));
1556 
1557 	list = of_get_property(np, list_name, &size);
1558 	if (!list)
1559 		return -ENOENT;
1560 
1561 	it->cells_name = cells_name;
1562 	it->cell_count = cell_count;
1563 	it->parent = np;
1564 	it->list_end = list + size / sizeof(*list);
1565 	it->phandle_end = list;
1566 	it->cur = list;
1567 
1568 	return 0;
1569 }
1570 
1571 int of_phandle_iterator_next(struct of_phandle_iterator *it)
1572 {
1573 	uint32_t count = 0;
1574 
1575 	if (it->node) {
1576 		of_node_put(it->node);
1577 		it->node = NULL;
1578 	}
1579 
1580 	if (!it->cur || it->phandle_end >= it->list_end)
1581 		return -ENOENT;
1582 
1583 	it->cur = it->phandle_end;
1584 
1585 	/* If phandle is 0, then it is an empty entry with no arguments. */
1586 	it->phandle = be32_to_cpup(it->cur++);
1587 
1588 	if (it->phandle) {
1589 
1590 		/*
1591 		 * Find the provider node and parse the #*-cells property to
1592 		 * determine the argument length.
1593 		 */
1594 		it->node = of_find_node_by_phandle(it->phandle);
1595 
1596 		if (it->cells_name) {
1597 			if (!it->node) {
1598 				pr_err("%s: could not find phandle\n",
1599 				       it->parent->full_name);
1600 				goto err;
1601 			}
1602 
1603 			if (of_property_read_u32(it->node, it->cells_name,
1604 						 &count)) {
1605 				pr_err("%s: could not get %s for %s\n",
1606 				       it->parent->full_name,
1607 				       it->cells_name,
1608 				       it->node->full_name);
1609 				goto err;
1610 			}
1611 		} else {
1612 			count = it->cell_count;
1613 		}
1614 
1615 		/*
1616 		 * Make sure that the arguments actually fit in the remaining
1617 		 * property data length
1618 		 */
1619 		if (it->cur + count > it->list_end) {
1620 			pr_err("%s: arguments longer than property\n",
1621 			       it->parent->full_name);
1622 			goto err;
1623 		}
1624 	}
1625 
1626 	it->phandle_end = it->cur + count;
1627 	it->cur_count = count;
1628 
1629 	return 0;
1630 
1631 err:
1632 	if (it->node) {
1633 		of_node_put(it->node);
1634 		it->node = NULL;
1635 	}
1636 
1637 	return -EINVAL;
1638 }
1639 
1640 int of_phandle_iterator_args(struct of_phandle_iterator *it,
1641 			     uint32_t *args,
1642 			     int size)
1643 {
1644 	int i, count;
1645 
1646 	count = it->cur_count;
1647 
1648 	if (WARN_ON(size < count))
1649 		count = size;
1650 
1651 	for (i = 0; i < count; i++)
1652 		args[i] = be32_to_cpup(it->cur++);
1653 
1654 	return count;
1655 }
1656 
1657 static int __of_parse_phandle_with_args(const struct device_node *np,
1658 					const char *list_name,
1659 					const char *cells_name,
1660 					int cell_count, int index,
1661 					struct of_phandle_args *out_args)
1662 {
1663 	struct of_phandle_iterator it;
1664 	int rc, cur_index = 0;
1665 
1666 	/* Loop over the phandles until all the requested entry is found */
1667 	of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
1668 		/*
1669 		 * All of the error cases bail out of the loop, so at
1670 		 * this point, the parsing is successful. If the requested
1671 		 * index matches, then fill the out_args structure and return,
1672 		 * or return -ENOENT for an empty entry.
1673 		 */
1674 		rc = -ENOENT;
1675 		if (cur_index == index) {
1676 			if (!it.phandle)
1677 				goto err;
1678 
1679 			if (out_args) {
1680 				int c;
1681 
1682 				c = of_phandle_iterator_args(&it,
1683 							     out_args->args,
1684 							     MAX_PHANDLE_ARGS);
1685 				out_args->np = it.node;
1686 				out_args->args_count = c;
1687 			} else {
1688 				of_node_put(it.node);
1689 			}
1690 
1691 			/* Found it! return success */
1692 			return 0;
1693 		}
1694 
1695 		cur_index++;
1696 	}
1697 
1698 	/*
1699 	 * Unlock node before returning result; will be one of:
1700 	 * -ENOENT : index is for empty phandle
1701 	 * -EINVAL : parsing error on data
1702 	 */
1703 
1704  err:
1705 	of_node_put(it.node);
1706 	return rc;
1707 }
1708 
1709 /**
1710  * of_parse_phandle - Resolve a phandle property to a device_node pointer
1711  * @np: Pointer to device node holding phandle property
1712  * @phandle_name: Name of property holding a phandle value
1713  * @index: For properties holding a table of phandles, this is the index into
1714  *         the table
1715  *
1716  * Returns the device_node pointer with refcount incremented.  Use
1717  * of_node_put() on it when done.
1718  */
1719 struct device_node *of_parse_phandle(const struct device_node *np,
1720 				     const char *phandle_name, int index)
1721 {
1722 	struct of_phandle_args args;
1723 
1724 	if (index < 0)
1725 		return NULL;
1726 
1727 	if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1728 					 index, &args))
1729 		return NULL;
1730 
1731 	return args.np;
1732 }
1733 EXPORT_SYMBOL(of_parse_phandle);
1734 
1735 /**
1736  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1737  * @np:		pointer to a device tree node containing a list
1738  * @list_name:	property name that contains a list
1739  * @cells_name:	property name that specifies phandles' arguments count
1740  * @index:	index of a phandle to parse out
1741  * @out_args:	optional pointer to output arguments structure (will be filled)
1742  *
1743  * This function is useful to parse lists of phandles and their arguments.
1744  * Returns 0 on success and fills out_args, on error returns appropriate
1745  * errno value.
1746  *
1747  * Caller is responsible to call of_node_put() on the returned out_args->np
1748  * pointer.
1749  *
1750  * Example:
1751  *
1752  * phandle1: node1 {
1753  *	#list-cells = <2>;
1754  * }
1755  *
1756  * phandle2: node2 {
1757  *	#list-cells = <1>;
1758  * }
1759  *
1760  * node3 {
1761  *	list = <&phandle1 1 2 &phandle2 3>;
1762  * }
1763  *
1764  * To get a device_node of the `node2' node you may call this:
1765  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1766  */
1767 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1768 				const char *cells_name, int index,
1769 				struct of_phandle_args *out_args)
1770 {
1771 	if (index < 0)
1772 		return -EINVAL;
1773 	return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1774 					    index, out_args);
1775 }
1776 EXPORT_SYMBOL(of_parse_phandle_with_args);
1777 
1778 /**
1779  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1780  * @np:		pointer to a device tree node containing a list
1781  * @list_name:	property name that contains a list
1782  * @cell_count: number of argument cells following the phandle
1783  * @index:	index of a phandle to parse out
1784  * @out_args:	optional pointer to output arguments structure (will be filled)
1785  *
1786  * This function is useful to parse lists of phandles and their arguments.
1787  * Returns 0 on success and fills out_args, on error returns appropriate
1788  * errno value.
1789  *
1790  * Caller is responsible to call of_node_put() on the returned out_args->np
1791  * pointer.
1792  *
1793  * Example:
1794  *
1795  * phandle1: node1 {
1796  * }
1797  *
1798  * phandle2: node2 {
1799  * }
1800  *
1801  * node3 {
1802  *	list = <&phandle1 0 2 &phandle2 2 3>;
1803  * }
1804  *
1805  * To get a device_node of the `node2' node you may call this:
1806  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1807  */
1808 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1809 				const char *list_name, int cell_count,
1810 				int index, struct of_phandle_args *out_args)
1811 {
1812 	if (index < 0)
1813 		return -EINVAL;
1814 	return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1815 					   index, out_args);
1816 }
1817 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1818 
1819 /**
1820  * of_count_phandle_with_args() - Find the number of phandles references in a property
1821  * @np:		pointer to a device tree node containing a list
1822  * @list_name:	property name that contains a list
1823  * @cells_name:	property name that specifies phandles' arguments count
1824  *
1825  * Returns the number of phandle + argument tuples within a property. It
1826  * is a typical pattern to encode a list of phandle and variable
1827  * arguments into a single property. The number of arguments is encoded
1828  * by a property in the phandle-target node. For example, a gpios
1829  * property would contain a list of GPIO specifies consisting of a
1830  * phandle and 1 or more arguments. The number of arguments are
1831  * determined by the #gpio-cells property in the node pointed to by the
1832  * phandle.
1833  */
1834 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1835 				const char *cells_name)
1836 {
1837 	struct of_phandle_iterator it;
1838 	int rc, cur_index = 0;
1839 
1840 	rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1841 	if (rc)
1842 		return rc;
1843 
1844 	while ((rc = of_phandle_iterator_next(&it)) == 0)
1845 		cur_index += 1;
1846 
1847 	if (rc != -ENOENT)
1848 		return rc;
1849 
1850 	return cur_index;
1851 }
1852 EXPORT_SYMBOL(of_count_phandle_with_args);
1853 
1854 /**
1855  * __of_add_property - Add a property to a node without lock operations
1856  */
1857 int __of_add_property(struct device_node *np, struct property *prop)
1858 {
1859 	struct property **next;
1860 
1861 	prop->next = NULL;
1862 	next = &np->properties;
1863 	while (*next) {
1864 		if (strcmp(prop->name, (*next)->name) == 0)
1865 			/* duplicate ! don't insert it */
1866 			return -EEXIST;
1867 
1868 		next = &(*next)->next;
1869 	}
1870 	*next = prop;
1871 
1872 	return 0;
1873 }
1874 
1875 /**
1876  * of_add_property - Add a property to a node
1877  */
1878 int of_add_property(struct device_node *np, struct property *prop)
1879 {
1880 	unsigned long flags;
1881 	int rc;
1882 
1883 	mutex_lock(&of_mutex);
1884 
1885 	raw_spin_lock_irqsave(&devtree_lock, flags);
1886 	rc = __of_add_property(np, prop);
1887 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1888 
1889 	if (!rc)
1890 		__of_add_property_sysfs(np, prop);
1891 
1892 	mutex_unlock(&of_mutex);
1893 
1894 	if (!rc)
1895 		of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1896 
1897 	return rc;
1898 }
1899 
1900 int __of_remove_property(struct device_node *np, struct property *prop)
1901 {
1902 	struct property **next;
1903 
1904 	for (next = &np->properties; *next; next = &(*next)->next) {
1905 		if (*next == prop)
1906 			break;
1907 	}
1908 	if (*next == NULL)
1909 		return -ENODEV;
1910 
1911 	/* found the node */
1912 	*next = prop->next;
1913 	prop->next = np->deadprops;
1914 	np->deadprops = prop;
1915 
1916 	return 0;
1917 }
1918 
1919 void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
1920 {
1921 	sysfs_remove_bin_file(&np->kobj, &prop->attr);
1922 	kfree(prop->attr.attr.name);
1923 }
1924 
1925 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1926 {
1927 	if (!IS_ENABLED(CONFIG_SYSFS))
1928 		return;
1929 
1930 	/* at early boot, bail here and defer setup to of_init() */
1931 	if (of_kset && of_node_is_attached(np))
1932 		__of_sysfs_remove_bin_file(np, prop);
1933 }
1934 
1935 /**
1936  * of_remove_property - Remove a property from a node.
1937  *
1938  * Note that we don't actually remove it, since we have given out
1939  * who-knows-how-many pointers to the data using get-property.
1940  * Instead we just move the property to the "dead properties"
1941  * list, so it won't be found any more.
1942  */
1943 int of_remove_property(struct device_node *np, struct property *prop)
1944 {
1945 	unsigned long flags;
1946 	int rc;
1947 
1948 	if (!prop)
1949 		return -ENODEV;
1950 
1951 	mutex_lock(&of_mutex);
1952 
1953 	raw_spin_lock_irqsave(&devtree_lock, flags);
1954 	rc = __of_remove_property(np, prop);
1955 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
1956 
1957 	if (!rc)
1958 		__of_remove_property_sysfs(np, prop);
1959 
1960 	mutex_unlock(&of_mutex);
1961 
1962 	if (!rc)
1963 		of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1964 
1965 	return rc;
1966 }
1967 
1968 int __of_update_property(struct device_node *np, struct property *newprop,
1969 		struct property **oldpropp)
1970 {
1971 	struct property **next, *oldprop;
1972 
1973 	for (next = &np->properties; *next; next = &(*next)->next) {
1974 		if (of_prop_cmp((*next)->name, newprop->name) == 0)
1975 			break;
1976 	}
1977 	*oldpropp = oldprop = *next;
1978 
1979 	if (oldprop) {
1980 		/* replace the node */
1981 		newprop->next = oldprop->next;
1982 		*next = newprop;
1983 		oldprop->next = np->deadprops;
1984 		np->deadprops = oldprop;
1985 	} else {
1986 		/* new node */
1987 		newprop->next = NULL;
1988 		*next = newprop;
1989 	}
1990 
1991 	return 0;
1992 }
1993 
1994 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1995 		struct property *oldprop)
1996 {
1997 	if (!IS_ENABLED(CONFIG_SYSFS))
1998 		return;
1999 
2000 	/* At early boot, bail out and defer setup to of_init() */
2001 	if (!of_kset)
2002 		return;
2003 
2004 	if (oldprop)
2005 		__of_sysfs_remove_bin_file(np, oldprop);
2006 	__of_add_property_sysfs(np, newprop);
2007 }
2008 
2009 /*
2010  * of_update_property - Update a property in a node, if the property does
2011  * not exist, add it.
2012  *
2013  * Note that we don't actually remove it, since we have given out
2014  * who-knows-how-many pointers to the data using get-property.
2015  * Instead we just move the property to the "dead properties" list,
2016  * and add the new property to the property list
2017  */
2018 int of_update_property(struct device_node *np, struct property *newprop)
2019 {
2020 	struct property *oldprop;
2021 	unsigned long flags;
2022 	int rc;
2023 
2024 	if (!newprop->name)
2025 		return -EINVAL;
2026 
2027 	mutex_lock(&of_mutex);
2028 
2029 	raw_spin_lock_irqsave(&devtree_lock, flags);
2030 	rc = __of_update_property(np, newprop, &oldprop);
2031 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
2032 
2033 	if (!rc)
2034 		__of_update_property_sysfs(np, newprop, oldprop);
2035 
2036 	mutex_unlock(&of_mutex);
2037 
2038 	if (!rc)
2039 		of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
2040 
2041 	return rc;
2042 }
2043 
2044 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
2045 			 int id, const char *stem, int stem_len)
2046 {
2047 	ap->np = np;
2048 	ap->id = id;
2049 	strncpy(ap->stem, stem, stem_len);
2050 	ap->stem[stem_len] = 0;
2051 	list_add_tail(&ap->link, &aliases_lookup);
2052 	pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
2053 		 ap->alias, ap->stem, ap->id, of_node_full_name(np));
2054 }
2055 
2056 /**
2057  * of_alias_scan - Scan all properties of the 'aliases' node
2058  *
2059  * The function scans all the properties of the 'aliases' node and populates
2060  * the global lookup table with the properties.  It returns the
2061  * number of alias properties found, or an error code in case of failure.
2062  *
2063  * @dt_alloc:	An allocator that provides a virtual address to memory
2064  *		for storing the resulting tree
2065  */
2066 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2067 {
2068 	struct property *pp;
2069 
2070 	of_aliases = of_find_node_by_path("/aliases");
2071 	of_chosen = of_find_node_by_path("/chosen");
2072 	if (of_chosen == NULL)
2073 		of_chosen = of_find_node_by_path("/chosen@0");
2074 
2075 	if (of_chosen) {
2076 		/* linux,stdout-path and /aliases/stdout are for legacy compatibility */
2077 		const char *name = of_get_property(of_chosen, "stdout-path", NULL);
2078 		if (!name)
2079 			name = of_get_property(of_chosen, "linux,stdout-path", NULL);
2080 		if (IS_ENABLED(CONFIG_PPC) && !name)
2081 			name = of_get_property(of_aliases, "stdout", NULL);
2082 		if (name)
2083 			of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
2084 	}
2085 
2086 	if (!of_aliases)
2087 		return;
2088 
2089 	for_each_property_of_node(of_aliases, pp) {
2090 		const char *start = pp->name;
2091 		const char *end = start + strlen(start);
2092 		struct device_node *np;
2093 		struct alias_prop *ap;
2094 		int id, len;
2095 
2096 		/* Skip those we do not want to proceed */
2097 		if (!strcmp(pp->name, "name") ||
2098 		    !strcmp(pp->name, "phandle") ||
2099 		    !strcmp(pp->name, "linux,phandle"))
2100 			continue;
2101 
2102 		np = of_find_node_by_path(pp->value);
2103 		if (!np)
2104 			continue;
2105 
2106 		/* walk the alias backwards to extract the id and work out
2107 		 * the 'stem' string */
2108 		while (isdigit(*(end-1)) && end > start)
2109 			end--;
2110 		len = end - start;
2111 
2112 		if (kstrtoint(end, 10, &id) < 0)
2113 			continue;
2114 
2115 		/* Allocate an alias_prop with enough space for the stem */
2116 		ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
2117 		if (!ap)
2118 			continue;
2119 		memset(ap, 0, sizeof(*ap) + len + 1);
2120 		ap->alias = start;
2121 		of_alias_add(ap, np, id, start, len);
2122 	}
2123 }
2124 
2125 /**
2126  * of_alias_get_id - Get alias id for the given device_node
2127  * @np:		Pointer to the given device_node
2128  * @stem:	Alias stem of the given device_node
2129  *
2130  * The function travels the lookup table to get the alias id for the given
2131  * device_node and alias stem.  It returns the alias id if found.
2132  */
2133 int of_alias_get_id(struct device_node *np, const char *stem)
2134 {
2135 	struct alias_prop *app;
2136 	int id = -ENODEV;
2137 
2138 	mutex_lock(&of_mutex);
2139 	list_for_each_entry(app, &aliases_lookup, link) {
2140 		if (strcmp(app->stem, stem) != 0)
2141 			continue;
2142 
2143 		if (np == app->np) {
2144 			id = app->id;
2145 			break;
2146 		}
2147 	}
2148 	mutex_unlock(&of_mutex);
2149 
2150 	return id;
2151 }
2152 EXPORT_SYMBOL_GPL(of_alias_get_id);
2153 
2154 /**
2155  * of_alias_get_highest_id - Get highest alias id for the given stem
2156  * @stem:	Alias stem to be examined
2157  *
2158  * The function travels the lookup table to get the highest alias id for the
2159  * given alias stem.  It returns the alias id if found.
2160  */
2161 int of_alias_get_highest_id(const char *stem)
2162 {
2163 	struct alias_prop *app;
2164 	int id = -ENODEV;
2165 
2166 	mutex_lock(&of_mutex);
2167 	list_for_each_entry(app, &aliases_lookup, link) {
2168 		if (strcmp(app->stem, stem) != 0)
2169 			continue;
2170 
2171 		if (app->id > id)
2172 			id = app->id;
2173 	}
2174 	mutex_unlock(&of_mutex);
2175 
2176 	return id;
2177 }
2178 EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2179 
2180 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
2181 			       u32 *pu)
2182 {
2183 	const void *curv = cur;
2184 
2185 	if (!prop)
2186 		return NULL;
2187 
2188 	if (!cur) {
2189 		curv = prop->value;
2190 		goto out_val;
2191 	}
2192 
2193 	curv += sizeof(*cur);
2194 	if (curv >= prop->value + prop->length)
2195 		return NULL;
2196 
2197 out_val:
2198 	*pu = be32_to_cpup(curv);
2199 	return curv;
2200 }
2201 EXPORT_SYMBOL_GPL(of_prop_next_u32);
2202 
2203 const char *of_prop_next_string(struct property *prop, const char *cur)
2204 {
2205 	const void *curv = cur;
2206 
2207 	if (!prop)
2208 		return NULL;
2209 
2210 	if (!cur)
2211 		return prop->value;
2212 
2213 	curv += strlen(cur) + 1;
2214 	if (curv >= prop->value + prop->length)
2215 		return NULL;
2216 
2217 	return curv;
2218 }
2219 EXPORT_SYMBOL_GPL(of_prop_next_string);
2220 
2221 /**
2222  * of_console_check() - Test and setup console for DT setup
2223  * @dn - Pointer to device node
2224  * @name - Name to use for preferred console without index. ex. "ttyS"
2225  * @index - Index to use for preferred console.
2226  *
2227  * Check if the given device node matches the stdout-path property in the
2228  * /chosen node. If it does then register it as the preferred console and return
2229  * TRUE. Otherwise return FALSE.
2230  */
2231 bool of_console_check(struct device_node *dn, char *name, int index)
2232 {
2233 	if (!dn || dn != of_stdout || console_set_on_cmdline)
2234 		return false;
2235 	return !add_preferred_console(name, index,
2236 				      kstrdup(of_stdout_options, GFP_KERNEL));
2237 }
2238 EXPORT_SYMBOL_GPL(of_console_check);
2239 
2240 /**
2241  *	of_find_next_cache_node - Find a node's subsidiary cache
2242  *	@np:	node of type "cpu" or "cache"
2243  *
2244  *	Returns a node pointer with refcount incremented, use
2245  *	of_node_put() on it when done.  Caller should hold a reference
2246  *	to np.
2247  */
2248 struct device_node *of_find_next_cache_node(const struct device_node *np)
2249 {
2250 	struct device_node *child;
2251 	const phandle *handle;
2252 
2253 	handle = of_get_property(np, "l2-cache", NULL);
2254 	if (!handle)
2255 		handle = of_get_property(np, "next-level-cache", NULL);
2256 
2257 	if (handle)
2258 		return of_find_node_by_phandle(be32_to_cpup(handle));
2259 
2260 	/* OF on pmac has nodes instead of properties named "l2-cache"
2261 	 * beneath CPU nodes.
2262 	 */
2263 	if (!strcmp(np->type, "cpu"))
2264 		for_each_child_of_node(np, child)
2265 			if (!strcmp(child->type, "cache"))
2266 				return child;
2267 
2268 	return NULL;
2269 }
2270 
2271 /**
2272  * of_find_last_cache_level - Find the level at which the last cache is
2273  * 		present for the given logical cpu
2274  *
2275  * @cpu: cpu number(logical index) for which the last cache level is needed
2276  *
2277  * Returns the the level at which the last cache is present. It is exactly
2278  * same as  the total number of cache levels for the given logical cpu.
2279  */
2280 int of_find_last_cache_level(unsigned int cpu)
2281 {
2282 	u32 cache_level = 0;
2283 	struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2284 
2285 	while (np) {
2286 		prev = np;
2287 		of_node_put(np);
2288 		np = of_find_next_cache_node(np);
2289 	}
2290 
2291 	of_property_read_u32(prev, "cache-level", &cache_level);
2292 
2293 	return cache_level;
2294 }
2295 
2296 /**
2297  * of_graph_parse_endpoint() - parse common endpoint node properties
2298  * @node: pointer to endpoint device_node
2299  * @endpoint: pointer to the OF endpoint data structure
2300  *
2301  * The caller should hold a reference to @node.
2302  */
2303 int of_graph_parse_endpoint(const struct device_node *node,
2304 			    struct of_endpoint *endpoint)
2305 {
2306 	struct device_node *port_node = of_get_parent(node);
2307 
2308 	WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2309 		  __func__, node->full_name);
2310 
2311 	memset(endpoint, 0, sizeof(*endpoint));
2312 
2313 	endpoint->local_node = node;
2314 	/*
2315 	 * It doesn't matter whether the two calls below succeed.
2316 	 * If they don't then the default value 0 is used.
2317 	 */
2318 	of_property_read_u32(port_node, "reg", &endpoint->port);
2319 	of_property_read_u32(node, "reg", &endpoint->id);
2320 
2321 	of_node_put(port_node);
2322 
2323 	return 0;
2324 }
2325 EXPORT_SYMBOL(of_graph_parse_endpoint);
2326 
2327 /**
2328  * of_graph_get_port_by_id() - get the port matching a given id
2329  * @parent: pointer to the parent device node
2330  * @id: id of the port
2331  *
2332  * Return: A 'port' node pointer with refcount incremented. The caller
2333  * has to use of_node_put() on it when done.
2334  */
2335 struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
2336 {
2337 	struct device_node *node, *port;
2338 
2339 	node = of_get_child_by_name(parent, "ports");
2340 	if (node)
2341 		parent = node;
2342 
2343 	for_each_child_of_node(parent, port) {
2344 		u32 port_id = 0;
2345 
2346 		if (of_node_cmp(port->name, "port") != 0)
2347 			continue;
2348 		of_property_read_u32(port, "reg", &port_id);
2349 		if (id == port_id)
2350 			break;
2351 	}
2352 
2353 	of_node_put(node);
2354 
2355 	return port;
2356 }
2357 EXPORT_SYMBOL(of_graph_get_port_by_id);
2358 
2359 /**
2360  * of_graph_get_next_endpoint() - get next endpoint node
2361  * @parent: pointer to the parent device node
2362  * @prev: previous endpoint node, or NULL to get first
2363  *
2364  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2365  * of the passed @prev node is decremented.
2366  */
2367 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2368 					struct device_node *prev)
2369 {
2370 	struct device_node *endpoint;
2371 	struct device_node *port;
2372 
2373 	if (!parent)
2374 		return NULL;
2375 
2376 	/*
2377 	 * Start by locating the port node. If no previous endpoint is specified
2378 	 * search for the first port node, otherwise get the previous endpoint
2379 	 * parent port node.
2380 	 */
2381 	if (!prev) {
2382 		struct device_node *node;
2383 
2384 		node = of_get_child_by_name(parent, "ports");
2385 		if (node)
2386 			parent = node;
2387 
2388 		port = of_get_child_by_name(parent, "port");
2389 		of_node_put(node);
2390 
2391 		if (!port) {
2392 			pr_err("graph: no port node found in %s\n",
2393 			       parent->full_name);
2394 			return NULL;
2395 		}
2396 	} else {
2397 		port = of_get_parent(prev);
2398 		if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2399 			      __func__, prev->full_name))
2400 			return NULL;
2401 	}
2402 
2403 	while (1) {
2404 		/*
2405 		 * Now that we have a port node, get the next endpoint by
2406 		 * getting the next child. If the previous endpoint is NULL this
2407 		 * will return the first child.
2408 		 */
2409 		endpoint = of_get_next_child(port, prev);
2410 		if (endpoint) {
2411 			of_node_put(port);
2412 			return endpoint;
2413 		}
2414 
2415 		/* No more endpoints under this port, try the next one. */
2416 		prev = NULL;
2417 
2418 		do {
2419 			port = of_get_next_child(parent, port);
2420 			if (!port)
2421 				return NULL;
2422 		} while (of_node_cmp(port->name, "port"));
2423 	}
2424 }
2425 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2426 
2427 /**
2428  * of_graph_get_endpoint_by_regs() - get endpoint node of specific identifiers
2429  * @parent: pointer to the parent device node
2430  * @port_reg: identifier (value of reg property) of the parent port node
2431  * @reg: identifier (value of reg property) of the endpoint node
2432  *
2433  * Return: An 'endpoint' node pointer which is identified by reg and at the same
2434  * is the child of a port node identified by port_reg. reg and port_reg are
2435  * ignored when they are -1.
2436  */
2437 struct device_node *of_graph_get_endpoint_by_regs(
2438 	const struct device_node *parent, int port_reg, int reg)
2439 {
2440 	struct of_endpoint endpoint;
2441 	struct device_node *node = NULL;
2442 
2443 	for_each_endpoint_of_node(parent, node) {
2444 		of_graph_parse_endpoint(node, &endpoint);
2445 		if (((port_reg == -1) || (endpoint.port == port_reg)) &&
2446 			((reg == -1) || (endpoint.id == reg)))
2447 			return node;
2448 	}
2449 
2450 	return NULL;
2451 }
2452 EXPORT_SYMBOL(of_graph_get_endpoint_by_regs);
2453 
2454 /**
2455  * of_graph_get_remote_port_parent() - get remote port's parent node
2456  * @node: pointer to a local endpoint device_node
2457  *
2458  * Return: Remote device node associated with remote endpoint node linked
2459  *	   to @node. Use of_node_put() on it when done.
2460  */
2461 struct device_node *of_graph_get_remote_port_parent(
2462 			       const struct device_node *node)
2463 {
2464 	struct device_node *np;
2465 	unsigned int depth;
2466 
2467 	/* Get remote endpoint node. */
2468 	np = of_parse_phandle(node, "remote-endpoint", 0);
2469 
2470 	/* Walk 3 levels up only if there is 'ports' node. */
2471 	for (depth = 3; depth && np; depth--) {
2472 		np = of_get_next_parent(np);
2473 		if (depth == 2 && of_node_cmp(np->name, "ports"))
2474 			break;
2475 	}
2476 	return np;
2477 }
2478 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2479 
2480 /**
2481  * of_graph_get_remote_port() - get remote port node
2482  * @node: pointer to a local endpoint device_node
2483  *
2484  * Return: Remote port node associated with remote endpoint node linked
2485  *	   to @node. Use of_node_put() on it when done.
2486  */
2487 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2488 {
2489 	struct device_node *np;
2490 
2491 	/* Get remote endpoint node. */
2492 	np = of_parse_phandle(node, "remote-endpoint", 0);
2493 	if (!np)
2494 		return NULL;
2495 	return of_get_next_parent(np);
2496 }
2497 EXPORT_SYMBOL(of_graph_get_remote_port);
2498