blob: 09ed7e597eee8a69f71767448bab6868567a48ae [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0
Grant Likelye169cfb2009-11-23 14:53:09 -07002/*
3 * Functions for working with the Flattened Device Tree data format
4 *
5 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
6 * benh@kernel.crashing.org
Grant Likelye169cfb2009-11-23 14:53:09 -07007 */
8
Frank Rowandbd0096d2016-10-17 12:21:23 -07009#define pr_fmt(fmt) "OF: fdt: " fmt
Rob Herring606ad422016-06-15 08:32:18 -050010
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010011#include <linux/crc32.h>
Grant Likely41f88002009-11-23 20:07:01 -070012#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070013#include <linux/initrd.h>
Grant Likelya1727da2013-08-28 21:18:32 +010014#include <linux/memblock.h>
Guenter Roeckf8062382015-12-05 16:13:53 -080015#include <linux/mutex.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070016#include <linux/of.h>
17#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010018#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +010019#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070020#include <linux/string.h>
21#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080022#include <linux/slab.h>
Rob Herringe6a69282014-04-02 15:10:14 -050023#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050024#include <linux/debugfs.h>
Rob Herringfb11ffe2014-03-27 08:07:01 -050025#include <linux/serial_core.h>
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010026#include <linux/sysfs.h>
Hsin-Yi Wang428826f2019-08-23 14:24:51 +080027#include <linux/random.h>
Grant Likely51975db2010-02-01 21:34:14 -070028
Fabio Estevamc89810a2012-01-02 14:19:03 -020029#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070030#include <asm/page.h>
31
Frank Rowand81d0848f2017-04-25 17:09:54 -070032#include "of_private.h"
33
Laura Abbott704033c2014-07-15 10:03:35 -070034/*
35 * of_fdt_limit_memory - limit the number of regions in the /memory node
36 * @limit: maximum entries
37 *
38 * Adjust the flattened device tree to have at most 'limit' number of
39 * memory entries in the /memory node. This function may be called
40 * any time after initial_boot_param is set.
41 */
Stephen Boyd9b4d2b62019-05-14 13:40:52 -070042void __init of_fdt_limit_memory(int limit)
Laura Abbott704033c2014-07-15 10:03:35 -070043{
44 int memory;
45 int len;
46 const void *val;
47 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
48 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
Rob Herring17a70352017-05-04 12:56:12 -050049 const __be32 *addr_prop;
50 const __be32 *size_prop;
Laura Abbott704033c2014-07-15 10:03:35 -070051 int root_offset;
52 int cell_size;
53
54 root_offset = fdt_path_offset(initial_boot_params, "/");
55 if (root_offset < 0)
56 return;
57
58 addr_prop = fdt_getprop(initial_boot_params, root_offset,
59 "#address-cells", NULL);
60 if (addr_prop)
61 nr_address_cells = fdt32_to_cpu(*addr_prop);
62
63 size_prop = fdt_getprop(initial_boot_params, root_offset,
64 "#size-cells", NULL);
65 if (size_prop)
66 nr_size_cells = fdt32_to_cpu(*size_prop);
67
68 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
69
70 memory = fdt_path_offset(initial_boot_params, "/memory");
71 if (memory > 0) {
72 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
73 if (len > limit*cell_size) {
74 len = limit*cell_size;
75 pr_debug("Limiting number of entries to %d\n", limit);
76 fdt_setprop(initial_boot_params, memory, "reg", val,
77 len);
78 }
79 }
80}
81
Rob Herringecc8a962017-09-28 19:20:32 -050082static bool of_fdt_device_is_available(const void *blob, unsigned long node)
83{
84 const char *status = fdt_getprop(blob, node, "status", NULL);
85
86 if (!status)
87 return true;
88
89 if (!strcmp(status, "ok") || !strcmp(status, "okay"))
90 return true;
91
92 return false;
93}
94
Grant Likely44856812013-08-29 13:30:35 +010095static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -070096 unsigned long align)
97{
98 void *res;
99
Grant Likely44856812013-08-29 13:30:35 +0100100 *mem = PTR_ALIGN(*mem, align);
101 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700102 *mem += size;
103
104 return res;
105}
106
Gavin Shandfbd4c62016-05-03 23:22:47 +1000107static void populate_properties(const void *blob,
108 int offset,
109 void **mem,
110 struct device_node *np,
111 const char *nodename,
Grant Likely5063e252014-10-03 16:28:27 +0100112 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700113{
Gavin Shandfbd4c62016-05-03 23:22:47 +1000114 struct property *pp, **pprev = NULL;
115 int cur;
116 bool has_name = false;
117
118 pprev = &np->properties;
119 for (cur = fdt_first_property_offset(blob, offset);
120 cur >= 0;
121 cur = fdt_next_property_offset(blob, cur)) {
122 const __be32 *val;
123 const char *pname;
124 u32 sz;
125
126 val = fdt_getprop_by_offset(blob, cur, &pname, &sz);
127 if (!val) {
Rob Herring606ad422016-06-15 08:32:18 -0500128 pr_warn("Cannot locate property at 0x%x\n", cur);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000129 continue;
130 }
131
132 if (!pname) {
Rob Herring606ad422016-06-15 08:32:18 -0500133 pr_warn("Cannot find property name at 0x%x\n", cur);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000134 continue;
135 }
136
137 if (!strcmp(pname, "name"))
138 has_name = true;
139
140 pp = unflatten_dt_alloc(mem, sizeof(struct property),
141 __alignof__(struct property));
142 if (dryrun)
143 continue;
144
145 /* We accept flattened tree phandles either in
146 * ePAPR-style "phandle" properties, or the
147 * legacy "linux,phandle" properties. If both
148 * appear and have different values, things
149 * will get weird. Don't do that.
150 */
151 if (!strcmp(pname, "phandle") ||
152 !strcmp(pname, "linux,phandle")) {
153 if (!np->phandle)
154 np->phandle = be32_to_cpup(val);
155 }
156
157 /* And we process the "ibm,phandle" property
158 * used in pSeries dynamic device tree
159 * stuff
160 */
161 if (!strcmp(pname, "ibm,phandle"))
162 np->phandle = be32_to_cpup(val);
163
164 pp->name = (char *)pname;
165 pp->length = sz;
166 pp->value = (__be32 *)val;
167 *pprev = pp;
168 pprev = &pp->next;
169 }
170
171 /* With version 0x10 we may not have the name property,
172 * recreate it here from the unit name if absent
173 */
174 if (!has_name) {
175 const char *p = nodename, *ps = p, *pa = NULL;
176 int len;
177
178 while (*p) {
179 if ((*p) == '@')
180 pa = p;
181 else if ((*p) == '/')
182 ps = p + 1;
183 p++;
184 }
185
186 if (pa < ps)
187 pa = p;
188 len = (pa - ps) + 1;
189 pp = unflatten_dt_alloc(mem, sizeof(struct property) + len,
190 __alignof__(struct property));
191 if (!dryrun) {
192 pp->name = "name";
193 pp->length = len;
194 pp->value = pp + 1;
195 *pprev = pp;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000196 memcpy(pp->value, ps, len - 1);
197 ((char *)pp->value)[len - 1] = 0;
198 pr_debug("fixed up name for %s -> %s\n",
199 nodename, (char *)pp->value);
200 }
201 }
Gavin Shandfbd4c62016-05-03 23:22:47 +1000202}
203
Frank Rowand649cab52021-04-04 22:28:45 -0500204static int populate_node(const void *blob,
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500205 int offset,
206 void **mem,
207 struct device_node *dad,
208 struct device_node **pnp,
209 bool dryrun)
Gavin Shandfbd4c62016-05-03 23:22:47 +1000210{
Grant Likelybbd33932009-11-23 20:07:00 -0700211 struct device_node *np;
Rob Herringe6a69282014-04-02 15:10:14 -0500212 const char *pathp;
Frank Rowand649cab52021-04-04 22:28:45 -0500213 int len;
Grant Likelybbd33932009-11-23 20:07:00 -0700214
Frank Rowand649cab52021-04-04 22:28:45 -0500215 pathp = fdt_get_name(blob, offset, &len);
Gavin Shandfbd4c62016-05-03 23:22:47 +1000216 if (!pathp) {
217 *pnp = NULL;
Frank Rowand649cab52021-04-04 22:28:45 -0500218 return len;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000219 }
Rob Herringe6a69282014-04-02 15:10:14 -0500220
Frank Rowand649cab52021-04-04 22:28:45 -0500221 len++;
Grant Likelybbd33932009-11-23 20:07:00 -0700222
Frank Rowand649cab52021-04-04 22:28:45 -0500223 np = unflatten_dt_alloc(mem, sizeof(struct device_node) + len,
Grant Likelybbd33932009-11-23 20:07:00 -0700224 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100225 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000226 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200227 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000228 np->full_name = fn = ((char *)np) + sizeof(*np);
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500229
Frank Rowand649cab52021-04-04 22:28:45 -0500230 memcpy(fn, pathp, len);
Grant Likelyc22618a2012-11-14 22:37:12 +0000231
Grant Likelybbd33932009-11-23 20:07:00 -0700232 if (dad != NULL) {
233 np->parent = dad;
Grant Likely70161ff2014-11-28 16:03:33 +0000234 np->sibling = dad->child;
235 dad->child = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700236 }
Grant Likelybbd33932009-11-23 20:07:00 -0700237 }
Grant Likelybbd33932009-11-23 20:07:00 -0700238
Gavin Shandfbd4c62016-05-03 23:22:47 +1000239 populate_properties(blob, offset, mem, np, pathp, dryrun);
Grant Likely5063e252014-10-03 16:28:27 +0100240 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700241 np->name = of_get_property(np, "name", NULL);
Grant Likelybbd33932009-11-23 20:07:00 -0700242 if (!np->name)
243 np->name = "<NULL>";
Grant Likelybbd33932009-11-23 20:07:00 -0700244 }
Rob Herringe6a69282014-04-02 15:10:14 -0500245
Gavin Shandfbd4c62016-05-03 23:22:47 +1000246 *pnp = np;
Rob Herringa7e4cfb2017-06-01 18:01:47 -0500247 return true;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000248}
249
Gavin Shan50800082016-05-03 23:22:48 +1000250static void reverse_nodes(struct device_node *parent)
251{
252 struct device_node *child, *next;
253
254 /* In-depth first */
255 child = parent->child;
256 while (child) {
257 reverse_nodes(child);
258
259 child = child->sibling;
260 }
261
262 /* Reverse the nodes in the child list */
263 child = parent->child;
264 parent->child = NULL;
265 while (child) {
266 next = child->sibling;
267
268 child->sibling = parent->child;
269 parent->child = child;
270 child = next;
271 }
272}
273
Gavin Shandfbd4c62016-05-03 23:22:47 +1000274/**
Gavin Shan947c82c2016-05-03 23:22:49 +1000275 * unflatten_dt_nodes - Alloc and populate a device_node from the flat tree
Gavin Shandfbd4c62016-05-03 23:22:47 +1000276 * @blob: The parent device tree blob
277 * @mem: Memory chunk to use for allocating device nodes and properties
Gavin Shandfbd4c62016-05-03 23:22:47 +1000278 * @dad: Parent struct device_node
279 * @nodepp: The device_node tree created by the call
Gavin Shan50800082016-05-03 23:22:48 +1000280 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600281 * Return: The size of unflattened device tree or error code
Gavin Shandfbd4c62016-05-03 23:22:47 +1000282 */
Gavin Shan947c82c2016-05-03 23:22:49 +1000283static int unflatten_dt_nodes(const void *blob,
284 void *mem,
285 struct device_node *dad,
286 struct device_node **nodepp)
Gavin Shandfbd4c62016-05-03 23:22:47 +1000287{
Gavin Shan50800082016-05-03 23:22:48 +1000288 struct device_node *root;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000289 int offset = 0, depth = 0, initial_depth = 0;
Gavin Shan50800082016-05-03 23:22:48 +1000290#define FDT_MAX_DEPTH 64
Gavin Shan50800082016-05-03 23:22:48 +1000291 struct device_node *nps[FDT_MAX_DEPTH];
292 void *base = mem;
293 bool dryrun = !base;
Frank Rowand649cab52021-04-04 22:28:45 -0500294 int ret;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000295
Gavin Shan50800082016-05-03 23:22:48 +1000296 if (nodepp)
297 *nodepp = NULL;
Gavin Shandfbd4c62016-05-03 23:22:47 +1000298
Gavin Shan8c237cd2016-06-09 15:50:49 +1000299 /*
300 * We're unflattening device sub-tree if @dad is valid. There are
301 * possibly multiple nodes in the first level of depth. We need
302 * set @depth to 1 to make fdt_next_node() happy as it bails
303 * immediately when negative @depth is found. Otherwise, the device
304 * nodes except the first one won't be unflattened successfully.
305 */
306 if (dad)
307 depth = initial_depth = 1;
308
Gavin Shan50800082016-05-03 23:22:48 +1000309 root = dad;
Rhyland Klein78c44d92016-05-11 13:36:57 -0400310 nps[depth] = dad;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000311
Gavin Shan50800082016-05-03 23:22:48 +1000312 for (offset = 0;
Gavin Shan8c237cd2016-06-09 15:50:49 +1000313 offset >= 0 && depth >= initial_depth;
Gavin Shan50800082016-05-03 23:22:48 +1000314 offset = fdt_next_node(blob, offset, &depth)) {
315 if (WARN_ON_ONCE(depth >= FDT_MAX_DEPTH))
316 continue;
Rob Herringe6a69282014-04-02 15:10:14 -0500317
Rob Herring77ea8a62017-10-03 11:07:55 -0500318 if (!IS_ENABLED(CONFIG_OF_KOBJ) &&
319 !of_fdt_device_is_available(blob, offset))
320 continue;
321
Frank Rowand649cab52021-04-04 22:28:45 -0500322 ret = populate_node(blob, offset, &mem, nps[depth],
323 &nps[depth+1], dryrun);
324 if (ret < 0)
325 return ret;
Gavin Shan50800082016-05-03 23:22:48 +1000326
327 if (!dryrun && nodepp && !*nodepp)
Rhyland Klein78c44d92016-05-11 13:36:57 -0400328 *nodepp = nps[depth+1];
Gavin Shan50800082016-05-03 23:22:48 +1000329 if (!dryrun && !root)
Rhyland Klein78c44d92016-05-11 13:36:57 -0400330 root = nps[depth+1];
Gavin Shan50800082016-05-03 23:22:48 +1000331 }
332
333 if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
Rob Herring606ad422016-06-15 08:32:18 -0500334 pr_err("Error %d processing FDT\n", offset);
Gavin Shan50800082016-05-03 23:22:48 +1000335 return -EINVAL;
336 }
Rob Herringe6a69282014-04-02 15:10:14 -0500337
Grant Likely70161ff2014-11-28 16:03:33 +0000338 /*
339 * Reverse the child list. Some drivers assumes node order matches .dts
340 * node order
341 */
Gavin Shan50800082016-05-03 23:22:48 +1000342 if (!dryrun)
343 reverse_nodes(root);
Grant Likely70161ff2014-11-28 16:03:33 +0000344
Gavin Shan50800082016-05-03 23:22:48 +1000345 return mem - base;
Grant Likelybbd33932009-11-23 20:07:00 -0700346}
Grant Likely41f88002009-11-23 20:07:01 -0700347
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800348/**
349 * __unflatten_device_tree - create tree of device_nodes from flat blob
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800350 * @blob: The blob to expand
Gavin Shanc4263232016-05-03 23:22:50 +1000351 * @dad: Parent device node
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800352 * @mynodes: The device_node tree created by the call
353 * @dt_alloc: An allocator that provides a virtual address to memory
354 * for the resulting tree
Stephen Boydf5d2da62017-10-13 00:41:29 -0700355 * @detached: if true set OF_DETACHED on @mynodes
Gavin Shan83262412016-05-03 23:22:51 +1000356 *
Rob Herring62f026f2021-03-26 13:26:06 -0600357 * unflattens a device-tree, creating the tree of struct device_node. It also
358 * fills the "name" and "type" pointers of the nodes so the normal device-tree
359 * walking functions can be used.
360 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600361 * Return: NULL on failure or the memory chunk containing the unflattened
Gavin Shan83262412016-05-03 23:22:51 +1000362 * device tree on success.
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800363 */
Frank Rowand81d0848f2017-04-25 17:09:54 -0700364void *__unflatten_device_tree(const void *blob,
365 struct device_node *dad,
366 struct device_node **mynodes,
367 void *(*dt_alloc)(u64 size, u64 align),
368 bool detached)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800369{
Gavin Shan50800082016-05-03 23:22:48 +1000370 int size;
Rob Herringe6a69282014-04-02 15:10:14 -0500371 void *mem;
Frank Rowand649cab52021-04-04 22:28:45 -0500372 int ret;
373
374 if (mynodes)
375 *mynodes = NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800376
377 pr_debug(" -> unflatten_device_tree()\n");
378
379 if (!blob) {
380 pr_debug("No device tree pointer\n");
Gavin Shan83262412016-05-03 23:22:51 +1000381 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800382 }
383
384 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500385 pr_debug("magic: %08x\n", fdt_magic(blob));
386 pr_debug("size: %08x\n", fdt_totalsize(blob));
387 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800388
Rob Herringc972de12014-04-01 22:48:01 -0500389 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800390 pr_err("Invalid device tree blob header\n");
Gavin Shan83262412016-05-03 23:22:51 +1000391 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800392 }
393
394 /* First pass, scan for size */
Gavin Shanc4263232016-05-03 23:22:50 +1000395 size = unflatten_dt_nodes(blob, NULL, dad, NULL);
Frank Rowand649cab52021-04-04 22:28:45 -0500396 if (size <= 0)
Gavin Shan83262412016-05-03 23:22:51 +1000397 return NULL;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800398
Gavin Shan50800082016-05-03 23:22:48 +1000399 size = ALIGN(size, 4);
400 pr_debug(" size is %d, allocating...\n", size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800401
402 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100403 mem = dt_alloc(size + 4, __alignof__(struct device_node));
Johan Hovold49e67dd2017-05-17 17:29:09 +0200404 if (!mem)
405 return NULL;
406
Grant Likely44856812013-08-29 13:30:35 +0100407 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800408
Grant Likely44856812013-08-29 13:30:35 +0100409 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200410
Grant Likely44856812013-08-29 13:30:35 +0100411 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800412
413 /* Second pass, do actual unflattening */
Frank Rowand649cab52021-04-04 22:28:45 -0500414 ret = unflatten_dt_nodes(blob, mem, dad, mynodes);
415
Grant Likely44856812013-08-29 13:30:35 +0100416 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Kefeng Wange2f04da2019-10-18 11:18:33 +0800417 pr_warn("End of tree marker overwritten: %08x\n",
418 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800419
Frank Rowand649cab52021-04-04 22:28:45 -0500420 if (ret <= 0)
421 return NULL;
422
423 if (detached && mynodes && *mynodes) {
Michal Suchanek1d1bde52016-07-19 00:01:12 +0200424 of_node_set_flag(*mynodes, OF_DETACHED);
425 pr_debug("unflattened tree is detached\n");
426 }
427
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800428 pr_debug(" <- unflatten_device_tree()\n");
Gavin Shan83262412016-05-03 23:22:51 +1000429 return mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800430}
431
432static void *kernel_tree_alloc(u64 size, u64 align)
433{
434 return kzalloc(size, GFP_KERNEL);
435}
436
Guenter Roeckf8062382015-12-05 16:13:53 -0800437static DEFINE_MUTEX(of_fdt_unflatten_mutex);
438
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800439/**
440 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
Gavin Shanc4263232016-05-03 23:22:50 +1000441 * @blob: Flat device tree blob
442 * @dad: Parent device node
443 * @mynodes: The device tree created by the call
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800444 *
445 * unflattens the device-tree passed by the firmware, creating the
446 * tree of struct device_node. It also fills the "name" and "type"
447 * pointers of the nodes so the normal device-tree walking functions
448 * can be used.
Gavin Shan83262412016-05-03 23:22:51 +1000449 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600450 * Return: NULL on failure or the memory chunk containing the unflattened
Gavin Shan83262412016-05-03 23:22:51 +1000451 * device tree on success.
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800452 */
Gavin Shan83262412016-05-03 23:22:51 +1000453void *of_fdt_unflatten_tree(const unsigned long *blob,
454 struct device_node *dad,
455 struct device_node **mynodes)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800456{
Gavin Shan83262412016-05-03 23:22:51 +1000457 void *mem;
458
Guenter Roeckf8062382015-12-05 16:13:53 -0800459 mutex_lock(&of_fdt_unflatten_mutex);
Michal Suchanek1d1bde52016-07-19 00:01:12 +0200460 mem = __unflatten_device_tree(blob, dad, mynodes, &kernel_tree_alloc,
461 true);
Guenter Roeckf8062382015-12-05 16:13:53 -0800462 mutex_unlock(&of_fdt_unflatten_mutex);
Gavin Shan83262412016-05-03 23:22:51 +1000463
464 return mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800465}
466EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
467
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800468/* Everything below here references initial_boot_params directly. */
469int __initdata dt_root_addr_cells;
470int __initdata dt_root_size_cells;
471
Stephen Boyd7c716502019-05-14 13:40:53 -0700472void *initial_boot_params __ro_after_init;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800473
474#ifdef CONFIG_OF_EARLY_FLATTREE
475
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +0100476static u32 of_fdt_crc32;
477
Geert Uytterhoeven18250b42021-08-11 10:52:28 +0200478static int __init early_init_dt_reserve_memory_arch(phys_addr_t base,
479 phys_addr_t size, bool nomap)
480{
481 if (nomap) {
482 /*
483 * If the memory is already reserved (by another region), we
484 * should not allow it to be marked nomap.
485 */
486 if (memblock_is_region_reserved(base, size))
487 return -EBUSY;
488
489 return memblock_mark_nomap(base, size);
490 }
491 return memblock_reserve(base, size);
492}
493
Lee Jonesa300dc82021-03-18 10:40:33 +0000494/*
chenqiwuc8813f72020-05-11 23:04:57 +0800495 * __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100496 */
497static int __init __reserved_mem_reserve_reg(unsigned long node,
498 const char *uname)
499{
500 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
501 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500502 int len;
503 const __be32 *prop;
Masahiro Yamada5c68b822019-05-30 19:39:27 +0900504 int first = 1;
505 bool nomap;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100506
507 prop = of_get_flat_dt_prop(node, "reg", &len);
508 if (!prop)
509 return -ENOENT;
510
511 if (len && len % t_len != 0) {
512 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
513 uname);
514 return -EINVAL;
515 }
516
517 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
518
519 while (len >= t_len) {
520 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
521 size = dt_mem_next_cell(dt_root_size_cells, &prop);
522
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400523 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100524 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
Geert Uytterhoeven2892d8a2021-06-16 11:27:44 +0200525 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
526 uname, &base, (unsigned long)(size / SZ_1M));
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100527 else
Geert Uytterhoeven2892d8a2021-06-16 11:27:44 +0200528 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
529 uname, &base, (unsigned long)(size / SZ_1M));
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100530
531 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100532 if (first) {
533 fdt_reserved_mem_save_node(node, uname, base, size);
534 first = 0;
535 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100536 }
537 return 0;
538}
539
Lee Jonesa300dc82021-03-18 10:40:33 +0000540/*
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100541 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
542 * in /reserved-memory matches the values supported by the current implementation,
543 * also check if ranges property has been provided
544 */
Xiubo Li5b624112014-04-08 13:48:07 +0800545static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100546{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500547 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100548
549 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
550 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
551 return -EINVAL;
552
553 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
554 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
555 return -EINVAL;
556
557 prop = of_get_flat_dt_prop(node, "ranges", NULL);
558 if (!prop)
559 return -EINVAL;
560 return 0;
561}
562
Lee Jonesa300dc82021-03-18 10:40:33 +0000563/*
564 * __fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100565 */
566static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
567 int depth, void *data)
568{
569 static int found;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100570 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100571
572 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
573 if (__reserved_mem_check_root(node) != 0) {
574 pr_err("Reserved memory: unsupported node format, ignoring\n");
575 /* break scan */
576 return 1;
577 }
578 found = 1;
579 /* scan next node */
580 return 0;
581 } else if (!found) {
582 /* scan next node */
583 return 0;
584 } else if (found && depth < 2) {
585 /* scanning of /reserved-memory has been finished */
586 return 1;
587 }
588
Rob Herringecc8a962017-09-28 19:20:32 -0500589 if (!of_fdt_device_is_available(initial_boot_params, node))
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100590 return 0;
591
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100592 err = __reserved_mem_reserve_reg(node, uname);
593 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
594 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100595
596 /* scan next node */
597 return 0;
598}
599
600/**
601 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
602 *
603 * This function grabs memory from early allocator for device exclusive use
604 * defined in device tree structures. It should be called by arch specific code
605 * once the early allocator (i.e. memblock) has been fully activated.
606 */
607void __init early_init_fdt_scan_reserved_mem(void)
608{
Rob Herringd1552ce2014-04-01 22:46:48 -0500609 int n;
610 u64 base, size;
611
Josh Cartwright2040b522014-03-13 16:36:36 -0500612 if (!initial_boot_params)
613 return;
614
Rob Herringd1552ce2014-04-01 22:46:48 -0500615 /* Process header /memreserve/ fields */
616 for (n = 0; ; n++) {
617 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
618 if (!size)
619 break;
Masahiro Yamada5c68b822019-05-30 19:39:27 +0900620 early_init_dt_reserve_memory_arch(base, size, false);
Rob Herringd1552ce2014-04-01 22:46:48 -0500621 }
622
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100623 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100624 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100625}
626
627/**
Ard Biesheuvel24bbd922015-06-01 13:40:31 +0200628 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
629 */
630void __init early_init_fdt_reserve_self(void)
631{
632 if (!initial_boot_params)
633 return;
634
635 /* Reserve the dtb region */
636 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
637 fdt_totalsize(initial_boot_params),
Masahiro Yamada5c68b822019-05-30 19:39:27 +0900638 false);
Ard Biesheuvel24bbd922015-06-01 13:40:31 +0200639}
640
641/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800642 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
643 * @it: callback function
644 * @data: context data pointer
645 *
646 * This function is used to scan the flattened device-tree, it is
647 * used to extract the memory information at boot before we can
648 * unflatten the tree
649 */
650int __init of_scan_flat_dt(int (*it)(unsigned long node,
651 const char *uname, int depth,
652 void *data),
653 void *data)
654{
Rob Herringe6a69282014-04-02 15:10:14 -0500655 const void *blob = initial_boot_params;
656 const char *pathp;
657 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800658
Tobias Wolf3ec75442016-11-23 10:40:07 +0100659 if (!blob)
660 return 0;
661
662 for (offset = fdt_next_node(blob, -1, &depth);
663 offset >= 0 && depth >= 0 && !rc;
664 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800665
Rob Herringe6a69282014-04-02 15:10:14 -0500666 pathp = fdt_get_name(blob, offset, NULL);
Rob Herringe6a69282014-04-02 15:10:14 -0500667 rc = it(offset, pathp, depth, data);
668 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800669 return rc;
670}
671
672/**
Nicholas Pigginea47dd12017-04-19 05:12:18 +1000673 * of_scan_flat_dt_subnodes - scan sub-nodes of a node call callback on each.
Lee Jonesa300dc82021-03-18 10:40:33 +0000674 * @parent: parent node
Nicholas Pigginea47dd12017-04-19 05:12:18 +1000675 * @it: callback function
676 * @data: context data pointer
677 *
678 * This function is used to scan sub-nodes of a node.
679 */
680int __init of_scan_flat_dt_subnodes(unsigned long parent,
681 int (*it)(unsigned long node,
682 const char *uname,
683 void *data),
684 void *data)
685{
686 const void *blob = initial_boot_params;
687 int node;
688
689 fdt_for_each_subnode(node, blob, parent) {
690 const char *pathp;
691 int rc;
692
693 pathp = fdt_get_name(blob, node, NULL);
Nicholas Pigginea47dd12017-04-19 05:12:18 +1000694 rc = it(node, pathp, data);
695 if (rc)
696 return rc;
697 }
698 return 0;
699}
700
701/**
Shannon Zhao9c609862016-04-07 20:03:33 +0800702 * of_get_flat_dt_subnode_by_name - get the subnode by given name
703 *
704 * @node: the parent node
705 * @uname: the name of subnode
706 * @return offset of the subnode, or -FDT_ERR_NOTFOUND if there is none
707 */
708
Stephen Boyd9b4d2b62019-05-14 13:40:52 -0700709int __init of_get_flat_dt_subnode_by_name(unsigned long node, const char *uname)
Shannon Zhao9c609862016-04-07 20:03:33 +0800710{
711 return fdt_subnode_offset(initial_boot_params, node, uname);
712}
713
Lee Jonesa300dc82021-03-18 10:40:33 +0000714/*
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800715 * of_get_flat_dt_root - find the root node in the flat blob
716 */
717unsigned long __init of_get_flat_dt_root(void)
718{
Rob Herringe6a69282014-04-02 15:10:14 -0500719 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800720}
721
Lee Jonesa300dc82021-03-18 10:40:33 +0000722/*
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800723 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
724 *
725 * This function can be used within scan_flattened_dt callback to get
726 * access to properties
727 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500728const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
729 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800730{
Rob Herringe6a69282014-04-02 15:10:14 -0500731 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800732}
733
734/**
Kefeng Wang5d9c4e92019-06-15 11:03:43 +0800735 * of_fdt_is_compatible - Return true if given node from the given blob has
736 * compat in its compatible list
737 * @blob: A device tree blob
738 * @node: node to test
739 * @compat: compatible string to compare with compatible list.
740 *
Rob Herring8c8239c2021-03-25 10:47:12 -0600741 * Return: a non-zero value on match with smaller values returned for more
Kefeng Wang5d9c4e92019-06-15 11:03:43 +0800742 * specific compatible values.
743 */
744static int of_fdt_is_compatible(const void *blob,
745 unsigned long node, const char *compat)
746{
747 const char *cp;
748 int cplen;
749 unsigned long l, score = 0;
750
751 cp = fdt_getprop(blob, node, "compatible", &cplen);
752 if (cp == NULL)
753 return 0;
754 while (cplen > 0) {
755 score++;
756 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
757 return score;
758 l = strlen(cp) + 1;
759 cp += l;
760 cplen -= l;
761 }
762
763 return 0;
764}
765
766/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800767 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
768 * @node: node to test
769 * @compat: compatible string to compare with compatible list.
770 */
771int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
772{
773 return of_fdt_is_compatible(initial_boot_params, node, compat);
774}
775
Lee Jonesa300dc82021-03-18 10:40:33 +0000776/*
Grant Likelya4f740c2010-10-30 11:49:09 -0400777 * of_flat_dt_match - Return true if node matches a list of compatible values
778 */
Stephen Boyd9b4d2b62019-05-14 13:40:52 -0700779static int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400780{
Kefeng Wang5d9c4e92019-06-15 11:03:43 +0800781 unsigned int tmp, score = 0;
782
783 if (!compat)
784 return 0;
785
786 while (*compat) {
787 tmp = of_fdt_is_compatible(initial_boot_params, node, *compat);
788 if (tmp && (score == 0 || (tmp < score)))
789 score = tmp;
790 compat++;
791 }
792
793 return score;
Grant Likelya4f740c2010-10-30 11:49:09 -0400794}
795
Lee Jonesa300dc82021-03-18 10:40:33 +0000796/*
797 * of_get_flat_dt_phandle - Given a node in the flat blob, return the phandle
Nicholas Pigginea47dd12017-04-19 05:12:18 +1000798 */
799uint32_t __init of_get_flat_dt_phandle(unsigned long node)
800{
801 return fdt_get_phandle(initial_boot_params, node);
802}
803
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200804struct fdt_scan_status {
805 const char *name;
806 int namelen;
807 int depth;
808 int found;
809 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
810 void *data;
811};
812
Rob Herring6a903a22013-08-27 21:41:56 -0500813const char * __init of_flat_dt_get_machine_name(void)
814{
815 const char *name;
816 unsigned long dt_root = of_get_flat_dt_root();
817
818 name = of_get_flat_dt_prop(dt_root, "model", NULL);
819 if (!name)
820 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
821 return name;
822}
823
824/**
825 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
826 *
827 * @default_match: A machine specific ptr to return in case of no match.
828 * @get_next_compat: callback function to return next compatible match table.
829 *
830 * Iterate through machine match tables to find the best match for the machine
831 * compatible string in the FDT.
832 */
833const void * __init of_flat_dt_match_machine(const void *default_match,
834 const void * (*get_next_compat)(const char * const**))
835{
836 const void *data = NULL;
837 const void *best_data = default_match;
838 const char *const *compat;
839 unsigned long dt_root;
840 unsigned int best_score = ~1, score = 0;
841
842 dt_root = of_get_flat_dt_root();
843 while ((data = get_next_compat(&compat))) {
844 score = of_flat_dt_match(dt_root, compat);
845 if (score > 0 && score < best_score) {
846 best_data = data;
847 best_score = score;
848 }
849 }
850 if (!best_data) {
851 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500852 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500853
854 pr_err("\n unrecognized device tree list:\n[ ");
855
856 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
857 if (prop) {
858 while (size > 0) {
859 printk("'%s' ", prop);
860 size -= strlen(prop) + 1;
861 prop += strlen(prop) + 1;
862 }
863 }
864 printk("]\n\n");
865 return NULL;
866 }
867
868 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
869
870 return best_data;
871}
872
Grant Likelyf7b3a832009-11-24 03:26:58 -0700873#ifdef CONFIG_BLK_DEV_INITRD
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100874static void __early_init_dt_declare_initrd(unsigned long start,
875 unsigned long end)
876{
Florian Fainellicdbc8482018-11-05 14:54:30 -0800877 /* ARM64 would cause a BUG to occur here when CONFIG_DEBUG_VM is
878 * enabled since __va() is called too early. ARM64 does make use
879 * of phys_initrd_start/phys_initrd_size so we can skip this
880 * conversion.
881 */
882 if (!IS_ENABLED(CONFIG_ARM64)) {
883 initrd_start = (unsigned long)__va(start);
884 initrd_end = (unsigned long)__va(end);
885 initrd_below_start_ok = 1;
886 }
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100887}
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100888
Grant Likelyf7b3a832009-11-24 03:26:58 -0700889/**
890 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
891 * @node: reference to node containing initrd location ('chosen')
892 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500893static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700894{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400895 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500896 int len;
897 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700898
899 pr_debug("Looking for initrd properties... ");
900
901 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700902 if (!prop)
903 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400904 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700905
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700906 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
907 if (!prop)
908 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400909 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700910
Ard Biesheuvel369bc9a2016-02-16 13:52:33 +0100911 __early_init_dt_declare_initrd(start, end);
Florian Fainellife7db752018-11-05 14:54:28 -0800912 phys_initrd_start = start;
913 phys_initrd_size = end - start;
Rob Herring29eb45a2013-08-30 17:06:53 -0500914
Geert Uytterhoeven0e407a92021-06-16 11:27:45 +0200915 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n", start, end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700916}
917#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500918static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700919{
920}
921#endif /* CONFIG_BLK_DEV_INITRD */
922
Rob Herringfb11ffe2014-03-27 08:07:01 -0500923#ifdef CONFIG_SERIAL_EARLYCON
Rob Herringfb11ffe2014-03-27 08:07:01 -0500924
Leif Lindholmd5031872016-09-27 23:54:12 +0300925int __init early_init_dt_scan_chosen_stdout(void)
Rob Herringfb11ffe2014-03-27 08:07:01 -0500926{
927 int offset;
Peter Hurley4d118c92016-01-16 15:23:42 -0800928 const char *p, *q, *options = NULL;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500929 int l;
Johan Hovold62dcd9c2020-11-23 11:23:13 +0100930 const struct earlycon_id *match;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500931 const void *fdt = initial_boot_params;
932
933 offset = fdt_path_offset(fdt, "/chosen");
934 if (offset < 0)
935 offset = fdt_path_offset(fdt, "/chosen@0");
936 if (offset < 0)
937 return -ENOENT;
938
939 p = fdt_getprop(fdt, offset, "stdout-path", &l);
940 if (!p)
941 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
942 if (!p || !l)
943 return -ENOENT;
944
Peter Hurley4d118c92016-01-16 15:23:42 -0800945 q = strchrnul(p, ':');
946 if (*q != '\0')
947 options = q + 1;
Peter Hurley0fcc2862016-01-16 15:23:48 -0800948 l = q - p;
Stefan Agner6296ad92015-10-10 01:29:30 -0700949
Rob Herringfb11ffe2014-03-27 08:07:01 -0500950 /* Get the node specified by stdout-path */
Peter Hurley0fcc2862016-01-16 15:23:48 -0800951 offset = fdt_path_offset_namelen(fdt, p, l);
952 if (offset < 0) {
953 pr_warn("earlycon: stdout-path %.*s not found\n", l, p);
954 return 0;
955 }
Rob Herringfb11ffe2014-03-27 08:07:01 -0500956
Johan Hovold62dcd9c2020-11-23 11:23:13 +0100957 for (match = __earlycon_table; match < __earlycon_table_end; match++) {
Peter Hurley2eaa7902016-01-16 15:23:39 -0800958 if (!match->compatible[0])
Rob Herringfb11ffe2014-03-27 08:07:01 -0500959 continue;
Peter Hurley2eaa7902016-01-16 15:23:39 -0800960
961 if (fdt_node_check_compatible(fdt, offset, match->compatible))
962 continue;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500963
Christoph Hellwigb2047312019-09-10 07:58:33 +0200964 if (of_setup_earlycon(match, offset, options) == 0)
965 return 0;
Rob Herringfb11ffe2014-03-27 08:07:01 -0500966 }
967 return -ENODEV;
968}
Rob Herringfb11ffe2014-03-27 08:07:01 -0500969#endif
970
Lee Jonesa300dc82021-03-18 10:40:33 +0000971/*
Grant Likelyf00abd92009-11-24 03:27:10 -0700972 * early_init_dt_scan_root - fetch the top level address and size cells
973 */
974int __init early_init_dt_scan_root(unsigned long node, const char *uname,
975 int depth, void *data)
976{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500977 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700978
979 if (depth != 0)
980 return 0;
981
Jeremy Kerr33714882010-01-30 01:45:26 -0700982 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
983 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
984
Grant Likelyf00abd92009-11-24 03:27:10 -0700985 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700986 if (prop)
987 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700988 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
989
990 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700991 if (prop)
992 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700993 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
994
995 /* break now */
996 return 1;
997}
998
Rob Herring9d0c4df2014-04-01 23:49:03 -0500999u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -07001000{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001001 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -07001002
1003 *cellp = p + s;
1004 return of_read_number(p, s);
1005}
1006
Lee Jonesa300dc82021-03-18 10:40:33 +00001007/*
Frank Rowand0ef5adc2017-06-20 16:46:28 -07001008 * early_init_dt_scan_memory - Look for and parse memory nodes
Grant Likely51975db2010-02-01 21:34:14 -07001009 */
1010int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
1011 int depth, void *data)
1012{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001013 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1014 const __be32 *reg, *endp;
1015 int l;
Reza Arbab41a9ada2016-12-12 16:43:02 -08001016 bool hotpluggable;
Grant Likely51975db2010-02-01 21:34:14 -07001017
1018 /* We are scanning "memory" nodes only */
Michael Ellermanda653132018-07-27 15:35:55 +10001019 if (type == NULL || strcmp(type, "memory") != 0)
Grant Likely51975db2010-02-01 21:34:14 -07001020 return 0;
1021
1022 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1023 if (reg == NULL)
1024 reg = of_get_flat_dt_prop(node, "reg", &l);
1025 if (reg == NULL)
1026 return 0;
1027
1028 endp = reg + (l / sizeof(__be32));
Reza Arbab41a9ada2016-12-12 16:43:02 -08001029 hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
Grant Likely51975db2010-02-01 21:34:14 -07001030
Florian Fainellic954b362015-04-12 13:16:26 -07001031 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
Grant Likely51975db2010-02-01 21:34:14 -07001032
1033 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1034 u64 base, size;
1035
1036 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1037 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1038
1039 if (size == 0)
1040 continue;
Geert Uytterhoeven0e407a92021-06-16 11:27:45 +02001041 pr_debug(" - %llx, %llx\n", base, size);
Grant Likely51975db2010-02-01 21:34:14 -07001042
1043 early_init_dt_add_memory_arch(base, size);
Reza Arbab41a9ada2016-12-12 16:43:02 -08001044
1045 if (!hotpluggable)
1046 continue;
1047
Geert Uytterhoeven39c6b3a2021-08-11 10:53:37 +02001048 if (memblock_mark_hotplug(base, size))
Reza Arbab41a9ada2016-12-12 16:43:02 -08001049 pr_warn("failed to mark hotplug range 0x%llx - 0x%llx\n",
1050 base, base + size);
Grant Likely51975db2010-02-01 21:34:14 -07001051 }
1052
1053 return 0;
1054}
1055
Grant Likely86e03222009-12-10 23:42:21 -07001056int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
1057 int depth, void *data)
1058{
Rob Herring9d0c4df2014-04-01 23:49:03 -05001059 int l;
1060 const char *p;
Hsin-Yi Wang428826f2019-08-23 14:24:51 +08001061 const void *rng_seed;
Grant Likely86e03222009-12-10 23:42:21 -07001062
1063 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1064
Grant Likely85f60ae2011-04-29 00:18:16 -06001065 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -07001066 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
1067 return 0;
1068
1069 early_init_dt_check_for_initrd(node);
1070
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001071 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -07001072 p = of_get_flat_dt_prop(node, "bootargs", &l);
1073 if (p != NULL && l > 0)
xiaojiangfengb827bcb2019-04-10 16:29:41 +08001074 strlcpy(data, p, min(l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -07001075
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +00001076 /*
1077 * CONFIG_CMDLINE is meant to be a default in case nothing else
1078 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
1079 * is set in which case we override whatever was found earlier.
1080 */
Grant Likely86e03222009-12-10 23:42:21 -07001081#ifdef CONFIG_CMDLINE
Max Uvarov34b82022016-04-13 12:52:16 +03001082#if defined(CONFIG_CMDLINE_EXTEND)
1083 strlcat(data, " ", COMMAND_LINE_SIZE);
1084 strlcat(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1085#elif defined(CONFIG_CMDLINE_FORCE)
1086 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1087#else
1088 /* No arguments from boot loader, use kernel's cmdl*/
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +00001089 if (!((char *)data)[0])
Grant Likely85f60ae2011-04-29 00:18:16 -06001090 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Max Uvarov34b82022016-04-13 12:52:16 +03001091#endif
Grant Likely86e03222009-12-10 23:42:21 -07001092#endif /* CONFIG_CMDLINE */
1093
tangjianqiang8d3cdfe2020-04-10 16:32:04 +08001094 pr_debug("Command line is: %s\n", (char *)data);
Grant Likely86e03222009-12-10 23:42:21 -07001095
Hsin-Yi Wang428826f2019-08-23 14:24:51 +08001096 rng_seed = of_get_flat_dt_prop(node, "rng-seed", &l);
1097 if (rng_seed && l > 0) {
1098 add_bootloader_randomness(rng_seed, l);
1099
1100 /* try to clear seed so it won't be found. */
1101 fdt_nop_property(initial_boot_params, node, "rng-seed");
Hsin-Yi Wangdd753d92019-08-27 18:33:53 +08001102
1103 /* update CRC check value */
1104 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1105 fdt_totalsize(initial_boot_params));
Hsin-Yi Wang428826f2019-08-23 14:24:51 +08001106 }
1107
Grant Likely86e03222009-12-10 23:42:21 -07001108 /* break now */
1109 return 1;
1110}
1111
Ard Biesheuvel270522a2016-02-16 13:52:32 +01001112#ifndef MIN_MEMBLOCK_ADDR
1113#define MIN_MEMBLOCK_ADDR __pa(PAGE_OFFSET)
1114#endif
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001115#ifndef MAX_MEMBLOCK_ADDR
1116#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
1117#endif
Laura Abbott3069f0c2014-07-07 17:45:43 -07001118
Rob Herring068f6312013-09-24 22:20:01 -05001119void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1120{
Ard Biesheuvel270522a2016-02-16 13:52:32 +01001121 const u64 phys_offset = MIN_MEMBLOCK_ADDR;
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +02001122
Mike Rapoport6072cf52018-10-26 15:04:48 -07001123 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
1124 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1125 base, base + size);
1126 return;
1127 }
1128
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +02001129 if (!PAGE_ALIGNED(base)) {
1130 size -= PAGE_SIZE - (base & ~PAGE_MASK);
1131 base = PAGE_ALIGN(base);
1132 }
Rob Herring068f6312013-09-24 22:20:01 -05001133 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001134
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001135 if (base > MAX_MEMBLOCK_ADDR) {
Kefeng Wange2f04da2019-10-18 11:18:33 +08001136 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1137 base, base + size);
Laura Abbott3069f0c2014-07-07 17:45:43 -07001138 return;
1139 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001140
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001141 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
Kefeng Wange2f04da2019-10-18 11:18:33 +08001142 pr_warn("Ignoring memory range 0x%llx - 0x%llx\n",
1143 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001144 size = MAX_MEMBLOCK_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001145 }
1146
Rob Herring068f6312013-09-24 22:20:01 -05001147 if (base + size < phys_offset) {
Kefeng Wange2f04da2019-10-18 11:18:33 +08001148 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
1149 base, base + size);
Rob Herring068f6312013-09-24 22:20:01 -05001150 return;
1151 }
1152 if (base < phys_offset) {
Kefeng Wange2f04da2019-10-18 11:18:33 +08001153 pr_warn("Ignoring memory range 0x%llx - 0x%llx\n",
1154 base, phys_offset);
Rob Herring068f6312013-09-24 22:20:01 -05001155 size -= phys_offset - base;
1156 base = phys_offset;
1157 }
1158 memblock_add(base, size);
1159}
1160
Rob Herring0fa1c572018-01-05 15:32:33 -06001161static void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
1162{
Mike Rapoport8a7f97b2019-03-11 23:30:31 -07001163 void *ptr = memblock_alloc(size, align);
1164
1165 if (!ptr)
1166 panic("%s: Failed to allocate %llu bytes align=0x%llx\n",
1167 __func__, size, align);
1168
1169 return ptr;
Rob Herring0fa1c572018-01-05 15:32:33 -06001170}
1171
Laura Abbott4972a742014-07-15 10:03:34 -07001172bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -05001173{
1174 if (!params)
1175 return false;
1176
Bjorn Helgaas50ba08f2014-10-29 12:15:00 -06001177 /* check device tree validity */
1178 if (fdt_check_header(params))
1179 return false;
1180
Rob Herring0288ffcb2013-08-26 09:47:40 -05001181 /* Setup flat device-tree pointer */
1182 initial_boot_params = params;
Hsin-Yi Wangdd753d92019-08-27 18:33:53 +08001183 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1184 fdt_totalsize(initial_boot_params));
Laura Abbott4972a742014-07-15 10:03:34 -07001185 return true;
1186}
1187
1188
1189void __init early_init_dt_scan_nodes(void)
1190{
Nick Kossifidise1e52542018-11-10 02:53:17 +02001191 int rc = 0;
1192
Rob Herring0288ffcb2013-08-26 09:47:40 -05001193 /* Retrieve various information from the /chosen node */
Nick Kossifidise1e52542018-11-10 02:53:17 +02001194 rc = of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1195 if (!rc)
1196 pr_warn("No chosen node found, continuing without\n");
Rob Herring0288ffcb2013-08-26 09:47:40 -05001197
1198 /* Initialize {size,address}-cells info */
1199 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1200
1201 /* Setup memory, calling early_init_dt_add_memory_arch */
1202 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001203}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001204
Laura Abbott4972a742014-07-15 10:03:34 -07001205bool __init early_init_dt_scan(void *params)
1206{
1207 bool status;
1208
1209 status = early_init_dt_verify(params);
1210 if (!status)
1211 return false;
1212
1213 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001214 return true;
1215}
1216
Grant Likelyf00abd92009-11-24 03:27:10 -07001217/**
Grant Likely41f88002009-11-23 20:07:01 -07001218 * unflatten_device_tree - create tree of device_nodes from flat blob
1219 *
1220 * unflattens the device-tree passed by the firmware, creating the
1221 * tree of struct device_node. It also fills the "name" and "type"
1222 * pointers of the nodes so the normal device-tree walking functions
1223 * can be used.
1224 */
1225void __init unflatten_device_tree(void)
1226{
Gavin Shanc4263232016-05-03 23:22:50 +10001227 __unflatten_device_tree(initial_boot_params, NULL, &of_root,
Michal Suchanek1d1bde52016-07-19 00:01:12 +02001228 early_init_dt_alloc_memory_arch, false);
Grant Likely41f88002009-11-23 20:07:01 -07001229
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001230 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001231 of_alias_scan(early_init_dt_alloc_memory_arch);
Frank Rowand81d0848f2017-04-25 17:09:54 -07001232
1233 unittest_unflatten_overlay_base();
Grant Likely41f88002009-11-23 20:07:01 -07001234}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001235
Rob Herringa8bf7522013-08-26 11:22:45 -05001236/**
1237 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1238 *
1239 * Copies and unflattens the device-tree passed by the firmware, creating the
1240 * tree of struct device_node. It also fills the "name" and "type"
1241 * pointers of the nodes so the normal device-tree walking functions
1242 * can be used. This should only be used when the FDT memory has not been
1243 * reserved such is the case when the FDT is built-in to the kernel init
1244 * section. If the FDT memory is reserved already then unflatten_device_tree
1245 * should be used instead.
1246 */
1247void __init unflatten_and_copy_device_tree(void)
1248{
James Hogan6f041e92013-11-21 13:44:14 +00001249 int size;
1250 void *dt;
1251
1252 if (!initial_boot_params) {
1253 pr_warn("No valid device tree found, continuing without\n");
1254 return;
1255 }
1256
Rob Herringc972de12014-04-01 22:48:01 -05001257 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001258 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001259 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001260
1261 if (dt) {
1262 memcpy(dt, initial_boot_params, size);
1263 initial_boot_params = dt;
1264 }
1265 unflatten_device_tree();
1266}
1267
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001268#ifdef CONFIG_SYSFS
1269static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1270 struct bin_attribute *bin_attr,
1271 char *buf, loff_t off, size_t count)
Rob Herringb0a6fb32014-04-02 16:56:48 -05001272{
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001273 memcpy(buf, initial_boot_params + off, count);
1274 return count;
Rob Herringb0a6fb32014-04-02 16:56:48 -05001275}
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001276
1277static int __init of_fdt_raw_init(void)
1278{
1279 static struct bin_attribute of_fdt_raw_attr =
1280 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1281
1282 if (!initial_boot_params)
1283 return 0;
1284
1285 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1286 fdt_totalsize(initial_boot_params))) {
Rob Herring606ad422016-06-15 08:32:18 -05001287 pr_warn("not creating '/sys/firmware/fdt': CRC check failed\n");
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001288 return 0;
1289 }
1290 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1291 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1292}
1293late_initcall(of_fdt_raw_init);
Rob Herringb0a6fb32014-04-02 16:56:48 -05001294#endif
1295
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001296#endif /* CONFIG_OF_EARLY_FLATTREE */