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*/
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');
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 */
60 if((str = strchr(str,',')) != NULL)
68 __setup("reboot=", reboot_setup);
71 * Reboot options and system auto-detection code provided by
72 * Dell Inc. so their systems "just work". :-)
76 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
78 static int __init set_bios_reboot(struct dmi_system_id *d)
80 if (!reboot_thru_bios) {
82 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
87 static struct dmi_system_id __initdata reboot_dmi_table[] = {
88 { /* Handle problems with rebooting on Dell 1300's */
89 .callback = set_bios_reboot,
90 .ident = "Dell PowerEdge 1300",
92 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
93 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
96 { /* Handle problems with rebooting on Dell 300's */
97 .callback = set_bios_reboot,
98 .ident = "Dell PowerEdge 300",
100 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
101 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
104 { /* Handle problems with rebooting on Dell 2400's */
105 .callback = set_bios_reboot,
106 .ident = "Dell PowerEdge 2400",
108 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
115 static int reboot_init(void)
117 dmi_check_system(reboot_dmi_table);
121 core_initcall(reboot_init);
123 /* The following code and data reboots the machine by switching to real
124 mode and jumping to the BIOS reset entry point, as if the CPU has
125 really been reset. The previous version asked the keyboard
126 controller to pulse the CPU reset line, which is more thorough, but
127 doesn't work with at least one type of 486 motherboard. It is easy
128 to stop this code working; hence the copious comments. */
130 static unsigned long long
131 real_mode_gdt_entries [3] =
133 0x0000000000000000ULL, /* Null descriptor */
134 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
135 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
140 unsigned short size __attribute__ ((packed));
141 unsigned long long * base __attribute__ ((packed));
143 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
144 real_mode_idt = { 0x3ff, NULL },
145 no_idt = { 0, NULL };
148 /* This is 16-bit protected mode code to disable paging and the cache,
149 switch to real mode and jump to the BIOS reset code.
151 The instruction that switches to real mode by writing to CR0 must be
152 followed immediately by a far jump instruction, which set CS to a
153 valid value for real mode, and flushes the prefetch queue to avoid
154 running instructions that have already been decoded in protected
157 Clears all the flags except ET, especially PG (paging), PE
158 (protected-mode enable) and TS (task switch for coprocessor state
159 save). Flushes the TLB after paging has been disabled. Sets CD and
160 NW, to disable the cache on a 486, and invalidates the cache. This
161 is more like the state of a 486 after reset. I don't know if
162 something else should be done for other chips.
164 More could be done here to set up the registers as if a CPU reset had
165 occurred; hopefully real BIOSs don't assume much. */
167 static unsigned char real_mode_switch [] =
169 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
170 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
171 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
172 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
173 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
174 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
175 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
176 0x74, 0x02, /* jz f */
177 0x0f, 0x09, /* wbinvd */
178 0x24, 0x10, /* f: andb $0x10,al */
179 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
181 static unsigned char jump_to_bios [] =
183 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
187 * Switch to real mode and then execute the code
188 * specified by the code and length parameters.
189 * We assume that length will aways be less that 100!
191 void machine_real_restart(unsigned char *code, int length)
197 /* Write zero to CMOS register number 0x0f, which the BIOS POST
198 routine will recognize as telling it to do a proper reboot. (Well
199 that's what this book in front of me says -- it may only apply to
200 the Phoenix BIOS though, it's not clear). At the same time,
201 disable NMIs by setting the top bit in the CMOS address register,
202 as we're about to do peculiar things to the CPU. I'm not sure if
203 `outb_p' is needed instead of just `outb'. Use it to be on the
204 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
207 spin_lock_irqsave(&rtc_lock, flags);
208 CMOS_WRITE(0x00, 0x8f);
209 spin_unlock_irqrestore(&rtc_lock, flags);
212 * Remap the first 16 MB of RAM (which includes the kernel image)
213 * at virtual address zero:
215 setup_identity_mappings(swapper_pg_dir, 0, LOW_MAPPINGS_SIZE);
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);