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