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