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