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 <linux/ctype.h>
15 #include <linux/reboot.h>
16 #include <asm/uaccess.h>
19 #include "mach_reboot.h"
20 #include <asm/reboot_fixups.h>
21 #include <asm/reboot.h>
24 * Power off function, if any
26 void (*pm_power_off)(void);
27 EXPORT_SYMBOL(pm_power_off);
29 static int reboot_mode;
30 static int reboot_thru_bios;
33 static int reboot_cpu = -1;
35 static int __init reboot_setup(char *str)
39 case 'w': /* "warm" reboot (no memory testing etc) */
42 case 'c': /* "cold" reboot (with memory testing etc) */
45 case 'b': /* "bios" reboot by jumping through the BIOS */
48 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
52 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
53 if (isdigit(*(str+1))) {
54 reboot_cpu = (int) (*(str+1) - '0');
55 if (isdigit(*(str+2)))
56 reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
58 /* we will leave sorting out the final value
59 when we are ready to reboot, since we might not
60 have set up boot_cpu_id or smp_num_cpu */
64 if((str = strchr(str,',')) != NULL)
72 __setup("reboot=", reboot_setup);
75 * Reboot options and system auto-detection code provided by
76 * Dell Inc. so their systems "just work". :-)
80 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
82 static int __init set_bios_reboot(struct dmi_system_id *d)
84 if (!reboot_thru_bios) {
86 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
91 static struct dmi_system_id __initdata reboot_dmi_table[] = {
92 { /* Handle problems with rebooting on Dell 1300's */
93 .callback = set_bios_reboot,
94 .ident = "Dell PowerEdge 1300",
96 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
97 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
100 { /* Handle problems with rebooting on Dell 300's */
101 .callback = set_bios_reboot,
102 .ident = "Dell PowerEdge 300",
104 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
105 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
108 { /* Handle problems with rebooting on Dell 2400's */
109 .callback = set_bios_reboot,
110 .ident = "Dell PowerEdge 2400",
112 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
113 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
116 { /* Handle problems with rebooting on HP laptops */
117 .callback = set_bios_reboot,
118 .ident = "HP Compaq Laptop",
120 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
121 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
127 static int __init reboot_init(void)
129 dmi_check_system(reboot_dmi_table);
133 core_initcall(reboot_init);
135 /* The following code and data reboots the machine by switching to real
136 mode and jumping to the BIOS reset entry point, as if the CPU has
137 really been reset. The previous version asked the keyboard
138 controller to pulse the CPU reset line, which is more thorough, but
139 doesn't work with at least one type of 486 motherboard. It is easy
140 to stop this code working; hence the copious comments. */
142 static unsigned long long
143 real_mode_gdt_entries [3] =
145 0x0000000000000000ULL, /* Null descriptor */
146 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
147 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
150 static struct Xgt_desc_struct
151 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, (long)real_mode_gdt_entries },
152 real_mode_idt = { 0x3ff, 0 },
156 /* This is 16-bit protected mode code to disable paging and the cache,
157 switch to real mode and jump to the BIOS reset code.
159 The instruction that switches to real mode by writing to CR0 must be
160 followed immediately by a far jump instruction, which set CS to a
161 valid value for real mode, and flushes the prefetch queue to avoid
162 running instructions that have already been decoded in protected
165 Clears all the flags except ET, especially PG (paging), PE
166 (protected-mode enable) and TS (task switch for coprocessor state
167 save). Flushes the TLB after paging has been disabled. Sets CD and
168 NW, to disable the cache on a 486, and invalidates the cache. This
169 is more like the state of a 486 after reset. I don't know if
170 something else should be done for other chips.
172 More could be done here to set up the registers as if a CPU reset had
173 occurred; hopefully real BIOSs don't assume much. */
175 static unsigned char real_mode_switch [] =
177 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
178 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
179 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
180 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
181 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
182 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
183 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
184 0x74, 0x02, /* jz f */
185 0x0f, 0x09, /* wbinvd */
186 0x24, 0x10, /* f: andb $0x10,al */
187 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
189 static unsigned char jump_to_bios [] =
191 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
195 * Switch to real mode and then execute the code
196 * specified by the code and length parameters.
197 * We assume that length will aways be less that 100!
199 void machine_real_restart(unsigned char *code, int length)
203 /* Write zero to CMOS register number 0x0f, which the BIOS POST
204 routine will recognize as telling it to do a proper reboot. (Well
205 that's what this book in front of me says -- it may only apply to
206 the Phoenix BIOS though, it's not clear). At the same time,
207 disable NMIs by setting the top bit in the CMOS address register,
208 as we're about to do peculiar things to the CPU. I'm not sure if
209 `outb_p' is needed instead of just `outb'. Use it to be on the
210 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
213 spin_lock(&rtc_lock);
214 CMOS_WRITE(0x00, 0x8f);
215 spin_unlock(&rtc_lock);
217 /* Remap the kernel at virtual address zero, as well as offset zero
218 from the kernel segment. This assumes the kernel segment starts at
219 virtual address PAGE_OFFSET. */
221 memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
222 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
225 * Use `swapper_pg_dir' as our page directory.
227 load_cr3(swapper_pg_dir);
229 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
230 this on booting to tell it to "Bypass memory test (also warm
231 boot)". This seems like a fairly standard thing that gets set by
232 REBOOT.COM programs, and the previous reset routine did this
235 *((unsigned short *)0x472) = reboot_mode;
237 /* For the switch to real mode, copy some code to low memory. It has
238 to be in the first 64k because it is running in 16-bit mode, and it
239 has to have the same physical and virtual address, because it turns
240 off paging. Copy it near the end of the first page, out of the way
241 of BIOS variables. */
243 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
244 real_mode_switch, sizeof (real_mode_switch));
245 memcpy ((void *) (0x1000 - 100), code, length);
247 /* Set up the IDT for real mode. */
249 load_idt(&real_mode_idt);
251 /* Set up a GDT from which we can load segment descriptors for real
252 mode. The GDT is not used in real mode; it is just needed here to
253 prepare the descriptors. */
255 load_gdt(&real_mode_gdt);
257 /* Load the data segment registers, and thus the descriptors ready for
258 real mode. The base address of each segment is 0x100, 16 times the
259 selector value being loaded here. This is so that the segment
260 registers don't have to be reloaded after switching to real mode:
261 the values are consistent for real mode operation already. */
263 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
264 "\tmovl %%eax,%%ds\n"
265 "\tmovl %%eax,%%es\n"
266 "\tmovl %%eax,%%fs\n"
267 "\tmovl %%eax,%%gs\n"
268 "\tmovl %%eax,%%ss" : : : "eax");
270 /* Jump to the 16-bit code that we copied earlier. It disables paging
271 and the cache, switches to real mode, and jumps to the BIOS reset
274 __asm__ __volatile__ ("ljmp $0x0008,%0"
276 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
278 #ifdef CONFIG_APM_MODULE
279 EXPORT_SYMBOL(machine_real_restart);
282 static void native_machine_shutdown(void)
287 /* The boot cpu is always logical cpu 0 */
290 /* See if there has been given a command line override */
291 if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
292 cpu_isset(reboot_cpu, cpu_online_map)) {
293 reboot_cpu_id = reboot_cpu;
296 /* Make certain the cpu I'm rebooting on is online */
297 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
298 reboot_cpu_id = smp_processor_id();
301 /* Make certain I only run on the appropriate processor */
302 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
304 /* O.K. Now that I'm on the appropriate processor, stop
305 * all of the others, and disable their local APICs.
309 #endif /* CONFIG_SMP */
313 #ifdef CONFIG_X86_IO_APIC
318 void __attribute__((weak)) mach_reboot_fixups(void)
322 static void native_machine_emergency_restart(void)
324 if (!reboot_thru_bios) {
326 efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
328 __asm__ __volatile__("int3");
330 /* rebooting needs to touch the page at absolute addr 0 */
331 *((unsigned short *)__va(0x472)) = reboot_mode;
333 mach_reboot_fixups(); /* for board specific fixups */
335 /* That didn't work - force a triple fault.. */
337 __asm__ __volatile__("int3");
341 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
343 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
346 static void native_machine_restart(char * __unused)
349 machine_emergency_restart();
352 static void native_machine_halt(void)
356 static void native_machine_power_off(void)
365 struct machine_ops machine_ops = {
366 .power_off = native_machine_power_off,
367 .shutdown = native_machine_shutdown,
368 .emergency_restart = native_machine_emergency_restart,
369 .restart = native_machine_restart,
370 .halt = native_machine_halt,
373 void machine_power_off(void)
375 machine_ops.power_off();
378 void machine_shutdown(void)
380 machine_ops.shutdown();
383 void machine_emergency_restart(void)
385 machine_ops.emergency_restart();
388 void machine_restart(char *cmd)
390 machine_ops.restart(cmd);
393 void machine_halt(void)