1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
6 #ifndef __ASM_LOONGARCH_KVM_HOST_H__
7 #define __ASM_LOONGARCH_KVM_HOST_H__
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>
20 #include <asm/kvm_mmu.h>
21 #include <asm/kvm_ipi.h>
22 #include <asm/kvm_eiointc.h>
23 #include <asm/kvm_pch_pic.h>
24 #include <asm/loongarch.h>
26 #define __KVM_HAVE_ARCH_INTC_INITIALIZED
28 /* Loongarch KVM register ids */
29 #define KVM_GET_IOC_CSR_IDX(id) ((id & KVM_CSR_IDX_MASK) >> LOONGARCH_REG_SHIFT)
30 #define KVM_GET_IOC_CPUCFG_IDX(id) ((id & KVM_CPUCFG_IDX_MASK) >> LOONGARCH_REG_SHIFT)
32 #define KVM_MAX_VCPUS 256
33 #define KVM_MAX_CPUCFG_REGS 21
35 #define KVM_HALT_POLL_NS_DEFAULT 500000
36 #define KVM_REQ_TLB_FLUSH_GPA KVM_ARCH_REQ(0)
37 #define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(1)
38 #define KVM_REQ_PMU KVM_ARCH_REQ(2)
40 #define KVM_GUESTDBG_SW_BP_MASK \
41 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)
42 #define KVM_GUESTDBG_VALID_MASK \
43 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP | KVM_GUESTDBG_SINGLESTEP)
45 #define KVM_DIRTY_LOG_MANUAL_CAPS \
46 (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | KVM_DIRTY_LOG_INITIALLY_SET)
49 struct kvm_vm_stat_generic generic
;
54 u64 eiointc_read_exits
;
55 u64 eiointc_write_exits
;
56 u64 pch_pic_read_exits
;
57 u64 pch_pic_write_exits
;
60 struct kvm_vcpu_stat
{
61 struct kvm_vcpu_stat_generic generic
;
69 #define KVM_MEM_HUGEPAGE_CAPABLE (1UL << 0)
70 #define KVM_MEM_HUGEPAGE_INCAPABLE (1UL << 1)
71 struct kvm_arch_memory_slot
{
75 #define HOST_MAX_PMNUM 16
77 unsigned long vpid_cache
;
78 struct kvm_vcpu
*last_vcpu
;
80 u64 perf_ctrl
[HOST_MAX_PMNUM
];
81 u64 perf_cntr
[HOST_MAX_PMNUM
];
84 struct kvm_world_switch
{
85 int (*exc_entry
)(void);
86 int (*enter_guest
)(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
);
87 unsigned long page_order
;
90 #define MAX_PGTABLE_LEVELS 4
93 * Physical CPUID is used for interrupt routing, there are different
94 * definitions about physical cpuid on different hardwares.
96 * For LOONGARCH_CSR_CPUID register, max CPUID size if 512
97 * For IPI hardware, max destination CPUID size 1024
98 * For eiointc interrupt controller, max destination CPUID size is 256
99 * For msgint interrupt controller, max supported CPUID size is 65536
101 * Currently max CPUID is defined as 256 for KVM hypervisor, in future
102 * it will be expanded to 4096, including 16 packages at most. And every
103 * package supports at most 256 vcpus
105 #define KVM_MAX_PHYID 256
107 struct kvm_phyid_info
{
108 struct kvm_vcpu
*vcpu
;
112 struct kvm_phyid_map
{
114 struct kvm_phyid_info phys_map
[KVM_MAX_PHYID
];
118 /* Guest physical mm */
120 unsigned long gpa_size
;
121 unsigned long invalid_ptes
[MAX_PGTABLE_LEVELS
];
122 unsigned int pte_shifts
[MAX_PGTABLE_LEVELS
];
123 unsigned int root_level
;
124 spinlock_t phyid_map_lock
;
125 struct kvm_phyid_map
*phyid_map
;
126 /* Enabled PV features */
127 unsigned long pv_features
;
130 struct kvm_context __percpu
*vmcs
;
131 struct loongarch_ipi
*ipi
;
132 struct loongarch_eiointc
*eiointc
;
133 struct loongarch_pch_pic
*pch_pic
;
136 #define CSR_MAX_NUMS 0x800
138 struct loongarch_csrs
{
139 unsigned long csrs
[CSR_MAX_NUMS
];
143 #define RESUME_HOST 0
144 #define RESUME_GUEST 1
146 enum emulation_result
{
147 EMULATE_DONE
, /* no further processing */
148 EMULATE_DO_MMIO
, /* kvm_run filled with MMIO request */
149 EMULATE_DO_IOCSR
, /* handle IOCSR request */
150 EMULATE_FAIL
, /* can't emulate this instruction */
151 EMULATE_EXCEPT
, /* A guest exception has been generated */
154 #define KVM_LARCH_FPU (0x1 << 0)
155 #define KVM_LARCH_LSX (0x1 << 1)
156 #define KVM_LARCH_LASX (0x1 << 2)
157 #define KVM_LARCH_LBT (0x1 << 3)
158 #define KVM_LARCH_PMU (0x1 << 4)
159 #define KVM_LARCH_SWCSR_LATEST (0x1 << 5)
160 #define KVM_LARCH_HWCSR_USABLE (0x1 << 6)
162 #define LOONGARCH_PV_FEAT_UPDATED BIT_ULL(63)
163 #define LOONGARCH_PV_FEAT_MASK (BIT(KVM_FEATURE_IPI) | \
164 BIT(KVM_FEATURE_STEAL_TIME) | \
165 BIT(KVM_FEATURE_VIRT_EXTIOI))
167 struct kvm_vcpu_arch
{
169 * Switch pointer-to-function type to unsigned long
170 * for loading the value into register directly.
172 unsigned long host_eentry
;
173 unsigned long guest_eentry
;
175 /* Pointers stored here for easy accessing from assembly code */
176 int (*handle_exit
)(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
);
178 /* Host registers preserved across guest mode execution */
179 unsigned long host_sp
;
180 unsigned long host_tp
;
181 unsigned long host_pgd
;
183 /* Host CSRs are used when handling exits from guest */
186 unsigned long host_ecfg
;
187 unsigned long host_estat
;
188 unsigned long host_percpu
;
191 unsigned long gprs
[32];
194 /* Which auxiliary state is loaded (KVM_LARCH_*) */
195 unsigned int aux_inuse
;
198 struct loongarch_fpu fpu FPU_ALIGN
;
199 struct loongarch_lbt lbt
;
202 struct loongarch_csrs
*csr
;
204 /* Guest max PMU CSR id */
207 /* GPR used as IO source/target */
210 /* KVM register to control count timer */
212 struct hrtimer swtimer
;
214 /* Bitmask of intr that are pending */
215 unsigned long irq_pending
;
216 /* Bitmask of pending intr to be cleared */
217 unsigned long irq_clear
;
219 /* Bitmask of exceptions that are pending */
220 unsigned long exception_pending
;
221 unsigned int esubcode
;
223 /* Cache for pages needed inside spinlock regions */
224 struct kvm_mmu_memory_cache mmu_page_cache
;
230 /* Frequency of stable timer in Hz */
234 /* Last CPU the vCPU state was loaded on */
237 struct kvm_mp_state mp_state
;
239 struct ipi_state ipi_state
;
241 u32 cpucfg
[KVM_MAX_CPUCFG_REGS
];
243 /* paravirt steal time */
247 struct gfn_to_hva_cache cache
;
251 static inline unsigned long readl_sw_gcsr(struct loongarch_csrs
*csr
, int reg
)
253 return csr
->csrs
[reg
];
256 static inline void writel_sw_gcsr(struct loongarch_csrs
*csr
, int reg
, unsigned long val
)
258 csr
->csrs
[reg
] = val
;
261 static inline bool kvm_guest_has_fpu(struct kvm_vcpu_arch
*arch
)
263 return arch
->cpucfg
[2] & CPUCFG2_FP
;
266 static inline bool kvm_guest_has_lsx(struct kvm_vcpu_arch
*arch
)
268 return arch
->cpucfg
[2] & CPUCFG2_LSX
;
271 static inline bool kvm_guest_has_lasx(struct kvm_vcpu_arch
*arch
)
273 return arch
->cpucfg
[2] & CPUCFG2_LASX
;
276 static inline bool kvm_guest_has_lbt(struct kvm_vcpu_arch
*arch
)
278 return arch
->cpucfg
[2] & (CPUCFG2_X86BT
| CPUCFG2_ARMBT
| CPUCFG2_MIPSBT
);
281 static inline bool kvm_guest_has_pmu(struct kvm_vcpu_arch
*arch
)
283 return arch
->cpucfg
[6] & CPUCFG6_PMP
;
286 static inline int kvm_get_pmu_num(struct kvm_vcpu_arch
*arch
)
288 return (arch
->cpucfg
[6] & CPUCFG6_PMNUM
) >> CPUCFG6_PMNUM_SHIFT
;
291 /* Debug: dump vcpu state */
292 int kvm_arch_vcpu_dump_regs(struct kvm_vcpu
*vcpu
);
295 void kvm_flush_tlb_all(void);
296 void kvm_flush_tlb_gpa(struct kvm_vcpu
*vcpu
, unsigned long gpa
);
297 int kvm_handle_mm_fault(struct kvm_vcpu
*vcpu
, unsigned long badv
, bool write
);
299 int kvm_unmap_hva_range(struct kvm
*kvm
, unsigned long start
, unsigned long end
, bool blockable
);
300 int kvm_age_hva(struct kvm
*kvm
, unsigned long start
, unsigned long end
);
301 int kvm_test_age_hva(struct kvm
*kvm
, unsigned long hva
);
303 static inline void update_pc(struct kvm_vcpu_arch
*arch
)
309 * kvm_is_ifetch_fault() - Find whether a TLBL exception is due to ifetch fault.
310 * @vcpu: Virtual CPU.
312 * Returns: Whether the TLBL exception was likely due to an instruction
313 * fetch fault rather than a data load fault.
315 static inline bool kvm_is_ifetch_fault(struct kvm_vcpu_arch
*arch
)
317 return arch
->pc
== arch
->badv
;
321 static inline void kvm_arch_hardware_unsetup(void) {}
322 static inline void kvm_arch_sync_events(struct kvm
*kvm
) {}
323 static inline void kvm_arch_memslots_updated(struct kvm
*kvm
, u64 gen
) {}
324 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu
*vcpu
) {}
325 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu
*vcpu
) {}
326 static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu
*vcpu
) {}
327 static inline void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
) {}
328 void kvm_check_vpid(struct kvm_vcpu
*vcpu
);
329 enum hrtimer_restart
kvm_swtimer_wakeup(struct hrtimer
*timer
);
330 void kvm_arch_flush_remote_tlbs_memslot(struct kvm
*kvm
, const struct kvm_memory_slot
*memslot
);
331 void kvm_init_vmcs(struct kvm
*kvm
);
332 void kvm_exc_entry(void);
333 int kvm_enter_guest(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
);
335 extern unsigned long vpid_mask
;
336 extern const unsigned long kvm_exception_size
;
337 extern const unsigned long kvm_enter_guest_size
;
338 extern struct kvm_world_switch
*kvm_loongarch_ops
;
340 #define SW_GCSR (1 << 0)
341 #define HW_GCSR (1 << 1)
342 #define INVALID_GCSR (1 << 2)
344 int get_gcsr_flag(int csr
);
345 void set_hw_gcsr(int csr_id
, unsigned long val
);
347 #endif /* __ASM_LOONGARCH_KVM_HOST_H__ */