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/threads.h>
26 #include <linux/spinlock.h>
27 #include <linux/types.h>
28 #include <linux/pci.h>
29 #include <linux/stringify.h>
30 #include <linux/delay.h>
31 #include <linux/initrd.h>
32 #include <linux/bitops.h>
33 #include <linux/module.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/ppcdebug.h>
50 #include <asm/btext.h>
51 #include <asm/sections.h>
52 #include <asm/machdep.h>
53 #include <asm/pSeries_reconfig.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 int interpret_func(struct device_node *, unsigned long *,
77 extern struct rtas_t rtas;
78 extern struct lmb lmb;
79 extern unsigned long klimit;
80 extern unsigned long memory_limit;
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;
86 unsigned long tce_alloc_start, tce_alloc_end;
91 static struct boot_param_header *initial_boot_params __initdata;
93 struct boot_param_header *initial_boot_params;
96 static struct device_node *allnodes = NULL;
98 /* use when traversing tree through the allnext, child, sibling,
99 * or parent members of struct device_node.
101 static DEFINE_RWLOCK(devtree_lock);
103 /* export that to outside world */
104 struct device_node *of_chosen;
107 * Wrapper for allocating memory for various data that needs to be
108 * attached to device nodes as they are processed at boot or when
109 * added to the device tree later (e.g. DLPAR). At boot there is
110 * already a region reserved so we just increment *mem_start by size;
111 * otherwise we call kmalloc.
113 static void * prom_alloc(unsigned long size, unsigned long *mem_start)
118 return kmalloc(size, GFP_KERNEL);
126 * Find the device_node with a given phandle.
128 static struct device_node * find_phandle(phandle ph)
130 struct device_node *np;
132 for (np = allnodes; np != 0; np = np->allnext)
133 if (np->linux_phandle == ph)
139 * Find the interrupt parent of a node.
141 static struct device_node * __devinit intr_parent(struct device_node *p)
145 parp = (phandle *) get_property(p, "interrupt-parent", NULL);
148 return find_phandle(*parp);
152 * Find out the size of each entry of the interrupts property
155 int __devinit prom_n_intr_cells(struct device_node *np)
157 struct device_node *p;
160 for (p = np; (p = intr_parent(p)) != NULL; ) {
161 icp = (unsigned int *)
162 get_property(p, "#interrupt-cells", NULL);
165 if (get_property(p, "interrupt-controller", NULL) != NULL
166 || get_property(p, "interrupt-map", NULL) != NULL) {
167 printk("oops, node %s doesn't have #interrupt-cells\n",
173 printk("prom_n_intr_cells failed for %s\n", np->full_name);
179 * Map an interrupt from a device up to the platform interrupt
182 static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
183 struct device_node *np, unsigned int *ints,
186 struct device_node *p, *ipar;
187 unsigned int *imap, *imask, *ip;
188 int i, imaplen, match;
189 int newintrc = 0, newaddrc = 0;
193 reg = (unsigned int *) get_property(np, "reg", NULL);
194 naddrc = prom_n_addr_cells(np);
197 if (get_property(p, "interrupt-controller", NULL) != NULL)
198 /* this node is an interrupt controller, stop here */
200 imap = (unsigned int *)
201 get_property(p, "interrupt-map", &imaplen);
206 imask = (unsigned int *)
207 get_property(p, "interrupt-map-mask", NULL);
209 printk("oops, %s has interrupt-map but no mask\n",
213 imaplen /= sizeof(unsigned int);
216 while (imaplen > 0 && !match) {
217 /* check the child-interrupt field */
219 for (i = 0; i < naddrc && match; ++i)
220 match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
221 for (; i < naddrc + nintrc && match; ++i)
222 match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
223 imap += naddrc + nintrc;
224 imaplen -= naddrc + nintrc;
225 /* grab the interrupt parent */
226 ipar = find_phandle((phandle) *imap++);
229 printk("oops, no int parent %x in map of %s\n",
230 imap[-1], p->full_name);
233 /* find the parent's # addr and intr cells */
234 ip = (unsigned int *)
235 get_property(ipar, "#interrupt-cells", NULL);
237 printk("oops, no #interrupt-cells on %s\n",
242 ip = (unsigned int *)
243 get_property(ipar, "#address-cells", NULL);
244 newaddrc = (ip == NULL)? 0: *ip;
245 imap += newaddrc + newintrc;
246 imaplen -= newaddrc + newintrc;
249 printk("oops, error decoding int-map on %s, len=%d\n",
250 p->full_name, imaplen);
255 printk("oops, no match in %s int-map for %s\n",
256 p->full_name, np->full_name);
263 ints = imap - nintrc;
268 printk("hmmm, int tree for %s doesn't have ctrler\n",
278 static int __devinit finish_node_interrupts(struct device_node *np,
279 unsigned long *mem_start,
283 int intlen, intrcells, intrcount;
285 unsigned int *irq, virq;
286 struct device_node *ic;
288 ints = (unsigned int *) get_property(np, "interrupts", &intlen);
291 intrcells = prom_n_intr_cells(np);
292 intlen /= intrcells * sizeof(unsigned int);
294 np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
302 for (i = 0; i < intlen; ++i, ints += intrcells) {
303 n = map_interrupt(&irq, &ic, np, ints, intrcells);
307 /* don't map IRQ numbers under a cascaded 8259 controller */
308 if (ic && device_is_compatible(ic, "chrp,iic")) {
309 np->intrs[intrcount].line = irq[0];
311 virq = virt_irq_create_mapping(irq[0]);
312 if (virq == NO_IRQ) {
313 printk(KERN_CRIT "Could not allocate interrupt"
314 " number for %s\n", np->full_name);
317 np->intrs[intrcount].line = irq_offset_up(virq);
320 /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
321 if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
322 char *name = get_property(ic->parent, "name", NULL);
323 if (name && !strcmp(name, "u3"))
324 np->intrs[intrcount].line += 128;
325 else if (!(name && !strcmp(name, "mac-io")))
326 /* ignore other cascaded controllers, such as
330 np->intrs[intrcount].sense = 1;
332 np->intrs[intrcount].sense = irq[1];
334 printk("hmmm, got %d intr cells for %s:", n,
336 for (j = 0; j < n; ++j)
337 printk(" %d", irq[j]);
342 np->n_intrs = intrcount;
347 static int __devinit interpret_pci_props(struct device_node *np,
348 unsigned long *mem_start,
349 int naddrc, int nsizec,
352 struct address_range *adr;
353 struct pci_reg_property *pci_addrs;
356 pci_addrs = (struct pci_reg_property *)
357 get_property(np, "assigned-addresses", &l);
361 n_addrs = l / sizeof(*pci_addrs);
363 adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
371 np->n_addrs = n_addrs;
373 for (i = 0; i < n_addrs; i++) {
374 adr[i].space = pci_addrs[i].addr.a_hi;
375 adr[i].address = pci_addrs[i].addr.a_lo |
376 ((u64)pci_addrs[i].addr.a_mid << 32);
377 adr[i].size = pci_addrs[i].size_lo;
383 static int __init interpret_dbdma_props(struct device_node *np,
384 unsigned long *mem_start,
385 int naddrc, int nsizec,
388 struct reg_property32 *rp;
389 struct address_range *adr;
390 unsigned long base_address;
392 struct device_node *db;
396 for (db = np->parent; db != NULL; db = db->parent) {
397 if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
398 base_address = db->addrs[0].address;
404 rp = (struct reg_property32 *) get_property(np, "reg", &l);
405 if (rp != 0 && l >= sizeof(struct reg_property32)) {
407 adr = (struct address_range *) (*mem_start);
408 while ((l -= sizeof(struct reg_property32)) >= 0) {
411 adr[i].address = rp[i].address + base_address;
412 adr[i].size = rp[i].size;
418 (*mem_start) += i * sizeof(struct address_range);
424 static int __init interpret_macio_props(struct device_node *np,
425 unsigned long *mem_start,
426 int naddrc, int nsizec,
429 struct reg_property32 *rp;
430 struct address_range *adr;
431 unsigned long base_address;
433 struct device_node *db;
437 for (db = np->parent; db != NULL; db = db->parent) {
438 if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
439 base_address = db->addrs[0].address;
445 rp = (struct reg_property32 *) get_property(np, "reg", &l);
446 if (rp != 0 && l >= sizeof(struct reg_property32)) {
448 adr = (struct address_range *) (*mem_start);
449 while ((l -= sizeof(struct reg_property32)) >= 0) {
452 adr[i].address = rp[i].address + base_address;
453 adr[i].size = rp[i].size;
459 (*mem_start) += i * sizeof(struct address_range);
465 static int __init interpret_isa_props(struct device_node *np,
466 unsigned long *mem_start,
467 int naddrc, int nsizec,
470 struct isa_reg_property *rp;
471 struct address_range *adr;
474 rp = (struct isa_reg_property *) get_property(np, "reg", &l);
475 if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
477 adr = (struct address_range *) (*mem_start);
478 while ((l -= sizeof(struct isa_reg_property)) >= 0) {
480 adr[i].space = rp[i].space;
481 adr[i].address = rp[i].address;
482 adr[i].size = rp[i].size;
488 (*mem_start) += i * sizeof(struct address_range);
494 static int __init interpret_root_props(struct device_node *np,
495 unsigned long *mem_start,
496 int naddrc, int nsizec,
499 struct address_range *adr;
502 int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
504 rp = (unsigned int *) get_property(np, "reg", &l);
505 if (rp != 0 && l >= rpsize) {
507 adr = (struct address_range *) (*mem_start);
508 while ((l -= rpsize) >= 0) {
511 adr[i].address = rp[naddrc - 1];
512 adr[i].size = rp[naddrc + nsizec - 1];
515 rp += naddrc + nsizec;
519 (*mem_start) += i * sizeof(struct address_range);
525 static int __devinit finish_node(struct device_node *np,
526 unsigned long *mem_start,
527 interpret_func *ifunc,
528 int naddrc, int nsizec,
531 struct device_node *child;
534 /* get the device addresses and interrupts */
536 rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
540 rc = finish_node_interrupts(np, mem_start, measure_only);
544 /* Look for #address-cells and #size-cells properties. */
545 ip = (int *) get_property(np, "#address-cells", NULL);
548 ip = (int *) get_property(np, "#size-cells", NULL);
552 if (!strcmp(np->name, "device-tree") || np->parent == NULL)
553 ifunc = interpret_root_props;
554 else if (np->type == 0)
556 else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
557 ifunc = interpret_pci_props;
558 else if (!strcmp(np->type, "dbdma"))
559 ifunc = interpret_dbdma_props;
560 else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
561 ifunc = interpret_macio_props;
562 else if (!strcmp(np->type, "isa"))
563 ifunc = interpret_isa_props;
564 else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
565 ifunc = interpret_root_props;
566 else if (!((ifunc == interpret_dbdma_props
567 || ifunc == interpret_macio_props)
568 && (!strcmp(np->type, "escc")
569 || !strcmp(np->type, "media-bay"))))
572 for (child = np->child; child != NULL; child = child->sibling) {
573 rc = finish_node(child, mem_start, ifunc,
574 naddrc, nsizec, measure_only);
583 * finish_device_tree is called once things are running normally
584 * (i.e. with text and data mapped to the address they were linked at).
585 * It traverses the device tree and fills in some of the additional,
586 * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
587 * mapping is also initialized at this point.
589 void __init finish_device_tree(void)
591 unsigned long start, end, size = 0;
593 DBG(" -> finish_device_tree\n");
595 if (ppc64_interrupt_controller == IC_INVALID) {
596 DBG("failed to configure interrupt controller type\n");
597 panic("failed to configure interrupt controller type\n");
600 /* Initialize virtual IRQ map */
604 * Finish device-tree (pre-parsing some properties etc...)
605 * We do this in 2 passes. One with "measure_only" set, which
606 * will only measure the amount of memory needed, then we can
607 * allocate that memory, and call finish_node again. However,
608 * we must be careful as most routines will fail nowadays when
609 * prom_alloc() returns 0, so we must make sure our first pass
610 * doesn't start at 0. We pre-initialize size to 16 for that
611 * reason and then remove those additional 16 bytes
614 finish_node(allnodes, &size, NULL, 0, 0, 1);
616 end = start = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
617 finish_node(allnodes, &end, NULL, 0, 0, 0);
618 BUG_ON(end != start + size);
620 DBG(" <- finish_device_tree\n");
624 #define printk udbg_printf
627 static inline char *find_flat_dt_string(u32 offset)
629 return ((char *)initial_boot_params) +
630 initial_boot_params->off_dt_strings + offset;
634 * This function is used to scan the flattened device-tree, it is
635 * used to extract the memory informations at boot before we can
638 static int __init scan_flat_dt(int (*it)(unsigned long node,
639 const char *uname, int depth,
643 unsigned long p = ((unsigned long)initial_boot_params) +
644 initial_boot_params->off_dt_struct;
649 u32 tag = *((u32 *)p);
653 if (tag == OF_DT_END_NODE) {
657 if (tag == OF_DT_NOP)
659 if (tag == OF_DT_END)
661 if (tag == OF_DT_PROP) {
662 u32 sz = *((u32 *)p);
664 if (initial_boot_params->version < 0x10)
665 p = _ALIGN(p, sz >= 8 ? 8 : 4);
670 if (tag != OF_DT_BEGIN_NODE) {
671 printk(KERN_WARNING "Invalid tag %x scanning flattened"
672 " device tree !\n", tag);
677 p = _ALIGN(p + strlen(pathp) + 1, 4);
678 if ((*pathp) == '/') {
680 for (lp = NULL, np = pathp; *np; np++)
686 rc = it(p, pathp, depth, data);
695 * This function can be used within scan_flattened_dt callback to get
696 * access to properties
698 static void* __init get_flat_dt_prop(unsigned long node, const char *name,
701 unsigned long p = node;
704 u32 tag = *((u32 *)p);
709 if (tag == OF_DT_NOP)
711 if (tag != OF_DT_PROP)
715 noff = *((u32 *)(p + 4));
717 if (initial_boot_params->version < 0x10)
718 p = _ALIGN(p, sz >= 8 ? 8 : 4);
720 nstr = find_flat_dt_string(noff);
722 printk(KERN_WARNING "Can't find property index"
726 if (strcmp(name, nstr) == 0) {
736 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
741 *mem = _ALIGN(*mem, align);
748 static unsigned long __init unflatten_dt_node(unsigned long mem,
750 struct device_node *dad,
751 struct device_node ***allnextpp,
752 unsigned long fpsize)
754 struct device_node *np;
755 struct property *pp, **prev_pp = NULL;
758 unsigned int l, allocl;
762 tag = *((u32 *)(*p));
763 if (tag != OF_DT_BEGIN_NODE) {
764 printk("Weird tag at start of node: %x\n", tag);
769 l = allocl = strlen(pathp) + 1;
770 *p = _ALIGN(*p + l, 4);
772 /* version 0x10 has a more compact unit name here instead of the full
773 * path. we accumulate the full path size using "fpsize", we'll rebuild
774 * it later. We detect this because the first character of the name is
777 if ((*pathp) != '/') {
780 /* root node: special case. fpsize accounts for path
781 * plus terminating zero. root node only has '/', so
782 * fpsize should be 2, but we want to avoid the first
783 * level nodes to have two '/' so we use fpsize 1 here
788 /* account for '/' and path size minus terminal 0
797 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
798 __alignof__(struct device_node));
800 memset(np, 0, sizeof(*np));
801 np->full_name = ((char*)np) + sizeof(struct device_node);
803 char *p = np->full_name;
804 /* rebuild full path for new format */
805 if (dad && dad->parent) {
806 strcpy(p, dad->full_name);
808 if ((strlen(p) + l + 1) != allocl) {
809 DBG("%s: p: %d, l: %d, a: %d\n",
810 pathp, strlen(p), l, allocl);
818 memcpy(np->full_name, pathp, l);
819 prev_pp = &np->properties;
821 *allnextpp = &np->allnext;
824 /* we temporarily use the next field as `last_child'*/
828 dad->next->sibling = np;
831 kref_init(&np->kref);
837 tag = *((u32 *)(*p));
838 if (tag == OF_DT_NOP) {
842 if (tag != OF_DT_PROP)
846 noff = *((u32 *)((*p) + 4));
848 if (initial_boot_params->version < 0x10)
849 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
851 pname = find_flat_dt_string(noff);
853 printk("Can't find property name in list !\n");
856 if (strcmp(pname, "name") == 0)
858 l = strlen(pname) + 1;
859 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
860 __alignof__(struct property));
862 if (strcmp(pname, "linux,phandle") == 0) {
863 np->node = *((u32 *)*p);
864 if (np->linux_phandle == 0)
865 np->linux_phandle = np->node;
867 if (strcmp(pname, "ibm,phandle") == 0)
868 np->linux_phandle = *((u32 *)*p);
871 pp->value = (void *)*p;
875 *p = _ALIGN((*p) + sz, 4);
877 /* with version 0x10 we may not have the name property, recreate
878 * it here from the unit name if absent
881 char *p = pathp, *ps = pathp, *pa = NULL;
894 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
895 __alignof__(struct property));
899 pp->value = (unsigned char *)(pp + 1);
902 memcpy(pp->value, ps, sz - 1);
903 ((char *)pp->value)[sz - 1] = 0;
904 DBG("fixed up name for %s -> %s\n", pathp, pp->value);
909 np->name = get_property(np, "name", NULL);
910 np->type = get_property(np, "device_type", NULL);
917 while (tag == OF_DT_BEGIN_NODE) {
918 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
919 tag = *((u32 *)(*p));
921 if (tag != OF_DT_END_NODE) {
922 printk("Weird tag at end of node: %x\n", tag);
931 * unflattens the device-tree passed by the firmware, creating the
932 * tree of struct device_node. It also fills the "name" and "type"
933 * pointers of the nodes so the normal device-tree walking functions
934 * can be used (this used to be done by finish_device_tree)
936 void __init unflatten_device_tree(void)
938 unsigned long start, mem, size;
939 struct device_node **allnextp = &allnodes;
943 DBG(" -> unflatten_device_tree()\n");
945 /* First pass, scan for size */
946 start = ((unsigned long)initial_boot_params) +
947 initial_boot_params->off_dt_struct;
948 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
949 size = (size | 3) + 1;
951 DBG(" size is %lx, allocating...\n", size);
953 /* Allocate memory for the expanded device tree */
954 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
956 DBG("Couldn't allocate memory with lmb_alloc()!\n");
957 panic("Couldn't allocate memory with lmb_alloc()!\n");
959 mem = (unsigned long)abs_to_virt(mem);
961 ((u32 *)mem)[size / 4] = 0xdeadbeef;
963 DBG(" unflattening...\n", mem);
965 /* Second pass, do actual unflattening */
966 start = ((unsigned long)initial_boot_params) +
967 initial_boot_params->off_dt_struct;
968 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
969 if (*((u32 *)start) != OF_DT_END)
970 printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
971 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
972 printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
973 ((u32 *)mem)[size / 4] );
976 /* Get pointer to OF "/chosen" node for use everywhere */
977 of_chosen = of_find_node_by_path("/chosen");
979 /* Retreive command line */
980 if (of_chosen != NULL) {
981 p = (char *)get_property(of_chosen, "bootargs", &l);
982 if (p != NULL && l > 0)
983 strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
985 #ifdef CONFIG_CMDLINE
986 if (l == 0 || (l == 1 && (*p) == 0))
987 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
988 #endif /* CONFIG_CMDLINE */
990 DBG("Command line is: %s\n", cmd_line);
992 DBG(" <- unflatten_device_tree()\n");
996 static int __init early_init_dt_scan_cpus(unsigned long node,
997 const char *uname, int depth, void *data)
999 char *type = get_flat_dt_prop(node, "device_type", NULL);
1003 /* We are scanning "cpu" nodes only */
1004 if (type == NULL || strcmp(type, "cpu") != 0)
1007 /* On LPAR, look for the first ibm,pft-size property for the hash table size
1009 if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
1011 pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
1012 if (pft_size != NULL) {
1013 /* pft_size[0] is the NUMA CEC cookie */
1014 ppc64_pft_size = pft_size[1];
1018 if (initial_boot_params && initial_boot_params->version >= 2) {
1019 /* version 2 of the kexec param format adds the phys cpuid
1022 boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
1025 /* Check if it's the boot-cpu, set it's hw index in paca now */
1026 if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
1027 u32 *prop = get_flat_dt_prop(node, "reg", NULL);
1028 set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
1029 boot_cpuid_phys = get_hard_smp_processor_id(0);
1033 #ifdef CONFIG_ALTIVEC
1034 /* Check if we have a VMX and eventually update CPU features */
1035 prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
1036 if (prop && (*prop) > 0) {
1037 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1038 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1041 /* Same goes for Apple's "altivec" property */
1042 prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
1044 cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
1045 cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
1047 #endif /* CONFIG_ALTIVEC */
1050 * Check for an SMT capable CPU and set the CPU feature. We do
1051 * this by looking at the size of the ibm,ppc-interrupt-server#s
1054 prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
1056 cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
1057 if (prop && ((size / sizeof(u32)) > 1))
1058 cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
1063 static int __init early_init_dt_scan_chosen(unsigned long node,
1064 const char *uname, int depth, void *data)
1069 DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
1071 if (depth != 1 || strcmp(uname, "chosen") != 0)
1074 /* get platform type */
1075 prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
1078 systemcfg->platform = *prop;
1080 /* check if iommu is forced on or off */
1081 if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
1083 if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
1086 prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
1088 memory_limit = *prop64;
1090 prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
1092 tce_alloc_start = *prop64;
1094 prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
1096 tce_alloc_end = *prop64;
1098 #ifdef CONFIG_PPC_RTAS
1099 /* To help early debugging via the front panel, we retreive a minimal
1100 * set of RTAS infos now if available
1103 u64 *basep, *entryp;
1105 basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
1106 entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
1107 prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
1108 if (basep && entryp && prop) {
1110 rtas.entry = *entryp;
1114 #endif /* CONFIG_PPC_RTAS */
1120 static int __init early_init_dt_scan_root(unsigned long node,
1121 const char *uname, int depth, void *data)
1128 prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
1129 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
1130 DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
1132 prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
1133 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
1134 DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
1140 static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
1143 unsigned long r = 0;
1145 /* Ignore more than 2 cells */
1161 static int __init early_init_dt_scan_memory(unsigned long node,
1162 const char *uname, int depth, void *data)
1164 char *type = get_flat_dt_prop(node, "device_type", NULL);
1168 /* We are scanning "memory" nodes only */
1169 if (type == NULL || strcmp(type, "memory") != 0)
1172 reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
1176 endp = reg + (l / sizeof(cell_t));
1178 DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
1179 uname, l, reg[0], reg[1], reg[2], reg[3]);
1181 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1182 unsigned long base, size;
1184 base = dt_mem_next_cell(dt_root_addr_cells, ®);
1185 size = dt_mem_next_cell(dt_root_size_cells, ®);
1189 DBG(" - %lx , %lx\n", base, size);
1191 if (base >= 0x80000000ul)
1193 if ((base + size) > 0x80000000ul)
1194 size = 0x80000000ul - base;
1196 lmb_add(base, size);
1201 static void __init early_reserve_mem(void)
1204 u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
1205 initial_boot_params->off_mem_rsvmap);
1207 base = *(reserve_map++);
1208 size = *(reserve_map++);
1211 DBG("reserving: %lx -> %lx\n", base, size);
1212 lmb_reserve(base, size);
1216 DBG("memory reserved, lmbs :\n");
1221 void __init early_init_devtree(void *params)
1223 DBG(" -> early_init_devtree()\n");
1225 /* Setup flat device-tree pointer */
1226 initial_boot_params = params;
1228 /* By default, hash size is not set */
1231 /* Retreive various informations from the /chosen node of the
1232 * device-tree, including the platform type, initrd location and
1233 * size, TCE reserve, and more ...
1235 scan_flat_dt(early_init_dt_scan_chosen, NULL);
1237 /* Scan memory nodes and rebuild LMBs */
1239 scan_flat_dt(early_init_dt_scan_root, NULL);
1240 scan_flat_dt(early_init_dt_scan_memory, NULL);
1241 lmb_enforce_memory_limit(memory_limit);
1243 systemcfg->physicalMemorySize = lmb_phys_mem_size();
1244 lmb_reserve(0, __pa(klimit));
1246 DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
1248 /* Reserve LMB regions used by kernel, initrd, dt, etc... */
1249 early_reserve_mem();
1251 DBG("Scanning CPUs ...\n");
1253 /* Retreive hash table size from flattened tree plus other
1254 * CPU related informations (altivec support, boot CPU ID, ...)
1256 scan_flat_dt(early_init_dt_scan_cpus, NULL);
1258 /* If hash size wasn't obtained above, we calculate it now based on
1259 * the total RAM size
1261 if (ppc64_pft_size == 0) {
1262 unsigned long rnd_mem_size, pteg_count;
1264 /* round mem_size up to next power of 2 */
1265 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
1266 if (rnd_mem_size < systemcfg->physicalMemorySize)
1270 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
1272 ppc64_pft_size = __ilog2(pteg_count << 7);
1275 DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
1276 DBG(" <- early_init_devtree()\n");
1282 prom_n_addr_cells(struct device_node* np)
1288 ip = (int *) get_property(np, "#address-cells", NULL);
1291 } while (np->parent);
1292 /* No #address-cells property for the root node, default to 1 */
1297 prom_n_size_cells(struct device_node* np)
1303 ip = (int *) get_property(np, "#size-cells", NULL);
1306 } while (np->parent);
1307 /* No #size-cells property for the root node, default to 1 */
1312 * Work out the sense (active-low level / active-high edge)
1313 * of each interrupt from the device tree.
1315 void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
1317 struct device_node *np;
1320 /* default to level-triggered */
1321 memset(senses, 1, max - off);
1323 for (np = allnodes; np != 0; np = np->allnext) {
1324 for (j = 0; j < np->n_intrs; j++) {
1325 i = np->intrs[j].line;
1326 if (i >= off && i < max)
1327 senses[i-off] = np->intrs[j].sense ?
1328 IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
1329 IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
1335 * Construct and return a list of the device_nodes with a given name.
1337 struct device_node *
1338 find_devices(const char *name)
1340 struct device_node *head, **prevp, *np;
1343 for (np = allnodes; np != 0; np = np->allnext) {
1344 if (np->name != 0 && strcasecmp(np->name, name) == 0) {
1352 EXPORT_SYMBOL(find_devices);
1355 * Construct and return a list of the device_nodes with a given type.
1357 struct device_node *
1358 find_type_devices(const char *type)
1360 struct device_node *head, **prevp, *np;
1363 for (np = allnodes; np != 0; np = np->allnext) {
1364 if (np->type != 0 && strcasecmp(np->type, type) == 0) {
1372 EXPORT_SYMBOL(find_type_devices);
1375 * Returns all nodes linked together
1377 struct device_node *
1378 find_all_nodes(void)
1380 struct device_node *head, **prevp, *np;
1383 for (np = allnodes; np != 0; np = np->allnext) {
1390 EXPORT_SYMBOL(find_all_nodes);
1392 /** Checks if the given "compat" string matches one of the strings in
1393 * the device's "compatible" property
1396 device_is_compatible(struct device_node *device, const char *compat)
1401 cp = (char *) get_property(device, "compatible", &cplen);
1405 if (strncasecmp(cp, compat, strlen(compat)) == 0)
1414 EXPORT_SYMBOL(device_is_compatible);
1418 * Indicates whether the root node has a given value in its
1419 * compatible property.
1422 machine_is_compatible(const char *compat)
1424 struct device_node *root;
1427 root = of_find_node_by_path("/");
1429 rc = device_is_compatible(root, compat);
1434 EXPORT_SYMBOL(machine_is_compatible);
1437 * Construct and return a list of the device_nodes with a given type
1438 * and compatible property.
1440 struct device_node *
1441 find_compatible_devices(const char *type, const char *compat)
1443 struct device_node *head, **prevp, *np;
1446 for (np = allnodes; np != 0; np = np->allnext) {
1448 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1450 if (device_is_compatible(np, compat)) {
1458 EXPORT_SYMBOL(find_compatible_devices);
1461 * Find the device_node with a given full_name.
1463 struct device_node *
1464 find_path_device(const char *path)
1466 struct device_node *np;
1468 for (np = allnodes; np != 0; np = np->allnext)
1469 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
1473 EXPORT_SYMBOL(find_path_device);
1477 * New implementation of the OF "find" APIs, return a refcounted
1478 * object, call of_node_put() when done. The device tree and list
1479 * are protected by a rw_lock.
1481 * Note that property management will need some locking as well,
1482 * this isn't dealt with yet.
1487 * of_find_node_by_name - Find a node by its "name" property
1488 * @from: The node to start searching from or NULL, the node
1489 * you pass will not be searched, only the next one
1490 * will; typically, you pass what the previous call
1491 * returned. of_node_put() will be called on it
1492 * @name: The name string to match against
1494 * Returns a node pointer with refcount incremented, use
1495 * of_node_put() on it when done.
1497 struct device_node *of_find_node_by_name(struct device_node *from,
1500 struct device_node *np;
1502 read_lock(&devtree_lock);
1503 np = from ? from->allnext : allnodes;
1504 for (; np != 0; np = np->allnext)
1505 if (np->name != 0 && strcasecmp(np->name, name) == 0
1510 read_unlock(&devtree_lock);
1513 EXPORT_SYMBOL(of_find_node_by_name);
1516 * of_find_node_by_type - Find a node by its "device_type" property
1517 * @from: The node to start searching from or NULL, the node
1518 * you pass will not be searched, only the next one
1519 * will; typically, you pass what the previous call
1520 * returned. of_node_put() will be called on it
1521 * @name: The type string to match against
1523 * Returns a node pointer with refcount incremented, use
1524 * of_node_put() on it when done.
1526 struct device_node *of_find_node_by_type(struct device_node *from,
1529 struct device_node *np;
1531 read_lock(&devtree_lock);
1532 np = from ? from->allnext : allnodes;
1533 for (; np != 0; np = np->allnext)
1534 if (np->type != 0 && strcasecmp(np->type, type) == 0
1539 read_unlock(&devtree_lock);
1542 EXPORT_SYMBOL(of_find_node_by_type);
1545 * of_find_compatible_node - Find a node based on type and one of the
1546 * tokens in its "compatible" property
1547 * @from: The node to start searching from or NULL, the node
1548 * you pass will not be searched, only the next one
1549 * will; typically, you pass what the previous call
1550 * returned. of_node_put() will be called on it
1551 * @type: The type string to match "device_type" or NULL to ignore
1552 * @compatible: The string to match to one of the tokens in the device
1553 * "compatible" list.
1555 * Returns a node pointer with refcount incremented, use
1556 * of_node_put() on it when done.
1558 struct device_node *of_find_compatible_node(struct device_node *from,
1559 const char *type, const char *compatible)
1561 struct device_node *np;
1563 read_lock(&devtree_lock);
1564 np = from ? from->allnext : allnodes;
1565 for (; np != 0; np = np->allnext) {
1567 && !(np->type != 0 && strcasecmp(np->type, type) == 0))
1569 if (device_is_compatible(np, compatible) && of_node_get(np))
1574 read_unlock(&devtree_lock);
1577 EXPORT_SYMBOL(of_find_compatible_node);
1580 * of_find_node_by_path - Find a node matching a full OF path
1581 * @path: The full path to match
1583 * Returns a node pointer with refcount incremented, use
1584 * of_node_put() on it when done.
1586 struct device_node *of_find_node_by_path(const char *path)
1588 struct device_node *np = allnodes;
1590 read_lock(&devtree_lock);
1591 for (; np != 0; np = np->allnext) {
1592 if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
1596 read_unlock(&devtree_lock);
1599 EXPORT_SYMBOL(of_find_node_by_path);
1602 * of_find_node_by_phandle - Find a node given a phandle
1603 * @handle: phandle of the node to find
1605 * Returns a node pointer with refcount incremented, use
1606 * of_node_put() on it when done.
1608 struct device_node *of_find_node_by_phandle(phandle handle)
1610 struct device_node *np;
1612 read_lock(&devtree_lock);
1613 for (np = allnodes; np != 0; np = np->allnext)
1614 if (np->linux_phandle == handle)
1618 read_unlock(&devtree_lock);
1621 EXPORT_SYMBOL(of_find_node_by_phandle);
1624 * of_find_all_nodes - Get next node in global list
1625 * @prev: Previous node or NULL to start iteration
1626 * of_node_put() will be called on it
1628 * Returns a node pointer with refcount incremented, use
1629 * of_node_put() on it when done.
1631 struct device_node *of_find_all_nodes(struct device_node *prev)
1633 struct device_node *np;
1635 read_lock(&devtree_lock);
1636 np = prev ? prev->allnext : allnodes;
1637 for (; np != 0; np = np->allnext)
1638 if (of_node_get(np))
1642 read_unlock(&devtree_lock);
1645 EXPORT_SYMBOL(of_find_all_nodes);
1648 * of_get_parent - Get a node's parent if any
1649 * @node: Node to get parent
1651 * Returns a node pointer with refcount incremented, use
1652 * of_node_put() on it when done.
1654 struct device_node *of_get_parent(const struct device_node *node)
1656 struct device_node *np;
1661 read_lock(&devtree_lock);
1662 np = of_node_get(node->parent);
1663 read_unlock(&devtree_lock);
1666 EXPORT_SYMBOL(of_get_parent);
1669 * of_get_next_child - Iterate a node childs
1670 * @node: parent node
1671 * @prev: previous child of the parent node, or NULL to get first
1673 * Returns a node pointer with refcount incremented, use
1674 * of_node_put() on it when done.
1676 struct device_node *of_get_next_child(const struct device_node *node,
1677 struct device_node *prev)
1679 struct device_node *next;
1681 read_lock(&devtree_lock);
1682 next = prev ? prev->sibling : node->child;
1683 for (; next != 0; next = next->sibling)
1684 if (of_node_get(next))
1688 read_unlock(&devtree_lock);
1691 EXPORT_SYMBOL(of_get_next_child);
1694 * of_node_get - Increment refcount of a node
1695 * @node: Node to inc refcount, NULL is supported to
1696 * simplify writing of callers
1700 struct device_node *of_node_get(struct device_node *node)
1703 kref_get(&node->kref);
1706 EXPORT_SYMBOL(of_node_get);
1708 static inline struct device_node * kref_to_device_node(struct kref *kref)
1710 return container_of(kref, struct device_node, kref);
1714 * of_node_release - release a dynamically allocated node
1715 * @kref: kref element of the node to be released
1717 * In of_node_put() this function is passed to kref_put()
1718 * as the destructor.
1720 static void of_node_release(struct kref *kref)
1722 struct device_node *node = kref_to_device_node(kref);
1723 struct property *prop = node->properties;
1725 if (!OF_IS_DYNAMIC(node))
1728 struct property *next = prop->next;
1736 kfree(node->full_name);
1742 * of_node_put - Decrement refcount of a node
1743 * @node: Node to dec refcount, NULL is supported to
1744 * simplify writing of callers
1747 void of_node_put(struct device_node *node)
1750 kref_put(&node->kref, of_node_release);
1752 EXPORT_SYMBOL(of_node_put);
1755 * Fix up the uninitialized fields in a new device node:
1756 * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
1758 * A lot of boot-time code is duplicated here, because functions such
1759 * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
1762 * This should probably be split up into smaller chunks.
1765 static int of_finish_dynamic_node(struct device_node *node,
1766 unsigned long *unused1, int unused2,
1767 int unused3, int unused4)
1769 struct device_node *parent = of_get_parent(node);
1771 phandle *ibm_phandle;
1773 node->name = get_property(node, "name", NULL);
1774 node->type = get_property(node, "device_type", NULL);
1781 /* We don't support that function on PowerMac, at least
1784 if (systemcfg->platform == PLATFORM_POWERMAC)
1787 /* fix up new node's linux_phandle field */
1788 if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
1789 node->linux_phandle = *ibm_phandle;
1792 of_node_put(parent);
1797 * Plug a device node into the tree and global list.
1799 void of_attach_node(struct device_node *np)
1801 write_lock(&devtree_lock);
1802 np->sibling = np->parent->child;
1803 np->allnext = allnodes;
1804 np->parent->child = np;
1806 write_unlock(&devtree_lock);
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 void of_detach_node(const struct device_node *np)
1816 struct device_node *parent;
1818 write_lock(&devtree_lock);
1820 parent = np->parent;
1823 allnodes = np->allnext;
1825 struct device_node *prev;
1826 for (prev = allnodes;
1827 prev->allnext != np;
1828 prev = prev->allnext)
1830 prev->allnext = np->allnext;
1833 if (parent->child == np)
1834 parent->child = np->sibling;
1836 struct device_node *prevsib;
1837 for (prevsib = np->parent->child;
1838 prevsib->sibling != np;
1839 prevsib = prevsib->sibling)
1841 prevsib->sibling = np->sibling;
1844 write_unlock(&devtree_lock);
1847 static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1852 case PSERIES_RECONFIG_ADD:
1853 err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
1855 printk(KERN_ERR "finish_node returned %d\n", err);
1866 static struct notifier_block prom_reconfig_nb = {
1867 .notifier_call = prom_reconfig_notifier,
1868 .priority = 10, /* This one needs to run first */
1871 static int __init prom_reconfig_setup(void)
1873 return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
1875 __initcall(prom_reconfig_setup);
1878 * Find a property with a given name for a given node
1879 * and return the value.
1882 get_property(struct device_node *np, const char *name, int *lenp)
1884 struct property *pp;
1886 for (pp = np->properties; pp != 0; pp = pp->next)
1887 if (strcmp(pp->name, name) == 0) {
1894 EXPORT_SYMBOL(get_property);
1897 * Add a property to a node
1900 prom_add_property(struct device_node* np, struct property* prop)
1902 struct property **next = &np->properties;
1906 next = &(*next)->next;
1912 print_properties(struct device_node *np)
1914 struct property *pp;
1918 for (pp = np->properties; pp != 0; pp = pp->next) {
1919 printk(KERN_INFO "%s", pp->name);
1920 for (i = strlen(pp->name); i < 16; ++i)
1922 cp = (char *) pp->value;
1923 for (i = pp->length; i > 0; --i, ++cp)
1924 if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
1925 || (i == 1 && *cp != 0))
1927 if (i == 0 && pp->length > 1) {
1928 /* looks like a string */
1929 printk(" %s\n", (char *) pp->value);
1931 /* dump it in hex */
1935 if (pp->length % 4 == 0) {
1936 unsigned int *p = (unsigned int *) pp->value;
1939 for (i = 0; i < n; ++i) {
1940 if (i != 0 && (i % 4) == 0)
1942 printk(" %08x", *p++);
1945 unsigned char *bp = pp->value;
1947 for (i = 0; i < n; ++i) {
1948 if (i != 0 && (i % 16) == 0)
1950 printk(" %02x", *bp++);
1954 if (pp->length > 64)
1955 printk(" ... (length = %d)\n",