reboot.c 20 KB

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