4 * Procedures for interfacing to Open Firmware.
6 * Paul Mackerras August 1996.
7 * Copyright (C) 1996 Paul Mackerras.
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.
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/proc_fs.h>
31 #include <linux/stringify.h>
32 #include <linux/delay.h>
33 #include <linux/initrd.h>
37 #include <asm/abs_addr.h>
39 #include <asm/processor.h>
43 #include <asm/system.h>
45 #include <asm/pgtable.h>
46 #include <asm/bitops.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>
58 #define DBG(fmt...) udbg_printf(fmt)
63 struct pci_reg_property {
64 struct pci_address addr;
69 struct isa_reg_property {
76 typedef unsigned long interpret_func(struct device_node *, unsigned long,
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;
90 static struct boot_param_header *initial_boot_params __initdata;
92 struct boot_param_header *initial_boot_params;
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 rwlock_t devtree_lock = RW_LOCK_UNLOCKED;
102 /* export that to outside world */
103 struct device_node *of_chosen;
106 * Find the device_node with a given phandle.
108 static struct device_node * find_phandle(phandle ph)
110 struct device_node *np;
112 for (np = allnodes; np != 0; np = np->allnext)
113 if (np->linux_phandle == ph)
119 * Find the interrupt parent of a node.
121 static struct device_node * __devinit intr_parent(struct device_node *p)
125 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
128 return find_phandle(*parp);
132 * Find out the size of each entry of the interrupts property
135 int __devinit prom_n_intr_cells(struct device_node *np)
137 struct device_node *p;
140 for (p = np; (p = intr_parent(p)) != NULL; ) {
141 icp = (unsigned int *)
142 get_property(p, "#interrupt-cells", NULL);
145 if (get_property(p, "interrupt-controller", NULL) != NULL
146 || get_property(p, "interrupt-map", NULL) != NULL) {
147 printk("oops, node %s doesn't have #interrupt-cells\n",
153 printk("prom_n_intr_cells failed for %s\n", np->full_name);
159 * Map an interrupt from a device up to the platform interrupt
162 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
163 struct device_node *np, unsigned int *ints,
166 struct device_node *p, *ipar;
167 unsigned int *imap, *imask, *ip;
168 int i, imaplen, match;
169 int newintrc = 0, newaddrc = 0;
173 reg = (unsigned int *) get_property(np, "reg", NULL);
174 naddrc = prom_n_addr_cells(np);
177 if (get_property(p, "interrupt-controller", NULL) != NULL)
178 /* this node is an interrupt controller, stop here */
180 imap = (unsigned int *)
181 get_property(p, "interrupt-map", &imaplen);
186 imask = (unsigned int *)
187 get_property(p, "interrupt-map-mask", NULL);
189 printk("oops, %s has interrupt-map but no mask\n",
193 imaplen /= sizeof(unsigned int);
196 while (imaplen > 0 && !match) {
197 /* check the child-interrupt field */
199 for (i = 0; i < naddrc && match; ++i)
200 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
201 for (; i < naddrc + nintrc && match; ++i)
202 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
203 imap += naddrc + nintrc;
204 imaplen -= naddrc + nintrc;
205 /* grab the interrupt parent */
206 ipar = find_phandle((phandle) *imap++);
209 printk("oops, no int parent %x in map of %s\n",
210 imap[-1], p->full_name);
213 /* find the parent's # addr and intr cells */
214 ip = (unsigned int *)
215 get_property(ipar, "#interrupt-cells", NULL);
217 printk("oops, no #interrupt-cells on %s\n",
222 ip = (unsigned int *)
223 get_property(ipar, "#address-cells", NULL);
224 newaddrc = (ip == NULL)? 0: *ip;
225 imap += newaddrc + newintrc;
226 imaplen -= newaddrc + newintrc;
229 printk("oops, error decoding int-map on %s, len=%d\n",
230 p->full_name, imaplen);
235 printk("oops, no match in %s int-map for %s\n",
236 p->full_name, np->full_name);
243 ints = imap - nintrc;
248 printk("hmmm, int tree for %s doesn't have ctrler\n",
258 static unsigned long __init finish_node_interrupts(struct device_node *np,
259 unsigned long mem_start,
263 int intlen, intrcells, intrcount;
265 unsigned int *irq, virq;
266 struct device_node *ic;
268 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
271 intrcells = prom_n_intr_cells(np);
272 intlen /= intrcells * sizeof(unsigned int);
273 np->intrs = (struct interrupt_info *) mem_start;
274 mem_start += intlen * sizeof(struct interrupt_info);
280 for (i = 0; i < intlen; ++i, ints += intrcells) {
281 n = map_interrupt(&irq, &ic, np, ints, intrcells);
285 /* don't map IRQ numbers under a cascaded 8259 controller */
286 if (ic && device_is_compatible(ic, "chrp,iic")) {
287 np->intrs[intrcount].line = irq[0];
289 virq = virt_irq_create_mapping(irq[0]);
290 if (virq == NO_IRQ) {
291 printk(KERN_CRIT "Could not allocate interrupt"
292 " number for %s\n", np->full_name);
295 np->intrs[intrcount].line = irq_offset_up(virq);
298 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
299 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
300 char *name = get_property(ic->parent, "name", NULL);
301 if (name && !strcmp(name, "u3"))
302 np->intrs[intrcount].line += 128;
304 np->intrs[intrcount].sense = 1;
306 np->intrs[intrcount].sense = irq[1];
308 printk("hmmm, got %d intr cells for %s:", n,
310 for (j = 0; j < n; ++j)
311 printk(" %d", irq[j]);
316 np->n_intrs = intrcount;
321 static unsigned long __init interpret_pci_props(struct device_node *np,
322 unsigned long mem_start,
323 int naddrc, int nsizec,
326 struct address_range *adr;
327 struct pci_reg_property *pci_addrs;
330 pci_addrs = (struct pci_reg_property *)
331 get_property(np, "assigned-addresses", &l);
332 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
334 adr = (struct address_range *) mem_start;
335 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
337 adr[i].space = pci_addrs[i].addr.a_hi;
338 adr[i].address = pci_addrs[i].addr.a_lo;
339 adr[i].size = pci_addrs[i].size_lo;
345 mem_start += i * sizeof(struct address_range);
350 static unsigned long __init interpret_dbdma_props(struct device_node *np,
351 unsigned long mem_start,
352 int naddrc, int nsizec,
355 struct reg_property32 *rp;
356 struct address_range *adr;
357 unsigned long base_address;
359 struct device_node *db;
363 for (db = np->parent; db != NULL; db = db->parent) {
364 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
365 base_address = db->addrs[0].address;
371 rp = (struct reg_property32 *) get_property(np, "reg", &l);
372 if (rp != 0 && l >= sizeof(struct reg_property32)) {
374 adr = (struct address_range *) mem_start;
375 while ((l -= sizeof(struct reg_property32)) >= 0) {
378 adr[i].address = rp[i].address + base_address;
379 adr[i].size = rp[i].size;
385 mem_start += i * sizeof(struct address_range);
391 static unsigned long __init interpret_macio_props(struct device_node *np,
392 unsigned long mem_start,
393 int naddrc, int nsizec,
396 struct reg_property32 *rp;
397 struct address_range *adr;
398 unsigned long base_address;
400 struct device_node *db;
404 for (db = np->parent; db != NULL; db = db->parent) {
405 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
406 base_address = db->addrs[0].address;
412 rp = (struct reg_property32 *) get_property(np, "reg", &l);
413 if (rp != 0 && l >= sizeof(struct reg_property32)) {
415 adr = (struct address_range *) mem_start;
416 while ((l -= sizeof(struct reg_property32)) >= 0) {
419 adr[i].address = rp[i].address + base_address;
420 adr[i].size = rp[i].size;
426 mem_start += i * sizeof(struct address_range);
432 static unsigned long __init interpret_isa_props(struct device_node *np,
433 unsigned long mem_start,
434 int naddrc, int nsizec,
437 struct isa_reg_property *rp;
438 struct address_range *adr;
441 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
442 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
444 adr = (struct address_range *) mem_start;
445 while ((l -= sizeof(struct reg_property)) >= 0) {
447 adr[i].space = rp[i].space;
448 adr[i].address = rp[i].address;
449 adr[i].size = rp[i].size;
455 mem_start += i * sizeof(struct address_range);
461 static unsigned long __init interpret_root_props(struct device_node *np,
462 unsigned long mem_start,
463 int naddrc, int nsizec,
466 struct address_range *adr;
469 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
471 rp = (unsigned int *) get_property(np, "reg", &l);
472 if (rp != 0 && l >= rpsize) {
474 adr = (struct address_range *) mem_start;
475 while ((l -= rpsize) >= 0) {
478 adr[i].address = rp[naddrc - 1];
479 adr[i].size = rp[naddrc + nsizec - 1];
482 rp += naddrc + nsizec;
486 mem_start += i * sizeof(struct address_range);
492 static unsigned long __init finish_node(struct device_node *np,
493 unsigned long mem_start,
494 interpret_func *ifunc,
495 int naddrc, int nsizec,
498 struct device_node *child;
501 /* get the device addresses and interrupts */
503 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
505 mem_start = finish_node_interrupts(np, mem_start, measure_only);
507 /* Look for #address-cells and #size-cells properties. */
508 ip = (int *) get_property(np, "#address-cells", NULL);
511 ip = (int *) get_property(np, "#size-cells", NULL);
515 /* the f50 sets the name to 'display' and 'compatible' to what we
516 * expect for the name -- Cort
518 if (!strcmp(np->name, "display"))
519 np->name = get_property(np, "compatible", NULL);
521 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
522 ifunc = interpret_root_props;
523 else if (np->type == 0)
525 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
526 ifunc = interpret_pci_props;
527 else if (!strcmp(np->type, "dbdma"))
528 ifunc = interpret_dbdma_props;
529 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
530 ifunc = interpret_macio_props;
531 else if (!strcmp(np->type, "isa"))
532 ifunc = interpret_isa_props;
533 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
534 ifunc = interpret_root_props;
535 else if (!((ifunc == interpret_dbdma_props
536 || ifunc == interpret_macio_props)
537 && (!strcmp(np->type, "escc")
538 || !strcmp(np->type, "media-bay"))))
541 for (child = np->child; child != NULL; child = child->sibling)
542 mem_start = finish_node(child, mem_start, ifunc,
543 naddrc, nsizec, measure_only);
549 * finish_device_tree is called once things are running normally
550 * (i.e. with text and data mapped to the address they were linked at).
551 * It traverses the device tree and fills in some of the additional,
552 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
553 * mapping is also initialized at this point.
555 void __init finish_device_tree(void)
557 unsigned long mem, size;
559 DBG(" -> finish_device_tree\n");
561 if (naca->interrupt_controller == IC_INVALID) {
562 DBG("failed to configure interrupt controller type\n");
563 panic("failed to configure interrupt controller type\n");
566 /* Initialize virtual IRQ map */
569 /* Finish device-tree (pre-parsing some properties etc...) */
570 size = finish_node(allnodes, 0, NULL, 0, 0, 1);
571 mem = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
572 if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + size)
575 DBG(" <- finish_device_tree\n");
579 #define printk udbg_printf
582 static inline char *find_flat_dt_string(u32 offset)
584 return ((char *)initial_boot_params) + initial_boot_params->off_dt_strings
589 * This function is used to scan the flattened device-tree, it is
590 * used to extract the memory informations at boot before we can
593 static int __init scan_flat_dt(int (*it)(unsigned long node,
594 const char *full_path, void *data),
597 unsigned long p = ((unsigned long)initial_boot_params) +
598 initial_boot_params->off_dt_struct;
602 u32 tag = *((u32 *)p);
606 if (tag == OF_DT_END_NODE)
608 if (tag == OF_DT_END)
610 if (tag == OF_DT_PROP) {
611 u32 sz = *((u32 *)p);
613 p = _ALIGN(p, sz >= 8 ? 8 : 4);
618 if (tag != OF_DT_BEGIN_NODE) {
619 printk(KERN_WARNING "Invalid tag %x scanning flattened"
620 " device tree !\n", tag);
624 p = _ALIGN(p + strlen(pathp) + 1, 4);
625 rc = it(p, pathp, data);
634 * This function can be used within scan_flattened_dt callback to get
635 * access to properties
637 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
640 unsigned long p = node;
643 u32 tag = *((u32 *)p);
648 if (tag != OF_DT_PROP)
652 noff = *((u32 *)(p + 4));
654 p = _ALIGN(p, sz >= 8 ? 8 : 4);
656 nstr = find_flat_dt_string(noff);
658 printk(KERN_WARNING "Can't find property index name !\n");
661 if (strcmp(name, nstr) == 0) {
671 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
676 *mem = _ALIGN(*mem, align);
683 static unsigned long __init unflatten_dt_node(unsigned long mem,
685 struct device_node *dad,
686 struct device_node ***allnextpp)
688 struct device_node *np;
689 struct property *pp, **prev_pp = NULL;
694 tag = *((u32 *)(*p));
695 if (tag != OF_DT_BEGIN_NODE) {
696 printk("Weird tag at start of node: %x\n", tag);
701 l = strlen(pathp) + 1;
702 *p = _ALIGN(*p + l, 4);
704 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + l,
705 __alignof__(struct device_node));
707 memset(np, 0, sizeof(*np));
708 np->full_name = ((char*)np) + sizeof(struct device_node);
709 memcpy(np->full_name, pathp, l);
710 prev_pp = &np->properties;
712 *allnextpp = &np->allnext;
715 /* we temporarily use the `next' field as `last_child'. */
719 dad->next->sibling = np;
727 tag = *((u32 *)(*p));
728 if (tag != OF_DT_PROP)
732 noff = *((u32 *)((*p) + 4));
733 *p = _ALIGN((*p) + 8, sz >= 8 ? 8 : 4);
735 pname = find_flat_dt_string(noff);
737 printk("Can't find property name in list !\n");
740 l = strlen(pname) + 1;
741 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
742 __alignof__(struct property));
744 if (strcmp(pname, "linux,phandle") == 0) {
745 np->node = *((u32 *)*p);
746 if (np->linux_phandle == 0)
747 np->linux_phandle = np->node;
749 if (strcmp(pname, "ibm,phandle") == 0)
750 np->linux_phandle = *((u32 *)*p);
753 pp->value = (void *)*p;
757 *p = _ALIGN((*p) + sz, 4);
761 np->name = get_property(np, "name", NULL);
762 np->type = get_property(np, "device_type", NULL);
769 while (tag == OF_DT_BEGIN_NODE) {
770 mem = unflatten_dt_node(mem, p, np, allnextpp);
771 tag = *((u32 *)(*p));
773 if (tag != OF_DT_END_NODE) {
774 printk("Weird tag at start of node: %x\n", tag);
783 * unflattens the device-tree passed by the firmware, creating the
784 * tree of struct device_node. It also fills the "name" and "type"
785 * pointers of the nodes so the normal device-tree walking functions
786 * can be used (this used to be done by finish_device_tree)
788 void __init unflatten_device_tree(void)
790 unsigned long start, mem, size;
791 struct device_node **allnextp = &allnodes;
795 DBG(" -> unflatten_device_tree()\n");
797 /* First pass, scan for size */
798 start = ((unsigned long)initial_boot_params) +
799 initial_boot_params->off_dt_struct;
800 size = unflatten_dt_node(0, &start, NULL, NULL);
802 DBG(" size is %lx, allocating...\n", size);
804 /* Allocate memory for the expanded device tree */
805 mem = (unsigned long)abs_to_virt(lmb_alloc(size,
806 __alignof__(struct device_node)));
807 DBG(" unflattening...\n", mem);
809 /* Second pass, do actual unflattening */
810 start = ((unsigned long)initial_boot_params) +
811 initial_boot_params->off_dt_struct;
812 unflatten_dt_node(mem, &start, NULL, &allnextp);
813 if (*((u32 *)start) != OF_DT_END)
814 printk(KERN_WARNING "Weird tag at end of tree: %x\n", *((u32 *)start));
817 /* Get pointer to OF "/chosen" node for use everywhere */
818 of_chosen = of_find_node_by_path("/chosen");
820 /* Retreive command line */
821 if (of_chosen != NULL) {
822 p = (char *)get_property(of_chosen, "bootargs", &l);
823 if (p != NULL && l > 0)
824 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
826 #ifdef CONFIG_CMDLINE
827 if (l == 0) /* dbl check */
828 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
829 #endif /* CONFIG_CMDLINE */
831 DBG("Command line is: %s\n", cmd_line);
833 DBG(" <- unflatten_device_tree()\n");
837 static int __init early_init_dt_scan_cpus(unsigned long node,
838 const char *full_path, void *data)
840 char *type = get_flat_dt_prop(node, "device_type", NULL);
842 /* We are scanning "cpu" nodes only */
843 if (type == NULL || strcmp(type, "cpu") != 0)
846 /* On LPAR, look for the first ibm,pft-size property for the hash table size
848 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && naca->pftSize == 0) {
850 pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
851 if (pft_size != NULL) {
852 /* pft_size[0] is the NUMA CEC cookie */
853 naca->pftSize = pft_size[1];
857 /* Check if it's the boot-cpu, set it's hw index in paca now */
858 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
859 u32 *prop = get_flat_dt_prop(node, "reg", NULL);
860 paca[0].hw_cpu_id = prop == NULL ? 0 : *prop;
866 static int __init early_init_dt_scan_chosen(unsigned long node,
867 const char *full_path, void *data)
871 if (strcmp(full_path, "/chosen") != 0)
874 /* get platform type */
875 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
878 systemcfg->platform = *prop;
880 /* check if iommu is forced on or off */
881 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
883 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
886 #ifdef CONFIG_PPC_PSERIES
887 /* To help early debugging via the front panel, we retreive a minimal
888 * set of RTAS infos now if available
893 basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
894 entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
895 prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
896 if (basep && entryp && prop) {
898 rtas.entry = *entryp;
902 #endif /* CONFIG_PPC_PSERIES */
908 static int __init early_init_dt_scan_root(unsigned long node,
909 const char *full_path, void *data)
913 if (strcmp(full_path, "/") != 0)
916 prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
917 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
919 prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
920 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
926 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
931 /* Ignore more than 2 cells */
947 static int __init early_init_dt_scan_memory(unsigned long node,
948 const char *full_path, void *data)
950 char *type = get_flat_dt_prop(node, "device_type", NULL);
954 /* We are scanning "memory" nodes only */
955 if (type == NULL || strcmp(type, "memory") != 0)
958 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
962 endp = reg + (l / sizeof(cell_t));
964 DBG("memory scan node %s ...\n", full_path);
965 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
966 unsigned long base, size;
968 base = dt_mem_next_cell(dt_root_addr_cells, ®);
969 size = dt_mem_next_cell(dt_root_size_cells, ®);
973 DBG(" - %lx , %lx\n", base, size);
975 if (base >= 0x80000000ul)
977 if ((base + size) > 0x80000000ul)
978 size = 0x80000000ul - base;
985 static void __init early_reserve_mem(void)
988 u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
989 initial_boot_params->off_mem_rsvmap);
991 base = *(reserve_map++);
992 size = *(reserve_map++);
995 DBG("reserving: %lx -> %lx\n", base, size);
996 lmb_reserve(base, size);
1000 DBG("memory reserved, lmbs :\n");
1005 void __init early_init_devtree(void *params)
1007 DBG(" -> early_init_devtree()\n");
1009 /* Setup flat device-tree pointer */
1010 initial_boot_params = params;
1012 /* By default, hash size is not set */
1015 /* Retreive various informations from the /chosen node of the
1016 * device-tree, including the platform type, initrd location and
1017 * size, TCE reserve, and more ...
1019 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1021 /* Scan memory nodes and rebuild LMBs */
1023 scan_flat_dt(early_init_dt_scan_root, NULL);
1024 scan_flat_dt(early_init_dt_scan_memory, NULL);
1026 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1028 DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
1030 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1031 early_reserve_mem();
1033 DBG("Scanning CPUs ...\n");
1035 /* Retreive hash table size from flattened tree */
1036 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1038 /* If hash size wasn't obtained above, we calculate it now based on
1039 * the total RAM size
1041 if (naca->pftSize == 0) {
1042 unsigned long rnd_mem_size, pteg_count;
1044 /* round mem_size up to next power of 2 */
1045 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
1046 if (rnd_mem_size < systemcfg->physicalMemorySize)
1050 pteg_count = (rnd_mem_size >> (12 + 1));
1052 naca->pftSize = __ilog2(pteg_count << 7);
1055 DBG("Hash pftSize: %x\n", (int)naca->pftSize);
1056 DBG(" <- early_init_devtree()\n");
1062 prom_n_addr_cells(struct device_node* np)
1068 ip = (int *) get_property(np, "#address-cells", NULL);
1071 } while (np->parent);
1072 /* No #address-cells property for the root node, default to 1 */
1077 prom_n_size_cells(struct device_node* np)
1083 ip = (int *) get_property(np, "#size-cells", NULL);
1086 } while (np->parent);
1087 /* No #size-cells property for the root node, default to 1 */
1092 * Work out the sense (active-low level / active-high edge)
1093 * of each interrupt from the device tree.
1096 prom_get_irq_senses(unsigned char *senses, int off, int max)
1098 struct device_node *np;
1101 /* default to level-triggered */
1102 memset(senses, 1, max - off);
1104 for (np = allnodes; np != 0; np = np->allnext) {
1105 for (j = 0; j < np->n_intrs; j++) {
1106 i = np->intrs[j].line;
1107 if (i >= off && i < max)
1108 senses[i-off] = np->intrs[j].sense;
1114 * Construct and return a list of the device_nodes with a given name.
1116 struct device_node *
1117 find_devices(const char *name)
1119 struct device_node *head, **prevp, *np;
1122 for (np = allnodes; np != 0; np = np->allnext) {
1123 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1133 * Construct and return a list of the device_nodes with a given type.
1135 struct device_node *
1136 find_type_devices(const char *type)
1138 struct device_node *head, **prevp, *np;
1141 for (np = allnodes; np != 0; np = np->allnext) {
1142 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1152 * Returns all nodes linked together
1154 struct device_node *
1155 find_all_nodes(void)
1157 struct device_node *head, **prevp, *np;
1160 for (np = allnodes; np != 0; np = np->allnext) {
1168 /** Checks if the given "compat" string matches one of the strings in
1169 * the device's "compatible" property
1172 device_is_compatible(struct device_node *device, const char *compat)
1177 cp = (char *) get_property(device, "compatible", &cplen);
1181 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1193 * Indicates whether the root node has a given value in its
1194 * compatible property.
1197 machine_is_compatible(const char *compat)
1199 struct device_node *root;
1202 root = of_find_node_by_path("/");
1204 rc = device_is_compatible(root, compat);
1211 * Construct and return a list of the device_nodes with a given type
1212 * and compatible property.
1214 struct device_node *
1215 find_compatible_devices(const char *type, const char *compat)
1217 struct device_node *head, **prevp, *np;
1220 for (np = allnodes; np != 0; np = np->allnext) {
1222 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1224 if (device_is_compatible(np, compat)) {
1234 * Find the device_node with a given full_name.
1236 struct device_node *
1237 find_path_device(const char *path)
1239 struct device_node *np;
1241 for (np = allnodes; np != 0; np = np->allnext)
1242 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1249 * New implementation of the OF "find" APIs, return a refcounted
1250 * object, call of_node_put() when done. The device tree and list
1251 * are protected by a rw_lock.
1253 * Note that property management will need some locking as well,
1254 * this isn't dealt with yet.
1259 * of_find_node_by_name - Find a node by its "name" property
1260 * @from: The node to start searching from or NULL, the node
1261 * you pass will not be searched, only the next one
1262 * will; typically, you pass what the previous call
1263 * returned. of_node_put() will be called on it
1264 * @name: The name string to match against
1266 * Returns a node pointer with refcount incremented, use
1267 * of_node_put() on it when done.
1269 struct device_node *of_find_node_by_name(struct device_node *from,
1272 struct device_node *np;
1274 read_lock(&devtree_lock);
1275 np = from ? from->allnext : allnodes;
1276 for (; np != 0; np = np->allnext)
1277 if (np->name != 0 && strcasecmp(np->name, name) == 0
1282 read_unlock(&devtree_lock);
1285 EXPORT_SYMBOL(of_find_node_by_name);
1288 * of_find_node_by_type - Find a node by its "device_type" property
1289 * @from: The node to start searching from or NULL, the node
1290 * you pass will not be searched, only the next one
1291 * will; typically, you pass what the previous call
1292 * returned. of_node_put() will be called on it
1293 * @name: The type string to match against
1295 * Returns a node pointer with refcount incremented, use
1296 * of_node_put() on it when done.
1298 struct device_node *of_find_node_by_type(struct device_node *from,
1301 struct device_node *np;
1303 read_lock(&devtree_lock);
1304 np = from ? from->allnext : allnodes;
1305 for (; np != 0; np = np->allnext)
1306 if (np->type != 0 && strcasecmp(np->type, type) == 0
1311 read_unlock(&devtree_lock);
1314 EXPORT_SYMBOL(of_find_node_by_type);
1317 * of_find_compatible_node - Find a node based on type and one of the
1318 * tokens in its "compatible" property
1319 * @from: The node to start searching from or NULL, the node
1320 * you pass will not be searched, only the next one
1321 * will; typically, you pass what the previous call
1322 * returned. of_node_put() will be called on it
1323 * @type: The type string to match "device_type" or NULL to ignore
1324 * @compatible: The string to match to one of the tokens in the device
1325 * "compatible" list.
1327 * Returns a node pointer with refcount incremented, use
1328 * of_node_put() on it when done.
1330 struct device_node *of_find_compatible_node(struct device_node *from,
1331 const char *type, const char *compatible)
1333 struct device_node *np;
1335 read_lock(&devtree_lock);
1336 np = from ? from->allnext : allnodes;
1337 for (; np != 0; np = np->allnext) {
1339 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1341 if (device_is_compatible(np, compatible) && of_node_get(np))
1346 read_unlock(&devtree_lock);
1349 EXPORT_SYMBOL(of_find_compatible_node);
1352 * of_find_node_by_path - Find a node matching a full OF path
1353 * @path: The full path to match
1355 * Returns a node pointer with refcount incremented, use
1356 * of_node_put() on it when done.
1358 struct device_node *of_find_node_by_path(const char *path)
1360 struct device_node *np = allnodes;
1362 read_lock(&devtree_lock);
1363 for (; np != 0; np = np->allnext)
1364 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1367 read_unlock(&devtree_lock);
1370 EXPORT_SYMBOL(of_find_node_by_path);
1373 * of_find_node_by_phandle - Find a node given a phandle
1374 * @handle: phandle of the node to find
1376 * Returns a node pointer with refcount incremented, use
1377 * of_node_put() on it when done.
1379 struct device_node *of_find_node_by_phandle(phandle handle)
1381 struct device_node *np;
1383 read_lock(&devtree_lock);
1384 for (np = allnodes; np != 0; np = np->allnext)
1385 if (np->linux_phandle == handle)
1389 read_unlock(&devtree_lock);
1392 EXPORT_SYMBOL(of_find_node_by_phandle);
1395 * of_find_all_nodes - Get next node in global list
1396 * @prev: Previous node or NULL to start iteration
1397 * of_node_put() will be called on it
1399 * Returns a node pointer with refcount incremented, use
1400 * of_node_put() on it when done.
1402 struct device_node *of_find_all_nodes(struct device_node *prev)
1404 struct device_node *np;
1406 read_lock(&devtree_lock);
1407 np = prev ? prev->allnext : allnodes;
1408 for (; np != 0; np = np->allnext)
1409 if (of_node_get(np))
1413 read_unlock(&devtree_lock);
1416 EXPORT_SYMBOL(of_find_all_nodes);
1419 * of_get_parent - Get a node's parent if any
1420 * @node: Node to get parent
1422 * Returns a node pointer with refcount incremented, use
1423 * of_node_put() on it when done.
1425 struct device_node *of_get_parent(const struct device_node *node)
1427 struct device_node *np;
1432 read_lock(&devtree_lock);
1433 np = of_node_get(node->parent);
1434 read_unlock(&devtree_lock);
1437 EXPORT_SYMBOL(of_get_parent);
1440 * of_get_next_child - Iterate a node childs
1441 * @node: parent node
1442 * @prev: previous child of the parent node, or NULL to get first
1444 * Returns a node pointer with refcount incremented, use
1445 * of_node_put() on it when done.
1447 struct device_node *of_get_next_child(const struct device_node *node,
1448 struct device_node *prev)
1450 struct device_node *next;
1452 read_lock(&devtree_lock);
1453 next = prev ? prev->sibling : node->child;
1454 for (; next != 0; next = next->sibling)
1455 if (of_node_get(next))
1459 read_unlock(&devtree_lock);
1462 EXPORT_SYMBOL(of_get_next_child);
1465 * of_node_get - Increment refcount of a node
1466 * @node: Node to inc refcount, NULL is supported to
1467 * simplify writing of callers
1469 * Returns the node itself or NULL if gone.
1471 struct device_node *of_node_get(struct device_node *node)
1473 if (node && !OF_IS_STALE(node)) {
1474 atomic_inc(&node->_users);
1479 EXPORT_SYMBOL(of_node_get);
1482 * of_node_cleanup - release a dynamically allocated node
1483 * @arg: Node to be released
1485 static void of_node_cleanup(struct device_node *node)
1487 struct property *prop = node->properties;
1489 if (!OF_IS_DYNAMIC(node))
1492 struct property *next = prop->next;
1500 kfree(node->full_name);
1505 * of_node_put - Decrement refcount of a node
1506 * @node: Node to dec refcount, NULL is supported to
1507 * simplify writing of callers
1510 void of_node_put(struct device_node *node)
1515 WARN_ON(0 == atomic_read(&node->_users));
1517 if (OF_IS_STALE(node)) {
1518 if (atomic_dec_and_test(&node->_users)) {
1519 of_node_cleanup(node);
1524 atomic_dec(&node->_users);
1526 EXPORT_SYMBOL(of_node_put);
1529 * derive_parent - basically like dirname(1)
1530 * @path: the full_name of a node to be added to the tree
1532 * Returns the node which should be the parent of the node
1533 * described by path. E.g., for path = "/foo/bar", returns
1534 * the node with full_name = "/foo".
1536 static struct device_node *derive_parent(const char *path)
1538 struct device_node *parent = NULL;
1539 char *parent_path = "/";
1540 size_t parent_path_len = strrchr(path, '/') - path + 1;
1542 /* reject if path is "/" */
1543 if (!strcmp(path, "/"))
1546 if (strrchr(path, '/') != path) {
1547 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
1550 strlcpy(parent_path, path, parent_path_len);
1552 parent = of_find_node_by_path(parent_path);
1553 if (strcmp(parent_path, "/"))
1559 * Routines for "runtime" addition and removal of device tree nodes.
1561 #ifdef CONFIG_PROC_DEVICETREE
1563 * Add a node to /proc/device-tree.
1565 static void add_node_proc_entries(struct device_node *np)
1567 struct proc_dir_entry *ent;
1569 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
1571 proc_device_tree_add_node(np, ent);
1574 static void remove_node_proc_entries(struct device_node *np)
1576 struct property *pp = np->properties;
1577 struct device_node *parent = np->parent;
1580 remove_proc_entry(pp->name, np->pde);
1584 /* Assuming that symlinks have the same parent directory as
1588 remove_proc_entry(np->name_link->name, parent->pde);
1590 remove_proc_entry(np->addr_link->name, parent->pde);
1592 remove_proc_entry(np->pde->name, parent->pde);
1594 #else /* !CONFIG_PROC_DEVICETREE */
1595 static void add_node_proc_entries(struct device_node *np)
1600 static void remove_node_proc_entries(struct device_node *np)
1604 #endif /* CONFIG_PROC_DEVICETREE */
1607 * Fix up n_intrs and intrs fields in a new device node
1610 static int of_finish_dynamic_node_interrupts(struct device_node *node)
1612 int intrcells, intlen, i;
1613 unsigned *irq, *ints, virq;
1614 struct device_node *ic;
1616 ints = (unsigned int *)get_property(node, "interrupts", &intlen);
1617 intrcells = prom_n_intr_cells(node);
1618 intlen /= intrcells * sizeof(unsigned int);
1619 node->n_intrs = intlen;
1620 node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
1625 for (i = 0; i < intlen; ++i) {
1627 node->intrs[i].line = 0;
1628 node->intrs[i].sense = 1;
1629 n = map_interrupt(&irq, &ic, node, ints, intrcells);
1632 virq = virt_irq_create_mapping(irq[0]);
1633 if (virq == NO_IRQ) {
1634 printk(KERN_CRIT "Could not allocate interrupt "
1635 "number for %s\n", node->full_name);
1638 node->intrs[i].line = irq_offset_up(virq);
1640 node->intrs[i].sense = irq[1];
1642 printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
1644 for (j = 0; j < n; ++j)
1645 printk(" %d", irq[j]);
1655 * Fix up the uninitialized fields in a new device node:
1656 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1658 * A lot of boot-time code is duplicated here, because functions such
1659 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1662 * This should probably be split up into smaller chunks.
1665 static int of_finish_dynamic_node(struct device_node *node)
1667 struct device_node *parent = of_get_parent(node);
1670 phandle *ibm_phandle;
1672 node->name = get_property(node, "name", NULL);
1673 node->type = get_property(node, "device_type", NULL);
1680 /* We don't support that function on PowerMac, at least
1683 if (systemcfg->platform == PLATFORM_POWERMAC)
1686 /* fix up new node's linux_phandle field */
1687 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1688 node->linux_phandle = *ibm_phandle;
1690 /* do the work of interpret_pci_props */
1691 if (parent->type && !strcmp(parent->type, "pci")) {
1692 struct address_range *adr;
1693 struct pci_reg_property *pci_addrs;
1696 pci_addrs = (struct pci_reg_property *)
1697 get_property(node, "assigned-addresses", &l);
1698 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
1700 adr = kmalloc(sizeof(struct address_range) *
1701 (l / sizeof(struct pci_reg_property)),
1707 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
1708 adr[i].space = pci_addrs[i].addr.a_hi;
1709 adr[i].address = pci_addrs[i].addr.a_lo;
1710 adr[i].size = pci_addrs[i].size_lo;
1718 /* now do the work of finish_node_interrupts */
1719 if (get_property(node, "interrupts", NULL)) {
1720 err = of_finish_dynamic_node_interrupts(node);
1724 /* now do the rough equivalent of update_dn_pci_info, this
1725 * probably is not correct for phb's, but should work for
1729 node->phb = parent->phb;
1731 regs = (u32 *)get_property(node, "reg", NULL);
1733 node->busno = (regs[0] >> 16) & 0xff;
1734 node->devfn = (regs[0] >> 8) & 0xff;
1737 /* fixing up iommu_table */
1739 #ifdef CONFIG_PPC_PSERIES
1740 if (strcmp(node->name, "pci") == 0 &&
1741 get_property(node, "ibm,dma-window", NULL)) {
1742 node->bussubno = node->busno;
1743 iommu_devnode_init(node);
1745 node->iommu_table = parent->iommu_table;
1746 #endif /* CONFIG_PPC_PSERIES */
1749 of_node_put(parent);
1754 * Given a path and a property list, construct an OF device node, add
1755 * it to the device tree and global list, and place it in
1756 * /proc/device-tree. This function may sleep.
1758 int of_add_node(const char *path, struct property *proplist)
1760 struct device_node *np;
1763 np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
1767 memset(np, 0, sizeof(*np));
1769 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
1770 if (!np->full_name) {
1774 strcpy(np->full_name, path);
1776 np->properties = proplist;
1777 OF_MARK_DYNAMIC(np);
1779 np->parent = derive_parent(path);
1782 return -EINVAL; /* could also be ENOMEM, though */
1785 if (0 != (err = of_finish_dynamic_node(np))) {
1790 write_lock(&devtree_lock);
1791 np->sibling = np->parent->child;
1792 np->allnext = allnodes;
1793 np->parent->child = np;
1795 write_unlock(&devtree_lock);
1797 add_node_proc_entries(np);
1799 of_node_put(np->parent);
1805 * Remove an OF device node from the system.
1806 * Caller should have already "gotten" np.
1808 int of_remove_node(struct device_node *np)
1810 struct device_node *parent, *child;
1812 parent = of_get_parent(np);
1816 if ((child = of_get_next_child(np, NULL))) {
1821 write_lock(&devtree_lock);
1823 remove_node_proc_entries(np);
1825 allnodes = np->allnext;
1827 struct device_node *prev;
1828 for (prev = allnodes;
1829 prev->allnext != np;
1830 prev = prev->allnext)
1832 prev->allnext = np->allnext;
1835 if (parent->child == np)
1836 parent->child = np->sibling;
1838 struct device_node *prevsib;
1839 for (prevsib = np->parent->child;
1840 prevsib->sibling != np;
1841 prevsib = prevsib->sibling)
1843 prevsib->sibling = np->sibling;
1845 write_unlock(&devtree_lock);
1846 of_node_put(parent);
1851 * Find a property with a given name for a given node
1852 * and return the value.
1855 get_property(struct device_node *np, const char *name, int *lenp)
1857 struct property *pp;
1859 for (pp = np->properties; pp != 0; pp = pp->next)
1860 if (strcmp(pp->name, name) == 0) {
1869 * Add a property to a node
1872 prom_add_property(struct device_node* np, struct property* prop)
1874 struct property **next = &np->properties;
1878 next = &(*next)->next;
1884 print_properties(struct device_node *np)
1886 struct property *pp;
1890 for (pp = np->properties; pp != 0; pp = pp->next) {
1891 printk(KERN_INFO "%s", pp->name);
1892 for (i = strlen(pp->name); i < 16; ++i)
1894 cp = (char *) pp->value;
1895 for (i = pp->length; i > 0; --i, ++cp)
1896 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1897 || (i == 1 && *cp != 0))
1899 if (i == 0 && pp->length > 1) {
1900 /* looks like a string */
1901 printk(" %s\n", (char *) pp->value);
1903 /* dump it in hex */
1907 if (pp->length % 4 == 0) {
1908 unsigned int *p = (unsigned int *) pp->value;
1911 for (i = 0; i < n; ++i) {
1912 if (i != 0 && (i % 4) == 0)
1914 printk(" %08x", *p++);
1917 unsigned char *bp = pp->value;
1919 for (i = 0; i < n; ++i) {
1920 if (i != 0 && (i % 16) == 0)
1922 printk(" %02x", *bp++);
1926 if (pp->length > 64)
1927 printk(" ... (length = %d)\n",