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