cpuidle: fix HP nx6125 regression
[pandora-kernel.git] / arch / i386 / kernel / paravirt.c
1 /*  Paravirtualization interfaces
2     Copyright (C) 2006 Rusty Russell IBM Corporation
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/efi.h>
21 #include <linux/bcd.h>
22 #include <linux/highmem.h>
23
24 #include <asm/bug.h>
25 #include <asm/paravirt.h>
26 #include <asm/desc.h>
27 #include <asm/setup.h>
28 #include <asm/arch_hooks.h>
29 #include <asm/time.h>
30 #include <asm/irq.h>
31 #include <asm/delay.h>
32 #include <asm/fixmap.h>
33 #include <asm/apic.h>
34 #include <asm/tlbflush.h>
35 #include <asm/timer.h>
36
37 /* nop stub */
38 void _paravirt_nop(void)
39 {
40 }
41
42 static void __init default_banner(void)
43 {
44         printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
45                paravirt_ops.name);
46 }
47
48 char *memory_setup(void)
49 {
50         return paravirt_ops.memory_setup();
51 }
52
53 /* Simple instruction patching code. */
54 #define DEF_NATIVE(name, code)                                  \
55         extern const char start_##name[], end_##name[];         \
56         asm("start_" #name ": " code "; end_" #name ":")
57
58 DEF_NATIVE(irq_disable, "cli");
59 DEF_NATIVE(irq_enable, "sti");
60 DEF_NATIVE(restore_fl, "push %eax; popf");
61 DEF_NATIVE(save_fl, "pushf; pop %eax");
62 DEF_NATIVE(iret, "iret");
63 DEF_NATIVE(irq_enable_sysexit, "sti; sysexit");
64 DEF_NATIVE(read_cr2, "mov %cr2, %eax");
65 DEF_NATIVE(write_cr3, "mov %eax, %cr3");
66 DEF_NATIVE(read_cr3, "mov %cr3, %eax");
67 DEF_NATIVE(clts, "clts");
68 DEF_NATIVE(read_tsc, "rdtsc");
69
70 DEF_NATIVE(ud2a, "ud2a");
71
72 static unsigned native_patch(u8 type, u16 clobbers, void *ibuf,
73                              unsigned long addr, unsigned len)
74 {
75         const unsigned char *start, *end;
76         unsigned ret;
77
78         switch(type) {
79 #define SITE(x) case PARAVIRT_PATCH(x): start = start_##x; end = end_##x; goto patch_site
80                 SITE(irq_disable);
81                 SITE(irq_enable);
82                 SITE(restore_fl);
83                 SITE(save_fl);
84                 SITE(iret);
85                 SITE(irq_enable_sysexit);
86                 SITE(read_cr2);
87                 SITE(read_cr3);
88                 SITE(write_cr3);
89                 SITE(clts);
90                 SITE(read_tsc);
91 #undef SITE
92
93         patch_site:
94                 ret = paravirt_patch_insns(ibuf, len, start, end);
95                 break;
96
97         case PARAVIRT_PATCH(make_pgd):
98         case PARAVIRT_PATCH(make_pte):
99         case PARAVIRT_PATCH(pgd_val):
100         case PARAVIRT_PATCH(pte_val):
101 #ifdef CONFIG_X86_PAE
102         case PARAVIRT_PATCH(make_pmd):
103         case PARAVIRT_PATCH(pmd_val):
104 #endif
105                 /* These functions end up returning exactly what
106                    they're passed, in the same registers. */
107                 ret = paravirt_patch_nop();
108                 break;
109
110         default:
111                 ret = paravirt_patch_default(type, clobbers, ibuf, addr, len);
112                 break;
113         }
114
115         return ret;
116 }
117
118 unsigned paravirt_patch_nop(void)
119 {
120         return 0;
121 }
122
123 unsigned paravirt_patch_ignore(unsigned len)
124 {
125         return len;
126 }
127
128 struct branch {
129         unsigned char opcode;
130         u32 delta;
131 } __attribute__((packed));
132
133 unsigned paravirt_patch_call(void *insnbuf,
134                              const void *target, u16 tgt_clobbers,
135                              unsigned long addr, u16 site_clobbers,
136                              unsigned len)
137 {
138         struct branch *b = insnbuf;
139         unsigned long delta = (unsigned long)target - (addr+5);
140
141         if (tgt_clobbers & ~site_clobbers)
142                 return len;     /* target would clobber too much for this site */
143         if (len < 5)
144                 return len;     /* call too long for patch site */
145
146         b->opcode = 0xe8; /* call */
147         b->delta = delta;
148         BUILD_BUG_ON(sizeof(*b) != 5);
149
150         return 5;
151 }
152
153 unsigned paravirt_patch_jmp(const void *target, void *insnbuf,
154                             unsigned long addr, unsigned len)
155 {
156         struct branch *b = insnbuf;
157         unsigned long delta = (unsigned long)target - (addr+5);
158
159         if (len < 5)
160                 return len;     /* call too long for patch site */
161
162         b->opcode = 0xe9;       /* jmp */
163         b->delta = delta;
164
165         return 5;
166 }
167
168 unsigned paravirt_patch_default(u8 type, u16 clobbers, void *insnbuf,
169                                 unsigned long addr, unsigned len)
170 {
171         void *opfunc = *((void **)&paravirt_ops + type);
172         unsigned ret;
173
174         if (opfunc == NULL)
175                 /* If there's no function, patch it with a ud2a (BUG) */
176                 ret = paravirt_patch_insns(insnbuf, len, start_ud2a, end_ud2a);
177         else if (opfunc == paravirt_nop)
178                 /* If the operation is a nop, then nop the callsite */
179                 ret = paravirt_patch_nop();
180         else if (type == PARAVIRT_PATCH(iret) ||
181                  type == PARAVIRT_PATCH(irq_enable_sysexit))
182                 /* If operation requires a jmp, then jmp */
183                 ret = paravirt_patch_jmp(opfunc, insnbuf, addr, len);
184         else
185                 /* Otherwise call the function; assume target could
186                    clobber any caller-save reg */
187                 ret = paravirt_patch_call(insnbuf, opfunc, CLBR_ANY,
188                                           addr, clobbers, len);
189
190         return ret;
191 }
192
193 unsigned paravirt_patch_insns(void *insnbuf, unsigned len,
194                               const char *start, const char *end)
195 {
196         unsigned insn_len = end - start;
197
198         if (insn_len > len || start == NULL)
199                 insn_len = len;
200         else
201                 memcpy(insnbuf, start, insn_len);
202
203         return insn_len;
204 }
205
206 void init_IRQ(void)
207 {
208         paravirt_ops.init_IRQ();
209 }
210
211 static void native_flush_tlb(void)
212 {
213         __native_flush_tlb();
214 }
215
216 /*
217  * Global pages have to be flushed a bit differently. Not a real
218  * performance problem because this does not happen often.
219  */
220 static void native_flush_tlb_global(void)
221 {
222         __native_flush_tlb_global();
223 }
224
225 static void native_flush_tlb_single(unsigned long addr)
226 {
227         __native_flush_tlb_single(addr);
228 }
229
230 /* These are in entry.S */
231 extern void native_iret(void);
232 extern void native_irq_enable_sysexit(void);
233
234 static int __init print_banner(void)
235 {
236         paravirt_ops.banner();
237         return 0;
238 }
239 core_initcall(print_banner);
240
241 static struct resource reserve_ioports = {
242         .start = 0,
243         .end = IO_SPACE_LIMIT,
244         .name = "paravirt-ioport",
245         .flags = IORESOURCE_IO | IORESOURCE_BUSY,
246 };
247
248 static struct resource reserve_iomem = {
249         .start = 0,
250         .end = -1,
251         .name = "paravirt-iomem",
252         .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
253 };
254
255 /*
256  * Reserve the whole legacy IO space to prevent any legacy drivers
257  * from wasting time probing for their hardware.  This is a fairly
258  * brute-force approach to disabling all non-virtual drivers.
259  *
260  * Note that this must be called very early to have any effect.
261  */
262 int paravirt_disable_iospace(void)
263 {
264         int ret;
265
266         ret = request_resource(&ioport_resource, &reserve_ioports);
267         if (ret == 0) {
268                 ret = request_resource(&iomem_resource, &reserve_iomem);
269                 if (ret)
270                         release_resource(&reserve_ioports);
271         }
272
273         return ret;
274 }
275
276 struct paravirt_ops paravirt_ops = {
277         .name = "bare hardware",
278         .paravirt_enabled = 0,
279         .kernel_rpl = 0,
280         .shared_kernel_pmd = 1, /* Only used when CONFIG_X86_PAE is set */
281
282         .patch = native_patch,
283         .banner = default_banner,
284         .arch_setup = paravirt_nop,
285         .memory_setup = machine_specific_memory_setup,
286         .get_wallclock = native_get_wallclock,
287         .set_wallclock = native_set_wallclock,
288         .time_init = hpet_time_init,
289         .init_IRQ = native_init_IRQ,
290
291         .cpuid = native_cpuid,
292         .get_debugreg = native_get_debugreg,
293         .set_debugreg = native_set_debugreg,
294         .clts = native_clts,
295         .read_cr0 = native_read_cr0,
296         .write_cr0 = native_write_cr0,
297         .read_cr2 = native_read_cr2,
298         .write_cr2 = native_write_cr2,
299         .read_cr3 = native_read_cr3,
300         .write_cr3 = native_write_cr3,
301         .read_cr4 = native_read_cr4,
302         .read_cr4_safe = native_read_cr4_safe,
303         .write_cr4 = native_write_cr4,
304         .save_fl = native_save_fl,
305         .restore_fl = native_restore_fl,
306         .irq_disable = native_irq_disable,
307         .irq_enable = native_irq_enable,
308         .safe_halt = native_safe_halt,
309         .halt = native_halt,
310         .wbinvd = native_wbinvd,
311         .read_msr = native_read_msr_safe,
312         .write_msr = native_write_msr_safe,
313         .read_tsc = native_read_tsc,
314         .read_pmc = native_read_pmc,
315         .sched_clock = native_sched_clock,
316         .get_cpu_khz = native_calculate_cpu_khz,
317         .load_tr_desc = native_load_tr_desc,
318         .set_ldt = native_set_ldt,
319         .load_gdt = native_load_gdt,
320         .load_idt = native_load_idt,
321         .store_gdt = native_store_gdt,
322         .store_idt = native_store_idt,
323         .store_tr = native_store_tr,
324         .load_tls = native_load_tls,
325         .write_ldt_entry = write_dt_entry,
326         .write_gdt_entry = write_dt_entry,
327         .write_idt_entry = write_dt_entry,
328         .load_esp0 = native_load_esp0,
329
330         .set_iopl_mask = native_set_iopl_mask,
331         .io_delay = native_io_delay,
332
333 #ifdef CONFIG_X86_LOCAL_APIC
334         .apic_write = native_apic_write,
335         .apic_write_atomic = native_apic_write_atomic,
336         .apic_read = native_apic_read,
337         .setup_boot_clock = setup_boot_APIC_clock,
338         .setup_secondary_clock = setup_secondary_APIC_clock,
339         .startup_ipi_hook = paravirt_nop,
340 #endif
341         .set_lazy_mode = paravirt_nop,
342
343         .pagetable_setup_start = native_pagetable_setup_start,
344         .pagetable_setup_done = native_pagetable_setup_done,
345
346         .flush_tlb_user = native_flush_tlb,
347         .flush_tlb_kernel = native_flush_tlb_global,
348         .flush_tlb_single = native_flush_tlb_single,
349         .flush_tlb_others = native_flush_tlb_others,
350
351         .alloc_pt = paravirt_nop,
352         .alloc_pd = paravirt_nop,
353         .alloc_pd_clone = paravirt_nop,
354         .release_pt = paravirt_nop,
355         .release_pd = paravirt_nop,
356
357         .set_pte = native_set_pte,
358         .set_pte_at = native_set_pte_at,
359         .set_pmd = native_set_pmd,
360         .pte_update = paravirt_nop,
361         .pte_update_defer = paravirt_nop,
362
363 #ifdef CONFIG_HIGHPTE
364         .kmap_atomic_pte = kmap_atomic,
365 #endif
366
367 #ifdef CONFIG_X86_PAE
368         .set_pte_atomic = native_set_pte_atomic,
369         .set_pte_present = native_set_pte_present,
370         .set_pud = native_set_pud,
371         .pte_clear = native_pte_clear,
372         .pmd_clear = native_pmd_clear,
373
374         .pmd_val = native_pmd_val,
375         .make_pmd = native_make_pmd,
376 #endif
377
378         .pte_val = native_pte_val,
379         .pgd_val = native_pgd_val,
380
381         .make_pte = native_make_pte,
382         .make_pgd = native_make_pgd,
383
384         .irq_enable_sysexit = native_irq_enable_sysexit,
385         .iret = native_iret,
386
387         .dup_mmap = paravirt_nop,
388         .exit_mmap = paravirt_nop,
389         .activate_mm = paravirt_nop,
390 };
391
392 EXPORT_SYMBOL(paravirt_ops);