xref: /openbmc/linux/arch/x86/kernel/reboot.c (revision 62e7ca52)
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  */
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 
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  */
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 
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 	/* ASUS */
186 	{	/* Handle problems with rebooting on ASUS P4S800 */
187 		.callback = set_bios_reboot,
188 		.ident = "ASUS P4S800",
189 		.matches = {
190 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
191 			DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
192 		},
193 	},
194 
195 	/* Certec */
196 	{       /* Handle problems with rebooting on Certec BPC600 */
197 		.callback = set_pci_reboot,
198 		.ident = "Certec BPC600",
199 		.matches = {
200 			DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
201 			DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
202 		},
203 	},
204 
205 	/* Dell */
206 	{	/* Handle problems with rebooting on Dell DXP061 */
207 		.callback = set_bios_reboot,
208 		.ident = "Dell DXP061",
209 		.matches = {
210 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
211 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
212 		},
213 	},
214 	{	/* Handle problems with rebooting on Dell E520's */
215 		.callback = set_bios_reboot,
216 		.ident = "Dell E520",
217 		.matches = {
218 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
219 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
220 		},
221 	},
222 	{	/* Handle problems with rebooting on the Latitude E5410. */
223 		.callback = set_pci_reboot,
224 		.ident = "Dell Latitude E5410",
225 		.matches = {
226 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
227 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
228 		},
229 	},
230 	{	/* Handle problems with rebooting on the Latitude E5420. */
231 		.callback = set_pci_reboot,
232 		.ident = "Dell Latitude E5420",
233 		.matches = {
234 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
235 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
236 		},
237 	},
238 	{	/* Handle problems with rebooting on the Latitude E6320. */
239 		.callback = set_pci_reboot,
240 		.ident = "Dell Latitude E6320",
241 		.matches = {
242 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
243 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
244 		},
245 	},
246 	{	/* Handle problems with rebooting on the Latitude E6420. */
247 		.callback = set_pci_reboot,
248 		.ident = "Dell Latitude E6420",
249 		.matches = {
250 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
251 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
252 		},
253 	},
254 	{	/* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
255 		.callback = set_bios_reboot,
256 		.ident = "Dell OptiPlex 330",
257 		.matches = {
258 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
259 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
260 			DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
261 		},
262 	},
263 	{	/* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
264 		.callback = set_bios_reboot,
265 		.ident = "Dell OptiPlex 360",
266 		.matches = {
267 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
268 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
269 			DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
270 		},
271 	},
272 	{	/* Handle problems with rebooting on Dell Optiplex 745's SFF */
273 		.callback = set_bios_reboot,
274 		.ident = "Dell OptiPlex 745",
275 		.matches = {
276 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
277 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
278 		},
279 	},
280 	{	/* Handle problems with rebooting on Dell Optiplex 745's DFF */
281 		.callback = set_bios_reboot,
282 		.ident = "Dell OptiPlex 745",
283 		.matches = {
284 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
285 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
286 			DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
287 		},
288 	},
289 	{	/* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
290 		.callback = set_bios_reboot,
291 		.ident = "Dell OptiPlex 745",
292 		.matches = {
293 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
294 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
295 			DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
296 		},
297 	},
298 	{	/* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
299 		.callback = set_bios_reboot,
300 		.ident = "Dell OptiPlex 760",
301 		.matches = {
302 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
303 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
304 			DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
305 		},
306 	},
307 	{	/* Handle problems with rebooting on the OptiPlex 990. */
308 		.callback = set_pci_reboot,
309 		.ident = "Dell OptiPlex 990",
310 		.matches = {
311 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
312 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
313 		},
314 	},
315 	{	/* Handle problems with rebooting on Dell 300's */
316 		.callback = set_bios_reboot,
317 		.ident = "Dell PowerEdge 300",
318 		.matches = {
319 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
320 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
321 		},
322 	},
323 	{	/* Handle problems with rebooting on Dell 1300's */
324 		.callback = set_bios_reboot,
325 		.ident = "Dell PowerEdge 1300",
326 		.matches = {
327 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
328 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
329 		},
330 	},
331 	{	/* Handle problems with rebooting on Dell 2400's */
332 		.callback = set_bios_reboot,
333 		.ident = "Dell PowerEdge 2400",
334 		.matches = {
335 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
336 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
337 		},
338 	},
339 	{	/* Handle problems with rebooting on the Dell PowerEdge C6100. */
340 		.callback = set_pci_reboot,
341 		.ident = "Dell PowerEdge C6100",
342 		.matches = {
343 			DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
344 			DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
345 		},
346 	},
347 	{	/* Handle problems with rebooting on the Precision M6600. */
348 		.callback = set_pci_reboot,
349 		.ident = "Dell Precision M6600",
350 		.matches = {
351 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
352 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
353 		},
354 	},
355 	{	/* Handle problems with rebooting on Dell T5400's */
356 		.callback = set_bios_reboot,
357 		.ident = "Dell Precision T5400",
358 		.matches = {
359 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
360 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
361 		},
362 	},
363 	{	/* Handle problems with rebooting on Dell T7400's */
364 		.callback = set_bios_reboot,
365 		.ident = "Dell Precision T7400",
366 		.matches = {
367 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
368 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
369 		},
370 	},
371 	{	/* Handle problems with rebooting on Dell XPS710 */
372 		.callback = set_bios_reboot,
373 		.ident = "Dell XPS710",
374 		.matches = {
375 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
376 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
377 		},
378 	},
379 
380 	/* Hewlett-Packard */
381 	{	/* Handle problems with rebooting on HP laptops */
382 		.callback = set_bios_reboot,
383 		.ident = "HP Compaq Laptop",
384 		.matches = {
385 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
386 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
387 		},
388 	},
389 
390 	/* Sony */
391 	{	/* Handle problems with rebooting on Sony VGN-Z540N */
392 		.callback = set_bios_reboot,
393 		.ident = "Sony VGN-Z540N",
394 		.matches = {
395 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
396 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
397 		},
398 	},
399 
400 	{ }
401 };
402 
403 static int __init reboot_init(void)
404 {
405 	int rv;
406 
407 	/*
408 	 * Only do the DMI check if reboot_type hasn't been overridden
409 	 * on the command line
410 	 */
411 	if (!reboot_default)
412 		return 0;
413 
414 	/*
415 	 * The DMI quirks table takes precedence. If no quirks entry
416 	 * matches and the ACPI Hardware Reduced bit is set, force EFI
417 	 * reboot.
418 	 */
419 	rv = dmi_check_system(reboot_dmi_table);
420 
421 	if (!rv && efi_reboot_required())
422 		reboot_type = BOOT_EFI;
423 
424 	return 0;
425 }
426 core_initcall(reboot_init);
427 
428 static inline void kb_wait(void)
429 {
430 	int i;
431 
432 	for (i = 0; i < 0x10000; i++) {
433 		if ((inb(0x64) & 0x02) == 0)
434 			break;
435 		udelay(2);
436 	}
437 }
438 
439 static void vmxoff_nmi(int cpu, struct pt_regs *regs)
440 {
441 	cpu_emergency_vmxoff();
442 }
443 
444 /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
445 static void emergency_vmx_disable_all(void)
446 {
447 	/* Just make sure we won't change CPUs while doing this */
448 	local_irq_disable();
449 
450 	/*
451 	 * We need to disable VMX on all CPUs before rebooting, otherwise
452 	 * we risk hanging up the machine, because the CPU ignore INIT
453 	 * signals when VMX is enabled.
454 	 *
455 	 * We can't take any locks and we may be on an inconsistent
456 	 * state, so we use NMIs as IPIs to tell the other CPUs to disable
457 	 * VMX and halt.
458 	 *
459 	 * For safety, we will avoid running the nmi_shootdown_cpus()
460 	 * stuff unnecessarily, but we don't have a way to check
461 	 * if other CPUs have VMX enabled. So we will call it only if the
462 	 * CPU we are running on has VMX enabled.
463 	 *
464 	 * We will miss cases where VMX is not enabled on all CPUs. This
465 	 * shouldn't do much harm because KVM always enable VMX on all
466 	 * CPUs anyway. But we can miss it on the small window where KVM
467 	 * is still enabling VMX.
468 	 */
469 	if (cpu_has_vmx() && cpu_vmx_enabled()) {
470 		/* Disable VMX on this CPU. */
471 		cpu_vmxoff();
472 
473 		/* Halt and disable VMX on the other CPUs */
474 		nmi_shootdown_cpus(vmxoff_nmi);
475 
476 	}
477 }
478 
479 
480 void __attribute__((weak)) mach_reboot_fixups(void)
481 {
482 }
483 
484 /*
485  * To the best of our knowledge Windows compatible x86 hardware expects
486  * the following on reboot:
487  *
488  * 1) If the FADT has the ACPI reboot register flag set, try it
489  * 2) If still alive, write to the keyboard controller
490  * 3) If still alive, write to the ACPI reboot register again
491  * 4) If still alive, write to the keyboard controller again
492  * 5) If still alive, call the EFI runtime service to reboot
493  * 6) If no EFI runtime service, call the BIOS to do a reboot
494  *
495  * We default to following the same pattern. We also have
496  * two other reboot methods: 'triple fault' and 'PCI', which
497  * can be triggered via the reboot= kernel boot option or
498  * via quirks.
499  *
500  * This means that this function can never return, it can misbehave
501  * by not rebooting properly and hanging.
502  */
503 static void native_machine_emergency_restart(void)
504 {
505 	int i;
506 	int attempt = 0;
507 	int orig_reboot_type = reboot_type;
508 	unsigned short mode;
509 
510 	if (reboot_emergency)
511 		emergency_vmx_disable_all();
512 
513 	tboot_shutdown(TB_SHUTDOWN_REBOOT);
514 
515 	/* Tell the BIOS if we want cold or warm reboot */
516 	mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
517 	*((unsigned short *)__va(0x472)) = mode;
518 
519 	for (;;) {
520 		/* Could also try the reset bit in the Hammer NB */
521 		switch (reboot_type) {
522 		case BOOT_ACPI:
523 			acpi_reboot();
524 			reboot_type = BOOT_KBD;
525 			break;
526 
527 		case BOOT_KBD:
528 			mach_reboot_fixups(); /* For board specific fixups */
529 
530 			for (i = 0; i < 10; i++) {
531 				kb_wait();
532 				udelay(50);
533 				outb(0xfe, 0x64); /* Pulse reset low */
534 				udelay(50);
535 			}
536 			if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
537 				attempt = 1;
538 				reboot_type = BOOT_ACPI;
539 			} else {
540 				reboot_type = BOOT_EFI;
541 			}
542 			break;
543 
544 		case BOOT_EFI:
545 			efi_reboot(reboot_mode, NULL);
546 			reboot_type = BOOT_BIOS;
547 			break;
548 
549 		case BOOT_BIOS:
550 			machine_real_restart(MRR_BIOS);
551 
552 			/* We're probably dead after this, but... */
553 			reboot_type = BOOT_CF9_SAFE;
554 			break;
555 
556 		case BOOT_CF9_FORCE:
557 			port_cf9_safe = true;
558 			/* Fall through */
559 
560 		case BOOT_CF9_SAFE:
561 			if (port_cf9_safe) {
562 				u8 reboot_code = reboot_mode == REBOOT_WARM ?  0x06 : 0x0E;
563 				u8 cf9 = inb(0xcf9) & ~reboot_code;
564 				outb(cf9|2, 0xcf9); /* Request hard reset */
565 				udelay(50);
566 				/* Actually do the reset */
567 				outb(cf9|reboot_code, 0xcf9);
568 				udelay(50);
569 			}
570 			reboot_type = BOOT_TRIPLE;
571 			break;
572 
573 		case BOOT_TRIPLE:
574 			load_idt(&no_idt);
575 			__asm__ __volatile__("int3");
576 
577 			/* We're probably dead after this, but... */
578 			reboot_type = BOOT_KBD;
579 			break;
580 		}
581 	}
582 }
583 
584 void native_machine_shutdown(void)
585 {
586 	/* Stop the cpus and apics */
587 #ifdef CONFIG_X86_IO_APIC
588 	/*
589 	 * Disabling IO APIC before local APIC is a workaround for
590 	 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
591 	 * Specification Update". In this situation, interrupts that target
592 	 * a Logical Processor whose Local APIC is either in the process of
593 	 * being hardware disabled or software disabled are neither delivered
594 	 * nor discarded. When this erratum occurs, the processor may hang.
595 	 *
596 	 * Even without the erratum, it still makes sense to quiet IO APIC
597 	 * before disabling Local APIC.
598 	 */
599 	disable_IO_APIC();
600 #endif
601 
602 #ifdef CONFIG_SMP
603 	/*
604 	 * Stop all of the others. Also disable the local irq to
605 	 * not receive the per-cpu timer interrupt which may trigger
606 	 * scheduler's load balance.
607 	 */
608 	local_irq_disable();
609 	stop_other_cpus();
610 #endif
611 
612 	lapic_shutdown();
613 
614 #ifdef CONFIG_HPET_TIMER
615 	hpet_disable();
616 #endif
617 
618 #ifdef CONFIG_X86_64
619 	x86_platform.iommu_shutdown();
620 #endif
621 }
622 
623 static void __machine_emergency_restart(int emergency)
624 {
625 	reboot_emergency = emergency;
626 	machine_ops.emergency_restart();
627 }
628 
629 static void native_machine_restart(char *__unused)
630 {
631 	pr_notice("machine restart\n");
632 
633 	if (!reboot_force)
634 		machine_shutdown();
635 	__machine_emergency_restart(0);
636 }
637 
638 static void native_machine_halt(void)
639 {
640 	/* Stop other cpus and apics */
641 	machine_shutdown();
642 
643 	tboot_shutdown(TB_SHUTDOWN_HALT);
644 
645 	stop_this_cpu(NULL);
646 }
647 
648 static void native_machine_power_off(void)
649 {
650 	if (pm_power_off) {
651 		if (!reboot_force)
652 			machine_shutdown();
653 		pm_power_off();
654 	}
655 	/* A fallback in case there is no PM info available */
656 	tboot_shutdown(TB_SHUTDOWN_HALT);
657 }
658 
659 struct machine_ops machine_ops = {
660 	.power_off = native_machine_power_off,
661 	.shutdown = native_machine_shutdown,
662 	.emergency_restart = native_machine_emergency_restart,
663 	.restart = native_machine_restart,
664 	.halt = native_machine_halt,
665 #ifdef CONFIG_KEXEC
666 	.crash_shutdown = native_machine_crash_shutdown,
667 #endif
668 };
669 
670 void machine_power_off(void)
671 {
672 	machine_ops.power_off();
673 }
674 
675 void machine_shutdown(void)
676 {
677 	machine_ops.shutdown();
678 }
679 
680 void machine_emergency_restart(void)
681 {
682 	__machine_emergency_restart(1);
683 }
684 
685 void machine_restart(char *cmd)
686 {
687 	machine_ops.restart(cmd);
688 }
689 
690 void machine_halt(void)
691 {
692 	machine_ops.halt();
693 }
694 
695 #ifdef CONFIG_KEXEC
696 void machine_crash_shutdown(struct pt_regs *regs)
697 {
698 	machine_ops.crash_shutdown(regs);
699 }
700 #endif
701 
702 
703 #if defined(CONFIG_SMP)
704 
705 /* This keeps a track of which one is crashing cpu. */
706 static int crashing_cpu;
707 static nmi_shootdown_cb shootdown_callback;
708 
709 static atomic_t waiting_for_crash_ipi;
710 
711 static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
712 {
713 	int cpu;
714 
715 	cpu = raw_smp_processor_id();
716 
717 	/*
718 	 * Don't do anything if this handler is invoked on crashing cpu.
719 	 * Otherwise, system will completely hang. Crashing cpu can get
720 	 * an NMI if system was initially booted with nmi_watchdog parameter.
721 	 */
722 	if (cpu == crashing_cpu)
723 		return NMI_HANDLED;
724 	local_irq_disable();
725 
726 	shootdown_callback(cpu, regs);
727 
728 	atomic_dec(&waiting_for_crash_ipi);
729 	/* Assume hlt works */
730 	halt();
731 	for (;;)
732 		cpu_relax();
733 
734 	return NMI_HANDLED;
735 }
736 
737 static void smp_send_nmi_allbutself(void)
738 {
739 	apic->send_IPI_allbutself(NMI_VECTOR);
740 }
741 
742 /*
743  * Halt all other CPUs, calling the specified function on each of them
744  *
745  * This function can be used to halt all other CPUs on crash
746  * or emergency reboot time. The function passed as parameter
747  * will be called inside a NMI handler on all CPUs.
748  */
749 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
750 {
751 	unsigned long msecs;
752 	local_irq_disable();
753 
754 	/* Make a note of crashing cpu. Will be used in NMI callback. */
755 	crashing_cpu = safe_smp_processor_id();
756 
757 	shootdown_callback = callback;
758 
759 	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
760 	/* Would it be better to replace the trap vector here? */
761 	if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
762 				 NMI_FLAG_FIRST, "crash"))
763 		return;		/* Return what? */
764 	/*
765 	 * Ensure the new callback function is set before sending
766 	 * out the NMI
767 	 */
768 	wmb();
769 
770 	smp_send_nmi_allbutself();
771 
772 	msecs = 1000; /* Wait at most a second for the other cpus to stop */
773 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
774 		mdelay(1);
775 		msecs--;
776 	}
777 
778 	/* Leave the nmi callback set */
779 }
780 #else /* !CONFIG_SMP */
781 void nmi_shootdown_cpus(nmi_shootdown_cb callback)
782 {
783 	/* No other CPUs to shoot down */
784 }
785 #endif
786