1 #include <linux/module.h>
2 #include <linux/reboot.h>
3 #include <linux/init.h>
6 #include <acpi/reboot.h>
11 #include <asm/pgtable.h>
12 #include <asm/proto.h>
13 #include <asm/reboot_fixups.h>
14 #include <asm/reboot.h>
17 # include <linux/dmi.h>
18 # include <linux/ctype.h>
19 # include <linux/mc146818rtc.h>
21 # include <asm/iommu.h>
25 * Power off function, if any
27 void (*pm_power_off)(void);
28 EXPORT_SYMBOL(pm_power_off);
30 static const struct desc_ptr no_idt = {};
31 static int reboot_mode;
32 enum reboot_type reboot_type = BOOT_KBD;
35 #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
36 static int reboot_cpu = -1;
39 /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
40 bool port_cf9_safe = false;
42 /* reboot=b[ios] | s[mp] | t[riple] | k[bd] | e[fi] [, [w]arm | [c]old] | p[ci]
43 warm Don't set the cold reboot flag
44 cold Set the cold reboot flag
45 bios Reboot by jumping through the BIOS (only for X86_32)
46 smp Reboot by executing reset on BSP or other CPU (only for X86_32)
47 triple Force a triple fault (init)
48 kbd Use the keyboard controller. cold reset (default)
49 acpi Use the RESET_REG in the FADT
50 efi Use efi reset_system runtime service
51 pci Use the so-called "PCI reset register", CF9
52 force Avoid anything that could hang.
54 static int __init reboot_setup(char *str)
69 if (isdigit(*(str+1))) {
70 reboot_cpu = (int) (*(str+1) - '0');
71 if (isdigit(*(str+2)))
72 reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
74 /* we will leave sorting out the final value
75 when we are ready to reboot, since we might not
76 have set up boot_cpu_id or smp_num_cpu */
78 #endif /* CONFIG_SMP */
95 str = strchr(str, ',');
104 __setup("reboot=", reboot_setup);
109 * Reboot options and system auto-detection code provided by
110 * Dell Inc. so their systems "just work". :-)
114 * Some machines require the "reboot=b" commandline option,
115 * this quirk makes that automatic.
117 static int __init set_bios_reboot(const struct dmi_system_id *d)
119 if (reboot_type != BOOT_BIOS) {
120 reboot_type = BOOT_BIOS;
121 printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
126 static struct dmi_system_id __initdata reboot_dmi_table[] = {
127 { /* Handle problems with rebooting on Dell E520's */
128 .callback = set_bios_reboot,
129 .ident = "Dell E520",
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
135 { /* Handle problems with rebooting on Dell 1300's */
136 .callback = set_bios_reboot,
137 .ident = "Dell PowerEdge 1300",
139 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
140 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
143 { /* Handle problems with rebooting on Dell 300's */
144 .callback = set_bios_reboot,
145 .ident = "Dell PowerEdge 300",
147 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
148 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
151 { /* Handle problems with rebooting on Dell Optiplex 745's SFF*/
152 .callback = set_bios_reboot,
153 .ident = "Dell OptiPlex 745",
155 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
156 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
159 { /* Handle problems with rebooting on Dell Optiplex 745's DFF*/
160 .callback = set_bios_reboot,
161 .ident = "Dell OptiPlex 745",
163 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
164 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
165 DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
168 { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
169 .callback = set_bios_reboot,
170 .ident = "Dell OptiPlex 745",
172 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
173 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
174 DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
177 { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
178 .callback = set_bios_reboot,
179 .ident = "Dell OptiPlex 330",
181 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
182 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
183 DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
186 { /* Handle problems with rebooting on Dell 2400's */
187 .callback = set_bios_reboot,
188 .ident = "Dell PowerEdge 2400",
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
191 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
194 { /* Handle problems with rebooting on Dell T5400's */
195 .callback = set_bios_reboot,
196 .ident = "Dell Precision T5400",
198 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
199 DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
202 { /* Handle problems with rebooting on HP laptops */
203 .callback = set_bios_reboot,
204 .ident = "HP Compaq Laptop",
206 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
207 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
213 static int __init reboot_init(void)
215 dmi_check_system(reboot_dmi_table);
218 core_initcall(reboot_init);
220 /* The following code and data reboots the machine by switching to real
221 mode and jumping to the BIOS reset entry point, as if the CPU has
222 really been reset. The previous version asked the keyboard
223 controller to pulse the CPU reset line, which is more thorough, but
224 doesn't work with at least one type of 486 motherboard. It is easy
225 to stop this code working; hence the copious comments. */
226 static const unsigned long long
227 real_mode_gdt_entries [3] =
229 0x0000000000000000ULL, /* Null descriptor */
230 0x00009b000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
231 0x000093000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
234 static const struct desc_ptr
235 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, (long)real_mode_gdt_entries },
236 real_mode_idt = { 0x3ff, 0 };
238 /* This is 16-bit protected mode code to disable paging and the cache,
239 switch to real mode and jump to the BIOS reset code.
241 The instruction that switches to real mode by writing to CR0 must be
242 followed immediately by a far jump instruction, which set CS to a
243 valid value for real mode, and flushes the prefetch queue to avoid
244 running instructions that have already been decoded in protected
247 Clears all the flags except ET, especially PG (paging), PE
248 (protected-mode enable) and TS (task switch for coprocessor state
249 save). Flushes the TLB after paging has been disabled. Sets CD and
250 NW, to disable the cache on a 486, and invalidates the cache. This
251 is more like the state of a 486 after reset. I don't know if
252 something else should be done for other chips.
254 More could be done here to set up the registers as if a CPU reset had
255 occurred; hopefully real BIOSs don't assume much. */
256 static const unsigned char real_mode_switch [] =
258 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
259 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
260 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
261 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
262 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
263 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
264 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
265 0x74, 0x02, /* jz f */
266 0x0f, 0x09, /* wbinvd */
267 0x24, 0x10, /* f: andb $0x10,al */
268 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
270 static const unsigned char jump_to_bios [] =
272 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
276 * Switch to real mode and then execute the code
277 * specified by the code and length parameters.
278 * We assume that length will aways be less that 100!
280 void machine_real_restart(const unsigned char *code, int length)
284 /* Write zero to CMOS register number 0x0f, which the BIOS POST
285 routine will recognize as telling it to do a proper reboot. (Well
286 that's what this book in front of me says -- it may only apply to
287 the Phoenix BIOS though, it's not clear). At the same time,
288 disable NMIs by setting the top bit in the CMOS address register,
289 as we're about to do peculiar things to the CPU. I'm not sure if
290 `outb_p' is needed instead of just `outb'. Use it to be on the
291 safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
293 spin_lock(&rtc_lock);
294 CMOS_WRITE(0x00, 0x8f);
295 spin_unlock(&rtc_lock);
297 /* Remap the kernel at virtual address zero, as well as offset zero
298 from the kernel segment. This assumes the kernel segment starts at
299 virtual address PAGE_OFFSET. */
300 memcpy(swapper_pg_dir, swapper_pg_dir + KERNEL_PGD_BOUNDARY,
301 sizeof(swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
304 * Use `swapper_pg_dir' as our page directory.
306 load_cr3(swapper_pg_dir);
308 /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
309 this on booting to tell it to "Bypass memory test (also warm
310 boot)". This seems like a fairly standard thing that gets set by
311 REBOOT.COM programs, and the previous reset routine did this
313 *((unsigned short *)0x472) = reboot_mode;
315 /* For the switch to real mode, copy some code to low memory. It has
316 to be in the first 64k because it is running in 16-bit mode, and it
317 has to have the same physical and virtual address, because it turns
318 off paging. Copy it near the end of the first page, out of the way
319 of BIOS variables. */
320 memcpy((void *)(0x1000 - sizeof(real_mode_switch) - 100),
321 real_mode_switch, sizeof (real_mode_switch));
322 memcpy((void *)(0x1000 - 100), code, length);
324 /* Set up the IDT for real mode. */
325 load_idt(&real_mode_idt);
327 /* Set up a GDT from which we can load segment descriptors for real
328 mode. The GDT is not used in real mode; it is just needed here to
329 prepare the descriptors. */
330 load_gdt(&real_mode_gdt);
332 /* Load the data segment registers, and thus the descriptors ready for
333 real mode. The base address of each segment is 0x100, 16 times the
334 selector value being loaded here. This is so that the segment
335 registers don't have to be reloaded after switching to real mode:
336 the values are consistent for real mode operation already. */
337 __asm__ __volatile__ ("movl $0x0010,%%eax\n"
338 "\tmovl %%eax,%%ds\n"
339 "\tmovl %%eax,%%es\n"
340 "\tmovl %%eax,%%fs\n"
341 "\tmovl %%eax,%%gs\n"
342 "\tmovl %%eax,%%ss" : : : "eax");
344 /* Jump to the 16-bit code that we copied earlier. It disables paging
345 and the cache, switches to real mode, and jumps to the BIOS reset
347 __asm__ __volatile__ ("ljmp $0x0008,%0"
349 : "i" ((void *)(0x1000 - sizeof (real_mode_switch) - 100)));
351 #ifdef CONFIG_APM_MODULE
352 EXPORT_SYMBOL(machine_real_restart);
355 #endif /* CONFIG_X86_32 */
357 static inline void kb_wait(void)
361 for (i = 0; i < 0x10000; i++) {
362 if ((inb(0x64) & 0x02) == 0)
368 void __attribute__((weak)) mach_reboot_fixups(void)
372 static void native_machine_emergency_restart(void)
376 /* Tell the BIOS if we want cold or warm reboot */
377 *((unsigned short *)__va(0x472)) = reboot_mode;
380 /* Could also try the reset bit in the Hammer NB */
381 switch (reboot_type) {
383 mach_reboot_fixups(); /* for board specific fixups */
385 for (i = 0; i < 10; i++) {
388 outb(0xfe, 0x64); /* pulse reset low */
394 __asm__ __volatile__("int3");
396 reboot_type = BOOT_KBD;
401 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
403 reboot_type = BOOT_KBD;
409 reboot_type = BOOT_KBD;
414 efi.reset_system(reboot_mode ?
417 EFI_SUCCESS, 0, NULL);
418 reboot_type = BOOT_KBD;
422 port_cf9_safe = true;
427 u8 cf9 = inb(0xcf9) & ~6;
428 outb(cf9|2, 0xcf9); /* Request hard reset */
430 outb(cf9|6, 0xcf9); /* Actually do the reset */
433 reboot_type = BOOT_KBD;
439 void native_machine_shutdown(void)
441 /* Stop the cpus and apics */
444 /* The boot cpu is always logical cpu 0 */
445 int reboot_cpu_id = 0;
448 /* See if there has been given a command line override */
449 if ((reboot_cpu != -1) && (reboot_cpu < NR_CPUS) &&
450 cpu_online(reboot_cpu))
451 reboot_cpu_id = reboot_cpu;
454 /* Make certain the cpu I'm about to reboot on is online */
455 if (!cpu_online(reboot_cpu_id))
456 reboot_cpu_id = smp_processor_id();
458 /* Make certain I only run on the appropriate processor */
459 set_cpus_allowed_ptr(current, &cpumask_of_cpu(reboot_cpu_id));
461 /* O.K Now that I'm on the appropriate processor,
462 * stop all of the others.
469 #ifdef CONFIG_X86_IO_APIC
473 #ifdef CONFIG_HPET_TIMER
478 pci_iommu_shutdown();
482 static void native_machine_restart(char *__unused)
484 printk("machine restart\n");
488 machine_emergency_restart();
491 static void native_machine_halt(void)
493 /* stop other cpus and apics */
500 static void native_machine_power_off(void)
509 struct machine_ops machine_ops = {
510 .power_off = native_machine_power_off,
511 .shutdown = native_machine_shutdown,
512 .emergency_restart = native_machine_emergency_restart,
513 .restart = native_machine_restart,
514 .halt = native_machine_halt,
516 .crash_shutdown = native_machine_crash_shutdown,
520 void machine_power_off(void)
522 machine_ops.power_off();
525 void machine_shutdown(void)
527 machine_ops.shutdown();
530 void machine_emergency_restart(void)
532 machine_ops.emergency_restart();
535 void machine_restart(char *cmd)
537 machine_ops.restart(cmd);
540 void machine_halt(void)
546 void machine_crash_shutdown(struct pt_regs *regs)
548 machine_ops.crash_shutdown(regs);