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/stringify.h>
31 #include <linux/delay.h>
32 #include <linux/initrd.h>
33 #include <linux/bitops.h>
34 #include <linux/module.h>
39 #include <asm/abs_addr.h>
41 #include <asm/processor.h>
45 #include <asm/system.h>
47 #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>
55 #include <asm/pSeries_reconfig.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 int 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 DEFINE_RWLOCK(devtree_lock);
102 /* export that to outside world */
103 struct device_node *of_chosen;
106 * Wrapper for allocating memory for various data that needs to be
107 * attached to device nodes as they are processed at boot or when
108 * added to the device tree later (e.g. DLPAR). At boot there is
109 * already a region reserved so we just increment *mem_start by size;
110 * otherwise we call kmalloc.
112 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
117 return kmalloc(size, GFP_KERNEL);
125 * Find the device_node with a given phandle.
127 static struct device_node * find_phandle(phandle ph)
129 struct device_node *np;
131 for (np = allnodes; np != 0; np = np->allnext)
132 if (np->linux_phandle == ph)
138 * Find the interrupt parent of a node.
140 static struct device_node * __devinit intr_parent(struct device_node *p)
144 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
147 return find_phandle(*parp);
151 * Find out the size of each entry of the interrupts property
154 int __devinit prom_n_intr_cells(struct device_node *np)
156 struct device_node *p;
159 for (p = np; (p = intr_parent(p)) != NULL; ) {
160 icp = (unsigned int *)
161 get_property(p, "#interrupt-cells", NULL);
164 if (get_property(p, "interrupt-controller", NULL) != NULL
165 || get_property(p, "interrupt-map", NULL) != NULL) {
166 printk("oops, node %s doesn't have #interrupt-cells\n",
172 printk("prom_n_intr_cells failed for %s\n", np->full_name);
178 * Map an interrupt from a device up to the platform interrupt
181 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
182 struct device_node *np, unsigned int *ints,
185 struct device_node *p, *ipar;
186 unsigned int *imap, *imask, *ip;
187 int i, imaplen, match;
188 int newintrc = 0, newaddrc = 0;
192 reg = (unsigned int *) get_property(np, "reg", NULL);
193 naddrc = prom_n_addr_cells(np);
196 if (get_property(p, "interrupt-controller", NULL) != NULL)
197 /* this node is an interrupt controller, stop here */
199 imap = (unsigned int *)
200 get_property(p, "interrupt-map", &imaplen);
205 imask = (unsigned int *)
206 get_property(p, "interrupt-map-mask", NULL);
208 printk("oops, %s has interrupt-map but no mask\n",
212 imaplen /= sizeof(unsigned int);
215 while (imaplen > 0 && !match) {
216 /* check the child-interrupt field */
218 for (i = 0; i < naddrc && match; ++i)
219 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
220 for (; i < naddrc + nintrc && match; ++i)
221 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
222 imap += naddrc + nintrc;
223 imaplen -= naddrc + nintrc;
224 /* grab the interrupt parent */
225 ipar = find_phandle((phandle) *imap++);
228 printk("oops, no int parent %x in map of %s\n",
229 imap[-1], p->full_name);
232 /* find the parent's # addr and intr cells */
233 ip = (unsigned int *)
234 get_property(ipar, "#interrupt-cells", NULL);
236 printk("oops, no #interrupt-cells on %s\n",
241 ip = (unsigned int *)
242 get_property(ipar, "#address-cells", NULL);
243 newaddrc = (ip == NULL)? 0: *ip;
244 imap += newaddrc + newintrc;
245 imaplen -= newaddrc + newintrc;
248 printk("oops, error decoding int-map on %s, len=%d\n",
249 p->full_name, imaplen);
254 printk("oops, no match in %s int-map for %s\n",
255 p->full_name, np->full_name);
262 ints = imap - nintrc;
267 printk("hmmm, int tree for %s doesn't have ctrler\n",
277 static int __devinit finish_node_interrupts(struct device_node *np,
278 unsigned long *mem_start,
282 int intlen, intrcells, intrcount;
284 unsigned int *irq, virq;
285 struct device_node *ic;
287 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
290 intrcells = prom_n_intr_cells(np);
291 intlen /= intrcells * sizeof(unsigned int);
293 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
301 for (i = 0; i < intlen; ++i, ints += intrcells) {
302 n = map_interrupt(&irq, &ic, np, ints, intrcells);
306 /* don't map IRQ numbers under a cascaded 8259 controller */
307 if (ic && device_is_compatible(ic, "chrp,iic")) {
308 np->intrs[intrcount].line = irq[0];
310 virq = virt_irq_create_mapping(irq[0]);
311 if (virq == NO_IRQ) {
312 printk(KERN_CRIT "Could not allocate interrupt"
313 " number for %s\n", np->full_name);
316 np->intrs[intrcount].line = irq_offset_up(virq);
319 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
320 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
321 char *name = get_property(ic->parent, "name", NULL);
322 if (name && !strcmp(name, "u3"))
323 np->intrs[intrcount].line += 128;
324 else if (!(name && !strcmp(name, "mac-io")))
325 /* ignore other cascaded controllers, such as
329 np->intrs[intrcount].sense = 1;
331 np->intrs[intrcount].sense = irq[1];
333 printk("hmmm, got %d intr cells for %s:", n,
335 for (j = 0; j < n; ++j)
336 printk(" %d", irq[j]);
341 np->n_intrs = intrcount;
346 static int __devinit interpret_pci_props(struct device_node *np,
347 unsigned long *mem_start,
348 int naddrc, int nsizec,
351 struct address_range *adr;
352 struct pci_reg_property *pci_addrs;
355 pci_addrs = (struct pci_reg_property *)
356 get_property(np, "assigned-addresses", &l);
360 n_addrs = l / sizeof(*pci_addrs);
362 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
370 np->n_addrs = n_addrs;
372 for (i = 0; i < n_addrs; i++) {
373 adr[i].space = pci_addrs[i].addr.a_hi;
374 adr[i].address = pci_addrs[i].addr.a_lo |
375 ((u64)pci_addrs[i].addr.a_mid << 32);
376 adr[i].size = pci_addrs[i].size_lo;
382 static int __init interpret_dbdma_props(struct device_node *np,
383 unsigned long *mem_start,
384 int naddrc, int nsizec,
387 struct reg_property32 *rp;
388 struct address_range *adr;
389 unsigned long base_address;
391 struct device_node *db;
395 for (db = np->parent; db != NULL; db = db->parent) {
396 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
397 base_address = db->addrs[0].address;
403 rp = (struct reg_property32 *) get_property(np, "reg", &l);
404 if (rp != 0 && l >= sizeof(struct reg_property32)) {
406 adr = (struct address_range *) (*mem_start);
407 while ((l -= sizeof(struct reg_property32)) >= 0) {
410 adr[i].address = rp[i].address + base_address;
411 adr[i].size = rp[i].size;
417 (*mem_start) += i * sizeof(struct address_range);
423 static int __init interpret_macio_props(struct device_node *np,
424 unsigned long *mem_start,
425 int naddrc, int nsizec,
428 struct reg_property32 *rp;
429 struct address_range *adr;
430 unsigned long base_address;
432 struct device_node *db;
436 for (db = np->parent; db != NULL; db = db->parent) {
437 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
438 base_address = db->addrs[0].address;
444 rp = (struct reg_property32 *) get_property(np, "reg", &l);
445 if (rp != 0 && l >= sizeof(struct reg_property32)) {
447 adr = (struct address_range *) (*mem_start);
448 while ((l -= sizeof(struct reg_property32)) >= 0) {
451 adr[i].address = rp[i].address + base_address;
452 adr[i].size = rp[i].size;
458 (*mem_start) += i * sizeof(struct address_range);
464 static int __init interpret_isa_props(struct device_node *np,
465 unsigned long *mem_start,
466 int naddrc, int nsizec,
469 struct isa_reg_property *rp;
470 struct address_range *adr;
473 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
474 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
476 adr = (struct address_range *) (*mem_start);
477 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
479 adr[i].space = rp[i].space;
480 adr[i].address = rp[i].address;
481 adr[i].size = rp[i].size;
487 (*mem_start) += i * sizeof(struct address_range);
493 static int __init interpret_root_props(struct device_node *np,
494 unsigned long *mem_start,
495 int naddrc, int nsizec,
498 struct address_range *adr;
501 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
503 rp = (unsigned int *) get_property(np, "reg", &l);
504 if (rp != 0 && l >= rpsize) {
506 adr = (struct address_range *) (*mem_start);
507 while ((l -= rpsize) >= 0) {
510 adr[i].address = rp[naddrc - 1];
511 adr[i].size = rp[naddrc + nsizec - 1];
514 rp += naddrc + nsizec;
518 (*mem_start) += i * sizeof(struct address_range);
524 static int __devinit finish_node(struct device_node *np,
525 unsigned long *mem_start,
526 interpret_func *ifunc,
527 int naddrc, int nsizec,
530 struct device_node *child;
533 /* get the device addresses and interrupts */
535 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
539 rc = finish_node_interrupts(np, mem_start, measure_only);
543 /* Look for #address-cells and #size-cells properties. */
544 ip = (int *) get_property(np, "#address-cells", NULL);
547 ip = (int *) get_property(np, "#size-cells", NULL);
551 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
552 ifunc = interpret_root_props;
553 else if (np->type == 0)
555 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
556 ifunc = interpret_pci_props;
557 else if (!strcmp(np->type, "dbdma"))
558 ifunc = interpret_dbdma_props;
559 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
560 ifunc = interpret_macio_props;
561 else if (!strcmp(np->type, "isa"))
562 ifunc = interpret_isa_props;
563 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
564 ifunc = interpret_root_props;
565 else if (!((ifunc == interpret_dbdma_props
566 || ifunc == interpret_macio_props)
567 && (!strcmp(np->type, "escc")
568 || !strcmp(np->type, "media-bay"))))
571 for (child = np->child; child != NULL; child = child->sibling) {
572 rc = finish_node(child, mem_start, ifunc,
573 naddrc, nsizec, measure_only);
582 * finish_device_tree is called once things are running normally
583 * (i.e. with text and data mapped to the address they were linked at).
584 * It traverses the device tree and fills in some of the additional,
585 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
586 * mapping is also initialized at this point.
588 void __init finish_device_tree(void)
590 unsigned long start, end, size = 0;
592 DBG(" -> finish_device_tree\n");
594 if (ppc64_interrupt_controller == IC_INVALID) {
595 DBG("failed to configure interrupt controller type\n");
596 panic("failed to configure interrupt controller type\n");
599 /* Initialize virtual IRQ map */
603 * Finish device-tree (pre-parsing some properties etc...)
604 * We do this in 2 passes. One with "measure_only" set, which
605 * will only measure the amount of memory needed, then we can
606 * allocate that memory, and call finish_node again. However,
607 * we must be careful as most routines will fail nowadays when
608 * prom_alloc() returns 0, so we must make sure our first pass
609 * doesn't start at 0. We pre-initialize size to 16 for that
610 * reason and then remove those additional 16 bytes
613 finish_node(allnodes, &size, NULL, 0, 0, 1);
615 end = start = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
616 finish_node(allnodes, &end, NULL, 0, 0, 0);
617 BUG_ON(end != start + size);
619 DBG(" <- finish_device_tree\n");
623 #define printk udbg_printf
626 static inline char *find_flat_dt_string(u32 offset)
628 return ((char *)initial_boot_params) +
629 initial_boot_params->off_dt_strings + offset;
633 * This function is used to scan the flattened device-tree, it is
634 * used to extract the memory informations at boot before we can
637 static int __init scan_flat_dt(int (*it)(unsigned long node,
638 const char *uname, int depth,
642 unsigned long p = ((unsigned long)initial_boot_params) +
643 initial_boot_params->off_dt_struct;
648 u32 tag = *((u32 *)p);
652 if (tag == OF_DT_END_NODE) {
656 if (tag == OF_DT_NOP)
658 if (tag == OF_DT_END)
660 if (tag == OF_DT_PROP) {
661 u32 sz = *((u32 *)p);
663 if (initial_boot_params->version < 0x10)
664 p = _ALIGN(p, sz >= 8 ? 8 : 4);
669 if (tag != OF_DT_BEGIN_NODE) {
670 printk(KERN_WARNING "Invalid tag %x scanning flattened"
671 " device tree !\n", tag);
676 p = _ALIGN(p + strlen(pathp) + 1, 4);
677 if ((*pathp) == '/') {
679 for (lp = NULL, np = pathp; *np; np++)
685 rc = it(p, pathp, depth, data);
694 * This function can be used within scan_flattened_dt callback to get
695 * access to properties
697 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
700 unsigned long p = node;
703 u32 tag = *((u32 *)p);
708 if (tag == OF_DT_NOP)
710 if (tag != OF_DT_PROP)
714 noff = *((u32 *)(p + 4));
716 if (initial_boot_params->version < 0x10)
717 p = _ALIGN(p, sz >= 8 ? 8 : 4);
719 nstr = find_flat_dt_string(noff);
721 printk(KERN_WARNING "Can't find property index"
725 if (strcmp(name, nstr) == 0) {
735 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
740 *mem = _ALIGN(*mem, align);
747 static unsigned long __init unflatten_dt_node(unsigned long mem,
749 struct device_node *dad,
750 struct device_node ***allnextpp,
751 unsigned long fpsize)
753 struct device_node *np;
754 struct property *pp, **prev_pp = NULL;
757 unsigned int l, allocl;
761 tag = *((u32 *)(*p));
762 if (tag != OF_DT_BEGIN_NODE) {
763 printk("Weird tag at start of node: %x\n", tag);
768 l = allocl = strlen(pathp) + 1;
769 *p = _ALIGN(*p + l, 4);
771 /* version 0x10 has a more compact unit name here instead of the full
772 * path. we accumulate the full path size using "fpsize", we'll rebuild
773 * it later. We detect this because the first character of the name is
776 if ((*pathp) != '/') {
779 /* root node: special case. fpsize accounts for path
780 * plus terminating zero. root node only has '/', so
781 * fpsize should be 2, but we want to avoid the first
782 * level nodes to have two '/' so we use fpsize 1 here
787 /* account for '/' and path size minus terminal 0
796 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
797 __alignof__(struct device_node));
799 memset(np, 0, sizeof(*np));
800 np->full_name = ((char*)np) + sizeof(struct device_node);
802 char *p = np->full_name;
803 /* rebuild full path for new format */
804 if (dad && dad->parent) {
805 strcpy(p, dad->full_name);
807 if ((strlen(p) + l + 1) != allocl) {
808 DBG("%s: p: %d, l: %d, a: %d\n",
809 pathp, strlen(p), l, allocl);
817 memcpy(np->full_name, pathp, l);
818 prev_pp = &np->properties;
820 *allnextpp = &np->allnext;
823 /* we temporarily use the next field as `last_child'*/
827 dad->next->sibling = np;
830 kref_init(&np->kref);
836 tag = *((u32 *)(*p));
837 if (tag == OF_DT_NOP) {
841 if (tag != OF_DT_PROP)
845 noff = *((u32 *)((*p) + 4));
847 if (initial_boot_params->version < 0x10)
848 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
850 pname = find_flat_dt_string(noff);
852 printk("Can't find property name in list !\n");
855 if (strcmp(pname, "name") == 0)
857 l = strlen(pname) + 1;
858 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
859 __alignof__(struct property));
861 if (strcmp(pname, "linux,phandle") == 0) {
862 np->node = *((u32 *)*p);
863 if (np->linux_phandle == 0)
864 np->linux_phandle = np->node;
866 if (strcmp(pname, "ibm,phandle") == 0)
867 np->linux_phandle = *((u32 *)*p);
870 pp->value = (void *)*p;
874 *p = _ALIGN((*p) + sz, 4);
876 /* with version 0x10 we may not have the name property, recreate
877 * it here from the unit name if absent
880 char *p = pathp, *ps = pathp, *pa = NULL;
893 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
894 __alignof__(struct property));
898 pp->value = (unsigned char *)(pp + 1);
901 memcpy(pp->value, ps, sz - 1);
902 ((char *)pp->value)[sz - 1] = 0;
903 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
908 np->name = get_property(np, "name", NULL);
909 np->type = get_property(np, "device_type", NULL);
916 while (tag == OF_DT_BEGIN_NODE) {
917 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
918 tag = *((u32 *)(*p));
920 if (tag != OF_DT_END_NODE) {
921 printk("Weird tag at start of node: %x\n", tag);
930 * unflattens the device-tree passed by the firmware, creating the
931 * tree of struct device_node. It also fills the "name" and "type"
932 * pointers of the nodes so the normal device-tree walking functions
933 * can be used (this used to be done by finish_device_tree)
935 void __init unflatten_device_tree(void)
937 unsigned long start, mem, size;
938 struct device_node **allnextp = &allnodes;
942 DBG(" -> unflatten_device_tree()\n");
944 /* First pass, scan for size */
945 start = ((unsigned long)initial_boot_params) +
946 initial_boot_params->off_dt_struct;
947 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
948 size = (size | 3) + 1;
950 DBG(" size is %lx, allocating...\n", size);
952 /* Allocate memory for the expanded device tree */
953 mem = (unsigned long)abs_to_virt(lmb_alloc(size + 4,
954 __alignof__(struct device_node)));
955 ((u32 *)mem)[size / 4] = 0xdeadbeef;
957 DBG(" unflattening...\n", mem);
959 /* Second pass, do actual unflattening */
960 start = ((unsigned long)initial_boot_params) +
961 initial_boot_params->off_dt_struct;
962 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
963 if (*((u32 *)start) != OF_DT_END)
964 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
965 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
966 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
967 ((u32 *)mem)[size / 4] );
970 /* Get pointer to OF "/chosen" node for use everywhere */
971 of_chosen = of_find_node_by_path("/chosen");
973 /* Retreive command line */
974 if (of_chosen != NULL) {
975 p = (char *)get_property(of_chosen, "bootargs", &l);
976 if (p != NULL && l > 0)
977 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
979 #ifdef CONFIG_CMDLINE
980 if (l == 0 || (l == 1 && (*p) == 0))
981 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
982 #endif /* CONFIG_CMDLINE */
984 DBG("Command line is: %s\n", cmd_line);
986 DBG(" <- unflatten_device_tree()\n");
990 static int __init early_init_dt_scan_cpus(unsigned long node,
991 const char *uname, int depth, void *data)
993 char *type = get_flat_dt_prop(node, "device_type", NULL);
997 /* We are scanning "cpu" nodes only */
998 if (type == NULL || strcmp(type, "cpu") != 0)
1001 /* On LPAR, look for the first ibm,pft-size property for the hash table size
1003 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
1005 pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
1006 if (pft_size != NULL) {
1007 /* pft_size[0] is the NUMA CEC cookie */
1008 ppc64_pft_size = pft_size[1];
1012 if (initial_boot_params && initial_boot_params->version >= 2) {
1013 /* version 2 of the kexec param format adds the phys cpuid
1016 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1019 /* Check if it's the boot-cpu, set it's hw index in paca now */
1020 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
1021 u32 *prop = get_flat_dt_prop(node, "reg", NULL);
1022 set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
1023 boot_cpuid_phys = get_hard_smp_processor_id(0);
1027 #ifdef CONFIG_ALTIVEC
1028 /* Check if we have a VMX and eventually update CPU features */
1029 prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
1030 if (prop && (*prop) > 0) {
1031 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1032 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1035 /* Same goes for Apple's "altivec" property */
1036 prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
1038 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1039 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1041 #endif /* CONFIG_ALTIVEC */
1044 * Check for an SMT capable CPU and set the CPU feature. We do
1045 * this by looking at the size of the ibm,ppc-interrupt-server#s
1048 prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1050 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
1051 if (prop && ((size / sizeof(u32)) > 1))
1052 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
1057 static int __init early_init_dt_scan_chosen(unsigned long node,
1058 const char *uname, int depth, void *data)
1062 extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
1064 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1066 if (depth != 1 || strcmp(uname, "chosen") != 0)
1069 /* get platform type */
1070 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
1073 systemcfg->platform = *prop;
1075 /* check if iommu is forced on or off */
1076 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1078 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1081 prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
1083 memory_limit = *prop64;
1085 prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1087 tce_alloc_start = *prop64;
1089 prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1091 tce_alloc_end = *prop64;
1093 #ifdef CONFIG_PPC_RTAS
1094 /* To help early debugging via the front panel, we retreive a minimal
1095 * set of RTAS infos now if available
1098 u64 *basep, *entryp;
1100 basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
1101 entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1102 prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
1103 if (basep && entryp && prop) {
1105 rtas.entry = *entryp;
1109 #endif /* CONFIG_PPC_RTAS */
1115 static int __init early_init_dt_scan_root(unsigned long node,
1116 const char *uname, int depth, void *data)
1123 prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
1124 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1125 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1127 prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
1128 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1129 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1135 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1138 unsigned long r = 0;
1140 /* Ignore more than 2 cells */
1156 static int __init early_init_dt_scan_memory(unsigned long node,
1157 const char *uname, int depth, void *data)
1159 char *type = get_flat_dt_prop(node, "device_type", NULL);
1163 /* We are scanning "memory" nodes only */
1164 if (type == NULL || strcmp(type, "memory") != 0)
1167 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
1171 endp = reg + (l / sizeof(cell_t));
1173 DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
1174 uname, l, reg[0], reg[1], reg[2], reg[3]);
1176 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1177 unsigned long base, size;
1179 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1180 size = dt_mem_next_cell(dt_root_size_cells, ®);
1184 DBG(" - %lx , %lx\n", base, size);
1186 if (base >= 0x80000000ul)
1188 if ((base + size) > 0x80000000ul)
1189 size = 0x80000000ul - base;
1191 lmb_add(base, size);
1196 static void __init early_reserve_mem(void)
1199 u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1200 initial_boot_params->off_mem_rsvmap);
1202 base = *(reserve_map++);
1203 size = *(reserve_map++);
1206 DBG("reserving: %lx -> %lx\n", base, size);
1207 lmb_reserve(base, size);
1211 DBG("memory reserved, lmbs :\n");
1216 void __init early_init_devtree(void *params)
1218 DBG(" -> early_init_devtree()\n");
1220 /* Setup flat device-tree pointer */
1221 initial_boot_params = params;
1223 /* By default, hash size is not set */
1226 /* Retreive various informations from the /chosen node of the
1227 * device-tree, including the platform type, initrd location and
1228 * size, TCE reserve, and more ...
1230 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1232 /* Scan memory nodes and rebuild LMBs */
1234 scan_flat_dt(early_init_dt_scan_root, NULL);
1235 scan_flat_dt(early_init_dt_scan_memory, NULL);
1236 lmb_enforce_memory_limit();
1238 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1239 lmb_reserve(0, __pa(klimit));
1241 DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
1243 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1244 early_reserve_mem();
1246 DBG("Scanning CPUs ...\n");
1248 /* Retreive hash table size from flattened tree plus other
1249 * CPU related informations (altivec support, boot CPU ID, ...)
1251 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1253 /* If hash size wasn't obtained above, we calculate it now based on
1254 * the total RAM size
1256 if (ppc64_pft_size == 0) {
1257 unsigned long rnd_mem_size, pteg_count;
1259 /* round mem_size up to next power of 2 */
1260 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
1261 if (rnd_mem_size < systemcfg->physicalMemorySize)
1265 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
1267 ppc64_pft_size = __ilog2(pteg_count << 7);
1270 DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
1271 DBG(" <- early_init_devtree()\n");
1277 prom_n_addr_cells(struct device_node* np)
1283 ip = (int *) get_property(np, "#address-cells", NULL);
1286 } while (np->parent);
1287 /* No #address-cells property for the root node, default to 1 */
1292 prom_n_size_cells(struct device_node* np)
1298 ip = (int *) get_property(np, "#size-cells", NULL);
1301 } while (np->parent);
1302 /* No #size-cells property for the root node, default to 1 */
1307 * Work out the sense (active-low level / active-high edge)
1308 * of each interrupt from the device tree.
1310 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1312 struct device_node *np;
1315 /* default to level-triggered */
1316 memset(senses, 1, max - off);
1318 for (np = allnodes; np != 0; np = np->allnext) {
1319 for (j = 0; j < np->n_intrs; j++) {
1320 i = np->intrs[j].line;
1321 if (i >= off && i < max)
1322 senses[i-off] = np->intrs[j].sense ?
1323 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
1324 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
1330 * Construct and return a list of the device_nodes with a given name.
1332 struct device_node *
1333 find_devices(const char *name)
1335 struct device_node *head, **prevp, *np;
1338 for (np = allnodes; np != 0; np = np->allnext) {
1339 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1347 EXPORT_SYMBOL(find_devices);
1350 * Construct and return a list of the device_nodes with a given type.
1352 struct device_node *
1353 find_type_devices(const char *type)
1355 struct device_node *head, **prevp, *np;
1358 for (np = allnodes; np != 0; np = np->allnext) {
1359 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1367 EXPORT_SYMBOL(find_type_devices);
1370 * Returns all nodes linked together
1372 struct device_node *
1373 find_all_nodes(void)
1375 struct device_node *head, **prevp, *np;
1378 for (np = allnodes; np != 0; np = np->allnext) {
1385 EXPORT_SYMBOL(find_all_nodes);
1387 /** Checks if the given "compat" string matches one of the strings in
1388 * the device's "compatible" property
1391 device_is_compatible(struct device_node *device, const char *compat)
1396 cp = (char *) get_property(device, "compatible", &cplen);
1400 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1409 EXPORT_SYMBOL(device_is_compatible);
1413 * Indicates whether the root node has a given value in its
1414 * compatible property.
1417 machine_is_compatible(const char *compat)
1419 struct device_node *root;
1422 root = of_find_node_by_path("/");
1424 rc = device_is_compatible(root, compat);
1429 EXPORT_SYMBOL(machine_is_compatible);
1432 * Construct and return a list of the device_nodes with a given type
1433 * and compatible property.
1435 struct device_node *
1436 find_compatible_devices(const char *type, const char *compat)
1438 struct device_node *head, **prevp, *np;
1441 for (np = allnodes; np != 0; np = np->allnext) {
1443 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1445 if (device_is_compatible(np, compat)) {
1453 EXPORT_SYMBOL(find_compatible_devices);
1456 * Find the device_node with a given full_name.
1458 struct device_node *
1459 find_path_device(const char *path)
1461 struct device_node *np;
1463 for (np = allnodes; np != 0; np = np->allnext)
1464 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1468 EXPORT_SYMBOL(find_path_device);
1472 * New implementation of the OF "find" APIs, return a refcounted
1473 * object, call of_node_put() when done. The device tree and list
1474 * are protected by a rw_lock.
1476 * Note that property management will need some locking as well,
1477 * this isn't dealt with yet.
1482 * of_find_node_by_name - Find a node by its "name" property
1483 * @from: The node to start searching from or NULL, the node
1484 * you pass will not be searched, only the next one
1485 * will; typically, you pass what the previous call
1486 * returned. of_node_put() will be called on it
1487 * @name: The name string to match against
1489 * Returns a node pointer with refcount incremented, use
1490 * of_node_put() on it when done.
1492 struct device_node *of_find_node_by_name(struct device_node *from,
1495 struct device_node *np;
1497 read_lock(&devtree_lock);
1498 np = from ? from->allnext : allnodes;
1499 for (; np != 0; np = np->allnext)
1500 if (np->name != 0 && strcasecmp(np->name, name) == 0
1505 read_unlock(&devtree_lock);
1508 EXPORT_SYMBOL(of_find_node_by_name);
1511 * of_find_node_by_type - Find a node by its "device_type" property
1512 * @from: The node to start searching from or NULL, the node
1513 * you pass will not be searched, only the next one
1514 * will; typically, you pass what the previous call
1515 * returned. of_node_put() will be called on it
1516 * @name: The type string to match against
1518 * Returns a node pointer with refcount incremented, use
1519 * of_node_put() on it when done.
1521 struct device_node *of_find_node_by_type(struct device_node *from,
1524 struct device_node *np;
1526 read_lock(&devtree_lock);
1527 np = from ? from->allnext : allnodes;
1528 for (; np != 0; np = np->allnext)
1529 if (np->type != 0 && strcasecmp(np->type, type) == 0
1534 read_unlock(&devtree_lock);
1537 EXPORT_SYMBOL(of_find_node_by_type);
1540 * of_find_compatible_node - Find a node based on type and one of the
1541 * tokens in its "compatible" property
1542 * @from: The node to start searching from or NULL, the node
1543 * you pass will not be searched, only the next one
1544 * will; typically, you pass what the previous call
1545 * returned. of_node_put() will be called on it
1546 * @type: The type string to match "device_type" or NULL to ignore
1547 * @compatible: The string to match to one of the tokens in the device
1548 * "compatible" list.
1550 * Returns a node pointer with refcount incremented, use
1551 * of_node_put() on it when done.
1553 struct device_node *of_find_compatible_node(struct device_node *from,
1554 const char *type, const char *compatible)
1556 struct device_node *np;
1558 read_lock(&devtree_lock);
1559 np = from ? from->allnext : allnodes;
1560 for (; np != 0; np = np->allnext) {
1562 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1564 if (device_is_compatible(np, compatible) && of_node_get(np))
1569 read_unlock(&devtree_lock);
1572 EXPORT_SYMBOL(of_find_compatible_node);
1575 * of_find_node_by_path - Find a node matching a full OF path
1576 * @path: The full path to match
1578 * Returns a node pointer with refcount incremented, use
1579 * of_node_put() on it when done.
1581 struct device_node *of_find_node_by_path(const char *path)
1583 struct device_node *np = allnodes;
1585 read_lock(&devtree_lock);
1586 for (; np != 0; np = np->allnext) {
1587 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1591 read_unlock(&devtree_lock);
1594 EXPORT_SYMBOL(of_find_node_by_path);
1597 * of_find_node_by_phandle - Find a node given a phandle
1598 * @handle: phandle of the node to find
1600 * Returns a node pointer with refcount incremented, use
1601 * of_node_put() on it when done.
1603 struct device_node *of_find_node_by_phandle(phandle handle)
1605 struct device_node *np;
1607 read_lock(&devtree_lock);
1608 for (np = allnodes; np != 0; np = np->allnext)
1609 if (np->linux_phandle == handle)
1613 read_unlock(&devtree_lock);
1616 EXPORT_SYMBOL(of_find_node_by_phandle);
1619 * of_find_all_nodes - Get next node in global list
1620 * @prev: Previous node or NULL to start iteration
1621 * of_node_put() will be called on it
1623 * Returns a node pointer with refcount incremented, use
1624 * of_node_put() on it when done.
1626 struct device_node *of_find_all_nodes(struct device_node *prev)
1628 struct device_node *np;
1630 read_lock(&devtree_lock);
1631 np = prev ? prev->allnext : allnodes;
1632 for (; np != 0; np = np->allnext)
1633 if (of_node_get(np))
1637 read_unlock(&devtree_lock);
1640 EXPORT_SYMBOL(of_find_all_nodes);
1643 * of_get_parent - Get a node's parent if any
1644 * @node: Node to get parent
1646 * Returns a node pointer with refcount incremented, use
1647 * of_node_put() on it when done.
1649 struct device_node *of_get_parent(const struct device_node *node)
1651 struct device_node *np;
1656 read_lock(&devtree_lock);
1657 np = of_node_get(node->parent);
1658 read_unlock(&devtree_lock);
1661 EXPORT_SYMBOL(of_get_parent);
1664 * of_get_next_child - Iterate a node childs
1665 * @node: parent node
1666 * @prev: previous child of the parent node, or NULL to get first
1668 * Returns a node pointer with refcount incremented, use
1669 * of_node_put() on it when done.
1671 struct device_node *of_get_next_child(const struct device_node *node,
1672 struct device_node *prev)
1674 struct device_node *next;
1676 read_lock(&devtree_lock);
1677 next = prev ? prev->sibling : node->child;
1678 for (; next != 0; next = next->sibling)
1679 if (of_node_get(next))
1683 read_unlock(&devtree_lock);
1686 EXPORT_SYMBOL(of_get_next_child);
1689 * of_node_get - Increment refcount of a node
1690 * @node: Node to inc refcount, NULL is supported to
1691 * simplify writing of callers
1695 struct device_node *of_node_get(struct device_node *node)
1698 kref_get(&node->kref);
1701 EXPORT_SYMBOL(of_node_get);
1703 static inline struct device_node * kref_to_device_node(struct kref *kref)
1705 return container_of(kref, struct device_node, kref);
1709 * of_node_release - release a dynamically allocated node
1710 * @kref: kref element of the node to be released
1712 * In of_node_put() this function is passed to kref_put()
1713 * as the destructor.
1715 static void of_node_release(struct kref *kref)
1717 struct device_node *node = kref_to_device_node(kref);
1718 struct property *prop = node->properties;
1720 if (!OF_IS_DYNAMIC(node))
1723 struct property *next = prop->next;
1731 kfree(node->full_name);
1736 * of_node_put - Decrement refcount of a node
1737 * @node: Node to dec refcount, NULL is supported to
1738 * simplify writing of callers
1741 void of_node_put(struct device_node *node)
1744 kref_put(&node->kref, of_node_release);
1746 EXPORT_SYMBOL(of_node_put);
1749 * Fix up the uninitialized fields in a new device node:
1750 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1752 * A lot of boot-time code is duplicated here, because functions such
1753 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1756 * This should probably be split up into smaller chunks.
1759 static int of_finish_dynamic_node(struct device_node *node,
1760 unsigned long *unused1, int unused2,
1761 int unused3, int unused4)
1763 struct device_node *parent = of_get_parent(node);
1765 phandle *ibm_phandle;
1767 node->name = get_property(node, "name", NULL);
1768 node->type = get_property(node, "device_type", NULL);
1775 /* We don't support that function on PowerMac, at least
1778 if (systemcfg->platform == PLATFORM_POWERMAC)
1781 /* fix up new node's linux_phandle field */
1782 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1783 node->linux_phandle = *ibm_phandle;
1786 of_node_put(parent);
1791 * Plug a device node into the tree and global list.
1793 void of_attach_node(struct device_node *np)
1795 write_lock(&devtree_lock);
1796 np->sibling = np->parent->child;
1797 np->allnext = allnodes;
1798 np->parent->child = np;
1800 write_unlock(&devtree_lock);
1804 * "Unplug" a node from the device tree. The caller must hold
1805 * a reference to the node. The memory associated with the node
1806 * is not freed until its refcount goes to zero.
1808 void of_detach_node(const struct device_node *np)
1810 struct device_node *parent;
1812 write_lock(&devtree_lock);
1814 parent = np->parent;
1817 allnodes = np->allnext;
1819 struct device_node *prev;
1820 for (prev = allnodes;
1821 prev->allnext != np;
1822 prev = prev->allnext)
1824 prev->allnext = np->allnext;
1827 if (parent->child == np)
1828 parent->child = np->sibling;
1830 struct device_node *prevsib;
1831 for (prevsib = np->parent->child;
1832 prevsib->sibling != np;
1833 prevsib = prevsib->sibling)
1835 prevsib->sibling = np->sibling;
1838 write_unlock(&devtree_lock);
1841 static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1846 case PSERIES_RECONFIG_ADD:
1847 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1849 printk(KERN_ERR "finish_node returned %d\n", err);
1860 static struct notifier_block prom_reconfig_nb = {
1861 .notifier_call = prom_reconfig_notifier,
1862 .priority = 10, /* This one needs to run first */
1865 static int __init prom_reconfig_setup(void)
1867 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1869 __initcall(prom_reconfig_setup);
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) {
1888 EXPORT_SYMBOL(get_property);
1891 * Add a property to a node
1894 prom_add_property(struct device_node* np, struct property* prop)
1896 struct property **next = &np->properties;
1900 next = &(*next)->next;
1906 print_properties(struct device_node *np)
1908 struct property *pp;
1912 for (pp = np->properties; pp != 0; pp = pp->next) {
1913 printk(KERN_INFO "%s", pp->name);
1914 for (i = strlen(pp->name); i < 16; ++i)
1916 cp = (char *) pp->value;
1917 for (i = pp->length; i > 0; --i, ++cp)
1918 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1919 || (i == 1 && *cp != 0))
1921 if (i == 0 && pp->length > 1) {
1922 /* looks like a string */
1923 printk(" %s\n", (char *) pp->value);
1925 /* dump it in hex */
1929 if (pp->length % 4 == 0) {
1930 unsigned int *p = (unsigned int *) pp->value;
1933 for (i = 0; i < n; ++i) {
1934 if (i != 0 && (i % 4) == 0)
1936 printk(" %08x", *p++);
1939 unsigned char *bp = pp->value;
1941 for (i = 0; i < n; ++i) {
1942 if (i != 0 && (i % 16) == 0)
1944 printk(" %02x", *bp++);
1948 if (pp->length > 64)
1949 printk(" ... (length = %d)\n",