blob: 42fcf572ebfe0526f46d79fa5355b9dc8442c04c [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 Szyprowski5b8f8282014-02-28 14:42:48 +010017#include <linux/of_reserved_mem.h>
Marek Szyprowski73eebf32014-02-28 14:42:47 +010018#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070019#include <linux/string.h>
20#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080021#include <linux/slab.h>
Marek Szyprowski73eebf32014-02-28 14:42:47 +010022#include <linux/memblock.h>
Rob Herringa0e28c92014-04-02 15:10:14 -050023#include <linux/libfdt.h>
Grant Likely51975db2010-02-01 21:34:14 -070024
Fabio Estevamc89810a2012-01-02 14:19:03 -020025#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Grant Likely86e03222009-12-10 23:42:21 -070026#ifdef CONFIG_PPC
27#include <asm/machdep.h>
28#endif /* CONFIG_PPC */
29
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070030#include <asm/page.h>
31
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080032/**
33 * of_fdt_is_compatible - Return true if given node from the given blob has
34 * compat in its compatible list
35 * @blob: A device tree blob
36 * @node: node to test
37 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040038 *
39 * On match, returns a non-zero value with smaller values returned for more
40 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080041 */
42int of_fdt_is_compatible(struct boot_param_header *blob,
43 unsigned long node, const char *compat)
44{
45 const char *cp;
Mark Brownd6c2d4f2014-07-24 21:06:21 +010046 int cplen;
47 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080048
Rob Herringa0e28c92014-04-02 15:10:14 -050049 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080050 if (cp == NULL)
51 return 0;
52 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -040053 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080054 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -040055 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080056 l = strlen(cp) + 1;
57 cp += l;
58 cplen -= l;
59 }
60
61 return 0;
62}
63
Grant Likelya4f740c2010-10-30 11:49:09 -040064/**
65 * of_fdt_match - Return true if node matches a list of compatible values
66 */
67int of_fdt_match(struct boot_param_header *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +010068 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -040069{
70 unsigned int tmp, score = 0;
71
72 if (!compat)
73 return 0;
74
75 while (*compat) {
76 tmp = of_fdt_is_compatible(blob, node, *compat);
77 if (tmp && (score == 0 || (tmp < score)))
78 score = tmp;
79 compat++;
80 }
81
82 return score;
83}
84
Rob Herringa0e28c92014-04-02 15:10:14 -050085static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -070086 unsigned long align)
87{
88 void *res;
89
Rob Herringa0e28c92014-04-02 15:10:14 -050090 *mem = PTR_ALIGN(*mem, align);
Grant Likelybbd33932009-11-23 20:07:00 -070091 res = (void *)*mem;
92 *mem += size;
93
94 return res;
95}
96
97/**
98 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -080099 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700100 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -0700101 * @p: pointer to node in flat tree
102 * @dad: Parent struct device_node
103 * @allnextpp: pointer to ->allnext from last allocated device_node
104 * @fpsize: Size of the node path up at the current depth.
105 */
Rob Herringa0e28c92014-04-02 15:10:14 -0500106static void * unflatten_dt_node(struct boot_param_header *blob,
107 void *mem,
108 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800109 struct device_node *dad,
110 struct device_node ***allnextpp,
111 unsigned long fpsize)
Grant Likelybbd33932009-11-23 20:07:00 -0700112{
Rob Herringa0e28c92014-04-02 15:10:14 -0500113 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700114 struct device_node *np;
115 struct property *pp, **prev_pp = NULL;
Rob Herringa0e28c92014-04-02 15:10:14 -0500116 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700117 unsigned int l, allocl;
Rob Herringa0e28c92014-04-02 15:10:14 -0500118 static int depth = 0;
119 int old_depth;
120 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700121 int has_name = 0;
122 int new_format = 0;
123
Rob Herringa0e28c92014-04-02 15:10:14 -0500124 pathp = fdt_get_name(blob, *poffset, &l);
125 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700126 return mem;
Rob Herringa0e28c92014-04-02 15:10:14 -0500127
128 allocl = l++;
Grant Likelybbd33932009-11-23 20:07:00 -0700129
130 /* version 0x10 has a more compact unit name here instead of the full
131 * path. we accumulate the full path size using "fpsize", we'll rebuild
132 * it later. We detect this because the first character of the name is
133 * not '/'.
134 */
135 if ((*pathp) != '/') {
136 new_format = 1;
137 if (fpsize == 0) {
138 /* root node: special case. fpsize accounts for path
139 * plus terminating zero. root node only has '/', so
140 * fpsize should be 2, but we want to avoid the first
141 * level nodes to have two '/' so we use fpsize 1 here
142 */
143 fpsize = 1;
144 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000145 l = 1;
Rob Herringa0e28c92014-04-02 15:10:14 -0500146 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700147 } else {
148 /* account for '/' and path size minus terminal 0
149 * already in 'l'
150 */
151 fpsize += l;
152 allocl = fpsize;
153 }
154 }
155
156 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
157 __alignof__(struct device_node));
158 if (allnextpp) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000159 char *fn;
Pantelis Antonioub74e9e12013-12-13 20:08:59 +0200160 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000161 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700162 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700163 /* rebuild full path for new format */
164 if (dad && dad->parent) {
165 strcpy(fn, dad->full_name);
166#ifdef DEBUG
167 if ((strlen(fn) + l + 1) != allocl) {
168 pr_debug("%s: p: %d, l: %d, a: %d\n",
169 pathp, (int)strlen(fn),
170 l, allocl);
171 }
172#endif
173 fn += strlen(fn);
174 }
175 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000176 }
177 memcpy(fn, pathp, l);
178
Grant Likelybbd33932009-11-23 20:07:00 -0700179 prev_pp = &np->properties;
180 **allnextpp = np;
181 *allnextpp = &np->allnext;
182 if (dad != NULL) {
183 np->parent = dad;
184 /* we temporarily use the next field as `last_child'*/
185 if (dad->next == NULL)
186 dad->child = np;
187 else
188 dad->next->sibling = np;
189 dad->next = np;
190 }
Grant Likelybbd33932009-11-23 20:07:00 -0700191 }
Andres Salomona7006c92011-03-17 17:32:35 -0700192 /* process properties */
Rob Herringa0e28c92014-04-02 15:10:14 -0500193 for (offset = fdt_first_property_offset(blob, *poffset);
194 (offset >= 0);
195 (offset = fdt_next_property_offset(blob, offset))) {
196 const char *pname;
197 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700198
Rob Herringa0e28c92014-04-02 15:10:14 -0500199 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
200 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700201 break;
Rob Herringa0e28c92014-04-02 15:10:14 -0500202 }
Grant Likelybbd33932009-11-23 20:07:00 -0700203
Grant Likelybbd33932009-11-23 20:07:00 -0700204 if (pname == NULL) {
205 pr_info("Can't find property name in list !\n");
206 break;
207 }
208 if (strcmp(pname, "name") == 0)
209 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700210 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
211 __alignof__(struct property));
212 if (allnextpp) {
David Gibson04b954a2010-02-01 21:34:15 -0700213 /* We accept flattened tree phandles either in
214 * ePAPR-style "phandle" properties, or the
215 * legacy "linux,phandle" properties. If both
216 * appear and have different values, things
217 * will get weird. Don't do that. */
218 if ((strcmp(pname, "phandle") == 0) ||
219 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700220 if (np->phandle == 0)
Rob Herringa0e28c92014-04-02 15:10:14 -0500221 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700222 }
David Gibson04b954a2010-02-01 21:34:15 -0700223 /* And we process the "ibm,phandle" property
224 * used in pSeries dynamic device tree
225 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700226 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringa0e28c92014-04-02 15:10:14 -0500227 np->phandle = be32_to_cpup(p);
228 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700229 pp->length = sz;
Rob Herringa0e28c92014-04-02 15:10:14 -0500230 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700231 *prev_pp = pp;
232 prev_pp = &pp->next;
233 }
Grant Likelybbd33932009-11-23 20:07:00 -0700234 }
235 /* with version 0x10 we may not have the name property, recreate
236 * it here from the unit name if absent
237 */
238 if (!has_name) {
Rob Herringa0e28c92014-04-02 15:10:14 -0500239 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700240 int sz;
241
242 while (*p1) {
243 if ((*p1) == '@')
244 pa = p1;
245 if ((*p1) == '/')
246 ps = p1 + 1;
247 p1++;
248 }
249 if (pa < ps)
250 pa = p1;
251 sz = (pa - ps) + 1;
252 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
253 __alignof__(struct property));
254 if (allnextpp) {
255 pp->name = "name";
256 pp->length = sz;
257 pp->value = pp + 1;
258 *prev_pp = pp;
259 prev_pp = &pp->next;
260 memcpy(pp->value, ps, sz - 1);
261 ((char *)pp->value)[sz - 1] = 0;
262 pr_debug("fixed up name for %s -> %s\n", pathp,
263 (char *)pp->value);
264 }
265 }
266 if (allnextpp) {
267 *prev_pp = NULL;
268 np->name = of_get_property(np, "name", NULL);
269 np->type = of_get_property(np, "device_type", NULL);
270
271 if (!np->name)
272 np->name = "<NULL>";
273 if (!np->type)
274 np->type = "<NULL>";
275 }
Rob Herringa0e28c92014-04-02 15:10:14 -0500276
277 old_depth = depth;
278 *poffset = fdt_next_node(blob, *poffset, &depth);
279 if (depth < 0)
280 depth = 0;
281 while (*poffset > 0 && depth > old_depth)
282 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
283 fpsize);
284
285 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
286 pr_err("unflatten: error %d processing FDT\n", *poffset);
287
Grant Likelybbd33932009-11-23 20:07:00 -0700288 return mem;
289}
Grant Likely41f88002009-11-23 20:07:01 -0700290
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800291/**
292 * __unflatten_device_tree - create tree of device_nodes from flat blob
293 *
294 * unflattens a device-tree, creating the
295 * tree of struct device_node. It also fills the "name" and "type"
296 * pointers of the nodes so the normal device-tree walking functions
297 * can be used.
298 * @blob: The blob to expand
299 * @mynodes: The device_node tree created by the call
300 * @dt_alloc: An allocator that provides a virtual address to memory
301 * for the resulting tree
302 */
Andres Salomona7006c92011-03-17 17:32:35 -0700303static void __unflatten_device_tree(struct boot_param_header *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800304 struct device_node **mynodes,
305 void * (*dt_alloc)(u64 size, u64 align))
306{
Rob Herringa0e28c92014-04-02 15:10:14 -0500307 unsigned long size;
308 int start;
309 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800310 struct device_node **allnextp = mynodes;
311
312 pr_debug(" -> unflatten_device_tree()\n");
313
314 if (!blob) {
315 pr_debug("No device tree pointer\n");
316 return;
317 }
318
319 pr_debug("Unflattening device tree:\n");
320 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
321 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
322 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
323
324 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
325 pr_err("Invalid device tree blob header\n");
326 return;
327 }
328
329 /* First pass, scan for size */
Rob Herringa0e28c92014-04-02 15:10:14 -0500330 start = 0;
331 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
332 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800333
334 pr_debug(" size is %lx, allocating...\n", size);
335
336 /* Allocate memory for the expanded device tree */
Rob Herringa0e28c92014-04-02 15:10:14 -0500337 mem = dt_alloc(size + 4, __alignof__(struct device_node));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800338
Wladislav Wiebeabcdf872013-08-12 13:06:53 +0200339 memset((void *)mem, 0, size);
340
Wladislav Wiebef80b1f82013-08-12 13:06:53 +0200341 memset((void *)mem, 0, size);
342
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800343 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
344
Rob Herringa0e28c92014-04-02 15:10:14 -0500345 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800346
347 /* Second pass, do actual unflattening */
Rob Herringa0e28c92014-04-02 15:10:14 -0500348 start = 0;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800349 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
Rob Herringa0e28c92014-04-02 15:10:14 -0500350 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800351 pr_warning("End of tree marker overwritten: %08x\n",
352 be32_to_cpu(((__be32 *)mem)[size / 4]));
353 *allnextp = NULL;
354
355 pr_debug(" <- unflatten_device_tree()\n");
356}
357
358static void *kernel_tree_alloc(u64 size, u64 align)
359{
360 return kzalloc(size, GFP_KERNEL);
361}
362
363/**
364 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
365 *
366 * unflattens the device-tree passed by the firmware, creating the
367 * tree of struct device_node. It also fills the "name" and "type"
368 * pointers of the nodes so the normal device-tree walking functions
369 * can be used.
370 */
371void of_fdt_unflatten_tree(unsigned long *blob,
372 struct device_node **mynodes)
373{
374 struct boot_param_header *device_tree =
375 (struct boot_param_header *)blob;
376 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
377}
378EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
379
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800380/* Everything below here references initial_boot_params directly. */
381int __initdata dt_root_addr_cells;
382int __initdata dt_root_size_cells;
383
384struct boot_param_header *initial_boot_params;
385
386#ifdef CONFIG_OF_EARLY_FLATTREE
387
388/**
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100389 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
390 */
391static int __init __reserved_mem_reserve_reg(unsigned long node,
392 const char *uname)
393{
394 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
395 phys_addr_t base, size;
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100396 int len;
397 const __be32 *prop;
Marek Szyprowski5b8f8282014-02-28 14:42:48 +0100398 int nomap, first = 1;
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100399
400 prop = of_get_flat_dt_prop(node, "reg", &len);
401 if (!prop)
402 return -ENOENT;
403
404 if (len && len % t_len != 0) {
405 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
406 uname);
407 return -EINVAL;
408 }
409
410 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
411
412 while (len >= t_len) {
413 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
414 size = dt_mem_next_cell(dt_root_size_cells, &prop);
415
416 if (base && size &&
417 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
418 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
419 uname, &base, (unsigned long)size / SZ_1M);
420 else
421 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
422 uname, &base, (unsigned long)size / SZ_1M);
423
424 len -= t_len;
Marek Szyprowski5b8f8282014-02-28 14:42:48 +0100425 if (first) {
426 fdt_reserved_mem_save_node(node, uname, base, size);
427 first = 0;
428 }
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100429 }
430 return 0;
431}
432
433/**
434 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
435 * in /reserved-memory matches the values supported by the current implementation,
436 * also check if ranges property has been provided
437 */
Xiubo Li550c31c2014-04-08 13:48:07 +0800438static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100439{
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100440 const __be32 *prop;
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100441
442 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
443 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
444 return -EINVAL;
445
446 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
447 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
448 return -EINVAL;
449
450 prop = of_get_flat_dt_prop(node, "ranges", NULL);
451 if (!prop)
452 return -EINVAL;
453 return 0;
454}
455
456/**
457 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
458 */
459static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
460 int depth, void *data)
461{
462 static int found;
463 const char *status;
Marek Szyprowski5b8f8282014-02-28 14:42:48 +0100464 int err;
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100465
466 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
467 if (__reserved_mem_check_root(node) != 0) {
468 pr_err("Reserved memory: unsupported node format, ignoring\n");
469 /* break scan */
470 return 1;
471 }
472 found = 1;
473 /* scan next node */
474 return 0;
475 } else if (!found) {
476 /* scan next node */
477 return 0;
478 } else if (found && depth < 2) {
479 /* scanning of /reserved-memory has been finished */
480 return 1;
481 }
482
483 status = of_get_flat_dt_prop(node, "status", NULL);
484 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
485 return 0;
486
Marek Szyprowski5b8f8282014-02-28 14:42:48 +0100487 err = __reserved_mem_reserve_reg(node, uname);
488 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
489 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100490
491 /* scan next node */
492 return 0;
493}
494
495/**
496 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
497 *
498 * This function grabs memory from early allocator for device exclusive use
499 * defined in device tree structures. It should be called by arch specific code
500 * once the early allocator (i.e. memblock) has been fully activated.
501 */
502void __init early_init_fdt_scan_reserved_mem(void)
503{
Josh Cartwrightb94c8be2014-03-13 16:36:36 -0500504 if (!initial_boot_params)
505 return;
506
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100507 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski5b8f8282014-02-28 14:42:48 +0100508 fdt_init_reserved_mem();
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100509}
510
511/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800512 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
513 * @it: callback function
514 * @data: context data pointer
515 *
516 * This function is used to scan the flattened device-tree, it is
517 * used to extract the memory information at boot before we can
518 * unflatten the tree
519 */
520int __init of_scan_flat_dt(int (*it)(unsigned long node,
521 const char *uname, int depth,
522 void *data),
523 void *data)
524{
Rob Herringa0e28c92014-04-02 15:10:14 -0500525 const void *blob = initial_boot_params;
526 const char *pathp;
527 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800528
Rob Herringa0e28c92014-04-02 15:10:14 -0500529 for (offset = fdt_next_node(blob, -1, &depth);
530 offset >= 0 && depth >= 0 && !rc;
531 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800532
Rob Herringa0e28c92014-04-02 15:10:14 -0500533 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800534 if (*pathp == '/')
535 pathp = kbasename(pathp);
Rob Herringa0e28c92014-04-02 15:10:14 -0500536 rc = it(offset, pathp, depth, data);
537 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800538 return rc;
539}
540
541/**
542 * of_get_flat_dt_root - find the root node in the flat blob
543 */
544unsigned long __init of_get_flat_dt_root(void)
545{
Rob Herringa0e28c92014-04-02 15:10:14 -0500546 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800547}
548
549/**
550 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
551 *
552 * This function can be used within scan_flattened_dt callback to get
553 * access to properties
554 */
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100555const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
556 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800557{
Rob Herringa0e28c92014-04-02 15:10:14 -0500558 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800559}
560
561/**
562 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
563 * @node: node to test
564 * @compat: compatible string to compare with compatible list.
565 */
566int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
567{
568 return of_fdt_is_compatible(initial_boot_params, node, compat);
569}
570
Grant Likelya4f740c2010-10-30 11:49:09 -0400571/**
572 * of_flat_dt_match - Return true if node matches a list of compatible values
573 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100574int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400575{
576 return of_fdt_match(initial_boot_params, node, compat);
577}
578
Marek Szyprowski3e129882013-08-26 14:41:56 +0200579struct fdt_scan_status {
580 const char *name;
581 int namelen;
582 int depth;
583 int found;
584 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
585 void *data;
586};
587
Grant Likelyf7b3a832009-11-24 03:26:58 -0700588#ifdef CONFIG_BLK_DEV_INITRD
589/**
590 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
591 * @node: reference to node containing initrd location ('chosen')
592 */
593void __init early_init_dt_check_for_initrd(unsigned long node)
594{
Santosh Shilimkar9a87b8d2013-07-01 14:20:35 -0400595 u64 start, end;
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100596 int len;
597 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700598
599 pr_debug("Looking for initrd properties... ");
600
601 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700602 if (!prop)
603 return;
Santosh Shilimkar9a87b8d2013-07-01 14:20:35 -0400604 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700605
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700606 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
607 if (!prop)
608 return;
Santosh Shilimkar9a87b8d2013-07-01 14:20:35 -0400609 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700610
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700611 early_init_dt_setup_initrd_arch(start, end);
Santosh Shilimkar9a87b8d2013-07-01 14:20:35 -0400612 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
613 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700614}
615#else
616inline void early_init_dt_check_for_initrd(unsigned long node)
617{
618}
619#endif /* CONFIG_BLK_DEV_INITRD */
620
Grant Likely41f88002009-11-23 20:07:01 -0700621/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700622 * early_init_dt_scan_root - fetch the top level address and size cells
623 */
624int __init early_init_dt_scan_root(unsigned long node, const char *uname,
625 int depth, void *data)
626{
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100627 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700628
629 if (depth != 0)
630 return 0;
631
Jeremy Kerr33714882010-01-30 01:45:26 -0700632 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
633 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
634
Grant Likelyf00abd92009-11-24 03:27:10 -0700635 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700636 if (prop)
637 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700638 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
639
640 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700641 if (prop)
642 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700643 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
644
645 /* break now */
646 return 1;
647}
648
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100649u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700650{
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100651 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700652
653 *cellp = p + s;
654 return of_read_number(p, s);
655}
656
Grant Likely51975db2010-02-01 21:34:14 -0700657/**
658 * early_init_dt_scan_memory - Look for an parse memory nodes
659 */
660int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
661 int depth, void *data)
662{
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100663 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
664 const __be32 *reg, *endp;
665 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700666
667 /* We are scanning "memory" nodes only */
668 if (type == NULL) {
669 /*
670 * The longtrail doesn't have a device_type on the
671 * /memory node, so look for the node called /memory@0.
672 */
673 if (depth != 1 || strcmp(uname, "memory@0") != 0)
674 return 0;
675 } else if (strcmp(type, "memory") != 0)
676 return 0;
677
678 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
679 if (reg == NULL)
680 reg = of_get_flat_dt_prop(node, "reg", &l);
681 if (reg == NULL)
682 return 0;
683
684 endp = reg + (l / sizeof(__be32));
685
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100686 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700687 uname, l, reg[0], reg[1], reg[2], reg[3]);
688
689 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
690 u64 base, size;
691
692 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
693 size = dt_mem_next_cell(dt_root_size_cells, &reg);
694
695 if (size == 0)
696 continue;
697 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
698 (unsigned long long)size);
699
700 early_init_dt_add_memory_arch(base, size);
701 }
702
703 return 0;
704}
705
Grant Likely86e03222009-12-10 23:42:21 -0700706int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
707 int depth, void *data)
708{
Mark Brownd6c2d4f2014-07-24 21:06:21 +0100709 int l;
710 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700711
712 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
713
Grant Likely85f60ae2011-04-29 00:18:16 -0600714 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700715 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
716 return 0;
717
718 early_init_dt_check_for_initrd(node);
719
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300720 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700721 p = of_get_flat_dt_prop(node, "bootargs", &l);
722 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600723 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700724
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000725 /*
726 * CONFIG_CMDLINE is meant to be a default in case nothing else
727 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
728 * is set in which case we override whatever was found earlier.
729 */
Grant Likely86e03222009-12-10 23:42:21 -0700730#ifdef CONFIG_CMDLINE
731#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000732 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700733#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600734 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700735#endif /* CONFIG_CMDLINE */
736
Grant Likely85f60ae2011-04-29 00:18:16 -0600737 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700738
739 /* break now */
740 return 1;
741}
742
Marek Szyprowski73eebf32014-02-28 14:42:47 +0100743#ifdef CONFIG_HAVE_MEMBLOCK
744void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
745{
746 const u64 phys_offset = __pa(PAGE_OFFSET);
747 base &= PAGE_MASK;
748 size &= PAGE_MASK;
749 if (base + size < phys_offset) {
750 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
751 base, base + size);
752 return;
753 }
754 if (base < phys_offset) {
755 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
756 base, phys_offset);
757 size -= phys_offset - base;
758 base = phys_offset;
759 }
760 memblock_add(base, size);
761}
762
763int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
764 phys_addr_t size, bool nomap)
765{
766 if (memblock_is_region_reserved(base, size))
767 return -EBUSY;
768 if (nomap)
769 return memblock_remove(base, size);
770 return memblock_reserve(base, size);
771}
772
773/*
774 * called from unflatten_device_tree() to bootstrap devicetree itself
775 * Architectures can override this definition if memblock isn't used
776 */
777void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
778{
779 return __va(memblock_alloc(size, align));
780}
781#else
782int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
783 phys_addr_t size, bool nomap)
784{
785 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
786 base, size, nomap ? " (nomap)" : "");
787 return -ENOSYS;
788}
789#endif
790
791bool __init early_init_dt_scan(void *params)
792{
793 if (!params)
794 return false;
795
796 /* Setup flat device-tree pointer */
797 initial_boot_params = params;
798
799 /* check device tree validity */
800 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
801 initial_boot_params = NULL;
802 return false;
803 }
804
805 /* Retrieve various information from the /chosen node */
806 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
807
808 /* Initialize {size,address}-cells info */
809 of_scan_flat_dt(early_init_dt_scan_root, NULL);
810
811 /* Setup memory, calling early_init_dt_add_memory_arch */
812 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
813
814 return true;
815}
816
Grant Likelyf00abd92009-11-24 03:27:10 -0700817/**
Grant Likely41f88002009-11-23 20:07:01 -0700818 * unflatten_device_tree - create tree of device_nodes from flat blob
819 *
820 * unflattens the device-tree passed by the firmware, creating the
821 * tree of struct device_node. It also fills the "name" and "type"
822 * pointers of the nodes so the normal device-tree walking functions
823 * can be used.
824 */
825void __init unflatten_device_tree(void)
826{
Randy Dunlap465aac62012-11-30 10:01:51 +0000827 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700828 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700829
Shawn Guo611cad72011-08-15 15:28:14 +0800830 /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
831 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700832}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800833
834#endif /* CONFIG_OF_EARLY_FLATTREE */