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