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>
2596b89dc6SJaswinder Singh Rajput #include <asm/cpu.h>
26c410b830SDon Zickus #include <asm/nmi.h>
2765051397SH. Peter Anvin #include <asm/smp.h>
284d022e35SMiguel Boton
294d022e35SMiguel Boton #include <linux/ctype.h>
304d022e35SMiguel Boton #include <linux/mc146818rtc.h>
315a8c9aebSJarkko Sakkinen #include <asm/realmode.h>
32338bac52SFUJITA Tomonori #include <asm/x86_init.h>
3344be28e9SMatt Fleming #include <asm/efi.h>
344d022e35SMiguel Boton
354d022e35SMiguel Boton /*
364d022e35SMiguel Boton * Power off function, if any
374d022e35SMiguel Boton */
384d022e35SMiguel Boton void (*pm_power_off)(void);
394d022e35SMiguel Boton EXPORT_SYMBOL(pm_power_off);
404d022e35SMiguel Boton
41144d102bSMichael D Labriola /*
42144d102bSMichael D Labriola * This is set if we need to go through the 'emergency' path.
43d176720dSEduardo Habkost * When machine_emergency_restart() is called, we may be on
44d176720dSEduardo Habkost * an inconsistent state and won't be able to do a clean cleanup
45d176720dSEduardo Habkost */
46d176720dSEduardo Habkost static int reboot_emergency;
47d176720dSEduardo Habkost
4814d7ca5cSH. Peter Anvin /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
4914d7ca5cSH. Peter Anvin bool port_cf9_safe = false;
5014d7ca5cSH. Peter Anvin
51144d102bSMichael D Labriola /*
524d022e35SMiguel Boton * Reboot options and system auto-detection code provided by
534d022e35SMiguel Boton * Dell Inc. so their systems "just work". :-)
544d022e35SMiguel Boton */
554d022e35SMiguel Boton
564d022e35SMiguel Boton /*
574d581259SAlex Hung * Some machines require the "reboot=a" commandline options
584d581259SAlex Hung */
set_acpi_reboot(const struct dmi_system_id * d)594d581259SAlex Hung static int __init set_acpi_reboot(const struct dmi_system_id *d)
604d581259SAlex Hung {
614d581259SAlex Hung if (reboot_type != BOOT_ACPI) {
624d581259SAlex Hung reboot_type = BOOT_ACPI;
634d581259SAlex Hung pr_info("%s series board detected. Selecting %s-method for reboots.\n",
644d581259SAlex Hung d->ident, "ACPI");
654d581259SAlex Hung }
664d581259SAlex Hung return 0;
674d581259SAlex Hung }
684d581259SAlex Hung
694d581259SAlex Hung /*
701ef03890SPeter Chubb * Some machines require the "reboot=b" or "reboot=k" commandline options,
714d022e35SMiguel Boton * this quirk makes that automatic.
724d022e35SMiguel Boton */
set_bios_reboot(const struct dmi_system_id * d)734d022e35SMiguel Boton static int __init set_bios_reboot(const struct dmi_system_id *d)
744d022e35SMiguel Boton {
754d022e35SMiguel Boton if (reboot_type != BOOT_BIOS) {
764d022e35SMiguel Boton reboot_type = BOOT_BIOS;
77c767a54bSJoe Perches pr_info("%s series board detected. Selecting %s-method for reboots.\n",
786d9153bbSLan Tianyu d->ident, "BIOS");
794d022e35SMiguel Boton }
804d022e35SMiguel Boton return 0;
814d022e35SMiguel Boton }
824d022e35SMiguel Boton
830082517fSJian-Hong Pan /*
840082517fSJian-Hong Pan * Some machines don't handle the default ACPI reboot method and
850082517fSJian-Hong Pan * require the EFI reboot method:
860082517fSJian-Hong Pan */
set_efi_reboot(const struct dmi_system_id * d)870082517fSJian-Hong Pan static int __init set_efi_reboot(const struct dmi_system_id *d)
880082517fSJian-Hong Pan {
890082517fSJian-Hong Pan if (reboot_type != BOOT_EFI && !efi_runtime_disabled()) {
900082517fSJian-Hong Pan reboot_type = BOOT_EFI;
910082517fSJian-Hong Pan pr_info("%s series board detected. Selecting EFI-method for reboot.\n", d->ident);
920082517fSJian-Hong Pan }
930082517fSJian-Hong Pan return 0;
940082517fSJian-Hong Pan }
950082517fSJian-Hong Pan
machine_real_restart(unsigned int type)9665051397SH. Peter Anvin void __noreturn machine_real_restart(unsigned int type)
9757b16594SMichael D Labriola {
9857b16594SMichael D Labriola local_irq_disable();
9957b16594SMichael D Labriola
100144d102bSMichael D Labriola /*
101144d102bSMichael D Labriola * Write zero to CMOS register number 0x0f, which the BIOS POST
102144d102bSMichael D Labriola * routine will recognize as telling it to do a proper reboot. (Well
103144d102bSMichael D Labriola * that's what this book in front of me says -- it may only apply to
104144d102bSMichael D Labriola * the Phoenix BIOS though, it's not clear). At the same time,
105144d102bSMichael D Labriola * disable NMIs by setting the top bit in the CMOS address register,
106144d102bSMichael D Labriola * as we're about to do peculiar things to the CPU. I'm not sure if
107144d102bSMichael D Labriola * `outb_p' is needed instead of just `outb'. Use it to be on the
108144d102bSMichael D Labriola * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
10957b16594SMichael D Labriola */
11057b16594SMichael D Labriola spin_lock(&rtc_lock);
11157b16594SMichael D Labriola CMOS_WRITE(0x00, 0x8f);
11257b16594SMichael D Labriola spin_unlock(&rtc_lock);
11357b16594SMichael D Labriola
11457b16594SMichael D Labriola /*
11571d5049bSJoerg Roedel * Switch to the trampoline page table.
11657b16594SMichael D Labriola */
11771d5049bSJoerg Roedel load_trampoline_pgtable();
11857b16594SMichael D Labriola
11957b16594SMichael D Labriola /* Jump to the identity-mapped low memory code */
12065051397SH. Peter Anvin #ifdef CONFIG_X86_32
12165051397SH. Peter Anvin asm volatile("jmpl *%0" : :
12265051397SH. Peter Anvin "rm" (real_mode_header->machine_real_restart_asm),
12365051397SH. Peter Anvin "a" (type));
12465051397SH. Peter Anvin #else
12565051397SH. Peter Anvin asm volatile("ljmpl *%0" : :
12665051397SH. Peter Anvin "m" (real_mode_header->machine_real_restart_asm),
12765051397SH. Peter Anvin "D" (type));
12865051397SH. Peter Anvin #endif
12965051397SH. Peter Anvin unreachable();
13057b16594SMichael D Labriola }
13157b16594SMichael D Labriola #ifdef CONFIG_APM_MODULE
13257b16594SMichael D Labriola EXPORT_SYMBOL(machine_real_restart);
13357b16594SMichael D Labriola #endif
134c207aee4SJosh Poimboeuf STACK_FRAME_NON_STANDARD(machine_real_restart);
13557b16594SMichael D Labriola
13657b16594SMichael D Labriola /*
13757b16594SMichael D Labriola * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
13857b16594SMichael D Labriola */
set_pci_reboot(const struct dmi_system_id * d)13957b16594SMichael D Labriola static int __init set_pci_reboot(const struct dmi_system_id *d)
14057b16594SMichael D Labriola {
1415be44a6fSIngo Molnar if (reboot_type != BOOT_CF9_FORCE) {
1425be44a6fSIngo Molnar reboot_type = BOOT_CF9_FORCE;
143c767a54bSJoe Perches pr_info("%s series board detected. Selecting %s-method for reboots.\n",
1446d9153bbSLan Tianyu d->ident, "PCI");
14557b16594SMichael D Labriola }
14657b16594SMichael D Labriola return 0;
14757b16594SMichael D Labriola }
14857b16594SMichael D Labriola
set_kbd_reboot(const struct dmi_system_id * d)1491ef03890SPeter Chubb static int __init set_kbd_reboot(const struct dmi_system_id *d)
1501ef03890SPeter Chubb {
1511ef03890SPeter Chubb if (reboot_type != BOOT_KBD) {
1521ef03890SPeter Chubb reboot_type = BOOT_KBD;
153c767a54bSJoe Perches pr_info("%s series board detected. Selecting %s-method for reboot.\n",
1546d9153bbSLan Tianyu d->ident, "KBD");
1551ef03890SPeter Chubb }
1561ef03890SPeter Chubb return 0;
1571ef03890SPeter Chubb }
1581ef03890SPeter Chubb
159144d102bSMichael D Labriola /*
16065051397SH. Peter Anvin * This is a single dmi_table handling all reboot quirks.
16157b16594SMichael D Labriola */
1626faadbbbSChristoph Hellwig static const struct dmi_system_id reboot_dmi_table[] __initconst = {
16357b16594SMichael D Labriola
164e56e57f6SDave Jones /* Acer */
165b49c78d4SPeter Chubb { /* Handle reboot issue on Acer Aspire one */
1661ef03890SPeter Chubb .callback = set_kbd_reboot,
167b49c78d4SPeter Chubb .ident = "Acer Aspire One A110",
168b49c78d4SPeter Chubb .matches = {
169b49c78d4SPeter Chubb DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
170b49c78d4SPeter Chubb DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
171b49c78d4SPeter Chubb },
172b49c78d4SPeter Chubb },
1730082517fSJian-Hong Pan { /* Handle reboot issue on Acer TravelMate X514-51T */
1740082517fSJian-Hong Pan .callback = set_efi_reboot,
1750082517fSJian-Hong Pan .ident = "Acer TravelMate X514-51T",
1760082517fSJian-Hong Pan .matches = {
1770082517fSJian-Hong Pan DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1780082517fSJian-Hong Pan DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate X514-51T"),
1790082517fSJian-Hong Pan },
1800082517fSJian-Hong Pan },
181e56e57f6SDave Jones
182e56e57f6SDave Jones /* Apple */
1833e03bbeaSShunichi Fuji { /* Handle problems with rebooting on Apple MacBook5 */
1846c6c51e4SPaul Mackerras .callback = set_pci_reboot,
1853e03bbeaSShunichi Fuji .ident = "Apple MacBook5",
1866c6c51e4SPaul Mackerras .matches = {
1876c6c51e4SPaul Mackerras DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1883e03bbeaSShunichi Fuji DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
1896c6c51e4SPaul Mackerras },
1906c6c51e4SPaul Mackerras },
191140fd4acSHill Ma { /* Handle problems with rebooting on Apple MacBook6,1 */
192140fd4acSHill Ma .callback = set_pci_reboot,
193140fd4acSHill Ma .ident = "Apple MacBook6,1",
194140fd4acSHill Ma .matches = {
195140fd4acSHill Ma DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
196140fd4acSHill Ma DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"),
197140fd4acSHill Ma },
198140fd4acSHill Ma },
1993e03bbeaSShunichi Fuji { /* Handle problems with rebooting on Apple MacBookPro5 */
200498cdbfbSOzan Çağlayan .callback = set_pci_reboot,
2013e03bbeaSShunichi Fuji .ident = "Apple MacBookPro5",
202498cdbfbSOzan Çağlayan .matches = {
203498cdbfbSOzan Çağlayan DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2043e03bbeaSShunichi Fuji DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
205498cdbfbSOzan Çağlayan },
206498cdbfbSOzan Çağlayan },
20705154752SGottfried Haider { /* Handle problems with rebooting on Apple Macmini3,1 */
20805154752SGottfried Haider .callback = set_pci_reboot,
20905154752SGottfried Haider .ident = "Apple Macmini3,1",
21005154752SGottfried Haider .matches = {
21105154752SGottfried Haider DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
21205154752SGottfried Haider DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
21305154752SGottfried Haider },
21405154752SGottfried Haider },
2150a832320SJustin P. Mattock { /* Handle problems with rebooting on the iMac9,1. */
2160a832320SJustin P. Mattock .callback = set_pci_reboot,
2170a832320SJustin P. Mattock .ident = "Apple iMac9,1",
2180a832320SJustin P. Mattock .matches = {
2190a832320SJustin P. Mattock DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2200a832320SJustin P. Mattock DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
2210a832320SJustin P. Mattock },
2220a832320SJustin P. Mattock },
2232f0c0b2dSMario Kleiner { /* Handle problems with rebooting on the iMac10,1. */
2242f0c0b2dSMario Kleiner .callback = set_pci_reboot,
2252f0c0b2dSMario Kleiner .ident = "Apple iMac10,1",
2262f0c0b2dSMario Kleiner .matches = {
2272f0c0b2dSMario Kleiner DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2282f0c0b2dSMario Kleiner DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
2292f0c0b2dSMario Kleiner },
2302f0c0b2dSMario Kleiner },
231e56e57f6SDave Jones
23280313b30SStefan Lippers-Hollmann /* ASRock */
23380313b30SStefan Lippers-Hollmann { /* Handle problems with rebooting on ASRock Q1900DC-ITX */
23480313b30SStefan Lippers-Hollmann .callback = set_pci_reboot,
23580313b30SStefan Lippers-Hollmann .ident = "ASRock Q1900DC-ITX",
23680313b30SStefan Lippers-Hollmann .matches = {
23780313b30SStefan Lippers-Hollmann DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
23880313b30SStefan Lippers-Hollmann DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
23980313b30SStefan Lippers-Hollmann },
24080313b30SStefan Lippers-Hollmann },
24180313b30SStefan Lippers-Hollmann
242e56e57f6SDave Jones /* ASUS */
243e56e57f6SDave Jones { /* Handle problems with rebooting on ASUS P4S800 */
244e56e57f6SDave Jones .callback = set_bios_reboot,
245e56e57f6SDave Jones .ident = "ASUS P4S800",
246e56e57f6SDave Jones .matches = {
247e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
248e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
249e56e57f6SDave Jones },
250e56e57f6SDave Jones },
25190b28dedSMatjaz Hegedic { /* Handle problems with rebooting on ASUS EeeBook X205TA */
25290b28dedSMatjaz Hegedic .callback = set_acpi_reboot,
25390b28dedSMatjaz Hegedic .ident = "ASUS EeeBook X205TA",
25490b28dedSMatjaz Hegedic .matches = {
25590b28dedSMatjaz Hegedic DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
256bba8376aSMatjaz Hegedic DMI_MATCH(DMI_PRODUCT_NAME, "X205TA"),
25790b28dedSMatjaz Hegedic },
25890b28dedSMatjaz Hegedic },
2593b3e7855SMatjaz Hegedic { /* Handle problems with rebooting on ASUS EeeBook X205TAW */
2603b3e7855SMatjaz Hegedic .callback = set_acpi_reboot,
2613b3e7855SMatjaz Hegedic .ident = "ASUS EeeBook X205TAW",
2623b3e7855SMatjaz Hegedic .matches = {
2633b3e7855SMatjaz Hegedic DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
2643b3e7855SMatjaz Hegedic DMI_MATCH(DMI_PRODUCT_NAME, "X205TAW"),
2653b3e7855SMatjaz Hegedic },
2663b3e7855SMatjaz Hegedic },
267e56e57f6SDave Jones
268aadca6faSChristian Gmeiner /* Certec */
269aadca6faSChristian Gmeiner { /* Handle problems with rebooting on Certec BPC600 */
270aadca6faSChristian Gmeiner .callback = set_pci_reboot,
271aadca6faSChristian Gmeiner .ident = "Certec BPC600",
272aadca6faSChristian Gmeiner .matches = {
273aadca6faSChristian Gmeiner DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
274aadca6faSChristian Gmeiner DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
275aadca6faSChristian Gmeiner },
276aadca6faSChristian Gmeiner },
277aadca6faSChristian Gmeiner
278e56e57f6SDave Jones /* Dell */
279e56e57f6SDave Jones { /* Handle problems with rebooting on Dell DXP061 */
280e56e57f6SDave Jones .callback = set_bios_reboot,
281e56e57f6SDave Jones .ident = "Dell DXP061",
2823628c3f5SMaxime Ripard .matches = {
2833628c3f5SMaxime Ripard DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
284e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
285e56e57f6SDave Jones },
286e56e57f6SDave Jones },
287e56e57f6SDave Jones { /* Handle problems with rebooting on Dell E520's */
288e56e57f6SDave Jones .callback = set_bios_reboot,
289e56e57f6SDave Jones .ident = "Dell E520",
290e56e57f6SDave Jones .matches = {
291e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
292e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
2933628c3f5SMaxime Ripard },
2943628c3f5SMaxime Ripard },
2958412da75SVille Syrjälä { /* Handle problems with rebooting on the Latitude E5410. */
2968412da75SVille Syrjälä .callback = set_pci_reboot,
2978412da75SVille Syrjälä .ident = "Dell Latitude E5410",
2988412da75SVille Syrjälä .matches = {
2998412da75SVille Syrjälä DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3008412da75SVille Syrjälä DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
3018412da75SVille Syrjälä },
3028412da75SVille Syrjälä },
303b7798d28SDaniel J Blueman { /* Handle problems with rebooting on the Latitude E5420. */
304b7798d28SDaniel J Blueman .callback = set_pci_reboot,
305b7798d28SDaniel J Blueman .ident = "Dell Latitude E5420",
306b7798d28SDaniel J Blueman .matches = {
307b7798d28SDaniel J Blueman DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
308b7798d28SDaniel J Blueman DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
309b7798d28SDaniel J Blueman },
310b7798d28SDaniel J Blueman },
311e56e57f6SDave Jones { /* Handle problems with rebooting on the Latitude E6320. */
312e56e57f6SDave Jones .callback = set_pci_reboot,
313e56e57f6SDave Jones .ident = "Dell Latitude E6320",
314e56e57f6SDave Jones .matches = {
315e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
316e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
317e56e57f6SDave Jones },
318e56e57f6SDave Jones },
319a536877eSH. Peter Anvin { /* Handle problems with rebooting on the Latitude E6420. */
320a536877eSH. Peter Anvin .callback = set_pci_reboot,
321a536877eSH. Peter Anvin .ident = "Dell Latitude E6420",
322a536877eSH. Peter Anvin .matches = {
323a536877eSH. Peter Anvin DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
324a536877eSH. Peter Anvin DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
325a536877eSH. Peter Anvin },
326a536877eSH. Peter Anvin },
327e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
328e56e57f6SDave Jones .callback = set_bios_reboot,
329e56e57f6SDave Jones .ident = "Dell OptiPlex 330",
330e56e57f6SDave Jones .matches = {
331e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
332e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
333e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
334e56e57f6SDave Jones },
335e56e57f6SDave Jones },
336e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
337e56e57f6SDave Jones .callback = set_bios_reboot,
338e56e57f6SDave Jones .ident = "Dell OptiPlex 360",
339e56e57f6SDave Jones .matches = {
340e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
341e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
342e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
343e56e57f6SDave Jones },
344e56e57f6SDave Jones },
345e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 745's SFF */
346e56e57f6SDave Jones .callback = set_bios_reboot,
347e56e57f6SDave Jones .ident = "Dell OptiPlex 745",
348e56e57f6SDave Jones .matches = {
349e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
350e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
351e56e57f6SDave Jones },
352e56e57f6SDave Jones },
353e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 745's DFF */
354e56e57f6SDave Jones .callback = set_bios_reboot,
355e56e57f6SDave Jones .ident = "Dell OptiPlex 745",
356e56e57f6SDave Jones .matches = {
357e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
358e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
359e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
360e56e57f6SDave Jones },
361e56e57f6SDave Jones },
362e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
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, "0KW626"),
369e56e57f6SDave Jones },
370e56e57f6SDave Jones },
371e56e57f6SDave Jones { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
372e56e57f6SDave Jones .callback = set_bios_reboot,
373e56e57f6SDave Jones .ident = "Dell OptiPlex 760",
374e56e57f6SDave Jones .matches = {
375e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
376e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
377e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
378e56e57f6SDave Jones },
379e56e57f6SDave Jones },
3806be30bb7SRafael J. Wysocki { /* Handle problems with rebooting on the OptiPlex 990. */
3816be30bb7SRafael J. Wysocki .callback = set_pci_reboot,
382a729691bSPaul Gortmaker .ident = "Dell OptiPlex 990 BIOS A0x",
3836be30bb7SRafael J. Wysocki .matches = {
3846be30bb7SRafael J. Wysocki DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3856be30bb7SRafael J. Wysocki DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
386a729691bSPaul Gortmaker DMI_MATCH(DMI_BIOS_VERSION, "A0"),
3876be30bb7SRafael J. Wysocki },
3886be30bb7SRafael J. Wysocki },
389e56e57f6SDave Jones { /* Handle problems with rebooting on Dell 300's */
390e56e57f6SDave Jones .callback = set_bios_reboot,
391e56e57f6SDave Jones .ident = "Dell PowerEdge 300",
39276eb9a30SZhang Rui .matches = {
393e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
394e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
395e56e57f6SDave Jones },
396e56e57f6SDave Jones },
397e56e57f6SDave Jones { /* Handle problems with rebooting on Dell 1300's */
398e56e57f6SDave Jones .callback = set_bios_reboot,
399e56e57f6SDave Jones .ident = "Dell PowerEdge 1300",
400e56e57f6SDave Jones .matches = {
401e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
402e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
403e56e57f6SDave Jones },
404e56e57f6SDave Jones },
405e56e57f6SDave Jones { /* Handle problems with rebooting on Dell 2400's */
406e56e57f6SDave Jones .callback = set_bios_reboot,
407e56e57f6SDave Jones .ident = "Dell PowerEdge 2400",
408e56e57f6SDave Jones .matches = {
409e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
410e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
41176eb9a30SZhang Rui },
41276eb9a30SZhang Rui },
4134f0acd31SMasoud Sharbiani { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
4144f0acd31SMasoud Sharbiani .callback = set_pci_reboot,
4154f0acd31SMasoud Sharbiani .ident = "Dell PowerEdge C6100",
4164f0acd31SMasoud Sharbiani .matches = {
4174f0acd31SMasoud Sharbiani DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
4184f0acd31SMasoud Sharbiani DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
4194f0acd31SMasoud Sharbiani },
4204f0acd31SMasoud Sharbiani },
4216c6c51e4SPaul Mackerras { /* Handle problems with rebooting on the Precision M6600. */
4226c6c51e4SPaul Mackerras .callback = set_pci_reboot,
4236c6c51e4SPaul Mackerras .ident = "Dell Precision M6600",
4246c6c51e4SPaul Mackerras .matches = {
4256c6c51e4SPaul Mackerras DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4266c6c51e4SPaul Mackerras DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
4276c6c51e4SPaul Mackerras },
4286c6c51e4SPaul Mackerras },
429e56e57f6SDave Jones { /* Handle problems with rebooting on Dell T5400's */
430e56e57f6SDave Jones .callback = set_bios_reboot,
431e56e57f6SDave Jones .ident = "Dell Precision T5400",
43257b16594SMichael D Labriola .matches = {
4336c6c51e4SPaul Mackerras DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
434e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
435144d102bSMichael D Labriola },
4365955633eSMichael D Labriola },
437e56e57f6SDave Jones { /* Handle problems with rebooting on Dell T7400's */
438e56e57f6SDave Jones .callback = set_bios_reboot,
439e56e57f6SDave Jones .ident = "Dell Precision T7400",
4406c6c51e4SPaul Mackerras .matches = {
441e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
442e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
4436c6c51e4SPaul Mackerras },
4444d022e35SMiguel Boton },
445e56e57f6SDave Jones { /* Handle problems with rebooting on Dell XPS710 */
446e56e57f6SDave Jones .callback = set_bios_reboot,
447e56e57f6SDave Jones .ident = "Dell XPS710",
448e56e57f6SDave Jones .matches = {
449e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
450e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
451e56e57f6SDave Jones },
452e56e57f6SDave Jones },
4534d581259SAlex Hung { /* Handle problems with rebooting on Dell Optiplex 7450 AIO */
4544d581259SAlex Hung .callback = set_acpi_reboot,
4554d581259SAlex Hung .ident = "Dell OptiPlex 7450 AIO",
4564d581259SAlex Hung .matches = {
4574d581259SAlex Hung DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
4584d581259SAlex Hung DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7450 AIO"),
4594d581259SAlex Hung },
4604d581259SAlex Hung },
461e56e57f6SDave Jones
462e56e57f6SDave Jones /* Hewlett-Packard */
463e56e57f6SDave Jones { /* Handle problems with rebooting on HP laptops */
464e56e57f6SDave Jones .callback = set_bios_reboot,
465e56e57f6SDave Jones .ident = "HP Compaq Laptop",
466e56e57f6SDave Jones .matches = {
467e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
468e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
469e56e57f6SDave Jones },
470e56e57f6SDave Jones },
471e56e57f6SDave Jones
4724b2d8ca9SHeiner Kallweit { /* PCIe Wifi card isn't detected after reboot otherwise */
4734b2d8ca9SHeiner Kallweit .callback = set_pci_reboot,
4744b2d8ca9SHeiner Kallweit .ident = "Zotac ZBOX CI327 nano",
4754b2d8ca9SHeiner Kallweit .matches = {
4764b2d8ca9SHeiner Kallweit DMI_MATCH(DMI_SYS_VENDOR, "NA"),
4774b2d8ca9SHeiner Kallweit DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
4784b2d8ca9SHeiner Kallweit },
4794b2d8ca9SHeiner Kallweit },
4804b2d8ca9SHeiner Kallweit
481e56e57f6SDave Jones /* Sony */
482e56e57f6SDave Jones { /* Handle problems with rebooting on Sony VGN-Z540N */
483e56e57f6SDave Jones .callback = set_bios_reboot,
484e56e57f6SDave Jones .ident = "Sony VGN-Z540N",
485e56e57f6SDave Jones .matches = {
486e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
487e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
488e56e57f6SDave Jones },
489e56e57f6SDave Jones },
490e56e57f6SDave Jones
4914d022e35SMiguel Boton { }
4924d022e35SMiguel Boton };
4939c48f1c6SDon Zickus
reboot_init(void)494d176720dSEduardo Habkost static int __init reboot_init(void)
495d176720dSEduardo Habkost {
49644be28e9SMatt Fleming int rv;
49744be28e9SMatt Fleming
498d176720dSEduardo Habkost /*
499d176720dSEduardo Habkost * Only do the DMI check if reboot_type hasn't been overridden
500d176720dSEduardo Habkost * on the command line
501d176720dSEduardo Habkost */
50244be28e9SMatt Fleming if (!reboot_default)
50344be28e9SMatt Fleming return 0;
50444be28e9SMatt Fleming
50544be28e9SMatt Fleming /*
50644be28e9SMatt Fleming * The DMI quirks table takes precedence. If no quirks entry
5074ecf7191SStefan Assmann * matches and the ACPI Hardware Reduced bit is set and EFI
5084ecf7191SStefan Assmann * runtime services are enabled, force EFI reboot.
50944be28e9SMatt Fleming */
51044be28e9SMatt Fleming rv = dmi_check_system(reboot_dmi_table);
51144be28e9SMatt Fleming
5124ecf7191SStefan Assmann if (!rv && efi_reboot_required() && !efi_runtime_disabled())
51344be28e9SMatt Fleming reboot_type = BOOT_EFI;
51444be28e9SMatt Fleming
515d176720dSEduardo Habkost return 0;
516d176720dSEduardo Habkost }
517d176720dSEduardo Habkost core_initcall(reboot_init);
518d176720dSEduardo Habkost
kb_wait(void)519d176720dSEduardo Habkost static inline void kb_wait(void)
520d176720dSEduardo Habkost {
521d176720dSEduardo Habkost int i;
522d176720dSEduardo Habkost
523d176720dSEduardo Habkost for (i = 0; i < 0x10000; i++) {
5247432d149SIngo Molnar if ((inb(0x64) & 0x02) == 0)
5257432d149SIngo Molnar break;
5267432d149SIngo Molnar udelay(2);
5277432d149SIngo Molnar }
528416e2d63SJody Belka }
5294d022e35SMiguel Boton
53026044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void);
5314d022e35SMiguel Boton
532*261cd5edSSean Christopherson #if IS_ENABLED(CONFIG_KVM_INTEL) || IS_ENABLED(CONFIG_KVM_AMD)
533edc8deb0SSean Christopherson /* RCU-protected callback to disable virtualization prior to reboot. */
534edc8deb0SSean Christopherson static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback;
535edc8deb0SSean Christopherson
cpu_emergency_register_virt_callback(cpu_emergency_virt_cb * callback)536edc8deb0SSean Christopherson void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback)
537edc8deb0SSean Christopherson {
538edc8deb0SSean Christopherson if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback)))
539edc8deb0SSean Christopherson return;
540edc8deb0SSean Christopherson
541edc8deb0SSean Christopherson rcu_assign_pointer(cpu_emergency_virt_callback, callback);
542edc8deb0SSean Christopherson }
543edc8deb0SSean Christopherson EXPORT_SYMBOL_GPL(cpu_emergency_register_virt_callback);
544edc8deb0SSean Christopherson
cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb * callback)545edc8deb0SSean Christopherson void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback)
546edc8deb0SSean Christopherson {
547edc8deb0SSean Christopherson if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback) != callback))
548edc8deb0SSean Christopherson return;
549edc8deb0SSean Christopherson
550edc8deb0SSean Christopherson rcu_assign_pointer(cpu_emergency_virt_callback, NULL);
551edc8deb0SSean Christopherson synchronize_rcu();
552edc8deb0SSean Christopherson }
553edc8deb0SSean Christopherson EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback);
554edc8deb0SSean Christopherson
555edc8deb0SSean Christopherson /*
556edc8deb0SSean Christopherson * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
557edc8deb0SSean Christopherson * reboot. VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
558edc8deb0SSean Christopherson * GIF=0, i.e. if the crash occurred between CLGI and STGI.
559edc8deb0SSean Christopherson */
cpu_emergency_disable_virtualization(void)560edc8deb0SSean Christopherson void cpu_emergency_disable_virtualization(void)
561edc8deb0SSean Christopherson {
562edc8deb0SSean Christopherson cpu_emergency_virt_cb *callback;
563edc8deb0SSean Christopherson
564edc8deb0SSean Christopherson /*
565edc8deb0SSean Christopherson * IRQs must be disabled as KVM enables virtualization in hardware via
566edc8deb0SSean Christopherson * function call IPIs, i.e. IRQs need to be disabled to guarantee
567edc8deb0SSean Christopherson * virtualization stays disabled.
568edc8deb0SSean Christopherson */
569edc8deb0SSean Christopherson lockdep_assert_irqs_disabled();
570edc8deb0SSean Christopherson
571edc8deb0SSean Christopherson rcu_read_lock();
572edc8deb0SSean Christopherson callback = rcu_dereference(cpu_emergency_virt_callback);
573edc8deb0SSean Christopherson if (callback)
574edc8deb0SSean Christopherson callback();
575edc8deb0SSean Christopherson rcu_read_unlock();
576edc8deb0SSean Christopherson }
577edc8deb0SSean Christopherson
emergency_reboot_disable_virtualization(void)578d81f952aSSean Christopherson static void emergency_reboot_disable_virtualization(void)
5794d022e35SMiguel Boton {
5804d022e35SMiguel Boton local_irq_disable();
5814d022e35SMiguel Boton
582144d102bSMichael D Labriola /*
583d81f952aSSean Christopherson * Disable virtualization on all CPUs before rebooting to avoid hanging
584d81f952aSSean Christopherson * the system, as VMX and SVM block INIT when running in the host.
5854d022e35SMiguel Boton *
586ed727361SSean Christopherson * We can't take any locks and we may be on an inconsistent state, so
587d81f952aSSean Christopherson * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt.
5884d022e35SMiguel Boton *
589d81f952aSSean Christopherson * Do the NMI shootdown even if virtualization is off on _this_ CPU, as
590d81f952aSSean Christopherson * other CPUs may have virtualization enabled.
5914d022e35SMiguel Boton */
59259765db5SSean Christopherson if (rcu_access_pointer(cpu_emergency_virt_callback)) {
593d81f952aSSean Christopherson /* Safely force _this_ CPU out of VMX/SVM operation. */
594d81f952aSSean Christopherson cpu_emergency_disable_virtualization();
5954d022e35SMiguel Boton
596d81f952aSSean Christopherson /* Disable VMX/SVM and halt on other CPUs. */
59726044affSSean Christopherson nmi_shootdown_cpus_on_restart();
5984d022e35SMiguel Boton }
5994d022e35SMiguel Boton }
600*261cd5edSSean Christopherson #else
emergency_reboot_disable_virtualization(void)601*261cd5edSSean Christopherson static void emergency_reboot_disable_virtualization(void) { }
602*261cd5edSSean Christopherson #endif /* CONFIG_KVM_INTEL || CONFIG_KVM_AMD */
6034d022e35SMiguel Boton
mach_reboot_fixups(void)6044d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void)
6054d022e35SMiguel Boton {
6064d022e35SMiguel Boton }
6074d022e35SMiguel Boton
608660e34ceSMatthew Garrett /*
6095be44a6fSIngo Molnar * To the best of our knowledge Windows compatible x86 hardware expects
6105be44a6fSIngo Molnar * the following on reboot:
611660e34ceSMatthew Garrett *
612660e34ceSMatthew Garrett * 1) If the FADT has the ACPI reboot register flag set, try it
613660e34ceSMatthew Garrett * 2) If still alive, write to the keyboard controller
614660e34ceSMatthew Garrett * 3) If still alive, write to the ACPI reboot register again
615660e34ceSMatthew Garrett * 4) If still alive, write to the keyboard controller again
616a4f1987eSLi, Aubrey * 5) If still alive, call the EFI runtime service to reboot
6175be44a6fSIngo Molnar * 6) If no EFI runtime service, call the BIOS to do a reboot
618660e34ceSMatthew Garrett *
6195be44a6fSIngo Molnar * We default to following the same pattern. We also have
6205be44a6fSIngo Molnar * two other reboot methods: 'triple fault' and 'PCI', which
6215be44a6fSIngo Molnar * can be triggered via the reboot= kernel boot option or
6225be44a6fSIngo Molnar * via quirks.
6235be44a6fSIngo Molnar *
6245be44a6fSIngo Molnar * This means that this function can never return, it can misbehave
6255be44a6fSIngo Molnar * by not rebooting properly and hanging.
626660e34ceSMatthew Garrett */
native_machine_emergency_restart(void)6274d022e35SMiguel Boton static void native_machine_emergency_restart(void)
6284d022e35SMiguel Boton {
6294d022e35SMiguel Boton int i;
630660e34ceSMatthew Garrett int attempt = 0;
631660e34ceSMatthew Garrett int orig_reboot_type = reboot_type;
632edf2b139SRobin Holt unsigned short mode;
6334d022e35SMiguel Boton
634d176720dSEduardo Habkost if (reboot_emergency)
635d81f952aSSean Christopherson emergency_reboot_disable_virtualization();
636d176720dSEduardo Habkost
637840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_REBOOT);
638840c2bafSJoseph Cihula
6394d022e35SMiguel Boton /* Tell the BIOS if we want cold or warm reboot */
640edf2b139SRobin Holt mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
641edf2b139SRobin Holt *((unsigned short *)__va(0x472)) = mode;
6424d022e35SMiguel Boton
64387615a34SMatt Fleming /*
64487615a34SMatt Fleming * If an EFI capsule has been registered with the firmware then
64587615a34SMatt Fleming * override the reboot= parameter.
64687615a34SMatt Fleming */
64787615a34SMatt Fleming if (efi_capsule_pending(NULL)) {
64887615a34SMatt Fleming pr_info("EFI capsule is pending, forcing EFI reboot.\n");
64987615a34SMatt Fleming reboot_type = BOOT_EFI;
65087615a34SMatt Fleming }
65187615a34SMatt Fleming
6524d022e35SMiguel Boton for (;;) {
6534d022e35SMiguel Boton /* Could also try the reset bit in the Hammer NB */
6544d022e35SMiguel Boton switch (reboot_type) {
6555be44a6fSIngo Molnar case BOOT_ACPI:
6565be44a6fSIngo Molnar acpi_reboot();
6575be44a6fSIngo Molnar reboot_type = BOOT_KBD;
6585be44a6fSIngo Molnar break;
6595be44a6fSIngo Molnar
6604d022e35SMiguel Boton case BOOT_KBD:
661144d102bSMichael D Labriola mach_reboot_fixups(); /* For board specific fixups */
6627432d149SIngo Molnar
6634d022e35SMiguel Boton for (i = 0; i < 10; i++) {
6644d022e35SMiguel Boton kb_wait();
6654d022e35SMiguel Boton udelay(50);
666144d102bSMichael D Labriola outb(0xfe, 0x64); /* Pulse reset low */
6674d022e35SMiguel Boton udelay(50);
6684d022e35SMiguel Boton }
669660e34ceSMatthew Garrett if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
670660e34ceSMatthew Garrett attempt = 1;
671660e34ceSMatthew Garrett reboot_type = BOOT_ACPI;
672660e34ceSMatthew Garrett } else {
673a4f1987eSLi, Aubrey reboot_type = BOOT_EFI;
674660e34ceSMatthew Garrett }
675660e34ceSMatthew Garrett break;
6764d022e35SMiguel Boton
6774d022e35SMiguel Boton case BOOT_EFI:
6788562c99cSMatt Fleming efi_reboot(reboot_mode, NULL);
6795be44a6fSIngo Molnar reboot_type = BOOT_BIOS;
68014d7ca5cSH. Peter Anvin break;
6814d022e35SMiguel Boton
6825be44a6fSIngo Molnar case BOOT_BIOS:
6835be44a6fSIngo Molnar machine_real_restart(MRR_BIOS);
6845be44a6fSIngo Molnar
6855be44a6fSIngo Molnar /* We're probably dead after this, but... */
6865be44a6fSIngo Molnar reboot_type = BOOT_CF9_SAFE;
6875be44a6fSIngo Molnar break;
6885be44a6fSIngo Molnar
6895be44a6fSIngo Molnar case BOOT_CF9_FORCE:
69014d7ca5cSH. Peter Anvin port_cf9_safe = true;
691df561f66SGustavo A. R. Silva fallthrough;
69214d7ca5cSH. Peter Anvin
6935be44a6fSIngo Molnar case BOOT_CF9_SAFE:
69414d7ca5cSH. Peter Anvin if (port_cf9_safe) {
6955be44a6fSIngo Molnar u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
69616c21ae5SLi Fei u8 cf9 = inb(0xcf9) & ~reboot_code;
69714d7ca5cSH. Peter Anvin outb(cf9|2, 0xcf9); /* Request hard reset */
69814d7ca5cSH. Peter Anvin udelay(50);
69916c21ae5SLi Fei /* Actually do the reset */
70016c21ae5SLi Fei outb(cf9|reboot_code, 0xcf9);
70114d7ca5cSH. Peter Anvin udelay(50);
70214d7ca5cSH. Peter Anvin }
7035be44a6fSIngo Molnar reboot_type = BOOT_TRIPLE;
7045be44a6fSIngo Molnar break;
7055be44a6fSIngo Molnar
7065be44a6fSIngo Molnar case BOOT_TRIPLE:
7078ec9069aSH. Peter Anvin (Intel) idt_invalidate();
7085be44a6fSIngo Molnar __asm__ __volatile__("int3");
7095be44a6fSIngo Molnar
7105be44a6fSIngo Molnar /* We're probably dead after this, but... */
7115be44a6fSIngo Molnar reboot_type = BOOT_KBD;
7124d022e35SMiguel Boton break;
7134d022e35SMiguel Boton }
7144d022e35SMiguel Boton }
7154d022e35SMiguel Boton }
7164d022e35SMiguel Boton
native_machine_shutdown(void)7173c62c625SGlauber Costa void native_machine_shutdown(void)
7184d022e35SMiguel Boton {
7194d022e35SMiguel Boton /* Stop the cpus and apics */
720522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC
7212885432aSFenghua Yu /*
7222885432aSFenghua Yu * Disabling IO APIC before local APIC is a workaround for
7232885432aSFenghua Yu * erratum AVR31 in "Intel Atom Processor C2000 Product Family
7242885432aSFenghua Yu * Specification Update". In this situation, interrupts that target
7252885432aSFenghua Yu * a Logical Processor whose Local APIC is either in the process of
7262885432aSFenghua Yu * being hardware disabled or software disabled are neither delivered
7272885432aSFenghua Yu * nor discarded. When this erratum occurs, the processor may hang.
7282885432aSFenghua Yu *
7292885432aSFenghua Yu * Even without the erratum, it still makes sense to quiet IO APIC
7302885432aSFenghua Yu * before disabling Local APIC.
7312885432aSFenghua Yu */
732339b2ae0SBaoquan He clear_IO_APIC();
733522e6646SFenghua Yu #endif
734522e6646SFenghua Yu
7354d022e35SMiguel Boton #ifdef CONFIG_SMP
736144d102bSMichael D Labriola /*
7371b3a5d02SRobin Holt * Stop all of the others. Also disable the local irq to
7381b3a5d02SRobin Holt * not receive the per-cpu timer interrupt which may trigger
7391b3a5d02SRobin Holt * scheduler's load balance.
7404d022e35SMiguel Boton */
74155c844a4SFeng Tang local_irq_disable();
74276fac077SAlok Kataria stop_other_cpus();
7434d022e35SMiguel Boton #endif
7444d022e35SMiguel Boton
7454d022e35SMiguel Boton lapic_shutdown();
746339b2ae0SBaoquan He restore_boot_irq_mode();
7474d022e35SMiguel Boton
7484d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER
7494d022e35SMiguel Boton hpet_disable();
7504d022e35SMiguel Boton #endif
7514d022e35SMiguel Boton
7524d022e35SMiguel Boton #ifdef CONFIG_X86_64
753338bac52SFUJITA Tomonori x86_platform.iommu_shutdown();
7544d022e35SMiguel Boton #endif
7554d022e35SMiguel Boton }
7564d022e35SMiguel Boton
__machine_emergency_restart(int emergency)757d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency)
758d176720dSEduardo Habkost {
759d176720dSEduardo Habkost reboot_emergency = emergency;
760d176720dSEduardo Habkost machine_ops.emergency_restart();
761d176720dSEduardo Habkost }
762d176720dSEduardo Habkost
native_machine_restart(char * __unused)763416e2d63SJody Belka static void native_machine_restart(char *__unused)
7644d022e35SMiguel Boton {
765c767a54bSJoe Perches pr_notice("machine restart\n");
7664d022e35SMiguel Boton
7674d022e35SMiguel Boton if (!reboot_force)
7684d022e35SMiguel Boton machine_shutdown();
769d176720dSEduardo Habkost __machine_emergency_restart(0);
7704d022e35SMiguel Boton }
7714d022e35SMiguel Boton
native_machine_halt(void)772416e2d63SJody Belka static void native_machine_halt(void)
7734d022e35SMiguel Boton {
774144d102bSMichael D Labriola /* Stop other cpus and apics */
775d3ec5caeSIvan Vecera machine_shutdown();
776d3ec5caeSIvan Vecera
777840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_HALT);
778840c2bafSJoseph Cihula
779d3ec5caeSIvan Vecera stop_this_cpu(NULL);
7804d022e35SMiguel Boton }
7814d022e35SMiguel Boton
native_machine_power_off(void)782416e2d63SJody Belka static void native_machine_power_off(void)
7834d022e35SMiguel Boton {
784d3577349SDmitry Osipenko if (kernel_can_power_off()) {
7854d022e35SMiguel Boton if (!reboot_force)
7864d022e35SMiguel Boton machine_shutdown();
787d3577349SDmitry Osipenko do_kernel_power_off();
7884d022e35SMiguel Boton }
789144d102bSMichael D Labriola /* A fallback in case there is no PM info available */
790840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_HALT);
7914d022e35SMiguel Boton }
7924d022e35SMiguel Boton
793404f6aacSKees Cook struct machine_ops machine_ops __ro_after_init = {
794416e2d63SJody Belka .power_off = native_machine_power_off,
795416e2d63SJody Belka .shutdown = native_machine_shutdown,
796416e2d63SJody Belka .emergency_restart = native_machine_emergency_restart,
797416e2d63SJody Belka .restart = native_machine_restart,
798ed23dc6fSGlauber Costa .halt = native_machine_halt,
7992965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
800ed23dc6fSGlauber Costa .crash_shutdown = native_machine_crash_shutdown,
801ed23dc6fSGlauber Costa #endif
8024d022e35SMiguel Boton };
803416e2d63SJody Belka
machine_power_off(void)804416e2d63SJody Belka void machine_power_off(void)
805416e2d63SJody Belka {
806416e2d63SJody Belka machine_ops.power_off();
807416e2d63SJody Belka }
808416e2d63SJody Belka
machine_shutdown(void)809416e2d63SJody Belka void machine_shutdown(void)
810416e2d63SJody Belka {
811416e2d63SJody Belka machine_ops.shutdown();
812416e2d63SJody Belka }
813416e2d63SJody Belka
machine_emergency_restart(void)814416e2d63SJody Belka void machine_emergency_restart(void)
815416e2d63SJody Belka {
816d176720dSEduardo Habkost __machine_emergency_restart(1);
817416e2d63SJody Belka }
818416e2d63SJody Belka
machine_restart(char * cmd)819416e2d63SJody Belka void machine_restart(char *cmd)
820416e2d63SJody Belka {
821416e2d63SJody Belka machine_ops.restart(cmd);
822416e2d63SJody Belka }
823416e2d63SJody Belka
machine_halt(void)824416e2d63SJody Belka void machine_halt(void)
825416e2d63SJody Belka {
826416e2d63SJody Belka machine_ops.halt();
827416e2d63SJody Belka }
828416e2d63SJody Belka
8292965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
machine_crash_shutdown(struct pt_regs * regs)830ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs)
831ed23dc6fSGlauber Costa {
832ed23dc6fSGlauber Costa machine_ops.crash_shutdown(regs);
833ed23dc6fSGlauber Costa }
834ed23dc6fSGlauber Costa #endif
8352ddded21SEduardo Habkost
8365bc32950SXunlei Pang /* This is the CPU performing the emergency shutdown work. */
8375bc32950SXunlei Pang int crashing_cpu = -1;
8385bc32950SXunlei Pang
839bb8dd270SEduardo Habkost #if defined(CONFIG_SMP)
8402ddded21SEduardo Habkost
8412ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback;
8422ddded21SEduardo Habkost
8432ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi;
84458c5661fSHidehiro Kawai static int crash_ipi_issued;
8452ddded21SEduardo Habkost
crash_nmi_callback(unsigned int val,struct pt_regs * regs)8469c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
8472ddded21SEduardo Habkost {
8482ddded21SEduardo Habkost int cpu;
8492ddded21SEduardo Habkost
8502ddded21SEduardo Habkost cpu = raw_smp_processor_id();
8512ddded21SEduardo Habkost
852144d102bSMichael D Labriola /*
853144d102bSMichael D Labriola * Don't do anything if this handler is invoked on crashing cpu.
8542ddded21SEduardo Habkost * Otherwise, system will completely hang. Crashing cpu can get
8552ddded21SEduardo Habkost * an NMI if system was initially booted with nmi_watchdog parameter.
8562ddded21SEduardo Habkost */
8572ddded21SEduardo Habkost if (cpu == crashing_cpu)
8589c48f1c6SDon Zickus return NMI_HANDLED;
8592ddded21SEduardo Habkost local_irq_disable();
8602ddded21SEduardo Habkost
86126044affSSean Christopherson if (shootdown_callback)
8629c48f1c6SDon Zickus shootdown_callback(cpu, regs);
8632ddded21SEduardo Habkost
86426044affSSean Christopherson /*
86526044affSSean Christopherson * Prepare the CPU for reboot _after_ invoking the callback so that the
86626044affSSean Christopherson * callback can safely use virtualization instructions, e.g. VMCLEAR.
86726044affSSean Christopherson */
86826044affSSean Christopherson cpu_emergency_disable_virtualization();
86926044affSSean Christopherson
8702ddded21SEduardo Habkost atomic_dec(&waiting_for_crash_ipi);
8712ddded21SEduardo Habkost /* Assume hlt works */
8722ddded21SEduardo Habkost halt();
8732ddded21SEduardo Habkost for (;;)
8742ddded21SEduardo Habkost cpu_relax();
8752ddded21SEduardo Habkost
8769c48f1c6SDon Zickus return NMI_HANDLED;
8772ddded21SEduardo Habkost }
8782ddded21SEduardo Habkost
87926044affSSean Christopherson /**
88026044affSSean Christopherson * nmi_shootdown_cpus - Stop other CPUs via NMI
88126044affSSean Christopherson * @callback: Optional callback to be invoked from the NMI handler
882bb8dd270SEduardo Habkost *
88326044affSSean Christopherson * The NMI handler on the remote CPUs invokes @callback, if not
88426044affSSean Christopherson * NULL, first and then disables virtualization to ensure that
88526044affSSean Christopherson * INIT is recognized during reboot.
88626044affSSean Christopherson *
88726044affSSean Christopherson * nmi_shootdown_cpus() can only be invoked once. After the first
88826044affSSean Christopherson * invocation all other CPUs are stuck in crash_nmi_callback() and
88926044affSSean Christopherson * cannot respond to a second NMI.
890bb8dd270SEduardo Habkost */
nmi_shootdown_cpus(nmi_shootdown_cb callback)8912ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
8922ddded21SEduardo Habkost {
8932ddded21SEduardo Habkost unsigned long msecs;
89426044affSSean Christopherson
895c415b3dcSEduardo Habkost local_irq_disable();
8962ddded21SEduardo Habkost
89726044affSSean Christopherson /*
89826044affSSean Christopherson * Avoid certain doom if a shootdown already occurred; re-registering
89926044affSSean Christopherson * the NMI handler will cause list corruption, modifying the callback
90026044affSSean Christopherson * will do who knows what, etc...
90126044affSSean Christopherson */
90226044affSSean Christopherson if (WARN_ON_ONCE(crash_ipi_issued))
90326044affSSean Christopherson return;
90426044affSSean Christopherson
9052ddded21SEduardo Habkost /* Make a note of crashing cpu. Will be used in NMI callback. */
9062ddded21SEduardo Habkost crashing_cpu = safe_smp_processor_id();
9072ddded21SEduardo Habkost
9082ddded21SEduardo Habkost shootdown_callback = callback;
9092ddded21SEduardo Habkost
9102ddded21SEduardo Habkost atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
9112ddded21SEduardo Habkost /* Would it be better to replace the trap vector here? */
9129c48f1c6SDon Zickus if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
9139c48f1c6SDon Zickus NMI_FLAG_FIRST, "crash"))
914144d102bSMichael D Labriola return; /* Return what? */
915144d102bSMichael D Labriola /*
916144d102bSMichael D Labriola * Ensure the new callback function is set before sending
9172ddded21SEduardo Habkost * out the NMI
9182ddded21SEduardo Habkost */
9192ddded21SEduardo Habkost wmb();
9202ddded21SEduardo Habkost
92122ca7ee9SThomas Gleixner apic_send_IPI_allbutself(NMI_VECTOR);
9222ddded21SEduardo Habkost
92358c5661fSHidehiro Kawai /* Kick CPUs looping in NMI context. */
92458c5661fSHidehiro Kawai WRITE_ONCE(crash_ipi_issued, 1);
92558c5661fSHidehiro Kawai
9262ddded21SEduardo Habkost msecs = 1000; /* Wait at most a second for the other cpus to stop */
9272ddded21SEduardo Habkost while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
9282ddded21SEduardo Habkost mdelay(1);
9292ddded21SEduardo Habkost msecs--;
9302ddded21SEduardo Habkost }
9312ddded21SEduardo Habkost
93226044affSSean Christopherson /*
93326044affSSean Christopherson * Leave the nmi callback set, shootdown is a one-time thing. Clearing
93426044affSSean Christopherson * the callback could result in a NULL pointer dereference if a CPU
93526044affSSean Christopherson * (finally) responds after the timeout expires.
93626044affSSean Christopherson */
93726044affSSean Christopherson }
93826044affSSean Christopherson
nmi_shootdown_cpus_on_restart(void)93926044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void)
94026044affSSean Christopherson {
94126044affSSean Christopherson if (!crash_ipi_issued)
94226044affSSean Christopherson nmi_shootdown_cpus(NULL);
9432ddded21SEduardo Habkost }
94458c5661fSHidehiro Kawai
945b279d67dSHidehiro Kawai /*
946b279d67dSHidehiro Kawai * Check if the crash dumping IPI got issued and if so, call its callback
947b279d67dSHidehiro Kawai * directly. This function is used when we have already been in NMI handler.
948b279d67dSHidehiro Kawai * It doesn't return.
949b279d67dSHidehiro Kawai */
run_crash_ipi_callback(struct pt_regs * regs)950b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
951b279d67dSHidehiro Kawai {
952b279d67dSHidehiro Kawai if (crash_ipi_issued)
953b279d67dSHidehiro Kawai crash_nmi_callback(0, regs);
954b279d67dSHidehiro Kawai }
955b279d67dSHidehiro Kawai
95658c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */
nmi_panic_self_stop(struct pt_regs * regs)95727dea14cSJosh Poimboeuf void __noreturn nmi_panic_self_stop(struct pt_regs *regs)
95858c5661fSHidehiro Kawai {
95958c5661fSHidehiro Kawai while (1) {
960b279d67dSHidehiro Kawai /* If no CPU is preparing crash dump, we simply loop here. */
961b279d67dSHidehiro Kawai run_crash_ipi_callback(regs);
96258c5661fSHidehiro Kawai cpu_relax();
96358c5661fSHidehiro Kawai }
96458c5661fSHidehiro Kawai }
96558c5661fSHidehiro Kawai
966bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */
nmi_shootdown_cpus(nmi_shootdown_cb callback)967bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
968bb8dd270SEduardo Habkost {
969bb8dd270SEduardo Habkost /* No other CPUs to shoot down */
970bb8dd270SEduardo Habkost }
971b279d67dSHidehiro Kawai
nmi_shootdown_cpus_on_restart(void)97226044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void) { }
97326044affSSean Christopherson
run_crash_ipi_callback(struct pt_regs * regs)974b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
975b279d67dSHidehiro Kawai {
976b279d67dSHidehiro Kawai }
9772ddded21SEduardo Habkost #endif
978