reboot.c 21 KB

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