1
2
3
4
5
6
7
8
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/prom.h>
31#include <asm/smp.h>
32#include <asm/paca.h>
33#include <asm/machdep.h>
34#include <asm/cputable.h>
35#include <asm/firmware.h>
36#include <asm/rtas.h>
37#include <asm/vdso_datapage.h>
38#include <asm/cputhreads.h>
39#include <asm/xics.h>
40#include <asm/xive.h>
41#include <asm/dbell.h>
42#include <asm/plpar_wrappers.h>
43#include <asm/code-patching.h>
44#include <asm/svm.h>
45#include <asm/kvm_guest.h>
46
47#include "pseries.h"
48
49
50
51
52
53static cpumask_var_t of_spin_mask;
54
55
56int smp_query_cpu_stopped(unsigned int pcpu)
57{
58 int cpu_status, status;
59 int qcss_tok = rtas_token("query-cpu-stopped-state");
60
61 if (qcss_tok == RTAS_UNKNOWN_SERVICE) {
62 printk_once(KERN_INFO
63 "Firmware doesn't support query-cpu-stopped-state\n");
64 return QCSS_HARDWARE_ERROR;
65 }
66
67 status = rtas_call(qcss_tok, 1, 2, &cpu_status, pcpu);
68 if (status != 0) {
69 printk(KERN_ERR
70 "RTAS query-cpu-stopped-state failed: %i\n", status);
71 return status;
72 }
73
74 return cpu_status;
75}
76
77
78
79
80
81
82
83
84
85
86
87
88static inline int smp_startup_cpu(unsigned int lcpu)
89{
90 int status;
91 unsigned long start_here =
92 __pa(ppc_function_entry(generic_secondary_smp_init));
93 unsigned int pcpu;
94 int start_cpu;
95
96 if (cpumask_test_cpu(lcpu, of_spin_mask))
97
98 return 1;
99
100 pcpu = get_hard_smp_processor_id(lcpu);
101
102
103 if (smp_query_cpu_stopped(pcpu) == QCSS_NOT_STOPPED){
104 cpumask_set_cpu(lcpu, of_spin_mask);
105 return 1;
106 }
107
108
109
110
111
112 start_cpu = rtas_token("start-cpu");
113 if (start_cpu == RTAS_UNKNOWN_SERVICE)
114 return 1;
115
116 status = rtas_call(start_cpu, 3, 1, NULL, pcpu, start_here, pcpu);
117 if (status != 0) {
118 printk(KERN_ERR "start-cpu failed: %i\n", status);
119 return 0;
120 }
121
122 return 1;
123}
124
125static void smp_setup_cpu(int cpu)
126{
127 if (xive_enabled())
128 xive_smp_setup_cpu();
129 else if (cpu != boot_cpuid)
130 xics_setup_cpu();
131
132 if (firmware_has_feature(FW_FEATURE_SPLPAR))
133 vpa_init(cpu);
134
135 cpumask_clear_cpu(cpu, of_spin_mask);
136}
137
138static int smp_pSeries_kick_cpu(int nr)
139{
140 if (nr < 0 || nr >= nr_cpu_ids)
141 return -EINVAL;
142
143 if (!smp_startup_cpu(nr))
144 return -ENOENT;
145
146
147
148
149
150
151 paca_ptrs[nr]->cpu_start = 1;
152
153 return 0;
154}
155
156static int pseries_smp_prepare_cpu(int cpu)
157{
158 if (xive_enabled())
159 return xive_smp_prepare_cpu(cpu);
160 return 0;
161}
162
163
164static void (*ic_cause_ipi)(int cpu) __ro_after_init;
165
166
167static void dbell_or_ic_cause_ipi(int cpu)
168{
169 if (doorbell_try_core_ipi(cpu))
170 return;
171
172 ic_cause_ipi(cpu);
173}
174
175static int pseries_cause_nmi_ipi(int cpu)
176{
177 int hwcpu;
178
179 if (cpu == NMI_IPI_ALL_OTHERS) {
180 hwcpu = H_SIGNAL_SYS_RESET_ALL_OTHERS;
181 } else {
182 if (cpu < 0) {
183 WARN_ONCE(true, "incorrect cpu parameter %d", cpu);
184 return 0;
185 }
186
187 hwcpu = get_hard_smp_processor_id(cpu);
188 }
189
190 if (plpar_signal_sys_reset(hwcpu) == H_SUCCESS)
191 return 1;
192
193 return 0;
194}
195
196static __init void pSeries_smp_probe(void)
197{
198 if (xive_enabled())
199 xive_smp_probe();
200 else
201 xics_smp_probe();
202
203
204 if (!cpu_has_feature(CPU_FTR_DBELL))
205 return;
206
207
208 if (!cpu_has_feature(CPU_FTR_SMT))
209 return;
210
211 check_kvm_guest();
212
213 if (is_kvm_guest()) {
214
215
216
217
218
219 if (xive_enabled())
220 return;
221
222
223
224
225
226
227
228 if (is_secure_guest())
229 return;
230 }
231
232
233
234
235
236
237
238
239 ic_cause_ipi = smp_ops->cause_ipi;
240 smp_ops->cause_ipi = dbell_or_ic_cause_ipi;
241}
242
243static struct smp_ops_t pseries_smp_ops = {
244 .message_pass = NULL,
245 .cause_ipi = NULL,
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,
252};
253
254
255void __init smp_init_pseries(void)
256{
257 int i;
258
259 pr_debug(" -> smp_init_pSeries()\n");
260 smp_ops = &pseries_smp_ops;
261
262 alloc_bootmem_cpumask_var(&of_spin_mask);
263
264
265
266
267
268
269
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);
275 }
276 } else
277 cpumask_copy(of_spin_mask, cpu_present_mask);
278
279 cpumask_clear_cpu(boot_cpuid, of_spin_mask);
280 }
281
282
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;
286 }
287
288 pr_debug(" <- smp_init_pSeries()\n");
289}
290