reboot.c 22 KB

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