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