ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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         /* Remap the kernel at virtual address zero, as well as offset zero
159            from the kernel segment.  This assumes the kernel segment starts at
160            virtual address PAGE_OFFSET. */
161
162         memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
163                 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
164
165         /*
166          * Use `swapper_pg_dir' as our page directory.
167          */
168         load_cr3(swapper_pg_dir);
169
170         /* Write 0x1234 to absolute memory location 0x472.  The BIOS reads
171            this on booting to tell it to "Bypass memory test (also warm
172            boot)".  This seems like a fairly standard thing that gets set by
173            REBOOT.COM programs, and the previous reset routine did this
174            too. */
175
176         *((unsigned short *)0x472) = reboot_mode;
177
178         /* For the switch to real mode, copy some code to low memory.  It has
179            to be in the first 64k because it is running in 16-bit mode, and it
180            has to have the same physical and virtual address, because it turns
181            off paging.  Copy it near the end of the first page, out of the way
182            of BIOS variables. */
183
184         memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
185                 real_mode_switch, sizeof (real_mode_switch));
186         memcpy ((void *) (0x1000 - 100), code, length);
187
188         /* Set up the IDT for real mode. */
189
190         __asm__ __volatile__ ("lidt %0" : : "m" (real_mode_idt));
191
192         /* Set up a GDT from which we can load segment descriptors for real
193            mode.  The GDT is not used in real mode; it is just needed here to
194            prepare the descriptors. */
195
196         __asm__ __volatile__ ("lgdt %0" : : "m" (real_mode_gdt));
197
198         /* Load the data segment registers, and thus the descriptors ready for
199            real mode.  The base address of each segment is 0x100, 16 times the
200            selector value being loaded here.  This is so that the segment
201            registers don't have to be reloaded after switching to real mode:
202            the values are consistent for real mode operation already. */
203
204         __asm__ __volatile__ ("movl $0x0010,%%eax\n"
205                                 "\tmovl %%eax,%%ds\n"
206                                 "\tmovl %%eax,%%es\n"
207                                 "\tmovl %%eax,%%fs\n"
208                                 "\tmovl %%eax,%%gs\n"
209                                 "\tmovl %%eax,%%ss" : : : "eax");
210
211         /* Jump to the 16-bit code that we copied earlier.  It disables paging
212            and the cache, switches to real mode, and jumps to the BIOS reset
213            entry point. */
214
215         __asm__ __volatile__ ("ljmp $0x0008,%0"
216                                 :
217                                 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
218 }
219
220 void machine_restart(char * __unused)
221 {
222 #ifdef CONFIG_SMP
223         int cpuid;
224         
225         cpuid = GET_APIC_ID(apic_read(APIC_ID));
226
227         if (reboot_smp) {
228
229                 /* check to see if reboot_cpu is valid 
230                    if its not, default to the BSP */
231                 if ((reboot_cpu == -1) ||  
232                       (reboot_cpu > (NR_CPUS -1))  || 
233                       !physid_isset(cpuid, phys_cpu_present_map))
234                         reboot_cpu = boot_cpu_physical_apicid;
235
236                 reboot_smp = 0;  /* use this as a flag to only go through this once*/
237                 /* re-run this function on the other CPUs
238                    it will fall though this section since we have 
239                    cleared reboot_smp, and do the reboot if it is the
240                    correct CPU, otherwise it halts. */
241                 if (reboot_cpu != cpuid)
242                         smp_call_function((void *)machine_restart , NULL, 1, 0);
243         }
244
245         /* if reboot_cpu is still -1, then we want a tradional reboot, 
246            and if we are not running on the reboot_cpu,, halt */
247         if ((reboot_cpu != -1) && (cpuid != reboot_cpu)) {
248                 for (;;)
249                 __asm__ __volatile__ ("hlt");
250         }
251         /*
252          * Stop all CPUs and turn off local APICs and the IO-APIC, so
253          * other OSs see a clean IRQ state.
254          */
255         smp_send_stop();
256 #elif defined(CONFIG_X86_LOCAL_APIC)
257         if (cpu_has_apic) {
258                 local_irq_disable();
259                 disable_local_APIC();
260                 local_irq_enable();
261         }
262 #endif
263 #ifdef CONFIG_X86_IO_APIC
264         disable_IO_APIC();
265 #endif
266
267         if (!reboot_thru_bios) {
268                 if (efi_enabled) {
269                         efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, 0);
270                         __asm__ __volatile__("lidt %0": :"m" (no_idt));
271                         __asm__ __volatile__("int3");
272                 }
273                 /* rebooting needs to touch the page at absolute addr 0 */
274                 *((unsigned short *)__va(0x472)) = reboot_mode;
275                 for (;;) {
276                         mach_reboot();
277                         /* That didn't work - force a triple fault.. */
278                         __asm__ __volatile__("lidt %0": :"m" (no_idt));
279                         __asm__ __volatile__("int3");
280                 }
281         }
282         if (efi_enabled)
283                 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, 0);
284
285         machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
286 }
287
288 EXPORT_SYMBOL(machine_restart);
289
290 void machine_halt(void)
291 {
292 }
293
294 EXPORT_SYMBOL(machine_halt);
295
296 void machine_power_off(void)
297 {
298         if (efi_enabled)
299                 efi.reset_system(EFI_RESET_SHUTDOWN, EFI_SUCCESS, 0, 0);
300         if (pm_power_off)
301                 pm_power_off();
302 }
303
304 EXPORT_SYMBOL(machine_power_off);
305