]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86_64/kernel/suspend.c
Pull sn_pci_legacy_read-write into release branch
[karo-tx-linux.git] / arch / x86_64 / kernel / suspend.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/smp.h>
12 #include <linux/suspend.h>
13 #include <asm/proto.h>
14 #include <asm/page.h>
15 #include <asm/pgtable.h>
16
17 struct saved_context saved_context;
18
19 unsigned long saved_context_eax, saved_context_ebx, saved_context_ecx, saved_context_edx;
20 unsigned long saved_context_esp, saved_context_ebp, saved_context_esi, saved_context_edi;
21 unsigned long saved_context_r08, saved_context_r09, saved_context_r10, saved_context_r11;
22 unsigned long saved_context_r12, saved_context_r13, saved_context_r14, saved_context_r15;
23 unsigned long saved_context_eflags;
24
25 void __save_processor_state(struct saved_context *ctxt)
26 {
27         kernel_fpu_begin();
28
29         /*
30          * descriptor tables
31          */
32         asm volatile ("sgdt %0" : "=m" (ctxt->gdt_limit));
33         asm volatile ("sidt %0" : "=m" (ctxt->idt_limit));
34         asm volatile ("str %0"  : "=m" (ctxt->tr));
35
36         /* XMM0..XMM15 should be handled by kernel_fpu_begin(). */
37         /* EFER should be constant for kernel version, no need to handle it. */
38         /*
39          * segment registers
40          */
41         asm volatile ("movw %%ds, %0" : "=m" (ctxt->ds));
42         asm volatile ("movw %%es, %0" : "=m" (ctxt->es));
43         asm volatile ("movw %%fs, %0" : "=m" (ctxt->fs));
44         asm volatile ("movw %%gs, %0" : "=m" (ctxt->gs));
45         asm volatile ("movw %%ss, %0" : "=m" (ctxt->ss));
46
47         rdmsrl(MSR_FS_BASE, ctxt->fs_base);
48         rdmsrl(MSR_GS_BASE, ctxt->gs_base);
49         rdmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
50
51         /*
52          * control registers 
53          */
54         asm volatile ("movq %%cr0, %0" : "=r" (ctxt->cr0));
55         asm volatile ("movq %%cr2, %0" : "=r" (ctxt->cr2));
56         asm volatile ("movq %%cr3, %0" : "=r" (ctxt->cr3));
57         asm volatile ("movq %%cr4, %0" : "=r" (ctxt->cr4));
58         asm volatile ("movq %%cr8, %0" : "=r" (ctxt->cr8));
59 }
60
61 void save_processor_state(void)
62 {
63         __save_processor_state(&saved_context);
64 }
65
66 static void
67 do_fpu_end(void)
68 {
69         /* restore FPU regs if necessary */
70         /* Do it out of line so that gcc does not move cr0 load to some stupid place */
71         kernel_fpu_end();
72         mxcsr_feature_mask_init();
73 }
74
75 void __restore_processor_state(struct saved_context *ctxt)
76 {
77         /*
78          * control registers
79          */
80         asm volatile ("movq %0, %%cr8" :: "r" (ctxt->cr8));
81         asm volatile ("movq %0, %%cr4" :: "r" (ctxt->cr4));
82         asm volatile ("movq %0, %%cr3" :: "r" (ctxt->cr3));
83         asm volatile ("movq %0, %%cr2" :: "r" (ctxt->cr2));
84         asm volatile ("movq %0, %%cr0" :: "r" (ctxt->cr0));
85
86         /*
87          * now restore the descriptor tables to their proper values
88          * ltr is done i fix_processor_context().
89          */
90         asm volatile ("lgdt %0" :: "m" (ctxt->gdt_limit));
91         asm volatile ("lidt %0" :: "m" (ctxt->idt_limit));
92
93         /*
94          * segment registers
95          */
96         asm volatile ("movw %0, %%ds" :: "r" (ctxt->ds));
97         asm volatile ("movw %0, %%es" :: "r" (ctxt->es));
98         asm volatile ("movw %0, %%fs" :: "r" (ctxt->fs));
99         load_gs_index(ctxt->gs);
100         asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss));
101
102         wrmsrl(MSR_FS_BASE, ctxt->fs_base);
103         wrmsrl(MSR_GS_BASE, ctxt->gs_base);
104         wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base);
105
106         fix_processor_context();
107
108         do_fpu_end();
109         mtrr_ap_init();
110 }
111
112 void restore_processor_state(void)
113 {
114         __restore_processor_state(&saved_context);
115 }
116
117 void fix_processor_context(void)
118 {
119         int cpu = smp_processor_id();
120         struct tss_struct *t = &per_cpu(init_tss, cpu);
121
122         set_tss_desc(cpu,t);    /* This just modifies memory; should not be neccessary. But... This is neccessary, because 386 hardware has concept of busy TSS or some similar stupidity. */
123
124         cpu_gdt_table[cpu][GDT_ENTRY_TSS].type = 9;
125
126         syscall_init();                         /* This sets MSR_*STAR and related */
127         load_TR_desc();                         /* This does ltr */
128         load_LDT(&current->active_mm->context); /* This does lldt */
129
130         /*
131          * Now maybe reload the debug registers
132          */
133         if (current->thread.debugreg7){
134                 loaddebug(&current->thread, 0);
135                 loaddebug(&current->thread, 1);
136                 loaddebug(&current->thread, 2);
137                 loaddebug(&current->thread, 3);
138                 /* no 4 and 5 */
139                 loaddebug(&current->thread, 6);
140                 loaddebug(&current->thread, 7);
141         }
142
143 }
144
145 #ifdef CONFIG_SOFTWARE_SUSPEND
146 /* Defined in arch/x86_64/kernel/suspend_asm.S */
147 extern int restore_image(void);
148
149 pgd_t *temp_level4_pgt;
150
151 static void **pages;
152
153 static inline void *__add_page(void)
154 {
155         void **c;
156
157         c = (void **)get_usable_page(GFP_ATOMIC);
158         if (c) {
159                 *c = pages;
160                 pages = c;
161         }
162         return c;
163 }
164
165 static inline void *__next_page(void)
166 {
167         void **c;
168
169         c = pages;
170         if (c) {
171                 pages = *c;
172                 *c = NULL;
173         }
174         return c;
175 }
176
177 /*
178  * Try to allocate as many usable pages as needed and daisy chain them.
179  * If one allocation fails, free the pages allocated so far
180  */
181 static int alloc_usable_pages(unsigned long n)
182 {
183         void *p;
184
185         pages = NULL;
186         do
187                 if (!__add_page())
188                         break;
189         while (--n);
190         if (n) {
191                 p = __next_page();
192                 while (p) {
193                         free_page((unsigned long)p);
194                         p = __next_page();
195                 }
196                 return -ENOMEM;
197         }
198         return 0;
199 }
200
201 static void res_phys_pud_init(pud_t *pud, unsigned long address, unsigned long end)
202 {
203         long i, j;
204
205         i = pud_index(address);
206         pud = pud + i;
207         for (; i < PTRS_PER_PUD; pud++, i++) {
208                 unsigned long paddr;
209                 pmd_t *pmd;
210
211                 paddr = address + i*PUD_SIZE;
212                 if (paddr >= end)
213                         break;
214
215                 pmd = (pmd_t *)__next_page();
216                 set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE));
217                 for (j = 0; j < PTRS_PER_PMD; pmd++, j++, paddr += PMD_SIZE) {
218                         unsigned long pe;
219
220                         if (paddr >= end)
221                                 break;
222                         pe = _PAGE_NX | _PAGE_PSE | _KERNPG_TABLE | paddr;
223                         pe &= __supported_pte_mask;
224                         set_pmd(pmd, __pmd(pe));
225                 }
226         }
227 }
228
229 static void set_up_temporary_mappings(void)
230 {
231         unsigned long start, end, next;
232
233         temp_level4_pgt = (pgd_t *)__next_page();
234
235         /* It is safe to reuse the original kernel mapping */
236         set_pgd(temp_level4_pgt + pgd_index(__START_KERNEL_map),
237                 init_level4_pgt[pgd_index(__START_KERNEL_map)]);
238
239         /* Set up the direct mapping from scratch */
240         start = (unsigned long)pfn_to_kaddr(0);
241         end = (unsigned long)pfn_to_kaddr(end_pfn);
242
243         for (; start < end; start = next) {
244                 pud_t *pud = (pud_t *)__next_page();
245                 next = start + PGDIR_SIZE;
246                 if (next > end)
247                         next = end;
248                 res_phys_pud_init(pud, __pa(start), __pa(next));
249                 set_pgd(temp_level4_pgt + pgd_index(start),
250                         mk_kernel_pgd(__pa(pud)));
251         }
252 }
253
254 int swsusp_arch_resume(void)
255 {
256         unsigned long n;
257
258         n = ((end_pfn << PAGE_SHIFT) + PUD_SIZE - 1) >> PUD_SHIFT;
259         n += (n + PTRS_PER_PUD - 1) / PTRS_PER_PUD + 1;
260         pr_debug("swsusp_arch_resume(): pages needed = %lu\n", n);
261         if (alloc_usable_pages(n)) {
262                 free_eaten_memory();
263                 return -ENOMEM;
264         }
265         /* We have got enough memory and from now on we cannot recover */
266         set_up_temporary_mappings();
267         restore_image();
268         return 0;
269 }
270 #endif /* CONFIG_SOFTWARE_SUSPEND */