reboot.c 13 KB

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