Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / arch / arm / include / asm / kvm_host.h
1 /*
2  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License, version 2, as
7  * published by the Free Software Foundation.
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, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #ifndef __ARM_KVM_HOST_H__
20 #define __ARM_KVM_HOST_H__
21
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/kvm_types.h>
25 #include <asm/cputype.h>
26 #include <asm/kvm.h>
27 #include <asm/kvm_asm.h>
28 #include <asm/kvm_mmio.h>
29 #include <asm/fpstate.h>
30 #include <asm/smp_plat.h>
31 #include <kvm/arm_arch_timer.h>
32
33 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
34
35 #define KVM_USER_MEM_SLOTS 32
36 #define KVM_HAVE_ONE_REG
37 #define KVM_HALT_POLL_NS_DEFAULT 500000
38
39 #define KVM_VCPU_MAX_FEATURES 2
40
41 #include <kvm/arm_vgic.h>
42
43
44 #ifdef CONFIG_ARM_GIC_V3
45 #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
46 #else
47 #define KVM_MAX_VCPUS VGIC_V2_MAX_CPUS
48 #endif
49
50 #define KVM_REQ_SLEEP \
51         KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
52 #define KVM_REQ_IRQ_PENDING     KVM_ARCH_REQ(1)
53 #define KVM_REQ_VCPU_RESET      KVM_ARCH_REQ(2)
54
55 DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
56
57 static inline int kvm_arm_init_sve(void) { return 0; }
58
59 u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode);
60 int __attribute_const__ kvm_target_cpu(void);
61 int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
62 void kvm_reset_coprocs(struct kvm_vcpu *vcpu);
63
64 struct kvm_vmid {
65         /* The VMID generation used for the virt. memory system */
66         u64    vmid_gen;
67         u32    vmid;
68 };
69
70 struct kvm_arch {
71         /* The last vcpu id that ran on each physical CPU */
72         int __percpu *last_vcpu_ran;
73
74         /*
75          * Anything that is not used directly from assembly code goes
76          * here.
77          */
78
79         /* The VMID generation used for the virt. memory system */
80         struct kvm_vmid vmid;
81
82         /* Stage-2 page table */
83         pgd_t *pgd;
84         phys_addr_t pgd_phys;
85
86         /* Interrupt controller */
87         struct vgic_dist        vgic;
88         int max_vcpus;
89
90         /* Mandated version of PSCI */
91         u32 psci_version;
92 };
93
94 #define KVM_NR_MEM_OBJS     40
95
96 /*
97  * We don't want allocation failures within the mmu code, so we preallocate
98  * enough memory for a single page fault in a cache.
99  */
100 struct kvm_mmu_memory_cache {
101         int nobjs;
102         void *objects[KVM_NR_MEM_OBJS];
103 };
104
105 struct kvm_vcpu_fault_info {
106         u32 hsr;                /* Hyp Syndrome Register */
107         u32 hxfar;              /* Hyp Data/Inst. Fault Address Register */
108         u32 hpfar;              /* Hyp IPA Fault Address Register */
109 };
110
111 /*
112  * 0 is reserved as an invalid value.
113  * Order should be kept in sync with the save/restore code.
114  */
115 enum vcpu_sysreg {
116         __INVALID_SYSREG__,
117         c0_MPIDR,               /* MultiProcessor ID Register */
118         c0_CSSELR,              /* Cache Size Selection Register */
119         c1_SCTLR,               /* System Control Register */
120         c1_ACTLR,               /* Auxiliary Control Register */
121         c1_CPACR,               /* Coprocessor Access Control */
122         c2_TTBR0,               /* Translation Table Base Register 0 */
123         c2_TTBR0_high,          /* TTBR0 top 32 bits */
124         c2_TTBR1,               /* Translation Table Base Register 1 */
125         c2_TTBR1_high,          /* TTBR1 top 32 bits */
126         c2_TTBCR,               /* Translation Table Base Control R. */
127         c3_DACR,                /* Domain Access Control Register */
128         c5_DFSR,                /* Data Fault Status Register */
129         c5_IFSR,                /* Instruction Fault Status Register */
130         c5_ADFSR,               /* Auxilary Data Fault Status R */
131         c5_AIFSR,               /* Auxilary Instrunction Fault Status R */
132         c6_DFAR,                /* Data Fault Address Register */
133         c6_IFAR,                /* Instruction Fault Address Register */
134         c7_PAR,                 /* Physical Address Register */
135         c7_PAR_high,            /* PAR top 32 bits */
136         c9_L2CTLR,              /* Cortex A15/A7 L2 Control Register */
137         c10_PRRR,               /* Primary Region Remap Register */
138         c10_NMRR,               /* Normal Memory Remap Register */
139         c12_VBAR,               /* Vector Base Address Register */
140         c13_CID,                /* Context ID Register */
141         c13_TID_URW,            /* Thread ID, User R/W */
142         c13_TID_URO,            /* Thread ID, User R/O */
143         c13_TID_PRIV,           /* Thread ID, Privileged */
144         c14_CNTKCTL,            /* Timer Control Register (PL1) */
145         c10_AMAIR0,             /* Auxilary Memory Attribute Indirection Reg0 */
146         c10_AMAIR1,             /* Auxilary Memory Attribute Indirection Reg1 */
147         NR_CP15_REGS            /* Number of regs (incl. invalid) */
148 };
149
150 struct kvm_cpu_context {
151         struct kvm_regs gp_regs;
152         struct vfp_hard_struct vfp;
153         u32 cp15[NR_CP15_REGS];
154 };
155
156 struct kvm_host_data {
157         struct kvm_cpu_context host_ctxt;
158 };
159
160 typedef struct kvm_host_data kvm_host_data_t;
161
162 static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt,
163                                              int cpu)
164 {
165         /* The host's MPIDR is immutable, so let's set it up at boot time */
166         cpu_ctxt->cp15[c0_MPIDR] = cpu_logical_map(cpu);
167 }
168
169 struct vcpu_reset_state {
170         unsigned long   pc;
171         unsigned long   r0;
172         bool            be;
173         bool            reset;
174 };
175
176 struct kvm_vcpu_arch {
177         struct kvm_cpu_context ctxt;
178
179         int target; /* Processor target */
180         DECLARE_BITMAP(features, KVM_VCPU_MAX_FEATURES);
181
182         /* The CPU type we expose to the VM */
183         u32 midr;
184
185         /* HYP trapping configuration */
186         u32 hcr;
187
188         /* Exception Information */
189         struct kvm_vcpu_fault_info fault;
190
191         /* Host FP context */
192         struct kvm_cpu_context *host_cpu_context;
193
194         /* VGIC state */
195         struct vgic_cpu vgic_cpu;
196         struct arch_timer_cpu timer_cpu;
197
198         /*
199          * Anything that is not used directly from assembly code goes
200          * here.
201          */
202
203         /* vcpu power-off state */
204         bool power_off;
205
206          /* Don't run the guest (internal implementation need) */
207         bool pause;
208
209         /* IO related fields */
210         struct kvm_decode mmio_decode;
211
212         /* Cache some mmu pages needed inside spinlock regions */
213         struct kvm_mmu_memory_cache mmu_page_cache;
214
215         struct vcpu_reset_state reset_state;
216
217         /* Detect first run of a vcpu */
218         bool has_run_once;
219 };
220
221 struct kvm_vm_stat {
222         ulong remote_tlb_flush;
223 };
224
225 struct kvm_vcpu_stat {
226         u64 halt_successful_poll;
227         u64 halt_attempted_poll;
228         u64 halt_poll_invalid;
229         u64 halt_wakeup;
230         u64 hvc_exit_stat;
231         u64 wfe_exit_stat;
232         u64 wfi_exit_stat;
233         u64 mmio_exit_user;
234         u64 mmio_exit_kernel;
235         u64 exits;
236 };
237
238 #define vcpu_cp15(v,r)  (v)->arch.ctxt.cp15[r]
239
240 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
241 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
242 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
243 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
244 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);
245
246 unsigned long __kvm_call_hyp(void *hypfn, ...);
247
248 /*
249  * The has_vhe() part doesn't get emitted, but is used for type-checking.
250  */
251 #define kvm_call_hyp(f, ...)                                            \
252         do {                                                            \
253                 if (has_vhe()) {                                        \
254                         f(__VA_ARGS__);                                 \
255                 } else {                                                \
256                         __kvm_call_hyp(kvm_ksym_ref(f), ##__VA_ARGS__); \
257                 }                                                       \
258         } while(0)
259
260 #define kvm_call_hyp_ret(f, ...)                                        \
261         ({                                                              \
262                 typeof(f(__VA_ARGS__)) ret;                             \
263                                                                         \
264                 if (has_vhe()) {                                        \
265                         ret = f(__VA_ARGS__);                           \
266                 } else {                                                \
267                         ret = __kvm_call_hyp(kvm_ksym_ref(f),           \
268                                              ##__VA_ARGS__);            \
269                 }                                                       \
270                                                                         \
271                 ret;                                                    \
272         })
273
274 void force_vm_exit(const cpumask_t *mask);
275 int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
276                               struct kvm_vcpu_events *events);
277
278 int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
279                               struct kvm_vcpu_events *events);
280
281 #define KVM_ARCH_WANT_MMU_NOTIFIER
282 int kvm_unmap_hva_range(struct kvm *kvm,
283                         unsigned long start, unsigned long end);
284 int kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
285
286 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
287 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
288 int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
289 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
290
291 struct kvm_vcpu *kvm_arm_get_running_vcpu(void);
292 struct kvm_vcpu __percpu **kvm_get_running_vcpus(void);
293 void kvm_arm_halt_guest(struct kvm *kvm);
294 void kvm_arm_resume_guest(struct kvm *kvm);
295
296 int kvm_arm_copy_coproc_indices(struct kvm_vcpu *vcpu, u64 __user *uindices);
297 unsigned long kvm_arm_num_coproc_regs(struct kvm_vcpu *vcpu);
298 int kvm_arm_coproc_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
299 int kvm_arm_coproc_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
300
301 int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
302                 int exception_index);
303
304 static inline void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run,
305                                      int exception_index) {}
306
307 static inline void __cpu_init_hyp_mode(phys_addr_t pgd_ptr,
308                                        unsigned long hyp_stack_ptr,
309                                        unsigned long vector_ptr)
310 {
311         /*
312          * Call initialization code, and switch to the full blown HYP
313          * code. The init code doesn't need to preserve these
314          * registers as r0-r3 are already callee saved according to
315          * the AAPCS.
316          * Note that we slightly misuse the prototype by casting the
317          * stack pointer to a void *.
318
319          * The PGDs are always passed as the third argument, in order
320          * to be passed into r2-r3 to the init code (yes, this is
321          * compliant with the PCS!).
322          */
323
324         __kvm_call_hyp((void*)hyp_stack_ptr, vector_ptr, pgd_ptr);
325 }
326
327 static inline void __cpu_init_stage2(void)
328 {
329         kvm_call_hyp(__init_stage2_translation);
330 }
331
332 static inline int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext)
333 {
334         return 0;
335 }
336
337 int kvm_perf_init(void);
338 int kvm_perf_teardown(void);
339
340 void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
341
342 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
343
344 static inline bool kvm_arch_requires_vhe(void) { return false; }
345 static inline void kvm_arch_hardware_unsetup(void) {}
346 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
347 static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}
348 static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
349 static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
350
351 static inline void kvm_arm_init_debug(void) {}
352 static inline void kvm_arm_setup_debug(struct kvm_vcpu *vcpu) {}
353 static inline void kvm_arm_clear_debug(struct kvm_vcpu *vcpu) {}
354 static inline void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu) {}
355
356 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
357                                struct kvm_device_attr *attr);
358 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
359                                struct kvm_device_attr *attr);
360 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
361                                struct kvm_device_attr *attr);
362
363 /*
364  * VFP/NEON switching is all done by the hyp switch code, so no need to
365  * coordinate with host context handling for this state:
366  */
367 static inline void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu) {}
368 static inline void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu) {}
369 static inline void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) {}
370
371 static inline void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu) {}
372 static inline void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu) {}
373
374 static inline void kvm_arm_vhe_guest_enter(void) {}
375 static inline void kvm_arm_vhe_guest_exit(void) {}
376
377 static inline bool kvm_arm_harden_branch_predictor(void)
378 {
379         switch(read_cpuid_part()) {
380 #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
381         case ARM_CPU_PART_BRAHMA_B15:
382         case ARM_CPU_PART_CORTEX_A12:
383         case ARM_CPU_PART_CORTEX_A15:
384         case ARM_CPU_PART_CORTEX_A17:
385                 return true;
386 #endif
387         default:
388                 return false;
389         }
390 }
391
392 #define KVM_SSBD_UNKNOWN                -1
393 #define KVM_SSBD_FORCE_DISABLE          0
394 #define KVM_SSBD_KERNEL         1
395 #define KVM_SSBD_FORCE_ENABLE           2
396 #define KVM_SSBD_MITIGATED              3
397
398 static inline int kvm_arm_have_ssbd(void)
399 {
400         /* No way to detect it yet, pretend it is not there. */
401         return KVM_SSBD_UNKNOWN;
402 }
403
404 static inline void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu) {}
405 static inline void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu) {}
406
407 #define __KVM_HAVE_ARCH_VM_ALLOC
408 struct kvm *kvm_arch_alloc_vm(void);
409 void kvm_arch_free_vm(struct kvm *kvm);
410
411 static inline int kvm_arm_setup_stage2(struct kvm *kvm, unsigned long type)
412 {
413         /*
414          * On 32bit ARM, VMs get a static 40bit IPA stage2 setup,
415          * so any non-zero value used as type is illegal.
416          */
417         if (type)
418                 return -EINVAL;
419         return 0;
420 }
421
422 static inline int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
423 {
424         return -EINVAL;
425 }
426
427 static inline bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
428 {
429         return true;
430 }
431
432 #endif /* __ARM_KVM_HOST_H__ */