reboot.c 20 KB

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