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>
48 #include <asm/iommu.h>
49 #include <asm/bootinfo.h>
50 #include <asm/ppcdebug.h>
51 #include <asm/btext.h>
52 #include <asm/sections.h>
53 #include <asm/machdep.h>
56 #define DBG(fmt...) udbg_printf(fmt)
61 struct pci_reg_property {
62 struct pci_address addr;
67 struct isa_reg_property {
74 typedef unsigned long interpret_func(struct device_node *, unsigned long,
77 extern struct rtas_t rtas;
78 extern struct lmb lmb;
79 extern unsigned long klimit;
81 static int __initdata dt_root_addr_cells;
82 static int __initdata dt_root_size_cells;
83 static int __initdata iommu_is_off;
84 int __initdata iommu_force_on;
88 static struct boot_param_header *initial_boot_params __initdata;
90 struct boot_param_header *initial_boot_params;
93 static struct device_node *allnodes = NULL;
95 /* use when traversing tree through the allnext, child, sibling,
96 * or parent members of struct device_node.
98 static DEFINE_RWLOCK(devtree_lock);
100 /* export that to outside world */
101 struct device_node *of_chosen;
104 * Find the device_node with a given phandle.
106 static struct device_node * find_phandle(phandle ph)
108 struct device_node *np;
110 for (np = allnodes; np != 0; np = np->allnext)
111 if (np->linux_phandle == ph)
117 * Find the interrupt parent of a node.
119 static struct device_node * __devinit intr_parent(struct device_node *p)
123 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
126 return find_phandle(*parp);
130 * Find out the size of each entry of the interrupts property
133 int __devinit prom_n_intr_cells(struct device_node *np)
135 struct device_node *p;
138 for (p = np; (p = intr_parent(p)) != NULL; ) {
139 icp = (unsigned int *)
140 get_property(p, "#interrupt-cells", NULL);
143 if (get_property(p, "interrupt-controller", NULL) != NULL
144 || get_property(p, "interrupt-map", NULL) != NULL) {
145 printk("oops, node %s doesn't have #interrupt-cells\n",
151 printk("prom_n_intr_cells failed for %s\n", np->full_name);
157 * Map an interrupt from a device up to the platform interrupt
160 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
161 struct device_node *np, unsigned int *ints,
164 struct device_node *p, *ipar;
165 unsigned int *imap, *imask, *ip;
166 int i, imaplen, match;
167 int newintrc = 0, newaddrc = 0;
171 reg = (unsigned int *) get_property(np, "reg", NULL);
172 naddrc = prom_n_addr_cells(np);
175 if (get_property(p, "interrupt-controller", NULL) != NULL)
176 /* this node is an interrupt controller, stop here */
178 imap = (unsigned int *)
179 get_property(p, "interrupt-map", &imaplen);
184 imask = (unsigned int *)
185 get_property(p, "interrupt-map-mask", NULL);
187 printk("oops, %s has interrupt-map but no mask\n",
191 imaplen /= sizeof(unsigned int);
194 while (imaplen > 0 && !match) {
195 /* check the child-interrupt field */
197 for (i = 0; i < naddrc && match; ++i)
198 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
199 for (; i < naddrc + nintrc && match; ++i)
200 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
201 imap += naddrc + nintrc;
202 imaplen -= naddrc + nintrc;
203 /* grab the interrupt parent */
204 ipar = find_phandle((phandle) *imap++);
207 printk("oops, no int parent %x in map of %s\n",
208 imap[-1], p->full_name);
211 /* find the parent's # addr and intr cells */
212 ip = (unsigned int *)
213 get_property(ipar, "#interrupt-cells", NULL);
215 printk("oops, no #interrupt-cells on %s\n",
220 ip = (unsigned int *)
221 get_property(ipar, "#address-cells", NULL);
222 newaddrc = (ip == NULL)? 0: *ip;
223 imap += newaddrc + newintrc;
224 imaplen -= newaddrc + newintrc;
227 printk("oops, error decoding int-map on %s, len=%d\n",
228 p->full_name, imaplen);
233 printk("oops, no match in %s int-map for %s\n",
234 p->full_name, np->full_name);
241 ints = imap - nintrc;
246 printk("hmmm, int tree for %s doesn't have ctrler\n",
256 static unsigned long __init finish_node_interrupts(struct device_node *np,
257 unsigned long mem_start,
261 int intlen, intrcells, intrcount;
263 unsigned int *irq, virq;
264 struct device_node *ic;
266 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
269 intrcells = prom_n_intr_cells(np);
270 intlen /= intrcells * sizeof(unsigned int);
271 np->intrs = (struct interrupt_info *) mem_start;
272 mem_start += intlen * sizeof(struct interrupt_info);
278 for (i = 0; i < intlen; ++i, ints += intrcells) {
279 n = map_interrupt(&irq, &ic, np, ints, intrcells);
283 /* don't map IRQ numbers under a cascaded 8259 controller */
284 if (ic && device_is_compatible(ic, "chrp,iic")) {
285 np->intrs[intrcount].line = irq[0];
287 virq = virt_irq_create_mapping(irq[0]);
288 if (virq == NO_IRQ) {
289 printk(KERN_CRIT "Could not allocate interrupt"
290 " number for %s\n", np->full_name);
293 np->intrs[intrcount].line = irq_offset_up(virq);
296 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
297 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
298 char *name = get_property(ic->parent, "name", NULL);
299 if (name && !strcmp(name, "u3"))
300 np->intrs[intrcount].line += 128;
302 np->intrs[intrcount].sense = 1;
304 np->intrs[intrcount].sense = irq[1];
306 printk("hmmm, got %d intr cells for %s:", n,
308 for (j = 0; j < n; ++j)
309 printk(" %d", irq[j]);
314 np->n_intrs = intrcount;
319 static unsigned long __init interpret_pci_props(struct device_node *np,
320 unsigned long mem_start,
321 int naddrc, int nsizec,
324 struct address_range *adr;
325 struct pci_reg_property *pci_addrs;
328 pci_addrs = (struct pci_reg_property *)
329 get_property(np, "assigned-addresses", &l);
330 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
332 adr = (struct address_range *) mem_start;
333 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
335 adr[i].space = pci_addrs[i].addr.a_hi;
336 adr[i].address = pci_addrs[i].addr.a_lo;
337 adr[i].size = pci_addrs[i].size_lo;
343 mem_start += i * sizeof(struct address_range);
348 static unsigned long __init interpret_dbdma_props(struct device_node *np,
349 unsigned long mem_start,
350 int naddrc, int nsizec,
353 struct reg_property32 *rp;
354 struct address_range *adr;
355 unsigned long base_address;
357 struct device_node *db;
361 for (db = np->parent; db != NULL; db = db->parent) {
362 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
363 base_address = db->addrs[0].address;
369 rp = (struct reg_property32 *) get_property(np, "reg", &l);
370 if (rp != 0 && l >= sizeof(struct reg_property32)) {
372 adr = (struct address_range *) mem_start;
373 while ((l -= sizeof(struct reg_property32)) >= 0) {
376 adr[i].address = rp[i].address + base_address;
377 adr[i].size = rp[i].size;
383 mem_start += i * sizeof(struct address_range);
389 static unsigned long __init interpret_macio_props(struct device_node *np,
390 unsigned long mem_start,
391 int naddrc, int nsizec,
394 struct reg_property32 *rp;
395 struct address_range *adr;
396 unsigned long base_address;
398 struct device_node *db;
402 for (db = np->parent; db != NULL; db = db->parent) {
403 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
404 base_address = db->addrs[0].address;
410 rp = (struct reg_property32 *) get_property(np, "reg", &l);
411 if (rp != 0 && l >= sizeof(struct reg_property32)) {
413 adr = (struct address_range *) mem_start;
414 while ((l -= sizeof(struct reg_property32)) >= 0) {
417 adr[i].address = rp[i].address + base_address;
418 adr[i].size = rp[i].size;
424 mem_start += i * sizeof(struct address_range);
430 static unsigned long __init interpret_isa_props(struct device_node *np,
431 unsigned long mem_start,
432 int naddrc, int nsizec,
435 struct isa_reg_property *rp;
436 struct address_range *adr;
439 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
440 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
442 adr = (struct address_range *) mem_start;
443 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
445 adr[i].space = rp[i].space;
446 adr[i].address = rp[i].address;
447 adr[i].size = rp[i].size;
453 mem_start += i * sizeof(struct address_range);
459 static unsigned long __init interpret_root_props(struct device_node *np,
460 unsigned long mem_start,
461 int naddrc, int nsizec,
464 struct address_range *adr;
467 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
469 rp = (unsigned int *) get_property(np, "reg", &l);
470 if (rp != 0 && l >= rpsize) {
472 adr = (struct address_range *) mem_start;
473 while ((l -= rpsize) >= 0) {
476 adr[i].address = rp[naddrc - 1];
477 adr[i].size = rp[naddrc + nsizec - 1];
480 rp += naddrc + nsizec;
484 mem_start += i * sizeof(struct address_range);
490 static unsigned long __init finish_node(struct device_node *np,
491 unsigned long mem_start,
492 interpret_func *ifunc,
493 int naddrc, int nsizec,
496 struct device_node *child;
499 /* get the device addresses and interrupts */
501 mem_start = ifunc(np, mem_start, naddrc, nsizec, measure_only);
503 mem_start = finish_node_interrupts(np, mem_start, measure_only);
505 /* Look for #address-cells and #size-cells properties. */
506 ip = (int *) get_property(np, "#address-cells", NULL);
509 ip = (int *) get_property(np, "#size-cells", NULL);
513 /* the f50 sets the name to 'display' and 'compatible' to what we
514 * expect for the name -- Cort
516 if (!strcmp(np->name, "display"))
517 np->name = get_property(np, "compatible", NULL);
519 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
520 ifunc = interpret_root_props;
521 else if (np->type == 0)
523 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
524 ifunc = interpret_pci_props;
525 else if (!strcmp(np->type, "dbdma"))
526 ifunc = interpret_dbdma_props;
527 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
528 ifunc = interpret_macio_props;
529 else if (!strcmp(np->type, "isa"))
530 ifunc = interpret_isa_props;
531 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
532 ifunc = interpret_root_props;
533 else if (!((ifunc == interpret_dbdma_props
534 || ifunc == interpret_macio_props)
535 && (!strcmp(np->type, "escc")
536 || !strcmp(np->type, "media-bay"))))
539 for (child = np->child; child != NULL; child = child->sibling)
540 mem_start = finish_node(child, mem_start, ifunc,
541 naddrc, nsizec, measure_only);
547 * finish_device_tree is called once things are running normally
548 * (i.e. with text and data mapped to the address they were linked at).
549 * It traverses the device tree and fills in some of the additional,
550 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
551 * mapping is also initialized at this point.
553 void __init finish_device_tree(void)
555 unsigned long mem, size;
557 DBG(" -> finish_device_tree\n");
559 if (ppc64_interrupt_controller == IC_INVALID) {
560 DBG("failed to configure interrupt controller type\n");
561 panic("failed to configure interrupt controller type\n");
564 /* Initialize virtual IRQ map */
567 /* Finish device-tree (pre-parsing some properties etc...) */
568 size = finish_node(allnodes, 0, NULL, 0, 0, 1);
569 mem = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
570 if (finish_node(allnodes, mem, NULL, 0, 0, 0) != mem + size)
573 DBG(" <- finish_device_tree\n");
577 #define printk udbg_printf
580 static inline char *find_flat_dt_string(u32 offset)
582 return ((char *)initial_boot_params) + initial_boot_params->off_dt_strings
587 * This function is used to scan the flattened device-tree, it is
588 * used to extract the memory informations at boot before we can
591 static int __init scan_flat_dt(int (*it)(unsigned long node,
592 const char *full_path, void *data),
595 unsigned long p = ((unsigned long)initial_boot_params) +
596 initial_boot_params->off_dt_struct;
600 u32 tag = *((u32 *)p);
604 if (tag == OF_DT_END_NODE)
606 if (tag == OF_DT_END)
608 if (tag == OF_DT_PROP) {
609 u32 sz = *((u32 *)p);
611 p = _ALIGN(p, sz >= 8 ? 8 : 4);
616 if (tag != OF_DT_BEGIN_NODE) {
617 printk(KERN_WARNING "Invalid tag %x scanning flattened"
618 " device tree !\n", tag);
622 p = _ALIGN(p + strlen(pathp) + 1, 4);
623 rc = it(p, pathp, data);
632 * This function can be used within scan_flattened_dt callback to get
633 * access to properties
635 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
638 unsigned long p = node;
641 u32 tag = *((u32 *)p);
646 if (tag != OF_DT_PROP)
650 noff = *((u32 *)(p + 4));
652 p = _ALIGN(p, sz >= 8 ? 8 : 4);
654 nstr = find_flat_dt_string(noff);
656 printk(KERN_WARNING "Can't find property index name !\n");
659 if (strcmp(name, nstr) == 0) {
669 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
674 *mem = _ALIGN(*mem, align);
681 static unsigned long __init unflatten_dt_node(unsigned long mem,
683 struct device_node *dad,
684 struct device_node ***allnextpp)
686 struct device_node *np;
687 struct property *pp, **prev_pp = NULL;
692 tag = *((u32 *)(*p));
693 if (tag != OF_DT_BEGIN_NODE) {
694 printk("Weird tag at start of node: %x\n", tag);
699 l = strlen(pathp) + 1;
700 *p = _ALIGN(*p + l, 4);
702 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + l,
703 __alignof__(struct device_node));
705 memset(np, 0, sizeof(*np));
706 np->full_name = ((char*)np) + sizeof(struct device_node);
707 memcpy(np->full_name, pathp, l);
708 prev_pp = &np->properties;
710 *allnextpp = &np->allnext;
713 /* we temporarily use the `next' field as `last_child'. */
717 dad->next->sibling = np;
720 kref_init(&np->kref);
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 && ppc64_pft_size == 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 ppc64_pft_size = 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 (ppc64_pft_size == 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 = max(rnd_mem_size >> (12 + 1), 1UL << 11);
1061 ppc64_pft_size = __ilog2(pteg_count << 7);
1064 DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
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
1481 struct device_node *of_node_get(struct device_node *node)
1484 kref_get(&node->kref);
1487 EXPORT_SYMBOL(of_node_get);
1489 static inline struct device_node * kref_to_device_node(struct kref *kref)
1491 return container_of(kref, struct device_node, kref);
1495 * of_node_release - release a dynamically allocated node
1496 * @kref: kref element of the node to be released
1498 * In of_node_put() this function is passed to kref_put()
1499 * as the destructor.
1501 static void of_node_release(struct kref *kref)
1503 struct device_node *node = kref_to_device_node(kref);
1504 struct property *prop = node->properties;
1506 if (!OF_IS_DYNAMIC(node))
1509 struct property *next = prop->next;
1517 kfree(node->full_name);
1522 * of_node_put - Decrement refcount of a node
1523 * @node: Node to dec refcount, NULL is supported to
1524 * simplify writing of callers
1527 void of_node_put(struct device_node *node)
1530 kref_put(&node->kref, of_node_release);
1532 EXPORT_SYMBOL(of_node_put);
1535 * derive_parent - basically like dirname(1)
1536 * @path: the full_name of a node to be added to the tree
1538 * Returns the node which should be the parent of the node
1539 * described by path. E.g., for path = "/foo/bar", returns
1540 * the node with full_name = "/foo".
1542 static struct device_node *derive_parent(const char *path)
1544 struct device_node *parent = NULL;
1545 char *parent_path = "/";
1546 size_t parent_path_len = strrchr(path, '/') - path + 1;
1548 /* reject if path is "/" */
1549 if (!strcmp(path, "/"))
1552 if (strrchr(path, '/') != path) {
1553 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
1556 strlcpy(parent_path, path, parent_path_len);
1558 parent = of_find_node_by_path(parent_path);
1559 if (strcmp(parent_path, "/"))
1565 * Routines for "runtime" addition and removal of device tree nodes.
1567 #ifdef CONFIG_PROC_DEVICETREE
1569 * Add a node to /proc/device-tree.
1571 static void add_node_proc_entries(struct device_node *np)
1573 struct proc_dir_entry *ent;
1575 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
1577 proc_device_tree_add_node(np, ent);
1580 static void remove_node_proc_entries(struct device_node *np)
1582 struct property *pp = np->properties;
1583 struct device_node *parent = np->parent;
1586 remove_proc_entry(pp->name, np->pde);
1590 /* Assuming that symlinks have the same parent directory as
1594 remove_proc_entry(np->name_link->name, parent->pde);
1596 remove_proc_entry(np->addr_link->name, parent->pde);
1598 remove_proc_entry(np->pde->name, parent->pde);
1600 #else /* !CONFIG_PROC_DEVICETREE */
1601 static void add_node_proc_entries(struct device_node *np)
1606 static void remove_node_proc_entries(struct device_node *np)
1610 #endif /* CONFIG_PROC_DEVICETREE */
1613 * Fix up n_intrs and intrs fields in a new device node
1616 static int of_finish_dynamic_node_interrupts(struct device_node *node)
1618 int intrcells, intlen, i;
1619 unsigned *irq, *ints, virq;
1620 struct device_node *ic;
1622 ints = (unsigned int *)get_property(node, "interrupts", &intlen);
1623 intrcells = prom_n_intr_cells(node);
1624 intlen /= intrcells * sizeof(unsigned int);
1625 node->n_intrs = intlen;
1626 node->intrs = kmalloc(sizeof(struct interrupt_info) * intlen,
1631 for (i = 0; i < intlen; ++i) {
1633 node->intrs[i].line = 0;
1634 node->intrs[i].sense = 1;
1635 n = map_interrupt(&irq, &ic, node, ints, intrcells);
1638 virq = virt_irq_create_mapping(irq[0]);
1639 if (virq == NO_IRQ) {
1640 printk(KERN_CRIT "Could not allocate interrupt "
1641 "number for %s\n", node->full_name);
1644 node->intrs[i].line = irq_offset_up(virq);
1646 node->intrs[i].sense = irq[1];
1648 printk(KERN_DEBUG "hmmm, got %d intr cells for %s:", n,
1650 for (j = 0; j < n; ++j)
1651 printk(" %d", irq[j]);
1661 * Fix up the uninitialized fields in a new device node:
1662 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1664 * A lot of boot-time code is duplicated here, because functions such
1665 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1668 * This should probably be split up into smaller chunks.
1671 static int of_finish_dynamic_node(struct device_node *node)
1673 struct device_node *parent = of_get_parent(node);
1676 phandle *ibm_phandle;
1678 node->name = get_property(node, "name", NULL);
1679 node->type = get_property(node, "device_type", NULL);
1686 /* We don't support that function on PowerMac, at least
1689 if (systemcfg->platform == PLATFORM_POWERMAC)
1692 /* fix up new node's linux_phandle field */
1693 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1694 node->linux_phandle = *ibm_phandle;
1696 /* do the work of interpret_pci_props */
1697 if (parent->type && !strcmp(parent->type, "pci")) {
1698 struct address_range *adr;
1699 struct pci_reg_property *pci_addrs;
1702 pci_addrs = (struct pci_reg_property *)
1703 get_property(node, "assigned-addresses", &l);
1704 if (pci_addrs != 0 && l >= sizeof(struct pci_reg_property)) {
1706 adr = kmalloc(sizeof(struct address_range) *
1707 (l / sizeof(struct pci_reg_property)),
1713 while ((l -= sizeof(struct pci_reg_property)) >= 0) {
1714 adr[i].space = pci_addrs[i].addr.a_hi;
1715 adr[i].address = pci_addrs[i].addr.a_lo;
1716 adr[i].size = pci_addrs[i].size_lo;
1724 /* now do the work of finish_node_interrupts */
1725 if (get_property(node, "interrupts", NULL)) {
1726 err = of_finish_dynamic_node_interrupts(node);
1730 /* now do the rough equivalent of update_dn_pci_info, this
1731 * probably is not correct for phb's, but should work for
1735 node->phb = parent->phb;
1737 regs = (u32 *)get_property(node, "reg", NULL);
1739 node->busno = (regs[0] >> 16) & 0xff;
1740 node->devfn = (regs[0] >> 8) & 0xff;
1744 of_node_put(parent);
1749 * Given a path and a property list, construct an OF device node, add
1750 * it to the device tree and global list, and place it in
1751 * /proc/device-tree. This function may sleep.
1753 int of_add_node(const char *path, struct property *proplist)
1755 struct device_node *np;
1758 np = kmalloc(sizeof(struct device_node), GFP_KERNEL);
1762 memset(np, 0, sizeof(*np));
1764 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
1765 if (!np->full_name) {
1769 strcpy(np->full_name, path);
1771 np->properties = proplist;
1772 OF_MARK_DYNAMIC(np);
1773 kref_init(&np->kref);
1775 np->parent = derive_parent(path);
1778 return -EINVAL; /* could also be ENOMEM, though */
1781 if (0 != (err = of_finish_dynamic_node(np))) {
1786 write_lock(&devtree_lock);
1787 np->sibling = np->parent->child;
1788 np->allnext = allnodes;
1789 np->parent->child = np;
1791 write_unlock(&devtree_lock);
1793 add_node_proc_entries(np);
1795 of_node_put(np->parent);
1801 * Prepare an OF node for removal from system
1803 static void of_cleanup_node(struct device_node *np)
1805 if (np->iommu_table && get_property(np, "ibm,dma-window", NULL))
1806 iommu_free_table(np);
1810 * "Unplug" a node from the device tree. The caller must hold
1811 * a reference to the node. The memory associated with the node
1812 * is not freed until its refcount goes to zero.
1814 int of_remove_node(struct device_node *np)
1816 struct device_node *parent, *child;
1818 parent = of_get_parent(np);
1822 if ((child = of_get_next_child(np, NULL))) {
1827 of_cleanup_node(np);
1829 write_lock(&devtree_lock);
1830 remove_node_proc_entries(np);
1832 allnodes = np->allnext;
1834 struct device_node *prev;
1835 for (prev = allnodes;
1836 prev->allnext != np;
1837 prev = prev->allnext)
1839 prev->allnext = np->allnext;
1842 if (parent->child == np)
1843 parent->child = np->sibling;
1845 struct device_node *prevsib;
1846 for (prevsib = np->parent->child;
1847 prevsib->sibling != np;
1848 prevsib = prevsib->sibling)
1850 prevsib->sibling = np->sibling;
1852 write_unlock(&devtree_lock);
1853 of_node_put(parent);
1854 of_node_put(np); /* Must decrement the refcount */
1859 * Find a property with a given name for a given node
1860 * and return the value.
1863 get_property(struct device_node *np, const char *name, int *lenp)
1865 struct property *pp;
1867 for (pp = np->properties; pp != 0; pp = pp->next)
1868 if (strcmp(pp->name, name) == 0) {
1877 * Add a property to a node
1880 prom_add_property(struct device_node* np, struct property* prop)
1882 struct property **next = &np->properties;
1886 next = &(*next)->next;
1892 print_properties(struct device_node *np)
1894 struct property *pp;
1898 for (pp = np->properties; pp != 0; pp = pp->next) {
1899 printk(KERN_INFO "%s", pp->name);
1900 for (i = strlen(pp->name); i < 16; ++i)
1902 cp = (char *) pp->value;
1903 for (i = pp->length; i > 0; --i, ++cp)
1904 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1905 || (i == 1 && *cp != 0))
1907 if (i == 0 && pp->length > 1) {
1908 /* looks like a string */
1909 printk(" %s\n", (char *) pp->value);
1911 /* dump it in hex */
1915 if (pp->length % 4 == 0) {
1916 unsigned int *p = (unsigned int *) pp->value;
1919 for (i = 0; i < n; ++i) {
1920 if (i != 0 && (i % 4) == 0)
1922 printk(" %08x", *p++);
1925 unsigned char *bp = pp->value;
1927 for (i = 0; i < n; ++i) {
1928 if (i != 0 && (i % 16) == 0)
1930 printk(" %02x", *bp++);
1934 if (pp->length > 64)
1935 printk(" ... (length = %d)\n",