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