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>
34 #include <linux/bitops.h>
38 #include <asm/abs_addr.h>
40 #include <asm/processor.h>
44 #include <asm/system.h>
46 #include <asm/pgtable.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>
57 #define DBG(fmt...) udbg_printf(fmt)
62 struct pci_reg_property {
63 struct pci_address addr;
68 struct isa_reg_property {
75 typedef unsigned long interpret_func(struct device_node *, unsigned long,
78 extern struct rtas_t rtas;
79 extern struct lmb lmb;
80 extern unsigned long klimit;
82 static int __initdata dt_root_addr_cells;
83 static int __initdata dt_root_size_cells;
84 static int __initdata iommu_is_off;
85 int __initdata iommu_force_on;
89 static struct boot_param_header *initial_boot_params __initdata;
91 struct boot_param_header *initial_boot_params;
94 static struct device_node *allnodes = NULL;
96 /* use when traversing tree through the allnext, child, sibling,
97 * or parent members of struct device_node.
99 static rwlock_t devtree_lock = RW_LOCK_UNLOCKED;
101 /* export that to outside world */
102 struct device_node *of_chosen;
105 * Find the device_node with a given phandle.
107 static struct device_node * find_phandle(phandle ph)
109 struct device_node *np;
111 for (np = allnodes; np != 0; np = np->allnext)
112 if (np->linux_phandle == ph)
118 * Find the interrupt parent of a node.
120 static struct device_node * __devinit intr_parent(struct device_node *p)
124 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
127 return find_phandle(*parp);
131 * Find out the size of each entry of the interrupts property
134 int __devinit prom_n_intr_cells(struct device_node *np)
136 struct device_node *p;
139 for (p = np; (p = intr_parent(p)) != NULL; ) {
140 icp = (unsigned int *)
141 get_property(p, "#interrupt-cells", NULL);
144 if (get_property(p, "interrupt-controller", NULL) != NULL
145 || get_property(p, "interrupt-map", NULL) != NULL) {
146 printk("oops, node %s doesn't have #interrupt-cells\n",
152 printk("prom_n_intr_cells failed for %s\n", np->full_name);
158 * Map an interrupt from a device up to the platform interrupt
161 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
162 struct device_node *np, unsigned int *ints,
165 struct device_node *p, *ipar;
166 unsigned int *imap, *imask, *ip;
167 int i, imaplen, match;
168 int newintrc = 0, newaddrc = 0;
172 reg = (unsigned int *) get_property(np, "reg", NULL);
173 naddrc = prom_n_addr_cells(np);
176 if (get_property(p, "interrupt-controller", NULL) != NULL)
177 /* this node is an interrupt controller, stop here */
179 imap = (unsigned int *)
180 get_property(p, "interrupt-map", &imaplen);
185 imask = (unsigned int *)
186 get_property(p, "interrupt-map-mask", NULL);
188 printk("oops, %s has interrupt-map but no mask\n",
192 imaplen /= sizeof(unsigned int);
195 while (imaplen > 0 && !match) {
196 /* check the child-interrupt field */
198 for (i = 0; i < naddrc && match; ++i)
199 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
200 for (; i < naddrc + nintrc && match; ++i)
201 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
202 imap += naddrc + nintrc;
203 imaplen -= naddrc + nintrc;
204 /* grab the interrupt parent */
205 ipar = find_phandle((phandle) *imap++);
208 printk("oops, no int parent %x in map of %s\n",
209 imap[-1], p->full_name);
212 /* find the parent's # addr and intr cells */
213 ip = (unsigned int *)
214 get_property(ipar, "#interrupt-cells", NULL);
216 printk("oops, no #interrupt-cells on %s\n",
221 ip = (unsigned int *)
222 get_property(ipar, "#address-cells", NULL);
223 newaddrc = (ip == NULL)? 0: *ip;
224 imap += newaddrc + newintrc;
225 imaplen -= newaddrc + newintrc;
228 printk("oops, error decoding int-map on %s, len=%d\n",
229 p->full_name, imaplen);
234 printk("oops, no match in %s int-map for %s\n",
235 p->full_name, np->full_name);
242 ints = imap - nintrc;
247 printk("hmmm, int tree for %s doesn't have ctrler\n",
257 static unsigned long __init finish_node_interrupts(struct device_node *np,
258 unsigned long mem_start,
262 int intlen, intrcells, intrcount;
264 unsigned int *irq, virq;
265 struct device_node *ic;
267 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
270 intrcells = prom_n_intr_cells(np);
271 intlen /= intrcells * sizeof(unsigned int);
272 np->intrs = (struct interrupt_info *) mem_start;
273 mem_start += intlen * sizeof(struct interrupt_info);
279 for (i = 0; i < intlen; ++i, ints += intrcells) {
280 n = map_interrupt(&irq, &ic, np, ints, intrcells);
284 /* don't map IRQ numbers under a cascaded 8259 controller */
285 if (ic && device_is_compatible(ic, "chrp,iic")) {
286 np->intrs[intrcount].line = irq[0];
288 virq = virt_irq_create_mapping(irq[0]);
289 if (virq == NO_IRQ) {
290 printk(KERN_CRIT "Could not allocate interrupt"
291 " number for %s\n", np->full_name);
294 np->intrs[intrcount].line = irq_offset_up(virq);
297 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
298 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
299 char *name = get_property(ic->parent, "name", NULL);
300 if (name && !strcmp(name, "u3"))
301 np->intrs[intrcount].line += 128;
303 np->intrs[intrcount].sense = 1;
305 np->intrs[intrcount].sense = irq[1];
307 printk("hmmm, got %d intr cells for %s:", n,
309 for (j = 0; j < n; ++j)
310 printk(" %d", irq[j]);
315 np->n_intrs = intrcount;
320 static unsigned long __init interpret_pci_props(struct device_node *np,
321 unsigned long mem_start,
322 int naddrc, int nsizec,
325 struct address_range *adr;
326 struct pci_reg_property *pci_addrs;
329 pci_addrs = (struct pci_reg_property *)
330 get_property(np, "assigned-addresses", &l);
331 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
333 adr = (struct address_range *) mem_start;
334 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
336 adr[i].space = pci_addrs[i].addr.a_hi;
337 adr[i].address = pci_addrs[i].addr.a_lo;
338 adr[i].size = pci_addrs[i].size_lo;
344 mem_start += i * sizeof(struct address_range);
349 static unsigned long __init interpret_dbdma_props(struct device_node *np,
350 unsigned long mem_start,
351 int naddrc, int nsizec,
354 struct reg_property32 *rp;
355 struct address_range *adr;
356 unsigned long base_address;
358 struct device_node *db;
362 for (db = np->parent; db != NULL; db = db->parent) {
363 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
364 base_address = db->addrs[0].address;
370 rp = (struct reg_property32 *) get_property(np, "reg", &l);
371 if (rp != 0 && l >= sizeof(struct reg_property32)) {
373 adr = (struct address_range *) mem_start;
374 while ((l -= sizeof(struct reg_property32)) >= 0) {
377 adr[i].address = rp[i].address + base_address;
378 adr[i].size = rp[i].size;
384 mem_start += i * sizeof(struct address_range);
390 static unsigned long __init interpret_macio_props(struct device_node *np,
391 unsigned long mem_start,
392 int naddrc, int nsizec,
395 struct reg_property32 *rp;
396 struct address_range *adr;
397 unsigned long base_address;
399 struct device_node *db;
403 for (db = np->parent; db != NULL; db = db->parent) {
404 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
405 base_address = db->addrs[0].address;
411 rp = (struct reg_property32 *) get_property(np, "reg", &l);
412 if (rp != 0 && l >= sizeof(struct reg_property32)) {
414 adr = (struct address_range *) mem_start;
415 while ((l -= sizeof(struct reg_property32)) >= 0) {
418 adr[i].address = rp[i].address + base_address;
419 adr[i].size = rp[i].size;
425 mem_start += i * sizeof(struct address_range);
431 static unsigned long __init interpret_isa_props(struct device_node *np,
432 unsigned long mem_start,
433 int naddrc, int nsizec,
436 struct isa_reg_property *rp;
437 struct address_range *adr;
440 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
441 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
443 adr = (struct address_range *) mem_start;
444 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
446 adr[i].space = rp[i].space;
447 adr[i].address = rp[i].address;
448 adr[i].size = rp[i].size;
454 mem_start += i * sizeof(struct address_range);
460 static unsigned long __init interpret_root_props(struct device_node *np,
461 unsigned long mem_start,
462 int naddrc, int nsizec,
465 struct address_range *adr;
468 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
470 rp = (unsigned int *) get_property(np, "reg", &l);
471 if (rp != 0 && l >= rpsize) {
473 adr = (struct address_range *) mem_start;
474 while ((l -= rpsize) >= 0) {
477 adr[i].address = rp[naddrc - 1];
478 adr[i].size = rp[naddrc + nsizec - 1];
481 rp += naddrc + nsizec;
485 mem_start += i * sizeof(struct address_range);
491 static unsigned long __init finish_node(struct device_node *np,
492 unsigned long mem_start,
493 interpret_func *ifunc,
494 int naddrc, int nsizec,
497 struct device_node *child;
500 /* get the device addresses and interrupts */
502 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
504 mem_start = finish_node_interrupts(np, mem_start, measure_only);
506 /* Look for #address-cells and #size-cells properties. */
507 ip = (int *) get_property(np, "#address-cells", NULL);
510 ip = (int *) get_property(np, "#size-cells", NULL);
514 /* the f50 sets the name to 'display' and 'compatible' to what we
515 * expect for the name -- Cort
517 if (!strcmp(np->name, "display"))
518 np->name = get_property(np, "compatible", NULL);
520 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
521 ifunc = interpret_root_props;
522 else if (np->type == 0)
524 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
525 ifunc = interpret_pci_props;
526 else if (!strcmp(np->type, "dbdma"))
527 ifunc = interpret_dbdma_props;
528 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
529 ifunc = interpret_macio_props;
530 else if (!strcmp(np->type, "isa"))
531 ifunc = interpret_isa_props;
532 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
533 ifunc = interpret_root_props;
534 else if (!((ifunc == interpret_dbdma_props
535 || ifunc == interpret_macio_props)
536 && (!strcmp(np->type, "escc")
537 || !strcmp(np->type, "media-bay"))))
540 for (child = np->child; child != NULL; child = child->sibling)
541 mem_start = finish_node(child, mem_start, ifunc,
542 naddrc, nsizec, measure_only);
548 * finish_device_tree is called once things are running normally
549 * (i.e. with text and data mapped to the address they were linked at).
550 * It traverses the device tree and fills in some of the additional,
551 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
552 * mapping is also initialized at this point.
554 void __init finish_device_tree(void)
556 unsigned long mem, size;
558 DBG(" -> finish_device_tree\n");
560 if (naca->interrupt_controller == IC_INVALID) {
561 DBG("failed to configure interrupt controller type\n");
562 panic("failed to configure interrupt controller type\n");
565 /* Initialize virtual IRQ map */
568 /* Finish device-tree (pre-parsing some properties etc...) */
569 size = finish_node(allnodes, 0, NULL, 0, 0, 1);
570 mem = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
571 if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + size)
574 DBG(" <- finish_device_tree\n");
578 #define printk udbg_printf
581 static inline char *find_flat_dt_string(u32 offset)
583 return ((char *)initial_boot_params) + initial_boot_params->off_dt_strings
588 * This function is used to scan the flattened device-tree, it is
589 * used to extract the memory informations at boot before we can
592 static int __init scan_flat_dt(int (*it)(unsigned long node,
593 const char *full_path, void *data),
596 unsigned long p = ((unsigned long)initial_boot_params) +
597 initial_boot_params->off_dt_struct;
601 u32 tag = *((u32 *)p);
605 if (tag == OF_DT_END_NODE)
607 if (tag == OF_DT_END)
609 if (tag == OF_DT_PROP) {
610 u32 sz = *((u32 *)p);
612 p = _ALIGN(p, sz >= 8 ? 8 : 4);
617 if (tag != OF_DT_BEGIN_NODE) {
618 printk(KERN_WARNING "Invalid tag %x scanning flattened"
619 " device tree !\n", tag);
623 p = _ALIGN(p + strlen(pathp) + 1, 4);
624 rc = it(p, pathp, data);
633 * This function can be used within scan_flattened_dt callback to get
634 * access to properties
636 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
639 unsigned long p = node;
642 u32 tag = *((u32 *)p);
647 if (tag != OF_DT_PROP)
651 noff = *((u32 *)(p + 4));
653 p = _ALIGN(p, sz >= 8 ? 8 : 4);
655 nstr = find_flat_dt_string(noff);
657 printk(KERN_WARNING "Can't find property index name !\n");
660 if (strcmp(name, nstr) == 0) {
670 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
675 *mem = _ALIGN(*mem, align);
682 static unsigned long __init unflatten_dt_node(unsigned long mem,
684 struct device_node *dad,
685 struct device_node ***allnextpp)
687 struct device_node *np;
688 struct property *pp, **prev_pp = NULL;
693 tag = *((u32 *)(*p));
694 if (tag != OF_DT_BEGIN_NODE) {
695 printk("Weird tag at start of node: %x\n", tag);
700 l = strlen(pathp) + 1;
701 *p = _ALIGN(*p + l, 4);
703 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + l,
704 __alignof__(struct device_node));
706 memset(np, 0, sizeof(*np));
707 np->full_name = ((char*)np) + sizeof(struct device_node);
708 memcpy(np->full_name, pathp, l);
709 prev_pp = &np->properties;
711 *allnextpp = &np->allnext;
714 /* we temporarily use the `next' field as `last_child'. */
718 dad->next->sibling = np;
726 tag = *((u32 *)(*p));
727 if (tag != OF_DT_PROP)
731 noff = *((u32 *)((*p) + 4));
732 *p = _ALIGN((*p) + 8, sz >= 8 ? 8 : 4);
734 pname = find_flat_dt_string(noff);
736 printk("Can't find property name in list !\n");
739 l = strlen(pname) + 1;
740 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
741 __alignof__(struct property));
743 if (strcmp(pname, "linux,phandle") == 0) {
744 np->node = *((u32 *)*p);
745 if (np->linux_phandle == 0)
746 np->linux_phandle = np->node;
748 if (strcmp(pname, "ibm,phandle") == 0)
749 np->linux_phandle = *((u32 *)*p);
752 pp->value = (void *)*p;
756 *p = _ALIGN((*p) + sz, 4);
760 np->name = get_property(np, "name", NULL);
761 np->type = get_property(np, "device_type", NULL);
768 while (tag == OF_DT_BEGIN_NODE) {
769 mem = unflatten_dt_node(mem, p, np, allnextpp);
770 tag = *((u32 *)(*p));
772 if (tag != OF_DT_END_NODE) {
773 printk("Weird tag at start of node: %x\n", tag);
782 * unflattens the device-tree passed by the firmware, creating the
783 * tree of struct device_node. It also fills the "name" and "type"
784 * pointers of the nodes so the normal device-tree walking functions
785 * can be used (this used to be done by finish_device_tree)
787 void __init unflatten_device_tree(void)
789 unsigned long start, mem, size;
790 struct device_node **allnextp = &allnodes;
794 DBG(" -> unflatten_device_tree()\n");
796 /* First pass, scan for size */
797 start = ((unsigned long)initial_boot_params) +
798 initial_boot_params->off_dt_struct;
799 size = unflatten_dt_node(0, &start, NULL, NULL);
801 DBG(" size is %lx, allocating...\n", size);
803 /* Allocate memory for the expanded device tree */
804 mem = (unsigned long)abs_to_virt(lmb_alloc(size,
805 __alignof__(struct device_node)));
806 DBG(" unflattening...\n", mem);
808 /* Second pass, do actual unflattening */
809 start = ((unsigned long)initial_boot_params) +
810 initial_boot_params->off_dt_struct;
811 unflatten_dt_node(mem, &start, NULL, &allnextp);
812 if (*((u32 *)start) != OF_DT_END)
813 printk(KERN_WARNING "Weird tag at end of tree: %x\n", *((u32 *)start));
816 /* Get pointer to OF "/chosen" node for use everywhere */
817 of_chosen = of_find_node_by_path("/chosen");
819 /* Retreive command line */
820 if (of_chosen != NULL) {
821 p = (char *)get_property(of_chosen, "bootargs", &l);
822 if (p != NULL && l > 0)
823 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
825 #ifdef CONFIG_CMDLINE
826 if (l == 0 || (l == 1 && (*p) == 0))
827 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
828 #endif /* CONFIG_CMDLINE */
830 DBG("Command line is: %s\n", cmd_line);
832 DBG(" <- unflatten_device_tree()\n");
836 static int __init early_init_dt_scan_cpus(unsigned long node,
837 const char *full_path, void *data)
839 char *type = get_flat_dt_prop(node, "device_type", NULL);
841 /* We are scanning "cpu" nodes only */
842 if (type == NULL || strcmp(type, "cpu") != 0)
845 /* On LPAR, look for the first ibm,pft-size property for the hash table size
847 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && naca->pftSize == 0) {
849 pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
850 if (pft_size != NULL) {
851 /* pft_size[0] is the NUMA CEC cookie */
852 naca->pftSize = pft_size[1];
856 if (initial_boot_params && initial_boot_params->version >= 2) {
857 /* version 2 of the kexec param format adds the phys cpuid
860 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
863 /* Check if it's the boot-cpu, set it's hw index in paca now */
864 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
865 u32 *prop = get_flat_dt_prop(node, "reg", NULL);
866 set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
867 boot_cpuid_phys = get_hard_smp_processor_id(0);
874 static int __init early_init_dt_scan_chosen(unsigned long node,
875 const char *full_path, void *data)
879 if (strcmp(full_path, "/chosen") != 0)
882 /* get platform type */
883 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
886 systemcfg->platform = *prop;
888 /* check if iommu is forced on or off */
889 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
891 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
894 #ifdef CONFIG_PPC_PSERIES
895 /* To help early debugging via the front panel, we retreive a minimal
896 * set of RTAS infos now if available
901 basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
902 entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
903 prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
904 if (basep && entryp && prop) {
906 rtas.entry = *entryp;
910 #endif /* CONFIG_PPC_PSERIES */
916 static int __init early_init_dt_scan_root(unsigned long node,
917 const char *full_path, void *data)
921 if (strcmp(full_path, "/") != 0)
924 prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
925 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
927 prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
928 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
934 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
939 /* Ignore more than 2 cells */
955 static int __init early_init_dt_scan_memory(unsigned long node,
956 const char *full_path, void *data)
958 char *type = get_flat_dt_prop(node, "device_type", NULL);
962 /* We are scanning "memory" nodes only */
963 if (type == NULL || strcmp(type, "memory") != 0)
966 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
970 endp = reg + (l / sizeof(cell_t));
972 DBG("memory scan node %s ...\n", full_path);
973 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
974 unsigned long base, size;
976 base = dt_mem_next_cell(dt_root_addr_cells, ®);
977 size = dt_mem_next_cell(dt_root_size_cells, ®);
981 DBG(" - %lx , %lx\n", base, size);
983 if (base >= 0x80000000ul)
985 if ((base + size) > 0x80000000ul)
986 size = 0x80000000ul - base;
993 static void __init early_reserve_mem(void)
996 u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
997 initial_boot_params->off_mem_rsvmap);
999 base = *(reserve_map++);
1000 size = *(reserve_map++);
1003 DBG("reserving: %lx -> %lx\n", base, size);
1004 lmb_reserve(base, size);
1008 DBG("memory reserved, lmbs :\n");
1013 void __init early_init_devtree(void *params)
1015 DBG(" -> early_init_devtree()\n");
1017 /* Setup flat device-tree pointer */
1018 initial_boot_params = params;
1020 /* By default, hash size is not set */
1023 /* Retreive various informations from the /chosen node of the
1024 * device-tree, including the platform type, initrd location and
1025 * size, TCE reserve, and more ...
1027 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1029 /* Scan memory nodes and rebuild LMBs */
1031 scan_flat_dt(early_init_dt_scan_root, NULL);
1032 scan_flat_dt(early_init_dt_scan_memory, NULL);
1034 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1035 lmb_reserve(0, __pa(klimit));
1037 DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
1039 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1040 early_reserve_mem();
1042 DBG("Scanning CPUs ...\n");
1044 /* Retreive hash table size from flattened tree */
1045 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1047 /* If hash size wasn't obtained above, we calculate it now based on
1048 * the total RAM size
1050 if (naca->pftSize == 0) {
1051 unsigned long rnd_mem_size, pteg_count;
1053 /* round mem_size up to next power of 2 */
1054 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
1055 if (rnd_mem_size < systemcfg->physicalMemorySize)
1059 pteg_count = (rnd_mem_size >> (12 + 1));
1061 naca->pftSize = __ilog2(pteg_count << 7);
1064 DBG("Hash pftSize: %x\n", (int)naca->pftSize);
1065 DBG(" <- early_init_devtree()\n");
1071 prom_n_addr_cells(struct device_node* np)
1077 ip = (int *) get_property(np, "#address-cells", NULL);
1080 } while (np->parent);
1081 /* No #address-cells property for the root node, default to 1 */
1086 prom_n_size_cells(struct device_node* np)
1092 ip = (int *) get_property(np, "#size-cells", NULL);
1095 } while (np->parent);
1096 /* No #size-cells property for the root node, default to 1 */
1101 * Work out the sense (active-low level / active-high edge)
1102 * of each interrupt from the device tree.
1104 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1106 struct device_node *np;
1109 /* default to level-triggered */
1110 memset(senses, 1, max - off);
1112 for (np = allnodes; np != 0; np = np->allnext) {
1113 for (j = 0; j < np->n_intrs; j++) {
1114 i = np->intrs[j].line;
1115 if (i >= off && i < max)
1116 senses[i-off] = np->intrs[j].sense ?
1117 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
1118 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
1124 * Construct and return a list of the device_nodes with a given name.
1126 struct device_node *
1127 find_devices(const char *name)
1129 struct device_node *head, **prevp, *np;
1132 for (np = allnodes; np != 0; np = np->allnext) {
1133 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1143 * Construct and return a list of the device_nodes with a given type.
1145 struct device_node *
1146 find_type_devices(const char *type)
1148 struct device_node *head, **prevp, *np;
1151 for (np = allnodes; np != 0; np = np->allnext) {
1152 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1162 * Returns all nodes linked together
1164 struct device_node *
1165 find_all_nodes(void)
1167 struct device_node *head, **prevp, *np;
1170 for (np = allnodes; np != 0; np = np->allnext) {
1178 /** Checks if the given "compat" string matches one of the strings in
1179 * the device's "compatible" property
1182 device_is_compatible(struct device_node *device, const char *compat)
1187 cp = (char *) get_property(device, "compatible", &cplen);
1191 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1203 * Indicates whether the root node has a given value in its
1204 * compatible property.
1207 machine_is_compatible(const char *compat)
1209 struct device_node *root;
1212 root = of_find_node_by_path("/");
1214 rc = device_is_compatible(root, compat);
1221 * Construct and return a list of the device_nodes with a given type
1222 * and compatible property.
1224 struct device_node *
1225 find_compatible_devices(const char *type, const char *compat)
1227 struct device_node *head, **prevp, *np;
1230 for (np = allnodes; np != 0; np = np->allnext) {
1232 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1234 if (device_is_compatible(np, compat)) {
1244 * Find the device_node with a given full_name.
1246 struct device_node *
1247 find_path_device(const char *path)
1249 struct device_node *np;
1251 for (np = allnodes; np != 0; np = np->allnext)
1252 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1259 * New implementation of the OF "find" APIs, return a refcounted
1260 * object, call of_node_put() when done. The device tree and list
1261 * are protected by a rw_lock.
1263 * Note that property management will need some locking as well,
1264 * this isn't dealt with yet.
1269 * of_find_node_by_name - Find a node by its "name" property
1270 * @from: The node to start searching from or NULL, the node
1271 * you pass will not be searched, only the next one
1272 * will; typically, you pass what the previous call
1273 * returned. of_node_put() will be called on it
1274 * @name: The name string to match against
1276 * Returns a node pointer with refcount incremented, use
1277 * of_node_put() on it when done.
1279 struct device_node *of_find_node_by_name(struct device_node *from,
1282 struct device_node *np;
1284 read_lock(&devtree_lock);
1285 np = from ? from->allnext : allnodes;
1286 for (; np != 0; np = np->allnext)
1287 if (np->name != 0 && strcasecmp(np->name, name) == 0
1292 read_unlock(&devtree_lock);
1295 EXPORT_SYMBOL(of_find_node_by_name);
1298 * of_find_node_by_type - Find a node by its "device_type" property
1299 * @from: The node to start searching from or NULL, the node
1300 * you pass will not be searched, only the next one
1301 * will; typically, you pass what the previous call
1302 * returned. of_node_put() will be called on it
1303 * @name: The type string to match against
1305 * Returns a node pointer with refcount incremented, use
1306 * of_node_put() on it when done.
1308 struct device_node *of_find_node_by_type(struct device_node *from,
1311 struct device_node *np;
1313 read_lock(&devtree_lock);
1314 np = from ? from->allnext : allnodes;
1315 for (; np != 0; np = np->allnext)
1316 if (np->type != 0 && strcasecmp(np->type, type) == 0
1321 read_unlock(&devtree_lock);
1324 EXPORT_SYMBOL(of_find_node_by_type);
1327 * of_find_compatible_node - Find a node based on type and one of the
1328 * tokens in its "compatible" property
1329 * @from: The node to start searching from or NULL, the node
1330 * you pass will not be searched, only the next one
1331 * will; typically, you pass what the previous call
1332 * returned. of_node_put() will be called on it
1333 * @type: The type string to match "device_type" or NULL to ignore
1334 * @compatible: The string to match to one of the tokens in the device
1335 * "compatible" list.
1337 * Returns a node pointer with refcount incremented, use
1338 * of_node_put() on it when done.
1340 struct device_node *of_find_compatible_node(struct device_node *from,
1341 const char *type, const char *compatible)
1343 struct device_node *np;
1345 read_lock(&devtree_lock);
1346 np = from ? from->allnext : allnodes;
1347 for (; np != 0; np = np->allnext) {
1349 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1351 if (device_is_compatible(np, compatible) && of_node_get(np))
1356 read_unlock(&devtree_lock);
1359 EXPORT_SYMBOL(of_find_compatible_node);
1362 * of_find_node_by_path - Find a node matching a full OF path
1363 * @path: The full path to match
1365 * Returns a node pointer with refcount incremented, use
1366 * of_node_put() on it when done.
1368 struct device_node *of_find_node_by_path(const char *path)
1370 struct device_node *np = allnodes;
1372 read_lock(&devtree_lock);
1373 for (; np != 0; np = np->allnext)
1374 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1377 read_unlock(&devtree_lock);
1380 EXPORT_SYMBOL(of_find_node_by_path);
1383 * of_find_node_by_phandle - Find a node given a phandle
1384 * @handle: phandle of the node to find
1386 * Returns a node pointer with refcount incremented, use
1387 * of_node_put() on it when done.
1389 struct device_node *of_find_node_by_phandle(phandle handle)
1391 struct device_node *np;
1393 read_lock(&devtree_lock);
1394 for (np = allnodes; np != 0; np = np->allnext)
1395 if (np->linux_phandle == handle)
1399 read_unlock(&devtree_lock);
1402 EXPORT_SYMBOL(of_find_node_by_phandle);
1405 * of_find_all_nodes - Get next node in global list
1406 * @prev: Previous node or NULL to start iteration
1407 * of_node_put() will be called on it
1409 * Returns a node pointer with refcount incremented, use
1410 * of_node_put() on it when done.
1412 struct device_node *of_find_all_nodes(struct device_node *prev)
1414 struct device_node *np;
1416 read_lock(&devtree_lock);
1417 np = prev ? prev->allnext : allnodes;
1418 for (; np != 0; np = np->allnext)
1419 if (of_node_get(np))
1423 read_unlock(&devtree_lock);
1426 EXPORT_SYMBOL(of_find_all_nodes);
1429 * of_get_parent - Get a node's parent if any
1430 * @node: Node to get parent
1432 * Returns a node pointer with refcount incremented, use
1433 * of_node_put() on it when done.
1435 struct device_node *of_get_parent(const struct device_node *node)
1437 struct device_node *np;
1442 read_lock(&devtree_lock);
1443 np = of_node_get(node->parent);
1444 read_unlock(&devtree_lock);
1447 EXPORT_SYMBOL(of_get_parent);
1450 * of_get_next_child - Iterate a node childs
1451 * @node: parent node
1452 * @prev: previous child of the parent node, or NULL to get first
1454 * Returns a node pointer with refcount incremented, use
1455 * of_node_put() on it when done.
1457 struct device_node *of_get_next_child(const struct device_node *node,
1458 struct device_node *prev)
1460 struct device_node *next;
1462 read_lock(&devtree_lock);
1463 next = prev ? prev->sibling : node->child;
1464 for (; next != 0; next = next->sibling)
1465 if (of_node_get(next))
1469 read_unlock(&devtree_lock);
1472 EXPORT_SYMBOL(of_get_next_child);
1475 * of_node_get - Increment refcount of a node
1476 * @node: Node to inc refcount, NULL is supported to
1477 * simplify writing of callers
1479 * Returns the node itself or NULL if gone.
1481 struct device_node *of_node_get(struct device_node *node)
1483 if (node && !OF_IS_STALE(node)) {
1484 atomic_inc(&node->_users);
1489 EXPORT_SYMBOL(of_node_get);
1492 * of_node_cleanup - release a dynamically allocated node
1493 * @arg: Node to be released
1495 static void of_node_cleanup(struct device_node *node)
1497 struct property *prop = node->properties;
1499 if (!OF_IS_DYNAMIC(node))
1502 struct property *next = prop->next;
1510 kfree(node->full_name);
1515 * of_node_put - Decrement refcount of a node
1516 * @node: Node to dec refcount, NULL is supported to
1517 * simplify writing of callers
1520 void of_node_put(struct device_node *node)
1525 WARN_ON(0 == atomic_read(&node->_users));
1527 if (OF_IS_STALE(node)) {
1528 if (atomic_dec_and_test(&node->_users)) {
1529 of_node_cleanup(node);
1534 atomic_dec(&node->_users);
1536 EXPORT_SYMBOL(of_node_put);
1539 * derive_parent - basically like dirname(1)
1540 * @path: the full_name of a node to be added to the tree
1542 * Returns the node which should be the parent of the node
1543 * described by path. E.g., for path = "/foo/bar", returns
1544 * the node with full_name = "/foo".
1546 static struct device_node *derive_parent(const char *path)
1548 struct device_node *parent = NULL;
1549 char *parent_path = "/";
1550 size_t parent_path_len = strrchr(path, '/') - path + 1;
1552 /* reject if path is "/" */
1553 if (!strcmp(path, "/"))
1556 if (strrchr(path, '/') != path) {
1557 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
1560 strlcpy(parent_path, path, parent_path_len);
1562 parent = of_find_node_by_path(parent_path);
1563 if (strcmp(parent_path, "/"))
1569 * Routines for "runtime" addition and removal of device tree nodes.
1571 #ifdef CONFIG_PROC_DEVICETREE
1573 * Add a node to /proc/device-tree.
1575 static void add_node_proc_entries(struct device_node *np)
1577 struct proc_dir_entry *ent;
1579 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
1581 proc_device_tree_add_node(np, ent);
1584 static void remove_node_proc_entries(struct device_node *np)
1586 struct property *pp = np->properties;
1587 struct device_node *parent = np->parent;
1590 remove_proc_entry(pp->name, np->pde);
1594 /* Assuming that symlinks have the same parent directory as
1598 remove_proc_entry(np->name_link->name, parent->pde);
1600 remove_proc_entry(np->addr_link->name, parent->pde);
1602 remove_proc_entry(np->pde->name, parent->pde);
1604 #else /* !CONFIG_PROC_DEVICETREE */
1605 static void add_node_proc_entries(struct device_node *np)
1610 static void remove_node_proc_entries(struct device_node *np)
1614 #endif /* CONFIG_PROC_DEVICETREE */
1617 * Fix up n_intrs and intrs fields in a new device node
1620 static int of_finish_dynamic_node_interrupts(struct device_node *node)
1622 int intrcells, intlen, i;
1623 unsigned *irq, *ints, virq;
1624 struct device_node *ic;
1626 ints = (unsigned int *)get_property(node, "interrupts", &intlen);
1627 intrcells = prom_n_intr_cells(node);
1628 intlen /= intrcells * sizeof(unsigned int);
1629 node->n_intrs = intlen;
1630 node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
1635 for (i = 0; i < intlen; ++i) {
1637 node->intrs[i].line = 0;
1638 node->intrs[i].sense = 1;
1639 n = map_interrupt(&irq, &ic, node, ints, intrcells);
1642 virq = virt_irq_create_mapping(irq[0]);
1643 if (virq == NO_IRQ) {
1644 printk(KERN_CRIT "Could not allocate interrupt "
1645 "number for %s\n", node->full_name);
1648 node->intrs[i].line = irq_offset_up(virq);
1650 node->intrs[i].sense = irq[1];
1652 printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
1654 for (j = 0; j < n; ++j)
1655 printk(" %d", irq[j]);
1665 * Fix up the uninitialized fields in a new device node:
1666 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1668 * A lot of boot-time code is duplicated here, because functions such
1669 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1672 * This should probably be split up into smaller chunks.
1675 static int of_finish_dynamic_node(struct device_node *node)
1677 struct device_node *parent = of_get_parent(node);
1680 phandle *ibm_phandle;
1682 node->name = get_property(node, "name", NULL);
1683 node->type = get_property(node, "device_type", NULL);
1690 /* We don't support that function on PowerMac, at least
1693 if (systemcfg->platform == PLATFORM_POWERMAC)
1696 /* fix up new node's linux_phandle field */
1697 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1698 node->linux_phandle = *ibm_phandle;
1700 /* do the work of interpret_pci_props */
1701 if (parent->type && !strcmp(parent->type, "pci")) {
1702 struct address_range *adr;
1703 struct pci_reg_property *pci_addrs;
1706 pci_addrs = (struct pci_reg_property *)
1707 get_property(node, "assigned-addresses", &l);
1708 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
1710 adr = kmalloc(sizeof(struct address_range) *
1711 (l / sizeof(struct pci_reg_property)),
1717 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
1718 adr[i].space = pci_addrs[i].addr.a_hi;
1719 adr[i].address = pci_addrs[i].addr.a_lo;
1720 adr[i].size = pci_addrs[i].size_lo;
1728 /* now do the work of finish_node_interrupts */
1729 if (get_property(node, "interrupts", NULL)) {
1730 err = of_finish_dynamic_node_interrupts(node);
1734 /* now do the rough equivalent of update_dn_pci_info, this
1735 * probably is not correct for phb's, but should work for
1739 node->phb = parent->phb;
1741 regs = (u32 *)get_property(node, "reg", NULL);
1743 node->busno = (regs[0] >> 16) & 0xff;
1744 node->devfn = (regs[0] >> 8) & 0xff;
1747 /* fixing up iommu_table */
1749 #ifdef CONFIG_PPC_PSERIES
1750 if (strcmp(node->name, "pci") == 0 &&
1751 get_property(node, "ibm,dma-window", NULL)) {
1752 node->bussubno = node->busno;
1753 iommu_devnode_init_pSeries(node);
1755 node->iommu_table = parent->iommu_table;
1756 #endif /* CONFIG_PPC_PSERIES */
1759 of_node_put(parent);
1764 * Given a path and a property list, construct an OF device node, add
1765 * it to the device tree and global list, and place it in
1766 * /proc/device-tree. This function may sleep.
1768 int of_add_node(const char *path, struct property *proplist)
1770 struct device_node *np;
1773 np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
1777 memset(np, 0, sizeof(*np));
1779 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
1780 if (!np->full_name) {
1784 strcpy(np->full_name, path);
1786 np->properties = proplist;
1787 OF_MARK_DYNAMIC(np);
1789 np->parent = derive_parent(path);
1792 return -EINVAL; /* could also be ENOMEM, though */
1795 if (0 != (err = of_finish_dynamic_node(np))) {
1800 write_lock(&devtree_lock);
1801 np->sibling = np->parent->child;
1802 np->allnext = allnodes;
1803 np->parent->child = np;
1805 write_unlock(&devtree_lock);
1807 add_node_proc_entries(np);
1809 of_node_put(np->parent);
1815 * Prepare an OF node for removal from system
1817 static void of_cleanup_node(struct device_node *np)
1819 if (np->iommu_table && get_property(np, "ibm,dma-window", NULL))
1820 iommu_free_table(np);
1824 * Remove an OF device node from the system.
1825 * Caller should have already "gotten" np.
1827 int of_remove_node(struct device_node *np)
1829 struct device_node *parent, *child;
1831 parent = of_get_parent(np);
1835 if ((child = of_get_next_child(np, NULL))) {
1840 of_cleanup_node(np);
1842 write_lock(&devtree_lock);
1844 remove_node_proc_entries(np);
1846 allnodes = np->allnext;
1848 struct device_node *prev;
1849 for (prev = allnodes;
1850 prev->allnext != np;
1851 prev = prev->allnext)
1853 prev->allnext = np->allnext;
1856 if (parent->child == np)
1857 parent->child = np->sibling;
1859 struct device_node *prevsib;
1860 for (prevsib = np->parent->child;
1861 prevsib->sibling != np;
1862 prevsib = prevsib->sibling)
1864 prevsib->sibling = np->sibling;
1866 write_unlock(&devtree_lock);
1867 of_node_put(parent);
1872 * Find a property with a given name for a given node
1873 * and return the value.
1876 get_property(struct device_node *np, const char *name, int *lenp)
1878 struct property *pp;
1880 for (pp = np->properties; pp != 0; pp = pp->next)
1881 if (strcmp(pp->name, name) == 0) {
1890 * Add a property to a node
1893 prom_add_property(struct device_node* np, struct property* prop)
1895 struct property **next = &np->properties;
1899 next = &(*next)->next;
1905 print_properties(struct device_node *np)
1907 struct property *pp;
1911 for (pp = np->properties; pp != 0; pp = pp->next) {
1912 printk(KERN_INFO "%s", pp->name);
1913 for (i = strlen(pp->name); i < 16; ++i)
1915 cp = (char *) pp->value;
1916 for (i = pp->length; i > 0; --i, ++cp)
1917 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1918 || (i == 1 && *cp != 0))
1920 if (i == 0 && pp->length > 1) {
1921 /* looks like a string */
1922 printk(" %s\n", (char *) pp->value);
1924 /* dump it in hex */
1928 if (pp->length % 4 == 0) {
1929 unsigned int *p = (unsigned int *) pp->value;
1932 for (i = 0; i < n; ++i) {
1933 if (i != 0 && (i % 4) == 0)
1935 printk(" %08x", *p++);
1938 unsigned char *bp = pp->value;
1940 for (i = 0; i < n; ++i) {
1941 if (i != 0 && (i % 16) == 0)
1943 printk(" %02x", *bp++);
1947 if (pp->length > 64)
1948 printk(" ... (length = %d)\n",