reboot.c 18 KB

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