reboot.c 19 KB

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