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 541974a76f5SPaul Mackerras static void __init scan_features(unsigned long node, unsigned char *ftrs, 542974a76f5SPaul Mackerras unsigned long tablelen, 543974a76f5SPaul Mackerras struct ibm_pa_feature *fp, 544974a76f5SPaul Mackerras unsigned long ft_size) 545d205819eSPaul Mackerras { 546974a76f5SPaul 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; 552974a76f5SPaul Mackerras len = 2 + ftrs[0]; 553d205819eSPaul Mackerras if (tablelen < len) 554d205819eSPaul Mackerras return; /* descriptor 0 not found */ 555974a76f5SPaul Mackerras if (ftrs[1] == 0) 556d205819eSPaul Mackerras break; 557d205819eSPaul Mackerras tablelen -= len; 558974a76f5SPaul Mackerras ftrs += len; 559d205819eSPaul Mackerras } 560d205819eSPaul Mackerras 561d205819eSPaul Mackerras /* loop over bits we know about */ 562974a76f5SPaul Mackerras for (i = 0; i < ft_size; ++i, ++fp) { 563974a76f5SPaul Mackerras if (fp->pabyte >= ftrs[0]) 564d205819eSPaul Mackerras continue; 565974a76f5SPaul 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 576974a76f5SPaul Mackerras static void __init check_cpu_pa_features(unsigned long node) 577974a76f5SPaul Mackerras { 578974a76f5SPaul Mackerras unsigned char *pa_ftrs; 579974a76f5SPaul Mackerras unsigned long tablelen; 580974a76f5SPaul Mackerras 581974a76f5SPaul Mackerras pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen); 582974a76f5SPaul Mackerras if (pa_ftrs == NULL) 583974a76f5SPaul Mackerras return; 584974a76f5SPaul Mackerras 585974a76f5SPaul Mackerras scan_features(node, pa_ftrs, tablelen, 586974a76f5SPaul Mackerras ibm_pa_features, ARRAY_SIZE(ibm_pa_features)); 587974a76f5SPaul Mackerras } 588974a76f5SPaul Mackerras 589974a76f5SPaul Mackerras static struct feature_property { 590974a76f5SPaul Mackerras const char *name; 591974a76f5SPaul Mackerras u32 min_value; 592974a76f5SPaul Mackerras unsigned long cpu_feature; 593974a76f5SPaul Mackerras unsigned long cpu_user_ftr; 594974a76f5SPaul Mackerras } feature_properties[] __initdata = { 595974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC 596974a76f5SPaul Mackerras {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC}, 597974a76f5SPaul Mackerras {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC}, 598974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */ 599974a76f5SPaul Mackerras #ifdef CONFIG_PPC64 600974a76f5SPaul Mackerras {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP}, 601974a76f5SPaul Mackerras {"ibm,purr", 1, CPU_FTR_PURR, 0}, 602974a76f5SPaul Mackerras {"ibm,spurr", 1, CPU_FTR_SPURR, 0}, 603974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */ 604974a76f5SPaul Mackerras }; 605974a76f5SPaul Mackerras 606974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node) 607974a76f5SPaul Mackerras { 608974a76f5SPaul Mackerras unsigned long i; 609974a76f5SPaul Mackerras struct feature_property *fp = feature_properties; 610974a76f5SPaul Mackerras const u32 *prop; 611974a76f5SPaul Mackerras 612974a76f5SPaul Mackerras for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) { 613974a76f5SPaul Mackerras prop = of_get_flat_dt_prop(node, fp->name, NULL); 614974a76f5SPaul Mackerras if (prop && *prop >= fp->min_value) { 615974a76f5SPaul Mackerras cur_cpu_spec->cpu_features |= fp->cpu_feature; 616974a76f5SPaul Mackerras cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr; 617974a76f5SPaul Mackerras } 618974a76f5SPaul Mackerras } 619974a76f5SPaul Mackerras } 620974a76f5SPaul 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); 627974a76f5SPaul Mackerras const u32 *prop; 628974a76f5SPaul 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]); 685974a76f5SPaul Mackerras 686974a76f5SPaul Mackerras /* 687974a76f5SPaul Mackerras * PAPR defines "logical" PVR values for cpus that 688974a76f5SPaul Mackerras * meet various levels of the architecture: 689974a76f5SPaul Mackerras * 0x0f000001 Architecture version 2.04 690974a76f5SPaul Mackerras * 0x0f000002 Architecture version 2.05 691974a76f5SPaul Mackerras * If the cpu-version property in the cpu node contains 692974a76f5SPaul Mackerras * such a value, we call identify_cpu again with the 693974a76f5SPaul Mackerras * logical PVR value in order to use the cpu feature 694974a76f5SPaul Mackerras * bits appropriate for the architecture level. 695974a76f5SPaul Mackerras * 696974a76f5SPaul Mackerras * A POWER6 partition in "POWER6 architected" mode 697974a76f5SPaul Mackerras * uses the 0x0f000002 PVR value; in POWER5+ mode 698974a76f5SPaul Mackerras * it uses 0x0f000001. 699974a76f5SPaul Mackerras */ 700974a76f5SPaul Mackerras prop = of_get_flat_dt_prop(node, "cpu-version", NULL); 701974a76f5SPaul Mackerras if (prop && (*prop & 0xff000000) == 0x0f000000) 702974a76f5SPaul Mackerras identify_cpu(0, *prop); 7034df20460SAnton Blanchard } 7049b6b563cSPaul Mackerras 705974a76f5SPaul 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; 722*30437b3eSDavid Gibson u32 *prop; 723329dda08SKumar Gala unsigned long l; 724329dda08SKumar Gala char *p; 7259b6b563cSPaul Mackerras 7269b6b563cSPaul Mackerras DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname); 7279b6b563cSPaul Mackerras 728a575b807SPaul Mackerras if (depth != 1 || 729a575b807SPaul Mackerras (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0)) 7309b6b563cSPaul Mackerras return 0; 7319b6b563cSPaul Mackerras 7329b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 7339b6b563cSPaul Mackerras /* check if iommu is forced on or off */ 7343c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL) 7359b6b563cSPaul Mackerras iommu_is_off = 1; 7363c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL) 7379b6b563cSPaul Mackerras iommu_force_on = 1; 7389b6b563cSPaul Mackerras #endif 7399b6b563cSPaul Mackerras 7402babf5c2SMichael Ellerman /* mem=x on the command line is the preferred mechanism */ 7413c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL); 7429b6b563cSPaul Mackerras if (lprop) 7439b6b563cSPaul Mackerras memory_limit = *lprop; 7449b6b563cSPaul Mackerras 7459b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 7463c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL); 7479b6b563cSPaul Mackerras if (lprop) 7489b6b563cSPaul Mackerras tce_alloc_start = *lprop; 7493c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL); 7509b6b563cSPaul Mackerras if (lprop) 7519b6b563cSPaul Mackerras tce_alloc_end = *lprop; 7529b6b563cSPaul Mackerras #endif 7539b6b563cSPaul Mackerras 754dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC 755dcee3036SMichael Ellerman lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL); 756dcee3036SMichael Ellerman if (lprop) 757dcee3036SMichael Ellerman crashk_res.start = *lprop; 758dcee3036SMichael Ellerman 759dcee3036SMichael Ellerman lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL); 760dcee3036SMichael Ellerman if (lprop) 761dcee3036SMichael Ellerman crashk_res.end = crashk_res.start + *lprop - 1; 762dcee3036SMichael Ellerman #endif 763dcee3036SMichael Ellerman 764*30437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD 765*30437b3eSDavid Gibson DBG("Looking for initrd properties... "); 766*30437b3eSDavid Gibson prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l); 767*30437b3eSDavid Gibson if (prop) { 768*30437b3eSDavid Gibson initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4)); 769*30437b3eSDavid Gibson prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l); 770*30437b3eSDavid Gibson if (prop) { 771*30437b3eSDavid Gibson initrd_end = (unsigned long)__va(of_read_ulong(prop, l/4)); 772*30437b3eSDavid Gibson initrd_below_start_ok = 1; 773*30437b3eSDavid Gibson } else { 774*30437b3eSDavid Gibson initrd_start = 0; 775*30437b3eSDavid Gibson } 776*30437b3eSDavid Gibson } 777*30437b3eSDavid Gibson DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end); 778*30437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */ 779*30437b3eSDavid Gibson 780329dda08SKumar Gala /* Retreive command line */ 781329dda08SKumar Gala p = of_get_flat_dt_prop(node, "bootargs", &l); 782329dda08SKumar Gala if (p != NULL && l > 0) 783329dda08SKumar Gala strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE)); 784329dda08SKumar Gala 785329dda08SKumar Gala #ifdef CONFIG_CMDLINE 786c1ce464dSGeoff Levand if (p == NULL || l == 0 || (l == 1 && (*p) == 0)) 787329dda08SKumar Gala strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE); 788329dda08SKumar Gala #endif /* CONFIG_CMDLINE */ 789329dda08SKumar Gala 790329dda08SKumar Gala DBG("Command line is: %s\n", cmd_line); 791329dda08SKumar Gala 7929b6b563cSPaul Mackerras /* break now */ 7939b6b563cSPaul Mackerras return 1; 7949b6b563cSPaul Mackerras } 7959b6b563cSPaul Mackerras 7969b6b563cSPaul Mackerras static int __init early_init_dt_scan_root(unsigned long node, 7979b6b563cSPaul Mackerras const char *uname, int depth, void *data) 7989b6b563cSPaul Mackerras { 7999b6b563cSPaul Mackerras u32 *prop; 8009b6b563cSPaul Mackerras 8019b6b563cSPaul Mackerras if (depth != 0) 8029b6b563cSPaul Mackerras return 0; 8039b6b563cSPaul Mackerras 8043c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "#size-cells", NULL); 8059b6b563cSPaul Mackerras dt_root_size_cells = (prop == NULL) ? 1 : *prop; 8069b6b563cSPaul Mackerras DBG("dt_root_size_cells = %x\n", dt_root_size_cells); 8079b6b563cSPaul Mackerras 8083c726f8dSBenjamin Herrenschmidt prop = of_get_flat_dt_prop(node, "#address-cells", NULL); 8099b6b563cSPaul Mackerras dt_root_addr_cells = (prop == NULL) ? 2 : *prop; 8109b6b563cSPaul Mackerras DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells); 8119b6b563cSPaul Mackerras 8129b6b563cSPaul Mackerras /* break now */ 8139b6b563cSPaul Mackerras return 1; 8149b6b563cSPaul Mackerras } 8159b6b563cSPaul Mackerras 8169b6b563cSPaul Mackerras static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp) 8179b6b563cSPaul Mackerras { 8189b6b563cSPaul Mackerras cell_t *p = *cellp; 8199b6b563cSPaul Mackerras 820a4dc7ff0SPaul Mackerras *cellp = p + s; 821a4dc7ff0SPaul Mackerras return of_read_ulong(p, s); 8229b6b563cSPaul Mackerras } 8239b6b563cSPaul Mackerras 8240204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 8250204568aSPaul Mackerras /* 8260204568aSPaul Mackerras * Interpret the ibm,dynamic-memory property in the 8270204568aSPaul Mackerras * /ibm,dynamic-reconfiguration-memory node. 8280204568aSPaul Mackerras * This contains a list of memory blocks along with NUMA affinity 8290204568aSPaul Mackerras * information. 8300204568aSPaul Mackerras */ 8310204568aSPaul Mackerras static int __init early_init_dt_scan_drconf_memory(unsigned long node) 8320204568aSPaul Mackerras { 8330204568aSPaul Mackerras cell_t *dm, *ls; 8340204568aSPaul Mackerras unsigned long l, n; 8350204568aSPaul Mackerras unsigned long base, size, lmb_size, flags; 8360204568aSPaul Mackerras 8370204568aSPaul Mackerras ls = (cell_t *)of_get_flat_dt_prop(node, "ibm,lmb-size", &l); 8380204568aSPaul Mackerras if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t)) 8390204568aSPaul Mackerras return 0; 8400204568aSPaul Mackerras lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls); 8410204568aSPaul Mackerras 8420204568aSPaul Mackerras dm = (cell_t *)of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l); 8430204568aSPaul Mackerras if (dm == NULL || l < sizeof(cell_t)) 8440204568aSPaul Mackerras return 0; 8450204568aSPaul Mackerras 8460204568aSPaul Mackerras n = *dm++; /* number of entries */ 8470204568aSPaul Mackerras if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t)) 8480204568aSPaul Mackerras return 0; 8490204568aSPaul Mackerras 8500204568aSPaul Mackerras for (; n != 0; --n) { 8510204568aSPaul Mackerras base = dt_mem_next_cell(dt_root_addr_cells, &dm); 8520204568aSPaul Mackerras flags = dm[3]; 8530204568aSPaul Mackerras /* skip DRC index, pad, assoc. list index, flags */ 8540204568aSPaul Mackerras dm += 4; 8550204568aSPaul Mackerras /* skip this block if the reserved bit is set in flags (0x80) 8560204568aSPaul Mackerras or if the block is not assigned to this partition (0x8) */ 8570204568aSPaul Mackerras if ((flags & 0x80) || !(flags & 0x8)) 8580204568aSPaul Mackerras continue; 8590204568aSPaul Mackerras size = lmb_size; 8600204568aSPaul Mackerras if (iommu_is_off) { 8610204568aSPaul Mackerras if (base >= 0x80000000ul) 8620204568aSPaul Mackerras continue; 8630204568aSPaul Mackerras if ((base + size) > 0x80000000ul) 8640204568aSPaul Mackerras size = 0x80000000ul - base; 8650204568aSPaul Mackerras } 8660204568aSPaul Mackerras lmb_add(base, size); 8670204568aSPaul Mackerras } 8680204568aSPaul Mackerras lmb_dump_all(); 8690204568aSPaul Mackerras return 0; 8700204568aSPaul Mackerras } 8710204568aSPaul Mackerras #else 8720204568aSPaul Mackerras #define early_init_dt_scan_drconf_memory(node) 0 8730204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */ 8749b6b563cSPaul Mackerras 8759b6b563cSPaul Mackerras static int __init early_init_dt_scan_memory(unsigned long node, 8769b6b563cSPaul Mackerras const char *uname, int depth, void *data) 8779b6b563cSPaul Mackerras { 8783c726f8dSBenjamin Herrenschmidt char *type = of_get_flat_dt_prop(node, "device_type", NULL); 8799b6b563cSPaul Mackerras cell_t *reg, *endp; 8809b6b563cSPaul Mackerras unsigned long l; 8819b6b563cSPaul Mackerras 8820204568aSPaul Mackerras /* Look for the ibm,dynamic-reconfiguration-memory node */ 8830204568aSPaul Mackerras if (depth == 1 && 8840204568aSPaul Mackerras strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0) 8850204568aSPaul Mackerras return early_init_dt_scan_drconf_memory(node); 8860204568aSPaul Mackerras 8879b6b563cSPaul Mackerras /* We are scanning "memory" nodes only */ 888a23414beSPaul Mackerras if (type == NULL) { 889a23414beSPaul Mackerras /* 890a23414beSPaul Mackerras * The longtrail doesn't have a device_type on the 891a23414beSPaul Mackerras * /memory node, so look for the node called /memory@0. 892a23414beSPaul Mackerras */ 893a23414beSPaul Mackerras if (depth != 1 || strcmp(uname, "memory@0") != 0) 894a23414beSPaul Mackerras return 0; 895a23414beSPaul Mackerras } else if (strcmp(type, "memory") != 0) 8969b6b563cSPaul Mackerras return 0; 8979b6b563cSPaul Mackerras 898ba759485SMichael Ellerman reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l); 899ba759485SMichael Ellerman if (reg == NULL) 9003c726f8dSBenjamin Herrenschmidt reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l); 9019b6b563cSPaul Mackerras if (reg == NULL) 9029b6b563cSPaul Mackerras return 0; 9039b6b563cSPaul Mackerras 9049b6b563cSPaul Mackerras endp = reg + (l / sizeof(cell_t)); 9059b6b563cSPaul Mackerras 906358c86fdSMichael Ellerman DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n", 9079b6b563cSPaul Mackerras uname, l, reg[0], reg[1], reg[2], reg[3]); 9089b6b563cSPaul Mackerras 9099b6b563cSPaul Mackerras while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) { 9109b6b563cSPaul Mackerras unsigned long base, size; 9119b6b563cSPaul Mackerras 9129b6b563cSPaul Mackerras base = dt_mem_next_cell(dt_root_addr_cells, ®); 9139b6b563cSPaul Mackerras size = dt_mem_next_cell(dt_root_size_cells, ®); 9149b6b563cSPaul Mackerras 9159b6b563cSPaul Mackerras if (size == 0) 9169b6b563cSPaul Mackerras continue; 9179b6b563cSPaul Mackerras DBG(" - %lx , %lx\n", base, size); 9189b6b563cSPaul Mackerras #ifdef CONFIG_PPC64 9199b6b563cSPaul Mackerras if (iommu_is_off) { 9209b6b563cSPaul Mackerras if (base >= 0x80000000ul) 9219b6b563cSPaul Mackerras continue; 9229b6b563cSPaul Mackerras if ((base + size) > 0x80000000ul) 9239b6b563cSPaul Mackerras size = 0x80000000ul - base; 9249b6b563cSPaul Mackerras } 9259b6b563cSPaul Mackerras #endif 9269b6b563cSPaul Mackerras lmb_add(base, size); 9279b6b563cSPaul Mackerras } 9289b6b563cSPaul Mackerras return 0; 9299b6b563cSPaul Mackerras } 9309b6b563cSPaul Mackerras 9319b6b563cSPaul Mackerras static void __init early_reserve_mem(void) 9329b6b563cSPaul Mackerras { 933cbbcf340SKumar Gala u64 base, size; 934cbbcf340SKumar Gala u64 *reserve_map; 9358a300887SJon Loeliger unsigned long self_base; 9368a300887SJon Loeliger unsigned long self_size; 9379b6b563cSPaul Mackerras 938cbbcf340SKumar Gala reserve_map = (u64 *)(((unsigned long)initial_boot_params) + 9399b6b563cSPaul Mackerras initial_boot_params->off_mem_rsvmap); 9404d1f3f25SJimi Xenidis 9414d1f3f25SJimi Xenidis /* before we do anything, lets reserve the dt blob */ 9428a300887SJon Loeliger self_base = __pa((unsigned long)initial_boot_params); 9438a300887SJon Loeliger self_size = initial_boot_params->totalsize; 9448a300887SJon Loeliger lmb_reserve(self_base, self_size); 9454d1f3f25SJimi Xenidis 946*30437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD 947*30437b3eSDavid Gibson /* then reserve the initrd, if any */ 948*30437b3eSDavid Gibson if (initrd_start && (initrd_end > initrd_start)) 949*30437b3eSDavid Gibson lmb_reserve(__pa(initrd_start), initrd_end - initrd_start); 950*30437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */ 951*30437b3eSDavid Gibson 952cbbcf340SKumar Gala #ifdef CONFIG_PPC32 953cbbcf340SKumar Gala /* 954cbbcf340SKumar Gala * Handle the case where we might be booting from an old kexec 955cbbcf340SKumar Gala * image that setup the mem_rsvmap as pairs of 32-bit values 956cbbcf340SKumar Gala */ 957cbbcf340SKumar Gala if (*reserve_map > 0xffffffffull) { 958cbbcf340SKumar Gala u32 base_32, size_32; 959cbbcf340SKumar Gala u32 *reserve_map_32 = (u32 *)reserve_map; 960cbbcf340SKumar Gala 961cbbcf340SKumar Gala while (1) { 962cbbcf340SKumar Gala base_32 = *(reserve_map_32++); 963cbbcf340SKumar Gala size_32 = *(reserve_map_32++); 964cbbcf340SKumar Gala if (size_32 == 0) 965cbbcf340SKumar Gala break; 9668a300887SJon Loeliger /* skip if the reservation is for the blob */ 9678a300887SJon Loeliger if (base_32 == self_base && size_32 == self_size) 9688a300887SJon Loeliger continue; 969329dda08SKumar Gala DBG("reserving: %x -> %x\n", base_32, size_32); 970cbbcf340SKumar Gala lmb_reserve(base_32, size_32); 971cbbcf340SKumar Gala } 972cbbcf340SKumar Gala return; 973cbbcf340SKumar Gala } 974cbbcf340SKumar Gala #endif 9759b6b563cSPaul Mackerras while (1) { 9769b6b563cSPaul Mackerras base = *(reserve_map++); 9779b6b563cSPaul Mackerras size = *(reserve_map++); 9789b6b563cSPaul Mackerras if (size == 0) 9799b6b563cSPaul Mackerras break; 980cbbcf340SKumar Gala DBG("reserving: %llx -> %llx\n", base, size); 9819b6b563cSPaul Mackerras lmb_reserve(base, size); 9829b6b563cSPaul Mackerras } 9839b6b563cSPaul Mackerras 9849b6b563cSPaul Mackerras #if 0 9859b6b563cSPaul Mackerras DBG("memory reserved, lmbs :\n"); 9869b6b563cSPaul Mackerras lmb_dump_all(); 9879b6b563cSPaul Mackerras #endif 9889b6b563cSPaul Mackerras } 9899b6b563cSPaul Mackerras 9909b6b563cSPaul Mackerras void __init early_init_devtree(void *params) 9919b6b563cSPaul Mackerras { 9929b6b563cSPaul Mackerras DBG(" -> early_init_devtree()\n"); 9939b6b563cSPaul Mackerras 9949b6b563cSPaul Mackerras /* Setup flat device-tree pointer */ 9959b6b563cSPaul Mackerras initial_boot_params = params; 9969b6b563cSPaul Mackerras 997458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS 998458148c0SMichael Ellerman /* Some machines might need RTAS info for debugging, grab it now. */ 999458148c0SMichael Ellerman of_scan_flat_dt(early_init_dt_scan_rtas, NULL); 1000458148c0SMichael Ellerman #endif 1001458148c0SMichael Ellerman 10029b6b563cSPaul Mackerras /* Retrieve various informations from the /chosen node of the 10039b6b563cSPaul Mackerras * device-tree, including the platform type, initrd location and 10049b6b563cSPaul Mackerras * size, TCE reserve, and more ... 10059b6b563cSPaul Mackerras */ 10063c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_chosen, NULL); 10079b6b563cSPaul Mackerras 10089b6b563cSPaul Mackerras /* Scan memory nodes and rebuild LMBs */ 10099b6b563cSPaul Mackerras lmb_init(); 10103c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_root, NULL); 10113c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_memory, NULL); 1012846f77b0SMichael Ellerman 1013846f77b0SMichael Ellerman /* Save command line for /proc/cmdline and then parse parameters */ 1014b8757b21SAlon Bar-Lev strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE); 1015846f77b0SMichael Ellerman parse_early_param(); 1016846f77b0SMichael Ellerman 10179b6b563cSPaul Mackerras /* Reserve LMB regions used by kernel, initrd, dt, etc... */ 10180cc4746cSMichael Ellerman lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START); 101947310413SMichael Ellerman reserve_kdump_trampoline(); 102035dd5432SMichael Ellerman reserve_crashkernel(); 10219b6b563cSPaul Mackerras early_reserve_mem(); 10229b6b563cSPaul Mackerras 10232babf5c2SMichael Ellerman lmb_enforce_memory_limit(memory_limit); 10242babf5c2SMichael Ellerman lmb_analyze(); 10252babf5c2SMichael Ellerman 10262babf5c2SMichael Ellerman DBG("Phys. mem: %lx\n", lmb_phys_mem_size()); 10272babf5c2SMichael Ellerman 10282babf5c2SMichael Ellerman /* We may need to relocate the flat tree, do it now. 10292babf5c2SMichael Ellerman * FIXME .. and the initrd too? */ 10302babf5c2SMichael Ellerman move_device_tree(); 10312babf5c2SMichael Ellerman 10329b6b563cSPaul Mackerras DBG("Scanning CPUs ...\n"); 10339b6b563cSPaul Mackerras 10343c726f8dSBenjamin Herrenschmidt /* Retreive CPU related informations from the flat tree 10353c726f8dSBenjamin Herrenschmidt * (altivec support, boot CPU ID, ...) 10369b6b563cSPaul Mackerras */ 10373c726f8dSBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_cpus, NULL); 10389b6b563cSPaul Mackerras 10399b6b563cSPaul Mackerras DBG(" <- early_init_devtree()\n"); 10409b6b563cSPaul Mackerras } 10419b6b563cSPaul Mackerras 10429b6b563cSPaul Mackerras #undef printk 10439b6b563cSPaul Mackerras 10449b6b563cSPaul Mackerras int 10459b6b563cSPaul Mackerras prom_n_addr_cells(struct device_node* np) 10469b6b563cSPaul Mackerras { 1047a7f67bdfSJeremy Kerr const int *ip; 10489b6b563cSPaul Mackerras do { 10499b6b563cSPaul Mackerras if (np->parent) 10509b6b563cSPaul Mackerras np = np->parent; 1051a7f67bdfSJeremy Kerr ip = get_property(np, "#address-cells", NULL); 10529b6b563cSPaul Mackerras if (ip != NULL) 10539b6b563cSPaul Mackerras return *ip; 10549b6b563cSPaul Mackerras } while (np->parent); 10559b6b563cSPaul Mackerras /* No #address-cells property for the root node, default to 1 */ 10569b6b563cSPaul Mackerras return 1; 10579b6b563cSPaul Mackerras } 10581dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_addr_cells); 10599b6b563cSPaul Mackerras 10609b6b563cSPaul Mackerras int 10619b6b563cSPaul Mackerras prom_n_size_cells(struct device_node* np) 10629b6b563cSPaul Mackerras { 1063a7f67bdfSJeremy Kerr const int* ip; 10649b6b563cSPaul Mackerras do { 10659b6b563cSPaul Mackerras if (np->parent) 10669b6b563cSPaul Mackerras np = np->parent; 1067a7f67bdfSJeremy Kerr ip = get_property(np, "#size-cells", NULL); 10689b6b563cSPaul Mackerras if (ip != NULL) 10699b6b563cSPaul Mackerras return *ip; 10709b6b563cSPaul Mackerras } while (np->parent); 10719b6b563cSPaul Mackerras /* No #size-cells property for the root node, default to 1 */ 10729b6b563cSPaul Mackerras return 1; 10739b6b563cSPaul Mackerras } 10741dfc6772SPaul Mackerras EXPORT_SYMBOL(prom_n_size_cells); 10759b6b563cSPaul Mackerras 10769b6b563cSPaul Mackerras /** 10779b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given name. 10789b6b563cSPaul Mackerras */ 10799b6b563cSPaul Mackerras struct device_node *find_devices(const char *name) 10809b6b563cSPaul Mackerras { 10819b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 10829b6b563cSPaul Mackerras 10839b6b563cSPaul Mackerras prevp = &head; 10849b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 10859b6b563cSPaul Mackerras if (np->name != 0 && strcasecmp(np->name, name) == 0) { 10869b6b563cSPaul Mackerras *prevp = np; 10879b6b563cSPaul Mackerras prevp = &np->next; 10889b6b563cSPaul Mackerras } 10899b6b563cSPaul Mackerras } 10909b6b563cSPaul Mackerras *prevp = NULL; 10919b6b563cSPaul Mackerras return head; 10929b6b563cSPaul Mackerras } 10939b6b563cSPaul Mackerras EXPORT_SYMBOL(find_devices); 10949b6b563cSPaul Mackerras 10959b6b563cSPaul Mackerras /** 10969b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type. 10979b6b563cSPaul Mackerras */ 10989b6b563cSPaul Mackerras struct device_node *find_type_devices(const char *type) 10999b6b563cSPaul Mackerras { 11009b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 11019b6b563cSPaul Mackerras 11029b6b563cSPaul Mackerras prevp = &head; 11039b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 11049b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0) { 11059b6b563cSPaul Mackerras *prevp = np; 11069b6b563cSPaul Mackerras prevp = &np->next; 11079b6b563cSPaul Mackerras } 11089b6b563cSPaul Mackerras } 11099b6b563cSPaul Mackerras *prevp = NULL; 11109b6b563cSPaul Mackerras return head; 11119b6b563cSPaul Mackerras } 11129b6b563cSPaul Mackerras EXPORT_SYMBOL(find_type_devices); 11139b6b563cSPaul Mackerras 11149b6b563cSPaul Mackerras /** 11159b6b563cSPaul Mackerras * Returns all nodes linked together 11169b6b563cSPaul Mackerras */ 11179b6b563cSPaul Mackerras struct device_node *find_all_nodes(void) 11189b6b563cSPaul Mackerras { 11199b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 11209b6b563cSPaul Mackerras 11219b6b563cSPaul Mackerras prevp = &head; 11229b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 11239b6b563cSPaul Mackerras *prevp = np; 11249b6b563cSPaul Mackerras prevp = &np->next; 11259b6b563cSPaul Mackerras } 11269b6b563cSPaul Mackerras *prevp = NULL; 11279b6b563cSPaul Mackerras return head; 11289b6b563cSPaul Mackerras } 11299b6b563cSPaul Mackerras EXPORT_SYMBOL(find_all_nodes); 11309b6b563cSPaul Mackerras 11319b6b563cSPaul Mackerras /** Checks if the given "compat" string matches one of the strings in 11329b6b563cSPaul Mackerras * the device's "compatible" property 11339b6b563cSPaul Mackerras */ 1134e2100efbSBenjamin Herrenschmidt int device_is_compatible(const struct device_node *device, const char *compat) 11359b6b563cSPaul Mackerras { 11369b6b563cSPaul Mackerras const char* cp; 11379b6b563cSPaul Mackerras int cplen, l; 11389b6b563cSPaul Mackerras 1139a7f67bdfSJeremy Kerr cp = get_property(device, "compatible", &cplen); 11409b6b563cSPaul Mackerras if (cp == NULL) 11419b6b563cSPaul Mackerras return 0; 11429b6b563cSPaul Mackerras while (cplen > 0) { 11439b6b563cSPaul Mackerras if (strncasecmp(cp, compat, strlen(compat)) == 0) 11449b6b563cSPaul Mackerras return 1; 11459b6b563cSPaul Mackerras l = strlen(cp) + 1; 11469b6b563cSPaul Mackerras cp += l; 11479b6b563cSPaul Mackerras cplen -= l; 11489b6b563cSPaul Mackerras } 11499b6b563cSPaul Mackerras 11509b6b563cSPaul Mackerras return 0; 11519b6b563cSPaul Mackerras } 11529b6b563cSPaul Mackerras EXPORT_SYMBOL(device_is_compatible); 11539b6b563cSPaul Mackerras 11549b6b563cSPaul Mackerras 11559b6b563cSPaul Mackerras /** 11569b6b563cSPaul Mackerras * Indicates whether the root node has a given value in its 11579b6b563cSPaul Mackerras * compatible property. 11589b6b563cSPaul Mackerras */ 11599b6b563cSPaul Mackerras int machine_is_compatible(const char *compat) 11609b6b563cSPaul Mackerras { 11619b6b563cSPaul Mackerras struct device_node *root; 11629b6b563cSPaul Mackerras int rc = 0; 11639b6b563cSPaul Mackerras 11649b6b563cSPaul Mackerras root = of_find_node_by_path("/"); 11659b6b563cSPaul Mackerras if (root) { 11669b6b563cSPaul Mackerras rc = device_is_compatible(root, compat); 11679b6b563cSPaul Mackerras of_node_put(root); 11689b6b563cSPaul Mackerras } 11699b6b563cSPaul Mackerras return rc; 11709b6b563cSPaul Mackerras } 11719b6b563cSPaul Mackerras EXPORT_SYMBOL(machine_is_compatible); 11729b6b563cSPaul Mackerras 11739b6b563cSPaul Mackerras /** 11749b6b563cSPaul Mackerras * Construct and return a list of the device_nodes with a given type 11759b6b563cSPaul Mackerras * and compatible property. 11769b6b563cSPaul Mackerras */ 11779b6b563cSPaul Mackerras struct device_node *find_compatible_devices(const char *type, 11789b6b563cSPaul Mackerras const char *compat) 11799b6b563cSPaul Mackerras { 11809b6b563cSPaul Mackerras struct device_node *head, **prevp, *np; 11819b6b563cSPaul Mackerras 11829b6b563cSPaul Mackerras prevp = &head; 11839b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) { 11849b6b563cSPaul Mackerras if (type != NULL 11859b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 11869b6b563cSPaul Mackerras continue; 11879b6b563cSPaul Mackerras if (device_is_compatible(np, compat)) { 11889b6b563cSPaul Mackerras *prevp = np; 11899b6b563cSPaul Mackerras prevp = &np->next; 11909b6b563cSPaul Mackerras } 11919b6b563cSPaul Mackerras } 11929b6b563cSPaul Mackerras *prevp = NULL; 11939b6b563cSPaul Mackerras return head; 11949b6b563cSPaul Mackerras } 11959b6b563cSPaul Mackerras EXPORT_SYMBOL(find_compatible_devices); 11969b6b563cSPaul Mackerras 11979b6b563cSPaul Mackerras /** 11989b6b563cSPaul Mackerras * Find the device_node with a given full_name. 11999b6b563cSPaul Mackerras */ 12009b6b563cSPaul Mackerras struct device_node *find_path_device(const char *path) 12019b6b563cSPaul Mackerras { 12029b6b563cSPaul Mackerras struct device_node *np; 12039b6b563cSPaul Mackerras 12049b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 12059b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0) 12069b6b563cSPaul Mackerras return np; 12079b6b563cSPaul Mackerras return NULL; 12089b6b563cSPaul Mackerras } 12099b6b563cSPaul Mackerras EXPORT_SYMBOL(find_path_device); 12109b6b563cSPaul Mackerras 12119b6b563cSPaul Mackerras /******* 12129b6b563cSPaul Mackerras * 12139b6b563cSPaul Mackerras * New implementation of the OF "find" APIs, return a refcounted 12149b6b563cSPaul Mackerras * object, call of_node_put() when done. The device tree and list 12159b6b563cSPaul Mackerras * are protected by a rw_lock. 12169b6b563cSPaul Mackerras * 12179b6b563cSPaul Mackerras * Note that property management will need some locking as well, 12189b6b563cSPaul Mackerras * this isn't dealt with yet. 12199b6b563cSPaul Mackerras * 12209b6b563cSPaul Mackerras *******/ 12219b6b563cSPaul Mackerras 12229b6b563cSPaul Mackerras /** 12239b6b563cSPaul Mackerras * of_find_node_by_name - Find a node by its "name" property 12249b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 12259b6b563cSPaul Mackerras * you pass will not be searched, only the next one 12269b6b563cSPaul Mackerras * will; typically, you pass what the previous call 12279b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 12289b6b563cSPaul Mackerras * @name: The name string to match against 12299b6b563cSPaul Mackerras * 12309b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 12319b6b563cSPaul Mackerras * of_node_put() on it when done. 12329b6b563cSPaul Mackerras */ 12339b6b563cSPaul Mackerras struct device_node *of_find_node_by_name(struct device_node *from, 12349b6b563cSPaul Mackerras const char *name) 12359b6b563cSPaul Mackerras { 12369b6b563cSPaul Mackerras struct device_node *np; 12379b6b563cSPaul Mackerras 12389b6b563cSPaul Mackerras read_lock(&devtree_lock); 12399b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 1240090db7c8SOlaf Hering for (; np != NULL; np = np->allnext) 1241090db7c8SOlaf Hering if (np->name != NULL && strcasecmp(np->name, name) == 0 12429b6b563cSPaul Mackerras && of_node_get(np)) 12439b6b563cSPaul Mackerras break; 12449b6b563cSPaul Mackerras of_node_put(from); 12459b6b563cSPaul Mackerras read_unlock(&devtree_lock); 12469b6b563cSPaul Mackerras return np; 12479b6b563cSPaul Mackerras } 12489b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_name); 12499b6b563cSPaul Mackerras 12509b6b563cSPaul Mackerras /** 12519b6b563cSPaul Mackerras * of_find_node_by_type - Find a node by its "device_type" property 12529b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 12539b6b563cSPaul Mackerras * you pass will not be searched, only the next one 12549b6b563cSPaul Mackerras * will; typically, you pass what the previous call 12559b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 12569b6b563cSPaul Mackerras * @name: The type string to match against 12579b6b563cSPaul Mackerras * 12589b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 12599b6b563cSPaul Mackerras * of_node_put() on it when done. 12609b6b563cSPaul Mackerras */ 12619b6b563cSPaul Mackerras struct device_node *of_find_node_by_type(struct device_node *from, 12629b6b563cSPaul Mackerras const char *type) 12639b6b563cSPaul Mackerras { 12649b6b563cSPaul Mackerras struct device_node *np; 12659b6b563cSPaul Mackerras 12669b6b563cSPaul Mackerras read_lock(&devtree_lock); 12679b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 12689b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 12699b6b563cSPaul Mackerras if (np->type != 0 && strcasecmp(np->type, type) == 0 12709b6b563cSPaul Mackerras && of_node_get(np)) 12719b6b563cSPaul Mackerras break; 12729b6b563cSPaul Mackerras of_node_put(from); 12739b6b563cSPaul Mackerras read_unlock(&devtree_lock); 12749b6b563cSPaul Mackerras return np; 12759b6b563cSPaul Mackerras } 12769b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_type); 12779b6b563cSPaul Mackerras 12789b6b563cSPaul Mackerras /** 12799b6b563cSPaul Mackerras * of_find_compatible_node - Find a node based on type and one of the 12809b6b563cSPaul Mackerras * tokens in its "compatible" property 12819b6b563cSPaul Mackerras * @from: The node to start searching from or NULL, the node 12829b6b563cSPaul Mackerras * you pass will not be searched, only the next one 12839b6b563cSPaul Mackerras * will; typically, you pass what the previous call 12849b6b563cSPaul Mackerras * returned. of_node_put() will be called on it 12859b6b563cSPaul Mackerras * @type: The type string to match "device_type" or NULL to ignore 12869b6b563cSPaul Mackerras * @compatible: The string to match to one of the tokens in the device 12879b6b563cSPaul Mackerras * "compatible" list. 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_compatible_node(struct device_node *from, 12939b6b563cSPaul Mackerras const char *type, const char *compatible) 12949b6b563cSPaul Mackerras { 12959b6b563cSPaul Mackerras struct device_node *np; 12969b6b563cSPaul Mackerras 12979b6b563cSPaul Mackerras read_lock(&devtree_lock); 12989b6b563cSPaul Mackerras np = from ? from->allnext : allnodes; 12999b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 13009b6b563cSPaul Mackerras if (type != NULL 13019b6b563cSPaul Mackerras && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 13029b6b563cSPaul Mackerras continue; 13039b6b563cSPaul Mackerras if (device_is_compatible(np, compatible) && of_node_get(np)) 13049b6b563cSPaul Mackerras break; 13059b6b563cSPaul Mackerras } 13069b6b563cSPaul Mackerras of_node_put(from); 13079b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13089b6b563cSPaul Mackerras return np; 13099b6b563cSPaul Mackerras } 13109b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_compatible_node); 13119b6b563cSPaul Mackerras 13129b6b563cSPaul Mackerras /** 13139b6b563cSPaul Mackerras * of_find_node_by_path - Find a node matching a full OF path 13149b6b563cSPaul Mackerras * @path: The full path to match 13159b6b563cSPaul Mackerras * 13169b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13179b6b563cSPaul Mackerras * of_node_put() on it when done. 13189b6b563cSPaul Mackerras */ 13199b6b563cSPaul Mackerras struct device_node *of_find_node_by_path(const char *path) 13209b6b563cSPaul Mackerras { 13219b6b563cSPaul Mackerras struct device_node *np = allnodes; 13229b6b563cSPaul Mackerras 13239b6b563cSPaul Mackerras read_lock(&devtree_lock); 13249b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) { 13259b6b563cSPaul Mackerras if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0 13269b6b563cSPaul Mackerras && of_node_get(np)) 13279b6b563cSPaul Mackerras break; 13289b6b563cSPaul Mackerras } 13299b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13309b6b563cSPaul Mackerras return np; 13319b6b563cSPaul Mackerras } 13329b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_path); 13339b6b563cSPaul Mackerras 13349b6b563cSPaul Mackerras /** 13359b6b563cSPaul Mackerras * of_find_node_by_phandle - Find a node given a phandle 13369b6b563cSPaul Mackerras * @handle: phandle of the node to find 13379b6b563cSPaul Mackerras * 13389b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13399b6b563cSPaul Mackerras * of_node_put() on it when done. 13409b6b563cSPaul Mackerras */ 13419b6b563cSPaul Mackerras struct device_node *of_find_node_by_phandle(phandle handle) 13429b6b563cSPaul Mackerras { 13439b6b563cSPaul Mackerras struct device_node *np; 13449b6b563cSPaul Mackerras 13459b6b563cSPaul Mackerras read_lock(&devtree_lock); 13469b6b563cSPaul Mackerras for (np = allnodes; np != 0; np = np->allnext) 13479b6b563cSPaul Mackerras if (np->linux_phandle == handle) 13489b6b563cSPaul Mackerras break; 13499b6b563cSPaul Mackerras of_node_get(np); 13509b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13519b6b563cSPaul Mackerras return np; 13529b6b563cSPaul Mackerras } 13539b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_phandle); 13549b6b563cSPaul Mackerras 13559b6b563cSPaul Mackerras /** 13569b6b563cSPaul Mackerras * of_find_all_nodes - Get next node in global list 13579b6b563cSPaul Mackerras * @prev: Previous node or NULL to start iteration 13589b6b563cSPaul Mackerras * of_node_put() will be called on it 13599b6b563cSPaul Mackerras * 13609b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13619b6b563cSPaul Mackerras * of_node_put() on it when done. 13629b6b563cSPaul Mackerras */ 13639b6b563cSPaul Mackerras struct device_node *of_find_all_nodes(struct device_node *prev) 13649b6b563cSPaul Mackerras { 13659b6b563cSPaul Mackerras struct device_node *np; 13669b6b563cSPaul Mackerras 13679b6b563cSPaul Mackerras read_lock(&devtree_lock); 13689b6b563cSPaul Mackerras np = prev ? prev->allnext : allnodes; 13699b6b563cSPaul Mackerras for (; np != 0; np = np->allnext) 13709b6b563cSPaul Mackerras if (of_node_get(np)) 13719b6b563cSPaul Mackerras break; 13729b6b563cSPaul Mackerras of_node_put(prev); 13739b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13749b6b563cSPaul Mackerras return np; 13759b6b563cSPaul Mackerras } 13769b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_all_nodes); 13779b6b563cSPaul Mackerras 13789b6b563cSPaul Mackerras /** 13799b6b563cSPaul Mackerras * of_get_parent - Get a node's parent if any 13809b6b563cSPaul Mackerras * @node: Node to get parent 13819b6b563cSPaul Mackerras * 13829b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 13839b6b563cSPaul Mackerras * of_node_put() on it when done. 13849b6b563cSPaul Mackerras */ 13859b6b563cSPaul Mackerras struct device_node *of_get_parent(const struct device_node *node) 13869b6b563cSPaul Mackerras { 13879b6b563cSPaul Mackerras struct device_node *np; 13889b6b563cSPaul Mackerras 13899b6b563cSPaul Mackerras if (!node) 13909b6b563cSPaul Mackerras return NULL; 13919b6b563cSPaul Mackerras 13929b6b563cSPaul Mackerras read_lock(&devtree_lock); 13939b6b563cSPaul Mackerras np = of_node_get(node->parent); 13949b6b563cSPaul Mackerras read_unlock(&devtree_lock); 13959b6b563cSPaul Mackerras return np; 13969b6b563cSPaul Mackerras } 13979b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_parent); 13989b6b563cSPaul Mackerras 13999b6b563cSPaul Mackerras /** 14009b6b563cSPaul Mackerras * of_get_next_child - Iterate a node childs 14019b6b563cSPaul Mackerras * @node: parent node 14029b6b563cSPaul Mackerras * @prev: previous child of the parent node, or NULL to get first 14039b6b563cSPaul Mackerras * 14049b6b563cSPaul Mackerras * Returns a node pointer with refcount incremented, use 14059b6b563cSPaul Mackerras * of_node_put() on it when done. 14069b6b563cSPaul Mackerras */ 14079b6b563cSPaul Mackerras struct device_node *of_get_next_child(const struct device_node *node, 14089b6b563cSPaul Mackerras struct device_node *prev) 14099b6b563cSPaul Mackerras { 14109b6b563cSPaul Mackerras struct device_node *next; 14119b6b563cSPaul Mackerras 14129b6b563cSPaul Mackerras read_lock(&devtree_lock); 14139b6b563cSPaul Mackerras next = prev ? prev->sibling : node->child; 14149b6b563cSPaul Mackerras for (; next != 0; next = next->sibling) 14159b6b563cSPaul Mackerras if (of_node_get(next)) 14169b6b563cSPaul Mackerras break; 14179b6b563cSPaul Mackerras of_node_put(prev); 14189b6b563cSPaul Mackerras read_unlock(&devtree_lock); 14199b6b563cSPaul Mackerras return next; 14209b6b563cSPaul Mackerras } 14219b6b563cSPaul Mackerras EXPORT_SYMBOL(of_get_next_child); 14229b6b563cSPaul Mackerras 14239b6b563cSPaul Mackerras /** 14249b6b563cSPaul Mackerras * of_node_get - Increment refcount of a node 14259b6b563cSPaul Mackerras * @node: Node to inc refcount, NULL is supported to 14269b6b563cSPaul Mackerras * simplify writing of callers 14279b6b563cSPaul Mackerras * 14289b6b563cSPaul Mackerras * Returns node. 14299b6b563cSPaul Mackerras */ 14309b6b563cSPaul Mackerras struct device_node *of_node_get(struct device_node *node) 14319b6b563cSPaul Mackerras { 14329b6b563cSPaul Mackerras if (node) 14339b6b563cSPaul Mackerras kref_get(&node->kref); 14349b6b563cSPaul Mackerras return node; 14359b6b563cSPaul Mackerras } 14369b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_get); 14379b6b563cSPaul Mackerras 14389b6b563cSPaul Mackerras static inline struct device_node * kref_to_device_node(struct kref *kref) 14399b6b563cSPaul Mackerras { 14409b6b563cSPaul Mackerras return container_of(kref, struct device_node, kref); 14419b6b563cSPaul Mackerras } 14429b6b563cSPaul Mackerras 14439b6b563cSPaul Mackerras /** 14449b6b563cSPaul Mackerras * of_node_release - release a dynamically allocated node 14459b6b563cSPaul Mackerras * @kref: kref element of the node to be released 14469b6b563cSPaul Mackerras * 14479b6b563cSPaul Mackerras * In of_node_put() this function is passed to kref_put() 14489b6b563cSPaul Mackerras * as the destructor. 14499b6b563cSPaul Mackerras */ 14509b6b563cSPaul Mackerras static void of_node_release(struct kref *kref) 14519b6b563cSPaul Mackerras { 14529b6b563cSPaul Mackerras struct device_node *node = kref_to_device_node(kref); 14539b6b563cSPaul Mackerras struct property *prop = node->properties; 14549b6b563cSPaul Mackerras 14559b6b563cSPaul Mackerras if (!OF_IS_DYNAMIC(node)) 14569b6b563cSPaul Mackerras return; 14579b6b563cSPaul Mackerras while (prop) { 14589b6b563cSPaul Mackerras struct property *next = prop->next; 14599b6b563cSPaul Mackerras kfree(prop->name); 14609b6b563cSPaul Mackerras kfree(prop->value); 14619b6b563cSPaul Mackerras kfree(prop); 14629b6b563cSPaul Mackerras prop = next; 1463088186deSDave C Boutcher 1464088186deSDave C Boutcher if (!prop) { 1465088186deSDave C Boutcher prop = node->deadprops; 1466088186deSDave C Boutcher node->deadprops = NULL; 1467088186deSDave C Boutcher } 14689b6b563cSPaul Mackerras } 14699b6b563cSPaul Mackerras kfree(node->full_name); 14709b6b563cSPaul Mackerras kfree(node->data); 14719b6b563cSPaul Mackerras kfree(node); 14729b6b563cSPaul Mackerras } 14739b6b563cSPaul Mackerras 14749b6b563cSPaul Mackerras /** 14759b6b563cSPaul Mackerras * of_node_put - Decrement refcount of a node 14769b6b563cSPaul Mackerras * @node: Node to dec refcount, NULL is supported to 14779b6b563cSPaul Mackerras * simplify writing of callers 14789b6b563cSPaul Mackerras * 14799b6b563cSPaul Mackerras */ 14809b6b563cSPaul Mackerras void of_node_put(struct device_node *node) 14819b6b563cSPaul Mackerras { 14829b6b563cSPaul Mackerras if (node) 14839b6b563cSPaul Mackerras kref_put(&node->kref, of_node_release); 14849b6b563cSPaul Mackerras } 14859b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_put); 14869b6b563cSPaul Mackerras 14879b6b563cSPaul Mackerras /* 14889b6b563cSPaul Mackerras * Plug a device node into the tree and global list. 14899b6b563cSPaul Mackerras */ 14909b6b563cSPaul Mackerras void of_attach_node(struct device_node *np) 14919b6b563cSPaul Mackerras { 14929b6b563cSPaul Mackerras write_lock(&devtree_lock); 14939b6b563cSPaul Mackerras np->sibling = np->parent->child; 14949b6b563cSPaul Mackerras np->allnext = allnodes; 14959b6b563cSPaul Mackerras np->parent->child = np; 14969b6b563cSPaul Mackerras allnodes = np; 14979b6b563cSPaul Mackerras write_unlock(&devtree_lock); 14989b6b563cSPaul Mackerras } 14999b6b563cSPaul Mackerras 15009b6b563cSPaul Mackerras /* 15019b6b563cSPaul Mackerras * "Unplug" a node from the device tree. The caller must hold 15029b6b563cSPaul Mackerras * a reference to the node. The memory associated with the node 15039b6b563cSPaul Mackerras * is not freed until its refcount goes to zero. 15049b6b563cSPaul Mackerras */ 15059b6b563cSPaul Mackerras void of_detach_node(const struct device_node *np) 15069b6b563cSPaul Mackerras { 15079b6b563cSPaul Mackerras struct device_node *parent; 15089b6b563cSPaul Mackerras 15099b6b563cSPaul Mackerras write_lock(&devtree_lock); 15109b6b563cSPaul Mackerras 15119b6b563cSPaul Mackerras parent = np->parent; 15129b6b563cSPaul Mackerras 15139b6b563cSPaul Mackerras if (allnodes == np) 15149b6b563cSPaul Mackerras allnodes = np->allnext; 15159b6b563cSPaul Mackerras else { 15169b6b563cSPaul Mackerras struct device_node *prev; 15179b6b563cSPaul Mackerras for (prev = allnodes; 15189b6b563cSPaul Mackerras prev->allnext != np; 15199b6b563cSPaul Mackerras prev = prev->allnext) 15209b6b563cSPaul Mackerras ; 15219b6b563cSPaul Mackerras prev->allnext = np->allnext; 15229b6b563cSPaul Mackerras } 15239b6b563cSPaul Mackerras 15249b6b563cSPaul Mackerras if (parent->child == np) 15259b6b563cSPaul Mackerras parent->child = np->sibling; 15269b6b563cSPaul Mackerras else { 15279b6b563cSPaul Mackerras struct device_node *prevsib; 15289b6b563cSPaul Mackerras for (prevsib = np->parent->child; 15299b6b563cSPaul Mackerras prevsib->sibling != np; 15309b6b563cSPaul Mackerras prevsib = prevsib->sibling) 15319b6b563cSPaul Mackerras ; 15329b6b563cSPaul Mackerras prevsib->sibling = np->sibling; 15339b6b563cSPaul Mackerras } 15349b6b563cSPaul Mackerras 15359b6b563cSPaul Mackerras write_unlock(&devtree_lock); 15369b6b563cSPaul Mackerras } 15379b6b563cSPaul Mackerras 15389b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES 15399b6b563cSPaul Mackerras /* 15409b6b563cSPaul Mackerras * Fix up the uninitialized fields in a new device node: 15410ebfff14SBenjamin Herrenschmidt * name, type and pci-specific fields 15429b6b563cSPaul Mackerras */ 15439b6b563cSPaul Mackerras 1544cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node) 15459b6b563cSPaul Mackerras { 15469b6b563cSPaul Mackerras struct device_node *parent = of_get_parent(node); 15479b6b563cSPaul Mackerras int err = 0; 1548a7f67bdfSJeremy Kerr const phandle *ibm_phandle; 15499b6b563cSPaul Mackerras 15509b6b563cSPaul Mackerras node->name = get_property(node, "name", NULL); 15519b6b563cSPaul Mackerras node->type = get_property(node, "device_type", NULL); 15529b6b563cSPaul Mackerras 15539b6b563cSPaul Mackerras if (!parent) { 15549b6b563cSPaul Mackerras err = -ENODEV; 15559b6b563cSPaul Mackerras goto out; 15569b6b563cSPaul Mackerras } 15579b6b563cSPaul Mackerras 15589b6b563cSPaul Mackerras /* We don't support that function on PowerMac, at least 15599b6b563cSPaul Mackerras * not yet 15609b6b563cSPaul Mackerras */ 1561e8222502SBenjamin Herrenschmidt if (machine_is(powermac)) 15629b6b563cSPaul Mackerras return -ENODEV; 15639b6b563cSPaul Mackerras 15649b6b563cSPaul Mackerras /* fix up new node's linux_phandle field */ 1565a7f67bdfSJeremy Kerr if ((ibm_phandle = get_property(node, "ibm,phandle", NULL))) 15669b6b563cSPaul Mackerras node->linux_phandle = *ibm_phandle; 15679b6b563cSPaul Mackerras 15689b6b563cSPaul Mackerras out: 15699b6b563cSPaul Mackerras of_node_put(parent); 15709b6b563cSPaul Mackerras return err; 15719b6b563cSPaul Mackerras } 15729b6b563cSPaul Mackerras 15739b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb, 15749b6b563cSPaul Mackerras unsigned long action, void *node) 15759b6b563cSPaul Mackerras { 15769b6b563cSPaul Mackerras int err; 15779b6b563cSPaul Mackerras 15789b6b563cSPaul Mackerras switch (action) { 15799b6b563cSPaul Mackerras case PSERIES_RECONFIG_ADD: 1580cc5d0189SBenjamin Herrenschmidt err = of_finish_dynamic_node(node); 15819b6b563cSPaul Mackerras if (err < 0) { 15829b6b563cSPaul Mackerras printk(KERN_ERR "finish_node returned %d\n", err); 15839b6b563cSPaul Mackerras err = NOTIFY_BAD; 15849b6b563cSPaul Mackerras } 15859b6b563cSPaul Mackerras break; 15869b6b563cSPaul Mackerras default: 15879b6b563cSPaul Mackerras err = NOTIFY_DONE; 15889b6b563cSPaul Mackerras break; 15899b6b563cSPaul Mackerras } 15909b6b563cSPaul Mackerras return err; 15919b6b563cSPaul Mackerras } 15929b6b563cSPaul Mackerras 15939b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = { 15949b6b563cSPaul Mackerras .notifier_call = prom_reconfig_notifier, 15959b6b563cSPaul Mackerras .priority = 10, /* This one needs to run first */ 15969b6b563cSPaul Mackerras }; 15979b6b563cSPaul Mackerras 15989b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void) 15999b6b563cSPaul Mackerras { 16009b6b563cSPaul Mackerras return pSeries_reconfig_notifier_register(&prom_reconfig_nb); 16019b6b563cSPaul Mackerras } 16029b6b563cSPaul Mackerras __initcall(prom_reconfig_setup); 16039b6b563cSPaul Mackerras #endif 16049b6b563cSPaul Mackerras 1605e2100efbSBenjamin Herrenschmidt struct property *of_find_property(const struct device_node *np, 1606e2100efbSBenjamin Herrenschmidt const char *name, 16079b6b563cSPaul Mackerras int *lenp) 16089b6b563cSPaul Mackerras { 16099b6b563cSPaul Mackerras struct property *pp; 16109b6b563cSPaul Mackerras 1611088186deSDave C Boutcher read_lock(&devtree_lock); 16129b6b563cSPaul Mackerras for (pp = np->properties; pp != 0; pp = pp->next) 16139b6b563cSPaul Mackerras if (strcmp(pp->name, name) == 0) { 16149b6b563cSPaul Mackerras if (lenp != 0) 16159b6b563cSPaul Mackerras *lenp = pp->length; 1616088186deSDave C Boutcher break; 16179b6b563cSPaul Mackerras } 1618088186deSDave C Boutcher read_unlock(&devtree_lock); 1619088186deSDave C Boutcher 1620ecaa8b0fSDave C Boutcher return pp; 1621ecaa8b0fSDave C Boutcher } 1622ae50517eSDave Jones EXPORT_SYMBOL(of_find_property); 1623ecaa8b0fSDave C Boutcher 1624ecaa8b0fSDave C Boutcher /* 1625ecaa8b0fSDave C Boutcher * Find a property with a given name for a given node 1626ecaa8b0fSDave C Boutcher * and return the value. 1627ecaa8b0fSDave C Boutcher */ 1628e2100efbSBenjamin Herrenschmidt const void *get_property(const struct device_node *np, const char *name, 1629e2100efbSBenjamin Herrenschmidt int *lenp) 1630ecaa8b0fSDave C Boutcher { 1631ecaa8b0fSDave C Boutcher struct property *pp = of_find_property(np,name,lenp); 1632088186deSDave C Boutcher return pp ? pp->value : NULL; 16339b6b563cSPaul Mackerras } 16349b6b563cSPaul Mackerras EXPORT_SYMBOL(get_property); 16359b6b563cSPaul Mackerras 16369b6b563cSPaul Mackerras /* 16379b6b563cSPaul Mackerras * Add a property to a node 16389b6b563cSPaul Mackerras */ 1639183d0202SBenjamin Herrenschmidt int prom_add_property(struct device_node* np, struct property* prop) 16409b6b563cSPaul Mackerras { 1641183d0202SBenjamin Herrenschmidt struct property **next; 16429b6b563cSPaul Mackerras 16439b6b563cSPaul Mackerras prop->next = NULL; 1644183d0202SBenjamin Herrenschmidt write_lock(&devtree_lock); 1645183d0202SBenjamin Herrenschmidt next = &np->properties; 1646183d0202SBenjamin Herrenschmidt while (*next) { 1647183d0202SBenjamin Herrenschmidt if (strcmp(prop->name, (*next)->name) == 0) { 1648183d0202SBenjamin Herrenschmidt /* duplicate ! don't insert it */ 1649183d0202SBenjamin Herrenschmidt write_unlock(&devtree_lock); 1650183d0202SBenjamin Herrenschmidt return -1; 1651183d0202SBenjamin Herrenschmidt } 16529b6b563cSPaul Mackerras next = &(*next)->next; 1653183d0202SBenjamin Herrenschmidt } 16549b6b563cSPaul Mackerras *next = prop; 1655183d0202SBenjamin Herrenschmidt write_unlock(&devtree_lock); 1656183d0202SBenjamin Herrenschmidt 1657799d6046SPaul Mackerras #ifdef CONFIG_PROC_DEVICETREE 1658183d0202SBenjamin Herrenschmidt /* try to add to proc as well if it was initialized */ 1659183d0202SBenjamin Herrenschmidt if (np->pde) 1660183d0202SBenjamin Herrenschmidt proc_device_tree_add_prop(np->pde, prop); 1661799d6046SPaul Mackerras #endif /* CONFIG_PROC_DEVICETREE */ 1662183d0202SBenjamin Herrenschmidt 1663183d0202SBenjamin Herrenschmidt return 0; 16649b6b563cSPaul Mackerras } 16659b6b563cSPaul Mackerras 1666088186deSDave C Boutcher /* 1667088186deSDave C Boutcher * Remove a property from a node. Note that we don't actually 1668088186deSDave C Boutcher * remove it, since we have given out who-knows-how-many pointers 1669088186deSDave C Boutcher * to the data using get-property. Instead we just move the property 1670088186deSDave C Boutcher * to the "dead properties" list, so it won't be found any more. 1671088186deSDave C Boutcher */ 1672088186deSDave C Boutcher int prom_remove_property(struct device_node *np, struct property *prop) 1673088186deSDave C Boutcher { 1674088186deSDave C Boutcher struct property **next; 1675088186deSDave C Boutcher int found = 0; 16769b6b563cSPaul Mackerras 1677088186deSDave C Boutcher write_lock(&devtree_lock); 1678088186deSDave C Boutcher next = &np->properties; 1679088186deSDave C Boutcher while (*next) { 1680088186deSDave C Boutcher if (*next == prop) { 1681088186deSDave C Boutcher /* found the node */ 1682088186deSDave C Boutcher *next = prop->next; 1683088186deSDave C Boutcher prop->next = np->deadprops; 1684088186deSDave C Boutcher np->deadprops = prop; 1685088186deSDave C Boutcher found = 1; 1686088186deSDave C Boutcher break; 1687088186deSDave C Boutcher } 1688088186deSDave C Boutcher next = &(*next)->next; 1689088186deSDave C Boutcher } 1690088186deSDave C Boutcher write_unlock(&devtree_lock); 1691088186deSDave C Boutcher 1692088186deSDave C Boutcher if (!found) 1693088186deSDave C Boutcher return -ENODEV; 1694088186deSDave C Boutcher 1695088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE 1696088186deSDave C Boutcher /* try to remove the proc node as well */ 1697088186deSDave C Boutcher if (np->pde) 1698088186deSDave C Boutcher proc_device_tree_remove_prop(np->pde, prop); 1699088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */ 1700088186deSDave C Boutcher 1701088186deSDave C Boutcher return 0; 1702088186deSDave C Boutcher } 1703088186deSDave C Boutcher 1704088186deSDave C Boutcher /* 1705088186deSDave C Boutcher * Update a property in a node. Note that we don't actually 1706088186deSDave C Boutcher * remove it, since we have given out who-knows-how-many pointers 1707088186deSDave C Boutcher * to the data using get-property. Instead we just move the property 1708088186deSDave C Boutcher * to the "dead properties" list, and add the new property to the 1709088186deSDave C Boutcher * property list 1710088186deSDave C Boutcher */ 1711088186deSDave C Boutcher int prom_update_property(struct device_node *np, 1712088186deSDave C Boutcher struct property *newprop, 1713088186deSDave C Boutcher struct property *oldprop) 1714088186deSDave C Boutcher { 1715088186deSDave C Boutcher struct property **next; 1716088186deSDave C Boutcher int found = 0; 1717088186deSDave C Boutcher 1718088186deSDave C Boutcher write_lock(&devtree_lock); 1719088186deSDave C Boutcher next = &np->properties; 1720088186deSDave C Boutcher while (*next) { 1721088186deSDave C Boutcher if (*next == oldprop) { 1722088186deSDave C Boutcher /* found the node */ 1723088186deSDave C Boutcher newprop->next = oldprop->next; 1724088186deSDave C Boutcher *next = newprop; 1725088186deSDave C Boutcher oldprop->next = np->deadprops; 1726088186deSDave C Boutcher np->deadprops = oldprop; 1727088186deSDave C Boutcher found = 1; 1728088186deSDave C Boutcher break; 1729088186deSDave C Boutcher } 1730088186deSDave C Boutcher next = &(*next)->next; 1731088186deSDave C Boutcher } 1732088186deSDave C Boutcher write_unlock(&devtree_lock); 1733088186deSDave C Boutcher 1734088186deSDave C Boutcher if (!found) 1735088186deSDave C Boutcher return -ENODEV; 1736088186deSDave C Boutcher 1737088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE 1738088186deSDave C Boutcher /* try to add to proc as well if it was initialized */ 1739088186deSDave C Boutcher if (np->pde) 1740088186deSDave C Boutcher proc_device_tree_update_prop(np->pde, newprop, oldprop); 1741088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */ 1742088186deSDave C Boutcher 1743088186deSDave C Boutcher return 0; 1744088186deSDave C Boutcher } 1745b68239eeSMichael Ellerman 1746acf7d768SBenjamin Herrenschmidt 1747acf7d768SBenjamin Herrenschmidt /* Find the device node for a given logical cpu number, also returns the cpu 1748acf7d768SBenjamin Herrenschmidt * local thread number (index in ibm,interrupt-server#s) if relevant and 1749acf7d768SBenjamin Herrenschmidt * asked for (non NULL) 1750acf7d768SBenjamin Herrenschmidt */ 1751acf7d768SBenjamin Herrenschmidt struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) 1752acf7d768SBenjamin Herrenschmidt { 1753acf7d768SBenjamin Herrenschmidt int hardid; 1754acf7d768SBenjamin Herrenschmidt struct device_node *np; 1755acf7d768SBenjamin Herrenschmidt 1756acf7d768SBenjamin Herrenschmidt hardid = get_hard_smp_processor_id(cpu); 1757acf7d768SBenjamin Herrenschmidt 1758acf7d768SBenjamin Herrenschmidt for_each_node_by_type(np, "cpu") { 1759a7f67bdfSJeremy Kerr const u32 *intserv; 1760acf7d768SBenjamin Herrenschmidt unsigned int plen, t; 1761acf7d768SBenjamin Herrenschmidt 1762acf7d768SBenjamin Herrenschmidt /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist 1763acf7d768SBenjamin Herrenschmidt * fallback to "reg" property and assume no threads 1764acf7d768SBenjamin Herrenschmidt */ 1765a7f67bdfSJeremy Kerr intserv = get_property(np, "ibm,ppc-interrupt-server#s", 1766acf7d768SBenjamin Herrenschmidt &plen); 1767acf7d768SBenjamin Herrenschmidt if (intserv == NULL) { 1768a7f67bdfSJeremy Kerr const u32 *reg = get_property(np, "reg", NULL); 1769acf7d768SBenjamin Herrenschmidt if (reg == NULL) 1770acf7d768SBenjamin Herrenschmidt continue; 1771acf7d768SBenjamin Herrenschmidt if (*reg == hardid) { 1772acf7d768SBenjamin Herrenschmidt if (thread) 1773acf7d768SBenjamin Herrenschmidt *thread = 0; 1774acf7d768SBenjamin Herrenschmidt return np; 1775acf7d768SBenjamin Herrenschmidt } 1776acf7d768SBenjamin Herrenschmidt } else { 1777acf7d768SBenjamin Herrenschmidt plen /= sizeof(u32); 1778acf7d768SBenjamin Herrenschmidt for (t = 0; t < plen; t++) { 1779acf7d768SBenjamin Herrenschmidt if (hardid == intserv[t]) { 1780acf7d768SBenjamin Herrenschmidt if (thread) 1781acf7d768SBenjamin Herrenschmidt *thread = t; 1782acf7d768SBenjamin Herrenschmidt return np; 1783acf7d768SBenjamin Herrenschmidt } 1784acf7d768SBenjamin Herrenschmidt } 1785acf7d768SBenjamin Herrenschmidt } 1786acf7d768SBenjamin Herrenschmidt } 1787acf7d768SBenjamin Herrenschmidt return NULL; 1788acf7d768SBenjamin Herrenschmidt } 178936ca4ba4SChristian Krafft EXPORT_SYMBOL(of_get_cpu_node); 17907a4571aeSMichael Ellerman 17917a4571aeSMichael Ellerman #ifdef DEBUG 17927a4571aeSMichael Ellerman static struct debugfs_blob_wrapper flat_dt_blob; 17937a4571aeSMichael Ellerman 17947a4571aeSMichael Ellerman static int __init export_flat_device_tree(void) 17957a4571aeSMichael Ellerman { 17967a4571aeSMichael Ellerman struct dentry *d; 17977a4571aeSMichael Ellerman 17987a4571aeSMichael Ellerman d = debugfs_create_dir("powerpc", NULL); 17997a4571aeSMichael Ellerman if (!d) 18007a4571aeSMichael Ellerman return 1; 18017a4571aeSMichael Ellerman 18027a4571aeSMichael Ellerman flat_dt_blob.data = initial_boot_params; 18037a4571aeSMichael Ellerman flat_dt_blob.size = initial_boot_params->totalsize; 18047a4571aeSMichael Ellerman 18057a4571aeSMichael Ellerman d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR, 18067a4571aeSMichael Ellerman d, &flat_dt_blob); 18077a4571aeSMichael Ellerman if (!d) 18087a4571aeSMichael Ellerman return 1; 18097a4571aeSMichael Ellerman 18107a4571aeSMichael Ellerman return 0; 18117a4571aeSMichael Ellerman } 18127a4571aeSMichael Ellerman __initcall(export_flat_device_tree); 18137a4571aeSMichael Ellerman #endif 1814