reboot.c 22 KB

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