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