reboot.c 23 KB

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