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