Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.git] / drivers / of / fdt.c
1 /*
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
12 #include <linux/kernel.h>
13 #include <linux/initrd.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/sizes.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/memblock.h>
23 #include <linux/libfdt.h>
24
25 #include <asm/setup.h>  /* for COMMAND_LINE_SIZE */
26 #ifdef CONFIG_PPC
27 #include <asm/machdep.h>
28 #endif /* CONFIG_PPC */
29
30 #include <asm/page.h>
31
32 /**
33  * of_fdt_is_compatible - Return true if given node from the given blob has
34  * compat in its compatible list
35  * @blob: A device tree blob
36  * @node: node to test
37  * @compat: compatible string to compare with compatible list.
38  *
39  * On match, returns a non-zero value with smaller values returned for more
40  * specific compatible values.
41  */
42 int of_fdt_is_compatible(struct boot_param_header *blob,
43                       unsigned long node, const char *compat)
44 {
45         const char *cp;
46         int cplen;
47         unsigned long l, score = 0;
48
49         cp = fdt_getprop(blob, node, "compatible", &cplen);
50         if (cp == NULL)
51                 return 0;
52         while (cplen > 0) {
53                 score++;
54                 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
55                         return score;
56                 l = strlen(cp) + 1;
57                 cp += l;
58                 cplen -= l;
59         }
60
61         return 0;
62 }
63
64 /**
65  * of_fdt_match - Return true if node matches a list of compatible values
66  */
67 int of_fdt_match(struct boot_param_header *blob, unsigned long node,
68                  const char *const *compat)
69 {
70         unsigned int tmp, score = 0;
71
72         if (!compat)
73                 return 0;
74
75         while (*compat) {
76                 tmp = of_fdt_is_compatible(blob, node, *compat);
77                 if (tmp && (score == 0 || (tmp < score)))
78                         score = tmp;
79                 compat++;
80         }
81
82         return score;
83 }
84
85 static void *unflatten_dt_alloc(void **mem, unsigned long size,
86                                        unsigned long align)
87 {
88         void *res;
89
90         *mem = PTR_ALIGN(*mem, align);
91         res = (void *)*mem;
92         *mem += size;
93
94         return res;
95 }
96
97 /**
98  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
99  * @blob: The parent device tree blob
100  * @mem: Memory chunk to use for allocating device nodes and properties
101  * @p: pointer to node in flat tree
102  * @dad: Parent struct device_node
103  * @allnextpp: pointer to ->allnext from last allocated device_node
104  * @fpsize: Size of the node path up at the current depth.
105  */
106 static void * unflatten_dt_node(struct boot_param_header *blob,
107                                 void *mem,
108                                 int *poffset,
109                                 struct device_node *dad,
110                                 struct device_node ***allnextpp,
111                                 unsigned long fpsize)
112 {
113         const __be32 *p;
114         struct device_node *np;
115         struct property *pp, **prev_pp = NULL;
116         const char *pathp;
117         unsigned int l, allocl;
118         static int depth = 0;
119         int old_depth;
120         int offset;
121         int has_name = 0;
122         int new_format = 0;
123
124         pathp = fdt_get_name(blob, *poffset, &l);
125         if (!pathp)
126                 return mem;
127
128         allocl = l++;
129
130         /* version 0x10 has a more compact unit name here instead of the full
131          * path. we accumulate the full path size using "fpsize", we'll rebuild
132          * it later. We detect this because the first character of the name is
133          * not '/'.
134          */
135         if ((*pathp) != '/') {
136                 new_format = 1;
137                 if (fpsize == 0) {
138                         /* root node: special case. fpsize accounts for path
139                          * plus terminating zero. root node only has '/', so
140                          * fpsize should be 2, but we want to avoid the first
141                          * level nodes to have two '/' so we use fpsize 1 here
142                          */
143                         fpsize = 1;
144                         allocl = 2;
145                         l = 1;
146                         pathp = "";
147                 } else {
148                         /* account for '/' and path size minus terminal 0
149                          * already in 'l'
150                          */
151                         fpsize += l;
152                         allocl = fpsize;
153                 }
154         }
155
156         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
157                                 __alignof__(struct device_node));
158         if (allnextpp) {
159                 char *fn;
160                 of_node_init(np);
161                 np->full_name = fn = ((char *)np) + sizeof(*np);
162                 if (new_format) {
163                         /* rebuild full path for new format */
164                         if (dad && dad->parent) {
165                                 strcpy(fn, dad->full_name);
166 #ifdef DEBUG
167                                 if ((strlen(fn) + l + 1) != allocl) {
168                                         pr_debug("%s: p: %d, l: %d, a: %d\n",
169                                                 pathp, (int)strlen(fn),
170                                                 l, allocl);
171                                 }
172 #endif
173                                 fn += strlen(fn);
174                         }
175                         *(fn++) = '/';
176                 }
177                 memcpy(fn, pathp, l);
178
179                 prev_pp = &np->properties;
180                 **allnextpp = np;
181                 *allnextpp = &np->allnext;
182                 if (dad != NULL) {
183                         np->parent = dad;
184                         /* we temporarily use the next field as `last_child'*/
185                         if (dad->next == NULL)
186                                 dad->child = np;
187                         else
188                                 dad->next->sibling = np;
189                         dad->next = np;
190                 }
191         }
192         /* process properties */
193         for (offset = fdt_first_property_offset(blob, *poffset);
194              (offset >= 0);
195              (offset = fdt_next_property_offset(blob, offset))) {
196                 const char *pname;
197                 u32 sz;
198
199                 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
200                         offset = -FDT_ERR_INTERNAL;
201                         break;
202                 }
203
204                 if (pname == NULL) {
205                         pr_info("Can't find property name in list !\n");
206                         break;
207                 }
208                 if (strcmp(pname, "name") == 0)
209                         has_name = 1;
210                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
211                                         __alignof__(struct property));
212                 if (allnextpp) {
213                         /* We accept flattened tree phandles either in
214                          * ePAPR-style "phandle" properties, or the
215                          * legacy "linux,phandle" properties.  If both
216                          * appear and have different values, things
217                          * will get weird.  Don't do that. */
218                         if ((strcmp(pname, "phandle") == 0) ||
219                             (strcmp(pname, "linux,phandle") == 0)) {
220                                 if (np->phandle == 0)
221                                         np->phandle = be32_to_cpup(p);
222                         }
223                         /* And we process the "ibm,phandle" property
224                          * used in pSeries dynamic device tree
225                          * stuff */
226                         if (strcmp(pname, "ibm,phandle") == 0)
227                                 np->phandle = be32_to_cpup(p);
228                         pp->name = (char *)pname;
229                         pp->length = sz;
230                         pp->value = (__be32 *)p;
231                         *prev_pp = pp;
232                         prev_pp = &pp->next;
233                 }
234         }
235         /* with version 0x10 we may not have the name property, recreate
236          * it here from the unit name if absent
237          */
238         if (!has_name) {
239                 const char *p1 = pathp, *ps = pathp, *pa = NULL;
240                 int sz;
241
242                 while (*p1) {
243                         if ((*p1) == '@')
244                                 pa = p1;
245                         if ((*p1) == '/')
246                                 ps = p1 + 1;
247                         p1++;
248                 }
249                 if (pa < ps)
250                         pa = p1;
251                 sz = (pa - ps) + 1;
252                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
253                                         __alignof__(struct property));
254                 if (allnextpp) {
255                         pp->name = "name";
256                         pp->length = sz;
257                         pp->value = pp + 1;
258                         *prev_pp = pp;
259                         prev_pp = &pp->next;
260                         memcpy(pp->value, ps, sz - 1);
261                         ((char *)pp->value)[sz - 1] = 0;
262                         pr_debug("fixed up name for %s -> %s\n", pathp,
263                                 (char *)pp->value);
264                 }
265         }
266         if (allnextpp) {
267                 *prev_pp = NULL;
268                 np->name = of_get_property(np, "name", NULL);
269                 np->type = of_get_property(np, "device_type", NULL);
270
271                 if (!np->name)
272                         np->name = "<NULL>";
273                 if (!np->type)
274                         np->type = "<NULL>";
275         }
276
277         old_depth = depth;
278         *poffset = fdt_next_node(blob, *poffset, &depth);
279         if (depth < 0)
280                 depth = 0;
281         while (*poffset > 0 && depth > old_depth)
282                 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
283                                         fpsize);
284
285         if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
286                 pr_err("unflatten: error %d processing FDT\n", *poffset);
287
288         return mem;
289 }
290
291 /**
292  * __unflatten_device_tree - create tree of device_nodes from flat blob
293  *
294  * unflattens a device-tree, creating the
295  * tree of struct device_node. It also fills the "name" and "type"
296  * pointers of the nodes so the normal device-tree walking functions
297  * can be used.
298  * @blob: The blob to expand
299  * @mynodes: The device_node tree created by the call
300  * @dt_alloc: An allocator that provides a virtual address to memory
301  * for the resulting tree
302  */
303 static void __unflatten_device_tree(struct boot_param_header *blob,
304                              struct device_node **mynodes,
305                              void * (*dt_alloc)(u64 size, u64 align))
306 {
307         unsigned long size;
308         int start;
309         void *mem;
310         struct device_node **allnextp = mynodes;
311
312         pr_debug(" -> unflatten_device_tree()\n");
313
314         if (!blob) {
315                 pr_debug("No device tree pointer\n");
316                 return;
317         }
318
319         pr_debug("Unflattening device tree:\n");
320         pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
321         pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
322         pr_debug("version: %08x\n", be32_to_cpu(blob->version));
323
324         if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
325                 pr_err("Invalid device tree blob header\n");
326                 return;
327         }
328
329         /* First pass, scan for size */
330         start = 0;
331         size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
332         size = ALIGN(size, 4);
333
334         pr_debug("  size is %lx, allocating...\n", size);
335
336         /* Allocate memory for the expanded device tree */
337         mem = dt_alloc(size + 4, __alignof__(struct device_node));
338
339         memset((void *)mem, 0, size);
340
341         memset((void *)mem, 0, size);
342
343         ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
344
345         pr_debug("  unflattening %p...\n", mem);
346
347         /* Second pass, do actual unflattening */
348         start = 0;
349         unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
350         if (be32_to_cpup(mem + size) != 0xdeadbeef)
351                 pr_warning("End of tree marker overwritten: %08x\n",
352                            be32_to_cpu(((__be32 *)mem)[size / 4]));
353         *allnextp = NULL;
354
355         pr_debug(" <- unflatten_device_tree()\n");
356 }
357
358 static void *kernel_tree_alloc(u64 size, u64 align)
359 {
360         return kzalloc(size, GFP_KERNEL);
361 }
362
363 /**
364  * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
365  *
366  * unflattens the device-tree passed by the firmware, creating the
367  * tree of struct device_node. It also fills the "name" and "type"
368  * pointers of the nodes so the normal device-tree walking functions
369  * can be used.
370  */
371 void of_fdt_unflatten_tree(unsigned long *blob,
372                         struct device_node **mynodes)
373 {
374         struct boot_param_header *device_tree =
375                 (struct boot_param_header *)blob;
376         __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
377 }
378 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
379
380 /* Everything below here references initial_boot_params directly. */
381 int __initdata dt_root_addr_cells;
382 int __initdata dt_root_size_cells;
383
384 struct boot_param_header *initial_boot_params;
385
386 #ifdef CONFIG_OF_EARLY_FLATTREE
387
388 /**
389  * res_mem_reserve_reg() - reserve all memory described in 'reg' property
390  */
391 static int __init __reserved_mem_reserve_reg(unsigned long node,
392                                              const char *uname)
393 {
394         int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
395         phys_addr_t base, size;
396         int len;
397         const __be32 *prop;
398         int nomap, first = 1;
399
400         prop = of_get_flat_dt_prop(node, "reg", &len);
401         if (!prop)
402                 return -ENOENT;
403
404         if (len && len % t_len != 0) {
405                 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
406                        uname);
407                 return -EINVAL;
408         }
409
410         nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
411
412         while (len >= t_len) {
413                 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
414                 size = dt_mem_next_cell(dt_root_size_cells, &prop);
415
416                 if (base && size &&
417                     early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
418                         pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
419                                 uname, &base, (unsigned long)size / SZ_1M);
420                 else
421                         pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
422                                 uname, &base, (unsigned long)size / SZ_1M);
423
424                 len -= t_len;
425                 if (first) {
426                         fdt_reserved_mem_save_node(node, uname, base, size);
427                         first = 0;
428                 }
429         }
430         return 0;
431 }
432
433 /**
434  * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
435  * in /reserved-memory matches the values supported by the current implementation,
436  * also check if ranges property has been provided
437  */
438 static int __init __reserved_mem_check_root(unsigned long node)
439 {
440         const __be32 *prop;
441
442         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
443         if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
444                 return -EINVAL;
445
446         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
447         if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
448                 return -EINVAL;
449
450         prop = of_get_flat_dt_prop(node, "ranges", NULL);
451         if (!prop)
452                 return -EINVAL;
453         return 0;
454 }
455
456 /**
457  * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
458  */
459 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
460                                           int depth, void *data)
461 {
462         static int found;
463         const char *status;
464         int err;
465
466         if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
467                 if (__reserved_mem_check_root(node) != 0) {
468                         pr_err("Reserved memory: unsupported node format, ignoring\n");
469                         /* break scan */
470                         return 1;
471                 }
472                 found = 1;
473                 /* scan next node */
474                 return 0;
475         } else if (!found) {
476                 /* scan next node */
477                 return 0;
478         } else if (found && depth < 2) {
479                 /* scanning of /reserved-memory has been finished */
480                 return 1;
481         }
482
483         status = of_get_flat_dt_prop(node, "status", NULL);
484         if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
485                 return 0;
486
487         err = __reserved_mem_reserve_reg(node, uname);
488         if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
489                 fdt_reserved_mem_save_node(node, uname, 0, 0);
490
491         /* scan next node */
492         return 0;
493 }
494
495 /**
496  * early_init_fdt_scan_reserved_mem() - create reserved memory regions
497  *
498  * This function grabs memory from early allocator for device exclusive use
499  * defined in device tree structures. It should be called by arch specific code
500  * once the early allocator (i.e. memblock) has been fully activated.
501  */
502 void __init early_init_fdt_scan_reserved_mem(void)
503 {
504         if (!initial_boot_params)
505                 return;
506
507         of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
508         fdt_init_reserved_mem();
509 }
510
511 /**
512  * of_scan_flat_dt - scan flattened tree blob and call callback on each.
513  * @it: callback function
514  * @data: context data pointer
515  *
516  * This function is used to scan the flattened device-tree, it is
517  * used to extract the memory information at boot before we can
518  * unflatten the tree
519  */
520 int __init of_scan_flat_dt(int (*it)(unsigned long node,
521                                      const char *uname, int depth,
522                                      void *data),
523                            void *data)
524 {
525         const void *blob = initial_boot_params;
526         const char *pathp;
527         int offset, rc = 0, depth = -1;
528
529         for (offset = fdt_next_node(blob, -1, &depth);
530              offset >= 0 && depth >= 0 && !rc;
531              offset = fdt_next_node(blob, offset, &depth)) {
532
533                 pathp = fdt_get_name(blob, offset, NULL);
534                 if (*pathp == '/')
535                         pathp = kbasename(pathp);
536                 rc = it(offset, pathp, depth, data);
537         }
538         return rc;
539 }
540
541 /**
542  * of_get_flat_dt_root - find the root node in the flat blob
543  */
544 unsigned long __init of_get_flat_dt_root(void)
545 {
546         return 0;
547 }
548
549 /**
550  * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
551  *
552  * This function can be used within scan_flattened_dt callback to get
553  * access to properties
554  */
555 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
556                                        int *size)
557 {
558         return fdt_getprop(initial_boot_params, node, name, size);
559 }
560
561 /**
562  * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
563  * @node: node to test
564  * @compat: compatible string to compare with compatible list.
565  */
566 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
567 {
568         return of_fdt_is_compatible(initial_boot_params, node, compat);
569 }
570
571 /**
572  * of_flat_dt_match - Return true if node matches a list of compatible values
573  */
574 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
575 {
576         return of_fdt_match(initial_boot_params, node, compat);
577 }
578
579 struct fdt_scan_status {
580         const char *name;
581         int namelen;
582         int depth;
583         int found;
584         int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
585         void *data;
586 };
587
588 #ifdef CONFIG_BLK_DEV_INITRD
589 /**
590  * early_init_dt_check_for_initrd - Decode initrd location from flat tree
591  * @node: reference to node containing initrd location ('chosen')
592  */
593 void __init early_init_dt_check_for_initrd(unsigned long node)
594 {
595         u64 start, end;
596         int len;
597         const __be32 *prop;
598
599         pr_debug("Looking for initrd properties... ");
600
601         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
602         if (!prop)
603                 return;
604         start = of_read_number(prop, len/4);
605
606         prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
607         if (!prop)
608                 return;
609         end = of_read_number(prop, len/4);
610
611         early_init_dt_setup_initrd_arch(start, end);
612         pr_debug("initrd_start=0x%llx  initrd_end=0x%llx\n",
613                  (unsigned long long)start, (unsigned long long)end);
614 }
615 #else
616 inline void early_init_dt_check_for_initrd(unsigned long node)
617 {
618 }
619 #endif /* CONFIG_BLK_DEV_INITRD */
620
621 /**
622  * early_init_dt_scan_root - fetch the top level address and size cells
623  */
624 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
625                                    int depth, void *data)
626 {
627         const __be32 *prop;
628
629         if (depth != 0)
630                 return 0;
631
632         dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
633         dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
634
635         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
636         if (prop)
637                 dt_root_size_cells = be32_to_cpup(prop);
638         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
639
640         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
641         if (prop)
642                 dt_root_addr_cells = be32_to_cpup(prop);
643         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
644
645         /* break now */
646         return 1;
647 }
648
649 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
650 {
651         const __be32 *p = *cellp;
652
653         *cellp = p + s;
654         return of_read_number(p, s);
655 }
656
657 /**
658  * early_init_dt_scan_memory - Look for an parse memory nodes
659  */
660 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
661                                      int depth, void *data)
662 {
663         const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
664         const __be32 *reg, *endp;
665         int l;
666
667         /* We are scanning "memory" nodes only */
668         if (type == NULL) {
669                 /*
670                  * The longtrail doesn't have a device_type on the
671                  * /memory node, so look for the node called /memory@0.
672                  */
673                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
674                         return 0;
675         } else if (strcmp(type, "memory") != 0)
676                 return 0;
677
678         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
679         if (reg == NULL)
680                 reg = of_get_flat_dt_prop(node, "reg", &l);
681         if (reg == NULL)
682                 return 0;
683
684         endp = reg + (l / sizeof(__be32));
685
686         pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
687             uname, l, reg[0], reg[1], reg[2], reg[3]);
688
689         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
690                 u64 base, size;
691
692                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
693                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
694
695                 if (size == 0)
696                         continue;
697                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
698                     (unsigned long long)size);
699
700                 early_init_dt_add_memory_arch(base, size);
701         }
702
703         return 0;
704 }
705
706 /*
707  * Convert configs to something easy to use in C code
708  */
709 #if defined(CONFIG_CMDLINE_FORCE)
710 static const int overwrite_incoming_cmdline = 1;
711 static const int read_dt_cmdline;
712 static const int concat_cmdline;
713 #elif defined(CONFIG_CMDLINE_EXTEND)
714 static const int overwrite_incoming_cmdline;
715 static const int read_dt_cmdline = 1;
716 static const int concat_cmdline = 1;
717 #else /* CMDLINE_FROM_BOOTLOADER */
718 static const int overwrite_incoming_cmdline;
719 static const int read_dt_cmdline = 1;
720 static const int concat_cmdline;
721 #endif
722
723 #ifdef CONFIG_CMDLINE
724 static const char *config_cmdline = CONFIG_CMDLINE;
725 #else
726 static const char *config_cmdline = "";
727 #endif
728
729 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
730                                      int depth, void *data)
731 {
732         int l;
733         const char *p;
734         char *cmdline = data;
735
736         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
737
738         if (depth != 1 || !cmdline ||
739             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
740                 return 0;
741
742         early_init_dt_check_for_initrd(node);
743
744         /* Put CONFIG_CMDLINE in if forced or if data had nothing in it to start */
745         if (overwrite_incoming_cmdline || !cmdline[0])
746                 strlcpy(cmdline, config_cmdline, COMMAND_LINE_SIZE);
747
748         /* Retrieve command line unless forcing */
749         if (read_dt_cmdline)
750                 p = of_get_flat_dt_prop(node, "bootargs", &l);
751
752         if (p != NULL && l > 0) {
753                 if (concat_cmdline) {
754                         int cmdline_len;
755                         int copy_len;
756                         strlcat(cmdline, " ", COMMAND_LINE_SIZE);
757                         cmdline_len = strlen(cmdline);
758                         copy_len = COMMAND_LINE_SIZE - cmdline_len - 1;
759                         copy_len = min((int)l, copy_len);
760                         strncpy(cmdline + cmdline_len, p, copy_len);
761                         cmdline[cmdline_len + copy_len] = '\0';
762                 } else {
763                         strlcpy(cmdline, p, min((int)l, COMMAND_LINE_SIZE));
764                 }
765         }
766
767         pr_debug("Command line is: %s\n", (char*)data);
768
769         /* break now */
770         return 1;
771 }
772
773 #ifdef CONFIG_HAVE_MEMBLOCK
774 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
775 {
776         const u64 phys_offset = __pa(PAGE_OFFSET);
777         base &= PAGE_MASK;
778         size &= PAGE_MASK;
779         if (base + size < phys_offset) {
780                 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
781                            base, base + size);
782                 return;
783         }
784         if (base < phys_offset) {
785                 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
786                            base, phys_offset);
787                 size -= phys_offset - base;
788                 base = phys_offset;
789         }
790         memblock_add(base, size);
791 }
792
793 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
794                                         phys_addr_t size, bool nomap)
795 {
796         if (memblock_is_region_reserved(base, size))
797                 return -EBUSY;
798         if (nomap)
799                 return memblock_remove(base, size);
800         return memblock_reserve(base, size);
801 }
802
803 /*
804  * called from unflatten_device_tree() to bootstrap devicetree itself
805  * Architectures can override this definition if memblock isn't used
806  */
807 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
808 {
809         return __va(memblock_alloc(size, align));
810 }
811 #else
812 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
813                                         phys_addr_t size, bool nomap)
814 {
815         pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
816                   base, size, nomap ? " (nomap)" : "");
817         return -ENOSYS;
818 }
819 #endif
820
821 bool __init early_init_dt_scan(void *params)
822 {
823         if (!params)
824                 return false;
825
826         /* Setup flat device-tree pointer */
827         initial_boot_params = params;
828
829         /* check device tree validity */
830         if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
831                 initial_boot_params = NULL;
832                 return false;
833         }
834
835         /* Retrieve various information from the /chosen node */
836         of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
837
838         /* Initialize {size,address}-cells info */
839         of_scan_flat_dt(early_init_dt_scan_root, NULL);
840
841         /* Setup memory, calling early_init_dt_add_memory_arch */
842         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
843
844         return true;
845 }
846
847 /**
848  * unflatten_device_tree - create tree of device_nodes from flat blob
849  *
850  * unflattens the device-tree passed by the firmware, creating the
851  * tree of struct device_node. It also fills the "name" and "type"
852  * pointers of the nodes so the normal device-tree walking functions
853  * can be used.
854  */
855 void __init unflatten_device_tree(void)
856 {
857         __unflatten_device_tree(initial_boot_params, &of_allnodes,
858                                 early_init_dt_alloc_memory_arch);
859
860         /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
861         of_alias_scan(early_init_dt_alloc_memory_arch);
862 }
863
864 #endif /* CONFIG_OF_EARLY_FLATTREE */