]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/of/fdt.c
of: make of_fdt_is_compatible() static
[mirror_ubuntu-jammy-kernel.git] / drivers / of / fdt.c
CommitLineData
e169cfbe
GL
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
bd0096d7 12#define pr_fmt(fmt) "OF: fdt: " fmt
606ad42a 13
08d53aa5 14#include <linux/crc32.h>
41f88009 15#include <linux/kernel.h>
f7b3a835 16#include <linux/initrd.h>
a1727da5 17#include <linux/memblock.h>
f8062386 18#include <linux/mutex.h>
e169cfbe
GL
19#include <linux/of.h>
20#include <linux/of_fdt.h>
3f0c8206 21#include <linux/of_reserved_mem.h>
e8d9d1f5 22#include <linux/sizes.h>
4ef7b373
JK
23#include <linux/string.h>
24#include <linux/errno.h>
fe140423 25#include <linux/slab.h>
e6a6928c 26#include <linux/libfdt.h>
b0a6fb36 27#include <linux/debugfs.h>
fb11ffe7 28#include <linux/serial_core.h>
08d53aa5 29#include <linux/sysfs.h>
51975db0 30
c89810ac 31#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
4ef7b373
JK
32#include <asm/page.h>
33
81d0848f
FR
34#include "of_private.h"
35
704033ce
LA
36/*
37 * of_fdt_limit_memory - limit the number of regions in the /memory node
38 * @limit: maximum entries
39 *
40 * Adjust the flattened device tree to have at most 'limit' number of
41 * memory entries in the /memory node. This function may be called
42 * any time after initial_boot_param is set.
43 */
44void of_fdt_limit_memory(int limit)
45{
46 int memory;
47 int len;
48 const void *val;
49 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
50 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
17a70355
RH
51 const __be32 *addr_prop;
52 const __be32 *size_prop;
704033ce
LA
53 int root_offset;
54 int cell_size;
55
56 root_offset = fdt_path_offset(initial_boot_params, "/");
57 if (root_offset < 0)
58 return;
59
60 addr_prop = fdt_getprop(initial_boot_params, root_offset,
61 "#address-cells", NULL);
62 if (addr_prop)
63 nr_address_cells = fdt32_to_cpu(*addr_prop);
64
65 size_prop = fdt_getprop(initial_boot_params, root_offset,
66 "#size-cells", NULL);
67 if (size_prop)
68 nr_size_cells = fdt32_to_cpu(*size_prop);
69
70 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
71
72 memory = fdt_path_offset(initial_boot_params, "/memory");
73 if (memory > 0) {
74 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
75 if (len > limit*cell_size) {
76 len = limit*cell_size;
77 pr_debug("Limiting number of entries to %d\n", limit);
78 fdt_setprop(initial_boot_params, memory, "reg", val,
79 len);
80 }
81 }
82}
83
9706a36e
SN
84/**
85 * of_fdt_is_compatible - Return true if given node from the given blob has
86 * compat in its compatible list
87 * @blob: A device tree blob
88 * @node: node to test
89 * @compat: compatible string to compare with compatible list.
a4f740cf
GL
90 *
91 * On match, returns a non-zero value with smaller values returned for more
92 * specific compatible values.
9706a36e 93 */
92af0899 94static int of_fdt_is_compatible(const void *blob,
9706a36e
SN
95 unsigned long node, const char *compat)
96{
97 const char *cp;
9d0c4dfe
RH
98 int cplen;
99 unsigned long l, score = 0;
9706a36e 100
e6a6928c 101 cp = fdt_getprop(blob, node, "compatible", &cplen);
9706a36e
SN
102 if (cp == NULL)
103 return 0;
104 while (cplen > 0) {
a4f740cf 105 score++;
9706a36e 106 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
a4f740cf 107 return score;
9706a36e
SN
108 l = strlen(cp) + 1;
109 cp += l;
110 cplen -= l;
111 }
112
113 return 0;
114}
115
cc783786
KC
116/**
117 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
118 * @blob: A device tree blob
119 * @node: node to test
120 *
121 * Returns true if the node has a "big-endian" property, or if the kernel
122 * was compiled for BE *and* the node has a "native-endian" property.
123 * Returns false otherwise.
124 */
125bool of_fdt_is_big_endian(const void *blob, unsigned long node)
126{
127 if (fdt_getprop(blob, node, "big-endian", NULL))
128 return true;
129 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
130 fdt_getprop(blob, node, "native-endian", NULL))
131 return true;
132 return false;
133}
134
a4f740cf
GL
135/**
136 * of_fdt_match - Return true if node matches a list of compatible values
137 */
c972de14 138int of_fdt_match(const void *blob, unsigned long node,
7b482c83 139 const char *const *compat)
a4f740cf
GL
140{
141 unsigned int tmp, score = 0;
142
143 if (!compat)
144 return 0;
145
146 while (*compat) {
147 tmp = of_fdt_is_compatible(blob, node, *compat);
148 if (tmp && (score == 0 || (tmp < score)))
149 score = tmp;
150 compat++;
151 }
152
153 return score;
154}
155
44856819 156static void *unflatten_dt_alloc(void **mem, unsigned long size,
bbd33931
GL
157 unsigned long align)
158{
159 void *res;
160
44856819
GL
161 *mem = PTR_ALIGN(*mem, align);
162 res = *mem;
bbd33931
GL
163 *mem += size;
164
165 return res;
166}
167
dfbd4c6e
GS
168static void populate_properties(const void *blob,
169 int offset,
170 void **mem,
171 struct device_node *np,
172 const char *nodename,
5063e25a 173 bool dryrun)
bbd33931 174{
dfbd4c6e
GS
175 struct property *pp, **pprev = NULL;
176 int cur;
177 bool has_name = false;
178
179 pprev = &np->properties;
180 for (cur = fdt_first_property_offset(blob, offset);
181 cur >= 0;
182 cur = fdt_next_property_offset(blob, cur)) {
183 const __be32 *val;
184 const char *pname;
185 u32 sz;
186
187 val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
188 if (!val) {
606ad42a 189 pr_warn("Cannot locate property at 0x%x\n", cur);
dfbd4c6e
GS
190 continue;
191 }
192
193 if (!pname) {
606ad42a 194 pr_warn("Cannot find property name at 0x%x\n", cur);
dfbd4c6e
GS
195 continue;
196 }
197
198 if (!strcmp(pname, "name"))
199 has_name = true;
200
201 pp = unflatten_dt_alloc(mem, sizeof(struct property),
202 __alignof__(struct property));
203 if (dryrun)
204 continue;
205
206 /* We accept flattened tree phandles either in
207 * ePAPR-style "phandle" properties, or the
208 * legacy "linux,phandle" properties. If both
209 * appear and have different values, things
210 * will get weird. Don't do that.
211 */
212 if (!strcmp(pname, "phandle") ||
213 !strcmp(pname, "linux,phandle")) {
214 if (!np->phandle)
215 np->phandle = be32_to_cpup(val);
216 }
217
218 /* And we process the "ibm,phandle" property
219 * used in pSeries dynamic device tree
220 * stuff
221 */
222 if (!strcmp(pname, "ibm,phandle"))
223 np->phandle = be32_to_cpup(val);
224
225 pp->name = (char *)pname;
226 pp->length = sz;
227 pp->value = (__be32 *)val;
228 *pprev = pp;
229 pprev = &pp->next;
230 }
231
232 /* With version 0x10 we may not have the name property,
233 * recreate it here from the unit name if absent
234 */
235 if (!has_name) {
236 const char *p = nodename, *ps = p, *pa = NULL;
237 int len;
238
239 while (*p) {
240 if ((*p) == '@')
241 pa = p;
242 else if ((*p) == '/')
243 ps = p + 1;
244 p++;
245 }
246
247 if (pa < ps)
248 pa = p;
249 len = (pa - ps) + 1;
250 pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
251 __alignof__(struct property));
252 if (!dryrun) {
253 pp->name = "name";
254 pp->length = len;
255 pp->value = pp + 1;
256 *pprev = pp;
257 pprev = &pp->next;
258 memcpy(pp->value, ps, len - 1);
259 ((char *)pp->value)[len - 1] = 0;
260 pr_debug("fixed up name for %s -> %s\n",
261 nodename, (char *)pp->value);
262 }
263 }
264
265 if (!dryrun)
266 *pprev = NULL;
267}
268
dddc33e5
GS
269static unsigned int populate_node(const void *blob,
270 int offset,
271 void **mem,
272 struct device_node *dad,
273 unsigned int fpsize,
274 struct device_node **pnp,
275 bool dryrun)
dfbd4c6e 276{
bbd33931 277 struct device_node *np;
e6a6928c 278 const char *pathp;
bbd33931 279 unsigned int l, allocl;
bbd33931
GL
280 int new_format = 0;
281
dfbd4c6e
GS
282 pathp = fdt_get_name(blob, offset, &l);
283 if (!pathp) {
284 *pnp = NULL;
285 return 0;
286 }
e6a6928c 287
05f4647b 288 allocl = ++l;
bbd33931
GL
289
290 /* version 0x10 has a more compact unit name here instead of the full
291 * path. we accumulate the full path size using "fpsize", we'll rebuild
292 * it later. We detect this because the first character of the name is
293 * not '/'.
294 */
295 if ((*pathp) != '/') {
296 new_format = 1;
297 if (fpsize == 0) {
298 /* root node: special case. fpsize accounts for path
299 * plus terminating zero. root node only has '/', so
300 * fpsize should be 2, but we want to avoid the first
301 * level nodes to have two '/' so we use fpsize 1 here
302 */
303 fpsize = 1;
304 allocl = 2;
0fca5dea 305 l = 1;
e6a6928c 306 pathp = "";
bbd33931
GL
307 } else {
308 /* account for '/' and path size minus terminal 0
309 * already in 'l'
310 */
311 fpsize += l;
312 allocl = fpsize;
313 }
314 }
315
dfbd4c6e 316 np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
bbd33931 317 __alignof__(struct device_node));
5063e25a 318 if (!dryrun) {
c22618a1 319 char *fn;
0829f6d1 320 of_node_init(np);
c22618a1 321 np->full_name = fn = ((char *)np) + sizeof(*np);
bbd33931 322 if (new_format) {
bbd33931
GL
323 /* rebuild full path for new format */
324 if (dad && dad->parent) {
325 strcpy(fn, dad->full_name);
326#ifdef DEBUG
327 if ((strlen(fn) + l + 1) != allocl) {
328 pr_debug("%s: p: %d, l: %d, a: %d\n",
329 pathp, (int)strlen(fn),
330 l, allocl);
331 }
332#endif
333 fn += strlen(fn);
334 }
335 *(fn++) = '/';
c22618a1
GL
336 }
337 memcpy(fn, pathp, l);
338
bbd33931
GL
339 if (dad != NULL) {
340 np->parent = dad;
70161ff3
GL
341 np->sibling = dad->child;
342 dad->child = np;
bbd33931 343 }
bbd33931 344 }
e6a6928c 345
dfbd4c6e 346 populate_properties(blob, offset, mem, np, pathp, dryrun);
5063e25a 347 if (!dryrun) {
bbd33931
GL
348 np->name = of_get_property(np, "name", NULL);
349 np->type = of_get_property(np, "device_type", NULL);
350
351 if (!np->name)
352 np->name = "<NULL>";
353 if (!np->type)
354 np->type = "<NULL>";
355 }
e6a6928c 356
dfbd4c6e
GS
357 *pnp = np;
358 return fpsize;
359}
360
50800082
GS
361static void reverse_nodes(struct device_node *parent)
362{
363 struct device_node *child, *next;
364
365 /* In-depth first */
366 child = parent->child;
367 while (child) {
368 reverse_nodes(child);
369
370 child = child->sibling;
371 }
372
373 /* Reverse the nodes in the child list */
374 child = parent->child;
375 parent->child = NULL;
376 while (child) {
377 next = child->sibling;
378
379 child->sibling = parent->child;
380 parent->child = child;
381 child = next;
382 }
383}
384
dfbd4c6e 385/**
947c82cb 386 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
dfbd4c6e
GS
387 * @blob: The parent device tree blob
388 * @mem: Memory chunk to use for allocating device nodes and properties
dfbd4c6e
GS
389 * @dad: Parent struct device_node
390 * @nodepp: The device_node tree created by the call
50800082
GS
391 *
392 * It returns the size of unflattened device tree or error code
dfbd4c6e 393 */
947c82cb
GS
394static int unflatten_dt_nodes(const void *blob,
395 void *mem,
396 struct device_node *dad,
397 struct device_node **nodepp)
dfbd4c6e 398{
50800082 399 struct device_node *root;
8c237cd0 400 int offset = 0, depth = 0, initial_depth = 0;
50800082 401#define FDT_MAX_DEPTH 64
dddc33e5 402 unsigned int fpsizes[FDT_MAX_DEPTH];
50800082
GS
403 struct device_node *nps[FDT_MAX_DEPTH];
404 void *base = mem;
405 bool dryrun = !base;
dfbd4c6e 406
50800082
GS
407 if (nodepp)
408 *nodepp = NULL;
409
8c237cd0
GS
410 /*
411 * We're unflattening device sub-tree if @dad is valid. There are
412 * possibly multiple nodes in the first level of depth. We need
413 * set @depth to 1 to make fdt_next_node() happy as it bails
414 * immediately when negative @depth is found. Otherwise, the device
415 * nodes except the first one won't be unflattened successfully.
416 */
417 if (dad)
418 depth = initial_depth = 1;
419
50800082
GS
420 root = dad;
421 fpsizes[depth] = dad ? strlen(of_node_full_name(dad)) : 0;
78c44d91 422 nps[depth] = dad;
8c237cd0 423
50800082 424 for (offset = 0;
8c237cd0 425 offset >= 0 && depth >= initial_depth;
50800082
GS
426 offset = fdt_next_node(blob, offset, &depth)) {
427 if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
428 continue;
dfbd4c6e 429
78c44d91
RK
430 fpsizes[depth+1] = populate_node(blob, offset, &mem,
431 nps[depth],
432 fpsizes[depth],
433 &nps[depth+1], dryrun);
434 if (!fpsizes[depth+1])
50800082
GS
435 return mem - base;
436
437 if (!dryrun && nodepp && !*nodepp)
78c44d91 438 *nodepp = nps[depth+1];
50800082 439 if (!dryrun && !root)
78c44d91 440 root = nps[depth+1];
50800082 441 }
e6a6928c 442
50800082 443 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
606ad42a 444 pr_err("Error %d processing FDT\n", offset);
50800082
GS
445 return -EINVAL;
446 }
e6a6928c 447
70161ff3
GL
448 /*
449 * Reverse the child list. Some drivers assumes node order matches .dts
450 * node order
451 */
50800082
GS
452 if (!dryrun)
453 reverse_nodes(root);
e6a6928c 454
50800082 455 return mem - base;
bbd33931 456}
41f88009 457
fe140423
SN
458/**
459 * __unflatten_device_tree - create tree of device_nodes from flat blob
460 *
461 * unflattens a device-tree, creating the
462 * tree of struct device_node. It also fills the "name" and "type"
463 * pointers of the nodes so the normal device-tree walking functions
464 * can be used.
465 * @blob: The blob to expand
c4263233 466 * @dad: Parent device node
fe140423
SN
467 * @mynodes: The device_node tree created by the call
468 * @dt_alloc: An allocator that provides a virtual address to memory
469 * for the resulting tree
83262418
GS
470 *
471 * Returns NULL on failure or the memory chunk containing the unflattened
472 * device tree on success.
fe140423 473 */
81d0848f
FR
474void *__unflatten_device_tree(const void *blob,
475 struct device_node *dad,
476 struct device_node **mynodes,
477 void *(*dt_alloc)(u64 size, u64 align),
478 bool detached)
fe140423 479{
50800082 480 int size;
e6a6928c 481 void *mem;
fe140423
SN
482
483 pr_debug(" -> unflatten_device_tree()\n");
484
485 if (!blob) {
486 pr_debug("No device tree pointer\n");
83262418 487 return NULL;
fe140423
SN
488 }
489
490 pr_debug("Unflattening device tree:\n");
c972de14
RH
491 pr_debug("magic: %08x\n", fdt_magic(blob));
492 pr_debug("size: %08x\n", fdt_totalsize(blob));
493 pr_debug("version: %08x\n", fdt_version(blob));
fe140423 494
c972de14 495 if (fdt_check_header(blob)) {
fe140423 496 pr_err("Invalid device tree blob header\n");
83262418 497 return NULL;
fe140423
SN
498 }
499
500 /* First pass, scan for size */
c4263233 501 size = unflatten_dt_nodes(blob, NULL, dad, NULL);
50800082 502 if (size < 0)
83262418 503 return NULL;
fe140423 504
50800082
GS
505 size = ALIGN(size, 4);
506 pr_debug(" size is %d, allocating...\n", size);
fe140423
SN
507
508 /* Allocate memory for the expanded device tree */
44856819
GL
509 mem = dt_alloc(size + 4, __alignof__(struct device_node));
510 memset(mem, 0, size);
fe140423 511
44856819 512 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
9e401275 513
44856819 514 pr_debug(" unflattening %p...\n", mem);
fe140423
SN
515
516 /* Second pass, do actual unflattening */
c4263233 517 unflatten_dt_nodes(blob, mem, dad, mynodes);
44856819 518 if (be32_to_cpup(mem + size) != 0xdeadbeef)
fe140423 519 pr_warning("End of tree marker overwritten: %08x\n",
44856819 520 be32_to_cpup(mem + size));
fe140423 521
89c67752 522 if (detached && mynodes) {
1d1bde55
MS
523 of_node_set_flag(*mynodes, OF_DETACHED);
524 pr_debug("unflattened tree is detached\n");
525 }
526
fe140423 527 pr_debug(" <- unflatten_device_tree()\n");
83262418 528 return mem;
fe140423
SN
529}
530
531static void *kernel_tree_alloc(u64 size, u64 align)
532{
533 return kzalloc(size, GFP_KERNEL);
534}
535
f8062386
GR
536static DEFINE_MUTEX(of_fdt_unflatten_mutex);
537
fe140423
SN
538/**
539 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
c4263233
GS
540 * @blob: Flat device tree blob
541 * @dad: Parent device node
542 * @mynodes: The device tree created by the call
fe140423
SN
543 *
544 * unflattens the device-tree passed by the firmware, creating the
545 * tree of struct device_node. It also fills the "name" and "type"
546 * pointers of the nodes so the normal device-tree walking functions
547 * can be used.
83262418
GS
548 *
549 * Returns NULL on failure or the memory chunk containing the unflattened
550 * device tree on success.
fe140423 551 */
83262418
GS
552void *of_fdt_unflatten_tree(const unsigned long *blob,
553 struct device_node *dad,
554 struct device_node **mynodes)
fe140423 555{
83262418
GS
556 void *mem;
557
f8062386 558 mutex_lock(&of_fdt_unflatten_mutex);
1d1bde55
MS
559 mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
560 true);
f8062386 561 mutex_unlock(&of_fdt_unflatten_mutex);
83262418
GS
562
563 return mem;
fe140423
SN
564}
565EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
566
57d00ecf
SN
567/* Everything below here references initial_boot_params directly. */
568int __initdata dt_root_addr_cells;
569int __initdata dt_root_size_cells;
570
1daa0c4c 571void *initial_boot_params;
57d00ecf
SN
572
573#ifdef CONFIG_OF_EARLY_FLATTREE
574
08d53aa5
AB
575static u32 of_fdt_crc32;
576
e8d9d1f5
MS
577/**
578 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
579 */
580static int __init __reserved_mem_reserve_reg(unsigned long node,
581 const char *uname)
582{
583 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
584 phys_addr_t base, size;
9d0c4dfe
RH
585 int len;
586 const __be32 *prop;
3f0c8206 587 int nomap, first = 1;
e8d9d1f5
MS
588
589 prop = of_get_flat_dt_prop(node, "reg", &len);
590 if (!prop)
591 return -ENOENT;
592
593 if (len && len % t_len != 0) {
594 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
595 uname);
596 return -EINVAL;
597 }
598
599 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
600
601 while (len >= t_len) {
602 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
603 size = dt_mem_next_cell(dt_root_size_cells, &prop);
604
b5f2a8c0 605 if (size &&
e8d9d1f5
MS
606 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
607 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
608 uname, &base, (unsigned long)size / SZ_1M);
609 else
610 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
611 uname, &base, (unsigned long)size / SZ_1M);
612
613 len -= t_len;
3f0c8206
MS
614 if (first) {
615 fdt_reserved_mem_save_node(node, uname, base, size);
616 first = 0;
617 }
e8d9d1f5
MS
618 }
619 return 0;
620}
621
622/**
623 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
624 * in /reserved-memory matches the values supported by the current implementation,
625 * also check if ranges property has been provided
626 */
5b624118 627static int __init __reserved_mem_check_root(unsigned long node)
e8d9d1f5 628{
9d0c4dfe 629 const __be32 *prop;
e8d9d1f5
MS
630
631 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
632 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
633 return -EINVAL;
634
635 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
636 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
637 return -EINVAL;
638
639 prop = of_get_flat_dt_prop(node, "ranges", NULL);
640 if (!prop)
641 return -EINVAL;
642 return 0;
643}
644
645/**
646 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
647 */
648static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
649 int depth, void *data)
650{
651 static int found;
652 const char *status;
3f0c8206 653 int err;
e8d9d1f5
MS
654
655 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
656 if (__reserved_mem_check_root(node) != 0) {
657 pr_err("Reserved memory: unsupported node format, ignoring\n");
658 /* break scan */
659 return 1;
660 }
661 found = 1;
662 /* scan next node */
663 return 0;
664 } else if (!found) {
665 /* scan next node */
666 return 0;
667 } else if (found && depth < 2) {
668 /* scanning of /reserved-memory has been finished */
669 return 1;
670 }
671
672 status = of_get_flat_dt_prop(node, "status", NULL);
673 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
674 return 0;
675
3f0c8206
MS
676 err = __reserved_mem_reserve_reg(node, uname);
677 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
678 fdt_reserved_mem_save_node(node, uname, 0, 0);
e8d9d1f5
MS
679
680 /* scan next node */
681 return 0;
682}
683
684/**
685 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
686 *
687 * This function grabs memory from early allocator for device exclusive use
688 * defined in device tree structures. It should be called by arch specific code
689 * once the early allocator (i.e. memblock) has been fully activated.
690 */
691void __init early_init_fdt_scan_reserved_mem(void)
692{
d1552ce4
RH
693 int n;
694 u64 base, size;
695
2040b527
JC
696 if (!initial_boot_params)
697 return;
698
d1552ce4
RH
699 /* Process header /memreserve/ fields */
700 for (n = 0; ; n++) {
701 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
702 if (!size)
703 break;
704 early_init_dt_reserve_memory_arch(base, size, 0);
705 }
706
e8d9d1f5 707 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
3f0c8206 708 fdt_init_reserved_mem();
e8d9d1f5
MS
709}
710
24bbd929
AB
711/**
712 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
713 */
714void __init early_init_fdt_reserve_self(void)
715{
716 if (!initial_boot_params)
717 return;
718
719 /* Reserve the dtb region */
720 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
721 fdt_totalsize(initial_boot_params),
722 0);
723}
724
57d00ecf
SN
725/**
726 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
727 * @it: callback function
728 * @data: context data pointer
729 *
730 * This function is used to scan the flattened device-tree, it is
731 * used to extract the memory information at boot before we can
732 * unflatten the tree
733 */
734int __init of_scan_flat_dt(int (*it)(unsigned long node,
735 const char *uname, int depth,
736 void *data),
737 void *data)
738{
e6a6928c
RH
739 const void *blob = initial_boot_params;
740 const char *pathp;
741 int offset, rc = 0, depth = -1;
742
3ec75441
TW
743 if (!blob)
744 return 0;
745
746 for (offset = fdt_next_node(blob, -1, &depth);
747 offset >= 0 && depth >= 0 && !rc;
748 offset = fdt_next_node(blob, offset, &depth)) {
e6a6928c
RH
749
750 pathp = fdt_get_name(blob, offset, NULL);
375da3a7
AS
751 if (*pathp == '/')
752 pathp = kbasename(pathp);
e6a6928c
RH
753 rc = it(offset, pathp, depth, data);
754 }
57d00ecf
SN
755 return rc;
756}
757
ea47dd19
NP
758/**
759 * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
760 * @it: callback function
761 * @data: context data pointer
762 *
763 * This function is used to scan sub-nodes of a node.
764 */
765int __init of_scan_flat_dt_subnodes(unsigned long parent,
766 int (*it)(unsigned long node,
767 const char *uname,
768 void *data),
769 void *data)
770{
771 const void *blob = initial_boot_params;
772 int node;
773
774 fdt_for_each_subnode(node, blob, parent) {
775 const char *pathp;
776 int rc;
777
778 pathp = fdt_get_name(blob, node, NULL);
779 if (*pathp == '/')
780 pathp = kbasename(pathp);
781 rc = it(node, pathp, data);
782 if (rc)
783 return rc;
784 }
785 return 0;
786}
787
9c609868
SZ
788/**
789 * of_get_flat_dt_subnode_by_name - get the subnode by given name
790 *
791 * @node: the parent node
792 * @uname: the name of subnode
793 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
794 */
795
796int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
797{
798 return fdt_subnode_offset(initial_boot_params, node, uname);
799}
800
57d00ecf
SN
801/**
802 * of_get_flat_dt_root - find the root node in the flat blob
803 */
804unsigned long __init of_get_flat_dt_root(void)
805{
e6a6928c 806 return 0;
57d00ecf
SN
807}
808
c0556d3f
RH
809/**
810 * of_get_flat_dt_size - Return the total size of the FDT
811 */
812int __init of_get_flat_dt_size(void)
813{
814 return fdt_totalsize(initial_boot_params);
815}
816
57d00ecf
SN
817/**
818 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
819 *
820 * This function can be used within scan_flattened_dt callback to get
821 * access to properties
822 */
9d0c4dfe
RH
823const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
824 int *size)
57d00ecf 825{
e6a6928c 826 return fdt_getprop(initial_boot_params, node, name, size);
57d00ecf
SN
827}
828
829/**
830 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
831 * @node: node to test
832 * @compat: compatible string to compare with compatible list.
833 */
834int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
835{
836 return of_fdt_is_compatible(initial_boot_params, node, compat);
837}
838
a4f740cf
GL
839/**
840 * of_flat_dt_match - Return true if node matches a list of compatible values
841 */
7b482c83 842int __init of_flat_dt_match(unsigned long node, const char *const *compat)
a4f740cf
GL
843{
844 return of_fdt_match(initial_boot_params, node, compat);
845}
846
ea47dd19
NP
847/**
848 * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
849 */
850uint32_t __init of_get_flat_dt_phandle(unsigned long node)
851{
852 return fdt_get_phandle(initial_boot_params, node);
853}
854
57d74bcf
MS
855struct fdt_scan_status {
856 const char *name;
857 int namelen;
858 int depth;
859 int found;
860 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
861 void *data;
862};
863
6a903a25
RH
864const char * __init of_flat_dt_get_machine_name(void)
865{
866 const char *name;
867 unsigned long dt_root = of_get_flat_dt_root();
868
869 name = of_get_flat_dt_prop(dt_root, "model", NULL);
870 if (!name)
871 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
872 return name;
873}
874
875/**
876 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
877 *
878 * @default_match: A machine specific ptr to return in case of no match.
879 * @get_next_compat: callback function to return next compatible match table.
880 *
881 * Iterate through machine match tables to find the best match for the machine
882 * compatible string in the FDT.
883 */
884const void * __init of_flat_dt_match_machine(const void *default_match,
885 const void * (*get_next_compat)(const char * const**))
886{
887 const void *data = NULL;
888 const void *best_data = default_match;
889 const char *const *compat;
890 unsigned long dt_root;
891 unsigned int best_score = ~1, score = 0;
892
893 dt_root = of_get_flat_dt_root();
894 while ((data = get_next_compat(&compat))) {
895 score = of_flat_dt_match(dt_root, compat);
896 if (score > 0 && score < best_score) {
897 best_data = data;
898 best_score = score;
899 }
900 }
901 if (!best_data) {
902 const char *prop;
9d0c4dfe 903 int size;
6a903a25
RH
904
905 pr_err("\n unrecognized device tree list:\n[ ");
906
907 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
908 if (prop) {
909 while (size > 0) {
910 printk("'%s' ", prop);
911 size -= strlen(prop) + 1;
912 prop += strlen(prop) + 1;
913 }
914 }
915 printk("]\n\n");
916 return NULL;
917 }
918
919 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
920
921 return best_data;
922}
923
f7b3a835 924#ifdef CONFIG_BLK_DEV_INITRD
369bc9ab
AB
925#ifndef __early_init_dt_declare_initrd
926static void __early_init_dt_declare_initrd(unsigned long start,
927 unsigned long end)
928{
929 initrd_start = (unsigned long)__va(start);
930 initrd_end = (unsigned long)__va(end);
931 initrd_below_start_ok = 1;
932}
933#endif
934
f7b3a835
GL
935/**
936 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
937 * @node: reference to node containing initrd location ('chosen')
938 */
29eb45a9 939static void __init early_init_dt_check_for_initrd(unsigned long node)
f7b3a835 940{
374d5c99 941 u64 start, end;
9d0c4dfe
RH
942 int len;
943 const __be32 *prop;
f7b3a835
GL
944
945 pr_debug("Looking for initrd properties... ");
946
947 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
1406bc2f
JK
948 if (!prop)
949 return;
374d5c99 950 start = of_read_number(prop, len/4);
1406bc2f
JK
951
952 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
953 if (!prop)
954 return;
374d5c99 955 end = of_read_number(prop, len/4);
f7b3a835 956
369bc9ab 957 __early_init_dt_declare_initrd(start, end);
29eb45a9 958
374d5c99
SS
959 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
960 (unsigned long long)start, (unsigned long long)end);
f7b3a835
GL
961}
962#else
29eb45a9 963static inline void early_init_dt_check_for_initrd(unsigned long node)
f7b3a835
GL
964{
965}
966#endif /* CONFIG_BLK_DEV_INITRD */
967
fb11ffe7 968#ifdef CONFIG_SERIAL_EARLYCON
fb11ffe7 969
d503187b 970int __init early_init_dt_scan_chosen_stdout(void)
fb11ffe7
RH
971{
972 int offset;
4d118c9a 973 const char *p, *q, *options = NULL;
fb11ffe7 974 int l;
2eaa7909 975 const struct earlycon_id *match;
fb11ffe7
RH
976 const void *fdt = initial_boot_params;
977
978 offset = fdt_path_offset(fdt, "/chosen");
979 if (offset < 0)
980 offset = fdt_path_offset(fdt, "/chosen@0");
981 if (offset < 0)
982 return -ENOENT;
983
984 p = fdt_getprop(fdt, offset, "stdout-path", &l);
985 if (!p)
986 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
987 if (!p || !l)
988 return -ENOENT;
989
4d118c9a
PH
990 q = strchrnul(p, ':');
991 if (*q != '\0')
992 options = q + 1;
0fcc286f 993 l = q - p;
6296ad9e 994
fb11ffe7 995 /* Get the node specified by stdout-path */
0fcc286f
PH
996 offset = fdt_path_offset_namelen(fdt, p, l);
997 if (offset < 0) {
998 pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
999 return 0;
1000 }
fb11ffe7 1001
2eaa7909 1002 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
2eaa7909
PH
1003 if (!match->compatible[0])
1004 continue;
1005
1006 if (fdt_node_check_compatible(fdt, offset, match->compatible))
fb11ffe7 1007 continue;
fb11ffe7 1008
c90fe9c0 1009 of_setup_earlycon(match, offset, options);
fb11ffe7
RH
1010 return 0;
1011 }
1012 return -ENODEV;
1013}
fb11ffe7
RH
1014#endif
1015
f00abd94
GL
1016/**
1017 * early_init_dt_scan_root - fetch the top level address and size cells
1018 */
1019int __init early_init_dt_scan_root(unsigned long node, const char *uname,
1020 int depth, void *data)
1021{
9d0c4dfe 1022 const __be32 *prop;
f00abd94
GL
1023
1024 if (depth != 0)
1025 return 0;
1026
33714881
JK
1027 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
1028 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
1029
f00abd94 1030 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
33714881
JK
1031 if (prop)
1032 dt_root_size_cells = be32_to_cpup(prop);
f00abd94
GL
1033 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
1034
1035 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
33714881
JK
1036 if (prop)
1037 dt_root_addr_cells = be32_to_cpup(prop);
f00abd94
GL
1038 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1039
1040 /* break now */
1041 return 1;
1042}
1043
9d0c4dfe 1044u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
83f7a06e 1045{
9d0c4dfe 1046 const __be32 *p = *cellp;
83f7a06e
GL
1047
1048 *cellp = p + s;
1049 return of_read_number(p, s);
1050}
1051
51975db0
GL
1052/**
1053 * early_init_dt_scan_memory - Look for an parse memory nodes
1054 */
1055int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
1056 int depth, void *data)
1057{
9d0c4dfe
RH
1058 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1059 const __be32 *reg, *endp;
1060 int l;
41a9ada3 1061 bool hotpluggable;
51975db0
GL
1062
1063 /* We are scanning "memory" nodes only */
1064 if (type == NULL) {
1065 /*
1066 * The longtrail doesn't have a device_type on the
1067 * /memory node, so look for the node called /memory@0.
1068 */
b44aa25d 1069 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
51975db0
GL
1070 return 0;
1071 } else if (strcmp(type, "memory") != 0)
1072 return 0;
1073
1074 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1075 if (reg == NULL)
1076 reg = of_get_flat_dt_prop(node, "reg", &l);
1077 if (reg == NULL)
1078 return 0;
1079
1080 endp = reg + (l / sizeof(__be32));
41a9ada3 1081 hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
51975db0 1082
c954b36e 1083 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
51975db0
GL
1084
1085 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1086 u64 base, size;
1087
1088 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1089 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1090
1091 if (size == 0)
1092 continue;
1093 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
1094 (unsigned long long)size);
1095
1096 early_init_dt_add_memory_arch(base, size);
41a9ada3
RA
1097
1098 if (!hotpluggable)
1099 continue;
1100
1101 if (early_init_dt_mark_hotplug_memory_arch(base, size))
1102 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1103 base, base + size);
51975db0
GL
1104 }
1105
1106 return 0;
1107}
1108
86e03221
GL
1109int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
1110 int depth, void *data)
1111{
9d0c4dfe
RH
1112 int l;
1113 const char *p;
86e03221
GL
1114
1115 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1116
85f60ae4 1117 if (depth != 1 || !data ||
86e03221
GL
1118 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1119 return 0;
1120
1121 early_init_dt_check_for_initrd(node);
1122
25985edc 1123 /* Retrieve command line */
86e03221
GL
1124 p = of_get_flat_dt_prop(node, "bootargs", &l);
1125 if (p != NULL && l > 0)
85f60ae4 1126 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
86e03221 1127
78b782cb
BH
1128 /*
1129 * CONFIG_CMDLINE is meant to be a default in case nothing else
1130 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1131 * is set in which case we override whatever was found earlier.
1132 */
86e03221 1133#ifdef CONFIG_CMDLINE
34b82026
MU
1134#if defined(CONFIG_CMDLINE_EXTEND)
1135 strlcat(data, " ", COMMAND_LINE_SIZE);
1136 strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1137#elif defined(CONFIG_CMDLINE_FORCE)
1138 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1139#else
1140 /* No arguments from boot loader, use kernel's cmdl*/
78b782cb 1141 if (!((char *)data)[0])
85f60ae4 1142 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
34b82026 1143#endif
86e03221
GL
1144#endif /* CONFIG_CMDLINE */
1145
85f60ae4 1146 pr_debug("Command line is: %s\n", (char*)data);
86e03221
GL
1147
1148 /* break now */
1149 return 1;
1150}
1151
a1727da5 1152#ifdef CONFIG_HAVE_MEMBLOCK
270522a0
AB
1153#ifndef MIN_MEMBLOCK_ADDR
1154#define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1155#endif
8eafeb48
AB
1156#ifndef MAX_MEMBLOCK_ADDR
1157#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1158#endif
3069f0c0 1159
068f6310
RH
1160void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1161{
270522a0 1162 const u64 phys_offset = MIN_MEMBLOCK_ADDR;
8f73d4b7
GU
1163
1164 if (!PAGE_ALIGNED(base)) {
8cccffc5
AB
1165 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
1166 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1167 base, base + size);
1168 return;
1169 }
8f73d4b7
GU
1170 size -= PAGE_SIZE - (base & ~PAGE_MASK);
1171 base = PAGE_ALIGN(base);
1172 }
068f6310 1173 size &= PAGE_MASK;
a67a6ed1 1174
8eafeb48 1175 if (base > MAX_MEMBLOCK_ADDR) {
3069f0c0
LA
1176 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1177 base, base + size);
1178 return;
1179 }
a67a6ed1 1180
8eafeb48 1181 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
9aacd602 1182 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
8eafeb48
AB
1183 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1184 size = MAX_MEMBLOCK_ADDR - base + 1;
a67a6ed1
LA
1185 }
1186
068f6310
RH
1187 if (base + size < phys_offset) {
1188 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1189 base, base + size);
1190 return;
1191 }
1192 if (base < phys_offset) {
1193 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1194 base, phys_offset);
1195 size -= phys_offset - base;
1196 base = phys_offset;
1197 }
1198 memblock_add(base, size);
1199}
1200
41a9ada3
RA
1201int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1202{
1203 return memblock_mark_hotplug(base, size);
1204}
1205
e8d9d1f5
MS
1206int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1207 phys_addr_t size, bool nomap)
1208{
e8d9d1f5
MS
1209 if (nomap)
1210 return memblock_remove(base, size);
1211 return memblock_reserve(base, size);
1212}
1213
a1727da5
GL
1214/*
1215 * called from unflatten_device_tree() to bootstrap devicetree itself
1216 * Architectures can override this definition if memblock isn't used
1217 */
1218void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1219{
1220 return __va(memblock_alloc(size, align));
1221}
e8d9d1f5 1222#else
aefc7ec2
RH
1223void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1224{
1225 WARN_ON(1);
1226}
1227
41a9ada3
RA
1228int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1229{
1230 return -ENOSYS;
1231}
1232
e8d9d1f5
MS
1233int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1234 phys_addr_t size, bool nomap)
1235{
78bb2abe 1236 pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
1d1a661d 1237 &base, &size, nomap ? " (nomap)" : "");
e8d9d1f5
MS
1238 return -ENOSYS;
1239}
aefc7ec2
RH
1240
1241void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1242{
1243 WARN_ON(1);
1244 return NULL;
1245}
a1727da5
GL
1246#endif
1247
4972a74b 1248bool __init early_init_dt_verify(void *params)
0288ffcb
RH
1249{
1250 if (!params)
1251 return false;
1252
0288ffcb 1253 /* check device tree validity */
50ba08f3 1254 if (fdt_check_header(params))
0288ffcb 1255 return false;
0288ffcb 1256
50ba08f3
BH
1257 /* Setup flat device-tree pointer */
1258 initial_boot_params = params;
08d53aa5
AB
1259 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1260 fdt_totalsize(initial_boot_params));
4972a74b
LA
1261 return true;
1262}
1263
1264
1265void __init early_init_dt_scan_nodes(void)
1266{
0288ffcb
RH
1267 /* Retrieve various information from the /chosen node */
1268 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1269
1270 /* Initialize {size,address}-cells info */
1271 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1272
1273 /* Setup memory, calling early_init_dt_add_memory_arch */
1274 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
4972a74b
LA
1275}
1276
1277bool __init early_init_dt_scan(void *params)
1278{
1279 bool status;
1280
1281 status = early_init_dt_verify(params);
1282 if (!status)
1283 return false;
0288ffcb 1284
4972a74b 1285 early_init_dt_scan_nodes();
0288ffcb
RH
1286 return true;
1287}
1288
41f88009
GL
1289/**
1290 * unflatten_device_tree - create tree of device_nodes from flat blob
1291 *
1292 * unflattens the device-tree passed by the firmware, creating the
1293 * tree of struct device_node. It also fills the "name" and "type"
1294 * pointers of the nodes so the normal device-tree walking functions
1295 * can be used.
1296 */
1297void __init unflatten_device_tree(void)
1298{
c4263233 1299 __unflatten_device_tree(initial_boot_params, NULL, &of_root,
1d1bde55 1300 early_init_dt_alloc_memory_arch, false);
41f88009 1301
4c7d6361 1302 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
611cad72 1303 of_alias_scan(early_init_dt_alloc_memory_arch);
81d0848f
FR
1304
1305 unittest_unflatten_overlay_base();
41f88009 1306}
e6ce1324 1307
a8bf7527
RH
1308/**
1309 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1310 *
1311 * Copies and unflattens the device-tree passed by the firmware, creating the
1312 * tree of struct device_node. It also fills the "name" and "type"
1313 * pointers of the nodes so the normal device-tree walking functions
1314 * can be used. This should only be used when the FDT memory has not been
1315 * reserved such is the case when the FDT is built-in to the kernel init
1316 * section. If the FDT memory is reserved already then unflatten_device_tree
1317 * should be used instead.
1318 */
1319void __init unflatten_and_copy_device_tree(void)
1320{
6f041e99
JH
1321 int size;
1322 void *dt;
1323
1324 if (!initial_boot_params) {
1325 pr_warn("No valid device tree found, continuing without\n");
1326 return;
1327 }
1328
c972de14 1329 size = fdt_totalsize(initial_boot_params);
6f041e99 1330 dt = early_init_dt_alloc_memory_arch(size,
c972de14 1331 roundup_pow_of_two(FDT_V17_SIZE));
a8bf7527
RH
1332
1333 if (dt) {
1334 memcpy(dt, initial_boot_params, size);
1335 initial_boot_params = dt;
1336 }
1337 unflatten_device_tree();
1338}
1339
08d53aa5
AB
1340#ifdef CONFIG_SYSFS
1341static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1342 struct bin_attribute *bin_attr,
1343 char *buf, loff_t off, size_t count)
b0a6fb36 1344{
08d53aa5
AB
1345 memcpy(buf, initial_boot_params + off, count);
1346 return count;
1347}
b0a6fb36 1348
08d53aa5
AB
1349static int __init of_fdt_raw_init(void)
1350{
1351 static struct bin_attribute of_fdt_raw_attr =
1352 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
b0a6fb36 1353
08d53aa5
AB
1354 if (!initial_boot_params)
1355 return 0;
b0a6fb36 1356
08d53aa5
AB
1357 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1358 fdt_totalsize(initial_boot_params))) {
606ad42a 1359 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
08d53aa5
AB
1360 return 0;
1361 }
1362 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1363 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
b0a6fb36 1364}
08d53aa5 1365late_initcall(of_fdt_raw_init);
b0a6fb36
RH
1366#endif
1367
e6ce1324 1368#endif /* CONFIG_OF_EARLY_FLATTREE */