reboot.c 21 KB

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