ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / power / cpu.c
1 /*
2  * Suspend support specific for i386.
3  *
4  * Distribute under GPLv2
5  *
6  * Copyright (c) 2002 Pavel Machek <pavel@suse.cz>
7  * Copyright (c) 2001 Patrick Mochel <mochel@osdl.org>
8  */
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/spinlock.h>
16 #include <linux/poll.h>
17 #include <linux/delay.h>
18 #include <linux/sysrq.h>
19 #include <linux/proc_fs.h>
20 #include <linux/irq.h>
21 #include <linux/pm.h>
22 #include <linux/device.h>
23 #include <linux/suspend.h>
24 #include <linux/acpi.h>
25 #include <asm/uaccess.h>
26 #include <asm/acpi.h>
27 #include <asm/tlbflush.h>
28
29 static struct saved_context saved_context;
30 static void fix_processor_context(void);
31
32 unsigned long saved_context_eax, saved_context_ebx;
33 unsigned long saved_context_ecx, saved_context_edx;
34 unsigned long saved_context_esp, saved_context_ebp;
35 unsigned long saved_context_esi, saved_context_edi;
36 unsigned long saved_context_eflags;
37
38 extern void enable_sep_cpu(void *);
39
40 void save_processor_state(void)
41 {
42         kernel_fpu_begin();
43
44         /*
45          * descriptor tables
46          */
47         asm volatile ("sgdt %0" : "=m" (saved_context.gdt_limit));
48         asm volatile ("sidt %0" : "=m" (saved_context.idt_limit));
49         asm volatile ("sldt %0" : "=m" (saved_context.ldt));
50         asm volatile ("str %0"  : "=m" (saved_context.tr));
51
52         /*
53          * segment registers
54          */
55         asm volatile ("movw %%es, %0" : "=m" (saved_context.es));
56         asm volatile ("movw %%fs, %0" : "=m" (saved_context.fs));
57         asm volatile ("movw %%gs, %0" : "=m" (saved_context.gs));
58         asm volatile ("movw %%ss, %0" : "=m" (saved_context.ss));
59
60         /*
61          * control registers 
62          */
63         asm volatile ("movl %%cr0, %0" : "=r" (saved_context.cr0));
64         asm volatile ("movl %%cr2, %0" : "=r" (saved_context.cr2));
65         asm volatile ("movl %%cr3, %0" : "=r" (saved_context.cr3));
66         asm volatile ("movl %%cr4, %0" : "=r" (saved_context.cr4));
67 }
68
69 static void
70 do_fpu_end(void)
71 {
72         /* restore FPU regs if necessary */
73         /* Do it out of line so that gcc does not move cr0 load to some stupid place */
74         kernel_fpu_end();
75         mxcsr_feature_mask_init();
76 }
77
78 void restore_processor_state(void)
79 {
80
81         /*
82          * control registers
83          */
84         asm volatile ("movl %0, %%cr4" :: "r" (saved_context.cr4));
85         asm volatile ("movl %0, %%cr3" :: "r" (saved_context.cr3));
86         asm volatile ("movl %0, %%cr2" :: "r" (saved_context.cr2));
87         asm volatile ("movl %0, %%cr0" :: "r" (saved_context.cr0));
88
89         /*
90          * segment registers
91          */
92         asm volatile ("movw %0, %%es" :: "r" (saved_context.es));
93         asm volatile ("movw %0, %%fs" :: "r" (saved_context.fs));
94         asm volatile ("movw %0, %%gs" :: "r" (saved_context.gs));
95         asm volatile ("movw %0, %%ss" :: "r" (saved_context.ss));
96
97         /*
98          * now restore the descriptor tables to their proper values
99          * ltr is done i fix_processor_context().
100          */
101         asm volatile ("lgdt %0" :: "m" (saved_context.gdt_limit));
102         asm volatile ("lidt %0" :: "m" (saved_context.idt_limit));
103         asm volatile ("lldt %0" :: "m" (saved_context.ldt));
104
105         /*
106          * sysenter MSRs
107          */
108         if (boot_cpu_has(X86_FEATURE_SEP))
109                 enable_sep_cpu(NULL);
110
111         fix_processor_context();
112         do_fpu_end();
113 }
114
115 static void fix_processor_context(void)
116 {
117         int cpu = smp_processor_id();
118         struct tss_struct * t = init_tss + cpu;
119
120         set_tss_desc(cpu,t);    /* This just modifies memory; should not be necessary. But... This is necessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
121         cpu_gdt_table[cpu][GDT_ENTRY_TSS].b &= 0xfffffdff;
122
123         load_TR_desc();                         /* This does ltr */
124         load_LDT(&current->active_mm->context); /* This does lldt */
125
126         /*
127          * Now maybe reload the debug registers
128          */
129         if (current->thread.debugreg[7]){
130                 loaddebug(&current->thread, 0);
131                 loaddebug(&current->thread, 1);
132                 loaddebug(&current->thread, 2);
133                 loaddebug(&current->thread, 3);
134                 /* no 4 and 5 */
135                 loaddebug(&current->thread, 6);
136                 loaddebug(&current->thread, 7);
137         }
138
139 }
140
141 EXPORT_SYMBOL(save_processor_state);
142 EXPORT_SYMBOL(restore_processor_state);