blob: cb19adfb3933bca3de8c7053aef8f275f3b49da3 [file] [log] [blame]
Grant Likelye169cfb2009-11-23 14:53:09 -07001/*
2 * Functions for working with the Flattened Device Tree data format
3 *
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5 * benh@kernel.crashing.org
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 */
11
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010012#include <linux/crc32.h>
Grant Likely41f88002009-11-23 20:07:01 -070013#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070014#include <linux/initrd.h>
Grant Likelya1727da2013-08-28 21:18:32 +010015#include <linux/memblock.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070016#include <linux/of.h>
17#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010018#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +010019#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070020#include <linux/string.h>
21#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080022#include <linux/slab.h>
Rob Herringe6a69282014-04-02 15:10:14 -050023#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050024#include <linux/debugfs.h>
Rob Herringfb11ffe2014-03-27 08:07:01 -050025#include <linux/serial_core.h>
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010026#include <linux/sysfs.h>
Grant Likely51975db2010-02-01 21:34:14 -070027
Fabio Estevamc89810a2012-01-02 14:19:03 -020028#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070029#include <asm/page.h>
30
Laura Abbott704033c2014-07-15 10:03:35 -070031/*
32 * of_fdt_limit_memory - limit the number of regions in the /memory node
33 * @limit: maximum entries
34 *
35 * Adjust the flattened device tree to have at most 'limit' number of
36 * memory entries in the /memory node. This function may be called
37 * any time after initial_boot_param is set.
38 */
39void of_fdt_limit_memory(int limit)
40{
41 int memory;
42 int len;
43 const void *val;
44 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
45 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
46 const uint32_t *addr_prop;
47 const uint32_t *size_prop;
48 int root_offset;
49 int cell_size;
50
51 root_offset = fdt_path_offset(initial_boot_params, "/");
52 if (root_offset < 0)
53 return;
54
55 addr_prop = fdt_getprop(initial_boot_params, root_offset,
56 "#address-cells", NULL);
57 if (addr_prop)
58 nr_address_cells = fdt32_to_cpu(*addr_prop);
59
60 size_prop = fdt_getprop(initial_boot_params, root_offset,
61 "#size-cells", NULL);
62 if (size_prop)
63 nr_size_cells = fdt32_to_cpu(*size_prop);
64
65 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
66
67 memory = fdt_path_offset(initial_boot_params, "/memory");
68 if (memory > 0) {
69 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
70 if (len > limit*cell_size) {
71 len = limit*cell_size;
72 pr_debug("Limiting number of entries to %d\n", limit);
73 fdt_setprop(initial_boot_params, memory, "reg", val,
74 len);
75 }
76 }
77}
78
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080079/**
80 * of_fdt_is_compatible - Return true if given node from the given blob has
81 * compat in its compatible list
82 * @blob: A device tree blob
83 * @node: node to test
84 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040085 *
86 * On match, returns a non-zero value with smaller values returned for more
87 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080088 */
Rob Herringc972de12014-04-01 22:48:01 -050089int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080090 unsigned long node, const char *compat)
91{
92 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050093 int cplen;
94 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080095
Rob Herringe6a69282014-04-02 15:10:14 -050096 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080097 if (cp == NULL)
98 return 0;
99 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -0400100 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800101 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -0400102 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800103 l = strlen(cp) + 1;
104 cp += l;
105 cplen -= l;
106 }
107
108 return 0;
109}
110
Grant Likelya4f740c2010-10-30 11:49:09 -0400111/**
112 * of_fdt_match - Return true if node matches a list of compatible values
113 */
Rob Herringc972de12014-04-01 22:48:01 -0500114int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100115 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400116{
117 unsigned int tmp, score = 0;
118
119 if (!compat)
120 return 0;
121
122 while (*compat) {
123 tmp = of_fdt_is_compatible(blob, node, *compat);
124 if (tmp && (score == 0 || (tmp < score)))
125 score = tmp;
126 compat++;
127 }
128
129 return score;
130}
131
Grant Likely44856812013-08-29 13:30:35 +0100132static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700133 unsigned long align)
134{
135 void *res;
136
Grant Likely44856812013-08-29 13:30:35 +0100137 *mem = PTR_ALIGN(*mem, align);
138 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700139 *mem += size;
140
141 return res;
142}
143
144/**
145 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800146 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700147 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -0700148 * @p: pointer to node in flat tree
149 * @dad: Parent struct device_node
Grant Likelybbd33932009-11-23 20:07:00 -0700150 * @fpsize: Size of the node path up at the current depth.
151 */
Rob Herringc972de12014-04-01 22:48:01 -0500152static void * unflatten_dt_node(void *blob,
Grant Likely44856812013-08-29 13:30:35 +0100153 void *mem,
Rob Herringe6a69282014-04-02 15:10:14 -0500154 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800155 struct device_node *dad,
Grant Likely5063e252014-10-03 16:28:27 +0100156 struct device_node **nodepp,
157 unsigned long fpsize,
158 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700159{
Rob Herringe6a69282014-04-02 15:10:14 -0500160 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700161 struct device_node *np;
162 struct property *pp, **prev_pp = NULL;
Rob Herringe6a69282014-04-02 15:10:14 -0500163 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700164 unsigned int l, allocl;
Rob Herringe6a69282014-04-02 15:10:14 -0500165 static int depth = 0;
166 int old_depth;
167 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700168 int has_name = 0;
169 int new_format = 0;
170
Rob Herringe6a69282014-04-02 15:10:14 -0500171 pathp = fdt_get_name(blob, *poffset, &l);
172 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700173 return mem;
Rob Herringe6a69282014-04-02 15:10:14 -0500174
175 allocl = l++;
Grant Likelybbd33932009-11-23 20:07:00 -0700176
177 /* version 0x10 has a more compact unit name here instead of the full
178 * path. we accumulate the full path size using "fpsize", we'll rebuild
179 * it later. We detect this because the first character of the name is
180 * not '/'.
181 */
182 if ((*pathp) != '/') {
183 new_format = 1;
184 if (fpsize == 0) {
185 /* root node: special case. fpsize accounts for path
186 * plus terminating zero. root node only has '/', so
187 * fpsize should be 2, but we want to avoid the first
188 * level nodes to have two '/' so we use fpsize 1 here
189 */
190 fpsize = 1;
191 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000192 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500193 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700194 } else {
195 /* account for '/' and path size minus terminal 0
196 * already in 'l'
197 */
198 fpsize += l;
199 allocl = fpsize;
200 }
201 }
202
203 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
204 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100205 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000206 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200207 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000208 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700209 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700210 /* rebuild full path for new format */
211 if (dad && dad->parent) {
212 strcpy(fn, dad->full_name);
213#ifdef DEBUG
214 if ((strlen(fn) + l + 1) != allocl) {
215 pr_debug("%s: p: %d, l: %d, a: %d\n",
216 pathp, (int)strlen(fn),
217 l, allocl);
218 }
219#endif
220 fn += strlen(fn);
221 }
222 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000223 }
224 memcpy(fn, pathp, l);
225
Grant Likelybbd33932009-11-23 20:07:00 -0700226 prev_pp = &np->properties;
Grant Likelybbd33932009-11-23 20:07:00 -0700227 if (dad != NULL) {
228 np->parent = dad;
229 /* we temporarily use the next field as `last_child'*/
230 if (dad->next == NULL)
231 dad->child = np;
232 else
233 dad->next->sibling = np;
234 dad->next = np;
235 }
Grant Likelybbd33932009-11-23 20:07:00 -0700236 }
Andres Salomona7006c92011-03-17 17:32:35 -0700237 /* process properties */
Rob Herringe6a69282014-04-02 15:10:14 -0500238 for (offset = fdt_first_property_offset(blob, *poffset);
239 (offset >= 0);
240 (offset = fdt_next_property_offset(blob, offset))) {
241 const char *pname;
242 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700243
Rob Herringe6a69282014-04-02 15:10:14 -0500244 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
245 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700246 break;
Rob Herringe6a69282014-04-02 15:10:14 -0500247 }
Grant Likelybbd33932009-11-23 20:07:00 -0700248
Grant Likelybbd33932009-11-23 20:07:00 -0700249 if (pname == NULL) {
250 pr_info("Can't find property name in list !\n");
251 break;
252 }
253 if (strcmp(pname, "name") == 0)
254 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700255 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
256 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100257 if (!dryrun) {
David Gibson04b954a2010-02-01 21:34:15 -0700258 /* We accept flattened tree phandles either in
259 * ePAPR-style "phandle" properties, or the
260 * legacy "linux,phandle" properties. If both
261 * appear and have different values, things
262 * will get weird. Don't do that. */
263 if ((strcmp(pname, "phandle") == 0) ||
264 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700265 if (np->phandle == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500266 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700267 }
David Gibson04b954a2010-02-01 21:34:15 -0700268 /* And we process the "ibm,phandle" property
269 * used in pSeries dynamic device tree
270 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700271 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500272 np->phandle = be32_to_cpup(p);
273 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700274 pp->length = sz;
Rob Herringe6a69282014-04-02 15:10:14 -0500275 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700276 *prev_pp = pp;
277 prev_pp = &pp->next;
278 }
Grant Likelybbd33932009-11-23 20:07:00 -0700279 }
280 /* with version 0x10 we may not have the name property, recreate
281 * it here from the unit name if absent
282 */
283 if (!has_name) {
Rob Herringe6a69282014-04-02 15:10:14 -0500284 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700285 int sz;
286
287 while (*p1) {
288 if ((*p1) == '@')
289 pa = p1;
290 if ((*p1) == '/')
291 ps = p1 + 1;
292 p1++;
293 }
294 if (pa < ps)
295 pa = p1;
296 sz = (pa - ps) + 1;
297 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
298 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100299 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700300 pp->name = "name";
301 pp->length = sz;
302 pp->value = pp + 1;
303 *prev_pp = pp;
304 prev_pp = &pp->next;
305 memcpy(pp->value, ps, sz - 1);
306 ((char *)pp->value)[sz - 1] = 0;
307 pr_debug("fixed up name for %s -> %s\n", pathp,
308 (char *)pp->value);
309 }
310 }
Grant Likely5063e252014-10-03 16:28:27 +0100311 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700312 *prev_pp = NULL;
313 np->name = of_get_property(np, "name", NULL);
314 np->type = of_get_property(np, "device_type", NULL);
315
316 if (!np->name)
317 np->name = "<NULL>";
318 if (!np->type)
319 np->type = "<NULL>";
320 }
Rob Herringe6a69282014-04-02 15:10:14 -0500321
322 old_depth = depth;
323 *poffset = fdt_next_node(blob, *poffset, &depth);
324 if (depth < 0)
325 depth = 0;
326 while (*poffset > 0 && depth > old_depth)
Grant Likely5063e252014-10-03 16:28:27 +0100327 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
328 fpsize, dryrun);
Rob Herringe6a69282014-04-02 15:10:14 -0500329
330 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
331 pr_err("unflatten: error %d processing FDT\n", *poffset);
Grant Likely5063e252014-10-03 16:28:27 +0100332 if (nodepp)
333 *nodepp = np;
Rob Herringe6a69282014-04-02 15:10:14 -0500334
Grant Likelybbd33932009-11-23 20:07:00 -0700335 return mem;
336}
Grant Likely41f88002009-11-23 20:07:01 -0700337
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800338/**
339 * __unflatten_device_tree - create tree of device_nodes from flat blob
340 *
341 * unflattens a device-tree, creating the
342 * tree of struct device_node. It also fills the "name" and "type"
343 * pointers of the nodes so the normal device-tree walking functions
344 * can be used.
345 * @blob: The blob to expand
346 * @mynodes: The device_node tree created by the call
347 * @dt_alloc: An allocator that provides a virtual address to memory
348 * for the resulting tree
349 */
Rob Herringc972de12014-04-01 22:48:01 -0500350static void __unflatten_device_tree(void *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800351 struct device_node **mynodes,
352 void * (*dt_alloc)(u64 size, u64 align))
353{
Grant Likely44856812013-08-29 13:30:35 +0100354 unsigned long size;
Rob Herringe6a69282014-04-02 15:10:14 -0500355 int start;
356 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800357
358 pr_debug(" -> unflatten_device_tree()\n");
359
360 if (!blob) {
361 pr_debug("No device tree pointer\n");
362 return;
363 }
364
365 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500366 pr_debug("magic: %08x\n", fdt_magic(blob));
367 pr_debug("size: %08x\n", fdt_totalsize(blob));
368 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800369
Rob Herringc972de12014-04-01 22:48:01 -0500370 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800371 pr_err("Invalid device tree blob header\n");
372 return;
373 }
374
375 /* First pass, scan for size */
Rob Herringe6a69282014-04-02 15:10:14 -0500376 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100377 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
Grant Likely44856812013-08-29 13:30:35 +0100378 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800379
380 pr_debug(" size is %lx, allocating...\n", size);
381
382 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100383 mem = dt_alloc(size + 4, __alignof__(struct device_node));
384 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800385
Grant Likely44856812013-08-29 13:30:35 +0100386 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200387
Grant Likely44856812013-08-29 13:30:35 +0100388 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800389
390 /* Second pass, do actual unflattening */
Rob Herringe6a69282014-04-02 15:10:14 -0500391 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100392 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
Grant Likely44856812013-08-29 13:30:35 +0100393 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800394 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100395 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800396
397 pr_debug(" <- unflatten_device_tree()\n");
398}
399
400static void *kernel_tree_alloc(u64 size, u64 align)
401{
402 return kzalloc(size, GFP_KERNEL);
403}
404
405/**
406 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
407 *
408 * unflattens the device-tree passed by the firmware, creating the
409 * tree of struct device_node. It also fills the "name" and "type"
410 * pointers of the nodes so the normal device-tree walking functions
411 * can be used.
412 */
413void of_fdt_unflatten_tree(unsigned long *blob,
414 struct device_node **mynodes)
415{
Rob Herringc972de12014-04-01 22:48:01 -0500416 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800417}
418EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
419
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800420/* Everything below here references initial_boot_params directly. */
421int __initdata dt_root_addr_cells;
422int __initdata dt_root_size_cells;
423
Rob Herring1daa0c42014-03-31 15:25:04 -0500424void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800425
426#ifdef CONFIG_OF_EARLY_FLATTREE
427
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +0100428static u32 of_fdt_crc32;
429
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800430/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100431 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
432 */
433static int __init __reserved_mem_reserve_reg(unsigned long node,
434 const char *uname)
435{
436 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
437 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500438 int len;
439 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100440 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100441
442 prop = of_get_flat_dt_prop(node, "reg", &len);
443 if (!prop)
444 return -ENOENT;
445
446 if (len && len % t_len != 0) {
447 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
448 uname);
449 return -EINVAL;
450 }
451
452 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
453
454 while (len >= t_len) {
455 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
456 size = dt_mem_next_cell(dt_root_size_cells, &prop);
457
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400458 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100459 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
460 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
461 uname, &base, (unsigned long)size / SZ_1M);
462 else
463 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
464 uname, &base, (unsigned long)size / SZ_1M);
465
466 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100467 if (first) {
468 fdt_reserved_mem_save_node(node, uname, base, size);
469 first = 0;
470 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100471 }
472 return 0;
473}
474
475/**
476 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
477 * in /reserved-memory matches the values supported by the current implementation,
478 * also check if ranges property has been provided
479 */
Xiubo Li5b624112014-04-08 13:48:07 +0800480static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100481{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500482 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100483
484 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
485 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
486 return -EINVAL;
487
488 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
489 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
490 return -EINVAL;
491
492 prop = of_get_flat_dt_prop(node, "ranges", NULL);
493 if (!prop)
494 return -EINVAL;
495 return 0;
496}
497
498/**
499 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
500 */
501static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
502 int depth, void *data)
503{
504 static int found;
505 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100506 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100507
508 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
509 if (__reserved_mem_check_root(node) != 0) {
510 pr_err("Reserved memory: unsupported node format, ignoring\n");
511 /* break scan */
512 return 1;
513 }
514 found = 1;
515 /* scan next node */
516 return 0;
517 } else if (!found) {
518 /* scan next node */
519 return 0;
520 } else if (found && depth < 2) {
521 /* scanning of /reserved-memory has been finished */
522 return 1;
523 }
524
525 status = of_get_flat_dt_prop(node, "status", NULL);
526 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
527 return 0;
528
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100529 err = __reserved_mem_reserve_reg(node, uname);
530 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
531 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100532
533 /* scan next node */
534 return 0;
535}
536
537/**
538 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
539 *
540 * This function grabs memory from early allocator for device exclusive use
541 * defined in device tree structures. It should be called by arch specific code
542 * once the early allocator (i.e. memblock) has been fully activated.
543 */
544void __init early_init_fdt_scan_reserved_mem(void)
545{
Rob Herringd1552ce2014-04-01 22:46:48 -0500546 int n;
547 u64 base, size;
548
Josh Cartwright2040b522014-03-13 16:36:36 -0500549 if (!initial_boot_params)
550 return;
551
Rob Herringd1552ce2014-04-01 22:46:48 -0500552 /* Reserve the dtb region */
553 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
554 fdt_totalsize(initial_boot_params),
555 0);
556
557 /* Process header /memreserve/ fields */
558 for (n = 0; ; n++) {
559 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
560 if (!size)
561 break;
562 early_init_dt_reserve_memory_arch(base, size, 0);
563 }
564
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100565 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100566 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100567}
568
569/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800570 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
571 * @it: callback function
572 * @data: context data pointer
573 *
574 * This function is used to scan the flattened device-tree, it is
575 * used to extract the memory information at boot before we can
576 * unflatten the tree
577 */
578int __init of_scan_flat_dt(int (*it)(unsigned long node,
579 const char *uname, int depth,
580 void *data),
581 void *data)
582{
Rob Herringe6a69282014-04-02 15:10:14 -0500583 const void *blob = initial_boot_params;
584 const char *pathp;
585 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800586
Rob Herringe6a69282014-04-02 15:10:14 -0500587 for (offset = fdt_next_node(blob, -1, &depth);
588 offset >= 0 && depth >= 0 && !rc;
589 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800590
Rob Herringe6a69282014-04-02 15:10:14 -0500591 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800592 if (*pathp == '/')
593 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500594 rc = it(offset, pathp, depth, data);
595 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800596 return rc;
597}
598
599/**
600 * of_get_flat_dt_root - find the root node in the flat blob
601 */
602unsigned long __init of_get_flat_dt_root(void)
603{
Rob Herringe6a69282014-04-02 15:10:14 -0500604 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800605}
606
607/**
Rob Herringc0556d32014-04-22 12:55:10 -0500608 * of_get_flat_dt_size - Return the total size of the FDT
609 */
610int __init of_get_flat_dt_size(void)
611{
612 return fdt_totalsize(initial_boot_params);
613}
614
615/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800616 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
617 *
618 * This function can be used within scan_flattened_dt callback to get
619 * access to properties
620 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500621const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
622 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800623{
Rob Herringe6a69282014-04-02 15:10:14 -0500624 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800625}
626
627/**
628 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
629 * @node: node to test
630 * @compat: compatible string to compare with compatible list.
631 */
632int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
633{
634 return of_fdt_is_compatible(initial_boot_params, node, compat);
635}
636
Grant Likelya4f740c2010-10-30 11:49:09 -0400637/**
638 * of_flat_dt_match - Return true if node matches a list of compatible values
639 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100640int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400641{
642 return of_fdt_match(initial_boot_params, node, compat);
643}
644
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200645struct fdt_scan_status {
646 const char *name;
647 int namelen;
648 int depth;
649 int found;
650 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
651 void *data;
652};
653
Rob Herring6a903a22013-08-27 21:41:56 -0500654const char * __init of_flat_dt_get_machine_name(void)
655{
656 const char *name;
657 unsigned long dt_root = of_get_flat_dt_root();
658
659 name = of_get_flat_dt_prop(dt_root, "model", NULL);
660 if (!name)
661 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
662 return name;
663}
664
665/**
666 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
667 *
668 * @default_match: A machine specific ptr to return in case of no match.
669 * @get_next_compat: callback function to return next compatible match table.
670 *
671 * Iterate through machine match tables to find the best match for the machine
672 * compatible string in the FDT.
673 */
674const void * __init of_flat_dt_match_machine(const void *default_match,
675 const void * (*get_next_compat)(const char * const**))
676{
677 const void *data = NULL;
678 const void *best_data = default_match;
679 const char *const *compat;
680 unsigned long dt_root;
681 unsigned int best_score = ~1, score = 0;
682
683 dt_root = of_get_flat_dt_root();
684 while ((data = get_next_compat(&compat))) {
685 score = of_flat_dt_match(dt_root, compat);
686 if (score > 0 && score < best_score) {
687 best_data = data;
688 best_score = score;
689 }
690 }
691 if (!best_data) {
692 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500693 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500694
695 pr_err("\n unrecognized device tree list:\n[ ");
696
697 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
698 if (prop) {
699 while (size > 0) {
700 printk("'%s' ", prop);
701 size -= strlen(prop) + 1;
702 prop += strlen(prop) + 1;
703 }
704 }
705 printk("]\n\n");
706 return NULL;
707 }
708
709 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
710
711 return best_data;
712}
713
Grant Likelyf7b3a832009-11-24 03:26:58 -0700714#ifdef CONFIG_BLK_DEV_INITRD
715/**
716 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
717 * @node: reference to node containing initrd location ('chosen')
718 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500719static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700720{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400721 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500722 int len;
723 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700724
725 pr_debug("Looking for initrd properties... ");
726
727 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700728 if (!prop)
729 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400730 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700731
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700732 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
733 if (!prop)
734 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400735 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700736
Rob Herring29eb45a2013-08-30 17:06:53 -0500737 initrd_start = (unsigned long)__va(start);
738 initrd_end = (unsigned long)__va(end);
739 initrd_below_start_ok = 1;
740
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400741 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
742 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700743}
744#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500745static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700746{
747}
748#endif /* CONFIG_BLK_DEV_INITRD */
749
Rob Herringfb11ffe2014-03-27 08:07:01 -0500750#ifdef CONFIG_SERIAL_EARLYCON
751extern struct of_device_id __earlycon_of_table[];
752
753int __init early_init_dt_scan_chosen_serial(void)
754{
755 int offset;
756 const char *p;
757 int l;
758 const struct of_device_id *match = __earlycon_of_table;
759 const void *fdt = initial_boot_params;
760
761 offset = fdt_path_offset(fdt, "/chosen");
762 if (offset < 0)
763 offset = fdt_path_offset(fdt, "/chosen@0");
764 if (offset < 0)
765 return -ENOENT;
766
767 p = fdt_getprop(fdt, offset, "stdout-path", &l);
768 if (!p)
769 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
770 if (!p || !l)
771 return -ENOENT;
772
773 /* Get the node specified by stdout-path */
774 offset = fdt_path_offset(fdt, p);
775 if (offset < 0)
776 return -ENODEV;
777
778 while (match->compatible) {
779 unsigned long addr;
780 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
781 match++;
782 continue;
783 }
784
785 addr = fdt_translate_address(fdt, offset);
786 if (!addr)
787 return -ENXIO;
788
789 of_setup_earlycon(addr, match->data);
790 return 0;
791 }
792 return -ENODEV;
793}
794
795static int __init setup_of_earlycon(char *buf)
796{
797 if (buf)
798 return 0;
799
800 return early_init_dt_scan_chosen_serial();
801}
802early_param("earlycon", setup_of_earlycon);
803#endif
804
Grant Likely41f88002009-11-23 20:07:01 -0700805/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700806 * early_init_dt_scan_root - fetch the top level address and size cells
807 */
808int __init early_init_dt_scan_root(unsigned long node, const char *uname,
809 int depth, void *data)
810{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500811 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700812
813 if (depth != 0)
814 return 0;
815
Jeremy Kerr33714882010-01-30 01:45:26 -0700816 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
817 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
818
Grant Likelyf00abd92009-11-24 03:27:10 -0700819 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700820 if (prop)
821 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700822 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
823
824 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700825 if (prop)
826 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700827 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
828
829 /* break now */
830 return 1;
831}
832
Rob Herring9d0c4df2014-04-01 23:49:03 -0500833u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700834{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500835 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700836
837 *cellp = p + s;
838 return of_read_number(p, s);
839}
840
Grant Likely51975db2010-02-01 21:34:14 -0700841/**
842 * early_init_dt_scan_memory - Look for an parse memory nodes
843 */
844int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
845 int depth, void *data)
846{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500847 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
848 const __be32 *reg, *endp;
849 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700850
851 /* We are scanning "memory" nodes only */
852 if (type == NULL) {
853 /*
854 * The longtrail doesn't have a device_type on the
855 * /memory node, so look for the node called /memory@0.
856 */
Leif Lindholmb44aa252014-04-17 18:42:01 +0100857 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
Grant Likely51975db2010-02-01 21:34:14 -0700858 return 0;
859 } else if (strcmp(type, "memory") != 0)
860 return 0;
861
862 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
863 if (reg == NULL)
864 reg = of_get_flat_dt_prop(node, "reg", &l);
865 if (reg == NULL)
866 return 0;
867
868 endp = reg + (l / sizeof(__be32));
869
Rob Herring9d0c4df2014-04-01 23:49:03 -0500870 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700871 uname, l, reg[0], reg[1], reg[2], reg[3]);
872
873 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
874 u64 base, size;
875
876 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
877 size = dt_mem_next_cell(dt_root_size_cells, &reg);
878
879 if (size == 0)
880 continue;
881 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
882 (unsigned long long)size);
883
884 early_init_dt_add_memory_arch(base, size);
885 }
886
887 return 0;
888}
889
Grant Likely86e03222009-12-10 23:42:21 -0700890int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
891 int depth, void *data)
892{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500893 int l;
894 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700895
896 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
897
Grant Likely85f60ae2011-04-29 00:18:16 -0600898 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700899 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
900 return 0;
901
902 early_init_dt_check_for_initrd(node);
903
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300904 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700905 p = of_get_flat_dt_prop(node, "bootargs", &l);
906 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600907 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700908
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000909 /*
910 * CONFIG_CMDLINE is meant to be a default in case nothing else
911 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
912 * is set in which case we override whatever was found earlier.
913 */
Grant Likely86e03222009-12-10 23:42:21 -0700914#ifdef CONFIG_CMDLINE
915#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000916 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700917#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600918 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700919#endif /* CONFIG_CMDLINE */
920
Grant Likely85f60ae2011-04-29 00:18:16 -0600921 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700922
923 /* break now */
924 return 1;
925}
926
Grant Likelya1727da2013-08-28 21:18:32 +0100927#ifdef CONFIG_HAVE_MEMBLOCK
Laura Abbott3069f0c2014-07-07 17:45:43 -0700928#define MAX_PHYS_ADDR ((phys_addr_t)~0)
929
Rob Herring068f6312013-09-24 22:20:01 -0500930void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
931{
932 const u64 phys_offset = __pa(PAGE_OFFSET);
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200933
934 if (!PAGE_ALIGNED(base)) {
Ard Biesheuvel8cccffc2014-10-29 17:09:32 +0100935 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
936 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
937 base, base + size);
938 return;
939 }
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200940 size -= PAGE_SIZE - (base & ~PAGE_MASK);
941 base = PAGE_ALIGN(base);
942 }
Rob Herring068f6312013-09-24 22:20:01 -0500943 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700944
Laura Abbott3069f0c2014-07-07 17:45:43 -0700945 if (base > MAX_PHYS_ADDR) {
946 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
947 base, base + size);
948 return;
949 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700950
Srinivas Kandagatla9aacd602014-09-23 10:59:09 +0100951 if (base + size - 1 > MAX_PHYS_ADDR) {
952 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
953 ((u64)MAX_PHYS_ADDR) + 1, base + size);
954 size = MAX_PHYS_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700955 }
956
Rob Herring068f6312013-09-24 22:20:01 -0500957 if (base + size < phys_offset) {
958 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
959 base, base + size);
960 return;
961 }
962 if (base < phys_offset) {
963 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
964 base, phys_offset);
965 size -= phys_offset - base;
966 base = phys_offset;
967 }
968 memblock_add(base, size);
969}
970
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100971int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
972 phys_addr_t size, bool nomap)
973{
974 if (memblock_is_region_reserved(base, size))
975 return -EBUSY;
976 if (nomap)
977 return memblock_remove(base, size);
978 return memblock_reserve(base, size);
979}
980
Grant Likelya1727da2013-08-28 21:18:32 +0100981/*
982 * called from unflatten_device_tree() to bootstrap devicetree itself
983 * Architectures can override this definition if memblock isn't used
984 */
985void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
986{
987 return __va(memblock_alloc(size, align));
988}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100989#else
990int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
991 phys_addr_t size, bool nomap)
992{
Rob Herring1d1a6612014-04-22 12:50:24 -0500993 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
994 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100995 return -ENOSYS;
996}
Grant Likelya1727da2013-08-28 21:18:32 +0100997#endif
998
Laura Abbott4972a742014-07-15 10:03:34 -0700999bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -05001000{
1001 if (!params)
1002 return false;
1003
Bjorn Helgaas50ba08f2014-10-29 12:15:00 -06001004 /* check device tree validity */
1005 if (fdt_check_header(params))
1006 return false;
1007
Rob Herring0288ffcb2013-08-26 09:47:40 -05001008 /* Setup flat device-tree pointer */
1009 initial_boot_params = params;
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001010 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1011 fdt_totalsize(initial_boot_params));
Laura Abbott4972a742014-07-15 10:03:34 -07001012 return true;
1013}
1014
1015
1016void __init early_init_dt_scan_nodes(void)
1017{
Rob Herring0288ffcb2013-08-26 09:47:40 -05001018 /* Retrieve various information from the /chosen node */
1019 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1020
1021 /* Initialize {size,address}-cells info */
1022 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1023
1024 /* Setup memory, calling early_init_dt_add_memory_arch */
1025 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001026}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001027
Laura Abbott4972a742014-07-15 10:03:34 -07001028bool __init early_init_dt_scan(void *params)
1029{
1030 bool status;
1031
1032 status = early_init_dt_verify(params);
1033 if (!status)
1034 return false;
1035
1036 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001037 return true;
1038}
1039
Grant Likelyf00abd92009-11-24 03:27:10 -07001040/**
Grant Likely41f88002009-11-23 20:07:01 -07001041 * unflatten_device_tree - create tree of device_nodes from flat blob
1042 *
1043 * unflattens the device-tree passed by the firmware, creating the
1044 * tree of struct device_node. It also fills the "name" and "type"
1045 * pointers of the nodes so the normal device-tree walking functions
1046 * can be used.
1047 */
1048void __init unflatten_device_tree(void)
1049{
Grant Likely5063e252014-10-03 16:28:27 +01001050 __unflatten_device_tree(initial_boot_params, &of_root,
Grant Likely672c5442011-01-13 15:36:09 -07001051 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001052
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001053 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001054 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001055}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001056
Rob Herringa8bf7522013-08-26 11:22:45 -05001057/**
1058 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1059 *
1060 * Copies and unflattens the device-tree passed by the firmware, creating the
1061 * tree of struct device_node. It also fills the "name" and "type"
1062 * pointers of the nodes so the normal device-tree walking functions
1063 * can be used. This should only be used when the FDT memory has not been
1064 * reserved such is the case when the FDT is built-in to the kernel init
1065 * section. If the FDT memory is reserved already then unflatten_device_tree
1066 * should be used instead.
1067 */
1068void __init unflatten_and_copy_device_tree(void)
1069{
James Hogan6f041e92013-11-21 13:44:14 +00001070 int size;
1071 void *dt;
1072
1073 if (!initial_boot_params) {
1074 pr_warn("No valid device tree found, continuing without\n");
1075 return;
1076 }
1077
Rob Herringc972de12014-04-01 22:48:01 -05001078 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001079 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001080 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001081
1082 if (dt) {
1083 memcpy(dt, initial_boot_params, size);
1084 initial_boot_params = dt;
1085 }
1086 unflatten_device_tree();
1087}
1088
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001089#ifdef CONFIG_SYSFS
1090static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1091 struct bin_attribute *bin_attr,
1092 char *buf, loff_t off, size_t count)
Rob Herringb0a6fb32014-04-02 16:56:48 -05001093{
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001094 memcpy(buf, initial_boot_params + off, count);
1095 return count;
Rob Herringb0a6fb32014-04-02 16:56:48 -05001096}
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001097
1098static int __init of_fdt_raw_init(void)
1099{
1100 static struct bin_attribute of_fdt_raw_attr =
1101 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1102
1103 if (!initial_boot_params)
1104 return 0;
1105
1106 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1107 fdt_totalsize(initial_boot_params))) {
1108 pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
1109 return 0;
1110 }
1111 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1112 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1113}
1114late_initcall(of_fdt_raw_init);
Rob Herringb0a6fb32014-04-02 16:56:48 -05001115#endif
1116
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001117#endif /* CONFIG_OF_EARLY_FLATTREE */