[PATCH] powerpc: Allow non zero boot cpuids
[linux-2.6.git] / arch / powerpc / kernel / prom.c
blob5a24415a2e3c4aadfb2ecdf2b21f9413a01a957e
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
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
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.
16 #undef DEBUG
18 #include <stdarg.h>
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26 #include <linux/pci.h>
27 #include <linux/stringify.h>
28 #include <linux/delay.h>
29 #include <linux/initrd.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <linux/kexec.h>
34 #include <asm/prom.h>
35 #include <asm/rtas.h>
36 #include <asm/lmb.h>
37 #include <asm/page.h>
38 #include <asm/processor.h>
39 #include <asm/irq.h>
40 #include <asm/io.h>
41 #include <asm/kdump.h>
42 #include <asm/smp.h>
43 #include <asm/system.h>
44 #include <asm/mmu.h>
45 #include <asm/pgtable.h>
46 #include <asm/pci.h>
47 #include <asm/iommu.h>
48 #include <asm/btext.h>
49 #include <asm/sections.h>
50 #include <asm/machdep.h>
51 #include <asm/pSeries_reconfig.h>
52 #include <asm/pci-bridge.h>
54 #ifdef DEBUG
55 #define DBG(fmt...) printk(KERN_ERR fmt)
56 #else
57 #define DBG(fmt...)
58 #endif
61 static int __initdata dt_root_addr_cells;
62 static int __initdata dt_root_size_cells;
64 #ifdef CONFIG_PPC64
65 static int __initdata iommu_is_off;
66 int __initdata iommu_force_on;
67 unsigned long tce_alloc_start, tce_alloc_end;
68 #endif
70 typedef u32 cell_t;
72 #if 0
73 static struct boot_param_header *initial_boot_params __initdata;
74 #else
75 struct boot_param_header *initial_boot_params;
76 #endif
78 static struct device_node *allnodes = NULL;
80 /* use when traversing tree through the allnext, child, sibling,
81 * or parent members of struct device_node.
83 static DEFINE_RWLOCK(devtree_lock);
85 /* export that to outside world */
86 struct device_node *of_chosen;
88 struct device_node *dflt_interrupt_controller;
89 int num_interrupt_controllers;
92 * Wrapper for allocating memory for various data that needs to be
93 * attached to device nodes as they are processed at boot or when
94 * added to the device tree later (e.g. DLPAR). At boot there is
95 * already a region reserved so we just increment *mem_start by size;
96 * otherwise we call kmalloc.
98 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
100 unsigned long tmp;
102 if (!mem_start)
103 return kmalloc(size, GFP_KERNEL);
105 tmp = *mem_start;
106 *mem_start += size;
107 return (void *)tmp;
111 * Find the device_node with a given phandle.
113 static struct device_node * find_phandle(phandle ph)
115 struct device_node *np;
117 for (np = allnodes; np != 0; np = np->allnext)
118 if (np->linux_phandle == ph)
119 return np;
120 return NULL;
124 * Find the interrupt parent of a node.
126 static struct device_node * __devinit intr_parent(struct device_node *p)
128 phandle *parp;
130 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
131 if (parp == NULL)
132 return p->parent;
133 p = find_phandle(*parp);
134 if (p != NULL)
135 return p;
137 * On a powermac booted with BootX, we don't get to know the
138 * phandles for any nodes, so find_phandle will return NULL.
139 * Fortunately these machines only have one interrupt controller
140 * so there isn't in fact any ambiguity. -- paulus
142 if (num_interrupt_controllers == 1)
143 p = dflt_interrupt_controller;
144 return p;
148 * Find out the size of each entry of the interrupts property
149 * for a node.
151 int __devinit prom_n_intr_cells(struct device_node *np)
153 struct device_node *p;
154 unsigned int *icp;
156 for (p = np; (p = intr_parent(p)) != NULL; ) {
157 icp = (unsigned int *)
158 get_property(p, "#interrupt-cells", NULL);
159 if (icp != NULL)
160 return *icp;
161 if (get_property(p, "interrupt-controller", NULL) != NULL
162 || get_property(p, "interrupt-map", NULL) != NULL) {
163 printk("oops, node %s doesn't have #interrupt-cells\n",
164 p->full_name);
165 return 1;
168 #ifdef DEBUG_IRQ
169 printk("prom_n_intr_cells failed for %s\n", np->full_name);
170 #endif
171 return 1;
175 * Map an interrupt from a device up to the platform interrupt
176 * descriptor.
178 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
179 struct device_node *np, unsigned int *ints,
180 int nintrc)
182 struct device_node *p, *ipar;
183 unsigned int *imap, *imask, *ip;
184 int i, imaplen, match;
185 int newintrc = 0, newaddrc = 0;
186 unsigned int *reg;
187 int naddrc;
189 reg = (unsigned int *) get_property(np, "reg", NULL);
190 naddrc = prom_n_addr_cells(np);
191 p = intr_parent(np);
192 while (p != NULL) {
193 if (get_property(p, "interrupt-controller", NULL) != NULL)
194 /* this node is an interrupt controller, stop here */
195 break;
196 imap = (unsigned int *)
197 get_property(p, "interrupt-map", &imaplen);
198 if (imap == NULL) {
199 p = intr_parent(p);
200 continue;
202 imask = (unsigned int *)
203 get_property(p, "interrupt-map-mask", NULL);
204 if (imask == NULL) {
205 printk("oops, %s has interrupt-map but no mask\n",
206 p->full_name);
207 return 0;
209 imaplen /= sizeof(unsigned int);
210 match = 0;
211 ipar = NULL;
212 while (imaplen > 0 && !match) {
213 /* check the child-interrupt field */
214 match = 1;
215 for (i = 0; i < naddrc && match; ++i)
216 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
217 for (; i < naddrc + nintrc && match; ++i)
218 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
219 imap += naddrc + nintrc;
220 imaplen -= naddrc + nintrc;
221 /* grab the interrupt parent */
222 ipar = find_phandle((phandle) *imap++);
223 --imaplen;
224 if (ipar == NULL && num_interrupt_controllers == 1)
225 /* cope with BootX not giving us phandles */
226 ipar = dflt_interrupt_controller;
227 if (ipar == NULL) {
228 printk("oops, no int parent %x in map of %s\n",
229 imap[-1], p->full_name);
230 return 0;
232 /* find the parent's # addr and intr cells */
233 ip = (unsigned int *)
234 get_property(ipar, "#interrupt-cells", NULL);
235 if (ip == NULL) {
236 printk("oops, no #interrupt-cells on %s\n",
237 ipar->full_name);
238 return 0;
240 newintrc = *ip;
241 ip = (unsigned int *)
242 get_property(ipar, "#address-cells", NULL);
243 newaddrc = (ip == NULL)? 0: *ip;
244 imap += newaddrc + newintrc;
245 imaplen -= newaddrc + newintrc;
247 if (imaplen < 0) {
248 printk("oops, error decoding int-map on %s, len=%d\n",
249 p->full_name, imaplen);
250 return 0;
252 if (!match) {
253 #ifdef DEBUG_IRQ
254 printk("oops, no match in %s int-map for %s\n",
255 p->full_name, np->full_name);
256 #endif
257 return 0;
259 p = ipar;
260 naddrc = newaddrc;
261 nintrc = newintrc;
262 ints = imap - nintrc;
263 reg = ints - naddrc;
265 if (p == NULL) {
266 #ifdef DEBUG_IRQ
267 printk("hmmm, int tree for %s doesn't have ctrler\n",
268 np->full_name);
269 #endif
270 return 0;
272 *irq = ints;
273 *ictrler = p;
274 return nintrc;
277 static unsigned char map_isa_senses[4] = {
278 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
279 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
280 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
281 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
284 static unsigned char map_mpic_senses[4] = {
285 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
286 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
287 /* 2 seems to be used for the 8259 cascade... */
288 IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
289 IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
292 static int __devinit finish_node_interrupts(struct device_node *np,
293 unsigned long *mem_start,
294 int measure_only)
296 unsigned int *ints;
297 int intlen, intrcells, intrcount;
298 int i, j, n, sense;
299 unsigned int *irq, virq;
300 struct device_node *ic;
301 int trace = 0;
303 //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
304 #define TRACE(fmt...)
306 if (!strcmp(np->name, "smu-doorbell"))
307 trace = 1;
309 TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
310 num_interrupt_controllers);
312 if (num_interrupt_controllers == 0) {
314 * Old machines just have a list of interrupt numbers
315 * and no interrupt-controller nodes.
317 ints = (unsigned int *) get_property(np, "AAPL,interrupts",
318 &intlen);
319 /* XXX old interpret_pci_props looked in parent too */
320 /* XXX old interpret_macio_props looked for interrupts
321 before AAPL,interrupts */
322 if (ints == NULL)
323 ints = (unsigned int *) get_property(np, "interrupts",
324 &intlen);
325 if (ints == NULL)
326 return 0;
328 np->n_intrs = intlen / sizeof(unsigned int);
329 np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
330 mem_start);
331 if (!np->intrs)
332 return -ENOMEM;
333 if (measure_only)
334 return 0;
336 for (i = 0; i < np->n_intrs; ++i) {
337 np->intrs[i].line = *ints++;
338 np->intrs[i].sense = IRQ_SENSE_LEVEL
339 | IRQ_POLARITY_NEGATIVE;
341 return 0;
344 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
345 TRACE("ints=%p, intlen=%d\n", ints, intlen);
346 if (ints == NULL)
347 return 0;
348 intrcells = prom_n_intr_cells(np);
349 intlen /= intrcells * sizeof(unsigned int);
350 TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
351 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
352 if (!np->intrs)
353 return -ENOMEM;
355 if (measure_only)
356 return 0;
358 intrcount = 0;
359 for (i = 0; i < intlen; ++i, ints += intrcells) {
360 n = map_interrupt(&irq, &ic, np, ints, intrcells);
361 TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
362 if (n <= 0)
363 continue;
365 /* don't map IRQ numbers under a cascaded 8259 controller */
366 if (ic && device_is_compatible(ic, "chrp,iic")) {
367 np->intrs[intrcount].line = irq[0];
368 sense = (n > 1)? (irq[1] & 3): 3;
369 np->intrs[intrcount].sense = map_isa_senses[sense];
370 } else {
371 virq = virt_irq_create_mapping(irq[0]);
372 TRACE("virq=%d\n", virq);
373 #ifdef CONFIG_PPC64
374 if (virq == NO_IRQ) {
375 printk(KERN_CRIT "Could not allocate interrupt"
376 " number for %s\n", np->full_name);
377 continue;
379 #endif
380 np->intrs[intrcount].line = irq_offset_up(virq);
381 sense = (n > 1)? (irq[1] & 3): 1;
383 /* Apple uses bits in there in a different way, let's
384 * only keep the real sense bit on macs
386 if (_machine == PLATFORM_POWERMAC)
387 sense &= 0x1;
388 np->intrs[intrcount].sense = map_mpic_senses[sense];
391 #ifdef CONFIG_PPC64
392 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
393 if (_machine == PLATFORM_POWERMAC && ic && ic->parent) {
394 char *name = get_property(ic->parent, "name", NULL);
395 if (name && !strcmp(name, "u3"))
396 np->intrs[intrcount].line += 128;
397 else if (!(name && (!strcmp(name, "mac-io") ||
398 !strcmp(name, "u4"))))
399 /* ignore other cascaded controllers, such as
400 the k2-sata-root */
401 break;
403 #endif /* CONFIG_PPC64 */
404 if (n > 2) {
405 printk("hmmm, got %d intr cells for %s:", n,
406 np->full_name);
407 for (j = 0; j < n; ++j)
408 printk(" %d", irq[j]);
409 printk("\n");
411 ++intrcount;
413 np->n_intrs = intrcount;
415 return 0;
418 static int __devinit finish_node(struct device_node *np,
419 unsigned long *mem_start,
420 int measure_only)
422 struct device_node *child;
423 int rc = 0;
425 rc = finish_node_interrupts(np, mem_start, measure_only);
426 if (rc)
427 goto out;
429 for (child = np->child; child != NULL; child = child->sibling) {
430 rc = finish_node(child, mem_start, measure_only);
431 if (rc)
432 goto out;
434 out:
435 return rc;
438 static void __init scan_interrupt_controllers(void)
440 struct device_node *np;
441 int n = 0;
442 char *name, *ic;
443 int iclen;
445 for (np = allnodes; np != NULL; np = np->allnext) {
446 ic = get_property(np, "interrupt-controller", &iclen);
447 name = get_property(np, "name", NULL);
448 /* checking iclen makes sure we don't get a false
449 match on /chosen.interrupt_controller */
450 if ((name != NULL
451 && strcmp(name, "interrupt-controller") == 0)
452 || (ic != NULL && iclen == 0
453 && strcmp(name, "AppleKiwi"))) {
454 if (n == 0)
455 dflt_interrupt_controller = np;
456 ++n;
459 num_interrupt_controllers = n;
463 * finish_device_tree is called once things are running normally
464 * (i.e. with text and data mapped to the address they were linked at).
465 * It traverses the device tree and fills in some of the additional,
466 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
467 * mapping is also initialized at this point.
469 void __init finish_device_tree(void)
471 unsigned long start, end, size = 0;
473 DBG(" -> finish_device_tree\n");
475 #ifdef CONFIG_PPC64
476 /* Initialize virtual IRQ map */
477 virt_irq_init();
478 #endif
479 scan_interrupt_controllers();
482 * Finish device-tree (pre-parsing some properties etc...)
483 * We do this in 2 passes. One with "measure_only" set, which
484 * will only measure the amount of memory needed, then we can
485 * allocate that memory, and call finish_node again. However,
486 * we must be careful as most routines will fail nowadays when
487 * prom_alloc() returns 0, so we must make sure our first pass
488 * doesn't start at 0. We pre-initialize size to 16 for that
489 * reason and then remove those additional 16 bytes
491 size = 16;
492 finish_node(allnodes, &size, 1);
493 size -= 16;
495 if (0 == size)
496 end = start = 0;
497 else
498 end = start = (unsigned long)__va(lmb_alloc(size, 128));
500 finish_node(allnodes, &end, 0);
501 BUG_ON(end != start + size);
503 DBG(" <- finish_device_tree\n");
506 static inline char *find_flat_dt_string(u32 offset)
508 return ((char *)initial_boot_params) +
509 initial_boot_params->off_dt_strings + offset;
513 * This function is used to scan the flattened device-tree, it is
514 * used to extract the memory informations at boot before we can
515 * unflatten the tree
517 int __init of_scan_flat_dt(int (*it)(unsigned long node,
518 const char *uname, int depth,
519 void *data),
520 void *data)
522 unsigned long p = ((unsigned long)initial_boot_params) +
523 initial_boot_params->off_dt_struct;
524 int rc = 0;
525 int depth = -1;
527 do {
528 u32 tag = *((u32 *)p);
529 char *pathp;
531 p += 4;
532 if (tag == OF_DT_END_NODE) {
533 depth --;
534 continue;
536 if (tag == OF_DT_NOP)
537 continue;
538 if (tag == OF_DT_END)
539 break;
540 if (tag == OF_DT_PROP) {
541 u32 sz = *((u32 *)p);
542 p += 8;
543 if (initial_boot_params->version < 0x10)
544 p = _ALIGN(p, sz >= 8 ? 8 : 4);
545 p += sz;
546 p = _ALIGN(p, 4);
547 continue;
549 if (tag != OF_DT_BEGIN_NODE) {
550 printk(KERN_WARNING "Invalid tag %x scanning flattened"
551 " device tree !\n", tag);
552 return -EINVAL;
554 depth++;
555 pathp = (char *)p;
556 p = _ALIGN(p + strlen(pathp) + 1, 4);
557 if ((*pathp) == '/') {
558 char *lp, *np;
559 for (lp = NULL, np = pathp; *np; np++)
560 if ((*np) == '/')
561 lp = np+1;
562 if (lp != NULL)
563 pathp = lp;
565 rc = it(p, pathp, depth, data);
566 if (rc != 0)
567 break;
568 } while(1);
570 return rc;
574 * This function can be used within scan_flattened_dt callback to get
575 * access to properties
577 void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
578 unsigned long *size)
580 unsigned long p = node;
582 do {
583 u32 tag = *((u32 *)p);
584 u32 sz, noff;
585 const char *nstr;
587 p += 4;
588 if (tag == OF_DT_NOP)
589 continue;
590 if (tag != OF_DT_PROP)
591 return NULL;
593 sz = *((u32 *)p);
594 noff = *((u32 *)(p + 4));
595 p += 8;
596 if (initial_boot_params->version < 0x10)
597 p = _ALIGN(p, sz >= 8 ? 8 : 4);
599 nstr = find_flat_dt_string(noff);
600 if (nstr == NULL) {
601 printk(KERN_WARNING "Can't find property index"
602 " name !\n");
603 return NULL;
605 if (strcmp(name, nstr) == 0) {
606 if (size)
607 *size = sz;
608 return (void *)p;
610 p += sz;
611 p = _ALIGN(p, 4);
612 } while(1);
615 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
616 unsigned long align)
618 void *res;
620 *mem = _ALIGN(*mem, align);
621 res = (void *)*mem;
622 *mem += size;
624 return res;
627 static unsigned long __init unflatten_dt_node(unsigned long mem,
628 unsigned long *p,
629 struct device_node *dad,
630 struct device_node ***allnextpp,
631 unsigned long fpsize)
633 struct device_node *np;
634 struct property *pp, **prev_pp = NULL;
635 char *pathp;
636 u32 tag;
637 unsigned int l, allocl;
638 int has_name = 0;
639 int new_format = 0;
641 tag = *((u32 *)(*p));
642 if (tag != OF_DT_BEGIN_NODE) {
643 printk("Weird tag at start of node: %x\n", tag);
644 return mem;
646 *p += 4;
647 pathp = (char *)*p;
648 l = allocl = strlen(pathp) + 1;
649 *p = _ALIGN(*p + l, 4);
651 /* version 0x10 has a more compact unit name here instead of the full
652 * path. we accumulate the full path size using "fpsize", we'll rebuild
653 * it later. We detect this because the first character of the name is
654 * not '/'.
656 if ((*pathp) != '/') {
657 new_format = 1;
658 if (fpsize == 0) {
659 /* root node: special case. fpsize accounts for path
660 * plus terminating zero. root node only has '/', so
661 * fpsize should be 2, but we want to avoid the first
662 * level nodes to have two '/' so we use fpsize 1 here
664 fpsize = 1;
665 allocl = 2;
666 } else {
667 /* account for '/' and path size minus terminal 0
668 * already in 'l'
670 fpsize += l;
671 allocl = fpsize;
676 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
677 __alignof__(struct device_node));
678 if (allnextpp) {
679 memset(np, 0, sizeof(*np));
680 np->full_name = ((char*)np) + sizeof(struct device_node);
681 if (new_format) {
682 char *p = np->full_name;
683 /* rebuild full path for new format */
684 if (dad && dad->parent) {
685 strcpy(p, dad->full_name);
686 #ifdef DEBUG
687 if ((strlen(p) + l + 1) != allocl) {
688 DBG("%s: p: %d, l: %d, a: %d\n",
689 pathp, strlen(p), l, allocl);
691 #endif
692 p += strlen(p);
694 *(p++) = '/';
695 memcpy(p, pathp, l);
696 } else
697 memcpy(np->full_name, pathp, l);
698 prev_pp = &np->properties;
699 **allnextpp = np;
700 *allnextpp = &np->allnext;
701 if (dad != NULL) {
702 np->parent = dad;
703 /* we temporarily use the next field as `last_child'*/
704 if (dad->next == 0)
705 dad->child = np;
706 else
707 dad->next->sibling = np;
708 dad->next = np;
710 kref_init(&np->kref);
712 while(1) {
713 u32 sz, noff;
714 char *pname;
716 tag = *((u32 *)(*p));
717 if (tag == OF_DT_NOP) {
718 *p += 4;
719 continue;
721 if (tag != OF_DT_PROP)
722 break;
723 *p += 4;
724 sz = *((u32 *)(*p));
725 noff = *((u32 *)((*p) + 4));
726 *p += 8;
727 if (initial_boot_params->version < 0x10)
728 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
730 pname = find_flat_dt_string(noff);
731 if (pname == NULL) {
732 printk("Can't find property name in list !\n");
733 break;
735 if (strcmp(pname, "name") == 0)
736 has_name = 1;
737 l = strlen(pname) + 1;
738 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
739 __alignof__(struct property));
740 if (allnextpp) {
741 if (strcmp(pname, "linux,phandle") == 0) {
742 np->node = *((u32 *)*p);
743 if (np->linux_phandle == 0)
744 np->linux_phandle = np->node;
746 if (strcmp(pname, "ibm,phandle") == 0)
747 np->linux_phandle = *((u32 *)*p);
748 pp->name = pname;
749 pp->length = sz;
750 pp->value = (void *)*p;
751 *prev_pp = pp;
752 prev_pp = &pp->next;
754 *p = _ALIGN((*p) + sz, 4);
756 /* with version 0x10 we may not have the name property, recreate
757 * it here from the unit name if absent
759 if (!has_name) {
760 char *p = pathp, *ps = pathp, *pa = NULL;
761 int sz;
763 while (*p) {
764 if ((*p) == '@')
765 pa = p;
766 if ((*p) == '/')
767 ps = p + 1;
768 p++;
770 if (pa < ps)
771 pa = p;
772 sz = (pa - ps) + 1;
773 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
774 __alignof__(struct property));
775 if (allnextpp) {
776 pp->name = "name";
777 pp->length = sz;
778 pp->value = (unsigned char *)(pp + 1);
779 *prev_pp = pp;
780 prev_pp = &pp->next;
781 memcpy(pp->value, ps, sz - 1);
782 ((char *)pp->value)[sz - 1] = 0;
783 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
786 if (allnextpp) {
787 *prev_pp = NULL;
788 np->name = get_property(np, "name", NULL);
789 np->type = get_property(np, "device_type", NULL);
791 if (!np->name)
792 np->name = "<NULL>";
793 if (!np->type)
794 np->type = "<NULL>";
796 while (tag == OF_DT_BEGIN_NODE) {
797 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
798 tag = *((u32 *)(*p));
800 if (tag != OF_DT_END_NODE) {
801 printk("Weird tag at end of node: %x\n", tag);
802 return mem;
804 *p += 4;
805 return mem;
810 * unflattens the device-tree passed by the firmware, creating the
811 * tree of struct device_node. It also fills the "name" and "type"
812 * pointers of the nodes so the normal device-tree walking functions
813 * can be used (this used to be done by finish_device_tree)
815 void __init unflatten_device_tree(void)
817 unsigned long start, mem, size;
818 struct device_node **allnextp = &allnodes;
820 DBG(" -> unflatten_device_tree()\n");
822 /* First pass, scan for size */
823 start = ((unsigned long)initial_boot_params) +
824 initial_boot_params->off_dt_struct;
825 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
826 size = (size | 3) + 1;
828 DBG(" size is %lx, allocating...\n", size);
830 /* Allocate memory for the expanded device tree */
831 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
832 mem = (unsigned long) __va(mem);
834 ((u32 *)mem)[size / 4] = 0xdeadbeef;
836 DBG(" unflattening %lx...\n", mem);
838 /* Second pass, do actual unflattening */
839 start = ((unsigned long)initial_boot_params) +
840 initial_boot_params->off_dt_struct;
841 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
842 if (*((u32 *)start) != OF_DT_END)
843 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
844 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
845 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
846 ((u32 *)mem)[size / 4] );
847 *allnextp = NULL;
849 /* Get pointer to OF "/chosen" node for use everywhere */
850 of_chosen = of_find_node_by_path("/chosen");
851 if (of_chosen == NULL)
852 of_chosen = of_find_node_by_path("/chosen@0");
854 DBG(" <- unflatten_device_tree()\n");
857 static int __init early_init_dt_scan_cpus(unsigned long node,
858 const char *uname, int depth,
859 void *data)
861 static int logical_cpuid = 0;
862 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
863 u32 *prop, *intserv;
864 int i, nthreads;
865 unsigned long len;
866 int found = 0;
868 /* We are scanning "cpu" nodes only */
869 if (type == NULL || strcmp(type, "cpu") != 0)
870 return 0;
872 /* Get physical cpuid */
873 intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
874 if (intserv) {
875 nthreads = len / sizeof(int);
876 } else {
877 intserv = of_get_flat_dt_prop(node, "reg", NULL);
878 nthreads = 1;
882 * Now see if any of these threads match our boot cpu.
883 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
885 for (i = 0; i < nthreads; i++) {
887 * version 2 of the kexec param format adds the phys cpuid of
888 * booted proc.
890 if (initial_boot_params && initial_boot_params->version >= 2) {
891 if (intserv[i] ==
892 initial_boot_params->boot_cpuid_phys) {
893 found = 1;
894 break;
896 } else {
898 * Check if it's the boot-cpu, set it's hw index now,
899 * unfortunately this format did not support booting
900 * off secondary threads.
902 if (of_get_flat_dt_prop(node,
903 "linux,boot-cpu", NULL) != NULL) {
904 found = 1;
905 break;
909 #ifdef CONFIG_SMP
910 /* logical cpu id is always 0 on UP kernels */
911 logical_cpuid++;
912 #endif
915 if (found) {
916 DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
917 intserv[i]);
918 boot_cpuid = logical_cpuid;
919 set_hard_smp_processor_id(boot_cpuid, intserv[i]);
922 #ifdef CONFIG_ALTIVEC
923 /* Check if we have a VMX and eventually update CPU features */
924 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
925 if (prop && (*prop) > 0) {
926 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
927 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
930 /* Same goes for Apple's "altivec" property */
931 prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
932 if (prop) {
933 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
934 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
936 #endif /* CONFIG_ALTIVEC */
938 #ifdef CONFIG_PPC_PSERIES
939 if (nthreads > 1)
940 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
941 else
942 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
943 #endif
945 return 0;
948 static int __init early_init_dt_scan_chosen(unsigned long node,
949 const char *uname, int depth, void *data)
951 u32 *prop;
952 unsigned long *lprop;
953 unsigned long l;
954 char *p;
956 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
958 if (depth != 1 ||
959 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
960 return 0;
962 /* get platform type */
963 prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL);
964 if (prop == NULL)
965 return 0;
966 #ifdef CONFIG_PPC_MULTIPLATFORM
967 _machine = *prop;
968 #endif
970 #ifdef CONFIG_PPC64
971 /* check if iommu is forced on or off */
972 if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
973 iommu_is_off = 1;
974 if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
975 iommu_force_on = 1;
976 #endif
978 lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
979 if (lprop)
980 memory_limit = *lprop;
982 #ifdef CONFIG_PPC64
983 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
984 if (lprop)
985 tce_alloc_start = *lprop;
986 lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
987 if (lprop)
988 tce_alloc_end = *lprop;
989 #endif
991 #ifdef CONFIG_PPC_RTAS
992 /* To help early debugging via the front panel, we retrieve a minimal
993 * set of RTAS infos now if available
996 u64 *basep, *entryp;
998 basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
999 entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1000 prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
1001 if (basep && entryp && prop) {
1002 rtas.base = *basep;
1003 rtas.entry = *entryp;
1004 rtas.size = *prop;
1007 #endif /* CONFIG_PPC_RTAS */
1009 #ifdef CONFIG_KEXEC
1010 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
1011 if (lprop)
1012 crashk_res.start = *lprop;
1014 lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
1015 if (lprop)
1016 crashk_res.end = crashk_res.start + *lprop - 1;
1017 #endif
1019 /* Retreive command line */
1020 p = of_get_flat_dt_prop(node, "bootargs", &l);
1021 if (p != NULL && l > 0)
1022 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
1024 #ifdef CONFIG_CMDLINE
1025 if (l == 0 || (l == 1 && (*p) == 0))
1026 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
1027 #endif /* CONFIG_CMDLINE */
1029 DBG("Command line is: %s\n", cmd_line);
1031 if (strstr(cmd_line, "mem=")) {
1032 char *p, *q;
1033 unsigned long maxmem = 0;
1035 for (q = cmd_line; (p = strstr(q, "mem=")) != 0; ) {
1036 q = p + 4;
1037 if (p > cmd_line && p[-1] != ' ')
1038 continue;
1039 maxmem = simple_strtoul(q, &q, 0);
1040 if (*q == 'k' || *q == 'K') {
1041 maxmem <<= 10;
1042 ++q;
1043 } else if (*q == 'm' || *q == 'M') {
1044 maxmem <<= 20;
1045 ++q;
1046 } else if (*q == 'g' || *q == 'G') {
1047 maxmem <<= 30;
1048 ++q;
1051 memory_limit = maxmem;
1054 /* break now */
1055 return 1;
1058 static int __init early_init_dt_scan_root(unsigned long node,
1059 const char *uname, int depth, void *data)
1061 u32 *prop;
1063 if (depth != 0)
1064 return 0;
1066 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
1067 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1068 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1070 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
1071 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1072 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1074 /* break now */
1075 return 1;
1078 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1080 cell_t *p = *cellp;
1081 unsigned long r;
1083 /* Ignore more than 2 cells */
1084 while (s > sizeof(unsigned long) / 4) {
1085 p++;
1086 s--;
1088 r = *p++;
1089 #ifdef CONFIG_PPC64
1090 if (s > 1) {
1091 r <<= 32;
1092 r |= *(p++);
1093 s--;
1095 #endif
1097 *cellp = p;
1098 return r;
1102 static int __init early_init_dt_scan_memory(unsigned long node,
1103 const char *uname, int depth, void *data)
1105 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
1106 cell_t *reg, *endp;
1107 unsigned long l;
1109 /* We are scanning "memory" nodes only */
1110 if (type == NULL) {
1112 * The longtrail doesn't have a device_type on the
1113 * /memory node, so look for the node called /memory@0.
1115 if (depth != 1 || strcmp(uname, "memory@0") != 0)
1116 return 0;
1117 } else if (strcmp(type, "memory") != 0)
1118 return 0;
1120 reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
1121 if (reg == NULL)
1122 reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
1123 if (reg == NULL)
1124 return 0;
1126 endp = reg + (l / sizeof(cell_t));
1128 DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
1129 uname, l, reg[0], reg[1], reg[2], reg[3]);
1131 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1132 unsigned long base, size;
1134 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1135 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1137 if (size == 0)
1138 continue;
1139 DBG(" - %lx , %lx\n", base, size);
1140 #ifdef CONFIG_PPC64
1141 if (iommu_is_off) {
1142 if (base >= 0x80000000ul)
1143 continue;
1144 if ((base + size) > 0x80000000ul)
1145 size = 0x80000000ul - base;
1147 #endif
1148 lmb_add(base, size);
1150 return 0;
1153 static void __init early_reserve_mem(void)
1155 u64 base, size;
1156 u64 *reserve_map;
1158 reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1159 initial_boot_params->off_mem_rsvmap);
1160 #ifdef CONFIG_PPC32
1162 * Handle the case where we might be booting from an old kexec
1163 * image that setup the mem_rsvmap as pairs of 32-bit values
1165 if (*reserve_map > 0xffffffffull) {
1166 u32 base_32, size_32;
1167 u32 *reserve_map_32 = (u32 *)reserve_map;
1169 while (1) {
1170 base_32 = *(reserve_map_32++);
1171 size_32 = *(reserve_map_32++);
1172 if (size_32 == 0)
1173 break;
1174 DBG("reserving: %x -> %x\n", base_32, size_32);
1175 lmb_reserve(base_32, size_32);
1177 return;
1179 #endif
1180 while (1) {
1181 base = *(reserve_map++);
1182 size = *(reserve_map++);
1183 if (size == 0)
1184 break;
1185 DBG("reserving: %llx -> %llx\n", base, size);
1186 lmb_reserve(base, size);
1189 #if 0
1190 DBG("memory reserved, lmbs :\n");
1191 lmb_dump_all();
1192 #endif
1195 void __init early_init_devtree(void *params)
1197 DBG(" -> early_init_devtree()\n");
1199 /* Setup flat device-tree pointer */
1200 initial_boot_params = params;
1202 /* Retrieve various informations from the /chosen node of the
1203 * device-tree, including the platform type, initrd location and
1204 * size, TCE reserve, and more ...
1206 of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
1208 /* Scan memory nodes and rebuild LMBs */
1209 lmb_init();
1210 of_scan_flat_dt(early_init_dt_scan_root, NULL);
1211 of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1212 lmb_enforce_memory_limit(memory_limit);
1213 lmb_analyze();
1215 DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
1217 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1218 lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1219 #ifdef CONFIG_CRASH_DUMP
1220 lmb_reserve(0, KDUMP_RESERVE_LIMIT);
1221 #endif
1222 early_reserve_mem();
1224 DBG("Scanning CPUs ...\n");
1226 /* Retreive CPU related informations from the flat tree
1227 * (altivec support, boot CPU ID, ...)
1229 of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
1231 DBG(" <- early_init_devtree()\n");
1234 #undef printk
1237 prom_n_addr_cells(struct device_node* np)
1239 int* ip;
1240 do {
1241 if (np->parent)
1242 np = np->parent;
1243 ip = (int *) get_property(np, "#address-cells", NULL);
1244 if (ip != NULL)
1245 return *ip;
1246 } while (np->parent);
1247 /* No #address-cells property for the root node, default to 1 */
1248 return 1;
1250 EXPORT_SYMBOL(prom_n_addr_cells);
1253 prom_n_size_cells(struct device_node* np)
1255 int* ip;
1256 do {
1257 if (np->parent)
1258 np = np->parent;
1259 ip = (int *) get_property(np, "#size-cells", NULL);
1260 if (ip != NULL)
1261 return *ip;
1262 } while (np->parent);
1263 /* No #size-cells property for the root node, default to 1 */
1264 return 1;
1266 EXPORT_SYMBOL(prom_n_size_cells);
1269 * Work out the sense (active-low level / active-high edge)
1270 * of each interrupt from the device tree.
1272 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1274 struct device_node *np;
1275 int i, j;
1277 /* default to level-triggered */
1278 memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
1280 for (np = allnodes; np != 0; np = np->allnext) {
1281 for (j = 0; j < np->n_intrs; j++) {
1282 i = np->intrs[j].line;
1283 if (i >= off && i < max)
1284 senses[i-off] = np->intrs[j].sense;
1290 * Construct and return a list of the device_nodes with a given name.
1292 struct device_node *find_devices(const char *name)
1294 struct device_node *head, **prevp, *np;
1296 prevp = &head;
1297 for (np = allnodes; np != 0; np = np->allnext) {
1298 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1299 *prevp = np;
1300 prevp = &np->next;
1303 *prevp = NULL;
1304 return head;
1306 EXPORT_SYMBOL(find_devices);
1309 * Construct and return a list of the device_nodes with a given type.
1311 struct device_node *find_type_devices(const char *type)
1313 struct device_node *head, **prevp, *np;
1315 prevp = &head;
1316 for (np = allnodes; np != 0; np = np->allnext) {
1317 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1318 *prevp = np;
1319 prevp = &np->next;
1322 *prevp = NULL;
1323 return head;
1325 EXPORT_SYMBOL(find_type_devices);
1328 * Returns all nodes linked together
1330 struct device_node *find_all_nodes(void)
1332 struct device_node *head, **prevp, *np;
1334 prevp = &head;
1335 for (np = allnodes; np != 0; np = np->allnext) {
1336 *prevp = np;
1337 prevp = &np->next;
1339 *prevp = NULL;
1340 return head;
1342 EXPORT_SYMBOL(find_all_nodes);
1344 /** Checks if the given "compat" string matches one of the strings in
1345 * the device's "compatible" property
1347 int device_is_compatible(struct device_node *device, const char *compat)
1349 const char* cp;
1350 int cplen, l;
1352 cp = (char *) get_property(device, "compatible", &cplen);
1353 if (cp == NULL)
1354 return 0;
1355 while (cplen > 0) {
1356 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1357 return 1;
1358 l = strlen(cp) + 1;
1359 cp += l;
1360 cplen -= l;
1363 return 0;
1365 EXPORT_SYMBOL(device_is_compatible);
1369 * Indicates whether the root node has a given value in its
1370 * compatible property.
1372 int machine_is_compatible(const char *compat)
1374 struct device_node *root;
1375 int rc = 0;
1377 root = of_find_node_by_path("/");
1378 if (root) {
1379 rc = device_is_compatible(root, compat);
1380 of_node_put(root);
1382 return rc;
1384 EXPORT_SYMBOL(machine_is_compatible);
1387 * Construct and return a list of the device_nodes with a given type
1388 * and compatible property.
1390 struct device_node *find_compatible_devices(const char *type,
1391 const char *compat)
1393 struct device_node *head, **prevp, *np;
1395 prevp = &head;
1396 for (np = allnodes; np != 0; np = np->allnext) {
1397 if (type != NULL
1398 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1399 continue;
1400 if (device_is_compatible(np, compat)) {
1401 *prevp = np;
1402 prevp = &np->next;
1405 *prevp = NULL;
1406 return head;
1408 EXPORT_SYMBOL(find_compatible_devices);
1411 * Find the device_node with a given full_name.
1413 struct device_node *find_path_device(const char *path)
1415 struct device_node *np;
1417 for (np = allnodes; np != 0; np = np->allnext)
1418 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1419 return np;
1420 return NULL;
1422 EXPORT_SYMBOL(find_path_device);
1424 /*******
1426 * New implementation of the OF "find" APIs, return a refcounted
1427 * object, call of_node_put() when done. The device tree and list
1428 * are protected by a rw_lock.
1430 * Note that property management will need some locking as well,
1431 * this isn't dealt with yet.
1433 *******/
1436 * of_find_node_by_name - Find a node by its "name" property
1437 * @from: The node to start searching from or NULL, the node
1438 * you pass will not be searched, only the next one
1439 * will; typically, you pass what the previous call
1440 * returned. of_node_put() will be called on it
1441 * @name: The name string to match against
1443 * Returns a node pointer with refcount incremented, use
1444 * of_node_put() on it when done.
1446 struct device_node *of_find_node_by_name(struct device_node *from,
1447 const char *name)
1449 struct device_node *np;
1451 read_lock(&devtree_lock);
1452 np = from ? from->allnext : allnodes;
1453 for (; np != NULL; np = np->allnext)
1454 if (np->name != NULL && strcasecmp(np->name, name) == 0
1455 && of_node_get(np))
1456 break;
1457 if (from)
1458 of_node_put(from);
1459 read_unlock(&devtree_lock);
1460 return np;
1462 EXPORT_SYMBOL(of_find_node_by_name);
1465 * of_find_node_by_type - Find a node by its "device_type" property
1466 * @from: The node to start searching from or NULL, the node
1467 * you pass will not be searched, only the next one
1468 * will; typically, you pass what the previous call
1469 * returned. of_node_put() will be called on it
1470 * @name: The type string to match against
1472 * Returns a node pointer with refcount incremented, use
1473 * of_node_put() on it when done.
1475 struct device_node *of_find_node_by_type(struct device_node *from,
1476 const char *type)
1478 struct device_node *np;
1480 read_lock(&devtree_lock);
1481 np = from ? from->allnext : allnodes;
1482 for (; np != 0; np = np->allnext)
1483 if (np->type != 0 && strcasecmp(np->type, type) == 0
1484 && of_node_get(np))
1485 break;
1486 if (from)
1487 of_node_put(from);
1488 read_unlock(&devtree_lock);
1489 return np;
1491 EXPORT_SYMBOL(of_find_node_by_type);
1494 * of_find_compatible_node - Find a node based on type and one of the
1495 * tokens in its "compatible" property
1496 * @from: The node to start searching from or NULL, the node
1497 * you pass will not be searched, only the next one
1498 * will; typically, you pass what the previous call
1499 * returned. of_node_put() will be called on it
1500 * @type: The type string to match "device_type" or NULL to ignore
1501 * @compatible: The string to match to one of the tokens in the device
1502 * "compatible" list.
1504 * Returns a node pointer with refcount incremented, use
1505 * of_node_put() on it when done.
1507 struct device_node *of_find_compatible_node(struct device_node *from,
1508 const char *type, const char *compatible)
1510 struct device_node *np;
1512 read_lock(&devtree_lock);
1513 np = from ? from->allnext : allnodes;
1514 for (; np != 0; np = np->allnext) {
1515 if (type != NULL
1516 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1517 continue;
1518 if (device_is_compatible(np, compatible) && of_node_get(np))
1519 break;
1521 if (from)
1522 of_node_put(from);
1523 read_unlock(&devtree_lock);
1524 return np;
1526 EXPORT_SYMBOL(of_find_compatible_node);
1529 * of_find_node_by_path - Find a node matching a full OF path
1530 * @path: The full path to match
1532 * Returns a node pointer with refcount incremented, use
1533 * of_node_put() on it when done.
1535 struct device_node *of_find_node_by_path(const char *path)
1537 struct device_node *np = allnodes;
1539 read_lock(&devtree_lock);
1540 for (; np != 0; np = np->allnext) {
1541 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1542 && of_node_get(np))
1543 break;
1545 read_unlock(&devtree_lock);
1546 return np;
1548 EXPORT_SYMBOL(of_find_node_by_path);
1551 * of_find_node_by_phandle - Find a node given a phandle
1552 * @handle: phandle of the node to find
1554 * Returns a node pointer with refcount incremented, use
1555 * of_node_put() on it when done.
1557 struct device_node *of_find_node_by_phandle(phandle handle)
1559 struct device_node *np;
1561 read_lock(&devtree_lock);
1562 for (np = allnodes; np != 0; np = np->allnext)
1563 if (np->linux_phandle == handle)
1564 break;
1565 if (np)
1566 of_node_get(np);
1567 read_unlock(&devtree_lock);
1568 return np;
1570 EXPORT_SYMBOL(of_find_node_by_phandle);
1573 * of_find_all_nodes - Get next node in global list
1574 * @prev: Previous node or NULL to start iteration
1575 * of_node_put() will be called on it
1577 * Returns a node pointer with refcount incremented, use
1578 * of_node_put() on it when done.
1580 struct device_node *of_find_all_nodes(struct device_node *prev)
1582 struct device_node *np;
1584 read_lock(&devtree_lock);
1585 np = prev ? prev->allnext : allnodes;
1586 for (; np != 0; np = np->allnext)
1587 if (of_node_get(np))
1588 break;
1589 if (prev)
1590 of_node_put(prev);
1591 read_unlock(&devtree_lock);
1592 return np;
1594 EXPORT_SYMBOL(of_find_all_nodes);
1597 * of_get_parent - Get a node's parent if any
1598 * @node: Node to get parent
1600 * Returns a node pointer with refcount incremented, use
1601 * of_node_put() on it when done.
1603 struct device_node *of_get_parent(const struct device_node *node)
1605 struct device_node *np;
1607 if (!node)
1608 return NULL;
1610 read_lock(&devtree_lock);
1611 np = of_node_get(node->parent);
1612 read_unlock(&devtree_lock);
1613 return np;
1615 EXPORT_SYMBOL(of_get_parent);
1618 * of_get_next_child - Iterate a node childs
1619 * @node: parent node
1620 * @prev: previous child of the parent node, or NULL to get first
1622 * Returns a node pointer with refcount incremented, use
1623 * of_node_put() on it when done.
1625 struct device_node *of_get_next_child(const struct device_node *node,
1626 struct device_node *prev)
1628 struct device_node *next;
1630 read_lock(&devtree_lock);
1631 next = prev ? prev->sibling : node->child;
1632 for (; next != 0; next = next->sibling)
1633 if (of_node_get(next))
1634 break;
1635 if (prev)
1636 of_node_put(prev);
1637 read_unlock(&devtree_lock);
1638 return next;
1640 EXPORT_SYMBOL(of_get_next_child);
1643 * of_node_get - Increment refcount of a node
1644 * @node: Node to inc refcount, NULL is supported to
1645 * simplify writing of callers
1647 * Returns node.
1649 struct device_node *of_node_get(struct device_node *node)
1651 if (node)
1652 kref_get(&node->kref);
1653 return node;
1655 EXPORT_SYMBOL(of_node_get);
1657 static inline struct device_node * kref_to_device_node(struct kref *kref)
1659 return container_of(kref, struct device_node, kref);
1663 * of_node_release - release a dynamically allocated node
1664 * @kref: kref element of the node to be released
1666 * In of_node_put() this function is passed to kref_put()
1667 * as the destructor.
1669 static void of_node_release(struct kref *kref)
1671 struct device_node *node = kref_to_device_node(kref);
1672 struct property *prop = node->properties;
1674 if (!OF_IS_DYNAMIC(node))
1675 return;
1676 while (prop) {
1677 struct property *next = prop->next;
1678 kfree(prop->name);
1679 kfree(prop->value);
1680 kfree(prop);
1681 prop = next;
1683 if (!prop) {
1684 prop = node->deadprops;
1685 node->deadprops = NULL;
1688 kfree(node->intrs);
1689 kfree(node->full_name);
1690 kfree(node->data);
1691 kfree(node);
1695 * of_node_put - Decrement refcount of a node
1696 * @node: Node to dec refcount, NULL is supported to
1697 * simplify writing of callers
1700 void of_node_put(struct device_node *node)
1702 if (node)
1703 kref_put(&node->kref, of_node_release);
1705 EXPORT_SYMBOL(of_node_put);
1708 * Plug a device node into the tree and global list.
1710 void of_attach_node(struct device_node *np)
1712 write_lock(&devtree_lock);
1713 np->sibling = np->parent->child;
1714 np->allnext = allnodes;
1715 np->parent->child = np;
1716 allnodes = np;
1717 write_unlock(&devtree_lock);
1721 * "Unplug" a node from the device tree. The caller must hold
1722 * a reference to the node. The memory associated with the node
1723 * is not freed until its refcount goes to zero.
1725 void of_detach_node(const struct device_node *np)
1727 struct device_node *parent;
1729 write_lock(&devtree_lock);
1731 parent = np->parent;
1733 if (allnodes == np)
1734 allnodes = np->allnext;
1735 else {
1736 struct device_node *prev;
1737 for (prev = allnodes;
1738 prev->allnext != np;
1739 prev = prev->allnext)
1741 prev->allnext = np->allnext;
1744 if (parent->child == np)
1745 parent->child = np->sibling;
1746 else {
1747 struct device_node *prevsib;
1748 for (prevsib = np->parent->child;
1749 prevsib->sibling != np;
1750 prevsib = prevsib->sibling)
1752 prevsib->sibling = np->sibling;
1755 write_unlock(&devtree_lock);
1758 #ifdef CONFIG_PPC_PSERIES
1760 * Fix up the uninitialized fields in a new device node:
1761 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1763 * A lot of boot-time code is duplicated here, because functions such
1764 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1765 * slab allocator.
1767 * This should probably be split up into smaller chunks.
1770 static int of_finish_dynamic_node(struct device_node *node)
1772 struct device_node *parent = of_get_parent(node);
1773 int err = 0;
1774 phandle *ibm_phandle;
1776 node->name = get_property(node, "name", NULL);
1777 node->type = get_property(node, "device_type", NULL);
1779 if (!parent) {
1780 err = -ENODEV;
1781 goto out;
1784 /* We don't support that function on PowerMac, at least
1785 * not yet
1787 if (_machine == PLATFORM_POWERMAC)
1788 return -ENODEV;
1790 /* fix up new node's linux_phandle field */
1791 if ((ibm_phandle = (unsigned int *)get_property(node,
1792 "ibm,phandle", NULL)))
1793 node->linux_phandle = *ibm_phandle;
1795 out:
1796 of_node_put(parent);
1797 return err;
1800 static int prom_reconfig_notifier(struct notifier_block *nb,
1801 unsigned long action, void *node)
1803 int err;
1805 switch (action) {
1806 case PSERIES_RECONFIG_ADD:
1807 err = of_finish_dynamic_node(node);
1808 if (!err)
1809 finish_node(node, NULL, 0);
1810 if (err < 0) {
1811 printk(KERN_ERR "finish_node returned %d\n", err);
1812 err = NOTIFY_BAD;
1814 break;
1815 default:
1816 err = NOTIFY_DONE;
1817 break;
1819 return err;
1822 static struct notifier_block prom_reconfig_nb = {
1823 .notifier_call = prom_reconfig_notifier,
1824 .priority = 10, /* This one needs to run first */
1827 static int __init prom_reconfig_setup(void)
1829 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1831 __initcall(prom_reconfig_setup);
1832 #endif
1834 struct property *of_find_property(struct device_node *np, const char *name,
1835 int *lenp)
1837 struct property *pp;
1839 read_lock(&devtree_lock);
1840 for (pp = np->properties; pp != 0; pp = pp->next)
1841 if (strcmp(pp->name, name) == 0) {
1842 if (lenp != 0)
1843 *lenp = pp->length;
1844 break;
1846 read_unlock(&devtree_lock);
1848 return pp;
1852 * Find a property with a given name for a given node
1853 * and return the value.
1855 unsigned char *get_property(struct device_node *np, const char *name,
1856 int *lenp)
1858 struct property *pp = of_find_property(np,name,lenp);
1859 return pp ? pp->value : NULL;
1861 EXPORT_SYMBOL(get_property);
1864 * Add a property to a node
1866 int prom_add_property(struct device_node* np, struct property* prop)
1868 struct property **next;
1870 prop->next = NULL;
1871 write_lock(&devtree_lock);
1872 next = &np->properties;
1873 while (*next) {
1874 if (strcmp(prop->name, (*next)->name) == 0) {
1875 /* duplicate ! don't insert it */
1876 write_unlock(&devtree_lock);
1877 return -1;
1879 next = &(*next)->next;
1881 *next = prop;
1882 write_unlock(&devtree_lock);
1884 #ifdef CONFIG_PROC_DEVICETREE
1885 /* try to add to proc as well if it was initialized */
1886 if (np->pde)
1887 proc_device_tree_add_prop(np->pde, prop);
1888 #endif /* CONFIG_PROC_DEVICETREE */
1890 return 0;
1894 * Remove a property from a node. Note that we don't actually
1895 * remove it, since we have given out who-knows-how-many pointers
1896 * to the data using get-property. Instead we just move the property
1897 * to the "dead properties" list, so it won't be found any more.
1899 int prom_remove_property(struct device_node *np, struct property *prop)
1901 struct property **next;
1902 int found = 0;
1904 write_lock(&devtree_lock);
1905 next = &np->properties;
1906 while (*next) {
1907 if (*next == prop) {
1908 /* found the node */
1909 *next = prop->next;
1910 prop->next = np->deadprops;
1911 np->deadprops = prop;
1912 found = 1;
1913 break;
1915 next = &(*next)->next;
1917 write_unlock(&devtree_lock);
1919 if (!found)
1920 return -ENODEV;
1922 #ifdef CONFIG_PROC_DEVICETREE
1923 /* try to remove the proc node as well */
1924 if (np->pde)
1925 proc_device_tree_remove_prop(np->pde, prop);
1926 #endif /* CONFIG_PROC_DEVICETREE */
1928 return 0;
1932 * Update a property in a node. Note that we don't actually
1933 * remove it, since we have given out who-knows-how-many pointers
1934 * to the data using get-property. Instead we just move the property
1935 * to the "dead properties" list, and add the new property to the
1936 * property list
1938 int prom_update_property(struct device_node *np,
1939 struct property *newprop,
1940 struct property *oldprop)
1942 struct property **next;
1943 int found = 0;
1945 write_lock(&devtree_lock);
1946 next = &np->properties;
1947 while (*next) {
1948 if (*next == oldprop) {
1949 /* found the node */
1950 newprop->next = oldprop->next;
1951 *next = newprop;
1952 oldprop->next = np->deadprops;
1953 np->deadprops = oldprop;
1954 found = 1;
1955 break;
1957 next = &(*next)->next;
1959 write_unlock(&devtree_lock);
1961 if (!found)
1962 return -ENODEV;
1964 #ifdef CONFIG_PROC_DEVICETREE
1965 /* try to add to proc as well if it was initialized */
1966 if (np->pde)
1967 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1968 #endif /* CONFIG_PROC_DEVICETREE */
1970 return 0;
1973 #ifdef CONFIG_KEXEC
1974 /* We may have allocated the flat device tree inside the crash kernel region
1975 * in prom_init. If so we need to move it out into regular memory. */
1976 void kdump_move_device_tree(void)
1978 unsigned long start, end;
1979 struct boot_param_header *new;
1981 start = __pa((unsigned long)initial_boot_params);
1982 end = start + initial_boot_params->totalsize;
1984 if (end < crashk_res.start || start > crashk_res.end)
1985 return;
1987 new = (struct boot_param_header*)
1988 __va(lmb_alloc(initial_boot_params->totalsize, PAGE_SIZE));
1990 memcpy(new, initial_boot_params, initial_boot_params->totalsize);
1992 initial_boot_params = new;
1994 DBG("Flat device tree blob moved to %p\n", initial_boot_params);
1996 /* XXX should we unreserve the old DT? */
1998 #endif /* CONFIG_KEXEC */