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