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