blob: a0972219ccfc5fccee139c46d9f4fd97445973e2 [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
Frank Rowandbd0096d2016-10-17 12:21:23 -070012#define pr_fmt(fmt) "OF: fdt: " fmt
Rob Herring606ad422016-06-15 08:32:18 -050013
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010014#include <linux/crc32.h>
Grant Likely41f88002009-11-23 20:07:01 -070015#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070016#include <linux/initrd.h>
Grant Likelya1727da2013-08-28 21:18:32 +010017#include <linux/memblock.h>
Guenter Roeckf8062382015-12-05 16:13:53 -080018#include <linux/mutex.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070019#include <linux/of.h>
20#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010021#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +010022#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070023#include <linux/string.h>
24#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080025#include <linux/slab.h>
Rob Herringe6a69282014-04-02 15:10:14 -050026#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050027#include <linux/debugfs.h>
Rob Herringfb11ffe2014-03-27 08:07:01 -050028#include <linux/serial_core.h>
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010029#include <linux/sysfs.h>
Grant Likely51975db2010-02-01 21:34:14 -070030
Fabio Estevamc89810a2012-01-02 14:19:03 -020031#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070032#include <asm/page.h>
33
Frank Rowand81d0848f2017-04-25 17:09:54 -070034#include "of_private.h"
35
Laura Abbott704033c2014-07-15 10:03:35 -070036/*
37 * of_fdt_limit_memory - limit the number of regions in the /memory node
38 * @limit: maximum entries
39 *
40 * Adjust the flattened device tree to have at most 'limit' number of
41 * memory entries in the /memory node. This function may be called
42 * any time after initial_boot_param is set.
43 */
44void of_fdt_limit_memory(int limit)
45{
46 int memory;
47 int len;
48 const void *val;
49 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
50 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
Rob Herring17a70352017-05-04 12:56:12 -050051 const __be32 *addr_prop;
52 const __be32 *size_prop;
Laura Abbott704033c2014-07-15 10:03:35 -070053 int root_offset;
54 int cell_size;
55
56 root_offset = fdt_path_offset(initial_boot_params, "/");
57 if (root_offset < 0)
58 return;
59
60 addr_prop = fdt_getprop(initial_boot_params, root_offset,
61 "#address-cells", NULL);
62 if (addr_prop)
63 nr_address_cells = fdt32_to_cpu(*addr_prop);
64
65 size_prop = fdt_getprop(initial_boot_params, root_offset,
66 "#size-cells", NULL);
67 if (size_prop)
68 nr_size_cells = fdt32_to_cpu(*size_prop);
69
70 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
71
72 memory = fdt_path_offset(initial_boot_params, "/memory");
73 if (memory > 0) {
74 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
75 if (len > limit*cell_size) {
76 len = limit*cell_size;
77 pr_debug("Limiting number of entries to %d\n", limit);
78 fdt_setprop(initial_boot_params, memory, "reg", val,
79 len);
80 }
81 }
82}
83
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080084/**
85 * of_fdt_is_compatible - Return true if given node from the given blob has
86 * compat in its compatible list
87 * @blob: A device tree blob
88 * @node: node to test
89 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040090 *
91 * On match, returns a non-zero value with smaller values returned for more
92 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080093 */
Rob Herringc972de12014-04-01 22:48:01 -050094int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080095 unsigned long node, const char *compat)
96{
97 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050098 int cplen;
99 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800100
Rob Herringe6a69282014-04-02 15:10:14 -0500101 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800102 if (cp == NULL)
103 return 0;
104 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -0400105 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800106 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -0400107 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800108 l = strlen(cp) + 1;
109 cp += l;
110 cplen -= l;
111 }
112
113 return 0;
114}
115
Grant Likelya4f740c2010-10-30 11:49:09 -0400116/**
Kevin Cernekeecc783782015-04-09 13:05:15 -0700117 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
118 * @blob: A device tree blob
119 * @node: node to test
120 *
121 * Returns true if the node has a "big-endian" property, or if the kernel
122 * was compiled for BE *and* the node has a "native-endian" property.
123 * Returns false otherwise.
124 */
125bool of_fdt_is_big_endian(const void *blob, unsigned long node)
126{
127 if (fdt_getprop(blob, node, "big-endian", NULL))
128 return true;
129 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
130 fdt_getprop(blob, node, "native-endian", NULL))
131 return true;
132 return false;
133}
134
135/**
Grant Likelya4f740c2010-10-30 11:49:09 -0400136 * of_fdt_match - Return true if node matches a list of compatible values
137 */
Rob Herringc972de12014-04-01 22:48:01 -0500138int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100139 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400140{
141 unsigned int tmp, score = 0;
142
143 if (!compat)
144 return 0;
145
146 while (*compat) {
147 tmp = of_fdt_is_compatible(blob, node, *compat);
148 if (tmp && (score == 0 || (tmp < score)))
149 score = tmp;
150 compat++;
151 }
152
153 return score;
154}
155
Grant Likely44856812013-08-29 13:30:35 +0100156static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700157 unsigned long align)
158{
159 void *res;
160
Grant Likely44856812013-08-29 13:30:35 +0100161 *mem = PTR_ALIGN(*mem, align);
162 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700163 *mem += size;
164
165 return res;
166}
167
Gavin Shandfbd4c62016-05-03 23:22:47 +1000168static void populate_properties(const void *blob,
169 int offset,
170 void **mem,
171 struct device_node *np,
172 const char *nodename,
Grant Likely5063e252014-10-03 16:28:27 +0100173 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700174{
Gavin Shandfbd4c62016-05-03 23:22:47 +1000175 struct property *pp, **pprev = NULL;
176 int cur;
177 bool has_name = false;
178
179 pprev = &np->properties;
180 for (cur = fdt_first_property_offset(blob, offset);
181 cur >= 0;
182 cur = fdt_next_property_offset(blob, cur)) {
183 const __be32 *val;
184 const char *pname;
185 u32 sz;
186
187 val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
188 if (!val) {
Rob Herring606ad422016-06-15 08:32:18 -0500189 pr_warn("Cannot locate property at 0x%x\n", cur);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000190 continue;
191 }
192
193 if (!pname) {
Rob Herring606ad422016-06-15 08:32:18 -0500194 pr_warn("Cannot find property name at 0x%x\n", cur);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000195 continue;
196 }
197
198 if (!strcmp(pname, "name"))
199 has_name = true;
200
201 pp = unflatten_dt_alloc(mem, sizeof(struct property),
202 __alignof__(struct property));
203 if (dryrun)
204 continue;
205
206 /* We accept flattened tree phandles either in
207 * ePAPR-style "phandle" properties, or the
208 * legacy "linux,phandle" properties. If both
209 * appear and have different values, things
210 * will get weird. Don't do that.
211 */
212 if (!strcmp(pname, "phandle") ||
213 !strcmp(pname, "linux,phandle")) {
214 if (!np->phandle)
215 np->phandle = be32_to_cpup(val);
216 }
217
218 /* And we process the "ibm,phandle" property
219 * used in pSeries dynamic device tree
220 * stuff
221 */
222 if (!strcmp(pname, "ibm,phandle"))
223 np->phandle = be32_to_cpup(val);
224
225 pp->name = (char *)pname;
226 pp->length = sz;
227 pp->value = (__be32 *)val;
228 *pprev = pp;
229 pprev = &pp->next;
230 }
231
232 /* With version 0x10 we may not have the name property,
233 * recreate it here from the unit name if absent
234 */
235 if (!has_name) {
236 const char *p = nodename, *ps = p, *pa = NULL;
237 int len;
238
239 while (*p) {
240 if ((*p) == '@')
241 pa = p;
242 else if ((*p) == '/')
243 ps = p + 1;
244 p++;
245 }
246
247 if (pa < ps)
248 pa = p;
249 len = (pa - ps) + 1;
250 pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
251 __alignof__(struct property));
252 if (!dryrun) {
253 pp->name = "name";
254 pp->length = len;
255 pp->value = pp + 1;
256 *pprev = pp;
257 pprev = &pp->next;
258 memcpy(pp->value, ps, len - 1);
259 ((char *)pp->value)[len - 1] = 0;
260 pr_debug("fixed up name for %s -> %s\n",
261 nodename, (char *)pp->value);
262 }
263 }
264
265 if (!dryrun)
266 *pprev = NULL;
267}
268
Gavin Shandddc33e2016-05-13 21:31:39 +1000269static unsigned int populate_node(const void *blob,
270 int offset,
271 void **mem,
272 struct device_node *dad,
273 unsigned int fpsize,
274 struct device_node **pnp,
275 bool dryrun)
Gavin Shandfbd4c62016-05-03 23:22:47 +1000276{
Grant Likelybbd33932009-11-23 20:07:00 -0700277 struct device_node *np;
Rob Herringe6a69282014-04-02 15:10:14 -0500278 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700279 unsigned int l, allocl;
Grant Likelybbd33932009-11-23 20:07:00 -0700280 int new_format = 0;
281
Gavin Shandfbd4c62016-05-03 23:22:47 +1000282 pathp = fdt_get_name(blob, offset, &l);
283 if (!pathp) {
284 *pnp = NULL;
285 return 0;
286 }
Rob Herringe6a69282014-04-02 15:10:14 -0500287
Ricky Liang05f46472015-04-14 12:36:05 +0800288 allocl = ++l;
Grant Likelybbd33932009-11-23 20:07:00 -0700289
290 /* version 0x10 has a more compact unit name here instead of the full
291 * path. we accumulate the full path size using "fpsize", we'll rebuild
292 * it later. We detect this because the first character of the name is
293 * not '/'.
294 */
295 if ((*pathp) != '/') {
296 new_format = 1;
297 if (fpsize == 0) {
298 /* root node: special case. fpsize accounts for path
299 * plus terminating zero. root node only has '/', so
300 * fpsize should be 2, but we want to avoid the first
301 * level nodes to have two '/' so we use fpsize 1 here
302 */
303 fpsize = 1;
304 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000305 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500306 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700307 } else {
308 /* account for '/' and path size minus terminal 0
309 * already in 'l'
310 */
311 fpsize += l;
312 allocl = fpsize;
313 }
314 }
315
Gavin Shandfbd4c62016-05-03 23:22:47 +1000316 np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
Grant Likelybbd33932009-11-23 20:07:00 -0700317 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100318 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000319 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200320 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000321 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700322 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700323 /* rebuild full path for new format */
324 if (dad && dad->parent) {
325 strcpy(fn, dad->full_name);
326#ifdef DEBUG
327 if ((strlen(fn) + l + 1) != allocl) {
328 pr_debug("%s: p: %d, l: %d, a: %d\n",
329 pathp, (int)strlen(fn),
330 l, allocl);
331 }
332#endif
333 fn += strlen(fn);
334 }
335 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000336 }
337 memcpy(fn, pathp, l);
338
Grant Likelybbd33932009-11-23 20:07:00 -0700339 if (dad != NULL) {
340 np->parent = dad;
Grant Likely70161ff2014-11-28 16:03:33 +0000341 np->sibling = dad->child;
342 dad->child = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700343 }
Grant Likelybbd33932009-11-23 20:07:00 -0700344 }
Grant Likelybbd33932009-11-23 20:07:00 -0700345
Gavin Shandfbd4c62016-05-03 23:22:47 +1000346 populate_properties(blob, offset, mem, np, pathp, dryrun);
Grant Likely5063e252014-10-03 16:28:27 +0100347 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700348 np->name = of_get_property(np, "name", NULL);
349 np->type = of_get_property(np, "device_type", NULL);
350
351 if (!np->name)
352 np->name = "<NULL>";
353 if (!np->type)
354 np->type = "<NULL>";
355 }
Rob Herringe6a69282014-04-02 15:10:14 -0500356
Gavin Shandfbd4c62016-05-03 23:22:47 +1000357 *pnp = np;
358 return fpsize;
359}
360
Gavin Shan50800082016-05-03 23:22:48 +1000361static void reverse_nodes(struct device_node *parent)
362{
363 struct device_node *child, *next;
364
365 /* In-depth first */
366 child = parent->child;
367 while (child) {
368 reverse_nodes(child);
369
370 child = child->sibling;
371 }
372
373 /* Reverse the nodes in the child list */
374 child = parent->child;
375 parent->child = NULL;
376 while (child) {
377 next = child->sibling;
378
379 child->sibling = parent->child;
380 parent->child = child;
381 child = next;
382 }
383}
384
Gavin Shandfbd4c62016-05-03 23:22:47 +1000385/**
Gavin Shan947c82c2016-05-03 23:22:49 +1000386 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
Gavin Shandfbd4c62016-05-03 23:22:47 +1000387 * @blob: The parent device tree blob
388 * @mem: Memory chunk to use for allocating device nodes and properties
Gavin Shandfbd4c62016-05-03 23:22:47 +1000389 * @dad: Parent struct device_node
390 * @nodepp: The device_node tree created by the call
Gavin Shan50800082016-05-03 23:22:48 +1000391 *
392 * It returns the size of unflattened device tree or error code
Gavin Shandfbd4c62016-05-03 23:22:47 +1000393 */
Gavin Shan947c82c2016-05-03 23:22:49 +1000394static int unflatten_dt_nodes(const void *blob,
395 void *mem,
396 struct device_node *dad,
397 struct device_node **nodepp)
Gavin Shandfbd4c62016-05-03 23:22:47 +1000398{
Gavin Shan50800082016-05-03 23:22:48 +1000399 struct device_node *root;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000400 int offset = 0, depth = 0, initial_depth = 0;
Gavin Shan50800082016-05-03 23:22:48 +1000401#define FDT_MAX_DEPTH 64
Gavin Shandddc33e2016-05-13 21:31:39 +1000402 unsigned int fpsizes[FDT_MAX_DEPTH];
Gavin Shan50800082016-05-03 23:22:48 +1000403 struct device_node *nps[FDT_MAX_DEPTH];
404 void *base = mem;
405 bool dryrun = !base;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000406
Gavin Shan50800082016-05-03 23:22:48 +1000407 if (nodepp)
408 *nodepp = NULL;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000409
Gavin Shan8c237cd2016-06-09 15:50:49 +1000410 /*
411 * We're unflattening device sub-tree if @dad is valid. There are
412 * possibly multiple nodes in the first level of depth. We need
413 * set @depth to 1 to make fdt_next_node() happy as it bails
414 * immediately when negative @depth is found. Otherwise, the device
415 * nodes except the first one won't be unflattened successfully.
416 */
417 if (dad)
418 depth = initial_depth = 1;
419
Gavin Shan50800082016-05-03 23:22:48 +1000420 root = dad;
421 fpsizes[depth] = dad ? strlen(of_node_full_name(dad)) : 0;
Rhyland Klein78c44d92016-05-11 13:36:57 -0400422 nps[depth] = dad;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000423
Gavin Shan50800082016-05-03 23:22:48 +1000424 for (offset = 0;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000425 offset >= 0 && depth >= initial_depth;
Gavin Shan50800082016-05-03 23:22:48 +1000426 offset = fdt_next_node(blob, offset, &depth)) {
427 if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
428 continue;
Rob Herringe6a69282014-04-02 15:10:14 -0500429
Rhyland Klein78c44d92016-05-11 13:36:57 -0400430 fpsizes[depth+1] = populate_node(blob, offset, &mem,
431 nps[depth],
432 fpsizes[depth],
433 &nps[depth+1], dryrun);
434 if (!fpsizes[depth+1])
Gavin Shan50800082016-05-03 23:22:48 +1000435 return mem - base;
436
437 if (!dryrun && nodepp && !*nodepp)
Rhyland Klein78c44d92016-05-11 13:36:57 -0400438 *nodepp = nps[depth+1];
Gavin Shan50800082016-05-03 23:22:48 +1000439 if (!dryrun && !root)
Rhyland Klein78c44d92016-05-11 13:36:57 -0400440 root = nps[depth+1];
Gavin Shan50800082016-05-03 23:22:48 +1000441 }
442
443 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
Rob Herring606ad422016-06-15 08:32:18 -0500444 pr_err("Error %d processing FDT\n", offset);
Gavin Shan50800082016-05-03 23:22:48 +1000445 return -EINVAL;
446 }
Rob Herringe6a69282014-04-02 15:10:14 -0500447
Grant Likely70161ff2014-11-28 16:03:33 +0000448 /*
449 * Reverse the child list. Some drivers assumes node order matches .dts
450 * node order
451 */
Gavin Shan50800082016-05-03 23:22:48 +1000452 if (!dryrun)
453 reverse_nodes(root);
Grant Likely70161ff2014-11-28 16:03:33 +0000454
Gavin Shan50800082016-05-03 23:22:48 +1000455 return mem - base;
Grant Likelybbd33932009-11-23 20:07:00 -0700456}
Grant Likely41f88002009-11-23 20:07:01 -0700457
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800458/**
459 * __unflatten_device_tree - create tree of device_nodes from flat blob
460 *
461 * unflattens a device-tree, creating the
462 * tree of struct device_node. It also fills the "name" and "type"
463 * pointers of the nodes so the normal device-tree walking functions
464 * can be used.
465 * @blob: The blob to expand
Gavin Shanc4263232016-05-03 23:22:50 +1000466 * @dad: Parent device node
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800467 * @mynodes: The device_node tree created by the call
468 * @dt_alloc: An allocator that provides a virtual address to memory
469 * for the resulting tree
Gavin Shan83262412016-05-03 23:22:51 +1000470 *
471 * Returns NULL on failure or the memory chunk containing the unflattened
472 * device tree on success.
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800473 */
Frank Rowand81d0848f2017-04-25 17:09:54 -0700474void *__unflatten_device_tree(const void *blob,
475 struct device_node *dad,
476 struct device_node **mynodes,
477 void *(*dt_alloc)(u64 size, u64 align),
478 bool detached)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800479{
Gavin Shan50800082016-05-03 23:22:48 +1000480 int size;
Rob Herringe6a69282014-04-02 15:10:14 -0500481 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800482
483 pr_debug(" -> unflatten_device_tree()\n");
484
485 if (!blob) {
486 pr_debug("No device tree pointer\n");
Gavin Shan83262412016-05-03 23:22:51 +1000487 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800488 }
489
490 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500491 pr_debug("magic: %08x\n", fdt_magic(blob));
492 pr_debug("size: %08x\n", fdt_totalsize(blob));
493 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800494
Rob Herringc972de12014-04-01 22:48:01 -0500495 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800496 pr_err("Invalid device tree blob header\n");
Gavin Shan83262412016-05-03 23:22:51 +1000497 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800498 }
499
500 /* First pass, scan for size */
Gavin Shanc4263232016-05-03 23:22:50 +1000501 size = unflatten_dt_nodes(blob, NULL, dad, NULL);
Gavin Shan50800082016-05-03 23:22:48 +1000502 if (size < 0)
Gavin Shan83262412016-05-03 23:22:51 +1000503 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800504
Gavin Shan50800082016-05-03 23:22:48 +1000505 size = ALIGN(size, 4);
506 pr_debug(" size is %d, allocating...\n", size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800507
508 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100509 mem = dt_alloc(size + 4, __alignof__(struct device_node));
510 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800511
Grant Likely44856812013-08-29 13:30:35 +0100512 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200513
Grant Likely44856812013-08-29 13:30:35 +0100514 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800515
516 /* Second pass, do actual unflattening */
Gavin Shanc4263232016-05-03 23:22:50 +1000517 unflatten_dt_nodes(blob, mem, dad, mynodes);
Grant Likely44856812013-08-29 13:30:35 +0100518 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800519 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100520 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800521
Gavin Shan89c67752016-08-01 17:17:53 +1000522 if (detached && mynodes) {
Michal Suchanek1d1bde52016-07-19 00:01:12 +0200523 of_node_set_flag(*mynodes, OF_DETACHED);
524 pr_debug("unflattened tree is detached\n");
525 }
526
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800527 pr_debug(" <- unflatten_device_tree()\n");
Gavin Shan83262412016-05-03 23:22:51 +1000528 return mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800529}
530
531static void *kernel_tree_alloc(u64 size, u64 align)
532{
533 return kzalloc(size, GFP_KERNEL);
534}
535
Guenter Roeckf8062382015-12-05 16:13:53 -0800536static DEFINE_MUTEX(of_fdt_unflatten_mutex);
537
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800538/**
539 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
Gavin Shanc4263232016-05-03 23:22:50 +1000540 * @blob: Flat device tree blob
541 * @dad: Parent device node
542 * @mynodes: The device tree created by the call
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800543 *
544 * unflattens the device-tree passed by the firmware, creating the
545 * tree of struct device_node. It also fills the "name" and "type"
546 * pointers of the nodes so the normal device-tree walking functions
547 * can be used.
Gavin Shan83262412016-05-03 23:22:51 +1000548 *
549 * Returns NULL on failure or the memory chunk containing the unflattened
550 * device tree on success.
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800551 */
Gavin Shan83262412016-05-03 23:22:51 +1000552void *of_fdt_unflatten_tree(const unsigned long *blob,
553 struct device_node *dad,
554 struct device_node **mynodes)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800555{
Gavin Shan83262412016-05-03 23:22:51 +1000556 void *mem;
557
Guenter Roeckf8062382015-12-05 16:13:53 -0800558 mutex_lock(&of_fdt_unflatten_mutex);
Michal Suchanek1d1bde52016-07-19 00:01:12 +0200559 mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
560 true);
Guenter Roeckf8062382015-12-05 16:13:53 -0800561 mutex_unlock(&of_fdt_unflatten_mutex);
Gavin Shan83262412016-05-03 23:22:51 +1000562
563 return mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800564}
565EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
566
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800567/* Everything below here references initial_boot_params directly. */
568int __initdata dt_root_addr_cells;
569int __initdata dt_root_size_cells;
570
Rob Herring1daa0c42014-03-31 15:25:04 -0500571void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800572
573#ifdef CONFIG_OF_EARLY_FLATTREE
574
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +0100575static u32 of_fdt_crc32;
576
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800577/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100578 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
579 */
580static int __init __reserved_mem_reserve_reg(unsigned long node,
581 const char *uname)
582{
583 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
584 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500585 int len;
586 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100587 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100588
589 prop = of_get_flat_dt_prop(node, "reg", &len);
590 if (!prop)
591 return -ENOENT;
592
593 if (len && len % t_len != 0) {
594 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
595 uname);
596 return -EINVAL;
597 }
598
599 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
600
601 while (len >= t_len) {
602 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
603 size = dt_mem_next_cell(dt_root_size_cells, &prop);
604
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400605 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100606 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
607 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
608 uname, &base, (unsigned long)size / SZ_1M);
609 else
610 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
611 uname, &base, (unsigned long)size / SZ_1M);
612
613 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100614 if (first) {
615 fdt_reserved_mem_save_node(node, uname, base, size);
616 first = 0;
617 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100618 }
619 return 0;
620}
621
622/**
623 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
624 * in /reserved-memory matches the values supported by the current implementation,
625 * also check if ranges property has been provided
626 */
Xiubo Li5b624112014-04-08 13:48:07 +0800627static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100628{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500629 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100630
631 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
632 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
633 return -EINVAL;
634
635 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
636 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
637 return -EINVAL;
638
639 prop = of_get_flat_dt_prop(node, "ranges", NULL);
640 if (!prop)
641 return -EINVAL;
642 return 0;
643}
644
645/**
646 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
647 */
648static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
649 int depth, void *data)
650{
651 static int found;
652 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100653 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100654
655 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
656 if (__reserved_mem_check_root(node) != 0) {
657 pr_err("Reserved memory: unsupported node format, ignoring\n");
658 /* break scan */
659 return 1;
660 }
661 found = 1;
662 /* scan next node */
663 return 0;
664 } else if (!found) {
665 /* scan next node */
666 return 0;
667 } else if (found && depth < 2) {
668 /* scanning of /reserved-memory has been finished */
669 return 1;
670 }
671
672 status = of_get_flat_dt_prop(node, "status", NULL);
673 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
674 return 0;
675
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100676 err = __reserved_mem_reserve_reg(node, uname);
677 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
678 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100679
680 /* scan next node */
681 return 0;
682}
683
684/**
685 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
686 *
687 * This function grabs memory from early allocator for device exclusive use
688 * defined in device tree structures. It should be called by arch specific code
689 * once the early allocator (i.e. memblock) has been fully activated.
690 */
691void __init early_init_fdt_scan_reserved_mem(void)
692{
Rob Herringd1552ce2014-04-01 22:46:48 -0500693 int n;
694 u64 base, size;
695
Josh Cartwright2040b522014-03-13 16:36:36 -0500696 if (!initial_boot_params)
697 return;
698
Rob Herringd1552ce2014-04-01 22:46:48 -0500699 /* Process header /memreserve/ fields */
700 for (n = 0; ; n++) {
701 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
702 if (!size)
703 break;
704 early_init_dt_reserve_memory_arch(base, size, 0);
705 }
706
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100707 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100708 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100709}
710
711/**
Ard Biesheuvel24bbd922015-06-01 13:40:31 +0200712 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
713 */
714void __init early_init_fdt_reserve_self(void)
715{
716 if (!initial_boot_params)
717 return;
718
719 /* Reserve the dtb region */
720 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
721 fdt_totalsize(initial_boot_params),
722 0);
723}
724
725/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800726 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
727 * @it: callback function
728 * @data: context data pointer
729 *
730 * This function is used to scan the flattened device-tree, it is
731 * used to extract the memory information at boot before we can
732 * unflatten the tree
733 */
734int __init of_scan_flat_dt(int (*it)(unsigned long node,
735 const char *uname, int depth,
736 void *data),
737 void *data)
738{
Rob Herringe6a69282014-04-02 15:10:14 -0500739 const void *blob = initial_boot_params;
740 const char *pathp;
741 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800742
Tobias Wolf3ec75442016-11-23 10:40:07 +0100743 if (!blob)
744 return 0;
745
746 for (offset = fdt_next_node(blob, -1, &depth);
747 offset >= 0 && depth >= 0 && !rc;
748 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800749
Rob Herringe6a69282014-04-02 15:10:14 -0500750 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800751 if (*pathp == '/')
752 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500753 rc = it(offset, pathp, depth, data);
754 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800755 return rc;
756}
757
758/**
Shannon Zhao9c609862016-04-07 20:03:33 +0800759 * of_get_flat_dt_subnode_by_name - get the subnode by given name
760 *
761 * @node: the parent node
762 * @uname: the name of subnode
763 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
764 */
765
766int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
767{
768 return fdt_subnode_offset(initial_boot_params, node, uname);
769}
770
771/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800772 * of_get_flat_dt_root - find the root node in the flat blob
773 */
774unsigned long __init of_get_flat_dt_root(void)
775{
Rob Herringe6a69282014-04-02 15:10:14 -0500776 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800777}
778
779/**
Rob Herringc0556d32014-04-22 12:55:10 -0500780 * of_get_flat_dt_size - Return the total size of the FDT
781 */
782int __init of_get_flat_dt_size(void)
783{
784 return fdt_totalsize(initial_boot_params);
785}
786
787/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800788 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
789 *
790 * This function can be used within scan_flattened_dt callback to get
791 * access to properties
792 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500793const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
794 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800795{
Rob Herringe6a69282014-04-02 15:10:14 -0500796 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800797}
798
799/**
800 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
801 * @node: node to test
802 * @compat: compatible string to compare with compatible list.
803 */
804int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
805{
806 return of_fdt_is_compatible(initial_boot_params, node, compat);
807}
808
Grant Likelya4f740c2010-10-30 11:49:09 -0400809/**
810 * of_flat_dt_match - Return true if node matches a list of compatible values
811 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100812int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400813{
814 return of_fdt_match(initial_boot_params, node, compat);
815}
816
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200817struct fdt_scan_status {
818 const char *name;
819 int namelen;
820 int depth;
821 int found;
822 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
823 void *data;
824};
825
Rob Herring6a903a22013-08-27 21:41:56 -0500826const char * __init of_flat_dt_get_machine_name(void)
827{
828 const char *name;
829 unsigned long dt_root = of_get_flat_dt_root();
830
831 name = of_get_flat_dt_prop(dt_root, "model", NULL);
832 if (!name)
833 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
834 return name;
835}
836
837/**
838 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
839 *
840 * @default_match: A machine specific ptr to return in case of no match.
841 * @get_next_compat: callback function to return next compatible match table.
842 *
843 * Iterate through machine match tables to find the best match for the machine
844 * compatible string in the FDT.
845 */
846const void * __init of_flat_dt_match_machine(const void *default_match,
847 const void * (*get_next_compat)(const char * const**))
848{
849 const void *data = NULL;
850 const void *best_data = default_match;
851 const char *const *compat;
852 unsigned long dt_root;
853 unsigned int best_score = ~1, score = 0;
854
855 dt_root = of_get_flat_dt_root();
856 while ((data = get_next_compat(&compat))) {
857 score = of_flat_dt_match(dt_root, compat);
858 if (score > 0 && score < best_score) {
859 best_data = data;
860 best_score = score;
861 }
862 }
863 if (!best_data) {
864 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500865 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500866
867 pr_err("\n unrecognized device tree list:\n[ ");
868
869 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
870 if (prop) {
871 while (size > 0) {
872 printk("'%s' ", prop);
873 size -= strlen(prop) + 1;
874 prop += strlen(prop) + 1;
875 }
876 }
877 printk("]\n\n");
878 return NULL;
879 }
880
881 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
882
883 return best_data;
884}
885
Grant Likelyf7b3a832009-11-24 03:26:58 -0700886#ifdef CONFIG_BLK_DEV_INITRD
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100887#ifndef __early_init_dt_declare_initrd
888static void __early_init_dt_declare_initrd(unsigned long start,
889 unsigned long end)
890{
891 initrd_start = (unsigned long)__va(start);
892 initrd_end = (unsigned long)__va(end);
893 initrd_below_start_ok = 1;
894}
895#endif
896
Grant Likelyf7b3a832009-11-24 03:26:58 -0700897/**
898 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
899 * @node: reference to node containing initrd location ('chosen')
900 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500901static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700902{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400903 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500904 int len;
905 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700906
907 pr_debug("Looking for initrd properties... ");
908
909 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700910 if (!prop)
911 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400912 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700913
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700914 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
915 if (!prop)
916 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400917 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700918
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100919 __early_init_dt_declare_initrd(start, end);
Rob Herring29eb45a2013-08-30 17:06:53 -0500920
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400921 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
922 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700923}
924#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500925static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700926{
927}
928#endif /* CONFIG_BLK_DEV_INITRD */
929
Rob Herringfb11ffe2014-03-27 08:07:01 -0500930#ifdef CONFIG_SERIAL_EARLYCON
Rob Herringfb11ffe2014-03-27 08:07:01 -0500931
Leif Lindholmd5031872016-09-27 23:54:12 +0300932int __init early_init_dt_scan_chosen_stdout(void)
Rob Herringfb11ffe2014-03-27 08:07:01 -0500933{
934 int offset;
Peter Hurley4d118c92016-01-16 15:23:42 -0800935 const char *p, *q, *options = NULL;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500936 int l;
Peter Hurley2eaa7902016-01-16 15:23:39 -0800937 const struct earlycon_id *match;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500938 const void *fdt = initial_boot_params;
939
940 offset = fdt_path_offset(fdt, "/chosen");
941 if (offset < 0)
942 offset = fdt_path_offset(fdt, "/chosen@0");
943 if (offset < 0)
944 return -ENOENT;
945
946 p = fdt_getprop(fdt, offset, "stdout-path", &l);
947 if (!p)
948 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
949 if (!p || !l)
950 return -ENOENT;
951
Peter Hurley4d118c92016-01-16 15:23:42 -0800952 q = strchrnul(p, ':');
953 if (*q != '\0')
954 options = q + 1;
Peter Hurley0fcc2862016-01-16 15:23:48 -0800955 l = q - p;
Stefan Agner6296ad92015-10-10 01:29:30 -0700956
Rob Herringfb11ffe2014-03-27 08:07:01 -0500957 /* Get the node specified by stdout-path */
Peter Hurley0fcc2862016-01-16 15:23:48 -0800958 offset = fdt_path_offset_namelen(fdt, p, l);
959 if (offset < 0) {
960 pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
961 return 0;
962 }
Rob Herringfb11ffe2014-03-27 08:07:01 -0500963
Peter Hurley2eaa7902016-01-16 15:23:39 -0800964 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley2eaa7902016-01-16 15:23:39 -0800965 if (!match->compatible[0])
Rob Herringfb11ffe2014-03-27 08:07:01 -0500966 continue;
Peter Hurley2eaa7902016-01-16 15:23:39 -0800967
968 if (fdt_node_check_compatible(fdt, offset, match->compatible))
969 continue;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500970
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800971 of_setup_earlycon(match, offset, options);
Rob Herringfb11ffe2014-03-27 08:07:01 -0500972 return 0;
973 }
974 return -ENODEV;
975}
Rob Herringfb11ffe2014-03-27 08:07:01 -0500976#endif
977
Grant Likely41f88002009-11-23 20:07:01 -0700978/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700979 * early_init_dt_scan_root - fetch the top level address and size cells
980 */
981int __init early_init_dt_scan_root(unsigned long node, const char *uname,
982 int depth, void *data)
983{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500984 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700985
986 if (depth != 0)
987 return 0;
988
Jeremy Kerr33714882010-01-30 01:45:26 -0700989 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
990 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
991
Grant Likelyf00abd92009-11-24 03:27:10 -0700992 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700993 if (prop)
994 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700995 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
996
997 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700998 if (prop)
999 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -07001000 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1001
1002 /* break now */
1003 return 1;
1004}
1005
Rob Herring9d0c4df2014-04-01 23:49:03 -05001006u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -07001007{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001008 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -07001009
1010 *cellp = p + s;
1011 return of_read_number(p, s);
1012}
1013
Grant Likely51975db2010-02-01 21:34:14 -07001014/**
1015 * early_init_dt_scan_memory - Look for an parse memory nodes
1016 */
1017int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
1018 int depth, void *data)
1019{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001020 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1021 const __be32 *reg, *endp;
1022 int l;
Reza Arbab41a9ada2016-12-12 16:43:02 -08001023 bool hotpluggable;
Grant Likely51975db2010-02-01 21:34:14 -07001024
1025 /* We are scanning "memory" nodes only */
1026 if (type == NULL) {
1027 /*
1028 * The longtrail doesn't have a device_type on the
1029 * /memory node, so look for the node called /memory@0.
1030 */
Leif Lindholmb44aa252014-04-17 18:42:01 +01001031 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
Grant Likely51975db2010-02-01 21:34:14 -07001032 return 0;
1033 } else if (strcmp(type, "memory") != 0)
1034 return 0;
1035
1036 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1037 if (reg == NULL)
1038 reg = of_get_flat_dt_prop(node, "reg", &l);
1039 if (reg == NULL)
1040 return 0;
1041
1042 endp = reg + (l / sizeof(__be32));
Reza Arbab41a9ada2016-12-12 16:43:02 -08001043 hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
Grant Likely51975db2010-02-01 21:34:14 -07001044
Florian Fainellic954b362015-04-12 13:16:26 -07001045 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
Grant Likely51975db2010-02-01 21:34:14 -07001046
1047 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1048 u64 base, size;
1049
1050 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1051 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1052
1053 if (size == 0)
1054 continue;
1055 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
1056 (unsigned long long)size);
1057
1058 early_init_dt_add_memory_arch(base, size);
Reza Arbab41a9ada2016-12-12 16:43:02 -08001059
1060 if (!hotpluggable)
1061 continue;
1062
1063 if (early_init_dt_mark_hotplug_memory_arch(base, size))
1064 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1065 base, base + size);
Grant Likely51975db2010-02-01 21:34:14 -07001066 }
1067
1068 return 0;
1069}
1070
Grant Likely86e03222009-12-10 23:42:21 -07001071int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
1072 int depth, void *data)
1073{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001074 int l;
1075 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -07001076
1077 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1078
Grant Likely85f60ae2011-04-29 00:18:16 -06001079 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -07001080 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1081 return 0;
1082
1083 early_init_dt_check_for_initrd(node);
1084
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001085 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -07001086 p = of_get_flat_dt_prop(node, "bootargs", &l);
1087 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -06001088 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -07001089
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +00001090 /*
1091 * CONFIG_CMDLINE is meant to be a default in case nothing else
1092 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1093 * is set in which case we override whatever was found earlier.
1094 */
Grant Likely86e03222009-12-10 23:42:21 -07001095#ifdef CONFIG_CMDLINE
Max Uvarov34b82022016-04-13 12:52:16 +03001096#if defined(CONFIG_CMDLINE_EXTEND)
1097 strlcat(data, " ", COMMAND_LINE_SIZE);
1098 strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1099#elif defined(CONFIG_CMDLINE_FORCE)
1100 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1101#else
1102 /* No arguments from boot loader, use kernel's cmdl*/
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +00001103 if (!((char *)data)[0])
Grant Likely85f60ae2011-04-29 00:18:16 -06001104 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Max Uvarov34b82022016-04-13 12:52:16 +03001105#endif
Grant Likely86e03222009-12-10 23:42:21 -07001106#endif /* CONFIG_CMDLINE */
1107
Grant Likely85f60ae2011-04-29 00:18:16 -06001108 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -07001109
1110 /* break now */
1111 return 1;
1112}
1113
Grant Likelya1727da2013-08-28 21:18:32 +01001114#ifdef CONFIG_HAVE_MEMBLOCK
Ard Biesheuvel270522a2016-02-16 13:52:32 +01001115#ifndef MIN_MEMBLOCK_ADDR
1116#define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1117#endif
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001118#ifndef MAX_MEMBLOCK_ADDR
1119#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1120#endif
Laura Abbott3069f0c2014-07-07 17:45:43 -07001121
Rob Herring068f6312013-09-24 22:20:01 -05001122void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1123{
Ard Biesheuvel270522a2016-02-16 13:52:32 +01001124 const u64 phys_offset = MIN_MEMBLOCK_ADDR;
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +02001125
1126 if (!PAGE_ALIGNED(base)) {
Ard Biesheuvel8cccffc2014-10-29 17:09:32 +01001127 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
1128 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1129 base, base + size);
1130 return;
1131 }
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +02001132 size -= PAGE_SIZE - (base & ~PAGE_MASK);
1133 base = PAGE_ALIGN(base);
1134 }
Rob Herring068f6312013-09-24 22:20:01 -05001135 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001136
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001137 if (base > MAX_MEMBLOCK_ADDR) {
Laura Abbott3069f0c2014-07-07 17:45:43 -07001138 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1139 base, base + size);
1140 return;
1141 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001142
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001143 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
Srinivas Kandagatla9aacd602014-09-23 10:59:09 +01001144 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001145 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1146 size = MAX_MEMBLOCK_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001147 }
1148
Rob Herring068f6312013-09-24 22:20:01 -05001149 if (base + size < phys_offset) {
1150 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1151 base, base + size);
1152 return;
1153 }
1154 if (base < phys_offset) {
1155 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1156 base, phys_offset);
1157 size -= phys_offset - base;
1158 base = phys_offset;
1159 }
1160 memblock_add(base, size);
1161}
1162
Reza Arbab41a9ada2016-12-12 16:43:02 -08001163int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1164{
1165 return memblock_mark_hotplug(base, size);
1166}
1167
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001168int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1169 phys_addr_t size, bool nomap)
1170{
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001171 if (nomap)
1172 return memblock_remove(base, size);
1173 return memblock_reserve(base, size);
1174}
1175
Grant Likelya1727da2013-08-28 21:18:32 +01001176/*
1177 * called from unflatten_device_tree() to bootstrap devicetree itself
1178 * Architectures can override this definition if memblock isn't used
1179 */
1180void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1181{
1182 return __va(memblock_alloc(size, align));
1183}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001184#else
Rob Herringaefc7ec2015-06-18 20:35:46 -05001185void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1186{
1187 WARN_ON(1);
1188}
1189
Reza Arbab41a9ada2016-12-12 16:43:02 -08001190int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1191{
1192 return -ENOSYS;
1193}
1194
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001195int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1196 phys_addr_t size, bool nomap)
1197{
Dmitry V. Krivenok78bb2ab2015-11-30 23:45:48 +03001198 pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
Rob Herring1d1a6612014-04-22 12:50:24 -05001199 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001200 return -ENOSYS;
1201}
Rob Herringaefc7ec2015-06-18 20:35:46 -05001202
1203void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1204{
1205 WARN_ON(1);
1206 return NULL;
1207}
Grant Likelya1727da2013-08-28 21:18:32 +01001208#endif
1209
Laura Abbott4972a742014-07-15 10:03:34 -07001210bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -05001211{
1212 if (!params)
1213 return false;
1214
Bjorn Helgaas50ba08f2014-10-29 12:15:00 -06001215 /* check device tree validity */
1216 if (fdt_check_header(params))
1217 return false;
1218
Rob Herring0288ffcb2013-08-26 09:47:40 -05001219 /* Setup flat device-tree pointer */
1220 initial_boot_params = params;
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001221 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1222 fdt_totalsize(initial_boot_params));
Laura Abbott4972a742014-07-15 10:03:34 -07001223 return true;
1224}
1225
1226
1227void __init early_init_dt_scan_nodes(void)
1228{
Rob Herring0288ffcb2013-08-26 09:47:40 -05001229 /* Retrieve various information from the /chosen node */
1230 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1231
1232 /* Initialize {size,address}-cells info */
1233 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1234
1235 /* Setup memory, calling early_init_dt_add_memory_arch */
1236 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001237}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001238
Laura Abbott4972a742014-07-15 10:03:34 -07001239bool __init early_init_dt_scan(void *params)
1240{
1241 bool status;
1242
1243 status = early_init_dt_verify(params);
1244 if (!status)
1245 return false;
1246
1247 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001248 return true;
1249}
1250
Grant Likelyf00abd92009-11-24 03:27:10 -07001251/**
Grant Likely41f88002009-11-23 20:07:01 -07001252 * unflatten_device_tree - create tree of device_nodes from flat blob
1253 *
1254 * unflattens the device-tree passed by the firmware, creating the
1255 * tree of struct device_node. It also fills the "name" and "type"
1256 * pointers of the nodes so the normal device-tree walking functions
1257 * can be used.
1258 */
1259void __init unflatten_device_tree(void)
1260{
Gavin Shanc4263232016-05-03 23:22:50 +10001261 __unflatten_device_tree(initial_boot_params, NULL, &of_root,
Michal Suchanek1d1bde52016-07-19 00:01:12 +02001262 early_init_dt_alloc_memory_arch, false);
Grant Likely41f88002009-11-23 20:07:01 -07001263
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001264 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001265 of_alias_scan(early_init_dt_alloc_memory_arch);
Frank Rowand81d0848f2017-04-25 17:09:54 -07001266
1267 unittest_unflatten_overlay_base();
Grant Likely41f88002009-11-23 20:07:01 -07001268}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001269
Rob Herringa8bf7522013-08-26 11:22:45 -05001270/**
1271 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1272 *
1273 * Copies and unflattens the device-tree passed by the firmware, creating the
1274 * tree of struct device_node. It also fills the "name" and "type"
1275 * pointers of the nodes so the normal device-tree walking functions
1276 * can be used. This should only be used when the FDT memory has not been
1277 * reserved such is the case when the FDT is built-in to the kernel init
1278 * section. If the FDT memory is reserved already then unflatten_device_tree
1279 * should be used instead.
1280 */
1281void __init unflatten_and_copy_device_tree(void)
1282{
James Hogan6f041e92013-11-21 13:44:14 +00001283 int size;
1284 void *dt;
1285
1286 if (!initial_boot_params) {
1287 pr_warn("No valid device tree found, continuing without\n");
1288 return;
1289 }
1290
Rob Herringc972de12014-04-01 22:48:01 -05001291 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001292 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001293 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001294
1295 if (dt) {
1296 memcpy(dt, initial_boot_params, size);
1297 initial_boot_params = dt;
1298 }
1299 unflatten_device_tree();
1300}
1301
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001302#ifdef CONFIG_SYSFS
1303static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1304 struct bin_attribute *bin_attr,
1305 char *buf, loff_t off, size_t count)
Rob Herringb0a6fb32014-04-02 16:56:48 -05001306{
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001307 memcpy(buf, initial_boot_params + off, count);
1308 return count;
Rob Herringb0a6fb32014-04-02 16:56:48 -05001309}
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001310
1311static int __init of_fdt_raw_init(void)
1312{
1313 static struct bin_attribute of_fdt_raw_attr =
1314 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1315
1316 if (!initial_boot_params)
1317 return 0;
1318
1319 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1320 fdt_totalsize(initial_boot_params))) {
Rob Herring606ad422016-06-15 08:32:18 -05001321 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001322 return 0;
1323 }
1324 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1325 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1326}
1327late_initcall(of_fdt_raw_init);
Rob Herringb0a6fb32014-04-02 16:56:48 -05001328#endif
1329
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001330#endif /* CONFIG_OF_EARLY_FLATTREE */