ima: fix ima_alloc_atfm()
[linux/fpc-iii.git] / drivers / of / fdt.c
blobb777d8f46bd59696e366f957c27211411b32ef76
1 /*
2 * Functions for working with the Flattened Device Tree data format
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5 * benh@kernel.crashing.org
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.
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>
26 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
27 #include <asm/page.h>
29 /**
30 * of_fdt_is_compatible - Return true if given node from the given blob has
31 * compat in its compatible list
32 * @blob: A device tree blob
33 * @node: node to test
34 * @compat: compatible string to compare with compatible list.
36 * On match, returns a non-zero value with smaller values returned for more
37 * specific compatible values.
39 int of_fdt_is_compatible(const void *blob,
40 unsigned long node, const char *compat)
42 const char *cp;
43 int cplen;
44 unsigned long l, score = 0;
46 cp = fdt_getprop(blob, node, "compatible", &cplen);
47 if (cp == NULL)
48 return 0;
49 while (cplen > 0) {
50 score++;
51 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
52 return score;
53 l = strlen(cp) + 1;
54 cp += l;
55 cplen -= l;
58 return 0;
61 /**
62 * of_fdt_match - Return true if node matches a list of compatible values
64 int of_fdt_match(const void *blob, unsigned long node,
65 const char *const *compat)
67 unsigned int tmp, score = 0;
69 if (!compat)
70 return 0;
72 while (*compat) {
73 tmp = of_fdt_is_compatible(blob, node, *compat);
74 if (tmp && (score == 0 || (tmp < score)))
75 score = tmp;
76 compat++;
79 return score;
82 static void *unflatten_dt_alloc(void **mem, unsigned long size,
83 unsigned long align)
85 void *res;
87 *mem = PTR_ALIGN(*mem, align);
88 res = *mem;
89 *mem += size;
91 return res;
94 /**
95 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
96 * @blob: The parent device tree blob
97 * @mem: Memory chunk to use for allocating device nodes and properties
98 * @p: pointer to node in flat tree
99 * @dad: Parent struct device_node
100 * @allnextpp: pointer to ->allnext from last allocated device_node
101 * @fpsize: Size of the node path up at the current depth.
103 static void * unflatten_dt_node(void *blob,
104 void *mem,
105 int *poffset,
106 struct device_node *dad,
107 struct device_node ***allnextpp,
108 unsigned long fpsize)
110 const __be32 *p;
111 struct device_node *np;
112 struct property *pp, **prev_pp = NULL;
113 const char *pathp;
114 unsigned int l, allocl;
115 static int depth = 0;
116 int old_depth;
117 int offset;
118 int has_name = 0;
119 int new_format = 0;
121 pathp = fdt_get_name(blob, *poffset, &l);
122 if (!pathp)
123 return mem;
125 allocl = l++;
127 /* version 0x10 has a more compact unit name here instead of the full
128 * path. we accumulate the full path size using "fpsize", we'll rebuild
129 * it later. We detect this because the first character of the name is
130 * not '/'.
132 if ((*pathp) != '/') {
133 new_format = 1;
134 if (fpsize == 0) {
135 /* root node: special case. fpsize accounts for path
136 * plus terminating zero. root node only has '/', so
137 * fpsize should be 2, but we want to avoid the first
138 * level nodes to have two '/' so we use fpsize 1 here
140 fpsize = 1;
141 allocl = 2;
142 l = 1;
143 pathp = "";
144 } else {
145 /* account for '/' and path size minus terminal 0
146 * already in 'l'
148 fpsize += l;
149 allocl = fpsize;
153 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
154 __alignof__(struct device_node));
155 if (allnextpp) {
156 char *fn;
157 of_node_init(np);
158 np->full_name = fn = ((char *)np) + sizeof(*np);
159 if (new_format) {
160 /* rebuild full path for new format */
161 if (dad && dad->parent) {
162 strcpy(fn, dad->full_name);
163 #ifdef DEBUG
164 if ((strlen(fn) + l + 1) != allocl) {
165 pr_debug("%s: p: %d, l: %d, a: %d\n",
166 pathp, (int)strlen(fn),
167 l, allocl);
169 #endif
170 fn += strlen(fn);
172 *(fn++) = '/';
174 memcpy(fn, pathp, l);
176 prev_pp = &np->properties;
177 **allnextpp = np;
178 *allnextpp = &np->allnext;
179 if (dad != NULL) {
180 np->parent = dad;
181 /* we temporarily use the next field as `last_child'*/
182 if (dad->next == NULL)
183 dad->child = np;
184 else
185 dad->next->sibling = np;
186 dad->next = np;
189 /* process properties */
190 for (offset = fdt_first_property_offset(blob, *poffset);
191 (offset >= 0);
192 (offset = fdt_next_property_offset(blob, offset))) {
193 const char *pname;
194 u32 sz;
196 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
197 offset = -FDT_ERR_INTERNAL;
198 break;
201 if (pname == NULL) {
202 pr_info("Can't find property name in list !\n");
203 break;
205 if (strcmp(pname, "name") == 0)
206 has_name = 1;
207 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
208 __alignof__(struct property));
209 if (allnextpp) {
210 /* We accept flattened tree phandles either in
211 * ePAPR-style "phandle" properties, or the
212 * legacy "linux,phandle" properties. If both
213 * appear and have different values, things
214 * will get weird. Don't do that. */
215 if ((strcmp(pname, "phandle") == 0) ||
216 (strcmp(pname, "linux,phandle") == 0)) {
217 if (np->phandle == 0)
218 np->phandle = be32_to_cpup(p);
220 /* And we process the "ibm,phandle" property
221 * used in pSeries dynamic device tree
222 * stuff */
223 if (strcmp(pname, "ibm,phandle") == 0)
224 np->phandle = be32_to_cpup(p);
225 pp->name = (char *)pname;
226 pp->length = sz;
227 pp->value = (__be32 *)p;
228 *prev_pp = pp;
229 prev_pp = &pp->next;
232 /* with version 0x10 we may not have the name property, recreate
233 * it here from the unit name if absent
235 if (!has_name) {
236 const char *p1 = pathp, *ps = pathp, *pa = NULL;
237 int sz;
239 while (*p1) {
240 if ((*p1) == '@')
241 pa = p1;
242 if ((*p1) == '/')
243 ps = p1 + 1;
244 p1++;
246 if (pa < ps)
247 pa = p1;
248 sz = (pa - ps) + 1;
249 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
250 __alignof__(struct property));
251 if (allnextpp) {
252 pp->name = "name";
253 pp->length = sz;
254 pp->value = pp + 1;
255 *prev_pp = pp;
256 prev_pp = &pp->next;
257 memcpy(pp->value, ps, sz - 1);
258 ((char *)pp->value)[sz - 1] = 0;
259 pr_debug("fixed up name for %s -> %s\n", pathp,
260 (char *)pp->value);
263 if (allnextpp) {
264 *prev_pp = NULL;
265 np->name = of_get_property(np, "name", NULL);
266 np->type = of_get_property(np, "device_type", NULL);
268 if (!np->name)
269 np->name = "<NULL>";
270 if (!np->type)
271 np->type = "<NULL>";
274 old_depth = depth;
275 *poffset = fdt_next_node(blob, *poffset, &depth);
276 if (depth < 0)
277 depth = 0;
278 while (*poffset > 0 && depth > old_depth)
279 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
280 fpsize);
282 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
283 pr_err("unflatten: error %d processing FDT\n", *poffset);
285 return mem;
289 * __unflatten_device_tree - create tree of device_nodes from flat blob
291 * unflattens a device-tree, creating the
292 * tree of struct device_node. It also fills the "name" and "type"
293 * pointers of the nodes so the normal device-tree walking functions
294 * can be used.
295 * @blob: The blob to expand
296 * @mynodes: The device_node tree created by the call
297 * @dt_alloc: An allocator that provides a virtual address to memory
298 * for the resulting tree
300 static void __unflatten_device_tree(void *blob,
301 struct device_node **mynodes,
302 void * (*dt_alloc)(u64 size, u64 align))
304 unsigned long size;
305 int start;
306 void *mem;
307 struct device_node **allnextp = mynodes;
309 pr_debug(" -> unflatten_device_tree()\n");
311 if (!blob) {
312 pr_debug("No device tree pointer\n");
313 return;
316 pr_debug("Unflattening device tree:\n");
317 pr_debug("magic: %08x\n", fdt_magic(blob));
318 pr_debug("size: %08x\n", fdt_totalsize(blob));
319 pr_debug("version: %08x\n", fdt_version(blob));
321 if (fdt_check_header(blob)) {
322 pr_err("Invalid device tree blob header\n");
323 return;
326 /* First pass, scan for size */
327 start = 0;
328 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0);
329 size = ALIGN(size, 4);
331 pr_debug(" size is %lx, allocating...\n", size);
333 /* Allocate memory for the expanded device tree */
334 mem = dt_alloc(size + 4, __alignof__(struct device_node));
335 memset(mem, 0, size);
337 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
339 pr_debug(" unflattening %p...\n", mem);
341 /* Second pass, do actual unflattening */
342 start = 0;
343 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
344 if (be32_to_cpup(mem + size) != 0xdeadbeef)
345 pr_warning("End of tree marker overwritten: %08x\n",
346 be32_to_cpup(mem + size));
347 *allnextp = NULL;
349 pr_debug(" <- unflatten_device_tree()\n");
352 static void *kernel_tree_alloc(u64 size, u64 align)
354 return kzalloc(size, GFP_KERNEL);
358 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
360 * unflattens the device-tree passed by the firmware, creating the
361 * tree of struct device_node. It also fills the "name" and "type"
362 * pointers of the nodes so the normal device-tree walking functions
363 * can be used.
365 void of_fdt_unflatten_tree(unsigned long *blob,
366 struct device_node **mynodes)
368 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
370 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
372 /* Everything below here references initial_boot_params directly. */
373 int __initdata dt_root_addr_cells;
374 int __initdata dt_root_size_cells;
376 void *initial_boot_params;
378 #ifdef CONFIG_OF_EARLY_FLATTREE
381 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
383 static int __init __reserved_mem_reserve_reg(unsigned long node,
384 const char *uname)
386 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
387 phys_addr_t base, size;
388 int len;
389 const __be32 *prop;
390 int nomap, first = 1;
392 prop = of_get_flat_dt_prop(node, "reg", &len);
393 if (!prop)
394 return -ENOENT;
396 if (len && len % t_len != 0) {
397 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
398 uname);
399 return -EINVAL;
402 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
404 while (len >= t_len) {
405 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
406 size = dt_mem_next_cell(dt_root_size_cells, &prop);
408 if (base && size &&
409 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
410 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
411 uname, &base, (unsigned long)size / SZ_1M);
412 else
413 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
414 uname, &base, (unsigned long)size / SZ_1M);
416 len -= t_len;
417 if (first) {
418 fdt_reserved_mem_save_node(node, uname, base, size);
419 first = 0;
422 return 0;
426 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
427 * in /reserved-memory matches the values supported by the current implementation,
428 * also check if ranges property has been provided
430 static int __init __reserved_mem_check_root(unsigned long node)
432 const __be32 *prop;
434 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
435 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
436 return -EINVAL;
438 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
439 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
440 return -EINVAL;
442 prop = of_get_flat_dt_prop(node, "ranges", NULL);
443 if (!prop)
444 return -EINVAL;
445 return 0;
449 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
451 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
452 int depth, void *data)
454 static int found;
455 const char *status;
456 int err;
458 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
459 if (__reserved_mem_check_root(node) != 0) {
460 pr_err("Reserved memory: unsupported node format, ignoring\n");
461 /* break scan */
462 return 1;
464 found = 1;
465 /* scan next node */
466 return 0;
467 } else if (!found) {
468 /* scan next node */
469 return 0;
470 } else if (found && depth < 2) {
471 /* scanning of /reserved-memory has been finished */
472 return 1;
475 status = of_get_flat_dt_prop(node, "status", NULL);
476 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
477 return 0;
479 err = __reserved_mem_reserve_reg(node, uname);
480 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
481 fdt_reserved_mem_save_node(node, uname, 0, 0);
483 /* scan next node */
484 return 0;
488 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
490 * This function grabs memory from early allocator for device exclusive use
491 * defined in device tree structures. It should be called by arch specific code
492 * once the early allocator (i.e. memblock) has been fully activated.
494 void __init early_init_fdt_scan_reserved_mem(void)
496 int n;
497 u64 base, size;
499 if (!initial_boot_params)
500 return;
502 /* Reserve the dtb region */
503 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
504 fdt_totalsize(initial_boot_params),
507 /* Process header /memreserve/ fields */
508 for (n = 0; ; n++) {
509 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
510 if (!size)
511 break;
512 early_init_dt_reserve_memory_arch(base, size, 0);
515 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
516 fdt_init_reserved_mem();
520 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
521 * @it: callback function
522 * @data: context data pointer
524 * This function is used to scan the flattened device-tree, it is
525 * used to extract the memory information at boot before we can
526 * unflatten the tree
528 int __init of_scan_flat_dt(int (*it)(unsigned long node,
529 const char *uname, int depth,
530 void *data),
531 void *data)
533 const void *blob = initial_boot_params;
534 const char *pathp;
535 int offset, rc = 0, depth = -1;
537 for (offset = fdt_next_node(blob, -1, &depth);
538 offset >= 0 && depth >= 0 && !rc;
539 offset = fdt_next_node(blob, offset, &depth)) {
541 pathp = fdt_get_name(blob, offset, NULL);
542 if (*pathp == '/')
543 pathp = kbasename(pathp);
544 rc = it(offset, pathp, depth, data);
546 return rc;
550 * of_get_flat_dt_root - find the root node in the flat blob
552 unsigned long __init of_get_flat_dt_root(void)
554 return 0;
558 * of_get_flat_dt_size - Return the total size of the FDT
560 int __init of_get_flat_dt_size(void)
562 return fdt_totalsize(initial_boot_params);
566 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
568 * This function can be used within scan_flattened_dt callback to get
569 * access to properties
571 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
572 int *size)
574 return fdt_getprop(initial_boot_params, node, name, size);
578 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
579 * @node: node to test
580 * @compat: compatible string to compare with compatible list.
582 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
584 return of_fdt_is_compatible(initial_boot_params, node, compat);
588 * of_flat_dt_match - Return true if node matches a list of compatible values
590 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
592 return of_fdt_match(initial_boot_params, node, compat);
595 struct fdt_scan_status {
596 const char *name;
597 int namelen;
598 int depth;
599 int found;
600 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
601 void *data;
604 const char * __init of_flat_dt_get_machine_name(void)
606 const char *name;
607 unsigned long dt_root = of_get_flat_dt_root();
609 name = of_get_flat_dt_prop(dt_root, "model", NULL);
610 if (!name)
611 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
612 return name;
616 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
618 * @default_match: A machine specific ptr to return in case of no match.
619 * @get_next_compat: callback function to return next compatible match table.
621 * Iterate through machine match tables to find the best match for the machine
622 * compatible string in the FDT.
624 const void * __init of_flat_dt_match_machine(const void *default_match,
625 const void * (*get_next_compat)(const char * const**))
627 const void *data = NULL;
628 const void *best_data = default_match;
629 const char *const *compat;
630 unsigned long dt_root;
631 unsigned int best_score = ~1, score = 0;
633 dt_root = of_get_flat_dt_root();
634 while ((data = get_next_compat(&compat))) {
635 score = of_flat_dt_match(dt_root, compat);
636 if (score > 0 && score < best_score) {
637 best_data = data;
638 best_score = score;
641 if (!best_data) {
642 const char *prop;
643 int size;
645 pr_err("\n unrecognized device tree list:\n[ ");
647 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
648 if (prop) {
649 while (size > 0) {
650 printk("'%s' ", prop);
651 size -= strlen(prop) + 1;
652 prop += strlen(prop) + 1;
655 printk("]\n\n");
656 return NULL;
659 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
661 return best_data;
664 #ifdef CONFIG_BLK_DEV_INITRD
666 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
667 * @node: reference to node containing initrd location ('chosen')
669 static void __init early_init_dt_check_for_initrd(unsigned long node)
671 u64 start, end;
672 int len;
673 const __be32 *prop;
675 pr_debug("Looking for initrd properties... ");
677 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
678 if (!prop)
679 return;
680 start = of_read_number(prop, len/4);
682 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
683 if (!prop)
684 return;
685 end = of_read_number(prop, len/4);
687 initrd_start = (unsigned long)__va(start);
688 initrd_end = (unsigned long)__va(end);
689 initrd_below_start_ok = 1;
691 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
692 (unsigned long long)start, (unsigned long long)end);
694 #else
695 static inline void early_init_dt_check_for_initrd(unsigned long node)
698 #endif /* CONFIG_BLK_DEV_INITRD */
700 #ifdef CONFIG_SERIAL_EARLYCON
701 extern struct of_device_id __earlycon_of_table[];
703 int __init early_init_dt_scan_chosen_serial(void)
705 int offset;
706 const char *p;
707 int l;
708 const struct of_device_id *match = __earlycon_of_table;
709 const void *fdt = initial_boot_params;
711 offset = fdt_path_offset(fdt, "/chosen");
712 if (offset < 0)
713 offset = fdt_path_offset(fdt, "/chosen@0");
714 if (offset < 0)
715 return -ENOENT;
717 p = fdt_getprop(fdt, offset, "stdout-path", &l);
718 if (!p)
719 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
720 if (!p || !l)
721 return -ENOENT;
723 /* Get the node specified by stdout-path */
724 offset = fdt_path_offset(fdt, p);
725 if (offset < 0)
726 return -ENODEV;
728 while (match->compatible) {
729 unsigned long addr;
730 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
731 match++;
732 continue;
735 addr = fdt_translate_address(fdt, offset);
736 if (!addr)
737 return -ENXIO;
739 of_setup_earlycon(addr, match->data);
740 return 0;
742 return -ENODEV;
745 static int __init setup_of_earlycon(char *buf)
747 if (buf)
748 return 0;
750 return early_init_dt_scan_chosen_serial();
752 early_param("earlycon", setup_of_earlycon);
753 #endif
756 * early_init_dt_scan_root - fetch the top level address and size cells
758 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
759 int depth, void *data)
761 const __be32 *prop;
763 if (depth != 0)
764 return 0;
766 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
767 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
769 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
770 if (prop)
771 dt_root_size_cells = be32_to_cpup(prop);
772 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
774 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
775 if (prop)
776 dt_root_addr_cells = be32_to_cpup(prop);
777 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
779 /* break now */
780 return 1;
783 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
785 const __be32 *p = *cellp;
787 *cellp = p + s;
788 return of_read_number(p, s);
792 * early_init_dt_scan_memory - Look for an parse memory nodes
794 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
795 int depth, void *data)
797 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
798 const __be32 *reg, *endp;
799 int l;
801 /* We are scanning "memory" nodes only */
802 if (type == NULL) {
804 * The longtrail doesn't have a device_type on the
805 * /memory node, so look for the node called /memory@0.
807 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
808 return 0;
809 } else if (strcmp(type, "memory") != 0)
810 return 0;
812 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
813 if (reg == NULL)
814 reg = of_get_flat_dt_prop(node, "reg", &l);
815 if (reg == NULL)
816 return 0;
818 endp = reg + (l / sizeof(__be32));
820 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
821 uname, l, reg[0], reg[1], reg[2], reg[3]);
823 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
824 u64 base, size;
826 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
827 size = dt_mem_next_cell(dt_root_size_cells, &reg);
829 if (size == 0)
830 continue;
831 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
832 (unsigned long long)size);
834 early_init_dt_add_memory_arch(base, size);
837 return 0;
840 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
841 int depth, void *data)
843 int l;
844 const char *p;
846 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
848 if (depth != 1 || !data ||
849 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
850 return 0;
852 early_init_dt_check_for_initrd(node);
854 /* Retrieve command line */
855 p = of_get_flat_dt_prop(node, "bootargs", &l);
856 if (p != NULL && l > 0)
857 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
860 * CONFIG_CMDLINE is meant to be a default in case nothing else
861 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
862 * is set in which case we override whatever was found earlier.
864 #ifdef CONFIG_CMDLINE
865 #ifndef CONFIG_CMDLINE_FORCE
866 if (!((char *)data)[0])
867 #endif
868 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
869 #endif /* CONFIG_CMDLINE */
871 pr_debug("Command line is: %s\n", (char*)data);
873 /* break now */
874 return 1;
877 #ifdef CONFIG_HAVE_MEMBLOCK
878 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
880 const u64 phys_offset = __pa(PAGE_OFFSET);
881 base &= PAGE_MASK;
882 size &= PAGE_MASK;
884 if (sizeof(phys_addr_t) < sizeof(u64)) {
885 if (base > ULONG_MAX) {
886 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
887 base, base + size);
888 return;
891 if (base + size > ULONG_MAX) {
892 pr_warning("Ignoring memory range 0x%lx - 0x%llx\n",
893 ULONG_MAX, base + size);
894 size = ULONG_MAX - base;
898 if (base + size < phys_offset) {
899 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
900 base, base + size);
901 return;
903 if (base < phys_offset) {
904 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
905 base, phys_offset);
906 size -= phys_offset - base;
907 base = phys_offset;
909 memblock_add(base, size);
912 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
913 phys_addr_t size, bool nomap)
915 if (memblock_is_region_reserved(base, size))
916 return -EBUSY;
917 if (nomap)
918 return memblock_remove(base, size);
919 return memblock_reserve(base, size);
923 * called from unflatten_device_tree() to bootstrap devicetree itself
924 * Architectures can override this definition if memblock isn't used
926 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
928 return __va(memblock_alloc(size, align));
930 #else
931 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
932 phys_addr_t size, bool nomap)
934 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
935 &base, &size, nomap ? " (nomap)" : "");
936 return -ENOSYS;
938 #endif
940 bool __init early_init_dt_scan(void *params)
942 if (!params)
943 return false;
945 /* Setup flat device-tree pointer */
946 initial_boot_params = params;
948 /* check device tree validity */
949 if (fdt_check_header(params)) {
950 initial_boot_params = NULL;
951 return false;
954 /* Retrieve various information from the /chosen node */
955 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
957 /* Initialize {size,address}-cells info */
958 of_scan_flat_dt(early_init_dt_scan_root, NULL);
960 /* Setup memory, calling early_init_dt_add_memory_arch */
961 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
963 return true;
967 * unflatten_device_tree - create tree of device_nodes from flat blob
969 * unflattens the device-tree passed by the firmware, creating the
970 * tree of struct device_node. It also fills the "name" and "type"
971 * pointers of the nodes so the normal device-tree walking functions
972 * can be used.
974 void __init unflatten_device_tree(void)
976 __unflatten_device_tree(initial_boot_params, &of_allnodes,
977 early_init_dt_alloc_memory_arch);
979 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
980 of_alias_scan(early_init_dt_alloc_memory_arch);
984 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
986 * Copies and unflattens the device-tree passed by the firmware, creating the
987 * tree of struct device_node. It also fills the "name" and "type"
988 * pointers of the nodes so the normal device-tree walking functions
989 * can be used. This should only be used when the FDT memory has not been
990 * reserved such is the case when the FDT is built-in to the kernel init
991 * section. If the FDT memory is reserved already then unflatten_device_tree
992 * should be used instead.
994 void __init unflatten_and_copy_device_tree(void)
996 int size;
997 void *dt;
999 if (!initial_boot_params) {
1000 pr_warn("No valid device tree found, continuing without\n");
1001 return;
1004 size = fdt_totalsize(initial_boot_params);
1005 dt = early_init_dt_alloc_memory_arch(size,
1006 roundup_pow_of_two(FDT_V17_SIZE));
1008 if (dt) {
1009 memcpy(dt, initial_boot_params, size);
1010 initial_boot_params = dt;
1012 unflatten_device_tree();
1015 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1016 static struct debugfs_blob_wrapper flat_dt_blob;
1018 static int __init of_flat_dt_debugfs_export_fdt(void)
1020 struct dentry *d = debugfs_create_dir("device-tree", NULL);
1022 if (!d)
1023 return -ENOENT;
1025 flat_dt_blob.data = initial_boot_params;
1026 flat_dt_blob.size = fdt_totalsize(initial_boot_params);
1028 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1029 d, &flat_dt_blob);
1030 if (!d)
1031 return -ENOENT;
1033 return 0;
1035 module_init(of_flat_dt_debugfs_export_fdt);
1036 #endif
1038 #endif /* CONFIG_OF_EARLY_FLATTREE */