Pull thermal into release branch
[linux-drm-fsl-dcu.git] / arch / i386 / kernel / vmi.c
1 /*
2  * VMI specific paravirt-ops implementation
3  *
4  * Copyright (C) 2005, VMware, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14  * NON INFRINGEMENT.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Send feedback to zach@vmware.com
22  *
23  */
24
25 #include <linux/module.h>
26 #include <linux/cpu.h>
27 #include <linux/bootmem.h>
28 #include <linux/mm.h>
29 #include <linux/highmem.h>
30 #include <asm/vmi.h>
31 #include <asm/io.h>
32 #include <asm/fixmap.h>
33 #include <asm/apicdef.h>
34 #include <asm/apic.h>
35 #include <asm/processor.h>
36 #include <asm/timer.h>
37 #include <asm/vmi_time.h>
38 #include <asm/kmap_types.h>
39
40 /* Convenient for calling VMI functions indirectly in the ROM */
41 typedef u32 __attribute__((regparm(1))) (VROMFUNC)(void);
42 typedef u64 __attribute__((regparm(2))) (VROMLONGFUNC)(int);
43
44 #define call_vrom_func(rom,func) \
45    (((VROMFUNC *)(rom->func))())
46
47 #define call_vrom_long_func(rom,func,arg) \
48    (((VROMLONGFUNC *)(rom->func)) (arg))
49
50 static struct vrom_header *vmi_rom;
51 static int disable_pge;
52 static int disable_pse;
53 static int disable_sep;
54 static int disable_tsc;
55 static int disable_mtrr;
56 static int disable_noidle;
57 static int disable_vmi_timer;
58
59 /* Cached VMI operations */
60 static struct {
61         void (*cpuid)(void /* non-c */);
62         void (*_set_ldt)(u32 selector);
63         void (*set_tr)(u32 selector);
64         void (*set_kernel_stack)(u32 selector, u32 esp0);
65         void (*allocate_page)(u32, u32, u32, u32, u32);
66         void (*release_page)(u32, u32);
67         void (*set_pte)(pte_t, pte_t *, unsigned);
68         void (*update_pte)(pte_t *, unsigned);
69         void (*set_linear_mapping)(int, void *, u32, u32);
70         void (*_flush_tlb)(int);
71         void (*set_initial_ap_state)(int, int);
72         void (*halt)(void);
73         void (*set_lazy_mode)(int mode);
74 } vmi_ops;
75
76 /* Cached VMI operations */
77 struct vmi_timer_ops vmi_timer_ops;
78
79 /*
80  * VMI patching routines.
81  */
82 #define MNEM_CALL 0xe8
83 #define MNEM_JMP  0xe9
84 #define MNEM_RET  0xc3
85
86 #define IRQ_PATCH_INT_MASK 0
87 #define IRQ_PATCH_DISABLE  5
88
89 static inline void patch_offset(unsigned char *eip, unsigned char *dest)
90 {
91         *(unsigned long *)(eip+1) = dest-eip-5;
92 }
93
94 static unsigned patch_internal(int call, unsigned len, void *insns)
95 {
96         u64 reloc;
97         struct vmi_relocation_info *const rel = (struct vmi_relocation_info *)&reloc;
98         reloc = call_vrom_long_func(vmi_rom, get_reloc, call);
99         switch(rel->type) {
100                 case VMI_RELOCATION_CALL_REL:
101                         BUG_ON(len < 5);
102                         *(char *)insns = MNEM_CALL;
103                         patch_offset(insns, rel->eip);
104                         return 5;
105
106                 case VMI_RELOCATION_JUMP_REL:
107                         BUG_ON(len < 5);
108                         *(char *)insns = MNEM_JMP;
109                         patch_offset(insns, rel->eip);
110                         return 5;
111
112                 case VMI_RELOCATION_NOP:
113                         /* obliterate the whole thing */
114                         return 0;
115
116                 case VMI_RELOCATION_NONE:
117                         /* leave native code in place */
118                         break;
119
120                 default:
121                         BUG();
122         }
123         return len;
124 }
125
126 /*
127  * Apply patch if appropriate, return length of new instruction
128  * sequence.  The callee does nop padding for us.
129  */
130 static unsigned vmi_patch(u8 type, u16 clobbers, void *insns, unsigned len)
131 {
132         switch (type) {
133                 case PARAVIRT_PATCH(irq_disable):
134                         return patch_internal(VMI_CALL_DisableInterrupts, len, insns);
135                 case PARAVIRT_PATCH(irq_enable):
136                         return patch_internal(VMI_CALL_EnableInterrupts, len, insns);
137                 case PARAVIRT_PATCH(restore_fl):
138                         return patch_internal(VMI_CALL_SetInterruptMask, len, insns);
139                 case PARAVIRT_PATCH(save_fl):
140                         return patch_internal(VMI_CALL_GetInterruptMask, len, insns);
141                 case PARAVIRT_PATCH(iret):
142                         return patch_internal(VMI_CALL_IRET, len, insns);
143                 case PARAVIRT_PATCH(irq_enable_sysexit):
144                         return patch_internal(VMI_CALL_SYSEXIT, len, insns);
145                 default:
146                         break;
147         }
148         return len;
149 }
150
151 /* CPUID has non-C semantics, and paravirt-ops API doesn't match hardware ISA */
152 static void vmi_cpuid(unsigned int *eax, unsigned int *ebx,
153                                unsigned int *ecx, unsigned int *edx)
154 {
155         int override = 0;
156         if (*eax == 1)
157                 override = 1;
158         asm volatile ("call *%6"
159                       : "=a" (*eax),
160                         "=b" (*ebx),
161                         "=c" (*ecx),
162                         "=d" (*edx)
163                       : "0" (*eax), "2" (*ecx), "r" (vmi_ops.cpuid));
164         if (override) {
165                 if (disable_pse)
166                         *edx &= ~X86_FEATURE_PSE;
167                 if (disable_pge)
168                         *edx &= ~X86_FEATURE_PGE;
169                 if (disable_sep)
170                         *edx &= ~X86_FEATURE_SEP;
171                 if (disable_tsc)
172                         *edx &= ~X86_FEATURE_TSC;
173                 if (disable_mtrr)
174                         *edx &= ~X86_FEATURE_MTRR;
175         }
176 }
177
178 static inline void vmi_maybe_load_tls(struct desc_struct *gdt, int nr, struct desc_struct *new)
179 {
180         if (gdt[nr].a != new->a || gdt[nr].b != new->b)
181                 write_gdt_entry(gdt, nr, new->a, new->b);
182 }
183
184 static void vmi_load_tls(struct thread_struct *t, unsigned int cpu)
185 {
186         struct desc_struct *gdt = get_cpu_gdt_table(cpu);
187         vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 0, &t->tls_array[0]);
188         vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 1, &t->tls_array[1]);
189         vmi_maybe_load_tls(gdt, GDT_ENTRY_TLS_MIN + 2, &t->tls_array[2]);
190 }
191
192 static void vmi_set_ldt(const void *addr, unsigned entries)
193 {
194         unsigned cpu = smp_processor_id();
195         u32 low, high;
196
197         pack_descriptor(&low, &high, (unsigned long)addr,
198                         entries * sizeof(struct desc_struct) - 1,
199                         DESCTYPE_LDT, 0);
200         write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_LDT, low, high);
201         vmi_ops._set_ldt(entries ? GDT_ENTRY_LDT*sizeof(struct desc_struct) : 0);
202 }
203
204 static void vmi_set_tr(void)
205 {
206         vmi_ops.set_tr(GDT_ENTRY_TSS*sizeof(struct desc_struct));
207 }
208
209 static void vmi_load_esp0(struct tss_struct *tss,
210                                    struct thread_struct *thread)
211 {
212         tss->x86_tss.esp0 = thread->esp0;
213
214         /* This can only happen when SEP is enabled, no need to test "SEP"arately */
215         if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
216                 tss->x86_tss.ss1 = thread->sysenter_cs;
217                 wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
218         }
219         vmi_ops.set_kernel_stack(__KERNEL_DS, tss->x86_tss.esp0);
220 }
221
222 static void vmi_flush_tlb_user(void)
223 {
224         vmi_ops._flush_tlb(VMI_FLUSH_TLB);
225 }
226
227 static void vmi_flush_tlb_kernel(void)
228 {
229         vmi_ops._flush_tlb(VMI_FLUSH_TLB | VMI_FLUSH_GLOBAL);
230 }
231
232 /* Stub to do nothing at all; used for delays and unimplemented calls */
233 static void vmi_nop(void)
234 {
235 }
236
237 #ifdef CONFIG_DEBUG_PAGE_TYPE
238
239 #ifdef CONFIG_X86_PAE
240 #define MAX_BOOT_PTS (2048+4+1)
241 #else
242 #define MAX_BOOT_PTS (1024+1)
243 #endif
244
245 /*
246  * During boot, mem_map is not yet available in paging_init, so stash
247  * all the boot page allocations here.
248  */
249 static struct {
250         u32 pfn;
251         int type;
252 } boot_page_allocations[MAX_BOOT_PTS];
253 static int num_boot_page_allocations;
254 static int boot_allocations_applied;
255
256 void vmi_apply_boot_page_allocations(void)
257 {
258         int i;
259         BUG_ON(!mem_map);
260         for (i = 0; i < num_boot_page_allocations; i++) {
261                 struct page *page = pfn_to_page(boot_page_allocations[i].pfn);
262                 page->type = boot_page_allocations[i].type;
263                 page->type = boot_page_allocations[i].type &
264                                 ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
265         }
266         boot_allocations_applied = 1;
267 }
268
269 static void record_page_type(u32 pfn, int type)
270 {
271         BUG_ON(num_boot_page_allocations >= MAX_BOOT_PTS);
272         boot_page_allocations[num_boot_page_allocations].pfn = pfn;
273         boot_page_allocations[num_boot_page_allocations].type = type;
274         num_boot_page_allocations++;
275 }
276
277 static void check_zeroed_page(u32 pfn, int type, struct page *page)
278 {
279         u32 *ptr;
280         int i;
281         int limit = PAGE_SIZE / sizeof(int);
282
283         if (page_address(page))
284                 ptr = (u32 *)page_address(page);
285         else
286                 ptr = (u32 *)__va(pfn << PAGE_SHIFT);
287         /*
288          * When cloning the root in non-PAE mode, only the userspace
289          * pdes need to be zeroed.
290          */
291         if (type & VMI_PAGE_CLONE)
292                 limit = USER_PTRS_PER_PGD;
293         for (i = 0; i < limit; i++)
294                 BUG_ON(ptr[i]);
295 }
296
297 /*
298  * We stash the page type into struct page so we can verify the page
299  * types are used properly.
300  */
301 static void vmi_set_page_type(u32 pfn, int type)
302 {
303         /* PAE can have multiple roots per page - don't track */
304         if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
305                 return;
306
307         if (boot_allocations_applied) {
308                 struct page *page = pfn_to_page(pfn);
309                 if (type != VMI_PAGE_NORMAL)
310                         BUG_ON(page->type);
311                 else
312                         BUG_ON(page->type == VMI_PAGE_NORMAL);
313                 page->type = type & ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
314                 if (type & VMI_PAGE_ZEROED)
315                         check_zeroed_page(pfn, type, page);
316         } else {
317                 record_page_type(pfn, type);
318         }
319 }
320
321 static void vmi_check_page_type(u32 pfn, int type)
322 {
323         /* PAE can have multiple roots per page - skip checks */
324         if (PTRS_PER_PMD > 1 && (type & VMI_PAGE_PDP))
325                 return;
326
327         type &= ~(VMI_PAGE_ZEROED | VMI_PAGE_CLONE);
328         if (boot_allocations_applied) {
329                 struct page *page = pfn_to_page(pfn);
330                 BUG_ON((page->type ^ type) & VMI_PAGE_PAE);
331                 BUG_ON(type == VMI_PAGE_NORMAL && page->type);
332                 BUG_ON((type & page->type) == 0);
333         }
334 }
335 #else
336 #define vmi_set_page_type(p,t) do { } while (0)
337 #define vmi_check_page_type(p,t) do { } while (0)
338 #endif
339
340 #ifdef CONFIG_HIGHPTE
341 static void *vmi_kmap_atomic_pte(struct page *page, enum km_type type)
342 {
343         void *va = kmap_atomic(page, type);
344
345         /*
346          * Internally, the VMI ROM must map virtual addresses to physical
347          * addresses for processing MMU updates.  By the time MMU updates
348          * are issued, this information is typically already lost.
349          * Fortunately, the VMI provides a cache of mapping slots for active
350          * page tables.
351          *
352          * We use slot zero for the linear mapping of physical memory, and
353          * in HIGHPTE kernels, slot 1 and 2 for KM_PTE0 and KM_PTE1.
354          *
355          *  args:                 SLOT                 VA    COUNT PFN
356          */
357         BUG_ON(type != KM_PTE0 && type != KM_PTE1);
358         vmi_ops.set_linear_mapping((type - KM_PTE0)+1, va, 1, page_to_pfn(page));
359
360         return va;
361 }
362 #endif
363
364 static void vmi_allocate_pt(u32 pfn)
365 {
366         vmi_set_page_type(pfn, VMI_PAGE_L1);
367         vmi_ops.allocate_page(pfn, VMI_PAGE_L1, 0, 0, 0);
368 }
369
370 static void vmi_allocate_pd(u32 pfn)
371 {
372         /*
373          * This call comes in very early, before mem_map is setup.
374          * It is called only for swapper_pg_dir, which already has
375          * data on it.
376          */
377         vmi_set_page_type(pfn, VMI_PAGE_L2);
378         vmi_ops.allocate_page(pfn, VMI_PAGE_L2, 0, 0, 0);
379 }
380
381 static void vmi_allocate_pd_clone(u32 pfn, u32 clonepfn, u32 start, u32 count)
382 {
383         vmi_set_page_type(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE);
384         vmi_check_page_type(clonepfn, VMI_PAGE_L2);
385         vmi_ops.allocate_page(pfn, VMI_PAGE_L2 | VMI_PAGE_CLONE, clonepfn, start, count);
386 }
387
388 static void vmi_release_pt(u32 pfn)
389 {
390         vmi_ops.release_page(pfn, VMI_PAGE_L1);
391         vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
392 }
393
394 static void vmi_release_pd(u32 pfn)
395 {
396         vmi_ops.release_page(pfn, VMI_PAGE_L2);
397         vmi_set_page_type(pfn, VMI_PAGE_NORMAL);
398 }
399
400 /*
401  * Helper macros for MMU update flags.  We can defer updates until a flush
402  * or page invalidation only if the update is to the current address space
403  * (otherwise, there is no flush).  We must check against init_mm, since
404  * this could be a kernel update, which usually passes init_mm, although
405  * sometimes this check can be skipped if we know the particular function
406  * is only called on user mode PTEs.  We could change the kernel to pass
407  * current->active_mm here, but in particular, I was unsure if changing
408  * mm/highmem.c to do this would still be correct on other architectures.
409  */
410 #define is_current_as(mm, mustbeuser) ((mm) == current->active_mm ||    \
411                                        (!mustbeuser && (mm) == &init_mm))
412 #define vmi_flags_addr(mm, addr, level, user)                           \
413         ((level) | (is_current_as(mm, user) ?                           \
414                 (VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
415 #define vmi_flags_addr_defer(mm, addr, level, user)                     \
416         ((level) | (is_current_as(mm, user) ?                           \
417                 (VMI_PAGE_DEFER | VMI_PAGE_CURRENT_AS | ((addr) & VMI_PAGE_VA_MASK)) : 0))
418
419 static void vmi_update_pte(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
420 {
421         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
422         vmi_ops.update_pte(ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
423 }
424
425 static void vmi_update_pte_defer(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
426 {
427         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
428         vmi_ops.update_pte(ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 0));
429 }
430
431 static void vmi_set_pte(pte_t *ptep, pte_t pte)
432 {
433         /* XXX because of set_pmd_pte, this can be called on PT or PD layers */
434         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE | VMI_PAGE_PD);
435         vmi_ops.set_pte(pte, ptep, VMI_PAGE_PT);
436 }
437
438 static void vmi_set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
439 {
440         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
441         vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
442 }
443
444 static void vmi_set_pmd(pmd_t *pmdp, pmd_t pmdval)
445 {
446 #ifdef CONFIG_X86_PAE
447         const pte_t pte = { pmdval.pmd, pmdval.pmd >> 32 };
448         vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PMD);
449 #else
450         const pte_t pte = { pmdval.pud.pgd.pgd };
451         vmi_check_page_type(__pa(pmdp) >> PAGE_SHIFT, VMI_PAGE_PGD);
452 #endif
453         vmi_ops.set_pte(pte, (pte_t *)pmdp, VMI_PAGE_PD);
454 }
455
456 #ifdef CONFIG_X86_PAE
457
458 static void vmi_set_pte_atomic(pte_t *ptep, pte_t pteval)
459 {
460         /*
461          * XXX This is called from set_pmd_pte, but at both PT
462          * and PD layers so the VMI_PAGE_PT flag is wrong.  But
463          * it is only called for large page mapping changes,
464          * the Xen backend, doesn't support large pages, and the
465          * ESX backend doesn't depend on the flag.
466          */
467         set_64bit((unsigned long long *)ptep,pte_val(pteval));
468         vmi_ops.update_pte(ptep, VMI_PAGE_PT);
469 }
470
471 static void vmi_set_pte_present(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte)
472 {
473         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
474         vmi_ops.set_pte(pte, ptep, vmi_flags_addr_defer(mm, addr, VMI_PAGE_PT, 1));
475 }
476
477 static void vmi_set_pud(pud_t *pudp, pud_t pudval)
478 {
479         /* Um, eww */
480         const pte_t pte = { pudval.pgd.pgd, pudval.pgd.pgd >> 32 };
481         vmi_check_page_type(__pa(pudp) >> PAGE_SHIFT, VMI_PAGE_PGD);
482         vmi_ops.set_pte(pte, (pte_t *)pudp, VMI_PAGE_PDP);
483 }
484
485 static void vmi_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
486 {
487         const pte_t pte = { 0 };
488         vmi_check_page_type(__pa(ptep) >> PAGE_SHIFT, VMI_PAGE_PTE);
489         vmi_ops.set_pte(pte, ptep, vmi_flags_addr(mm, addr, VMI_PAGE_PT, 0));
490 }
491
492 static void vmi_pmd_clear(pmd_t *pmd)
493 {
494         const pte_t pte = { 0 };
495         vmi_check_page_type(__pa(pmd) >> PAGE_SHIFT, VMI_PAGE_PMD);
496         vmi_ops.set_pte(pte, (pte_t *)pmd, VMI_PAGE_PD);
497 }
498 #endif
499
500 #ifdef CONFIG_SMP
501 static void __devinit
502 vmi_startup_ipi_hook(int phys_apicid, unsigned long start_eip,
503                      unsigned long start_esp)
504 {
505         struct vmi_ap_state ap;
506
507         /* Default everything to zero.  This is fine for most GPRs. */
508         memset(&ap, 0, sizeof(struct vmi_ap_state));
509
510         ap.gdtr_limit = GDT_SIZE - 1;
511         ap.gdtr_base = (unsigned long) get_cpu_gdt_table(phys_apicid);
512
513         ap.idtr_limit = IDT_ENTRIES * 8 - 1;
514         ap.idtr_base = (unsigned long) idt_table;
515
516         ap.ldtr = 0;
517
518         ap.cs = __KERNEL_CS;
519         ap.eip = (unsigned long) start_eip;
520         ap.ss = __KERNEL_DS;
521         ap.esp = (unsigned long) start_esp;
522
523         ap.ds = __USER_DS;
524         ap.es = __USER_DS;
525         ap.fs = __KERNEL_PERCPU;
526         ap.gs = 0;
527
528         ap.eflags = 0;
529
530 #ifdef CONFIG_X86_PAE
531         /* efer should match BSP efer. */
532         if (cpu_has_nx) {
533                 unsigned l, h;
534                 rdmsr(MSR_EFER, l, h);
535                 ap.efer = (unsigned long long) h << 32 | l;
536         }
537 #endif
538
539         ap.cr3 = __pa(swapper_pg_dir);
540         /* Protected mode, paging, AM, WP, NE, MP. */
541         ap.cr0 = 0x80050023;
542         ap.cr4 = mmu_cr4_features;
543         vmi_ops.set_initial_ap_state((u32)&ap, phys_apicid);
544 }
545 #endif
546
547 static void vmi_set_lazy_mode(enum paravirt_lazy_mode mode)
548 {
549         static DEFINE_PER_CPU(enum paravirt_lazy_mode, lazy_mode);
550
551         if (!vmi_ops.set_lazy_mode)
552                 return;
553
554         /* Modes should never nest or overlap */
555         BUG_ON(__get_cpu_var(lazy_mode) && !(mode == PARAVIRT_LAZY_NONE ||
556                                              mode == PARAVIRT_LAZY_FLUSH));
557
558         if (mode == PARAVIRT_LAZY_FLUSH) {
559                 vmi_ops.set_lazy_mode(0);
560                 vmi_ops.set_lazy_mode(__get_cpu_var(lazy_mode));
561         } else {
562                 vmi_ops.set_lazy_mode(mode);
563                 __get_cpu_var(lazy_mode) = mode;
564         }
565 }
566
567 static inline int __init check_vmi_rom(struct vrom_header *rom)
568 {
569         struct pci_header *pci;
570         struct pnp_header *pnp;
571         const char *manufacturer = "UNKNOWN";
572         const char *product = "UNKNOWN";
573         const char *license = "unspecified";
574
575         if (rom->rom_signature != 0xaa55)
576                 return 0;
577         if (rom->vrom_signature != VMI_SIGNATURE)
578                 return 0;
579         if (rom->api_version_maj != VMI_API_REV_MAJOR ||
580             rom->api_version_min+1 < VMI_API_REV_MINOR+1) {
581                 printk(KERN_WARNING "VMI: Found mismatched rom version %d.%d\n",
582                                 rom->api_version_maj,
583                                 rom->api_version_min);
584                 return 0;
585         }
586
587         /*
588          * Relying on the VMI_SIGNATURE field is not 100% safe, so check
589          * the PCI header and device type to make sure this is really a
590          * VMI device.
591          */
592         if (!rom->pci_header_offs) {
593                 printk(KERN_WARNING "VMI: ROM does not contain PCI header.\n");
594                 return 0;
595         }
596
597         pci = (struct pci_header *)((char *)rom+rom->pci_header_offs);
598         if (pci->vendorID != PCI_VENDOR_ID_VMWARE ||
599             pci->deviceID != PCI_DEVICE_ID_VMWARE_VMI) {
600                 /* Allow it to run... anyways, but warn */
601                 printk(KERN_WARNING "VMI: ROM from unknown manufacturer\n");
602         }
603
604         if (rom->pnp_header_offs) {
605                 pnp = (struct pnp_header *)((char *)rom+rom->pnp_header_offs);
606                 if (pnp->manufacturer_offset)
607                         manufacturer = (const char *)rom+pnp->manufacturer_offset;
608                 if (pnp->product_offset)
609                         product = (const char *)rom+pnp->product_offset;
610         }
611
612         if (rom->license_offs)
613                 license = (char *)rom+rom->license_offs;
614
615         printk(KERN_INFO "VMI: Found %s %s, API version %d.%d, ROM version %d.%d\n",
616                 manufacturer, product,
617                 rom->api_version_maj, rom->api_version_min,
618                 pci->rom_version_maj, pci->rom_version_min);
619
620         /* Don't allow BSD/MIT here for now because we don't want to end up
621            with any binary only shim layers */
622         if (strcmp(license, "GPL") && strcmp(license, "GPL v2")) {
623                 printk(KERN_WARNING "VMI: Non GPL license `%s' found for ROM. Not used.\n",
624                         license);
625                 return 0;
626         }
627
628         return 1;
629 }
630
631 /*
632  * Probe for the VMI option ROM
633  */
634 static inline int __init probe_vmi_rom(void)
635 {
636         unsigned long base;
637
638         /* VMI ROM is in option ROM area, check signature */
639         for (base = 0xC0000; base < 0xE0000; base += 2048) {
640                 struct vrom_header *romstart;
641                 romstart = (struct vrom_header *)isa_bus_to_virt(base);
642                 if (check_vmi_rom(romstart)) {
643                         vmi_rom = romstart;
644                         return 1;
645                 }
646         }
647         return 0;
648 }
649
650 /*
651  * VMI setup common to all processors
652  */
653 void vmi_bringup(void)
654 {
655         /* We must establish the lowmem mapping for MMU ops to work */
656         if (vmi_ops.set_linear_mapping)
657                 vmi_ops.set_linear_mapping(0, (void *)__PAGE_OFFSET, max_low_pfn, 0);
658 }
659
660 /*
661  * Return a pointer to a VMI function or NULL if unimplemented
662  */
663 static void *vmi_get_function(int vmicall)
664 {
665         u64 reloc;
666         const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
667         reloc = call_vrom_long_func(vmi_rom, get_reloc, vmicall);
668         BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);
669         if (rel->type == VMI_RELOCATION_CALL_REL)
670                 return (void *)rel->eip;
671         else
672                 return NULL;
673 }
674
675 /*
676  * Helper macro for making the VMI paravirt-ops fill code readable.
677  * For unimplemented operations, fall back to default, unless nop
678  * is returned by the ROM.
679  */
680 #define para_fill(opname, vmicall)                              \
681 do {                                                            \
682         reloc = call_vrom_long_func(vmi_rom, get_reloc,         \
683                                     VMI_CALL_##vmicall);        \
684         if (rel->type == VMI_RELOCATION_CALL_REL)               \
685                 paravirt_ops.opname = (void *)rel->eip;         \
686         else if (rel->type == VMI_RELOCATION_NOP)               \
687                 paravirt_ops.opname = (void *)vmi_nop;          \
688         else if (rel->type != VMI_RELOCATION_NONE)              \
689                 printk(KERN_WARNING "VMI: Unknown relocation "  \
690                                     "type %d for " #vmicall"\n",\
691                                         rel->type);             \
692 } while (0)
693
694 /*
695  * Helper macro for making the VMI paravirt-ops fill code readable.
696  * For cached operations which do not match the VMI ROM ABI and must
697  * go through a tranlation stub.  Ignore NOPs, since it is not clear
698  * a NOP * VMI function corresponds to a NOP paravirt-op when the
699  * functions are not in 1-1 correspondence.
700  */
701 #define para_wrap(opname, wrapper, cache, vmicall)              \
702 do {                                                            \
703         reloc = call_vrom_long_func(vmi_rom, get_reloc,         \
704                                     VMI_CALL_##vmicall);        \
705         BUG_ON(rel->type == VMI_RELOCATION_JUMP_REL);           \
706         if (rel->type == VMI_RELOCATION_CALL_REL) {             \
707                 paravirt_ops.opname = wrapper;                  \
708                 vmi_ops.cache = (void *)rel->eip;               \
709         }                                                       \
710 } while (0)
711
712 /*
713  * Activate the VMI interface and switch into paravirtualized mode
714  */
715 static inline int __init activate_vmi(void)
716 {
717         short kernel_cs;
718         u64 reloc;
719         const struct vmi_relocation_info *rel = (struct vmi_relocation_info *)&reloc;
720
721         if (call_vrom_func(vmi_rom, vmi_init) != 0) {
722                 printk(KERN_ERR "VMI ROM failed to initialize!");
723                 return 0;
724         }
725         savesegment(cs, kernel_cs);
726
727         paravirt_ops.paravirt_enabled = 1;
728         paravirt_ops.kernel_rpl = kernel_cs & SEGMENT_RPL_MASK;
729
730         paravirt_ops.patch = vmi_patch;
731         paravirt_ops.name = "vmi";
732
733         /*
734          * Many of these operations are ABI compatible with VMI.
735          * This means we can fill in the paravirt-ops with direct
736          * pointers into the VMI ROM.  If the calling convention for
737          * these operations changes, this code needs to be updated.
738          *
739          * Exceptions
740          *  CPUID paravirt-op uses pointers, not the native ISA
741          *  halt has no VMI equivalent; all VMI halts are "safe"
742          *  no MSR support yet - just trap and emulate.  VMI uses the
743          *    same ABI as the native ISA, but Linux wants exceptions
744          *    from bogus MSR read / write handled
745          *  rdpmc is not yet used in Linux
746          */
747
748         /* CPUID is special, so very special it gets wrapped like a present */
749         para_wrap(cpuid, vmi_cpuid, cpuid, CPUID);
750
751         para_fill(clts, CLTS);
752         para_fill(get_debugreg, GetDR);
753         para_fill(set_debugreg, SetDR);
754         para_fill(read_cr0, GetCR0);
755         para_fill(read_cr2, GetCR2);
756         para_fill(read_cr3, GetCR3);
757         para_fill(read_cr4, GetCR4);
758         para_fill(write_cr0, SetCR0);
759         para_fill(write_cr2, SetCR2);
760         para_fill(write_cr3, SetCR3);
761         para_fill(write_cr4, SetCR4);
762         para_fill(save_fl, GetInterruptMask);
763         para_fill(restore_fl, SetInterruptMask);
764         para_fill(irq_disable, DisableInterrupts);
765         para_fill(irq_enable, EnableInterrupts);
766
767         para_fill(wbinvd, WBINVD);
768         para_fill(read_tsc, RDTSC);
769
770         /* The following we emulate with trap and emulate for now */
771         /* paravirt_ops.read_msr = vmi_rdmsr */
772         /* paravirt_ops.write_msr = vmi_wrmsr */
773         /* paravirt_ops.rdpmc = vmi_rdpmc */
774
775         /* TR interface doesn't pass TR value, wrap */
776         para_wrap(load_tr_desc, vmi_set_tr, set_tr, SetTR);
777
778         /* LDT is special, too */
779         para_wrap(set_ldt, vmi_set_ldt, _set_ldt, SetLDT);
780
781         para_fill(load_gdt, SetGDT);
782         para_fill(load_idt, SetIDT);
783         para_fill(store_gdt, GetGDT);
784         para_fill(store_idt, GetIDT);
785         para_fill(store_tr, GetTR);
786         paravirt_ops.load_tls = vmi_load_tls;
787         para_fill(write_ldt_entry, WriteLDTEntry);
788         para_fill(write_gdt_entry, WriteGDTEntry);
789         para_fill(write_idt_entry, WriteIDTEntry);
790         para_wrap(load_esp0, vmi_load_esp0, set_kernel_stack, UpdateKernelStack);
791         para_fill(set_iopl_mask, SetIOPLMask);
792         para_fill(io_delay, IODelay);
793         para_wrap(set_lazy_mode, vmi_set_lazy_mode, set_lazy_mode, SetLazyMode);
794
795         /* user and kernel flush are just handled with different flags to FlushTLB */
796         para_wrap(flush_tlb_user, vmi_flush_tlb_user, _flush_tlb, FlushTLB);
797         para_wrap(flush_tlb_kernel, vmi_flush_tlb_kernel, _flush_tlb, FlushTLB);
798         para_fill(flush_tlb_single, InvalPage);
799
800         /*
801          * Until a standard flag format can be agreed on, we need to
802          * implement these as wrappers in Linux.  Get the VMI ROM
803          * function pointers for the two backend calls.
804          */
805 #ifdef CONFIG_X86_PAE
806         vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxELong);
807         vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxELong);
808 #else
809         vmi_ops.set_pte = vmi_get_function(VMI_CALL_SetPxE);
810         vmi_ops.update_pte = vmi_get_function(VMI_CALL_UpdatePxE);
811 #endif
812
813         if (vmi_ops.set_pte) {
814                 paravirt_ops.set_pte = vmi_set_pte;
815                 paravirt_ops.set_pte_at = vmi_set_pte_at;
816                 paravirt_ops.set_pmd = vmi_set_pmd;
817 #ifdef CONFIG_X86_PAE
818                 paravirt_ops.set_pte_atomic = vmi_set_pte_atomic;
819                 paravirt_ops.set_pte_present = vmi_set_pte_present;
820                 paravirt_ops.set_pud = vmi_set_pud;
821                 paravirt_ops.pte_clear = vmi_pte_clear;
822                 paravirt_ops.pmd_clear = vmi_pmd_clear;
823 #endif
824         }
825
826         if (vmi_ops.update_pte) {
827                 paravirt_ops.pte_update = vmi_update_pte;
828                 paravirt_ops.pte_update_defer = vmi_update_pte_defer;
829         }
830
831         vmi_ops.allocate_page = vmi_get_function(VMI_CALL_AllocatePage);
832         if (vmi_ops.allocate_page) {
833                 paravirt_ops.alloc_pt = vmi_allocate_pt;
834                 paravirt_ops.alloc_pd = vmi_allocate_pd;
835                 paravirt_ops.alloc_pd_clone = vmi_allocate_pd_clone;
836         }
837
838         vmi_ops.release_page = vmi_get_function(VMI_CALL_ReleasePage);
839         if (vmi_ops.release_page) {
840                 paravirt_ops.release_pt = vmi_release_pt;
841                 paravirt_ops.release_pd = vmi_release_pd;
842         }
843
844         /* Set linear is needed in all cases */
845         vmi_ops.set_linear_mapping = vmi_get_function(VMI_CALL_SetLinearMapping);
846 #ifdef CONFIG_HIGHPTE
847         if (vmi_ops.set_linear_mapping)
848                 paravirt_ops.kmap_atomic_pte = vmi_kmap_atomic_pte;
849 #endif
850
851         /*
852          * These MUST always be patched.  Don't support indirect jumps
853          * through these operations, as the VMI interface may use either
854          * a jump or a call to get to these operations, depending on
855          * the backend.  They are performance critical anyway, so requiring
856          * a patch is not a big problem.
857          */
858         paravirt_ops.irq_enable_sysexit = (void *)0xfeedbab0;
859         paravirt_ops.iret = (void *)0xbadbab0;
860
861 #ifdef CONFIG_SMP
862         para_wrap(startup_ipi_hook, vmi_startup_ipi_hook, set_initial_ap_state, SetInitialAPState);
863 #endif
864
865 #ifdef CONFIG_X86_LOCAL_APIC
866         para_fill(apic_read, APICRead);
867         para_fill(apic_write, APICWrite);
868         para_fill(apic_write_atomic, APICWrite);
869 #endif
870
871         /*
872          * Check for VMI timer functionality by probing for a cycle frequency method
873          */
874         reloc = call_vrom_long_func(vmi_rom, get_reloc, VMI_CALL_GetCycleFrequency);
875         if (!disable_vmi_timer && rel->type != VMI_RELOCATION_NONE) {
876                 vmi_timer_ops.get_cycle_frequency = (void *)rel->eip;
877                 vmi_timer_ops.get_cycle_counter =
878                         vmi_get_function(VMI_CALL_GetCycleCounter);
879                 vmi_timer_ops.get_wallclock =
880                         vmi_get_function(VMI_CALL_GetWallclockTime);
881                 vmi_timer_ops.wallclock_updated =
882                         vmi_get_function(VMI_CALL_WallclockUpdated);
883                 vmi_timer_ops.set_alarm = vmi_get_function(VMI_CALL_SetAlarm);
884                 vmi_timer_ops.cancel_alarm =
885                          vmi_get_function(VMI_CALL_CancelAlarm);
886                 paravirt_ops.time_init = vmi_time_init;
887                 paravirt_ops.get_wallclock = vmi_get_wallclock;
888                 paravirt_ops.set_wallclock = vmi_set_wallclock;
889 #ifdef CONFIG_X86_LOCAL_APIC
890                 paravirt_ops.setup_boot_clock = vmi_time_bsp_init;
891                 paravirt_ops.setup_secondary_clock = vmi_time_ap_init;
892 #endif
893                 paravirt_ops.get_scheduled_cycles = vmi_get_sched_cycles;
894                 paravirt_ops.get_cpu_khz = vmi_cpu_khz;
895
896                 /* We have true wallclock functions; disable CMOS clock sync */
897                 no_sync_cmos_clock = 1;
898         } else {
899                 disable_noidle = 1;
900                 disable_vmi_timer = 1;
901         }
902
903         para_fill(safe_halt, Halt);
904
905         /*
906          * Alternative instruction rewriting doesn't happen soon enough
907          * to convert VMI_IRET to a call instead of a jump; so we have
908          * to do this before IRQs get reenabled.  Fortunately, it is
909          * idempotent.
910          */
911         apply_paravirt(__parainstructions, __parainstructions_end);
912
913         vmi_bringup();
914
915         return 1;
916 }
917
918 #undef para_fill
919
920 void __init vmi_init(void)
921 {
922         unsigned long flags;
923
924         if (!vmi_rom)
925                 probe_vmi_rom();
926         else
927                 check_vmi_rom(vmi_rom);
928
929         /* In case probing for or validating the ROM failed, basil */
930         if (!vmi_rom)
931                 return;
932
933         reserve_top_address(-vmi_rom->virtual_top);
934
935         local_irq_save(flags);
936         activate_vmi();
937
938 #ifdef CONFIG_X86_IO_APIC
939         /* This is virtual hardware; timer routing is wired correctly */
940         no_timer_check = 1;
941 #endif
942         local_irq_restore(flags & X86_EFLAGS_IF);
943 }
944
945 static int __init parse_vmi(char *arg)
946 {
947         if (!arg)
948                 return -EINVAL;
949
950         if (!strcmp(arg, "disable_pge")) {
951                 clear_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability);
952                 disable_pge = 1;
953         } else if (!strcmp(arg, "disable_pse")) {
954                 clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
955                 disable_pse = 1;
956         } else if (!strcmp(arg, "disable_sep")) {
957                 clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
958                 disable_sep = 1;
959         } else if (!strcmp(arg, "disable_tsc")) {
960                 clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
961                 disable_tsc = 1;
962         } else if (!strcmp(arg, "disable_mtrr")) {
963                 clear_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability);
964                 disable_mtrr = 1;
965         } else if (!strcmp(arg, "disable_timer")) {
966                 disable_vmi_timer = 1;
967                 disable_noidle = 1;
968         } else if (!strcmp(arg, "disable_noidle"))
969                 disable_noidle = 1;
970         return 0;
971 }
972
973 early_param("vmi", parse_vmi);