of: Eliminate of_allnodes list
[deliverable/linux.git] / drivers / of / fdt.c
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
12 #include <linux/kernel.h>
13 #include <linux/initrd.h>
14 #include <linux/memblock.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/sizes.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/libfdt.h>
23 #include <linux/debugfs.h>
24 #include <linux/serial_core.h>
25
26 #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
27 #include <asm/page.h>
28
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 */
37 void 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
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.
83 *
84 * On match, returns a non-zero value with smaller values returned for more
85 * specific compatible values.
86 */
87 int of_fdt_is_compatible(const void *blob,
88 unsigned long node, const char *compat)
89 {
90 const char *cp;
91 int cplen;
92 unsigned long l, score = 0;
93
94 cp = fdt_getprop(blob, node, "compatible", &cplen);
95 if (cp == NULL)
96 return 0;
97 while (cplen > 0) {
98 score++;
99 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
100 return score;
101 l = strlen(cp) + 1;
102 cp += l;
103 cplen -= l;
104 }
105
106 return 0;
107 }
108
109 /**
110 * of_fdt_match - Return true if node matches a list of compatible values
111 */
112 int of_fdt_match(const void *blob, unsigned long node,
113 const char *const *compat)
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
130 static void *unflatten_dt_alloc(void **mem, unsigned long size,
131 unsigned long align)
132 {
133 void *res;
134
135 *mem = PTR_ALIGN(*mem, align);
136 res = *mem;
137 *mem += size;
138
139 return res;
140 }
141
142 /**
143 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
144 * @blob: The parent device tree blob
145 * @mem: Memory chunk to use for allocating device nodes and properties
146 * @p: pointer to node in flat tree
147 * @dad: Parent struct device_node
148 * @fpsize: Size of the node path up at the current depth.
149 */
150 static void * unflatten_dt_node(void *blob,
151 void *mem,
152 int *poffset,
153 struct device_node *dad,
154 struct device_node **nodepp,
155 unsigned long fpsize,
156 bool dryrun)
157 {
158 const __be32 *p;
159 struct device_node *np;
160 struct property *pp, **prev_pp = NULL;
161 const char *pathp;
162 unsigned int l, allocl;
163 static int depth = 0;
164 int old_depth;
165 int offset;
166 int has_name = 0;
167 int new_format = 0;
168
169 pathp = fdt_get_name(blob, *poffset, &l);
170 if (!pathp)
171 return mem;
172
173 allocl = l++;
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;
190 l = 1;
191 pathp = "";
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));
203 if (!dryrun) {
204 char *fn;
205 of_node_init(np);
206 np->full_name = fn = ((char *)np) + sizeof(*np);
207 if (new_format) {
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++) = '/';
221 }
222 memcpy(fn, pathp, l);
223
224 prev_pp = &np->properties;
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 }
234 }
235 /* process properties */
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;
244 break;
245 }
246
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;
253 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
254 __alignof__(struct property));
255 if (!dryrun) {
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)) {
263 if (np->phandle == 0)
264 np->phandle = be32_to_cpup(p);
265 }
266 /* And we process the "ibm,phandle" property
267 * used in pSeries dynamic device tree
268 * stuff */
269 if (strcmp(pname, "ibm,phandle") == 0)
270 np->phandle = be32_to_cpup(p);
271 pp->name = (char *)pname;
272 pp->length = sz;
273 pp->value = (__be32 *)p;
274 *prev_pp = pp;
275 prev_pp = &pp->next;
276 }
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) {
282 const char *p1 = pathp, *ps = pathp, *pa = NULL;
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));
297 if (!dryrun) {
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 }
309 if (!dryrun) {
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 }
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)
325 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
326 fpsize, dryrun);
327
328 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
329 pr_err("unflatten: error %d processing FDT\n", *poffset);
330 if (nodepp)
331 *nodepp = np;
332
333 return mem;
334 }
335
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 */
348 static void __unflatten_device_tree(void *blob,
349 struct device_node **mynodes,
350 void * (*dt_alloc)(u64 size, u64 align))
351 {
352 unsigned long size;
353 int start;
354 void *mem;
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");
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));
367
368 if (fdt_check_header(blob)) {
369 pr_err("Invalid device tree blob header\n");
370 return;
371 }
372
373 /* First pass, scan for size */
374 start = 0;
375 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
376 size = ALIGN(size, 4);
377
378 pr_debug(" size is %lx, allocating...\n", size);
379
380 /* Allocate memory for the expanded device tree */
381 mem = dt_alloc(size + 4, __alignof__(struct device_node));
382 memset(mem, 0, size);
383
384 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
385
386 pr_debug(" unflattening %p...\n", mem);
387
388 /* Second pass, do actual unflattening */
389 start = 0;
390 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
391 if (be32_to_cpup(mem + size) != 0xdeadbeef)
392 pr_warning("End of tree marker overwritten: %08x\n",
393 be32_to_cpup(mem + size));
394
395 pr_debug(" <- unflatten_device_tree()\n");
396 }
397
398 static 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 */
411 void of_fdt_unflatten_tree(unsigned long *blob,
412 struct device_node **mynodes)
413 {
414 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
415 }
416 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
417
418 /* Everything below here references initial_boot_params directly. */
419 int __initdata dt_root_addr_cells;
420 int __initdata dt_root_size_cells;
421
422 void *initial_boot_params;
423
424 #ifdef CONFIG_OF_EARLY_FLATTREE
425
426 /**
427 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
428 */
429 static 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;
434 int len;
435 const __be32 *prop;
436 int nomap, first = 1;
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
454 if (size &&
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;
463 if (first) {
464 fdt_reserved_mem_save_node(node, uname, base, size);
465 first = 0;
466 }
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 */
476 static int __init __reserved_mem_check_root(unsigned long node)
477 {
478 const __be32 *prop;
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 */
497 static 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;
502 int err;
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
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);
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 */
540 void __init early_init_fdt_scan_reserved_mem(void)
541 {
542 int n;
543 u64 base, size;
544
545 if (!initial_boot_params)
546 return;
547
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
561 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
562 fdt_init_reserved_mem();
563 }
564
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 */
574 int __init of_scan_flat_dt(int (*it)(unsigned long node,
575 const char *uname, int depth,
576 void *data),
577 void *data)
578 {
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);
588 if (*pathp == '/')
589 pathp = kbasename(pathp);
590 rc = it(offset, pathp, depth, data);
591 }
592 return rc;
593 }
594
595 /**
596 * of_get_flat_dt_root - find the root node in the flat blob
597 */
598 unsigned long __init of_get_flat_dt_root(void)
599 {
600 return 0;
601 }
602
603 /**
604 * of_get_flat_dt_size - Return the total size of the FDT
605 */
606 int __init of_get_flat_dt_size(void)
607 {
608 return fdt_totalsize(initial_boot_params);
609 }
610
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 */
617 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
618 int *size)
619 {
620 return fdt_getprop(initial_boot_params, node, name, size);
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 */
628 int __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
633 /**
634 * of_flat_dt_match - Return true if node matches a list of compatible values
635 */
636 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
637 {
638 return of_fdt_match(initial_boot_params, node, compat);
639 }
640
641 struct 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
650 const 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 */
670 const 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;
689 int size;
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
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 */
715 static void __init early_init_dt_check_for_initrd(unsigned long node)
716 {
717 u64 start, end;
718 int len;
719 const __be32 *prop;
720
721 pr_debug("Looking for initrd properties... ");
722
723 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
724 if (!prop)
725 return;
726 start = of_read_number(prop, len/4);
727
728 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
729 if (!prop)
730 return;
731 end = of_read_number(prop, len/4);
732
733 initrd_start = (unsigned long)__va(start);
734 initrd_end = (unsigned long)__va(end);
735 initrd_below_start_ok = 1;
736
737 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
738 (unsigned long long)start, (unsigned long long)end);
739 }
740 #else
741 static inline void early_init_dt_check_for_initrd(unsigned long node)
742 {
743 }
744 #endif /* CONFIG_BLK_DEV_INITRD */
745
746 #ifdef CONFIG_SERIAL_EARLYCON
747 extern struct of_device_id __earlycon_of_table[];
748
749 int __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
791 static int __init setup_of_earlycon(char *buf)
792 {
793 if (buf)
794 return 0;
795
796 return early_init_dt_scan_chosen_serial();
797 }
798 early_param("earlycon", setup_of_earlycon);
799 #endif
800
801 /**
802 * early_init_dt_scan_root - fetch the top level address and size cells
803 */
804 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
805 int depth, void *data)
806 {
807 const __be32 *prop;
808
809 if (depth != 0)
810 return 0;
811
812 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
813 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
814
815 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
816 if (prop)
817 dt_root_size_cells = be32_to_cpup(prop);
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);
821 if (prop)
822 dt_root_addr_cells = be32_to_cpup(prop);
823 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
824
825 /* break now */
826 return 1;
827 }
828
829 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
830 {
831 const __be32 *p = *cellp;
832
833 *cellp = p + s;
834 return of_read_number(p, s);
835 }
836
837 /**
838 * early_init_dt_scan_memory - Look for an parse memory nodes
839 */
840 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
841 int depth, void *data)
842 {
843 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
844 const __be32 *reg, *endp;
845 int l;
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 */
853 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
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
866 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
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
886 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
887 int depth, void *data)
888 {
889 int l;
890 const char *p;
891
892 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
893
894 if (depth != 1 || !data ||
895 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
896 return 0;
897
898 early_init_dt_check_for_initrd(node);
899
900 /* Retrieve command line */
901 p = of_get_flat_dt_prop(node, "bootargs", &l);
902 if (p != NULL && l > 0)
903 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
904
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 */
910 #ifdef CONFIG_CMDLINE
911 #ifndef CONFIG_CMDLINE_FORCE
912 if (!((char *)data)[0])
913 #endif
914 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
915 #endif /* CONFIG_CMDLINE */
916
917 pr_debug("Command line is: %s\n", (char*)data);
918
919 /* break now */
920 return 1;
921 }
922
923 #ifdef CONFIG_HAVE_MEMBLOCK
924 #define MAX_PHYS_ADDR ((phys_addr_t)~0)
925
926 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
927 {
928 const u64 phys_offset = __pa(PAGE_OFFSET);
929
930 if (!PAGE_ALIGNED(base)) {
931 size -= PAGE_SIZE - (base & ~PAGE_MASK);
932 base = PAGE_ALIGN(base);
933 }
934 size &= PAGE_MASK;
935
936 if (base > MAX_PHYS_ADDR) {
937 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
938 base, base + size);
939 return;
940 }
941
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;
946 }
947
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
962 int __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
972 /*
973 * called from unflatten_device_tree() to bootstrap devicetree itself
974 * Architectures can override this definition if memblock isn't used
975 */
976 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
977 {
978 return __va(memblock_alloc(size, align));
979 }
980 #else
981 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
982 phys_addr_t size, bool nomap)
983 {
984 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
985 &base, &size, nomap ? " (nomap)" : "");
986 return -ENOSYS;
987 }
988 #endif
989
990 bool __init early_init_dt_verify(void *params)
991 {
992 if (!params)
993 return false;
994
995 /* Setup flat device-tree pointer */
996 initial_boot_params = params;
997
998 /* check device tree validity */
999 if (fdt_check_header(params)) {
1000 initial_boot_params = NULL;
1001 return false;
1002 }
1003
1004 return true;
1005 }
1006
1007
1008 void __init early_init_dt_scan_nodes(void)
1009 {
1010 /* Retrieve various information from the /chosen node */
1011 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1012
1013 /* Initialize {size,address}-cells info */
1014 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1015
1016 /* Setup memory, calling early_init_dt_add_memory_arch */
1017 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1018 }
1019
1020 bool __init early_init_dt_scan(void *params)
1021 {
1022 bool status;
1023
1024 status = early_init_dt_verify(params);
1025 if (!status)
1026 return false;
1027
1028 early_init_dt_scan_nodes();
1029 return true;
1030 }
1031
1032 /**
1033 * unflatten_device_tree - create tree of device_nodes from flat blob
1034 *
1035 * unflattens the device-tree passed by the firmware, creating the
1036 * tree of struct device_node. It also fills the "name" and "type"
1037 * pointers of the nodes so the normal device-tree walking functions
1038 * can be used.
1039 */
1040 void __init unflatten_device_tree(void)
1041 {
1042 __unflatten_device_tree(initial_boot_params, &of_root,
1043 early_init_dt_alloc_memory_arch);
1044
1045 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
1046 of_alias_scan(early_init_dt_alloc_memory_arch);
1047 }
1048
1049 /**
1050 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1051 *
1052 * Copies and unflattens the device-tree passed by the firmware, creating the
1053 * tree of struct device_node. It also fills the "name" and "type"
1054 * pointers of the nodes so the normal device-tree walking functions
1055 * can be used. This should only be used when the FDT memory has not been
1056 * reserved such is the case when the FDT is built-in to the kernel init
1057 * section. If the FDT memory is reserved already then unflatten_device_tree
1058 * should be used instead.
1059 */
1060 void __init unflatten_and_copy_device_tree(void)
1061 {
1062 int size;
1063 void *dt;
1064
1065 if (!initial_boot_params) {
1066 pr_warn("No valid device tree found, continuing without\n");
1067 return;
1068 }
1069
1070 size = fdt_totalsize(initial_boot_params);
1071 dt = early_init_dt_alloc_memory_arch(size,
1072 roundup_pow_of_two(FDT_V17_SIZE));
1073
1074 if (dt) {
1075 memcpy(dt, initial_boot_params, size);
1076 initial_boot_params = dt;
1077 }
1078 unflatten_device_tree();
1079 }
1080
1081 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1082 static struct debugfs_blob_wrapper flat_dt_blob;
1083
1084 static int __init of_flat_dt_debugfs_export_fdt(void)
1085 {
1086 struct dentry *d = debugfs_create_dir("device-tree", NULL);
1087
1088 if (!d)
1089 return -ENOENT;
1090
1091 flat_dt_blob.data = initial_boot_params;
1092 flat_dt_blob.size = fdt_totalsize(initial_boot_params);
1093
1094 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1095 d, &flat_dt_blob);
1096 if (!d)
1097 return -ENOENT;
1098
1099 return 0;
1100 }
1101 module_init(of_flat_dt_debugfs_export_fdt);
1102 #endif
1103
1104 #endif /* CONFIG_OF_EARLY_FLATTREE */
This page took 0.314883 seconds and 5 git commands to generate.