xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision 44ae3ab3358e962039c36ad4ae461ae9fb29596c)
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>
3495f72d1eSYinghai Lu #include <linux/memblock.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>
461426d5a3SMichael Ellerman #include <asm/paca.h>
479b6b563cSPaul Mackerras #include <asm/pgtable.h>
489b6b563cSPaul Mackerras #include <asm/pci.h>
499b6b563cSPaul Mackerras #include <asm/iommu.h>
509b6b563cSPaul Mackerras #include <asm/btext.h>
519b6b563cSPaul Mackerras #include <asm/sections.h>
529b6b563cSPaul Mackerras #include <asm/machdep.h>
539b6b563cSPaul Mackerras #include <asm/pSeries_reconfig.h>
5440ef8cbcSPaul Mackerras #include <asm/pci-bridge.h>
556ac26c8aSManish Ahuja #include <asm/phyp_dump.h>
562babf5c2SMichael Ellerman #include <asm/kexec.h>
5737dd2badSKumar Gala #include <mm/mmu_decl.h>
589b6b563cSPaul Mackerras 
599b6b563cSPaul Mackerras #ifdef DEBUG
609b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt)
619b6b563cSPaul Mackerras #else
629b6b563cSPaul Mackerras #define DBG(fmt...)
639b6b563cSPaul Mackerras #endif
649b6b563cSPaul Mackerras 
659b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6628897731SOlof Johansson int __initdata iommu_is_off;
679b6b563cSPaul Mackerras int __initdata iommu_force_on;
68cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
69cd3db0c4SBenjamin Herrenschmidt u64 ppc64_rma_size;
709b6b563cSPaul Mackerras #endif
719b6b563cSPaul Mackerras 
722babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
732babf5c2SMichael Ellerman {
742babf5c2SMichael Ellerman 	if (!p)
752babf5c2SMichael Ellerman 		return 1;
762babf5c2SMichael Ellerman 
772babf5c2SMichael Ellerman 	memory_limit = PAGE_ALIGN(memparse(p, &p));
7849a84965SBecky Bruce 	DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
792babf5c2SMichael Ellerman 
802babf5c2SMichael Ellerman 	return 0;
812babf5c2SMichael Ellerman }
822babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
832babf5c2SMichael Ellerman 
843c607ce2SLinas Vepstas /**
853c607ce2SLinas Vepstas  * move_device_tree - move tree to an unused area, if needed.
863c607ce2SLinas Vepstas  *
873c607ce2SLinas Vepstas  * The device tree may be allocated beyond our memory limit, or inside the
883c607ce2SLinas Vepstas  * crash kernel region for kdump. If so, move it out of the way.
892babf5c2SMichael Ellerman  */
9018f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
912babf5c2SMichael Ellerman {
922babf5c2SMichael Ellerman 	unsigned long start, size;
932babf5c2SMichael Ellerman 	void *p;
942babf5c2SMichael Ellerman 
952babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
962babf5c2SMichael Ellerman 
972babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
98087f79c4SJeremy Kerr 	size = be32_to_cpu(initial_boot_params->totalsize);
992babf5c2SMichael Ellerman 
1006dd22700SScott Wood 	if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
1012babf5c2SMichael Ellerman 			overlaps_crashkernel(start, size)) {
102e63075a3SBenjamin Herrenschmidt 		p = __va(memblock_alloc(size, PAGE_SIZE));
1032babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
1042babf5c2SMichael Ellerman 		initial_boot_params = (struct boot_param_header *)p;
1052babf5c2SMichael Ellerman 		DBG("Moved device tree to 0x%p\n", p);
1062babf5c2SMichael Ellerman 	}
1072babf5c2SMichael Ellerman 
1082babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
1092babf5c2SMichael Ellerman }
1109b6b563cSPaul Mackerras 
111d205819eSPaul Mackerras /*
112d205819eSPaul Mackerras  * ibm,pa-features is a per-cpu property that contains a string of
113d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
114d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
115d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
116d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
117d205819eSPaul Mackerras  * zero is the only currently-defined value.
118d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
119d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
120d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
121d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
122d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
123d205819eSPaul Mackerras  */
124d205819eSPaul Mackerras static struct ibm_pa_feature {
125d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
126*44ae3ab3SMatt Evans 	unsigned long	mmu_features;	/* MMU_FTR_xxx bit */
127d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
128d205819eSPaul Mackerras 	unsigned char	pabyte;		/* byte number in ibm,pa-features */
129d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
130d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
131d205819eSPaul Mackerras } ibm_pa_features[] __initdata = {
132*44ae3ab3SMatt Evans 	{0, 0, PPC_FEATURE_HAS_MMU,	0, 0, 0},
133*44ae3ab3SMatt Evans 	{0, 0, PPC_FEATURE_HAS_FPU,	0, 1, 0},
134*44ae3ab3SMatt Evans 	{0, MMU_FTR_SLB, 0,		0, 2, 0},
135*44ae3ab3SMatt Evans 	{CPU_FTR_CTRL, 0, 0,		0, 3, 0},
136*44ae3ab3SMatt Evans 	{CPU_FTR_NOEXECUTE, 0, 0,	0, 6, 0},
137*44ae3ab3SMatt Evans 	{CPU_FTR_NODSISRALIGN, 0, 0,	1, 1, 1},
138*44ae3ab3SMatt Evans 	{0, MMU_FTR_CI_LARGE_PAGE, 0,	1, 2, 0},
139339d76c5SPaul Mackerras 	{CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
140d205819eSPaul Mackerras };
141d205819eSPaul Mackerras 
142974a76f5SPaul Mackerras static void __init scan_features(unsigned long node, unsigned char *ftrs,
143974a76f5SPaul Mackerras 				 unsigned long tablelen,
144974a76f5SPaul Mackerras 				 struct ibm_pa_feature *fp,
145974a76f5SPaul Mackerras 				 unsigned long ft_size)
146d205819eSPaul Mackerras {
147974a76f5SPaul Mackerras 	unsigned long i, len, bit;
148d205819eSPaul Mackerras 
149d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
150d205819eSPaul Mackerras 	for (;;) {
151d205819eSPaul Mackerras 		if (tablelen < 3)
152d205819eSPaul Mackerras 			return;
153974a76f5SPaul Mackerras 		len = 2 + ftrs[0];
154d205819eSPaul Mackerras 		if (tablelen < len)
155d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
156974a76f5SPaul Mackerras 		if (ftrs[1] == 0)
157d205819eSPaul Mackerras 			break;
158d205819eSPaul Mackerras 		tablelen -= len;
159974a76f5SPaul Mackerras 		ftrs += len;
160d205819eSPaul Mackerras 	}
161d205819eSPaul Mackerras 
162d205819eSPaul Mackerras 	/* loop over bits we know about */
163974a76f5SPaul Mackerras 	for (i = 0; i < ft_size; ++i, ++fp) {
164974a76f5SPaul Mackerras 		if (fp->pabyte >= ftrs[0])
165d205819eSPaul Mackerras 			continue;
166974a76f5SPaul Mackerras 		bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
167d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
168d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
169d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
170*44ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features |= fp->mmu_features;
171d205819eSPaul Mackerras 		} else {
172d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
173d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
174*44ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features &= ~fp->mmu_features;
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 	char *type = of_get_flat_dt_prop(node, "device_type", NULL);
275974a76f5SPaul Mackerras 	const u32 *prop;
276974a76f5SPaul Mackerras 	const u32 *intserv;
2774df20460SAnton Blanchard 	int i, nthreads;
2784df20460SAnton Blanchard 	unsigned long len;
2799d07bc84SBenjamin Herrenschmidt 	int found = -1;
2809b6b563cSPaul Mackerras 
2819b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
2829b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
2839b6b563cSPaul Mackerras 		return 0;
2849b6b563cSPaul Mackerras 
2854df20460SAnton Blanchard 	/* Get physical cpuid */
2864df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
2874df20460SAnton Blanchard 	if (intserv) {
2884df20460SAnton Blanchard 		nthreads = len / sizeof(int);
2899b6b563cSPaul Mackerras 	} else {
2904df20460SAnton Blanchard 		intserv = of_get_flat_dt_prop(node, "reg", NULL);
2914df20460SAnton Blanchard 		nthreads = 1;
2924df20460SAnton Blanchard 	}
2934df20460SAnton Blanchard 
2944df20460SAnton Blanchard 	/*
2954df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
2964df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
2974df20460SAnton Blanchard 	 */
2984df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
2994df20460SAnton Blanchard 		/*
3004df20460SAnton Blanchard 		 * version 2 of the kexec param format adds the phys cpuid of
3014df20460SAnton Blanchard 		 * booted proc.
3024df20460SAnton Blanchard 		 */
3034df20460SAnton Blanchard 		if (initial_boot_params && initial_boot_params->version >= 2) {
3049d07bc84SBenjamin Herrenschmidt 			if (intserv[i] == initial_boot_params->boot_cpuid_phys)
3059d07bc84SBenjamin Herrenschmidt 				found = boot_cpu_count;
3064df20460SAnton Blanchard 		} else {
3074df20460SAnton Blanchard 			/*
3084df20460SAnton Blanchard 			 * Check if it's the boot-cpu, set it's hw index now,
3094df20460SAnton Blanchard 			 * unfortunately this format did not support booting
3104df20460SAnton Blanchard 			 * off secondary threads.
3114df20460SAnton Blanchard 			 */
3123c726f8dSBenjamin Herrenschmidt 			if (of_get_flat_dt_prop(node,
3139d07bc84SBenjamin Herrenschmidt 					"linux,boot-cpu", NULL) != NULL)
3149d07bc84SBenjamin Herrenschmidt 				found = boot_cpu_count;
3159b6b563cSPaul Mackerras 		}
3164df20460SAnton Blanchard #ifdef CONFIG_SMP
3174df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
3189d07bc84SBenjamin Herrenschmidt 		boot_cpu_count++;
3194df20460SAnton Blanchard #endif
3204df20460SAnton Blanchard 	}
3214df20460SAnton Blanchard 
3229d07bc84SBenjamin Herrenschmidt 	if (found >= 0) {
3239d07bc84SBenjamin Herrenschmidt 		DBG("boot cpu: logical %d physical %d\n", found,
3244df20460SAnton Blanchard 			intserv[i]);
3259d07bc84SBenjamin Herrenschmidt 		boot_cpuid = found;
3269d07bc84SBenjamin Herrenschmidt 		set_hard_smp_processor_id(found, intserv[i]);
327974a76f5SPaul Mackerras 
328974a76f5SPaul Mackerras 		/*
329974a76f5SPaul Mackerras 		 * PAPR defines "logical" PVR values for cpus that
330974a76f5SPaul Mackerras 		 * meet various levels of the architecture:
331974a76f5SPaul Mackerras 		 * 0x0f000001	Architecture version 2.04
332974a76f5SPaul Mackerras 		 * 0x0f000002	Architecture version 2.05
333974a76f5SPaul Mackerras 		 * If the cpu-version property in the cpu node contains
334974a76f5SPaul Mackerras 		 * such a value, we call identify_cpu again with the
335974a76f5SPaul Mackerras 		 * logical PVR value in order to use the cpu feature
336974a76f5SPaul Mackerras 		 * bits appropriate for the architecture level.
337974a76f5SPaul Mackerras 		 *
338974a76f5SPaul Mackerras 		 * A POWER6 partition in "POWER6 architected" mode
339974a76f5SPaul Mackerras 		 * uses the 0x0f000002 PVR value; in POWER5+ mode
340974a76f5SPaul Mackerras 		 * it uses 0x0f000001.
341974a76f5SPaul Mackerras 		 */
342974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
343974a76f5SPaul Mackerras 		if (prop && (*prop & 0xff000000) == 0x0f000000)
344974a76f5SPaul Mackerras 			identify_cpu(0, *prop);
34514b3d926SValentine Barshak 
34614b3d926SValentine Barshak 		identical_pvr_fixup(node);
3474df20460SAnton Blanchard 	}
3489b6b563cSPaul Mackerras 
349974a76f5SPaul Mackerras 	check_cpu_feature_properties(node);
350d205819eSPaul Mackerras 	check_cpu_pa_features(node);
351584f8b71SMichael Neuling 	check_cpu_slb_size(node);
352d205819eSPaul Mackerras 
3539b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
3544df20460SAnton Blanchard 	if (nthreads > 1)
3559b6b563cSPaul Mackerras 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
3564df20460SAnton Blanchard 	else
3574df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
3589b6b563cSPaul Mackerras #endif
3599b6b563cSPaul Mackerras 
3609b6b563cSPaul Mackerras 	return 0;
3619b6b563cSPaul Mackerras }
3629b6b563cSPaul Mackerras 
36332c97689SGrant Likely int __init early_init_dt_scan_chosen_ppc(unsigned long node, const char *uname,
36432c97689SGrant Likely 					 int depth, void *data)
3659b6b563cSPaul Mackerras {
3669b6b563cSPaul Mackerras 	unsigned long *lprop;
3679b6b563cSPaul Mackerras 
36832c97689SGrant Likely 	/* Use common scan routine to determine if this is the chosen node */
36932c97689SGrant Likely 	if (early_init_dt_scan_chosen(node, uname, depth, data) == 0)
37032c97689SGrant Likely 		return 0;
37132c97689SGrant Likely 
3729b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3739b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
3743c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
3759b6b563cSPaul Mackerras 		iommu_is_off = 1;
3763c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
3779b6b563cSPaul Mackerras 		iommu_force_on = 1;
3789b6b563cSPaul Mackerras #endif
3799b6b563cSPaul Mackerras 
3802babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
3813c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
3829b6b563cSPaul Mackerras 	if (lprop)
3839b6b563cSPaul Mackerras 		memory_limit = *lprop;
3849b6b563cSPaul Mackerras 
3859b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
3863c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
3879b6b563cSPaul Mackerras 	if (lprop)
3889b6b563cSPaul Mackerras 		tce_alloc_start = *lprop;
3893c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
3909b6b563cSPaul Mackerras 	if (lprop)
3919b6b563cSPaul Mackerras 		tce_alloc_end = *lprop;
3929b6b563cSPaul Mackerras #endif
3939b6b563cSPaul Mackerras 
394dcee3036SMichael Ellerman #ifdef CONFIG_KEXEC
39563277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
396dcee3036SMichael Ellerman 	if (lprop)
397dcee3036SMichael Ellerman 		crashk_res.start = *lprop;
398dcee3036SMichael Ellerman 
39963277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
400dcee3036SMichael Ellerman 	if (lprop)
401dcee3036SMichael Ellerman 		crashk_res.end = crashk_res.start + *lprop - 1;
402dcee3036SMichael Ellerman #endif
40332c97689SGrant Likely 
40432c97689SGrant Likely 	/* break now */
40532c97689SGrant Likely 	return 1;
4069b6b563cSPaul Mackerras }
4079b6b563cSPaul Mackerras 
4080204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
4090204568aSPaul Mackerras /*
4100204568aSPaul Mackerras  * Interpret the ibm,dynamic-memory property in the
4110204568aSPaul Mackerras  * /ibm,dynamic-reconfiguration-memory node.
4120204568aSPaul Mackerras  * This contains a list of memory blocks along with NUMA affinity
4130204568aSPaul Mackerras  * information.
4140204568aSPaul Mackerras  */
4150204568aSPaul Mackerras static int __init early_init_dt_scan_drconf_memory(unsigned long node)
4160204568aSPaul Mackerras {
4170f0b56c3SGrant Likely 	__be32 *dm, *ls, *usm;
418abe76885SBecky Bruce 	unsigned long l, n, flags;
41995f72d1eSYinghai Lu 	u64 base, size, memblock_size;
420cf00085dSChandru 	unsigned int is_kexec_kdump = 0, rngs;
4210204568aSPaul Mackerras 
4223fdfd990SBenjamin Herrenschmidt 	ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
4230f0b56c3SGrant Likely 	if (ls == NULL || l < dt_root_size_cells * sizeof(__be32))
4240204568aSPaul Mackerras 		return 0;
42595f72d1eSYinghai Lu 	memblock_size = dt_mem_next_cell(dt_root_size_cells, &ls);
4260204568aSPaul Mackerras 
42763277161SStephen Rothwell 	dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
4280f0b56c3SGrant Likely 	if (dm == NULL || l < sizeof(__be32))
4290204568aSPaul Mackerras 		return 0;
4300204568aSPaul Mackerras 
4310204568aSPaul Mackerras 	n = *dm++;	/* number of entries */
4320f0b56c3SGrant Likely 	if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(__be32))
4330204568aSPaul Mackerras 		return 0;
4340204568aSPaul Mackerras 
435cf00085dSChandru 	/* check if this is a kexec/kdump kernel. */
43663277161SStephen Rothwell 	usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
437cf00085dSChandru 						 &l);
438cf00085dSChandru 	if (usm != NULL)
439cf00085dSChandru 		is_kexec_kdump = 1;
440cf00085dSChandru 
4410204568aSPaul Mackerras 	for (; n != 0; --n) {
4420204568aSPaul Mackerras 		base = dt_mem_next_cell(dt_root_addr_cells, &dm);
4430204568aSPaul Mackerras 		flags = dm[3];
4440204568aSPaul Mackerras 		/* skip DRC index, pad, assoc. list index, flags */
4450204568aSPaul Mackerras 		dm += 4;
4460204568aSPaul Mackerras 		/* skip this block if the reserved bit is set in flags (0x80)
4470204568aSPaul Mackerras 		   or if the block is not assigned to this partition (0x8) */
4480204568aSPaul Mackerras 		if ((flags & 0x80) || !(flags & 0x8))
4490204568aSPaul Mackerras 			continue;
45095f72d1eSYinghai Lu 		size = memblock_size;
451cf00085dSChandru 		rngs = 1;
452cf00085dSChandru 		if (is_kexec_kdump) {
453cf00085dSChandru 			/*
45495f72d1eSYinghai Lu 			 * For each memblock in ibm,dynamic-memory, a corresponding
455cf00085dSChandru 			 * entry in linux,drconf-usable-memory property contains
456cf00085dSChandru 			 * a counter 'p' followed by 'p' (base, size) duple.
457cf00085dSChandru 			 * Now read the counter from
458cf00085dSChandru 			 * linux,drconf-usable-memory property
459cf00085dSChandru 			 */
460cf00085dSChandru 			rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
461cf00085dSChandru 			if (!rngs) /* there are no (base, size) duple */
462cf00085dSChandru 				continue;
463cf00085dSChandru 		}
464cf00085dSChandru 		do {
465cf00085dSChandru 			if (is_kexec_kdump) {
466cf00085dSChandru 				base = dt_mem_next_cell(dt_root_addr_cells,
467cf00085dSChandru 							 &usm);
468cf00085dSChandru 				size = dt_mem_next_cell(dt_root_size_cells,
469cf00085dSChandru 							 &usm);
470cf00085dSChandru 			}
4710204568aSPaul Mackerras 			if (iommu_is_off) {
4720204568aSPaul Mackerras 				if (base >= 0x80000000ul)
4730204568aSPaul Mackerras 					continue;
4740204568aSPaul Mackerras 				if ((base + size) > 0x80000000ul)
4750204568aSPaul Mackerras 					size = 0x80000000ul - base;
4760204568aSPaul Mackerras 			}
47795f72d1eSYinghai Lu 			memblock_add(base, size);
478cf00085dSChandru 		} while (--rngs);
4790204568aSPaul Mackerras 	}
48095f72d1eSYinghai Lu 	memblock_dump_all();
4810204568aSPaul Mackerras 	return 0;
4820204568aSPaul Mackerras }
4830204568aSPaul Mackerras #else
4840204568aSPaul Mackerras #define early_init_dt_scan_drconf_memory(node)	0
4850204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
4869b6b563cSPaul Mackerras 
48751975db0SGrant Likely static int __init early_init_dt_scan_memory_ppc(unsigned long node,
48851975db0SGrant Likely 						const char *uname,
48951975db0SGrant Likely 						int depth, void *data)
4909b6b563cSPaul Mackerras {
4910204568aSPaul Mackerras 	if (depth == 1 &&
4920204568aSPaul Mackerras 	    strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
4930204568aSPaul Mackerras 		return early_init_dt_scan_drconf_memory(node);
4940204568aSPaul Mackerras 
49551975db0SGrant Likely 	return early_init_dt_scan_memory(node, uname, depth, data);
49651975db0SGrant Likely }
4979b6b563cSPaul Mackerras 
49851975db0SGrant Likely void __init early_init_dt_add_memory_arch(u64 base, u64 size)
49951975db0SGrant Likely {
500cd3db0c4SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
5019b6b563cSPaul Mackerras 	if (iommu_is_off) {
5029b6b563cSPaul Mackerras 		if (base >= 0x80000000ul)
50351975db0SGrant Likely 			return;
5049b6b563cSPaul Mackerras 		if ((base + size) > 0x80000000ul)
5059b6b563cSPaul Mackerras 			size = 0x80000000ul - base;
5069b6b563cSPaul Mackerras 	}
5079b6b563cSPaul Mackerras #endif
50851975db0SGrant Likely 
509cd3db0c4SBenjamin Herrenschmidt 	/* First MEMBLOCK added, do some special initializations */
510cd3db0c4SBenjamin Herrenschmidt 	if (memstart_addr == ~(phys_addr_t)0)
511cd3db0c4SBenjamin Herrenschmidt 		setup_initial_memory_limit(base, size);
51237dd2badSKumar Gala 	memstart_addr = min((u64)memstart_addr, base);
513cd3db0c4SBenjamin Herrenschmidt 
514cd3db0c4SBenjamin Herrenschmidt 	/* Add the chunk to the MEMBLOCK list */
515cd3db0c4SBenjamin Herrenschmidt 	memblock_add(base, size);
5169b6b563cSPaul Mackerras }
51737dd2badSKumar Gala 
518672c5446SGrant Likely void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
5194ef7b373SJeremy Kerr {
520672c5446SGrant Likely 	return __va(memblock_alloc(size, align));
5214ef7b373SJeremy Kerr }
5224ef7b373SJeremy Kerr 
5231406bc2fSJeremy Kerr #ifdef CONFIG_BLK_DEV_INITRD
5241406bc2fSJeremy Kerr void __init early_init_dt_setup_initrd_arch(unsigned long start,
5251406bc2fSJeremy Kerr 		unsigned long end)
5261406bc2fSJeremy Kerr {
5271406bc2fSJeremy Kerr 	initrd_start = (unsigned long)__va(start);
5281406bc2fSJeremy Kerr 	initrd_end = (unsigned long)__va(end);
5291406bc2fSJeremy Kerr 	initrd_below_start_ok = 1;
5301406bc2fSJeremy Kerr }
5311406bc2fSJeremy Kerr #endif
5321406bc2fSJeremy Kerr 
5339b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
5349b6b563cSPaul Mackerras {
535cbbcf340SKumar Gala 	u64 base, size;
536cbbcf340SKumar Gala 	u64 *reserve_map;
5378a300887SJon Loeliger 	unsigned long self_base;
5388a300887SJon Loeliger 	unsigned long self_size;
5399b6b563cSPaul Mackerras 
540cbbcf340SKumar Gala 	reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
5419b6b563cSPaul Mackerras 					initial_boot_params->off_mem_rsvmap);
5424d1f3f25SJimi Xenidis 
5434d1f3f25SJimi Xenidis 	/* before we do anything, lets reserve the dt blob */
5448a300887SJon Loeliger 	self_base = __pa((unsigned long)initial_boot_params);
5458a300887SJon Loeliger 	self_size = initial_boot_params->totalsize;
54695f72d1eSYinghai Lu 	memblock_reserve(self_base, self_size);
5474d1f3f25SJimi Xenidis 
54830437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
54930437b3eSDavid Gibson 	/* then reserve the initrd, if any */
55030437b3eSDavid Gibson 	if (initrd_start && (initrd_end > initrd_start))
55195f72d1eSYinghai Lu 		memblock_reserve(__pa(initrd_start), initrd_end - initrd_start);
55230437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
55330437b3eSDavid Gibson 
554cbbcf340SKumar Gala #ifdef CONFIG_PPC32
555cbbcf340SKumar Gala 	/*
556cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
557cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
558cbbcf340SKumar Gala 	 */
559cbbcf340SKumar Gala 	if (*reserve_map > 0xffffffffull) {
560cbbcf340SKumar Gala 		u32 base_32, size_32;
561cbbcf340SKumar Gala 		u32 *reserve_map_32 = (u32 *)reserve_map;
562cbbcf340SKumar Gala 
563cbbcf340SKumar Gala 		while (1) {
564cbbcf340SKumar Gala 			base_32 = *(reserve_map_32++);
565cbbcf340SKumar Gala 			size_32 = *(reserve_map_32++);
566cbbcf340SKumar Gala 			if (size_32 == 0)
567cbbcf340SKumar Gala 				break;
5688a300887SJon Loeliger 			/* skip if the reservation is for the blob */
5698a300887SJon Loeliger 			if (base_32 == self_base && size_32 == self_size)
5708a300887SJon Loeliger 				continue;
571329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
57295f72d1eSYinghai Lu 			memblock_reserve(base_32, size_32);
573cbbcf340SKumar Gala 		}
574cbbcf340SKumar Gala 		return;
575cbbcf340SKumar Gala 	}
576cbbcf340SKumar Gala #endif
5779b6b563cSPaul Mackerras 	while (1) {
5789b6b563cSPaul Mackerras 		base = *(reserve_map++);
5799b6b563cSPaul Mackerras 		size = *(reserve_map++);
5809b6b563cSPaul Mackerras 		if (size == 0)
5819b6b563cSPaul Mackerras 			break;
582cbbcf340SKumar Gala 		DBG("reserving: %llx -> %llx\n", base, size);
58395f72d1eSYinghai Lu 		memblock_reserve(base, size);
5849b6b563cSPaul Mackerras 	}
5859b6b563cSPaul Mackerras }
5869b6b563cSPaul Mackerras 
5876ac26c8aSManish Ahuja #ifdef CONFIG_PHYP_DUMP
5886ac26c8aSManish Ahuja /**
58937ddd5d0SManish Ahuja  * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
59037ddd5d0SManish Ahuja  *
59137ddd5d0SManish Ahuja  * Function to find the largest size we need to reserve
59237ddd5d0SManish Ahuja  * during early boot process.
59337ddd5d0SManish Ahuja  *
59437ddd5d0SManish Ahuja  * It either looks for boot param and returns that OR
59537ddd5d0SManish Ahuja  * returns larger of 256 or 5% rounded down to multiples of 256MB.
59637ddd5d0SManish Ahuja  *
59737ddd5d0SManish Ahuja  */
59837ddd5d0SManish Ahuja static inline unsigned long phyp_dump_calculate_reserve_size(void)
59937ddd5d0SManish Ahuja {
60037ddd5d0SManish Ahuja 	unsigned long tmp;
60137ddd5d0SManish Ahuja 
60237ddd5d0SManish Ahuja 	if (phyp_dump_info->reserve_bootvar)
60337ddd5d0SManish Ahuja 		return phyp_dump_info->reserve_bootvar;
60437ddd5d0SManish Ahuja 
60537ddd5d0SManish Ahuja 	/* divide by 20 to get 5% of value */
60695f72d1eSYinghai Lu 	tmp = memblock_end_of_DRAM();
60737ddd5d0SManish Ahuja 	do_div(tmp, 20);
60837ddd5d0SManish Ahuja 
60937ddd5d0SManish Ahuja 	/* round it down in multiples of 256 */
61037ddd5d0SManish Ahuja 	tmp = tmp & ~0x0FFFFFFFUL;
61137ddd5d0SManish Ahuja 
61237ddd5d0SManish Ahuja 	return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
61337ddd5d0SManish Ahuja }
61437ddd5d0SManish Ahuja 
61537ddd5d0SManish Ahuja /**
6166ac26c8aSManish Ahuja  * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
6176ac26c8aSManish Ahuja  *
6186ac26c8aSManish Ahuja  * This routine may reserve memory regions in the kernel only
6196ac26c8aSManish Ahuja  * if the system is supported and a dump was taken in last
6206ac26c8aSManish Ahuja  * boot instance or if the hardware is supported and the
6216ac26c8aSManish Ahuja  * scratch area needs to be setup. In other instances it returns
6226ac26c8aSManish Ahuja  * without reserving anything. The memory in case of dump being
6236ac26c8aSManish Ahuja  * active is freed when the dump is collected (by userland tools).
6246ac26c8aSManish Ahuja  */
6256ac26c8aSManish Ahuja static void __init phyp_dump_reserve_mem(void)
6266ac26c8aSManish Ahuja {
6276ac26c8aSManish Ahuja 	unsigned long base, size;
62837ddd5d0SManish Ahuja 	unsigned long variable_reserve_size;
62937ddd5d0SManish Ahuja 
6306ac26c8aSManish Ahuja 	if (!phyp_dump_info->phyp_dump_configured) {
6316ac26c8aSManish Ahuja 		printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
6326ac26c8aSManish Ahuja 		return;
6336ac26c8aSManish Ahuja 	}
6346ac26c8aSManish Ahuja 
635654f596dSManish Ahuja 	if (!phyp_dump_info->phyp_dump_at_boot) {
636654f596dSManish Ahuja 		printk(KERN_INFO "Phyp-dump disabled at boot time\n");
637654f596dSManish Ahuja 		return;
638654f596dSManish Ahuja 	}
639654f596dSManish Ahuja 
64037ddd5d0SManish Ahuja 	variable_reserve_size = phyp_dump_calculate_reserve_size();
64137ddd5d0SManish Ahuja 
6426ac26c8aSManish Ahuja 	if (phyp_dump_info->phyp_dump_is_active) {
6436ac26c8aSManish Ahuja 		/* Reserve *everything* above RMR.Area freed by userland tools*/
64437ddd5d0SManish Ahuja 		base = variable_reserve_size;
64595f72d1eSYinghai Lu 		size = memblock_end_of_DRAM() - base;
6466ac26c8aSManish Ahuja 
6476ac26c8aSManish Ahuja 		/* XXX crashed_ram_end is wrong, since it may be beyond
6486ac26c8aSManish Ahuja 		 * the memory_limit, it will need to be adjusted. */
64995f72d1eSYinghai Lu 		memblock_reserve(base, size);
6506ac26c8aSManish Ahuja 
6516ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_start = base;
6526ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_size = size;
6536ac26c8aSManish Ahuja 	} else {
6546ac26c8aSManish Ahuja 		size = phyp_dump_info->cpu_state_size +
6556ac26c8aSManish Ahuja 			phyp_dump_info->hpte_region_size +
65637ddd5d0SManish Ahuja 			variable_reserve_size;
65795f72d1eSYinghai Lu 		base = memblock_end_of_DRAM() - size;
65895f72d1eSYinghai Lu 		memblock_reserve(base, size);
6596ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_start = base;
6606ac26c8aSManish Ahuja 		phyp_dump_info->init_reserve_size = size;
6616ac26c8aSManish Ahuja 	}
6626ac26c8aSManish Ahuja }
6636ac26c8aSManish Ahuja #else
6646ac26c8aSManish Ahuja static inline void __init phyp_dump_reserve_mem(void) {}
6656ac26c8aSManish Ahuja #endif /* CONFIG_PHYP_DUMP  && CONFIG_PPC_RTAS */
6666ac26c8aSManish Ahuja 
6679b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
6689b6b563cSPaul Mackerras {
66949a84965SBecky Bruce 	phys_addr_t limit;
6706ca4f749SHollis Blanchard 
67144348105SGeoff Levand 	DBG(" -> early_init_devtree(%p)\n", params);
6729b6b563cSPaul Mackerras 
6739b6b563cSPaul Mackerras 	/* Setup flat device-tree pointer */
6749b6b563cSPaul Mackerras 	initial_boot_params = params;
6759b6b563cSPaul Mackerras 
676458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
677458148c0SMichael Ellerman 	/* Some machines might need RTAS info for debugging, grab it now. */
678458148c0SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
679458148c0SMichael Ellerman #endif
680458148c0SMichael Ellerman 
6816ac26c8aSManish Ahuja #ifdef CONFIG_PHYP_DUMP
68225985edcSLucas De Marchi 	/* scan tree to see if dump occurred during last boot */
6836ac26c8aSManish Ahuja 	of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
6846ac26c8aSManish Ahuja #endif
6856ac26c8aSManish Ahuja 
6869b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
6879b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
6889b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
6899b6b563cSPaul Mackerras 	 */
69032c97689SGrant Likely 	of_scan_flat_dt(early_init_dt_scan_chosen_ppc, NULL);
6919b6b563cSPaul Mackerras 
69295f72d1eSYinghai Lu 	/* Scan memory nodes and rebuild MEMBLOCKs */
69395f72d1eSYinghai Lu 	memblock_init();
694e63075a3SBenjamin Herrenschmidt 
6953c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
69651975db0SGrant Likely 	of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
697846f77b0SMichael Ellerman 
698846f77b0SMichael Ellerman 	/* Save command line for /proc/cmdline and then parse parameters */
699b8757b21SAlon Bar-Lev 	strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
700846f77b0SMichael Ellerman 	parse_early_param();
701846f77b0SMichael Ellerman 
70295f72d1eSYinghai Lu 	/* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
70395f72d1eSYinghai Lu 	memblock_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
704549e8152SPaul Mackerras 	/* If relocatable, reserve first 32k for interrupt vectors etc. */
705549e8152SPaul Mackerras 	if (PHYSICAL_START > MEMORY_START)
70695f72d1eSYinghai Lu 		memblock_reserve(MEMORY_START, 0x8000);
70747310413SMichael Ellerman 	reserve_kdump_trampoline();
70835dd5432SMichael Ellerman 	reserve_crashkernel();
7099b6b563cSPaul Mackerras 	early_reserve_mem();
7106ac26c8aSManish Ahuja 	phyp_dump_reserve_mem();
7119b6b563cSPaul Mackerras 
7126ca4f749SHollis Blanchard 	limit = memory_limit;
7136ca4f749SHollis Blanchard 	if (! limit) {
71449a84965SBecky Bruce 		phys_addr_t memsize;
7156ca4f749SHollis Blanchard 
7166ca4f749SHollis Blanchard 		/* Ensure that total memory size is page-aligned, because
7176ca4f749SHollis Blanchard 		 * otherwise mark_bootmem() gets upset. */
71895f72d1eSYinghai Lu 		memblock_analyze();
71995f72d1eSYinghai Lu 		memsize = memblock_phys_mem_size();
7206ca4f749SHollis Blanchard 		if ((memsize & PAGE_MASK) != memsize)
7216ca4f749SHollis Blanchard 			limit = memsize & PAGE_MASK;
7226ca4f749SHollis Blanchard 	}
72395f72d1eSYinghai Lu 	memblock_enforce_memory_limit(limit);
7246ca4f749SHollis Blanchard 
72595f72d1eSYinghai Lu 	memblock_analyze();
72695f72d1eSYinghai Lu 	memblock_dump_all();
7272babf5c2SMichael Ellerman 
72895f72d1eSYinghai Lu 	DBG("Phys. mem: %llx\n", memblock_phys_mem_size());
7292babf5c2SMichael Ellerman 
7302babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
7312babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
7322babf5c2SMichael Ellerman 	move_device_tree();
7332babf5c2SMichael Ellerman 
7341426d5a3SMichael Ellerman 	allocate_pacas();
7351426d5a3SMichael Ellerman 
7369b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
7379b6b563cSPaul Mackerras 
73825985edcSLucas De Marchi 	/* Retrieve CPU related informations from the flat tree
7393c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
7409b6b563cSPaul Mackerras 	 */
7413c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
7429b6b563cSPaul Mackerras 
7439b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
7449b6b563cSPaul Mackerras }
7459b6b563cSPaul Mackerras 
7469b6b563cSPaul Mackerras /*******
7479b6b563cSPaul Mackerras  *
7489b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
7499b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
7509b6b563cSPaul Mackerras  * are protected by a rw_lock.
7519b6b563cSPaul Mackerras  *
7529b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
7539b6b563cSPaul Mackerras  * this isn't dealt with yet.
7549b6b563cSPaul Mackerras  *
7559b6b563cSPaul Mackerras  *******/
7569b6b563cSPaul Mackerras 
7579b6b563cSPaul Mackerras /**
758e523f723SNathan Lynch  *	of_find_next_cache_node - Find a node's subsidiary cache
759e523f723SNathan Lynch  *	@np:	node of type "cpu" or "cache"
760e523f723SNathan Lynch  *
761e523f723SNathan Lynch  *	Returns a node pointer with refcount incremented, use
762e523f723SNathan Lynch  *	of_node_put() on it when done.  Caller should hold a reference
763e523f723SNathan Lynch  *	to np.
764e523f723SNathan Lynch  */
765e523f723SNathan Lynch struct device_node *of_find_next_cache_node(struct device_node *np)
766e523f723SNathan Lynch {
767e523f723SNathan Lynch 	struct device_node *child;
768e523f723SNathan Lynch 	const phandle *handle;
769e523f723SNathan Lynch 
770e523f723SNathan Lynch 	handle = of_get_property(np, "l2-cache", NULL);
771e523f723SNathan Lynch 	if (!handle)
772e523f723SNathan Lynch 		handle = of_get_property(np, "next-level-cache", NULL);
773e523f723SNathan Lynch 
774e523f723SNathan Lynch 	if (handle)
775e523f723SNathan Lynch 		return of_find_node_by_phandle(*handle);
776e523f723SNathan Lynch 
777e523f723SNathan Lynch 	/* OF on pmac has nodes instead of properties named "l2-cache"
778e523f723SNathan Lynch 	 * beneath CPU nodes.
779e523f723SNathan Lynch 	 */
780e523f723SNathan Lynch 	if (!strcmp(np->type, "cpu"))
781e523f723SNathan Lynch 		for_each_child_of_node(np, child)
782e523f723SNathan Lynch 			if (!strcmp(child->type, "cache"))
783e523f723SNathan Lynch 				return child;
784e523f723SNathan Lynch 
785e523f723SNathan Lynch 	return NULL;
786e523f723SNathan Lynch }
787e523f723SNathan Lynch 
7889b6b563cSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
7899b6b563cSPaul Mackerras /*
7909b6b563cSPaul Mackerras  * Fix up the uninitialized fields in a new device node:
7910ebfff14SBenjamin Herrenschmidt  * name, type and pci-specific fields
7929b6b563cSPaul Mackerras  */
7939b6b563cSPaul Mackerras 
794cc5d0189SBenjamin Herrenschmidt static int of_finish_dynamic_node(struct device_node *node)
7959b6b563cSPaul Mackerras {
7969b6b563cSPaul Mackerras 	struct device_node *parent = of_get_parent(node);
7979b6b563cSPaul Mackerras 	int err = 0;
798a7f67bdfSJeremy Kerr 	const phandle *ibm_phandle;
7999b6b563cSPaul Mackerras 
8000e56efc7SStephen Rothwell 	node->name = of_get_property(node, "name", NULL);
8010e56efc7SStephen Rothwell 	node->type = of_get_property(node, "device_type", NULL);
8029b6b563cSPaul Mackerras 
803847f5976SBenjamin Herrenschmidt 	if (!node->name)
804847f5976SBenjamin Herrenschmidt 		node->name = "<NULL>";
805847f5976SBenjamin Herrenschmidt 	if (!node->type)
806847f5976SBenjamin Herrenschmidt 		node->type = "<NULL>";
807847f5976SBenjamin Herrenschmidt 
8089b6b563cSPaul Mackerras 	if (!parent) {
8099b6b563cSPaul Mackerras 		err = -ENODEV;
8109b6b563cSPaul Mackerras 		goto out;
8119b6b563cSPaul Mackerras 	}
8129b6b563cSPaul Mackerras 
8139b6b563cSPaul Mackerras 	/* We don't support that function on PowerMac, at least
8149b6b563cSPaul Mackerras 	 * not yet
8159b6b563cSPaul Mackerras 	 */
816e8222502SBenjamin Herrenschmidt 	if (machine_is(powermac))
8179b6b563cSPaul Mackerras 		return -ENODEV;
8189b6b563cSPaul Mackerras 
8196016a363SGrant Likely 	/* fix up new node's phandle field */
8200e56efc7SStephen Rothwell 	if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
8216016a363SGrant Likely 		node->phandle = *ibm_phandle;
8229b6b563cSPaul Mackerras 
8239b6b563cSPaul Mackerras out:
8249b6b563cSPaul Mackerras 	of_node_put(parent);
8259b6b563cSPaul Mackerras 	return err;
8269b6b563cSPaul Mackerras }
8279b6b563cSPaul Mackerras 
8289b6b563cSPaul Mackerras static int prom_reconfig_notifier(struct notifier_block *nb,
8299b6b563cSPaul Mackerras 				  unsigned long action, void *node)
8309b6b563cSPaul Mackerras {
8319b6b563cSPaul Mackerras 	int err;
8329b6b563cSPaul Mackerras 
8339b6b563cSPaul Mackerras 	switch (action) {
8349b6b563cSPaul Mackerras 	case PSERIES_RECONFIG_ADD:
835cc5d0189SBenjamin Herrenschmidt 		err = of_finish_dynamic_node(node);
8369b6b563cSPaul Mackerras 		if (err < 0) {
8379b6b563cSPaul Mackerras 			printk(KERN_ERR "finish_node returned %d\n", err);
8389b6b563cSPaul Mackerras 			err = NOTIFY_BAD;
8399b6b563cSPaul Mackerras 		}
8409b6b563cSPaul Mackerras 		break;
8419b6b563cSPaul Mackerras 	default:
8429b6b563cSPaul Mackerras 		err = NOTIFY_DONE;
8439b6b563cSPaul Mackerras 		break;
8449b6b563cSPaul Mackerras 	}
8459b6b563cSPaul Mackerras 	return err;
8469b6b563cSPaul Mackerras }
8479b6b563cSPaul Mackerras 
8489b6b563cSPaul Mackerras static struct notifier_block prom_reconfig_nb = {
8499b6b563cSPaul Mackerras 	.notifier_call = prom_reconfig_notifier,
8509b6b563cSPaul Mackerras 	.priority = 10, /* This one needs to run first */
8519b6b563cSPaul Mackerras };
8529b6b563cSPaul Mackerras 
8539b6b563cSPaul Mackerras static int __init prom_reconfig_setup(void)
8549b6b563cSPaul Mackerras {
8559b6b563cSPaul Mackerras 	return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
8569b6b563cSPaul Mackerras }
8579b6b563cSPaul Mackerras __initcall(prom_reconfig_setup);
8589b6b563cSPaul Mackerras #endif
8599b6b563cSPaul Mackerras 
860acf7d768SBenjamin Herrenschmidt /* Find the device node for a given logical cpu number, also returns the cpu
861acf7d768SBenjamin Herrenschmidt  * local thread number (index in ibm,interrupt-server#s) if relevant and
862acf7d768SBenjamin Herrenschmidt  * asked for (non NULL)
863acf7d768SBenjamin Herrenschmidt  */
864acf7d768SBenjamin Herrenschmidt struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
865acf7d768SBenjamin Herrenschmidt {
866acf7d768SBenjamin Herrenschmidt 	int hardid;
867acf7d768SBenjamin Herrenschmidt 	struct device_node *np;
868acf7d768SBenjamin Herrenschmidt 
869acf7d768SBenjamin Herrenschmidt 	hardid = get_hard_smp_processor_id(cpu);
870acf7d768SBenjamin Herrenschmidt 
871acf7d768SBenjamin Herrenschmidt 	for_each_node_by_type(np, "cpu") {
872a7f67bdfSJeremy Kerr 		const u32 *intserv;
873acf7d768SBenjamin Herrenschmidt 		unsigned int plen, t;
874acf7d768SBenjamin Herrenschmidt 
875acf7d768SBenjamin Herrenschmidt 		/* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
876acf7d768SBenjamin Herrenschmidt 		 * fallback to "reg" property and assume no threads
877acf7d768SBenjamin Herrenschmidt 		 */
8780e56efc7SStephen Rothwell 		intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
879acf7d768SBenjamin Herrenschmidt 				&plen);
880acf7d768SBenjamin Herrenschmidt 		if (intserv == NULL) {
8810e56efc7SStephen Rothwell 			const u32 *reg = of_get_property(np, "reg", NULL);
882acf7d768SBenjamin Herrenschmidt 			if (reg == NULL)
883acf7d768SBenjamin Herrenschmidt 				continue;
884acf7d768SBenjamin Herrenschmidt 			if (*reg == hardid) {
885acf7d768SBenjamin Herrenschmidt 				if (thread)
886acf7d768SBenjamin Herrenschmidt 					*thread = 0;
887acf7d768SBenjamin Herrenschmidt 				return np;
888acf7d768SBenjamin Herrenschmidt 			}
889acf7d768SBenjamin Herrenschmidt 		} else {
890acf7d768SBenjamin Herrenschmidt 			plen /= sizeof(u32);
891acf7d768SBenjamin Herrenschmidt 			for (t = 0; t < plen; t++) {
892acf7d768SBenjamin Herrenschmidt 				if (hardid == intserv[t]) {
893acf7d768SBenjamin Herrenschmidt 					if (thread)
894acf7d768SBenjamin Herrenschmidt 						*thread = t;
895acf7d768SBenjamin Herrenschmidt 					return np;
896acf7d768SBenjamin Herrenschmidt 				}
897acf7d768SBenjamin Herrenschmidt 			}
898acf7d768SBenjamin Herrenschmidt 		}
899acf7d768SBenjamin Herrenschmidt 	}
900acf7d768SBenjamin Herrenschmidt 	return NULL;
901acf7d768SBenjamin Herrenschmidt }
90236ca4ba4SChristian Krafft EXPORT_SYMBOL(of_get_cpu_node);
9037a4571aeSMichael Ellerman 
90494a3807cSMichael Ellerman #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
9057a4571aeSMichael Ellerman static struct debugfs_blob_wrapper flat_dt_blob;
9067a4571aeSMichael Ellerman 
9077a4571aeSMichael Ellerman static int __init export_flat_device_tree(void)
9087a4571aeSMichael Ellerman {
9097a4571aeSMichael Ellerman 	struct dentry *d;
9107a4571aeSMichael Ellerman 
9117a4571aeSMichael Ellerman 	flat_dt_blob.data = initial_boot_params;
9127a4571aeSMichael Ellerman 	flat_dt_blob.size = initial_boot_params->totalsize;
9137a4571aeSMichael Ellerman 
9147a4571aeSMichael Ellerman 	d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
91594a3807cSMichael Ellerman 				powerpc_debugfs_root, &flat_dt_blob);
9167a4571aeSMichael Ellerman 	if (!d)
9177a4571aeSMichael Ellerman 		return 1;
9187a4571aeSMichael Ellerman 
9197a4571aeSMichael Ellerman 	return 0;
9207a4571aeSMichael Ellerman }
9217a4571aeSMichael Ellerman __initcall(export_flat_device_tree);
9227a4571aeSMichael Ellerman #endif
923