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>
47 #define DBG(fmt...) udbg_printf(fmt)
52 static void pnv_smp_setup_cpu(int cpu
)
55 * P9 workaround for CI vector load (see traps.c),
56 * enable the corresponding HMI interrupt
58 if (pvr_version_is(PVR_POWER9
))
59 mtspr(SPRN_HMEER
, mfspr(SPRN_HMEER
) | PPC_BIT(17));
63 else if (cpu
!= boot_cpuid
)
67 static int pnv_smp_kick_cpu(int nr
)
70 unsigned long start_here
=
71 __pa(ppc_function_entry(generic_secondary_smp_init
));
75 if (nr
< 0 || nr
>= nr_cpu_ids
)
78 pcpu
= get_hard_smp_processor_id(nr
);
80 * If we already started or OPAL is not supported, we just
81 * kick the CPU via the PACA
83 if (paca
[nr
].cpu_start
|| !firmware_has_feature(FW_FEATURE_OPAL
))
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
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
);
99 * Already started, just kick it, probably coming from
102 if (status
== OPAL_THREAD_STARTED
)
106 * Available/inactive, let's kick it
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
);
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
122 pr_devel("OPAL: CPU %d (HW 0x%x) is unavailable"
123 " (status %d)...\n", nr
, pcpu
, status
);
128 return smp_generic_kick_cpu(nr
);
131 #ifdef CONFIG_HOTPLUG_CPU
133 static int pnv_smp_cpu_disable(void)
135 int cpu
= smp_processor_id();
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.
141 set_cpu_online(cpu
, false);
142 vdso_data
->processorCount
--;
143 if (cpu
== boot_cpuid
)
144 boot_cpuid
= cpumask_any(cpu_online_mask
);
146 xive_smp_disable_cpu();
148 xics_migrate_irqs_away();
152 static void pnv_smp_cpu_kill_self(void)
155 unsigned long srr1
, wmask
;
157 /* Standard hot unplug procedure */
159 * This hard disables local interurpts, ensuring we have no lazy
162 WARN_ON(irqs_disabled());
164 WARN_ON(lazy_irq_pending());
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
);
173 wmask
= SRR1_WAKEMASK
;
174 if (cpu_has_feature(CPU_FTR_ARCH_207S
))
175 wmask
= SRR1_WAKEMASK_P8
;
177 while (!generic_check_cpu_restart(cpu
)) {
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.
185 kvmppc_set_host_ipi(cpu
, 0);
187 srr1
= pnv_cpu_offline(cpu
);
189 WARN_ON(lazy_irq_pending());
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
202 if (((srr1
& wmask
) == SRR1_WAKEEE
) ||
203 ((srr1
& wmask
) == SRR1_WAKEHVI
)) {
204 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
206 xive_flush_interrupt();
208 icp_opal_flush_interrupt();
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 */
222 * For kdump kernels, we process the ipi and jump to
225 if (kdump_in_progress()) {
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
235 ppc_save_regs(®s
);
236 crash_ipi_callback(®s
);
237 /* Does not return */
240 if (cpu_core_split_required())
243 if (srr1
&& !generic_check_cpu_restart(cpu
))
244 DBG("CPU%d Unexpected exit while offline srr1=%lx!\n",
249 DBG("CPU%d coming online...\n", cpu
);
252 #endif /* CONFIG_HOTPLUG_CPU */
254 static int pnv_cpu_bootable(unsigned int nr
)
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).
262 if (cpu_has_feature(CPU_FTR_ARCH_207S
))
265 return smp_generic_cpu_bootable(nr
);
268 static int pnv_smp_prepare_cpu(int cpu
)
271 return xive_smp_prepare_cpu(cpu
);
275 /* Cause IPI as setup by the interrupt controller (xics or xive) */
276 static void (*ic_cause_ipi
)(int cpu
);
278 static void pnv_cause_ipi(int cpu
)
280 if (doorbell_try_core_ipi(cpu
))
286 static void pnv_p9_dd1_cause_ipi(int cpu
)
288 int this_cpu
= get_cpu();
291 * POWER9 DD1 has a global addressed msgsnd, but for now we restrict
292 * IPIs to same core, because it requires additional synchronization
293 * for inter-core doorbells which we do not implement.
295 if (cpumask_test_cpu(cpu
, cpu_sibling_mask(this_cpu
)))
296 doorbell_global_ipi(cpu
);
303 static void __init
pnv_smp_probe(void)
310 if (cpu_has_feature(CPU_FTR_DBELL
)) {
311 ic_cause_ipi
= smp_ops
->cause_ipi
;
312 WARN_ON(!ic_cause_ipi
);
314 if (cpu_has_feature(CPU_FTR_ARCH_300
)) {
315 if (cpu_has_feature(CPU_FTR_POWER9_DD1
))
316 smp_ops
->cause_ipi
= pnv_p9_dd1_cause_ipi
;
318 smp_ops
->cause_ipi
= doorbell_global_ipi
;
320 smp_ops
->cause_ipi
= pnv_cause_ipi
;
325 static int pnv_system_reset_exception(struct pt_regs
*regs
)
327 if (smp_handle_nmi_ipi(regs
))
332 static int pnv_cause_nmi_ipi(int cpu
)
337 rc
= opal_signal_system_reset(get_hard_smp_processor_id(cpu
));
338 if (rc
!= OPAL_SUCCESS
)
342 } else if (cpu
== NMI_IPI_ALL_OTHERS
) {
348 * We do not use broadcasts (yet), because it's not clear
349 * exactly what semantics Linux wants or the firmware should
352 for_each_online_cpu(c
) {
353 if (c
== smp_processor_id())
356 rc
= opal_signal_system_reset(
357 get_hard_smp_processor_id(c
));
358 if (rc
!= OPAL_SUCCESS
)
365 * Caller will fall back to doorbells, which may pick
373 static struct smp_ops_t pnv_smp_ops
= {
374 .message_pass
= NULL
, /* Use smp_muxed_ipi_message_pass */
375 .cause_ipi
= NULL
, /* Filled at runtime by pnv_smp_probe() */
376 .cause_nmi_ipi
= NULL
,
377 .probe
= pnv_smp_probe
,
378 .prepare_cpu
= pnv_smp_prepare_cpu
,
379 .kick_cpu
= pnv_smp_kick_cpu
,
380 .setup_cpu
= pnv_smp_setup_cpu
,
381 .cpu_bootable
= pnv_cpu_bootable
,
382 #ifdef CONFIG_HOTPLUG_CPU
383 .cpu_disable
= pnv_smp_cpu_disable
,
384 .cpu_die
= generic_cpu_die
,
385 #endif /* CONFIG_HOTPLUG_CPU */
388 /* This is called very early during platform setup_arch */
389 void __init
pnv_smp_init(void)
391 if (opal_check_token(OPAL_SIGNAL_SYSTEM_RESET
)) {
392 ppc_md
.system_reset_exception
= pnv_system_reset_exception
;
393 pnv_smp_ops
.cause_nmi_ipi
= pnv_cause_nmi_ipi
;
395 smp_ops
= &pnv_smp_ops
;
397 #ifdef CONFIG_HOTPLUG_CPU
398 ppc_md
.cpu_die
= pnv_smp_cpu_kill_self
;
399 #ifdef CONFIG_KEXEC_CORE
400 crash_wake_offline
= 1;