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