reboot.c 22 KB

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