reboot.c 20 KB

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