reboot.c 22 KB

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