blob: 8e820a2b106d5c999869c45703fb2870483c46b4 [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 */
37int of_fdt_is_compatible(struct boot_param_header *blob,
38 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 */
62int of_fdt_match(struct boot_param_header *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 */
Grant Likely44856812013-08-29 13:30:35 +0100101static void * unflatten_dt_node(struct boot_param_header *blob,
102 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 */
Andres Salomona7006c92011-03-17 17:32:35 -0700298static void __unflatten_device_tree(struct boot_param_header *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");
315 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
316 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
317 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
318
319 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
320 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{
366 struct boot_param_header *device_tree =
367 (struct boot_param_header *)blob;
368 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
369}
370EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
371
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800372/* Everything below here references initial_boot_params directly. */
373int __initdata dt_root_addr_cells;
374int __initdata dt_root_size_cells;
375
376struct boot_param_header *initial_boot_params;
377
378#ifdef CONFIG_OF_EARLY_FLATTREE
379
380/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100381 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
382 */
383static int __init __reserved_mem_reserve_reg(unsigned long node,
384 const char *uname)
385{
386 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
387 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500388 int len;
389 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100390 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100391
392 prop = of_get_flat_dt_prop(node, "reg", &len);
393 if (!prop)
394 return -ENOENT;
395
396 if (len && len % t_len != 0) {
397 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
398 uname);
399 return -EINVAL;
400 }
401
402 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
403
404 while (len >= t_len) {
405 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
406 size = dt_mem_next_cell(dt_root_size_cells, &prop);
407
408 if (base && size &&
409 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
410 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
411 uname, &base, (unsigned long)size / SZ_1M);
412 else
413 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
414 uname, &base, (unsigned long)size / SZ_1M);
415
416 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100417 if (first) {
418 fdt_reserved_mem_save_node(node, uname, base, size);
419 first = 0;
420 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100421 }
422 return 0;
423}
424
425/**
426 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
427 * in /reserved-memory matches the values supported by the current implementation,
428 * also check if ranges property has been provided
429 */
Xiubo Li5b624112014-04-08 13:48:07 +0800430static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100431{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500432 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100433
434 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
435 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
436 return -EINVAL;
437
438 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
439 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
440 return -EINVAL;
441
442 prop = of_get_flat_dt_prop(node, "ranges", NULL);
443 if (!prop)
444 return -EINVAL;
445 return 0;
446}
447
448/**
449 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
450 */
451static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
452 int depth, void *data)
453{
454 static int found;
455 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100456 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100457
458 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
459 if (__reserved_mem_check_root(node) != 0) {
460 pr_err("Reserved memory: unsupported node format, ignoring\n");
461 /* break scan */
462 return 1;
463 }
464 found = 1;
465 /* scan next node */
466 return 0;
467 } else if (!found) {
468 /* scan next node */
469 return 0;
470 } else if (found && depth < 2) {
471 /* scanning of /reserved-memory has been finished */
472 return 1;
473 }
474
475 status = of_get_flat_dt_prop(node, "status", NULL);
476 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
477 return 0;
478
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100479 err = __reserved_mem_reserve_reg(node, uname);
480 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
481 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100482
483 /* scan next node */
484 return 0;
485}
486
487/**
488 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
489 *
490 * This function grabs memory from early allocator for device exclusive use
491 * defined in device tree structures. It should be called by arch specific code
492 * once the early allocator (i.e. memblock) has been fully activated.
493 */
494void __init early_init_fdt_scan_reserved_mem(void)
495{
Josh Cartwright2040b522014-03-13 16:36:36 -0500496 if (!initial_boot_params)
497 return;
498
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100499 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100500 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100501}
502
503/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800504 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
505 * @it: callback function
506 * @data: context data pointer
507 *
508 * This function is used to scan the flattened device-tree, it is
509 * used to extract the memory information at boot before we can
510 * unflatten the tree
511 */
512int __init of_scan_flat_dt(int (*it)(unsigned long node,
513 const char *uname, int depth,
514 void *data),
515 void *data)
516{
Rob Herringe6a69282014-04-02 15:10:14 -0500517 const void *blob = initial_boot_params;
518 const char *pathp;
519 int offset, rc = 0, depth = -1;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800520
Rob Herringe6a69282014-04-02 15:10:14 -0500521 for (offset = fdt_next_node(blob, -1, &depth);
522 offset >= 0 && depth >= 0 && !rc;
523 offset = fdt_next_node(blob, offset, &depth)) {
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800524
Rob Herringe6a69282014-04-02 15:10:14 -0500525 pathp = fdt_get_name(blob, offset, NULL);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800526 if (*pathp == '/')
527 pathp = kbasename(pathp);
Rob Herringe6a69282014-04-02 15:10:14 -0500528 rc = it(offset, pathp, depth, data);
529 }
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800530 return rc;
531}
532
533/**
534 * of_get_flat_dt_root - find the root node in the flat blob
535 */
536unsigned long __init of_get_flat_dt_root(void)
537{
Rob Herringe6a69282014-04-02 15:10:14 -0500538 return 0;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800539}
540
541/**
542 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
543 *
544 * This function can be used within scan_flattened_dt callback to get
545 * access to properties
546 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500547const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
548 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800549{
Rob Herringe6a69282014-04-02 15:10:14 -0500550 return fdt_getprop(initial_boot_params, node, name, size);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800551}
552
553/**
554 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
555 * @node: node to test
556 * @compat: compatible string to compare with compatible list.
557 */
558int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
559{
560 return of_fdt_is_compatible(initial_boot_params, node, compat);
561}
562
Grant Likelya4f740c2010-10-30 11:49:09 -0400563/**
564 * of_flat_dt_match - Return true if node matches a list of compatible values
565 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100566int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400567{
568 return of_fdt_match(initial_boot_params, node, compat);
569}
570
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200571struct fdt_scan_status {
572 const char *name;
573 int namelen;
574 int depth;
575 int found;
576 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
577 void *data;
578};
579
Rob Herring6a903a22013-08-27 21:41:56 -0500580const char * __init of_flat_dt_get_machine_name(void)
581{
582 const char *name;
583 unsigned long dt_root = of_get_flat_dt_root();
584
585 name = of_get_flat_dt_prop(dt_root, "model", NULL);
586 if (!name)
587 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
588 return name;
589}
590
591/**
592 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
593 *
594 * @default_match: A machine specific ptr to return in case of no match.
595 * @get_next_compat: callback function to return next compatible match table.
596 *
597 * Iterate through machine match tables to find the best match for the machine
598 * compatible string in the FDT.
599 */
600const void * __init of_flat_dt_match_machine(const void *default_match,
601 const void * (*get_next_compat)(const char * const**))
602{
603 const void *data = NULL;
604 const void *best_data = default_match;
605 const char *const *compat;
606 unsigned long dt_root;
607 unsigned int best_score = ~1, score = 0;
608
609 dt_root = of_get_flat_dt_root();
610 while ((data = get_next_compat(&compat))) {
611 score = of_flat_dt_match(dt_root, compat);
612 if (score > 0 && score < best_score) {
613 best_data = data;
614 best_score = score;
615 }
616 }
617 if (!best_data) {
618 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500619 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500620
621 pr_err("\n unrecognized device tree list:\n[ ");
622
623 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
624 if (prop) {
625 while (size > 0) {
626 printk("'%s' ", prop);
627 size -= strlen(prop) + 1;
628 prop += strlen(prop) + 1;
629 }
630 }
631 printk("]\n\n");
632 return NULL;
633 }
634
635 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
636
637 return best_data;
638}
639
Grant Likelyf7b3a832009-11-24 03:26:58 -0700640#ifdef CONFIG_BLK_DEV_INITRD
641/**
642 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
643 * @node: reference to node containing initrd location ('chosen')
644 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500645static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700646{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400647 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500648 int len;
649 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700650
651 pr_debug("Looking for initrd properties... ");
652
653 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700654 if (!prop)
655 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400656 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700657
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700658 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
659 if (!prop)
660 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400661 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700662
Rob Herring29eb45a2013-08-30 17:06:53 -0500663 initrd_start = (unsigned long)__va(start);
664 initrd_end = (unsigned long)__va(end);
665 initrd_below_start_ok = 1;
666
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400667 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
668 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700669}
670#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500671static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700672{
673}
674#endif /* CONFIG_BLK_DEV_INITRD */
675
Grant Likely41f88002009-11-23 20:07:01 -0700676/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700677 * early_init_dt_scan_root - fetch the top level address and size cells
678 */
679int __init early_init_dt_scan_root(unsigned long node, const char *uname,
680 int depth, void *data)
681{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500682 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700683
684 if (depth != 0)
685 return 0;
686
Jeremy Kerr33714882010-01-30 01:45:26 -0700687 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
688 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
689
Grant Likelyf00abd92009-11-24 03:27:10 -0700690 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700691 if (prop)
692 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700693 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
694
695 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700696 if (prop)
697 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700698 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
699
700 /* break now */
701 return 1;
702}
703
Rob Herring9d0c4df2014-04-01 23:49:03 -0500704u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700705{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500706 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700707
708 *cellp = p + s;
709 return of_read_number(p, s);
710}
711
Grant Likely51975db2010-02-01 21:34:14 -0700712/**
713 * early_init_dt_scan_memory - Look for an parse memory nodes
714 */
715int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
716 int depth, void *data)
717{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500718 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
719 const __be32 *reg, *endp;
720 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700721
722 /* We are scanning "memory" nodes only */
723 if (type == NULL) {
724 /*
725 * The longtrail doesn't have a device_type on the
726 * /memory node, so look for the node called /memory@0.
727 */
728 if (depth != 1 || strcmp(uname, "memory@0") != 0)
729 return 0;
730 } else if (strcmp(type, "memory") != 0)
731 return 0;
732
733 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
734 if (reg == NULL)
735 reg = of_get_flat_dt_prop(node, "reg", &l);
736 if (reg == NULL)
737 return 0;
738
739 endp = reg + (l / sizeof(__be32));
740
Rob Herring9d0c4df2014-04-01 23:49:03 -0500741 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700742 uname, l, reg[0], reg[1], reg[2], reg[3]);
743
744 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
745 u64 base, size;
746
747 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
748 size = dt_mem_next_cell(dt_root_size_cells, &reg);
749
750 if (size == 0)
751 continue;
752 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
753 (unsigned long long)size);
754
755 early_init_dt_add_memory_arch(base, size);
756 }
757
758 return 0;
759}
760
Grant Likely86e03222009-12-10 23:42:21 -0700761int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
762 int depth, void *data)
763{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500764 int l;
765 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700766
767 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
768
Grant Likely85f60ae2011-04-29 00:18:16 -0600769 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700770 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
771 return 0;
772
773 early_init_dt_check_for_initrd(node);
774
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300775 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700776 p = of_get_flat_dt_prop(node, "bootargs", &l);
777 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600778 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700779
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000780 /*
781 * CONFIG_CMDLINE is meant to be a default in case nothing else
782 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
783 * is set in which case we override whatever was found earlier.
784 */
Grant Likely86e03222009-12-10 23:42:21 -0700785#ifdef CONFIG_CMDLINE
786#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000787 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700788#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600789 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700790#endif /* CONFIG_CMDLINE */
791
Grant Likely85f60ae2011-04-29 00:18:16 -0600792 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700793
794 /* break now */
795 return 1;
796}
797
Grant Likelya1727da2013-08-28 21:18:32 +0100798#ifdef CONFIG_HAVE_MEMBLOCK
Rob Herring068f6312013-09-24 22:20:01 -0500799void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
800{
801 const u64 phys_offset = __pa(PAGE_OFFSET);
802 base &= PAGE_MASK;
803 size &= PAGE_MASK;
804 if (base + size < phys_offset) {
805 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
806 base, base + size);
807 return;
808 }
809 if (base < phys_offset) {
810 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
811 base, phys_offset);
812 size -= phys_offset - base;
813 base = phys_offset;
814 }
815 memblock_add(base, size);
816}
817
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100818int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
819 phys_addr_t size, bool nomap)
820{
821 if (memblock_is_region_reserved(base, size))
822 return -EBUSY;
823 if (nomap)
824 return memblock_remove(base, size);
825 return memblock_reserve(base, size);
826}
827
Grant Likelya1727da2013-08-28 21:18:32 +0100828/*
829 * called from unflatten_device_tree() to bootstrap devicetree itself
830 * Architectures can override this definition if memblock isn't used
831 */
832void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
833{
834 return __va(memblock_alloc(size, align));
835}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100836#else
837int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
838 phys_addr_t size, bool nomap)
839{
840 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
841 base, size, nomap ? " (nomap)" : "");
842 return -ENOSYS;
843}
Grant Likelya1727da2013-08-28 21:18:32 +0100844#endif
845
Rob Herring0288ffc2013-08-26 09:47:40 -0500846bool __init early_init_dt_scan(void *params)
847{
848 if (!params)
849 return false;
850
851 /* Setup flat device-tree pointer */
852 initial_boot_params = params;
853
854 /* check device tree validity */
855 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
856 initial_boot_params = NULL;
857 return false;
858 }
859
860 /* Retrieve various information from the /chosen node */
861 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
862
863 /* Initialize {size,address}-cells info */
864 of_scan_flat_dt(early_init_dt_scan_root, NULL);
865
866 /* Setup memory, calling early_init_dt_add_memory_arch */
867 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
868
869 return true;
870}
871
Grant Likelyf00abd92009-11-24 03:27:10 -0700872/**
Grant Likely41f88002009-11-23 20:07:01 -0700873 * unflatten_device_tree - create tree of device_nodes from flat blob
874 *
875 * unflattens the device-tree passed by the firmware, creating the
876 * tree of struct device_node. It also fills the "name" and "type"
877 * pointers of the nodes so the normal device-tree walking functions
878 * can be used.
879 */
880void __init unflatten_device_tree(void)
881{
Randy Dunlap465aac62012-11-30 10:01:51 +0000882 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700883 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700884
Robert P. J. Day4c7d6362013-05-30 05:38:08 -0400885 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +0800886 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700887}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800888
Rob Herringa8bf7522013-08-26 11:22:45 -0500889/**
890 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
891 *
892 * Copies and unflattens the device-tree passed by the firmware, creating the
893 * tree of struct device_node. It also fills the "name" and "type"
894 * pointers of the nodes so the normal device-tree walking functions
895 * can be used. This should only be used when the FDT memory has not been
896 * reserved such is the case when the FDT is built-in to the kernel init
897 * section. If the FDT memory is reserved already then unflatten_device_tree
898 * should be used instead.
899 */
900void __init unflatten_and_copy_device_tree(void)
901{
James Hogan6f041e92013-11-21 13:44:14 +0000902 int size;
903 void *dt;
904
905 if (!initial_boot_params) {
906 pr_warn("No valid device tree found, continuing without\n");
907 return;
908 }
909
910 size = __be32_to_cpu(initial_boot_params->totalsize);
911 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringa8bf7522013-08-26 11:22:45 -0500912 __alignof__(struct boot_param_header));
913
914 if (dt) {
915 memcpy(dt, initial_boot_params, size);
916 initial_boot_params = dt;
917 }
918 unflatten_device_tree();
919}
920
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800921#endif /* CONFIG_OF_EARLY_FLATTREE */