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