blob: 2aefe2a4129a7fc4bd563db0a92f5a8ded54801e [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
76#if 0
77static struct boot_param_header *initial_boot_params __initdata;
78#else
79struct boot_param_header *initial_boot_params;
80#endif
81
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100082extern struct device_node *allnodes; /* temporary while merging */
Paul Mackerras9b6b5632005-10-06 12:06:20 +100083
Stephen Rothwell581b6052007-04-24 16:46:53 +100084extern rwlock_t devtree_lock; /* temporary while merging */
Paul Mackerras9b6b5632005-10-06 12:06:20 +100085
86/* export that to outside world */
87struct device_node *of_chosen;
88
Paul Mackerras9b6b5632005-10-06 12:06:20 +100089static inline char *find_flat_dt_string(u32 offset)
90{
91 return ((char *)initial_boot_params) +
92 initial_boot_params->off_dt_strings + offset;
93}
94
95/**
96 * This function is used to scan the flattened device-tree, it is
97 * used to extract the memory informations at boot before we can
98 * unflatten the tree
99 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100100int __init of_scan_flat_dt(int (*it)(unsigned long node,
101 const char *uname, int depth,
102 void *data),
103 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000104{
105 unsigned long p = ((unsigned long)initial_boot_params) +
106 initial_boot_params->off_dt_struct;
107 int rc = 0;
108 int depth = -1;
109
110 do {
111 u32 tag = *((u32 *)p);
112 char *pathp;
113
114 p += 4;
115 if (tag == OF_DT_END_NODE) {
116 depth --;
117 continue;
118 }
119 if (tag == OF_DT_NOP)
120 continue;
121 if (tag == OF_DT_END)
122 break;
123 if (tag == OF_DT_PROP) {
124 u32 sz = *((u32 *)p);
125 p += 8;
126 if (initial_boot_params->version < 0x10)
127 p = _ALIGN(p, sz >= 8 ? 8 : 4);
128 p += sz;
129 p = _ALIGN(p, 4);
130 continue;
131 }
132 if (tag != OF_DT_BEGIN_NODE) {
133 printk(KERN_WARNING "Invalid tag %x scanning flattened"
134 " device tree !\n", tag);
135 return -EINVAL;
136 }
137 depth++;
138 pathp = (char *)p;
139 p = _ALIGN(p + strlen(pathp) + 1, 4);
140 if ((*pathp) == '/') {
141 char *lp, *np;
142 for (lp = NULL, np = pathp; *np; np++)
143 if ((*np) == '/')
144 lp = np+1;
145 if (lp != NULL)
146 pathp = lp;
147 }
148 rc = it(p, pathp, depth, data);
149 if (rc != 0)
150 break;
151 } while(1);
152
153 return rc;
154}
155
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100156unsigned long __init of_get_flat_dt_root(void)
157{
158 unsigned long p = ((unsigned long)initial_boot_params) +
159 initial_boot_params->off_dt_struct;
160
161 while(*((u32 *)p) == OF_DT_NOP)
162 p += 4;
163 BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
164 p += 4;
165 return _ALIGN(p + strlen((char *)p) + 1, 4);
166}
167
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000168/**
169 * This function can be used within scan_flattened_dt callback to get
170 * access to properties
171 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100172void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
173 unsigned long *size)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000174{
175 unsigned long p = node;
176
177 do {
178 u32 tag = *((u32 *)p);
179 u32 sz, noff;
180 const char *nstr;
181
182 p += 4;
183 if (tag == OF_DT_NOP)
184 continue;
185 if (tag != OF_DT_PROP)
186 return NULL;
187
188 sz = *((u32 *)p);
189 noff = *((u32 *)(p + 4));
190 p += 8;
191 if (initial_boot_params->version < 0x10)
192 p = _ALIGN(p, sz >= 8 ? 8 : 4);
193
194 nstr = find_flat_dt_string(noff);
195 if (nstr == NULL) {
196 printk(KERN_WARNING "Can't find property index"
197 " name !\n");
198 return NULL;
199 }
200 if (strcmp(name, nstr) == 0) {
201 if (size)
202 *size = sz;
203 return (void *)p;
204 }
205 p += sz;
206 p = _ALIGN(p, 4);
207 } while(1);
208}
209
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100210int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
211{
212 const char* cp;
213 unsigned long cplen, l;
214
215 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
216 if (cp == NULL)
217 return 0;
218 while (cplen > 0) {
219 if (strncasecmp(cp, compat, strlen(compat)) == 0)
220 return 1;
221 l = strlen(cp) + 1;
222 cp += l;
223 cplen -= l;
224 }
225
226 return 0;
227}
228
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000229static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
230 unsigned long align)
231{
232 void *res;
233
234 *mem = _ALIGN(*mem, align);
235 res = (void *)*mem;
236 *mem += size;
237
238 return res;
239}
240
241static unsigned long __init unflatten_dt_node(unsigned long mem,
242 unsigned long *p,
243 struct device_node *dad,
244 struct device_node ***allnextpp,
245 unsigned long fpsize)
246{
247 struct device_node *np;
248 struct property *pp, **prev_pp = NULL;
249 char *pathp;
250 u32 tag;
251 unsigned int l, allocl;
252 int has_name = 0;
253 int new_format = 0;
254
255 tag = *((u32 *)(*p));
256 if (tag != OF_DT_BEGIN_NODE) {
257 printk("Weird tag at start of node: %x\n", tag);
258 return mem;
259 }
260 *p += 4;
261 pathp = (char *)*p;
262 l = allocl = strlen(pathp) + 1;
263 *p = _ALIGN(*p + l, 4);
264
265 /* version 0x10 has a more compact unit name here instead of the full
266 * path. we accumulate the full path size using "fpsize", we'll rebuild
267 * it later. We detect this because the first character of the name is
268 * not '/'.
269 */
270 if ((*pathp) != '/') {
271 new_format = 1;
272 if (fpsize == 0) {
273 /* root node: special case. fpsize accounts for path
274 * plus terminating zero. root node only has '/', so
275 * fpsize should be 2, but we want to avoid the first
276 * level nodes to have two '/' so we use fpsize 1 here
277 */
278 fpsize = 1;
279 allocl = 2;
280 } else {
281 /* account for '/' and path size minus terminal 0
282 * already in 'l'
283 */
284 fpsize += l;
285 allocl = fpsize;
286 }
287 }
288
289
290 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
291 __alignof__(struct device_node));
292 if (allnextpp) {
293 memset(np, 0, sizeof(*np));
294 np->full_name = ((char*)np) + sizeof(struct device_node);
295 if (new_format) {
296 char *p = np->full_name;
297 /* rebuild full path for new format */
298 if (dad && dad->parent) {
299 strcpy(p, dad->full_name);
300#ifdef DEBUG
301 if ((strlen(p) + l + 1) != allocl) {
302 DBG("%s: p: %d, l: %d, a: %d\n",
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100303 pathp, (int)strlen(p), l, allocl);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000304 }
305#endif
306 p += strlen(p);
307 }
308 *(p++) = '/';
309 memcpy(p, pathp, l);
310 } else
311 memcpy(np->full_name, pathp, l);
312 prev_pp = &np->properties;
313 **allnextpp = np;
314 *allnextpp = &np->allnext;
315 if (dad != NULL) {
316 np->parent = dad;
317 /* we temporarily use the next field as `last_child'*/
318 if (dad->next == 0)
319 dad->child = np;
320 else
321 dad->next->sibling = np;
322 dad->next = np;
323 }
324 kref_init(&np->kref);
325 }
326 while(1) {
327 u32 sz, noff;
328 char *pname;
329
330 tag = *((u32 *)(*p));
331 if (tag == OF_DT_NOP) {
332 *p += 4;
333 continue;
334 }
335 if (tag != OF_DT_PROP)
336 break;
337 *p += 4;
338 sz = *((u32 *)(*p));
339 noff = *((u32 *)((*p) + 4));
340 *p += 8;
341 if (initial_boot_params->version < 0x10)
342 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
343
344 pname = find_flat_dt_string(noff);
345 if (pname == NULL) {
346 printk("Can't find property name in list !\n");
347 break;
348 }
349 if (strcmp(pname, "name") == 0)
350 has_name = 1;
351 l = strlen(pname) + 1;
352 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
353 __alignof__(struct property));
354 if (allnextpp) {
355 if (strcmp(pname, "linux,phandle") == 0) {
356 np->node = *((u32 *)*p);
357 if (np->linux_phandle == 0)
358 np->linux_phandle = np->node;
359 }
360 if (strcmp(pname, "ibm,phandle") == 0)
361 np->linux_phandle = *((u32 *)*p);
362 pp->name = pname;
363 pp->length = sz;
364 pp->value = (void *)*p;
365 *prev_pp = pp;
366 prev_pp = &pp->next;
367 }
368 *p = _ALIGN((*p) + sz, 4);
369 }
370 /* with version 0x10 we may not have the name property, recreate
371 * it here from the unit name if absent
372 */
373 if (!has_name) {
374 char *p = pathp, *ps = pathp, *pa = NULL;
375 int sz;
376
377 while (*p) {
378 if ((*p) == '@')
379 pa = p;
380 if ((*p) == '/')
381 ps = p + 1;
382 p++;
383 }
384 if (pa < ps)
385 pa = p;
386 sz = (pa - ps) + 1;
387 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
388 __alignof__(struct property));
389 if (allnextpp) {
390 pp->name = "name";
391 pp->length = sz;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000392 pp->value = pp + 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000393 *prev_pp = pp;
394 prev_pp = &pp->next;
395 memcpy(pp->value, ps, sz - 1);
396 ((char *)pp->value)[sz - 1] = 0;
Stephen Rothwell1a381472007-04-03 10:58:52 +1000397 DBG("fixed up name for %s -> %s\n", pathp,
398 (char *)pp->value);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000399 }
400 }
401 if (allnextpp) {
402 *prev_pp = NULL;
Stephen Rothwell0e56efc2007-04-03 10:54:01 +1000403 np->name = of_get_property(np, "name", NULL);
404 np->type = of_get_property(np, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000405
406 if (!np->name)
407 np->name = "<NULL>";
408 if (!np->type)
409 np->type = "<NULL>";
410 }
411 while (tag == OF_DT_BEGIN_NODE) {
412 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
413 tag = *((u32 *)(*p));
414 }
415 if (tag != OF_DT_END_NODE) {
416 printk("Weird tag at end of node: %x\n", tag);
417 return mem;
418 }
419 *p += 4;
420 return mem;
421}
422
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000423static int __init early_parse_mem(char *p)
424{
425 if (!p)
426 return 1;
427
428 memory_limit = PAGE_ALIGN(memparse(p, &p));
429 DBG("memory limit = 0x%lx\n", memory_limit);
430
431 return 0;
432}
433early_param("mem", early_parse_mem);
434
Linas Vepstas3c607ce2007-09-07 03:47:29 +1000435/**
436 * move_device_tree - move tree to an unused area, if needed.
437 *
438 * The device tree may be allocated beyond our memory limit, or inside the
439 * crash kernel region for kdump. If so, move it out of the way.
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000440 */
Geert Uytterhoeven18f032c2008-03-29 03:07:45 +1100441static void __init move_device_tree(void)
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000442{
443 unsigned long start, size;
444 void *p;
445
446 DBG("-> move_device_tree\n");
447
448 start = __pa(initial_boot_params);
449 size = initial_boot_params->totalsize;
450
451 if ((memory_limit && (start + size) > memory_limit) ||
452 overlaps_crashkernel(start, size)) {
453 p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
454 memcpy(p, initial_boot_params, size);
455 initial_boot_params = (struct boot_param_header *)p;
456 DBG("Moved device tree to 0x%p\n", p);
457 }
458
459 DBG("<- move_device_tree\n");
460}
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000461
462/**
463 * unflattens the device-tree passed by the firmware, creating the
464 * tree of struct device_node. It also fills the "name" and "type"
465 * pointers of the nodes so the normal device-tree walking functions
466 * can be used (this used to be done by finish_device_tree)
467 */
468void __init unflatten_device_tree(void)
469{
470 unsigned long start, mem, size;
471 struct device_node **allnextp = &allnodes;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000472
473 DBG(" -> unflatten_device_tree()\n");
474
475 /* First pass, scan for size */
476 start = ((unsigned long)initial_boot_params) +
477 initial_boot_params->off_dt_struct;
478 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
479 size = (size | 3) + 1;
480
481 DBG(" size is %lx, allocating...\n", size);
482
483 /* Allocate memory for the expanded device tree */
484 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000485 mem = (unsigned long) __va(mem);
486
487 ((u32 *)mem)[size / 4] = 0xdeadbeef;
488
489 DBG(" unflattening %lx...\n", mem);
490
491 /* Second pass, do actual unflattening */
492 start = ((unsigned long)initial_boot_params) +
493 initial_boot_params->off_dt_struct;
494 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
495 if (*((u32 *)start) != OF_DT_END)
496 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
497 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
498 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
499 ((u32 *)mem)[size / 4] );
500 *allnextp = NULL;
501
502 /* Get pointer to OF "/chosen" node for use everywhere */
503 of_chosen = of_find_node_by_path("/chosen");
Paul Mackerrasa575b802005-10-23 17:23:21 +1000504 if (of_chosen == NULL)
505 of_chosen = of_find_node_by_path("/chosen@0");
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000506
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000507 DBG(" <- unflatten_device_tree()\n");
508}
509
Paul Mackerrasd2058192006-05-03 23:04:37 +1000510/*
511 * ibm,pa-features is a per-cpu property that contains a string of
512 * attribute descriptors, each of which has a 2 byte header plus up
513 * to 254 bytes worth of processor attribute bits. First header
514 * byte specifies the number of bytes following the header.
515 * Second header byte is an "attribute-specifier" type, of which
516 * zero is the only currently-defined value.
517 * Implementation: Pass in the byte and bit offset for the feature
518 * that we are interested in. The function will return -1 if the
519 * pa-features property is missing, or a 1/0 to indicate if the feature
520 * is supported/not supported. Note that the bit numbers are
521 * big-endian to match the definition in PAPR.
522 */
523static struct ibm_pa_feature {
524 unsigned long cpu_features; /* CPU_FTR_xxx bit */
525 unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
526 unsigned char pabyte; /* byte number in ibm,pa-features */
527 unsigned char pabit; /* bit number (big-endian) */
528 unsigned char invert; /* if 1, pa bit set => clear feature */
529} ibm_pa_features[] __initdata = {
530 {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
531 {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
532 {CPU_FTR_SLB, 0, 0, 2, 0},
533 {CPU_FTR_CTRL, 0, 0, 3, 0},
534 {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
535 {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
536 {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
Paul Mackerras339d76c2006-06-29 17:12:30 +1000537 {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
Paul Mackerrasd2058192006-05-03 23:04:37 +1000538};
539
Paul Mackerras974a76f2006-11-10 20:38:53 +1100540static void __init scan_features(unsigned long node, unsigned char *ftrs,
541 unsigned long tablelen,
542 struct ibm_pa_feature *fp,
543 unsigned long ft_size)
Paul Mackerrasd2058192006-05-03 23:04:37 +1000544{
Paul Mackerras974a76f2006-11-10 20:38:53 +1100545 unsigned long i, len, bit;
Paul Mackerrasd2058192006-05-03 23:04:37 +1000546
547 /* find descriptor with type == 0 */
548 for (;;) {
549 if (tablelen < 3)
550 return;
Paul Mackerras974a76f2006-11-10 20:38:53 +1100551 len = 2 + ftrs[0];
Paul Mackerrasd2058192006-05-03 23:04:37 +1000552 if (tablelen < len)
553 return; /* descriptor 0 not found */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100554 if (ftrs[1] == 0)
Paul Mackerrasd2058192006-05-03 23:04:37 +1000555 break;
556 tablelen -= len;
Paul Mackerras974a76f2006-11-10 20:38:53 +1100557 ftrs += len;
Paul Mackerrasd2058192006-05-03 23:04:37 +1000558 }
559
560 /* loop over bits we know about */
Paul Mackerras974a76f2006-11-10 20:38:53 +1100561 for (i = 0; i < ft_size; ++i, ++fp) {
562 if (fp->pabyte >= ftrs[0])
Paul Mackerrasd2058192006-05-03 23:04:37 +1000563 continue;
Paul Mackerras974a76f2006-11-10 20:38:53 +1100564 bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
Paul Mackerrasd2058192006-05-03 23:04:37 +1000565 if (bit ^ fp->invert) {
566 cur_cpu_spec->cpu_features |= fp->cpu_features;
567 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
568 } else {
569 cur_cpu_spec->cpu_features &= ~fp->cpu_features;
570 cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
571 }
572 }
573}
574
Paul Mackerras974a76f2006-11-10 20:38:53 +1100575static void __init check_cpu_pa_features(unsigned long node)
576{
577 unsigned char *pa_ftrs;
578 unsigned long tablelen;
579
580 pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
581 if (pa_ftrs == NULL)
582 return;
583
584 scan_features(node, pa_ftrs, tablelen,
585 ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
586}
587
Michael Neuling584f8b72007-12-06 17:24:48 +1100588#ifdef CONFIG_PPC64
589static void __init check_cpu_slb_size(unsigned long node)
590{
591 u32 *slb_size_ptr;
592
593 slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
594 if (slb_size_ptr != NULL) {
595 mmu_slb_size = *slb_size_ptr;
596 }
597}
598#else
599#define check_cpu_slb_size(node) do { } while(0)
600#endif
601
Paul Mackerras974a76f2006-11-10 20:38:53 +1100602static struct feature_property {
603 const char *name;
604 u32 min_value;
605 unsigned long cpu_feature;
606 unsigned long cpu_user_ftr;
607} feature_properties[] __initdata = {
608#ifdef CONFIG_ALTIVEC
609 {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
610 {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
611#endif /* CONFIG_ALTIVEC */
612#ifdef CONFIG_PPC64
613 {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
614 {"ibm,purr", 1, CPU_FTR_PURR, 0},
615 {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
616#endif /* CONFIG_PPC64 */
617};
618
Valentine Barshak14b3d922007-12-22 03:24:02 +1100619#if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
620static inline void identical_pvr_fixup(unsigned long node)
621{
622 unsigned int pvr;
623 char *model = of_get_flat_dt_prop(node, "model", NULL);
624
625 /*
626 * Since 440GR(x)/440EP(x) processors have the same pvr,
627 * we check the node path and set bit 28 in the cur_cpu_spec
628 * pvr for EP(x) processor version. This bit is always 0 in
629 * the "real" pvr. Then we call identify_cpu again with
630 * the new logical pvr to enable FPU support.
631 */
632 if (model && strstr(model, "440EP")) {
633 pvr = cur_cpu_spec->pvr_value | 0x8;
634 identify_cpu(0, pvr);
635 DBG("Using logical pvr %x for %s\n", pvr, model);
636 }
637}
638#else
639#define identical_pvr_fixup(node) do { } while(0)
640#endif
641
Paul Mackerras974a76f2006-11-10 20:38:53 +1100642static void __init check_cpu_feature_properties(unsigned long node)
643{
644 unsigned long i;
645 struct feature_property *fp = feature_properties;
646 const u32 *prop;
647
648 for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
649 prop = of_get_flat_dt_prop(node, fp->name, NULL);
650 if (prop && *prop >= fp->min_value) {
651 cur_cpu_spec->cpu_features |= fp->cpu_feature;
652 cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
653 }
654 }
655}
656
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000657static int __init early_init_dt_scan_cpus(unsigned long node,
Anton Blanchard4df20462006-03-25 17:25:17 +1100658 const char *uname, int depth,
659 void *data)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000660{
Anton Blanchard4df20462006-03-25 17:25:17 +1100661 static int logical_cpuid = 0;
662 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Paul Mackerras974a76f2006-11-10 20:38:53 +1100663 const u32 *prop;
664 const u32 *intserv;
Anton Blanchard4df20462006-03-25 17:25:17 +1100665 int i, nthreads;
666 unsigned long len;
667 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000668
669 /* We are scanning "cpu" nodes only */
670 if (type == NULL || strcmp(type, "cpu") != 0)
671 return 0;
672
Anton Blanchard4df20462006-03-25 17:25:17 +1100673 /* Get physical cpuid */
674 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
675 if (intserv) {
676 nthreads = len / sizeof(int);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000677 } else {
Anton Blanchard4df20462006-03-25 17:25:17 +1100678 intserv = of_get_flat_dt_prop(node, "reg", NULL);
679 nthreads = 1;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000680 }
Anton Blanchard4df20462006-03-25 17:25:17 +1100681
682 /*
683 * Now see if any of these threads match our boot cpu.
684 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
685 */
686 for (i = 0; i < nthreads; i++) {
687 /*
688 * version 2 of the kexec param format adds the phys cpuid of
689 * booted proc.
690 */
691 if (initial_boot_params && initial_boot_params->version >= 2) {
692 if (intserv[i] ==
693 initial_boot_params->boot_cpuid_phys) {
694 found = 1;
695 break;
696 }
697 } else {
698 /*
699 * Check if it's the boot-cpu, set it's hw index now,
700 * unfortunately this format did not support booting
701 * off secondary threads.
702 */
703 if (of_get_flat_dt_prop(node,
704 "linux,boot-cpu", NULL) != NULL) {
705 found = 1;
706 break;
707 }
708 }
709
710#ifdef CONFIG_SMP
711 /* logical cpu id is always 0 on UP kernels */
712 logical_cpuid++;
713#endif
714 }
715
716 if (found) {
717 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
718 intserv[i]);
719 boot_cpuid = logical_cpuid;
720 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
Paul Mackerras974a76f2006-11-10 20:38:53 +1100721
722 /*
723 * PAPR defines "logical" PVR values for cpus that
724 * meet various levels of the architecture:
725 * 0x0f000001 Architecture version 2.04
726 * 0x0f000002 Architecture version 2.05
727 * If the cpu-version property in the cpu node contains
728 * such a value, we call identify_cpu again with the
729 * logical PVR value in order to use the cpu feature
730 * bits appropriate for the architecture level.
731 *
732 * A POWER6 partition in "POWER6 architected" mode
733 * uses the 0x0f000002 PVR value; in POWER5+ mode
734 * it uses 0x0f000001.
735 */
736 prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
737 if (prop && (*prop & 0xff000000) == 0x0f000000)
738 identify_cpu(0, *prop);
Valentine Barshak14b3d922007-12-22 03:24:02 +1100739
740 identical_pvr_fixup(node);
Anton Blanchard4df20462006-03-25 17:25:17 +1100741 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000742
Paul Mackerras974a76f2006-11-10 20:38:53 +1100743 check_cpu_feature_properties(node);
Paul Mackerrasd2058192006-05-03 23:04:37 +1000744 check_cpu_pa_features(node);
Michael Neuling584f8b72007-12-06 17:24:48 +1100745 check_cpu_slb_size(node);
Paul Mackerrasd2058192006-05-03 23:04:37 +1000746
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000747#ifdef CONFIG_PPC_PSERIES
Anton Blanchard4df20462006-03-25 17:25:17 +1100748 if (nthreads > 1)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000749 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
Anton Blanchard4df20462006-03-25 17:25:17 +1100750 else
751 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000752#endif
753
754 return 0;
755}
756
Michael Ellerman40472a52007-05-10 17:06:30 +1000757#ifdef CONFIG_BLK_DEV_INITRD
758static void __init early_init_dt_check_for_initrd(unsigned long node)
759{
760 unsigned long l;
761 u32 *prop;
762
763 DBG("Looking for initrd properties... ");
764
765 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
766 if (prop) {
767 initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
768
769 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
770 if (prop) {
771 initrd_end = (unsigned long)
772 __va(of_read_ulong(prop, l/4));
773 initrd_below_start_ok = 1;
774 } else {
775 initrd_start = 0;
776 }
777 }
778
779 DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
780}
781#else
782static inline void early_init_dt_check_for_initrd(unsigned long node)
783{
784}
785#endif /* CONFIG_BLK_DEV_INITRD */
786
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000787static int __init early_init_dt_scan_chosen(unsigned long node,
788 const char *uname, int depth, void *data)
789{
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000790 unsigned long *lprop;
Kumar Gala329dda02006-02-24 10:54:52 -0600791 unsigned long l;
792 char *p;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000793
794 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
795
Paul Mackerrasa575b802005-10-23 17:23:21 +1000796 if (depth != 1 ||
797 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000798 return 0;
799
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000800#ifdef CONFIG_PPC64
801 /* check if iommu is forced on or off */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100802 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000803 iommu_is_off = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100804 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000805 iommu_force_on = 1;
806#endif
807
Michael Ellerman2babf5c2006-05-17 18:00:46 +1000808 /* mem=x on the command line is the preferred mechanism */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100809 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000810 if (lprop)
811 memory_limit = *lprop;
812
813#ifdef CONFIG_PPC64
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100814 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000815 if (lprop)
816 tce_alloc_start = *lprop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100817 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000818 if (lprop)
819 tce_alloc_end = *lprop;
820#endif
821
Michael Ellermandcee3032005-12-04 18:39:48 +1100822#ifdef CONFIG_KEXEC
Linas Vepstas70c6cc32007-09-07 03:46:15 +1000823 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
824 if (lprop)
825 crashk_res.start = *lprop;
Michael Ellermandcee3032005-12-04 18:39:48 +1100826
Linas Vepstas70c6cc32007-09-07 03:46:15 +1000827 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
828 if (lprop)
829 crashk_res.end = crashk_res.start + *lprop - 1;
Michael Ellermandcee3032005-12-04 18:39:48 +1100830#endif
831
Michael Ellerman40472a52007-05-10 17:06:30 +1000832 early_init_dt_check_for_initrd(node);
David Gibson30437b32007-02-28 14:12:29 +1100833
Kumar Gala329dda02006-02-24 10:54:52 -0600834 /* Retreive command line */
835 p = of_get_flat_dt_prop(node, "bootargs", &l);
836 if (p != NULL && l > 0)
837 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
838
839#ifdef CONFIG_CMDLINE
Geoff Levandc1ce4642006-10-05 11:35:16 -0700840 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
Kumar Gala329dda02006-02-24 10:54:52 -0600841 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
842#endif /* CONFIG_CMDLINE */
843
844 DBG("Command line is: %s\n", cmd_line);
845
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000846 /* break now */
847 return 1;
848}
849
850static int __init early_init_dt_scan_root(unsigned long node,
851 const char *uname, int depth, void *data)
852{
853 u32 *prop;
854
855 if (depth != 0)
856 return 0;
857
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100858 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000859 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
860 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
861
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100862 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000863 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
864 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
865
866 /* break now */
867 return 1;
868}
869
Becky Bruceabe76882008-02-16 05:17:14 +1100870static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000871{
872 cell_t *p = *cellp;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000873
Paul Mackerrasa4dc7ff2006-09-19 14:06:27 +1000874 *cellp = p + s;
Becky Bruceabe76882008-02-16 05:17:14 +1100875 return of_read_number(p, s);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000876}
877
Paul Mackerras02045682006-11-29 22:27:42 +1100878#ifdef CONFIG_PPC_PSERIES
879/*
880 * Interpret the ibm,dynamic-memory property in the
881 * /ibm,dynamic-reconfiguration-memory node.
882 * This contains a list of memory blocks along with NUMA affinity
883 * information.
884 */
885static int __init early_init_dt_scan_drconf_memory(unsigned long node)
886{
887 cell_t *dm, *ls;
Becky Bruceabe76882008-02-16 05:17:14 +1100888 unsigned long l, n, flags;
889 u64 base, size, lmb_size;
Paul Mackerras02045682006-11-29 22:27:42 +1100890
891 ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
892 if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
893 return 0;
894 lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
895
896 dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
897 if (dm == NULL || l < sizeof(cell_t))
898 return 0;
899
900 n = *dm++; /* number of entries */
901 if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
902 return 0;
903
904 for (; n != 0; --n) {
905 base = dt_mem_next_cell(dt_root_addr_cells, &dm);
906 flags = dm[3];
907 /* skip DRC index, pad, assoc. list index, flags */
908 dm += 4;
909 /* skip this block if the reserved bit is set in flags (0x80)
910 or if the block is not assigned to this partition (0x8) */
911 if ((flags & 0x80) || !(flags & 0x8))
912 continue;
913 size = lmb_size;
914 if (iommu_is_off) {
915 if (base >= 0x80000000ul)
916 continue;
917 if ((base + size) > 0x80000000ul)
918 size = 0x80000000ul - base;
919 }
920 lmb_add(base, size);
921 }
922 lmb_dump_all();
923 return 0;
924}
925#else
926#define early_init_dt_scan_drconf_memory(node) 0
927#endif /* CONFIG_PPC_PSERIES */
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000928
929static int __init early_init_dt_scan_memory(unsigned long node,
930 const char *uname, int depth, void *data)
931{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100932 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000933 cell_t *reg, *endp;
934 unsigned long l;
935
Paul Mackerras02045682006-11-29 22:27:42 +1100936 /* Look for the ibm,dynamic-reconfiguration-memory node */
937 if (depth == 1 &&
938 strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
939 return early_init_dt_scan_drconf_memory(node);
940
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000941 /* We are scanning "memory" nodes only */
Paul Mackerrasa23414b2005-11-10 12:00:55 +1100942 if (type == NULL) {
943 /*
944 * The longtrail doesn't have a device_type on the
945 * /memory node, so look for the node called /memory@0.
946 */
947 if (depth != 1 || strcmp(uname, "memory@0") != 0)
948 return 0;
949 } else if (strcmp(type, "memory") != 0)
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000950 return 0;
951
Michael Ellermanba759482005-12-04 18:39:55 +1100952 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
953 if (reg == NULL)
954 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000955 if (reg == NULL)
956 return 0;
957
958 endp = reg + (l / sizeof(cell_t));
959
Michael Ellerman358c86f2005-11-03 15:39:09 +1100960 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000961 uname, l, reg[0], reg[1], reg[2], reg[3]);
962
963 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
Becky Bruceabe76882008-02-16 05:17:14 +1100964 u64 base, size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000965
966 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
967 size = dt_mem_next_cell(dt_root_size_cells, &reg);
968
969 if (size == 0)
970 continue;
Becky Bruceabe76882008-02-16 05:17:14 +1100971 DBG(" - %llx , %llx\n", (unsigned long long)base,
972 (unsigned long long)size);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000973#ifdef CONFIG_PPC64
974 if (iommu_is_off) {
975 if (base >= 0x80000000ul)
976 continue;
977 if ((base + size) > 0x80000000ul)
978 size = 0x80000000ul - base;
979 }
980#endif
981 lmb_add(base, size);
Kumar Gala37dd2ba2008-04-22 04:22:34 +1000982
983 memstart_addr = min((u64)memstart_addr, base);
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000984 }
Kumar Gala37dd2ba2008-04-22 04:22:34 +1000985
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000986 return 0;
987}
988
989static void __init early_reserve_mem(void)
990{
Kumar Galacbbcf342006-01-11 17:57:13 -0600991 u64 base, size;
992 u64 *reserve_map;
Jon Loeliger8a300882006-06-17 17:51:09 -0500993 unsigned long self_base;
994 unsigned long self_size;
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000995
Kumar Galacbbcf342006-01-11 17:57:13 -0600996 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
Paul Mackerras9b6b5632005-10-06 12:06:20 +1000997 initial_boot_params->off_mem_rsvmap);
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -0500998
999 /* before we do anything, lets reserve the dt blob */
Jon Loeliger8a300882006-06-17 17:51:09 -05001000 self_base = __pa((unsigned long)initial_boot_params);
1001 self_size = initial_boot_params->totalsize;
1002 lmb_reserve(self_base, self_size);
Jimi Xenidis4d1f3f22006-05-18 17:03:05 -05001003
David Gibson30437b32007-02-28 14:12:29 +11001004#ifdef CONFIG_BLK_DEV_INITRD
1005 /* then reserve the initrd, if any */
1006 if (initrd_start && (initrd_end > initrd_start))
1007 lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
1008#endif /* CONFIG_BLK_DEV_INITRD */
1009
Kumar Galacbbcf342006-01-11 17:57:13 -06001010#ifdef CONFIG_PPC32
1011 /*
1012 * Handle the case where we might be booting from an old kexec
1013 * image that setup the mem_rsvmap as pairs of 32-bit values
1014 */
1015 if (*reserve_map > 0xffffffffull) {
1016 u32 base_32, size_32;
1017 u32 *reserve_map_32 = (u32 *)reserve_map;
1018
1019 while (1) {
1020 base_32 = *(reserve_map_32++);
1021 size_32 = *(reserve_map_32++);
1022 if (size_32 == 0)
1023 break;
Jon Loeliger8a300882006-06-17 17:51:09 -05001024 /* skip if the reservation is for the blob */
1025 if (base_32 == self_base && size_32 == self_size)
1026 continue;
Kumar Gala329dda02006-02-24 10:54:52 -06001027 DBG("reserving: %x -> %x\n", base_32, size_32);
Kumar Galacbbcf342006-01-11 17:57:13 -06001028 lmb_reserve(base_32, size_32);
1029 }
1030 return;
1031 }
1032#endif
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001033 while (1) {
1034 base = *(reserve_map++);
1035 size = *(reserve_map++);
1036 if (size == 0)
1037 break;
Kumar Galacbbcf342006-01-11 17:57:13 -06001038 DBG("reserving: %llx -> %llx\n", base, size);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001039 lmb_reserve(base, size);
1040 }
1041
1042#if 0
1043 DBG("memory reserved, lmbs :\n");
1044 lmb_dump_all();
1045#endif
1046}
1047
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001048#ifdef CONFIG_PHYP_DUMP
1049/**
Manish Ahuja37ddd5d2008-04-12 09:31:52 +10001050 * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
1051 *
1052 * Function to find the largest size we need to reserve
1053 * during early boot process.
1054 *
1055 * It either looks for boot param and returns that OR
1056 * returns larger of 256 or 5% rounded down to multiples of 256MB.
1057 *
1058 */
1059static inline unsigned long phyp_dump_calculate_reserve_size(void)
1060{
1061 unsigned long tmp;
1062
1063 if (phyp_dump_info->reserve_bootvar)
1064 return phyp_dump_info->reserve_bootvar;
1065
1066 /* divide by 20 to get 5% of value */
1067 tmp = lmb_end_of_DRAM();
1068 do_div(tmp, 20);
1069
1070 /* round it down in multiples of 256 */
1071 tmp = tmp & ~0x0FFFFFFFUL;
1072
1073 return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
1074}
1075
1076/**
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001077 * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
1078 *
1079 * This routine may reserve memory regions in the kernel only
1080 * if the system is supported and a dump was taken in last
1081 * boot instance or if the hardware is supported and the
1082 * scratch area needs to be setup. In other instances it returns
1083 * without reserving anything. The memory in case of dump being
1084 * active is freed when the dump is collected (by userland tools).
1085 */
1086static void __init phyp_dump_reserve_mem(void)
1087{
1088 unsigned long base, size;
Manish Ahuja37ddd5d2008-04-12 09:31:52 +10001089 unsigned long variable_reserve_size;
1090
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001091 if (!phyp_dump_info->phyp_dump_configured) {
1092 printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
1093 return;
1094 }
1095
Manish Ahuja654f5962008-03-22 11:38:59 +11001096 if (!phyp_dump_info->phyp_dump_at_boot) {
1097 printk(KERN_INFO "Phyp-dump disabled at boot time\n");
1098 return;
1099 }
1100
Manish Ahuja37ddd5d2008-04-12 09:31:52 +10001101 variable_reserve_size = phyp_dump_calculate_reserve_size();
1102
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001103 if (phyp_dump_info->phyp_dump_is_active) {
1104 /* Reserve *everything* above RMR.Area freed by userland tools*/
Manish Ahuja37ddd5d2008-04-12 09:31:52 +10001105 base = variable_reserve_size;
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001106 size = lmb_end_of_DRAM() - base;
1107
1108 /* XXX crashed_ram_end is wrong, since it may be beyond
1109 * the memory_limit, it will need to be adjusted. */
1110 lmb_reserve(base, size);
1111
1112 phyp_dump_info->init_reserve_start = base;
1113 phyp_dump_info->init_reserve_size = size;
1114 } else {
1115 size = phyp_dump_info->cpu_state_size +
1116 phyp_dump_info->hpte_region_size +
Manish Ahuja37ddd5d2008-04-12 09:31:52 +10001117 variable_reserve_size;
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001118 base = lmb_end_of_DRAM() - size;
1119 lmb_reserve(base, size);
1120 phyp_dump_info->init_reserve_start = base;
1121 phyp_dump_info->init_reserve_size = size;
1122 }
1123}
1124#else
1125static inline void __init phyp_dump_reserve_mem(void) {}
1126#endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
1127
1128
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001129void __init early_init_devtree(void *params)
1130{
Geoff Levand44348102007-06-16 08:06:14 +10001131 DBG(" -> early_init_devtree(%p)\n", params);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001132
1133 /* Setup flat device-tree pointer */
1134 initial_boot_params = params;
1135
Michael Ellerman458148c2006-06-23 18:20:13 +10001136#ifdef CONFIG_PPC_RTAS
1137 /* Some machines might need RTAS info for debugging, grab it now. */
1138 of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
1139#endif
1140
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001141#ifdef CONFIG_PHYP_DUMP
1142 /* scan tree to see if dump occured during last boot */
1143 of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
1144#endif
1145
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001146 /* Retrieve various informations from the /chosen node of the
1147 * device-tree, including the platform type, initrd location and
1148 * size, TCE reserve, and more ...
1149 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001150 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001151
1152 /* Scan memory nodes and rebuild LMBs */
1153 lmb_init();
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001154 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1155 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
Michael Ellerman846f77b2006-05-17 18:00:45 +10001156
1157 /* Save command line for /proc/cmdline and then parse parameters */
Alon Bar-Levb8757b22007-02-12 00:54:17 -08001158 strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
Michael Ellerman846f77b2006-05-17 18:00:45 +10001159 parse_early_param();
1160
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001161 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
Michael Ellerman0cc47462005-12-04 18:39:37 +11001162 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
Michael Ellerman47310412006-05-17 18:00:49 +10001163 reserve_kdump_trampoline();
Michael Ellerman35dd5432006-05-18 11:16:11 +10001164 reserve_crashkernel();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001165 early_reserve_mem();
Manish Ahuja6ac26c82008-03-22 10:37:08 +11001166 phyp_dump_reserve_mem();
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001167
Michael Ellerman2babf5c2006-05-17 18:00:46 +10001168 lmb_enforce_memory_limit(memory_limit);
1169 lmb_analyze();
1170
1171 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1172
1173 /* We may need to relocate the flat tree, do it now.
1174 * FIXME .. and the initrd too? */
1175 move_device_tree();
1176
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001177 DBG("Scanning CPUs ...\n");
1178
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001179 /* Retreive CPU related informations from the flat tree
1180 * (altivec support, boot CPU ID, ...)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001181 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001182 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001183
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001184 DBG(" <- early_init_devtree()\n");
1185}
1186
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001187
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001188/**
1189 * Indicates whether the root node has a given value in its
1190 * compatible property.
1191 */
1192int machine_is_compatible(const char *compat)
1193{
1194 struct device_node *root;
1195 int rc = 0;
1196
1197 root = of_find_node_by_path("/");
1198 if (root) {
Stephen Rothwell7a92f742007-04-03 10:55:39 +10001199 rc = of_device_is_compatible(root, compat);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001200 of_node_put(root);
1201 }
1202 return rc;
1203}
1204EXPORT_SYMBOL(machine_is_compatible);
1205
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001206/*******
1207 *
1208 * New implementation of the OF "find" APIs, return a refcounted
1209 * object, call of_node_put() when done. The device tree and list
1210 * are protected by a rw_lock.
1211 *
1212 * Note that property management will need some locking as well,
1213 * this isn't dealt with yet.
1214 *
1215 *******/
1216
1217/**
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001218 * of_find_node_by_phandle - Find a node given a phandle
1219 * @handle: phandle of the node to find
1220 *
1221 * Returns a node pointer with refcount incremented, use
1222 * of_node_put() on it when done.
1223 */
1224struct device_node *of_find_node_by_phandle(phandle handle)
1225{
1226 struct device_node *np;
1227
1228 read_lock(&devtree_lock);
1229 for (np = allnodes; np != 0; np = np->allnext)
1230 if (np->linux_phandle == handle)
1231 break;
Mariusz Kozlowskib1374052007-01-02 12:31:47 +01001232 of_node_get(np);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001233 read_unlock(&devtree_lock);
1234 return np;
1235}
1236EXPORT_SYMBOL(of_find_node_by_phandle);
1237
1238/**
1239 * of_find_all_nodes - Get next node in global list
1240 * @prev: Previous node or NULL to start iteration
1241 * of_node_put() will be called on it
1242 *
1243 * Returns a node pointer with refcount incremented, use
1244 * of_node_put() on it when done.
1245 */
1246struct device_node *of_find_all_nodes(struct device_node *prev)
1247{
1248 struct device_node *np;
1249
1250 read_lock(&devtree_lock);
1251 np = prev ? prev->allnext : allnodes;
1252 for (; np != 0; np = np->allnext)
1253 if (of_node_get(np))
1254 break;
Mariusz Kozlowskib1374052007-01-02 12:31:47 +01001255 of_node_put(prev);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001256 read_unlock(&devtree_lock);
1257 return np;
1258}
1259EXPORT_SYMBOL(of_find_all_nodes);
1260
1261/**
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001262 * of_node_get - Increment refcount of a node
1263 * @node: Node to inc refcount, NULL is supported to
1264 * simplify writing of callers
1265 *
1266 * Returns node.
1267 */
1268struct device_node *of_node_get(struct device_node *node)
1269{
1270 if (node)
1271 kref_get(&node->kref);
1272 return node;
1273}
1274EXPORT_SYMBOL(of_node_get);
1275
1276static inline struct device_node * kref_to_device_node(struct kref *kref)
1277{
1278 return container_of(kref, struct device_node, kref);
1279}
1280
1281/**
1282 * of_node_release - release a dynamically allocated node
1283 * @kref: kref element of the node to be released
1284 *
1285 * In of_node_put() this function is passed to kref_put()
1286 * as the destructor.
1287 */
1288static void of_node_release(struct kref *kref)
1289{
1290 struct device_node *node = kref_to_device_node(kref);
1291 struct property *prop = node->properties;
1292
Michael Ellerman6a2818562007-06-19 16:08:00 +10001293 /* We should never be releasing nodes that haven't been detached. */
1294 if (!of_node_check_flag(node, OF_DETACHED)) {
1295 printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
1296 dump_stack();
1297 kref_init(&node->kref);
1298 return;
1299 }
1300
Michael Ellermand3b814b2007-06-19 16:07:58 +10001301 if (!of_node_check_flag(node, OF_DYNAMIC))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001302 return;
Michael Ellerman6a2818562007-06-19 16:08:00 +10001303
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001304 while (prop) {
1305 struct property *next = prop->next;
1306 kfree(prop->name);
1307 kfree(prop->value);
1308 kfree(prop);
1309 prop = next;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001310
1311 if (!prop) {
1312 prop = node->deadprops;
1313 node->deadprops = NULL;
1314 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001315 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001316 kfree(node->full_name);
1317 kfree(node->data);
1318 kfree(node);
1319}
1320
1321/**
1322 * of_node_put - Decrement refcount of a node
1323 * @node: Node to dec refcount, NULL is supported to
1324 * simplify writing of callers
1325 *
1326 */
1327void of_node_put(struct device_node *node)
1328{
1329 if (node)
1330 kref_put(&node->kref, of_node_release);
1331}
1332EXPORT_SYMBOL(of_node_put);
1333
1334/*
1335 * Plug a device node into the tree and global list.
1336 */
1337void of_attach_node(struct device_node *np)
1338{
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001339 unsigned long flags;
1340
1341 write_lock_irqsave(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001342 np->sibling = np->parent->child;
1343 np->allnext = allnodes;
1344 np->parent->child = np;
1345 allnodes = np;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001346 write_unlock_irqrestore(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001347}
1348
1349/*
1350 * "Unplug" a node from the device tree. The caller must hold
1351 * a reference to the node. The memory associated with the node
1352 * is not freed until its refcount goes to zero.
1353 */
Segher Boessenkool34f329d2007-07-20 15:58:38 +10001354void of_detach_node(struct device_node *np)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001355{
1356 struct device_node *parent;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001357 unsigned long flags;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001358
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001359 write_lock_irqsave(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001360
1361 parent = np->parent;
Michael Ellerman972d17c2007-06-19 16:07:56 +10001362 if (!parent)
1363 goto out_unlock;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001364
1365 if (allnodes == np)
1366 allnodes = np->allnext;
1367 else {
1368 struct device_node *prev;
1369 for (prev = allnodes;
1370 prev->allnext != np;
1371 prev = prev->allnext)
1372 ;
1373 prev->allnext = np->allnext;
1374 }
1375
1376 if (parent->child == np)
1377 parent->child = np->sibling;
1378 else {
1379 struct device_node *prevsib;
1380 for (prevsib = np->parent->child;
1381 prevsib->sibling != np;
1382 prevsib = prevsib->sibling)
1383 ;
1384 prevsib->sibling = np->sibling;
1385 }
1386
Michael Ellerman6a2818562007-06-19 16:08:00 +10001387 of_node_set_flag(np, OF_DETACHED);
1388
Michael Ellerman972d17c2007-06-19 16:07:56 +10001389out_unlock:
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001390 write_unlock_irqrestore(&devtree_lock, flags);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001391}
1392
1393#ifdef CONFIG_PPC_PSERIES
1394/*
1395 * Fix up the uninitialized fields in a new device node:
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001396 * name, type and pci-specific fields
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001397 */
1398
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001399static int of_finish_dynamic_node(struct device_node *node)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001400{
1401 struct device_node *parent = of_get_parent(node);
1402 int err = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001403 const phandle *ibm_phandle;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001404
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001405 node->name = of_get_property(node, "name", NULL);
1406 node->type = of_get_property(node, "device_type", NULL);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001407
Benjamin Herrenschmidt847f5972007-05-16 16:57:24 +10001408 if (!node->name)
1409 node->name = "<NULL>";
1410 if (!node->type)
1411 node->type = "<NULL>";
1412
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001413 if (!parent) {
1414 err = -ENODEV;
1415 goto out;
1416 }
1417
1418 /* We don't support that function on PowerMac, at least
1419 * not yet
1420 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001421 if (machine_is(powermac))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001422 return -ENODEV;
1423
1424 /* fix up new node's linux_phandle field */
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001425 if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001426 node->linux_phandle = *ibm_phandle;
1427
1428out:
1429 of_node_put(parent);
1430 return err;
1431}
1432
1433static int prom_reconfig_notifier(struct notifier_block *nb,
1434 unsigned long action, void *node)
1435{
1436 int err;
1437
1438 switch (action) {
1439 case PSERIES_RECONFIG_ADD:
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001440 err = of_finish_dynamic_node(node);
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001441 if (err < 0) {
1442 printk(KERN_ERR "finish_node returned %d\n", err);
1443 err = NOTIFY_BAD;
1444 }
1445 break;
1446 default:
1447 err = NOTIFY_DONE;
1448 break;
1449 }
1450 return err;
1451}
1452
1453static struct notifier_block prom_reconfig_nb = {
1454 .notifier_call = prom_reconfig_notifier,
1455 .priority = 10, /* This one needs to run first */
1456};
1457
1458static int __init prom_reconfig_setup(void)
1459{
1460 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1461}
1462__initcall(prom_reconfig_setup);
1463#endif
1464
Dave C Boutcherecaa8b02006-01-12 16:09:29 -06001465/*
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001466 * Add a property to a node
1467 */
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001468int prom_add_property(struct device_node* np, struct property* prop)
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001469{
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001470 struct property **next;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001471 unsigned long flags;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001472
1473 prop->next = NULL;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001474 write_lock_irqsave(&devtree_lock, flags);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001475 next = &np->properties;
1476 while (*next) {
1477 if (strcmp(prop->name, (*next)->name) == 0) {
1478 /* duplicate ! don't insert it */
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001479 write_unlock_irqrestore(&devtree_lock, flags);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001480 return -1;
1481 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001482 next = &(*next)->next;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001483 }
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001484 *next = prop;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001485 write_unlock_irqrestore(&devtree_lock, flags);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001486
Paul Mackerras799d6042005-11-10 13:37:51 +11001487#ifdef CONFIG_PROC_DEVICETREE
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001488 /* try to add to proc as well if it was initialized */
1489 if (np->pde)
1490 proc_device_tree_add_prop(np->pde, prop);
Paul Mackerras799d6042005-11-10 13:37:51 +11001491#endif /* CONFIG_PROC_DEVICETREE */
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001492
1493 return 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001494}
1495
Dave C Boutcher088186d2006-01-12 16:08:27 -06001496/*
1497 * Remove a property from a node. Note that we don't actually
1498 * remove it, since we have given out who-knows-how-many pointers
1499 * to the data using get-property. Instead we just move the property
1500 * to the "dead properties" list, so it won't be found any more.
1501 */
1502int prom_remove_property(struct device_node *np, struct property *prop)
1503{
1504 struct property **next;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001505 unsigned long flags;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001506 int found = 0;
Paul Mackerras9b6b5632005-10-06 12:06:20 +10001507
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001508 write_lock_irqsave(&devtree_lock, flags);
Dave C Boutcher088186d2006-01-12 16:08:27 -06001509 next = &np->properties;
1510 while (*next) {
1511 if (*next == prop) {
1512 /* found the node */
1513 *next = prop->next;
1514 prop->next = np->deadprops;
1515 np->deadprops = prop;
1516 found = 1;
1517 break;
1518 }
1519 next = &(*next)->next;
1520 }
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001521 write_unlock_irqrestore(&devtree_lock, flags);
Dave C Boutcher088186d2006-01-12 16:08:27 -06001522
1523 if (!found)
1524 return -ENODEV;
1525
1526#ifdef CONFIG_PROC_DEVICETREE
1527 /* try to remove the proc node as well */
1528 if (np->pde)
1529 proc_device_tree_remove_prop(np->pde, prop);
1530#endif /* CONFIG_PROC_DEVICETREE */
1531
1532 return 0;
1533}
1534
1535/*
1536 * Update a property in a node. Note that we don't actually
1537 * remove it, since we have given out who-knows-how-many pointers
1538 * to the data using get-property. Instead we just move the property
1539 * to the "dead properties" list, and add the new property to the
1540 * property list
1541 */
1542int prom_update_property(struct device_node *np,
1543 struct property *newprop,
1544 struct property *oldprop)
1545{
1546 struct property **next;
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001547 unsigned long flags;
Dave C Boutcher088186d2006-01-12 16:08:27 -06001548 int found = 0;
1549
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001550 write_lock_irqsave(&devtree_lock, flags);
Dave C Boutcher088186d2006-01-12 16:08:27 -06001551 next = &np->properties;
1552 while (*next) {
1553 if (*next == oldprop) {
1554 /* found the node */
1555 newprop->next = oldprop->next;
1556 *next = newprop;
1557 oldprop->next = np->deadprops;
1558 np->deadprops = oldprop;
1559 found = 1;
1560 break;
1561 }
1562 next = &(*next)->next;
1563 }
Benjamin Herrenschmidtf4ac7b52008-04-09 17:21:36 +10001564 write_unlock_irqrestore(&devtree_lock, flags);
Dave C Boutcher088186d2006-01-12 16:08:27 -06001565
1566 if (!found)
1567 return -ENODEV;
1568
1569#ifdef CONFIG_PROC_DEVICETREE
1570 /* try to add to proc as well if it was initialized */
1571 if (np->pde)
1572 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1573#endif /* CONFIG_PROC_DEVICETREE */
1574
1575 return 0;
1576}
Michael Ellermanb68239e2006-02-03 19:05:47 +11001577
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001578
1579/* Find the device node for a given logical cpu number, also returns the cpu
1580 * local thread number (index in ibm,interrupt-server#s) if relevant and
1581 * asked for (non NULL)
1582 */
1583struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1584{
1585 int hardid;
1586 struct device_node *np;
1587
1588 hardid = get_hard_smp_processor_id(cpu);
1589
1590 for_each_node_by_type(np, "cpu") {
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001591 const u32 *intserv;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001592 unsigned int plen, t;
1593
1594 /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1595 * fallback to "reg" property and assume no threads
1596 */
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001597 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
Jeremy Kerra7f67bd2006-07-12 15:35:54 +10001598 &plen);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001599 if (intserv == NULL) {
Stephen Rothwell0e56efc2007-04-03 10:54:01 +10001600 const u32 *reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +02001601 if (reg == NULL)
1602 continue;
1603 if (*reg == hardid) {
1604 if (thread)
1605 *thread = 0;
1606 return np;
1607 }
1608 } else {
1609 plen /= sizeof(u32);
1610 for (t = 0; t < plen; t++) {
1611 if (hardid == intserv[t]) {
1612 if (thread)
1613 *thread = t;
1614 return np;
1615 }
1616 }
1617 }
1618 }
1619 return NULL;
1620}
Christian Krafft36ca4ba2006-10-24 18:39:45 +02001621EXPORT_SYMBOL(of_get_cpu_node);
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001622
Michael Ellerman94a38072007-06-20 10:54:19 +10001623#if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001624static struct debugfs_blob_wrapper flat_dt_blob;
1625
1626static int __init export_flat_device_tree(void)
1627{
1628 struct dentry *d;
1629
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001630 flat_dt_blob.data = initial_boot_params;
1631 flat_dt_blob.size = initial_boot_params->totalsize;
1632
1633 d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
Michael Ellerman94a38072007-06-20 10:54:19 +10001634 powerpc_debugfs_root, &flat_dt_blob);
Michael Ellerman7a4571a2006-06-23 18:16:03 +10001635 if (!d)
1636 return 1;
1637
1638 return 0;
1639}
1640__initcall(export_flat_device_tree);
1641#endif