blob: 7f8856655144fd8d588f5ca50e9708f84d0e8a45 [file] [log] [blame]
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001/*
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>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100019#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>
Michael Ellermandcee3032005-12-04 18:39:48 +110031#include <linux/kexec.h>
Michael Ellerman7a4571a2006-06-23 18:16:03 +100032#include <linux/debugfs.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100033#include <linux/irq.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080034#include <linux/lmb.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100035
36#include <asm/prom.h>
37#include <asm/rtas.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100038#include <asm/page.h>
39#include <asm/processor.h>
40#include <asm/irq.h>
41#include <asm/io.h>
Michael Ellerman0cc47462005-12-04 18:39:37 +110042#include <asm/kdump.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100043#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>
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100053#include <asm/pci-bridge.h>
Manish Ahuja6ac26c82008-03-22 10:37:08 +110054#include <asm/phyp_dump.h>
Michael Ellerman2babf5c2006-05-17 18:00:46 +100055#include <asm/kexec.h>
Kumar Gala37dd2ba2008-04-22 04:22:34 +100056#include <mm/mmu_decl.h>
Paul Mackerras9b6b5632005-10-06 12:06:20 +100057
58#ifdef DEBUG
59#define DBG(fmt...) printk(KERN_ERR fmt)
60#else
61#define DBG(fmt...)
62#endif
63
Paul Mackerras9b6b5632005-10-06 12:06:20 +100064
Paul Mackerras9b6b5632005-10-06 12:06:20 +100065static int __initdata dt_root_addr_cells;
66static int __initdata dt_root_size_cells;
67
68#ifdef CONFIG_PPC64
Olof Johansson28897732006-04-12 21:52:33 -050069int __initdata iommu_is_off;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100070int __initdata iommu_force_on;
Paul Mackerrascf00a8d2005-10-31 13:07:02 +110071unsigned long tce_alloc_start, tce_alloc_end;
Paul Mackerras9b6b5632005-10-06 12:06:20 +100072#endif
73
74typedef u32 cell_t;
75
Stephen Rothwell581b6052007-04-24 16:46:53 +100076extern rwlock_t devtree_lock; /* temporary while merging */
Paul Mackerras9b6b5632005-10-06 12:06:20 +100077
78/* export that to outside world */
79struct device_node *of_chosen;
80
Michael Ellerman2babf5c2006-05-17 18:00:46 +100081static int __init early_parse_mem(char *p)
82{
83 if (!p)
84 return 1;
85
86 memory_limit = PAGE_ALIGN(memparse(p, &p));
Becky Bruce49a84962009-05-08 12:19:27 +000087 DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
Michael Ellerman2babf5c2006-05-17 18:00:46 +100088
89 return 0;
90}
91early_param("mem", early_parse_mem);
92
Linas Vepstas3c607ce2007-09-07 03:47:29 +100093/**
94 * move_device_tree - move tree to an unused area, if needed.
95 *
96 * The device tree may be allocated beyond our memory limit, or inside the
97 * crash kernel region for kdump. If so, move it out of the way.
Michael Ellerman2babf5c2006-05-17 18:00:46 +100098 */
Geert Uytterhoeven18f032c2008-03-29 03:07:45 +110099static void __init move_device_tree(void)
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000100{
101 unsigned long start, size;
102 void *p;
103
104 DBG("-> move_device_tree\n");
105
106 start = __pa(initial_boot_params);
107 size = initial_boot_params->totalsize;
108
109 if ((memory_limit && (start + size) > memory_limit) ||
110 overlaps_crashkernel(start, size)) {
111 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
112 memcpy(p, initial_boot_params, size);
113 initial_boot_params = (struct boot_param_header *)p;
114 DBG("Moved device tree to 0x%p\n", p);
115 }
116
117 DBG("<- move_device_tree\n");
118}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000119
Paul Mackerrasd2058192006-05-03 23:04:37 +1000120/*
121 * ibm,pa-features is a per-cpu property that contains a string of
122 * attribute descriptors, each of which has a 2 byte header plus up
123 * to 254 bytes worth of processor attribute bits. First header
124 * byte specifies the number of bytes following the header.
125 * Second header byte is an "attribute-specifier" type, of which
126 * zero is the only currently-defined value.
127 * Implementation: Pass in the byte and bit offset for the feature
128 * that we are interested in. The function will return -1 if the
129 * pa-features property is missing, or a 1/0 to indicate if the feature
130 * is supported/not supported. Note that the bit numbers are
131 * big-endian to match the definition in PAPR.
132 */
133static struct ibm_pa_feature {
134 unsigned long cpu_features; /* CPU_FTR_xxx bit */
135 unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
136 unsigned char pabyte; /* byte number in ibm,pa-features */
137 unsigned char pabit; /* bit number (big-endian) */
138 unsigned char invert; /* if 1, pa bit set => clear feature */
139} ibm_pa_features[] __initdata = {
140 {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
141 {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
142 {CPU_FTR_SLB, 0, 0, 2, 0},
143 {CPU_FTR_CTRL, 0, 0, 3, 0},
144 {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
145 {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
146 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
Paul Mackerras339d76c2006-06-29 17:12:30 +1000147 {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
Paul Mackerrasd2058192006-05-03 23:04:37 +1000148};
149
Paul Mackerras974a76f2006-11-10 20:38:53 +1100150static void __init scan_features(unsigned long node, unsigned char *ftrs,
151 unsigned long tablelen,
152 struct ibm_pa_feature *fp,
153 unsigned long ft_size)
Paul Mackerrasd2058192006-05-03 23:04:37 +1000154{
Paul Mackerras974a76f2006-11-10 20:38:53 +1100155 unsigned long i, len, bit;
Paul Mackerrasd2058192006-05-03 23:04:37 +1000156
157 /* find descriptor with type == 0 */
158 for (;;) {
159 if (tablelen < 3)
160 return;
Paul Mackerras974a76f2006-11-10 20:38:53 +1100161 len = 2 + ftrs[0];
Paul Mackerrasd2058192006-05-03 23:04:37 +1000162 if (tablelen < len)
163 return; /* descriptor 0 not found */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100164 if (ftrs[1] == 0)
Paul Mackerrasd2058192006-05-03 23:04:37 +1000165 break;
166 tablelen -= len;
Paul Mackerras974a76f2006-11-10 20:38:53 +1100167 ftrs += len;
Paul Mackerrasd2058192006-05-03 23:04:37 +1000168 }
169
170 /* loop over bits we know about */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100171 for (i = 0; i < ft_size; ++i, ++fp) {
172 if (fp->pabyte >= ftrs[0])
Paul Mackerrasd2058192006-05-03 23:04:37 +1000173 continue;
Paul Mackerras974a76f2006-11-10 20:38:53 +1100174 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
Paul Mackerrasd2058192006-05-03 23:04:37 +1000175 if (bit ^ fp->invert) {
176 cur_cpu_spec->cpu_features |= fp->cpu_features;
177 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
178 } else {
179 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
180 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
181 }
182 }
183}
184
Paul Mackerras974a76f2006-11-10 20:38:53 +1100185static void __init check_cpu_pa_features(unsigned long node)
186{
187 unsigned char *pa_ftrs;
188 unsigned long tablelen;
189
190 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
191 if (pa_ftrs == NULL)
192 return;
193
194 scan_features(node, pa_ftrs, tablelen,
195 ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
196}
197
Benjamin Herrenschmidt94491682009-06-02 21:17:45 +0000198#ifdef CONFIG_PPC_STD_MMU_64
Michael Neuling584f8b72007-12-06 17:24:48 +1100199static void __init check_cpu_slb_size(unsigned long node)
200{
201 u32 *slb_size_ptr;
202
Michael Neulingb60c31d2009-01-14 13:42:41 +0000203 slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
204 if (slb_size_ptr != NULL) {
205 mmu_slb_size = *slb_size_ptr;
206 return;
207 }
Michael Neuling584f8b72007-12-06 17:24:48 +1100208 slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
209 if (slb_size_ptr != NULL) {
210 mmu_slb_size = *slb_size_ptr;
211 }
212}
213#else
214#define check_cpu_slb_size(node) do { } while(0)
215#endif
216
Paul Mackerras974a76f2006-11-10 20:38:53 +1100217static struct feature_property {
218 const char *name;
219 u32 min_value;
220 unsigned long cpu_feature;
221 unsigned long cpu_user_ftr;
222} feature_properties[] __initdata = {
223#ifdef CONFIG_ALTIVEC
224 {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
225 {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
226#endif /* CONFIG_ALTIVEC */
Michael Neulingb962ce92008-06-25 14:07:18 +1000227#ifdef CONFIG_VSX
228 /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
229 {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
230#endif /* CONFIG_VSX */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100231#ifdef CONFIG_PPC64
232 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
233 {"ibm,purr", 1, CPU_FTR_PURR, 0},
234 {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
235#endif /* CONFIG_PPC64 */
236};
237
Valentine Barshak14b3d922007-12-22 03:24:02 +1100238#if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
239static inline void identical_pvr_fixup(unsigned long node)
240{
241 unsigned int pvr;
242 char *model = of_get_flat_dt_prop(node, "model", NULL);
243
244 /*
245 * Since 440GR(x)/440EP(x) processors have the same pvr,
246 * we check the node path and set bit 28 in the cur_cpu_spec
247 * pvr for EP(x) processor version. This bit is always 0 in
248 * the "real" pvr. Then we call identify_cpu again with
249 * the new logical pvr to enable FPU support.
250 */
251 if (model && strstr(model, "440EP")) {
252 pvr = cur_cpu_spec->pvr_value | 0x8;
253 identify_cpu(0, pvr);
254 DBG("Using logical pvr %x for %s\n", pvr, model);
255 }
256}
257#else
258#define identical_pvr_fixup(node) do { } while(0)
259#endif
260
Paul Mackerras974a76f2006-11-10 20:38:53 +1100261static void __init check_cpu_feature_properties(unsigned long node)
262{
263 unsigned long i;
264 struct feature_property *fp = feature_properties;
265 const u32 *prop;
266
267 for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
268 prop = of_get_flat_dt_prop(node, fp->name, NULL);
269 if (prop && *prop >= fp->min_value) {
270 cur_cpu_spec->cpu_features |= fp->cpu_feature;
271 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
272 }
273 }
274}
275
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000276static int __init early_init_dt_scan_cpus(unsigned long node,
Anton Blanchard4df20462006-03-25 17:25:17 +1100277 const char *uname, int depth,
278 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000279{
Anton Blanchard4df20462006-03-25 17:25:17 +1100280 static int logical_cpuid = 0;
281 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Paul Mackerras974a76f2006-11-10 20:38:53 +1100282 const u32 *prop;
283 const u32 *intserv;
Anton Blanchard4df20462006-03-25 17:25:17 +1100284 int i, nthreads;
285 unsigned long len;
286 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000287
288 /* We are scanning "cpu" nodes only */
289 if (type == NULL || strcmp(type, "cpu") != 0)
290 return 0;
291
Anton Blanchard4df20462006-03-25 17:25:17 +1100292 /* Get physical cpuid */
293 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
294 if (intserv) {
295 nthreads = len / sizeof(int);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000296 } else {
Anton Blanchard4df20462006-03-25 17:25:17 +1100297 intserv = of_get_flat_dt_prop(node, "reg", NULL);
298 nthreads = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000299 }
Anton Blanchard4df20462006-03-25 17:25:17 +1100300
301 /*
302 * Now see if any of these threads match our boot cpu.
303 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
304 */
305 for (i = 0; i < nthreads; i++) {
306 /*
307 * version 2 of the kexec param format adds the phys cpuid of
308 * booted proc.
309 */
310 if (initial_boot_params && initial_boot_params->version >= 2) {
311 if (intserv[i] ==
312 initial_boot_params->boot_cpuid_phys) {
313 found = 1;
314 break;
315 }
316 } else {
317 /*
318 * Check if it's the boot-cpu, set it's hw index now,
319 * unfortunately this format did not support booting
320 * off secondary threads.
321 */
322 if (of_get_flat_dt_prop(node,
323 "linux,boot-cpu", NULL) != NULL) {
324 found = 1;
325 break;
326 }
327 }
328
329#ifdef CONFIG_SMP
330 /* logical cpu id is always 0 on UP kernels */
331 logical_cpuid++;
332#endif
333 }
334
335 if (found) {
336 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
337 intserv[i]);
338 boot_cpuid = logical_cpuid;
339 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
Paul Mackerras974a76f2006-11-10 20:38:53 +1100340
341 /*
342 * PAPR defines "logical" PVR values for cpus that
343 * meet various levels of the architecture:
344 * 0x0f000001 Architecture version 2.04
345 * 0x0f000002 Architecture version 2.05
346 * If the cpu-version property in the cpu node contains
347 * such a value, we call identify_cpu again with the
348 * logical PVR value in order to use the cpu feature
349 * bits appropriate for the architecture level.
350 *
351 * A POWER6 partition in "POWER6 architected" mode
352 * uses the 0x0f000002 PVR value; in POWER5+ mode
353 * it uses 0x0f000001.
354 */
355 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
356 if (prop && (*prop & 0xff000000) == 0x0f000000)
357 identify_cpu(0, *prop);
Valentine Barshak14b3d922007-12-22 03:24:02 +1100358
359 identical_pvr_fixup(node);
Anton Blanchard4df20462006-03-25 17:25:17 +1100360 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000361
Paul Mackerras974a76f2006-11-10 20:38:53 +1100362 check_cpu_feature_properties(node);
Paul Mackerrasd2058192006-05-03 23:04:37 +1000363 check_cpu_pa_features(node);
Michael Neuling584f8b72007-12-06 17:24:48 +1100364 check_cpu_slb_size(node);
Paul Mackerrasd2058192006-05-03 23:04:37 +1000365
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000366#ifdef CONFIG_PPC_PSERIES
Anton Blanchard4df20462006-03-25 17:25:17 +1100367 if (nthreads > 1)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000368 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
Anton Blanchard4df20462006-03-25 17:25:17 +1100369 else
370 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000371#endif
372
373 return 0;
374}
375
Michael Ellerman40472a52007-05-10 17:06:30 +1000376#ifdef CONFIG_BLK_DEV_INITRD
377static void __init early_init_dt_check_for_initrd(unsigned long node)
378{
379 unsigned long l;
380 u32 *prop;
381
382 DBG("Looking for initrd properties... ");
383
384 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
385 if (prop) {
386 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
387
388 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
389 if (prop) {
390 initrd_end = (unsigned long)
391 __va(of_read_ulong(prop, l/4));
392 initrd_below_start_ok = 1;
393 } else {
394 initrd_start = 0;
395 }
396 }
397
398 DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
399}
400#else
401static inline void early_init_dt_check_for_initrd(unsigned long node)
402{
403}
404#endif /* CONFIG_BLK_DEV_INITRD */
405
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000406static int __init early_init_dt_scan_chosen(unsigned long node,
407 const char *uname, int depth, void *data)
408{
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000409 unsigned long *lprop;
Kumar Gala329dda02006-02-24 10:54:52 -0600410 unsigned long l;
411 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000412
413 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
414
Paul Mackerrasa575b802005-10-23 17:23:21 +1000415 if (depth != 1 ||
416 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000417 return 0;
418
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000419#ifdef CONFIG_PPC64
420 /* check if iommu is forced on or off */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100421 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000422 iommu_is_off = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100423 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000424 iommu_force_on = 1;
425#endif
426
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000427 /* mem=x on the command line is the preferred mechanism */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100428 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000429 if (lprop)
430 memory_limit = *lprop;
431
432#ifdef CONFIG_PPC64
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100433 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000434 if (lprop)
435 tce_alloc_start = *lprop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100436 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000437 if (lprop)
438 tce_alloc_end = *lprop;
439#endif
440
Michael Ellermandcee3032005-12-04 18:39:48 +1100441#ifdef CONFIG_KEXEC
Stephen Rothwell63277162009-01-06 13:54:25 +0000442 lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
Linas Vepstas70c6cc32007-09-07 03:46:15 +1000443 if (lprop)
444 crashk_res.start = *lprop;
Michael Ellermandcee3032005-12-04 18:39:48 +1100445
Stephen Rothwell63277162009-01-06 13:54:25 +0000446 lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
Linas Vepstas70c6cc32007-09-07 03:46:15 +1000447 if (lprop)
448 crashk_res.end = crashk_res.start + *lprop - 1;
Michael Ellermandcee3032005-12-04 18:39:48 +1100449#endif
450
Michael Ellerman40472a52007-05-10 17:06:30 +1000451 early_init_dt_check_for_initrd(node);
David Gibson30437b32007-02-28 14:12:29 +1100452
Kumar Gala329dda02006-02-24 10:54:52 -0600453 /* Retreive command line */
454 p = of_get_flat_dt_prop(node, "bootargs", &l);
455 if (p != NULL && l > 0)
456 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
457
458#ifdef CONFIG_CMDLINE
Geoff Levandc1ce4642006-10-05 11:35:16 -0700459 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
Kumar Gala329dda02006-02-24 10:54:52 -0600460 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
461#endif /* CONFIG_CMDLINE */
462
463 DBG("Command line is: %s\n", cmd_line);
464
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000465 /* break now */
466 return 1;
467}
468
469static int __init early_init_dt_scan_root(unsigned long node,
470 const char *uname, int depth, void *data)
471{
472 u32 *prop;
473
474 if (depth != 0)
475 return 0;
476
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100477 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000478 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
479 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
480
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100481 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000482 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
483 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
484
485 /* break now */
486 return 1;
487}
488
Becky Bruceabe76882008-02-16 05:17:14 +1100489static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000490{
491 cell_t *p = *cellp;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000492
Paul Mackerrasa4dc7ff2006-09-19 14:06:27 +1000493 *cellp = p + s;
Becky Bruceabe76882008-02-16 05:17:14 +1100494 return of_read_number(p, s);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000495}
496
Paul Mackerras02045682006-11-29 22:27:42 +1100497#ifdef CONFIG_PPC_PSERIES
498/*
499 * Interpret the ibm,dynamic-memory property in the
500 * /ibm,dynamic-reconfiguration-memory node.
501 * This contains a list of memory blocks along with NUMA affinity
502 * information.
503 */
504static int __init early_init_dt_scan_drconf_memory(unsigned long node)
505{
Chandrucf000852008-08-30 00:28:16 +1000506 cell_t *dm, *ls, *usm;
Becky Bruceabe76882008-02-16 05:17:14 +1100507 unsigned long l, n, flags;
508 u64 base, size, lmb_size;
Chandrucf000852008-08-30 00:28:16 +1000509 unsigned int is_kexec_kdump = 0, rngs;
Paul Mackerras02045682006-11-29 22:27:42 +1100510
Stephen Rothwell63277162009-01-06 13:54:25 +0000511 ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
Paul Mackerras02045682006-11-29 22:27:42 +1100512 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
513 return 0;
514 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
515
Stephen Rothwell63277162009-01-06 13:54:25 +0000516 dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
Paul Mackerras02045682006-11-29 22:27:42 +1100517 if (dm == NULL || l < sizeof(cell_t))
518 return 0;
519
520 n = *dm++; /* number of entries */
521 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
522 return 0;
523
Chandrucf000852008-08-30 00:28:16 +1000524 /* check if this is a kexec/kdump kernel. */
Stephen Rothwell63277162009-01-06 13:54:25 +0000525 usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
Chandrucf000852008-08-30 00:28:16 +1000526 &l);
527 if (usm != NULL)
528 is_kexec_kdump = 1;
529
Paul Mackerras02045682006-11-29 22:27:42 +1100530 for (; n != 0; --n) {
531 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
532 flags = dm[3];
533 /* skip DRC index, pad, assoc. list index, flags */
534 dm += 4;
535 /* skip this block if the reserved bit is set in flags (0x80)
536 or if the block is not assigned to this partition (0x8) */
537 if ((flags & 0x80) || !(flags & 0x8))
538 continue;
539 size = lmb_size;
Chandrucf000852008-08-30 00:28:16 +1000540 rngs = 1;
541 if (is_kexec_kdump) {
542 /*
543 * For each lmb in ibm,dynamic-memory, a corresponding
544 * entry in linux,drconf-usable-memory property contains
545 * a counter 'p' followed by 'p' (base, size) duple.
546 * Now read the counter from
547 * linux,drconf-usable-memory property
548 */
549 rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
550 if (!rngs) /* there are no (base, size) duple */
Paul Mackerras02045682006-11-29 22:27:42 +1100551 continue;
Paul Mackerras02045682006-11-29 22:27:42 +1100552 }
Chandrucf000852008-08-30 00:28:16 +1000553 do {
554 if (is_kexec_kdump) {
555 base = dt_mem_next_cell(dt_root_addr_cells,
556 &usm);
557 size = dt_mem_next_cell(dt_root_size_cells,
558 &usm);
559 }
560 if (iommu_is_off) {
561 if (base >= 0x80000000ul)
562 continue;
563 if ((base + size) > 0x80000000ul)
564 size = 0x80000000ul - base;
565 }
566 lmb_add(base, size);
567 } while (--rngs);
Paul Mackerras02045682006-11-29 22:27:42 +1100568 }
569 lmb_dump_all();
570 return 0;
571}
572#else
573#define early_init_dt_scan_drconf_memory(node) 0
574#endif /* CONFIG_PPC_PSERIES */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000575
576static int __init early_init_dt_scan_memory(unsigned long node,
577 const char *uname, int depth, void *data)
578{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100579 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000580 cell_t *reg, *endp;
581 unsigned long l;
582
Paul Mackerras02045682006-11-29 22:27:42 +1100583 /* Look for the ibm,dynamic-reconfiguration-memory node */
584 if (depth == 1 &&
585 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
586 return early_init_dt_scan_drconf_memory(node);
587
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000588 /* We are scanning "memory" nodes only */
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100589 if (type == NULL) {
590 /*
591 * The longtrail doesn't have a device_type on the
592 * /memory node, so look for the node called /memory@0.
593 */
594 if (depth != 1 || strcmp(uname, "memory@0") != 0)
595 return 0;
596 } else if (strcmp(type, "memory") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000597 return 0;
598
Stephen Rothwell63277162009-01-06 13:54:25 +0000599 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
Michael Ellermanba759482005-12-04 18:39:55 +1100600 if (reg == NULL)
Stephen Rothwell63277162009-01-06 13:54:25 +0000601 reg = of_get_flat_dt_prop(node, "reg", &l);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000602 if (reg == NULL)
603 return 0;
604
605 endp = reg + (l / sizeof(cell_t));
606
Michael Ellerman358c86f2005-11-03 15:39:09 +1100607 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000608 uname, l, reg[0], reg[1], reg[2], reg[3]);
609
610 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
Becky Bruceabe76882008-02-16 05:17:14 +1100611 u64 base, size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000612
613 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
614 size = dt_mem_next_cell(dt_root_size_cells, &reg);
615
616 if (size == 0)
617 continue;
Becky Bruceabe76882008-02-16 05:17:14 +1100618 DBG(" - %llx , %llx\n", (unsigned long long)base,
619 (unsigned long long)size);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000620#ifdef CONFIG_PPC64
621 if (iommu_is_off) {
622 if (base >= 0x80000000ul)
623 continue;
624 if ((base + size) > 0x80000000ul)
625 size = 0x80000000ul - base;
626 }
627#endif
628 lmb_add(base, size);
Kumar Gala37dd2ba2008-04-22 04:22:34 +1000629
630 memstart_addr = min((u64)memstart_addr, base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000631 }
Kumar Gala37dd2ba2008-04-22 04:22:34 +1000632
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000633 return 0;
634}
635
636static void __init early_reserve_mem(void)
637{
Kumar Galacbbcf342006-01-11 17:57:13 -0600638 u64 base, size;
639 u64 *reserve_map;
Jon Loeliger8a300882006-06-17 17:51:09 -0500640 unsigned long self_base;
641 unsigned long self_size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000642
Kumar Galacbbcf342006-01-11 17:57:13 -0600643 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000644 initial_boot_params->off_mem_rsvmap);
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -0500645
646 /* before we do anything, lets reserve the dt blob */
Jon Loeliger8a300882006-06-17 17:51:09 -0500647 self_base = __pa((unsigned long)initial_boot_params);
648 self_size = initial_boot_params->totalsize;
649 lmb_reserve(self_base, self_size);
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -0500650
David Gibson30437b32007-02-28 14:12:29 +1100651#ifdef CONFIG_BLK_DEV_INITRD
652 /* then reserve the initrd, if any */
653 if (initrd_start && (initrd_end > initrd_start))
654 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
655#endif /* CONFIG_BLK_DEV_INITRD */
656
Kumar Galacbbcf342006-01-11 17:57:13 -0600657#ifdef CONFIG_PPC32
658 /*
659 * Handle the case where we might be booting from an old kexec
660 * image that setup the mem_rsvmap as pairs of 32-bit values
661 */
662 if (*reserve_map > 0xffffffffull) {
663 u32 base_32, size_32;
664 u32 *reserve_map_32 = (u32 *)reserve_map;
665
666 while (1) {
667 base_32 = *(reserve_map_32++);
668 size_32 = *(reserve_map_32++);
669 if (size_32 == 0)
670 break;
Jon Loeliger8a300882006-06-17 17:51:09 -0500671 /* skip if the reservation is for the blob */
672 if (base_32 == self_base && size_32 == self_size)
673 continue;
Kumar Gala329dda02006-02-24 10:54:52 -0600674 DBG("reserving: %x -> %x\n", base_32, size_32);
Kumar Galacbbcf342006-01-11 17:57:13 -0600675 lmb_reserve(base_32, size_32);
676 }
677 return;
678 }
679#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000680 while (1) {
681 base = *(reserve_map++);
682 size = *(reserve_map++);
683 if (size == 0)
684 break;
Kumar Galacbbcf342006-01-11 17:57:13 -0600685 DBG("reserving: %llx -> %llx\n", base, size);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000686 lmb_reserve(base, size);
687 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000688}
689
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100690#ifdef CONFIG_PHYP_DUMP
691/**
Manish Ahuja37ddd5d2008-04-12 09:31:52 +1000692 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
693 *
694 * Function to find the largest size we need to reserve
695 * during early boot process.
696 *
697 * It either looks for boot param and returns that OR
698 * returns larger of 256 or 5% rounded down to multiples of 256MB.
699 *
700 */
701static inline unsigned long phyp_dump_calculate_reserve_size(void)
702{
703 unsigned long tmp;
704
705 if (phyp_dump_info->reserve_bootvar)
706 return phyp_dump_info->reserve_bootvar;
707
708 /* divide by 20 to get 5% of value */
709 tmp = lmb_end_of_DRAM();
710 do_div(tmp, 20);
711
712 /* round it down in multiples of 256 */
713 tmp = tmp & ~0x0FFFFFFFUL;
714
715 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
716}
717
718/**
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100719 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
720 *
721 * This routine may reserve memory regions in the kernel only
722 * if the system is supported and a dump was taken in last
723 * boot instance or if the hardware is supported and the
724 * scratch area needs to be setup. In other instances it returns
725 * without reserving anything. The memory in case of dump being
726 * active is freed when the dump is collected (by userland tools).
727 */
728static void __init phyp_dump_reserve_mem(void)
729{
730 unsigned long base, size;
Manish Ahuja37ddd5d2008-04-12 09:31:52 +1000731 unsigned long variable_reserve_size;
732
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100733 if (!phyp_dump_info->phyp_dump_configured) {
734 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
735 return;
736 }
737
Manish Ahuja654f5962008-03-22 11:38:59 +1100738 if (!phyp_dump_info->phyp_dump_at_boot) {
739 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
740 return;
741 }
742
Manish Ahuja37ddd5d2008-04-12 09:31:52 +1000743 variable_reserve_size = phyp_dump_calculate_reserve_size();
744
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100745 if (phyp_dump_info->phyp_dump_is_active) {
746 /* Reserve *everything* above RMR.Area freed by userland tools*/
Manish Ahuja37ddd5d2008-04-12 09:31:52 +1000747 base = variable_reserve_size;
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100748 size = lmb_end_of_DRAM() - base;
749
750 /* XXX crashed_ram_end is wrong, since it may be beyond
751 * the memory_limit, it will need to be adjusted. */
752 lmb_reserve(base, size);
753
754 phyp_dump_info->init_reserve_start = base;
755 phyp_dump_info->init_reserve_size = size;
756 } else {
757 size = phyp_dump_info->cpu_state_size +
758 phyp_dump_info->hpte_region_size +
Manish Ahuja37ddd5d2008-04-12 09:31:52 +1000759 variable_reserve_size;
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100760 base = lmb_end_of_DRAM() - size;
761 lmb_reserve(base, size);
762 phyp_dump_info->init_reserve_start = base;
763 phyp_dump_info->init_reserve_size = size;
764 }
765}
766#else
767static inline void __init phyp_dump_reserve_mem(void) {}
768#endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
769
770
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000771void __init early_init_devtree(void *params)
772{
Becky Bruce49a84962009-05-08 12:19:27 +0000773 phys_addr_t limit;
Hollis Blanchard6ca4f742008-11-26 10:19:26 -0600774
Geoff Levand44348102007-06-16 08:06:14 +1000775 DBG(" -> early_init_devtree(%p)\n", params);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000776
777 /* Setup flat device-tree pointer */
778 initial_boot_params = params;
779
Michael Ellerman458148c2006-06-23 18:20:13 +1000780#ifdef CONFIG_PPC_RTAS
781 /* Some machines might need RTAS info for debugging, grab it now. */
782 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
783#endif
784
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100785#ifdef CONFIG_PHYP_DUMP
786 /* scan tree to see if dump occured during last boot */
787 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
788#endif
789
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000790 /* Retrieve various informations from the /chosen node of the
791 * device-tree, including the platform type, initrd location and
792 * size, TCE reserve, and more ...
793 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100794 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000795
796 /* Scan memory nodes and rebuild LMBs */
797 lmb_init();
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100798 of_scan_flat_dt(early_init_dt_scan_root, NULL);
799 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Michael Ellerman846f77b2006-05-17 18:00:45 +1000800
801 /* Save command line for /proc/cmdline and then parse parameters */
Alon Bar-Levb8757b22007-02-12 00:54:17 -0800802 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
Michael Ellerman846f77b2006-05-17 18:00:45 +1000803 parse_early_param();
804
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000805 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
Michael Ellerman0cc47462005-12-04 18:39:37 +1100806 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
Paul Mackerras549e8152008-08-30 11:43:47 +1000807 /* If relocatable, reserve first 32k for interrupt vectors etc. */
808 if (PHYSICAL_START > MEMORY_START)
809 lmb_reserve(MEMORY_START, 0x8000);
Michael Ellerman47310412006-05-17 18:00:49 +1000810 reserve_kdump_trampoline();
Michael Ellerman35dd5432006-05-18 11:16:11 +1000811 reserve_crashkernel();
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000812 early_reserve_mem();
Manish Ahuja6ac26c82008-03-22 10:37:08 +1100813 phyp_dump_reserve_mem();
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000814
Hollis Blanchard6ca4f742008-11-26 10:19:26 -0600815 limit = memory_limit;
816 if (! limit) {
Becky Bruce49a84962009-05-08 12:19:27 +0000817 phys_addr_t memsize;
Hollis Blanchard6ca4f742008-11-26 10:19:26 -0600818
819 /* Ensure that total memory size is page-aligned, because
820 * otherwise mark_bootmem() gets upset. */
821 lmb_analyze();
822 memsize = lmb_phys_mem_size();
823 if ((memsize & PAGE_MASK) != memsize)
824 limit = memsize & PAGE_MASK;
825 }
826 lmb_enforce_memory_limit(limit);
827
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000828 lmb_analyze();
Michael Ellerman059f1342009-01-14 20:46:01 +0000829 lmb_dump_all();
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000830
Becky Bruce49a84962009-05-08 12:19:27 +0000831 DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000832
833 /* We may need to relocate the flat tree, do it now.
834 * FIXME .. and the initrd too? */
835 move_device_tree();
836
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000837 DBG("Scanning CPUs ...\n");
838
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100839 /* Retreive CPU related informations from the flat tree
840 * (altivec support, boot CPU ID, ...)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000841 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100842 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000843
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000844 DBG(" <- early_init_devtree()\n");
845}
846
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000847
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000848/**
849 * Indicates whether the root node has a given value in its
850 * compatible property.
851 */
852int machine_is_compatible(const char *compat)
853{
854 struct device_node *root;
855 int rc = 0;
856
857 root = of_find_node_by_path("/");
858 if (root) {
Stephen Rothwell7a92f742007-04-03 10:55:39 +1000859 rc = of_device_is_compatible(root, compat);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000860 of_node_put(root);
861 }
862 return rc;
863}
864EXPORT_SYMBOL(machine_is_compatible);
865
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000866/*******
867 *
868 * New implementation of the OF "find" APIs, return a refcounted
869 * object, call of_node_put() when done. The device tree and list
870 * are protected by a rw_lock.
871 *
872 * Note that property management will need some locking as well,
873 * this isn't dealt with yet.
874 *
875 *******/
876
877/**
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000878 * of_find_node_by_phandle - Find a node given a phandle
879 * @handle: phandle of the node to find
880 *
881 * Returns a node pointer with refcount incremented, use
882 * of_node_put() on it when done.
883 */
884struct device_node *of_find_node_by_phandle(phandle handle)
885{
886 struct device_node *np;
887
888 read_lock(&devtree_lock);
889 for (np = allnodes; np != 0; np = np->allnext)
890 if (np->linux_phandle == handle)
891 break;
Mariusz Kozlowskib1374052007-01-02 12:31:47 +0100892 of_node_get(np);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000893 read_unlock(&devtree_lock);
894 return np;
895}
896EXPORT_SYMBOL(of_find_node_by_phandle);
897
898/**
Nathan Lynche523f722008-12-10 14:46:04 +0000899 * of_find_next_cache_node - Find a node's subsidiary cache
900 * @np: node of type "cpu" or "cache"
901 *
902 * Returns a node pointer with refcount incremented, use
903 * of_node_put() on it when done. Caller should hold a reference
904 * to np.
905 */
906struct device_node *of_find_next_cache_node(struct device_node *np)
907{
908 struct device_node *child;
909 const phandle *handle;
910
911 handle = of_get_property(np, "l2-cache", NULL);
912 if (!handle)
913 handle = of_get_property(np, "next-level-cache", NULL);
914
915 if (handle)
916 return of_find_node_by_phandle(*handle);
917
918 /* OF on pmac has nodes instead of properties named "l2-cache"
919 * beneath CPU nodes.
920 */
921 if (!strcmp(np->type, "cpu"))
922 for_each_child_of_node(np, child)
923 if (!strcmp(child->type, "cache"))
924 return child;
925
926 return NULL;
927}
928
929/**
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000930 * of_node_get - Increment refcount of a node
931 * @node: Node to inc refcount, NULL is supported to
932 * simplify writing of callers
933 *
934 * Returns node.
935 */
936struct device_node *of_node_get(struct device_node *node)
937{
938 if (node)
939 kref_get(&node->kref);
940 return node;
941}
942EXPORT_SYMBOL(of_node_get);
943
944static inline struct device_node * kref_to_device_node(struct kref *kref)
945{
946 return container_of(kref, struct device_node, kref);
947}
948
949/**
950 * of_node_release - release a dynamically allocated node
951 * @kref: kref element of the node to be released
952 *
953 * In of_node_put() this function is passed to kref_put()
954 * as the destructor.
955 */
956static void of_node_release(struct kref *kref)
957{
958 struct device_node *node = kref_to_device_node(kref);
959 struct property *prop = node->properties;
960
Michael Ellerman6a2818562007-06-19 16:08:00 +1000961 /* We should never be releasing nodes that haven't been detached. */
962 if (!of_node_check_flag(node, OF_DETACHED)) {
963 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
964 dump_stack();
965 kref_init(&node->kref);
966 return;
967 }
968
Michael Ellermand3b814b2007-06-19 16:07:58 +1000969 if (!of_node_check_flag(node, OF_DYNAMIC))
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000970 return;
Michael Ellerman6a2818562007-06-19 16:08:00 +1000971
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000972 while (prop) {
973 struct property *next = prop->next;
974 kfree(prop->name);
975 kfree(prop->value);
976 kfree(prop);
977 prop = next;
Dave C Boutcher088186d2006-01-12 16:08:27 -0600978
979 if (!prop) {
980 prop = node->deadprops;
981 node->deadprops = NULL;
982 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000983 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000984 kfree(node->full_name);
985 kfree(node->data);
986 kfree(node);
987}
988
989/**
990 * of_node_put - Decrement refcount of a node
991 * @node: Node to dec refcount, NULL is supported to
992 * simplify writing of callers
993 *
994 */
995void of_node_put(struct device_node *node)
996{
997 if (node)
998 kref_put(&node->kref, of_node_release);
999}
1000EXPORT_SYMBOL(of_node_put);
1001
1002/*
1003 * Plug a device node into the tree and global list.
1004 */
1005void of_attach_node(struct device_node *np)
1006{
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001007 unsigned long flags;
1008
1009 write_lock_irqsave(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001010 np->sibling = np->parent->child;
1011 np->allnext = allnodes;
1012 np->parent->child = np;
1013 allnodes = np;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001014 write_unlock_irqrestore(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001015}
1016
1017/*
1018 * "Unplug" a node from the device tree. The caller must hold
1019 * a reference to the node. The memory associated with the node
1020 * is not freed until its refcount goes to zero.
1021 */
Segher Boessenkool34f329d2007-07-20 15:58:38 +10001022void of_detach_node(struct device_node *np)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001023{
1024 struct device_node *parent;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001025 unsigned long flags;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001026
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001027 write_lock_irqsave(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001028
1029 parent = np->parent;
Michael Ellerman972d17c2007-06-19 16:07:56 +10001030 if (!parent)
1031 goto out_unlock;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001032
1033 if (allnodes == np)
1034 allnodes = np->allnext;
1035 else {
1036 struct device_node *prev;
1037 for (prev = allnodes;
1038 prev->allnext != np;
1039 prev = prev->allnext)
1040 ;
1041 prev->allnext = np->allnext;
1042 }
1043
1044 if (parent->child == np)
1045 parent->child = np->sibling;
1046 else {
1047 struct device_node *prevsib;
1048 for (prevsib = np->parent->child;
1049 prevsib->sibling != np;
1050 prevsib = prevsib->sibling)
1051 ;
1052 prevsib->sibling = np->sibling;
1053 }
1054
Michael Ellerman6a2818562007-06-19 16:08:00 +10001055 of_node_set_flag(np, OF_DETACHED);
1056
Michael Ellerman972d17c2007-06-19 16:07:56 +10001057out_unlock:
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001058 write_unlock_irqrestore(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001059}
1060
1061#ifdef CONFIG_PPC_PSERIES
1062/*
1063 * Fix up the uninitialized fields in a new device node:
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001064 * name, type and pci-specific fields
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001065 */
1066
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001067static int of_finish_dynamic_node(struct device_node *node)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001068{
1069 struct device_node *parent = of_get_parent(node);
1070 int err = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001071 const phandle *ibm_phandle;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001072
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001073 node->name = of_get_property(node, "name", NULL);
1074 node->type = of_get_property(node, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001075
Benjamin Herrenschmidt847f5972007-05-16 16:57:24 +10001076 if (!node->name)
1077 node->name = "<NULL>";
1078 if (!node->type)
1079 node->type = "<NULL>";
1080
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001081 if (!parent) {
1082 err = -ENODEV;
1083 goto out;
1084 }
1085
1086 /* We don't support that function on PowerMac, at least
1087 * not yet
1088 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001089 if (machine_is(powermac))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001090 return -ENODEV;
1091
1092 /* fix up new node's linux_phandle field */
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001093 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001094 node->linux_phandle = *ibm_phandle;
1095
1096out:
1097 of_node_put(parent);
1098 return err;
1099}
1100
1101static int prom_reconfig_notifier(struct notifier_block *nb,
1102 unsigned long action, void *node)
1103{
1104 int err;
1105
1106 switch (action) {
1107 case PSERIES_RECONFIG_ADD:
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001108 err = of_finish_dynamic_node(node);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001109 if (err < 0) {
1110 printk(KERN_ERR "finish_node returned %d\n", err);
1111 err = NOTIFY_BAD;
1112 }
1113 break;
1114 default:
1115 err = NOTIFY_DONE;
1116 break;
1117 }
1118 return err;
1119}
1120
1121static struct notifier_block prom_reconfig_nb = {
1122 .notifier_call = prom_reconfig_notifier,
1123 .priority = 10, /* This one needs to run first */
1124};
1125
1126static int __init prom_reconfig_setup(void)
1127{
1128 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1129}
1130__initcall(prom_reconfig_setup);
1131#endif
1132
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001133/* Find the device node for a given logical cpu number, also returns the cpu
1134 * local thread number (index in ibm,interrupt-server#s) if relevant and
1135 * asked for (non NULL)
1136 */
1137struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1138{
1139 int hardid;
1140 struct device_node *np;
1141
1142 hardid = get_hard_smp_processor_id(cpu);
1143
1144 for_each_node_by_type(np, "cpu") {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001145 const u32 *intserv;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001146 unsigned int plen, t;
1147
1148 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1149 * fallback to "reg" property and assume no threads
1150 */
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001151 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001152 &plen);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001153 if (intserv == NULL) {
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001154 const u32 *reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001155 if (reg == NULL)
1156 continue;
1157 if (*reg == hardid) {
1158 if (thread)
1159 *thread = 0;
1160 return np;
1161 }
1162 } else {
1163 plen /= sizeof(u32);
1164 for (t = 0; t < plen; t++) {
1165 if (hardid == intserv[t]) {
1166 if (thread)
1167 *thread = t;
1168 return np;
1169 }
1170 }
1171 }
1172 }
1173 return NULL;
1174}
Christian Krafft36ca4ba2006-10-24 18:39:45 +02001175EXPORT_SYMBOL(of_get_cpu_node);
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001176
Michael Ellerman94a38072007-06-20 10:54:19 +10001177#if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001178static struct debugfs_blob_wrapper flat_dt_blob;
1179
1180static int __init export_flat_device_tree(void)
1181{
1182 struct dentry *d;
1183
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001184 flat_dt_blob.data = initial_boot_params;
1185 flat_dt_blob.size = initial_boot_params->totalsize;
1186
1187 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
Michael Ellerman94a38072007-06-20 10:54:19 +10001188 powerpc_debugfs_root, &flat_dt_blob);
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001189 if (!d)
1190 return 1;
1191
1192 return 0;
1193}
1194__initcall(export_flat_device_tree);
1195#endif