1 /*
2 * SMP support for pSeries machines.
3 *
4 * Dave Engebretsen, Peter Bergner, and
5 * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
6 *
7 * Plus various changes from other IBM teams...
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #include <linux/cache.h>
24 #include <linux/err.h>
25 #include <linux/device.h>
26 #include <linux/cpu.h>
27
28 #include <asm/ptrace.h>
29 #include <linux/atomic.h>
30 #include <asm/irq.h>
31 #include <asm/page.h>
32 #include <asm/pgtable.h>
33 #include <asm/io.h>
34 #include <asm/prom.h>
35 #include <asm/smp.h>
36 #include <asm/paca.h>
37 #include <asm/machdep.h>
38 #include <asm/cputable.h>
39 #include <asm/firmware.h>
40 #include <asm/rtas.h>
41 #include <asm/vdso_datapage.h>
42 #include <asm/cputhreads.h>
43 #include <asm/xics.h>
44 #include <asm/xive.h>
45 #include <asm/dbell.h>
46 #include <asm/plpar_wrappers.h>
47 #include <asm/code-patching.h>
48
49 #include "pseries.h"
50 #include "offline_states.h"
51
52
53 /*
54 * The Primary thread of each non-boot processor was started from the OF client
55 * interface by prom_hold_cpus and is spinning on secondary_hold_spinloop.
56 */
57 static cpumask_var_t of_spin_mask;
58
59 /* Query where a cpu is now. Return codes #defined in plpar_wrappers.h */
smp_query_cpu_stopped(unsigned int pcpu)60 int smp_query_cpu_stopped(unsigned int pcpu)
61 {
62 int cpu_status, status;
63 int qcss_tok = rtas_token("query-cpu-stopped-state");
64
65 if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
66 printk_once(KERN_INFO
67 "Firmware doesn't support query-cpu-stopped-state\n");
68 return QCSS_HARDWARE_ERROR;
69 }
70
71 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
72 if (status != 0) {
73 printk(KERN_ERR
74 "RTAS query-cpu-stopped-state failed: %i\n", status);
75 return status;
76 }
77
78 return cpu_status;
79 }
80
81 /**
82 * smp_startup_cpu() - start the given cpu
83 *
84 * At boot time, there is nothing to do for primary threads which were
85 * started from Open Firmware. For anything else, call RTAS with the
86 * appropriate start location.
87 *
88 * Returns:
89 * 0 - failure
90 * 1 - success
91 */
smp_startup_cpu(unsigned int lcpu)92 static inline int smp_startup_cpu(unsigned int lcpu)
93 {
94 int status;
95 unsigned long start_here =
96 __pa(ppc_function_entry(generic_secondary_smp_init));
97 unsigned int pcpu;
98 int start_cpu;
99
100 if (cpumask_test_cpu(lcpu, of_spin_mask))
101 /* Already started by OF and sitting in spin loop */
102 return 1;
103
104 pcpu = get_hard_smp_processor_id(lcpu);
105
106 /* Check to see if the CPU out of FW already for kexec */
107 if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
108 cpumask_set_cpu(lcpu, of_spin_mask);
109 return 1;
110 }
111
112 /* Fixup atomic count: it exited inside IRQ handler. */
113 task_thread_info(paca_ptrs[lcpu]->__current)->preempt_count = 0;
114 #ifdef CONFIG_HOTPLUG_CPU
115 if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
116 goto out;
117 #endif
118 /*
119 * If the RTAS start-cpu token does not exist then presume the
120 * cpu is already spinning.
121 */
122 start_cpu = rtas_token("start-cpu");
123 if (start_cpu == RTAS_UNKNOWN_SERVICE)
124 return 1;
125
126 status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
127 if (status != 0) {
128 printk(KERN_ERR "start-cpu failed: %i\n", status);
129 return 0;
130 }
131
132 #ifdef CONFIG_HOTPLUG_CPU
133 out:
134 #endif
135 return 1;
136 }
137
smp_setup_cpu(int cpu)138 static void smp_setup_cpu(int cpu)
139 {
140 if (xive_enabled())
141 xive_smp_setup_cpu();
142 else if (cpu != boot_cpuid)
143 xics_setup_cpu();
144
145 if (firmware_has_feature(FW_FEATURE_SPLPAR))
146 vpa_init(cpu);
147
148 cpumask_clear_cpu(cpu, of_spin_mask);
149 #ifdef CONFIG_HOTPLUG_CPU
150 set_cpu_current_state(cpu, CPU_STATE_ONLINE);
151 set_default_offline_state(cpu);
152 #endif
153 }
154
smp_pSeries_kick_cpu(int nr)155 static int smp_pSeries_kick_cpu(int nr)
156 {
157 if (nr < 0 || nr >= nr_cpu_ids)
158 return -EINVAL;
159
160 if (!smp_startup_cpu(nr))
161 return -ENOENT;
162
163 /*
164 * The processor is currently spinning, waiting for the
165 * cpu_start field to become non-zero After we set cpu_start,
166 * the processor will continue on to secondary_start
167 */
168 paca_ptrs[nr]->cpu_start = 1;
169 #ifdef CONFIG_HOTPLUG_CPU
170 set_preferred_offline_state(nr, CPU_STATE_ONLINE);
171
172 if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
173 long rc;
174 unsigned long hcpuid;
175
176 hcpuid = get_hard_smp_processor_id(nr);
177 rc = plpar_hcall_norets(H_PROD, hcpuid);
178 if (rc != H_SUCCESS)
179 printk(KERN_ERR "Error: Prod to wake up processor %d "
180 "Ret= %ld\n", nr, rc);
181 }
182 #endif
183
184 return 0;
185 }
186
pseries_smp_prepare_cpu(int cpu)187 static int pseries_smp_prepare_cpu(int cpu)
188 {
189 if (xive_enabled())
190 return xive_smp_prepare_cpu(cpu);
191 return 0;
192 }
193
smp_pseries_cause_ipi(int cpu)194 static void smp_pseries_cause_ipi(int cpu)
195 {
196 /* POWER9 should not use this handler */
197 if (doorbell_try_core_ipi(cpu))
198 return;
199
200 icp_ops->cause_ipi(cpu);
201 }
202
pseries_cause_nmi_ipi(int cpu)203 static int pseries_cause_nmi_ipi(int cpu)
204 {
205 int hwcpu;
206
207 if (cpu == NMI_IPI_ALL_OTHERS) {
208 hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
209 } else {
210 if (cpu < 0) {
211 WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
212 return 0;
213 }
214
215 hwcpu = get_hard_smp_processor_id(cpu);
216 }
217
218 if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
219 return 1;
220
221 return 0;
222 }
223
pSeries_smp_probe_xics(void)224 static __init void pSeries_smp_probe_xics(void)
225 {
226 xics_smp_probe();
227
228 if (cpu_has_feature(CPU_FTR_DBELL))
229 smp_ops->cause_ipi = smp_pseries_cause_ipi;
230 else
231 smp_ops->cause_ipi = icp_ops->cause_ipi;
232 }
233
pSeries_smp_probe(void)234 static __init void pSeries_smp_probe(void)
235 {
236 if (xive_enabled())
237 /*
238 * Don't use P9 doorbells when XIVE is enabled. IPIs
239 * using MMIOs should be faster
240 */
241 xive_smp_probe();
242 else
243 pSeries_smp_probe_xics();
244 }
245
246 static struct smp_ops_t pseries_smp_ops = {
247 .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
248 .cause_ipi = NULL, /* Filled at runtime by pSeries_smp_probe() */
249 .cause_nmi_ipi = pseries_cause_nmi_ipi,
250 .probe = pSeries_smp_probe,
251 .prepare_cpu = pseries_smp_prepare_cpu,
252 .kick_cpu = smp_pSeries_kick_cpu,
253 .setup_cpu = smp_setup_cpu,
254 .cpu_bootable = smp_generic_cpu_bootable,
255 };
256
257 /* This is called very early */
smp_init_pseries(void)258 void __init smp_init_pseries(void)
259 {
260 int i;
261
262 pr_debug(" -> smp_init_pSeries()\n");
263 smp_ops = &pseries_smp_ops;
264
265 alloc_bootmem_cpumask_var(&of_spin_mask);
266
267 /*
268 * Mark threads which are still spinning in hold loops
269 *
270 * We know prom_init will not have started them if RTAS supports
271 * query-cpu-stopped-state.
272 */
273 if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
274 if (cpu_has_feature(CPU_FTR_SMT)) {
275 for_each_present_cpu(i) {
276 if (cpu_thread_in_core(i) == 0)
277 cpumask_set_cpu(i, of_spin_mask);
278 }
279 } else
280 cpumask_copy(of_spin_mask, cpu_present_mask);
281
282 cpumask_clear_cpu(boot_cpuid, of_spin_mask);
283 }
284
285 /* Non-lpar has additional take/give timebase */
286 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
287 smp_ops->give_timebase = rtas_give_timebase;
288 smp_ops->take_timebase = rtas_take_timebase;
289 }
290
291 pr_debug(" <- smp_init_pSeries()\n");
292 }
293