1 /*
2 * Functions for working with the Flattened Device Tree data format
3 *
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5 * benh@kernel.crashing.org
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/initrd.h>
14 #include <linux/memblock.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/sizes.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/libfdt.h>
23 #include <linux/debugfs.h>
24 #include <linux/serial_core.h>
25
26 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
27 #include <asm/page.h>
28
29 /*
30 * of_fdt_limit_memory - limit the number of regions in the /memory node
31 * @limit: maximum entries
32 *
33 * Adjust the flattened device tree to have at most 'limit' number of
34 * memory entries in the /memory node. This function may be called
35 * any time after initial_boot_param is set.
36 */
of_fdt_limit_memory(int limit)37 void of_fdt_limit_memory(int limit)
38 {
39 int memory;
40 int len;
41 const void *val;
42 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
43 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
44 const uint32_t *addr_prop;
45 const uint32_t *size_prop;
46 int root_offset;
47 int cell_size;
48
49 root_offset = fdt_path_offset(initial_boot_params, "/");
50 if (root_offset < 0)
51 return;
52
53 addr_prop = fdt_getprop(initial_boot_params, root_offset,
54 "#address-cells", NULL);
55 if (addr_prop)
56 nr_address_cells = fdt32_to_cpu(*addr_prop);
57
58 size_prop = fdt_getprop(initial_boot_params, root_offset,
59 "#size-cells", NULL);
60 if (size_prop)
61 nr_size_cells = fdt32_to_cpu(*size_prop);
62
63 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
64
65 memory = fdt_path_offset(initial_boot_params, "/memory");
66 if (memory > 0) {
67 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
68 if (len > limit*cell_size) {
69 len = limit*cell_size;
70 pr_debug("Limiting number of entries to %d\n", limit);
71 fdt_setprop(initial_boot_params, memory, "reg", val,
72 len);
73 }
74 }
75 }
76
77 /**
78 * of_fdt_is_compatible - Return true if given node from the given blob has
79 * compat in its compatible list
80 * @blob: A device tree blob
81 * @node: node to test
82 * @compat: compatible string to compare with compatible list.
83 *
84 * On match, returns a non-zero value with smaller values returned for more
85 * specific compatible values.
86 */
of_fdt_is_compatible(const void * blob,unsigned long node,const char * compat)87 int of_fdt_is_compatible(const void *blob,
88 unsigned long node, const char *compat)
89 {
90 const char *cp;
91 int cplen;
92 unsigned long l, score = 0;
93
94 cp = fdt_getprop(blob, node, "compatible", &cplen);
95 if (cp == NULL)
96 return 0;
97 while (cplen > 0) {
98 score++;
99 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
100 return score;
101 l = strlen(cp) + 1;
102 cp += l;
103 cplen -= l;
104 }
105
106 return 0;
107 }
108
109 /**
110 * of_fdt_match - Return true if node matches a list of compatible values
111 */
of_fdt_match(const void * blob,unsigned long node,const char * const * compat)112 int of_fdt_match(const void *blob, unsigned long node,
113 const char *const *compat)
114 {
115 unsigned int tmp, score = 0;
116
117 if (!compat)
118 return 0;
119
120 while (*compat) {
121 tmp = of_fdt_is_compatible(blob, node, *compat);
122 if (tmp && (score == 0 || (tmp < score)))
123 score = tmp;
124 compat++;
125 }
126
127 return score;
128 }
129
unflatten_dt_alloc(void ** mem,unsigned long size,unsigned long align)130 static void *unflatten_dt_alloc(void **mem, unsigned long size,
131 unsigned long align)
132 {
133 void *res;
134
135 *mem = PTR_ALIGN(*mem, align);
136 res = *mem;
137 *mem += size;
138
139 return res;
140 }
141
142 /**
143 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
144 * @blob: The parent device tree blob
145 * @mem: Memory chunk to use for allocating device nodes and properties
146 * @p: pointer to node in flat tree
147 * @dad: Parent struct device_node
148 * @allnextpp: pointer to ->allnext from last allocated device_node
149 * @fpsize: Size of the node path up at the current depth.
150 */
unflatten_dt_node(void * blob,void * mem,int * poffset,struct device_node * dad,struct device_node *** allnextpp,unsigned long fpsize)151 static void * unflatten_dt_node(void *blob,
152 void *mem,
153 int *poffset,
154 struct device_node *dad,
155 struct device_node ***allnextpp,
156 unsigned long fpsize)
157 {
158 const __be32 *p;
159 struct device_node *np;
160 struct property *pp, **prev_pp = NULL;
161 const char *pathp;
162 unsigned int l, allocl;
163 static int depth = 0;
164 int old_depth;
165 int offset;
166 int has_name = 0;
167 int new_format = 0;
168
169 pathp = fdt_get_name(blob, *poffset, &l);
170 if (!pathp)
171 return mem;
172
173 allocl = l++;
174
175 /* version 0x10 has a more compact unit name here instead of the full
176 * path. we accumulate the full path size using "fpsize", we'll rebuild
177 * it later. We detect this because the first character of the name is
178 * not '/'.
179 */
180 if ((*pathp) != '/') {
181 new_format = 1;
182 if (fpsize == 0) {
183 /* root node: special case. fpsize accounts for path
184 * plus terminating zero. root node only has '/', so
185 * fpsize should be 2, but we want to avoid the first
186 * level nodes to have two '/' so we use fpsize 1 here
187 */
188 fpsize = 1;
189 allocl = 2;
190 l = 1;
191 pathp = "";
192 } else {
193 /* account for '/' and path size minus terminal 0
194 * already in 'l'
195 */
196 fpsize += l;
197 allocl = fpsize;
198 }
199 }
200
201 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
202 __alignof__(struct device_node));
203 if (allnextpp) {
204 char *fn;
205 of_node_init(np);
206 np->full_name = fn = ((char *)np) + sizeof(*np);
207 if (new_format) {
208 /* rebuild full path for new format */
209 if (dad && dad->parent) {
210 strcpy(fn, dad->full_name);
211 #ifdef DEBUG
212 if ((strlen(fn) + l + 1) != allocl) {
213 pr_debug("%s: p: %d, l: %d, a: %d\n",
214 pathp, (int)strlen(fn),
215 l, allocl);
216 }
217 #endif
218 fn += strlen(fn);
219 }
220 *(fn++) = '/';
221 }
222 memcpy(fn, pathp, l);
223
224 prev_pp = &np->properties;
225 **allnextpp = np;
226 *allnextpp = &np->allnext;
227 if (dad != NULL) {
228 np->parent = dad;
229 /* we temporarily use the next field as `last_child'*/
230 if (dad->next == NULL)
231 dad->child = np;
232 else
233 dad->next->sibling = np;
234 dad->next = np;
235 }
236 }
237 /* process properties */
238 for (offset = fdt_first_property_offset(blob, *poffset);
239 (offset >= 0);
240 (offset = fdt_next_property_offset(blob, offset))) {
241 const char *pname;
242 u32 sz;
243
244 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
245 offset = -FDT_ERR_INTERNAL;
246 break;
247 }
248
249 if (pname == NULL) {
250 pr_info("Can't find property name in list !\n");
251 break;
252 }
253 if (strcmp(pname, "name") == 0)
254 has_name = 1;
255 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
256 __alignof__(struct property));
257 if (allnextpp) {
258 /* We accept flattened tree phandles either in
259 * ePAPR-style "phandle" properties, or the
260 * legacy "linux,phandle" properties. If both
261 * appear and have different values, things
262 * will get weird. Don't do that. */
263 if ((strcmp(pname, "phandle") == 0) ||
264 (strcmp(pname, "linux,phandle") == 0)) {
265 if (np->phandle == 0)
266 np->phandle = be32_to_cpup(p);
267 }
268 /* And we process the "ibm,phandle" property
269 * used in pSeries dynamic device tree
270 * stuff */
271 if (strcmp(pname, "ibm,phandle") == 0)
272 np->phandle = be32_to_cpup(p);
273 pp->name = (char *)pname;
274 pp->length = sz;
275 pp->value = (__be32 *)p;
276 *prev_pp = pp;
277 prev_pp = &pp->next;
278 }
279 }
280 /* with version 0x10 we may not have the name property, recreate
281 * it here from the unit name if absent
282 */
283 if (!has_name) {
284 const char *p1 = pathp, *ps = pathp, *pa = NULL;
285 int sz;
286
287 while (*p1) {
288 if ((*p1) == '@')
289 pa = p1;
290 if ((*p1) == '/')
291 ps = p1 + 1;
292 p1++;
293 }
294 if (pa < ps)
295 pa = p1;
296 sz = (pa - ps) + 1;
297 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
298 __alignof__(struct property));
299 if (allnextpp) {
300 pp->name = "name";
301 pp->length = sz;
302 pp->value = pp + 1;
303 *prev_pp = pp;
304 prev_pp = &pp->next;
305 memcpy(pp->value, ps, sz - 1);
306 ((char *)pp->value)[sz - 1] = 0;
307 pr_debug("fixed up name for %s -> %s\n", pathp,
308 (char *)pp->value);
309 }
310 }
311 if (allnextpp) {
312 *prev_pp = NULL;
313 np->name = of_get_property(np, "name", NULL);
314 np->type = of_get_property(np, "device_type", NULL);
315
316 if (!np->name)
317 np->name = "<NULL>";
318 if (!np->type)
319 np->type = "<NULL>";
320 }
321
322 old_depth = depth;
323 *poffset = fdt_next_node(blob, *poffset, &depth);
324 if (depth < 0)
325 depth = 0;
326 while (*poffset > 0 && depth > old_depth)
327 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
328 fpsize);
329
330 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
331 pr_err("unflatten: error %d processing FDT\n", *poffset);
332
333 return mem;
334 }
335
336 /**
337 * __unflatten_device_tree - create tree of device_nodes from flat blob
338 *
339 * unflattens a device-tree, creating the
340 * tree of struct device_node. It also fills the "name" and "type"
341 * pointers of the nodes so the normal device-tree walking functions
342 * can be used.
343 * @blob: The blob to expand
344 * @mynodes: The device_node tree created by the call
345 * @dt_alloc: An allocator that provides a virtual address to memory
346 * for the resulting tree
347 */
__unflatten_device_tree(void * blob,struct device_node ** mynodes,void * (* dt_alloc)(u64 size,u64 align))348 static void __unflatten_device_tree(void *blob,
349 struct device_node **mynodes,
350 void * (*dt_alloc)(u64 size, u64 align))
351 {
352 unsigned long size;
353 int start;
354 void *mem;
355 struct device_node **allnextp = mynodes;
356
357 pr_debug(" -> unflatten_device_tree()\n");
358
359 if (!blob) {
360 pr_debug("No device tree pointer\n");
361 return;
362 }
363
364 pr_debug("Unflattening device tree:\n");
365 pr_debug("magic: %08x\n", fdt_magic(blob));
366 pr_debug("size: %08x\n", fdt_totalsize(blob));
367 pr_debug("version: %08x\n", fdt_version(blob));
368
369 if (fdt_check_header(blob)) {
370 pr_err("Invalid device tree blob header\n");
371 return;
372 }
373
374 /* First pass, scan for size */
375 start = 0;
376 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0);
377 size = ALIGN(size, 4);
378
379 pr_debug(" size is %lx, allocating...\n", size);
380
381 /* Allocate memory for the expanded device tree */
382 mem = dt_alloc(size + 4, __alignof__(struct device_node));
383 if (!mem)
384 return;
385
386 memset(mem, 0, size);
387
388 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
389
390 pr_debug(" unflattening %p...\n", mem);
391
392 /* Second pass, do actual unflattening */
393 start = 0;
394 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
395 if (be32_to_cpup(mem + size) != 0xdeadbeef)
396 pr_warning("End of tree marker overwritten: %08x\n",
397 be32_to_cpup(mem + size));
398 *allnextp = NULL;
399
400 pr_debug(" <- unflatten_device_tree()\n");
401 }
402
kernel_tree_alloc(u64 size,u64 align)403 static void *kernel_tree_alloc(u64 size, u64 align)
404 {
405 return kzalloc(size, GFP_KERNEL);
406 }
407
408 /**
409 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
410 *
411 * unflattens the device-tree passed by the firmware, creating the
412 * tree of struct device_node. It also fills the "name" and "type"
413 * pointers of the nodes so the normal device-tree walking functions
414 * can be used.
415 */
of_fdt_unflatten_tree(unsigned long * blob,struct device_node ** mynodes)416 void of_fdt_unflatten_tree(unsigned long *blob,
417 struct device_node **mynodes)
418 {
419 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
420 }
421 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
422
423 /* Everything below here references initial_boot_params directly. */
424 int __initdata dt_root_addr_cells;
425 int __initdata dt_root_size_cells;
426
427 void *initial_boot_params;
428
429 #ifdef CONFIG_OF_EARLY_FLATTREE
430
431 /**
432 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
433 */
__reserved_mem_reserve_reg(unsigned long node,const char * uname)434 static int __init __reserved_mem_reserve_reg(unsigned long node,
435 const char *uname)
436 {
437 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
438 phys_addr_t base, size;
439 int len;
440 const __be32 *prop;
441 int nomap, first = 1;
442
443 prop = of_get_flat_dt_prop(node, "reg", &len);
444 if (!prop)
445 return -ENOENT;
446
447 if (len && len % t_len != 0) {
448 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
449 uname);
450 return -EINVAL;
451 }
452
453 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
454
455 while (len >= t_len) {
456 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
457 size = dt_mem_next_cell(dt_root_size_cells, &prop);
458
459 if (size &&
460 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
461 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
462 uname, &base, (unsigned long)size / SZ_1M);
463 else
464 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
465 uname, &base, (unsigned long)size / SZ_1M);
466
467 len -= t_len;
468 if (first) {
469 fdt_reserved_mem_save_node(node, uname, base, size);
470 first = 0;
471 }
472 }
473 return 0;
474 }
475
476 /**
477 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
478 * in /reserved-memory matches the values supported by the current implementation,
479 * also check if ranges property has been provided
480 */
__reserved_mem_check_root(unsigned long node)481 static int __init __reserved_mem_check_root(unsigned long node)
482 {
483 const __be32 *prop;
484
485 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
486 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
487 return -EINVAL;
488
489 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
490 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
491 return -EINVAL;
492
493 prop = of_get_flat_dt_prop(node, "ranges", NULL);
494 if (!prop)
495 return -EINVAL;
496 return 0;
497 }
498
499 /**
500 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
501 */
__fdt_scan_reserved_mem(unsigned long node,const char * uname,int depth,void * data)502 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
503 int depth, void *data)
504 {
505 static int found;
506 const char *status;
507 int err;
508
509 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
510 if (__reserved_mem_check_root(node) != 0) {
511 pr_err("Reserved memory: unsupported node format, ignoring\n");
512 /* break scan */
513 return 1;
514 }
515 found = 1;
516 /* scan next node */
517 return 0;
518 } else if (!found) {
519 /* scan next node */
520 return 0;
521 } else if (found && depth < 2) {
522 /* scanning of /reserved-memory has been finished */
523 return 1;
524 }
525
526 status = of_get_flat_dt_prop(node, "status", NULL);
527 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
528 return 0;
529
530 err = __reserved_mem_reserve_reg(node, uname);
531 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
532 fdt_reserved_mem_save_node(node, uname, 0, 0);
533
534 /* scan next node */
535 return 0;
536 }
537
538 /**
539 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
540 *
541 * This function grabs memory from early allocator for device exclusive use
542 * defined in device tree structures. It should be called by arch specific code
543 * once the early allocator (i.e. memblock) has been fully activated.
544 */
early_init_fdt_scan_reserved_mem(void)545 void __init early_init_fdt_scan_reserved_mem(void)
546 {
547 int n;
548 u64 base, size;
549
550 if (!initial_boot_params)
551 return;
552
553 /* Reserve the dtb region */
554 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
555 fdt_totalsize(initial_boot_params),
556 0);
557
558 /* Process header /memreserve/ fields */
559 for (n = 0; ; n++) {
560 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
561 if (!size)
562 break;
563 early_init_dt_reserve_memory_arch(base, size, 0);
564 }
565
566 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
567 fdt_init_reserved_mem();
568 }
569
570 /**
571 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
572 * @it: callback function
573 * @data: context data pointer
574 *
575 * This function is used to scan the flattened device-tree, it is
576 * used to extract the memory information at boot before we can
577 * unflatten the tree
578 */
of_scan_flat_dt(int (* it)(unsigned long node,const char * uname,int depth,void * data),void * data)579 int __init of_scan_flat_dt(int (*it)(unsigned long node,
580 const char *uname, int depth,
581 void *data),
582 void *data)
583 {
584 const void *blob = initial_boot_params;
585 const char *pathp;
586 int offset, rc = 0, depth = -1;
587
588 if (!blob)
589 return 0;
590
591 for (offset = fdt_next_node(blob, -1, &depth);
592 offset >= 0 && depth >= 0 && !rc;
593 offset = fdt_next_node(blob, offset, &depth)) {
594
595 pathp = fdt_get_name(blob, offset, NULL);
596 if (*pathp == '/')
597 pathp = kbasename(pathp);
598 rc = it(offset, pathp, depth, data);
599 }
600 return rc;
601 }
602
603 /**
604 * of_get_flat_dt_root - find the root node in the flat blob
605 */
of_get_flat_dt_root(void)606 unsigned long __init of_get_flat_dt_root(void)
607 {
608 return 0;
609 }
610
611 /**
612 * of_get_flat_dt_size - Return the total size of the FDT
613 */
of_get_flat_dt_size(void)614 int __init of_get_flat_dt_size(void)
615 {
616 return fdt_totalsize(initial_boot_params);
617 }
618
619 /**
620 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
621 *
622 * This function can be used within scan_flattened_dt callback to get
623 * access to properties
624 */
of_get_flat_dt_prop(unsigned long node,const char * name,int * size)625 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
626 int *size)
627 {
628 return fdt_getprop(initial_boot_params, node, name, size);
629 }
630
631 /**
632 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
633 * @node: node to test
634 * @compat: compatible string to compare with compatible list.
635 */
of_flat_dt_is_compatible(unsigned long node,const char * compat)636 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
637 {
638 return of_fdt_is_compatible(initial_boot_params, node, compat);
639 }
640
641 /**
642 * of_flat_dt_match - Return true if node matches a list of compatible values
643 */
of_flat_dt_match(unsigned long node,const char * const * compat)644 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
645 {
646 return of_fdt_match(initial_boot_params, node, compat);
647 }
648
649 struct fdt_scan_status {
650 const char *name;
651 int namelen;
652 int depth;
653 int found;
654 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
655 void *data;
656 };
657
of_flat_dt_get_machine_name(void)658 const char * __init of_flat_dt_get_machine_name(void)
659 {
660 const char *name;
661 unsigned long dt_root = of_get_flat_dt_root();
662
663 name = of_get_flat_dt_prop(dt_root, "model", NULL);
664 if (!name)
665 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
666 return name;
667 }
668
669 /**
670 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
671 *
672 * @default_match: A machine specific ptr to return in case of no match.
673 * @get_next_compat: callback function to return next compatible match table.
674 *
675 * Iterate through machine match tables to find the best match for the machine
676 * compatible string in the FDT.
677 */
of_flat_dt_match_machine(const void * default_match,const void * (* get_next_compat)(const char * const **))678 const void * __init of_flat_dt_match_machine(const void *default_match,
679 const void * (*get_next_compat)(const char * const**))
680 {
681 const void *data = NULL;
682 const void *best_data = default_match;
683 const char *const *compat;
684 unsigned long dt_root;
685 unsigned int best_score = ~1, score = 0;
686
687 dt_root = of_get_flat_dt_root();
688 while ((data = get_next_compat(&compat))) {
689 score = of_flat_dt_match(dt_root, compat);
690 if (score > 0 && score < best_score) {
691 best_data = data;
692 best_score = score;
693 }
694 }
695 if (!best_data) {
696 const char *prop;
697 int size;
698
699 pr_err("\n unrecognized device tree list:\n[ ");
700
701 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
702 if (prop) {
703 while (size > 0) {
704 printk("'%s' ", prop);
705 size -= strlen(prop) + 1;
706 prop += strlen(prop) + 1;
707 }
708 }
709 printk("]\n\n");
710 return NULL;
711 }
712
713 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
714
715 return best_data;
716 }
717
718 #ifdef CONFIG_BLK_DEV_INITRD
719 /**
720 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
721 * @node: reference to node containing initrd location ('chosen')
722 */
early_init_dt_check_for_initrd(unsigned long node)723 static void __init early_init_dt_check_for_initrd(unsigned long node)
724 {
725 u64 start, end;
726 int len;
727 const __be32 *prop;
728
729 pr_debug("Looking for initrd properties... ");
730
731 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
732 if (!prop)
733 return;
734 start = of_read_number(prop, len/4);
735
736 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
737 if (!prop)
738 return;
739 end = of_read_number(prop, len/4);
740
741 initrd_start = (unsigned long)__va(start);
742 initrd_end = (unsigned long)__va(end);
743 initrd_below_start_ok = 1;
744
745 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
746 (unsigned long long)start, (unsigned long long)end);
747 }
748 #else
early_init_dt_check_for_initrd(unsigned long node)749 static inline void early_init_dt_check_for_initrd(unsigned long node)
750 {
751 }
752 #endif /* CONFIG_BLK_DEV_INITRD */
753
754 #ifdef CONFIG_SERIAL_EARLYCON
755 extern struct of_device_id __earlycon_of_table[];
756
early_init_dt_scan_chosen_serial(void)757 int __init early_init_dt_scan_chosen_serial(void)
758 {
759 int offset;
760 const char *p;
761 int l;
762 const struct of_device_id *match = __earlycon_of_table;
763 const void *fdt = initial_boot_params;
764
765 offset = fdt_path_offset(fdt, "/chosen");
766 if (offset < 0)
767 offset = fdt_path_offset(fdt, "/chosen@0");
768 if (offset < 0)
769 return -ENOENT;
770
771 p = fdt_getprop(fdt, offset, "stdout-path", &l);
772 if (!p)
773 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
774 if (!p || !l)
775 return -ENOENT;
776
777 /* Get the node specified by stdout-path */
778 offset = fdt_path_offset(fdt, p);
779 if (offset < 0)
780 return -ENODEV;
781
782 while (match->compatible[0]) {
783 unsigned long addr;
784 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
785 match++;
786 continue;
787 }
788
789 addr = fdt_translate_address(fdt, offset);
790 if (!addr)
791 return -ENXIO;
792
793 of_setup_earlycon(addr, match->data);
794 return 0;
795 }
796 return -ENODEV;
797 }
798
setup_of_earlycon(char * buf)799 static int __init setup_of_earlycon(char *buf)
800 {
801 if (buf)
802 return 0;
803
804 return early_init_dt_scan_chosen_serial();
805 }
806 early_param("earlycon", setup_of_earlycon);
807 #endif
808
809 /**
810 * early_init_dt_scan_root - fetch the top level address and size cells
811 */
early_init_dt_scan_root(unsigned long node,const char * uname,int depth,void * data)812 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
813 int depth, void *data)
814 {
815 const __be32 *prop;
816
817 if (depth != 0)
818 return 0;
819
820 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
821 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
822
823 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
824 if (prop)
825 dt_root_size_cells = be32_to_cpup(prop);
826 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
827
828 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
829 if (prop)
830 dt_root_addr_cells = be32_to_cpup(prop);
831 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
832
833 /* break now */
834 return 1;
835 }
836
dt_mem_next_cell(int s,const __be32 ** cellp)837 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
838 {
839 const __be32 *p = *cellp;
840
841 *cellp = p + s;
842 return of_read_number(p, s);
843 }
844
845 /**
846 * early_init_dt_scan_memory - Look for an parse memory nodes
847 */
early_init_dt_scan_memory(unsigned long node,const char * uname,int depth,void * data)848 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
849 int depth, void *data)
850 {
851 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
852 const __be32 *reg, *endp;
853 int l;
854
855 /* We are scanning "memory" nodes only */
856 if (type == NULL) {
857 /*
858 * The longtrail doesn't have a device_type on the
859 * /memory node, so look for the node called /memory@0.
860 */
861 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
862 return 0;
863 } else if (strcmp(type, "memory") != 0)
864 return 0;
865
866 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
867 if (reg == NULL)
868 reg = of_get_flat_dt_prop(node, "reg", &l);
869 if (reg == NULL)
870 return 0;
871
872 endp = reg + (l / sizeof(__be32));
873
874 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
875 uname, l, reg[0], reg[1], reg[2], reg[3]);
876
877 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
878 u64 base, size;
879
880 base = dt_mem_next_cell(dt_root_addr_cells, ®);
881 size = dt_mem_next_cell(dt_root_size_cells, ®);
882
883 if (size == 0)
884 continue;
885 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
886 (unsigned long long)size);
887
888 early_init_dt_add_memory_arch(base, size);
889 }
890
891 return 0;
892 }
893
894 /*
895 * Convert configs to something easy to use in C code
896 */
897 #if defined(CONFIG_CMDLINE_FORCE)
898 static const int overwrite_incoming_cmdline = 1;
899 static const int read_dt_cmdline;
900 static const int concat_cmdline;
901 #elif defined(CONFIG_CMDLINE_EXTEND)
902 static const int overwrite_incoming_cmdline;
903 static const int read_dt_cmdline = 1;
904 static const int concat_cmdline = 1;
905 #else /* CMDLINE_FROM_BOOTLOADER */
906 static const int overwrite_incoming_cmdline;
907 static const int read_dt_cmdline = 1;
908 static const int concat_cmdline;
909 #endif
910
911 #ifdef CONFIG_CMDLINE
912 static const char *config_cmdline = CONFIG_CMDLINE;
913 #else
914 static const char *config_cmdline = "";
915 #endif
916
early_init_dt_scan_chosen(unsigned long node,const char * uname,int depth,void * data)917 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
918 int depth, void *data)
919 {
920 int l = 0;
921 const char *p = NULL;
922 char *cmdline = data;
923
924 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
925
926 if (depth != 1 || !cmdline ||
927 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
928 return 0;
929
930 early_init_dt_check_for_initrd(node);
931
932 /* Put CONFIG_CMDLINE in if forced or if data had nothing in it to start */
933 if (overwrite_incoming_cmdline || !cmdline[0])
934 strlcpy(cmdline, config_cmdline, COMMAND_LINE_SIZE);
935
936 /* Retrieve command line unless forcing */
937 if (read_dt_cmdline)
938 p = of_get_flat_dt_prop(node, "bootargs", &l);
939
940 if (p != NULL && l > 0) {
941 if (concat_cmdline) {
942 int cmdline_len;
943 int copy_len;
944 strlcat(cmdline, " ", COMMAND_LINE_SIZE);
945 cmdline_len = strlen(cmdline);
946 copy_len = COMMAND_LINE_SIZE - cmdline_len - 1;
947 copy_len = min((int)l, copy_len);
948 strncpy(cmdline + cmdline_len, p, copy_len);
949 cmdline[cmdline_len + copy_len] = '\0';
950 } else {
951 strlcpy(cmdline, p, min((int)l, COMMAND_LINE_SIZE));
952 }
953 }
954
955 pr_debug("Command line is: %s\n", (char*)data);
956
957 /* break now */
958 return 1;
959 }
960
961 #ifdef CONFIG_HAVE_MEMBLOCK
962 #define MAX_PHYS_ADDR ((phys_addr_t)~0)
963
early_init_dt_add_memory_arch(u64 base,u64 size)964 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
965 {
966 const u64 phys_offset = __pa(PAGE_OFFSET);
967
968 if (!PAGE_ALIGNED(base)) {
969 size -= PAGE_SIZE - (base & ~PAGE_MASK);
970 base = PAGE_ALIGN(base);
971 }
972 size &= PAGE_MASK;
973
974 if (base > MAX_PHYS_ADDR) {
975 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
976 base, base + size);
977 return;
978 }
979
980 if (base + size - 1 > MAX_PHYS_ADDR) {
981 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
982 ((u64)MAX_PHYS_ADDR) + 1, base + size);
983 size = MAX_PHYS_ADDR - base + 1;
984 }
985
986 if (base + size < phys_offset) {
987 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
988 base, base + size);
989 return;
990 }
991 if (base < phys_offset) {
992 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
993 base, phys_offset);
994 size -= phys_offset - base;
995 base = phys_offset;
996 }
997 memblock_add(base, size);
998 }
999
early_init_dt_reserve_memory_arch(phys_addr_t base,phys_addr_t size,bool nomap)1000 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1001 phys_addr_t size, bool nomap)
1002 {
1003 if (nomap)
1004 return memblock_remove(base, size);
1005 return memblock_reserve(base, size);
1006 }
1007
1008 /*
1009 * called from unflatten_device_tree() to bootstrap devicetree itself
1010 * Architectures can override this definition if memblock isn't used
1011 */
early_init_dt_alloc_memory_arch(u64 size,u64 align)1012 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1013 {
1014 return __va(memblock_alloc(size, align));
1015 }
1016 #else
early_init_dt_reserve_memory_arch(phys_addr_t base,phys_addr_t size,bool nomap)1017 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1018 phys_addr_t size, bool nomap)
1019 {
1020 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
1021 &base, &size, nomap ? " (nomap)" : "");
1022 return -ENOSYS;
1023 }
1024 #endif
1025
early_init_dt_verify(void * params)1026 bool __init early_init_dt_verify(void *params)
1027 {
1028 if (!params)
1029 return false;
1030
1031 /* Setup flat device-tree pointer */
1032 initial_boot_params = params;
1033
1034 /* check device tree validity */
1035 if (fdt_check_header(params)) {
1036 initial_boot_params = NULL;
1037 return false;
1038 }
1039
1040 return true;
1041 }
1042
1043
early_init_dt_scan_nodes(void)1044 void __init early_init_dt_scan_nodes(void)
1045 {
1046 /* Retrieve various information from the /chosen node */
1047 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1048
1049 /* Initialize {size,address}-cells info */
1050 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1051
1052 /* Setup memory, calling early_init_dt_add_memory_arch */
1053 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1054 }
1055
early_init_dt_scan(void * params)1056 bool __init early_init_dt_scan(void *params)
1057 {
1058 bool status;
1059
1060 status = early_init_dt_verify(params);
1061 if (!status)
1062 return false;
1063
1064 early_init_dt_scan_nodes();
1065 return true;
1066 }
1067
1068 /**
1069 * unflatten_device_tree - create tree of device_nodes from flat blob
1070 *
1071 * unflattens the device-tree passed by the firmware, creating the
1072 * tree of struct device_node. It also fills the "name" and "type"
1073 * pointers of the nodes so the normal device-tree walking functions
1074 * can be used.
1075 */
unflatten_device_tree(void)1076 void __init unflatten_device_tree(void)
1077 {
1078 __unflatten_device_tree(initial_boot_params, &of_allnodes,
1079 early_init_dt_alloc_memory_arch);
1080
1081 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1082 of_alias_scan(early_init_dt_alloc_memory_arch);
1083 }
1084
1085 /**
1086 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1087 *
1088 * Copies and unflattens the device-tree passed by the firmware, creating the
1089 * tree of struct device_node. It also fills the "name" and "type"
1090 * pointers of the nodes so the normal device-tree walking functions
1091 * can be used. This should only be used when the FDT memory has not been
1092 * reserved such is the case when the FDT is built-in to the kernel init
1093 * section. If the FDT memory is reserved already then unflatten_device_tree
1094 * should be used instead.
1095 */
unflatten_and_copy_device_tree(void)1096 void __init unflatten_and_copy_device_tree(void)
1097 {
1098 int size;
1099 void *dt;
1100
1101 if (!initial_boot_params) {
1102 pr_warn("No valid device tree found, continuing without\n");
1103 return;
1104 }
1105
1106 size = fdt_totalsize(initial_boot_params);
1107 dt = early_init_dt_alloc_memory_arch(size,
1108 roundup_pow_of_two(FDT_V17_SIZE));
1109
1110 if (dt) {
1111 memcpy(dt, initial_boot_params, size);
1112 initial_boot_params = dt;
1113 }
1114 unflatten_device_tree();
1115 }
1116
1117 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1118 static struct debugfs_blob_wrapper flat_dt_blob;
1119
of_flat_dt_debugfs_export_fdt(void)1120 static int __init of_flat_dt_debugfs_export_fdt(void)
1121 {
1122 struct dentry *d = debugfs_create_dir("device-tree", NULL);
1123
1124 if (!d)
1125 return -ENOENT;
1126
1127 flat_dt_blob.data = initial_boot_params;
1128 flat_dt_blob.size = fdt_totalsize(initial_boot_params);
1129
1130 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1131 d, &flat_dt_blob);
1132 if (!d)
1133 return -ENOENT;
1134
1135 return 0;
1136 }
1137 module_init(of_flat_dt_debugfs_export_fdt);
1138 #endif
1139
1140 #endif /* CONFIG_OF_EARLY_FLATTREE */
1141