reboot.c 21 KB

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