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