reboot.c 19 KB

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