[PATCH] ppc64: Detect altivec via firmware on unknown CPUs
[firewire-audio.git] / arch / ppc64 / kernel / prom.c
blobb08aac68baff4a3c2b59f3337ea4b98cf60b64a3
1 /*
2 *
4 * Procedures for interfacing to Open Firmware.
6 * Paul Mackerras August 1996.
7 * Copyright (C) 1996 Paul Mackerras.
8 *
9 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
10 * {engebret|bergner}@us.ibm.com
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #undef DEBUG
20 #include <stdarg.h>
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/version.h>
26 #include <linux/threads.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/stringify.h>
31 #include <linux/delay.h>
32 #include <linux/initrd.h>
33 #include <linux/bitops.h>
34 #include <linux/module.h>
36 #include <asm/prom.h>
37 #include <asm/rtas.h>
38 #include <asm/lmb.h>
39 #include <asm/abs_addr.h>
40 #include <asm/page.h>
41 #include <asm/processor.h>
42 #include <asm/irq.h>
43 #include <asm/io.h>
44 #include <asm/smp.h>
45 #include <asm/system.h>
46 #include <asm/mmu.h>
47 #include <asm/pgtable.h>
48 #include <asm/pci.h>
49 #include <asm/iommu.h>
50 #include <asm/bootinfo.h>
51 #include <asm/ppcdebug.h>
52 #include <asm/btext.h>
53 #include <asm/sections.h>
54 #include <asm/machdep.h>
55 #include <asm/pSeries_reconfig.h>
57 #ifdef DEBUG
58 #define DBG(fmt...) udbg_printf(fmt)
59 #else
60 #define DBG(fmt...)
61 #endif
63 struct pci_reg_property {
64 struct pci_address addr;
65 u32 size_hi;
66 u32 size_lo;
69 struct isa_reg_property {
70 u32 space;
71 u32 address;
72 u32 size;
76 typedef int interpret_func(struct device_node *, unsigned long *,
77 int, int, int);
79 extern struct rtas_t rtas;
80 extern struct lmb lmb;
81 extern unsigned long klimit;
83 static int __initdata dt_root_addr_cells;
84 static int __initdata dt_root_size_cells;
85 static int __initdata iommu_is_off;
86 int __initdata iommu_force_on;
87 typedef u32 cell_t;
89 #if 0
90 static struct boot_param_header *initial_boot_params __initdata;
91 #else
92 struct boot_param_header *initial_boot_params;
93 #endif
95 static struct device_node *allnodes = NULL;
97 /* use when traversing tree through the allnext, child, sibling,
98 * or parent members of struct device_node.
100 static DEFINE_RWLOCK(devtree_lock);
102 /* export that to outside world */
103 struct device_node *of_chosen;
106 * Wrapper for allocating memory for various data that needs to be
107 * attached to device nodes as they are processed at boot or when
108 * added to the device tree later (e.g. DLPAR). At boot there is
109 * already a region reserved so we just increment *mem_start by size;
110 * otherwise we call kmalloc.
112 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
114 unsigned long tmp;
116 if (!mem_start)
117 return kmalloc(size, GFP_KERNEL);
119 tmp = *mem_start;
120 *mem_start += size;
121 return (void *)tmp;
125 * Find the device_node with a given phandle.
127 static struct device_node * find_phandle(phandle ph)
129 struct device_node *np;
131 for (np = allnodes; np != 0; np = np->allnext)
132 if (np->linux_phandle == ph)
133 return np;
134 return NULL;
138 * Find the interrupt parent of a node.
140 static struct device_node * __devinit intr_parent(struct device_node *p)
142 phandle *parp;
144 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
145 if (parp == NULL)
146 return p->parent;
147 return find_phandle(*parp);
151 * Find out the size of each entry of the interrupts property
152 * for a node.
154 int __devinit prom_n_intr_cells(struct device_node *np)
156 struct device_node *p;
157 unsigned int *icp;
159 for (p = np; (p = intr_parent(p)) != NULL; ) {
160 icp = (unsigned int *)
161 get_property(p, "#interrupt-cells", NULL);
162 if (icp != NULL)
163 return *icp;
164 if (get_property(p, "interrupt-controller", NULL) != NULL
165 || get_property(p, "interrupt-map", NULL) != NULL) {
166 printk("oops, node %s doesn't have #interrupt-cells\n",
167 p->full_name);
168 return 1;
171 #ifdef DEBUG_IRQ
172 printk("prom_n_intr_cells failed for %s\n", np->full_name);
173 #endif
174 return 1;
178 * Map an interrupt from a device up to the platform interrupt
179 * descriptor.
181 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
182 struct device_node *np, unsigned int *ints,
183 int nintrc)
185 struct device_node *p, *ipar;
186 unsigned int *imap, *imask, *ip;
187 int i, imaplen, match;
188 int newintrc = 0, newaddrc = 0;
189 unsigned int *reg;
190 int naddrc;
192 reg = (unsigned int *) get_property(np, "reg", NULL);
193 naddrc = prom_n_addr_cells(np);
194 p = intr_parent(np);
195 while (p != NULL) {
196 if (get_property(p, "interrupt-controller", NULL) != NULL)
197 /* this node is an interrupt controller, stop here */
198 break;
199 imap = (unsigned int *)
200 get_property(p, "interrupt-map", &imaplen);
201 if (imap == NULL) {
202 p = intr_parent(p);
203 continue;
205 imask = (unsigned int *)
206 get_property(p, "interrupt-map-mask", NULL);
207 if (imask == NULL) {
208 printk("oops, %s has interrupt-map but no mask\n",
209 p->full_name);
210 return 0;
212 imaplen /= sizeof(unsigned int);
213 match = 0;
214 ipar = NULL;
215 while (imaplen > 0 && !match) {
216 /* check the child-interrupt field */
217 match = 1;
218 for (i = 0; i < naddrc && match; ++i)
219 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
220 for (; i < naddrc + nintrc && match; ++i)
221 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
222 imap += naddrc + nintrc;
223 imaplen -= naddrc + nintrc;
224 /* grab the interrupt parent */
225 ipar = find_phandle((phandle) *imap++);
226 --imaplen;
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 int __devinit finish_node_interrupts(struct device_node *np,
278 unsigned long *mem_start,
279 int measure_only)
281 unsigned int *ints;
282 int intlen, intrcells, intrcount;
283 int i, j, n;
284 unsigned int *irq, virq;
285 struct device_node *ic;
287 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
288 if (ints == NULL)
289 return 0;
290 intrcells = prom_n_intr_cells(np);
291 intlen /= intrcells * sizeof(unsigned int);
293 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
294 if (!np->intrs)
295 return -ENOMEM;
297 if (measure_only)
298 return 0;
300 intrcount = 0;
301 for (i = 0; i < intlen; ++i, ints += intrcells) {
302 n = map_interrupt(&irq, &ic, np, ints, intrcells);
303 if (n <= 0)
304 continue;
306 /* don't map IRQ numbers under a cascaded 8259 controller */
307 if (ic && device_is_compatible(ic, "chrp,iic")) {
308 np->intrs[intrcount].line = irq[0];
309 } else {
310 virq = virt_irq_create_mapping(irq[0]);
311 if (virq == NO_IRQ) {
312 printk(KERN_CRIT "Could not allocate interrupt"
313 " number for %s\n", np->full_name);
314 continue;
316 np->intrs[intrcount].line = irq_offset_up(virq);
319 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
320 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
321 char *name = get_property(ic->parent, "name", NULL);
322 if (name && !strcmp(name, "u3"))
323 np->intrs[intrcount].line += 128;
325 np->intrs[intrcount].sense = 1;
326 if (n > 1)
327 np->intrs[intrcount].sense = irq[1];
328 if (n > 2) {
329 printk("hmmm, got %d intr cells for %s:", n,
330 np->full_name);
331 for (j = 0; j < n; ++j)
332 printk(" %d", irq[j]);
333 printk("\n");
335 ++intrcount;
337 np->n_intrs = intrcount;
339 return 0;
342 static int __devinit interpret_pci_props(struct device_node *np,
343 unsigned long *mem_start,
344 int naddrc, int nsizec,
345 int measure_only)
347 struct address_range *adr;
348 struct pci_reg_property *pci_addrs;
349 int i, l, n_addrs;
351 pci_addrs = (struct pci_reg_property *)
352 get_property(np, "assigned-addresses", &l);
353 if (!pci_addrs)
354 return 0;
356 n_addrs = l / sizeof(*pci_addrs);
358 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
359 if (!adr)
360 return -ENOMEM;
362 if (measure_only)
363 return 0;
365 np->addrs = adr;
366 np->n_addrs = n_addrs;
368 for (i = 0; i < n_addrs; i++) {
369 adr[i].space = pci_addrs[i].addr.a_hi;
370 adr[i].address = pci_addrs[i].addr.a_lo |
371 ((u64)pci_addrs[i].addr.a_mid << 32);
372 adr[i].size = pci_addrs[i].size_lo;
375 return 0;
378 static int __init interpret_dbdma_props(struct device_node *np,
379 unsigned long *mem_start,
380 int naddrc, int nsizec,
381 int measure_only)
383 struct reg_property32 *rp;
384 struct address_range *adr;
385 unsigned long base_address;
386 int i, l;
387 struct device_node *db;
389 base_address = 0;
390 if (!measure_only) {
391 for (db = np->parent; db != NULL; db = db->parent) {
392 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
393 base_address = db->addrs[0].address;
394 break;
399 rp = (struct reg_property32 *) get_property(np, "reg", &l);
400 if (rp != 0 && l >= sizeof(struct reg_property32)) {
401 i = 0;
402 adr = (struct address_range *) (*mem_start);
403 while ((l -= sizeof(struct reg_property32)) >= 0) {
404 if (!measure_only) {
405 adr[i].space = 2;
406 adr[i].address = rp[i].address + base_address;
407 adr[i].size = rp[i].size;
409 ++i;
411 np->addrs = adr;
412 np->n_addrs = i;
413 (*mem_start) += i * sizeof(struct address_range);
416 return 0;
419 static int __init interpret_macio_props(struct device_node *np,
420 unsigned long *mem_start,
421 int naddrc, int nsizec,
422 int measure_only)
424 struct reg_property32 *rp;
425 struct address_range *adr;
426 unsigned long base_address;
427 int i, l;
428 struct device_node *db;
430 base_address = 0;
431 if (!measure_only) {
432 for (db = np->parent; db != NULL; db = db->parent) {
433 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
434 base_address = db->addrs[0].address;
435 break;
440 rp = (struct reg_property32 *) get_property(np, "reg", &l);
441 if (rp != 0 && l >= sizeof(struct reg_property32)) {
442 i = 0;
443 adr = (struct address_range *) (*mem_start);
444 while ((l -= sizeof(struct reg_property32)) >= 0) {
445 if (!measure_only) {
446 adr[i].space = 2;
447 adr[i].address = rp[i].address + base_address;
448 adr[i].size = rp[i].size;
450 ++i;
452 np->addrs = adr;
453 np->n_addrs = i;
454 (*mem_start) += i * sizeof(struct address_range);
457 return 0;
460 static int __init interpret_isa_props(struct device_node *np,
461 unsigned long *mem_start,
462 int naddrc, int nsizec,
463 int measure_only)
465 struct isa_reg_property *rp;
466 struct address_range *adr;
467 int i, l;
469 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
470 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
471 i = 0;
472 adr = (struct address_range *) (*mem_start);
473 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
474 if (!measure_only) {
475 adr[i].space = rp[i].space;
476 adr[i].address = rp[i].address;
477 adr[i].size = rp[i].size;
479 ++i;
481 np->addrs = adr;
482 np->n_addrs = i;
483 (*mem_start) += i * sizeof(struct address_range);
486 return 0;
489 static int __init interpret_root_props(struct device_node *np,
490 unsigned long *mem_start,
491 int naddrc, int nsizec,
492 int measure_only)
494 struct address_range *adr;
495 int i, l;
496 unsigned int *rp;
497 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
499 rp = (unsigned int *) get_property(np, "reg", &l);
500 if (rp != 0 && l >= rpsize) {
501 i = 0;
502 adr = (struct address_range *) (*mem_start);
503 while ((l -= rpsize) >= 0) {
504 if (!measure_only) {
505 adr[i].space = 0;
506 adr[i].address = rp[naddrc - 1];
507 adr[i].size = rp[naddrc + nsizec - 1];
509 ++i;
510 rp += naddrc + nsizec;
512 np->addrs = adr;
513 np->n_addrs = i;
514 (*mem_start) += i * sizeof(struct address_range);
517 return 0;
520 static int __devinit finish_node(struct device_node *np,
521 unsigned long *mem_start,
522 interpret_func *ifunc,
523 int naddrc, int nsizec,
524 int measure_only)
526 struct device_node *child;
527 int *ip, rc = 0;
529 /* get the device addresses and interrupts */
530 if (ifunc != NULL)
531 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
532 if (rc)
533 goto out;
535 rc = finish_node_interrupts(np, mem_start, measure_only);
536 if (rc)
537 goto out;
539 /* Look for #address-cells and #size-cells properties. */
540 ip = (int *) get_property(np, "#address-cells", NULL);
541 if (ip != NULL)
542 naddrc = *ip;
543 ip = (int *) get_property(np, "#size-cells", NULL);
544 if (ip != NULL)
545 nsizec = *ip;
547 /* the f50 sets the name to 'display' and 'compatible' to what we
548 * expect for the name -- Cort
550 if (!strcmp(np->name, "display"))
551 np->name = get_property(np, "compatible", NULL);
553 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
554 ifunc = interpret_root_props;
555 else if (np->type == 0)
556 ifunc = NULL;
557 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
558 ifunc = interpret_pci_props;
559 else if (!strcmp(np->type, "dbdma"))
560 ifunc = interpret_dbdma_props;
561 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
562 ifunc = interpret_macio_props;
563 else if (!strcmp(np->type, "isa"))
564 ifunc = interpret_isa_props;
565 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
566 ifunc = interpret_root_props;
567 else if (!((ifunc == interpret_dbdma_props
568 || ifunc == interpret_macio_props)
569 && (!strcmp(np->type, "escc")
570 || !strcmp(np->type, "media-bay"))))
571 ifunc = NULL;
573 for (child = np->child; child != NULL; child = child->sibling) {
574 rc = finish_node(child, mem_start, ifunc,
575 naddrc, nsizec, measure_only);
576 if (rc)
577 goto out;
579 out:
580 return rc;
584 * finish_device_tree is called once things are running normally
585 * (i.e. with text and data mapped to the address they were linked at).
586 * It traverses the device tree and fills in some of the additional,
587 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
588 * mapping is also initialized at this point.
590 void __init finish_device_tree(void)
592 unsigned long start, end, size = 0;
594 DBG(" -> finish_device_tree\n");
596 if (ppc64_interrupt_controller == IC_INVALID) {
597 DBG("failed to configure interrupt controller type\n");
598 panic("failed to configure interrupt controller type\n");
601 /* Initialize virtual IRQ map */
602 virt_irq_init();
605 * Finish device-tree (pre-parsing some properties etc...)
606 * We do this in 2 passes. One with "measure_only" set, which
607 * will only measure the amount of memory needed, then we can
608 * allocate that memory, and call finish_node again. However,
609 * we must be careful as most routines will fail nowadays when
610 * prom_alloc() returns 0, so we must make sure our first pass
611 * doesn't start at 0. We pre-initialize size to 16 for that
612 * reason and then remove those additional 16 bytes
614 size = 16;
615 finish_node(allnodes, &size, NULL, 0, 0, 1);
616 size -= 16;
617 end = start = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
618 finish_node(allnodes, &end, NULL, 0, 0, 0);
619 BUG_ON(end != start + size);
621 DBG(" <- finish_device_tree\n");
624 #ifdef DEBUG
625 #define printk udbg_printf
626 #endif
628 static inline char *find_flat_dt_string(u32 offset)
630 return ((char *)initial_boot_params) + initial_boot_params->off_dt_strings
631 + offset;
635 * This function is used to scan the flattened device-tree, it is
636 * used to extract the memory informations at boot before we can
637 * unflatten the tree
639 static int __init scan_flat_dt(int (*it)(unsigned long node,
640 const char *full_path, void *data),
641 void *data)
643 unsigned long p = ((unsigned long)initial_boot_params) +
644 initial_boot_params->off_dt_struct;
645 int rc = 0;
647 do {
648 u32 tag = *((u32 *)p);
649 char *pathp;
651 p += 4;
652 if (tag == OF_DT_END_NODE)
653 continue;
654 if (tag == OF_DT_END)
655 break;
656 if (tag == OF_DT_PROP) {
657 u32 sz = *((u32 *)p);
658 p += 8;
659 p = _ALIGN(p, sz >= 8 ? 8 : 4);
660 p += sz;
661 p = _ALIGN(p, 4);
662 continue;
664 if (tag != OF_DT_BEGIN_NODE) {
665 printk(KERN_WARNING "Invalid tag %x scanning flattened"
666 " device tree !\n", tag);
667 return -EINVAL;
669 pathp = (char *)p;
670 p = _ALIGN(p + strlen(pathp) + 1, 4);
671 rc = it(p, pathp, data);
672 if (rc != 0)
673 break;
674 } while(1);
676 return rc;
680 * This function can be used within scan_flattened_dt callback to get
681 * access to properties
683 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
684 unsigned long *size)
686 unsigned long p = node;
688 do {
689 u32 tag = *((u32 *)p);
690 u32 sz, noff;
691 const char *nstr;
693 p += 4;
694 if (tag != OF_DT_PROP)
695 return NULL;
697 sz = *((u32 *)p);
698 noff = *((u32 *)(p + 4));
699 p += 8;
700 p = _ALIGN(p, sz >= 8 ? 8 : 4);
702 nstr = find_flat_dt_string(noff);
703 if (nstr == NULL) {
704 printk(KERN_WARNING "Can't find property index name !\n");
705 return NULL;
707 if (strcmp(name, nstr) == 0) {
708 if (size)
709 *size = sz;
710 return (void *)p;
712 p += sz;
713 p = _ALIGN(p, 4);
714 } while(1);
717 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
718 unsigned long align)
720 void *res;
722 *mem = _ALIGN(*mem, align);
723 res = (void *)*mem;
724 *mem += size;
726 return res;
729 static unsigned long __init unflatten_dt_node(unsigned long mem,
730 unsigned long *p,
731 struct device_node *dad,
732 struct device_node ***allnextpp)
734 struct device_node *np;
735 struct property *pp, **prev_pp = NULL;
736 char *pathp;
737 u32 tag;
738 unsigned int l;
740 tag = *((u32 *)(*p));
741 if (tag != OF_DT_BEGIN_NODE) {
742 printk("Weird tag at start of node: %x\n", tag);
743 return mem;
745 *p += 4;
746 pathp = (char *)*p;
747 l = strlen(pathp) + 1;
748 *p = _ALIGN(*p + l, 4);
750 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + l,
751 __alignof__(struct device_node));
752 if (allnextpp) {
753 memset(np, 0, sizeof(*np));
754 np->full_name = ((char*)np) + sizeof(struct device_node);
755 memcpy(np->full_name, pathp, l);
756 prev_pp = &np->properties;
757 **allnextpp = np;
758 *allnextpp = &np->allnext;
759 if (dad != NULL) {
760 np->parent = dad;
761 /* we temporarily use the `next' field as `last_child'. */
762 if (dad->next == 0)
763 dad->child = np;
764 else
765 dad->next->sibling = np;
766 dad->next = np;
768 kref_init(&np->kref);
770 while(1) {
771 u32 sz, noff;
772 char *pname;
774 tag = *((u32 *)(*p));
775 if (tag != OF_DT_PROP)
776 break;
777 *p += 4;
778 sz = *((u32 *)(*p));
779 noff = *((u32 *)((*p) + 4));
780 *p = _ALIGN((*p) + 8, sz >= 8 ? 8 : 4);
782 pname = find_flat_dt_string(noff);
783 if (pname == NULL) {
784 printk("Can't find property name in list !\n");
785 break;
787 l = strlen(pname) + 1;
788 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
789 __alignof__(struct property));
790 if (allnextpp) {
791 if (strcmp(pname, "linux,phandle") == 0) {
792 np->node = *((u32 *)*p);
793 if (np->linux_phandle == 0)
794 np->linux_phandle = np->node;
796 if (strcmp(pname, "ibm,phandle") == 0)
797 np->linux_phandle = *((u32 *)*p);
798 pp->name = pname;
799 pp->length = sz;
800 pp->value = (void *)*p;
801 *prev_pp = pp;
802 prev_pp = &pp->next;
804 *p = _ALIGN((*p) + sz, 4);
806 if (allnextpp) {
807 *prev_pp = NULL;
808 np->name = get_property(np, "name", NULL);
809 np->type = get_property(np, "device_type", NULL);
811 if (!np->name)
812 np->name = "<NULL>";
813 if (!np->type)
814 np->type = "<NULL>";
816 while (tag == OF_DT_BEGIN_NODE) {
817 mem = unflatten_dt_node(mem, p, np, allnextpp);
818 tag = *((u32 *)(*p));
820 if (tag != OF_DT_END_NODE) {
821 printk("Weird tag at start of node: %x\n", tag);
822 return mem;
824 *p += 4;
825 return mem;
830 * unflattens the device-tree passed by the firmware, creating the
831 * tree of struct device_node. It also fills the "name" and "type"
832 * pointers of the nodes so the normal device-tree walking functions
833 * can be used (this used to be done by finish_device_tree)
835 void __init unflatten_device_tree(void)
837 unsigned long start, mem, size;
838 struct device_node **allnextp = &allnodes;
839 char *p;
840 int l = 0;
842 DBG(" -> unflatten_device_tree()\n");
844 /* First pass, scan for size */
845 start = ((unsigned long)initial_boot_params) +
846 initial_boot_params->off_dt_struct;
847 size = unflatten_dt_node(0, &start, NULL, NULL);
849 DBG(" size is %lx, allocating...\n", size);
851 /* Allocate memory for the expanded device tree */
852 mem = (unsigned long)abs_to_virt(lmb_alloc(size,
853 __alignof__(struct device_node)));
854 DBG(" unflattening...\n", mem);
856 /* Second pass, do actual unflattening */
857 start = ((unsigned long)initial_boot_params) +
858 initial_boot_params->off_dt_struct;
859 unflatten_dt_node(mem, &start, NULL, &allnextp);
860 if (*((u32 *)start) != OF_DT_END)
861 printk(KERN_WARNING "Weird tag at end of tree: %x\n", *((u32 *)start));
862 *allnextp = NULL;
864 /* Get pointer to OF "/chosen" node for use everywhere */
865 of_chosen = of_find_node_by_path("/chosen");
867 /* Retreive command line */
868 if (of_chosen != NULL) {
869 p = (char *)get_property(of_chosen, "bootargs", &l);
870 if (p != NULL && l > 0)
871 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
873 #ifdef CONFIG_CMDLINE
874 if (l == 0 || (l == 1 && (*p) == 0))
875 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
876 #endif /* CONFIG_CMDLINE */
878 DBG("Command line is: %s\n", cmd_line);
880 DBG(" <- unflatten_device_tree()\n");
884 static int __init early_init_dt_scan_cpus(unsigned long node,
885 const char *full_path, void *data)
887 char *type = get_flat_dt_prop(node, "device_type", NULL);
888 u32 *prop;
890 /* We are scanning "cpu" nodes only */
891 if (type == NULL || strcmp(type, "cpu") != 0)
892 return 0;
894 /* On LPAR, look for the first ibm,pft-size property for the hash table size
896 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
897 u32 *pft_size;
898 pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
899 if (pft_size != NULL) {
900 /* pft_size[0] is the NUMA CEC cookie */
901 ppc64_pft_size = pft_size[1];
905 if (initial_boot_params && initial_boot_params->version >= 2) {
906 /* version 2 of the kexec param format adds the phys cpuid
907 * of booted proc.
909 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
910 boot_cpuid = 0;
911 } else {
912 /* Check if it's the boot-cpu, set it's hw index in paca now */
913 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
914 u32 *prop = get_flat_dt_prop(node, "reg", NULL);
915 set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
916 boot_cpuid_phys = get_hard_smp_processor_id(0);
920 /* Check if we have a VMX and eventually update CPU features */
921 prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
922 if (prop && (*prop) > 0) {
923 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
924 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
927 /* Same goes for Apple's "altivec" property */
928 prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
929 if (prop) {
930 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
931 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
934 return 0;
937 static int __init early_init_dt_scan_chosen(unsigned long node,
938 const char *full_path, void *data)
940 u32 *prop;
941 u64 *prop64;
942 extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
944 if (strcmp(full_path, "/chosen") != 0)
945 return 0;
947 /* get platform type */
948 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
949 if (prop == NULL)
950 return 0;
951 systemcfg->platform = *prop;
953 /* check if iommu is forced on or off */
954 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
955 iommu_is_off = 1;
956 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
957 iommu_force_on = 1;
959 prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
960 if (prop64)
961 memory_limit = *prop64;
963 prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
964 if (prop64)
965 tce_alloc_start = *prop64;
967 prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
968 if (prop64)
969 tce_alloc_end = *prop64;
971 #ifdef CONFIG_PPC_RTAS
972 /* To help early debugging via the front panel, we retreive a minimal
973 * set of RTAS infos now if available
976 u64 *basep, *entryp;
978 basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
979 entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
980 prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
981 if (basep && entryp && prop) {
982 rtas.base = *basep;
983 rtas.entry = *entryp;
984 rtas.size = *prop;
987 #endif /* CONFIG_PPC_RTAS */
989 /* break now */
990 return 1;
993 static int __init early_init_dt_scan_root(unsigned long node,
994 const char *full_path, void *data)
996 u32 *prop;
998 if (strcmp(full_path, "/") != 0)
999 return 0;
1001 prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
1002 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1004 prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
1005 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1007 /* break now */
1008 return 1;
1011 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1013 cell_t *p = *cellp;
1014 unsigned long r = 0;
1016 /* Ignore more than 2 cells */
1017 while (s > 2) {
1018 p++;
1019 s--;
1021 while (s) {
1022 r <<= 32;
1023 r |= *(p++);
1024 s--;
1027 *cellp = p;
1028 return r;
1032 static int __init early_init_dt_scan_memory(unsigned long node,
1033 const char *full_path, void *data)
1035 char *type = get_flat_dt_prop(node, "device_type", NULL);
1036 cell_t *reg, *endp;
1037 unsigned long l;
1039 /* We are scanning "memory" nodes only */
1040 if (type == NULL || strcmp(type, "memory") != 0)
1041 return 0;
1043 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
1044 if (reg == NULL)
1045 return 0;
1047 endp = reg + (l / sizeof(cell_t));
1049 DBG("memory scan node %s ...\n", full_path);
1050 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1051 unsigned long base, size;
1053 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
1054 size = dt_mem_next_cell(dt_root_size_cells, &reg);
1056 if (size == 0)
1057 continue;
1058 DBG(" - %lx , %lx\n", base, size);
1059 if (iommu_is_off) {
1060 if (base >= 0x80000000ul)
1061 continue;
1062 if ((base + size) > 0x80000000ul)
1063 size = 0x80000000ul - base;
1065 lmb_add(base, size);
1067 return 0;
1070 static void __init early_reserve_mem(void)
1072 u64 base, size;
1073 u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1074 initial_boot_params->off_mem_rsvmap);
1075 while (1) {
1076 base = *(reserve_map++);
1077 size = *(reserve_map++);
1078 if (size == 0)
1079 break;
1080 DBG("reserving: %lx -> %lx\n", base, size);
1081 lmb_reserve(base, size);
1084 #if 0
1085 DBG("memory reserved, lmbs :\n");
1086 lmb_dump_all();
1087 #endif
1090 void __init early_init_devtree(void *params)
1092 DBG(" -> early_init_devtree()\n");
1094 /* Setup flat device-tree pointer */
1095 initial_boot_params = params;
1097 /* By default, hash size is not set */
1098 ppc64_pft_size = 0;
1100 /* Retreive various informations from the /chosen node of the
1101 * device-tree, including the platform type, initrd location and
1102 * size, TCE reserve, and more ...
1104 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1106 /* Scan memory nodes and rebuild LMBs */
1107 lmb_init();
1108 scan_flat_dt(early_init_dt_scan_root, NULL);
1109 scan_flat_dt(early_init_dt_scan_memory, NULL);
1110 lmb_enforce_memory_limit();
1111 lmb_analyze();
1112 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1113 lmb_reserve(0, __pa(klimit));
1115 DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
1117 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1118 early_reserve_mem();
1120 DBG("Scanning CPUs ...\n");
1122 /* Retreive hash table size from flattened tree plus other
1123 * CPU related informations (altivec support, boot CPU ID, ...)
1125 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1127 /* If hash size wasn't obtained above, we calculate it now based on
1128 * the total RAM size
1130 if (ppc64_pft_size == 0) {
1131 unsigned long rnd_mem_size, pteg_count;
1133 /* round mem_size up to next power of 2 */
1134 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
1135 if (rnd_mem_size < systemcfg->physicalMemorySize)
1136 rnd_mem_size <<= 1;
1138 /* # pages / 2 */
1139 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
1141 ppc64_pft_size = __ilog2(pteg_count << 7);
1144 DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
1145 DBG(" <- early_init_devtree()\n");
1148 #undef printk
1151 prom_n_addr_cells(struct device_node* np)
1153 int* ip;
1154 do {
1155 if (np->parent)
1156 np = np->parent;
1157 ip = (int *) get_property(np, "#address-cells", NULL);
1158 if (ip != NULL)
1159 return *ip;
1160 } while (np->parent);
1161 /* No #address-cells property for the root node, default to 1 */
1162 return 1;
1166 prom_n_size_cells(struct device_node* np)
1168 int* ip;
1169 do {
1170 if (np->parent)
1171 np = np->parent;
1172 ip = (int *) get_property(np, "#size-cells", NULL);
1173 if (ip != NULL)
1174 return *ip;
1175 } while (np->parent);
1176 /* No #size-cells property for the root node, default to 1 */
1177 return 1;
1181 * Work out the sense (active-low level / active-high edge)
1182 * of each interrupt from the device tree.
1184 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1186 struct device_node *np;
1187 int i, j;
1189 /* default to level-triggered */
1190 memset(senses, 1, max - off);
1192 for (np = allnodes; np != 0; np = np->allnext) {
1193 for (j = 0; j < np->n_intrs; j++) {
1194 i = np->intrs[j].line;
1195 if (i >= off && i < max)
1196 senses[i-off] = np->intrs[j].sense ?
1197 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
1198 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
1204 * Construct and return a list of the device_nodes with a given name.
1206 struct device_node *
1207 find_devices(const char *name)
1209 struct device_node *head, **prevp, *np;
1211 prevp = &head;
1212 for (np = allnodes; np != 0; np = np->allnext) {
1213 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1214 *prevp = np;
1215 prevp = &np->next;
1218 *prevp = NULL;
1219 return head;
1221 EXPORT_SYMBOL(find_devices);
1224 * Construct and return a list of the device_nodes with a given type.
1226 struct device_node *
1227 find_type_devices(const char *type)
1229 struct device_node *head, **prevp, *np;
1231 prevp = &head;
1232 for (np = allnodes; np != 0; np = np->allnext) {
1233 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1234 *prevp = np;
1235 prevp = &np->next;
1238 *prevp = NULL;
1239 return head;
1241 EXPORT_SYMBOL(find_type_devices);
1244 * Returns all nodes linked together
1246 struct device_node *
1247 find_all_nodes(void)
1249 struct device_node *head, **prevp, *np;
1251 prevp = &head;
1252 for (np = allnodes; np != 0; np = np->allnext) {
1253 *prevp = np;
1254 prevp = &np->next;
1256 *prevp = NULL;
1257 return head;
1259 EXPORT_SYMBOL(find_all_nodes);
1261 /** Checks if the given "compat" string matches one of the strings in
1262 * the device's "compatible" property
1265 device_is_compatible(struct device_node *device, const char *compat)
1267 const char* cp;
1268 int cplen, l;
1270 cp = (char *) get_property(device, "compatible", &cplen);
1271 if (cp == NULL)
1272 return 0;
1273 while (cplen > 0) {
1274 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1275 return 1;
1276 l = strlen(cp) + 1;
1277 cp += l;
1278 cplen -= l;
1281 return 0;
1283 EXPORT_SYMBOL(device_is_compatible);
1287 * Indicates whether the root node has a given value in its
1288 * compatible property.
1291 machine_is_compatible(const char *compat)
1293 struct device_node *root;
1294 int rc = 0;
1296 root = of_find_node_by_path("/");
1297 if (root) {
1298 rc = device_is_compatible(root, compat);
1299 of_node_put(root);
1301 return rc;
1303 EXPORT_SYMBOL(machine_is_compatible);
1306 * Construct and return a list of the device_nodes with a given type
1307 * and compatible property.
1309 struct device_node *
1310 find_compatible_devices(const char *type, const char *compat)
1312 struct device_node *head, **prevp, *np;
1314 prevp = &head;
1315 for (np = allnodes; np != 0; np = np->allnext) {
1316 if (type != NULL
1317 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1318 continue;
1319 if (device_is_compatible(np, compat)) {
1320 *prevp = np;
1321 prevp = &np->next;
1324 *prevp = NULL;
1325 return head;
1327 EXPORT_SYMBOL(find_compatible_devices);
1330 * Find the device_node with a given full_name.
1332 struct device_node *
1333 find_path_device(const char *path)
1335 struct device_node *np;
1337 for (np = allnodes; np != 0; np = np->allnext)
1338 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1339 return np;
1340 return NULL;
1342 EXPORT_SYMBOL(find_path_device);
1344 /*******
1346 * New implementation of the OF "find" APIs, return a refcounted
1347 * object, call of_node_put() when done. The device tree and list
1348 * are protected by a rw_lock.
1350 * Note that property management will need some locking as well,
1351 * this isn't dealt with yet.
1353 *******/
1356 * of_find_node_by_name - Find a node by its "name" property
1357 * @from: The node to start searching from or NULL, the node
1358 * you pass will not be searched, only the next one
1359 * will; typically, you pass what the previous call
1360 * returned. of_node_put() will be called on it
1361 * @name: The name string to match against
1363 * Returns a node pointer with refcount incremented, use
1364 * of_node_put() on it when done.
1366 struct device_node *of_find_node_by_name(struct device_node *from,
1367 const char *name)
1369 struct device_node *np;
1371 read_lock(&devtree_lock);
1372 np = from ? from->allnext : allnodes;
1373 for (; np != 0; np = np->allnext)
1374 if (np->name != 0 && strcasecmp(np->name, name) == 0
1375 && of_node_get(np))
1376 break;
1377 if (from)
1378 of_node_put(from);
1379 read_unlock(&devtree_lock);
1380 return np;
1382 EXPORT_SYMBOL(of_find_node_by_name);
1385 * of_find_node_by_type - Find a node by its "device_type" property
1386 * @from: The node to start searching from or NULL, the node
1387 * you pass will not be searched, only the next one
1388 * will; typically, you pass what the previous call
1389 * returned. of_node_put() will be called on it
1390 * @name: The type string to match against
1392 * Returns a node pointer with refcount incremented, use
1393 * of_node_put() on it when done.
1395 struct device_node *of_find_node_by_type(struct device_node *from,
1396 const char *type)
1398 struct device_node *np;
1400 read_lock(&devtree_lock);
1401 np = from ? from->allnext : allnodes;
1402 for (; np != 0; np = np->allnext)
1403 if (np->type != 0 && strcasecmp(np->type, type) == 0
1404 && of_node_get(np))
1405 break;
1406 if (from)
1407 of_node_put(from);
1408 read_unlock(&devtree_lock);
1409 return np;
1411 EXPORT_SYMBOL(of_find_node_by_type);
1414 * of_find_compatible_node - Find a node based on type and one of the
1415 * tokens in its "compatible" property
1416 * @from: The node to start searching from or NULL, the node
1417 * you pass will not be searched, only the next one
1418 * will; typically, you pass what the previous call
1419 * returned. of_node_put() will be called on it
1420 * @type: The type string to match "device_type" or NULL to ignore
1421 * @compatible: The string to match to one of the tokens in the device
1422 * "compatible" list.
1424 * Returns a node pointer with refcount incremented, use
1425 * of_node_put() on it when done.
1427 struct device_node *of_find_compatible_node(struct device_node *from,
1428 const char *type, const char *compatible)
1430 struct device_node *np;
1432 read_lock(&devtree_lock);
1433 np = from ? from->allnext : allnodes;
1434 for (; np != 0; np = np->allnext) {
1435 if (type != NULL
1436 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1437 continue;
1438 if (device_is_compatible(np, compatible) && of_node_get(np))
1439 break;
1441 if (from)
1442 of_node_put(from);
1443 read_unlock(&devtree_lock);
1444 return np;
1446 EXPORT_SYMBOL(of_find_compatible_node);
1449 * of_find_node_by_path - Find a node matching a full OF path
1450 * @path: The full path to match
1452 * Returns a node pointer with refcount incremented, use
1453 * of_node_put() on it when done.
1455 struct device_node *of_find_node_by_path(const char *path)
1457 struct device_node *np = allnodes;
1459 read_lock(&devtree_lock);
1460 for (; np != 0; np = np->allnext)
1461 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1462 && of_node_get(np))
1463 break;
1464 read_unlock(&devtree_lock);
1465 return np;
1467 EXPORT_SYMBOL(of_find_node_by_path);
1470 * of_find_node_by_phandle - Find a node given a phandle
1471 * @handle: phandle of the node to find
1473 * Returns a node pointer with refcount incremented, use
1474 * of_node_put() on it when done.
1476 struct device_node *of_find_node_by_phandle(phandle handle)
1478 struct device_node *np;
1480 read_lock(&devtree_lock);
1481 for (np = allnodes; np != 0; np = np->allnext)
1482 if (np->linux_phandle == handle)
1483 break;
1484 if (np)
1485 of_node_get(np);
1486 read_unlock(&devtree_lock);
1487 return np;
1489 EXPORT_SYMBOL(of_find_node_by_phandle);
1492 * of_find_all_nodes - Get next node in global list
1493 * @prev: Previous node or NULL to start iteration
1494 * of_node_put() will be called on it
1496 * Returns a node pointer with refcount incremented, use
1497 * of_node_put() on it when done.
1499 struct device_node *of_find_all_nodes(struct device_node *prev)
1501 struct device_node *np;
1503 read_lock(&devtree_lock);
1504 np = prev ? prev->allnext : allnodes;
1505 for (; np != 0; np = np->allnext)
1506 if (of_node_get(np))
1507 break;
1508 if (prev)
1509 of_node_put(prev);
1510 read_unlock(&devtree_lock);
1511 return np;
1513 EXPORT_SYMBOL(of_find_all_nodes);
1516 * of_get_parent - Get a node's parent if any
1517 * @node: Node to get parent
1519 * Returns a node pointer with refcount incremented, use
1520 * of_node_put() on it when done.
1522 struct device_node *of_get_parent(const struct device_node *node)
1524 struct device_node *np;
1526 if (!node)
1527 return NULL;
1529 read_lock(&devtree_lock);
1530 np = of_node_get(node->parent);
1531 read_unlock(&devtree_lock);
1532 return np;
1534 EXPORT_SYMBOL(of_get_parent);
1537 * of_get_next_child - Iterate a node childs
1538 * @node: parent node
1539 * @prev: previous child of the parent node, or NULL to get first
1541 * Returns a node pointer with refcount incremented, use
1542 * of_node_put() on it when done.
1544 struct device_node *of_get_next_child(const struct device_node *node,
1545 struct device_node *prev)
1547 struct device_node *next;
1549 read_lock(&devtree_lock);
1550 next = prev ? prev->sibling : node->child;
1551 for (; next != 0; next = next->sibling)
1552 if (of_node_get(next))
1553 break;
1554 if (prev)
1555 of_node_put(prev);
1556 read_unlock(&devtree_lock);
1557 return next;
1559 EXPORT_SYMBOL(of_get_next_child);
1562 * of_node_get - Increment refcount of a node
1563 * @node: Node to inc refcount, NULL is supported to
1564 * simplify writing of callers
1566 * Returns node.
1568 struct device_node *of_node_get(struct device_node *node)
1570 if (node)
1571 kref_get(&node->kref);
1572 return node;
1574 EXPORT_SYMBOL(of_node_get);
1576 static inline struct device_node * kref_to_device_node(struct kref *kref)
1578 return container_of(kref, struct device_node, kref);
1582 * of_node_release - release a dynamically allocated node
1583 * @kref: kref element of the node to be released
1585 * In of_node_put() this function is passed to kref_put()
1586 * as the destructor.
1588 static void of_node_release(struct kref *kref)
1590 struct device_node *node = kref_to_device_node(kref);
1591 struct property *prop = node->properties;
1593 if (!OF_IS_DYNAMIC(node))
1594 return;
1595 while (prop) {
1596 struct property *next = prop->next;
1597 kfree(prop->name);
1598 kfree(prop->value);
1599 kfree(prop);
1600 prop = next;
1602 kfree(node->intrs);
1603 kfree(node->addrs);
1604 kfree(node->full_name);
1605 kfree(node);
1609 * of_node_put - Decrement refcount of a node
1610 * @node: Node to dec refcount, NULL is supported to
1611 * simplify writing of callers
1614 void of_node_put(struct device_node *node)
1616 if (node)
1617 kref_put(&node->kref, of_node_release);
1619 EXPORT_SYMBOL(of_node_put);
1622 * Fix up the uninitialized fields in a new device node:
1623 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1625 * A lot of boot-time code is duplicated here, because functions such
1626 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1627 * slab allocator.
1629 * This should probably be split up into smaller chunks.
1632 static int of_finish_dynamic_node(struct device_node *node,
1633 unsigned long *unused1, int unused2,
1634 int unused3, int unused4)
1636 struct device_node *parent = of_get_parent(node);
1637 int err = 0;
1638 phandle *ibm_phandle;
1640 node->name = get_property(node, "name", NULL);
1641 node->type = get_property(node, "device_type", NULL);
1643 if (!parent) {
1644 err = -ENODEV;
1645 goto out;
1648 /* We don't support that function on PowerMac, at least
1649 * not yet
1651 if (systemcfg->platform == PLATFORM_POWERMAC)
1652 return -ENODEV;
1654 /* fix up new node's linux_phandle field */
1655 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1656 node->linux_phandle = *ibm_phandle;
1658 out:
1659 of_node_put(parent);
1660 return err;
1664 * Plug a device node into the tree and global list.
1666 void of_attach_node(struct device_node *np)
1668 write_lock(&devtree_lock);
1669 np->sibling = np->parent->child;
1670 np->allnext = allnodes;
1671 np->parent->child = np;
1672 allnodes = np;
1673 write_unlock(&devtree_lock);
1677 * "Unplug" a node from the device tree. The caller must hold
1678 * a reference to the node. The memory associated with the node
1679 * is not freed until its refcount goes to zero.
1681 void of_detach_node(const struct device_node *np)
1683 struct device_node *parent;
1685 write_lock(&devtree_lock);
1687 parent = np->parent;
1689 if (allnodes == np)
1690 allnodes = np->allnext;
1691 else {
1692 struct device_node *prev;
1693 for (prev = allnodes;
1694 prev->allnext != np;
1695 prev = prev->allnext)
1697 prev->allnext = np->allnext;
1700 if (parent->child == np)
1701 parent->child = np->sibling;
1702 else {
1703 struct device_node *prevsib;
1704 for (prevsib = np->parent->child;
1705 prevsib->sibling != np;
1706 prevsib = prevsib->sibling)
1708 prevsib->sibling = np->sibling;
1711 write_unlock(&devtree_lock);
1714 static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1716 int err;
1718 switch (action) {
1719 case PSERIES_RECONFIG_ADD:
1720 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1721 if (err < 0) {
1722 printk(KERN_ERR "finish_node returned %d\n", err);
1723 err = NOTIFY_BAD;
1725 break;
1726 default:
1727 err = NOTIFY_DONE;
1728 break;
1730 return err;
1733 static struct notifier_block prom_reconfig_nb = {
1734 .notifier_call = prom_reconfig_notifier,
1735 .priority = 10, /* This one needs to run first */
1738 static int __init prom_reconfig_setup(void)
1740 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1742 __initcall(prom_reconfig_setup);
1745 * Find a property with a given name for a given node
1746 * and return the value.
1748 unsigned char *
1749 get_property(struct device_node *np, const char *name, int *lenp)
1751 struct property *pp;
1753 for (pp = np->properties; pp != 0; pp = pp->next)
1754 if (strcmp(pp->name, name) == 0) {
1755 if (lenp != 0)
1756 *lenp = pp->length;
1757 return pp->value;
1759 return NULL;
1761 EXPORT_SYMBOL(get_property);
1764 * Add a property to a node
1766 void
1767 prom_add_property(struct device_node* np, struct property* prop)
1769 struct property **next = &np->properties;
1771 prop->next = NULL;
1772 while (*next)
1773 next = &(*next)->next;
1774 *next = prop;
1777 #if 0
1778 void
1779 print_properties(struct device_node *np)
1781 struct property *pp;
1782 char *cp;
1783 int i, n;
1785 for (pp = np->properties; pp != 0; pp = pp->next) {
1786 printk(KERN_INFO "%s", pp->name);
1787 for (i = strlen(pp->name); i < 16; ++i)
1788 printk(" ");
1789 cp = (char *) pp->value;
1790 for (i = pp->length; i > 0; --i, ++cp)
1791 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1792 || (i == 1 && *cp != 0))
1793 break;
1794 if (i == 0 && pp->length > 1) {
1795 /* looks like a string */
1796 printk(" %s\n", (char *) pp->value);
1797 } else {
1798 /* dump it in hex */
1799 n = pp->length;
1800 if (n > 64)
1801 n = 64;
1802 if (pp->length % 4 == 0) {
1803 unsigned int *p = (unsigned int *) pp->value;
1805 n /= 4;
1806 for (i = 0; i < n; ++i) {
1807 if (i != 0 && (i % 4) == 0)
1808 printk("\n ");
1809 printk(" %08x", *p++);
1811 } else {
1812 unsigned char *bp = pp->value;
1814 for (i = 0; i < n; ++i) {
1815 if (i != 0 && (i % 16) == 0)
1816 printk("\n ");
1817 printk(" %02x", *bp++);
1820 printk("\n");
1821 if (pp->length > 64)
1822 printk(" ... (length = %d)\n",
1823 pp->length);
1827 #endif