blob: 9fc3568302265811c5a0ae37ec6a245179ec11df [file] [log] [blame]
Grant Likelye169cfb2009-11-23 14:53:09 -07001/*
2 * Functions for working with the Flattened Device Tree data format
3 *
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5 * benh@kernel.crashing.org
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 */
11
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010012#include <linux/crc32.h>
Grant Likely41f88002009-11-23 20:07:01 -070013#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070014#include <linux/initrd.h>
Grant Likelya1727da2013-08-28 21:18:32 +010015#include <linux/memblock.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070016#include <linux/of.h>
17#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010018#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +010019#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070020#include <linux/string.h>
21#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080022#include <linux/slab.h>
Rob Herringe6a69282014-04-02 15:10:14 -050023#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050024#include <linux/debugfs.h>
Rob Herringfb11ffe2014-03-27 08:07:01 -050025#include <linux/serial_core.h>
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +010026#include <linux/sysfs.h>
Grant Likely51975db2010-02-01 21:34:14 -070027
Fabio Estevamc89810a2012-01-02 14:19:03 -020028#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070029#include <asm/page.h>
30
Laura Abbott704033c2014-07-15 10:03:35 -070031/*
32 * of_fdt_limit_memory - limit the number of regions in the /memory node
33 * @limit: maximum entries
34 *
35 * Adjust the flattened device tree to have at most 'limit' number of
36 * memory entries in the /memory node. This function may be called
37 * any time after initial_boot_param is set.
38 */
39void of_fdt_limit_memory(int limit)
40{
41 int memory;
42 int len;
43 const void *val;
44 int nr_address_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
45 int nr_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
46 const uint32_t *addr_prop;
47 const uint32_t *size_prop;
48 int root_offset;
49 int cell_size;
50
51 root_offset = fdt_path_offset(initial_boot_params, "/");
52 if (root_offset < 0)
53 return;
54
55 addr_prop = fdt_getprop(initial_boot_params, root_offset,
56 "#address-cells", NULL);
57 if (addr_prop)
58 nr_address_cells = fdt32_to_cpu(*addr_prop);
59
60 size_prop = fdt_getprop(initial_boot_params, root_offset,
61 "#size-cells", NULL);
62 if (size_prop)
63 nr_size_cells = fdt32_to_cpu(*size_prop);
64
65 cell_size = sizeof(uint32_t)*(nr_address_cells + nr_size_cells);
66
67 memory = fdt_path_offset(initial_boot_params, "/memory");
68 if (memory > 0) {
69 val = fdt_getprop(initial_boot_params, memory, "reg", &len);
70 if (len > limit*cell_size) {
71 len = limit*cell_size;
72 pr_debug("Limiting number of entries to %d\n", limit);
73 fdt_setprop(initial_boot_params, memory, "reg", val,
74 len);
75 }
76 }
77}
78
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080079/**
80 * of_fdt_is_compatible - Return true if given node from the given blob has
81 * compat in its compatible list
82 * @blob: A device tree blob
83 * @node: node to test
84 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040085 *
86 * On match, returns a non-zero value with smaller values returned for more
87 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080088 */
Rob Herringc972de12014-04-01 22:48:01 -050089int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080090 unsigned long node, const char *compat)
91{
92 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050093 int cplen;
94 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080095
Rob Herringe6a69282014-04-02 15:10:14 -050096 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080097 if (cp == NULL)
98 return 0;
99 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -0400100 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800101 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -0400102 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800103 l = strlen(cp) + 1;
104 cp += l;
105 cplen -= l;
106 }
107
108 return 0;
109}
110
Grant Likelya4f740c2010-10-30 11:49:09 -0400111/**
Kevin Cernekeecc783782015-04-09 13:05:15 -0700112 * of_fdt_is_big_endian - Return true if given node needs BE MMIO accesses
113 * @blob: A device tree blob
114 * @node: node to test
115 *
116 * Returns true if the node has a "big-endian" property, or if the kernel
117 * was compiled for BE *and* the node has a "native-endian" property.
118 * Returns false otherwise.
119 */
120bool of_fdt_is_big_endian(const void *blob, unsigned long node)
121{
122 if (fdt_getprop(blob, node, "big-endian", NULL))
123 return true;
124 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
125 fdt_getprop(blob, node, "native-endian", NULL))
126 return true;
127 return false;
128}
129
130/**
Grant Likelya4f740c2010-10-30 11:49:09 -0400131 * of_fdt_match - Return true if node matches a list of compatible values
132 */
Rob Herringc972de12014-04-01 22:48:01 -0500133int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100134 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400135{
136 unsigned int tmp, score = 0;
137
138 if (!compat)
139 return 0;
140
141 while (*compat) {
142 tmp = of_fdt_is_compatible(blob, node, *compat);
143 if (tmp && (score == 0 || (tmp < score)))
144 score = tmp;
145 compat++;
146 }
147
148 return score;
149}
150
Grant Likely44856812013-08-29 13:30:35 +0100151static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700152 unsigned long align)
153{
154 void *res;
155
Grant Likely44856812013-08-29 13:30:35 +0100156 *mem = PTR_ALIGN(*mem, align);
157 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700158 *mem += size;
159
160 return res;
161}
162
163/**
164 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800165 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700166 * @mem: Memory chunk to use for allocating device nodes and properties
Masahiro Yamadace32f852015-04-13 10:30:20 +0900167 * @poffset: pointer to node in flat tree
Grant Likelybbd33932009-11-23 20:07:00 -0700168 * @dad: Parent struct device_node
Masahiro Yamadace32f852015-04-13 10:30:20 +0900169 * @nodepp: The device_node tree created by the call
Grant Likelybbd33932009-11-23 20:07:00 -0700170 * @fpsize: Size of the node path up at the current depth.
Masahiro Yamadace32f852015-04-13 10:30:20 +0900171 * @dryrun: If true, do not allocate device nodes but still calculate needed
172 * memory size
Grant Likelybbd33932009-11-23 20:07:00 -0700173 */
Geert Uytterhoeven3b1a2c92015-05-13 16:33:56 +0200174static void * unflatten_dt_node(const void *blob,
Grant Likely44856812013-08-29 13:30:35 +0100175 void *mem,
Rob Herringe6a69282014-04-02 15:10:14 -0500176 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800177 struct device_node *dad,
Grant Likely5063e252014-10-03 16:28:27 +0100178 struct device_node **nodepp,
179 unsigned long fpsize,
180 bool dryrun)
Grant Likelybbd33932009-11-23 20:07:00 -0700181{
Rob Herringe6a69282014-04-02 15:10:14 -0500182 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700183 struct device_node *np;
184 struct property *pp, **prev_pp = NULL;
Rob Herringe6a69282014-04-02 15:10:14 -0500185 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700186 unsigned int l, allocl;
Rob Herringe6a69282014-04-02 15:10:14 -0500187 static int depth = 0;
188 int old_depth;
189 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700190 int has_name = 0;
191 int new_format = 0;
192
Rob Herringe6a69282014-04-02 15:10:14 -0500193 pathp = fdt_get_name(blob, *poffset, &l);
194 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700195 return mem;
Rob Herringe6a69282014-04-02 15:10:14 -0500196
Ricky Liang05f46472015-04-14 12:36:05 +0800197 allocl = ++l;
Grant Likelybbd33932009-11-23 20:07:00 -0700198
199 /* version 0x10 has a more compact unit name here instead of the full
200 * path. we accumulate the full path size using "fpsize", we'll rebuild
201 * it later. We detect this because the first character of the name is
202 * not '/'.
203 */
204 if ((*pathp) != '/') {
205 new_format = 1;
206 if (fpsize == 0) {
207 /* root node: special case. fpsize accounts for path
208 * plus terminating zero. root node only has '/', so
209 * fpsize should be 2, but we want to avoid the first
210 * level nodes to have two '/' so we use fpsize 1 here
211 */
212 fpsize = 1;
213 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000214 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500215 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700216 } else {
217 /* account for '/' and path size minus terminal 0
218 * already in 'l'
219 */
220 fpsize += l;
221 allocl = fpsize;
222 }
223 }
224
225 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
226 __alignof__(struct device_node));
Grant Likely5063e252014-10-03 16:28:27 +0100227 if (!dryrun) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000228 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200229 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000230 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700231 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700232 /* rebuild full path for new format */
233 if (dad && dad->parent) {
234 strcpy(fn, dad->full_name);
235#ifdef DEBUG
236 if ((strlen(fn) + l + 1) != allocl) {
237 pr_debug("%s: p: %d, l: %d, a: %d\n",
238 pathp, (int)strlen(fn),
239 l, allocl);
240 }
241#endif
242 fn += strlen(fn);
243 }
244 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000245 }
246 memcpy(fn, pathp, l);
247
Grant Likelybbd33932009-11-23 20:07:00 -0700248 prev_pp = &np->properties;
Grant Likelybbd33932009-11-23 20:07:00 -0700249 if (dad != NULL) {
250 np->parent = dad;
Grant Likely70161ff2014-11-28 16:03:33 +0000251 np->sibling = dad->child;
252 dad->child = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700253 }
Grant Likelybbd33932009-11-23 20:07:00 -0700254 }
Andres Salomona7006c92011-03-17 17:32:35 -0700255 /* process properties */
Rob Herringe6a69282014-04-02 15:10:14 -0500256 for (offset = fdt_first_property_offset(blob, *poffset);
257 (offset >= 0);
258 (offset = fdt_next_property_offset(blob, offset))) {
259 const char *pname;
260 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700261
Rob Herringe6a69282014-04-02 15:10:14 -0500262 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
263 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700264 break;
Rob Herringe6a69282014-04-02 15:10:14 -0500265 }
Grant Likelybbd33932009-11-23 20:07:00 -0700266
Grant Likelybbd33932009-11-23 20:07:00 -0700267 if (pname == NULL) {
268 pr_info("Can't find property name in list !\n");
269 break;
270 }
271 if (strcmp(pname, "name") == 0)
272 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700273 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
274 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100275 if (!dryrun) {
David Gibson04b954a2010-02-01 21:34:15 -0700276 /* We accept flattened tree phandles either in
277 * ePAPR-style "phandle" properties, or the
278 * legacy "linux,phandle" properties. If both
279 * appear and have different values, things
280 * will get weird. Don't do that. */
281 if ((strcmp(pname, "phandle") == 0) ||
282 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700283 if (np->phandle == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500284 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700285 }
David Gibson04b954a2010-02-01 21:34:15 -0700286 /* And we process the "ibm,phandle" property
287 * used in pSeries dynamic device tree
288 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700289 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500290 np->phandle = be32_to_cpup(p);
291 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700292 pp->length = sz;
Rob Herringe6a69282014-04-02 15:10:14 -0500293 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700294 *prev_pp = pp;
295 prev_pp = &pp->next;
296 }
Grant Likelybbd33932009-11-23 20:07:00 -0700297 }
298 /* with version 0x10 we may not have the name property, recreate
299 * it here from the unit name if absent
300 */
301 if (!has_name) {
Rob Herringe6a69282014-04-02 15:10:14 -0500302 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700303 int sz;
304
305 while (*p1) {
306 if ((*p1) == '@')
307 pa = p1;
308 if ((*p1) == '/')
309 ps = p1 + 1;
310 p1++;
311 }
312 if (pa < ps)
313 pa = p1;
314 sz = (pa - ps) + 1;
315 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
316 __alignof__(struct property));
Grant Likely5063e252014-10-03 16:28:27 +0100317 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700318 pp->name = "name";
319 pp->length = sz;
320 pp->value = pp + 1;
321 *prev_pp = pp;
322 prev_pp = &pp->next;
323 memcpy(pp->value, ps, sz - 1);
324 ((char *)pp->value)[sz - 1] = 0;
325 pr_debug("fixed up name for %s -> %s\n", pathp,
326 (char *)pp->value);
327 }
328 }
Grant Likely5063e252014-10-03 16:28:27 +0100329 if (!dryrun) {
Grant Likelybbd33932009-11-23 20:07:00 -0700330 *prev_pp = NULL;
331 np->name = of_get_property(np, "name", NULL);
332 np->type = of_get_property(np, "device_type", NULL);
333
334 if (!np->name)
335 np->name = "<NULL>";
336 if (!np->type)
337 np->type = "<NULL>";
338 }
Rob Herringe6a69282014-04-02 15:10:14 -0500339
340 old_depth = depth;
341 *poffset = fdt_next_node(blob, *poffset, &depth);
342 if (depth < 0)
343 depth = 0;
344 while (*poffset > 0 && depth > old_depth)
Grant Likely5063e252014-10-03 16:28:27 +0100345 mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
346 fpsize, dryrun);
Rob Herringe6a69282014-04-02 15:10:14 -0500347
348 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
349 pr_err("unflatten: error %d processing FDT\n", *poffset);
350
Grant Likely70161ff2014-11-28 16:03:33 +0000351 /*
352 * Reverse the child list. Some drivers assumes node order matches .dts
353 * node order
354 */
355 if (!dryrun && np->child) {
356 struct device_node *child = np->child;
357 np->child = NULL;
358 while (child) {
359 struct device_node *next = child->sibling;
360 child->sibling = np->child;
361 np->child = child;
362 child = next;
363 }
364 }
365
Grant Likely5063e252014-10-03 16:28:27 +0100366 if (nodepp)
367 *nodepp = np;
Grant Likelybbd33932009-11-23 20:07:00 -0700368
369 return mem;
370}
Grant Likely41f88002009-11-23 20:07:01 -0700371
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800372/**
373 * __unflatten_device_tree - create tree of device_nodes from flat blob
374 *
375 * unflattens a device-tree, creating the
376 * tree of struct device_node. It also fills the "name" and "type"
377 * pointers of the nodes so the normal device-tree walking functions
378 * can be used.
379 * @blob: The blob to expand
380 * @mynodes: The device_node tree created by the call
381 * @dt_alloc: An allocator that provides a virtual address to memory
382 * for the resulting tree
383 */
Geert Uytterhoeven3b1a2c92015-05-13 16:33:56 +0200384static void __unflatten_device_tree(const void *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800385 struct device_node **mynodes,
386 void * (*dt_alloc)(u64 size, u64 align))
387{
Grant Likely44856812013-08-29 13:30:35 +0100388 unsigned long size;
Rob Herringe6a69282014-04-02 15:10:14 -0500389 int start;
390 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800391
392 pr_debug(" -> unflatten_device_tree()\n");
393
394 if (!blob) {
395 pr_debug("No device tree pointer\n");
396 return;
397 }
398
399 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500400 pr_debug("magic: %08x\n", fdt_magic(blob));
401 pr_debug("size: %08x\n", fdt_totalsize(blob));
402 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800403
Rob Herringc972de12014-04-01 22:48:01 -0500404 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800405 pr_err("Invalid device tree blob header\n");
406 return;
407 }
408
409 /* First pass, scan for size */
Rob Herringe6a69282014-04-02 15:10:14 -0500410 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100411 size = (unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
Grant Likely44856812013-08-29 13:30:35 +0100412 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800413
414 pr_debug(" size is %lx, allocating...\n", size);
415
416 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100417 mem = dt_alloc(size + 4, __alignof__(struct device_node));
418 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800419
Grant Likely44856812013-08-29 13:30:35 +0100420 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200421
Grant Likely44856812013-08-29 13:30:35 +0100422 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800423
424 /* Second pass, do actual unflattening */
Rob Herringe6a69282014-04-02 15:10:14 -0500425 start = 0;
Grant Likely5063e252014-10-03 16:28:27 +0100426 unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
Grant Likely44856812013-08-29 13:30:35 +0100427 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800428 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100429 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800430
431 pr_debug(" <- unflatten_device_tree()\n");
432}
433
434static void *kernel_tree_alloc(u64 size, u64 align)
435{
436 return kzalloc(size, GFP_KERNEL);
437}
438
439/**
440 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
441 *
442 * unflattens the device-tree passed by the firmware, creating the
443 * tree of struct device_node. It also fills the "name" and "type"
444 * pointers of the nodes so the normal device-tree walking functions
445 * can be used.
446 */
Geert Uytterhoeven3b1a2c92015-05-13 16:33:56 +0200447void of_fdt_unflatten_tree(const unsigned long *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800448 struct device_node **mynodes)
449{
Rob Herringc972de12014-04-01 22:48:01 -0500450 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800451}
452EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
453
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800454/* Everything below here references initial_boot_params directly. */
455int __initdata dt_root_addr_cells;
456int __initdata dt_root_size_cells;
457
Rob Herring1daa0c42014-03-31 15:25:04 -0500458void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800459
460#ifdef CONFIG_OF_EARLY_FLATTREE
461
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +0100462static u32 of_fdt_crc32;
463
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800464/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100465 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
466 */
467static int __init __reserved_mem_reserve_reg(unsigned long node,
468 const char *uname)
469{
470 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
471 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500472 int len;
473 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100474 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100475
476 prop = of_get_flat_dt_prop(node, "reg", &len);
477 if (!prop)
478 return -ENOENT;
479
480 if (len && len % t_len != 0) {
481 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
482 uname);
483 return -EINVAL;
484 }
485
486 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
487
488 while (len >= t_len) {
489 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
490 size = dt_mem_next_cell(dt_root_size_cells, &prop);
491
Al Cooperb5f2a8c2014-08-06 16:30:04 -0400492 if (size &&
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100493 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
494 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
495 uname, &base, (unsigned long)size / SZ_1M);
496 else
497 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
498 uname, &base, (unsigned long)size / SZ_1M);
499
500 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100501 if (first) {
502 fdt_reserved_mem_save_node(node, uname, base, size);
503 first = 0;
504 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100505 }
506 return 0;
507}
508
509/**
510 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
511 * in /reserved-memory matches the values supported by the current implementation,
512 * also check if ranges property has been provided
513 */
Xiubo Li5b624112014-04-08 13:48:07 +0800514static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100515{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500516 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100517
518 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
519 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
520 return -EINVAL;
521
522 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
523 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
524 return -EINVAL;
525
526 prop = of_get_flat_dt_prop(node, "ranges", NULL);
527 if (!prop)
528 return -EINVAL;
529 return 0;
530}
531
532/**
533 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
534 */
535static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
536 int depth, void *data)
537{
538 static int found;
539 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100540 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100541
542 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
543 if (__reserved_mem_check_root(node) != 0) {
544 pr_err("Reserved memory: unsupported node format, ignoring\n");
545 /* break scan */
546 return 1;
547 }
548 found = 1;
549 /* scan next node */
550 return 0;
551 } else if (!found) {
552 /* scan next node */
553 return 0;
554 } else if (found && depth < 2) {
555 /* scanning of /reserved-memory has been finished */
556 return 1;
557 }
558
559 status = of_get_flat_dt_prop(node, "status", NULL);
560 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
561 return 0;
562
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100563 err = __reserved_mem_reserve_reg(node, uname);
564 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
565 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100566
567 /* scan next node */
568 return 0;
569}
570
571/**
572 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
573 *
574 * This function grabs memory from early allocator for device exclusive use
575 * defined in device tree structures. It should be called by arch specific code
576 * once the early allocator (i.e. memblock) has been fully activated.
577 */
578void __init early_init_fdt_scan_reserved_mem(void)
579{
Rob Herringd1552ce2014-04-01 22:46:48 -0500580 int n;
581 u64 base, size;
582
Josh Cartwright2040b522014-03-13 16:36:36 -0500583 if (!initial_boot_params)
584 return;
585
Rob Herringd1552ce2014-04-01 22:46:48 -0500586 /* Process header /memreserve/ fields */
587 for (n = 0; ; n++) {
588 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
589 if (!size)
590 break;
591 early_init_dt_reserve_memory_arch(base, size, 0);
592 }
593
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100594 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100595 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100596}
597
598/**
Ard Biesheuvel24bbd922015-06-01 13:40:31 +0200599 * early_init_fdt_reserve_self() - reserve the memory used by the FDT blob
600 */
601void __init early_init_fdt_reserve_self(void)
602{
603 if (!initial_boot_params)
604 return;
605
606 /* Reserve the dtb region */
607 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
608 fdt_totalsize(initial_boot_params),
609 0);
610}
611
612/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800613 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
614 * @it: callback function
615 * @data: context data pointer
616 *
617 * This function is used to scan the flattened device-tree, it is
618 * used to extract the memory information at boot before we can
619 * unflatten the tree
620 */
621int __init of_scan_flat_dt(int (*it)(unsigned long node,
622 const char *uname, int depth,
623 void *data),
624 void *data)
625{
Rob Herringe6a69282014-04-02 15:10:14 -0500626 const void *blob = initial_boot_params;
627 const char *pathp;
628 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800629
Rob Herringe6a69282014-04-02 15:10:14 -0500630 for (offset = fdt_next_node(blob, -1, &depth);
631 offset >= 0 && depth >= 0 && !rc;
632 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800633
Rob Herringe6a69282014-04-02 15:10:14 -0500634 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800635 if (*pathp == '/')
636 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500637 rc = it(offset, pathp, depth, data);
638 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800639 return rc;
640}
641
642/**
643 * of_get_flat_dt_root - find the root node in the flat blob
644 */
645unsigned long __init of_get_flat_dt_root(void)
646{
Rob Herringe6a69282014-04-02 15:10:14 -0500647 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800648}
649
650/**
Rob Herringc0556d32014-04-22 12:55:10 -0500651 * of_get_flat_dt_size - Return the total size of the FDT
652 */
653int __init of_get_flat_dt_size(void)
654{
655 return fdt_totalsize(initial_boot_params);
656}
657
658/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800659 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
660 *
661 * This function can be used within scan_flattened_dt callback to get
662 * access to properties
663 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500664const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
665 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800666{
Rob Herringe6a69282014-04-02 15:10:14 -0500667 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800668}
669
670/**
671 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
672 * @node: node to test
673 * @compat: compatible string to compare with compatible list.
674 */
675int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
676{
677 return of_fdt_is_compatible(initial_boot_params, node, compat);
678}
679
Grant Likelya4f740c2010-10-30 11:49:09 -0400680/**
681 * of_flat_dt_match - Return true if node matches a list of compatible values
682 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100683int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400684{
685 return of_fdt_match(initial_boot_params, node, compat);
686}
687
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200688struct fdt_scan_status {
689 const char *name;
690 int namelen;
691 int depth;
692 int found;
693 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
694 void *data;
695};
696
Rob Herring6a903a22013-08-27 21:41:56 -0500697const char * __init of_flat_dt_get_machine_name(void)
698{
699 const char *name;
700 unsigned long dt_root = of_get_flat_dt_root();
701
702 name = of_get_flat_dt_prop(dt_root, "model", NULL);
703 if (!name)
704 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
705 return name;
706}
707
708/**
709 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
710 *
711 * @default_match: A machine specific ptr to return in case of no match.
712 * @get_next_compat: callback function to return next compatible match table.
713 *
714 * Iterate through machine match tables to find the best match for the machine
715 * compatible string in the FDT.
716 */
717const void * __init of_flat_dt_match_machine(const void *default_match,
718 const void * (*get_next_compat)(const char * const**))
719{
720 const void *data = NULL;
721 const void *best_data = default_match;
722 const char *const *compat;
723 unsigned long dt_root;
724 unsigned int best_score = ~1, score = 0;
725
726 dt_root = of_get_flat_dt_root();
727 while ((data = get_next_compat(&compat))) {
728 score = of_flat_dt_match(dt_root, compat);
729 if (score > 0 && score < best_score) {
730 best_data = data;
731 best_score = score;
732 }
733 }
734 if (!best_data) {
735 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500736 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500737
738 pr_err("\n unrecognized device tree list:\n[ ");
739
740 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
741 if (prop) {
742 while (size > 0) {
743 printk("'%s' ", prop);
744 size -= strlen(prop) + 1;
745 prop += strlen(prop) + 1;
746 }
747 }
748 printk("]\n\n");
749 return NULL;
750 }
751
752 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
753
754 return best_data;
755}
756
Grant Likelyf7b3a832009-11-24 03:26:58 -0700757#ifdef CONFIG_BLK_DEV_INITRD
758/**
759 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
760 * @node: reference to node containing initrd location ('chosen')
761 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500762static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700763{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400764 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500765 int len;
766 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700767
768 pr_debug("Looking for initrd properties... ");
769
770 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700771 if (!prop)
772 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400773 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700774
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700775 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
776 if (!prop)
777 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400778 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700779
Rob Herring29eb45a2013-08-30 17:06:53 -0500780 initrd_start = (unsigned long)__va(start);
781 initrd_end = (unsigned long)__va(end);
782 initrd_below_start_ok = 1;
783
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400784 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
785 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700786}
787#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500788static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700789{
790}
791#endif /* CONFIG_BLK_DEV_INITRD */
792
Rob Herringfb11ffe2014-03-27 08:07:01 -0500793#ifdef CONFIG_SERIAL_EARLYCON
794extern struct of_device_id __earlycon_of_table[];
795
Lad, Prabhakar523bf172015-02-04 12:04:06 +0000796static int __init early_init_dt_scan_chosen_serial(void)
Rob Herringfb11ffe2014-03-27 08:07:01 -0500797{
798 int offset;
799 const char *p;
800 int l;
801 const struct of_device_id *match = __earlycon_of_table;
802 const void *fdt = initial_boot_params;
803
804 offset = fdt_path_offset(fdt, "/chosen");
805 if (offset < 0)
806 offset = fdt_path_offset(fdt, "/chosen@0");
807 if (offset < 0)
808 return -ENOENT;
809
810 p = fdt_getprop(fdt, offset, "stdout-path", &l);
811 if (!p)
812 p = fdt_getprop(fdt, offset, "linux,stdout-path", &l);
813 if (!p || !l)
814 return -ENOENT;
815
Stefan Agner6296ad92015-10-10 01:29:30 -0700816 /* Remove console options if present */
817 l = strchrnul(p, ':') - p;
818
Rob Herringfb11ffe2014-03-27 08:07:01 -0500819 /* Get the node specified by stdout-path */
Stefan Agner6296ad92015-10-10 01:29:30 -0700820 offset = fdt_path_offset_namelen(fdt, p, l);
Rob Herringfb11ffe2014-03-27 08:07:01 -0500821 if (offset < 0)
822 return -ENODEV;
823
Kevin Cernekeeab74d002014-11-09 00:55:47 -0800824 while (match->compatible[0]) {
Rob Herringfb11ffe2014-03-27 08:07:01 -0500825 unsigned long addr;
826 if (fdt_node_check_compatible(fdt, offset, match->compatible)) {
827 match++;
828 continue;
829 }
830
831 addr = fdt_translate_address(fdt, offset);
832 if (!addr)
833 return -ENXIO;
834
835 of_setup_earlycon(addr, match->data);
836 return 0;
837 }
838 return -ENODEV;
839}
840
841static int __init setup_of_earlycon(char *buf)
842{
843 if (buf)
844 return 0;
845
846 return early_init_dt_scan_chosen_serial();
847}
848early_param("earlycon", setup_of_earlycon);
849#endif
850
Grant Likely41f88002009-11-23 20:07:01 -0700851/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700852 * early_init_dt_scan_root - fetch the top level address and size cells
853 */
854int __init early_init_dt_scan_root(unsigned long node, const char *uname,
855 int depth, void *data)
856{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500857 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700858
859 if (depth != 0)
860 return 0;
861
Jeremy Kerr33714882010-01-30 01:45:26 -0700862 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
863 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
864
Grant Likelyf00abd92009-11-24 03:27:10 -0700865 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700866 if (prop)
867 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700868 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
869
870 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700871 if (prop)
872 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700873 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
874
875 /* break now */
876 return 1;
877}
878
Rob Herring9d0c4df2014-04-01 23:49:03 -0500879u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700880{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500881 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700882
883 *cellp = p + s;
884 return of_read_number(p, s);
885}
886
Grant Likely51975db2010-02-01 21:34:14 -0700887/**
888 * early_init_dt_scan_memory - Look for an parse memory nodes
889 */
890int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
891 int depth, void *data)
892{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500893 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
894 const __be32 *reg, *endp;
895 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700896
897 /* We are scanning "memory" nodes only */
898 if (type == NULL) {
899 /*
900 * The longtrail doesn't have a device_type on the
901 * /memory node, so look for the node called /memory@0.
902 */
Leif Lindholmb44aa252014-04-17 18:42:01 +0100903 if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
Grant Likely51975db2010-02-01 21:34:14 -0700904 return 0;
905 } else if (strcmp(type, "memory") != 0)
906 return 0;
907
908 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
909 if (reg == NULL)
910 reg = of_get_flat_dt_prop(node, "reg", &l);
911 if (reg == NULL)
912 return 0;
913
914 endp = reg + (l / sizeof(__be32));
915
Florian Fainellic954b362015-04-12 13:16:26 -0700916 pr_debug("memory scan node %s, reg size %d,\n", uname, l);
Grant Likely51975db2010-02-01 21:34:14 -0700917
918 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
919 u64 base, size;
920
921 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
922 size = dt_mem_next_cell(dt_root_size_cells, &reg);
923
924 if (size == 0)
925 continue;
926 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
927 (unsigned long long)size);
928
929 early_init_dt_add_memory_arch(base, size);
930 }
931
932 return 0;
933}
934
Grant Likely86e03222009-12-10 23:42:21 -0700935int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
936 int depth, void *data)
937{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500938 int l;
939 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700940
941 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
942
Grant Likely85f60ae2011-04-29 00:18:16 -0600943 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700944 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
945 return 0;
946
947 early_init_dt_check_for_initrd(node);
948
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300949 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700950 p = of_get_flat_dt_prop(node, "bootargs", &l);
951 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600952 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700953
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +0000954 /*
955 * CONFIG_CMDLINE is meant to be a default in case nothing else
956 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
957 * is set in which case we override whatever was found earlier.
958 */
Grant Likely86e03222009-12-10 23:42:21 -0700959#ifdef CONFIG_CMDLINE
960#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +0000961 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700962#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600963 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700964#endif /* CONFIG_CMDLINE */
965
Grant Likely85f60ae2011-04-29 00:18:16 -0600966 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700967
968 /* break now */
969 return 1;
970}
971
Grant Likelya1727da2013-08-28 21:18:32 +0100972#ifdef CONFIG_HAVE_MEMBLOCK
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +0100973#ifndef MAX_MEMBLOCK_ADDR
974#define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0)
975#endif
Laura Abbott3069f0c2014-07-07 17:45:43 -0700976
Rob Herring068f6312013-09-24 22:20:01 -0500977void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
978{
979 const u64 phys_offset = __pa(PAGE_OFFSET);
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200980
981 if (!PAGE_ALIGNED(base)) {
Ard Biesheuvel8cccffc2014-10-29 17:09:32 +0100982 if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
983 pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
984 base, base + size);
985 return;
986 }
Geert Uytterhoeven8f73d4b2014-08-20 17:10:31 +0200987 size -= PAGE_SIZE - (base & ~PAGE_MASK);
988 base = PAGE_ALIGN(base);
989 }
Rob Herring068f6312013-09-24 22:20:01 -0500990 size &= PAGE_MASK;
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700991
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +0100992 if (base > MAX_MEMBLOCK_ADDR) {
Laura Abbott3069f0c2014-07-07 17:45:43 -0700993 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
994 base, base + size);
995 return;
996 }
Laura Abbotta67a6ed2014-06-19 20:13:38 -0700997
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +0100998 if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
Srinivas Kandagatla9aacd602014-09-23 10:59:09 +0100999 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
Ard Biesheuvel8eafeb42015-08-18 10:34:41 +01001000 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1001 size = MAX_MEMBLOCK_ADDR - base + 1;
Laura Abbotta67a6ed2014-06-19 20:13:38 -07001002 }
1003
Rob Herring068f6312013-09-24 22:20:01 -05001004 if (base + size < phys_offset) {
1005 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1006 base, base + size);
1007 return;
1008 }
1009 if (base < phys_offset) {
1010 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1011 base, phys_offset);
1012 size -= phys_offset - base;
1013 base = phys_offset;
1014 }
1015 memblock_add(base, size);
1016}
1017
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001018int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1019 phys_addr_t size, bool nomap)
1020{
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001021 if (nomap)
1022 return memblock_remove(base, size);
1023 return memblock_reserve(base, size);
1024}
1025
Grant Likelya1727da2013-08-28 21:18:32 +01001026/*
1027 * called from unflatten_device_tree() to bootstrap devicetree itself
1028 * Architectures can override this definition if memblock isn't used
1029 */
1030void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1031{
1032 return __va(memblock_alloc(size, align));
1033}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001034#else
Rob Herringaefc7ec2015-06-18 20:35:46 -05001035void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
1036{
1037 WARN_ON(1);
1038}
1039
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001040int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
1041 phys_addr_t size, bool nomap)
1042{
Rob Herring1d1a6612014-04-22 12:50:24 -05001043 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
1044 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +01001045 return -ENOSYS;
1046}
Rob Herringaefc7ec2015-06-18 20:35:46 -05001047
1048void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
1049{
1050 WARN_ON(1);
1051 return NULL;
1052}
Grant Likelya1727da2013-08-28 21:18:32 +01001053#endif
1054
Laura Abbott4972a742014-07-15 10:03:34 -07001055bool __init early_init_dt_verify(void *params)
Rob Herring0288ffcb2013-08-26 09:47:40 -05001056{
1057 if (!params)
1058 return false;
1059
Bjorn Helgaas50ba08f2014-10-29 12:15:00 -06001060 /* check device tree validity */
1061 if (fdt_check_header(params))
1062 return false;
1063
Rob Herring0288ffcb2013-08-26 09:47:40 -05001064 /* Setup flat device-tree pointer */
1065 initial_boot_params = params;
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001066 of_fdt_crc32 = crc32_be(~0, initial_boot_params,
1067 fdt_totalsize(initial_boot_params));
Laura Abbott4972a742014-07-15 10:03:34 -07001068 return true;
1069}
1070
1071
1072void __init early_init_dt_scan_nodes(void)
1073{
Rob Herring0288ffcb2013-08-26 09:47:40 -05001074 /* Retrieve various information from the /chosen node */
1075 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1076
1077 /* Initialize {size,address}-cells info */
1078 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1079
1080 /* Setup memory, calling early_init_dt_add_memory_arch */
1081 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Laura Abbott4972a742014-07-15 10:03:34 -07001082}
Rob Herring0288ffcb2013-08-26 09:47:40 -05001083
Laura Abbott4972a742014-07-15 10:03:34 -07001084bool __init early_init_dt_scan(void *params)
1085{
1086 bool status;
1087
1088 status = early_init_dt_verify(params);
1089 if (!status)
1090 return false;
1091
1092 early_init_dt_scan_nodes();
Rob Herring0288ffcb2013-08-26 09:47:40 -05001093 return true;
1094}
1095
Grant Likelyf00abd92009-11-24 03:27:10 -07001096/**
Grant Likely41f88002009-11-23 20:07:01 -07001097 * unflatten_device_tree - create tree of device_nodes from flat blob
1098 *
1099 * unflattens the device-tree passed by the firmware, creating the
1100 * tree of struct device_node. It also fills the "name" and "type"
1101 * pointers of the nodes so the normal device-tree walking functions
1102 * can be used.
1103 */
1104void __init unflatten_device_tree(void)
1105{
Grant Likely5063e252014-10-03 16:28:27 +01001106 __unflatten_device_tree(initial_boot_params, &of_root,
Grant Likely672c5442011-01-13 15:36:09 -07001107 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001108
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001109 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001110 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001111}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001112
Rob Herringa8bf7522013-08-26 11:22:45 -05001113/**
1114 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1115 *
1116 * Copies and unflattens the device-tree passed by the firmware, creating the
1117 * tree of struct device_node. It also fills the "name" and "type"
1118 * pointers of the nodes so the normal device-tree walking functions
1119 * can be used. This should only be used when the FDT memory has not been
1120 * reserved such is the case when the FDT is built-in to the kernel init
1121 * section. If the FDT memory is reserved already then unflatten_device_tree
1122 * should be used instead.
1123 */
1124void __init unflatten_and_copy_device_tree(void)
1125{
James Hogan6f041e92013-11-21 13:44:14 +00001126 int size;
1127 void *dt;
1128
1129 if (!initial_boot_params) {
1130 pr_warn("No valid device tree found, continuing without\n");
1131 return;
1132 }
1133
Rob Herringc972de12014-04-01 22:48:01 -05001134 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +00001135 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -05001136 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -05001137
1138 if (dt) {
1139 memcpy(dt, initial_boot_params, size);
1140 initial_boot_params = dt;
1141 }
1142 unflatten_device_tree();
1143}
1144
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001145#ifdef CONFIG_SYSFS
1146static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj,
1147 struct bin_attribute *bin_attr,
1148 char *buf, loff_t off, size_t count)
Rob Herringb0a6fb32014-04-02 16:56:48 -05001149{
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001150 memcpy(buf, initial_boot_params + off, count);
1151 return count;
Rob Herringb0a6fb32014-04-02 16:56:48 -05001152}
Ard Biesheuvel08d53aa2014-11-14 18:05:35 +01001153
1154static int __init of_fdt_raw_init(void)
1155{
1156 static struct bin_attribute of_fdt_raw_attr =
1157 __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0);
1158
1159 if (!initial_boot_params)
1160 return 0;
1161
1162 if (of_fdt_crc32 != crc32_be(~0, initial_boot_params,
1163 fdt_totalsize(initial_boot_params))) {
1164 pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n");
1165 return 0;
1166 }
1167 of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params);
1168 return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr);
1169}
1170late_initcall(of_fdt_raw_init);
Rob Herringb0a6fb32014-04-02 16:56:48 -05001171#endif
1172
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001173#endif /* CONFIG_OF_EARLY_FLATTREE */