GNU Linux-libre 4.4.282-gnu1
[releases.git] / arch / x86 / kernel / reboot.c
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/io_apic.h>
16 #include <asm/desc.h>
17 #include <asm/hpet.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>
24 #include <asm/cpu.h>
25 #include <asm/nmi.h>
26 #include <asm/smp.h>
27
28 #include <linux/ctype.h>
29 #include <linux/mc146818rtc.h>
30 #include <asm/realmode.h>
31 #include <asm/x86_init.h>
32 #include <asm/efi.h>
33
34 /*
35  * Power off function, if any
36  */
37 void (*pm_power_off)(void);
38 EXPORT_SYMBOL(pm_power_off);
39
40 static const struct desc_ptr no_idt = {};
41
42 /*
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
46  */
47 static int reboot_emergency;
48
49 /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
50 bool port_cf9_safe = false;
51
52 /*
53  * Reboot options and system auto-detection code provided by
54  * Dell Inc. so their systems "just work". :-)
55  */
56
57 /*
58  * Some machines require the "reboot=b" or "reboot=k"  commandline options,
59  * this quirk makes that automatic.
60  */
61 static int __init set_bios_reboot(const struct dmi_system_id *d)
62 {
63         if (reboot_type != BOOT_BIOS) {
64                 reboot_type = BOOT_BIOS;
65                 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
66                         d->ident, "BIOS");
67         }
68         return 0;
69 }
70
71 void __noreturn machine_real_restart(unsigned int type)
72 {
73         local_irq_disable();
74
75         /*
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.)
84          */
85         spin_lock(&rtc_lock);
86         CMOS_WRITE(0x00, 0x8f);
87         spin_unlock(&rtc_lock);
88
89         /*
90          * Switch back to the initial page table.
91          */
92 #ifdef CONFIG_X86_32
93         load_cr3(initial_page_table);
94 #else
95         write_cr3(real_mode_header->trampoline_pgd);
96
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);
100 #endif
101
102         /* Jump to the identity-mapped low memory code */
103 #ifdef CONFIG_X86_32
104         asm volatile("jmpl *%0" : :
105                      "rm" (real_mode_header->machine_real_restart_asm),
106                      "a" (type));
107 #else
108         asm volatile("ljmpl *%0" : :
109                      "m" (real_mode_header->machine_real_restart_asm),
110                      "D" (type));
111 #endif
112         unreachable();
113 }
114 #ifdef CONFIG_APM_MODULE
115 EXPORT_SYMBOL(machine_real_restart);
116 #endif
117
118 /*
119  * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
120  */
121 static int __init set_pci_reboot(const struct dmi_system_id *d)
122 {
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",
126                         d->ident, "PCI");
127         }
128         return 0;
129 }
130
131 static int __init set_kbd_reboot(const struct dmi_system_id *d)
132 {
133         if (reboot_type != BOOT_KBD) {
134                 reboot_type = BOOT_KBD;
135                 pr_info("%s series board detected. Selecting %s-method for reboot.\n",
136                         d->ident, "KBD");
137         }
138         return 0;
139 }
140
141 /*
142  * This is a single dmi_table handling all reboot quirks.
143  */
144 static struct dmi_system_id __initdata reboot_dmi_table[] = {
145
146         /* Acer */
147         {       /* Handle reboot issue on Acer Aspire one */
148                 .callback = set_kbd_reboot,
149                 .ident = "Acer Aspire One A110",
150                 .matches = {
151                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
152                         DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
153                 },
154         },
155
156         /* Apple */
157         {       /* Handle problems with rebooting on Apple MacBook5 */
158                 .callback = set_pci_reboot,
159                 .ident = "Apple MacBook5",
160                 .matches = {
161                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
162                         DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
163                 },
164         },
165         {       /* Handle problems with rebooting on Apple MacBook6,1 */
166                 .callback = set_pci_reboot,
167                 .ident = "Apple MacBook6,1",
168                 .matches = {
169                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
170                         DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"),
171                 },
172         },
173         {       /* Handle problems with rebooting on Apple MacBookPro5 */
174                 .callback = set_pci_reboot,
175                 .ident = "Apple MacBookPro5",
176                 .matches = {
177                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
178                         DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
179                 },
180         },
181         {       /* Handle problems with rebooting on Apple Macmini3,1 */
182                 .callback = set_pci_reboot,
183                 .ident = "Apple Macmini3,1",
184                 .matches = {
185                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
186                         DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
187                 },
188         },
189         {       /* Handle problems with rebooting on the iMac9,1. */
190                 .callback = set_pci_reboot,
191                 .ident = "Apple iMac9,1",
192                 .matches = {
193                         DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
194                         DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
195                 },
196         },
197         {       /* Handle problems with rebooting on the iMac10,1. */
198                 .callback = set_pci_reboot,
199                 .ident = "Apple iMac10,1",
200                 .matches = {
201                     DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
202                     DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
203                 },
204         },
205
206         /* ASRock */
207         {       /* Handle problems with rebooting on ASRock Q1900DC-ITX */
208                 .callback = set_pci_reboot,
209                 .ident = "ASRock Q1900DC-ITX",
210                 .matches = {
211                         DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
212                         DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
213                 },
214         },
215
216         /* ASUS */
217         {       /* Handle problems with rebooting on ASUS P4S800 */
218                 .callback = set_bios_reboot,
219                 .ident = "ASUS P4S800",
220                 .matches = {
221                         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
222                         DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
223                 },
224         },
225
226         /* Certec */
227         {       /* Handle problems with rebooting on Certec BPC600 */
228                 .callback = set_pci_reboot,
229                 .ident = "Certec BPC600",
230                 .matches = {
231                         DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
232                         DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
233                 },
234         },
235
236         /* Dell */
237         {       /* Handle problems with rebooting on Dell DXP061 */
238                 .callback = set_bios_reboot,
239                 .ident = "Dell DXP061",
240                 .matches = {
241                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
242                         DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
243                 },
244         },
245         {       /* Handle problems with rebooting on Dell E520's */
246                 .callback = set_bios_reboot,
247                 .ident = "Dell E520",
248                 .matches = {
249                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
250                         DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
251                 },
252         },
253         {       /* Handle problems with rebooting on the Latitude E5410. */
254                 .callback = set_pci_reboot,
255                 .ident = "Dell Latitude E5410",
256                 .matches = {
257                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
258                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
259                 },
260         },
261         {       /* Handle problems with rebooting on the Latitude E5420. */
262                 .callback = set_pci_reboot,
263                 .ident = "Dell Latitude E5420",
264                 .matches = {
265                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
266                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
267                 },
268         },
269         {       /* Handle problems with rebooting on the Latitude E6320. */
270                 .callback = set_pci_reboot,
271                 .ident = "Dell Latitude E6320",
272                 .matches = {
273                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
274                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
275                 },
276         },
277         {       /* Handle problems with rebooting on the Latitude E6420. */
278                 .callback = set_pci_reboot,
279                 .ident = "Dell Latitude E6420",
280                 .matches = {
281                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
282                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
283                 },
284         },
285         {       /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
286                 .callback = set_bios_reboot,
287                 .ident = "Dell OptiPlex 330",
288                 .matches = {
289                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
290                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
291                         DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
292                 },
293         },
294         {       /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
295                 .callback = set_bios_reboot,
296                 .ident = "Dell OptiPlex 360",
297                 .matches = {
298                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
299                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
300                         DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
301                 },
302         },
303         {       /* Handle problems with rebooting on Dell Optiplex 745's SFF */
304                 .callback = set_bios_reboot,
305                 .ident = "Dell OptiPlex 745",
306                 .matches = {
307                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
308                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
309                 },
310         },
311         {       /* Handle problems with rebooting on Dell Optiplex 745's DFF */
312                 .callback = set_bios_reboot,
313                 .ident = "Dell OptiPlex 745",
314                 .matches = {
315                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
316                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
317                         DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
318                 },
319         },
320         {       /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
321                 .callback = set_bios_reboot,
322                 .ident = "Dell OptiPlex 745",
323                 .matches = {
324                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
325                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
326                         DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
327                 },
328         },
329         {       /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
330                 .callback = set_bios_reboot,
331                 .ident = "Dell OptiPlex 760",
332                 .matches = {
333                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
334                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
335                         DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
336                 },
337         },
338         {       /* Handle problems with rebooting on the OptiPlex 990. */
339                 .callback = set_pci_reboot,
340                 .ident = "Dell OptiPlex 990",
341                 .matches = {
342                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
343                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
344                 },
345         },
346         {       /* Handle problems with rebooting on Dell 300's */
347                 .callback = set_bios_reboot,
348                 .ident = "Dell PowerEdge 300",
349                 .matches = {
350                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
351                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
352                 },
353         },
354         {       /* Handle problems with rebooting on Dell 1300's */
355                 .callback = set_bios_reboot,
356                 .ident = "Dell PowerEdge 1300",
357                 .matches = {
358                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
359                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
360                 },
361         },
362         {       /* Handle problems with rebooting on Dell 2400's */
363                 .callback = set_bios_reboot,
364                 .ident = "Dell PowerEdge 2400",
365                 .matches = {
366                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
367                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
368                 },
369         },
370         {       /* Handle problems with rebooting on the Dell PowerEdge C6100. */
371                 .callback = set_pci_reboot,
372                 .ident = "Dell PowerEdge C6100",
373                 .matches = {
374                         DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
375                         DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
376                 },
377         },
378         {       /* Handle problems with rebooting on the Precision M6600. */
379                 .callback = set_pci_reboot,
380                 .ident = "Dell Precision M6600",
381                 .matches = {
382                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
383                         DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
384                 },
385         },
386         {       /* Handle problems with rebooting on Dell T5400's */
387                 .callback = set_bios_reboot,
388                 .ident = "Dell Precision T5400",
389                 .matches = {
390                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
391                         DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
392                 },
393         },
394         {       /* Handle problems with rebooting on Dell T7400's */
395                 .callback = set_bios_reboot,
396                 .ident = "Dell Precision T7400",
397                 .matches = {
398                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
399                         DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
400                 },
401         },
402         {       /* Handle problems with rebooting on Dell XPS710 */
403                 .callback = set_bios_reboot,
404                 .ident = "Dell XPS710",
405                 .matches = {
406                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
407                         DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
408                 },
409         },
410
411         /* Hewlett-Packard */
412         {       /* Handle problems with rebooting on HP laptops */
413                 .callback = set_bios_reboot,
414                 .ident = "HP Compaq Laptop",
415                 .matches = {
416                         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
417                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
418                 },
419         },
420
421         {       /* PCIe Wifi card isn't detected after reboot otherwise */
422                 .callback = set_pci_reboot,
423                 .ident = "Zotac ZBOX CI327 nano",
424                 .matches = {
425                         DMI_MATCH(DMI_SYS_VENDOR, "NA"),
426                         DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
427                 },
428         },
429
430         /* Sony */
431         {       /* Handle problems with rebooting on Sony VGN-Z540N */
432                 .callback = set_bios_reboot,
433                 .ident = "Sony VGN-Z540N",
434                 .matches = {
435                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
436                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
437                 },
438         },
439
440         { }
441 };
442
443 static int __init reboot_init(void)
444 {
445         int rv;
446
447         /*
448          * Only do the DMI check if reboot_type hasn't been overridden
449          * on the command line
450          */
451         if (!reboot_default)
452                 return 0;
453
454         /*
455          * The DMI quirks table takes precedence. If no quirks entry
456          * matches and the ACPI Hardware Reduced bit is set, force EFI
457          * reboot.
458          */
459         rv = dmi_check_system(reboot_dmi_table);
460
461         if (!rv && efi_reboot_required())
462                 reboot_type = BOOT_EFI;
463
464         return 0;
465 }
466 core_initcall(reboot_init);
467
468 static inline void kb_wait(void)
469 {
470         int i;
471
472         for (i = 0; i < 0x10000; i++) {
473                 if ((inb(0x64) & 0x02) == 0)
474                         break;
475                 udelay(2);
476         }
477 }
478
479 static void vmxoff_nmi(int cpu, struct pt_regs *regs)
480 {
481         cpu_emergency_vmxoff();
482 }
483
484 /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
485 static void emergency_vmx_disable_all(void)
486 {
487         /* Just make sure we won't change CPUs while doing this */
488         local_irq_disable();
489
490         /*
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.
493          *
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.
496          *
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.
499          */
500         if (cpu_has_vmx()) {
501                 /* Safely force _this_ CPU out of VMX root operation. */
502                 __cpu_emergency_vmxoff();
503
504                 /* Halt and exit VMX root operation on the other CPUs. */
505                 nmi_shootdown_cpus(vmxoff_nmi);
506
507         }
508 }
509
510
511 void __attribute__((weak)) mach_reboot_fixups(void)
512 {
513 }
514
515 /*
516  * To the best of our knowledge Windows compatible x86 hardware expects
517  * the following on reboot:
518  *
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
525  *
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
529  * via quirks.
530  *
531  * This means that this function can never return, it can misbehave
532  * by not rebooting properly and hanging.
533  */
534 static void native_machine_emergency_restart(void)
535 {
536         int i;
537         int attempt = 0;
538         int orig_reboot_type = reboot_type;
539         unsigned short mode;
540
541         if (reboot_emergency)
542                 emergency_vmx_disable_all();
543
544         tboot_shutdown(TB_SHUTDOWN_REBOOT);
545
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;
549
550         for (;;) {
551                 /* Could also try the reset bit in the Hammer NB */
552                 switch (reboot_type) {
553                 case BOOT_ACPI:
554                         acpi_reboot();
555                         reboot_type = BOOT_KBD;
556                         break;
557
558                 case BOOT_KBD:
559                         mach_reboot_fixups(); /* For board specific fixups */
560
561                         for (i = 0; i < 10; i++) {
562                                 kb_wait();
563                                 udelay(50);
564                                 outb(0xfe, 0x64); /* Pulse reset low */
565                                 udelay(50);
566                         }
567                         if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
568                                 attempt = 1;
569                                 reboot_type = BOOT_ACPI;
570                         } else {
571                                 reboot_type = BOOT_EFI;
572                         }
573                         break;
574
575                 case BOOT_EFI:
576                         efi_reboot(reboot_mode, NULL);
577                         reboot_type = BOOT_BIOS;
578                         break;
579
580                 case BOOT_BIOS:
581                         machine_real_restart(MRR_BIOS);
582
583                         /* We're probably dead after this, but... */
584                         reboot_type = BOOT_CF9_SAFE;
585                         break;
586
587                 case BOOT_CF9_FORCE:
588                         port_cf9_safe = true;
589                         /* Fall through */
590
591                 case BOOT_CF9_SAFE:
592                         if (port_cf9_safe) {
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 */
596                                 udelay(50);
597                                 /* Actually do the reset */
598                                 outb(cf9|reboot_code, 0xcf9);
599                                 udelay(50);
600                         }
601                         reboot_type = BOOT_TRIPLE;
602                         break;
603
604                 case BOOT_TRIPLE:
605                         load_idt(&no_idt);
606                         __asm__ __volatile__("int3");
607
608                         /* We're probably dead after this, but... */
609                         reboot_type = BOOT_KBD;
610                         break;
611                 }
612         }
613 }
614
615 void native_machine_shutdown(void)
616 {
617         /* Stop the cpus and apics */
618 #ifdef CONFIG_X86_IO_APIC
619         /*
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.
626          *
627          * Even without the erratum, it still makes sense to quiet IO APIC
628          * before disabling Local APIC.
629          */
630         disable_IO_APIC();
631 #endif
632
633 #ifdef CONFIG_SMP
634         /*
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.
638          */
639         local_irq_disable();
640         stop_other_cpus();
641 #endif
642
643         lapic_shutdown();
644
645 #ifdef CONFIG_HPET_TIMER
646         hpet_disable();
647 #endif
648
649 #ifdef CONFIG_X86_64
650         x86_platform.iommu_shutdown();
651 #endif
652 }
653
654 static void __machine_emergency_restart(int emergency)
655 {
656         reboot_emergency = emergency;
657         machine_ops.emergency_restart();
658 }
659
660 static void native_machine_restart(char *__unused)
661 {
662         pr_notice("machine restart\n");
663
664         if (!reboot_force)
665                 machine_shutdown();
666         __machine_emergency_restart(0);
667 }
668
669 static void native_machine_halt(void)
670 {
671         /* Stop other cpus and apics */
672         machine_shutdown();
673
674         tboot_shutdown(TB_SHUTDOWN_HALT);
675
676         stop_this_cpu(NULL);
677 }
678
679 static void native_machine_power_off(void)
680 {
681         if (pm_power_off) {
682                 if (!reboot_force)
683                         machine_shutdown();
684                 pm_power_off();
685         }
686         /* A fallback in case there is no PM info available */
687         tboot_shutdown(TB_SHUTDOWN_HALT);
688 }
689
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,
698 #endif
699 };
700
701 void machine_power_off(void)
702 {
703         machine_ops.power_off();
704 }
705
706 void machine_shutdown(void)
707 {
708         machine_ops.shutdown();
709 }
710
711 void machine_emergency_restart(void)
712 {
713         __machine_emergency_restart(1);
714 }
715
716 void machine_restart(char *cmd)
717 {
718         machine_ops.restart(cmd);
719 }
720
721 void machine_halt(void)
722 {
723         machine_ops.halt();
724 }
725
726 #ifdef CONFIG_KEXEC_CORE
727 void machine_crash_shutdown(struct pt_regs *regs)
728 {
729         machine_ops.crash_shutdown(regs);
730 }
731 #endif
732
733
734 #if defined(CONFIG_SMP)
735
736 /* This keeps a track of which one is crashing cpu. */
737 static int crashing_cpu;
738 static nmi_shootdown_cb shootdown_callback;
739
740 static atomic_t waiting_for_crash_ipi;
741
742 static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
743 {
744         int cpu;
745
746         cpu = raw_smp_processor_id();
747
748         /*
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.
752          */
753         if (cpu == crashing_cpu)
754                 return NMI_HANDLED;
755         local_irq_disable();
756
757         shootdown_callback(cpu, regs);
758
759         atomic_dec(&waiting_for_crash_ipi);
760         /* Assume hlt works */
761         halt();
762         for (;;)
763                 cpu_relax();
764
765         return NMI_HANDLED;
766 }
767
768 static void smp_send_nmi_allbutself(void)
769 {
770         apic->send_IPI_allbutself(NMI_VECTOR);
771 }
772
773 /*
774  * Halt all other CPUs, calling the specified function on each of them
775  *
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.
779  */
780 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
781 {
782         unsigned long msecs;
783         local_irq_disable();
784
785         /* Make a note of crashing cpu. Will be used in NMI callback. */
786         crashing_cpu = safe_smp_processor_id();
787
788         shootdown_callback = callback;
789
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? */
795         /*
796          * Ensure the new callback function is set before sending
797          * out the NMI
798          */
799         wmb();
800
801         smp_send_nmi_allbutself();
802
803         msecs = 1000; /* Wait at most a second for the other cpus to stop */
804         while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
805                 mdelay(1);
806                 msecs--;
807         }
808
809         /* Leave the nmi callback set */
810 }
811 #else /* !CONFIG_SMP */
812 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
813 {
814         /* No other CPUs to shoot down */
815 }
816 #endif