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