blob: 1d30b9f964663dcedec2613e6173526ecf15984e [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>
Grant Likelya1727da2013-08-28 21:18:32 +010014#include <linux/memblock.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070015#include <linux/of.h>
16#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010017#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-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>
Rob Herringe6a69282014-04-02 15:10:14 -050022#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050023#include <linux/debugfs.h>
Rob Herringfb11ffe2014-03-27 08:07:01 -050024#include <linux/serial_core.h>
Grant Likely51975db2010-02-01 21:34:14 -070025
Fabio Estevamc89810a2012-01-02 14:19:03 -020026#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070027#include <asm/page.h>
28
Laura Abbott704033c2014-07-15 10:03:35 -070029/*
30 * of_fdt_limit_memory - limit the number of regions in the /memory node
31 * @limit: maximum entries
32 *
33 * Adjust the flattened device tree to have at most 'limit' number of
34 * memory entries in the /memory node. This function may be called
35 * any time after initial_boot_param is set.
36 */
37void of_fdt_limit_memory(int limit)
38{
39 int memory;
40 int len;
41 const void *val;
42 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
43 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
44 const uint32_t *addr_prop;
45 const uint32_t *size_prop;
46 int root_offset;
47 int cell_size;
48
49 root_offset = fdt_path_offset(initial_boot_params, "/");
50 if (root_offset < 0)
51 return;
52
53 addr_prop = fdt_getprop(initial_boot_params, root_offset,
54 "#address-cells", NULL);
55 if (addr_prop)
56 nr_address_cells = fdt32_to_cpu(*addr_prop);
57
58 size_prop = fdt_getprop(initial_boot_params, root_offset,
59 "#size-cells", NULL);
60 if (size_prop)
61 nr_size_cells = fdt32_to_cpu(*size_prop);
62
63 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
64
65 memory = fdt_path_offset(initial_boot_params, "/memory");
66 if (memory > 0) {
67 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
68 if (len > limit*cell_size) {
69 len = limit*cell_size;
70 pr_debug("Limiting number of entries to %d\n", limit);
71 fdt_setprop(initial_boot_params, memory, "reg", val,
72 len);
73 }
74 }
75}
76
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080077/**
78 * of_fdt_is_compatible - Return true if given node from the given blob has
79 * compat in its compatible list
80 * @blob: A device tree blob
81 * @node: node to test
82 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040083 *
84 * On match, returns a non-zero value with smaller values returned for more
85 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080086 */
Rob Herringc972de12014-04-01 22:48:01 -050087int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080088 unsigned long node, const char *compat)
89{
90 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050091 int cplen;
92 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080093
Rob Herringe6a69282014-04-02 15:10:14 -050094 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080095 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 */
Rob Herringc972de12014-04-01 22:48:01 -0500112int of_fdt_match(const void *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
Grant Likely44856812013-08-29 13:30:35 +0100130static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700131 unsigned long align)
132{
133 void *res;
134
Grant Likely44856812013-08-29 13:30:35 +0100135 *mem = PTR_ALIGN(*mem, align);
136 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700137 *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
Grant Likelybbd33932009-11-23 20:07:00 -0700148 * @fpsize: Size of the node path up at the current depth.
149 */
Rob Herringc972de12014-04-01 22:48:01 -0500150static void * unflatten_dt_node(void *blob,
Grant Likely44856812013-08-29 13:30:35 +0100151 void *mem,
Rob Herringe6a69282014-04-02 15:10:14 -0500152 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800153 struct device_node *dad,
Grant Likely5063e252014-10-03 16:28:27 +0100154 struct device_node **nodepp,
155 unsigned long fpsize,
156 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700157{
Rob Herringe6a69282014-04-02 15:10:14 -0500158 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700159 struct device_node *np;
160 struct property *pp, **prev_pp = NULL;
Rob Herringe6a69282014-04-02 15:10:14 -0500161 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700162 unsigned int l, allocl;
Rob Herringe6a69282014-04-02 15:10:14 -0500163 static int depth = 0;
164 int old_depth;
165 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700166 int has_name = 0;
167 int new_format = 0;
168
Rob Herringe6a69282014-04-02 15:10:14 -0500169 pathp = fdt_get_name(blob, *poffset, &l);
170 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700171 return mem;
Rob Herringe6a69282014-04-02 15:10:14 -0500172
173 allocl = l++;
Grant Likelybbd33932009-11-23 20:07:00 -0700174
175 /* version 0x10 has a more compact unit name here instead of the full
176 * path. we accumulate the full path size using "fpsize", we'll rebuild
177 * it later. We detect this because the first character of the name is
178 * not '/'.
179 */
180 if ((*pathp) != '/') {
181 new_format = 1;
182 if (fpsize == 0) {
183 /* root node: special case. fpsize accounts for path
184 * plus terminating zero. root node only has '/', so
185 * fpsize should be 2, but we want to avoid the first
186 * level nodes to have two '/' so we use fpsize 1 here
187 */
188 fpsize = 1;
189 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000190 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500191 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700192 } else {
193 /* account for '/' and path size minus terminal 0
194 * already in 'l'
195 */
196 fpsize += l;
197 allocl = fpsize;
198 }
199 }
200
201 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
202 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100203 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000204 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200205 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000206 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700207 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700208 /* rebuild full path for new format */
209 if (dad && dad->parent) {
210 strcpy(fn, dad->full_name);
211#ifdef DEBUG
212 if ((strlen(fn) + l + 1) != allocl) {
213 pr_debug("%s: p: %d, l: %d, a: %d\n",
214 pathp, (int)strlen(fn),
215 l, allocl);
216 }
217#endif
218 fn += strlen(fn);
219 }
220 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000221 }
222 memcpy(fn, pathp, l);
223
Grant Likelybbd33932009-11-23 20:07:00 -0700224 prev_pp = &np->properties;
Grant Likelybbd33932009-11-23 20:07:00 -0700225 if (dad != NULL) {
226 np->parent = dad;
227 /* we temporarily use the next field as `last_child'*/
228 if (dad->next == NULL)
229 dad->child = np;
230 else
231 dad->next->sibling = np;
232 dad->next = np;
233 }
Grant Likelybbd33932009-11-23 20:07:00 -0700234 }
Andres Salomona7006c92011-03-17 17:32:35 -0700235 /* process properties */
Rob Herringe6a69282014-04-02 15:10:14 -0500236 for (offset = fdt_first_property_offset(blob, *poffset);
237 (offset >= 0);
238 (offset = fdt_next_property_offset(blob, offset))) {
239 const char *pname;
240 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700241
Rob Herringe6a69282014-04-02 15:10:14 -0500242 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
243 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700244 break;
Rob Herringe6a69282014-04-02 15:10:14 -0500245 }
Grant Likelybbd33932009-11-23 20:07:00 -0700246
Grant Likelybbd33932009-11-23 20:07:00 -0700247 if (pname == NULL) {
248 pr_info("Can't find property name in list !\n");
249 break;
250 }
251 if (strcmp(pname, "name") == 0)
252 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700253 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
254 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100255 if (!dryrun) {
David Gibson04b954a2010-02-01 21:34:15 -0700256 /* We accept flattened tree phandles either in
257 * ePAPR-style "phandle" properties, or the
258 * legacy "linux,phandle" properties. If both
259 * appear and have different values, things
260 * will get weird. Don't do that. */
261 if ((strcmp(pname, "phandle") == 0) ||
262 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700263 if (np->phandle == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500264 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700265 }
David Gibson04b954a2010-02-01 21:34:15 -0700266 /* And we process the "ibm,phandle" property
267 * used in pSeries dynamic device tree
268 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700269 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500270 np->phandle = be32_to_cpup(p);
271 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700272 pp->length = sz;
Rob Herringe6a69282014-04-02 15:10:14 -0500273 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700274 *prev_pp = pp;
275 prev_pp = &pp->next;
276 }
Grant Likelybbd33932009-11-23 20:07:00 -0700277 }
278 /* with version 0x10 we may not have the name property, recreate
279 * it here from the unit name if absent
280 */
281 if (!has_name) {
Rob Herringe6a69282014-04-02 15:10:14 -0500282 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700283 int sz;
284
285 while (*p1) {
286 if ((*p1) == '@')
287 pa = p1;
288 if ((*p1) == '/')
289 ps = p1 + 1;
290 p1++;
291 }
292 if (pa < ps)
293 pa = p1;
294 sz = (pa - ps) + 1;
295 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
296 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100297 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700298 pp->name = "name";
299 pp->length = sz;
300 pp->value = pp + 1;
301 *prev_pp = pp;
302 prev_pp = &pp->next;
303 memcpy(pp->value, ps, sz - 1);
304 ((char *)pp->value)[sz - 1] = 0;
305 pr_debug("fixed up name for %s -> %s\n", pathp,
306 (char *)pp->value);
307 }
308 }
Grant Likely5063e252014-10-03 16:28:27 +0100309 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700310 *prev_pp = NULL;
311 np->name = of_get_property(np, "name", NULL);
312 np->type = of_get_property(np, "device_type", NULL);
313
314 if (!np->name)
315 np->name = "<NULL>";
316 if (!np->type)
317 np->type = "<NULL>";
318 }
Rob Herringe6a69282014-04-02 15:10:14 -0500319
320 old_depth = depth;
321 *poffset = fdt_next_node(blob, *poffset, &depth);
322 if (depth < 0)
323 depth = 0;
324 while (*poffset > 0 && depth > old_depth)
Grant Likely5063e252014-10-03 16:28:27 +0100325 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
326 fpsize, dryrun);
Rob Herringe6a69282014-04-02 15:10:14 -0500327
328 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
329 pr_err("unflatten: error %d processing FDT\n", *poffset);
Grant Likely5063e252014-10-03 16:28:27 +0100330 if (nodepp)
331 *nodepp = np;
Rob Herringe6a69282014-04-02 15:10:14 -0500332
Grant Likelybbd33932009-11-23 20:07:00 -0700333 return mem;
334}
Grant Likely41f88002009-11-23 20:07:01 -0700335
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800336/**
337 * __unflatten_device_tree - create tree of device_nodes from flat blob
338 *
339 * unflattens a device-tree, creating the
340 * tree of struct device_node. It also fills the "name" and "type"
341 * pointers of the nodes so the normal device-tree walking functions
342 * can be used.
343 * @blob: The blob to expand
344 * @mynodes: The device_node tree created by the call
345 * @dt_alloc: An allocator that provides a virtual address to memory
346 * for the resulting tree
347 */
Rob Herringc972de12014-04-01 22:48:01 -0500348static void __unflatten_device_tree(void *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800349 struct device_node **mynodes,
350 void * (*dt_alloc)(u64 size, u64 align))
351{
Grant Likely44856812013-08-29 13:30:35 +0100352 unsigned long size;
Rob Herringe6a69282014-04-02 15:10:14 -0500353 int start;
354 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800355
356 pr_debug(" -> unflatten_device_tree()\n");
357
358 if (!blob) {
359 pr_debug("No device tree pointer\n");
360 return;
361 }
362
363 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500364 pr_debug("magic: %08x\n", fdt_magic(blob));
365 pr_debug("size: %08x\n", fdt_totalsize(blob));
366 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800367
Rob Herringc972de12014-04-01 22:48:01 -0500368 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800369 pr_err("Invalid device tree blob header\n");
370 return;
371 }
372
373 /* First pass, scan for size */
Rob Herringe6a69282014-04-02 15:10:14 -0500374 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100375 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
Grant Likely44856812013-08-29 13:30:35 +0100376 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800377
378 pr_debug(" size is %lx, allocating...\n", size);
379
380 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100381 mem = dt_alloc(size + 4, __alignof__(struct device_node));
382 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800383
Grant Likely44856812013-08-29 13:30:35 +0100384 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200385
Grant Likely44856812013-08-29 13:30:35 +0100386 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800387
388 /* Second pass, do actual unflattening */
Rob Herringe6a69282014-04-02 15:10:14 -0500389 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100390 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
Grant Likely44856812013-08-29 13:30:35 +0100391 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800392 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100393 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800394
395 pr_debug(" <- unflatten_device_tree()\n");
396}
397
398static void *kernel_tree_alloc(u64 size, u64 align)
399{
400 return kzalloc(size, GFP_KERNEL);
401}
402
403/**
404 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
405 *
406 * unflattens the device-tree passed by the firmware, creating the
407 * tree of struct device_node. It also fills the "name" and "type"
408 * pointers of the nodes so the normal device-tree walking functions
409 * can be used.
410 */
411void of_fdt_unflatten_tree(unsigned long *blob,
412 struct device_node **mynodes)
413{
Rob Herringc972de12014-04-01 22:48:01 -0500414 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800415}
416EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
417
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800418/* Everything below here references initial_boot_params directly. */
419int __initdata dt_root_addr_cells;
420int __initdata dt_root_size_cells;
421
Rob Herring1daa0c42014-03-31 15:25:04 -0500422void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800423
424#ifdef CONFIG_OF_EARLY_FLATTREE
425
426/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100427 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
428 */
429static int __init __reserved_mem_reserve_reg(unsigned long node,
430 const char *uname)
431{
432 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
433 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500434 int len;
435 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100436 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100437
438 prop = of_get_flat_dt_prop(node, "reg", &len);
439 if (!prop)
440 return -ENOENT;
441
442 if (len && len % t_len != 0) {
443 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
444 uname);
445 return -EINVAL;
446 }
447
448 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
449
450 while (len >= t_len) {
451 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
452 size = dt_mem_next_cell(dt_root_size_cells, &prop);
453
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400454 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100455 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
456 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
457 uname, &base, (unsigned long)size / SZ_1M);
458 else
459 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
460 uname, &base, (unsigned long)size / SZ_1M);
461
462 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100463 if (first) {
464 fdt_reserved_mem_save_node(node, uname, base, size);
465 first = 0;
466 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100467 }
468 return 0;
469}
470
471/**
472 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
473 * in /reserved-memory matches the values supported by the current implementation,
474 * also check if ranges property has been provided
475 */
Xiubo Li5b624112014-04-08 13:48:07 +0800476static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100477{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500478 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100479
480 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
481 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
482 return -EINVAL;
483
484 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
485 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
486 return -EINVAL;
487
488 prop = of_get_flat_dt_prop(node, "ranges", NULL);
489 if (!prop)
490 return -EINVAL;
491 return 0;
492}
493
494/**
495 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
496 */
497static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
498 int depth, void *data)
499{
500 static int found;
501 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100502 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100503
504 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
505 if (__reserved_mem_check_root(node) != 0) {
506 pr_err("Reserved memory: unsupported node format, ignoring\n");
507 /* break scan */
508 return 1;
509 }
510 found = 1;
511 /* scan next node */
512 return 0;
513 } else if (!found) {
514 /* scan next node */
515 return 0;
516 } else if (found && depth < 2) {
517 /* scanning of /reserved-memory has been finished */
518 return 1;
519 }
520
521 status = of_get_flat_dt_prop(node, "status", NULL);
522 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
523 return 0;
524
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100525 err = __reserved_mem_reserve_reg(node, uname);
526 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
527 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100528
529 /* scan next node */
530 return 0;
531}
532
533/**
534 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
535 *
536 * This function grabs memory from early allocator for device exclusive use
537 * defined in device tree structures. It should be called by arch specific code
538 * once the early allocator (i.e. memblock) has been fully activated.
539 */
540void __init early_init_fdt_scan_reserved_mem(void)
541{
Rob Herringd1552ce2014-04-01 22:46:48 -0500542 int n;
543 u64 base, size;
544
Josh Cartwright2040b522014-03-13 16:36:36 -0500545 if (!initial_boot_params)
546 return;
547
Rob Herringd1552ce2014-04-01 22:46:48 -0500548 /* Reserve the dtb region */
549 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
550 fdt_totalsize(initial_boot_params),
551 0);
552
553 /* Process header /memreserve/ fields */
554 for (n = 0; ; n++) {
555 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
556 if (!size)
557 break;
558 early_init_dt_reserve_memory_arch(base, size, 0);
559 }
560
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100561 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100562 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100563}
564
565/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800566 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
567 * @it: callback function
568 * @data: context data pointer
569 *
570 * This function is used to scan the flattened device-tree, it is
571 * used to extract the memory information at boot before we can
572 * unflatten the tree
573 */
574int __init of_scan_flat_dt(int (*it)(unsigned long node,
575 const char *uname, int depth,
576 void *data),
577 void *data)
578{
Rob Herringe6a69282014-04-02 15:10:14 -0500579 const void *blob = initial_boot_params;
580 const char *pathp;
581 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800582
Rob Herringe6a69282014-04-02 15:10:14 -0500583 for (offset = fdt_next_node(blob, -1, &depth);
584 offset >= 0 && depth >= 0 && !rc;
585 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800586
Rob Herringe6a69282014-04-02 15:10:14 -0500587 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800588 if (*pathp == '/')
589 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500590 rc = it(offset, pathp, depth, data);
591 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800592 return rc;
593}
594
595/**
596 * of_get_flat_dt_root - find the root node in the flat blob
597 */
598unsigned long __init of_get_flat_dt_root(void)
599{
Rob Herringe6a69282014-04-02 15:10:14 -0500600 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800601}
602
603/**
Rob Herringc0556d32014-04-22 12:55:10 -0500604 * of_get_flat_dt_size - Return the total size of the FDT
605 */
606int __init of_get_flat_dt_size(void)
607{
608 return fdt_totalsize(initial_boot_params);
609}
610
611/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800612 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
613 *
614 * This function can be used within scan_flattened_dt callback to get
615 * access to properties
616 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500617const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
618 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800619{
Rob Herringe6a69282014-04-02 15:10:14 -0500620 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800621}
622
623/**
624 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
625 * @node: node to test
626 * @compat: compatible string to compare with compatible list.
627 */
628int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
629{
630 return of_fdt_is_compatible(initial_boot_params, node, compat);
631}
632
Grant Likelya4f740c2010-10-30 11:49:09 -0400633/**
634 * of_flat_dt_match - Return true if node matches a list of compatible values
635 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100636int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400637{
638 return of_fdt_match(initial_boot_params, node, compat);
639}
640
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200641struct fdt_scan_status {
642 const char *name;
643 int namelen;
644 int depth;
645 int found;
646 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
647 void *data;
648};
649
Rob Herring6a903a22013-08-27 21:41:56 -0500650const char * __init of_flat_dt_get_machine_name(void)
651{
652 const char *name;
653 unsigned long dt_root = of_get_flat_dt_root();
654
655 name = of_get_flat_dt_prop(dt_root, "model", NULL);
656 if (!name)
657 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
658 return name;
659}
660
661/**
662 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
663 *
664 * @default_match: A machine specific ptr to return in case of no match.
665 * @get_next_compat: callback function to return next compatible match table.
666 *
667 * Iterate through machine match tables to find the best match for the machine
668 * compatible string in the FDT.
669 */
670const void * __init of_flat_dt_match_machine(const void *default_match,
671 const void * (*get_next_compat)(const char * const**))
672{
673 const void *data = NULL;
674 const void *best_data = default_match;
675 const char *const *compat;
676 unsigned long dt_root;
677 unsigned int best_score = ~1, score = 0;
678
679 dt_root = of_get_flat_dt_root();
680 while ((data = get_next_compat(&compat))) {
681 score = of_flat_dt_match(dt_root, compat);
682 if (score > 0 && score < best_score) {
683 best_data = data;
684 best_score = score;
685 }
686 }
687 if (!best_data) {
688 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500689 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500690
691 pr_err("\n unrecognized device tree list:\n[ ");
692
693 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
694 if (prop) {
695 while (size > 0) {
696 printk("'%s' ", prop);
697 size -= strlen(prop) + 1;
698 prop += strlen(prop) + 1;
699 }
700 }
701 printk("]\n\n");
702 return NULL;
703 }
704
705 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
706
707 return best_data;
708}
709
Grant Likelyf7b3a832009-11-24 03:26:58 -0700710#ifdef CONFIG_BLK_DEV_INITRD
711/**
712 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
713 * @node: reference to node containing initrd location ('chosen')
714 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500715static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700716{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400717 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500718 int len;
719 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700720
721 pr_debug("Looking for initrd properties... ");
722
723 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700724 if (!prop)
725 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400726 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700727
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700728 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
729 if (!prop)
730 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400731 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700732
Rob Herring29eb45a2013-08-30 17:06:53 -0500733 initrd_start = (unsigned long)__va(start);
734 initrd_end = (unsigned long)__va(end);
735 initrd_below_start_ok = 1;
736
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400737 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
738 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700739}
740#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500741static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700742{
743}
744#endif /* CONFIG_BLK_DEV_INITRD */
745
Rob Herringfb11ffe2014-03-27 08:07:01 -0500746#ifdef CONFIG_SERIAL_EARLYCON
747extern struct of_device_id __earlycon_of_table[];
748
749int __init early_init_dt_scan_chosen_serial(void)
750{
751 int offset;
752 const char *p;
753 int l;
754 const struct of_device_id *match = __earlycon_of_table;
755 const void *fdt = initial_boot_params;
756
757 offset = fdt_path_offset(fdt, "/chosen");
758 if (offset < 0)
759 offset = fdt_path_offset(fdt, "/chosen@0");
760 if (offset < 0)
761 return -ENOENT;
762
763 p = fdt_getprop(fdt, offset, "stdout-path", &l);
764 if (!p)
765 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
766 if (!p || !l)
767 return -ENOENT;
768
769 /* Get the node specified by stdout-path */
770 offset = fdt_path_offset(fdt, p);
771 if (offset < 0)
772 return -ENODEV;
773
774 while (match->compatible) {
775 unsigned long addr;
776 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
777 match++;
778 continue;
779 }
780
781 addr = fdt_translate_address(fdt, offset);
782 if (!addr)
783 return -ENXIO;
784
785 of_setup_earlycon(addr, match->data);
786 return 0;
787 }
788 return -ENODEV;
789}
790
791static int __init setup_of_earlycon(char *buf)
792{
793 if (buf)
794 return 0;
795
796 return early_init_dt_scan_chosen_serial();
797}
798early_param("earlycon", setup_of_earlycon);
799#endif
800
Grant Likely41f88002009-11-23 20:07:01 -0700801/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700802 * early_init_dt_scan_root - fetch the top level address and size cells
803 */
804int __init early_init_dt_scan_root(unsigned long node, const char *uname,
805 int depth, void *data)
806{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500807 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700808
809 if (depth != 0)
810 return 0;
811
Jeremy Kerr33714882010-01-30 01:45:26 -0700812 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
813 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
814
Grant Likelyf00abd92009-11-24 03:27:10 -0700815 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700816 if (prop)
817 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700818 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
819
820 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700821 if (prop)
822 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700823 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
824
825 /* break now */
826 return 1;
827}
828
Rob Herring9d0c4df2014-04-01 23:49:03 -0500829u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700830{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500831 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700832
833 *cellp = p + s;
834 return of_read_number(p, s);
835}
836
Grant Likely51975db2010-02-01 21:34:14 -0700837/**
838 * early_init_dt_scan_memory - Look for an parse memory nodes
839 */
840int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
841 int depth, void *data)
842{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500843 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
844 const __be32 *reg, *endp;
845 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700846
847 /* We are scanning "memory" nodes only */
848 if (type == NULL) {
849 /*
850 * The longtrail doesn't have a device_type on the
851 * /memory node, so look for the node called /memory@0.
852 */
Leif Lindholmb44aa252014-04-17 18:42:01 +0100853 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
Grant Likely51975db2010-02-01 21:34:14 -0700854 return 0;
855 } else if (strcmp(type, "memory") != 0)
856 return 0;
857
858 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
859 if (reg == NULL)
860 reg = of_get_flat_dt_prop(node, "reg", &l);
861 if (reg == NULL)
862 return 0;
863
864 endp = reg + (l / sizeof(__be32));
865
Rob Herring9d0c4df2014-04-01 23:49:03 -0500866 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700867 uname, l, reg[0], reg[1], reg[2], reg[3]);
868
869 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
870 u64 base, size;
871
872 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
873 size = dt_mem_next_cell(dt_root_size_cells, &reg);
874
875 if (size == 0)
876 continue;
877 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
878 (unsigned long long)size);
879
880 early_init_dt_add_memory_arch(base, size);
881 }
882
883 return 0;
884}
885
Grant Likely86e03222009-12-10 23:42:21 -0700886int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
887 int depth, void *data)
888{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500889 int l;
890 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700891
892 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
893
Grant Likely85f60ae2011-04-29 00:18:16 -0600894 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700895 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
896 return 0;
897
898 early_init_dt_check_for_initrd(node);
899
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300900 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700901 p = of_get_flat_dt_prop(node, "bootargs", &l);
902 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600903 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700904
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +0000905 /*
906 * CONFIG_CMDLINE is meant to be a default in case nothing else
907 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
908 * is set in which case we override whatever was found earlier.
909 */
Grant Likely86e03222009-12-10 23:42:21 -0700910#ifdef CONFIG_CMDLINE
911#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +0000912 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700913#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600914 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700915#endif /* CONFIG_CMDLINE */
916
Grant Likely85f60ae2011-04-29 00:18:16 -0600917 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700918
919 /* break now */
920 return 1;
921}
922
Grant Likelya1727da2013-08-28 21:18:32 +0100923#ifdef CONFIG_HAVE_MEMBLOCK
Laura Abbott3069f0c2014-07-07 17:45:43 -0700924#define MAX_PHYS_ADDR ((phys_addr_t)~0)
925
Rob Herring068f6312013-09-24 22:20:01 -0500926void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
927{
928 const u64 phys_offset = __pa(PAGE_OFFSET);
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200929
930 if (!PAGE_ALIGNED(base)) {
931 size -= PAGE_SIZE - (base & ~PAGE_MASK);
932 base = PAGE_ALIGN(base);
933 }
Rob Herring068f6312013-09-24 22:20:01 -0500934 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700935
Laura Abbott3069f0c2014-07-07 17:45:43 -0700936 if (base > MAX_PHYS_ADDR) {
937 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
938 base, base + size);
939 return;
940 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700941
Srinivas Kandagatla9aacd602014-09-23 10:59:09 +0100942 if (base + size - 1 > MAX_PHYS_ADDR) {
943 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
944 ((u64)MAX_PHYS_ADDR) + 1, base + size);
945 size = MAX_PHYS_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700946 }
947
Rob Herring068f6312013-09-24 22:20:01 -0500948 if (base + size < phys_offset) {
949 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
950 base, base + size);
951 return;
952 }
953 if (base < phys_offset) {
954 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
955 base, phys_offset);
956 size -= phys_offset - base;
957 base = phys_offset;
958 }
959 memblock_add(base, size);
960}
961
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100962int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
963 phys_addr_t size, bool nomap)
964{
965 if (memblock_is_region_reserved(base, size))
966 return -EBUSY;
967 if (nomap)
968 return memblock_remove(base, size);
969 return memblock_reserve(base, size);
970}
971
Grant Likelya1727da2013-08-28 21:18:32 +0100972/*
973 * called from unflatten_device_tree() to bootstrap devicetree itself
974 * Architectures can override this definition if memblock isn't used
975 */
976void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
977{
978 return __va(memblock_alloc(size, align));
979}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100980#else
981int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
982 phys_addr_t size, bool nomap)
983{
Rob Herring1d1a6612014-04-22 12:50:24 -0500984 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
985 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100986 return -ENOSYS;
987}
Grant Likelya1727da2013-08-28 21:18:32 +0100988#endif
989
Laura Abbott4972a742014-07-15 10:03:34 -0700990bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -0500991{
992 if (!params)
993 return false;
994
995 /* Setup flat device-tree pointer */
996 initial_boot_params = params;
997
998 /* check device tree validity */
Rob Herringc972de12014-04-01 22:48:01 -0500999 if (fdt_check_header(params)) {
Rob Herring0288ffcb2013-08-26 09:47:40 -05001000 initial_boot_params = NULL;
1001 return false;
1002 }
1003
Laura Abbott4972a742014-07-15 10:03:34 -07001004 return true;
1005}
1006
1007
1008void __init early_init_dt_scan_nodes(void)
1009{
Rob Herring0288ffcb2013-08-26 09:47:40 -05001010 /* Retrieve various information from the /chosen node */
1011 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1012
1013 /* Initialize {size,address}-cells info */
1014 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1015
1016 /* Setup memory, calling early_init_dt_add_memory_arch */
1017 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001018}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001019
Laura Abbott4972a742014-07-15 10:03:34 -07001020bool __init early_init_dt_scan(void *params)
1021{
1022 bool status;
1023
1024 status = early_init_dt_verify(params);
1025 if (!status)
1026 return false;
1027
1028 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001029 return true;
1030}
1031
Grant Likelyf00abd92009-11-24 03:27:10 -07001032/**
Grant Likely41f88002009-11-23 20:07:01 -07001033 * unflatten_device_tree - create tree of device_nodes from flat blob
1034 *
1035 * unflattens the device-tree passed by the firmware, creating the
1036 * tree of struct device_node. It also fills the "name" and "type"
1037 * pointers of the nodes so the normal device-tree walking functions
1038 * can be used.
1039 */
1040void __init unflatten_device_tree(void)
1041{
Grant Likely5063e252014-10-03 16:28:27 +01001042 __unflatten_device_tree(initial_boot_params, &of_root,
Grant Likely672c5442011-01-13 15:36:09 -07001043 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001044
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001045 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001046 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001047}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001048
Rob Herringa8bf7522013-08-26 11:22:45 -05001049/**
1050 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1051 *
1052 * Copies and unflattens the device-tree passed by the firmware, creating the
1053 * tree of struct device_node. It also fills the "name" and "type"
1054 * pointers of the nodes so the normal device-tree walking functions
1055 * can be used. This should only be used when the FDT memory has not been
1056 * reserved such is the case when the FDT is built-in to the kernel init
1057 * section. If the FDT memory is reserved already then unflatten_device_tree
1058 * should be used instead.
1059 */
1060void __init unflatten_and_copy_device_tree(void)
1061{
James Hogan6f041e92013-11-21 13:44:14 +00001062 int size;
1063 void *dt;
1064
1065 if (!initial_boot_params) {
1066 pr_warn("No valid device tree found, continuing without\n");
1067 return;
1068 }
1069
Rob Herringc972de12014-04-01 22:48:01 -05001070 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001071 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001072 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001073
1074 if (dt) {
1075 memcpy(dt, initial_boot_params, size);
1076 initial_boot_params = dt;
1077 }
1078 unflatten_device_tree();
1079}
1080
Rob Herringb0a6fb32014-04-02 16:56:48 -05001081#if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1082static struct debugfs_blob_wrapper flat_dt_blob;
1083
1084static int __init of_flat_dt_debugfs_export_fdt(void)
1085{
1086 struct dentry *d = debugfs_create_dir("device-tree", NULL);
1087
1088 if (!d)
1089 return -ENOENT;
1090
1091 flat_dt_blob.data = initial_boot_params;
1092 flat_dt_blob.size = fdt_totalsize(initial_boot_params);
1093
1094 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1095 d, &flat_dt_blob);
1096 if (!d)
1097 return -ENOENT;
1098
1099 return 0;
1100}
1101module_init(of_flat_dt_debugfs_export_fdt);
1102#endif
1103
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001104#endif /* CONFIG_OF_EARLY_FLATTREE */