blob: 79ab36714580dd6f23e4a9556d32273cacc960c5 [file] [log] [blame]
Grant Likelye169cfb2009-11-23 14:53:09 -07001/*
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
Grant Likely41f88002009-11-23 20:07:01 -070012#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070013#include <linux/initrd.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080014#include <linux/module.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070015#include <linux/of.h>
16#include <linux/of_fdt.h>
Marek Szyprowski73eebf32014-02-28 14:42:47 +010017#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070018#include <linux/string.h>
19#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080020#include <linux/slab.h>
Marek Szyprowski73eebf32014-02-28 14:42:47 +010021#include <linux/memblock.h>
Grant Likely51975db2010-02-01 21:34:14 -070022
Fabio Estevamc89810a2012-01-02 14:19:03 -020023#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Grant Likely86e03222009-12-10 23:42:21 -070024#ifdef CONFIG_PPC
25#include <asm/machdep.h>
26#endif /* CONFIG_PPC */
27
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070028#include <asm/page.h>
29
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080030char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
31{
32 return ((char *)blob) +
33 be32_to_cpu(blob->off_dt_strings) + offset;
34}
35
36/**
37 * of_fdt_get_property - Given a node in the given flat blob, return
38 * the property ptr
39 */
40void *of_fdt_get_property(struct boot_param_header *blob,
41 unsigned long node, const char *name,
42 unsigned long *size)
43{
44 unsigned long p = node;
45
46 do {
47 u32 tag = be32_to_cpup((__be32 *)p);
48 u32 sz, noff;
49 const char *nstr;
50
51 p += 4;
52 if (tag == OF_DT_NOP)
53 continue;
54 if (tag != OF_DT_PROP)
55 return NULL;
56
57 sz = be32_to_cpup((__be32 *)p);
58 noff = be32_to_cpup((__be32 *)(p + 4));
59 p += 8;
60 if (be32_to_cpu(blob->version) < 0x10)
61 p = ALIGN(p, sz >= 8 ? 8 : 4);
62
63 nstr = of_fdt_get_string(blob, noff);
64 if (nstr == NULL) {
65 pr_warning("Can't find property index name !\n");
66 return NULL;
67 }
68 if (strcmp(name, nstr) == 0) {
69 if (size)
70 *size = sz;
71 return (void *)p;
72 }
73 p += sz;
74 p = ALIGN(p, 4);
75 } while (1);
76}
77
78/**
79 * of_fdt_is_compatible - Return true if given node from the given blob has
80 * compat in its compatible list
81 * @blob: A device tree blob
82 * @node: node to test
83 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040084 *
85 * On match, returns a non-zero value with smaller values returned for more
86 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080087 */
88int of_fdt_is_compatible(struct boot_param_header *blob,
89 unsigned long node, const char *compat)
90{
91 const char *cp;
Grant Likelya4f740c2010-10-30 11:49:09 -040092 unsigned long cplen, l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080093
94 cp = of_fdt_get_property(blob, node, "compatible", &cplen);
95 if (cp == NULL)
96 return 0;
97 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -040098 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080099 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -0400100 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800101 l = strlen(cp) + 1;
102 cp += l;
103 cplen -= l;
104 }
105
106 return 0;
107}
108
Grant Likelya4f740c2010-10-30 11:49:09 -0400109/**
110 * of_fdt_match - Return true if node matches a list of compatible values
111 */
112int of_fdt_match(struct boot_param_header *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100113 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400114{
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
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800130static void *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700131 unsigned long align)
132{
133 void *res;
134
Grant Likelyf1d4c3a2010-06-25 12:16:52 -0600135 *mem = ALIGN(*mem, align);
Grant Likelybbd33932009-11-23 20:07:00 -0700136 res = (void *)*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
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800144 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700145 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -0700146 * @p: pointer to node in flat tree
147 * @dad: Parent struct device_node
148 * @allnextpp: pointer to ->allnext from last allocated device_node
149 * @fpsize: Size of the node path up at the current depth.
150 */
Andres Salomona7006c92011-03-17 17:32:35 -0700151static unsigned long unflatten_dt_node(struct boot_param_header *blob,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800152 unsigned long mem,
153 unsigned long *p,
154 struct device_node *dad,
155 struct device_node ***allnextpp,
156 unsigned long fpsize)
Grant Likelybbd33932009-11-23 20:07:00 -0700157{
158 struct device_node *np;
159 struct property *pp, **prev_pp = NULL;
160 char *pathp;
161 u32 tag;
162 unsigned int l, allocl;
163 int has_name = 0;
164 int new_format = 0;
165
Jeremy Kerr33714882010-01-30 01:45:26 -0700166 tag = be32_to_cpup((__be32 *)(*p));
Grant Likelybbd33932009-11-23 20:07:00 -0700167 if (tag != OF_DT_BEGIN_NODE) {
168 pr_err("Weird tag at start of node: %x\n", tag);
169 return mem;
170 }
171 *p += 4;
172 pathp = (char *)*p;
173 l = allocl = strlen(pathp) + 1;
Grant Likelyf1d4c3a2010-06-25 12:16:52 -0600174 *p = ALIGN(*p + l, 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700175
176 /* version 0x10 has a more compact unit name here instead of the full
177 * path. we accumulate the full path size using "fpsize", we'll rebuild
178 * it later. We detect this because the first character of the name is
179 * not '/'.
180 */
181 if ((*pathp) != '/') {
182 new_format = 1;
183 if (fpsize == 0) {
184 /* root node: special case. fpsize accounts for path
185 * plus terminating zero. root node only has '/', so
186 * fpsize should be 2, but we want to avoid the first
187 * level nodes to have two '/' so we use fpsize 1 here
188 */
189 fpsize = 1;
190 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000191 l = 1;
192 *pathp = '\0';
Grant Likelybbd33932009-11-23 20:07:00 -0700193 } else {
194 /* account for '/' and path size minus terminal 0
195 * already in 'l'
196 */
197 fpsize += l;
198 allocl = fpsize;
199 }
200 }
201
202 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
203 __alignof__(struct device_node));
204 if (allnextpp) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000205 char *fn;
Grant Likelybbd33932009-11-23 20:07:00 -0700206 memset(np, 0, sizeof(*np));
Grant Likelyc22618a2012-11-14 22:37:12 +0000207 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700208 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700209 /* rebuild full path for new format */
210 if (dad && dad->parent) {
211 strcpy(fn, dad->full_name);
212#ifdef DEBUG
213 if ((strlen(fn) + l + 1) != allocl) {
214 pr_debug("%s: p: %d, l: %d, a: %d\n",
215 pathp, (int)strlen(fn),
216 l, allocl);
217 }
218#endif
219 fn += strlen(fn);
220 }
221 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000222 }
223 memcpy(fn, pathp, l);
224
Grant Likelybbd33932009-11-23 20:07:00 -0700225 prev_pp = &np->properties;
226 **allnextpp = np;
227 *allnextpp = &np->allnext;
228 if (dad != NULL) {
229 np->parent = dad;
230 /* we temporarily use the next field as `last_child'*/
231 if (dad->next == NULL)
232 dad->child = np;
233 else
234 dad->next->sibling = np;
235 dad->next = np;
236 }
237 kref_init(&np->kref);
238 }
Andres Salomona7006c92011-03-17 17:32:35 -0700239 /* process properties */
Grant Likelybbd33932009-11-23 20:07:00 -0700240 while (1) {
241 u32 sz, noff;
242 char *pname;
243
Jeremy Kerr33714882010-01-30 01:45:26 -0700244 tag = be32_to_cpup((__be32 *)(*p));
Grant Likelybbd33932009-11-23 20:07:00 -0700245 if (tag == OF_DT_NOP) {
246 *p += 4;
247 continue;
248 }
249 if (tag != OF_DT_PROP)
250 break;
251 *p += 4;
Jeremy Kerr33714882010-01-30 01:45:26 -0700252 sz = be32_to_cpup((__be32 *)(*p));
253 noff = be32_to_cpup((__be32 *)((*p) + 4));
Grant Likelybbd33932009-11-23 20:07:00 -0700254 *p += 8;
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800255 if (be32_to_cpu(blob->version) < 0x10)
Grant Likelyf1d4c3a2010-06-25 12:16:52 -0600256 *p = ALIGN(*p, sz >= 8 ? 8 : 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700257
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800258 pname = of_fdt_get_string(blob, noff);
Grant Likelybbd33932009-11-23 20:07:00 -0700259 if (pname == NULL) {
260 pr_info("Can't find property name in list !\n");
261 break;
262 }
263 if (strcmp(pname, "name") == 0)
264 has_name = 1;
265 l = strlen(pname) + 1;
266 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
267 __alignof__(struct property));
268 if (allnextpp) {
David Gibson04b954a2010-02-01 21:34:15 -0700269 /* We accept flattened tree phandles either in
270 * ePAPR-style "phandle" properties, or the
271 * legacy "linux,phandle" properties. If both
272 * appear and have different values, things
273 * will get weird. Don't do that. */
274 if ((strcmp(pname, "phandle") == 0) ||
275 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700276 if (np->phandle == 0)
Grant Likely9a6b2e52010-07-23 01:48:25 -0600277 np->phandle = be32_to_cpup((__be32*)*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700278 }
David Gibson04b954a2010-02-01 21:34:15 -0700279 /* And we process the "ibm,phandle" property
280 * used in pSeries dynamic device tree
281 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700282 if (strcmp(pname, "ibm,phandle") == 0)
Grant Likely9a6b2e52010-07-23 01:48:25 -0600283 np->phandle = be32_to_cpup((__be32 *)*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700284 pp->name = pname;
285 pp->length = sz;
286 pp->value = (void *)*p;
287 *prev_pp = pp;
288 prev_pp = &pp->next;
289 }
Grant Likelyf1d4c3a2010-06-25 12:16:52 -0600290 *p = ALIGN((*p) + sz, 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700291 }
292 /* with version 0x10 we may not have the name property, recreate
293 * it here from the unit name if absent
294 */
295 if (!has_name) {
296 char *p1 = pathp, *ps = pathp, *pa = NULL;
297 int sz;
298
299 while (*p1) {
300 if ((*p1) == '@')
301 pa = p1;
302 if ((*p1) == '/')
303 ps = p1 + 1;
304 p1++;
305 }
306 if (pa < ps)
307 pa = p1;
308 sz = (pa - ps) + 1;
309 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
310 __alignof__(struct property));
311 if (allnextpp) {
312 pp->name = "name";
313 pp->length = sz;
314 pp->value = pp + 1;
315 *prev_pp = pp;
316 prev_pp = &pp->next;
317 memcpy(pp->value, ps, sz - 1);
318 ((char *)pp->value)[sz - 1] = 0;
319 pr_debug("fixed up name for %s -> %s\n", pathp,
320 (char *)pp->value);
321 }
322 }
323 if (allnextpp) {
324 *prev_pp = NULL;
325 np->name = of_get_property(np, "name", NULL);
326 np->type = of_get_property(np, "device_type", NULL);
327
328 if (!np->name)
329 np->name = "<NULL>";
330 if (!np->type)
331 np->type = "<NULL>";
332 }
Jason Gunthorpe7f809e12010-03-26 22:09:56 -0600333 while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
334 if (tag == OF_DT_NOP)
335 *p += 4;
336 else
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800337 mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
338 fpsize);
Jeremy Kerr33714882010-01-30 01:45:26 -0700339 tag = be32_to_cpup((__be32 *)(*p));
Grant Likelybbd33932009-11-23 20:07:00 -0700340 }
341 if (tag != OF_DT_END_NODE) {
342 pr_err("Weird tag at end of node: %x\n", tag);
343 return mem;
344 }
345 *p += 4;
346 return mem;
347}
Grant Likely41f88002009-11-23 20:07:01 -0700348
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800349/**
350 * __unflatten_device_tree - create tree of device_nodes from flat blob
351 *
352 * unflattens a device-tree, creating the
353 * tree of struct device_node. It also fills the "name" and "type"
354 * pointers of the nodes so the normal device-tree walking functions
355 * can be used.
356 * @blob: The blob to expand
357 * @mynodes: The device_node tree created by the call
358 * @dt_alloc: An allocator that provides a virtual address to memory
359 * for the resulting tree
360 */
Andres Salomona7006c92011-03-17 17:32:35 -0700361static void __unflatten_device_tree(struct boot_param_header *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800362 struct device_node **mynodes,
363 void * (*dt_alloc)(u64 size, u64 align))
364{
365 unsigned long start, mem, size;
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 */
386 start = ((unsigned long)blob) +
387 be32_to_cpu(blob->off_dt_struct);
388 size = unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
389 size = (size | 3) + 1;
390
391 pr_debug(" size is %lx, allocating...\n", size);
392
393 /* Allocate memory for the expanded device tree */
394 mem = (unsigned long)
395 dt_alloc(size + 4, __alignof__(struct device_node));
396
397 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
398
399 pr_debug(" unflattening %lx...\n", mem);
400
401 /* Second pass, do actual unflattening */
402 start = ((unsigned long)blob) +
403 be32_to_cpu(blob->off_dt_struct);
404 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
405 if (be32_to_cpup((__be32 *)start) != OF_DT_END)
406 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
407 if (be32_to_cpu(((__be32 *)mem)[size / 4]) != 0xdeadbeef)
408 pr_warning("End of tree marker overwritten: %08x\n",
409 be32_to_cpu(((__be32 *)mem)[size / 4]));
410 *allnextp = NULL;
411
412 pr_debug(" <- unflatten_device_tree()\n");
413}
414
415static void *kernel_tree_alloc(u64 size, u64 align)
416{
417 return kzalloc(size, GFP_KERNEL);
418}
419
420/**
421 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
422 *
423 * unflattens the device-tree passed by the firmware, creating the
424 * tree of struct device_node. It also fills the "name" and "type"
425 * pointers of the nodes so the normal device-tree walking functions
426 * can be used.
427 */
428void of_fdt_unflatten_tree(unsigned long *blob,
429 struct device_node **mynodes)
430{
431 struct boot_param_header *device_tree =
432 (struct boot_param_header *)blob;
433 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
434}
435EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
436
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800437/* Everything below here references initial_boot_params directly. */
438int __initdata dt_root_addr_cells;
439int __initdata dt_root_size_cells;
440
441struct boot_param_header *initial_boot_params;
442
443#ifdef CONFIG_OF_EARLY_FLATTREE
444
445/**
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100446 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
447 */
448static int __init __reserved_mem_reserve_reg(unsigned long node,
449 const char *uname)
450{
451 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
452 phys_addr_t base, size;
453 unsigned long len;
454 __be32 *prop;
455 int nomap;
456
457 prop = of_get_flat_dt_prop(node, "reg", &len);
458 if (!prop)
459 return -ENOENT;
460
461 if (len && len % t_len != 0) {
462 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
463 uname);
464 return -EINVAL;
465 }
466
467 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
468
469 while (len >= t_len) {
470 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
471 size = dt_mem_next_cell(dt_root_size_cells, &prop);
472
473 if (base && size &&
474 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
475 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
476 uname, &base, (unsigned long)size / SZ_1M);
477 else
478 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
479 uname, &base, (unsigned long)size / SZ_1M);
480
481 len -= t_len;
482 }
483 return 0;
484}
485
486/**
487 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
488 * in /reserved-memory matches the values supported by the current implementation,
489 * also check if ranges property has been provided
490 */
491static int __reserved_mem_check_root(unsigned long node)
492{
493 __be32 *prop;
494
495 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
496 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
497 return -EINVAL;
498
499 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
500 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
501 return -EINVAL;
502
503 prop = of_get_flat_dt_prop(node, "ranges", NULL);
504 if (!prop)
505 return -EINVAL;
506 return 0;
507}
508
509/**
510 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
511 */
512static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
513 int depth, void *data)
514{
515 static int found;
516 const char *status;
517
518 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
519 if (__reserved_mem_check_root(node) != 0) {
520 pr_err("Reserved memory: unsupported node format, ignoring\n");
521 /* break scan */
522 return 1;
523 }
524 found = 1;
525 /* scan next node */
526 return 0;
527 } else if (!found) {
528 /* scan next node */
529 return 0;
530 } else if (found && depth < 2) {
531 /* scanning of /reserved-memory has been finished */
532 return 1;
533 }
534
535 status = of_get_flat_dt_prop(node, "status", NULL);
536 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
537 return 0;
538
539 __reserved_mem_reserve_reg(node, uname);
540
541 /* scan next node */
542 return 0;
543}
544
545/**
546 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
547 *
548 * This function grabs memory from early allocator for device exclusive use
549 * defined in device tree structures. It should be called by arch specific code
550 * once the early allocator (i.e. memblock) has been fully activated.
551 */
552void __init early_init_fdt_scan_reserved_mem(void)
553{
554 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
555}
556
557/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800558 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
559 * @it: callback function
560 * @data: context data pointer
561 *
562 * This function is used to scan the flattened device-tree, it is
563 * used to extract the memory information at boot before we can
564 * unflatten the tree
565 */
566int __init of_scan_flat_dt(int (*it)(unsigned long node,
567 const char *uname, int depth,
568 void *data),
569 void *data)
570{
571 unsigned long p = ((unsigned long)initial_boot_params) +
572 be32_to_cpu(initial_boot_params->off_dt_struct);
573 int rc = 0;
574 int depth = -1;
575
576 do {
577 u32 tag = be32_to_cpup((__be32 *)p);
Fabio Estevame55b0822012-11-12 18:30:49 -0200578 const char *pathp;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800579
580 p += 4;
581 if (tag == OF_DT_END_NODE) {
582 depth--;
583 continue;
584 }
585 if (tag == OF_DT_NOP)
586 continue;
587 if (tag == OF_DT_END)
588 break;
589 if (tag == OF_DT_PROP) {
590 u32 sz = be32_to_cpup((__be32 *)p);
591 p += 8;
592 if (be32_to_cpu(initial_boot_params->version) < 0x10)
593 p = ALIGN(p, sz >= 8 ? 8 : 4);
594 p += sz;
595 p = ALIGN(p, 4);
596 continue;
597 }
598 if (tag != OF_DT_BEGIN_NODE) {
599 pr_err("Invalid tag %x in flat device tree!\n", tag);
600 return -EINVAL;
601 }
602 depth++;
603 pathp = (char *)p;
604 p = ALIGN(p + strlen(pathp) + 1, 4);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800605 if (*pathp == '/')
606 pathp = kbasename(pathp);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800607 rc = it(p, pathp, depth, data);
608 if (rc != 0)
609 break;
610 } while (1);
611
612 return rc;
613}
614
615/**
616 * of_get_flat_dt_root - find the root node in the flat blob
617 */
618unsigned long __init of_get_flat_dt_root(void)
619{
620 unsigned long p = ((unsigned long)initial_boot_params) +
621 be32_to_cpu(initial_boot_params->off_dt_struct);
622
623 while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
624 p += 4;
625 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
626 p += 4;
627 return ALIGN(p + strlen((char *)p) + 1, 4);
628}
629
630/**
631 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
632 *
633 * This function can be used within scan_flattened_dt callback to get
634 * access to properties
635 */
636void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
637 unsigned long *size)
638{
639 return of_fdt_get_property(initial_boot_params, node, name, size);
640}
641
642/**
643 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
644 * @node: node to test
645 * @compat: compatible string to compare with compatible list.
646 */
647int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
648{
649 return of_fdt_is_compatible(initial_boot_params, node, compat);
650}
651
Grant Likelya4f740c2010-10-30 11:49:09 -0400652/**
653 * of_flat_dt_match - Return true if node matches a list of compatible values
654 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100655int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400656{
657 return of_fdt_match(initial_boot_params, node, compat);
658}
659
Marek Szyprowski3e129882013-08-26 14:41:56 +0200660struct fdt_scan_status {
661 const char *name;
662 int namelen;
663 int depth;
664 int found;
665 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
666 void *data;
667};
668
669/**
670 * fdt_scan_node_by_path - iterator for of_scan_flat_dt_by_path function
671 */
672static int __init fdt_scan_node_by_path(unsigned long node, const char *uname,
673 int depth, void *data)
674{
675 struct fdt_scan_status *st = data;
676
677 /*
678 * if scan at the requested fdt node has been completed,
679 * return -ENXIO to abort further scanning
680 */
681 if (depth <= st->depth)
682 return -ENXIO;
683
684 /* requested fdt node has been found, so call iterator function */
685 if (st->found)
686 return st->iterator(node, uname, depth, st->data);
687
688 /* check if scanning automata is entering next level of fdt nodes */
689 if (depth == st->depth + 1 &&
690 strncmp(st->name, uname, st->namelen) == 0 &&
691 uname[st->namelen] == 0) {
692 st->depth += 1;
693 if (st->name[st->namelen] == 0) {
694 st->found = 1;
695 } else {
696 const char *next = st->name + st->namelen + 1;
697 st->name = next;
698 st->namelen = strcspn(next, "/");
699 }
700 return 0;
701 }
702
703 /* scan next fdt node */
704 return 0;
705}
706
707/**
708 * of_scan_flat_dt_by_path - scan flattened tree blob and call callback on each
709 * child of the given path.
710 * @path: path to start searching for children
711 * @it: callback function
712 * @data: context data pointer
713 *
714 * This function is used to scan the flattened device-tree starting from the
715 * node given by path. It is used to extract information (like reserved
716 * memory), which is required on ealy boot before we can unflatten the tree.
717 */
718int __init of_scan_flat_dt_by_path(const char *path,
719 int (*it)(unsigned long node, const char *name, int depth, void *data),
720 void *data)
721{
722 struct fdt_scan_status st = {path, 0, -1, 0, it, data};
723 int ret = 0;
724
725 if (initial_boot_params)
726 ret = of_scan_flat_dt(fdt_scan_node_by_path, &st);
727
728 if (!st.found)
729 return -ENOENT;
730 else if (ret == -ENXIO) /* scan has been completed */
731 return 0;
732 else
733 return ret;
734}
735
Grant Likelyf7b3a832009-11-24 03:26:58 -0700736#ifdef CONFIG_BLK_DEV_INITRD
737/**
738 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
739 * @node: reference to node containing initrd location ('chosen')
740 */
741void __init early_init_dt_check_for_initrd(unsigned long node)
742{
Santosh Shilimkarfb0399c2013-07-01 14:20:35 -0400743 u64 start, end;
744 unsigned long len;
Jeremy Kerr33714882010-01-30 01:45:26 -0700745 __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700746
747 pr_debug("Looking for initrd properties... ");
748
749 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700750 if (!prop)
751 return;
Santosh Shilimkarfb0399c2013-07-01 14:20:35 -0400752 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700753
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700754 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
755 if (!prop)
756 return;
Santosh Shilimkarfb0399c2013-07-01 14:20:35 -0400757 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700758
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700759 early_init_dt_setup_initrd_arch(start, end);
Santosh Shilimkarfb0399c2013-07-01 14:20:35 -0400760 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
761 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700762}
763#else
764inline void early_init_dt_check_for_initrd(unsigned long node)
765{
766}
767#endif /* CONFIG_BLK_DEV_INITRD */
768
Grant Likely41f88002009-11-23 20:07:01 -0700769/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700770 * early_init_dt_scan_root - fetch the top level address and size cells
771 */
772int __init early_init_dt_scan_root(unsigned long node, const char *uname,
773 int depth, void *data)
774{
Jeremy Kerr33714882010-01-30 01:45:26 -0700775 __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700776
777 if (depth != 0)
778 return 0;
779
Jeremy Kerr33714882010-01-30 01:45:26 -0700780 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
781 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
782
Grant Likelyf00abd92009-11-24 03:27:10 -0700783 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700784 if (prop)
785 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700786 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
787
788 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700789 if (prop)
790 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700791 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
792
793 /* break now */
794 return 1;
795}
796
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700797u64 __init dt_mem_next_cell(int s, __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700798{
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700799 __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700800
801 *cellp = p + s;
802 return of_read_number(p, s);
803}
804
Grant Likely51975db2010-02-01 21:34:14 -0700805/**
806 * early_init_dt_scan_memory - Look for an parse memory nodes
807 */
808int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
809 int depth, void *data)
810{
811 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
812 __be32 *reg, *endp;
813 unsigned long l;
814
815 /* We are scanning "memory" nodes only */
816 if (type == NULL) {
817 /*
818 * The longtrail doesn't have a device_type on the
819 * /memory node, so look for the node called /memory@0.
820 */
821 if (depth != 1 || strcmp(uname, "memory@0") != 0)
822 return 0;
823 } else if (strcmp(type, "memory") != 0)
824 return 0;
825
826 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
827 if (reg == NULL)
828 reg = of_get_flat_dt_prop(node, "reg", &l);
829 if (reg == NULL)
830 return 0;
831
832 endp = reg + (l / sizeof(__be32));
833
834 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
835 uname, l, reg[0], reg[1], reg[2], reg[3]);
836
837 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
838 u64 base, size;
839
840 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
841 size = dt_mem_next_cell(dt_root_size_cells, &reg);
842
843 if (size == 0)
844 continue;
845 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
846 (unsigned long long)size);
847
848 early_init_dt_add_memory_arch(base, size);
849 }
850
851 return 0;
852}
853
Grant Likely86e03222009-12-10 23:42:21 -0700854int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
855 int depth, void *data)
856{
857 unsigned long l;
858 char *p;
859
860 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
861
Grant Likely85f60ae2011-04-29 00:18:16 -0600862 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700863 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
864 return 0;
865
866 early_init_dt_check_for_initrd(node);
867
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300868 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700869 p = of_get_flat_dt_prop(node, "bootargs", &l);
870 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600871 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700872
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000873 /*
874 * CONFIG_CMDLINE is meant to be a default in case nothing else
875 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
876 * is set in which case we override whatever was found earlier.
877 */
Grant Likely86e03222009-12-10 23:42:21 -0700878#ifdef CONFIG_CMDLINE
879#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000880 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700881#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600882 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700883#endif /* CONFIG_CMDLINE */
884
Grant Likely85f60ae2011-04-29 00:18:16 -0600885 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700886
887 /* break now */
888 return 1;
889}
890
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100891#ifdef CONFIG_HAVE_MEMBLOCK
892void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
893{
894 const u64 phys_offset = __pa(PAGE_OFFSET);
895 base &= PAGE_MASK;
896 size &= PAGE_MASK;
897 if (base + size < phys_offset) {
898 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
899 base, base + size);
900 return;
901 }
902 if (base < phys_offset) {
903 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
904 base, phys_offset);
905 size -= phys_offset - base;
906 base = phys_offset;
907 }
908 memblock_add(base, size);
909}
910
911int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
912 phys_addr_t size, bool nomap)
913{
914 if (memblock_is_region_reserved(base, size))
915 return -EBUSY;
916 if (nomap)
917 return memblock_remove(base, size);
918 return memblock_reserve(base, size);
919}
920
921/*
922 * called from unflatten_device_tree() to bootstrap devicetree itself
923 * Architectures can override this definition if memblock isn't used
924 */
925void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
926{
927 return __va(memblock_alloc(size, align));
928}
929#else
930int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
931 phys_addr_t size, bool nomap)
932{
933 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
934 base, size, nomap ? " (nomap)" : "");
935 return -ENOSYS;
936}
937#endif
938
939bool __init early_init_dt_scan(void *params)
940{
941 if (!params)
942 return false;
943
944 /* Setup flat device-tree pointer */
945 initial_boot_params = params;
946
947 /* check device tree validity */
948 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
949 initial_boot_params = NULL;
950 return false;
951 }
952
953 /* Retrieve various information from the /chosen node */
954 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
955
956 /* Initialize {size,address}-cells info */
957 of_scan_flat_dt(early_init_dt_scan_root, NULL);
958
959 /* Setup memory, calling early_init_dt_add_memory_arch */
960 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
961
962 return true;
963}
964
Grant Likelyf00abd92009-11-24 03:27:10 -0700965/**
Grant Likely41f88002009-11-23 20:07:01 -0700966 * unflatten_device_tree - create tree of device_nodes from flat blob
967 *
968 * unflattens the device-tree passed by the firmware, creating the
969 * tree of struct device_node. It also fills the "name" and "type"
970 * pointers of the nodes so the normal device-tree walking functions
971 * can be used.
972 */
973void __init unflatten_device_tree(void)
974{
Randy Dunlap465aac62012-11-30 10:01:51 +0000975 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700976 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700977
Shawn Guo611cad72011-08-15 15:28:14 +0800978 /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
979 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700980}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800981
982#endif /* CONFIG_OF_EARLY_FLATTREE */