reboot.c 20 KB

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