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