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