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>
15 #include <asm/io_apic.h>
18 #include <asm/pgtable.h>
19 #include <asm/proto.h>
20 #include <asm/reboot_fixups.h>
21 #include <asm/reboot.h>
22 #include <asm/pci_x86.h>
23 #include <asm/virtext.h>
28 #include <linux/ctype.h>
29 #include <linux/mc146818rtc.h>
30 #include <asm/realmode.h>
31 #include <asm/x86_init.h>
35 * Power off function, if any
37 void (*pm_power_off)(void);
38 EXPORT_SYMBOL(pm_power_off);
40 static const struct desc_ptr no_idt = {};
43 * This is set if we need to go through the 'emergency' path.
44 * When machine_emergency_restart() is called, we may be on
45 * an inconsistent state and won't be able to do a clean cleanup
47 static int reboot_emergency;
49 /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
50 bool port_cf9_safe = false;
53 * Reboot options and system auto-detection code provided by
54 * Dell Inc. so their systems "just work". :-)
58 * Some machines require the "reboot=b" or "reboot=k" commandline options,
59 * this quirk makes that automatic.
61 static int __init set_bios_reboot(const struct dmi_system_id *d)
63 if (reboot_type != BOOT_BIOS) {
64 reboot_type = BOOT_BIOS;
65 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
71 void __noreturn machine_real_restart(unsigned int type)
76 * Write zero to CMOS register number 0x0f, which the BIOS POST
77 * routine will recognize as telling it to do a proper reboot. (Well
78 * that's what this book in front of me says -- it may only apply to
79 * the Phoenix BIOS though, it's not clear). At the same time,
80 * disable NMIs by setting the top bit in the CMOS address register,
81 * as we're about to do peculiar things to the CPU. I'm not sure if
82 * `outb_p' is needed instead of just `outb'. Use it to be on the
83 * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
86 CMOS_WRITE(0x00, 0x8f);
87 spin_unlock(&rtc_lock);
90 * Switch back to the initial page table.
93 load_cr3(initial_page_table);
95 write_cr3(real_mode_header->trampoline_pgd);
97 /* Exiting long mode will fail if CR4.PCIDE is set. */
98 if (static_cpu_has(X86_FEATURE_PCID))
99 cr4_clear_bits(X86_CR4_PCIDE);
102 /* Jump to the identity-mapped low memory code */
104 asm volatile("jmpl *%0" : :
105 "rm" (real_mode_header->machine_real_restart_asm),
108 asm volatile("ljmpl *%0" : :
109 "m" (real_mode_header->machine_real_restart_asm),
114 #ifdef CONFIG_APM_MODULE
115 EXPORT_SYMBOL(machine_real_restart);
119 * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
121 static int __init set_pci_reboot(const struct dmi_system_id *d)
123 if (reboot_type != BOOT_CF9_FORCE) {
124 reboot_type = BOOT_CF9_FORCE;
125 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
131 static int __init set_kbd_reboot(const struct dmi_system_id *d)
133 if (reboot_type != BOOT_KBD) {
134 reboot_type = BOOT_KBD;
135 pr_info("%s series board detected. Selecting %s-method for reboot.\n",
142 * This is a single dmi_table handling all reboot quirks.
144 static struct dmi_system_id __initdata reboot_dmi_table[] = {
147 { /* Handle reboot issue on Acer Aspire one */
148 .callback = set_kbd_reboot,
149 .ident = "Acer Aspire One A110",
151 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
152 DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
157 { /* Handle problems with rebooting on Apple MacBook5 */
158 .callback = set_pci_reboot,
159 .ident = "Apple MacBook5",
161 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
162 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
165 { /* Handle problems with rebooting on Apple MacBook6,1 */
166 .callback = set_pci_reboot,
167 .ident = "Apple MacBook6,1",
169 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
170 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"),
173 { /* Handle problems with rebooting on Apple MacBookPro5 */
174 .callback = set_pci_reboot,
175 .ident = "Apple MacBookPro5",
177 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
178 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
181 { /* Handle problems with rebooting on Apple Macmini3,1 */
182 .callback = set_pci_reboot,
183 .ident = "Apple Macmini3,1",
185 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
186 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
189 { /* Handle problems with rebooting on the iMac9,1. */
190 .callback = set_pci_reboot,
191 .ident = "Apple iMac9,1",
193 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
194 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
197 { /* Handle problems with rebooting on the iMac10,1. */
198 .callback = set_pci_reboot,
199 .ident = "Apple iMac10,1",
201 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
202 DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
207 { /* Handle problems with rebooting on ASRock Q1900DC-ITX */
208 .callback = set_pci_reboot,
209 .ident = "ASRock Q1900DC-ITX",
211 DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
212 DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
217 { /* Handle problems with rebooting on ASUS P4S800 */
218 .callback = set_bios_reboot,
219 .ident = "ASUS P4S800",
221 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
222 DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
227 { /* Handle problems with rebooting on Certec BPC600 */
228 .callback = set_pci_reboot,
229 .ident = "Certec BPC600",
231 DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
232 DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
237 { /* Handle problems with rebooting on Dell DXP061 */
238 .callback = set_bios_reboot,
239 .ident = "Dell DXP061",
241 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
242 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
245 { /* Handle problems with rebooting on Dell E520's */
246 .callback = set_bios_reboot,
247 .ident = "Dell E520",
249 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
253 { /* Handle problems with rebooting on the Latitude E5410. */
254 .callback = set_pci_reboot,
255 .ident = "Dell Latitude E5410",
257 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
258 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
261 { /* Handle problems with rebooting on the Latitude E5420. */
262 .callback = set_pci_reboot,
263 .ident = "Dell Latitude E5420",
265 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
266 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
269 { /* Handle problems with rebooting on the Latitude E6320. */
270 .callback = set_pci_reboot,
271 .ident = "Dell Latitude E6320",
273 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
274 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
277 { /* Handle problems with rebooting on the Latitude E6420. */
278 .callback = set_pci_reboot,
279 .ident = "Dell Latitude E6420",
281 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
282 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
285 { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
286 .callback = set_bios_reboot,
287 .ident = "Dell OptiPlex 330",
289 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
290 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
291 DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
294 { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
295 .callback = set_bios_reboot,
296 .ident = "Dell OptiPlex 360",
298 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
299 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
300 DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
303 { /* Handle problems with rebooting on Dell Optiplex 745's SFF */
304 .callback = set_bios_reboot,
305 .ident = "Dell OptiPlex 745",
307 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
308 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
311 { /* Handle problems with rebooting on Dell Optiplex 745's DFF */
312 .callback = set_bios_reboot,
313 .ident = "Dell OptiPlex 745",
315 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
316 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
317 DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
320 { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
321 .callback = set_bios_reboot,
322 .ident = "Dell OptiPlex 745",
324 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
325 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
326 DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
329 { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
330 .callback = set_bios_reboot,
331 .ident = "Dell OptiPlex 760",
333 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
334 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
335 DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
338 { /* Handle problems with rebooting on the OptiPlex 990. */
339 .callback = set_pci_reboot,
340 .ident = "Dell OptiPlex 990 BIOS A0x",
342 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
343 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
344 DMI_MATCH(DMI_BIOS_VERSION, "A0"),
347 { /* Handle problems with rebooting on Dell 300's */
348 .callback = set_bios_reboot,
349 .ident = "Dell PowerEdge 300",
351 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
352 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
355 { /* Handle problems with rebooting on Dell 1300's */
356 .callback = set_bios_reboot,
357 .ident = "Dell PowerEdge 1300",
359 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
360 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
363 { /* Handle problems with rebooting on Dell 2400's */
364 .callback = set_bios_reboot,
365 .ident = "Dell PowerEdge 2400",
367 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
368 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
371 { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
372 .callback = set_pci_reboot,
373 .ident = "Dell PowerEdge C6100",
375 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
376 DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
379 { /* Handle problems with rebooting on the Precision M6600. */
380 .callback = set_pci_reboot,
381 .ident = "Dell Precision M6600",
383 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
384 DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
387 { /* Handle problems with rebooting on Dell T5400's */
388 .callback = set_bios_reboot,
389 .ident = "Dell Precision T5400",
391 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
392 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
395 { /* Handle problems with rebooting on Dell T7400's */
396 .callback = set_bios_reboot,
397 .ident = "Dell Precision T7400",
399 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
400 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
403 { /* Handle problems with rebooting on Dell XPS710 */
404 .callback = set_bios_reboot,
405 .ident = "Dell XPS710",
407 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
408 DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
412 /* Hewlett-Packard */
413 { /* Handle problems with rebooting on HP laptops */
414 .callback = set_bios_reboot,
415 .ident = "HP Compaq Laptop",
417 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
418 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
422 { /* PCIe Wifi card isn't detected after reboot otherwise */
423 .callback = set_pci_reboot,
424 .ident = "Zotac ZBOX CI327 nano",
426 DMI_MATCH(DMI_SYS_VENDOR, "NA"),
427 DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
432 { /* Handle problems with rebooting on Sony VGN-Z540N */
433 .callback = set_bios_reboot,
434 .ident = "Sony VGN-Z540N",
436 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
437 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
444 static int __init reboot_init(void)
449 * Only do the DMI check if reboot_type hasn't been overridden
450 * on the command line
456 * The DMI quirks table takes precedence. If no quirks entry
457 * matches and the ACPI Hardware Reduced bit is set, force EFI
460 rv = dmi_check_system(reboot_dmi_table);
462 if (!rv && efi_reboot_required())
463 reboot_type = BOOT_EFI;
467 core_initcall(reboot_init);
469 static inline void kb_wait(void)
473 for (i = 0; i < 0x10000; i++) {
474 if ((inb(0x64) & 0x02) == 0)
480 static void vmxoff_nmi(int cpu, struct pt_regs *regs)
482 cpu_emergency_vmxoff();
485 /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
486 static void emergency_vmx_disable_all(void)
488 /* Just make sure we won't change CPUs while doing this */
492 * Disable VMX on all CPUs before rebooting, otherwise we risk hanging
493 * the machine, because the CPU blocks INIT when it's in VMX root.
495 * We can't take any locks and we may be on an inconsistent state, so
496 * use NMIs as IPIs to tell the other CPUs to exit VMX root and halt.
498 * Do the NMI shootdown even if VMX if off on _this_ CPU, as that
499 * doesn't prevent a different CPU from being in VMX root operation.
502 /* Safely force _this_ CPU out of VMX root operation. */
503 __cpu_emergency_vmxoff();
505 /* Halt and exit VMX root operation on the other CPUs. */
506 nmi_shootdown_cpus(vmxoff_nmi);
512 void __attribute__((weak)) mach_reboot_fixups(void)
517 * To the best of our knowledge Windows compatible x86 hardware expects
518 * the following on reboot:
520 * 1) If the FADT has the ACPI reboot register flag set, try it
521 * 2) If still alive, write to the keyboard controller
522 * 3) If still alive, write to the ACPI reboot register again
523 * 4) If still alive, write to the keyboard controller again
524 * 5) If still alive, call the EFI runtime service to reboot
525 * 6) If no EFI runtime service, call the BIOS to do a reboot
527 * We default to following the same pattern. We also have
528 * two other reboot methods: 'triple fault' and 'PCI', which
529 * can be triggered via the reboot= kernel boot option or
532 * This means that this function can never return, it can misbehave
533 * by not rebooting properly and hanging.
535 static void native_machine_emergency_restart(void)
539 int orig_reboot_type = reboot_type;
542 if (reboot_emergency)
543 emergency_vmx_disable_all();
545 tboot_shutdown(TB_SHUTDOWN_REBOOT);
547 /* Tell the BIOS if we want cold or warm reboot */
548 mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
549 *((unsigned short *)__va(0x472)) = mode;
552 /* Could also try the reset bit in the Hammer NB */
553 switch (reboot_type) {
556 reboot_type = BOOT_KBD;
560 mach_reboot_fixups(); /* For board specific fixups */
562 for (i = 0; i < 10; i++) {
565 outb(0xfe, 0x64); /* Pulse reset low */
568 if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
570 reboot_type = BOOT_ACPI;
572 reboot_type = BOOT_EFI;
577 efi_reboot(reboot_mode, NULL);
578 reboot_type = BOOT_BIOS;
582 machine_real_restart(MRR_BIOS);
584 /* We're probably dead after this, but... */
585 reboot_type = BOOT_CF9_SAFE;
589 port_cf9_safe = true;
594 u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
595 u8 cf9 = inb(0xcf9) & ~reboot_code;
596 outb(cf9|2, 0xcf9); /* Request hard reset */
598 /* Actually do the reset */
599 outb(cf9|reboot_code, 0xcf9);
602 reboot_type = BOOT_TRIPLE;
607 __asm__ __volatile__("int3");
609 /* We're probably dead after this, but... */
610 reboot_type = BOOT_KBD;
616 void native_machine_shutdown(void)
618 /* Stop the cpus and apics */
619 #ifdef CONFIG_X86_IO_APIC
621 * Disabling IO APIC before local APIC is a workaround for
622 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
623 * Specification Update". In this situation, interrupts that target
624 * a Logical Processor whose Local APIC is either in the process of
625 * being hardware disabled or software disabled are neither delivered
626 * nor discarded. When this erratum occurs, the processor may hang.
628 * Even without the erratum, it still makes sense to quiet IO APIC
629 * before disabling Local APIC.
636 * Stop all of the others. Also disable the local irq to
637 * not receive the per-cpu timer interrupt which may trigger
638 * scheduler's load balance.
646 #ifdef CONFIG_HPET_TIMER
651 x86_platform.iommu_shutdown();
655 static void __machine_emergency_restart(int emergency)
657 reboot_emergency = emergency;
658 machine_ops.emergency_restart();
661 static void native_machine_restart(char *__unused)
663 pr_notice("machine restart\n");
667 __machine_emergency_restart(0);
670 static void native_machine_halt(void)
672 /* Stop other cpus and apics */
675 tboot_shutdown(TB_SHUTDOWN_HALT);
680 static void native_machine_power_off(void)
687 /* A fallback in case there is no PM info available */
688 tboot_shutdown(TB_SHUTDOWN_HALT);
691 struct machine_ops machine_ops = {
692 .power_off = native_machine_power_off,
693 .shutdown = native_machine_shutdown,
694 .emergency_restart = native_machine_emergency_restart,
695 .restart = native_machine_restart,
696 .halt = native_machine_halt,
697 #ifdef CONFIG_KEXEC_CORE
698 .crash_shutdown = native_machine_crash_shutdown,
702 void machine_power_off(void)
704 machine_ops.power_off();
707 void machine_shutdown(void)
709 machine_ops.shutdown();
712 void machine_emergency_restart(void)
714 __machine_emergency_restart(1);
717 void machine_restart(char *cmd)
719 machine_ops.restart(cmd);
722 void machine_halt(void)
727 #ifdef CONFIG_KEXEC_CORE
728 void machine_crash_shutdown(struct pt_regs *regs)
730 machine_ops.crash_shutdown(regs);
735 #if defined(CONFIG_SMP)
737 /* This keeps a track of which one is crashing cpu. */
738 static int crashing_cpu;
739 static nmi_shootdown_cb shootdown_callback;
741 static atomic_t waiting_for_crash_ipi;
743 static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
747 cpu = raw_smp_processor_id();
750 * Don't do anything if this handler is invoked on crashing cpu.
751 * Otherwise, system will completely hang. Crashing cpu can get
752 * an NMI if system was initially booted with nmi_watchdog parameter.
754 if (cpu == crashing_cpu)
758 shootdown_callback(cpu, regs);
760 atomic_dec(&waiting_for_crash_ipi);
761 /* Assume hlt works */
769 static void smp_send_nmi_allbutself(void)
771 apic->send_IPI_allbutself(NMI_VECTOR);
775 * Halt all other CPUs, calling the specified function on each of them
777 * This function can be used to halt all other CPUs on crash
778 * or emergency reboot time. The function passed as parameter
779 * will be called inside a NMI handler on all CPUs.
781 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
786 /* Make a note of crashing cpu. Will be used in NMI callback. */
787 crashing_cpu = safe_smp_processor_id();
789 shootdown_callback = callback;
791 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
792 /* Would it be better to replace the trap vector here? */
793 if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
794 NMI_FLAG_FIRST, "crash"))
795 return; /* Return what? */
797 * Ensure the new callback function is set before sending
802 smp_send_nmi_allbutself();
804 msecs = 1000; /* Wait at most a second for the other cpus to stop */
805 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
810 /* Leave the nmi callback set */
812 #else /* !CONFIG_SMP */
813 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
815 /* No other CPUs to shoot down */