reboot.c 20 KB

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