1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/module.h>
4 #include <linux/reboot.h>
5 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/tboot.h>
11 #include <linux/delay.h>
12 #include <acpi/reboot.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>
27 #include <linux/ctype.h>
28 #include <linux/mc146818rtc.h>
29 #include <asm/realmode.h>
30 #include <asm/x86_init.h>
34 * Power off function, if any
36 void (*pm_power_off)(void);
37 EXPORT_SYMBOL(pm_power_off);
39 static const struct desc_ptr no_idt = {};
42 * This is set if we need to go through the 'emergency' path.
43 * When machine_emergency_restart() is called, we may be on
44 * an inconsistent state and won't be able to do a clean cleanup
46 static int reboot_emergency;
48 /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
49 bool port_cf9_safe = false;
52 * Reboot options and system auto-detection code provided by
53 * Dell Inc. so their systems "just work". :-)
57 * Some machines require the "reboot=b" or "reboot=k" commandline options,
58 * this quirk makes that automatic.
60 static int __init set_bios_reboot(const struct dmi_system_id *d)
62 if (reboot_type != BOOT_BIOS) {
63 reboot_type = BOOT_BIOS;
64 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
70 void __noreturn machine_real_restart(unsigned int type)
75 * Write zero to CMOS register number 0x0f, which the BIOS POST
76 * routine will recognize as telling it to do a proper reboot. (Well
77 * that's what this book in front of me says -- it may only apply to
78 * the Phoenix BIOS though, it's not clear). At the same time,
79 * disable NMIs by setting the top bit in the CMOS address register,
80 * as we're about to do peculiar things to the CPU. I'm not sure if
81 * `outb_p' is needed instead of just `outb'. Use it to be on the
82 * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
85 CMOS_WRITE(0x00, 0x8f);
86 spin_unlock(&rtc_lock);
89 * Switch back to the initial page table.
92 load_cr3(initial_page_table);
94 write_cr3(real_mode_header->trampoline_pgd);
97 /* Jump to the identity-mapped low memory code */
99 asm volatile("jmpl *%0" : :
100 "rm" (real_mode_header->machine_real_restart_asm),
103 asm volatile("ljmpl *%0" : :
104 "m" (real_mode_header->machine_real_restart_asm),
109 #ifdef CONFIG_APM_MODULE
110 EXPORT_SYMBOL(machine_real_restart);
114 * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
116 static int __init set_pci_reboot(const struct dmi_system_id *d)
118 if (reboot_type != BOOT_CF9_FORCE) {
119 reboot_type = BOOT_CF9_FORCE;
120 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
126 static int __init set_kbd_reboot(const struct dmi_system_id *d)
128 if (reboot_type != BOOT_KBD) {
129 reboot_type = BOOT_KBD;
130 pr_info("%s series board detected. Selecting %s-method for reboot.\n",
137 * This is a single dmi_table handling all reboot quirks.
139 static struct dmi_system_id __initdata reboot_dmi_table[] = {
142 { /* Handle reboot issue on Acer Aspire one */
143 .callback = set_kbd_reboot,
144 .ident = "Acer Aspire One A110",
146 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
147 DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
152 { /* Handle problems with rebooting on Apple MacBook5 */
153 .callback = set_pci_reboot,
154 .ident = "Apple MacBook5",
156 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
157 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
160 { /* Handle problems with rebooting on Apple MacBookPro5 */
161 .callback = set_pci_reboot,
162 .ident = "Apple MacBookPro5",
164 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
165 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
168 { /* Handle problems with rebooting on Apple Macmini3,1 */
169 .callback = set_pci_reboot,
170 .ident = "Apple Macmini3,1",
172 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
176 { /* Handle problems with rebooting on the iMac9,1. */
177 .callback = set_pci_reboot,
178 .ident = "Apple iMac9,1",
180 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
181 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
186 { /* Handle problems with rebooting on ASUS P4S800 */
187 .callback = set_bios_reboot,
188 .ident = "ASUS P4S800",
190 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
191 DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
196 { /* Handle problems with rebooting on Certec BPC600 */
197 .callback = set_pci_reboot,
198 .ident = "Certec BPC600",
200 DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
201 DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
206 { /* Handle problems with rebooting on Dell DXP061 */
207 .callback = set_bios_reboot,
208 .ident = "Dell DXP061",
210 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
211 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
214 { /* Handle problems with rebooting on Dell E520's */
215 .callback = set_bios_reboot,
216 .ident = "Dell E520",
218 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
219 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
222 { /* Handle problems with rebooting on the Latitude E5410. */
223 .callback = set_pci_reboot,
224 .ident = "Dell Latitude E5410",
226 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
227 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
230 { /* Handle problems with rebooting on the Latitude E5420. */
231 .callback = set_pci_reboot,
232 .ident = "Dell Latitude E5420",
234 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
235 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
238 { /* Handle problems with rebooting on the Latitude E6320. */
239 .callback = set_pci_reboot,
240 .ident = "Dell Latitude E6320",
242 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
243 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
246 { /* Handle problems with rebooting on the Latitude E6420. */
247 .callback = set_pci_reboot,
248 .ident = "Dell Latitude E6420",
250 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
251 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
254 { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
255 .callback = set_bios_reboot,
256 .ident = "Dell OptiPlex 330",
258 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
260 DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
263 { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
264 .callback = set_bios_reboot,
265 .ident = "Dell OptiPlex 360",
267 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
268 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
269 DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
272 { /* Handle problems with rebooting on Dell Optiplex 745's SFF */
273 .callback = set_bios_reboot,
274 .ident = "Dell OptiPlex 745",
276 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
277 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
280 { /* Handle problems with rebooting on Dell Optiplex 745's DFF */
281 .callback = set_bios_reboot,
282 .ident = "Dell OptiPlex 745",
284 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
285 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
286 DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
289 { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
290 .callback = set_bios_reboot,
291 .ident = "Dell OptiPlex 745",
293 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
294 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
295 DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
298 { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
299 .callback = set_bios_reboot,
300 .ident = "Dell OptiPlex 760",
302 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
303 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
304 DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
307 { /* Handle problems with rebooting on the OptiPlex 990. */
308 .callback = set_pci_reboot,
309 .ident = "Dell OptiPlex 990",
311 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
312 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
315 { /* Handle problems with rebooting on Dell 300's */
316 .callback = set_bios_reboot,
317 .ident = "Dell PowerEdge 300",
319 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
320 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
323 { /* Handle problems with rebooting on Dell 1300's */
324 .callback = set_bios_reboot,
325 .ident = "Dell PowerEdge 1300",
327 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
328 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
331 { /* Handle problems with rebooting on Dell 2400's */
332 .callback = set_bios_reboot,
333 .ident = "Dell PowerEdge 2400",
335 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
336 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
339 { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
340 .callback = set_pci_reboot,
341 .ident = "Dell PowerEdge C6100",
343 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
344 DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
347 { /* Handle problems with rebooting on the Precision M6600. */
348 .callback = set_pci_reboot,
349 .ident = "Dell Precision M6600",
351 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
352 DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
355 { /* Handle problems with rebooting on Dell T5400's */
356 .callback = set_bios_reboot,
357 .ident = "Dell Precision T5400",
359 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
360 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
363 { /* Handle problems with rebooting on Dell T7400's */
364 .callback = set_bios_reboot,
365 .ident = "Dell Precision T7400",
367 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
368 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
371 { /* Handle problems with rebooting on Dell XPS710 */
372 .callback = set_bios_reboot,
373 .ident = "Dell XPS710",
375 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
376 DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
380 /* Hewlett-Packard */
381 { /* Handle problems with rebooting on HP laptops */
382 .callback = set_bios_reboot,
383 .ident = "HP Compaq Laptop",
385 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
386 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
391 { /* Handle problems with rebooting on Sony VGN-Z540N */
392 .callback = set_bios_reboot,
393 .ident = "Sony VGN-Z540N",
395 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
396 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
403 static int __init reboot_init(void)
408 * Only do the DMI check if reboot_type hasn't been overridden
409 * on the command line
415 * The DMI quirks table takes precedence. If no quirks entry
416 * matches and the ACPI Hardware Reduced bit is set, force EFI
419 rv = dmi_check_system(reboot_dmi_table);
421 if (!rv && efi_reboot_required())
422 reboot_type = BOOT_EFI;
426 core_initcall(reboot_init);
428 static inline void kb_wait(void)
432 for (i = 0; i < 0x10000; i++) {
433 if ((inb(0x64) & 0x02) == 0)
439 static void vmxoff_nmi(int cpu, struct pt_regs *regs)
441 cpu_emergency_vmxoff();
444 /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
445 static void emergency_vmx_disable_all(void)
447 /* Just make sure we won't change CPUs while doing this */
451 * We need to disable VMX on all CPUs before rebooting, otherwise
452 * we risk hanging up the machine, because the CPU ignore INIT
453 * signals when VMX is enabled.
455 * We can't take any locks and we may be on an inconsistent
456 * state, so we use NMIs as IPIs to tell the other CPUs to disable
459 * For safety, we will avoid running the nmi_shootdown_cpus()
460 * stuff unnecessarily, but we don't have a way to check
461 * if other CPUs have VMX enabled. So we will call it only if the
462 * CPU we are running on has VMX enabled.
464 * We will miss cases where VMX is not enabled on all CPUs. This
465 * shouldn't do much harm because KVM always enable VMX on all
466 * CPUs anyway. But we can miss it on the small window where KVM
467 * is still enabling VMX.
469 if (cpu_has_vmx() && cpu_vmx_enabled()) {
470 /* Disable VMX on this CPU. */
473 /* Halt and disable VMX on the other CPUs */
474 nmi_shootdown_cpus(vmxoff_nmi);
480 void __attribute__((weak)) mach_reboot_fixups(void)
485 * To the best of our knowledge Windows compatible x86 hardware expects
486 * the following on reboot:
488 * 1) If the FADT has the ACPI reboot register flag set, try it
489 * 2) If still alive, write to the keyboard controller
490 * 3) If still alive, write to the ACPI reboot register again
491 * 4) If still alive, write to the keyboard controller again
492 * 5) If still alive, call the EFI runtime service to reboot
493 * 6) If no EFI runtime service, call the BIOS to do a reboot
495 * We default to following the same pattern. We also have
496 * two other reboot methods: 'triple fault' and 'PCI', which
497 * can be triggered via the reboot= kernel boot option or
500 * This means that this function can never return, it can misbehave
501 * by not rebooting properly and hanging.
503 static void native_machine_emergency_restart(void)
507 int orig_reboot_type = reboot_type;
510 if (reboot_emergency)
511 emergency_vmx_disable_all();
513 tboot_shutdown(TB_SHUTDOWN_REBOOT);
515 /* Tell the BIOS if we want cold or warm reboot */
516 mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
517 *((unsigned short *)__va(0x472)) = mode;
520 /* Could also try the reset bit in the Hammer NB */
521 switch (reboot_type) {
524 reboot_type = BOOT_KBD;
528 mach_reboot_fixups(); /* For board specific fixups */
530 for (i = 0; i < 10; i++) {
533 outb(0xfe, 0x64); /* Pulse reset low */
536 if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
538 reboot_type = BOOT_ACPI;
540 reboot_type = BOOT_EFI;
545 efi_reboot(reboot_mode, NULL);
546 reboot_type = BOOT_BIOS;
550 machine_real_restart(MRR_BIOS);
552 /* We're probably dead after this, but... */
553 reboot_type = BOOT_CF9_SAFE;
557 port_cf9_safe = true;
562 u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
563 u8 cf9 = inb(0xcf9) & ~reboot_code;
564 outb(cf9|2, 0xcf9); /* Request hard reset */
566 /* Actually do the reset */
567 outb(cf9|reboot_code, 0xcf9);
570 reboot_type = BOOT_TRIPLE;
575 __asm__ __volatile__("int3");
577 /* We're probably dead after this, but... */
578 reboot_type = BOOT_KBD;
584 void native_machine_shutdown(void)
586 /* Stop the cpus and apics */
587 #ifdef CONFIG_X86_IO_APIC
589 * Disabling IO APIC before local APIC is a workaround for
590 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
591 * Specification Update". In this situation, interrupts that target
592 * a Logical Processor whose Local APIC is either in the process of
593 * being hardware disabled or software disabled are neither delivered
594 * nor discarded. When this erratum occurs, the processor may hang.
596 * Even without the erratum, it still makes sense to quiet IO APIC
597 * before disabling Local APIC.
604 * Stop all of the others. Also disable the local irq to
605 * not receive the per-cpu timer interrupt which may trigger
606 * scheduler's load balance.
614 #ifdef CONFIG_HPET_TIMER
619 x86_platform.iommu_shutdown();
623 static void __machine_emergency_restart(int emergency)
625 reboot_emergency = emergency;
626 machine_ops.emergency_restart();
629 static void native_machine_restart(char *__unused)
631 pr_notice("machine restart\n");
635 __machine_emergency_restart(0);
638 static void native_machine_halt(void)
640 /* Stop other cpus and apics */
643 tboot_shutdown(TB_SHUTDOWN_HALT);
648 static void native_machine_power_off(void)
655 /* A fallback in case there is no PM info available */
656 tboot_shutdown(TB_SHUTDOWN_HALT);
659 struct machine_ops machine_ops = {
660 .power_off = native_machine_power_off,
661 .shutdown = native_machine_shutdown,
662 .emergency_restart = native_machine_emergency_restart,
663 .restart = native_machine_restart,
664 .halt = native_machine_halt,
666 .crash_shutdown = native_machine_crash_shutdown,
670 void machine_power_off(void)
672 machine_ops.power_off();
675 void machine_shutdown(void)
677 machine_ops.shutdown();
680 void machine_emergency_restart(void)
682 __machine_emergency_restart(1);
685 void machine_restart(char *cmd)
687 machine_ops.restart(cmd);
690 void machine_halt(void)
696 void machine_crash_shutdown(struct pt_regs *regs)
698 machine_ops.crash_shutdown(regs);
703 #if defined(CONFIG_SMP)
705 /* This keeps a track of which one is crashing cpu. */
706 static int crashing_cpu;
707 static nmi_shootdown_cb shootdown_callback;
709 static atomic_t waiting_for_crash_ipi;
711 static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
715 cpu = raw_smp_processor_id();
718 * Don't do anything if this handler is invoked on crashing cpu.
719 * Otherwise, system will completely hang. Crashing cpu can get
720 * an NMI if system was initially booted with nmi_watchdog parameter.
722 if (cpu == crashing_cpu)
726 shootdown_callback(cpu, regs);
728 atomic_dec(&waiting_for_crash_ipi);
729 /* Assume hlt works */
737 static void smp_send_nmi_allbutself(void)
739 apic->send_IPI_allbutself(NMI_VECTOR);
743 * Halt all other CPUs, calling the specified function on each of them
745 * This function can be used to halt all other CPUs on crash
746 * or emergency reboot time. The function passed as parameter
747 * will be called inside a NMI handler on all CPUs.
749 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
754 /* Make a note of crashing cpu. Will be used in NMI callback. */
755 crashing_cpu = safe_smp_processor_id();
757 shootdown_callback = callback;
759 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
760 /* Would it be better to replace the trap vector here? */
761 if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
762 NMI_FLAG_FIRST, "crash"))
763 return; /* Return what? */
765 * Ensure the new callback function is set before sending
770 smp_send_nmi_allbutself();
772 msecs = 1000; /* Wait at most a second for the other cpus to stop */
773 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
778 /* Leave the nmi callback set */
780 #else /* !CONFIG_SMP */
781 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
783 /* No other CPUs to shoot down */