xref: /linux/arch/x86/kernel/reboot.c (revision b279d67df88a49c6ca32b3eebd195660254be394)
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 /*
581ef03890SPeter Chubb  * Some machines require the "reboot=b" or "reboot=k"  commandline options,
594d022e35SMiguel Boton  * this quirk makes that automatic.
604d022e35SMiguel Boton  */
614d022e35SMiguel Boton static int __init set_bios_reboot(const struct dmi_system_id *d)
624d022e35SMiguel Boton {
634d022e35SMiguel Boton 	if (reboot_type != BOOT_BIOS) {
644d022e35SMiguel Boton 		reboot_type = BOOT_BIOS;
65c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
666d9153bbSLan Tianyu 			d->ident, "BIOS");
674d022e35SMiguel Boton 	}
684d022e35SMiguel Boton 	return 0;
694d022e35SMiguel Boton }
704d022e35SMiguel Boton 
7165051397SH. Peter Anvin void __noreturn machine_real_restart(unsigned int type)
7257b16594SMichael D Labriola {
7357b16594SMichael D Labriola 	local_irq_disable();
7457b16594SMichael D Labriola 
75144d102bSMichael D Labriola 	/*
76144d102bSMichael D Labriola 	 * Write zero to CMOS register number 0x0f, which the BIOS POST
77144d102bSMichael D Labriola 	 * routine will recognize as telling it to do a proper reboot.  (Well
78144d102bSMichael D Labriola 	 * that's what this book in front of me says -- it may only apply to
79144d102bSMichael D Labriola 	 * the Phoenix BIOS though, it's not clear).  At the same time,
80144d102bSMichael D Labriola 	 * disable NMIs by setting the top bit in the CMOS address register,
81144d102bSMichael D Labriola 	 * as we're about to do peculiar things to the CPU.  I'm not sure if
82144d102bSMichael D Labriola 	 * `outb_p' is needed instead of just `outb'.  Use it to be on the
83144d102bSMichael D Labriola 	 * safe side.  (Yes, CMOS_WRITE does outb_p's. -  Paul G.)
8457b16594SMichael D Labriola 	 */
8557b16594SMichael D Labriola 	spin_lock(&rtc_lock);
8657b16594SMichael D Labriola 	CMOS_WRITE(0x00, 0x8f);
8757b16594SMichael D Labriola 	spin_unlock(&rtc_lock);
8857b16594SMichael D Labriola 
8957b16594SMichael D Labriola 	/*
9057b16594SMichael D Labriola 	 * Switch back to the initial page table.
9157b16594SMichael D Labriola 	 */
9265051397SH. Peter Anvin #ifdef CONFIG_X86_32
9357b16594SMichael D Labriola 	load_cr3(initial_page_table);
9465051397SH. Peter Anvin #else
9565051397SH. Peter Anvin 	write_cr3(real_mode_header->trampoline_pgd);
9665051397SH. Peter Anvin #endif
9757b16594SMichael D Labriola 
9857b16594SMichael D Labriola 	/* Jump to the identity-mapped low memory code */
9965051397SH. Peter Anvin #ifdef CONFIG_X86_32
10065051397SH. Peter Anvin 	asm volatile("jmpl *%0" : :
10165051397SH. Peter Anvin 		     "rm" (real_mode_header->machine_real_restart_asm),
10265051397SH. Peter Anvin 		     "a" (type));
10365051397SH. Peter Anvin #else
10465051397SH. Peter Anvin 	asm volatile("ljmpl *%0" : :
10565051397SH. Peter Anvin 		     "m" (real_mode_header->machine_real_restart_asm),
10665051397SH. Peter Anvin 		     "D" (type));
10765051397SH. Peter Anvin #endif
10865051397SH. Peter Anvin 	unreachable();
10957b16594SMichael D Labriola }
11057b16594SMichael D Labriola #ifdef CONFIG_APM_MODULE
11157b16594SMichael D Labriola EXPORT_SYMBOL(machine_real_restart);
11257b16594SMichael D Labriola #endif
11357b16594SMichael D Labriola 
11457b16594SMichael D Labriola /*
11557b16594SMichael D Labriola  * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
11657b16594SMichael D Labriola  */
11757b16594SMichael D Labriola static int __init set_pci_reboot(const struct dmi_system_id *d)
11857b16594SMichael D Labriola {
1195be44a6fSIngo Molnar 	if (reboot_type != BOOT_CF9_FORCE) {
1205be44a6fSIngo Molnar 		reboot_type = BOOT_CF9_FORCE;
121c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboots.\n",
1226d9153bbSLan Tianyu 			d->ident, "PCI");
12357b16594SMichael D Labriola 	}
12457b16594SMichael D Labriola 	return 0;
12557b16594SMichael D Labriola }
12657b16594SMichael D Labriola 
1271ef03890SPeter Chubb static int __init set_kbd_reboot(const struct dmi_system_id *d)
1281ef03890SPeter Chubb {
1291ef03890SPeter Chubb 	if (reboot_type != BOOT_KBD) {
1301ef03890SPeter Chubb 		reboot_type = BOOT_KBD;
131c767a54bSJoe Perches 		pr_info("%s series board detected. Selecting %s-method for reboot.\n",
1326d9153bbSLan Tianyu 			d->ident, "KBD");
1331ef03890SPeter Chubb 	}
1341ef03890SPeter Chubb 	return 0;
1351ef03890SPeter Chubb }
1361ef03890SPeter Chubb 
137144d102bSMichael D Labriola /*
13865051397SH. Peter Anvin  * This is a single dmi_table handling all reboot quirks.
13957b16594SMichael D Labriola  */
1404d022e35SMiguel Boton static struct dmi_system_id __initdata reboot_dmi_table[] = {
14157b16594SMichael D Labriola 
142e56e57f6SDave Jones 	/* Acer */
143b49c78d4SPeter Chubb 	{	/* Handle reboot issue on Acer Aspire one */
1441ef03890SPeter Chubb 		.callback = set_kbd_reboot,
145b49c78d4SPeter Chubb 		.ident = "Acer Aspire One A110",
146b49c78d4SPeter Chubb 		.matches = {
147b49c78d4SPeter Chubb 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
148b49c78d4SPeter Chubb 			DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
149b49c78d4SPeter Chubb 		},
150b49c78d4SPeter Chubb 	},
151e56e57f6SDave Jones 
152e56e57f6SDave Jones 	/* Apple */
1533e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBook5 */
1546c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
1553e03bbeaSShunichi Fuji 		.ident = "Apple MacBook5",
1566c6c51e4SPaul Mackerras 		.matches = {
1576c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1583e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
1596c6c51e4SPaul Mackerras 		},
1606c6c51e4SPaul Mackerras 	},
1613e03bbeaSShunichi Fuji 	{	/* Handle problems with rebooting on Apple MacBookPro5 */
162498cdbfbSOzan Çağlayan 		.callback = set_pci_reboot,
1633e03bbeaSShunichi Fuji 		.ident = "Apple MacBookPro5",
164498cdbfbSOzan Çağlayan 		.matches = {
165498cdbfbSOzan Çağlayan 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1663e03bbeaSShunichi Fuji 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
167498cdbfbSOzan Çağlayan 		},
168498cdbfbSOzan Çağlayan 	},
16905154752SGottfried Haider 	{	/* Handle problems with rebooting on Apple Macmini3,1 */
17005154752SGottfried Haider 		.callback = set_pci_reboot,
17105154752SGottfried Haider 		.ident = "Apple Macmini3,1",
17205154752SGottfried Haider 		.matches = {
17305154752SGottfried Haider 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
17405154752SGottfried Haider 			DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
17505154752SGottfried Haider 		},
17605154752SGottfried Haider 	},
1770a832320SJustin P. Mattock 	{	/* Handle problems with rebooting on the iMac9,1. */
1780a832320SJustin P. Mattock 		.callback = set_pci_reboot,
1790a832320SJustin P. Mattock 		.ident = "Apple iMac9,1",
1800a832320SJustin P. Mattock 		.matches = {
1810a832320SJustin P. Mattock 			DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
1820a832320SJustin P. Mattock 			DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
1830a832320SJustin P. Mattock 		},
1840a832320SJustin P. Mattock 	},
185e56e57f6SDave Jones 
18680313b30SStefan Lippers-Hollmann 	/* ASRock */
18780313b30SStefan Lippers-Hollmann 	{	/* Handle problems with rebooting on ASRock Q1900DC-ITX */
18880313b30SStefan Lippers-Hollmann 		.callback = set_pci_reboot,
18980313b30SStefan Lippers-Hollmann 		.ident = "ASRock Q1900DC-ITX",
19080313b30SStefan Lippers-Hollmann 		.matches = {
19180313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
19280313b30SStefan Lippers-Hollmann 			DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
19380313b30SStefan Lippers-Hollmann 		},
19480313b30SStefan Lippers-Hollmann 	},
19580313b30SStefan Lippers-Hollmann 
196e56e57f6SDave Jones 	/* ASUS */
197e56e57f6SDave Jones 	{	/* Handle problems with rebooting on ASUS P4S800 */
198e56e57f6SDave Jones 		.callback = set_bios_reboot,
199e56e57f6SDave Jones 		.ident = "ASUS P4S800",
200e56e57f6SDave Jones 		.matches = {
201e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
202e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
203e56e57f6SDave Jones 		},
204e56e57f6SDave Jones 	},
205e56e57f6SDave Jones 
206aadca6faSChristian Gmeiner 	/* Certec */
207aadca6faSChristian Gmeiner 	{       /* Handle problems with rebooting on Certec BPC600 */
208aadca6faSChristian Gmeiner 		.callback = set_pci_reboot,
209aadca6faSChristian Gmeiner 		.ident = "Certec BPC600",
210aadca6faSChristian Gmeiner 		.matches = {
211aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
212aadca6faSChristian Gmeiner 			DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
213aadca6faSChristian Gmeiner 		},
214aadca6faSChristian Gmeiner 	},
215aadca6faSChristian Gmeiner 
216e56e57f6SDave Jones 	/* Dell */
217e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell DXP061 */
218e56e57f6SDave Jones 		.callback = set_bios_reboot,
219e56e57f6SDave Jones 		.ident = "Dell DXP061",
2203628c3f5SMaxime Ripard 		.matches = {
2213628c3f5SMaxime Ripard 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
222e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
223e56e57f6SDave Jones 		},
224e56e57f6SDave Jones 	},
225e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell E520's */
226e56e57f6SDave Jones 		.callback = set_bios_reboot,
227e56e57f6SDave Jones 		.ident = "Dell E520",
228e56e57f6SDave Jones 		.matches = {
229e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
230e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
2313628c3f5SMaxime Ripard 		},
2323628c3f5SMaxime Ripard 	},
2338412da75SVille Syrjälä 	{	/* Handle problems with rebooting on the Latitude E5410. */
2348412da75SVille Syrjälä 		.callback = set_pci_reboot,
2358412da75SVille Syrjälä 		.ident = "Dell Latitude E5410",
2368412da75SVille Syrjälä 		.matches = {
2378412da75SVille Syrjälä 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
2388412da75SVille Syrjälä 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
2398412da75SVille Syrjälä 		},
2408412da75SVille Syrjälä 	},
241b7798d28SDaniel J Blueman 	{	/* Handle problems with rebooting on the Latitude E5420. */
242b7798d28SDaniel J Blueman 		.callback = set_pci_reboot,
243b7798d28SDaniel J Blueman 		.ident = "Dell Latitude E5420",
244b7798d28SDaniel J Blueman 		.matches = {
245b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
246b7798d28SDaniel J Blueman 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
247b7798d28SDaniel J Blueman 		},
248b7798d28SDaniel J Blueman 	},
249e56e57f6SDave Jones 	{	/* Handle problems with rebooting on the Latitude E6320. */
250e56e57f6SDave Jones 		.callback = set_pci_reboot,
251e56e57f6SDave Jones 		.ident = "Dell Latitude E6320",
252e56e57f6SDave Jones 		.matches = {
253e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
254e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
255e56e57f6SDave Jones 		},
256e56e57f6SDave Jones 	},
257a536877eSH. Peter Anvin 	{	/* Handle problems with rebooting on the Latitude E6420. */
258a536877eSH. Peter Anvin 		.callback = set_pci_reboot,
259a536877eSH. Peter Anvin 		.ident = "Dell Latitude E6420",
260a536877eSH. Peter Anvin 		.matches = {
261a536877eSH. Peter Anvin 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
262a536877eSH. Peter Anvin 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
263a536877eSH. Peter Anvin 		},
264a536877eSH. Peter Anvin 	},
265e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
266e56e57f6SDave Jones 		.callback = set_bios_reboot,
267e56e57f6SDave Jones 		.ident = "Dell OptiPlex 330",
268e56e57f6SDave Jones 		.matches = {
269e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
270e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
271e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
272e56e57f6SDave Jones 		},
273e56e57f6SDave Jones 	},
274e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
275e56e57f6SDave Jones 		.callback = set_bios_reboot,
276e56e57f6SDave Jones 		.ident = "Dell OptiPlex 360",
277e56e57f6SDave Jones 		.matches = {
278e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
279e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
280e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
281e56e57f6SDave Jones 		},
282e56e57f6SDave Jones 	},
283e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's SFF */
284e56e57f6SDave Jones 		.callback = set_bios_reboot,
285e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
286e56e57f6SDave Jones 		.matches = {
287e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
288e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
289e56e57f6SDave Jones 		},
290e56e57f6SDave Jones 	},
291e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745's DFF */
292e56e57f6SDave Jones 		.callback = set_bios_reboot,
293e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
294e56e57f6SDave Jones 		.matches = {
295e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
296e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
297e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
298e56e57f6SDave Jones 		},
299e56e57f6SDave Jones 	},
300e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
301e56e57f6SDave Jones 		.callback = set_bios_reboot,
302e56e57f6SDave Jones 		.ident = "Dell OptiPlex 745",
303e56e57f6SDave Jones 		.matches = {
304e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
305e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
306e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
307e56e57f6SDave Jones 		},
308e56e57f6SDave Jones 	},
309e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
310e56e57f6SDave Jones 		.callback = set_bios_reboot,
311e56e57f6SDave Jones 		.ident = "Dell OptiPlex 760",
312e56e57f6SDave Jones 		.matches = {
313e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
314e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
315e56e57f6SDave Jones 			DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
316e56e57f6SDave Jones 		},
317e56e57f6SDave Jones 	},
3186be30bb7SRafael J. Wysocki 	{	/* Handle problems with rebooting on the OptiPlex 990. */
3196be30bb7SRafael J. Wysocki 		.callback = set_pci_reboot,
3206be30bb7SRafael J. Wysocki 		.ident = "Dell OptiPlex 990",
3216be30bb7SRafael J. Wysocki 		.matches = {
3226be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3236be30bb7SRafael J. Wysocki 			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
3246be30bb7SRafael J. Wysocki 		},
3256be30bb7SRafael J. Wysocki 	},
326e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 300's */
327e56e57f6SDave Jones 		.callback = set_bios_reboot,
328e56e57f6SDave Jones 		.ident = "Dell PowerEdge 300",
32976eb9a30SZhang Rui 		.matches = {
330e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
331e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
332e56e57f6SDave Jones 		},
333e56e57f6SDave Jones 	},
334e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 1300's */
335e56e57f6SDave Jones 		.callback = set_bios_reboot,
336e56e57f6SDave Jones 		.ident = "Dell PowerEdge 1300",
337e56e57f6SDave Jones 		.matches = {
338e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
339e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
340e56e57f6SDave Jones 		},
341e56e57f6SDave Jones 	},
342e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell 2400's */
343e56e57f6SDave Jones 		.callback = set_bios_reboot,
344e56e57f6SDave Jones 		.ident = "Dell PowerEdge 2400",
345e56e57f6SDave Jones 		.matches = {
346e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
347e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
34876eb9a30SZhang Rui 		},
34976eb9a30SZhang Rui 	},
3504f0acd31SMasoud Sharbiani 	{	/* Handle problems with rebooting on the Dell PowerEdge C6100. */
3514f0acd31SMasoud Sharbiani 		.callback = set_pci_reboot,
3524f0acd31SMasoud Sharbiani 		.ident = "Dell PowerEdge C6100",
3534f0acd31SMasoud Sharbiani 		.matches = {
3544f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
3554f0acd31SMasoud Sharbiani 			DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
3564f0acd31SMasoud Sharbiani 		},
3574f0acd31SMasoud Sharbiani 	},
3586c6c51e4SPaul Mackerras 	{	/* Handle problems with rebooting on the Precision M6600. */
3596c6c51e4SPaul Mackerras 		.callback = set_pci_reboot,
3606c6c51e4SPaul Mackerras 		.ident = "Dell Precision M6600",
3616c6c51e4SPaul Mackerras 		.matches = {
3626c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
3636c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
3646c6c51e4SPaul Mackerras 		},
3656c6c51e4SPaul Mackerras 	},
366e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T5400's */
367e56e57f6SDave Jones 		.callback = set_bios_reboot,
368e56e57f6SDave Jones 		.ident = "Dell Precision T5400",
36957b16594SMichael D Labriola 		.matches = {
3706c6c51e4SPaul Mackerras 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
371e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
372144d102bSMichael D Labriola 		},
3735955633eSMichael D Labriola 	},
374e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell T7400's */
375e56e57f6SDave Jones 		.callback = set_bios_reboot,
376e56e57f6SDave Jones 		.ident = "Dell Precision T7400",
3776c6c51e4SPaul Mackerras 		.matches = {
378e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
379e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
3806c6c51e4SPaul Mackerras 		},
3814d022e35SMiguel Boton 	},
382e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Dell XPS710 */
383e56e57f6SDave Jones 		.callback = set_bios_reboot,
384e56e57f6SDave Jones 		.ident = "Dell XPS710",
385e56e57f6SDave Jones 		.matches = {
386e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
387e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
388e56e57f6SDave Jones 		},
389e56e57f6SDave Jones 	},
390e56e57f6SDave Jones 
391e56e57f6SDave Jones 	/* Hewlett-Packard */
392e56e57f6SDave Jones 	{	/* Handle problems with rebooting on HP laptops */
393e56e57f6SDave Jones 		.callback = set_bios_reboot,
394e56e57f6SDave Jones 		.ident = "HP Compaq Laptop",
395e56e57f6SDave Jones 		.matches = {
396e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
397e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
398e56e57f6SDave Jones 		},
399e56e57f6SDave Jones 	},
400e56e57f6SDave Jones 
401e56e57f6SDave Jones 	/* Sony */
402e56e57f6SDave Jones 	{	/* Handle problems with rebooting on Sony VGN-Z540N */
403e56e57f6SDave Jones 		.callback = set_bios_reboot,
404e56e57f6SDave Jones 		.ident = "Sony VGN-Z540N",
405e56e57f6SDave Jones 		.matches = {
406e56e57f6SDave Jones 			DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
407e56e57f6SDave Jones 			DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
408e56e57f6SDave Jones 		},
409e56e57f6SDave Jones 	},
410e56e57f6SDave Jones 
4114d022e35SMiguel Boton 	{ }
4124d022e35SMiguel Boton };
4139c48f1c6SDon Zickus 
414d176720dSEduardo Habkost static int __init reboot_init(void)
415d176720dSEduardo Habkost {
41644be28e9SMatt Fleming 	int rv;
41744be28e9SMatt Fleming 
418d176720dSEduardo Habkost 	/*
419d176720dSEduardo Habkost 	 * Only do the DMI check if reboot_type hasn't been overridden
420d176720dSEduardo Habkost 	 * on the command line
421d176720dSEduardo Habkost 	 */
42244be28e9SMatt Fleming 	if (!reboot_default)
42344be28e9SMatt Fleming 		return 0;
42444be28e9SMatt Fleming 
42544be28e9SMatt Fleming 	/*
42644be28e9SMatt Fleming 	 * The DMI quirks table takes precedence. If no quirks entry
42744be28e9SMatt Fleming 	 * matches and the ACPI Hardware Reduced bit is set, force EFI
42844be28e9SMatt Fleming 	 * reboot.
42944be28e9SMatt Fleming 	 */
43044be28e9SMatt Fleming 	rv = dmi_check_system(reboot_dmi_table);
43144be28e9SMatt Fleming 
43244be28e9SMatt Fleming 	if (!rv && efi_reboot_required())
43344be28e9SMatt Fleming 		reboot_type = BOOT_EFI;
43444be28e9SMatt Fleming 
435d176720dSEduardo Habkost 	return 0;
436d176720dSEduardo Habkost }
437d176720dSEduardo Habkost core_initcall(reboot_init);
438d176720dSEduardo Habkost 
439d176720dSEduardo Habkost static inline void kb_wait(void)
440d176720dSEduardo Habkost {
441d176720dSEduardo Habkost 	int i;
442d176720dSEduardo Habkost 
443d176720dSEduardo Habkost 	for (i = 0; i < 0x10000; i++) {
4447432d149SIngo Molnar 		if ((inb(0x64) & 0x02) == 0)
4457432d149SIngo Molnar 			break;
4467432d149SIngo Molnar 		udelay(2);
4477432d149SIngo Molnar 	}
448416e2d63SJody Belka }
4494d022e35SMiguel Boton 
4504d022e35SMiguel Boton static void vmxoff_nmi(int cpu, struct pt_regs *regs)
4514d022e35SMiguel Boton {
452d176720dSEduardo Habkost 	cpu_emergency_vmxoff();
453d176720dSEduardo Habkost }
454d176720dSEduardo Habkost 
455144d102bSMichael D Labriola /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
4564d022e35SMiguel Boton static void emergency_vmx_disable_all(void)
4574d022e35SMiguel Boton {
4584d022e35SMiguel Boton 	/* Just make sure we won't change CPUs while doing this */
4594d022e35SMiguel Boton 	local_irq_disable();
4604d022e35SMiguel Boton 
461144d102bSMichael D Labriola 	/*
462144d102bSMichael D Labriola 	 * We need to disable VMX on all CPUs before rebooting, otherwise
4634d022e35SMiguel Boton 	 * we risk hanging up the machine, because the CPU ignore INIT
4644d022e35SMiguel Boton 	 * signals when VMX is enabled.
4654d022e35SMiguel Boton 	 *
4664d022e35SMiguel Boton 	 * We can't take any locks and we may be on an inconsistent
4674d022e35SMiguel Boton 	 * state, so we use NMIs as IPIs to tell the other CPUs to disable
4684d022e35SMiguel Boton 	 * VMX and halt.
4694d022e35SMiguel Boton 	 *
4704d022e35SMiguel Boton 	 * For safety, we will avoid running the nmi_shootdown_cpus()
4714d022e35SMiguel Boton 	 * stuff unnecessarily, but we don't have a way to check
4724d022e35SMiguel Boton 	 * if other CPUs have VMX enabled. So we will call it only if the
4734d022e35SMiguel Boton 	 * CPU we are running on has VMX enabled.
4744d022e35SMiguel Boton 	 *
4754d022e35SMiguel Boton 	 * We will miss cases where VMX is not enabled on all CPUs. This
4764d022e35SMiguel Boton 	 * shouldn't do much harm because KVM always enable VMX on all
4774d022e35SMiguel Boton 	 * CPUs anyway. But we can miss it on the small window where KVM
4784d022e35SMiguel Boton 	 * is still enabling VMX.
4794d022e35SMiguel Boton 	 */
4804d022e35SMiguel Boton 	if (cpu_has_vmx() && cpu_vmx_enabled()) {
481144d102bSMichael D Labriola 		/* Disable VMX on this CPU. */
4824d022e35SMiguel Boton 		cpu_vmxoff();
4834d022e35SMiguel Boton 
4844d022e35SMiguel Boton 		/* Halt and disable VMX on the other CPUs */
4854d022e35SMiguel Boton 		nmi_shootdown_cpus(vmxoff_nmi);
4864d022e35SMiguel Boton 
4874d022e35SMiguel Boton 	}
4884d022e35SMiguel Boton }
4894d022e35SMiguel Boton 
4904d022e35SMiguel Boton 
4914d022e35SMiguel Boton void __attribute__((weak)) mach_reboot_fixups(void)
4924d022e35SMiguel Boton {
4934d022e35SMiguel Boton }
4944d022e35SMiguel Boton 
495660e34ceSMatthew Garrett /*
4965be44a6fSIngo Molnar  * To the best of our knowledge Windows compatible x86 hardware expects
4975be44a6fSIngo Molnar  * the following on reboot:
498660e34ceSMatthew Garrett  *
499660e34ceSMatthew Garrett  * 1) If the FADT has the ACPI reboot register flag set, try it
500660e34ceSMatthew Garrett  * 2) If still alive, write to the keyboard controller
501660e34ceSMatthew Garrett  * 3) If still alive, write to the ACPI reboot register again
502660e34ceSMatthew Garrett  * 4) If still alive, write to the keyboard controller again
503a4f1987eSLi, Aubrey  * 5) If still alive, call the EFI runtime service to reboot
5045be44a6fSIngo Molnar  * 6) If no EFI runtime service, call the BIOS to do a reboot
505660e34ceSMatthew Garrett  *
5065be44a6fSIngo Molnar  * We default to following the same pattern. We also have
5075be44a6fSIngo Molnar  * two other reboot methods: 'triple fault' and 'PCI', which
5085be44a6fSIngo Molnar  * can be triggered via the reboot= kernel boot option or
5095be44a6fSIngo Molnar  * via quirks.
5105be44a6fSIngo Molnar  *
5115be44a6fSIngo Molnar  * This means that this function can never return, it can misbehave
5125be44a6fSIngo Molnar  * by not rebooting properly and hanging.
513660e34ceSMatthew Garrett  */
5144d022e35SMiguel Boton static void native_machine_emergency_restart(void)
5154d022e35SMiguel Boton {
5164d022e35SMiguel Boton 	int i;
517660e34ceSMatthew Garrett 	int attempt = 0;
518660e34ceSMatthew Garrett 	int orig_reboot_type = reboot_type;
519edf2b139SRobin Holt 	unsigned short mode;
5204d022e35SMiguel Boton 
5214d022e35SMiguel Boton 	if (reboot_emergency)
5224d022e35SMiguel Boton 		emergency_vmx_disable_all();
5234d022e35SMiguel Boton 
524840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_REBOOT);
525840c2bafSJoseph Cihula 
5264d022e35SMiguel Boton 	/* Tell the BIOS if we want cold or warm reboot */
527edf2b139SRobin Holt 	mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
528edf2b139SRobin Holt 	*((unsigned short *)__va(0x472)) = mode;
5294d022e35SMiguel Boton 
5304d022e35SMiguel Boton 	for (;;) {
5314d022e35SMiguel Boton 		/* Could also try the reset bit in the Hammer NB */
5324d022e35SMiguel Boton 		switch (reboot_type) {
5335be44a6fSIngo Molnar 		case BOOT_ACPI:
5345be44a6fSIngo Molnar 			acpi_reboot();
5355be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
5365be44a6fSIngo Molnar 			break;
5375be44a6fSIngo Molnar 
5384d022e35SMiguel Boton 		case BOOT_KBD:
539144d102bSMichael D Labriola 			mach_reboot_fixups(); /* For board specific fixups */
5407432d149SIngo Molnar 
5414d022e35SMiguel Boton 			for (i = 0; i < 10; i++) {
5424d022e35SMiguel Boton 				kb_wait();
5434d022e35SMiguel Boton 				udelay(50);
544144d102bSMichael D Labriola 				outb(0xfe, 0x64); /* Pulse reset low */
5454d022e35SMiguel Boton 				udelay(50);
5464d022e35SMiguel Boton 			}
547660e34ceSMatthew Garrett 			if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
548660e34ceSMatthew Garrett 				attempt = 1;
549660e34ceSMatthew Garrett 				reboot_type = BOOT_ACPI;
550660e34ceSMatthew Garrett 			} else {
551a4f1987eSLi, Aubrey 				reboot_type = BOOT_EFI;
552660e34ceSMatthew Garrett 			}
553660e34ceSMatthew Garrett 			break;
5544d022e35SMiguel Boton 
5554d022e35SMiguel Boton 		case BOOT_EFI:
5568562c99cSMatt Fleming 			efi_reboot(reboot_mode, NULL);
5575be44a6fSIngo Molnar 			reboot_type = BOOT_BIOS;
55814d7ca5cSH. Peter Anvin 			break;
5594d022e35SMiguel Boton 
5605be44a6fSIngo Molnar 		case BOOT_BIOS:
5615be44a6fSIngo Molnar 			machine_real_restart(MRR_BIOS);
5625be44a6fSIngo Molnar 
5635be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
5645be44a6fSIngo Molnar 			reboot_type = BOOT_CF9_SAFE;
5655be44a6fSIngo Molnar 			break;
5665be44a6fSIngo Molnar 
5675be44a6fSIngo Molnar 		case BOOT_CF9_FORCE:
56814d7ca5cSH. Peter Anvin 			port_cf9_safe = true;
569144d102bSMichael D Labriola 			/* Fall through */
57014d7ca5cSH. Peter Anvin 
5715be44a6fSIngo Molnar 		case BOOT_CF9_SAFE:
57214d7ca5cSH. Peter Anvin 			if (port_cf9_safe) {
5735be44a6fSIngo Molnar 				u8 reboot_code = reboot_mode == REBOOT_WARM ?  0x06 : 0x0E;
57416c21ae5SLi Fei 				u8 cf9 = inb(0xcf9) & ~reboot_code;
57514d7ca5cSH. Peter Anvin 				outb(cf9|2, 0xcf9); /* Request hard reset */
57614d7ca5cSH. Peter Anvin 				udelay(50);
57716c21ae5SLi Fei 				/* Actually do the reset */
57816c21ae5SLi Fei 				outb(cf9|reboot_code, 0xcf9);
57914d7ca5cSH. Peter Anvin 				udelay(50);
58014d7ca5cSH. Peter Anvin 			}
5815be44a6fSIngo Molnar 			reboot_type = BOOT_TRIPLE;
5825be44a6fSIngo Molnar 			break;
5835be44a6fSIngo Molnar 
5845be44a6fSIngo Molnar 		case BOOT_TRIPLE:
5855be44a6fSIngo Molnar 			load_idt(&no_idt);
5865be44a6fSIngo Molnar 			__asm__ __volatile__("int3");
5875be44a6fSIngo Molnar 
5885be44a6fSIngo Molnar 			/* We're probably dead after this, but... */
5895be44a6fSIngo Molnar 			reboot_type = BOOT_KBD;
5904d022e35SMiguel Boton 			break;
5914d022e35SMiguel Boton 		}
5924d022e35SMiguel Boton 	}
5934d022e35SMiguel Boton }
5944d022e35SMiguel Boton 
5953c62c625SGlauber Costa void native_machine_shutdown(void)
5964d022e35SMiguel Boton {
5974d022e35SMiguel Boton 	/* Stop the cpus and apics */
598522e6646SFenghua Yu #ifdef CONFIG_X86_IO_APIC
5992885432aSFenghua Yu 	/*
6002885432aSFenghua Yu 	 * Disabling IO APIC before local APIC is a workaround for
6012885432aSFenghua Yu 	 * erratum AVR31 in "Intel Atom Processor C2000 Product Family
6022885432aSFenghua Yu 	 * Specification Update". In this situation, interrupts that target
6032885432aSFenghua Yu 	 * a Logical Processor whose Local APIC is either in the process of
6042885432aSFenghua Yu 	 * being hardware disabled or software disabled are neither delivered
6052885432aSFenghua Yu 	 * nor discarded. When this erratum occurs, the processor may hang.
6062885432aSFenghua Yu 	 *
6072885432aSFenghua Yu 	 * Even without the erratum, it still makes sense to quiet IO APIC
6082885432aSFenghua Yu 	 * before disabling Local APIC.
6092885432aSFenghua Yu 	 */
610522e6646SFenghua Yu 	disable_IO_APIC();
611522e6646SFenghua Yu #endif
612522e6646SFenghua Yu 
6134d022e35SMiguel Boton #ifdef CONFIG_SMP
614144d102bSMichael D Labriola 	/*
6151b3a5d02SRobin Holt 	 * Stop all of the others. Also disable the local irq to
6161b3a5d02SRobin Holt 	 * not receive the per-cpu timer interrupt which may trigger
6171b3a5d02SRobin Holt 	 * scheduler's load balance.
6184d022e35SMiguel Boton 	 */
61955c844a4SFeng Tang 	local_irq_disable();
62076fac077SAlok Kataria 	stop_other_cpus();
6214d022e35SMiguel Boton #endif
6224d022e35SMiguel Boton 
6234d022e35SMiguel Boton 	lapic_shutdown();
6244d022e35SMiguel Boton 
6254d022e35SMiguel Boton #ifdef CONFIG_HPET_TIMER
6264d022e35SMiguel Boton 	hpet_disable();
6274d022e35SMiguel Boton #endif
6284d022e35SMiguel Boton 
6294d022e35SMiguel Boton #ifdef CONFIG_X86_64
630338bac52SFUJITA Tomonori 	x86_platform.iommu_shutdown();
6314d022e35SMiguel Boton #endif
6324d022e35SMiguel Boton }
6334d022e35SMiguel Boton 
634d176720dSEduardo Habkost static void __machine_emergency_restart(int emergency)
635d176720dSEduardo Habkost {
636d176720dSEduardo Habkost 	reboot_emergency = emergency;
637d176720dSEduardo Habkost 	machine_ops.emergency_restart();
638d176720dSEduardo Habkost }
639d176720dSEduardo Habkost 
640416e2d63SJody Belka static void native_machine_restart(char *__unused)
6414d022e35SMiguel Boton {
642c767a54bSJoe Perches 	pr_notice("machine restart\n");
6434d022e35SMiguel Boton 
6444d022e35SMiguel Boton 	if (!reboot_force)
6454d022e35SMiguel Boton 		machine_shutdown();
646d176720dSEduardo Habkost 	__machine_emergency_restart(0);
6474d022e35SMiguel Boton }
6484d022e35SMiguel Boton 
649416e2d63SJody Belka static void native_machine_halt(void)
6504d022e35SMiguel Boton {
651144d102bSMichael D Labriola 	/* Stop other cpus and apics */
652d3ec5caeSIvan Vecera 	machine_shutdown();
653d3ec5caeSIvan Vecera 
654840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
655840c2bafSJoseph Cihula 
656d3ec5caeSIvan Vecera 	stop_this_cpu(NULL);
6574d022e35SMiguel Boton }
6584d022e35SMiguel Boton 
659416e2d63SJody Belka static void native_machine_power_off(void)
6604d022e35SMiguel Boton {
6614d022e35SMiguel Boton 	if (pm_power_off) {
6624d022e35SMiguel Boton 		if (!reboot_force)
6634d022e35SMiguel Boton 			machine_shutdown();
6644d022e35SMiguel Boton 		pm_power_off();
6654d022e35SMiguel Boton 	}
666144d102bSMichael D Labriola 	/* A fallback in case there is no PM info available */
667840c2bafSJoseph Cihula 	tboot_shutdown(TB_SHUTDOWN_HALT);
6684d022e35SMiguel Boton }
6694d022e35SMiguel Boton 
6704d022e35SMiguel Boton struct machine_ops machine_ops = {
671416e2d63SJody Belka 	.power_off = native_machine_power_off,
672416e2d63SJody Belka 	.shutdown = native_machine_shutdown,
673416e2d63SJody Belka 	.emergency_restart = native_machine_emergency_restart,
674416e2d63SJody Belka 	.restart = native_machine_restart,
675ed23dc6fSGlauber Costa 	.halt = native_machine_halt,
6762965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
677ed23dc6fSGlauber Costa 	.crash_shutdown = native_machine_crash_shutdown,
678ed23dc6fSGlauber Costa #endif
6794d022e35SMiguel Boton };
680416e2d63SJody Belka 
681416e2d63SJody Belka void machine_power_off(void)
682416e2d63SJody Belka {
683416e2d63SJody Belka 	machine_ops.power_off();
684416e2d63SJody Belka }
685416e2d63SJody Belka 
686416e2d63SJody Belka void machine_shutdown(void)
687416e2d63SJody Belka {
688416e2d63SJody Belka 	machine_ops.shutdown();
689416e2d63SJody Belka }
690416e2d63SJody Belka 
691416e2d63SJody Belka void machine_emergency_restart(void)
692416e2d63SJody Belka {
693d176720dSEduardo Habkost 	__machine_emergency_restart(1);
694416e2d63SJody Belka }
695416e2d63SJody Belka 
696416e2d63SJody Belka void machine_restart(char *cmd)
697416e2d63SJody Belka {
698416e2d63SJody Belka 	machine_ops.restart(cmd);
699416e2d63SJody Belka }
700416e2d63SJody Belka 
701416e2d63SJody Belka void machine_halt(void)
702416e2d63SJody Belka {
703416e2d63SJody Belka 	machine_ops.halt();
704416e2d63SJody Belka }
705416e2d63SJody Belka 
7062965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
707ed23dc6fSGlauber Costa void machine_crash_shutdown(struct pt_regs *regs)
708ed23dc6fSGlauber Costa {
709ed23dc6fSGlauber Costa 	machine_ops.crash_shutdown(regs);
710ed23dc6fSGlauber Costa }
711ed23dc6fSGlauber Costa #endif
7122ddded21SEduardo Habkost 
7132ddded21SEduardo Habkost 
714bb8dd270SEduardo Habkost #if defined(CONFIG_SMP)
7152ddded21SEduardo Habkost 
7162ddded21SEduardo Habkost /* This keeps a track of which one is crashing cpu. */
7172ddded21SEduardo Habkost static int crashing_cpu;
7182ddded21SEduardo Habkost static nmi_shootdown_cb shootdown_callback;
7192ddded21SEduardo Habkost 
7202ddded21SEduardo Habkost static atomic_t waiting_for_crash_ipi;
72158c5661fSHidehiro Kawai static int crash_ipi_issued;
7222ddded21SEduardo Habkost 
7239c48f1c6SDon Zickus static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
7242ddded21SEduardo Habkost {
7252ddded21SEduardo Habkost 	int cpu;
7262ddded21SEduardo Habkost 
7272ddded21SEduardo Habkost 	cpu = raw_smp_processor_id();
7282ddded21SEduardo Habkost 
729144d102bSMichael D Labriola 	/*
730144d102bSMichael D Labriola 	 * Don't do anything if this handler is invoked on crashing cpu.
7312ddded21SEduardo Habkost 	 * Otherwise, system will completely hang. Crashing cpu can get
7322ddded21SEduardo Habkost 	 * an NMI if system was initially booted with nmi_watchdog parameter.
7332ddded21SEduardo Habkost 	 */
7342ddded21SEduardo Habkost 	if (cpu == crashing_cpu)
7359c48f1c6SDon Zickus 		return NMI_HANDLED;
7362ddded21SEduardo Habkost 	local_irq_disable();
7372ddded21SEduardo Habkost 
7389c48f1c6SDon Zickus 	shootdown_callback(cpu, regs);
7392ddded21SEduardo Habkost 
7402ddded21SEduardo Habkost 	atomic_dec(&waiting_for_crash_ipi);
7412ddded21SEduardo Habkost 	/* Assume hlt works */
7422ddded21SEduardo Habkost 	halt();
7432ddded21SEduardo Habkost 	for (;;)
7442ddded21SEduardo Habkost 		cpu_relax();
7452ddded21SEduardo Habkost 
7469c48f1c6SDon Zickus 	return NMI_HANDLED;
7472ddded21SEduardo Habkost }
7482ddded21SEduardo Habkost 
7492ddded21SEduardo Habkost static void smp_send_nmi_allbutself(void)
7502ddded21SEduardo Habkost {
751dac5f412SIngo Molnar 	apic->send_IPI_allbutself(NMI_VECTOR);
7522ddded21SEduardo Habkost }
7532ddded21SEduardo Habkost 
754144d102bSMichael D Labriola /*
755144d102bSMichael D Labriola  * Halt all other CPUs, calling the specified function on each of them
756bb8dd270SEduardo Habkost  *
757bb8dd270SEduardo Habkost  * This function can be used to halt all other CPUs on crash
758bb8dd270SEduardo Habkost  * or emergency reboot time. The function passed as parameter
759bb8dd270SEduardo Habkost  * will be called inside a NMI handler on all CPUs.
760bb8dd270SEduardo Habkost  */
7612ddded21SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
7622ddded21SEduardo Habkost {
7632ddded21SEduardo Habkost 	unsigned long msecs;
764c415b3dcSEduardo Habkost 	local_irq_disable();
7652ddded21SEduardo Habkost 
7662ddded21SEduardo Habkost 	/* Make a note of crashing cpu. Will be used in NMI callback. */
7672ddded21SEduardo Habkost 	crashing_cpu = safe_smp_processor_id();
7682ddded21SEduardo Habkost 
7692ddded21SEduardo Habkost 	shootdown_callback = callback;
7702ddded21SEduardo Habkost 
7712ddded21SEduardo Habkost 	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
7722ddded21SEduardo Habkost 	/* Would it be better to replace the trap vector here? */
7739c48f1c6SDon Zickus 	if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
7749c48f1c6SDon Zickus 				 NMI_FLAG_FIRST, "crash"))
775144d102bSMichael D Labriola 		return;		/* Return what? */
776144d102bSMichael D Labriola 	/*
777144d102bSMichael D Labriola 	 * Ensure the new callback function is set before sending
7782ddded21SEduardo Habkost 	 * out the NMI
7792ddded21SEduardo Habkost 	 */
7802ddded21SEduardo Habkost 	wmb();
7812ddded21SEduardo Habkost 
7822ddded21SEduardo Habkost 	smp_send_nmi_allbutself();
7832ddded21SEduardo Habkost 
78458c5661fSHidehiro Kawai 	/* Kick CPUs looping in NMI context. */
78558c5661fSHidehiro Kawai 	WRITE_ONCE(crash_ipi_issued, 1);
78658c5661fSHidehiro Kawai 
7872ddded21SEduardo Habkost 	msecs = 1000; /* Wait at most a second for the other cpus to stop */
7882ddded21SEduardo Habkost 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
7892ddded21SEduardo Habkost 		mdelay(1);
7902ddded21SEduardo Habkost 		msecs--;
7912ddded21SEduardo Habkost 	}
7922ddded21SEduardo Habkost 
7932ddded21SEduardo Habkost 	/* Leave the nmi callback set */
7942ddded21SEduardo Habkost }
79558c5661fSHidehiro Kawai 
796*b279d67dSHidehiro Kawai /*
797*b279d67dSHidehiro Kawai  * Check if the crash dumping IPI got issued and if so, call its callback
798*b279d67dSHidehiro Kawai  * directly. This function is used when we have already been in NMI handler.
799*b279d67dSHidehiro Kawai  * It doesn't return.
800*b279d67dSHidehiro Kawai  */
801*b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
802*b279d67dSHidehiro Kawai {
803*b279d67dSHidehiro Kawai 	if (crash_ipi_issued)
804*b279d67dSHidehiro Kawai 		crash_nmi_callback(0, regs);
805*b279d67dSHidehiro Kawai }
806*b279d67dSHidehiro Kawai 
80758c5661fSHidehiro Kawai /* Override the weak function in kernel/panic.c */
80858c5661fSHidehiro Kawai void nmi_panic_self_stop(struct pt_regs *regs)
80958c5661fSHidehiro Kawai {
81058c5661fSHidehiro Kawai 	while (1) {
811*b279d67dSHidehiro Kawai 		/* If no CPU is preparing crash dump, we simply loop here. */
812*b279d67dSHidehiro Kawai 		run_crash_ipi_callback(regs);
81358c5661fSHidehiro Kawai 		cpu_relax();
81458c5661fSHidehiro Kawai 	}
81558c5661fSHidehiro Kawai }
81658c5661fSHidehiro Kawai 
817bb8dd270SEduardo Habkost #else /* !CONFIG_SMP */
818bb8dd270SEduardo Habkost void nmi_shootdown_cpus(nmi_shootdown_cb callback)
819bb8dd270SEduardo Habkost {
820bb8dd270SEduardo Habkost 	/* No other CPUs to shoot down */
821bb8dd270SEduardo Habkost }
822*b279d67dSHidehiro Kawai 
823*b279d67dSHidehiro Kawai void run_crash_ipi_callback(struct pt_regs *regs)
824*b279d67dSHidehiro Kawai {
825*b279d67dSHidehiro Kawai }
8262ddded21SEduardo Habkost #endif
827