of/flattree: Merge earlyinit_dt_scan_root()
[deliverable/linux.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 static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
436 {
437 cell_t *p = *cellp;
438
439 *cellp = p + s;
440 return of_read_number(p, s);
441 }
442
443 #ifdef CONFIG_PPC_PSERIES
444 /*
445 * Interpret the ibm,dynamic-memory property in the
446 * /ibm,dynamic-reconfiguration-memory node.
447 * This contains a list of memory blocks along with NUMA affinity
448 * information.
449 */
450 static int __init early_init_dt_scan_drconf_memory(unsigned long node)
451 {
452 cell_t *dm, *ls, *usm;
453 unsigned long l, n, flags;
454 u64 base, size, lmb_size;
455 unsigned int is_kexec_kdump = 0, rngs;
456
457 ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
458 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
459 return 0;
460 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
461
462 dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
463 if (dm == NULL || l < sizeof(cell_t))
464 return 0;
465
466 n = *dm++; /* number of entries */
467 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
468 return 0;
469
470 /* check if this is a kexec/kdump kernel. */
471 usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
472 &l);
473 if (usm != NULL)
474 is_kexec_kdump = 1;
475
476 for (; n != 0; --n) {
477 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
478 flags = dm[3];
479 /* skip DRC index, pad, assoc. list index, flags */
480 dm += 4;
481 /* skip this block if the reserved bit is set in flags (0x80)
482 or if the block is not assigned to this partition (0x8) */
483 if ((flags & 0x80) || !(flags & 0x8))
484 continue;
485 size = lmb_size;
486 rngs = 1;
487 if (is_kexec_kdump) {
488 /*
489 * For each lmb in ibm,dynamic-memory, a corresponding
490 * entry in linux,drconf-usable-memory property contains
491 * a counter 'p' followed by 'p' (base, size) duple.
492 * Now read the counter from
493 * linux,drconf-usable-memory property
494 */
495 rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
496 if (!rngs) /* there are no (base, size) duple */
497 continue;
498 }
499 do {
500 if (is_kexec_kdump) {
501 base = dt_mem_next_cell(dt_root_addr_cells,
502 &usm);
503 size = dt_mem_next_cell(dt_root_size_cells,
504 &usm);
505 }
506 if (iommu_is_off) {
507 if (base >= 0x80000000ul)
508 continue;
509 if ((base + size) > 0x80000000ul)
510 size = 0x80000000ul - base;
511 }
512 lmb_add(base, size);
513 } while (--rngs);
514 }
515 lmb_dump_all();
516 return 0;
517 }
518 #else
519 #define early_init_dt_scan_drconf_memory(node) 0
520 #endif /* CONFIG_PPC_PSERIES */
521
522 static int __init early_init_dt_scan_memory(unsigned long node,
523 const char *uname, int depth, void *data)
524 {
525 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
526 cell_t *reg, *endp;
527 unsigned long l;
528
529 /* Look for the ibm,dynamic-reconfiguration-memory node */
530 if (depth == 1 &&
531 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
532 return early_init_dt_scan_drconf_memory(node);
533
534 /* We are scanning "memory" nodes only */
535 if (type == NULL) {
536 /*
537 * The longtrail doesn't have a device_type on the
538 * /memory node, so look for the node called /memory@0.
539 */
540 if (depth != 1 || strcmp(uname, "memory@0") != 0)
541 return 0;
542 } else if (strcmp(type, "memory") != 0)
543 return 0;
544
545 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
546 if (reg == NULL)
547 reg = of_get_flat_dt_prop(node, "reg", &l);
548 if (reg == NULL)
549 return 0;
550
551 endp = reg + (l / sizeof(cell_t));
552
553 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
554 uname, l, reg[0], reg[1], reg[2], reg[3]);
555
556 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
557 u64 base, size;
558
559 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
560 size = dt_mem_next_cell(dt_root_size_cells, &reg);
561
562 if (size == 0)
563 continue;
564 DBG(" - %llx , %llx\n", (unsigned long long)base,
565 (unsigned long long)size);
566 #ifdef CONFIG_PPC64
567 if (iommu_is_off) {
568 if (base >= 0x80000000ul)
569 continue;
570 if ((base + size) > 0x80000000ul)
571 size = 0x80000000ul - base;
572 }
573 #endif
574 lmb_add(base, size);
575
576 memstart_addr = min((u64)memstart_addr, base);
577 }
578
579 return 0;
580 }
581
582 static void __init early_reserve_mem(void)
583 {
584 u64 base, size;
585 u64 *reserve_map;
586 unsigned long self_base;
587 unsigned long self_size;
588
589 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
590 initial_boot_params->off_mem_rsvmap);
591
592 /* before we do anything, lets reserve the dt blob */
593 self_base = __pa((unsigned long)initial_boot_params);
594 self_size = initial_boot_params->totalsize;
595 lmb_reserve(self_base, self_size);
596
597 #ifdef CONFIG_BLK_DEV_INITRD
598 /* then reserve the initrd, if any */
599 if (initrd_start && (initrd_end > initrd_start))
600 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
601 #endif /* CONFIG_BLK_DEV_INITRD */
602
603 #ifdef CONFIG_PPC32
604 /*
605 * Handle the case where we might be booting from an old kexec
606 * image that setup the mem_rsvmap as pairs of 32-bit values
607 */
608 if (*reserve_map > 0xffffffffull) {
609 u32 base_32, size_32;
610 u32 *reserve_map_32 = (u32 *)reserve_map;
611
612 while (1) {
613 base_32 = *(reserve_map_32++);
614 size_32 = *(reserve_map_32++);
615 if (size_32 == 0)
616 break;
617 /* skip if the reservation is for the blob */
618 if (base_32 == self_base && size_32 == self_size)
619 continue;
620 DBG("reserving: %x -> %x\n", base_32, size_32);
621 lmb_reserve(base_32, size_32);
622 }
623 return;
624 }
625 #endif
626 while (1) {
627 base = *(reserve_map++);
628 size = *(reserve_map++);
629 if (size == 0)
630 break;
631 DBG("reserving: %llx -> %llx\n", base, size);
632 lmb_reserve(base, size);
633 }
634 }
635
636 #ifdef CONFIG_PHYP_DUMP
637 /**
638 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
639 *
640 * Function to find the largest size we need to reserve
641 * during early boot process.
642 *
643 * It either looks for boot param and returns that OR
644 * returns larger of 256 or 5% rounded down to multiples of 256MB.
645 *
646 */
647 static inline unsigned long phyp_dump_calculate_reserve_size(void)
648 {
649 unsigned long tmp;
650
651 if (phyp_dump_info->reserve_bootvar)
652 return phyp_dump_info->reserve_bootvar;
653
654 /* divide by 20 to get 5% of value */
655 tmp = lmb_end_of_DRAM();
656 do_div(tmp, 20);
657
658 /* round it down in multiples of 256 */
659 tmp = tmp & ~0x0FFFFFFFUL;
660
661 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
662 }
663
664 /**
665 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
666 *
667 * This routine may reserve memory regions in the kernel only
668 * if the system is supported and a dump was taken in last
669 * boot instance or if the hardware is supported and the
670 * scratch area needs to be setup. In other instances it returns
671 * without reserving anything. The memory in case of dump being
672 * active is freed when the dump is collected (by userland tools).
673 */
674 static void __init phyp_dump_reserve_mem(void)
675 {
676 unsigned long base, size;
677 unsigned long variable_reserve_size;
678
679 if (!phyp_dump_info->phyp_dump_configured) {
680 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
681 return;
682 }
683
684 if (!phyp_dump_info->phyp_dump_at_boot) {
685 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
686 return;
687 }
688
689 variable_reserve_size = phyp_dump_calculate_reserve_size();
690
691 if (phyp_dump_info->phyp_dump_is_active) {
692 /* Reserve *everything* above RMR.Area freed by userland tools*/
693 base = variable_reserve_size;
694 size = lmb_end_of_DRAM() - base;
695
696 /* XXX crashed_ram_end is wrong, since it may be beyond
697 * the memory_limit, it will need to be adjusted. */
698 lmb_reserve(base, size);
699
700 phyp_dump_info->init_reserve_start = base;
701 phyp_dump_info->init_reserve_size = size;
702 } else {
703 size = phyp_dump_info->cpu_state_size +
704 phyp_dump_info->hpte_region_size +
705 variable_reserve_size;
706 base = lmb_end_of_DRAM() - size;
707 lmb_reserve(base, size);
708 phyp_dump_info->init_reserve_start = base;
709 phyp_dump_info->init_reserve_size = size;
710 }
711 }
712 #else
713 static inline void __init phyp_dump_reserve_mem(void) {}
714 #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
715
716
717 void __init early_init_devtree(void *params)
718 {
719 phys_addr_t limit;
720
721 DBG(" -> early_init_devtree(%p)\n", params);
722
723 /* Setup flat device-tree pointer */
724 initial_boot_params = params;
725
726 #ifdef CONFIG_PPC_RTAS
727 /* Some machines might need RTAS info for debugging, grab it now. */
728 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
729 #endif
730
731 #ifdef CONFIG_PHYP_DUMP
732 /* scan tree to see if dump occured during last boot */
733 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
734 #endif
735
736 /* Retrieve various informations from the /chosen node of the
737 * device-tree, including the platform type, initrd location and
738 * size, TCE reserve, and more ...
739 */
740 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
741
742 /* Scan memory nodes and rebuild LMBs */
743 lmb_init();
744 of_scan_flat_dt(early_init_dt_scan_root, NULL);
745 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
746
747 /* Save command line for /proc/cmdline and then parse parameters */
748 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
749 parse_early_param();
750
751 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
752 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
753 /* If relocatable, reserve first 32k for interrupt vectors etc. */
754 if (PHYSICAL_START > MEMORY_START)
755 lmb_reserve(MEMORY_START, 0x8000);
756 reserve_kdump_trampoline();
757 reserve_crashkernel();
758 early_reserve_mem();
759 phyp_dump_reserve_mem();
760
761 limit = memory_limit;
762 if (! limit) {
763 phys_addr_t memsize;
764
765 /* Ensure that total memory size is page-aligned, because
766 * otherwise mark_bootmem() gets upset. */
767 lmb_analyze();
768 memsize = lmb_phys_mem_size();
769 if ((memsize & PAGE_MASK) != memsize)
770 limit = memsize & PAGE_MASK;
771 }
772 lmb_enforce_memory_limit(limit);
773
774 lmb_analyze();
775 lmb_dump_all();
776
777 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
778
779 /* We may need to relocate the flat tree, do it now.
780 * FIXME .. and the initrd too? */
781 move_device_tree();
782
783 DBG("Scanning CPUs ...\n");
784
785 /* Retreive CPU related informations from the flat tree
786 * (altivec support, boot CPU ID, ...)
787 */
788 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
789
790 DBG(" <- early_init_devtree()\n");
791 }
792
793
794 /**
795 * Indicates whether the root node has a given value in its
796 * compatible property.
797 */
798 int machine_is_compatible(const char *compat)
799 {
800 struct device_node *root;
801 int rc = 0;
802
803 root = of_find_node_by_path("/");
804 if (root) {
805 rc = of_device_is_compatible(root, compat);
806 of_node_put(root);
807 }
808 return rc;
809 }
810 EXPORT_SYMBOL(machine_is_compatible);
811
812 /*******
813 *
814 * New implementation of the OF "find" APIs, return a refcounted
815 * object, call of_node_put() when done. The device tree and list
816 * are protected by a rw_lock.
817 *
818 * Note that property management will need some locking as well,
819 * this isn't dealt with yet.
820 *
821 *******/
822
823 /**
824 * of_find_node_by_phandle - Find a node given a phandle
825 * @handle: phandle of the node to find
826 *
827 * Returns a node pointer with refcount incremented, use
828 * of_node_put() on it when done.
829 */
830 struct device_node *of_find_node_by_phandle(phandle handle)
831 {
832 struct device_node *np;
833
834 read_lock(&devtree_lock);
835 for (np = allnodes; np != 0; np = np->allnext)
836 if (np->linux_phandle == handle)
837 break;
838 of_node_get(np);
839 read_unlock(&devtree_lock);
840 return np;
841 }
842 EXPORT_SYMBOL(of_find_node_by_phandle);
843
844 /**
845 * of_find_next_cache_node - Find a node's subsidiary cache
846 * @np: node of type "cpu" or "cache"
847 *
848 * Returns a node pointer with refcount incremented, use
849 * of_node_put() on it when done. Caller should hold a reference
850 * to np.
851 */
852 struct device_node *of_find_next_cache_node(struct device_node *np)
853 {
854 struct device_node *child;
855 const phandle *handle;
856
857 handle = of_get_property(np, "l2-cache", NULL);
858 if (!handle)
859 handle = of_get_property(np, "next-level-cache", NULL);
860
861 if (handle)
862 return of_find_node_by_phandle(*handle);
863
864 /* OF on pmac has nodes instead of properties named "l2-cache"
865 * beneath CPU nodes.
866 */
867 if (!strcmp(np->type, "cpu"))
868 for_each_child_of_node(np, child)
869 if (!strcmp(child->type, "cache"))
870 return child;
871
872 return NULL;
873 }
874
875 /**
876 * of_node_get - Increment refcount of a node
877 * @node: Node to inc refcount, NULL is supported to
878 * simplify writing of callers
879 *
880 * Returns node.
881 */
882 struct device_node *of_node_get(struct device_node *node)
883 {
884 if (node)
885 kref_get(&node->kref);
886 return node;
887 }
888 EXPORT_SYMBOL(of_node_get);
889
890 static inline struct device_node * kref_to_device_node(struct kref *kref)
891 {
892 return container_of(kref, struct device_node, kref);
893 }
894
895 /**
896 * of_node_release - release a dynamically allocated node
897 * @kref: kref element of the node to be released
898 *
899 * In of_node_put() this function is passed to kref_put()
900 * as the destructor.
901 */
902 static void of_node_release(struct kref *kref)
903 {
904 struct device_node *node = kref_to_device_node(kref);
905 struct property *prop = node->properties;
906
907 /* We should never be releasing nodes that haven't been detached. */
908 if (!of_node_check_flag(node, OF_DETACHED)) {
909 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
910 dump_stack();
911 kref_init(&node->kref);
912 return;
913 }
914
915 if (!of_node_check_flag(node, OF_DYNAMIC))
916 return;
917
918 while (prop) {
919 struct property *next = prop->next;
920 kfree(prop->name);
921 kfree(prop->value);
922 kfree(prop);
923 prop = next;
924
925 if (!prop) {
926 prop = node->deadprops;
927 node->deadprops = NULL;
928 }
929 }
930 kfree(node->full_name);
931 kfree(node->data);
932 kfree(node);
933 }
934
935 /**
936 * of_node_put - Decrement refcount of a node
937 * @node: Node to dec refcount, NULL is supported to
938 * simplify writing of callers
939 *
940 */
941 void of_node_put(struct device_node *node)
942 {
943 if (node)
944 kref_put(&node->kref, of_node_release);
945 }
946 EXPORT_SYMBOL(of_node_put);
947
948 /*
949 * Plug a device node into the tree and global list.
950 */
951 void of_attach_node(struct device_node *np)
952 {
953 unsigned long flags;
954
955 write_lock_irqsave(&devtree_lock, flags);
956 np->sibling = np->parent->child;
957 np->allnext = allnodes;
958 np->parent->child = np;
959 allnodes = np;
960 write_unlock_irqrestore(&devtree_lock, flags);
961 }
962
963 /*
964 * "Unplug" a node from the device tree. The caller must hold
965 * a reference to the node. The memory associated with the node
966 * is not freed until its refcount goes to zero.
967 */
968 void of_detach_node(struct device_node *np)
969 {
970 struct device_node *parent;
971 unsigned long flags;
972
973 write_lock_irqsave(&devtree_lock, flags);
974
975 parent = np->parent;
976 if (!parent)
977 goto out_unlock;
978
979 if (allnodes == np)
980 allnodes = np->allnext;
981 else {
982 struct device_node *prev;
983 for (prev = allnodes;
984 prev->allnext != np;
985 prev = prev->allnext)
986 ;
987 prev->allnext = np->allnext;
988 }
989
990 if (parent->child == np)
991 parent->child = np->sibling;
992 else {
993 struct device_node *prevsib;
994 for (prevsib = np->parent->child;
995 prevsib->sibling != np;
996 prevsib = prevsib->sibling)
997 ;
998 prevsib->sibling = np->sibling;
999 }
1000
1001 of_node_set_flag(np, OF_DETACHED);
1002
1003 out_unlock:
1004 write_unlock_irqrestore(&devtree_lock, flags);
1005 }
1006
1007 #ifdef CONFIG_PPC_PSERIES
1008 /*
1009 * Fix up the uninitialized fields in a new device node:
1010 * name, type and pci-specific fields
1011 */
1012
1013 static int of_finish_dynamic_node(struct device_node *node)
1014 {
1015 struct device_node *parent = of_get_parent(node);
1016 int err = 0;
1017 const phandle *ibm_phandle;
1018
1019 node->name = of_get_property(node, "name", NULL);
1020 node->type = of_get_property(node, "device_type", NULL);
1021
1022 if (!node->name)
1023 node->name = "<NULL>";
1024 if (!node->type)
1025 node->type = "<NULL>";
1026
1027 if (!parent) {
1028 err = -ENODEV;
1029 goto out;
1030 }
1031
1032 /* We don't support that function on PowerMac, at least
1033 * not yet
1034 */
1035 if (machine_is(powermac))
1036 return -ENODEV;
1037
1038 /* fix up new node's linux_phandle field */
1039 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
1040 node->linux_phandle = *ibm_phandle;
1041
1042 out:
1043 of_node_put(parent);
1044 return err;
1045 }
1046
1047 static int prom_reconfig_notifier(struct notifier_block *nb,
1048 unsigned long action, void *node)
1049 {
1050 int err;
1051
1052 switch (action) {
1053 case PSERIES_RECONFIG_ADD:
1054 err = of_finish_dynamic_node(node);
1055 if (err < 0) {
1056 printk(KERN_ERR "finish_node returned %d\n", err);
1057 err = NOTIFY_BAD;
1058 }
1059 break;
1060 default:
1061 err = NOTIFY_DONE;
1062 break;
1063 }
1064 return err;
1065 }
1066
1067 static struct notifier_block prom_reconfig_nb = {
1068 .notifier_call = prom_reconfig_notifier,
1069 .priority = 10, /* This one needs to run first */
1070 };
1071
1072 static int __init prom_reconfig_setup(void)
1073 {
1074 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1075 }
1076 __initcall(prom_reconfig_setup);
1077 #endif
1078
1079 /* Find the device node for a given logical cpu number, also returns the cpu
1080 * local thread number (index in ibm,interrupt-server#s) if relevant and
1081 * asked for (non NULL)
1082 */
1083 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1084 {
1085 int hardid;
1086 struct device_node *np;
1087
1088 hardid = get_hard_smp_processor_id(cpu);
1089
1090 for_each_node_by_type(np, "cpu") {
1091 const u32 *intserv;
1092 unsigned int plen, t;
1093
1094 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1095 * fallback to "reg" property and assume no threads
1096 */
1097 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1098 &plen);
1099 if (intserv == NULL) {
1100 const u32 *reg = of_get_property(np, "reg", NULL);
1101 if (reg == NULL)
1102 continue;
1103 if (*reg == hardid) {
1104 if (thread)
1105 *thread = 0;
1106 return np;
1107 }
1108 } else {
1109 plen /= sizeof(u32);
1110 for (t = 0; t < plen; t++) {
1111 if (hardid == intserv[t]) {
1112 if (thread)
1113 *thread = t;
1114 return np;
1115 }
1116 }
1117 }
1118 }
1119 return NULL;
1120 }
1121 EXPORT_SYMBOL(of_get_cpu_node);
1122
1123 #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
1124 static struct debugfs_blob_wrapper flat_dt_blob;
1125
1126 static int __init export_flat_device_tree(void)
1127 {
1128 struct dentry *d;
1129
1130 flat_dt_blob.data = initial_boot_params;
1131 flat_dt_blob.size = initial_boot_params->totalsize;
1132
1133 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
1134 powerpc_debugfs_root, &flat_dt_blob);
1135 if (!d)
1136 return 1;
1137
1138 return 0;
1139 }
1140 __initcall(export_flat_device_tree);
1141 #endif
This page took 0.089292 seconds and 5 git commands to generate.