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/kernel.h> 209b6b563cSPaul Mackerras #include <linux/string.h> 219b6b563cSPaul Mackerras #include <linux/init.h> 229b6b563cSPaul Mackerras #include <linux/threads.h> 239b6b563cSPaul Mackerras #include <linux/spinlock.h> 249b6b563cSPaul Mackerras #include <linux/types.h> 259b6b563cSPaul Mackerras #include <linux/pci.h> 269b6b563cSPaul Mackerras #include <linux/stringify.h> 279b6b563cSPaul Mackerras #include <linux/delay.h> 289b6b563cSPaul Mackerras #include <linux/initrd.h> 299b6b563cSPaul Mackerras #include <linux/bitops.h> 309b6b563cSPaul Mackerras #include <linux/module.h> 31dcee3036SMichael Ellerman #include <linux/kexec.h> 327a4571aeSMichael Ellerman #include <linux/debugfs.h> 330ebfff14SBenjamin Herrenschmidt #include <linux/irq.h> 349b6b563cSPaul Mackerras 359b6b563cSPaul Mackerras #include <asm/prom.h> 369b6b563cSPaul Mackerras #include <asm/rtas.h> 379b6b563cSPaul Mackerras #include <asm/lmb.h> 389b6b563cSPaul Mackerras #include <asm/page.h> 399b6b563cSPaul Mackerras #include <asm/processor.h> 409b6b563cSPaul Mackerras #include <asm/irq.h> 419b6b563cSPaul Mackerras #include <asm/io.h> 420cc4746cSMichael Ellerman #include <asm/kdump.h> 439b6b563cSPaul Mackerras #include <asm/smp.h> 449b6b563cSPaul Mackerras #include <asm/system.h> 459b6b563cSPaul Mackerras #include <asm/mmu.h> 469b6b563cSPaul Mackerras #include <asm/pgtable.h> 479b6b563cSPaul Mackerras #include <asm/pci.h> 489b6b563cSPaul Mackerras #include <asm/iommu.h> 499b6b563cSPaul Mackerras #include <asm/btext.h> 509b6b563cSPaul Mackerras #include <asm/sections.h> 519b6b563cSPaul Mackerras #include <asm/machdep.h> 529b6b563cSPaul Mackerras #include <asm/pSeries_reconfig.h> 5340ef8cbcSPaul Mackerras #include <asm/pci-bridge.h> 542babf5c2SMichael Ellerman #include <asm/kexec.h> 559b6b563cSPaul Mackerras 569b6b563cSPaul Mackerras #ifdef DEBUG 579b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt) 589b6b563cSPaul Mackerras #else 599b6b563cSPaul Mackerras #define DBG(fmt...) 609b6b563cSPaul Mackerras #endif 619b6b563cSPaul Mackerras 629b6b563cSPaul Mackerras 639b6b563cSPaul Mackerras static int __initdata dt_root_addr_cells; 649b6b563cSPaul Mackerras static int __initdata dt_root_size_cells; 659b6b563cSPaul Mackerras 669b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 6728897731SOlof Johansson int __initdata iommu_is_off; 689b6b563cSPaul Mackerras int __initdata iommu_force_on; 69cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end; 709b6b563cSPaul Mackerras #endif 719b6b563cSPaul Mackerras 729b6b563cSPaul Mackerras typedef u32 cell_t; 739b6b563cSPaul Mackerras 749b6b563cSPaul Mackerras #if 0 759b6b563cSPaul Mackerras static struct boot_param_header *initial_boot_params __initdata; 769b6b563cSPaul Mackerras #else 779b6b563cSPaul Mackerras struct boot_param_header *initial_boot_params; 789b6b563cSPaul Mackerras #endif 799b6b563cSPaul Mackerras 809b6b563cSPaul Mackerras static struct device_node *allnodes = NULL; 819b6b563cSPaul Mackerras 829b6b563cSPaul Mackerras /* use when traversing tree through the allnext, child, sibling, 839b6b563cSPaul Mackerras * or parent members of struct device_node. 849b6b563cSPaul Mackerras */ 859b6b563cSPaul Mackerras static DEFINE_RWLOCK(devtree_lock); 869b6b563cSPaul Mackerras 879b6b563cSPaul Mackerras /* export that to outside world */ 889b6b563cSPaul Mackerras struct device_node *of_chosen; 899b6b563cSPaul Mackerras 909b6b563cSPaul Mackerras static inline char *find_flat_dt_string(u32 offset) 919b6b563cSPaul Mackerras { 929b6b563cSPaul Mackerras return ((char *)initial_boot_params) + 939b6b563cSPaul Mackerras initial_boot_params->off_dt_strings + offset; 949b6b563cSPaul Mackerras } 959b6b563cSPaul Mackerras 969b6b563cSPaul Mackerras /** 979b6b563cSPaul Mackerras * This function is used to scan the flattened device-tree, it is 989b6b563cSPaul Mackerras * used to extract the memory informations at boot before we can 999b6b563cSPaul Mackerras * unflatten the tree 1009b6b563cSPaul Mackerras */ 1013c726f8dSBenjamin Herrenschmidt int __init of_scan_flat_dt(int (*it)(unsigned long node, 1029b6b563cSPaul Mackerras const char *uname, int depth, 1039b6b563cSPaul Mackerras void *data), 1049b6b563cSPaul Mackerras void *data) 1059b6b563cSPaul Mackerras { 1069b6b563cSPaul Mackerras unsigned long p = ((unsigned long)initial_boot_params) + 1079b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 1089b6b563cSPaul Mackerras int rc = 0; 1099b6b563cSPaul Mackerras int depth = -1; 1109b6b563cSPaul Mackerras 1119b6b563cSPaul Mackerras do { 1129b6b563cSPaul Mackerras u32 tag = *((u32 *)p); 1139b6b563cSPaul Mackerras char *pathp; 1149b6b563cSPaul Mackerras 1159b6b563cSPaul Mackerras p += 4; 1169b6b563cSPaul Mackerras if (tag == OF_DT_END_NODE) { 1179b6b563cSPaul Mackerras depth --; 1189b6b563cSPaul Mackerras continue; 1199b6b563cSPaul Mackerras } 1209b6b563cSPaul Mackerras if (tag == OF_DT_NOP) 1219b6b563cSPaul Mackerras continue; 1229b6b563cSPaul Mackerras if (tag == OF_DT_END) 1239b6b563cSPaul Mackerras break; 1249b6b563cSPaul Mackerras if (tag == OF_DT_PROP) { 1259b6b563cSPaul Mackerras u32 sz = *((u32 *)p); 1269b6b563cSPaul Mackerras p += 8; 1279b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 1289b6b563cSPaul Mackerras p = _ALIGN(p, sz >= 8 ? 8 : 4); 1299b6b563cSPaul Mackerras p += sz; 1309b6b563cSPaul Mackerras p = _ALIGN(p, 4); 1319b6b563cSPaul Mackerras continue; 1329b6b563cSPaul Mackerras } 1339b6b563cSPaul Mackerras if (tag != OF_DT_BEGIN_NODE) { 1349b6b563cSPaul Mackerras printk(KERN_WARNING "Invalid tag %x scanning flattened" 1359b6b563cSPaul Mackerras " device tree !\n", tag); 1369b6b563cSPaul Mackerras return -EINVAL; 1379b6b563cSPaul Mackerras } 1389b6b563cSPaul Mackerras depth++; 1399b6b563cSPaul Mackerras pathp = (char *)p; 1409b6b563cSPaul Mackerras p = _ALIGN(p + strlen(pathp) + 1, 4); 1419b6b563cSPaul Mackerras if ((*pathp) == '/') { 1429b6b563cSPaul Mackerras char *lp, *np; 1439b6b563cSPaul Mackerras for (lp = NULL, np = pathp; *np; np++) 1449b6b563cSPaul Mackerras if ((*np) == '/') 1459b6b563cSPaul Mackerras lp = np+1; 1469b6b563cSPaul Mackerras if (lp != NULL) 1479b6b563cSPaul Mackerras pathp = lp; 1489b6b563cSPaul Mackerras } 1499b6b563cSPaul Mackerras rc = it(p, pathp, depth, data); 1509b6b563cSPaul Mackerras if (rc != 0) 1519b6b563cSPaul Mackerras break; 1529b6b563cSPaul Mackerras } while(1); 1539b6b563cSPaul Mackerras 1549b6b563cSPaul Mackerras return rc; 1559b6b563cSPaul Mackerras } 1569b6b563cSPaul Mackerras 157e8222502SBenjamin Herrenschmidt unsigned long __init of_get_flat_dt_root(void) 158e8222502SBenjamin Herrenschmidt { 159e8222502SBenjamin Herrenschmidt unsigned long p = ((unsigned long)initial_boot_params) + 160e8222502SBenjamin Herrenschmidt initial_boot_params->off_dt_struct; 161e8222502SBenjamin Herrenschmidt 162e8222502SBenjamin Herrenschmidt while(*((u32 *)p) == OF_DT_NOP) 163e8222502SBenjamin Herrenschmidt p += 4; 164e8222502SBenjamin Herrenschmidt BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE); 165e8222502SBenjamin Herrenschmidt p += 4; 166e8222502SBenjamin Herrenschmidt return _ALIGN(p + strlen((char *)p) + 1, 4); 167e8222502SBenjamin Herrenschmidt } 168e8222502SBenjamin Herrenschmidt 1699b6b563cSPaul Mackerras /** 1709b6b563cSPaul Mackerras * This function can be used within scan_flattened_dt callback to get 1719b6b563cSPaul Mackerras * access to properties 1729b6b563cSPaul Mackerras */ 1733c726f8dSBenjamin Herrenschmidt void* __init of_get_flat_dt_prop(unsigned long node, const char *name, 1749b6b563cSPaul Mackerras unsigned long *size) 1759b6b563cSPaul Mackerras { 1769b6b563cSPaul Mackerras unsigned long p = node; 1779b6b563cSPaul Mackerras 1789b6b563cSPaul Mackerras do { 1799b6b563cSPaul Mackerras u32 tag = *((u32 *)p); 1809b6b563cSPaul Mackerras u32 sz, noff; 1819b6b563cSPaul Mackerras const char *nstr; 1829b6b563cSPaul Mackerras 1839b6b563cSPaul Mackerras p += 4; 1849b6b563cSPaul Mackerras if (tag == OF_DT_NOP) 1859b6b563cSPaul Mackerras continue; 1869b6b563cSPaul Mackerras if (tag != OF_DT_PROP) 1879b6b563cSPaul Mackerras return NULL; 1889b6b563cSPaul Mackerras 1899b6b563cSPaul Mackerras sz = *((u32 *)p); 1909b6b563cSPaul Mackerras noff = *((u32 *)(p + 4)); 1919b6b563cSPaul Mackerras p += 8; 1929b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 1939b6b563cSPaul Mackerras p = _ALIGN(p, sz >= 8 ? 8 : 4); 1949b6b563cSPaul Mackerras 1959b6b563cSPaul Mackerras nstr = find_flat_dt_string(noff); 1969b6b563cSPaul Mackerras if (nstr == NULL) { 1979b6b563cSPaul Mackerras printk(KERN_WARNING "Can't find property index" 1989b6b563cSPaul Mackerras " name !\n"); 1999b6b563cSPaul Mackerras return NULL; 2009b6b563cSPaul Mackerras } 2019b6b563cSPaul Mackerras if (strcmp(name, nstr) == 0) { 2029b6b563cSPaul Mackerras if (size) 2039b6b563cSPaul Mackerras *size = sz; 2049b6b563cSPaul Mackerras return (void *)p; 2059b6b563cSPaul Mackerras } 2069b6b563cSPaul Mackerras p += sz; 2079b6b563cSPaul Mackerras p = _ALIGN(p, 4); 2089b6b563cSPaul Mackerras } while(1); 2099b6b563cSPaul Mackerras } 2109b6b563cSPaul Mackerras 211e8222502SBenjamin Herrenschmidt int __init of_flat_dt_is_compatible(unsigned long node, const char *compat) 212e8222502SBenjamin Herrenschmidt { 213e8222502SBenjamin Herrenschmidt const char* cp; 214e8222502SBenjamin Herrenschmidt unsigned long cplen, l; 215e8222502SBenjamin Herrenschmidt 216e8222502SBenjamin Herrenschmidt cp = of_get_flat_dt_prop(node, "compatible", &cplen); 217e8222502SBenjamin Herrenschmidt if (cp == NULL) 218e8222502SBenjamin Herrenschmidt return 0; 219e8222502SBenjamin Herrenschmidt while (cplen > 0) { 220e8222502SBenjamin Herrenschmidt if (strncasecmp(cp, compat, strlen(compat)) == 0) 221e8222502SBenjamin Herrenschmidt return 1; 222e8222502SBenjamin Herrenschmidt l = strlen(cp) + 1; 223e8222502SBenjamin Herrenschmidt cp += l; 224e8222502SBenjamin Herrenschmidt cplen -= l; 225e8222502SBenjamin Herrenschmidt } 226e8222502SBenjamin Herrenschmidt 227e8222502SBenjamin Herrenschmidt return 0; 228e8222502SBenjamin Herrenschmidt } 229e8222502SBenjamin Herrenschmidt 2309b6b563cSPaul Mackerras static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size, 2319b6b563cSPaul Mackerras unsigned long align) 2329b6b563cSPaul Mackerras { 2339b6b563cSPaul Mackerras void *res; 2349b6b563cSPaul Mackerras 2359b6b563cSPaul Mackerras *mem = _ALIGN(*mem, align); 2369b6b563cSPaul Mackerras res = (void *)*mem; 2379b6b563cSPaul Mackerras *mem += size; 2389b6b563cSPaul Mackerras 2399b6b563cSPaul Mackerras return res; 2409b6b563cSPaul Mackerras } 2419b6b563cSPaul Mackerras 2429b6b563cSPaul Mackerras static unsigned long __init unflatten_dt_node(unsigned long mem, 2439b6b563cSPaul Mackerras unsigned long *p, 2449b6b563cSPaul Mackerras struct device_node *dad, 2459b6b563cSPaul Mackerras struct device_node ***allnextpp, 2469b6b563cSPaul Mackerras unsigned long fpsize) 2479b6b563cSPaul Mackerras { 2489b6b563cSPaul Mackerras struct device_node *np; 2499b6b563cSPaul Mackerras struct property *pp, **prev_pp = NULL; 2509b6b563cSPaul Mackerras char *pathp; 2519b6b563cSPaul Mackerras u32 tag; 2529b6b563cSPaul Mackerras unsigned int l, allocl; 2539b6b563cSPaul Mackerras int has_name = 0; 2549b6b563cSPaul Mackerras int new_format = 0; 2559b6b563cSPaul Mackerras 2569b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 2579b6b563cSPaul Mackerras if (tag != OF_DT_BEGIN_NODE) { 2589b6b563cSPaul Mackerras printk("Weird tag at start of node: %x\n", tag); 2599b6b563cSPaul Mackerras return mem; 2609b6b563cSPaul Mackerras } 2619b6b563cSPaul Mackerras *p += 4; 2629b6b563cSPaul Mackerras pathp = (char *)*p; 2639b6b563cSPaul Mackerras l = allocl = strlen(pathp) + 1; 2649b6b563cSPaul Mackerras *p = _ALIGN(*p + l, 4); 2659b6b563cSPaul Mackerras 2669b6b563cSPaul Mackerras /* version 0x10 has a more compact unit name here instead of the full 2679b6b563cSPaul Mackerras * path. we accumulate the full path size using "fpsize", we'll rebuild 2689b6b563cSPaul Mackerras * it later. We detect this because the first character of the name is 2699b6b563cSPaul Mackerras * not '/'. 2709b6b563cSPaul Mackerras */ 2719b6b563cSPaul Mackerras if ((*pathp) != '/') { 2729b6b563cSPaul Mackerras new_format = 1; 2739b6b563cSPaul Mackerras if (fpsize == 0) { 2749b6b563cSPaul Mackerras /* root node: special case. fpsize accounts for path 2759b6b563cSPaul Mackerras * plus terminating zero. root node only has '/', so 2769b6b563cSPaul Mackerras * fpsize should be 2, but we want to avoid the first 2779b6b563cSPaul Mackerras * level nodes to have two '/' so we use fpsize 1 here 2789b6b563cSPaul Mackerras */ 2799b6b563cSPaul Mackerras fpsize = 1; 2809b6b563cSPaul Mackerras allocl = 2; 2819b6b563cSPaul Mackerras } else { 2829b6b563cSPaul Mackerras /* account for '/' and path size minus terminal 0 2839b6b563cSPaul Mackerras * already in 'l' 2849b6b563cSPaul Mackerras */ 2859b6b563cSPaul Mackerras fpsize += l; 2869b6b563cSPaul Mackerras allocl = fpsize; 2879b6b563cSPaul Mackerras } 2889b6b563cSPaul Mackerras } 2899b6b563cSPaul Mackerras 2909b6b563cSPaul Mackerras 2919b6b563cSPaul Mackerras np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl, 2929b6b563cSPaul Mackerras __alignof__(struct device_node)); 2939b6b563cSPaul Mackerras if (allnextpp) { 2949b6b563cSPaul Mackerras memset(np, 0, sizeof(*np)); 2959b6b563cSPaul Mackerras np->full_name = ((char*)np) + sizeof(struct device_node); 2969b6b563cSPaul Mackerras if (new_format) { 2979b6b563cSPaul Mackerras char *p = np->full_name; 2989b6b563cSPaul Mackerras /* rebuild full path for new format */ 2999b6b563cSPaul Mackerras if (dad && dad->parent) { 3009b6b563cSPaul Mackerras strcpy(p, dad->full_name); 3019b6b563cSPaul Mackerras #ifdef DEBUG 3029b6b563cSPaul Mackerras if ((strlen(p) + l + 1) != allocl) { 3039b6b563cSPaul Mackerras DBG("%s: p: %d, l: %d, a: %d\n", 304e8222502SBenjamin Herrenschmidt pathp, (int)strlen(p), l, allocl); 3059b6b563cSPaul Mackerras } 3069b6b563cSPaul Mackerras #endif 3079b6b563cSPaul Mackerras p += strlen(p); 3089b6b563cSPaul Mackerras } 3099b6b563cSPaul Mackerras *(p++) = '/'; 3109b6b563cSPaul Mackerras memcpy(p, pathp, l); 3119b6b563cSPaul Mackerras } else 3129b6b563cSPaul Mackerras memcpy(np->full_name, pathp, l); 3139b6b563cSPaul Mackerras prev_pp = &np->properties; 3149b6b563cSPaul Mackerras **allnextpp = np; 3159b6b563cSPaul Mackerras *allnextpp = &np->allnext; 3169b6b563cSPaul Mackerras if (dad != NULL) { 3179b6b563cSPaul Mackerras np->parent = dad; 3189b6b563cSPaul Mackerras /* we temporarily use the next field as `last_child'*/ 3199b6b563cSPaul Mackerras if (dad->next == 0) 3209b6b563cSPaul Mackerras dad->child = np; 3219b6b563cSPaul Mackerras else 3229b6b563cSPaul Mackerras dad->next->sibling = np; 3239b6b563cSPaul Mackerras dad->next = np; 3249b6b563cSPaul Mackerras } 3259b6b563cSPaul Mackerras kref_init(&np->kref); 3269b6b563cSPaul Mackerras } 3279b6b563cSPaul Mackerras while(1) { 3289b6b563cSPaul Mackerras u32 sz, noff; 3299b6b563cSPaul Mackerras char *pname; 3309b6b563cSPaul Mackerras 3319b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 3329b6b563cSPaul Mackerras if (tag == OF_DT_NOP) { 3339b6b563cSPaul Mackerras *p += 4; 3349b6b563cSPaul Mackerras continue; 3359b6b563cSPaul Mackerras } 3369b6b563cSPaul Mackerras if (tag != OF_DT_PROP) 3379b6b563cSPaul Mackerras break; 3389b6b563cSPaul Mackerras *p += 4; 3399b6b563cSPaul Mackerras sz = *((u32 *)(*p)); 3409b6b563cSPaul Mackerras noff = *((u32 *)((*p) + 4)); 3419b6b563cSPaul Mackerras *p += 8; 3429b6b563cSPaul Mackerras if (initial_boot_params->version < 0x10) 3439b6b563cSPaul Mackerras *p = _ALIGN(*p, sz >= 8 ? 8 : 4); 3449b6b563cSPaul Mackerras 3459b6b563cSPaul Mackerras pname = find_flat_dt_string(noff); 3469b6b563cSPaul Mackerras if (pname == NULL) { 3479b6b563cSPaul Mackerras printk("Can't find property name in list !\n"); 3489b6b563cSPaul Mackerras break; 3499b6b563cSPaul Mackerras } 3509b6b563cSPaul Mackerras if (strcmp(pname, "name") == 0) 3519b6b563cSPaul Mackerras has_name = 1; 3529b6b563cSPaul Mackerras l = strlen(pname) + 1; 3539b6b563cSPaul Mackerras pp = unflatten_dt_alloc(&mem, sizeof(struct property), 3549b6b563cSPaul Mackerras __alignof__(struct property)); 3559b6b563cSPaul Mackerras if (allnextpp) { 3569b6b563cSPaul Mackerras if (strcmp(pname, "linux,phandle") == 0) { 3579b6b563cSPaul Mackerras np->node = *((u32 *)*p); 3589b6b563cSPaul Mackerras if (np->linux_phandle == 0) 3599b6b563cSPaul Mackerras np->linux_phandle = np->node; 3609b6b563cSPaul Mackerras } 3619b6b563cSPaul Mackerras if (strcmp(pname, "ibm,phandle") == 0) 3629b6b563cSPaul Mackerras np->linux_phandle = *((u32 *)*p); 3639b6b563cSPaul Mackerras pp->name = pname; 3649b6b563cSPaul Mackerras pp->length = sz; 3659b6b563cSPaul Mackerras pp->value = (void *)*p; 3669b6b563cSPaul Mackerras *prev_pp = pp; 3679b6b563cSPaul Mackerras prev_pp = &pp->next; 3689b6b563cSPaul Mackerras } 3699b6b563cSPaul Mackerras *p = _ALIGN((*p) + sz, 4); 3709b6b563cSPaul Mackerras } 3719b6b563cSPaul Mackerras /* with version 0x10 we may not have the name property, recreate 3729b6b563cSPaul Mackerras * it here from the unit name if absent 3739b6b563cSPaul Mackerras */ 3749b6b563cSPaul Mackerras if (!has_name) { 3759b6b563cSPaul Mackerras char *p = pathp, *ps = pathp, *pa = NULL; 3769b6b563cSPaul Mackerras int sz; 3779b6b563cSPaul Mackerras 3789b6b563cSPaul Mackerras while (*p) { 3799b6b563cSPaul Mackerras if ((*p) == '@') 3809b6b563cSPaul Mackerras pa = p; 3819b6b563cSPaul Mackerras if ((*p) == '/') 3829b6b563cSPaul Mackerras ps = p + 1; 3839b6b563cSPaul Mackerras p++; 3849b6b563cSPaul Mackerras } 3859b6b563cSPaul Mackerras if (pa < ps) 3869b6b563cSPaul Mackerras pa = p; 3879b6b563cSPaul Mackerras sz = (pa - ps) + 1; 3889b6b563cSPaul Mackerras pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz, 3899b6b563cSPaul Mackerras __alignof__(struct property)); 3909b6b563cSPaul Mackerras if (allnextpp) { 3919b6b563cSPaul Mackerras pp->name = "name"; 3929b6b563cSPaul Mackerras pp->length = sz; 3939b6b563cSPaul Mackerras pp->value = (unsigned char *)(pp + 1); 3949b6b563cSPaul Mackerras *prev_pp = pp; 3959b6b563cSPaul Mackerras prev_pp = &pp->next; 3969b6b563cSPaul Mackerras memcpy(pp->value, ps, sz - 1); 3979b6b563cSPaul Mackerras ((char *)pp->value)[sz - 1] = 0; 3989b6b563cSPaul Mackerras DBG("fixed up name for %s -> %s\n", pathp, pp->value); 3999b6b563cSPaul Mackerras } 4009b6b563cSPaul Mackerras } 4019b6b563cSPaul Mackerras if (allnextpp) { 4029b6b563cSPaul Mackerras *prev_pp = NULL; 4039b6b563cSPaul Mackerras np->name = get_property(np, "name", NULL); 4049b6b563cSPaul Mackerras np->type = get_property(np, "device_type", NULL); 4059b6b563cSPaul Mackerras 4069b6b563cSPaul Mackerras if (!np->name) 4079b6b563cSPaul Mackerras np->name = "<NULL>"; 4089b6b563cSPaul Mackerras if (!np->type) 4099b6b563cSPaul Mackerras np->type = "<NULL>"; 4109b6b563cSPaul Mackerras } 4119b6b563cSPaul Mackerras while (tag == OF_DT_BEGIN_NODE) { 4129b6b563cSPaul Mackerras mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize); 4139b6b563cSPaul Mackerras tag = *((u32 *)(*p)); 4149b6b563cSPaul Mackerras } 4159b6b563cSPaul Mackerras if (tag != OF_DT_END_NODE) { 4169b6b563cSPaul Mackerras printk("Weird tag at end of node: %x\n", tag); 4179b6b563cSPaul Mackerras return mem; 4189b6b563cSPaul Mackerras } 4199b6b563cSPaul Mackerras *p += 4; 4209b6b563cSPaul Mackerras return mem; 4219b6b563cSPaul Mackerras } 4229b6b563cSPaul Mackerras 4232babf5c2SMichael Ellerman static int __init early_parse_mem(char *p) 4242babf5c2SMichael Ellerman { 4252babf5c2SMichael Ellerman if (!p) 4262babf5c2SMichael Ellerman return 1; 4272babf5c2SMichael Ellerman 4282babf5c2SMichael Ellerman memory_limit = PAGE_ALIGN(memparse(p, &p)); 4292babf5c2SMichael Ellerman DBG("memory limit = 0x%lx\n", memory_limit); 4302babf5c2SMichael Ellerman 4312babf5c2SMichael Ellerman return 0; 4322babf5c2SMichael Ellerman } 4332babf5c2SMichael Ellerman early_param("mem", early_parse_mem); 4342babf5c2SMichael Ellerman 4352babf5c2SMichael Ellerman /* 4362babf5c2SMichael Ellerman * The device tree may be allocated below our memory limit, or inside the 4372babf5c2SMichael Ellerman * crash kernel region for kdump. If so, move it out now. 4382babf5c2SMichael Ellerman */ 4392babf5c2SMichael Ellerman static void move_device_tree(void) 4402babf5c2SMichael Ellerman { 4412babf5c2SMichael Ellerman unsigned long start, size; 4422babf5c2SMichael Ellerman void *p; 4432babf5c2SMichael Ellerman 4442babf5c2SMichael Ellerman DBG("-> move_device_tree\n"); 4452babf5c2SMichael Ellerman 4462babf5c2SMichael Ellerman start = __pa(initial_boot_params); 4472babf5c2SMichael Ellerman size = initial_boot_params->totalsize; 4482babf5c2SMichael Ellerman 4492babf5c2SMichael Ellerman if ((memory_limit && (start + size) > memory_limit) || 4502babf5c2SMichael Ellerman overlaps_crashkernel(start, size)) { 4512babf5c2SMichael Ellerman p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size)); 4522babf5c2SMichael Ellerman memcpy(p, initial_boot_params, size); 4532babf5c2SMichael Ellerman initial_boot_params = (struct boot_param_header *)p; 4542babf5c2SMichael Ellerman DBG("Moved device tree to 0x%p\n", p); 4552babf5c2SMichael Ellerman } 4562babf5c2SMichael Ellerman 4572babf5c2SMichael Ellerman DBG("<- move_device_tree\n"); 4582babf5c2SMichael Ellerman } 4599b6b563cSPaul Mackerras 4609b6b563cSPaul Mackerras /** 4619b6b563cSPaul Mackerras * unflattens the device-tree passed by the firmware, creating the 4629b6b563cSPaul Mackerras * tree of struct device_node. It also fills the "name" and "type" 4639b6b563cSPaul Mackerras * pointers of the nodes so the normal device-tree walking functions 4649b6b563cSPaul Mackerras * can be used (this used to be done by finish_device_tree) 4659b6b563cSPaul Mackerras */ 4669b6b563cSPaul Mackerras void __init unflatten_device_tree(void) 4679b6b563cSPaul Mackerras { 4689b6b563cSPaul Mackerras unsigned long start, mem, size; 4699b6b563cSPaul Mackerras struct device_node **allnextp = &allnodes; 4709b6b563cSPaul Mackerras 4719b6b563cSPaul Mackerras DBG(" -> unflatten_device_tree()\n"); 4729b6b563cSPaul Mackerras 4739b6b563cSPaul Mackerras /* First pass, scan for size */ 4749b6b563cSPaul Mackerras start = ((unsigned long)initial_boot_params) + 4759b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 4769b6b563cSPaul Mackerras size = unflatten_dt_node(0, &start, NULL, NULL, 0); 4779b6b563cSPaul Mackerras size = (size | 3) + 1; 4789b6b563cSPaul Mackerras 4799b6b563cSPaul Mackerras DBG(" size is %lx, allocating...\n", size); 4809b6b563cSPaul Mackerras 4819b6b563cSPaul Mackerras /* Allocate memory for the expanded device tree */ 4829b6b563cSPaul Mackerras mem = lmb_alloc(size + 4, __alignof__(struct device_node)); 4839b6b563cSPaul Mackerras mem = (unsigned long) __va(mem); 4849b6b563cSPaul Mackerras 4859b6b563cSPaul Mackerras ((u32 *)mem)[size / 4] = 0xdeadbeef; 4869b6b563cSPaul Mackerras 4879b6b563cSPaul Mackerras DBG(" unflattening %lx...\n", mem); 4889b6b563cSPaul Mackerras 4899b6b563cSPaul Mackerras /* Second pass, do actual unflattening */ 4909b6b563cSPaul Mackerras start = ((unsigned long)initial_boot_params) + 4919b6b563cSPaul Mackerras initial_boot_params->off_dt_struct; 4929b6b563cSPaul Mackerras unflatten_dt_node(mem, &start, NULL, &allnextp, 0); 4939b6b563cSPaul Mackerras if (*((u32 *)start) != OF_DT_END) 4949b6b563cSPaul Mackerras printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start)); 4959b6b563cSPaul Mackerras if (((u32 *)mem)[size / 4] != 0xdeadbeef) 4969b6b563cSPaul Mackerras printk(KERN_WARNING "End of tree marker overwritten: %08x\n", 4979b6b563cSPaul Mackerras ((u32 *)mem)[size / 4] ); 4989b6b563cSPaul Mackerras *allnextp = NULL; 4999b6b563cSPaul Mackerras 5009b6b563cSPaul Mackerras /* Get pointer to OF "/chosen" node for use everywhere */ 5019b6b563cSPaul Mackerras of_chosen = of_find_node_by_path("/chosen"); 502a575b807SPaul Mackerras if (of_chosen == NULL) 503a575b807SPaul Mackerras of_chosen = of_find_node_by_path("/chosen@0"); 5049b6b563cSPaul Mackerras 5059b6b563cSPaul Mackerras DBG(" <- unflatten_device_tree()\n"); 5069b6b563cSPaul Mackerras } 5079b6b563cSPaul Mackerras 508d205819eSPaul Mackerras /* 509d205819eSPaul Mackerras * ibm,pa-features is a per-cpu property that contains a string of 510d205819eSPaul Mackerras * attribute descriptors, each of which has a 2 byte header plus up 511d205819eSPaul Mackerras * to 254 bytes worth of processor attribute bits. First header 512d205819eSPaul Mackerras * byte specifies the number of bytes following the header. 513d205819eSPaul Mackerras * Second header byte is an "attribute-specifier" type, of which 514d205819eSPaul Mackerras * zero is the only currently-defined value. 515d205819eSPaul Mackerras * Implementation: Pass in the byte and bit offset for the feature 516d205819eSPaul Mackerras * that we are interested in. The function will return -1 if the 517d205819eSPaul Mackerras * pa-features property is missing, or a 1/0 to indicate if the feature 518d205819eSPaul Mackerras * is supported/not supported. Note that the bit numbers are 519d205819eSPaul Mackerras * big-endian to match the definition in PAPR. 520d205819eSPaul Mackerras */ 521d205819eSPaul Mackerras static struct ibm_pa_feature { 522d205819eSPaul Mackerras unsigned long cpu_features; /* CPU_FTR_xxx bit */ 523d205819eSPaul Mackerras unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */ 524d205819eSPaul Mackerras unsigned char pabyte; /* byte number in ibm,pa-features */ 525d205819eSPaul Mackerras unsigned char pabit; /* bit number (big-endian) */ 526d205819eSPaul Mackerras unsigned char invert; /* if 1, pa bit set => clear feature */ 527d205819eSPaul Mackerras } ibm_pa_features[] __initdata = { 528d205819eSPaul Mackerras {0, PPC_FEATURE_HAS_MMU, 0, 0, 0}, 529d205819eSPaul Mackerras {0, PPC_FEATURE_HAS_FPU, 0, 1, 0}, 530d205819eSPaul Mackerras {CPU_FTR_SLB, 0, 0, 2, 0}, 531d205819eSPaul Mackerras {CPU_FTR_CTRL, 0, 0, 3, 0}, 532d205819eSPaul Mackerras {CPU_FTR_NOEXECUTE, 0, 0, 6, 0}, 533d205819eSPaul Mackerras {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1}, 534bf72aebaSPaul Mackerras #if 0 535bf72aebaSPaul Mackerras /* put this back once we know how to test if firmware does 64k IO */ 536d205819eSPaul Mackerras {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0}, 537bf72aebaSPaul Mackerras #endif 538339d76c5SPaul Mackerras {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0}, 539d205819eSPaul Mackerras }; 540d205819eSPaul Mackerras 541*974a76f5SPaul Mackerras static void __init scan_features(unsigned long node, unsigned char *ftrs, 542*974a76f5SPaul Mackerras unsigned long tablelen, 543*974a76f5SPaul Mackerras struct ibm_pa_feature *fp, 544*974a76f5SPaul Mackerras unsigned long ft_size) 545d205819eSPaul Mackerras { 546*974a76f5SPaul Mackerras unsigned long i, len, bit; 547d205819eSPaul Mackerras 548d205819eSPaul Mackerras /* find descriptor with type == 0 */ 549d205819eSPaul Mackerras for (;;) { 550d205819eSPaul Mackerras if (tablelen < 3) 551d205819eSPaul Mackerras return; 552*974a76f5SPaul Mackerras len = 2 + ftrs[0]; 553d205819eSPaul Mackerras if (tablelen < len) 554d205819eSPaul Mackerras return; /* descriptor 0 not found */ 555*974a76f5SPaul Mackerras if (ftrs[1] == 0) 556d205819eSPaul Mackerras break; 557d205819eSPaul Mackerras tablelen -= len; 558*974a76f5SPaul Mackerras ftrs += len; 559d205819eSPaul Mackerras } 560d205819eSPaul Mackerras 561d205819eSPaul Mackerras /* loop over bits we know about */ 562*974a76f5SPaul Mackerras for (i = 0; i < ft_size; ++i, ++fp) { 563*974a76f5SPaul Mackerras if (fp->pabyte >= ftrs[0]) 564d205819eSPaul Mackerras continue; 565*974a76f5SPaul Mackerras bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1; 566d205819eSPaul Mackerras if (bit ^ fp->invert) { 567d205819eSPaul Mackerras cur_cpu_spec->cpu_features |= fp->cpu_features; 568d205819eSPaul Mackerras cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs; 569d205819eSPaul Mackerras } else { 570d205819eSPaul Mackerras cur_cpu_spec->cpu_features &= ~fp->cpu_features; 571d205819eSPaul Mackerras cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs; 572d205819eSPaul Mackerras } 573d205819eSPaul Mackerras } 574d205819eSPaul Mackerras } 575d205819eSPaul Mackerras 576*974a76f5SPaul Mackerras static void __init check_cpu_pa_features(unsigned long node) 577*974a76f5SPaul Mackerras { 578*974a76f5SPaul Mackerras unsigned char *pa_ftrs; 579*974a76f5SPaul Mackerras unsigned long tablelen; 580*974a76f5SPaul Mackerras 581*974a76f5SPaul Mackerras pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen); 582*974a76f5SPaul Mackerras if (pa_ftrs == NULL) 583*974a76f5SPaul Mackerras return; 584*974a76f5SPaul Mackerras 585*974a76f5SPaul Mackerras scan_features(node, pa_ftrs, tablelen, 586*974a76f5SPaul Mackerras ibm_pa_features, ARRAY_SIZE(ibm_pa_features)); 587*974a76f5SPaul Mackerras } 588*974a76f5SPaul Mackerras 589*974a76f5SPaul Mackerras static struct feature_property { 590*974a76f5SPaul Mackerras const char *name; 591*974a76f5SPaul Mackerras u32 min_value; 592*974a76f5SPaul Mackerras unsigned long cpu_feature; 593*974a76f5SPaul Mackerras unsigned long cpu_user_ftr; 594*974a76f5SPaul Mackerras } feature_properties[] __initdata = { 595*974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC 596*974a76f5SPaul Mackerras {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC}, 597*974a76f5SPaul Mackerras {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC}, 598*974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */ 599*974a76f5SPaul Mackerras #ifdef CONFIG_PPC64 600*974a76f5SPaul Mackerras {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP}, 601*974a76f5SPaul Mackerras {"ibm,purr", 1, CPU_FTR_PURR, 0}, 602*974a76f5SPaul Mackerras {"ibm,spurr", 1, CPU_FTR_SPURR, 0}, 603*974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */ 604*974a76f5SPaul Mackerras }; 605*974a76f5SPaul Mackerras 606*974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node) 607*974a76f5SPaul Mackerras { 608*974a76f5SPaul Mackerras unsigned long i; 609*974a76f5SPaul Mackerras struct feature_property *fp = feature_properties; 610*974a76f5SPaul Mackerras const u32 *prop; 611*974a76f5SPaul Mackerras 612*974a76f5SPaul Mackerras for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) { 613*974a76f5SPaul Mackerras prop = of_get_flat_dt_prop(node, fp->name, NULL); 614*974a76f5SPaul Mackerras if (prop && *prop >= fp->min_value) { 615*974a76f5SPaul Mackerras cur_cpu_spec->cpu_features |= fp->cpu_feature; 616*974a76f5SPaul Mackerras cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr; 617*974a76f5SPaul Mackerras } 618*974a76f5SPaul Mackerras } 619*974a76f5SPaul Mackerras } 620*974a76f5SPaul Mackerras 6219b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node, 6224df20460SAnton Blanchard const char *uname, int depth, 6234df20460SAnton Blanchard void *data) 6249b6b563cSPaul Mackerras { 6254df20460SAnton Blanchard static int logical_cpuid = 0; 6264df20460SAnton Blanchard char *type = of_get_flat_dt_prop(node, "device_type", NULL); 627*974a76f5SPaul Mackerras const u32 *prop; 628*974a76f5SPaul Mackerras const u32 *intserv; 6294df20460SAnton Blanchard int i, nthreads; 6304df20460SAnton Blanchard unsigned long len; 6314df20460SAnton Blanchard int found = 0; 6329b6b563cSPaul Mackerras 6339b6b563cSPaul Mackerras /* We are scanning "cpu" nodes only */ 6349b6b563cSPaul Mackerras if (type == NULL || strcmp(type, "cpu") != 0) 6359b6b563cSPaul Mackerras return 0; 6369b6b563cSPaul Mackerras 6374df20460SAnton Blanchard /* Get physical cpuid */ 6384df20460SAnton Blanchard intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len); 6394df20460SAnton Blanchard if (intserv) { 6404df20460SAnton Blanchard nthreads = len / sizeof(int); 6419b6b563cSPaul Mackerras } else { 6424df20460SAnton Blanchard intserv = of_get_flat_dt_prop(node, "reg", NULL); 6434df20460SAnton Blanchard nthreads = 1; 6444df20460SAnton Blanchard } 6454df20460SAnton Blanchard 6464df20460SAnton Blanchard /* 6474df20460SAnton Blanchard * Now see if any of these threads match our boot cpu. 6484df20460SAnton Blanchard * NOTE: This must match the parsing done in smp_setup_cpu_maps. 6494df20460SAnton Blanchard */ 6504df20460SAnton Blanchard for (i = 0; i < nthreads; i++) { 6514df20460SAnton Blanchard /* 6524df20460SAnton Blanchard * version 2 of the kexec param format adds the phys cpuid of 6534df20460SAnton Blanchard * booted proc. 6544df20460SAnton Blanchard */ 6554df20460SAnton Blanchard if (initial_boot_params && initial_boot_params->version >= 2) { 6564df20460SAnton Blanchard if (intserv[i] == 6574df20460SAnton Blanchard initial_boot_params->boot_cpuid_phys) { 6584df20460SAnton Blanchard found = 1; 6594df20460SAnton Blanchard break; 6604df20460SAnton Blanchard } 6614df20460SAnton Blanchard } else { 6624df20460SAnton Blanchard /* 6634df20460SAnton Blanchard * Check if it's the boot-cpu, set it's hw index now, 6644df20460SAnton Blanchard * unfortunately this format did not support booting 6654df20460SAnton Blanchard * off secondary threads. 6664df20460SAnton Blanchard */ 6673c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, 6683c726f8dSBenjamin Herrenschmidt "linux,boot-cpu", NULL) != NULL) { 6694df20460SAnton Blanchard found = 1; 6704df20460SAnton Blanchard break; 6719b6b563cSPaul Mackerras } 6729b6b563cSPaul Mackerras } 6734df20460SAnton Blanchard 6744df20460SAnton Blanchard #ifdef CONFIG_SMP 6754df20460SAnton Blanchard /* logical cpu id is always 0 on UP kernels */ 6764df20460SAnton Blanchard logical_cpuid++; 6774df20460SAnton Blanchard #endif 6784df20460SAnton Blanchard } 6794df20460SAnton Blanchard 6804df20460SAnton Blanchard if (found) { 6814df20460SAnton Blanchard DBG("boot cpu: logical %d physical %d\n", logical_cpuid, 6824df20460SAnton Blanchard intserv[i]); 6834df20460SAnton Blanchard boot_cpuid = logical_cpuid; 6844df20460SAnton Blanchard set_hard_smp_processor_id(boot_cpuid, intserv[i]); 685*974a76f5SPaul Mackerras 686*974a76f5SPaul Mackerras /* 687*974a76f5SPaul Mackerras * PAPR defines "logical" PVR values for cpus that 688*974a76f5SPaul Mackerras * meet various levels of the architecture: 689*974a76f5SPaul Mackerras * 0x0f000001 Architecture version 2.04 690*974a76f5SPaul Mackerras * 0x0f000002 Architecture version 2.05 691*974a76f5SPaul Mackerras * If the cpu-version property in the cpu node contains 692*974a76f5SPaul Mackerras * such a value, we call identify_cpu again with the 693*974a76f5SPaul Mackerras * logical PVR value in order to use the cpu feature 694*974a76f5SPaul Mackerras * bits appropriate for the architecture level. 695*974a76f5SPaul Mackerras * 696*974a76f5SPaul Mackerras * A POWER6 partition in "POWER6 architected" mode 697*974a76f5SPaul Mackerras * uses the 0x0f000002 PVR value; in POWER5+ mode 698*974a76f5SPaul Mackerras * it uses 0x0f000001. 699*974a76f5SPaul Mackerras */ 700*974a76f5SPaul Mackerras prop = of_get_flat_dt_prop(node, "cpu-version", NULL); 701*974a76f5SPaul Mackerras if (prop && (*prop & 0xff000000) == 0x0f000000) 702*974a76f5SPaul Mackerras identify_cpu(0, *prop); 7034df20460SAnton Blanchard } 7049b6b563cSPaul Mackerras 705*974a76f5SPaul Mackerras check_cpu_feature_properties(node); 706d205819eSPaul Mackerras check_cpu_pa_features(node); 707d205819eSPaul Mackerras 7089b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 7094df20460SAnton Blanchard if (nthreads > 1) 7109b6b563cSPaul Mackerras cur_cpu_spec->cpu_features |= CPU_FTR_SMT; 7114df20460SAnton Blanchard else 7124df20460SAnton Blanchard cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT; 7139b6b563cSPaul Mackerras #endif 7149b6b563cSPaul Mackerras 7159b6b563cSPaul Mackerras return 0; 7169b6b563cSPaul Mackerras } 7179b6b563cSPaul Mackerras 7189b6b563cSPaul Mackerras static int __init early_init_dt_scan_chosen(unsigned long node, 7199b6b563cSPaul Mackerras const char *uname, int depth, void *data) 7209b6b563cSPaul Mackerras { 7219b6b563cSPaul Mackerras unsigned long *lprop; 722329dda08SKumar Gala unsigned long l; 723329dda08SKumar Gala char *p; 7249b6b563cSPaul Mackerras 7259b6b563cSPaul Mackerras DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname); 7269b6b563cSPaul Mackerras 727a575b807SPaul Mackerras if (depth != 1 || 728a575b807SPaul Mackerras (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) 7299b6b563cSPaul Mackerras return 0; 7309b6b563cSPaul Mackerras 7319b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 7329b6b563cSPaul Mackerras /* check if iommu is forced on or off */ 7333c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL) 7349b6b563cSPaul Mackerras iommu_is_off = 1; 7353c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL) 7369b6b563cSPaul Mackerras iommu_force_on = 1; 7379b6b563cSPaul Mackerras #endif 7389b6b563cSPaul Mackerras 7392babf5c2SMichael Ellerman /* mem=x on the command line is the preferred mechanism */ 7403c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL); 7419b6b563cSPaul Mackerras if (lprop) 7429b6b563cSPaul Mackerras memory_limit = *lprop; 7439b6b563cSPaul Mackerras 7449b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 7453c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL); 7469b6b563cSPaul Mackerras if (lprop) 7479b6b563cSPaul Mackerras tce_alloc_start = *lprop; 7483c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL); 7499b6b563cSPaul Mackerras if (lprop) 7509b6b563cSPaul Mackerras tce_alloc_end = *lprop; 7519b6b563cSPaul Mackerras #endif 7529b6b563cSPaul Mackerras 753dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC 754dcee3036SMichael Ellerman lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL); 755dcee3036SMichael Ellerman if (lprop) 756dcee3036SMichael Ellerman crashk_res.start = *lprop; 757dcee3036SMichael Ellerman 758dcee3036SMichael Ellerman lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL); 759dcee3036SMichael Ellerman if (lprop) 760dcee3036SMichael Ellerman crashk_res.end = crashk_res.start + *lprop - 1; 761dcee3036SMichael Ellerman #endif 762dcee3036SMichael Ellerman 763329dda08SKumar Gala /* Retreive command line */ 764329dda08SKumar Gala p = of_get_flat_dt_prop(node, "bootargs", &l); 765329dda08SKumar Gala if (p != NULL && l > 0) 766329dda08SKumar Gala strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE)); 767329dda08SKumar Gala 768329dda08SKumar Gala #ifdef CONFIG_CMDLINE 769c1ce464dSGeoff Levand if (p == NULL || l == 0 || (l == 1 && (*p) == 0)) 770329dda08SKumar Gala strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); 771329dda08SKumar Gala #endif /* CONFIG_CMDLINE */ 772329dda08SKumar Gala 773329dda08SKumar Gala DBG("Command line is: %s\n", cmd_line); 774329dda08SKumar Gala 7759b6b563cSPaul Mackerras /* break now */ 7769b6b563cSPaul Mackerras return 1; 7779b6b563cSPaul Mackerras } 7789b6b563cSPaul Mackerras 7799b6b563cSPaul Mackerras static int __init early_init_dt_scan_root(unsigned long node, 7809b6b563cSPaul Mackerras const char *uname, int depth, void *data) 7819b6b563cSPaul Mackerras { 7829b6b563cSPaul Mackerras u32 *prop; 7839b6b563cSPaul Mackerras 7849b6b563cSPaul Mackerras if (depth != 0) 7859b6b563cSPaul Mackerras return 0; 7869b6b563cSPaul Mackerras 7873c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "#size-cells", NULL); 7889b6b563cSPaul Mackerras dt_root_size_cells = (prop == NULL) ? 1 : *prop; 7899b6b563cSPaul Mackerras DBG("dt_root_size_cells = %x\n", dt_root_size_cells); 7909b6b563cSPaul Mackerras 7913c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "#address-cells", NULL); 7929b6b563cSPaul Mackerras dt_root_addr_cells = (prop == NULL) ? 2 : *prop; 7939b6b563cSPaul Mackerras DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells); 7949b6b563cSPaul Mackerras 7959b6b563cSPaul Mackerras /* break now */ 7969b6b563cSPaul Mackerras return 1; 7979b6b563cSPaul Mackerras } 7989b6b563cSPaul Mackerras 7999b6b563cSPaul Mackerras static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp) 8009b6b563cSPaul Mackerras { 8019b6b563cSPaul Mackerras cell_t *p = *cellp; 8029b6b563cSPaul Mackerras 803a4dc7ff0SPaul Mackerras *cellp = p + s; 804a4dc7ff0SPaul Mackerras return of_read_ulong(p, s); 8059b6b563cSPaul Mackerras } 8069b6b563cSPaul Mackerras 8079b6b563cSPaul Mackerras 8089b6b563cSPaul Mackerras static int __init early_init_dt_scan_memory(unsigned long node, 8099b6b563cSPaul Mackerras const char *uname, int depth, void *data) 8109b6b563cSPaul Mackerras { 8113c726f8dSBenjamin Herrenschmidt char *type = of_get_flat_dt_prop(node, "device_type", NULL); 8129b6b563cSPaul Mackerras cell_t *reg, *endp; 8139b6b563cSPaul Mackerras unsigned long l; 8149b6b563cSPaul Mackerras 8159b6b563cSPaul Mackerras /* We are scanning "memory" nodes only */ 816a23414beSPaul Mackerras if (type == NULL) { 817a23414beSPaul Mackerras /* 818a23414beSPaul Mackerras * The longtrail doesn't have a device_type on the 819a23414beSPaul Mackerras * /memory node, so look for the node called /memory@0. 820a23414beSPaul Mackerras */ 821a23414beSPaul Mackerras if (depth != 1 || strcmp(uname, "memory@0") != 0) 822a23414beSPaul Mackerras return 0; 823a23414beSPaul Mackerras } else if (strcmp(type, "memory") != 0) 8249b6b563cSPaul Mackerras return 0; 8259b6b563cSPaul Mackerras 826ba759485SMichael Ellerman reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l); 827ba759485SMichael Ellerman if (reg == NULL) 8283c726f8dSBenjamin Herrenschmidt reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l); 8299b6b563cSPaul Mackerras if (reg == NULL) 8309b6b563cSPaul Mackerras return 0; 8319b6b563cSPaul Mackerras 8329b6b563cSPaul Mackerras endp = reg + (l / sizeof(cell_t)); 8339b6b563cSPaul Mackerras 834358c86fdSMichael Ellerman DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n", 8359b6b563cSPaul Mackerras uname, l, reg[0], reg[1], reg[2], reg[3]); 8369b6b563cSPaul Mackerras 8379b6b563cSPaul Mackerras while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { 8389b6b563cSPaul Mackerras unsigned long base, size; 8399b6b563cSPaul Mackerras 8409b6b563cSPaul Mackerras base = dt_mem_next_cell(dt_root_addr_cells, ®); 8419b6b563cSPaul Mackerras size = dt_mem_next_cell(dt_root_size_cells, ®); 8429b6b563cSPaul Mackerras 8439b6b563cSPaul Mackerras if (size == 0) 8449b6b563cSPaul Mackerras continue; 8459b6b563cSPaul Mackerras DBG(" - %lx , %lx\n", base, size); 8469b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 8479b6b563cSPaul Mackerras if (iommu_is_off) { 8489b6b563cSPaul Mackerras if (base >= 0x80000000ul) 8499b6b563cSPaul Mackerras continue; 8509b6b563cSPaul Mackerras if ((base + size) > 0x80000000ul) 8519b6b563cSPaul Mackerras size = 0x80000000ul - base; 8529b6b563cSPaul Mackerras } 8539b6b563cSPaul Mackerras #endif 8549b6b563cSPaul Mackerras lmb_add(base, size); 8559b6b563cSPaul Mackerras } 8569b6b563cSPaul Mackerras return 0; 8579b6b563cSPaul Mackerras } 8589b6b563cSPaul Mackerras 8599b6b563cSPaul Mackerras static void __init early_reserve_mem(void) 8609b6b563cSPaul Mackerras { 861cbbcf340SKumar Gala u64 base, size; 862cbbcf340SKumar Gala u64 *reserve_map; 8638a300887SJon Loeliger unsigned long self_base; 8648a300887SJon Loeliger unsigned long self_size; 8659b6b563cSPaul Mackerras 866cbbcf340SKumar Gala reserve_map = (u64 *)(((unsigned long)initial_boot_params) + 8679b6b563cSPaul Mackerras initial_boot_params->off_mem_rsvmap); 8684d1f3f25SJimi Xenidis 8694d1f3f25SJimi Xenidis /* before we do anything, lets reserve the dt blob */ 8708a300887SJon Loeliger self_base = __pa((unsigned long)initial_boot_params); 8718a300887SJon Loeliger self_size = initial_boot_params->totalsize; 8728a300887SJon Loeliger lmb_reserve(self_base, self_size); 8734d1f3f25SJimi Xenidis 874cbbcf340SKumar Gala #ifdef CONFIG_PPC32 875cbbcf340SKumar Gala /* 876cbbcf340SKumar Gala * Handle the case where we might be booting from an old kexec 877cbbcf340SKumar Gala * image that setup the mem_rsvmap as pairs of 32-bit values 878cbbcf340SKumar Gala */ 879cbbcf340SKumar Gala if (*reserve_map > 0xffffffffull) { 880cbbcf340SKumar Gala u32 base_32, size_32; 881cbbcf340SKumar Gala u32 *reserve_map_32 = (u32 *)reserve_map; 882cbbcf340SKumar Gala 883cbbcf340SKumar Gala while (1) { 884cbbcf340SKumar Gala base_32 = *(reserve_map_32++); 885cbbcf340SKumar Gala size_32 = *(reserve_map_32++); 886cbbcf340SKumar Gala if (size_32 == 0) 887cbbcf340SKumar Gala break; 8888a300887SJon Loeliger /* skip if the reservation is for the blob */ 8898a300887SJon Loeliger if (base_32 == self_base && size_32 == self_size) 8908a300887SJon Loeliger continue; 891329dda08SKumar Gala DBG("reserving: %x -> %x\n", base_32, size_32); 892cbbcf340SKumar Gala lmb_reserve(base_32, size_32); 893cbbcf340SKumar Gala } 894cbbcf340SKumar Gala return; 895cbbcf340SKumar Gala } 896cbbcf340SKumar Gala #endif 8979b6b563cSPaul Mackerras while (1) { 8989b6b563cSPaul Mackerras base = *(reserve_map++); 8999b6b563cSPaul Mackerras size = *(reserve_map++); 9009b6b563cSPaul Mackerras if (size == 0) 9019b6b563cSPaul Mackerras break; 9028a300887SJon Loeliger /* skip if the reservation is for the blob */ 9038a300887SJon Loeliger if (base == self_base && size == self_size) 9048a300887SJon Loeliger continue; 905cbbcf340SKumar Gala DBG("reserving: %llx -> %llx\n", base, size); 9069b6b563cSPaul Mackerras lmb_reserve(base, size); 9079b6b563cSPaul Mackerras } 9089b6b563cSPaul Mackerras 9099b6b563cSPaul Mackerras #if 0 9109b6b563cSPaul Mackerras DBG("memory reserved, lmbs :\n"); 9119b6b563cSPaul Mackerras lmb_dump_all(); 9129b6b563cSPaul Mackerras #endif 9139b6b563cSPaul Mackerras } 9149b6b563cSPaul Mackerras 9159b6b563cSPaul Mackerras void __init early_init_devtree(void *params) 9169b6b563cSPaul Mackerras { 9179b6b563cSPaul Mackerras DBG(" -> early_init_devtree()\n"); 9189b6b563cSPaul Mackerras 9199b6b563cSPaul Mackerras /* Setup flat device-tree pointer */ 9209b6b563cSPaul Mackerras initial_boot_params = params; 9219b6b563cSPaul Mackerras 922458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS 923458148c0SMichael Ellerman /* Some machines might need RTAS info for debugging, grab it now. */ 924458148c0SMichael Ellerman of_scan_flat_dt(early_init_dt_scan_rtas, NULL); 925458148c0SMichael Ellerman #endif 926458148c0SMichael Ellerman 9279b6b563cSPaul Mackerras /* Retrieve various informations from the /chosen node of the 9289b6b563cSPaul Mackerras * device-tree, including the platform type, initrd location and 9299b6b563cSPaul Mackerras * size, TCE reserve, and more ... 9309b6b563cSPaul Mackerras */ 9313c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_chosen, NULL); 9329b6b563cSPaul Mackerras 9339b6b563cSPaul Mackerras /* Scan memory nodes and rebuild LMBs */ 9349b6b563cSPaul Mackerras lmb_init(); 9353c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_root, NULL); 9363c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_memory, NULL); 937846f77b0SMichael Ellerman 938846f77b0SMichael Ellerman /* Save command line for /proc/cmdline and then parse parameters */ 939846f77b0SMichael Ellerman strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE); 940846f77b0SMichael Ellerman parse_early_param(); 941846f77b0SMichael Ellerman 9429b6b563cSPaul Mackerras /* Reserve LMB regions used by kernel, initrd, dt, etc... */ 9430cc4746cSMichael Ellerman lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START); 94447310413SMichael Ellerman reserve_kdump_trampoline(); 94535dd5432SMichael Ellerman reserve_crashkernel(); 9469b6b563cSPaul Mackerras early_reserve_mem(); 9479b6b563cSPaul Mackerras 9482babf5c2SMichael Ellerman lmb_enforce_memory_limit(memory_limit); 9492babf5c2SMichael Ellerman lmb_analyze(); 9502babf5c2SMichael Ellerman 9512babf5c2SMichael Ellerman DBG("Phys. mem: %lx\n", lmb_phys_mem_size()); 9522babf5c2SMichael Ellerman 9532babf5c2SMichael Ellerman /* We may need to relocate the flat tree, do it now. 9542babf5c2SMichael Ellerman * FIXME .. and the initrd too? */ 9552babf5c2SMichael Ellerman move_device_tree(); 9562babf5c2SMichael Ellerman 9579b6b563cSPaul Mackerras DBG("Scanning CPUs ...\n"); 9589b6b563cSPaul Mackerras 9593c726f8dSBenjamin Herrenschmidt /* Retreive CPU related informations from the flat tree 9603c726f8dSBenjamin Herrenschmidt * (altivec support, boot CPU ID, ...) 9619b6b563cSPaul Mackerras */ 9623c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_cpus, NULL); 9639b6b563cSPaul Mackerras 9649b6b563cSPaul Mackerras DBG(" <- early_init_devtree()\n"); 9659b6b563cSPaul Mackerras } 9669b6b563cSPaul Mackerras 9679b6b563cSPaul Mackerras #undef printk 9689b6b563cSPaul Mackerras 9699b6b563cSPaul Mackerras int 9709b6b563cSPaul Mackerras prom_n_addr_cells(struct device_node* np) 9719b6b563cSPaul Mackerras { 972a7f67bdfSJeremy Kerr const int *ip; 9739b6b563cSPaul Mackerras do { 9749b6b563cSPaul Mackerras if (np->parent) 9759b6b563cSPaul Mackerras np = np->parent; 976a7f67bdfSJeremy Kerr ip = get_property(np, "#address-cells", NULL); 9779b6b563cSPaul Mackerras if (ip != NULL) 9789b6b563cSPaul Mackerras return *ip; 9799b6b563cSPaul Mackerras } while (np->parent); 9809b6b563cSPaul Mackerras /* No #address-cells property for the root node, default to 1 */ 9819b6b563cSPaul Mackerras return 1; 9829b6b563cSPaul Mackerras } 9831dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_addr_cells); 9849b6b563cSPaul Mackerras 9859b6b563cSPaul Mackerras int 9869b6b563cSPaul Mackerras prom_n_size_cells(struct device_node* np) 9879b6b563cSPaul Mackerras { 988a7f67bdfSJeremy Kerr const int* ip; 9899b6b563cSPaul Mackerras do { 9909b6b563cSPaul Mackerras if (np->parent) 9919b6b563cSPaul Mackerras np = np->parent; 992a7f67bdfSJeremy Kerr ip = get_property(np, "#size-cells", NULL); 9939b6b563cSPaul Mackerras if (ip != NULL) 9949b6b563cSPaul Mackerras return *ip; 9959b6b563cSPaul Mackerras } while (np->parent); 9969b6b563cSPaul Mackerras /* No #size-cells property for the root node, default to 1 */ 9979b6b563cSPaul Mackerras return 1; 9989b6b563cSPaul Mackerras } 9991dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_size_cells); 10009b6b563cSPaul Mackerras 10019b6b563cSPaul Mackerras /** 10029b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given name. 10039b6b563cSPaul Mackerras */ 10049b6b563cSPaul Mackerras struct device_node *find_devices(const char *name) 10059b6b563cSPaul Mackerras { 10069b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 10079b6b563cSPaul Mackerras 10089b6b563cSPaul Mackerras prevp = &head; 10099b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 10109b6b563cSPaul Mackerras if (np->name != 0 && strcasecmp(np->name, name) == 0) { 10119b6b563cSPaul Mackerras *prevp = np; 10129b6b563cSPaul Mackerras prevp = &np->next; 10139b6b563cSPaul Mackerras } 10149b6b563cSPaul Mackerras } 10159b6b563cSPaul Mackerras *prevp = NULL; 10169b6b563cSPaul Mackerras return head; 10179b6b563cSPaul Mackerras } 10189b6b563cSPaul Mackerras EXPORT_SYMBOL(find_devices); 10199b6b563cSPaul Mackerras 10209b6b563cSPaul Mackerras /** 10219b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type. 10229b6b563cSPaul Mackerras */ 10239b6b563cSPaul Mackerras struct device_node *find_type_devices(const char *type) 10249b6b563cSPaul Mackerras { 10259b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 10269b6b563cSPaul Mackerras 10279b6b563cSPaul Mackerras prevp = &head; 10289b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 10299b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0) { 10309b6b563cSPaul Mackerras *prevp = np; 10319b6b563cSPaul Mackerras prevp = &np->next; 10329b6b563cSPaul Mackerras } 10339b6b563cSPaul Mackerras } 10349b6b563cSPaul Mackerras *prevp = NULL; 10359b6b563cSPaul Mackerras return head; 10369b6b563cSPaul Mackerras } 10379b6b563cSPaul Mackerras EXPORT_SYMBOL(find_type_devices); 10389b6b563cSPaul Mackerras 10399b6b563cSPaul Mackerras /** 10409b6b563cSPaul Mackerras * Returns all nodes linked together 10419b6b563cSPaul Mackerras */ 10429b6b563cSPaul Mackerras struct device_node *find_all_nodes(void) 10439b6b563cSPaul Mackerras { 10449b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 10459b6b563cSPaul Mackerras 10469b6b563cSPaul Mackerras prevp = &head; 10479b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 10489b6b563cSPaul Mackerras *prevp = np; 10499b6b563cSPaul Mackerras prevp = &np->next; 10509b6b563cSPaul Mackerras } 10519b6b563cSPaul Mackerras *prevp = NULL; 10529b6b563cSPaul Mackerras return head; 10539b6b563cSPaul Mackerras } 10549b6b563cSPaul Mackerras EXPORT_SYMBOL(find_all_nodes); 10559b6b563cSPaul Mackerras 10569b6b563cSPaul Mackerras /** Checks if the given "compat" string matches one of the strings in 10579b6b563cSPaul Mackerras * the device's "compatible" property 10589b6b563cSPaul Mackerras */ 1059e2100efbSBenjamin Herrenschmidt int device_is_compatible(const struct device_node *device, const char *compat) 10609b6b563cSPaul Mackerras { 10619b6b563cSPaul Mackerras const char* cp; 10629b6b563cSPaul Mackerras int cplen, l; 10639b6b563cSPaul Mackerras 1064a7f67bdfSJeremy Kerr cp = get_property(device, "compatible", &cplen); 10659b6b563cSPaul Mackerras if (cp == NULL) 10669b6b563cSPaul Mackerras return 0; 10679b6b563cSPaul Mackerras while (cplen > 0) { 10689b6b563cSPaul Mackerras if (strncasecmp(cp, compat, strlen(compat)) == 0) 10699b6b563cSPaul Mackerras return 1; 10709b6b563cSPaul Mackerras l = strlen(cp) + 1; 10719b6b563cSPaul Mackerras cp += l; 10729b6b563cSPaul Mackerras cplen -= l; 10739b6b563cSPaul Mackerras } 10749b6b563cSPaul Mackerras 10759b6b563cSPaul Mackerras return 0; 10769b6b563cSPaul Mackerras } 10779b6b563cSPaul Mackerras EXPORT_SYMBOL(device_is_compatible); 10789b6b563cSPaul Mackerras 10799b6b563cSPaul Mackerras 10809b6b563cSPaul Mackerras /** 10819b6b563cSPaul Mackerras * Indicates whether the root node has a given value in its 10829b6b563cSPaul Mackerras * compatible property. 10839b6b563cSPaul Mackerras */ 10849b6b563cSPaul Mackerras int machine_is_compatible(const char *compat) 10859b6b563cSPaul Mackerras { 10869b6b563cSPaul Mackerras struct device_node *root; 10879b6b563cSPaul Mackerras int rc = 0; 10889b6b563cSPaul Mackerras 10899b6b563cSPaul Mackerras root = of_find_node_by_path("/"); 10909b6b563cSPaul Mackerras if (root) { 10919b6b563cSPaul Mackerras rc = device_is_compatible(root, compat); 10929b6b563cSPaul Mackerras of_node_put(root); 10939b6b563cSPaul Mackerras } 10949b6b563cSPaul Mackerras return rc; 10959b6b563cSPaul Mackerras } 10969b6b563cSPaul Mackerras EXPORT_SYMBOL(machine_is_compatible); 10979b6b563cSPaul Mackerras 10989b6b563cSPaul Mackerras /** 10999b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type 11009b6b563cSPaul Mackerras * and compatible property. 11019b6b563cSPaul Mackerras */ 11029b6b563cSPaul Mackerras struct device_node *find_compatible_devices(const char *type, 11039b6b563cSPaul Mackerras const char *compat) 11049b6b563cSPaul Mackerras { 11059b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 11069b6b563cSPaul Mackerras 11079b6b563cSPaul Mackerras prevp = &head; 11089b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 11099b6b563cSPaul Mackerras if (type != NULL 11109b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 11119b6b563cSPaul Mackerras continue; 11129b6b563cSPaul Mackerras if (device_is_compatible(np, compat)) { 11139b6b563cSPaul Mackerras *prevp = np; 11149b6b563cSPaul Mackerras prevp = &np->next; 11159b6b563cSPaul Mackerras } 11169b6b563cSPaul Mackerras } 11179b6b563cSPaul Mackerras *prevp = NULL; 11189b6b563cSPaul Mackerras return head; 11199b6b563cSPaul Mackerras } 11209b6b563cSPaul Mackerras EXPORT_SYMBOL(find_compatible_devices); 11219b6b563cSPaul Mackerras 11229b6b563cSPaul Mackerras /** 11239b6b563cSPaul Mackerras * Find the device_node with a given full_name. 11249b6b563cSPaul Mackerras */ 11259b6b563cSPaul Mackerras struct device_node *find_path_device(const char *path) 11269b6b563cSPaul Mackerras { 11279b6b563cSPaul Mackerras struct device_node *np; 11289b6b563cSPaul Mackerras 11299b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 11309b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) 11319b6b563cSPaul Mackerras return np; 11329b6b563cSPaul Mackerras return NULL; 11339b6b563cSPaul Mackerras } 11349b6b563cSPaul Mackerras EXPORT_SYMBOL(find_path_device); 11359b6b563cSPaul Mackerras 11369b6b563cSPaul Mackerras /******* 11379b6b563cSPaul Mackerras * 11389b6b563cSPaul Mackerras * New implementation of the OF "find" APIs, return a refcounted 11399b6b563cSPaul Mackerras * object, call of_node_put() when done. The device tree and list 11409b6b563cSPaul Mackerras * are protected by a rw_lock. 11419b6b563cSPaul Mackerras * 11429b6b563cSPaul Mackerras * Note that property management will need some locking as well, 11439b6b563cSPaul Mackerras * this isn't dealt with yet. 11449b6b563cSPaul Mackerras * 11459b6b563cSPaul Mackerras *******/ 11469b6b563cSPaul Mackerras 11479b6b563cSPaul Mackerras /** 11489b6b563cSPaul Mackerras * of_find_node_by_name - Find a node by its "name" property 11499b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 11509b6b563cSPaul Mackerras * you pass will not be searched, only the next one 11519b6b563cSPaul Mackerras * will; typically, you pass what the previous call 11529b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 11539b6b563cSPaul Mackerras * @name: The name string to match against 11549b6b563cSPaul Mackerras * 11559b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 11569b6b563cSPaul Mackerras * of_node_put() on it when done. 11579b6b563cSPaul Mackerras */ 11589b6b563cSPaul Mackerras struct device_node *of_find_node_by_name(struct device_node *from, 11599b6b563cSPaul Mackerras const char *name) 11609b6b563cSPaul Mackerras { 11619b6b563cSPaul Mackerras struct device_node *np; 11629b6b563cSPaul Mackerras 11639b6b563cSPaul Mackerras read_lock(&devtree_lock); 11649b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 1165090db7c8SOlaf Hering for (; np != NULL; np = np->allnext) 1166090db7c8SOlaf Hering if (np->name != NULL && strcasecmp(np->name, name) == 0 11679b6b563cSPaul Mackerras && of_node_get(np)) 11689b6b563cSPaul Mackerras break; 11699b6b563cSPaul Mackerras if (from) 11709b6b563cSPaul Mackerras of_node_put(from); 11719b6b563cSPaul Mackerras read_unlock(&devtree_lock); 11729b6b563cSPaul Mackerras return np; 11739b6b563cSPaul Mackerras } 11749b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_name); 11759b6b563cSPaul Mackerras 11769b6b563cSPaul Mackerras /** 11779b6b563cSPaul Mackerras * of_find_node_by_type - Find a node by its "device_type" property 11789b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 11799b6b563cSPaul Mackerras * you pass will not be searched, only the next one 11809b6b563cSPaul Mackerras * will; typically, you pass what the previous call 11819b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 11829b6b563cSPaul Mackerras * @name: The type string to match against 11839b6b563cSPaul Mackerras * 11849b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 11859b6b563cSPaul Mackerras * of_node_put() on it when done. 11869b6b563cSPaul Mackerras */ 11879b6b563cSPaul Mackerras struct device_node *of_find_node_by_type(struct device_node *from, 11889b6b563cSPaul Mackerras const char *type) 11899b6b563cSPaul Mackerras { 11909b6b563cSPaul Mackerras struct device_node *np; 11919b6b563cSPaul Mackerras 11929b6b563cSPaul Mackerras read_lock(&devtree_lock); 11939b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 11949b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 11959b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0 11969b6b563cSPaul Mackerras && of_node_get(np)) 11979b6b563cSPaul Mackerras break; 11989b6b563cSPaul Mackerras if (from) 11999b6b563cSPaul Mackerras of_node_put(from); 12009b6b563cSPaul Mackerras read_unlock(&devtree_lock); 12019b6b563cSPaul Mackerras return np; 12029b6b563cSPaul Mackerras } 12039b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_type); 12049b6b563cSPaul Mackerras 12059b6b563cSPaul Mackerras /** 12069b6b563cSPaul Mackerras * of_find_compatible_node - Find a node based on type and one of the 12079b6b563cSPaul Mackerras * tokens in its "compatible" property 12089b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 12099b6b563cSPaul Mackerras * you pass will not be searched, only the next one 12109b6b563cSPaul Mackerras * will; typically, you pass what the previous call 12119b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 12129b6b563cSPaul Mackerras * @type: The type string to match "device_type" or NULL to ignore 12139b6b563cSPaul Mackerras * @compatible: The string to match to one of the tokens in the device 12149b6b563cSPaul Mackerras * "compatible" list. 12159b6b563cSPaul Mackerras * 12169b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 12179b6b563cSPaul Mackerras * of_node_put() on it when done. 12189b6b563cSPaul Mackerras */ 12199b6b563cSPaul Mackerras struct device_node *of_find_compatible_node(struct device_node *from, 12209b6b563cSPaul Mackerras const char *type, const char *compatible) 12219b6b563cSPaul Mackerras { 12229b6b563cSPaul Mackerras struct device_node *np; 12239b6b563cSPaul Mackerras 12249b6b563cSPaul Mackerras read_lock(&devtree_lock); 12259b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 12269b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 12279b6b563cSPaul Mackerras if (type != NULL 12289b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 12299b6b563cSPaul Mackerras continue; 12309b6b563cSPaul Mackerras if (device_is_compatible(np, compatible) && of_node_get(np)) 12319b6b563cSPaul Mackerras break; 12329b6b563cSPaul Mackerras } 12339b6b563cSPaul Mackerras if (from) 12349b6b563cSPaul Mackerras of_node_put(from); 12359b6b563cSPaul Mackerras read_unlock(&devtree_lock); 12369b6b563cSPaul Mackerras return np; 12379b6b563cSPaul Mackerras } 12389b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_compatible_node); 12399b6b563cSPaul Mackerras 12409b6b563cSPaul Mackerras /** 12419b6b563cSPaul Mackerras * of_find_node_by_path - Find a node matching a full OF path 12429b6b563cSPaul Mackerras * @path: The full path to match 12439b6b563cSPaul Mackerras * 12449b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 12459b6b563cSPaul Mackerras * of_node_put() on it when done. 12469b6b563cSPaul Mackerras */ 12479b6b563cSPaul Mackerras struct device_node *of_find_node_by_path(const char *path) 12489b6b563cSPaul Mackerras { 12499b6b563cSPaul Mackerras struct device_node *np = allnodes; 12509b6b563cSPaul Mackerras 12519b6b563cSPaul Mackerras read_lock(&devtree_lock); 12529b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 12539b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0 12549b6b563cSPaul Mackerras && of_node_get(np)) 12559b6b563cSPaul Mackerras break; 12569b6b563cSPaul Mackerras } 12579b6b563cSPaul Mackerras read_unlock(&devtree_lock); 12589b6b563cSPaul Mackerras return np; 12599b6b563cSPaul Mackerras } 12609b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_path); 12619b6b563cSPaul Mackerras 12629b6b563cSPaul Mackerras /** 12639b6b563cSPaul Mackerras * of_find_node_by_phandle - Find a node given a phandle 12649b6b563cSPaul Mackerras * @handle: phandle of the node to find 12659b6b563cSPaul Mackerras * 12669b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 12679b6b563cSPaul Mackerras * of_node_put() on it when done. 12689b6b563cSPaul Mackerras */ 12699b6b563cSPaul Mackerras struct device_node *of_find_node_by_phandle(phandle handle) 12709b6b563cSPaul Mackerras { 12719b6b563cSPaul Mackerras struct device_node *np; 12729b6b563cSPaul Mackerras 12739b6b563cSPaul Mackerras read_lock(&devtree_lock); 12749b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 12759b6b563cSPaul Mackerras if (np->linux_phandle == handle) 12769b6b563cSPaul Mackerras break; 12779b6b563cSPaul Mackerras if (np) 12789b6b563cSPaul Mackerras of_node_get(np); 12799b6b563cSPaul Mackerras read_unlock(&devtree_lock); 12809b6b563cSPaul Mackerras return np; 12819b6b563cSPaul Mackerras } 12829b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_phandle); 12839b6b563cSPaul Mackerras 12849b6b563cSPaul Mackerras /** 12859b6b563cSPaul Mackerras * of_find_all_nodes - Get next node in global list 12869b6b563cSPaul Mackerras * @prev: Previous node or NULL to start iteration 12879b6b563cSPaul Mackerras * of_node_put() will be called on it 12889b6b563cSPaul Mackerras * 12899b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 12909b6b563cSPaul Mackerras * of_node_put() on it when done. 12919b6b563cSPaul Mackerras */ 12929b6b563cSPaul Mackerras struct device_node *of_find_all_nodes(struct device_node *prev) 12939b6b563cSPaul Mackerras { 12949b6b563cSPaul Mackerras struct device_node *np; 12959b6b563cSPaul Mackerras 12969b6b563cSPaul Mackerras read_lock(&devtree_lock); 12979b6b563cSPaul Mackerras np = prev ? prev->allnext : allnodes; 12989b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 12999b6b563cSPaul Mackerras if (of_node_get(np)) 13009b6b563cSPaul Mackerras break; 13019b6b563cSPaul Mackerras if (prev) 13029b6b563cSPaul Mackerras of_node_put(prev); 13039b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13049b6b563cSPaul Mackerras return np; 13059b6b563cSPaul Mackerras } 13069b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_all_nodes); 13079b6b563cSPaul Mackerras 13089b6b563cSPaul Mackerras /** 13099b6b563cSPaul Mackerras * of_get_parent - Get a node's parent if any 13109b6b563cSPaul Mackerras * @node: Node to get parent 13119b6b563cSPaul Mackerras * 13129b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13139b6b563cSPaul Mackerras * of_node_put() on it when done. 13149b6b563cSPaul Mackerras */ 13159b6b563cSPaul Mackerras struct device_node *of_get_parent(const struct device_node *node) 13169b6b563cSPaul Mackerras { 13179b6b563cSPaul Mackerras struct device_node *np; 13189b6b563cSPaul Mackerras 13199b6b563cSPaul Mackerras if (!node) 13209b6b563cSPaul Mackerras return NULL; 13219b6b563cSPaul Mackerras 13229b6b563cSPaul Mackerras read_lock(&devtree_lock); 13239b6b563cSPaul Mackerras np = of_node_get(node->parent); 13249b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13259b6b563cSPaul Mackerras return np; 13269b6b563cSPaul Mackerras } 13279b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_parent); 13289b6b563cSPaul Mackerras 13299b6b563cSPaul Mackerras /** 13309b6b563cSPaul Mackerras * of_get_next_child - Iterate a node childs 13319b6b563cSPaul Mackerras * @node: parent node 13329b6b563cSPaul Mackerras * @prev: previous child of the parent node, or NULL to get first 13339b6b563cSPaul Mackerras * 13349b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13359b6b563cSPaul Mackerras * of_node_put() on it when done. 13369b6b563cSPaul Mackerras */ 13379b6b563cSPaul Mackerras struct device_node *of_get_next_child(const struct device_node *node, 13389b6b563cSPaul Mackerras struct device_node *prev) 13399b6b563cSPaul Mackerras { 13409b6b563cSPaul Mackerras struct device_node *next; 13419b6b563cSPaul Mackerras 13429b6b563cSPaul Mackerras read_lock(&devtree_lock); 13439b6b563cSPaul Mackerras next = prev ? prev->sibling : node->child; 13449b6b563cSPaul Mackerras for (; next != 0; next = next->sibling) 13459b6b563cSPaul Mackerras if (of_node_get(next)) 13469b6b563cSPaul Mackerras break; 13479b6b563cSPaul Mackerras if (prev) 13489b6b563cSPaul Mackerras of_node_put(prev); 13499b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13509b6b563cSPaul Mackerras return next; 13519b6b563cSPaul Mackerras } 13529b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_next_child); 13539b6b563cSPaul Mackerras 13549b6b563cSPaul Mackerras /** 13559b6b563cSPaul Mackerras * of_node_get - Increment refcount of a node 13569b6b563cSPaul Mackerras * @node: Node to inc refcount, NULL is supported to 13579b6b563cSPaul Mackerras * simplify writing of callers 13589b6b563cSPaul Mackerras * 13599b6b563cSPaul Mackerras * Returns node. 13609b6b563cSPaul Mackerras */ 13619b6b563cSPaul Mackerras struct device_node *of_node_get(struct device_node *node) 13629b6b563cSPaul Mackerras { 13639b6b563cSPaul Mackerras if (node) 13649b6b563cSPaul Mackerras kref_get(&node->kref); 13659b6b563cSPaul Mackerras return node; 13669b6b563cSPaul Mackerras } 13679b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_get); 13689b6b563cSPaul Mackerras 13699b6b563cSPaul Mackerras static inline struct device_node * kref_to_device_node(struct kref *kref) 13709b6b563cSPaul Mackerras { 13719b6b563cSPaul Mackerras return container_of(kref, struct device_node, kref); 13729b6b563cSPaul Mackerras } 13739b6b563cSPaul Mackerras 13749b6b563cSPaul Mackerras /** 13759b6b563cSPaul Mackerras * of_node_release - release a dynamically allocated node 13769b6b563cSPaul Mackerras * @kref: kref element of the node to be released 13779b6b563cSPaul Mackerras * 13789b6b563cSPaul Mackerras * In of_node_put() this function is passed to kref_put() 13799b6b563cSPaul Mackerras * as the destructor. 13809b6b563cSPaul Mackerras */ 13819b6b563cSPaul Mackerras static void of_node_release(struct kref *kref) 13829b6b563cSPaul Mackerras { 13839b6b563cSPaul Mackerras struct device_node *node = kref_to_device_node(kref); 13849b6b563cSPaul Mackerras struct property *prop = node->properties; 13859b6b563cSPaul Mackerras 13869b6b563cSPaul Mackerras if (!OF_IS_DYNAMIC(node)) 13879b6b563cSPaul Mackerras return; 13889b6b563cSPaul Mackerras while (prop) { 13899b6b563cSPaul Mackerras struct property *next = prop->next; 13909b6b563cSPaul Mackerras kfree(prop->name); 13919b6b563cSPaul Mackerras kfree(prop->value); 13929b6b563cSPaul Mackerras kfree(prop); 13939b6b563cSPaul Mackerras prop = next; 1394088186deSDave C Boutcher 1395088186deSDave C Boutcher if (!prop) { 1396088186deSDave C Boutcher prop = node->deadprops; 1397088186deSDave C Boutcher node->deadprops = NULL; 1398088186deSDave C Boutcher } 13999b6b563cSPaul Mackerras } 14009b6b563cSPaul Mackerras kfree(node->full_name); 14019b6b563cSPaul Mackerras kfree(node->data); 14029b6b563cSPaul Mackerras kfree(node); 14039b6b563cSPaul Mackerras } 14049b6b563cSPaul Mackerras 14059b6b563cSPaul Mackerras /** 14069b6b563cSPaul Mackerras * of_node_put - Decrement refcount of a node 14079b6b563cSPaul Mackerras * @node: Node to dec refcount, NULL is supported to 14089b6b563cSPaul Mackerras * simplify writing of callers 14099b6b563cSPaul Mackerras * 14109b6b563cSPaul Mackerras */ 14119b6b563cSPaul Mackerras void of_node_put(struct device_node *node) 14129b6b563cSPaul Mackerras { 14139b6b563cSPaul Mackerras if (node) 14149b6b563cSPaul Mackerras kref_put(&node->kref, of_node_release); 14159b6b563cSPaul Mackerras } 14169b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_put); 14179b6b563cSPaul Mackerras 14189b6b563cSPaul Mackerras /* 14199b6b563cSPaul Mackerras * Plug a device node into the tree and global list. 14209b6b563cSPaul Mackerras */ 14219b6b563cSPaul Mackerras void of_attach_node(struct device_node *np) 14229b6b563cSPaul Mackerras { 14239b6b563cSPaul Mackerras write_lock(&devtree_lock); 14249b6b563cSPaul Mackerras np->sibling = np->parent->child; 14259b6b563cSPaul Mackerras np->allnext = allnodes; 14269b6b563cSPaul Mackerras np->parent->child = np; 14279b6b563cSPaul Mackerras allnodes = np; 14289b6b563cSPaul Mackerras write_unlock(&devtree_lock); 14299b6b563cSPaul Mackerras } 14309b6b563cSPaul Mackerras 14319b6b563cSPaul Mackerras /* 14329b6b563cSPaul Mackerras * "Unplug" a node from the device tree. The caller must hold 14339b6b563cSPaul Mackerras * a reference to the node. The memory associated with the node 14349b6b563cSPaul Mackerras * is not freed until its refcount goes to zero. 14359b6b563cSPaul Mackerras */ 14369b6b563cSPaul Mackerras void of_detach_node(const struct device_node *np) 14379b6b563cSPaul Mackerras { 14389b6b563cSPaul Mackerras struct device_node *parent; 14399b6b563cSPaul Mackerras 14409b6b563cSPaul Mackerras write_lock(&devtree_lock); 14419b6b563cSPaul Mackerras 14429b6b563cSPaul Mackerras parent = np->parent; 14439b6b563cSPaul Mackerras 14449b6b563cSPaul Mackerras if (allnodes == np) 14459b6b563cSPaul Mackerras allnodes = np->allnext; 14469b6b563cSPaul Mackerras else { 14479b6b563cSPaul Mackerras struct device_node *prev; 14489b6b563cSPaul Mackerras for (prev = allnodes; 14499b6b563cSPaul Mackerras prev->allnext != np; 14509b6b563cSPaul Mackerras prev = prev->allnext) 14519b6b563cSPaul Mackerras ; 14529b6b563cSPaul Mackerras prev->allnext = np->allnext; 14539b6b563cSPaul Mackerras } 14549b6b563cSPaul Mackerras 14559b6b563cSPaul Mackerras if (parent->child == np) 14569b6b563cSPaul Mackerras parent->child = np->sibling; 14579b6b563cSPaul Mackerras else { 14589b6b563cSPaul Mackerras struct device_node *prevsib; 14599b6b563cSPaul Mackerras for (prevsib = np->parent->child; 14609b6b563cSPaul Mackerras prevsib->sibling != np; 14619b6b563cSPaul Mackerras prevsib = prevsib->sibling) 14629b6b563cSPaul Mackerras ; 14639b6b563cSPaul Mackerras prevsib->sibling = np->sibling; 14649b6b563cSPaul Mackerras } 14659b6b563cSPaul Mackerras 14669b6b563cSPaul Mackerras write_unlock(&devtree_lock); 14679b6b563cSPaul Mackerras } 14689b6b563cSPaul Mackerras 14699b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 14709b6b563cSPaul Mackerras /* 14719b6b563cSPaul Mackerras * Fix up the uninitialized fields in a new device node: 14720ebfff14SBenjamin Herrenschmidt * name, type and pci-specific fields 14739b6b563cSPaul Mackerras */ 14749b6b563cSPaul Mackerras 1475cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node) 14769b6b563cSPaul Mackerras { 14779b6b563cSPaul Mackerras struct device_node *parent = of_get_parent(node); 14789b6b563cSPaul Mackerras int err = 0; 1479a7f67bdfSJeremy Kerr const phandle *ibm_phandle; 14809b6b563cSPaul Mackerras 14819b6b563cSPaul Mackerras node->name = get_property(node, "name", NULL); 14829b6b563cSPaul Mackerras node->type = get_property(node, "device_type", NULL); 14839b6b563cSPaul Mackerras 14849b6b563cSPaul Mackerras if (!parent) { 14859b6b563cSPaul Mackerras err = -ENODEV; 14869b6b563cSPaul Mackerras goto out; 14879b6b563cSPaul Mackerras } 14889b6b563cSPaul Mackerras 14899b6b563cSPaul Mackerras /* We don't support that function on PowerMac, at least 14909b6b563cSPaul Mackerras * not yet 14919b6b563cSPaul Mackerras */ 1492e8222502SBenjamin Herrenschmidt if (machine_is(powermac)) 14939b6b563cSPaul Mackerras return -ENODEV; 14949b6b563cSPaul Mackerras 14959b6b563cSPaul Mackerras /* fix up new node's linux_phandle field */ 1496a7f67bdfSJeremy Kerr if ((ibm_phandle = get_property(node, "ibm,phandle", NULL))) 14979b6b563cSPaul Mackerras node->linux_phandle = *ibm_phandle; 14989b6b563cSPaul Mackerras 14999b6b563cSPaul Mackerras out: 15009b6b563cSPaul Mackerras of_node_put(parent); 15019b6b563cSPaul Mackerras return err; 15029b6b563cSPaul Mackerras } 15039b6b563cSPaul Mackerras 15049b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb, 15059b6b563cSPaul Mackerras unsigned long action, void *node) 15069b6b563cSPaul Mackerras { 15079b6b563cSPaul Mackerras int err; 15089b6b563cSPaul Mackerras 15099b6b563cSPaul Mackerras switch (action) { 15109b6b563cSPaul Mackerras case PSERIES_RECONFIG_ADD: 1511cc5d0189SBenjamin Herrenschmidt err = of_finish_dynamic_node(node); 15129b6b563cSPaul Mackerras if (err < 0) { 15139b6b563cSPaul Mackerras printk(KERN_ERR "finish_node returned %d\n", err); 15149b6b563cSPaul Mackerras err = NOTIFY_BAD; 15159b6b563cSPaul Mackerras } 15169b6b563cSPaul Mackerras break; 15179b6b563cSPaul Mackerras default: 15189b6b563cSPaul Mackerras err = NOTIFY_DONE; 15199b6b563cSPaul Mackerras break; 15209b6b563cSPaul Mackerras } 15219b6b563cSPaul Mackerras return err; 15229b6b563cSPaul Mackerras } 15239b6b563cSPaul Mackerras 15249b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = { 15259b6b563cSPaul Mackerras .notifier_call = prom_reconfig_notifier, 15269b6b563cSPaul Mackerras .priority = 10, /* This one needs to run first */ 15279b6b563cSPaul Mackerras }; 15289b6b563cSPaul Mackerras 15299b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void) 15309b6b563cSPaul Mackerras { 15319b6b563cSPaul Mackerras return pSeries_reconfig_notifier_register(&prom_reconfig_nb); 15329b6b563cSPaul Mackerras } 15339b6b563cSPaul Mackerras __initcall(prom_reconfig_setup); 15349b6b563cSPaul Mackerras #endif 15359b6b563cSPaul Mackerras 1536e2100efbSBenjamin Herrenschmidt struct property *of_find_property(const struct device_node *np, 1537e2100efbSBenjamin Herrenschmidt const char *name, 15389b6b563cSPaul Mackerras int *lenp) 15399b6b563cSPaul Mackerras { 15409b6b563cSPaul Mackerras struct property *pp; 15419b6b563cSPaul Mackerras 1542088186deSDave C Boutcher read_lock(&devtree_lock); 15439b6b563cSPaul Mackerras for (pp = np->properties; pp != 0; pp = pp->next) 15449b6b563cSPaul Mackerras if (strcmp(pp->name, name) == 0) { 15459b6b563cSPaul Mackerras if (lenp != 0) 15469b6b563cSPaul Mackerras *lenp = pp->length; 1547088186deSDave C Boutcher break; 15489b6b563cSPaul Mackerras } 1549088186deSDave C Boutcher read_unlock(&devtree_lock); 1550088186deSDave C Boutcher 1551ecaa8b0fSDave C Boutcher return pp; 1552ecaa8b0fSDave C Boutcher } 1553ecaa8b0fSDave C Boutcher 1554ecaa8b0fSDave C Boutcher /* 1555ecaa8b0fSDave C Boutcher * Find a property with a given name for a given node 1556ecaa8b0fSDave C Boutcher * and return the value. 1557ecaa8b0fSDave C Boutcher */ 1558e2100efbSBenjamin Herrenschmidt const void *get_property(const struct device_node *np, const char *name, 1559e2100efbSBenjamin Herrenschmidt int *lenp) 1560ecaa8b0fSDave C Boutcher { 1561ecaa8b0fSDave C Boutcher struct property *pp = of_find_property(np,name,lenp); 1562088186deSDave C Boutcher return pp ? pp->value : NULL; 15639b6b563cSPaul Mackerras } 15649b6b563cSPaul Mackerras EXPORT_SYMBOL(get_property); 15659b6b563cSPaul Mackerras 15669b6b563cSPaul Mackerras /* 15679b6b563cSPaul Mackerras * Add a property to a node 15689b6b563cSPaul Mackerras */ 1569183d0202SBenjamin Herrenschmidt int prom_add_property(struct device_node* np, struct property* prop) 15709b6b563cSPaul Mackerras { 1571183d0202SBenjamin Herrenschmidt struct property **next; 15729b6b563cSPaul Mackerras 15739b6b563cSPaul Mackerras prop->next = NULL; 1574183d0202SBenjamin Herrenschmidt write_lock(&devtree_lock); 1575183d0202SBenjamin Herrenschmidt next = &np->properties; 1576183d0202SBenjamin Herrenschmidt while (*next) { 1577183d0202SBenjamin Herrenschmidt if (strcmp(prop->name, (*next)->name) == 0) { 1578183d0202SBenjamin Herrenschmidt /* duplicate ! don't insert it */ 1579183d0202SBenjamin Herrenschmidt write_unlock(&devtree_lock); 1580183d0202SBenjamin Herrenschmidt return -1; 1581183d0202SBenjamin Herrenschmidt } 15829b6b563cSPaul Mackerras next = &(*next)->next; 1583183d0202SBenjamin Herrenschmidt } 15849b6b563cSPaul Mackerras *next = prop; 1585183d0202SBenjamin Herrenschmidt write_unlock(&devtree_lock); 1586183d0202SBenjamin Herrenschmidt 1587799d6046SPaul Mackerras #ifdef CONFIG_PROC_DEVICETREE 1588183d0202SBenjamin Herrenschmidt /* try to add to proc as well if it was initialized */ 1589183d0202SBenjamin Herrenschmidt if (np->pde) 1590183d0202SBenjamin Herrenschmidt proc_device_tree_add_prop(np->pde, prop); 1591799d6046SPaul Mackerras #endif /* CONFIG_PROC_DEVICETREE */ 1592183d0202SBenjamin Herrenschmidt 1593183d0202SBenjamin Herrenschmidt return 0; 15949b6b563cSPaul Mackerras } 15959b6b563cSPaul Mackerras 1596088186deSDave C Boutcher /* 1597088186deSDave C Boutcher * Remove a property from a node. Note that we don't actually 1598088186deSDave C Boutcher * remove it, since we have given out who-knows-how-many pointers 1599088186deSDave C Boutcher * to the data using get-property. Instead we just move the property 1600088186deSDave C Boutcher * to the "dead properties" list, so it won't be found any more. 1601088186deSDave C Boutcher */ 1602088186deSDave C Boutcher int prom_remove_property(struct device_node *np, struct property *prop) 1603088186deSDave C Boutcher { 1604088186deSDave C Boutcher struct property **next; 1605088186deSDave C Boutcher int found = 0; 16069b6b563cSPaul Mackerras 1607088186deSDave C Boutcher write_lock(&devtree_lock); 1608088186deSDave C Boutcher next = &np->properties; 1609088186deSDave C Boutcher while (*next) { 1610088186deSDave C Boutcher if (*next == prop) { 1611088186deSDave C Boutcher /* found the node */ 1612088186deSDave C Boutcher *next = prop->next; 1613088186deSDave C Boutcher prop->next = np->deadprops; 1614088186deSDave C Boutcher np->deadprops = prop; 1615088186deSDave C Boutcher found = 1; 1616088186deSDave C Boutcher break; 1617088186deSDave C Boutcher } 1618088186deSDave C Boutcher next = &(*next)->next; 1619088186deSDave C Boutcher } 1620088186deSDave C Boutcher write_unlock(&devtree_lock); 1621088186deSDave C Boutcher 1622088186deSDave C Boutcher if (!found) 1623088186deSDave C Boutcher return -ENODEV; 1624088186deSDave C Boutcher 1625088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE 1626088186deSDave C Boutcher /* try to remove the proc node as well */ 1627088186deSDave C Boutcher if (np->pde) 1628088186deSDave C Boutcher proc_device_tree_remove_prop(np->pde, prop); 1629088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */ 1630088186deSDave C Boutcher 1631088186deSDave C Boutcher return 0; 1632088186deSDave C Boutcher } 1633088186deSDave C Boutcher 1634088186deSDave C Boutcher /* 1635088186deSDave C Boutcher * Update a property in a node. Note that we don't actually 1636088186deSDave C Boutcher * remove it, since we have given out who-knows-how-many pointers 1637088186deSDave C Boutcher * to the data using get-property. Instead we just move the property 1638088186deSDave C Boutcher * to the "dead properties" list, and add the new property to the 1639088186deSDave C Boutcher * property list 1640088186deSDave C Boutcher */ 1641088186deSDave C Boutcher int prom_update_property(struct device_node *np, 1642088186deSDave C Boutcher struct property *newprop, 1643088186deSDave C Boutcher struct property *oldprop) 1644088186deSDave C Boutcher { 1645088186deSDave C Boutcher struct property **next; 1646088186deSDave C Boutcher int found = 0; 1647088186deSDave C Boutcher 1648088186deSDave C Boutcher write_lock(&devtree_lock); 1649088186deSDave C Boutcher next = &np->properties; 1650088186deSDave C Boutcher while (*next) { 1651088186deSDave C Boutcher if (*next == oldprop) { 1652088186deSDave C Boutcher /* found the node */ 1653088186deSDave C Boutcher newprop->next = oldprop->next; 1654088186deSDave C Boutcher *next = newprop; 1655088186deSDave C Boutcher oldprop->next = np->deadprops; 1656088186deSDave C Boutcher np->deadprops = oldprop; 1657088186deSDave C Boutcher found = 1; 1658088186deSDave C Boutcher break; 1659088186deSDave C Boutcher } 1660088186deSDave C Boutcher next = &(*next)->next; 1661088186deSDave C Boutcher } 1662088186deSDave C Boutcher write_unlock(&devtree_lock); 1663088186deSDave C Boutcher 1664088186deSDave C Boutcher if (!found) 1665088186deSDave C Boutcher return -ENODEV; 1666088186deSDave C Boutcher 1667088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE 1668088186deSDave C Boutcher /* try to add to proc as well if it was initialized */ 1669088186deSDave C Boutcher if (np->pde) 1670088186deSDave C Boutcher proc_device_tree_update_prop(np->pde, newprop, oldprop); 1671088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */ 1672088186deSDave C Boutcher 1673088186deSDave C Boutcher return 0; 1674088186deSDave C Boutcher } 1675b68239eeSMichael Ellerman 1676acf7d768SBenjamin Herrenschmidt 1677acf7d768SBenjamin Herrenschmidt /* Find the device node for a given logical cpu number, also returns the cpu 1678acf7d768SBenjamin Herrenschmidt * local thread number (index in ibm,interrupt-server#s) if relevant and 1679acf7d768SBenjamin Herrenschmidt * asked for (non NULL) 1680acf7d768SBenjamin Herrenschmidt */ 1681acf7d768SBenjamin Herrenschmidt struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) 1682acf7d768SBenjamin Herrenschmidt { 1683acf7d768SBenjamin Herrenschmidt int hardid; 1684acf7d768SBenjamin Herrenschmidt struct device_node *np; 1685acf7d768SBenjamin Herrenschmidt 1686acf7d768SBenjamin Herrenschmidt hardid = get_hard_smp_processor_id(cpu); 1687acf7d768SBenjamin Herrenschmidt 1688acf7d768SBenjamin Herrenschmidt for_each_node_by_type(np, "cpu") { 1689a7f67bdfSJeremy Kerr const u32 *intserv; 1690acf7d768SBenjamin Herrenschmidt unsigned int plen, t; 1691acf7d768SBenjamin Herrenschmidt 1692acf7d768SBenjamin Herrenschmidt /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist 1693acf7d768SBenjamin Herrenschmidt * fallback to "reg" property and assume no threads 1694acf7d768SBenjamin Herrenschmidt */ 1695a7f67bdfSJeremy Kerr intserv = get_property(np, "ibm,ppc-interrupt-server#s", 1696acf7d768SBenjamin Herrenschmidt &plen); 1697acf7d768SBenjamin Herrenschmidt if (intserv == NULL) { 1698a7f67bdfSJeremy Kerr const u32 *reg = get_property(np, "reg", NULL); 1699acf7d768SBenjamin Herrenschmidt if (reg == NULL) 1700acf7d768SBenjamin Herrenschmidt continue; 1701acf7d768SBenjamin Herrenschmidt if (*reg == hardid) { 1702acf7d768SBenjamin Herrenschmidt if (thread) 1703acf7d768SBenjamin Herrenschmidt *thread = 0; 1704acf7d768SBenjamin Herrenschmidt return np; 1705acf7d768SBenjamin Herrenschmidt } 1706acf7d768SBenjamin Herrenschmidt } else { 1707acf7d768SBenjamin Herrenschmidt plen /= sizeof(u32); 1708acf7d768SBenjamin Herrenschmidt for (t = 0; t < plen; t++) { 1709acf7d768SBenjamin Herrenschmidt if (hardid == intserv[t]) { 1710acf7d768SBenjamin Herrenschmidt if (thread) 1711acf7d768SBenjamin Herrenschmidt *thread = t; 1712acf7d768SBenjamin Herrenschmidt return np; 1713acf7d768SBenjamin Herrenschmidt } 1714acf7d768SBenjamin Herrenschmidt } 1715acf7d768SBenjamin Herrenschmidt } 1716acf7d768SBenjamin Herrenschmidt } 1717acf7d768SBenjamin Herrenschmidt return NULL; 1718acf7d768SBenjamin Herrenschmidt } 171936ca4ba4SChristian Krafft EXPORT_SYMBOL(of_get_cpu_node); 17207a4571aeSMichael Ellerman 17217a4571aeSMichael Ellerman #ifdef DEBUG 17227a4571aeSMichael Ellerman static struct debugfs_blob_wrapper flat_dt_blob; 17237a4571aeSMichael Ellerman 17247a4571aeSMichael Ellerman static int __init export_flat_device_tree(void) 17257a4571aeSMichael Ellerman { 17267a4571aeSMichael Ellerman struct dentry *d; 17277a4571aeSMichael Ellerman 17287a4571aeSMichael Ellerman d = debugfs_create_dir("powerpc", NULL); 17297a4571aeSMichael Ellerman if (!d) 17307a4571aeSMichael Ellerman return 1; 17317a4571aeSMichael Ellerman 17327a4571aeSMichael Ellerman flat_dt_blob.data = initial_boot_params; 17337a4571aeSMichael Ellerman flat_dt_blob.size = initial_boot_params->totalsize; 17347a4571aeSMichael Ellerman 17357a4571aeSMichael Ellerman d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR, 17367a4571aeSMichael Ellerman d, &flat_dt_blob); 17377a4571aeSMichael Ellerman if (!d) 17387a4571aeSMichael Ellerman return 1; 17397a4571aeSMichael Ellerman 17407a4571aeSMichael Ellerman return 0; 17417a4571aeSMichael Ellerman } 17427a4571aeSMichael Ellerman __initcall(export_flat_device_tree); 17437a4571aeSMichael Ellerman #endif 1744