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