1
2
3
4
5
6
7
8
9
10
11
12
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#include <asm/svm.h>
49
50#include "pseries.h"
51#include "offline_states.h"
52
53
54
55
56
57
58static cpumask_var_t of_spin_mask;
59
60
61int smp_query_cpu_stopped(unsigned int pcpu)
62{
63 int cpu_status, status;
64 int qcss_tok = rtas_token("query-cpu-stopped-state");
65
66 if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
67 printk_once(KERN_INFO
68 "Firmware doesn't support query-cpu-stopped-state\n");
69 return QCSS_HARDWARE_ERROR;
70 }
71
72 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
73 if (status != 0) {
74 printk(KERN_ERR
75 "RTAS query-cpu-stopped-state failed: %i\n", status);
76 return status;
77 }
78
79 return cpu_status;
80}
81
82
83
84
85
86
87
88
89
90
91
92
93static inline int smp_startup_cpu(unsigned int lcpu)
94{
95 int status;
96 unsigned long start_here =
97 __pa(ppc_function_entry(generic_secondary_smp_init));
98 unsigned int pcpu;
99 int start_cpu;
100
101 if (cpumask_test_cpu(lcpu, of_spin_mask))
102
103 return 1;
104
105 pcpu = get_hard_smp_processor_id(lcpu);
106
107
108 if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
109 cpumask_set_cpu(lcpu, of_spin_mask);
110 return 1;
111 }
112
113
114 task_thread_info(paca_ptrs[lcpu]->__current)->preempt_count = 0;
115#ifdef CONFIG_HOTPLUG_CPU
116 if (get_cpu_current_state(lcpu) == CPU_STATE_INACTIVE)
117 goto out;
118#endif
119
120
121
122
123 start_cpu = rtas_token("start-cpu");
124 if (start_cpu == RTAS_UNKNOWN_SERVICE)
125 return 1;
126
127 status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
128 if (status != 0) {
129 printk(KERN_ERR "start-cpu failed: %i\n", status);
130 return 0;
131 }
132
133#ifdef CONFIG_HOTPLUG_CPU
134out:
135#endif
136 return 1;
137}
138
139static void smp_setup_cpu(int cpu)
140{
141 if (xive_enabled())
142 xive_smp_setup_cpu();
143 else if (cpu != boot_cpuid)
144 xics_setup_cpu();
145
146 if (firmware_has_feature(FW_FEATURE_SPLPAR))
147 vpa_init(cpu);
148
149 cpumask_clear_cpu(cpu, of_spin_mask);
150#ifdef CONFIG_HOTPLUG_CPU
151 set_cpu_current_state(cpu, CPU_STATE_ONLINE);
152 set_default_offline_state(cpu);
153#endif
154}
155
156static int smp_pSeries_kick_cpu(int nr)
157{
158 if (nr < 0 || nr >= nr_cpu_ids)
159 return -EINVAL;
160
161 if (!smp_startup_cpu(nr))
162 return -ENOENT;
163
164
165
166
167
168
169 paca_ptrs[nr]->cpu_start = 1;
170#ifdef CONFIG_HOTPLUG_CPU
171 set_preferred_offline_state(nr, CPU_STATE_ONLINE);
172
173 if (get_cpu_current_state(nr) == CPU_STATE_INACTIVE) {
174 long rc;
175 unsigned long hcpuid;
176
177 hcpuid = get_hard_smp_processor_id(nr);
178 rc = plpar_hcall_norets(H_PROD, hcpuid);
179 if (rc != H_SUCCESS)
180 printk(KERN_ERR "Error: Prod to wake up processor %d "
181 "Ret= %ld\n", nr, rc);
182 }
183#endif
184
185 return 0;
186}
187
188static int pseries_smp_prepare_cpu(int cpu)
189{
190 if (xive_enabled())
191 return xive_smp_prepare_cpu(cpu);
192 return 0;
193}
194
195
196static void (*ic_cause_ipi)(int cpu) __ro_after_init;
197
198
199static void dbell_or_ic_cause_ipi(int cpu)
200{
201 if (doorbell_try_core_ipi(cpu))
202 return;
203
204 ic_cause_ipi(cpu);
205}
206
207static int pseries_cause_nmi_ipi(int cpu)
208{
209 int hwcpu;
210
211 if (cpu == NMI_IPI_ALL_OTHERS) {
212 hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
213 } else {
214 if (cpu < 0) {
215 WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
216 return 0;
217 }
218
219 hwcpu = get_hard_smp_processor_id(cpu);
220 }
221
222 if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
223 return 1;
224
225 return 0;
226}
227
228static __init void pSeries_smp_probe(void)
229{
230 if (xive_enabled())
231 xive_smp_probe();
232 else
233 xics_smp_probe();
234
235
236 if (!cpu_has_feature(CPU_FTR_DBELL))
237 return;
238
239
240 if (!cpu_has_feature(CPU_FTR_SMT))
241 return;
242
243 if (is_kvm_guest()) {
244
245
246
247
248
249 if (xive_enabled())
250 return;
251
252
253
254
255
256
257
258 if (is_secure_guest())
259 return;
260 }
261
262
263
264
265
266
267
268
269 ic_cause_ipi = smp_ops->cause_ipi;
270 smp_ops->cause_ipi = dbell_or_ic_cause_ipi;
271}
272
273static struct smp_ops_t pseries_smp_ops = {
274 .message_pass = NULL,
275 .cause_ipi = NULL,
276 .cause_nmi_ipi = pseries_cause_nmi_ipi,
277 .probe = pSeries_smp_probe,
278 .prepare_cpu = pseries_smp_prepare_cpu,
279 .kick_cpu = smp_pSeries_kick_cpu,
280 .setup_cpu = smp_setup_cpu,
281 .cpu_bootable = smp_generic_cpu_bootable,
282};
283
284
285void __init smp_init_pseries(void)
286{
287 int i;
288
289 pr_debug(" -> smp_init_pSeries()\n");
290 smp_ops = &pseries_smp_ops;
291
292 alloc_bootmem_cpumask_var(&of_spin_mask);
293
294
295
296
297
298
299
300 if (rtas_token("query-cpu-stopped-state") == RTAS_UNKNOWN_SERVICE) {
301 if (cpu_has_feature(CPU_FTR_SMT)) {
302 for_each_present_cpu(i) {
303 if (cpu_thread_in_core(i) == 0)
304 cpumask_set_cpu(i, of_spin_mask);
305 }
306 } else
307 cpumask_copy(of_spin_mask, cpu_present_mask);
308
309 cpumask_clear_cpu(boot_cpuid, of_spin_mask);
310 }
311
312
313 if (rtas_token("freeze-time-base") != RTAS_UNKNOWN_SERVICE) {
314 smp_ops->give_timebase = rtas_give_timebase;
315 smp_ops->take_timebase = rtas_take_timebase;
316 }
317
318 pr_debug(" <- smp_init_pSeries()\n");
319}
320