xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision 6c6fdbb2b7002aa04e418b5d2b26df1c5ba5ab80)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29b6b563cSPaul Mackerras /*
39b6b563cSPaul Mackerras  * Procedures for creating, accessing and interpreting the device tree.
49b6b563cSPaul Mackerras  *
59b6b563cSPaul Mackerras  * Paul Mackerras	August 1996.
69b6b563cSPaul Mackerras  * Copyright (C) 1996-2005 Paul Mackerras.
79b6b563cSPaul Mackerras  *
89b6b563cSPaul Mackerras  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
99b6b563cSPaul Mackerras  *    {engebret|bergner}@us.ibm.com
109b6b563cSPaul Mackerras  */
119b6b563cSPaul Mackerras 
129b6b563cSPaul Mackerras #undef DEBUG
139b6b563cSPaul Mackerras 
149b6b563cSPaul Mackerras #include <stdarg.h>
159b6b563cSPaul Mackerras #include <linux/kernel.h>
169b6b563cSPaul Mackerras #include <linux/string.h>
179b6b563cSPaul Mackerras #include <linux/init.h>
189b6b563cSPaul Mackerras #include <linux/threads.h>
199b6b563cSPaul Mackerras #include <linux/spinlock.h>
209b6b563cSPaul Mackerras #include <linux/types.h>
219b6b563cSPaul Mackerras #include <linux/pci.h>
229b6b563cSPaul Mackerras #include <linux/delay.h>
239b6b563cSPaul Mackerras #include <linux/initrd.h>
249b6b563cSPaul Mackerras #include <linux/bitops.h>
254b16f8e2SPaul Gortmaker #include <linux/export.h>
26dcee3036SMichael Ellerman #include <linux/kexec.h>
270ebfff14SBenjamin Herrenschmidt #include <linux/irq.h>
2895f72d1eSYinghai Lu #include <linux/memblock.h>
291cf3d8b3SNathan Fontenot #include <linux/of.h>
30708b7eefSMarek Szyprowski #include <linux/of_fdt.h>
31060f78c2SRob Herring #include <linux/libfdt.h>
327f92bc56SDaniel Axtens #include <linux/cpu.h>
3365fddcfcSMike Rapoport #include <linux/pgtable.h>
349b6b563cSPaul Mackerras 
359b6b563cSPaul Mackerras #include <asm/prom.h>
369b6b563cSPaul Mackerras #include <asm/rtas.h>
379b6b563cSPaul Mackerras #include <asm/page.h>
389b6b563cSPaul Mackerras #include <asm/processor.h>
399b6b563cSPaul Mackerras #include <asm/irq.h>
409b6b563cSPaul Mackerras #include <asm/io.h>
410cc4746cSMichael Ellerman #include <asm/kdump.h>
429b6b563cSPaul Mackerras #include <asm/smp.h>
439b6b563cSPaul Mackerras #include <asm/mmu.h>
441426d5a3SMichael Ellerman #include <asm/paca.h>
4554820530SMichael Ellerman #include <asm/powernv.h>
469b6b563cSPaul Mackerras #include <asm/iommu.h>
479b6b563cSPaul Mackerras #include <asm/btext.h>
489b6b563cSPaul Mackerras #include <asm/sections.h>
499b6b563cSPaul Mackerras #include <asm/machdep.h>
5040ef8cbcSPaul Mackerras #include <asm/pci-bridge.h>
512babf5c2SMichael Ellerman #include <asm/kexec.h>
5214a43e69SBenjamin Herrenschmidt #include <asm/opal.h>
53eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
54da6a97bfSBenjamin Herrenschmidt #include <asm/epapr_hcalls.h>
553808a889SBenjamin Herrenschmidt #include <asm/firmware.h>
565a61ef74SNicholas Piggin #include <asm/dt_cpu_ftrs.h>
576c6ea537SNathan Fontenot #include <asm/drmem.h>
58bb04ffe8SClaudio Carvalho #include <asm/ultravisor.h>
5914a43e69SBenjamin Herrenschmidt 
6037dd2badSKumar Gala #include <mm/mmu_decl.h>
619b6b563cSPaul Mackerras 
629b6b563cSPaul Mackerras #ifdef DEBUG
639b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt)
649b6b563cSPaul Mackerras #else
659b6b563cSPaul Mackerras #define DBG(fmt...)
669b6b563cSPaul Mackerras #endif
679b6b563cSPaul Mackerras 
689b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
6928897731SOlof Johansson int __initdata iommu_is_off;
709b6b563cSPaul Mackerras int __initdata iommu_force_on;
71cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
72cd3db0c4SBenjamin Herrenschmidt u64 ppc64_rma_size;
739b6b563cSPaul Mackerras #endif
7403bf469aSBenjamin Herrenschmidt static phys_addr_t first_memblock_size;
757ac87abbSMatt Evans static int __initdata boot_cpu_count;
769b6b563cSPaul Mackerras 
772babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
782babf5c2SMichael Ellerman {
792babf5c2SMichael Ellerman 	if (!p)
802babf5c2SMichael Ellerman 		return 1;
812babf5c2SMichael Ellerman 
822babf5c2SMichael Ellerman 	memory_limit = PAGE_ALIGN(memparse(p, &p));
83a84fcd46SSuzuki Poulose 	DBG("memory limit = 0x%llx\n", memory_limit);
842babf5c2SMichael Ellerman 
852babf5c2SMichael Ellerman 	return 0;
862babf5c2SMichael Ellerman }
872babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
882babf5c2SMichael Ellerman 
89307cfe71SBenjamin Herrenschmidt /*
90307cfe71SBenjamin Herrenschmidt  * overlaps_initrd - check for overlap with page aligned extension of
91307cfe71SBenjamin Herrenschmidt  * initrd.
92307cfe71SBenjamin Herrenschmidt  */
93307cfe71SBenjamin Herrenschmidt static inline int overlaps_initrd(unsigned long start, unsigned long size)
94307cfe71SBenjamin Herrenschmidt {
95307cfe71SBenjamin Herrenschmidt #ifdef CONFIG_BLK_DEV_INITRD
96307cfe71SBenjamin Herrenschmidt 	if (!initrd_start)
97307cfe71SBenjamin Herrenschmidt 		return 0;
98307cfe71SBenjamin Herrenschmidt 
99e96d904eSChristophe Leroy 	return	(start + size) > ALIGN_DOWN(initrd_start, PAGE_SIZE) &&
100b7115316SChristophe Leroy 			start <= ALIGN(initrd_end, PAGE_SIZE);
101307cfe71SBenjamin Herrenschmidt #else
102307cfe71SBenjamin Herrenschmidt 	return 0;
103307cfe71SBenjamin Herrenschmidt #endif
104307cfe71SBenjamin Herrenschmidt }
105307cfe71SBenjamin Herrenschmidt 
1063c607ce2SLinas Vepstas /**
1073c607ce2SLinas Vepstas  * move_device_tree - move tree to an unused area, if needed.
1083c607ce2SLinas Vepstas  *
1093c607ce2SLinas Vepstas  * The device tree may be allocated beyond our memory limit, or inside the
110307cfe71SBenjamin Herrenschmidt  * crash kernel region for kdump, or within the page aligned range of initrd.
111307cfe71SBenjamin Herrenschmidt  * If so, move it out of the way.
1122babf5c2SMichael Ellerman  */
11318f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
1142babf5c2SMichael Ellerman {
1152babf5c2SMichael Ellerman 	unsigned long start, size;
1162babf5c2SMichael Ellerman 	void *p;
1172babf5c2SMichael Ellerman 
1182babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
1192babf5c2SMichael Ellerman 
1202babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
121060f78c2SRob Herring 	size = fdt_totalsize(initial_boot_params);
1222babf5c2SMichael Ellerman 
1236dd22700SScott Wood 	if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
124f242e0acSChristophe Leroy 	    !memblock_is_memory(start + size - 1) ||
125f242e0acSChristophe Leroy 	    overlaps_crashkernel(start, size) || overlaps_initrd(start, size)) {
1261269f7b8SChristophe Leroy 		p = memblock_alloc_raw(size, PAGE_SIZE);
1271269f7b8SChristophe Leroy 		if (!p)
1281269f7b8SChristophe Leroy 			panic("Failed to allocate %lu bytes to move device tree\n",
1291269f7b8SChristophe Leroy 			      size);
1302babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
131060f78c2SRob Herring 		initial_boot_params = p;
132b18f0ae9SChristophe Leroy 		DBG("Moved device tree to 0x%px\n", p);
1332babf5c2SMichael Ellerman 	}
1342babf5c2SMichael Ellerman 
1352babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
1362babf5c2SMichael Ellerman }
1379b6b563cSPaul Mackerras 
138d205819eSPaul Mackerras /*
139d205819eSPaul Mackerras  * ibm,pa-features is a per-cpu property that contains a string of
140d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
141d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
142d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
143d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
144d205819eSPaul Mackerras  * zero is the only currently-defined value.
145d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
146d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
147d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
148d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
149d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
150d205819eSPaul Mackerras  */
151d205819eSPaul Mackerras static struct ibm_pa_feature {
152d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
15344ae3ab3SMatt Evans 	unsigned long	mmu_features;	/* MMU_FTR_xxx bit */
154d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
155beff8237SAnton Blanchard 	unsigned int	cpu_user_ftrs2;	/* PPC_FEATURE2_xxx bit */
156d205819eSPaul Mackerras 	unsigned char	pabyte;		/* byte number in ibm,pa-features */
157d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
158d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
159d205819eSPaul Mackerras } ibm_pa_features[] __initdata = {
160e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 0, .cpu_user_ftrs = PPC_FEATURE_HAS_MMU },
161e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 1, .cpu_user_ftrs = PPC_FEATURE_HAS_FPU },
162e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 3, .cpu_features  = CPU_FTR_CTRL },
163e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 6, .cpu_features  = CPU_FTR_NOEXECUTE },
164e9eb0278SMichael Ellerman 	{ .pabyte = 1,  .pabit = 2, .mmu_features  = MMU_FTR_CI_LARGE_PAGE },
165d957fb4dSMichael Neuling #ifdef CONFIG_PPC_RADIX_MMU
1669a77c4a0SNicholas Piggin 	{ .pabyte = 40, .pabit = 0, .mmu_features  = MMU_FTR_TYPE_RADIX | MMU_FTR_GTSE },
167d957fb4dSMichael Neuling #endif
168e9eb0278SMichael Ellerman 	{ .pabyte = 5,  .pabit = 0, .cpu_features  = CPU_FTR_REAL_LE,
169e9eb0278SMichael Ellerman 				    .cpu_user_ftrs = PPC_FEATURE_TRUE_LE },
1709e819963SAneesh Kumar K.V 	/*
1714705e024SAnton Blanchard 	 * If the kernel doesn't support TM (ie CONFIG_PPC_TRANSACTIONAL_MEM=n),
1724705e024SAnton Blanchard 	 * we don't want to turn on TM here, so we use the *_COMP versions
1734705e024SAnton Blanchard 	 * which are 0 if the kernel doesn't support TM.
1749e819963SAneesh Kumar K.V 	 */
175e9eb0278SMichael Ellerman 	{ .pabyte = 22, .pabit = 0, .cpu_features = CPU_FTR_TM_COMP,
176e9eb0278SMichael Ellerman 	  .cpu_user_ftrs2 = PPC_FEATURE2_HTM_COMP | PPC_FEATURE2_HTM_NOSC_COMP },
1778f45ca3fSRavi Bangoria 
1788f45ca3fSRavi Bangoria 	{ .pabyte = 64, .pabit = 0, .cpu_features = CPU_FTR_DAWR1 },
179d205819eSPaul Mackerras };
180d205819eSPaul Mackerras 
1819d0c4dfeSRob Herring static void __init scan_features(unsigned long node, const unsigned char *ftrs,
182974a76f5SPaul Mackerras 				 unsigned long tablelen,
183974a76f5SPaul Mackerras 				 struct ibm_pa_feature *fp,
184974a76f5SPaul Mackerras 				 unsigned long ft_size)
185d205819eSPaul Mackerras {
186974a76f5SPaul Mackerras 	unsigned long i, len, bit;
187d205819eSPaul Mackerras 
188d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
189d205819eSPaul Mackerras 	for (;;) {
190d205819eSPaul Mackerras 		if (tablelen < 3)
191d205819eSPaul Mackerras 			return;
192974a76f5SPaul Mackerras 		len = 2 + ftrs[0];
193d205819eSPaul Mackerras 		if (tablelen < len)
194d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
195974a76f5SPaul Mackerras 		if (ftrs[1] == 0)
196d205819eSPaul Mackerras 			break;
197d205819eSPaul Mackerras 		tablelen -= len;
198974a76f5SPaul Mackerras 		ftrs += len;
199d205819eSPaul Mackerras 	}
200d205819eSPaul Mackerras 
201d205819eSPaul Mackerras 	/* loop over bits we know about */
202974a76f5SPaul Mackerras 	for (i = 0; i < ft_size; ++i, ++fp) {
203974a76f5SPaul Mackerras 		if (fp->pabyte >= ftrs[0])
204d205819eSPaul Mackerras 			continue;
205974a76f5SPaul Mackerras 		bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
206d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
207d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
208d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
209beff8237SAnton Blanchard 			cur_cpu_spec->cpu_user_features2 |= fp->cpu_user_ftrs2;
21044ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features |= fp->mmu_features;
211d205819eSPaul Mackerras 		} else {
212d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
213d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
214beff8237SAnton Blanchard 			cur_cpu_spec->cpu_user_features2 &= ~fp->cpu_user_ftrs2;
21544ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features &= ~fp->mmu_features;
216d205819eSPaul Mackerras 		}
217d205819eSPaul Mackerras 	}
218d205819eSPaul Mackerras }
219d205819eSPaul Mackerras 
220974a76f5SPaul Mackerras static void __init check_cpu_pa_features(unsigned long node)
221974a76f5SPaul Mackerras {
2229d0c4dfeSRob Herring 	const unsigned char *pa_ftrs;
2239d0c4dfeSRob Herring 	int tablelen;
224974a76f5SPaul Mackerras 
225974a76f5SPaul Mackerras 	pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
226974a76f5SPaul Mackerras 	if (pa_ftrs == NULL)
227974a76f5SPaul Mackerras 		return;
228974a76f5SPaul Mackerras 
229974a76f5SPaul Mackerras 	scan_features(node, pa_ftrs, tablelen,
230974a76f5SPaul Mackerras 		      ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
231974a76f5SPaul Mackerras }
232974a76f5SPaul Mackerras 
2334e003747SMichael Ellerman #ifdef CONFIG_PPC_BOOK3S_64
2349c61f7a0SAnshuman Khandual static void __init init_mmu_slb_size(unsigned long node)
235584f8b71SMichael Neuling {
2369d0c4dfeSRob Herring 	const __be32 *slb_size_ptr;
237584f8b71SMichael Neuling 
2389c61f7a0SAnshuman Khandual 	slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL) ? :
2399c61f7a0SAnshuman Khandual 			of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
2409c61f7a0SAnshuman Khandual 
2419c61f7a0SAnshuman Khandual 	if (slb_size_ptr)
242dc0e643aSIan Munsie 		mmu_slb_size = be32_to_cpup(slb_size_ptr);
243584f8b71SMichael Neuling }
244584f8b71SMichael Neuling #else
2459c61f7a0SAnshuman Khandual #define init_mmu_slb_size(node) do { } while(0)
246584f8b71SMichael Neuling #endif
247584f8b71SMichael Neuling 
248974a76f5SPaul Mackerras static struct feature_property {
249974a76f5SPaul Mackerras 	const char *name;
250974a76f5SPaul Mackerras 	u32 min_value;
251974a76f5SPaul Mackerras 	unsigned long cpu_feature;
252974a76f5SPaul Mackerras 	unsigned long cpu_user_ftr;
253974a76f5SPaul Mackerras } feature_properties[] __initdata = {
254974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC
255974a76f5SPaul Mackerras 	{"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
256974a76f5SPaul Mackerras 	{"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
257974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */
258b962ce9dSMichael Neuling #ifdef CONFIG_VSX
259b962ce9dSMichael Neuling 	/* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
260b962ce9dSMichael Neuling 	{"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
261b962ce9dSMichael Neuling #endif /* CONFIG_VSX */
262974a76f5SPaul Mackerras #ifdef CONFIG_PPC64
263974a76f5SPaul Mackerras 	{"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
264974a76f5SPaul Mackerras 	{"ibm,purr", 1, CPU_FTR_PURR, 0},
265974a76f5SPaul Mackerras 	{"ibm,spurr", 1, CPU_FTR_SPURR, 0},
266974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */
267974a76f5SPaul Mackerras };
268974a76f5SPaul Mackerras 
26914b3d926SValentine Barshak #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
27014b3d926SValentine Barshak static inline void identical_pvr_fixup(unsigned long node)
27114b3d926SValentine Barshak {
27214b3d926SValentine Barshak 	unsigned int pvr;
2739d0c4dfeSRob Herring 	const char *model = of_get_flat_dt_prop(node, "model", NULL);
27414b3d926SValentine Barshak 
27514b3d926SValentine Barshak 	/*
27614b3d926SValentine Barshak 	 * Since 440GR(x)/440EP(x) processors have the same pvr,
27714b3d926SValentine Barshak 	 * we check the node path and set bit 28 in the cur_cpu_spec
27814b3d926SValentine Barshak 	 * pvr for EP(x) processor version. This bit is always 0 in
27914b3d926SValentine Barshak 	 * the "real" pvr. Then we call identify_cpu again with
28014b3d926SValentine Barshak 	 * the new logical pvr to enable FPU support.
28114b3d926SValentine Barshak 	 */
28214b3d926SValentine Barshak 	if (model && strstr(model, "440EP")) {
28314b3d926SValentine Barshak 		pvr = cur_cpu_spec->pvr_value | 0x8;
28414b3d926SValentine Barshak 		identify_cpu(0, pvr);
28514b3d926SValentine Barshak 		DBG("Using logical pvr %x for %s\n", pvr, model);
28614b3d926SValentine Barshak 	}
28714b3d926SValentine Barshak }
28814b3d926SValentine Barshak #else
28914b3d926SValentine Barshak #define identical_pvr_fixup(node) do { } while(0)
29014b3d926SValentine Barshak #endif
29114b3d926SValentine Barshak 
292974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node)
293974a76f5SPaul Mackerras {
2944f1f40f7SMathieu Malaterre 	int i;
295974a76f5SPaul Mackerras 	struct feature_property *fp = feature_properties;
296dc0e643aSIan Munsie 	const __be32 *prop;
297974a76f5SPaul Mackerras 
2984f1f40f7SMathieu Malaterre 	for (i = 0; i < (int)ARRAY_SIZE(feature_properties); ++i, ++fp) {
299974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, fp->name, NULL);
300dc0e643aSIan Munsie 		if (prop && be32_to_cpup(prop) >= fp->min_value) {
301974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_feature;
302974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
303974a76f5SPaul Mackerras 		}
304974a76f5SPaul Mackerras 	}
305974a76f5SPaul Mackerras }
306974a76f5SPaul Mackerras 
3079b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
3084df20460SAnton Blanchard 					  const char *uname, int depth,
3094df20460SAnton Blanchard 					  void *data)
3109b6b563cSPaul Mackerras {
3119d0c4dfeSRob Herring 	const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
312dc0e643aSIan Munsie 	const __be32 *prop;
313dc0e643aSIan Munsie 	const __be32 *intserv;
3144df20460SAnton Blanchard 	int i, nthreads;
3159d0c4dfeSRob Herring 	int len;
3169d07bc84SBenjamin Herrenschmidt 	int found = -1;
317208b3a4cSBenjamin Herrenschmidt 	int found_thread = 0;
3189b6b563cSPaul Mackerras 
3199b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
3209b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
3219b6b563cSPaul Mackerras 		return 0;
3229b6b563cSPaul Mackerras 
3234df20460SAnton Blanchard 	/* Get physical cpuid */
3244df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
325e16c8765SAndy Fleming 	if (!intserv)
326e16c8765SAndy Fleming 		intserv = of_get_flat_dt_prop(node, "reg", &len);
327e16c8765SAndy Fleming 
3284df20460SAnton Blanchard 	nthreads = len / sizeof(int);
3294df20460SAnton Blanchard 
3304df20460SAnton Blanchard 	/*
3314df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
3324df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
3334df20460SAnton Blanchard 	 */
3344df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
335dc0e643aSIan Munsie 		if (be32_to_cpu(intserv[i]) ==
336060f78c2SRob Herring 			fdt_boot_cpuid_phys(initial_boot_params)) {
3379d07bc84SBenjamin Herrenschmidt 			found = boot_cpu_count;
338208b3a4cSBenjamin Herrenschmidt 			found_thread = i;
339208b3a4cSBenjamin Herrenschmidt 		}
3404df20460SAnton Blanchard #ifdef CONFIG_SMP
3414df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
3429d07bc84SBenjamin Herrenschmidt 		boot_cpu_count++;
3434df20460SAnton Blanchard #endif
3444df20460SAnton Blanchard 	}
3454df20460SAnton Blanchard 
3467222f779SBenjamin Herrenschmidt 	/* Not the boot CPU */
3477222f779SBenjamin Herrenschmidt 	if (found < 0)
3487222f779SBenjamin Herrenschmidt 		return 0;
3497222f779SBenjamin Herrenschmidt 
3509d07bc84SBenjamin Herrenschmidt 	DBG("boot cpu: logical %d physical %d\n", found,
351dc0e643aSIan Munsie 	    be32_to_cpu(intserv[found_thread]));
3529d07bc84SBenjamin Herrenschmidt 	boot_cpuid = found;
353974a76f5SPaul Mackerras 
354974a76f5SPaul Mackerras 	/*
355974a76f5SPaul Mackerras 	 * PAPR defines "logical" PVR values for cpus that
356974a76f5SPaul Mackerras 	 * meet various levels of the architecture:
357974a76f5SPaul Mackerras 	 * 0x0f000001	Architecture version 2.04
358974a76f5SPaul Mackerras 	 * 0x0f000002	Architecture version 2.05
359974a76f5SPaul Mackerras 	 * If the cpu-version property in the cpu node contains
360974a76f5SPaul Mackerras 	 * such a value, we call identify_cpu again with the
361974a76f5SPaul Mackerras 	 * logical PVR value in order to use the cpu feature
362974a76f5SPaul Mackerras 	 * bits appropriate for the architecture level.
363974a76f5SPaul Mackerras 	 *
364974a76f5SPaul Mackerras 	 * A POWER6 partition in "POWER6 architected" mode
365974a76f5SPaul Mackerras 	 * uses the 0x0f000002 PVR value; in POWER5+ mode
366974a76f5SPaul Mackerras 	 * it uses 0x0f000001.
3675a61ef74SNicholas Piggin 	 *
3685a61ef74SNicholas Piggin 	 * If we're using device tree CPU feature discovery then we don't
3695a61ef74SNicholas Piggin 	 * support the cpu-version property, and it's the responsibility of the
3705a61ef74SNicholas Piggin 	 * firmware/hypervisor to provide the correct feature set for the
3715a61ef74SNicholas Piggin 	 * architecture level via the ibm,powerpc-cpu-features binding.
372974a76f5SPaul Mackerras 	 */
3735a61ef74SNicholas Piggin 	if (!dt_cpu_ftrs_in_use()) {
374974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
375dc0e643aSIan Munsie 		if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000)
376dc0e643aSIan Munsie 			identify_cpu(0, be32_to_cpup(prop));
37714b3d926SValentine Barshak 
378974a76f5SPaul Mackerras 		check_cpu_feature_properties(node);
379d205819eSPaul Mackerras 		check_cpu_pa_features(node);
3805a61ef74SNicholas Piggin 	}
3815a61ef74SNicholas Piggin 
3825a61ef74SNicholas Piggin 	identical_pvr_fixup(node);
3839c61f7a0SAnshuman Khandual 	init_mmu_slb_size(node);
384d205819eSPaul Mackerras 
3854a85b31dSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
3865a61ef74SNicholas Piggin 	if (nthreads == 1)
3874df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
3885a61ef74SNicholas Piggin 	else if (!dt_cpu_ftrs_in_use())
3895a61ef74SNicholas Piggin 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
39059f57774SNicholas Piggin 	allocate_paca(boot_cpuid);
3919b6b563cSPaul Mackerras #endif
39259f57774SNicholas Piggin 	set_hard_smp_processor_id(found, be32_to_cpu(intserv[found_thread]));
3935a61ef74SNicholas Piggin 
3949b6b563cSPaul Mackerras 	return 0;
3959b6b563cSPaul Mackerras }
3969b6b563cSPaul Mackerras 
397e51df2c1SAnton Blanchard static int __init early_init_dt_scan_chosen_ppc(unsigned long node,
398e51df2c1SAnton Blanchard 						const char *uname,
39932c97689SGrant Likely 						int depth, void *data)
4009b6b563cSPaul Mackerras {
4019d0c4dfeSRob Herring 	const unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
4029b6b563cSPaul Mackerras 
40332c97689SGrant Likely 	/* Use common scan routine to determine if this is the chosen node */
40432c97689SGrant Likely 	if (early_init_dt_scan_chosen(node, uname, depth, data) == 0)
40532c97689SGrant Likely 		return 0;
40632c97689SGrant Likely 
4079b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4089b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
4093c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
4109b6b563cSPaul Mackerras 		iommu_is_off = 1;
4113c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
4129b6b563cSPaul Mackerras 		iommu_force_on = 1;
4139b6b563cSPaul Mackerras #endif
4149b6b563cSPaul Mackerras 
4152babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
4163c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
4179b6b563cSPaul Mackerras 	if (lprop)
4189b6b563cSPaul Mackerras 		memory_limit = *lprop;
4199b6b563cSPaul Mackerras 
4209b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4213c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
4229b6b563cSPaul Mackerras 	if (lprop)
4239b6b563cSPaul Mackerras 		tce_alloc_start = *lprop;
4243c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
4259b6b563cSPaul Mackerras 	if (lprop)
4269b6b563cSPaul Mackerras 		tce_alloc_end = *lprop;
4279b6b563cSPaul Mackerras #endif
4289b6b563cSPaul Mackerras 
429da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
43063277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
431dcee3036SMichael Ellerman 	if (lprop)
432dcee3036SMichael Ellerman 		crashk_res.start = *lprop;
433dcee3036SMichael Ellerman 
43463277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
435dcee3036SMichael Ellerman 	if (lprop)
436dcee3036SMichael Ellerman 		crashk_res.end = crashk_res.start + *lprop - 1;
437dcee3036SMichael Ellerman #endif
43832c97689SGrant Likely 
43932c97689SGrant Likely 	/* break now */
44032c97689SGrant Likely 	return 1;
4419b6b563cSPaul Mackerras }
4429b6b563cSPaul Mackerras 
4436aba0c84SAneesh Kumar K.V /*
4446aba0c84SAneesh Kumar K.V  * Compare the range against max mem limit and update
4456aba0c84SAneesh Kumar K.V  * size if it cross the limit.
4466aba0c84SAneesh Kumar K.V  */
4476aba0c84SAneesh Kumar K.V 
4486aba0c84SAneesh Kumar K.V #ifdef CONFIG_SPARSEMEM
4496aba0c84SAneesh Kumar K.V static bool validate_mem_limit(u64 base, u64 *size)
4506aba0c84SAneesh Kumar K.V {
4516aba0c84SAneesh Kumar K.V 	u64 max_mem = 1UL << (MAX_PHYSMEM_BITS);
4526aba0c84SAneesh Kumar K.V 
4536aba0c84SAneesh Kumar K.V 	if (base >= max_mem)
4546aba0c84SAneesh Kumar K.V 		return false;
4556aba0c84SAneesh Kumar K.V 	if ((base + *size) > max_mem)
4566aba0c84SAneesh Kumar K.V 		*size = max_mem - base;
4576aba0c84SAneesh Kumar K.V 	return true;
4586aba0c84SAneesh Kumar K.V }
4596aba0c84SAneesh Kumar K.V #else
4606aba0c84SAneesh Kumar K.V static bool validate_mem_limit(u64 base, u64 *size)
4616aba0c84SAneesh Kumar K.V {
4626aba0c84SAneesh Kumar K.V 	return true;
4636aba0c84SAneesh Kumar K.V }
4646aba0c84SAneesh Kumar K.V #endif
4656aba0c84SAneesh Kumar K.V 
4660204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
4670204568aSPaul Mackerras /*
4686c6ea537SNathan Fontenot  * Interpret the ibm dynamic reconfiguration memory LMBs.
4690204568aSPaul Mackerras  * This contains a list of memory blocks along with NUMA affinity
4700204568aSPaul Mackerras  * information.
4710204568aSPaul Mackerras  */
472adfefc60SHari Bathini static int  __init early_init_drmem_lmb(struct drmem_lmb *lmb,
473adfefc60SHari Bathini 					const __be32 **usm,
474adfefc60SHari Bathini 					void *data)
4750204568aSPaul Mackerras {
4766c6ea537SNathan Fontenot 	u64 base, size;
4776c6ea537SNathan Fontenot 	int is_kexec_kdump = 0, rngs;
4780204568aSPaul Mackerras 
4796c6ea537SNathan Fontenot 	base = lmb->base_addr;
4806c6ea537SNathan Fontenot 	size = drmem_lmb_size();
4816c6ea537SNathan Fontenot 	rngs = 1;
4820204568aSPaul Mackerras 
4836c6ea537SNathan Fontenot 	/*
4846c6ea537SNathan Fontenot 	 * Skip this block if the reserved bit is set in flags
4856c6ea537SNathan Fontenot 	 * or if the block is not assigned to this partition.
4866c6ea537SNathan Fontenot 	 */
4876c6ea537SNathan Fontenot 	if ((lmb->flags & DRCONF_MEM_RESERVED) ||
4886c6ea537SNathan Fontenot 	    !(lmb->flags & DRCONF_MEM_ASSIGNED))
489adfefc60SHari Bathini 		return 0;
4900204568aSPaul Mackerras 
4916c6ea537SNathan Fontenot 	if (*usm)
492cf00085dSChandru 		is_kexec_kdump = 1;
493cf00085dSChandru 
494cf00085dSChandru 	if (is_kexec_kdump) {
495cf00085dSChandru 		/*
4966c6ea537SNathan Fontenot 		 * For each memblock in ibm,dynamic-memory, a
4976c6ea537SNathan Fontenot 		 * corresponding entry in linux,drconf-usable-memory
4986c6ea537SNathan Fontenot 		 * property contains a counter 'p' followed by 'p'
4996c6ea537SNathan Fontenot 		 * (base, size) duple. Now read the counter from
500cf00085dSChandru 		 * linux,drconf-usable-memory property
501cf00085dSChandru 		 */
5026c6ea537SNathan Fontenot 		rngs = dt_mem_next_cell(dt_root_size_cells, usm);
503cf00085dSChandru 		if (!rngs) /* there are no (base, size) duple */
504adfefc60SHari Bathini 			return 0;
505cf00085dSChandru 	}
5066c6ea537SNathan Fontenot 
507cf00085dSChandru 	do {
508cf00085dSChandru 		if (is_kexec_kdump) {
5096c6ea537SNathan Fontenot 			base = dt_mem_next_cell(dt_root_addr_cells, usm);
5106c6ea537SNathan Fontenot 			size = dt_mem_next_cell(dt_root_size_cells, usm);
511cf00085dSChandru 		}
5126c6ea537SNathan Fontenot 
5130204568aSPaul Mackerras 		if (iommu_is_off) {
5140204568aSPaul Mackerras 			if (base >= 0x80000000ul)
5150204568aSPaul Mackerras 				continue;
5160204568aSPaul Mackerras 			if ((base + size) > 0x80000000ul)
5170204568aSPaul Mackerras 				size = 0x80000000ul - base;
5180204568aSPaul Mackerras 		}
5196c6ea537SNathan Fontenot 
520b6eca183SLeonardo Bras 		if (!validate_mem_limit(base, &size))
521b6eca183SLeonardo Bras 			continue;
522b6eca183SLeonardo Bras 
5236c6ea537SNathan Fontenot 		DBG("Adding: %llx -> %llx\n", base, size);
52495f72d1eSYinghai Lu 		memblock_add(base, size);
525b6eca183SLeonardo Bras 
526b6eca183SLeonardo Bras 		if (lmb->flags & DRCONF_MEM_HOTREMOVABLE)
527b6eca183SLeonardo Bras 			memblock_mark_hotplug(base, size);
528cf00085dSChandru 	} while (--rngs);
529adfefc60SHari Bathini 
530adfefc60SHari Bathini 	return 0;
5310204568aSPaul Mackerras }
5320204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
5339b6b563cSPaul Mackerras 
53451975db0SGrant Likely static int __init early_init_dt_scan_memory_ppc(unsigned long node,
53551975db0SGrant Likely 						const char *uname,
53651975db0SGrant Likely 						int depth, void *data)
5379b6b563cSPaul Mackerras {
5386c6ea537SNathan Fontenot #ifdef CONFIG_PPC_PSERIES
5390204568aSPaul Mackerras 	if (depth == 1 &&
5406c6ea537SNathan Fontenot 	    strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0) {
541adfefc60SHari Bathini 		walk_drmem_lmbs_early(node, NULL, early_init_drmem_lmb);
5426c6ea537SNathan Fontenot 		return 0;
5436c6ea537SNathan Fontenot 	}
5446c6ea537SNathan Fontenot #endif
5450204568aSPaul Mackerras 
54651975db0SGrant Likely 	return early_init_dt_scan_memory(node, uname, depth, data);
54751975db0SGrant Likely }
5489b6b563cSPaul Mackerras 
549b27652ddSKevin Hao /*
550b27652ddSKevin Hao  * For a relocatable kernel, we need to get the memstart_addr first,
551b27652ddSKevin Hao  * then use it to calculate the virtual kernel start address. This has
552b27652ddSKevin Hao  * to happen at a very early stage (before machine_init). In this case,
553b27652ddSKevin Hao  * we just want to get the memstart_address and would not like to mess the
554b27652ddSKevin Hao  * memblock at this stage. So introduce a variable to skip the memblock_add()
555b27652ddSKevin Hao  * for this reason.
556b27652ddSKevin Hao  */
557b27652ddSKevin Hao #ifdef CONFIG_RELOCATABLE
558b27652ddSKevin Hao static int add_mem_to_memblock = 1;
559b27652ddSKevin Hao #else
560b27652ddSKevin Hao #define add_mem_to_memblock 1
561b27652ddSKevin Hao #endif
562b27652ddSKevin Hao 
56351975db0SGrant Likely void __init early_init_dt_add_memory_arch(u64 base, u64 size)
56451975db0SGrant Likely {
565cd3db0c4SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
5669b6b563cSPaul Mackerras 	if (iommu_is_off) {
5679b6b563cSPaul Mackerras 		if (base >= 0x80000000ul)
56851975db0SGrant Likely 			return;
5699b6b563cSPaul Mackerras 		if ((base + size) > 0x80000000ul)
5709b6b563cSPaul Mackerras 			size = 0x80000000ul - base;
5719b6b563cSPaul Mackerras 	}
5729b6b563cSPaul Mackerras #endif
57303bf469aSBenjamin Herrenschmidt 	/* Keep track of the beginning of memory -and- the size of
57403bf469aSBenjamin Herrenschmidt 	 * the very first block in the device-tree as it represents
57503bf469aSBenjamin Herrenschmidt 	 * the RMA on ppc64 server
57603bf469aSBenjamin Herrenschmidt 	 */
57703bf469aSBenjamin Herrenschmidt 	if (base < memstart_addr) {
57803bf469aSBenjamin Herrenschmidt 		memstart_addr = base;
57903bf469aSBenjamin Herrenschmidt 		first_memblock_size = size;
58003bf469aSBenjamin Herrenschmidt 	}
581cd3db0c4SBenjamin Herrenschmidt 
582cd3db0c4SBenjamin Herrenschmidt 	/* Add the chunk to the MEMBLOCK list */
5836aba0c84SAneesh Kumar K.V 	if (add_mem_to_memblock) {
5846aba0c84SAneesh Kumar K.V 		if (validate_mem_limit(base, &size))
585cd3db0c4SBenjamin Herrenschmidt 			memblock_add(base, size);
5869b6b563cSPaul Mackerras 	}
5876aba0c84SAneesh Kumar K.V }
58837dd2badSKumar Gala 
589c039e3a8SBenjamin Herrenschmidt static void __init early_reserve_mem_dt(void)
5900962e800SJeremy Kerr {
5919d0c4dfeSRob Herring 	unsigned long i, dt_root;
5929d0c4dfeSRob Herring 	int len;
5930962e800SJeremy Kerr 	const __be32 *prop;
5940962e800SJeremy Kerr 
59524bbd929SArd Biesheuvel 	early_init_fdt_reserve_self();
596edba274cSRob Herring 	early_init_fdt_scan_reserved_mem();
597edba274cSRob Herring 
5980962e800SJeremy Kerr 	dt_root = of_get_flat_dt_root();
5990962e800SJeremy Kerr 
6000962e800SJeremy Kerr 	prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
6010962e800SJeremy Kerr 
6020962e800SJeremy Kerr 	if (!prop)
603c039e3a8SBenjamin Herrenschmidt 		return;
604c039e3a8SBenjamin Herrenschmidt 
605c039e3a8SBenjamin Herrenschmidt 	DBG("Found new-style reserved-ranges\n");
6060962e800SJeremy Kerr 
6070962e800SJeremy Kerr 	/* Each reserved range is an (address,size) pair, 2 cells each,
6080962e800SJeremy Kerr 	 * totalling 4 cells per range. */
6090962e800SJeremy Kerr 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
6100962e800SJeremy Kerr 		u64 base, size;
6110962e800SJeremy Kerr 
6120962e800SJeremy Kerr 		base = of_read_number(prop + (i * 4) + 0, 2);
6130962e800SJeremy Kerr 		size = of_read_number(prop + (i * 4) + 2, 2);
6140962e800SJeremy Kerr 
615c039e3a8SBenjamin Herrenschmidt 		if (size) {
616c039e3a8SBenjamin Herrenschmidt 			DBG("reserving: %llx -> %llx\n", base, size);
6170962e800SJeremy Kerr 			memblock_reserve(base, size);
6180962e800SJeremy Kerr 		}
619c039e3a8SBenjamin Herrenschmidt 	}
6200962e800SJeremy Kerr }
6210962e800SJeremy Kerr 
6229b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
6239b6b563cSPaul Mackerras {
624dc0e643aSIan Munsie 	__be64 *reserve_map;
6259b6b563cSPaul Mackerras 
626dc0e643aSIan Munsie 	reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
627060f78c2SRob Herring 			fdt_off_mem_rsvmap(initial_boot_params));
6284d1f3f25SJimi Xenidis 
629c039e3a8SBenjamin Herrenschmidt 	/* Look for the new "reserved-regions" property in the DT */
630c039e3a8SBenjamin Herrenschmidt 	early_reserve_mem_dt();
6310962e800SJeremy Kerr 
63230437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
633c039e3a8SBenjamin Herrenschmidt 	/* Then reserve the initrd, if any */
634c039e3a8SBenjamin Herrenschmidt 	if (initrd_start && (initrd_end > initrd_start)) {
635e96d904eSChristophe Leroy 		memblock_reserve(ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
636b7115316SChristophe Leroy 			ALIGN(initrd_end, PAGE_SIZE) -
637e96d904eSChristophe Leroy 			ALIGN_DOWN(initrd_start, PAGE_SIZE));
638c039e3a8SBenjamin Herrenschmidt 	}
63930437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
64030437b3eSDavid Gibson 
641cbbcf340SKumar Gala #ifdef CONFIG_PPC32
642cbbcf340SKumar Gala 	/*
643cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
644cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
645cbbcf340SKumar Gala 	 */
646dc0e643aSIan Munsie 	if (be64_to_cpup(reserve_map) > 0xffffffffull) {
647cbbcf340SKumar Gala 		u32 base_32, size_32;
648dc0e643aSIan Munsie 		__be32 *reserve_map_32 = (__be32 *)reserve_map;
649cbbcf340SKumar Gala 
650c039e3a8SBenjamin Herrenschmidt 		DBG("Found old 32-bit reserve map\n");
651c039e3a8SBenjamin Herrenschmidt 
652cbbcf340SKumar Gala 		while (1) {
653dc0e643aSIan Munsie 			base_32 = be32_to_cpup(reserve_map_32++);
654dc0e643aSIan Munsie 			size_32 = be32_to_cpup(reserve_map_32++);
655cbbcf340SKumar Gala 			if (size_32 == 0)
656cbbcf340SKumar Gala 				break;
657329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
65895f72d1eSYinghai Lu 			memblock_reserve(base_32, size_32);
659cbbcf340SKumar Gala 		}
660cbbcf340SKumar Gala 		return;
661cbbcf340SKumar Gala 	}
662cbbcf340SKumar Gala #endif
6639b6b563cSPaul Mackerras }
6649b6b563cSPaul Mackerras 
66507fd1761SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
66607fd1761SCyril Bur static bool tm_disabled __initdata;
66707fd1761SCyril Bur 
66807fd1761SCyril Bur static int __init parse_ppc_tm(char *str)
66907fd1761SCyril Bur {
67007fd1761SCyril Bur 	bool res;
67107fd1761SCyril Bur 
67207fd1761SCyril Bur 	if (kstrtobool(str, &res))
67307fd1761SCyril Bur 		return -EINVAL;
67407fd1761SCyril Bur 
67507fd1761SCyril Bur 	tm_disabled = !res;
67607fd1761SCyril Bur 
67707fd1761SCyril Bur 	return 0;
67807fd1761SCyril Bur }
67907fd1761SCyril Bur early_param("ppc_tm", parse_ppc_tm);
68007fd1761SCyril Bur 
68107fd1761SCyril Bur static void __init tm_init(void)
68207fd1761SCyril Bur {
68307fd1761SCyril Bur 	if (tm_disabled) {
68407fd1761SCyril Bur 		pr_info("Disabling hardware transactional memory (HTM)\n");
68507fd1761SCyril Bur 		cur_cpu_spec->cpu_user_features2 &=
68607fd1761SCyril Bur 			~(PPC_FEATURE2_HTM_NOSC | PPC_FEATURE2_HTM);
68707fd1761SCyril Bur 		cur_cpu_spec->cpu_features &= ~CPU_FTR_TM;
68854820530SMichael Ellerman 		return;
68907fd1761SCyril Bur 	}
69054820530SMichael Ellerman 
69154820530SMichael Ellerman 	pnv_tm_init();
69207fd1761SCyril Bur }
69307fd1761SCyril Bur #else
69407fd1761SCyril Bur static void tm_init(void) { }
69507fd1761SCyril Bur #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
69607fd1761SCyril Bur 
697912c0a7fSMichael Ellerman #ifdef CONFIG_PPC64
698912c0a7fSMichael Ellerman static void __init save_fscr_to_task(void)
699912c0a7fSMichael Ellerman {
700912c0a7fSMichael Ellerman 	/*
701912c0a7fSMichael Ellerman 	 * Ensure the init_task (pid 0, aka swapper) uses the value of FSCR we
702912c0a7fSMichael Ellerman 	 * have configured via the device tree features or via __init_FSCR().
703912c0a7fSMichael Ellerman 	 * That value will then be propagated to pid 1 (init) and all future
704912c0a7fSMichael Ellerman 	 * processes.
705912c0a7fSMichael Ellerman 	 */
706912c0a7fSMichael Ellerman 	if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
707912c0a7fSMichael Ellerman 		init_task.thread.fscr = mfspr(SPRN_FSCR);
708912c0a7fSMichael Ellerman }
709912c0a7fSMichael Ellerman #else
710*6c6fdbb2SChengyang Fan static inline void save_fscr_to_task(void) {}
711912c0a7fSMichael Ellerman #endif
712912c0a7fSMichael Ellerman 
713912c0a7fSMichael Ellerman 
7149b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
7159b6b563cSPaul Mackerras {
71649a84965SBecky Bruce 	phys_addr_t limit;
7176ca4f749SHollis Blanchard 
718b18f0ae9SChristophe Leroy 	DBG(" -> early_init_devtree(%px)\n", params);
7199b6b563cSPaul Mackerras 
720ad72a279SMichael Ellerman 	/* Too early to BUG_ON(), do it by hand */
721ad72a279SMichael Ellerman 	if (!early_init_dt_verify(params))
722ad72a279SMichael Ellerman 		panic("BUG: Failed verifying flat device tree, bad version?");
723ad72a279SMichael Ellerman 
724458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
725458148c0SMichael Ellerman 	/* Some machines might need RTAS info for debugging, grab it now. */
726458148c0SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
727458148c0SMichael Ellerman #endif
728458148c0SMichael Ellerman 
72914a43e69SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
73014a43e69SBenjamin Herrenschmidt 	/* Some machines might need OPAL info for debugging, grab it now. */
73114a43e69SBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_opal, NULL);
732bb04ffe8SClaudio Carvalho 
733bb04ffe8SClaudio Carvalho 	/* Scan tree for ultravisor feature */
734bb04ffe8SClaudio Carvalho 	of_scan_flat_dt(early_init_dt_scan_ultravisor, NULL);
73514a43e69SBenjamin Herrenschmidt #endif
73614a43e69SBenjamin Herrenschmidt 
737bec53196SHari Bathini #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
738eb39c880SMahesh Salgaonkar 	/* scan tree to see if dump is active during last boot */
739eb39c880SMahesh Salgaonkar 	of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
740eb39c880SMahesh Salgaonkar #endif
741eb39c880SMahesh Salgaonkar 
7429b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
7439b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
7449b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
7459b6b563cSPaul Mackerras 	 */
7463e47d147SAnton Blanchard 	of_scan_flat_dt(early_init_dt_scan_chosen_ppc, boot_command_line);
7479b6b563cSPaul Mackerras 
74895f72d1eSYinghai Lu 	/* Scan memory nodes and rebuild MEMBLOCKs */
7493c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
75051975db0SGrant Likely 	of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
751846f77b0SMichael Ellerman 
752846f77b0SMichael Ellerman 	parse_early_param();
753846f77b0SMichael Ellerman 
754ba14f649SKumar Gala 	/* make sure we've parsed cmdline for mem= before this */
755ba14f649SKumar Gala 	if (memory_limit)
756a84fcd46SSuzuki Poulose 		first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
757ba14f649SKumar Gala 	setup_initial_memory_limit(memstart_addr, first_memblock_size);
75895f72d1eSYinghai Lu 	/* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
75995f72d1eSYinghai Lu 	memblock_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
760549e8152SPaul Mackerras 	/* If relocatable, reserve first 32k for interrupt vectors etc. */
761549e8152SPaul Mackerras 	if (PHYSICAL_START > MEMORY_START)
76295f72d1eSYinghai Lu 		memblock_reserve(MEMORY_START, 0x8000);
76347310413SMichael Ellerman 	reserve_kdump_trampoline();
764bec53196SHari Bathini #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
765eb39c880SMahesh Salgaonkar 	/*
766eb39c880SMahesh Salgaonkar 	 * If we fail to reserve memory for firmware-assisted dump then
767eb39c880SMahesh Salgaonkar 	 * fallback to kexec based kdump.
768eb39c880SMahesh Salgaonkar 	 */
769eb39c880SMahesh Salgaonkar 	if (fadump_reserve_mem() == 0)
770eb39c880SMahesh Salgaonkar #endif
77135dd5432SMichael Ellerman 		reserve_crashkernel();
7729b6b563cSPaul Mackerras 	early_reserve_mem();
7739b6b563cSPaul Mackerras 
77414ed7409SAnton Blanchard 	/* Ensure that total memory size is page-aligned. */
7756fbef13cSTejun Heo 	limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
77695f72d1eSYinghai Lu 	memblock_enforce_memory_limit(limit);
7776ca4f749SHollis Blanchard 
778b32d5d7eSAneesh Kumar K.V #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_PPC_4K_PAGES)
779b32d5d7eSAneesh Kumar K.V 	if (!early_radix_enabled())
780b32d5d7eSAneesh Kumar K.V 		memblock_cap_memory_range(0, 1UL << (H_MAX_PHYSMEM_BITS));
781b32d5d7eSAneesh Kumar K.V #endif
782b32d5d7eSAneesh Kumar K.V 
7831aadc056STejun Heo 	memblock_allow_resize();
78495f72d1eSYinghai Lu 	memblock_dump_all();
7852babf5c2SMichael Ellerman 
786b18f0ae9SChristophe Leroy 	DBG("Phys. mem: %llx\n", (unsigned long long)memblock_phys_mem_size());
7872babf5c2SMichael Ellerman 
7882babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
7892babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
7902babf5c2SMichael Ellerman 	move_device_tree();
7912babf5c2SMichael Ellerman 
79259f57774SNicholas Piggin 	allocate_paca_ptrs();
7931426d5a3SMichael Ellerman 
7949b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
7959b6b563cSPaul Mackerras 
7965a61ef74SNicholas Piggin 	dt_cpu_ftrs_scan();
7975a61ef74SNicholas Piggin 
79825985edcSLucas De Marchi 	/* Retrieve CPU related informations from the flat tree
7993c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
8009b6b563cSPaul Mackerras 	 */
8013c726f8dSBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
80236ae37e3SBenjamin Herrenschmidt 	if (boot_cpuid < 0) {
803d939be3aSMasanari Iida 		printk("Failed to identify boot CPU !\n");
80436ae37e3SBenjamin Herrenschmidt 		BUG();
80536ae37e3SBenjamin Herrenschmidt 	}
8069b6b563cSPaul Mackerras 
807912c0a7fSMichael Ellerman 	save_fscr_to_task();
808912c0a7fSMichael Ellerman 
8097ac87abbSMatt Evans #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
8107ac87abbSMatt Evans 	/* We'll later wait for secondaries to check in; there are
8117ac87abbSMatt Evans 	 * NCPUS-1 non-boot CPUs  :-)
8127ac87abbSMatt Evans 	 */
8137ac87abbSMatt Evans 	spinning_secondaries = boot_cpu_count - 1;
8147ac87abbSMatt Evans #endif
8157ac87abbSMatt Evans 
8161a01dc87SMichael Ellerman 	mmu_early_init_devtree();
8171a01dc87SMichael Ellerman 
81855672ecfSMahesh Salgaonkar #ifdef CONFIG_PPC_POWERNV
81955672ecfSMahesh Salgaonkar 	/* Scan and build the list of machine check recoverable ranges */
82055672ecfSMahesh Salgaonkar 	of_scan_flat_dt(early_init_dt_scan_recoverable_ranges, NULL);
82155672ecfSMahesh Salgaonkar #endif
822da6a97bfSBenjamin Herrenschmidt 	epapr_paravirt_early_init();
82355672ecfSMahesh Salgaonkar 
8243808a889SBenjamin Herrenschmidt 	/* Now try to figure out if we are running on LPAR and so on */
8253808a889SBenjamin Herrenschmidt 	pseries_probe_fw_features();
8263808a889SBenjamin Herrenschmidt 
827d3cd91fbSAneesh Kumar K.V 	/*
828d3cd91fbSAneesh Kumar K.V 	 * Initialize pkey features and default AMR/IAMR values
829d3cd91fbSAneesh Kumar K.V 	 */
830d3cd91fbSAneesh Kumar K.V 	pkey_early_init_devtree();
831d3cd91fbSAneesh Kumar K.V 
832166dd7d3SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PS3
833166dd7d3SBenjamin Herrenschmidt 	/* Identify PS3 firmware */
834166dd7d3SBenjamin Herrenschmidt 	if (of_flat_dt_is_compatible(of_get_flat_dt_root(), "sony,ps3"))
835166dd7d3SBenjamin Herrenschmidt 		powerpc_firmware_features |= FW_FEATURE_PS3_POSSIBLE;
836166dd7d3SBenjamin Herrenschmidt #endif
837166dd7d3SBenjamin Herrenschmidt 
83807fd1761SCyril Bur 	tm_init();
83907fd1761SCyril Bur 
8409b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
8419b6b563cSPaul Mackerras }
8429b6b563cSPaul Mackerras 
843b27652ddSKevin Hao #ifdef CONFIG_RELOCATABLE
844b27652ddSKevin Hao /*
845b27652ddSKevin Hao  * This function run before early_init_devtree, so we have to init
846b27652ddSKevin Hao  * initial_boot_params.
847b27652ddSKevin Hao  */
848b27652ddSKevin Hao void __init early_get_first_memblock_info(void *params, phys_addr_t *size)
849b27652ddSKevin Hao {
850b27652ddSKevin Hao 	/* Setup flat device-tree pointer */
851b27652ddSKevin Hao 	initial_boot_params = params;
852b27652ddSKevin Hao 
853b27652ddSKevin Hao 	/*
854b27652ddSKevin Hao 	 * Scan the memory nodes and set add_mem_to_memblock to 0 to avoid
855b27652ddSKevin Hao 	 * mess the memblock.
856b27652ddSKevin Hao 	 */
857b27652ddSKevin Hao 	add_mem_to_memblock = 0;
858b27652ddSKevin Hao 	of_scan_flat_dt(early_init_dt_scan_root, NULL);
859b27652ddSKevin Hao 	of_scan_flat_dt(early_init_dt_scan_memory_ppc, NULL);
860b27652ddSKevin Hao 	add_mem_to_memblock = 1;
861b27652ddSKevin Hao 
862b27652ddSKevin Hao 	if (size)
863b27652ddSKevin Hao 		*size = first_memblock_size;
864b27652ddSKevin Hao }
865b27652ddSKevin Hao #endif
866b27652ddSKevin Hao 
8679b6b563cSPaul Mackerras /*******
8689b6b563cSPaul Mackerras  *
8699b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
8709b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
8719b6b563cSPaul Mackerras  * are protected by a rw_lock.
8729b6b563cSPaul Mackerras  *
8739b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
8749b6b563cSPaul Mackerras  * this isn't dealt with yet.
8759b6b563cSPaul Mackerras  *
8769b6b563cSPaul Mackerras  *******/
8779b6b563cSPaul Mackerras 
8789b6b563cSPaul Mackerras /**
879b37193b7SBenjamin Herrenschmidt  * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
880b37193b7SBenjamin Herrenschmidt  * @np: device node of the device
881b37193b7SBenjamin Herrenschmidt  *
882b37193b7SBenjamin Herrenschmidt  * This looks for a property "ibm,chip-id" in the node or any
883b37193b7SBenjamin Herrenschmidt  * of its parents and returns its content, or -1 if it cannot
884b37193b7SBenjamin Herrenschmidt  * be found.
885b37193b7SBenjamin Herrenschmidt  */
886b37193b7SBenjamin Herrenschmidt int of_get_ibm_chip_id(struct device_node *np)
887b37193b7SBenjamin Herrenschmidt {
888b37193b7SBenjamin Herrenschmidt 	of_node_get(np);
889b37193b7SBenjamin Herrenschmidt 	while (np) {
8901856f50cSChristophe Jaillet 		u32 chip_id;
891b37193b7SBenjamin Herrenschmidt 
8921856f50cSChristophe Jaillet 		/*
8931856f50cSChristophe Jaillet 		 * Skiboot may produce memory nodes that contain more than one
8941856f50cSChristophe Jaillet 		 * cell in chip-id, we only read the first one here.
8951856f50cSChristophe Jaillet 		 */
8961856f50cSChristophe Jaillet 		if (!of_property_read_u32(np, "ibm,chip-id", &chip_id)) {
897b37193b7SBenjamin Herrenschmidt 			of_node_put(np);
8981856f50cSChristophe Jaillet 			return chip_id;
899b37193b7SBenjamin Herrenschmidt 		}
90016c1d606SMichael Ellerman 
90116c1d606SMichael Ellerman 		np = of_get_next_parent(np);
902b37193b7SBenjamin Herrenschmidt 	}
903b37193b7SBenjamin Herrenschmidt 	return -1;
904b37193b7SBenjamin Herrenschmidt }
905b130e7c0SDan Streetman EXPORT_SYMBOL(of_get_ibm_chip_id);
906b37193b7SBenjamin Herrenschmidt 
9073eb906c6SMichael Ellerman /**
9083eb906c6SMichael Ellerman  * cpu_to_chip_id - Return the cpus chip-id
9093eb906c6SMichael Ellerman  * @cpu: The logical cpu number.
9103eb906c6SMichael Ellerman  *
9113eb906c6SMichael Ellerman  * Return the value of the ibm,chip-id property corresponding to the given
9123eb906c6SMichael Ellerman  * logical cpu number. If the chip-id can not be found, returns -1.
9133eb906c6SMichael Ellerman  */
9143eb906c6SMichael Ellerman int cpu_to_chip_id(int cpu)
9153eb906c6SMichael Ellerman {
9163eb906c6SMichael Ellerman 	struct device_node *np;
9173eb906c6SMichael Ellerman 
9183eb906c6SMichael Ellerman 	np = of_get_cpu_node(cpu, NULL);
9193eb906c6SMichael Ellerman 	if (!np)
9203eb906c6SMichael Ellerman 		return -1;
9213eb906c6SMichael Ellerman 
9223eb906c6SMichael Ellerman 	of_node_put(np);
9233eb906c6SMichael Ellerman 	return of_get_ibm_chip_id(np);
9243eb906c6SMichael Ellerman }
9253eb906c6SMichael Ellerman EXPORT_SYMBOL(cpu_to_chip_id);
9263eb906c6SMichael Ellerman 
927819d5965SSudeep KarkadaNagesha bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
928819d5965SSudeep KarkadaNagesha {
9299f593f13SNicholas Piggin #ifdef CONFIG_SMP
9309f593f13SNicholas Piggin 	/*
9319f593f13SNicholas Piggin 	 * Early firmware scanning must use this rather than
9329f593f13SNicholas Piggin 	 * get_hard_smp_processor_id because we don't have pacas allocated
9339f593f13SNicholas Piggin 	 * until memory topology is discovered.
9349f593f13SNicholas Piggin 	 */
9359f593f13SNicholas Piggin 	if (cpu_to_phys_id != NULL)
9369f593f13SNicholas Piggin 		return (int)phys_id == cpu_to_phys_id[cpu];
9379f593f13SNicholas Piggin #endif
9389f593f13SNicholas Piggin 
939819d5965SSudeep KarkadaNagesha 	return (int)phys_id == get_hard_smp_processor_id(cpu);
940819d5965SSudeep KarkadaNagesha }
941