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