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> 13c207aee4SJosh Poimboeuf #include <linux/frame.h> 144d022e35SMiguel Boton #include <acpi/reboot.h> 154d022e35SMiguel Boton #include <asm/io.h> 164d022e35SMiguel Boton #include <asm/apic.h> 178643e28dSJiang Liu #include <asm/io_apic.h> 184d022e35SMiguel Boton #include <asm/desc.h> 194d022e35SMiguel Boton #include <asm/hpet.h> 2068db065cSJeremy Fitzhardinge #include <asm/pgtable.h> 214412620fSDmitri Vorobiev #include <asm/proto.h> 224d022e35SMiguel Boton #include <asm/reboot_fixups.h> 234d022e35SMiguel Boton #include <asm/reboot.h> 2482487711SJaswinder Singh Rajput #include <asm/pci_x86.h> 25d176720dSEduardo Habkost #include <asm/virtext.h> 2696b89dc6SJaswinder Singh Rajput #include <asm/cpu.h> 27c410b830SDon Zickus #include <asm/nmi.h> 2865051397SH. Peter Anvin #include <asm/smp.h> 294d022e35SMiguel Boton 304d022e35SMiguel Boton #include <linux/ctype.h> 314d022e35SMiguel Boton #include <linux/mc146818rtc.h> 325a8c9aebSJarkko Sakkinen #include <asm/realmode.h> 33338bac52SFUJITA Tomonori #include <asm/x86_init.h> 3444be28e9SMatt Fleming #include <asm/efi.h> 354d022e35SMiguel Boton 364d022e35SMiguel Boton /* 374d022e35SMiguel Boton * Power off function, if any 384d022e35SMiguel Boton */ 394d022e35SMiguel Boton void (*pm_power_off)(void); 404d022e35SMiguel Boton EXPORT_SYMBOL(pm_power_off); 414d022e35SMiguel Boton 42144d102bSMichael D Labriola /* 43144d102bSMichael D Labriola * This is set if we need to go through the 'emergency' path. 44d176720dSEduardo Habkost * When machine_emergency_restart() is called, we may be on 45d176720dSEduardo Habkost * an inconsistent state and won't be able to do a clean cleanup 46d176720dSEduardo Habkost */ 47d176720dSEduardo Habkost static int reboot_emergency; 48d176720dSEduardo Habkost 4914d7ca5cSH. Peter Anvin /* This is set by the PCI code if either type 1 or type 2 PCI is detected */ 5014d7ca5cSH. Peter Anvin bool port_cf9_safe = false; 5114d7ca5cSH. Peter Anvin 52144d102bSMichael D Labriola /* 534d022e35SMiguel Boton * Reboot options and system auto-detection code provided by 544d022e35SMiguel Boton * Dell Inc. so their systems "just work". :-) 554d022e35SMiguel Boton */ 564d022e35SMiguel Boton 574d022e35SMiguel Boton /* 584d581259SAlex Hung * Some machines require the "reboot=a" commandline options 594d581259SAlex Hung */ 604d581259SAlex Hung static int __init set_acpi_reboot(const struct dmi_system_id *d) 614d581259SAlex Hung { 624d581259SAlex Hung if (reboot_type != BOOT_ACPI) { 634d581259SAlex Hung reboot_type = BOOT_ACPI; 644d581259SAlex Hung pr_info("%s series board detected. Selecting %s-method for reboots.\n", 654d581259SAlex Hung d->ident, "ACPI"); 664d581259SAlex Hung } 674d581259SAlex Hung return 0; 684d581259SAlex Hung } 694d581259SAlex Hung 704d581259SAlex Hung /* 711ef03890SPeter Chubb * Some machines require the "reboot=b" or "reboot=k" commandline options, 724d022e35SMiguel Boton * this quirk makes that automatic. 734d022e35SMiguel Boton */ 744d022e35SMiguel Boton static int __init set_bios_reboot(const struct dmi_system_id *d) 754d022e35SMiguel Boton { 764d022e35SMiguel Boton if (reboot_type != BOOT_BIOS) { 774d022e35SMiguel Boton reboot_type = BOOT_BIOS; 78c767a54bSJoe Perches pr_info("%s series board detected. Selecting %s-method for reboots.\n", 796d9153bbSLan Tianyu d->ident, "BIOS"); 804d022e35SMiguel Boton } 814d022e35SMiguel Boton return 0; 824d022e35SMiguel Boton } 834d022e35SMiguel Boton 8465051397SH. Peter Anvin void __noreturn machine_real_restart(unsigned int type) 8557b16594SMichael D Labriola { 8657b16594SMichael D Labriola local_irq_disable(); 8757b16594SMichael D Labriola 88144d102bSMichael D Labriola /* 89144d102bSMichael D Labriola * Write zero to CMOS register number 0x0f, which the BIOS POST 90144d102bSMichael D Labriola * routine will recognize as telling it to do a proper reboot. (Well 91144d102bSMichael D Labriola * that's what this book in front of me says -- it may only apply to 92144d102bSMichael D Labriola * the Phoenix BIOS though, it's not clear). At the same time, 93144d102bSMichael D Labriola * disable NMIs by setting the top bit in the CMOS address register, 94144d102bSMichael D Labriola * as we're about to do peculiar things to the CPU. I'm not sure if 95144d102bSMichael D Labriola * `outb_p' is needed instead of just `outb'. Use it to be on the 96144d102bSMichael D Labriola * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.) 9757b16594SMichael D Labriola */ 9857b16594SMichael D Labriola spin_lock(&rtc_lock); 9957b16594SMichael D Labriola CMOS_WRITE(0x00, 0x8f); 10057b16594SMichael D Labriola spin_unlock(&rtc_lock); 10157b16594SMichael D Labriola 10257b16594SMichael D Labriola /* 10357b16594SMichael D Labriola * Switch back to the initial page table. 10457b16594SMichael D Labriola */ 10565051397SH. Peter Anvin #ifdef CONFIG_X86_32 10657b16594SMichael D Labriola load_cr3(initial_page_table); 10765051397SH. Peter Anvin #else 10865051397SH. Peter Anvin write_cr3(real_mode_header->trampoline_pgd); 109924c6b90SAndy Lutomirski 110924c6b90SAndy Lutomirski /* Exiting long mode will fail if CR4.PCIDE is set. */ 111924c6b90SAndy Lutomirski if (static_cpu_has(X86_FEATURE_PCID)) 112924c6b90SAndy Lutomirski cr4_clear_bits(X86_CR4_PCIDE); 11365051397SH. Peter Anvin #endif 11457b16594SMichael D Labriola 11557b16594SMichael D Labriola /* Jump to the identity-mapped low memory code */ 11665051397SH. Peter Anvin #ifdef CONFIG_X86_32 11765051397SH. Peter Anvin asm volatile("jmpl *%0" : : 11865051397SH. Peter Anvin "rm" (real_mode_header->machine_real_restart_asm), 11965051397SH. Peter Anvin "a" (type)); 12065051397SH. Peter Anvin #else 12165051397SH. Peter Anvin asm volatile("ljmpl *%0" : : 12265051397SH. Peter Anvin "m" (real_mode_header->machine_real_restart_asm), 12365051397SH. Peter Anvin "D" (type)); 12465051397SH. Peter Anvin #endif 12565051397SH. Peter Anvin unreachable(); 12657b16594SMichael D Labriola } 12757b16594SMichael D Labriola #ifdef CONFIG_APM_MODULE 12857b16594SMichael D Labriola EXPORT_SYMBOL(machine_real_restart); 12957b16594SMichael D Labriola #endif 130c207aee4SJosh Poimboeuf STACK_FRAME_NON_STANDARD(machine_real_restart); 13157b16594SMichael D Labriola 13257b16594SMichael D Labriola /* 13357b16594SMichael D Labriola * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot 13457b16594SMichael D Labriola */ 13557b16594SMichael D Labriola static int __init set_pci_reboot(const struct dmi_system_id *d) 13657b16594SMichael D Labriola { 1375be44a6fSIngo Molnar if (reboot_type != BOOT_CF9_FORCE) { 1385be44a6fSIngo Molnar reboot_type = BOOT_CF9_FORCE; 139c767a54bSJoe Perches pr_info("%s series board detected. Selecting %s-method for reboots.\n", 1406d9153bbSLan Tianyu d->ident, "PCI"); 14157b16594SMichael D Labriola } 14257b16594SMichael D Labriola return 0; 14357b16594SMichael D Labriola } 14457b16594SMichael D Labriola 1451ef03890SPeter Chubb static int __init set_kbd_reboot(const struct dmi_system_id *d) 1461ef03890SPeter Chubb { 1471ef03890SPeter Chubb if (reboot_type != BOOT_KBD) { 1481ef03890SPeter Chubb reboot_type = BOOT_KBD; 149c767a54bSJoe Perches pr_info("%s series board detected. Selecting %s-method for reboot.\n", 1506d9153bbSLan Tianyu d->ident, "KBD"); 1511ef03890SPeter Chubb } 1521ef03890SPeter Chubb return 0; 1531ef03890SPeter Chubb } 1541ef03890SPeter Chubb 155144d102bSMichael D Labriola /* 15665051397SH. Peter Anvin * This is a single dmi_table handling all reboot quirks. 15757b16594SMichael D Labriola */ 1586faadbbbSChristoph Hellwig static const struct dmi_system_id reboot_dmi_table[] __initconst = { 15957b16594SMichael D Labriola 160e56e57f6SDave Jones /* Acer */ 161b49c78d4SPeter Chubb { /* Handle reboot issue on Acer Aspire one */ 1621ef03890SPeter Chubb .callback = set_kbd_reboot, 163b49c78d4SPeter Chubb .ident = "Acer Aspire One A110", 164b49c78d4SPeter Chubb .matches = { 165b49c78d4SPeter Chubb DMI_MATCH(DMI_SYS_VENDOR, "Acer"), 166b49c78d4SPeter Chubb DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"), 167b49c78d4SPeter Chubb }, 168b49c78d4SPeter Chubb }, 169e56e57f6SDave Jones 170e56e57f6SDave Jones /* Apple */ 1713e03bbeaSShunichi Fuji { /* Handle problems with rebooting on Apple MacBook5 */ 1726c6c51e4SPaul Mackerras .callback = set_pci_reboot, 1733e03bbeaSShunichi Fuji .ident = "Apple MacBook5", 1746c6c51e4SPaul Mackerras .matches = { 1756c6c51e4SPaul Mackerras DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), 1763e03bbeaSShunichi Fuji DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"), 1776c6c51e4SPaul Mackerras }, 1786c6c51e4SPaul Mackerras }, 1793e03bbeaSShunichi Fuji { /* Handle problems with rebooting on Apple MacBookPro5 */ 180498cdbfbSOzan Çağlayan .callback = set_pci_reboot, 1813e03bbeaSShunichi Fuji .ident = "Apple MacBookPro5", 182498cdbfbSOzan Çağlayan .matches = { 183498cdbfbSOzan Çağlayan DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), 1843e03bbeaSShunichi Fuji DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"), 185498cdbfbSOzan Çağlayan }, 186498cdbfbSOzan Çağlayan }, 18705154752SGottfried Haider { /* Handle problems with rebooting on Apple Macmini3,1 */ 18805154752SGottfried Haider .callback = set_pci_reboot, 18905154752SGottfried Haider .ident = "Apple Macmini3,1", 19005154752SGottfried Haider .matches = { 19105154752SGottfried Haider DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), 19205154752SGottfried Haider DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"), 19305154752SGottfried Haider }, 19405154752SGottfried Haider }, 1950a832320SJustin P. Mattock { /* Handle problems with rebooting on the iMac9,1. */ 1960a832320SJustin P. Mattock .callback = set_pci_reboot, 1970a832320SJustin P. Mattock .ident = "Apple iMac9,1", 1980a832320SJustin P. Mattock .matches = { 1990a832320SJustin P. Mattock DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), 2000a832320SJustin P. Mattock DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"), 2010a832320SJustin P. Mattock }, 2020a832320SJustin P. Mattock }, 2032f0c0b2dSMario Kleiner { /* Handle problems with rebooting on the iMac10,1. */ 2042f0c0b2dSMario Kleiner .callback = set_pci_reboot, 2052f0c0b2dSMario Kleiner .ident = "Apple iMac10,1", 2062f0c0b2dSMario Kleiner .matches = { 2072f0c0b2dSMario Kleiner DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), 2082f0c0b2dSMario Kleiner DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"), 2092f0c0b2dSMario Kleiner }, 2102f0c0b2dSMario Kleiner }, 211e56e57f6SDave Jones 21280313b30SStefan Lippers-Hollmann /* ASRock */ 21380313b30SStefan Lippers-Hollmann { /* Handle problems with rebooting on ASRock Q1900DC-ITX */ 21480313b30SStefan Lippers-Hollmann .callback = set_pci_reboot, 21580313b30SStefan Lippers-Hollmann .ident = "ASRock Q1900DC-ITX", 21680313b30SStefan Lippers-Hollmann .matches = { 21780313b30SStefan Lippers-Hollmann DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"), 21880313b30SStefan Lippers-Hollmann DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"), 21980313b30SStefan Lippers-Hollmann }, 22080313b30SStefan Lippers-Hollmann }, 22180313b30SStefan Lippers-Hollmann 222e56e57f6SDave Jones /* ASUS */ 223e56e57f6SDave Jones { /* Handle problems with rebooting on ASUS P4S800 */ 224e56e57f6SDave Jones .callback = set_bios_reboot, 225e56e57f6SDave Jones .ident = "ASUS P4S800", 226e56e57f6SDave Jones .matches = { 227e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."), 228e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "P4S800"), 229e56e57f6SDave Jones }, 230e56e57f6SDave Jones }, 23190b28dedSMatjaz Hegedic { /* Handle problems with rebooting on ASUS EeeBook X205TA */ 23290b28dedSMatjaz Hegedic .callback = set_acpi_reboot, 23390b28dedSMatjaz Hegedic .ident = "ASUS EeeBook X205TA", 23490b28dedSMatjaz Hegedic .matches = { 23590b28dedSMatjaz Hegedic DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), 236bba8376aSMatjaz Hegedic DMI_MATCH(DMI_PRODUCT_NAME, "X205TA"), 23790b28dedSMatjaz Hegedic }, 23890b28dedSMatjaz Hegedic }, 2393b3e7855SMatjaz Hegedic { /* Handle problems with rebooting on ASUS EeeBook X205TAW */ 2403b3e7855SMatjaz Hegedic .callback = set_acpi_reboot, 2413b3e7855SMatjaz Hegedic .ident = "ASUS EeeBook X205TAW", 2423b3e7855SMatjaz Hegedic .matches = { 2433b3e7855SMatjaz Hegedic DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), 2443b3e7855SMatjaz Hegedic DMI_MATCH(DMI_PRODUCT_NAME, "X205TAW"), 2453b3e7855SMatjaz Hegedic }, 2463b3e7855SMatjaz Hegedic }, 247e56e57f6SDave Jones 248aadca6faSChristian Gmeiner /* Certec */ 249aadca6faSChristian Gmeiner { /* Handle problems with rebooting on Certec BPC600 */ 250aadca6faSChristian Gmeiner .callback = set_pci_reboot, 251aadca6faSChristian Gmeiner .ident = "Certec BPC600", 252aadca6faSChristian Gmeiner .matches = { 253aadca6faSChristian Gmeiner DMI_MATCH(DMI_SYS_VENDOR, "Certec"), 254aadca6faSChristian Gmeiner DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"), 255aadca6faSChristian Gmeiner }, 256aadca6faSChristian Gmeiner }, 257aadca6faSChristian Gmeiner 258e56e57f6SDave Jones /* Dell */ 259e56e57f6SDave Jones { /* Handle problems with rebooting on Dell DXP061 */ 260e56e57f6SDave Jones .callback = set_bios_reboot, 261e56e57f6SDave Jones .ident = "Dell DXP061", 2623628c3f5SMaxime Ripard .matches = { 2633628c3f5SMaxime Ripard DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 264e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"), 265e56e57f6SDave Jones }, 266e56e57f6SDave Jones }, 267e56e57f6SDave Jones { /* Handle problems with rebooting on Dell E520's */ 268e56e57f6SDave Jones .callback = set_bios_reboot, 269e56e57f6SDave Jones .ident = "Dell E520", 270e56e57f6SDave Jones .matches = { 271e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 272e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"), 2733628c3f5SMaxime Ripard }, 2743628c3f5SMaxime Ripard }, 2758412da75SVille Syrjälä { /* Handle problems with rebooting on the Latitude E5410. */ 2768412da75SVille Syrjälä .callback = set_pci_reboot, 2778412da75SVille Syrjälä .ident = "Dell Latitude E5410", 2788412da75SVille Syrjälä .matches = { 2798412da75SVille Syrjälä DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 2808412da75SVille Syrjälä DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"), 2818412da75SVille Syrjälä }, 2828412da75SVille Syrjälä }, 283b7798d28SDaniel J Blueman { /* Handle problems with rebooting on the Latitude E5420. */ 284b7798d28SDaniel J Blueman .callback = set_pci_reboot, 285b7798d28SDaniel J Blueman .ident = "Dell Latitude E5420", 286b7798d28SDaniel J Blueman .matches = { 287b7798d28SDaniel J Blueman DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 288b7798d28SDaniel J Blueman DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"), 289b7798d28SDaniel J Blueman }, 290b7798d28SDaniel J Blueman }, 291e56e57f6SDave Jones { /* Handle problems with rebooting on the Latitude E6320. */ 292e56e57f6SDave Jones .callback = set_pci_reboot, 293e56e57f6SDave Jones .ident = "Dell Latitude E6320", 294e56e57f6SDave Jones .matches = { 295e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 296e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"), 297e56e57f6SDave Jones }, 298e56e57f6SDave Jones }, 299a536877eSH. Peter Anvin { /* Handle problems with rebooting on the Latitude E6420. */ 300a536877eSH. Peter Anvin .callback = set_pci_reboot, 301a536877eSH. Peter Anvin .ident = "Dell Latitude E6420", 302a536877eSH. Peter Anvin .matches = { 303a536877eSH. Peter Anvin DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 304a536877eSH. Peter Anvin DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"), 305a536877eSH. Peter Anvin }, 306a536877eSH. Peter Anvin }, 307e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */ 308e56e57f6SDave Jones .callback = set_bios_reboot, 309e56e57f6SDave Jones .ident = "Dell OptiPlex 330", 310e56e57f6SDave Jones .matches = { 311e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 312e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"), 313e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0KP561"), 314e56e57f6SDave Jones }, 315e56e57f6SDave Jones }, 316e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */ 317e56e57f6SDave Jones .callback = set_bios_reboot, 318e56e57f6SDave Jones .ident = "Dell OptiPlex 360", 319e56e57f6SDave Jones .matches = { 320e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 321e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"), 322e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0T656F"), 323e56e57f6SDave Jones }, 324e56e57f6SDave Jones }, 325e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 745's SFF */ 326e56e57f6SDave Jones .callback = set_bios_reboot, 327e56e57f6SDave Jones .ident = "Dell OptiPlex 745", 328e56e57f6SDave Jones .matches = { 329e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 330e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"), 331e56e57f6SDave Jones }, 332e56e57f6SDave Jones }, 333e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 745's DFF */ 334e56e57f6SDave Jones .callback = set_bios_reboot, 335e56e57f6SDave Jones .ident = "Dell OptiPlex 745", 336e56e57f6SDave Jones .matches = { 337e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 338e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"), 339e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0MM599"), 340e56e57f6SDave Jones }, 341e56e57f6SDave Jones }, 342e56e57f6SDave Jones { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */ 343e56e57f6SDave Jones .callback = set_bios_reboot, 344e56e57f6SDave Jones .ident = "Dell OptiPlex 745", 345e56e57f6SDave Jones .matches = { 346e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 347e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"), 348e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0KW626"), 349e56e57f6SDave Jones }, 350e56e57f6SDave Jones }, 351e56e57f6SDave Jones { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */ 352e56e57f6SDave Jones .callback = set_bios_reboot, 353e56e57f6SDave Jones .ident = "Dell OptiPlex 760", 354e56e57f6SDave Jones .matches = { 355e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 356e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"), 357e56e57f6SDave Jones DMI_MATCH(DMI_BOARD_NAME, "0G919G"), 358e56e57f6SDave Jones }, 359e56e57f6SDave Jones }, 3606be30bb7SRafael J. Wysocki { /* Handle problems with rebooting on the OptiPlex 990. */ 3616be30bb7SRafael J. Wysocki .callback = set_pci_reboot, 3626be30bb7SRafael J. Wysocki .ident = "Dell OptiPlex 990", 3636be30bb7SRafael J. Wysocki .matches = { 3646be30bb7SRafael J. Wysocki DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 3656be30bb7SRafael J. Wysocki DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"), 3666be30bb7SRafael J. Wysocki }, 3676be30bb7SRafael J. Wysocki }, 368e56e57f6SDave Jones { /* Handle problems with rebooting on Dell 300's */ 369e56e57f6SDave Jones .callback = set_bios_reboot, 370e56e57f6SDave Jones .ident = "Dell PowerEdge 300", 37176eb9a30SZhang Rui .matches = { 372e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"), 373e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"), 374e56e57f6SDave Jones }, 375e56e57f6SDave Jones }, 376e56e57f6SDave Jones { /* Handle problems with rebooting on Dell 1300's */ 377e56e57f6SDave Jones .callback = set_bios_reboot, 378e56e57f6SDave Jones .ident = "Dell PowerEdge 1300", 379e56e57f6SDave Jones .matches = { 380e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"), 381e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"), 382e56e57f6SDave Jones }, 383e56e57f6SDave Jones }, 384e56e57f6SDave Jones { /* Handle problems with rebooting on Dell 2400's */ 385e56e57f6SDave Jones .callback = set_bios_reboot, 386e56e57f6SDave Jones .ident = "Dell PowerEdge 2400", 387e56e57f6SDave Jones .matches = { 388e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"), 389e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"), 39076eb9a30SZhang Rui }, 39176eb9a30SZhang Rui }, 3924f0acd31SMasoud Sharbiani { /* Handle problems with rebooting on the Dell PowerEdge C6100. */ 3934f0acd31SMasoud Sharbiani .callback = set_pci_reboot, 3944f0acd31SMasoud Sharbiani .ident = "Dell PowerEdge C6100", 3954f0acd31SMasoud Sharbiani .matches = { 3964f0acd31SMasoud Sharbiani DMI_MATCH(DMI_SYS_VENDOR, "Dell"), 3974f0acd31SMasoud Sharbiani DMI_MATCH(DMI_PRODUCT_NAME, "C6100"), 3984f0acd31SMasoud Sharbiani }, 3994f0acd31SMasoud Sharbiani }, 4006c6c51e4SPaul Mackerras { /* Handle problems with rebooting on the Precision M6600. */ 4016c6c51e4SPaul Mackerras .callback = set_pci_reboot, 4026c6c51e4SPaul Mackerras .ident = "Dell Precision M6600", 4036c6c51e4SPaul Mackerras .matches = { 4046c6c51e4SPaul Mackerras DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 4056c6c51e4SPaul Mackerras DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"), 4066c6c51e4SPaul Mackerras }, 4076c6c51e4SPaul Mackerras }, 408e56e57f6SDave Jones { /* Handle problems with rebooting on Dell T5400's */ 409e56e57f6SDave Jones .callback = set_bios_reboot, 410e56e57f6SDave Jones .ident = "Dell Precision T5400", 41157b16594SMichael D Labriola .matches = { 4126c6c51e4SPaul Mackerras DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 413e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"), 414144d102bSMichael D Labriola }, 4155955633eSMichael D Labriola }, 416e56e57f6SDave Jones { /* Handle problems with rebooting on Dell T7400's */ 417e56e57f6SDave Jones .callback = set_bios_reboot, 418e56e57f6SDave Jones .ident = "Dell Precision T7400", 4196c6c51e4SPaul Mackerras .matches = { 420e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 421e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"), 4226c6c51e4SPaul Mackerras }, 4234d022e35SMiguel Boton }, 424e56e57f6SDave Jones { /* Handle problems with rebooting on Dell XPS710 */ 425e56e57f6SDave Jones .callback = set_bios_reboot, 426e56e57f6SDave Jones .ident = "Dell XPS710", 427e56e57f6SDave Jones .matches = { 428e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 429e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"), 430e56e57f6SDave Jones }, 431e56e57f6SDave Jones }, 4324d581259SAlex Hung { /* Handle problems with rebooting on Dell Optiplex 7450 AIO */ 4334d581259SAlex Hung .callback = set_acpi_reboot, 4344d581259SAlex Hung .ident = "Dell OptiPlex 7450 AIO", 4354d581259SAlex Hung .matches = { 4364d581259SAlex Hung DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), 4374d581259SAlex Hung DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7450 AIO"), 4384d581259SAlex Hung }, 4394d581259SAlex Hung }, 440e56e57f6SDave Jones 441e56e57f6SDave Jones /* Hewlett-Packard */ 442e56e57f6SDave Jones { /* Handle problems with rebooting on HP laptops */ 443e56e57f6SDave Jones .callback = set_bios_reboot, 444e56e57f6SDave Jones .ident = "HP Compaq Laptop", 445e56e57f6SDave Jones .matches = { 446e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 447e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"), 448e56e57f6SDave Jones }, 449e56e57f6SDave Jones }, 450e56e57f6SDave Jones 451e56e57f6SDave Jones /* Sony */ 452e56e57f6SDave Jones { /* Handle problems with rebooting on Sony VGN-Z540N */ 453e56e57f6SDave Jones .callback = set_bios_reboot, 454e56e57f6SDave Jones .ident = "Sony VGN-Z540N", 455e56e57f6SDave Jones .matches = { 456e56e57f6SDave Jones DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"), 457e56e57f6SDave Jones DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"), 458e56e57f6SDave Jones }, 459e56e57f6SDave Jones }, 460e56e57f6SDave Jones 4614d022e35SMiguel Boton { } 4624d022e35SMiguel Boton }; 4639c48f1c6SDon Zickus 464d176720dSEduardo Habkost static int __init reboot_init(void) 465d176720dSEduardo Habkost { 46644be28e9SMatt Fleming int rv; 46744be28e9SMatt Fleming 468d176720dSEduardo Habkost /* 469d176720dSEduardo Habkost * Only do the DMI check if reboot_type hasn't been overridden 470d176720dSEduardo Habkost * on the command line 471d176720dSEduardo Habkost */ 47244be28e9SMatt Fleming if (!reboot_default) 47344be28e9SMatt Fleming return 0; 47444be28e9SMatt Fleming 47544be28e9SMatt Fleming /* 47644be28e9SMatt Fleming * The DMI quirks table takes precedence. If no quirks entry 4774ecf7191SStefan Assmann * matches and the ACPI Hardware Reduced bit is set and EFI 4784ecf7191SStefan Assmann * runtime services are enabled, force EFI reboot. 47944be28e9SMatt Fleming */ 48044be28e9SMatt Fleming rv = dmi_check_system(reboot_dmi_table); 48144be28e9SMatt Fleming 4824ecf7191SStefan Assmann if (!rv && efi_reboot_required() && !efi_runtime_disabled()) 48344be28e9SMatt Fleming reboot_type = BOOT_EFI; 48444be28e9SMatt Fleming 485d176720dSEduardo Habkost return 0; 486d176720dSEduardo Habkost } 487d176720dSEduardo Habkost core_initcall(reboot_init); 488d176720dSEduardo Habkost 489d176720dSEduardo Habkost static inline void kb_wait(void) 490d176720dSEduardo Habkost { 491d176720dSEduardo Habkost int i; 492d176720dSEduardo Habkost 493d176720dSEduardo Habkost for (i = 0; i < 0x10000; i++) { 4947432d149SIngo Molnar if ((inb(0x64) & 0x02) == 0) 4957432d149SIngo Molnar break; 4967432d149SIngo Molnar udelay(2); 4977432d149SIngo Molnar } 498416e2d63SJody Belka } 4994d022e35SMiguel Boton 5004d022e35SMiguel Boton static void vmxoff_nmi(int cpu, struct pt_regs *regs) 5014d022e35SMiguel Boton { 502d176720dSEduardo Habkost cpu_emergency_vmxoff(); 503d176720dSEduardo Habkost } 504d176720dSEduardo Habkost 505144d102bSMichael D Labriola /* Use NMIs as IPIs to tell all CPUs to disable virtualization */ 5064d022e35SMiguel Boton static void emergency_vmx_disable_all(void) 5074d022e35SMiguel Boton { 5084d022e35SMiguel Boton /* Just make sure we won't change CPUs while doing this */ 5094d022e35SMiguel Boton local_irq_disable(); 5104d022e35SMiguel Boton 511144d102bSMichael D Labriola /* 512144d102bSMichael D Labriola * We need to disable VMX on all CPUs before rebooting, otherwise 5134d022e35SMiguel Boton * we risk hanging up the machine, because the CPU ignore INIT 5144d022e35SMiguel Boton * signals when VMX is enabled. 5154d022e35SMiguel Boton * 5164d022e35SMiguel Boton * We can't take any locks and we may be on an inconsistent 5174d022e35SMiguel Boton * state, so we use NMIs as IPIs to tell the other CPUs to disable 5184d022e35SMiguel Boton * VMX and halt. 5194d022e35SMiguel Boton * 5204d022e35SMiguel Boton * For safety, we will avoid running the nmi_shootdown_cpus() 5214d022e35SMiguel Boton * stuff unnecessarily, but we don't have a way to check 5224d022e35SMiguel Boton * if other CPUs have VMX enabled. So we will call it only if the 5234d022e35SMiguel Boton * CPU we are running on has VMX enabled. 5244d022e35SMiguel Boton * 5254d022e35SMiguel Boton * We will miss cases where VMX is not enabled on all CPUs. This 5264d022e35SMiguel Boton * shouldn't do much harm because KVM always enable VMX on all 5274d022e35SMiguel Boton * CPUs anyway. But we can miss it on the small window where KVM 5284d022e35SMiguel Boton * is still enabling VMX. 5294d022e35SMiguel Boton */ 5304d022e35SMiguel Boton if (cpu_has_vmx() && cpu_vmx_enabled()) { 531144d102bSMichael D Labriola /* Disable VMX on this CPU. */ 5324d022e35SMiguel Boton cpu_vmxoff(); 5334d022e35SMiguel Boton 5344d022e35SMiguel Boton /* Halt and disable VMX on the other CPUs */ 5354d022e35SMiguel Boton nmi_shootdown_cpus(vmxoff_nmi); 5364d022e35SMiguel Boton 5374d022e35SMiguel Boton } 5384d022e35SMiguel Boton } 5394d022e35SMiguel Boton 5404d022e35SMiguel Boton 5414d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void) 5424d022e35SMiguel Boton { 5434d022e35SMiguel Boton } 5444d022e35SMiguel Boton 545660e34ceSMatthew Garrett /* 5465be44a6fSIngo Molnar * To the best of our knowledge Windows compatible x86 hardware expects 5475be44a6fSIngo Molnar * the following on reboot: 548660e34ceSMatthew Garrett * 549660e34ceSMatthew Garrett * 1) If the FADT has the ACPI reboot register flag set, try it 550660e34ceSMatthew Garrett * 2) If still alive, write to the keyboard controller 551660e34ceSMatthew Garrett * 3) If still alive, write to the ACPI reboot register again 552660e34ceSMatthew Garrett * 4) If still alive, write to the keyboard controller again 553a4f1987eSLi, Aubrey * 5) If still alive, call the EFI runtime service to reboot 5545be44a6fSIngo Molnar * 6) If no EFI runtime service, call the BIOS to do a reboot 555660e34ceSMatthew Garrett * 5565be44a6fSIngo Molnar * We default to following the same pattern. We also have 5575be44a6fSIngo Molnar * two other reboot methods: 'triple fault' and 'PCI', which 5585be44a6fSIngo Molnar * can be triggered via the reboot= kernel boot option or 5595be44a6fSIngo Molnar * via quirks. 5605be44a6fSIngo Molnar * 5615be44a6fSIngo Molnar * This means that this function can never return, it can misbehave 5625be44a6fSIngo Molnar * by not rebooting properly and hanging. 563660e34ceSMatthew Garrett */ 5644d022e35SMiguel Boton static void native_machine_emergency_restart(void) 5654d022e35SMiguel Boton { 5664d022e35SMiguel Boton int i; 567660e34ceSMatthew Garrett int attempt = 0; 568660e34ceSMatthew Garrett int orig_reboot_type = reboot_type; 569edf2b139SRobin Holt unsigned short mode; 5704d022e35SMiguel Boton 5714d022e35SMiguel Boton if (reboot_emergency) 5724d022e35SMiguel Boton emergency_vmx_disable_all(); 5734d022e35SMiguel Boton 574840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_REBOOT); 575840c2bafSJoseph Cihula 5764d022e35SMiguel Boton /* Tell the BIOS if we want cold or warm reboot */ 577edf2b139SRobin Holt mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0; 578edf2b139SRobin Holt *((unsigned short *)__va(0x472)) = mode; 5794d022e35SMiguel Boton 58087615a34SMatt Fleming /* 58187615a34SMatt Fleming * If an EFI capsule has been registered with the firmware then 58287615a34SMatt Fleming * override the reboot= parameter. 58387615a34SMatt Fleming */ 58487615a34SMatt Fleming if (efi_capsule_pending(NULL)) { 58587615a34SMatt Fleming pr_info("EFI capsule is pending, forcing EFI reboot.\n"); 58687615a34SMatt Fleming reboot_type = BOOT_EFI; 58787615a34SMatt Fleming } 58887615a34SMatt Fleming 5894d022e35SMiguel Boton for (;;) { 5904d022e35SMiguel Boton /* Could also try the reset bit in the Hammer NB */ 5914d022e35SMiguel Boton switch (reboot_type) { 5925be44a6fSIngo Molnar case BOOT_ACPI: 5935be44a6fSIngo Molnar acpi_reboot(); 5945be44a6fSIngo Molnar reboot_type = BOOT_KBD; 5955be44a6fSIngo Molnar break; 5965be44a6fSIngo Molnar 5974d022e35SMiguel Boton case BOOT_KBD: 598144d102bSMichael D Labriola mach_reboot_fixups(); /* For board specific fixups */ 5997432d149SIngo Molnar 6004d022e35SMiguel Boton for (i = 0; i < 10; i++) { 6014d022e35SMiguel Boton kb_wait(); 6024d022e35SMiguel Boton udelay(50); 603144d102bSMichael D Labriola outb(0xfe, 0x64); /* Pulse reset low */ 6044d022e35SMiguel Boton udelay(50); 6054d022e35SMiguel Boton } 606660e34ceSMatthew Garrett if (attempt == 0 && orig_reboot_type == BOOT_ACPI) { 607660e34ceSMatthew Garrett attempt = 1; 608660e34ceSMatthew Garrett reboot_type = BOOT_ACPI; 609660e34ceSMatthew Garrett } else { 610a4f1987eSLi, Aubrey reboot_type = BOOT_EFI; 611660e34ceSMatthew Garrett } 612660e34ceSMatthew Garrett break; 6134d022e35SMiguel Boton 6144d022e35SMiguel Boton case BOOT_EFI: 6158562c99cSMatt Fleming efi_reboot(reboot_mode, NULL); 6165be44a6fSIngo Molnar reboot_type = BOOT_BIOS; 61714d7ca5cSH. Peter Anvin break; 6184d022e35SMiguel Boton 6195be44a6fSIngo Molnar case BOOT_BIOS: 6205be44a6fSIngo Molnar machine_real_restart(MRR_BIOS); 6215be44a6fSIngo Molnar 6225be44a6fSIngo Molnar /* We're probably dead after this, but... */ 6235be44a6fSIngo Molnar reboot_type = BOOT_CF9_SAFE; 6245be44a6fSIngo Molnar break; 6255be44a6fSIngo Molnar 6265be44a6fSIngo Molnar case BOOT_CF9_FORCE: 62714d7ca5cSH. Peter Anvin port_cf9_safe = true; 628144d102bSMichael D Labriola /* Fall through */ 62914d7ca5cSH. Peter Anvin 6305be44a6fSIngo Molnar case BOOT_CF9_SAFE: 63114d7ca5cSH. Peter Anvin if (port_cf9_safe) { 6325be44a6fSIngo Molnar u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E; 63316c21ae5SLi Fei u8 cf9 = inb(0xcf9) & ~reboot_code; 63414d7ca5cSH. Peter Anvin outb(cf9|2, 0xcf9); /* Request hard reset */ 63514d7ca5cSH. Peter Anvin udelay(50); 63616c21ae5SLi Fei /* Actually do the reset */ 63716c21ae5SLi Fei outb(cf9|reboot_code, 0xcf9); 63814d7ca5cSH. Peter Anvin udelay(50); 63914d7ca5cSH. Peter Anvin } 6405be44a6fSIngo Molnar reboot_type = BOOT_TRIPLE; 6415be44a6fSIngo Molnar break; 6425be44a6fSIngo Molnar 6435be44a6fSIngo Molnar case BOOT_TRIPLE: 644e802a51eSThomas Gleixner idt_invalidate(NULL); 6455be44a6fSIngo Molnar __asm__ __volatile__("int3"); 6465be44a6fSIngo Molnar 6475be44a6fSIngo Molnar /* We're probably dead after this, but... */ 6485be44a6fSIngo Molnar reboot_type = BOOT_KBD; 6494d022e35SMiguel Boton break; 6504d022e35SMiguel Boton } 6514d022e35SMiguel Boton } 6524d022e35SMiguel Boton } 6534d022e35SMiguel Boton 6543c62c625SGlauber Costa void native_machine_shutdown(void) 6554d022e35SMiguel Boton { 6564d022e35SMiguel Boton /* Stop the cpus and apics */ 657522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC 6582885432aSFenghua Yu /* 6592885432aSFenghua Yu * Disabling IO APIC before local APIC is a workaround for 6602885432aSFenghua Yu * erratum AVR31 in "Intel Atom Processor C2000 Product Family 6612885432aSFenghua Yu * Specification Update". In this situation, interrupts that target 6622885432aSFenghua Yu * a Logical Processor whose Local APIC is either in the process of 6632885432aSFenghua Yu * being hardware disabled or software disabled are neither delivered 6642885432aSFenghua Yu * nor discarded. When this erratum occurs, the processor may hang. 6652885432aSFenghua Yu * 6662885432aSFenghua Yu * Even without the erratum, it still makes sense to quiet IO APIC 6672885432aSFenghua Yu * before disabling Local APIC. 6682885432aSFenghua Yu */ 669*339b2ae0SBaoquan He clear_IO_APIC(); 670522e6646SFenghua Yu #endif 671522e6646SFenghua Yu 6724d022e35SMiguel Boton #ifdef CONFIG_SMP 673144d102bSMichael D Labriola /* 6741b3a5d02SRobin Holt * Stop all of the others. Also disable the local irq to 6751b3a5d02SRobin Holt * not receive the per-cpu timer interrupt which may trigger 6761b3a5d02SRobin Holt * scheduler's load balance. 6774d022e35SMiguel Boton */ 67855c844a4SFeng Tang local_irq_disable(); 67976fac077SAlok Kataria stop_other_cpus(); 6804d022e35SMiguel Boton #endif 6814d022e35SMiguel Boton 6824d022e35SMiguel Boton lapic_shutdown(); 683*339b2ae0SBaoquan He restore_boot_irq_mode(); 6844d022e35SMiguel Boton 6854d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER 6864d022e35SMiguel Boton hpet_disable(); 6874d022e35SMiguel Boton #endif 6884d022e35SMiguel Boton 6894d022e35SMiguel Boton #ifdef CONFIG_X86_64 690338bac52SFUJITA Tomonori x86_platform.iommu_shutdown(); 6914d022e35SMiguel Boton #endif 6924d022e35SMiguel Boton } 6934d022e35SMiguel Boton 694d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency) 695d176720dSEduardo Habkost { 696d176720dSEduardo Habkost reboot_emergency = emergency; 697d176720dSEduardo Habkost machine_ops.emergency_restart(); 698d176720dSEduardo Habkost } 699d176720dSEduardo Habkost 700416e2d63SJody Belka static void native_machine_restart(char *__unused) 7014d022e35SMiguel Boton { 702c767a54bSJoe Perches pr_notice("machine restart\n"); 7034d022e35SMiguel Boton 7044d022e35SMiguel Boton if (!reboot_force) 7054d022e35SMiguel Boton machine_shutdown(); 706d176720dSEduardo Habkost __machine_emergency_restart(0); 7074d022e35SMiguel Boton } 7084d022e35SMiguel Boton 709416e2d63SJody Belka static void native_machine_halt(void) 7104d022e35SMiguel Boton { 711144d102bSMichael D Labriola /* Stop other cpus and apics */ 712d3ec5caeSIvan Vecera machine_shutdown(); 713d3ec5caeSIvan Vecera 714840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_HALT); 715840c2bafSJoseph Cihula 716d3ec5caeSIvan Vecera stop_this_cpu(NULL); 7174d022e35SMiguel Boton } 7184d022e35SMiguel Boton 719416e2d63SJody Belka static void native_machine_power_off(void) 7204d022e35SMiguel Boton { 7214d022e35SMiguel Boton if (pm_power_off) { 7224d022e35SMiguel Boton if (!reboot_force) 7234d022e35SMiguel Boton machine_shutdown(); 7244d022e35SMiguel Boton pm_power_off(); 7254d022e35SMiguel Boton } 726144d102bSMichael D Labriola /* A fallback in case there is no PM info available */ 727840c2bafSJoseph Cihula tboot_shutdown(TB_SHUTDOWN_HALT); 7284d022e35SMiguel Boton } 7294d022e35SMiguel Boton 730404f6aacSKees Cook struct machine_ops machine_ops __ro_after_init = { 731416e2d63SJody Belka .power_off = native_machine_power_off, 732416e2d63SJody Belka .shutdown = native_machine_shutdown, 733416e2d63SJody Belka .emergency_restart = native_machine_emergency_restart, 734416e2d63SJody Belka .restart = native_machine_restart, 735ed23dc6fSGlauber Costa .halt = native_machine_halt, 7362965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 737ed23dc6fSGlauber Costa .crash_shutdown = native_machine_crash_shutdown, 738ed23dc6fSGlauber Costa #endif 7394d022e35SMiguel Boton }; 740416e2d63SJody Belka 741416e2d63SJody Belka void machine_power_off(void) 742416e2d63SJody Belka { 743416e2d63SJody Belka machine_ops.power_off(); 744416e2d63SJody Belka } 745416e2d63SJody Belka 746416e2d63SJody Belka void machine_shutdown(void) 747416e2d63SJody Belka { 748416e2d63SJody Belka machine_ops.shutdown(); 749416e2d63SJody Belka } 750416e2d63SJody Belka 751416e2d63SJody Belka void machine_emergency_restart(void) 752416e2d63SJody Belka { 753d176720dSEduardo Habkost __machine_emergency_restart(1); 754416e2d63SJody Belka } 755416e2d63SJody Belka 756416e2d63SJody Belka void machine_restart(char *cmd) 757416e2d63SJody Belka { 758416e2d63SJody Belka machine_ops.restart(cmd); 759416e2d63SJody Belka } 760416e2d63SJody Belka 761416e2d63SJody Belka void machine_halt(void) 762416e2d63SJody Belka { 763416e2d63SJody Belka machine_ops.halt(); 764416e2d63SJody Belka } 765416e2d63SJody Belka 7662965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 767ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs) 768ed23dc6fSGlauber Costa { 769ed23dc6fSGlauber Costa machine_ops.crash_shutdown(regs); 770ed23dc6fSGlauber Costa } 771ed23dc6fSGlauber Costa #endif 7722ddded21SEduardo Habkost 7732ddded21SEduardo Habkost 7745bc32950SXunlei Pang /* This is the CPU performing the emergency shutdown work. */ 7755bc32950SXunlei Pang int crashing_cpu = -1; 7765bc32950SXunlei Pang 777bb8dd270SEduardo Habkost #if defined(CONFIG_SMP) 7782ddded21SEduardo Habkost 7792ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback; 7802ddded21SEduardo Habkost 7812ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi; 78258c5661fSHidehiro Kawai static int crash_ipi_issued; 7832ddded21SEduardo Habkost 7849c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs) 7852ddded21SEduardo Habkost { 7862ddded21SEduardo Habkost int cpu; 7872ddded21SEduardo Habkost 7882ddded21SEduardo Habkost cpu = raw_smp_processor_id(); 7892ddded21SEduardo Habkost 790144d102bSMichael D Labriola /* 791144d102bSMichael D Labriola * Don't do anything if this handler is invoked on crashing cpu. 7922ddded21SEduardo Habkost * Otherwise, system will completely hang. Crashing cpu can get 7932ddded21SEduardo Habkost * an NMI if system was initially booted with nmi_watchdog parameter. 7942ddded21SEduardo Habkost */ 7952ddded21SEduardo Habkost if (cpu == crashing_cpu) 7969c48f1c6SDon Zickus return NMI_HANDLED; 7972ddded21SEduardo Habkost local_irq_disable(); 7982ddded21SEduardo Habkost 7999c48f1c6SDon Zickus shootdown_callback(cpu, regs); 8002ddded21SEduardo Habkost 8012ddded21SEduardo Habkost atomic_dec(&waiting_for_crash_ipi); 8022ddded21SEduardo Habkost /* Assume hlt works */ 8032ddded21SEduardo Habkost halt(); 8042ddded21SEduardo Habkost for (;;) 8052ddded21SEduardo Habkost cpu_relax(); 8062ddded21SEduardo Habkost 8079c48f1c6SDon Zickus return NMI_HANDLED; 8082ddded21SEduardo Habkost } 8092ddded21SEduardo Habkost 8102ddded21SEduardo Habkost static void smp_send_nmi_allbutself(void) 8112ddded21SEduardo Habkost { 812dac5f412SIngo Molnar apic->send_IPI_allbutself(NMI_VECTOR); 8132ddded21SEduardo Habkost } 8142ddded21SEduardo Habkost 815144d102bSMichael D Labriola /* 816144d102bSMichael D Labriola * Halt all other CPUs, calling the specified function on each of them 817bb8dd270SEduardo Habkost * 818bb8dd270SEduardo Habkost * This function can be used to halt all other CPUs on crash 819bb8dd270SEduardo Habkost * or emergency reboot time. The function passed as parameter 820bb8dd270SEduardo Habkost * will be called inside a NMI handler on all CPUs. 821bb8dd270SEduardo Habkost */ 8222ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback) 8232ddded21SEduardo Habkost { 8242ddded21SEduardo Habkost unsigned long msecs; 825c415b3dcSEduardo Habkost local_irq_disable(); 8262ddded21SEduardo Habkost 8272ddded21SEduardo Habkost /* Make a note of crashing cpu. Will be used in NMI callback. */ 8282ddded21SEduardo Habkost crashing_cpu = safe_smp_processor_id(); 8292ddded21SEduardo Habkost 8302ddded21SEduardo Habkost shootdown_callback = callback; 8312ddded21SEduardo Habkost 8322ddded21SEduardo Habkost atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1); 8332ddded21SEduardo Habkost /* Would it be better to replace the trap vector here? */ 8349c48f1c6SDon Zickus if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback, 8359c48f1c6SDon Zickus NMI_FLAG_FIRST, "crash")) 836144d102bSMichael D Labriola return; /* Return what? */ 837144d102bSMichael D Labriola /* 838144d102bSMichael D Labriola * Ensure the new callback function is set before sending 8392ddded21SEduardo Habkost * out the NMI 8402ddded21SEduardo Habkost */ 8412ddded21SEduardo Habkost wmb(); 8422ddded21SEduardo Habkost 8432ddded21SEduardo Habkost smp_send_nmi_allbutself(); 8442ddded21SEduardo Habkost 84558c5661fSHidehiro Kawai /* Kick CPUs looping in NMI context. */ 84658c5661fSHidehiro Kawai WRITE_ONCE(crash_ipi_issued, 1); 84758c5661fSHidehiro Kawai 8482ddded21SEduardo Habkost msecs = 1000; /* Wait at most a second for the other cpus to stop */ 8492ddded21SEduardo Habkost while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) { 8502ddded21SEduardo Habkost mdelay(1); 8512ddded21SEduardo Habkost msecs--; 8522ddded21SEduardo Habkost } 8532ddded21SEduardo Habkost 8542ddded21SEduardo Habkost /* Leave the nmi callback set */ 8552ddded21SEduardo Habkost } 85658c5661fSHidehiro Kawai 857b279d67dSHidehiro Kawai /* 858b279d67dSHidehiro Kawai * Check if the crash dumping IPI got issued and if so, call its callback 859b279d67dSHidehiro Kawai * directly. This function is used when we have already been in NMI handler. 860b279d67dSHidehiro Kawai * It doesn't return. 861b279d67dSHidehiro Kawai */ 862b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs) 863b279d67dSHidehiro Kawai { 864b279d67dSHidehiro Kawai if (crash_ipi_issued) 865b279d67dSHidehiro Kawai crash_nmi_callback(0, regs); 866b279d67dSHidehiro Kawai } 867b279d67dSHidehiro Kawai 86858c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */ 86958c5661fSHidehiro Kawai void nmi_panic_self_stop(struct pt_regs *regs) 87058c5661fSHidehiro Kawai { 87158c5661fSHidehiro Kawai while (1) { 872b279d67dSHidehiro Kawai /* If no CPU is preparing crash dump, we simply loop here. */ 873b279d67dSHidehiro Kawai run_crash_ipi_callback(regs); 87458c5661fSHidehiro Kawai cpu_relax(); 87558c5661fSHidehiro Kawai } 87658c5661fSHidehiro Kawai } 87758c5661fSHidehiro Kawai 878bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */ 879bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback) 880bb8dd270SEduardo Habkost { 881bb8dd270SEduardo Habkost /* No other CPUs to shoot down */ 882bb8dd270SEduardo Habkost } 883b279d67dSHidehiro Kawai 884b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs) 885b279d67dSHidehiro Kawai { 886b279d67dSHidehiro Kawai } 8872ddded21SEduardo Habkost #endif 888