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