reboot.c 18 KB

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