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