linux/arch/x86/kernel/reboot.c
<<
>>
Prefs
   1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   2
   3#include <linux/module.h>
   4#include <linux/reboot.h>
   5#include <linux/init.h>
   6#include <linux/pm.h>
   7#include <linux/efi.h>
   8#include <linux/dmi.h>
   9#include <linux/sched.h>
  10#include <linux/tboot.h>
  11#include <linux/delay.h>
  12#include <acpi/reboot.h>
  13#include <asm/io.h>
  14#include <asm/apic.h>
  15#include <asm/desc.h>
  16#include <asm/hpet.h>
  17#include <asm/pgtable.h>
  18#include <asm/proto.h>
  19#include <asm/reboot_fixups.h>
  20#include <asm/reboot.h>
  21#include <asm/pci_x86.h>
  22#include <asm/virtext.h>
  23#include <asm/cpu.h>
  24#include <asm/nmi.h>
  25#include <asm/smp.h>
  26
  27#include <linux/ctype.h>
  28#include <linux/mc146818rtc.h>
  29#include <asm/realmode.h>
  30#include <asm/x86_init.h>
  31
  32/*
  33 * Power off function, if any
  34 */
  35void (*pm_power_off)(void);
  36EXPORT_SYMBOL(pm_power_off);
  37
  38static const struct desc_ptr no_idt = {};
  39static int reboot_mode;
  40enum reboot_type reboot_type = BOOT_ACPI;
  41int reboot_force;
  42
  43/*
  44 * This variable is used privately to keep track of whether or not
  45 * reboot_type is still set to its default value (i.e., reboot= hasn't
  46 * been set on the command line).  This is needed so that we can
  47 * suppress DMI scanning for reboot quirks.  Without it, it's
  48 * impossible to override a faulty reboot quirk without recompiling.
  49 */
  50static int reboot_default = 1;
  51
  52#ifdef CONFIG_SMP
  53static int reboot_cpu = -1;
  54#endif
  55
  56/*
  57 * This is set if we need to go through the 'emergency' path.
  58 * When machine_emergency_restart() is called, we may be on
  59 * an inconsistent state and won't be able to do a clean cleanup
  60 */
  61static int reboot_emergency;
  62
  63/* This is set by the PCI code if either type 1 or type 2 PCI is detected */
  64bool port_cf9_safe = false;
  65
  66/*
  67 * reboot=b[ios] | s[mp] | t[riple] | k[bd] | e[fi] [, [w]arm | [c]old] | p[ci]
  68 * warm   Don't set the cold reboot flag
  69 * cold   Set the cold reboot flag
  70 * bios   Reboot by jumping through the BIOS
  71 * smp    Reboot by executing reset on BSP or other CPU
  72 * triple Force a triple fault (init)
  73 * kbd    Use the keyboard controller. cold reset (default)
  74 * acpi   Use the RESET_REG in the FADT
  75 * efi    Use efi reset_system runtime service
  76 * pci    Use the so-called "PCI reset register", CF9
  77 * force  Avoid anything that could hang.
  78 */
  79static int __init reboot_setup(char *str)
  80{
  81        for (;;) {
  82                /*
  83                 * Having anything passed on the command line via
  84                 * reboot= will cause us to disable DMI checking
  85                 * below.
  86                 */
  87                reboot_default = 0;
  88
  89                switch (*str) {
  90                case 'w':
  91                        reboot_mode = 0x1234;
  92                        break;
  93
  94                case 'c':
  95                        reboot_mode = 0;
  96                        break;
  97
  98#ifdef CONFIG_SMP
  99                case 's':
 100                        if (isdigit(*(str+1))) {
 101                                reboot_cpu = (int) (*(str+1) - '0');
 102                                if (isdigit(*(str+2)))
 103                                        reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
 104                        }
 105                        /*
 106                         * We will leave sorting out the final value
 107                         * when we are ready to reboot, since we might not
 108                         * have detected BSP APIC ID or smp_num_cpu
 109                         */
 110                        break;
 111#endif /* CONFIG_SMP */
 112
 113                case 'b':
 114                case 'a':
 115                case 'k':
 116                case 't':
 117                case 'e':
 118                case 'p':
 119                        reboot_type = *str;
 120                        break;
 121
 122                case 'f':
 123                        reboot_force = 1;
 124                        break;
 125                }
 126
 127                str = strchr(str, ',');
 128                if (str)
 129                        str++;
 130                else
 131                        break;
 132        }
 133        return 1;
 134}
 135
 136__setup("reboot=", reboot_setup);
 137
 138
 139/*
 140 * Reboot options and system auto-detection code provided by
 141 * Dell Inc. so their systems "just work". :-)
 142 */
 143
 144/*
 145 * Some machines require the "reboot=b" or "reboot=k"  commandline options,
 146 * this quirk makes that automatic.
 147 */
 148static int __init set_bios_reboot(const struct dmi_system_id *d)
 149{
 150        if (reboot_type != BOOT_BIOS) {
 151                reboot_type = BOOT_BIOS;
 152                pr_info("%s series board detected. Selecting %s-method for reboots.\n",
 153                        "BIOS", d->ident);
 154        }
 155        return 0;
 156}
 157
 158void __noreturn machine_real_restart(unsigned int type)
 159{
 160        local_irq_disable();
 161
 162        /*
 163         * Write zero to CMOS register number 0x0f, which the BIOS POST
 164         * routine will recognize as telling it to do a proper reboot.  (Well
 165         * that's what this book in front of me says -- it may only apply to
 166         * the Phoenix BIOS though, it's not clear).  At the same time,
 167         * disable NMIs by setting the top bit in the CMOS address register,
 168         * as we're about to do peculiar things to the CPU.  I'm not sure if
 169         * `outb_p' is needed instead of just `outb'.  Use it to be on the
 170         * safe side.  (Yes, CMOS_WRITE does outb_p's. -  Paul G.)
 171         */
 172        spin_lock(&rtc_lock);
 173        CMOS_WRITE(0x00, 0x8f);
 174        spin_unlock(&rtc_lock);
 175
 176        /*
 177         * Switch back to the initial page table.
 178         */
 179#ifdef CONFIG_X86_32
 180        load_cr3(initial_page_table);
 181#else
 182        write_cr3(real_mode_header->trampoline_pgd);
 183
 184        /* Exiting long mode will fail if CR4.PCIDE is set. */
 185        if (static_cpu_has(X86_FEATURE_PCID))
 186                clear_in_cr4(X86_CR4_PCIDE);
 187#endif
 188
 189        /* Jump to the identity-mapped low memory code */
 190#ifdef CONFIG_X86_32
 191        asm volatile("jmpl *%0" : :
 192                     "rm" (real_mode_header->machine_real_restart_asm),
 193                     "a" (type));
 194#else
 195        asm volatile("ljmpl *%0" : :
 196                     "m" (real_mode_header->machine_real_restart_asm),
 197                     "D" (type));
 198#endif
 199        unreachable();
 200}
 201#ifdef CONFIG_APM_MODULE
 202EXPORT_SYMBOL(machine_real_restart);
 203#endif
 204
 205/*
 206 * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
 207 */
 208static int __init set_pci_reboot(const struct dmi_system_id *d)
 209{
 210        if (reboot_type != BOOT_CF9) {
 211                reboot_type = BOOT_CF9;
 212                pr_info("%s series board detected. Selecting %s-method for reboots.\n",
 213                        "PCI", d->ident);
 214        }
 215        return 0;
 216}
 217
 218static int __init set_kbd_reboot(const struct dmi_system_id *d)
 219{
 220        if (reboot_type != BOOT_KBD) {
 221                reboot_type = BOOT_KBD;
 222                pr_info("%s series board detected. Selecting %s-method for reboot.\n",
 223                        "KBD", d->ident);
 224        }
 225        return 0;
 226}
 227
 228/*
 229 * This is a single dmi_table handling all reboot quirks.
 230 */
 231static struct dmi_system_id __initdata reboot_dmi_table[] = {
 232        {       /* Handle problems with rebooting on Dell E520's */
 233                .callback = set_bios_reboot,
 234                .ident = "Dell E520",
 235                .matches = {
 236                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 237                        DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
 238                },
 239        },
 240        {       /* Handle problems with rebooting on Dell 1300's */
 241                .callback = set_bios_reboot,
 242                .ident = "Dell PowerEdge 1300",
 243                .matches = {
 244                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
 245                        DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
 246                },
 247        },
 248        {       /* Handle problems with rebooting on Dell 300's */
 249                .callback = set_bios_reboot,
 250                .ident = "Dell PowerEdge 300",
 251                .matches = {
 252                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
 253                        DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
 254                },
 255        },
 256        {       /* Handle problems with rebooting on Dell Optiplex 745's SFF */
 257                .callback = set_bios_reboot,
 258                .ident = "Dell OptiPlex 745",
 259                .matches = {
 260                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 261                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
 262                },
 263        },
 264        {       /* Handle problems with rebooting on Dell Optiplex 745's DFF */
 265                .callback = set_bios_reboot,
 266                .ident = "Dell OptiPlex 745",
 267                .matches = {
 268                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 269                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
 270                        DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
 271                },
 272        },
 273        {       /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
 274                .callback = set_bios_reboot,
 275                .ident = "Dell OptiPlex 745",
 276                .matches = {
 277                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 278                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
 279                        DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
 280                },
 281        },
 282        {       /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
 283                .callback = set_bios_reboot,
 284                .ident = "Dell OptiPlex 330",
 285                .matches = {
 286                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 287                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
 288                        DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
 289                },
 290        },
 291        {       /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
 292                .callback = set_bios_reboot,
 293                .ident = "Dell OptiPlex 360",
 294                .matches = {
 295                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 296                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
 297                        DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
 298                },
 299        },
 300        {       /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
 301                .callback = set_bios_reboot,
 302                .ident = "Dell OptiPlex 760",
 303                .matches = {
 304                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 305                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
 306                        DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
 307                },
 308        },
 309        {       /* Handle problems with rebooting on Dell 2400's */
 310                .callback = set_bios_reboot,
 311                .ident = "Dell PowerEdge 2400",
 312                .matches = {
 313                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
 314                        DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
 315                },
 316        },
 317        {       /* Handle problems with rebooting on Dell T5400's */
 318                .callback = set_bios_reboot,
 319                .ident = "Dell Precision T5400",
 320                .matches = {
 321                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 322                        DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
 323                },
 324        },
 325        {       /* Handle problems with rebooting on Dell T7400's */
 326                .callback = set_bios_reboot,
 327                .ident = "Dell Precision T7400",
 328                .matches = {
 329                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 330                        DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
 331                },
 332        },
 333        {       /* Handle problems with rebooting on HP laptops */
 334                .callback = set_bios_reboot,
 335                .ident = "HP Compaq Laptop",
 336                .matches = {
 337                        DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 338                        DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
 339                },
 340        },
 341        {       /* Handle problems with rebooting on Dell XPS710 */
 342                .callback = set_bios_reboot,
 343                .ident = "Dell XPS710",
 344                .matches = {
 345                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 346                        DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
 347                },
 348        },
 349        {       /* Handle problems with rebooting on Dell DXP061 */
 350                .callback = set_bios_reboot,
 351                .ident = "Dell DXP061",
 352                .matches = {
 353                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 354                        DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
 355                },
 356        },
 357        {       /* Handle problems with rebooting on Sony VGN-Z540N */
 358                .callback = set_bios_reboot,
 359                .ident = "Sony VGN-Z540N",
 360                .matches = {
 361                        DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 362                        DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
 363                },
 364        },
 365        {       /* Handle problems with rebooting on ASUS P4S800 */
 366                .callback = set_bios_reboot,
 367                .ident = "ASUS P4S800",
 368                .matches = {
 369                        DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
 370                        DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
 371                },
 372        },
 373
 374        {       /* Handle reboot issue on Acer Aspire one */
 375                .callback = set_kbd_reboot,
 376                .ident = "Acer Aspire One A110",
 377                .matches = {
 378                        DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
 379                        DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
 380                },
 381        },
 382        {       /* Handle problems with rebooting on Apple MacBook5 */
 383                .callback = set_pci_reboot,
 384                .ident = "Apple MacBook5",
 385                .matches = {
 386                        DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
 387                        DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
 388                },
 389        },
 390        {       /* Handle problems with rebooting on Apple MacBookPro5 */
 391                .callback = set_pci_reboot,
 392                .ident = "Apple MacBookPro5",
 393                .matches = {
 394                        DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
 395                        DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
 396                },
 397        },
 398        {       /* Handle problems with rebooting on Apple Macmini3,1 */
 399                .callback = set_pci_reboot,
 400                .ident = "Apple Macmini3,1",
 401                .matches = {
 402                        DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
 403                        DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
 404                },
 405        },
 406        {       /* Handle problems with rebooting on the iMac9,1. */
 407                .callback = set_pci_reboot,
 408                .ident = "Apple iMac9,1",
 409                .matches = {
 410                        DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
 411                        DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
 412                },
 413        },
 414        {       /* Handle problems with rebooting on the Latitude E6320. */
 415                .callback = set_pci_reboot,
 416                .ident = "Dell Latitude E6320",
 417                .matches = {
 418                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 419                        DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
 420                },
 421        },
 422        {       /* Handle problems with rebooting on the Latitude E5420. */
 423                .callback = set_pci_reboot,
 424                .ident = "Dell Latitude E5420",
 425                .matches = {
 426                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 427                        DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
 428                },
 429        },
 430        {       /* Handle problems with rebooting on the Latitude E6420. */
 431                .callback = set_pci_reboot,
 432                .ident = "Dell Latitude E6420",
 433                .matches = {
 434                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 435                        DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
 436                },
 437        },
 438        {       /* Handle problems with rebooting on the OptiPlex 990. */
 439                .callback = set_pci_reboot,
 440                .ident = "Dell OptiPlex 990",
 441                .matches = {
 442                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 443                        DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
 444                },
 445        },
 446        {       /* Handle problems with rebooting on the Precision M6600. */
 447                .callback = set_pci_reboot,
 448                .ident = "Dell OptiPlex 990",
 449                .matches = {
 450                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 451                        DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
 452                },
 453        },
 454        {       /* Handle problems with rebooting on the Dell PowerEdge C6100. */
 455                .callback = set_pci_reboot,
 456                .ident = "Dell PowerEdge C6100",
 457                .matches = {
 458                        DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 459                        DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
 460                },
 461        },
 462        {       /* Some C6100 machines were shipped with vendor being 'Dell'. */
 463                .callback = set_pci_reboot,
 464                .ident = "Dell PowerEdge C6100",
 465                .matches = {
 466                        DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
 467                        DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
 468                },
 469        },
 470        /* ASRock */
 471        {       /* Handle problems with rebooting on ASRock Q1900DC-ITX */
 472                .callback = set_pci_reboot,
 473                .ident = "ASRock Q1900DC-ITX",
 474                .matches = {
 475                        DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
 476                        DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
 477                },
 478        },
 479        { }
 480};
 481
 482static int __init reboot_init(void)
 483{
 484        /*
 485         * Only do the DMI check if reboot_type hasn't been overridden
 486         * on the command line
 487         */
 488        if (reboot_default)
 489                dmi_check_system(reboot_dmi_table);
 490        return 0;
 491}
 492core_initcall(reboot_init);
 493
 494static inline void kb_wait(void)
 495{
 496        int i;
 497
 498        for (i = 0; i < 0x10000; i++) {
 499                if ((inb(0x64) & 0x02) == 0)
 500                        break;
 501                udelay(2);
 502        }
 503}
 504
 505static void vmxoff_nmi(int cpu, struct pt_regs *regs)
 506{
 507        cpu_emergency_vmxoff();
 508}
 509
 510/* Use NMIs as IPIs to tell all CPUs to disable virtualization */
 511static void emergency_vmx_disable_all(void)
 512{
 513        /* Just make sure we won't change CPUs while doing this */
 514        local_irq_disable();
 515
 516        /*
 517         * We need to disable VMX on all CPUs before rebooting, otherwise
 518         * we risk hanging up the machine, because the CPU ignore INIT
 519         * signals when VMX is enabled.
 520         *
 521         * We can't take any locks and we may be on an inconsistent
 522         * state, so we use NMIs as IPIs to tell the other CPUs to disable
 523         * VMX and halt.
 524         *
 525         * For safety, we will avoid running the nmi_shootdown_cpus()
 526         * stuff unnecessarily, but we don't have a way to check
 527         * if other CPUs have VMX enabled. So we will call it only if the
 528         * CPU we are running on has VMX enabled.
 529         *
 530         * We will miss cases where VMX is not enabled on all CPUs. This
 531         * shouldn't do much harm because KVM always enable VMX on all
 532         * CPUs anyway. But we can miss it on the small window where KVM
 533         * is still enabling VMX.
 534         */
 535        if (cpu_has_vmx() && cpu_vmx_enabled()) {
 536                /* Disable VMX on this CPU. */
 537                cpu_vmxoff();
 538
 539                /* Halt and disable VMX on the other CPUs */
 540                nmi_shootdown_cpus(vmxoff_nmi);
 541
 542        }
 543}
 544
 545
 546void __attribute__((weak)) mach_reboot_fixups(void)
 547{
 548}
 549
 550/*
 551 * Windows compatible x86 hardware expects the following on reboot:
 552 *
 553 * 1) If the FADT has the ACPI reboot register flag set, try it
 554 * 2) If still alive, write to the keyboard controller
 555 * 3) If still alive, write to the ACPI reboot register again
 556 * 4) If still alive, write to the keyboard controller again
 557 *
 558 * If the machine is still alive at this stage, it gives up. We default to
 559 * following the same pattern, except that if we're still alive after (4) we'll
 560 * try to force a triple fault and then cycle between hitting the keyboard
 561 * controller and doing that
 562 */
 563static void native_machine_emergency_restart(void)
 564{
 565        int i;
 566        int attempt = 0;
 567        int orig_reboot_type = reboot_type;
 568
 569        if (reboot_emergency)
 570                emergency_vmx_disable_all();
 571
 572        tboot_shutdown(TB_SHUTDOWN_REBOOT);
 573
 574        /* Tell the BIOS if we want cold or warm reboot */
 575        *((unsigned short *)__va(0x472)) = reboot_mode;
 576
 577        for (;;) {
 578                /* Could also try the reset bit in the Hammer NB */
 579                switch (reboot_type) {
 580                case BOOT_KBD:
 581                        mach_reboot_fixups(); /* For board specific fixups */
 582
 583                        for (i = 0; i < 10; i++) {
 584                                kb_wait();
 585                                udelay(50);
 586                                outb(0xfe, 0x64); /* Pulse reset low */
 587                                udelay(50);
 588                        }
 589                        if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
 590                                attempt = 1;
 591                                reboot_type = BOOT_ACPI;
 592                        } else {
 593                                reboot_type = BOOT_TRIPLE;
 594                        }
 595                        break;
 596
 597                case BOOT_TRIPLE:
 598                        load_idt(&no_idt);
 599                        __asm__ __volatile__("int3");
 600
 601                        reboot_type = BOOT_KBD;
 602                        break;
 603
 604                case BOOT_BIOS:
 605                        machine_real_restart(MRR_BIOS);
 606
 607                        reboot_type = BOOT_KBD;
 608                        break;
 609
 610                case BOOT_ACPI:
 611                        acpi_reboot();
 612                        reboot_type = BOOT_KBD;
 613                        break;
 614
 615                case BOOT_EFI:
 616                        if (efi_enabled(EFI_RUNTIME_SERVICES))
 617                                efi.reset_system(reboot_mode ?
 618                                                 EFI_RESET_WARM :
 619                                                 EFI_RESET_COLD,
 620                                                 EFI_SUCCESS, 0, NULL);
 621                        reboot_type = BOOT_KBD;
 622                        break;
 623
 624                case BOOT_CF9:
 625                        port_cf9_safe = true;
 626                        /* Fall through */
 627
 628                case BOOT_CF9_COND:
 629                        if (port_cf9_safe) {
 630                                u8 cf9 = inb(0xcf9) & ~6;
 631                                outb(cf9|2, 0xcf9); /* Request hard reset */
 632                                udelay(50);
 633                                outb(cf9|6, 0xcf9); /* Actually do the reset */
 634                                udelay(50);
 635                        }
 636                        reboot_type = BOOT_KBD;
 637                        break;
 638                }
 639        }
 640}
 641
 642void native_machine_shutdown(void)
 643{
 644#ifdef CONFIG_SMP
 645        /* The boot cpu is always logical cpu 0 */
 646        int reboot_cpu_id = 0;
 647#endif
 648
 649        /* Stop the cpus and apics */
 650#ifdef CONFIG_X86_IO_APIC
 651        /*
 652         * Disabling IO APIC before local APIC is a workaround for
 653         * erratum AVR31 in "Intel Atom Processor C2000 Product Family
 654         * Specification Update". In this situation, interrupts that target
 655         * a Logical Processor whose Local APIC is either in the process of
 656         * being hardware disabled or software disabled are neither delivered
 657         * nor discarded. When this erratum occurs, the processor may hang.
 658         *
 659         * Even without the erratum, it still makes sense to quiet IO APIC
 660         * before disabling Local APIC.
 661         */
 662        disable_IO_APIC();
 663#endif
 664
 665#ifdef CONFIG_SMP
 666        /* See if there has been given a command line override */
 667        if ((reboot_cpu != -1) && (reboot_cpu < nr_cpu_ids) &&
 668                cpu_online(reboot_cpu))
 669                reboot_cpu_id = reboot_cpu;
 670
 671        /* Make certain the cpu I'm about to reboot on is online */
 672        if (!cpu_online(reboot_cpu_id))
 673                reboot_cpu_id = smp_processor_id();
 674
 675        /* Make certain I only run on the appropriate processor */
 676        set_cpus_allowed_ptr(current, cpumask_of(reboot_cpu_id));
 677
 678        /*
 679         * O.K Now that I'm on the appropriate processor, stop all of the
 680         * others. Also disable the local irq to not receive the per-cpu
 681         * timer interrupt which may trigger scheduler's load balance.
 682         */
 683        local_irq_disable();
 684        stop_other_cpus();
 685#endif
 686
 687        lapic_shutdown();
 688
 689#ifdef CONFIG_HPET_TIMER
 690        hpet_disable();
 691#endif
 692
 693#ifdef CONFIG_X86_64
 694        x86_platform.iommu_shutdown();
 695#endif
 696}
 697
 698static void __machine_emergency_restart(int emergency)
 699{
 700        reboot_emergency = emergency;
 701        machine_ops.emergency_restart();
 702}
 703
 704static void native_machine_restart(char *__unused)
 705{
 706        pr_notice("machine restart\n");
 707
 708        if (!reboot_force)
 709                machine_shutdown();
 710        __machine_emergency_restart(0);
 711}
 712
 713static void native_machine_halt(void)
 714{
 715        /* Stop other cpus and apics */
 716        machine_shutdown();
 717
 718        tboot_shutdown(TB_SHUTDOWN_HALT);
 719
 720        stop_this_cpu(NULL);
 721}
 722
 723static void native_machine_power_off(void)
 724{
 725        if (pm_power_off) {
 726                if (!reboot_force)
 727                        machine_shutdown();
 728                pm_power_off();
 729        }
 730        /* A fallback in case there is no PM info available */
 731        tboot_shutdown(TB_SHUTDOWN_HALT);
 732}
 733
 734struct machine_ops machine_ops = {
 735        .power_off = native_machine_power_off,
 736        .shutdown = native_machine_shutdown,
 737        .emergency_restart = native_machine_emergency_restart,
 738        .restart = native_machine_restart,
 739        .halt = native_machine_halt,
 740#ifdef CONFIG_KEXEC_CORE
 741        .crash_shutdown = native_machine_crash_shutdown,
 742#endif
 743};
 744
 745void machine_power_off(void)
 746{
 747        machine_ops.power_off();
 748}
 749
 750void machine_shutdown(void)
 751{
 752        machine_ops.shutdown();
 753}
 754
 755void machine_emergency_restart(void)
 756{
 757        __machine_emergency_restart(1);
 758}
 759
 760void machine_restart(char *cmd)
 761{
 762        machine_ops.restart(cmd);
 763}
 764
 765void machine_halt(void)
 766{
 767        machine_ops.halt();
 768}
 769
 770#ifdef CONFIG_KEXEC_CORE
 771void machine_crash_shutdown(struct pt_regs *regs)
 772{
 773        machine_ops.crash_shutdown(regs);
 774}
 775#endif
 776
 777
 778/* This is the CPU performing the emergency shutdown work. */
 779int crashing_cpu = -1;
 780
 781#if defined(CONFIG_SMP)
 782
 783static nmi_shootdown_cb shootdown_callback;
 784
 785static atomic_t waiting_for_crash_ipi;
 786static int crash_ipi_issued;
 787
 788static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
 789{
 790        int cpu;
 791
 792        cpu = raw_smp_processor_id();
 793
 794        /*
 795         * Don't do anything if this handler is invoked on crashing cpu.
 796         * Otherwise, system will completely hang. Crashing cpu can get
 797         * an NMI if system was initially booted with nmi_watchdog parameter.
 798         */
 799        if (cpu == crashing_cpu)
 800                return NMI_HANDLED;
 801        local_irq_disable();
 802
 803        shootdown_callback(cpu, regs);
 804
 805        atomic_dec(&waiting_for_crash_ipi);
 806        /* Assume hlt works */
 807        halt();
 808        for (;;)
 809                cpu_relax();
 810
 811        return NMI_HANDLED;
 812}
 813
 814static void smp_send_nmi_allbutself(void)
 815{
 816        apic->send_IPI_allbutself(NMI_VECTOR);
 817}
 818
 819/*
 820 * Halt all other CPUs, calling the specified function on each of them
 821 *
 822 * This function can be used to halt all other CPUs on crash
 823 * or emergency reboot time. The function passed as parameter
 824 * will be called inside a NMI handler on all CPUs.
 825 */
 826void nmi_shootdown_cpus(nmi_shootdown_cb callback)
 827{
 828        unsigned long msecs;
 829        local_irq_disable();
 830
 831        /* Make a note of crashing cpu. Will be used in NMI callback. */
 832        crashing_cpu = safe_smp_processor_id();
 833
 834        shootdown_callback = callback;
 835
 836        atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
 837        /* Would it be better to replace the trap vector here? */
 838        if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
 839                                 NMI_FLAG_FIRST, "crash"))
 840                return;         /* Return what? */
 841        /*
 842         * Ensure the new callback function is set before sending
 843         * out the NMI
 844         */
 845        wmb();
 846
 847        smp_send_nmi_allbutself();
 848
 849        /* Kick CPUs looping in NMI context. */
 850        WRITE_ONCE(crash_ipi_issued, 1);
 851
 852        msecs = 1000; /* Wait at most a second for the other cpus to stop */
 853        while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
 854                mdelay(1);
 855                msecs--;
 856        }
 857
 858        /* Leave the nmi callback set */
 859}
 860
 861/*
 862 * Check if the crash dumping IPI got issued and if so, call its callback
 863 * directly. This function is used when we have already been in NMI handler.
 864 * It doesn't return.
 865 */
 866void run_crash_ipi_callback(struct pt_regs *regs)
 867{
 868        if (crash_ipi_issued)
 869                crash_nmi_callback(0, regs);
 870}
 871
 872/* Override the weak function in kernel/panic.c */
 873void nmi_panic_self_stop(struct pt_regs *regs)
 874{
 875        while (1) {
 876                /* If no CPU is preparing crash dump, we simply loop here. */
 877                run_crash_ipi_callback(regs);
 878                cpu_relax();
 879        }
 880}
 881
 882#else /* !CONFIG_SMP */
 883void nmi_shootdown_cpus(nmi_shootdown_cb callback)
 884{
 885        /* No other CPUs to shoot down */
 886}
 887
 888void run_crash_ipi_callback(struct pt_regs *regs)
 889{
 890}
 891#endif
 892