2 * linux/arch/i386/kernel/reboot.c
5 #include <linux/config.h>
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/mc146818rtc.h>
12 #include <linux/efi.h>
13 #include <linux/dmi.h>
14 #include <linux/ctype.h>
16 #include <asm/uaccess.h>
19 #include "mach_reboot.h"
20 #include <linux/reboot_fixups.h>
23 * Power off function, if any
25 void (*pm_power_off)(void);
26 EXPORT_SYMBOL(pm_power_off);
28 static int reboot_mode;
29 static int reboot_thru_bios;
32 static int reboot_cpu = -1;
34 static int __init reboot_setup(char *str)
38 case 'w': /* "warm" reboot (no memory testing etc) */
41 case 'c': /* "cold" reboot (with memory testing etc) */
44 case 'b': /* "bios" reboot by jumping through the BIOS */
47 case 'h': /* "hard" reboot by toggling RESET and/or crashing the CPU */
51 case 's': /* "smp" reboot by executing reset on BSP or other CPU*/
52 if (isdigit(*(str+1))) {
53 reboot_cpu = (int) (*(str+1) - '0');
54 if (isdigit(*(str+2)))
55 reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
57 /* we will leave sorting out the final value
58 when we are ready to reboot, since we might not
59 have set up boot_cpu_id or smp_num_cpu */
63 if((str = strchr(str,',')) != NULL)
71 __setup("reboot=", reboot_setup);
74 * Reboot options and system auto-detection code provided by
75 * Dell Inc. so their systems "just work". :-)
79 * Some machines require the "reboot=b" commandline option, this quirk makes that automatic.
81 static int __init set_bios_reboot(struct dmi_system_id *d)
83 if (!reboot_thru_bios) {
85 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
90 static struct dmi_system_id __initdata reboot_dmi_table[] = {
91 { /* Handle problems with rebooting on Dell 1300's */
92 .callback = set_bios_reboot,
93 .ident = "Dell PowerEdge 1300",
95 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
96 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
99 { /* Handle problems with rebooting on Dell 300's */
100 .callback = set_bios_reboot,
101 .ident = "Dell PowerEdge 300",
103 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
104 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
107 { /* Handle problems with rebooting on Dell 2400's */
108 .callback = set_bios_reboot,
109 .ident = "Dell PowerEdge 2400",
111 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
112 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
115 { /* Handle problems with rebooting on HP laptops */
116 .callback = set_bios_reboot,
117 .ident = "HP Compaq Laptop",
119 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
120 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
126 static int __init reboot_init(void)
128 dmi_check_system(reboot_dmi_table);
132 core_initcall(reboot_init);
134 /* The following code and data reboots the machine by switching to real
135 mode and jumping to the BIOS reset entry point, as if the CPU has
136 really been reset. The previous version asked the keyboard
137 controller to pulse the CPU reset line, which is more thorough, but
138 doesn't work with at least one type of 486 motherboard. It is easy
139 to stop this code working; hence the copious comments. */
141 static unsigned long long
142 real_mode_gdt_entries [3] =
144 0x0000000000000000ULL, /* Null descriptor */
145 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
146 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
151 unsigned short size __attribute__ ((packed));
152 unsigned long long * base __attribute__ ((packed));
154 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
155 real_mode_idt = { 0x3ff, NULL },
156 no_idt = { 0, NULL };
159 /* This is 16-bit protected mode code to disable paging and the cache,
160 switch to real mode and jump to the BIOS reset code.
162 The instruction that switches to real mode by writing to CR0 must be
163 followed immediately by a far jump instruction, which set CS to a
164 valid value for real mode, and flushes the prefetch queue to avoid
165 running instructions that have already been decoded in protected
168 Clears all the flags except ET, especially PG (paging), PE
169 (protected-mode enable) and TS (task switch for coprocessor state
170 save). Flushes the TLB after paging has been disabled. Sets CD and
171 NW, to disable the cache on a 486, and invalidates the cache. This
172 is more like the state of a 486 after reset. I don't know if
173 something else should be done for other chips.
175 More could be done here to set up the registers as if a CPU reset had
176 occurred; hopefully real BIOSs don't assume much. */
178 static unsigned char real_mode_switch [] =
180 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
181 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
182 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
183 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
184 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
185 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
186 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
187 0x74, 0x02, /* jz f */
188 0x0f, 0x09, /* wbinvd */
189 0x24, 0x10, /* f: andb $0x10,al */
190 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
192 static unsigned char jump_to_bios [] =
194 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
198 * Switch to real mode and then execute the code
199 * specified by the code and length parameters.
200 * We assume that length will aways be less that 100!
202 void machine_real_restart(unsigned char *code, int length)
208 /* Write zero to CMOS register number 0x0f, which the BIOS POST
209 routine will recognize as telling it to do a proper reboot. (Well
210 that's what this book in front of me says -- it may only apply to
211 the Phoenix BIOS though, it's not clear). At the same time,
212 disable NMIs by setting the top bit in the CMOS address register,
213 as we're about to do peculiar things to the CPU. I'm not sure if
214 `outb_p' is needed instead of just `outb'. Use it to be on the
215 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
218 spin_lock_irqsave(&rtc_lock, flags);
219 CMOS_WRITE(0x00, 0x8f);
220 spin_unlock_irqrestore(&rtc_lock, flags);
222 /* Remap the kernel at virtual address zero, as well as offset zero
223 from the kernel segment. This assumes the kernel segment starts at
224 virtual address PAGE_OFFSET. */
226 memcpy (swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
227 sizeof (swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
230 * Use `swapper_pg_dir' as our page directory.
232 load_cr3(swapper_pg_dir);
234 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
235 this on booting to tell it to "Bypass memory test (also warm
236 boot)". This seems like a fairly standard thing that gets set by
237 REBOOT.COM programs, and the previous reset routine did this
240 *((unsigned short *)0x472) = reboot_mode;
242 /* For the switch to real mode, copy some code to low memory. It has
243 to be in the first 64k because it is running in 16-bit mode, and it
244 has to have the same physical and virtual address, because it turns
245 off paging. Copy it near the end of the first page, out of the way
246 of BIOS variables. */
248 memcpy ((void *) (0x1000 - sizeof (real_mode_switch) - 100),
249 real_mode_switch, sizeof (real_mode_switch));
250 memcpy ((void *) (0x1000 - 100), code, length);
252 /* Set up the IDT for real mode. */
254 load_idt(&real_mode_idt);
256 /* Set up a GDT from which we can load segment descriptors for real
257 mode. The GDT is not used in real mode; it is just needed here to
258 prepare the descriptors. */
260 load_gdt(&real_mode_gdt);
262 /* Load the data segment registers, and thus the descriptors ready for
263 real mode. The base address of each segment is 0x100, 16 times the
264 selector value being loaded here. This is so that the segment
265 registers don't have to be reloaded after switching to real mode:
266 the values are consistent for real mode operation already. */
268 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
269 "\tmovl %%eax,%%ds\n"
270 "\tmovl %%eax,%%es\n"
271 "\tmovl %%eax,%%fs\n"
272 "\tmovl %%eax,%%gs\n"
273 "\tmovl %%eax,%%ss" : : : "eax");
275 /* Jump to the 16-bit code that we copied earlier. It disables paging
276 and the cache, switches to real mode, and jumps to the BIOS reset
279 __asm__ __volatile__ ("ljmp $0x0008,%0"
281 : "i" ((void *) (0x1000 - sizeof (real_mode_switch) - 100)));
283 #ifdef CONFIG_APM_MODULE
284 EXPORT_SYMBOL(machine_real_restart);
287 void machine_shutdown(void)
292 /* The boot cpu is always logical cpu 0 */
295 /* See if there has been given a command line override */
296 if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
297 cpu_isset(reboot_cpu, cpu_online_map)) {
298 reboot_cpu_id = reboot_cpu;
301 /* Make certain the cpu I'm rebooting on is online */
302 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
303 reboot_cpu_id = smp_processor_id();
306 /* Make certain I only run on the appropriate processor */
307 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
309 /* O.K. Now that I'm on the appropriate processor, stop
310 * all of the others, and disable their local APICs.
314 #endif /* CONFIG_SMP */
318 #ifdef CONFIG_X86_IO_APIC
323 void machine_emergency_restart(void)
325 if (!reboot_thru_bios) {
327 efi.reset_system(EFI_RESET_COLD, EFI_SUCCESS, 0, NULL);
329 __asm__ __volatile__("int3");
331 /* rebooting needs to touch the page at absolute addr 0 */
332 *((unsigned short *)__va(0x472)) = reboot_mode;
334 mach_reboot_fixups(); /* for board specific fixups */
336 /* That didn't work - force a triple fault.. */
338 __asm__ __volatile__("int3");
342 efi.reset_system(EFI_RESET_WARM, EFI_SUCCESS, 0, NULL);
344 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
347 void machine_restart(char * __unused)
350 machine_emergency_restart();
353 void machine_halt(void)
357 void machine_power_off(void)