blob: 2d515b85a198fd58fc0429f880803e563d30af51 [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 */
Frank Rowand92af0892017-06-20 16:38:28 -070094static int 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
Rob Herringecc8a962017-09-28 19:20:32 -0500135static bool of_fdt_device_is_available(const void *blob, unsigned long node)
136{
137 const char *status = fdt_getprop(blob, node, "status", NULL);
138
139 if (!status)
140 return true;
141
142 if (!strcmp(status, "ok") || !strcmp(status, "okay"))
143 return true;
144
145 return false;
146}
147
Kevin Cernekeecc783782015-04-09 13:05:15 -0700148/**
Grant Likelya4f740c2010-10-30 11:49:09 -0400149 * of_fdt_match - Return true if node matches a list of compatible values
150 */
Rob Herringc972de12014-04-01 22:48:01 -0500151int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100152 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400153{
154 unsigned int tmp, score = 0;
155
156 if (!compat)
157 return 0;
158
159 while (*compat) {
160 tmp = of_fdt_is_compatible(blob, node, *compat);
161 if (tmp && (score == 0 || (tmp < score)))
162 score = tmp;
163 compat++;
164 }
165
166 return score;
167}
168
Grant Likely44856812013-08-29 13:30:35 +0100169static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700170 unsigned long align)
171{
172 void *res;
173
Grant Likely44856812013-08-29 13:30:35 +0100174 *mem = PTR_ALIGN(*mem, align);
175 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700176 *mem += size;
177
178 return res;
179}
180
Gavin Shandfbd4c62016-05-03 23:22:47 +1000181static void populate_properties(const void *blob,
182 int offset,
183 void **mem,
184 struct device_node *np,
185 const char *nodename,
Grant Likely5063e252014-10-03 16:28:27 +0100186 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700187{
Gavin Shandfbd4c62016-05-03 23:22:47 +1000188 struct property *pp, **pprev = NULL;
189 int cur;
190 bool has_name = false;
191
192 pprev = &np->properties;
193 for (cur = fdt_first_property_offset(blob, offset);
194 cur >= 0;
195 cur = fdt_next_property_offset(blob, cur)) {
196 const __be32 *val;
197 const char *pname;
198 u32 sz;
199
200 val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
201 if (!val) {
Rob Herring606ad422016-06-15 08:32:18 -0500202 pr_warn("Cannot locate property at 0x%x\n", cur);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000203 continue;
204 }
205
206 if (!pname) {
Rob Herring606ad422016-06-15 08:32:18 -0500207 pr_warn("Cannot find property name at 0x%x\n", cur);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000208 continue;
209 }
210
211 if (!strcmp(pname, "name"))
212 has_name = true;
213
214 pp = unflatten_dt_alloc(mem, sizeof(struct property),
215 __alignof__(struct property));
216 if (dryrun)
217 continue;
218
219 /* We accept flattened tree phandles either in
220 * ePAPR-style "phandle" properties, or the
221 * legacy "linux,phandle" properties. If both
222 * appear and have different values, things
223 * will get weird. Don't do that.
224 */
225 if (!strcmp(pname, "phandle") ||
226 !strcmp(pname, "linux,phandle")) {
227 if (!np->phandle)
228 np->phandle = be32_to_cpup(val);
229 }
230
231 /* And we process the "ibm,phandle" property
232 * used in pSeries dynamic device tree
233 * stuff
234 */
235 if (!strcmp(pname, "ibm,phandle"))
236 np->phandle = be32_to_cpup(val);
237
238 pp->name = (char *)pname;
239 pp->length = sz;
240 pp->value = (__be32 *)val;
241 *pprev = pp;
242 pprev = &pp->next;
243 }
244
245 /* With version 0x10 we may not have the name property,
246 * recreate it here from the unit name if absent
247 */
248 if (!has_name) {
249 const char *p = nodename, *ps = p, *pa = NULL;
250 int len;
251
252 while (*p) {
253 if ((*p) == '@')
254 pa = p;
255 else if ((*p) == '/')
256 ps = p + 1;
257 p++;
258 }
259
260 if (pa < ps)
261 pa = p;
262 len = (pa - ps) + 1;
263 pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
264 __alignof__(struct property));
265 if (!dryrun) {
266 pp->name = "name";
267 pp->length = len;
268 pp->value = pp + 1;
269 *pprev = pp;
270 pprev = &pp->next;
271 memcpy(pp->value, ps, len - 1);
272 ((char *)pp->value)[len - 1] = 0;
273 pr_debug("fixed up name for %s -> %s\n",
274 nodename, (char *)pp->value);
275 }
276 }
277
278 if (!dryrun)
279 *pprev = NULL;
280}
281
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500282static bool populate_node(const void *blob,
283 int offset,
284 void **mem,
285 struct device_node *dad,
286 struct device_node **pnp,
287 bool dryrun)
Gavin Shandfbd4c62016-05-03 23:22:47 +1000288{
Grant Likelybbd33932009-11-23 20:07:00 -0700289 struct device_node *np;
Rob Herringe6a69282014-04-02 15:10:14 -0500290 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700291 unsigned int l, allocl;
Grant Likelybbd33932009-11-23 20:07:00 -0700292
Gavin Shandfbd4c62016-05-03 23:22:47 +1000293 pathp = fdt_get_name(blob, offset, &l);
294 if (!pathp) {
295 *pnp = NULL;
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500296 return false;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000297 }
Rob Herringe6a69282014-04-02 15:10:14 -0500298
Ricky Liang05f46472015-04-14 12:36:05 +0800299 allocl = ++l;
Grant Likelybbd33932009-11-23 20:07:00 -0700300
Gavin Shandfbd4c62016-05-03 23:22:47 +1000301 np = unflatten_dt_alloc(mem, sizeof(struct device_node) + allocl,
Grant Likelybbd33932009-11-23 20:07:00 -0700302 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100303 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000304 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200305 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000306 np->full_name = fn = ((char *)np) + sizeof(*np);
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500307
Grant Likelyc22618a2012-11-14 22:37:12 +0000308 memcpy(fn, pathp, l);
309
Grant Likelybbd33932009-11-23 20:07:00 -0700310 if (dad != NULL) {
311 np->parent = dad;
Grant Likely70161ff2014-11-28 16:03:33 +0000312 np->sibling = dad->child;
313 dad->child = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700314 }
Grant Likelybbd33932009-11-23 20:07:00 -0700315 }
Grant Likelybbd33932009-11-23 20:07:00 -0700316
Gavin Shandfbd4c62016-05-03 23:22:47 +1000317 populate_properties(blob, offset, mem, np, pathp, dryrun);
Grant Likely5063e252014-10-03 16:28:27 +0100318 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700319 np->name = of_get_property(np, "name", NULL);
320 np->type = of_get_property(np, "device_type", NULL);
321
322 if (!np->name)
323 np->name = "<NULL>";
324 if (!np->type)
325 np->type = "<NULL>";
326 }
Rob Herringe6a69282014-04-02 15:10:14 -0500327
Gavin Shandfbd4c62016-05-03 23:22:47 +1000328 *pnp = np;
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500329 return true;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000330}
331
Gavin Shan50800082016-05-03 23:22:48 +1000332static void reverse_nodes(struct device_node *parent)
333{
334 struct device_node *child, *next;
335
336 /* In-depth first */
337 child = parent->child;
338 while (child) {
339 reverse_nodes(child);
340
341 child = child->sibling;
342 }
343
344 /* Reverse the nodes in the child list */
345 child = parent->child;
346 parent->child = NULL;
347 while (child) {
348 next = child->sibling;
349
350 child->sibling = parent->child;
351 parent->child = child;
352 child = next;
353 }
354}
355
Gavin Shandfbd4c62016-05-03 23:22:47 +1000356/**
Gavin Shan947c82c2016-05-03 23:22:49 +1000357 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
Gavin Shandfbd4c62016-05-03 23:22:47 +1000358 * @blob: The parent device tree blob
359 * @mem: Memory chunk to use for allocating device nodes and properties
Gavin Shandfbd4c62016-05-03 23:22:47 +1000360 * @dad: Parent struct device_node
361 * @nodepp: The device_node tree created by the call
Gavin Shan50800082016-05-03 23:22:48 +1000362 *
363 * It returns the size of unflattened device tree or error code
Gavin Shandfbd4c62016-05-03 23:22:47 +1000364 */
Gavin Shan947c82c2016-05-03 23:22:49 +1000365static int unflatten_dt_nodes(const void *blob,
366 void *mem,
367 struct device_node *dad,
368 struct device_node **nodepp)
Gavin Shandfbd4c62016-05-03 23:22:47 +1000369{
Gavin Shan50800082016-05-03 23:22:48 +1000370 struct device_node *root;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000371 int offset = 0, depth = 0, initial_depth = 0;
Gavin Shan50800082016-05-03 23:22:48 +1000372#define FDT_MAX_DEPTH 64
Gavin Shan50800082016-05-03 23:22:48 +1000373 struct device_node *nps[FDT_MAX_DEPTH];
374 void *base = mem;
375 bool dryrun = !base;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000376
Gavin Shan50800082016-05-03 23:22:48 +1000377 if (nodepp)
378 *nodepp = NULL;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000379
Gavin Shan8c237cd2016-06-09 15:50:49 +1000380 /*
381 * We're unflattening device sub-tree if @dad is valid. There are
382 * possibly multiple nodes in the first level of depth. We need
383 * set @depth to 1 to make fdt_next_node() happy as it bails
384 * immediately when negative @depth is found. Otherwise, the device
385 * nodes except the first one won't be unflattened successfully.
386 */
387 if (dad)
388 depth = initial_depth = 1;
389
Gavin Shan50800082016-05-03 23:22:48 +1000390 root = dad;
Rhyland Klein78c44d92016-05-11 13:36:57 -0400391 nps[depth] = dad;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000392
Gavin Shan50800082016-05-03 23:22:48 +1000393 for (offset = 0;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000394 offset >= 0 && depth >= initial_depth;
Gavin Shan50800082016-05-03 23:22:48 +1000395 offset = fdt_next_node(blob, offset, &depth)) {
396 if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
397 continue;
Rob Herringe6a69282014-04-02 15:10:14 -0500398
Rob Herring77ea8a62017-10-03 11:07:55 -0500399 if (!IS_ENABLED(CONFIG_OF_KOBJ) &&
400 !of_fdt_device_is_available(blob, offset))
401 continue;
402
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500403 if (!populate_node(blob, offset, &mem, nps[depth],
404 &nps[depth+1], dryrun))
Gavin Shan50800082016-05-03 23:22:48 +1000405 return mem - base;
406
407 if (!dryrun && nodepp && !*nodepp)
Rhyland Klein78c44d92016-05-11 13:36:57 -0400408 *nodepp = nps[depth+1];
Gavin Shan50800082016-05-03 23:22:48 +1000409 if (!dryrun && !root)
Rhyland Klein78c44d92016-05-11 13:36:57 -0400410 root = nps[depth+1];
Gavin Shan50800082016-05-03 23:22:48 +1000411 }
412
413 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
Rob Herring606ad422016-06-15 08:32:18 -0500414 pr_err("Error %d processing FDT\n", offset);
Gavin Shan50800082016-05-03 23:22:48 +1000415 return -EINVAL;
416 }
Rob Herringe6a69282014-04-02 15:10:14 -0500417
Grant Likely70161ff2014-11-28 16:03:33 +0000418 /*
419 * Reverse the child list. Some drivers assumes node order matches .dts
420 * node order
421 */
Gavin Shan50800082016-05-03 23:22:48 +1000422 if (!dryrun)
423 reverse_nodes(root);
Grant Likely70161ff2014-11-28 16:03:33 +0000424
Gavin Shan50800082016-05-03 23:22:48 +1000425 return mem - base;
Grant Likelybbd33932009-11-23 20:07:00 -0700426}
Grant Likely41f88002009-11-23 20:07:01 -0700427
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800428/**
429 * __unflatten_device_tree - create tree of device_nodes from flat blob
430 *
431 * unflattens a device-tree, creating the
432 * tree of struct device_node. It also fills the "name" and "type"
433 * pointers of the nodes so the normal device-tree walking functions
434 * can be used.
435 * @blob: The blob to expand
Gavin Shanc4263232016-05-03 23:22:50 +1000436 * @dad: Parent device node
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800437 * @mynodes: The device_node tree created by the call
438 * @dt_alloc: An allocator that provides a virtual address to memory
439 * for the resulting tree
Gavin Shan83262412016-05-03 23:22:51 +1000440 *
441 * Returns NULL on failure or the memory chunk containing the unflattened
442 * device tree on success.
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800443 */
Frank Rowand81d0848f2017-04-25 17:09:54 -0700444void *__unflatten_device_tree(const void *blob,
445 struct device_node *dad,
446 struct device_node **mynodes,
447 void *(*dt_alloc)(u64 size, u64 align),
448 bool detached)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800449{
Gavin Shan50800082016-05-03 23:22:48 +1000450 int size;
Rob Herringe6a69282014-04-02 15:10:14 -0500451 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800452
453 pr_debug(" -> unflatten_device_tree()\n");
454
455 if (!blob) {
456 pr_debug("No device tree pointer\n");
Gavin Shan83262412016-05-03 23:22:51 +1000457 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800458 }
459
460 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500461 pr_debug("magic: %08x\n", fdt_magic(blob));
462 pr_debug("size: %08x\n", fdt_totalsize(blob));
463 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800464
Rob Herringc972de12014-04-01 22:48:01 -0500465 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800466 pr_err("Invalid device tree blob header\n");
Gavin Shan83262412016-05-03 23:22:51 +1000467 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800468 }
469
470 /* First pass, scan for size */
Gavin Shanc4263232016-05-03 23:22:50 +1000471 size = unflatten_dt_nodes(blob, NULL, dad, NULL);
Gavin Shan50800082016-05-03 23:22:48 +1000472 if (size < 0)
Gavin Shan83262412016-05-03 23:22:51 +1000473 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800474
Gavin Shan50800082016-05-03 23:22:48 +1000475 size = ALIGN(size, 4);
476 pr_debug(" size is %d, allocating...\n", size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800477
478 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100479 mem = dt_alloc(size + 4, __alignof__(struct device_node));
Johan Hovold49e67dd2017-05-17 17:29:09 +0200480 if (!mem)
481 return NULL;
482
Grant Likely44856812013-08-29 13:30:35 +0100483 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800484
Grant Likely44856812013-08-29 13:30:35 +0100485 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200486
Grant Likely44856812013-08-29 13:30:35 +0100487 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800488
489 /* Second pass, do actual unflattening */
Gavin Shanc4263232016-05-03 23:22:50 +1000490 unflatten_dt_nodes(blob, mem, dad, mynodes);
Grant Likely44856812013-08-29 13:30:35 +0100491 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800492 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100493 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800494
Gavin Shan89c67752016-08-01 17:17:53 +1000495 if (detached && mynodes) {
Michal Suchanek1d1bde52016-07-19 00:01:12 +0200496 of_node_set_flag(*mynodes, OF_DETACHED);
497 pr_debug("unflattened tree is detached\n");
498 }
499
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800500 pr_debug(" <- unflatten_device_tree()\n");
Gavin Shan83262412016-05-03 23:22:51 +1000501 return mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800502}
503
504static void *kernel_tree_alloc(u64 size, u64 align)
505{
506 return kzalloc(size, GFP_KERNEL);
507}
508
Guenter Roeckf8062382015-12-05 16:13:53 -0800509static DEFINE_MUTEX(of_fdt_unflatten_mutex);
510
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800511/**
512 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
Gavin Shanc4263232016-05-03 23:22:50 +1000513 * @blob: Flat device tree blob
514 * @dad: Parent device node
515 * @mynodes: The device tree created by the call
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800516 *
517 * unflattens the device-tree passed by the firmware, creating the
518 * tree of struct device_node. It also fills the "name" and "type"
519 * pointers of the nodes so the normal device-tree walking functions
520 * can be used.
Gavin Shan83262412016-05-03 23:22:51 +1000521 *
522 * Returns NULL on failure or the memory chunk containing the unflattened
523 * device tree on success.
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800524 */
Gavin Shan83262412016-05-03 23:22:51 +1000525void *of_fdt_unflatten_tree(const unsigned long *blob,
526 struct device_node *dad,
527 struct device_node **mynodes)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800528{
Gavin Shan83262412016-05-03 23:22:51 +1000529 void *mem;
530
Guenter Roeckf8062382015-12-05 16:13:53 -0800531 mutex_lock(&of_fdt_unflatten_mutex);
Michal Suchanek1d1bde52016-07-19 00:01:12 +0200532 mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
533 true);
Guenter Roeckf8062382015-12-05 16:13:53 -0800534 mutex_unlock(&of_fdt_unflatten_mutex);
Gavin Shan83262412016-05-03 23:22:51 +1000535
536 return mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800537}
538EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
539
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800540/* Everything below here references initial_boot_params directly. */
541int __initdata dt_root_addr_cells;
542int __initdata dt_root_size_cells;
543
Rob Herring1daa0c42014-03-31 15:25:04 -0500544void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800545
546#ifdef CONFIG_OF_EARLY_FLATTREE
547
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +0100548static u32 of_fdt_crc32;
549
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800550/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100551 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
552 */
553static int __init __reserved_mem_reserve_reg(unsigned long node,
554 const char *uname)
555{
556 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
557 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500558 int len;
559 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100560 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100561
562 prop = of_get_flat_dt_prop(node, "reg", &len);
563 if (!prop)
564 return -ENOENT;
565
566 if (len && len % t_len != 0) {
567 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
568 uname);
569 return -EINVAL;
570 }
571
572 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
573
574 while (len >= t_len) {
575 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
576 size = dt_mem_next_cell(dt_root_size_cells, &prop);
577
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400578 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100579 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
580 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
581 uname, &base, (unsigned long)size / SZ_1M);
582 else
583 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
584 uname, &base, (unsigned long)size / SZ_1M);
585
586 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100587 if (first) {
588 fdt_reserved_mem_save_node(node, uname, base, size);
589 first = 0;
590 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100591 }
592 return 0;
593}
594
595/**
596 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
597 * in /reserved-memory matches the values supported by the current implementation,
598 * also check if ranges property has been provided
599 */
Xiubo Li5b624112014-04-08 13:48:07 +0800600static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100601{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500602 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100603
604 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
605 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
606 return -EINVAL;
607
608 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
609 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
610 return -EINVAL;
611
612 prop = of_get_flat_dt_prop(node, "ranges", NULL);
613 if (!prop)
614 return -EINVAL;
615 return 0;
616}
617
618/**
619 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
620 */
621static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
622 int depth, void *data)
623{
624 static int found;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100625 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100626
627 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
628 if (__reserved_mem_check_root(node) != 0) {
629 pr_err("Reserved memory: unsupported node format, ignoring\n");
630 /* break scan */
631 return 1;
632 }
633 found = 1;
634 /* scan next node */
635 return 0;
636 } else if (!found) {
637 /* scan next node */
638 return 0;
639 } else if (found && depth < 2) {
640 /* scanning of /reserved-memory has been finished */
641 return 1;
642 }
643
Rob Herringecc8a962017-09-28 19:20:32 -0500644 if (!of_fdt_device_is_available(initial_boot_params, node))
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100645 return 0;
646
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100647 err = __reserved_mem_reserve_reg(node, uname);
648 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
649 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100650
651 /* scan next node */
652 return 0;
653}
654
655/**
656 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
657 *
658 * This function grabs memory from early allocator for device exclusive use
659 * defined in device tree structures. It should be called by arch specific code
660 * once the early allocator (i.e. memblock) has been fully activated.
661 */
662void __init early_init_fdt_scan_reserved_mem(void)
663{
Rob Herringd1552ce2014-04-01 22:46:48 -0500664 int n;
665 u64 base, size;
666
Josh Cartwright2040b522014-03-13 16:36:36 -0500667 if (!initial_boot_params)
668 return;
669
Rob Herringd1552ce2014-04-01 22:46:48 -0500670 /* Process header /memreserve/ fields */
671 for (n = 0; ; n++) {
672 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
673 if (!size)
674 break;
675 early_init_dt_reserve_memory_arch(base, size, 0);
676 }
677
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100678 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100679 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100680}
681
682/**
Ard Biesheuvel24bbd922015-06-01 13:40:31 +0200683 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
684 */
685void __init early_init_fdt_reserve_self(void)
686{
687 if (!initial_boot_params)
688 return;
689
690 /* Reserve the dtb region */
691 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
692 fdt_totalsize(initial_boot_params),
693 0);
694}
695
696/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800697 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
698 * @it: callback function
699 * @data: context data pointer
700 *
701 * This function is used to scan the flattened device-tree, it is
702 * used to extract the memory information at boot before we can
703 * unflatten the tree
704 */
705int __init of_scan_flat_dt(int (*it)(unsigned long node,
706 const char *uname, int depth,
707 void *data),
708 void *data)
709{
Rob Herringe6a69282014-04-02 15:10:14 -0500710 const void *blob = initial_boot_params;
711 const char *pathp;
712 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800713
Tobias Wolf3ec75442016-11-23 10:40:07 +0100714 if (!blob)
715 return 0;
716
717 for (offset = fdt_next_node(blob, -1, &depth);
718 offset >= 0 && depth >= 0 && !rc;
719 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800720
Rob Herringe6a69282014-04-02 15:10:14 -0500721 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800722 if (*pathp == '/')
723 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500724 rc = it(offset, pathp, depth, data);
725 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800726 return rc;
727}
728
729/**
Nicholas Pigginea47dd12017-04-19 05:12:18 +1000730 * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
731 * @it: callback function
732 * @data: context data pointer
733 *
734 * This function is used to scan sub-nodes of a node.
735 */
736int __init of_scan_flat_dt_subnodes(unsigned long parent,
737 int (*it)(unsigned long node,
738 const char *uname,
739 void *data),
740 void *data)
741{
742 const void *blob = initial_boot_params;
743 int node;
744
745 fdt_for_each_subnode(node, blob, parent) {
746 const char *pathp;
747 int rc;
748
749 pathp = fdt_get_name(blob, node, NULL);
750 if (*pathp == '/')
751 pathp = kbasename(pathp);
752 rc = it(node, pathp, data);
753 if (rc)
754 return rc;
755 }
756 return 0;
757}
758
759/**
Shannon Zhao9c609862016-04-07 20:03:33 +0800760 * of_get_flat_dt_subnode_by_name - get the subnode by given name
761 *
762 * @node: the parent node
763 * @uname: the name of subnode
764 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
765 */
766
767int of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
768{
769 return fdt_subnode_offset(initial_boot_params, node, uname);
770}
771
772/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800773 * of_get_flat_dt_root - find the root node in the flat blob
774 */
775unsigned long __init of_get_flat_dt_root(void)
776{
Rob Herringe6a69282014-04-02 15:10:14 -0500777 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800778}
779
780/**
Rob Herringc0556d32014-04-22 12:55:10 -0500781 * of_get_flat_dt_size - Return the total size of the FDT
782 */
783int __init of_get_flat_dt_size(void)
784{
785 return fdt_totalsize(initial_boot_params);
786}
787
788/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800789 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
790 *
791 * This function can be used within scan_flattened_dt callback to get
792 * access to properties
793 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500794const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
795 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800796{
Rob Herringe6a69282014-04-02 15:10:14 -0500797 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800798}
799
800/**
801 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
802 * @node: node to test
803 * @compat: compatible string to compare with compatible list.
804 */
805int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
806{
807 return of_fdt_is_compatible(initial_boot_params, node, compat);
808}
809
Grant Likelya4f740c2010-10-30 11:49:09 -0400810/**
811 * of_flat_dt_match - Return true if node matches a list of compatible values
812 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100813int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400814{
815 return of_fdt_match(initial_boot_params, node, compat);
816}
817
Nicholas Pigginea47dd12017-04-19 05:12:18 +1000818/**
819 * of_get_flat_dt_prop - Given a node in the flat blob, return the phandle
820 */
821uint32_t __init of_get_flat_dt_phandle(unsigned long node)
822{
823 return fdt_get_phandle(initial_boot_params, node);
824}
825
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200826struct fdt_scan_status {
827 const char *name;
828 int namelen;
829 int depth;
830 int found;
831 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
832 void *data;
833};
834
Rob Herring6a903a22013-08-27 21:41:56 -0500835const char * __init of_flat_dt_get_machine_name(void)
836{
837 const char *name;
838 unsigned long dt_root = of_get_flat_dt_root();
839
840 name = of_get_flat_dt_prop(dt_root, "model", NULL);
841 if (!name)
842 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
843 return name;
844}
845
846/**
847 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
848 *
849 * @default_match: A machine specific ptr to return in case of no match.
850 * @get_next_compat: callback function to return next compatible match table.
851 *
852 * Iterate through machine match tables to find the best match for the machine
853 * compatible string in the FDT.
854 */
855const void * __init of_flat_dt_match_machine(const void *default_match,
856 const void * (*get_next_compat)(const char * const**))
857{
858 const void *data = NULL;
859 const void *best_data = default_match;
860 const char *const *compat;
861 unsigned long dt_root;
862 unsigned int best_score = ~1, score = 0;
863
864 dt_root = of_get_flat_dt_root();
865 while ((data = get_next_compat(&compat))) {
866 score = of_flat_dt_match(dt_root, compat);
867 if (score > 0 && score < best_score) {
868 best_data = data;
869 best_score = score;
870 }
871 }
872 if (!best_data) {
873 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500874 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500875
876 pr_err("\n unrecognized device tree list:\n[ ");
877
878 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
879 if (prop) {
880 while (size > 0) {
881 printk("'%s' ", prop);
882 size -= strlen(prop) + 1;
883 prop += strlen(prop) + 1;
884 }
885 }
886 printk("]\n\n");
887 return NULL;
888 }
889
890 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
891
892 return best_data;
893}
894
Grant Likelyf7b3a832009-11-24 03:26:58 -0700895#ifdef CONFIG_BLK_DEV_INITRD
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100896#ifndef __early_init_dt_declare_initrd
897static void __early_init_dt_declare_initrd(unsigned long start,
898 unsigned long end)
899{
900 initrd_start = (unsigned long)__va(start);
901 initrd_end = (unsigned long)__va(end);
902 initrd_below_start_ok = 1;
903}
904#endif
905
Grant Likelyf7b3a832009-11-24 03:26:58 -0700906/**
907 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
908 * @node: reference to node containing initrd location ('chosen')
909 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500910static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700911{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400912 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500913 int len;
914 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700915
916 pr_debug("Looking for initrd properties... ");
917
918 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700919 if (!prop)
920 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400921 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700922
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700923 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
924 if (!prop)
925 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400926 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700927
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100928 __early_init_dt_declare_initrd(start, end);
Rob Herring29eb45a2013-08-30 17:06:53 -0500929
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400930 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
931 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700932}
933#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500934static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700935{
936}
937#endif /* CONFIG_BLK_DEV_INITRD */
938
Rob Herringfb11ffe2014-03-27 08:07:01 -0500939#ifdef CONFIG_SERIAL_EARLYCON
Rob Herringfb11ffe2014-03-27 08:07:01 -0500940
Leif Lindholmd5031872016-09-27 23:54:12 +0300941int __init early_init_dt_scan_chosen_stdout(void)
Rob Herringfb11ffe2014-03-27 08:07:01 -0500942{
943 int offset;
Peter Hurley4d118c92016-01-16 15:23:42 -0800944 const char *p, *q, *options = NULL;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500945 int l;
Peter Hurley2eaa7902016-01-16 15:23:39 -0800946 const struct earlycon_id *match;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500947 const void *fdt = initial_boot_params;
948
949 offset = fdt_path_offset(fdt, "/chosen");
950 if (offset < 0)
951 offset = fdt_path_offset(fdt, "/chosen@0");
952 if (offset < 0)
953 return -ENOENT;
954
955 p = fdt_getprop(fdt, offset, "stdout-path", &l);
956 if (!p)
957 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
958 if (!p || !l)
959 return -ENOENT;
960
Peter Hurley4d118c92016-01-16 15:23:42 -0800961 q = strchrnul(p, ':');
962 if (*q != '\0')
963 options = q + 1;
Peter Hurley0fcc2862016-01-16 15:23:48 -0800964 l = q - p;
Stefan Agner6296ad92015-10-10 01:29:30 -0700965
Rob Herringfb11ffe2014-03-27 08:07:01 -0500966 /* Get the node specified by stdout-path */
Peter Hurley0fcc2862016-01-16 15:23:48 -0800967 offset = fdt_path_offset_namelen(fdt, p, l);
968 if (offset < 0) {
969 pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
970 return 0;
971 }
Rob Herringfb11ffe2014-03-27 08:07:01 -0500972
Peter Hurley2eaa7902016-01-16 15:23:39 -0800973 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley2eaa7902016-01-16 15:23:39 -0800974 if (!match->compatible[0])
Rob Herringfb11ffe2014-03-27 08:07:01 -0500975 continue;
Peter Hurley2eaa7902016-01-16 15:23:39 -0800976
977 if (fdt_node_check_compatible(fdt, offset, match->compatible))
978 continue;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500979
Peter Hurleyc90fe9c2016-01-16 15:23:44 -0800980 of_setup_earlycon(match, offset, options);
Rob Herringfb11ffe2014-03-27 08:07:01 -0500981 return 0;
982 }
983 return -ENODEV;
984}
Rob Herringfb11ffe2014-03-27 08:07:01 -0500985#endif
986
Grant Likely41f88002009-11-23 20:07:01 -0700987/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700988 * early_init_dt_scan_root - fetch the top level address and size cells
989 */
990int __init early_init_dt_scan_root(unsigned long node, const char *uname,
991 int depth, void *data)
992{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500993 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700994
995 if (depth != 0)
996 return 0;
997
Jeremy Kerr33714882010-01-30 01:45:26 -0700998 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
999 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
1000
Grant Likelyf00abd92009-11-24 03:27:10 -07001001 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -07001002 if (prop)
1003 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -07001004 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
1005
1006 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -07001007 if (prop)
1008 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -07001009 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1010
1011 /* break now */
1012 return 1;
1013}
1014
Rob Herring9d0c4df2014-04-01 23:49:03 -05001015u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -07001016{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001017 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -07001018
1019 *cellp = p + s;
1020 return of_read_number(p, s);
1021}
1022
Grant Likely51975db2010-02-01 21:34:14 -07001023/**
Frank Rowand0ef5adc2017-06-20 16:46:28 -07001024 * early_init_dt_scan_memory - Look for and parse memory nodes
Grant Likely51975db2010-02-01 21:34:14 -07001025 */
1026int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
1027 int depth, void *data)
1028{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001029 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1030 const __be32 *reg, *endp;
1031 int l;
Reza Arbab41a9ada2016-12-12 16:43:02 -08001032 bool hotpluggable;
Grant Likely51975db2010-02-01 21:34:14 -07001033
1034 /* We are scanning "memory" nodes only */
1035 if (type == NULL) {
1036 /*
1037 * The longtrail doesn't have a device_type on the
1038 * /memory node, so look for the node called /memory@0.
1039 */
Leif Lindholmb44aa252014-04-17 18:42:01 +01001040 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
Grant Likely51975db2010-02-01 21:34:14 -07001041 return 0;
1042 } else if (strcmp(type, "memory") != 0)
1043 return 0;
1044
1045 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1046 if (reg == NULL)
1047 reg = of_get_flat_dt_prop(node, "reg", &l);
1048 if (reg == NULL)
1049 return 0;
1050
1051 endp = reg + (l / sizeof(__be32));
Reza Arbab41a9ada2016-12-12 16:43:02 -08001052 hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
Grant Likely51975db2010-02-01 21:34:14 -07001053
Florian Fainellic954b362015-04-12 13:16:26 -07001054 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
Grant Likely51975db2010-02-01 21:34:14 -07001055
1056 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1057 u64 base, size;
1058
1059 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1060 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1061
1062 if (size == 0)
1063 continue;
1064 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
1065 (unsigned long long)size);
1066
1067 early_init_dt_add_memory_arch(base, size);
Reza Arbab41a9ada2016-12-12 16:43:02 -08001068
1069 if (!hotpluggable)
1070 continue;
1071
1072 if (early_init_dt_mark_hotplug_memory_arch(base, size))
1073 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1074 base, base + size);
Grant Likely51975db2010-02-01 21:34:14 -07001075 }
1076
1077 return 0;
1078}
1079
Grant Likely86e03222009-12-10 23:42:21 -07001080int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
1081 int depth, void *data)
1082{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001083 int l;
1084 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -07001085
1086 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1087
Grant Likely85f60ae2011-04-29 00:18:16 -06001088 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -07001089 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1090 return 0;
1091
1092 early_init_dt_check_for_initrd(node);
1093
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001094 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -07001095 p = of_get_flat_dt_prop(node, "bootargs", &l);
1096 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -06001097 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -07001098
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +00001099 /*
1100 * CONFIG_CMDLINE is meant to be a default in case nothing else
1101 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1102 * is set in which case we override whatever was found earlier.
1103 */
Grant Likely86e03222009-12-10 23:42:21 -07001104#ifdef CONFIG_CMDLINE
Max Uvarov34b82022016-04-13 12:52:16 +03001105#if defined(CONFIG_CMDLINE_EXTEND)
1106 strlcat(data, " ", COMMAND_LINE_SIZE);
1107 strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1108#elif defined(CONFIG_CMDLINE_FORCE)
1109 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1110#else
1111 /* No arguments from boot loader, use kernel's cmdl*/
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +00001112 if (!((char *)data)[0])
Grant Likely85f60ae2011-04-29 00:18:16 -06001113 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Max Uvarov34b82022016-04-13 12:52:16 +03001114#endif
Grant Likely86e03222009-12-10 23:42:21 -07001115#endif /* CONFIG_CMDLINE */
1116
Grant Likely85f60ae2011-04-29 00:18:16 -06001117 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -07001118
1119 /* break now */
1120 return 1;
1121}
1122
Grant Likelya1727da2013-08-28 21:18:32 +01001123#ifdef CONFIG_HAVE_MEMBLOCK
Ard Biesheuvel270522a2016-02-16 13:52:32 +01001124#ifndef MIN_MEMBLOCK_ADDR
1125#define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1126#endif
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001127#ifndef MAX_MEMBLOCK_ADDR
1128#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1129#endif
Laura Abbott3069f0c2014-07-07 17:45:43 -07001130
Rob Herring068f6312013-09-24 22:20:01 -05001131void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1132{
Ard Biesheuvel270522a2016-02-16 13:52:32 +01001133 const u64 phys_offset = MIN_MEMBLOCK_ADDR;
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +02001134
1135 if (!PAGE_ALIGNED(base)) {
Ard Biesheuvel8cccffc2014-10-29 17:09:32 +01001136 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
1137 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1138 base, base + size);
1139 return;
1140 }
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +02001141 size -= PAGE_SIZE - (base & ~PAGE_MASK);
1142 base = PAGE_ALIGN(base);
1143 }
Rob Herring068f6312013-09-24 22:20:01 -05001144 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001145
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001146 if (base > MAX_MEMBLOCK_ADDR) {
Laura Abbott3069f0c2014-07-07 17:45:43 -07001147 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1148 base, base + size);
1149 return;
1150 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001151
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001152 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
Srinivas Kandagatla9aacd602014-09-23 10:59:09 +01001153 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001154 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1155 size = MAX_MEMBLOCK_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001156 }
1157
Rob Herring068f6312013-09-24 22:20:01 -05001158 if (base + size < phys_offset) {
1159 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1160 base, base + size);
1161 return;
1162 }
1163 if (base < phys_offset) {
1164 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1165 base, phys_offset);
1166 size -= phys_offset - base;
1167 base = phys_offset;
1168 }
1169 memblock_add(base, size);
1170}
1171
Reza Arbab41a9ada2016-12-12 16:43:02 -08001172int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1173{
1174 return memblock_mark_hotplug(base, size);
1175}
1176
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001177int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1178 phys_addr_t size, bool nomap)
1179{
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001180 if (nomap)
1181 return memblock_remove(base, size);
1182 return memblock_reserve(base, size);
1183}
1184
Grant Likelya1727da2013-08-28 21:18:32 +01001185/*
1186 * called from unflatten_device_tree() to bootstrap devicetree itself
1187 * Architectures can override this definition if memblock isn't used
1188 */
1189void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1190{
1191 return __va(memblock_alloc(size, align));
1192}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001193#else
Rob Herringaefc7ec2015-06-18 20:35:46 -05001194void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1195{
1196 WARN_ON(1);
1197}
1198
Reza Arbab41a9ada2016-12-12 16:43:02 -08001199int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)
1200{
1201 return -ENOSYS;
1202}
1203
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001204int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1205 phys_addr_t size, bool nomap)
1206{
Dmitry V. Krivenok78bb2ab2015-11-30 23:45:48 +03001207 pr_err("Reserved memory not supported, ignoring range %pa - %pa%s\n",
Rob Herring1d1a6612014-04-22 12:50:24 -05001208 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001209 return -ENOSYS;
1210}
Rob Herringaefc7ec2015-06-18 20:35:46 -05001211
1212void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1213{
1214 WARN_ON(1);
1215 return NULL;
1216}
Grant Likelya1727da2013-08-28 21:18:32 +01001217#endif
1218
Laura Abbott4972a742014-07-15 10:03:34 -07001219bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -05001220{
1221 if (!params)
1222 return false;
1223
Bjorn Helgaas50ba08f2014-10-29 12:15:00 -06001224 /* check device tree validity */
1225 if (fdt_check_header(params))
1226 return false;
1227
Rob Herring0288ffcb2013-08-26 09:47:40 -05001228 /* Setup flat device-tree pointer */
1229 initial_boot_params = params;
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001230 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1231 fdt_totalsize(initial_boot_params));
Laura Abbott4972a742014-07-15 10:03:34 -07001232 return true;
1233}
1234
1235
1236void __init early_init_dt_scan_nodes(void)
1237{
Rob Herring0288ffcb2013-08-26 09:47:40 -05001238 /* Retrieve various information from the /chosen node */
1239 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1240
1241 /* Initialize {size,address}-cells info */
1242 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1243
1244 /* Setup memory, calling early_init_dt_add_memory_arch */
1245 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001246}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001247
Laura Abbott4972a742014-07-15 10:03:34 -07001248bool __init early_init_dt_scan(void *params)
1249{
1250 bool status;
1251
1252 status = early_init_dt_verify(params);
1253 if (!status)
1254 return false;
1255
1256 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001257 return true;
1258}
1259
Grant Likelyf00abd92009-11-24 03:27:10 -07001260/**
Grant Likely41f88002009-11-23 20:07:01 -07001261 * unflatten_device_tree - create tree of device_nodes from flat blob
1262 *
1263 * unflattens the device-tree passed by the firmware, creating the
1264 * tree of struct device_node. It also fills the "name" and "type"
1265 * pointers of the nodes so the normal device-tree walking functions
1266 * can be used.
1267 */
1268void __init unflatten_device_tree(void)
1269{
Gavin Shanc4263232016-05-03 23:22:50 +10001270 __unflatten_device_tree(initial_boot_params, NULL, &of_root,
Michal Suchanek1d1bde52016-07-19 00:01:12 +02001271 early_init_dt_alloc_memory_arch, false);
Grant Likely41f88002009-11-23 20:07:01 -07001272
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001273 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001274 of_alias_scan(early_init_dt_alloc_memory_arch);
Frank Rowand81d0848f2017-04-25 17:09:54 -07001275
1276 unittest_unflatten_overlay_base();
Grant Likely41f88002009-11-23 20:07:01 -07001277}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001278
Rob Herringa8bf7522013-08-26 11:22:45 -05001279/**
1280 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1281 *
1282 * Copies and unflattens the device-tree passed by the firmware, creating the
1283 * tree of struct device_node. It also fills the "name" and "type"
1284 * pointers of the nodes so the normal device-tree walking functions
1285 * can be used. This should only be used when the FDT memory has not been
1286 * reserved such is the case when the FDT is built-in to the kernel init
1287 * section. If the FDT memory is reserved already then unflatten_device_tree
1288 * should be used instead.
1289 */
1290void __init unflatten_and_copy_device_tree(void)
1291{
James Hogan6f041e92013-11-21 13:44:14 +00001292 int size;
1293 void *dt;
1294
1295 if (!initial_boot_params) {
1296 pr_warn("No valid device tree found, continuing without\n");
1297 return;
1298 }
1299
Rob Herringc972de12014-04-01 22:48:01 -05001300 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001301 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001302 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001303
1304 if (dt) {
1305 memcpy(dt, initial_boot_params, size);
1306 initial_boot_params = dt;
1307 }
1308 unflatten_device_tree();
1309}
1310
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001311#ifdef CONFIG_SYSFS
1312static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1313 struct bin_attribute *bin_attr,
1314 char *buf, loff_t off, size_t count)
Rob Herringb0a6fb32014-04-02 16:56:48 -05001315{
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001316 memcpy(buf, initial_boot_params + off, count);
1317 return count;
Rob Herringb0a6fb32014-04-02 16:56:48 -05001318}
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001319
1320static int __init of_fdt_raw_init(void)
1321{
1322 static struct bin_attribute of_fdt_raw_attr =
1323 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1324
1325 if (!initial_boot_params)
1326 return 0;
1327
1328 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1329 fdt_totalsize(initial_boot_params))) {
Rob Herring606ad422016-06-15 08:32:18 -05001330 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001331 return 0;
1332 }
1333 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1334 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1335}
1336late_initcall(of_fdt_raw_init);
Rob Herringb0a6fb32014-04-02 16:56:48 -05001337#endif
1338
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001339#endif /* CONFIG_OF_EARLY_FLATTREE */