xref: /linux/arch/powerpc/platforms/pseries/smp.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SMP support for pSeries machines.
4  *
5  * Dave Engebretsen, Peter Bergner, and
6  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
7  *
8  * Plus various changes from other IBM teams...
9  */
10 
11 
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/spinlock.h>
19 #include <linux/cache.h>
20 #include <linux/err.h>
21 #include <linux/device.h>
22 #include <linux/cpu.h>
23 #include <linux/pgtable.h>
24 
25 #include <asm/ptrace.h>
26 #include <linux/atomic.h>
27 #include <asm/irq.h>
28 #include <asm/page.h>
29 #include <asm/io.h>
30 #include <asm/smp.h>
31 #include <asm/paca.h>
32 #include <asm/machdep.h>
33 #include <asm/cputable.h>
34 #include <asm/firmware.h>
35 #include <asm/rtas.h>
36 #include <asm/vdso_datapage.h>
37 #include <asm/cputhreads.h>
38 #include <asm/xics.h>
39 #include <asm/xive.h>
40 #include <asm/dbell.h>
41 #include <asm/plpar_wrappers.h>
42 #include <asm/text-patching.h>
43 #include <asm/svm.h>
44 #include <asm/kvm_guest.h>
45 
46 #include "pseries.h"
47 
48 /*
49  * The Primary thread of each non-boot processor was started from the OF client
50  * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
51  */
52 static cpumask_var_t of_spin_mask;
53 
54 /* Query where a cpu is now.  Return codes #defined in plpar_wrappers.h */
smp_query_cpu_stopped(unsigned int pcpu)55 int smp_query_cpu_stopped(unsigned int pcpu)
56 {
57 	int cpu_status, status;
58 	int qcss_tok = rtas_function_token(RTAS_FN_QUERY_CPU_STOPPED_STATE);
59 
60 	if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
61 		printk_once(KERN_INFO
62 			"Firmware doesn't support query-cpu-stopped-state\n");
63 		return QCSS_HARDWARE_ERROR;
64 	}
65 
66 	status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
67 	if (status != 0) {
68 		printk(KERN_ERR
69 		       "RTAS query-cpu-stopped-state failed: %i\n", status);
70 		return status;
71 	}
72 
73 	return cpu_status;
74 }
75 
76 /**
77  * smp_startup_cpu() - start the given cpu
78  *
79  * At boot time, there is nothing to do for primary threads which were
80  * started from Open Firmware.  For anything else, call RTAS with the
81  * appropriate start location.
82  *
83  * Returns:
84  *	0	- failure
85  *	1	- success
86  */
smp_startup_cpu(unsigned int lcpu)87 static inline int smp_startup_cpu(unsigned int lcpu)
88 {
89 	int status;
90 	unsigned long start_here =
91 			__pa(ppc_function_entry(generic_secondary_smp_init));
92 	unsigned int pcpu;
93 	int start_cpu;
94 
95 	if (cpumask_test_cpu(lcpu, of_spin_mask))
96 		/* Already started by OF and sitting in spin loop */
97 		return 1;
98 
99 	pcpu = get_hard_smp_processor_id(lcpu);
100 
101 	/* Check to see if the CPU out of FW already for kexec */
102 	if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
103 		cpumask_set_cpu(lcpu, of_spin_mask);
104 		return 1;
105 	}
106 
107 	/*
108 	 * If the RTAS start-cpu token does not exist then presume the
109 	 * cpu is already spinning.
110 	 */
111 	start_cpu = rtas_function_token(RTAS_FN_START_CPU);
112 	if (start_cpu == RTAS_UNKNOWN_SERVICE)
113 		return 1;
114 
115 	status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
116 	if (status != 0) {
117 		printk(KERN_ERR "start-cpu failed: %i\n", status);
118 		return 0;
119 	}
120 
121 	return 1;
122 }
123 
smp_setup_cpu(int cpu)124 static void smp_setup_cpu(int cpu)
125 {
126 	if (xive_enabled())
127 		xive_smp_setup_cpu();
128 	else if (cpu != boot_cpuid)
129 		xics_setup_cpu();
130 
131 	/*
132 	 * Initialize VPA on non-boot cpus since boot-cpu vpa was
133 	 * already initialized in pSeries_setup_arch()
134 	 */
135 	if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
136 	    cpu != boot_cpuid)
137 		vpa_init(cpu);
138 
139 	cpumask_clear_cpu(cpu, of_spin_mask);
140 }
141 
smp_pSeries_kick_cpu(int nr)142 static int smp_pSeries_kick_cpu(int nr)
143 {
144 	if (nr < 0 || nr >= nr_cpu_ids)
145 		return -EINVAL;
146 
147 	if (!smp_startup_cpu(nr))
148 		return -ENOENT;
149 
150 	/*
151 	 * The processor is currently spinning, waiting for the
152 	 * cpu_start field to become non-zero After we set cpu_start,
153 	 * the processor will continue on to secondary_start
154 	 */
155 	paca_ptrs[nr]->cpu_start = 1;
156 
157 	return 0;
158 }
159 
pseries_smp_prepare_cpu(int cpu)160 static int pseries_smp_prepare_cpu(int cpu)
161 {
162 	if (xive_enabled())
163 		return xive_smp_prepare_cpu(cpu);
164 	return 0;
165 }
166 
167 /* Cause IPI as setup by the interrupt controller (xics or xive) */
168 static void (*ic_cause_ipi)(int cpu) __ro_after_init;
169 
170 /* Use msgsndp doorbells target is a sibling, else use interrupt controller */
dbell_or_ic_cause_ipi(int cpu)171 static void dbell_or_ic_cause_ipi(int cpu)
172 {
173 	if (doorbell_try_core_ipi(cpu))
174 		return;
175 
176 	ic_cause_ipi(cpu);
177 }
178 
pseries_cause_nmi_ipi(int cpu)179 static int pseries_cause_nmi_ipi(int cpu)
180 {
181 	int hwcpu;
182 
183 	if (cpu == NMI_IPI_ALL_OTHERS) {
184 		hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
185 	} else {
186 		if (cpu < 0) {
187 			WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
188 			return 0;
189 		}
190 
191 		hwcpu = get_hard_smp_processor_id(cpu);
192 	}
193 
194 	if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
195 		return 1;
196 
197 	return 0;
198 }
199 
pSeries_smp_probe(void)200 static __init void pSeries_smp_probe(void)
201 {
202 	if (xive_enabled()) {
203 		if (xive_smp_probe() < 0)
204 			return;
205 	} else {
206 		xics_smp_probe();
207 	}
208 
209 	/* No doorbell facility, must use the interrupt controller for IPIs */
210 	if (!cpu_has_feature(CPU_FTR_DBELL))
211 		return;
212 
213 	/* Doorbells can only be used for IPIs between SMT siblings */
214 	if (!cpu_has_feature(CPU_FTR_SMT))
215 		return;
216 
217 	check_kvm_guest();
218 
219 	if (is_kvm_guest()) {
220 		/*
221 		 * KVM emulates doorbells by disabling FSCR[MSGP] so msgsndp
222 		 * faults to the hypervisor which then reads the instruction
223 		 * from guest memory, which tends to be slower than using XIVE.
224 		 */
225 		if (xive_enabled())
226 			return;
227 
228 		/*
229 		 * XICS hcalls aren't as fast, so we can use msgsndp (which
230 		 * also helps exercise KVM emulation), however KVM can't
231 		 * emulate secure guests because it can't read the instruction
232 		 * out of their memory.
233 		 */
234 		if (is_secure_guest())
235 			return;
236 	}
237 
238 	/*
239 	 * Under PowerVM, FSCR[MSGP] is enabled as guest vCPU siblings are
240 	 * gang scheduled on the same physical core, so doorbells are always
241 	 * faster than the interrupt controller, and they can be used by
242 	 * secure guests.
243 	 */
244 
245 	ic_cause_ipi = smp_ops->cause_ipi;
246 	smp_ops->cause_ipi = dbell_or_ic_cause_ipi;
247 }
248 
249 static struct smp_ops_t pseries_smp_ops = {
250 	.message_pass	= NULL,	/* Use smp_muxed_ipi_message_pass */
251 	.cause_ipi	= NULL,	/* Filled at runtime by pSeries_smp_probe() */
252 	.cause_nmi_ipi	= pseries_cause_nmi_ipi,
253 	.probe		= pSeries_smp_probe,
254 	.prepare_cpu	= pseries_smp_prepare_cpu,
255 	.kick_cpu	= smp_pSeries_kick_cpu,
256 	.setup_cpu	= smp_setup_cpu,
257 	.cpu_bootable	= smp_generic_cpu_bootable,
258 };
259 
260 /* This is called very early */
smp_init_pseries(void)261 void __init smp_init_pseries(void)
262 {
263 	int i;
264 
265 	pr_debug(" -> smp_init_pSeries()\n");
266 	smp_ops = &pseries_smp_ops;
267 
268 	alloc_bootmem_cpumask_var(&of_spin_mask);
269 
270 	/*
271 	 * Mark threads which are still spinning in hold loops
272 	 *
273 	 * We know prom_init will not have started them if RTAS supports
274 	 * query-cpu-stopped-state.
275 	 */
276 	if (rtas_function_token(RTAS_FN_QUERY_CPU_STOPPED_STATE) == RTAS_UNKNOWN_SERVICE) {
277 		if (cpu_has_feature(CPU_FTR_SMT)) {
278 			for_each_present_cpu(i) {
279 				if (cpu_thread_in_core(i) == 0)
280 					cpumask_set_cpu(i, of_spin_mask);
281 			}
282 		} else
283 			cpumask_copy(of_spin_mask, cpu_present_mask);
284 
285 		cpumask_clear_cpu(boot_cpuid, of_spin_mask);
286 	}
287 
288 	pr_debug(" <- smp_init_pSeries()\n");
289 }
290