xref: /openbmc/linux/arch/powerpc/kernel/prom.c (revision dc222fa7737212fe0da513e5b8937c156d02225d)
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 <linux/kernel.h>
159b6b563cSPaul Mackerras #include <linux/string.h>
169b6b563cSPaul Mackerras #include <linux/init.h>
179b6b563cSPaul Mackerras #include <linux/threads.h>
189b6b563cSPaul Mackerras #include <linux/spinlock.h>
199b6b563cSPaul Mackerras #include <linux/types.h>
209b6b563cSPaul Mackerras #include <linux/pci.h>
219b6b563cSPaul Mackerras #include <linux/delay.h>
229b6b563cSPaul Mackerras #include <linux/initrd.h>
239b6b563cSPaul Mackerras #include <linux/bitops.h>
244b16f8e2SPaul Gortmaker #include <linux/export.h>
25dcee3036SMichael Ellerman #include <linux/kexec.h>
260ebfff14SBenjamin Herrenschmidt #include <linux/irq.h>
2795f72d1eSYinghai Lu #include <linux/memblock.h>
281cf3d8b3SNathan Fontenot #include <linux/of.h>
29708b7eefSMarek Szyprowski #include <linux/of_fdt.h>
30060f78c2SRob Herring #include <linux/libfdt.h>
317f92bc56SDaniel Axtens #include <linux/cpu.h>
3265fddcfcSMike Rapoport #include <linux/pgtable.h>
33bd649d40SMichael Ellerman #include <linux/seq_buf.h>
349b6b563cSPaul Mackerras 
359b6b563cSPaul Mackerras #include <asm/rtas.h>
369b6b563cSPaul Mackerras #include <asm/page.h>
379b6b563cSPaul Mackerras #include <asm/processor.h>
389b6b563cSPaul Mackerras #include <asm/irq.h>
399b6b563cSPaul Mackerras #include <asm/io.h>
400cc4746cSMichael Ellerman #include <asm/kdump.h>
419b6b563cSPaul Mackerras #include <asm/smp.h>
429b6b563cSPaul Mackerras #include <asm/mmu.h>
431426d5a3SMichael Ellerman #include <asm/paca.h>
4454820530SMichael Ellerman #include <asm/powernv.h>
459b6b563cSPaul Mackerras #include <asm/iommu.h>
469b6b563cSPaul Mackerras #include <asm/btext.h>
479b6b563cSPaul Mackerras #include <asm/sections.h>
48113fe88eSChristophe Leroy #include <asm/setup.h>
4940ef8cbcSPaul Mackerras #include <asm/pci-bridge.h>
502babf5c2SMichael Ellerman #include <asm/kexec.h>
5114a43e69SBenjamin Herrenschmidt #include <asm/opal.h>
52eb39c880SMahesh Salgaonkar #include <asm/fadump.h>
53da6a97bfSBenjamin Herrenschmidt #include <asm/epapr_hcalls.h>
543808a889SBenjamin Herrenschmidt #include <asm/firmware.h>
555a61ef74SNicholas Piggin #include <asm/dt_cpu_ftrs.h>
566c6ea537SNathan Fontenot #include <asm/drmem.h>
57bb04ffe8SClaudio Carvalho #include <asm/ultravisor.h>
584d5c5badSChristophe Leroy #include <asm/prom.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 
68c1e53367SSrikar Dronamraju int *chip_id_lookup_table;
69c1e53367SSrikar Dronamraju 
709b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
7128897731SOlof Johansson int __initdata iommu_is_off;
729b6b563cSPaul Mackerras int __initdata iommu_force_on;
73cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
74cd3db0c4SBenjamin Herrenschmidt u64 ppc64_rma_size;
75e13d23a4SLaurent Dufour unsigned int boot_cpu_node_count __ro_after_init;
769b6b563cSPaul Mackerras #endif
7703bf469aSBenjamin Herrenschmidt static phys_addr_t first_memblock_size;
787ac87abbSMatt Evans static int __initdata boot_cpu_count;
799b6b563cSPaul Mackerras 
802babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
812babf5c2SMichael Ellerman {
822babf5c2SMichael Ellerman 	if (!p)
832babf5c2SMichael Ellerman 		return 1;
842babf5c2SMichael Ellerman 
852babf5c2SMichael Ellerman 	memory_limit = PAGE_ALIGN(memparse(p, &p));
86a84fcd46SSuzuki Poulose 	DBG("memory limit = 0x%llx\n", memory_limit);
872babf5c2SMichael Ellerman 
882babf5c2SMichael Ellerman 	return 0;
892babf5c2SMichael Ellerman }
902babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
912babf5c2SMichael Ellerman 
92307cfe71SBenjamin Herrenschmidt /*
93307cfe71SBenjamin Herrenschmidt  * overlaps_initrd - check for overlap with page aligned extension of
94307cfe71SBenjamin Herrenschmidt  * initrd.
95307cfe71SBenjamin Herrenschmidt  */
96307cfe71SBenjamin Herrenschmidt static inline int overlaps_initrd(unsigned long start, unsigned long size)
97307cfe71SBenjamin Herrenschmidt {
98307cfe71SBenjamin Herrenschmidt #ifdef CONFIG_BLK_DEV_INITRD
99307cfe71SBenjamin Herrenschmidt 	if (!initrd_start)
100307cfe71SBenjamin Herrenschmidt 		return 0;
101307cfe71SBenjamin Herrenschmidt 
102e96d904eSChristophe Leroy 	return	(start + size) > ALIGN_DOWN(initrd_start, PAGE_SIZE) &&
103b7115316SChristophe Leroy 			start <= ALIGN(initrd_end, PAGE_SIZE);
104307cfe71SBenjamin Herrenschmidt #else
105307cfe71SBenjamin Herrenschmidt 	return 0;
106307cfe71SBenjamin Herrenschmidt #endif
107307cfe71SBenjamin Herrenschmidt }
108307cfe71SBenjamin Herrenschmidt 
1093c607ce2SLinas Vepstas /**
1103c607ce2SLinas Vepstas  * move_device_tree - move tree to an unused area, if needed.
1113c607ce2SLinas Vepstas  *
1123c607ce2SLinas Vepstas  * The device tree may be allocated beyond our memory limit, or inside the
113307cfe71SBenjamin Herrenschmidt  * crash kernel region for kdump, or within the page aligned range of initrd.
114307cfe71SBenjamin Herrenschmidt  * If so, move it out of the way.
1152babf5c2SMichael Ellerman  */
11618f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
1172babf5c2SMichael Ellerman {
1182babf5c2SMichael Ellerman 	unsigned long start, size;
1192babf5c2SMichael Ellerman 	void *p;
1202babf5c2SMichael Ellerman 
1212babf5c2SMichael Ellerman 	DBG("-> move_device_tree\n");
1222babf5c2SMichael Ellerman 
1232babf5c2SMichael Ellerman 	start = __pa(initial_boot_params);
124060f78c2SRob Herring 	size = fdt_totalsize(initial_boot_params);
1252babf5c2SMichael Ellerman 
1266dd22700SScott Wood 	if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
127f242e0acSChristophe Leroy 	    !memblock_is_memory(start + size - 1) ||
128f242e0acSChristophe Leroy 	    overlaps_crashkernel(start, size) || overlaps_initrd(start, size)) {
1291269f7b8SChristophe Leroy 		p = memblock_alloc_raw(size, PAGE_SIZE);
1301269f7b8SChristophe Leroy 		if (!p)
1311269f7b8SChristophe Leroy 			panic("Failed to allocate %lu bytes to move device tree\n",
1321269f7b8SChristophe Leroy 			      size);
1332babf5c2SMichael Ellerman 		memcpy(p, initial_boot_params, size);
134060f78c2SRob Herring 		initial_boot_params = p;
135b18f0ae9SChristophe Leroy 		DBG("Moved device tree to 0x%px\n", p);
1362babf5c2SMichael Ellerman 	}
1372babf5c2SMichael Ellerman 
1382babf5c2SMichael Ellerman 	DBG("<- move_device_tree\n");
1392babf5c2SMichael Ellerman }
1409b6b563cSPaul Mackerras 
141d205819eSPaul Mackerras /*
142335e1a91SHaren Myneni  * ibm,pa/pi-features is a per-cpu property that contains a string of
143d205819eSPaul Mackerras  * attribute descriptors, each of which has a 2 byte header plus up
144d205819eSPaul Mackerras  * to 254 bytes worth of processor attribute bits.  First header
145d205819eSPaul Mackerras  * byte specifies the number of bytes following the header.
146d205819eSPaul Mackerras  * Second header byte is an "attribute-specifier" type, of which
147d205819eSPaul Mackerras  * zero is the only currently-defined value.
148d205819eSPaul Mackerras  * Implementation:  Pass in the byte and bit offset for the feature
149d205819eSPaul Mackerras  * that we are interested in.  The function will return -1 if the
150d205819eSPaul Mackerras  * pa-features property is missing, or a 1/0 to indicate if the feature
151d205819eSPaul Mackerras  * is supported/not supported.  Note that the bit numbers are
152d205819eSPaul Mackerras  * big-endian to match the definition in PAPR.
153d205819eSPaul Mackerras  */
154335e1a91SHaren Myneni struct ibm_feature {
155d205819eSPaul Mackerras 	unsigned long	cpu_features;	/* CPU_FTR_xxx bit */
15644ae3ab3SMatt Evans 	unsigned long	mmu_features;	/* MMU_FTR_xxx bit */
157d205819eSPaul Mackerras 	unsigned int	cpu_user_ftrs;	/* PPC_FEATURE_xxx bit */
158beff8237SAnton Blanchard 	unsigned int	cpu_user_ftrs2;	/* PPC_FEATURE2_xxx bit */
159335e1a91SHaren Myneni 	unsigned char	pabyte;		/* byte number in ibm,pa/pi-features */
160d205819eSPaul Mackerras 	unsigned char	pabit;		/* bit number (big-endian) */
161d205819eSPaul Mackerras 	unsigned char	invert;		/* if 1, pa bit set => clear feature */
162335e1a91SHaren Myneni };
163335e1a91SHaren Myneni 
164335e1a91SHaren Myneni static struct ibm_feature ibm_pa_features[] __initdata = {
165e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 0, .cpu_user_ftrs = PPC_FEATURE_HAS_MMU },
166e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 1, .cpu_user_ftrs = PPC_FEATURE_HAS_FPU },
167e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 3, .cpu_features  = CPU_FTR_CTRL },
168e9eb0278SMichael Ellerman 	{ .pabyte = 0,  .pabit = 6, .cpu_features  = CPU_FTR_NOEXECUTE },
169e9eb0278SMichael Ellerman 	{ .pabyte = 1,  .pabit = 2, .mmu_features  = MMU_FTR_CI_LARGE_PAGE },
170d957fb4dSMichael Neuling #ifdef CONFIG_PPC_RADIX_MMU
1719a77c4a0SNicholas Piggin 	{ .pabyte = 40, .pabit = 0, .mmu_features  = MMU_FTR_TYPE_RADIX | MMU_FTR_GTSE },
172d957fb4dSMichael Neuling #endif
173e9eb0278SMichael Ellerman 	{ .pabyte = 5,  .pabit = 0, .cpu_features  = CPU_FTR_REAL_LE,
174e9eb0278SMichael Ellerman 				    .cpu_user_ftrs = PPC_FEATURE_TRUE_LE },
1759e819963SAneesh Kumar K.V 	/*
1764705e024SAnton Blanchard 	 * If the kernel doesn't support TM (ie CONFIG_PPC_TRANSACTIONAL_MEM=n),
1774705e024SAnton Blanchard 	 * we don't want to turn on TM here, so we use the *_COMP versions
1784705e024SAnton Blanchard 	 * which are 0 if the kernel doesn't support TM.
1799e819963SAneesh Kumar K.V 	 */
180e9eb0278SMichael Ellerman 	{ .pabyte = 22, .pabit = 0, .cpu_features = CPU_FTR_TM_COMP,
181e9eb0278SMichael Ellerman 	  .cpu_user_ftrs2 = PPC_FEATURE2_HTM_COMP | PPC_FEATURE2_HTM_NOSC_COMP },
1828f45ca3fSRavi Bangoria 
1838f45ca3fSRavi Bangoria 	{ .pabyte = 64, .pabit = 0, .cpu_features = CPU_FTR_DAWR1 },
184d205819eSPaul Mackerras };
185d205819eSPaul Mackerras 
186335e1a91SHaren Myneni /*
187335e1a91SHaren Myneni  * ibm,pi-features property provides the support of processor specific
188335e1a91SHaren Myneni  * options not described in ibm,pa-features. Right now use byte 0, bit 3
189335e1a91SHaren Myneni  * which indicates the occurrence of DSI interrupt when the paste operation
190335e1a91SHaren Myneni  * on the suspended NX window.
191335e1a91SHaren Myneni  */
192335e1a91SHaren Myneni static struct ibm_feature ibm_pi_features[] __initdata = {
193335e1a91SHaren Myneni 	{ .pabyte = 0, .pabit = 3, .mmu_features  = MMU_FTR_NX_DSI },
194335e1a91SHaren Myneni };
195335e1a91SHaren Myneni 
1969d0c4dfeSRob Herring static void __init scan_features(unsigned long node, const unsigned char *ftrs,
197974a76f5SPaul Mackerras 				 unsigned long tablelen,
198335e1a91SHaren Myneni 				 struct ibm_feature *fp,
199974a76f5SPaul Mackerras 				 unsigned long ft_size)
200d205819eSPaul Mackerras {
201974a76f5SPaul Mackerras 	unsigned long i, len, bit;
202d205819eSPaul Mackerras 
203d205819eSPaul Mackerras 	/* find descriptor with type == 0 */
204d205819eSPaul Mackerras 	for (;;) {
205d205819eSPaul Mackerras 		if (tablelen < 3)
206d205819eSPaul Mackerras 			return;
207974a76f5SPaul Mackerras 		len = 2 + ftrs[0];
208d205819eSPaul Mackerras 		if (tablelen < len)
209d205819eSPaul Mackerras 			return;		/* descriptor 0 not found */
210974a76f5SPaul Mackerras 		if (ftrs[1] == 0)
211d205819eSPaul Mackerras 			break;
212d205819eSPaul Mackerras 		tablelen -= len;
213974a76f5SPaul Mackerras 		ftrs += len;
214d205819eSPaul Mackerras 	}
215d205819eSPaul Mackerras 
216d205819eSPaul Mackerras 	/* loop over bits we know about */
217974a76f5SPaul Mackerras 	for (i = 0; i < ft_size; ++i, ++fp) {
218974a76f5SPaul Mackerras 		if (fp->pabyte >= ftrs[0])
219d205819eSPaul Mackerras 			continue;
220974a76f5SPaul Mackerras 		bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
221d205819eSPaul Mackerras 		if (bit ^ fp->invert) {
222d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_features;
223d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
224beff8237SAnton Blanchard 			cur_cpu_spec->cpu_user_features2 |= fp->cpu_user_ftrs2;
22544ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features |= fp->mmu_features;
226d205819eSPaul Mackerras 		} else {
227d205819eSPaul Mackerras 			cur_cpu_spec->cpu_features &= ~fp->cpu_features;
228d205819eSPaul Mackerras 			cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
229beff8237SAnton Blanchard 			cur_cpu_spec->cpu_user_features2 &= ~fp->cpu_user_ftrs2;
23044ae3ab3SMatt Evans 			cur_cpu_spec->mmu_features &= ~fp->mmu_features;
231d205819eSPaul Mackerras 		}
232d205819eSPaul Mackerras 	}
233d205819eSPaul Mackerras }
234d205819eSPaul Mackerras 
235335e1a91SHaren Myneni static void __init check_cpu_features(unsigned long node, char *name,
236335e1a91SHaren Myneni 				      struct ibm_feature *fp,
237335e1a91SHaren Myneni 				      unsigned long size)
238974a76f5SPaul Mackerras {
2399d0c4dfeSRob Herring 	const unsigned char *pa_ftrs;
2409d0c4dfeSRob Herring 	int tablelen;
241974a76f5SPaul Mackerras 
242335e1a91SHaren Myneni 	pa_ftrs = of_get_flat_dt_prop(node, name, &tablelen);
243974a76f5SPaul Mackerras 	if (pa_ftrs == NULL)
244974a76f5SPaul Mackerras 		return;
245974a76f5SPaul Mackerras 
246335e1a91SHaren Myneni 	scan_features(node, pa_ftrs, tablelen, fp, size);
247974a76f5SPaul Mackerras }
248974a76f5SPaul Mackerras 
249387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
2509c61f7a0SAnshuman Khandual static void __init init_mmu_slb_size(unsigned long node)
251584f8b71SMichael Neuling {
2529d0c4dfeSRob Herring 	const __be32 *slb_size_ptr;
253584f8b71SMichael Neuling 
2549c61f7a0SAnshuman Khandual 	slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL) ? :
2559c61f7a0SAnshuman Khandual 			of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
2569c61f7a0SAnshuman Khandual 
2579c61f7a0SAnshuman Khandual 	if (slb_size_ptr)
258dc0e643aSIan Munsie 		mmu_slb_size = be32_to_cpup(slb_size_ptr);
259584f8b71SMichael Neuling }
260584f8b71SMichael Neuling #else
2619c61f7a0SAnshuman Khandual #define init_mmu_slb_size(node) do { } while(0)
262584f8b71SMichael Neuling #endif
263584f8b71SMichael Neuling 
264974a76f5SPaul Mackerras static struct feature_property {
265974a76f5SPaul Mackerras 	const char *name;
266974a76f5SPaul Mackerras 	u32 min_value;
267974a76f5SPaul Mackerras 	unsigned long cpu_feature;
268974a76f5SPaul Mackerras 	unsigned long cpu_user_ftr;
269974a76f5SPaul Mackerras } feature_properties[] __initdata = {
270974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC
271974a76f5SPaul Mackerras 	{"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
272974a76f5SPaul Mackerras 	{"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
273974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */
274b962ce9dSMichael Neuling #ifdef CONFIG_VSX
275b962ce9dSMichael Neuling 	/* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
276b962ce9dSMichael Neuling 	{"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
277b962ce9dSMichael Neuling #endif /* CONFIG_VSX */
278974a76f5SPaul Mackerras #ifdef CONFIG_PPC64
279974a76f5SPaul Mackerras 	{"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
280974a76f5SPaul Mackerras 	{"ibm,purr", 1, CPU_FTR_PURR, 0},
281974a76f5SPaul Mackerras 	{"ibm,spurr", 1, CPU_FTR_SPURR, 0},
282974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */
283974a76f5SPaul Mackerras };
284974a76f5SPaul Mackerras 
28514b3d926SValentine Barshak #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
2861ef1dd9cSNathan Chancellor static __init void identical_pvr_fixup(unsigned long node)
28714b3d926SValentine Barshak {
28814b3d926SValentine Barshak 	unsigned int pvr;
2899d0c4dfeSRob Herring 	const char *model = of_get_flat_dt_prop(node, "model", NULL);
29014b3d926SValentine Barshak 
29114b3d926SValentine Barshak 	/*
29214b3d926SValentine Barshak 	 * Since 440GR(x)/440EP(x) processors have the same pvr,
29314b3d926SValentine Barshak 	 * we check the node path and set bit 28 in the cur_cpu_spec
29414b3d926SValentine Barshak 	 * pvr for EP(x) processor version. This bit is always 0 in
29514b3d926SValentine Barshak 	 * the "real" pvr. Then we call identify_cpu again with
29614b3d926SValentine Barshak 	 * the new logical pvr to enable FPU support.
29714b3d926SValentine Barshak 	 */
29814b3d926SValentine Barshak 	if (model && strstr(model, "440EP")) {
29914b3d926SValentine Barshak 		pvr = cur_cpu_spec->pvr_value | 0x8;
30014b3d926SValentine Barshak 		identify_cpu(0, pvr);
30114b3d926SValentine Barshak 		DBG("Using logical pvr %x for %s\n", pvr, model);
30214b3d926SValentine Barshak 	}
30314b3d926SValentine Barshak }
30414b3d926SValentine Barshak #else
30514b3d926SValentine Barshak #define identical_pvr_fixup(node) do { } while(0)
30614b3d926SValentine Barshak #endif
30714b3d926SValentine Barshak 
308974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node)
309974a76f5SPaul Mackerras {
3104f1f40f7SMathieu Malaterre 	int i;
311974a76f5SPaul Mackerras 	struct feature_property *fp = feature_properties;
312dc0e643aSIan Munsie 	const __be32 *prop;
313974a76f5SPaul Mackerras 
3144f1f40f7SMathieu Malaterre 	for (i = 0; i < (int)ARRAY_SIZE(feature_properties); ++i, ++fp) {
315974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, fp->name, NULL);
316dc0e643aSIan Munsie 		if (prop && be32_to_cpup(prop) >= fp->min_value) {
317974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_features |= fp->cpu_feature;
318974a76f5SPaul Mackerras 			cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
319974a76f5SPaul Mackerras 		}
320974a76f5SPaul Mackerras 	}
321974a76f5SPaul Mackerras }
322974a76f5SPaul Mackerras 
3239b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
3244df20460SAnton Blanchard 					  const char *uname, int depth,
3254df20460SAnton Blanchard 					  void *data)
3269b6b563cSPaul Mackerras {
3279d0c4dfeSRob Herring 	const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
328dc0e643aSIan Munsie 	const __be32 *prop;
329dc0e643aSIan Munsie 	const __be32 *intserv;
3304df20460SAnton Blanchard 	int i, nthreads;
3319d0c4dfeSRob Herring 	int len;
3329d07bc84SBenjamin Herrenschmidt 	int found = -1;
333208b3a4cSBenjamin Herrenschmidt 	int found_thread = 0;
3349b6b563cSPaul Mackerras 
3359b6b563cSPaul Mackerras 	/* We are scanning "cpu" nodes only */
3369b6b563cSPaul Mackerras 	if (type == NULL || strcmp(type, "cpu") != 0)
3379b6b563cSPaul Mackerras 		return 0;
3389b6b563cSPaul Mackerras 
33964fdcbccSMichael Ellerman 	if (IS_ENABLED(CONFIG_PPC64))
340e13d23a4SLaurent Dufour 		boot_cpu_node_count++;
341e13d23a4SLaurent Dufour 
3424df20460SAnton Blanchard 	/* Get physical cpuid */
3434df20460SAnton Blanchard 	intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
344e16c8765SAndy Fleming 	if (!intserv)
345e16c8765SAndy Fleming 		intserv = of_get_flat_dt_prop(node, "reg", &len);
346e16c8765SAndy Fleming 
3474df20460SAnton Blanchard 	nthreads = len / sizeof(int);
3484df20460SAnton Blanchard 
3494df20460SAnton Blanchard 	/*
3504df20460SAnton Blanchard 	 * Now see if any of these threads match our boot cpu.
3514df20460SAnton Blanchard 	 * NOTE: This must match the parsing done in smp_setup_cpu_maps.
3524df20460SAnton Blanchard 	 */
3534df20460SAnton Blanchard 	for (i = 0; i < nthreads; i++) {
354dc0e643aSIan Munsie 		if (be32_to_cpu(intserv[i]) ==
355060f78c2SRob Herring 			fdt_boot_cpuid_phys(initial_boot_params)) {
3569d07bc84SBenjamin Herrenschmidt 			found = boot_cpu_count;
357208b3a4cSBenjamin Herrenschmidt 			found_thread = i;
358208b3a4cSBenjamin Herrenschmidt 		}
3594df20460SAnton Blanchard #ifdef CONFIG_SMP
3604df20460SAnton Blanchard 		/* logical cpu id is always 0 on UP kernels */
3619d07bc84SBenjamin Herrenschmidt 		boot_cpu_count++;
3624df20460SAnton Blanchard #endif
3634df20460SAnton Blanchard 	}
3644df20460SAnton Blanchard 
3657222f779SBenjamin Herrenschmidt 	/* Not the boot CPU */
3667222f779SBenjamin Herrenschmidt 	if (found < 0)
3677222f779SBenjamin Herrenschmidt 		return 0;
3687222f779SBenjamin Herrenschmidt 
3699d07bc84SBenjamin Herrenschmidt 	DBG("boot cpu: logical %d physical %d\n", found,
370dc0e643aSIan Munsie 	    be32_to_cpu(intserv[found_thread]));
3719d07bc84SBenjamin Herrenschmidt 	boot_cpuid = found;
372974a76f5SPaul Mackerras 
373*dc222fa7SNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC64))
374*dc222fa7SNicholas Piggin 		boot_cpu_hwid = be32_to_cpu(intserv[found_thread]);
3752e7f1e2bSMichael Ellerman 
376974a76f5SPaul Mackerras 	/*
377974a76f5SPaul Mackerras 	 * PAPR defines "logical" PVR values for cpus that
378974a76f5SPaul Mackerras 	 * meet various levels of the architecture:
379974a76f5SPaul Mackerras 	 * 0x0f000001	Architecture version 2.04
380974a76f5SPaul Mackerras 	 * 0x0f000002	Architecture version 2.05
381974a76f5SPaul Mackerras 	 * If the cpu-version property in the cpu node contains
382974a76f5SPaul Mackerras 	 * such a value, we call identify_cpu again with the
383974a76f5SPaul Mackerras 	 * logical PVR value in order to use the cpu feature
384974a76f5SPaul Mackerras 	 * bits appropriate for the architecture level.
385974a76f5SPaul Mackerras 	 *
386974a76f5SPaul Mackerras 	 * A POWER6 partition in "POWER6 architected" mode
387974a76f5SPaul Mackerras 	 * uses the 0x0f000002 PVR value; in POWER5+ mode
388974a76f5SPaul Mackerras 	 * it uses 0x0f000001.
3895a61ef74SNicholas Piggin 	 *
3905a61ef74SNicholas Piggin 	 * If we're using device tree CPU feature discovery then we don't
3915a61ef74SNicholas Piggin 	 * support the cpu-version property, and it's the responsibility of the
3925a61ef74SNicholas Piggin 	 * firmware/hypervisor to provide the correct feature set for the
3935a61ef74SNicholas Piggin 	 * architecture level via the ibm,powerpc-cpu-features binding.
394974a76f5SPaul Mackerras 	 */
3955a61ef74SNicholas Piggin 	if (!dt_cpu_ftrs_in_use()) {
396974a76f5SPaul Mackerras 		prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
39748b7019bSMichael Ellerman 		if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000) {
398dc0e643aSIan Munsie 			identify_cpu(0, be32_to_cpup(prop));
39948b7019bSMichael Ellerman 			seq_buf_printf(&ppc_hw_desc, "0x%04x ", be32_to_cpup(prop));
40048b7019bSMichael Ellerman 		}
40114b3d926SValentine Barshak 
402974a76f5SPaul Mackerras 		check_cpu_feature_properties(node);
403335e1a91SHaren Myneni 		check_cpu_features(node, "ibm,pa-features", ibm_pa_features,
404335e1a91SHaren Myneni 				   ARRAY_SIZE(ibm_pa_features));
405335e1a91SHaren Myneni 		check_cpu_features(node, "ibm,pi-features", ibm_pi_features,
406335e1a91SHaren Myneni 				   ARRAY_SIZE(ibm_pi_features));
4075a61ef74SNicholas Piggin 	}
4085a61ef74SNicholas Piggin 
4095a61ef74SNicholas Piggin 	identical_pvr_fixup(node);
4109c61f7a0SAnshuman Khandual 	init_mmu_slb_size(node);
411d205819eSPaul Mackerras 
4124a85b31dSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
4135a61ef74SNicholas Piggin 	if (nthreads == 1)
4144df20460SAnton Blanchard 		cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
4155a61ef74SNicholas Piggin 	else if (!dt_cpu_ftrs_in_use())
4165a61ef74SNicholas Piggin 		cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
4179b6b563cSPaul Mackerras #endif
4185a61ef74SNicholas Piggin 
4199b6b563cSPaul Mackerras 	return 0;
4209b6b563cSPaul Mackerras }
4219b6b563cSPaul Mackerras 
422e51df2c1SAnton Blanchard static int __init early_init_dt_scan_chosen_ppc(unsigned long node,
423e51df2c1SAnton Blanchard 						const char *uname,
42432c97689SGrant Likely 						int depth, void *data)
4259b6b563cSPaul Mackerras {
4269d0c4dfeSRob Herring 	const unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
4279b6b563cSPaul Mackerras 
42832c97689SGrant Likely 	/* Use common scan routine to determine if this is the chosen node */
42960f20d84SRob Herring 	if (early_init_dt_scan_chosen(data) < 0)
43032c97689SGrant Likely 		return 0;
43132c97689SGrant Likely 
4329b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4339b6b563cSPaul Mackerras 	/* check if iommu is forced on or off */
4343c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
4359b6b563cSPaul Mackerras 		iommu_is_off = 1;
4363c726f8dSBenjamin Herrenschmidt 	if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
4379b6b563cSPaul Mackerras 		iommu_force_on = 1;
4389b6b563cSPaul Mackerras #endif
4399b6b563cSPaul Mackerras 
4402babf5c2SMichael Ellerman 	/* mem=x on the command line is the preferred mechanism */
4413c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
4429b6b563cSPaul Mackerras 	if (lprop)
4439b6b563cSPaul Mackerras 		memory_limit = *lprop;
4449b6b563cSPaul Mackerras 
4459b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4463c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
4479b6b563cSPaul Mackerras 	if (lprop)
4489b6b563cSPaul Mackerras 		tce_alloc_start = *lprop;
4493c726f8dSBenjamin Herrenschmidt 	lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
4509b6b563cSPaul Mackerras 	if (lprop)
4519b6b563cSPaul Mackerras 		tce_alloc_end = *lprop;
4529b6b563cSPaul Mackerras #endif
4539b6b563cSPaul Mackerras 
454da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
45563277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
456dcee3036SMichael Ellerman 	if (lprop)
457dcee3036SMichael Ellerman 		crashk_res.start = *lprop;
458dcee3036SMichael Ellerman 
45963277161SStephen Rothwell 	lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
460dcee3036SMichael Ellerman 	if (lprop)
461dcee3036SMichael Ellerman 		crashk_res.end = crashk_res.start + *lprop - 1;
462dcee3036SMichael Ellerman #endif
46332c97689SGrant Likely 
46432c97689SGrant Likely 	/* break now */
46532c97689SGrant Likely 	return 1;
4669b6b563cSPaul Mackerras }
4679b6b563cSPaul Mackerras 
4686aba0c84SAneesh Kumar K.V /*
4696aba0c84SAneesh Kumar K.V  * Compare the range against max mem limit and update
4706aba0c84SAneesh Kumar K.V  * size if it cross the limit.
4716aba0c84SAneesh Kumar K.V  */
4726aba0c84SAneesh Kumar K.V 
4736aba0c84SAneesh Kumar K.V #ifdef CONFIG_SPARSEMEM
474d276960dSNick Child static bool __init validate_mem_limit(u64 base, u64 *size)
4756aba0c84SAneesh Kumar K.V {
4766aba0c84SAneesh Kumar K.V 	u64 max_mem = 1UL << (MAX_PHYSMEM_BITS);
4776aba0c84SAneesh Kumar K.V 
4786aba0c84SAneesh Kumar K.V 	if (base >= max_mem)
4796aba0c84SAneesh Kumar K.V 		return false;
4806aba0c84SAneesh Kumar K.V 	if ((base + *size) > max_mem)
4816aba0c84SAneesh Kumar K.V 		*size = max_mem - base;
4826aba0c84SAneesh Kumar K.V 	return true;
4836aba0c84SAneesh Kumar K.V }
4846aba0c84SAneesh Kumar K.V #else
485d276960dSNick Child static bool __init validate_mem_limit(u64 base, u64 *size)
4866aba0c84SAneesh Kumar K.V {
4876aba0c84SAneesh Kumar K.V 	return true;
4886aba0c84SAneesh Kumar K.V }
4896aba0c84SAneesh Kumar K.V #endif
4906aba0c84SAneesh Kumar K.V 
4910204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
4920204568aSPaul Mackerras /*
4936c6ea537SNathan Fontenot  * Interpret the ibm dynamic reconfiguration memory LMBs.
4940204568aSPaul Mackerras  * This contains a list of memory blocks along with NUMA affinity
4950204568aSPaul Mackerras  * information.
4960204568aSPaul Mackerras  */
497adfefc60SHari Bathini static int  __init early_init_drmem_lmb(struct drmem_lmb *lmb,
498adfefc60SHari Bathini 					const __be32 **usm,
499adfefc60SHari Bathini 					void *data)
5000204568aSPaul Mackerras {
5016c6ea537SNathan Fontenot 	u64 base, size;
5026c6ea537SNathan Fontenot 	int is_kexec_kdump = 0, rngs;
5030204568aSPaul Mackerras 
5046c6ea537SNathan Fontenot 	base = lmb->base_addr;
5056c6ea537SNathan Fontenot 	size = drmem_lmb_size();
5066c6ea537SNathan Fontenot 	rngs = 1;
5070204568aSPaul Mackerras 
5086c6ea537SNathan Fontenot 	/*
5096c6ea537SNathan Fontenot 	 * Skip this block if the reserved bit is set in flags
5106c6ea537SNathan Fontenot 	 * or if the block is not assigned to this partition.
5116c6ea537SNathan Fontenot 	 */
5126c6ea537SNathan Fontenot 	if ((lmb->flags & DRCONF_MEM_RESERVED) ||
5136c6ea537SNathan Fontenot 	    !(lmb->flags & DRCONF_MEM_ASSIGNED))
514adfefc60SHari Bathini 		return 0;
5150204568aSPaul Mackerras 
5166c6ea537SNathan Fontenot 	if (*usm)
517cf00085dSChandru 		is_kexec_kdump = 1;
518cf00085dSChandru 
519cf00085dSChandru 	if (is_kexec_kdump) {
520cf00085dSChandru 		/*
5216c6ea537SNathan Fontenot 		 * For each memblock in ibm,dynamic-memory, a
5226c6ea537SNathan Fontenot 		 * corresponding entry in linux,drconf-usable-memory
5236c6ea537SNathan Fontenot 		 * property contains a counter 'p' followed by 'p'
5246c6ea537SNathan Fontenot 		 * (base, size) duple. Now read the counter from
525cf00085dSChandru 		 * linux,drconf-usable-memory property
526cf00085dSChandru 		 */
5276c6ea537SNathan Fontenot 		rngs = dt_mem_next_cell(dt_root_size_cells, usm);
528cf00085dSChandru 		if (!rngs) /* there are no (base, size) duple */
529adfefc60SHari Bathini 			return 0;
530cf00085dSChandru 	}
5316c6ea537SNathan Fontenot 
532cf00085dSChandru 	do {
533cf00085dSChandru 		if (is_kexec_kdump) {
5346c6ea537SNathan Fontenot 			base = dt_mem_next_cell(dt_root_addr_cells, usm);
5356c6ea537SNathan Fontenot 			size = dt_mem_next_cell(dt_root_size_cells, usm);
536cf00085dSChandru 		}
5376c6ea537SNathan Fontenot 
5380204568aSPaul Mackerras 		if (iommu_is_off) {
5390204568aSPaul Mackerras 			if (base >= 0x80000000ul)
5400204568aSPaul Mackerras 				continue;
5410204568aSPaul Mackerras 			if ((base + size) > 0x80000000ul)
5420204568aSPaul Mackerras 				size = 0x80000000ul - base;
5430204568aSPaul Mackerras 		}
5446c6ea537SNathan Fontenot 
545b6eca183SLeonardo Bras 		if (!validate_mem_limit(base, &size))
546b6eca183SLeonardo Bras 			continue;
547b6eca183SLeonardo Bras 
5486c6ea537SNathan Fontenot 		DBG("Adding: %llx -> %llx\n", base, size);
54995f72d1eSYinghai Lu 		memblock_add(base, size);
550b6eca183SLeonardo Bras 
551b6eca183SLeonardo Bras 		if (lmb->flags & DRCONF_MEM_HOTREMOVABLE)
552b6eca183SLeonardo Bras 			memblock_mark_hotplug(base, size);
553cf00085dSChandru 	} while (--rngs);
554adfefc60SHari Bathini 
555adfefc60SHari Bathini 	return 0;
5560204568aSPaul Mackerras }
5570204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
5589b6b563cSPaul Mackerras 
5591f012283SRob Herring static int __init early_init_dt_scan_memory_ppc(void)
5609b6b563cSPaul Mackerras {
5616c6ea537SNathan Fontenot #ifdef CONFIG_PPC_PSERIES
5621f012283SRob Herring 	const void *fdt = initial_boot_params;
5631f012283SRob Herring 	int node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
5641f012283SRob Herring 
5651f012283SRob Herring 	if (node > 0)
566adfefc60SHari Bathini 		walk_drmem_lmbs_early(node, NULL, early_init_drmem_lmb);
5671f012283SRob Herring 
5686c6ea537SNathan Fontenot #endif
5690204568aSPaul Mackerras 
5701f012283SRob Herring 	return early_init_dt_scan_memory();
57151975db0SGrant Likely }
5729b6b563cSPaul Mackerras 
573b27652ddSKevin Hao /*
574b27652ddSKevin Hao  * For a relocatable kernel, we need to get the memstart_addr first,
575b27652ddSKevin Hao  * then use it to calculate the virtual kernel start address. This has
576b27652ddSKevin Hao  * to happen at a very early stage (before machine_init). In this case,
577b27652ddSKevin Hao  * we just want to get the memstart_address and would not like to mess the
578b27652ddSKevin Hao  * memblock at this stage. So introduce a variable to skip the memblock_add()
579b27652ddSKevin Hao  * for this reason.
580b27652ddSKevin Hao  */
581b27652ddSKevin Hao #ifdef CONFIG_RELOCATABLE
582b27652ddSKevin Hao static int add_mem_to_memblock = 1;
583b27652ddSKevin Hao #else
584b27652ddSKevin Hao #define add_mem_to_memblock 1
585b27652ddSKevin Hao #endif
586b27652ddSKevin Hao 
58751975db0SGrant Likely void __init early_init_dt_add_memory_arch(u64 base, u64 size)
58851975db0SGrant Likely {
589cd3db0c4SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
5909b6b563cSPaul Mackerras 	if (iommu_is_off) {
5919b6b563cSPaul Mackerras 		if (base >= 0x80000000ul)
59251975db0SGrant Likely 			return;
5939b6b563cSPaul Mackerras 		if ((base + size) > 0x80000000ul)
5949b6b563cSPaul Mackerras 			size = 0x80000000ul - base;
5959b6b563cSPaul Mackerras 	}
5969b6b563cSPaul Mackerras #endif
59703bf469aSBenjamin Herrenschmidt 	/* Keep track of the beginning of memory -and- the size of
59803bf469aSBenjamin Herrenschmidt 	 * the very first block in the device-tree as it represents
59903bf469aSBenjamin Herrenschmidt 	 * the RMA on ppc64 server
60003bf469aSBenjamin Herrenschmidt 	 */
60103bf469aSBenjamin Herrenschmidt 	if (base < memstart_addr) {
60203bf469aSBenjamin Herrenschmidt 		memstart_addr = base;
60303bf469aSBenjamin Herrenschmidt 		first_memblock_size = size;
60403bf469aSBenjamin Herrenschmidt 	}
605cd3db0c4SBenjamin Herrenschmidt 
606cd3db0c4SBenjamin Herrenschmidt 	/* Add the chunk to the MEMBLOCK list */
6076aba0c84SAneesh Kumar K.V 	if (add_mem_to_memblock) {
6086aba0c84SAneesh Kumar K.V 		if (validate_mem_limit(base, &size))
609cd3db0c4SBenjamin Herrenschmidt 			memblock_add(base, size);
6109b6b563cSPaul Mackerras 	}
6116aba0c84SAneesh Kumar K.V }
61237dd2badSKumar Gala 
613c039e3a8SBenjamin Herrenschmidt static void __init early_reserve_mem_dt(void)
6140962e800SJeremy Kerr {
6159d0c4dfeSRob Herring 	unsigned long i, dt_root;
6169d0c4dfeSRob Herring 	int len;
6170962e800SJeremy Kerr 	const __be32 *prop;
6180962e800SJeremy Kerr 
61924bbd929SArd Biesheuvel 	early_init_fdt_reserve_self();
620edba274cSRob Herring 	early_init_fdt_scan_reserved_mem();
621edba274cSRob Herring 
6220962e800SJeremy Kerr 	dt_root = of_get_flat_dt_root();
6230962e800SJeremy Kerr 
6240962e800SJeremy Kerr 	prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
6250962e800SJeremy Kerr 
6260962e800SJeremy Kerr 	if (!prop)
627c039e3a8SBenjamin Herrenschmidt 		return;
628c039e3a8SBenjamin Herrenschmidt 
629c039e3a8SBenjamin Herrenschmidt 	DBG("Found new-style reserved-ranges\n");
6300962e800SJeremy Kerr 
6310962e800SJeremy Kerr 	/* Each reserved range is an (address,size) pair, 2 cells each,
6320962e800SJeremy Kerr 	 * totalling 4 cells per range. */
6330962e800SJeremy Kerr 	for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
6340962e800SJeremy Kerr 		u64 base, size;
6350962e800SJeremy Kerr 
6360962e800SJeremy Kerr 		base = of_read_number(prop + (i * 4) + 0, 2);
6370962e800SJeremy Kerr 		size = of_read_number(prop + (i * 4) + 2, 2);
6380962e800SJeremy Kerr 
639c039e3a8SBenjamin Herrenschmidt 		if (size) {
640c039e3a8SBenjamin Herrenschmidt 			DBG("reserving: %llx -> %llx\n", base, size);
6410962e800SJeremy Kerr 			memblock_reserve(base, size);
6420962e800SJeremy Kerr 		}
643c039e3a8SBenjamin Herrenschmidt 	}
6440962e800SJeremy Kerr }
6450962e800SJeremy Kerr 
6469b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
6479b6b563cSPaul Mackerras {
648dc0e643aSIan Munsie 	__be64 *reserve_map;
6499b6b563cSPaul Mackerras 
650dc0e643aSIan Munsie 	reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
651060f78c2SRob Herring 			fdt_off_mem_rsvmap(initial_boot_params));
6524d1f3f25SJimi Xenidis 
653c039e3a8SBenjamin Herrenschmidt 	/* Look for the new "reserved-regions" property in the DT */
654c039e3a8SBenjamin Herrenschmidt 	early_reserve_mem_dt();
6550962e800SJeremy Kerr 
65630437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
657c039e3a8SBenjamin Herrenschmidt 	/* Then reserve the initrd, if any */
658c039e3a8SBenjamin Herrenschmidt 	if (initrd_start && (initrd_end > initrd_start)) {
659e96d904eSChristophe Leroy 		memblock_reserve(ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
660b7115316SChristophe Leroy 			ALIGN(initrd_end, PAGE_SIZE) -
661e96d904eSChristophe Leroy 			ALIGN_DOWN(initrd_start, PAGE_SIZE));
662c039e3a8SBenjamin Herrenschmidt 	}
66330437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
66430437b3eSDavid Gibson 
6653accc0faSCédric Le Goater 	if (!IS_ENABLED(CONFIG_PPC32))
6663accc0faSCédric Le Goater 		return;
6673accc0faSCédric Le Goater 
668cbbcf340SKumar Gala 	/*
669cbbcf340SKumar Gala 	 * Handle the case where we might be booting from an old kexec
670cbbcf340SKumar Gala 	 * image that setup the mem_rsvmap as pairs of 32-bit values
671cbbcf340SKumar Gala 	 */
672dc0e643aSIan Munsie 	if (be64_to_cpup(reserve_map) > 0xffffffffull) {
673cbbcf340SKumar Gala 		u32 base_32, size_32;
674dc0e643aSIan Munsie 		__be32 *reserve_map_32 = (__be32 *)reserve_map;
675cbbcf340SKumar Gala 
676c039e3a8SBenjamin Herrenschmidt 		DBG("Found old 32-bit reserve map\n");
677c039e3a8SBenjamin Herrenschmidt 
678cbbcf340SKumar Gala 		while (1) {
679dc0e643aSIan Munsie 			base_32 = be32_to_cpup(reserve_map_32++);
680dc0e643aSIan Munsie 			size_32 = be32_to_cpup(reserve_map_32++);
681cbbcf340SKumar Gala 			if (size_32 == 0)
682cbbcf340SKumar Gala 				break;
683329dda08SKumar Gala 			DBG("reserving: %x -> %x\n", base_32, size_32);
68495f72d1eSYinghai Lu 			memblock_reserve(base_32, size_32);
685cbbcf340SKumar Gala 		}
686cbbcf340SKumar Gala 		return;
687cbbcf340SKumar Gala 	}
6889b6b563cSPaul Mackerras }
6899b6b563cSPaul Mackerras 
69007fd1761SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
69107fd1761SCyril Bur static bool tm_disabled __initdata;
69207fd1761SCyril Bur 
69307fd1761SCyril Bur static int __init parse_ppc_tm(char *str)
69407fd1761SCyril Bur {
69507fd1761SCyril Bur 	bool res;
69607fd1761SCyril Bur 
69707fd1761SCyril Bur 	if (kstrtobool(str, &res))
69807fd1761SCyril Bur 		return -EINVAL;
69907fd1761SCyril Bur 
70007fd1761SCyril Bur 	tm_disabled = !res;
70107fd1761SCyril Bur 
70207fd1761SCyril Bur 	return 0;
70307fd1761SCyril Bur }
70407fd1761SCyril Bur early_param("ppc_tm", parse_ppc_tm);
70507fd1761SCyril Bur 
70607fd1761SCyril Bur static void __init tm_init(void)
70707fd1761SCyril Bur {
70807fd1761SCyril Bur 	if (tm_disabled) {
70907fd1761SCyril Bur 		pr_info("Disabling hardware transactional memory (HTM)\n");
71007fd1761SCyril Bur 		cur_cpu_spec->cpu_user_features2 &=
71107fd1761SCyril Bur 			~(PPC_FEATURE2_HTM_NOSC | PPC_FEATURE2_HTM);
71207fd1761SCyril Bur 		cur_cpu_spec->cpu_features &= ~CPU_FTR_TM;
71354820530SMichael Ellerman 		return;
71407fd1761SCyril Bur 	}
71554820530SMichael Ellerman 
71654820530SMichael Ellerman 	pnv_tm_init();
71707fd1761SCyril Bur }
71807fd1761SCyril Bur #else
71907fd1761SCyril Bur static void tm_init(void) { }
72007fd1761SCyril Bur #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
72107fd1761SCyril Bur 
72254122970SMichael Ellerman static int __init
72354122970SMichael Ellerman early_init_dt_scan_model(unsigned long node, const char *uname,
72454122970SMichael Ellerman 			 int depth, void *data)
72554122970SMichael Ellerman {
72654122970SMichael Ellerman 	const char *prop;
72754122970SMichael Ellerman 
72854122970SMichael Ellerman 	if (depth != 0)
72954122970SMichael Ellerman 		return 0;
73054122970SMichael Ellerman 
73154122970SMichael Ellerman 	prop = of_get_flat_dt_prop(node, "model", NULL);
73254122970SMichael Ellerman 	if (prop)
73354122970SMichael Ellerman 		seq_buf_printf(&ppc_hw_desc, "%s ", prop);
73454122970SMichael Ellerman 
73554122970SMichael Ellerman 	/* break now */
73654122970SMichael Ellerman 	return 1;
73754122970SMichael Ellerman }
73854122970SMichael Ellerman 
739912c0a7fSMichael Ellerman #ifdef CONFIG_PPC64
740912c0a7fSMichael Ellerman static void __init save_fscr_to_task(void)
741912c0a7fSMichael Ellerman {
742912c0a7fSMichael Ellerman 	/*
743912c0a7fSMichael Ellerman 	 * Ensure the init_task (pid 0, aka swapper) uses the value of FSCR we
744912c0a7fSMichael Ellerman 	 * have configured via the device tree features or via __init_FSCR().
745912c0a7fSMichael Ellerman 	 * That value will then be propagated to pid 1 (init) and all future
746912c0a7fSMichael Ellerman 	 * processes.
747912c0a7fSMichael Ellerman 	 */
748912c0a7fSMichael Ellerman 	if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
749912c0a7fSMichael Ellerman 		init_task.thread.fscr = mfspr(SPRN_FSCR);
750912c0a7fSMichael Ellerman }
751912c0a7fSMichael Ellerman #else
7526c6fdbb2SChengyang Fan static inline void save_fscr_to_task(void) {}
753912c0a7fSMichael Ellerman #endif
754912c0a7fSMichael Ellerman 
755912c0a7fSMichael Ellerman 
7569b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
7579b6b563cSPaul Mackerras {
75849a84965SBecky Bruce 	phys_addr_t limit;
7596ca4f749SHollis Blanchard 
760b18f0ae9SChristophe Leroy 	DBG(" -> early_init_devtree(%px)\n", params);
7619b6b563cSPaul Mackerras 
762ad72a279SMichael Ellerman 	/* Too early to BUG_ON(), do it by hand */
763ad72a279SMichael Ellerman 	if (!early_init_dt_verify(params))
764ad72a279SMichael Ellerman 		panic("BUG: Failed verifying flat device tree, bad version?");
765ad72a279SMichael Ellerman 
76654122970SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_model, NULL);
76754122970SMichael Ellerman 
768458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
769458148c0SMichael Ellerman 	/* Some machines might need RTAS info for debugging, grab it now. */
770458148c0SMichael Ellerman 	of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
771458148c0SMichael Ellerman #endif
772458148c0SMichael Ellerman 
77314a43e69SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
77414a43e69SBenjamin Herrenschmidt 	/* Some machines might need OPAL info for debugging, grab it now. */
77514a43e69SBenjamin Herrenschmidt 	of_scan_flat_dt(early_init_dt_scan_opal, NULL);
776bb04ffe8SClaudio Carvalho 
777bb04ffe8SClaudio Carvalho 	/* Scan tree for ultravisor feature */
778bb04ffe8SClaudio Carvalho 	of_scan_flat_dt(early_init_dt_scan_ultravisor, NULL);
77914a43e69SBenjamin Herrenschmidt #endif
78014a43e69SBenjamin Herrenschmidt 
781bec53196SHari Bathini #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
782eb39c880SMahesh Salgaonkar 	/* scan tree to see if dump is active during last boot */
783eb39c880SMahesh Salgaonkar 	of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
784eb39c880SMahesh Salgaonkar #endif
785eb39c880SMahesh Salgaonkar 
7869b6b563cSPaul Mackerras 	/* Retrieve various informations from the /chosen node of the
7879b6b563cSPaul Mackerras 	 * device-tree, including the platform type, initrd location and
7889b6b563cSPaul Mackerras 	 * size, TCE reserve, and more ...
7899b6b563cSPaul Mackerras 	 */
7903e47d147SAnton Blanchard 	of_scan_flat_dt(early_init_dt_scan_chosen_ppc, boot_command_line);
7919b6b563cSPaul Mackerras 
79295f72d1eSYinghai Lu 	/* Scan memory nodes and rebuild MEMBLOCKs */
793d665881dSRob Herring 	early_init_dt_scan_root();
7941f012283SRob Herring 	early_init_dt_scan_memory_ppc();
795846f77b0SMichael Ellerman 
796ca829e05SZhouyi Zhou 	/*
797ca829e05SZhouyi Zhou 	 * As generic code authors expect to be able to use static keys
798ca829e05SZhouyi Zhou 	 * in early_param() handlers, we initialize the static keys just
799ca829e05SZhouyi Zhou 	 * before parsing early params (it's fine to call jump_label_init()
800ca829e05SZhouyi Zhou 	 * more than once).
801ca829e05SZhouyi Zhou 	 */
802ca829e05SZhouyi Zhou 	jump_label_init();
803846f77b0SMichael Ellerman 	parse_early_param();
804846f77b0SMichael Ellerman 
805ba14f649SKumar Gala 	/* make sure we've parsed cmdline for mem= before this */
806ba14f649SKumar Gala 	if (memory_limit)
807a84fcd46SSuzuki Poulose 		first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
808ba14f649SKumar Gala 	setup_initial_memory_limit(memstart_addr, first_memblock_size);
80995f72d1eSYinghai Lu 	/* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
81056afad88SChristophe Leroy 	memblock_reserve(PHYSICAL_START, __pa(_end) - PHYSICAL_START);
811549e8152SPaul Mackerras 	/* If relocatable, reserve first 32k for interrupt vectors etc. */
812549e8152SPaul Mackerras 	if (PHYSICAL_START > MEMORY_START)
81395f72d1eSYinghai Lu 		memblock_reserve(MEMORY_START, 0x8000);
81447310413SMichael Ellerman 	reserve_kdump_trampoline();
815bec53196SHari Bathini #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
816eb39c880SMahesh Salgaonkar 	/*
817eb39c880SMahesh Salgaonkar 	 * If we fail to reserve memory for firmware-assisted dump then
818eb39c880SMahesh Salgaonkar 	 * fallback to kexec based kdump.
819eb39c880SMahesh Salgaonkar 	 */
820eb39c880SMahesh Salgaonkar 	if (fadump_reserve_mem() == 0)
821eb39c880SMahesh Salgaonkar #endif
82235dd5432SMichael Ellerman 		reserve_crashkernel();
8239b6b563cSPaul Mackerras 	early_reserve_mem();
8249b6b563cSPaul Mackerras 
82514ed7409SAnton Blanchard 	/* Ensure that total memory size is page-aligned. */
8266fbef13cSTejun Heo 	limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
82795f72d1eSYinghai Lu 	memblock_enforce_memory_limit(limit);
8286ca4f749SHollis Blanchard 
829b32d5d7eSAneesh Kumar K.V #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_PPC_4K_PAGES)
830b32d5d7eSAneesh Kumar K.V 	if (!early_radix_enabled())
831b32d5d7eSAneesh Kumar K.V 		memblock_cap_memory_range(0, 1UL << (H_MAX_PHYSMEM_BITS));
832b32d5d7eSAneesh Kumar K.V #endif
833b32d5d7eSAneesh Kumar K.V 
8341aadc056STejun Heo 	memblock_allow_resize();
83595f72d1eSYinghai Lu 	memblock_dump_all();
8362babf5c2SMichael Ellerman 
837b18f0ae9SChristophe Leroy 	DBG("Phys. mem: %llx\n", (unsigned long long)memblock_phys_mem_size());
8382babf5c2SMichael Ellerman 
8392babf5c2SMichael Ellerman 	/* We may need to relocate the flat tree, do it now.
8402babf5c2SMichael Ellerman 	 * FIXME .. and the initrd too? */
8412babf5c2SMichael Ellerman 	move_device_tree();
8422babf5c2SMichael Ellerman 
8439b6b563cSPaul Mackerras 	DBG("Scanning CPUs ...\n");
8449b6b563cSPaul Mackerras 
8455a61ef74SNicholas Piggin 	dt_cpu_ftrs_scan();
8465a61ef74SNicholas Piggin 
847bd649d40SMichael Ellerman 	// We can now add the CPU name & PVR to the hardware description
848bd649d40SMichael Ellerman 	seq_buf_printf(&ppc_hw_desc, "%s 0x%04lx ", cur_cpu_spec->cpu_name, mfspr(SPRN_PVR));
849bd649d40SMichael Ellerman 
85025985edcSLucas De Marchi 	/* Retrieve CPU related informations from the flat tree
8513c726f8dSBenjamin Herrenschmidt 	 * (altivec support, boot CPU ID, ...)
8529b6b563cSPaul Mackerras 	 */
853*dc222fa7SNicholas Piggin 	of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
85436ae37e3SBenjamin Herrenschmidt 	if (boot_cpuid < 0) {
855d939be3aSMasanari Iida 		printk("Failed to identify boot CPU !\n");
85636ae37e3SBenjamin Herrenschmidt 		BUG();
85736ae37e3SBenjamin Herrenschmidt 	}
8589b6b563cSPaul Mackerras 
859912c0a7fSMichael Ellerman 	save_fscr_to_task();
860912c0a7fSMichael Ellerman 
8617ac87abbSMatt Evans #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
8627ac87abbSMatt Evans 	/* We'll later wait for secondaries to check in; there are
8637ac87abbSMatt Evans 	 * NCPUS-1 non-boot CPUs  :-)
8647ac87abbSMatt Evans 	 */
8657ac87abbSMatt Evans 	spinning_secondaries = boot_cpu_count - 1;
8667ac87abbSMatt Evans #endif
8677ac87abbSMatt Evans 
8681a01dc87SMichael Ellerman 	mmu_early_init_devtree();
8691a01dc87SMichael Ellerman 
87055672ecfSMahesh Salgaonkar #ifdef CONFIG_PPC_POWERNV
87155672ecfSMahesh Salgaonkar 	/* Scan and build the list of machine check recoverable ranges */
87255672ecfSMahesh Salgaonkar 	of_scan_flat_dt(early_init_dt_scan_recoverable_ranges, NULL);
87355672ecfSMahesh Salgaonkar #endif
874da6a97bfSBenjamin Herrenschmidt 	epapr_paravirt_early_init();
87555672ecfSMahesh Salgaonkar 
8763808a889SBenjamin Herrenschmidt 	/* Now try to figure out if we are running on LPAR and so on */
8773808a889SBenjamin Herrenschmidt 	pseries_probe_fw_features();
8783808a889SBenjamin Herrenschmidt 
879d3cd91fbSAneesh Kumar K.V 	/*
880d3cd91fbSAneesh Kumar K.V 	 * Initialize pkey features and default AMR/IAMR values
881d3cd91fbSAneesh Kumar K.V 	 */
882d3cd91fbSAneesh Kumar K.V 	pkey_early_init_devtree();
883d3cd91fbSAneesh Kumar K.V 
884166dd7d3SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PS3
885166dd7d3SBenjamin Herrenschmidt 	/* Identify PS3 firmware */
886166dd7d3SBenjamin Herrenschmidt 	if (of_flat_dt_is_compatible(of_get_flat_dt_root(), "sony,ps3"))
887166dd7d3SBenjamin Herrenschmidt 		powerpc_firmware_features |= FW_FEATURE_PS3_POSSIBLE;
888166dd7d3SBenjamin Herrenschmidt #endif
889166dd7d3SBenjamin Herrenschmidt 
89007fd1761SCyril Bur 	tm_init();
89107fd1761SCyril Bur 
8929b6b563cSPaul Mackerras 	DBG(" <- early_init_devtree()\n");
8939b6b563cSPaul Mackerras }
8949b6b563cSPaul Mackerras 
895b27652ddSKevin Hao #ifdef CONFIG_RELOCATABLE
896b27652ddSKevin Hao /*
897b27652ddSKevin Hao  * This function run before early_init_devtree, so we have to init
898b27652ddSKevin Hao  * initial_boot_params.
899b27652ddSKevin Hao  */
900b27652ddSKevin Hao void __init early_get_first_memblock_info(void *params, phys_addr_t *size)
901b27652ddSKevin Hao {
902b27652ddSKevin Hao 	/* Setup flat device-tree pointer */
903b27652ddSKevin Hao 	initial_boot_params = params;
904b27652ddSKevin Hao 
905b27652ddSKevin Hao 	/*
906b27652ddSKevin Hao 	 * Scan the memory nodes and set add_mem_to_memblock to 0 to avoid
907b27652ddSKevin Hao 	 * mess the memblock.
908b27652ddSKevin Hao 	 */
909b27652ddSKevin Hao 	add_mem_to_memblock = 0;
910d665881dSRob Herring 	early_init_dt_scan_root();
9111f012283SRob Herring 	early_init_dt_scan_memory_ppc();
912b27652ddSKevin Hao 	add_mem_to_memblock = 1;
913b27652ddSKevin Hao 
914b27652ddSKevin Hao 	if (size)
915b27652ddSKevin Hao 		*size = first_memblock_size;
916b27652ddSKevin Hao }
917b27652ddSKevin Hao #endif
918b27652ddSKevin Hao 
9199b6b563cSPaul Mackerras /*******
9209b6b563cSPaul Mackerras  *
9219b6b563cSPaul Mackerras  * New implementation of the OF "find" APIs, return a refcounted
9229b6b563cSPaul Mackerras  * object, call of_node_put() when done.  The device tree and list
9239b6b563cSPaul Mackerras  * are protected by a rw_lock.
9249b6b563cSPaul Mackerras  *
9259b6b563cSPaul Mackerras  * Note that property management will need some locking as well,
9269b6b563cSPaul Mackerras  * this isn't dealt with yet.
9279b6b563cSPaul Mackerras  *
9289b6b563cSPaul Mackerras  *******/
9299b6b563cSPaul Mackerras 
9309b6b563cSPaul Mackerras /**
931b37193b7SBenjamin Herrenschmidt  * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
932b37193b7SBenjamin Herrenschmidt  * @np: device node of the device
933b37193b7SBenjamin Herrenschmidt  *
934b37193b7SBenjamin Herrenschmidt  * This looks for a property "ibm,chip-id" in the node or any
935b37193b7SBenjamin Herrenschmidt  * of its parents and returns its content, or -1 if it cannot
936b37193b7SBenjamin Herrenschmidt  * be found.
937b37193b7SBenjamin Herrenschmidt  */
938b37193b7SBenjamin Herrenschmidt int of_get_ibm_chip_id(struct device_node *np)
939b37193b7SBenjamin Herrenschmidt {
940b37193b7SBenjamin Herrenschmidt 	of_node_get(np);
941b37193b7SBenjamin Herrenschmidt 	while (np) {
9421856f50cSChristophe Jaillet 		u32 chip_id;
943b37193b7SBenjamin Herrenschmidt 
9441856f50cSChristophe Jaillet 		/*
9451856f50cSChristophe Jaillet 		 * Skiboot may produce memory nodes that contain more than one
9461856f50cSChristophe Jaillet 		 * cell in chip-id, we only read the first one here.
9471856f50cSChristophe Jaillet 		 */
9481856f50cSChristophe Jaillet 		if (!of_property_read_u32(np, "ibm,chip-id", &chip_id)) {
949b37193b7SBenjamin Herrenschmidt 			of_node_put(np);
9501856f50cSChristophe Jaillet 			return chip_id;
951b37193b7SBenjamin Herrenschmidt 		}
95216c1d606SMichael Ellerman 
95316c1d606SMichael Ellerman 		np = of_get_next_parent(np);
954b37193b7SBenjamin Herrenschmidt 	}
955b37193b7SBenjamin Herrenschmidt 	return -1;
956b37193b7SBenjamin Herrenschmidt }
957b130e7c0SDan Streetman EXPORT_SYMBOL(of_get_ibm_chip_id);
958b37193b7SBenjamin Herrenschmidt 
9593eb906c6SMichael Ellerman /**
9603eb906c6SMichael Ellerman  * cpu_to_chip_id - Return the cpus chip-id
9613eb906c6SMichael Ellerman  * @cpu: The logical cpu number.
9623eb906c6SMichael Ellerman  *
9633eb906c6SMichael Ellerman  * Return the value of the ibm,chip-id property corresponding to the given
9643eb906c6SMichael Ellerman  * logical cpu number. If the chip-id can not be found, returns -1.
9653eb906c6SMichael Ellerman  */
9663eb906c6SMichael Ellerman int cpu_to_chip_id(int cpu)
9673eb906c6SMichael Ellerman {
9683eb906c6SMichael Ellerman 	struct device_node *np;
969c1e53367SSrikar Dronamraju 	int ret = -1, idx;
970c1e53367SSrikar Dronamraju 
971c1e53367SSrikar Dronamraju 	idx = cpu / threads_per_core;
972c1e53367SSrikar Dronamraju 	if (chip_id_lookup_table && chip_id_lookup_table[idx] != -1)
973c1e53367SSrikar Dronamraju 		return chip_id_lookup_table[idx];
9743eb906c6SMichael Ellerman 
9753eb906c6SMichael Ellerman 	np = of_get_cpu_node(cpu, NULL);
976c1e53367SSrikar Dronamraju 	if (np) {
977c1e53367SSrikar Dronamraju 		ret = of_get_ibm_chip_id(np);
9783eb906c6SMichael Ellerman 		of_node_put(np);
979c1e53367SSrikar Dronamraju 
980c1e53367SSrikar Dronamraju 		if (chip_id_lookup_table)
981c1e53367SSrikar Dronamraju 			chip_id_lookup_table[idx] = ret;
982c1e53367SSrikar Dronamraju 	}
983c1e53367SSrikar Dronamraju 
984c1e53367SSrikar Dronamraju 	return ret;
9853eb906c6SMichael Ellerman }
9863eb906c6SMichael Ellerman EXPORT_SYMBOL(cpu_to_chip_id);
9873eb906c6SMichael Ellerman 
988819d5965SSudeep KarkadaNagesha bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
989819d5965SSudeep KarkadaNagesha {
9909f593f13SNicholas Piggin #ifdef CONFIG_SMP
9919f593f13SNicholas Piggin 	/*
9929f593f13SNicholas Piggin 	 * Early firmware scanning must use this rather than
9939f593f13SNicholas Piggin 	 * get_hard_smp_processor_id because we don't have pacas allocated
9949f593f13SNicholas Piggin 	 * until memory topology is discovered.
9959f593f13SNicholas Piggin 	 */
9969f593f13SNicholas Piggin 	if (cpu_to_phys_id != NULL)
9979f593f13SNicholas Piggin 		return (int)phys_id == cpu_to_phys_id[cpu];
9989f593f13SNicholas Piggin #endif
9999f593f13SNicholas Piggin 
1000819d5965SSudeep KarkadaNagesha 	return (int)phys_id == get_hard_smp_processor_id(cpu);
1001819d5965SSudeep KarkadaNagesha }
1002