12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
240ef8cbcSPaul Mackerras /*
340ef8cbcSPaul Mackerras *
440ef8cbcSPaul Mackerras * Common boot and setup code.
540ef8cbcSPaul Mackerras *
640ef8cbcSPaul Mackerras * Copyright (C) 2001 PPC64 Team, IBM Corp
740ef8cbcSPaul Mackerras */
840ef8cbcSPaul Mackerras
94b16f8e2SPaul Gortmaker #include <linux/export.h>
1040ef8cbcSPaul Mackerras #include <linux/string.h>
1140ef8cbcSPaul Mackerras #include <linux/sched.h>
1240ef8cbcSPaul Mackerras #include <linux/init.h>
1340ef8cbcSPaul Mackerras #include <linux/kernel.h>
1440ef8cbcSPaul Mackerras #include <linux/reboot.h>
1540ef8cbcSPaul Mackerras #include <linux/delay.h>
1640ef8cbcSPaul Mackerras #include <linux/initrd.h>
1740ef8cbcSPaul Mackerras #include <linux/seq_file.h>
1840ef8cbcSPaul Mackerras #include <linux/ioport.h>
1940ef8cbcSPaul Mackerras #include <linux/console.h>
2040ef8cbcSPaul Mackerras #include <linux/utsname.h>
2140ef8cbcSPaul Mackerras #include <linux/tty.h>
2240ef8cbcSPaul Mackerras #include <linux/root_dev.h>
2340ef8cbcSPaul Mackerras #include <linux/notifier.h>
2440ef8cbcSPaul Mackerras #include <linux/cpu.h>
2540ef8cbcSPaul Mackerras #include <linux/unistd.h>
2640ef8cbcSPaul Mackerras #include <linux/serial.h>
2740ef8cbcSPaul Mackerras #include <linux/serial_8250.h>
2857c8a661SMike Rapoport #include <linux/memblock.h>
2912d04eefSBenjamin Herrenschmidt #include <linux/pci.h>
30945feb17SBenjamin Herrenschmidt #include <linux/lockdep.h>
31a5d86257SAnton Blanchard #include <linux/memory.h>
32c54b2bf1SAnton Blanchard #include <linux/nmi.h>
3365fddcfcSMike Rapoport #include <linux/pgtable.h>
34e6f6390aSChristophe Leroy #include <linux/of.h>
35e6f6390aSChristophe Leroy #include <linux/of_fdt.h>
36a6146888SBecky Bruce
372f5182cfSNicholas Piggin #include <asm/asm-prototypes.h>
38633c8e98SNicholas Piggin #include <asm/kvm_guest.h>
3940ef8cbcSPaul Mackerras #include <asm/io.h>
400cc4746cSMichael Ellerman #include <asm/kdump.h>
4140ef8cbcSPaul Mackerras #include <asm/processor.h>
4240ef8cbcSPaul Mackerras #include <asm/smp.h>
4340ef8cbcSPaul Mackerras #include <asm/elf.h>
4440ef8cbcSPaul Mackerras #include <asm/machdep.h>
4540ef8cbcSPaul Mackerras #include <asm/paca.h>
4640ef8cbcSPaul Mackerras #include <asm/time.h>
4740ef8cbcSPaul Mackerras #include <asm/cputable.h>
485a61ef74SNicholas Piggin #include <asm/dt_cpu_ftrs.h>
4940ef8cbcSPaul Mackerras #include <asm/sections.h>
5040ef8cbcSPaul Mackerras #include <asm/btext.h>
5140ef8cbcSPaul Mackerras #include <asm/nvram.h>
5240ef8cbcSPaul Mackerras #include <asm/setup.h>
5340ef8cbcSPaul Mackerras #include <asm/rtas.h>
5440ef8cbcSPaul Mackerras #include <asm/iommu.h>
5540ef8cbcSPaul Mackerras #include <asm/serial.h>
5640ef8cbcSPaul Mackerras #include <asm/cache.h>
5740ef8cbcSPaul Mackerras #include <asm/page.h>
5840ef8cbcSPaul Mackerras #include <asm/mmu.h>
5940ef8cbcSPaul Mackerras #include <asm/firmware.h>
60f78541dcSPaul Mackerras #include <asm/xmon.h>
61dcad47fcSDavid Gibson #include <asm/udbg.h>
62593e537bSMichael Ellerman #include <asm/kexec.h>
63d36b4c4fSKumar Gala #include <asm/code-patching.h>
645d7c8545SChristophe Leroy #include <asm/ftrace.h>
65d3cbff1bSBenjamin Herrenschmidt #include <asm/opal.h>
66b1923caaSBenjamin Herrenschmidt #include <asm/cputhreads.h>
67c2e480baSMadhavan Srinivasan #include <asm/hw_irq.h>
682c86cd18SChristophe Leroy #include <asm/feature-fixups.h>
6969795cabSChristophe Leroy #include <asm/kup.h>
70265c3491SChristophe Leroy #include <asm/early_ioremap.h>
71eb553f16SAneesh Kumar K.V #include <asm/pgalloc.h>
7240ef8cbcSPaul Mackerras
731696d0fbSNicholas Piggin #include "setup.h"
741696d0fbSNicholas Piggin
758246aca7SChen Gang int spinning_secondaries;
7640ef8cbcSPaul Mackerras u64 ppc64_pft_size;
7740ef8cbcSPaul Mackerras
78dabcafd3SOlof Johansson struct ppc64_caches ppc64_caches = {
79e2827fe5SBenjamin Herrenschmidt .l1d = {
80e2827fe5SBenjamin Herrenschmidt .block_size = 0x40,
81e2827fe5SBenjamin Herrenschmidt .log_block_size = 6,
82e2827fe5SBenjamin Herrenschmidt },
83e2827fe5SBenjamin Herrenschmidt .l1i = {
84e2827fe5SBenjamin Herrenschmidt .block_size = 0x40,
85e2827fe5SBenjamin Herrenschmidt .log_block_size = 6
86e2827fe5SBenjamin Herrenschmidt },
87dabcafd3SOlof Johansson };
8840ef8cbcSPaul Mackerras EXPORT_SYMBOL_GPL(ppc64_caches);
8940ef8cbcSPaul Mackerras
90e0d68273SChristophe Leroy #if defined(CONFIG_PPC_BOOK3E_64) && defined(CONFIG_SMP)
setup_tlb_core_data(void)91b1923caaSBenjamin Herrenschmidt void __init setup_tlb_core_data(void)
9228efc35fSScott Wood {
9328efc35fSScott Wood int cpu;
9428efc35fSScott Wood
9582d86de2SScott Wood BUILD_BUG_ON(offsetof(struct tlb_core_data, lock) != 0);
9682d86de2SScott Wood
9728efc35fSScott Wood for_each_possible_cpu(cpu) {
9828efc35fSScott Wood int first = cpu_first_thread_sibling(cpu);
9928efc35fSScott Wood
100d9e1831aSScott Wood /*
101d9e1831aSScott Wood * If we boot via kdump on a non-primary thread,
102d9e1831aSScott Wood * make sure we point at the thread that actually
103d9e1831aSScott Wood * set up this TLB.
104d9e1831aSScott Wood */
105d9e1831aSScott Wood if (cpu_first_thread_sibling(boot_cpuid) == first)
106d9e1831aSScott Wood first = boot_cpuid;
107d9e1831aSScott Wood
108d2e60075SNicholas Piggin paca_ptrs[cpu]->tcd_ptr = &paca_ptrs[first]->tcd;
10928efc35fSScott Wood
11028efc35fSScott Wood /*
11128efc35fSScott Wood * If we have threads, we need either tlbsrx.
11228efc35fSScott Wood * or e6500 tablewalk mode, or else TLB handlers
11328efc35fSScott Wood * will be racy and could produce duplicate entries.
1140d2b5cdcSMichael Ellerman * Should we panic instead?
11528efc35fSScott Wood */
1160d2b5cdcSMichael Ellerman WARN_ONCE(smt_enabled_at_boot >= 2 &&
1170d2b5cdcSMichael Ellerman book3e_htw_mode != PPC_HTW_E6500,
1180d2b5cdcSMichael Ellerman "%s: unsupported MMU configuration\n", __func__);
11928efc35fSScott Wood }
12028efc35fSScott Wood }
12128efc35fSScott Wood #endif
12228efc35fSScott Wood
12340ef8cbcSPaul Mackerras #ifdef CONFIG_SMP
12440ef8cbcSPaul Mackerras
125954e6da5SNathan Fontenot static char *smt_enabled_cmdline;
12640ef8cbcSPaul Mackerras
12740ef8cbcSPaul Mackerras /* Look for ibm,smt-enabled OF option */
check_smt_enabled(void)128b1923caaSBenjamin Herrenschmidt void __init check_smt_enabled(void)
12940ef8cbcSPaul Mackerras {
13040ef8cbcSPaul Mackerras struct device_node *dn;
131a7f67bdfSJeremy Kerr const char *smt_option;
13240ef8cbcSPaul Mackerras
133954e6da5SNathan Fontenot /* Default to enabling all threads */
134954e6da5SNathan Fontenot smt_enabled_at_boot = threads_per_core;
135954e6da5SNathan Fontenot
13640ef8cbcSPaul Mackerras /* Allow the command line to overrule the OF option */
137954e6da5SNathan Fontenot if (smt_enabled_cmdline) {
138954e6da5SNathan Fontenot if (!strcmp(smt_enabled_cmdline, "on"))
139954e6da5SNathan Fontenot smt_enabled_at_boot = threads_per_core;
140954e6da5SNathan Fontenot else if (!strcmp(smt_enabled_cmdline, "off"))
141954e6da5SNathan Fontenot smt_enabled_at_boot = 0;
142954e6da5SNathan Fontenot else {
1431618bd53SDaniel Walter int smt;
144954e6da5SNathan Fontenot int rc;
14540ef8cbcSPaul Mackerras
1461618bd53SDaniel Walter rc = kstrtoint(smt_enabled_cmdline, 10, &smt);
147954e6da5SNathan Fontenot if (!rc)
148954e6da5SNathan Fontenot smt_enabled_at_boot =
1491618bd53SDaniel Walter min(threads_per_core, smt);
150954e6da5SNathan Fontenot }
151954e6da5SNathan Fontenot } else {
15240ef8cbcSPaul Mackerras dn = of_find_node_by_path("/options");
15340ef8cbcSPaul Mackerras if (dn) {
154954e6da5SNathan Fontenot smt_option = of_get_property(dn, "ibm,smt-enabled",
155954e6da5SNathan Fontenot NULL);
15640ef8cbcSPaul Mackerras
15740ef8cbcSPaul Mackerras if (smt_option) {
15840ef8cbcSPaul Mackerras if (!strcmp(smt_option, "on"))
159954e6da5SNathan Fontenot smt_enabled_at_boot = threads_per_core;
16040ef8cbcSPaul Mackerras else if (!strcmp(smt_option, "off"))
16140ef8cbcSPaul Mackerras smt_enabled_at_boot = 0;
16240ef8cbcSPaul Mackerras }
163954e6da5SNathan Fontenot
164954e6da5SNathan Fontenot of_node_put(dn);
165954e6da5SNathan Fontenot }
16640ef8cbcSPaul Mackerras }
16740ef8cbcSPaul Mackerras }
16840ef8cbcSPaul Mackerras
16940ef8cbcSPaul Mackerras /* Look for smt-enabled= cmdline option */
early_smt_enabled(char * p)17040ef8cbcSPaul Mackerras static int __init early_smt_enabled(char *p)
17140ef8cbcSPaul Mackerras {
172954e6da5SNathan Fontenot smt_enabled_cmdline = p;
17340ef8cbcSPaul Mackerras return 0;
17440ef8cbcSPaul Mackerras }
17540ef8cbcSPaul Mackerras early_param("smt-enabled", early_smt_enabled);
17640ef8cbcSPaul Mackerras
17740ef8cbcSPaul Mackerras #endif /* CONFIG_SMP */
17840ef8cbcSPaul Mackerras
17925e13814SMichael Ellerman /** Fix up paca fields required for the boot cpu */
fixup_boot_paca(struct paca_struct * boot_paca)180519b2e31SNicholas Piggin static void __init fixup_boot_paca(struct paca_struct *boot_paca)
18125e13814SMichael Ellerman {
18225e13814SMichael Ellerman /* The boot cpu is started */
183519b2e31SNicholas Piggin boot_paca->cpu_start = 1;
1842f5182cfSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
1852f5182cfSNicholas Piggin /*
1862f5182cfSNicholas Piggin * Give the early boot machine check stack somewhere to use, use
1872f5182cfSNicholas Piggin * half of the init stack. This is a bit hacky but there should not be
1882f5182cfSNicholas Piggin * deep stack usage in early init so shouldn't overflow it or overwrite
1892f5182cfSNicholas Piggin * things.
1902f5182cfSNicholas Piggin */
191519b2e31SNicholas Piggin boot_paca->mc_emergency_sp = (void *)&init_thread_union +
1922f5182cfSNicholas Piggin (THREAD_SIZE/2);
1932f5182cfSNicholas Piggin #endif
19425e13814SMichael Ellerman /* Allow percpu accesses to work until we setup percpu data */
195519b2e31SNicholas Piggin boot_paca->data_offset = 0;
196799f7063SNicholas Piggin /* Mark interrupts soft and hard disabled in PACA */
197519b2e31SNicholas Piggin boot_paca->irq_soft_mask = IRQS_DISABLED;
198519b2e31SNicholas Piggin boot_paca->irq_happened = PACA_IRQ_HARD_DIS;
199799f7063SNicholas Piggin WARN_ON(mfmsr() & MSR_EE);
20025e13814SMichael Ellerman }
20125e13814SMichael Ellerman
configure_exceptions(void)202009776baSBenjamin Herrenschmidt static void __init configure_exceptions(void)
2038f619b54SBenjamin Herrenschmidt {
204d3cbff1bSBenjamin Herrenschmidt /*
205d3cbff1bSBenjamin Herrenschmidt * Setup the trampolines from the lowmem exception vectors
206d3cbff1bSBenjamin Herrenschmidt * to the kdump kernel when not using a relocatable kernel.
207d3cbff1bSBenjamin Herrenschmidt */
208d3cbff1bSBenjamin Herrenschmidt setup_kdump_trampoline();
209d3cbff1bSBenjamin Herrenschmidt
210d3cbff1bSBenjamin Herrenschmidt /* Under a PAPR hypervisor, we need hypercalls */
211d3cbff1bSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_SET_MODE)) {
212b5149e22SNicholas Piggin /*
213b5149e22SNicholas Piggin * - PR KVM does not support AIL mode interrupts in the host
214b5149e22SNicholas Piggin * while a PR guest is running.
215b5149e22SNicholas Piggin *
216b5149e22SNicholas Piggin * - SCV system call interrupt vectors are only implemented for
217b5149e22SNicholas Piggin * AIL mode interrupts.
218b5149e22SNicholas Piggin *
219b5149e22SNicholas Piggin * - On pseries, AIL mode can only be enabled and disabled
220b5149e22SNicholas Piggin * system-wide so when a PR VM is created on a pseries host,
221b5149e22SNicholas Piggin * all CPUs of the host are set to AIL=0 mode.
222b5149e22SNicholas Piggin *
223b5149e22SNicholas Piggin * - Therefore host CPUs must not execute scv while a PR VM
224b5149e22SNicholas Piggin * exists.
225b5149e22SNicholas Piggin *
226b5149e22SNicholas Piggin * - SCV support can not be disabled dynamically because the
227b5149e22SNicholas Piggin * feature is advertised to host userspace. Disabling the
228b5149e22SNicholas Piggin * facility and emulating it would be possible but is not
229b5149e22SNicholas Piggin * implemented.
230b5149e22SNicholas Piggin *
231b5149e22SNicholas Piggin * - So SCV support is blanket disabled if PR KVM could possibly
232b5149e22SNicholas Piggin * run. That is, PR support compiled in, booting on pseries
233b5149e22SNicholas Piggin * with hash MMU.
234b5149e22SNicholas Piggin */
235b5149e22SNicholas Piggin if (IS_ENABLED(CONFIG_KVM_BOOK3S_PR_POSSIBLE) && !radix_enabled()) {
236b5149e22SNicholas Piggin init_task.thread.fscr &= ~FSCR_SCV;
237b5149e22SNicholas Piggin cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_SCV;
238b5149e22SNicholas Piggin }
239b5149e22SNicholas Piggin
240d3cbff1bSBenjamin Herrenschmidt /* Enable AIL if possible */
2417fa95f9aSNicholas Piggin if (!pseries_enable_reloc_on_exc()) {
2427fa95f9aSNicholas Piggin init_task.thread.fscr &= ~FSCR_SCV;
2437fa95f9aSNicholas Piggin cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_SCV;
2447fa95f9aSNicholas Piggin }
2458f619b54SBenjamin Herrenschmidt
246633440f1SMichael Ellerman /*
247d3cbff1bSBenjamin Herrenschmidt * Tell the hypervisor that we want our exceptions to
248d3cbff1bSBenjamin Herrenschmidt * be taken in little endian mode.
249d3cbff1bSBenjamin Herrenschmidt *
250d3cbff1bSBenjamin Herrenschmidt * We don't call this for big endian as our calling convention
251d3cbff1bSBenjamin Herrenschmidt * makes us always enter in BE, and the call may fail under
252d3cbff1bSBenjamin Herrenschmidt * some circumstances with kdump.
253633440f1SMichael Ellerman */
254d3cbff1bSBenjamin Herrenschmidt #ifdef __LITTLE_ENDIAN__
255d3cbff1bSBenjamin Herrenschmidt pseries_little_endian_exceptions();
256d3cbff1bSBenjamin Herrenschmidt #endif
257d3cbff1bSBenjamin Herrenschmidt } else {
258d3cbff1bSBenjamin Herrenschmidt /* Set endian mode using OPAL */
259d3cbff1bSBenjamin Herrenschmidt if (firmware_has_feature(FW_FEATURE_OPAL))
260d3cbff1bSBenjamin Herrenschmidt opal_configure_cores();
261d3cbff1bSBenjamin Herrenschmidt
262c0a36013SBenjamin Herrenschmidt /* AIL on native is done in cpu_ready_for_interrupts() */
2638f619b54SBenjamin Herrenschmidt }
264d3cbff1bSBenjamin Herrenschmidt }
265d3cbff1bSBenjamin Herrenschmidt
cpu_ready_for_interrupts(void)266d3cbff1bSBenjamin Herrenschmidt static void cpu_ready_for_interrupts(void)
267d3cbff1bSBenjamin Herrenschmidt {
268c0a36013SBenjamin Herrenschmidt /*
269c0a36013SBenjamin Herrenschmidt * Enable AIL if supported, and we are in hypervisor mode. This
270c0a36013SBenjamin Herrenschmidt * is called once for every processor.
271c0a36013SBenjamin Herrenschmidt *
272c0a36013SBenjamin Herrenschmidt * If we are not in hypervisor mode the job is done once for
273c0a36013SBenjamin Herrenschmidt * the whole partition in configure_exceptions().
274c0a36013SBenjamin Herrenschmidt */
27549c1d07fSNicholas Piggin if (cpu_has_feature(CPU_FTR_HVMODE)) {
276c0a36013SBenjamin Herrenschmidt unsigned long lpcr = mfspr(SPRN_LPCR);
27749c1d07fSNicholas Piggin unsigned long new_lpcr = lpcr;
27849c1d07fSNicholas Piggin
27949c1d07fSNicholas Piggin if (cpu_has_feature(CPU_FTR_ARCH_31)) {
28049c1d07fSNicholas Piggin /* P10 DD1 does not have HAIL */
28149c1d07fSNicholas Piggin if (pvr_version_is(PVR_POWER10) &&
28249c1d07fSNicholas Piggin (mfspr(SPRN_PVR) & 0xf00) == 0x100)
28349c1d07fSNicholas Piggin new_lpcr |= LPCR_AIL_3;
28449c1d07fSNicholas Piggin else
28549c1d07fSNicholas Piggin new_lpcr |= LPCR_HAIL;
28649c1d07fSNicholas Piggin } else if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
28749c1d07fSNicholas Piggin new_lpcr |= LPCR_AIL_3;
28849c1d07fSNicholas Piggin }
28949c1d07fSNicholas Piggin
29049c1d07fSNicholas Piggin if (new_lpcr != lpcr)
29149c1d07fSNicholas Piggin mtspr(SPRN_LPCR, new_lpcr);
292c0a36013SBenjamin Herrenschmidt }
293c0a36013SBenjamin Herrenschmidt
2947ed23e1bSBenjamin Herrenschmidt /*
295dd9a8c5aSMichael Neuling * Set HFSCR:TM based on CPU features:
296dd9a8c5aSMichael Neuling * In the special case of TM no suspend (P9N DD2.1), Linux is
297dd9a8c5aSMichael Neuling * told TM is off via the dt-ftrs but told to (partially) use
298dd9a8c5aSMichael Neuling * it via OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED. So HFSCR[TM]
299dd9a8c5aSMichael Neuling * will be off from dt-ftrs but we need to turn it on for the
300dd9a8c5aSMichael Neuling * no suspend case.
3017ed23e1bSBenjamin Herrenschmidt */
302dd9a8c5aSMichael Neuling if (cpu_has_feature(CPU_FTR_HVMODE)) {
303dd9a8c5aSMichael Neuling if (cpu_has_feature(CPU_FTR_TM_COMP))
304dd9a8c5aSMichael Neuling mtspr(SPRN_HFSCR, mfspr(SPRN_HFSCR) | HFSCR_TM);
305dd9a8c5aSMichael Neuling else
3067ed23e1bSBenjamin Herrenschmidt mtspr(SPRN_HFSCR, mfspr(SPRN_HFSCR) & ~HFSCR_TM);
307dd9a8c5aSMichael Neuling }
3087ed23e1bSBenjamin Herrenschmidt
309d3cbff1bSBenjamin Herrenschmidt /* Set IR and DR in PACA MSR */
310d3cbff1bSBenjamin Herrenschmidt get_paca()->kernel_msr = MSR_KERNEL;
311d3cbff1bSBenjamin Herrenschmidt }
3128f619b54SBenjamin Herrenschmidt
313c0abd0c7SNicholas Piggin unsigned long spr_default_dscr = 0;
314c0abd0c7SNicholas Piggin
record_spr_defaults(void)315692e5928SCédric Le Goater static void __init record_spr_defaults(void)
316c0abd0c7SNicholas Piggin {
317c0abd0c7SNicholas Piggin if (early_cpu_has_feature(CPU_FTR_DSCR))
318c0abd0c7SNicholas Piggin spr_default_dscr = mfspr(SPRN_DSCR);
319c0abd0c7SNicholas Piggin }
320c0abd0c7SNicholas Piggin
32140ef8cbcSPaul Mackerras /*
32240ef8cbcSPaul Mackerras * Early initialization entry point. This is called by head.S
32340ef8cbcSPaul Mackerras * with MMU translation disabled. We rely on the "feature" of
32440ef8cbcSPaul Mackerras * the CPU that ignores the top 2 bits of the address in real
32540ef8cbcSPaul Mackerras * mode so we can access kernel globals normally provided we
32640ef8cbcSPaul Mackerras * only toy with things in the RMO region. From here, we do
32795f72d1eSYinghai Lu * some early parsing of the device-tree to setup out MEMBLOCK
32840ef8cbcSPaul Mackerras * data structures, and allocate & initialize the hash table
32940ef8cbcSPaul Mackerras * and segment tables so we can start running with translation
33040ef8cbcSPaul Mackerras * enabled.
33140ef8cbcSPaul Mackerras *
33240ef8cbcSPaul Mackerras * It is this function which will call the probe() callback of
33340ef8cbcSPaul Mackerras * the various platform types and copy the matching one to the
33440ef8cbcSPaul Mackerras * global ppc_md structure. Your platform can eventually do
33540ef8cbcSPaul Mackerras * some very early initializations from the probe() routine, but
33640ef8cbcSPaul Mackerras * this is not recommended, be very careful as, for example, the
33740ef8cbcSPaul Mackerras * device-tree is not accessible via normal means at this point.
33840ef8cbcSPaul Mackerras */
33940ef8cbcSPaul Mackerras
early_setup(unsigned long dt_ptr)340a7223f5bSArd Biesheuvel void __init early_setup(unsigned long dt_ptr)
34140ef8cbcSPaul Mackerras {
3426a7e4064SGeoff Levand static __initdata struct paca_struct boot_paca;
3436a7e4064SGeoff Levand
34424d96495SBenjamin Herrenschmidt /* -------- printk is _NOT_ safe to use here ! ------- */
34524d96495SBenjamin Herrenschmidt
346d4a8e986SDaniel Axtens /*
347d4a8e986SDaniel Axtens * Assume we're on cpu 0 for now.
348d4a8e986SDaniel Axtens *
349d4a8e986SDaniel Axtens * We need to load a PACA very early for a few reasons.
350d4a8e986SDaniel Axtens *
351d4a8e986SDaniel Axtens * The stack protector canary is stored in the paca, so as soon as we
352d4a8e986SDaniel Axtens * call any stack protected code we need r13 pointing somewhere valid.
353d4a8e986SDaniel Axtens *
354d4a8e986SDaniel Axtens * If we are using kcov it will call in_task() in its instrumentation,
355d4a8e986SDaniel Axtens * which relies on the current task from the PACA.
356d4a8e986SDaniel Axtens *
357d4a8e986SDaniel Axtens * dt_cpu_ftrs_init() calls into generic OF/fdt code, as well as
358d4a8e986SDaniel Axtens * printk(), which can trigger both stack protector and kcov.
359d4a8e986SDaniel Axtens *
360d4a8e986SDaniel Axtens * percpu variables and spin locks also use the paca.
361d4a8e986SDaniel Axtens *
362d4a8e986SDaniel Axtens * So set up a temporary paca. It will be replaced below once we know
363d4a8e986SDaniel Axtens * what CPU we are on.
364d4a8e986SDaniel Axtens */
3651426d5a3SMichael Ellerman initialise_paca(&boot_paca, 0);
366519b2e31SNicholas Piggin fixup_boot_paca(&boot_paca);
367e1100ceeSNicholas Piggin WARN_ON(local_paca != 0);
368519b2e31SNicholas Piggin setup_paca(&boot_paca); /* install the paca into registers */
36933dbcf72SMichael Ellerman
37024d96495SBenjamin Herrenschmidt /* -------- printk is now safe to use ------- */
37124d96495SBenjamin Herrenschmidt
3722f5182cfSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && (mfmsr() & MSR_HV))
3732f5182cfSNicholas Piggin enable_machine_check();
3742f5182cfSNicholas Piggin
375d4a8e986SDaniel Axtens /* Try new device tree based feature discovery ... */
376d4a8e986SDaniel Axtens if (!dt_cpu_ftrs_init(__va(dt_ptr)))
377d4a8e986SDaniel Axtens /* Otherwise use the old style CPU table */
378d4a8e986SDaniel Axtens identify_cpu(0, mfspr(SPRN_PVR));
379d4a8e986SDaniel Axtens
380f2fd2513SBenjamin Herrenschmidt /* Enable early debugging if any specified (see udbg.h) */
381f2fd2513SBenjamin Herrenschmidt udbg_early_init();
382f2fd2513SBenjamin Herrenschmidt
3833b9176e9SQian Cai udbg_printf(" -> %s(), dt_ptr: 0x%lx\n", __func__, dt_ptr);
38440ef8cbcSPaul Mackerras
38540ef8cbcSPaul Mackerras /*
3863c607ce2SLinas Vepstas * Do early initialization using the flattened device
3873c607ce2SLinas Vepstas * tree, such as retrieving the physical memory map or
388dc222fa7SNicholas Piggin * calculating/retrieving the hash table size, discover
389dc222fa7SNicholas Piggin * boot_cpuid and boot_cpu_hwid.
39040ef8cbcSPaul Mackerras */
39140ef8cbcSPaul Mackerras early_init_devtree(__va(dt_ptr));
39240ef8cbcSPaul Mackerras
393dc222fa7SNicholas Piggin allocate_paca_ptrs();
394dc222fa7SNicholas Piggin allocate_paca(boot_cpuid);
395dc222fa7SNicholas Piggin set_hard_smp_processor_id(boot_cpuid, boot_cpu_hwid);
396519b2e31SNicholas Piggin fixup_boot_paca(paca_ptrs[boot_cpuid]);
397519b2e31SNicholas Piggin setup_paca(paca_ptrs[boot_cpuid]); /* install the paca into registers */
3989fa24404SNicholas Piggin // smp_processor_id() now reports boot_cpuid
3999fa24404SNicholas Piggin
4009fa24404SNicholas Piggin #ifdef CONFIG_SMP
4019fa24404SNicholas Piggin task_thread_info(current)->cpu = boot_cpuid; // fix task_cpu(current)
4029fa24404SNicholas Piggin #endif
4034df20460SAnton Blanchard
40463c254a5SBenjamin Herrenschmidt /*
405d3cbff1bSBenjamin Herrenschmidt * Configure exception handlers. This include setting up trampolines
406d3cbff1bSBenjamin Herrenschmidt * if needed, setting exception endian mode, etc...
40763c254a5SBenjamin Herrenschmidt */
408d3cbff1bSBenjamin Herrenschmidt configure_exceptions();
4090cc4746cSMichael Ellerman
41069795cabSChristophe Leroy /*
41169795cabSChristophe Leroy * Configure Kernel Userspace Protection. This needs to happen before
41269795cabSChristophe Leroy * feature fixups for platforms that implement this using features.
41369795cabSChristophe Leroy */
41469795cabSChristophe Leroy setup_kup();
41569795cabSChristophe Leroy
416c4bd6cb8SBenjamin Herrenschmidt /* Apply all the dynamic patching */
417c4bd6cb8SBenjamin Herrenschmidt apply_feature_fixups();
41897f6e0ccSBenjamin Herrenschmidt setup_feature_keys();
419c4bd6cb8SBenjamin Herrenschmidt
4209e8066f3SMichael Ellerman /* Initialize the hash table or TLB handling */
4219e8066f3SMichael Ellerman early_init_mmu();
4229e8066f3SMichael Ellerman
423e2f5efd0SAlexey Kardashevskiy early_ioremap_setup();
424e2f5efd0SAlexey Kardashevskiy
425a944a9c4SBenjamin Herrenschmidt /*
4261696d0fbSNicholas Piggin * After firmware and early platform setup code has set things up,
4271696d0fbSNicholas Piggin * we note the SPR values for configurable control/performance
4281696d0fbSNicholas Piggin * registers, and use those as initial defaults.
4291696d0fbSNicholas Piggin */
4301696d0fbSNicholas Piggin record_spr_defaults();
4311696d0fbSNicholas Piggin
4321696d0fbSNicholas Piggin /*
433a944a9c4SBenjamin Herrenschmidt * At this point, we can let interrupts switch to virtual mode
434a944a9c4SBenjamin Herrenschmidt * (the MMU has been setup), so adjust the MSR in the PACA to
4358f619b54SBenjamin Herrenschmidt * have IR and DR set and enable AIL if it exists
436a944a9c4SBenjamin Herrenschmidt */
4378f619b54SBenjamin Herrenschmidt cpu_ready_for_interrupts();
438a944a9c4SBenjamin Herrenschmidt
439d1039786SNaveen N. Rao /*
440d1039786SNaveen N. Rao * We enable ftrace here, but since we only support DYNAMIC_FTRACE, it
441d1039786SNaveen N. Rao * will only actually get enabled on the boot cpu much later once
442d1039786SNaveen N. Rao * ftrace itself has been initialized.
443d1039786SNaveen N. Rao */
444d1039786SNaveen N. Rao this_cpu_enable_ftrace();
445d1039786SNaveen N. Rao
4463b9176e9SQian Cai udbg_printf(" <- %s()\n", __func__);
4477191b615SBenjamin Herrenschmidt
4487191b615SBenjamin Herrenschmidt #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
4497191b615SBenjamin Herrenschmidt /*
4503b9176e9SQian Cai * This needs to be done *last* (after the above udbg_printf() even)
4517191b615SBenjamin Herrenschmidt *
4527191b615SBenjamin Herrenschmidt * Right after we return from this function, we turn on the MMU
4537191b615SBenjamin Herrenschmidt * which means the real-mode access trick that btext does will
4547191b615SBenjamin Herrenschmidt * no longer work, it needs to switch to using a real MMU
4557191b615SBenjamin Herrenschmidt * mapping. This call will ensure that it does
4567191b615SBenjamin Herrenschmidt */
4577191b615SBenjamin Herrenschmidt btext_map();
4587191b615SBenjamin Herrenschmidt #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
45940ef8cbcSPaul Mackerras }
46040ef8cbcSPaul Mackerras
461799d6046SPaul Mackerras #ifdef CONFIG_SMP
early_setup_secondary(void)462799d6046SPaul Mackerras void early_setup_secondary(void)
463799d6046SPaul Mackerras {
464103b7827SMadhavan Srinivasan /* Mark interrupts disabled in PACA */
4654e26bc4aSMadhavan Srinivasan irq_soft_mask_set(IRQS_DISABLED);
466799d6046SPaul Mackerras
467757c74d2SBenjamin Herrenschmidt /* Initialize the hash table or TLB handling */
468757c74d2SBenjamin Herrenschmidt early_init_mmu_secondary();
469a944a9c4SBenjamin Herrenschmidt
470b28c9750SRussell Currey /* Perform any KUP setup that is per-cpu */
471b28c9750SRussell Currey setup_kup();
472b28c9750SRussell Currey
473a944a9c4SBenjamin Herrenschmidt /*
474a944a9c4SBenjamin Herrenschmidt * At this point, we can let interrupts switch to virtual mode
475a944a9c4SBenjamin Herrenschmidt * (the MMU has been setup), so adjust the MSR in the PACA to
476a944a9c4SBenjamin Herrenschmidt * have IR and DR set.
477a944a9c4SBenjamin Herrenschmidt */
4788f619b54SBenjamin Herrenschmidt cpu_ready_for_interrupts();
479799d6046SPaul Mackerras }
480799d6046SPaul Mackerras
481799d6046SPaul Mackerras #endif /* CONFIG_SMP */
48240ef8cbcSPaul Mackerras
panic_smp_self_stop(void)4837412a60dSJosh Poimboeuf void __noreturn panic_smp_self_stop(void)
4848c1aef6aSNicholas Piggin {
4858c1aef6aSNicholas Piggin hard_irq_disable();
4868c1aef6aSNicholas Piggin spin_begin();
4878c1aef6aSNicholas Piggin while (1)
4888c1aef6aSNicholas Piggin spin_cpu_relax();
4898c1aef6aSNicholas Piggin }
4908c1aef6aSNicholas Piggin
491da665885SThiago Jung Bauermann #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
use_spinloop(void)492567cf94dSScott Wood static bool use_spinloop(void)
493567cf94dSScott Wood {
494339a3293SNicholas Piggin if (IS_ENABLED(CONFIG_PPC_BOOK3S)) {
495339a3293SNicholas Piggin /*
496339a3293SNicholas Piggin * See comments in head_64.S -- not all platforms insert
497339a3293SNicholas Piggin * secondaries at __secondary_hold and wait at the spin
498339a3293SNicholas Piggin * loop.
499339a3293SNicholas Piggin */
500339a3293SNicholas Piggin if (firmware_has_feature(FW_FEATURE_OPAL))
501339a3293SNicholas Piggin return false;
502567cf94dSScott Wood return true;
503339a3293SNicholas Piggin }
504567cf94dSScott Wood
505567cf94dSScott Wood /*
506567cf94dSScott Wood * When book3e boots from kexec, the ePAPR spin table does
507567cf94dSScott Wood * not get used.
508567cf94dSScott Wood */
509567cf94dSScott Wood return of_property_read_bool(of_chosen, "linux,booted-from-kexec");
510567cf94dSScott Wood }
511567cf94dSScott Wood
smp_release_cpus(void)512b8f51021SMichael Ellerman void smp_release_cpus(void)
513b8f51021SMichael Ellerman {
514758438a7SMichael Ellerman unsigned long *ptr;
5159d07bc84SBenjamin Herrenschmidt int i;
516b8f51021SMichael Ellerman
517567cf94dSScott Wood if (!use_spinloop())
518567cf94dSScott Wood return;
519567cf94dSScott Wood
520b8f51021SMichael Ellerman /* All secondary cpus are spinning on a common spinloop, release them
521b8f51021SMichael Ellerman * all now so they can start to spin on their individual paca
522b8f51021SMichael Ellerman * spinloops. For non SMP kernels, the secondary cpus never get out
523b8f51021SMichael Ellerman * of the common spinloop.
5241f6a93e4SPaul Mackerras */
525b8f51021SMichael Ellerman
526758438a7SMichael Ellerman ptr = (unsigned long *)((unsigned long)&__secondary_hold_spinloop
527758438a7SMichael Ellerman - PHYSICAL_START);
5282751b628SAnton Blanchard *ptr = ppc_function_entry(generic_secondary_smp_init);
5299d07bc84SBenjamin Herrenschmidt
5309d07bc84SBenjamin Herrenschmidt /* And wait a bit for them to catch up */
5319d07bc84SBenjamin Herrenschmidt for (i = 0; i < 100000; i++) {
532b8f51021SMichael Ellerman mb();
5339d07bc84SBenjamin Herrenschmidt HMT_low();
5347ac87abbSMatt Evans if (spinning_secondaries == 0)
5359d07bc84SBenjamin Herrenschmidt break;
5369d07bc84SBenjamin Herrenschmidt udelay(1);
5379d07bc84SBenjamin Herrenschmidt }
5383b9176e9SQian Cai pr_debug("spinning_secondaries = %d\n", spinning_secondaries);
539b8f51021SMichael Ellerman }
540da665885SThiago Jung Bauermann #endif /* CONFIG_SMP || CONFIG_KEXEC_CORE */
541b8f51021SMichael Ellerman
54240ef8cbcSPaul Mackerras /*
543799d6046SPaul Mackerras * Initialize some remaining members of the ppc64_caches and systemcfg
544799d6046SPaul Mackerras * structures
54540ef8cbcSPaul Mackerras * (at least until we get rid of them completely). This is mostly some
54640ef8cbcSPaul Mackerras * cache informations about the CPU that will be used by cache flush
54740ef8cbcSPaul Mackerras * routines and/or provided to userland
54840ef8cbcSPaul Mackerras */
549e2827fe5SBenjamin Herrenschmidt
init_cache_info(struct ppc_cache_info * info,u32 size,u32 lsize,u32 bsize,u32 sets)550d276960dSNick Child static void __init init_cache_info(struct ppc_cache_info *info, u32 size, u32 lsize,
551e2827fe5SBenjamin Herrenschmidt u32 bsize, u32 sets)
55240ef8cbcSPaul Mackerras {
553e2827fe5SBenjamin Herrenschmidt info->size = size;
554e2827fe5SBenjamin Herrenschmidt info->sets = sets;
555e2827fe5SBenjamin Herrenschmidt info->line_size = lsize;
556e2827fe5SBenjamin Herrenschmidt info->block_size = bsize;
557e2827fe5SBenjamin Herrenschmidt info->log_block_size = __ilog2(bsize);
5586ba422c7SAnton Blanchard if (bsize)
559e2827fe5SBenjamin Herrenschmidt info->blocks_per_page = PAGE_SIZE / bsize;
5606ba422c7SAnton Blanchard else
5616ba422c7SAnton Blanchard info->blocks_per_page = 0;
56298a5f361SBenjamin Herrenschmidt
56398a5f361SBenjamin Herrenschmidt if (sets == 0)
56498a5f361SBenjamin Herrenschmidt info->assoc = 0xffff;
56598a5f361SBenjamin Herrenschmidt else
56698a5f361SBenjamin Herrenschmidt info->assoc = size / (sets * lsize);
567e2827fe5SBenjamin Herrenschmidt }
56840ef8cbcSPaul Mackerras
parse_cache_info(struct device_node * np,bool icache,struct ppc_cache_info * info)569e2827fe5SBenjamin Herrenschmidt static bool __init parse_cache_info(struct device_node *np,
570e2827fe5SBenjamin Herrenschmidt bool icache,
571e2827fe5SBenjamin Herrenschmidt struct ppc_cache_info *info)
572e2827fe5SBenjamin Herrenschmidt {
573e2827fe5SBenjamin Herrenschmidt static const char *ipropnames[] __initdata = {
574e2827fe5SBenjamin Herrenschmidt "i-cache-size",
575e2827fe5SBenjamin Herrenschmidt "i-cache-sets",
576e2827fe5SBenjamin Herrenschmidt "i-cache-block-size",
577e2827fe5SBenjamin Herrenschmidt "i-cache-line-size",
578e2827fe5SBenjamin Herrenschmidt };
579e2827fe5SBenjamin Herrenschmidt static const char *dpropnames[] __initdata = {
580e2827fe5SBenjamin Herrenschmidt "d-cache-size",
581e2827fe5SBenjamin Herrenschmidt "d-cache-sets",
582e2827fe5SBenjamin Herrenschmidt "d-cache-block-size",
583e2827fe5SBenjamin Herrenschmidt "d-cache-line-size",
584e2827fe5SBenjamin Herrenschmidt };
585e2827fe5SBenjamin Herrenschmidt const char **propnames = icache ? ipropnames : dpropnames;
5865d451a87SBenjamin Herrenschmidt const __be32 *sizep, *lsizep, *bsizep, *setsp;
5875d451a87SBenjamin Herrenschmidt u32 size, lsize, bsize, sets;
588e2827fe5SBenjamin Herrenschmidt bool success = true;
58940ef8cbcSPaul Mackerras
59040ef8cbcSPaul Mackerras size = 0;
5915d451a87SBenjamin Herrenschmidt sets = -1u;
592bd067f83SBenjamin Herrenschmidt lsize = bsize = cur_cpu_spec->dcache_bsize;
593e2827fe5SBenjamin Herrenschmidt sizep = of_get_property(np, propnames[0], NULL);
59440ef8cbcSPaul Mackerras if (sizep != NULL)
5957946d5a5SAnton Blanchard size = be32_to_cpu(*sizep);
596e2827fe5SBenjamin Herrenschmidt setsp = of_get_property(np, propnames[1], NULL);
5975d451a87SBenjamin Herrenschmidt if (setsp != NULL)
5985d451a87SBenjamin Herrenschmidt sets = be32_to_cpu(*setsp);
599e2827fe5SBenjamin Herrenschmidt bsizep = of_get_property(np, propnames[2], NULL);
600e2827fe5SBenjamin Herrenschmidt lsizep = of_get_property(np, propnames[3], NULL);
601bd067f83SBenjamin Herrenschmidt if (bsizep == NULL)
602bd067f83SBenjamin Herrenschmidt bsizep = lsizep;
60394c0b013SChris Packham if (lsizep == NULL)
60494c0b013SChris Packham lsizep = bsizep;
60540ef8cbcSPaul Mackerras if (lsizep != NULL)
6067946d5a5SAnton Blanchard lsize = be32_to_cpu(*lsizep);
607bd067f83SBenjamin Herrenschmidt if (bsizep != NULL)
608bd067f83SBenjamin Herrenschmidt bsize = be32_to_cpu(*bsizep);
609bd067f83SBenjamin Herrenschmidt if (sizep == NULL || bsizep == NULL || lsizep == NULL)
610e2827fe5SBenjamin Herrenschmidt success = false;
61140ef8cbcSPaul Mackerras
6125d451a87SBenjamin Herrenschmidt /*
6135d451a87SBenjamin Herrenschmidt * OF is weird .. it represents fully associative caches
6145d451a87SBenjamin Herrenschmidt * as "1 way" which doesn't make much sense and doesn't
6155d451a87SBenjamin Herrenschmidt * leave room for direct mapped. We'll assume that 0
6165d451a87SBenjamin Herrenschmidt * in OF means direct mapped for that reason.
6175d451a87SBenjamin Herrenschmidt */
6185d451a87SBenjamin Herrenschmidt if (sets == 1)
6195d451a87SBenjamin Herrenschmidt sets = 0;
6205d451a87SBenjamin Herrenschmidt else if (sets == 0)
6215d451a87SBenjamin Herrenschmidt sets = 1;
62240ef8cbcSPaul Mackerras
623e2827fe5SBenjamin Herrenschmidt init_cache_info(info, size, lsize, bsize, sets);
62440ef8cbcSPaul Mackerras
625e2827fe5SBenjamin Herrenschmidt return success;
62640ef8cbcSPaul Mackerras }
627e2827fe5SBenjamin Herrenschmidt
initialize_cache_info(void)628e2827fe5SBenjamin Herrenschmidt void __init initialize_cache_info(void)
629e2827fe5SBenjamin Herrenschmidt {
630608b4214SBenjamin Herrenschmidt struct device_node *cpu = NULL, *l2, *l3 = NULL;
631608b4214SBenjamin Herrenschmidt u32 pvr;
632e2827fe5SBenjamin Herrenschmidt
633608b4214SBenjamin Herrenschmidt /*
634608b4214SBenjamin Herrenschmidt * All shipping POWER8 machines have a firmware bug that
635608b4214SBenjamin Herrenschmidt * puts incorrect information in the device-tree. This will
636608b4214SBenjamin Herrenschmidt * be (hopefully) fixed for future chips but for now hard
637608b4214SBenjamin Herrenschmidt * code the values if we are running on one of these
638608b4214SBenjamin Herrenschmidt */
639608b4214SBenjamin Herrenschmidt pvr = PVR_VER(mfspr(SPRN_PVR));
640608b4214SBenjamin Herrenschmidt if (pvr == PVR_POWER8 || pvr == PVR_POWER8E ||
641608b4214SBenjamin Herrenschmidt pvr == PVR_POWER8NVL) {
642608b4214SBenjamin Herrenschmidt /* size lsize blk sets */
643608b4214SBenjamin Herrenschmidt init_cache_info(&ppc64_caches.l1i, 0x8000, 128, 128, 32);
644608b4214SBenjamin Herrenschmidt init_cache_info(&ppc64_caches.l1d, 0x10000, 128, 128, 64);
645608b4214SBenjamin Herrenschmidt init_cache_info(&ppc64_caches.l2, 0x80000, 128, 0, 512);
646608b4214SBenjamin Herrenschmidt init_cache_info(&ppc64_caches.l3, 0x800000, 128, 0, 8192);
647608b4214SBenjamin Herrenschmidt } else
64865e01f38SBenjamin Herrenschmidt cpu = of_find_node_by_type(NULL, "cpu");
649e2827fe5SBenjamin Herrenschmidt
650e2827fe5SBenjamin Herrenschmidt /*
651e2827fe5SBenjamin Herrenschmidt * We're assuming *all* of the CPUs have the same
652e2827fe5SBenjamin Herrenschmidt * d-cache and i-cache sizes... -Peter
653e2827fe5SBenjamin Herrenschmidt */
65465e01f38SBenjamin Herrenschmidt if (cpu) {
65565e01f38SBenjamin Herrenschmidt if (!parse_cache_info(cpu, false, &ppc64_caches.l1d))
6563b9176e9SQian Cai pr_warn("Argh, can't find dcache properties !\n");
657e2827fe5SBenjamin Herrenschmidt
65865e01f38SBenjamin Herrenschmidt if (!parse_cache_info(cpu, true, &ppc64_caches.l1i))
6593b9176e9SQian Cai pr_warn("Argh, can't find icache properties !\n");
66065e01f38SBenjamin Herrenschmidt
66165e01f38SBenjamin Herrenschmidt /*
66265e01f38SBenjamin Herrenschmidt * Try to find the L2 and L3 if any. Assume they are
66365e01f38SBenjamin Herrenschmidt * unified and use the D-side properties.
66465e01f38SBenjamin Herrenschmidt */
66565e01f38SBenjamin Herrenschmidt l2 = of_find_next_cache_node(cpu);
66665e01f38SBenjamin Herrenschmidt of_node_put(cpu);
66765e01f38SBenjamin Herrenschmidt if (l2) {
66865e01f38SBenjamin Herrenschmidt parse_cache_info(l2, false, &ppc64_caches.l2);
66965e01f38SBenjamin Herrenschmidt l3 = of_find_next_cache_node(l2);
67065e01f38SBenjamin Herrenschmidt of_node_put(l2);
67165e01f38SBenjamin Herrenschmidt }
67265e01f38SBenjamin Herrenschmidt if (l3) {
67365e01f38SBenjamin Herrenschmidt parse_cache_info(l3, false, &ppc64_caches.l3);
67465e01f38SBenjamin Herrenschmidt of_node_put(l3);
67565e01f38SBenjamin Herrenschmidt }
67640ef8cbcSPaul Mackerras }
67740ef8cbcSPaul Mackerras
6789df549afSBenjamin Herrenschmidt /* For use by binfmt_elf */
679e2827fe5SBenjamin Herrenschmidt dcache_bsize = ppc64_caches.l1d.block_size;
680e2827fe5SBenjamin Herrenschmidt icache_bsize = ppc64_caches.l1i.block_size;
6819df549afSBenjamin Herrenschmidt
6825a61ef74SNicholas Piggin cur_cpu_spec->dcache_bsize = dcache_bsize;
6835a61ef74SNicholas Piggin cur_cpu_spec->icache_bsize = icache_bsize;
68440ef8cbcSPaul Mackerras }
68540ef8cbcSPaul Mackerras
6861af19331SNicholas Piggin /*
6871af19331SNicholas Piggin * This returns the limit below which memory accesses to the linear
6881af19331SNicholas Piggin * mapping are guarnateed not to cause an architectural exception (e.g.,
6891af19331SNicholas Piggin * TLB or SLB miss fault).
6901af19331SNicholas Piggin *
6911af19331SNicholas Piggin * This is used to allocate PACAs and various interrupt stacks that
6921af19331SNicholas Piggin * that are accessed early in interrupt handlers that must not cause
6931af19331SNicholas Piggin * re-entrant interrupts.
69440bd587aSBenjamin Herrenschmidt */
ppc64_bolted_size(void)6951af19331SNicholas Piggin __init u64 ppc64_bolted_size(void)
696095c7965SAnton Blanchard {
697e0d68273SChristophe Leroy #ifdef CONFIG_PPC_BOOK3E_64
69840bd587aSBenjamin Herrenschmidt /* Freescale BookE bolts the entire linear mapping */
6991af19331SNicholas Piggin /* XXX: BookE ppc64_rma_limit setup seems to disagree? */
7001af19331SNicholas Piggin if (early_mmu_has_feature(MMU_FTR_TYPE_FSL_E))
70140bd587aSBenjamin Herrenschmidt return linear_map_top;
70240bd587aSBenjamin Herrenschmidt /* Other BookE, we assume the first GB is bolted */
70340bd587aSBenjamin Herrenschmidt return 1ul << 30;
70440bd587aSBenjamin Herrenschmidt #else
7051af19331SNicholas Piggin /* BookS radix, does not take faults on linear mapping */
706d5507190SNicholas Piggin if (early_radix_enabled())
707d5507190SNicholas Piggin return ULONG_MAX;
708d5507190SNicholas Piggin
7091af19331SNicholas Piggin /* BookS hash, the first segment is bolted */
7101af19331SNicholas Piggin if (early_mmu_has_feature(MMU_FTR_1T_SEGMENT))
711095c7965SAnton Blanchard return 1UL << SID_SHIFT_1T;
712095c7965SAnton Blanchard return 1UL << SID_SHIFT;
71340bd587aSBenjamin Herrenschmidt #endif
714095c7965SAnton Blanchard }
715095c7965SAnton Blanchard
alloc_stack(unsigned long limit,int cpu)716f3865f9aSNicholas Piggin static void *__init alloc_stack(unsigned long limit, int cpu)
717f3865f9aSNicholas Piggin {
718c8e409a3SChristophe Leroy void *ptr;
719f3865f9aSNicholas Piggin
72066f93c5aSNicholas Piggin BUILD_BUG_ON(STACK_INT_FRAME_SIZE % 16);
72166f93c5aSNicholas Piggin
72263289e7dSChristophe Leroy ptr = memblock_alloc_try_nid(THREAD_SIZE, THREAD_ALIGN,
723c8e409a3SChristophe Leroy MEMBLOCK_LOW_LIMIT, limit,
724c8e409a3SChristophe Leroy early_cpu_to_node(cpu));
725c8e409a3SChristophe Leroy if (!ptr)
726f3865f9aSNicholas Piggin panic("cannot allocate stacks");
727f3865f9aSNicholas Piggin
728c8e409a3SChristophe Leroy return ptr;
729f3865f9aSNicholas Piggin }
730f3865f9aSNicholas Piggin
irqstack_early_init(void)731b1923caaSBenjamin Herrenschmidt void __init irqstack_early_init(void)
73240ef8cbcSPaul Mackerras {
7331af19331SNicholas Piggin u64 limit = ppc64_bolted_size();
73440ef8cbcSPaul Mackerras unsigned int i;
73540ef8cbcSPaul Mackerras
73640ef8cbcSPaul Mackerras /*
7378f4da26eSAnton Blanchard * Interrupt stacks must be in the first segment since we
738d5507190SNicholas Piggin * cannot afford to take SLB misses on them. They are not
739d5507190SNicholas Piggin * accessed in realmode.
74040ef8cbcSPaul Mackerras */
7410e551954SKAMEZAWA Hiroyuki for_each_possible_cpu(i) {
742f3865f9aSNicholas Piggin softirq_ctx[i] = alloc_stack(limit, i);
743f3865f9aSNicholas Piggin hardirq_ctx[i] = alloc_stack(limit, i);
74440ef8cbcSPaul Mackerras }
74540ef8cbcSPaul Mackerras }
74640ef8cbcSPaul Mackerras
747e0d68273SChristophe Leroy #ifdef CONFIG_PPC_BOOK3E_64
exc_lvl_early_init(void)748b1923caaSBenjamin Herrenschmidt void __init exc_lvl_early_init(void)
7492d27cfd3SBenjamin Herrenschmidt {
7502d27cfd3SBenjamin Herrenschmidt unsigned int i;
7512d27cfd3SBenjamin Herrenschmidt
7522d27cfd3SBenjamin Herrenschmidt for_each_possible_cpu(i) {
753f3865f9aSNicholas Piggin void *sp;
754160c7324STiejun Chen
755f3865f9aSNicholas Piggin sp = alloc_stack(ULONG_MAX, i);
756f3865f9aSNicholas Piggin critirq_ctx[i] = sp;
757f3865f9aSNicholas Piggin paca_ptrs[i]->crit_kstack = sp + THREAD_SIZE;
758160c7324STiejun Chen
759f3865f9aSNicholas Piggin sp = alloc_stack(ULONG_MAX, i);
760f3865f9aSNicholas Piggin dbgirq_ctx[i] = sp;
761f3865f9aSNicholas Piggin paca_ptrs[i]->dbg_kstack = sp + THREAD_SIZE;
762f3865f9aSNicholas Piggin
763f3865f9aSNicholas Piggin sp = alloc_stack(ULONG_MAX, i);
764f3865f9aSNicholas Piggin mcheckirq_ctx[i] = sp;
765f3865f9aSNicholas Piggin paca_ptrs[i]->mc_kstack = sp + THREAD_SIZE;
7662d27cfd3SBenjamin Herrenschmidt }
767d36b4c4fSKumar Gala
768d36b4c4fSKumar Gala if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
769565c2f24SKevin Hao patch_exception(0x040, exc_debug_debug_book3e);
7702d27cfd3SBenjamin Herrenschmidt }
7712d27cfd3SBenjamin Herrenschmidt #endif
7722d27cfd3SBenjamin Herrenschmidt
77340ef8cbcSPaul Mackerras /*
77440ef8cbcSPaul Mackerras * Stack space used when we detect a bad kernel stack pointer, and
775729b0f71SMahesh Salgaonkar * early in SMP boots before relocation is enabled. Exclusive emergency
776729b0f71SMahesh Salgaonkar * stack for machine checks.
77740ef8cbcSPaul Mackerras */
emergency_stack_init(void)778b1923caaSBenjamin Herrenschmidt void __init emergency_stack_init(void)
77940ef8cbcSPaul Mackerras {
780d2cbbd45SNicholas Piggin u64 limit, mce_limit;
78140ef8cbcSPaul Mackerras unsigned int i;
78240ef8cbcSPaul Mackerras
78340ef8cbcSPaul Mackerras /*
78440ef8cbcSPaul Mackerras * Emergency stacks must be under 256MB, we cannot afford to take
78540ef8cbcSPaul Mackerras * SLB misses on them. The ABI also requires them to be 128-byte
78640ef8cbcSPaul Mackerras * aligned.
78740ef8cbcSPaul Mackerras *
78840ef8cbcSPaul Mackerras * Since we use these as temporary stacks during secondary CPU
789d5507190SNicholas Piggin * bringup, machine check, system reset, and HMI, we need to get
790d5507190SNicholas Piggin * at them in real mode. This means they must also be within the RMO
791d5507190SNicholas Piggin * region.
79234f19ff1SNicholas Piggin *
79334f19ff1SNicholas Piggin * The IRQ stacks allocated elsewhere in this file are zeroed and
79434f19ff1SNicholas Piggin * initialized in kernel/irq.c. These are initialized here in order
79534f19ff1SNicholas Piggin * to have emergency stacks available as early as possible.
79640ef8cbcSPaul Mackerras */
797d2cbbd45SNicholas Piggin limit = mce_limit = min(ppc64_bolted_size(), ppc64_rma_size);
798d2cbbd45SNicholas Piggin
799d2cbbd45SNicholas Piggin /*
800d2cbbd45SNicholas Piggin * Machine check on pseries calls rtas, but can't use the static
801d2cbbd45SNicholas Piggin * rtas_args due to a machine check hitting while the lock is held.
802d2cbbd45SNicholas Piggin * rtas args have to be under 4GB, so the machine check stack is
803d2cbbd45SNicholas Piggin * limited to 4GB so args can be put on stack.
804d2cbbd45SNicholas Piggin */
805d2cbbd45SNicholas Piggin if (firmware_has_feature(FW_FEATURE_LPAR) && mce_limit > SZ_4G)
806d2cbbd45SNicholas Piggin mce_limit = SZ_4G;
80740ef8cbcSPaul Mackerras
8083243d874SMichael Ellerman for_each_possible_cpu(i) {
809d608898aSChristophe Leroy paca_ptrs[i]->emergency_sp = alloc_stack(limit, i) + THREAD_SIZE;
810729b0f71SMahesh Salgaonkar
811729b0f71SMahesh Salgaonkar #ifdef CONFIG_PPC_BOOK3S_64
812b1ee8a3dSNicholas Piggin /* emergency stack for NMI exception handling. */
813d608898aSChristophe Leroy paca_ptrs[i]->nmi_emergency_sp = alloc_stack(limit, i) + THREAD_SIZE;
814b1ee8a3dSNicholas Piggin
815729b0f71SMahesh Salgaonkar /* emergency stack for machine check exception handling. */
816d2cbbd45SNicholas Piggin paca_ptrs[i]->mc_emergency_sp = alloc_stack(mce_limit, i) + THREAD_SIZE;
817729b0f71SMahesh Salgaonkar #endif
8183243d874SMichael Ellerman }
81940ef8cbcSPaul Mackerras }
82040ef8cbcSPaul Mackerras
8217a0268faSAnton Blanchard #ifdef CONFIG_SMP
pcpu_cpu_distance(unsigned int from,unsigned int to)822c2a7e818STejun Heo static int pcpu_cpu_distance(unsigned int from, unsigned int to)
823c2a7e818STejun Heo {
824ba4a648fSMichael Ellerman if (early_cpu_to_node(from) == early_cpu_to_node(to))
825c2a7e818STejun Heo return LOCAL_DISTANCE;
826c2a7e818STejun Heo else
827c2a7e818STejun Heo return REMOTE_DISTANCE;
828c2a7e818STejun Heo }
829c2a7e818STejun Heo
pcpu_cpu_to_node(int cpu)8301ca3fb3aSKefeng Wang static __init int pcpu_cpu_to_node(int cpu)
8311ca3fb3aSKefeng Wang {
8321ca3fb3aSKefeng Wang return early_cpu_to_node(cpu);
8331ca3fb3aSKefeng Wang }
8341ca3fb3aSKefeng Wang
835ae01f84bSAnton Blanchard unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
836ae01f84bSAnton Blanchard EXPORT_SYMBOL(__per_cpu_offset);
8370f37946cSMahesh Salgaonkar DEFINE_STATIC_KEY_FALSE(__percpu_first_chunk_is_paged);
838ae01f84bSAnton Blanchard
setup_per_cpu_areas(void)8397a0268faSAnton Blanchard void __init setup_per_cpu_areas(void)
8407a0268faSAnton Blanchard {
841c2a7e818STejun Heo const size_t dyn_size = PERCPU_MODULE_RESERVE + PERCPU_DYNAMIC_RESERVE;
842c2a7e818STejun Heo size_t atom_size;
843c2a7e818STejun Heo unsigned long delta;
844c2a7e818STejun Heo unsigned int cpu;
845eb553f16SAneesh Kumar K.V int rc = -EINVAL;
8467a0268faSAnton Blanchard
847c2a7e818STejun Heo /*
848ffbe5d21SNicholas Piggin * BookE and BookS radix are historical values and should be revisited.
849ffbe5d21SNicholas Piggin */
850e0d68273SChristophe Leroy if (IS_ENABLED(CONFIG_PPC_BOOK3E_64)) {
851ffbe5d21SNicholas Piggin atom_size = SZ_1M;
852ffbe5d21SNicholas Piggin } else if (radix_enabled()) {
853ffbe5d21SNicholas Piggin atom_size = PAGE_SIZE;
854387e220aSNicholas Piggin } else if (IS_ENABLED(CONFIG_PPC_64S_HASH_MMU)) {
855ffbe5d21SNicholas Piggin /*
856c2a7e818STejun Heo * Linear mapping is one of 4K, 1M and 16M. For 4K, no need
857c2a7e818STejun Heo * to group units. For larger mappings, use 1M atom which
858c2a7e818STejun Heo * should be large enough to contain a number of units.
859c2a7e818STejun Heo */
860c2a7e818STejun Heo if (mmu_linear_psize == MMU_PAGE_4K)
861c2a7e818STejun Heo atom_size = PAGE_SIZE;
862c2a7e818STejun Heo else
863ffbe5d21SNicholas Piggin atom_size = SZ_1M;
864ffbe5d21SNicholas Piggin }
8657a0268faSAnton Blanchard
866eb553f16SAneesh Kumar K.V if (pcpu_chosen_fc != PCPU_FC_PAGE) {
867c2a7e818STejun Heo rc = pcpu_embed_first_chunk(0, dyn_size, atom_size, pcpu_cpu_distance,
86823f91716SKefeng Wang pcpu_cpu_to_node);
869eb553f16SAneesh Kumar K.V if (rc)
870eb553f16SAneesh Kumar K.V pr_warn("PERCPU: %s allocator failed (%d), "
871eb553f16SAneesh Kumar K.V "falling back to page size\n",
872eb553f16SAneesh Kumar K.V pcpu_fc_names[pcpu_chosen_fc], rc);
873eb553f16SAneesh Kumar K.V }
874eb553f16SAneesh Kumar K.V
875eb553f16SAneesh Kumar K.V if (rc < 0)
87620c03576SKefeng Wang rc = pcpu_page_first_chunk(0, pcpu_cpu_to_node);
877c2a7e818STejun Heo if (rc < 0)
878c2a7e818STejun Heo panic("cannot initialize percpu area (err=%d)", rc);
8797a0268faSAnton Blanchard
8800f37946cSMahesh Salgaonkar static_key_enable(&__percpu_first_chunk_is_paged.key);
881c2a7e818STejun Heo delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
882ae01f84bSAnton Blanchard for_each_possible_cpu(cpu) {
883ae01f84bSAnton Blanchard __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
884d2e60075SNicholas Piggin paca_ptrs[cpu]->data_offset = __per_cpu_offset[cpu];
885ae01f84bSAnton Blanchard }
8867a0268faSAnton Blanchard }
8877a0268faSAnton Blanchard #endif
8884cb3cee0SBenjamin Herrenschmidt
88950f9481eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
memory_block_size_bytes(void)890a5d86257SAnton Blanchard unsigned long memory_block_size_bytes(void)
891a5d86257SAnton Blanchard {
892a5d86257SAnton Blanchard if (ppc_md.memory_block_size)
893a5d86257SAnton Blanchard return ppc_md.memory_block_size();
894a5d86257SAnton Blanchard
895a5d86257SAnton Blanchard return MIN_MEMORY_BLOCK_SIZE;
896a5d86257SAnton Blanchard }
897a5d86257SAnton Blanchard #endif
8984cb3cee0SBenjamin Herrenschmidt
899ecd73cc5SBenjamin Herrenschmidt #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
9004cb3cee0SBenjamin Herrenschmidt struct ppc_pci_io ppc_pci_io;
9014cb3cee0SBenjamin Herrenschmidt EXPORT_SYMBOL(ppc_pci_io);
902ecd73cc5SBenjamin Herrenschmidt #endif
90370412c55SNicholas Piggin
90470412c55SNicholas Piggin #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
hw_nmi_get_sample_period(int watchdog_thresh)90570412c55SNicholas Piggin u64 hw_nmi_get_sample_period(int watchdog_thresh)
90670412c55SNicholas Piggin {
90770412c55SNicholas Piggin return ppc_proc_freq * watchdog_thresh;
90870412c55SNicholas Piggin }
90970412c55SNicholas Piggin #endif
91070412c55SNicholas Piggin
91170412c55SNicholas Piggin /*
91270412c55SNicholas Piggin * The perf based hardlockup detector breaks PMU event based branches, so
91370412c55SNicholas Piggin * disable it by default. Book3S has a soft-nmi hardlockup detector based
91470412c55SNicholas Piggin * on the decrementer interrupt, so it does not suffer from this problem.
91570412c55SNicholas Piggin *
916633c8e98SNicholas Piggin * It is likely to get false positives in KVM guests, so disable it there
917633c8e98SNicholas Piggin * by default too. PowerVM will not stop or arbitrarily oversubscribe
918633c8e98SNicholas Piggin * CPUs, but give a minimum regular allotment even with SPLPAR, so enable
919633c8e98SNicholas Piggin * the detector for non-KVM guests, assume PowerVM.
92070412c55SNicholas Piggin */
disable_hardlockup_detector(void)92170412c55SNicholas Piggin static int __init disable_hardlockup_detector(void)
92270412c55SNicholas Piggin {
92370412c55SNicholas Piggin #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
92470412c55SNicholas Piggin hardlockup_detector_disable();
92570412c55SNicholas Piggin #else
926633c8e98SNicholas Piggin if (firmware_has_feature(FW_FEATURE_LPAR)) {
927*10c95568SGautam Menghani check_kvm_guest();
928633c8e98SNicholas Piggin if (is_kvm_guest())
92970412c55SNicholas Piggin hardlockup_detector_disable();
930633c8e98SNicholas Piggin }
93170412c55SNicholas Piggin #endif
93270412c55SNicholas Piggin
93370412c55SNicholas Piggin return 0;
93470412c55SNicholas Piggin }
93570412c55SNicholas Piggin early_initcall(disable_hardlockup_detector);
936