1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SMP support for pSeries machines.
5 * Dave Engebretsen, Peter Bergner, and
6 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
8 * Plus various changes from other IBM teams...
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>
25 #include <asm/ptrace.h>
26 #include <linux/atomic.h>
33 #include <asm/machdep.h>
34 #include <asm/cputable.h>
35 #include <asm/firmware.h>
37 #include <asm/vdso_datapage.h>
38 #include <asm/cputhreads.h>
41 #include <asm/dbell.h>
42 #include <asm/plpar_wrappers.h>
43 #include <asm/code-patching.h>
47 #include "offline_states.h"
51 * The Primary thread of each non-boot processor was started from the OF client
52 * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
54 static cpumask_var_t of_spin_mask;
56 /* Query where a cpu is now. Return codes #defined in plpar_wrappers.h */
57 int smp_query_cpu_stopped(unsigned int pcpu)
59 int cpu_status, status;
60 int qcss_tok = rtas_token("query-cpu-stopped-state");
62 if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
64 "Firmware doesn't support query-cpu-stopped-state\n");
65 return QCSS_HARDWARE_ERROR;
68 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
71 "RTAS query-cpu-stopped-state failed: %i\n", status);
79 * smp_startup_cpu() - start the given cpu
81 * At boot time, there is nothing to do for primary threads which were
82 * started from Open Firmware. For anything else, call RTAS with the
83 * appropriate start location.
89 static inline int smp_startup_cpu(unsigned int lcpu)
92 unsigned long start_here =
93 __pa(ppc_function_entry(generic_secondary_smp_init));
97 if (cpumask_test_cpu(lcpu, of_spin_mask))
98 /* Already started by OF and sitting in spin loop */
101 pcpu = get_hard_smp_processor_id(lcpu);
103 /* Check to see if the CPU out of FW already for kexec */
104 if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
105 cpumask_set_cpu(lcpu, of_spin_mask);
109 /* Fixup atomic count: it exited inside IRQ handler. */
110 task_thread_info(paca_ptrs[lcpu]->__current)->preempt_count = 0;
111 #ifdef CONFIG_HOTPLUG_CPU
112 if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
116 * If the RTAS start-cpu token does not exist then presume the
117 * cpu is already spinning.
119 start_cpu = rtas_token("start-cpu");
120 if (start_cpu == RTAS_UNKNOWN_SERVICE)
123 status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
125 printk(KERN_ERR "start-cpu failed: %i\n", status);
129 #ifdef CONFIG_HOTPLUG_CPU
135 static void smp_setup_cpu(int cpu)
138 xive_smp_setup_cpu();
139 else if (cpu != boot_cpuid)
142 if (firmware_has_feature(FW_FEATURE_SPLPAR))
145 cpumask_clear_cpu(cpu, of_spin_mask);
146 #ifdef CONFIG_HOTPLUG_CPU
147 set_cpu_current_state(cpu, CPU_STATE_ONLINE);
148 set_default_offline_state(cpu);
152 static int smp_pSeries_kick_cpu(int nr)
154 if (nr < 0 || nr >= nr_cpu_ids)
157 if (!smp_startup_cpu(nr))
161 * The processor is currently spinning, waiting for the
162 * cpu_start field to become non-zero After we set cpu_start,
163 * the processor will continue on to secondary_start
165 paca_ptrs[nr]->cpu_start = 1;
166 #ifdef CONFIG_HOTPLUG_CPU
167 set_preferred_offline_state(nr, CPU_STATE_ONLINE);
169 if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
171 unsigned long hcpuid;
173 hcpuid = get_hard_smp_processor_id(nr);
174 rc = plpar_hcall_norets(H_PROD, hcpuid);
176 printk(KERN_ERR "Error: Prod to wake up processor %d "
177 "Ret= %ld\n", nr, rc);
184 static int pseries_smp_prepare_cpu(int cpu)
187 return xive_smp_prepare_cpu(cpu);
191 static void smp_pseries_cause_ipi(int cpu)
193 /* POWER9 should not use this handler */
194 if (doorbell_try_core_ipi(cpu))
197 icp_ops->cause_ipi(cpu);
200 static int pseries_cause_nmi_ipi(int cpu)
204 if (cpu == NMI_IPI_ALL_OTHERS) {
205 hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
208 WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
212 hwcpu = get_hard_smp_processor_id(cpu);
215 if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
221 static __init void pSeries_smp_probe_xics(void)
225 if (cpu_has_feature(CPU_FTR_DBELL) && !is_secure_guest())
226 smp_ops->cause_ipi = smp_pseries_cause_ipi;
228 smp_ops->cause_ipi = icp_ops->cause_ipi;
231 static __init void pSeries_smp_probe(void)
235 * Don't use P9 doorbells when XIVE is enabled. IPIs
236 * using MMIOs should be faster
240 pSeries_smp_probe_xics();
243 static struct smp_ops_t pseries_smp_ops = {
244 .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
245 .cause_ipi = NULL, /* Filled at runtime by pSeries_smp_probe() */
246 .cause_nmi_ipi = pseries_cause_nmi_ipi,
247 .probe = pSeries_smp_probe,
248 .prepare_cpu = pseries_smp_prepare_cpu,
249 .kick_cpu = smp_pSeries_kick_cpu,
250 .setup_cpu = smp_setup_cpu,
251 .cpu_bootable = smp_generic_cpu_bootable,
254 /* This is called very early */
255 void __init smp_init_pseries(void)
259 pr_debug(" -> smp_init_pSeries()\n");
260 smp_ops = &pseries_smp_ops;
262 alloc_bootmem_cpumask_var(&of_spin_mask);
265 * Mark threads which are still spinning in hold loops
267 * We know prom_init will not have started them if RTAS supports
268 * query-cpu-stopped-state.
270 if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
271 if (cpu_has_feature(CPU_FTR_SMT)) {
272 for_each_present_cpu(i) {
273 if (cpu_thread_in_core(i) == 0)
274 cpumask_set_cpu(i, of_spin_mask);
277 cpumask_copy(of_spin_mask, cpu_present_mask);
279 cpumask_clear_cpu(boot_cpuid, of_spin_mask);
282 /* Non-lpar has additional take/give timebase */
283 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
284 smp_ops->give_timebase = rtas_give_timebase;
285 smp_ops->take_timebase = rtas_take_timebase;
288 pr_debug(" <- smp_init_pSeries()\n");