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