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