of/flattree: merge of_scan_flat_dt
[firefly-linux-kernel-4.4.55.git] / arch / microblaze / kernel / prom.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <stdarg.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/init.h>
20 #include <linux/threads.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/stringify.h>
25 #include <linux/delay.h>
26 #include <linux/initrd.h>
27 #include <linux/bitops.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/debugfs.h>
31 #include <linux/irq.h>
32 #include <linux/lmb.h>
33
34 #include <asm/prom.h>
35 #include <asm/page.h>
36 #include <asm/processor.h>
37 #include <asm/irq.h>
38 #include <linux/io.h>
39 #include <asm/system.h>
40 #include <asm/mmu.h>
41 #include <asm/pgtable.h>
42 #include <asm/sections.h>
43 #include <asm/pci-bridge.h>
44
45 static int __initdata dt_root_addr_cells;
46 static int __initdata dt_root_size_cells;
47
48 typedef u32 cell_t;
49
50 /* export that to outside world */
51 struct device_node *of_chosen;
52
53 unsigned long __init of_get_flat_dt_root(void)
54 {
55         unsigned long p = ((unsigned long)initial_boot_params) +
56                 initial_boot_params->off_dt_struct;
57
58         while (*((u32 *)p) == OF_DT_NOP)
59                 p += 4;
60         BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
61         p += 4;
62         return _ALIGN(p + strlen((char *)p) + 1, 4);
63 }
64
65 /**
66  * This function can be used within scan_flattened_dt callback to get
67  * access to properties
68  */
69 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
70                                 unsigned long *size)
71 {
72         unsigned long p = node;
73
74         do {
75                 u32 tag = *((u32 *)p);
76                 u32 sz, noff;
77                 const char *nstr;
78
79                 p += 4;
80                 if (tag == OF_DT_NOP)
81                         continue;
82                 if (tag != OF_DT_PROP)
83                         return NULL;
84
85                 sz = *((u32 *)p);
86                 noff = *((u32 *)(p + 4));
87                 p += 8;
88                 if (initial_boot_params->version < 0x10)
89                         p = _ALIGN(p, sz >= 8 ? 8 : 4);
90
91                 nstr = find_flat_dt_string(noff);
92                 if (nstr == NULL) {
93                         printk(KERN_WARNING "Can't find property index"
94                                 " name !\n");
95                         return NULL;
96                 }
97                 if (strcmp(name, nstr) == 0) {
98                         if (size)
99                                 *size = sz;
100                         return (void *)p;
101                 }
102                 p += sz;
103                 p = _ALIGN(p, 4);
104         } while (1);
105 }
106
107 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
108 {
109         const char *cp;
110         unsigned long cplen, l;
111
112         cp = of_get_flat_dt_prop(node, "compatible", &cplen);
113         if (cp == NULL)
114                 return 0;
115         while (cplen > 0) {
116                 if (strncasecmp(cp, compat, strlen(compat)) == 0)
117                         return 1;
118                 l = strlen(cp) + 1;
119                 cp += l;
120                 cplen -= l;
121         }
122
123         return 0;
124 }
125
126 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
127                                         unsigned long align)
128 {
129         void *res;
130
131         *mem = _ALIGN(*mem, align);
132         res = (void *)*mem;
133         *mem += size;
134
135         return res;
136 }
137
138 static unsigned long __init unflatten_dt_node(unsigned long mem,
139                                         unsigned long *p,
140                                         struct device_node *dad,
141                                         struct device_node ***allnextpp,
142                                         unsigned long fpsize)
143 {
144         struct device_node *np;
145         struct property *pp, **prev_pp = NULL;
146         char *pathp;
147         u32 tag;
148         unsigned int l, allocl;
149         int has_name = 0;
150         int new_format = 0;
151
152         tag = *((u32 *)(*p));
153         if (tag != OF_DT_BEGIN_NODE) {
154                 printk("Weird tag at start of node: %x\n", tag);
155                 return mem;
156         }
157         *p += 4;
158         pathp = (char *)*p;
159         l = allocl = strlen(pathp) + 1;
160         *p = _ALIGN(*p + l, 4);
161
162         /* version 0x10 has a more compact unit name here instead of the full
163          * path. we accumulate the full path size using "fpsize", we'll rebuild
164          * it later. We detect this because the first character of the name is
165          * not '/'.
166          */
167         if ((*pathp) != '/') {
168                 new_format = 1;
169                 if (fpsize == 0) {
170                         /* root node: special case. fpsize accounts for path
171                          * plus terminating zero. root node only has '/', so
172                          * fpsize should be 2, but we want to avoid the first
173                          * level nodes to have two '/' so we use fpsize 1 here
174                          */
175                         fpsize = 1;
176                         allocl = 2;
177                 } else {
178                         /* account for '/' and path size minus terminal 0
179                          * already in 'l'
180                          */
181                         fpsize += l;
182                         allocl = fpsize;
183                 }
184         }
185
186         np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
187                                 __alignof__(struct device_node));
188         if (allnextpp) {
189                 memset(np, 0, sizeof(*np));
190                 np->full_name = ((char *)np) + sizeof(struct device_node);
191                 if (new_format) {
192                         char *p2 = np->full_name;
193                         /* rebuild full path for new format */
194                         if (dad && dad->parent) {
195                                 strcpy(p2, dad->full_name);
196 #ifdef DEBUG
197                                 if ((strlen(p2) + l + 1) != allocl) {
198                                         pr_debug("%s: p: %d, l: %d, a: %d\n",
199                                                 pathp, (int)strlen(p2),
200                                                 l, allocl);
201                                 }
202 #endif
203                                 p2 += strlen(p2);
204                         }
205                         *(p2++) = '/';
206                         memcpy(p2, pathp, l);
207                 } else
208                         memcpy(np->full_name, pathp, l);
209                 prev_pp = &np->properties;
210                 **allnextpp = np;
211                 *allnextpp = &np->allnext;
212                 if (dad != NULL) {
213                         np->parent = dad;
214                         /* we temporarily use the next field as `last_child'*/
215                         if (dad->next == NULL)
216                                 dad->child = np;
217                         else
218                                 dad->next->sibling = np;
219                         dad->next = np;
220                 }
221                 kref_init(&np->kref);
222         }
223         while (1) {
224                 u32 sz, noff;
225                 char *pname;
226
227                 tag = *((u32 *)(*p));
228                 if (tag == OF_DT_NOP) {
229                         *p += 4;
230                         continue;
231                 }
232                 if (tag != OF_DT_PROP)
233                         break;
234                 *p += 4;
235                 sz = *((u32 *)(*p));
236                 noff = *((u32 *)((*p) + 4));
237                 *p += 8;
238                 if (initial_boot_params->version < 0x10)
239                         *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
240
241                 pname = find_flat_dt_string(noff);
242                 if (pname == NULL) {
243                         printk(KERN_INFO
244                                 "Can't find property name in list !\n");
245                         break;
246                 }
247                 if (strcmp(pname, "name") == 0)
248                         has_name = 1;
249                 l = strlen(pname) + 1;
250                 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
251                                         __alignof__(struct property));
252                 if (allnextpp) {
253                         if (strcmp(pname, "linux,phandle") == 0) {
254                                 np->node = *((u32 *)*p);
255                                 if (np->linux_phandle == 0)
256                                         np->linux_phandle = np->node;
257                         }
258                         if (strcmp(pname, "ibm,phandle") == 0)
259                                 np->linux_phandle = *((u32 *)*p);
260                         pp->name = pname;
261                         pp->length = sz;
262                         pp->value = (void *)*p;
263                         *prev_pp = pp;
264                         prev_pp = &pp->next;
265                 }
266                 *p = _ALIGN((*p) + sz, 4);
267         }
268         /* with version 0x10 we may not have the name property, recreate
269          * it here from the unit name if absent
270          */
271         if (!has_name) {
272                 char *p1 = pathp, *ps = pathp, *pa = NULL;
273                 int sz;
274
275                 while (*p1) {
276                         if ((*p1) == '@')
277                                 pa = p1;
278                         if ((*p1) == '/')
279                                 ps = p1 + 1;
280                         p1++;
281                 }
282                 if (pa < ps)
283                         pa = p1;
284                 sz = (pa - ps) + 1;
285                 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
286                                         __alignof__(struct property));
287                 if (allnextpp) {
288                         pp->name = "name";
289                         pp->length = sz;
290                         pp->value = pp + 1;
291                         *prev_pp = pp;
292                         prev_pp = &pp->next;
293                         memcpy(pp->value, ps, sz - 1);
294                         ((char *)pp->value)[sz - 1] = 0;
295                         pr_debug("fixed up name for %s -> %s\n", pathp,
296                                 (char *)pp->value);
297                 }
298         }
299         if (allnextpp) {
300                 *prev_pp = NULL;
301                 np->name = of_get_property(np, "name", NULL);
302                 np->type = of_get_property(np, "device_type", NULL);
303
304                 if (!np->name)
305                         np->name = "<NULL>";
306                 if (!np->type)
307                         np->type = "<NULL>";
308         }
309         while (tag == OF_DT_BEGIN_NODE) {
310                 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
311                 tag = *((u32 *)(*p));
312         }
313         if (tag != OF_DT_END_NODE) {
314                 printk(KERN_INFO "Weird tag at end of node: %x\n", tag);
315                 return mem;
316         }
317         *p += 4;
318         return mem;
319 }
320
321 /**
322  * unflattens the device-tree passed by the firmware, creating the
323  * tree of struct device_node. It also fills the "name" and "type"
324  * pointers of the nodes so the normal device-tree walking functions
325  * can be used (this used to be done by finish_device_tree)
326  */
327 void __init unflatten_device_tree(void)
328 {
329         unsigned long start, mem, size;
330         struct device_node **allnextp = &allnodes;
331
332         pr_debug(" -> unflatten_device_tree()\n");
333
334         /* First pass, scan for size */
335         start = ((unsigned long)initial_boot_params) +
336                 initial_boot_params->off_dt_struct;
337         size = unflatten_dt_node(0, &start, NULL, NULL, 0);
338         size = (size | 3) + 1;
339
340         pr_debug("  size is %lx, allocating...\n", size);
341
342         /* Allocate memory for the expanded device tree */
343         mem = lmb_alloc(size + 4, __alignof__(struct device_node));
344         mem = (unsigned long) __va(mem);
345
346         ((u32 *)mem)[size / 4] = 0xdeadbeef;
347
348         pr_debug("  unflattening %lx...\n", mem);
349
350         /* Second pass, do actual unflattening */
351         start = ((unsigned long)initial_boot_params) +
352                 initial_boot_params->off_dt_struct;
353         unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
354         if (*((u32 *)start) != OF_DT_END)
355                 printk(KERN_WARNING "Weird tag at end of tree: %08x\n",
356                         *((u32 *)start));
357         if (((u32 *)mem)[size / 4] != 0xdeadbeef)
358                 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
359                         ((u32 *)mem)[size / 4]);
360         *allnextp = NULL;
361
362         /* Get pointer to OF "/chosen" node for use everywhere */
363         of_chosen = of_find_node_by_path("/chosen");
364         if (of_chosen == NULL)
365                 of_chosen = of_find_node_by_path("/chosen@0");
366
367         pr_debug(" <- unflatten_device_tree()\n");
368 }
369
370 #define early_init_dt_scan_drconf_memory(node) 0
371
372 static int __init early_init_dt_scan_cpus(unsigned long node,
373                                           const char *uname, int depth,
374                                           void *data)
375 {
376         static int logical_cpuid;
377         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
378         const u32 *intserv;
379         int i, nthreads;
380         int found = 0;
381
382         /* We are scanning "cpu" nodes only */
383         if (type == NULL || strcmp(type, "cpu") != 0)
384                 return 0;
385
386         /* Get physical cpuid */
387         intserv = of_get_flat_dt_prop(node, "reg", NULL);
388         nthreads = 1;
389
390         /*
391          * Now see if any of these threads match our boot cpu.
392          * NOTE: This must match the parsing done in smp_setup_cpu_maps.
393          */
394         for (i = 0; i < nthreads; i++) {
395                 /*
396                  * version 2 of the kexec param format adds the phys cpuid of
397                  * booted proc.
398                  */
399                 if (initial_boot_params && initial_boot_params->version >= 2) {
400                         if (intserv[i] ==
401                                         initial_boot_params->boot_cpuid_phys) {
402                                 found = 1;
403                                 break;
404                         }
405                 } else {
406                         /*
407                          * Check if it's the boot-cpu, set it's hw index now,
408                          * unfortunately this format did not support booting
409                          * off secondary threads.
410                          */
411                         if (of_get_flat_dt_prop(node,
412                                         "linux,boot-cpu", NULL) != NULL) {
413                                 found = 1;
414                                 break;
415                         }
416                 }
417
418 #ifdef CONFIG_SMP
419                 /* logical cpu id is always 0 on UP kernels */
420                 logical_cpuid++;
421 #endif
422         }
423
424         if (found) {
425                 pr_debug("boot cpu: logical %d physical %d\n", logical_cpuid,
426                         intserv[i]);
427                 boot_cpuid = logical_cpuid;
428         }
429
430         return 0;
431 }
432
433 #ifdef CONFIG_BLK_DEV_INITRD
434 static void __init early_init_dt_check_for_initrd(unsigned long node)
435 {
436         unsigned long l;
437         u32 *prop;
438
439         pr_debug("Looking for initrd properties... ");
440
441         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
442         if (prop) {
443                 initrd_start = (unsigned long)
444                                         __va((u32)of_read_ulong(prop, l/4));
445
446                 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
447                 if (prop) {
448                         initrd_end = (unsigned long)
449                                         __va((u32)of_read_ulong(prop, 1/4));
450                         initrd_below_start_ok = 1;
451                 } else {
452                         initrd_start = 0;
453                 }
454         }
455
456         pr_debug("initrd_start=0x%lx  initrd_end=0x%lx\n",
457                                         initrd_start, initrd_end);
458 }
459 #else
460 static inline void early_init_dt_check_for_initrd(unsigned long node)
461 {
462 }
463 #endif /* CONFIG_BLK_DEV_INITRD */
464
465 static int __init early_init_dt_scan_chosen(unsigned long node,
466                                 const char *uname, int depth, void *data)
467 {
468         unsigned long l;
469         char *p;
470
471         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
472
473         if (depth != 1 ||
474                 (strcmp(uname, "chosen") != 0 &&
475                                 strcmp(uname, "chosen@0") != 0))
476                 return 0;
477
478 #ifdef CONFIG_KEXEC
479         lprop = (u64 *)of_get_flat_dt_prop(node,
480                                 "linux,crashkernel-base", NULL);
481         if (lprop)
482                 crashk_res.start = *lprop;
483
484         lprop = (u64 *)of_get_flat_dt_prop(node,
485                                 "linux,crashkernel-size", NULL);
486         if (lprop)
487                 crashk_res.end = crashk_res.start + *lprop - 1;
488 #endif
489
490         early_init_dt_check_for_initrd(node);
491
492         /* Retreive command line */
493         p = of_get_flat_dt_prop(node, "bootargs", &l);
494         if (p != NULL && l > 0)
495                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
496
497 #ifdef CONFIG_CMDLINE
498 #ifndef CONFIG_CMDLINE_FORCE
499         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
500 #endif
501                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
502 #endif /* CONFIG_CMDLINE */
503
504         pr_debug("Command line is: %s\n", cmd_line);
505
506         /* break now */
507         return 1;
508 }
509
510 static int __init early_init_dt_scan_root(unsigned long node,
511                                 const char *uname, int depth, void *data)
512 {
513         u32 *prop;
514
515         if (depth != 0)
516                 return 0;
517
518         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
519         dt_root_size_cells = (prop == NULL) ? 1 : *prop;
520         pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
521
522         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
523         dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
524         pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
525
526         /* break now */
527         return 1;
528 }
529
530 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
531 {
532         cell_t *p = *cellp;
533
534         *cellp = p + s;
535         return of_read_number(p, s);
536 }
537
538 static int __init early_init_dt_scan_memory(unsigned long node,
539                                 const char *uname, int depth, void *data)
540 {
541         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
542         cell_t *reg, *endp;
543         unsigned long l;
544
545         /* Look for the ibm,dynamic-reconfiguration-memory node */
546 /*      if (depth == 1 &&
547                 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
548                 return early_init_dt_scan_drconf_memory(node);
549 */
550         /* We are scanning "memory" nodes only */
551         if (type == NULL) {
552                 /*
553                  * The longtrail doesn't have a device_type on the
554                  * /memory node, so look for the node called /memory@0.
555                  */
556                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
557                         return 0;
558         } else if (strcmp(type, "memory") != 0)
559                 return 0;
560
561         reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
562         if (reg == NULL)
563                 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
564         if (reg == NULL)
565                 return 0;
566
567         endp = reg + (l / sizeof(cell_t));
568
569         pr_debug("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
570                 uname, l, reg[0], reg[1], reg[2], reg[3]);
571
572         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
573                 u64 base, size;
574
575                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
576                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
577
578                 if (size == 0)
579                         continue;
580                 pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
581                         (unsigned long long)size);
582
583                 lmb_add(base, size);
584         }
585         return 0;
586 }
587
588 #ifdef CONFIG_PHYP_DUMP
589 /**
590  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
591  *
592  * Function to find the largest size we need to reserve
593  * during early boot process.
594  *
595  * It either looks for boot param and returns that OR
596  * returns larger of 256 or 5% rounded down to multiples of 256MB.
597  *
598  */
599 static inline unsigned long phyp_dump_calculate_reserve_size(void)
600 {
601         unsigned long tmp;
602
603         if (phyp_dump_info->reserve_bootvar)
604                 return phyp_dump_info->reserve_bootvar;
605
606         /* divide by 20 to get 5% of value */
607         tmp = lmb_end_of_DRAM();
608         do_div(tmp, 20);
609
610         /* round it down in multiples of 256 */
611         tmp = tmp & ~0x0FFFFFFFUL;
612
613         return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
614 }
615
616 /**
617  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
618  *
619  * This routine may reserve memory regions in the kernel only
620  * if the system is supported and a dump was taken in last
621  * boot instance or if the hardware is supported and the
622  * scratch area needs to be setup. In other instances it returns
623  * without reserving anything. The memory in case of dump being
624  * active is freed when the dump is collected (by userland tools).
625  */
626 static void __init phyp_dump_reserve_mem(void)
627 {
628         unsigned long base, size;
629         unsigned long variable_reserve_size;
630
631         if (!phyp_dump_info->phyp_dump_configured) {
632                 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
633                 return;
634         }
635
636         if (!phyp_dump_info->phyp_dump_at_boot) {
637                 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
638                 return;
639         }
640
641         variable_reserve_size = phyp_dump_calculate_reserve_size();
642
643         if (phyp_dump_info->phyp_dump_is_active) {
644                 /* Reserve *everything* above RMR.Area freed by userland tools*/
645                 base = variable_reserve_size;
646                 size = lmb_end_of_DRAM() - base;
647
648                 /* XXX crashed_ram_end is wrong, since it may be beyond
649                  * the memory_limit, it will need to be adjusted. */
650                 lmb_reserve(base, size);
651
652                 phyp_dump_info->init_reserve_start = base;
653                 phyp_dump_info->init_reserve_size = size;
654         } else {
655                 size = phyp_dump_info->cpu_state_size +
656                         phyp_dump_info->hpte_region_size +
657                         variable_reserve_size;
658                 base = lmb_end_of_DRAM() - size;
659                 lmb_reserve(base, size);
660                 phyp_dump_info->init_reserve_start = base;
661                 phyp_dump_info->init_reserve_size = size;
662         }
663 }
664 #else
665 static inline void __init phyp_dump_reserve_mem(void) {}
666 #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
667
668 #ifdef CONFIG_EARLY_PRINTK
669 /* MS this is Microblaze specifig function */
670 static int __init early_init_dt_scan_serial(unsigned long node,
671                                 const char *uname, int depth, void *data)
672 {
673         unsigned long l;
674         char *p;
675         int *addr;
676
677         pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
678
679 /* find all serial nodes */
680         if (strncmp(uname, "serial", 6) != 0)
681                 return 0;
682
683         early_init_dt_check_for_initrd(node);
684
685 /* find compatible node with uartlite */
686         p = of_get_flat_dt_prop(node, "compatible", &l);
687         if ((strncmp(p, "xlnx,xps-uartlite", 17) != 0) &&
688                         (strncmp(p, "xlnx,opb-uartlite", 17) != 0))
689                 return 0;
690
691         addr = of_get_flat_dt_prop(node, "reg", &l);
692         return *addr; /* return address */
693 }
694
695 /* this function is looking for early uartlite console - Microblaze specific */
696 int __init early_uartlite_console(void)
697 {
698         return of_scan_flat_dt(early_init_dt_scan_serial, NULL);
699 }
700 #endif
701
702 void __init early_init_devtree(void *params)
703 {
704         pr_debug(" -> early_init_devtree(%p)\n", params);
705
706         /* Setup flat device-tree pointer */
707         initial_boot_params = params;
708
709 #ifdef CONFIG_PHYP_DUMP
710         /* scan tree to see if dump occured during last boot */
711         of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
712 #endif
713
714         /* Retrieve various informations from the /chosen node of the
715          * device-tree, including the platform type, initrd location and
716          * size, TCE reserve, and more ...
717          */
718         of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
719
720         /* Scan memory nodes and rebuild LMBs */
721         lmb_init();
722         of_scan_flat_dt(early_init_dt_scan_root, NULL);
723         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
724
725         /* Save command line for /proc/cmdline and then parse parameters */
726         strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
727         parse_early_param();
728
729         lmb_analyze();
730
731         pr_debug("Phys. mem: %lx\n", (unsigned long) lmb_phys_mem_size());
732
733         pr_debug("Scanning CPUs ...\n");
734
735         /* Retreive CPU related informations from the flat tree
736          * (altivec support, boot CPU ID, ...)
737          */
738         of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
739
740         pr_debug(" <- early_init_devtree()\n");
741 }
742
743 /**
744  * Indicates whether the root node has a given value in its
745  * compatible property.
746  */
747 int machine_is_compatible(const char *compat)
748 {
749         struct device_node *root;
750         int rc = 0;
751
752         root = of_find_node_by_path("/");
753         if (root) {
754                 rc = of_device_is_compatible(root, compat);
755                 of_node_put(root);
756         }
757         return rc;
758 }
759 EXPORT_SYMBOL(machine_is_compatible);
760
761 /*******
762  *
763  * New implementation of the OF "find" APIs, return a refcounted
764  * object, call of_node_put() when done.  The device tree and list
765  * are protected by a rw_lock.
766  *
767  * Note that property management will need some locking as well,
768  * this isn't dealt with yet.
769  *
770  *******/
771
772 /**
773  *      of_find_node_by_phandle - Find a node given a phandle
774  *      @handle:        phandle of the node to find
775  *
776  *      Returns a node pointer with refcount incremented, use
777  *      of_node_put() on it when done.
778  */
779 struct device_node *of_find_node_by_phandle(phandle handle)
780 {
781         struct device_node *np;
782
783         read_lock(&devtree_lock);
784         for (np = allnodes; np != NULL; np = np->allnext)
785                 if (np->linux_phandle == handle)
786                         break;
787         of_node_get(np);
788         read_unlock(&devtree_lock);
789         return np;
790 }
791 EXPORT_SYMBOL(of_find_node_by_phandle);
792
793 /**
794  *      of_node_get - Increment refcount of a node
795  *      @node:  Node to inc refcount, NULL is supported to
796  *              simplify writing of callers
797  *
798  *      Returns node.
799  */
800 struct device_node *of_node_get(struct device_node *node)
801 {
802         if (node)
803                 kref_get(&node->kref);
804         return node;
805 }
806 EXPORT_SYMBOL(of_node_get);
807
808 static inline struct device_node *kref_to_device_node(struct kref *kref)
809 {
810         return container_of(kref, struct device_node, kref);
811 }
812
813 /**
814  *      of_node_release - release a dynamically allocated node
815  *      @kref:  kref element of the node to be released
816  *
817  *      In of_node_put() this function is passed to kref_put()
818  *      as the destructor.
819  */
820 static void of_node_release(struct kref *kref)
821 {
822         struct device_node *node = kref_to_device_node(kref);
823         struct property *prop = node->properties;
824
825         /* We should never be releasing nodes that haven't been detached. */
826         if (!of_node_check_flag(node, OF_DETACHED)) {
827                 printk(KERN_INFO "WARNING: Bad of_node_put() on %s\n",
828                         node->full_name);
829                 dump_stack();
830                 kref_init(&node->kref);
831                 return;
832         }
833
834         if (!of_node_check_flag(node, OF_DYNAMIC))
835                 return;
836
837         while (prop) {
838                 struct property *next = prop->next;
839                 kfree(prop->name);
840                 kfree(prop->value);
841                 kfree(prop);
842                 prop = next;
843
844                 if (!prop) {
845                         prop = node->deadprops;
846                         node->deadprops = NULL;
847                 }
848         }
849         kfree(node->full_name);
850         kfree(node->data);
851         kfree(node);
852 }
853
854 /**
855  *      of_node_put - Decrement refcount of a node
856  *      @node:  Node to dec refcount, NULL is supported to
857  *              simplify writing of callers
858  *
859  */
860 void of_node_put(struct device_node *node)
861 {
862         if (node)
863                 kref_put(&node->kref, of_node_release);
864 }
865 EXPORT_SYMBOL(of_node_put);
866
867 /*
868  * Plug a device node into the tree and global list.
869  */
870 void of_attach_node(struct device_node *np)
871 {
872         unsigned long flags;
873
874         write_lock_irqsave(&devtree_lock, flags);
875         np->sibling = np->parent->child;
876         np->allnext = allnodes;
877         np->parent->child = np;
878         allnodes = np;
879         write_unlock_irqrestore(&devtree_lock, flags);
880 }
881
882 /*
883  * "Unplug" a node from the device tree.  The caller must hold
884  * a reference to the node.  The memory associated with the node
885  * is not freed until its refcount goes to zero.
886  */
887 void of_detach_node(struct device_node *np)
888 {
889         struct device_node *parent;
890         unsigned long flags;
891
892         write_lock_irqsave(&devtree_lock, flags);
893
894         parent = np->parent;
895         if (!parent)
896                 goto out_unlock;
897
898         if (allnodes == np)
899                 allnodes = np->allnext;
900         else {
901                 struct device_node *prev;
902                 for (prev = allnodes;
903                      prev->allnext != np;
904                      prev = prev->allnext)
905                         ;
906                 prev->allnext = np->allnext;
907         }
908
909         if (parent->child == np)
910                 parent->child = np->sibling;
911         else {
912                 struct device_node *prevsib;
913                 for (prevsib = np->parent->child;
914                      prevsib->sibling != np;
915                      prevsib = prevsib->sibling)
916                         ;
917                 prevsib->sibling = np->sibling;
918         }
919
920         of_node_set_flag(np, OF_DETACHED);
921
922 out_unlock:
923         write_unlock_irqrestore(&devtree_lock, flags);
924 }
925
926 /*
927  * Add a property to a node
928  */
929 int prom_add_property(struct device_node *np, struct property *prop)
930 {
931         struct property **next;
932         unsigned long flags;
933
934         prop->next = NULL;
935         write_lock_irqsave(&devtree_lock, flags);
936         next = &np->properties;
937         while (*next) {
938                 if (strcmp(prop->name, (*next)->name) == 0) {
939                         /* duplicate ! don't insert it */
940                         write_unlock_irqrestore(&devtree_lock, flags);
941                         return -1;
942                 }
943                 next = &(*next)->next;
944         }
945         *next = prop;
946         write_unlock_irqrestore(&devtree_lock, flags);
947
948 #ifdef CONFIG_PROC_DEVICETREE
949         /* try to add to proc as well if it was initialized */
950         if (np->pde)
951                 proc_device_tree_add_prop(np->pde, prop);
952 #endif /* CONFIG_PROC_DEVICETREE */
953
954         return 0;
955 }
956
957 /*
958  * Remove a property from a node.  Note that we don't actually
959  * remove it, since we have given out who-knows-how-many pointers
960  * to the data using get-property.  Instead we just move the property
961  * to the "dead properties" list, so it won't be found any more.
962  */
963 int prom_remove_property(struct device_node *np, struct property *prop)
964 {
965         struct property **next;
966         unsigned long flags;
967         int found = 0;
968
969         write_lock_irqsave(&devtree_lock, flags);
970         next = &np->properties;
971         while (*next) {
972                 if (*next == prop) {
973                         /* found the node */
974                         *next = prop->next;
975                         prop->next = np->deadprops;
976                         np->deadprops = prop;
977                         found = 1;
978                         break;
979                 }
980                 next = &(*next)->next;
981         }
982         write_unlock_irqrestore(&devtree_lock, flags);
983
984         if (!found)
985                 return -ENODEV;
986
987 #ifdef CONFIG_PROC_DEVICETREE
988         /* try to remove the proc node as well */
989         if (np->pde)
990                 proc_device_tree_remove_prop(np->pde, prop);
991 #endif /* CONFIG_PROC_DEVICETREE */
992
993         return 0;
994 }
995
996 /*
997  * Update a property in a node.  Note that we don't actually
998  * remove it, since we have given out who-knows-how-many pointers
999  * to the data using get-property.  Instead we just move the property
1000  * to the "dead properties" list, and add the new property to the
1001  * property list
1002  */
1003 int prom_update_property(struct device_node *np,
1004                          struct property *newprop,
1005                          struct property *oldprop)
1006 {
1007         struct property **next;
1008         unsigned long flags;
1009         int found = 0;
1010
1011         write_lock_irqsave(&devtree_lock, flags);
1012         next = &np->properties;
1013         while (*next) {
1014                 if (*next == oldprop) {
1015                         /* found the node */
1016                         newprop->next = oldprop->next;
1017                         *next = newprop;
1018                         oldprop->next = np->deadprops;
1019                         np->deadprops = oldprop;
1020                         found = 1;
1021                         break;
1022                 }
1023                 next = &(*next)->next;
1024         }
1025         write_unlock_irqrestore(&devtree_lock, flags);
1026
1027         if (!found)
1028                 return -ENODEV;
1029
1030 #ifdef CONFIG_PROC_DEVICETREE
1031         /* try to add to proc as well if it was initialized */
1032         if (np->pde)
1033                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1034 #endif /* CONFIG_PROC_DEVICETREE */
1035
1036         return 0;
1037 }
1038
1039 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1040 static struct debugfs_blob_wrapper flat_dt_blob;
1041
1042 static int __init export_flat_device_tree(void)
1043 {
1044         struct dentry *d;
1045
1046         flat_dt_blob.data = initial_boot_params;
1047         flat_dt_blob.size = initial_boot_params->totalsize;
1048
1049         d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1050                                 of_debugfs_root, &flat_dt_blob);
1051         if (!d)
1052                 return 1;
1053
1054         return 0;
1055 }
1056 device_initcall(export_flat_device_tree);
1057 #endif