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>
5991361b51SRussell Currey #include <asm/plpks.h>
6014a43e69SBenjamin Herrenschmidt
6137dd2badSKumar Gala #include <mm/mmu_decl.h>
629b6b563cSPaul Mackerras
639b6b563cSPaul Mackerras #ifdef DEBUG
649b6b563cSPaul Mackerras #define DBG(fmt...) printk(KERN_ERR fmt)
659b6b563cSPaul Mackerras #else
669b6b563cSPaul Mackerras #define DBG(fmt...)
679b6b563cSPaul Mackerras #endif
689b6b563cSPaul Mackerras
69c1e53367SSrikar Dronamraju int *chip_id_lookup_table;
70c1e53367SSrikar Dronamraju
719b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
7228897731SOlof Johansson int __initdata iommu_is_off;
739b6b563cSPaul Mackerras int __initdata iommu_force_on;
74cf00a8d1SPaul Mackerras unsigned long tce_alloc_start, tce_alloc_end;
75cd3db0c4SBenjamin Herrenschmidt u64 ppc64_rma_size;
76e13d23a4SLaurent Dufour unsigned int boot_cpu_node_count __ro_after_init;
779b6b563cSPaul Mackerras #endif
7803bf469aSBenjamin Herrenschmidt static phys_addr_t first_memblock_size;
797ac87abbSMatt Evans static int __initdata boot_cpu_count;
809b6b563cSPaul Mackerras
early_parse_mem(char * p)812babf5c2SMichael Ellerman static int __init early_parse_mem(char *p)
822babf5c2SMichael Ellerman {
832babf5c2SMichael Ellerman if (!p)
842babf5c2SMichael Ellerman return 1;
852babf5c2SMichael Ellerman
862babf5c2SMichael Ellerman memory_limit = PAGE_ALIGN(memparse(p, &p));
87a84fcd46SSuzuki Poulose DBG("memory limit = 0x%llx\n", memory_limit);
882babf5c2SMichael Ellerman
892babf5c2SMichael Ellerman return 0;
902babf5c2SMichael Ellerman }
912babf5c2SMichael Ellerman early_param("mem", early_parse_mem);
922babf5c2SMichael Ellerman
93307cfe71SBenjamin Herrenschmidt /*
94307cfe71SBenjamin Herrenschmidt * overlaps_initrd - check for overlap with page aligned extension of
95307cfe71SBenjamin Herrenschmidt * initrd.
96307cfe71SBenjamin Herrenschmidt */
overlaps_initrd(unsigned long start,unsigned long size)97307cfe71SBenjamin Herrenschmidt static inline int overlaps_initrd(unsigned long start, unsigned long size)
98307cfe71SBenjamin Herrenschmidt {
99307cfe71SBenjamin Herrenschmidt #ifdef CONFIG_BLK_DEV_INITRD
100307cfe71SBenjamin Herrenschmidt if (!initrd_start)
101307cfe71SBenjamin Herrenschmidt return 0;
102307cfe71SBenjamin Herrenschmidt
103e96d904eSChristophe Leroy return (start + size) > ALIGN_DOWN(initrd_start, PAGE_SIZE) &&
104b7115316SChristophe Leroy start <= ALIGN(initrd_end, PAGE_SIZE);
105307cfe71SBenjamin Herrenschmidt #else
106307cfe71SBenjamin Herrenschmidt return 0;
107307cfe71SBenjamin Herrenschmidt #endif
108307cfe71SBenjamin Herrenschmidt }
109307cfe71SBenjamin Herrenschmidt
1103c607ce2SLinas Vepstas /**
1113c607ce2SLinas Vepstas * move_device_tree - move tree to an unused area, if needed.
1123c607ce2SLinas Vepstas *
1133c607ce2SLinas Vepstas * The device tree may be allocated beyond our memory limit, or inside the
114307cfe71SBenjamin Herrenschmidt * crash kernel region for kdump, or within the page aligned range of initrd.
115307cfe71SBenjamin Herrenschmidt * If so, move it out of the way.
1162babf5c2SMichael Ellerman */
move_device_tree(void)11718f032cbSGeert Uytterhoeven static void __init move_device_tree(void)
1182babf5c2SMichael Ellerman {
1192babf5c2SMichael Ellerman unsigned long start, size;
1202babf5c2SMichael Ellerman void *p;
1212babf5c2SMichael Ellerman
1222babf5c2SMichael Ellerman DBG("-> move_device_tree\n");
1232babf5c2SMichael Ellerman
1242babf5c2SMichael Ellerman start = __pa(initial_boot_params);
125060f78c2SRob Herring size = fdt_totalsize(initial_boot_params);
1262babf5c2SMichael Ellerman
1276dd22700SScott Wood if ((memory_limit && (start + size) > PHYSICAL_START + memory_limit) ||
128f242e0acSChristophe Leroy !memblock_is_memory(start + size - 1) ||
129f242e0acSChristophe Leroy overlaps_crashkernel(start, size) || overlaps_initrd(start, size)) {
1301269f7b8SChristophe Leroy p = memblock_alloc_raw(size, PAGE_SIZE);
1311269f7b8SChristophe Leroy if (!p)
1321269f7b8SChristophe Leroy panic("Failed to allocate %lu bytes to move device tree\n",
1331269f7b8SChristophe Leroy size);
1342babf5c2SMichael Ellerman memcpy(p, initial_boot_params, size);
135060f78c2SRob Herring initial_boot_params = p;
136b18f0ae9SChristophe Leroy DBG("Moved device tree to 0x%px\n", p);
1372babf5c2SMichael Ellerman }
1382babf5c2SMichael Ellerman
1392babf5c2SMichael Ellerman DBG("<- move_device_tree\n");
1402babf5c2SMichael Ellerman }
1419b6b563cSPaul Mackerras
142d205819eSPaul Mackerras /*
143335e1a91SHaren Myneni * ibm,pa/pi-features is a per-cpu property that contains a string of
144d205819eSPaul Mackerras * attribute descriptors, each of which has a 2 byte header plus up
145d205819eSPaul Mackerras * to 254 bytes worth of processor attribute bits. First header
146d205819eSPaul Mackerras * byte specifies the number of bytes following the header.
147d205819eSPaul Mackerras * Second header byte is an "attribute-specifier" type, of which
148d205819eSPaul Mackerras * zero is the only currently-defined value.
149d205819eSPaul Mackerras * Implementation: Pass in the byte and bit offset for the feature
150d205819eSPaul Mackerras * that we are interested in. The function will return -1 if the
151d205819eSPaul Mackerras * pa-features property is missing, or a 1/0 to indicate if the feature
152d205819eSPaul Mackerras * is supported/not supported. Note that the bit numbers are
153d205819eSPaul Mackerras * big-endian to match the definition in PAPR.
154d205819eSPaul Mackerras */
155335e1a91SHaren Myneni struct ibm_feature {
156d205819eSPaul Mackerras unsigned long cpu_features; /* CPU_FTR_xxx bit */
15744ae3ab3SMatt Evans unsigned long mmu_features; /* MMU_FTR_xxx bit */
158d205819eSPaul Mackerras unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
159beff8237SAnton Blanchard unsigned int cpu_user_ftrs2; /* PPC_FEATURE2_xxx bit */
160335e1a91SHaren Myneni unsigned char pabyte; /* byte number in ibm,pa/pi-features */
161d205819eSPaul Mackerras unsigned char pabit; /* bit number (big-endian) */
162d205819eSPaul Mackerras unsigned char invert; /* if 1, pa bit set => clear feature */
163335e1a91SHaren Myneni };
164335e1a91SHaren Myneni
165335e1a91SHaren Myneni static struct ibm_feature ibm_pa_features[] __initdata = {
166e9eb0278SMichael Ellerman { .pabyte = 0, .pabit = 0, .cpu_user_ftrs = PPC_FEATURE_HAS_MMU },
167e9eb0278SMichael Ellerman { .pabyte = 0, .pabit = 1, .cpu_user_ftrs = PPC_FEATURE_HAS_FPU },
168e9eb0278SMichael Ellerman { .pabyte = 0, .pabit = 3, .cpu_features = CPU_FTR_CTRL },
169e9eb0278SMichael Ellerman { .pabyte = 0, .pabit = 6, .cpu_features = CPU_FTR_NOEXECUTE },
170e9eb0278SMichael Ellerman { .pabyte = 1, .pabit = 2, .mmu_features = MMU_FTR_CI_LARGE_PAGE },
171d957fb4dSMichael Neuling #ifdef CONFIG_PPC_RADIX_MMU
1729a77c4a0SNicholas Piggin { .pabyte = 40, .pabit = 0, .mmu_features = MMU_FTR_TYPE_RADIX | MMU_FTR_GTSE },
173d957fb4dSMichael Neuling #endif
174e9eb0278SMichael Ellerman { .pabyte = 5, .pabit = 0, .cpu_features = CPU_FTR_REAL_LE,
175e9eb0278SMichael Ellerman .cpu_user_ftrs = PPC_FEATURE_TRUE_LE },
1769e819963SAneesh Kumar K.V /*
1774705e024SAnton Blanchard * If the kernel doesn't support TM (ie CONFIG_PPC_TRANSACTIONAL_MEM=n),
1784705e024SAnton Blanchard * we don't want to turn on TM here, so we use the *_COMP versions
1794705e024SAnton Blanchard * which are 0 if the kernel doesn't support TM.
1809e819963SAneesh Kumar K.V */
181e9eb0278SMichael Ellerman { .pabyte = 22, .pabit = 0, .cpu_features = CPU_FTR_TM_COMP,
182e9eb0278SMichael Ellerman .cpu_user_ftrs2 = PPC_FEATURE2_HTM_COMP | PPC_FEATURE2_HTM_NOSC_COMP },
1838f45ca3fSRavi Bangoria
1848f45ca3fSRavi Bangoria { .pabyte = 64, .pabit = 0, .cpu_features = CPU_FTR_DAWR1 },
1850ffd60b7SBenjamin Gray { .pabyte = 68, .pabit = 5, .cpu_features = CPU_FTR_DEXCR_NPHIE },
186d205819eSPaul Mackerras };
187d205819eSPaul Mackerras
188335e1a91SHaren Myneni /*
189335e1a91SHaren Myneni * ibm,pi-features property provides the support of processor specific
190335e1a91SHaren Myneni * options not described in ibm,pa-features. Right now use byte 0, bit 3
191335e1a91SHaren Myneni * which indicates the occurrence of DSI interrupt when the paste operation
192335e1a91SHaren Myneni * on the suspended NX window.
193335e1a91SHaren Myneni */
194335e1a91SHaren Myneni static struct ibm_feature ibm_pi_features[] __initdata = {
195335e1a91SHaren Myneni { .pabyte = 0, .pabit = 3, .mmu_features = MMU_FTR_NX_DSI },
196335e1a91SHaren Myneni };
197335e1a91SHaren Myneni
scan_features(unsigned long node,const unsigned char * ftrs,unsigned long tablelen,struct ibm_feature * fp,unsigned long ft_size)1989d0c4dfeSRob Herring static void __init scan_features(unsigned long node, const unsigned char *ftrs,
199974a76f5SPaul Mackerras unsigned long tablelen,
200335e1a91SHaren Myneni struct ibm_feature *fp,
201974a76f5SPaul Mackerras unsigned long ft_size)
202d205819eSPaul Mackerras {
203974a76f5SPaul Mackerras unsigned long i, len, bit;
204d205819eSPaul Mackerras
205d205819eSPaul Mackerras /* find descriptor with type == 0 */
206d205819eSPaul Mackerras for (;;) {
207d205819eSPaul Mackerras if (tablelen < 3)
208d205819eSPaul Mackerras return;
209974a76f5SPaul Mackerras len = 2 + ftrs[0];
210d205819eSPaul Mackerras if (tablelen < len)
211d205819eSPaul Mackerras return; /* descriptor 0 not found */
212974a76f5SPaul Mackerras if (ftrs[1] == 0)
213d205819eSPaul Mackerras break;
214d205819eSPaul Mackerras tablelen -= len;
215974a76f5SPaul Mackerras ftrs += len;
216d205819eSPaul Mackerras }
217d205819eSPaul Mackerras
218d205819eSPaul Mackerras /* loop over bits we know about */
219974a76f5SPaul Mackerras for (i = 0; i < ft_size; ++i, ++fp) {
220974a76f5SPaul Mackerras if (fp->pabyte >= ftrs[0])
221d205819eSPaul Mackerras continue;
222974a76f5SPaul Mackerras bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
223d205819eSPaul Mackerras if (bit ^ fp->invert) {
224d205819eSPaul Mackerras cur_cpu_spec->cpu_features |= fp->cpu_features;
225d205819eSPaul Mackerras cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
226beff8237SAnton Blanchard cur_cpu_spec->cpu_user_features2 |= fp->cpu_user_ftrs2;
22744ae3ab3SMatt Evans cur_cpu_spec->mmu_features |= fp->mmu_features;
228d205819eSPaul Mackerras } else {
229d205819eSPaul Mackerras cur_cpu_spec->cpu_features &= ~fp->cpu_features;
230d205819eSPaul Mackerras cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
231beff8237SAnton Blanchard cur_cpu_spec->cpu_user_features2 &= ~fp->cpu_user_ftrs2;
23244ae3ab3SMatt Evans cur_cpu_spec->mmu_features &= ~fp->mmu_features;
233d205819eSPaul Mackerras }
234d205819eSPaul Mackerras }
235d205819eSPaul Mackerras }
236d205819eSPaul Mackerras
check_cpu_features(unsigned long node,char * name,struct ibm_feature * fp,unsigned long size)237335e1a91SHaren Myneni static void __init check_cpu_features(unsigned long node, char *name,
238335e1a91SHaren Myneni struct ibm_feature *fp,
239335e1a91SHaren Myneni unsigned long size)
240974a76f5SPaul Mackerras {
2419d0c4dfeSRob Herring const unsigned char *pa_ftrs;
2429d0c4dfeSRob Herring int tablelen;
243974a76f5SPaul Mackerras
244335e1a91SHaren Myneni pa_ftrs = of_get_flat_dt_prop(node, name, &tablelen);
245974a76f5SPaul Mackerras if (pa_ftrs == NULL)
246974a76f5SPaul Mackerras return;
247974a76f5SPaul Mackerras
248335e1a91SHaren Myneni scan_features(node, pa_ftrs, tablelen, fp, size);
249974a76f5SPaul Mackerras }
250974a76f5SPaul Mackerras
251387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
init_mmu_slb_size(unsigned long node)2529c61f7a0SAnshuman Khandual static void __init init_mmu_slb_size(unsigned long node)
253584f8b71SMichael Neuling {
2549d0c4dfeSRob Herring const __be32 *slb_size_ptr;
255584f8b71SMichael Neuling
2569c61f7a0SAnshuman Khandual slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL) ? :
2579c61f7a0SAnshuman Khandual of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
2589c61f7a0SAnshuman Khandual
2599c61f7a0SAnshuman Khandual if (slb_size_ptr)
260dc0e643aSIan Munsie mmu_slb_size = be32_to_cpup(slb_size_ptr);
261584f8b71SMichael Neuling }
262584f8b71SMichael Neuling #else
2639c61f7a0SAnshuman Khandual #define init_mmu_slb_size(node) do { } while(0)
264584f8b71SMichael Neuling #endif
265584f8b71SMichael Neuling
266974a76f5SPaul Mackerras static struct feature_property {
267974a76f5SPaul Mackerras const char *name;
268974a76f5SPaul Mackerras u32 min_value;
269974a76f5SPaul Mackerras unsigned long cpu_feature;
270974a76f5SPaul Mackerras unsigned long cpu_user_ftr;
271974a76f5SPaul Mackerras } feature_properties[] __initdata = {
272974a76f5SPaul Mackerras #ifdef CONFIG_ALTIVEC
273974a76f5SPaul Mackerras {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
274974a76f5SPaul Mackerras {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
275974a76f5SPaul Mackerras #endif /* CONFIG_ALTIVEC */
276b962ce9dSMichael Neuling #ifdef CONFIG_VSX
277b962ce9dSMichael Neuling /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
278b962ce9dSMichael Neuling {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
279b962ce9dSMichael Neuling #endif /* CONFIG_VSX */
280974a76f5SPaul Mackerras #ifdef CONFIG_PPC64
281974a76f5SPaul Mackerras {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
282974a76f5SPaul Mackerras {"ibm,purr", 1, CPU_FTR_PURR, 0},
283974a76f5SPaul Mackerras {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
284974a76f5SPaul Mackerras #endif /* CONFIG_PPC64 */
285974a76f5SPaul Mackerras };
286974a76f5SPaul Mackerras
28714b3d926SValentine Barshak #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
identical_pvr_fixup(unsigned long node)2881ef1dd9cSNathan Chancellor static __init void identical_pvr_fixup(unsigned long node)
28914b3d926SValentine Barshak {
29014b3d926SValentine Barshak unsigned int pvr;
2919d0c4dfeSRob Herring const char *model = of_get_flat_dt_prop(node, "model", NULL);
29214b3d926SValentine Barshak
29314b3d926SValentine Barshak /*
29414b3d926SValentine Barshak * Since 440GR(x)/440EP(x) processors have the same pvr,
29514b3d926SValentine Barshak * we check the node path and set bit 28 in the cur_cpu_spec
29614b3d926SValentine Barshak * pvr for EP(x) processor version. This bit is always 0 in
29714b3d926SValentine Barshak * the "real" pvr. Then we call identify_cpu again with
29814b3d926SValentine Barshak * the new logical pvr to enable FPU support.
29914b3d926SValentine Barshak */
30014b3d926SValentine Barshak if (model && strstr(model, "440EP")) {
30114b3d926SValentine Barshak pvr = cur_cpu_spec->pvr_value | 0x8;
30214b3d926SValentine Barshak identify_cpu(0, pvr);
30314b3d926SValentine Barshak DBG("Using logical pvr %x for %s\n", pvr, model);
30414b3d926SValentine Barshak }
30514b3d926SValentine Barshak }
30614b3d926SValentine Barshak #else
30714b3d926SValentine Barshak #define identical_pvr_fixup(node) do { } while(0)
30814b3d926SValentine Barshak #endif
30914b3d926SValentine Barshak
check_cpu_feature_properties(unsigned long node)310974a76f5SPaul Mackerras static void __init check_cpu_feature_properties(unsigned long node)
311974a76f5SPaul Mackerras {
3124f1f40f7SMathieu Malaterre int i;
313974a76f5SPaul Mackerras struct feature_property *fp = feature_properties;
314dc0e643aSIan Munsie const __be32 *prop;
315974a76f5SPaul Mackerras
3164f1f40f7SMathieu Malaterre for (i = 0; i < (int)ARRAY_SIZE(feature_properties); ++i, ++fp) {
317974a76f5SPaul Mackerras prop = of_get_flat_dt_prop(node, fp->name, NULL);
318dc0e643aSIan Munsie if (prop && be32_to_cpup(prop) >= fp->min_value) {
319974a76f5SPaul Mackerras cur_cpu_spec->cpu_features |= fp->cpu_feature;
320974a76f5SPaul Mackerras cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
321974a76f5SPaul Mackerras }
322974a76f5SPaul Mackerras }
323974a76f5SPaul Mackerras }
324974a76f5SPaul Mackerras
early_init_dt_scan_cpus(unsigned long node,const char * uname,int depth,void * data)3259b6b563cSPaul Mackerras static int __init early_init_dt_scan_cpus(unsigned long node,
3264df20460SAnton Blanchard const char *uname, int depth,
3274df20460SAnton Blanchard void *data)
3289b6b563cSPaul Mackerras {
3299d0c4dfeSRob Herring const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
3306e891b04SNathan Lynch const __be32 *cpu_version = NULL;
331dc0e643aSIan Munsie const __be32 *prop;
332dc0e643aSIan Munsie const __be32 *intserv;
3334df20460SAnton Blanchard int i, nthreads;
3349d0c4dfeSRob Herring int len;
3359d07bc84SBenjamin Herrenschmidt int found = -1;
336208b3a4cSBenjamin Herrenschmidt int found_thread = 0;
3379b6b563cSPaul Mackerras
3389b6b563cSPaul Mackerras /* We are scanning "cpu" nodes only */
3399b6b563cSPaul Mackerras if (type == NULL || strcmp(type, "cpu") != 0)
3409b6b563cSPaul Mackerras return 0;
3419b6b563cSPaul Mackerras
34264fdcbccSMichael Ellerman if (IS_ENABLED(CONFIG_PPC64))
343e13d23a4SLaurent Dufour boot_cpu_node_count++;
344e13d23a4SLaurent Dufour
3454df20460SAnton Blanchard /* Get physical cpuid */
3464df20460SAnton Blanchard intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
347e16c8765SAndy Fleming if (!intserv)
348e16c8765SAndy Fleming intserv = of_get_flat_dt_prop(node, "reg", &len);
349e16c8765SAndy Fleming
3504df20460SAnton Blanchard nthreads = len / sizeof(int);
3514df20460SAnton Blanchard
3524df20460SAnton Blanchard /*
3534df20460SAnton Blanchard * Now see if any of these threads match our boot cpu.
3544df20460SAnton Blanchard * NOTE: This must match the parsing done in smp_setup_cpu_maps.
3554df20460SAnton Blanchard */
3564df20460SAnton Blanchard for (i = 0; i < nthreads; i++) {
357dc0e643aSIan Munsie if (be32_to_cpu(intserv[i]) ==
358060f78c2SRob Herring fdt_boot_cpuid_phys(initial_boot_params)) {
3599d07bc84SBenjamin Herrenschmidt found = boot_cpu_count;
360208b3a4cSBenjamin Herrenschmidt found_thread = i;
361208b3a4cSBenjamin Herrenschmidt }
3624df20460SAnton Blanchard #ifdef CONFIG_SMP
3634df20460SAnton Blanchard /* logical cpu id is always 0 on UP kernels */
3649d07bc84SBenjamin Herrenschmidt boot_cpu_count++;
3654df20460SAnton Blanchard #endif
3664df20460SAnton Blanchard }
3674df20460SAnton Blanchard
3687222f779SBenjamin Herrenschmidt /* Not the boot CPU */
3697222f779SBenjamin Herrenschmidt if (found < 0)
3707222f779SBenjamin Herrenschmidt return 0;
3717222f779SBenjamin Herrenschmidt
3729d07bc84SBenjamin Herrenschmidt DBG("boot cpu: logical %d physical %d\n", found,
373dc0e643aSIan Munsie be32_to_cpu(intserv[found_thread]));
3749d07bc84SBenjamin Herrenschmidt boot_cpuid = found;
375974a76f5SPaul Mackerras
376dc222fa7SNicholas Piggin if (IS_ENABLED(CONFIG_PPC64))
377dc222fa7SNicholas Piggin boot_cpu_hwid = be32_to_cpu(intserv[found_thread]);
3782e7f1e2bSMichael Ellerman
37968a9c41fSMichael Ellerman if (nr_cpu_ids % nthreads != 0) {
38068a9c41fSMichael Ellerman set_nr_cpu_ids(ALIGN(nr_cpu_ids, nthreads));
38168a9c41fSMichael Ellerman pr_warn("nr_cpu_ids was not a multiple of threads_per_core, adjusted to %d\n",
38268a9c41fSMichael Ellerman nr_cpu_ids);
38368a9c41fSMichael Ellerman }
38468a9c41fSMichael Ellerman
385a0b9f4f8SMichael Ellerman if (boot_cpuid >= nr_cpu_ids) {
386a0b9f4f8SMichael Ellerman set_nr_cpu_ids(min(CONFIG_NR_CPUS, ALIGN(boot_cpuid + 1, nthreads)));
387a0b9f4f8SMichael Ellerman pr_warn("Boot CPU %d >= nr_cpu_ids, adjusted nr_cpu_ids to %d\n",
388a0b9f4f8SMichael Ellerman boot_cpuid, nr_cpu_ids);
389a0b9f4f8SMichael Ellerman }
390a0b9f4f8SMichael Ellerman
391974a76f5SPaul Mackerras /*
392974a76f5SPaul Mackerras * PAPR defines "logical" PVR values for cpus that
393974a76f5SPaul Mackerras * meet various levels of the architecture:
394974a76f5SPaul Mackerras * 0x0f000001 Architecture version 2.04
395974a76f5SPaul Mackerras * 0x0f000002 Architecture version 2.05
396974a76f5SPaul Mackerras * If the cpu-version property in the cpu node contains
397974a76f5SPaul Mackerras * such a value, we call identify_cpu again with the
398974a76f5SPaul Mackerras * logical PVR value in order to use the cpu feature
399974a76f5SPaul Mackerras * bits appropriate for the architecture level.
400974a76f5SPaul Mackerras *
401974a76f5SPaul Mackerras * A POWER6 partition in "POWER6 architected" mode
402974a76f5SPaul Mackerras * uses the 0x0f000002 PVR value; in POWER5+ mode
403974a76f5SPaul Mackerras * it uses 0x0f000001.
4045a61ef74SNicholas Piggin *
4055a61ef74SNicholas Piggin * If we're using device tree CPU feature discovery then we don't
4065a61ef74SNicholas Piggin * support the cpu-version property, and it's the responsibility of the
4075a61ef74SNicholas Piggin * firmware/hypervisor to provide the correct feature set for the
4085a61ef74SNicholas Piggin * architecture level via the ibm,powerpc-cpu-features binding.
409974a76f5SPaul Mackerras */
4105a61ef74SNicholas Piggin if (!dt_cpu_ftrs_in_use()) {
411974a76f5SPaul Mackerras prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
41248b7019bSMichael Ellerman if (prop && (be32_to_cpup(prop) & 0xff000000) == 0x0f000000) {
413dc0e643aSIan Munsie identify_cpu(0, be32_to_cpup(prop));
4146e891b04SNathan Lynch cpu_version = prop;
41548b7019bSMichael Ellerman }
41614b3d926SValentine Barshak
417974a76f5SPaul Mackerras check_cpu_feature_properties(node);
418335e1a91SHaren Myneni check_cpu_features(node, "ibm,pa-features", ibm_pa_features,
419335e1a91SHaren Myneni ARRAY_SIZE(ibm_pa_features));
420335e1a91SHaren Myneni check_cpu_features(node, "ibm,pi-features", ibm_pi_features,
421335e1a91SHaren Myneni ARRAY_SIZE(ibm_pi_features));
4225a61ef74SNicholas Piggin }
4235a61ef74SNicholas Piggin
4245a61ef74SNicholas Piggin identical_pvr_fixup(node);
4256e891b04SNathan Lynch
4266e891b04SNathan Lynch // We can now add the CPU name & PVR to the hardware description
4276e891b04SNathan Lynch seq_buf_printf(&ppc_hw_desc, "%s 0x%04lx ", cur_cpu_spec->cpu_name, mfspr(SPRN_PVR));
4286e891b04SNathan Lynch if (cpu_version)
4296e891b04SNathan Lynch seq_buf_printf(&ppc_hw_desc, "0x%04x ", be32_to_cpup(cpu_version));
4306e891b04SNathan Lynch
4319c61f7a0SAnshuman Khandual init_mmu_slb_size(node);
432d205819eSPaul Mackerras
4334a85b31dSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
4345a61ef74SNicholas Piggin if (nthreads == 1)
4354df20460SAnton Blanchard cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
4365a61ef74SNicholas Piggin else if (!dt_cpu_ftrs_in_use())
4375a61ef74SNicholas Piggin cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
4389b6b563cSPaul Mackerras #endif
4395a61ef74SNicholas Piggin
4409b6b563cSPaul Mackerras return 0;
4419b6b563cSPaul Mackerras }
4429b6b563cSPaul Mackerras
early_init_dt_scan_chosen_ppc(unsigned long node,const char * uname,int depth,void * data)443e51df2c1SAnton Blanchard static int __init early_init_dt_scan_chosen_ppc(unsigned long node,
444e51df2c1SAnton Blanchard const char *uname,
44532c97689SGrant Likely int depth, void *data)
4469b6b563cSPaul Mackerras {
4479d0c4dfeSRob Herring const unsigned long *lprop; /* All these set by kernel, so no need to convert endian */
4489b6b563cSPaul Mackerras
44932c97689SGrant Likely /* Use common scan routine to determine if this is the chosen node */
45060f20d84SRob Herring if (early_init_dt_scan_chosen(data) < 0)
45132c97689SGrant Likely return 0;
45232c97689SGrant Likely
4539b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4549b6b563cSPaul Mackerras /* check if iommu is forced on or off */
4553c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
4569b6b563cSPaul Mackerras iommu_is_off = 1;
4573c726f8dSBenjamin Herrenschmidt if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
4589b6b563cSPaul Mackerras iommu_force_on = 1;
4599b6b563cSPaul Mackerras #endif
4609b6b563cSPaul Mackerras
4612babf5c2SMichael Ellerman /* mem=x on the command line is the preferred mechanism */
4623c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
4639b6b563cSPaul Mackerras if (lprop)
4649b6b563cSPaul Mackerras memory_limit = *lprop;
4659b6b563cSPaul Mackerras
4669b6b563cSPaul Mackerras #ifdef CONFIG_PPC64
4673c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
4689b6b563cSPaul Mackerras if (lprop)
4699b6b563cSPaul Mackerras tce_alloc_start = *lprop;
4703c726f8dSBenjamin Herrenschmidt lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
4719b6b563cSPaul Mackerras if (lprop)
4729b6b563cSPaul Mackerras tce_alloc_end = *lprop;
4739b6b563cSPaul Mackerras #endif
4749b6b563cSPaul Mackerras
475da665885SThiago Jung Bauermann #ifdef CONFIG_KEXEC_CORE
47663277161SStephen Rothwell lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
477dcee3036SMichael Ellerman if (lprop)
478dcee3036SMichael Ellerman crashk_res.start = *lprop;
479dcee3036SMichael Ellerman
48063277161SStephen Rothwell lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
481dcee3036SMichael Ellerman if (lprop)
482dcee3036SMichael Ellerman crashk_res.end = crashk_res.start + *lprop - 1;
483dcee3036SMichael Ellerman #endif
48432c97689SGrant Likely
48532c97689SGrant Likely /* break now */
48632c97689SGrant Likely return 1;
4879b6b563cSPaul Mackerras }
4889b6b563cSPaul Mackerras
4896aba0c84SAneesh Kumar K.V /*
4906aba0c84SAneesh Kumar K.V * Compare the range against max mem limit and update
4916aba0c84SAneesh Kumar K.V * size if it cross the limit.
4926aba0c84SAneesh Kumar K.V */
4936aba0c84SAneesh Kumar K.V
4946aba0c84SAneesh Kumar K.V #ifdef CONFIG_SPARSEMEM
validate_mem_limit(u64 base,u64 * size)495d276960dSNick Child static bool __init validate_mem_limit(u64 base, u64 *size)
4966aba0c84SAneesh Kumar K.V {
4976aba0c84SAneesh Kumar K.V u64 max_mem = 1UL << (MAX_PHYSMEM_BITS);
4986aba0c84SAneesh Kumar K.V
4996aba0c84SAneesh Kumar K.V if (base >= max_mem)
5006aba0c84SAneesh Kumar K.V return false;
5016aba0c84SAneesh Kumar K.V if ((base + *size) > max_mem)
5026aba0c84SAneesh Kumar K.V *size = max_mem - base;
5036aba0c84SAneesh Kumar K.V return true;
5046aba0c84SAneesh Kumar K.V }
5056aba0c84SAneesh Kumar K.V #else
validate_mem_limit(u64 base,u64 * size)506d276960dSNick Child static bool __init validate_mem_limit(u64 base, u64 *size)
5076aba0c84SAneesh Kumar K.V {
5086aba0c84SAneesh Kumar K.V return true;
5096aba0c84SAneesh Kumar K.V }
5106aba0c84SAneesh Kumar K.V #endif
5116aba0c84SAneesh Kumar K.V
5120204568aSPaul Mackerras #ifdef CONFIG_PPC_PSERIES
5130204568aSPaul Mackerras /*
5146c6ea537SNathan Fontenot * Interpret the ibm dynamic reconfiguration memory LMBs.
5150204568aSPaul Mackerras * This contains a list of memory blocks along with NUMA affinity
5160204568aSPaul Mackerras * information.
5170204568aSPaul Mackerras */
early_init_drmem_lmb(struct drmem_lmb * lmb,const __be32 ** usm,void * data)518adfefc60SHari Bathini static int __init early_init_drmem_lmb(struct drmem_lmb *lmb,
519adfefc60SHari Bathini const __be32 **usm,
520adfefc60SHari Bathini void *data)
5210204568aSPaul Mackerras {
5226c6ea537SNathan Fontenot u64 base, size;
5236c6ea537SNathan Fontenot int is_kexec_kdump = 0, rngs;
5240204568aSPaul Mackerras
5256c6ea537SNathan Fontenot base = lmb->base_addr;
5266c6ea537SNathan Fontenot size = drmem_lmb_size();
5276c6ea537SNathan Fontenot rngs = 1;
5280204568aSPaul Mackerras
5296c6ea537SNathan Fontenot /*
5306c6ea537SNathan Fontenot * Skip this block if the reserved bit is set in flags
5316c6ea537SNathan Fontenot * or if the block is not assigned to this partition.
5326c6ea537SNathan Fontenot */
5336c6ea537SNathan Fontenot if ((lmb->flags & DRCONF_MEM_RESERVED) ||
5346c6ea537SNathan Fontenot !(lmb->flags & DRCONF_MEM_ASSIGNED))
535adfefc60SHari Bathini return 0;
5360204568aSPaul Mackerras
5376c6ea537SNathan Fontenot if (*usm)
538cf00085dSChandru is_kexec_kdump = 1;
539cf00085dSChandru
540cf00085dSChandru if (is_kexec_kdump) {
541cf00085dSChandru /*
5426c6ea537SNathan Fontenot * For each memblock in ibm,dynamic-memory, a
5436c6ea537SNathan Fontenot * corresponding entry in linux,drconf-usable-memory
5446c6ea537SNathan Fontenot * property contains a counter 'p' followed by 'p'
5456c6ea537SNathan Fontenot * (base, size) duple. Now read the counter from
546cf00085dSChandru * linux,drconf-usable-memory property
547cf00085dSChandru */
5486c6ea537SNathan Fontenot rngs = dt_mem_next_cell(dt_root_size_cells, usm);
549cf00085dSChandru if (!rngs) /* there are no (base, size) duple */
550adfefc60SHari Bathini return 0;
551cf00085dSChandru }
5526c6ea537SNathan Fontenot
553cf00085dSChandru do {
554cf00085dSChandru if (is_kexec_kdump) {
5556c6ea537SNathan Fontenot base = dt_mem_next_cell(dt_root_addr_cells, usm);
5566c6ea537SNathan Fontenot size = dt_mem_next_cell(dt_root_size_cells, usm);
557cf00085dSChandru }
5586c6ea537SNathan Fontenot
5590204568aSPaul Mackerras if (iommu_is_off) {
5600204568aSPaul Mackerras if (base >= 0x80000000ul)
5610204568aSPaul Mackerras continue;
5620204568aSPaul Mackerras if ((base + size) > 0x80000000ul)
5630204568aSPaul Mackerras size = 0x80000000ul - base;
5640204568aSPaul Mackerras }
5656c6ea537SNathan Fontenot
566b6eca183SLeonardo Bras if (!validate_mem_limit(base, &size))
567b6eca183SLeonardo Bras continue;
568b6eca183SLeonardo Bras
5696c6ea537SNathan Fontenot DBG("Adding: %llx -> %llx\n", base, size);
57095f72d1eSYinghai Lu memblock_add(base, size);
571b6eca183SLeonardo Bras
572b6eca183SLeonardo Bras if (lmb->flags & DRCONF_MEM_HOTREMOVABLE)
573b6eca183SLeonardo Bras memblock_mark_hotplug(base, size);
574cf00085dSChandru } while (--rngs);
575adfefc60SHari Bathini
576adfefc60SHari Bathini return 0;
5770204568aSPaul Mackerras }
5780204568aSPaul Mackerras #endif /* CONFIG_PPC_PSERIES */
5799b6b563cSPaul Mackerras
early_init_dt_scan_memory_ppc(void)5801f012283SRob Herring static int __init early_init_dt_scan_memory_ppc(void)
5819b6b563cSPaul Mackerras {
5826c6ea537SNathan Fontenot #ifdef CONFIG_PPC_PSERIES
5831f012283SRob Herring const void *fdt = initial_boot_params;
5841f012283SRob Herring int node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory");
5851f012283SRob Herring
5861f012283SRob Herring if (node > 0)
587adfefc60SHari Bathini walk_drmem_lmbs_early(node, NULL, early_init_drmem_lmb);
5881f012283SRob Herring
5896c6ea537SNathan Fontenot #endif
5900204568aSPaul Mackerras
5911f012283SRob Herring return early_init_dt_scan_memory();
59251975db0SGrant Likely }
5939b6b563cSPaul Mackerras
594b27652ddSKevin Hao /*
595b27652ddSKevin Hao * For a relocatable kernel, we need to get the memstart_addr first,
596b27652ddSKevin Hao * then use it to calculate the virtual kernel start address. This has
597b27652ddSKevin Hao * to happen at a very early stage (before machine_init). In this case,
598b27652ddSKevin Hao * we just want to get the memstart_address and would not like to mess the
599b27652ddSKevin Hao * memblock at this stage. So introduce a variable to skip the memblock_add()
600b27652ddSKevin Hao * for this reason.
601b27652ddSKevin Hao */
602b27652ddSKevin Hao #ifdef CONFIG_RELOCATABLE
603b27652ddSKevin Hao static int add_mem_to_memblock = 1;
604b27652ddSKevin Hao #else
605b27652ddSKevin Hao #define add_mem_to_memblock 1
606b27652ddSKevin Hao #endif
607b27652ddSKevin Hao
early_init_dt_add_memory_arch(u64 base,u64 size)60851975db0SGrant Likely void __init early_init_dt_add_memory_arch(u64 base, u64 size)
60951975db0SGrant Likely {
610cd3db0c4SBenjamin Herrenschmidt #ifdef CONFIG_PPC64
6119b6b563cSPaul Mackerras if (iommu_is_off) {
6129b6b563cSPaul Mackerras if (base >= 0x80000000ul)
61351975db0SGrant Likely return;
6149b6b563cSPaul Mackerras if ((base + size) > 0x80000000ul)
6159b6b563cSPaul Mackerras size = 0x80000000ul - base;
6169b6b563cSPaul Mackerras }
6179b6b563cSPaul Mackerras #endif
61803bf469aSBenjamin Herrenschmidt /* Keep track of the beginning of memory -and- the size of
61903bf469aSBenjamin Herrenschmidt * the very first block in the device-tree as it represents
62003bf469aSBenjamin Herrenschmidt * the RMA on ppc64 server
62103bf469aSBenjamin Herrenschmidt */
62203bf469aSBenjamin Herrenschmidt if (base < memstart_addr) {
62303bf469aSBenjamin Herrenschmidt memstart_addr = base;
62403bf469aSBenjamin Herrenschmidt first_memblock_size = size;
62503bf469aSBenjamin Herrenschmidt }
626cd3db0c4SBenjamin Herrenschmidt
627cd3db0c4SBenjamin Herrenschmidt /* Add the chunk to the MEMBLOCK list */
6286aba0c84SAneesh Kumar K.V if (add_mem_to_memblock) {
6296aba0c84SAneesh Kumar K.V if (validate_mem_limit(base, &size))
630cd3db0c4SBenjamin Herrenschmidt memblock_add(base, size);
6319b6b563cSPaul Mackerras }
6326aba0c84SAneesh Kumar K.V }
63337dd2badSKumar Gala
early_reserve_mem_dt(void)634c039e3a8SBenjamin Herrenschmidt static void __init early_reserve_mem_dt(void)
6350962e800SJeremy Kerr {
6369d0c4dfeSRob Herring unsigned long i, dt_root;
6379d0c4dfeSRob Herring int len;
6380962e800SJeremy Kerr const __be32 *prop;
6390962e800SJeremy Kerr
64024bbd929SArd Biesheuvel early_init_fdt_reserve_self();
641edba274cSRob Herring early_init_fdt_scan_reserved_mem();
642edba274cSRob Herring
6430962e800SJeremy Kerr dt_root = of_get_flat_dt_root();
6440962e800SJeremy Kerr
6450962e800SJeremy Kerr prop = of_get_flat_dt_prop(dt_root, "reserved-ranges", &len);
6460962e800SJeremy Kerr
6470962e800SJeremy Kerr if (!prop)
648c039e3a8SBenjamin Herrenschmidt return;
649c039e3a8SBenjamin Herrenschmidt
650c039e3a8SBenjamin Herrenschmidt DBG("Found new-style reserved-ranges\n");
6510962e800SJeremy Kerr
6520962e800SJeremy Kerr /* Each reserved range is an (address,size) pair, 2 cells each,
6530962e800SJeremy Kerr * totalling 4 cells per range. */
6540962e800SJeremy Kerr for (i = 0; i < len / (sizeof(*prop) * 4); i++) {
6550962e800SJeremy Kerr u64 base, size;
6560962e800SJeremy Kerr
6570962e800SJeremy Kerr base = of_read_number(prop + (i * 4) + 0, 2);
6580962e800SJeremy Kerr size = of_read_number(prop + (i * 4) + 2, 2);
6590962e800SJeremy Kerr
660c039e3a8SBenjamin Herrenschmidt if (size) {
661c039e3a8SBenjamin Herrenschmidt DBG("reserving: %llx -> %llx\n", base, size);
6620962e800SJeremy Kerr memblock_reserve(base, size);
6630962e800SJeremy Kerr }
664c039e3a8SBenjamin Herrenschmidt }
6650962e800SJeremy Kerr }
6660962e800SJeremy Kerr
early_reserve_mem(void)6679b6b563cSPaul Mackerras static void __init early_reserve_mem(void)
6689b6b563cSPaul Mackerras {
669dc0e643aSIan Munsie __be64 *reserve_map;
6709b6b563cSPaul Mackerras
671dc0e643aSIan Munsie reserve_map = (__be64 *)(((unsigned long)initial_boot_params) +
672060f78c2SRob Herring fdt_off_mem_rsvmap(initial_boot_params));
6734d1f3f25SJimi Xenidis
674c039e3a8SBenjamin Herrenschmidt /* Look for the new "reserved-regions" property in the DT */
675c039e3a8SBenjamin Herrenschmidt early_reserve_mem_dt();
6760962e800SJeremy Kerr
67730437b3eSDavid Gibson #ifdef CONFIG_BLK_DEV_INITRD
678c039e3a8SBenjamin Herrenschmidt /* Then reserve the initrd, if any */
679c039e3a8SBenjamin Herrenschmidt if (initrd_start && (initrd_end > initrd_start)) {
680e96d904eSChristophe Leroy memblock_reserve(ALIGN_DOWN(__pa(initrd_start), PAGE_SIZE),
681b7115316SChristophe Leroy ALIGN(initrd_end, PAGE_SIZE) -
682e96d904eSChristophe Leroy ALIGN_DOWN(initrd_start, PAGE_SIZE));
683c039e3a8SBenjamin Herrenschmidt }
68430437b3eSDavid Gibson #endif /* CONFIG_BLK_DEV_INITRD */
68530437b3eSDavid Gibson
6863accc0faSCédric Le Goater if (!IS_ENABLED(CONFIG_PPC32))
6873accc0faSCédric Le Goater return;
6883accc0faSCédric Le Goater
689cbbcf340SKumar Gala /*
690cbbcf340SKumar Gala * Handle the case where we might be booting from an old kexec
691cbbcf340SKumar Gala * image that setup the mem_rsvmap as pairs of 32-bit values
692cbbcf340SKumar Gala */
693dc0e643aSIan Munsie if (be64_to_cpup(reserve_map) > 0xffffffffull) {
694cbbcf340SKumar Gala u32 base_32, size_32;
695dc0e643aSIan Munsie __be32 *reserve_map_32 = (__be32 *)reserve_map;
696cbbcf340SKumar Gala
697c039e3a8SBenjamin Herrenschmidt DBG("Found old 32-bit reserve map\n");
698c039e3a8SBenjamin Herrenschmidt
699cbbcf340SKumar Gala while (1) {
700dc0e643aSIan Munsie base_32 = be32_to_cpup(reserve_map_32++);
701dc0e643aSIan Munsie size_32 = be32_to_cpup(reserve_map_32++);
702cbbcf340SKumar Gala if (size_32 == 0)
703cbbcf340SKumar Gala break;
704329dda08SKumar Gala DBG("reserving: %x -> %x\n", base_32, size_32);
70595f72d1eSYinghai Lu memblock_reserve(base_32, size_32);
706cbbcf340SKumar Gala }
707cbbcf340SKumar Gala return;
708cbbcf340SKumar Gala }
7099b6b563cSPaul Mackerras }
7109b6b563cSPaul Mackerras
71107fd1761SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
71207fd1761SCyril Bur static bool tm_disabled __initdata;
71307fd1761SCyril Bur
parse_ppc_tm(char * str)71407fd1761SCyril Bur static int __init parse_ppc_tm(char *str)
71507fd1761SCyril Bur {
71607fd1761SCyril Bur bool res;
71707fd1761SCyril Bur
71807fd1761SCyril Bur if (kstrtobool(str, &res))
71907fd1761SCyril Bur return -EINVAL;
72007fd1761SCyril Bur
72107fd1761SCyril Bur tm_disabled = !res;
72207fd1761SCyril Bur
72307fd1761SCyril Bur return 0;
72407fd1761SCyril Bur }
72507fd1761SCyril Bur early_param("ppc_tm", parse_ppc_tm);
72607fd1761SCyril Bur
tm_init(void)72707fd1761SCyril Bur static void __init tm_init(void)
72807fd1761SCyril Bur {
72907fd1761SCyril Bur if (tm_disabled) {
73007fd1761SCyril Bur pr_info("Disabling hardware transactional memory (HTM)\n");
73107fd1761SCyril Bur cur_cpu_spec->cpu_user_features2 &=
73207fd1761SCyril Bur ~(PPC_FEATURE2_HTM_NOSC | PPC_FEATURE2_HTM);
73307fd1761SCyril Bur cur_cpu_spec->cpu_features &= ~CPU_FTR_TM;
73454820530SMichael Ellerman return;
73507fd1761SCyril Bur }
73654820530SMichael Ellerman
73754820530SMichael Ellerman pnv_tm_init();
73807fd1761SCyril Bur }
73907fd1761SCyril Bur #else
tm_init(void)74007fd1761SCyril Bur static void tm_init(void) { }
74107fd1761SCyril Bur #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
74207fd1761SCyril Bur
74354122970SMichael Ellerman static int __init
early_init_dt_scan_model(unsigned long node,const char * uname,int depth,void * data)74454122970SMichael Ellerman early_init_dt_scan_model(unsigned long node, const char *uname,
74554122970SMichael Ellerman int depth, void *data)
74654122970SMichael Ellerman {
74754122970SMichael Ellerman const char *prop;
74854122970SMichael Ellerman
74954122970SMichael Ellerman if (depth != 0)
75054122970SMichael Ellerman return 0;
75154122970SMichael Ellerman
75254122970SMichael Ellerman prop = of_get_flat_dt_prop(node, "model", NULL);
75354122970SMichael Ellerman if (prop)
75454122970SMichael Ellerman seq_buf_printf(&ppc_hw_desc, "%s ", prop);
75554122970SMichael Ellerman
75654122970SMichael Ellerman /* break now */
75754122970SMichael Ellerman return 1;
75854122970SMichael Ellerman }
75954122970SMichael Ellerman
760912c0a7fSMichael Ellerman #ifdef CONFIG_PPC64
save_fscr_to_task(void)761912c0a7fSMichael Ellerman static void __init save_fscr_to_task(void)
762912c0a7fSMichael Ellerman {
763912c0a7fSMichael Ellerman /*
764912c0a7fSMichael Ellerman * Ensure the init_task (pid 0, aka swapper) uses the value of FSCR we
765912c0a7fSMichael Ellerman * have configured via the device tree features or via __init_FSCR().
766912c0a7fSMichael Ellerman * That value will then be propagated to pid 1 (init) and all future
767912c0a7fSMichael Ellerman * processes.
768912c0a7fSMichael Ellerman */
769912c0a7fSMichael Ellerman if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
770912c0a7fSMichael Ellerman init_task.thread.fscr = mfspr(SPRN_FSCR);
771912c0a7fSMichael Ellerman }
772912c0a7fSMichael Ellerman #else
save_fscr_to_task(void)7736c6fdbb2SChengyang Fan static inline void save_fscr_to_task(void) {}
774912c0a7fSMichael Ellerman #endif
775912c0a7fSMichael Ellerman
776912c0a7fSMichael Ellerman
early_init_devtree(void * params)7779b6b563cSPaul Mackerras void __init early_init_devtree(void *params)
7789b6b563cSPaul Mackerras {
77949a84965SBecky Bruce phys_addr_t limit;
7806ca4f749SHollis Blanchard
781b18f0ae9SChristophe Leroy DBG(" -> early_init_devtree(%px)\n", params);
7829b6b563cSPaul Mackerras
783ad72a279SMichael Ellerman /* Too early to BUG_ON(), do it by hand */
784*1103d3b5SUsama Arif if (!early_init_dt_verify(params, __pa(params)))
785ad72a279SMichael Ellerman panic("BUG: Failed verifying flat device tree, bad version?");
786ad72a279SMichael Ellerman
78754122970SMichael Ellerman of_scan_flat_dt(early_init_dt_scan_model, NULL);
78854122970SMichael Ellerman
789458148c0SMichael Ellerman #ifdef CONFIG_PPC_RTAS
790458148c0SMichael Ellerman /* Some machines might need RTAS info for debugging, grab it now. */
791458148c0SMichael Ellerman of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
792458148c0SMichael Ellerman #endif
793458148c0SMichael Ellerman
79414a43e69SBenjamin Herrenschmidt #ifdef CONFIG_PPC_POWERNV
79514a43e69SBenjamin Herrenschmidt /* Some machines might need OPAL info for debugging, grab it now. */
79614a43e69SBenjamin Herrenschmidt of_scan_flat_dt(early_init_dt_scan_opal, NULL);
797bb04ffe8SClaudio Carvalho
798bb04ffe8SClaudio Carvalho /* Scan tree for ultravisor feature */
799bb04ffe8SClaudio Carvalho of_scan_flat_dt(early_init_dt_scan_ultravisor, NULL);
80014a43e69SBenjamin Herrenschmidt #endif
80114a43e69SBenjamin Herrenschmidt
802bec53196SHari Bathini #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
803eb39c880SMahesh Salgaonkar /* scan tree to see if dump is active during last boot */
804eb39c880SMahesh Salgaonkar of_scan_flat_dt(early_init_dt_scan_fw_dump, NULL);
805eb39c880SMahesh Salgaonkar #endif
806eb39c880SMahesh Salgaonkar
8079b6b563cSPaul Mackerras /* Retrieve various informations from the /chosen node of the
8089b6b563cSPaul Mackerras * device-tree, including the platform type, initrd location and
8099b6b563cSPaul Mackerras * size, TCE reserve, and more ...
8109b6b563cSPaul Mackerras */
8113e47d147SAnton Blanchard of_scan_flat_dt(early_init_dt_scan_chosen_ppc, boot_command_line);
8129b6b563cSPaul Mackerras
81395f72d1eSYinghai Lu /* Scan memory nodes and rebuild MEMBLOCKs */
814d665881dSRob Herring early_init_dt_scan_root();
8151f012283SRob Herring early_init_dt_scan_memory_ppc();
816846f77b0SMichael Ellerman
817ca829e05SZhouyi Zhou /*
818ca829e05SZhouyi Zhou * As generic code authors expect to be able to use static keys
819ca829e05SZhouyi Zhou * in early_param() handlers, we initialize the static keys just
820ca829e05SZhouyi Zhou * before parsing early params (it's fine to call jump_label_init()
821ca829e05SZhouyi Zhou * more than once).
822ca829e05SZhouyi Zhou */
823ca829e05SZhouyi Zhou jump_label_init();
824846f77b0SMichael Ellerman parse_early_param();
825846f77b0SMichael Ellerman
826ba14f649SKumar Gala /* make sure we've parsed cmdline for mem= before this */
827ba14f649SKumar Gala if (memory_limit)
828a84fcd46SSuzuki Poulose first_memblock_size = min_t(u64, first_memblock_size, memory_limit);
829ba14f649SKumar Gala setup_initial_memory_limit(memstart_addr, first_memblock_size);
83095f72d1eSYinghai Lu /* Reserve MEMBLOCK regions used by kernel, initrd, dt, etc... */
83156afad88SChristophe Leroy memblock_reserve(PHYSICAL_START, __pa(_end) - PHYSICAL_START);
832549e8152SPaul Mackerras /* If relocatable, reserve first 32k for interrupt vectors etc. */
833549e8152SPaul Mackerras if (PHYSICAL_START > MEMORY_START)
83495f72d1eSYinghai Lu memblock_reserve(MEMORY_START, 0x8000);
83547310413SMichael Ellerman reserve_kdump_trampoline();
836bec53196SHari Bathini #if defined(CONFIG_FA_DUMP) || defined(CONFIG_PRESERVE_FA_DUMP)
837eb39c880SMahesh Salgaonkar /*
838eb39c880SMahesh Salgaonkar * If we fail to reserve memory for firmware-assisted dump then
839eb39c880SMahesh Salgaonkar * fallback to kexec based kdump.
840eb39c880SMahesh Salgaonkar */
841eb39c880SMahesh Salgaonkar if (fadump_reserve_mem() == 0)
842eb39c880SMahesh Salgaonkar #endif
84335dd5432SMichael Ellerman reserve_crashkernel();
8449b6b563cSPaul Mackerras early_reserve_mem();
8459b6b563cSPaul Mackerras
84614ed7409SAnton Blanchard /* Ensure that total memory size is page-aligned. */
8476fbef13cSTejun Heo limit = ALIGN(memory_limit ?: memblock_phys_mem_size(), PAGE_SIZE);
84895f72d1eSYinghai Lu memblock_enforce_memory_limit(limit);
8496ca4f749SHollis Blanchard
850b32d5d7eSAneesh Kumar K.V #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_PPC_4K_PAGES)
851b32d5d7eSAneesh Kumar K.V if (!early_radix_enabled())
852b32d5d7eSAneesh Kumar K.V memblock_cap_memory_range(0, 1UL << (H_MAX_PHYSMEM_BITS));
853b32d5d7eSAneesh Kumar K.V #endif
854b32d5d7eSAneesh Kumar K.V
8551aadc056STejun Heo memblock_allow_resize();
85695f72d1eSYinghai Lu memblock_dump_all();
8572babf5c2SMichael Ellerman
858b18f0ae9SChristophe Leroy DBG("Phys. mem: %llx\n", (unsigned long long)memblock_phys_mem_size());
8592babf5c2SMichael Ellerman
8602babf5c2SMichael Ellerman /* We may need to relocate the flat tree, do it now.
8612babf5c2SMichael Ellerman * FIXME .. and the initrd too? */
8622babf5c2SMichael Ellerman move_device_tree();
8632babf5c2SMichael Ellerman
8649b6b563cSPaul Mackerras DBG("Scanning CPUs ...\n");
8659b6b563cSPaul Mackerras
8665a61ef74SNicholas Piggin dt_cpu_ftrs_scan();
8675a61ef74SNicholas Piggin
86825985edcSLucas De Marchi /* Retrieve CPU related informations from the flat tree
8693c726f8dSBenjamin Herrenschmidt * (altivec support, boot CPU ID, ...)
8709b6b563cSPaul Mackerras */
871dc222fa7SNicholas Piggin of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
87236ae37e3SBenjamin Herrenschmidt if (boot_cpuid < 0) {
873d939be3aSMasanari Iida printk("Failed to identify boot CPU !\n");
87436ae37e3SBenjamin Herrenschmidt BUG();
87536ae37e3SBenjamin Herrenschmidt }
8769b6b563cSPaul Mackerras
877912c0a7fSMichael Ellerman save_fscr_to_task();
878912c0a7fSMichael Ellerman
8797ac87abbSMatt Evans #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
8807ac87abbSMatt Evans /* We'll later wait for secondaries to check in; there are
8817ac87abbSMatt Evans * NCPUS-1 non-boot CPUs :-)
8827ac87abbSMatt Evans */
8837ac87abbSMatt Evans spinning_secondaries = boot_cpu_count - 1;
8847ac87abbSMatt Evans #endif
8857ac87abbSMatt Evans
8861a01dc87SMichael Ellerman mmu_early_init_devtree();
8871a01dc87SMichael Ellerman
88855672ecfSMahesh Salgaonkar #ifdef CONFIG_PPC_POWERNV
88955672ecfSMahesh Salgaonkar /* Scan and build the list of machine check recoverable ranges */
89055672ecfSMahesh Salgaonkar of_scan_flat_dt(early_init_dt_scan_recoverable_ranges, NULL);
89155672ecfSMahesh Salgaonkar #endif
892da6a97bfSBenjamin Herrenschmidt epapr_paravirt_early_init();
89355672ecfSMahesh Salgaonkar
8943808a889SBenjamin Herrenschmidt /* Now try to figure out if we are running on LPAR and so on */
8953808a889SBenjamin Herrenschmidt pseries_probe_fw_features();
8963808a889SBenjamin Herrenschmidt
897d3cd91fbSAneesh Kumar K.V /*
898d3cd91fbSAneesh Kumar K.V * Initialize pkey features and default AMR/IAMR values
899d3cd91fbSAneesh Kumar K.V */
900d3cd91fbSAneesh Kumar K.V pkey_early_init_devtree();
901d3cd91fbSAneesh Kumar K.V
902166dd7d3SBenjamin Herrenschmidt #ifdef CONFIG_PPC_PS3
903166dd7d3SBenjamin Herrenschmidt /* Identify PS3 firmware */
904166dd7d3SBenjamin Herrenschmidt if (of_flat_dt_is_compatible(of_get_flat_dt_root(), "sony,ps3"))
905166dd7d3SBenjamin Herrenschmidt powerpc_firmware_features |= FW_FEATURE_PS3_POSSIBLE;
906166dd7d3SBenjamin Herrenschmidt #endif
907166dd7d3SBenjamin Herrenschmidt
90891361b51SRussell Currey /* If kexec left a PLPKS password in the DT, get it and clear it */
90991361b51SRussell Currey plpks_early_init_devtree();
91091361b51SRussell Currey
91107fd1761SCyril Bur tm_init();
91207fd1761SCyril Bur
9139b6b563cSPaul Mackerras DBG(" <- early_init_devtree()\n");
9149b6b563cSPaul Mackerras }
9159b6b563cSPaul Mackerras
916b27652ddSKevin Hao #ifdef CONFIG_RELOCATABLE
917b27652ddSKevin Hao /*
918b27652ddSKevin Hao * This function run before early_init_devtree, so we have to init
919b27652ddSKevin Hao * initial_boot_params.
920b27652ddSKevin Hao */
early_get_first_memblock_info(void * params,phys_addr_t * size)921b27652ddSKevin Hao void __init early_get_first_memblock_info(void *params, phys_addr_t *size)
922b27652ddSKevin Hao {
923b27652ddSKevin Hao /* Setup flat device-tree pointer */
924b27652ddSKevin Hao initial_boot_params = params;
925b27652ddSKevin Hao
926b27652ddSKevin Hao /*
927b27652ddSKevin Hao * Scan the memory nodes and set add_mem_to_memblock to 0 to avoid
928b27652ddSKevin Hao * mess the memblock.
929b27652ddSKevin Hao */
930b27652ddSKevin Hao add_mem_to_memblock = 0;
931d665881dSRob Herring early_init_dt_scan_root();
9321f012283SRob Herring early_init_dt_scan_memory_ppc();
933b27652ddSKevin Hao add_mem_to_memblock = 1;
934b27652ddSKevin Hao
935b27652ddSKevin Hao if (size)
936b27652ddSKevin Hao *size = first_memblock_size;
937b27652ddSKevin Hao }
938b27652ddSKevin Hao #endif
939b27652ddSKevin Hao
9409b6b563cSPaul Mackerras /*******
9419b6b563cSPaul Mackerras *
9429b6b563cSPaul Mackerras * New implementation of the OF "find" APIs, return a refcounted
9439b6b563cSPaul Mackerras * object, call of_node_put() when done. The device tree and list
9449b6b563cSPaul Mackerras * are protected by a rw_lock.
9459b6b563cSPaul Mackerras *
9469b6b563cSPaul Mackerras * Note that property management will need some locking as well,
9479b6b563cSPaul Mackerras * this isn't dealt with yet.
9489b6b563cSPaul Mackerras *
9499b6b563cSPaul Mackerras *******/
9509b6b563cSPaul Mackerras
9519b6b563cSPaul Mackerras /**
952b37193b7SBenjamin Herrenschmidt * of_get_ibm_chip_id - Returns the IBM "chip-id" of a device
953b37193b7SBenjamin Herrenschmidt * @np: device node of the device
954b37193b7SBenjamin Herrenschmidt *
955b37193b7SBenjamin Herrenschmidt * This looks for a property "ibm,chip-id" in the node or any
956b37193b7SBenjamin Herrenschmidt * of its parents and returns its content, or -1 if it cannot
957b37193b7SBenjamin Herrenschmidt * be found.
958b37193b7SBenjamin Herrenschmidt */
of_get_ibm_chip_id(struct device_node * np)959b37193b7SBenjamin Herrenschmidt int of_get_ibm_chip_id(struct device_node *np)
960b37193b7SBenjamin Herrenschmidt {
961b37193b7SBenjamin Herrenschmidt of_node_get(np);
962b37193b7SBenjamin Herrenschmidt while (np) {
9631856f50cSChristophe Jaillet u32 chip_id;
964b37193b7SBenjamin Herrenschmidt
9651856f50cSChristophe Jaillet /*
9661856f50cSChristophe Jaillet * Skiboot may produce memory nodes that contain more than one
9671856f50cSChristophe Jaillet * cell in chip-id, we only read the first one here.
9681856f50cSChristophe Jaillet */
9691856f50cSChristophe Jaillet if (!of_property_read_u32(np, "ibm,chip-id", &chip_id)) {
970b37193b7SBenjamin Herrenschmidt of_node_put(np);
9711856f50cSChristophe Jaillet return chip_id;
972b37193b7SBenjamin Herrenschmidt }
97316c1d606SMichael Ellerman
97416c1d606SMichael Ellerman np = of_get_next_parent(np);
975b37193b7SBenjamin Herrenschmidt }
976b37193b7SBenjamin Herrenschmidt return -1;
977b37193b7SBenjamin Herrenschmidt }
978b130e7c0SDan Streetman EXPORT_SYMBOL(of_get_ibm_chip_id);
979b37193b7SBenjamin Herrenschmidt
9803eb906c6SMichael Ellerman /**
9813eb906c6SMichael Ellerman * cpu_to_chip_id - Return the cpus chip-id
9823eb906c6SMichael Ellerman * @cpu: The logical cpu number.
9833eb906c6SMichael Ellerman *
9843eb906c6SMichael Ellerman * Return the value of the ibm,chip-id property corresponding to the given
9853eb906c6SMichael Ellerman * logical cpu number. If the chip-id can not be found, returns -1.
9863eb906c6SMichael Ellerman */
cpu_to_chip_id(int cpu)9873eb906c6SMichael Ellerman int cpu_to_chip_id(int cpu)
9883eb906c6SMichael Ellerman {
9893eb906c6SMichael Ellerman struct device_node *np;
990c1e53367SSrikar Dronamraju int ret = -1, idx;
991c1e53367SSrikar Dronamraju
992c1e53367SSrikar Dronamraju idx = cpu / threads_per_core;
993c1e53367SSrikar Dronamraju if (chip_id_lookup_table && chip_id_lookup_table[idx] != -1)
994c1e53367SSrikar Dronamraju return chip_id_lookup_table[idx];
9953eb906c6SMichael Ellerman
9963eb906c6SMichael Ellerman np = of_get_cpu_node(cpu, NULL);
997c1e53367SSrikar Dronamraju if (np) {
998c1e53367SSrikar Dronamraju ret = of_get_ibm_chip_id(np);
9993eb906c6SMichael Ellerman of_node_put(np);
1000c1e53367SSrikar Dronamraju
1001c1e53367SSrikar Dronamraju if (chip_id_lookup_table)
1002c1e53367SSrikar Dronamraju chip_id_lookup_table[idx] = ret;
1003c1e53367SSrikar Dronamraju }
1004c1e53367SSrikar Dronamraju
1005c1e53367SSrikar Dronamraju return ret;
10063eb906c6SMichael Ellerman }
10073eb906c6SMichael Ellerman EXPORT_SYMBOL(cpu_to_chip_id);
10083eb906c6SMichael Ellerman
arch_match_cpu_phys_id(int cpu,u64 phys_id)1009819d5965SSudeep KarkadaNagesha bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
1010819d5965SSudeep KarkadaNagesha {
10119f593f13SNicholas Piggin #ifdef CONFIG_SMP
10129f593f13SNicholas Piggin /*
10139f593f13SNicholas Piggin * Early firmware scanning must use this rather than
10149f593f13SNicholas Piggin * get_hard_smp_processor_id because we don't have pacas allocated
10159f593f13SNicholas Piggin * until memory topology is discovered.
10169f593f13SNicholas Piggin */
10179f593f13SNicholas Piggin if (cpu_to_phys_id != NULL)
10189f593f13SNicholas Piggin return (int)phys_id == cpu_to_phys_id[cpu];
10199f593f13SNicholas Piggin #endif
10209f593f13SNicholas Piggin
1021819d5965SSudeep KarkadaNagesha return (int)phys_id == get_hard_smp_processor_id(cpu);
1022819d5965SSudeep KarkadaNagesha }
1023