xref: /linux/arch/x86/kernel/reboot.c (revision 4d581259b7d44c8120a614b4e9244094c824d51f)
1c767a54bSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2c767a54bSJoe Perches 
34d022e35SMiguel Boton #include <linux/module.h>
44d022e35SMiguel Boton #include <linux/reboot.h>
54d022e35SMiguel Boton #include <linux/init.h>
64d022e35SMiguel Boton #include <linux/pm.h>
74d022e35SMiguel Boton #include <linux/efi.h>
86c6c51e4SPaul Mackerras #include <linux/dmi.h>
9d43c36dcSAlexey Dobriyan #include <linux/sched.h>
1069575d38SShane Wang #include <linux/tboot.h>
11ca444564SJean Delvare #include <linux/delay.h>
124d022e35SMiguel Boton #include <acpi/reboot.h>
134d022e35SMiguel Boton #include <asm/io.h>
144d022e35SMiguel Boton #include <asm/apic.h>
158643e28dSJiang Liu #include <asm/io_apic.h>
164d022e35SMiguel Boton #include <asm/desc.h>
174d022e35SMiguel Boton #include <asm/hpet.h>
1868db065cSJeremy Fitzhardinge #include <asm/pgtable.h>
194412620fSDmitri Vorobiev #include <asm/proto.h>
204d022e35SMiguel Boton #include <asm/reboot_fixups.h>
214d022e35SMiguel Boton #include <asm/reboot.h>
2282487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
23d176720dSEduardo Habkost #include <asm/virtext.h>
2496b89dc6SJaswinder Singh Rajput #include <asm/cpu.h>
25c410b830SDon Zickus #include <asm/nmi.h>
2665051397SH. Peter Anvin #include <asm/smp.h>
274d022e35SMiguel Boton 
284d022e35SMiguel Boton #include <linux/ctype.h>
294d022e35SMiguel Boton #include <linux/mc146818rtc.h>
305a8c9aebSJarkko Sakkinen #include <asm/realmode.h>
31338bac52SFUJITA Tomonori #include <asm/x86_init.h>
3244be28e9SMatt Fleming #include <asm/efi.h>
334d022e35SMiguel Boton 
344d022e35SMiguel Boton /*
354d022e35SMiguel Boton  * Power off function, if any
364d022e35SMiguel Boton  */
374d022e35SMiguel Boton void (*pm_power_off)(void);
384d022e35SMiguel Boton EXPORT_SYMBOL(pm_power_off);
394d022e35SMiguel Boton 
40ebdd561aSJan Beulich static const struct desc_ptr no_idt = {};
414d022e35SMiguel Boton 
42144d102bSMichael D Labriola /*
43144d102bSMichael D Labriola  * This is set if we need to go through the 'emergency' path.
44d176720dSEduardo Habkost  * When machine_emergency_restart() is called, we may be on
45d176720dSEduardo Habkost  * an inconsistent state and won't be able to do a clean cleanup
46d176720dSEduardo Habkost  */
47d176720dSEduardo Habkost static int reboot_emergency;
48d176720dSEduardo Habkost 
4914d7ca5cSH. Peter Anvin /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
5014d7ca5cSH. Peter Anvin bool port_cf9_safe = false;
5114d7ca5cSH. Peter Anvin 
52144d102bSMichael D Labriola /*
534d022e35SMiguel Boton  * Reboot options and system auto-detection code provided by
544d022e35SMiguel Boton  * Dell Inc. so their systems "just work". :-)
554d022e35SMiguel Boton  */
564d022e35SMiguel Boton 
574d022e35SMiguel Boton /*
58*4d581259SAlex Hung  * Some machines require the "reboot=a" commandline options
59*4d581259SAlex Hung  */
60*4d581259SAlex Hung static int __init set_acpi_reboot(const struct dmi_system_id *d)
61*4d581259SAlex Hung {
62*4d581259SAlex Hung 	if (reboot_type != BOOT_ACPI) {
63*4d581259SAlex Hung 		reboot_type = BOOT_ACPI;
64*4d581259SAlex Hung 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
65*4d581259SAlex Hung 			d->ident, "ACPI");
66*4d581259SAlex Hung 	}
67*4d581259SAlex Hung 	return 0;
68*4d581259SAlex Hung }
69*4d581259SAlex Hung 
70*4d581259SAlex Hung /*
711ef03890SPeter Chubb  * Some machines require the "reboot=b" or "reboot=k"  commandline options,
724d022e35SMiguel Boton  * this quirk makes that automatic.
734d022e35SMiguel Boton  */
744d022e35SMiguel Boton static int __init set_bios_reboot(const struct dmi_system_id *d)
754d022e35SMiguel Boton {
764d022e35SMiguel Boton 	if (reboot_type != BOOT_BIOS) {
774d022e35SMiguel Boton 		reboot_type = BOOT_BIOS;
78c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
796d9153bbSLan Tianyu 			d->ident, "BIOS");
804d022e35SMiguel Boton 	}
814d022e35SMiguel Boton 	return 0;
824d022e35SMiguel Boton }
834d022e35SMiguel Boton 
8465051397SH. Peter Anvin void __noreturn machine_real_restart(unsigned int type)
8557b16594SMichael D Labriola {
8657b16594SMichael D Labriola 	local_irq_disable();
8757b16594SMichael D Labriola 
88144d102bSMichael D Labriola 	/*
89144d102bSMichael D Labriola 	 * Write zero to CMOS register number 0x0f, which the BIOS POST
90144d102bSMichael D Labriola 	 * routine will recognize as telling it to do a proper reboot.  (Well
91144d102bSMichael D Labriola 	 * that's what this book in front of me says -- it may only apply to
92144d102bSMichael D Labriola 	 * the Phoenix BIOS though, it's not clear).  At the same time,
93144d102bSMichael D Labriola 	 * disable NMIs by setting the top bit in the CMOS address register,
94144d102bSMichael D Labriola 	 * as we're about to do peculiar things to the CPU.  I'm not sure if
95144d102bSMichael D Labriola 	 * `outb_p' is needed instead of just `outb'.  Use it to be on the
96144d102bSMichael D Labriola 	 * safe side.  (Yes, CMOS_WRITE does outb_p's. -  Paul G.)
9757b16594SMichael D Labriola 	 */
9857b16594SMichael D Labriola 	spin_lock(&rtc_lock);
9957b16594SMichael D Labriola 	CMOS_WRITE(0x00, 0x8f);
10057b16594SMichael D Labriola 	spin_unlock(&rtc_lock);
10157b16594SMichael D Labriola 
10257b16594SMichael D Labriola 	/*
10357b16594SMichael D Labriola 	 * Switch back to the initial page table.
10457b16594SMichael D Labriola 	 */
10565051397SH. Peter Anvin #ifdef CONFIG_X86_32
10657b16594SMichael D Labriola 	load_cr3(initial_page_table);
10765051397SH. Peter Anvin #else
10865051397SH. Peter Anvin 	write_cr3(real_mode_header->trampoline_pgd);
10965051397SH. Peter Anvin #endif
11057b16594SMichael D Labriola 
11157b16594SMichael D Labriola 	/* Jump to the identity-mapped low memory code */
11265051397SH. Peter Anvin #ifdef CONFIG_X86_32
11365051397SH. Peter Anvin 	asm volatile("jmpl *%0" : :
11465051397SH. Peter Anvin 		     "rm" (real_mode_header->machine_real_restart_asm),
11565051397SH. Peter Anvin 		     "a" (type));
11665051397SH. Peter Anvin #else
11765051397SH. Peter Anvin 	asm volatile("ljmpl *%0" : :
11865051397SH. Peter Anvin 		     "m" (real_mode_header->machine_real_restart_asm),
11965051397SH. Peter Anvin 		     "D" (type));
12065051397SH. Peter Anvin #endif
12165051397SH. Peter Anvin 	unreachable();
12257b16594SMichael D Labriola }
12357b16594SMichael D Labriola #ifdef CONFIG_APM_MODULE
12457b16594SMichael D Labriola EXPORT_SYMBOL(machine_real_restart);
12557b16594SMichael D Labriola #endif
12657b16594SMichael D Labriola 
12757b16594SMichael D Labriola /*
12857b16594SMichael D Labriola  * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
12957b16594SMichael D Labriola  */
13057b16594SMichael D Labriola static int __init set_pci_reboot(const struct dmi_system_id *d)
13157b16594SMichael D Labriola {
1325be44a6fSIngo Molnar 	if (reboot_type != BOOT_CF9_FORCE) {
1335be44a6fSIngo Molnar 		reboot_type = BOOT_CF9_FORCE;
134c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
1356d9153bbSLan Tianyu 			d->ident, "PCI");
13657b16594SMichael D Labriola 	}
13757b16594SMichael D Labriola 	return 0;
13857b16594SMichael D Labriola }
13957b16594SMichael D Labriola 
1401ef03890SPeter Chubb static int __init set_kbd_reboot(const struct dmi_system_id *d)
1411ef03890SPeter Chubb {
1421ef03890SPeter Chubb 	if (reboot_type != BOOT_KBD) {
1431ef03890SPeter Chubb 		reboot_type = BOOT_KBD;
144c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboot.\n",
1456d9153bbSLan Tianyu 			d->ident, "KBD");
1461ef03890SPeter Chubb 	}
1471ef03890SPeter Chubb 	return 0;
1481ef03890SPeter Chubb }
1491ef03890SPeter Chubb 
150144d102bSMichael D Labriola /*
15165051397SH. Peter Anvin  * This is a single dmi_table handling all reboot quirks.
15257b16594SMichael D Labriola  */
1534d022e35SMiguel Boton static struct dmi_system_id __initdata reboot_dmi_table[] = {
15457b16594SMichael D Labriola 
155e56e57f6SDave Jones 	/* Acer */
156b49c78d4SPeter Chubb 	{	/* Handle reboot issue on Acer Aspire one */
1571ef03890SPeter Chubb 		.callback = set_kbd_reboot,
158b49c78d4SPeter Chubb 		.ident = "Acer Aspire One A110",
159b49c78d4SPeter Chubb 		.matches = {
160b49c78d4SPeter Chubb 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
161b49c78d4SPeter Chubb 			DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
162b49c78d4SPeter Chubb 		},
163b49c78d4SPeter Chubb 	},
164e56e57f6SDave Jones 
165e56e57f6SDave Jones 	/* Apple */
1663e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBook5 */
1676c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
1683e03bbeaSShunichi Fuji 		.ident = "Apple MacBook5",
1696c6c51e4SPaul Mackerras 		.matches = {
1706c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1713e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
1726c6c51e4SPaul Mackerras 		},
1736c6c51e4SPaul Mackerras 	},
1743e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBookPro5 */
175498cdbfbSOzan Çağlayan 		.callback = set_pci_reboot,
1763e03bbeaSShunichi Fuji 		.ident = "Apple MacBookPro5",
177498cdbfbSOzan Çağlayan 		.matches = {
178498cdbfbSOzan Çağlayan 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1793e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
180498cdbfbSOzan Çağlayan 		},
181498cdbfbSOzan Çağlayan 	},
18205154752SGottfried Haider 	{	/* Handle problems with rebooting on Apple Macmini3,1 */
18305154752SGottfried Haider 		.callback = set_pci_reboot,
18405154752SGottfried Haider 		.ident = "Apple Macmini3,1",
18505154752SGottfried Haider 		.matches = {
18605154752SGottfried Haider 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
18705154752SGottfried Haider 			DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
18805154752SGottfried Haider 		},
18905154752SGottfried Haider 	},
1900a832320SJustin P. Mattock 	{	/* Handle problems with rebooting on the iMac9,1. */
1910a832320SJustin P. Mattock 		.callback = set_pci_reboot,
1920a832320SJustin P. Mattock 		.ident = "Apple iMac9,1",
1930a832320SJustin P. Mattock 		.matches = {
1940a832320SJustin P. Mattock 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1950a832320SJustin P. Mattock 			DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
1960a832320SJustin P. Mattock 		},
1970a832320SJustin P. Mattock 	},
1982f0c0b2dSMario Kleiner 	{	/* Handle problems with rebooting on the iMac10,1. */
1992f0c0b2dSMario Kleiner 		.callback = set_pci_reboot,
2002f0c0b2dSMario Kleiner 		.ident = "Apple iMac10,1",
2012f0c0b2dSMario Kleiner 		.matches = {
2022f0c0b2dSMario Kleiner 		    DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
2032f0c0b2dSMario Kleiner 		    DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
2042f0c0b2dSMario Kleiner 		},
2052f0c0b2dSMario Kleiner 	},
206e56e57f6SDave Jones 
20780313b30SStefan Lippers-Hollmann 	/* ASRock */
20880313b30SStefan Lippers-Hollmann 	{	/* Handle problems with rebooting on ASRock Q1900DC-ITX */
20980313b30SStefan Lippers-Hollmann 		.callback = set_pci_reboot,
21080313b30SStefan Lippers-Hollmann 		.ident = "ASRock Q1900DC-ITX",
21180313b30SStefan Lippers-Hollmann 		.matches = {
21280313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
21380313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
21480313b30SStefan Lippers-Hollmann 		},
21580313b30SStefan Lippers-Hollmann 	},
21680313b30SStefan Lippers-Hollmann 
217e56e57f6SDave Jones 	/* ASUS */
218e56e57f6SDave Jones 	{	/* Handle problems with rebooting on ASUS P4S800 */
219e56e57f6SDave Jones 		.callback = set_bios_reboot,
220e56e57f6SDave Jones 		.ident = "ASUS P4S800",
221e56e57f6SDave Jones 		.matches = {
222e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
223e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
224e56e57f6SDave Jones 		},
225e56e57f6SDave Jones 	},
226e56e57f6SDave Jones 
227aadca6faSChristian Gmeiner 	/* Certec */
228aadca6faSChristian Gmeiner 	{       /* Handle problems with rebooting on Certec BPC600 */
229aadca6faSChristian Gmeiner 		.callback = set_pci_reboot,
230aadca6faSChristian Gmeiner 		.ident = "Certec BPC600",
231aadca6faSChristian Gmeiner 		.matches = {
232aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
233aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
234aadca6faSChristian Gmeiner 		},
235aadca6faSChristian Gmeiner 	},
236aadca6faSChristian Gmeiner 
237e56e57f6SDave Jones 	/* Dell */
238e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell DXP061 */
239e56e57f6SDave Jones 		.callback = set_bios_reboot,
240e56e57f6SDave Jones 		.ident = "Dell DXP061",
2413628c3f5SMaxime Ripard 		.matches = {
2423628c3f5SMaxime Ripard 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
243e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
244e56e57f6SDave Jones 		},
245e56e57f6SDave Jones 	},
246e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell E520's */
247e56e57f6SDave Jones 		.callback = set_bios_reboot,
248e56e57f6SDave Jones 		.ident = "Dell E520",
249e56e57f6SDave Jones 		.matches = {
250e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
251e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
2523628c3f5SMaxime Ripard 		},
2533628c3f5SMaxime Ripard 	},
2548412da75SVille Syrjälä 	{	/* Handle problems with rebooting on the Latitude E5410. */
2558412da75SVille Syrjälä 		.callback = set_pci_reboot,
2568412da75SVille Syrjälä 		.ident = "Dell Latitude E5410",
2578412da75SVille Syrjälä 		.matches = {
2588412da75SVille Syrjälä 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
2598412da75SVille Syrjälä 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
2608412da75SVille Syrjälä 		},
2618412da75SVille Syrjälä 	},
262b7798d28SDaniel J Blueman 	{	/* Handle problems with rebooting on the Latitude E5420. */
263b7798d28SDaniel J Blueman 		.callback = set_pci_reboot,
264b7798d28SDaniel J Blueman 		.ident = "Dell Latitude E5420",
265b7798d28SDaniel J Blueman 		.matches = {
266b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
267b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
268b7798d28SDaniel J Blueman 		},
269b7798d28SDaniel J Blueman 	},
270e56e57f6SDave Jones 	{	/* Handle problems with rebooting on the Latitude E6320. */
271e56e57f6SDave Jones 		.callback = set_pci_reboot,
272e56e57f6SDave Jones 		.ident = "Dell Latitude E6320",
273e56e57f6SDave Jones 		.matches = {
274e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
275e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
276e56e57f6SDave Jones 		},
277e56e57f6SDave Jones 	},
278a536877eSH. Peter Anvin 	{	/* Handle problems with rebooting on the Latitude E6420. */
279a536877eSH. Peter Anvin 		.callback = set_pci_reboot,
280a536877eSH. Peter Anvin 		.ident = "Dell Latitude E6420",
281a536877eSH. Peter Anvin 		.matches = {
282a536877eSH. Peter Anvin 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
283a536877eSH. Peter Anvin 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
284a536877eSH. Peter Anvin 		},
285a536877eSH. Peter Anvin 	},
286e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
287e56e57f6SDave Jones 		.callback = set_bios_reboot,
288e56e57f6SDave Jones 		.ident = "Dell OptiPlex 330",
289e56e57f6SDave Jones 		.matches = {
290e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
291e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
292e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
293e56e57f6SDave Jones 		},
294e56e57f6SDave Jones 	},
295e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
296e56e57f6SDave Jones 		.callback = set_bios_reboot,
297e56e57f6SDave Jones 		.ident = "Dell OptiPlex 360",
298e56e57f6SDave Jones 		.matches = {
299e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
300e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
301e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
302e56e57f6SDave Jones 		},
303e56e57f6SDave Jones 	},
304e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's SFF */
305e56e57f6SDave Jones 		.callback = set_bios_reboot,
306e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
307e56e57f6SDave Jones 		.matches = {
308e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
309e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
310e56e57f6SDave Jones 		},
311e56e57f6SDave Jones 	},
312e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's DFF */
313e56e57f6SDave Jones 		.callback = set_bios_reboot,
314e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
315e56e57f6SDave Jones 		.matches = {
316e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
317e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
318e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
319e56e57f6SDave Jones 		},
320e56e57f6SDave Jones 	},
321e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
322e56e57f6SDave Jones 		.callback = set_bios_reboot,
323e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
324e56e57f6SDave Jones 		.matches = {
325e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
326e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
327e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
328e56e57f6SDave Jones 		},
329e56e57f6SDave Jones 	},
330e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
331e56e57f6SDave Jones 		.callback = set_bios_reboot,
332e56e57f6SDave Jones 		.ident = "Dell OptiPlex 760",
333e56e57f6SDave Jones 		.matches = {
334e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
335e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
336e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
337e56e57f6SDave Jones 		},
338e56e57f6SDave Jones 	},
3396be30bb7SRafael J. Wysocki 	{	/* Handle problems with rebooting on the OptiPlex 990. */
3406be30bb7SRafael J. Wysocki 		.callback = set_pci_reboot,
3416be30bb7SRafael J. Wysocki 		.ident = "Dell OptiPlex 990",
3426be30bb7SRafael J. Wysocki 		.matches = {
3436be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3446be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
3456be30bb7SRafael J. Wysocki 		},
3466be30bb7SRafael J. Wysocki 	},
347e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 300's */
348e56e57f6SDave Jones 		.callback = set_bios_reboot,
349e56e57f6SDave Jones 		.ident = "Dell PowerEdge 300",
35076eb9a30SZhang Rui 		.matches = {
351e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
352e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
353e56e57f6SDave Jones 		},
354e56e57f6SDave Jones 	},
355e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 1300's */
356e56e57f6SDave Jones 		.callback = set_bios_reboot,
357e56e57f6SDave Jones 		.ident = "Dell PowerEdge 1300",
358e56e57f6SDave Jones 		.matches = {
359e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
360e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
361e56e57f6SDave Jones 		},
362e56e57f6SDave Jones 	},
363e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 2400's */
364e56e57f6SDave Jones 		.callback = set_bios_reboot,
365e56e57f6SDave Jones 		.ident = "Dell PowerEdge 2400",
366e56e57f6SDave Jones 		.matches = {
367e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
368e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
36976eb9a30SZhang Rui 		},
37076eb9a30SZhang Rui 	},
3714f0acd31SMasoud Sharbiani 	{	/* Handle problems with rebooting on the Dell PowerEdge C6100. */
3724f0acd31SMasoud Sharbiani 		.callback = set_pci_reboot,
3734f0acd31SMasoud Sharbiani 		.ident = "Dell PowerEdge C6100",
3744f0acd31SMasoud Sharbiani 		.matches = {
3754f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
3764f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
3774f0acd31SMasoud Sharbiani 		},
3784f0acd31SMasoud Sharbiani 	},
3796c6c51e4SPaul Mackerras 	{	/* Handle problems with rebooting on the Precision M6600. */
3806c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
3816c6c51e4SPaul Mackerras 		.ident = "Dell Precision M6600",
3826c6c51e4SPaul Mackerras 		.matches = {
3836c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3846c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
3856c6c51e4SPaul Mackerras 		},
3866c6c51e4SPaul Mackerras 	},
387e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T5400's */
388e56e57f6SDave Jones 		.callback = set_bios_reboot,
389e56e57f6SDave Jones 		.ident = "Dell Precision T5400",
39057b16594SMichael D Labriola 		.matches = {
3916c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
392e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
393144d102bSMichael D Labriola 		},
3945955633eSMichael D Labriola 	},
395e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T7400's */
396e56e57f6SDave Jones 		.callback = set_bios_reboot,
397e56e57f6SDave Jones 		.ident = "Dell Precision T7400",
3986c6c51e4SPaul Mackerras 		.matches = {
399e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
400e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
4016c6c51e4SPaul Mackerras 		},
4024d022e35SMiguel Boton 	},
403e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell XPS710 */
404e56e57f6SDave Jones 		.callback = set_bios_reboot,
405e56e57f6SDave Jones 		.ident = "Dell XPS710",
406e56e57f6SDave Jones 		.matches = {
407e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
408e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
409e56e57f6SDave Jones 		},
410e56e57f6SDave Jones 	},
411*4d581259SAlex Hung 	{	/* Handle problems with rebooting on Dell Optiplex 7450 AIO */
412*4d581259SAlex Hung 		.callback = set_acpi_reboot,
413*4d581259SAlex Hung 		.ident = "Dell OptiPlex 7450 AIO",
414*4d581259SAlex Hung 		.matches = {
415*4d581259SAlex Hung 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
416*4d581259SAlex Hung 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7450 AIO"),
417*4d581259SAlex Hung 		},
418*4d581259SAlex Hung 	},
419e56e57f6SDave Jones 
420e56e57f6SDave Jones 	/* Hewlett-Packard */
421e56e57f6SDave Jones 	{	/* Handle problems with rebooting on HP laptops */
422e56e57f6SDave Jones 		.callback = set_bios_reboot,
423e56e57f6SDave Jones 		.ident = "HP Compaq Laptop",
424e56e57f6SDave Jones 		.matches = {
425e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
426e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
427e56e57f6SDave Jones 		},
428e56e57f6SDave Jones 	},
429e56e57f6SDave Jones 
430e56e57f6SDave Jones 	/* Sony */
431e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Sony VGN-Z540N */
432e56e57f6SDave Jones 		.callback = set_bios_reboot,
433e56e57f6SDave Jones 		.ident = "Sony VGN-Z540N",
434e56e57f6SDave Jones 		.matches = {
435e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
436e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
437e56e57f6SDave Jones 		},
438e56e57f6SDave Jones 	},
439e56e57f6SDave Jones 
4404d022e35SMiguel Boton 	{ }
4414d022e35SMiguel Boton };
4429c48f1c6SDon Zickus 
443d176720dSEduardo Habkost static int __init reboot_init(void)
444d176720dSEduardo Habkost {
44544be28e9SMatt Fleming 	int rv;
44644be28e9SMatt Fleming 
447d176720dSEduardo Habkost 	/*
448d176720dSEduardo Habkost 	 * Only do the DMI check if reboot_type hasn't been overridden
449d176720dSEduardo Habkost 	 * on the command line
450d176720dSEduardo Habkost 	 */
45144be28e9SMatt Fleming 	if (!reboot_default)
45244be28e9SMatt Fleming 		return 0;
45344be28e9SMatt Fleming 
45444be28e9SMatt Fleming 	/*
45544be28e9SMatt Fleming 	 * The DMI quirks table takes precedence. If no quirks entry
45644be28e9SMatt Fleming 	 * matches and the ACPI Hardware Reduced bit is set, force EFI
45744be28e9SMatt Fleming 	 * reboot.
45844be28e9SMatt Fleming 	 */
45944be28e9SMatt Fleming 	rv = dmi_check_system(reboot_dmi_table);
46044be28e9SMatt Fleming 
46144be28e9SMatt Fleming 	if (!rv && efi_reboot_required())
46244be28e9SMatt Fleming 		reboot_type = BOOT_EFI;
46344be28e9SMatt Fleming 
464d176720dSEduardo Habkost 	return 0;
465d176720dSEduardo Habkost }
466d176720dSEduardo Habkost core_initcall(reboot_init);
467d176720dSEduardo Habkost 
468d176720dSEduardo Habkost static inline void kb_wait(void)
469d176720dSEduardo Habkost {
470d176720dSEduardo Habkost 	int i;
471d176720dSEduardo Habkost 
472d176720dSEduardo Habkost 	for (i = 0; i < 0x10000; i++) {
4737432d149SIngo Molnar 		if ((inb(0x64) & 0x02) == 0)
4747432d149SIngo Molnar 			break;
4757432d149SIngo Molnar 		udelay(2);
4767432d149SIngo Molnar 	}
477416e2d63SJody Belka }
4784d022e35SMiguel Boton 
4794d022e35SMiguel Boton static void vmxoff_nmi(int cpu, struct pt_regs *regs)
4804d022e35SMiguel Boton {
481d176720dSEduardo Habkost 	cpu_emergency_vmxoff();
482d176720dSEduardo Habkost }
483d176720dSEduardo Habkost 
484144d102bSMichael D Labriola /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
4854d022e35SMiguel Boton static void emergency_vmx_disable_all(void)
4864d022e35SMiguel Boton {
4874d022e35SMiguel Boton 	/* Just make sure we won't change CPUs while doing this */
4884d022e35SMiguel Boton 	local_irq_disable();
4894d022e35SMiguel Boton 
490144d102bSMichael D Labriola 	/*
491144d102bSMichael D Labriola 	 * We need to disable VMX on all CPUs before rebooting, otherwise
4924d022e35SMiguel Boton 	 * we risk hanging up the machine, because the CPU ignore INIT
4934d022e35SMiguel Boton 	 * signals when VMX is enabled.
4944d022e35SMiguel Boton 	 *
4954d022e35SMiguel Boton 	 * We can't take any locks and we may be on an inconsistent
4964d022e35SMiguel Boton 	 * state, so we use NMIs as IPIs to tell the other CPUs to disable
4974d022e35SMiguel Boton 	 * VMX and halt.
4984d022e35SMiguel Boton 	 *
4994d022e35SMiguel Boton 	 * For safety, we will avoid running the nmi_shootdown_cpus()
5004d022e35SMiguel Boton 	 * stuff unnecessarily, but we don't have a way to check
5014d022e35SMiguel Boton 	 * if other CPUs have VMX enabled. So we will call it only if the
5024d022e35SMiguel Boton 	 * CPU we are running on has VMX enabled.
5034d022e35SMiguel Boton 	 *
5044d022e35SMiguel Boton 	 * We will miss cases where VMX is not enabled on all CPUs. This
5054d022e35SMiguel Boton 	 * shouldn't do much harm because KVM always enable VMX on all
5064d022e35SMiguel Boton 	 * CPUs anyway. But we can miss it on the small window where KVM
5074d022e35SMiguel Boton 	 * is still enabling VMX.
5084d022e35SMiguel Boton 	 */
5094d022e35SMiguel Boton 	if (cpu_has_vmx() && cpu_vmx_enabled()) {
510144d102bSMichael D Labriola 		/* Disable VMX on this CPU. */
5114d022e35SMiguel Boton 		cpu_vmxoff();
5124d022e35SMiguel Boton 
5134d022e35SMiguel Boton 		/* Halt and disable VMX on the other CPUs */
5144d022e35SMiguel Boton 		nmi_shootdown_cpus(vmxoff_nmi);
5154d022e35SMiguel Boton 
5164d022e35SMiguel Boton 	}
5174d022e35SMiguel Boton }
5184d022e35SMiguel Boton 
5194d022e35SMiguel Boton 
5204d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void)
5214d022e35SMiguel Boton {
5224d022e35SMiguel Boton }
5234d022e35SMiguel Boton 
524660e34ceSMatthew Garrett /*
5255be44a6fSIngo Molnar  * To the best of our knowledge Windows compatible x86 hardware expects
5265be44a6fSIngo Molnar  * the following on reboot:
527660e34ceSMatthew Garrett  *
528660e34ceSMatthew Garrett  * 1) If the FADT has the ACPI reboot register flag set, try it
529660e34ceSMatthew Garrett  * 2) If still alive, write to the keyboard controller
530660e34ceSMatthew Garrett  * 3) If still alive, write to the ACPI reboot register again
531660e34ceSMatthew Garrett  * 4) If still alive, write to the keyboard controller again
532a4f1987eSLi, Aubrey  * 5) If still alive, call the EFI runtime service to reboot
5335be44a6fSIngo Molnar  * 6) If no EFI runtime service, call the BIOS to do a reboot
534660e34ceSMatthew Garrett  *
5355be44a6fSIngo Molnar  * We default to following the same pattern. We also have
5365be44a6fSIngo Molnar  * two other reboot methods: 'triple fault' and 'PCI', which
5375be44a6fSIngo Molnar  * can be triggered via the reboot= kernel boot option or
5385be44a6fSIngo Molnar  * via quirks.
5395be44a6fSIngo Molnar  *
5405be44a6fSIngo Molnar  * This means that this function can never return, it can misbehave
5415be44a6fSIngo Molnar  * by not rebooting properly and hanging.
542660e34ceSMatthew Garrett  */
5434d022e35SMiguel Boton static void native_machine_emergency_restart(void)
5444d022e35SMiguel Boton {
5454d022e35SMiguel Boton 	int i;
546660e34ceSMatthew Garrett 	int attempt = 0;
547660e34ceSMatthew Garrett 	int orig_reboot_type = reboot_type;
548edf2b139SRobin Holt 	unsigned short mode;
5494d022e35SMiguel Boton 
5504d022e35SMiguel Boton 	if (reboot_emergency)
5514d022e35SMiguel Boton 		emergency_vmx_disable_all();
5524d022e35SMiguel Boton 
553840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_REBOOT);
554840c2bafSJoseph Cihula 
5554d022e35SMiguel Boton 	/* Tell the BIOS if we want cold or warm reboot */
556edf2b139SRobin Holt 	mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
557edf2b139SRobin Holt 	*((unsigned short *)__va(0x472)) = mode;
5584d022e35SMiguel Boton 
55987615a34SMatt Fleming 	/*
56087615a34SMatt Fleming 	 * If an EFI capsule has been registered with the firmware then
56187615a34SMatt Fleming 	 * override the reboot= parameter.
56287615a34SMatt Fleming 	 */
56387615a34SMatt Fleming 	if (efi_capsule_pending(NULL)) {
56487615a34SMatt Fleming 		pr_info("EFI capsule is pending, forcing EFI reboot.\n");
56587615a34SMatt Fleming 		reboot_type = BOOT_EFI;
56687615a34SMatt Fleming 	}
56787615a34SMatt Fleming 
5684d022e35SMiguel Boton 	for (;;) {
5694d022e35SMiguel Boton 		/* Could also try the reset bit in the Hammer NB */
5704d022e35SMiguel Boton 		switch (reboot_type) {
5715be44a6fSIngo Molnar 		case BOOT_ACPI:
5725be44a6fSIngo Molnar 			acpi_reboot();
5735be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
5745be44a6fSIngo Molnar 			break;
5755be44a6fSIngo Molnar 
5764d022e35SMiguel Boton 		case BOOT_KBD:
577144d102bSMichael D Labriola 			mach_reboot_fixups(); /* For board specific fixups */
5787432d149SIngo Molnar 
5794d022e35SMiguel Boton 			for (i = 0; i < 10; i++) {
5804d022e35SMiguel Boton 				kb_wait();
5814d022e35SMiguel Boton 				udelay(50);
582144d102bSMichael D Labriola 				outb(0xfe, 0x64); /* Pulse reset low */
5834d022e35SMiguel Boton 				udelay(50);
5844d022e35SMiguel Boton 			}
585660e34ceSMatthew Garrett 			if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
586660e34ceSMatthew Garrett 				attempt = 1;
587660e34ceSMatthew Garrett 				reboot_type = BOOT_ACPI;
588660e34ceSMatthew Garrett 			} else {
589a4f1987eSLi, Aubrey 				reboot_type = BOOT_EFI;
590660e34ceSMatthew Garrett 			}
591660e34ceSMatthew Garrett 			break;
5924d022e35SMiguel Boton 
5934d022e35SMiguel Boton 		case BOOT_EFI:
5948562c99cSMatt Fleming 			efi_reboot(reboot_mode, NULL);
5955be44a6fSIngo Molnar 			reboot_type = BOOT_BIOS;
59614d7ca5cSH. Peter Anvin 			break;
5974d022e35SMiguel Boton 
5985be44a6fSIngo Molnar 		case BOOT_BIOS:
5995be44a6fSIngo Molnar 			machine_real_restart(MRR_BIOS);
6005be44a6fSIngo Molnar 
6015be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6025be44a6fSIngo Molnar 			reboot_type = BOOT_CF9_SAFE;
6035be44a6fSIngo Molnar 			break;
6045be44a6fSIngo Molnar 
6055be44a6fSIngo Molnar 		case BOOT_CF9_FORCE:
60614d7ca5cSH. Peter Anvin 			port_cf9_safe = true;
607144d102bSMichael D Labriola 			/* Fall through */
60814d7ca5cSH. Peter Anvin 
6095be44a6fSIngo Molnar 		case BOOT_CF9_SAFE:
61014d7ca5cSH. Peter Anvin 			if (port_cf9_safe) {
6115be44a6fSIngo Molnar 				u8 reboot_code = reboot_mode == REBOOT_WARM ?  0x06 : 0x0E;
61216c21ae5SLi Fei 				u8 cf9 = inb(0xcf9) & ~reboot_code;
61314d7ca5cSH. Peter Anvin 				outb(cf9|2, 0xcf9); /* Request hard reset */
61414d7ca5cSH. Peter Anvin 				udelay(50);
61516c21ae5SLi Fei 				/* Actually do the reset */
61616c21ae5SLi Fei 				outb(cf9|reboot_code, 0xcf9);
61714d7ca5cSH. Peter Anvin 				udelay(50);
61814d7ca5cSH. Peter Anvin 			}
6195be44a6fSIngo Molnar 			reboot_type = BOOT_TRIPLE;
6205be44a6fSIngo Molnar 			break;
6215be44a6fSIngo Molnar 
6225be44a6fSIngo Molnar 		case BOOT_TRIPLE:
6235be44a6fSIngo Molnar 			load_idt(&no_idt);
6245be44a6fSIngo Molnar 			__asm__ __volatile__("int3");
6255be44a6fSIngo Molnar 
6265be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
6275be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
6284d022e35SMiguel Boton 			break;
6294d022e35SMiguel Boton 		}
6304d022e35SMiguel Boton 	}
6314d022e35SMiguel Boton }
6324d022e35SMiguel Boton 
6333c62c625SGlauber Costa void native_machine_shutdown(void)
6344d022e35SMiguel Boton {
6354d022e35SMiguel Boton 	/* Stop the cpus and apics */
636522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC
6372885432aSFenghua Yu 	/*
6382885432aSFenghua Yu 	 * Disabling IO APIC before local APIC is a workaround for
6392885432aSFenghua Yu 	 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
6402885432aSFenghua Yu 	 * Specification Update". In this situation, interrupts that target
6412885432aSFenghua Yu 	 * a Logical Processor whose Local APIC is either in the process of
6422885432aSFenghua Yu 	 * being hardware disabled or software disabled are neither delivered
6432885432aSFenghua Yu 	 * nor discarded. When this erratum occurs, the processor may hang.
6442885432aSFenghua Yu 	 *
6452885432aSFenghua Yu 	 * Even without the erratum, it still makes sense to quiet IO APIC
6462885432aSFenghua Yu 	 * before disabling Local APIC.
6472885432aSFenghua Yu 	 */
648522e6646SFenghua Yu 	disable_IO_APIC();
649522e6646SFenghua Yu #endif
650522e6646SFenghua Yu 
6514d022e35SMiguel Boton #ifdef CONFIG_SMP
652144d102bSMichael D Labriola 	/*
6531b3a5d02SRobin Holt 	 * Stop all of the others. Also disable the local irq to
6541b3a5d02SRobin Holt 	 * not receive the per-cpu timer interrupt which may trigger
6551b3a5d02SRobin Holt 	 * scheduler's load balance.
6564d022e35SMiguel Boton 	 */
65755c844a4SFeng Tang 	local_irq_disable();
65876fac077SAlok Kataria 	stop_other_cpus();
6594d022e35SMiguel Boton #endif
6604d022e35SMiguel Boton 
6614d022e35SMiguel Boton 	lapic_shutdown();
6624d022e35SMiguel Boton 
6634d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER
6644d022e35SMiguel Boton 	hpet_disable();
6654d022e35SMiguel Boton #endif
6664d022e35SMiguel Boton 
6674d022e35SMiguel Boton #ifdef CONFIG_X86_64
668338bac52SFUJITA Tomonori 	x86_platform.iommu_shutdown();
6694d022e35SMiguel Boton #endif
6704d022e35SMiguel Boton }
6714d022e35SMiguel Boton 
672d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency)
673d176720dSEduardo Habkost {
674d176720dSEduardo Habkost 	reboot_emergency = emergency;
675d176720dSEduardo Habkost 	machine_ops.emergency_restart();
676d176720dSEduardo Habkost }
677d176720dSEduardo Habkost 
678416e2d63SJody Belka static void native_machine_restart(char *__unused)
6794d022e35SMiguel Boton {
680c767a54bSJoe Perches 	pr_notice("machine restart\n");
6814d022e35SMiguel Boton 
6824d022e35SMiguel Boton 	if (!reboot_force)
6834d022e35SMiguel Boton 		machine_shutdown();
684d176720dSEduardo Habkost 	__machine_emergency_restart(0);
6854d022e35SMiguel Boton }
6864d022e35SMiguel Boton 
687416e2d63SJody Belka static void native_machine_halt(void)
6884d022e35SMiguel Boton {
689144d102bSMichael D Labriola 	/* Stop other cpus and apics */
690d3ec5caeSIvan Vecera 	machine_shutdown();
691d3ec5caeSIvan Vecera 
692840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
693840c2bafSJoseph Cihula 
694d3ec5caeSIvan Vecera 	stop_this_cpu(NULL);
6954d022e35SMiguel Boton }
6964d022e35SMiguel Boton 
697416e2d63SJody Belka static void native_machine_power_off(void)
6984d022e35SMiguel Boton {
6994d022e35SMiguel Boton 	if (pm_power_off) {
7004d022e35SMiguel Boton 		if (!reboot_force)
7014d022e35SMiguel Boton 			machine_shutdown();
7024d022e35SMiguel Boton 		pm_power_off();
7034d022e35SMiguel Boton 	}
704144d102bSMichael D Labriola 	/* A fallback in case there is no PM info available */
705840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
7064d022e35SMiguel Boton }
7074d022e35SMiguel Boton 
7084d022e35SMiguel Boton struct machine_ops machine_ops = {
709416e2d63SJody Belka 	.power_off = native_machine_power_off,
710416e2d63SJody Belka 	.shutdown = native_machine_shutdown,
711416e2d63SJody Belka 	.emergency_restart = native_machine_emergency_restart,
712416e2d63SJody Belka 	.restart = native_machine_restart,
713ed23dc6fSGlauber Costa 	.halt = native_machine_halt,
7142965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
715ed23dc6fSGlauber Costa 	.crash_shutdown = native_machine_crash_shutdown,
716ed23dc6fSGlauber Costa #endif
7174d022e35SMiguel Boton };
718416e2d63SJody Belka 
719416e2d63SJody Belka void machine_power_off(void)
720416e2d63SJody Belka {
721416e2d63SJody Belka 	machine_ops.power_off();
722416e2d63SJody Belka }
723416e2d63SJody Belka 
724416e2d63SJody Belka void machine_shutdown(void)
725416e2d63SJody Belka {
726416e2d63SJody Belka 	machine_ops.shutdown();
727416e2d63SJody Belka }
728416e2d63SJody Belka 
729416e2d63SJody Belka void machine_emergency_restart(void)
730416e2d63SJody Belka {
731d176720dSEduardo Habkost 	__machine_emergency_restart(1);
732416e2d63SJody Belka }
733416e2d63SJody Belka 
734416e2d63SJody Belka void machine_restart(char *cmd)
735416e2d63SJody Belka {
736416e2d63SJody Belka 	machine_ops.restart(cmd);
737416e2d63SJody Belka }
738416e2d63SJody Belka 
739416e2d63SJody Belka void machine_halt(void)
740416e2d63SJody Belka {
741416e2d63SJody Belka 	machine_ops.halt();
742416e2d63SJody Belka }
743416e2d63SJody Belka 
7442965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
745ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs)
746ed23dc6fSGlauber Costa {
747ed23dc6fSGlauber Costa 	machine_ops.crash_shutdown(regs);
748ed23dc6fSGlauber Costa }
749ed23dc6fSGlauber Costa #endif
7502ddded21SEduardo Habkost 
7512ddded21SEduardo Habkost 
752bb8dd270SEduardo Habkost #if defined(CONFIG_SMP)
7532ddded21SEduardo Habkost 
7542ddded21SEduardo Habkost /* This keeps a track of which one is crashing cpu. */
7552ddded21SEduardo Habkost static int crashing_cpu;
7562ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback;
7572ddded21SEduardo Habkost 
7582ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi;
75958c5661fSHidehiro Kawai static int crash_ipi_issued;
7602ddded21SEduardo Habkost 
7619c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
7622ddded21SEduardo Habkost {
7632ddded21SEduardo Habkost 	int cpu;
7642ddded21SEduardo Habkost 
7652ddded21SEduardo Habkost 	cpu = raw_smp_processor_id();
7662ddded21SEduardo Habkost 
767144d102bSMichael D Labriola 	/*
768144d102bSMichael D Labriola 	 * Don't do anything if this handler is invoked on crashing cpu.
7692ddded21SEduardo Habkost 	 * Otherwise, system will completely hang. Crashing cpu can get
7702ddded21SEduardo Habkost 	 * an NMI if system was initially booted with nmi_watchdog parameter.
7712ddded21SEduardo Habkost 	 */
7722ddded21SEduardo Habkost 	if (cpu == crashing_cpu)
7739c48f1c6SDon Zickus 		return NMI_HANDLED;
7742ddded21SEduardo Habkost 	local_irq_disable();
7752ddded21SEduardo Habkost 
7769c48f1c6SDon Zickus 	shootdown_callback(cpu, regs);
7772ddded21SEduardo Habkost 
7782ddded21SEduardo Habkost 	atomic_dec(&waiting_for_crash_ipi);
7792ddded21SEduardo Habkost 	/* Assume hlt works */
7802ddded21SEduardo Habkost 	halt();
7812ddded21SEduardo Habkost 	for (;;)
7822ddded21SEduardo Habkost 		cpu_relax();
7832ddded21SEduardo Habkost 
7849c48f1c6SDon Zickus 	return NMI_HANDLED;
7852ddded21SEduardo Habkost }
7862ddded21SEduardo Habkost 
7872ddded21SEduardo Habkost static void smp_send_nmi_allbutself(void)
7882ddded21SEduardo Habkost {
789dac5f412SIngo Molnar 	apic->send_IPI_allbutself(NMI_VECTOR);
7902ddded21SEduardo Habkost }
7912ddded21SEduardo Habkost 
792144d102bSMichael D Labriola /*
793144d102bSMichael D Labriola  * Halt all other CPUs, calling the specified function on each of them
794bb8dd270SEduardo Habkost  *
795bb8dd270SEduardo Habkost  * This function can be used to halt all other CPUs on crash
796bb8dd270SEduardo Habkost  * or emergency reboot time. The function passed as parameter
797bb8dd270SEduardo Habkost  * will be called inside a NMI handler on all CPUs.
798bb8dd270SEduardo Habkost  */
7992ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
8002ddded21SEduardo Habkost {
8012ddded21SEduardo Habkost 	unsigned long msecs;
802c415b3dcSEduardo Habkost 	local_irq_disable();
8032ddded21SEduardo Habkost 
8042ddded21SEduardo Habkost 	/* Make a note of crashing cpu. Will be used in NMI callback. */
8052ddded21SEduardo Habkost 	crashing_cpu = safe_smp_processor_id();
8062ddded21SEduardo Habkost 
8072ddded21SEduardo Habkost 	shootdown_callback = callback;
8082ddded21SEduardo Habkost 
8092ddded21SEduardo Habkost 	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
8102ddded21SEduardo Habkost 	/* Would it be better to replace the trap vector here? */
8119c48f1c6SDon Zickus 	if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
8129c48f1c6SDon Zickus 				 NMI_FLAG_FIRST, "crash"))
813144d102bSMichael D Labriola 		return;		/* Return what? */
814144d102bSMichael D Labriola 	/*
815144d102bSMichael D Labriola 	 * Ensure the new callback function is set before sending
8162ddded21SEduardo Habkost 	 * out the NMI
8172ddded21SEduardo Habkost 	 */
8182ddded21SEduardo Habkost 	wmb();
8192ddded21SEduardo Habkost 
8202ddded21SEduardo Habkost 	smp_send_nmi_allbutself();
8212ddded21SEduardo Habkost 
82258c5661fSHidehiro Kawai 	/* Kick CPUs looping in NMI context. */
82358c5661fSHidehiro Kawai 	WRITE_ONCE(crash_ipi_issued, 1);
82458c5661fSHidehiro Kawai 
8252ddded21SEduardo Habkost 	msecs = 1000; /* Wait at most a second for the other cpus to stop */
8262ddded21SEduardo Habkost 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
8272ddded21SEduardo Habkost 		mdelay(1);
8282ddded21SEduardo Habkost 		msecs--;
8292ddded21SEduardo Habkost 	}
8302ddded21SEduardo Habkost 
8312ddded21SEduardo Habkost 	/* Leave the nmi callback set */
8322ddded21SEduardo Habkost }
83358c5661fSHidehiro Kawai 
834b279d67dSHidehiro Kawai /*
835b279d67dSHidehiro Kawai  * Check if the crash dumping IPI got issued and if so, call its callback
836b279d67dSHidehiro Kawai  * directly. This function is used when we have already been in NMI handler.
837b279d67dSHidehiro Kawai  * It doesn't return.
838b279d67dSHidehiro Kawai  */
839b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
840b279d67dSHidehiro Kawai {
841b279d67dSHidehiro Kawai 	if (crash_ipi_issued)
842b279d67dSHidehiro Kawai 		crash_nmi_callback(0, regs);
843b279d67dSHidehiro Kawai }
844b279d67dSHidehiro Kawai 
84558c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */
84658c5661fSHidehiro Kawai void nmi_panic_self_stop(struct pt_regs *regs)
84758c5661fSHidehiro Kawai {
84858c5661fSHidehiro Kawai 	while (1) {
849b279d67dSHidehiro Kawai 		/* If no CPU is preparing crash dump, we simply loop here. */
850b279d67dSHidehiro Kawai 		run_crash_ipi_callback(regs);
85158c5661fSHidehiro Kawai 		cpu_relax();
85258c5661fSHidehiro Kawai 	}
85358c5661fSHidehiro Kawai }
85458c5661fSHidehiro Kawai 
855bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */
856bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
857bb8dd270SEduardo Habkost {
858bb8dd270SEduardo Habkost 	/* No other CPUs to shoot down */
859bb8dd270SEduardo Habkost }
860b279d67dSHidehiro Kawai 
861b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
862b279d67dSHidehiro Kawai {
863b279d67dSHidehiro Kawai }
8642ddded21SEduardo Habkost #endif
865