xref: /linux/arch/x86/kernel/reboot.c (revision 2a5fe5a01668e831af1de3951718fbf88b9a9b9c)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c767a54bSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3c767a54bSJoe Perches 
4186f4360SPaul Gortmaker #include <linux/export.h>
54d022e35SMiguel Boton #include <linux/reboot.h>
64d022e35SMiguel Boton #include <linux/init.h>
74d022e35SMiguel Boton #include <linux/pm.h>
84d022e35SMiguel Boton #include <linux/efi.h>
96c6c51e4SPaul Mackerras #include <linux/dmi.h>
10d43c36dcSAlexey Dobriyan #include <linux/sched.h>
1169575d38SShane Wang #include <linux/tboot.h>
12ca444564SJean Delvare #include <linux/delay.h>
1300089c04SJulien Thierry #include <linux/objtool.h>
1465fddcfcSMike Rapoport #include <linux/pgtable.h>
1522daa422SKirill A. Shutemov #include <linux/kexec.h>
164d022e35SMiguel Boton #include <acpi/reboot.h>
174d022e35SMiguel Boton #include <asm/io.h>
184d022e35SMiguel Boton #include <asm/apic.h>
198643e28dSJiang Liu #include <asm/io_apic.h>
204d022e35SMiguel Boton #include <asm/desc.h>
214d022e35SMiguel Boton #include <asm/hpet.h>
224412620fSDmitri Vorobiev #include <asm/proto.h>
234d022e35SMiguel Boton #include <asm/reboot_fixups.h>
244d022e35SMiguel Boton #include <asm/reboot.h>
2582487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
2696b89dc6SJaswinder Singh Rajput #include <asm/cpu.h>
27c410b830SDon Zickus #include <asm/nmi.h>
2865051397SH. Peter Anvin #include <asm/smp.h>
294d022e35SMiguel Boton 
304d022e35SMiguel Boton #include <linux/ctype.h>
314d022e35SMiguel Boton #include <linux/mc146818rtc.h>
325a8c9aebSJarkko Sakkinen #include <asm/realmode.h>
33338bac52SFUJITA Tomonori #include <asm/x86_init.h>
3444be28e9SMatt Fleming #include <asm/efi.h>
354d022e35SMiguel Boton 
364d022e35SMiguel Boton /*
374d022e35SMiguel Boton  * Power off function, if any
384d022e35SMiguel Boton  */
394d022e35SMiguel Boton void (*pm_power_off)(void);
404d022e35SMiguel Boton EXPORT_SYMBOL(pm_power_off);
414d022e35SMiguel Boton 
42144d102bSMichael D Labriola /*
43144d102bSMichael D Labriola  * This is set if we need to go through the 'emergency' path.
44d176720dSEduardo Habkost  * When machine_emergency_restart() is called, we may be on
45d176720dSEduardo Habkost  * an inconsistent state and won't be able to do a clean cleanup
46d176720dSEduardo Habkost  */
47d176720dSEduardo Habkost static int reboot_emergency;
48d176720dSEduardo Habkost 
4914d7ca5cSH. Peter Anvin /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
5014d7ca5cSH. Peter Anvin bool port_cf9_safe = false;
5114d7ca5cSH. Peter Anvin 
52144d102bSMichael D Labriola /*
534d022e35SMiguel Boton  * Reboot options and system auto-detection code provided by
544d022e35SMiguel Boton  * Dell Inc. so their systems "just work". :-)
554d022e35SMiguel Boton  */
564d022e35SMiguel Boton 
574d022e35SMiguel Boton /*
584d581259SAlex Hung  * Some machines require the "reboot=a" commandline options
594d581259SAlex Hung  */
604d581259SAlex Hung static int __init set_acpi_reboot(const struct dmi_system_id *d)
614d581259SAlex Hung {
624d581259SAlex Hung 	if (reboot_type != BOOT_ACPI) {
634d581259SAlex Hung 		reboot_type = BOOT_ACPI;
644d581259SAlex Hung 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
654d581259SAlex Hung 			d->ident, "ACPI");
664d581259SAlex Hung 	}
674d581259SAlex Hung 	return 0;
684d581259SAlex Hung }
694d581259SAlex Hung 
704d581259SAlex Hung /*
711ef03890SPeter Chubb  * Some machines require the "reboot=b" or "reboot=k"  commandline options,
724d022e35SMiguel Boton  * this quirk makes that automatic.
734d022e35SMiguel Boton  */
744d022e35SMiguel Boton static int __init set_bios_reboot(const struct dmi_system_id *d)
754d022e35SMiguel Boton {
764d022e35SMiguel Boton 	if (reboot_type != BOOT_BIOS) {
774d022e35SMiguel Boton 		reboot_type = BOOT_BIOS;
78c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
796d9153bbSLan Tianyu 			d->ident, "BIOS");
804d022e35SMiguel Boton 	}
814d022e35SMiguel Boton 	return 0;
824d022e35SMiguel Boton }
834d022e35SMiguel Boton 
840082517fSJian-Hong Pan /*
850082517fSJian-Hong Pan  * Some machines don't handle the default ACPI reboot method and
860082517fSJian-Hong Pan  * require the EFI reboot method:
870082517fSJian-Hong Pan  */
880082517fSJian-Hong Pan static int __init set_efi_reboot(const struct dmi_system_id *d)
890082517fSJian-Hong Pan {
900082517fSJian-Hong Pan 	if (reboot_type != BOOT_EFI && !efi_runtime_disabled()) {
910082517fSJian-Hong Pan 		reboot_type = BOOT_EFI;
920082517fSJian-Hong Pan 		pr_info("%s series board detected. Selecting EFI-method for reboot.\n", d->ident);
930082517fSJian-Hong Pan 	}
940082517fSJian-Hong Pan 	return 0;
950082517fSJian-Hong Pan }
960082517fSJian-Hong Pan 
9765051397SH. Peter Anvin void __noreturn machine_real_restart(unsigned int type)
9857b16594SMichael D Labriola {
9957b16594SMichael D Labriola 	local_irq_disable();
10057b16594SMichael D Labriola 
101144d102bSMichael D Labriola 	/*
102144d102bSMichael D Labriola 	 * Write zero to CMOS register number 0x0f, which the BIOS POST
103144d102bSMichael D Labriola 	 * routine will recognize as telling it to do a proper reboot.  (Well
104144d102bSMichael D Labriola 	 * that's what this book in front of me says -- it may only apply to
105144d102bSMichael D Labriola 	 * the Phoenix BIOS though, it's not clear).  At the same time,
106144d102bSMichael D Labriola 	 * disable NMIs by setting the top bit in the CMOS address register,
107144d102bSMichael D Labriola 	 * as we're about to do peculiar things to the CPU.  I'm not sure if
108144d102bSMichael D Labriola 	 * `outb_p' is needed instead of just `outb'.  Use it to be on the
109144d102bSMichael D Labriola 	 * safe side.  (Yes, CMOS_WRITE does outb_p's. -  Paul G.)
11057b16594SMichael D Labriola 	 */
11157b16594SMichael D Labriola 	spin_lock(&rtc_lock);
11257b16594SMichael D Labriola 	CMOS_WRITE(0x00, 0x8f);
11357b16594SMichael D Labriola 	spin_unlock(&rtc_lock);
11457b16594SMichael D Labriola 
11557b16594SMichael D Labriola 	/*
11671d5049bSJoerg Roedel 	 * Switch to the trampoline page table.
11757b16594SMichael D Labriola 	 */
11871d5049bSJoerg Roedel 	load_trampoline_pgtable();
11957b16594SMichael D Labriola 
12057b16594SMichael D Labriola 	/* Jump to the identity-mapped low memory code */
12165051397SH. Peter Anvin #ifdef CONFIG_X86_32
12265051397SH. Peter Anvin 	asm volatile("jmpl *%0" : :
12365051397SH. Peter Anvin 		     "rm" (real_mode_header->machine_real_restart_asm),
12465051397SH. Peter Anvin 		     "a" (type));
12565051397SH. Peter Anvin #else
12665051397SH. Peter Anvin 	asm volatile("ljmpl *%0" : :
12765051397SH. Peter Anvin 		     "m" (real_mode_header->machine_real_restart_asm),
12865051397SH. Peter Anvin 		     "D" (type));
12965051397SH. Peter Anvin #endif
13065051397SH. Peter Anvin 	unreachable();
13157b16594SMichael D Labriola }
13257b16594SMichael D Labriola #ifdef CONFIG_APM_MODULE
13357b16594SMichael D Labriola EXPORT_SYMBOL(machine_real_restart);
13457b16594SMichael D Labriola #endif
135c207aee4SJosh Poimboeuf STACK_FRAME_NON_STANDARD(machine_real_restart);
13657b16594SMichael D Labriola 
13757b16594SMichael D Labriola /*
13857b16594SMichael D Labriola  * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
13957b16594SMichael D Labriola  */
14057b16594SMichael D Labriola static int __init set_pci_reboot(const struct dmi_system_id *d)
14157b16594SMichael D Labriola {
1425be44a6fSIngo Molnar 	if (reboot_type != BOOT_CF9_FORCE) {
1435be44a6fSIngo Molnar 		reboot_type = BOOT_CF9_FORCE;
144c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
1456d9153bbSLan Tianyu 			d->ident, "PCI");
14657b16594SMichael D Labriola 	}
14757b16594SMichael D Labriola 	return 0;
14857b16594SMichael D Labriola }
14957b16594SMichael D Labriola 
1501ef03890SPeter Chubb static int __init set_kbd_reboot(const struct dmi_system_id *d)
1511ef03890SPeter Chubb {
1521ef03890SPeter Chubb 	if (reboot_type != BOOT_KBD) {
1531ef03890SPeter Chubb 		reboot_type = BOOT_KBD;
154c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboot.\n",
1556d9153bbSLan Tianyu 			d->ident, "KBD");
1561ef03890SPeter Chubb 	}
1571ef03890SPeter Chubb 	return 0;
1581ef03890SPeter Chubb }
1591ef03890SPeter Chubb 
160144d102bSMichael D Labriola /*
16165051397SH. Peter Anvin  * This is a single dmi_table handling all reboot quirks.
16257b16594SMichael D Labriola  */
1636faadbbbSChristoph Hellwig static const struct dmi_system_id reboot_dmi_table[] __initconst = {
16457b16594SMichael D Labriola 
165e56e57f6SDave Jones 	/* Acer */
166b49c78d4SPeter Chubb 	{	/* Handle reboot issue on Acer Aspire one */
1671ef03890SPeter Chubb 		.callback = set_kbd_reboot,
168b49c78d4SPeter Chubb 		.ident = "Acer Aspire One A110",
169b49c78d4SPeter Chubb 		.matches = {
170b49c78d4SPeter Chubb 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
171b49c78d4SPeter Chubb 			DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
172b49c78d4SPeter Chubb 		},
173b49c78d4SPeter Chubb 	},
1740082517fSJian-Hong Pan 	{	/* Handle reboot issue on Acer TravelMate X514-51T */
1750082517fSJian-Hong Pan 		.callback = set_efi_reboot,
1760082517fSJian-Hong Pan 		.ident = "Acer TravelMate X514-51T",
1770082517fSJian-Hong Pan 		.matches = {
1780082517fSJian-Hong Pan 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1790082517fSJian-Hong Pan 			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate X514-51T"),
1800082517fSJian-Hong Pan 		},
1810082517fSJian-Hong Pan 	},
182e56e57f6SDave Jones 
183e56e57f6SDave Jones 	/* Apple */
1843e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBook5 */
1856c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
1863e03bbeaSShunichi Fuji 		.ident = "Apple MacBook5",
1876c6c51e4SPaul Mackerras 		.matches = {
1886c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1893e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
1906c6c51e4SPaul Mackerras 		},
1916c6c51e4SPaul Mackerras 	},
192140fd4acSHill Ma 	{	/* Handle problems with rebooting on Apple MacBook6,1 */
193140fd4acSHill Ma 		.callback = set_pci_reboot,
194140fd4acSHill Ma 		.ident = "Apple MacBook6,1",
195140fd4acSHill Ma 		.matches = {
196140fd4acSHill Ma 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
197140fd4acSHill Ma 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"),
198140fd4acSHill Ma 		},
199140fd4acSHill Ma 	},
2003e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBookPro5 */
201498cdbfbSOzan Çağlayan 		.callback = set_pci_reboot,
2023e03bbeaSShunichi Fuji 		.ident = "Apple MacBookPro5",
203498cdbfbSOzan Çağlayan 		.matches = {
204498cdbfbSOzan Çağlayan 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2053e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
206498cdbfbSOzan Çağlayan 		},
207498cdbfbSOzan Çağlayan 	},
20805154752SGottfried Haider 	{	/* Handle problems with rebooting on Apple Macmini3,1 */
20905154752SGottfried Haider 		.callback = set_pci_reboot,
21005154752SGottfried Haider 		.ident = "Apple Macmini3,1",
21105154752SGottfried Haider 		.matches = {
21205154752SGottfried Haider 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
21305154752SGottfried Haider 			DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
21405154752SGottfried Haider 		},
21505154752SGottfried Haider 	},
2160a832320SJustin P. Mattock 	{	/* Handle problems with rebooting on the iMac9,1. */
2170a832320SJustin P. Mattock 		.callback = set_pci_reboot,
2180a832320SJustin P. Mattock 		.ident = "Apple iMac9,1",
2190a832320SJustin P. Mattock 		.matches = {
2200a832320SJustin P. Mattock 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2210a832320SJustin P. Mattock 			DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
2220a832320SJustin P. Mattock 		},
2230a832320SJustin P. Mattock 	},
2242f0c0b2dSMario Kleiner 	{	/* Handle problems with rebooting on the iMac10,1. */
2252f0c0b2dSMario Kleiner 		.callback = set_pci_reboot,
2262f0c0b2dSMario Kleiner 		.ident = "Apple iMac10,1",
2272f0c0b2dSMario Kleiner 		.matches = {
2282f0c0b2dSMario Kleiner 		    DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2292f0c0b2dSMario Kleiner 		    DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
2302f0c0b2dSMario Kleiner 		},
2312f0c0b2dSMario Kleiner 	},
232e56e57f6SDave Jones 
23380313b30SStefan Lippers-Hollmann 	/* ASRock */
23480313b30SStefan Lippers-Hollmann 	{	/* Handle problems with rebooting on ASRock Q1900DC-ITX */
23580313b30SStefan Lippers-Hollmann 		.callback = set_pci_reboot,
23680313b30SStefan Lippers-Hollmann 		.ident = "ASRock Q1900DC-ITX",
23780313b30SStefan Lippers-Hollmann 		.matches = {
23880313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
23980313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
24080313b30SStefan Lippers-Hollmann 		},
24180313b30SStefan Lippers-Hollmann 	},
24280313b30SStefan Lippers-Hollmann 
243e56e57f6SDave Jones 	/* ASUS */
244e56e57f6SDave Jones 	{	/* Handle problems with rebooting on ASUS P4S800 */
245e56e57f6SDave Jones 		.callback = set_bios_reboot,
246e56e57f6SDave Jones 		.ident = "ASUS P4S800",
247e56e57f6SDave Jones 		.matches = {
248e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
249e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
250e56e57f6SDave Jones 		},
251e56e57f6SDave Jones 	},
25290b28dedSMatjaz Hegedic 	{	/* Handle problems with rebooting on ASUS EeeBook X205TA */
25390b28dedSMatjaz Hegedic 		.callback = set_acpi_reboot,
25490b28dedSMatjaz Hegedic 		.ident = "ASUS EeeBook X205TA",
25590b28dedSMatjaz Hegedic 		.matches = {
25690b28dedSMatjaz Hegedic 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
257bba8376aSMatjaz Hegedic 			DMI_MATCH(DMI_PRODUCT_NAME, "X205TA"),
25890b28dedSMatjaz Hegedic 		},
25990b28dedSMatjaz Hegedic 	},
2603b3e7855SMatjaz Hegedic 	{	/* Handle problems with rebooting on ASUS EeeBook X205TAW */
2613b3e7855SMatjaz Hegedic 		.callback = set_acpi_reboot,
2623b3e7855SMatjaz Hegedic 		.ident = "ASUS EeeBook X205TAW",
2633b3e7855SMatjaz Hegedic 		.matches = {
2643b3e7855SMatjaz Hegedic 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
2653b3e7855SMatjaz Hegedic 			DMI_MATCH(DMI_PRODUCT_NAME, "X205TAW"),
2663b3e7855SMatjaz Hegedic 		},
2673b3e7855SMatjaz Hegedic 	},
268e56e57f6SDave Jones 
269aadca6faSChristian Gmeiner 	/* Certec */
270aadca6faSChristian Gmeiner 	{       /* Handle problems with rebooting on Certec BPC600 */
271aadca6faSChristian Gmeiner 		.callback = set_pci_reboot,
272aadca6faSChristian Gmeiner 		.ident = "Certec BPC600",
273aadca6faSChristian Gmeiner 		.matches = {
274aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
275aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
276aadca6faSChristian Gmeiner 		},
277aadca6faSChristian Gmeiner 	},
278aadca6faSChristian Gmeiner 
279e56e57f6SDave Jones 	/* Dell */
280e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell DXP061 */
281e56e57f6SDave Jones 		.callback = set_bios_reboot,
282e56e57f6SDave Jones 		.ident = "Dell DXP061",
2833628c3f5SMaxime Ripard 		.matches = {
2843628c3f5SMaxime Ripard 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
285e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
286e56e57f6SDave Jones 		},
287e56e57f6SDave Jones 	},
288e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell E520's */
289e56e57f6SDave Jones 		.callback = set_bios_reboot,
290e56e57f6SDave Jones 		.ident = "Dell E520",
291e56e57f6SDave Jones 		.matches = {
292e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
293e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
2943628c3f5SMaxime Ripard 		},
2953628c3f5SMaxime Ripard 	},
2968412da75SVille Syrjälä 	{	/* Handle problems with rebooting on the Latitude E5410. */
2978412da75SVille Syrjälä 		.callback = set_pci_reboot,
2988412da75SVille Syrjälä 		.ident = "Dell Latitude E5410",
2998412da75SVille Syrjälä 		.matches = {
3008412da75SVille Syrjälä 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3018412da75SVille Syrjälä 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
3028412da75SVille Syrjälä 		},
3038412da75SVille Syrjälä 	},
304b7798d28SDaniel J Blueman 	{	/* Handle problems with rebooting on the Latitude E5420. */
305b7798d28SDaniel J Blueman 		.callback = set_pci_reboot,
306b7798d28SDaniel J Blueman 		.ident = "Dell Latitude E5420",
307b7798d28SDaniel J Blueman 		.matches = {
308b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
309b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
310b7798d28SDaniel J Blueman 		},
311b7798d28SDaniel J Blueman 	},
312e56e57f6SDave Jones 	{	/* Handle problems with rebooting on the Latitude E6320. */
313e56e57f6SDave Jones 		.callback = set_pci_reboot,
314e56e57f6SDave Jones 		.ident = "Dell Latitude E6320",
315e56e57f6SDave Jones 		.matches = {
316e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
317e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
318e56e57f6SDave Jones 		},
319e56e57f6SDave Jones 	},
320a536877eSH. Peter Anvin 	{	/* Handle problems with rebooting on the Latitude E6420. */
321a536877eSH. Peter Anvin 		.callback = set_pci_reboot,
322a536877eSH. Peter Anvin 		.ident = "Dell Latitude E6420",
323a536877eSH. Peter Anvin 		.matches = {
324a536877eSH. Peter Anvin 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
325a536877eSH. Peter Anvin 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
326a536877eSH. Peter Anvin 		},
327a536877eSH. Peter Anvin 	},
328e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
329e56e57f6SDave Jones 		.callback = set_bios_reboot,
330e56e57f6SDave Jones 		.ident = "Dell OptiPlex 330",
331e56e57f6SDave Jones 		.matches = {
332e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
333e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
334e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
335e56e57f6SDave Jones 		},
336e56e57f6SDave Jones 	},
337e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
338e56e57f6SDave Jones 		.callback = set_bios_reboot,
339e56e57f6SDave Jones 		.ident = "Dell OptiPlex 360",
340e56e57f6SDave Jones 		.matches = {
341e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
342e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
343e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
344e56e57f6SDave Jones 		},
345e56e57f6SDave Jones 	},
346e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's SFF */
347e56e57f6SDave Jones 		.callback = set_bios_reboot,
348e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
349e56e57f6SDave Jones 		.matches = {
350e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
351e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
352e56e57f6SDave Jones 		},
353e56e57f6SDave Jones 	},
354e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's DFF */
355e56e57f6SDave Jones 		.callback = set_bios_reboot,
356e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
357e56e57f6SDave Jones 		.matches = {
358e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
359e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
360e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
361e56e57f6SDave Jones 		},
362e56e57f6SDave Jones 	},
363e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
364e56e57f6SDave Jones 		.callback = set_bios_reboot,
365e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
366e56e57f6SDave Jones 		.matches = {
367e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
368e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
369e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
370e56e57f6SDave Jones 		},
371e56e57f6SDave Jones 	},
372e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
373e56e57f6SDave Jones 		.callback = set_bios_reboot,
374e56e57f6SDave Jones 		.ident = "Dell OptiPlex 760",
375e56e57f6SDave Jones 		.matches = {
376e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
377e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
378e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
379e56e57f6SDave Jones 		},
380e56e57f6SDave Jones 	},
3816be30bb7SRafael J. Wysocki 	{	/* Handle problems with rebooting on the OptiPlex 990. */
3826be30bb7SRafael J. Wysocki 		.callback = set_pci_reboot,
383a729691bSPaul Gortmaker 		.ident = "Dell OptiPlex 990 BIOS A0x",
3846be30bb7SRafael J. Wysocki 		.matches = {
3856be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3866be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
387a729691bSPaul Gortmaker 			DMI_MATCH(DMI_BIOS_VERSION, "A0"),
3886be30bb7SRafael J. Wysocki 		},
3896be30bb7SRafael J. Wysocki 	},
390e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 300's */
391e56e57f6SDave Jones 		.callback = set_bios_reboot,
392e56e57f6SDave Jones 		.ident = "Dell PowerEdge 300",
39376eb9a30SZhang Rui 		.matches = {
394e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
395e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
396e56e57f6SDave Jones 		},
397e56e57f6SDave Jones 	},
398e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 1300's */
399e56e57f6SDave Jones 		.callback = set_bios_reboot,
400e56e57f6SDave Jones 		.ident = "Dell PowerEdge 1300",
401e56e57f6SDave Jones 		.matches = {
402e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
403e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
404e56e57f6SDave Jones 		},
405e56e57f6SDave Jones 	},
406e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 2400's */
407e56e57f6SDave Jones 		.callback = set_bios_reboot,
408e56e57f6SDave Jones 		.ident = "Dell PowerEdge 2400",
409e56e57f6SDave Jones 		.matches = {
410e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
411e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
41276eb9a30SZhang Rui 		},
41376eb9a30SZhang Rui 	},
4144f0acd31SMasoud Sharbiani 	{	/* Handle problems with rebooting on the Dell PowerEdge C6100. */
4154f0acd31SMasoud Sharbiani 		.callback = set_pci_reboot,
4164f0acd31SMasoud Sharbiani 		.ident = "Dell PowerEdge C6100",
4174f0acd31SMasoud Sharbiani 		.matches = {
4184f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
4194f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
4204f0acd31SMasoud Sharbiani 		},
4214f0acd31SMasoud Sharbiani 	},
4226c6c51e4SPaul Mackerras 	{	/* Handle problems with rebooting on the Precision M6600. */
4236c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
4246c6c51e4SPaul Mackerras 		.ident = "Dell Precision M6600",
4256c6c51e4SPaul Mackerras 		.matches = {
4266c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4276c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
4286c6c51e4SPaul Mackerras 		},
4296c6c51e4SPaul Mackerras 	},
430e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T5400's */
431e56e57f6SDave Jones 		.callback = set_bios_reboot,
432e56e57f6SDave Jones 		.ident = "Dell Precision T5400",
43357b16594SMichael D Labriola 		.matches = {
4346c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
435e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
436144d102bSMichael D Labriola 		},
4375955633eSMichael D Labriola 	},
438e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T7400's */
439e56e57f6SDave Jones 		.callback = set_bios_reboot,
440e56e57f6SDave Jones 		.ident = "Dell Precision T7400",
4416c6c51e4SPaul Mackerras 		.matches = {
442e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
443e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
4446c6c51e4SPaul Mackerras 		},
4454d022e35SMiguel Boton 	},
446e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell XPS710 */
447e56e57f6SDave Jones 		.callback = set_bios_reboot,
448e56e57f6SDave Jones 		.ident = "Dell XPS710",
449e56e57f6SDave Jones 		.matches = {
450e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
451e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
452e56e57f6SDave Jones 		},
453e56e57f6SDave Jones 	},
4544d581259SAlex Hung 	{	/* Handle problems with rebooting on Dell Optiplex 7450 AIO */
4554d581259SAlex Hung 		.callback = set_acpi_reboot,
4564d581259SAlex Hung 		.ident = "Dell OptiPlex 7450 AIO",
4574d581259SAlex Hung 		.matches = {
4584d581259SAlex Hung 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4594d581259SAlex Hung 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7450 AIO"),
4604d581259SAlex Hung 		},
4614d581259SAlex Hung 	},
462e56e57f6SDave Jones 
463e56e57f6SDave Jones 	/* Hewlett-Packard */
464e56e57f6SDave Jones 	{	/* Handle problems with rebooting on HP laptops */
465e56e57f6SDave Jones 		.callback = set_bios_reboot,
466e56e57f6SDave Jones 		.ident = "HP Compaq Laptop",
467e56e57f6SDave Jones 		.matches = {
468e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
469e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
470e56e57f6SDave Jones 		},
471e56e57f6SDave Jones 	},
472e56e57f6SDave Jones 
4734b2d8ca9SHeiner Kallweit 	{	/* PCIe Wifi card isn't detected after reboot otherwise */
4744b2d8ca9SHeiner Kallweit 		.callback = set_pci_reboot,
4754b2d8ca9SHeiner Kallweit 		.ident = "Zotac ZBOX CI327 nano",
4764b2d8ca9SHeiner Kallweit 		.matches = {
4774b2d8ca9SHeiner Kallweit 			DMI_MATCH(DMI_SYS_VENDOR, "NA"),
4784b2d8ca9SHeiner Kallweit 			DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
4794b2d8ca9SHeiner Kallweit 		},
4804b2d8ca9SHeiner Kallweit 	},
4814b2d8ca9SHeiner Kallweit 
482e56e57f6SDave Jones 	/* Sony */
483e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Sony VGN-Z540N */
484e56e57f6SDave Jones 		.callback = set_bios_reboot,
485e56e57f6SDave Jones 		.ident = "Sony VGN-Z540N",
486e56e57f6SDave Jones 		.matches = {
487e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
488e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
489e56e57f6SDave Jones 		},
490e56e57f6SDave Jones 	},
491e56e57f6SDave Jones 
4924d022e35SMiguel Boton 	{ }
4934d022e35SMiguel Boton };
4949c48f1c6SDon Zickus 
495d176720dSEduardo Habkost static int __init reboot_init(void)
496d176720dSEduardo Habkost {
49744be28e9SMatt Fleming 	int rv;
49844be28e9SMatt Fleming 
499d176720dSEduardo Habkost 	/*
500d176720dSEduardo Habkost 	 * Only do the DMI check if reboot_type hasn't been overridden
501d176720dSEduardo Habkost 	 * on the command line
502d176720dSEduardo Habkost 	 */
50344be28e9SMatt Fleming 	if (!reboot_default)
50444be28e9SMatt Fleming 		return 0;
50544be28e9SMatt Fleming 
50644be28e9SMatt Fleming 	/*
50744be28e9SMatt Fleming 	 * The DMI quirks table takes precedence. If no quirks entry
5084ecf7191SStefan Assmann 	 * matches and the ACPI Hardware Reduced bit is set and EFI
5094ecf7191SStefan Assmann 	 * runtime services are enabled, force EFI reboot.
51044be28e9SMatt Fleming 	 */
51144be28e9SMatt Fleming 	rv = dmi_check_system(reboot_dmi_table);
51244be28e9SMatt Fleming 
5134ecf7191SStefan Assmann 	if (!rv && efi_reboot_required() && !efi_runtime_disabled())
51444be28e9SMatt Fleming 		reboot_type = BOOT_EFI;
51544be28e9SMatt Fleming 
516d176720dSEduardo Habkost 	return 0;
517d176720dSEduardo Habkost }
518d176720dSEduardo Habkost core_initcall(reboot_init);
519d176720dSEduardo Habkost 
520d176720dSEduardo Habkost static inline void kb_wait(void)
521d176720dSEduardo Habkost {
522d176720dSEduardo Habkost 	int i;
523d176720dSEduardo Habkost 
524d176720dSEduardo Habkost 	for (i = 0; i < 0x10000; i++) {
5257432d149SIngo Molnar 		if ((inb(0x64) & 0x02) == 0)
5267432d149SIngo Molnar 			break;
5277432d149SIngo Molnar 		udelay(2);
5287432d149SIngo Molnar 	}
529416e2d63SJody Belka }
5304d022e35SMiguel Boton 
53126044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void);
5324d022e35SMiguel Boton 
533*2a5fe5a0SPaolo Bonzini #if IS_ENABLED(CONFIG_KVM_X86)
534edc8deb0SSean Christopherson /* RCU-protected callback to disable virtualization prior to reboot. */
535edc8deb0SSean Christopherson static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback;
536edc8deb0SSean Christopherson 
537edc8deb0SSean Christopherson void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback)
538edc8deb0SSean Christopherson {
539edc8deb0SSean Christopherson 	if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback)))
540edc8deb0SSean Christopherson 		return;
541edc8deb0SSean Christopherson 
542edc8deb0SSean Christopherson 	rcu_assign_pointer(cpu_emergency_virt_callback, callback);
543edc8deb0SSean Christopherson }
544edc8deb0SSean Christopherson EXPORT_SYMBOL_GPL(cpu_emergency_register_virt_callback);
545edc8deb0SSean Christopherson 
546edc8deb0SSean Christopherson void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback)
547edc8deb0SSean Christopherson {
548edc8deb0SSean Christopherson 	if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback) != callback))
549edc8deb0SSean Christopherson 		return;
550edc8deb0SSean Christopherson 
551edc8deb0SSean Christopherson 	rcu_assign_pointer(cpu_emergency_virt_callback, NULL);
552edc8deb0SSean Christopherson 	synchronize_rcu();
553edc8deb0SSean Christopherson }
554edc8deb0SSean Christopherson EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback);
555edc8deb0SSean Christopherson 
556edc8deb0SSean Christopherson /*
557edc8deb0SSean Christopherson  * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
558edc8deb0SSean Christopherson  * reboot.  VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
559edc8deb0SSean Christopherson  * GIF=0, i.e. if the crash occurred between CLGI and STGI.
560edc8deb0SSean Christopherson  */
561edc8deb0SSean Christopherson void cpu_emergency_disable_virtualization(void)
562edc8deb0SSean Christopherson {
563edc8deb0SSean Christopherson 	cpu_emergency_virt_cb *callback;
564edc8deb0SSean Christopherson 
565edc8deb0SSean Christopherson 	/*
566edc8deb0SSean Christopherson 	 * IRQs must be disabled as KVM enables virtualization in hardware via
567edc8deb0SSean Christopherson 	 * function call IPIs, i.e. IRQs need to be disabled to guarantee
568edc8deb0SSean Christopherson 	 * virtualization stays disabled.
569edc8deb0SSean Christopherson 	 */
570edc8deb0SSean Christopherson 	lockdep_assert_irqs_disabled();
571edc8deb0SSean Christopherson 
572edc8deb0SSean Christopherson 	rcu_read_lock();
573edc8deb0SSean Christopherson 	callback = rcu_dereference(cpu_emergency_virt_callback);
574edc8deb0SSean Christopherson 	if (callback)
575edc8deb0SSean Christopherson 		callback();
576edc8deb0SSean Christopherson 	rcu_read_unlock();
577edc8deb0SSean Christopherson }
578edc8deb0SSean Christopherson 
579d81f952aSSean Christopherson static void emergency_reboot_disable_virtualization(void)
5804d022e35SMiguel Boton {
5814d022e35SMiguel Boton 	local_irq_disable();
5824d022e35SMiguel Boton 
583144d102bSMichael D Labriola 	/*
584d81f952aSSean Christopherson 	 * Disable virtualization on all CPUs before rebooting to avoid hanging
585d81f952aSSean Christopherson 	 * the system, as VMX and SVM block INIT when running in the host.
5864d022e35SMiguel Boton 	 *
587ed727361SSean Christopherson 	 * We can't take any locks and we may be on an inconsistent state, so
588d81f952aSSean Christopherson 	 * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt.
5894d022e35SMiguel Boton 	 *
590d81f952aSSean Christopherson 	 * Do the NMI shootdown even if virtualization is off on _this_ CPU, as
591d81f952aSSean Christopherson 	 * other CPUs may have virtualization enabled.
5924d022e35SMiguel Boton 	 */
59359765db5SSean Christopherson 	if (rcu_access_pointer(cpu_emergency_virt_callback)) {
594d81f952aSSean Christopherson 		/* Safely force _this_ CPU out of VMX/SVM operation. */
595d81f952aSSean Christopherson 		cpu_emergency_disable_virtualization();
5964d022e35SMiguel Boton 
597d81f952aSSean Christopherson 		/* Disable VMX/SVM and halt on other CPUs. */
59826044affSSean Christopherson 		nmi_shootdown_cpus_on_restart();
5994d022e35SMiguel Boton 	}
6004d022e35SMiguel Boton }
601261cd5edSSean Christopherson #else
602261cd5edSSean Christopherson static void emergency_reboot_disable_virtualization(void) { }
603*2a5fe5a0SPaolo Bonzini #endif /* CONFIG_KVM_X86 */
6044d022e35SMiguel Boton 
6054d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void)
6064d022e35SMiguel Boton {
6074d022e35SMiguel Boton }
6084d022e35SMiguel Boton 
609660e34ceSMatthew Garrett /*
6105be44a6fSIngo Molnar  * To the best of our knowledge Windows compatible x86 hardware expects
6115be44a6fSIngo Molnar  * the following on reboot:
612660e34ceSMatthew Garrett  *
613660e34ceSMatthew Garrett  * 1) If the FADT has the ACPI reboot register flag set, try it
614660e34ceSMatthew Garrett  * 2) If still alive, write to the keyboard controller
615660e34ceSMatthew Garrett  * 3) If still alive, write to the ACPI reboot register again
616660e34ceSMatthew Garrett  * 4) If still alive, write to the keyboard controller again
617a4f1987eSLi, Aubrey  * 5) If still alive, call the EFI runtime service to reboot
6185be44a6fSIngo Molnar  * 6) If no EFI runtime service, call the BIOS to do a reboot
619660e34ceSMatthew Garrett  *
6205be44a6fSIngo Molnar  * We default to following the same pattern. We also have
6215be44a6fSIngo Molnar  * two other reboot methods: 'triple fault' and 'PCI', which
6225be44a6fSIngo Molnar  * can be triggered via the reboot= kernel boot option or
6235be44a6fSIngo Molnar  * via quirks.
6245be44a6fSIngo Molnar  *
6255be44a6fSIngo Molnar  * This means that this function can never return, it can misbehave
6265be44a6fSIngo Molnar  * by not rebooting properly and hanging.
627660e34ceSMatthew Garrett  */
6284d022e35SMiguel Boton static void native_machine_emergency_restart(void)
6294d022e35SMiguel Boton {
6304d022e35SMiguel Boton 	int i;
631660e34ceSMatthew Garrett 	int attempt = 0;
632660e34ceSMatthew Garrett 	int orig_reboot_type = reboot_type;
633edf2b139SRobin Holt 	unsigned short mode;
6344d022e35SMiguel Boton 
635d176720dSEduardo Habkost 	if (reboot_emergency)
636d81f952aSSean Christopherson 		emergency_reboot_disable_virtualization();
637d176720dSEduardo Habkost 
638840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_REBOOT);
639840c2bafSJoseph Cihula 
6404d022e35SMiguel Boton 	/* Tell the BIOS if we want cold or warm reboot */
641edf2b139SRobin Holt 	mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
642edf2b139SRobin Holt 	*((unsigned short *)__va(0x472)) = mode;
6434d022e35SMiguel Boton 
64487615a34SMatt Fleming 	/*
64587615a34SMatt Fleming 	 * If an EFI capsule has been registered with the firmware then
64687615a34SMatt Fleming 	 * override the reboot= parameter.
64787615a34SMatt Fleming 	 */
64887615a34SMatt Fleming 	if (efi_capsule_pending(NULL)) {
64987615a34SMatt Fleming 		pr_info("EFI capsule is pending, forcing EFI reboot.\n");
65087615a34SMatt Fleming 		reboot_type = BOOT_EFI;
65187615a34SMatt Fleming 	}
65287615a34SMatt Fleming 
6534d022e35SMiguel Boton 	for (;;) {
6544d022e35SMiguel Boton 		/* Could also try the reset bit in the Hammer NB */
6554d022e35SMiguel Boton 		switch (reboot_type) {
6565be44a6fSIngo Molnar 		case BOOT_ACPI:
6575be44a6fSIngo Molnar 			acpi_reboot();
6585be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
6595be44a6fSIngo Molnar 			break;
6605be44a6fSIngo Molnar 
6614d022e35SMiguel Boton 		case BOOT_KBD:
662144d102bSMichael D Labriola 			mach_reboot_fixups(); /* For board specific fixups */
6637432d149SIngo Molnar 
6644d022e35SMiguel Boton 			for (i = 0; i < 10; i++) {
6654d022e35SMiguel Boton 				kb_wait();
6664d022e35SMiguel Boton 				udelay(50);
667144d102bSMichael D Labriola 				outb(0xfe, 0x64); /* Pulse reset low */
6684d022e35SMiguel Boton 				udelay(50);
6694d022e35SMiguel Boton 			}
670660e34ceSMatthew Garrett 			if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
671660e34ceSMatthew Garrett 				attempt = 1;
672660e34ceSMatthew Garrett 				reboot_type = BOOT_ACPI;
673660e34ceSMatthew Garrett 			} else {
674a4f1987eSLi, Aubrey 				reboot_type = BOOT_EFI;
675660e34ceSMatthew Garrett 			}
676660e34ceSMatthew Garrett 			break;
6774d022e35SMiguel Boton 
6784d022e35SMiguel Boton 		case BOOT_EFI:
6798562c99cSMatt Fleming 			efi_reboot(reboot_mode, NULL);
6805be44a6fSIngo Molnar 			reboot_type = BOOT_BIOS;
68114d7ca5cSH. Peter Anvin 			break;
6824d022e35SMiguel Boton 
6835be44a6fSIngo Molnar 		case BOOT_BIOS:
6845be44a6fSIngo Molnar 			machine_real_restart(MRR_BIOS);
6855be44a6fSIngo Molnar 
6865be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6875be44a6fSIngo Molnar 			reboot_type = BOOT_CF9_SAFE;
6885be44a6fSIngo Molnar 			break;
6895be44a6fSIngo Molnar 
6905be44a6fSIngo Molnar 		case BOOT_CF9_FORCE:
69114d7ca5cSH. Peter Anvin 			port_cf9_safe = true;
692df561f66SGustavo A. R. Silva 			fallthrough;
69314d7ca5cSH. Peter Anvin 
6945be44a6fSIngo Molnar 		case BOOT_CF9_SAFE:
69514d7ca5cSH. Peter Anvin 			if (port_cf9_safe) {
6965be44a6fSIngo Molnar 				u8 reboot_code = reboot_mode == REBOOT_WARM ?  0x06 : 0x0E;
69716c21ae5SLi Fei 				u8 cf9 = inb(0xcf9) & ~reboot_code;
69814d7ca5cSH. Peter Anvin 				outb(cf9|2, 0xcf9); /* Request hard reset */
69914d7ca5cSH. Peter Anvin 				udelay(50);
70016c21ae5SLi Fei 				/* Actually do the reset */
70116c21ae5SLi Fei 				outb(cf9|reboot_code, 0xcf9);
70214d7ca5cSH. Peter Anvin 				udelay(50);
70314d7ca5cSH. Peter Anvin 			}
7045be44a6fSIngo Molnar 			reboot_type = BOOT_TRIPLE;
7055be44a6fSIngo Molnar 			break;
7065be44a6fSIngo Molnar 
7075be44a6fSIngo Molnar 		case BOOT_TRIPLE:
7088ec9069aSH. Peter Anvin (Intel) 			idt_invalidate();
7095be44a6fSIngo Molnar 			__asm__ __volatile__("int3");
7105be44a6fSIngo Molnar 
7115be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
7125be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
7134d022e35SMiguel Boton 			break;
7144d022e35SMiguel Boton 		}
7154d022e35SMiguel Boton 	}
7164d022e35SMiguel Boton }
7174d022e35SMiguel Boton 
7183c62c625SGlauber Costa void native_machine_shutdown(void)
7194d022e35SMiguel Boton {
72022daa422SKirill A. Shutemov 	/*
72122daa422SKirill A. Shutemov 	 * Call enc_kexec_begin() while all CPUs are still active and
72222daa422SKirill A. Shutemov 	 * interrupts are enabled. This will allow all in-flight memory
72322daa422SKirill A. Shutemov 	 * conversions to finish cleanly.
72422daa422SKirill A. Shutemov 	 */
72522daa422SKirill A. Shutemov 	if (kexec_in_progress)
72622daa422SKirill A. Shutemov 		x86_platform.guest.enc_kexec_begin();
72722daa422SKirill A. Shutemov 
7284d022e35SMiguel Boton 	/* Stop the cpus and apics */
729522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC
7302885432aSFenghua Yu 	/*
7312885432aSFenghua Yu 	 * Disabling IO APIC before local APIC is a workaround for
7322885432aSFenghua Yu 	 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
7332885432aSFenghua Yu 	 * Specification Update". In this situation, interrupts that target
7342885432aSFenghua Yu 	 * a Logical Processor whose Local APIC is either in the process of
7352885432aSFenghua Yu 	 * being hardware disabled or software disabled are neither delivered
7362885432aSFenghua Yu 	 * nor discarded. When this erratum occurs, the processor may hang.
7372885432aSFenghua Yu 	 *
7382885432aSFenghua Yu 	 * Even without the erratum, it still makes sense to quiet IO APIC
7392885432aSFenghua Yu 	 * before disabling Local APIC.
7402885432aSFenghua Yu 	 */
741339b2ae0SBaoquan He 	clear_IO_APIC();
742522e6646SFenghua Yu #endif
743522e6646SFenghua Yu 
7444d022e35SMiguel Boton #ifdef CONFIG_SMP
745144d102bSMichael D Labriola 	/*
7461b3a5d02SRobin Holt 	 * Stop all of the others. Also disable the local irq to
7471b3a5d02SRobin Holt 	 * not receive the per-cpu timer interrupt which may trigger
7481b3a5d02SRobin Holt 	 * scheduler's load balance.
7494d022e35SMiguel Boton 	 */
75055c844a4SFeng Tang 	local_irq_disable();
75176fac077SAlok Kataria 	stop_other_cpus();
7524d022e35SMiguel Boton #endif
7534d022e35SMiguel Boton 
7544d022e35SMiguel Boton 	lapic_shutdown();
755339b2ae0SBaoquan He 	restore_boot_irq_mode();
7564d022e35SMiguel Boton 
7574d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER
7584d022e35SMiguel Boton 	hpet_disable();
7594d022e35SMiguel Boton #endif
7604d022e35SMiguel Boton 
7614d022e35SMiguel Boton #ifdef CONFIG_X86_64
762338bac52SFUJITA Tomonori 	x86_platform.iommu_shutdown();
7634d022e35SMiguel Boton #endif
76422daa422SKirill A. Shutemov 
76522daa422SKirill A. Shutemov 	if (kexec_in_progress)
76622daa422SKirill A. Shutemov 		x86_platform.guest.enc_kexec_finish();
7674d022e35SMiguel Boton }
7684d022e35SMiguel Boton 
769d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency)
770d176720dSEduardo Habkost {
771d176720dSEduardo Habkost 	reboot_emergency = emergency;
772d176720dSEduardo Habkost 	machine_ops.emergency_restart();
773d176720dSEduardo Habkost }
774d176720dSEduardo Habkost 
775416e2d63SJody Belka static void native_machine_restart(char *__unused)
7764d022e35SMiguel Boton {
777c767a54bSJoe Perches 	pr_notice("machine restart\n");
7784d022e35SMiguel Boton 
7794d022e35SMiguel Boton 	if (!reboot_force)
7804d022e35SMiguel Boton 		machine_shutdown();
781d176720dSEduardo Habkost 	__machine_emergency_restart(0);
7824d022e35SMiguel Boton }
7834d022e35SMiguel Boton 
784416e2d63SJody Belka static void native_machine_halt(void)
7854d022e35SMiguel Boton {
786144d102bSMichael D Labriola 	/* Stop other cpus and apics */
787d3ec5caeSIvan Vecera 	machine_shutdown();
788d3ec5caeSIvan Vecera 
789840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
790840c2bafSJoseph Cihula 
791d3ec5caeSIvan Vecera 	stop_this_cpu(NULL);
7924d022e35SMiguel Boton }
7934d022e35SMiguel Boton 
794416e2d63SJody Belka static void native_machine_power_off(void)
7954d022e35SMiguel Boton {
796d3577349SDmitry Osipenko 	if (kernel_can_power_off()) {
7974d022e35SMiguel Boton 		if (!reboot_force)
7984d022e35SMiguel Boton 			machine_shutdown();
799d3577349SDmitry Osipenko 		do_kernel_power_off();
8004d022e35SMiguel Boton 	}
801144d102bSMichael D Labriola 	/* A fallback in case there is no PM info available */
802840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
8034d022e35SMiguel Boton }
8044d022e35SMiguel Boton 
805404f6aacSKees Cook struct machine_ops machine_ops __ro_after_init = {
806416e2d63SJody Belka 	.power_off = native_machine_power_off,
807416e2d63SJody Belka 	.shutdown = native_machine_shutdown,
808416e2d63SJody Belka 	.emergency_restart = native_machine_emergency_restart,
809416e2d63SJody Belka 	.restart = native_machine_restart,
810ed23dc6fSGlauber Costa 	.halt = native_machine_halt,
811a4eeb217SBaoquan He #ifdef CONFIG_CRASH_DUMP
812ed23dc6fSGlauber Costa 	.crash_shutdown = native_machine_crash_shutdown,
813ed23dc6fSGlauber Costa #endif
8144d022e35SMiguel Boton };
815416e2d63SJody Belka 
816416e2d63SJody Belka void machine_power_off(void)
817416e2d63SJody Belka {
818416e2d63SJody Belka 	machine_ops.power_off();
819416e2d63SJody Belka }
820416e2d63SJody Belka 
821416e2d63SJody Belka void machine_shutdown(void)
822416e2d63SJody Belka {
823416e2d63SJody Belka 	machine_ops.shutdown();
824416e2d63SJody Belka }
825416e2d63SJody Belka 
826416e2d63SJody Belka void machine_emergency_restart(void)
827416e2d63SJody Belka {
828d176720dSEduardo Habkost 	__machine_emergency_restart(1);
829416e2d63SJody Belka }
830416e2d63SJody Belka 
831416e2d63SJody Belka void machine_restart(char *cmd)
832416e2d63SJody Belka {
833416e2d63SJody Belka 	machine_ops.restart(cmd);
834416e2d63SJody Belka }
835416e2d63SJody Belka 
836416e2d63SJody Belka void machine_halt(void)
837416e2d63SJody Belka {
838416e2d63SJody Belka 	machine_ops.halt();
839416e2d63SJody Belka }
840416e2d63SJody Belka 
841a4eeb217SBaoquan He #ifdef CONFIG_CRASH_DUMP
842ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs)
843ed23dc6fSGlauber Costa {
844ed23dc6fSGlauber Costa 	machine_ops.crash_shutdown(regs);
845ed23dc6fSGlauber Costa }
846ed23dc6fSGlauber Costa #endif
8472ddded21SEduardo Habkost 
8485bc32950SXunlei Pang /* This is the CPU performing the emergency shutdown work. */
8495bc32950SXunlei Pang int crashing_cpu = -1;
8505bc32950SXunlei Pang 
851bb8dd270SEduardo Habkost #if defined(CONFIG_SMP)
8522ddded21SEduardo Habkost 
8532ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback;
8542ddded21SEduardo Habkost 
8552ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi;
85658c5661fSHidehiro Kawai static int crash_ipi_issued;
8572ddded21SEduardo Habkost 
8589c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
8592ddded21SEduardo Habkost {
8602ddded21SEduardo Habkost 	int cpu;
8612ddded21SEduardo Habkost 
8622ddded21SEduardo Habkost 	cpu = raw_smp_processor_id();
8632ddded21SEduardo Habkost 
864144d102bSMichael D Labriola 	/*
865144d102bSMichael D Labriola 	 * Don't do anything if this handler is invoked on crashing cpu.
8662ddded21SEduardo Habkost 	 * Otherwise, system will completely hang. Crashing cpu can get
8672ddded21SEduardo Habkost 	 * an NMI if system was initially booted with nmi_watchdog parameter.
8682ddded21SEduardo Habkost 	 */
8692ddded21SEduardo Habkost 	if (cpu == crashing_cpu)
8709c48f1c6SDon Zickus 		return NMI_HANDLED;
8712ddded21SEduardo Habkost 	local_irq_disable();
8722ddded21SEduardo Habkost 
87326044affSSean Christopherson 	if (shootdown_callback)
8749c48f1c6SDon Zickus 		shootdown_callback(cpu, regs);
8752ddded21SEduardo Habkost 
87626044affSSean Christopherson 	/*
87726044affSSean Christopherson 	 * Prepare the CPU for reboot _after_ invoking the callback so that the
87826044affSSean Christopherson 	 * callback can safely use virtualization instructions, e.g. VMCLEAR.
87926044affSSean Christopherson 	 */
88026044affSSean Christopherson 	cpu_emergency_disable_virtualization();
88126044affSSean Christopherson 
8822ddded21SEduardo Habkost 	atomic_dec(&waiting_for_crash_ipi);
88326ba7353SKirill A. Shutemov 
88426ba7353SKirill A. Shutemov 	if (smp_ops.stop_this_cpu) {
88526ba7353SKirill A. Shutemov 		smp_ops.stop_this_cpu();
88626ba7353SKirill A. Shutemov 		unreachable();
88726ba7353SKirill A. Shutemov 	}
88826ba7353SKirill A. Shutemov 
8892ddded21SEduardo Habkost 	/* Assume hlt works */
8902ddded21SEduardo Habkost 	halt();
8912ddded21SEduardo Habkost 	for (;;)
8922ddded21SEduardo Habkost 		cpu_relax();
8932ddded21SEduardo Habkost 
8949c48f1c6SDon Zickus 	return NMI_HANDLED;
8952ddded21SEduardo Habkost }
8962ddded21SEduardo Habkost 
89726044affSSean Christopherson /**
89826044affSSean Christopherson  * nmi_shootdown_cpus - Stop other CPUs via NMI
89926044affSSean Christopherson  * @callback:	Optional callback to be invoked from the NMI handler
900bb8dd270SEduardo Habkost  *
90126044affSSean Christopherson  * The NMI handler on the remote CPUs invokes @callback, if not
90226044affSSean Christopherson  * NULL, first and then disables virtualization to ensure that
90326044affSSean Christopherson  * INIT is recognized during reboot.
90426044affSSean Christopherson  *
90526044affSSean Christopherson  * nmi_shootdown_cpus() can only be invoked once. After the first
90626044affSSean Christopherson  * invocation all other CPUs are stuck in crash_nmi_callback() and
90726044affSSean Christopherson  * cannot respond to a second NMI.
908bb8dd270SEduardo Habkost  */
9092ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
9102ddded21SEduardo Habkost {
9112ddded21SEduardo Habkost 	unsigned long msecs;
91226044affSSean Christopherson 
913c415b3dcSEduardo Habkost 	local_irq_disable();
9142ddded21SEduardo Habkost 
91526044affSSean Christopherson 	/*
91626044affSSean Christopherson 	 * Avoid certain doom if a shootdown already occurred; re-registering
91726044affSSean Christopherson 	 * the NMI handler will cause list corruption, modifying the callback
91826044affSSean Christopherson 	 * will do who knows what, etc...
91926044affSSean Christopherson 	 */
92026044affSSean Christopherson 	if (WARN_ON_ONCE(crash_ipi_issued))
92126044affSSean Christopherson 		return;
92226044affSSean Christopherson 
9232ddded21SEduardo Habkost 	/* Make a note of crashing cpu. Will be used in NMI callback. */
9242ddded21SEduardo Habkost 	crashing_cpu = safe_smp_processor_id();
9252ddded21SEduardo Habkost 
9262ddded21SEduardo Habkost 	shootdown_callback = callback;
9272ddded21SEduardo Habkost 
9282ddded21SEduardo Habkost 	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
9292ddded21SEduardo Habkost 	/* Would it be better to replace the trap vector here? */
9309c48f1c6SDon Zickus 	if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
9319c48f1c6SDon Zickus 				 NMI_FLAG_FIRST, "crash"))
932144d102bSMichael D Labriola 		return;		/* Return what? */
933144d102bSMichael D Labriola 	/*
934144d102bSMichael D Labriola 	 * Ensure the new callback function is set before sending
9352ddded21SEduardo Habkost 	 * out the NMI
9362ddded21SEduardo Habkost 	 */
9372ddded21SEduardo Habkost 	wmb();
9382ddded21SEduardo Habkost 
93922ca7ee9SThomas Gleixner 	apic_send_IPI_allbutself(NMI_VECTOR);
9402ddded21SEduardo Habkost 
94158c5661fSHidehiro Kawai 	/* Kick CPUs looping in NMI context. */
94258c5661fSHidehiro Kawai 	WRITE_ONCE(crash_ipi_issued, 1);
94358c5661fSHidehiro Kawai 
9442ddded21SEduardo Habkost 	msecs = 1000; /* Wait at most a second for the other cpus to stop */
9452ddded21SEduardo Habkost 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
9462ddded21SEduardo Habkost 		mdelay(1);
9472ddded21SEduardo Habkost 		msecs--;
9482ddded21SEduardo Habkost 	}
9492ddded21SEduardo Habkost 
95026044affSSean Christopherson 	/*
95126044affSSean Christopherson 	 * Leave the nmi callback set, shootdown is a one-time thing.  Clearing
95226044affSSean Christopherson 	 * the callback could result in a NULL pointer dereference if a CPU
95326044affSSean Christopherson 	 * (finally) responds after the timeout expires.
95426044affSSean Christopherson 	 */
95526044affSSean Christopherson }
95626044affSSean Christopherson 
95726044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void)
95826044affSSean Christopherson {
95926044affSSean Christopherson 	if (!crash_ipi_issued)
96026044affSSean Christopherson 		nmi_shootdown_cpus(NULL);
9612ddded21SEduardo Habkost }
96258c5661fSHidehiro Kawai 
963b279d67dSHidehiro Kawai /*
964b279d67dSHidehiro Kawai  * Check if the crash dumping IPI got issued and if so, call its callback
965b279d67dSHidehiro Kawai  * directly. This function is used when we have already been in NMI handler.
966b279d67dSHidehiro Kawai  * It doesn't return.
967b279d67dSHidehiro Kawai  */
968b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
969b279d67dSHidehiro Kawai {
970b279d67dSHidehiro Kawai 	if (crash_ipi_issued)
971b279d67dSHidehiro Kawai 		crash_nmi_callback(0, regs);
972b279d67dSHidehiro Kawai }
973b279d67dSHidehiro Kawai 
97458c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */
97527dea14cSJosh Poimboeuf void __noreturn nmi_panic_self_stop(struct pt_regs *regs)
97658c5661fSHidehiro Kawai {
97758c5661fSHidehiro Kawai 	while (1) {
978b279d67dSHidehiro Kawai 		/* If no CPU is preparing crash dump, we simply loop here. */
979b279d67dSHidehiro Kawai 		run_crash_ipi_callback(regs);
98058c5661fSHidehiro Kawai 		cpu_relax();
98158c5661fSHidehiro Kawai 	}
98258c5661fSHidehiro Kawai }
98358c5661fSHidehiro Kawai 
984bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */
985bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
986bb8dd270SEduardo Habkost {
987bb8dd270SEduardo Habkost 	/* No other CPUs to shoot down */
988bb8dd270SEduardo Habkost }
989b279d67dSHidehiro Kawai 
99026044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void) { }
99126044affSSean Christopherson 
992b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
993b279d67dSHidehiro Kawai {
994b279d67dSHidehiro Kawai }
9952ddded21SEduardo Habkost #endif
996