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> 32dcee3036SMichael Ellerman #include <linux/kexec.h> 339b6b563cSPaul Mackerras 349b6b563cSPaul Mackerras #include <asm/prom.h> 359b6b563cSPaul Mackerras #include <asm/rtas.h> 369b6b563cSPaul Mackerras #include <asm/lmb.h> 379b6b563cSPaul Mackerras #include <asm/page.h> 389b6b563cSPaul Mackerras #include <asm/processor.h> 399b6b563cSPaul Mackerras #include <asm/irq.h> 409b6b563cSPaul Mackerras #include <asm/io.h> 410cc4746cSMichael Ellerman #include <asm/kdump.h> 429b6b563cSPaul Mackerras #include <asm/smp.h> 439b6b563cSPaul Mackerras #include <asm/system.h> 449b6b563cSPaul Mackerras #include <asm/mmu.h> 459b6b563cSPaul Mackerras #include <asm/pgtable.h> 469b6b563cSPaul Mackerras #include <asm/pci.h> 479b6b563cSPaul Mackerras #include <asm/iommu.h> 489b6b563cSPaul Mackerras #include <asm/btext.h> 499b6b563cSPaul Mackerras #include <asm/sections.h> 509b6b563cSPaul Mackerras #include <asm/machdep.h> 519b6b563cSPaul Mackerras #include <asm/pSeries_reconfig.h> 5240ef8cbcSPaul Mackerras #include <asm/pci-bridge.h> 539b6b563cSPaul Mackerras 549b6b563cSPaul Mackerras #ifdef DEBUG 559b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt) 569b6b563cSPaul Mackerras #else 579b6b563cSPaul Mackerras #define DBG(fmt...) 589b6b563cSPaul Mackerras #endif 599b6b563cSPaul Mackerras 609b6b563cSPaul Mackerras 619b6b563cSPaul Mackerras static int __initdata dt_root_addr_cells; 629b6b563cSPaul Mackerras static int __initdata dt_root_size_cells; 639b6b563cSPaul Mackerras 649b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 659b6b563cSPaul Mackerras static int __initdata iommu_is_off; 669b6b563cSPaul Mackerras int __initdata iommu_force_on; 67cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end; 689b6b563cSPaul Mackerras #endif 699b6b563cSPaul Mackerras 709b6b563cSPaul Mackerras typedef u32 cell_t; 719b6b563cSPaul Mackerras 729b6b563cSPaul Mackerras #if 0 739b6b563cSPaul Mackerras static struct boot_param_header *initial_boot_params __initdata; 749b6b563cSPaul Mackerras #else 759b6b563cSPaul Mackerras struct boot_param_header *initial_boot_params; 769b6b563cSPaul Mackerras #endif 779b6b563cSPaul Mackerras 789b6b563cSPaul Mackerras static struct device_node *allnodes = NULL; 799b6b563cSPaul Mackerras 809b6b563cSPaul Mackerras /* use when traversing tree through the allnext, child, sibling, 819b6b563cSPaul Mackerras * or parent members of struct device_node. 829b6b563cSPaul Mackerras */ 839b6b563cSPaul Mackerras static DEFINE_RWLOCK(devtree_lock); 849b6b563cSPaul Mackerras 859b6b563cSPaul Mackerras /* export that to outside world */ 869b6b563cSPaul Mackerras struct device_node *of_chosen; 879b6b563cSPaul Mackerras 889b6b563cSPaul Mackerras struct device_node *dflt_interrupt_controller; 899b6b563cSPaul Mackerras int num_interrupt_controllers; 909b6b563cSPaul Mackerras 919b6b563cSPaul Mackerras /* 929b6b563cSPaul Mackerras * Wrapper for allocating memory for various data that needs to be 939b6b563cSPaul Mackerras * attached to device nodes as they are processed at boot or when 949b6b563cSPaul Mackerras * added to the device tree later (e.g. DLPAR). At boot there is 959b6b563cSPaul Mackerras * already a region reserved so we just increment *mem_start by size; 969b6b563cSPaul Mackerras * otherwise we call kmalloc. 979b6b563cSPaul Mackerras */ 989b6b563cSPaul Mackerras static void * prom_alloc(unsigned long size, unsigned long *mem_start) 999b6b563cSPaul Mackerras { 1009b6b563cSPaul Mackerras unsigned long tmp; 1019b6b563cSPaul Mackerras 1029b6b563cSPaul Mackerras if (!mem_start) 1039b6b563cSPaul Mackerras return kmalloc(size, GFP_KERNEL); 1049b6b563cSPaul Mackerras 1059b6b563cSPaul Mackerras tmp = *mem_start; 1069b6b563cSPaul Mackerras *mem_start += size; 1079b6b563cSPaul Mackerras return (void *)tmp; 1089b6b563cSPaul Mackerras } 1099b6b563cSPaul Mackerras 1109b6b563cSPaul Mackerras /* 1119b6b563cSPaul Mackerras * Find the device_node with a given phandle. 1129b6b563cSPaul Mackerras */ 1139b6b563cSPaul Mackerras static struct device_node * find_phandle(phandle ph) 1149b6b563cSPaul Mackerras { 1159b6b563cSPaul Mackerras struct device_node *np; 1169b6b563cSPaul Mackerras 1179b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 1189b6b563cSPaul Mackerras if (np->linux_phandle == ph) 1199b6b563cSPaul Mackerras return np; 1209b6b563cSPaul Mackerras return NULL; 1219b6b563cSPaul Mackerras } 1229b6b563cSPaul Mackerras 1239b6b563cSPaul Mackerras /* 1249b6b563cSPaul Mackerras * Find the interrupt parent of a node. 1259b6b563cSPaul Mackerras */ 1269b6b563cSPaul Mackerras static struct device_node * __devinit intr_parent(struct device_node *p) 1279b6b563cSPaul Mackerras { 1289b6b563cSPaul Mackerras phandle *parp; 1299b6b563cSPaul Mackerras 1309b6b563cSPaul Mackerras parp = (phandle *) get_property(p, "interrupt-parent", NULL); 1319b6b563cSPaul Mackerras if (parp == NULL) 1329b6b563cSPaul Mackerras return p->parent; 1339b6b563cSPaul Mackerras p = find_phandle(*parp); 1349b6b563cSPaul Mackerras if (p != NULL) 1359b6b563cSPaul Mackerras return p; 1369b6b563cSPaul Mackerras /* 1379b6b563cSPaul Mackerras * On a powermac booted with BootX, we don't get to know the 1389b6b563cSPaul Mackerras * phandles for any nodes, so find_phandle will return NULL. 1399b6b563cSPaul Mackerras * Fortunately these machines only have one interrupt controller 1409b6b563cSPaul Mackerras * so there isn't in fact any ambiguity. -- paulus 1419b6b563cSPaul Mackerras */ 1429b6b563cSPaul Mackerras if (num_interrupt_controllers == 1) 1439b6b563cSPaul Mackerras p = dflt_interrupt_controller; 1449b6b563cSPaul Mackerras return p; 1459b6b563cSPaul Mackerras } 1469b6b563cSPaul Mackerras 1479b6b563cSPaul Mackerras /* 1489b6b563cSPaul Mackerras * Find out the size of each entry of the interrupts property 1499b6b563cSPaul Mackerras * for a node. 1509b6b563cSPaul Mackerras */ 1519b6b563cSPaul Mackerras int __devinit prom_n_intr_cells(struct device_node *np) 1529b6b563cSPaul Mackerras { 1539b6b563cSPaul Mackerras struct device_node *p; 1549b6b563cSPaul Mackerras unsigned int *icp; 1559b6b563cSPaul Mackerras 1569b6b563cSPaul Mackerras for (p = np; (p = intr_parent(p)) != NULL; ) { 1579b6b563cSPaul Mackerras icp = (unsigned int *) 1589b6b563cSPaul Mackerras get_property(p, "#interrupt-cells", NULL); 1599b6b563cSPaul Mackerras if (icp != NULL) 1609b6b563cSPaul Mackerras return *icp; 1619b6b563cSPaul Mackerras if (get_property(p, "interrupt-controller", NULL) != NULL 1629b6b563cSPaul Mackerras || get_property(p, "interrupt-map", NULL) != NULL) { 1639b6b563cSPaul Mackerras printk("oops, node %s doesn't have #interrupt-cells\n", 1649b6b563cSPaul Mackerras p->full_name); 1659b6b563cSPaul Mackerras return 1; 1669b6b563cSPaul Mackerras } 1679b6b563cSPaul Mackerras } 1689b6b563cSPaul Mackerras #ifdef DEBUG_IRQ 1699b6b563cSPaul Mackerras printk("prom_n_intr_cells failed for %s\n", np->full_name); 1709b6b563cSPaul Mackerras #endif 1719b6b563cSPaul Mackerras return 1; 1729b6b563cSPaul Mackerras } 1739b6b563cSPaul Mackerras 1749b6b563cSPaul Mackerras /* 1759b6b563cSPaul Mackerras * Map an interrupt from a device up to the platform interrupt 1769b6b563cSPaul Mackerras * descriptor. 1779b6b563cSPaul Mackerras */ 1789b6b563cSPaul Mackerras static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler, 1799b6b563cSPaul Mackerras struct device_node *np, unsigned int *ints, 1809b6b563cSPaul Mackerras int nintrc) 1819b6b563cSPaul Mackerras { 1829b6b563cSPaul Mackerras struct device_node *p, *ipar; 1839b6b563cSPaul Mackerras unsigned int *imap, *imask, *ip; 1849b6b563cSPaul Mackerras int i, imaplen, match; 1859b6b563cSPaul Mackerras int newintrc = 0, newaddrc = 0; 1869b6b563cSPaul Mackerras unsigned int *reg; 1879b6b563cSPaul Mackerras int naddrc; 1889b6b563cSPaul Mackerras 1899b6b563cSPaul Mackerras reg = (unsigned int *) get_property(np, "reg", NULL); 1909b6b563cSPaul Mackerras naddrc = prom_n_addr_cells(np); 1919b6b563cSPaul Mackerras p = intr_parent(np); 1929b6b563cSPaul Mackerras while (p != NULL) { 1939b6b563cSPaul Mackerras if (get_property(p, "interrupt-controller", NULL) != NULL) 1949b6b563cSPaul Mackerras /* this node is an interrupt controller, stop here */ 1959b6b563cSPaul Mackerras break; 1969b6b563cSPaul Mackerras imap = (unsigned int *) 1979b6b563cSPaul Mackerras get_property(p, "interrupt-map", &imaplen); 1989b6b563cSPaul Mackerras if (imap == NULL) { 1999b6b563cSPaul Mackerras p = intr_parent(p); 2009b6b563cSPaul Mackerras continue; 2019b6b563cSPaul Mackerras } 2029b6b563cSPaul Mackerras imask = (unsigned int *) 2039b6b563cSPaul Mackerras get_property(p, "interrupt-map-mask", NULL); 2049b6b563cSPaul Mackerras if (imask == NULL) { 2059b6b563cSPaul Mackerras printk("oops, %s has interrupt-map but no mask\n", 2069b6b563cSPaul Mackerras p->full_name); 2079b6b563cSPaul Mackerras return 0; 2089b6b563cSPaul Mackerras } 2099b6b563cSPaul Mackerras imaplen /= sizeof(unsigned int); 2109b6b563cSPaul Mackerras match = 0; 2119b6b563cSPaul Mackerras ipar = NULL; 2129b6b563cSPaul Mackerras while (imaplen > 0 && !match) { 2139b6b563cSPaul Mackerras /* check the child-interrupt field */ 2149b6b563cSPaul Mackerras match = 1; 2159b6b563cSPaul Mackerras for (i = 0; i < naddrc && match; ++i) 2169b6b563cSPaul Mackerras match = ((reg[i] ^ imap[i]) & imask[i]) == 0; 2179b6b563cSPaul Mackerras for (; i < naddrc + nintrc && match; ++i) 2189b6b563cSPaul Mackerras match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0; 2199b6b563cSPaul Mackerras imap += naddrc + nintrc; 2209b6b563cSPaul Mackerras imaplen -= naddrc + nintrc; 2219b6b563cSPaul Mackerras /* grab the interrupt parent */ 2229b6b563cSPaul Mackerras ipar = find_phandle((phandle) *imap++); 2239b6b563cSPaul Mackerras --imaplen; 2249b6b563cSPaul Mackerras if (ipar == NULL && num_interrupt_controllers == 1) 2259b6b563cSPaul Mackerras /* cope with BootX not giving us phandles */ 2269b6b563cSPaul Mackerras ipar = dflt_interrupt_controller; 2279b6b563cSPaul Mackerras if (ipar == NULL) { 2289b6b563cSPaul Mackerras printk("oops, no int parent %x in map of %s\n", 2299b6b563cSPaul Mackerras imap[-1], p->full_name); 2309b6b563cSPaul Mackerras return 0; 2319b6b563cSPaul Mackerras } 2329b6b563cSPaul Mackerras /* find the parent's # addr and intr cells */ 2339b6b563cSPaul Mackerras ip = (unsigned int *) 2349b6b563cSPaul Mackerras get_property(ipar, "#interrupt-cells", NULL); 2359b6b563cSPaul Mackerras if (ip == NULL) { 2369b6b563cSPaul Mackerras printk("oops, no #interrupt-cells on %s\n", 2379b6b563cSPaul Mackerras ipar->full_name); 2389b6b563cSPaul Mackerras return 0; 2399b6b563cSPaul Mackerras } 2409b6b563cSPaul Mackerras newintrc = *ip; 2419b6b563cSPaul Mackerras ip = (unsigned int *) 2429b6b563cSPaul Mackerras get_property(ipar, "#address-cells", NULL); 2439b6b563cSPaul Mackerras newaddrc = (ip == NULL)? 0: *ip; 2449b6b563cSPaul Mackerras imap += newaddrc + newintrc; 2459b6b563cSPaul Mackerras imaplen -= newaddrc + newintrc; 2469b6b563cSPaul Mackerras } 2479b6b563cSPaul Mackerras if (imaplen < 0) { 2489b6b563cSPaul Mackerras printk("oops, error decoding int-map on %s, len=%d\n", 2499b6b563cSPaul Mackerras p->full_name, imaplen); 2509b6b563cSPaul Mackerras return 0; 2519b6b563cSPaul Mackerras } 2529b6b563cSPaul Mackerras if (!match) { 2539b6b563cSPaul Mackerras #ifdef DEBUG_IRQ 2549b6b563cSPaul Mackerras printk("oops, no match in %s int-map for %s\n", 2559b6b563cSPaul Mackerras p->full_name, np->full_name); 2569b6b563cSPaul Mackerras #endif 2579b6b563cSPaul Mackerras return 0; 2589b6b563cSPaul Mackerras } 2599b6b563cSPaul Mackerras p = ipar; 2609b6b563cSPaul Mackerras naddrc = newaddrc; 2619b6b563cSPaul Mackerras nintrc = newintrc; 2629b6b563cSPaul Mackerras ints = imap - nintrc; 2639b6b563cSPaul Mackerras reg = ints - naddrc; 2649b6b563cSPaul Mackerras } 2659b6b563cSPaul Mackerras if (p == NULL) { 2669b6b563cSPaul Mackerras #ifdef DEBUG_IRQ 2679b6b563cSPaul Mackerras printk("hmmm, int tree for %s doesn't have ctrler\n", 2689b6b563cSPaul Mackerras np->full_name); 2699b6b563cSPaul Mackerras #endif 2709b6b563cSPaul Mackerras return 0; 2719b6b563cSPaul Mackerras } 2729b6b563cSPaul Mackerras *irq = ints; 2739b6b563cSPaul Mackerras *ictrler = p; 2749b6b563cSPaul Mackerras return nintrc; 2759b6b563cSPaul Mackerras } 2769b6b563cSPaul Mackerras 2776d0124fcSPaul Mackerras static unsigned char map_isa_senses[4] = { 2786d0124fcSPaul Mackerras IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, 2796d0124fcSPaul Mackerras IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE, 2806d0124fcSPaul Mackerras IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE, 2816d0124fcSPaul Mackerras IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE 2826d0124fcSPaul Mackerras }; 2836d0124fcSPaul Mackerras 2846d0124fcSPaul Mackerras static unsigned char map_mpic_senses[4] = { 2856d0124fcSPaul Mackerras IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE, 2866d0124fcSPaul Mackerras IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, 2876d0124fcSPaul Mackerras /* 2 seems to be used for the 8259 cascade... */ 2886d0124fcSPaul Mackerras IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE, 2896d0124fcSPaul Mackerras IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE, 2906d0124fcSPaul Mackerras }; 2916d0124fcSPaul Mackerras 2929b6b563cSPaul Mackerras static int __devinit finish_node_interrupts(struct device_node *np, 2939b6b563cSPaul Mackerras unsigned long *mem_start, 2949b6b563cSPaul Mackerras int measure_only) 2959b6b563cSPaul Mackerras { 2969b6b563cSPaul Mackerras unsigned int *ints; 2979b6b563cSPaul Mackerras int intlen, intrcells, intrcount; 2986d0124fcSPaul Mackerras int i, j, n, sense; 2999b6b563cSPaul Mackerras unsigned int *irq, virq; 3009b6b563cSPaul Mackerras struct device_node *ic; 3011beb6a7dSBenjamin Herrenschmidt int trace = 0; 3021beb6a7dSBenjamin Herrenschmidt 3031beb6a7dSBenjamin Herrenschmidt //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0) 3041beb6a7dSBenjamin Herrenschmidt #define TRACE(fmt...) 3051beb6a7dSBenjamin Herrenschmidt 3061beb6a7dSBenjamin Herrenschmidt if (!strcmp(np->name, "smu-doorbell")) 3071beb6a7dSBenjamin Herrenschmidt trace = 1; 3081beb6a7dSBenjamin Herrenschmidt 3091beb6a7dSBenjamin Herrenschmidt TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n", 3101beb6a7dSBenjamin Herrenschmidt num_interrupt_controllers); 3119b6b563cSPaul Mackerras 312a575b807SPaul Mackerras if (num_interrupt_controllers == 0) { 313a575b807SPaul Mackerras /* 314a575b807SPaul Mackerras * Old machines just have a list of interrupt numbers 315a575b807SPaul Mackerras * and no interrupt-controller nodes. 316a575b807SPaul Mackerras */ 317a575b807SPaul Mackerras ints = (unsigned int *) get_property(np, "AAPL,interrupts", 318a575b807SPaul Mackerras &intlen); 319a575b807SPaul Mackerras /* XXX old interpret_pci_props looked in parent too */ 320a575b807SPaul Mackerras /* XXX old interpret_macio_props looked for interrupts 321a575b807SPaul Mackerras before AAPL,interrupts */ 322a575b807SPaul Mackerras if (ints == NULL) 323a575b807SPaul Mackerras ints = (unsigned int *) get_property(np, "interrupts", 324a575b807SPaul Mackerras &intlen); 325a575b807SPaul Mackerras if (ints == NULL) 326a575b807SPaul Mackerras return 0; 327a575b807SPaul Mackerras 328a575b807SPaul Mackerras np->n_intrs = intlen / sizeof(unsigned int); 329a575b807SPaul Mackerras np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]), 330a575b807SPaul Mackerras mem_start); 331a575b807SPaul Mackerras if (!np->intrs) 332a575b807SPaul Mackerras return -ENOMEM; 333a575b807SPaul Mackerras if (measure_only) 334a575b807SPaul Mackerras return 0; 335a575b807SPaul Mackerras 336a575b807SPaul Mackerras for (i = 0; i < np->n_intrs; ++i) { 337a575b807SPaul Mackerras np->intrs[i].line = *ints++; 3386d0124fcSPaul Mackerras np->intrs[i].sense = IRQ_SENSE_LEVEL 3396d0124fcSPaul Mackerras | IRQ_POLARITY_NEGATIVE; 340a575b807SPaul Mackerras } 341a575b807SPaul Mackerras return 0; 342a575b807SPaul Mackerras } 343a575b807SPaul Mackerras 3449b6b563cSPaul Mackerras ints = (unsigned int *) get_property(np, "interrupts", &intlen); 3451beb6a7dSBenjamin Herrenschmidt TRACE("ints=%p, intlen=%d\n", ints, intlen); 3469b6b563cSPaul Mackerras if (ints == NULL) 3479b6b563cSPaul Mackerras return 0; 3489b6b563cSPaul Mackerras intrcells = prom_n_intr_cells(np); 3499b6b563cSPaul Mackerras intlen /= intrcells * sizeof(unsigned int); 3501beb6a7dSBenjamin Herrenschmidt TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen); 3519b6b563cSPaul Mackerras np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start); 3529b6b563cSPaul Mackerras if (!np->intrs) 3539b6b563cSPaul Mackerras return -ENOMEM; 3549b6b563cSPaul Mackerras 3559b6b563cSPaul Mackerras if (measure_only) 3569b6b563cSPaul Mackerras return 0; 3579b6b563cSPaul Mackerras 3589b6b563cSPaul Mackerras intrcount = 0; 3599b6b563cSPaul Mackerras for (i = 0; i < intlen; ++i, ints += intrcells) { 3609b6b563cSPaul Mackerras n = map_interrupt(&irq, &ic, np, ints, intrcells); 3611beb6a7dSBenjamin Herrenschmidt TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n); 3629b6b563cSPaul Mackerras if (n <= 0) 3639b6b563cSPaul Mackerras continue; 3649b6b563cSPaul Mackerras 3659b6b563cSPaul Mackerras /* don't map IRQ numbers under a cascaded 8259 controller */ 3669b6b563cSPaul Mackerras if (ic && device_is_compatible(ic, "chrp,iic")) { 3679b6b563cSPaul Mackerras np->intrs[intrcount].line = irq[0]; 3686d0124fcSPaul Mackerras sense = (n > 1)? (irq[1] & 3): 3; 3696d0124fcSPaul Mackerras np->intrs[intrcount].sense = map_isa_senses[sense]; 3709b6b563cSPaul Mackerras } else { 3719b6b563cSPaul Mackerras virq = virt_irq_create_mapping(irq[0]); 3721beb6a7dSBenjamin Herrenschmidt TRACE("virq=%d\n", virq); 3736d0124fcSPaul Mackerras #ifdef CONFIG_PPC64 3749b6b563cSPaul Mackerras if (virq == NO_IRQ) { 3759b6b563cSPaul Mackerras printk(KERN_CRIT "Could not allocate interrupt" 3769b6b563cSPaul Mackerras " number for %s\n", np->full_name); 3779b6b563cSPaul Mackerras continue; 3789b6b563cSPaul Mackerras } 3799b6b563cSPaul Mackerras #endif 3806d0124fcSPaul Mackerras np->intrs[intrcount].line = irq_offset_up(virq); 3816d0124fcSPaul Mackerras sense = (n > 1)? (irq[1] & 3): 1; 3821beb6a7dSBenjamin Herrenschmidt 3831beb6a7dSBenjamin Herrenschmidt /* Apple uses bits in there in a different way, let's 3841beb6a7dSBenjamin Herrenschmidt * only keep the real sense bit on macs 3851beb6a7dSBenjamin Herrenschmidt */ 3861beb6a7dSBenjamin Herrenschmidt if (_machine == PLATFORM_POWERMAC) 3871beb6a7dSBenjamin Herrenschmidt sense &= 0x1; 3886d0124fcSPaul Mackerras np->intrs[intrcount].sense = map_mpic_senses[sense]; 3899b6b563cSPaul Mackerras } 3909b6b563cSPaul Mackerras 3919b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 3929b6b563cSPaul Mackerras /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */ 393799d6046SPaul Mackerras if (_machine == PLATFORM_POWERMAC && ic && ic->parent) { 3949b6b563cSPaul Mackerras char *name = get_property(ic->parent, "name", NULL); 3959b6b563cSPaul Mackerras if (name && !strcmp(name, "u3")) 3969b6b563cSPaul Mackerras np->intrs[intrcount].line += 128; 3971beb6a7dSBenjamin Herrenschmidt else if (!(name && (!strcmp(name, "mac-io") || 3981beb6a7dSBenjamin Herrenschmidt !strcmp(name, "u4")))) 3999b6b563cSPaul Mackerras /* ignore other cascaded controllers, such as 4009b6b563cSPaul Mackerras the k2-sata-root */ 4019b6b563cSPaul Mackerras break; 4029b6b563cSPaul Mackerras } 4031beb6a7dSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */ 4049b6b563cSPaul Mackerras if (n > 2) { 4059b6b563cSPaul Mackerras printk("hmmm, got %d intr cells for %s:", n, 4069b6b563cSPaul Mackerras np->full_name); 4079b6b563cSPaul Mackerras for (j = 0; j < n; ++j) 4089b6b563cSPaul Mackerras printk(" %d", irq[j]); 4099b6b563cSPaul Mackerras printk("\n"); 4109b6b563cSPaul Mackerras } 4119b6b563cSPaul Mackerras ++intrcount; 4129b6b563cSPaul Mackerras } 4139b6b563cSPaul Mackerras np->n_intrs = intrcount; 4149b6b563cSPaul Mackerras 4159b6b563cSPaul Mackerras return 0; 4169b6b563cSPaul Mackerras } 4179b6b563cSPaul Mackerras 4189b6b563cSPaul Mackerras static int __devinit finish_node(struct device_node *np, 4199b6b563cSPaul Mackerras unsigned long *mem_start, 4209b6b563cSPaul Mackerras int measure_only) 4219b6b563cSPaul Mackerras { 4229b6b563cSPaul Mackerras struct device_node *child; 423cc5d0189SBenjamin Herrenschmidt int rc = 0; 4249b6b563cSPaul Mackerras 4259b6b563cSPaul Mackerras rc = finish_node_interrupts(np, mem_start, measure_only); 4269b6b563cSPaul Mackerras if (rc) 4279b6b563cSPaul Mackerras goto out; 4289b6b563cSPaul Mackerras 4299b6b563cSPaul Mackerras for (child = np->child; child != NULL; child = child->sibling) { 430cc5d0189SBenjamin Herrenschmidt rc = finish_node(child, mem_start, measure_only); 4319b6b563cSPaul Mackerras if (rc) 4329b6b563cSPaul Mackerras goto out; 4339b6b563cSPaul Mackerras } 4349b6b563cSPaul Mackerras out: 4359b6b563cSPaul Mackerras return rc; 4369b6b563cSPaul Mackerras } 4379b6b563cSPaul Mackerras 4389b6b563cSPaul Mackerras static void __init scan_interrupt_controllers(void) 4399b6b563cSPaul Mackerras { 4409b6b563cSPaul Mackerras struct device_node *np; 4419b6b563cSPaul Mackerras int n = 0; 4429b6b563cSPaul Mackerras char *name, *ic; 4439b6b563cSPaul Mackerras int iclen; 4449b6b563cSPaul Mackerras 4459b6b563cSPaul Mackerras for (np = allnodes; np != NULL; np = np->allnext) { 4469b6b563cSPaul Mackerras ic = get_property(np, "interrupt-controller", &iclen); 4479b6b563cSPaul Mackerras name = get_property(np, "name", NULL); 4489b6b563cSPaul Mackerras /* checking iclen makes sure we don't get a false 4499b6b563cSPaul Mackerras match on /chosen.interrupt_controller */ 4509b6b563cSPaul Mackerras if ((name != NULL 4519b6b563cSPaul Mackerras && strcmp(name, "interrupt-controller") == 0) 4529b6b563cSPaul Mackerras || (ic != NULL && iclen == 0 4539b6b563cSPaul Mackerras && strcmp(name, "AppleKiwi"))) { 4549b6b563cSPaul Mackerras if (n == 0) 4559b6b563cSPaul Mackerras dflt_interrupt_controller = np; 4569b6b563cSPaul Mackerras ++n; 4579b6b563cSPaul Mackerras } 4589b6b563cSPaul Mackerras } 4599b6b563cSPaul Mackerras num_interrupt_controllers = n; 4609b6b563cSPaul Mackerras } 4619b6b563cSPaul Mackerras 4629b6b563cSPaul Mackerras /** 4639b6b563cSPaul Mackerras * finish_device_tree is called once things are running normally 4649b6b563cSPaul Mackerras * (i.e. with text and data mapped to the address they were linked at). 4659b6b563cSPaul Mackerras * It traverses the device tree and fills in some of the additional, 4669b6b563cSPaul Mackerras * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt 4679b6b563cSPaul Mackerras * mapping is also initialized at this point. 4689b6b563cSPaul Mackerras */ 4699b6b563cSPaul Mackerras void __init finish_device_tree(void) 4709b6b563cSPaul Mackerras { 4719b6b563cSPaul Mackerras unsigned long start, end, size = 0; 4729b6b563cSPaul Mackerras 4739b6b563cSPaul Mackerras DBG(" -> finish_device_tree\n"); 4749b6b563cSPaul Mackerras 4759b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 4769b6b563cSPaul Mackerras /* Initialize virtual IRQ map */ 4779b6b563cSPaul Mackerras virt_irq_init(); 4789b6b563cSPaul Mackerras #endif 4799b6b563cSPaul Mackerras scan_interrupt_controllers(); 4809b6b563cSPaul Mackerras 4819b6b563cSPaul Mackerras /* 4829b6b563cSPaul Mackerras * Finish device-tree (pre-parsing some properties etc...) 4839b6b563cSPaul Mackerras * We do this in 2 passes. One with "measure_only" set, which 4849b6b563cSPaul Mackerras * will only measure the amount of memory needed, then we can 4859b6b563cSPaul Mackerras * allocate that memory, and call finish_node again. However, 4869b6b563cSPaul Mackerras * we must be careful as most routines will fail nowadays when 4879b6b563cSPaul Mackerras * prom_alloc() returns 0, so we must make sure our first pass 4889b6b563cSPaul Mackerras * doesn't start at 0. We pre-initialize size to 16 for that 4899b6b563cSPaul Mackerras * reason and then remove those additional 16 bytes 4909b6b563cSPaul Mackerras */ 4919b6b563cSPaul Mackerras size = 16; 492cc5d0189SBenjamin Herrenschmidt finish_node(allnodes, &size, 1); 4939b6b563cSPaul Mackerras size -= 16; 4949b6b563cSPaul Mackerras end = start = (unsigned long) __va(lmb_alloc(size, 128)); 495cc5d0189SBenjamin Herrenschmidt finish_node(allnodes, &end, 0); 4969b6b563cSPaul Mackerras BUG_ON(end != start + size); 4979b6b563cSPaul Mackerras 4989b6b563cSPaul Mackerras DBG(" <- finish_device_tree\n"); 4999b6b563cSPaul Mackerras } 5009b6b563cSPaul Mackerras 5019b6b563cSPaul Mackerras static inline char *find_flat_dt_string(u32 offset) 5029b6b563cSPaul Mackerras { 5039b6b563cSPaul Mackerras return ((char *)initial_boot_params) + 5049b6b563cSPaul Mackerras initial_boot_params->off_dt_strings + offset; 5059b6b563cSPaul Mackerras } 5069b6b563cSPaul Mackerras 5079b6b563cSPaul Mackerras /** 5089b6b563cSPaul Mackerras * This function is used to scan the flattened device-tree, it is 5099b6b563cSPaul Mackerras * used to extract the memory informations at boot before we can 5109b6b563cSPaul Mackerras * unflatten the tree 5119b6b563cSPaul Mackerras */ 5123c726f8dSBenjamin Herrenschmidt int __init of_scan_flat_dt(int (*it)(unsigned long node, 5139b6b563cSPaul Mackerras const char *uname, int depth, 5149b6b563cSPaul Mackerras void *data), 5159b6b563cSPaul Mackerras void *data) 5169b6b563cSPaul Mackerras { 5179b6b563cSPaul Mackerras unsigned long p = ((unsigned long)initial_boot_params) + 5189b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 5199b6b563cSPaul Mackerras int rc = 0; 5209b6b563cSPaul Mackerras int depth = -1; 5219b6b563cSPaul Mackerras 5229b6b563cSPaul Mackerras do { 5239b6b563cSPaul Mackerras u32 tag = *((u32 *)p); 5249b6b563cSPaul Mackerras char *pathp; 5259b6b563cSPaul Mackerras 5269b6b563cSPaul Mackerras p += 4; 5279b6b563cSPaul Mackerras if (tag == OF_DT_END_NODE) { 5289b6b563cSPaul Mackerras depth --; 5299b6b563cSPaul Mackerras continue; 5309b6b563cSPaul Mackerras } 5319b6b563cSPaul Mackerras if (tag == OF_DT_NOP) 5329b6b563cSPaul Mackerras continue; 5339b6b563cSPaul Mackerras if (tag == OF_DT_END) 5349b6b563cSPaul Mackerras break; 5359b6b563cSPaul Mackerras if (tag == OF_DT_PROP) { 5369b6b563cSPaul Mackerras u32 sz = *((u32 *)p); 5379b6b563cSPaul Mackerras p += 8; 5389b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 5399b6b563cSPaul Mackerras p = _ALIGN(p, sz >= 8 ? 8 : 4); 5409b6b563cSPaul Mackerras p += sz; 5419b6b563cSPaul Mackerras p = _ALIGN(p, 4); 5429b6b563cSPaul Mackerras continue; 5439b6b563cSPaul Mackerras } 5449b6b563cSPaul Mackerras if (tag != OF_DT_BEGIN_NODE) { 5459b6b563cSPaul Mackerras printk(KERN_WARNING "Invalid tag %x scanning flattened" 5469b6b563cSPaul Mackerras " device tree !\n", tag); 5479b6b563cSPaul Mackerras return -EINVAL; 5489b6b563cSPaul Mackerras } 5499b6b563cSPaul Mackerras depth++; 5509b6b563cSPaul Mackerras pathp = (char *)p; 5519b6b563cSPaul Mackerras p = _ALIGN(p + strlen(pathp) + 1, 4); 5529b6b563cSPaul Mackerras if ((*pathp) == '/') { 5539b6b563cSPaul Mackerras char *lp, *np; 5549b6b563cSPaul Mackerras for (lp = NULL, np = pathp; *np; np++) 5559b6b563cSPaul Mackerras if ((*np) == '/') 5569b6b563cSPaul Mackerras lp = np+1; 5579b6b563cSPaul Mackerras if (lp != NULL) 5589b6b563cSPaul Mackerras pathp = lp; 5599b6b563cSPaul Mackerras } 5609b6b563cSPaul Mackerras rc = it(p, pathp, depth, data); 5619b6b563cSPaul Mackerras if (rc != 0) 5629b6b563cSPaul Mackerras break; 5639b6b563cSPaul Mackerras } while(1); 5649b6b563cSPaul Mackerras 5659b6b563cSPaul Mackerras return rc; 5669b6b563cSPaul Mackerras } 5679b6b563cSPaul Mackerras 5689b6b563cSPaul Mackerras /** 5699b6b563cSPaul Mackerras * This function can be used within scan_flattened_dt callback to get 5709b6b563cSPaul Mackerras * access to properties 5719b6b563cSPaul Mackerras */ 5723c726f8dSBenjamin Herrenschmidt void* __init of_get_flat_dt_prop(unsigned long node, const char *name, 5739b6b563cSPaul Mackerras unsigned long *size) 5749b6b563cSPaul Mackerras { 5759b6b563cSPaul Mackerras unsigned long p = node; 5769b6b563cSPaul Mackerras 5779b6b563cSPaul Mackerras do { 5789b6b563cSPaul Mackerras u32 tag = *((u32 *)p); 5799b6b563cSPaul Mackerras u32 sz, noff; 5809b6b563cSPaul Mackerras const char *nstr; 5819b6b563cSPaul Mackerras 5829b6b563cSPaul Mackerras p += 4; 5839b6b563cSPaul Mackerras if (tag == OF_DT_NOP) 5849b6b563cSPaul Mackerras continue; 5859b6b563cSPaul Mackerras if (tag != OF_DT_PROP) 5869b6b563cSPaul Mackerras return NULL; 5879b6b563cSPaul Mackerras 5889b6b563cSPaul Mackerras sz = *((u32 *)p); 5899b6b563cSPaul Mackerras noff = *((u32 *)(p + 4)); 5909b6b563cSPaul Mackerras p += 8; 5919b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 5929b6b563cSPaul Mackerras p = _ALIGN(p, sz >= 8 ? 8 : 4); 5939b6b563cSPaul Mackerras 5949b6b563cSPaul Mackerras nstr = find_flat_dt_string(noff); 5959b6b563cSPaul Mackerras if (nstr == NULL) { 5969b6b563cSPaul Mackerras printk(KERN_WARNING "Can't find property index" 5979b6b563cSPaul Mackerras " name !\n"); 5989b6b563cSPaul Mackerras return NULL; 5999b6b563cSPaul Mackerras } 6009b6b563cSPaul Mackerras if (strcmp(name, nstr) == 0) { 6019b6b563cSPaul Mackerras if (size) 6029b6b563cSPaul Mackerras *size = sz; 6039b6b563cSPaul Mackerras return (void *)p; 6049b6b563cSPaul Mackerras } 6059b6b563cSPaul Mackerras p += sz; 6069b6b563cSPaul Mackerras p = _ALIGN(p, 4); 6079b6b563cSPaul Mackerras } while(1); 6089b6b563cSPaul Mackerras } 6099b6b563cSPaul Mackerras 6109b6b563cSPaul Mackerras static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size, 6119b6b563cSPaul Mackerras unsigned long align) 6129b6b563cSPaul Mackerras { 6139b6b563cSPaul Mackerras void *res; 6149b6b563cSPaul Mackerras 6159b6b563cSPaul Mackerras *mem = _ALIGN(*mem, align); 6169b6b563cSPaul Mackerras res = (void *)*mem; 6179b6b563cSPaul Mackerras *mem += size; 6189b6b563cSPaul Mackerras 6199b6b563cSPaul Mackerras return res; 6209b6b563cSPaul Mackerras } 6219b6b563cSPaul Mackerras 6229b6b563cSPaul Mackerras static unsigned long __init unflatten_dt_node(unsigned long mem, 6239b6b563cSPaul Mackerras unsigned long *p, 6249b6b563cSPaul Mackerras struct device_node *dad, 6259b6b563cSPaul Mackerras struct device_node ***allnextpp, 6269b6b563cSPaul Mackerras unsigned long fpsize) 6279b6b563cSPaul Mackerras { 6289b6b563cSPaul Mackerras struct device_node *np; 6299b6b563cSPaul Mackerras struct property *pp, **prev_pp = NULL; 6309b6b563cSPaul Mackerras char *pathp; 6319b6b563cSPaul Mackerras u32 tag; 6329b6b563cSPaul Mackerras unsigned int l, allocl; 6339b6b563cSPaul Mackerras int has_name = 0; 6349b6b563cSPaul Mackerras int new_format = 0; 6359b6b563cSPaul Mackerras 6369b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 6379b6b563cSPaul Mackerras if (tag != OF_DT_BEGIN_NODE) { 6389b6b563cSPaul Mackerras printk("Weird tag at start of node: %x\n", tag); 6399b6b563cSPaul Mackerras return mem; 6409b6b563cSPaul Mackerras } 6419b6b563cSPaul Mackerras *p += 4; 6429b6b563cSPaul Mackerras pathp = (char *)*p; 6439b6b563cSPaul Mackerras l = allocl = strlen(pathp) + 1; 6449b6b563cSPaul Mackerras *p = _ALIGN(*p + l, 4); 6459b6b563cSPaul Mackerras 6469b6b563cSPaul Mackerras /* version 0x10 has a more compact unit name here instead of the full 6479b6b563cSPaul Mackerras * path. we accumulate the full path size using "fpsize", we'll rebuild 6489b6b563cSPaul Mackerras * it later. We detect this because the first character of the name is 6499b6b563cSPaul Mackerras * not '/'. 6509b6b563cSPaul Mackerras */ 6519b6b563cSPaul Mackerras if ((*pathp) != '/') { 6529b6b563cSPaul Mackerras new_format = 1; 6539b6b563cSPaul Mackerras if (fpsize == 0) { 6549b6b563cSPaul Mackerras /* root node: special case. fpsize accounts for path 6559b6b563cSPaul Mackerras * plus terminating zero. root node only has '/', so 6569b6b563cSPaul Mackerras * fpsize should be 2, but we want to avoid the first 6579b6b563cSPaul Mackerras * level nodes to have two '/' so we use fpsize 1 here 6589b6b563cSPaul Mackerras */ 6599b6b563cSPaul Mackerras fpsize = 1; 6609b6b563cSPaul Mackerras allocl = 2; 6619b6b563cSPaul Mackerras } else { 6629b6b563cSPaul Mackerras /* account for '/' and path size minus terminal 0 6639b6b563cSPaul Mackerras * already in 'l' 6649b6b563cSPaul Mackerras */ 6659b6b563cSPaul Mackerras fpsize += l; 6669b6b563cSPaul Mackerras allocl = fpsize; 6679b6b563cSPaul Mackerras } 6689b6b563cSPaul Mackerras } 6699b6b563cSPaul Mackerras 6709b6b563cSPaul Mackerras 6719b6b563cSPaul Mackerras np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl, 6729b6b563cSPaul Mackerras __alignof__(struct device_node)); 6739b6b563cSPaul Mackerras if (allnextpp) { 6749b6b563cSPaul Mackerras memset(np, 0, sizeof(*np)); 6759b6b563cSPaul Mackerras np->full_name = ((char*)np) + sizeof(struct device_node); 6769b6b563cSPaul Mackerras if (new_format) { 6779b6b563cSPaul Mackerras char *p = np->full_name; 6789b6b563cSPaul Mackerras /* rebuild full path for new format */ 6799b6b563cSPaul Mackerras if (dad && dad->parent) { 6809b6b563cSPaul Mackerras strcpy(p, dad->full_name); 6819b6b563cSPaul Mackerras #ifdef DEBUG 6829b6b563cSPaul Mackerras if ((strlen(p) + l + 1) != allocl) { 6839b6b563cSPaul Mackerras DBG("%s: p: %d, l: %d, a: %d\n", 6849b6b563cSPaul Mackerras pathp, strlen(p), l, allocl); 6859b6b563cSPaul Mackerras } 6869b6b563cSPaul Mackerras #endif 6879b6b563cSPaul Mackerras p += strlen(p); 6889b6b563cSPaul Mackerras } 6899b6b563cSPaul Mackerras *(p++) = '/'; 6909b6b563cSPaul Mackerras memcpy(p, pathp, l); 6919b6b563cSPaul Mackerras } else 6929b6b563cSPaul Mackerras memcpy(np->full_name, pathp, l); 6939b6b563cSPaul Mackerras prev_pp = &np->properties; 6949b6b563cSPaul Mackerras **allnextpp = np; 6959b6b563cSPaul Mackerras *allnextpp = &np->allnext; 6969b6b563cSPaul Mackerras if (dad != NULL) { 6979b6b563cSPaul Mackerras np->parent = dad; 6989b6b563cSPaul Mackerras /* we temporarily use the next field as `last_child'*/ 6999b6b563cSPaul Mackerras if (dad->next == 0) 7009b6b563cSPaul Mackerras dad->child = np; 7019b6b563cSPaul Mackerras else 7029b6b563cSPaul Mackerras dad->next->sibling = np; 7039b6b563cSPaul Mackerras dad->next = np; 7049b6b563cSPaul Mackerras } 7059b6b563cSPaul Mackerras kref_init(&np->kref); 7069b6b563cSPaul Mackerras } 7079b6b563cSPaul Mackerras while(1) { 7089b6b563cSPaul Mackerras u32 sz, noff; 7099b6b563cSPaul Mackerras char *pname; 7109b6b563cSPaul Mackerras 7119b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 7129b6b563cSPaul Mackerras if (tag == OF_DT_NOP) { 7139b6b563cSPaul Mackerras *p += 4; 7149b6b563cSPaul Mackerras continue; 7159b6b563cSPaul Mackerras } 7169b6b563cSPaul Mackerras if (tag != OF_DT_PROP) 7179b6b563cSPaul Mackerras break; 7189b6b563cSPaul Mackerras *p += 4; 7199b6b563cSPaul Mackerras sz = *((u32 *)(*p)); 7209b6b563cSPaul Mackerras noff = *((u32 *)((*p) + 4)); 7219b6b563cSPaul Mackerras *p += 8; 7229b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 7239b6b563cSPaul Mackerras *p = _ALIGN(*p, sz >= 8 ? 8 : 4); 7249b6b563cSPaul Mackerras 7259b6b563cSPaul Mackerras pname = find_flat_dt_string(noff); 7269b6b563cSPaul Mackerras if (pname == NULL) { 7279b6b563cSPaul Mackerras printk("Can't find property name in list !\n"); 7289b6b563cSPaul Mackerras break; 7299b6b563cSPaul Mackerras } 7309b6b563cSPaul Mackerras if (strcmp(pname, "name") == 0) 7319b6b563cSPaul Mackerras has_name = 1; 7329b6b563cSPaul Mackerras l = strlen(pname) + 1; 7339b6b563cSPaul Mackerras pp = unflatten_dt_alloc(&mem, sizeof(struct property), 7349b6b563cSPaul Mackerras __alignof__(struct property)); 7359b6b563cSPaul Mackerras if (allnextpp) { 7369b6b563cSPaul Mackerras if (strcmp(pname, "linux,phandle") == 0) { 7379b6b563cSPaul Mackerras np->node = *((u32 *)*p); 7389b6b563cSPaul Mackerras if (np->linux_phandle == 0) 7399b6b563cSPaul Mackerras np->linux_phandle = np->node; 7409b6b563cSPaul Mackerras } 7419b6b563cSPaul Mackerras if (strcmp(pname, "ibm,phandle") == 0) 7429b6b563cSPaul Mackerras np->linux_phandle = *((u32 *)*p); 7439b6b563cSPaul Mackerras pp->name = pname; 7449b6b563cSPaul Mackerras pp->length = sz; 7459b6b563cSPaul Mackerras pp->value = (void *)*p; 7469b6b563cSPaul Mackerras *prev_pp = pp; 7479b6b563cSPaul Mackerras prev_pp = &pp->next; 7489b6b563cSPaul Mackerras } 7499b6b563cSPaul Mackerras *p = _ALIGN((*p) + sz, 4); 7509b6b563cSPaul Mackerras } 7519b6b563cSPaul Mackerras /* with version 0x10 we may not have the name property, recreate 7529b6b563cSPaul Mackerras * it here from the unit name if absent 7539b6b563cSPaul Mackerras */ 7549b6b563cSPaul Mackerras if (!has_name) { 7559b6b563cSPaul Mackerras char *p = pathp, *ps = pathp, *pa = NULL; 7569b6b563cSPaul Mackerras int sz; 7579b6b563cSPaul Mackerras 7589b6b563cSPaul Mackerras while (*p) { 7599b6b563cSPaul Mackerras if ((*p) == '@') 7609b6b563cSPaul Mackerras pa = p; 7619b6b563cSPaul Mackerras if ((*p) == '/') 7629b6b563cSPaul Mackerras ps = p + 1; 7639b6b563cSPaul Mackerras p++; 7649b6b563cSPaul Mackerras } 7659b6b563cSPaul Mackerras if (pa < ps) 7669b6b563cSPaul Mackerras pa = p; 7679b6b563cSPaul Mackerras sz = (pa - ps) + 1; 7689b6b563cSPaul Mackerras pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz, 7699b6b563cSPaul Mackerras __alignof__(struct property)); 7709b6b563cSPaul Mackerras if (allnextpp) { 7719b6b563cSPaul Mackerras pp->name = "name"; 7729b6b563cSPaul Mackerras pp->length = sz; 7739b6b563cSPaul Mackerras pp->value = (unsigned char *)(pp + 1); 7749b6b563cSPaul Mackerras *prev_pp = pp; 7759b6b563cSPaul Mackerras prev_pp = &pp->next; 7769b6b563cSPaul Mackerras memcpy(pp->value, ps, sz - 1); 7779b6b563cSPaul Mackerras ((char *)pp->value)[sz - 1] = 0; 7789b6b563cSPaul Mackerras DBG("fixed up name for %s -> %s\n", pathp, pp->value); 7799b6b563cSPaul Mackerras } 7809b6b563cSPaul Mackerras } 7819b6b563cSPaul Mackerras if (allnextpp) { 7829b6b563cSPaul Mackerras *prev_pp = NULL; 7839b6b563cSPaul Mackerras np->name = get_property(np, "name", NULL); 7849b6b563cSPaul Mackerras np->type = get_property(np, "device_type", NULL); 7859b6b563cSPaul Mackerras 7869b6b563cSPaul Mackerras if (!np->name) 7879b6b563cSPaul Mackerras np->name = "<NULL>"; 7889b6b563cSPaul Mackerras if (!np->type) 7899b6b563cSPaul Mackerras np->type = "<NULL>"; 7909b6b563cSPaul Mackerras } 7919b6b563cSPaul Mackerras while (tag == OF_DT_BEGIN_NODE) { 7929b6b563cSPaul Mackerras mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize); 7939b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 7949b6b563cSPaul Mackerras } 7959b6b563cSPaul Mackerras if (tag != OF_DT_END_NODE) { 7969b6b563cSPaul Mackerras printk("Weird tag at end of node: %x\n", tag); 7979b6b563cSPaul Mackerras return mem; 7989b6b563cSPaul Mackerras } 7999b6b563cSPaul Mackerras *p += 4; 8009b6b563cSPaul Mackerras return mem; 8019b6b563cSPaul Mackerras } 8029b6b563cSPaul Mackerras 8039b6b563cSPaul Mackerras 8049b6b563cSPaul Mackerras /** 8059b6b563cSPaul Mackerras * unflattens the device-tree passed by the firmware, creating the 8069b6b563cSPaul Mackerras * tree of struct device_node. It also fills the "name" and "type" 8079b6b563cSPaul Mackerras * pointers of the nodes so the normal device-tree walking functions 8089b6b563cSPaul Mackerras * can be used (this used to be done by finish_device_tree) 8099b6b563cSPaul Mackerras */ 8109b6b563cSPaul Mackerras void __init unflatten_device_tree(void) 8119b6b563cSPaul Mackerras { 8129b6b563cSPaul Mackerras unsigned long start, mem, size; 8139b6b563cSPaul Mackerras struct device_node **allnextp = &allnodes; 8149b6b563cSPaul Mackerras char *p = NULL; 8159b6b563cSPaul Mackerras int l = 0; 8169b6b563cSPaul Mackerras 8179b6b563cSPaul Mackerras DBG(" -> unflatten_device_tree()\n"); 8189b6b563cSPaul Mackerras 8199b6b563cSPaul Mackerras /* First pass, scan for size */ 8209b6b563cSPaul Mackerras start = ((unsigned long)initial_boot_params) + 8219b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 8229b6b563cSPaul Mackerras size = unflatten_dt_node(0, &start, NULL, NULL, 0); 8239b6b563cSPaul Mackerras size = (size | 3) + 1; 8249b6b563cSPaul Mackerras 8259b6b563cSPaul Mackerras DBG(" size is %lx, allocating...\n", size); 8269b6b563cSPaul Mackerras 8279b6b563cSPaul Mackerras /* Allocate memory for the expanded device tree */ 8289b6b563cSPaul Mackerras mem = lmb_alloc(size + 4, __alignof__(struct device_node)); 8299b6b563cSPaul Mackerras if (!mem) { 8309b6b563cSPaul Mackerras DBG("Couldn't allocate memory with lmb_alloc()!\n"); 8319b6b563cSPaul Mackerras panic("Couldn't allocate memory with lmb_alloc()!\n"); 8329b6b563cSPaul Mackerras } 8339b6b563cSPaul Mackerras mem = (unsigned long) __va(mem); 8349b6b563cSPaul Mackerras 8359b6b563cSPaul Mackerras ((u32 *)mem)[size / 4] = 0xdeadbeef; 8369b6b563cSPaul Mackerras 8379b6b563cSPaul Mackerras DBG(" unflattening %lx...\n", mem); 8389b6b563cSPaul Mackerras 8399b6b563cSPaul Mackerras /* Second pass, do actual unflattening */ 8409b6b563cSPaul Mackerras start = ((unsigned long)initial_boot_params) + 8419b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 8429b6b563cSPaul Mackerras unflatten_dt_node(mem, &start, NULL, &allnextp, 0); 8439b6b563cSPaul Mackerras if (*((u32 *)start) != OF_DT_END) 8449b6b563cSPaul Mackerras printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start)); 8459b6b563cSPaul Mackerras if (((u32 *)mem)[size / 4] != 0xdeadbeef) 8469b6b563cSPaul Mackerras printk(KERN_WARNING "End of tree marker overwritten: %08x\n", 8479b6b563cSPaul Mackerras ((u32 *)mem)[size / 4] ); 8489b6b563cSPaul Mackerras *allnextp = NULL; 8499b6b563cSPaul Mackerras 8509b6b563cSPaul Mackerras /* Get pointer to OF "/chosen" node for use everywhere */ 8519b6b563cSPaul Mackerras of_chosen = of_find_node_by_path("/chosen"); 852a575b807SPaul Mackerras if (of_chosen == NULL) 853a575b807SPaul Mackerras of_chosen = of_find_node_by_path("/chosen@0"); 8549b6b563cSPaul Mackerras 8559b6b563cSPaul Mackerras /* Retreive command line */ 8569b6b563cSPaul Mackerras if (of_chosen != NULL) { 8579b6b563cSPaul Mackerras p = (char *)get_property(of_chosen, "bootargs", &l); 8589b6b563cSPaul Mackerras if (p != NULL && l > 0) 8599b6b563cSPaul Mackerras strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE)); 8609b6b563cSPaul Mackerras } 8619b6b563cSPaul Mackerras #ifdef CONFIG_CMDLINE 8629b6b563cSPaul Mackerras if (l == 0 || (l == 1 && (*p) == 0)) 8639b6b563cSPaul Mackerras strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); 8649b6b563cSPaul Mackerras #endif /* CONFIG_CMDLINE */ 8659b6b563cSPaul Mackerras 8669b6b563cSPaul Mackerras DBG("Command line is: %s\n", cmd_line); 8679b6b563cSPaul Mackerras 8689b6b563cSPaul Mackerras DBG(" <- unflatten_device_tree()\n"); 8699b6b563cSPaul Mackerras } 8709b6b563cSPaul Mackerras 8719b6b563cSPaul Mackerras 8729b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node, 8739b6b563cSPaul Mackerras const char *uname, int depth, void *data) 8749b6b563cSPaul Mackerras { 8759b6b563cSPaul Mackerras u32 *prop; 876676e2497SStephen Rothwell unsigned long size; 877676e2497SStephen Rothwell char *type = of_get_flat_dt_prop(node, "device_type", &size); 8789b6b563cSPaul Mackerras 8799b6b563cSPaul Mackerras /* We are scanning "cpu" nodes only */ 8809b6b563cSPaul Mackerras if (type == NULL || strcmp(type, "cpu") != 0) 8819b6b563cSPaul Mackerras return 0; 8829b6b563cSPaul Mackerras 88380579e1fSPaul Mackerras boot_cpuid = 0; 88480579e1fSPaul Mackerras boot_cpuid_phys = 0; 8859b6b563cSPaul Mackerras if (initial_boot_params && initial_boot_params->version >= 2) { 8869b6b563cSPaul Mackerras /* version 2 of the kexec param format adds the phys cpuid 8879b6b563cSPaul Mackerras * of booted proc. 8889b6b563cSPaul Mackerras */ 8899b6b563cSPaul Mackerras boot_cpuid_phys = initial_boot_params->boot_cpuid_phys; 8909b6b563cSPaul Mackerras } else { 89180579e1fSPaul Mackerras /* Check if it's the boot-cpu, set it's hw index now */ 8923c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, 8933c726f8dSBenjamin Herrenschmidt "linux,boot-cpu", NULL) != NULL) { 8943c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "reg", NULL); 89580579e1fSPaul Mackerras if (prop != NULL) 89680579e1fSPaul Mackerras boot_cpuid_phys = *prop; 8979b6b563cSPaul Mackerras } 8989b6b563cSPaul Mackerras } 89980579e1fSPaul Mackerras set_hard_smp_processor_id(0, boot_cpuid_phys); 9009b6b563cSPaul Mackerras 9019b6b563cSPaul Mackerras #ifdef CONFIG_ALTIVEC 9029b6b563cSPaul Mackerras /* Check if we have a VMX and eventually update CPU features */ 903676e2497SStephen Rothwell prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL); 9049b6b563cSPaul Mackerras if (prop && (*prop) > 0) { 9059b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; 9069b6b563cSPaul Mackerras cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; 9079b6b563cSPaul Mackerras } 9089b6b563cSPaul Mackerras 9099b6b563cSPaul Mackerras /* Same goes for Apple's "altivec" property */ 9103c726f8dSBenjamin Herrenschmidt prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL); 9119b6b563cSPaul Mackerras if (prop) { 9129b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC; 9139b6b563cSPaul Mackerras cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC; 9149b6b563cSPaul Mackerras } 9159b6b563cSPaul Mackerras #endif /* CONFIG_ALTIVEC */ 9169b6b563cSPaul Mackerras 9179b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 9189b6b563cSPaul Mackerras /* 9199b6b563cSPaul Mackerras * Check for an SMT capable CPU and set the CPU feature. We do 9209b6b563cSPaul Mackerras * this by looking at the size of the ibm,ppc-interrupt-server#s 9219b6b563cSPaul Mackerras * property 9229b6b563cSPaul Mackerras */ 9233c726f8dSBenjamin Herrenschmidt prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", 9249b6b563cSPaul Mackerras &size); 9259b6b563cSPaul Mackerras cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT; 9269b6b563cSPaul Mackerras if (prop && ((size / sizeof(u32)) > 1)) 9279b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_SMT; 9289b6b563cSPaul Mackerras #endif 9299b6b563cSPaul Mackerras 9309b6b563cSPaul Mackerras return 0; 9319b6b563cSPaul Mackerras } 9329b6b563cSPaul Mackerras 9339b6b563cSPaul Mackerras static int __init early_init_dt_scan_chosen(unsigned long node, 9349b6b563cSPaul Mackerras const char *uname, int depth, void *data) 9359b6b563cSPaul Mackerras { 9369b6b563cSPaul Mackerras u32 *prop; 9379b6b563cSPaul Mackerras unsigned long *lprop; 9389b6b563cSPaul Mackerras 9399b6b563cSPaul Mackerras DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname); 9409b6b563cSPaul Mackerras 941a575b807SPaul Mackerras if (depth != 1 || 942a575b807SPaul Mackerras (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) 9439b6b563cSPaul Mackerras return 0; 9449b6b563cSPaul Mackerras 9459b6b563cSPaul Mackerras /* get platform type */ 9463c726f8dSBenjamin Herrenschmidt prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL); 9479b6b563cSPaul Mackerras if (prop == NULL) 9489b6b563cSPaul Mackerras return 0; 94960dda256SKumar Gala #ifdef CONFIG_PPC_MULTIPLATFORM 9509b6b563cSPaul Mackerras _machine = *prop; 9519b6b563cSPaul Mackerras #endif 9529b6b563cSPaul Mackerras 9539b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 9549b6b563cSPaul Mackerras /* check if iommu is forced on or off */ 9553c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL) 9569b6b563cSPaul Mackerras iommu_is_off = 1; 9573c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL) 9589b6b563cSPaul Mackerras iommu_force_on = 1; 9599b6b563cSPaul Mackerras #endif 9609b6b563cSPaul Mackerras 9613c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL); 9629b6b563cSPaul Mackerras if (lprop) 9639b6b563cSPaul Mackerras memory_limit = *lprop; 9649b6b563cSPaul Mackerras 9659b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 9663c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL); 9679b6b563cSPaul Mackerras if (lprop) 9689b6b563cSPaul Mackerras tce_alloc_start = *lprop; 9693c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL); 9709b6b563cSPaul Mackerras if (lprop) 9719b6b563cSPaul Mackerras tce_alloc_end = *lprop; 9729b6b563cSPaul Mackerras #endif 9739b6b563cSPaul Mackerras 9749b6b563cSPaul Mackerras #ifdef CONFIG_PPC_RTAS 975943ffb58SAdrian Bunk /* To help early debugging via the front panel, we retrieve a minimal 9769b6b563cSPaul Mackerras * set of RTAS infos now if available 9779b6b563cSPaul Mackerras */ 9789b6b563cSPaul Mackerras { 9799b6b563cSPaul Mackerras u64 *basep, *entryp; 9809b6b563cSPaul Mackerras 9813c726f8dSBenjamin Herrenschmidt basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL); 9823c726f8dSBenjamin Herrenschmidt entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL); 9833c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL); 9849b6b563cSPaul Mackerras if (basep && entryp && prop) { 9859b6b563cSPaul Mackerras rtas.base = *basep; 9869b6b563cSPaul Mackerras rtas.entry = *entryp; 9879b6b563cSPaul Mackerras rtas.size = *prop; 9889b6b563cSPaul Mackerras } 9899b6b563cSPaul Mackerras } 9909b6b563cSPaul Mackerras #endif /* CONFIG_PPC_RTAS */ 9919b6b563cSPaul Mackerras 992dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC 993dcee3036SMichael Ellerman lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL); 994dcee3036SMichael Ellerman if (lprop) 995dcee3036SMichael Ellerman crashk_res.start = *lprop; 996dcee3036SMichael Ellerman 997dcee3036SMichael Ellerman lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL); 998dcee3036SMichael Ellerman if (lprop) 999dcee3036SMichael Ellerman crashk_res.end = crashk_res.start + *lprop - 1; 1000dcee3036SMichael Ellerman #endif 1001dcee3036SMichael Ellerman 10029b6b563cSPaul Mackerras /* break now */ 10039b6b563cSPaul Mackerras return 1; 10049b6b563cSPaul Mackerras } 10059b6b563cSPaul Mackerras 10069b6b563cSPaul Mackerras static int __init early_init_dt_scan_root(unsigned long node, 10079b6b563cSPaul Mackerras const char *uname, int depth, void *data) 10089b6b563cSPaul Mackerras { 10099b6b563cSPaul Mackerras u32 *prop; 10109b6b563cSPaul Mackerras 10119b6b563cSPaul Mackerras if (depth != 0) 10129b6b563cSPaul Mackerras return 0; 10139b6b563cSPaul Mackerras 10143c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "#size-cells", NULL); 10159b6b563cSPaul Mackerras dt_root_size_cells = (prop == NULL) ? 1 : *prop; 10169b6b563cSPaul Mackerras DBG("dt_root_size_cells = %x\n", dt_root_size_cells); 10179b6b563cSPaul Mackerras 10183c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "#address-cells", NULL); 10199b6b563cSPaul Mackerras dt_root_addr_cells = (prop == NULL) ? 2 : *prop; 10209b6b563cSPaul Mackerras DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells); 10219b6b563cSPaul Mackerras 10229b6b563cSPaul Mackerras /* break now */ 10239b6b563cSPaul Mackerras return 1; 10249b6b563cSPaul Mackerras } 10259b6b563cSPaul Mackerras 10269b6b563cSPaul Mackerras static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp) 10279b6b563cSPaul Mackerras { 10289b6b563cSPaul Mackerras cell_t *p = *cellp; 10299b6b563cSPaul Mackerras unsigned long r; 10309b6b563cSPaul Mackerras 10319b6b563cSPaul Mackerras /* Ignore more than 2 cells */ 10329b6b563cSPaul Mackerras while (s > sizeof(unsigned long) / 4) { 10339b6b563cSPaul Mackerras p++; 10349b6b563cSPaul Mackerras s--; 10359b6b563cSPaul Mackerras } 10369b6b563cSPaul Mackerras r = *p++; 10379b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 10389b6b563cSPaul Mackerras if (s > 1) { 10399b6b563cSPaul Mackerras r <<= 32; 10409b6b563cSPaul Mackerras r |= *(p++); 10419b6b563cSPaul Mackerras s--; 10429b6b563cSPaul Mackerras } 10439b6b563cSPaul Mackerras #endif 10449b6b563cSPaul Mackerras 10459b6b563cSPaul Mackerras *cellp = p; 10469b6b563cSPaul Mackerras return r; 10479b6b563cSPaul Mackerras } 10489b6b563cSPaul Mackerras 10499b6b563cSPaul Mackerras 10509b6b563cSPaul Mackerras static int __init early_init_dt_scan_memory(unsigned long node, 10519b6b563cSPaul Mackerras const char *uname, int depth, void *data) 10529b6b563cSPaul Mackerras { 10533c726f8dSBenjamin Herrenschmidt char *type = of_get_flat_dt_prop(node, "device_type", NULL); 10549b6b563cSPaul Mackerras cell_t *reg, *endp; 10559b6b563cSPaul Mackerras unsigned long l; 10569b6b563cSPaul Mackerras 10579b6b563cSPaul Mackerras /* We are scanning "memory" nodes only */ 1058a23414beSPaul Mackerras if (type == NULL) { 1059a23414beSPaul Mackerras /* 1060a23414beSPaul Mackerras * The longtrail doesn't have a device_type on the 1061a23414beSPaul Mackerras * /memory node, so look for the node called /memory@0. 1062a23414beSPaul Mackerras */ 1063a23414beSPaul Mackerras if (depth != 1 || strcmp(uname, "memory@0") != 0) 1064a23414beSPaul Mackerras return 0; 1065a23414beSPaul Mackerras } else if (strcmp(type, "memory") != 0) 10669b6b563cSPaul Mackerras return 0; 10679b6b563cSPaul Mackerras 1068ba759485SMichael Ellerman reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l); 1069ba759485SMichael Ellerman if (reg == NULL) 10703c726f8dSBenjamin Herrenschmidt reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l); 10719b6b563cSPaul Mackerras if (reg == NULL) 10729b6b563cSPaul Mackerras return 0; 10739b6b563cSPaul Mackerras 10749b6b563cSPaul Mackerras endp = reg + (l / sizeof(cell_t)); 10759b6b563cSPaul Mackerras 1076358c86fdSMichael Ellerman DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n", 10779b6b563cSPaul Mackerras uname, l, reg[0], reg[1], reg[2], reg[3]); 10789b6b563cSPaul Mackerras 10799b6b563cSPaul Mackerras while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { 10809b6b563cSPaul Mackerras unsigned long base, size; 10819b6b563cSPaul Mackerras 10829b6b563cSPaul Mackerras base = dt_mem_next_cell(dt_root_addr_cells, ®); 10839b6b563cSPaul Mackerras size = dt_mem_next_cell(dt_root_size_cells, ®); 10849b6b563cSPaul Mackerras 10859b6b563cSPaul Mackerras if (size == 0) 10869b6b563cSPaul Mackerras continue; 10879b6b563cSPaul Mackerras DBG(" - %lx , %lx\n", base, size); 10889b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 10899b6b563cSPaul Mackerras if (iommu_is_off) { 10909b6b563cSPaul Mackerras if (base >= 0x80000000ul) 10919b6b563cSPaul Mackerras continue; 10929b6b563cSPaul Mackerras if ((base + size) > 0x80000000ul) 10939b6b563cSPaul Mackerras size = 0x80000000ul - base; 10949b6b563cSPaul Mackerras } 10959b6b563cSPaul Mackerras #endif 10969b6b563cSPaul Mackerras lmb_add(base, size); 10979b6b563cSPaul Mackerras } 10989b6b563cSPaul Mackerras return 0; 10999b6b563cSPaul Mackerras } 11009b6b563cSPaul Mackerras 11019b6b563cSPaul Mackerras static void __init early_reserve_mem(void) 11029b6b563cSPaul Mackerras { 1103*cbbcf340SKumar Gala u64 base, size; 1104*cbbcf340SKumar Gala u64 *reserve_map; 11059b6b563cSPaul Mackerras 1106*cbbcf340SKumar Gala reserve_map = (u64 *)(((unsigned long)initial_boot_params) + 11079b6b563cSPaul Mackerras initial_boot_params->off_mem_rsvmap); 1108*cbbcf340SKumar Gala #ifdef CONFIG_PPC32 1109*cbbcf340SKumar Gala /* 1110*cbbcf340SKumar Gala * Handle the case where we might be booting from an old kexec 1111*cbbcf340SKumar Gala * image that setup the mem_rsvmap as pairs of 32-bit values 1112*cbbcf340SKumar Gala */ 1113*cbbcf340SKumar Gala if (*reserve_map > 0xffffffffull) { 1114*cbbcf340SKumar Gala u32 base_32, size_32; 1115*cbbcf340SKumar Gala u32 *reserve_map_32 = (u32 *)reserve_map; 1116*cbbcf340SKumar Gala 1117*cbbcf340SKumar Gala while (1) { 1118*cbbcf340SKumar Gala base_32 = *(reserve_map_32++); 1119*cbbcf340SKumar Gala size_32 = *(reserve_map_32++); 1120*cbbcf340SKumar Gala if (size_32 == 0) 1121*cbbcf340SKumar Gala break; 1122*cbbcf340SKumar Gala DBG("reserving: %lx -> %lx\n", base_32, size_32); 1123*cbbcf340SKumar Gala lmb_reserve(base_32, size_32); 1124*cbbcf340SKumar Gala } 1125*cbbcf340SKumar Gala return; 1126*cbbcf340SKumar Gala } 1127*cbbcf340SKumar Gala #endif 11289b6b563cSPaul Mackerras while (1) { 11299b6b563cSPaul Mackerras base = *(reserve_map++); 11309b6b563cSPaul Mackerras size = *(reserve_map++); 11319b6b563cSPaul Mackerras if (size == 0) 11329b6b563cSPaul Mackerras break; 1133*cbbcf340SKumar Gala DBG("reserving: %llx -> %llx\n", base, size); 11349b6b563cSPaul Mackerras lmb_reserve(base, size); 11359b6b563cSPaul Mackerras } 11369b6b563cSPaul Mackerras 11379b6b563cSPaul Mackerras #if 0 11389b6b563cSPaul Mackerras DBG("memory reserved, lmbs :\n"); 11399b6b563cSPaul Mackerras lmb_dump_all(); 11409b6b563cSPaul Mackerras #endif 11419b6b563cSPaul Mackerras } 11429b6b563cSPaul Mackerras 11439b6b563cSPaul Mackerras void __init early_init_devtree(void *params) 11449b6b563cSPaul Mackerras { 11459b6b563cSPaul Mackerras DBG(" -> early_init_devtree()\n"); 11469b6b563cSPaul Mackerras 11479b6b563cSPaul Mackerras /* Setup flat device-tree pointer */ 11489b6b563cSPaul Mackerras initial_boot_params = params; 11499b6b563cSPaul Mackerras 11509b6b563cSPaul Mackerras /* Retrieve various informations from the /chosen node of the 11519b6b563cSPaul Mackerras * device-tree, including the platform type, initrd location and 11529b6b563cSPaul Mackerras * size, TCE reserve, and more ... 11539b6b563cSPaul Mackerras */ 11543c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_chosen, NULL); 11559b6b563cSPaul Mackerras 11569b6b563cSPaul Mackerras /* Scan memory nodes and rebuild LMBs */ 11579b6b563cSPaul Mackerras lmb_init(); 11583c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_root, NULL); 11593c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_memory, NULL); 11609b6b563cSPaul Mackerras lmb_enforce_memory_limit(memory_limit); 11619b6b563cSPaul Mackerras lmb_analyze(); 11629b6b563cSPaul Mackerras 11639b6b563cSPaul Mackerras DBG("Phys. mem: %lx\n", lmb_phys_mem_size()); 11649b6b563cSPaul Mackerras 11659b6b563cSPaul Mackerras /* Reserve LMB regions used by kernel, initrd, dt, etc... */ 11660cc4746cSMichael Ellerman lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START); 11670cc4746cSMichael Ellerman #ifdef CONFIG_CRASH_DUMP 11680cc4746cSMichael Ellerman lmb_reserve(0, KDUMP_RESERVE_LIMIT); 11690cc4746cSMichael Ellerman #endif 11709b6b563cSPaul Mackerras early_reserve_mem(); 11719b6b563cSPaul Mackerras 11729b6b563cSPaul Mackerras DBG("Scanning CPUs ...\n"); 11739b6b563cSPaul Mackerras 11743c726f8dSBenjamin Herrenschmidt /* Retreive CPU related informations from the flat tree 11753c726f8dSBenjamin Herrenschmidt * (altivec support, boot CPU ID, ...) 11769b6b563cSPaul Mackerras */ 11773c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_cpus, NULL); 11789b6b563cSPaul Mackerras 11799b6b563cSPaul Mackerras DBG(" <- early_init_devtree()\n"); 11809b6b563cSPaul Mackerras } 11819b6b563cSPaul Mackerras 11829b6b563cSPaul Mackerras #undef printk 11839b6b563cSPaul Mackerras 11849b6b563cSPaul Mackerras int 11859b6b563cSPaul Mackerras prom_n_addr_cells(struct device_node* np) 11869b6b563cSPaul Mackerras { 11879b6b563cSPaul Mackerras int* ip; 11889b6b563cSPaul Mackerras do { 11899b6b563cSPaul Mackerras if (np->parent) 11909b6b563cSPaul Mackerras np = np->parent; 11919b6b563cSPaul Mackerras ip = (int *) get_property(np, "#address-cells", NULL); 11929b6b563cSPaul Mackerras if (ip != NULL) 11939b6b563cSPaul Mackerras return *ip; 11949b6b563cSPaul Mackerras } while (np->parent); 11959b6b563cSPaul Mackerras /* No #address-cells property for the root node, default to 1 */ 11969b6b563cSPaul Mackerras return 1; 11979b6b563cSPaul Mackerras } 11981dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_addr_cells); 11999b6b563cSPaul Mackerras 12009b6b563cSPaul Mackerras int 12019b6b563cSPaul Mackerras prom_n_size_cells(struct device_node* np) 12029b6b563cSPaul Mackerras { 12039b6b563cSPaul Mackerras int* ip; 12049b6b563cSPaul Mackerras do { 12059b6b563cSPaul Mackerras if (np->parent) 12069b6b563cSPaul Mackerras np = np->parent; 12079b6b563cSPaul Mackerras ip = (int *) get_property(np, "#size-cells", NULL); 12089b6b563cSPaul Mackerras if (ip != NULL) 12099b6b563cSPaul Mackerras return *ip; 12109b6b563cSPaul Mackerras } while (np->parent); 12119b6b563cSPaul Mackerras /* No #size-cells property for the root node, default to 1 */ 12129b6b563cSPaul Mackerras return 1; 12139b6b563cSPaul Mackerras } 12141dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_size_cells); 12159b6b563cSPaul Mackerras 12169b6b563cSPaul Mackerras /** 12179b6b563cSPaul Mackerras * Work out the sense (active-low level / active-high edge) 12189b6b563cSPaul Mackerras * of each interrupt from the device tree. 12199b6b563cSPaul Mackerras */ 12209b6b563cSPaul Mackerras void __init prom_get_irq_senses(unsigned char *senses, int off, int max) 12219b6b563cSPaul Mackerras { 12229b6b563cSPaul Mackerras struct device_node *np; 12239b6b563cSPaul Mackerras int i, j; 12249b6b563cSPaul Mackerras 12259b6b563cSPaul Mackerras /* default to level-triggered */ 12266d0124fcSPaul Mackerras memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off); 12279b6b563cSPaul Mackerras 12289b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 12299b6b563cSPaul Mackerras for (j = 0; j < np->n_intrs; j++) { 12309b6b563cSPaul Mackerras i = np->intrs[j].line; 12319b6b563cSPaul Mackerras if (i >= off && i < max) 12326d0124fcSPaul Mackerras senses[i-off] = np->intrs[j].sense; 12339b6b563cSPaul Mackerras } 12349b6b563cSPaul Mackerras } 12359b6b563cSPaul Mackerras } 12369b6b563cSPaul Mackerras 12379b6b563cSPaul Mackerras /** 12389b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given name. 12399b6b563cSPaul Mackerras */ 12409b6b563cSPaul Mackerras struct device_node *find_devices(const char *name) 12419b6b563cSPaul Mackerras { 12429b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 12439b6b563cSPaul Mackerras 12449b6b563cSPaul Mackerras prevp = &head; 12459b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 12469b6b563cSPaul Mackerras if (np->name != 0 && strcasecmp(np->name, name) == 0) { 12479b6b563cSPaul Mackerras *prevp = np; 12489b6b563cSPaul Mackerras prevp = &np->next; 12499b6b563cSPaul Mackerras } 12509b6b563cSPaul Mackerras } 12519b6b563cSPaul Mackerras *prevp = NULL; 12529b6b563cSPaul Mackerras return head; 12539b6b563cSPaul Mackerras } 12549b6b563cSPaul Mackerras EXPORT_SYMBOL(find_devices); 12559b6b563cSPaul Mackerras 12569b6b563cSPaul Mackerras /** 12579b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type. 12589b6b563cSPaul Mackerras */ 12599b6b563cSPaul Mackerras struct device_node *find_type_devices(const char *type) 12609b6b563cSPaul Mackerras { 12619b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 12629b6b563cSPaul Mackerras 12639b6b563cSPaul Mackerras prevp = &head; 12649b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 12659b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0) { 12669b6b563cSPaul Mackerras *prevp = np; 12679b6b563cSPaul Mackerras prevp = &np->next; 12689b6b563cSPaul Mackerras } 12699b6b563cSPaul Mackerras } 12709b6b563cSPaul Mackerras *prevp = NULL; 12719b6b563cSPaul Mackerras return head; 12729b6b563cSPaul Mackerras } 12739b6b563cSPaul Mackerras EXPORT_SYMBOL(find_type_devices); 12749b6b563cSPaul Mackerras 12759b6b563cSPaul Mackerras /** 12769b6b563cSPaul Mackerras * Returns all nodes linked together 12779b6b563cSPaul Mackerras */ 12789b6b563cSPaul Mackerras struct device_node *find_all_nodes(void) 12799b6b563cSPaul Mackerras { 12809b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 12819b6b563cSPaul Mackerras 12829b6b563cSPaul Mackerras prevp = &head; 12839b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 12849b6b563cSPaul Mackerras *prevp = np; 12859b6b563cSPaul Mackerras prevp = &np->next; 12869b6b563cSPaul Mackerras } 12879b6b563cSPaul Mackerras *prevp = NULL; 12889b6b563cSPaul Mackerras return head; 12899b6b563cSPaul Mackerras } 12909b6b563cSPaul Mackerras EXPORT_SYMBOL(find_all_nodes); 12919b6b563cSPaul Mackerras 12929b6b563cSPaul Mackerras /** Checks if the given "compat" string matches one of the strings in 12939b6b563cSPaul Mackerras * the device's "compatible" property 12949b6b563cSPaul Mackerras */ 12959b6b563cSPaul Mackerras int device_is_compatible(struct device_node *device, const char *compat) 12969b6b563cSPaul Mackerras { 12979b6b563cSPaul Mackerras const char* cp; 12989b6b563cSPaul Mackerras int cplen, l; 12999b6b563cSPaul Mackerras 13009b6b563cSPaul Mackerras cp = (char *) get_property(device, "compatible", &cplen); 13019b6b563cSPaul Mackerras if (cp == NULL) 13029b6b563cSPaul Mackerras return 0; 13039b6b563cSPaul Mackerras while (cplen > 0) { 13049b6b563cSPaul Mackerras if (strncasecmp(cp, compat, strlen(compat)) == 0) 13059b6b563cSPaul Mackerras return 1; 13069b6b563cSPaul Mackerras l = strlen(cp) + 1; 13079b6b563cSPaul Mackerras cp += l; 13089b6b563cSPaul Mackerras cplen -= l; 13099b6b563cSPaul Mackerras } 13109b6b563cSPaul Mackerras 13119b6b563cSPaul Mackerras return 0; 13129b6b563cSPaul Mackerras } 13139b6b563cSPaul Mackerras EXPORT_SYMBOL(device_is_compatible); 13149b6b563cSPaul Mackerras 13159b6b563cSPaul Mackerras 13169b6b563cSPaul Mackerras /** 13179b6b563cSPaul Mackerras * Indicates whether the root node has a given value in its 13189b6b563cSPaul Mackerras * compatible property. 13199b6b563cSPaul Mackerras */ 13209b6b563cSPaul Mackerras int machine_is_compatible(const char *compat) 13219b6b563cSPaul Mackerras { 13229b6b563cSPaul Mackerras struct device_node *root; 13239b6b563cSPaul Mackerras int rc = 0; 13249b6b563cSPaul Mackerras 13259b6b563cSPaul Mackerras root = of_find_node_by_path("/"); 13269b6b563cSPaul Mackerras if (root) { 13279b6b563cSPaul Mackerras rc = device_is_compatible(root, compat); 13289b6b563cSPaul Mackerras of_node_put(root); 13299b6b563cSPaul Mackerras } 13309b6b563cSPaul Mackerras return rc; 13319b6b563cSPaul Mackerras } 13329b6b563cSPaul Mackerras EXPORT_SYMBOL(machine_is_compatible); 13339b6b563cSPaul Mackerras 13349b6b563cSPaul Mackerras /** 13359b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type 13369b6b563cSPaul Mackerras * and compatible property. 13379b6b563cSPaul Mackerras */ 13389b6b563cSPaul Mackerras struct device_node *find_compatible_devices(const char *type, 13399b6b563cSPaul Mackerras const char *compat) 13409b6b563cSPaul Mackerras { 13419b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 13429b6b563cSPaul Mackerras 13439b6b563cSPaul Mackerras prevp = &head; 13449b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 13459b6b563cSPaul Mackerras if (type != NULL 13469b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 13479b6b563cSPaul Mackerras continue; 13489b6b563cSPaul Mackerras if (device_is_compatible(np, compat)) { 13499b6b563cSPaul Mackerras *prevp = np; 13509b6b563cSPaul Mackerras prevp = &np->next; 13519b6b563cSPaul Mackerras } 13529b6b563cSPaul Mackerras } 13539b6b563cSPaul Mackerras *prevp = NULL; 13549b6b563cSPaul Mackerras return head; 13559b6b563cSPaul Mackerras } 13569b6b563cSPaul Mackerras EXPORT_SYMBOL(find_compatible_devices); 13579b6b563cSPaul Mackerras 13589b6b563cSPaul Mackerras /** 13599b6b563cSPaul Mackerras * Find the device_node with a given full_name. 13609b6b563cSPaul Mackerras */ 13619b6b563cSPaul Mackerras struct device_node *find_path_device(const char *path) 13629b6b563cSPaul Mackerras { 13639b6b563cSPaul Mackerras struct device_node *np; 13649b6b563cSPaul Mackerras 13659b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 13669b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) 13679b6b563cSPaul Mackerras return np; 13689b6b563cSPaul Mackerras return NULL; 13699b6b563cSPaul Mackerras } 13709b6b563cSPaul Mackerras EXPORT_SYMBOL(find_path_device); 13719b6b563cSPaul Mackerras 13729b6b563cSPaul Mackerras /******* 13739b6b563cSPaul Mackerras * 13749b6b563cSPaul Mackerras * New implementation of the OF "find" APIs, return a refcounted 13759b6b563cSPaul Mackerras * object, call of_node_put() when done. The device tree and list 13769b6b563cSPaul Mackerras * are protected by a rw_lock. 13779b6b563cSPaul Mackerras * 13789b6b563cSPaul Mackerras * Note that property management will need some locking as well, 13799b6b563cSPaul Mackerras * this isn't dealt with yet. 13809b6b563cSPaul Mackerras * 13819b6b563cSPaul Mackerras *******/ 13829b6b563cSPaul Mackerras 13839b6b563cSPaul Mackerras /** 13849b6b563cSPaul Mackerras * of_find_node_by_name - Find a node by its "name" property 13859b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 13869b6b563cSPaul Mackerras * you pass will not be searched, only the next one 13879b6b563cSPaul Mackerras * will; typically, you pass what the previous call 13889b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 13899b6b563cSPaul Mackerras * @name: The name string to match against 13909b6b563cSPaul Mackerras * 13919b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13929b6b563cSPaul Mackerras * of_node_put() on it when done. 13939b6b563cSPaul Mackerras */ 13949b6b563cSPaul Mackerras struct device_node *of_find_node_by_name(struct device_node *from, 13959b6b563cSPaul Mackerras const char *name) 13969b6b563cSPaul Mackerras { 13979b6b563cSPaul Mackerras struct device_node *np; 13989b6b563cSPaul Mackerras 13999b6b563cSPaul Mackerras read_lock(&devtree_lock); 14009b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 14019b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 14029b6b563cSPaul Mackerras if (np->name != 0 && strcasecmp(np->name, name) == 0 14039b6b563cSPaul Mackerras && of_node_get(np)) 14049b6b563cSPaul Mackerras break; 14059b6b563cSPaul Mackerras if (from) 14069b6b563cSPaul Mackerras of_node_put(from); 14079b6b563cSPaul Mackerras read_unlock(&devtree_lock); 14089b6b563cSPaul Mackerras return np; 14099b6b563cSPaul Mackerras } 14109b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_name); 14119b6b563cSPaul Mackerras 14129b6b563cSPaul Mackerras /** 14139b6b563cSPaul Mackerras * of_find_node_by_type - Find a node by its "device_type" property 14149b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 14159b6b563cSPaul Mackerras * you pass will not be searched, only the next one 14169b6b563cSPaul Mackerras * will; typically, you pass what the previous call 14179b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 14189b6b563cSPaul Mackerras * @name: The type string to match against 14199b6b563cSPaul Mackerras * 14209b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 14219b6b563cSPaul Mackerras * of_node_put() on it when done. 14229b6b563cSPaul Mackerras */ 14239b6b563cSPaul Mackerras struct device_node *of_find_node_by_type(struct device_node *from, 14249b6b563cSPaul Mackerras const char *type) 14259b6b563cSPaul Mackerras { 14269b6b563cSPaul Mackerras struct device_node *np; 14279b6b563cSPaul Mackerras 14289b6b563cSPaul Mackerras read_lock(&devtree_lock); 14299b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 14309b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 14319b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0 14329b6b563cSPaul Mackerras && of_node_get(np)) 14339b6b563cSPaul Mackerras break; 14349b6b563cSPaul Mackerras if (from) 14359b6b563cSPaul Mackerras of_node_put(from); 14369b6b563cSPaul Mackerras read_unlock(&devtree_lock); 14379b6b563cSPaul Mackerras return np; 14389b6b563cSPaul Mackerras } 14399b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_type); 14409b6b563cSPaul Mackerras 14419b6b563cSPaul Mackerras /** 14429b6b563cSPaul Mackerras * of_find_compatible_node - Find a node based on type and one of the 14439b6b563cSPaul Mackerras * tokens in its "compatible" property 14449b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 14459b6b563cSPaul Mackerras * you pass will not be searched, only the next one 14469b6b563cSPaul Mackerras * will; typically, you pass what the previous call 14479b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 14489b6b563cSPaul Mackerras * @type: The type string to match "device_type" or NULL to ignore 14499b6b563cSPaul Mackerras * @compatible: The string to match to one of the tokens in the device 14509b6b563cSPaul Mackerras * "compatible" list. 14519b6b563cSPaul Mackerras * 14529b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 14539b6b563cSPaul Mackerras * of_node_put() on it when done. 14549b6b563cSPaul Mackerras */ 14559b6b563cSPaul Mackerras struct device_node *of_find_compatible_node(struct device_node *from, 14569b6b563cSPaul Mackerras const char *type, const char *compatible) 14579b6b563cSPaul Mackerras { 14589b6b563cSPaul Mackerras struct device_node *np; 14599b6b563cSPaul Mackerras 14609b6b563cSPaul Mackerras read_lock(&devtree_lock); 14619b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 14629b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 14639b6b563cSPaul Mackerras if (type != NULL 14649b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 14659b6b563cSPaul Mackerras continue; 14669b6b563cSPaul Mackerras if (device_is_compatible(np, compatible) && of_node_get(np)) 14679b6b563cSPaul Mackerras break; 14689b6b563cSPaul Mackerras } 14699b6b563cSPaul Mackerras if (from) 14709b6b563cSPaul Mackerras of_node_put(from); 14719b6b563cSPaul Mackerras read_unlock(&devtree_lock); 14729b6b563cSPaul Mackerras return np; 14739b6b563cSPaul Mackerras } 14749b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_compatible_node); 14759b6b563cSPaul Mackerras 14769b6b563cSPaul Mackerras /** 14779b6b563cSPaul Mackerras * of_find_node_by_path - Find a node matching a full OF path 14789b6b563cSPaul Mackerras * @path: The full path to match 14799b6b563cSPaul Mackerras * 14809b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 14819b6b563cSPaul Mackerras * of_node_put() on it when done. 14829b6b563cSPaul Mackerras */ 14839b6b563cSPaul Mackerras struct device_node *of_find_node_by_path(const char *path) 14849b6b563cSPaul Mackerras { 14859b6b563cSPaul Mackerras struct device_node *np = allnodes; 14869b6b563cSPaul Mackerras 14879b6b563cSPaul Mackerras read_lock(&devtree_lock); 14889b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 14899b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0 14909b6b563cSPaul Mackerras && of_node_get(np)) 14919b6b563cSPaul Mackerras break; 14929b6b563cSPaul Mackerras } 14939b6b563cSPaul Mackerras read_unlock(&devtree_lock); 14949b6b563cSPaul Mackerras return np; 14959b6b563cSPaul Mackerras } 14969b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_path); 14979b6b563cSPaul Mackerras 14989b6b563cSPaul Mackerras /** 14999b6b563cSPaul Mackerras * of_find_node_by_phandle - Find a node given a phandle 15009b6b563cSPaul Mackerras * @handle: phandle of the node to find 15019b6b563cSPaul Mackerras * 15029b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 15039b6b563cSPaul Mackerras * of_node_put() on it when done. 15049b6b563cSPaul Mackerras */ 15059b6b563cSPaul Mackerras struct device_node *of_find_node_by_phandle(phandle handle) 15069b6b563cSPaul Mackerras { 15079b6b563cSPaul Mackerras struct device_node *np; 15089b6b563cSPaul Mackerras 15099b6b563cSPaul Mackerras read_lock(&devtree_lock); 15109b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 15119b6b563cSPaul Mackerras if (np->linux_phandle == handle) 15129b6b563cSPaul Mackerras break; 15139b6b563cSPaul Mackerras if (np) 15149b6b563cSPaul Mackerras of_node_get(np); 15159b6b563cSPaul Mackerras read_unlock(&devtree_lock); 15169b6b563cSPaul Mackerras return np; 15179b6b563cSPaul Mackerras } 15189b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_phandle); 15199b6b563cSPaul Mackerras 15209b6b563cSPaul Mackerras /** 15219b6b563cSPaul Mackerras * of_find_all_nodes - Get next node in global list 15229b6b563cSPaul Mackerras * @prev: Previous node or NULL to start iteration 15239b6b563cSPaul Mackerras * of_node_put() will be called on it 15249b6b563cSPaul Mackerras * 15259b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 15269b6b563cSPaul Mackerras * of_node_put() on it when done. 15279b6b563cSPaul Mackerras */ 15289b6b563cSPaul Mackerras struct device_node *of_find_all_nodes(struct device_node *prev) 15299b6b563cSPaul Mackerras { 15309b6b563cSPaul Mackerras struct device_node *np; 15319b6b563cSPaul Mackerras 15329b6b563cSPaul Mackerras read_lock(&devtree_lock); 15339b6b563cSPaul Mackerras np = prev ? prev->allnext : allnodes; 15349b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 15359b6b563cSPaul Mackerras if (of_node_get(np)) 15369b6b563cSPaul Mackerras break; 15379b6b563cSPaul Mackerras if (prev) 15389b6b563cSPaul Mackerras of_node_put(prev); 15399b6b563cSPaul Mackerras read_unlock(&devtree_lock); 15409b6b563cSPaul Mackerras return np; 15419b6b563cSPaul Mackerras } 15429b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_all_nodes); 15439b6b563cSPaul Mackerras 15449b6b563cSPaul Mackerras /** 15459b6b563cSPaul Mackerras * of_get_parent - Get a node's parent if any 15469b6b563cSPaul Mackerras * @node: Node to get parent 15479b6b563cSPaul Mackerras * 15489b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 15499b6b563cSPaul Mackerras * of_node_put() on it when done. 15509b6b563cSPaul Mackerras */ 15519b6b563cSPaul Mackerras struct device_node *of_get_parent(const struct device_node *node) 15529b6b563cSPaul Mackerras { 15539b6b563cSPaul Mackerras struct device_node *np; 15549b6b563cSPaul Mackerras 15559b6b563cSPaul Mackerras if (!node) 15569b6b563cSPaul Mackerras return NULL; 15579b6b563cSPaul Mackerras 15589b6b563cSPaul Mackerras read_lock(&devtree_lock); 15599b6b563cSPaul Mackerras np = of_node_get(node->parent); 15609b6b563cSPaul Mackerras read_unlock(&devtree_lock); 15619b6b563cSPaul Mackerras return np; 15629b6b563cSPaul Mackerras } 15639b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_parent); 15649b6b563cSPaul Mackerras 15659b6b563cSPaul Mackerras /** 15669b6b563cSPaul Mackerras * of_get_next_child - Iterate a node childs 15679b6b563cSPaul Mackerras * @node: parent node 15689b6b563cSPaul Mackerras * @prev: previous child of the parent node, or NULL to get first 15699b6b563cSPaul Mackerras * 15709b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 15719b6b563cSPaul Mackerras * of_node_put() on it when done. 15729b6b563cSPaul Mackerras */ 15739b6b563cSPaul Mackerras struct device_node *of_get_next_child(const struct device_node *node, 15749b6b563cSPaul Mackerras struct device_node *prev) 15759b6b563cSPaul Mackerras { 15769b6b563cSPaul Mackerras struct device_node *next; 15779b6b563cSPaul Mackerras 15789b6b563cSPaul Mackerras read_lock(&devtree_lock); 15799b6b563cSPaul Mackerras next = prev ? prev->sibling : node->child; 15809b6b563cSPaul Mackerras for (; next != 0; next = next->sibling) 15819b6b563cSPaul Mackerras if (of_node_get(next)) 15829b6b563cSPaul Mackerras break; 15839b6b563cSPaul Mackerras if (prev) 15849b6b563cSPaul Mackerras of_node_put(prev); 15859b6b563cSPaul Mackerras read_unlock(&devtree_lock); 15869b6b563cSPaul Mackerras return next; 15879b6b563cSPaul Mackerras } 15889b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_next_child); 15899b6b563cSPaul Mackerras 15909b6b563cSPaul Mackerras /** 15919b6b563cSPaul Mackerras * of_node_get - Increment refcount of a node 15929b6b563cSPaul Mackerras * @node: Node to inc refcount, NULL is supported to 15939b6b563cSPaul Mackerras * simplify writing of callers 15949b6b563cSPaul Mackerras * 15959b6b563cSPaul Mackerras * Returns node. 15969b6b563cSPaul Mackerras */ 15979b6b563cSPaul Mackerras struct device_node *of_node_get(struct device_node *node) 15989b6b563cSPaul Mackerras { 15999b6b563cSPaul Mackerras if (node) 16009b6b563cSPaul Mackerras kref_get(&node->kref); 16019b6b563cSPaul Mackerras return node; 16029b6b563cSPaul Mackerras } 16039b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_get); 16049b6b563cSPaul Mackerras 16059b6b563cSPaul Mackerras static inline struct device_node * kref_to_device_node(struct kref *kref) 16069b6b563cSPaul Mackerras { 16079b6b563cSPaul Mackerras return container_of(kref, struct device_node, kref); 16089b6b563cSPaul Mackerras } 16099b6b563cSPaul Mackerras 16109b6b563cSPaul Mackerras /** 16119b6b563cSPaul Mackerras * of_node_release - release a dynamically allocated node 16129b6b563cSPaul Mackerras * @kref: kref element of the node to be released 16139b6b563cSPaul Mackerras * 16149b6b563cSPaul Mackerras * In of_node_put() this function is passed to kref_put() 16159b6b563cSPaul Mackerras * as the destructor. 16169b6b563cSPaul Mackerras */ 16179b6b563cSPaul Mackerras static void of_node_release(struct kref *kref) 16189b6b563cSPaul Mackerras { 16199b6b563cSPaul Mackerras struct device_node *node = kref_to_device_node(kref); 16209b6b563cSPaul Mackerras struct property *prop = node->properties; 16219b6b563cSPaul Mackerras 16229b6b563cSPaul Mackerras if (!OF_IS_DYNAMIC(node)) 16239b6b563cSPaul Mackerras return; 16249b6b563cSPaul Mackerras while (prop) { 16259b6b563cSPaul Mackerras struct property *next = prop->next; 16269b6b563cSPaul Mackerras kfree(prop->name); 16279b6b563cSPaul Mackerras kfree(prop->value); 16289b6b563cSPaul Mackerras kfree(prop); 16299b6b563cSPaul Mackerras prop = next; 16309b6b563cSPaul Mackerras } 16319b6b563cSPaul Mackerras kfree(node->intrs); 16329b6b563cSPaul Mackerras kfree(node->full_name); 16339b6b563cSPaul Mackerras kfree(node->data); 16349b6b563cSPaul Mackerras kfree(node); 16359b6b563cSPaul Mackerras } 16369b6b563cSPaul Mackerras 16379b6b563cSPaul Mackerras /** 16389b6b563cSPaul Mackerras * of_node_put - Decrement refcount of a node 16399b6b563cSPaul Mackerras * @node: Node to dec refcount, NULL is supported to 16409b6b563cSPaul Mackerras * simplify writing of callers 16419b6b563cSPaul Mackerras * 16429b6b563cSPaul Mackerras */ 16439b6b563cSPaul Mackerras void of_node_put(struct device_node *node) 16449b6b563cSPaul Mackerras { 16459b6b563cSPaul Mackerras if (node) 16469b6b563cSPaul Mackerras kref_put(&node->kref, of_node_release); 16479b6b563cSPaul Mackerras } 16489b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_put); 16499b6b563cSPaul Mackerras 16509b6b563cSPaul Mackerras /* 16519b6b563cSPaul Mackerras * Plug a device node into the tree and global list. 16529b6b563cSPaul Mackerras */ 16539b6b563cSPaul Mackerras void of_attach_node(struct device_node *np) 16549b6b563cSPaul Mackerras { 16559b6b563cSPaul Mackerras write_lock(&devtree_lock); 16569b6b563cSPaul Mackerras np->sibling = np->parent->child; 16579b6b563cSPaul Mackerras np->allnext = allnodes; 16589b6b563cSPaul Mackerras np->parent->child = np; 16599b6b563cSPaul Mackerras allnodes = np; 16609b6b563cSPaul Mackerras write_unlock(&devtree_lock); 16619b6b563cSPaul Mackerras } 16629b6b563cSPaul Mackerras 16639b6b563cSPaul Mackerras /* 16649b6b563cSPaul Mackerras * "Unplug" a node from the device tree. The caller must hold 16659b6b563cSPaul Mackerras * a reference to the node. The memory associated with the node 16669b6b563cSPaul Mackerras * is not freed until its refcount goes to zero. 16679b6b563cSPaul Mackerras */ 16689b6b563cSPaul Mackerras void of_detach_node(const struct device_node *np) 16699b6b563cSPaul Mackerras { 16709b6b563cSPaul Mackerras struct device_node *parent; 16719b6b563cSPaul Mackerras 16729b6b563cSPaul Mackerras write_lock(&devtree_lock); 16739b6b563cSPaul Mackerras 16749b6b563cSPaul Mackerras parent = np->parent; 16759b6b563cSPaul Mackerras 16769b6b563cSPaul Mackerras if (allnodes == np) 16779b6b563cSPaul Mackerras allnodes = np->allnext; 16789b6b563cSPaul Mackerras else { 16799b6b563cSPaul Mackerras struct device_node *prev; 16809b6b563cSPaul Mackerras for (prev = allnodes; 16819b6b563cSPaul Mackerras prev->allnext != np; 16829b6b563cSPaul Mackerras prev = prev->allnext) 16839b6b563cSPaul Mackerras ; 16849b6b563cSPaul Mackerras prev->allnext = np->allnext; 16859b6b563cSPaul Mackerras } 16869b6b563cSPaul Mackerras 16879b6b563cSPaul Mackerras if (parent->child == np) 16889b6b563cSPaul Mackerras parent->child = np->sibling; 16899b6b563cSPaul Mackerras else { 16909b6b563cSPaul Mackerras struct device_node *prevsib; 16919b6b563cSPaul Mackerras for (prevsib = np->parent->child; 16929b6b563cSPaul Mackerras prevsib->sibling != np; 16939b6b563cSPaul Mackerras prevsib = prevsib->sibling) 16949b6b563cSPaul Mackerras ; 16959b6b563cSPaul Mackerras prevsib->sibling = np->sibling; 16969b6b563cSPaul Mackerras } 16979b6b563cSPaul Mackerras 16989b6b563cSPaul Mackerras write_unlock(&devtree_lock); 16999b6b563cSPaul Mackerras } 17009b6b563cSPaul Mackerras 17019b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 17029b6b563cSPaul Mackerras /* 17039b6b563cSPaul Mackerras * Fix up the uninitialized fields in a new device node: 17049b6b563cSPaul Mackerras * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields 17059b6b563cSPaul Mackerras * 17069b6b563cSPaul Mackerras * A lot of boot-time code is duplicated here, because functions such 17079b6b563cSPaul Mackerras * as finish_node_interrupts, interpret_pci_props, etc. cannot use the 17089b6b563cSPaul Mackerras * slab allocator. 17099b6b563cSPaul Mackerras * 17109b6b563cSPaul Mackerras * This should probably be split up into smaller chunks. 17119b6b563cSPaul Mackerras */ 17129b6b563cSPaul Mackerras 1713cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node) 17149b6b563cSPaul Mackerras { 17159b6b563cSPaul Mackerras struct device_node *parent = of_get_parent(node); 17169b6b563cSPaul Mackerras int err = 0; 17179b6b563cSPaul Mackerras phandle *ibm_phandle; 17189b6b563cSPaul Mackerras 17199b6b563cSPaul Mackerras node->name = get_property(node, "name", NULL); 17209b6b563cSPaul Mackerras node->type = get_property(node, "device_type", NULL); 17219b6b563cSPaul Mackerras 17229b6b563cSPaul Mackerras if (!parent) { 17239b6b563cSPaul Mackerras err = -ENODEV; 17249b6b563cSPaul Mackerras goto out; 17259b6b563cSPaul Mackerras } 17269b6b563cSPaul Mackerras 17279b6b563cSPaul Mackerras /* We don't support that function on PowerMac, at least 17289b6b563cSPaul Mackerras * not yet 17299b6b563cSPaul Mackerras */ 1730799d6046SPaul Mackerras if (_machine == PLATFORM_POWERMAC) 17319b6b563cSPaul Mackerras return -ENODEV; 17329b6b563cSPaul Mackerras 17339b6b563cSPaul Mackerras /* fix up new node's linux_phandle field */ 1734cc5d0189SBenjamin Herrenschmidt if ((ibm_phandle = (unsigned int *)get_property(node, 1735cc5d0189SBenjamin Herrenschmidt "ibm,phandle", NULL))) 17369b6b563cSPaul Mackerras node->linux_phandle = *ibm_phandle; 17379b6b563cSPaul Mackerras 17389b6b563cSPaul Mackerras out: 17399b6b563cSPaul Mackerras of_node_put(parent); 17409b6b563cSPaul Mackerras return err; 17419b6b563cSPaul Mackerras } 17429b6b563cSPaul Mackerras 17439b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb, 17449b6b563cSPaul Mackerras unsigned long action, void *node) 17459b6b563cSPaul Mackerras { 17469b6b563cSPaul Mackerras int err; 17479b6b563cSPaul Mackerras 17489b6b563cSPaul Mackerras switch (action) { 17499b6b563cSPaul Mackerras case PSERIES_RECONFIG_ADD: 1750cc5d0189SBenjamin Herrenschmidt err = of_finish_dynamic_node(node); 1751cc5d0189SBenjamin Herrenschmidt if (!err) 1752cc5d0189SBenjamin Herrenschmidt finish_node(node, NULL, 0); 17539b6b563cSPaul Mackerras if (err < 0) { 17549b6b563cSPaul Mackerras printk(KERN_ERR "finish_node returned %d\n", err); 17559b6b563cSPaul Mackerras err = NOTIFY_BAD; 17569b6b563cSPaul Mackerras } 17579b6b563cSPaul Mackerras break; 17589b6b563cSPaul Mackerras default: 17599b6b563cSPaul Mackerras err = NOTIFY_DONE; 17609b6b563cSPaul Mackerras break; 17619b6b563cSPaul Mackerras } 17629b6b563cSPaul Mackerras return err; 17639b6b563cSPaul Mackerras } 17649b6b563cSPaul Mackerras 17659b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = { 17669b6b563cSPaul Mackerras .notifier_call = prom_reconfig_notifier, 17679b6b563cSPaul Mackerras .priority = 10, /* This one needs to run first */ 17689b6b563cSPaul Mackerras }; 17699b6b563cSPaul Mackerras 17709b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void) 17719b6b563cSPaul Mackerras { 17729b6b563cSPaul Mackerras return pSeries_reconfig_notifier_register(&prom_reconfig_nb); 17739b6b563cSPaul Mackerras } 17749b6b563cSPaul Mackerras __initcall(prom_reconfig_setup); 17759b6b563cSPaul Mackerras #endif 17769b6b563cSPaul Mackerras 17779b6b563cSPaul Mackerras /* 17789b6b563cSPaul Mackerras * Find a property with a given name for a given node 17799b6b563cSPaul Mackerras * and return the value. 17809b6b563cSPaul Mackerras */ 17819b6b563cSPaul Mackerras unsigned char *get_property(struct device_node *np, const char *name, 17829b6b563cSPaul Mackerras int *lenp) 17839b6b563cSPaul Mackerras { 17849b6b563cSPaul Mackerras struct property *pp; 17859b6b563cSPaul Mackerras 17869b6b563cSPaul Mackerras for (pp = np->properties; pp != 0; pp = pp->next) 17879b6b563cSPaul Mackerras if (strcmp(pp->name, name) == 0) { 17889b6b563cSPaul Mackerras if (lenp != 0) 17899b6b563cSPaul Mackerras *lenp = pp->length; 17909b6b563cSPaul Mackerras return pp->value; 17919b6b563cSPaul Mackerras } 17929b6b563cSPaul Mackerras return NULL; 17939b6b563cSPaul Mackerras } 17949b6b563cSPaul Mackerras EXPORT_SYMBOL(get_property); 17959b6b563cSPaul Mackerras 17969b6b563cSPaul Mackerras /* 17979b6b563cSPaul Mackerras * Add a property to a node 17989b6b563cSPaul Mackerras */ 1799183d0202SBenjamin Herrenschmidt int prom_add_property(struct device_node* np, struct property* prop) 18009b6b563cSPaul Mackerras { 1801183d0202SBenjamin Herrenschmidt struct property **next; 18029b6b563cSPaul Mackerras 18039b6b563cSPaul Mackerras prop->next = NULL; 1804183d0202SBenjamin Herrenschmidt write_lock(&devtree_lock); 1805183d0202SBenjamin Herrenschmidt next = &np->properties; 1806183d0202SBenjamin Herrenschmidt while (*next) { 1807183d0202SBenjamin Herrenschmidt if (strcmp(prop->name, (*next)->name) == 0) { 1808183d0202SBenjamin Herrenschmidt /* duplicate ! don't insert it */ 1809183d0202SBenjamin Herrenschmidt write_unlock(&devtree_lock); 1810183d0202SBenjamin Herrenschmidt return -1; 1811183d0202SBenjamin Herrenschmidt } 18129b6b563cSPaul Mackerras next = &(*next)->next; 1813183d0202SBenjamin Herrenschmidt } 18149b6b563cSPaul Mackerras *next = prop; 1815183d0202SBenjamin Herrenschmidt write_unlock(&devtree_lock); 1816183d0202SBenjamin Herrenschmidt 1817799d6046SPaul Mackerras #ifdef CONFIG_PROC_DEVICETREE 1818183d0202SBenjamin Herrenschmidt /* try to add to proc as well if it was initialized */ 1819183d0202SBenjamin Herrenschmidt if (np->pde) 1820183d0202SBenjamin Herrenschmidt proc_device_tree_add_prop(np->pde, prop); 1821799d6046SPaul Mackerras #endif /* CONFIG_PROC_DEVICETREE */ 1822183d0202SBenjamin Herrenschmidt 1823183d0202SBenjamin Herrenschmidt return 0; 18249b6b563cSPaul Mackerras } 18259b6b563cSPaul Mackerras 18269b6b563cSPaul Mackerras 1827