reboot.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 OptiPlex 990",
  340. .matches = {
  341. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  342. DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
  343. },
  344. },
  345. { }
  346. };
  347. static int __init reboot_init(void)
  348. {
  349. /*
  350. * Only do the DMI check if reboot_type hasn't been overridden
  351. * on the command line
  352. */
  353. if (reboot_default)
  354. dmi_check_system(reboot_dmi_table);
  355. return 0;
  356. }
  357. core_initcall(reboot_init);
  358. static inline void kb_wait(void)
  359. {
  360. int i;
  361. for (i = 0; i < 0x10000; i++) {
  362. if ((inb(0x64) & 0x02) == 0)
  363. break;
  364. udelay(2);
  365. }
  366. }
  367. static void vmxoff_nmi(int cpu, struct pt_regs *regs)
  368. {
  369. cpu_emergency_vmxoff();
  370. }
  371. /* Use NMIs as IPIs to tell all CPUs to disable virtualization */
  372. static void emergency_vmx_disable_all(void)
  373. {
  374. /* Just make sure we won't change CPUs while doing this */
  375. local_irq_disable();
  376. /*
  377. * We need to disable VMX on all CPUs before rebooting, otherwise
  378. * we risk hanging up the machine, because the CPU ignore INIT
  379. * signals when VMX is enabled.
  380. *
  381. * We can't take any locks and we may be on an inconsistent
  382. * state, so we use NMIs as IPIs to tell the other CPUs to disable
  383. * VMX and halt.
  384. *
  385. * For safety, we will avoid running the nmi_shootdown_cpus()
  386. * stuff unnecessarily, but we don't have a way to check
  387. * if other CPUs have VMX enabled. So we will call it only if the
  388. * CPU we are running on has VMX enabled.
  389. *
  390. * We will miss cases where VMX is not enabled on all CPUs. This
  391. * shouldn't do much harm because KVM always enable VMX on all
  392. * CPUs anyway. But we can miss it on the small window where KVM
  393. * is still enabling VMX.
  394. */
  395. if (cpu_has_vmx() && cpu_vmx_enabled()) {
  396. /* Disable VMX on this CPU. */
  397. cpu_vmxoff();
  398. /* Halt and disable VMX on the other CPUs */
  399. nmi_shootdown_cpus(vmxoff_nmi);
  400. }
  401. }
  402. void __attribute__((weak)) mach_reboot_fixups(void)
  403. {
  404. }
  405. /*
  406. * Windows compatible x86 hardware expects the following on reboot:
  407. *
  408. * 1) If the FADT has the ACPI reboot register flag set, try it
  409. * 2) If still alive, write to the keyboard controller
  410. * 3) If still alive, write to the ACPI reboot register again
  411. * 4) If still alive, write to the keyboard controller again
  412. *
  413. * If the machine is still alive at this stage, it gives up. We default to
  414. * following the same pattern, except that if we're still alive after (4) we'll
  415. * try to force a triple fault and then cycle between hitting the keyboard
  416. * controller and doing that
  417. */
  418. static void native_machine_emergency_restart(void)
  419. {
  420. int i;
  421. int attempt = 0;
  422. int orig_reboot_type = reboot_type;
  423. unsigned short mode;
  424. if (reboot_emergency)
  425. emergency_vmx_disable_all();
  426. tboot_shutdown(TB_SHUTDOWN_REBOOT);
  427. /* Tell the BIOS if we want cold or warm reboot */
  428. mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
  429. *((unsigned short *)__va(0x472)) = mode;
  430. for (;;) {
  431. /* Could also try the reset bit in the Hammer NB */
  432. switch (reboot_type) {
  433. case BOOT_KBD:
  434. mach_reboot_fixups(); /* For board specific fixups */
  435. for (i = 0; i < 10; i++) {
  436. kb_wait();
  437. udelay(50);
  438. outb(0xfe, 0x64); /* Pulse reset low */
  439. udelay(50);
  440. }
  441. if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
  442. attempt = 1;
  443. reboot_type = BOOT_ACPI;
  444. } else {
  445. reboot_type = BOOT_TRIPLE;
  446. }
  447. break;
  448. case BOOT_TRIPLE:
  449. load_idt(&no_idt);
  450. __asm__ __volatile__("int3");
  451. reboot_type = BOOT_KBD;
  452. break;
  453. case BOOT_BIOS:
  454. machine_real_restart(MRR_BIOS);
  455. reboot_type = BOOT_KBD;
  456. break;
  457. case BOOT_ACPI:
  458. acpi_reboot();
  459. reboot_type = BOOT_KBD;
  460. break;
  461. case BOOT_EFI:
  462. if (efi_enabled(EFI_RUNTIME_SERVICES))
  463. efi.reset_system(reboot_mode == REBOOT_WARM ?
  464. EFI_RESET_WARM :
  465. EFI_RESET_COLD,
  466. EFI_SUCCESS, 0, NULL);
  467. reboot_type = BOOT_KBD;
  468. break;
  469. case BOOT_CF9:
  470. port_cf9_safe = true;
  471. /* Fall through */
  472. case BOOT_CF9_COND:
  473. if (port_cf9_safe) {
  474. u8 cf9 = inb(0xcf9) & ~6;
  475. outb(cf9|2, 0xcf9); /* Request hard reset */
  476. udelay(50);
  477. outb(cf9|6, 0xcf9); /* Actually do the reset */
  478. udelay(50);
  479. }
  480. reboot_type = BOOT_KBD;
  481. break;
  482. }
  483. }
  484. }
  485. void native_machine_shutdown(void)
  486. {
  487. /* Stop the cpus and apics */
  488. #ifdef CONFIG_SMP
  489. /*
  490. * Stop all of the others. Also disable the local irq to
  491. * not receive the per-cpu timer interrupt which may trigger
  492. * scheduler's load balance.
  493. */
  494. local_irq_disable();
  495. stop_other_cpus();
  496. #endif
  497. lapic_shutdown();
  498. #ifdef CONFIG_X86_IO_APIC
  499. disable_IO_APIC();
  500. #endif
  501. #ifdef CONFIG_HPET_TIMER
  502. hpet_disable();
  503. #endif
  504. #ifdef CONFIG_X86_64
  505. x86_platform.iommu_shutdown();
  506. #endif
  507. }
  508. static void __machine_emergency_restart(int emergency)
  509. {
  510. reboot_emergency = emergency;
  511. machine_ops.emergency_restart();
  512. }
  513. static void native_machine_restart(char *__unused)
  514. {
  515. pr_notice("machine restart\n");
  516. if (!reboot_force)
  517. machine_shutdown();
  518. __machine_emergency_restart(0);
  519. }
  520. static void native_machine_halt(void)
  521. {
  522. /* Stop other cpus and apics */
  523. machine_shutdown();
  524. tboot_shutdown(TB_SHUTDOWN_HALT);
  525. stop_this_cpu(NULL);
  526. }
  527. static void native_machine_power_off(void)
  528. {
  529. if (pm_power_off) {
  530. if (!reboot_force)
  531. machine_shutdown();
  532. pm_power_off();
  533. }
  534. /* A fallback in case there is no PM info available */
  535. tboot_shutdown(TB_SHUTDOWN_HALT);
  536. }
  537. struct machine_ops machine_ops = {
  538. .power_off = native_machine_power_off,
  539. .shutdown = native_machine_shutdown,
  540. .emergency_restart = native_machine_emergency_restart,
  541. .restart = native_machine_restart,
  542. .halt = native_machine_halt,
  543. #ifdef CONFIG_KEXEC
  544. .crash_shutdown = native_machine_crash_shutdown,
  545. #endif
  546. };
  547. void machine_power_off(void)
  548. {
  549. machine_ops.power_off();
  550. }
  551. void machine_shutdown(void)
  552. {
  553. machine_ops.shutdown();
  554. }
  555. void machine_emergency_restart(void)
  556. {
  557. __machine_emergency_restart(1);
  558. }
  559. void machine_restart(char *cmd)
  560. {
  561. machine_ops.restart(cmd);
  562. }
  563. void machine_halt(void)
  564. {
  565. machine_ops.halt();
  566. }
  567. #ifdef CONFIG_KEXEC
  568. void machine_crash_shutdown(struct pt_regs *regs)
  569. {
  570. machine_ops.crash_shutdown(regs);
  571. }
  572. #endif
  573. #if defined(CONFIG_SMP)
  574. /* This keeps a track of which one is crashing cpu. */
  575. static int crashing_cpu;
  576. static nmi_shootdown_cb shootdown_callback;
  577. static atomic_t waiting_for_crash_ipi;
  578. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  579. {
  580. int cpu;
  581. cpu = raw_smp_processor_id();
  582. /*
  583. * Don't do anything if this handler is invoked on crashing cpu.
  584. * Otherwise, system will completely hang. Crashing cpu can get
  585. * an NMI if system was initially booted with nmi_watchdog parameter.
  586. */
  587. if (cpu == crashing_cpu)
  588. return NMI_HANDLED;
  589. local_irq_disable();
  590. shootdown_callback(cpu, regs);
  591. atomic_dec(&waiting_for_crash_ipi);
  592. /* Assume hlt works */
  593. halt();
  594. for (;;)
  595. cpu_relax();
  596. return NMI_HANDLED;
  597. }
  598. static void smp_send_nmi_allbutself(void)
  599. {
  600. apic->send_IPI_allbutself(NMI_VECTOR);
  601. }
  602. /*
  603. * Halt all other CPUs, calling the specified function on each of them
  604. *
  605. * This function can be used to halt all other CPUs on crash
  606. * or emergency reboot time. The function passed as parameter
  607. * will be called inside a NMI handler on all CPUs.
  608. */
  609. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  610. {
  611. unsigned long msecs;
  612. local_irq_disable();
  613. /* Make a note of crashing cpu. Will be used in NMI callback. */
  614. crashing_cpu = safe_smp_processor_id();
  615. shootdown_callback = callback;
  616. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  617. /* Would it be better to replace the trap vector here? */
  618. if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
  619. NMI_FLAG_FIRST, "crash"))
  620. return; /* Return what? */
  621. /*
  622. * Ensure the new callback function is set before sending
  623. * out the NMI
  624. */
  625. wmb();
  626. smp_send_nmi_allbutself();
  627. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  628. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  629. mdelay(1);
  630. msecs--;
  631. }
  632. /* Leave the nmi callback set */
  633. }
  634. #else /* !CONFIG_SMP */
  635. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  636. {
  637. /* No other CPUs to shoot down */
  638. }
  639. #endif