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