xref: /linux/arch/x86/kernel/reboot.c (revision 27dea14c7f05106f39850a9239874cd38703b405)
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>
154d022e35SMiguel Boton #include <acpi/reboot.h>
164d022e35SMiguel Boton #include <asm/io.h>
174d022e35SMiguel Boton #include <asm/apic.h>
188643e28dSJiang Liu #include <asm/io_apic.h>
194d022e35SMiguel Boton #include <asm/desc.h>
204d022e35SMiguel Boton #include <asm/hpet.h>
214412620fSDmitri Vorobiev #include <asm/proto.h>
224d022e35SMiguel Boton #include <asm/reboot_fixups.h>
234d022e35SMiguel Boton #include <asm/reboot.h>
2482487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
25d176720dSEduardo Habkost #include <asm/virtext.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 
533d81f952aSSean Christopherson static void emergency_reboot_disable_virtualization(void)
5344d022e35SMiguel Boton {
5354d022e35SMiguel Boton 	/* Just make sure we won't change CPUs while doing this */
5364d022e35SMiguel Boton 	local_irq_disable();
5374d022e35SMiguel Boton 
538144d102bSMichael D Labriola 	/*
539d81f952aSSean Christopherson 	 * Disable virtualization on all CPUs before rebooting to avoid hanging
540d81f952aSSean Christopherson 	 * the system, as VMX and SVM block INIT when running in the host.
5414d022e35SMiguel Boton 	 *
542ed727361SSean Christopherson 	 * We can't take any locks and we may be on an inconsistent state, so
543d81f952aSSean Christopherson 	 * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt.
5444d022e35SMiguel Boton 	 *
545d81f952aSSean Christopherson 	 * Do the NMI shootdown even if virtualization is off on _this_ CPU, as
546d81f952aSSean Christopherson 	 * other CPUs may have virtualization enabled.
5474d022e35SMiguel Boton 	 */
548d81f952aSSean Christopherson 	if (cpu_has_vmx() || cpu_has_svm(NULL)) {
549d81f952aSSean Christopherson 		/* Safely force _this_ CPU out of VMX/SVM operation. */
550d81f952aSSean Christopherson 		cpu_emergency_disable_virtualization();
5514d022e35SMiguel Boton 
552d81f952aSSean Christopherson 		/* Disable VMX/SVM and halt on other CPUs. */
55326044affSSean Christopherson 		nmi_shootdown_cpus_on_restart();
5544d022e35SMiguel Boton 	}
5554d022e35SMiguel Boton }
5564d022e35SMiguel Boton 
5574d022e35SMiguel Boton 
5584d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void)
5594d022e35SMiguel Boton {
5604d022e35SMiguel Boton }
5614d022e35SMiguel Boton 
562660e34ceSMatthew Garrett /*
5635be44a6fSIngo Molnar  * To the best of our knowledge Windows compatible x86 hardware expects
5645be44a6fSIngo Molnar  * the following on reboot:
565660e34ceSMatthew Garrett  *
566660e34ceSMatthew Garrett  * 1) If the FADT has the ACPI reboot register flag set, try it
567660e34ceSMatthew Garrett  * 2) If still alive, write to the keyboard controller
568660e34ceSMatthew Garrett  * 3) If still alive, write to the ACPI reboot register again
569660e34ceSMatthew Garrett  * 4) If still alive, write to the keyboard controller again
570a4f1987eSLi, Aubrey  * 5) If still alive, call the EFI runtime service to reboot
5715be44a6fSIngo Molnar  * 6) If no EFI runtime service, call the BIOS to do a reboot
572660e34ceSMatthew Garrett  *
5735be44a6fSIngo Molnar  * We default to following the same pattern. We also have
5745be44a6fSIngo Molnar  * two other reboot methods: 'triple fault' and 'PCI', which
5755be44a6fSIngo Molnar  * can be triggered via the reboot= kernel boot option or
5765be44a6fSIngo Molnar  * via quirks.
5775be44a6fSIngo Molnar  *
5785be44a6fSIngo Molnar  * This means that this function can never return, it can misbehave
5795be44a6fSIngo Molnar  * by not rebooting properly and hanging.
580660e34ceSMatthew Garrett  */
5814d022e35SMiguel Boton static void native_machine_emergency_restart(void)
5824d022e35SMiguel Boton {
5834d022e35SMiguel Boton 	int i;
584660e34ceSMatthew Garrett 	int attempt = 0;
585660e34ceSMatthew Garrett 	int orig_reboot_type = reboot_type;
586edf2b139SRobin Holt 	unsigned short mode;
5874d022e35SMiguel Boton 
588d176720dSEduardo Habkost 	if (reboot_emergency)
589d81f952aSSean Christopherson 		emergency_reboot_disable_virtualization();
590d176720dSEduardo Habkost 
591840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_REBOOT);
592840c2bafSJoseph Cihula 
5934d022e35SMiguel Boton 	/* Tell the BIOS if we want cold or warm reboot */
594edf2b139SRobin Holt 	mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
595edf2b139SRobin Holt 	*((unsigned short *)__va(0x472)) = mode;
5964d022e35SMiguel Boton 
59787615a34SMatt Fleming 	/*
59887615a34SMatt Fleming 	 * If an EFI capsule has been registered with the firmware then
59987615a34SMatt Fleming 	 * override the reboot= parameter.
60087615a34SMatt Fleming 	 */
60187615a34SMatt Fleming 	if (efi_capsule_pending(NULL)) {
60287615a34SMatt Fleming 		pr_info("EFI capsule is pending, forcing EFI reboot.\n");
60387615a34SMatt Fleming 		reboot_type = BOOT_EFI;
60487615a34SMatt Fleming 	}
60587615a34SMatt Fleming 
6064d022e35SMiguel Boton 	for (;;) {
6074d022e35SMiguel Boton 		/* Could also try the reset bit in the Hammer NB */
6084d022e35SMiguel Boton 		switch (reboot_type) {
6095be44a6fSIngo Molnar 		case BOOT_ACPI:
6105be44a6fSIngo Molnar 			acpi_reboot();
6115be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
6125be44a6fSIngo Molnar 			break;
6135be44a6fSIngo Molnar 
6144d022e35SMiguel Boton 		case BOOT_KBD:
615144d102bSMichael D Labriola 			mach_reboot_fixups(); /* For board specific fixups */
6167432d149SIngo Molnar 
6174d022e35SMiguel Boton 			for (i = 0; i < 10; i++) {
6184d022e35SMiguel Boton 				kb_wait();
6194d022e35SMiguel Boton 				udelay(50);
620144d102bSMichael D Labriola 				outb(0xfe, 0x64); /* Pulse reset low */
6214d022e35SMiguel Boton 				udelay(50);
6224d022e35SMiguel Boton 			}
623660e34ceSMatthew Garrett 			if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
624660e34ceSMatthew Garrett 				attempt = 1;
625660e34ceSMatthew Garrett 				reboot_type = BOOT_ACPI;
626660e34ceSMatthew Garrett 			} else {
627a4f1987eSLi, Aubrey 				reboot_type = BOOT_EFI;
628660e34ceSMatthew Garrett 			}
629660e34ceSMatthew Garrett 			break;
6304d022e35SMiguel Boton 
6314d022e35SMiguel Boton 		case BOOT_EFI:
6328562c99cSMatt Fleming 			efi_reboot(reboot_mode, NULL);
6335be44a6fSIngo Molnar 			reboot_type = BOOT_BIOS;
63414d7ca5cSH. Peter Anvin 			break;
6354d022e35SMiguel Boton 
6365be44a6fSIngo Molnar 		case BOOT_BIOS:
6375be44a6fSIngo Molnar 			machine_real_restart(MRR_BIOS);
6385be44a6fSIngo Molnar 
6395be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6405be44a6fSIngo Molnar 			reboot_type = BOOT_CF9_SAFE;
6415be44a6fSIngo Molnar 			break;
6425be44a6fSIngo Molnar 
6435be44a6fSIngo Molnar 		case BOOT_CF9_FORCE:
64414d7ca5cSH. Peter Anvin 			port_cf9_safe = true;
645df561f66SGustavo A. R. Silva 			fallthrough;
64614d7ca5cSH. Peter Anvin 
6475be44a6fSIngo Molnar 		case BOOT_CF9_SAFE:
64814d7ca5cSH. Peter Anvin 			if (port_cf9_safe) {
6495be44a6fSIngo Molnar 				u8 reboot_code = reboot_mode == REBOOT_WARM ?  0x06 : 0x0E;
65016c21ae5SLi Fei 				u8 cf9 = inb(0xcf9) & ~reboot_code;
65114d7ca5cSH. Peter Anvin 				outb(cf9|2, 0xcf9); /* Request hard reset */
65214d7ca5cSH. Peter Anvin 				udelay(50);
65316c21ae5SLi Fei 				/* Actually do the reset */
65416c21ae5SLi Fei 				outb(cf9|reboot_code, 0xcf9);
65514d7ca5cSH. Peter Anvin 				udelay(50);
65614d7ca5cSH. Peter Anvin 			}
6575be44a6fSIngo Molnar 			reboot_type = BOOT_TRIPLE;
6585be44a6fSIngo Molnar 			break;
6595be44a6fSIngo Molnar 
6605be44a6fSIngo Molnar 		case BOOT_TRIPLE:
6618ec9069aSH. Peter Anvin (Intel) 			idt_invalidate();
6625be44a6fSIngo Molnar 			__asm__ __volatile__("int3");
6635be44a6fSIngo Molnar 
6645be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6655be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
6664d022e35SMiguel Boton 			break;
6674d022e35SMiguel Boton 		}
6684d022e35SMiguel Boton 	}
6694d022e35SMiguel Boton }
6704d022e35SMiguel Boton 
6713c62c625SGlauber Costa void native_machine_shutdown(void)
6724d022e35SMiguel Boton {
6734d022e35SMiguel Boton 	/* Stop the cpus and apics */
674522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC
6752885432aSFenghua Yu 	/*
6762885432aSFenghua Yu 	 * Disabling IO APIC before local APIC is a workaround for
6772885432aSFenghua Yu 	 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
6782885432aSFenghua Yu 	 * Specification Update". In this situation, interrupts that target
6792885432aSFenghua Yu 	 * a Logical Processor whose Local APIC is either in the process of
6802885432aSFenghua Yu 	 * being hardware disabled or software disabled are neither delivered
6812885432aSFenghua Yu 	 * nor discarded. When this erratum occurs, the processor may hang.
6822885432aSFenghua Yu 	 *
6832885432aSFenghua Yu 	 * Even without the erratum, it still makes sense to quiet IO APIC
6842885432aSFenghua Yu 	 * before disabling Local APIC.
6852885432aSFenghua Yu 	 */
686339b2ae0SBaoquan He 	clear_IO_APIC();
687522e6646SFenghua Yu #endif
688522e6646SFenghua Yu 
6894d022e35SMiguel Boton #ifdef CONFIG_SMP
690144d102bSMichael D Labriola 	/*
6911b3a5d02SRobin Holt 	 * Stop all of the others. Also disable the local irq to
6921b3a5d02SRobin Holt 	 * not receive the per-cpu timer interrupt which may trigger
6931b3a5d02SRobin Holt 	 * scheduler's load balance.
6944d022e35SMiguel Boton 	 */
69555c844a4SFeng Tang 	local_irq_disable();
69676fac077SAlok Kataria 	stop_other_cpus();
6974d022e35SMiguel Boton #endif
6984d022e35SMiguel Boton 
6994d022e35SMiguel Boton 	lapic_shutdown();
700339b2ae0SBaoquan He 	restore_boot_irq_mode();
7014d022e35SMiguel Boton 
7024d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER
7034d022e35SMiguel Boton 	hpet_disable();
7044d022e35SMiguel Boton #endif
7054d022e35SMiguel Boton 
7064d022e35SMiguel Boton #ifdef CONFIG_X86_64
707338bac52SFUJITA Tomonori 	x86_platform.iommu_shutdown();
7084d022e35SMiguel Boton #endif
7094d022e35SMiguel Boton }
7104d022e35SMiguel Boton 
711d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency)
712d176720dSEduardo Habkost {
713d176720dSEduardo Habkost 	reboot_emergency = emergency;
714d176720dSEduardo Habkost 	machine_ops.emergency_restart();
715d176720dSEduardo Habkost }
716d176720dSEduardo Habkost 
717416e2d63SJody Belka static void native_machine_restart(char *__unused)
7184d022e35SMiguel Boton {
719c767a54bSJoe Perches 	pr_notice("machine restart\n");
7204d022e35SMiguel Boton 
7214d022e35SMiguel Boton 	if (!reboot_force)
7224d022e35SMiguel Boton 		machine_shutdown();
723d176720dSEduardo Habkost 	__machine_emergency_restart(0);
7244d022e35SMiguel Boton }
7254d022e35SMiguel Boton 
726416e2d63SJody Belka static void native_machine_halt(void)
7274d022e35SMiguel Boton {
728144d102bSMichael D Labriola 	/* Stop other cpus and apics */
729d3ec5caeSIvan Vecera 	machine_shutdown();
730d3ec5caeSIvan Vecera 
731840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
732840c2bafSJoseph Cihula 
733d3ec5caeSIvan Vecera 	stop_this_cpu(NULL);
7344d022e35SMiguel Boton }
7354d022e35SMiguel Boton 
736416e2d63SJody Belka static void native_machine_power_off(void)
7374d022e35SMiguel Boton {
738d3577349SDmitry Osipenko 	if (kernel_can_power_off()) {
7394d022e35SMiguel Boton 		if (!reboot_force)
7404d022e35SMiguel Boton 			machine_shutdown();
741d3577349SDmitry Osipenko 		do_kernel_power_off();
7424d022e35SMiguel Boton 	}
743144d102bSMichael D Labriola 	/* A fallback in case there is no PM info available */
744840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
7454d022e35SMiguel Boton }
7464d022e35SMiguel Boton 
747404f6aacSKees Cook struct machine_ops machine_ops __ro_after_init = {
748416e2d63SJody Belka 	.power_off = native_machine_power_off,
749416e2d63SJody Belka 	.shutdown = native_machine_shutdown,
750416e2d63SJody Belka 	.emergency_restart = native_machine_emergency_restart,
751416e2d63SJody Belka 	.restart = native_machine_restart,
752ed23dc6fSGlauber Costa 	.halt = native_machine_halt,
7532965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
754ed23dc6fSGlauber Costa 	.crash_shutdown = native_machine_crash_shutdown,
755ed23dc6fSGlauber Costa #endif
7564d022e35SMiguel Boton };
757416e2d63SJody Belka 
758416e2d63SJody Belka void machine_power_off(void)
759416e2d63SJody Belka {
760416e2d63SJody Belka 	machine_ops.power_off();
761416e2d63SJody Belka }
762416e2d63SJody Belka 
763416e2d63SJody Belka void machine_shutdown(void)
764416e2d63SJody Belka {
765416e2d63SJody Belka 	machine_ops.shutdown();
766416e2d63SJody Belka }
767416e2d63SJody Belka 
768416e2d63SJody Belka void machine_emergency_restart(void)
769416e2d63SJody Belka {
770d176720dSEduardo Habkost 	__machine_emergency_restart(1);
771416e2d63SJody Belka }
772416e2d63SJody Belka 
773416e2d63SJody Belka void machine_restart(char *cmd)
774416e2d63SJody Belka {
775416e2d63SJody Belka 	machine_ops.restart(cmd);
776416e2d63SJody Belka }
777416e2d63SJody Belka 
778416e2d63SJody Belka void machine_halt(void)
779416e2d63SJody Belka {
780416e2d63SJody Belka 	machine_ops.halt();
781416e2d63SJody Belka }
782416e2d63SJody Belka 
7832965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
784ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs)
785ed23dc6fSGlauber Costa {
786ed23dc6fSGlauber Costa 	machine_ops.crash_shutdown(regs);
787ed23dc6fSGlauber Costa }
788ed23dc6fSGlauber Costa #endif
7892ddded21SEduardo Habkost 
7902ddded21SEduardo Habkost 
7915bc32950SXunlei Pang /* This is the CPU performing the emergency shutdown work. */
7925bc32950SXunlei Pang int crashing_cpu = -1;
7935bc32950SXunlei Pang 
79426044affSSean Christopherson /*
79526044affSSean Christopherson  * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
79626044affSSean Christopherson  * reboot.  VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
79726044affSSean Christopherson  * GIF=0, i.e. if the crash occurred between CLGI and STGI.
79826044affSSean Christopherson  */
79926044affSSean Christopherson void cpu_emergency_disable_virtualization(void)
80026044affSSean Christopherson {
80126044affSSean Christopherson 	cpu_emergency_vmxoff();
80226044affSSean Christopherson 	cpu_emergency_svm_disable();
80326044affSSean Christopherson }
80426044affSSean Christopherson 
805bb8dd270SEduardo Habkost #if defined(CONFIG_SMP)
8062ddded21SEduardo Habkost 
8072ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback;
8082ddded21SEduardo Habkost 
8092ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi;
81058c5661fSHidehiro Kawai static int crash_ipi_issued;
8112ddded21SEduardo Habkost 
8129c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
8132ddded21SEduardo Habkost {
8142ddded21SEduardo Habkost 	int cpu;
8152ddded21SEduardo Habkost 
8162ddded21SEduardo Habkost 	cpu = raw_smp_processor_id();
8172ddded21SEduardo Habkost 
818144d102bSMichael D Labriola 	/*
819144d102bSMichael D Labriola 	 * Don't do anything if this handler is invoked on crashing cpu.
8202ddded21SEduardo Habkost 	 * Otherwise, system will completely hang. Crashing cpu can get
8212ddded21SEduardo Habkost 	 * an NMI if system was initially booted with nmi_watchdog parameter.
8222ddded21SEduardo Habkost 	 */
8232ddded21SEduardo Habkost 	if (cpu == crashing_cpu)
8249c48f1c6SDon Zickus 		return NMI_HANDLED;
8252ddded21SEduardo Habkost 	local_irq_disable();
8262ddded21SEduardo Habkost 
82726044affSSean Christopherson 	if (shootdown_callback)
8289c48f1c6SDon Zickus 		shootdown_callback(cpu, regs);
8292ddded21SEduardo Habkost 
83026044affSSean Christopherson 	/*
83126044affSSean Christopherson 	 * Prepare the CPU for reboot _after_ invoking the callback so that the
83226044affSSean Christopherson 	 * callback can safely use virtualization instructions, e.g. VMCLEAR.
83326044affSSean Christopherson 	 */
83426044affSSean Christopherson 	cpu_emergency_disable_virtualization();
83526044affSSean Christopherson 
8362ddded21SEduardo Habkost 	atomic_dec(&waiting_for_crash_ipi);
8372ddded21SEduardo Habkost 	/* Assume hlt works */
8382ddded21SEduardo Habkost 	halt();
8392ddded21SEduardo Habkost 	for (;;)
8402ddded21SEduardo Habkost 		cpu_relax();
8412ddded21SEduardo Habkost 
8429c48f1c6SDon Zickus 	return NMI_HANDLED;
8432ddded21SEduardo Habkost }
8442ddded21SEduardo Habkost 
84526044affSSean Christopherson /**
84626044affSSean Christopherson  * nmi_shootdown_cpus - Stop other CPUs via NMI
84726044affSSean Christopherson  * @callback:	Optional callback to be invoked from the NMI handler
848bb8dd270SEduardo Habkost  *
84926044affSSean Christopherson  * The NMI handler on the remote CPUs invokes @callback, if not
85026044affSSean Christopherson  * NULL, first and then disables virtualization to ensure that
85126044affSSean Christopherson  * INIT is recognized during reboot.
85226044affSSean Christopherson  *
85326044affSSean Christopherson  * nmi_shootdown_cpus() can only be invoked once. After the first
85426044affSSean Christopherson  * invocation all other CPUs are stuck in crash_nmi_callback() and
85526044affSSean Christopherson  * cannot respond to a second NMI.
856bb8dd270SEduardo Habkost  */
8572ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
8582ddded21SEduardo Habkost {
8592ddded21SEduardo Habkost 	unsigned long msecs;
86026044affSSean Christopherson 
861c415b3dcSEduardo Habkost 	local_irq_disable();
8622ddded21SEduardo Habkost 
86326044affSSean Christopherson 	/*
86426044affSSean Christopherson 	 * Avoid certain doom if a shootdown already occurred; re-registering
86526044affSSean Christopherson 	 * the NMI handler will cause list corruption, modifying the callback
86626044affSSean Christopherson 	 * will do who knows what, etc...
86726044affSSean Christopherson 	 */
86826044affSSean Christopherson 	if (WARN_ON_ONCE(crash_ipi_issued))
86926044affSSean Christopherson 		return;
87026044affSSean Christopherson 
8712ddded21SEduardo Habkost 	/* Make a note of crashing cpu. Will be used in NMI callback. */
8722ddded21SEduardo Habkost 	crashing_cpu = safe_smp_processor_id();
8732ddded21SEduardo Habkost 
8742ddded21SEduardo Habkost 	shootdown_callback = callback;
8752ddded21SEduardo Habkost 
8762ddded21SEduardo Habkost 	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
8772ddded21SEduardo Habkost 	/* Would it be better to replace the trap vector here? */
8789c48f1c6SDon Zickus 	if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
8799c48f1c6SDon Zickus 				 NMI_FLAG_FIRST, "crash"))
880144d102bSMichael D Labriola 		return;		/* Return what? */
881144d102bSMichael D Labriola 	/*
882144d102bSMichael D Labriola 	 * Ensure the new callback function is set before sending
8832ddded21SEduardo Habkost 	 * out the NMI
8842ddded21SEduardo Habkost 	 */
8852ddded21SEduardo Habkost 	wmb();
8862ddded21SEduardo Habkost 
88722ca7ee9SThomas Gleixner 	apic_send_IPI_allbutself(NMI_VECTOR);
8882ddded21SEduardo Habkost 
88958c5661fSHidehiro Kawai 	/* Kick CPUs looping in NMI context. */
89058c5661fSHidehiro Kawai 	WRITE_ONCE(crash_ipi_issued, 1);
89158c5661fSHidehiro Kawai 
8922ddded21SEduardo Habkost 	msecs = 1000; /* Wait at most a second for the other cpus to stop */
8932ddded21SEduardo Habkost 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
8942ddded21SEduardo Habkost 		mdelay(1);
8952ddded21SEduardo Habkost 		msecs--;
8962ddded21SEduardo Habkost 	}
8972ddded21SEduardo Habkost 
89826044affSSean Christopherson 	/*
89926044affSSean Christopherson 	 * Leave the nmi callback set, shootdown is a one-time thing.  Clearing
90026044affSSean Christopherson 	 * the callback could result in a NULL pointer dereference if a CPU
90126044affSSean Christopherson 	 * (finally) responds after the timeout expires.
90226044affSSean Christopherson 	 */
90326044affSSean Christopherson }
90426044affSSean Christopherson 
90526044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void)
90626044affSSean Christopherson {
90726044affSSean Christopherson 	if (!crash_ipi_issued)
90826044affSSean Christopherson 		nmi_shootdown_cpus(NULL);
9092ddded21SEduardo Habkost }
91058c5661fSHidehiro Kawai 
911b279d67dSHidehiro Kawai /*
912b279d67dSHidehiro Kawai  * Check if the crash dumping IPI got issued and if so, call its callback
913b279d67dSHidehiro Kawai  * directly. This function is used when we have already been in NMI handler.
914b279d67dSHidehiro Kawai  * It doesn't return.
915b279d67dSHidehiro Kawai  */
916b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
917b279d67dSHidehiro Kawai {
918b279d67dSHidehiro Kawai 	if (crash_ipi_issued)
919b279d67dSHidehiro Kawai 		crash_nmi_callback(0, regs);
920b279d67dSHidehiro Kawai }
921b279d67dSHidehiro Kawai 
92258c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */
923*27dea14cSJosh Poimboeuf void __noreturn nmi_panic_self_stop(struct pt_regs *regs)
92458c5661fSHidehiro Kawai {
92558c5661fSHidehiro Kawai 	while (1) {
926b279d67dSHidehiro Kawai 		/* If no CPU is preparing crash dump, we simply loop here. */
927b279d67dSHidehiro Kawai 		run_crash_ipi_callback(regs);
92858c5661fSHidehiro Kawai 		cpu_relax();
92958c5661fSHidehiro Kawai 	}
93058c5661fSHidehiro Kawai }
93158c5661fSHidehiro Kawai 
932bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */
933bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
934bb8dd270SEduardo Habkost {
935bb8dd270SEduardo Habkost 	/* No other CPUs to shoot down */
936bb8dd270SEduardo Habkost }
937b279d67dSHidehiro Kawai 
93826044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void) { }
93926044affSSean Christopherson 
940b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
941b279d67dSHidehiro Kawai {
942b279d67dSHidehiro Kawai }
9432ddded21SEduardo Habkost #endif
944