ia64/xen-unstable

view xen/arch/x86/shutdown.c @ 18806:ed8524f4a044

x86: Re-initialise HPET on resume from S3

Signed-off-by: Guanqun Lu <guanqun.lu@intel.com>
Signed-off-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Nov 18 15:55:14 2008 +0000 (2008-11-18)
parents 87300352440b
children edd7f612ebe0
line source
1 /******************************************************************************
2 * arch/x86/shutdown.c
3 *
4 * x86-specific shutdown handling.
5 */
7 #include <xen/config.h>
8 #include <xen/init.h>
9 #include <xen/lib.h>
10 #include <xen/sched.h>
11 #include <xen/smp.h>
12 #include <xen/delay.h>
13 #include <xen/dmi.h>
14 #include <xen/irq.h>
15 #include <xen/console.h>
16 #include <xen/shutdown.h>
17 #include <xen/acpi.h>
18 #include <asm/msr.h>
19 #include <asm/regs.h>
20 #include <asm/mc146818rtc.h>
21 #include <asm/system.h>
22 #include <asm/io.h>
23 #include <asm/processor.h>
24 #include <asm/mpspec.h>
25 #include <asm/tboot.h>
27 enum reboot_type {
28 BOOT_TRIPLE = 't',
29 BOOT_KBD = 'k',
30 BOOT_ACPI = 'a',
31 BOOT_BIOS = 'b',
32 };
34 static long no_idt[2];
35 static int reboot_mode;
37 /*
38 * reboot=b[ios] | t[riple] | k[bd] | [, [w]arm | [c]old]
39 * warm Don't set the cold reboot flag
40 * cold Set the cold reboot flag
41 * bios Reboot by jumping through the BIOS (only for X86_32)
42 * triple Force a triple fault (init)
43 * kbd Use the keyboard controller. cold reset (default)
44 * acpi Use the RESET_REG in the FADT
45 */
46 static enum reboot_type reboot_type = BOOT_ACPI;
47 static void __init set_reboot_type(char *str)
48 {
49 for ( ; ; )
50 {
51 switch ( *str )
52 {
53 case 'w': /* "warm" reboot (no memory testing etc) */
54 reboot_mode = 0x1234;
55 break;
56 case 'c': /* "cold" reboot (with memory testing etc) */
57 reboot_mode = 0x0;
58 break;
59 case 'b':
60 case 'a':
61 case 'k':
62 case 't':
63 reboot_type = *str;
64 break;
65 }
66 if ( (str = strchr(str, ',')) == NULL )
67 break;
68 str++;
69 }
70 }
71 custom_param("reboot", set_reboot_type);
73 static inline void kb_wait(void)
74 {
75 int i;
77 for ( i = 0; i < 0x10000; i++ )
78 if ( (inb_p(0x64) & 0x02) == 0 )
79 break;
80 }
82 static void __attribute__((noreturn)) __machine_halt(void *unused)
83 {
84 local_irq_disable();
85 for ( ; ; )
86 halt();
87 }
89 void machine_halt(void)
90 {
91 watchdog_disable();
92 console_start_sync();
93 local_irq_enable();
94 smp_call_function(__machine_halt, NULL, 1, 0);
95 __machine_halt(NULL);
96 }
98 #ifdef __i386__
100 /* The following code and data reboots the machine by switching to real
101 mode and jumping to the BIOS reset entry point, as if the CPU has
102 really been reset. The previous version asked the keyboard
103 controller to pulse the CPU reset line, which is more thorough, but
104 doesn't work with at least one type of 486 motherboard. It is easy
105 to stop this code working; hence the copious comments. */
107 static unsigned long long
108 real_mode_gdt_entries [3] =
109 {
110 0x0000000000000000ULL, /* Null descriptor */
111 0x00009a000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
112 0x000092000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
113 };
115 static const struct
116 {
117 unsigned short size __attribute__ ((packed));
118 unsigned long long * base __attribute__ ((packed));
119 }
120 real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, real_mode_gdt_entries },
121 real_mode_idt = { 0x3ff, NULL };
124 /* This is 16-bit protected mode code to disable paging and the cache,
125 switch to real mode and jump to the BIOS reset code.
127 The instruction that switches to real mode by writing to CR0 must be
128 followed immediately by a far jump instruction, which set CS to a
129 valid value for real mode, and flushes the prefetch queue to avoid
130 running instructions that have already been decoded in protected
131 mode.
133 Clears all the flags except ET, especially PG (paging), PE
134 (protected-mode enable) and TS (task switch for coprocessor state
135 save). Flushes the TLB after paging has been disabled. Sets CD and
136 NW, to disable the cache on a 486, and invalidates the cache. This
137 is more like the state of a 486 after reset. I don't know if
138 something else should be done for other chips.
140 More could be done here to set up the registers as if a CPU reset had
141 occurred; hopefully real BIOSs don't assume much. */
143 static const unsigned char real_mode_switch [] =
144 {
145 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
146 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
147 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
148 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
149 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
150 0x0f, 0x20, 0xc2, /* movl %cr0,%edx */
151 0x66, 0x81, 0xe2, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%edx */
152 0x74, 0x02, /* jz f */
153 0x0f, 0x09, /* wbinvd */
154 0x24, 0x10, /* f: andb $0x10,al */
155 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
156 };
157 #define MAX_LENGTH 0x40
158 static const unsigned char jump_to_bios [] =
159 {
160 0xea, 0xf0, 0xff, 0x00, 0xf0 /* ljmp $0xf000,$0xfff0 */
161 };
163 /*
164 * Switch to real mode and then execute the code
165 * specified by the code and length parameters.
166 * We assume that length will aways be less that MAX_LENGTH!
167 */
168 static void machine_real_restart(const unsigned char *code, unsigned length)
169 {
170 local_irq_disable();
172 /* Write zero to CMOS register number 0x0f, which the BIOS POST
173 routine will recognize as telling it to do a proper reboot. (Well
174 that's what this book in front of me says -- it may only apply to
175 the Phoenix BIOS though, it's not clear). At the same time,
176 disable NMIs by setting the top bit in the CMOS address register,
177 as we're about to do peculiar things to the CPU. */
179 spin_lock(&rtc_lock);
180 CMOS_WRITE(0x00, 0x8f);
181 spin_unlock(&rtc_lock);
183 /* Identity-map virtual address zero. */
185 map_pages_to_xen(0, 0, 1, __PAGE_HYPERVISOR|MAP_SMALL_PAGES);
186 set_current(idle_vcpu[0]);
187 write_ptbase(idle_vcpu[0]);
189 /* For the switch to real mode, copy some code to low memory. It has
190 to be in the first 64k because it is running in 16-bit mode, and it
191 has to have the same physical and virtual address, because it turns
192 off paging. Copy it near the end of the first page, out of the way
193 of BIOS variables. */
195 memcpy((void *)(PAGE_SIZE - sizeof(real_mode_switch) - MAX_LENGTH),
196 real_mode_switch, sizeof(real_mode_switch));
197 memcpy((void *)(PAGE_SIZE - MAX_LENGTH), code, length);
199 /* Set up the IDT for real mode. */
201 __asm__ __volatile__("lidt %0": : "m" (real_mode_idt));
203 /* Set up a GDT from which we can load segment descriptors for real
204 mode. The GDT is not used in real mode; it is just needed here to
205 prepare the descriptors. */
207 __asm__ __volatile__("lgdt %0": : "m" (real_mode_gdt));
209 /* Load the data segment registers, and thus the descriptors ready for
210 real mode. The base address of each segment is 0x100, 16 times the
211 selector value being loaded here. This is so that the segment
212 registers don't have to be reloaded after switching to real mode:
213 the values are consistent for real mode operation already. */
215 __asm__ __volatile__ ("\tmov %0,%%ds\n"
216 "\tmov %0,%%es\n"
217 "\tmov %0,%%fs\n"
218 "\tmov %0,%%gs\n"
219 "\tmov %0,%%ss"
220 :
221 : "r" (0x0010));
223 /* Jump to the 16-bit code that we copied earlier. It disables paging
224 and the cache, switches to real mode, and jumps to the BIOS reset
225 entry point. */
227 __asm__ __volatile__ ("ljmp $0x0008,%0"
228 :
229 : "i" ((void *)(PAGE_SIZE -
230 sizeof(real_mode_switch) -
231 MAX_LENGTH)));
232 }
234 static int __init set_bios_reboot(struct dmi_system_id *d)
235 {
236 if ( reboot_type != BOOT_BIOS )
237 {
238 reboot_type = BOOT_BIOS;
239 printk("%s series board detected. "
240 "Selecting BIOS-method for reboots.\n", d->ident);
241 }
242 return 0;
243 }
245 static struct dmi_system_id __initdata reboot_dmi_table[] = {
246 { /* Handle problems with rebooting on Dell 1300's */
247 .callback = set_bios_reboot,
248 .ident = "Dell PowerEdge 1300",
249 .matches = {
250 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
251 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
252 },
253 },
254 { /* Handle problems with rebooting on Dell 300's */
255 .callback = set_bios_reboot,
256 .ident = "Dell PowerEdge 300",
257 .matches = {
258 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
259 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
260 },
261 },
262 { /* Handle problems with rebooting on Dell 2400's */
263 .callback = set_bios_reboot,
264 .ident = "Dell PowerEdge 2400",
265 .matches = {
266 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
267 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
268 },
269 },
270 { /* Handle problems with rebooting on HP laptops */
271 .callback = set_bios_reboot,
272 .ident = "HP Compaq Laptop",
273 .matches = {
274 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
275 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
276 },
277 },
278 { }
279 };
281 static int __init reboot_init(void)
282 {
283 dmi_check_system(reboot_dmi_table);
284 return 0;
285 }
286 __initcall(reboot_init);
288 #else /* __x86_64__ */
290 #define machine_real_restart(x, y)
292 #endif
294 static void __machine_restart(void *pdelay)
295 {
296 machine_restart(*(unsigned int *)pdelay);
297 }
299 void machine_restart(unsigned int delay_millisecs)
300 {
301 int i;
303 watchdog_disable();
304 console_start_sync();
306 local_irq_enable();
308 /* Ensure we are the boot CPU. */
309 if ( get_apic_id() != boot_cpu_physical_apicid )
310 {
311 /* Send IPI to the boot CPU (logical cpu 0). */
312 on_selected_cpus(cpumask_of_cpu(0), __machine_restart,
313 &delay_millisecs, 1, 0);
314 for ( ; ; )
315 halt();
316 }
318 smp_send_stop();
320 mdelay(delay_millisecs);
322 if ( tboot_in_measured_env() )
323 tboot_shutdown(TB_SHUTDOWN_REBOOT);
325 /* Rebooting needs to touch the page at absolute address 0. */
326 *((unsigned short *)__va(0x472)) = reboot_mode;
328 for ( ; ; )
329 {
330 switch ( reboot_type )
331 {
332 case BOOT_KBD:
333 /* Pulse the keyboard reset line. */
334 for ( i = 0; i < 100; i++ )
335 {
336 kb_wait();
337 udelay(50);
338 outb(0xfe,0x64); /* pulse reset low */
339 udelay(50);
340 }
341 /* fall through */
342 case BOOT_TRIPLE:
343 asm volatile ( "lidt %0 ; int3" : "=m" (no_idt) );
344 break;
345 case BOOT_BIOS:
346 machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
347 break;
348 case BOOT_ACPI:
349 acpi_reboot();
350 break;
351 }
353 reboot_type = BOOT_KBD;
354 }
355 }
357 /*
358 * Local variables:
359 * mode: C
360 * c-set-style: "BSD"
361 * c-basic-offset: 4
362 * tab-width: 4
363 * indent-tabs-mode: nil
364 * End:
365 */