Merge tag 'v3.10.13' into lsk/v3.10/topic/kvm
[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                 memset(np, 0, sizeof(*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                 kref_init(&np->kref);
192         }
193         /* process properties */
194         for (offset = fdt_first_property_offset(blob, *poffset);
195              (offset >= 0);
196              (offset = fdt_next_property_offset(blob, offset))) {
197                 const char *pname;
198                 u32 sz;
199
200                 if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
201                         offset = -FDT_ERR_INTERNAL;
202                         break;
203                 }
204
205                 if (pname == NULL) {
206                         pr_info("Can't find property name in list !\n");
207                         break;
208                 }
209                 if (strcmp(pname, "name") == 0)
210                         has_name = 1;
211                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
212                                         __alignof__(struct property));
213                 if (allnextpp) {
214                         /* We accept flattened tree phandles either in
215                          * ePAPR-style "phandle" properties, or the
216                          * legacy "linux,phandle" properties.  If both
217                          * appear and have different values, things
218                          * will get weird.  Don't do that. */
219                         if ((strcmp(pname, "phandle") == 0) ||
220                             (strcmp(pname, "linux,phandle") == 0)) {
221                                 if (np->phandle == 0)
222                                         np->phandle = be32_to_cpup(p);
223                         }
224                         /* And we process the "ibm,phandle" property
225                          * used in pSeries dynamic device tree
226                          * stuff */
227                         if (strcmp(pname, "ibm,phandle") == 0)
228                                 np->phandle = be32_to_cpup(p);
229                         pp->name = (char *)pname;
230                         pp->length = sz;
231                         pp->value = (__be32 *)p;
232                         *prev_pp = pp;
233                         prev_pp = &pp->next;
234                 }
235         }
236         /* with version 0x10 we may not have the name property, recreate
237          * it here from the unit name if absent
238          */
239         if (!has_name) {
240                 const char *p1 = pathp, *ps = pathp, *pa = NULL;
241                 int sz;
242
243                 while (*p1) {
244                         if ((*p1) == '@')
245                                 pa = p1;
246                         if ((*p1) == '/')
247                                 ps = p1 + 1;
248                         p1++;
249                 }
250                 if (pa < ps)
251                         pa = p1;
252                 sz = (pa - ps) + 1;
253                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
254                                         __alignof__(struct property));
255                 if (allnextpp) {
256                         pp->name = "name";
257                         pp->length = sz;
258                         pp->value = pp + 1;
259                         *prev_pp = pp;
260                         prev_pp = &pp->next;
261                         memcpy(pp->value, ps, sz - 1);
262                         ((char *)pp->value)[sz - 1] = 0;
263                         pr_debug("fixed up name for %s -> %s\n", pathp,
264                                 (char *)pp->value);
265                 }
266         }
267         if (allnextpp) {
268                 *prev_pp = NULL;
269                 np->name = of_get_property(np, "name", NULL);
270                 np->type = of_get_property(np, "device_type", NULL);
271
272                 if (!np->name)
273                         np->name = "<NULL>";
274                 if (!np->type)
275                         np->type = "<NULL>";
276         }
277
278         old_depth = depth;
279         *poffset = fdt_next_node(blob, *poffset, &depth);
280         if (depth < 0)
281                 depth = 0;
282         while (*poffset > 0 && depth > old_depth)
283                 mem = unflatten_dt_node(blob, mem, poffset, np, allnextpp,
284                                         fpsize);
285
286         if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
287                 pr_err("unflatten: error %d processing FDT\n", *poffset);
288
289         return mem;
290 }
291
292 /**
293  * __unflatten_device_tree - create tree of device_nodes from flat blob
294  *
295  * unflattens a device-tree, creating the
296  * tree of struct device_node. It also fills the "name" and "type"
297  * pointers of the nodes so the normal device-tree walking functions
298  * can be used.
299  * @blob: The blob to expand
300  * @mynodes: The device_node tree created by the call
301  * @dt_alloc: An allocator that provides a virtual address to memory
302  * for the resulting tree
303  */
304 static void __unflatten_device_tree(struct boot_param_header *blob,
305                              struct device_node **mynodes,
306                              void * (*dt_alloc)(u64 size, u64 align))
307 {
308         unsigned long size;
309         int start;
310         void *mem;
311         struct device_node **allnextp = mynodes;
312
313         pr_debug(" -> unflatten_device_tree()\n");
314
315         if (!blob) {
316                 pr_debug("No device tree pointer\n");
317                 return;
318         }
319
320         pr_debug("Unflattening device tree:\n");
321         pr_debug("magic: %08x\n", be32_to_cpu(blob->magic));
322         pr_debug("size: %08x\n", be32_to_cpu(blob->totalsize));
323         pr_debug("version: %08x\n", be32_to_cpu(blob->version));
324
325         if (be32_to_cpu(blob->magic) != OF_DT_HEADER) {
326                 pr_err("Invalid device tree blob header\n");
327                 return;
328         }
329
330         /* First pass, scan for size */
331         start = 0;
332         size = (unsigned long)unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
333         size = ALIGN(size, 4);
334
335         pr_debug("  size is %lx, allocating...\n", size);
336
337         /* Allocate memory for the expanded device tree */
338         mem = dt_alloc(size + 4, __alignof__(struct device_node));
339
340         memset((void *)mem, 0, size);
341
342         ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
343
344         pr_debug("  unflattening %p...\n", mem);
345
346         /* Second pass, do actual unflattening */
347         start = 0;
348         unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
349         if (be32_to_cpup(mem + size) != 0xdeadbeef)
350                 pr_warning("End of tree marker overwritten: %08x\n",
351                            be32_to_cpu(((__be32 *)mem)[size / 4]));
352         *allnextp = NULL;
353
354         pr_debug(" <- unflatten_device_tree()\n");
355 }
356
357 static void *kernel_tree_alloc(u64 size, u64 align)
358 {
359         return kzalloc(size, GFP_KERNEL);
360 }
361
362 /**
363  * of_fdt_unflatten_tree - create tree of device_nodes from flat blob
364  *
365  * unflattens the device-tree passed by the firmware, creating the
366  * tree of struct device_node. It also fills the "name" and "type"
367  * pointers of the nodes so the normal device-tree walking functions
368  * can be used.
369  */
370 void of_fdt_unflatten_tree(unsigned long *blob,
371                         struct device_node **mynodes)
372 {
373         struct boot_param_header *device_tree =
374                 (struct boot_param_header *)blob;
375         __unflatten_device_tree(device_tree, mynodes, &kernel_tree_alloc);
376 }
377 EXPORT_SYMBOL_GPL(of_fdt_unflatten_tree);
378
379 /* Everything below here references initial_boot_params directly. */
380 int __initdata dt_root_addr_cells;
381 int __initdata dt_root_size_cells;
382
383 struct boot_param_header *initial_boot_params;
384
385 #ifdef CONFIG_OF_EARLY_FLATTREE
386
387 /**
388  * res_mem_reserve_reg() - reserve all memory described in 'reg' property
389  */
390 static int __init __reserved_mem_reserve_reg(unsigned long node,
391                                              const char *uname)
392 {
393         int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
394         phys_addr_t base, size;
395         int len;
396         const __be32 *prop;
397         int nomap, first = 1;
398
399         prop = of_get_flat_dt_prop(node, "reg", &len);
400         if (!prop)
401                 return -ENOENT;
402
403         if (len && len % t_len != 0) {
404                 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
405                        uname);
406                 return -EINVAL;
407         }
408
409         nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
410
411         while (len >= t_len) {
412                 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
413                 size = dt_mem_next_cell(dt_root_size_cells, &prop);
414
415                 if (base && size &&
416                     early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
417                         pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
418                                 uname, &base, (unsigned long)size / SZ_1M);
419                 else
420                         pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
421                                 uname, &base, (unsigned long)size / SZ_1M);
422
423                 len -= t_len;
424                 if (first) {
425                         fdt_reserved_mem_save_node(node, uname, base, size);
426                         first = 0;
427                 }
428         }
429         return 0;
430 }
431
432 /**
433  * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
434  * in /reserved-memory matches the values supported by the current implementation,
435  * also check if ranges property has been provided
436  */
437 static int __init __reserved_mem_check_root(unsigned long node)
438 {
439         const __be32 *prop;
440
441         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
442         if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
443                 return -EINVAL;
444
445         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
446         if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
447                 return -EINVAL;
448
449         prop = of_get_flat_dt_prop(node, "ranges", NULL);
450         if (!prop)
451                 return -EINVAL;
452         return 0;
453 }
454
455 /**
456  * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
457  */
458 static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
459                                           int depth, void *data)
460 {
461         static int found;
462         const char *status;
463         int err;
464
465         if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
466                 if (__reserved_mem_check_root(node) != 0) {
467                         pr_err("Reserved memory: unsupported node format, ignoring\n");
468                         /* break scan */
469                         return 1;
470                 }
471                 found = 1;
472                 /* scan next node */
473                 return 0;
474         } else if (!found) {
475                 /* scan next node */
476                 return 0;
477         } else if (found && depth < 2) {
478                 /* scanning of /reserved-memory has been finished */
479                 return 1;
480         }
481
482         status = of_get_flat_dt_prop(node, "status", NULL);
483         if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
484                 return 0;
485
486         err = __reserved_mem_reserve_reg(node, uname);
487         if (err == -ENOENT && of_get_flat_dt_prop(node, "size", NULL))
488                 fdt_reserved_mem_save_node(node, uname, 0, 0);
489
490         /* scan next node */
491         return 0;
492 }
493
494 /**
495  * early_init_fdt_scan_reserved_mem() - create reserved memory regions
496  *
497  * This function grabs memory from early allocator for device exclusive use
498  * defined in device tree structures. It should be called by arch specific code
499  * once the early allocator (i.e. memblock) has been fully activated.
500  */
501 void __init early_init_fdt_scan_reserved_mem(void)
502 {
503         if (!initial_boot_params)
504                 return;
505
506         of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
507         fdt_init_reserved_mem();
508 }
509
510 /**
511  * of_scan_flat_dt - scan flattened tree blob and call callback on each.
512  * @it: callback function
513  * @data: context data pointer
514  *
515  * This function is used to scan the flattened device-tree, it is
516  * used to extract the memory information at boot before we can
517  * unflatten the tree
518  */
519 int __init of_scan_flat_dt(int (*it)(unsigned long node,
520                                      const char *uname, int depth,
521                                      void *data),
522                            void *data)
523 {
524         const void *blob = initial_boot_params;
525         const char *pathp;
526         int offset, rc = 0, depth = -1;
527
528         for (offset = fdt_next_node(blob, -1, &depth);
529              offset >= 0 && depth >= 0 && !rc;
530              offset = fdt_next_node(blob, offset, &depth)) {
531
532                 pathp = fdt_get_name(blob, offset, NULL);
533                 if (*pathp == '/')
534                         pathp = kbasename(pathp);
535                 rc = it(offset, pathp, depth, data);
536         }
537         return rc;
538 }
539
540 /**
541  * of_get_flat_dt_root - find the root node in the flat blob
542  */
543 unsigned long __init of_get_flat_dt_root(void)
544 {
545         return 0;
546 }
547
548 /**
549  * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
550  *
551  * This function can be used within scan_flattened_dt callback to get
552  * access to properties
553  */
554 const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
555                                        int *size)
556 {
557         return fdt_getprop(initial_boot_params, node, name, size);
558 }
559
560 /**
561  * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
562  * @node: node to test
563  * @compat: compatible string to compare with compatible list.
564  */
565 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
566 {
567         return of_fdt_is_compatible(initial_boot_params, node, compat);
568 }
569
570 /**
571  * of_flat_dt_match - Return true if node matches a list of compatible values
572  */
573 int __init of_flat_dt_match(unsigned long node, const char *const *compat)
574 {
575         return of_fdt_match(initial_boot_params, node, compat);
576 }
577
578 struct fdt_scan_status {
579         const char *name;
580         int namelen;
581         int depth;
582         int found;
583         int (*iterator)(unsigned long node, const char *uname, int depth, void *data);
584         void *data;
585 };
586
587 #ifdef CONFIG_BLK_DEV_INITRD
588 /**
589  * early_init_dt_check_for_initrd - Decode initrd location from flat tree
590  * @node: reference to node containing initrd location ('chosen')
591  */
592 void __init early_init_dt_check_for_initrd(unsigned long node)
593 {
594         u64 start, end;
595         int len;
596         const __be32 *prop;
597
598         pr_debug("Looking for initrd properties... ");
599
600         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
601         if (!prop)
602                 return;
603         start = of_read_number(prop, len/4);
604
605         prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
606         if (!prop)
607                 return;
608         end = of_read_number(prop, len/4);
609
610         early_init_dt_setup_initrd_arch(start, end);
611         pr_debug("initrd_start=0x%llx  initrd_end=0x%llx\n",
612                  (unsigned long long)start, (unsigned long long)end);
613 }
614 #else
615 inline void early_init_dt_check_for_initrd(unsigned long node)
616 {
617 }
618 #endif /* CONFIG_BLK_DEV_INITRD */
619
620 /**
621  * early_init_dt_scan_root - fetch the top level address and size cells
622  */
623 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
624                                    int depth, void *data)
625 {
626         const __be32 *prop;
627
628         if (depth != 0)
629                 return 0;
630
631         dt_root_size_cells = OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
632         dt_root_addr_cells = OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
633
634         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
635         if (prop)
636                 dt_root_size_cells = be32_to_cpup(prop);
637         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
638
639         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
640         if (prop)
641                 dt_root_addr_cells = be32_to_cpup(prop);
642         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
643
644         /* break now */
645         return 1;
646 }
647
648 u64 __init dt_mem_next_cell(int s, const __be32 **cellp)
649 {
650         const __be32 *p = *cellp;
651
652         *cellp = p + s;
653         return of_read_number(p, s);
654 }
655
656 /**
657  * early_init_dt_scan_memory - Look for an parse memory nodes
658  */
659 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
660                                      int depth, void *data)
661 {
662         const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
663         const __be32 *reg, *endp;
664         int l;
665
666         /* We are scanning "memory" nodes only */
667         if (type == NULL) {
668                 /*
669                  * The longtrail doesn't have a device_type on the
670                  * /memory node, so look for the node called /memory@0.
671                  */
672                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
673                         return 0;
674         } else if (strcmp(type, "memory") != 0)
675                 return 0;
676
677         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
678         if (reg == NULL)
679                 reg = of_get_flat_dt_prop(node, "reg", &l);
680         if (reg == NULL)
681                 return 0;
682
683         endp = reg + (l / sizeof(__be32));
684
685         pr_debug("memory scan node %s, reg size %d, data: %x %x %x %x,\n",
686             uname, l, reg[0], reg[1], reg[2], reg[3]);
687
688         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
689                 u64 base, size;
690
691                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
692                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
693
694                 if (size == 0)
695                         continue;
696                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
697                     (unsigned long long)size);
698
699                 early_init_dt_add_memory_arch(base, size);
700         }
701
702         return 0;
703 }
704
705 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
706                                      int depth, void *data)
707 {
708         int l;
709         const char *p;
710
711         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
712
713         if (depth != 1 || !data ||
714             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
715                 return 0;
716
717         early_init_dt_check_for_initrd(node);
718
719         /* Retrieve command line */
720         p = of_get_flat_dt_prop(node, "bootargs", &l);
721         if (p != NULL && l > 0)
722                 strlcpy(data, p, min((int)l, COMMAND_LINE_SIZE));
723
724         /*
725          * CONFIG_CMDLINE is meant to be a default in case nothing else
726          * managed to set the command line, unless CONFIG_CMDLINE_FORCE
727          * is set in which case we override whatever was found earlier.
728          */
729 #ifdef CONFIG_CMDLINE
730 #ifndef CONFIG_CMDLINE_FORCE
731         if (!((char *)data)[0])
732 #endif
733                 strlcpy(data, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
734 #endif /* CONFIG_CMDLINE */
735
736         pr_debug("Command line is: %s\n", (char*)data);
737
738         /* break now */
739         return 1;
740 }
741
742 #ifdef CONFIG_HAVE_MEMBLOCK
743 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
744 {
745         const u64 phys_offset = __pa(PAGE_OFFSET);
746         base &= PAGE_MASK;
747         size &= PAGE_MASK;
748         if (base + size < phys_offset) {
749                 pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
750                            base, base + size);
751                 return;
752         }
753         if (base < phys_offset) {
754                 pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
755                            base, phys_offset);
756                 size -= phys_offset - base;
757                 base = phys_offset;
758         }
759         memblock_add(base, size);
760 }
761
762 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
763                                         phys_addr_t size, bool nomap)
764 {
765         if (memblock_is_region_reserved(base, size))
766                 return -EBUSY;
767         if (nomap)
768                 return memblock_remove(base, size);
769         return memblock_reserve(base, size);
770 }
771
772 /*
773  * called from unflatten_device_tree() to bootstrap devicetree itself
774  * Architectures can override this definition if memblock isn't used
775  */
776 void * __init __weak early_init_dt_alloc_memory_arch(u64 size, u64 align)
777 {
778         return __va(memblock_alloc(size, align));
779 }
780 #else
781 int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
782                                         phys_addr_t size, bool nomap)
783 {
784         pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
785                   base, size, nomap ? " (nomap)" : "");
786         return -ENOSYS;
787 }
788 #endif
789
790 bool __init early_init_dt_scan(void *params)
791 {
792         if (!params)
793                 return false;
794
795         /* Setup flat device-tree pointer */
796         initial_boot_params = params;
797
798         /* check device tree validity */
799         if (be32_to_cpu(initial_boot_params->magic) != OF_DT_HEADER) {
800                 initial_boot_params = NULL;
801                 return false;
802         }
803
804         /* Retrieve various information from the /chosen node */
805         of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
806
807         /* Initialize {size,address}-cells info */
808         of_scan_flat_dt(early_init_dt_scan_root, NULL);
809
810         /* Setup memory, calling early_init_dt_add_memory_arch */
811         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
812
813         return true;
814 }
815
816 /**
817  * unflatten_device_tree - create tree of device_nodes from flat blob
818  *
819  * unflattens the device-tree passed by the firmware, creating the
820  * tree of struct device_node. It also fills the "name" and "type"
821  * pointers of the nodes so the normal device-tree walking functions
822  * can be used.
823  */
824 void __init unflatten_device_tree(void)
825 {
826         __unflatten_device_tree(initial_boot_params, &of_allnodes,
827                                 early_init_dt_alloc_memory_arch);
828
829         /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
830         of_alias_scan(early_init_dt_alloc_memory_arch);
831 }
832
833 #endif /* CONFIG_OF_EARLY_FLATTREE */