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