blob: a6f83ea107ae03f8c1d71db45548a015dca6794b [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
Rob Herring1daa0c42014-03-31 15:25:04 -0500375void *initial_boot_params;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800376
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/**
Rob Herringc0556d32014-04-22 12:55:10 -0500557 * of_get_flat_dt_size - Return the total size of the FDT
558 */
559int __init of_get_flat_dt_size(void)
560{
561 return fdt_totalsize(initial_boot_params);
562}
563
564/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800565 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
566 *
567 * This function can be used within scan_flattened_dt callback to get
568 * access to properties
569 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500570const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
571 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800572{
Rob Herringe6a69282014-04-02 15:10:14 -0500573 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800574}
575
576/**
577 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
578 * @node: node to test
579 * @compat: compatible string to compare with compatible list.
580 */
581int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
582{
583 return of_fdt_is_compatible(initial_boot_params, node, compat);
584}
585
Grant Likelya4f740c2010-10-30 11:49:09 -0400586/**
587 * of_flat_dt_match - Return true if node matches a list of compatible values
588 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100589int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400590{
591 return of_fdt_match(initial_boot_params, node, compat);
592}
593
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200594struct fdt_scan_status {
595 const char *name;
596 int namelen;
597 int depth;
598 int found;
599 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
600 void *data;
601};
602
Rob Herring6a903a22013-08-27 21:41:56 -0500603const char * __init of_flat_dt_get_machine_name(void)
604{
605 const char *name;
606 unsigned long dt_root = of_get_flat_dt_root();
607
608 name = of_get_flat_dt_prop(dt_root, "model", NULL);
609 if (!name)
610 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
611 return name;
612}
613
614/**
615 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
616 *
617 * @default_match: A machine specific ptr to return in case of no match.
618 * @get_next_compat: callback function to return next compatible match table.
619 *
620 * Iterate through machine match tables to find the best match for the machine
621 * compatible string in the FDT.
622 */
623const void * __init of_flat_dt_match_machine(const void *default_match,
624 const void * (*get_next_compat)(const char * const**))
625{
626 const void *data = NULL;
627 const void *best_data = default_match;
628 const char *const *compat;
629 unsigned long dt_root;
630 unsigned int best_score = ~1, score = 0;
631
632 dt_root = of_get_flat_dt_root();
633 while ((data = get_next_compat(&compat))) {
634 score = of_flat_dt_match(dt_root, compat);
635 if (score > 0 && score < best_score) {
636 best_data = data;
637 best_score = score;
638 }
639 }
640 if (!best_data) {
641 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500642 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500643
644 pr_err("\n unrecognized device tree list:\n[ ");
645
646 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
647 if (prop) {
648 while (size > 0) {
649 printk("'%s' ", prop);
650 size -= strlen(prop) + 1;
651 prop += strlen(prop) + 1;
652 }
653 }
654 printk("]\n\n");
655 return NULL;
656 }
657
658 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
659
660 return best_data;
661}
662
Grant Likelyf7b3a832009-11-24 03:26:58 -0700663#ifdef CONFIG_BLK_DEV_INITRD
664/**
665 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
666 * @node: reference to node containing initrd location ('chosen')
667 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500668static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700669{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400670 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500671 int len;
672 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700673
674 pr_debug("Looking for initrd properties... ");
675
676 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700677 if (!prop)
678 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400679 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700680
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700681 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
682 if (!prop)
683 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400684 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700685
Rob Herring29eb45a2013-08-30 17:06:53 -0500686 initrd_start = (unsigned long)__va(start);
687 initrd_end = (unsigned long)__va(end);
688 initrd_below_start_ok = 1;
689
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400690 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
691 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700692}
693#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500694static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700695{
696}
697#endif /* CONFIG_BLK_DEV_INITRD */
698
Grant Likely41f88002009-11-23 20:07:01 -0700699/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700700 * early_init_dt_scan_root - fetch the top level address and size cells
701 */
702int __init early_init_dt_scan_root(unsigned long node, const char *uname,
703 int depth, void *data)
704{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500705 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700706
707 if (depth != 0)
708 return 0;
709
Jeremy Kerr33714882010-01-30 01:45:26 -0700710 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
711 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
712
Grant Likelyf00abd92009-11-24 03:27:10 -0700713 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700714 if (prop)
715 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700716 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
717
718 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700719 if (prop)
720 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700721 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
722
723 /* break now */
724 return 1;
725}
726
Rob Herring9d0c4df2014-04-01 23:49:03 -0500727u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700728{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500729 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700730
731 *cellp = p + s;
732 return of_read_number(p, s);
733}
734
Grant Likely51975db2010-02-01 21:34:14 -0700735/**
736 * early_init_dt_scan_memory - Look for an parse memory nodes
737 */
738int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
739 int depth, void *data)
740{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500741 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
742 const __be32 *reg, *endp;
743 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700744
745 /* We are scanning "memory" nodes only */
746 if (type == NULL) {
747 /*
748 * The longtrail doesn't have a device_type on the
749 * /memory node, so look for the node called /memory@0.
750 */
751 if (depth != 1 || strcmp(uname, "memory@0") != 0)
752 return 0;
753 } else if (strcmp(type, "memory") != 0)
754 return 0;
755
756 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
757 if (reg == NULL)
758 reg = of_get_flat_dt_prop(node, "reg", &l);
759 if (reg == NULL)
760 return 0;
761
762 endp = reg + (l / sizeof(__be32));
763
Rob Herring9d0c4df2014-04-01 23:49:03 -0500764 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700765 uname, l, reg[0], reg[1], reg[2], reg[3]);
766
767 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
768 u64 base, size;
769
770 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
771 size = dt_mem_next_cell(dt_root_size_cells, &reg);
772
773 if (size == 0)
774 continue;
775 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
776 (unsigned long long)size);
777
778 early_init_dt_add_memory_arch(base, size);
779 }
780
781 return 0;
782}
783
Grant Likely86e03222009-12-10 23:42:21 -0700784int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
785 int depth, void *data)
786{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500787 int l;
788 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700789
790 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
791
Grant Likely85f60ae2011-04-29 00:18:16 -0600792 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700793 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
794 return 0;
795
796 early_init_dt_check_for_initrd(node);
797
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300798 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700799 p = of_get_flat_dt_prop(node, "bootargs", &l);
800 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600801 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700802
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000803 /*
804 * CONFIG_CMDLINE is meant to be a default in case nothing else
805 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
806 * is set in which case we override whatever was found earlier.
807 */
Grant Likely86e03222009-12-10 23:42:21 -0700808#ifdef CONFIG_CMDLINE
809#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000810 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700811#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600812 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700813#endif /* CONFIG_CMDLINE */
814
Grant Likely85f60ae2011-04-29 00:18:16 -0600815 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700816
817 /* break now */
818 return 1;
819}
820
Grant Likelya1727da2013-08-28 21:18:32 +0100821#ifdef CONFIG_HAVE_MEMBLOCK
Rob Herring068f6312013-09-24 22:20:01 -0500822void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
823{
824 const u64 phys_offset = __pa(PAGE_OFFSET);
825 base &= PAGE_MASK;
826 size &= PAGE_MASK;
827 if (base + size < phys_offset) {
828 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
829 base, base + size);
830 return;
831 }
832 if (base < phys_offset) {
833 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
834 base, phys_offset);
835 size -= phys_offset - base;
836 base = phys_offset;
837 }
838 memblock_add(base, size);
839}
840
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100841int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
842 phys_addr_t size, bool nomap)
843{
844 if (memblock_is_region_reserved(base, size))
845 return -EBUSY;
846 if (nomap)
847 return memblock_remove(base, size);
848 return memblock_reserve(base, size);
849}
850
Grant Likelya1727da2013-08-28 21:18:32 +0100851/*
852 * called from unflatten_device_tree() to bootstrap devicetree itself
853 * Architectures can override this definition if memblock isn't used
854 */
855void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
856{
857 return __va(memblock_alloc(size, align));
858}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100859#else
860int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
861 phys_addr_t size, bool nomap)
862{
Rob Herring1d1a6612014-04-22 12:50:24 -0500863 pr_err("Reserved memory not supported, ignoring range 0x%pa - 0x%pa%s\n",
864 &base, &size, nomap ? " (nomap)" : "");
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100865 return -ENOSYS;
866}
Grant Likelya1727da2013-08-28 21:18:32 +0100867#endif
868
Rob Herring0288ffcb2013-08-26 09:47:40 -0500869bool __init early_init_dt_scan(void *params)
870{
871 if (!params)
872 return false;
873
874 /* Setup flat device-tree pointer */
875 initial_boot_params = params;
876
877 /* check device tree validity */
Rob Herringc972de12014-04-01 22:48:01 -0500878 if (fdt_check_header(params)) {
Rob Herring0288ffcb2013-08-26 09:47:40 -0500879 initial_boot_params = NULL;
880 return false;
881 }
882
883 /* Retrieve various information from the /chosen node */
884 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
885
886 /* Initialize {size,address}-cells info */
887 of_scan_flat_dt(early_init_dt_scan_root, NULL);
888
889 /* Setup memory, calling early_init_dt_add_memory_arch */
890 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
891
892 return true;
893}
894
Grant Likelyf00abd92009-11-24 03:27:10 -0700895/**
Grant Likely41f88002009-11-23 20:07:01 -0700896 * unflatten_device_tree - create tree of device_nodes from flat blob
897 *
898 * unflattens the device-tree passed by the firmware, creating the
899 * tree of struct device_node. It also fills the "name" and "type"
900 * pointers of the nodes so the normal device-tree walking functions
901 * can be used.
902 */
903void __init unflatten_device_tree(void)
904{
Randy Dunlap465aac62012-11-30 10:01:51 +0000905 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700906 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700907
Robert P. J. Day4c7d6362013-05-30 05:38:08 -0400908 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +0800909 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700910}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800911
Rob Herringa8bf7522013-08-26 11:22:45 -0500912/**
913 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
914 *
915 * Copies and unflattens the device-tree passed by the firmware, creating the
916 * tree of struct device_node. It also fills the "name" and "type"
917 * pointers of the nodes so the normal device-tree walking functions
918 * can be used. This should only be used when the FDT memory has not been
919 * reserved such is the case when the FDT is built-in to the kernel init
920 * section. If the FDT memory is reserved already then unflatten_device_tree
921 * should be used instead.
922 */
923void __init unflatten_and_copy_device_tree(void)
924{
James Hogan6f041e92013-11-21 13:44:14 +0000925 int size;
926 void *dt;
927
928 if (!initial_boot_params) {
929 pr_warn("No valid device tree found, continuing without\n");
930 return;
931 }
932
Rob Herringc972de12014-04-01 22:48:01 -0500933 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +0000934 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -0500935 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -0500936
937 if (dt) {
938 memcpy(dt, initial_boot_params, size);
939 initial_boot_params = dt;
940 }
941 unflatten_device_tree();
942}
943
Rob Herringb0a6fb32014-04-02 16:56:48 -0500944#if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
945static struct debugfs_blob_wrapper flat_dt_blob;
946
947static int __init of_flat_dt_debugfs_export_fdt(void)
948{
949 struct dentry *d = debugfs_create_dir("device-tree", NULL);
950
951 if (!d)
952 return -ENOENT;
953
954 flat_dt_blob.data = initial_boot_params;
955 flat_dt_blob.size = fdt_totalsize(initial_boot_params);
956
957 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
958 d, &flat_dt_blob);
959 if (!d)
960 return -ENOENT;
961
962 return 0;
963}
964module_init(of_flat_dt_debugfs_export_fdt);
965#endif
966
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800967#endif /* CONFIG_OF_EARLY_FLATTREE */