reboot.c 21 KB

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