blob: 0b38a6aa8603dd9629ebab967db9cfc3dfabd3c9 [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>
Grant Likely51975db2010-02-01 21:34:14 -070023
Fabio Estevamc89810a2012-01-02 14:19:03 -020024#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070025#include <asm/page.h>
26
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080027/**
28 * of_fdt_is_compatible - Return true if given node from the given blob has
29 * compat in its compatible list
30 * @blob: A device tree blob
31 * @node: node to test
32 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040033 *
34 * On match, returns a non-zero value with smaller values returned for more
35 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080036 */
Rob Herringc972de12014-04-01 22:48:01 -050037int of_fdt_is_compatible(const void *blob,
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080038 unsigned long node, const char *compat)
39{
40 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050041 int cplen;
42 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080043
Rob Herringe6a69282014-04-02 15:10:14 -050044 cp = fdt_getprop(blob, node, "compatible", &cplen);
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080045 if (cp == NULL)
46 return 0;
47 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -040048 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080049 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -040050 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080051 l = strlen(cp) + 1;
52 cp += l;
53 cplen -= l;
54 }
55
56 return 0;
57}
58
Grant Likelya4f740c2010-10-30 11:49:09 -040059/**
60 * of_fdt_match - Return true if node matches a list of compatible values
61 */
Rob Herringc972de12014-04-01 22:48:01 -050062int of_fdt_match(const void *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +010063 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -040064{
65 unsigned int tmp, score = 0;
66
67 if (!compat)
68 return 0;
69
70 while (*compat) {
71 tmp = of_fdt_is_compatible(blob, node, *compat);
72 if (tmp && (score == 0 || (tmp < score)))
73 score = tmp;
74 compat++;
75 }
76
77 return score;
78}
79
Grant Likely44856812013-08-29 13:30:35 +010080static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -070081 unsigned long align)
82{
83 void *res;
84
Grant Likely44856812013-08-29 13:30:35 +010085 *mem = PTR_ALIGN(*mem, align);
86 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -070087 *mem += size;
88
89 return res;
90}
91
92/**
93 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -080094 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -070095 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -070096 * @p: pointer to node in flat tree
97 * @dad: Parent struct device_node
98 * @allnextpp: pointer to ->allnext from last allocated device_node
99 * @fpsize: Size of the node path up at the current depth.
100 */
Rob Herringc972de12014-04-01 22:48:01 -0500101static void * unflatten_dt_node(void *blob,
Grant Likely44856812013-08-29 13:30:35 +0100102 void *mem,
Rob Herringe6a69282014-04-02 15:10:14 -0500103 int *poffset,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800104 struct device_node *dad,
105 struct device_node ***allnextpp,
106 unsigned long fpsize)
Grant Likelybbd33932009-11-23 20:07:00 -0700107{
Rob Herringe6a69282014-04-02 15:10:14 -0500108 const __be32 *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700109 struct device_node *np;
110 struct property *pp, **prev_pp = NULL;
Rob Herringe6a69282014-04-02 15:10:14 -0500111 const char *pathp;
Grant Likelybbd33932009-11-23 20:07:00 -0700112 unsigned int l, allocl;
Rob Herringe6a69282014-04-02 15:10:14 -0500113 static int depth = 0;
114 int old_depth;
115 int offset;
Grant Likelybbd33932009-11-23 20:07:00 -0700116 int has_name = 0;
117 int new_format = 0;
118
Rob Herringe6a69282014-04-02 15:10:14 -0500119 pathp = fdt_get_name(blob, *poffset, &l);
120 if (!pathp)
Grant Likelybbd33932009-11-23 20:07:00 -0700121 return mem;
Rob Herringe6a69282014-04-02 15:10:14 -0500122
123 allocl = l++;
Grant Likelybbd33932009-11-23 20:07:00 -0700124
125 /* version 0x10 has a more compact unit name here instead of the full
126 * path. we accumulate the full path size using "fpsize", we'll rebuild
127 * it later. We detect this because the first character of the name is
128 * not '/'.
129 */
130 if ((*pathp) != '/') {
131 new_format = 1;
132 if (fpsize == 0) {
133 /* root node: special case. fpsize accounts for path
134 * plus terminating zero. root node only has '/', so
135 * fpsize should be 2, but we want to avoid the first
136 * level nodes to have two '/' so we use fpsize 1 here
137 */
138 fpsize = 1;
139 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000140 l = 1;
Rob Herringe6a69282014-04-02 15:10:14 -0500141 pathp = "";
Grant Likelybbd33932009-11-23 20:07:00 -0700142 } else {
143 /* account for '/' and path size minus terminal 0
144 * already in 'l'
145 */
146 fpsize += l;
147 allocl = fpsize;
148 }
149 }
150
151 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
152 __alignof__(struct device_node));
153 if (allnextpp) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000154 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200155 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000156 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700157 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700158 /* rebuild full path for new format */
159 if (dad && dad->parent) {
160 strcpy(fn, dad->full_name);
161#ifdef DEBUG
162 if ((strlen(fn) + l + 1) != allocl) {
163 pr_debug("%s: p: %d, l: %d, a: %d\n",
164 pathp, (int)strlen(fn),
165 l, allocl);
166 }
167#endif
168 fn += strlen(fn);
169 }
170 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000171 }
172 memcpy(fn, pathp, l);
173
Grant Likelybbd33932009-11-23 20:07:00 -0700174 prev_pp = &np->properties;
175 **allnextpp = np;
176 *allnextpp = &np->allnext;
177 if (dad != NULL) {
178 np->parent = dad;
179 /* we temporarily use the next field as `last_child'*/
180 if (dad->next == NULL)
181 dad->child = np;
182 else
183 dad->next->sibling = np;
184 dad->next = np;
185 }
Grant Likelybbd33932009-11-23 20:07:00 -0700186 }
Andres Salomona7006c92011-03-17 17:32:35 -0700187 /* process properties */
Rob Herringe6a69282014-04-02 15:10:14 -0500188 for (offset = fdt_first_property_offset(blob, *poffset);
189 (offset >= 0);
190 (offset = fdt_next_property_offset(blob, offset))) {
191 const char *pname;
192 u32 sz;
Grant Likelybbd33932009-11-23 20:07:00 -0700193
Rob Herringe6a69282014-04-02 15:10:14 -0500194 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
195 offset = -FDT_ERR_INTERNAL;
Grant Likelybbd33932009-11-23 20:07:00 -0700196 break;
Rob Herringe6a69282014-04-02 15:10:14 -0500197 }
Grant Likelybbd33932009-11-23 20:07:00 -0700198
Grant Likelybbd33932009-11-23 20:07:00 -0700199 if (pname == NULL) {
200 pr_info("Can't find property name in list !\n");
201 break;
202 }
203 if (strcmp(pname, "name") == 0)
204 has_name = 1;
Grant Likelybbd33932009-11-23 20:07:00 -0700205 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
206 __alignof__(struct property));
207 if (allnextpp) {
David Gibson04b954a2010-02-01 21:34:15 -0700208 /* We accept flattened tree phandles either in
209 * ePAPR-style "phandle" properties, or the
210 * legacy "linux,phandle" properties. If both
211 * appear and have different values, things
212 * will get weird. Don't do that. */
213 if ((strcmp(pname, "phandle") == 0) ||
214 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700215 if (np->phandle == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500216 np->phandle = be32_to_cpup(p);
Grant Likelybbd33932009-11-23 20:07:00 -0700217 }
David Gibson04b954a2010-02-01 21:34:15 -0700218 /* And we process the "ibm,phandle" property
219 * used in pSeries dynamic device tree
220 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700221 if (strcmp(pname, "ibm,phandle") == 0)
Rob Herringe6a69282014-04-02 15:10:14 -0500222 np->phandle = be32_to_cpup(p);
223 pp->name = (char *)pname;
Grant Likelybbd33932009-11-23 20:07:00 -0700224 pp->length = sz;
Rob Herringe6a69282014-04-02 15:10:14 -0500225 pp->value = (__be32 *)p;
Grant Likelybbd33932009-11-23 20:07:00 -0700226 *prev_pp = pp;
227 prev_pp = &pp->next;
228 }
Grant Likelybbd33932009-11-23 20:07:00 -0700229 }
230 /* with version 0x10 we may not have the name property, recreate
231 * it here from the unit name if absent
232 */
233 if (!has_name) {
Rob Herringe6a69282014-04-02 15:10:14 -0500234 const char *p1 = pathp, *ps = pathp, *pa = NULL;
Grant Likelybbd33932009-11-23 20:07:00 -0700235 int sz;
236
237 while (*p1) {
238 if ((*p1) == '@')
239 pa = p1;
240 if ((*p1) == '/')
241 ps = p1 + 1;
242 p1++;
243 }
244 if (pa < ps)
245 pa = p1;
246 sz = (pa - ps) + 1;
247 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
248 __alignof__(struct property));
249 if (allnextpp) {
250 pp->name = "name";
251 pp->length = sz;
252 pp->value = pp + 1;
253 *prev_pp = pp;
254 prev_pp = &pp->next;
255 memcpy(pp->value, ps, sz - 1);
256 ((char *)pp->value)[sz - 1] = 0;
257 pr_debug("fixed up name for %s -> %s\n", pathp,
258 (char *)pp->value);
259 }
260 }
261 if (allnextpp) {
262 *prev_pp = NULL;
263 np->name = of_get_property(np, "name", NULL);
264 np->type = of_get_property(np, "device_type", NULL);
265
266 if (!np->name)
267 np->name = "<NULL>";
268 if (!np->type)
269 np->type = "<NULL>";
270 }
Rob Herringe6a69282014-04-02 15:10:14 -0500271
272 old_depth = depth;
273 *poffset = fdt_next_node(blob, *poffset, &depth);
274 if (depth < 0)
275 depth = 0;
276 while (*poffset > 0 && depth > old_depth)
277 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
278 fpsize);
279
280 if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
281 pr_err("unflatten: error %d processing FDT\n", *poffset);
282
Grant Likelybbd33932009-11-23 20:07:00 -0700283 return mem;
284}
Grant Likely41f88002009-11-23 20:07:01 -0700285
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800286/**
287 * __unflatten_device_tree - create tree of device_nodes from flat blob
288 *
289 * unflattens a device-tree, creating the
290 * tree of struct device_node. It also fills the "name" and "type"
291 * pointers of the nodes so the normal device-tree walking functions
292 * can be used.
293 * @blob: The blob to expand
294 * @mynodes: The device_node tree created by the call
295 * @dt_alloc: An allocator that provides a virtual address to memory
296 * for the resulting tree
297 */
Rob Herringc972de12014-04-01 22:48:01 -0500298static void __unflatten_device_tree(void *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800299 struct device_node **mynodes,
300 void * (*dt_alloc)(u64 size, u64 align))
301{
Grant Likely44856812013-08-29 13:30:35 +0100302 unsigned long size;
Rob Herringe6a69282014-04-02 15:10:14 -0500303 int start;
304 void *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800305 struct device_node **allnextp = mynodes;
306
307 pr_debug(" -> unflatten_device_tree()\n");
308
309 if (!blob) {
310 pr_debug("No device tree pointer\n");
311 return;
312 }
313
314 pr_debug("Unflattening device tree:\n");
Rob Herringc972de12014-04-01 22:48:01 -0500315 pr_debug("magic: %08x\n", fdt_magic(blob));
316 pr_debug("size: %08x\n", fdt_totalsize(blob));
317 pr_debug("version: %08x\n", fdt_version(blob));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800318
Rob Herringc972de12014-04-01 22:48:01 -0500319 if (fdt_check_header(blob)) {
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800320 pr_err("Invalid device tree blob header\n");
321 return;
322 }
323
324 /* First pass, scan for size */
Rob Herringe6a69282014-04-02 15:10:14 -0500325 start = 0;
Grant Likely44856812013-08-29 13:30:35 +0100326 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
327 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800328
329 pr_debug(" size is %lx, allocating...\n", size);
330
331 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100332 mem = dt_alloc(size + 4, __alignof__(struct device_node));
333 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800334
Grant Likely44856812013-08-29 13:30:35 +0100335 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200336
Grant Likely44856812013-08-29 13:30:35 +0100337 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800338
339 /* Second pass, do actual unflattening */
Rob Herringe6a69282014-04-02 15:10:14 -0500340 start = 0;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800341 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
Grant Likely44856812013-08-29 13:30:35 +0100342 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800343 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100344 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800345 *allnextp = NULL;
346
347 pr_debug(" <- unflatten_device_tree()\n");
348}
349
350static void *kernel_tree_alloc(u64 size, u64 align)
351{
352 return kzalloc(size, GFP_KERNEL);
353}
354
355/**
356 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
357 *
358 * unflattens the device-tree passed by the firmware, creating the
359 * tree of struct device_node. It also fills the "name" and "type"
360 * pointers of the nodes so the normal device-tree walking functions
361 * can be used.
362 */
363void of_fdt_unflatten_tree(unsigned long *blob,
364 struct device_node **mynodes)
365{
Rob Herringc972de12014-04-01 22:48:01 -0500366 __unflatten_device_tree(blob, mynodes, &kernel_tree_alloc);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800367}
368EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
369
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800370/* Everything below here references initial_boot_params directly. */
371int __initdata dt_root_addr_cells;
372int __initdata dt_root_size_cells;
373
374struct boot_param_header *initial_boot_params;
375
376#ifdef CONFIG_OF_EARLY_FLATTREE
377
378/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100379 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
380 */
381static int __init __reserved_mem_reserve_reg(unsigned long node,
382 const char *uname)
383{
384 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
385 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500386 int len;
387 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100388 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100389
390 prop = of_get_flat_dt_prop(node, "reg", &len);
391 if (!prop)
392 return -ENOENT;
393
394 if (len && len % t_len != 0) {
395 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
396 uname);
397 return -EINVAL;
398 }
399
400 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
401
402 while (len >= t_len) {
403 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
404 size = dt_mem_next_cell(dt_root_size_cells, &prop);
405
406 if (base && size &&
407 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
408 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
409 uname, &base, (unsigned long)size / SZ_1M);
410 else
411 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
412 uname, &base, (unsigned long)size / SZ_1M);
413
414 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100415 if (first) {
416 fdt_reserved_mem_save_node(node, uname, base, size);
417 first = 0;
418 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100419 }
420 return 0;
421}
422
423/**
424 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
425 * in /reserved-memory matches the values supported by the current implementation,
426 * also check if ranges property has been provided
427 */
Xiubo Li5b624112014-04-08 13:48:07 +0800428static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100429{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500430 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100431
432 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
433 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
434 return -EINVAL;
435
436 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
437 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
438 return -EINVAL;
439
440 prop = of_get_flat_dt_prop(node, "ranges", NULL);
441 if (!prop)
442 return -EINVAL;
443 return 0;
444}
445
446/**
447 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
448 */
449static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
450 int depth, void *data)
451{
452 static int found;
453 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100454 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100455
456 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
457 if (__reserved_mem_check_root(node) != 0) {
458 pr_err("Reserved memory: unsupported node format, ignoring\n");
459 /* break scan */
460 return 1;
461 }
462 found = 1;
463 /* scan next node */
464 return 0;
465 } else if (!found) {
466 /* scan next node */
467 return 0;
468 } else if (found && depth < 2) {
469 /* scanning of /reserved-memory has been finished */
470 return 1;
471 }
472
473 status = of_get_flat_dt_prop(node, "status", NULL);
474 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
475 return 0;
476
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100477 err = __reserved_mem_reserve_reg(node, uname);
478 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
479 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100480
481 /* scan next node */
482 return 0;
483}
484
485/**
486 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
487 *
488 * This function grabs memory from early allocator for device exclusive use
489 * defined in device tree structures. It should be called by arch specific code
490 * once the early allocator (i.e. memblock) has been fully activated.
491 */
492void __init early_init_fdt_scan_reserved_mem(void)
493{
Josh Cartwright2040b522014-03-13 16:36:36 -0500494 if (!initial_boot_params)
495 return;
496
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100497 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100498 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100499}
500
501/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800502 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
503 * @it: callback function
504 * @data: context data pointer
505 *
506 * This function is used to scan the flattened device-tree, it is
507 * used to extract the memory information at boot before we can
508 * unflatten the tree
509 */
510int __init of_scan_flat_dt(int (*it)(unsigned long node,
511 const char *uname, int depth,
512 void *data),
513 void *data)
514{
Rob Herringe6a69282014-04-02 15:10:14 -0500515 const void *blob = initial_boot_params;
516 const char *pathp;
517 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800518
Rob Herringe6a69282014-04-02 15:10:14 -0500519 for (offset = fdt_next_node(blob, -1, &depth);
520 offset >= 0 && depth >= 0 && !rc;
521 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800522
Rob Herringe6a69282014-04-02 15:10:14 -0500523 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800524 if (*pathp == '/')
525 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500526 rc = it(offset, pathp, depth, data);
527 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800528 return rc;
529}
530
531/**
532 * of_get_flat_dt_root - find the root node in the flat blob
533 */
534unsigned long __init of_get_flat_dt_root(void)
535{
Rob Herringe6a69282014-04-02 15:10:14 -0500536 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800537}
538
539/**
540 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
541 *
542 * This function can be used within scan_flattened_dt callback to get
543 * access to properties
544 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500545const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
546 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800547{
Rob Herringe6a69282014-04-02 15:10:14 -0500548 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800549}
550
551/**
552 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
553 * @node: node to test
554 * @compat: compatible string to compare with compatible list.
555 */
556int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
557{
558 return of_fdt_is_compatible(initial_boot_params, node, compat);
559}
560
Grant Likelya4f740c2010-10-30 11:49:09 -0400561/**
562 * of_flat_dt_match - Return true if node matches a list of compatible values
563 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100564int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400565{
566 return of_fdt_match(initial_boot_params, node, compat);
567}
568
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200569struct fdt_scan_status {
570 const char *name;
571 int namelen;
572 int depth;
573 int found;
574 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
575 void *data;
576};
577
Rob Herring6a903a22013-08-27 21:41:56 -0500578const char * __init of_flat_dt_get_machine_name(void)
579{
580 const char *name;
581 unsigned long dt_root = of_get_flat_dt_root();
582
583 name = of_get_flat_dt_prop(dt_root, "model", NULL);
584 if (!name)
585 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
586 return name;
587}
588
589/**
590 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
591 *
592 * @default_match: A machine specific ptr to return in case of no match.
593 * @get_next_compat: callback function to return next compatible match table.
594 *
595 * Iterate through machine match tables to find the best match for the machine
596 * compatible string in the FDT.
597 */
598const void * __init of_flat_dt_match_machine(const void *default_match,
599 const void * (*get_next_compat)(const char * const**))
600{
601 const void *data = NULL;
602 const void *best_data = default_match;
603 const char *const *compat;
604 unsigned long dt_root;
605 unsigned int best_score = ~1, score = 0;
606
607 dt_root = of_get_flat_dt_root();
608 while ((data = get_next_compat(&compat))) {
609 score = of_flat_dt_match(dt_root, compat);
610 if (score > 0 && score < best_score) {
611 best_data = data;
612 best_score = score;
613 }
614 }
615 if (!best_data) {
616 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500617 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500618
619 pr_err("\n unrecognized device tree list:\n[ ");
620
621 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
622 if (prop) {
623 while (size > 0) {
624 printk("'%s' ", prop);
625 size -= strlen(prop) + 1;
626 prop += strlen(prop) + 1;
627 }
628 }
629 printk("]\n\n");
630 return NULL;
631 }
632
633 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
634
635 return best_data;
636}
637
Grant Likelyf7b3a832009-11-24 03:26:58 -0700638#ifdef CONFIG_BLK_DEV_INITRD
639/**
640 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
641 * @node: reference to node containing initrd location ('chosen')
642 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500643static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700644{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400645 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500646 int len;
647 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700648
649 pr_debug("Looking for initrd properties... ");
650
651 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700652 if (!prop)
653 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400654 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700655
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700656 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
657 if (!prop)
658 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400659 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700660
Rob Herring29eb45a2013-08-30 17:06:53 -0500661 initrd_start = (unsigned long)__va(start);
662 initrd_end = (unsigned long)__va(end);
663 initrd_below_start_ok = 1;
664
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400665 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
666 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700667}
668#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500669static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700670{
671}
672#endif /* CONFIG_BLK_DEV_INITRD */
673
Grant Likely41f88002009-11-23 20:07:01 -0700674/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700675 * early_init_dt_scan_root - fetch the top level address and size cells
676 */
677int __init early_init_dt_scan_root(unsigned long node, const char *uname,
678 int depth, void *data)
679{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500680 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700681
682 if (depth != 0)
683 return 0;
684
Jeremy Kerr33714882010-01-30 01:45:26 -0700685 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
686 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
687
Grant Likelyf00abd92009-11-24 03:27:10 -0700688 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700689 if (prop)
690 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700691 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
692
693 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700694 if (prop)
695 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700696 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
697
698 /* break now */
699 return 1;
700}
701
Rob Herring9d0c4df2014-04-01 23:49:03 -0500702u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700703{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500704 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700705
706 *cellp = p + s;
707 return of_read_number(p, s);
708}
709
Grant Likely51975db2010-02-01 21:34:14 -0700710/**
711 * early_init_dt_scan_memory - Look for an parse memory nodes
712 */
713int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
714 int depth, void *data)
715{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500716 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
717 const __be32 *reg, *endp;
718 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700719
720 /* We are scanning "memory" nodes only */
721 if (type == NULL) {
722 /*
723 * The longtrail doesn't have a device_type on the
724 * /memory node, so look for the node called /memory@0.
725 */
726 if (depth != 1 || strcmp(uname, "memory@0") != 0)
727 return 0;
728 } else if (strcmp(type, "memory") != 0)
729 return 0;
730
731 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
732 if (reg == NULL)
733 reg = of_get_flat_dt_prop(node, "reg", &l);
734 if (reg == NULL)
735 return 0;
736
737 endp = reg + (l / sizeof(__be32));
738
Rob Herring9d0c4df2014-04-01 23:49:03 -0500739 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700740 uname, l, reg[0], reg[1], reg[2], reg[3]);
741
742 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
743 u64 base, size;
744
745 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
746 size = dt_mem_next_cell(dt_root_size_cells, &reg);
747
748 if (size == 0)
749 continue;
750 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
751 (unsigned long long)size);
752
753 early_init_dt_add_memory_arch(base, size);
754 }
755
756 return 0;
757}
758
Grant Likely86e03222009-12-10 23:42:21 -0700759int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
760 int depth, void *data)
761{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500762 int l;
763 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700764
765 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
766
Grant Likely85f60ae2011-04-29 00:18:16 -0600767 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700768 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
769 return 0;
770
771 early_init_dt_check_for_initrd(node);
772
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300773 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700774 p = of_get_flat_dt_prop(node, "bootargs", &l);
775 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600776 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700777
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000778 /*
779 * CONFIG_CMDLINE is meant to be a default in case nothing else
780 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
781 * is set in which case we override whatever was found earlier.
782 */
Grant Likely86e03222009-12-10 23:42:21 -0700783#ifdef CONFIG_CMDLINE
784#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000785 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700786#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600787 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700788#endif /* CONFIG_CMDLINE */
789
Grant Likely85f60ae2011-04-29 00:18:16 -0600790 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700791
792 /* break now */
793 return 1;
794}
795
Grant Likelya1727da2013-08-28 21:18:32 +0100796#ifdef CONFIG_HAVE_MEMBLOCK
Rob Herring068f6312013-09-24 22:20:01 -0500797void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
798{
799 const u64 phys_offset = __pa(PAGE_OFFSET);
800 base &= PAGE_MASK;
801 size &= PAGE_MASK;
802 if (base + size < phys_offset) {
803 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
804 base, base + size);
805 return;
806 }
807 if (base < phys_offset) {
808 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
809 base, phys_offset);
810 size -= phys_offset - base;
811 base = phys_offset;
812 }
813 memblock_add(base, size);
814}
815
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100816int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
817 phys_addr_t size, bool nomap)
818{
819 if (memblock_is_region_reserved(base, size))
820 return -EBUSY;
821 if (nomap)
822 return memblock_remove(base, size);
823 return memblock_reserve(base, size);
824}
825
Grant Likelya1727da2013-08-28 21:18:32 +0100826/*
827 * called from unflatten_device_tree() to bootstrap devicetree itself
828 * Architectures can override this definition if memblock isn't used
829 */
830void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
831{
832 return __va(memblock_alloc(size, align));
833}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100834#else
835int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
836 phys_addr_t size, bool nomap)
837{
838 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
839 base, size, nomap ? " (nomap)" : "");
840 return -ENOSYS;
841}
Grant Likelya1727da2013-08-28 21:18:32 +0100842#endif
843
Rob Herring0288ffcb2013-08-26 09:47:40 -0500844bool __init early_init_dt_scan(void *params)
845{
846 if (!params)
847 return false;
848
849 /* Setup flat device-tree pointer */
850 initial_boot_params = params;
851
852 /* check device tree validity */
Rob Herringc972de12014-04-01 22:48:01 -0500853 if (fdt_check_header(params)) {
Rob Herring0288ffcb2013-08-26 09:47:40 -0500854 initial_boot_params = NULL;
855 return false;
856 }
857
858 /* Retrieve various information from the /chosen node */
859 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
860
861 /* Initialize {size,address}-cells info */
862 of_scan_flat_dt(early_init_dt_scan_root, NULL);
863
864 /* Setup memory, calling early_init_dt_add_memory_arch */
865 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
866
867 return true;
868}
869
Grant Likelyf00abd92009-11-24 03:27:10 -0700870/**
Grant Likely41f88002009-11-23 20:07:01 -0700871 * unflatten_device_tree - create tree of device_nodes from flat blob
872 *
873 * unflattens the device-tree passed by the firmware, creating the
874 * tree of struct device_node. It also fills the "name" and "type"
875 * pointers of the nodes so the normal device-tree walking functions
876 * can be used.
877 */
878void __init unflatten_device_tree(void)
879{
Randy Dunlap465aac62012-11-30 10:01:51 +0000880 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700881 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700882
Robert P. J. Day4c7d6362013-05-30 05:38:08 -0400883 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +0800884 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700885}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800886
Rob Herringa8bf7522013-08-26 11:22:45 -0500887/**
888 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
889 *
890 * Copies and 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. This should only be used when the FDT memory has not been
894 * reserved such is the case when the FDT is built-in to the kernel init
895 * section. If the FDT memory is reserved already then unflatten_device_tree
896 * should be used instead.
897 */
898void __init unflatten_and_copy_device_tree(void)
899{
James Hogan6f041e92013-11-21 13:44:14 +0000900 int size;
901 void *dt;
902
903 if (!initial_boot_params) {
904 pr_warn("No valid device tree found, continuing without\n");
905 return;
906 }
907
Rob Herringc972de12014-04-01 22:48:01 -0500908 size = fdt_totalsize(initial_boot_params);
James Hogan6f041e92013-11-21 13:44:14 +0000909 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringc972de12014-04-01 22:48:01 -0500910 roundup_pow_of_two(FDT_V17_SIZE));
Rob Herringa8bf7522013-08-26 11:22:45 -0500911
912 if (dt) {
913 memcpy(dt, initial_boot_params, size);
914 initial_boot_params = dt;
915 }
916 unflatten_device_tree();
917}
918
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800919#endif /* CONFIG_OF_EARLY_FLATTREE */