blob: 051be4ca25b9655485ee1618d109feca5b3ab66c [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
Grant Likely41f88002009-11-23 20:07:01 -070012#include <linux/kernel.h>
Grant Likelyf7b3a832009-11-24 03:26:58 -070013#include <linux/initrd.h>
Grant Likelya1727da2013-08-28 21:18:32 +010014#include <linux/memblock.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070015#include <linux/of.h>
16#include <linux/of_fdt.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010017#include <linux/of_reserved_mem.h>
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +010018#include <linux/sizes.h>
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070019#include <linux/string.h>
20#include <linux/errno.h>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080021#include <linux/slab.h>
Rob Herringe6a69282014-04-02 15:10:14 -050022#include <linux/libfdt.h>
Rob Herringb0a6fb32014-04-02 16:56:48 -050023#include <linux/debugfs.h>
Grant Likely51975db2010-02-01 21:34:14 -070024
Fabio Estevamc89810a2012-01-02 14:19:03 -020025#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070026#include <asm/page.h>
27
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080028/**
29 * of_fdt_is_compatible - Return true if given node from the given blob has
30 * compat in its compatible list
31 * @blob: A device tree blob
32 * @node: node to test
33 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040034 *
35 * On match, returns a non-zero value with smaller values returned for more
36 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080037 */
Rob Herringc972de12014-04-01 22:48:01 -050038int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080039 unsigned long node, const char *compat)
40{
41 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050042 int cplen;
43 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080044
Rob Herringe6a69282014-04-02 15:10:14 -050045 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080046 if (cp == NULL)
47 return 0;
48 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -040049 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080050 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -040051 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080052 l = strlen(cp) + 1;
53 cp += l;
54 cplen -= l;
55 }
56
57 return 0;
58}
59
Grant Likelya4f740c2010-10-30 11:49:09 -040060/**
61 * of_fdt_match - Return true if node matches a list of compatible values
62 */
Rob Herringc972de12014-04-01 22:48:01 -050063int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +010064 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -040065{
66 unsigned int tmp, score = 0;
67
68 if (!compat)
69 return 0;
70
71 while (*compat) {
72 tmp = of_fdt_is_compatible(blob, node, *compat);
73 if (tmp && (score == 0 || (tmp < score)))
74 score = tmp;
75 compat++;
76 }
77
78 return score;
79}
80
Grant Likely44856812013-08-29 13:30:35 +010081static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -070082 unsigned long align)
83{
84 void *res;
85
Grant Likely44856812013-08-29 13:30:35 +010086 *mem = PTR_ALIGN(*mem, align);
87 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -070088 *mem += size;
89
90 return res;
91}
92
93/**
94 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -080095 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -070096 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -070097 * @p: pointer to node in flat tree
98 * @dad: Parent struct device_node
99 * @allnextpp: pointer to ->allnext from last allocated device_node
100 * @fpsize: Size of the node path up at the current depth.
101 */
Rob Herringc972de12014-04-01 22:48:01 -0500102static void * unflatten_dt_node(void *blob,
Grant Likely44856812013-08-29 13:30:35 +0100103 void *mem,
Rob Herringe6a69282014-04-02 15:10:14 -0500104 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800105 struct device_node *dad,
106 struct device_node ***allnextpp,
107 unsigned long fpsize)
Grant Likelybbd33932009-11-23 20:07:00 -0700108{
Rob Herringe6a69282014-04-02 15:10:14 -0500109 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700110 struct device_node *np;
111 struct property *pp, **prev_pp = NULL;
Rob Herringe6a69282014-04-02 15:10:14 -0500112 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700113 unsigned int l, allocl;
Rob Herringe6a69282014-04-02 15:10:14 -0500114 static int depth = 0;
115 int old_depth;
116 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700117 int has_name = 0;
118 int new_format = 0;
119
Rob Herringe6a69282014-04-02 15:10:14 -0500120 pathp = fdt_get_name(blob, *poffset, &l);
121 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700122 return mem;
Rob Herringe6a69282014-04-02 15:10:14 -0500123
124 allocl = l++;
Grant Likelybbd33932009-11-23 20:07:00 -0700125
126 /* version 0x10 has a more compact unit name here instead of the full
127 * path. we accumulate the full path size using "fpsize", we'll rebuild
128 * it later. We detect this because the first character of the name is
129 * not '/'.
130 */
131 if ((*pathp) != '/') {
132 new_format = 1;
133 if (fpsize == 0) {
134 /* root node: special case. fpsize accounts for path
135 * plus terminating zero. root node only has '/', so
136 * fpsize should be 2, but we want to avoid the first
137 * level nodes to have two '/' so we use fpsize 1 here
138 */
139 fpsize = 1;
140 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000141 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500142 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700143 } else {
144 /* account for '/' and path size minus terminal 0
145 * already in 'l'
146 */
147 fpsize += l;
148 allocl = fpsize;
149 }
150 }
151
152 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
153 __alignof__(struct device_node));
154 if (allnextpp) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000155 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200156 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000157 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700158 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700159 /* rebuild full path for new format */
160 if (dad && dad->parent) {
161 strcpy(fn, dad->full_name);
162#ifdef DEBUG
163 if ((strlen(fn) + l + 1) != allocl) {
164 pr_debug("%s: p: %d, l: %d, a: %d\n",
165 pathp, (int)strlen(fn),
166 l, allocl);
167 }
168#endif
169 fn += strlen(fn);
170 }
171 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000172 }
173 memcpy(fn, pathp, l);
174
Grant Likelybbd33932009-11-23 20:07:00 -0700175 prev_pp = &np->properties;
176 **allnextpp = np;
177 *allnextpp = &np->allnext;
178 if (dad != NULL) {
179 np->parent = dad;
180 /* we temporarily use the next field as `last_child'*/
181 if (dad->next == NULL)
182 dad->child = np;
183 else
184 dad->next->sibling = np;
185 dad->next = np;
186 }
Grant Likelybbd33932009-11-23 20:07:00 -0700187 }
Andres Salomona7006c92011-03-17 17:32:35 -0700188 /* process properties */
Rob Herringe6a69282014-04-02 15:10:14 -0500189 for (offset = fdt_first_property_offset(blob, *poffset);
190 (offset >= 0);
191 (offset = fdt_next_property_offset(blob, offset))) {
192 const char *pname;
193 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700194
Rob Herringe6a69282014-04-02 15:10:14 -0500195 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
196 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700197 break;
Rob Herringe6a69282014-04-02 15:10:14 -0500198 }
Grant Likelybbd33932009-11-23 20:07:00 -0700199
Grant Likelybbd33932009-11-23 20:07:00 -0700200 if (pname == NULL) {
201 pr_info("Can't find property name in list !\n");
202 break;
203 }
204 if (strcmp(pname, "name") == 0)
205 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700206 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
207 __alignof__(struct property));
208 if (allnextpp) {
David Gibson04b954a2010-02-01 21:34:15 -0700209 /* We accept flattened tree phandles either in
210 * ePAPR-style "phandle" properties, or the
211 * legacy "linux,phandle" properties. If both
212 * appear and have different values, things
213 * will get weird. Don't do that. */
214 if ((strcmp(pname, "phandle") == 0) ||
215 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700216 if (np->phandle == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500217 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700218 }
David Gibson04b954a2010-02-01 21:34:15 -0700219 /* And we process the "ibm,phandle" property
220 * used in pSeries dynamic device tree
221 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700222 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500223 np->phandle = be32_to_cpup(p);
224 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700225 pp->length = sz;
Rob Herringe6a69282014-04-02 15:10:14 -0500226 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700227 *prev_pp = pp;
228 prev_pp = &pp->next;
229 }
Grant Likelybbd33932009-11-23 20:07:00 -0700230 }
231 /* with version 0x10 we may not have the name property, recreate
232 * it here from the unit name if absent
233 */
234 if (!has_name) {
Rob Herringe6a69282014-04-02 15:10:14 -0500235 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700236 int sz;
237
238 while (*p1) {
239 if ((*p1) == '@')
240 pa = p1;
241 if ((*p1) == '/')
242 ps = p1 + 1;
243 p1++;
244 }
245 if (pa < ps)
246 pa = p1;
247 sz = (pa - ps) + 1;
248 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
249 __alignof__(struct property));
250 if (allnextpp) {
251 pp->name = "name";
252 pp->length = sz;
253 pp->value = pp + 1;
254 *prev_pp = pp;
255 prev_pp = &pp->next;
256 memcpy(pp->value, ps, sz - 1);
257 ((char *)pp->value)[sz - 1] = 0;
258 pr_debug("fixed up name for %s -> %s\n", pathp,
259 (char *)pp->value);
260 }
261 }
262 if (allnextpp) {
263 *prev_pp = NULL;
264 np->name = of_get_property(np, "name", NULL);
265 np->type = of_get_property(np, "device_type", NULL);
266
267 if (!np->name)
268 np->name = "<NULL>";
269 if (!np->type)
270 np->type = "<NULL>";
271 }
Rob Herringe6a69282014-04-02 15:10:14 -0500272
273 old_depth = depth;
274 *poffset = fdt_next_node(blob, *poffset, &depth);
275 if (depth < 0)
276 depth = 0;
277 while (*poffset > 0 && depth > old_depth)
278 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
279 fpsize);
280
281 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
282 pr_err("unflatten: error %d processing FDT\n", *poffset);
283
Grant Likelybbd33932009-11-23 20:07:00 -0700284 return mem;
285}
Grant Likely41f88002009-11-23 20:07:01 -0700286
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800287/**
288 * __unflatten_device_tree - create tree of device_nodes from flat blob
289 *
290 * unflattens a device-tree, creating the
291 * tree of struct device_node. It also fills the "name" and "type"
292 * pointers of the nodes so the normal device-tree walking functions
293 * can be used.
294 * @blob: The blob to expand
295 * @mynodes: The device_node tree created by the call
296 * @dt_alloc: An allocator that provides a virtual address to memory
297 * for the resulting tree
298 */
Rob Herringc972de12014-04-01 22:48:01 -0500299static void __unflatten_device_tree(void *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800300 struct device_node **mynodes,
301 void * (*dt_alloc)(u64 size, u64 align))
302{
Grant Likely44856812013-08-29 13:30:35 +0100303 unsigned long size;
Rob Herringe6a69282014-04-02 15:10:14 -0500304 int start;
305 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800306 struct device_node **allnextp = mynodes;
307
308 pr_debug(" -> unflatten_device_tree()\n");
309
310 if (!blob) {
311 pr_debug("No device tree pointer\n");
312 return;
313 }
314
315 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500316 pr_debug("magic: %08x\n", fdt_magic(blob));
317 pr_debug("size: %08x\n", fdt_totalsize(blob));
318 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800319
Rob Herringc972de12014-04-01 22:48:01 -0500320 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800321 pr_err("Invalid device tree blob header\n");
322 return;
323 }
324
325 /* First pass, scan for size */
Rob Herringe6a69282014-04-02 15:10:14 -0500326 start = 0;
Grant Likely44856812013-08-29 13:30:35 +0100327 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
328 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800329
330 pr_debug(" size is %lx, allocating...\n", size);
331
332 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100333 mem = dt_alloc(size + 4, __alignof__(struct device_node));
334 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800335
Grant Likely44856812013-08-29 13:30:35 +0100336 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200337
Grant Likely44856812013-08-29 13:30:35 +0100338 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800339
340 /* Second pass, do actual unflattening */
Rob Herringe6a69282014-04-02 15:10:14 -0500341 start = 0;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800342 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
Grant Likely44856812013-08-29 13:30:35 +0100343 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800344 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100345 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800346 *allnextp = NULL;
347
348 pr_debug(" <- unflatten_device_tree()\n");
349}
350
351static void *kernel_tree_alloc(u64 size, u64 align)
352{
353 return kzalloc(size, GFP_KERNEL);
354}
355
356/**
357 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
358 *
359 * unflattens the device-tree passed by the firmware, creating the
360 * tree of struct device_node. It also fills the "name" and "type"
361 * pointers of the nodes so the normal device-tree walking functions
362 * can be used.
363 */
364void of_fdt_unflatten_tree(unsigned long *blob,
365 struct device_node **mynodes)
366{
Rob Herringc972de12014-04-01 22:48:01 -0500367 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800368}
369EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
370
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800371/* Everything below here references initial_boot_params directly. */
372int __initdata dt_root_addr_cells;
373int __initdata dt_root_size_cells;
374
375struct boot_param_header *initial_boot_params;
376
377#ifdef CONFIG_OF_EARLY_FLATTREE
378
379/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100380 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
381 */
382static int __init __reserved_mem_reserve_reg(unsigned long node,
383 const char *uname)
384{
385 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
386 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500387 int len;
388 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100389 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100390
391 prop = of_get_flat_dt_prop(node, "reg", &len);
392 if (!prop)
393 return -ENOENT;
394
395 if (len && len % t_len != 0) {
396 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
397 uname);
398 return -EINVAL;
399 }
400
401 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
402
403 while (len >= t_len) {
404 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
405 size = dt_mem_next_cell(dt_root_size_cells, &prop);
406
407 if (base && size &&
408 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
409 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
410 uname, &base, (unsigned long)size / SZ_1M);
411 else
412 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
413 uname, &base, (unsigned long)size / SZ_1M);
414
415 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100416 if (first) {
417 fdt_reserved_mem_save_node(node, uname, base, size);
418 first = 0;
419 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100420 }
421 return 0;
422}
423
424/**
425 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
426 * in /reserved-memory matches the values supported by the current implementation,
427 * also check if ranges property has been provided
428 */
Xiubo Li5b624112014-04-08 13:48:07 +0800429static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100430{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500431 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100432
433 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
434 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
435 return -EINVAL;
436
437 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
438 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
439 return -EINVAL;
440
441 prop = of_get_flat_dt_prop(node, "ranges", NULL);
442 if (!prop)
443 return -EINVAL;
444 return 0;
445}
446
447/**
448 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
449 */
450static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
451 int depth, void *data)
452{
453 static int found;
454 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100455 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100456
457 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
458 if (__reserved_mem_check_root(node) != 0) {
459 pr_err("Reserved memory: unsupported node format, ignoring\n");
460 /* break scan */
461 return 1;
462 }
463 found = 1;
464 /* scan next node */
465 return 0;
466 } else if (!found) {
467 /* scan next node */
468 return 0;
469 } else if (found && depth < 2) {
470 /* scanning of /reserved-memory has been finished */
471 return 1;
472 }
473
474 status = of_get_flat_dt_prop(node, "status", NULL);
475 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
476 return 0;
477
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100478 err = __reserved_mem_reserve_reg(node, uname);
479 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
480 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100481
482 /* scan next node */
483 return 0;
484}
485
486/**
487 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
488 *
489 * This function grabs memory from early allocator for device exclusive use
490 * defined in device tree structures. It should be called by arch specific code
491 * once the early allocator (i.e. memblock) has been fully activated.
492 */
493void __init early_init_fdt_scan_reserved_mem(void)
494{
Rob Herringd1552ce2014-04-01 22:46:48 -0500495 int n;
496 u64 base, size;
497
Josh Cartwright2040b522014-03-13 16:36:36 -0500498 if (!initial_boot_params)
499 return;
500
Rob Herringd1552ce2014-04-01 22:46:48 -0500501 /* Reserve the dtb region */
502 early_init_dt_reserve_memory_arch(__pa(initial_boot_params),
503 fdt_totalsize(initial_boot_params),
504 0);
505
506 /* Process header /memreserve/ fields */
507 for (n = 0; ; n++) {
508 fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
509 if (!size)
510 break;
511 early_init_dt_reserve_memory_arch(base, size, 0);
512 }
513
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100514 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100515 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100516}
517
518/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800519 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
520 * @it: callback function
521 * @data: context data pointer
522 *
523 * This function is used to scan the flattened device-tree, it is
524 * used to extract the memory information at boot before we can
525 * unflatten the tree
526 */
527int __init of_scan_flat_dt(int (*it)(unsigned long node,
528 const char *uname, int depth,
529 void *data),
530 void *data)
531{
Rob Herringe6a69282014-04-02 15:10:14 -0500532 const void *blob = initial_boot_params;
533 const char *pathp;
534 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800535
Rob Herringe6a69282014-04-02 15:10:14 -0500536 for (offset = fdt_next_node(blob, -1, &depth);
537 offset >= 0 && depth >= 0 && !rc;
538 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800539
Rob Herringe6a69282014-04-02 15:10:14 -0500540 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800541 if (*pathp == '/')
542 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500543 rc = it(offset, pathp, depth, data);
544 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800545 return rc;
546}
547
548/**
549 * of_get_flat_dt_root - find the root node in the flat blob
550 */
551unsigned long __init of_get_flat_dt_root(void)
552{
Rob Herringe6a69282014-04-02 15:10:14 -0500553 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800554}
555
556/**
557 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
558 *
559 * This function can be used within scan_flattened_dt callback to get
560 * access to properties
561 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500562const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
563 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800564{
Rob Herringe6a69282014-04-02 15:10:14 -0500565 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800566}
567
568/**
569 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
570 * @node: node to test
571 * @compat: compatible string to compare with compatible list.
572 */
573int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
574{
575 return of_fdt_is_compatible(initial_boot_params, node, compat);
576}
577
Grant Likelya4f740c2010-10-30 11:49:09 -0400578/**
579 * of_flat_dt_match - Return true if node matches a list of compatible values
580 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100581int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400582{
583 return of_fdt_match(initial_boot_params, node, compat);
584}
585
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200586struct fdt_scan_status {
587 const char *name;
588 int namelen;
589 int depth;
590 int found;
591 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
592 void *data;
593};
594
Rob Herring6a903a22013-08-27 21:41:56 -0500595const char * __init of_flat_dt_get_machine_name(void)
596{
597 const char *name;
598 unsigned long dt_root = of_get_flat_dt_root();
599
600 name = of_get_flat_dt_prop(dt_root, "model", NULL);
601 if (!name)
602 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
603 return name;
604}
605
606/**
607 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
608 *
609 * @default_match: A machine specific ptr to return in case of no match.
610 * @get_next_compat: callback function to return next compatible match table.
611 *
612 * Iterate through machine match tables to find the best match for the machine
613 * compatible string in the FDT.
614 */
615const void * __init of_flat_dt_match_machine(const void *default_match,
616 const void * (*get_next_compat)(const char * const**))
617{
618 const void *data = NULL;
619 const void *best_data = default_match;
620 const char *const *compat;
621 unsigned long dt_root;
622 unsigned int best_score = ~1, score = 0;
623
624 dt_root = of_get_flat_dt_root();
625 while ((data = get_next_compat(&compat))) {
626 score = of_flat_dt_match(dt_root, compat);
627 if (score > 0 && score < best_score) {
628 best_data = data;
629 best_score = score;
630 }
631 }
632 if (!best_data) {
633 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500634 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500635
636 pr_err("\n unrecognized device tree list:\n[ ");
637
638 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
639 if (prop) {
640 while (size > 0) {
641 printk("'%s' ", prop);
642 size -= strlen(prop) + 1;
643 prop += strlen(prop) + 1;
644 }
645 }
646 printk("]\n\n");
647 return NULL;
648 }
649
650 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
651
652 return best_data;
653}
654
Grant Likelyf7b3a832009-11-24 03:26:58 -0700655#ifdef CONFIG_BLK_DEV_INITRD
656/**
657 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
658 * @node: reference to node containing initrd location ('chosen')
659 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500660static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700661{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400662 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500663 int len;
664 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700665
666 pr_debug("Looking for initrd properties... ");
667
668 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700669 if (!prop)
670 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400671 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700672
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700673 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
674 if (!prop)
675 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400676 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700677
Rob Herring29eb45a2013-08-30 17:06:53 -0500678 initrd_start = (unsigned long)__va(start);
679 initrd_end = (unsigned long)__va(end);
680 initrd_below_start_ok = 1;
681
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400682 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
683 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700684}
685#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500686static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700687{
688}
689#endif /* CONFIG_BLK_DEV_INITRD */
690
Grant Likely41f88002009-11-23 20:07:01 -0700691/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700692 * early_init_dt_scan_root - fetch the top level address and size cells
693 */
694int __init early_init_dt_scan_root(unsigned long node, const char *uname,
695 int depth, void *data)
696{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500697 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700698
699 if (depth != 0)
700 return 0;
701
Jeremy Kerr33714882010-01-30 01:45:26 -0700702 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
703 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
704
Grant Likelyf00abd92009-11-24 03:27:10 -0700705 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700706 if (prop)
707 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700708 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
709
710 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700711 if (prop)
712 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700713 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
714
715 /* break now */
716 return 1;
717}
718
Rob Herring9d0c4df2014-04-01 23:49:03 -0500719u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700720{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500721 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700722
723 *cellp = p + s;
724 return of_read_number(p, s);
725}
726
Grant Likely51975db2010-02-01 21:34:14 -0700727/**
728 * early_init_dt_scan_memory - Look for an parse memory nodes
729 */
730int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
731 int depth, void *data)
732{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500733 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
734 const __be32 *reg, *endp;
735 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700736
737 /* We are scanning "memory" nodes only */
738 if (type == NULL) {
739 /*
740 * The longtrail doesn't have a device_type on the
741 * /memory node, so look for the node called /memory@0.
742 */
743 if (depth != 1 || strcmp(uname, "memory@0") != 0)
744 return 0;
745 } else if (strcmp(type, "memory") != 0)
746 return 0;
747
748 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
749 if (reg == NULL)
750 reg = of_get_flat_dt_prop(node, "reg", &l);
751 if (reg == NULL)
752 return 0;
753
754 endp = reg + (l / sizeof(__be32));
755
Rob Herring9d0c4df2014-04-01 23:49:03 -0500756 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700757 uname, l, reg[0], reg[1], reg[2], reg[3]);
758
759 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
760 u64 base, size;
761
762 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
763 size = dt_mem_next_cell(dt_root_size_cells, &reg);
764
765 if (size == 0)
766 continue;
767 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
768 (unsigned long long)size);
769
770 early_init_dt_add_memory_arch(base, size);
771 }
772
773 return 0;
774}
775
Grant Likely86e03222009-12-10 23:42:21 -0700776int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
777 int depth, void *data)
778{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500779 int l;
780 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700781
782 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
783
Grant Likely85f60ae2011-04-29 00:18:16 -0600784 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700785 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
786 return 0;
787
788 early_init_dt_check_for_initrd(node);
789
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300790 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700791 p = of_get_flat_dt_prop(node, "bootargs", &l);
792 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600793 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700794
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +0000795 /*
796 * CONFIG_CMDLINE is meant to be a default in case nothing else
797 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
798 * is set in which case we override whatever was found earlier.
799 */
Grant Likely86e03222009-12-10 23:42:21 -0700800#ifdef CONFIG_CMDLINE
801#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782cb2011-09-19 18:50:15 +0000802 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700803#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600804 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700805#endif /* CONFIG_CMDLINE */
806
Grant Likely85f60ae2011-04-29 00:18:16 -0600807 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700808
809 /* break now */
810 return 1;
811}
812
Grant Likelya1727da2013-08-28 21:18:32 +0100813#ifdef CONFIG_HAVE_MEMBLOCK
Rob Herring068f6312013-09-24 22:20:01 -0500814void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
815{
816 const u64 phys_offset = __pa(PAGE_OFFSET);
817 base &= PAGE_MASK;
818 size &= PAGE_MASK;
819 if (base + size < phys_offset) {
820 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
821 base, base + size);
822 return;
823 }
824 if (base < phys_offset) {
825 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
826 base, phys_offset);
827 size -= phys_offset - base;
828 base = phys_offset;
829 }
830 memblock_add(base, size);
831}
832
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100833int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
834 phys_addr_t size, bool nomap)
835{
836 if (memblock_is_region_reserved(base, size))
837 return -EBUSY;
838 if (nomap)
839 return memblock_remove(base, size);
840 return memblock_reserve(base, size);
841}
842
Grant Likelya1727da2013-08-28 21:18:32 +0100843/*
844 * called from unflatten_device_tree() to bootstrap devicetree itself
845 * Architectures can override this definition if memblock isn't used
846 */
847void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
848{
849 return __va(memblock_alloc(size, align));
850}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100851#else
852int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
853 phys_addr_t size, bool nomap)
854{
855 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
856 base, size, nomap ? " (nomap)" : "");
857 return -ENOSYS;
858}
Grant Likelya1727da2013-08-28 21:18:32 +0100859#endif
860
Rob Herring0288ffcb2013-08-26 09:47:40 -0500861bool __init early_init_dt_scan(void *params)
862{
863 if (!params)
864 return false;
865
866 /* Setup flat device-tree pointer */
867 initial_boot_params = params;
868
869 /* check device tree validity */
Rob Herringc972de12014-04-01 22:48:01 -0500870 if (fdt_check_header(params)) {
Rob Herring0288ffcb2013-08-26 09:47:40 -0500871 initial_boot_params = NULL;
872 return false;
873 }
874
875 /* Retrieve various information from the /chosen node */
876 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
877
878 /* Initialize {size,address}-cells info */
879 of_scan_flat_dt(early_init_dt_scan_root, NULL);
880
881 /* Setup memory, calling early_init_dt_add_memory_arch */
882 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
883
884 return true;
885}
886
Grant Likelyf00abd92009-11-24 03:27:10 -0700887/**
Grant Likely41f88002009-11-23 20:07:01 -0700888 * unflatten_device_tree - create tree of device_nodes from flat blob
889 *
890 * unflattens the device-tree passed by the firmware, creating the
891 * tree of struct device_node. It also fills the "name" and "type"
892 * pointers of the nodes so the normal device-tree walking functions
893 * can be used.
894 */
895void __init unflatten_device_tree(void)
896{
Randy Dunlap465aac62012-11-30 10:01:51 +0000897 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700898 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700899
Robert P. J. Day4c7d6362013-05-30 05:38:08 -0400900 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +0800901 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700902}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800903
Rob Herringa8bf7522013-08-26 11:22:45 -0500904/**
905 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
906 *
907 * Copies and unflattens the device-tree passed by the firmware, creating the
908 * tree of struct device_node. It also fills the "name" and "type"
909 * pointers of the nodes so the normal device-tree walking functions
910 * can be used. This should only be used when the FDT memory has not been
911 * reserved such is the case when the FDT is built-in to the kernel init
912 * section. If the FDT memory is reserved already then unflatten_device_tree
913 * should be used instead.
914 */
915void __init unflatten_and_copy_device_tree(void)
916{
James Hogan6f041e92013-11-21 13:44:14 +0000917 int size;
918 void *dt;
919
920 if (!initial_boot_params) {
921 pr_warn("No valid device tree found, continuing without\n");
922 return;
923 }
924
Rob Herringc972de12014-04-01 22:48:01 -0500925 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +0000926 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -0500927 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -0500928
929 if (dt) {
930 memcpy(dt, initial_boot_params, size);
931 initial_boot_params = dt;
932 }
933 unflatten_device_tree();
934}
935
Rob Herringb0a6fb32014-04-02 16:56:48 -0500936#if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
937static struct debugfs_blob_wrapper flat_dt_blob;
938
939static int __init of_flat_dt_debugfs_export_fdt(void)
940{
941 struct dentry *d = debugfs_create_dir("device-tree", NULL);
942
943 if (!d)
944 return -ENOENT;
945
946 flat_dt_blob.data = initial_boot_params;
947 flat_dt_blob.size = fdt_totalsize(initial_boot_params);
948
949 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
950 d, &flat_dt_blob);
951 if (!d)
952 return -ENOENT;
953
954 return 0;
955}
956module_init(of_flat_dt_debugfs_export_fdt);
957#endif
958
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800959#endif /* CONFIG_OF_EARLY_FLATTREE */