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/console.h>
20#include <linux/ctype.h>
21#include <linux/cpu.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/of_graph.h>
26#include <linux/spinlock.h>
27#include <linux/slab.h>
28#include <linux/string.h>
29#include <linux/proc_fs.h>
30
31#include "of_private.h"
32
33LIST_HEAD(aliases_lookup);
34
35struct device_node *of_root;
36EXPORT_SYMBOL(of_root);
37struct device_node *of_chosen;
38struct device_node *of_aliases;
39struct device_node *of_stdout;
40static const char *of_stdout_options;
41
42struct kset *of_kset;
43
44
45
46
47
48
49
50DEFINE_MUTEX(of_mutex);
51
52
53
54
55DEFINE_RAW_SPINLOCK(devtree_lock);
56
57int of_n_addr_cells(struct device_node *np)
58{
59 u32 cells;
60
61 do {
62 if (np->parent)
63 np = np->parent;
64 if (!of_property_read_u32(np, "#address-cells", &cells))
65 return cells;
66 } while (np->parent);
67
68 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
69}
70EXPORT_SYMBOL(of_n_addr_cells);
71
72int of_n_size_cells(struct device_node *np)
73{
74 u32 cells;
75
76 do {
77 if (np->parent)
78 np = np->parent;
79 if (!of_property_read_u32(np, "#size-cells", &cells))
80 return cells;
81 } while (np->parent);
82
83 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
84}
85EXPORT_SYMBOL(of_n_size_cells);
86
87#ifdef CONFIG_NUMA
88int __weak of_node_to_nid(struct device_node *np)
89{
90 return NUMA_NO_NODE;
91}
92#endif
93
94void __init of_core_init(void)
95{
96 struct device_node *np;
97
98
99 mutex_lock(&of_mutex);
100 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
101 if (!of_kset) {
102 mutex_unlock(&of_mutex);
103 pr_err("failed to register existing nodes\n");
104 return;
105 }
106 for_each_of_allnodes(np)
107 __of_attach_node_sysfs(np);
108 mutex_unlock(&of_mutex);
109
110
111 if (of_root)
112 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
113}
114
115static struct property *__of_find_property(const struct device_node *np,
116 const char *name, int *lenp)
117{
118 struct property *pp;
119
120 if (!np)
121 return NULL;
122
123 for (pp = np->properties; pp; pp = pp->next) {
124 if (of_prop_cmp(pp->name, name) == 0) {
125 if (lenp)
126 *lenp = pp->length;
127 break;
128 }
129 }
130
131 return pp;
132}
133
134struct property *of_find_property(const struct device_node *np,
135 const char *name,
136 int *lenp)
137{
138 struct property *pp;
139 unsigned long flags;
140
141 raw_spin_lock_irqsave(&devtree_lock, flags);
142 pp = __of_find_property(np, name, lenp);
143 raw_spin_unlock_irqrestore(&devtree_lock, flags);
144
145 return pp;
146}
147EXPORT_SYMBOL(of_find_property);
148
149struct device_node *__of_find_all_nodes(struct device_node *prev)
150{
151 struct device_node *np;
152 if (!prev) {
153 np = of_root;
154 } else if (prev->child) {
155 np = prev->child;
156 } else {
157
158 np = prev;
159 while (np->parent && !np->sibling)
160 np = np->parent;
161 np = np->sibling;
162 }
163 return np;
164}
165
166
167
168
169
170
171
172
173
174struct device_node *of_find_all_nodes(struct device_node *prev)
175{
176 struct device_node *np;
177 unsigned long flags;
178
179 raw_spin_lock_irqsave(&devtree_lock, flags);
180 np = __of_find_all_nodes(prev);
181 of_node_get(np);
182 of_node_put(prev);
183 raw_spin_unlock_irqrestore(&devtree_lock, flags);
184 return np;
185}
186EXPORT_SYMBOL(of_find_all_nodes);
187
188
189
190
191
192const void *__of_get_property(const struct device_node *np,
193 const char *name, int *lenp)
194{
195 struct property *pp = __of_find_property(np, name, lenp);
196
197 return pp ? pp->value : NULL;
198}
199
200
201
202
203
204const void *of_get_property(const struct device_node *np, const char *name,
205 int *lenp)
206{
207 struct property *pp = of_find_property(np, name, lenp);
208
209 return pp ? pp->value : NULL;
210}
211EXPORT_SYMBOL(of_get_property);
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
228{
229 return (u32)phys_id == cpu;
230}
231
232
233
234
235
236
237static bool __of_find_n_match_cpu_property(struct device_node *cpun,
238 const char *prop_name, int cpu, unsigned int *thread)
239{
240 const __be32 *cell;
241 int ac, prop_len, tid;
242 u64 hwid;
243
244 ac = of_n_addr_cells(cpun);
245 cell = of_get_property(cpun, prop_name, &prop_len);
246 if (!cell || !ac)
247 return false;
248 prop_len /= sizeof(*cell) * ac;
249 for (tid = 0; tid < prop_len; tid++) {
250 hwid = of_read_number(cell, ac);
251 if (arch_match_cpu_phys_id(cpu, hwid)) {
252 if (thread)
253 *thread = tid;
254 return true;
255 }
256 cell += ac;
257 }
258 return false;
259}
260
261
262
263
264
265
266
267bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
268 int cpu, unsigned int *thread)
269{
270
271
272
273
274 if (IS_ENABLED(CONFIG_PPC) &&
275 __of_find_n_match_cpu_property(cpun,
276 "ibm,ppc-interrupt-server#s",
277 cpu, thread))
278 return true;
279
280 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
281}
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
303{
304 struct device_node *cpun;
305
306 for_each_node_by_type(cpun, "cpu") {
307 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
308 return cpun;
309 }
310 return NULL;
311}
312EXPORT_SYMBOL(of_get_cpu_node);
313
314
315
316
317
318
319
320
321
322int of_cpu_node_to_id(struct device_node *cpu_node)
323{
324 int cpu;
325 bool found = false;
326 struct device_node *np;
327
328 for_each_possible_cpu(cpu) {
329 np = of_cpu_device_node_get(cpu);
330 found = (cpu_node == np);
331 of_node_put(np);
332 if (found)
333 return cpu;
334 }
335
336 return -ENODEV;
337}
338EXPORT_SYMBOL(of_cpu_node_to_id);
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370static int __of_device_is_compatible(const struct device_node *device,
371 const char *compat, const char *type, const char *name)
372{
373 struct property *prop;
374 const char *cp;
375 int index = 0, score = 0;
376
377
378 if (compat && compat[0]) {
379 prop = __of_find_property(device, "compatible", NULL);
380 for (cp = of_prop_next_string(prop, NULL); cp;
381 cp = of_prop_next_string(prop, cp), index++) {
382 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
383 score = INT_MAX/2 - (index << 2);
384 break;
385 }
386 }
387 if (!score)
388 return 0;
389 }
390
391
392 if (type && type[0]) {
393 if (!device->type || of_node_cmp(type, device->type))
394 return 0;
395 score += 2;
396 }
397
398
399 if (name && name[0]) {
400 if (!device->name || of_node_cmp(name, device->name))
401 return 0;
402 score++;
403 }
404
405 return score;
406}
407
408
409
410
411int of_device_is_compatible(const struct device_node *device,
412 const char *compat)
413{
414 unsigned long flags;
415 int res;
416
417 raw_spin_lock_irqsave(&devtree_lock, flags);
418 res = __of_device_is_compatible(device, compat, NULL, NULL);
419 raw_spin_unlock_irqrestore(&devtree_lock, flags);
420 return res;
421}
422EXPORT_SYMBOL(of_device_is_compatible);
423
424
425
426
427
428int of_device_compatible_match(struct device_node *device,
429 const char *const *compat)
430{
431 unsigned int tmp, score = 0;
432
433 if (!compat)
434 return 0;
435
436 while (*compat) {
437 tmp = of_device_is_compatible(device, *compat);
438 if (tmp > score)
439 score = tmp;
440 compat++;
441 }
442
443 return score;
444}
445
446
447
448
449
450
451
452
453int of_machine_is_compatible(const char *compat)
454{
455 struct device_node *root;
456 int rc = 0;
457
458 root = of_find_node_by_path("/");
459 if (root) {
460 rc = of_device_is_compatible(root, compat);
461 of_node_put(root);
462 }
463 return rc;
464}
465EXPORT_SYMBOL(of_machine_is_compatible);
466
467
468
469
470
471
472
473
474
475static bool __of_device_is_available(const struct device_node *device)
476{
477 const char *status;
478 int statlen;
479
480 if (!device)
481 return false;
482
483 status = __of_get_property(device, "status", &statlen);
484 if (status == NULL)
485 return true;
486
487 if (statlen > 0) {
488 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
489 return true;
490 }
491
492 return false;
493}
494
495
496
497
498
499
500
501
502
503bool of_device_is_available(const struct device_node *device)
504{
505 unsigned long flags;
506 bool res;
507
508 raw_spin_lock_irqsave(&devtree_lock, flags);
509 res = __of_device_is_available(device);
510 raw_spin_unlock_irqrestore(&devtree_lock, flags);
511 return res;
512
513}
514EXPORT_SYMBOL(of_device_is_available);
515
516
517
518
519
520
521
522
523
524
525
526
527
528bool of_device_is_big_endian(const struct device_node *device)
529{
530 if (of_property_read_bool(device, "big-endian"))
531 return true;
532 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
533 of_property_read_bool(device, "native-endian"))
534 return true;
535 return false;
536}
537EXPORT_SYMBOL(of_device_is_big_endian);
538
539
540
541
542
543
544
545
546struct device_node *of_get_parent(const struct device_node *node)
547{
548 struct device_node *np;
549 unsigned long flags;
550
551 if (!node)
552 return NULL;
553
554 raw_spin_lock_irqsave(&devtree_lock, flags);
555 np = of_node_get(node->parent);
556 raw_spin_unlock_irqrestore(&devtree_lock, flags);
557 return np;
558}
559EXPORT_SYMBOL(of_get_parent);
560
561
562
563
564
565
566
567
568
569
570
571
572struct device_node *of_get_next_parent(struct device_node *node)
573{
574 struct device_node *parent;
575 unsigned long flags;
576
577 if (!node)
578 return NULL;
579
580 raw_spin_lock_irqsave(&devtree_lock, flags);
581 parent = of_node_get(node->parent);
582 of_node_put(node);
583 raw_spin_unlock_irqrestore(&devtree_lock, flags);
584 return parent;
585}
586EXPORT_SYMBOL(of_get_next_parent);
587
588static struct device_node *__of_get_next_child(const struct device_node *node,
589 struct device_node *prev)
590{
591 struct device_node *next;
592
593 if (!node)
594 return NULL;
595
596 next = prev ? prev->sibling : node->child;
597 for (; next; next = next->sibling)
598 if (of_node_get(next))
599 break;
600 of_node_put(prev);
601 return next;
602}
603#define __for_each_child_of_node(parent, child) \
604 for (child = __of_get_next_child(parent, NULL); child != NULL; \
605 child = __of_get_next_child(parent, child))
606
607
608
609
610
611
612
613
614
615
616struct device_node *of_get_next_child(const struct device_node *node,
617 struct device_node *prev)
618{
619 struct device_node *next;
620 unsigned long flags;
621
622 raw_spin_lock_irqsave(&devtree_lock, flags);
623 next = __of_get_next_child(node, prev);
624 raw_spin_unlock_irqrestore(&devtree_lock, flags);
625 return next;
626}
627EXPORT_SYMBOL(of_get_next_child);
628
629
630
631
632
633
634
635
636
637struct device_node *of_get_next_available_child(const struct device_node *node,
638 struct device_node *prev)
639{
640 struct device_node *next;
641 unsigned long flags;
642
643 if (!node)
644 return NULL;
645
646 raw_spin_lock_irqsave(&devtree_lock, flags);
647 next = prev ? prev->sibling : node->child;
648 for (; next; next = next->sibling) {
649 if (!__of_device_is_available(next))
650 continue;
651 if (of_node_get(next))
652 break;
653 }
654 of_node_put(prev);
655 raw_spin_unlock_irqrestore(&devtree_lock, flags);
656 return next;
657}
658EXPORT_SYMBOL(of_get_next_available_child);
659
660
661
662
663
664
665
666
667
668
669
670
671struct device_node *of_get_child_by_name(const struct device_node *node,
672 const char *name)
673{
674 struct device_node *child;
675
676 for_each_child_of_node(node, child)
677 if (child->name && (of_node_cmp(child->name, name) == 0))
678 break;
679 return child;
680}
681EXPORT_SYMBOL(of_get_child_by_name);
682
683struct device_node *__of_find_node_by_path(struct device_node *parent,
684 const char *path)
685{
686 struct device_node *child;
687 int len;
688
689 len = strcspn(path, "/:");
690 if (!len)
691 return NULL;
692
693 __for_each_child_of_node(parent, child) {
694 const char *name = kbasename(child->full_name);
695 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
696 return child;
697 }
698 return NULL;
699}
700
701struct device_node *__of_find_node_by_full_path(struct device_node *node,
702 const char *path)
703{
704 const char *separator = strchr(path, ':');
705
706 while (node && *path == '/') {
707 struct device_node *tmp = node;
708
709 path++;
710 node = __of_find_node_by_path(node, path);
711 of_node_put(tmp);
712 path = strchrnul(path, '/');
713 if (separator && separator < path)
714 break;
715 }
716 return node;
717}
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
738{
739 struct device_node *np = NULL;
740 struct property *pp;
741 unsigned long flags;
742 const char *separator = strchr(path, ':');
743
744 if (opts)
745 *opts = separator ? separator + 1 : NULL;
746
747 if (strcmp(path, "/") == 0)
748 return of_node_get(of_root);
749
750
751 if (*path != '/') {
752 int len;
753 const char *p = separator;
754
755 if (!p)
756 p = strchrnul(path, '/');
757 len = p - path;
758
759
760 if (!of_aliases)
761 return NULL;
762
763 for_each_property_of_node(of_aliases, pp) {
764 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
765 np = of_find_node_by_path(pp->value);
766 break;
767 }
768 }
769 if (!np)
770 return NULL;
771 path = p;
772 }
773
774
775 raw_spin_lock_irqsave(&devtree_lock, flags);
776 if (!np)
777 np = of_node_get(of_root);
778 np = __of_find_node_by_full_path(np, path);
779 raw_spin_unlock_irqrestore(&devtree_lock, flags);
780 return np;
781}
782EXPORT_SYMBOL(of_find_node_opts_by_path);
783
784
785
786
787
788
789
790
791
792
793
794
795struct device_node *of_find_node_by_name(struct device_node *from,
796 const char *name)
797{
798 struct device_node *np;
799 unsigned long flags;
800
801 raw_spin_lock_irqsave(&devtree_lock, flags);
802 for_each_of_allnodes_from(from, np)
803 if (np->name && (of_node_cmp(np->name, name) == 0)
804 && of_node_get(np))
805 break;
806 of_node_put(from);
807 raw_spin_unlock_irqrestore(&devtree_lock, flags);
808 return np;
809}
810EXPORT_SYMBOL(of_find_node_by_name);
811
812
813
814
815
816
817
818
819
820
821
822
823
824struct device_node *of_find_node_by_type(struct device_node *from,
825 const char *type)
826{
827 struct device_node *np;
828 unsigned long flags;
829
830 raw_spin_lock_irqsave(&devtree_lock, flags);
831 for_each_of_allnodes_from(from, np)
832 if (np->type && (of_node_cmp(np->type, type) == 0)
833 && of_node_get(np))
834 break;
835 of_node_put(from);
836 raw_spin_unlock_irqrestore(&devtree_lock, flags);
837 return np;
838}
839EXPORT_SYMBOL(of_find_node_by_type);
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855struct device_node *of_find_compatible_node(struct device_node *from,
856 const char *type, const char *compatible)
857{
858 struct device_node *np;
859 unsigned long flags;
860
861 raw_spin_lock_irqsave(&devtree_lock, flags);
862 for_each_of_allnodes_from(from, np)
863 if (__of_device_is_compatible(np, compatible, type, NULL) &&
864 of_node_get(np))
865 break;
866 of_node_put(from);
867 raw_spin_unlock_irqrestore(&devtree_lock, flags);
868 return np;
869}
870EXPORT_SYMBOL(of_find_compatible_node);
871
872
873
874
875
876
877
878
879
880
881
882
883
884struct device_node *of_find_node_with_property(struct device_node *from,
885 const char *prop_name)
886{
887 struct device_node *np;
888 struct property *pp;
889 unsigned long flags;
890
891 raw_spin_lock_irqsave(&devtree_lock, flags);
892 for_each_of_allnodes_from(from, np) {
893 for (pp = np->properties; pp; pp = pp->next) {
894 if (of_prop_cmp(pp->name, prop_name) == 0) {
895 of_node_get(np);
896 goto out;
897 }
898 }
899 }
900out:
901 of_node_put(from);
902 raw_spin_unlock_irqrestore(&devtree_lock, flags);
903 return np;
904}
905EXPORT_SYMBOL(of_find_node_with_property);
906
907static
908const struct of_device_id *__of_match_node(const struct of_device_id *matches,
909 const struct device_node *node)
910{
911 const struct of_device_id *best_match = NULL;
912 int score, best_score = 0;
913
914 if (!matches)
915 return NULL;
916
917 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
918 score = __of_device_is_compatible(node, matches->compatible,
919 matches->type, matches->name);
920 if (score > best_score) {
921 best_match = matches;
922 best_score = score;
923 }
924 }
925
926 return best_match;
927}
928
929
930
931
932
933
934
935
936const struct of_device_id *of_match_node(const struct of_device_id *matches,
937 const struct device_node *node)
938{
939 const struct of_device_id *match;
940 unsigned long flags;
941
942 raw_spin_lock_irqsave(&devtree_lock, flags);
943 match = __of_match_node(matches, node);
944 raw_spin_unlock_irqrestore(&devtree_lock, flags);
945 return match;
946}
947EXPORT_SYMBOL(of_match_node);
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962struct device_node *of_find_matching_node_and_match(struct device_node *from,
963 const struct of_device_id *matches,
964 const struct of_device_id **match)
965{
966 struct device_node *np;
967 const struct of_device_id *m;
968 unsigned long flags;
969
970 if (match)
971 *match = NULL;
972
973 raw_spin_lock_irqsave(&devtree_lock, flags);
974 for_each_of_allnodes_from(from, np) {
975 m = __of_match_node(matches, np);
976 if (m && of_node_get(np)) {
977 if (match)
978 *match = m;
979 break;
980 }
981 }
982 of_node_put(from);
983 raw_spin_unlock_irqrestore(&devtree_lock, flags);
984 return np;
985}
986EXPORT_SYMBOL(of_find_matching_node_and_match);
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001int of_modalias_node(struct device_node *node, char *modalias, int len)
1002{
1003 const char *compatible, *p;
1004 int cplen;
1005
1006 compatible = of_get_property(node, "compatible", &cplen);
1007 if (!compatible || strlen(compatible) > cplen)
1008 return -ENODEV;
1009 p = strchr(compatible, ',');
1010 strlcpy(modalias, p ? p + 1 : compatible, len);
1011 return 0;
1012}
1013EXPORT_SYMBOL_GPL(of_modalias_node);
1014
1015
1016
1017
1018
1019
1020
1021
1022struct device_node *of_find_node_by_phandle(phandle handle)
1023{
1024 struct device_node *np;
1025 unsigned long flags;
1026
1027 if (!handle)
1028 return NULL;
1029
1030 raw_spin_lock_irqsave(&devtree_lock, flags);
1031 for_each_of_allnodes(np)
1032 if (np->phandle == handle)
1033 break;
1034 of_node_get(np);
1035 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1036 return np;
1037}
1038EXPORT_SYMBOL(of_find_node_by_phandle);
1039
1040void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1041{
1042 int i;
1043 printk("%s %pOF", msg, args->np);
1044 for (i = 0; i < args->args_count; i++) {
1045 const char delim = i ? ',' : ':';
1046
1047 pr_cont("%c%08x", delim, args->args[i]);
1048 }
1049 pr_cont("\n");
1050}
1051
1052int of_phandle_iterator_init(struct of_phandle_iterator *it,
1053 const struct device_node *np,
1054 const char *list_name,
1055 const char *cells_name,
1056 int cell_count)
1057{
1058 const __be32 *list;
1059 int size;
1060
1061 memset(it, 0, sizeof(*it));
1062
1063 list = of_get_property(np, list_name, &size);
1064 if (!list)
1065 return -ENOENT;
1066
1067 it->cells_name = cells_name;
1068 it->cell_count = cell_count;
1069 it->parent = np;
1070 it->list_end = list + size / sizeof(*list);
1071 it->phandle_end = list;
1072 it->cur = list;
1073
1074 return 0;
1075}
1076EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
1077
1078int of_phandle_iterator_next(struct of_phandle_iterator *it)
1079{
1080 uint32_t count = 0;
1081
1082 if (it->node) {
1083 of_node_put(it->node);
1084 it->node = NULL;
1085 }
1086
1087 if (!it->cur || it->phandle_end >= it->list_end)
1088 return -ENOENT;
1089
1090 it->cur = it->phandle_end;
1091
1092
1093 it->phandle = be32_to_cpup(it->cur++);
1094
1095 if (it->phandle) {
1096
1097
1098
1099
1100
1101 it->node = of_find_node_by_phandle(it->phandle);
1102
1103 if (it->cells_name) {
1104 if (!it->node) {
1105 pr_err("%pOF: could not find phandle\n",
1106 it->parent);
1107 goto err;
1108 }
1109
1110 if (of_property_read_u32(it->node, it->cells_name,
1111 &count)) {
1112 pr_err("%pOF: could not get %s for %pOF\n",
1113 it->parent,
1114 it->cells_name,
1115 it->node);
1116 goto err;
1117 }
1118 } else {
1119 count = it->cell_count;
1120 }
1121
1122
1123
1124
1125
1126 if (it->cur + count > it->list_end) {
1127 pr_err("%pOF: arguments longer than property\n",
1128 it->parent);
1129 goto err;
1130 }
1131 }
1132
1133 it->phandle_end = it->cur + count;
1134 it->cur_count = count;
1135
1136 return 0;
1137
1138err:
1139 if (it->node) {
1140 of_node_put(it->node);
1141 it->node = NULL;
1142 }
1143
1144 return -EINVAL;
1145}
1146EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
1147
1148int of_phandle_iterator_args(struct of_phandle_iterator *it,
1149 uint32_t *args,
1150 int size)
1151{
1152 int i, count;
1153
1154 count = it->cur_count;
1155
1156 if (WARN_ON(size < count))
1157 count = size;
1158
1159 for (i = 0; i < count; i++)
1160 args[i] = be32_to_cpup(it->cur++);
1161
1162 return count;
1163}
1164
1165static int __of_parse_phandle_with_args(const struct device_node *np,
1166 const char *list_name,
1167 const char *cells_name,
1168 int cell_count, int index,
1169 struct of_phandle_args *out_args)
1170{
1171 struct of_phandle_iterator it;
1172 int rc, cur_index = 0;
1173
1174
1175 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
1176
1177
1178
1179
1180
1181
1182 rc = -ENOENT;
1183 if (cur_index == index) {
1184 if (!it.phandle)
1185 goto err;
1186
1187 if (out_args) {
1188 int c;
1189
1190 c = of_phandle_iterator_args(&it,
1191 out_args->args,
1192 MAX_PHANDLE_ARGS);
1193 out_args->np = it.node;
1194 out_args->args_count = c;
1195 } else {
1196 of_node_put(it.node);
1197 }
1198
1199
1200 return 0;
1201 }
1202
1203 cur_index++;
1204 }
1205
1206
1207
1208
1209
1210
1211
1212 err:
1213 of_node_put(it.node);
1214 return rc;
1215}
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227struct device_node *of_parse_phandle(const struct device_node *np,
1228 const char *phandle_name, int index)
1229{
1230 struct of_phandle_args args;
1231
1232 if (index < 0)
1233 return NULL;
1234
1235 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1236 index, &args))
1237 return NULL;
1238
1239 return args.np;
1240}
1241EXPORT_SYMBOL(of_parse_phandle);
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1276 const char *cells_name, int index,
1277 struct of_phandle_args *out_args)
1278{
1279 if (index < 0)
1280 return -EINVAL;
1281 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1282 index, out_args);
1283}
1284EXPORT_SYMBOL(of_parse_phandle_with_args);
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316int of_parse_phandle_with_fixed_args(const struct device_node *np,
1317 const char *list_name, int cell_count,
1318 int index, struct of_phandle_args *out_args)
1319{
1320 if (index < 0)
1321 return -EINVAL;
1322 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1323 index, out_args);
1324}
1325EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1343 const char *cells_name)
1344{
1345 struct of_phandle_iterator it;
1346 int rc, cur_index = 0;
1347
1348 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1349 if (rc)
1350 return rc;
1351
1352 while ((rc = of_phandle_iterator_next(&it)) == 0)
1353 cur_index += 1;
1354
1355 if (rc != -ENOENT)
1356 return rc;
1357
1358 return cur_index;
1359}
1360EXPORT_SYMBOL(of_count_phandle_with_args);
1361
1362
1363
1364
1365int __of_add_property(struct device_node *np, struct property *prop)
1366{
1367 struct property **next;
1368
1369 prop->next = NULL;
1370 next = &np->properties;
1371 while (*next) {
1372 if (strcmp(prop->name, (*next)->name) == 0)
1373
1374 return -EEXIST;
1375
1376 next = &(*next)->next;
1377 }
1378 *next = prop;
1379
1380 return 0;
1381}
1382
1383
1384
1385
1386int of_add_property(struct device_node *np, struct property *prop)
1387{
1388 unsigned long flags;
1389 int rc;
1390
1391 mutex_lock(&of_mutex);
1392
1393 raw_spin_lock_irqsave(&devtree_lock, flags);
1394 rc = __of_add_property(np, prop);
1395 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1396
1397 if (!rc)
1398 __of_add_property_sysfs(np, prop);
1399
1400 mutex_unlock(&of_mutex);
1401
1402 if (!rc)
1403 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1404
1405 return rc;
1406}
1407
1408int __of_remove_property(struct device_node *np, struct property *prop)
1409{
1410 struct property **next;
1411
1412 for (next = &np->properties; *next; next = &(*next)->next) {
1413 if (*next == prop)
1414 break;
1415 }
1416 if (*next == NULL)
1417 return -ENODEV;
1418
1419
1420 *next = prop->next;
1421 prop->next = np->deadprops;
1422 np->deadprops = prop;
1423
1424 return 0;
1425}
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435int of_remove_property(struct device_node *np, struct property *prop)
1436{
1437 unsigned long flags;
1438 int rc;
1439
1440 if (!prop)
1441 return -ENODEV;
1442
1443 mutex_lock(&of_mutex);
1444
1445 raw_spin_lock_irqsave(&devtree_lock, flags);
1446 rc = __of_remove_property(np, prop);
1447 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1448
1449 if (!rc)
1450 __of_remove_property_sysfs(np, prop);
1451
1452 mutex_unlock(&of_mutex);
1453
1454 if (!rc)
1455 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1456
1457 return rc;
1458}
1459
1460int __of_update_property(struct device_node *np, struct property *newprop,
1461 struct property **oldpropp)
1462{
1463 struct property **next, *oldprop;
1464
1465 for (next = &np->properties; *next; next = &(*next)->next) {
1466 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1467 break;
1468 }
1469 *oldpropp = oldprop = *next;
1470
1471 if (oldprop) {
1472
1473 newprop->next = oldprop->next;
1474 *next = newprop;
1475 oldprop->next = np->deadprops;
1476 np->deadprops = oldprop;
1477 } else {
1478
1479 newprop->next = NULL;
1480 *next = newprop;
1481 }
1482
1483 return 0;
1484}
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495int of_update_property(struct device_node *np, struct property *newprop)
1496{
1497 struct property *oldprop;
1498 unsigned long flags;
1499 int rc;
1500
1501 if (!newprop->name)
1502 return -EINVAL;
1503
1504 mutex_lock(&of_mutex);
1505
1506 raw_spin_lock_irqsave(&devtree_lock, flags);
1507 rc = __of_update_property(np, newprop, &oldprop);
1508 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1509
1510 if (!rc)
1511 __of_update_property_sysfs(np, newprop, oldprop);
1512
1513 mutex_unlock(&of_mutex);
1514
1515 if (!rc)
1516 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1517
1518 return rc;
1519}
1520
1521static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1522 int id, const char *stem, int stem_len)
1523{
1524 ap->np = np;
1525 ap->id = id;
1526 strncpy(ap->stem, stem, stem_len);
1527 ap->stem[stem_len] = 0;
1528 list_add_tail(&ap->link, &aliases_lookup);
1529 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1530 ap->alias, ap->stem, ap->id, np);
1531}
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1544{
1545 struct property *pp;
1546
1547 of_aliases = of_find_node_by_path("/aliases");
1548 of_chosen = of_find_node_by_path("/chosen");
1549 if (of_chosen == NULL)
1550 of_chosen = of_find_node_by_path("/chosen@0");
1551
1552 if (of_chosen) {
1553
1554 const char *name = NULL;
1555
1556 if (of_property_read_string(of_chosen, "stdout-path", &name))
1557 of_property_read_string(of_chosen, "linux,stdout-path",
1558 &name);
1559 if (IS_ENABLED(CONFIG_PPC) && !name)
1560 of_property_read_string(of_aliases, "stdout", &name);
1561 if (name)
1562 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
1563 }
1564
1565 if (!of_aliases)
1566 return;
1567
1568 for_each_property_of_node(of_aliases, pp) {
1569 const char *start = pp->name;
1570 const char *end = start + strlen(start);
1571 struct device_node *np;
1572 struct alias_prop *ap;
1573 int id, len;
1574
1575
1576 if (!strcmp(pp->name, "name") ||
1577 !strcmp(pp->name, "phandle") ||
1578 !strcmp(pp->name, "linux,phandle"))
1579 continue;
1580
1581 np = of_find_node_by_path(pp->value);
1582 if (!np)
1583 continue;
1584
1585
1586
1587 while (isdigit(*(end-1)) && end > start)
1588 end--;
1589 len = end - start;
1590
1591 if (kstrtoint(end, 10, &id) < 0)
1592 continue;
1593
1594
1595 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
1596 if (!ap)
1597 continue;
1598 memset(ap, 0, sizeof(*ap) + len + 1);
1599 ap->alias = start;
1600 of_alias_add(ap, np, id, start, len);
1601 }
1602}
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612int of_alias_get_id(struct device_node *np, const char *stem)
1613{
1614 struct alias_prop *app;
1615 int id = -ENODEV;
1616
1617 mutex_lock(&of_mutex);
1618 list_for_each_entry(app, &aliases_lookup, link) {
1619 if (strcmp(app->stem, stem) != 0)
1620 continue;
1621
1622 if (np == app->np) {
1623 id = app->id;
1624 break;
1625 }
1626 }
1627 mutex_unlock(&of_mutex);
1628
1629 return id;
1630}
1631EXPORT_SYMBOL_GPL(of_alias_get_id);
1632
1633
1634
1635
1636
1637
1638
1639
1640int of_alias_get_highest_id(const char *stem)
1641{
1642 struct alias_prop *app;
1643 int id = -ENODEV;
1644
1645 mutex_lock(&of_mutex);
1646 list_for_each_entry(app, &aliases_lookup, link) {
1647 if (strcmp(app->stem, stem) != 0)
1648 continue;
1649
1650 if (app->id > id)
1651 id = app->id;
1652 }
1653 mutex_unlock(&of_mutex);
1654
1655 return id;
1656}
1657EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669bool of_console_check(struct device_node *dn, char *name, int index)
1670{
1671 if (!dn || dn != of_stdout || console_set_on_cmdline)
1672 return false;
1673
1674
1675
1676
1677
1678 return !add_preferred_console(name, index, (char *)of_stdout_options);
1679}
1680EXPORT_SYMBOL_GPL(of_console_check);
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690struct device_node *of_find_next_cache_node(const struct device_node *np)
1691{
1692 struct device_node *child, *cache_node;
1693
1694 cache_node = of_parse_phandle(np, "l2-cache", 0);
1695 if (!cache_node)
1696 cache_node = of_parse_phandle(np, "next-level-cache", 0);
1697
1698 if (cache_node)
1699 return cache_node;
1700
1701
1702
1703
1704 if (!strcmp(np->type, "cpu"))
1705 for_each_child_of_node(np, child)
1706 if (!strcmp(child->type, "cache"))
1707 return child;
1708
1709 return NULL;
1710}
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721int of_find_last_cache_level(unsigned int cpu)
1722{
1723 u32 cache_level = 0;
1724 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
1725
1726 while (np) {
1727 prev = np;
1728 of_node_put(np);
1729 np = of_find_next_cache_node(np);
1730 }
1731
1732 of_property_read_u32(prev, "cache-level", &cache_level);
1733
1734 return cache_level;
1735}
1736