12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
269a80d3fSPaul Mackerras /*
369a80d3fSPaul Mackerras  * SMP support for pSeries machines.
469a80d3fSPaul Mackerras  *
569a80d3fSPaul Mackerras  * Dave Engebretsen, Peter Bergner, and
669a80d3fSPaul Mackerras  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
769a80d3fSPaul Mackerras  *
869a80d3fSPaul Mackerras  * Plus various changes from other IBM teams...
969a80d3fSPaul Mackerras  */
1069a80d3fSPaul Mackerras 
1169a80d3fSPaul Mackerras 
1269a80d3fSPaul Mackerras #include <linux/kernel.h>
1369a80d3fSPaul Mackerras #include <linux/sched.h>
1469a80d3fSPaul Mackerras #include <linux/smp.h>
1569a80d3fSPaul Mackerras #include <linux/interrupt.h>
1669a80d3fSPaul Mackerras #include <linux/delay.h>
1769a80d3fSPaul Mackerras #include <linux/init.h>
1869a80d3fSPaul Mackerras #include <linux/spinlock.h>
1969a80d3fSPaul Mackerras #include <linux/cache.h>
2069a80d3fSPaul Mackerras #include <linux/err.h>
21edbaa603SKay Sievers #include <linux/device.h>
2269a80d3fSPaul Mackerras #include <linux/cpu.h>
2365fddcfcSMike Rapoport #include <linux/pgtable.h>
2469a80d3fSPaul Mackerras 
2569a80d3fSPaul Mackerras #include <asm/ptrace.h>
2660063497SArun Sharma #include <linux/atomic.h>
2769a80d3fSPaul Mackerras #include <asm/irq.h>
2869a80d3fSPaul Mackerras #include <asm/page.h>
2969a80d3fSPaul Mackerras #include <asm/io.h>
3069a80d3fSPaul Mackerras #include <asm/prom.h>
3169a80d3fSPaul Mackerras #include <asm/smp.h>
3269a80d3fSPaul Mackerras #include <asm/paca.h>
3369a80d3fSPaul Mackerras #include <asm/machdep.h>
3469a80d3fSPaul Mackerras #include <asm/cputable.h>
3569a80d3fSPaul Mackerras #include <asm/firmware.h>
3669a80d3fSPaul Mackerras #include <asm/rtas.h>
37271c3f35SPaul Mackerras #include <asm/vdso_datapage.h>
388d089085SBenjamin Herrenschmidt #include <asm/cputhreads.h>
390b05ac6eSBenjamin Herrenschmidt #include <asm/xics.h>
40eac1e731SCédric Le Goater #include <asm/xive.h>
41e5e84f0aSIan Munsie #include <asm/dbell.h>
42212bebb4SDeepthi Dharwar #include <asm/plpar_wrappers.h>
432751b628SAnton Blanchard #include <asm/code-patching.h>
444edaac51SSukadev Bhattiprolu #include <asm/svm.h>
4569a80d3fSPaul Mackerras 
468feaeca2SMichael Ellerman #include "pseries.h"
473aa565f5SGautham R Shenoy #include "offline_states.h"
48a1218720SMichael Ellerman 
4969a80d3fSPaul Mackerras 
5069a80d3fSPaul Mackerras /*
516a75a6b8SMilton Miller  * The Primary thread of each non-boot processor was started from the OF client
526a75a6b8SMilton Miller  * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
5369a80d3fSPaul Mackerras  */
54af831e1eSAnton Blanchard static cpumask_var_t of_spin_mask;
5569a80d3fSPaul Mackerras 
56f8b67691SMichael Neuling /* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
57f8b67691SMichael Neuling int smp_query_cpu_stopped(unsigned int pcpu)
58f8b67691SMichael Neuling {
59f8b67691SMichael Neuling 	int cpu_status, status;
60f8b67691SMichael Neuling 	int qcss_tok = rtas_token("query-cpu-stopped-state");
61f8b67691SMichael Neuling 
62f8b67691SMichael Neuling 	if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
632d86938aSMilton Miller 		printk_once(KERN_INFO
642d86938aSMilton Miller 			"Firmware doesn't support query-cpu-stopped-state\n");
65f8b67691SMichael Neuling 		return QCSS_HARDWARE_ERROR;
66f8b67691SMichael Neuling 	}
67f8b67691SMichael Neuling 
68f8b67691SMichael Neuling 	status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
69f8b67691SMichael Neuling 	if (status != 0) {
70f8b67691SMichael Neuling 		printk(KERN_ERR
71f8b67691SMichael Neuling 		       "RTAS query-cpu-stopped-state failed: %i\n", status);
72f8b67691SMichael Neuling 		return status;
73f8b67691SMichael Neuling 	}
74f8b67691SMichael Neuling 
75f8b67691SMichael Neuling 	return cpu_status;
76f8b67691SMichael Neuling }
77f8b67691SMichael Neuling 
7869a80d3fSPaul Mackerras /**
7969a80d3fSPaul Mackerras  * smp_startup_cpu() - start the given cpu
8069a80d3fSPaul Mackerras  *
8169a80d3fSPaul Mackerras  * At boot time, there is nothing to do for primary threads which were
8269a80d3fSPaul Mackerras  * started from Open Firmware.  For anything else, call RTAS with the
8369a80d3fSPaul Mackerras  * appropriate start location.
8469a80d3fSPaul Mackerras  *
8569a80d3fSPaul Mackerras  * Returns:
8669a80d3fSPaul Mackerras  *	0	- failure
8769a80d3fSPaul Mackerras  *	1	- success
8869a80d3fSPaul Mackerras  */
89cad5cef6SGreg Kroah-Hartman static inline int smp_startup_cpu(unsigned int lcpu)
9069a80d3fSPaul Mackerras {
9169a80d3fSPaul Mackerras 	int status;
922751b628SAnton Blanchard 	unsigned long start_here =
932751b628SAnton Blanchard 			__pa(ppc_function_entry(generic_secondary_smp_init));
9469a80d3fSPaul Mackerras 	unsigned int pcpu;
9569a80d3fSPaul Mackerras 	int start_cpu;
9669a80d3fSPaul Mackerras 
97af831e1eSAnton Blanchard 	if (cpumask_test_cpu(lcpu, of_spin_mask))
9869a80d3fSPaul Mackerras 		/* Already started by OF and sitting in spin loop */
9969a80d3fSPaul Mackerras 		return 1;
10069a80d3fSPaul Mackerras 
10169a80d3fSPaul Mackerras 	pcpu = get_hard_smp_processor_id(lcpu);
10269a80d3fSPaul Mackerras 
103aef40e87SMichael Neuling 	/* Check to see if the CPU out of FW already for kexec */
104aef40e87SMichael Neuling 	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
105af831e1eSAnton Blanchard 		cpumask_set_cpu(lcpu, of_spin_mask);
106aef40e87SMichael Neuling 		return 1;
107aef40e87SMichael Neuling 	}
108aef40e87SMichael Neuling 
10969a80d3fSPaul Mackerras 	/* Fixup atomic count: it exited inside IRQ handler. */
110d2e60075SNicholas Piggin 	task_thread_info(paca_ptrs[lcpu]->__current)->preempt_count	= 0;
111c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1123aa565f5SGautham R Shenoy 	if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
1133aa565f5SGautham R Shenoy 		goto out;
114c60e65d7SMatt Evans #endif
11569a80d3fSPaul Mackerras 	/*
11669a80d3fSPaul Mackerras 	 * If the RTAS start-cpu token does not exist then presume the
11769a80d3fSPaul Mackerras 	 * cpu is already spinning.
11869a80d3fSPaul Mackerras 	 */
11969a80d3fSPaul Mackerras 	start_cpu = rtas_token("start-cpu");
12069a80d3fSPaul Mackerras 	if (start_cpu == RTAS_UNKNOWN_SERVICE)
12169a80d3fSPaul Mackerras 		return 1;
12269a80d3fSPaul Mackerras 
123496b7a51SMichael Ellerman 	status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
12469a80d3fSPaul Mackerras 	if (status != 0) {
12569a80d3fSPaul Mackerras 		printk(KERN_ERR "start-cpu failed: %i\n", status);
12669a80d3fSPaul Mackerras 		return 0;
12769a80d3fSPaul Mackerras 	}
12869a80d3fSPaul Mackerras 
129c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1303aa565f5SGautham R Shenoy out:
131c60e65d7SMatt Evans #endif
13269a80d3fSPaul Mackerras 	return 1;
13369a80d3fSPaul Mackerras }
13469a80d3fSPaul Mackerras 
13586425bedSRashmica Gupta static void smp_setup_cpu(int cpu)
13669a80d3fSPaul Mackerras {
137eac1e731SCédric Le Goater 	if (xive_enabled())
138eac1e731SCédric Le Goater 		xive_smp_setup_cpu();
139eac1e731SCédric Le Goater 	else if (cpu != boot_cpuid)
14069a80d3fSPaul Mackerras 		xics_setup_cpu();
14169a80d3fSPaul Mackerras 
14269a80d3fSPaul Mackerras 	if (firmware_has_feature(FW_FEATURE_SPLPAR))
14369a80d3fSPaul Mackerras 		vpa_init(cpu);
14469a80d3fSPaul Mackerras 
145af831e1eSAnton Blanchard 	cpumask_clear_cpu(cpu, of_spin_mask);
146c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1473aa565f5SGautham R Shenoy 	set_cpu_current_state(cpu, CPU_STATE_ONLINE);
1483aa565f5SGautham R Shenoy 	set_default_offline_state(cpu);
149c60e65d7SMatt Evans #endif
15069a80d3fSPaul Mackerras }
15169a80d3fSPaul Mackerras 
152cad5cef6SGreg Kroah-Hartman static int smp_pSeries_kick_cpu(int nr)
15369a80d3fSPaul Mackerras {
154c642af9cSSantosh Sivaraj 	if (nr < 0 || nr >= nr_cpu_ids)
155f8d0d5dcSSantosh Sivaraj 		return -EINVAL;
15669a80d3fSPaul Mackerras 
15769a80d3fSPaul Mackerras 	if (!smp_startup_cpu(nr))
158de300974SMichael Ellerman 		return -ENOENT;
15969a80d3fSPaul Mackerras 
16069a80d3fSPaul Mackerras 	/*
16169a80d3fSPaul Mackerras 	 * The processor is currently spinning, waiting for the
16269a80d3fSPaul Mackerras 	 * cpu_start field to become non-zero After we set cpu_start,
16369a80d3fSPaul Mackerras 	 * the processor will continue on to secondary_start
16469a80d3fSPaul Mackerras 	 */
165d2e60075SNicholas Piggin 	paca_ptrs[nr]->cpu_start = 1;
166c60e65d7SMatt Evans #ifdef CONFIG_HOTPLUG_CPU
1673aa565f5SGautham R Shenoy 	set_preferred_offline_state(nr, CPU_STATE_ONLINE);
1683aa565f5SGautham R Shenoy 
1693aa565f5SGautham R Shenoy 	if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
170c60e65d7SMatt Evans 		long rc;
171c60e65d7SMatt Evans 		unsigned long hcpuid;
172c60e65d7SMatt Evans 
1733aa565f5SGautham R Shenoy 		hcpuid = get_hard_smp_processor_id(nr);
1743aa565f5SGautham R Shenoy 		rc = plpar_hcall_norets(H_PROD, hcpuid);
1753aa565f5SGautham R Shenoy 		if (rc != H_SUCCESS)
1765a2ad98eSJoe Perches 			printk(KERN_ERR "Error: Prod to wake up processor %d "
1775a2ad98eSJoe Perches 						"Ret= %ld\n", nr, rc);
1783aa565f5SGautham R Shenoy 	}
179c60e65d7SMatt Evans #endif
180de300974SMichael Ellerman 
181de300974SMichael Ellerman 	return 0;
18269a80d3fSPaul Mackerras }
18369a80d3fSPaul Mackerras 
184eac1e731SCédric Le Goater static int pseries_smp_prepare_cpu(int cpu)
185eac1e731SCédric Le Goater {
186eac1e731SCédric Le Goater 	if (xive_enabled())
187eac1e731SCédric Le Goater 		return xive_smp_prepare_cpu(cpu);
188eac1e731SCédric Le Goater 	return 0;
189eac1e731SCédric Le Goater }
190eac1e731SCédric Le Goater 
191b866cc21SNicholas Piggin static void smp_pseries_cause_ipi(int cpu)
192e5e84f0aSIan Munsie {
193b866cc21SNicholas Piggin 	/* POWER9 should not use this handler */
194b866cc21SNicholas Piggin 	if (doorbell_try_core_ipi(cpu))
195b866cc21SNicholas Piggin 		return;
196b866cc21SNicholas Piggin 
197b866cc21SNicholas Piggin 	icp_ops->cause_ipi(cpu);
198e5e84f0aSIan Munsie }
199e5e84f0aSIan Munsie 
200102c05e8SNicholas Piggin static int pseries_cause_nmi_ipi(int cpu)
201102c05e8SNicholas Piggin {
202102c05e8SNicholas Piggin 	int hwcpu;
203102c05e8SNicholas Piggin 
204102c05e8SNicholas Piggin 	if (cpu == NMI_IPI_ALL_OTHERS) {
205102c05e8SNicholas Piggin 		hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
206102c05e8SNicholas Piggin 	} else {
207102c05e8SNicholas Piggin 		if (cpu < 0) {
208102c05e8SNicholas Piggin 			WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
209102c05e8SNicholas Piggin 			return 0;
210102c05e8SNicholas Piggin 		}
211102c05e8SNicholas Piggin 
212102c05e8SNicholas Piggin 		hwcpu = get_hard_smp_processor_id(cpu);
213102c05e8SNicholas Piggin 	}
214102c05e8SNicholas Piggin 
2157c09c186SMichael Ellerman 	if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
216102c05e8SNicholas Piggin 		return 1;
217102c05e8SNicholas Piggin 
218102c05e8SNicholas Piggin 	return 0;
219102c05e8SNicholas Piggin }
220102c05e8SNicholas Piggin 
221eac1e731SCédric Le Goater static __init void pSeries_smp_probe_xics(void)
222e5e84f0aSIan Munsie {
223a7f4ee1fSMichael Ellerman 	xics_smp_probe();
224e5e84f0aSIan Munsie 
2254edaac51SSukadev Bhattiprolu 	if (cpu_has_feature(CPU_FTR_DBELL) && !is_secure_guest())
226b866cc21SNicholas Piggin 		smp_ops->cause_ipi = smp_pseries_cause_ipi;
227b866cc21SNicholas Piggin 	else
228b866cc21SNicholas Piggin 		smp_ops->cause_ipi = icp_ops->cause_ipi;
229e5e84f0aSIan Munsie }
230e5e84f0aSIan Munsie 
231eac1e731SCédric Le Goater static __init void pSeries_smp_probe(void)
232eac1e731SCédric Le Goater {
233eac1e731SCédric Le Goater 	if (xive_enabled())
234eac1e731SCédric Le Goater 		/*
235eac1e731SCédric Le Goater 		 * Don't use P9 doorbells when XIVE is enabled. IPIs
236eac1e731SCédric Le Goater 		 * using MMIOs should be faster
237eac1e731SCédric Le Goater 		 */
238eac1e731SCédric Le Goater 		xive_smp_probe();
239eac1e731SCédric Le Goater 	else
240eac1e731SCédric Le Goater 		pSeries_smp_probe_xics();
241eac1e731SCédric Le Goater }
242eac1e731SCédric Le Goater 
24386425bedSRashmica Gupta static struct smp_ops_t pseries_smp_ops = {
2449ca980dcSPaul Mackerras 	.message_pass	= NULL,	/* Use smp_muxed_ipi_message_pass */
245e5e84f0aSIan Munsie 	.cause_ipi	= NULL,	/* Filled at runtime by pSeries_smp_probe() */
246102c05e8SNicholas Piggin 	.cause_nmi_ipi	= pseries_cause_nmi_ipi,
247e5e84f0aSIan Munsie 	.probe		= pSeries_smp_probe,
248eac1e731SCédric Le Goater 	.prepare_cpu	= pseries_smp_prepare_cpu,
24969a80d3fSPaul Mackerras 	.kick_cpu	= smp_pSeries_kick_cpu,
25086425bedSRashmica Gupta 	.setup_cpu	= smp_setup_cpu,
25139fd4027SAndy Fleming 	.cpu_bootable	= smp_generic_cpu_bootable,
25269a80d3fSPaul Mackerras };
25369a80d3fSPaul Mackerras 
25469a80d3fSPaul Mackerras /* This is called very early */
25586425bedSRashmica Gupta void __init smp_init_pseries(void)
25669a80d3fSPaul Mackerras {
25769a80d3fSPaul Mackerras 	int i;
25869a80d3fSPaul Mackerras 
259f7ebf352SMichael Ellerman 	pr_debug(" -> smp_init_pSeries()\n");
26086425bedSRashmica Gupta 	smp_ops = &pseries_smp_ops;
26169a80d3fSPaul Mackerras 
262af831e1eSAnton Blanchard 	alloc_bootmem_cpumask_var(&of_spin_mask);
263af831e1eSAnton Blanchard 
264dbe78b40SBenjamin Herrenschmidt 	/*
265dbe78b40SBenjamin Herrenschmidt 	 * Mark threads which are still spinning in hold loops
266dbe78b40SBenjamin Herrenschmidt 	 *
267dbe78b40SBenjamin Herrenschmidt 	 * We know prom_init will not have started them if RTAS supports
268dbe78b40SBenjamin Herrenschmidt 	 * query-cpu-stopped-state.
269dbe78b40SBenjamin Herrenschmidt 	 */
270dbe78b40SBenjamin Herrenschmidt 	if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
27169a80d3fSPaul Mackerras 		if (cpu_has_feature(CPU_FTR_SMT)) {
27269a80d3fSPaul Mackerras 			for_each_present_cpu(i) {
2736a75a6b8SMilton Miller 				if (cpu_thread_in_core(i) == 0)
274af831e1eSAnton Blanchard 					cpumask_set_cpu(i, of_spin_mask);
27569a80d3fSPaul Mackerras 			}
276dbe78b40SBenjamin Herrenschmidt 		} else
277af831e1eSAnton Blanchard 			cpumask_copy(of_spin_mask, cpu_present_mask);
27869a80d3fSPaul Mackerras 
279af831e1eSAnton Blanchard 		cpumask_clear_cpu(boot_cpuid, of_spin_mask);
280dbe78b40SBenjamin Herrenschmidt 	}
28169a80d3fSPaul Mackerras 
28269a80d3fSPaul Mackerras 	/* Non-lpar has additional take/give timebase */
28369a80d3fSPaul Mackerras 	if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
284c4007a2fSBenjamin Herrenschmidt 		smp_ops->give_timebase = rtas_give_timebase;
285c4007a2fSBenjamin Herrenschmidt 		smp_ops->take_timebase = rtas_take_timebase;
28669a80d3fSPaul Mackerras 	}
28769a80d3fSPaul Mackerras 
288f7ebf352SMichael Ellerman 	pr_debug(" <- smp_init_pSeries()\n");
28969a80d3fSPaul Mackerras }
290