]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/reboot_32.c
9e2269d00918c13948b03b92532498015e95460b
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / reboot_32.c
1 #include <linux/mm.h>
2 #include <linux/module.h>
3 #include <linux/delay.h>
4 #include <linux/init.h>
5 #include <linux/interrupt.h>
6 #include <linux/mc146818rtc.h>
7 #include <linux/efi.h>
8 #include <linux/dmi.h>
9 #include <linux/ctype.h>
10 #include <linux/pm.h>
11 #include <linux/reboot.h>
12 #include <asm/uaccess.h>
13 #include <asm/apic.h>
14 #include <asm/desc.h>
15 #include "mach_reboot.h"
16 #include <asm/reboot_fixups.h>
17 #include <asm/reboot.h>
18
19 /*
20  * Power off function, if any
21  */
22 void (*pm_power_off)(void);
23 EXPORT_SYMBOL(pm_power_off);
24
25 static int reboot_mode;
26 static int reboot_thru_bios;
27
28 #ifdef CONFIG_SMP
29 static int reboot_cpu = -1;
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 (isdigit(*(str+1))) {
50                                 reboot_cpu = (int) (*(str+1) - '0');
51                                 if (isdigit(*(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(const 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 E520's */
89                 .callback = set_bios_reboot,
90                 .ident = "Dell E520",
91                 .matches = {
92                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
93                         DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
94                 },
95         },
96         {       /* Handle problems with rebooting on Dell 1300's */
97                 .callback = set_bios_reboot,
98                 .ident = "Dell PowerEdge 1300",
99                 .matches = {
100                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
101                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
102                 },
103         },
104         {       /* Handle problems with rebooting on Dell 300's */
105                 .callback = set_bios_reboot,
106                 .ident = "Dell PowerEdge 300",
107                 .matches = {
108                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
109                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
110                 },
111         },
112         {       /* Handle problems with rebooting on Dell Optiplex 745's SFF*/
113                 .callback = set_bios_reboot,
114                 .ident = "Dell OptiPlex 745",
115                 .matches = {
116                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
117                         DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
118                         DMI_MATCH(DMI_BOARD_NAME, "0WF810"),
119                 },
120         },
121         {       /* Handle problems with rebooting on Dell 2400's */
122                 .callback = set_bios_reboot,
123                 .ident = "Dell PowerEdge 2400",
124                 .matches = {
125                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
126                         DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
127                 },
128         },
129         {       /* Handle problems with rebooting on HP laptops */
130                 .callback = set_bios_reboot,
131                 .ident = "HP Compaq Laptop",
132                 .matches = {
133                         DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
134                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
135                 },
136         },
137         { }
138 };
139
140 static int __init 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 Xgt_desc_struct
164 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, (long)real_mode_gdt_entries },
165 real_mode_idt = { 0x3ff, 0 },
166 no_idt = { 0, 0 };
167
168
169 /* This is 16-bit protected mode code to disable paging and the cache,
170    switch to real mode and jump to the BIOS reset code.
171
172    The instruction that switches to real mode by writing to CR0 must be
173    followed immediately by a far jump instruction, which set CS to a
174    valid value for real mode, and flushes the prefetch queue to avoid
175    running instructions that have already been decoded in protected
176    mode.
177
178    Clears all the flags except ET, especially PG (paging), PE
179    (protected-mode enable) and TS (task switch for coprocessor state
180    save).  Flushes the TLB after paging has been disabled.  Sets CD and
181    NW, to disable the cache on a 486, and invalidates the cache.  This
182    is more like the state of a 486 after reset.  I don't know if
183    something else should be done for other chips.
184
185    More could be done here to set up the registers as if a CPU reset had
186    occurred; hopefully real BIOSs don't assume much. */
187
188 static unsigned char real_mode_switch [] =
189 {
190         0x66, 0x0f, 0x20, 0xc0,                 /*    movl  %cr0,%eax        */
191         0x66, 0x83, 0xe0, 0x11,                 /*    andl  $0x00000011,%eax */
192         0x66, 0x0d, 0x00, 0x00, 0x00, 0x60,     /*    orl   $0x60000000,%eax */
193         0x66, 0x0f, 0x22, 0xc0,                 /*    movl  %eax,%cr0        */
194         0x66, 0x0f, 0x22, 0xd8,                 /*    movl  %eax,%cr3        */
195         0x66, 0x0f, 0x20, 0xc3,                 /*    movl  %cr0,%ebx        */
196         0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60,       /*    andl  $0x60000000,%ebx */
197         0x74, 0x02,                             /*    jz    f                */
198         0x0f, 0x09,                             /*    wbinvd                 */
199         0x24, 0x10,                             /* f: andb  $0x10,al         */
200         0x66, 0x0f, 0x22, 0xc0                  /*    movl  %eax,%cr0        */
201 };
202 static unsigned char jump_to_bios [] =
203 {
204         0xea, 0x00, 0x00, 0xff, 0xff            /*    ljmp  $0xffff,$0x0000  */
205 };
206
207 /*
208  * Switch to real mode and then execute the code
209  * specified by the code and length parameters.
210  * We assume that length will aways be less that 100!
211  */
212 void machine_real_restart(unsigned char *code, int length)
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(&rtc_lock);
227         CMOS_WRITE(0x00, 0x8f);
228         spin_unlock(&rtc_lock);
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 static void native_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 __attribute__((weak)) mach_reboot_fixups(void)
332 {
333 }
334
335 static void native_machine_emergency_restart(void)
336 {
337         if (!reboot_thru_bios) {
338                 if (efi_enabled) {
339                         efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
340                         load_idt(&no_idt);
341                         __asm__ __volatile__("int3");
342                 }
343                 /* rebooting needs to touch the page at absolute addr 0 */
344                 *((unsigned short *)__va(0x472)) = reboot_mode;
345                 for (;;) {
346                         mach_reboot_fixups(); /* for board specific fixups */
347                         mach_reboot();
348                         /* That didn't work - force a triple fault.. */
349                         load_idt(&no_idt);
350                         __asm__ __volatile__("int3");
351                 }
352         }
353         if (efi_enabled)
354                 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
355
356         machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
357 }
358
359 static void native_machine_restart(char * __unused)
360 {
361         machine_shutdown();
362         machine_emergency_restart();
363 }
364
365 static void native_machine_halt(void)
366 {
367 }
368
369 static void native_machine_power_off(void)
370 {
371         if (pm_power_off) {
372                 machine_shutdown();
373                 pm_power_off();
374         }
375 }
376
377
378 struct machine_ops machine_ops = {
379         .power_off = native_machine_power_off,
380         .shutdown = native_machine_shutdown,
381         .emergency_restart = native_machine_emergency_restart,
382         .restart = native_machine_restart,
383         .halt = native_machine_halt,
384 };
385
386 void machine_power_off(void)
387 {
388         machine_ops.power_off();
389 }
390
391 void machine_shutdown(void)
392 {
393         machine_ops.shutdown();
394 }
395
396 void machine_emergency_restart(void)
397 {
398         machine_ops.emergency_restart();
399 }
400
401 void machine_restart(char *cmd)
402 {
403         machine_ops.restart(cmd);
404 }
405
406 void machine_halt(void)
407 {
408         machine_ops.halt();
409 }