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 int reboot_smp = 0;
28 static int reboot_cpu = -1;
29 /* shamelessly grabbed from lib/vsprintf.c for readability */
30 #define is_digit(c)     ((c) >= '0' && (c) <= '9')
31 #endif
32 static int __init reboot_setup(char *str)
33 {
34         while(1) {
35                 switch (*str) {
36                 case 'w': /* "warm" reboot (no memory testing etc) */
37                         reboot_mode = 0x1234;
38                         break;
39                 case 'c': /* "cold" reboot (with memory testing etc) */
40                         reboot_mode = 0x0;
41                         break;
42                 case 'b': /* "bios" reboot by jumping through the BIOS */
43                         reboot_thru_bios = 1;
44                         break;
45                 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
46                         reboot_thru_bios = 0;
47                         break;
48 #ifdef CONFIG_SMP
49                 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
50                         reboot_smp = 1;
51                         if (is_digit(*(str+1))) {
52                                 reboot_cpu = (int) (*(str+1) - '0');
53                                 if (is_digit(*(str+2))) 
54                                         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  */
80 static 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
89 /*
90  * Some machines require the "reboot=s"  commandline option, this quirk makes that automatic.
91  */
92 static int __init set_smp_reboot(struct dmi_system_id *d)
93 {
94 #ifdef CONFIG_SMP
95         if (!reboot_smp) {
96                 reboot_smp = 1;
97                 printk(KERN_INFO "%s series board detected. Selecting SMP-method for reboots.\n", d->ident);
98         }
99 #endif
100         return 0;
101 }
102
103 /*
104  * Some machines require the "reboot=b,s"  commandline option, this quirk makes that automatic.
105  */
106 static int __init set_smp_bios_reboot(struct dmi_system_id *d)
107 {
108         set_smp_reboot(d);
109         set_bios_reboot(d);
110         return 0;
111 }
112
113 static struct dmi_system_id __initdata reboot_dmi_table[] = {
114         {       /* Handle problems with rebooting on Dell 1300's */
115                 .callback = set_smp_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 2400's */
131                 .callback = set_bios_reboot,
132                 .ident = "Dell PowerEdge 2400",
133                 .matches = {
134                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
135                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
136                 },
137         },
138         { }
139 };
140
141 static int __init reboot_init(void)
142 {
143         dmi_check_system(reboot_dmi_table);
144         return 0;
145 }
146
147 core_initcall(reboot_init);
148
149 /* The following code and data reboots the machine by switching to real
150    mode and jumping to the BIOS reset entry point, as if the CPU has
151    really been reset.  The previous version asked the keyboard
152    controller to pulse the CPU reset line, which is more thorough, but
153    doesn't work with at least one type of 486 motherboard.  It is easy
154    to stop this code working; hence the copious comments. */
155
156 static unsigned long long
157 real_mode_gdt_entries [3] =
158 {
159         0x0000000000000000ULL,  /* Null descriptor */
160         0x00009a000000ffffULL,  /* 16-bit real-mode 64k code at 0x00000000 */
161         0x000092000100ffffULL   /* 16-bit real-mode 64k data at 0x00000100 */
162 };
163
164 static struct
165 {
166         unsigned short       size __attribute__ ((packed));
167         unsigned long long * base __attribute__ ((packed));
168 }
169 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
170 real_mode_idt = { 0x3ff, NULL },
171 no_idt = { 0, NULL };
172
173
174 /* This is 16-bit protected mode code to disable paging and the cache,
175    switch to real mode and jump to the BIOS reset code.
176
177    The instruction that switches to real mode by writing to CR0 must be
178    followed immediately by a far jump instruction, which set CS to a
179    valid value for real mode, and flushes the prefetch queue to avoid
180    running instructions that have already been decoded in protected
181    mode.
182
183    Clears all the flags except ET, especially PG (paging), PE
184    (protected-mode enable) and TS (task switch for coprocessor state
185    save).  Flushes the TLB after paging has been disabled.  Sets CD and
186    NW, to disable the cache on a 486, and invalidates the cache.  This
187    is more like the state of a 486 after reset.  I don't know if
188    something else should be done for other chips.
189
190    More could be done here to set up the registers as if a CPU reset had
191    occurred; hopefully real BIOSs don't assume much. */
192
193 static unsigned char real_mode_switch [] =
194 {
195         0x66, 0x0f, 0x20, 0xc0,                 /*    movl  %cr0,%eax        */
196         0x66, 0x83, 0xe0, 0x11,                 /*    andl  $0x00000011,%eax */
197         0x66, 0x0d, 0x00, 0x00, 0x00, 0x60,     /*    orl   $0x60000000,%eax */
198         0x66, 0x0f, 0x22, 0xc0,                 /*    movl  %eax,%cr0        */
199         0x66, 0x0f, 0x22, 0xd8,                 /*    movl  %eax,%cr3        */
200         0x66, 0x0f, 0x20, 0xc3,                 /*    movl  %cr0,%ebx        */
201         0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60,       /*    andl  $0x60000000,%ebx */
202         0x74, 0x02,                             /*    jz    f                */
203         0x0f, 0x09,                             /*    wbinvd                 */
204         0x24, 0x10,                             /* f: andb  $0x10,al         */
205         0x66, 0x0f, 0x22, 0xc0                  /*    movl  %eax,%cr0        */
206 };
207 static unsigned char jump_to_bios [] =
208 {
209         0xea, 0x00, 0x00, 0xff, 0xff            /*    ljmp  $0xffff,$0x0000  */
210 };
211
212 /*
213  * Switch to real mode and then execute the code
214  * specified by the code and length parameters.
215  * We assume that length will aways be less that 100!
216  */
217 void machine_real_restart(unsigned char *code, int length)
218 {
219         unsigned long flags;
220
221         local_irq_disable();
222
223         /* Write zero to CMOS register number 0x0f, which the BIOS POST
224            routine will recognize as telling it to do a proper reboot.  (Well
225            that's what this book in front of me says -- it may only apply to
226            the Phoenix BIOS though, it's not clear).  At the same time,
227            disable NMIs by setting the top bit in the CMOS address register,
228            as we're about to do peculiar things to the CPU.  I'm not sure if
229            `outb_p' is needed instead of just `outb'.  Use it to be on the
230            safe side.  (Yes, CMOS_WRITE does outb_p's. -  Paul G.)
231          */
232
233         spin_lock_irqsave(&rtc_lock, flags);
234         CMOS_WRITE(0x00, 0x8f);
235         spin_unlock_irqrestore(&rtc_lock, flags);
236
237         /* Remap the kernel at virtual address zero, as well as offset zero
238            from the kernel segment.  This assumes the kernel segment starts at
239            virtual address PAGE_OFFSET. */
240
241         memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
242                 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
243
244         /*
245          * Use `swapper_pg_dir' as our page directory.
246          */
247         load_cr3(swapper_pg_dir);
248
249         /* Write 0x1234 to absolute memory location 0x472.  The BIOS reads
250            this on booting to tell it to "Bypass memory test (also warm
251            boot)".  This seems like a fairly standard thing that gets set by
252            REBOOT.COM programs, and the previous reset routine did this
253            too. */
254
255         *((unsigned short *)0x472) = reboot_mode;
256
257         /* For the switch to real mode, copy some code to low memory.  It has
258            to be in the first 64k because it is running in 16-bit mode, and it
259            has to have the same physical and virtual address, because it turns
260            off paging.  Copy it near the end of the first page, out of the way
261            of BIOS variables. */
262
263         memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
264                 real_mode_switch, sizeof (real_mode_switch));
265         memcpy ((void *) (0x1000 - 100), code, length);
266
267         /* Set up the IDT for real mode. */
268
269         __asm__ __volatile__ ("lidt %0" : : "m" (real_mode_idt));
270
271         /* Set up a GDT from which we can load segment descriptors for real
272            mode.  The GDT is not used in real mode; it is just needed here to
273            prepare the descriptors. */
274
275         __asm__ __volatile__ ("lgdt %0" : : "m" (real_mode_gdt));
276
277         /* Load the data segment registers, and thus the descriptors ready for
278            real mode.  The base address of each segment is 0x100, 16 times the
279            selector value being loaded here.  This is so that the segment
280            registers don't have to be reloaded after switching to real mode:
281            the values are consistent for real mode operation already. */
282
283         __asm__ __volatile__ ("movl $0x0010,%%eax\n"
284                                 "\tmovl %%eax,%%ds\n"
285                                 "\tmovl %%eax,%%es\n"
286                                 "\tmovl %%eax,%%fs\n"
287                                 "\tmovl %%eax,%%gs\n"
288                                 "\tmovl %%eax,%%ss" : : : "eax");
289
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
294         __asm__ __volatile__ ("ljmp $0x0008,%0"
295                                 :
296                                 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
297 }
298
299 void machine_restart(char * __unused)
300 {
301 #ifdef CONFIG_SMP
302         int cpuid;
303         
304         cpuid = GET_APIC_ID(apic_read(APIC_ID));
305
306         if (reboot_smp) {
307
308                 /* check to see if reboot_cpu is valid 
309                    if its not, default to the BSP */
310                 if ((reboot_cpu == -1) ||  
311                       (reboot_cpu > (NR_CPUS -1))  || 
312                       !physid_isset(cpuid, phys_cpu_present_map))
313                         reboot_cpu = boot_cpu_physical_apicid;
314
315                 reboot_smp = 0;  /* use this as a flag to only go through this once*/
316                 /* re-run this function on the other CPUs
317                    it will fall though this section since we have 
318                    cleared reboot_smp, and do the reboot if it is the
319                    correct CPU, otherwise it halts. */
320                 if (reboot_cpu != cpuid)
321                         smp_call_function((void *)machine_restart , NULL, 1, 0);
322         }
323
324         /* if reboot_cpu is still -1, then we want a tradional reboot, 
325            and if we are not running on the reboot_cpu,, halt */
326         if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
327                 for (;;)
328                 __asm__ __volatile__ ("hlt");
329         }
330         /*
331          * Stop all CPUs and turn off local APICs and the IO-APIC, so
332          * other OSs see a clean IRQ state.
333          */
334         smp_send_stop();
335 #endif /* CONFIG_SMP */
336
337         lapic_shutdown();
338
339 #ifdef CONFIG_X86_IO_APIC
340         disable_IO_APIC();
341 #endif
342
343         if (!reboot_thru_bios) {
344                 if (efi_enabled) {
345                         efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
346                         __asm__ __volatile__("lidt %0": :"m" (no_idt));
347                         __asm__ __volatile__("int3");
348                 }
349                 /* rebooting needs to touch the page at absolute addr 0 */
350                 *((unsigned short *)__va(0x472)) = reboot_mode;
351                 for (;;) {
352                         mach_reboot_fixups(); /* for board specific fixups */
353                         mach_reboot();
354                         /* That didn't work - force a triple fault.. */
355                         __asm__ __volatile__("lidt %0": :"m" (no_idt));
356                         __asm__ __volatile__("int3");
357                 }
358         }
359         if (efi_enabled)
360                 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
361
362         machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
363 }
364
365 EXPORT_SYMBOL(machine_restart);
366
367 void machine_halt(void)
368 {
369 }
370
371 EXPORT_SYMBOL(machine_halt);
372
373 void machine_power_off(void)
374 {
375         lapic_shutdown();
376
377         if (efi_enabled)
378                 efi.reset_system(EFI_RESET_SHUTDOWN, EFI_SUCCESS, 0, NULL);
379         if (pm_power_off)
380                 pm_power_off();
381 }
382
383 EXPORT_SYMBOL(machine_power_off);
384