19b6b563cSPaul Mackerras /* 29b6b563cSPaul Mackerras * Procedures for creating, accessing and interpreting the device tree. 39b6b563cSPaul Mackerras * 49b6b563cSPaul Mackerras * Paul Mackerras August 1996. 59b6b563cSPaul Mackerras * Copyright (C) 1996-2005 Paul Mackerras. 69b6b563cSPaul Mackerras * 79b6b563cSPaul Mackerras * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. 89b6b563cSPaul Mackerras * {engebret|bergner}@us.ibm.com 99b6b563cSPaul Mackerras * 109b6b563cSPaul Mackerras * This program is free software; you can redistribute it and/or 119b6b563cSPaul Mackerras * modify it under the terms of the GNU General Public License 129b6b563cSPaul Mackerras * as published by the Free Software Foundation; either version 139b6b563cSPaul Mackerras * 2 of the License, or (at your option) any later version. 149b6b563cSPaul Mackerras */ 159b6b563cSPaul Mackerras 169b6b563cSPaul Mackerras #undef DEBUG 179b6b563cSPaul Mackerras 189b6b563cSPaul Mackerras #include <stdarg.h> 199b6b563cSPaul Mackerras #include <linux/config.h> 209b6b563cSPaul Mackerras #include <linux/kernel.h> 219b6b563cSPaul Mackerras #include <linux/string.h> 229b6b563cSPaul Mackerras #include <linux/init.h> 239b6b563cSPaul Mackerras #include <linux/threads.h> 249b6b563cSPaul Mackerras #include <linux/spinlock.h> 259b6b563cSPaul Mackerras #include <linux/types.h> 269b6b563cSPaul Mackerras #include <linux/pci.h> 279b6b563cSPaul Mackerras #include <linux/stringify.h> 289b6b563cSPaul Mackerras #include <linux/delay.h> 299b6b563cSPaul Mackerras #include <linux/initrd.h> 309b6b563cSPaul Mackerras #include <linux/bitops.h> 319b6b563cSPaul Mackerras #include <linux/module.h> 329b6b563cSPaul Mackerras 339b6b563cSPaul Mackerras #include <asm/prom.h> 349b6b563cSPaul Mackerras #include <asm/rtas.h> 359b6b563cSPaul Mackerras #include <asm/lmb.h> 369b6b563cSPaul Mackerras #include <asm/page.h> 379b6b563cSPaul Mackerras #include <asm/processor.h> 389b6b563cSPaul Mackerras #include <asm/irq.h> 399b6b563cSPaul Mackerras #include <asm/io.h> 409b6b563cSPaul Mackerras #include <asm/smp.h> 419b6b563cSPaul Mackerras #include <asm/system.h> 429b6b563cSPaul Mackerras #include <asm/mmu.h> 439b6b563cSPaul Mackerras #include <asm/pgtable.h> 449b6b563cSPaul Mackerras #include <asm/pci.h> 459b6b563cSPaul Mackerras #include <asm/iommu.h> 469b6b563cSPaul Mackerras #include <asm/btext.h> 479b6b563cSPaul Mackerras #include <asm/sections.h> 489b6b563cSPaul Mackerras #include <asm/machdep.h> 499b6b563cSPaul Mackerras #include <asm/pSeries_reconfig.h> 50*40ef8cbcSPaul Mackerras #include <asm/pci-bridge.h> 51*40ef8cbcSPaul Mackerras #ifdef CONFIG_PPC64 52*40ef8cbcSPaul Mackerras #include <asm/systemcfg.h> 53*40ef8cbcSPaul Mackerras #endif 549b6b563cSPaul Mackerras 559b6b563cSPaul Mackerras #ifdef DEBUG 569b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt) 579b6b563cSPaul Mackerras #else 589b6b563cSPaul Mackerras #define DBG(fmt...) 599b6b563cSPaul Mackerras #endif 609b6b563cSPaul Mackerras 619b6b563cSPaul Mackerras struct pci_reg_property { 629b6b563cSPaul Mackerras struct pci_address addr; 639b6b563cSPaul Mackerras u32 size_hi; 649b6b563cSPaul Mackerras u32 size_lo; 659b6b563cSPaul Mackerras }; 669b6b563cSPaul Mackerras 679b6b563cSPaul Mackerras struct isa_reg_property { 689b6b563cSPaul Mackerras u32 space; 699b6b563cSPaul Mackerras u32 address; 709b6b563cSPaul Mackerras u32 size; 719b6b563cSPaul Mackerras }; 729b6b563cSPaul Mackerras 739b6b563cSPaul Mackerras 749b6b563cSPaul Mackerras typedef int interpret_func(struct device_node *, unsigned long *, 759b6b563cSPaul Mackerras int, int, int); 769b6b563cSPaul Mackerras 779b6b563cSPaul Mackerras extern struct rtas_t rtas; 789b6b563cSPaul Mackerras extern struct lmb lmb; 799b6b563cSPaul Mackerras extern unsigned long klimit; 809b6b563cSPaul Mackerras 819b6b563cSPaul Mackerras static unsigned long memory_limit; 829b6b563cSPaul Mackerras 839b6b563cSPaul Mackerras static int __initdata dt_root_addr_cells; 849b6b563cSPaul Mackerras static int __initdata dt_root_size_cells; 859b6b563cSPaul Mackerras 869b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 879b6b563cSPaul Mackerras static int __initdata iommu_is_off; 889b6b563cSPaul Mackerras int __initdata iommu_force_on; 899b6b563cSPaul Mackerras extern unsigned long tce_alloc_start, tce_alloc_end; 909b6b563cSPaul Mackerras #endif 919b6b563cSPaul Mackerras 929b6b563cSPaul Mackerras typedef u32 cell_t; 939b6b563cSPaul Mackerras 949b6b563cSPaul Mackerras #if 0 959b6b563cSPaul Mackerras static struct boot_param_header *initial_boot_params __initdata; 969b6b563cSPaul Mackerras #else 979b6b563cSPaul Mackerras struct boot_param_header *initial_boot_params; 989b6b563cSPaul Mackerras #endif 999b6b563cSPaul Mackerras 1009b6b563cSPaul Mackerras static struct device_node *allnodes = NULL; 1019b6b563cSPaul Mackerras 1029b6b563cSPaul Mackerras /* use when traversing tree through the allnext, child, sibling, 1039b6b563cSPaul Mackerras * or parent members of struct device_node. 1049b6b563cSPaul Mackerras */ 1059b6b563cSPaul Mackerras static DEFINE_RWLOCK(devtree_lock); 1069b6b563cSPaul Mackerras 1079b6b563cSPaul Mackerras /* export that to outside world */ 1089b6b563cSPaul Mackerras struct device_node *of_chosen; 1099b6b563cSPaul Mackerras 1109b6b563cSPaul Mackerras struct device_node *dflt_interrupt_controller; 1119b6b563cSPaul Mackerras int num_interrupt_controllers; 1129b6b563cSPaul Mackerras 1139b6b563cSPaul Mackerras u32 rtas_data; 1149b6b563cSPaul Mackerras u32 rtas_entry; 1159b6b563cSPaul Mackerras 1169b6b563cSPaul Mackerras /* 1179b6b563cSPaul Mackerras * Wrapper for allocating memory for various data that needs to be 1189b6b563cSPaul Mackerras * attached to device nodes as they are processed at boot or when 1199b6b563cSPaul Mackerras * added to the device tree later (e.g. DLPAR). At boot there is 1209b6b563cSPaul Mackerras * already a region reserved so we just increment *mem_start by size; 1219b6b563cSPaul Mackerras * otherwise we call kmalloc. 1229b6b563cSPaul Mackerras */ 1239b6b563cSPaul Mackerras static void * prom_alloc(unsigned long size, unsigned long *mem_start) 1249b6b563cSPaul Mackerras { 1259b6b563cSPaul Mackerras unsigned long tmp; 1269b6b563cSPaul Mackerras 1279b6b563cSPaul Mackerras if (!mem_start) 1289b6b563cSPaul Mackerras return kmalloc(size, GFP_KERNEL); 1299b6b563cSPaul Mackerras 1309b6b563cSPaul Mackerras tmp = *mem_start; 1319b6b563cSPaul Mackerras *mem_start += size; 1329b6b563cSPaul Mackerras return (void *)tmp; 1339b6b563cSPaul Mackerras } 1349b6b563cSPaul Mackerras 1359b6b563cSPaul Mackerras /* 1369b6b563cSPaul Mackerras * Find the device_node with a given phandle. 1379b6b563cSPaul Mackerras */ 1389b6b563cSPaul Mackerras static struct device_node * find_phandle(phandle ph) 1399b6b563cSPaul Mackerras { 1409b6b563cSPaul Mackerras struct device_node *np; 1419b6b563cSPaul Mackerras 1429b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 1439b6b563cSPaul Mackerras if (np->linux_phandle == ph) 1449b6b563cSPaul Mackerras return np; 1459b6b563cSPaul Mackerras return NULL; 1469b6b563cSPaul Mackerras } 1479b6b563cSPaul Mackerras 1489b6b563cSPaul Mackerras /* 1499b6b563cSPaul Mackerras * Find the interrupt parent of a node. 1509b6b563cSPaul Mackerras */ 1519b6b563cSPaul Mackerras static struct device_node * __devinit intr_parent(struct device_node *p) 1529b6b563cSPaul Mackerras { 1539b6b563cSPaul Mackerras phandle *parp; 1549b6b563cSPaul Mackerras 1559b6b563cSPaul Mackerras parp = (phandle *) get_property(p, "interrupt-parent", NULL); 1569b6b563cSPaul Mackerras if (parp == NULL) 1579b6b563cSPaul Mackerras return p->parent; 1589b6b563cSPaul Mackerras p = find_phandle(*parp); 1599b6b563cSPaul Mackerras if (p != NULL) 1609b6b563cSPaul Mackerras return p; 1619b6b563cSPaul Mackerras /* 1629b6b563cSPaul Mackerras * On a powermac booted with BootX, we don't get to know the 1639b6b563cSPaul Mackerras * phandles for any nodes, so find_phandle will return NULL. 1649b6b563cSPaul Mackerras * Fortunately these machines only have one interrupt controller 1659b6b563cSPaul Mackerras * so there isn't in fact any ambiguity. -- paulus 1669b6b563cSPaul Mackerras */ 1679b6b563cSPaul Mackerras if (num_interrupt_controllers == 1) 1689b6b563cSPaul Mackerras p = dflt_interrupt_controller; 1699b6b563cSPaul Mackerras return p; 1709b6b563cSPaul Mackerras } 1719b6b563cSPaul Mackerras 1729b6b563cSPaul Mackerras /* 1739b6b563cSPaul Mackerras * Find out the size of each entry of the interrupts property 1749b6b563cSPaul Mackerras * for a node. 1759b6b563cSPaul Mackerras */ 1769b6b563cSPaul Mackerras int __devinit prom_n_intr_cells(struct device_node *np) 1779b6b563cSPaul Mackerras { 1789b6b563cSPaul Mackerras struct device_node *p; 1799b6b563cSPaul Mackerras unsigned int *icp; 1809b6b563cSPaul Mackerras 1819b6b563cSPaul Mackerras for (p = np; (p = intr_parent(p)) != NULL; ) { 1829b6b563cSPaul Mackerras icp = (unsigned int *) 1839b6b563cSPaul Mackerras get_property(p, "#interrupt-cells", NULL); 1849b6b563cSPaul Mackerras if (icp != NULL) 1859b6b563cSPaul Mackerras return *icp; 1869b6b563cSPaul Mackerras if (get_property(p, "interrupt-controller", NULL) != NULL 1879b6b563cSPaul Mackerras || get_property(p, "interrupt-map", NULL) != NULL) { 1889b6b563cSPaul Mackerras printk("oops, node %s doesn't have #interrupt-cells\n", 1899b6b563cSPaul Mackerras p->full_name); 1909b6b563cSPaul Mackerras return 1; 1919b6b563cSPaul Mackerras } 1929b6b563cSPaul Mackerras } 1939b6b563cSPaul Mackerras #ifdef DEBUG_IRQ 1949b6b563cSPaul Mackerras printk("prom_n_intr_cells failed for %s\n", np->full_name); 1959b6b563cSPaul Mackerras #endif 1969b6b563cSPaul Mackerras return 1; 1979b6b563cSPaul Mackerras } 1989b6b563cSPaul Mackerras 1999b6b563cSPaul Mackerras /* 2009b6b563cSPaul Mackerras * Map an interrupt from a device up to the platform interrupt 2019b6b563cSPaul Mackerras * descriptor. 2029b6b563cSPaul Mackerras */ 2039b6b563cSPaul Mackerras static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler, 2049b6b563cSPaul Mackerras struct device_node *np, unsigned int *ints, 2059b6b563cSPaul Mackerras int nintrc) 2069b6b563cSPaul Mackerras { 2079b6b563cSPaul Mackerras struct device_node *p, *ipar; 2089b6b563cSPaul Mackerras unsigned int *imap, *imask, *ip; 2099b6b563cSPaul Mackerras int i, imaplen, match; 2109b6b563cSPaul Mackerras int newintrc = 0, newaddrc = 0; 2119b6b563cSPaul Mackerras unsigned int *reg; 2129b6b563cSPaul Mackerras int naddrc; 2139b6b563cSPaul Mackerras 2149b6b563cSPaul Mackerras reg = (unsigned int *) get_property(np, "reg", NULL); 2159b6b563cSPaul Mackerras naddrc = prom_n_addr_cells(np); 2169b6b563cSPaul Mackerras p = intr_parent(np); 2179b6b563cSPaul Mackerras while (p != NULL) { 2189b6b563cSPaul Mackerras if (get_property(p, "interrupt-controller", NULL) != NULL) 2199b6b563cSPaul Mackerras /* this node is an interrupt controller, stop here */ 2209b6b563cSPaul Mackerras break; 2219b6b563cSPaul Mackerras imap = (unsigned int *) 2229b6b563cSPaul Mackerras get_property(p, "interrupt-map", &imaplen); 2239b6b563cSPaul Mackerras if (imap == NULL) { 2249b6b563cSPaul Mackerras p = intr_parent(p); 2259b6b563cSPaul Mackerras continue; 2269b6b563cSPaul Mackerras } 2279b6b563cSPaul Mackerras imask = (unsigned int *) 2289b6b563cSPaul Mackerras get_property(p, "interrupt-map-mask", NULL); 2299b6b563cSPaul Mackerras if (imask == NULL) { 2309b6b563cSPaul Mackerras printk("oops, %s has interrupt-map but no mask\n", 2319b6b563cSPaul Mackerras p->full_name); 2329b6b563cSPaul Mackerras return 0; 2339b6b563cSPaul Mackerras } 2349b6b563cSPaul Mackerras imaplen /= sizeof(unsigned int); 2359b6b563cSPaul Mackerras match = 0; 2369b6b563cSPaul Mackerras ipar = NULL; 2379b6b563cSPaul Mackerras while (imaplen > 0 && !match) { 2389b6b563cSPaul Mackerras /* check the child-interrupt field */ 2399b6b563cSPaul Mackerras match = 1; 2409b6b563cSPaul Mackerras for (i = 0; i < naddrc && match; ++i) 2419b6b563cSPaul Mackerras match = ((reg[i] ^ imap[i]) & imask[i]) == 0; 2429b6b563cSPaul Mackerras for (; i < naddrc + nintrc && match; ++i) 2439b6b563cSPaul Mackerras match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0; 2449b6b563cSPaul Mackerras imap += naddrc + nintrc; 2459b6b563cSPaul Mackerras imaplen -= naddrc + nintrc; 2469b6b563cSPaul Mackerras /* grab the interrupt parent */ 2479b6b563cSPaul Mackerras ipar = find_phandle((phandle) *imap++); 2489b6b563cSPaul Mackerras --imaplen; 2499b6b563cSPaul Mackerras if (ipar == NULL && num_interrupt_controllers == 1) 2509b6b563cSPaul Mackerras /* cope with BootX not giving us phandles */ 2519b6b563cSPaul Mackerras ipar = dflt_interrupt_controller; 2529b6b563cSPaul Mackerras if (ipar == NULL) { 2539b6b563cSPaul Mackerras printk("oops, no int parent %x in map of %s\n", 2549b6b563cSPaul Mackerras imap[-1], p->full_name); 2559b6b563cSPaul Mackerras return 0; 2569b6b563cSPaul Mackerras } 2579b6b563cSPaul Mackerras /* find the parent's # addr and intr cells */ 2589b6b563cSPaul Mackerras ip = (unsigned int *) 2599b6b563cSPaul Mackerras get_property(ipar, "#interrupt-cells", NULL); 2609b6b563cSPaul Mackerras if (ip == NULL) { 2619b6b563cSPaul Mackerras printk("oops, no #interrupt-cells on %s\n", 2629b6b563cSPaul Mackerras ipar->full_name); 2639b6b563cSPaul Mackerras return 0; 2649b6b563cSPaul Mackerras } 2659b6b563cSPaul Mackerras newintrc = *ip; 2669b6b563cSPaul Mackerras ip = (unsigned int *) 2679b6b563cSPaul Mackerras get_property(ipar, "#address-cells", NULL); 2689b6b563cSPaul Mackerras newaddrc = (ip == NULL)? 0: *ip; 2699b6b563cSPaul Mackerras imap += newaddrc + newintrc; 2709b6b563cSPaul Mackerras imaplen -= newaddrc + newintrc; 2719b6b563cSPaul Mackerras } 2729b6b563cSPaul Mackerras if (imaplen < 0) { 2739b6b563cSPaul Mackerras printk("oops, error decoding int-map on %s, len=%d\n", 2749b6b563cSPaul Mackerras p->full_name, imaplen); 2759b6b563cSPaul Mackerras return 0; 2769b6b563cSPaul Mackerras } 2779b6b563cSPaul Mackerras if (!match) { 2789b6b563cSPaul Mackerras #ifdef DEBUG_IRQ 2799b6b563cSPaul Mackerras printk("oops, no match in %s int-map for %s\n", 2809b6b563cSPaul Mackerras p->full_name, np->full_name); 2819b6b563cSPaul Mackerras #endif 2829b6b563cSPaul Mackerras return 0; 2839b6b563cSPaul Mackerras } 2849b6b563cSPaul Mackerras p = ipar; 2859b6b563cSPaul Mackerras naddrc = newaddrc; 2869b6b563cSPaul Mackerras nintrc = newintrc; 2879b6b563cSPaul Mackerras ints = imap - nintrc; 2889b6b563cSPaul Mackerras reg = ints - naddrc; 2899b6b563cSPaul Mackerras } 2909b6b563cSPaul Mackerras if (p == NULL) { 2919b6b563cSPaul Mackerras #ifdef DEBUG_IRQ 2929b6b563cSPaul Mackerras printk("hmmm, int tree for %s doesn't have ctrler\n", 2939b6b563cSPaul Mackerras np->full_name); 2949b6b563cSPaul Mackerras #endif 2959b6b563cSPaul Mackerras return 0; 2969b6b563cSPaul Mackerras } 2979b6b563cSPaul Mackerras *irq = ints; 2989b6b563cSPaul Mackerras *ictrler = p; 2999b6b563cSPaul Mackerras return nintrc; 3009b6b563cSPaul Mackerras } 3019b6b563cSPaul Mackerras 3029b6b563cSPaul Mackerras static int __devinit finish_node_interrupts(struct device_node *np, 3039b6b563cSPaul Mackerras unsigned long *mem_start, 3049b6b563cSPaul Mackerras int measure_only) 3059b6b563cSPaul Mackerras { 3069b6b563cSPaul Mackerras unsigned int *ints; 3079b6b563cSPaul Mackerras int intlen, intrcells, intrcount; 3089b6b563cSPaul Mackerras int i, j, n; 3099b6b563cSPaul Mackerras unsigned int *irq, virq; 3109b6b563cSPaul Mackerras struct device_node *ic; 3119b6b563cSPaul Mackerras 3129b6b563cSPaul Mackerras ints = (unsigned int *) get_property(np, "interrupts", &intlen); 3139b6b563cSPaul Mackerras if (ints == NULL) 3149b6b563cSPaul Mackerras return 0; 3159b6b563cSPaul Mackerras intrcells = prom_n_intr_cells(np); 3169b6b563cSPaul Mackerras intlen /= intrcells * sizeof(unsigned int); 3179b6b563cSPaul Mackerras 3189b6b563cSPaul Mackerras np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start); 3199b6b563cSPaul Mackerras if (!np->intrs) 3209b6b563cSPaul Mackerras return -ENOMEM; 3219b6b563cSPaul Mackerras 3229b6b563cSPaul Mackerras if (measure_only) 3239b6b563cSPaul Mackerras return 0; 3249b6b563cSPaul Mackerras 3259b6b563cSPaul Mackerras intrcount = 0; 3269b6b563cSPaul Mackerras for (i = 0; i < intlen; ++i, ints += intrcells) { 3279b6b563cSPaul Mackerras n = map_interrupt(&irq, &ic, np, ints, intrcells); 3289b6b563cSPaul Mackerras if (n <= 0) 3299b6b563cSPaul Mackerras continue; 3309b6b563cSPaul Mackerras 3319b6b563cSPaul Mackerras /* don't map IRQ numbers under a cascaded 8259 controller */ 3329b6b563cSPaul Mackerras if (ic && device_is_compatible(ic, "chrp,iic")) { 3339b6b563cSPaul Mackerras np->intrs[intrcount].line = irq[0]; 3349b6b563cSPaul Mackerras } else { 3359b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 3369b6b563cSPaul Mackerras virq = virt_irq_create_mapping(irq[0]); 3379b6b563cSPaul Mackerras if (virq == NO_IRQ) { 3389b6b563cSPaul Mackerras printk(KERN_CRIT "Could not allocate interrupt" 3399b6b563cSPaul Mackerras " number for %s\n", np->full_name); 3409b6b563cSPaul Mackerras continue; 3419b6b563cSPaul Mackerras } 3429b6b563cSPaul Mackerras virq = irq_offset_up(virq); 3439b6b563cSPaul Mackerras #else 3449b6b563cSPaul Mackerras virq = irq[0]; 3459b6b563cSPaul Mackerras #endif 3469b6b563cSPaul Mackerras np->intrs[intrcount].line = virq; 3479b6b563cSPaul Mackerras } 3489b6b563cSPaul Mackerras 3499b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 3509b6b563cSPaul Mackerras /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */ 3519b6b563cSPaul Mackerras if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) { 3529b6b563cSPaul Mackerras char *name = get_property(ic->parent, "name", NULL); 3539b6b563cSPaul Mackerras if (name && !strcmp(name, "u3")) 3549b6b563cSPaul Mackerras np->intrs[intrcount].line += 128; 3559b6b563cSPaul Mackerras else if (!(name && !strcmp(name, "mac-io"))) 3569b6b563cSPaul Mackerras /* ignore other cascaded controllers, such as 3579b6b563cSPaul Mackerras the k2-sata-root */ 3589b6b563cSPaul Mackerras break; 3599b6b563cSPaul Mackerras } 3609b6b563cSPaul Mackerras #endif 3619b6b563cSPaul Mackerras np->intrs[intrcount].sense = 1; 3629b6b563cSPaul Mackerras if (n > 1) 3639b6b563cSPaul Mackerras np->intrs[intrcount].sense = irq[1]; 3649b6b563cSPaul Mackerras if (n > 2) { 3659b6b563cSPaul Mackerras printk("hmmm, got %d intr cells for %s:", n, 3669b6b563cSPaul Mackerras np->full_name); 3679b6b563cSPaul Mackerras for (j = 0; j < n; ++j) 3689b6b563cSPaul Mackerras printk(" %d", irq[j]); 3699b6b563cSPaul Mackerras printk("\n"); 3709b6b563cSPaul Mackerras } 3719b6b563cSPaul Mackerras ++intrcount; 3729b6b563cSPaul Mackerras } 3739b6b563cSPaul Mackerras np->n_intrs = intrcount; 3749b6b563cSPaul Mackerras 3759b6b563cSPaul Mackerras return 0; 3769b6b563cSPaul Mackerras } 3779b6b563cSPaul Mackerras 3789b6b563cSPaul Mackerras static int __devinit interpret_pci_props(struct device_node *np, 3799b6b563cSPaul Mackerras unsigned long *mem_start, 3809b6b563cSPaul Mackerras int naddrc, int nsizec, 3819b6b563cSPaul Mackerras int measure_only) 3829b6b563cSPaul Mackerras { 3839b6b563cSPaul Mackerras struct address_range *adr; 3849b6b563cSPaul Mackerras struct pci_reg_property *pci_addrs; 3859b6b563cSPaul Mackerras int i, l, n_addrs; 3869b6b563cSPaul Mackerras 3879b6b563cSPaul Mackerras pci_addrs = (struct pci_reg_property *) 3889b6b563cSPaul Mackerras get_property(np, "assigned-addresses", &l); 3899b6b563cSPaul Mackerras if (!pci_addrs) 3909b6b563cSPaul Mackerras return 0; 3919b6b563cSPaul Mackerras 3929b6b563cSPaul Mackerras n_addrs = l / sizeof(*pci_addrs); 3939b6b563cSPaul Mackerras 3949b6b563cSPaul Mackerras adr = prom_alloc(n_addrs * sizeof(*adr), mem_start); 3959b6b563cSPaul Mackerras if (!adr) 3969b6b563cSPaul Mackerras return -ENOMEM; 3979b6b563cSPaul Mackerras 3989b6b563cSPaul Mackerras if (measure_only) 3999b6b563cSPaul Mackerras return 0; 4009b6b563cSPaul Mackerras 4019b6b563cSPaul Mackerras np->addrs = adr; 4029b6b563cSPaul Mackerras np->n_addrs = n_addrs; 4039b6b563cSPaul Mackerras 4049b6b563cSPaul Mackerras for (i = 0; i < n_addrs; i++) { 4059b6b563cSPaul Mackerras adr[i].space = pci_addrs[i].addr.a_hi; 4069b6b563cSPaul Mackerras adr[i].address = pci_addrs[i].addr.a_lo | 4079b6b563cSPaul Mackerras ((u64)pci_addrs[i].addr.a_mid << 32); 4089b6b563cSPaul Mackerras adr[i].size = pci_addrs[i].size_lo; 4099b6b563cSPaul Mackerras } 4109b6b563cSPaul Mackerras 4119b6b563cSPaul Mackerras return 0; 4129b6b563cSPaul Mackerras } 4139b6b563cSPaul Mackerras 4149b6b563cSPaul Mackerras static int __init interpret_dbdma_props(struct device_node *np, 4159b6b563cSPaul Mackerras unsigned long *mem_start, 4169b6b563cSPaul Mackerras int naddrc, int nsizec, 4179b6b563cSPaul Mackerras int measure_only) 4189b6b563cSPaul Mackerras { 4199b6b563cSPaul Mackerras struct reg_property32 *rp; 4209b6b563cSPaul Mackerras struct address_range *adr; 4219b6b563cSPaul Mackerras unsigned long base_address; 4229b6b563cSPaul Mackerras int i, l; 4239b6b563cSPaul Mackerras struct device_node *db; 4249b6b563cSPaul Mackerras 4259b6b563cSPaul Mackerras base_address = 0; 4269b6b563cSPaul Mackerras if (!measure_only) { 4279b6b563cSPaul Mackerras for (db = np->parent; db != NULL; db = db->parent) { 4289b6b563cSPaul Mackerras if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) { 4299b6b563cSPaul Mackerras base_address = db->addrs[0].address; 4309b6b563cSPaul Mackerras break; 4319b6b563cSPaul Mackerras } 4329b6b563cSPaul Mackerras } 4339b6b563cSPaul Mackerras } 4349b6b563cSPaul Mackerras 4359b6b563cSPaul Mackerras rp = (struct reg_property32 *) get_property(np, "reg", &l); 4369b6b563cSPaul Mackerras if (rp != 0 && l >= sizeof(struct reg_property32)) { 4379b6b563cSPaul Mackerras i = 0; 4389b6b563cSPaul Mackerras adr = (struct address_range *) (*mem_start); 4399b6b563cSPaul Mackerras while ((l -= sizeof(struct reg_property32)) >= 0) { 4409b6b563cSPaul Mackerras if (!measure_only) { 4419b6b563cSPaul Mackerras adr[i].space = 2; 4429b6b563cSPaul Mackerras adr[i].address = rp[i].address + base_address; 4439b6b563cSPaul Mackerras adr[i].size = rp[i].size; 4449b6b563cSPaul Mackerras } 4459b6b563cSPaul Mackerras ++i; 4469b6b563cSPaul Mackerras } 4479b6b563cSPaul Mackerras np->addrs = adr; 4489b6b563cSPaul Mackerras np->n_addrs = i; 4499b6b563cSPaul Mackerras (*mem_start) += i * sizeof(struct address_range); 4509b6b563cSPaul Mackerras } 4519b6b563cSPaul Mackerras 4529b6b563cSPaul Mackerras return 0; 4539b6b563cSPaul Mackerras } 4549b6b563cSPaul Mackerras 4559b6b563cSPaul Mackerras static int __init interpret_macio_props(struct device_node *np, 4569b6b563cSPaul Mackerras unsigned long *mem_start, 4579b6b563cSPaul Mackerras int naddrc, int nsizec, 4589b6b563cSPaul Mackerras int measure_only) 4599b6b563cSPaul Mackerras { 4609b6b563cSPaul Mackerras struct reg_property32 *rp; 4619b6b563cSPaul Mackerras struct address_range *adr; 4629b6b563cSPaul Mackerras unsigned long base_address; 4639b6b563cSPaul Mackerras int i, l; 4649b6b563cSPaul Mackerras struct device_node *db; 4659b6b563cSPaul Mackerras 4669b6b563cSPaul Mackerras base_address = 0; 4679b6b563cSPaul Mackerras if (!measure_only) { 4689b6b563cSPaul Mackerras for (db = np->parent; db != NULL; db = db->parent) { 4699b6b563cSPaul Mackerras if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) { 4709b6b563cSPaul Mackerras base_address = db->addrs[0].address; 4719b6b563cSPaul Mackerras break; 4729b6b563cSPaul Mackerras } 4739b6b563cSPaul Mackerras } 4749b6b563cSPaul Mackerras } 4759b6b563cSPaul Mackerras 4769b6b563cSPaul Mackerras rp = (struct reg_property32 *) get_property(np, "reg", &l); 4779b6b563cSPaul Mackerras if (rp != 0 && l >= sizeof(struct reg_property32)) { 4789b6b563cSPaul Mackerras i = 0; 4799b6b563cSPaul Mackerras adr = (struct address_range *) (*mem_start); 4809b6b563cSPaul Mackerras while ((l -= sizeof(struct reg_property32)) >= 0) { 4819b6b563cSPaul Mackerras if (!measure_only) { 4829b6b563cSPaul Mackerras adr[i].space = 2; 4839b6b563cSPaul Mackerras adr[i].address = rp[i].address + base_address; 4849b6b563cSPaul Mackerras adr[i].size = rp[i].size; 4859b6b563cSPaul Mackerras } 4869b6b563cSPaul Mackerras ++i; 4879b6b563cSPaul Mackerras } 4889b6b563cSPaul Mackerras np->addrs = adr; 4899b6b563cSPaul Mackerras np->n_addrs = i; 4909b6b563cSPaul Mackerras (*mem_start) += i * sizeof(struct address_range); 4919b6b563cSPaul Mackerras } 4929b6b563cSPaul Mackerras 4939b6b563cSPaul Mackerras return 0; 4949b6b563cSPaul Mackerras } 4959b6b563cSPaul Mackerras 4969b6b563cSPaul Mackerras static int __init interpret_isa_props(struct device_node *np, 4979b6b563cSPaul Mackerras unsigned long *mem_start, 4989b6b563cSPaul Mackerras int naddrc, int nsizec, 4999b6b563cSPaul Mackerras int measure_only) 5009b6b563cSPaul Mackerras { 5019b6b563cSPaul Mackerras struct isa_reg_property *rp; 5029b6b563cSPaul Mackerras struct address_range *adr; 5039b6b563cSPaul Mackerras int i, l; 5049b6b563cSPaul Mackerras 5059b6b563cSPaul Mackerras rp = (struct isa_reg_property *) get_property(np, "reg", &l); 5069b6b563cSPaul Mackerras if (rp != 0 && l >= sizeof(struct isa_reg_property)) { 5079b6b563cSPaul Mackerras i = 0; 5089b6b563cSPaul Mackerras adr = (struct address_range *) (*mem_start); 5099b6b563cSPaul Mackerras while ((l -= sizeof(struct isa_reg_property)) >= 0) { 5109b6b563cSPaul Mackerras if (!measure_only) { 5119b6b563cSPaul Mackerras adr[i].space = rp[i].space; 5129b6b563cSPaul Mackerras adr[i].address = rp[i].address; 5139b6b563cSPaul Mackerras adr[i].size = rp[i].size; 5149b6b563cSPaul Mackerras } 5159b6b563cSPaul Mackerras ++i; 5169b6b563cSPaul Mackerras } 5179b6b563cSPaul Mackerras np->addrs = adr; 5189b6b563cSPaul Mackerras np->n_addrs = i; 5199b6b563cSPaul Mackerras (*mem_start) += i * sizeof(struct address_range); 5209b6b563cSPaul Mackerras } 5219b6b563cSPaul Mackerras 5229b6b563cSPaul Mackerras return 0; 5239b6b563cSPaul Mackerras } 5249b6b563cSPaul Mackerras 5259b6b563cSPaul Mackerras static int __init interpret_root_props(struct device_node *np, 5269b6b563cSPaul Mackerras unsigned long *mem_start, 5279b6b563cSPaul Mackerras int naddrc, int nsizec, 5289b6b563cSPaul Mackerras int measure_only) 5299b6b563cSPaul Mackerras { 5309b6b563cSPaul Mackerras struct address_range *adr; 5319b6b563cSPaul Mackerras int i, l; 5329b6b563cSPaul Mackerras unsigned int *rp; 5339b6b563cSPaul Mackerras int rpsize = (naddrc + nsizec) * sizeof(unsigned int); 5349b6b563cSPaul Mackerras 5359b6b563cSPaul Mackerras rp = (unsigned int *) get_property(np, "reg", &l); 5369b6b563cSPaul Mackerras if (rp != 0 && l >= rpsize) { 5379b6b563cSPaul Mackerras i = 0; 5389b6b563cSPaul Mackerras adr = (struct address_range *) (*mem_start); 5399b6b563cSPaul Mackerras while ((l -= rpsize) >= 0) { 5409b6b563cSPaul Mackerras if (!measure_only) { 5419b6b563cSPaul Mackerras adr[i].space = 0; 5429b6b563cSPaul Mackerras adr[i].address = rp[naddrc - 1]; 5439b6b563cSPaul Mackerras adr[i].size = rp[naddrc + nsizec - 1]; 5449b6b563cSPaul Mackerras } 5459b6b563cSPaul Mackerras ++i; 5469b6b563cSPaul Mackerras rp += naddrc + nsizec; 5479b6b563cSPaul Mackerras } 5489b6b563cSPaul Mackerras np->addrs = adr; 5499b6b563cSPaul Mackerras np->n_addrs = i; 5509b6b563cSPaul Mackerras (*mem_start) += i * sizeof(struct address_range); 5519b6b563cSPaul Mackerras } 5529b6b563cSPaul Mackerras 5539b6b563cSPaul Mackerras return 0; 5549b6b563cSPaul Mackerras } 5559b6b563cSPaul Mackerras 5569b6b563cSPaul Mackerras static int __devinit finish_node(struct device_node *np, 5579b6b563cSPaul Mackerras unsigned long *mem_start, 5589b6b563cSPaul Mackerras interpret_func *ifunc, 5599b6b563cSPaul Mackerras int naddrc, int nsizec, 5609b6b563cSPaul Mackerras int measure_only) 5619b6b563cSPaul Mackerras { 5629b6b563cSPaul Mackerras struct device_node *child; 5639b6b563cSPaul Mackerras int *ip, rc = 0; 5649b6b563cSPaul Mackerras 5659b6b563cSPaul Mackerras /* get the device addresses and interrupts */ 5669b6b563cSPaul Mackerras if (ifunc != NULL) 5679b6b563cSPaul Mackerras rc = ifunc(np, mem_start, naddrc, nsizec, measure_only); 5689b6b563cSPaul Mackerras if (rc) 5699b6b563cSPaul Mackerras goto out; 5709b6b563cSPaul Mackerras 5719b6b563cSPaul Mackerras rc = finish_node_interrupts(np, mem_start, measure_only); 5729b6b563cSPaul Mackerras if (rc) 5739b6b563cSPaul Mackerras goto out; 5749b6b563cSPaul Mackerras 5759b6b563cSPaul Mackerras /* Look for #address-cells and #size-cells properties. */ 5769b6b563cSPaul Mackerras ip = (int *) get_property(np, "#address-cells", NULL); 5779b6b563cSPaul Mackerras if (ip != NULL) 5789b6b563cSPaul Mackerras naddrc = *ip; 5799b6b563cSPaul Mackerras ip = (int *) get_property(np, "#size-cells", NULL); 5809b6b563cSPaul Mackerras if (ip != NULL) 5819b6b563cSPaul Mackerras nsizec = *ip; 5829b6b563cSPaul Mackerras 5839b6b563cSPaul Mackerras if (!strcmp(np->name, "device-tree") || np->parent == NULL) 5849b6b563cSPaul Mackerras ifunc = interpret_root_props; 5859b6b563cSPaul Mackerras else if (np->type == 0) 5869b6b563cSPaul Mackerras ifunc = NULL; 5879b6b563cSPaul Mackerras else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci")) 5889b6b563cSPaul Mackerras ifunc = interpret_pci_props; 5899b6b563cSPaul Mackerras else if (!strcmp(np->type, "dbdma")) 5909b6b563cSPaul Mackerras ifunc = interpret_dbdma_props; 5919b6b563cSPaul Mackerras else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props) 5929b6b563cSPaul Mackerras ifunc = interpret_macio_props; 5939b6b563cSPaul Mackerras else if (!strcmp(np->type, "isa")) 5949b6b563cSPaul Mackerras ifunc = interpret_isa_props; 5959b6b563cSPaul Mackerras else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3")) 5969b6b563cSPaul Mackerras ifunc = interpret_root_props; 5979b6b563cSPaul Mackerras else if (!((ifunc == interpret_dbdma_props 5989b6b563cSPaul Mackerras || ifunc == interpret_macio_props) 5999b6b563cSPaul Mackerras && (!strcmp(np->type, "escc") 6009b6b563cSPaul Mackerras || !strcmp(np->type, "media-bay")))) 6019b6b563cSPaul Mackerras ifunc = NULL; 6029b6b563cSPaul Mackerras 6039b6b563cSPaul Mackerras for (child = np->child; child != NULL; child = child->sibling) { 6049b6b563cSPaul Mackerras rc = finish_node(child, mem_start, ifunc, 6059b6b563cSPaul Mackerras naddrc, nsizec, measure_only); 6069b6b563cSPaul Mackerras if (rc) 6079b6b563cSPaul Mackerras goto out; 6089b6b563cSPaul Mackerras } 6099b6b563cSPaul Mackerras out: 6109b6b563cSPaul Mackerras return rc; 6119b6b563cSPaul Mackerras } 6129b6b563cSPaul Mackerras 6139b6b563cSPaul Mackerras static void __init scan_interrupt_controllers(void) 6149b6b563cSPaul Mackerras { 6159b6b563cSPaul Mackerras struct device_node *np; 6169b6b563cSPaul Mackerras int n = 0; 6179b6b563cSPaul Mackerras char *name, *ic; 6189b6b563cSPaul Mackerras int iclen; 6199b6b563cSPaul Mackerras 6209b6b563cSPaul Mackerras for (np = allnodes; np != NULL; np = np->allnext) { 6219b6b563cSPaul Mackerras ic = get_property(np, "interrupt-controller", &iclen); 6229b6b563cSPaul Mackerras name = get_property(np, "name", NULL); 6239b6b563cSPaul Mackerras /* checking iclen makes sure we don't get a false 6249b6b563cSPaul Mackerras match on /chosen.interrupt_controller */ 6259b6b563cSPaul Mackerras if ((name != NULL 6269b6b563cSPaul Mackerras && strcmp(name, "interrupt-controller") == 0) 6279b6b563cSPaul Mackerras || (ic != NULL && iclen == 0 6289b6b563cSPaul Mackerras && strcmp(name, "AppleKiwi"))) { 6299b6b563cSPaul Mackerras if (n == 0) 6309b6b563cSPaul Mackerras dflt_interrupt_controller = np; 6319b6b563cSPaul Mackerras ++n; 6329b6b563cSPaul Mackerras } 6339b6b563cSPaul Mackerras } 6349b6b563cSPaul Mackerras num_interrupt_controllers = n; 6359b6b563cSPaul Mackerras } 6369b6b563cSPaul Mackerras 6379b6b563cSPaul Mackerras /** 6389b6b563cSPaul Mackerras * finish_device_tree is called once things are running normally 6399b6b563cSPaul Mackerras * (i.e. with text and data mapped to the address they were linked at). 6409b6b563cSPaul Mackerras * It traverses the device tree and fills in some of the additional, 6419b6b563cSPaul Mackerras * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt 6429b6b563cSPaul Mackerras * mapping is also initialized at this point. 6439b6b563cSPaul Mackerras */ 6449b6b563cSPaul Mackerras void __init finish_device_tree(void) 6459b6b563cSPaul Mackerras { 6469b6b563cSPaul Mackerras unsigned long start, end, size = 0; 6479b6b563cSPaul Mackerras 6489b6b563cSPaul Mackerras DBG(" -> finish_device_tree\n"); 6499b6b563cSPaul Mackerras 6509b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 6519b6b563cSPaul Mackerras /* Initialize virtual IRQ map */ 6529b6b563cSPaul Mackerras virt_irq_init(); 6539b6b563cSPaul Mackerras #endif 6549b6b563cSPaul Mackerras scan_interrupt_controllers(); 6559b6b563cSPaul Mackerras 6569b6b563cSPaul Mackerras /* 6579b6b563cSPaul Mackerras * Finish device-tree (pre-parsing some properties etc...) 6589b6b563cSPaul Mackerras * We do this in 2 passes. One with "measure_only" set, which 6599b6b563cSPaul Mackerras * will only measure the amount of memory needed, then we can 6609b6b563cSPaul Mackerras * allocate that memory, and call finish_node again. However, 6619b6b563cSPaul Mackerras * we must be careful as most routines will fail nowadays when 6629b6b563cSPaul Mackerras * prom_alloc() returns 0, so we must make sure our first pass 6639b6b563cSPaul Mackerras * doesn't start at 0. We pre-initialize size to 16 for that 6649b6b563cSPaul Mackerras * reason and then remove those additional 16 bytes 6659b6b563cSPaul Mackerras */ 6669b6b563cSPaul Mackerras size = 16; 6679b6b563cSPaul Mackerras finish_node(allnodes, &size, NULL, 0, 0, 1); 6689b6b563cSPaul Mackerras size -= 16; 6699b6b563cSPaul Mackerras end = start = (unsigned long) __va(lmb_alloc(size, 128)); 6709b6b563cSPaul Mackerras finish_node(allnodes, &end, NULL, 0, 0, 0); 6719b6b563cSPaul Mackerras BUG_ON(end != start + size); 6729b6b563cSPaul Mackerras 6739b6b563cSPaul Mackerras DBG(" <- finish_device_tree\n"); 6749b6b563cSPaul Mackerras } 6759b6b563cSPaul Mackerras 6769b6b563cSPaul Mackerras static inline char *find_flat_dt_string(u32 offset) 6779b6b563cSPaul Mackerras { 6789b6b563cSPaul Mackerras return ((char *)initial_boot_params) + 6799b6b563cSPaul Mackerras initial_boot_params->off_dt_strings + offset; 6809b6b563cSPaul Mackerras } 6819b6b563cSPaul Mackerras 6829b6b563cSPaul Mackerras /** 6839b6b563cSPaul Mackerras * This function is used to scan the flattened device-tree, it is 6849b6b563cSPaul Mackerras * used to extract the memory informations at boot before we can 6859b6b563cSPaul Mackerras * unflatten the tree 6869b6b563cSPaul Mackerras */ 6879b6b563cSPaul Mackerras static int __init scan_flat_dt(int (*it)(unsigned long node, 6889b6b563cSPaul Mackerras const char *uname, int depth, 6899b6b563cSPaul Mackerras void *data), 6909b6b563cSPaul Mackerras void *data) 6919b6b563cSPaul Mackerras { 6929b6b563cSPaul Mackerras unsigned long p = ((unsigned long)initial_boot_params) + 6939b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 6949b6b563cSPaul Mackerras int rc = 0; 6959b6b563cSPaul Mackerras int depth = -1; 6969b6b563cSPaul Mackerras 6979b6b563cSPaul Mackerras do { 6989b6b563cSPaul Mackerras u32 tag = *((u32 *)p); 6999b6b563cSPaul Mackerras char *pathp; 7009b6b563cSPaul Mackerras 7019b6b563cSPaul Mackerras p += 4; 7029b6b563cSPaul Mackerras if (tag == OF_DT_END_NODE) { 7039b6b563cSPaul Mackerras depth --; 7049b6b563cSPaul Mackerras continue; 7059b6b563cSPaul Mackerras } 7069b6b563cSPaul Mackerras if (tag == OF_DT_NOP) 7079b6b563cSPaul Mackerras continue; 7089b6b563cSPaul Mackerras if (tag == OF_DT_END) 7099b6b563cSPaul Mackerras break; 7109b6b563cSPaul Mackerras if (tag == OF_DT_PROP) { 7119b6b563cSPaul Mackerras u32 sz = *((u32 *)p); 7129b6b563cSPaul Mackerras p += 8; 7139b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 7149b6b563cSPaul Mackerras p = _ALIGN(p, sz >= 8 ? 8 : 4); 7159b6b563cSPaul Mackerras p += sz; 7169b6b563cSPaul Mackerras p = _ALIGN(p, 4); 7179b6b563cSPaul Mackerras continue; 7189b6b563cSPaul Mackerras } 7199b6b563cSPaul Mackerras if (tag != OF_DT_BEGIN_NODE) { 7209b6b563cSPaul Mackerras printk(KERN_WARNING "Invalid tag %x scanning flattened" 7219b6b563cSPaul Mackerras " device tree !\n", tag); 7229b6b563cSPaul Mackerras return -EINVAL; 7239b6b563cSPaul Mackerras } 7249b6b563cSPaul Mackerras depth++; 7259b6b563cSPaul Mackerras pathp = (char *)p; 7269b6b563cSPaul Mackerras p = _ALIGN(p + strlen(pathp) + 1, 4); 7279b6b563cSPaul Mackerras if ((*pathp) == '/') { 7289b6b563cSPaul Mackerras char *lp, *np; 7299b6b563cSPaul Mackerras for (lp = NULL, np = pathp; *np; np++) 7309b6b563cSPaul Mackerras if ((*np) == '/') 7319b6b563cSPaul Mackerras lp = np+1; 7329b6b563cSPaul Mackerras if (lp != NULL) 7339b6b563cSPaul Mackerras pathp = lp; 7349b6b563cSPaul Mackerras } 7359b6b563cSPaul Mackerras rc = it(p, pathp, depth, data); 7369b6b563cSPaul Mackerras if (rc != 0) 7379b6b563cSPaul Mackerras break; 7389b6b563cSPaul Mackerras } while(1); 7399b6b563cSPaul Mackerras 7409b6b563cSPaul Mackerras return rc; 7419b6b563cSPaul Mackerras } 7429b6b563cSPaul Mackerras 7439b6b563cSPaul Mackerras /** 7449b6b563cSPaul Mackerras * This function can be used within scan_flattened_dt callback to get 7459b6b563cSPaul Mackerras * access to properties 7469b6b563cSPaul Mackerras */ 7479b6b563cSPaul Mackerras static void* __init get_flat_dt_prop(unsigned long node, const char *name, 7489b6b563cSPaul Mackerras unsigned long *size) 7499b6b563cSPaul Mackerras { 7509b6b563cSPaul Mackerras unsigned long p = node; 7519b6b563cSPaul Mackerras 7529b6b563cSPaul Mackerras do { 7539b6b563cSPaul Mackerras u32 tag = *((u32 *)p); 7549b6b563cSPaul Mackerras u32 sz, noff; 7559b6b563cSPaul Mackerras const char *nstr; 7569b6b563cSPaul Mackerras 7579b6b563cSPaul Mackerras p += 4; 7589b6b563cSPaul Mackerras if (tag == OF_DT_NOP) 7599b6b563cSPaul Mackerras continue; 7609b6b563cSPaul Mackerras if (tag != OF_DT_PROP) 7619b6b563cSPaul Mackerras return NULL; 7629b6b563cSPaul Mackerras 7639b6b563cSPaul Mackerras sz = *((u32 *)p); 7649b6b563cSPaul Mackerras noff = *((u32 *)(p + 4)); 7659b6b563cSPaul Mackerras p += 8; 7669b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 7679b6b563cSPaul Mackerras p = _ALIGN(p, sz >= 8 ? 8 : 4); 7689b6b563cSPaul Mackerras 7699b6b563cSPaul Mackerras nstr = find_flat_dt_string(noff); 7709b6b563cSPaul Mackerras if (nstr == NULL) { 7719b6b563cSPaul Mackerras printk(KERN_WARNING "Can't find property index" 7729b6b563cSPaul Mackerras " name !\n"); 7739b6b563cSPaul Mackerras return NULL; 7749b6b563cSPaul Mackerras } 7759b6b563cSPaul Mackerras if (strcmp(name, nstr) == 0) { 7769b6b563cSPaul Mackerras if (size) 7779b6b563cSPaul Mackerras *size = sz; 7789b6b563cSPaul Mackerras return (void *)p; 7799b6b563cSPaul Mackerras } 7809b6b563cSPaul Mackerras p += sz; 7819b6b563cSPaul Mackerras p = _ALIGN(p, 4); 7829b6b563cSPaul Mackerras } while(1); 7839b6b563cSPaul Mackerras } 7849b6b563cSPaul Mackerras 7859b6b563cSPaul Mackerras static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size, 7869b6b563cSPaul Mackerras unsigned long align) 7879b6b563cSPaul Mackerras { 7889b6b563cSPaul Mackerras void *res; 7899b6b563cSPaul Mackerras 7909b6b563cSPaul Mackerras *mem = _ALIGN(*mem, align); 7919b6b563cSPaul Mackerras res = (void *)*mem; 7929b6b563cSPaul Mackerras *mem += size; 7939b6b563cSPaul Mackerras 7949b6b563cSPaul Mackerras return res; 7959b6b563cSPaul Mackerras } 7969b6b563cSPaul Mackerras 7979b6b563cSPaul Mackerras static unsigned long __init unflatten_dt_node(unsigned long mem, 7989b6b563cSPaul Mackerras unsigned long *p, 7999b6b563cSPaul Mackerras struct device_node *dad, 8009b6b563cSPaul Mackerras struct device_node ***allnextpp, 8019b6b563cSPaul Mackerras unsigned long fpsize) 8029b6b563cSPaul Mackerras { 8039b6b563cSPaul Mackerras struct device_node *np; 8049b6b563cSPaul Mackerras struct property *pp, **prev_pp = NULL; 8059b6b563cSPaul Mackerras char *pathp; 8069b6b563cSPaul Mackerras u32 tag; 8079b6b563cSPaul Mackerras unsigned int l, allocl; 8089b6b563cSPaul Mackerras int has_name = 0; 8099b6b563cSPaul Mackerras int new_format = 0; 8109b6b563cSPaul Mackerras 8119b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 8129b6b563cSPaul Mackerras if (tag != OF_DT_BEGIN_NODE) { 8139b6b563cSPaul Mackerras printk("Weird tag at start of node: %x\n", tag); 8149b6b563cSPaul Mackerras return mem; 8159b6b563cSPaul Mackerras } 8169b6b563cSPaul Mackerras *p += 4; 8179b6b563cSPaul Mackerras pathp = (char *)*p; 8189b6b563cSPaul Mackerras l = allocl = strlen(pathp) + 1; 8199b6b563cSPaul Mackerras *p = _ALIGN(*p + l, 4); 8209b6b563cSPaul Mackerras 8219b6b563cSPaul Mackerras /* version 0x10 has a more compact unit name here instead of the full 8229b6b563cSPaul Mackerras * path. we accumulate the full path size using "fpsize", we'll rebuild 8239b6b563cSPaul Mackerras * it later. We detect this because the first character of the name is 8249b6b563cSPaul Mackerras * not '/'. 8259b6b563cSPaul Mackerras */ 8269b6b563cSPaul Mackerras if ((*pathp) != '/') { 8279b6b563cSPaul Mackerras new_format = 1; 8289b6b563cSPaul Mackerras if (fpsize == 0) { 8299b6b563cSPaul Mackerras /* root node: special case. fpsize accounts for path 8309b6b563cSPaul Mackerras * plus terminating zero. root node only has '/', so 8319b6b563cSPaul Mackerras * fpsize should be 2, but we want to avoid the first 8329b6b563cSPaul Mackerras * level nodes to have two '/' so we use fpsize 1 here 8339b6b563cSPaul Mackerras */ 8349b6b563cSPaul Mackerras fpsize = 1; 8359b6b563cSPaul Mackerras allocl = 2; 8369b6b563cSPaul Mackerras } else { 8379b6b563cSPaul Mackerras /* account for '/' and path size minus terminal 0 8389b6b563cSPaul Mackerras * already in 'l' 8399b6b563cSPaul Mackerras */ 8409b6b563cSPaul Mackerras fpsize += l; 8419b6b563cSPaul Mackerras allocl = fpsize; 8429b6b563cSPaul Mackerras } 8439b6b563cSPaul Mackerras } 8449b6b563cSPaul Mackerras 8459b6b563cSPaul Mackerras 8469b6b563cSPaul Mackerras np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl, 8479b6b563cSPaul Mackerras __alignof__(struct device_node)); 8489b6b563cSPaul Mackerras if (allnextpp) { 8499b6b563cSPaul Mackerras memset(np, 0, sizeof(*np)); 8509b6b563cSPaul Mackerras np->full_name = ((char*)np) + sizeof(struct device_node); 8519b6b563cSPaul Mackerras if (new_format) { 8529b6b563cSPaul Mackerras char *p = np->full_name; 8539b6b563cSPaul Mackerras /* rebuild full path for new format */ 8549b6b563cSPaul Mackerras if (dad && dad->parent) { 8559b6b563cSPaul Mackerras strcpy(p, dad->full_name); 8569b6b563cSPaul Mackerras #ifdef DEBUG 8579b6b563cSPaul Mackerras if ((strlen(p) + l + 1) != allocl) { 8589b6b563cSPaul Mackerras DBG("%s: p: %d, l: %d, a: %d\n", 8599b6b563cSPaul Mackerras pathp, strlen(p), l, allocl); 8609b6b563cSPaul Mackerras } 8619b6b563cSPaul Mackerras #endif 8629b6b563cSPaul Mackerras p += strlen(p); 8639b6b563cSPaul Mackerras } 8649b6b563cSPaul Mackerras *(p++) = '/'; 8659b6b563cSPaul Mackerras memcpy(p, pathp, l); 8669b6b563cSPaul Mackerras } else 8679b6b563cSPaul Mackerras memcpy(np->full_name, pathp, l); 8689b6b563cSPaul Mackerras prev_pp = &np->properties; 8699b6b563cSPaul Mackerras **allnextpp = np; 8709b6b563cSPaul Mackerras *allnextpp = &np->allnext; 8719b6b563cSPaul Mackerras if (dad != NULL) { 8729b6b563cSPaul Mackerras np->parent = dad; 8739b6b563cSPaul Mackerras /* we temporarily use the next field as `last_child'*/ 8749b6b563cSPaul Mackerras if (dad->next == 0) 8759b6b563cSPaul Mackerras dad->child = np; 8769b6b563cSPaul Mackerras else 8779b6b563cSPaul Mackerras dad->next->sibling = np; 8789b6b563cSPaul Mackerras dad->next = np; 8799b6b563cSPaul Mackerras } 8809b6b563cSPaul Mackerras kref_init(&np->kref); 8819b6b563cSPaul Mackerras } 8829b6b563cSPaul Mackerras while(1) { 8839b6b563cSPaul Mackerras u32 sz, noff; 8849b6b563cSPaul Mackerras char *pname; 8859b6b563cSPaul Mackerras 8869b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 8879b6b563cSPaul Mackerras if (tag == OF_DT_NOP) { 8889b6b563cSPaul Mackerras *p += 4; 8899b6b563cSPaul Mackerras continue; 8909b6b563cSPaul Mackerras } 8919b6b563cSPaul Mackerras if (tag != OF_DT_PROP) 8929b6b563cSPaul Mackerras break; 8939b6b563cSPaul Mackerras *p += 4; 8949b6b563cSPaul Mackerras sz = *((u32 *)(*p)); 8959b6b563cSPaul Mackerras noff = *((u32 *)((*p) + 4)); 8969b6b563cSPaul Mackerras *p += 8; 8979b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 8989b6b563cSPaul Mackerras *p = _ALIGN(*p, sz >= 8 ? 8 : 4); 8999b6b563cSPaul Mackerras 9009b6b563cSPaul Mackerras pname = find_flat_dt_string(noff); 9019b6b563cSPaul Mackerras if (pname == NULL) { 9029b6b563cSPaul Mackerras printk("Can't find property name in list !\n"); 9039b6b563cSPaul Mackerras break; 9049b6b563cSPaul Mackerras } 9059b6b563cSPaul Mackerras if (strcmp(pname, "name") == 0) 9069b6b563cSPaul Mackerras has_name = 1; 9079b6b563cSPaul Mackerras l = strlen(pname) + 1; 9089b6b563cSPaul Mackerras pp = unflatten_dt_alloc(&mem, sizeof(struct property), 9099b6b563cSPaul Mackerras __alignof__(struct property)); 9109b6b563cSPaul Mackerras if (allnextpp) { 9119b6b563cSPaul Mackerras if (strcmp(pname, "linux,phandle") == 0) { 9129b6b563cSPaul Mackerras np->node = *((u32 *)*p); 9139b6b563cSPaul Mackerras if (np->linux_phandle == 0) 9149b6b563cSPaul Mackerras np->linux_phandle = np->node; 9159b6b563cSPaul Mackerras } 9169b6b563cSPaul Mackerras if (strcmp(pname, "ibm,phandle") == 0) 9179b6b563cSPaul Mackerras np->linux_phandle = *((u32 *)*p); 9189b6b563cSPaul Mackerras pp->name = pname; 9199b6b563cSPaul Mackerras pp->length = sz; 9209b6b563cSPaul Mackerras pp->value = (void *)*p; 9219b6b563cSPaul Mackerras *prev_pp = pp; 9229b6b563cSPaul Mackerras prev_pp = &pp->next; 9239b6b563cSPaul Mackerras } 9249b6b563cSPaul Mackerras *p = _ALIGN((*p) + sz, 4); 9259b6b563cSPaul Mackerras } 9269b6b563cSPaul Mackerras /* with version 0x10 we may not have the name property, recreate 9279b6b563cSPaul Mackerras * it here from the unit name if absent 9289b6b563cSPaul Mackerras */ 9299b6b563cSPaul Mackerras if (!has_name) { 9309b6b563cSPaul Mackerras char *p = pathp, *ps = pathp, *pa = NULL; 9319b6b563cSPaul Mackerras int sz; 9329b6b563cSPaul Mackerras 9339b6b563cSPaul Mackerras while (*p) { 9349b6b563cSPaul Mackerras if ((*p) == '@') 9359b6b563cSPaul Mackerras pa = p; 9369b6b563cSPaul Mackerras if ((*p) == '/') 9379b6b563cSPaul Mackerras ps = p + 1; 9389b6b563cSPaul Mackerras p++; 9399b6b563cSPaul Mackerras } 9409b6b563cSPaul Mackerras if (pa < ps) 9419b6b563cSPaul Mackerras pa = p; 9429b6b563cSPaul Mackerras sz = (pa - ps) + 1; 9439b6b563cSPaul Mackerras pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz, 9449b6b563cSPaul Mackerras __alignof__(struct property)); 9459b6b563cSPaul Mackerras if (allnextpp) { 9469b6b563cSPaul Mackerras pp->name = "name"; 9479b6b563cSPaul Mackerras pp->length = sz; 9489b6b563cSPaul Mackerras pp->value = (unsigned char *)(pp + 1); 9499b6b563cSPaul Mackerras *prev_pp = pp; 9509b6b563cSPaul Mackerras prev_pp = &pp->next; 9519b6b563cSPaul Mackerras memcpy(pp->value, ps, sz - 1); 9529b6b563cSPaul Mackerras ((char *)pp->value)[sz - 1] = 0; 9539b6b563cSPaul Mackerras DBG("fixed up name for %s -> %s\n", pathp, pp->value); 9549b6b563cSPaul Mackerras } 9559b6b563cSPaul Mackerras } 9569b6b563cSPaul Mackerras if (allnextpp) { 9579b6b563cSPaul Mackerras *prev_pp = NULL; 9589b6b563cSPaul Mackerras np->name = get_property(np, "name", NULL); 9599b6b563cSPaul Mackerras np->type = get_property(np, "device_type", NULL); 9609b6b563cSPaul Mackerras 9619b6b563cSPaul Mackerras if (!np->name) 9629b6b563cSPaul Mackerras np->name = "<NULL>"; 9639b6b563cSPaul Mackerras if (!np->type) 9649b6b563cSPaul Mackerras np->type = "<NULL>"; 9659b6b563cSPaul Mackerras } 9669b6b563cSPaul Mackerras while (tag == OF_DT_BEGIN_NODE) { 9679b6b563cSPaul Mackerras mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize); 9689b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 9699b6b563cSPaul Mackerras } 9709b6b563cSPaul Mackerras if (tag != OF_DT_END_NODE) { 9719b6b563cSPaul Mackerras printk("Weird tag at end of node: %x\n", tag); 9729b6b563cSPaul Mackerras return mem; 9739b6b563cSPaul Mackerras } 9749b6b563cSPaul Mackerras *p += 4; 9759b6b563cSPaul Mackerras return mem; 9769b6b563cSPaul Mackerras } 9779b6b563cSPaul Mackerras 9789b6b563cSPaul Mackerras 9799b6b563cSPaul Mackerras /** 9809b6b563cSPaul Mackerras * unflattens the device-tree passed by the firmware, creating the 9819b6b563cSPaul Mackerras * tree of struct device_node. It also fills the "name" and "type" 9829b6b563cSPaul Mackerras * pointers of the nodes so the normal device-tree walking functions 9839b6b563cSPaul Mackerras * can be used (this used to be done by finish_device_tree) 9849b6b563cSPaul Mackerras */ 9859b6b563cSPaul Mackerras void __init unflatten_device_tree(void) 9869b6b563cSPaul Mackerras { 9879b6b563cSPaul Mackerras unsigned long start, mem, size; 9889b6b563cSPaul Mackerras struct device_node **allnextp = &allnodes; 9899b6b563cSPaul Mackerras char *p = NULL; 9909b6b563cSPaul Mackerras int l = 0; 9919b6b563cSPaul Mackerras 9929b6b563cSPaul Mackerras DBG(" -> unflatten_device_tree()\n"); 9939b6b563cSPaul Mackerras 9949b6b563cSPaul Mackerras /* First pass, scan for size */ 9959b6b563cSPaul Mackerras start = ((unsigned long)initial_boot_params) + 9969b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 9979b6b563cSPaul Mackerras size = unflatten_dt_node(0, &start, NULL, NULL, 0); 9989b6b563cSPaul Mackerras size = (size | 3) + 1; 9999b6b563cSPaul Mackerras 10009b6b563cSPaul Mackerras DBG(" size is %lx, allocating...\n", size); 10019b6b563cSPaul Mackerras 10029b6b563cSPaul Mackerras /* Allocate memory for the expanded device tree */ 10039b6b563cSPaul Mackerras mem = lmb_alloc(size + 4, __alignof__(struct device_node)); 10049b6b563cSPaul Mackerras if (!mem) { 10059b6b563cSPaul Mackerras DBG("Couldn't allocate memory with lmb_alloc()!\n"); 10069b6b563cSPaul Mackerras panic("Couldn't allocate memory with lmb_alloc()!\n"); 10079b6b563cSPaul Mackerras } 10089b6b563cSPaul Mackerras mem = (unsigned long) __va(mem); 10099b6b563cSPaul Mackerras 10109b6b563cSPaul Mackerras ((u32 *)mem)[size / 4] = 0xdeadbeef; 10119b6b563cSPaul Mackerras 10129b6b563cSPaul Mackerras DBG(" unflattening %lx...\n", mem); 10139b6b563cSPaul Mackerras 10149b6b563cSPaul Mackerras /* Second pass, do actual unflattening */ 10159b6b563cSPaul Mackerras start = ((unsigned long)initial_boot_params) + 10169b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 10179b6b563cSPaul Mackerras unflatten_dt_node(mem, &start, NULL, &allnextp, 0); 10189b6b563cSPaul Mackerras if (*((u32 *)start) != OF_DT_END) 10199b6b563cSPaul Mackerras printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start)); 10209b6b563cSPaul Mackerras if (((u32 *)mem)[size / 4] != 0xdeadbeef) 10219b6b563cSPaul Mackerras printk(KERN_WARNING "End of tree marker overwritten: %08x\n", 10229b6b563cSPaul Mackerras ((u32 *)mem)[size / 4] ); 10239b6b563cSPaul Mackerras *allnextp = NULL; 10249b6b563cSPaul Mackerras 10259b6b563cSPaul Mackerras /* Get pointer to OF "/chosen" node for use everywhere */ 10269b6b563cSPaul Mackerras of_chosen = of_find_node_by_path("/chosen"); 10279b6b563cSPaul Mackerras 10289b6b563cSPaul Mackerras /* Retreive command line */ 10299b6b563cSPaul Mackerras if (of_chosen != NULL) { 10309b6b563cSPaul Mackerras p = (char *)get_property(of_chosen, "bootargs", &l); 10319b6b563cSPaul Mackerras if (p != NULL && l > 0) 10329b6b563cSPaul Mackerras strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE)); 10339b6b563cSPaul Mackerras } 10349b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE 10359b6b563cSPaul Mackerras if (l == 0 || (l == 1 && (*p) == 0)) 10369b6b563cSPaul Mackerras strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); 10379b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */ 10389b6b563cSPaul Mackerras 10399b6b563cSPaul Mackerras DBG("Command line is: %s\n", cmd_line); 10409b6b563cSPaul Mackerras 10419b6b563cSPaul Mackerras DBG(" <- unflatten_device_tree()\n"); 10429b6b563cSPaul Mackerras } 10439b6b563cSPaul Mackerras 10449b6b563cSPaul Mackerras 10459b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node, 10469b6b563cSPaul Mackerras const char *uname, int depth, void *data) 10479b6b563cSPaul Mackerras { 10489b6b563cSPaul Mackerras char *type = get_flat_dt_prop(node, "device_type", NULL); 10499b6b563cSPaul Mackerras u32 *prop; 10509b6b563cSPaul Mackerras unsigned long size = 0; 10519b6b563cSPaul Mackerras 10529b6b563cSPaul Mackerras /* We are scanning "cpu" nodes only */ 10539b6b563cSPaul Mackerras if (type == NULL || strcmp(type, "cpu") != 0) 10549b6b563cSPaul Mackerras return 0; 10559b6b563cSPaul Mackerras 10569b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 10579b6b563cSPaul Mackerras /* On LPAR, look for the first ibm,pft-size property for the hash table size 10589b6b563cSPaul Mackerras */ 10599b6b563cSPaul Mackerras if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) { 10609b6b563cSPaul Mackerras u32 *pft_size; 10619b6b563cSPaul Mackerras pft_size = get_flat_dt_prop(node, "ibm,pft-size", NULL); 10629b6b563cSPaul Mackerras if (pft_size != NULL) { 10639b6b563cSPaul Mackerras /* pft_size[0] is the NUMA CEC cookie */ 10649b6b563cSPaul Mackerras ppc64_pft_size = pft_size[1]; 10659b6b563cSPaul Mackerras } 10669b6b563cSPaul Mackerras } 10679b6b563cSPaul Mackerras #endif 10689b6b563cSPaul Mackerras 10699b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 10709b6b563cSPaul Mackerras if (initial_boot_params && initial_boot_params->version >= 2) { 10719b6b563cSPaul Mackerras /* version 2 of the kexec param format adds the phys cpuid 10729b6b563cSPaul Mackerras * of booted proc. 10739b6b563cSPaul Mackerras */ 10749b6b563cSPaul Mackerras boot_cpuid_phys = initial_boot_params->boot_cpuid_phys; 10759b6b563cSPaul Mackerras boot_cpuid = 0; 10769b6b563cSPaul Mackerras } else { 10779b6b563cSPaul Mackerras /* Check if it's the boot-cpu, set it's hw index in paca now */ 10789b6b563cSPaul Mackerras if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) { 1079*40ef8cbcSPaul Mackerras prop = get_flat_dt_prop(node, "reg", NULL); 10809b6b563cSPaul Mackerras set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop); 10819b6b563cSPaul Mackerras boot_cpuid_phys = get_hard_smp_processor_id(0); 10829b6b563cSPaul Mackerras } 10839b6b563cSPaul Mackerras } 10849b6b563cSPaul Mackerras #endif 10859b6b563cSPaul Mackerras 10869b6b563cSPaul Mackerras #ifdef CONFIG_ALTIVEC 10879b6b563cSPaul Mackerras /* Check if we have a VMX and eventually update CPU features */ 10889b6b563cSPaul Mackerras prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", &size); 10899b6b563cSPaul Mackerras if (prop && (*prop) > 0) { 10909b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; 10919b6b563cSPaul Mackerras cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; 10929b6b563cSPaul Mackerras } 10939b6b563cSPaul Mackerras 10949b6b563cSPaul Mackerras /* Same goes for Apple's "altivec" property */ 10959b6b563cSPaul Mackerras prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL); 10969b6b563cSPaul Mackerras if (prop) { 10979b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; 10989b6b563cSPaul Mackerras cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; 10999b6b563cSPaul Mackerras } 11009b6b563cSPaul Mackerras #endif /* CONFIG_ALTIVEC */ 11019b6b563cSPaul Mackerras 11029b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 11039b6b563cSPaul Mackerras /* 11049b6b563cSPaul Mackerras * Check for an SMT capable CPU and set the CPU feature. We do 11059b6b563cSPaul Mackerras * this by looking at the size of the ibm,ppc-interrupt-server#s 11069b6b563cSPaul Mackerras * property 11079b6b563cSPaul Mackerras */ 11089b6b563cSPaul Mackerras prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", 11099b6b563cSPaul Mackerras &size); 11109b6b563cSPaul Mackerras cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT; 11119b6b563cSPaul Mackerras if (prop && ((size / sizeof(u32)) > 1)) 11129b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_SMT; 11139b6b563cSPaul Mackerras #endif 11149b6b563cSPaul Mackerras 11159b6b563cSPaul Mackerras return 0; 11169b6b563cSPaul Mackerras } 11179b6b563cSPaul Mackerras 11189b6b563cSPaul Mackerras static int __init early_init_dt_scan_chosen(unsigned long node, 11199b6b563cSPaul Mackerras const char *uname, int depth, void *data) 11209b6b563cSPaul Mackerras { 11219b6b563cSPaul Mackerras u32 *prop; 11229b6b563cSPaul Mackerras unsigned long *lprop; 11239b6b563cSPaul Mackerras 11249b6b563cSPaul Mackerras DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname); 11259b6b563cSPaul Mackerras 11269b6b563cSPaul Mackerras if (depth != 1 || strcmp(uname, "chosen") != 0) 11279b6b563cSPaul Mackerras return 0; 11289b6b563cSPaul Mackerras 11299b6b563cSPaul Mackerras /* get platform type */ 11309b6b563cSPaul Mackerras prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL); 11319b6b563cSPaul Mackerras if (prop == NULL) 11329b6b563cSPaul Mackerras return 0; 11339b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 11349b6b563cSPaul Mackerras systemcfg->platform = *prop; 11359b6b563cSPaul Mackerras #else 11369b6b563cSPaul Mackerras _machine = *prop; 11379b6b563cSPaul Mackerras #endif 11389b6b563cSPaul Mackerras 11399b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 11409b6b563cSPaul Mackerras /* check if iommu is forced on or off */ 11419b6b563cSPaul Mackerras if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL) 11429b6b563cSPaul Mackerras iommu_is_off = 1; 11439b6b563cSPaul Mackerras if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL) 11449b6b563cSPaul Mackerras iommu_force_on = 1; 11459b6b563cSPaul Mackerras #endif 11469b6b563cSPaul Mackerras 11479b6b563cSPaul Mackerras lprop = get_flat_dt_prop(node, "linux,memory-limit", NULL); 11489b6b563cSPaul Mackerras if (lprop) 11499b6b563cSPaul Mackerras memory_limit = *lprop; 11509b6b563cSPaul Mackerras 11519b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 11529b6b563cSPaul Mackerras lprop = get_flat_dt_prop(node, "linux,tce-alloc-start", NULL); 11539b6b563cSPaul Mackerras if (lprop) 11549b6b563cSPaul Mackerras tce_alloc_start = *lprop; 11559b6b563cSPaul Mackerras lprop = get_flat_dt_prop(node, "linux,tce-alloc-end", NULL); 11569b6b563cSPaul Mackerras if (lprop) 11579b6b563cSPaul Mackerras tce_alloc_end = *lprop; 11589b6b563cSPaul Mackerras #endif 11599b6b563cSPaul Mackerras 11609b6b563cSPaul Mackerras #ifdef CONFIG_PPC_RTAS 11619b6b563cSPaul Mackerras /* To help early debugging via the front panel, we retreive a minimal 11629b6b563cSPaul Mackerras * set of RTAS infos now if available 11639b6b563cSPaul Mackerras */ 11649b6b563cSPaul Mackerras { 11659b6b563cSPaul Mackerras u64 *basep, *entryp; 11669b6b563cSPaul Mackerras 11679b6b563cSPaul Mackerras basep = get_flat_dt_prop(node, "linux,rtas-base", NULL); 11689b6b563cSPaul Mackerras entryp = get_flat_dt_prop(node, "linux,rtas-entry", NULL); 11699b6b563cSPaul Mackerras prop = get_flat_dt_prop(node, "linux,rtas-size", NULL); 11709b6b563cSPaul Mackerras if (basep && entryp && prop) { 11719b6b563cSPaul Mackerras rtas.base = *basep; 11729b6b563cSPaul Mackerras rtas.entry = *entryp; 11739b6b563cSPaul Mackerras rtas.size = *prop; 11749b6b563cSPaul Mackerras } 11759b6b563cSPaul Mackerras } 11769b6b563cSPaul Mackerras #endif /* CONFIG_PPC_RTAS */ 11779b6b563cSPaul Mackerras 11789b6b563cSPaul Mackerras /* break now */ 11799b6b563cSPaul Mackerras return 1; 11809b6b563cSPaul Mackerras } 11819b6b563cSPaul Mackerras 11829b6b563cSPaul Mackerras static int __init early_init_dt_scan_root(unsigned long node, 11839b6b563cSPaul Mackerras const char *uname, int depth, void *data) 11849b6b563cSPaul Mackerras { 11859b6b563cSPaul Mackerras u32 *prop; 11869b6b563cSPaul Mackerras 11879b6b563cSPaul Mackerras if (depth != 0) 11889b6b563cSPaul Mackerras return 0; 11899b6b563cSPaul Mackerras 11909b6b563cSPaul Mackerras prop = get_flat_dt_prop(node, "#size-cells", NULL); 11919b6b563cSPaul Mackerras dt_root_size_cells = (prop == NULL) ? 1 : *prop; 11929b6b563cSPaul Mackerras DBG("dt_root_size_cells = %x\n", dt_root_size_cells); 11939b6b563cSPaul Mackerras 11949b6b563cSPaul Mackerras prop = get_flat_dt_prop(node, "#address-cells", NULL); 11959b6b563cSPaul Mackerras dt_root_addr_cells = (prop == NULL) ? 2 : *prop; 11969b6b563cSPaul Mackerras DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells); 11979b6b563cSPaul Mackerras 11989b6b563cSPaul Mackerras /* break now */ 11999b6b563cSPaul Mackerras return 1; 12009b6b563cSPaul Mackerras } 12019b6b563cSPaul Mackerras 12029b6b563cSPaul Mackerras static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp) 12039b6b563cSPaul Mackerras { 12049b6b563cSPaul Mackerras cell_t *p = *cellp; 12059b6b563cSPaul Mackerras unsigned long r; 12069b6b563cSPaul Mackerras 12079b6b563cSPaul Mackerras /* Ignore more than 2 cells */ 12089b6b563cSPaul Mackerras while (s > sizeof(unsigned long) / 4) { 12099b6b563cSPaul Mackerras p++; 12109b6b563cSPaul Mackerras s--; 12119b6b563cSPaul Mackerras } 12129b6b563cSPaul Mackerras r = *p++; 12139b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 12149b6b563cSPaul Mackerras if (s > 1) { 12159b6b563cSPaul Mackerras r <<= 32; 12169b6b563cSPaul Mackerras r |= *(p++); 12179b6b563cSPaul Mackerras s--; 12189b6b563cSPaul Mackerras } 12199b6b563cSPaul Mackerras #endif 12209b6b563cSPaul Mackerras 12219b6b563cSPaul Mackerras *cellp = p; 12229b6b563cSPaul Mackerras return r; 12239b6b563cSPaul Mackerras } 12249b6b563cSPaul Mackerras 12259b6b563cSPaul Mackerras 12269b6b563cSPaul Mackerras static int __init early_init_dt_scan_memory(unsigned long node, 12279b6b563cSPaul Mackerras const char *uname, int depth, void *data) 12289b6b563cSPaul Mackerras { 12299b6b563cSPaul Mackerras char *type = get_flat_dt_prop(node, "device_type", NULL); 12309b6b563cSPaul Mackerras cell_t *reg, *endp; 12319b6b563cSPaul Mackerras unsigned long l; 12329b6b563cSPaul Mackerras 12339b6b563cSPaul Mackerras /* We are scanning "memory" nodes only */ 12349b6b563cSPaul Mackerras if (type == NULL || strcmp(type, "memory") != 0) 12359b6b563cSPaul Mackerras return 0; 12369b6b563cSPaul Mackerras 12379b6b563cSPaul Mackerras reg = (cell_t *)get_flat_dt_prop(node, "reg", &l); 12389b6b563cSPaul Mackerras if (reg == NULL) 12399b6b563cSPaul Mackerras return 0; 12409b6b563cSPaul Mackerras 12419b6b563cSPaul Mackerras endp = reg + (l / sizeof(cell_t)); 12429b6b563cSPaul Mackerras 12439b6b563cSPaul Mackerras DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n", 12449b6b563cSPaul Mackerras uname, l, reg[0], reg[1], reg[2], reg[3]); 12459b6b563cSPaul Mackerras 12469b6b563cSPaul Mackerras while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { 12479b6b563cSPaul Mackerras unsigned long base, size; 12489b6b563cSPaul Mackerras 12499b6b563cSPaul Mackerras base = dt_mem_next_cell(dt_root_addr_cells, ®); 12509b6b563cSPaul Mackerras size = dt_mem_next_cell(dt_root_size_cells, ®); 12519b6b563cSPaul Mackerras 12529b6b563cSPaul Mackerras if (size == 0) 12539b6b563cSPaul Mackerras continue; 12549b6b563cSPaul Mackerras DBG(" - %lx , %lx\n", base, size); 12559b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 12569b6b563cSPaul Mackerras if (iommu_is_off) { 12579b6b563cSPaul Mackerras if (base >= 0x80000000ul) 12589b6b563cSPaul Mackerras continue; 12599b6b563cSPaul Mackerras if ((base + size) > 0x80000000ul) 12609b6b563cSPaul Mackerras size = 0x80000000ul - base; 12619b6b563cSPaul Mackerras } 12629b6b563cSPaul Mackerras #endif 12639b6b563cSPaul Mackerras lmb_add(base, size); 12649b6b563cSPaul Mackerras } 12659b6b563cSPaul Mackerras return 0; 12669b6b563cSPaul Mackerras } 12679b6b563cSPaul Mackerras 12689b6b563cSPaul Mackerras static void __init early_reserve_mem(void) 12699b6b563cSPaul Mackerras { 12709b6b563cSPaul Mackerras unsigned long base, size; 12719b6b563cSPaul Mackerras unsigned long *reserve_map; 12729b6b563cSPaul Mackerras 12739b6b563cSPaul Mackerras reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) + 12749b6b563cSPaul Mackerras initial_boot_params->off_mem_rsvmap); 12759b6b563cSPaul Mackerras while (1) { 12769b6b563cSPaul Mackerras base = *(reserve_map++); 12779b6b563cSPaul Mackerras size = *(reserve_map++); 12789b6b563cSPaul Mackerras if (size == 0) 12799b6b563cSPaul Mackerras break; 12809b6b563cSPaul Mackerras DBG("reserving: %lx -> %lx\n", base, size); 12819b6b563cSPaul Mackerras lmb_reserve(base, size); 12829b6b563cSPaul Mackerras } 12839b6b563cSPaul Mackerras 12849b6b563cSPaul Mackerras #if 0 12859b6b563cSPaul Mackerras DBG("memory reserved, lmbs :\n"); 12869b6b563cSPaul Mackerras lmb_dump_all(); 12879b6b563cSPaul Mackerras #endif 12889b6b563cSPaul Mackerras } 12899b6b563cSPaul Mackerras 12909b6b563cSPaul Mackerras void __init early_init_devtree(void *params) 12919b6b563cSPaul Mackerras { 12929b6b563cSPaul Mackerras DBG(" -> early_init_devtree()\n"); 12939b6b563cSPaul Mackerras 12949b6b563cSPaul Mackerras /* Setup flat device-tree pointer */ 12959b6b563cSPaul Mackerras initial_boot_params = params; 12969b6b563cSPaul Mackerras 12979b6b563cSPaul Mackerras /* Retrieve various informations from the /chosen node of the 12989b6b563cSPaul Mackerras * device-tree, including the platform type, initrd location and 12999b6b563cSPaul Mackerras * size, TCE reserve, and more ... 13009b6b563cSPaul Mackerras */ 13019b6b563cSPaul Mackerras scan_flat_dt(early_init_dt_scan_chosen, NULL); 13029b6b563cSPaul Mackerras 13039b6b563cSPaul Mackerras /* Scan memory nodes and rebuild LMBs */ 13049b6b563cSPaul Mackerras lmb_init(); 13059b6b563cSPaul Mackerras scan_flat_dt(early_init_dt_scan_root, NULL); 13069b6b563cSPaul Mackerras scan_flat_dt(early_init_dt_scan_memory, NULL); 13079b6b563cSPaul Mackerras lmb_enforce_memory_limit(memory_limit); 13089b6b563cSPaul Mackerras lmb_analyze(); 13099b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 13109b6b563cSPaul Mackerras systemcfg->physicalMemorySize = lmb_phys_mem_size(); 13119b6b563cSPaul Mackerras #endif 13129b6b563cSPaul Mackerras lmb_reserve(0, __pa(klimit)); 13139b6b563cSPaul Mackerras 13149b6b563cSPaul Mackerras DBG("Phys. mem: %lx\n", lmb_phys_mem_size()); 13159b6b563cSPaul Mackerras 13169b6b563cSPaul Mackerras /* Reserve LMB regions used by kernel, initrd, dt, etc... */ 13179b6b563cSPaul Mackerras early_reserve_mem(); 13189b6b563cSPaul Mackerras 13199b6b563cSPaul Mackerras DBG("Scanning CPUs ...\n"); 13209b6b563cSPaul Mackerras 13219b6b563cSPaul Mackerras /* Retreive hash table size from flattened tree plus other 13229b6b563cSPaul Mackerras * CPU related informations (altivec support, boot CPU ID, ...) 13239b6b563cSPaul Mackerras */ 13249b6b563cSPaul Mackerras scan_flat_dt(early_init_dt_scan_cpus, NULL); 13259b6b563cSPaul Mackerras 13269b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 13279b6b563cSPaul Mackerras /* If hash size wasn't obtained above, we calculate it now based on 13289b6b563cSPaul Mackerras * the total RAM size 13299b6b563cSPaul Mackerras */ 13309b6b563cSPaul Mackerras if (ppc64_pft_size == 0) { 13319b6b563cSPaul Mackerras unsigned long rnd_mem_size, pteg_count; 13329b6b563cSPaul Mackerras 13339b6b563cSPaul Mackerras /* round mem_size up to next power of 2 */ 13349b6b563cSPaul Mackerras rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize); 13359b6b563cSPaul Mackerras if (rnd_mem_size < systemcfg->physicalMemorySize) 13369b6b563cSPaul Mackerras rnd_mem_size <<= 1; 13379b6b563cSPaul Mackerras 13389b6b563cSPaul Mackerras /* # pages / 2 */ 13399b6b563cSPaul Mackerras pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11); 13409b6b563cSPaul Mackerras 13419b6b563cSPaul Mackerras ppc64_pft_size = __ilog2(pteg_count << 7); 13429b6b563cSPaul Mackerras } 13439b6b563cSPaul Mackerras 13449b6b563cSPaul Mackerras DBG("Hash pftSize: %x\n", (int)ppc64_pft_size); 13459b6b563cSPaul Mackerras #endif 13469b6b563cSPaul Mackerras DBG(" <- early_init_devtree()\n"); 13479b6b563cSPaul Mackerras } 13489b6b563cSPaul Mackerras 13499b6b563cSPaul Mackerras #undef printk 13509b6b563cSPaul Mackerras 13519b6b563cSPaul Mackerras int 13529b6b563cSPaul Mackerras prom_n_addr_cells(struct device_node* np) 13539b6b563cSPaul Mackerras { 13549b6b563cSPaul Mackerras int* ip; 13559b6b563cSPaul Mackerras do { 13569b6b563cSPaul Mackerras if (np->parent) 13579b6b563cSPaul Mackerras np = np->parent; 13589b6b563cSPaul Mackerras ip = (int *) get_property(np, "#address-cells", NULL); 13599b6b563cSPaul Mackerras if (ip != NULL) 13609b6b563cSPaul Mackerras return *ip; 13619b6b563cSPaul Mackerras } while (np->parent); 13629b6b563cSPaul Mackerras /* No #address-cells property for the root node, default to 1 */ 13639b6b563cSPaul Mackerras return 1; 13649b6b563cSPaul Mackerras } 13659b6b563cSPaul Mackerras 13669b6b563cSPaul Mackerras int 13679b6b563cSPaul Mackerras prom_n_size_cells(struct device_node* np) 13689b6b563cSPaul Mackerras { 13699b6b563cSPaul Mackerras int* ip; 13709b6b563cSPaul Mackerras do { 13719b6b563cSPaul Mackerras if (np->parent) 13729b6b563cSPaul Mackerras np = np->parent; 13739b6b563cSPaul Mackerras ip = (int *) get_property(np, "#size-cells", NULL); 13749b6b563cSPaul Mackerras if (ip != NULL) 13759b6b563cSPaul Mackerras return *ip; 13769b6b563cSPaul Mackerras } while (np->parent); 13779b6b563cSPaul Mackerras /* No #size-cells property for the root node, default to 1 */ 13789b6b563cSPaul Mackerras return 1; 13799b6b563cSPaul Mackerras } 13809b6b563cSPaul Mackerras 13819b6b563cSPaul Mackerras /** 13829b6b563cSPaul Mackerras * Work out the sense (active-low level / active-high edge) 13839b6b563cSPaul Mackerras * of each interrupt from the device tree. 13849b6b563cSPaul Mackerras */ 13859b6b563cSPaul Mackerras void __init prom_get_irq_senses(unsigned char *senses, int off, int max) 13869b6b563cSPaul Mackerras { 13879b6b563cSPaul Mackerras struct device_node *np; 13889b6b563cSPaul Mackerras int i, j; 13899b6b563cSPaul Mackerras 13909b6b563cSPaul Mackerras /* default to level-triggered */ 13919b6b563cSPaul Mackerras memset(senses, 1, max - off); 13929b6b563cSPaul Mackerras 13939b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 13949b6b563cSPaul Mackerras for (j = 0; j < np->n_intrs; j++) { 13959b6b563cSPaul Mackerras i = np->intrs[j].line; 13969b6b563cSPaul Mackerras if (i >= off && i < max) 13979b6b563cSPaul Mackerras senses[i-off] = np->intrs[j].sense ? 13989b6b563cSPaul Mackerras IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE : 13999b6b563cSPaul Mackerras IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE; 14009b6b563cSPaul Mackerras } 14019b6b563cSPaul Mackerras } 14029b6b563cSPaul Mackerras } 14039b6b563cSPaul Mackerras 14049b6b563cSPaul Mackerras /** 14059b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given name. 14069b6b563cSPaul Mackerras */ 14079b6b563cSPaul Mackerras struct device_node *find_devices(const char *name) 14089b6b563cSPaul Mackerras { 14099b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 14109b6b563cSPaul Mackerras 14119b6b563cSPaul Mackerras prevp = &head; 14129b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 14139b6b563cSPaul Mackerras if (np->name != 0 && strcasecmp(np->name, name) == 0) { 14149b6b563cSPaul Mackerras *prevp = np; 14159b6b563cSPaul Mackerras prevp = &np->next; 14169b6b563cSPaul Mackerras } 14179b6b563cSPaul Mackerras } 14189b6b563cSPaul Mackerras *prevp = NULL; 14199b6b563cSPaul Mackerras return head; 14209b6b563cSPaul Mackerras } 14219b6b563cSPaul Mackerras EXPORT_SYMBOL(find_devices); 14229b6b563cSPaul Mackerras 14239b6b563cSPaul Mackerras /** 14249b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type. 14259b6b563cSPaul Mackerras */ 14269b6b563cSPaul Mackerras struct device_node *find_type_devices(const char *type) 14279b6b563cSPaul Mackerras { 14289b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 14299b6b563cSPaul Mackerras 14309b6b563cSPaul Mackerras prevp = &head; 14319b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 14329b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0) { 14339b6b563cSPaul Mackerras *prevp = np; 14349b6b563cSPaul Mackerras prevp = &np->next; 14359b6b563cSPaul Mackerras } 14369b6b563cSPaul Mackerras } 14379b6b563cSPaul Mackerras *prevp = NULL; 14389b6b563cSPaul Mackerras return head; 14399b6b563cSPaul Mackerras } 14409b6b563cSPaul Mackerras EXPORT_SYMBOL(find_type_devices); 14419b6b563cSPaul Mackerras 14429b6b563cSPaul Mackerras /** 14439b6b563cSPaul Mackerras * Returns all nodes linked together 14449b6b563cSPaul Mackerras */ 14459b6b563cSPaul Mackerras struct device_node *find_all_nodes(void) 14469b6b563cSPaul Mackerras { 14479b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 14489b6b563cSPaul Mackerras 14499b6b563cSPaul Mackerras prevp = &head; 14509b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 14519b6b563cSPaul Mackerras *prevp = np; 14529b6b563cSPaul Mackerras prevp = &np->next; 14539b6b563cSPaul Mackerras } 14549b6b563cSPaul Mackerras *prevp = NULL; 14559b6b563cSPaul Mackerras return head; 14569b6b563cSPaul Mackerras } 14579b6b563cSPaul Mackerras EXPORT_SYMBOL(find_all_nodes); 14589b6b563cSPaul Mackerras 14599b6b563cSPaul Mackerras /** Checks if the given "compat" string matches one of the strings in 14609b6b563cSPaul Mackerras * the device's "compatible" property 14619b6b563cSPaul Mackerras */ 14629b6b563cSPaul Mackerras int device_is_compatible(struct device_node *device, const char *compat) 14639b6b563cSPaul Mackerras { 14649b6b563cSPaul Mackerras const char* cp; 14659b6b563cSPaul Mackerras int cplen, l; 14669b6b563cSPaul Mackerras 14679b6b563cSPaul Mackerras cp = (char *) get_property(device, "compatible", &cplen); 14689b6b563cSPaul Mackerras if (cp == NULL) 14699b6b563cSPaul Mackerras return 0; 14709b6b563cSPaul Mackerras while (cplen > 0) { 14719b6b563cSPaul Mackerras if (strncasecmp(cp, compat, strlen(compat)) == 0) 14729b6b563cSPaul Mackerras return 1; 14739b6b563cSPaul Mackerras l = strlen(cp) + 1; 14749b6b563cSPaul Mackerras cp += l; 14759b6b563cSPaul Mackerras cplen -= l; 14769b6b563cSPaul Mackerras } 14779b6b563cSPaul Mackerras 14789b6b563cSPaul Mackerras return 0; 14799b6b563cSPaul Mackerras } 14809b6b563cSPaul Mackerras EXPORT_SYMBOL(device_is_compatible); 14819b6b563cSPaul Mackerras 14829b6b563cSPaul Mackerras 14839b6b563cSPaul Mackerras /** 14849b6b563cSPaul Mackerras * Indicates whether the root node has a given value in its 14859b6b563cSPaul Mackerras * compatible property. 14869b6b563cSPaul Mackerras */ 14879b6b563cSPaul Mackerras int machine_is_compatible(const char *compat) 14889b6b563cSPaul Mackerras { 14899b6b563cSPaul Mackerras struct device_node *root; 14909b6b563cSPaul Mackerras int rc = 0; 14919b6b563cSPaul Mackerras 14929b6b563cSPaul Mackerras root = of_find_node_by_path("/"); 14939b6b563cSPaul Mackerras if (root) { 14949b6b563cSPaul Mackerras rc = device_is_compatible(root, compat); 14959b6b563cSPaul Mackerras of_node_put(root); 14969b6b563cSPaul Mackerras } 14979b6b563cSPaul Mackerras return rc; 14989b6b563cSPaul Mackerras } 14999b6b563cSPaul Mackerras EXPORT_SYMBOL(machine_is_compatible); 15009b6b563cSPaul Mackerras 15019b6b563cSPaul Mackerras /** 15029b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type 15039b6b563cSPaul Mackerras * and compatible property. 15049b6b563cSPaul Mackerras */ 15059b6b563cSPaul Mackerras struct device_node *find_compatible_devices(const char *type, 15069b6b563cSPaul Mackerras const char *compat) 15079b6b563cSPaul Mackerras { 15089b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 15099b6b563cSPaul Mackerras 15109b6b563cSPaul Mackerras prevp = &head; 15119b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 15129b6b563cSPaul Mackerras if (type != NULL 15139b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 15149b6b563cSPaul Mackerras continue; 15159b6b563cSPaul Mackerras if (device_is_compatible(np, compat)) { 15169b6b563cSPaul Mackerras *prevp = np; 15179b6b563cSPaul Mackerras prevp = &np->next; 15189b6b563cSPaul Mackerras } 15199b6b563cSPaul Mackerras } 15209b6b563cSPaul Mackerras *prevp = NULL; 15219b6b563cSPaul Mackerras return head; 15229b6b563cSPaul Mackerras } 15239b6b563cSPaul Mackerras EXPORT_SYMBOL(find_compatible_devices); 15249b6b563cSPaul Mackerras 15259b6b563cSPaul Mackerras /** 15269b6b563cSPaul Mackerras * Find the device_node with a given full_name. 15279b6b563cSPaul Mackerras */ 15289b6b563cSPaul Mackerras struct device_node *find_path_device(const char *path) 15299b6b563cSPaul Mackerras { 15309b6b563cSPaul Mackerras struct device_node *np; 15319b6b563cSPaul Mackerras 15329b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 15339b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) 15349b6b563cSPaul Mackerras return np; 15359b6b563cSPaul Mackerras return NULL; 15369b6b563cSPaul Mackerras } 15379b6b563cSPaul Mackerras EXPORT_SYMBOL(find_path_device); 15389b6b563cSPaul Mackerras 15399b6b563cSPaul Mackerras /******* 15409b6b563cSPaul Mackerras * 15419b6b563cSPaul Mackerras * New implementation of the OF "find" APIs, return a refcounted 15429b6b563cSPaul Mackerras * object, call of_node_put() when done. The device tree and list 15439b6b563cSPaul Mackerras * are protected by a rw_lock. 15449b6b563cSPaul Mackerras * 15459b6b563cSPaul Mackerras * Note that property management will need some locking as well, 15469b6b563cSPaul Mackerras * this isn't dealt with yet. 15479b6b563cSPaul Mackerras * 15489b6b563cSPaul Mackerras *******/ 15499b6b563cSPaul Mackerras 15509b6b563cSPaul Mackerras /** 15519b6b563cSPaul Mackerras * of_find_node_by_name - Find a node by its "name" property 15529b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 15539b6b563cSPaul Mackerras * you pass will not be searched, only the next one 15549b6b563cSPaul Mackerras * will; typically, you pass what the previous call 15559b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 15569b6b563cSPaul Mackerras * @name: The name string to match against 15579b6b563cSPaul Mackerras * 15589b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 15599b6b563cSPaul Mackerras * of_node_put() on it when done. 15609b6b563cSPaul Mackerras */ 15619b6b563cSPaul Mackerras struct device_node *of_find_node_by_name(struct device_node *from, 15629b6b563cSPaul Mackerras const char *name) 15639b6b563cSPaul Mackerras { 15649b6b563cSPaul Mackerras struct device_node *np; 15659b6b563cSPaul Mackerras 15669b6b563cSPaul Mackerras read_lock(&devtree_lock); 15679b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 15689b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 15699b6b563cSPaul Mackerras if (np->name != 0 && strcasecmp(np->name, name) == 0 15709b6b563cSPaul Mackerras && of_node_get(np)) 15719b6b563cSPaul Mackerras break; 15729b6b563cSPaul Mackerras if (from) 15739b6b563cSPaul Mackerras of_node_put(from); 15749b6b563cSPaul Mackerras read_unlock(&devtree_lock); 15759b6b563cSPaul Mackerras return np; 15769b6b563cSPaul Mackerras } 15779b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_name); 15789b6b563cSPaul Mackerras 15799b6b563cSPaul Mackerras /** 15809b6b563cSPaul Mackerras * of_find_node_by_type - Find a node by its "device_type" property 15819b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 15829b6b563cSPaul Mackerras * you pass will not be searched, only the next one 15839b6b563cSPaul Mackerras * will; typically, you pass what the previous call 15849b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 15859b6b563cSPaul Mackerras * @name: The type string to match against 15869b6b563cSPaul Mackerras * 15879b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 15889b6b563cSPaul Mackerras * of_node_put() on it when done. 15899b6b563cSPaul Mackerras */ 15909b6b563cSPaul Mackerras struct device_node *of_find_node_by_type(struct device_node *from, 15919b6b563cSPaul Mackerras const char *type) 15929b6b563cSPaul Mackerras { 15939b6b563cSPaul Mackerras struct device_node *np; 15949b6b563cSPaul Mackerras 15959b6b563cSPaul Mackerras read_lock(&devtree_lock); 15969b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 15979b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 15989b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0 15999b6b563cSPaul Mackerras && of_node_get(np)) 16009b6b563cSPaul Mackerras break; 16019b6b563cSPaul Mackerras if (from) 16029b6b563cSPaul Mackerras of_node_put(from); 16039b6b563cSPaul Mackerras read_unlock(&devtree_lock); 16049b6b563cSPaul Mackerras return np; 16059b6b563cSPaul Mackerras } 16069b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_type); 16079b6b563cSPaul Mackerras 16089b6b563cSPaul Mackerras /** 16099b6b563cSPaul Mackerras * of_find_compatible_node - Find a node based on type and one of the 16109b6b563cSPaul Mackerras * tokens in its "compatible" property 16119b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 16129b6b563cSPaul Mackerras * you pass will not be searched, only the next one 16139b6b563cSPaul Mackerras * will; typically, you pass what the previous call 16149b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 16159b6b563cSPaul Mackerras * @type: The type string to match "device_type" or NULL to ignore 16169b6b563cSPaul Mackerras * @compatible: The string to match to one of the tokens in the device 16179b6b563cSPaul Mackerras * "compatible" list. 16189b6b563cSPaul Mackerras * 16199b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 16209b6b563cSPaul Mackerras * of_node_put() on it when done. 16219b6b563cSPaul Mackerras */ 16229b6b563cSPaul Mackerras struct device_node *of_find_compatible_node(struct device_node *from, 16239b6b563cSPaul Mackerras const char *type, const char *compatible) 16249b6b563cSPaul Mackerras { 16259b6b563cSPaul Mackerras struct device_node *np; 16269b6b563cSPaul Mackerras 16279b6b563cSPaul Mackerras read_lock(&devtree_lock); 16289b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 16299b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 16309b6b563cSPaul Mackerras if (type != NULL 16319b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 16329b6b563cSPaul Mackerras continue; 16339b6b563cSPaul Mackerras if (device_is_compatible(np, compatible) && of_node_get(np)) 16349b6b563cSPaul Mackerras break; 16359b6b563cSPaul Mackerras } 16369b6b563cSPaul Mackerras if (from) 16379b6b563cSPaul Mackerras of_node_put(from); 16389b6b563cSPaul Mackerras read_unlock(&devtree_lock); 16399b6b563cSPaul Mackerras return np; 16409b6b563cSPaul Mackerras } 16419b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_compatible_node); 16429b6b563cSPaul Mackerras 16439b6b563cSPaul Mackerras /** 16449b6b563cSPaul Mackerras * of_find_node_by_path - Find a node matching a full OF path 16459b6b563cSPaul Mackerras * @path: The full path to match 16469b6b563cSPaul Mackerras * 16479b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 16489b6b563cSPaul Mackerras * of_node_put() on it when done. 16499b6b563cSPaul Mackerras */ 16509b6b563cSPaul Mackerras struct device_node *of_find_node_by_path(const char *path) 16519b6b563cSPaul Mackerras { 16529b6b563cSPaul Mackerras struct device_node *np = allnodes; 16539b6b563cSPaul Mackerras 16549b6b563cSPaul Mackerras read_lock(&devtree_lock); 16559b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 16569b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0 16579b6b563cSPaul Mackerras && of_node_get(np)) 16589b6b563cSPaul Mackerras break; 16599b6b563cSPaul Mackerras } 16609b6b563cSPaul Mackerras read_unlock(&devtree_lock); 16619b6b563cSPaul Mackerras return np; 16629b6b563cSPaul Mackerras } 16639b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_path); 16649b6b563cSPaul Mackerras 16659b6b563cSPaul Mackerras /** 16669b6b563cSPaul Mackerras * of_find_node_by_phandle - Find a node given a phandle 16679b6b563cSPaul Mackerras * @handle: phandle of the node to find 16689b6b563cSPaul Mackerras * 16699b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 16709b6b563cSPaul Mackerras * of_node_put() on it when done. 16719b6b563cSPaul Mackerras */ 16729b6b563cSPaul Mackerras struct device_node *of_find_node_by_phandle(phandle handle) 16739b6b563cSPaul Mackerras { 16749b6b563cSPaul Mackerras struct device_node *np; 16759b6b563cSPaul Mackerras 16769b6b563cSPaul Mackerras read_lock(&devtree_lock); 16779b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 16789b6b563cSPaul Mackerras if (np->linux_phandle == handle) 16799b6b563cSPaul Mackerras break; 16809b6b563cSPaul Mackerras if (np) 16819b6b563cSPaul Mackerras of_node_get(np); 16829b6b563cSPaul Mackerras read_unlock(&devtree_lock); 16839b6b563cSPaul Mackerras return np; 16849b6b563cSPaul Mackerras } 16859b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_phandle); 16869b6b563cSPaul Mackerras 16879b6b563cSPaul Mackerras /** 16889b6b563cSPaul Mackerras * of_find_all_nodes - Get next node in global list 16899b6b563cSPaul Mackerras * @prev: Previous node or NULL to start iteration 16909b6b563cSPaul Mackerras * of_node_put() will be called on it 16919b6b563cSPaul Mackerras * 16929b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 16939b6b563cSPaul Mackerras * of_node_put() on it when done. 16949b6b563cSPaul Mackerras */ 16959b6b563cSPaul Mackerras struct device_node *of_find_all_nodes(struct device_node *prev) 16969b6b563cSPaul Mackerras { 16979b6b563cSPaul Mackerras struct device_node *np; 16989b6b563cSPaul Mackerras 16999b6b563cSPaul Mackerras read_lock(&devtree_lock); 17009b6b563cSPaul Mackerras np = prev ? prev->allnext : allnodes; 17019b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 17029b6b563cSPaul Mackerras if (of_node_get(np)) 17039b6b563cSPaul Mackerras break; 17049b6b563cSPaul Mackerras if (prev) 17059b6b563cSPaul Mackerras of_node_put(prev); 17069b6b563cSPaul Mackerras read_unlock(&devtree_lock); 17079b6b563cSPaul Mackerras return np; 17089b6b563cSPaul Mackerras } 17099b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_all_nodes); 17109b6b563cSPaul Mackerras 17119b6b563cSPaul Mackerras /** 17129b6b563cSPaul Mackerras * of_get_parent - Get a node's parent if any 17139b6b563cSPaul Mackerras * @node: Node to get parent 17149b6b563cSPaul Mackerras * 17159b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 17169b6b563cSPaul Mackerras * of_node_put() on it when done. 17179b6b563cSPaul Mackerras */ 17189b6b563cSPaul Mackerras struct device_node *of_get_parent(const struct device_node *node) 17199b6b563cSPaul Mackerras { 17209b6b563cSPaul Mackerras struct device_node *np; 17219b6b563cSPaul Mackerras 17229b6b563cSPaul Mackerras if (!node) 17239b6b563cSPaul Mackerras return NULL; 17249b6b563cSPaul Mackerras 17259b6b563cSPaul Mackerras read_lock(&devtree_lock); 17269b6b563cSPaul Mackerras np = of_node_get(node->parent); 17279b6b563cSPaul Mackerras read_unlock(&devtree_lock); 17289b6b563cSPaul Mackerras return np; 17299b6b563cSPaul Mackerras } 17309b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_parent); 17319b6b563cSPaul Mackerras 17329b6b563cSPaul Mackerras /** 17339b6b563cSPaul Mackerras * of_get_next_child - Iterate a node childs 17349b6b563cSPaul Mackerras * @node: parent node 17359b6b563cSPaul Mackerras * @prev: previous child of the parent node, or NULL to get first 17369b6b563cSPaul Mackerras * 17379b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 17389b6b563cSPaul Mackerras * of_node_put() on it when done. 17399b6b563cSPaul Mackerras */ 17409b6b563cSPaul Mackerras struct device_node *of_get_next_child(const struct device_node *node, 17419b6b563cSPaul Mackerras struct device_node *prev) 17429b6b563cSPaul Mackerras { 17439b6b563cSPaul Mackerras struct device_node *next; 17449b6b563cSPaul Mackerras 17459b6b563cSPaul Mackerras read_lock(&devtree_lock); 17469b6b563cSPaul Mackerras next = prev ? prev->sibling : node->child; 17479b6b563cSPaul Mackerras for (; next != 0; next = next->sibling) 17489b6b563cSPaul Mackerras if (of_node_get(next)) 17499b6b563cSPaul Mackerras break; 17509b6b563cSPaul Mackerras if (prev) 17519b6b563cSPaul Mackerras of_node_put(prev); 17529b6b563cSPaul Mackerras read_unlock(&devtree_lock); 17539b6b563cSPaul Mackerras return next; 17549b6b563cSPaul Mackerras } 17559b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_next_child); 17569b6b563cSPaul Mackerras 17579b6b563cSPaul Mackerras /** 17589b6b563cSPaul Mackerras * of_node_get - Increment refcount of a node 17599b6b563cSPaul Mackerras * @node: Node to inc refcount, NULL is supported to 17609b6b563cSPaul Mackerras * simplify writing of callers 17619b6b563cSPaul Mackerras * 17629b6b563cSPaul Mackerras * Returns node. 17639b6b563cSPaul Mackerras */ 17649b6b563cSPaul Mackerras struct device_node *of_node_get(struct device_node *node) 17659b6b563cSPaul Mackerras { 17669b6b563cSPaul Mackerras if (node) 17679b6b563cSPaul Mackerras kref_get(&node->kref); 17689b6b563cSPaul Mackerras return node; 17699b6b563cSPaul Mackerras } 17709b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_get); 17719b6b563cSPaul Mackerras 17729b6b563cSPaul Mackerras static inline struct device_node * kref_to_device_node(struct kref *kref) 17739b6b563cSPaul Mackerras { 17749b6b563cSPaul Mackerras return container_of(kref, struct device_node, kref); 17759b6b563cSPaul Mackerras } 17769b6b563cSPaul Mackerras 17779b6b563cSPaul Mackerras /** 17789b6b563cSPaul Mackerras * of_node_release - release a dynamically allocated node 17799b6b563cSPaul Mackerras * @kref: kref element of the node to be released 17809b6b563cSPaul Mackerras * 17819b6b563cSPaul Mackerras * In of_node_put() this function is passed to kref_put() 17829b6b563cSPaul Mackerras * as the destructor. 17839b6b563cSPaul Mackerras */ 17849b6b563cSPaul Mackerras static void of_node_release(struct kref *kref) 17859b6b563cSPaul Mackerras { 17869b6b563cSPaul Mackerras struct device_node *node = kref_to_device_node(kref); 17879b6b563cSPaul Mackerras struct property *prop = node->properties; 17889b6b563cSPaul Mackerras 17899b6b563cSPaul Mackerras if (!OF_IS_DYNAMIC(node)) 17909b6b563cSPaul Mackerras return; 17919b6b563cSPaul Mackerras while (prop) { 17929b6b563cSPaul Mackerras struct property *next = prop->next; 17939b6b563cSPaul Mackerras kfree(prop->name); 17949b6b563cSPaul Mackerras kfree(prop->value); 17959b6b563cSPaul Mackerras kfree(prop); 17969b6b563cSPaul Mackerras prop = next; 17979b6b563cSPaul Mackerras } 17989b6b563cSPaul Mackerras kfree(node->intrs); 17999b6b563cSPaul Mackerras kfree(node->addrs); 18009b6b563cSPaul Mackerras kfree(node->full_name); 18019b6b563cSPaul Mackerras kfree(node->data); 18029b6b563cSPaul Mackerras kfree(node); 18039b6b563cSPaul Mackerras } 18049b6b563cSPaul Mackerras 18059b6b563cSPaul Mackerras /** 18069b6b563cSPaul Mackerras * of_node_put - Decrement refcount of a node 18079b6b563cSPaul Mackerras * @node: Node to dec refcount, NULL is supported to 18089b6b563cSPaul Mackerras * simplify writing of callers 18099b6b563cSPaul Mackerras * 18109b6b563cSPaul Mackerras */ 18119b6b563cSPaul Mackerras void of_node_put(struct device_node *node) 18129b6b563cSPaul Mackerras { 18139b6b563cSPaul Mackerras if (node) 18149b6b563cSPaul Mackerras kref_put(&node->kref, of_node_release); 18159b6b563cSPaul Mackerras } 18169b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_put); 18179b6b563cSPaul Mackerras 18189b6b563cSPaul Mackerras /* 18199b6b563cSPaul Mackerras * Plug a device node into the tree and global list. 18209b6b563cSPaul Mackerras */ 18219b6b563cSPaul Mackerras void of_attach_node(struct device_node *np) 18229b6b563cSPaul Mackerras { 18239b6b563cSPaul Mackerras write_lock(&devtree_lock); 18249b6b563cSPaul Mackerras np->sibling = np->parent->child; 18259b6b563cSPaul Mackerras np->allnext = allnodes; 18269b6b563cSPaul Mackerras np->parent->child = np; 18279b6b563cSPaul Mackerras allnodes = np; 18289b6b563cSPaul Mackerras write_unlock(&devtree_lock); 18299b6b563cSPaul Mackerras } 18309b6b563cSPaul Mackerras 18319b6b563cSPaul Mackerras /* 18329b6b563cSPaul Mackerras * "Unplug" a node from the device tree. The caller must hold 18339b6b563cSPaul Mackerras * a reference to the node. The memory associated with the node 18349b6b563cSPaul Mackerras * is not freed until its refcount goes to zero. 18359b6b563cSPaul Mackerras */ 18369b6b563cSPaul Mackerras void of_detach_node(const struct device_node *np) 18379b6b563cSPaul Mackerras { 18389b6b563cSPaul Mackerras struct device_node *parent; 18399b6b563cSPaul Mackerras 18409b6b563cSPaul Mackerras write_lock(&devtree_lock); 18419b6b563cSPaul Mackerras 18429b6b563cSPaul Mackerras parent = np->parent; 18439b6b563cSPaul Mackerras 18449b6b563cSPaul Mackerras if (allnodes == np) 18459b6b563cSPaul Mackerras allnodes = np->allnext; 18469b6b563cSPaul Mackerras else { 18479b6b563cSPaul Mackerras struct device_node *prev; 18489b6b563cSPaul Mackerras for (prev = allnodes; 18499b6b563cSPaul Mackerras prev->allnext != np; 18509b6b563cSPaul Mackerras prev = prev->allnext) 18519b6b563cSPaul Mackerras ; 18529b6b563cSPaul Mackerras prev->allnext = np->allnext; 18539b6b563cSPaul Mackerras } 18549b6b563cSPaul Mackerras 18559b6b563cSPaul Mackerras if (parent->child == np) 18569b6b563cSPaul Mackerras parent->child = np->sibling; 18579b6b563cSPaul Mackerras else { 18589b6b563cSPaul Mackerras struct device_node *prevsib; 18599b6b563cSPaul Mackerras for (prevsib = np->parent->child; 18609b6b563cSPaul Mackerras prevsib->sibling != np; 18619b6b563cSPaul Mackerras prevsib = prevsib->sibling) 18629b6b563cSPaul Mackerras ; 18639b6b563cSPaul Mackerras prevsib->sibling = np->sibling; 18649b6b563cSPaul Mackerras } 18659b6b563cSPaul Mackerras 18669b6b563cSPaul Mackerras write_unlock(&devtree_lock); 18679b6b563cSPaul Mackerras } 18689b6b563cSPaul Mackerras 18699b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 18709b6b563cSPaul Mackerras /* 18719b6b563cSPaul Mackerras * Fix up the uninitialized fields in a new device node: 18729b6b563cSPaul Mackerras * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields 18739b6b563cSPaul Mackerras * 18749b6b563cSPaul Mackerras * A lot of boot-time code is duplicated here, because functions such 18759b6b563cSPaul Mackerras * as finish_node_interrupts, interpret_pci_props, etc. cannot use the 18769b6b563cSPaul Mackerras * slab allocator. 18779b6b563cSPaul Mackerras * 18789b6b563cSPaul Mackerras * This should probably be split up into smaller chunks. 18799b6b563cSPaul Mackerras */ 18809b6b563cSPaul Mackerras 18819b6b563cSPaul Mackerras static int of_finish_dynamic_node(struct device_node *node, 18829b6b563cSPaul Mackerras unsigned long *unused1, int unused2, 18839b6b563cSPaul Mackerras int unused3, int unused4) 18849b6b563cSPaul Mackerras { 18859b6b563cSPaul Mackerras struct device_node *parent = of_get_parent(node); 18869b6b563cSPaul Mackerras int err = 0; 18879b6b563cSPaul Mackerras phandle *ibm_phandle; 18889b6b563cSPaul Mackerras 18899b6b563cSPaul Mackerras node->name = get_property(node, "name", NULL); 18909b6b563cSPaul Mackerras node->type = get_property(node, "device_type", NULL); 18919b6b563cSPaul Mackerras 18929b6b563cSPaul Mackerras if (!parent) { 18939b6b563cSPaul Mackerras err = -ENODEV; 18949b6b563cSPaul Mackerras goto out; 18959b6b563cSPaul Mackerras } 18969b6b563cSPaul Mackerras 18979b6b563cSPaul Mackerras /* We don't support that function on PowerMac, at least 18989b6b563cSPaul Mackerras * not yet 18999b6b563cSPaul Mackerras */ 19009b6b563cSPaul Mackerras if (systemcfg->platform == PLATFORM_POWERMAC) 19019b6b563cSPaul Mackerras return -ENODEV; 19029b6b563cSPaul Mackerras 19039b6b563cSPaul Mackerras /* fix up new node's linux_phandle field */ 19049b6b563cSPaul Mackerras if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL))) 19059b6b563cSPaul Mackerras node->linux_phandle = *ibm_phandle; 19069b6b563cSPaul Mackerras 19079b6b563cSPaul Mackerras out: 19089b6b563cSPaul Mackerras of_node_put(parent); 19099b6b563cSPaul Mackerras return err; 19109b6b563cSPaul Mackerras } 19119b6b563cSPaul Mackerras 19129b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb, 19139b6b563cSPaul Mackerras unsigned long action, void *node) 19149b6b563cSPaul Mackerras { 19159b6b563cSPaul Mackerras int err; 19169b6b563cSPaul Mackerras 19179b6b563cSPaul Mackerras switch (action) { 19189b6b563cSPaul Mackerras case PSERIES_RECONFIG_ADD: 19199b6b563cSPaul Mackerras err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0); 19209b6b563cSPaul Mackerras if (err < 0) { 19219b6b563cSPaul Mackerras printk(KERN_ERR "finish_node returned %d\n", err); 19229b6b563cSPaul Mackerras err = NOTIFY_BAD; 19239b6b563cSPaul Mackerras } 19249b6b563cSPaul Mackerras break; 19259b6b563cSPaul Mackerras default: 19269b6b563cSPaul Mackerras err = NOTIFY_DONE; 19279b6b563cSPaul Mackerras break; 19289b6b563cSPaul Mackerras } 19299b6b563cSPaul Mackerras return err; 19309b6b563cSPaul Mackerras } 19319b6b563cSPaul Mackerras 19329b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = { 19339b6b563cSPaul Mackerras .notifier_call = prom_reconfig_notifier, 19349b6b563cSPaul Mackerras .priority = 10, /* This one needs to run first */ 19359b6b563cSPaul Mackerras }; 19369b6b563cSPaul Mackerras 19379b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void) 19389b6b563cSPaul Mackerras { 19399b6b563cSPaul Mackerras return pSeries_reconfig_notifier_register(&prom_reconfig_nb); 19409b6b563cSPaul Mackerras } 19419b6b563cSPaul Mackerras __initcall(prom_reconfig_setup); 19429b6b563cSPaul Mackerras #endif 19439b6b563cSPaul Mackerras 19449b6b563cSPaul Mackerras /* 19459b6b563cSPaul Mackerras * Find a property with a given name for a given node 19469b6b563cSPaul Mackerras * and return the value. 19479b6b563cSPaul Mackerras */ 19489b6b563cSPaul Mackerras unsigned char *get_property(struct device_node *np, const char *name, 19499b6b563cSPaul Mackerras int *lenp) 19509b6b563cSPaul Mackerras { 19519b6b563cSPaul Mackerras struct property *pp; 19529b6b563cSPaul Mackerras 19539b6b563cSPaul Mackerras for (pp = np->properties; pp != 0; pp = pp->next) 19549b6b563cSPaul Mackerras if (strcmp(pp->name, name) == 0) { 19559b6b563cSPaul Mackerras if (lenp != 0) 19569b6b563cSPaul Mackerras *lenp = pp->length; 19579b6b563cSPaul Mackerras return pp->value; 19589b6b563cSPaul Mackerras } 19599b6b563cSPaul Mackerras return NULL; 19609b6b563cSPaul Mackerras } 19619b6b563cSPaul Mackerras EXPORT_SYMBOL(get_property); 19629b6b563cSPaul Mackerras 19639b6b563cSPaul Mackerras /* 19649b6b563cSPaul Mackerras * Add a property to a node 19659b6b563cSPaul Mackerras */ 19669b6b563cSPaul Mackerras void prom_add_property(struct device_node* np, struct property* prop) 19679b6b563cSPaul Mackerras { 19689b6b563cSPaul Mackerras struct property **next = &np->properties; 19699b6b563cSPaul Mackerras 19709b6b563cSPaul Mackerras prop->next = NULL; 19719b6b563cSPaul Mackerras while (*next) 19729b6b563cSPaul Mackerras next = &(*next)->next; 19739b6b563cSPaul Mackerras *next = prop; 19749b6b563cSPaul Mackerras } 19759b6b563cSPaul Mackerras 19769b6b563cSPaul Mackerras /* I quickly hacked that one, check against spec ! */ 19779b6b563cSPaul Mackerras static inline unsigned long 19789b6b563cSPaul Mackerras bus_space_to_resource_flags(unsigned int bus_space) 19799b6b563cSPaul Mackerras { 19809b6b563cSPaul Mackerras u8 space = (bus_space >> 24) & 0xf; 19819b6b563cSPaul Mackerras if (space == 0) 19829b6b563cSPaul Mackerras space = 0x02; 19839b6b563cSPaul Mackerras if (space == 0x02) 19849b6b563cSPaul Mackerras return IORESOURCE_MEM; 19859b6b563cSPaul Mackerras else if (space == 0x01) 19869b6b563cSPaul Mackerras return IORESOURCE_IO; 19879b6b563cSPaul Mackerras else { 19889b6b563cSPaul Mackerras printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n", 19899b6b563cSPaul Mackerras bus_space); 19909b6b563cSPaul Mackerras return 0; 19919b6b563cSPaul Mackerras } 19929b6b563cSPaul Mackerras } 19939b6b563cSPaul Mackerras 19949b6b563cSPaul Mackerras static struct resource *find_parent_pci_resource(struct pci_dev* pdev, 19959b6b563cSPaul Mackerras struct address_range *range) 19969b6b563cSPaul Mackerras { 19979b6b563cSPaul Mackerras unsigned long mask; 19989b6b563cSPaul Mackerras int i; 19999b6b563cSPaul Mackerras 20009b6b563cSPaul Mackerras /* Check this one */ 20019b6b563cSPaul Mackerras mask = bus_space_to_resource_flags(range->space); 20029b6b563cSPaul Mackerras for (i=0; i<DEVICE_COUNT_RESOURCE; i++) { 20039b6b563cSPaul Mackerras if ((pdev->resource[i].flags & mask) == mask && 20049b6b563cSPaul Mackerras pdev->resource[i].start <= range->address && 20059b6b563cSPaul Mackerras pdev->resource[i].end > range->address) { 20069b6b563cSPaul Mackerras if ((range->address + range->size - 1) > pdev->resource[i].end) { 20079b6b563cSPaul Mackerras /* Add better message */ 20089b6b563cSPaul Mackerras printk(KERN_WARNING "PCI/OF resource overlap !\n"); 20099b6b563cSPaul Mackerras return NULL; 20109b6b563cSPaul Mackerras } 20119b6b563cSPaul Mackerras break; 20129b6b563cSPaul Mackerras } 20139b6b563cSPaul Mackerras } 20149b6b563cSPaul Mackerras if (i == DEVICE_COUNT_RESOURCE) 20159b6b563cSPaul Mackerras return NULL; 20169b6b563cSPaul Mackerras return &pdev->resource[i]; 20179b6b563cSPaul Mackerras } 20189b6b563cSPaul Mackerras 20199b6b563cSPaul Mackerras /* 20209b6b563cSPaul Mackerras * Request an OF device resource. Currently handles child of PCI devices, 20219b6b563cSPaul Mackerras * or other nodes attached to the root node. Ultimately, put some 20229b6b563cSPaul Mackerras * link to resources in the OF node. 20239b6b563cSPaul Mackerras */ 20249b6b563cSPaul Mackerras struct resource *request_OF_resource(struct device_node* node, int index, 20259b6b563cSPaul Mackerras const char* name_postfix) 20269b6b563cSPaul Mackerras { 20279b6b563cSPaul Mackerras struct pci_dev* pcidev; 20289b6b563cSPaul Mackerras u8 pci_bus, pci_devfn; 20299b6b563cSPaul Mackerras unsigned long iomask; 20309b6b563cSPaul Mackerras struct device_node* nd; 20319b6b563cSPaul Mackerras struct resource* parent; 20329b6b563cSPaul Mackerras struct resource *res = NULL; 20339b6b563cSPaul Mackerras int nlen, plen; 20349b6b563cSPaul Mackerras 20359b6b563cSPaul Mackerras if (index >= node->n_addrs) 20369b6b563cSPaul Mackerras goto fail; 20379b6b563cSPaul Mackerras 20389b6b563cSPaul Mackerras /* Sanity check on bus space */ 20399b6b563cSPaul Mackerras iomask = bus_space_to_resource_flags(node->addrs[index].space); 20409b6b563cSPaul Mackerras if (iomask & IORESOURCE_MEM) 20419b6b563cSPaul Mackerras parent = &iomem_resource; 20429b6b563cSPaul Mackerras else if (iomask & IORESOURCE_IO) 20439b6b563cSPaul Mackerras parent = &ioport_resource; 20449b6b563cSPaul Mackerras else 20459b6b563cSPaul Mackerras goto fail; 20469b6b563cSPaul Mackerras 20479b6b563cSPaul Mackerras /* Find a PCI parent if any */ 20489b6b563cSPaul Mackerras nd = node; 20499b6b563cSPaul Mackerras pcidev = NULL; 20509b6b563cSPaul Mackerras while (nd) { 20519b6b563cSPaul Mackerras if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn)) 20529b6b563cSPaul Mackerras pcidev = pci_find_slot(pci_bus, pci_devfn); 20539b6b563cSPaul Mackerras if (pcidev) break; 20549b6b563cSPaul Mackerras nd = nd->parent; 20559b6b563cSPaul Mackerras } 20569b6b563cSPaul Mackerras if (pcidev) 20579b6b563cSPaul Mackerras parent = find_parent_pci_resource(pcidev, &node->addrs[index]); 20589b6b563cSPaul Mackerras if (!parent) { 20599b6b563cSPaul Mackerras printk(KERN_WARNING "request_OF_resource(%s), parent not found\n", 20609b6b563cSPaul Mackerras node->name); 20619b6b563cSPaul Mackerras goto fail; 20629b6b563cSPaul Mackerras } 20639b6b563cSPaul Mackerras 20649b6b563cSPaul Mackerras res = __request_region(parent, node->addrs[index].address, 20659b6b563cSPaul Mackerras node->addrs[index].size, NULL); 20669b6b563cSPaul Mackerras if (!res) 20679b6b563cSPaul Mackerras goto fail; 20689b6b563cSPaul Mackerras nlen = strlen(node->name); 20699b6b563cSPaul Mackerras plen = name_postfix ? strlen(name_postfix) : 0; 20709b6b563cSPaul Mackerras res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL); 20719b6b563cSPaul Mackerras if (res->name) { 20729b6b563cSPaul Mackerras strcpy((char *)res->name, node->name); 20739b6b563cSPaul Mackerras if (plen) 20749b6b563cSPaul Mackerras strcpy((char *)res->name+nlen, name_postfix); 20759b6b563cSPaul Mackerras } 20769b6b563cSPaul Mackerras return res; 20779b6b563cSPaul Mackerras fail: 20789b6b563cSPaul Mackerras return NULL; 20799b6b563cSPaul Mackerras } 20809b6b563cSPaul Mackerras EXPORT_SYMBOL(request_OF_resource); 20819b6b563cSPaul Mackerras 20829b6b563cSPaul Mackerras int release_OF_resource(struct device_node *node, int index) 20839b6b563cSPaul Mackerras { 20849b6b563cSPaul Mackerras struct pci_dev* pcidev; 20859b6b563cSPaul Mackerras u8 pci_bus, pci_devfn; 20869b6b563cSPaul Mackerras unsigned long iomask, start, end; 20879b6b563cSPaul Mackerras struct device_node* nd; 20889b6b563cSPaul Mackerras struct resource* parent; 20899b6b563cSPaul Mackerras struct resource *res = NULL; 20909b6b563cSPaul Mackerras 20919b6b563cSPaul Mackerras if (index >= node->n_addrs) 20929b6b563cSPaul Mackerras return -EINVAL; 20939b6b563cSPaul Mackerras 20949b6b563cSPaul Mackerras /* Sanity check on bus space */ 20959b6b563cSPaul Mackerras iomask = bus_space_to_resource_flags(node->addrs[index].space); 20969b6b563cSPaul Mackerras if (iomask & IORESOURCE_MEM) 20979b6b563cSPaul Mackerras parent = &iomem_resource; 20989b6b563cSPaul Mackerras else if (iomask & IORESOURCE_IO) 20999b6b563cSPaul Mackerras parent = &ioport_resource; 21009b6b563cSPaul Mackerras else 21019b6b563cSPaul Mackerras return -EINVAL; 21029b6b563cSPaul Mackerras 21039b6b563cSPaul Mackerras /* Find a PCI parent if any */ 21049b6b563cSPaul Mackerras nd = node; 21059b6b563cSPaul Mackerras pcidev = NULL; 21069b6b563cSPaul Mackerras while(nd) { 21079b6b563cSPaul Mackerras if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn)) 21089b6b563cSPaul Mackerras pcidev = pci_find_slot(pci_bus, pci_devfn); 21099b6b563cSPaul Mackerras if (pcidev) break; 21109b6b563cSPaul Mackerras nd = nd->parent; 21119b6b563cSPaul Mackerras } 21129b6b563cSPaul Mackerras if (pcidev) 21139b6b563cSPaul Mackerras parent = find_parent_pci_resource(pcidev, &node->addrs[index]); 21149b6b563cSPaul Mackerras if (!parent) { 21159b6b563cSPaul Mackerras printk(KERN_WARNING "release_OF_resource(%s), parent not found\n", 21169b6b563cSPaul Mackerras node->name); 21179b6b563cSPaul Mackerras return -ENODEV; 21189b6b563cSPaul Mackerras } 21199b6b563cSPaul Mackerras 21209b6b563cSPaul Mackerras /* Find us in the parent and its childs */ 21219b6b563cSPaul Mackerras res = parent->child; 21229b6b563cSPaul Mackerras start = node->addrs[index].address; 21239b6b563cSPaul Mackerras end = start + node->addrs[index].size - 1; 21249b6b563cSPaul Mackerras while (res) { 21259b6b563cSPaul Mackerras if (res->start == start && res->end == end && 21269b6b563cSPaul Mackerras (res->flags & IORESOURCE_BUSY)) 21279b6b563cSPaul Mackerras break; 21289b6b563cSPaul Mackerras if (res->start <= start && res->end >= end) 21299b6b563cSPaul Mackerras res = res->child; 21309b6b563cSPaul Mackerras else 21319b6b563cSPaul Mackerras res = res->sibling; 21329b6b563cSPaul Mackerras } 21339b6b563cSPaul Mackerras if (!res) 21349b6b563cSPaul Mackerras return -ENODEV; 21359b6b563cSPaul Mackerras 21369b6b563cSPaul Mackerras if (res->name) { 21379b6b563cSPaul Mackerras kfree(res->name); 21389b6b563cSPaul Mackerras res->name = NULL; 21399b6b563cSPaul Mackerras } 21409b6b563cSPaul Mackerras release_resource(res); 21419b6b563cSPaul Mackerras kfree(res); 21429b6b563cSPaul Mackerras 21439b6b563cSPaul Mackerras return 0; 21449b6b563cSPaul Mackerras } 21459b6b563cSPaul Mackerras EXPORT_SYMBOL(release_OF_resource); 2146