blob: 1d1582bb81fba0846dda78d4309030ad7b164b47 [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>
Grant Likely51975db2010-02-01 21:34:14 -070022
Fabio Estevamc89810a2012-01-02 14:19:03 -020023#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
Jeremy Kerr4ef7b372010-02-14 07:13:47 -070024#include <asm/page.h>
25
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080026char *of_fdt_get_string(struct boot_param_header *blob, u32 offset)
27{
28 return ((char *)blob) +
29 be32_to_cpu(blob->off_dt_strings) + offset;
30}
31
32/**
33 * of_fdt_get_property - Given a node in the given flat blob, return
34 * the property ptr
35 */
36void *of_fdt_get_property(struct boot_param_header *blob,
37 unsigned long node, const char *name,
Rob Herring9d0c4df2014-04-01 23:49:03 -050038 int *size)
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080039{
40 unsigned long p = node;
41
42 do {
43 u32 tag = be32_to_cpup((__be32 *)p);
44 u32 sz, noff;
45 const char *nstr;
46
47 p += 4;
48 if (tag == OF_DT_NOP)
49 continue;
50 if (tag != OF_DT_PROP)
51 return NULL;
52
53 sz = be32_to_cpup((__be32 *)p);
54 noff = be32_to_cpup((__be32 *)(p + 4));
55 p += 8;
56 if (be32_to_cpu(blob->version) < 0x10)
57 p = ALIGN(p, sz >= 8 ? 8 : 4);
58
59 nstr = of_fdt_get_string(blob, noff);
60 if (nstr == NULL) {
61 pr_warning("Can't find property index name !\n");
62 return NULL;
63 }
64 if (strcmp(name, nstr) == 0) {
65 if (size)
66 *size = sz;
67 return (void *)p;
68 }
69 p += sz;
70 p = ALIGN(p, 4);
71 } while (1);
72}
73
74/**
75 * of_fdt_is_compatible - Return true if given node from the given blob has
76 * compat in its compatible list
77 * @blob: A device tree blob
78 * @node: node to test
79 * @compat: compatible string to compare with compatible list.
Grant Likelya4f740c2010-10-30 11:49:09 -040080 *
81 * On match, returns a non-zero value with smaller values returned for more
82 * specific compatible values.
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080083 */
84int of_fdt_is_compatible(struct boot_param_header *blob,
85 unsigned long node, const char *compat)
86{
87 const char *cp;
Rob Herring9d0c4df2014-04-01 23:49:03 -050088 int cplen;
89 unsigned long l, score = 0;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080090
91 cp = of_fdt_get_property(blob, node, "compatible", &cplen);
92 if (cp == NULL)
93 return 0;
94 while (cplen > 0) {
Grant Likelya4f740c2010-10-30 11:49:09 -040095 score++;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080096 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
Grant Likelya4f740c2010-10-30 11:49:09 -040097 return score;
Stephen Neuendorffer9706a362010-11-18 15:54:59 -080098 l = strlen(cp) + 1;
99 cp += l;
100 cplen -= l;
101 }
102
103 return 0;
104}
105
Grant Likelya4f740c2010-10-30 11:49:09 -0400106/**
107 * of_fdt_match - Return true if node matches a list of compatible values
108 */
109int of_fdt_match(struct boot_param_header *blob, unsigned long node,
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100110 const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400111{
112 unsigned int tmp, score = 0;
113
114 if (!compat)
115 return 0;
116
117 while (*compat) {
118 tmp = of_fdt_is_compatible(blob, node, *compat);
119 if (tmp && (score == 0 || (tmp < score)))
120 score = tmp;
121 compat++;
122 }
123
124 return score;
125}
126
Grant Likely44856812013-08-29 13:30:35 +0100127static void *unflatten_dt_alloc(void **mem, unsigned long size,
Grant Likelybbd33932009-11-23 20:07:00 -0700128 unsigned long align)
129{
130 void *res;
131
Grant Likely44856812013-08-29 13:30:35 +0100132 *mem = PTR_ALIGN(*mem, align);
133 res = *mem;
Grant Likelybbd33932009-11-23 20:07:00 -0700134 *mem += size;
135
136 return res;
137}
138
139/**
140 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800141 * @blob: The parent device tree blob
Andres Salomona7006c92011-03-17 17:32:35 -0700142 * @mem: Memory chunk to use for allocating device nodes and properties
Grant Likelybbd33932009-11-23 20:07:00 -0700143 * @p: pointer to node in flat tree
144 * @dad: Parent struct device_node
145 * @allnextpp: pointer to ->allnext from last allocated device_node
146 * @fpsize: Size of the node path up at the current depth.
147 */
Grant Likely44856812013-08-29 13:30:35 +0100148static void * unflatten_dt_node(struct boot_param_header *blob,
149 void *mem,
150 void **p,
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800151 struct device_node *dad,
152 struct device_node ***allnextpp,
153 unsigned long fpsize)
Grant Likelybbd33932009-11-23 20:07:00 -0700154{
155 struct device_node *np;
156 struct property *pp, **prev_pp = NULL;
157 char *pathp;
158 u32 tag;
159 unsigned int l, allocl;
160 int has_name = 0;
161 int new_format = 0;
162
Grant Likely44856812013-08-29 13:30:35 +0100163 tag = be32_to_cpup(*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700164 if (tag != OF_DT_BEGIN_NODE) {
165 pr_err("Weird tag at start of node: %x\n", tag);
166 return mem;
167 }
168 *p += 4;
Grant Likely44856812013-08-29 13:30:35 +0100169 pathp = *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700170 l = allocl = strlen(pathp) + 1;
Grant Likely44856812013-08-29 13:30:35 +0100171 *p = PTR_ALIGN(*p + l, 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700172
173 /* version 0x10 has a more compact unit name here instead of the full
174 * path. we accumulate the full path size using "fpsize", we'll rebuild
175 * it later. We detect this because the first character of the name is
176 * not '/'.
177 */
178 if ((*pathp) != '/') {
179 new_format = 1;
180 if (fpsize == 0) {
181 /* root node: special case. fpsize accounts for path
182 * plus terminating zero. root node only has '/', so
183 * fpsize should be 2, but we want to avoid the first
184 * level nodes to have two '/' so we use fpsize 1 here
185 */
186 fpsize = 1;
187 allocl = 2;
Catalin Marinas0fca5de2012-11-16 15:14:38 +0000188 l = 1;
189 *pathp = '\0';
Grant Likelybbd33932009-11-23 20:07:00 -0700190 } else {
191 /* account for '/' and path size minus terminal 0
192 * already in 'l'
193 */
194 fpsize += l;
195 allocl = fpsize;
196 }
197 }
198
199 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
200 __alignof__(struct device_node));
201 if (allnextpp) {
Grant Likelyc22618a2012-11-14 22:37:12 +0000202 char *fn;
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +0200203 of_node_init(np);
Grant Likelyc22618a2012-11-14 22:37:12 +0000204 np->full_name = fn = ((char *)np) + sizeof(*np);
Grant Likelybbd33932009-11-23 20:07:00 -0700205 if (new_format) {
Grant Likelybbd33932009-11-23 20:07:00 -0700206 /* rebuild full path for new format */
207 if (dad && dad->parent) {
208 strcpy(fn, dad->full_name);
209#ifdef DEBUG
210 if ((strlen(fn) + l + 1) != allocl) {
211 pr_debug("%s: p: %d, l: %d, a: %d\n",
212 pathp, (int)strlen(fn),
213 l, allocl);
214 }
215#endif
216 fn += strlen(fn);
217 }
218 *(fn++) = '/';
Grant Likelyc22618a2012-11-14 22:37:12 +0000219 }
220 memcpy(fn, pathp, l);
221
Grant Likelybbd33932009-11-23 20:07:00 -0700222 prev_pp = &np->properties;
223 **allnextpp = np;
224 *allnextpp = &np->allnext;
225 if (dad != NULL) {
226 np->parent = dad;
227 /* we temporarily use the next field as `last_child'*/
228 if (dad->next == NULL)
229 dad->child = np;
230 else
231 dad->next->sibling = np;
232 dad->next = np;
233 }
Grant Likelybbd33932009-11-23 20:07:00 -0700234 }
Andres Salomona7006c92011-03-17 17:32:35 -0700235 /* process properties */
Grant Likelybbd33932009-11-23 20:07:00 -0700236 while (1) {
237 u32 sz, noff;
238 char *pname;
239
Grant Likely44856812013-08-29 13:30:35 +0100240 tag = be32_to_cpup(*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700241 if (tag == OF_DT_NOP) {
242 *p += 4;
243 continue;
244 }
245 if (tag != OF_DT_PROP)
246 break;
247 *p += 4;
Grant Likely44856812013-08-29 13:30:35 +0100248 sz = be32_to_cpup(*p);
249 noff = be32_to_cpup(*p + 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700250 *p += 8;
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800251 if (be32_to_cpu(blob->version) < 0x10)
Grant Likely44856812013-08-29 13:30:35 +0100252 *p = PTR_ALIGN(*p, sz >= 8 ? 8 : 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700253
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800254 pname = of_fdt_get_string(blob, noff);
Grant Likelybbd33932009-11-23 20:07:00 -0700255 if (pname == NULL) {
256 pr_info("Can't find property name in list !\n");
257 break;
258 }
259 if (strcmp(pname, "name") == 0)
260 has_name = 1;
261 l = strlen(pname) + 1;
262 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
263 __alignof__(struct property));
264 if (allnextpp) {
David Gibson04b954a2010-02-01 21:34:15 -0700265 /* We accept flattened tree phandles either in
266 * ePAPR-style "phandle" properties, or the
267 * legacy "linux,phandle" properties. If both
268 * appear and have different values, things
269 * will get weird. Don't do that. */
270 if ((strcmp(pname, "phandle") == 0) ||
271 (strcmp(pname, "linux,phandle") == 0)) {
Grant Likely6016a362010-01-28 14:06:53 -0700272 if (np->phandle == 0)
Grant Likely9a6b2e52010-07-23 01:48:25 -0600273 np->phandle = be32_to_cpup((__be32*)*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700274 }
David Gibson04b954a2010-02-01 21:34:15 -0700275 /* And we process the "ibm,phandle" property
276 * used in pSeries dynamic device tree
277 * stuff */
Grant Likelybbd33932009-11-23 20:07:00 -0700278 if (strcmp(pname, "ibm,phandle") == 0)
Grant Likely9a6b2e52010-07-23 01:48:25 -0600279 np->phandle = be32_to_cpup((__be32 *)*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700280 pp->name = pname;
281 pp->length = sz;
Grant Likely44856812013-08-29 13:30:35 +0100282 pp->value = *p;
Grant Likelybbd33932009-11-23 20:07:00 -0700283 *prev_pp = pp;
284 prev_pp = &pp->next;
285 }
Grant Likely44856812013-08-29 13:30:35 +0100286 *p = PTR_ALIGN((*p) + sz, 4);
Grant Likelybbd33932009-11-23 20:07:00 -0700287 }
288 /* with version 0x10 we may not have the name property, recreate
289 * it here from the unit name if absent
290 */
291 if (!has_name) {
292 char *p1 = pathp, *ps = pathp, *pa = NULL;
293 int sz;
294
295 while (*p1) {
296 if ((*p1) == '@')
297 pa = p1;
298 if ((*p1) == '/')
299 ps = p1 + 1;
300 p1++;
301 }
302 if (pa < ps)
303 pa = p1;
304 sz = (pa - ps) + 1;
305 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
306 __alignof__(struct property));
307 if (allnextpp) {
308 pp->name = "name";
309 pp->length = sz;
310 pp->value = pp + 1;
311 *prev_pp = pp;
312 prev_pp = &pp->next;
313 memcpy(pp->value, ps, sz - 1);
314 ((char *)pp->value)[sz - 1] = 0;
315 pr_debug("fixed up name for %s -> %s\n", pathp,
316 (char *)pp->value);
317 }
318 }
319 if (allnextpp) {
320 *prev_pp = NULL;
321 np->name = of_get_property(np, "name", NULL);
322 np->type = of_get_property(np, "device_type", NULL);
323
324 if (!np->name)
325 np->name = "<NULL>";
326 if (!np->type)
327 np->type = "<NULL>";
328 }
Jason Gunthorpe7f809e12010-03-26 22:09:56 -0600329 while (tag == OF_DT_BEGIN_NODE || tag == OF_DT_NOP) {
330 if (tag == OF_DT_NOP)
331 *p += 4;
332 else
Stephen Neuendorffera40d6c42010-11-18 15:55:00 -0800333 mem = unflatten_dt_node(blob, mem, p, np, allnextpp,
334 fpsize);
Grant Likely44856812013-08-29 13:30:35 +0100335 tag = be32_to_cpup(*p);
Grant Likelybbd33932009-11-23 20:07:00 -0700336 }
337 if (tag != OF_DT_END_NODE) {
338 pr_err("Weird tag at end of node: %x\n", tag);
339 return mem;
340 }
341 *p += 4;
342 return mem;
343}
Grant Likely41f88002009-11-23 20:07:01 -0700344
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800345/**
346 * __unflatten_device_tree - create tree of device_nodes from flat blob
347 *
348 * unflattens a device-tree, creating the
349 * tree of struct device_node. It also fills the "name" and "type"
350 * pointers of the nodes so the normal device-tree walking functions
351 * can be used.
352 * @blob: The blob to expand
353 * @mynodes: The device_node tree created by the call
354 * @dt_alloc: An allocator that provides a virtual address to memory
355 * for the resulting tree
356 */
Andres Salomona7006c92011-03-17 17:32:35 -0700357static void __unflatten_device_tree(struct boot_param_header *blob,
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800358 struct device_node **mynodes,
359 void * (*dt_alloc)(u64 size, u64 align))
360{
Grant Likely44856812013-08-29 13:30:35 +0100361 unsigned long size;
362 void *start, *mem;
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800363 struct device_node **allnextp = mynodes;
364
365 pr_debug(" -> unflatten_device_tree()\n");
366
367 if (!blob) {
368 pr_debug("No device tree pointer\n");
369 return;
370 }
371
372 pr_debug("Unflattening device tree:\n");
373 pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
374 pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
375 pr_debug("version: %08x\n", be32_to_cpu(blob->version));
376
377 if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
378 pr_err("Invalid device tree blob header\n");
379 return;
380 }
381
382 /* First pass, scan for size */
Grant Likely44856812013-08-29 13:30:35 +0100383 start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
384 size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
385 size = ALIGN(size, 4);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800386
387 pr_debug(" size is %lx, allocating...\n", size);
388
389 /* Allocate memory for the expanded device tree */
Grant Likely44856812013-08-29 13:30:35 +0100390 mem = dt_alloc(size + 4, __alignof__(struct device_node));
391 memset(mem, 0, size);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800392
Grant Likely44856812013-08-29 13:30:35 +0100393 *(__be32 *)(mem + size) = cpu_to_be32(0xdeadbeef);
Wladislav Wiebe9e401272013-08-12 13:06:53 +0200394
Grant Likely44856812013-08-29 13:30:35 +0100395 pr_debug(" unflattening %p...\n", mem);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800396
397 /* Second pass, do actual unflattening */
Grant Likely44856812013-08-29 13:30:35 +0100398 start = ((void *)blob) + be32_to_cpu(blob->off_dt_struct);
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800399 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
Grant Likely44856812013-08-29 13:30:35 +0100400 if (be32_to_cpup(start) != OF_DT_END)
401 pr_warning("Weird tag at end of tree: %08x\n", be32_to_cpup(start));
402 if (be32_to_cpup(mem + size) != 0xdeadbeef)
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800403 pr_warning("End of tree marker overwritten: %08x\n",
Grant Likely44856812013-08-29 13:30:35 +0100404 be32_to_cpup(mem + size));
Stephen Neuendorfferfe140422010-11-18 15:55:02 -0800405 *allnextp = NULL;
406
407 pr_debug(" <- unflatten_device_tree()\n");
408}
409
410static void *kernel_tree_alloc(u64 size, u64 align)
411{
412 return kzalloc(size, GFP_KERNEL);
413}
414
415/**
416 * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
417 *
418 * unflattens the device-tree passed by the firmware, creating the
419 * tree of struct device_node. It also fills the "name" and "type"
420 * pointers of the nodes so the normal device-tree walking functions
421 * can be used.
422 */
423void of_fdt_unflatten_tree(unsigned long *blob,
424 struct device_node **mynodes)
425{
426 struct boot_param_header *device_tree =
427 (struct boot_param_header *)blob;
428 __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
429}
430EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
431
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800432/* Everything below here references initial_boot_params directly. */
433int __initdata dt_root_addr_cells;
434int __initdata dt_root_size_cells;
435
436struct boot_param_header *initial_boot_params;
437
438#ifdef CONFIG_OF_EARLY_FLATTREE
439
440/**
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100441 * res_mem_reserve_reg() - reserve all memory described in 'reg' property
442 */
443static int __init __reserved_mem_reserve_reg(unsigned long node,
444 const char *uname)
445{
446 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
447 phys_addr_t base, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500448 int len;
449 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100450 int nomap, first = 1;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100451
452 prop = of_get_flat_dt_prop(node, "reg", &len);
453 if (!prop)
454 return -ENOENT;
455
456 if (len && len % t_len != 0) {
457 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
458 uname);
459 return -EINVAL;
460 }
461
462 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
463
464 while (len >= t_len) {
465 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
466 size = dt_mem_next_cell(dt_root_size_cells, &prop);
467
468 if (base && size &&
469 early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
470 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
471 uname, &base, (unsigned long)size / SZ_1M);
472 else
473 pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
474 uname, &base, (unsigned long)size / SZ_1M);
475
476 len -= t_len;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100477 if (first) {
478 fdt_reserved_mem_save_node(node, uname, base, size);
479 first = 0;
480 }
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100481 }
482 return 0;
483}
484
485/**
486 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
487 * in /reserved-memory matches the values supported by the current implementation,
488 * also check if ranges property has been provided
489 */
Xiubo Li5b624112014-04-08 13:48:07 +0800490static int __init __reserved_mem_check_root(unsigned long node)
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100491{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500492 const __be32 *prop;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100493
494 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
495 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
496 return -EINVAL;
497
498 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
499 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
500 return -EINVAL;
501
502 prop = of_get_flat_dt_prop(node, "ranges", NULL);
503 if (!prop)
504 return -EINVAL;
505 return 0;
506}
507
508/**
509 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
510 */
511static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
512 int depth, void *data)
513{
514 static int found;
515 const char *status;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100516 int err;
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100517
518 if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
519 if (__reserved_mem_check_root(node) != 0) {
520 pr_err("Reserved memory: unsupported node format, ignoring\n");
521 /* break scan */
522 return 1;
523 }
524 found = 1;
525 /* scan next node */
526 return 0;
527 } else if (!found) {
528 /* scan next node */
529 return 0;
530 } else if (found && depth < 2) {
531 /* scanning of /reserved-memory has been finished */
532 return 1;
533 }
534
535 status = of_get_flat_dt_prop(node, "status", NULL);
536 if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
537 return 0;
538
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100539 err = __reserved_mem_reserve_reg(node, uname);
540 if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
541 fdt_reserved_mem_save_node(node, uname, 0, 0);
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100542
543 /* scan next node */
544 return 0;
545}
546
547/**
548 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
549 *
550 * This function grabs memory from early allocator for device exclusive use
551 * defined in device tree structures. It should be called by arch specific code
552 * once the early allocator (i.e. memblock) has been fully activated.
553 */
554void __init early_init_fdt_scan_reserved_mem(void)
555{
Josh Cartwright2040b522014-03-13 16:36:36 -0500556 if (!initial_boot_params)
557 return;
558
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100559 of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100560 fdt_init_reserved_mem();
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100561}
562
563/**
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800564 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
565 * @it: callback function
566 * @data: context data pointer
567 *
568 * This function is used to scan the flattened device-tree, it is
569 * used to extract the memory information at boot before we can
570 * unflatten the tree
571 */
572int __init of_scan_flat_dt(int (*it)(unsigned long node,
573 const char *uname, int depth,
574 void *data),
575 void *data)
576{
577 unsigned long p = ((unsigned long)initial_boot_params) +
578 be32_to_cpu(initial_boot_params->off_dt_struct);
579 int rc = 0;
580 int depth = -1;
581
582 do {
583 u32 tag = be32_to_cpup((__be32 *)p);
Fabio Estevame55b0822012-11-12 18:30:49 -0200584 const char *pathp;
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800585
586 p += 4;
587 if (tag == OF_DT_END_NODE) {
588 depth--;
589 continue;
590 }
591 if (tag == OF_DT_NOP)
592 continue;
593 if (tag == OF_DT_END)
594 break;
595 if (tag == OF_DT_PROP) {
596 u32 sz = be32_to_cpup((__be32 *)p);
597 p += 8;
598 if (be32_to_cpu(initial_boot_params->version) < 0x10)
599 p = ALIGN(p, sz >= 8 ? 8 : 4);
600 p += sz;
601 p = ALIGN(p, 4);
602 continue;
603 }
604 if (tag != OF_DT_BEGIN_NODE) {
605 pr_err("Invalid tag %x in flat device tree!\n", tag);
606 return -EINVAL;
607 }
608 depth++;
609 pathp = (char *)p;
610 p = ALIGN(p + strlen(pathp) + 1, 4);
Andy Shevchenko375da3a2012-12-17 16:01:28 -0800611 if (*pathp == '/')
612 pathp = kbasename(pathp);
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800613 rc = it(p, pathp, depth, data);
614 if (rc != 0)
615 break;
616 } while (1);
617
618 return rc;
619}
620
621/**
622 * of_get_flat_dt_root - find the root node in the flat blob
623 */
624unsigned long __init of_get_flat_dt_root(void)
625{
626 unsigned long p = ((unsigned long)initial_boot_params) +
627 be32_to_cpu(initial_boot_params->off_dt_struct);
628
629 while (be32_to_cpup((__be32 *)p) == OF_DT_NOP)
630 p += 4;
631 BUG_ON(be32_to_cpup((__be32 *)p) != OF_DT_BEGIN_NODE);
632 p += 4;
633 return ALIGN(p + strlen((char *)p) + 1, 4);
634}
635
636/**
637 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
638 *
639 * This function can be used within scan_flattened_dt callback to get
640 * access to properties
641 */
Rob Herring9d0c4df2014-04-01 23:49:03 -0500642const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
643 int *size)
Stephen Neuendorffer57d00ec2010-11-18 15:55:01 -0800644{
645 return of_fdt_get_property(initial_boot_params, node, name, size);
646}
647
648/**
649 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
650 * @node: node to test
651 * @compat: compatible string to compare with compatible list.
652 */
653int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
654{
655 return of_fdt_is_compatible(initial_boot_params, node, compat);
656}
657
Grant Likelya4f740c2010-10-30 11:49:09 -0400658/**
659 * of_flat_dt_match - Return true if node matches a list of compatible values
660 */
Uwe Kleine-König7b482c82011-12-20 22:56:45 +0100661int __init of_flat_dt_match(unsigned long node, const char *const *compat)
Grant Likelya4f740c2010-10-30 11:49:09 -0400662{
663 return of_fdt_match(initial_boot_params, node, compat);
664}
665
Marek Szyprowski57d74bc2013-08-26 14:41:56 +0200666struct fdt_scan_status {
667 const char *name;
668 int namelen;
669 int depth;
670 int found;
671 int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
672 void *data;
673};
674
Rob Herring6a903a22013-08-27 21:41:56 -0500675const char * __init of_flat_dt_get_machine_name(void)
676{
677 const char *name;
678 unsigned long dt_root = of_get_flat_dt_root();
679
680 name = of_get_flat_dt_prop(dt_root, "model", NULL);
681 if (!name)
682 name = of_get_flat_dt_prop(dt_root, "compatible", NULL);
683 return name;
684}
685
686/**
687 * of_flat_dt_match_machine - Iterate match tables to find matching machine.
688 *
689 * @default_match: A machine specific ptr to return in case of no match.
690 * @get_next_compat: callback function to return next compatible match table.
691 *
692 * Iterate through machine match tables to find the best match for the machine
693 * compatible string in the FDT.
694 */
695const void * __init of_flat_dt_match_machine(const void *default_match,
696 const void * (*get_next_compat)(const char * const**))
697{
698 const void *data = NULL;
699 const void *best_data = default_match;
700 const char *const *compat;
701 unsigned long dt_root;
702 unsigned int best_score = ~1, score = 0;
703
704 dt_root = of_get_flat_dt_root();
705 while ((data = get_next_compat(&compat))) {
706 score = of_flat_dt_match(dt_root, compat);
707 if (score > 0 && score < best_score) {
708 best_data = data;
709 best_score = score;
710 }
711 }
712 if (!best_data) {
713 const char *prop;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500714 int size;
Rob Herring6a903a22013-08-27 21:41:56 -0500715
716 pr_err("\n unrecognized device tree list:\n[ ");
717
718 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
719 if (prop) {
720 while (size > 0) {
721 printk("'%s' ", prop);
722 size -= strlen(prop) + 1;
723 prop += strlen(prop) + 1;
724 }
725 }
726 printk("]\n\n");
727 return NULL;
728 }
729
730 pr_info("Machine model: %s\n", of_flat_dt_get_machine_name());
731
732 return best_data;
733}
734
Grant Likelyf7b3a832009-11-24 03:26:58 -0700735#ifdef CONFIG_BLK_DEV_INITRD
736/**
737 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
738 * @node: reference to node containing initrd location ('chosen')
739 */
Rob Herring29eb45a2013-08-30 17:06:53 -0500740static void __init early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700741{
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400742 u64 start, end;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500743 int len;
744 const __be32 *prop;
Grant Likelyf7b3a832009-11-24 03:26:58 -0700745
746 pr_debug("Looking for initrd properties... ");
747
748 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700749 if (!prop)
750 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400751 start = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700752
Jeremy Kerr1406bc22010-01-30 01:31:21 -0700753 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
754 if (!prop)
755 return;
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400756 end = of_read_number(prop, len/4);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700757
Rob Herring29eb45a2013-08-30 17:06:53 -0500758 initrd_start = (unsigned long)__va(start);
759 initrd_end = (unsigned long)__va(end);
760 initrd_below_start_ok = 1;
761
Santosh Shilimkar374d5c92013-07-01 14:20:35 -0400762 pr_debug("initrd_start=0x%llx initrd_end=0x%llx\n",
763 (unsigned long long)start, (unsigned long long)end);
Grant Likelyf7b3a832009-11-24 03:26:58 -0700764}
765#else
Rob Herring29eb45a2013-08-30 17:06:53 -0500766static inline void early_init_dt_check_for_initrd(unsigned long node)
Grant Likelyf7b3a832009-11-24 03:26:58 -0700767{
768}
769#endif /* CONFIG_BLK_DEV_INITRD */
770
Grant Likely41f88002009-11-23 20:07:01 -0700771/**
Grant Likelyf00abd92009-11-24 03:27:10 -0700772 * early_init_dt_scan_root - fetch the top level address and size cells
773 */
774int __init early_init_dt_scan_root(unsigned long node, const char *uname,
775 int depth, void *data)
776{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500777 const __be32 *prop;
Grant Likelyf00abd92009-11-24 03:27:10 -0700778
779 if (depth != 0)
780 return 0;
781
Jeremy Kerr33714882010-01-30 01:45:26 -0700782 dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
783 dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
784
Grant Likelyf00abd92009-11-24 03:27:10 -0700785 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700786 if (prop)
787 dt_root_size_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700788 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
789
790 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Jeremy Kerr33714882010-01-30 01:45:26 -0700791 if (prop)
792 dt_root_addr_cells = be32_to_cpup(prop);
Grant Likelyf00abd92009-11-24 03:27:10 -0700793 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
794
795 /* break now */
796 return 1;
797}
798
Rob Herring9d0c4df2014-04-01 23:49:03 -0500799u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
Grant Likely83f7a062009-11-24 03:37:56 -0700800{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500801 const __be32 *p = *cellp;
Grant Likely83f7a062009-11-24 03:37:56 -0700802
803 *cellp = p + s;
804 return of_read_number(p, s);
805}
806
Grant Likely51975db2010-02-01 21:34:14 -0700807/**
808 * early_init_dt_scan_memory - Look for an parse memory nodes
809 */
810int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
811 int depth, void *data)
812{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500813 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
814 const __be32 *reg, *endp;
815 int l;
Grant Likely51975db2010-02-01 21:34:14 -0700816
817 /* We are scanning "memory" nodes only */
818 if (type == NULL) {
819 /*
820 * The longtrail doesn't have a device_type on the
821 * /memory node, so look for the node called /memory@0.
822 */
823 if (depth != 1 || strcmp(uname, "memory@0") != 0)
824 return 0;
825 } else if (strcmp(type, "memory") != 0)
826 return 0;
827
828 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
829 if (reg == NULL)
830 reg = of_get_flat_dt_prop(node, "reg", &l);
831 if (reg == NULL)
832 return 0;
833
834 endp = reg + (l / sizeof(__be32));
835
Rob Herring9d0c4df2014-04-01 23:49:03 -0500836 pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
Grant Likely51975db2010-02-01 21:34:14 -0700837 uname, l, reg[0], reg[1], reg[2], reg[3]);
838
839 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
840 u64 base, size;
841
842 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
843 size = dt_mem_next_cell(dt_root_size_cells, &reg);
844
845 if (size == 0)
846 continue;
847 pr_debug(" - %llx , %llx\n", (unsigned long long)base,
848 (unsigned long long)size);
849
850 early_init_dt_add_memory_arch(base, size);
851 }
852
853 return 0;
854}
855
Grant Likely86e03222009-12-10 23:42:21 -0700856int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
857 int depth, void *data)
858{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500859 int l;
860 const char *p;
Grant Likely86e03222009-12-10 23:42:21 -0700861
862 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
863
Grant Likely85f60ae2011-04-29 00:18:16 -0600864 if (depth != 1 || !data ||
Grant Likely86e03222009-12-10 23:42:21 -0700865 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
866 return 0;
867
868 early_init_dt_check_for_initrd(node);
869
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300870 /* Retrieve command line */
Grant Likely86e03222009-12-10 23:42:21 -0700871 p = of_get_flat_dt_prop(node, "bootargs", &l);
872 if (p != NULL && l > 0)
Grant Likely85f60ae2011-04-29 00:18:16 -0600873 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
Grant Likely86e03222009-12-10 23:42:21 -0700874
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000875 /*
876 * CONFIG_CMDLINE is meant to be a default in case nothing else
877 * managed to set the command line, unless CONFIG_CMDLINE_FORCE
878 * is set in which case we override whatever was found earlier.
879 */
Grant Likely86e03222009-12-10 23:42:21 -0700880#ifdef CONFIG_CMDLINE
881#ifndef CONFIG_CMDLINE_FORCE
Benjamin Herrenschmidt78b782c2011-09-19 18:50:15 +0000882 if (!((char *)data)[0])
Grant Likely86e03222009-12-10 23:42:21 -0700883#endif
Grant Likely85f60ae2011-04-29 00:18:16 -0600884 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
Grant Likely86e03222009-12-10 23:42:21 -0700885#endif /* CONFIG_CMDLINE */
886
Grant Likely85f60ae2011-04-29 00:18:16 -0600887 pr_debug("Command line is: %s\n", (char*)data);
Grant Likely86e03222009-12-10 23:42:21 -0700888
889 /* break now */
890 return 1;
891}
892
Grant Likelya1727da2013-08-28 21:18:32 +0100893#ifdef CONFIG_HAVE_MEMBLOCK
Rob Herring068f6312013-09-24 22:20:01 -0500894void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
895{
896 const u64 phys_offset = __pa(PAGE_OFFSET);
897 base &= PAGE_MASK;
898 size &= PAGE_MASK;
899 if (base + size < phys_offset) {
900 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
901 base, base + size);
902 return;
903 }
904 if (base < phys_offset) {
905 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
906 base, phys_offset);
907 size -= phys_offset - base;
908 base = phys_offset;
909 }
910 memblock_add(base, size);
911}
912
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100913int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
914 phys_addr_t size, bool nomap)
915{
916 if (memblock_is_region_reserved(base, size))
917 return -EBUSY;
918 if (nomap)
919 return memblock_remove(base, size);
920 return memblock_reserve(base, size);
921}
922
Grant Likelya1727da2013-08-28 21:18:32 +0100923/*
924 * called from unflatten_device_tree() to bootstrap devicetree itself
925 * Architectures can override this definition if memblock isn't used
926 */
927void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
928{
929 return __va(memblock_alloc(size, align));
930}
Marek Szyprowskie8d9d1f2014-02-28 14:42:47 +0100931#else
932int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
933 phys_addr_t size, bool nomap)
934{
935 pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
936 base, size, nomap ? " (nomap)" : "");
937 return -ENOSYS;
938}
Grant Likelya1727da2013-08-28 21:18:32 +0100939#endif
940
Rob Herring0288ffcb2013-08-26 09:47:40 -0500941bool __init early_init_dt_scan(void *params)
942{
943 if (!params)
944 return false;
945
946 /* Setup flat device-tree pointer */
947 initial_boot_params = params;
948
949 /* check device tree validity */
950 if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
951 initial_boot_params = NULL;
952 return false;
953 }
954
955 /* Retrieve various information from the /chosen node */
956 of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
957
958 /* Initialize {size,address}-cells info */
959 of_scan_flat_dt(early_init_dt_scan_root, NULL);
960
961 /* Setup memory, calling early_init_dt_add_memory_arch */
962 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
963
964 return true;
965}
966
Grant Likelyf00abd92009-11-24 03:27:10 -0700967/**
Grant Likely41f88002009-11-23 20:07:01 -0700968 * unflatten_device_tree - create tree of device_nodes from flat blob
969 *
970 * unflattens the device-tree passed by the firmware, creating the
971 * tree of struct device_node. It also fills the "name" and "type"
972 * pointers of the nodes so the normal device-tree walking functions
973 * can be used.
974 */
975void __init unflatten_device_tree(void)
976{
Randy Dunlap465aac62012-11-30 10:01:51 +0000977 __unflatten_device_tree(initial_boot_params, &of_allnodes,
Grant Likely672c5442011-01-13 15:36:09 -0700978 early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700979
Robert P. J. Day4c7d6362013-05-30 05:38:08 -0400980 /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
Shawn Guo611cad72011-08-15 15:28:14 +0800981 of_alias_scan(early_init_dt_alloc_memory_arch);
Grant Likely41f88002009-11-23 20:07:01 -0700982}
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -0800983
Rob Herringa8bf7522013-08-26 11:22:45 -0500984/**
985 * unflatten_and_copy_device_tree - copy and create tree of device_nodes from flat blob
986 *
987 * Copies and unflattens the device-tree passed by the firmware, creating the
988 * tree of struct device_node. It also fills the "name" and "type"
989 * pointers of the nodes so the normal device-tree walking functions
990 * can be used. This should only be used when the FDT memory has not been
991 * reserved such is the case when the FDT is built-in to the kernel init
992 * section. If the FDT memory is reserved already then unflatten_device_tree
993 * should be used instead.
994 */
995void __init unflatten_and_copy_device_tree(void)
996{
James Hogan6f041e92013-11-21 13:44:14 +0000997 int size;
998 void *dt;
999
1000 if (!initial_boot_params) {
1001 pr_warn("No valid device tree found, continuing without\n");
1002 return;
1003 }
1004
1005 size = __be32_to_cpu(initial_boot_params->totalsize);
1006 dt = early_init_dt_alloc_memory_arch(size,
Rob Herringa8bf7522013-08-26 11:22:45 -05001007 __alignof__(struct boot_param_header));
1008
1009 if (dt) {
1010 memcpy(dt, initial_boot_params, size);
1011 initial_boot_params = dt;
1012 }
1013 unflatten_device_tree();
1014}
1015
Stephen Neuendorffere6ce1322010-11-18 15:54:56 -08001016#endif /* CONFIG_OF_EARLY_FLATTREE */