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