xref: /linux/arch/x86/kernel/reboot.c (revision 8ec9069a432c873e52e6f4ce1496f282a4299604)
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 	/*
11657b16594SMichael D Labriola 	 * Switch back to the initial page table.
11757b16594SMichael D Labriola 	 */
11865051397SH. Peter Anvin #ifdef CONFIG_X86_32
11957b16594SMichael D Labriola 	load_cr3(initial_page_table);
12065051397SH. Peter Anvin #else
12165051397SH. Peter Anvin 	write_cr3(real_mode_header->trampoline_pgd);
122924c6b90SAndy Lutomirski 
123924c6b90SAndy Lutomirski 	/* Exiting long mode will fail if CR4.PCIDE is set. */
12467e87d43SBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_PCID))
125924c6b90SAndy Lutomirski 		cr4_clear_bits(X86_CR4_PCIDE);
12665051397SH. Peter Anvin #endif
12757b16594SMichael D Labriola 
12857b16594SMichael D Labriola 	/* Jump to the identity-mapped low memory code */
12965051397SH. Peter Anvin #ifdef CONFIG_X86_32
13065051397SH. Peter Anvin 	asm volatile("jmpl *%0" : :
13165051397SH. Peter Anvin 		     "rm" (real_mode_header->machine_real_restart_asm),
13265051397SH. Peter Anvin 		     "a" (type));
13365051397SH. Peter Anvin #else
13465051397SH. Peter Anvin 	asm volatile("ljmpl *%0" : :
13565051397SH. Peter Anvin 		     "m" (real_mode_header->machine_real_restart_asm),
13665051397SH. Peter Anvin 		     "D" (type));
13765051397SH. Peter Anvin #endif
13865051397SH. Peter Anvin 	unreachable();
13957b16594SMichael D Labriola }
14057b16594SMichael D Labriola #ifdef CONFIG_APM_MODULE
14157b16594SMichael D Labriola EXPORT_SYMBOL(machine_real_restart);
14257b16594SMichael D Labriola #endif
143c207aee4SJosh Poimboeuf STACK_FRAME_NON_STANDARD(machine_real_restart);
14457b16594SMichael D Labriola 
14557b16594SMichael D Labriola /*
14657b16594SMichael D Labriola  * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
14757b16594SMichael D Labriola  */
14857b16594SMichael D Labriola static int __init set_pci_reboot(const struct dmi_system_id *d)
14957b16594SMichael D Labriola {
1505be44a6fSIngo Molnar 	if (reboot_type != BOOT_CF9_FORCE) {
1515be44a6fSIngo Molnar 		reboot_type = BOOT_CF9_FORCE;
152c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
1536d9153bbSLan Tianyu 			d->ident, "PCI");
15457b16594SMichael D Labriola 	}
15557b16594SMichael D Labriola 	return 0;
15657b16594SMichael D Labriola }
15757b16594SMichael D Labriola 
1581ef03890SPeter Chubb static int __init set_kbd_reboot(const struct dmi_system_id *d)
1591ef03890SPeter Chubb {
1601ef03890SPeter Chubb 	if (reboot_type != BOOT_KBD) {
1611ef03890SPeter Chubb 		reboot_type = BOOT_KBD;
162c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboot.\n",
1636d9153bbSLan Tianyu 			d->ident, "KBD");
1641ef03890SPeter Chubb 	}
1651ef03890SPeter Chubb 	return 0;
1661ef03890SPeter Chubb }
1671ef03890SPeter Chubb 
168144d102bSMichael D Labriola /*
16965051397SH. Peter Anvin  * This is a single dmi_table handling all reboot quirks.
17057b16594SMichael D Labriola  */
1716faadbbbSChristoph Hellwig static const struct dmi_system_id reboot_dmi_table[] __initconst = {
17257b16594SMichael D Labriola 
173e56e57f6SDave Jones 	/* Acer */
174b49c78d4SPeter Chubb 	{	/* Handle reboot issue on Acer Aspire one */
1751ef03890SPeter Chubb 		.callback = set_kbd_reboot,
176b49c78d4SPeter Chubb 		.ident = "Acer Aspire One A110",
177b49c78d4SPeter Chubb 		.matches = {
178b49c78d4SPeter Chubb 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
179b49c78d4SPeter Chubb 			DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
180b49c78d4SPeter Chubb 		},
181b49c78d4SPeter Chubb 	},
1820082517fSJian-Hong Pan 	{	/* Handle reboot issue on Acer TravelMate X514-51T */
1830082517fSJian-Hong Pan 		.callback = set_efi_reboot,
1840082517fSJian-Hong Pan 		.ident = "Acer TravelMate X514-51T",
1850082517fSJian-Hong Pan 		.matches = {
1860082517fSJian-Hong Pan 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1870082517fSJian-Hong Pan 			DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate X514-51T"),
1880082517fSJian-Hong Pan 		},
1890082517fSJian-Hong Pan 	},
190e56e57f6SDave Jones 
191e56e57f6SDave Jones 	/* Apple */
1923e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBook5 */
1936c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
1943e03bbeaSShunichi Fuji 		.ident = "Apple MacBook5",
1956c6c51e4SPaul Mackerras 		.matches = {
1966c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1973e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
1986c6c51e4SPaul Mackerras 		},
1996c6c51e4SPaul Mackerras 	},
200140fd4acSHill Ma 	{	/* Handle problems with rebooting on Apple MacBook6,1 */
201140fd4acSHill Ma 		.callback = set_pci_reboot,
202140fd4acSHill Ma 		.ident = "Apple MacBook6,1",
203140fd4acSHill Ma 		.matches = {
204140fd4acSHill Ma 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
205140fd4acSHill Ma 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"),
206140fd4acSHill Ma 		},
207140fd4acSHill Ma 	},
2083e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBookPro5 */
209498cdbfbSOzan Çağlayan 		.callback = set_pci_reboot,
2103e03bbeaSShunichi Fuji 		.ident = "Apple MacBookPro5",
211498cdbfbSOzan Çağlayan 		.matches = {
212498cdbfbSOzan Çağlayan 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2133e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
214498cdbfbSOzan Çağlayan 		},
215498cdbfbSOzan Çağlayan 	},
21605154752SGottfried Haider 	{	/* Handle problems with rebooting on Apple Macmini3,1 */
21705154752SGottfried Haider 		.callback = set_pci_reboot,
21805154752SGottfried Haider 		.ident = "Apple Macmini3,1",
21905154752SGottfried Haider 		.matches = {
22005154752SGottfried Haider 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
22105154752SGottfried Haider 			DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
22205154752SGottfried Haider 		},
22305154752SGottfried Haider 	},
2240a832320SJustin P. Mattock 	{	/* Handle problems with rebooting on the iMac9,1. */
2250a832320SJustin P. Mattock 		.callback = set_pci_reboot,
2260a832320SJustin P. Mattock 		.ident = "Apple iMac9,1",
2270a832320SJustin P. Mattock 		.matches = {
2280a832320SJustin P. Mattock 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2290a832320SJustin P. Mattock 			DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
2300a832320SJustin P. Mattock 		},
2310a832320SJustin P. Mattock 	},
2322f0c0b2dSMario Kleiner 	{	/* Handle problems with rebooting on the iMac10,1. */
2332f0c0b2dSMario Kleiner 		.callback = set_pci_reboot,
2342f0c0b2dSMario Kleiner 		.ident = "Apple iMac10,1",
2352f0c0b2dSMario Kleiner 		.matches = {
2362f0c0b2dSMario Kleiner 		    DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2372f0c0b2dSMario Kleiner 		    DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
2382f0c0b2dSMario Kleiner 		},
2392f0c0b2dSMario Kleiner 	},
240e56e57f6SDave Jones 
24180313b30SStefan Lippers-Hollmann 	/* ASRock */
24280313b30SStefan Lippers-Hollmann 	{	/* Handle problems with rebooting on ASRock Q1900DC-ITX */
24380313b30SStefan Lippers-Hollmann 		.callback = set_pci_reboot,
24480313b30SStefan Lippers-Hollmann 		.ident = "ASRock Q1900DC-ITX",
24580313b30SStefan Lippers-Hollmann 		.matches = {
24680313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
24780313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
24880313b30SStefan Lippers-Hollmann 		},
24980313b30SStefan Lippers-Hollmann 	},
25080313b30SStefan Lippers-Hollmann 
251e56e57f6SDave Jones 	/* ASUS */
252e56e57f6SDave Jones 	{	/* Handle problems with rebooting on ASUS P4S800 */
253e56e57f6SDave Jones 		.callback = set_bios_reboot,
254e56e57f6SDave Jones 		.ident = "ASUS P4S800",
255e56e57f6SDave Jones 		.matches = {
256e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
257e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
258e56e57f6SDave Jones 		},
259e56e57f6SDave Jones 	},
26090b28dedSMatjaz Hegedic 	{	/* Handle problems with rebooting on ASUS EeeBook X205TA */
26190b28dedSMatjaz Hegedic 		.callback = set_acpi_reboot,
26290b28dedSMatjaz Hegedic 		.ident = "ASUS EeeBook X205TA",
26390b28dedSMatjaz Hegedic 		.matches = {
26490b28dedSMatjaz Hegedic 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
265bba8376aSMatjaz Hegedic 			DMI_MATCH(DMI_PRODUCT_NAME, "X205TA"),
26690b28dedSMatjaz Hegedic 		},
26790b28dedSMatjaz Hegedic 	},
2683b3e7855SMatjaz Hegedic 	{	/* Handle problems with rebooting on ASUS EeeBook X205TAW */
2693b3e7855SMatjaz Hegedic 		.callback = set_acpi_reboot,
2703b3e7855SMatjaz Hegedic 		.ident = "ASUS EeeBook X205TAW",
2713b3e7855SMatjaz Hegedic 		.matches = {
2723b3e7855SMatjaz Hegedic 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
2733b3e7855SMatjaz Hegedic 			DMI_MATCH(DMI_PRODUCT_NAME, "X205TAW"),
2743b3e7855SMatjaz Hegedic 		},
2753b3e7855SMatjaz Hegedic 	},
276e56e57f6SDave Jones 
277aadca6faSChristian Gmeiner 	/* Certec */
278aadca6faSChristian Gmeiner 	{       /* Handle problems with rebooting on Certec BPC600 */
279aadca6faSChristian Gmeiner 		.callback = set_pci_reboot,
280aadca6faSChristian Gmeiner 		.ident = "Certec BPC600",
281aadca6faSChristian Gmeiner 		.matches = {
282aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
283aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
284aadca6faSChristian Gmeiner 		},
285aadca6faSChristian Gmeiner 	},
286aadca6faSChristian Gmeiner 
287e56e57f6SDave Jones 	/* Dell */
288e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell DXP061 */
289e56e57f6SDave Jones 		.callback = set_bios_reboot,
290e56e57f6SDave Jones 		.ident = "Dell DXP061",
2913628c3f5SMaxime Ripard 		.matches = {
2923628c3f5SMaxime Ripard 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
293e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
294e56e57f6SDave Jones 		},
295e56e57f6SDave Jones 	},
296e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell E520's */
297e56e57f6SDave Jones 		.callback = set_bios_reboot,
298e56e57f6SDave Jones 		.ident = "Dell E520",
299e56e57f6SDave Jones 		.matches = {
300e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
301e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
3023628c3f5SMaxime Ripard 		},
3033628c3f5SMaxime Ripard 	},
3048412da75SVille Syrjälä 	{	/* Handle problems with rebooting on the Latitude E5410. */
3058412da75SVille Syrjälä 		.callback = set_pci_reboot,
3068412da75SVille Syrjälä 		.ident = "Dell Latitude E5410",
3078412da75SVille Syrjälä 		.matches = {
3088412da75SVille Syrjälä 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3098412da75SVille Syrjälä 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
3108412da75SVille Syrjälä 		},
3118412da75SVille Syrjälä 	},
312b7798d28SDaniel J Blueman 	{	/* Handle problems with rebooting on the Latitude E5420. */
313b7798d28SDaniel J Blueman 		.callback = set_pci_reboot,
314b7798d28SDaniel J Blueman 		.ident = "Dell Latitude E5420",
315b7798d28SDaniel J Blueman 		.matches = {
316b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
317b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
318b7798d28SDaniel J Blueman 		},
319b7798d28SDaniel J Blueman 	},
320e56e57f6SDave Jones 	{	/* Handle problems with rebooting on the Latitude E6320. */
321e56e57f6SDave Jones 		.callback = set_pci_reboot,
322e56e57f6SDave Jones 		.ident = "Dell Latitude E6320",
323e56e57f6SDave Jones 		.matches = {
324e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
325e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
326e56e57f6SDave Jones 		},
327e56e57f6SDave Jones 	},
328a536877eSH. Peter Anvin 	{	/* Handle problems with rebooting on the Latitude E6420. */
329a536877eSH. Peter Anvin 		.callback = set_pci_reboot,
330a536877eSH. Peter Anvin 		.ident = "Dell Latitude E6420",
331a536877eSH. Peter Anvin 		.matches = {
332a536877eSH. Peter Anvin 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
333a536877eSH. Peter Anvin 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
334a536877eSH. Peter Anvin 		},
335a536877eSH. Peter Anvin 	},
336e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
337e56e57f6SDave Jones 		.callback = set_bios_reboot,
338e56e57f6SDave Jones 		.ident = "Dell OptiPlex 330",
339e56e57f6SDave Jones 		.matches = {
340e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
341e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
342e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
343e56e57f6SDave Jones 		},
344e56e57f6SDave Jones 	},
345e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
346e56e57f6SDave Jones 		.callback = set_bios_reboot,
347e56e57f6SDave Jones 		.ident = "Dell OptiPlex 360",
348e56e57f6SDave Jones 		.matches = {
349e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
350e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
351e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
352e56e57f6SDave Jones 		},
353e56e57f6SDave Jones 	},
354e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's SFF */
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 		},
361e56e57f6SDave Jones 	},
362e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's DFF */
363e56e57f6SDave Jones 		.callback = set_bios_reboot,
364e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
365e56e57f6SDave Jones 		.matches = {
366e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
367e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
368e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
369e56e57f6SDave Jones 		},
370e56e57f6SDave Jones 	},
371e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
372e56e57f6SDave Jones 		.callback = set_bios_reboot,
373e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
374e56e57f6SDave Jones 		.matches = {
375e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
376e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
377e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
378e56e57f6SDave Jones 		},
379e56e57f6SDave Jones 	},
380e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
381e56e57f6SDave Jones 		.callback = set_bios_reboot,
382e56e57f6SDave Jones 		.ident = "Dell OptiPlex 760",
383e56e57f6SDave Jones 		.matches = {
384e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
385e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
386e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
387e56e57f6SDave Jones 		},
388e56e57f6SDave Jones 	},
3896be30bb7SRafael J. Wysocki 	{	/* Handle problems with rebooting on the OptiPlex 990. */
3906be30bb7SRafael J. Wysocki 		.callback = set_pci_reboot,
3916be30bb7SRafael J. Wysocki 		.ident = "Dell OptiPlex 990",
3926be30bb7SRafael J. Wysocki 		.matches = {
3936be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3946be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
3956be30bb7SRafael J. Wysocki 		},
3966be30bb7SRafael J. Wysocki 	},
397e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 300's */
398e56e57f6SDave Jones 		.callback = set_bios_reboot,
399e56e57f6SDave Jones 		.ident = "Dell PowerEdge 300",
40076eb9a30SZhang Rui 		.matches = {
401e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
402e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
403e56e57f6SDave Jones 		},
404e56e57f6SDave Jones 	},
405e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 1300's */
406e56e57f6SDave Jones 		.callback = set_bios_reboot,
407e56e57f6SDave Jones 		.ident = "Dell PowerEdge 1300",
408e56e57f6SDave Jones 		.matches = {
409e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
410e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
411e56e57f6SDave Jones 		},
412e56e57f6SDave Jones 	},
413e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 2400's */
414e56e57f6SDave Jones 		.callback = set_bios_reboot,
415e56e57f6SDave Jones 		.ident = "Dell PowerEdge 2400",
416e56e57f6SDave Jones 		.matches = {
417e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
418e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
41976eb9a30SZhang Rui 		},
42076eb9a30SZhang Rui 	},
4214f0acd31SMasoud Sharbiani 	{	/* Handle problems with rebooting on the Dell PowerEdge C6100. */
4224f0acd31SMasoud Sharbiani 		.callback = set_pci_reboot,
4234f0acd31SMasoud Sharbiani 		.ident = "Dell PowerEdge C6100",
4244f0acd31SMasoud Sharbiani 		.matches = {
4254f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
4264f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
4274f0acd31SMasoud Sharbiani 		},
4284f0acd31SMasoud Sharbiani 	},
4296c6c51e4SPaul Mackerras 	{	/* Handle problems with rebooting on the Precision M6600. */
4306c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
4316c6c51e4SPaul Mackerras 		.ident = "Dell Precision M6600",
4326c6c51e4SPaul Mackerras 		.matches = {
4336c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4346c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
4356c6c51e4SPaul Mackerras 		},
4366c6c51e4SPaul Mackerras 	},
437e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T5400's */
438e56e57f6SDave Jones 		.callback = set_bios_reboot,
439e56e57f6SDave Jones 		.ident = "Dell Precision T5400",
44057b16594SMichael D Labriola 		.matches = {
4416c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
442e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
443144d102bSMichael D Labriola 		},
4445955633eSMichael D Labriola 	},
445e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T7400's */
446e56e57f6SDave Jones 		.callback = set_bios_reboot,
447e56e57f6SDave Jones 		.ident = "Dell Precision T7400",
4486c6c51e4SPaul Mackerras 		.matches = {
449e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
450e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
4516c6c51e4SPaul Mackerras 		},
4524d022e35SMiguel Boton 	},
453e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell XPS710 */
454e56e57f6SDave Jones 		.callback = set_bios_reboot,
455e56e57f6SDave Jones 		.ident = "Dell XPS710",
456e56e57f6SDave Jones 		.matches = {
457e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
458e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
459e56e57f6SDave Jones 		},
460e56e57f6SDave Jones 	},
4614d581259SAlex Hung 	{	/* Handle problems with rebooting on Dell Optiplex 7450 AIO */
4624d581259SAlex Hung 		.callback = set_acpi_reboot,
4634d581259SAlex Hung 		.ident = "Dell OptiPlex 7450 AIO",
4644d581259SAlex Hung 		.matches = {
4654d581259SAlex Hung 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4664d581259SAlex Hung 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7450 AIO"),
4674d581259SAlex Hung 		},
4684d581259SAlex Hung 	},
469e56e57f6SDave Jones 
470e56e57f6SDave Jones 	/* Hewlett-Packard */
471e56e57f6SDave Jones 	{	/* Handle problems with rebooting on HP laptops */
472e56e57f6SDave Jones 		.callback = set_bios_reboot,
473e56e57f6SDave Jones 		.ident = "HP Compaq Laptop",
474e56e57f6SDave Jones 		.matches = {
475e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
476e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
477e56e57f6SDave Jones 		},
478e56e57f6SDave Jones 	},
479e56e57f6SDave Jones 
4804b2d8ca9SHeiner Kallweit 	{	/* PCIe Wifi card isn't detected after reboot otherwise */
4814b2d8ca9SHeiner Kallweit 		.callback = set_pci_reboot,
4824b2d8ca9SHeiner Kallweit 		.ident = "Zotac ZBOX CI327 nano",
4834b2d8ca9SHeiner Kallweit 		.matches = {
4844b2d8ca9SHeiner Kallweit 			DMI_MATCH(DMI_SYS_VENDOR, "NA"),
4854b2d8ca9SHeiner Kallweit 			DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
4864b2d8ca9SHeiner Kallweit 		},
4874b2d8ca9SHeiner Kallweit 	},
4884b2d8ca9SHeiner Kallweit 
489e56e57f6SDave Jones 	/* Sony */
490e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Sony VGN-Z540N */
491e56e57f6SDave Jones 		.callback = set_bios_reboot,
492e56e57f6SDave Jones 		.ident = "Sony VGN-Z540N",
493e56e57f6SDave Jones 		.matches = {
494e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
495e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
496e56e57f6SDave Jones 		},
497e56e57f6SDave Jones 	},
498e56e57f6SDave Jones 
4994d022e35SMiguel Boton 	{ }
5004d022e35SMiguel Boton };
5019c48f1c6SDon Zickus 
502d176720dSEduardo Habkost static int __init reboot_init(void)
503d176720dSEduardo Habkost {
50444be28e9SMatt Fleming 	int rv;
50544be28e9SMatt Fleming 
506d176720dSEduardo Habkost 	/*
507d176720dSEduardo Habkost 	 * Only do the DMI check if reboot_type hasn't been overridden
508d176720dSEduardo Habkost 	 * on the command line
509d176720dSEduardo Habkost 	 */
51044be28e9SMatt Fleming 	if (!reboot_default)
51144be28e9SMatt Fleming 		return 0;
51244be28e9SMatt Fleming 
51344be28e9SMatt Fleming 	/*
51444be28e9SMatt Fleming 	 * The DMI quirks table takes precedence. If no quirks entry
5154ecf7191SStefan Assmann 	 * matches and the ACPI Hardware Reduced bit is set and EFI
5164ecf7191SStefan Assmann 	 * runtime services are enabled, force EFI reboot.
51744be28e9SMatt Fleming 	 */
51844be28e9SMatt Fleming 	rv = dmi_check_system(reboot_dmi_table);
51944be28e9SMatt Fleming 
5204ecf7191SStefan Assmann 	if (!rv && efi_reboot_required() && !efi_runtime_disabled())
52144be28e9SMatt Fleming 		reboot_type = BOOT_EFI;
52244be28e9SMatt Fleming 
523d176720dSEduardo Habkost 	return 0;
524d176720dSEduardo Habkost }
525d176720dSEduardo Habkost core_initcall(reboot_init);
526d176720dSEduardo Habkost 
527d176720dSEduardo Habkost static inline void kb_wait(void)
528d176720dSEduardo Habkost {
529d176720dSEduardo Habkost 	int i;
530d176720dSEduardo Habkost 
531d176720dSEduardo Habkost 	for (i = 0; i < 0x10000; i++) {
5327432d149SIngo Molnar 		if ((inb(0x64) & 0x02) == 0)
5337432d149SIngo Molnar 			break;
5347432d149SIngo Molnar 		udelay(2);
5357432d149SIngo Molnar 	}
536416e2d63SJody Belka }
5374d022e35SMiguel Boton 
5384d022e35SMiguel Boton static void vmxoff_nmi(int cpu, struct pt_regs *regs)
5394d022e35SMiguel Boton {
540d176720dSEduardo Habkost 	cpu_emergency_vmxoff();
541d176720dSEduardo Habkost }
542d176720dSEduardo Habkost 
543144d102bSMichael D Labriola /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
5444d022e35SMiguel Boton static void emergency_vmx_disable_all(void)
5454d022e35SMiguel Boton {
5464d022e35SMiguel Boton 	/* Just make sure we won't change CPUs while doing this */
5474d022e35SMiguel Boton 	local_irq_disable();
5484d022e35SMiguel Boton 
549144d102bSMichael D Labriola 	/*
550ed727361SSean Christopherson 	 * Disable VMX on all CPUs before rebooting, otherwise we risk hanging
551ed727361SSean Christopherson 	 * the machine, because the CPU blocks INIT when it's in VMX root.
5524d022e35SMiguel Boton 	 *
553ed727361SSean Christopherson 	 * We can't take any locks and we may be on an inconsistent state, so
554ed727361SSean Christopherson 	 * use NMIs as IPIs to tell the other CPUs to exit VMX root and halt.
5554d022e35SMiguel Boton 	 *
556ed727361SSean Christopherson 	 * Do the NMI shootdown even if VMX if off on _this_ CPU, as that
557ed727361SSean Christopherson 	 * doesn't prevent a different CPU from being in VMX root operation.
5584d022e35SMiguel Boton 	 */
559ed727361SSean Christopherson 	if (cpu_has_vmx()) {
560ed727361SSean Christopherson 		/* Safely force _this_ CPU out of VMX root operation. */
561ed727361SSean Christopherson 		__cpu_emergency_vmxoff();
5624d022e35SMiguel Boton 
563ed727361SSean Christopherson 		/* Halt and exit VMX root operation on the other CPUs. */
5644d022e35SMiguel Boton 		nmi_shootdown_cpus(vmxoff_nmi);
5654d022e35SMiguel Boton 	}
5664d022e35SMiguel Boton }
5674d022e35SMiguel Boton 
5684d022e35SMiguel Boton 
5694d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void)
5704d022e35SMiguel Boton {
5714d022e35SMiguel Boton }
5724d022e35SMiguel Boton 
573660e34ceSMatthew Garrett /*
5745be44a6fSIngo Molnar  * To the best of our knowledge Windows compatible x86 hardware expects
5755be44a6fSIngo Molnar  * the following on reboot:
576660e34ceSMatthew Garrett  *
577660e34ceSMatthew Garrett  * 1) If the FADT has the ACPI reboot register flag set, try it
578660e34ceSMatthew Garrett  * 2) If still alive, write to the keyboard controller
579660e34ceSMatthew Garrett  * 3) If still alive, write to the ACPI reboot register again
580660e34ceSMatthew Garrett  * 4) If still alive, write to the keyboard controller again
581a4f1987eSLi, Aubrey  * 5) If still alive, call the EFI runtime service to reboot
5825be44a6fSIngo Molnar  * 6) If no EFI runtime service, call the BIOS to do a reboot
583660e34ceSMatthew Garrett  *
5845be44a6fSIngo Molnar  * We default to following the same pattern. We also have
5855be44a6fSIngo Molnar  * two other reboot methods: 'triple fault' and 'PCI', which
5865be44a6fSIngo Molnar  * can be triggered via the reboot= kernel boot option or
5875be44a6fSIngo Molnar  * via quirks.
5885be44a6fSIngo Molnar  *
5895be44a6fSIngo Molnar  * This means that this function can never return, it can misbehave
5905be44a6fSIngo Molnar  * by not rebooting properly and hanging.
591660e34ceSMatthew Garrett  */
5924d022e35SMiguel Boton static void native_machine_emergency_restart(void)
5934d022e35SMiguel Boton {
5944d022e35SMiguel Boton 	int i;
595660e34ceSMatthew Garrett 	int attempt = 0;
596660e34ceSMatthew Garrett 	int orig_reboot_type = reboot_type;
597edf2b139SRobin Holt 	unsigned short mode;
5984d022e35SMiguel Boton 
5994d022e35SMiguel Boton 	if (reboot_emergency)
6004d022e35SMiguel Boton 		emergency_vmx_disable_all();
6014d022e35SMiguel Boton 
602840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_REBOOT);
603840c2bafSJoseph Cihula 
6044d022e35SMiguel Boton 	/* Tell the BIOS if we want cold or warm reboot */
605edf2b139SRobin Holt 	mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
606edf2b139SRobin Holt 	*((unsigned short *)__va(0x472)) = mode;
6074d022e35SMiguel Boton 
60887615a34SMatt Fleming 	/*
60987615a34SMatt Fleming 	 * If an EFI capsule has been registered with the firmware then
61087615a34SMatt Fleming 	 * override the reboot= parameter.
61187615a34SMatt Fleming 	 */
61287615a34SMatt Fleming 	if (efi_capsule_pending(NULL)) {
61387615a34SMatt Fleming 		pr_info("EFI capsule is pending, forcing EFI reboot.\n");
61487615a34SMatt Fleming 		reboot_type = BOOT_EFI;
61587615a34SMatt Fleming 	}
61687615a34SMatt Fleming 
6174d022e35SMiguel Boton 	for (;;) {
6184d022e35SMiguel Boton 		/* Could also try the reset bit in the Hammer NB */
6194d022e35SMiguel Boton 		switch (reboot_type) {
6205be44a6fSIngo Molnar 		case BOOT_ACPI:
6215be44a6fSIngo Molnar 			acpi_reboot();
6225be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
6235be44a6fSIngo Molnar 			break;
6245be44a6fSIngo Molnar 
6254d022e35SMiguel Boton 		case BOOT_KBD:
626144d102bSMichael D Labriola 			mach_reboot_fixups(); /* For board specific fixups */
6277432d149SIngo Molnar 
6284d022e35SMiguel Boton 			for (i = 0; i < 10; i++) {
6294d022e35SMiguel Boton 				kb_wait();
6304d022e35SMiguel Boton 				udelay(50);
631144d102bSMichael D Labriola 				outb(0xfe, 0x64); /* Pulse reset low */
6324d022e35SMiguel Boton 				udelay(50);
6334d022e35SMiguel Boton 			}
634660e34ceSMatthew Garrett 			if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
635660e34ceSMatthew Garrett 				attempt = 1;
636660e34ceSMatthew Garrett 				reboot_type = BOOT_ACPI;
637660e34ceSMatthew Garrett 			} else {
638a4f1987eSLi, Aubrey 				reboot_type = BOOT_EFI;
639660e34ceSMatthew Garrett 			}
640660e34ceSMatthew Garrett 			break;
6414d022e35SMiguel Boton 
6424d022e35SMiguel Boton 		case BOOT_EFI:
6438562c99cSMatt Fleming 			efi_reboot(reboot_mode, NULL);
6445be44a6fSIngo Molnar 			reboot_type = BOOT_BIOS;
64514d7ca5cSH. Peter Anvin 			break;
6464d022e35SMiguel Boton 
6475be44a6fSIngo Molnar 		case BOOT_BIOS:
6485be44a6fSIngo Molnar 			machine_real_restart(MRR_BIOS);
6495be44a6fSIngo Molnar 
6505be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6515be44a6fSIngo Molnar 			reboot_type = BOOT_CF9_SAFE;
6525be44a6fSIngo Molnar 			break;
6535be44a6fSIngo Molnar 
6545be44a6fSIngo Molnar 		case BOOT_CF9_FORCE:
65514d7ca5cSH. Peter Anvin 			port_cf9_safe = true;
656df561f66SGustavo A. R. Silva 			fallthrough;
65714d7ca5cSH. Peter Anvin 
6585be44a6fSIngo Molnar 		case BOOT_CF9_SAFE:
65914d7ca5cSH. Peter Anvin 			if (port_cf9_safe) {
6605be44a6fSIngo Molnar 				u8 reboot_code = reboot_mode == REBOOT_WARM ?  0x06 : 0x0E;
66116c21ae5SLi Fei 				u8 cf9 = inb(0xcf9) & ~reboot_code;
66214d7ca5cSH. Peter Anvin 				outb(cf9|2, 0xcf9); /* Request hard reset */
66314d7ca5cSH. Peter Anvin 				udelay(50);
66416c21ae5SLi Fei 				/* Actually do the reset */
66516c21ae5SLi Fei 				outb(cf9|reboot_code, 0xcf9);
66614d7ca5cSH. Peter Anvin 				udelay(50);
66714d7ca5cSH. Peter Anvin 			}
6685be44a6fSIngo Molnar 			reboot_type = BOOT_TRIPLE;
6695be44a6fSIngo Molnar 			break;
6705be44a6fSIngo Molnar 
6715be44a6fSIngo Molnar 		case BOOT_TRIPLE:
672*8ec9069aSH. Peter Anvin (Intel) 			idt_invalidate();
6735be44a6fSIngo Molnar 			__asm__ __volatile__("int3");
6745be44a6fSIngo Molnar 
6755be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6765be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
6774d022e35SMiguel Boton 			break;
6784d022e35SMiguel Boton 		}
6794d022e35SMiguel Boton 	}
6804d022e35SMiguel Boton }
6814d022e35SMiguel Boton 
6823c62c625SGlauber Costa void native_machine_shutdown(void)
6834d022e35SMiguel Boton {
6844d022e35SMiguel Boton 	/* Stop the cpus and apics */
685522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC
6862885432aSFenghua Yu 	/*
6872885432aSFenghua Yu 	 * Disabling IO APIC before local APIC is a workaround for
6882885432aSFenghua Yu 	 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
6892885432aSFenghua Yu 	 * Specification Update". In this situation, interrupts that target
6902885432aSFenghua Yu 	 * a Logical Processor whose Local APIC is either in the process of
6912885432aSFenghua Yu 	 * being hardware disabled or software disabled are neither delivered
6922885432aSFenghua Yu 	 * nor discarded. When this erratum occurs, the processor may hang.
6932885432aSFenghua Yu 	 *
6942885432aSFenghua Yu 	 * Even without the erratum, it still makes sense to quiet IO APIC
6952885432aSFenghua Yu 	 * before disabling Local APIC.
6962885432aSFenghua Yu 	 */
697339b2ae0SBaoquan He 	clear_IO_APIC();
698522e6646SFenghua Yu #endif
699522e6646SFenghua Yu 
7004d022e35SMiguel Boton #ifdef CONFIG_SMP
701144d102bSMichael D Labriola 	/*
7021b3a5d02SRobin Holt 	 * Stop all of the others. Also disable the local irq to
7031b3a5d02SRobin Holt 	 * not receive the per-cpu timer interrupt which may trigger
7041b3a5d02SRobin Holt 	 * scheduler's load balance.
7054d022e35SMiguel Boton 	 */
70655c844a4SFeng Tang 	local_irq_disable();
70776fac077SAlok Kataria 	stop_other_cpus();
7084d022e35SMiguel Boton #endif
7094d022e35SMiguel Boton 
7104d022e35SMiguel Boton 	lapic_shutdown();
711339b2ae0SBaoquan He 	restore_boot_irq_mode();
7124d022e35SMiguel Boton 
7134d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER
7144d022e35SMiguel Boton 	hpet_disable();
7154d022e35SMiguel Boton #endif
7164d022e35SMiguel Boton 
7174d022e35SMiguel Boton #ifdef CONFIG_X86_64
718338bac52SFUJITA Tomonori 	x86_platform.iommu_shutdown();
7194d022e35SMiguel Boton #endif
7204d022e35SMiguel Boton }
7214d022e35SMiguel Boton 
722d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency)
723d176720dSEduardo Habkost {
724d176720dSEduardo Habkost 	reboot_emergency = emergency;
725d176720dSEduardo Habkost 	machine_ops.emergency_restart();
726d176720dSEduardo Habkost }
727d176720dSEduardo Habkost 
728416e2d63SJody Belka static void native_machine_restart(char *__unused)
7294d022e35SMiguel Boton {
730c767a54bSJoe Perches 	pr_notice("machine restart\n");
7314d022e35SMiguel Boton 
7324d022e35SMiguel Boton 	if (!reboot_force)
7334d022e35SMiguel Boton 		machine_shutdown();
734d176720dSEduardo Habkost 	__machine_emergency_restart(0);
7354d022e35SMiguel Boton }
7364d022e35SMiguel Boton 
737416e2d63SJody Belka static void native_machine_halt(void)
7384d022e35SMiguel Boton {
739144d102bSMichael D Labriola 	/* Stop other cpus and apics */
740d3ec5caeSIvan Vecera 	machine_shutdown();
741d3ec5caeSIvan Vecera 
742840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
743840c2bafSJoseph Cihula 
744d3ec5caeSIvan Vecera 	stop_this_cpu(NULL);
7454d022e35SMiguel Boton }
7464d022e35SMiguel Boton 
747416e2d63SJody Belka static void native_machine_power_off(void)
7484d022e35SMiguel Boton {
7494d022e35SMiguel Boton 	if (pm_power_off) {
7504d022e35SMiguel Boton 		if (!reboot_force)
7514d022e35SMiguel Boton 			machine_shutdown();
7524d022e35SMiguel Boton 		pm_power_off();
7534d022e35SMiguel Boton 	}
754144d102bSMichael D Labriola 	/* A fallback in case there is no PM info available */
755840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
7564d022e35SMiguel Boton }
7574d022e35SMiguel Boton 
758404f6aacSKees Cook struct machine_ops machine_ops __ro_after_init = {
759416e2d63SJody Belka 	.power_off = native_machine_power_off,
760416e2d63SJody Belka 	.shutdown = native_machine_shutdown,
761416e2d63SJody Belka 	.emergency_restart = native_machine_emergency_restart,
762416e2d63SJody Belka 	.restart = native_machine_restart,
763ed23dc6fSGlauber Costa 	.halt = native_machine_halt,
7642965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
765ed23dc6fSGlauber Costa 	.crash_shutdown = native_machine_crash_shutdown,
766ed23dc6fSGlauber Costa #endif
7674d022e35SMiguel Boton };
768416e2d63SJody Belka 
769416e2d63SJody Belka void machine_power_off(void)
770416e2d63SJody Belka {
771416e2d63SJody Belka 	machine_ops.power_off();
772416e2d63SJody Belka }
773416e2d63SJody Belka 
774416e2d63SJody Belka void machine_shutdown(void)
775416e2d63SJody Belka {
776416e2d63SJody Belka 	machine_ops.shutdown();
777416e2d63SJody Belka }
778416e2d63SJody Belka 
779416e2d63SJody Belka void machine_emergency_restart(void)
780416e2d63SJody Belka {
781d176720dSEduardo Habkost 	__machine_emergency_restart(1);
782416e2d63SJody Belka }
783416e2d63SJody Belka 
784416e2d63SJody Belka void machine_restart(char *cmd)
785416e2d63SJody Belka {
786416e2d63SJody Belka 	machine_ops.restart(cmd);
787416e2d63SJody Belka }
788416e2d63SJody Belka 
789416e2d63SJody Belka void machine_halt(void)
790416e2d63SJody Belka {
791416e2d63SJody Belka 	machine_ops.halt();
792416e2d63SJody Belka }
793416e2d63SJody Belka 
7942965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
795ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs)
796ed23dc6fSGlauber Costa {
797ed23dc6fSGlauber Costa 	machine_ops.crash_shutdown(regs);
798ed23dc6fSGlauber Costa }
799ed23dc6fSGlauber Costa #endif
8002ddded21SEduardo Habkost 
8012ddded21SEduardo Habkost 
8025bc32950SXunlei Pang /* This is the CPU performing the emergency shutdown work. */
8035bc32950SXunlei Pang int crashing_cpu = -1;
8045bc32950SXunlei Pang 
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 
8279c48f1c6SDon Zickus 	shootdown_callback(cpu, regs);
8282ddded21SEduardo Habkost 
8292ddded21SEduardo Habkost 	atomic_dec(&waiting_for_crash_ipi);
8302ddded21SEduardo Habkost 	/* Assume hlt works */
8312ddded21SEduardo Habkost 	halt();
8322ddded21SEduardo Habkost 	for (;;)
8332ddded21SEduardo Habkost 		cpu_relax();
8342ddded21SEduardo Habkost 
8359c48f1c6SDon Zickus 	return NMI_HANDLED;
8362ddded21SEduardo Habkost }
8372ddded21SEduardo Habkost 
838144d102bSMichael D Labriola /*
839144d102bSMichael D Labriola  * Halt all other CPUs, calling the specified function on each of them
840bb8dd270SEduardo Habkost  *
841bb8dd270SEduardo Habkost  * This function can be used to halt all other CPUs on crash
842bb8dd270SEduardo Habkost  * or emergency reboot time. The function passed as parameter
843bb8dd270SEduardo Habkost  * will be called inside a NMI handler on all CPUs.
844bb8dd270SEduardo Habkost  */
8452ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
8462ddded21SEduardo Habkost {
8472ddded21SEduardo Habkost 	unsigned long msecs;
848c415b3dcSEduardo Habkost 	local_irq_disable();
8492ddded21SEduardo Habkost 
8502ddded21SEduardo Habkost 	/* Make a note of crashing cpu. Will be used in NMI callback. */
8512ddded21SEduardo Habkost 	crashing_cpu = safe_smp_processor_id();
8522ddded21SEduardo Habkost 
8532ddded21SEduardo Habkost 	shootdown_callback = callback;
8542ddded21SEduardo Habkost 
8552ddded21SEduardo Habkost 	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
8562ddded21SEduardo Habkost 	/* Would it be better to replace the trap vector here? */
8579c48f1c6SDon Zickus 	if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
8589c48f1c6SDon Zickus 				 NMI_FLAG_FIRST, "crash"))
859144d102bSMichael D Labriola 		return;		/* Return what? */
860144d102bSMichael D Labriola 	/*
861144d102bSMichael D Labriola 	 * Ensure the new callback function is set before sending
8622ddded21SEduardo Habkost 	 * out the NMI
8632ddded21SEduardo Habkost 	 */
8642ddded21SEduardo Habkost 	wmb();
8652ddded21SEduardo Habkost 
86622ca7ee9SThomas Gleixner 	apic_send_IPI_allbutself(NMI_VECTOR);
8672ddded21SEduardo Habkost 
86858c5661fSHidehiro Kawai 	/* Kick CPUs looping in NMI context. */
86958c5661fSHidehiro Kawai 	WRITE_ONCE(crash_ipi_issued, 1);
87058c5661fSHidehiro Kawai 
8712ddded21SEduardo Habkost 	msecs = 1000; /* Wait at most a second for the other cpus to stop */
8722ddded21SEduardo Habkost 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
8732ddded21SEduardo Habkost 		mdelay(1);
8742ddded21SEduardo Habkost 		msecs--;
8752ddded21SEduardo Habkost 	}
8762ddded21SEduardo Habkost 
8772ddded21SEduardo Habkost 	/* Leave the nmi callback set */
8782ddded21SEduardo Habkost }
87958c5661fSHidehiro Kawai 
880b279d67dSHidehiro Kawai /*
881b279d67dSHidehiro Kawai  * Check if the crash dumping IPI got issued and if so, call its callback
882b279d67dSHidehiro Kawai  * directly. This function is used when we have already been in NMI handler.
883b279d67dSHidehiro Kawai  * It doesn't return.
884b279d67dSHidehiro Kawai  */
885b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
886b279d67dSHidehiro Kawai {
887b279d67dSHidehiro Kawai 	if (crash_ipi_issued)
888b279d67dSHidehiro Kawai 		crash_nmi_callback(0, regs);
889b279d67dSHidehiro Kawai }
890b279d67dSHidehiro Kawai 
89158c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */
89258c5661fSHidehiro Kawai void nmi_panic_self_stop(struct pt_regs *regs)
89358c5661fSHidehiro Kawai {
89458c5661fSHidehiro Kawai 	while (1) {
895b279d67dSHidehiro Kawai 		/* If no CPU is preparing crash dump, we simply loop here. */
896b279d67dSHidehiro Kawai 		run_crash_ipi_callback(regs);
89758c5661fSHidehiro Kawai 		cpu_relax();
89858c5661fSHidehiro Kawai 	}
89958c5661fSHidehiro Kawai }
90058c5661fSHidehiro Kawai 
901bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */
902bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
903bb8dd270SEduardo Habkost {
904bb8dd270SEduardo Habkost 	/* No other CPUs to shoot down */
905bb8dd270SEduardo Habkost }
906b279d67dSHidehiro Kawai 
907b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
908b279d67dSHidehiro Kawai {
909b279d67dSHidehiro Kawai }
9102ddded21SEduardo Habkost #endif
911