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