2 * linux/arch/i386/kernel/reboot.c
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>
15 #include "mach_reboot.h"
18 * Power off function, if any
20 void (*pm_power_off
)(void);
22 static int reboot_mode
;
27 static int reboot_cpu
= -1;
28 /* shamelessly grabbed from lib/vsprintf.c for readability */
29 #define is_digit(c) ((c) >= '0' && (c) <= '9')
31 static int __init
reboot_setup(char *str
)
35 case 'w': /* "warm" reboot (no memory testing etc) */
38 case 'c': /* "cold" reboot (with memory testing etc) */
41 case 'b': /* "bios" reboot by jumping through the BIOS */
44 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
48 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
50 if (is_digit(*(str
+1))) {
51 reboot_cpu
= (int) (*(str
+1) - '0');
52 if (is_digit(*(str
+2)))
53 reboot_cpu
= reboot_cpu
*10 + (int)(*(str
+2) - '0');
55 /* we will leave sorting out the final value
56 when we are ready to reboot, since we might not
57 have set up boot_cpu_id or smp_num_cpu */
61 if((str
= strchr(str
,',')) != NULL
)
69 __setup("reboot=", reboot_setup
);
72 * Reboot options and system auto-detection code provided by
73 * Dell Inc. so their systems "just work". :-)
77 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
79 static int __init
set_bios_reboot(struct dmi_system_id
*d
)
81 if (!reboot_thru_bios
) {
83 printk(KERN_INFO
"%s series board detected. Selecting BIOS-method for reboots.\n", d
->ident
);
89 * Some machines require the "reboot=s" commandline option, this quirk makes that automatic.
91 static int __init
set_smp_reboot(struct dmi_system_id
*d
)
96 printk(KERN_INFO
"%s series board detected. Selecting SMP-method for reboots.\n", d
->ident
);
103 * Some machines require the "reboot=b,s" commandline option, this quirk makes that automatic.
105 static int __init
set_smp_bios_reboot(struct dmi_system_id
*d
)
112 static struct dmi_system_id __initdata reboot_dmi_table
[] = {
113 { /* Handle problems with rebooting on Dell 1300's */
114 .callback
= set_smp_bios_reboot
,
115 .ident
= "Dell PowerEdge 1300",
117 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Computer Corporation"),
118 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 1300/"),
121 { /* Handle problems with rebooting on Dell 300's */
122 .callback
= set_bios_reboot
,
123 .ident
= "Dell PowerEdge 300",
125 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Computer Corporation"),
126 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 300/"),
129 { /* Handle problems with rebooting on Dell 2400's */
130 .callback
= set_bios_reboot
,
131 .ident
= "Dell PowerEdge 2400",
133 DMI_MATCH(DMI_SYS_VENDOR
, "Dell Computer Corporation"),
134 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 2400"),
140 static int reboot_init(void)
142 dmi_check_system(reboot_dmi_table
);
146 core_initcall(reboot_init
);
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. */
155 static unsigned long long
156 real_mode_gdt_entries
[3] =
158 0x0000000000000000ULL
, /* Null descriptor */
159 0x00009a000000ffffULL
, /* 16-bit real-mode 64k code at 0x00000000 */
160 0x000092000100ffffULL
/* 16-bit real-mode 64k data at 0x00000100 */
165 unsigned short size
__attribute__ ((packed
));
166 unsigned long long * base
__attribute__ ((packed
));
168 real_mode_gdt
= { sizeof (real_mode_gdt_entries
) - 1, real_mode_gdt_entries
},
169 real_mode_idt
= { 0x3ff, NULL
},
170 no_idt
= { 0, NULL
};
173 /* This is 16-bit protected mode code to disable paging and the cache,
174 switch to real mode and jump to the BIOS reset code.
176 The instruction that switches to real mode by writing to CR0 must be
177 followed immediately by a far jump instruction, which set CS to a
178 valid value for real mode, and flushes the prefetch queue to avoid
179 running instructions that have already been decoded in protected
182 Clears all the flags except ET, especially PG (paging), PE
183 (protected-mode enable) and TS (task switch for coprocessor state
184 save). Flushes the TLB after paging has been disabled. Sets CD and
185 NW, to disable the cache on a 486, and invalidates the cache. This
186 is more like the state of a 486 after reset. I don't know if
187 something else should be done for other chips.
189 More could be done here to set up the registers as if a CPU reset had
190 occurred; hopefully real BIOSs don't assume much. */
192 static unsigned char real_mode_switch
[] =
194 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
195 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
196 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
197 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
198 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
199 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
200 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
201 0x74, 0x02, /* jz f */
202 0x0f, 0x09, /* wbinvd */
203 0x24, 0x10, /* f: andb $0x10,al */
204 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
206 static unsigned char jump_to_bios
[] =
208 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
212 * Switch to real mode and then execute the code
213 * specified by the code and length parameters.
214 * We assume that length will aways be less that 100!
216 void machine_real_restart(unsigned char *code
, int length
)
222 /* Write zero to CMOS register number 0x0f, which the BIOS POST
223 routine will recognize as telling it to do a proper reboot. (Well
224 that's what this book in front of me says -- it may only apply to
225 the Phoenix BIOS though, it's not clear). At the same time,
226 disable NMIs by setting the top bit in the CMOS address register,
227 as we're about to do peculiar things to the CPU. I'm not sure if
228 `outb_p' is needed instead of just `outb'. Use it to be on the
229 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
232 spin_lock_irqsave(&rtc_lock
, flags
);
233 CMOS_WRITE(0x00, 0x8f);
234 spin_unlock_irqrestore(&rtc_lock
, flags
);
236 /* Remap the kernel at virtual address zero, as well as offset zero
237 from the kernel segment. This assumes the kernel segment starts at
238 virtual address PAGE_OFFSET. */
240 memcpy (swapper_pg_dir
, swapper_pg_dir
+ USER_PGD_PTRS
,
241 sizeof (swapper_pg_dir
[0]) * KERNEL_PGD_PTRS
);
244 * Use `swapper_pg_dir' as our page directory.
246 load_cr3(swapper_pg_dir
);
248 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
249 this on booting to tell it to "Bypass memory test (also warm
250 boot)". This seems like a fairly standard thing that gets set by
251 REBOOT.COM programs, and the previous reset routine did this
254 *((unsigned short *)0x472) = reboot_mode
;
256 /* For the switch to real mode, copy some code to low memory. It has
257 to be in the first 64k because it is running in 16-bit mode, and it
258 has to have the same physical and virtual address, because it turns
259 off paging. Copy it near the end of the first page, out of the way
260 of BIOS variables. */
262 memcpy ((void *) (0x1000 - sizeof (real_mode_switch
) - 100),
263 real_mode_switch
, sizeof (real_mode_switch
));
264 memcpy ((void *) (0x1000 - 100), code
, length
);
266 /* Set up the IDT for real mode. */
268 __asm__
__volatile__ ("lidt %0" : : "m" (real_mode_idt
));
270 /* Set up a GDT from which we can load segment descriptors for real
271 mode. The GDT is not used in real mode; it is just needed here to
272 prepare the descriptors. */
274 __asm__
__volatile__ ("lgdt %0" : : "m" (real_mode_gdt
));
276 /* Load the data segment registers, and thus the descriptors ready for
277 real mode. The base address of each segment is 0x100, 16 times the
278 selector value being loaded here. This is so that the segment
279 registers don't have to be reloaded after switching to real mode:
280 the values are consistent for real mode operation already. */
282 __asm__
__volatile__ ("movl $0x0010,%%eax\n"
283 "\tmovl %%eax,%%ds\n"
284 "\tmovl %%eax,%%es\n"
285 "\tmovl %%eax,%%fs\n"
286 "\tmovl %%eax,%%gs\n"
287 "\tmovl %%eax,%%ss" : : : "eax");
289 /* Jump to the 16-bit code that we copied earlier. It disables paging
290 and the cache, switches to real mode, and jumps to the BIOS reset
293 __asm__
__volatile__ ("ljmp $0x0008,%0"
295 : "i" ((void *) (0x1000 - sizeof (real_mode_switch
) - 100)));
298 void machine_restart(char * __unused
)
303 cpuid
= GET_APIC_ID(apic_read(APIC_ID
));
307 /* check to see if reboot_cpu is valid
308 if its not, default to the BSP */
309 if ((reboot_cpu
== -1) ||
310 (reboot_cpu
> (NR_CPUS
-1)) ||
311 !physid_isset(cpuid
, phys_cpu_present_map
))
312 reboot_cpu
= boot_cpu_physical_apicid
;
314 reboot_smp
= 0; /* use this as a flag to only go through this once*/
315 /* re-run this function on the other CPUs
316 it will fall though this section since we have
317 cleared reboot_smp, and do the reboot if it is the
318 correct CPU, otherwise it halts. */
319 if (reboot_cpu
!= cpuid
)
320 smp_call_function((void *)machine_restart
, NULL
, 1, 0);
323 /* if reboot_cpu is still -1, then we want a tradional reboot,
324 and if we are not running on the reboot_cpu,, halt */
325 if ((reboot_cpu
!= -1) && (cpuid
!= reboot_cpu
)) {
327 __asm__
__volatile__ ("hlt");
330 * Stop all CPUs and turn off local APICs and the IO-APIC, so
331 * other OSs see a clean IRQ state.
334 #elif defined(CONFIG_X86_LOCAL_APIC)
337 disable_local_APIC();
341 #ifdef CONFIG_X86_IO_APIC
345 if (!reboot_thru_bios
) {
347 efi
.reset_system(EFI_RESET_COLD
, EFI_SUCCESS
, 0, NULL
);
348 __asm__
__volatile__("lidt %0": :"m" (no_idt
));
349 __asm__
__volatile__("int3");
351 /* rebooting needs to touch the page at absolute addr 0 */
352 *((unsigned short *)__va(0x472)) = reboot_mode
;
355 /* That didn't work - force a triple fault.. */
356 __asm__
__volatile__("lidt %0": :"m" (no_idt
));
357 __asm__
__volatile__("int3");
361 efi
.reset_system(EFI_RESET_WARM
, EFI_SUCCESS
, 0, NULL
);
363 machine_real_restart(jump_to_bios
, sizeof(jump_to_bios
));
366 EXPORT_SYMBOL(machine_restart
);
368 void machine_halt(void)
372 EXPORT_SYMBOL(machine_halt
);
374 void machine_power_off(void)
377 efi
.reset_system(EFI_RESET_SHUTDOWN
, EFI_SUCCESS
, 0, NULL
);
382 EXPORT_SYMBOL(machine_power_off
);