mm/memory_hotplug: make remove_memory() take the device_hotplug_lock
[sfrench/cifs-2.6.git] / arch / powerpc / platforms / powernv / smp.c
1 /*
2  * SMP support for PowerNV machines.
3  *
4  * Copyright 2011 IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/sched/hotplug.h>
16 #include <linux/smp.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/cpu.h>
22
23 #include <asm/irq.h>
24 #include <asm/smp.h>
25 #include <asm/paca.h>
26 #include <asm/machdep.h>
27 #include <asm/cputable.h>
28 #include <asm/firmware.h>
29 #include <asm/vdso_datapage.h>
30 #include <asm/cputhreads.h>
31 #include <asm/xics.h>
32 #include <asm/xive.h>
33 #include <asm/opal.h>
34 #include <asm/runlatch.h>
35 #include <asm/code-patching.h>
36 #include <asm/dbell.h>
37 #include <asm/kvm_ppc.h>
38 #include <asm/ppc-opcode.h>
39 #include <asm/cpuidle.h>
40 #include <asm/kexec.h>
41 #include <asm/reg.h>
42
43 #include "powernv.h"
44
45 #ifdef DEBUG
46 #include <asm/udbg.h>
47 #define DBG(fmt...) udbg_printf(fmt)
48 #else
49 #define DBG(fmt...)
50 #endif
51
52 static void pnv_smp_setup_cpu(int cpu)
53 {
54         /*
55          * P9 workaround for CI vector load (see traps.c),
56          * enable the corresponding HMI interrupt
57          */
58         if (pvr_version_is(PVR_POWER9))
59                 mtspr(SPRN_HMEER, mfspr(SPRN_HMEER) | PPC_BIT(17));
60
61         if (xive_enabled())
62                 xive_smp_setup_cpu();
63         else if (cpu != boot_cpuid)
64                 xics_setup_cpu();
65 }
66
67 static int pnv_smp_kick_cpu(int nr)
68 {
69         unsigned int pcpu;
70         unsigned long start_here =
71                         __pa(ppc_function_entry(generic_secondary_smp_init));
72         long rc;
73         uint8_t status;
74
75         if (nr < 0 || nr >= nr_cpu_ids)
76                 return -EINVAL;
77
78         pcpu = get_hard_smp_processor_id(nr);
79         /*
80          * If we already started or OPAL is not supported, we just
81          * kick the CPU via the PACA
82          */
83         if (paca_ptrs[nr]->cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
84                 goto kick;
85
86         /*
87          * At this point, the CPU can either be spinning on the way in
88          * from kexec or be inside OPAL waiting to be started for the
89          * first time. OPAL v3 allows us to query OPAL to know if it
90          * has the CPUs, so we do that
91          */
92         rc = opal_query_cpu_status(pcpu, &status);
93         if (rc != OPAL_SUCCESS) {
94                 pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
95                 return -ENODEV;
96         }
97
98         /*
99          * Already started, just kick it, probably coming from
100          * kexec and spinning
101          */
102         if (status == OPAL_THREAD_STARTED)
103                 goto kick;
104
105         /*
106          * Available/inactive, let's kick it
107          */
108         if (status == OPAL_THREAD_INACTIVE) {
109                 pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
110                 rc = opal_start_cpu(pcpu, start_here);
111                 if (rc != OPAL_SUCCESS) {
112                         pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
113                         return -ENODEV;
114                 }
115         } else {
116                 /*
117                  * An unavailable CPU (or any other unknown status)
118                  * shouldn't be started. It should also
119                  * not be in the possible map but currently it can
120                  * happen
121                  */
122                 pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
123                          " (status %d)...\n", nr, pcpu, status);
124                 return -ENODEV;
125         }
126
127 kick:
128         return smp_generic_kick_cpu(nr);
129 }
130
131 #ifdef CONFIG_HOTPLUG_CPU
132
133 static int pnv_smp_cpu_disable(void)
134 {
135         int cpu = smp_processor_id();
136
137         /* This is identical to pSeries... might consolidate by
138          * moving migrate_irqs_away to a ppc_md with default to
139          * the generic fixup_irqs. --BenH.
140          */
141         set_cpu_online(cpu, false);
142         vdso_data->processorCount--;
143         if (cpu == boot_cpuid)
144                 boot_cpuid = cpumask_any(cpu_online_mask);
145         if (xive_enabled())
146                 xive_smp_disable_cpu();
147         else
148                 xics_migrate_irqs_away();
149         return 0;
150 }
151
152 static void pnv_smp_cpu_kill_self(void)
153 {
154         unsigned int cpu;
155         unsigned long srr1, wmask;
156
157         /* Standard hot unplug procedure */
158         /*
159          * This hard disables local interurpts, ensuring we have no lazy
160          * irqs pending.
161          */
162         WARN_ON(irqs_disabled());
163         hard_irq_disable();
164         WARN_ON(lazy_irq_pending());
165
166         idle_task_exit();
167         current->active_mm = NULL; /* for sanity */
168         cpu = smp_processor_id();
169         DBG("CPU%d offline\n", cpu);
170         generic_set_cpu_dead(cpu);
171         smp_wmb();
172
173         wmask = SRR1_WAKEMASK;
174         if (cpu_has_feature(CPU_FTR_ARCH_207S))
175                 wmask = SRR1_WAKEMASK_P8;
176
177         while (!generic_check_cpu_restart(cpu)) {
178                 /*
179                  * Clear IPI flag, since we don't handle IPIs while
180                  * offline, except for those when changing micro-threading
181                  * mode, which are handled explicitly below, and those
182                  * for coming online, which are handled via
183                  * generic_check_cpu_restart() calls.
184                  */
185                 kvmppc_set_host_ipi(cpu, 0);
186
187                 srr1 = pnv_cpu_offline(cpu);
188
189                 WARN_ON(lazy_irq_pending());
190
191                 /*
192                  * If the SRR1 value indicates that we woke up due to
193                  * an external interrupt, then clear the interrupt.
194                  * We clear the interrupt before checking for the
195                  * reason, so as to avoid a race where we wake up for
196                  * some other reason, find nothing and clear the interrupt
197                  * just as some other cpu is sending us an interrupt.
198                  * If we returned from power7_nap as a result of
199                  * having finished executing in a KVM guest, then srr1
200                  * contains 0.
201                  */
202                 if (((srr1 & wmask) == SRR1_WAKEEE) ||
203                     ((srr1 & wmask) == SRR1_WAKEHVI)) {
204                         if (cpu_has_feature(CPU_FTR_ARCH_300)) {
205                                 if (xive_enabled())
206                                         xive_flush_interrupt();
207                                 else
208                                         icp_opal_flush_interrupt();
209                         } else
210                                 icp_native_flush_interrupt();
211                 } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
212                         unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
213                         asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
214                 } else if ((srr1 & wmask) == SRR1_WAKERESET) {
215                         irq_set_pending_from_srr1(srr1);
216                         /* Does not return */
217                 }
218
219                 smp_mb();
220
221                 /*
222                  * For kdump kernels, we process the ipi and jump to
223                  * crash_ipi_callback
224                  */
225                 if (kdump_in_progress()) {
226                         /*
227                          * If we got to this point, we've not used
228                          * NMI's, otherwise we would have gone
229                          * via the SRR1_WAKERESET path. We are
230                          * using regular IPI's for waking up offline
231                          * threads.
232                          */
233                         struct pt_regs regs;
234
235                         ppc_save_regs(&regs);
236                         crash_ipi_callback(&regs);
237                         /* Does not return */
238                 }
239
240                 if (cpu_core_split_required())
241                         continue;
242
243                 if (srr1 && !generic_check_cpu_restart(cpu))
244                         DBG("CPU%d Unexpected exit while offline srr1=%lx!\n",
245                                         cpu, srr1);
246
247         }
248
249         DBG("CPU%d coming online...\n", cpu);
250 }
251
252 #endif /* CONFIG_HOTPLUG_CPU */
253
254 static int pnv_cpu_bootable(unsigned int nr)
255 {
256         /*
257          * Starting with POWER8, the subcore logic relies on all threads of a
258          * core being booted so that they can participate in split mode
259          * switches. So on those machines we ignore the smt_enabled_at_boot
260          * setting (smt-enabled on the kernel command line).
261          */
262         if (cpu_has_feature(CPU_FTR_ARCH_207S))
263                 return 1;
264
265         return smp_generic_cpu_bootable(nr);
266 }
267
268 static int pnv_smp_prepare_cpu(int cpu)
269 {
270         if (xive_enabled())
271                 return xive_smp_prepare_cpu(cpu);
272         return 0;
273 }
274
275 /* Cause IPI as setup by the interrupt controller (xics or xive) */
276 static void (*ic_cause_ipi)(int cpu);
277
278 static void pnv_cause_ipi(int cpu)
279 {
280         if (doorbell_try_core_ipi(cpu))
281                 return;
282
283         ic_cause_ipi(cpu);
284 }
285
286 static void __init pnv_smp_probe(void)
287 {
288         if (xive_enabled())
289                 xive_smp_probe();
290         else
291                 xics_smp_probe();
292
293         if (cpu_has_feature(CPU_FTR_DBELL)) {
294                 ic_cause_ipi = smp_ops->cause_ipi;
295                 WARN_ON(!ic_cause_ipi);
296
297                 if (cpu_has_feature(CPU_FTR_ARCH_300))
298                         smp_ops->cause_ipi = doorbell_global_ipi;
299                 else
300                         smp_ops->cause_ipi = pnv_cause_ipi;
301         }
302 }
303
304 static int pnv_system_reset_exception(struct pt_regs *regs)
305 {
306         if (smp_handle_nmi_ipi(regs))
307                 return 1;
308         return 0;
309 }
310
311 static int pnv_cause_nmi_ipi(int cpu)
312 {
313         int64_t rc;
314
315         if (cpu >= 0) {
316                 int h = get_hard_smp_processor_id(cpu);
317
318                 if (opal_check_token(OPAL_QUIESCE))
319                         opal_quiesce(QUIESCE_HOLD, h);
320
321                 rc = opal_signal_system_reset(h);
322
323                 if (opal_check_token(OPAL_QUIESCE))
324                         opal_quiesce(QUIESCE_RESUME, h);
325
326                 if (rc != OPAL_SUCCESS)
327                         return 0;
328                 return 1;
329
330         } else if (cpu == NMI_IPI_ALL_OTHERS) {
331                 bool success = true;
332                 int c;
333
334                 if (opal_check_token(OPAL_QUIESCE))
335                         opal_quiesce(QUIESCE_HOLD, -1);
336
337                 /*
338                  * We do not use broadcasts (yet), because it's not clear
339                  * exactly what semantics Linux wants or the firmware should
340                  * provide.
341                  */
342                 for_each_online_cpu(c) {
343                         if (c == smp_processor_id())
344                                 continue;
345
346                         rc = opal_signal_system_reset(
347                                                 get_hard_smp_processor_id(c));
348                         if (rc != OPAL_SUCCESS)
349                                 success = false;
350                 }
351
352                 if (opal_check_token(OPAL_QUIESCE))
353                         opal_quiesce(QUIESCE_RESUME, -1);
354
355                 if (success)
356                         return 1;
357
358                 /*
359                  * Caller will fall back to doorbells, which may pick
360                  * up the remainders.
361                  */
362         }
363
364         return 0;
365 }
366
367 static struct smp_ops_t pnv_smp_ops = {
368         .message_pass   = NULL, /* Use smp_muxed_ipi_message_pass */
369         .cause_ipi      = NULL, /* Filled at runtime by pnv_smp_probe() */
370         .cause_nmi_ipi  = NULL,
371         .probe          = pnv_smp_probe,
372         .prepare_cpu    = pnv_smp_prepare_cpu,
373         .kick_cpu       = pnv_smp_kick_cpu,
374         .setup_cpu      = pnv_smp_setup_cpu,
375         .cpu_bootable   = pnv_cpu_bootable,
376 #ifdef CONFIG_HOTPLUG_CPU
377         .cpu_disable    = pnv_smp_cpu_disable,
378         .cpu_die        = generic_cpu_die,
379 #endif /* CONFIG_HOTPLUG_CPU */
380 };
381
382 /* This is called very early during platform setup_arch */
383 void __init pnv_smp_init(void)
384 {
385         if (opal_check_token(OPAL_SIGNAL_SYSTEM_RESET)) {
386                 ppc_md.system_reset_exception = pnv_system_reset_exception;
387                 pnv_smp_ops.cause_nmi_ipi = pnv_cause_nmi_ipi;
388         }
389         smp_ops = &pnv_smp_ops;
390
391 #ifdef CONFIG_HOTPLUG_CPU
392         ppc_md.cpu_die  = pnv_smp_cpu_kill_self;
393 #ifdef CONFIG_KEXEC_CORE
394         crash_wake_offline = 1;
395 #endif
396 #endif
397 }