1
2
3
4
5
6
7
8
9#define pr_fmt(fmt) "OF: fdt: " fmt
10
11#include <linux/crc32.h>
12#include <linux/kernel.h>
13#include <linux/initrd.h>
14#include <linux/memblock.h>
15#include <linux/mutex.h>
16#include <linux/of.h>
17#include <linux/of_fdt.h>
18#include <linux/of_reserved_mem.h>
19#include <linux/sizes.h>
20#include <linux/string.h>
21#include <linux/errno.h>
22#include <linux/slab.h>
23#include <linux/libfdt.h>
24#include <linux/debugfs.h>
25#include <linux/serial_core.h>
26#include <linux/sysfs.h>
27
28#include <asm/setup.h>
29#include <asm/page.h>
30
31#include "of_private.h"
32
33
34
35
36
37
38
39
40
41void of_fdt_limit_memory(int limit)
42{
43 int memory;
44 int len;
45 const void *val;
46 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
47 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
48 const __be32 *addr_prop;
49 const __be32 *size_prop;
50 int root_offset;
51 int cell_size;
52
53 root_offset = fdt_path_offset(initial_boot_params, "/");
54 if (root_offset < 0)
55 return;
56
57 addr_prop = fdt_getprop(initial_boot_params, root_offset,
58 "#address-cells", NULL);
59 if (addr_prop)
60 nr_address_cells = fdt32_to_cpu(*addr_prop);
61
62 size_prop = fdt_getprop(initial_boot_params, root_offset,
63 "#size-cells", NULL);
64 if (size_prop)
65 nr_size_cells = fdt32_to_cpu(*size_prop);
66
67 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
68
69 memory = fdt_path_offset(initial_boot_params, "/memory");
70 if (memory > 0) {
71 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
72 if (len > limit*cell_size) {
73 len = limit*cell_size;
74 pr_debug("Limiting number of entries to %d\n", limit);
75 fdt_setprop(initial_boot_params, memory, "reg", val,
76 len);
77 }
78 }
79}
80
81
82
83
84
85
86
87
88
89
90
91static int of_fdt_is_compatible(const void *blob,
92 unsigned long node, const char *compat)
93{
94 const char *cp;
95 int cplen;
96 unsigned long l, score = 0;
97
98 cp = fdt_getprop(blob, node, "compatible", &cplen);
99 if (cp == NULL)
100 return 0;
101 while (cplen > 0) {
102 score++;
103 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
104 return score;
105 l = strlen(cp) + 1;
106 cp += l;
107 cplen -= l;
108 }
109
110 return 0;
111}
112
113
114
115
116
117
118
119
120
121
122bool of_fdt_is_big_endian(const void *blob, unsigned long node)
123{
124 if (fdt_getprop(blob, node, "big-endian", NULL))
125 return true;
126 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
127 fdt_getprop(blob, node, "native-endian", NULL))
128 return true;
129 return false;
130}
131
132static bool of_fdt_device_is_available(const void *blob, unsigned long node)
133{
134 const char *status = fdt_getprop(blob, node, "status", NULL);
135
136 if (!status)
137 return true;
138
139 if (!strcmp(status, "ok") || !strcmp(status, "okay"))
140 return true;
141
142 return false;
143}
144
145
146
147
148int of_fdt_match(const void *blob, unsigned long node,
149 const char *const *compat)
150{
151 unsigned int tmp, score = 0;
152
153 if (!compat)
154 return 0;
155
156 while (*compat) {
157 tmp = of_fdt_is_compatible(blob, node, *compat);
158 if (tmp && (score == 0 || (tmp < score)))
159 score = tmp;
160 compat++;
161 }
162
163 return score;
164}
165
166static void *unflatten_dt_alloc(void **mem, unsigned long size,
167 unsigned long align)
168{
169 void *res;
170
171 *mem = PTR_ALIGN(*mem, align);
172 res = *mem;
173 *mem += size;
174
175 return res;
176}
177
178static void populate_properties(const void *blob,
179 int offset,
180 void **mem,
181 struct device_node *np,
182 const char *nodename,
183 bool dryrun)
184{
185 struct property *pp, **pprev = NULL;
186 int cur;
187 bool has_name = false;
188
189 pprev = &np->properties;
190 for (cur = fdt_first_property_offset(blob, offset);
191 cur >= 0;
192 cur = fdt_next_property_offset(blob, cur)) {
193 const __be32 *val;
194 const char *pname;
195 u32 sz;
196
197 val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
198 if (!val) {
199 pr_warn("Cannot locate property at 0x%x\n", cur);
200 continue;
201 }
202
203 if (!pname) {
204 pr_warn("Cannot find property name at 0x%x\n", cur);
205 continue;
206 }
207
208 if (!strcmp(pname, "name"))
209 has_name = true;
210
211 pp = unflatten_dt_alloc(mem, sizeof(struct property),
212 __alignof__(struct property));
213 if (dryrun)
214 continue;
215
216
217
218
219
220
221
222 if (!strcmp(pname, "phandle") ||
223 !strcmp(pname, "linux,phandle")) {
224 if (!np->phandle)
225 np->phandle = be32_to_cpup(val);
226 }
227
228
229
230
231
232 if (!strcmp(pname, "ibm,phandle"))
233 np->phandle = be32_to_cpup(val);
234
235 pp->name = (char *)pname;
236 pp->length = sz;
237 pp->value = (__be32 *)val;
238 *pprev = pp;
239 pprev = &pp->next;
240 }
241
242
243
244
245 if (!has_name) {
246 const char *p = nodename, *ps = p, *pa = NULL;
247 int len;
248
249 while (*p) {
250 if ((*p) == '@')
251 pa = p;
252 else if ((*p) == '/')
253 ps = p + 1;
254 p++;
255 }
256
257 if (pa < ps)
258 pa = p;
259 len = (pa - ps) + 1;
260 pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
261 __alignof__(struct property));
262 if (!dryrun) {
263 pp->name = "name";
264 pp->length = len;
265 pp->value = pp + 1;
266 *pprev = pp;
267 pprev = &pp->next;
268 memcpy(pp->value, ps, len - 1);
269 ((char *)pp->value)[len - 1] = 0;
270 pr_debug("fixed up name for %s -> %s\n",
271 nodename, (char *)pp->value);
272 }
273 }
274
275 if (!dryrun)
276 *pprev = NULL;
277}
278
279static bool populate_node(const void *blob,
280 int offset,
281 void **mem,
282 struct device_node *dad,
283 struct device_node **pnp,
284 bool dryrun)
285{
286 struct device_node *np;
287 const char *pathp;
288 unsigned int l, allocl;
289
290 pathp = fdt_get_name(blob, offset, &l);
291 if (!pathp) {
292 *pnp = NULL;
293 return false;
294 }
295
296 allocl = ++l;
297
298 np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
299 __alignof__(struct device_node));
300 if (!dryrun) {
301 char *fn;
302 of_node_init(np);
303 np->full_name = fn = ((char *)np) + sizeof(*np);
304
305 memcpy(fn, pathp, l);
306
307 if (dad != NULL) {
308 np->parent = dad;
309 np->sibling = dad->child;
310 dad->child = np;
311 }
312 }
313
314 populate_properties(blob, offset, mem, np, pathp, dryrun);
315 if (!dryrun) {
316 np->name = of_get_property(np, "name", NULL);
317 np->type = of_get_property(np, "device_type", NULL);
318
319 if (!np->name)
320 np->name = "<NULL>";
321 if (!np->type)
322 np->type = "<NULL>";
323 }
324
325 *pnp = np;
326 return true;
327}
328
329static void reverse_nodes(struct device_node *parent)
330{
331 struct device_node *child, *next;
332
333
334 child = parent->child;
335 while (child) {
336 reverse_nodes(child);
337
338 child = child->sibling;
339 }
340
341
342 child = parent->child;
343 parent->child = NULL;
344 while (child) {
345 next = child->sibling;
346
347 child->sibling = parent->child;
348 parent->child = child;
349 child = next;
350 }
351}
352
353
354
355
356
357
358
359
360
361
362static int unflatten_dt_nodes(const void *blob,
363 void *mem,
364 struct device_node *dad,
365 struct device_node **nodepp)
366{
367 struct device_node *root;
368 int offset = 0, depth = 0, initial_depth = 0;
369#define FDT_MAX_DEPTH 64
370 struct device_node *nps[FDT_MAX_DEPTH];
371 void *base = mem;
372 bool dryrun = !base;
373
374 if (nodepp)
375 *nodepp = NULL;
376
377
378
379
380
381
382
383
384 if (dad)
385 depth = initial_depth = 1;
386
387 root = dad;
388 nps[depth] = dad;
389
390 for (offset = 0;
391 offset >= 0 && depth >= initial_depth;
392 offset = fdt_next_node(blob, offset, &depth)) {
393 if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
394 continue;
395
396 if (!IS_ENABLED(CONFIG_OF_KOBJ) &&
397 !of_fdt_device_is_available(blob, offset))
398 continue;
399
400 if (!populate_node(blob, offset, &mem, nps[depth],
401 &nps[depth+1], dryrun))
402 return mem - base;
403
404 if (!dryrun && nodepp && !*nodepp)
405 *nodepp = nps[depth+1];
406 if (!dryrun && !root)
407 root = nps[depth+1];
408 }
409
410 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
411 pr_err("Error %d processing FDT\n", offset);
412 return -EINVAL;
413 }
414
415
416
417
418
419 if (!dryrun)
420 reverse_nodes(root);
421
422 return mem - base;
423}
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442void *__unflatten_device_tree(const void *blob,
443 struct device_node *dad,
444 struct device_node **mynodes,
445 void *(*dt_alloc)(u64 size, u64 align),
446 bool detached)
447{
448 int size;
449 void *mem;
450
451 pr_debug(" -> unflatten_device_tree()\n");
452
453 if (!blob) {
454 pr_debug("No device tree pointer\n");
455 return NULL;
456 }
457
458 pr_debug("Unflattening device tree:\n");
459 pr_debug("magic: %08x\n", fdt_magic(blob));
460 pr_debug("size: %08x\n", fdt_totalsize(blob));
461 pr_debug("version: %08x\n", fdt_version(blob));
462
463 if (fdt_check_header(blob)) {
464 pr_err("Invalid device tree blob header\n");
465 return NULL;
466 }
467
468
469 size = unflatten_dt_nodes(blob, NULL, dad, NULL);
470 if (size < 0)
471 return NULL;
472
473 size = ALIGN(size, 4);
474 pr_debug(" size is %d, allocating...\n", size);
475
476
477 mem = dt_alloc(size + 4, __alignof__(struct device_node));
478 if (!mem)
479 return NULL;
480
481 memset(mem, 0, size);
482
483 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
484
485 pr_debug(" unflattening %p...\n", mem);
486
487
488 unflatten_dt_nodes(blob, mem, dad, mynodes);
489 if (be32_to_cpup(mem + size) != 0xdeadbeef)
490 pr_warning("End of tree marker overwritten: %08x\n",
491 be32_to_cpup(mem + size));
492
493 if (detached && mynodes) {
494 of_node_set_flag(*mynodes, OF_DETACHED);
495 pr_debug("unflattened tree is detached\n");
496 }
497
498 pr_debug(" <- unflatten_device_tree()\n");
499 return mem;
500}
501
502static void *kernel_tree_alloc(u64 size, u64 align)
503{
504 return kzalloc(size, GFP_KERNEL);
505}
506
507static DEFINE_MUTEX(of_fdt_unflatten_mutex);
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523void *of_fdt_unflatten_tree(const unsigned long *blob,
524 struct device_node *dad,
525 struct device_node **mynodes)
526{
527 void *mem;
528
529 mutex_lock(&of_fdt_unflatten_mutex);
530 mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
531 true);
532 mutex_unlock(&of_fdt_unflatten_mutex);
533
534 return mem;
535}
536EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
537
538
539int __initdata dt_root_addr_cells;
540int __initdata dt_root_size_cells;
541
542void *initial_boot_params;
543
544#ifdef CONFIG_OF_EARLY_FLATTREE
545
546static u32 of_fdt_crc32;
547
548
549
550
551static int __init __reserved_mem_reserve_reg(unsigned long node,
552 const char *uname)
553{
554 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
555 phys_addr_t base, size;
556 int len;
557 const __be32 *prop;
558 int nomap, first = 1;
559
560 prop = of_get_flat_dt_prop(node, "reg", &len);
561 if (!prop)
562 return -ENOENT;
563
564 if (len && len % t_len != 0) {
565 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
566 uname);
567 return -EINVAL;
568 }
569
570 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
571
572 while (len >= t_len) {
573 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
574 size = dt_mem_next_cell(dt_root_size_cells, &prop);
575
576 if (size &&
577 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
578 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
579 uname, &base, (unsigned long)size / SZ_1M);
580 else
581 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
582 uname, &base, (unsigned long)size / SZ_1M);
583
584 len -= t_len;
585 if (first) {
586 fdt_reserved_mem_save_node(node, uname, base, size);
587 first = 0;
588 }
589 }
590 return 0;
591}
592
593
594
595
596
597
598static int __init __reserved_mem_check_root(unsigned long node)
599{
600 const __be32 *prop;
601
602 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
603 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
604 return -EINVAL;
605
606 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
607 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
608 return -EINVAL;
609
610 prop = of_get_flat_dt_prop(node, "ranges", NULL);
611 if (!prop)
612 return -EINVAL;
613 return 0;
614}
615
616
617
618
619static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
620 int depth, void *data)
621{
622 static int found;
623 int err;
624
625 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
626 if (__reserved_mem_check_root(node) != 0) {
627 pr_err("Reserved memory: unsupported node format, ignoring\n");
628
629 return 1;
630 }
631 found = 1;
632
633 return 0;
634 } else if (!found) {
635
636 return 0;
637 } else if (found && depth < 2) {
638
639 return 1;
640 }
641
642 if (!of_fdt_device_is_available(initial_boot_params, node))
643 return 0;
644
645 err = __reserved_mem_reserve_reg(node, uname);
646 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
647 fdt_reserved_mem_save_node(node, uname, 0, 0);
648
649
650 return 0;
651}
652
653
654
655
656
657
658
659
660void __init early_init_fdt_scan_reserved_mem(void)
661{
662 int n;
663 u64 base, size;
664
665 if (!initial_boot_params)
666 return;
667
668
669 for (n = 0; ; n++) {
670 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
671 if (!size)
672 break;
673 early_init_dt_reserve_memory_arch(base, size, 0);
674 }
675
676 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
677 fdt_init_reserved_mem();
678}
679
680
681
682
683void __init early_init_fdt_reserve_self(void)
684{
685 if (!initial_boot_params)
686 return;
687
688
689 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
690 fdt_totalsize(initial_boot_params),
691 0);
692}
693
694
695
696
697
698
699
700
701
702
703int __init of_scan_flat_dt(int (*it)(unsigned long node,
704 const char *uname, int depth,
705 void *data),
706 void *data)
707{
708 const void *blob = initial_boot_params;
709 const char *pathp;
710 int offset, rc = 0, depth = -1;
711
712 if (!blob)
713 return 0;
714
715 for (offset = fdt_next_node(blob, -1, &depth);
716 offset >= 0 && depth >= 0 && !rc;
717 offset = fdt_next_node(blob, offset, &depth)) {
718
719 pathp = fdt_get_name(blob, offset, NULL);
720 if (*pathp == '/')
721 pathp = kbasename(pathp);
722 rc = it(offset, pathp, depth, data);
723 }
724 return rc;
725}
726
727
728
729
730
731
732
733
734int __init of_scan_flat_dt_subnodes(unsigned long parent,
735 int (*it)(unsigned long node,
736 const char *uname,
737 void *data),
738 void *data)
739{
740 const void *blob = initial_boot_params;
741 int node;
742
743 fdt_for_each_subnode(node, blob, parent) {
744 const char *pathp;
745 int rc;
746
747 pathp = fdt_get_name(blob, node, NULL);
748 if (*pathp == '/')
749 pathp = kbasename(pathp);
750 rc = it(node, pathp, data);
751 if (rc)
752 return rc;
753 }
754 return 0;
755}
756
757
758
759
760
761
762
763
764
765int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
766{
767 return fdt_subnode_offset(initial_boot_params, node, uname);
768}
769
770
771
772
773unsigned long __init of_get_flat_dt_root(void)
774{
775 return 0;
776}
777
778
779
780
781int __init of_get_flat_dt_size(void)
782{
783 return fdt_totalsize(initial_boot_params);
784}
785
786
787
788
789
790
791
792const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
793 int *size)
794{
795 return fdt_getprop(initial_boot_params, node, name, size);
796}
797
798
799
800
801
802
803int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
804{
805 return of_fdt_is_compatible(initial_boot_params, node, compat);
806}
807
808
809
810
811int __init of_flat_dt_match(unsigned long node, const char *const *compat)
812{
813 return of_fdt_match(initial_boot_params, node, compat);
814}
815
816
817
818
819uint32_t __init of_get_flat_dt_phandle(unsigned long node)
820{
821 return fdt_get_phandle(initial_boot_params, node);
822}
823
824struct fdt_scan_status {
825 const char *name;
826 int namelen;
827 int depth;
828 int found;
829 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
830 void *data;
831};
832
833const char * __init of_flat_dt_get_machine_name(void)
834{
835 const char *name;
836 unsigned long dt_root = of_get_flat_dt_root();
837
838 name = of_get_flat_dt_prop(dt_root, "model", NULL);
839 if (!name)
840 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
841 return name;
842}
843
844
845
846
847
848
849
850
851
852
853const void * __init of_flat_dt_match_machine(const void *default_match,
854 const void * (*get_next_compat)(const char * const**))
855{
856 const void *data = NULL;
857 const void *best_data = default_match;
858 const char *const *compat;
859 unsigned long dt_root;
860 unsigned int best_score = ~1, score = 0;
861
862 dt_root = of_get_flat_dt_root();
863 while ((data = get_next_compat(&compat))) {
864 score = of_flat_dt_match(dt_root, compat);
865 if (score > 0 && score < best_score) {
866 best_data = data;
867 best_score = score;
868 }
869 }
870 if (!best_data) {
871 const char *prop;
872 int size;
873
874 pr_err("\n unrecognized device tree list:\n[ ");
875
876 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
877 if (prop) {
878 while (size > 0) {
879 printk("'%s' ", prop);
880 size -= strlen(prop) + 1;
881 prop += strlen(prop) + 1;
882 }
883 }
884 printk("]\n\n");
885 return NULL;
886 }
887
888 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
889
890 return best_data;
891}
892
893#ifdef CONFIG_BLK_DEV_INITRD
894#ifndef __early_init_dt_declare_initrd
895static void __early_init_dt_declare_initrd(unsigned long start,
896 unsigned long end)
897{
898 initrd_start = (unsigned long)__va(start);
899 initrd_end = (unsigned long)__va(end);
900 initrd_below_start_ok = 1;
901}
902#endif
903
904
905
906
907
908static void __init early_init_dt_check_for_initrd(unsigned long node)
909{
910 u64 start, end;
911 int len;
912 const __be32 *prop;
913
914 pr_debug("Looking for initrd properties... ");
915
916 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
917 if (!prop)
918 return;
919 start = of_read_number(prop, len/4);
920
921 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
922 if (!prop)
923 return;
924 end = of_read_number(prop, len/4);
925
926 __early_init_dt_declare_initrd(start, end);
927
928 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
929 (unsigned long long)start, (unsigned long long)end);
930}
931#else
932static inline void early_init_dt_check_for_initrd(unsigned long node)
933{
934}
935#endif
936
937#ifdef CONFIG_SERIAL_EARLYCON
938
939int __init early_init_dt_scan_chosen_stdout(void)
940{
941 int offset;
942 const char *p, *q, *options = NULL;
943 int l;
944 const struct earlycon_id **p_match;
945 const void *fdt = initial_boot_params;
946
947 offset = fdt_path_offset(fdt, "/chosen");
948 if (offset < 0)
949 offset = fdt_path_offset(fdt, "/chosen@0");
950 if (offset < 0)
951 return -ENOENT;
952
953 p = fdt_getprop(fdt, offset, "stdout-path", &l);
954 if (!p)
955 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
956 if (!p || !l)
957 return -ENOENT;
958
959 q = strchrnul(p, ':');
960 if (*q != '\0')
961 options = q + 1;
962 l = q - p;
963
964
965 offset = fdt_path_offset_namelen(fdt, p, l);
966 if (offset < 0) {
967 pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
968 return 0;
969 }
970
971 for (p_match = __earlycon_table; p_match < __earlycon_table_end;
972 p_match++) {
973 const struct earlycon_id *match = *p_match;
974
975 if (!match->compatible[0])
976 continue;
977
978 if (fdt_node_check_compatible(fdt, offset, match->compatible))
979 continue;
980
981 of_setup_earlycon(match, offset, options);
982 return 0;
983 }
984 return -ENODEV;
985}
986#endif
987
988
989
990
991int __init early_init_dt_scan_root(unsigned long node, const char *uname,
992 int depth, void *data)
993{
994 const __be32 *prop;
995
996 if (depth != 0)
997 return 0;
998
999 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
1000 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
1001
1002 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
1003 if (prop)
1004 dt_root_size_cells = be32_to_cpup(prop);
1005 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
1006
1007 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
1008 if (prop)
1009 dt_root_addr_cells = be32_to_cpup(prop);
1010 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1011
1012
1013 return 1;
1014}
1015
1016u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
1017{
1018 const __be32 *p = *cellp;
1019
1020 *cellp = p + s;
1021 return of_read_number(p, s);
1022}
1023
1024
1025
1026
1027int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
1028 int depth, void *data)
1029{
1030 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1031 const __be32 *reg, *endp;
1032 int l;
1033 bool hotpluggable;
1034
1035
1036 if (type == NULL) {
1037
1038
1039
1040
1041 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
1042 return 0;
1043 } else if (strcmp(type, "memory") != 0)
1044 return 0;
1045
1046 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1047 if (reg == NULL)
1048 reg = of_get_flat_dt_prop(node, "reg", &l);
1049 if (reg == NULL)
1050 return 0;
1051
1052 endp = reg + (l / sizeof(__be32));
1053 hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
1054
1055 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
1056
1057 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1058 u64 base, size;
1059
1060 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1061 size = dt_mem_next_cell(dt_root_size_cells, ®);
1062
1063 if (size == 0)
1064 continue;
1065 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
1066 (unsigned long long)size);
1067
1068 early_init_dt_add_memory_arch(base, size);
1069
1070 if (!hotpluggable)
1071 continue;
1072
1073 if (early_init_dt_mark_hotplug_memory_arch(base, size))
1074 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1075 base, base + size);
1076 }
1077
1078 return 0;
1079}
1080
1081int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
1082 int depth, void *data)
1083{
1084 int l;
1085 const char *p;
1086
1087 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1088
1089 if (depth != 1 || !data ||
1090 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1091 return 0;
1092
1093 early_init_dt_check_for_initrd(node);
1094
1095
1096 p = of_get_flat_dt_prop(node, "bootargs", &l);
1097 if (p != NULL && l > 0)
1098 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
1099
1100
1101
1102
1103
1104
1105#ifdef CONFIG_CMDLINE
1106#if defined(CONFIG_CMDLINE_EXTEND)
1107 strlcat(data, " ", COMMAND_LINE_SIZE);
1108 strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1109#elif defined(CONFIG_CMDLINE_FORCE)
1110 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1111#else
1112
1113 if (!((char *)data)[0])
1114 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1115#endif
1116#endif
1117
1118 pr_debug("Command line is: %s\n", (char*)data);
1119
1120
1121 return 1;
1122}
1123
1124#ifndef MIN_MEMBLOCK_ADDR
1125#define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1126#endif
1127#ifndef MAX_MEMBLOCK_ADDR
1128#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1129#endif
1130
1131void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1132{
1133 const u64 phys_offset = MIN_MEMBLOCK_ADDR;
1134
1135 if (!PAGE_ALIGNED(base)) {
1136 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
1137 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1138 base, base + size);
1139 return;
1140 }
1141 size -= PAGE_SIZE - (base & ~PAGE_MASK);
1142 base = PAGE_ALIGN(base);
1143 }
1144 size &= PAGE_MASK;
1145
1146 if (base > MAX_MEMBLOCK_ADDR) {
1147 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1148 base, base + size);
1149 return;
1150 }
1151
1152 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
1153 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1154 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1155 size = MAX_MEMBLOCK_ADDR - base + 1;
1156 }
1157
1158 if (base + size < phys_offset) {
1159 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1160 base, base + size);
1161 return;
1162 }
1163 if (base < phys_offset) {
1164 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1165 base, phys_offset);
1166 size -= phys_offset - base;
1167 base = phys_offset;
1168 }
1169 memblock_add(base, size);
1170}
1171
1172int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1173{
1174 return memblock_mark_hotplug(base, size);
1175}
1176
1177int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1178 phys_addr_t size, bool nomap)
1179{
1180 if (nomap)
1181 return memblock_remove(base, size);
1182 return memblock_reserve(base, size);
1183}
1184
1185static void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
1186{
1187 void *ptr = memblock_alloc(size, align);
1188
1189 if (!ptr)
1190 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
1191 __func__, size, align);
1192
1193 return ptr;
1194}
1195
1196bool __init early_init_dt_verify(void *params)
1197{
1198 if (!params)
1199 return false;
1200
1201
1202 if (fdt_check_header(params))
1203 return false;
1204
1205
1206 initial_boot_params = params;
1207 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1208 fdt_totalsize(initial_boot_params));
1209 return true;
1210}
1211
1212
1213void __init early_init_dt_scan_nodes(void)
1214{
1215
1216 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1217
1218
1219 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1220
1221
1222 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1223}
1224
1225bool __init early_init_dt_scan(void *params)
1226{
1227 bool status;
1228
1229 status = early_init_dt_verify(params);
1230 if (!status)
1231 return false;
1232
1233 early_init_dt_scan_nodes();
1234 return true;
1235}
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245void __init unflatten_device_tree(void)
1246{
1247 __unflatten_device_tree(initial_boot_params, NULL, &of_root,
1248 early_init_dt_alloc_memory_arch, false);
1249
1250
1251 of_alias_scan(early_init_dt_alloc_memory_arch);
1252
1253 unittest_unflatten_overlay_base();
1254}
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267void __init unflatten_and_copy_device_tree(void)
1268{
1269 int size;
1270 void *dt;
1271
1272 if (!initial_boot_params) {
1273 pr_warn("No valid device tree found, continuing without\n");
1274 return;
1275 }
1276
1277 size = fdt_totalsize(initial_boot_params);
1278 dt = early_init_dt_alloc_memory_arch(size,
1279 roundup_pow_of_two(FDT_V17_SIZE));
1280
1281 if (dt) {
1282 memcpy(dt, initial_boot_params, size);
1283 initial_boot_params = dt;
1284 }
1285 unflatten_device_tree();
1286}
1287
1288#ifdef CONFIG_SYSFS
1289static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1290 struct bin_attribute *bin_attr,
1291 char *buf, loff_t off, size_t count)
1292{
1293 memcpy(buf, initial_boot_params + off, count);
1294 return count;
1295}
1296
1297static int __init of_fdt_raw_init(void)
1298{
1299 static struct bin_attribute of_fdt_raw_attr =
1300 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1301
1302 if (!initial_boot_params)
1303 return 0;
1304
1305 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1306 fdt_totalsize(initial_boot_params))) {
1307 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
1308 return 0;
1309 }
1310 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1311 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1312}
1313late_initcall(of_fdt_raw_init);
1314#endif
1315
1316#endif
1317