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