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