xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision 1406bc2f57787797d1f6a3675c019a7093769275)
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 #ifdef CONFIG_PPC64
6528897731SOlof Johansson int __initdata iommu_is_off;
669b6b563cSPaul Mackerras int __initdata iommu_force_on;
67cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
689b6b563cSPaul Mackerras #endif
699b6b563cSPaul Mackerras 
70581b605aSStephen Rothwell extern rwlock_t devtree_lock;	/* temporary while merging */
719b6b563cSPaul Mackerras 
729b6b563cSPaul Mackerras /* export that to outside world */
739b6b563cSPaul Mackerras struct device_node *of_chosen;
749b6b563cSPaul Mackerras 
752babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
762babf5c2SMichael Ellerman {
772babf5c2SMichael Ellerman 	if (!p)
782babf5c2SMichael Ellerman 		return 1;
792babf5c2SMichael Ellerman 
802babf5c2SMichael Ellerman 	memory_limit = PAGE_ALIGN(memparse(p, &p));
8149a84965SBecky Bruce 	DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
822babf5c2SMichael Ellerman 
832babf5c2SMichael Ellerman 	return 0;
842babf5c2SMichael Ellerman }
852babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
862babf5c2SMichael Ellerman 
873c607ce2SLinas Vepstas /**
883c607ce2SLinas Vepstas  * move_device_tree - move tree to an unused area, if needed.
893c607ce2SLinas Vepstas  *
903c607ce2SLinas Vepstas  * The device tree may be allocated beyond our memory limit, or inside the
913c607ce2SLinas Vepstas  * crash kernel region for kdump. If so, move it out of the way.
922babf5c2SMichael Ellerman  */
9318f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
942babf5c2SMichael Ellerman {
952babf5c2SMichael Ellerman 	unsigned long start, size;
962babf5c2SMichael Ellerman 	void *p;
972babf5c2SMichael Ellerman 
982babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
992babf5c2SMichael Ellerman 
1002babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
1012babf5c2SMichael Ellerman 	size = initial_boot_params->totalsize;
1022babf5c2SMichael Ellerman 
1032babf5c2SMichael Ellerman 	if ((memory_limit && (start + size) > memory_limit) ||
1042babf5c2SMichael Ellerman 			overlaps_crashkernel(start, size)) {
1052babf5c2SMichael Ellerman 		p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
1062babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
1072babf5c2SMichael Ellerman 		initial_boot_params = (struct boot_param_header *)p;
1082babf5c2SMichael Ellerman 		DBG("Moved device tree to 0x%p\n", p);
1092babf5c2SMichael Ellerman 	}
1102babf5c2SMichael Ellerman 
1112babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
1122babf5c2SMichael Ellerman }
1139b6b563cSPaul Mackerras 
114d205819eSPaul Mackerras /*
115d205819eSPaul Mackerras  * ibm,pa-features is a per-cpu property that contains a string of
116d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
117d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
118d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
119d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
120d205819eSPaul Mackerras  * zero is the only currently-defined value.
121d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
122d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
123d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
124d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
125d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
126d205819eSPaul Mackerras  */
127d205819eSPaul Mackerras static struct ibm_pa_feature {
128d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
129d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
130d205819eSPaul Mackerras 	unsigned char	pabyte;		/* byte number in ibm,pa-features */
131d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
132d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
133d205819eSPaul Mackerras } ibm_pa_features[] __initdata = {
134d205819eSPaul Mackerras 	{0, PPC_FEATURE_HAS_MMU,	0, 0, 0},
135d205819eSPaul Mackerras 	{0, PPC_FEATURE_HAS_FPU,	0, 1, 0},
136d205819eSPaul Mackerras 	{CPU_FTR_SLB, 0,		0, 2, 0},
137d205819eSPaul Mackerras 	{CPU_FTR_CTRL, 0,		0, 3, 0},
138d205819eSPaul Mackerras 	{CPU_FTR_NOEXECUTE, 0,		0, 6, 0},
139d205819eSPaul Mackerras 	{CPU_FTR_NODSISRALIGN, 0,	1, 1, 1},
140d205819eSPaul Mackerras 	{CPU_FTR_CI_LARGE_PAGE, 0,	1, 2, 0},
141339d76c5SPaul Mackerras 	{CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
142d205819eSPaul Mackerras };
143d205819eSPaul Mackerras 
144974a76f5SPaul Mackerras static void __init scan_features(unsigned long node, unsigned char *ftrs,
145974a76f5SPaul Mackerras 				 unsigned long tablelen,
146974a76f5SPaul Mackerras 				 struct ibm_pa_feature *fp,
147974a76f5SPaul Mackerras 				 unsigned long ft_size)
148d205819eSPaul Mackerras {
149974a76f5SPaul Mackerras 	unsigned long i, len, bit;
150d205819eSPaul Mackerras 
151d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
152d205819eSPaul Mackerras 	for (;;) {
153d205819eSPaul Mackerras 		if (tablelen < 3)
154d205819eSPaul Mackerras 			return;
155974a76f5SPaul Mackerras 		len = 2 + ftrs[0];
156d205819eSPaul Mackerras 		if (tablelen < len)
157d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
158974a76f5SPaul Mackerras 		if (ftrs[1] == 0)
159d205819eSPaul Mackerras 			break;
160d205819eSPaul Mackerras 		tablelen -= len;
161974a76f5SPaul Mackerras 		ftrs += len;
162d205819eSPaul Mackerras 	}
163d205819eSPaul Mackerras 
164d205819eSPaul Mackerras 	/* loop over bits we know about */
165974a76f5SPaul Mackerras 	for (i = 0; i < ft_size; ++i, ++fp) {
166974a76f5SPaul Mackerras 		if (fp->pabyte >= ftrs[0])
167d205819eSPaul Mackerras 			continue;
168974a76f5SPaul Mackerras 		bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
169d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
170d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
171d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
172d205819eSPaul Mackerras 		} else {
173d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
174d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
175d205819eSPaul Mackerras 		}
176d205819eSPaul Mackerras 	}
177d205819eSPaul Mackerras }
178d205819eSPaul Mackerras 
179974a76f5SPaul Mackerras static void __init check_cpu_pa_features(unsigned long node)
180974a76f5SPaul Mackerras {
181974a76f5SPaul Mackerras 	unsigned char *pa_ftrs;
182974a76f5SPaul Mackerras 	unsigned long tablelen;
183974a76f5SPaul Mackerras 
184974a76f5SPaul Mackerras 	pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
185974a76f5SPaul Mackerras 	if (pa_ftrs == NULL)
186974a76f5SPaul Mackerras 		return;
187974a76f5SPaul Mackerras 
188974a76f5SPaul Mackerras 	scan_features(node, pa_ftrs, tablelen,
189974a76f5SPaul Mackerras 		      ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
190974a76f5SPaul Mackerras }
191974a76f5SPaul Mackerras 
19294491685SBenjamin Herrenschmidt #ifdef CONFIG_PPC_STD_MMU_64
193584f8b71SMichael Neuling static void __init check_cpu_slb_size(unsigned long node)
194584f8b71SMichael Neuling {
195584f8b71SMichael Neuling 	u32 *slb_size_ptr;
196584f8b71SMichael Neuling 
197b60c31d8SMichael Neuling 	slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
198b60c31d8SMichael Neuling 	if (slb_size_ptr != NULL) {
199b60c31d8SMichael Neuling 		mmu_slb_size = *slb_size_ptr;
200b60c31d8SMichael Neuling 		return;
201b60c31d8SMichael Neuling 	}
202584f8b71SMichael Neuling 	slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
203584f8b71SMichael Neuling 	if (slb_size_ptr != NULL) {
204584f8b71SMichael Neuling 		mmu_slb_size = *slb_size_ptr;
205584f8b71SMichael Neuling 	}
206584f8b71SMichael Neuling }
207584f8b71SMichael Neuling #else
208584f8b71SMichael Neuling #define check_cpu_slb_size(node) do { } while(0)
209584f8b71SMichael Neuling #endif
210584f8b71SMichael Neuling 
211974a76f5SPaul Mackerras static struct feature_property {
212974a76f5SPaul Mackerras 	const char *name;
213974a76f5SPaul Mackerras 	u32 min_value;
214974a76f5SPaul Mackerras 	unsigned long cpu_feature;
215974a76f5SPaul Mackerras 	unsigned long cpu_user_ftr;
216974a76f5SPaul Mackerras } feature_properties[] __initdata = {
217974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC
218974a76f5SPaul Mackerras 	{"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
219974a76f5SPaul Mackerras 	{"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
220974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */
221b962ce9dSMichael Neuling #ifdef CONFIG_VSX
222b962ce9dSMichael Neuling 	/* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
223b962ce9dSMichael Neuling 	{"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
224b962ce9dSMichael Neuling #endif /* CONFIG_VSX */
225974a76f5SPaul Mackerras #ifdef CONFIG_PPC64
226974a76f5SPaul Mackerras 	{"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
227974a76f5SPaul Mackerras 	{"ibm,purr", 1, CPU_FTR_PURR, 0},
228974a76f5SPaul Mackerras 	{"ibm,spurr", 1, CPU_FTR_SPURR, 0},
229974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */
230974a76f5SPaul Mackerras };
231974a76f5SPaul Mackerras 
23214b3d926SValentine Barshak #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
23314b3d926SValentine Barshak static inline void identical_pvr_fixup(unsigned long node)
23414b3d926SValentine Barshak {
23514b3d926SValentine Barshak 	unsigned int pvr;
23614b3d926SValentine Barshak 	char *model = of_get_flat_dt_prop(node, "model", NULL);
23714b3d926SValentine Barshak 
23814b3d926SValentine Barshak 	/*
23914b3d926SValentine Barshak 	 * Since 440GR(x)/440EP(x) processors have the same pvr,
24014b3d926SValentine Barshak 	 * we check the node path and set bit 28 in the cur_cpu_spec
24114b3d926SValentine Barshak 	 * pvr for EP(x) processor version. This bit is always 0 in
24214b3d926SValentine Barshak 	 * the "real" pvr. Then we call identify_cpu again with
24314b3d926SValentine Barshak 	 * the new logical pvr to enable FPU support.
24414b3d926SValentine Barshak 	 */
24514b3d926SValentine Barshak 	if (model && strstr(model, "440EP")) {
24614b3d926SValentine Barshak 		pvr = cur_cpu_spec->pvr_value | 0x8;
24714b3d926SValentine Barshak 		identify_cpu(0, pvr);
24814b3d926SValentine Barshak 		DBG("Using logical pvr %x for %s\n", pvr, model);
24914b3d926SValentine Barshak 	}
25014b3d926SValentine Barshak }
25114b3d926SValentine Barshak #else
25214b3d926SValentine Barshak #define identical_pvr_fixup(node) do { } while(0)
25314b3d926SValentine Barshak #endif
25414b3d926SValentine Barshak 
255974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node)
256974a76f5SPaul Mackerras {
257974a76f5SPaul Mackerras 	unsigned long i;
258974a76f5SPaul Mackerras 	struct feature_property *fp = feature_properties;
259974a76f5SPaul Mackerras 	const u32 *prop;
260974a76f5SPaul Mackerras 
261974a76f5SPaul Mackerras 	for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
262974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, fp->name, NULL);
263974a76f5SPaul Mackerras 		if (prop && *prop >= fp->min_value) {
264974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_feature;
265974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
266974a76f5SPaul Mackerras 		}
267974a76f5SPaul Mackerras 	}
268974a76f5SPaul Mackerras }
269974a76f5SPaul Mackerras 
2709b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
2714df20460SAnton Blanchard 					  const char *uname, int depth,
2724df20460SAnton Blanchard 					  void *data)
2739b6b563cSPaul Mackerras {
2744df20460SAnton Blanchard 	static int logical_cpuid = 0;
2754df20460SAnton Blanchard 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
276974a76f5SPaul Mackerras 	const u32 *prop;
277974a76f5SPaul Mackerras 	const u32 *intserv;
2784df20460SAnton Blanchard 	int i, nthreads;
2794df20460SAnton Blanchard 	unsigned long len;
2804df20460SAnton Blanchard 	int found = 0;
2819b6b563cSPaul Mackerras 
2829b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
2839b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
2849b6b563cSPaul Mackerras 		return 0;
2859b6b563cSPaul Mackerras 
2864df20460SAnton Blanchard 	/* Get physical cpuid */
2874df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
2884df20460SAnton Blanchard 	if (intserv) {
2894df20460SAnton Blanchard 		nthreads = len / sizeof(int);
2909b6b563cSPaul Mackerras 	} else {
2914df20460SAnton Blanchard 		intserv = of_get_flat_dt_prop(node, "reg", NULL);
2924df20460SAnton Blanchard 		nthreads = 1;
2934df20460SAnton Blanchard 	}
2944df20460SAnton Blanchard 
2954df20460SAnton Blanchard 	/*
2964df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
2974df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
2984df20460SAnton Blanchard 	 */
2994df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
3004df20460SAnton Blanchard 		/*
3014df20460SAnton Blanchard 		 * version 2 of the kexec param format adds the phys cpuid of
3024df20460SAnton Blanchard 		 * booted proc.
3034df20460SAnton Blanchard 		 */
3044df20460SAnton Blanchard 		if (initial_boot_params && initial_boot_params->version >= 2) {
3054df20460SAnton Blanchard 			if (intserv[i] ==
3064df20460SAnton Blanchard 					initial_boot_params->boot_cpuid_phys) {
3074df20460SAnton Blanchard 				found = 1;
3084df20460SAnton Blanchard 				break;
3094df20460SAnton Blanchard 			}
3104df20460SAnton Blanchard 		} else {
3114df20460SAnton Blanchard 			/*
3124df20460SAnton Blanchard 			 * Check if it's the boot-cpu, set it's hw index now,
3134df20460SAnton Blanchard 			 * unfortunately this format did not support booting
3144df20460SAnton Blanchard 			 * off secondary threads.
3154df20460SAnton Blanchard 			 */
3163c726f8dSBenjamin Herrenschmidt 			if (of_get_flat_dt_prop(node,
3173c726f8dSBenjamin Herrenschmidt 					"linux,boot-cpu", NULL) != NULL) {
3184df20460SAnton Blanchard 				found = 1;
3194df20460SAnton Blanchard 				break;
3209b6b563cSPaul Mackerras 			}
3219b6b563cSPaul Mackerras 		}
3224df20460SAnton Blanchard 
3234df20460SAnton Blanchard #ifdef CONFIG_SMP
3244df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
3254df20460SAnton Blanchard 		logical_cpuid++;
3264df20460SAnton Blanchard #endif
3274df20460SAnton Blanchard 	}
3284df20460SAnton Blanchard 
3294df20460SAnton Blanchard 	if (found) {
3304df20460SAnton Blanchard 		DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
3314df20460SAnton Blanchard 			intserv[i]);
3324df20460SAnton Blanchard 		boot_cpuid = logical_cpuid;
3334df20460SAnton Blanchard 		set_hard_smp_processor_id(boot_cpuid, intserv[i]);
334974a76f5SPaul Mackerras 
335974a76f5SPaul Mackerras 		/*
336974a76f5SPaul Mackerras 		 * PAPR defines "logical" PVR values for cpus that
337974a76f5SPaul Mackerras 		 * meet various levels of the architecture:
338974a76f5SPaul Mackerras 		 * 0x0f000001	Architecture version 2.04
339974a76f5SPaul Mackerras 		 * 0x0f000002	Architecture version 2.05
340974a76f5SPaul Mackerras 		 * If the cpu-version property in the cpu node contains
341974a76f5SPaul Mackerras 		 * such a value, we call identify_cpu again with the
342974a76f5SPaul Mackerras 		 * logical PVR value in order to use the cpu feature
343974a76f5SPaul Mackerras 		 * bits appropriate for the architecture level.
344974a76f5SPaul Mackerras 		 *
345974a76f5SPaul Mackerras 		 * A POWER6 partition in "POWER6 architected" mode
346974a76f5SPaul Mackerras 		 * uses the 0x0f000002 PVR value; in POWER5+ mode
347974a76f5SPaul Mackerras 		 * it uses 0x0f000001.
348974a76f5SPaul Mackerras 		 */
349974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
350974a76f5SPaul Mackerras 		if (prop && (*prop & 0xff000000) == 0x0f000000)
351974a76f5SPaul Mackerras 			identify_cpu(0, *prop);
35214b3d926SValentine Barshak 
35314b3d926SValentine Barshak 		identical_pvr_fixup(node);
3544df20460SAnton Blanchard 	}
3559b6b563cSPaul Mackerras 
356974a76f5SPaul Mackerras 	check_cpu_feature_properties(node);
357d205819eSPaul Mackerras 	check_cpu_pa_features(node);
358584f8b71SMichael Neuling 	check_cpu_slb_size(node);
359d205819eSPaul Mackerras 
3609b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
3614df20460SAnton Blanchard 	if (nthreads > 1)
3629b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
3634df20460SAnton Blanchard 	else
3644df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
3659b6b563cSPaul Mackerras #endif
3669b6b563cSPaul Mackerras 
3679b6b563cSPaul Mackerras 	return 0;
3689b6b563cSPaul Mackerras }
3699b6b563cSPaul Mackerras 
37086e03221SGrant Likely void __init early_init_dt_scan_chosen_arch(unsigned long node)
3719b6b563cSPaul Mackerras {
3729b6b563cSPaul Mackerras 	unsigned long *lprop;
3739b6b563cSPaul Mackerras 
3749b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3759b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
3763c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
3779b6b563cSPaul Mackerras 		iommu_is_off = 1;
3783c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
3799b6b563cSPaul Mackerras 		iommu_force_on = 1;
3809b6b563cSPaul Mackerras #endif
3819b6b563cSPaul Mackerras 
3822babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
3833c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
3849b6b563cSPaul Mackerras 	if (lprop)
3859b6b563cSPaul Mackerras 		memory_limit = *lprop;
3869b6b563cSPaul Mackerras 
3879b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3883c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
3899b6b563cSPaul Mackerras 	if (lprop)
3909b6b563cSPaul Mackerras 		tce_alloc_start = *lprop;
3913c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
3929b6b563cSPaul Mackerras 	if (lprop)
3939b6b563cSPaul Mackerras 		tce_alloc_end = *lprop;
3949b6b563cSPaul Mackerras #endif
3959b6b563cSPaul Mackerras 
396dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC
39763277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
398dcee3036SMichael Ellerman 	if (lprop)
399dcee3036SMichael Ellerman 		crashk_res.start = *lprop;
400dcee3036SMichael Ellerman 
40163277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
402dcee3036SMichael Ellerman 	if (lprop)
403dcee3036SMichael Ellerman 		crashk_res.end = crashk_res.start + *lprop - 1;
404dcee3036SMichael Ellerman #endif
4059b6b563cSPaul Mackerras }
4069b6b563cSPaul Mackerras 
4070204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
4080204568aSPaul Mackerras /*
4090204568aSPaul Mackerras  * Interpret the ibm,dynamic-memory property in the
4100204568aSPaul Mackerras  * /ibm,dynamic-reconfiguration-memory node.
4110204568aSPaul Mackerras  * This contains a list of memory blocks along with NUMA affinity
4120204568aSPaul Mackerras  * information.
4130204568aSPaul Mackerras  */
4140204568aSPaul Mackerras static int __init early_init_dt_scan_drconf_memory(unsigned long node)
4150204568aSPaul Mackerras {
4160f0b56c3SGrant Likely 	__be32 *dm, *ls, *usm;
417abe76885SBecky Bruce 	unsigned long l, n, flags;
418abe76885SBecky Bruce 	u64 base, size, lmb_size;
419cf00085dSChandru 	unsigned int is_kexec_kdump = 0, rngs;
4200204568aSPaul Mackerras 
42163277161SStephen Rothwell 	ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
4220f0b56c3SGrant Likely 	if (ls == NULL || l < dt_root_size_cells * sizeof(__be32))
4230204568aSPaul Mackerras 		return 0;
4240204568aSPaul Mackerras 	lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
4250204568aSPaul Mackerras 
42663277161SStephen Rothwell 	dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
4270f0b56c3SGrant Likely 	if (dm == NULL || l < sizeof(__be32))
4280204568aSPaul Mackerras 		return 0;
4290204568aSPaul Mackerras 
4300204568aSPaul Mackerras 	n = *dm++;	/* number of entries */
4310f0b56c3SGrant Likely 	if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(__be32))
4320204568aSPaul Mackerras 		return 0;
4330204568aSPaul Mackerras 
434cf00085dSChandru 	/* check if this is a kexec/kdump kernel. */
43563277161SStephen Rothwell 	usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
436cf00085dSChandru 						 &l);
437cf00085dSChandru 	if (usm != NULL)
438cf00085dSChandru 		is_kexec_kdump = 1;
439cf00085dSChandru 
4400204568aSPaul Mackerras 	for (; n != 0; --n) {
4410204568aSPaul Mackerras 		base = dt_mem_next_cell(dt_root_addr_cells, &dm);
4420204568aSPaul Mackerras 		flags = dm[3];
4430204568aSPaul Mackerras 		/* skip DRC index, pad, assoc. list index, flags */
4440204568aSPaul Mackerras 		dm += 4;
4450204568aSPaul Mackerras 		/* skip this block if the reserved bit is set in flags (0x80)
4460204568aSPaul Mackerras 		   or if the block is not assigned to this partition (0x8) */
4470204568aSPaul Mackerras 		if ((flags & 0x80) || !(flags & 0x8))
4480204568aSPaul Mackerras 			continue;
4490204568aSPaul Mackerras 		size = lmb_size;
450cf00085dSChandru 		rngs = 1;
451cf00085dSChandru 		if (is_kexec_kdump) {
452cf00085dSChandru 			/*
453cf00085dSChandru 			 * For each lmb in ibm,dynamic-memory, a corresponding
454cf00085dSChandru 			 * entry in linux,drconf-usable-memory property contains
455cf00085dSChandru 			 * a counter 'p' followed by 'p' (base, size) duple.
456cf00085dSChandru 			 * Now read the counter from
457cf00085dSChandru 			 * linux,drconf-usable-memory property
458cf00085dSChandru 			 */
459cf00085dSChandru 			rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
460cf00085dSChandru 			if (!rngs) /* there are no (base, size) duple */
461cf00085dSChandru 				continue;
462cf00085dSChandru 		}
463cf00085dSChandru 		do {
464cf00085dSChandru 			if (is_kexec_kdump) {
465cf00085dSChandru 				base = dt_mem_next_cell(dt_root_addr_cells,
466cf00085dSChandru 							 &usm);
467cf00085dSChandru 				size = dt_mem_next_cell(dt_root_size_cells,
468cf00085dSChandru 							 &usm);
469cf00085dSChandru 			}
4700204568aSPaul Mackerras 			if (iommu_is_off) {
4710204568aSPaul Mackerras 				if (base >= 0x80000000ul)
4720204568aSPaul Mackerras 					continue;
4730204568aSPaul Mackerras 				if ((base + size) > 0x80000000ul)
4740204568aSPaul Mackerras 					size = 0x80000000ul - base;
4750204568aSPaul Mackerras 			}
4760204568aSPaul Mackerras 			lmb_add(base, size);
477cf00085dSChandru 		} while (--rngs);
4780204568aSPaul Mackerras 	}
4790204568aSPaul Mackerras 	lmb_dump_all();
4800204568aSPaul Mackerras 	return 0;
4810204568aSPaul Mackerras }
4820204568aSPaul Mackerras #else
4830204568aSPaul Mackerras #define early_init_dt_scan_drconf_memory(node)	0
4840204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
4859b6b563cSPaul Mackerras 
48651975db0SGrant Likely static int __init early_init_dt_scan_memory_ppc(unsigned long node,
48751975db0SGrant Likely 						const char *uname,
48851975db0SGrant Likely 						int depth, void *data)
4899b6b563cSPaul Mackerras {
4900204568aSPaul Mackerras 	if (depth == 1 &&
4910204568aSPaul Mackerras 	    strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
4920204568aSPaul Mackerras 		return early_init_dt_scan_drconf_memory(node);
4930204568aSPaul Mackerras 
49451975db0SGrant Likely 	return early_init_dt_scan_memory(node, uname, depth, data);
49551975db0SGrant Likely }
4969b6b563cSPaul Mackerras 
49751975db0SGrant Likely void __init early_init_dt_add_memory_arch(u64 base, u64 size)
49851975db0SGrant Likely {
49951975db0SGrant Likely #if defined(CONFIG_PPC64)
5009b6b563cSPaul Mackerras 	if (iommu_is_off) {
5019b6b563cSPaul Mackerras 		if (base >= 0x80000000ul)
50251975db0SGrant Likely 			return;
5039b6b563cSPaul Mackerras 		if ((base + size) > 0x80000000ul)
5049b6b563cSPaul Mackerras 			size = 0x80000000ul - base;
5059b6b563cSPaul Mackerras 	}
5069b6b563cSPaul Mackerras #endif
50751975db0SGrant Likely 
5089b6b563cSPaul Mackerras 	lmb_add(base, size);
50937dd2badSKumar Gala 
51037dd2badSKumar Gala 	memstart_addr = min((u64)memstart_addr, base);
5119b6b563cSPaul Mackerras }
51237dd2badSKumar Gala 
513*1406bc2fSJeremy Kerr #ifdef CONFIG_BLK_DEV_INITRD
514*1406bc2fSJeremy Kerr void __init early_init_dt_setup_initrd_arch(unsigned long start,
515*1406bc2fSJeremy Kerr 		unsigned long end)
516*1406bc2fSJeremy Kerr {
517*1406bc2fSJeremy Kerr 	initrd_start = (unsigned long)__va(start);
518*1406bc2fSJeremy Kerr 	initrd_end = (unsigned long)__va(end);
519*1406bc2fSJeremy Kerr 	initrd_below_start_ok = 1;
520*1406bc2fSJeremy Kerr }
521*1406bc2fSJeremy Kerr #endif
522*1406bc2fSJeremy Kerr 
5239b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
5249b6b563cSPaul Mackerras {
525cbbcf340SKumar Gala 	u64 base, size;
526cbbcf340SKumar Gala 	u64 *reserve_map;
5278a300887SJon Loeliger 	unsigned long self_base;
5288a300887SJon Loeliger 	unsigned long self_size;
5299b6b563cSPaul Mackerras 
530cbbcf340SKumar Gala 	reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
5319b6b563cSPaul Mackerras 					initial_boot_params->off_mem_rsvmap);
5324d1f3f25SJimi Xenidis 
5334d1f3f25SJimi Xenidis 	/* before we do anything, lets reserve the dt blob */
5348a300887SJon Loeliger 	self_base = __pa((unsigned long)initial_boot_params);
5358a300887SJon Loeliger 	self_size = initial_boot_params->totalsize;
5368a300887SJon Loeliger 	lmb_reserve(self_base, self_size);
5374d1f3f25SJimi Xenidis 
53830437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
53930437b3eSDavid Gibson 	/* then reserve the initrd, if any */
54030437b3eSDavid Gibson 	if (initrd_start && (initrd_end > initrd_start))
54130437b3eSDavid Gibson 		lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
54230437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
54330437b3eSDavid Gibson 
544cbbcf340SKumar Gala #ifdef CONFIG_PPC32
545cbbcf340SKumar Gala 	/*
546cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
547cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
548cbbcf340SKumar Gala 	 */
549cbbcf340SKumar Gala 	if (*reserve_map > 0xffffffffull) {
550cbbcf340SKumar Gala 		u32 base_32, size_32;
551cbbcf340SKumar Gala 		u32 *reserve_map_32 = (u32 *)reserve_map;
552cbbcf340SKumar Gala 
553cbbcf340SKumar Gala 		while (1) {
554cbbcf340SKumar Gala 			base_32 = *(reserve_map_32++);
555cbbcf340SKumar Gala 			size_32 = *(reserve_map_32++);
556cbbcf340SKumar Gala 			if (size_32 == 0)
557cbbcf340SKumar Gala 				break;
5588a300887SJon Loeliger 			/* skip if the reservation is for the blob */
5598a300887SJon Loeliger 			if (base_32 == self_base && size_32 == self_size)
5608a300887SJon Loeliger 				continue;
561329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
562cbbcf340SKumar Gala 			lmb_reserve(base_32, size_32);
563cbbcf340SKumar Gala 		}
564cbbcf340SKumar Gala 		return;
565cbbcf340SKumar Gala 	}
566cbbcf340SKumar Gala #endif
5679b6b563cSPaul Mackerras 	while (1) {
5689b6b563cSPaul Mackerras 		base = *(reserve_map++);
5699b6b563cSPaul Mackerras 		size = *(reserve_map++);
5709b6b563cSPaul Mackerras 		if (size == 0)
5719b6b563cSPaul Mackerras 			break;
572cbbcf340SKumar Gala 		DBG("reserving: %llx -> %llx\n", base, size);
5739b6b563cSPaul Mackerras 		lmb_reserve(base, size);
5749b6b563cSPaul Mackerras 	}
5759b6b563cSPaul Mackerras }
5769b6b563cSPaul Mackerras 
5776ac26c8aSManish Ahuja #ifdef CONFIG_PHYP_DUMP
5786ac26c8aSManish Ahuja /**
57937ddd5d0SManish Ahuja  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
58037ddd5d0SManish Ahuja  *
58137ddd5d0SManish Ahuja  * Function to find the largest size we need to reserve
58237ddd5d0SManish Ahuja  * during early boot process.
58337ddd5d0SManish Ahuja  *
58437ddd5d0SManish Ahuja  * It either looks for boot param and returns that OR
58537ddd5d0SManish Ahuja  * returns larger of 256 or 5% rounded down to multiples of 256MB.
58637ddd5d0SManish Ahuja  *
58737ddd5d0SManish Ahuja  */
58837ddd5d0SManish Ahuja static inline unsigned long phyp_dump_calculate_reserve_size(void)
58937ddd5d0SManish Ahuja {
59037ddd5d0SManish Ahuja 	unsigned long tmp;
59137ddd5d0SManish Ahuja 
59237ddd5d0SManish Ahuja 	if (phyp_dump_info->reserve_bootvar)
59337ddd5d0SManish Ahuja 		return phyp_dump_info->reserve_bootvar;
59437ddd5d0SManish Ahuja 
59537ddd5d0SManish Ahuja 	/* divide by 20 to get 5% of value */
59637ddd5d0SManish Ahuja 	tmp = lmb_end_of_DRAM();
59737ddd5d0SManish Ahuja 	do_div(tmp, 20);
59837ddd5d0SManish Ahuja 
59937ddd5d0SManish Ahuja 	/* round it down in multiples of 256 */
60037ddd5d0SManish Ahuja 	tmp = tmp & ~0x0FFFFFFFUL;
60137ddd5d0SManish Ahuja 
60237ddd5d0SManish Ahuja 	return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
60337ddd5d0SManish Ahuja }
60437ddd5d0SManish Ahuja 
60537ddd5d0SManish Ahuja /**
6066ac26c8aSManish Ahuja  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
6076ac26c8aSManish Ahuja  *
6086ac26c8aSManish Ahuja  * This routine may reserve memory regions in the kernel only
6096ac26c8aSManish Ahuja  * if the system is supported and a dump was taken in last
6106ac26c8aSManish Ahuja  * boot instance or if the hardware is supported and the
6116ac26c8aSManish Ahuja  * scratch area needs to be setup. In other instances it returns
6126ac26c8aSManish Ahuja  * without reserving anything. The memory in case of dump being
6136ac26c8aSManish Ahuja  * active is freed when the dump is collected (by userland tools).
6146ac26c8aSManish Ahuja  */
6156ac26c8aSManish Ahuja static void __init phyp_dump_reserve_mem(void)
6166ac26c8aSManish Ahuja {
6176ac26c8aSManish Ahuja 	unsigned long base, size;
61837ddd5d0SManish Ahuja 	unsigned long variable_reserve_size;
61937ddd5d0SManish Ahuja 
6206ac26c8aSManish Ahuja 	if (!phyp_dump_info->phyp_dump_configured) {
6216ac26c8aSManish Ahuja 		printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
6226ac26c8aSManish Ahuja 		return;
6236ac26c8aSManish Ahuja 	}
6246ac26c8aSManish Ahuja 
625654f596dSManish Ahuja 	if (!phyp_dump_info->phyp_dump_at_boot) {
626654f596dSManish Ahuja 		printk(KERN_INFO "Phyp-dump disabled at boot time\n");
627654f596dSManish Ahuja 		return;
628654f596dSManish Ahuja 	}
629654f596dSManish Ahuja 
63037ddd5d0SManish Ahuja 	variable_reserve_size = phyp_dump_calculate_reserve_size();
63137ddd5d0SManish Ahuja 
6326ac26c8aSManish Ahuja 	if (phyp_dump_info->phyp_dump_is_active) {
6336ac26c8aSManish Ahuja 		/* Reserve *everything* above RMR.Area freed by userland tools*/
63437ddd5d0SManish Ahuja 		base = variable_reserve_size;
6356ac26c8aSManish Ahuja 		size = lmb_end_of_DRAM() - base;
6366ac26c8aSManish Ahuja 
6376ac26c8aSManish Ahuja 		/* XXX crashed_ram_end is wrong, since it may be beyond
6386ac26c8aSManish Ahuja 		 * the memory_limit, it will need to be adjusted. */
6396ac26c8aSManish Ahuja 		lmb_reserve(base, size);
6406ac26c8aSManish Ahuja 
6416ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_start = base;
6426ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_size = size;
6436ac26c8aSManish Ahuja 	} else {
6446ac26c8aSManish Ahuja 		size = phyp_dump_info->cpu_state_size +
6456ac26c8aSManish Ahuja 			phyp_dump_info->hpte_region_size +
64637ddd5d0SManish Ahuja 			variable_reserve_size;
6476ac26c8aSManish Ahuja 		base = lmb_end_of_DRAM() - size;
6486ac26c8aSManish Ahuja 		lmb_reserve(base, size);
6496ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_start = base;
6506ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_size = size;
6516ac26c8aSManish Ahuja 	}
6526ac26c8aSManish Ahuja }
6536ac26c8aSManish Ahuja #else
6546ac26c8aSManish Ahuja static inline void __init phyp_dump_reserve_mem(void) {}
6556ac26c8aSManish Ahuja #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
6566ac26c8aSManish Ahuja 
6576ac26c8aSManish Ahuja 
6589b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
6599b6b563cSPaul Mackerras {
66049a84965SBecky Bruce 	phys_addr_t limit;
6616ca4f749SHollis Blanchard 
66244348105SGeoff Levand 	DBG(" -> early_init_devtree(%p)\n", params);
6639b6b563cSPaul Mackerras 
6649b6b563cSPaul Mackerras 	/* Setup flat device-tree pointer */
6659b6b563cSPaul Mackerras 	initial_boot_params = params;
6669b6b563cSPaul Mackerras 
667458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
668458148c0SMichael Ellerman 	/* Some machines might need RTAS info for debugging, grab it now. */
669458148c0SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
670458148c0SMichael Ellerman #endif
671458148c0SMichael Ellerman 
6726ac26c8aSManish Ahuja #ifdef CONFIG_PHYP_DUMP
6736ac26c8aSManish Ahuja 	/* scan tree to see if dump occured during last boot */
6746ac26c8aSManish Ahuja 	of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
6756ac26c8aSManish Ahuja #endif
6766ac26c8aSManish Ahuja 
6779b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
6789b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
6799b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
6809b6b563cSPaul Mackerras 	 */
6813c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
6829b6b563cSPaul Mackerras 
6839b6b563cSPaul Mackerras 	/* Scan memory nodes and rebuild LMBs */
6849b6b563cSPaul Mackerras 	lmb_init();
6853c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
68651975db0SGrant Likely 	of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
687846f77b0SMichael Ellerman 
688846f77b0SMichael Ellerman 	/* Save command line for /proc/cmdline and then parse parameters */
689b8757b21SAlon Bar-Lev 	strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
690846f77b0SMichael Ellerman 	parse_early_param();
691846f77b0SMichael Ellerman 
6929b6b563cSPaul Mackerras 	/* Reserve LMB regions used by kernel, initrd, dt, etc... */
6930cc4746cSMichael Ellerman 	lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
694549e8152SPaul Mackerras 	/* If relocatable, reserve first 32k for interrupt vectors etc. */
695549e8152SPaul Mackerras 	if (PHYSICAL_START > MEMORY_START)
696549e8152SPaul Mackerras 		lmb_reserve(MEMORY_START, 0x8000);
69747310413SMichael Ellerman 	reserve_kdump_trampoline();
69835dd5432SMichael Ellerman 	reserve_crashkernel();
6999b6b563cSPaul Mackerras 	early_reserve_mem();
7006ac26c8aSManish Ahuja 	phyp_dump_reserve_mem();
7019b6b563cSPaul Mackerras 
7026ca4f749SHollis Blanchard 	limit = memory_limit;
7036ca4f749SHollis Blanchard 	if (! limit) {
70449a84965SBecky Bruce 		phys_addr_t memsize;
7056ca4f749SHollis Blanchard 
7066ca4f749SHollis Blanchard 		/* Ensure that total memory size is page-aligned, because
7076ca4f749SHollis Blanchard 		 * otherwise mark_bootmem() gets upset. */
7086ca4f749SHollis Blanchard 		lmb_analyze();
7096ca4f749SHollis Blanchard 		memsize = lmb_phys_mem_size();
7106ca4f749SHollis Blanchard 		if ((memsize & PAGE_MASK) != memsize)
7116ca4f749SHollis Blanchard 			limit = memsize & PAGE_MASK;
7126ca4f749SHollis Blanchard 	}
7136ca4f749SHollis Blanchard 	lmb_enforce_memory_limit(limit);
7146ca4f749SHollis Blanchard 
7152babf5c2SMichael Ellerman 	lmb_analyze();
716059f134fSMichael Ellerman 	lmb_dump_all();
7172babf5c2SMichael Ellerman 
71849a84965SBecky Bruce 	DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
7192babf5c2SMichael Ellerman 
7202babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
7212babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
7222babf5c2SMichael Ellerman 	move_device_tree();
7232babf5c2SMichael Ellerman 
7249b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
7259b6b563cSPaul Mackerras 
7263c726f8dSBenjamin Herrenschmidt 	/* Retreive CPU related informations from the flat tree
7273c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
7289b6b563cSPaul Mackerras 	 */
7293c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
7309b6b563cSPaul Mackerras 
7319b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
7329b6b563cSPaul Mackerras }
7339b6b563cSPaul Mackerras 
7349b6b563cSPaul Mackerras /*******
7359b6b563cSPaul Mackerras  *
7369b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
7379b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
7389b6b563cSPaul Mackerras  * are protected by a rw_lock.
7399b6b563cSPaul Mackerras  *
7409b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
7419b6b563cSPaul Mackerras  * this isn't dealt with yet.
7429b6b563cSPaul Mackerras  *
7439b6b563cSPaul Mackerras  *******/
7449b6b563cSPaul Mackerras 
7459b6b563cSPaul Mackerras /**
746e523f723SNathan Lynch  *	of_find_next_cache_node - Find a node's subsidiary cache
747e523f723SNathan Lynch  *	@np:	node of type "cpu" or "cache"
748e523f723SNathan Lynch  *
749e523f723SNathan Lynch  *	Returns a node pointer with refcount incremented, use
750e523f723SNathan Lynch  *	of_node_put() on it when done.  Caller should hold a reference
751e523f723SNathan Lynch  *	to np.
752e523f723SNathan Lynch  */
753e523f723SNathan Lynch struct device_node *of_find_next_cache_node(struct device_node *np)
754e523f723SNathan Lynch {
755e523f723SNathan Lynch 	struct device_node *child;
756e523f723SNathan Lynch 	const phandle *handle;
757e523f723SNathan Lynch 
758e523f723SNathan Lynch 	handle = of_get_property(np, "l2-cache", NULL);
759e523f723SNathan Lynch 	if (!handle)
760e523f723SNathan Lynch 		handle = of_get_property(np, "next-level-cache", NULL);
761e523f723SNathan Lynch 
762e523f723SNathan Lynch 	if (handle)
763e523f723SNathan Lynch 		return of_find_node_by_phandle(*handle);
764e523f723SNathan Lynch 
765e523f723SNathan Lynch 	/* OF on pmac has nodes instead of properties named "l2-cache"
766e523f723SNathan Lynch 	 * beneath CPU nodes.
767e523f723SNathan Lynch 	 */
768e523f723SNathan Lynch 	if (!strcmp(np->type, "cpu"))
769e523f723SNathan Lynch 		for_each_child_of_node(np, child)
770e523f723SNathan Lynch 			if (!strcmp(child->type, "cache"))
771e523f723SNathan Lynch 				return child;
772e523f723SNathan Lynch 
773e523f723SNathan Lynch 	return NULL;
774e523f723SNathan Lynch }
775e523f723SNathan Lynch 
7769b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
7779b6b563cSPaul Mackerras /*
7789b6b563cSPaul Mackerras  * Fix up the uninitialized fields in a new device node:
7790ebfff14SBenjamin Herrenschmidt  * name, type and pci-specific fields
7809b6b563cSPaul Mackerras  */
7819b6b563cSPaul Mackerras 
782cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node)
7839b6b563cSPaul Mackerras {
7849b6b563cSPaul Mackerras 	struct device_node *parent = of_get_parent(node);
7859b6b563cSPaul Mackerras 	int err = 0;
786a7f67bdfSJeremy Kerr 	const phandle *ibm_phandle;
7879b6b563cSPaul Mackerras 
7880e56efc7SStephen Rothwell 	node->name = of_get_property(node, "name", NULL);
7890e56efc7SStephen Rothwell 	node->type = of_get_property(node, "device_type", NULL);
7909b6b563cSPaul Mackerras 
791847f5976SBenjamin Herrenschmidt 	if (!node->name)
792847f5976SBenjamin Herrenschmidt 		node->name = "<NULL>";
793847f5976SBenjamin Herrenschmidt 	if (!node->type)
794847f5976SBenjamin Herrenschmidt 		node->type = "<NULL>";
795847f5976SBenjamin Herrenschmidt 
7969b6b563cSPaul Mackerras 	if (!parent) {
7979b6b563cSPaul Mackerras 		err = -ENODEV;
7989b6b563cSPaul Mackerras 		goto out;
7999b6b563cSPaul Mackerras 	}
8009b6b563cSPaul Mackerras 
8019b6b563cSPaul Mackerras 	/* We don't support that function on PowerMac, at least
8029b6b563cSPaul Mackerras 	 * not yet
8039b6b563cSPaul Mackerras 	 */
804e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac))
8059b6b563cSPaul Mackerras 		return -ENODEV;
8069b6b563cSPaul Mackerras 
8076016a363SGrant Likely 	/* fix up new node's phandle field */
8080e56efc7SStephen Rothwell 	if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
8096016a363SGrant Likely 		node->phandle = *ibm_phandle;
8109b6b563cSPaul Mackerras 
8119b6b563cSPaul Mackerras out:
8129b6b563cSPaul Mackerras 	of_node_put(parent);
8139b6b563cSPaul Mackerras 	return err;
8149b6b563cSPaul Mackerras }
8159b6b563cSPaul Mackerras 
8169b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb,
8179b6b563cSPaul Mackerras 				  unsigned long action, void *node)
8189b6b563cSPaul Mackerras {
8199b6b563cSPaul Mackerras 	int err;
8209b6b563cSPaul Mackerras 
8219b6b563cSPaul Mackerras 	switch (action) {
8229b6b563cSPaul Mackerras 	case PSERIES_RECONFIG_ADD:
823cc5d0189SBenjamin Herrenschmidt 		err = of_finish_dynamic_node(node);
8249b6b563cSPaul Mackerras 		if (err < 0) {
8259b6b563cSPaul Mackerras 			printk(KERN_ERR "finish_node returned %d\n", err);
8269b6b563cSPaul Mackerras 			err = NOTIFY_BAD;
8279b6b563cSPaul Mackerras 		}
8289b6b563cSPaul Mackerras 		break;
8299b6b563cSPaul Mackerras 	default:
8309b6b563cSPaul Mackerras 		err = NOTIFY_DONE;
8319b6b563cSPaul Mackerras 		break;
8329b6b563cSPaul Mackerras 	}
8339b6b563cSPaul Mackerras 	return err;
8349b6b563cSPaul Mackerras }
8359b6b563cSPaul Mackerras 
8369b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = {
8379b6b563cSPaul Mackerras 	.notifier_call = prom_reconfig_notifier,
8389b6b563cSPaul Mackerras 	.priority = 10, /* This one needs to run first */
8399b6b563cSPaul Mackerras };
8409b6b563cSPaul Mackerras 
8419b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void)
8429b6b563cSPaul Mackerras {
8439b6b563cSPaul Mackerras 	return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
8449b6b563cSPaul Mackerras }
8459b6b563cSPaul Mackerras __initcall(prom_reconfig_setup);
8469b6b563cSPaul Mackerras #endif
8479b6b563cSPaul Mackerras 
848acf7d768SBenjamin Herrenschmidt /* Find the device node for a given logical cpu number, also returns the cpu
849acf7d768SBenjamin Herrenschmidt  * local thread number (index in ibm,interrupt-server#s) if relevant and
850acf7d768SBenjamin Herrenschmidt  * asked for (non NULL)
851acf7d768SBenjamin Herrenschmidt  */
852acf7d768SBenjamin Herrenschmidt struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
853acf7d768SBenjamin Herrenschmidt {
854acf7d768SBenjamin Herrenschmidt 	int hardid;
855acf7d768SBenjamin Herrenschmidt 	struct device_node *np;
856acf7d768SBenjamin Herrenschmidt 
857acf7d768SBenjamin Herrenschmidt 	hardid = get_hard_smp_processor_id(cpu);
858acf7d768SBenjamin Herrenschmidt 
859acf7d768SBenjamin Herrenschmidt 	for_each_node_by_type(np, "cpu") {
860a7f67bdfSJeremy Kerr 		const u32 *intserv;
861acf7d768SBenjamin Herrenschmidt 		unsigned int plen, t;
862acf7d768SBenjamin Herrenschmidt 
863acf7d768SBenjamin Herrenschmidt 		/* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
864acf7d768SBenjamin Herrenschmidt 		 * fallback to "reg" property and assume no threads
865acf7d768SBenjamin Herrenschmidt 		 */
8660e56efc7SStephen Rothwell 		intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
867acf7d768SBenjamin Herrenschmidt 				&plen);
868acf7d768SBenjamin Herrenschmidt 		if (intserv == NULL) {
8690e56efc7SStephen Rothwell 			const u32 *reg = of_get_property(np, "reg", NULL);
870acf7d768SBenjamin Herrenschmidt 			if (reg == NULL)
871acf7d768SBenjamin Herrenschmidt 				continue;
872acf7d768SBenjamin Herrenschmidt 			if (*reg == hardid) {
873acf7d768SBenjamin Herrenschmidt 				if (thread)
874acf7d768SBenjamin Herrenschmidt 					*thread = 0;
875acf7d768SBenjamin Herrenschmidt 				return np;
876acf7d768SBenjamin Herrenschmidt 			}
877acf7d768SBenjamin Herrenschmidt 		} else {
878acf7d768SBenjamin Herrenschmidt 			plen /= sizeof(u32);
879acf7d768SBenjamin Herrenschmidt 			for (t = 0; t < plen; t++) {
880acf7d768SBenjamin Herrenschmidt 				if (hardid == intserv[t]) {
881acf7d768SBenjamin Herrenschmidt 					if (thread)
882acf7d768SBenjamin Herrenschmidt 						*thread = t;
883acf7d768SBenjamin Herrenschmidt 					return np;
884acf7d768SBenjamin Herrenschmidt 				}
885acf7d768SBenjamin Herrenschmidt 			}
886acf7d768SBenjamin Herrenschmidt 		}
887acf7d768SBenjamin Herrenschmidt 	}
888acf7d768SBenjamin Herrenschmidt 	return NULL;
889acf7d768SBenjamin Herrenschmidt }
89036ca4ba4SChristian Krafft EXPORT_SYMBOL(of_get_cpu_node);
8917a4571aeSMichael Ellerman 
89294a3807cSMichael Ellerman #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
8937a4571aeSMichael Ellerman static struct debugfs_blob_wrapper flat_dt_blob;
8947a4571aeSMichael Ellerman 
8957a4571aeSMichael Ellerman static int __init export_flat_device_tree(void)
8967a4571aeSMichael Ellerman {
8977a4571aeSMichael Ellerman 	struct dentry *d;
8987a4571aeSMichael Ellerman 
8997a4571aeSMichael Ellerman 	flat_dt_blob.data = initial_boot_params;
9007a4571aeSMichael Ellerman 	flat_dt_blob.size = initial_boot_params->totalsize;
9017a4571aeSMichael Ellerman 
9027a4571aeSMichael Ellerman 	d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
90394a3807cSMichael Ellerman 				powerpc_debugfs_root, &flat_dt_blob);
9047a4571aeSMichael Ellerman 	if (!d)
9057a4571aeSMichael Ellerman 		return 1;
9067a4571aeSMichael Ellerman 
9077a4571aeSMichael Ellerman 	return 0;
9087a4571aeSMichael Ellerman }
9097a4571aeSMichael Ellerman __initcall(export_flat_device_tree);
9107a4571aeSMichael Ellerman #endif
911