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 */ 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 */ 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 */ 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 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 */ 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 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 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 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 532edc8deb0SSean Christopherson /* RCU-protected callback to disable virtualization prior to reboot. */ 533edc8deb0SSean Christopherson static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback; 534edc8deb0SSean Christopherson 535edc8deb0SSean Christopherson void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback) 536edc8deb0SSean Christopherson { 537edc8deb0SSean Christopherson if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback))) 538edc8deb0SSean Christopherson return; 539edc8deb0SSean Christopherson 540edc8deb0SSean Christopherson rcu_assign_pointer(cpu_emergency_virt_callback, callback); 541edc8deb0SSean Christopherson } 542edc8deb0SSean Christopherson EXPORT_SYMBOL_GPL(cpu_emergency_register_virt_callback); 543edc8deb0SSean Christopherson 544edc8deb0SSean Christopherson void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback) 545edc8deb0SSean Christopherson { 546edc8deb0SSean Christopherson if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback) != callback)) 547edc8deb0SSean Christopherson return; 548edc8deb0SSean Christopherson 549edc8deb0SSean Christopherson rcu_assign_pointer(cpu_emergency_virt_callback, NULL); 550edc8deb0SSean Christopherson synchronize_rcu(); 551edc8deb0SSean Christopherson } 552edc8deb0SSean Christopherson EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback); 553edc8deb0SSean Christopherson 554edc8deb0SSean Christopherson /* 555edc8deb0SSean Christopherson * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during 556edc8deb0SSean Christopherson * reboot. VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if 557edc8deb0SSean Christopherson * GIF=0, i.e. if the crash occurred between CLGI and STGI. 558edc8deb0SSean Christopherson */ 559edc8deb0SSean Christopherson void cpu_emergency_disable_virtualization(void) 560edc8deb0SSean Christopherson { 561edc8deb0SSean Christopherson cpu_emergency_virt_cb *callback; 562edc8deb0SSean Christopherson 563edc8deb0SSean Christopherson /* 564edc8deb0SSean Christopherson * IRQs must be disabled as KVM enables virtualization in hardware via 565edc8deb0SSean Christopherson * function call IPIs, i.e. IRQs need to be disabled to guarantee 566edc8deb0SSean Christopherson * virtualization stays disabled. 567edc8deb0SSean Christopherson */ 568edc8deb0SSean Christopherson lockdep_assert_irqs_disabled(); 569edc8deb0SSean Christopherson 570edc8deb0SSean Christopherson rcu_read_lock(); 571edc8deb0SSean Christopherson callback = rcu_dereference(cpu_emergency_virt_callback); 572edc8deb0SSean Christopherson if (callback) 573edc8deb0SSean Christopherson callback(); 574edc8deb0SSean Christopherson rcu_read_unlock(); 575edc8deb0SSean Christopherson } 576edc8deb0SSean Christopherson 577d81f952aSSean Christopherson static void emergency_reboot_disable_virtualization(void) 5784d022e35SMiguel Boton { 5794d022e35SMiguel Boton local_irq_disable(); 5804d022e35SMiguel Boton 581144d102bSMichael D Labriola /* 582d81f952aSSean Christopherson * Disable virtualization on all CPUs before rebooting to avoid hanging 583d81f952aSSean Christopherson * the system, as VMX and SVM block INIT when running in the host. 5844d022e35SMiguel Boton * 585ed727361SSean Christopherson * We can't take any locks and we may be on an inconsistent state, so 586d81f952aSSean Christopherson * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt. 5874d022e35SMiguel Boton * 588d81f952aSSean Christopherson * Do the NMI shootdown even if virtualization is off on _this_ CPU, as 589d81f952aSSean Christopherson * other CPUs may have virtualization enabled. 5904d022e35SMiguel Boton */ 591*59765db5SSean Christopherson if (rcu_access_pointer(cpu_emergency_virt_callback)) { 592d81f952aSSean Christopherson /* Safely force _this_ CPU out of VMX/SVM operation. */ 593d81f952aSSean Christopherson cpu_emergency_disable_virtualization(); 5944d022e35SMiguel Boton 595d81f952aSSean Christopherson /* Disable VMX/SVM and halt on other CPUs. */ 59626044affSSean Christopherson nmi_shootdown_cpus_on_restart(); 5974d022e35SMiguel Boton } 5984d022e35SMiguel Boton } 5994d022e35SMiguel Boton 6004d022e35SMiguel Boton 6014d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void) 6024d022e35SMiguel Boton { 6034d022e35SMiguel Boton } 6044d022e35SMiguel Boton 605660e34ceSMatthew Garrett /* 6065be44a6fSIngo Molnar * To the best of our knowledge Windows compatible x86 hardware expects 6075be44a6fSIngo Molnar * the following on reboot: 608660e34ceSMatthew Garrett * 609660e34ceSMatthew Garrett * 1) If the FADT has the ACPI reboot register flag set, try it 610660e34ceSMatthew Garrett * 2) If still alive, write to the keyboard controller 611660e34ceSMatthew Garrett * 3) If still alive, write to the ACPI reboot register again 612660e34ceSMatthew Garrett * 4) If still alive, write to the keyboard controller again 613a4f1987eSLi, Aubrey * 5) If still alive, call the EFI runtime service to reboot 6145be44a6fSIngo Molnar * 6) If no EFI runtime service, call the BIOS to do a reboot 615660e34ceSMatthew Garrett * 6165be44a6fSIngo Molnar * We default to following the same pattern. We also have 6175be44a6fSIngo Molnar * two other reboot methods: 'triple fault' and 'PCI', which 6185be44a6fSIngo Molnar * can be triggered via the reboot= kernel boot option or 6195be44a6fSIngo Molnar * via quirks. 6205be44a6fSIngo Molnar * 6215be44a6fSIngo Molnar * This means that this function can never return, it can misbehave 6225be44a6fSIngo Molnar * by not rebooting properly and hanging. 623660e34ceSMatthew Garrett */ 6244d022e35SMiguel Boton static void native_machine_emergency_restart(void) 6254d022e35SMiguel Boton { 6264d022e35SMiguel Boton int i; 627660e34ceSMatthew Garrett int attempt = 0; 628660e34ceSMatthew Garrett int orig_reboot_type = reboot_type; 629edf2b139SRobin Holt unsigned short mode; 6304d022e35SMiguel Boton 631d176720dSEduardo Habkost if (reboot_emergency) 632d81f952aSSean Christopherson emergency_reboot_disable_virtualization(); 633d176720dSEduardo Habkost 634840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_REBOOT); 635840c2bafSJoseph Cihula 6364d022e35SMiguel Boton /* Tell the BIOS if we want cold or warm reboot */ 637edf2b139SRobin Holt mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0; 638edf2b139SRobin Holt *((unsigned short *)__va(0x472)) = mode; 6394d022e35SMiguel Boton 64087615a34SMatt Fleming /* 64187615a34SMatt Fleming * If an EFI capsule has been registered with the firmware then 64287615a34SMatt Fleming * override the reboot= parameter. 64387615a34SMatt Fleming */ 64487615a34SMatt Fleming if (efi_capsule_pending(NULL)) { 64587615a34SMatt Fleming pr_info("EFI capsule is pending, forcing EFI reboot.\n"); 64687615a34SMatt Fleming reboot_type = BOOT_EFI; 64787615a34SMatt Fleming } 64887615a34SMatt Fleming 6494d022e35SMiguel Boton for (;;) { 6504d022e35SMiguel Boton /* Could also try the reset bit in the Hammer NB */ 6514d022e35SMiguel Boton switch (reboot_type) { 6525be44a6fSIngo Molnar case BOOT_ACPI: 6535be44a6fSIngo Molnar acpi_reboot(); 6545be44a6fSIngo Molnar reboot_type = BOOT_KBD; 6555be44a6fSIngo Molnar break; 6565be44a6fSIngo Molnar 6574d022e35SMiguel Boton case BOOT_KBD: 658144d102bSMichael D Labriola mach_reboot_fixups(); /* For board specific fixups */ 6597432d149SIngo Molnar 6604d022e35SMiguel Boton for (i = 0; i < 10; i++) { 6614d022e35SMiguel Boton kb_wait(); 6624d022e35SMiguel Boton udelay(50); 663144d102bSMichael D Labriola outb(0xfe, 0x64); /* Pulse reset low */ 6644d022e35SMiguel Boton udelay(50); 6654d022e35SMiguel Boton } 666660e34ceSMatthew Garrett if (attempt == 0 && orig_reboot_type == BOOT_ACPI) { 667660e34ceSMatthew Garrett attempt = 1; 668660e34ceSMatthew Garrett reboot_type = BOOT_ACPI; 669660e34ceSMatthew Garrett } else { 670a4f1987eSLi, Aubrey reboot_type = BOOT_EFI; 671660e34ceSMatthew Garrett } 672660e34ceSMatthew Garrett break; 6734d022e35SMiguel Boton 6744d022e35SMiguel Boton case BOOT_EFI: 6758562c99cSMatt Fleming efi_reboot(reboot_mode, NULL); 6765be44a6fSIngo Molnar reboot_type = BOOT_BIOS; 67714d7ca5cSH. Peter Anvin break; 6784d022e35SMiguel Boton 6795be44a6fSIngo Molnar case BOOT_BIOS: 6805be44a6fSIngo Molnar machine_real_restart(MRR_BIOS); 6815be44a6fSIngo Molnar 6825be44a6fSIngo Molnar /* We're probably dead after this, but... */ 6835be44a6fSIngo Molnar reboot_type = BOOT_CF9_SAFE; 6845be44a6fSIngo Molnar break; 6855be44a6fSIngo Molnar 6865be44a6fSIngo Molnar case BOOT_CF9_FORCE: 68714d7ca5cSH. Peter Anvin port_cf9_safe = true; 688df561f66SGustavo A. R. Silva fallthrough; 68914d7ca5cSH. Peter Anvin 6905be44a6fSIngo Molnar case BOOT_CF9_SAFE: 69114d7ca5cSH. Peter Anvin if (port_cf9_safe) { 6925be44a6fSIngo Molnar u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E; 69316c21ae5SLi Fei u8 cf9 = inb(0xcf9) & ~reboot_code; 69414d7ca5cSH. Peter Anvin outb(cf9|2, 0xcf9); /* Request hard reset */ 69514d7ca5cSH. Peter Anvin udelay(50); 69616c21ae5SLi Fei /* Actually do the reset */ 69716c21ae5SLi Fei outb(cf9|reboot_code, 0xcf9); 69814d7ca5cSH. Peter Anvin udelay(50); 69914d7ca5cSH. Peter Anvin } 7005be44a6fSIngo Molnar reboot_type = BOOT_TRIPLE; 7015be44a6fSIngo Molnar break; 7025be44a6fSIngo Molnar 7035be44a6fSIngo Molnar case BOOT_TRIPLE: 7048ec9069aSH. Peter Anvin (Intel) idt_invalidate(); 7055be44a6fSIngo Molnar __asm__ __volatile__("int3"); 7065be44a6fSIngo Molnar 7075be44a6fSIngo Molnar /* We're probably dead after this, but... */ 7085be44a6fSIngo Molnar reboot_type = BOOT_KBD; 7094d022e35SMiguel Boton break; 7104d022e35SMiguel Boton } 7114d022e35SMiguel Boton } 7124d022e35SMiguel Boton } 7134d022e35SMiguel Boton 7143c62c625SGlauber Costa void native_machine_shutdown(void) 7154d022e35SMiguel Boton { 7164d022e35SMiguel Boton /* Stop the cpus and apics */ 717522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC 7182885432aSFenghua Yu /* 7192885432aSFenghua Yu * Disabling IO APIC before local APIC is a workaround for 7202885432aSFenghua Yu * erratum AVR31 in "Intel Atom Processor C2000 Product Family 7212885432aSFenghua Yu * Specification Update". In this situation, interrupts that target 7222885432aSFenghua Yu * a Logical Processor whose Local APIC is either in the process of 7232885432aSFenghua Yu * being hardware disabled or software disabled are neither delivered 7242885432aSFenghua Yu * nor discarded. When this erratum occurs, the processor may hang. 7252885432aSFenghua Yu * 7262885432aSFenghua Yu * Even without the erratum, it still makes sense to quiet IO APIC 7272885432aSFenghua Yu * before disabling Local APIC. 7282885432aSFenghua Yu */ 729339b2ae0SBaoquan He clear_IO_APIC(); 730522e6646SFenghua Yu #endif 731522e6646SFenghua Yu 7324d022e35SMiguel Boton #ifdef CONFIG_SMP 733144d102bSMichael D Labriola /* 7341b3a5d02SRobin Holt * Stop all of the others. Also disable the local irq to 7351b3a5d02SRobin Holt * not receive the per-cpu timer interrupt which may trigger 7361b3a5d02SRobin Holt * scheduler's load balance. 7374d022e35SMiguel Boton */ 73855c844a4SFeng Tang local_irq_disable(); 73976fac077SAlok Kataria stop_other_cpus(); 7404d022e35SMiguel Boton #endif 7414d022e35SMiguel Boton 7424d022e35SMiguel Boton lapic_shutdown(); 743339b2ae0SBaoquan He restore_boot_irq_mode(); 7444d022e35SMiguel Boton 7454d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER 7464d022e35SMiguel Boton hpet_disable(); 7474d022e35SMiguel Boton #endif 7484d022e35SMiguel Boton 7494d022e35SMiguel Boton #ifdef CONFIG_X86_64 750338bac52SFUJITA Tomonori x86_platform.iommu_shutdown(); 7514d022e35SMiguel Boton #endif 7524d022e35SMiguel Boton } 7534d022e35SMiguel Boton 754d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency) 755d176720dSEduardo Habkost { 756d176720dSEduardo Habkost reboot_emergency = emergency; 757d176720dSEduardo Habkost machine_ops.emergency_restart(); 758d176720dSEduardo Habkost } 759d176720dSEduardo Habkost 760416e2d63SJody Belka static void native_machine_restart(char *__unused) 7614d022e35SMiguel Boton { 762c767a54bSJoe Perches pr_notice("machine restart\n"); 7634d022e35SMiguel Boton 7644d022e35SMiguel Boton if (!reboot_force) 7654d022e35SMiguel Boton machine_shutdown(); 766d176720dSEduardo Habkost __machine_emergency_restart(0); 7674d022e35SMiguel Boton } 7684d022e35SMiguel Boton 769416e2d63SJody Belka static void native_machine_halt(void) 7704d022e35SMiguel Boton { 771144d102bSMichael D Labriola /* Stop other cpus and apics */ 772d3ec5caeSIvan Vecera machine_shutdown(); 773d3ec5caeSIvan Vecera 774840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_HALT); 775840c2bafSJoseph Cihula 776d3ec5caeSIvan Vecera stop_this_cpu(NULL); 7774d022e35SMiguel Boton } 7784d022e35SMiguel Boton 779416e2d63SJody Belka static void native_machine_power_off(void) 7804d022e35SMiguel Boton { 781d3577349SDmitry Osipenko if (kernel_can_power_off()) { 7824d022e35SMiguel Boton if (!reboot_force) 7834d022e35SMiguel Boton machine_shutdown(); 784d3577349SDmitry Osipenko do_kernel_power_off(); 7854d022e35SMiguel Boton } 786144d102bSMichael D Labriola /* A fallback in case there is no PM info available */ 787840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_HALT); 7884d022e35SMiguel Boton } 7894d022e35SMiguel Boton 790404f6aacSKees Cook struct machine_ops machine_ops __ro_after_init = { 791416e2d63SJody Belka .power_off = native_machine_power_off, 792416e2d63SJody Belka .shutdown = native_machine_shutdown, 793416e2d63SJody Belka .emergency_restart = native_machine_emergency_restart, 794416e2d63SJody Belka .restart = native_machine_restart, 795ed23dc6fSGlauber Costa .halt = native_machine_halt, 7962965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 797ed23dc6fSGlauber Costa .crash_shutdown = native_machine_crash_shutdown, 798ed23dc6fSGlauber Costa #endif 7994d022e35SMiguel Boton }; 800416e2d63SJody Belka 801416e2d63SJody Belka void machine_power_off(void) 802416e2d63SJody Belka { 803416e2d63SJody Belka machine_ops.power_off(); 804416e2d63SJody Belka } 805416e2d63SJody Belka 806416e2d63SJody Belka void machine_shutdown(void) 807416e2d63SJody Belka { 808416e2d63SJody Belka machine_ops.shutdown(); 809416e2d63SJody Belka } 810416e2d63SJody Belka 811416e2d63SJody Belka void machine_emergency_restart(void) 812416e2d63SJody Belka { 813d176720dSEduardo Habkost __machine_emergency_restart(1); 814416e2d63SJody Belka } 815416e2d63SJody Belka 816416e2d63SJody Belka void machine_restart(char *cmd) 817416e2d63SJody Belka { 818416e2d63SJody Belka machine_ops.restart(cmd); 819416e2d63SJody Belka } 820416e2d63SJody Belka 821416e2d63SJody Belka void machine_halt(void) 822416e2d63SJody Belka { 823416e2d63SJody Belka machine_ops.halt(); 824416e2d63SJody Belka } 825416e2d63SJody Belka 8262965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 827ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs) 828ed23dc6fSGlauber Costa { 829ed23dc6fSGlauber Costa machine_ops.crash_shutdown(regs); 830ed23dc6fSGlauber Costa } 831ed23dc6fSGlauber Costa #endif 8322ddded21SEduardo Habkost 8335bc32950SXunlei Pang /* This is the CPU performing the emergency shutdown work. */ 8345bc32950SXunlei Pang int crashing_cpu = -1; 8355bc32950SXunlei Pang 836bb8dd270SEduardo Habkost #if defined(CONFIG_SMP) 8372ddded21SEduardo Habkost 8382ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback; 8392ddded21SEduardo Habkost 8402ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi; 84158c5661fSHidehiro Kawai static int crash_ipi_issued; 8422ddded21SEduardo Habkost 8439c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs) 8442ddded21SEduardo Habkost { 8452ddded21SEduardo Habkost int cpu; 8462ddded21SEduardo Habkost 8472ddded21SEduardo Habkost cpu = raw_smp_processor_id(); 8482ddded21SEduardo Habkost 849144d102bSMichael D Labriola /* 850144d102bSMichael D Labriola * Don't do anything if this handler is invoked on crashing cpu. 8512ddded21SEduardo Habkost * Otherwise, system will completely hang. Crashing cpu can get 8522ddded21SEduardo Habkost * an NMI if system was initially booted with nmi_watchdog parameter. 8532ddded21SEduardo Habkost */ 8542ddded21SEduardo Habkost if (cpu == crashing_cpu) 8559c48f1c6SDon Zickus return NMI_HANDLED; 8562ddded21SEduardo Habkost local_irq_disable(); 8572ddded21SEduardo Habkost 85826044affSSean Christopherson if (shootdown_callback) 8599c48f1c6SDon Zickus shootdown_callback(cpu, regs); 8602ddded21SEduardo Habkost 86126044affSSean Christopherson /* 86226044affSSean Christopherson * Prepare the CPU for reboot _after_ invoking the callback so that the 86326044affSSean Christopherson * callback can safely use virtualization instructions, e.g. VMCLEAR. 86426044affSSean Christopherson */ 86526044affSSean Christopherson cpu_emergency_disable_virtualization(); 86626044affSSean Christopherson 8672ddded21SEduardo Habkost atomic_dec(&waiting_for_crash_ipi); 8682ddded21SEduardo Habkost /* Assume hlt works */ 8692ddded21SEduardo Habkost halt(); 8702ddded21SEduardo Habkost for (;;) 8712ddded21SEduardo Habkost cpu_relax(); 8722ddded21SEduardo Habkost 8739c48f1c6SDon Zickus return NMI_HANDLED; 8742ddded21SEduardo Habkost } 8752ddded21SEduardo Habkost 87626044affSSean Christopherson /** 87726044affSSean Christopherson * nmi_shootdown_cpus - Stop other CPUs via NMI 87826044affSSean Christopherson * @callback: Optional callback to be invoked from the NMI handler 879bb8dd270SEduardo Habkost * 88026044affSSean Christopherson * The NMI handler on the remote CPUs invokes @callback, if not 88126044affSSean Christopherson * NULL, first and then disables virtualization to ensure that 88226044affSSean Christopherson * INIT is recognized during reboot. 88326044affSSean Christopherson * 88426044affSSean Christopherson * nmi_shootdown_cpus() can only be invoked once. After the first 88526044affSSean Christopherson * invocation all other CPUs are stuck in crash_nmi_callback() and 88626044affSSean Christopherson * cannot respond to a second NMI. 887bb8dd270SEduardo Habkost */ 8882ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback) 8892ddded21SEduardo Habkost { 8902ddded21SEduardo Habkost unsigned long msecs; 89126044affSSean Christopherson 892c415b3dcSEduardo Habkost local_irq_disable(); 8932ddded21SEduardo Habkost 89426044affSSean Christopherson /* 89526044affSSean Christopherson * Avoid certain doom if a shootdown already occurred; re-registering 89626044affSSean Christopherson * the NMI handler will cause list corruption, modifying the callback 89726044affSSean Christopherson * will do who knows what, etc... 89826044affSSean Christopherson */ 89926044affSSean Christopherson if (WARN_ON_ONCE(crash_ipi_issued)) 90026044affSSean Christopherson return; 90126044affSSean Christopherson 9022ddded21SEduardo Habkost /* Make a note of crashing cpu. Will be used in NMI callback. */ 9032ddded21SEduardo Habkost crashing_cpu = safe_smp_processor_id(); 9042ddded21SEduardo Habkost 9052ddded21SEduardo Habkost shootdown_callback = callback; 9062ddded21SEduardo Habkost 9072ddded21SEduardo Habkost atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); 9082ddded21SEduardo Habkost /* Would it be better to replace the trap vector here? */ 9099c48f1c6SDon Zickus if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback, 9109c48f1c6SDon Zickus NMI_FLAG_FIRST, "crash")) 911144d102bSMichael D Labriola return; /* Return what? */ 912144d102bSMichael D Labriola /* 913144d102bSMichael D Labriola * Ensure the new callback function is set before sending 9142ddded21SEduardo Habkost * out the NMI 9152ddded21SEduardo Habkost */ 9162ddded21SEduardo Habkost wmb(); 9172ddded21SEduardo Habkost 91822ca7ee9SThomas Gleixner apic_send_IPI_allbutself(NMI_VECTOR); 9192ddded21SEduardo Habkost 92058c5661fSHidehiro Kawai /* Kick CPUs looping in NMI context. */ 92158c5661fSHidehiro Kawai WRITE_ONCE(crash_ipi_issued, 1); 92258c5661fSHidehiro Kawai 9232ddded21SEduardo Habkost msecs = 1000; /* Wait at most a second for the other cpus to stop */ 9242ddded21SEduardo Habkost while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { 9252ddded21SEduardo Habkost mdelay(1); 9262ddded21SEduardo Habkost msecs--; 9272ddded21SEduardo Habkost } 9282ddded21SEduardo Habkost 92926044affSSean Christopherson /* 93026044affSSean Christopherson * Leave the nmi callback set, shootdown is a one-time thing. Clearing 93126044affSSean Christopherson * the callback could result in a NULL pointer dereference if a CPU 93226044affSSean Christopherson * (finally) responds after the timeout expires. 93326044affSSean Christopherson */ 93426044affSSean Christopherson } 93526044affSSean Christopherson 93626044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void) 93726044affSSean Christopherson { 93826044affSSean Christopherson if (!crash_ipi_issued) 93926044affSSean Christopherson nmi_shootdown_cpus(NULL); 9402ddded21SEduardo Habkost } 94158c5661fSHidehiro Kawai 942b279d67dSHidehiro Kawai /* 943b279d67dSHidehiro Kawai * Check if the crash dumping IPI got issued and if so, call its callback 944b279d67dSHidehiro Kawai * directly. This function is used when we have already been in NMI handler. 945b279d67dSHidehiro Kawai * It doesn't return. 946b279d67dSHidehiro Kawai */ 947b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs) 948b279d67dSHidehiro Kawai { 949b279d67dSHidehiro Kawai if (crash_ipi_issued) 950b279d67dSHidehiro Kawai crash_nmi_callback(0, regs); 951b279d67dSHidehiro Kawai } 952b279d67dSHidehiro Kawai 95358c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */ 95427dea14cSJosh Poimboeuf void __noreturn nmi_panic_self_stop(struct pt_regs *regs) 95558c5661fSHidehiro Kawai { 95658c5661fSHidehiro Kawai while (1) { 957b279d67dSHidehiro Kawai /* If no CPU is preparing crash dump, we simply loop here. */ 958b279d67dSHidehiro Kawai run_crash_ipi_callback(regs); 95958c5661fSHidehiro Kawai cpu_relax(); 96058c5661fSHidehiro Kawai } 96158c5661fSHidehiro Kawai } 96258c5661fSHidehiro Kawai 963bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */ 964bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback) 965bb8dd270SEduardo Habkost { 966bb8dd270SEduardo Habkost /* No other CPUs to shoot down */ 967bb8dd270SEduardo Habkost } 968b279d67dSHidehiro Kawai 96926044affSSean Christopherson static inline void nmi_shootdown_cpus_on_restart(void) { } 97026044affSSean Christopherson 971b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs) 972b279d67dSHidehiro Kawai { 973b279d67dSHidehiro Kawai } 9742ddded21SEduardo Habkost #endif 975