Merge master.kernel.org:/home/rmk/linux-2.6-arm
[linux-2.6] / 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/module.h>
12 #include <linux/suspend.h>
13
14 static struct saved_context saved_context;
15
16 unsigned long saved_context_ebx;
17 unsigned long saved_context_esp, saved_context_ebp;
18 unsigned long saved_context_esi, saved_context_edi;
19 unsigned long saved_context_eflags;
20
21 void __save_processor_state(struct saved_context *ctxt)
22 {
23         kernel_fpu_begin();
24
25         /*
26          * descriptor tables
27          */
28         store_gdt(&ctxt->gdt_limit);
29         store_idt(&ctxt->idt_limit);
30         store_tr(ctxt->tr);
31
32         /*
33          * segment registers
34          */
35         savesegment(es, ctxt->es);
36         savesegment(fs, ctxt->fs);
37         savesegment(gs, ctxt->gs);
38         savesegment(ss, ctxt->ss);
39
40         /*
41          * control registers 
42          */
43         ctxt->cr0 = read_cr0();
44         ctxt->cr2 = read_cr2();
45         ctxt->cr3 = read_cr3();
46         ctxt->cr4 = read_cr4();
47 }
48
49 void save_processor_state(void)
50 {
51         __save_processor_state(&saved_context);
52 }
53
54 static void
55 do_fpu_end(void)
56 {
57         /* restore FPU regs if necessary */
58         /* Do it out of line so that gcc does not move cr0 load to some stupid place */
59         kernel_fpu_end();
60         mxcsr_feature_mask_init();
61 }
62
63
64 static void fix_processor_context(void)
65 {
66         int cpu = smp_processor_id();
67         struct tss_struct * t = &per_cpu(init_tss, cpu);
68
69         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. */
70
71         load_TR_desc();                         /* This does ltr */
72         load_LDT(&current->active_mm->context); /* This does lldt */
73
74         /*
75          * Now maybe reload the debug registers
76          */
77         if (current->thread.debugreg[7]){
78                 set_debugreg(current->thread.debugreg[0], 0);
79                 set_debugreg(current->thread.debugreg[1], 1);
80                 set_debugreg(current->thread.debugreg[2], 2);
81                 set_debugreg(current->thread.debugreg[3], 3);
82                 /* no 4 and 5 */
83                 set_debugreg(current->thread.debugreg[6], 6);
84                 set_debugreg(current->thread.debugreg[7], 7);
85         }
86
87 }
88
89 void __restore_processor_state(struct saved_context *ctxt)
90 {
91         /*
92          * control registers
93          */
94         write_cr4(ctxt->cr4);
95         write_cr3(ctxt->cr3);
96         write_cr2(ctxt->cr2);
97         write_cr2(ctxt->cr0);
98
99         /*
100          * now restore the descriptor tables to their proper values
101          * ltr is done i fix_processor_context().
102          */
103         load_gdt(&ctxt->gdt_limit);
104         load_idt(&ctxt->idt_limit);
105
106         /*
107          * segment registers
108          */
109         loadsegment(es, ctxt->es);
110         loadsegment(fs, ctxt->fs);
111         loadsegment(gs, ctxt->gs);
112         loadsegment(ss, ctxt->ss);
113
114         /*
115          * sysenter MSRs
116          */
117         if (boot_cpu_has(X86_FEATURE_SEP))
118                 enable_sep_cpu();
119
120         fix_processor_context();
121         do_fpu_end();
122         mtrr_ap_init();
123 }
124
125 void restore_processor_state(void)
126 {
127         __restore_processor_state(&saved_context);
128 }
129
130 /* Needed by apm.c */
131 EXPORT_SYMBOL(save_processor_state);
132 EXPORT_SYMBOL(restore_processor_state);