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;
26 static int reboot_cpu = -1;
27 /* shamelessly grabbed from lib/vsprintf.c for readability */
28 #define is_digit(c) ((c) >= '0' && (c) <= '9')
30 static int __init reboot_setup(char *str)
34 case 'w': /* "warm" reboot (no memory testing etc) */
37 case 'c': /* "cold" reboot (with memory testing etc) */
40 case 'b': /* "bios" reboot by jumping through the BIOS */
43 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
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');
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 */
59 if((str = strchr(str,',')) != NULL)
67 __setup("reboot=", reboot_setup);
70 * Reboot options and system auto-detection code provided by
71 * Dell Inc. so their systems "just work". :-)
75 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
77 static int __init set_bios_reboot(struct dmi_system_id *d)
79 if (!reboot_thru_bios) {
81 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
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",
91 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
92 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
95 { /* Handle problems with rebooting on Dell 300's */
96 .callback = set_bios_reboot,
97 .ident = "Dell PowerEdge 300",
99 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
100 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
103 { /* Handle problems with rebooting on Dell 2400's */
104 .callback = set_bios_reboot,
105 .ident = "Dell PowerEdge 2400",
107 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
108 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
114 static int reboot_init(void)
116 dmi_check_system(reboot_dmi_table);
120 core_initcall(reboot_init);
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. */
129 static unsigned long long
130 real_mode_gdt_entries [3] =
132 0x0000000000000000ULL, /* Null descriptor */
133 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
134 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
139 unsigned short size __attribute__ ((packed));
140 unsigned long long * base __attribute__ ((packed));
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 };
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.
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
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.
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. */
166 static unsigned char real_mode_switch [] =
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 */
180 static unsigned char jump_to_bios [] =
182 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
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!
190 void machine_real_restart(unsigned char *code, int length)
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.)
206 spin_lock_irqsave(&rtc_lock, flags);
207 CMOS_WRITE(0x00, 0x8f);
208 spin_unlock_irqrestore(&rtc_lock, flags);
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. */
214 memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
215 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
218 * Use `swapper_pg_dir' as our page directory.
220 load_cr3(swapper_pg_dir);
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
228 *((unsigned short *)0x472) = reboot_mode;
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. */
236 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
237 real_mode_switch, sizeof (real_mode_switch));
238 memcpy ((void *) (0x1000 - 100), code, length);
240 /* Set up the IDT for real mode. */
242 __asm__ __volatile__ ("lidt %0" : : "m" (real_mode_idt));
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. */
248 __asm__ __volatile__ ("lgdt %0" : : "m" (real_mode_gdt));
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. */
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");
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
267 __asm__ __volatile__ ("ljmp $0x0008,%0"
269 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
272 void machine_shutdown(void)
277 /* The boot cpu is always logical cpu 0 */
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;
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();
291 /* Make certain I only run on the appropriate processor */
292 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
294 /* O.K. Now that I'm on the appropriate processor, stop
295 * all of the others, and disable their local APICs.
300 #elif defined(CONFIG_X86_LOCAL_APIC)
303 disable_local_APIC();
307 #ifdef CONFIG_X86_IO_APIC
312 void machine_restart(char * __unused)
316 if (!reboot_thru_bios) {
318 efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
319 __asm__ __volatile__("lidt %0": :"m" (no_idt));
320 __asm__ __volatile__("int3");
322 /* rebooting needs to touch the page at absolute addr 0 */
323 *((unsigned short *)__va(0x472)) = reboot_mode;
326 /* That didn't work - force a triple fault.. */
327 __asm__ __volatile__("lidt %0": :"m" (no_idt));
328 __asm__ __volatile__("int3");
332 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
334 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
337 EXPORT_SYMBOL(machine_restart);
339 void machine_halt(void)
343 EXPORT_SYMBOL(machine_halt);
345 void machine_power_off(void)
348 efi.reset_system(EFI_RESET_SHUTDOWN, EFI_SUCCESS, 0, NULL);
353 EXPORT_SYMBOL(machine_power_off);