of/flattree: Merge unflatten_device_tree
[safe/jmp/linux-2.6] / arch / powerpc / 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 #undef DEBUG
17
18 #include <stdarg.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/threads.h>
23 #include <linux/spinlock.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/stringify.h>
27 #include <linux/delay.h>
28 #include <linux/initrd.h>
29 #include <linux/bitops.h>
30 #include <linux/module.h>
31 #include <linux/kexec.h>
32 #include <linux/debugfs.h>
33 #include <linux/irq.h>
34 #include <linux/lmb.h>
35
36 #include <asm/prom.h>
37 #include <asm/rtas.h>
38 #include <asm/page.h>
39 #include <asm/processor.h>
40 #include <asm/irq.h>
41 #include <asm/io.h>
42 #include <asm/kdump.h>
43 #include <asm/smp.h>
44 #include <asm/system.h>
45 #include <asm/mmu.h>
46 #include <asm/pgtable.h>
47 #include <asm/pci.h>
48 #include <asm/iommu.h>
49 #include <asm/btext.h>
50 #include <asm/sections.h>
51 #include <asm/machdep.h>
52 #include <asm/pSeries_reconfig.h>
53 #include <asm/pci-bridge.h>
54 #include <asm/phyp_dump.h>
55 #include <asm/kexec.h>
56 #include <mm/mmu_decl.h>
57
58 #ifdef DEBUG
59 #define DBG(fmt...) printk(KERN_ERR fmt)
60 #else
61 #define DBG(fmt...)
62 #endif
63
64
65 static int __initdata dt_root_addr_cells;
66 static int __initdata dt_root_size_cells;
67
68 #ifdef CONFIG_PPC64
69 int __initdata iommu_is_off;
70 int __initdata iommu_force_on;
71 unsigned long tce_alloc_start, tce_alloc_end;
72 #endif
73
74 typedef u32 cell_t;
75
76 extern rwlock_t devtree_lock;   /* temporary while merging */
77
78 /* export that to outside world */
79 struct device_node *of_chosen;
80
81 static int __init early_parse_mem(char *p)
82 {
83         if (!p)
84                 return 1;
85
86         memory_limit = PAGE_ALIGN(memparse(p, &p));
87         DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
88
89         return 0;
90 }
91 early_param("mem", early_parse_mem);
92
93 /**
94  * move_device_tree - move tree to an unused area, if needed.
95  *
96  * The device tree may be allocated beyond our memory limit, or inside the
97  * crash kernel region for kdump. If so, move it out of the way.
98  */
99 static void __init move_device_tree(void)
100 {
101         unsigned long start, size;
102         void *p;
103
104         DBG("-> move_device_tree\n");
105
106         start = __pa(initial_boot_params);
107         size = initial_boot_params->totalsize;
108
109         if ((memory_limit && (start + size) > memory_limit) ||
110                         overlaps_crashkernel(start, size)) {
111                 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
112                 memcpy(p, initial_boot_params, size);
113                 initial_boot_params = (struct boot_param_header *)p;
114                 DBG("Moved device tree to 0x%p\n", p);
115         }
116
117         DBG("<- move_device_tree\n");
118 }
119
120 /*
121  * ibm,pa-features is a per-cpu property that contains a string of
122  * attribute descriptors, each of which has a 2 byte header plus up
123  * to 254 bytes worth of processor attribute bits.  First header
124  * byte specifies the number of bytes following the header.
125  * Second header byte is an "attribute-specifier" type, of which
126  * zero is the only currently-defined value.
127  * Implementation:  Pass in the byte and bit offset for the feature
128  * that we are interested in.  The function will return -1 if the
129  * pa-features property is missing, or a 1/0 to indicate if the feature
130  * is supported/not supported.  Note that the bit numbers are
131  * big-endian to match the definition in PAPR.
132  */
133 static struct ibm_pa_feature {
134         unsigned long   cpu_features;   /* CPU_FTR_xxx bit */
135         unsigned int    cpu_user_ftrs;  /* PPC_FEATURE_xxx bit */
136         unsigned char   pabyte;         /* byte number in ibm,pa-features */
137         unsigned char   pabit;          /* bit number (big-endian) */
138         unsigned char   invert;         /* if 1, pa bit set => clear feature */
139 } ibm_pa_features[] __initdata = {
140         {0, PPC_FEATURE_HAS_MMU,        0, 0, 0},
141         {0, PPC_FEATURE_HAS_FPU,        0, 1, 0},
142         {CPU_FTR_SLB, 0,                0, 2, 0},
143         {CPU_FTR_CTRL, 0,               0, 3, 0},
144         {CPU_FTR_NOEXECUTE, 0,          0, 6, 0},
145         {CPU_FTR_NODSISRALIGN, 0,       1, 1, 1},
146         {CPU_FTR_CI_LARGE_PAGE, 0,      1, 2, 0},
147         {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
148 };
149
150 static void __init scan_features(unsigned long node, unsigned char *ftrs,
151                                  unsigned long tablelen,
152                                  struct ibm_pa_feature *fp,
153                                  unsigned long ft_size)
154 {
155         unsigned long i, len, bit;
156
157         /* find descriptor with type == 0 */
158         for (;;) {
159                 if (tablelen < 3)
160                         return;
161                 len = 2 + ftrs[0];
162                 if (tablelen < len)
163                         return;         /* descriptor 0 not found */
164                 if (ftrs[1] == 0)
165                         break;
166                 tablelen -= len;
167                 ftrs += len;
168         }
169
170         /* loop over bits we know about */
171         for (i = 0; i < ft_size; ++i, ++fp) {
172                 if (fp->pabyte >= ftrs[0])
173                         continue;
174                 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
175                 if (bit ^ fp->invert) {
176                         cur_cpu_spec->cpu_features |= fp->cpu_features;
177                         cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
178                 } else {
179                         cur_cpu_spec->cpu_features &= ~fp->cpu_features;
180                         cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
181                 }
182         }
183 }
184
185 static void __init check_cpu_pa_features(unsigned long node)
186 {
187         unsigned char *pa_ftrs;
188         unsigned long tablelen;
189
190         pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
191         if (pa_ftrs == NULL)
192                 return;
193
194         scan_features(node, pa_ftrs, tablelen,
195                       ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
196 }
197
198 #ifdef CONFIG_PPC_STD_MMU_64
199 static void __init check_cpu_slb_size(unsigned long node)
200 {
201         u32 *slb_size_ptr;
202
203         slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
204         if (slb_size_ptr != NULL) {
205                 mmu_slb_size = *slb_size_ptr;
206                 return;
207         }
208         slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
209         if (slb_size_ptr != NULL) {
210                 mmu_slb_size = *slb_size_ptr;
211         }
212 }
213 #else
214 #define check_cpu_slb_size(node) do { } while(0)
215 #endif
216
217 static struct feature_property {
218         const char *name;
219         u32 min_value;
220         unsigned long cpu_feature;
221         unsigned long cpu_user_ftr;
222 } feature_properties[] __initdata = {
223 #ifdef CONFIG_ALTIVEC
224         {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
225         {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
226 #endif /* CONFIG_ALTIVEC */
227 #ifdef CONFIG_VSX
228         /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
229         {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
230 #endif /* CONFIG_VSX */
231 #ifdef CONFIG_PPC64
232         {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
233         {"ibm,purr", 1, CPU_FTR_PURR, 0},
234         {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
235 #endif /* CONFIG_PPC64 */
236 };
237
238 #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
239 static inline void identical_pvr_fixup(unsigned long node)
240 {
241         unsigned int pvr;
242         char *model = of_get_flat_dt_prop(node, "model", NULL);
243
244         /*
245          * Since 440GR(x)/440EP(x) processors have the same pvr,
246          * we check the node path and set bit 28 in the cur_cpu_spec
247          * pvr for EP(x) processor version. This bit is always 0 in
248          * the "real" pvr. Then we call identify_cpu again with
249          * the new logical pvr to enable FPU support.
250          */
251         if (model && strstr(model, "440EP")) {
252                 pvr = cur_cpu_spec->pvr_value | 0x8;
253                 identify_cpu(0, pvr);
254                 DBG("Using logical pvr %x for %s\n", pvr, model);
255         }
256 }
257 #else
258 #define identical_pvr_fixup(node) do { } while(0)
259 #endif
260
261 static void __init check_cpu_feature_properties(unsigned long node)
262 {
263         unsigned long i;
264         struct feature_property *fp = feature_properties;
265         const u32 *prop;
266
267         for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
268                 prop = of_get_flat_dt_prop(node, fp->name, NULL);
269                 if (prop && *prop >= fp->min_value) {
270                         cur_cpu_spec->cpu_features |= fp->cpu_feature;
271                         cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
272                 }
273         }
274 }
275
276 static int __init early_init_dt_scan_cpus(unsigned long node,
277                                           const char *uname, int depth,
278                                           void *data)
279 {
280         static int logical_cpuid = 0;
281         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
282         const u32 *prop;
283         const u32 *intserv;
284         int i, nthreads;
285         unsigned long len;
286         int found = 0;
287
288         /* We are scanning "cpu" nodes only */
289         if (type == NULL || strcmp(type, "cpu") != 0)
290                 return 0;
291
292         /* Get physical cpuid */
293         intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
294         if (intserv) {
295                 nthreads = len / sizeof(int);
296         } else {
297                 intserv = of_get_flat_dt_prop(node, "reg", NULL);
298                 nthreads = 1;
299         }
300
301         /*
302          * Now see if any of these threads match our boot cpu.
303          * NOTE: This must match the parsing done in smp_setup_cpu_maps.
304          */
305         for (i = 0; i < nthreads; i++) {
306                 /*
307                  * version 2 of the kexec param format adds the phys cpuid of
308                  * booted proc.
309                  */
310                 if (initial_boot_params && initial_boot_params->version >= 2) {
311                         if (intserv[i] ==
312                                         initial_boot_params->boot_cpuid_phys) {
313                                 found = 1;
314                                 break;
315                         }
316                 } else {
317                         /*
318                          * Check if it's the boot-cpu, set it's hw index now,
319                          * unfortunately this format did not support booting
320                          * off secondary threads.
321                          */
322                         if (of_get_flat_dt_prop(node,
323                                         "linux,boot-cpu", NULL) != NULL) {
324                                 found = 1;
325                                 break;
326                         }
327                 }
328
329 #ifdef CONFIG_SMP
330                 /* logical cpu id is always 0 on UP kernels */
331                 logical_cpuid++;
332 #endif
333         }
334
335         if (found) {
336                 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
337                         intserv[i]);
338                 boot_cpuid = logical_cpuid;
339                 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
340
341                 /*
342                  * PAPR defines "logical" PVR values for cpus that
343                  * meet various levels of the architecture:
344                  * 0x0f000001   Architecture version 2.04
345                  * 0x0f000002   Architecture version 2.05
346                  * If the cpu-version property in the cpu node contains
347                  * such a value, we call identify_cpu again with the
348                  * logical PVR value in order to use the cpu feature
349                  * bits appropriate for the architecture level.
350                  *
351                  * A POWER6 partition in "POWER6 architected" mode
352                  * uses the 0x0f000002 PVR value; in POWER5+ mode
353                  * it uses 0x0f000001.
354                  */
355                 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
356                 if (prop && (*prop & 0xff000000) == 0x0f000000)
357                         identify_cpu(0, *prop);
358
359                 identical_pvr_fixup(node);
360         }
361
362         check_cpu_feature_properties(node);
363         check_cpu_pa_features(node);
364         check_cpu_slb_size(node);
365
366 #ifdef CONFIG_PPC_PSERIES
367         if (nthreads > 1)
368                 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
369         else
370                 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
371 #endif
372
373         return 0;
374 }
375
376 #ifdef CONFIG_BLK_DEV_INITRD
377 static void __init early_init_dt_check_for_initrd(unsigned long node)
378 {
379         unsigned long l;
380         u32 *prop;
381
382         DBG("Looking for initrd properties... ");
383
384         prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
385         if (prop) {
386                 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
387
388                 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
389                 if (prop) {
390                         initrd_end = (unsigned long)
391                                         __va(of_read_ulong(prop, l/4));
392                         initrd_below_start_ok = 1;
393                 } else {
394                         initrd_start = 0;
395                 }
396         }
397
398         DBG("initrd_start=0x%lx  initrd_end=0x%lx\n", initrd_start, initrd_end);
399 }
400 #else
401 static inline void early_init_dt_check_for_initrd(unsigned long node)
402 {
403 }
404 #endif /* CONFIG_BLK_DEV_INITRD */
405
406 static int __init early_init_dt_scan_chosen(unsigned long node,
407                                             const char *uname, int depth, void *data)
408 {
409         unsigned long *lprop;
410         unsigned long l;
411         char *p;
412
413         DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
414
415         if (depth != 1 ||
416             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
417                 return 0;
418
419 #ifdef CONFIG_PPC64
420         /* check if iommu is forced on or off */
421         if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
422                 iommu_is_off = 1;
423         if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
424                 iommu_force_on = 1;
425 #endif
426
427         /* mem=x on the command line is the preferred mechanism */
428         lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
429         if (lprop)
430                 memory_limit = *lprop;
431
432 #ifdef CONFIG_PPC64
433         lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
434         if (lprop)
435                 tce_alloc_start = *lprop;
436         lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
437         if (lprop)
438                 tce_alloc_end = *lprop;
439 #endif
440
441 #ifdef CONFIG_KEXEC
442         lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
443         if (lprop)
444                 crashk_res.start = *lprop;
445
446         lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
447         if (lprop)
448                 crashk_res.end = crashk_res.start + *lprop - 1;
449 #endif
450
451         early_init_dt_check_for_initrd(node);
452
453         /* Retreive command line */
454         p = of_get_flat_dt_prop(node, "bootargs", &l);
455         if (p != NULL && l > 0)
456                 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
457
458 #ifdef CONFIG_CMDLINE
459         if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
460                 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
461 #endif /* CONFIG_CMDLINE */
462
463         DBG("Command line is: %s\n", cmd_line);
464
465         /* break now */
466         return 1;
467 }
468
469 static int __init early_init_dt_scan_root(unsigned long node,
470                                           const char *uname, int depth, void *data)
471 {
472         u32 *prop;
473
474         if (depth != 0)
475                 return 0;
476
477         prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
478         dt_root_size_cells = (prop == NULL) ? 1 : *prop;
479         DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
480
481         prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
482         dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
483         DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
484         
485         /* break now */
486         return 1;
487 }
488
489 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
490 {
491         cell_t *p = *cellp;
492
493         *cellp = p + s;
494         return of_read_number(p, s);
495 }
496
497 #ifdef CONFIG_PPC_PSERIES
498 /*
499  * Interpret the ibm,dynamic-memory property in the
500  * /ibm,dynamic-reconfiguration-memory node.
501  * This contains a list of memory blocks along with NUMA affinity
502  * information.
503  */
504 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
505 {
506         cell_t *dm, *ls, *usm;
507         unsigned long l, n, flags;
508         u64 base, size, lmb_size;
509         unsigned int is_kexec_kdump = 0, rngs;
510
511         ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
512         if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
513                 return 0;
514         lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
515
516         dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
517         if (dm == NULL || l < sizeof(cell_t))
518                 return 0;
519
520         n = *dm++;      /* number of entries */
521         if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
522                 return 0;
523
524         /* check if this is a kexec/kdump kernel. */
525         usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
526                                                  &l);
527         if (usm != NULL)
528                 is_kexec_kdump = 1;
529
530         for (; n != 0; --n) {
531                 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
532                 flags = dm[3];
533                 /* skip DRC index, pad, assoc. list index, flags */
534                 dm += 4;
535                 /* skip this block if the reserved bit is set in flags (0x80)
536                    or if the block is not assigned to this partition (0x8) */
537                 if ((flags & 0x80) || !(flags & 0x8))
538                         continue;
539                 size = lmb_size;
540                 rngs = 1;
541                 if (is_kexec_kdump) {
542                         /*
543                          * For each lmb in ibm,dynamic-memory, a corresponding
544                          * entry in linux,drconf-usable-memory property contains
545                          * a counter 'p' followed by 'p' (base, size) duple.
546                          * Now read the counter from
547                          * linux,drconf-usable-memory property
548                          */
549                         rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
550                         if (!rngs) /* there are no (base, size) duple */
551                                 continue;
552                 }
553                 do {
554                         if (is_kexec_kdump) {
555                                 base = dt_mem_next_cell(dt_root_addr_cells,
556                                                          &usm);
557                                 size = dt_mem_next_cell(dt_root_size_cells,
558                                                          &usm);
559                         }
560                         if (iommu_is_off) {
561                                 if (base >= 0x80000000ul)
562                                         continue;
563                                 if ((base + size) > 0x80000000ul)
564                                         size = 0x80000000ul - base;
565                         }
566                         lmb_add(base, size);
567                 } while (--rngs);
568         }
569         lmb_dump_all();
570         return 0;
571 }
572 #else
573 #define early_init_dt_scan_drconf_memory(node)  0
574 #endif /* CONFIG_PPC_PSERIES */
575
576 static int __init early_init_dt_scan_memory(unsigned long node,
577                                             const char *uname, int depth, void *data)
578 {
579         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
580         cell_t *reg, *endp;
581         unsigned long l;
582
583         /* Look for the ibm,dynamic-reconfiguration-memory node */
584         if (depth == 1 &&
585             strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
586                 return early_init_dt_scan_drconf_memory(node);
587
588         /* We are scanning "memory" nodes only */
589         if (type == NULL) {
590                 /*
591                  * The longtrail doesn't have a device_type on the
592                  * /memory node, so look for the node called /memory@0.
593                  */
594                 if (depth != 1 || strcmp(uname, "memory@0") != 0)
595                         return 0;
596         } else if (strcmp(type, "memory") != 0)
597                 return 0;
598
599         reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
600         if (reg == NULL)
601                 reg = of_get_flat_dt_prop(node, "reg", &l);
602         if (reg == NULL)
603                 return 0;
604
605         endp = reg + (l / sizeof(cell_t));
606
607         DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
608             uname, l, reg[0], reg[1], reg[2], reg[3]);
609
610         while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
611                 u64 base, size;
612
613                 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
614                 size = dt_mem_next_cell(dt_root_size_cells, &reg);
615
616                 if (size == 0)
617                         continue;
618                 DBG(" - %llx ,  %llx\n", (unsigned long long)base,
619                     (unsigned long long)size);
620 #ifdef CONFIG_PPC64
621                 if (iommu_is_off) {
622                         if (base >= 0x80000000ul)
623                                 continue;
624                         if ((base + size) > 0x80000000ul)
625                                 size = 0x80000000ul - base;
626                 }
627 #endif
628                 lmb_add(base, size);
629
630                 memstart_addr = min((u64)memstart_addr, base);
631         }
632
633         return 0;
634 }
635
636 static void __init early_reserve_mem(void)
637 {
638         u64 base, size;
639         u64 *reserve_map;
640         unsigned long self_base;
641         unsigned long self_size;
642
643         reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
644                                         initial_boot_params->off_mem_rsvmap);
645
646         /* before we do anything, lets reserve the dt blob */
647         self_base = __pa((unsigned long)initial_boot_params);
648         self_size = initial_boot_params->totalsize;
649         lmb_reserve(self_base, self_size);
650
651 #ifdef CONFIG_BLK_DEV_INITRD
652         /* then reserve the initrd, if any */
653         if (initrd_start && (initrd_end > initrd_start))
654                 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
655 #endif /* CONFIG_BLK_DEV_INITRD */
656
657 #ifdef CONFIG_PPC32
658         /* 
659          * Handle the case where we might be booting from an old kexec
660          * image that setup the mem_rsvmap as pairs of 32-bit values
661          */
662         if (*reserve_map > 0xffffffffull) {
663                 u32 base_32, size_32;
664                 u32 *reserve_map_32 = (u32 *)reserve_map;
665
666                 while (1) {
667                         base_32 = *(reserve_map_32++);
668                         size_32 = *(reserve_map_32++);
669                         if (size_32 == 0)
670                                 break;
671                         /* skip if the reservation is for the blob */
672                         if (base_32 == self_base && size_32 == self_size)
673                                 continue;
674                         DBG("reserving: %x -> %x\n", base_32, size_32);
675                         lmb_reserve(base_32, size_32);
676                 }
677                 return;
678         }
679 #endif
680         while (1) {
681                 base = *(reserve_map++);
682                 size = *(reserve_map++);
683                 if (size == 0)
684                         break;
685                 DBG("reserving: %llx -> %llx\n", base, size);
686                 lmb_reserve(base, size);
687         }
688 }
689
690 #ifdef CONFIG_PHYP_DUMP
691 /**
692  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
693  *
694  * Function to find the largest size we need to reserve
695  * during early boot process.
696  *
697  * It either looks for boot param and returns that OR
698  * returns larger of 256 or 5% rounded down to multiples of 256MB.
699  *
700  */
701 static inline unsigned long phyp_dump_calculate_reserve_size(void)
702 {
703         unsigned long tmp;
704
705         if (phyp_dump_info->reserve_bootvar)
706                 return phyp_dump_info->reserve_bootvar;
707
708         /* divide by 20 to get 5% of value */
709         tmp = lmb_end_of_DRAM();
710         do_div(tmp, 20);
711
712         /* round it down in multiples of 256 */
713         tmp = tmp & ~0x0FFFFFFFUL;
714
715         return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
716 }
717
718 /**
719  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
720  *
721  * This routine may reserve memory regions in the kernel only
722  * if the system is supported and a dump was taken in last
723  * boot instance or if the hardware is supported and the
724  * scratch area needs to be setup. In other instances it returns
725  * without reserving anything. The memory in case of dump being
726  * active is freed when the dump is collected (by userland tools).
727  */
728 static void __init phyp_dump_reserve_mem(void)
729 {
730         unsigned long base, size;
731         unsigned long variable_reserve_size;
732
733         if (!phyp_dump_info->phyp_dump_configured) {
734                 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
735                 return;
736         }
737
738         if (!phyp_dump_info->phyp_dump_at_boot) {
739                 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
740                 return;
741         }
742
743         variable_reserve_size = phyp_dump_calculate_reserve_size();
744
745         if (phyp_dump_info->phyp_dump_is_active) {
746                 /* Reserve *everything* above RMR.Area freed by userland tools*/
747                 base = variable_reserve_size;
748                 size = lmb_end_of_DRAM() - base;
749
750                 /* XXX crashed_ram_end is wrong, since it may be beyond
751                  * the memory_limit, it will need to be adjusted. */
752                 lmb_reserve(base, size);
753
754                 phyp_dump_info->init_reserve_start = base;
755                 phyp_dump_info->init_reserve_size = size;
756         } else {
757                 size = phyp_dump_info->cpu_state_size +
758                         phyp_dump_info->hpte_region_size +
759                         variable_reserve_size;
760                 base = lmb_end_of_DRAM() - size;
761                 lmb_reserve(base, size);
762                 phyp_dump_info->init_reserve_start = base;
763                 phyp_dump_info->init_reserve_size = size;
764         }
765 }
766 #else
767 static inline void __init phyp_dump_reserve_mem(void) {}
768 #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
769
770
771 void __init early_init_devtree(void *params)
772 {
773         phys_addr_t limit;
774
775         DBG(" -> early_init_devtree(%p)\n", params);
776
777         /* Setup flat device-tree pointer */
778         initial_boot_params = params;
779
780 #ifdef CONFIG_PPC_RTAS
781         /* Some machines might need RTAS info for debugging, grab it now. */
782         of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
783 #endif
784
785 #ifdef CONFIG_PHYP_DUMP
786         /* scan tree to see if dump occured during last boot */
787         of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
788 #endif
789
790         /* Retrieve various informations from the /chosen node of the
791          * device-tree, including the platform type, initrd location and
792          * size, TCE reserve, and more ...
793          */
794         of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
795
796         /* Scan memory nodes and rebuild LMBs */
797         lmb_init();
798         of_scan_flat_dt(early_init_dt_scan_root, NULL);
799         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
800
801         /* Save command line for /proc/cmdline and then parse parameters */
802         strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
803         parse_early_param();
804
805         /* Reserve LMB regions used by kernel, initrd, dt, etc... */
806         lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
807         /* If relocatable, reserve first 32k for interrupt vectors etc. */
808         if (PHYSICAL_START > MEMORY_START)
809                 lmb_reserve(MEMORY_START, 0x8000);
810         reserve_kdump_trampoline();
811         reserve_crashkernel();
812         early_reserve_mem();
813         phyp_dump_reserve_mem();
814
815         limit = memory_limit;
816         if (! limit) {
817                 phys_addr_t memsize;
818
819                 /* Ensure that total memory size is page-aligned, because
820                  * otherwise mark_bootmem() gets upset. */
821                 lmb_analyze();
822                 memsize = lmb_phys_mem_size();
823                 if ((memsize & PAGE_MASK) != memsize)
824                         limit = memsize & PAGE_MASK;
825         }
826         lmb_enforce_memory_limit(limit);
827
828         lmb_analyze();
829         lmb_dump_all();
830
831         DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
832
833         /* We may need to relocate the flat tree, do it now.
834          * FIXME .. and the initrd too? */
835         move_device_tree();
836
837         DBG("Scanning CPUs ...\n");
838
839         /* Retreive CPU related informations from the flat tree
840          * (altivec support, boot CPU ID, ...)
841          */
842         of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
843
844         DBG(" <- early_init_devtree()\n");
845 }
846
847
848 /**
849  * Indicates whether the root node has a given value in its
850  * compatible property.
851  */
852 int machine_is_compatible(const char *compat)
853 {
854         struct device_node *root;
855         int rc = 0;
856
857         root = of_find_node_by_path("/");
858         if (root) {
859                 rc = of_device_is_compatible(root, compat);
860                 of_node_put(root);
861         }
862         return rc;
863 }
864 EXPORT_SYMBOL(machine_is_compatible);
865
866 /*******
867  *
868  * New implementation of the OF "find" APIs, return a refcounted
869  * object, call of_node_put() when done.  The device tree and list
870  * are protected by a rw_lock.
871  *
872  * Note that property management will need some locking as well,
873  * this isn't dealt with yet.
874  *
875  *******/
876
877 /**
878  *      of_find_node_by_phandle - Find a node given a phandle
879  *      @handle:        phandle of the node to find
880  *
881  *      Returns a node pointer with refcount incremented, use
882  *      of_node_put() on it when done.
883  */
884 struct device_node *of_find_node_by_phandle(phandle handle)
885 {
886         struct device_node *np;
887
888         read_lock(&devtree_lock);
889         for (np = allnodes; np != 0; np = np->allnext)
890                 if (np->linux_phandle == handle)
891                         break;
892         of_node_get(np);
893         read_unlock(&devtree_lock);
894         return np;
895 }
896 EXPORT_SYMBOL(of_find_node_by_phandle);
897
898 /**
899  *      of_find_next_cache_node - Find a node's subsidiary cache
900  *      @np:    node of type "cpu" or "cache"
901  *
902  *      Returns a node pointer with refcount incremented, use
903  *      of_node_put() on it when done.  Caller should hold a reference
904  *      to np.
905  */
906 struct device_node *of_find_next_cache_node(struct device_node *np)
907 {
908         struct device_node *child;
909         const phandle *handle;
910
911         handle = of_get_property(np, "l2-cache", NULL);
912         if (!handle)
913                 handle = of_get_property(np, "next-level-cache", NULL);
914
915         if (handle)
916                 return of_find_node_by_phandle(*handle);
917
918         /* OF on pmac has nodes instead of properties named "l2-cache"
919          * beneath CPU nodes.
920          */
921         if (!strcmp(np->type, "cpu"))
922                 for_each_child_of_node(np, child)
923                         if (!strcmp(child->type, "cache"))
924                                 return child;
925
926         return NULL;
927 }
928
929 /**
930  *      of_node_get - Increment refcount of a node
931  *      @node:  Node to inc refcount, NULL is supported to
932  *              simplify writing of callers
933  *
934  *      Returns node.
935  */
936 struct device_node *of_node_get(struct device_node *node)
937 {
938         if (node)
939                 kref_get(&node->kref);
940         return node;
941 }
942 EXPORT_SYMBOL(of_node_get);
943
944 static inline struct device_node * kref_to_device_node(struct kref *kref)
945 {
946         return container_of(kref, struct device_node, kref);
947 }
948
949 /**
950  *      of_node_release - release a dynamically allocated node
951  *      @kref:  kref element of the node to be released
952  *
953  *      In of_node_put() this function is passed to kref_put()
954  *      as the destructor.
955  */
956 static void of_node_release(struct kref *kref)
957 {
958         struct device_node *node = kref_to_device_node(kref);
959         struct property *prop = node->properties;
960
961         /* We should never be releasing nodes that haven't been detached. */
962         if (!of_node_check_flag(node, OF_DETACHED)) {
963                 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
964                 dump_stack();
965                 kref_init(&node->kref);
966                 return;
967         }
968
969         if (!of_node_check_flag(node, OF_DYNAMIC))
970                 return;
971
972         while (prop) {
973                 struct property *next = prop->next;
974                 kfree(prop->name);
975                 kfree(prop->value);
976                 kfree(prop);
977                 prop = next;
978
979                 if (!prop) {
980                         prop = node->deadprops;
981                         node->deadprops = NULL;
982                 }
983         }
984         kfree(node->full_name);
985         kfree(node->data);
986         kfree(node);
987 }
988
989 /**
990  *      of_node_put - Decrement refcount of a node
991  *      @node:  Node to dec refcount, NULL is supported to
992  *              simplify writing of callers
993  *
994  */
995 void of_node_put(struct device_node *node)
996 {
997         if (node)
998                 kref_put(&node->kref, of_node_release);
999 }
1000 EXPORT_SYMBOL(of_node_put);
1001
1002 /*
1003  * Plug a device node into the tree and global list.
1004  */
1005 void of_attach_node(struct device_node *np)
1006 {
1007         unsigned long flags;
1008
1009         write_lock_irqsave(&devtree_lock, flags);
1010         np->sibling = np->parent->child;
1011         np->allnext = allnodes;
1012         np->parent->child = np;
1013         allnodes = np;
1014         write_unlock_irqrestore(&devtree_lock, flags);
1015 }
1016
1017 /*
1018  * "Unplug" a node from the device tree.  The caller must hold
1019  * a reference to the node.  The memory associated with the node
1020  * is not freed until its refcount goes to zero.
1021  */
1022 void of_detach_node(struct device_node *np)
1023 {
1024         struct device_node *parent;
1025         unsigned long flags;
1026
1027         write_lock_irqsave(&devtree_lock, flags);
1028
1029         parent = np->parent;
1030         if (!parent)
1031                 goto out_unlock;
1032
1033         if (allnodes == np)
1034                 allnodes = np->allnext;
1035         else {
1036                 struct device_node *prev;
1037                 for (prev = allnodes;
1038                      prev->allnext != np;
1039                      prev = prev->allnext)
1040                         ;
1041                 prev->allnext = np->allnext;
1042         }
1043
1044         if (parent->child == np)
1045                 parent->child = np->sibling;
1046         else {
1047                 struct device_node *prevsib;
1048                 for (prevsib = np->parent->child;
1049                      prevsib->sibling != np;
1050                      prevsib = prevsib->sibling)
1051                         ;
1052                 prevsib->sibling = np->sibling;
1053         }
1054
1055         of_node_set_flag(np, OF_DETACHED);
1056
1057 out_unlock:
1058         write_unlock_irqrestore(&devtree_lock, flags);
1059 }
1060
1061 #ifdef CONFIG_PPC_PSERIES
1062 /*
1063  * Fix up the uninitialized fields in a new device node:
1064  * name, type and pci-specific fields
1065  */
1066
1067 static int of_finish_dynamic_node(struct device_node *node)
1068 {
1069         struct device_node *parent = of_get_parent(node);
1070         int err = 0;
1071         const phandle *ibm_phandle;
1072
1073         node->name = of_get_property(node, "name", NULL);
1074         node->type = of_get_property(node, "device_type", NULL);
1075
1076         if (!node->name)
1077                 node->name = "<NULL>";
1078         if (!node->type)
1079                 node->type = "<NULL>";
1080
1081         if (!parent) {
1082                 err = -ENODEV;
1083                 goto out;
1084         }
1085
1086         /* We don't support that function on PowerMac, at least
1087          * not yet
1088          */
1089         if (machine_is(powermac))
1090                 return -ENODEV;
1091
1092         /* fix up new node's linux_phandle field */
1093         if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1094                 node->linux_phandle = *ibm_phandle;
1095
1096 out:
1097         of_node_put(parent);
1098         return err;
1099 }
1100
1101 static int prom_reconfig_notifier(struct notifier_block *nb,
1102                                   unsigned long action, void *node)
1103 {
1104         int err;
1105
1106         switch (action) {
1107         case PSERIES_RECONFIG_ADD:
1108                 err = of_finish_dynamic_node(node);
1109                 if (err < 0) {
1110                         printk(KERN_ERR "finish_node returned %d\n", err);
1111                         err = NOTIFY_BAD;
1112                 }
1113                 break;
1114         default:
1115                 err = NOTIFY_DONE;
1116                 break;
1117         }
1118         return err;
1119 }
1120
1121 static struct notifier_block prom_reconfig_nb = {
1122         .notifier_call = prom_reconfig_notifier,
1123         .priority = 10, /* This one needs to run first */
1124 };
1125
1126 static int __init prom_reconfig_setup(void)
1127 {
1128         return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1129 }
1130 __initcall(prom_reconfig_setup);
1131 #endif
1132
1133 /*
1134  * Add a property to a node
1135  */
1136 int prom_add_property(struct device_node* np, struct property* prop)
1137 {
1138         struct property **next;
1139         unsigned long flags;
1140
1141         prop->next = NULL;      
1142         write_lock_irqsave(&devtree_lock, flags);
1143         next = &np->properties;
1144         while (*next) {
1145                 if (strcmp(prop->name, (*next)->name) == 0) {
1146                         /* duplicate ! don't insert it */
1147                         write_unlock_irqrestore(&devtree_lock, flags);
1148                         return -1;
1149                 }
1150                 next = &(*next)->next;
1151         }
1152         *next = prop;
1153         write_unlock_irqrestore(&devtree_lock, flags);
1154
1155 #ifdef CONFIG_PROC_DEVICETREE
1156         /* try to add to proc as well if it was initialized */
1157         if (np->pde)
1158                 proc_device_tree_add_prop(np->pde, prop);
1159 #endif /* CONFIG_PROC_DEVICETREE */
1160
1161         return 0;
1162 }
1163
1164 /*
1165  * Remove a property from a node.  Note that we don't actually
1166  * remove it, since we have given out who-knows-how-many pointers
1167  * to the data using get-property.  Instead we just move the property
1168  * to the "dead properties" list, so it won't be found any more.
1169  */
1170 int prom_remove_property(struct device_node *np, struct property *prop)
1171 {
1172         struct property **next;
1173         unsigned long flags;
1174         int found = 0;
1175
1176         write_lock_irqsave(&devtree_lock, flags);
1177         next = &np->properties;
1178         while (*next) {
1179                 if (*next == prop) {
1180                         /* found the node */
1181                         *next = prop->next;
1182                         prop->next = np->deadprops;
1183                         np->deadprops = prop;
1184                         found = 1;
1185                         break;
1186                 }
1187                 next = &(*next)->next;
1188         }
1189         write_unlock_irqrestore(&devtree_lock, flags);
1190
1191         if (!found)
1192                 return -ENODEV;
1193
1194 #ifdef CONFIG_PROC_DEVICETREE
1195         /* try to remove the proc node as well */
1196         if (np->pde)
1197                 proc_device_tree_remove_prop(np->pde, prop);
1198 #endif /* CONFIG_PROC_DEVICETREE */
1199
1200         return 0;
1201 }
1202
1203 /*
1204  * Update a property in a node.  Note that we don't actually
1205  * remove it, since we have given out who-knows-how-many pointers
1206  * to the data using get-property.  Instead we just move the property
1207  * to the "dead properties" list, and add the new property to the
1208  * property list
1209  */
1210 int prom_update_property(struct device_node *np,
1211                          struct property *newprop,
1212                          struct property *oldprop)
1213 {
1214         struct property **next;
1215         unsigned long flags;
1216         int found = 0;
1217
1218         write_lock_irqsave(&devtree_lock, flags);
1219         next = &np->properties;
1220         while (*next) {
1221                 if (*next == oldprop) {
1222                         /* found the node */
1223                         newprop->next = oldprop->next;
1224                         *next = newprop;
1225                         oldprop->next = np->deadprops;
1226                         np->deadprops = oldprop;
1227                         found = 1;
1228                         break;
1229                 }
1230                 next = &(*next)->next;
1231         }
1232         write_unlock_irqrestore(&devtree_lock, flags);
1233
1234         if (!found)
1235                 return -ENODEV;
1236
1237 #ifdef CONFIG_PROC_DEVICETREE
1238         /* try to add to proc as well if it was initialized */
1239         if (np->pde)
1240                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1241 #endif /* CONFIG_PROC_DEVICETREE */
1242
1243         return 0;
1244 }
1245
1246
1247 /* Find the device node for a given logical cpu number, also returns the cpu
1248  * local thread number (index in ibm,interrupt-server#s) if relevant and
1249  * asked for (non NULL)
1250  */
1251 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1252 {
1253         int hardid;
1254         struct device_node *np;
1255
1256         hardid = get_hard_smp_processor_id(cpu);
1257
1258         for_each_node_by_type(np, "cpu") {
1259                 const u32 *intserv;
1260                 unsigned int plen, t;
1261
1262                 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1263                  * fallback to "reg" property and assume no threads
1264                  */
1265                 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1266                                 &plen);
1267                 if (intserv == NULL) {
1268                         const u32 *reg = of_get_property(np, "reg", NULL);
1269                         if (reg == NULL)
1270                                 continue;
1271                         if (*reg == hardid) {
1272                                 if (thread)
1273                                         *thread = 0;
1274                                 return np;
1275                         }
1276                 } else {
1277                         plen /= sizeof(u32);
1278                         for (t = 0; t < plen; t++) {
1279                                 if (hardid == intserv[t]) {
1280                                         if (thread)
1281                                                 *thread = t;
1282                                         return np;
1283                                 }
1284                         }
1285                 }
1286         }
1287         return NULL;
1288 }
1289 EXPORT_SYMBOL(of_get_cpu_node);
1290
1291 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1292 static struct debugfs_blob_wrapper flat_dt_blob;
1293
1294 static int __init export_flat_device_tree(void)
1295 {
1296         struct dentry *d;
1297
1298         flat_dt_blob.data = initial_boot_params;
1299         flat_dt_blob.size = initial_boot_params->totalsize;
1300
1301         d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1302                                 powerpc_debugfs_root, &flat_dt_blob);
1303         if (!d)
1304                 return 1;
1305
1306         return 0;
1307 }
1308 __initcall(export_flat_device_tree);
1309 #endif