2 * SMP support for PowerNV machines.
4 * Copyright 2011 IBM Corp.
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.
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>
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>
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>
42 #include <asm/powernv.h>
48 #define DBG(fmt...) udbg_printf(fmt)
50 #define DBG(fmt...) do { } while (0)
53 static void pnv_smp_setup_cpu(int cpu)
56 * P9 workaround for CI vector load (see traps.c),
57 * enable the corresponding HMI interrupt
59 if (pvr_version_is(PVR_POWER9))
60 mtspr(SPRN_HMEER, mfspr(SPRN_HMEER) | PPC_BIT(17));
64 else if (cpu != boot_cpuid)
68 static int pnv_smp_kick_cpu(int nr)
71 unsigned long start_here =
72 __pa(ppc_function_entry(generic_secondary_smp_init));
76 if (nr < 0 || nr >= nr_cpu_ids)
79 pcpu = get_hard_smp_processor_id(nr);
81 * If we already started or OPAL is not supported, we just
82 * kick the CPU via the PACA
84 if (paca_ptrs[nr]->cpu_start || !firmware_has_feature(FW_FEATURE_OPAL))
88 * At this point, the CPU can either be spinning on the way in
89 * from kexec or be inside OPAL waiting to be started for the
90 * first time. OPAL v3 allows us to query OPAL to know if it
91 * has the CPUs, so we do that
93 rc = opal_query_cpu_status(pcpu, &status);
94 if (rc != OPAL_SUCCESS) {
95 pr_warn("OPAL Error %ld querying CPU %d state\n", rc, nr);
100 * Already started, just kick it, probably coming from
103 if (status == OPAL_THREAD_STARTED)
107 * Available/inactive, let's kick it
109 if (status == OPAL_THREAD_INACTIVE) {
110 pr_devel("OPAL: Starting CPU %d (HW 0x%x)...\n", nr, pcpu);
111 rc = opal_start_cpu(pcpu, start_here);
112 if (rc != OPAL_SUCCESS) {
113 pr_warn("OPAL Error %ld starting CPU %d\n", rc, nr);
118 * An unavailable CPU (or any other unknown status)
119 * shouldn't be started. It should also
120 * not be in the possible map but currently it can
123 pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
124 " (status %d)...\n", nr, pcpu, status);
129 return smp_generic_kick_cpu(nr);
132 #ifdef CONFIG_HOTPLUG_CPU
134 static int pnv_smp_cpu_disable(void)
136 int cpu = smp_processor_id();
138 /* This is identical to pSeries... might consolidate by
139 * moving migrate_irqs_away to a ppc_md with default to
140 * the generic fixup_irqs. --BenH.
142 set_cpu_online(cpu, false);
143 vdso_data->processorCount--;
144 if (cpu == boot_cpuid)
145 boot_cpuid = cpumask_any(cpu_online_mask);
147 xive_smp_disable_cpu();
149 xics_migrate_irqs_away();
153 static void pnv_flush_interrupts(void)
155 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
157 xive_flush_interrupt();
159 icp_opal_flush_interrupt();
161 icp_native_flush_interrupt();
165 static void pnv_smp_cpu_kill_self(void)
167 unsigned long srr1, unexpected_mask, wmask;
171 /* Standard hot unplug procedure */
174 cpu = smp_processor_id();
175 DBG("CPU%d offline\n", cpu);
176 generic_set_cpu_dead(cpu);
179 wmask = SRR1_WAKEMASK;
180 if (cpu_has_feature(CPU_FTR_ARCH_207S))
181 wmask = SRR1_WAKEMASK_P8;
184 * This turns the irq soft-disabled state we're called with, into a
185 * hard-disabled state with pending irq_happened interrupts cleared.
187 * PACA_IRQ_DEC - Decrementer should be ignored.
188 * PACA_IRQ_HMI - Can be ignored, processing is done in real mode.
189 * PACA_IRQ_DBELL, EE, PMI - Unexpected.
192 if (generic_check_cpu_restart(cpu))
195 unexpected_mask = ~(PACA_IRQ_DEC | PACA_IRQ_HMI | PACA_IRQ_HARD_DIS);
196 if (local_paca->irq_happened & unexpected_mask) {
197 if (local_paca->irq_happened & PACA_IRQ_EE)
198 pnv_flush_interrupts();
199 DBG("CPU%d Unexpected exit while offline irq_happened=%lx!\n",
200 cpu, local_paca->irq_happened);
202 local_paca->irq_happened = PACA_IRQ_HARD_DIS;
205 * We don't want to take decrementer interrupts while we are
206 * offline, so clear LPCR:PECE1. We keep PECE2 (and
207 * LPCR_PECE_HVEE on P9) enabled so as to let IPIs in.
209 * If the CPU gets woken up by a special wakeup, ensure that
210 * the SLW engine sets LPCR with decrementer bit cleared, else
211 * the CPU will come back to the kernel due to a spurious
214 lpcr_val = mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1;
215 pnv_program_cpu_hotplug_lpcr(cpu, lpcr_val);
217 while (!generic_check_cpu_restart(cpu)) {
219 * Clear IPI flag, since we don't handle IPIs while
220 * offline, except for those when changing micro-threading
221 * mode, which are handled explicitly below, and those
222 * for coming online, which are handled via
223 * generic_check_cpu_restart() calls.
225 kvmppc_clear_host_ipi(cpu);
227 srr1 = pnv_cpu_offline(cpu);
229 WARN_ON_ONCE(!irqs_disabled());
230 WARN_ON(lazy_irq_pending());
233 * If the SRR1 value indicates that we woke up due to
234 * an external interrupt, then clear the interrupt.
235 * We clear the interrupt before checking for the
236 * reason, so as to avoid a race where we wake up for
237 * some other reason, find nothing and clear the interrupt
238 * just as some other cpu is sending us an interrupt.
239 * If we returned from power7_nap as a result of
240 * having finished executing in a KVM guest, then srr1
243 if (((srr1 & wmask) == SRR1_WAKEEE) ||
244 ((srr1 & wmask) == SRR1_WAKEHVI)) {
245 pnv_flush_interrupts();
246 } else if ((srr1 & wmask) == SRR1_WAKEHDBELL) {
247 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
248 asm volatile(PPC_MSGCLR(%0) : : "r" (msg));
249 } else if ((srr1 & wmask) == SRR1_WAKERESET) {
250 irq_set_pending_from_srr1(srr1);
251 /* Does not return */
257 * For kdump kernels, we process the ipi and jump to
260 if (kdump_in_progress()) {
262 * If we got to this point, we've not used
263 * NMI's, otherwise we would have gone
264 * via the SRR1_WAKERESET path. We are
265 * using regular IPI's for waking up offline
270 ppc_save_regs(®s);
271 crash_ipi_callback(®s);
272 /* Does not return */
275 if (cpu_core_split_required())
278 if (srr1 && !generic_check_cpu_restart(cpu))
279 DBG("CPU%d Unexpected exit while offline srr1=%lx!\n",
285 * Re-enable decrementer interrupts in LPCR.
287 * Further, we want stop states to be woken up by decrementer
288 * for non-hotplug cases. So program the LPCR via stop api as
291 lpcr_val = mfspr(SPRN_LPCR) | (u64)LPCR_PECE1;
292 pnv_program_cpu_hotplug_lpcr(cpu, lpcr_val);
294 DBG("CPU%d coming online...\n", cpu);
297 #endif /* CONFIG_HOTPLUG_CPU */
299 static int pnv_cpu_bootable(unsigned int nr)
302 * Starting with POWER8, the subcore logic relies on all threads of a
303 * core being booted so that they can participate in split mode
304 * switches. So on those machines we ignore the smt_enabled_at_boot
305 * setting (smt-enabled on the kernel command line).
307 if (cpu_has_feature(CPU_FTR_ARCH_207S))
310 return smp_generic_cpu_bootable(nr);
313 static int pnv_smp_prepare_cpu(int cpu)
316 return xive_smp_prepare_cpu(cpu);
320 /* Cause IPI as setup by the interrupt controller (xics or xive) */
321 static void (*ic_cause_ipi)(int cpu);
323 static void pnv_cause_ipi(int cpu)
325 if (doorbell_try_core_ipi(cpu))
331 static void __init pnv_smp_probe(void)
338 if (cpu_has_feature(CPU_FTR_DBELL)) {
339 ic_cause_ipi = smp_ops->cause_ipi;
340 WARN_ON(!ic_cause_ipi);
342 if (cpu_has_feature(CPU_FTR_ARCH_300))
343 smp_ops->cause_ipi = doorbell_global_ipi;
345 smp_ops->cause_ipi = pnv_cause_ipi;
349 static int pnv_system_reset_exception(struct pt_regs *regs)
351 if (smp_handle_nmi_ipi(regs))
356 static int pnv_cause_nmi_ipi(int cpu)
361 int h = get_hard_smp_processor_id(cpu);
363 if (opal_check_token(OPAL_QUIESCE))
364 opal_quiesce(QUIESCE_HOLD, h);
366 rc = opal_signal_system_reset(h);
368 if (opal_check_token(OPAL_QUIESCE))
369 opal_quiesce(QUIESCE_RESUME, h);
371 if (rc != OPAL_SUCCESS)
375 } else if (cpu == NMI_IPI_ALL_OTHERS) {
379 if (opal_check_token(OPAL_QUIESCE))
380 opal_quiesce(QUIESCE_HOLD, -1);
383 * We do not use broadcasts (yet), because it's not clear
384 * exactly what semantics Linux wants or the firmware should
387 for_each_online_cpu(c) {
388 if (c == smp_processor_id())
391 rc = opal_signal_system_reset(
392 get_hard_smp_processor_id(c));
393 if (rc != OPAL_SUCCESS)
397 if (opal_check_token(OPAL_QUIESCE))
398 opal_quiesce(QUIESCE_RESUME, -1);
404 * Caller will fall back to doorbells, which may pick
412 static struct smp_ops_t pnv_smp_ops = {
413 .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
414 .cause_ipi = NULL, /* Filled at runtime by pnv_smp_probe() */
415 .cause_nmi_ipi = NULL,
416 .probe = pnv_smp_probe,
417 .prepare_cpu = pnv_smp_prepare_cpu,
418 .kick_cpu = pnv_smp_kick_cpu,
419 .setup_cpu = pnv_smp_setup_cpu,
420 .cpu_bootable = pnv_cpu_bootable,
421 #ifdef CONFIG_HOTPLUG_CPU
422 .cpu_disable = pnv_smp_cpu_disable,
423 .cpu_die = generic_cpu_die,
424 #endif /* CONFIG_HOTPLUG_CPU */
427 /* This is called very early during platform setup_arch */
428 void __init pnv_smp_init(void)
430 if (opal_check_token(OPAL_SIGNAL_SYSTEM_RESET)) {
431 ppc_md.system_reset_exception = pnv_system_reset_exception;
432 pnv_smp_ops.cause_nmi_ipi = pnv_cause_nmi_ipi;
434 smp_ops = &pnv_smp_ops;
436 #ifdef CONFIG_HOTPLUG_CPU
437 ppc_md.cpu_die = pnv_smp_cpu_kill_self;
438 #ifdef CONFIG_KEXEC_CORE
439 crash_wake_offline = 1;