xref: /openbmc/linux/arch/x86/kernel/smp.c (revision fbe1bf1e5ff1e3b298420d7a8434983ef8d72bd1)
19ff554e9SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20941ecb5SGlauber Costa /*
30941ecb5SGlauber Costa  *	Intel SMP support routines.
40941ecb5SGlauber Costa  *
587c6fe26SAlan Cox  *	(c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
68f47e163SIngo Molnar  *	(c) 1998-99, 2000, 2009 Ingo Molnar <mingo@redhat.com>
70941ecb5SGlauber Costa  *      (c) 2002,2003 Andi Kleen, SuSE Labs.
80941ecb5SGlauber Costa  *
90941ecb5SGlauber Costa  *	i386 and x86_64 integration by Glauber Costa <gcosta@redhat.com>
100941ecb5SGlauber Costa  */
110941ecb5SGlauber Costa 
12f9e47a12SGlauber Costa #include <linux/init.h>
13f9e47a12SGlauber Costa 
14f9e47a12SGlauber Costa #include <linux/mm.h>
15f9e47a12SGlauber Costa #include <linux/delay.h>
16f9e47a12SGlauber Costa #include <linux/spinlock.h>
1769c60c88SPaul Gortmaker #include <linux/export.h>
18f9e47a12SGlauber Costa #include <linux/kernel_stat.h>
19f9e47a12SGlauber Costa #include <linux/mc146818rtc.h>
20f9e47a12SGlauber Costa #include <linux/cache.h>
21f9e47a12SGlauber Costa #include <linux/interrupt.h>
22f9e47a12SGlauber Costa #include <linux/cpu.h>
235a0e3ad6STejun Heo #include <linux/gfp.h>
24d7893093SThomas Gleixner #include <linux/kexec.h>
25f9e47a12SGlauber Costa 
26f9e47a12SGlauber Costa #include <asm/mtrr.h>
27f9e47a12SGlauber Costa #include <asm/tlbflush.h>
28f9e47a12SGlauber Costa #include <asm/mmu_context.h>
29f9e47a12SGlauber Costa #include <asm/proto.h>
307b6aa335SIngo Molnar #include <asm/apic.h>
311f5e7eb7SThomas Gleixner #include <asm/cpu.h>
32582f9191SThomas Gleixner #include <asm/idtentry.h>
337d007d21SDon Zickus #include <asm/nmi.h>
348838eb6cSAshok Raj #include <asm/mce.h>
35cf910e83SSeiji Aguchi #include <asm/trace/irq_vectors.h>
360ee59413SHidehiro Kawai #include <asm/kexec.h>
37a2b07fa7SSean Christopherson #include <asm/reboot.h>
380ee59413SHidehiro Kawai 
390941ecb5SGlauber Costa /*
400941ecb5SGlauber Costa  *	Some notes on x86 processor bugs affecting SMP operation:
410941ecb5SGlauber Costa  *
420941ecb5SGlauber Costa  *	Pentium, Pentium Pro, II, III (and all CPUs) have bugs.
430941ecb5SGlauber Costa  *	The Linux implications for SMP are handled as follows:
440941ecb5SGlauber Costa  *
450941ecb5SGlauber Costa  *	Pentium III / [Xeon]
460941ecb5SGlauber Costa  *		None of the E1AP-E3AP errata are visible to the user.
470941ecb5SGlauber Costa  *
480941ecb5SGlauber Costa  *	E1AP.	see PII A1AP
490941ecb5SGlauber Costa  *	E2AP.	see PII A2AP
500941ecb5SGlauber Costa  *	E3AP.	see PII A3AP
510941ecb5SGlauber Costa  *
520941ecb5SGlauber Costa  *	Pentium II / [Xeon]
530941ecb5SGlauber Costa  *		None of the A1AP-A3AP errata are visible to the user.
540941ecb5SGlauber Costa  *
550941ecb5SGlauber Costa  *	A1AP.	see PPro 1AP
560941ecb5SGlauber Costa  *	A2AP.	see PPro 2AP
570941ecb5SGlauber Costa  *	A3AP.	see PPro 7AP
580941ecb5SGlauber Costa  *
590941ecb5SGlauber Costa  *	Pentium Pro
600941ecb5SGlauber Costa  *		None of 1AP-9AP errata are visible to the normal user,
610941ecb5SGlauber Costa  *	except occasional delivery of 'spurious interrupt' as trap #15.
620941ecb5SGlauber Costa  *	This is very rare and a non-problem.
630941ecb5SGlauber Costa  *
640941ecb5SGlauber Costa  *	1AP.	Linux maps APIC as non-cacheable
650941ecb5SGlauber Costa  *	2AP.	worked around in hardware
660941ecb5SGlauber Costa  *	3AP.	fixed in C0 and above steppings microcode update.
670941ecb5SGlauber Costa  *		Linux does not use excessive STARTUP_IPIs.
680941ecb5SGlauber Costa  *	4AP.	worked around in hardware
690941ecb5SGlauber Costa  *	5AP.	symmetric IO mode (normal Linux operation) not affected.
700941ecb5SGlauber Costa  *		'noapic' mode has vector 0xf filled out properly.
710941ecb5SGlauber Costa  *	6AP.	'noapic' mode might be affected - fixed in later steppings
72d9f6e12fSIngo Molnar  *	7AP.	We do not assume writes to the LVT deasserting IRQs
730941ecb5SGlauber Costa  *	8AP.	We do not enable low power mode (deep sleep) during MP bootup
740941ecb5SGlauber Costa  *	9AP.	We do not use mixed mode
750941ecb5SGlauber Costa  *
760941ecb5SGlauber Costa  *	Pentium
770941ecb5SGlauber Costa  *		There is a marginal case where REP MOVS on 100MHz SMP
780941ecb5SGlauber Costa  *	machines with B stepping processors can fail. XXX should provide
790941ecb5SGlauber Costa  *	an L1cache=Writethrough or L1cache=off option.
800941ecb5SGlauber Costa  *
810941ecb5SGlauber Costa  *		B stepping CPUs may hang. There are hardware work arounds
820941ecb5SGlauber Costa  *	for this. We warn about it in case your board doesn't have the work
830941ecb5SGlauber Costa  *	arounds. Basically that's so I can tell anyone with a B stepping
840941ecb5SGlauber Costa  *	CPU and SMP problems "tough".
850941ecb5SGlauber Costa  *
860941ecb5SGlauber Costa  *	Specific items [From Pentium Processor Specification Update]
870941ecb5SGlauber Costa  *
880941ecb5SGlauber Costa  *	1AP.	Linux doesn't use remote read
890941ecb5SGlauber Costa  *	2AP.	Linux doesn't trust APIC errors
900941ecb5SGlauber Costa  *	3AP.	We work around this
910941ecb5SGlauber Costa  *	4AP.	Linux never generated 3 interrupts of the same priority
920941ecb5SGlauber Costa  *		to cause a lost local interrupt.
930941ecb5SGlauber Costa  *	5AP.	Remote read is never used
940941ecb5SGlauber Costa  *	6AP.	not affected - worked around in hardware
950941ecb5SGlauber Costa  *	7AP.	not affected - worked around in hardware
960941ecb5SGlauber Costa  *	8AP.	worked around in hardware - we get explicit CS errors if not
970941ecb5SGlauber Costa  *	9AP.	only 'noapic' mode affected. Might generate spurious
980941ecb5SGlauber Costa  *		interrupts, we log only the first one and count the
990941ecb5SGlauber Costa  *		rest silently.
1000941ecb5SGlauber Costa  *	10AP.	not affected - worked around in hardware
1010941ecb5SGlauber Costa  *	11AP.	Linux reads the APIC between writes to avoid this, as per
1020941ecb5SGlauber Costa  *		the documentation. Make sure you preserve this as it affects
1030941ecb5SGlauber Costa  *		the C stepping chips too.
1040941ecb5SGlauber Costa  *	12AP.	not affected - worked around in hardware
1050941ecb5SGlauber Costa  *	13AP.	not affected - worked around in hardware
1060941ecb5SGlauber Costa  *	14AP.	we always deassert INIT during bootup
1070941ecb5SGlauber Costa  *	15AP.	not affected - worked around in hardware
1080941ecb5SGlauber Costa  *	16AP.	not affected - worked around in hardware
1090941ecb5SGlauber Costa  *	17AP.	not affected - worked around in hardware
1100941ecb5SGlauber Costa  *	18AP.	not affected - worked around in hardware
1110941ecb5SGlauber Costa  *	19AP.	not affected - worked around in BIOS
1120941ecb5SGlauber Costa  *
1130941ecb5SGlauber Costa  *	If this sounds worrying believe me these bugs are either ___RARE___,
1140941ecb5SGlauber Costa  *	or are signal timing bugs worked around in hardware and there's
1150941ecb5SGlauber Costa  *	about nothing of note with C stepping upwards.
1160941ecb5SGlauber Costa  */
117f9e47a12SGlauber Costa 
1187d007d21SDon Zickus static atomic_t stopping_cpu = ATOMIC_INIT(-1);
1193aac27abSDon Zickus static bool smp_no_nmi_ipi = false;
1207d007d21SDon Zickus 
smp_stop_nmi_callback(unsigned int val,struct pt_regs * regs)1217d007d21SDon Zickus static int smp_stop_nmi_callback(unsigned int val, struct pt_regs *regs)
1227d007d21SDon Zickus {
1237d007d21SDon Zickus 	/* We are registered on stopping cpu too, avoid spurious NMI */
1247d007d21SDon Zickus 	if (raw_smp_processor_id() == atomic_read(&stopping_cpu))
1257d007d21SDon Zickus 		return NMI_HANDLED;
1267d007d21SDon Zickus 
127a2b07fa7SSean Christopherson 	cpu_emergency_disable_virtualization();
1287d007d21SDon Zickus 	stop_this_cpu(NULL);
1297d007d21SDon Zickus 
1307d007d21SDon Zickus 	return NMI_HANDLED;
1317d007d21SDon Zickus }
1327d007d21SDon Zickus 
133f9e47a12SGlauber Costa /*
134*fbe1bf1eSLinus Torvalds  * this function calls the 'stop' function on all other CPUs in the system.
135f9e47a12SGlauber Costa  */
DEFINE_IDTENTRY_SYSVEC(sysvec_reboot)136582f9191SThomas Gleixner DEFINE_IDTENTRY_SYSVEC(sysvec_reboot)
1374ef702c1SAndi Kleen {
138670c04adSDave Hansen 	apic_eoi();
139a2b07fa7SSean Christopherson 	cpu_emergency_disable_virtualization();
1404ef702c1SAndi Kleen 	stop_this_cpu(NULL);
1414ef702c1SAndi Kleen }
1424ef702c1SAndi Kleen 
register_stop_handler(void)143747d5a1bSGrzegorz Halat static int register_stop_handler(void)
144747d5a1bSGrzegorz Halat {
145747d5a1bSGrzegorz Halat 	return register_nmi_handler(NMI_LOCAL, smp_stop_nmi_callback,
146747d5a1bSGrzegorz Halat 				    NMI_FLAG_FIRST, "smp_stop");
147747d5a1bSGrzegorz Halat }
148747d5a1bSGrzegorz Halat 
native_stop_other_cpus(int wait)1495d2b86d9SDon Zickus static void native_stop_other_cpus(int wait)
150f9e47a12SGlauber Costa {
1511f5e7eb7SThomas Gleixner 	unsigned int cpu = smp_processor_id();
1521f5e7eb7SThomas Gleixner 	unsigned long flags, timeout;
153f9e47a12SGlauber Costa 
154f9e47a12SGlauber Costa 	if (reboot_force)
155f9e47a12SGlauber Costa 		return;
156f9e47a12SGlauber Costa 
1571f5e7eb7SThomas Gleixner 	/* Only proceed if this is the first CPU to reach this code */
1581f5e7eb7SThomas Gleixner 	if (atomic_cmpxchg(&stopping_cpu, -1, cpu) != -1)
1597d007d21SDon Zickus 		return;
1607d007d21SDon Zickus 
161d7893093SThomas Gleixner 	/* For kexec, ensure that offline CPUs are out of MWAIT and in HLT */
162d7893093SThomas Gleixner 	if (kexec_in_progress)
163d7893093SThomas Gleixner 		smp_kick_mwait_play_dead();
1647d007d21SDon Zickus 
1651f5e7eb7SThomas Gleixner 	/*
1661f5e7eb7SThomas Gleixner 	 * 1) Send an IPI on the reboot vector to all other CPUs.
1671f5e7eb7SThomas Gleixner 	 *
1681f5e7eb7SThomas Gleixner 	 *    The other CPUs should react on it after leaving critical
1691f5e7eb7SThomas Gleixner 	 *    sections and re-enabling interrupts. They might still hold
1701f5e7eb7SThomas Gleixner 	 *    locks, but there is nothing which can be done about that.
1711f5e7eb7SThomas Gleixner 	 *
1721f5e7eb7SThomas Gleixner 	 * 2) Wait for all other CPUs to report that they reached the
1731f5e7eb7SThomas Gleixner 	 *    HLT loop in stop_this_cpu()
1741f5e7eb7SThomas Gleixner 	 *
175*fbe1bf1eSLinus Torvalds 	 * 3) If #2 timed out send an NMI to the CPUs which did not
176*fbe1bf1eSLinus Torvalds 	 *    yet report
1771f5e7eb7SThomas Gleixner 	 *
178*fbe1bf1eSLinus Torvalds 	 * 4) Wait for all other CPUs to report that they reached the
1791f5e7eb7SThomas Gleixner 	 *    HLT loop in stop_this_cpu()
1801f5e7eb7SThomas Gleixner 	 *
181*fbe1bf1eSLinus Torvalds 	 * #3 can obviously race against a CPU reaching the HLT loop late.
1821f5e7eb7SThomas Gleixner 	 * That CPU will have reported already and the "have all CPUs
1831f5e7eb7SThomas Gleixner 	 * reached HLT" condition will be true despite the fact that the
1841f5e7eb7SThomas Gleixner 	 * other CPU is still handling the NMI. Again, there is no
1851f5e7eb7SThomas Gleixner 	 * protection against that as "disabled" APICs still respond to
1861f5e7eb7SThomas Gleixner 	 * NMIs.
1871f5e7eb7SThomas Gleixner 	 */
1881f5e7eb7SThomas Gleixner 	cpumask_copy(&cpus_stop_mask, cpu_online_mask);
1891f5e7eb7SThomas Gleixner 	cpumask_clear_cpu(cpu, &cpus_stop_mask);
1901f5e7eb7SThomas Gleixner 
1911f5e7eb7SThomas Gleixner 	if (!cpumask_empty(&cpus_stop_mask)) {
19222ca7ee9SThomas Gleixner 		apic_send_IPI_allbutself(REBOOT_VECTOR);
1934ef702c1SAndi Kleen 
19476fac077SAlok Kataria 		/*
195747d5a1bSGrzegorz Halat 		 * Don't wait longer than a second for IPI completion. The
196747d5a1bSGrzegorz Halat 		 * wait request is not checked here because that would
197*fbe1bf1eSLinus Torvalds 		 * prevent an NMI shutdown attempt in case that not all
198747d5a1bSGrzegorz Halat 		 * CPUs reach shutdown state.
19976fac077SAlok Kataria 		 */
20076fac077SAlok Kataria 		timeout = USEC_PER_SEC;
2011f5e7eb7SThomas Gleixner 		while (!cpumask_empty(&cpus_stop_mask) && timeout--)
2024ef702c1SAndi Kleen 			udelay(1);
2034ef702c1SAndi Kleen 	}
2044ef702c1SAndi Kleen 
205*fbe1bf1eSLinus Torvalds 	/* if the REBOOT_VECTOR didn't work, try with the NMI */
2061f5e7eb7SThomas Gleixner 	if (!cpumask_empty(&cpus_stop_mask)) {
207747d5a1bSGrzegorz Halat 		/*
208747d5a1bSGrzegorz Halat 		 * If NMI IPI is enabled, try to register the stop handler
209747d5a1bSGrzegorz Halat 		 * and send the IPI. In any case try to wait for the other
210747d5a1bSGrzegorz Halat 		 * CPUs to stop.
211747d5a1bSGrzegorz Halat 		 */
212747d5a1bSGrzegorz Halat 		if (!smp_no_nmi_ipi && !register_stop_handler()) {
2137d007d21SDon Zickus 			pr_emerg("Shutting down cpus with NMI\n");
2147d007d21SDon Zickus 
2151f5e7eb7SThomas Gleixner 			for_each_cpu(cpu, &cpus_stop_mask)
21628b82352SDave Hansen 				__apic_send_IPI(cpu, NMI_VECTOR);
217747d5a1bSGrzegorz Halat 		}
2187d007d21SDon Zickus 		/*
219747d5a1bSGrzegorz Halat 		 * Don't wait longer than 10 ms if the caller didn't
220163b0991SIngo Molnar 		 * request it. If wait is true, the machine hangs here if
221747d5a1bSGrzegorz Halat 		 * one or more CPUs do not reach shutdown state.
2227d007d21SDon Zickus 		 */
2237d007d21SDon Zickus 		timeout = USEC_PER_MSEC * 10;
2241f5e7eb7SThomas Gleixner 		while (!cpumask_empty(&cpus_stop_mask) && (wait || timeout--))
2257d007d21SDon Zickus 			udelay(1);
2267d007d21SDon Zickus 	}
2277d007d21SDon Zickus 
228f9e47a12SGlauber Costa 	local_irq_save(flags);
229f9e47a12SGlauber Costa 	disable_local_APIC();
2308838eb6cSAshok Raj 	mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
231f9e47a12SGlauber Costa 	local_irq_restore(flags);
2321f5e7eb7SThomas Gleixner 
2331f5e7eb7SThomas Gleixner 	/*
2341f5e7eb7SThomas Gleixner 	 * Ensure that the cpus_stop_mask cache lines are invalidated on
2351f5e7eb7SThomas Gleixner 	 * the other CPUs. See comment vs. SME in stop_this_cpu().
2361f5e7eb7SThomas Gleixner 	 */
2371f5e7eb7SThomas Gleixner 	cpumask_clear(&cpus_stop_mask);
238f9e47a12SGlauber Costa }
239f9e47a12SGlauber Costa 
240f9e47a12SGlauber Costa /*
2413cd788c1SThomas Gleixner  * Reschedule call back. KVM uses this interrupt to force a cpu out of
24213cad985SThomas Gleixner  * guest mode.
243f9e47a12SGlauber Costa  */
DEFINE_IDTENTRY_SYSVEC_SIMPLE(sysvec_reschedule_ipi)24413cad985SThomas Gleixner DEFINE_IDTENTRY_SYSVEC_SIMPLE(sysvec_reschedule_ipi)
245f9e47a12SGlauber Costa {
246670c04adSDave Hansen 	apic_eoi();
2473cd788c1SThomas Gleixner 	trace_reschedule_entry(RESCHEDULE_VECTOR);
24813cad985SThomas Gleixner 	inc_irq_stat(irq_resched_count);
24985b77cddSThomas Gleixner 	scheduler_ipi();
2504787c368SSeiji Aguchi 	trace_reschedule_exit(RESCHEDULE_VECTOR);
251eddc0e92SSeiji Aguchi }
252eddc0e92SSeiji Aguchi 
DEFINE_IDTENTRY_SYSVEC(sysvec_call_function)253582f9191SThomas Gleixner DEFINE_IDTENTRY_SYSVEC(sysvec_call_function)
254eddc0e92SSeiji Aguchi {
255670c04adSDave Hansen 	apic_eoi();
256cf910e83SSeiji Aguchi 	trace_call_function_entry(CALL_FUNCTION_VECTOR);
25785b77cddSThomas Gleixner 	inc_irq_stat(irq_call_count);
25885b77cddSThomas Gleixner 	generic_smp_call_function_interrupt();
259cf910e83SSeiji Aguchi 	trace_call_function_exit(CALL_FUNCTION_VECTOR);
260cf910e83SSeiji Aguchi }
261cf910e83SSeiji Aguchi 
DEFINE_IDTENTRY_SYSVEC(sysvec_call_function_single)262582f9191SThomas Gleixner DEFINE_IDTENTRY_SYSVEC(sysvec_call_function_single)
263cf910e83SSeiji Aguchi {
264670c04adSDave Hansen 	apic_eoi();
265cf910e83SSeiji Aguchi 	trace_call_function_single_entry(CALL_FUNCTION_SINGLE_VECTOR);
26685b77cddSThomas Gleixner 	inc_irq_stat(irq_call_count);
26785b77cddSThomas Gleixner 	generic_smp_call_function_single_interrupt();
268cf910e83SSeiji Aguchi 	trace_call_function_single_exit(CALL_FUNCTION_SINGLE_VECTOR);
269cf910e83SSeiji Aguchi }
270cf910e83SSeiji Aguchi 
nonmi_ipi_setup(char * str)271bda62633SDon Zickus static int __init nonmi_ipi_setup(char *str)
272bda62633SDon Zickus {
2733aac27abSDon Zickus 	smp_no_nmi_ipi = true;
274bda62633SDon Zickus 	return 1;
275bda62633SDon Zickus }
276bda62633SDon Zickus 
277bda62633SDon Zickus __setup("nonmi_ipi", nonmi_ipi_setup);
278bda62633SDon Zickus 
279f9e47a12SGlauber Costa struct smp_ops smp_ops = {
280f9e47a12SGlauber Costa 	.smp_prepare_boot_cpu	= native_smp_prepare_boot_cpu,
281f9e47a12SGlauber Costa 	.smp_prepare_cpus	= native_smp_prepare_cpus,
282f9e47a12SGlauber Costa 	.smp_cpus_done		= native_smp_cpus_done,
283f9e47a12SGlauber Costa 
2845d2b86d9SDon Zickus 	.stop_other_cpus	= native_stop_other_cpus,
2850ee59413SHidehiro Kawai #if defined(CONFIG_KEXEC_CORE)
2860ee59413SHidehiro Kawai 	.crash_stop_other_cpus	= kdump_nmi_shootdown_cpus,
2870ee59413SHidehiro Kawai #endif
288f9e47a12SGlauber Costa 	.smp_send_reschedule	= native_smp_send_reschedule,
2893b16cf87SJens Axboe 
2908b5a0f95SThomas Gleixner 	.kick_ap_alive		= native_kick_ap,
29193be71b6SAlex Nixon 	.cpu_disable		= native_cpu_disable,
29293be71b6SAlex Nixon 	.play_dead		= native_play_dead,
29393be71b6SAlex Nixon 
2943b16cf87SJens Axboe 	.send_call_func_ipi	= native_send_call_func_ipi,
2953b16cf87SJens Axboe 	.send_call_func_single_ipi = native_send_call_func_single_ipi,
296f9e47a12SGlauber Costa };
297f9e47a12SGlauber Costa EXPORT_SYMBOL_GPL(smp_ops);
298