xref: /linux/arch/loongarch/include/asm/kvm_host.h (revision c2a96b7f187fb6a455836d4a6e113947ff11de97)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
4  */
5 
6 #ifndef __ASM_LOONGARCH_KVM_HOST_H__
7 #define __ASM_LOONGARCH_KVM_HOST_H__
8 
9 #include <linux/cpumask.h>
10 #include <linux/hrtimer.h>
11 #include <linux/interrupt.h>
12 #include <linux/kvm.h>
13 #include <linux/kvm_types.h>
14 #include <linux/mutex.h>
15 #include <linux/spinlock.h>
16 #include <linux/threads.h>
17 #include <linux/types.h>
18 
19 #include <asm/inst.h>
20 #include <asm/kvm_mmu.h>
21 #include <asm/loongarch.h>
22 
23 /* Loongarch KVM register ids */
24 #define KVM_GET_IOC_CSR_IDX(id)		((id & KVM_CSR_IDX_MASK) >> LOONGARCH_REG_SHIFT)
25 #define KVM_GET_IOC_CPUCFG_IDX(id)	((id & KVM_CPUCFG_IDX_MASK) >> LOONGARCH_REG_SHIFT)
26 
27 #define KVM_MAX_VCPUS			256
28 #define KVM_MAX_CPUCFG_REGS		21
29 /* memory slots that does not exposed to userspace */
30 #define KVM_PRIVATE_MEM_SLOTS		0
31 
32 #define KVM_HALT_POLL_NS_DEFAULT	500000
33 #define KVM_REQ_TLB_FLUSH_GPA		KVM_ARCH_REQ(0)
34 #define KVM_REQ_STEAL_UPDATE		KVM_ARCH_REQ(1)
35 
36 #define KVM_GUESTDBG_SW_BP_MASK		\
37 	(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)
38 #define KVM_GUESTDBG_VALID_MASK		\
39 	(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP | KVM_GUESTDBG_SINGLESTEP)
40 
41 #define KVM_DIRTY_LOG_MANUAL_CAPS	\
42 	(KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | KVM_DIRTY_LOG_INITIALLY_SET)
43 
44 struct kvm_vm_stat {
45 	struct kvm_vm_stat_generic generic;
46 	u64 pages;
47 	u64 hugepages;
48 };
49 
50 struct kvm_vcpu_stat {
51 	struct kvm_vcpu_stat_generic generic;
52 	u64 int_exits;
53 	u64 idle_exits;
54 	u64 cpucfg_exits;
55 	u64 signal_exits;
56 	u64 hypercall_exits;
57 };
58 
59 #define KVM_MEM_HUGEPAGE_CAPABLE	(1UL << 0)
60 #define KVM_MEM_HUGEPAGE_INCAPABLE	(1UL << 1)
61 struct kvm_arch_memory_slot {
62 	unsigned long flags;
63 };
64 
65 struct kvm_context {
66 	unsigned long vpid_cache;
67 	struct kvm_vcpu *last_vcpu;
68 };
69 
70 struct kvm_world_switch {
71 	int (*exc_entry)(void);
72 	int (*enter_guest)(struct kvm_run *run, struct kvm_vcpu *vcpu);
73 	unsigned long page_order;
74 };
75 
76 #define MAX_PGTABLE_LEVELS	4
77 
78 /*
79  * Physical CPUID is used for interrupt routing, there are different
80  * definitions about physical cpuid on different hardwares.
81  *
82  *  For LOONGARCH_CSR_CPUID register, max CPUID size if 512
83  *  For IPI hardware, max destination CPUID size 1024
84  *  For extioi interrupt controller, max destination CPUID size is 256
85  *  For msgint interrupt controller, max supported CPUID size is 65536
86  *
87  * Currently max CPUID is defined as 256 for KVM hypervisor, in future
88  * it will be expanded to 4096, including 16 packages at most. And every
89  * package supports at most 256 vcpus
90  */
91 #define KVM_MAX_PHYID		256
92 
93 struct kvm_phyid_info {
94 	struct kvm_vcpu	*vcpu;
95 	bool		enabled;
96 };
97 
98 struct kvm_phyid_map {
99 	int max_phyid;
100 	struct kvm_phyid_info phys_map[KVM_MAX_PHYID];
101 };
102 
103 struct kvm_arch {
104 	/* Guest physical mm */
105 	kvm_pte_t *pgd;
106 	unsigned long gpa_size;
107 	unsigned long invalid_ptes[MAX_PGTABLE_LEVELS];
108 	unsigned int  pte_shifts[MAX_PGTABLE_LEVELS];
109 	unsigned int  root_level;
110 	spinlock_t    phyid_map_lock;
111 	struct kvm_phyid_map  *phyid_map;
112 
113 	s64 time_offset;
114 	struct kvm_context __percpu *vmcs;
115 };
116 
117 #define CSR_MAX_NUMS		0x800
118 
119 struct loongarch_csrs {
120 	unsigned long csrs[CSR_MAX_NUMS];
121 };
122 
123 /* Resume Flags */
124 #define RESUME_HOST		0
125 #define RESUME_GUEST		1
126 
127 enum emulation_result {
128 	EMULATE_DONE,		/* no further processing */
129 	EMULATE_DO_MMIO,	/* kvm_run filled with MMIO request */
130 	EMULATE_DO_IOCSR,	/* handle IOCSR request */
131 	EMULATE_FAIL,		/* can't emulate this instruction */
132 	EMULATE_EXCEPT,		/* A guest exception has been generated */
133 };
134 
135 #define KVM_LARCH_FPU		(0x1 << 0)
136 #define KVM_LARCH_LSX		(0x1 << 1)
137 #define KVM_LARCH_LASX		(0x1 << 2)
138 #define KVM_LARCH_SWCSR_LATEST	(0x1 << 3)
139 #define KVM_LARCH_HWCSR_USABLE	(0x1 << 4)
140 
141 struct kvm_vcpu_arch {
142 	/*
143 	 * Switch pointer-to-function type to unsigned long
144 	 * for loading the value into register directly.
145 	 */
146 	unsigned long host_eentry;
147 	unsigned long guest_eentry;
148 
149 	/* Pointers stored here for easy accessing from assembly code */
150 	int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
151 
152 	/* Host registers preserved across guest mode execution */
153 	unsigned long host_sp;
154 	unsigned long host_tp;
155 	unsigned long host_pgd;
156 
157 	/* Host CSRs are used when handling exits from guest */
158 	unsigned long badi;
159 	unsigned long badv;
160 	unsigned long host_ecfg;
161 	unsigned long host_estat;
162 	unsigned long host_percpu;
163 
164 	/* GPRs */
165 	unsigned long gprs[32];
166 	unsigned long pc;
167 
168 	/* Which auxiliary state is loaded (KVM_LARCH_*) */
169 	unsigned int aux_inuse;
170 
171 	/* FPU state */
172 	struct loongarch_fpu fpu FPU_ALIGN;
173 
174 	/* CSR state */
175 	struct loongarch_csrs *csr;
176 
177 	/* GPR used as IO source/target */
178 	u32 io_gpr;
179 
180 	/* KVM register to control count timer */
181 	u32 count_ctl;
182 	struct hrtimer swtimer;
183 
184 	/* Bitmask of intr that are pending */
185 	unsigned long irq_pending;
186 	/* Bitmask of pending intr to be cleared */
187 	unsigned long irq_clear;
188 
189 	/* Bitmask of exceptions that are pending */
190 	unsigned long exception_pending;
191 	unsigned int  esubcode;
192 
193 	/* Cache for pages needed inside spinlock regions */
194 	struct kvm_mmu_memory_cache mmu_page_cache;
195 
196 	/* vcpu's vpid */
197 	u64 vpid;
198 	gpa_t flush_gpa;
199 
200 	/* Frequency of stable timer in Hz */
201 	u64 timer_mhz;
202 	ktime_t expire;
203 
204 	/* Last CPU the vCPU state was loaded on */
205 	int last_sched_cpu;
206 	/* mp state */
207 	struct kvm_mp_state mp_state;
208 	/* cpucfg */
209 	u32 cpucfg[KVM_MAX_CPUCFG_REGS];
210 
211 	/* paravirt steal time */
212 	struct {
213 		u64 guest_addr;
214 		u64 last_steal;
215 		struct gfn_to_hva_cache cache;
216 	} st;
217 };
218 
219 static inline unsigned long readl_sw_gcsr(struct loongarch_csrs *csr, int reg)
220 {
221 	return csr->csrs[reg];
222 }
223 
224 static inline void writel_sw_gcsr(struct loongarch_csrs *csr, int reg, unsigned long val)
225 {
226 	csr->csrs[reg] = val;
227 }
228 
229 static inline bool kvm_guest_has_fpu(struct kvm_vcpu_arch *arch)
230 {
231 	return arch->cpucfg[2] & CPUCFG2_FP;
232 }
233 
234 static inline bool kvm_guest_has_lsx(struct kvm_vcpu_arch *arch)
235 {
236 	return arch->cpucfg[2] & CPUCFG2_LSX;
237 }
238 
239 static inline bool kvm_guest_has_lasx(struct kvm_vcpu_arch *arch)
240 {
241 	return arch->cpucfg[2] & CPUCFG2_LASX;
242 }
243 
244 /* Debug: dump vcpu state */
245 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu);
246 
247 /* MMU handling */
248 void kvm_flush_tlb_all(void);
249 void kvm_flush_tlb_gpa(struct kvm_vcpu *vcpu, unsigned long gpa);
250 int kvm_handle_mm_fault(struct kvm_vcpu *vcpu, unsigned long badv, bool write);
251 
252 int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end, bool blockable);
253 int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
254 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
255 
256 static inline void update_pc(struct kvm_vcpu_arch *arch)
257 {
258 	arch->pc += 4;
259 }
260 
261 /*
262  * kvm_is_ifetch_fault() - Find whether a TLBL exception is due to ifetch fault.
263  * @vcpu:	Virtual CPU.
264  *
265  * Returns:	Whether the TLBL exception was likely due to an instruction
266  *		fetch fault rather than a data load fault.
267  */
268 static inline bool kvm_is_ifetch_fault(struct kvm_vcpu_arch *arch)
269 {
270 	return arch->pc == arch->badv;
271 }
272 
273 /* Misc */
274 static inline void kvm_arch_hardware_unsetup(void) {}
275 static inline void kvm_arch_sync_events(struct kvm *kvm) {}
276 static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
277 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
278 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
279 static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
280 static inline void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) {}
281 void kvm_check_vpid(struct kvm_vcpu *vcpu);
282 enum hrtimer_restart kvm_swtimer_wakeup(struct hrtimer *timer);
283 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, const struct kvm_memory_slot *memslot);
284 void kvm_init_vmcs(struct kvm *kvm);
285 void kvm_exc_entry(void);
286 int  kvm_enter_guest(struct kvm_run *run, struct kvm_vcpu *vcpu);
287 
288 extern unsigned long vpid_mask;
289 extern const unsigned long kvm_exception_size;
290 extern const unsigned long kvm_enter_guest_size;
291 extern struct kvm_world_switch *kvm_loongarch_ops;
292 
293 #define SW_GCSR		(1 << 0)
294 #define HW_GCSR		(1 << 1)
295 #define INVALID_GCSR	(1 << 2)
296 
297 int get_gcsr_flag(int csr);
298 void set_hw_gcsr(int csr_id, unsigned long val);
299 
300 #endif /* __ASM_LOONGARCH_KVM_HOST_H__ */
301