reboot.c 20 KB

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