xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision 49a849652513235a244dfbf5e58c54f796bd1148)
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>
34d9b2b2a2SDavid S. Miller #include <linux/lmb.h>
359b6b563cSPaul Mackerras 
369b6b563cSPaul Mackerras #include <asm/prom.h>
379b6b563cSPaul Mackerras #include <asm/rtas.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>
546ac26c8aSManish Ahuja #include <asm/phyp_dump.h>
552babf5c2SMichael Ellerman #include <asm/kexec.h>
5637dd2badSKumar Gala #include <mm/mmu_decl.h>
579b6b563cSPaul Mackerras 
589b6b563cSPaul Mackerras #ifdef DEBUG
599b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt)
609b6b563cSPaul Mackerras #else
619b6b563cSPaul Mackerras #define DBG(fmt...)
629b6b563cSPaul Mackerras #endif
639b6b563cSPaul Mackerras 
649b6b563cSPaul Mackerras 
659b6b563cSPaul Mackerras static int __initdata dt_root_addr_cells;
669b6b563cSPaul Mackerras static int __initdata dt_root_size_cells;
679b6b563cSPaul Mackerras 
689b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6928897731SOlof Johansson int __initdata iommu_is_off;
709b6b563cSPaul Mackerras int __initdata iommu_force_on;
71cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
729b6b563cSPaul Mackerras #endif
739b6b563cSPaul Mackerras 
749b6b563cSPaul Mackerras typedef u32 cell_t;
759b6b563cSPaul Mackerras 
769b6b563cSPaul Mackerras #if 0
779b6b563cSPaul Mackerras static struct boot_param_header *initial_boot_params __initdata;
789b6b563cSPaul Mackerras #else
799b6b563cSPaul Mackerras struct boot_param_header *initial_boot_params;
809b6b563cSPaul Mackerras #endif
819b6b563cSPaul Mackerras 
821ef4d424SStephen Rothwell extern struct device_node *allnodes;	/* temporary while merging */
839b6b563cSPaul Mackerras 
84581b605aSStephen Rothwell extern rwlock_t devtree_lock;	/* temporary while merging */
859b6b563cSPaul Mackerras 
869b6b563cSPaul Mackerras /* export that to outside world */
879b6b563cSPaul Mackerras struct device_node *of_chosen;
889b6b563cSPaul Mackerras 
899b6b563cSPaul Mackerras static inline char *find_flat_dt_string(u32 offset)
909b6b563cSPaul Mackerras {
919b6b563cSPaul Mackerras 	return ((char *)initial_boot_params) +
929b6b563cSPaul Mackerras 		initial_boot_params->off_dt_strings + offset;
939b6b563cSPaul Mackerras }
949b6b563cSPaul Mackerras 
959b6b563cSPaul Mackerras /**
969b6b563cSPaul Mackerras  * This function is used to scan the flattened device-tree, it is
979b6b563cSPaul Mackerras  * used to extract the memory informations at boot before we can
989b6b563cSPaul Mackerras  * unflatten the tree
999b6b563cSPaul Mackerras  */
1003c726f8dSBenjamin Herrenschmidt int __init of_scan_flat_dt(int (*it)(unsigned long node,
1019b6b563cSPaul Mackerras 				     const char *uname, int depth,
1029b6b563cSPaul Mackerras 				     void *data),
1039b6b563cSPaul Mackerras 			   void *data)
1049b6b563cSPaul Mackerras {
1059b6b563cSPaul Mackerras 	unsigned long p = ((unsigned long)initial_boot_params) +
1069b6b563cSPaul Mackerras 		initial_boot_params->off_dt_struct;
1079b6b563cSPaul Mackerras 	int rc = 0;
1089b6b563cSPaul Mackerras 	int depth = -1;
1099b6b563cSPaul Mackerras 
1109b6b563cSPaul Mackerras 	do {
1119b6b563cSPaul Mackerras 		u32 tag = *((u32 *)p);
1129b6b563cSPaul Mackerras 		char *pathp;
1139b6b563cSPaul Mackerras 
1149b6b563cSPaul Mackerras 		p += 4;
1159b6b563cSPaul Mackerras 		if (tag == OF_DT_END_NODE) {
1169b6b563cSPaul Mackerras 			depth --;
1179b6b563cSPaul Mackerras 			continue;
1189b6b563cSPaul Mackerras 		}
1199b6b563cSPaul Mackerras 		if (tag == OF_DT_NOP)
1209b6b563cSPaul Mackerras 			continue;
1219b6b563cSPaul Mackerras 		if (tag == OF_DT_END)
1229b6b563cSPaul Mackerras 			break;
1239b6b563cSPaul Mackerras 		if (tag == OF_DT_PROP) {
1249b6b563cSPaul Mackerras 			u32 sz = *((u32 *)p);
1259b6b563cSPaul Mackerras 			p += 8;
1269b6b563cSPaul Mackerras 			if (initial_boot_params->version < 0x10)
1279b6b563cSPaul Mackerras 				p = _ALIGN(p, sz >= 8 ? 8 : 4);
1289b6b563cSPaul Mackerras 			p += sz;
1299b6b563cSPaul Mackerras 			p = _ALIGN(p, 4);
1309b6b563cSPaul Mackerras 			continue;
1319b6b563cSPaul Mackerras 		}
1329b6b563cSPaul Mackerras 		if (tag != OF_DT_BEGIN_NODE) {
1339b6b563cSPaul Mackerras 			printk(KERN_WARNING "Invalid tag %x scanning flattened"
1349b6b563cSPaul Mackerras 			       " device tree !\n", tag);
1359b6b563cSPaul Mackerras 			return -EINVAL;
1369b6b563cSPaul Mackerras 		}
1379b6b563cSPaul Mackerras 		depth++;
1389b6b563cSPaul Mackerras 		pathp = (char *)p;
1399b6b563cSPaul Mackerras 		p = _ALIGN(p + strlen(pathp) + 1, 4);
1409b6b563cSPaul Mackerras 		if ((*pathp) == '/') {
1419b6b563cSPaul Mackerras 			char *lp, *np;
1429b6b563cSPaul Mackerras 			for (lp = NULL, np = pathp; *np; np++)
1439b6b563cSPaul Mackerras 				if ((*np) == '/')
1449b6b563cSPaul Mackerras 					lp = np+1;
1459b6b563cSPaul Mackerras 			if (lp != NULL)
1469b6b563cSPaul Mackerras 				pathp = lp;
1479b6b563cSPaul Mackerras 		}
1489b6b563cSPaul Mackerras 		rc = it(p, pathp, depth, data);
1499b6b563cSPaul Mackerras 		if (rc != 0)
1509b6b563cSPaul Mackerras 			break;
1519b6b563cSPaul Mackerras 	} while(1);
1529b6b563cSPaul Mackerras 
1539b6b563cSPaul Mackerras 	return rc;
1549b6b563cSPaul Mackerras }
1559b6b563cSPaul Mackerras 
156e8222502SBenjamin Herrenschmidt unsigned long __init of_get_flat_dt_root(void)
157e8222502SBenjamin Herrenschmidt {
158e8222502SBenjamin Herrenschmidt 	unsigned long p = ((unsigned long)initial_boot_params) +
159e8222502SBenjamin Herrenschmidt 		initial_boot_params->off_dt_struct;
160e8222502SBenjamin Herrenschmidt 
161e8222502SBenjamin Herrenschmidt 	while(*((u32 *)p) == OF_DT_NOP)
162e8222502SBenjamin Herrenschmidt 		p += 4;
163e8222502SBenjamin Herrenschmidt 	BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
164e8222502SBenjamin Herrenschmidt 	p += 4;
165e8222502SBenjamin Herrenschmidt 	return _ALIGN(p + strlen((char *)p) + 1, 4);
166e8222502SBenjamin Herrenschmidt }
167e8222502SBenjamin Herrenschmidt 
1689b6b563cSPaul Mackerras /**
1699b6b563cSPaul Mackerras  * This  function can be used within scan_flattened_dt callback to get
1709b6b563cSPaul Mackerras  * access to properties
1719b6b563cSPaul Mackerras  */
1723c726f8dSBenjamin Herrenschmidt void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
1739b6b563cSPaul Mackerras 				 unsigned long *size)
1749b6b563cSPaul Mackerras {
1759b6b563cSPaul Mackerras 	unsigned long p = node;
1769b6b563cSPaul Mackerras 
1779b6b563cSPaul Mackerras 	do {
1789b6b563cSPaul Mackerras 		u32 tag = *((u32 *)p);
1799b6b563cSPaul Mackerras 		u32 sz, noff;
1809b6b563cSPaul Mackerras 		const char *nstr;
1819b6b563cSPaul Mackerras 
1829b6b563cSPaul Mackerras 		p += 4;
1839b6b563cSPaul Mackerras 		if (tag == OF_DT_NOP)
1849b6b563cSPaul Mackerras 			continue;
1859b6b563cSPaul Mackerras 		if (tag != OF_DT_PROP)
1869b6b563cSPaul Mackerras 			return NULL;
1879b6b563cSPaul Mackerras 
1889b6b563cSPaul Mackerras 		sz = *((u32 *)p);
1899b6b563cSPaul Mackerras 		noff = *((u32 *)(p + 4));
1909b6b563cSPaul Mackerras 		p += 8;
1919b6b563cSPaul Mackerras 		if (initial_boot_params->version < 0x10)
1929b6b563cSPaul Mackerras 			p = _ALIGN(p, sz >= 8 ? 8 : 4);
1939b6b563cSPaul Mackerras 
1949b6b563cSPaul Mackerras 		nstr = find_flat_dt_string(noff);
1959b6b563cSPaul Mackerras 		if (nstr == NULL) {
1969b6b563cSPaul Mackerras 			printk(KERN_WARNING "Can't find property index"
1979b6b563cSPaul Mackerras 			       " name !\n");
1989b6b563cSPaul Mackerras 			return NULL;
1999b6b563cSPaul Mackerras 		}
2009b6b563cSPaul Mackerras 		if (strcmp(name, nstr) == 0) {
2019b6b563cSPaul Mackerras 			if (size)
2029b6b563cSPaul Mackerras 				*size = sz;
2039b6b563cSPaul Mackerras 			return (void *)p;
2049b6b563cSPaul Mackerras 		}
2059b6b563cSPaul Mackerras 		p += sz;
2069b6b563cSPaul Mackerras 		p = _ALIGN(p, 4);
2079b6b563cSPaul Mackerras 	} while(1);
2089b6b563cSPaul Mackerras }
2099b6b563cSPaul Mackerras 
210e8222502SBenjamin Herrenschmidt int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
211e8222502SBenjamin Herrenschmidt {
212e8222502SBenjamin Herrenschmidt 	const char* cp;
213e8222502SBenjamin Herrenschmidt 	unsigned long cplen, l;
214e8222502SBenjamin Herrenschmidt 
215e8222502SBenjamin Herrenschmidt 	cp = of_get_flat_dt_prop(node, "compatible", &cplen);
216e8222502SBenjamin Herrenschmidt 	if (cp == NULL)
217e8222502SBenjamin Herrenschmidt 		return 0;
218e8222502SBenjamin Herrenschmidt 	while (cplen > 0) {
219e8222502SBenjamin Herrenschmidt 		if (strncasecmp(cp, compat, strlen(compat)) == 0)
220e8222502SBenjamin Herrenschmidt 			return 1;
221e8222502SBenjamin Herrenschmidt 		l = strlen(cp) + 1;
222e8222502SBenjamin Herrenschmidt 		cp += l;
223e8222502SBenjamin Herrenschmidt 		cplen -= l;
224e8222502SBenjamin Herrenschmidt 	}
225e8222502SBenjamin Herrenschmidt 
226e8222502SBenjamin Herrenschmidt 	return 0;
227e8222502SBenjamin Herrenschmidt }
228e8222502SBenjamin Herrenschmidt 
2299b6b563cSPaul Mackerras static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
2309b6b563cSPaul Mackerras 				       unsigned long align)
2319b6b563cSPaul Mackerras {
2329b6b563cSPaul Mackerras 	void *res;
2339b6b563cSPaul Mackerras 
2349b6b563cSPaul Mackerras 	*mem = _ALIGN(*mem, align);
2359b6b563cSPaul Mackerras 	res = (void *)*mem;
2369b6b563cSPaul Mackerras 	*mem += size;
2379b6b563cSPaul Mackerras 
2389b6b563cSPaul Mackerras 	return res;
2399b6b563cSPaul Mackerras }
2409b6b563cSPaul Mackerras 
2419b6b563cSPaul Mackerras static unsigned long __init unflatten_dt_node(unsigned long mem,
2429b6b563cSPaul Mackerras 					      unsigned long *p,
2439b6b563cSPaul Mackerras 					      struct device_node *dad,
2449b6b563cSPaul Mackerras 					      struct device_node ***allnextpp,
2459b6b563cSPaul Mackerras 					      unsigned long fpsize)
2469b6b563cSPaul Mackerras {
2479b6b563cSPaul Mackerras 	struct device_node *np;
2489b6b563cSPaul Mackerras 	struct property *pp, **prev_pp = NULL;
2499b6b563cSPaul Mackerras 	char *pathp;
2509b6b563cSPaul Mackerras 	u32 tag;
2519b6b563cSPaul Mackerras 	unsigned int l, allocl;
2529b6b563cSPaul Mackerras 	int has_name = 0;
2539b6b563cSPaul Mackerras 	int new_format = 0;
2549b6b563cSPaul Mackerras 
2559b6b563cSPaul Mackerras 	tag = *((u32 *)(*p));
2569b6b563cSPaul Mackerras 	if (tag != OF_DT_BEGIN_NODE) {
2579b6b563cSPaul Mackerras 		printk("Weird tag at start of node: %x\n", tag);
2589b6b563cSPaul Mackerras 		return mem;
2599b6b563cSPaul Mackerras 	}
2609b6b563cSPaul Mackerras 	*p += 4;
2619b6b563cSPaul Mackerras 	pathp = (char *)*p;
2629b6b563cSPaul Mackerras 	l = allocl = strlen(pathp) + 1;
2639b6b563cSPaul Mackerras 	*p = _ALIGN(*p + l, 4);
2649b6b563cSPaul Mackerras 
2659b6b563cSPaul Mackerras 	/* version 0x10 has a more compact unit name here instead of the full
2669b6b563cSPaul Mackerras 	 * path. we accumulate the full path size using "fpsize", we'll rebuild
2679b6b563cSPaul Mackerras 	 * it later. We detect this because the first character of the name is
2689b6b563cSPaul Mackerras 	 * not '/'.
2699b6b563cSPaul Mackerras 	 */
2709b6b563cSPaul Mackerras 	if ((*pathp) != '/') {
2719b6b563cSPaul Mackerras 		new_format = 1;
2729b6b563cSPaul Mackerras 		if (fpsize == 0) {
2739b6b563cSPaul Mackerras 			/* root node: special case. fpsize accounts for path
2749b6b563cSPaul Mackerras 			 * plus terminating zero. root node only has '/', so
2759b6b563cSPaul Mackerras 			 * fpsize should be 2, but we want to avoid the first
2769b6b563cSPaul Mackerras 			 * level nodes to have two '/' so we use fpsize 1 here
2779b6b563cSPaul Mackerras 			 */
2789b6b563cSPaul Mackerras 			fpsize = 1;
2799b6b563cSPaul Mackerras 			allocl = 2;
2809b6b563cSPaul Mackerras 		} else {
2819b6b563cSPaul Mackerras 			/* account for '/' and path size minus terminal 0
2829b6b563cSPaul Mackerras 			 * already in 'l'
2839b6b563cSPaul Mackerras 			 */
2849b6b563cSPaul Mackerras 			fpsize += l;
2859b6b563cSPaul Mackerras 			allocl = fpsize;
2869b6b563cSPaul Mackerras 		}
2879b6b563cSPaul Mackerras 	}
2889b6b563cSPaul Mackerras 
2899b6b563cSPaul Mackerras 
2909b6b563cSPaul Mackerras 	np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
2919b6b563cSPaul Mackerras 				__alignof__(struct device_node));
2929b6b563cSPaul Mackerras 	if (allnextpp) {
2939b6b563cSPaul Mackerras 		memset(np, 0, sizeof(*np));
2949b6b563cSPaul Mackerras 		np->full_name = ((char*)np) + sizeof(struct device_node);
2959b6b563cSPaul Mackerras 		if (new_format) {
2969b6b563cSPaul Mackerras 			char *p = np->full_name;
2979b6b563cSPaul Mackerras 			/* rebuild full path for new format */
2989b6b563cSPaul Mackerras 			if (dad && dad->parent) {
2999b6b563cSPaul Mackerras 				strcpy(p, dad->full_name);
3009b6b563cSPaul Mackerras #ifdef DEBUG
3019b6b563cSPaul Mackerras 				if ((strlen(p) + l + 1) != allocl) {
3029b6b563cSPaul Mackerras 					DBG("%s: p: %d, l: %d, a: %d\n",
303e8222502SBenjamin Herrenschmidt 					    pathp, (int)strlen(p), l, allocl);
3049b6b563cSPaul Mackerras 				}
3059b6b563cSPaul Mackerras #endif
3069b6b563cSPaul Mackerras 				p += strlen(p);
3079b6b563cSPaul Mackerras 			}
3089b6b563cSPaul Mackerras 			*(p++) = '/';
3099b6b563cSPaul Mackerras 			memcpy(p, pathp, l);
3109b6b563cSPaul Mackerras 		} else
3119b6b563cSPaul Mackerras 			memcpy(np->full_name, pathp, l);
3129b6b563cSPaul Mackerras 		prev_pp = &np->properties;
3139b6b563cSPaul Mackerras 		**allnextpp = np;
3149b6b563cSPaul Mackerras 		*allnextpp = &np->allnext;
3159b6b563cSPaul Mackerras 		if (dad != NULL) {
3169b6b563cSPaul Mackerras 			np->parent = dad;
3179b6b563cSPaul Mackerras 			/* we temporarily use the next field as `last_child'*/
3189b6b563cSPaul Mackerras 			if (dad->next == 0)
3199b6b563cSPaul Mackerras 				dad->child = np;
3209b6b563cSPaul Mackerras 			else
3219b6b563cSPaul Mackerras 				dad->next->sibling = np;
3229b6b563cSPaul Mackerras 			dad->next = np;
3239b6b563cSPaul Mackerras 		}
3249b6b563cSPaul Mackerras 		kref_init(&np->kref);
3259b6b563cSPaul Mackerras 	}
3269b6b563cSPaul Mackerras 	while(1) {
3279b6b563cSPaul Mackerras 		u32 sz, noff;
3289b6b563cSPaul Mackerras 		char *pname;
3299b6b563cSPaul Mackerras 
3309b6b563cSPaul Mackerras 		tag = *((u32 *)(*p));
3319b6b563cSPaul Mackerras 		if (tag == OF_DT_NOP) {
3329b6b563cSPaul Mackerras 			*p += 4;
3339b6b563cSPaul Mackerras 			continue;
3349b6b563cSPaul Mackerras 		}
3359b6b563cSPaul Mackerras 		if (tag != OF_DT_PROP)
3369b6b563cSPaul Mackerras 			break;
3379b6b563cSPaul Mackerras 		*p += 4;
3389b6b563cSPaul Mackerras 		sz = *((u32 *)(*p));
3399b6b563cSPaul Mackerras 		noff = *((u32 *)((*p) + 4));
3409b6b563cSPaul Mackerras 		*p += 8;
3419b6b563cSPaul Mackerras 		if (initial_boot_params->version < 0x10)
3429b6b563cSPaul Mackerras 			*p = _ALIGN(*p, sz >= 8 ? 8 : 4);
3439b6b563cSPaul Mackerras 
3449b6b563cSPaul Mackerras 		pname = find_flat_dt_string(noff);
3459b6b563cSPaul Mackerras 		if (pname == NULL) {
3469b6b563cSPaul Mackerras 			printk("Can't find property name in list !\n");
3479b6b563cSPaul Mackerras 			break;
3489b6b563cSPaul Mackerras 		}
3499b6b563cSPaul Mackerras 		if (strcmp(pname, "name") == 0)
3509b6b563cSPaul Mackerras 			has_name = 1;
3519b6b563cSPaul Mackerras 		l = strlen(pname) + 1;
3529b6b563cSPaul Mackerras 		pp = unflatten_dt_alloc(&mem, sizeof(struct property),
3539b6b563cSPaul Mackerras 					__alignof__(struct property));
3549b6b563cSPaul Mackerras 		if (allnextpp) {
3559b6b563cSPaul Mackerras 			if (strcmp(pname, "linux,phandle") == 0) {
3569b6b563cSPaul Mackerras 				np->node = *((u32 *)*p);
3579b6b563cSPaul Mackerras 				if (np->linux_phandle == 0)
3589b6b563cSPaul Mackerras 					np->linux_phandle = np->node;
3599b6b563cSPaul Mackerras 			}
3609b6b563cSPaul Mackerras 			if (strcmp(pname, "ibm,phandle") == 0)
3619b6b563cSPaul Mackerras 				np->linux_phandle = *((u32 *)*p);
3629b6b563cSPaul Mackerras 			pp->name = pname;
3639b6b563cSPaul Mackerras 			pp->length = sz;
3649b6b563cSPaul Mackerras 			pp->value = (void *)*p;
3659b6b563cSPaul Mackerras 			*prev_pp = pp;
3669b6b563cSPaul Mackerras 			prev_pp = &pp->next;
3679b6b563cSPaul Mackerras 		}
3689b6b563cSPaul Mackerras 		*p = _ALIGN((*p) + sz, 4);
3699b6b563cSPaul Mackerras 	}
3709b6b563cSPaul Mackerras 	/* with version 0x10 we may not have the name property, recreate
3719b6b563cSPaul Mackerras 	 * it here from the unit name if absent
3729b6b563cSPaul Mackerras 	 */
3739b6b563cSPaul Mackerras 	if (!has_name) {
3749b6b563cSPaul Mackerras 		char *p = pathp, *ps = pathp, *pa = NULL;
3759b6b563cSPaul Mackerras 		int sz;
3769b6b563cSPaul Mackerras 
3779b6b563cSPaul Mackerras 		while (*p) {
3789b6b563cSPaul Mackerras 			if ((*p) == '@')
3799b6b563cSPaul Mackerras 				pa = p;
3809b6b563cSPaul Mackerras 			if ((*p) == '/')
3819b6b563cSPaul Mackerras 				ps = p + 1;
3829b6b563cSPaul Mackerras 			p++;
3839b6b563cSPaul Mackerras 		}
3849b6b563cSPaul Mackerras 		if (pa < ps)
3859b6b563cSPaul Mackerras 			pa = p;
3869b6b563cSPaul Mackerras 		sz = (pa - ps) + 1;
3879b6b563cSPaul Mackerras 		pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
3889b6b563cSPaul Mackerras 					__alignof__(struct property));
3899b6b563cSPaul Mackerras 		if (allnextpp) {
3909b6b563cSPaul Mackerras 			pp->name = "name";
3919b6b563cSPaul Mackerras 			pp->length = sz;
3921a38147eSStephen Rothwell 			pp->value = pp + 1;
3939b6b563cSPaul Mackerras 			*prev_pp = pp;
3949b6b563cSPaul Mackerras 			prev_pp = &pp->next;
3959b6b563cSPaul Mackerras 			memcpy(pp->value, ps, sz - 1);
3969b6b563cSPaul Mackerras 			((char *)pp->value)[sz - 1] = 0;
3971a38147eSStephen Rothwell 			DBG("fixed up name for %s -> %s\n", pathp,
3981a38147eSStephen Rothwell 				(char *)pp->value);
3999b6b563cSPaul Mackerras 		}
4009b6b563cSPaul Mackerras 	}
4019b6b563cSPaul Mackerras 	if (allnextpp) {
4029b6b563cSPaul Mackerras 		*prev_pp = NULL;
4030e56efc7SStephen Rothwell 		np->name = of_get_property(np, "name", NULL);
4040e56efc7SStephen Rothwell 		np->type = of_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));
429*49a84965SBecky Bruce 	DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
4302babf5c2SMichael Ellerman 
4312babf5c2SMichael Ellerman 	return 0;
4322babf5c2SMichael Ellerman }
4332babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
4342babf5c2SMichael Ellerman 
4353c607ce2SLinas Vepstas /**
4363c607ce2SLinas Vepstas  * move_device_tree - move tree to an unused area, if needed.
4373c607ce2SLinas Vepstas  *
4383c607ce2SLinas Vepstas  * The device tree may be allocated beyond our memory limit, or inside the
4393c607ce2SLinas Vepstas  * crash kernel region for kdump. If so, move it out of the way.
4402babf5c2SMichael Ellerman  */
44118f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
4422babf5c2SMichael Ellerman {
4432babf5c2SMichael Ellerman 	unsigned long start, size;
4442babf5c2SMichael Ellerman 	void *p;
4452babf5c2SMichael Ellerman 
4462babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
4472babf5c2SMichael Ellerman 
4482babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
4492babf5c2SMichael Ellerman 	size = initial_boot_params->totalsize;
4502babf5c2SMichael Ellerman 
4512babf5c2SMichael Ellerman 	if ((memory_limit && (start + size) > memory_limit) ||
4522babf5c2SMichael Ellerman 			overlaps_crashkernel(start, size)) {
4532babf5c2SMichael Ellerman 		p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
4542babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
4552babf5c2SMichael Ellerman 		initial_boot_params = (struct boot_param_header *)p;
4562babf5c2SMichael Ellerman 		DBG("Moved device tree to 0x%p\n", p);
4572babf5c2SMichael Ellerman 	}
4582babf5c2SMichael Ellerman 
4592babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
4602babf5c2SMichael Ellerman }
4619b6b563cSPaul Mackerras 
4629b6b563cSPaul Mackerras /**
4639b6b563cSPaul Mackerras  * unflattens the device-tree passed by the firmware, creating the
4649b6b563cSPaul Mackerras  * tree of struct device_node. It also fills the "name" and "type"
4659b6b563cSPaul Mackerras  * pointers of the nodes so the normal device-tree walking functions
4669b6b563cSPaul Mackerras  * can be used (this used to be done by finish_device_tree)
4679b6b563cSPaul Mackerras  */
4689b6b563cSPaul Mackerras void __init unflatten_device_tree(void)
4699b6b563cSPaul Mackerras {
4709b6b563cSPaul Mackerras 	unsigned long start, mem, size;
4719b6b563cSPaul Mackerras 	struct device_node **allnextp = &allnodes;
4729b6b563cSPaul Mackerras 
4739b6b563cSPaul Mackerras 	DBG(" -> unflatten_device_tree()\n");
4749b6b563cSPaul Mackerras 
4759b6b563cSPaul Mackerras 	/* First pass, scan for size */
4769b6b563cSPaul Mackerras 	start = ((unsigned long)initial_boot_params) +
4779b6b563cSPaul Mackerras 		initial_boot_params->off_dt_struct;
4789b6b563cSPaul Mackerras 	size = unflatten_dt_node(0, &start, NULL, NULL, 0);
4799b6b563cSPaul Mackerras 	size = (size | 3) + 1;
4809b6b563cSPaul Mackerras 
4819b6b563cSPaul Mackerras 	DBG("  size is %lx, allocating...\n", size);
4829b6b563cSPaul Mackerras 
4839b6b563cSPaul Mackerras 	/* Allocate memory for the expanded device tree */
4849b6b563cSPaul Mackerras 	mem = lmb_alloc(size + 4, __alignof__(struct device_node));
4859b6b563cSPaul Mackerras 	mem = (unsigned long) __va(mem);
4869b6b563cSPaul Mackerras 
4879b6b563cSPaul Mackerras 	((u32 *)mem)[size / 4] = 0xdeadbeef;
4889b6b563cSPaul Mackerras 
4899b6b563cSPaul Mackerras 	DBG("  unflattening %lx...\n", mem);
4909b6b563cSPaul Mackerras 
4919b6b563cSPaul Mackerras 	/* Second pass, do actual unflattening */
4929b6b563cSPaul Mackerras 	start = ((unsigned long)initial_boot_params) +
4939b6b563cSPaul Mackerras 		initial_boot_params->off_dt_struct;
4949b6b563cSPaul Mackerras 	unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
4959b6b563cSPaul Mackerras 	if (*((u32 *)start) != OF_DT_END)
4969b6b563cSPaul Mackerras 		printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
4979b6b563cSPaul Mackerras 	if (((u32 *)mem)[size / 4] != 0xdeadbeef)
4989b6b563cSPaul Mackerras 		printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
4999b6b563cSPaul Mackerras 		       ((u32 *)mem)[size / 4] );
5009b6b563cSPaul Mackerras 	*allnextp = NULL;
5019b6b563cSPaul Mackerras 
5029b6b563cSPaul Mackerras 	/* Get pointer to OF "/chosen" node for use everywhere */
5039b6b563cSPaul Mackerras 	of_chosen = of_find_node_by_path("/chosen");
504a575b807SPaul Mackerras 	if (of_chosen == NULL)
505a575b807SPaul Mackerras 		of_chosen = of_find_node_by_path("/chosen@0");
5069b6b563cSPaul Mackerras 
5079b6b563cSPaul Mackerras 	DBG(" <- unflatten_device_tree()\n");
5089b6b563cSPaul Mackerras }
5099b6b563cSPaul Mackerras 
510d205819eSPaul Mackerras /*
511d205819eSPaul Mackerras  * ibm,pa-features is a per-cpu property that contains a string of
512d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
513d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
514d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
515d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
516d205819eSPaul Mackerras  * zero is the only currently-defined value.
517d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
518d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
519d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
520d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
521d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
522d205819eSPaul Mackerras  */
523d205819eSPaul Mackerras static struct ibm_pa_feature {
524d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
525d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
526d205819eSPaul Mackerras 	unsigned char	pabyte;		/* byte number in ibm,pa-features */
527d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
528d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
529d205819eSPaul Mackerras } ibm_pa_features[] __initdata = {
530d205819eSPaul Mackerras 	{0, PPC_FEATURE_HAS_MMU,	0, 0, 0},
531d205819eSPaul Mackerras 	{0, PPC_FEATURE_HAS_FPU,	0, 1, 0},
532d205819eSPaul Mackerras 	{CPU_FTR_SLB, 0,		0, 2, 0},
533d205819eSPaul Mackerras 	{CPU_FTR_CTRL, 0,		0, 3, 0},
534d205819eSPaul Mackerras 	{CPU_FTR_NOEXECUTE, 0,		0, 6, 0},
535d205819eSPaul Mackerras 	{CPU_FTR_NODSISRALIGN, 0,	1, 1, 1},
536d205819eSPaul Mackerras 	{CPU_FTR_CI_LARGE_PAGE, 0,	1, 2, 0},
537339d76c5SPaul Mackerras 	{CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
538d205819eSPaul Mackerras };
539d205819eSPaul Mackerras 
540974a76f5SPaul Mackerras static void __init scan_features(unsigned long node, unsigned char *ftrs,
541974a76f5SPaul Mackerras 				 unsigned long tablelen,
542974a76f5SPaul Mackerras 				 struct ibm_pa_feature *fp,
543974a76f5SPaul Mackerras 				 unsigned long ft_size)
544d205819eSPaul Mackerras {
545974a76f5SPaul Mackerras 	unsigned long i, len, bit;
546d205819eSPaul Mackerras 
547d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
548d205819eSPaul Mackerras 	for (;;) {
549d205819eSPaul Mackerras 		if (tablelen < 3)
550d205819eSPaul Mackerras 			return;
551974a76f5SPaul Mackerras 		len = 2 + ftrs[0];
552d205819eSPaul Mackerras 		if (tablelen < len)
553d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
554974a76f5SPaul Mackerras 		if (ftrs[1] == 0)
555d205819eSPaul Mackerras 			break;
556d205819eSPaul Mackerras 		tablelen -= len;
557974a76f5SPaul Mackerras 		ftrs += len;
558d205819eSPaul Mackerras 	}
559d205819eSPaul Mackerras 
560d205819eSPaul Mackerras 	/* loop over bits we know about */
561974a76f5SPaul Mackerras 	for (i = 0; i < ft_size; ++i, ++fp) {
562974a76f5SPaul Mackerras 		if (fp->pabyte >= ftrs[0])
563d205819eSPaul Mackerras 			continue;
564974a76f5SPaul Mackerras 		bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
565d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
566d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
567d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
568d205819eSPaul Mackerras 		} else {
569d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
570d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
571d205819eSPaul Mackerras 		}
572d205819eSPaul Mackerras 	}
573d205819eSPaul Mackerras }
574d205819eSPaul Mackerras 
575974a76f5SPaul Mackerras static void __init check_cpu_pa_features(unsigned long node)
576974a76f5SPaul Mackerras {
577974a76f5SPaul Mackerras 	unsigned char *pa_ftrs;
578974a76f5SPaul Mackerras 	unsigned long tablelen;
579974a76f5SPaul Mackerras 
580974a76f5SPaul Mackerras 	pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
581974a76f5SPaul Mackerras 	if (pa_ftrs == NULL)
582974a76f5SPaul Mackerras 		return;
583974a76f5SPaul Mackerras 
584974a76f5SPaul Mackerras 	scan_features(node, pa_ftrs, tablelen,
585974a76f5SPaul Mackerras 		      ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
586974a76f5SPaul Mackerras }
587974a76f5SPaul Mackerras 
588584f8b71SMichael Neuling #ifdef CONFIG_PPC64
589584f8b71SMichael Neuling static void __init check_cpu_slb_size(unsigned long node)
590584f8b71SMichael Neuling {
591584f8b71SMichael Neuling 	u32 *slb_size_ptr;
592584f8b71SMichael Neuling 
593b60c31d8SMichael Neuling 	slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
594b60c31d8SMichael Neuling 	if (slb_size_ptr != NULL) {
595b60c31d8SMichael Neuling 		mmu_slb_size = *slb_size_ptr;
596b60c31d8SMichael Neuling 		return;
597b60c31d8SMichael Neuling 	}
598584f8b71SMichael Neuling 	slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
599584f8b71SMichael Neuling 	if (slb_size_ptr != NULL) {
600584f8b71SMichael Neuling 		mmu_slb_size = *slb_size_ptr;
601584f8b71SMichael Neuling 	}
602584f8b71SMichael Neuling }
603584f8b71SMichael Neuling #else
604584f8b71SMichael Neuling #define check_cpu_slb_size(node) do { } while(0)
605584f8b71SMichael Neuling #endif
606584f8b71SMichael Neuling 
607974a76f5SPaul Mackerras static struct feature_property {
608974a76f5SPaul Mackerras 	const char *name;
609974a76f5SPaul Mackerras 	u32 min_value;
610974a76f5SPaul Mackerras 	unsigned long cpu_feature;
611974a76f5SPaul Mackerras 	unsigned long cpu_user_ftr;
612974a76f5SPaul Mackerras } feature_properties[] __initdata = {
613974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC
614974a76f5SPaul Mackerras 	{"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
615974a76f5SPaul Mackerras 	{"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
616974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */
617b962ce9dSMichael Neuling #ifdef CONFIG_VSX
618b962ce9dSMichael Neuling 	/* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
619b962ce9dSMichael Neuling 	{"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
620b962ce9dSMichael Neuling #endif /* CONFIG_VSX */
621974a76f5SPaul Mackerras #ifdef CONFIG_PPC64
622974a76f5SPaul Mackerras 	{"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
623974a76f5SPaul Mackerras 	{"ibm,purr", 1, CPU_FTR_PURR, 0},
624974a76f5SPaul Mackerras 	{"ibm,spurr", 1, CPU_FTR_SPURR, 0},
625974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */
626974a76f5SPaul Mackerras };
627974a76f5SPaul Mackerras 
62814b3d926SValentine Barshak #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
62914b3d926SValentine Barshak static inline void identical_pvr_fixup(unsigned long node)
63014b3d926SValentine Barshak {
63114b3d926SValentine Barshak 	unsigned int pvr;
63214b3d926SValentine Barshak 	char *model = of_get_flat_dt_prop(node, "model", NULL);
63314b3d926SValentine Barshak 
63414b3d926SValentine Barshak 	/*
63514b3d926SValentine Barshak 	 * Since 440GR(x)/440EP(x) processors have the same pvr,
63614b3d926SValentine Barshak 	 * we check the node path and set bit 28 in the cur_cpu_spec
63714b3d926SValentine Barshak 	 * pvr for EP(x) processor version. This bit is always 0 in
63814b3d926SValentine Barshak 	 * the "real" pvr. Then we call identify_cpu again with
63914b3d926SValentine Barshak 	 * the new logical pvr to enable FPU support.
64014b3d926SValentine Barshak 	 */
64114b3d926SValentine Barshak 	if (model && strstr(model, "440EP")) {
64214b3d926SValentine Barshak 		pvr = cur_cpu_spec->pvr_value | 0x8;
64314b3d926SValentine Barshak 		identify_cpu(0, pvr);
64414b3d926SValentine Barshak 		DBG("Using logical pvr %x for %s\n", pvr, model);
64514b3d926SValentine Barshak 	}
64614b3d926SValentine Barshak }
64714b3d926SValentine Barshak #else
64814b3d926SValentine Barshak #define identical_pvr_fixup(node) do { } while(0)
64914b3d926SValentine Barshak #endif
65014b3d926SValentine Barshak 
651974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node)
652974a76f5SPaul Mackerras {
653974a76f5SPaul Mackerras 	unsigned long i;
654974a76f5SPaul Mackerras 	struct feature_property *fp = feature_properties;
655974a76f5SPaul Mackerras 	const u32 *prop;
656974a76f5SPaul Mackerras 
657974a76f5SPaul Mackerras 	for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
658974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, fp->name, NULL);
659974a76f5SPaul Mackerras 		if (prop && *prop >= fp->min_value) {
660974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_feature;
661974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
662974a76f5SPaul Mackerras 		}
663974a76f5SPaul Mackerras 	}
664974a76f5SPaul Mackerras }
665974a76f5SPaul Mackerras 
6669b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
6674df20460SAnton Blanchard 					  const char *uname, int depth,
6684df20460SAnton Blanchard 					  void *data)
6699b6b563cSPaul Mackerras {
6704df20460SAnton Blanchard 	static int logical_cpuid = 0;
6714df20460SAnton Blanchard 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
672974a76f5SPaul Mackerras 	const u32 *prop;
673974a76f5SPaul Mackerras 	const u32 *intserv;
6744df20460SAnton Blanchard 	int i, nthreads;
6754df20460SAnton Blanchard 	unsigned long len;
6764df20460SAnton Blanchard 	int found = 0;
6779b6b563cSPaul Mackerras 
6789b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
6799b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
6809b6b563cSPaul Mackerras 		return 0;
6819b6b563cSPaul Mackerras 
6824df20460SAnton Blanchard 	/* Get physical cpuid */
6834df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
6844df20460SAnton Blanchard 	if (intserv) {
6854df20460SAnton Blanchard 		nthreads = len / sizeof(int);
6869b6b563cSPaul Mackerras 	} else {
6874df20460SAnton Blanchard 		intserv = of_get_flat_dt_prop(node, "reg", NULL);
6884df20460SAnton Blanchard 		nthreads = 1;
6894df20460SAnton Blanchard 	}
6904df20460SAnton Blanchard 
6914df20460SAnton Blanchard 	/*
6924df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
6934df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
6944df20460SAnton Blanchard 	 */
6954df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
6964df20460SAnton Blanchard 		/*
6974df20460SAnton Blanchard 		 * version 2 of the kexec param format adds the phys cpuid of
6984df20460SAnton Blanchard 		 * booted proc.
6994df20460SAnton Blanchard 		 */
7004df20460SAnton Blanchard 		if (initial_boot_params && initial_boot_params->version >= 2) {
7014df20460SAnton Blanchard 			if (intserv[i] ==
7024df20460SAnton Blanchard 					initial_boot_params->boot_cpuid_phys) {
7034df20460SAnton Blanchard 				found = 1;
7044df20460SAnton Blanchard 				break;
7054df20460SAnton Blanchard 			}
7064df20460SAnton Blanchard 		} else {
7074df20460SAnton Blanchard 			/*
7084df20460SAnton Blanchard 			 * Check if it's the boot-cpu, set it's hw index now,
7094df20460SAnton Blanchard 			 * unfortunately this format did not support booting
7104df20460SAnton Blanchard 			 * off secondary threads.
7114df20460SAnton Blanchard 			 */
7123c726f8dSBenjamin Herrenschmidt 			if (of_get_flat_dt_prop(node,
7133c726f8dSBenjamin Herrenschmidt 					"linux,boot-cpu", NULL) != NULL) {
7144df20460SAnton Blanchard 				found = 1;
7154df20460SAnton Blanchard 				break;
7169b6b563cSPaul Mackerras 			}
7179b6b563cSPaul Mackerras 		}
7184df20460SAnton Blanchard 
7194df20460SAnton Blanchard #ifdef CONFIG_SMP
7204df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
7214df20460SAnton Blanchard 		logical_cpuid++;
7224df20460SAnton Blanchard #endif
7234df20460SAnton Blanchard 	}
7244df20460SAnton Blanchard 
7254df20460SAnton Blanchard 	if (found) {
7264df20460SAnton Blanchard 		DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
7274df20460SAnton Blanchard 			intserv[i]);
7284df20460SAnton Blanchard 		boot_cpuid = logical_cpuid;
7294df20460SAnton Blanchard 		set_hard_smp_processor_id(boot_cpuid, intserv[i]);
730974a76f5SPaul Mackerras 
731974a76f5SPaul Mackerras 		/*
732974a76f5SPaul Mackerras 		 * PAPR defines "logical" PVR values for cpus that
733974a76f5SPaul Mackerras 		 * meet various levels of the architecture:
734974a76f5SPaul Mackerras 		 * 0x0f000001	Architecture version 2.04
735974a76f5SPaul Mackerras 		 * 0x0f000002	Architecture version 2.05
736974a76f5SPaul Mackerras 		 * If the cpu-version property in the cpu node contains
737974a76f5SPaul Mackerras 		 * such a value, we call identify_cpu again with the
738974a76f5SPaul Mackerras 		 * logical PVR value in order to use the cpu feature
739974a76f5SPaul Mackerras 		 * bits appropriate for the architecture level.
740974a76f5SPaul Mackerras 		 *
741974a76f5SPaul Mackerras 		 * A POWER6 partition in "POWER6 architected" mode
742974a76f5SPaul Mackerras 		 * uses the 0x0f000002 PVR value; in POWER5+ mode
743974a76f5SPaul Mackerras 		 * it uses 0x0f000001.
744974a76f5SPaul Mackerras 		 */
745974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
746974a76f5SPaul Mackerras 		if (prop && (*prop & 0xff000000) == 0x0f000000)
747974a76f5SPaul Mackerras 			identify_cpu(0, *prop);
74814b3d926SValentine Barshak 
74914b3d926SValentine Barshak 		identical_pvr_fixup(node);
7504df20460SAnton Blanchard 	}
7519b6b563cSPaul Mackerras 
752974a76f5SPaul Mackerras 	check_cpu_feature_properties(node);
753d205819eSPaul Mackerras 	check_cpu_pa_features(node);
754584f8b71SMichael Neuling 	check_cpu_slb_size(node);
755d205819eSPaul Mackerras 
7569b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
7574df20460SAnton Blanchard 	if (nthreads > 1)
7589b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
7594df20460SAnton Blanchard 	else
7604df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
7619b6b563cSPaul Mackerras #endif
7629b6b563cSPaul Mackerras 
7639b6b563cSPaul Mackerras 	return 0;
7649b6b563cSPaul Mackerras }
7659b6b563cSPaul Mackerras 
76640472a55SMichael Ellerman #ifdef CONFIG_BLK_DEV_INITRD
76740472a55SMichael Ellerman static void __init early_init_dt_check_for_initrd(unsigned long node)
76840472a55SMichael Ellerman {
76940472a55SMichael Ellerman 	unsigned long l;
77040472a55SMichael Ellerman 	u32 *prop;
77140472a55SMichael Ellerman 
77240472a55SMichael Ellerman 	DBG("Looking for initrd properties... ");
77340472a55SMichael Ellerman 
77440472a55SMichael Ellerman 	prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
77540472a55SMichael Ellerman 	if (prop) {
77640472a55SMichael Ellerman 		initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
77740472a55SMichael Ellerman 
77840472a55SMichael Ellerman 		prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
77940472a55SMichael Ellerman 		if (prop) {
78040472a55SMichael Ellerman 			initrd_end = (unsigned long)
78140472a55SMichael Ellerman 					__va(of_read_ulong(prop, l/4));
78240472a55SMichael Ellerman 			initrd_below_start_ok = 1;
78340472a55SMichael Ellerman 		} else {
78440472a55SMichael Ellerman 			initrd_start = 0;
78540472a55SMichael Ellerman 		}
78640472a55SMichael Ellerman 	}
78740472a55SMichael Ellerman 
78840472a55SMichael Ellerman 	DBG("initrd_start=0x%lx  initrd_end=0x%lx\n", initrd_start, initrd_end);
78940472a55SMichael Ellerman }
79040472a55SMichael Ellerman #else
79140472a55SMichael Ellerman static inline void early_init_dt_check_for_initrd(unsigned long node)
79240472a55SMichael Ellerman {
79340472a55SMichael Ellerman }
79440472a55SMichael Ellerman #endif /* CONFIG_BLK_DEV_INITRD */
79540472a55SMichael Ellerman 
7969b6b563cSPaul Mackerras static int __init early_init_dt_scan_chosen(unsigned long node,
7979b6b563cSPaul Mackerras 					    const char *uname, int depth, void *data)
7989b6b563cSPaul Mackerras {
7999b6b563cSPaul Mackerras 	unsigned long *lprop;
800329dda08SKumar Gala 	unsigned long l;
801329dda08SKumar Gala 	char *p;
8029b6b563cSPaul Mackerras 
8039b6b563cSPaul Mackerras 	DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
8049b6b563cSPaul Mackerras 
805a575b807SPaul Mackerras 	if (depth != 1 ||
806a575b807SPaul Mackerras 	    (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
8079b6b563cSPaul Mackerras 		return 0;
8089b6b563cSPaul Mackerras 
8099b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
8109b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
8113c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
8129b6b563cSPaul Mackerras 		iommu_is_off = 1;
8133c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
8149b6b563cSPaul Mackerras 		iommu_force_on = 1;
8159b6b563cSPaul Mackerras #endif
8169b6b563cSPaul Mackerras 
8172babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
8183c726f8dSBenjamin Herrenschmidt  	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
8199b6b563cSPaul Mackerras  	if (lprop)
8209b6b563cSPaul Mackerras  		memory_limit = *lprop;
8219b6b563cSPaul Mackerras 
8229b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
8233c726f8dSBenjamin Herrenschmidt  	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
8249b6b563cSPaul Mackerras  	if (lprop)
8259b6b563cSPaul Mackerras  		tce_alloc_start = *lprop;
8263c726f8dSBenjamin Herrenschmidt  	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
8279b6b563cSPaul Mackerras  	if (lprop)
8289b6b563cSPaul Mackerras  		tce_alloc_end = *lprop;
8299b6b563cSPaul Mackerras #endif
8309b6b563cSPaul Mackerras 
831dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC
83263277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
833dcee3036SMichael Ellerman 	if (lprop)
834dcee3036SMichael Ellerman 		crashk_res.start = *lprop;
835dcee3036SMichael Ellerman 
83663277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
837dcee3036SMichael Ellerman 	if (lprop)
838dcee3036SMichael Ellerman 		crashk_res.end = crashk_res.start + *lprop - 1;
839dcee3036SMichael Ellerman #endif
840dcee3036SMichael Ellerman 
84140472a55SMichael Ellerman 	early_init_dt_check_for_initrd(node);
84230437b3eSDavid Gibson 
843329dda08SKumar Gala 	/* Retreive command line */
844329dda08SKumar Gala  	p = of_get_flat_dt_prop(node, "bootargs", &l);
845329dda08SKumar Gala 	if (p != NULL && l > 0)
846329dda08SKumar Gala 		strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
847329dda08SKumar Gala 
848329dda08SKumar Gala #ifdef CONFIG_CMDLINE
849c1ce464dSGeoff Levand 	if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
850329dda08SKumar Gala 		strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
851329dda08SKumar Gala #endif /* CONFIG_CMDLINE */
852329dda08SKumar Gala 
853329dda08SKumar Gala 	DBG("Command line is: %s\n", cmd_line);
854329dda08SKumar Gala 
8559b6b563cSPaul Mackerras 	/* break now */
8569b6b563cSPaul Mackerras 	return 1;
8579b6b563cSPaul Mackerras }
8589b6b563cSPaul Mackerras 
8599b6b563cSPaul Mackerras static int __init early_init_dt_scan_root(unsigned long node,
8609b6b563cSPaul Mackerras 					  const char *uname, int depth, void *data)
8619b6b563cSPaul Mackerras {
8629b6b563cSPaul Mackerras 	u32 *prop;
8639b6b563cSPaul Mackerras 
8649b6b563cSPaul Mackerras 	if (depth != 0)
8659b6b563cSPaul Mackerras 		return 0;
8669b6b563cSPaul Mackerras 
8673c726f8dSBenjamin Herrenschmidt 	prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
8689b6b563cSPaul Mackerras 	dt_root_size_cells = (prop == NULL) ? 1 : *prop;
8699b6b563cSPaul Mackerras 	DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
8709b6b563cSPaul Mackerras 
8713c726f8dSBenjamin Herrenschmidt 	prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
8729b6b563cSPaul Mackerras 	dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
8739b6b563cSPaul Mackerras 	DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
8749b6b563cSPaul Mackerras 
8759b6b563cSPaul Mackerras 	/* break now */
8769b6b563cSPaul Mackerras 	return 1;
8779b6b563cSPaul Mackerras }
8789b6b563cSPaul Mackerras 
879abe76885SBecky Bruce static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
8809b6b563cSPaul Mackerras {
8819b6b563cSPaul Mackerras 	cell_t *p = *cellp;
8829b6b563cSPaul Mackerras 
883a4dc7ff0SPaul Mackerras 	*cellp = p + s;
884abe76885SBecky Bruce 	return of_read_number(p, s);
8859b6b563cSPaul Mackerras }
8869b6b563cSPaul Mackerras 
8870204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
8880204568aSPaul Mackerras /*
8890204568aSPaul Mackerras  * Interpret the ibm,dynamic-memory property in the
8900204568aSPaul Mackerras  * /ibm,dynamic-reconfiguration-memory node.
8910204568aSPaul Mackerras  * This contains a list of memory blocks along with NUMA affinity
8920204568aSPaul Mackerras  * information.
8930204568aSPaul Mackerras  */
8940204568aSPaul Mackerras static int __init early_init_dt_scan_drconf_memory(unsigned long node)
8950204568aSPaul Mackerras {
896cf00085dSChandru 	cell_t *dm, *ls, *usm;
897abe76885SBecky Bruce 	unsigned long l, n, flags;
898abe76885SBecky Bruce 	u64 base, size, lmb_size;
899cf00085dSChandru 	unsigned int is_kexec_kdump = 0, rngs;
9000204568aSPaul Mackerras 
90163277161SStephen Rothwell 	ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
9020204568aSPaul Mackerras 	if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
9030204568aSPaul Mackerras 		return 0;
9040204568aSPaul Mackerras 	lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
9050204568aSPaul Mackerras 
90663277161SStephen Rothwell 	dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
9070204568aSPaul Mackerras 	if (dm == NULL || l < sizeof(cell_t))
9080204568aSPaul Mackerras 		return 0;
9090204568aSPaul Mackerras 
9100204568aSPaul Mackerras 	n = *dm++;	/* number of entries */
9110204568aSPaul Mackerras 	if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
9120204568aSPaul Mackerras 		return 0;
9130204568aSPaul Mackerras 
914cf00085dSChandru 	/* check if this is a kexec/kdump kernel. */
91563277161SStephen Rothwell 	usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
916cf00085dSChandru 						 &l);
917cf00085dSChandru 	if (usm != NULL)
918cf00085dSChandru 		is_kexec_kdump = 1;
919cf00085dSChandru 
9200204568aSPaul Mackerras 	for (; n != 0; --n) {
9210204568aSPaul Mackerras 		base = dt_mem_next_cell(dt_root_addr_cells, &dm);
9220204568aSPaul Mackerras 		flags = dm[3];
9230204568aSPaul Mackerras 		/* skip DRC index, pad, assoc. list index, flags */
9240204568aSPaul Mackerras 		dm += 4;
9250204568aSPaul Mackerras 		/* skip this block if the reserved bit is set in flags (0x80)
9260204568aSPaul Mackerras 		   or if the block is not assigned to this partition (0x8) */
9270204568aSPaul Mackerras 		if ((flags & 0x80) || !(flags & 0x8))
9280204568aSPaul Mackerras 			continue;
9290204568aSPaul Mackerras 		size = lmb_size;
930cf00085dSChandru 		rngs = 1;
931cf00085dSChandru 		if (is_kexec_kdump) {
932cf00085dSChandru 			/*
933cf00085dSChandru 			 * For each lmb in ibm,dynamic-memory, a corresponding
934cf00085dSChandru 			 * entry in linux,drconf-usable-memory property contains
935cf00085dSChandru 			 * a counter 'p' followed by 'p' (base, size) duple.
936cf00085dSChandru 			 * Now read the counter from
937cf00085dSChandru 			 * linux,drconf-usable-memory property
938cf00085dSChandru 			 */
939cf00085dSChandru 			rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
940cf00085dSChandru 			if (!rngs) /* there are no (base, size) duple */
941cf00085dSChandru 				continue;
942cf00085dSChandru 		}
943cf00085dSChandru 		do {
944cf00085dSChandru 			if (is_kexec_kdump) {
945cf00085dSChandru 				base = dt_mem_next_cell(dt_root_addr_cells,
946cf00085dSChandru 							 &usm);
947cf00085dSChandru 				size = dt_mem_next_cell(dt_root_size_cells,
948cf00085dSChandru 							 &usm);
949cf00085dSChandru 			}
9500204568aSPaul Mackerras 			if (iommu_is_off) {
9510204568aSPaul Mackerras 				if (base >= 0x80000000ul)
9520204568aSPaul Mackerras 					continue;
9530204568aSPaul Mackerras 				if ((base + size) > 0x80000000ul)
9540204568aSPaul Mackerras 					size = 0x80000000ul - base;
9550204568aSPaul Mackerras 			}
9560204568aSPaul Mackerras 			lmb_add(base, size);
957cf00085dSChandru 		} while (--rngs);
9580204568aSPaul Mackerras 	}
9590204568aSPaul Mackerras 	lmb_dump_all();
9600204568aSPaul Mackerras 	return 0;
9610204568aSPaul Mackerras }
9620204568aSPaul Mackerras #else
9630204568aSPaul Mackerras #define early_init_dt_scan_drconf_memory(node)	0
9640204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
9659b6b563cSPaul Mackerras 
9669b6b563cSPaul Mackerras static int __init early_init_dt_scan_memory(unsigned long node,
9679b6b563cSPaul Mackerras 					    const char *uname, int depth, void *data)
9689b6b563cSPaul Mackerras {
9693c726f8dSBenjamin Herrenschmidt 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
9709b6b563cSPaul Mackerras 	cell_t *reg, *endp;
9719b6b563cSPaul Mackerras 	unsigned long l;
9729b6b563cSPaul Mackerras 
9730204568aSPaul Mackerras 	/* Look for the ibm,dynamic-reconfiguration-memory node */
9740204568aSPaul Mackerras 	if (depth == 1 &&
9750204568aSPaul Mackerras 	    strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
9760204568aSPaul Mackerras 		return early_init_dt_scan_drconf_memory(node);
9770204568aSPaul Mackerras 
9789b6b563cSPaul Mackerras 	/* We are scanning "memory" nodes only */
979a23414beSPaul Mackerras 	if (type == NULL) {
980a23414beSPaul Mackerras 		/*
981a23414beSPaul Mackerras 		 * The longtrail doesn't have a device_type on the
982a23414beSPaul Mackerras 		 * /memory node, so look for the node called /memory@0.
983a23414beSPaul Mackerras 		 */
984a23414beSPaul Mackerras 		if (depth != 1 || strcmp(uname, "memory@0") != 0)
985a23414beSPaul Mackerras 			return 0;
986a23414beSPaul Mackerras 	} else if (strcmp(type, "memory") != 0)
9879b6b563cSPaul Mackerras 		return 0;
9889b6b563cSPaul Mackerras 
98963277161SStephen Rothwell 	reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
990ba759485SMichael Ellerman 	if (reg == NULL)
99163277161SStephen Rothwell 		reg = of_get_flat_dt_prop(node, "reg", &l);
9929b6b563cSPaul Mackerras 	if (reg == NULL)
9939b6b563cSPaul Mackerras 		return 0;
9949b6b563cSPaul Mackerras 
9959b6b563cSPaul Mackerras 	endp = reg + (l / sizeof(cell_t));
9969b6b563cSPaul Mackerras 
997358c86fdSMichael Ellerman 	DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
9989b6b563cSPaul Mackerras 	    uname, l, reg[0], reg[1], reg[2], reg[3]);
9999b6b563cSPaul Mackerras 
10009b6b563cSPaul Mackerras 	while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
1001abe76885SBecky Bruce 		u64 base, size;
10029b6b563cSPaul Mackerras 
10039b6b563cSPaul Mackerras 		base = dt_mem_next_cell(dt_root_addr_cells, &reg);
10049b6b563cSPaul Mackerras 		size = dt_mem_next_cell(dt_root_size_cells, &reg);
10059b6b563cSPaul Mackerras 
10069b6b563cSPaul Mackerras 		if (size == 0)
10079b6b563cSPaul Mackerras 			continue;
1008abe76885SBecky Bruce 		DBG(" - %llx ,  %llx\n", (unsigned long long)base,
1009abe76885SBecky Bruce 		    (unsigned long long)size);
10109b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
10119b6b563cSPaul Mackerras 		if (iommu_is_off) {
10129b6b563cSPaul Mackerras 			if (base >= 0x80000000ul)
10139b6b563cSPaul Mackerras 				continue;
10149b6b563cSPaul Mackerras 			if ((base + size) > 0x80000000ul)
10159b6b563cSPaul Mackerras 				size = 0x80000000ul - base;
10169b6b563cSPaul Mackerras 		}
10179b6b563cSPaul Mackerras #endif
10189b6b563cSPaul Mackerras 		lmb_add(base, size);
101937dd2badSKumar Gala 
102037dd2badSKumar Gala 		memstart_addr = min((u64)memstart_addr, base);
10219b6b563cSPaul Mackerras 	}
102237dd2badSKumar Gala 
10239b6b563cSPaul Mackerras 	return 0;
10249b6b563cSPaul Mackerras }
10259b6b563cSPaul Mackerras 
10269b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
10279b6b563cSPaul Mackerras {
1028cbbcf340SKumar Gala 	u64 base, size;
1029cbbcf340SKumar Gala 	u64 *reserve_map;
10308a300887SJon Loeliger 	unsigned long self_base;
10318a300887SJon Loeliger 	unsigned long self_size;
10329b6b563cSPaul Mackerras 
1033cbbcf340SKumar Gala 	reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
10349b6b563cSPaul Mackerras 					initial_boot_params->off_mem_rsvmap);
10354d1f3f25SJimi Xenidis 
10364d1f3f25SJimi Xenidis 	/* before we do anything, lets reserve the dt blob */
10378a300887SJon Loeliger 	self_base = __pa((unsigned long)initial_boot_params);
10388a300887SJon Loeliger 	self_size = initial_boot_params->totalsize;
10398a300887SJon Loeliger 	lmb_reserve(self_base, self_size);
10404d1f3f25SJimi Xenidis 
104130437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
104230437b3eSDavid Gibson 	/* then reserve the initrd, if any */
104330437b3eSDavid Gibson 	if (initrd_start && (initrd_end > initrd_start))
104430437b3eSDavid Gibson 		lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
104530437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
104630437b3eSDavid Gibson 
1047cbbcf340SKumar Gala #ifdef CONFIG_PPC32
1048cbbcf340SKumar Gala 	/*
1049cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
1050cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
1051cbbcf340SKumar Gala 	 */
1052cbbcf340SKumar Gala 	if (*reserve_map > 0xffffffffull) {
1053cbbcf340SKumar Gala 		u32 base_32, size_32;
1054cbbcf340SKumar Gala 		u32 *reserve_map_32 = (u32 *)reserve_map;
1055cbbcf340SKumar Gala 
1056cbbcf340SKumar Gala 		while (1) {
1057cbbcf340SKumar Gala 			base_32 = *(reserve_map_32++);
1058cbbcf340SKumar Gala 			size_32 = *(reserve_map_32++);
1059cbbcf340SKumar Gala 			if (size_32 == 0)
1060cbbcf340SKumar Gala 				break;
10618a300887SJon Loeliger 			/* skip if the reservation is for the blob */
10628a300887SJon Loeliger 			if (base_32 == self_base && size_32 == self_size)
10638a300887SJon Loeliger 				continue;
1064329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
1065cbbcf340SKumar Gala 			lmb_reserve(base_32, size_32);
1066cbbcf340SKumar Gala 		}
1067cbbcf340SKumar Gala 		return;
1068cbbcf340SKumar Gala 	}
1069cbbcf340SKumar Gala #endif
10709b6b563cSPaul Mackerras 	while (1) {
10719b6b563cSPaul Mackerras 		base = *(reserve_map++);
10729b6b563cSPaul Mackerras 		size = *(reserve_map++);
10739b6b563cSPaul Mackerras 		if (size == 0)
10749b6b563cSPaul Mackerras 			break;
1075cbbcf340SKumar Gala 		DBG("reserving: %llx -> %llx\n", base, size);
10769b6b563cSPaul Mackerras 		lmb_reserve(base, size);
10779b6b563cSPaul Mackerras 	}
10789b6b563cSPaul Mackerras }
10799b6b563cSPaul Mackerras 
10806ac26c8aSManish Ahuja #ifdef CONFIG_PHYP_DUMP
10816ac26c8aSManish Ahuja /**
108237ddd5d0SManish Ahuja  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
108337ddd5d0SManish Ahuja  *
108437ddd5d0SManish Ahuja  * Function to find the largest size we need to reserve
108537ddd5d0SManish Ahuja  * during early boot process.
108637ddd5d0SManish Ahuja  *
108737ddd5d0SManish Ahuja  * It either looks for boot param and returns that OR
108837ddd5d0SManish Ahuja  * returns larger of 256 or 5% rounded down to multiples of 256MB.
108937ddd5d0SManish Ahuja  *
109037ddd5d0SManish Ahuja  */
109137ddd5d0SManish Ahuja static inline unsigned long phyp_dump_calculate_reserve_size(void)
109237ddd5d0SManish Ahuja {
109337ddd5d0SManish Ahuja 	unsigned long tmp;
109437ddd5d0SManish Ahuja 
109537ddd5d0SManish Ahuja 	if (phyp_dump_info->reserve_bootvar)
109637ddd5d0SManish Ahuja 		return phyp_dump_info->reserve_bootvar;
109737ddd5d0SManish Ahuja 
109837ddd5d0SManish Ahuja 	/* divide by 20 to get 5% of value */
109937ddd5d0SManish Ahuja 	tmp = lmb_end_of_DRAM();
110037ddd5d0SManish Ahuja 	do_div(tmp, 20);
110137ddd5d0SManish Ahuja 
110237ddd5d0SManish Ahuja 	/* round it down in multiples of 256 */
110337ddd5d0SManish Ahuja 	tmp = tmp & ~0x0FFFFFFFUL;
110437ddd5d0SManish Ahuja 
110537ddd5d0SManish Ahuja 	return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
110637ddd5d0SManish Ahuja }
110737ddd5d0SManish Ahuja 
110837ddd5d0SManish Ahuja /**
11096ac26c8aSManish Ahuja  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
11106ac26c8aSManish Ahuja  *
11116ac26c8aSManish Ahuja  * This routine may reserve memory regions in the kernel only
11126ac26c8aSManish Ahuja  * if the system is supported and a dump was taken in last
11136ac26c8aSManish Ahuja  * boot instance or if the hardware is supported and the
11146ac26c8aSManish Ahuja  * scratch area needs to be setup. In other instances it returns
11156ac26c8aSManish Ahuja  * without reserving anything. The memory in case of dump being
11166ac26c8aSManish Ahuja  * active is freed when the dump is collected (by userland tools).
11176ac26c8aSManish Ahuja  */
11186ac26c8aSManish Ahuja static void __init phyp_dump_reserve_mem(void)
11196ac26c8aSManish Ahuja {
11206ac26c8aSManish Ahuja 	unsigned long base, size;
112137ddd5d0SManish Ahuja 	unsigned long variable_reserve_size;
112237ddd5d0SManish Ahuja 
11236ac26c8aSManish Ahuja 	if (!phyp_dump_info->phyp_dump_configured) {
11246ac26c8aSManish Ahuja 		printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
11256ac26c8aSManish Ahuja 		return;
11266ac26c8aSManish Ahuja 	}
11276ac26c8aSManish Ahuja 
1128654f596dSManish Ahuja 	if (!phyp_dump_info->phyp_dump_at_boot) {
1129654f596dSManish Ahuja 		printk(KERN_INFO "Phyp-dump disabled at boot time\n");
1130654f596dSManish Ahuja 		return;
1131654f596dSManish Ahuja 	}
1132654f596dSManish Ahuja 
113337ddd5d0SManish Ahuja 	variable_reserve_size = phyp_dump_calculate_reserve_size();
113437ddd5d0SManish Ahuja 
11356ac26c8aSManish Ahuja 	if (phyp_dump_info->phyp_dump_is_active) {
11366ac26c8aSManish Ahuja 		/* Reserve *everything* above RMR.Area freed by userland tools*/
113737ddd5d0SManish Ahuja 		base = variable_reserve_size;
11386ac26c8aSManish Ahuja 		size = lmb_end_of_DRAM() - base;
11396ac26c8aSManish Ahuja 
11406ac26c8aSManish Ahuja 		/* XXX crashed_ram_end is wrong, since it may be beyond
11416ac26c8aSManish Ahuja 		 * the memory_limit, it will need to be adjusted. */
11426ac26c8aSManish Ahuja 		lmb_reserve(base, size);
11436ac26c8aSManish Ahuja 
11446ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_start = base;
11456ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_size = size;
11466ac26c8aSManish Ahuja 	} else {
11476ac26c8aSManish Ahuja 		size = phyp_dump_info->cpu_state_size +
11486ac26c8aSManish Ahuja 			phyp_dump_info->hpte_region_size +
114937ddd5d0SManish Ahuja 			variable_reserve_size;
11506ac26c8aSManish Ahuja 		base = lmb_end_of_DRAM() - size;
11516ac26c8aSManish Ahuja 		lmb_reserve(base, size);
11526ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_start = base;
11536ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_size = size;
11546ac26c8aSManish Ahuja 	}
11556ac26c8aSManish Ahuja }
11566ac26c8aSManish Ahuja #else
11576ac26c8aSManish Ahuja static inline void __init phyp_dump_reserve_mem(void) {}
11586ac26c8aSManish Ahuja #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
11596ac26c8aSManish Ahuja 
11606ac26c8aSManish Ahuja 
11619b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
11629b6b563cSPaul Mackerras {
1163*49a84965SBecky Bruce 	phys_addr_t limit;
11646ca4f749SHollis Blanchard 
116544348105SGeoff Levand 	DBG(" -> early_init_devtree(%p)\n", params);
11669b6b563cSPaul Mackerras 
11679b6b563cSPaul Mackerras 	/* Setup flat device-tree pointer */
11689b6b563cSPaul Mackerras 	initial_boot_params = params;
11699b6b563cSPaul Mackerras 
1170458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
1171458148c0SMichael Ellerman 	/* Some machines might need RTAS info for debugging, grab it now. */
1172458148c0SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
1173458148c0SMichael Ellerman #endif
1174458148c0SMichael Ellerman 
11756ac26c8aSManish Ahuja #ifdef CONFIG_PHYP_DUMP
11766ac26c8aSManish Ahuja 	/* scan tree to see if dump occured during last boot */
11776ac26c8aSManish Ahuja 	of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
11786ac26c8aSManish Ahuja #endif
11796ac26c8aSManish Ahuja 
11809b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
11819b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
11829b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
11839b6b563cSPaul Mackerras 	 */
11843c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
11859b6b563cSPaul Mackerras 
11869b6b563cSPaul Mackerras 	/* Scan memory nodes and rebuild LMBs */
11879b6b563cSPaul Mackerras 	lmb_init();
11883c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
11893c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1190846f77b0SMichael Ellerman 
1191846f77b0SMichael Ellerman 	/* Save command line for /proc/cmdline and then parse parameters */
1192b8757b21SAlon Bar-Lev 	strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
1193846f77b0SMichael Ellerman 	parse_early_param();
1194846f77b0SMichael Ellerman 
11959b6b563cSPaul Mackerras 	/* Reserve LMB regions used by kernel, initrd, dt, etc... */
11960cc4746cSMichael Ellerman 	lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
1197549e8152SPaul Mackerras 	/* If relocatable, reserve first 32k for interrupt vectors etc. */
1198549e8152SPaul Mackerras 	if (PHYSICAL_START > MEMORY_START)
1199549e8152SPaul Mackerras 		lmb_reserve(MEMORY_START, 0x8000);
120047310413SMichael Ellerman 	reserve_kdump_trampoline();
120135dd5432SMichael Ellerman 	reserve_crashkernel();
12029b6b563cSPaul Mackerras 	early_reserve_mem();
12036ac26c8aSManish Ahuja 	phyp_dump_reserve_mem();
12049b6b563cSPaul Mackerras 
12056ca4f749SHollis Blanchard 	limit = memory_limit;
12066ca4f749SHollis Blanchard 	if (! limit) {
1207*49a84965SBecky Bruce 		phys_addr_t memsize;
12086ca4f749SHollis Blanchard 
12096ca4f749SHollis Blanchard 		/* Ensure that total memory size is page-aligned, because
12106ca4f749SHollis Blanchard 		 * otherwise mark_bootmem() gets upset. */
12116ca4f749SHollis Blanchard 		lmb_analyze();
12126ca4f749SHollis Blanchard 		memsize = lmb_phys_mem_size();
12136ca4f749SHollis Blanchard 		if ((memsize & PAGE_MASK) != memsize)
12146ca4f749SHollis Blanchard 			limit = memsize & PAGE_MASK;
12156ca4f749SHollis Blanchard 	}
12166ca4f749SHollis Blanchard 	lmb_enforce_memory_limit(limit);
12176ca4f749SHollis Blanchard 
12182babf5c2SMichael Ellerman 	lmb_analyze();
1219059f134fSMichael Ellerman 	lmb_dump_all();
12202babf5c2SMichael Ellerman 
1221*49a84965SBecky Bruce 	DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
12222babf5c2SMichael Ellerman 
12232babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
12242babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
12252babf5c2SMichael Ellerman 	move_device_tree();
12262babf5c2SMichael Ellerman 
12279b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
12289b6b563cSPaul Mackerras 
12293c726f8dSBenjamin Herrenschmidt 	/* Retreive CPU related informations from the flat tree
12303c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
12319b6b563cSPaul Mackerras 	 */
12323c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
12339b6b563cSPaul Mackerras 
12349b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
12359b6b563cSPaul Mackerras }
12369b6b563cSPaul Mackerras 
12379b6b563cSPaul Mackerras 
12389b6b563cSPaul Mackerras /**
12399b6b563cSPaul Mackerras  * Indicates whether the root node has a given value in its
12409b6b563cSPaul Mackerras  * compatible property.
12419b6b563cSPaul Mackerras  */
12429b6b563cSPaul Mackerras int machine_is_compatible(const char *compat)
12439b6b563cSPaul Mackerras {
12449b6b563cSPaul Mackerras 	struct device_node *root;
12459b6b563cSPaul Mackerras 	int rc = 0;
12469b6b563cSPaul Mackerras 
12479b6b563cSPaul Mackerras 	root = of_find_node_by_path("/");
12489b6b563cSPaul Mackerras 	if (root) {
12497a92f74fSStephen Rothwell 		rc = of_device_is_compatible(root, compat);
12509b6b563cSPaul Mackerras 		of_node_put(root);
12519b6b563cSPaul Mackerras 	}
12529b6b563cSPaul Mackerras 	return rc;
12539b6b563cSPaul Mackerras }
12549b6b563cSPaul Mackerras EXPORT_SYMBOL(machine_is_compatible);
12559b6b563cSPaul Mackerras 
12569b6b563cSPaul Mackerras /*******
12579b6b563cSPaul Mackerras  *
12589b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
12599b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
12609b6b563cSPaul Mackerras  * are protected by a rw_lock.
12619b6b563cSPaul Mackerras  *
12629b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
12639b6b563cSPaul Mackerras  * this isn't dealt with yet.
12649b6b563cSPaul Mackerras  *
12659b6b563cSPaul Mackerras  *******/
12669b6b563cSPaul Mackerras 
12679b6b563cSPaul Mackerras /**
12689b6b563cSPaul Mackerras  *	of_find_node_by_phandle - Find a node given a phandle
12699b6b563cSPaul Mackerras  *	@handle:	phandle of the node to find
12709b6b563cSPaul Mackerras  *
12719b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
12729b6b563cSPaul Mackerras  *	of_node_put() on it when done.
12739b6b563cSPaul Mackerras  */
12749b6b563cSPaul Mackerras struct device_node *of_find_node_by_phandle(phandle handle)
12759b6b563cSPaul Mackerras {
12769b6b563cSPaul Mackerras 	struct device_node *np;
12779b6b563cSPaul Mackerras 
12789b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
12799b6b563cSPaul Mackerras 	for (np = allnodes; np != 0; np = np->allnext)
12809b6b563cSPaul Mackerras 		if (np->linux_phandle == handle)
12819b6b563cSPaul Mackerras 			break;
12829b6b563cSPaul Mackerras 	of_node_get(np);
12839b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
12849b6b563cSPaul Mackerras 	return np;
12859b6b563cSPaul Mackerras }
12869b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_node_by_phandle);
12879b6b563cSPaul Mackerras 
12889b6b563cSPaul Mackerras /**
1289e523f723SNathan Lynch  *	of_find_next_cache_node - Find a node's subsidiary cache
1290e523f723SNathan Lynch  *	@np:	node of type "cpu" or "cache"
1291e523f723SNathan Lynch  *
1292e523f723SNathan Lynch  *	Returns a node pointer with refcount incremented, use
1293e523f723SNathan Lynch  *	of_node_put() on it when done.  Caller should hold a reference
1294e523f723SNathan Lynch  *	to np.
1295e523f723SNathan Lynch  */
1296e523f723SNathan Lynch struct device_node *of_find_next_cache_node(struct device_node *np)
1297e523f723SNathan Lynch {
1298e523f723SNathan Lynch 	struct device_node *child;
1299e523f723SNathan Lynch 	const phandle *handle;
1300e523f723SNathan Lynch 
1301e523f723SNathan Lynch 	handle = of_get_property(np, "l2-cache", NULL);
1302e523f723SNathan Lynch 	if (!handle)
1303e523f723SNathan Lynch 		handle = of_get_property(np, "next-level-cache", NULL);
1304e523f723SNathan Lynch 
1305e523f723SNathan Lynch 	if (handle)
1306e523f723SNathan Lynch 		return of_find_node_by_phandle(*handle);
1307e523f723SNathan Lynch 
1308e523f723SNathan Lynch 	/* OF on pmac has nodes instead of properties named "l2-cache"
1309e523f723SNathan Lynch 	 * beneath CPU nodes.
1310e523f723SNathan Lynch 	 */
1311e523f723SNathan Lynch 	if (!strcmp(np->type, "cpu"))
1312e523f723SNathan Lynch 		for_each_child_of_node(np, child)
1313e523f723SNathan Lynch 			if (!strcmp(child->type, "cache"))
1314e523f723SNathan Lynch 				return child;
1315e523f723SNathan Lynch 
1316e523f723SNathan Lynch 	return NULL;
1317e523f723SNathan Lynch }
1318e523f723SNathan Lynch 
1319e523f723SNathan Lynch /**
13209b6b563cSPaul Mackerras  *	of_find_all_nodes - Get next node in global list
13219b6b563cSPaul Mackerras  *	@prev:	Previous node or NULL to start iteration
13229b6b563cSPaul Mackerras  *		of_node_put() will be called on it
13239b6b563cSPaul Mackerras  *
13249b6b563cSPaul Mackerras  *	Returns a node pointer with refcount incremented, use
13259b6b563cSPaul Mackerras  *	of_node_put() on it when done.
13269b6b563cSPaul Mackerras  */
13279b6b563cSPaul Mackerras struct device_node *of_find_all_nodes(struct device_node *prev)
13289b6b563cSPaul Mackerras {
13299b6b563cSPaul Mackerras 	struct device_node *np;
13309b6b563cSPaul Mackerras 
13319b6b563cSPaul Mackerras 	read_lock(&devtree_lock);
13329b6b563cSPaul Mackerras 	np = prev ? prev->allnext : allnodes;
13339b6b563cSPaul Mackerras 	for (; np != 0; np = np->allnext)
13349b6b563cSPaul Mackerras 		if (of_node_get(np))
13359b6b563cSPaul Mackerras 			break;
13369b6b563cSPaul Mackerras 	of_node_put(prev);
13379b6b563cSPaul Mackerras 	read_unlock(&devtree_lock);
13389b6b563cSPaul Mackerras 	return np;
13399b6b563cSPaul Mackerras }
13409b6b563cSPaul Mackerras EXPORT_SYMBOL(of_find_all_nodes);
13419b6b563cSPaul Mackerras 
13429b6b563cSPaul Mackerras /**
13439b6b563cSPaul Mackerras  *	of_node_get - Increment refcount of a node
13449b6b563cSPaul Mackerras  *	@node:	Node to inc refcount, NULL is supported to
13459b6b563cSPaul Mackerras  *		simplify writing of callers
13469b6b563cSPaul Mackerras  *
13479b6b563cSPaul Mackerras  *	Returns node.
13489b6b563cSPaul Mackerras  */
13499b6b563cSPaul Mackerras struct device_node *of_node_get(struct device_node *node)
13509b6b563cSPaul Mackerras {
13519b6b563cSPaul Mackerras 	if (node)
13529b6b563cSPaul Mackerras 		kref_get(&node->kref);
13539b6b563cSPaul Mackerras 	return node;
13549b6b563cSPaul Mackerras }
13559b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_get);
13569b6b563cSPaul Mackerras 
13579b6b563cSPaul Mackerras static inline struct device_node * kref_to_device_node(struct kref *kref)
13589b6b563cSPaul Mackerras {
13599b6b563cSPaul Mackerras 	return container_of(kref, struct device_node, kref);
13609b6b563cSPaul Mackerras }
13619b6b563cSPaul Mackerras 
13629b6b563cSPaul Mackerras /**
13639b6b563cSPaul Mackerras  *	of_node_release - release a dynamically allocated node
13649b6b563cSPaul Mackerras  *	@kref:  kref element of the node to be released
13659b6b563cSPaul Mackerras  *
13669b6b563cSPaul Mackerras  *	In of_node_put() this function is passed to kref_put()
13679b6b563cSPaul Mackerras  *	as the destructor.
13689b6b563cSPaul Mackerras  */
13699b6b563cSPaul Mackerras static void of_node_release(struct kref *kref)
13709b6b563cSPaul Mackerras {
13719b6b563cSPaul Mackerras 	struct device_node *node = kref_to_device_node(kref);
13729b6b563cSPaul Mackerras 	struct property *prop = node->properties;
13739b6b563cSPaul Mackerras 
13746a281856SMichael Ellerman 	/* We should never be releasing nodes that haven't been detached. */
13756a281856SMichael Ellerman 	if (!of_node_check_flag(node, OF_DETACHED)) {
13766a281856SMichael Ellerman 		printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
13776a281856SMichael Ellerman 		dump_stack();
13786a281856SMichael Ellerman 		kref_init(&node->kref);
13796a281856SMichael Ellerman 		return;
13806a281856SMichael Ellerman 	}
13816a281856SMichael Ellerman 
1382d3b814bbSMichael Ellerman 	if (!of_node_check_flag(node, OF_DYNAMIC))
13839b6b563cSPaul Mackerras 		return;
13846a281856SMichael Ellerman 
13859b6b563cSPaul Mackerras 	while (prop) {
13869b6b563cSPaul Mackerras 		struct property *next = prop->next;
13879b6b563cSPaul Mackerras 		kfree(prop->name);
13889b6b563cSPaul Mackerras 		kfree(prop->value);
13899b6b563cSPaul Mackerras 		kfree(prop);
13909b6b563cSPaul Mackerras 		prop = next;
1391088186deSDave C Boutcher 
1392088186deSDave C Boutcher 		if (!prop) {
1393088186deSDave C Boutcher 			prop = node->deadprops;
1394088186deSDave C Boutcher 			node->deadprops = NULL;
1395088186deSDave C Boutcher 		}
13969b6b563cSPaul Mackerras 	}
13979b6b563cSPaul Mackerras 	kfree(node->full_name);
13989b6b563cSPaul Mackerras 	kfree(node->data);
13999b6b563cSPaul Mackerras 	kfree(node);
14009b6b563cSPaul Mackerras }
14019b6b563cSPaul Mackerras 
14029b6b563cSPaul Mackerras /**
14039b6b563cSPaul Mackerras  *	of_node_put - Decrement refcount of a node
14049b6b563cSPaul Mackerras  *	@node:	Node to dec refcount, NULL is supported to
14059b6b563cSPaul Mackerras  *		simplify writing of callers
14069b6b563cSPaul Mackerras  *
14079b6b563cSPaul Mackerras  */
14089b6b563cSPaul Mackerras void of_node_put(struct device_node *node)
14099b6b563cSPaul Mackerras {
14109b6b563cSPaul Mackerras 	if (node)
14119b6b563cSPaul Mackerras 		kref_put(&node->kref, of_node_release);
14129b6b563cSPaul Mackerras }
14139b6b563cSPaul Mackerras EXPORT_SYMBOL(of_node_put);
14149b6b563cSPaul Mackerras 
14159b6b563cSPaul Mackerras /*
14169b6b563cSPaul Mackerras  * Plug a device node into the tree and global list.
14179b6b563cSPaul Mackerras  */
14189b6b563cSPaul Mackerras void of_attach_node(struct device_node *np)
14199b6b563cSPaul Mackerras {
1420f4ac7b5eSBenjamin Herrenschmidt 	unsigned long flags;
1421f4ac7b5eSBenjamin Herrenschmidt 
1422f4ac7b5eSBenjamin Herrenschmidt 	write_lock_irqsave(&devtree_lock, flags);
14239b6b563cSPaul Mackerras 	np->sibling = np->parent->child;
14249b6b563cSPaul Mackerras 	np->allnext = allnodes;
14259b6b563cSPaul Mackerras 	np->parent->child = np;
14269b6b563cSPaul Mackerras 	allnodes = np;
1427f4ac7b5eSBenjamin Herrenschmidt 	write_unlock_irqrestore(&devtree_lock, flags);
14289b6b563cSPaul Mackerras }
14299b6b563cSPaul Mackerras 
14309b6b563cSPaul Mackerras /*
14319b6b563cSPaul Mackerras  * "Unplug" a node from the device tree.  The caller must hold
14329b6b563cSPaul Mackerras  * a reference to the node.  The memory associated with the node
14339b6b563cSPaul Mackerras  * is not freed until its refcount goes to zero.
14349b6b563cSPaul Mackerras  */
143534f329dbSSegher Boessenkool void of_detach_node(struct device_node *np)
14369b6b563cSPaul Mackerras {
14379b6b563cSPaul Mackerras 	struct device_node *parent;
1438f4ac7b5eSBenjamin Herrenschmidt 	unsigned long flags;
14399b6b563cSPaul Mackerras 
1440f4ac7b5eSBenjamin Herrenschmidt 	write_lock_irqsave(&devtree_lock, flags);
14419b6b563cSPaul Mackerras 
14429b6b563cSPaul Mackerras 	parent = np->parent;
1443972d17c9SMichael Ellerman 	if (!parent)
1444972d17c9SMichael Ellerman 		goto out_unlock;
14459b6b563cSPaul Mackerras 
14469b6b563cSPaul Mackerras 	if (allnodes == np)
14479b6b563cSPaul Mackerras 		allnodes = np->allnext;
14489b6b563cSPaul Mackerras 	else {
14499b6b563cSPaul Mackerras 		struct device_node *prev;
14509b6b563cSPaul Mackerras 		for (prev = allnodes;
14519b6b563cSPaul Mackerras 		     prev->allnext != np;
14529b6b563cSPaul Mackerras 		     prev = prev->allnext)
14539b6b563cSPaul Mackerras 			;
14549b6b563cSPaul Mackerras 		prev->allnext = np->allnext;
14559b6b563cSPaul Mackerras 	}
14569b6b563cSPaul Mackerras 
14579b6b563cSPaul Mackerras 	if (parent->child == np)
14589b6b563cSPaul Mackerras 		parent->child = np->sibling;
14599b6b563cSPaul Mackerras 	else {
14609b6b563cSPaul Mackerras 		struct device_node *prevsib;
14619b6b563cSPaul Mackerras 		for (prevsib = np->parent->child;
14629b6b563cSPaul Mackerras 		     prevsib->sibling != np;
14639b6b563cSPaul Mackerras 		     prevsib = prevsib->sibling)
14649b6b563cSPaul Mackerras 			;
14659b6b563cSPaul Mackerras 		prevsib->sibling = np->sibling;
14669b6b563cSPaul Mackerras 	}
14679b6b563cSPaul Mackerras 
14686a281856SMichael Ellerman 	of_node_set_flag(np, OF_DETACHED);
14696a281856SMichael Ellerman 
1470972d17c9SMichael Ellerman out_unlock:
1471f4ac7b5eSBenjamin Herrenschmidt 	write_unlock_irqrestore(&devtree_lock, flags);
14729b6b563cSPaul Mackerras }
14739b6b563cSPaul Mackerras 
14749b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
14759b6b563cSPaul Mackerras /*
14769b6b563cSPaul Mackerras  * Fix up the uninitialized fields in a new device node:
14770ebfff14SBenjamin Herrenschmidt  * name, type and pci-specific fields
14789b6b563cSPaul Mackerras  */
14799b6b563cSPaul Mackerras 
1480cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node)
14819b6b563cSPaul Mackerras {
14829b6b563cSPaul Mackerras 	struct device_node *parent = of_get_parent(node);
14839b6b563cSPaul Mackerras 	int err = 0;
1484a7f67bdfSJeremy Kerr 	const phandle *ibm_phandle;
14859b6b563cSPaul Mackerras 
14860e56efc7SStephen Rothwell 	node->name = of_get_property(node, "name", NULL);
14870e56efc7SStephen Rothwell 	node->type = of_get_property(node, "device_type", NULL);
14889b6b563cSPaul Mackerras 
1489847f5976SBenjamin Herrenschmidt 	if (!node->name)
1490847f5976SBenjamin Herrenschmidt 		node->name = "<NULL>";
1491847f5976SBenjamin Herrenschmidt 	if (!node->type)
1492847f5976SBenjamin Herrenschmidt 		node->type = "<NULL>";
1493847f5976SBenjamin Herrenschmidt 
14949b6b563cSPaul Mackerras 	if (!parent) {
14959b6b563cSPaul Mackerras 		err = -ENODEV;
14969b6b563cSPaul Mackerras 		goto out;
14979b6b563cSPaul Mackerras 	}
14989b6b563cSPaul Mackerras 
14999b6b563cSPaul Mackerras 	/* We don't support that function on PowerMac, at least
15009b6b563cSPaul Mackerras 	 * not yet
15019b6b563cSPaul Mackerras 	 */
1502e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac))
15039b6b563cSPaul Mackerras 		return -ENODEV;
15049b6b563cSPaul Mackerras 
15059b6b563cSPaul Mackerras 	/* fix up new node's linux_phandle field */
15060e56efc7SStephen Rothwell 	if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
15079b6b563cSPaul Mackerras 		node->linux_phandle = *ibm_phandle;
15089b6b563cSPaul Mackerras 
15099b6b563cSPaul Mackerras out:
15109b6b563cSPaul Mackerras 	of_node_put(parent);
15119b6b563cSPaul Mackerras 	return err;
15129b6b563cSPaul Mackerras }
15139b6b563cSPaul Mackerras 
15149b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb,
15159b6b563cSPaul Mackerras 				  unsigned long action, void *node)
15169b6b563cSPaul Mackerras {
15179b6b563cSPaul Mackerras 	int err;
15189b6b563cSPaul Mackerras 
15199b6b563cSPaul Mackerras 	switch (action) {
15209b6b563cSPaul Mackerras 	case PSERIES_RECONFIG_ADD:
1521cc5d0189SBenjamin Herrenschmidt 		err = of_finish_dynamic_node(node);
15229b6b563cSPaul Mackerras 		if (err < 0) {
15239b6b563cSPaul Mackerras 			printk(KERN_ERR "finish_node returned %d\n", err);
15249b6b563cSPaul Mackerras 			err = NOTIFY_BAD;
15259b6b563cSPaul Mackerras 		}
15269b6b563cSPaul Mackerras 		break;
15279b6b563cSPaul Mackerras 	default:
15289b6b563cSPaul Mackerras 		err = NOTIFY_DONE;
15299b6b563cSPaul Mackerras 		break;
15309b6b563cSPaul Mackerras 	}
15319b6b563cSPaul Mackerras 	return err;
15329b6b563cSPaul Mackerras }
15339b6b563cSPaul Mackerras 
15349b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = {
15359b6b563cSPaul Mackerras 	.notifier_call = prom_reconfig_notifier,
15369b6b563cSPaul Mackerras 	.priority = 10, /* This one needs to run first */
15379b6b563cSPaul Mackerras };
15389b6b563cSPaul Mackerras 
15399b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void)
15409b6b563cSPaul Mackerras {
15419b6b563cSPaul Mackerras 	return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
15429b6b563cSPaul Mackerras }
15439b6b563cSPaul Mackerras __initcall(prom_reconfig_setup);
15449b6b563cSPaul Mackerras #endif
15459b6b563cSPaul Mackerras 
1546ecaa8b0fSDave C Boutcher /*
15479b6b563cSPaul Mackerras  * Add a property to a node
15489b6b563cSPaul Mackerras  */
1549183d0202SBenjamin Herrenschmidt int prom_add_property(struct device_node* np, struct property* prop)
15509b6b563cSPaul Mackerras {
1551183d0202SBenjamin Herrenschmidt 	struct property **next;
1552f4ac7b5eSBenjamin Herrenschmidt 	unsigned long flags;
15539b6b563cSPaul Mackerras 
15549b6b563cSPaul Mackerras 	prop->next = NULL;
1555f4ac7b5eSBenjamin Herrenschmidt 	write_lock_irqsave(&devtree_lock, flags);
1556183d0202SBenjamin Herrenschmidt 	next = &np->properties;
1557183d0202SBenjamin Herrenschmidt 	while (*next) {
1558183d0202SBenjamin Herrenschmidt 		if (strcmp(prop->name, (*next)->name) == 0) {
1559183d0202SBenjamin Herrenschmidt 			/* duplicate ! don't insert it */
1560f4ac7b5eSBenjamin Herrenschmidt 			write_unlock_irqrestore(&devtree_lock, flags);
1561183d0202SBenjamin Herrenschmidt 			return -1;
1562183d0202SBenjamin Herrenschmidt 		}
15639b6b563cSPaul Mackerras 		next = &(*next)->next;
1564183d0202SBenjamin Herrenschmidt 	}
15659b6b563cSPaul Mackerras 	*next = prop;
1566f4ac7b5eSBenjamin Herrenschmidt 	write_unlock_irqrestore(&devtree_lock, flags);
1567183d0202SBenjamin Herrenschmidt 
1568799d6046SPaul Mackerras #ifdef CONFIG_PROC_DEVICETREE
1569183d0202SBenjamin Herrenschmidt 	/* try to add to proc as well if it was initialized */
1570183d0202SBenjamin Herrenschmidt 	if (np->pde)
1571183d0202SBenjamin Herrenschmidt 		proc_device_tree_add_prop(np->pde, prop);
1572799d6046SPaul Mackerras #endif /* CONFIG_PROC_DEVICETREE */
1573183d0202SBenjamin Herrenschmidt 
1574183d0202SBenjamin Herrenschmidt 	return 0;
15759b6b563cSPaul Mackerras }
15769b6b563cSPaul Mackerras 
1577088186deSDave C Boutcher /*
1578088186deSDave C Boutcher  * Remove a property from a node.  Note that we don't actually
1579088186deSDave C Boutcher  * remove it, since we have given out who-knows-how-many pointers
1580088186deSDave C Boutcher  * to the data using get-property.  Instead we just move the property
1581088186deSDave C Boutcher  * to the "dead properties" list, so it won't be found any more.
1582088186deSDave C Boutcher  */
1583088186deSDave C Boutcher int prom_remove_property(struct device_node *np, struct property *prop)
1584088186deSDave C Boutcher {
1585088186deSDave C Boutcher 	struct property **next;
1586f4ac7b5eSBenjamin Herrenschmidt 	unsigned long flags;
1587088186deSDave C Boutcher 	int found = 0;
15889b6b563cSPaul Mackerras 
1589f4ac7b5eSBenjamin Herrenschmidt 	write_lock_irqsave(&devtree_lock, flags);
1590088186deSDave C Boutcher 	next = &np->properties;
1591088186deSDave C Boutcher 	while (*next) {
1592088186deSDave C Boutcher 		if (*next == prop) {
1593088186deSDave C Boutcher 			/* found the node */
1594088186deSDave C Boutcher 			*next = prop->next;
1595088186deSDave C Boutcher 			prop->next = np->deadprops;
1596088186deSDave C Boutcher 			np->deadprops = prop;
1597088186deSDave C Boutcher 			found = 1;
1598088186deSDave C Boutcher 			break;
1599088186deSDave C Boutcher 		}
1600088186deSDave C Boutcher 		next = &(*next)->next;
1601088186deSDave C Boutcher 	}
1602f4ac7b5eSBenjamin Herrenschmidt 	write_unlock_irqrestore(&devtree_lock, flags);
1603088186deSDave C Boutcher 
1604088186deSDave C Boutcher 	if (!found)
1605088186deSDave C Boutcher 		return -ENODEV;
1606088186deSDave C Boutcher 
1607088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE
1608088186deSDave C Boutcher 	/* try to remove the proc node as well */
1609088186deSDave C Boutcher 	if (np->pde)
1610088186deSDave C Boutcher 		proc_device_tree_remove_prop(np->pde, prop);
1611088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */
1612088186deSDave C Boutcher 
1613088186deSDave C Boutcher 	return 0;
1614088186deSDave C Boutcher }
1615088186deSDave C Boutcher 
1616088186deSDave C Boutcher /*
1617088186deSDave C Boutcher  * Update a property in a node.  Note that we don't actually
1618088186deSDave C Boutcher  * remove it, since we have given out who-knows-how-many pointers
1619088186deSDave C Boutcher  * to the data using get-property.  Instead we just move the property
1620088186deSDave C Boutcher  * to the "dead properties" list, and add the new property to the
1621088186deSDave C Boutcher  * property list
1622088186deSDave C Boutcher  */
1623088186deSDave C Boutcher int prom_update_property(struct device_node *np,
1624088186deSDave C Boutcher 			 struct property *newprop,
1625088186deSDave C Boutcher 			 struct property *oldprop)
1626088186deSDave C Boutcher {
1627088186deSDave C Boutcher 	struct property **next;
1628f4ac7b5eSBenjamin Herrenschmidt 	unsigned long flags;
1629088186deSDave C Boutcher 	int found = 0;
1630088186deSDave C Boutcher 
1631f4ac7b5eSBenjamin Herrenschmidt 	write_lock_irqsave(&devtree_lock, flags);
1632088186deSDave C Boutcher 	next = &np->properties;
1633088186deSDave C Boutcher 	while (*next) {
1634088186deSDave C Boutcher 		if (*next == oldprop) {
1635088186deSDave C Boutcher 			/* found the node */
1636088186deSDave C Boutcher 			newprop->next = oldprop->next;
1637088186deSDave C Boutcher 			*next = newprop;
1638088186deSDave C Boutcher 			oldprop->next = np->deadprops;
1639088186deSDave C Boutcher 			np->deadprops = oldprop;
1640088186deSDave C Boutcher 			found = 1;
1641088186deSDave C Boutcher 			break;
1642088186deSDave C Boutcher 		}
1643088186deSDave C Boutcher 		next = &(*next)->next;
1644088186deSDave C Boutcher 	}
1645f4ac7b5eSBenjamin Herrenschmidt 	write_unlock_irqrestore(&devtree_lock, flags);
1646088186deSDave C Boutcher 
1647088186deSDave C Boutcher 	if (!found)
1648088186deSDave C Boutcher 		return -ENODEV;
1649088186deSDave C Boutcher 
1650088186deSDave C Boutcher #ifdef CONFIG_PROC_DEVICETREE
1651088186deSDave C Boutcher 	/* try to add to proc as well if it was initialized */
1652088186deSDave C Boutcher 	if (np->pde)
1653088186deSDave C Boutcher 		proc_device_tree_update_prop(np->pde, newprop, oldprop);
1654088186deSDave C Boutcher #endif /* CONFIG_PROC_DEVICETREE */
1655088186deSDave C Boutcher 
1656088186deSDave C Boutcher 	return 0;
1657088186deSDave C Boutcher }
1658b68239eeSMichael Ellerman 
1659acf7d768SBenjamin Herrenschmidt 
1660acf7d768SBenjamin Herrenschmidt /* Find the device node for a given logical cpu number, also returns the cpu
1661acf7d768SBenjamin Herrenschmidt  * local thread number (index in ibm,interrupt-server#s) if relevant and
1662acf7d768SBenjamin Herrenschmidt  * asked for (non NULL)
1663acf7d768SBenjamin Herrenschmidt  */
1664acf7d768SBenjamin Herrenschmidt struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
1665acf7d768SBenjamin Herrenschmidt {
1666acf7d768SBenjamin Herrenschmidt 	int hardid;
1667acf7d768SBenjamin Herrenschmidt 	struct device_node *np;
1668acf7d768SBenjamin Herrenschmidt 
1669acf7d768SBenjamin Herrenschmidt 	hardid = get_hard_smp_processor_id(cpu);
1670acf7d768SBenjamin Herrenschmidt 
1671acf7d768SBenjamin Herrenschmidt 	for_each_node_by_type(np, "cpu") {
1672a7f67bdfSJeremy Kerr 		const u32 *intserv;
1673acf7d768SBenjamin Herrenschmidt 		unsigned int plen, t;
1674acf7d768SBenjamin Herrenschmidt 
1675acf7d768SBenjamin Herrenschmidt 		/* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
1676acf7d768SBenjamin Herrenschmidt 		 * fallback to "reg" property and assume no threads
1677acf7d768SBenjamin Herrenschmidt 		 */
16780e56efc7SStephen Rothwell 		intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
1679acf7d768SBenjamin Herrenschmidt 				&plen);
1680acf7d768SBenjamin Herrenschmidt 		if (intserv == NULL) {
16810e56efc7SStephen Rothwell 			const u32 *reg = of_get_property(np, "reg", NULL);
1682acf7d768SBenjamin Herrenschmidt 			if (reg == NULL)
1683acf7d768SBenjamin Herrenschmidt 				continue;
1684acf7d768SBenjamin Herrenschmidt 			if (*reg == hardid) {
1685acf7d768SBenjamin Herrenschmidt 				if (thread)
1686acf7d768SBenjamin Herrenschmidt 					*thread = 0;
1687acf7d768SBenjamin Herrenschmidt 				return np;
1688acf7d768SBenjamin Herrenschmidt 			}
1689acf7d768SBenjamin Herrenschmidt 		} else {
1690acf7d768SBenjamin Herrenschmidt 			plen /= sizeof(u32);
1691acf7d768SBenjamin Herrenschmidt 			for (t = 0; t < plen; t++) {
1692acf7d768SBenjamin Herrenschmidt 				if (hardid == intserv[t]) {
1693acf7d768SBenjamin Herrenschmidt 					if (thread)
1694acf7d768SBenjamin Herrenschmidt 						*thread = t;
1695acf7d768SBenjamin Herrenschmidt 					return np;
1696acf7d768SBenjamin Herrenschmidt 				}
1697acf7d768SBenjamin Herrenschmidt 			}
1698acf7d768SBenjamin Herrenschmidt 		}
1699acf7d768SBenjamin Herrenschmidt 	}
1700acf7d768SBenjamin Herrenschmidt 	return NULL;
1701acf7d768SBenjamin Herrenschmidt }
170236ca4ba4SChristian Krafft EXPORT_SYMBOL(of_get_cpu_node);
17037a4571aeSMichael Ellerman 
170494a3807cSMichael Ellerman #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
17057a4571aeSMichael Ellerman static struct debugfs_blob_wrapper flat_dt_blob;
17067a4571aeSMichael Ellerman 
17077a4571aeSMichael Ellerman static int __init export_flat_device_tree(void)
17087a4571aeSMichael Ellerman {
17097a4571aeSMichael Ellerman 	struct dentry *d;
17107a4571aeSMichael Ellerman 
17117a4571aeSMichael Ellerman 	flat_dt_blob.data = initial_boot_params;
17127a4571aeSMichael Ellerman 	flat_dt_blob.size = initial_boot_params->totalsize;
17137a4571aeSMichael Ellerman 
17147a4571aeSMichael Ellerman 	d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
171594a3807cSMichael Ellerman 				powerpc_debugfs_root, &flat_dt_blob);
17167a4571aeSMichael Ellerman 	if (!d)
17177a4571aeSMichael Ellerman 		return 1;
17187a4571aeSMichael Ellerman 
17197a4571aeSMichael Ellerman 	return 0;
17207a4571aeSMichael Ellerman }
17217a4571aeSMichael Ellerman __initcall(export_flat_device_tree);
17227a4571aeSMichael Ellerman #endif
1723