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