reboot.c 13 KB

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