reboot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <acpi/reboot.h>
  7. #include <asm/io.h>
  8. #include <asm/apic.h>
  9. #include <asm/desc.h>
  10. #include <asm/hpet.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/proto.h>
  13. #include <asm/reboot_fixups.h>
  14. #include <asm/reboot.h>
  15. #ifdef CONFIG_X86_32
  16. # include <linux/dmi.h>
  17. # include <linux/ctype.h>
  18. # include <linux/mc146818rtc.h>
  19. #else
  20. # include <asm/iommu.h>
  21. #endif
  22. /*
  23. * Power off function, if any
  24. */
  25. void (*pm_power_off)(void);
  26. EXPORT_SYMBOL(pm_power_off);
  27. static const struct desc_ptr no_idt = {};
  28. static int reboot_mode;
  29. /*
  30. * Keyboard reset and triple fault may result in INIT, not RESET, which
  31. * doesn't work when we're in vmx root mode. Try ACPI first.
  32. */
  33. enum reboot_type reboot_type = BOOT_ACPI;
  34. int reboot_force;
  35. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  36. static int reboot_cpu = -1;
  37. #endif
  38. /* reboot=b[ios] | s[mp] | t[riple] | k[bd] | e[fi] [, [w]arm | [c]old]
  39. warm Don't set the cold reboot flag
  40. cold Set the cold reboot flag
  41. bios Reboot by jumping through the BIOS (only for X86_32)
  42. smp Reboot by executing reset on BSP or other CPU (only for X86_32)
  43. triple Force a triple fault (init)
  44. kbd Use the keyboard controller. cold reset (default)
  45. acpi Use the RESET_REG in the FADT
  46. efi Use efi reset_system runtime service
  47. force Avoid anything that could hang.
  48. */
  49. static int __init reboot_setup(char *str)
  50. {
  51. for (;;) {
  52. switch (*str) {
  53. case 'w':
  54. reboot_mode = 0x1234;
  55. break;
  56. case 'c':
  57. reboot_mode = 0;
  58. break;
  59. #ifdef CONFIG_X86_32
  60. #ifdef CONFIG_SMP
  61. case 's':
  62. if (isdigit(*(str+1))) {
  63. reboot_cpu = (int) (*(str+1) - '0');
  64. if (isdigit(*(str+2)))
  65. reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
  66. }
  67. /* we will leave sorting out the final value
  68. when we are ready to reboot, since we might not
  69. have set up boot_cpu_id or smp_num_cpu */
  70. break;
  71. #endif /* CONFIG_SMP */
  72. case 'b':
  73. #endif
  74. case 'a':
  75. case 'k':
  76. case 't':
  77. case 'e':
  78. reboot_type = *str;
  79. break;
  80. case 'f':
  81. reboot_force = 1;
  82. break;
  83. }
  84. str = strchr(str, ',');
  85. if (str)
  86. str++;
  87. else
  88. break;
  89. }
  90. return 1;
  91. }
  92. __setup("reboot=", reboot_setup);
  93. #ifdef CONFIG_X86_32
  94. /*
  95. * Reboot options and system auto-detection code provided by
  96. * Dell Inc. so their systems "just work". :-)
  97. */
  98. /*
  99. * Some machines require the "reboot=b" commandline option,
  100. * this quirk makes that automatic.
  101. */
  102. static int __init set_bios_reboot(const struct dmi_system_id *d)
  103. {
  104. if (reboot_type != BOOT_BIOS) {
  105. reboot_type = BOOT_BIOS;
  106. printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
  107. }
  108. return 0;
  109. }
  110. static struct dmi_system_id __initdata reboot_dmi_table[] = {
  111. { /* Handle problems with rebooting on Dell E520's */
  112. .callback = set_bios_reboot,
  113. .ident = "Dell E520",
  114. .matches = {
  115. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  116. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
  117. },
  118. },
  119. { /* Handle problems with rebooting on Dell 1300's */
  120. .callback = set_bios_reboot,
  121. .ident = "Dell PowerEdge 1300",
  122. .matches = {
  123. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  124. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
  125. },
  126. },
  127. { /* Handle problems with rebooting on Dell 300's */
  128. .callback = set_bios_reboot,
  129. .ident = "Dell PowerEdge 300",
  130. .matches = {
  131. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  132. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
  133. },
  134. },
  135. { /* Handle problems with rebooting on Dell Optiplex 745's SFF*/
  136. .callback = set_bios_reboot,
  137. .ident = "Dell OptiPlex 745",
  138. .matches = {
  139. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  140. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  141. },
  142. },
  143. { /* Handle problems with rebooting on Dell Optiplex 745's DFF*/
  144. .callback = set_bios_reboot,
  145. .ident = "Dell OptiPlex 745",
  146. .matches = {
  147. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  148. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  149. DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
  150. },
  151. },
  152. { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
  153. .callback = set_bios_reboot,
  154. .ident = "Dell OptiPlex 745",
  155. .matches = {
  156. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  157. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  158. DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
  159. },
  160. },
  161. { /* Handle problems with rebooting on Dell 2400's */
  162. .callback = set_bios_reboot,
  163. .ident = "Dell PowerEdge 2400",
  164. .matches = {
  165. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  166. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
  167. },
  168. },
  169. { /* Handle problems with rebooting on Dell T5400's */
  170. .callback = set_bios_reboot,
  171. .ident = "Dell Precision T5400",
  172. .matches = {
  173. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  174. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
  175. },
  176. },
  177. { /* Handle problems with rebooting on HP laptops */
  178. .callback = set_bios_reboot,
  179. .ident = "HP Compaq Laptop",
  180. .matches = {
  181. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  182. DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
  183. },
  184. },
  185. { }
  186. };
  187. static int __init reboot_init(void)
  188. {
  189. dmi_check_system(reboot_dmi_table);
  190. return 0;
  191. }
  192. core_initcall(reboot_init);
  193. /* The following code and data reboots the machine by switching to real
  194. mode and jumping to the BIOS reset entry point, as if the CPU has
  195. really been reset. The previous version asked the keyboard
  196. controller to pulse the CPU reset line, which is more thorough, but
  197. doesn't work with at least one type of 486 motherboard. It is easy
  198. to stop this code working; hence the copious comments. */
  199. static const unsigned long long
  200. real_mode_gdt_entries [3] =
  201. {
  202. 0x0000000000000000ULL, /* Null descriptor */
  203. 0x00009b000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
  204. 0x000093000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
  205. };
  206. static const struct desc_ptr
  207. real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, (long)real_mode_gdt_entries },
  208. real_mode_idt = { 0x3ff, 0 };
  209. /* This is 16-bit protected mode code to disable paging and the cache,
  210. switch to real mode and jump to the BIOS reset code.
  211. The instruction that switches to real mode by writing to CR0 must be
  212. followed immediately by a far jump instruction, which set CS to a
  213. valid value for real mode, and flushes the prefetch queue to avoid
  214. running instructions that have already been decoded in protected
  215. mode.
  216. Clears all the flags except ET, especially PG (paging), PE
  217. (protected-mode enable) and TS (task switch for coprocessor state
  218. save). Flushes the TLB after paging has been disabled. Sets CD and
  219. NW, to disable the cache on a 486, and invalidates the cache. This
  220. is more like the state of a 486 after reset. I don't know if
  221. something else should be done for other chips.
  222. More could be done here to set up the registers as if a CPU reset had
  223. occurred; hopefully real BIOSs don't assume much. */
  224. static const unsigned char real_mode_switch [] =
  225. {
  226. 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
  227. 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
  228. 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
  229. 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
  230. 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
  231. 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
  232. 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
  233. 0x74, 0x02, /* jz f */
  234. 0x0f, 0x09, /* wbinvd */
  235. 0x24, 0x10, /* f: andb $0x10,al */
  236. 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
  237. };
  238. static const unsigned char jump_to_bios [] =
  239. {
  240. 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
  241. };
  242. /*
  243. * Switch to real mode and then execute the code
  244. * specified by the code and length parameters.
  245. * We assume that length will aways be less that 100!
  246. */
  247. void machine_real_restart(const unsigned char *code, int length)
  248. {
  249. local_irq_disable();
  250. /* Write zero to CMOS register number 0x0f, which the BIOS POST
  251. routine will recognize as telling it to do a proper reboot. (Well
  252. that's what this book in front of me says -- it may only apply to
  253. the Phoenix BIOS though, it's not clear). At the same time,
  254. disable NMIs by setting the top bit in the CMOS address register,
  255. as we're about to do peculiar things to the CPU. I'm not sure if
  256. `outb_p' is needed instead of just `outb'. Use it to be on the
  257. safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
  258. */
  259. spin_lock(&rtc_lock);
  260. CMOS_WRITE(0x00, 0x8f);
  261. spin_unlock(&rtc_lock);
  262. /* Remap the kernel at virtual address zero, as well as offset zero
  263. from the kernel segment. This assumes the kernel segment starts at
  264. virtual address PAGE_OFFSET. */
  265. memcpy(swapper_pg_dir, swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  266. sizeof(swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
  267. /*
  268. * Use `swapper_pg_dir' as our page directory.
  269. */
  270. load_cr3(swapper_pg_dir);
  271. /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
  272. this on booting to tell it to "Bypass memory test (also warm
  273. boot)". This seems like a fairly standard thing that gets set by
  274. REBOOT.COM programs, and the previous reset routine did this
  275. too. */
  276. *((unsigned short *)0x472) = reboot_mode;
  277. /* For the switch to real mode, copy some code to low memory. It has
  278. to be in the first 64k because it is running in 16-bit mode, and it
  279. has to have the same physical and virtual address, because it turns
  280. off paging. Copy it near the end of the first page, out of the way
  281. of BIOS variables. */
  282. memcpy((void *)(0x1000 - sizeof(real_mode_switch) - 100),
  283. real_mode_switch, sizeof (real_mode_switch));
  284. memcpy((void *)(0x1000 - 100), code, length);
  285. /* Set up the IDT for real mode. */
  286. load_idt(&real_mode_idt);
  287. /* Set up a GDT from which we can load segment descriptors for real
  288. mode. The GDT is not used in real mode; it is just needed here to
  289. prepare the descriptors. */
  290. load_gdt(&real_mode_gdt);
  291. /* Load the data segment registers, and thus the descriptors ready for
  292. real mode. The base address of each segment is 0x100, 16 times the
  293. selector value being loaded here. This is so that the segment
  294. registers don't have to be reloaded after switching to real mode:
  295. the values are consistent for real mode operation already. */
  296. __asm__ __volatile__ ("movl $0x0010,%%eax\n"
  297. "\tmovl %%eax,%%ds\n"
  298. "\tmovl %%eax,%%es\n"
  299. "\tmovl %%eax,%%fs\n"
  300. "\tmovl %%eax,%%gs\n"
  301. "\tmovl %%eax,%%ss" : : : "eax");
  302. /* Jump to the 16-bit code that we copied earlier. It disables paging
  303. and the cache, switches to real mode, and jumps to the BIOS reset
  304. entry point. */
  305. __asm__ __volatile__ ("ljmp $0x0008,%0"
  306. :
  307. : "i" ((void *)(0x1000 - sizeof (real_mode_switch) - 100)));
  308. }
  309. #ifdef CONFIG_APM_MODULE
  310. EXPORT_SYMBOL(machine_real_restart);
  311. #endif
  312. #endif /* CONFIG_X86_32 */
  313. static inline void kb_wait(void)
  314. {
  315. int i;
  316. for (i = 0; i < 0x10000; i++) {
  317. if ((inb(0x64) & 0x02) == 0)
  318. break;
  319. udelay(2);
  320. }
  321. }
  322. void __attribute__((weak)) mach_reboot_fixups(void)
  323. {
  324. }
  325. static void native_machine_emergency_restart(void)
  326. {
  327. int i;
  328. /* Tell the BIOS if we want cold or warm reboot */
  329. *((unsigned short *)__va(0x472)) = reboot_mode;
  330. for (;;) {
  331. /* Could also try the reset bit in the Hammer NB */
  332. switch (reboot_type) {
  333. case BOOT_KBD:
  334. mach_reboot_fixups(); /* for board specific fixups */
  335. for (i = 0; i < 10; i++) {
  336. kb_wait();
  337. udelay(50);
  338. outb(0xfe, 0x64); /* pulse reset low */
  339. udelay(50);
  340. }
  341. case BOOT_TRIPLE:
  342. load_idt(&no_idt);
  343. __asm__ __volatile__("int3");
  344. reboot_type = BOOT_KBD;
  345. break;
  346. #ifdef CONFIG_X86_32
  347. case BOOT_BIOS:
  348. machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
  349. reboot_type = BOOT_KBD;
  350. break;
  351. #endif
  352. case BOOT_ACPI:
  353. acpi_reboot();
  354. reboot_type = BOOT_KBD;
  355. break;
  356. case BOOT_EFI:
  357. if (efi_enabled)
  358. efi.reset_system(reboot_mode ? EFI_RESET_WARM : EFI_RESET_COLD,
  359. EFI_SUCCESS, 0, NULL);
  360. reboot_type = BOOT_KBD;
  361. break;
  362. }
  363. }
  364. }
  365. void native_machine_shutdown(void)
  366. {
  367. /* Stop the cpus and apics */
  368. #ifdef CONFIG_SMP
  369. /* The boot cpu is always logical cpu 0 */
  370. int reboot_cpu_id = 0;
  371. #ifdef CONFIG_X86_32
  372. /* See if there has been given a command line override */
  373. if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
  374. cpu_online(reboot_cpu))
  375. reboot_cpu_id = reboot_cpu;
  376. #endif
  377. /* Make certain the cpu I'm about to reboot on is online */
  378. if (!cpu_online(reboot_cpu_id))
  379. reboot_cpu_id = smp_processor_id();
  380. /* Make certain I only run on the appropriate processor */
  381. set_cpus_allowed_ptr(current, &cpumask_of_cpu(reboot_cpu_id));
  382. /* O.K Now that I'm on the appropriate processor,
  383. * stop all of the others.
  384. */
  385. smp_send_stop();
  386. #endif
  387. lapic_shutdown();
  388. #ifdef CONFIG_X86_IO_APIC
  389. disable_IO_APIC();
  390. #endif
  391. #ifdef CONFIG_HPET_TIMER
  392. hpet_disable();
  393. #endif
  394. #ifdef CONFIG_X86_64
  395. pci_iommu_shutdown();
  396. #endif
  397. }
  398. static void native_machine_restart(char *__unused)
  399. {
  400. printk("machine restart\n");
  401. if (!reboot_force)
  402. machine_shutdown();
  403. machine_emergency_restart();
  404. }
  405. static void native_machine_halt(void)
  406. {
  407. }
  408. static void native_machine_power_off(void)
  409. {
  410. if (pm_power_off) {
  411. if (!reboot_force)
  412. machine_shutdown();
  413. pm_power_off();
  414. }
  415. }
  416. struct machine_ops machine_ops = {
  417. .power_off = native_machine_power_off,
  418. .shutdown = native_machine_shutdown,
  419. .emergency_restart = native_machine_emergency_restart,
  420. .restart = native_machine_restart,
  421. .halt = native_machine_halt,
  422. #ifdef CONFIG_KEXEC
  423. .crash_shutdown = native_machine_crash_shutdown,
  424. #endif
  425. };
  426. void machine_power_off(void)
  427. {
  428. machine_ops.power_off();
  429. }
  430. void machine_shutdown(void)
  431. {
  432. machine_ops.shutdown();
  433. }
  434. void machine_emergency_restart(void)
  435. {
  436. machine_ops.emergency_restart();
  437. }
  438. void machine_restart(char *cmd)
  439. {
  440. machine_ops.restart(cmd);
  441. }
  442. void machine_halt(void)
  443. {
  444. machine_ops.halt();
  445. }
  446. #ifdef CONFIG_KEXEC
  447. void machine_crash_shutdown(struct pt_regs *regs)
  448. {
  449. machine_ops.crash_shutdown(regs);
  450. }
  451. #endif