]> Git Repo - linux.git/blame - arch/x86/kernel/reboot.c
Merge remote-tracking branches 'asoc/fix/topology' and 'asoc/fix/wm8998' into asoc...
[linux.git] / arch / x86 / kernel / reboot.c
CommitLineData
c767a54b
JP
1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
186f4360 3#include <linux/export.h>
cd6ed525 4#include <linux/reboot.h>
4d022e35
MB
5#include <linux/init.h>
6#include <linux/pm.h>
7#include <linux/efi.h>
6c6c51e4 8#include <linux/dmi.h>
d43c36dc 9#include <linux/sched.h>
69575d38 10#include <linux/tboot.h>
ca444564 11#include <linux/delay.h>
c207aee4 12#include <linux/frame.h>
4d022e35
MB
13#include <acpi/reboot.h>
14#include <asm/io.h>
1da177e4 15#include <asm/apic.h>
8643e28d 16#include <asm/io_apic.h>
4d37e7e3 17#include <asm/desc.h>
4d022e35 18#include <asm/hpet.h>
68db065c 19#include <asm/pgtable.h>
4412620f 20#include <asm/proto.h>
973efae2 21#include <asm/reboot_fixups.h>
07f3331c 22#include <asm/reboot.h>
82487711 23#include <asm/pci_x86.h>
d176720d 24#include <asm/virtext.h>
96b89dc6 25#include <asm/cpu.h>
c410b830 26#include <asm/nmi.h>
65051397 27#include <asm/smp.h>
1da177e4 28
65051397
PA
29#include <linux/ctype.h>
30#include <linux/mc146818rtc.h>
31#include <asm/realmode.h>
32#include <asm/x86_init.h>
44be28e9 33#include <asm/efi.h>
4d022e35 34
1da177e4
LT
35/*
36 * Power off function, if any
37 */
38void (*pm_power_off)(void);
129f6946 39EXPORT_SYMBOL(pm_power_off);
1da177e4 40
144d102b
ML
41/*
42 * This is set if we need to go through the 'emergency' path.
d176720d
EH
43 * When machine_emergency_restart() is called, we may be on
44 * an inconsistent state and won't be able to do a clean cleanup
45 */
46static int reboot_emergency;
47
14d7ca5c
PA
48/* This is set by the PCI code if either type 1 or type 2 PCI is detected */
49bool port_cf9_safe = false;
50
1da177e4
LT
51/*
52 * Reboot options and system auto-detection code provided by
53 * Dell Inc. so their systems "just work". :-)
54 */
55
4d581259
AH
56/*
57 * Some machines require the "reboot=a" commandline options
58 */
59static int __init set_acpi_reboot(const struct dmi_system_id *d)
60{
61 if (reboot_type != BOOT_ACPI) {
62 reboot_type = BOOT_ACPI;
63 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
64 d->ident, "ACPI");
65 }
66 return 0;
67}
68
1da177e4 69/*
1ef03890 70 * Some machines require the "reboot=b" or "reboot=k" commandline options,
4d022e35 71 * this quirk makes that automatic.
1da177e4 72 */
1855256c 73static int __init set_bios_reboot(const struct dmi_system_id *d)
1da177e4 74{
4d022e35
MB
75 if (reboot_type != BOOT_BIOS) {
76 reboot_type = BOOT_BIOS;
c767a54b 77 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
6d9153bb 78 d->ident, "BIOS");
1da177e4
LT
79 }
80 return 0;
81}
82
65051397 83void __noreturn machine_real_restart(unsigned int type)
57b16594 84{
57b16594
ML
85 local_irq_disable();
86
144d102b
ML
87 /*
88 * Write zero to CMOS register number 0x0f, which the BIOS POST
89 * routine will recognize as telling it to do a proper reboot. (Well
90 * that's what this book in front of me says -- it may only apply to
91 * the Phoenix BIOS though, it's not clear). At the same time,
92 * disable NMIs by setting the top bit in the CMOS address register,
93 * as we're about to do peculiar things to the CPU. I'm not sure if
94 * `outb_p' is needed instead of just `outb'. Use it to be on the
95 * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
57b16594
ML
96 */
97 spin_lock(&rtc_lock);
98 CMOS_WRITE(0x00, 0x8f);
99 spin_unlock(&rtc_lock);
100
101 /*
102 * Switch back to the initial page table.
103 */
65051397 104#ifdef CONFIG_X86_32
57b16594 105 load_cr3(initial_page_table);
65051397
PA
106#else
107 write_cr3(real_mode_header->trampoline_pgd);
924c6b90
AL
108
109 /* Exiting long mode will fail if CR4.PCIDE is set. */
110 if (static_cpu_has(X86_FEATURE_PCID))
111 cr4_clear_bits(X86_CR4_PCIDE);
65051397 112#endif
57b16594 113
57b16594 114 /* Jump to the identity-mapped low memory code */
65051397
PA
115#ifdef CONFIG_X86_32
116 asm volatile("jmpl *%0" : :
117 "rm" (real_mode_header->machine_real_restart_asm),
118 "a" (type));
119#else
120 asm volatile("ljmpl *%0" : :
121 "m" (real_mode_header->machine_real_restart_asm),
122 "D" (type));
123#endif
124 unreachable();
57b16594
ML
125}
126#ifdef CONFIG_APM_MODULE
127EXPORT_SYMBOL(machine_real_restart);
128#endif
c207aee4 129STACK_FRAME_NON_STANDARD(machine_real_restart);
57b16594 130
57b16594
ML
131/*
132 * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
133 */
134static int __init set_pci_reboot(const struct dmi_system_id *d)
135{
5be44a6f
IM
136 if (reboot_type != BOOT_CF9_FORCE) {
137 reboot_type = BOOT_CF9_FORCE;
c767a54b 138 pr_info("%s series board detected. Selecting %s-method for reboots.\n",
6d9153bb 139 d->ident, "PCI");
57b16594
ML
140 }
141 return 0;
142}
143
1ef03890
PC
144static int __init set_kbd_reboot(const struct dmi_system_id *d)
145{
146 if (reboot_type != BOOT_KBD) {
147 reboot_type = BOOT_KBD;
c767a54b 148 pr_info("%s series board detected. Selecting %s-method for reboot.\n",
6d9153bb 149 d->ident, "KBD");
1ef03890
PC
150 }
151 return 0;
152}
153
144d102b 154/*
65051397 155 * This is a single dmi_table handling all reboot quirks.
57b16594 156 */
6faadbbb 157static const struct dmi_system_id reboot_dmi_table[] __initconst = {
e56e57f6
DJ
158
159 /* Acer */
160 { /* Handle reboot issue on Acer Aspire one */
161 .callback = set_kbd_reboot,
162 .ident = "Acer Aspire One A110",
b9e82af8 163 .matches = {
e56e57f6
DJ
164 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
165 DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
b9e82af8
TG
166 },
167 },
e56e57f6
DJ
168
169 /* Apple */
170 { /* Handle problems with rebooting on Apple MacBook5 */
171 .callback = set_pci_reboot,
172 .ident = "Apple MacBook5",
1da177e4 173 .matches = {
e56e57f6
DJ
174 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
175 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
1da177e4
LT
176 },
177 },
e56e57f6
DJ
178 { /* Handle problems with rebooting on Apple MacBookPro5 */
179 .callback = set_pci_reboot,
180 .ident = "Apple MacBookPro5",
1da177e4 181 .matches = {
e56e57f6
DJ
182 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
183 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
1da177e4
LT
184 },
185 },
e56e57f6
DJ
186 { /* Handle problems with rebooting on Apple Macmini3,1 */
187 .callback = set_pci_reboot,
188 .ident = "Apple Macmini3,1",
df2edcf3 189 .matches = {
e56e57f6
DJ
190 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
191 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
df2edcf3
JJ
192 },
193 },
e56e57f6
DJ
194 { /* Handle problems with rebooting on the iMac9,1. */
195 .callback = set_pci_reboot,
196 .ident = "Apple iMac9,1",
fc115bf1 197 .matches = {
e56e57f6
DJ
198 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
199 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
fc115bf1
CK
200 },
201 },
2f0c0b2d
MK
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 },
e56e57f6 210
80313b30
SLH
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
e56e57f6
DJ
221 /* ASUS */
222 { /* Handle problems with rebooting on ASUS P4S800 */
fc1c8925 223 .callback = set_bios_reboot,
e56e57f6 224 .ident = "ASUS P4S800",
fc1c8925 225 .matches = {
e56e57f6
DJ
226 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
227 DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
fc1c8925
HAA
228 },
229 },
90b28ded
MH
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."),
bba8376a 235 DMI_MATCH(DMI_PRODUCT_NAME, "X205TA"),
90b28ded
MH
236 },
237 },
3b3e7855
MH
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 },
e56e57f6 246
aadca6fa
CG
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
e56e57f6
DJ
257 /* Dell */
258 { /* Handle problems with rebooting on Dell DXP061 */
093bac15 259 .callback = set_bios_reboot,
e56e57f6 260 .ident = "Dell DXP061",
093bac15
SC
261 .matches = {
262 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 263 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
093bac15
SC
264 },
265 },
e56e57f6 266 { /* Handle problems with rebooting on Dell E520's */
4a4aca64 267 .callback = set_bios_reboot,
e56e57f6 268 .ident = "Dell E520",
4a4aca64
JD
269 .matches = {
270 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 271 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
4a4aca64
JD
272 },
273 },
986189f9
LT
274 { /* Handle problems with rebooting on the Latitude E5410. */
275 .callback = set_pci_reboot,
276 .ident = "Dell Latitude E5410",
35ea63d7
LO
277 .matches = {
278 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
986189f9 279 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
35ea63d7
LO
280 },
281 },
e56e57f6
DJ
282 { /* Handle problems with rebooting on the Latitude E5420. */
283 .callback = set_pci_reboot,
284 .ident = "Dell Latitude E5420",
1da177e4 285 .matches = {
35ea63d7 286 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 287 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
1da177e4
LT
288 },
289 },
e56e57f6
DJ
290 { /* Handle problems with rebooting on the Latitude E6320. */
291 .callback = set_pci_reboot,
292 .ident = "Dell Latitude E6320",
fab3b58d
IM
293 .matches = {
294 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 295 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
fab3b58d
IM
296 },
297 },
e56e57f6
DJ
298 { /* Handle problems with rebooting on the Latitude E6420. */
299 .callback = set_pci_reboot,
300 .ident = "Dell Latitude E6420",
890ffedc
TB
301 .matches = {
302 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 303 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
890ffedc
TB
304 },
305 },
e56e57f6 306 { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
d91b14c4 307 .callback = set_bios_reboot,
e56e57f6 308 .ident = "Dell OptiPlex 330",
d91b14c4 309 .matches = {
890ffedc 310 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6
DJ
311 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
312 DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
d91b14c4
TV
313 },
314 },
e56e57f6 315 { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
dd4124a8 316 .callback = set_bios_reboot,
e56e57f6 317 .ident = "Dell OptiPlex 360",
dd4124a8
LO
318 .matches = {
319 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6
DJ
320 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
321 DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
dd4124a8
LO
322 },
323 },
e56e57f6 324 { /* Handle problems with rebooting on Dell Optiplex 745's SFF */
c5da9a2b 325 .callback = set_bios_reboot,
e56e57f6 326 .ident = "Dell OptiPlex 745",
c5da9a2b
AC
327 .matches = {
328 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 329 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
c5da9a2b
AC
330 },
331 },
e56e57f6 332 { /* Handle problems with rebooting on Dell Optiplex 745's DFF */
88dff493 333 .callback = set_bios_reboot,
e56e57f6 334 .ident = "Dell OptiPlex 745",
88dff493 335 .matches = {
c5da9a2b 336 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6
DJ
337 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
338 DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
88dff493
ZR
339 },
340 },
e56e57f6 341 { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
4832ddda 342 .callback = set_bios_reboot,
e56e57f6 343 .ident = "Dell OptiPlex 745",
b49c78d4 344 .matches = {
e56e57f6
DJ
345 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
346 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
347 DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
b49c78d4
PC
348 },
349 },
e56e57f6 350 { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
4832ddda 351 .callback = set_bios_reboot,
e56e57f6 352 .ident = "Dell OptiPlex 760",
6c6c51e4 353 .matches = {
e56e57f6
DJ
354 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
355 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
356 DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
6c6c51e4
PM
357 },
358 },
e56e57f6 359 { /* Handle problems with rebooting on the OptiPlex 990. */
498cdbfb 360 .callback = set_pci_reboot,
e56e57f6 361 .ident = "Dell OptiPlex 990",
498cdbfb 362 .matches = {
e56e57f6
DJ
363 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
364 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
498cdbfb
OÇ
365 },
366 },
e56e57f6
DJ
367 { /* Handle problems with rebooting on Dell 300's */
368 .callback = set_bios_reboot,
369 .ident = "Dell PowerEdge 300",
05154752 370 .matches = {
e56e57f6
DJ
371 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
372 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
05154752
GH
373 },
374 },
e56e57f6
DJ
375 { /* Handle problems with rebooting on Dell 1300's */
376 .callback = set_bios_reboot,
377 .ident = "Dell PowerEdge 1300",
0a832320 378 .matches = {
e56e57f6
DJ
379 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
380 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
0a832320
JM
381 },
382 },
e56e57f6
DJ
383 { /* Handle problems with rebooting on Dell 2400's */
384 .callback = set_bios_reboot,
385 .ident = "Dell PowerEdge 2400",
3628c3f5 386 .matches = {
e56e57f6
DJ
387 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
388 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
3628c3f5
MR
389 },
390 },
e56e57f6 391 { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
8412da75 392 .callback = set_pci_reboot,
e56e57f6 393 .ident = "Dell PowerEdge C6100",
8412da75 394 .matches = {
e56e57f6
DJ
395 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
396 DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
8412da75
VS
397 },
398 },
e56e57f6 399 { /* Handle problems with rebooting on the Precision M6600. */
b7798d28 400 .callback = set_pci_reboot,
e56e57f6 401 .ident = "Dell Precision M6600",
b7798d28
DB
402 .matches = {
403 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 404 DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
b7798d28
DB
405 },
406 },
e56e57f6
DJ
407 { /* Handle problems with rebooting on Dell T5400's */
408 .callback = set_bios_reboot,
409 .ident = "Dell Precision T5400",
a536877e
PA
410 .matches = {
411 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 412 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
a536877e
PA
413 },
414 },
e56e57f6
DJ
415 { /* Handle problems with rebooting on Dell T7400's */
416 .callback = set_bios_reboot,
417 .ident = "Dell Precision T7400",
6be30bb7
RW
418 .matches = {
419 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 420 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
6be30bb7
RW
421 },
422 },
e56e57f6
DJ
423 { /* Handle problems with rebooting on Dell XPS710 */
424 .callback = set_bios_reboot,
425 .ident = "Dell XPS710",
76eb9a30
ZR
426 .matches = {
427 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
e56e57f6 428 DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
76eb9a30
ZR
429 },
430 },
4d581259
AH
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 },
e56e57f6
DJ
439
440 /* Hewlett-Packard */
441 { /* Handle problems with rebooting on HP laptops */
442 .callback = set_bios_reboot,
443 .ident = "HP Compaq Laptop",
4f0acd31 444 .matches = {
e56e57f6
DJ
445 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
446 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
4f0acd31
MS
447 },
448 },
e56e57f6
DJ
449
450 /* Sony */
451 { /* Handle problems with rebooting on Sony VGN-Z540N */
452 .callback = set_bios_reboot,
453 .ident = "Sony VGN-Z540N",
4f0acd31 454 .matches = {
e56e57f6
DJ
455 DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
4f0acd31
MS
457 },
458 },
e56e57f6 459
6c6c51e4
PM
460 { }
461};
462
57b16594 463static int __init reboot_init(void)
6c6c51e4 464{
44be28e9
MF
465 int rv;
466
144d102b
ML
467 /*
468 * Only do the DMI check if reboot_type hasn't been overridden
5955633e
ML
469 * on the command line
470 */
44be28e9
MF
471 if (!reboot_default)
472 return 0;
473
474 /*
475 * The DMI quirks table takes precedence. If no quirks entry
4ecf7191
SA
476 * matches and the ACPI Hardware Reduced bit is set and EFI
477 * runtime services are enabled, force EFI reboot.
44be28e9
MF
478 */
479 rv = dmi_check_system(reboot_dmi_table);
480
4ecf7191 481 if (!rv && efi_reboot_required() && !efi_runtime_disabled())
44be28e9
MF
482 reboot_type = BOOT_EFI;
483
6c6c51e4
PM
484 return 0;
485}
57b16594 486core_initcall(reboot_init);
6c6c51e4 487
4d022e35
MB
488static inline void kb_wait(void)
489{
490 int i;
491
c84d6af8
AC
492 for (i = 0; i < 0x10000; i++) {
493 if ((inb(0x64) & 0x02) == 0)
4d022e35 494 break;
c84d6af8
AC
495 udelay(2);
496 }
4d022e35
MB
497}
498
9c48f1c6 499static void vmxoff_nmi(int cpu, struct pt_regs *regs)
d176720d
EH
500{
501 cpu_emergency_vmxoff();
502}
503
144d102b 504/* Use NMIs as IPIs to tell all CPUs to disable virtualization */
d176720d
EH
505static void emergency_vmx_disable_all(void)
506{
507 /* Just make sure we won't change CPUs while doing this */
508 local_irq_disable();
509
144d102b
ML
510 /*
511 * We need to disable VMX on all CPUs before rebooting, otherwise
d176720d
EH
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()) {
144d102b 530 /* Disable VMX on this CPU. */
d176720d
EH
531 cpu_vmxoff();
532
533 /* Halt and disable VMX on the other CPUs */
534 nmi_shootdown_cpus(vmxoff_nmi);
535
536 }
537}
538
539
7432d149
IM
540void __attribute__((weak)) mach_reboot_fixups(void)
541{
542}
543
660e34ce 544/*
5be44a6f
IM
545 * To the best of our knowledge Windows compatible x86 hardware expects
546 * the following on reboot:
660e34ce
MG
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
a4f1987e 552 * 5) If still alive, call the EFI runtime service to reboot
5be44a6f 553 * 6) If no EFI runtime service, call the BIOS to do a reboot
660e34ce 554 *
5be44a6f
IM
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.
660e34ce 562 */
416e2d63 563static void native_machine_emergency_restart(void)
1da177e4 564{
4d022e35 565 int i;
660e34ce
MG
566 int attempt = 0;
567 int orig_reboot_type = reboot_type;
edf2b139 568 unsigned short mode;
4d022e35 569
d176720d
EH
570 if (reboot_emergency)
571 emergency_vmx_disable_all();
572
840c2baf
JC
573 tboot_shutdown(TB_SHUTDOWN_REBOOT);
574
4d022e35 575 /* Tell the BIOS if we want cold or warm reboot */
edf2b139
RH
576 mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
577 *((unsigned short *)__va(0x472)) = mode;
4d022e35 578
87615a34
MF
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
4d022e35
MB
588 for (;;) {
589 /* Could also try the reset bit in the Hammer NB */
590 switch (reboot_type) {
5be44a6f
IM
591 case BOOT_ACPI:
592 acpi_reboot();
593 reboot_type = BOOT_KBD;
594 break;
595
4d022e35 596 case BOOT_KBD:
144d102b 597 mach_reboot_fixups(); /* For board specific fixups */
7432d149 598
4d022e35
MB
599 for (i = 0; i < 10; i++) {
600 kb_wait();
601 udelay(50);
144d102b 602 outb(0xfe, 0x64); /* Pulse reset low */
4d022e35
MB
603 udelay(50);
604 }
660e34ce
MG
605 if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
606 attempt = 1;
607 reboot_type = BOOT_ACPI;
608 } else {
a4f1987e 609 reboot_type = BOOT_EFI;
660e34ce
MG
610 }
611 break;
4d022e35 612
4d022e35 613 case BOOT_EFI:
8562c99c 614 efi_reboot(reboot_mode, NULL);
5be44a6f
IM
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;
14d7ca5c 623 break;
4d022e35 624
5be44a6f 625 case BOOT_CF9_FORCE:
14d7ca5c 626 port_cf9_safe = true;
144d102b 627 /* Fall through */
4d022e35 628
5be44a6f 629 case BOOT_CF9_SAFE:
14d7ca5c 630 if (port_cf9_safe) {
5be44a6f 631 u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
16c21ae5 632 u8 cf9 = inb(0xcf9) & ~reboot_code;
14d7ca5c
PA
633 outb(cf9|2, 0xcf9); /* Request hard reset */
634 udelay(50);
16c21ae5
LF
635 /* Actually do the reset */
636 outb(cf9|reboot_code, 0xcf9);
14d7ca5c
PA
637 udelay(50);
638 }
5be44a6f
IM
639 reboot_type = BOOT_TRIPLE;
640 break;
641
642 case BOOT_TRIPLE:
e802a51e 643 idt_invalidate(NULL);
5be44a6f
IM
644 __asm__ __volatile__("int3");
645
646 /* We're probably dead after this, but... */
647 reboot_type = BOOT_KBD;
4d022e35
MB
648 break;
649 }
650 }
651}
652
3c62c625 653void native_machine_shutdown(void)
4d022e35
MB
654{
655 /* Stop the cpus and apics */
522e6646 656#ifdef CONFIG_X86_IO_APIC
2885432a
FY
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 */
522e6646
FY
668 disable_IO_APIC();
669#endif
670
1da177e4 671#ifdef CONFIG_SMP
144d102b 672 /*
1b3a5d02
RH
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.
1da177e4 676 */
55c844a4 677 local_irq_disable();
76fac077 678 stop_other_cpus();
4d022e35 679#endif
1da177e4
LT
680
681 lapic_shutdown();
682
c86c7fbc
OH
683#ifdef CONFIG_HPET_TIMER
684 hpet_disable();
685#endif
dd2a1305 686
4d022e35 687#ifdef CONFIG_X86_64
338bac52 688 x86_platform.iommu_shutdown();
4d022e35 689#endif
973efae2
JF
690}
691
d176720d
EH
692static void __machine_emergency_restart(int emergency)
693{
694 reboot_emergency = emergency;
695 machine_ops.emergency_restart();
696}
697
416e2d63 698static void native_machine_restart(char *__unused)
dd2a1305 699{
c767a54b 700 pr_notice("machine restart\n");
1da177e4 701
4d022e35
MB
702 if (!reboot_force)
703 machine_shutdown();
d176720d 704 __machine_emergency_restart(0);
4a1421f8
EB
705}
706
416e2d63 707static void native_machine_halt(void)
1da177e4 708{
144d102b 709 /* Stop other cpus and apics */
d3ec5cae
IV
710 machine_shutdown();
711
840c2baf
JC
712 tboot_shutdown(TB_SHUTDOWN_HALT);
713
d3ec5cae 714 stop_this_cpu(NULL);
1da177e4
LT
715}
716
416e2d63 717static void native_machine_power_off(void)
1da177e4 718{
6e3fbee5 719 if (pm_power_off) {
4d022e35
MB
720 if (!reboot_force)
721 machine_shutdown();
1da177e4 722 pm_power_off();
6e3fbee5 723 }
144d102b 724 /* A fallback in case there is no PM info available */
840c2baf 725 tboot_shutdown(TB_SHUTDOWN_HALT);
1da177e4
LT
726}
727
404f6aac 728struct machine_ops machine_ops __ro_after_init = {
416e2d63
JB
729 .power_off = native_machine_power_off,
730 .shutdown = native_machine_shutdown,
731 .emergency_restart = native_machine_emergency_restart,
732 .restart = native_machine_restart,
ed23dc6f 733 .halt = native_machine_halt,
2965faa5 734#ifdef CONFIG_KEXEC_CORE
ed23dc6f
GC
735 .crash_shutdown = native_machine_crash_shutdown,
736#endif
07f3331c 737};
416e2d63
JB
738
739void machine_power_off(void)
740{
741 machine_ops.power_off();
742}
743
744void machine_shutdown(void)
745{
746 machine_ops.shutdown();
747}
748
749void machine_emergency_restart(void)
750{
d176720d 751 __machine_emergency_restart(1);
416e2d63
JB
752}
753
754void machine_restart(char *cmd)
755{
756 machine_ops.restart(cmd);
757}
758
759void machine_halt(void)
760{
761 machine_ops.halt();
762}
763
2965faa5 764#ifdef CONFIG_KEXEC_CORE
ed23dc6f
GC
765void machine_crash_shutdown(struct pt_regs *regs)
766{
767 machine_ops.crash_shutdown(regs);
768}
769#endif
2ddded21
EH
770
771
5bc32950
XP
772/* This is the CPU performing the emergency shutdown work. */
773int crashing_cpu = -1;
774
bb8dd270 775#if defined(CONFIG_SMP)
2ddded21 776
2ddded21
EH
777static nmi_shootdown_cb shootdown_callback;
778
779static atomic_t waiting_for_crash_ipi;
58c5661f 780static int crash_ipi_issued;
2ddded21 781
9c48f1c6 782static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
2ddded21
EH
783{
784 int cpu;
785
2ddded21
EH
786 cpu = raw_smp_processor_id();
787
144d102b
ML
788 /*
789 * Don't do anything if this handler is invoked on crashing cpu.
2ddded21
EH
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)
9c48f1c6 794 return NMI_HANDLED;
2ddded21
EH
795 local_irq_disable();
796
9c48f1c6 797 shootdown_callback(cpu, regs);
2ddded21
EH
798
799 atomic_dec(&waiting_for_crash_ipi);
800 /* Assume hlt works */
801 halt();
802 for (;;)
803 cpu_relax();
804
9c48f1c6 805 return NMI_HANDLED;
2ddded21
EH
806}
807
808static void smp_send_nmi_allbutself(void)
809{
dac5f412 810 apic->send_IPI_allbutself(NMI_VECTOR);
2ddded21
EH
811}
812
144d102b
ML
813/*
814 * Halt all other CPUs, calling the specified function on each of them
bb8dd270
EH
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 */
2ddded21
EH
820void nmi_shootdown_cpus(nmi_shootdown_cb callback)
821{
822 unsigned long msecs;
c415b3dc 823 local_irq_disable();
2ddded21 824
144d102b 825 /* Make a note of crashing cpu. Will be used in NMI callback. */
2ddded21
EH
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? */
9c48f1c6
DZ
832 if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
833 NMI_FLAG_FIRST, "crash"))
144d102b
ML
834 return; /* Return what? */
835 /*
836 * Ensure the new callback function is set before sending
2ddded21
EH
837 * out the NMI
838 */
839 wmb();
840
841 smp_send_nmi_allbutself();
842
58c5661f
HK
843 /* Kick CPUs looping in NMI context. */
844 WRITE_ONCE(crash_ipi_issued, 1);
845
2ddded21
EH
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}
58c5661f 854
b279d67d
HK
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 */
860void run_crash_ipi_callback(struct pt_regs *regs)
861{
862 if (crash_ipi_issued)
863 crash_nmi_callback(0, regs);
864}
865
58c5661f
HK
866/* Override the weak function in kernel/panic.c */
867void nmi_panic_self_stop(struct pt_regs *regs)
868{
869 while (1) {
b279d67d
HK
870 /* If no CPU is preparing crash dump, we simply loop here. */
871 run_crash_ipi_callback(regs);
58c5661f
HK
872 cpu_relax();
873 }
874}
875
bb8dd270
EH
876#else /* !CONFIG_SMP */
877void nmi_shootdown_cpus(nmi_shootdown_cb callback)
878{
879 /* No other CPUs to shoot down */
880}
b279d67d
HK
881
882void run_crash_ipi_callback(struct pt_regs *regs)
883{
884}
2ddded21 885#endif
This page took 2.256495 seconds and 4 git commands to generate.