blob: 819e11209718f4fcdfd2fc13a022b55b9bf66ea0 [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>
Stephen Neuendorfferfe140422010-11-18 15:55:02 -080015#include <linux/module.h>
Grant Likelye169cfb2009-11-23 14:53:09 -070016#include <linux/of.h>
17#include <linux/of_fdt.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>
Grant Likely51975db2010-02-01 21:34:14 -070022
Fabio Estevamc89810a2012-01-02 14:19:03 -020023#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Grant Likely86e03222009-12-10 23:42:21 -070024#ifdef CONFIG_PPC
25#include <asm/machdep.h>
26#endif /* CONFIG_PPC */
27
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070028#include <asm/page.h>
29
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080030char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
31{
32 return ((char *)blob) +
33 be32_to_cpu(blob->off_dt_strings) + offset;
34}
35
36/**
37 * of_fdt_get_property - Given a node in the given flat blob, return
38 * the property ptr
39 */
40void *of_fdt_get_property(struct boot_param_header *blob,
41 unsigned long node, const char *name,
42 unsigned long *size)
43{
44 unsigned long p = node;
45
46 do {
47 u32 tag = be32_to_cpup((__be32 *)p);
48 u32 sz, noff;
49 const char *nstr;
50
51 p += 4;
52 if (tag == OF_DT_NOP)
53 continue;
54 if (tag != OF_DT_PROP)
55 return NULL;
56
57 sz = be32_to_cpup((__be32 *)p);
58 noff = be32_to_cpup((__be32 *)(p + 4));
59 p += 8;
60 if (be32_to_cpu(blob->version) < 0x10)
61 p = ALIGN(p, sz >= 8 ? 8 : 4);
62
63 nstr = of_fdt_get_string(blob, noff);
64 if (nstr == NULL) {
65 pr_warning("Can't find property index name !\n");
66 return NULL;
67 }
68 if (strcmp(name, nstr) == 0) {
69 if (size)
70 *size = sz;
71 return (void *)p;
72 }
73 p += sz;
74 p = ALIGN(p, 4);
75 } while (1);
76}
77
78/**
79 * of_fdt_is_compatible - Return true if given node from the given blob has
80 * compat in its compatible list
81 * @blob: A device tree blob
82 * @node: node to test
83 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040084 *
85 * On match, returns a non-zero value with smaller values returned for more
86 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080087 */
88int of_fdt_is_compatible(struct boot_param_header *blob,
89 unsigned long node, const char *compat)
90{
91 const char *cp;
Grant Likelya4f740c2010-10-30 11:49:09 -040092 unsigned long cplen, l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080093
94 cp = of_fdt_get_property(blob, node, "compatible", &cplen);
95 if (cp == NULL)
96 return 0;
97 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -040098 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080099 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -0400100 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -0800101 l = strlen(cp) + 1;
102 cp += l;
103 cplen -= l;
104 }
105
106 return 0;
107}
108
Grant Likelya4f740c2010-10-30 11:49:09 -0400109/**
110 * of_fdt_match - Return true if node matches a list of compatible values
111 */
112int of_fdt_match(struct boot_param_header *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100113 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400114{
115 unsigned int tmp, score = 0;
116
117 if (!compat)
118 return 0;
119
120 while (*compat) {
121 tmp = of_fdt_is_compatible(blob, node, *compat);
122 if (tmp && (score == 0 || (tmp < score)))
123 score = tmp;
124 compat++;
125 }
126
127 return score;
128}
129
Grant Likely44856812013-08-29 13:30:35 +0100130static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700131 unsigned long align)
132{
133 void *res;
134
Grant Likely44856812013-08-29 13:30:35 +0100135 *mem = PTR_ALIGN(*mem, align);
136 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700137 *mem += size;
138
139 return res;
140}
141
142/**
143 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800144 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700145 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -0700146 * @p: pointer to node in flat tree
147 * @dad: Parent struct device_node
148 * @allnextpp: pointer to ->allnext from last allocated device_node
149 * @fpsize: Size of the node path up at the current depth.
150 */
Grant Likely44856812013-08-29 13:30:35 +0100151static void * unflatten_dt_node(struct boot_param_header *blob,
152 void *mem,
153 void **p,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800154 struct device_node *dad,
155 struct device_node ***allnextpp,
156 unsigned long fpsize)
Grant Likelybbd33932009-11-23 20:07:00 -0700157{
158 struct device_node *np;
159 struct property *pp, **prev_pp = NULL;
160 char *pathp;
161 u32 tag;
162 unsigned int l, allocl;
163 int has_name = 0;
164 int new_format = 0;
165
Grant Likely44856812013-08-29 13:30:35 +0100166 tag = be32_to_cpup(*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700167 if (tag != OF_DT_BEGIN_NODE) {
168 pr_err("Weird tag at start of node: %x\n", tag);
169 return mem;
170 }
171 *p += 4;
Grant Likely44856812013-08-29 13:30:35 +0100172 pathp = *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700173 l = allocl = strlen(pathp) + 1;
Grant Likely44856812013-08-29 13:30:35 +0100174 *p = PTR_ALIGN(*p + l, 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700175
176 /* version 0x10 has a more compact unit name here instead of the full
177 * path. we accumulate the full path size using "fpsize", we'll rebuild
178 * it later. We detect this because the first character of the name is
179 * not '/'.
180 */
181 if ((*pathp) != '/') {
182 new_format = 1;
183 if (fpsize == 0) {
184 /* root node: special case. fpsize accounts for path
185 * plus terminating zero. root node only has '/', so
186 * fpsize should be 2, but we want to avoid the first
187 * level nodes to have two '/' so we use fpsize 1 here
188 */
189 fpsize = 1;
190 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000191 l = 1;
192 *pathp = '\0';
Grant Likelybbd33932009-11-23 20:07:00 -0700193 } else {
194 /* account for '/' and path size minus terminal 0
195 * already in 'l'
196 */
197 fpsize += l;
198 allocl = fpsize;
199 }
200 }
201
202 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
203 __alignof__(struct device_node));
204 if (allnextpp) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000205 char *fn;
Grant Likelyc22618a2012-11-14 22:37:12 +0000206 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700207 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700208 /* rebuild full path for new format */
209 if (dad && dad->parent) {
210 strcpy(fn, dad->full_name);
211#ifdef DEBUG
212 if ((strlen(fn) + l + 1) != allocl) {
213 pr_debug("%s: p: %d, l: %d, a: %d\n",
214 pathp, (int)strlen(fn),
215 l, allocl);
216 }
217#endif
218 fn += strlen(fn);
219 }
220 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000221 }
222 memcpy(fn, pathp, l);
223
Grant Likelybbd33932009-11-23 20:07:00 -0700224 prev_pp = &np->properties;
225 **allnextpp = np;
226 *allnextpp = &np->allnext;
227 if (dad != NULL) {
228 np->parent = dad;
229 /* we temporarily use the next field as `last_child'*/
230 if (dad->next == NULL)
231 dad->child = np;
232 else
233 dad->next->sibling = np;
234 dad->next = np;
235 }
236 kref_init(&np->kref);
237 }
Andres Salomona7006c92011-03-17 17:32:35 -0700238 /* process properties */
Grant Likelybbd33932009-11-23 20:07:00 -0700239 while (1) {
240 u32 sz, noff;
241 char *pname;
242
Grant Likely44856812013-08-29 13:30:35 +0100243 tag = be32_to_cpup(*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700244 if (tag == OF_DT_NOP) {
245 *p += 4;
246 continue;
247 }
248 if (tag != OF_DT_PROP)
249 break;
250 *p += 4;
Grant Likely44856812013-08-29 13:30:35 +0100251 sz = be32_to_cpup(*p);
252 noff = be32_to_cpup(*p + 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700253 *p += 8;
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800254 if (be32_to_cpu(blob->version) < 0x10)
Grant Likely44856812013-08-29 13:30:35 +0100255 *p = PTR_ALIGN(*p, sz >= 8 ? 8 : 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700256
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800257 pname = of_fdt_get_string(blob, noff);
Grant Likelybbd33932009-11-23 20:07:00 -0700258 if (pname == NULL) {
259 pr_info("Can't find property name in list !\n");
260 break;
261 }
262 if (strcmp(pname, "name") == 0)
263 has_name = 1;
264 l = strlen(pname) + 1;
265 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
266 __alignof__(struct property));
267 if (allnextpp) {
David Gibson04b954a2010-02-01 21:34:15 -0700268 /* We accept flattened tree phandles either in
269 * ePAPR-style "phandle" properties, or the
270 * legacy "linux,phandle" properties. If both
271 * appear and have different values, things
272 * will get weird. Don't do that. */
273 if ((strcmp(pname, "phandle") == 0) ||
274 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700275 if (np->phandle == 0)
Grant Likely9a6b2e52010-07-23 01:48:25 -0600276 np->phandle = be32_to_cpup((__be32*)*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700277 }
David Gibson04b954a2010-02-01 21:34:15 -0700278 /* And we process the "ibm,phandle" property
279 * used in pSeries dynamic device tree
280 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700281 if (strcmp(pname, "ibm,phandle") == 0)
Grant Likely9a6b2e52010-07-23 01:48:25 -0600282 np->phandle = be32_to_cpup((__be32 *)*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700283 pp->name = pname;
284 pp->length = sz;
Grant Likely44856812013-08-29 13:30:35 +0100285 pp->value = *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700286 *prev_pp = pp;
287 prev_pp = &pp->next;
288 }
Grant Likely44856812013-08-29 13:30:35 +0100289 *p = PTR_ALIGN((*p) + sz, 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700290 }
291 /* with version 0x10 we may not have the name property, recreate
292 * it here from the unit name if absent
293 */
294 if (!has_name) {
295 char *p1 = pathp, *ps = pathp, *pa = NULL;
296 int sz;
297
298 while (*p1) {
299 if ((*p1) == '@')
300 pa = p1;
301 if ((*p1) == '/')
302 ps = p1 + 1;
303 p1++;
304 }
305 if (pa < ps)
306 pa = p1;
307 sz = (pa - ps) + 1;
308 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
309 __alignof__(struct property));
310 if (allnextpp) {
311 pp->name = "name";
312 pp->length = sz;
313 pp->value = pp + 1;
314 *prev_pp = pp;
315 prev_pp = &pp->next;
316 memcpy(pp->value, ps, sz - 1);
317 ((char *)pp->value)[sz - 1] = 0;
318 pr_debug("fixed up name for %s -> %s\n", pathp,
319 (char *)pp->value);
320 }
321 }
322 if (allnextpp) {
323 *prev_pp = NULL;
324 np->name = of_get_property(np, "name", NULL);
325 np->type = of_get_property(np, "device_type", NULL);
326
327 if (!np->name)
328 np->name = "<NULL>";
329 if (!np->type)
330 np->type = "<NULL>";
331 }
Jason Gunthorpe7f809e12010-03-26 22:09:56 -0600332 while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
333 if (tag == OF_DT_NOP)
334 *p += 4;
335 else
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800336 mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
337 fpsize);
Grant Likely44856812013-08-29 13:30:35 +0100338 tag = be32_to_cpup(*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700339 }
340 if (tag != OF_DT_END_NODE) {
341 pr_err("Weird tag at end of node: %x\n", tag);
342 return mem;
343 }
344 *p += 4;
345 return mem;
346}
Grant Likely41f88002009-11-23 20:07:01 -0700347
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800348/**
349 * __unflatten_device_tree - create tree of device_nodes from flat blob
350 *
351 * unflattens a device-tree, creating the
352 * tree of struct device_node. It also fills the "name" and "type"
353 * pointers of the nodes so the normal device-tree walking functions
354 * can be used.
355 * @blob: The blob to expand
356 * @mynodes: The device_node tree created by the call
357 * @dt_alloc: An allocator that provides a virtual address to memory
358 * for the resulting tree
359 */
Andres Salomona7006c92011-03-17 17:32:35 -0700360static void __unflatten_device_tree(struct boot_param_header *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800361 struct device_node **mynodes,
362 void * (*dt_alloc)(u64 size, u64 align))
363{
Grant Likely44856812013-08-29 13:30:35 +0100364 unsigned long size;
365 void *start, *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800366 struct device_node **allnextp = mynodes;
367
368 pr_debug(" -> unflatten_device_tree()\n");
369
370 if (!blob) {
371 pr_debug("No device tree pointer\n");
372 return;
373 }
374
375 pr_debug("Unflattening device tree:\n");
376 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
377 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
378 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
379
380 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
381 pr_err("Invalid device tree blob header\n");
382 return;
383 }
384
385 /* First pass, scan for size */
Grant Likely44856812013-08-29 13:30:35 +0100386 start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
387 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
388 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800389
390 pr_debug(" size is %lx, allocating...\n", size);
391
392 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100393 mem = dt_alloc(size + 4, __alignof__(struct device_node));
394 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800395
Grant Likely44856812013-08-29 13:30:35 +0100396 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200397
Grant Likely44856812013-08-29 13:30:35 +0100398 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800399
400 /* Second pass, do actual unflattening */
Grant Likely44856812013-08-29 13:30:35 +0100401 start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800402 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
Grant Likely44856812013-08-29 13:30:35 +0100403 if (be32_to_cpup(start) != OF_DT_END)
404 pr_warning("Weird tag at end of tree: %08x\n", be32_to_cpup(start));
405 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800406 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100407 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800408 *allnextp = NULL;
409
410 pr_debug(" <- unflatten_device_tree()\n");
411}
412
413static void *kernel_tree_alloc(u64 size, u64 align)
414{
415 return kzalloc(size, GFP_KERNEL);
416}
417
418/**
419 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
420 *
421 * unflattens the device-tree passed by the firmware, creating the
422 * tree of struct device_node. It also fills the "name" and "type"
423 * pointers of the nodes so the normal device-tree walking functions
424 * can be used.
425 */
426void of_fdt_unflatten_tree(unsigned long *blob,
427 struct device_node **mynodes)
428{
429 struct boot_param_header *device_tree =
430 (struct boot_param_header *)blob;
431 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
432}
433EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
434
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800435/* Everything below here references initial_boot_params directly. */
436int __initdata dt_root_addr_cells;
437int __initdata dt_root_size_cells;
438
439struct boot_param_header *initial_boot_params;
440
441#ifdef CONFIG_OF_EARLY_FLATTREE
442
443/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100444 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
445 */
446static int __init __reserved_mem_reserve_reg(unsigned long node,
447 const char *uname)
448{
449 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
450 phys_addr_t base, size;
451 unsigned long len;
452 __be32 *prop;
453 int nomap;
454
455 prop = of_get_flat_dt_prop(node, "reg", &len);
456 if (!prop)
457 return -ENOENT;
458
459 if (len && len % t_len != 0) {
460 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
461 uname);
462 return -EINVAL;
463 }
464
465 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
466
467 while (len >= t_len) {
468 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
469 size = dt_mem_next_cell(dt_root_size_cells, &prop);
470
471 if (base && size &&
472 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
473 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
474 uname, &base, (unsigned long)size / SZ_1M);
475 else
476 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
477 uname, &base, (unsigned long)size / SZ_1M);
478
479 len -= t_len;
480 }
481 return 0;
482}
483
484/**
485 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
486 * in /reserved-memory matches the values supported by the current implementation,
487 * also check if ranges property has been provided
488 */
489static int __reserved_mem_check_root(unsigned long node)
490{
491 __be32 *prop;
492
493 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
494 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
495 return -EINVAL;
496
497 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
498 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
499 return -EINVAL;
500
501 prop = of_get_flat_dt_prop(node, "ranges", NULL);
502 if (!prop)
503 return -EINVAL;
504 return 0;
505}
506
507/**
508 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
509 */
510static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
511 int depth, void *data)
512{
513 static int found;
514 const char *status;
515
516 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
517 if (__reserved_mem_check_root(node) != 0) {
518 pr_err("Reserved memory: unsupported node format, ignoring\n");
519 /* break scan */
520 return 1;
521 }
522 found = 1;
523 /* scan next node */
524 return 0;
525 } else if (!found) {
526 /* scan next node */
527 return 0;
528 } else if (found && depth < 2) {
529 /* scanning of /reserved-memory has been finished */
530 return 1;
531 }
532
533 status = of_get_flat_dt_prop(node, "status", NULL);
534 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
535 return 0;
536
537 __reserved_mem_reserve_reg(node, uname);
538
539 /* scan next node */
540 return 0;
541}
542
543/**
544 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
545 *
546 * This function grabs memory from early allocator for device exclusive use
547 * defined in device tree structures. It should be called by arch specific code
548 * once the early allocator (i.e. memblock) has been fully activated.
549 */
550void __init early_init_fdt_scan_reserved_mem(void)
551{
552 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
553}
554
555/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800556 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
557 * @it: callback function
558 * @data: context data pointer
559 *
560 * This function is used to scan the flattened device-tree, it is
561 * used to extract the memory information at boot before we can
562 * unflatten the tree
563 */
564int __init of_scan_flat_dt(int (*it)(unsigned long node,
565 const char *uname, int depth,
566 void *data),
567 void *data)
568{
569 unsigned long p = ((unsigned long)initial_boot_params) +
570 be32_to_cpu(initial_boot_params->off_dt_struct);
571 int rc = 0;
572 int depth = -1;
573
574 do {
575 u32 tag = be32_to_cpup((__be32 *)p);
Fabio Estevame55b0822012-11-12 18:30:49 -0200576 const char *pathp;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800577
578 p += 4;
579 if (tag == OF_DT_END_NODE) {
580 depth--;
581 continue;
582 }
583 if (tag == OF_DT_NOP)
584 continue;
585 if (tag == OF_DT_END)
586 break;
587 if (tag == OF_DT_PROP) {
588 u32 sz = be32_to_cpup((__be32 *)p);
589 p += 8;
590 if (be32_to_cpu(initial_boot_params->version) < 0x10)
591 p = ALIGN(p, sz >= 8 ? 8 : 4);
592 p += sz;
593 p = ALIGN(p, 4);
594 continue;
595 }
596 if (tag != OF_DT_BEGIN_NODE) {
597 pr_err("Invalid tag %x in flat device tree!\n", tag);
598 return -EINVAL;
599 }
600 depth++;
601 pathp = (char *)p;
602 p = ALIGN(p + strlen(pathp) + 1, 4);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800603 if (*pathp == '/')
604 pathp = kbasename(pathp);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800605 rc = it(p, pathp, depth, data);
606 if (rc != 0)
607 break;
608 } while (1);
609
610 return rc;
611}
612
613/**
614 * of_get_flat_dt_root - find the root node in the flat blob
615 */
616unsigned long __init of_get_flat_dt_root(void)
617{
618 unsigned long p = ((unsigned long)initial_boot_params) +
619 be32_to_cpu(initial_boot_params->off_dt_struct);
620
621 while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
622 p += 4;
623 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
624 p += 4;
625 return ALIGN(p + strlen((char *)p) + 1, 4);
626}
627
628/**
629 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
630 *
631 * This function can be used within scan_flattened_dt callback to get
632 * access to properties
633 */
634void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
635 unsigned long *size)
636{
637 return of_fdt_get_property(initial_boot_params, node, name, size);
638}
639
640/**
641 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
642 * @node: node to test
643 * @compat: compatible string to compare with compatible list.
644 */
645int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
646{
647 return of_fdt_is_compatible(initial_boot_params, node, compat);
648}
649
Grant Likelya4f740c2010-10-30 11:49:09 -0400650/**
651 * of_flat_dt_match - Return true if node matches a list of compatible values
652 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100653int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400654{
655 return of_fdt_match(initial_boot_params, node, compat);
656}
657
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200658struct fdt_scan_status {
659 const char *name;
660 int namelen;
661 int depth;
662 int found;
663 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
664 void *data;
665};
666
667/**
668 * fdt_scan_node_by_path - iterator for of_scan_flat_dt_by_path function
669 */
670static int __init fdt_scan_node_by_path(unsigned long node, const char *uname,
671 int depth, void *data)
672{
673 struct fdt_scan_status *st = data;
674
675 /*
676 * if scan at the requested fdt node has been completed,
677 * return -ENXIO to abort further scanning
678 */
679 if (depth <= st->depth)
680 return -ENXIO;
681
682 /* requested fdt node has been found, so call iterator function */
683 if (st->found)
684 return st->iterator(node, uname, depth, st->data);
685
686 /* check if scanning automata is entering next level of fdt nodes */
687 if (depth == st->depth + 1 &&
688 strncmp(st->name, uname, st->namelen) == 0 &&
689 uname[st->namelen] == 0) {
690 st->depth += 1;
691 if (st->name[st->namelen] == 0) {
692 st->found = 1;
693 } else {
694 const char *next = st->name + st->namelen + 1;
695 st->name = next;
696 st->namelen = strcspn(next, "/");
697 }
698 return 0;
699 }
700
701 /* scan next fdt node */
702 return 0;
703}
704
705/**
706 * of_scan_flat_dt_by_path - scan flattened tree blob and call callback on each
707 * child of the given path.
708 * @path: path to start searching for children
709 * @it: callback function
710 * @data: context data pointer
711 *
712 * This function is used to scan the flattened device-tree starting from the
713 * node given by path. It is used to extract information (like reserved
714 * memory), which is required on ealy boot before we can unflatten the tree.
715 */
716int __init of_scan_flat_dt_by_path(const char *path,
717 int (*it)(unsigned long node, const char *name, int depth, void *data),
718 void *data)
719{
720 struct fdt_scan_status st = {path, 0, -1, 0, it, data};
721 int ret = 0;
722
723 if (initial_boot_params)
724 ret = of_scan_flat_dt(fdt_scan_node_by_path, &st);
725
726 if (!st.found)
727 return -ENOENT;
728 else if (ret == -ENXIO) /* scan has been completed */
729 return 0;
730 else
731 return ret;
732}
733
Rob Herring6a903a22013-08-27 21:41:56 -0500734const char * __init of_flat_dt_get_machine_name(void)
735{
736 const char *name;
737 unsigned long dt_root = of_get_flat_dt_root();
738
739 name = of_get_flat_dt_prop(dt_root, "model", NULL);
740 if (!name)
741 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
742 return name;
743}
744
745/**
746 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
747 *
748 * @default_match: A machine specific ptr to return in case of no match.
749 * @get_next_compat: callback function to return next compatible match table.
750 *
751 * Iterate through machine match tables to find the best match for the machine
752 * compatible string in the FDT.
753 */
754const void * __init of_flat_dt_match_machine(const void *default_match,
755 const void * (*get_next_compat)(const char * const**))
756{
757 const void *data = NULL;
758 const void *best_data = default_match;
759 const char *const *compat;
760 unsigned long dt_root;
761 unsigned int best_score = ~1, score = 0;
762
763 dt_root = of_get_flat_dt_root();
764 while ((data = get_next_compat(&compat))) {
765 score = of_flat_dt_match(dt_root, compat);
766 if (score > 0 && score < best_score) {
767 best_data = data;
768 best_score = score;
769 }
770 }
771 if (!best_data) {
772 const char *prop;
773 long size;
774
775 pr_err("\n unrecognized device tree list:\n[ ");
776
777 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
778 if (prop) {
779 while (size > 0) {
780 printk("'%s' ", prop);
781 size -= strlen(prop) + 1;
782 prop += strlen(prop) + 1;
783 }
784 }
785 printk("]\n\n");
786 return NULL;
787 }
788
789 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
790
791 return best_data;
792}
793
Grant Likelyf7b3a832009-11-24 03:26:58 -0700794#ifdef CONFIG_BLK_DEV_INITRD
795/**
796 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
797 * @node: reference to node containing initrd location ('chosen')
798 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500799static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700800{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400801 u64 start, end;
802 unsigned long len;
Jeremy Kerr33714882010-01-30 01:45:26 -0700803 __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700804
805 pr_debug("Looking for initrd properties... ");
806
807 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700808 if (!prop)
809 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400810 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700811
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700812 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
813 if (!prop)
814 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400815 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700816
Rob Herring29eb45a2013-08-30 17:06:53 -0500817 initrd_start = (unsigned long)__va(start);
818 initrd_end = (unsigned long)__va(end);
819 initrd_below_start_ok = 1;
820
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400821 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
822 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700823}
824#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500825static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700826{
827}
828#endif /* CONFIG_BLK_DEV_INITRD */
829
Grant Likely41f88002009-11-23 20:07:01 -0700830/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700831 * early_init_dt_scan_root - fetch the top level address and size cells
832 */
833int __init early_init_dt_scan_root(unsigned long node, const char *uname,
834 int depth, void *data)
835{
Jeremy Kerr33714882010-01-30 01:45:26 -0700836 __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700837
838 if (depth != 0)
839 return 0;
840
Jeremy Kerr33714882010-01-30 01:45:26 -0700841 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
842 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
843
Grant Likelyf00abd92009-11-24 03:27:10 -0700844 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700845 if (prop)
846 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700847 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
848
849 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700850 if (prop)
851 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700852 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
853
854 /* break now */
855 return 1;
856}
857
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700858u64 __init dt_mem_next_cell(int s, __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700859{
Jeremy Kerr2e89e682010-01-30 01:41:49 -0700860 __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700861
862 *cellp = p + s;
863 return of_read_number(p, s);
864}
865
Grant Likely51975db2010-02-01 21:34:14 -0700866/**
867 * early_init_dt_scan_memory - Look for an parse memory nodes
868 */
869int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
870 int depth, void *data)
871{
872 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
873 __be32 *reg, *endp;
874 unsigned long l;
875
876 /* We are scanning "memory" nodes only */
877 if (type == NULL) {
878 /*
879 * The longtrail doesn't have a device_type on the
880 * /memory node, so look for the node called /memory@0.
881 */
882 if (depth != 1 || strcmp(uname, "memory@0") != 0)
883 return 0;
884 } else if (strcmp(type, "memory") != 0)
885 return 0;
886
887 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
888 if (reg == NULL)
889 reg = of_get_flat_dt_prop(node, "reg", &l);
890 if (reg == NULL)
891 return 0;
892
893 endp = reg + (l / sizeof(__be32));
894
895 pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
896 uname, l, reg[0], reg[1], reg[2], reg[3]);
897
898 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
899 u64 base, size;
900
901 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
902 size = dt_mem_next_cell(dt_root_size_cells, &reg);
903
904 if (size == 0)
905 continue;
906 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
907 (unsigned long long)size);
908
909 early_init_dt_add_memory_arch(base, size);
910 }
911
912 return 0;
913}
914
Grant Likely86e03222009-12-10 23:42:21 -0700915int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
916 int depth, void *data)
917{
918 unsigned long l;
919 char *p;
920
921 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
922
Grant Likely85f60ae2011-04-29 00:18:16 -0600923 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700924 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
925 return 0;
926
927 early_init_dt_check_for_initrd(node);
928
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300929 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700930 p = of_get_flat_dt_prop(node, "bootargs", &l);
931 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600932 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700933
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000934 /*
935 * CONFIG_CMDLINE is meant to be a default in case nothing else
936 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
937 * is set in which case we override whatever was found earlier.
938 */
Grant Likely86e03222009-12-10 23:42:21 -0700939#ifdef CONFIG_CMDLINE
940#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000941 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700942#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600943 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700944#endif /* CONFIG_CMDLINE */
945
Grant Likely85f60ae2011-04-29 00:18:16 -0600946 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700947
948 /* break now */
949 return 1;
950}
951
Grant Likelya1727da2013-08-28 21:18:32 +0100952#ifdef CONFIG_HAVE_MEMBLOCK
Rob Herring068f6312013-09-24 22:20:01 -0500953void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
954{
955 const u64 phys_offset = __pa(PAGE_OFFSET);
956 base &= PAGE_MASK;
957 size &= PAGE_MASK;
958 if (base + size < phys_offset) {
959 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
960 base, base + size);
961 return;
962 }
963 if (base < phys_offset) {
964 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
965 base, phys_offset);
966 size -= phys_offset - base;
967 base = phys_offset;
968 }
969 memblock_add(base, size);
970}
971
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100972int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
973 phys_addr_t size, bool nomap)
974{
975 if (memblock_is_region_reserved(base, size))
976 return -EBUSY;
977 if (nomap)
978 return memblock_remove(base, size);
979 return memblock_reserve(base, size);
980}
981
Grant Likelya1727da2013-08-28 21:18:32 +0100982/*
983 * called from unflatten_device_tree() to bootstrap devicetree itself
984 * Architectures can override this definition if memblock isn't used
985 */
986void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
987{
988 return __va(memblock_alloc(size, align));
989}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100990#else
991int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
992 phys_addr_t size, bool nomap)
993{
994 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
995 base, size, nomap ? " (nomap)" : "");
996 return -ENOSYS;
997}
Grant Likelya1727da2013-08-28 21:18:32 +0100998#endif
999
Rob Herring0288ffcb2013-08-26 09:47:40 -05001000bool __init early_init_dt_scan(void *params)
1001{
1002 if (!params)
1003 return false;
1004
1005 /* Setup flat device-tree pointer */
1006 initial_boot_params = params;
1007
1008 /* check device tree validity */
1009 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
1010 initial_boot_params = NULL;
1011 return false;
1012 }
1013
1014 /* Retrieve various information from the /chosen node */
1015 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1016
1017 /* Initialize {size,address}-cells info */
1018 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1019
1020 /* Setup memory, calling early_init_dt_add_memory_arch */
1021 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1022
1023 return true;
1024}
1025
Grant Likelyf00abd92009-11-24 03:27:10 -07001026/**
Grant Likely41f88002009-11-23 20:07:01 -07001027 * unflatten_device_tree - create tree of device_nodes from flat blob
1028 *
1029 * unflattens the device-tree passed by the firmware, creating the
1030 * tree of struct device_node. It also fills the "name" and "type"
1031 * pointers of the nodes so the normal device-tree walking functions
1032 * can be used.
1033 */
1034void __init unflatten_device_tree(void)
1035{
Randy Dunlap465aac62012-11-30 10:01:51 +00001036 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -07001037 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001038
Robert P. J. Day4c7d6362013-05-30 05:38:08 -04001039 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +08001040 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -07001041}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001042
Rob Herringa8bf7522013-08-26 11:22:45 -05001043/**
1044 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
1045 *
1046 * Copies and unflattens the device-tree passed by the firmware, creating the
1047 * tree of struct device_node. It also fills the "name" and "type"
1048 * pointers of the nodes so the normal device-tree walking functions
1049 * can be used. This should only be used when the FDT memory has not been
1050 * reserved such is the case when the FDT is built-in to the kernel init
1051 * section. If the FDT memory is reserved already then unflatten_device_tree
1052 * should be used instead.
1053 */
1054void __init unflatten_and_copy_device_tree(void)
1055{
James Hogan6f041e92013-11-21 13:44:14 +00001056 int size;
1057 void *dt;
1058
1059 if (!initial_boot_params) {
1060 pr_warn("No valid device tree found, continuing without\n");
1061 return;
1062 }
1063
1064 size = __be32_to_cpu(initial_boot_params->totalsize);
1065 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringa8bf7522013-08-26 11:22:45 -05001066 __alignof__(struct boot_param_header));
1067
1068 if (dt) {
1069 memcpy(dt, initial_boot_params, size);
1070 initial_boot_params = dt;
1071 }
1072 unflatten_device_tree();
1073}
1074
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001075#endif /* CONFIG_OF_EARLY_FLATTREE */