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",
342 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
343 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
346 { /* Handle problems with rebooting on Dell 300's */
347 .callback = set_bios_reboot,
348 .ident = "Dell PowerEdge 300",
350 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
351 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
354 { /* Handle problems with rebooting on Dell 1300's */
355 .callback = set_bios_reboot,
356 .ident = "Dell PowerEdge 1300",
358 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
359 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
362 { /* Handle problems with rebooting on Dell 2400's */
363 .callback = set_bios_reboot,
364 .ident = "Dell PowerEdge 2400",
366 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
367 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
370 { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
371 .callback = set_pci_reboot,
372 .ident = "Dell PowerEdge C6100",
374 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
375 DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
378 { /* Handle problems with rebooting on the Precision M6600. */
379 .callback = set_pci_reboot,
380 .ident = "Dell Precision M6600",
382 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
383 DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
386 { /* Handle problems with rebooting on Dell T5400's */
387 .callback = set_bios_reboot,
388 .ident = "Dell Precision T5400",
390 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
391 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
394 { /* Handle problems with rebooting on Dell T7400's */
395 .callback = set_bios_reboot,
396 .ident = "Dell Precision T7400",
398 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
399 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
402 { /* Handle problems with rebooting on Dell XPS710 */
403 .callback = set_bios_reboot,
404 .ident = "Dell XPS710",
406 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
407 DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
411 /* Hewlett-Packard */
412 { /* Handle problems with rebooting on HP laptops */
413 .callback = set_bios_reboot,
414 .ident = "HP Compaq Laptop",
416 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
417 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
421 { /* PCIe Wifi card isn't detected after reboot otherwise */
422 .callback = set_pci_reboot,
423 .ident = "Zotac ZBOX CI327 nano",
425 DMI_MATCH(DMI_SYS_VENDOR, "NA"),
426 DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
431 { /* Handle problems with rebooting on Sony VGN-Z540N */
432 .callback = set_bios_reboot,
433 .ident = "Sony VGN-Z540N",
435 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
436 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
443 static int __init reboot_init(void)
448 * Only do the DMI check if reboot_type hasn't been overridden
449 * on the command line
455 * The DMI quirks table takes precedence. If no quirks entry
456 * matches and the ACPI Hardware Reduced bit is set, force EFI
459 rv = dmi_check_system(reboot_dmi_table);
461 if (!rv && efi_reboot_required())
462 reboot_type = BOOT_EFI;
466 core_initcall(reboot_init);
468 static inline void kb_wait(void)
472 for (i = 0; i < 0x10000; i++) {
473 if ((inb(0x64) & 0x02) == 0)
479 static void vmxoff_nmi(int cpu, struct pt_regs *regs)
481 cpu_emergency_vmxoff();
484 /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
485 static void emergency_vmx_disable_all(void)
487 /* Just make sure we won't change CPUs while doing this */
491 * Disable VMX on all CPUs before rebooting, otherwise we risk hanging
492 * the machine, because the CPU blocks INIT when it's in VMX root.
494 * We can't take any locks and we may be on an inconsistent state, so
495 * use NMIs as IPIs to tell the other CPUs to exit VMX root and halt.
497 * Do the NMI shootdown even if VMX if off on _this_ CPU, as that
498 * doesn't prevent a different CPU from being in VMX root operation.
501 /* Safely force _this_ CPU out of VMX root operation. */
502 __cpu_emergency_vmxoff();
504 /* Halt and exit VMX root operation on the other CPUs. */
505 nmi_shootdown_cpus(vmxoff_nmi);
511 void __attribute__((weak)) mach_reboot_fixups(void)
516 * To the best of our knowledge Windows compatible x86 hardware expects
517 * the following on reboot:
519 * 1) If the FADT has the ACPI reboot register flag set, try it
520 * 2) If still alive, write to the keyboard controller
521 * 3) If still alive, write to the ACPI reboot register again
522 * 4) If still alive, write to the keyboard controller again
523 * 5) If still alive, call the EFI runtime service to reboot
524 * 6) If no EFI runtime service, call the BIOS to do a reboot
526 * We default to following the same pattern. We also have
527 * two other reboot methods: 'triple fault' and 'PCI', which
528 * can be triggered via the reboot= kernel boot option or
531 * This means that this function can never return, it can misbehave
532 * by not rebooting properly and hanging.
534 static void native_machine_emergency_restart(void)
538 int orig_reboot_type = reboot_type;
541 if (reboot_emergency)
542 emergency_vmx_disable_all();
544 tboot_shutdown(TB_SHUTDOWN_REBOOT);
546 /* Tell the BIOS if we want cold or warm reboot */
547 mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
548 *((unsigned short *)__va(0x472)) = mode;
551 /* Could also try the reset bit in the Hammer NB */
552 switch (reboot_type) {
555 reboot_type = BOOT_KBD;
559 mach_reboot_fixups(); /* For board specific fixups */
561 for (i = 0; i < 10; i++) {
564 outb(0xfe, 0x64); /* Pulse reset low */
567 if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
569 reboot_type = BOOT_ACPI;
571 reboot_type = BOOT_EFI;
576 efi_reboot(reboot_mode, NULL);
577 reboot_type = BOOT_BIOS;
581 machine_real_restart(MRR_BIOS);
583 /* We're probably dead after this, but... */
584 reboot_type = BOOT_CF9_SAFE;
588 port_cf9_safe = true;
593 u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
594 u8 cf9 = inb(0xcf9) & ~reboot_code;
595 outb(cf9|2, 0xcf9); /* Request hard reset */
597 /* Actually do the reset */
598 outb(cf9|reboot_code, 0xcf9);
601 reboot_type = BOOT_TRIPLE;
606 __asm__ __volatile__("int3");
608 /* We're probably dead after this, but... */
609 reboot_type = BOOT_KBD;
615 void native_machine_shutdown(void)
617 /* Stop the cpus and apics */
618 #ifdef CONFIG_X86_IO_APIC
620 * Disabling IO APIC before local APIC is a workaround for
621 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
622 * Specification Update". In this situation, interrupts that target
623 * a Logical Processor whose Local APIC is either in the process of
624 * being hardware disabled or software disabled are neither delivered
625 * nor discarded. When this erratum occurs, the processor may hang.
627 * Even without the erratum, it still makes sense to quiet IO APIC
628 * before disabling Local APIC.
635 * Stop all of the others. Also disable the local irq to
636 * not receive the per-cpu timer interrupt which may trigger
637 * scheduler's load balance.
645 #ifdef CONFIG_HPET_TIMER
650 x86_platform.iommu_shutdown();
654 static void __machine_emergency_restart(int emergency)
656 reboot_emergency = emergency;
657 machine_ops.emergency_restart();
660 static void native_machine_restart(char *__unused)
662 pr_notice("machine restart\n");
666 __machine_emergency_restart(0);
669 static void native_machine_halt(void)
671 /* Stop other cpus and apics */
674 tboot_shutdown(TB_SHUTDOWN_HALT);
679 static void native_machine_power_off(void)
686 /* A fallback in case there is no PM info available */
687 tboot_shutdown(TB_SHUTDOWN_HALT);
690 struct machine_ops machine_ops = {
691 .power_off = native_machine_power_off,
692 .shutdown = native_machine_shutdown,
693 .emergency_restart = native_machine_emergency_restart,
694 .restart = native_machine_restart,
695 .halt = native_machine_halt,
696 #ifdef CONFIG_KEXEC_CORE
697 .crash_shutdown = native_machine_crash_shutdown,
701 void machine_power_off(void)
703 machine_ops.power_off();
706 void machine_shutdown(void)
708 machine_ops.shutdown();
711 void machine_emergency_restart(void)
713 __machine_emergency_restart(1);
716 void machine_restart(char *cmd)
718 machine_ops.restart(cmd);
721 void machine_halt(void)
726 #ifdef CONFIG_KEXEC_CORE
727 void machine_crash_shutdown(struct pt_regs *regs)
729 machine_ops.crash_shutdown(regs);
734 #if defined(CONFIG_SMP)
736 /* This keeps a track of which one is crashing cpu. */
737 static int crashing_cpu;
738 static nmi_shootdown_cb shootdown_callback;
740 static atomic_t waiting_for_crash_ipi;
742 static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
746 cpu = raw_smp_processor_id();
749 * Don't do anything if this handler is invoked on crashing cpu.
750 * Otherwise, system will completely hang. Crashing cpu can get
751 * an NMI if system was initially booted with nmi_watchdog parameter.
753 if (cpu == crashing_cpu)
757 shootdown_callback(cpu, regs);
759 atomic_dec(&waiting_for_crash_ipi);
760 /* Assume hlt works */
768 static void smp_send_nmi_allbutself(void)
770 apic->send_IPI_allbutself(NMI_VECTOR);
774 * Halt all other CPUs, calling the specified function on each of them
776 * This function can be used to halt all other CPUs on crash
777 * or emergency reboot time. The function passed as parameter
778 * will be called inside a NMI handler on all CPUs.
780 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
785 /* Make a note of crashing cpu. Will be used in NMI callback. */
786 crashing_cpu = safe_smp_processor_id();
788 shootdown_callback = callback;
790 atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
791 /* Would it be better to replace the trap vector here? */
792 if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
793 NMI_FLAG_FIRST, "crash"))
794 return; /* Return what? */
796 * Ensure the new callback function is set before sending
801 smp_send_nmi_allbutself();
803 msecs = 1000; /* Wait at most a second for the other cpus to stop */
804 while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
809 /* Leave the nmi callback set */
811 #else /* !CONFIG_SMP */
812 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
814 /* No other CPUs to shoot down */