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