blob: 350ea6680f63782dcfb237480f83c42b9229005a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/i386/kernel/reboot.c
3 */
4
Alexey Dobriyan129f6942005-06-23 00:08:33 -07005#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/mm.h>
7#include <linux/module.h>
8#include <linux/delay.h>
9#include <linux/init.h>
10#include <linux/interrupt.h>
11#include <linux/mc146818rtc.h>
12#include <linux/efi.h>
13#include <linux/dmi.h>
Tobias Klauser6f673d82005-09-13 01:25:48 -070014#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/uaccess.h>
16#include <asm/apic.h>
Zachary Amsden4d37e7e2005-09-03 15:56:38 -070017#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "mach_reboot.h"
Jaya Kumara2f7c352005-05-01 08:58:49 -070019#include <linux/reboot_fixups.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21/*
22 * Power off function, if any
23 */
24void (*pm_power_off)(void);
Alexey Dobriyan129f6942005-06-23 00:08:33 -070025EXPORT_SYMBOL(pm_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static int reboot_mode;
28static int reboot_thru_bios;
29
30#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int reboot_cpu = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#endif
33static int __init reboot_setup(char *str)
34{
35 while(1) {
36 switch (*str) {
37 case 'w': /* "warm" reboot (no memory testing etc) */
38 reboot_mode = 0x1234;
39 break;
40 case 'c': /* "cold" reboot (with memory testing etc) */
41 reboot_mode = 0x0;
42 break;
43 case 'b': /* "bios" reboot by jumping through the BIOS */
44 reboot_thru_bios = 1;
45 break;
46 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
47 reboot_thru_bios = 0;
48 break;
49#ifdef CONFIG_SMP
50 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
Tobias Klauser6f673d82005-09-13 01:25:48 -070051 if (isdigit(*(str+1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 reboot_cpu = (int) (*(str+1) - '0');
Tobias Klauser6f673d82005-09-13 01:25:48 -070053 if (isdigit(*(str+2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
55 }
56 /* we will leave sorting out the final value
57 when we are ready to reboot, since we might not
58 have set up boot_cpu_id or smp_num_cpu */
59 break;
60#endif
61 }
62 if((str = strchr(str,',')) != NULL)
63 str++;
64 else
65 break;
66 }
67 return 1;
68}
69
70__setup("reboot=", reboot_setup);
71
72/*
73 * Reboot options and system auto-detection code provided by
74 * Dell Inc. so their systems "just work". :-)
75 */
76
77/*
78 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
79 */
80static int __init set_bios_reboot(struct dmi_system_id *d)
81{
82 if (!reboot_thru_bios) {
83 reboot_thru_bios = 1;
84 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
85 }
86 return 0;
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct dmi_system_id __initdata reboot_dmi_table[] = {
90 { /* Handle problems with rebooting on Dell 1300's */
Eric W. Biedermandd2a1302005-06-25 14:57:55 -070091 .callback = set_bios_reboot,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .ident = "Dell PowerEdge 1300",
93 .matches = {
94 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
95 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
96 },
97 },
98 { /* Handle problems with rebooting on Dell 300's */
99 .callback = set_bios_reboot,
100 .ident = "Dell PowerEdge 300",
101 .matches = {
102 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
103 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
104 },
105 },
106 { /* Handle problems with rebooting on Dell 2400's */
107 .callback = set_bios_reboot,
108 .ident = "Dell PowerEdge 2400",
109 .matches = {
110 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
111 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
112 },
113 },
114 { }
115};
116
117static int __init reboot_init(void)
118{
119 dmi_check_system(reboot_dmi_table);
120 return 0;
121}
122
123core_initcall(reboot_init);
124
125/* The following code and data reboots the machine by switching to real
126 mode and jumping to the BIOS reset entry point, as if the CPU has
127 really been reset. The previous version asked the keyboard
128 controller to pulse the CPU reset line, which is more thorough, but
129 doesn't work with at least one type of 486 motherboard. It is easy
130 to stop this code working; hence the copious comments. */
131
132static unsigned long long
133real_mode_gdt_entries [3] =
134{
135 0x0000000000000000ULL, /* Null descriptor */
136 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
137 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
138};
139
140static struct
141{
142 unsigned short size __attribute__ ((packed));
143 unsigned long long * base __attribute__ ((packed));
144}
145real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
146real_mode_idt = { 0x3ff, NULL },
147no_idt = { 0, NULL };
148
149
150/* This is 16-bit protected mode code to disable paging and the cache,
151 switch to real mode and jump to the BIOS reset code.
152
153 The instruction that switches to real mode by writing to CR0 must be
154 followed immediately by a far jump instruction, which set CS to a
155 valid value for real mode, and flushes the prefetch queue to avoid
156 running instructions that have already been decoded in protected
157 mode.
158
159 Clears all the flags except ET, especially PG (paging), PE
160 (protected-mode enable) and TS (task switch for coprocessor state
161 save). Flushes the TLB after paging has been disabled. Sets CD and
162 NW, to disable the cache on a 486, and invalidates the cache. This
163 is more like the state of a 486 after reset. I don't know if
164 something else should be done for other chips.
165
166 More could be done here to set up the registers as if a CPU reset had
167 occurred; hopefully real BIOSs don't assume much. */
168
169static unsigned char real_mode_switch [] =
170{
171 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
172 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
173 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
174 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
175 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
176 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
177 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
178 0x74, 0x02, /* jz f */
179 0x0f, 0x09, /* wbinvd */
180 0x24, 0x10, /* f: andb $0x10,al */
181 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
182};
183static unsigned char jump_to_bios [] =
184{
185 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
186};
187
188/*
189 * Switch to real mode and then execute the code
190 * specified by the code and length parameters.
191 * We assume that length will aways be less that 100!
192 */
193void machine_real_restart(unsigned char *code, int length)
194{
195 unsigned long flags;
196
197 local_irq_disable();
198
199 /* Write zero to CMOS register number 0x0f, which the BIOS POST
200 routine will recognize as telling it to do a proper reboot. (Well
201 that's what this book in front of me says -- it may only apply to
202 the Phoenix BIOS though, it's not clear). At the same time,
203 disable NMIs by setting the top bit in the CMOS address register,
204 as we're about to do peculiar things to the CPU. I'm not sure if
205 `outb_p' is needed instead of just `outb'. Use it to be on the
206 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
207 */
208
209 spin_lock_irqsave(&rtc_lock, flags);
210 CMOS_WRITE(0x00, 0x8f);
211 spin_unlock_irqrestore(&rtc_lock, flags);
212
213 /* Remap the kernel at virtual address zero, as well as offset zero
214 from the kernel segment. This assumes the kernel segment starts at
215 virtual address PAGE_OFFSET. */
216
217 memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
218 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
219
220 /*
221 * Use `swapper_pg_dir' as our page directory.
222 */
223 load_cr3(swapper_pg_dir);
224
225 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
226 this on booting to tell it to "Bypass memory test (also warm
227 boot)". This seems like a fairly standard thing that gets set by
228 REBOOT.COM programs, and the previous reset routine did this
229 too. */
230
231 *((unsigned short *)0x472) = reboot_mode;
232
233 /* For the switch to real mode, copy some code to low memory. It has
234 to be in the first 64k because it is running in 16-bit mode, and it
235 has to have the same physical and virtual address, because it turns
236 off paging. Copy it near the end of the first page, out of the way
237 of BIOS variables. */
238
239 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
240 real_mode_switch, sizeof (real_mode_switch));
241 memcpy ((void *) (0x1000 - 100), code, length);
242
243 /* Set up the IDT for real mode. */
244
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700245 load_idt(&real_mode_idt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* Set up a GDT from which we can load segment descriptors for real
248 mode. The GDT is not used in real mode; it is just needed here to
249 prepare the descriptors. */
250
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700251 load_gdt(&real_mode_gdt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* Load the data segment registers, and thus the descriptors ready for
254 real mode. The base address of each segment is 0x100, 16 times the
255 selector value being loaded here. This is so that the segment
256 registers don't have to be reloaded after switching to real mode:
257 the values are consistent for real mode operation already. */
258
259 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
260 "\tmovl %%eax,%%ds\n"
261 "\tmovl %%eax,%%es\n"
262 "\tmovl %%eax,%%fs\n"
263 "\tmovl %%eax,%%gs\n"
264 "\tmovl %%eax,%%ss" : : : "eax");
265
266 /* Jump to the 16-bit code that we copied earlier. It disables paging
267 and the cache, switches to real mode, and jumps to the BIOS reset
268 entry point. */
269
270 __asm__ __volatile__ ("ljmp $0x0008,%0"
271 :
272 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
273}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700274#ifdef CONFIG_APM_MODULE
275EXPORT_SYMBOL(machine_real_restart);
276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700278void machine_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280#ifdef CONFIG_SMP
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700281 int reboot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700283 /* The boot cpu is always logical cpu 0 */
284 reboot_cpu_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700286 /* See if there has been given a command line override */
Eric W. Biedermand8e392e2005-07-26 12:07:01 -0600287 if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700288 cpu_isset(reboot_cpu, cpu_online_map)) {
289 reboot_cpu_id = reboot_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700292 /* Make certain the cpu I'm rebooting on is online */
293 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
294 reboot_cpu_id = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700296
297 /* Make certain I only run on the appropriate processor */
298 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
299
300 /* O.K. Now that I'm on the appropriate processor, stop
301 * all of the others, and disable their local APICs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 smp_send_stop();
305#endif /* CONFIG_SMP */
306
307 lapic_shutdown();
308
309#ifdef CONFIG_X86_IO_APIC
310 disable_IO_APIC();
311#endif
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700312}
313
Eric W. Biederman4a1421f2005-07-26 11:41:26 -0600314void machine_emergency_restart(void)
Eric W. Biedermandd2a1302005-06-25 14:57:55 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!reboot_thru_bios) {
317 if (efi_enabled) {
318 efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700319 load_idt(&no_idt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 __asm__ __volatile__("int3");
321 }
322 /* rebooting needs to touch the page at absolute addr 0 */
323 *((unsigned short *)__va(0x472)) = reboot_mode;
324 for (;;) {
Jaya Kumara2f7c352005-05-01 08:58:49 -0700325 mach_reboot_fixups(); /* for board specific fixups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 mach_reboot();
327 /* That didn't work - force a triple fault.. */
Zachary Amsden4d37e7e2005-09-03 15:56:38 -0700328 load_idt(&no_idt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 __asm__ __volatile__("int3");
330 }
331 }
332 if (efi_enabled)
333 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
334
335 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
336}
337
Eric W. Biederman4a1421f2005-07-26 11:41:26 -0600338void machine_restart(char * __unused)
339{
340 machine_shutdown();
341 machine_emergency_restart();
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344void machine_halt(void)
345{
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348void machine_power_off(void)
349{
Eric W. Biederman4fa25642005-07-26 12:08:53 -0600350 machine_shutdown();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (pm_power_off)
353 pm_power_off();
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356