blob: cde35c5d0191bd5950c7953d5b8e88a2d139306f [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
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010012#include <linux/crc32.h>
Grant Likely41f88002009-11-23 20:07:01 -070013#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070014#include <linux/initrd.h>
Grant Likelya1727da2013-08-28 21:18:32 +010015#include <linux/memblock.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070016#include <linux/of.h>
17#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010018#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +010019#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070020#include <linux/string.h>
21#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080022#include <linux/slab.h>
Rob Herringe6a69282014-04-02 15:10:14 -050023#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050024#include <linux/debugfs.h>
Rob Herringfb11ffe2014-03-27 08:07:01 -050025#include <linux/serial_core.h>
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010026#include <linux/sysfs.h>
Grant Likely51975db2010-02-01 21:34:14 -070027
Fabio Estevamc89810a2012-01-02 14:19:03 -020028#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070029#include <asm/page.h>
30
Laura Abbott704033c2014-07-15 10:03:35 -070031/*
32 * of_fdt_limit_memory - limit the number of regions in the /memory node
33 * @limit: maximum entries
34 *
35 * Adjust the flattened device tree to have at most 'limit' number of
36 * memory entries in the /memory node. This function may be called
37 * any time after initial_boot_param is set.
38 */
39void of_fdt_limit_memory(int limit)
40{
41 int memory;
42 int len;
43 const void *val;
44 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
45 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
46 const uint32_t *addr_prop;
47 const uint32_t *size_prop;
48 int root_offset;
49 int cell_size;
50
51 root_offset = fdt_path_offset(initial_boot_params, "/");
52 if (root_offset < 0)
53 return;
54
55 addr_prop = fdt_getprop(initial_boot_params, root_offset,
56 "#address-cells", NULL);
57 if (addr_prop)
58 nr_address_cells = fdt32_to_cpu(*addr_prop);
59
60 size_prop = fdt_getprop(initial_boot_params, root_offset,
61 "#size-cells", NULL);
62 if (size_prop)
63 nr_size_cells = fdt32_to_cpu(*size_prop);
64
65 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
66
67 memory = fdt_path_offset(initial_boot_params, "/memory");
68 if (memory > 0) {
69 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
70 if (len > limit*cell_size) {
71 len = limit*cell_size;
72 pr_debug("Limiting number of entries to %d\n", limit);
73 fdt_setprop(initial_boot_params, memory, "reg", val,
74 len);
75 }
76 }
77}
78
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080079/**
80 * of_fdt_is_compatible - Return true if given node from the given blob has
81 * compat in its compatible list
82 * @blob: A device tree blob
83 * @node: node to test
84 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040085 *
86 * On match, returns a non-zero value with smaller values returned for more
87 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080088 */
Rob Herringc972de12014-04-01 22:48:01 -050089int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080090 unsigned long node, const char *compat)
91{
92 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050093 int cplen;
94 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080095
Rob Herringe6a69282014-04-02 15:10:14 -050096 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080097 if (cp == NULL)
98 return 0;
99 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -0400100 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800101 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -0400102 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800103 l = strlen(cp) + 1;
104 cp += l;
105 cplen -= l;
106 }
107
108 return 0;
109}
110
Grant Likelya4f740c2010-10-30 11:49:09 -0400111/**
Kevin Cernekeecc783782015-04-09 13:05:15 -0700112 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
113 * @blob: A device tree blob
114 * @node: node to test
115 *
116 * Returns true if the node has a "big-endian" property, or if the kernel
117 * was compiled for BE *and* the node has a "native-endian" property.
118 * Returns false otherwise.
119 */
120bool of_fdt_is_big_endian(const void *blob, unsigned long node)
121{
122 if (fdt_getprop(blob, node, "big-endian", NULL))
123 return true;
124 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
125 fdt_getprop(blob, node, "native-endian", NULL))
126 return true;
127 return false;
128}
129
130/**
Grant Likelya4f740c2010-10-30 11:49:09 -0400131 * of_fdt_match - Return true if node matches a list of compatible values
132 */
Rob Herringc972de12014-04-01 22:48:01 -0500133int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100134 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400135{
136 unsigned int tmp, score = 0;
137
138 if (!compat)
139 return 0;
140
141 while (*compat) {
142 tmp = of_fdt_is_compatible(blob, node, *compat);
143 if (tmp && (score == 0 || (tmp < score)))
144 score = tmp;
145 compat++;
146 }
147
148 return score;
149}
150
Grant Likely44856812013-08-29 13:30:35 +0100151static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700152 unsigned long align)
153{
154 void *res;
155
Grant Likely44856812013-08-29 13:30:35 +0100156 *mem = PTR_ALIGN(*mem, align);
157 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700158 *mem += size;
159
160 return res;
161}
162
163/**
164 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800165 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700166 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -0700167 * @p: pointer to node in flat tree
168 * @dad: Parent struct device_node
Grant Likelybbd33932009-11-23 20:07:00 -0700169 * @fpsize: Size of the node path up at the current depth.
170 */
Rob Herringc972de12014-04-01 22:48:01 -0500171static void * unflatten_dt_node(void *blob,
Grant Likely44856812013-08-29 13:30:35 +0100172 void *mem,
Rob Herringe6a69282014-04-02 15:10:14 -0500173 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800174 struct device_node *dad,
Grant Likely5063e252014-10-03 16:28:27 +0100175 struct device_node **nodepp,
176 unsigned long fpsize,
177 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700178{
Rob Herringe6a69282014-04-02 15:10:14 -0500179 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700180 struct device_node *np;
181 struct property *pp, **prev_pp = NULL;
Rob Herringe6a69282014-04-02 15:10:14 -0500182 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700183 unsigned int l, allocl;
Rob Herringe6a69282014-04-02 15:10:14 -0500184 static int depth = 0;
185 int old_depth;
186 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700187 int has_name = 0;
188 int new_format = 0;
189
Rob Herringe6a69282014-04-02 15:10:14 -0500190 pathp = fdt_get_name(blob, *poffset, &l);
191 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700192 return mem;
Rob Herringe6a69282014-04-02 15:10:14 -0500193
Ricky Liang05f46472015-04-14 12:36:05 +0800194 allocl = ++l;
Grant Likelybbd33932009-11-23 20:07:00 -0700195
196 /* version 0x10 has a more compact unit name here instead of the full
197 * path. we accumulate the full path size using "fpsize", we'll rebuild
198 * it later. We detect this because the first character of the name is
199 * not '/'.
200 */
201 if ((*pathp) != '/') {
202 new_format = 1;
203 if (fpsize == 0) {
204 /* root node: special case. fpsize accounts for path
205 * plus terminating zero. root node only has '/', so
206 * fpsize should be 2, but we want to avoid the first
207 * level nodes to have two '/' so we use fpsize 1 here
208 */
209 fpsize = 1;
210 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000211 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500212 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700213 } else {
214 /* account for '/' and path size minus terminal 0
215 * already in 'l'
216 */
217 fpsize += l;
218 allocl = fpsize;
219 }
220 }
221
222 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
223 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100224 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000225 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200226 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000227 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700228 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700229 /* rebuild full path for new format */
230 if (dad && dad->parent) {
231 strcpy(fn, dad->full_name);
232#ifdef DEBUG
233 if ((strlen(fn) + l + 1) != allocl) {
234 pr_debug("%s: p: %d, l: %d, a: %d\n",
235 pathp, (int)strlen(fn),
236 l, allocl);
237 }
238#endif
239 fn += strlen(fn);
240 }
241 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000242 }
243 memcpy(fn, pathp, l);
244
Grant Likelybbd33932009-11-23 20:07:00 -0700245 prev_pp = &np->properties;
Grant Likelybbd33932009-11-23 20:07:00 -0700246 if (dad != NULL) {
247 np->parent = dad;
Grant Likely70161ff2014-11-28 16:03:33 +0000248 np->sibling = dad->child;
249 dad->child = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700250 }
Grant Likelybbd33932009-11-23 20:07:00 -0700251 }
Andres Salomona7006c92011-03-17 17:32:35 -0700252 /* process properties */
Rob Herringe6a69282014-04-02 15:10:14 -0500253 for (offset = fdt_first_property_offset(blob, *poffset);
254 (offset >= 0);
255 (offset = fdt_next_property_offset(blob, offset))) {
256 const char *pname;
257 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700258
Rob Herringe6a69282014-04-02 15:10:14 -0500259 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
260 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700261 break;
Rob Herringe6a69282014-04-02 15:10:14 -0500262 }
Grant Likelybbd33932009-11-23 20:07:00 -0700263
Grant Likelybbd33932009-11-23 20:07:00 -0700264 if (pname == NULL) {
265 pr_info("Can't find property name in list !\n");
266 break;
267 }
268 if (strcmp(pname, "name") == 0)
269 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700270 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
271 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100272 if (!dryrun) {
David Gibson04b954a2010-02-01 21:34:15 -0700273 /* We accept flattened tree phandles either in
274 * ePAPR-style "phandle" properties, or the
275 * legacy "linux,phandle" properties. If both
276 * appear and have different values, things
277 * will get weird. Don't do that. */
278 if ((strcmp(pname, "phandle") == 0) ||
279 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700280 if (np->phandle == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500281 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700282 }
David Gibson04b954a2010-02-01 21:34:15 -0700283 /* And we process the "ibm,phandle" property
284 * used in pSeries dynamic device tree
285 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700286 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500287 np->phandle = be32_to_cpup(p);
288 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700289 pp->length = sz;
Rob Herringe6a69282014-04-02 15:10:14 -0500290 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700291 *prev_pp = pp;
292 prev_pp = &pp->next;
293 }
Grant Likelybbd33932009-11-23 20:07:00 -0700294 }
295 /* with version 0x10 we may not have the name property, recreate
296 * it here from the unit name if absent
297 */
298 if (!has_name) {
Rob Herringe6a69282014-04-02 15:10:14 -0500299 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700300 int sz;
301
302 while (*p1) {
303 if ((*p1) == '@')
304 pa = p1;
305 if ((*p1) == '/')
306 ps = p1 + 1;
307 p1++;
308 }
309 if (pa < ps)
310 pa = p1;
311 sz = (pa - ps) + 1;
312 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
313 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100314 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700315 pp->name = "name";
316 pp->length = sz;
317 pp->value = pp + 1;
318 *prev_pp = pp;
319 prev_pp = &pp->next;
320 memcpy(pp->value, ps, sz - 1);
321 ((char *)pp->value)[sz - 1] = 0;
322 pr_debug("fixed up name for %s -> %s\n", pathp,
323 (char *)pp->value);
324 }
325 }
Grant Likely5063e252014-10-03 16:28:27 +0100326 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700327 *prev_pp = NULL;
328 np->name = of_get_property(np, "name", NULL);
329 np->type = of_get_property(np, "device_type", NULL);
330
331 if (!np->name)
332 np->name = "<NULL>";
333 if (!np->type)
334 np->type = "<NULL>";
335 }
Rob Herringe6a69282014-04-02 15:10:14 -0500336
337 old_depth = depth;
338 *poffset = fdt_next_node(blob, *poffset, &depth);
339 if (depth < 0)
340 depth = 0;
341 while (*poffset > 0 && depth > old_depth)
Grant Likely5063e252014-10-03 16:28:27 +0100342 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
343 fpsize, dryrun);
Rob Herringe6a69282014-04-02 15:10:14 -0500344
345 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
346 pr_err("unflatten: error %d processing FDT\n", *poffset);
347
Grant Likely70161ff2014-11-28 16:03:33 +0000348 /*
349 * Reverse the child list. Some drivers assumes node order matches .dts
350 * node order
351 */
352 if (!dryrun && np->child) {
353 struct device_node *child = np->child;
354 np->child = NULL;
355 while (child) {
356 struct device_node *next = child->sibling;
357 child->sibling = np->child;
358 np->child = child;
359 child = next;
360 }
361 }
362
Grant Likely5063e252014-10-03 16:28:27 +0100363 if (nodepp)
364 *nodepp = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700365
366 return mem;
367}
Grant Likely41f88002009-11-23 20:07:01 -0700368
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800369/**
370 * __unflatten_device_tree - create tree of device_nodes from flat blob
371 *
372 * unflattens a device-tree, creating the
373 * tree of struct device_node. It also fills the "name" and "type"
374 * pointers of the nodes so the normal device-tree walking functions
375 * can be used.
376 * @blob: The blob to expand
377 * @mynodes: The device_node tree created by the call
378 * @dt_alloc: An allocator that provides a virtual address to memory
379 * for the resulting tree
380 */
Rob Herringc972de12014-04-01 22:48:01 -0500381static void __unflatten_device_tree(void *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800382 struct device_node **mynodes,
383 void * (*dt_alloc)(u64 size, u64 align))
384{
Grant Likely44856812013-08-29 13:30:35 +0100385 unsigned long size;
Rob Herringe6a69282014-04-02 15:10:14 -0500386 int start;
387 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800388
389 pr_debug(" -> unflatten_device_tree()\n");
390
391 if (!blob) {
392 pr_debug("No device tree pointer\n");
393 return;
394 }
395
396 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500397 pr_debug("magic: %08x\n", fdt_magic(blob));
398 pr_debug("size: %08x\n", fdt_totalsize(blob));
399 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800400
Rob Herringc972de12014-04-01 22:48:01 -0500401 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800402 pr_err("Invalid device tree blob header\n");
403 return;
404 }
405
406 /* First pass, scan for size */
Rob Herringe6a69282014-04-02 15:10:14 -0500407 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100408 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
Grant Likely44856812013-08-29 13:30:35 +0100409 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800410
411 pr_debug(" size is %lx, allocating...\n", size);
412
413 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100414 mem = dt_alloc(size + 4, __alignof__(struct device_node));
415 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800416
Grant Likely44856812013-08-29 13:30:35 +0100417 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200418
Grant Likely44856812013-08-29 13:30:35 +0100419 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800420
421 /* Second pass, do actual unflattening */
Rob Herringe6a69282014-04-02 15:10:14 -0500422 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100423 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
Grant Likely44856812013-08-29 13:30:35 +0100424 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800425 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100426 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800427
428 pr_debug(" <- unflatten_device_tree()\n");
429}
430
431static void *kernel_tree_alloc(u64 size, u64 align)
432{
433 return kzalloc(size, GFP_KERNEL);
434}
435
436/**
437 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
438 *
439 * unflattens the device-tree passed by the firmware, creating the
440 * tree of struct device_node. It also fills the "name" and "type"
441 * pointers of the nodes so the normal device-tree walking functions
442 * can be used.
443 */
444void of_fdt_unflatten_tree(unsigned long *blob,
445 struct device_node **mynodes)
446{
Rob Herringc972de12014-04-01 22:48:01 -0500447 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800448}
449EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
450
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800451/* Everything below here references initial_boot_params directly. */
452int __initdata dt_root_addr_cells;
453int __initdata dt_root_size_cells;
454
Rob Herring1daa0c42014-03-31 15:25:04 -0500455void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800456
457#ifdef CONFIG_OF_EARLY_FLATTREE
458
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +0100459static u32 of_fdt_crc32;
460
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800461/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100462 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
463 */
464static int __init __reserved_mem_reserve_reg(unsigned long node,
465 const char *uname)
466{
467 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
468 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500469 int len;
470 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100471 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100472
473 prop = of_get_flat_dt_prop(node, "reg", &len);
474 if (!prop)
475 return -ENOENT;
476
477 if (len && len % t_len != 0) {
478 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
479 uname);
480 return -EINVAL;
481 }
482
483 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
484
485 while (len >= t_len) {
486 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
487 size = dt_mem_next_cell(dt_root_size_cells, &prop);
488
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400489 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100490 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
491 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
492 uname, &base, (unsigned long)size / SZ_1M);
493 else
494 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
495 uname, &base, (unsigned long)size / SZ_1M);
496
497 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100498 if (first) {
499 fdt_reserved_mem_save_node(node, uname, base, size);
500 first = 0;
501 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100502 }
503 return 0;
504}
505
506/**
507 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
508 * in /reserved-memory matches the values supported by the current implementation,
509 * also check if ranges property has been provided
510 */
Xiubo Li5b624112014-04-08 13:48:07 +0800511static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100512{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500513 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100514
515 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
516 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
517 return -EINVAL;
518
519 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
520 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
521 return -EINVAL;
522
523 prop = of_get_flat_dt_prop(node, "ranges", NULL);
524 if (!prop)
525 return -EINVAL;
526 return 0;
527}
528
529/**
530 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
531 */
532static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
533 int depth, void *data)
534{
535 static int found;
536 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100537 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100538
539 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
540 if (__reserved_mem_check_root(node) != 0) {
541 pr_err("Reserved memory: unsupported node format, ignoring\n");
542 /* break scan */
543 return 1;
544 }
545 found = 1;
546 /* scan next node */
547 return 0;
548 } else if (!found) {
549 /* scan next node */
550 return 0;
551 } else if (found && depth < 2) {
552 /* scanning of /reserved-memory has been finished */
553 return 1;
554 }
555
556 status = of_get_flat_dt_prop(node, "status", NULL);
557 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
558 return 0;
559
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100560 err = __reserved_mem_reserve_reg(node, uname);
561 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
562 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100563
564 /* scan next node */
565 return 0;
566}
567
568/**
569 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
570 *
571 * This function grabs memory from early allocator for device exclusive use
572 * defined in device tree structures. It should be called by arch specific code
573 * once the early allocator (i.e. memblock) has been fully activated.
574 */
575void __init early_init_fdt_scan_reserved_mem(void)
576{
Rob Herringd1552ce2014-04-01 22:46:48 -0500577 int n;
578 u64 base, size;
579
Josh Cartwright2040b522014-03-13 16:36:36 -0500580 if (!initial_boot_params)
581 return;
582
Rob Herringd1552ce2014-04-01 22:46:48 -0500583 /* Reserve the dtb region */
584 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
585 fdt_totalsize(initial_boot_params),
586 0);
587
588 /* Process header /memreserve/ fields */
589 for (n = 0; ; n++) {
590 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
591 if (!size)
592 break;
593 early_init_dt_reserve_memory_arch(base, size, 0);
594 }
595
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100596 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100597 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100598}
599
600/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800601 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
602 * @it: callback function
603 * @data: context data pointer
604 *
605 * This function is used to scan the flattened device-tree, it is
606 * used to extract the memory information at boot before we can
607 * unflatten the tree
608 */
609int __init of_scan_flat_dt(int (*it)(unsigned long node,
610 const char *uname, int depth,
611 void *data),
612 void *data)
613{
Rob Herringe6a69282014-04-02 15:10:14 -0500614 const void *blob = initial_boot_params;
615 const char *pathp;
616 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800617
Rob Herringe6a69282014-04-02 15:10:14 -0500618 for (offset = fdt_next_node(blob, -1, &depth);
619 offset >= 0 && depth >= 0 && !rc;
620 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800621
Rob Herringe6a69282014-04-02 15:10:14 -0500622 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800623 if (*pathp == '/')
624 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500625 rc = it(offset, pathp, depth, data);
626 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800627 return rc;
628}
629
630/**
631 * of_get_flat_dt_root - find the root node in the flat blob
632 */
633unsigned long __init of_get_flat_dt_root(void)
634{
Rob Herringe6a69282014-04-02 15:10:14 -0500635 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800636}
637
638/**
Rob Herringc0556d32014-04-22 12:55:10 -0500639 * of_get_flat_dt_size - Return the total size of the FDT
640 */
641int __init of_get_flat_dt_size(void)
642{
643 return fdt_totalsize(initial_boot_params);
644}
645
646/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800647 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
648 *
649 * This function can be used within scan_flattened_dt callback to get
650 * access to properties
651 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500652const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
653 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800654{
Rob Herringe6a69282014-04-02 15:10:14 -0500655 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800656}
657
658/**
659 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
660 * @node: node to test
661 * @compat: compatible string to compare with compatible list.
662 */
663int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
664{
665 return of_fdt_is_compatible(initial_boot_params, node, compat);
666}
667
Grant Likelya4f740c2010-10-30 11:49:09 -0400668/**
669 * of_flat_dt_match - Return true if node matches a list of compatible values
670 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100671int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400672{
673 return of_fdt_match(initial_boot_params, node, compat);
674}
675
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200676struct fdt_scan_status {
677 const char *name;
678 int namelen;
679 int depth;
680 int found;
681 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
682 void *data;
683};
684
Rob Herring6a903a22013-08-27 21:41:56 -0500685const char * __init of_flat_dt_get_machine_name(void)
686{
687 const char *name;
688 unsigned long dt_root = of_get_flat_dt_root();
689
690 name = of_get_flat_dt_prop(dt_root, "model", NULL);
691 if (!name)
692 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
693 return name;
694}
695
696/**
697 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
698 *
699 * @default_match: A machine specific ptr to return in case of no match.
700 * @get_next_compat: callback function to return next compatible match table.
701 *
702 * Iterate through machine match tables to find the best match for the machine
703 * compatible string in the FDT.
704 */
705const void * __init of_flat_dt_match_machine(const void *default_match,
706 const void * (*get_next_compat)(const char * const**))
707{
708 const void *data = NULL;
709 const void *best_data = default_match;
710 const char *const *compat;
711 unsigned long dt_root;
712 unsigned int best_score = ~1, score = 0;
713
714 dt_root = of_get_flat_dt_root();
715 while ((data = get_next_compat(&compat))) {
716 score = of_flat_dt_match(dt_root, compat);
717 if (score > 0 && score < best_score) {
718 best_data = data;
719 best_score = score;
720 }
721 }
722 if (!best_data) {
723 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500724 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500725
726 pr_err("\n unrecognized device tree list:\n[ ");
727
728 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
729 if (prop) {
730 while (size > 0) {
731 printk("'%s' ", prop);
732 size -= strlen(prop) + 1;
733 prop += strlen(prop) + 1;
734 }
735 }
736 printk("]\n\n");
737 return NULL;
738 }
739
740 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
741
742 return best_data;
743}
744
Grant Likelyf7b3a832009-11-24 03:26:58 -0700745#ifdef CONFIG_BLK_DEV_INITRD
746/**
747 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
748 * @node: reference to node containing initrd location ('chosen')
749 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500750static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700751{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400752 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500753 int len;
754 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700755
756 pr_debug("Looking for initrd properties... ");
757
758 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700759 if (!prop)
760 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400761 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700762
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700763 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
764 if (!prop)
765 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400766 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700767
Rob Herring29eb45a2013-08-30 17:06:53 -0500768 initrd_start = (unsigned long)__va(start);
769 initrd_end = (unsigned long)__va(end);
770 initrd_below_start_ok = 1;
771
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400772 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
773 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700774}
775#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500776static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700777{
778}
779#endif /* CONFIG_BLK_DEV_INITRD */
780
Rob Herringfb11ffe2014-03-27 08:07:01 -0500781#ifdef CONFIG_SERIAL_EARLYCON
782extern struct of_device_id __earlycon_of_table[];
783
Lad, Prabhakar523bf172015-02-04 12:04:06 +0000784static int __init early_init_dt_scan_chosen_serial(void)
Rob Herringfb11ffe2014-03-27 08:07:01 -0500785{
786 int offset;
787 const char *p;
788 int l;
789 const struct of_device_id *match = __earlycon_of_table;
790 const void *fdt = initial_boot_params;
791
792 offset = fdt_path_offset(fdt, "/chosen");
793 if (offset < 0)
794 offset = fdt_path_offset(fdt, "/chosen@0");
795 if (offset < 0)
796 return -ENOENT;
797
798 p = fdt_getprop(fdt, offset, "stdout-path", &l);
799 if (!p)
800 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
801 if (!p || !l)
802 return -ENOENT;
803
804 /* Get the node specified by stdout-path */
805 offset = fdt_path_offset(fdt, p);
806 if (offset < 0)
807 return -ENODEV;
808
Kevin Cernekeeab74d002014-11-09 00:55:47 -0800809 while (match->compatible[0]) {
Rob Herringfb11ffe2014-03-27 08:07:01 -0500810 unsigned long addr;
811 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
812 match++;
813 continue;
814 }
815
816 addr = fdt_translate_address(fdt, offset);
817 if (!addr)
818 return -ENXIO;
819
820 of_setup_earlycon(addr, match->data);
821 return 0;
822 }
823 return -ENODEV;
824}
825
826static int __init setup_of_earlycon(char *buf)
827{
828 if (buf)
829 return 0;
830
831 return early_init_dt_scan_chosen_serial();
832}
833early_param("earlycon", setup_of_earlycon);
834#endif
835
Grant Likely41f88002009-11-23 20:07:01 -0700836/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700837 * early_init_dt_scan_root - fetch the top level address and size cells
838 */
839int __init early_init_dt_scan_root(unsigned long node, const char *uname,
840 int depth, void *data)
841{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500842 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700843
844 if (depth != 0)
845 return 0;
846
Jeremy Kerr33714882010-01-30 01:45:26 -0700847 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
848 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
849
Grant Likelyf00abd92009-11-24 03:27:10 -0700850 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700851 if (prop)
852 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700853 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
854
855 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700856 if (prop)
857 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700858 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
859
860 /* break now */
861 return 1;
862}
863
Rob Herring9d0c4df2014-04-01 23:49:03 -0500864u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700865{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500866 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700867
868 *cellp = p + s;
869 return of_read_number(p, s);
870}
871
Grant Likely51975db2010-02-01 21:34:14 -0700872/**
873 * early_init_dt_scan_memory - Look for an parse memory nodes
874 */
875int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
876 int depth, void *data)
877{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500878 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
879 const __be32 *reg, *endp;
880 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700881
882 /* We are scanning "memory" nodes only */
883 if (type == NULL) {
884 /*
885 * The longtrail doesn't have a device_type on the
886 * /memory node, so look for the node called /memory@0.
887 */
Leif Lindholmb44aa252014-04-17 18:42:01 +0100888 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
Grant Likely51975db2010-02-01 21:34:14 -0700889 return 0;
890 } else if (strcmp(type, "memory") != 0)
891 return 0;
892
893 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
894 if (reg == NULL)
895 reg = of_get_flat_dt_prop(node, "reg", &l);
896 if (reg == NULL)
897 return 0;
898
899 endp = reg + (l / sizeof(__be32));
900
Florian Fainellic954b362015-04-12 13:16:26 -0700901 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
Grant Likely51975db2010-02-01 21:34:14 -0700902
903 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
904 u64 base, size;
905
906 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
907 size = dt_mem_next_cell(dt_root_size_cells, &reg);
908
909 if (size == 0)
910 continue;
911 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
912 (unsigned long long)size);
913
914 early_init_dt_add_memory_arch(base, size);
915 }
916
917 return 0;
918}
919
Grant Likely86e03222009-12-10 23:42:21 -0700920int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
921 int depth, void *data)
922{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500923 int l;
924 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700925
926 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
927
Grant Likely85f60ae2011-04-29 00:18:16 -0600928 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700929 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
930 return 0;
931
932 early_init_dt_check_for_initrd(node);
933
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300934 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700935 p = of_get_flat_dt_prop(node, "bootargs", &l);
936 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600937 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700938
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000939 /*
940 * CONFIG_CMDLINE is meant to be a default in case nothing else
941 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
942 * is set in which case we override whatever was found earlier.
943 */
Grant Likely86e03222009-12-10 23:42:21 -0700944#ifdef CONFIG_CMDLINE
945#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000946 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700947#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600948 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700949#endif /* CONFIG_CMDLINE */
950
Grant Likely85f60ae2011-04-29 00:18:16 -0600951 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700952
953 /* break now */
954 return 1;
955}
956
Grant Likelya1727da2013-08-28 21:18:32 +0100957#ifdef CONFIG_HAVE_MEMBLOCK
Laura Abbott3069f0c2014-07-07 17:45:43 -0700958#define MAX_PHYS_ADDR ((phys_addr_t)~0)
959
Rob Herring068f6312013-09-24 22:20:01 -0500960void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
961{
962 const u64 phys_offset = __pa(PAGE_OFFSET);
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200963
964 if (!PAGE_ALIGNED(base)) {
Ard Biesheuvel8cccffc2014-10-29 17:09:32 +0100965 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
966 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
967 base, base + size);
968 return;
969 }
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200970 size -= PAGE_SIZE - (base & ~PAGE_MASK);
971 base = PAGE_ALIGN(base);
972 }
Rob Herring068f6312013-09-24 22:20:01 -0500973 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700974
Laura Abbott3069f0c2014-07-07 17:45:43 -0700975 if (base > MAX_PHYS_ADDR) {
976 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
977 base, base + size);
978 return;
979 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700980
Srinivas Kandagatla9aacd602014-09-23 10:59:09 +0100981 if (base + size - 1 > MAX_PHYS_ADDR) {
982 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
983 ((u64)MAX_PHYS_ADDR) + 1, base + size);
984 size = MAX_PHYS_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700985 }
986
Rob Herring068f6312013-09-24 22:20:01 -0500987 if (base + size < phys_offset) {
988 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
989 base, base + size);
990 return;
991 }
992 if (base < phys_offset) {
993 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
994 base, phys_offset);
995 size -= phys_offset - base;
996 base = phys_offset;
997 }
998 memblock_add(base, size);
999}
1000
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001001int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1002 phys_addr_t size, bool nomap)
1003{
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001004 if (nomap)
1005 return memblock_remove(base, size);
1006 return memblock_reserve(base, size);
1007}
1008
Grant Likelya1727da2013-08-28 21:18:32 +01001009/*
1010 * called from unflatten_device_tree() to bootstrap devicetree itself
1011 * Architectures can override this definition if memblock isn't used
1012 */
1013void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1014{
1015 return __va(memblock_alloc(size, align));
1016}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001017#else
1018int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1019 phys_addr_t size, bool nomap)
1020{
Rob Herring1d1a6612014-04-22 12:50:24 -05001021 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
1022 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001023 return -ENOSYS;
1024}
Grant Likelya1727da2013-08-28 21:18:32 +01001025#endif
1026
Laura Abbott4972a742014-07-15 10:03:34 -07001027bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -05001028{
1029 if (!params)
1030 return false;
1031
Bjorn Helgaas50ba08f2014-10-29 12:15:00 -06001032 /* check device tree validity */
1033 if (fdt_check_header(params))
1034 return false;
1035
Rob Herring0288ffcb2013-08-26 09:47:40 -05001036 /* Setup flat device-tree pointer */
1037 initial_boot_params = params;
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001038 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1039 fdt_totalsize(initial_boot_params));
Laura Abbott4972a742014-07-15 10:03:34 -07001040 return true;
1041}
1042
1043
1044void __init early_init_dt_scan_nodes(void)
1045{
Rob Herring0288ffcb2013-08-26 09:47:40 -05001046 /* Retrieve various information from the /chosen node */
1047 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1048
1049 /* Initialize {size,address}-cells info */
1050 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1051
1052 /* Setup memory, calling early_init_dt_add_memory_arch */
1053 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001054}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001055
Laura Abbott4972a742014-07-15 10:03:34 -07001056bool __init early_init_dt_scan(void *params)
1057{
1058 bool status;
1059
1060 status = early_init_dt_verify(params);
1061 if (!status)
1062 return false;
1063
1064 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001065 return true;
1066}
1067
Grant Likelyf00abd92009-11-24 03:27:10 -07001068/**
Grant Likely41f88002009-11-23 20:07:01 -07001069 * unflatten_device_tree - create tree of device_nodes from flat blob
1070 *
1071 * unflattens the device-tree passed by the firmware, creating the
1072 * tree of struct device_node. It also fills the "name" and "type"
1073 * pointers of the nodes so the normal device-tree walking functions
1074 * can be used.
1075 */
1076void __init unflatten_device_tree(void)
1077{
Grant Likely5063e252014-10-03 16:28:27 +01001078 __unflatten_device_tree(initial_boot_params, &of_root,
Grant Likely672c5442011-01-13 15:36:09 -07001079 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001080
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001081 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001082 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001083}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001084
Rob Herringa8bf7522013-08-26 11:22:45 -05001085/**
1086 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1087 *
1088 * Copies and unflattens the device-tree passed by the firmware, creating the
1089 * tree of struct device_node. It also fills the "name" and "type"
1090 * pointers of the nodes so the normal device-tree walking functions
1091 * can be used. This should only be used when the FDT memory has not been
1092 * reserved such is the case when the FDT is built-in to the kernel init
1093 * section. If the FDT memory is reserved already then unflatten_device_tree
1094 * should be used instead.
1095 */
1096void __init unflatten_and_copy_device_tree(void)
1097{
James Hogan6f041e92013-11-21 13:44:14 +00001098 int size;
1099 void *dt;
1100
1101 if (!initial_boot_params) {
1102 pr_warn("No valid device tree found, continuing without\n");
1103 return;
1104 }
1105
Rob Herringc972de12014-04-01 22:48:01 -05001106 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001107 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001108 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001109
1110 if (dt) {
1111 memcpy(dt, initial_boot_params, size);
1112 initial_boot_params = dt;
1113 }
1114 unflatten_device_tree();
1115}
1116
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001117#ifdef CONFIG_SYSFS
1118static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1119 struct bin_attribute *bin_attr,
1120 char *buf, loff_t off, size_t count)
Rob Herringb0a6fb32014-04-02 16:56:48 -05001121{
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001122 memcpy(buf, initial_boot_params + off, count);
1123 return count;
Rob Herringb0a6fb32014-04-02 16:56:48 -05001124}
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001125
1126static int __init of_fdt_raw_init(void)
1127{
1128 static struct bin_attribute of_fdt_raw_attr =
1129 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1130
1131 if (!initial_boot_params)
1132 return 0;
1133
1134 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1135 fdt_totalsize(initial_boot_params))) {
1136 pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
1137 return 0;
1138 }
1139 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1140 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1141}
1142late_initcall(of_fdt_raw_init);
Rob Herringb0a6fb32014-04-02 16:56:48 -05001143#endif
1144
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001145#endif /* CONFIG_OF_EARLY_FLATTREE */