1740765ceSSanjay Lal /*
2740765ceSSanjay Lal * This file is subject to the terms and conditions of the GNU General Public
3740765ceSSanjay Lal * License. See the file "COPYING" in the main directory of this archive
4740765ceSSanjay Lal * for more details.
5740765ceSSanjay Lal *
6740765ceSSanjay Lal * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
7740765ceSSanjay Lal * Authors: Sanjay Lal <sanjayl@kymasys.com>
8740765ceSSanjay Lal */
9740765ceSSanjay Lal
10740765ceSSanjay Lal #ifndef __MIPS_KVM_HOST_H__
11740765ceSSanjay Lal #define __MIPS_KVM_HOST_H__
12740765ceSSanjay Lal
13c992a4f6SJames Hogan #include <linux/cpumask.h>
14740765ceSSanjay Lal #include <linux/mutex.h>
15740765ceSSanjay Lal #include <linux/hrtimer.h>
16740765ceSSanjay Lal #include <linux/interrupt.h>
17740765ceSSanjay Lal #include <linux/types.h>
18740765ceSSanjay Lal #include <linux/kvm.h>
19740765ceSSanjay Lal #include <linux/kvm_types.h>
20740765ceSSanjay Lal #include <linux/threads.h>
21740765ceSSanjay Lal #include <linux/spinlock.h>
22740765ceSSanjay Lal
23f0b7ddbdSHuang Pei #include <asm/asm.h>
24258f3a2eSJames Hogan #include <asm/inst.h>
25e6207bbeSJames Hogan #include <asm/mipsregs.h>
26e6207bbeSJames Hogan
27f21db309SHuacai Chen #include <kvm/iodev.h>
28f21db309SHuacai Chen
2948a3c4e4SJames Hogan /* MIPS KVM register ids */
3048a3c4e4SJames Hogan #define MIPS_CP0_32(_R, _S) \
317bd4acecSJames Hogan (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
3248a3c4e4SJames Hogan
3348a3c4e4SJames Hogan #define MIPS_CP0_64(_R, _S) \
347bd4acecSJames Hogan (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
3548a3c4e4SJames Hogan
3648a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
3748a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_ENTRYLO0 MIPS_CP0_64(2, 0)
3848a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_ENTRYLO1 MIPS_CP0_64(3, 0)
3948a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
40dffe042fSJames Hogan #define KVM_REG_MIPS_CP0_CONTEXTCONFIG MIPS_CP0_32(4, 1)
4148a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
42dffe042fSJames Hogan #define KVM_REG_MIPS_CP0_XCONTEXTCONFIG MIPS_CP0_64(4, 3)
4348a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
4448a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1)
454b7de028SJames Hogan #define KVM_REG_MIPS_CP0_SEGCTL0 MIPS_CP0_64(5, 2)
464b7de028SJames Hogan #define KVM_REG_MIPS_CP0_SEGCTL1 MIPS_CP0_64(5, 3)
474b7de028SJames Hogan #define KVM_REG_MIPS_CP0_SEGCTL2 MIPS_CP0_64(5, 4)
485a2f352fSJames Hogan #define KVM_REG_MIPS_CP0_PWBASE MIPS_CP0_64(5, 5)
495a2f352fSJames Hogan #define KVM_REG_MIPS_CP0_PWFIELD MIPS_CP0_64(5, 6)
505a2f352fSJames Hogan #define KVM_REG_MIPS_CP0_PWSIZE MIPS_CP0_64(5, 7)
5148a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
525a2f352fSJames Hogan #define KVM_REG_MIPS_CP0_PWCTL MIPS_CP0_32(6, 6)
5348a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
5448a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
55edc89260SJames Hogan #define KVM_REG_MIPS_CP0_BADINSTR MIPS_CP0_32(8, 1)
56edc89260SJames Hogan #define KVM_REG_MIPS_CP0_BADINSTRP MIPS_CP0_32(8, 2)
5748a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
5848a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
5948a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
6048a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
61ad58d4d4SJames Hogan #define KVM_REG_MIPS_CP0_INTCTL MIPS_CP0_32(12, 1)
6248a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
6348a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
641068eaafSJames Hogan #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0)
6548a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_EBASE MIPS_CP0_64(15, 1)
6648a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
6748a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
6848a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
6948a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
70c771607aSJames Hogan #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
71c771607aSJames Hogan #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
728a5097eeSHuacai Chen #define KVM_REG_MIPS_CP0_CONFIG6 MIPS_CP0_32(16, 6)
7348a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_CONFIG7 MIPS_CP0_32(16, 7)
74d42a008fSJames Hogan #define KVM_REG_MIPS_CP0_MAARI MIPS_CP0_64(17, 2)
7548a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0)
768a5097eeSHuacai Chen #define KVM_REG_MIPS_CP0_DIAG MIPS_CP0_32(22, 0)
7748a3c4e4SJames Hogan #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
7805108709SJames Hogan #define KVM_REG_MIPS_CP0_KSCRATCH1 MIPS_CP0_64(31, 2)
7905108709SJames Hogan #define KVM_REG_MIPS_CP0_KSCRATCH2 MIPS_CP0_64(31, 3)
8005108709SJames Hogan #define KVM_REG_MIPS_CP0_KSCRATCH3 MIPS_CP0_64(31, 4)
8105108709SJames Hogan #define KVM_REG_MIPS_CP0_KSCRATCH4 MIPS_CP0_64(31, 5)
8205108709SJames Hogan #define KVM_REG_MIPS_CP0_KSCRATCH5 MIPS_CP0_64(31, 6)
8305108709SJames Hogan #define KVM_REG_MIPS_CP0_KSCRATCH6 MIPS_CP0_64(31, 7)
8448a3c4e4SJames Hogan
85740765ceSSanjay Lal
86210b4b91SHuacai Chen #define KVM_MAX_VCPUS 16
87740765ceSSanjay Lal
88920552b2SDavid Hildenbrand #define KVM_HALT_POLL_NS_DEFAULT 500000
89740765ceSSanjay Lal
90c992a4f6SJames Hogan extern unsigned long GUESTID_MASK;
91c992a4f6SJames Hogan extern unsigned long GUESTID_FIRST_VERSION;
92c992a4f6SJames Hogan extern unsigned long GUESTID_VERSION_MASK;
93740765ceSSanjay Lal
94740765ceSSanjay Lal #define KVM_INVALID_ADDR 0xdeadbeef
95740765ceSSanjay Lal
96f6f70171SJames Hogan /*
97f6f70171SJames Hogan * EVA has overlapping user & kernel address spaces, so user VAs may be >
98f6f70171SJames Hogan * PAGE_OFFSET. For this reason we can't use the default KVM_HVA_ERR_BAD of
99f6f70171SJames Hogan * PAGE_OFFSET.
100f6f70171SJames Hogan */
101f6f70171SJames Hogan
102f6f70171SJames Hogan #define KVM_HVA_ERR_BAD (-1UL)
103f6f70171SJames Hogan #define KVM_HVA_ERR_RO_BAD (-2UL)
104f6f70171SJames Hogan
kvm_is_error_hva(unsigned long addr)105f6f70171SJames Hogan static inline bool kvm_is_error_hva(unsigned long addr)
106f6f70171SJames Hogan {
107f6f70171SJames Hogan return IS_ERR_VALUE(addr);
108f6f70171SJames Hogan }
109f6f70171SJames Hogan
110740765ceSSanjay Lal struct kvm_vm_stat {
1110193cc90SJing Zhang struct kvm_vm_stat_generic generic;
112740765ceSSanjay Lal };
113740765ceSSanjay Lal
114740765ceSSanjay Lal struct kvm_vcpu_stat {
1150193cc90SJing Zhang struct kvm_vcpu_stat_generic generic;
1168a7e75d4SSuraj Jitindar Singh u64 wait_exits;
1178a7e75d4SSuraj Jitindar Singh u64 cache_exits;
1188a7e75d4SSuraj Jitindar Singh u64 signal_exits;
1198a7e75d4SSuraj Jitindar Singh u64 int_exits;
1208a7e75d4SSuraj Jitindar Singh u64 cop_unusable_exits;
1218a7e75d4SSuraj Jitindar Singh u64 tlbmod_exits;
1228a7e75d4SSuraj Jitindar Singh u64 tlbmiss_ld_exits;
1238a7e75d4SSuraj Jitindar Singh u64 tlbmiss_st_exits;
1248a7e75d4SSuraj Jitindar Singh u64 addrerr_st_exits;
1258a7e75d4SSuraj Jitindar Singh u64 addrerr_ld_exits;
1268a7e75d4SSuraj Jitindar Singh u64 syscall_exits;
1278a7e75d4SSuraj Jitindar Singh u64 resvd_inst_exits;
1288a7e75d4SSuraj Jitindar Singh u64 break_inst_exits;
1298a7e75d4SSuraj Jitindar Singh u64 trap_inst_exits;
1308a7e75d4SSuraj Jitindar Singh u64 msa_fpe_exits;
1318a7e75d4SSuraj Jitindar Singh u64 fpe_exits;
1328a7e75d4SSuraj Jitindar Singh u64 msa_disabled_exits;
1338a7e75d4SSuraj Jitindar Singh u64 flush_dcache_exits;
134a7244920SJames Hogan u64 vz_gpsi_exits;
135a7244920SJames Hogan u64 vz_gsfc_exits;
136a7244920SJames Hogan u64 vz_hc_exits;
137a7244920SJames Hogan u64 vz_grr_exits;
138a7244920SJames Hogan u64 vz_gva_exits;
139a7244920SJames Hogan u64 vz_ghfc_exits;
140a7244920SJames Hogan u64 vz_gpa_exits;
141a7244920SJames Hogan u64 vz_resvd_exits;
1427f2a83f1SHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
1437f2a83f1SHuacai Chen u64 vz_cpucfg_exits;
1447f2a83f1SHuacai Chen #endif
145740765ceSSanjay Lal };
146740765ceSSanjay Lal
147740765ceSSanjay Lal struct kvm_arch_memory_slot {
148740765ceSSanjay Lal };
149740765ceSSanjay Lal
150f21db309SHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
151f21db309SHuacai Chen struct ipi_state {
152f21db309SHuacai Chen uint32_t status;
153f21db309SHuacai Chen uint32_t en;
154f21db309SHuacai Chen uint32_t set;
155f21db309SHuacai Chen uint32_t clear;
156f21db309SHuacai Chen uint64_t buf[4];
157f21db309SHuacai Chen };
158f21db309SHuacai Chen
159f21db309SHuacai Chen struct loongson_kvm_ipi;
160f21db309SHuacai Chen
161f21db309SHuacai Chen struct ipi_io_device {
162f21db309SHuacai Chen int node_id;
163f21db309SHuacai Chen struct loongson_kvm_ipi *ipi;
164f21db309SHuacai Chen struct kvm_io_device device;
165f21db309SHuacai Chen };
166f21db309SHuacai Chen
167f21db309SHuacai Chen struct loongson_kvm_ipi {
168f21db309SHuacai Chen spinlock_t lock;
169f21db309SHuacai Chen struct kvm *kvm;
170f21db309SHuacai Chen struct ipi_state ipistate[16];
171f21db309SHuacai Chen struct ipi_io_device dev_ipi[4];
172f21db309SHuacai Chen };
173f21db309SHuacai Chen #endif
174f21db309SHuacai Chen
175740765ceSSanjay Lal struct kvm_arch {
17606c158c9SJames Hogan /* Guest physical mm */
17706c158c9SJames Hogan struct mm_struct gpa_mm;
178c992a4f6SJames Hogan /* Mask of CPUs needing GPA ASID flush */
179c992a4f6SJames Hogan cpumask_t asid_flush_mask;
180f21db309SHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
181f21db309SHuacai Chen struct loongson_kvm_ipi ipi;
182f21db309SHuacai Chen #endif
183740765ceSSanjay Lal };
184740765ceSSanjay Lal
185740765ceSSanjay Lal #define N_MIPS_COPROC_REGS 32
186740765ceSSanjay Lal #define N_MIPS_COPROC_SEL 8
187740765ceSSanjay Lal
188740765ceSSanjay Lal struct mips_coproc {
189740765ceSSanjay Lal unsigned long reg[N_MIPS_COPROC_REGS][N_MIPS_COPROC_SEL];
190740765ceSSanjay Lal #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
191740765ceSSanjay Lal unsigned long stat[N_MIPS_COPROC_REGS][N_MIPS_COPROC_SEL];
192740765ceSSanjay Lal #endif
193740765ceSSanjay Lal };
194740765ceSSanjay Lal
195740765ceSSanjay Lal /*
196740765ceSSanjay Lal * Coprocessor 0 register names
197740765ceSSanjay Lal */
198740765ceSSanjay Lal #define MIPS_CP0_TLB_INDEX 0
199740765ceSSanjay Lal #define MIPS_CP0_TLB_RANDOM 1
200740765ceSSanjay Lal #define MIPS_CP0_TLB_LOW 2
201740765ceSSanjay Lal #define MIPS_CP0_TLB_LO0 2
202740765ceSSanjay Lal #define MIPS_CP0_TLB_LO1 3
203740765ceSSanjay Lal #define MIPS_CP0_TLB_CONTEXT 4
204740765ceSSanjay Lal #define MIPS_CP0_TLB_PG_MASK 5
205740765ceSSanjay Lal #define MIPS_CP0_TLB_WIRED 6
206740765ceSSanjay Lal #define MIPS_CP0_HWRENA 7
207740765ceSSanjay Lal #define MIPS_CP0_BAD_VADDR 8
208740765ceSSanjay Lal #define MIPS_CP0_COUNT 9
209740765ceSSanjay Lal #define MIPS_CP0_TLB_HI 10
210740765ceSSanjay Lal #define MIPS_CP0_COMPARE 11
211740765ceSSanjay Lal #define MIPS_CP0_STATUS 12
212740765ceSSanjay Lal #define MIPS_CP0_CAUSE 13
213740765ceSSanjay Lal #define MIPS_CP0_EXC_PC 14
214740765ceSSanjay Lal #define MIPS_CP0_PRID 15
215740765ceSSanjay Lal #define MIPS_CP0_CONFIG 16
216740765ceSSanjay Lal #define MIPS_CP0_LLADDR 17
217740765ceSSanjay Lal #define MIPS_CP0_WATCH_LO 18
218740765ceSSanjay Lal #define MIPS_CP0_WATCH_HI 19
219740765ceSSanjay Lal #define MIPS_CP0_TLB_XCONTEXT 20
2208a5097eeSHuacai Chen #define MIPS_CP0_DIAG 22
221740765ceSSanjay Lal #define MIPS_CP0_ECC 26
222740765ceSSanjay Lal #define MIPS_CP0_CACHE_ERR 27
223740765ceSSanjay Lal #define MIPS_CP0_TAG_LO 28
224740765ceSSanjay Lal #define MIPS_CP0_TAG_HI 29
225740765ceSSanjay Lal #define MIPS_CP0_ERROR_PC 30
226740765ceSSanjay Lal #define MIPS_CP0_DEBUG 23
227740765ceSSanjay Lal #define MIPS_CP0_DEPC 24
228740765ceSSanjay Lal #define MIPS_CP0_PERFCNT 25
229740765ceSSanjay Lal #define MIPS_CP0_ERRCTL 26
230740765ceSSanjay Lal #define MIPS_CP0_DATA_LO 28
231740765ceSSanjay Lal #define MIPS_CP0_DATA_HI 29
232740765ceSSanjay Lal #define MIPS_CP0_DESAVE 31
233740765ceSSanjay Lal
234740765ceSSanjay Lal #define MIPS_CP0_CONFIG_SEL 0
235740765ceSSanjay Lal #define MIPS_CP0_CONFIG1_SEL 1
236740765ceSSanjay Lal #define MIPS_CP0_CONFIG2_SEL 2
237740765ceSSanjay Lal #define MIPS_CP0_CONFIG3_SEL 3
238c771607aSJames Hogan #define MIPS_CP0_CONFIG4_SEL 4
239c771607aSJames Hogan #define MIPS_CP0_CONFIG5_SEL 5
240740765ceSSanjay Lal
241c992a4f6SJames Hogan #define MIPS_CP0_GUESTCTL2 10
242c992a4f6SJames Hogan #define MIPS_CP0_GUESTCTL2_SEL 5
243c992a4f6SJames Hogan #define MIPS_CP0_GTOFFSET 12
244c992a4f6SJames Hogan #define MIPS_CP0_GTOFFSET_SEL 7
245c992a4f6SJames Hogan
246740765ceSSanjay Lal /* Resume Flags */
247740765ceSSanjay Lal #define RESUME_FLAG_DR (1<<0) /* Reload guest nonvolatile state? */
248740765ceSSanjay Lal #define RESUME_FLAG_HOST (1<<1) /* Resume host? */
249740765ceSSanjay Lal
250740765ceSSanjay Lal #define RESUME_GUEST 0
251740765ceSSanjay Lal #define RESUME_GUEST_DR RESUME_FLAG_DR
252740765ceSSanjay Lal #define RESUME_HOST RESUME_FLAG_HOST
253740765ceSSanjay Lal
254740765ceSSanjay Lal enum emulation_result {
255740765ceSSanjay Lal EMULATE_DONE, /* no further processing */
256740765ceSSanjay Lal EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
257740765ceSSanjay Lal EMULATE_FAIL, /* can't emulate this instruction */
258740765ceSSanjay Lal EMULATE_WAIT, /* WAIT instruction */
259740765ceSSanjay Lal EMULATE_PRIV_FAIL,
2604cf74c9cSJames Hogan EMULATE_EXCEPT, /* A guest exception has been generated */
261955d8dc3SJames Hogan EMULATE_HYPERCALL, /* HYPCALL instruction */
262740765ceSSanjay Lal };
263740765ceSSanjay Lal
2645816c76dSXing Li #if defined(CONFIG_64BIT)
2655816c76dSXing Li #define VPN2_MASK GENMASK(cpu_vmbits - 1, 13)
2665816c76dSXing Li #else
267740765ceSSanjay Lal #define VPN2_MASK 0xffffe000
2685816c76dSXing Li #endif
269fe2b73dbSXing Li #define KVM_ENTRYHI_ASID cpu_asid_mask(&boot_cpu_data)
270e6207bbeSJames Hogan #define TLB_IS_GLOBAL(x) ((x).tlb_lo[0] & (x).tlb_lo[1] & ENTRYLO_G)
271740765ceSSanjay Lal #define TLB_VPN2(x) ((x).tlb_hi & VPN2_MASK)
272ca64c2beSPaul Burton #define TLB_ASID(x) ((x).tlb_hi & KVM_ENTRYHI_ASID)
27319d194c6SJames Hogan #define TLB_LO_IDX(x, va) (((va) >> PAGE_SHIFT) & 1)
274e6207bbeSJames Hogan #define TLB_IS_VALID(x, va) ((x).tlb_lo[TLB_LO_IDX(x, va)] & ENTRYLO_V)
2751880afd6SJames Hogan #define TLB_IS_DIRTY(x, va) ((x).tlb_lo[TLB_LO_IDX(x, va)] & ENTRYLO_D)
276d116e812SDeng-Cheng Zhu #define TLB_HI_VPN2_HIT(x, y) ((TLB_VPN2(x) & ~(x).tlb_mask) == \
277d116e812SDeng-Cheng Zhu ((y) & VPN2_MASK & ~(x).tlb_mask))
278d116e812SDeng-Cheng Zhu #define TLB_HI_ASID_HIT(x, y) (TLB_IS_GLOBAL(x) || \
279ca64c2beSPaul Burton TLB_ASID(x) == ((y) & KVM_ENTRYHI_ASID))
280740765ceSSanjay Lal
281740765ceSSanjay Lal struct kvm_mips_tlb {
282740765ceSSanjay Lal long tlb_mask;
283740765ceSSanjay Lal long tlb_hi;
2849fbfb06aSJames Hogan long tlb_lo[2];
285740765ceSSanjay Lal };
286740765ceSSanjay Lal
287f943176aSJames Hogan #define KVM_MIPS_AUX_FPU 0x1
288f943176aSJames Hogan #define KVM_MIPS_AUX_MSA 0x2
28998e91b84SJames Hogan
290740765ceSSanjay Lal struct kvm_vcpu_arch {
291878edf01SJames Hogan void *guest_ebase;
2920b7aa583STianjia Zhang int (*vcpu_run)(struct kvm_vcpu *vcpu);
2931934a3adSJames Hogan
2941934a3adSJames Hogan /* Host registers preserved across guest mode execution */
295740765ceSSanjay Lal unsigned long host_stack;
296740765ceSSanjay Lal unsigned long host_gp;
2971934a3adSJames Hogan unsigned long host_pgd;
2981934a3adSJames Hogan unsigned long host_entryhi;
299740765ceSSanjay Lal
300740765ceSSanjay Lal /* Host CP0 registers used when handling exits from guest */
301740765ceSSanjay Lal unsigned long host_cp0_badvaddr;
302740765ceSSanjay Lal unsigned long host_cp0_epc;
30331cf7498SJames Hogan u32 host_cp0_cause;
3041934a3adSJames Hogan u32 host_cp0_guestctl0;
3056a97c775SJames Hogan u32 host_cp0_badinstr;
3066a97c775SJames Hogan u32 host_cp0_badinstrp;
307740765ceSSanjay Lal
308740765ceSSanjay Lal /* GPRS */
309740765ceSSanjay Lal unsigned long gprs[32];
310740765ceSSanjay Lal unsigned long hi;
311740765ceSSanjay Lal unsigned long lo;
312740765ceSSanjay Lal unsigned long pc;
313740765ceSSanjay Lal
314740765ceSSanjay Lal /* FPU State */
315740765ceSSanjay Lal struct mips_fpu_struct fpu;
316f943176aSJames Hogan /* Which auxiliary state is loaded (KVM_MIPS_AUX_*) */
317f943176aSJames Hogan unsigned int aux_inuse;
318740765ceSSanjay Lal
319740765ceSSanjay Lal /* COP0 State */
320e4de2057SHuacai Chen struct mips_coproc cop0;
321740765ceSSanjay Lal
322e1e575f6SJames Hogan /* Resume PC after MMIO completion */
323e1e575f6SJames Hogan unsigned long io_pc;
324e1e575f6SJames Hogan /* GPR used as IO source/target */
325e1e575f6SJames Hogan u32 io_gpr;
326740765ceSSanjay Lal
327e30492bbSJames Hogan struct hrtimer comparecount_timer;
328f8239342SJames Hogan /* Count timer control KVM register */
329bdb7ed86SJames Hogan u32 count_ctl;
330e30492bbSJames Hogan /* Count bias from the raw time */
331bdb7ed86SJames Hogan u32 count_bias;
332e30492bbSJames Hogan /* Frequency of timer in Hz */
333bdb7ed86SJames Hogan u32 count_hz;
334e30492bbSJames Hogan /* Dynamic nanosecond bias (multiple of count_period) to avoid overflow */
335e30492bbSJames Hogan s64 count_dyn_bias;
336f8239342SJames Hogan /* Resume time */
337f8239342SJames Hogan ktime_t count_resume;
338e30492bbSJames Hogan /* Period of timer tick in ns */
339e30492bbSJames Hogan u64 count_period;
340740765ceSSanjay Lal
341740765ceSSanjay Lal /* Bitmask of exceptions that are pending */
342740765ceSSanjay Lal unsigned long pending_exceptions;
343740765ceSSanjay Lal
344740765ceSSanjay Lal /* Bitmask of pending exceptions to be cleared */
345740765ceSSanjay Lal unsigned long pending_exceptions_clr;
346740765ceSSanjay Lal
347aba85929SJames Hogan /* Cache some mmu pages needed inside spinlock regions */
348aba85929SJames Hogan struct kvm_mmu_memory_cache mmu_page_cache;
349aba85929SJames Hogan
350c992a4f6SJames Hogan /* vcpu's vzguestid is different on each host cpu in an smp system */
351c992a4f6SJames Hogan u32 vzguestid[NR_CPUS];
352c992a4f6SJames Hogan
353c992a4f6SJames Hogan /* wired guest TLB entries */
354c992a4f6SJames Hogan struct kvm_mips_tlb *wired_tlb;
355c992a4f6SJames Hogan unsigned int wired_tlb_limit;
356c992a4f6SJames Hogan unsigned int wired_tlb_used;
357d42a008fSJames Hogan
358d42a008fSJames Hogan /* emulated guest MAAR registers */
359d42a008fSJames Hogan unsigned long maar[6];
360c992a4f6SJames Hogan
361c992a4f6SJames Hogan /* Last CPU the VCPU state was loaded on */
362740765ceSSanjay Lal int last_sched_cpu;
363c992a4f6SJames Hogan /* Last CPU the VCPU actually executed guest code on */
364c992a4f6SJames Hogan int last_exec_cpu;
365740765ceSSanjay Lal
366740765ceSSanjay Lal /* WAIT executed */
367740765ceSSanjay Lal int wait;
36898e91b84SJames Hogan
36998e91b84SJames Hogan u8 fpu_enabled;
370539cb89fSJames Hogan u8 msa_enabled;
371740765ceSSanjay Lal };
372740765ceSSanjay Lal
_kvm_atomic_set_c0_guest_reg(unsigned long * reg,unsigned long val)373c73c99b0SJames Hogan static inline void _kvm_atomic_set_c0_guest_reg(unsigned long *reg,
374c73c99b0SJames Hogan unsigned long val)
375c73c99b0SJames Hogan {
376c73c99b0SJames Hogan unsigned long temp;
377c73c99b0SJames Hogan do {
378c73c99b0SJames Hogan __asm__ __volatile__(
379378ed6f0SPaul Burton " .set push \n"
380d85ebff0SJames Hogan " .set "MIPS_ISA_ARCH_LEVEL" \n"
381f0b7ddbdSHuang Pei " "__stringify(LONG_LL) " %0, %1 \n"
382c73c99b0SJames Hogan " or %0, %2 \n"
383f0b7ddbdSHuang Pei " "__stringify(LONG_SC) " %0, %1 \n"
384378ed6f0SPaul Burton " .set pop \n"
385c73c99b0SJames Hogan : "=&r" (temp), "+m" (*reg)
386c73c99b0SJames Hogan : "r" (val));
387c73c99b0SJames Hogan } while (unlikely(!temp));
388c73c99b0SJames Hogan }
389c73c99b0SJames Hogan
_kvm_atomic_clear_c0_guest_reg(unsigned long * reg,unsigned long val)390c73c99b0SJames Hogan static inline void _kvm_atomic_clear_c0_guest_reg(unsigned long *reg,
391c73c99b0SJames Hogan unsigned long val)
392c73c99b0SJames Hogan {
393c73c99b0SJames Hogan unsigned long temp;
394c73c99b0SJames Hogan do {
395c73c99b0SJames Hogan __asm__ __volatile__(
396378ed6f0SPaul Burton " .set push \n"
397d85ebff0SJames Hogan " .set "MIPS_ISA_ARCH_LEVEL" \n"
398f0b7ddbdSHuang Pei " "__stringify(LONG_LL) " %0, %1 \n"
399c73c99b0SJames Hogan " and %0, %2 \n"
400f0b7ddbdSHuang Pei " "__stringify(LONG_SC) " %0, %1 \n"
401378ed6f0SPaul Burton " .set pop \n"
402c73c99b0SJames Hogan : "=&r" (temp), "+m" (*reg)
403c73c99b0SJames Hogan : "r" (~val));
404c73c99b0SJames Hogan } while (unlikely(!temp));
405c73c99b0SJames Hogan }
406c73c99b0SJames Hogan
_kvm_atomic_change_c0_guest_reg(unsigned long * reg,unsigned long change,unsigned long val)407c73c99b0SJames Hogan static inline void _kvm_atomic_change_c0_guest_reg(unsigned long *reg,
408c73c99b0SJames Hogan unsigned long change,
409c73c99b0SJames Hogan unsigned long val)
410c73c99b0SJames Hogan {
411c73c99b0SJames Hogan unsigned long temp;
412c73c99b0SJames Hogan do {
413c73c99b0SJames Hogan __asm__ __volatile__(
414378ed6f0SPaul Burton " .set push \n"
415d85ebff0SJames Hogan " .set "MIPS_ISA_ARCH_LEVEL" \n"
416f0b7ddbdSHuang Pei " "__stringify(LONG_LL) " %0, %1 \n"
417c73c99b0SJames Hogan " and %0, %2 \n"
418c73c99b0SJames Hogan " or %0, %3 \n"
419f0b7ddbdSHuang Pei " "__stringify(LONG_SC) " %0, %1 \n"
420378ed6f0SPaul Burton " .set pop \n"
421c73c99b0SJames Hogan : "=&r" (temp), "+m" (*reg)
422c73c99b0SJames Hogan : "r" (~change), "r" (val & change));
423c73c99b0SJames Hogan } while (unlikely(!temp));
424c73c99b0SJames Hogan }
425c73c99b0SJames Hogan
426a27660f3SJames Hogan /* Guest register types, used in accessor build below */
427a27660f3SJames Hogan #define __KVMT32 u32
428a27660f3SJames Hogan #define __KVMTl unsigned long
429c73c99b0SJames Hogan
430a27660f3SJames Hogan /*
431a27660f3SJames Hogan * __BUILD_KVM_$ops_SAVED(): kvm_$op_sw_gc0_$reg()
432a27660f3SJames Hogan * These operate on the saved guest C0 state in RAM.
433a27660f3SJames Hogan */
434c73c99b0SJames Hogan
435a27660f3SJames Hogan /* Generate saved context simple accessors */
436a27660f3SJames Hogan #define __BUILD_KVM_RW_SAVED(name, type, _reg, sel) \
437a27660f3SJames Hogan static inline __KVMT##type kvm_read_sw_gc0_##name(struct mips_coproc *cop0) \
438740765ceSSanjay Lal { \
439a27660f3SJames Hogan return cop0->reg[(_reg)][(sel)]; \
440a27660f3SJames Hogan } \
441a27660f3SJames Hogan static inline void kvm_write_sw_gc0_##name(struct mips_coproc *cop0, \
442a27660f3SJames Hogan __KVMT##type val) \
443a27660f3SJames Hogan { \
444a27660f3SJames Hogan cop0->reg[(_reg)][(sel)] = val; \
445740765ceSSanjay Lal }
446740765ceSSanjay Lal
447a27660f3SJames Hogan /* Generate saved context bitwise modifiers */
448a27660f3SJames Hogan #define __BUILD_KVM_SET_SAVED(name, type, _reg, sel) \
449a27660f3SJames Hogan static inline void kvm_set_sw_gc0_##name(struct mips_coproc *cop0, \
450a27660f3SJames Hogan __KVMT##type val) \
451a27660f3SJames Hogan { \
452a27660f3SJames Hogan cop0->reg[(_reg)][(sel)] |= val; \
453a27660f3SJames Hogan } \
454a27660f3SJames Hogan static inline void kvm_clear_sw_gc0_##name(struct mips_coproc *cop0, \
455a27660f3SJames Hogan __KVMT##type val) \
456a27660f3SJames Hogan { \
457a27660f3SJames Hogan cop0->reg[(_reg)][(sel)] &= ~val; \
458a27660f3SJames Hogan } \
459a27660f3SJames Hogan static inline void kvm_change_sw_gc0_##name(struct mips_coproc *cop0, \
460a27660f3SJames Hogan __KVMT##type mask, \
461a27660f3SJames Hogan __KVMT##type val) \
462a27660f3SJames Hogan { \
463a27660f3SJames Hogan unsigned long _mask = mask; \
464a27660f3SJames Hogan cop0->reg[(_reg)][(sel)] &= ~_mask; \
465a27660f3SJames Hogan cop0->reg[(_reg)][(sel)] |= val & _mask; \
466a27660f3SJames Hogan }
467a27660f3SJames Hogan
468a27660f3SJames Hogan /* Generate saved context atomic bitwise modifiers */
469a27660f3SJames Hogan #define __BUILD_KVM_ATOMIC_SAVED(name, type, _reg, sel) \
470a27660f3SJames Hogan static inline void kvm_set_sw_gc0_##name(struct mips_coproc *cop0, \
471a27660f3SJames Hogan __KVMT##type val) \
472a27660f3SJames Hogan { \
473a27660f3SJames Hogan _kvm_atomic_set_c0_guest_reg(&cop0->reg[(_reg)][(sel)], val); \
474a27660f3SJames Hogan } \
475a27660f3SJames Hogan static inline void kvm_clear_sw_gc0_##name(struct mips_coproc *cop0, \
476a27660f3SJames Hogan __KVMT##type val) \
477a27660f3SJames Hogan { \
478a27660f3SJames Hogan _kvm_atomic_clear_c0_guest_reg(&cop0->reg[(_reg)][(sel)], val); \
479a27660f3SJames Hogan } \
480a27660f3SJames Hogan static inline void kvm_change_sw_gc0_##name(struct mips_coproc *cop0, \
481a27660f3SJames Hogan __KVMT##type mask, \
482a27660f3SJames Hogan __KVMT##type val) \
483a27660f3SJames Hogan { \
484a27660f3SJames Hogan _kvm_atomic_change_c0_guest_reg(&cop0->reg[(_reg)][(sel)], mask, \
485a27660f3SJames Hogan val); \
486a27660f3SJames Hogan }
487a27660f3SJames Hogan
488a27660f3SJames Hogan /*
489a27660f3SJames Hogan * __BUILD_KVM_$ops_VZ(): kvm_$op_vz_gc0_$reg()
490a27660f3SJames Hogan * These operate on the VZ guest C0 context in hardware.
491a27660f3SJames Hogan */
492a27660f3SJames Hogan
493a27660f3SJames Hogan /* Generate VZ guest context simple accessors */
494a27660f3SJames Hogan #define __BUILD_KVM_RW_VZ(name, type, _reg, sel) \
495a27660f3SJames Hogan static inline __KVMT##type kvm_read_vz_gc0_##name(struct mips_coproc *cop0) \
496a27660f3SJames Hogan { \
497a27660f3SJames Hogan return read_gc0_##name(); \
498a27660f3SJames Hogan } \
499a27660f3SJames Hogan static inline void kvm_write_vz_gc0_##name(struct mips_coproc *cop0, \
500a27660f3SJames Hogan __KVMT##type val) \
501a27660f3SJames Hogan { \
502a27660f3SJames Hogan write_gc0_##name(val); \
503a27660f3SJames Hogan }
504a27660f3SJames Hogan
505a27660f3SJames Hogan /* Generate VZ guest context bitwise modifiers */
506a27660f3SJames Hogan #define __BUILD_KVM_SET_VZ(name, type, _reg, sel) \
507a27660f3SJames Hogan static inline void kvm_set_vz_gc0_##name(struct mips_coproc *cop0, \
508a27660f3SJames Hogan __KVMT##type val) \
509a27660f3SJames Hogan { \
510a27660f3SJames Hogan set_gc0_##name(val); \
511a27660f3SJames Hogan } \
512a27660f3SJames Hogan static inline void kvm_clear_vz_gc0_##name(struct mips_coproc *cop0, \
513a27660f3SJames Hogan __KVMT##type val) \
514a27660f3SJames Hogan { \
515a27660f3SJames Hogan clear_gc0_##name(val); \
516a27660f3SJames Hogan } \
517a27660f3SJames Hogan static inline void kvm_change_vz_gc0_##name(struct mips_coproc *cop0, \
518a27660f3SJames Hogan __KVMT##type mask, \
519a27660f3SJames Hogan __KVMT##type val) \
520a27660f3SJames Hogan { \
521a27660f3SJames Hogan change_gc0_##name(mask, val); \
522a27660f3SJames Hogan }
523a27660f3SJames Hogan
524a27660f3SJames Hogan /* Generate VZ guest context save/restore to/from saved context */
525a27660f3SJames Hogan #define __BUILD_KVM_SAVE_VZ(name, _reg, sel) \
526a27660f3SJames Hogan static inline void kvm_restore_gc0_##name(struct mips_coproc *cop0) \
527a27660f3SJames Hogan { \
528a27660f3SJames Hogan write_gc0_##name(cop0->reg[(_reg)][(sel)]); \
529a27660f3SJames Hogan } \
530a27660f3SJames Hogan static inline void kvm_save_gc0_##name(struct mips_coproc *cop0) \
531a27660f3SJames Hogan { \
532a27660f3SJames Hogan cop0->reg[(_reg)][(sel)] = read_gc0_##name(); \
533a27660f3SJames Hogan }
534a27660f3SJames Hogan
535a27660f3SJames Hogan /*
536a27660f3SJames Hogan * __BUILD_KVM_$ops_WRAP(): kvm_$op_$name1() -> kvm_$op_$name2()
537a27660f3SJames Hogan * These wrap a set of operations to provide them with a different name.
538a27660f3SJames Hogan */
539a27660f3SJames Hogan
540a27660f3SJames Hogan /* Generate simple accessor wrapper */
541a27660f3SJames Hogan #define __BUILD_KVM_RW_WRAP(name1, name2, type) \
542a27660f3SJames Hogan static inline __KVMT##type kvm_read_##name1(struct mips_coproc *cop0) \
543a27660f3SJames Hogan { \
544a27660f3SJames Hogan return kvm_read_##name2(cop0); \
545a27660f3SJames Hogan } \
546a27660f3SJames Hogan static inline void kvm_write_##name1(struct mips_coproc *cop0, \
547a27660f3SJames Hogan __KVMT##type val) \
548a27660f3SJames Hogan { \
549a27660f3SJames Hogan kvm_write_##name2(cop0, val); \
550a27660f3SJames Hogan }
551a27660f3SJames Hogan
552a27660f3SJames Hogan /* Generate bitwise modifier wrapper */
553a27660f3SJames Hogan #define __BUILD_KVM_SET_WRAP(name1, name2, type) \
554a27660f3SJames Hogan static inline void kvm_set_##name1(struct mips_coproc *cop0, \
555a27660f3SJames Hogan __KVMT##type val) \
556a27660f3SJames Hogan { \
557a27660f3SJames Hogan kvm_set_##name2(cop0, val); \
558a27660f3SJames Hogan } \
559a27660f3SJames Hogan static inline void kvm_clear_##name1(struct mips_coproc *cop0, \
560a27660f3SJames Hogan __KVMT##type val) \
561a27660f3SJames Hogan { \
562a27660f3SJames Hogan kvm_clear_##name2(cop0, val); \
563a27660f3SJames Hogan } \
564a27660f3SJames Hogan static inline void kvm_change_##name1(struct mips_coproc *cop0, \
565a27660f3SJames Hogan __KVMT##type mask, \
566a27660f3SJames Hogan __KVMT##type val) \
567a27660f3SJames Hogan { \
568a27660f3SJames Hogan kvm_change_##name2(cop0, mask, val); \
569a27660f3SJames Hogan }
570a27660f3SJames Hogan
571a27660f3SJames Hogan /*
572a27660f3SJames Hogan * __BUILD_KVM_$ops_SW(): kvm_$op_c0_guest_$reg() -> kvm_$op_sw_gc0_$reg()
573a27660f3SJames Hogan * These generate accessors operating on the saved context in RAM, and wrap them
574a27660f3SJames Hogan * with the common guest C0 accessors (for use by common emulation code).
575a27660f3SJames Hogan */
576a27660f3SJames Hogan
577a27660f3SJames Hogan #define __BUILD_KVM_RW_SW(name, type, _reg, sel) \
578a27660f3SJames Hogan __BUILD_KVM_RW_SAVED(name, type, _reg, sel) \
579a27660f3SJames Hogan __BUILD_KVM_RW_WRAP(c0_guest_##name, sw_gc0_##name, type)
580a27660f3SJames Hogan
581a27660f3SJames Hogan #define __BUILD_KVM_SET_SW(name, type, _reg, sel) \
582a27660f3SJames Hogan __BUILD_KVM_SET_SAVED(name, type, _reg, sel) \
583a27660f3SJames Hogan __BUILD_KVM_SET_WRAP(c0_guest_##name, sw_gc0_##name, type)
584a27660f3SJames Hogan
585a27660f3SJames Hogan #define __BUILD_KVM_ATOMIC_SW(name, type, _reg, sel) \
586a27660f3SJames Hogan __BUILD_KVM_ATOMIC_SAVED(name, type, _reg, sel) \
587a27660f3SJames Hogan __BUILD_KVM_SET_WRAP(c0_guest_##name, sw_gc0_##name, type)
588a27660f3SJames Hogan
589a27660f3SJames Hogan /*
590a27660f3SJames Hogan * VZ (hardware assisted virtualisation)
591a27660f3SJames Hogan * These macros use the active guest state in VZ mode (hardware registers),
592a27660f3SJames Hogan */
593a27660f3SJames Hogan
594a27660f3SJames Hogan /*
595a27660f3SJames Hogan * __BUILD_KVM_$ops_HW(): kvm_$op_c0_guest_$reg() -> kvm_$op_vz_gc0_$reg()
596a27660f3SJames Hogan * These generate accessors operating on the VZ guest context in hardware, and
597a27660f3SJames Hogan * wrap them with the common guest C0 accessors (for use by common emulation
598a27660f3SJames Hogan * code).
599a27660f3SJames Hogan *
600a27660f3SJames Hogan * Accessors operating on the saved context in RAM are also generated to allow
601a27660f3SJames Hogan * convenient explicit saving and restoring of the state.
602a27660f3SJames Hogan */
603a27660f3SJames Hogan
604a27660f3SJames Hogan #define __BUILD_KVM_RW_HW(name, type, _reg, sel) \
605a27660f3SJames Hogan __BUILD_KVM_RW_SAVED(name, type, _reg, sel) \
606a27660f3SJames Hogan __BUILD_KVM_RW_VZ(name, type, _reg, sel) \
607a27660f3SJames Hogan __BUILD_KVM_RW_WRAP(c0_guest_##name, vz_gc0_##name, type) \
608a27660f3SJames Hogan __BUILD_KVM_SAVE_VZ(name, _reg, sel)
609a27660f3SJames Hogan
610a27660f3SJames Hogan #define __BUILD_KVM_SET_HW(name, type, _reg, sel) \
611a27660f3SJames Hogan __BUILD_KVM_SET_SAVED(name, type, _reg, sel) \
612a27660f3SJames Hogan __BUILD_KVM_SET_VZ(name, type, _reg, sel) \
613a27660f3SJames Hogan __BUILD_KVM_SET_WRAP(c0_guest_##name, vz_gc0_##name, type)
614a27660f3SJames Hogan
615a27660f3SJames Hogan /*
616a27660f3SJames Hogan * We can't do atomic modifications of COP0 state if hardware can modify it.
617a27660f3SJames Hogan * Races must be handled explicitly.
618a27660f3SJames Hogan */
619a27660f3SJames Hogan #define __BUILD_KVM_ATOMIC_HW __BUILD_KVM_SET_HW
620a27660f3SJames Hogan
621a27660f3SJames Hogan /*
622a27660f3SJames Hogan * Define accessors for CP0 registers that are accessible to the guest. These
623a27660f3SJames Hogan * are primarily used by common emulation code, which may need to access the
624a27660f3SJames Hogan * registers differently depending on the implementation.
625a27660f3SJames Hogan *
626a27660f3SJames Hogan * fns_hw/sw name type reg num select
627a27660f3SJames Hogan */
628a27660f3SJames Hogan __BUILD_KVM_RW_HW(index, 32, MIPS_CP0_TLB_INDEX, 0)
629a27660f3SJames Hogan __BUILD_KVM_RW_HW(entrylo0, l, MIPS_CP0_TLB_LO0, 0)
630a27660f3SJames Hogan __BUILD_KVM_RW_HW(entrylo1, l, MIPS_CP0_TLB_LO1, 0)
631a27660f3SJames Hogan __BUILD_KVM_RW_HW(context, l, MIPS_CP0_TLB_CONTEXT, 0)
632dffe042fSJames Hogan __BUILD_KVM_RW_HW(contextconfig, 32, MIPS_CP0_TLB_CONTEXT, 1)
633a27660f3SJames Hogan __BUILD_KVM_RW_HW(userlocal, l, MIPS_CP0_TLB_CONTEXT, 2)
634dffe042fSJames Hogan __BUILD_KVM_RW_HW(xcontextconfig, l, MIPS_CP0_TLB_CONTEXT, 3)
635a27660f3SJames Hogan __BUILD_KVM_RW_HW(pagemask, l, MIPS_CP0_TLB_PG_MASK, 0)
636a27660f3SJames Hogan __BUILD_KVM_RW_HW(pagegrain, 32, MIPS_CP0_TLB_PG_MASK, 1)
6374b7de028SJames Hogan __BUILD_KVM_RW_HW(segctl0, l, MIPS_CP0_TLB_PG_MASK, 2)
6384b7de028SJames Hogan __BUILD_KVM_RW_HW(segctl1, l, MIPS_CP0_TLB_PG_MASK, 3)
6394b7de028SJames Hogan __BUILD_KVM_RW_HW(segctl2, l, MIPS_CP0_TLB_PG_MASK, 4)
6405a2f352fSJames Hogan __BUILD_KVM_RW_HW(pwbase, l, MIPS_CP0_TLB_PG_MASK, 5)
6415a2f352fSJames Hogan __BUILD_KVM_RW_HW(pwfield, l, MIPS_CP0_TLB_PG_MASK, 6)
6425a2f352fSJames Hogan __BUILD_KVM_RW_HW(pwsize, l, MIPS_CP0_TLB_PG_MASK, 7)
643a27660f3SJames Hogan __BUILD_KVM_RW_HW(wired, 32, MIPS_CP0_TLB_WIRED, 0)
6445a2f352fSJames Hogan __BUILD_KVM_RW_HW(pwctl, 32, MIPS_CP0_TLB_WIRED, 6)
645a27660f3SJames Hogan __BUILD_KVM_RW_HW(hwrena, 32, MIPS_CP0_HWRENA, 0)
646a27660f3SJames Hogan __BUILD_KVM_RW_HW(badvaddr, l, MIPS_CP0_BAD_VADDR, 0)
647edc89260SJames Hogan __BUILD_KVM_RW_HW(badinstr, 32, MIPS_CP0_BAD_VADDR, 1)
648edc89260SJames Hogan __BUILD_KVM_RW_HW(badinstrp, 32, MIPS_CP0_BAD_VADDR, 2)
649a27660f3SJames Hogan __BUILD_KVM_RW_SW(count, 32, MIPS_CP0_COUNT, 0)
650a27660f3SJames Hogan __BUILD_KVM_RW_HW(entryhi, l, MIPS_CP0_TLB_HI, 0)
651a27660f3SJames Hogan __BUILD_KVM_RW_HW(compare, 32, MIPS_CP0_COMPARE, 0)
652a27660f3SJames Hogan __BUILD_KVM_RW_HW(status, 32, MIPS_CP0_STATUS, 0)
653a27660f3SJames Hogan __BUILD_KVM_RW_HW(intctl, 32, MIPS_CP0_STATUS, 1)
654a27660f3SJames Hogan __BUILD_KVM_RW_HW(cause, 32, MIPS_CP0_CAUSE, 0)
655a27660f3SJames Hogan __BUILD_KVM_RW_HW(epc, l, MIPS_CP0_EXC_PC, 0)
656a27660f3SJames Hogan __BUILD_KVM_RW_SW(prid, 32, MIPS_CP0_PRID, 0)
657a27660f3SJames Hogan __BUILD_KVM_RW_HW(ebase, l, MIPS_CP0_PRID, 1)
658a27660f3SJames Hogan __BUILD_KVM_RW_HW(config, 32, MIPS_CP0_CONFIG, 0)
659a27660f3SJames Hogan __BUILD_KVM_RW_HW(config1, 32, MIPS_CP0_CONFIG, 1)
660a27660f3SJames Hogan __BUILD_KVM_RW_HW(config2, 32, MIPS_CP0_CONFIG, 2)
661a27660f3SJames Hogan __BUILD_KVM_RW_HW(config3, 32, MIPS_CP0_CONFIG, 3)
662a27660f3SJames Hogan __BUILD_KVM_RW_HW(config4, 32, MIPS_CP0_CONFIG, 4)
663a27660f3SJames Hogan __BUILD_KVM_RW_HW(config5, 32, MIPS_CP0_CONFIG, 5)
664a27660f3SJames Hogan __BUILD_KVM_RW_HW(config6, 32, MIPS_CP0_CONFIG, 6)
665a27660f3SJames Hogan __BUILD_KVM_RW_HW(config7, 32, MIPS_CP0_CONFIG, 7)
666d42a008fSJames Hogan __BUILD_KVM_RW_SW(maari, l, MIPS_CP0_LLADDR, 2)
667c992a4f6SJames Hogan __BUILD_KVM_RW_HW(xcontext, l, MIPS_CP0_TLB_XCONTEXT, 0)
668a27660f3SJames Hogan __BUILD_KVM_RW_HW(errorepc, l, MIPS_CP0_ERROR_PC, 0)
669a27660f3SJames Hogan __BUILD_KVM_RW_HW(kscratch1, l, MIPS_CP0_DESAVE, 2)
670a27660f3SJames Hogan __BUILD_KVM_RW_HW(kscratch2, l, MIPS_CP0_DESAVE, 3)
671a27660f3SJames Hogan __BUILD_KVM_RW_HW(kscratch3, l, MIPS_CP0_DESAVE, 4)
672a27660f3SJames Hogan __BUILD_KVM_RW_HW(kscratch4, l, MIPS_CP0_DESAVE, 5)
673a27660f3SJames Hogan __BUILD_KVM_RW_HW(kscratch5, l, MIPS_CP0_DESAVE, 6)
674a27660f3SJames Hogan __BUILD_KVM_RW_HW(kscratch6, l, MIPS_CP0_DESAVE, 7)
675a27660f3SJames Hogan
676a27660f3SJames Hogan /* Bitwise operations (on HW state) */
677a27660f3SJames Hogan __BUILD_KVM_SET_HW(status, 32, MIPS_CP0_STATUS, 0)
678a27660f3SJames Hogan /* Cause can be modified asynchronously from hardirq hrtimer callback */
679a27660f3SJames Hogan __BUILD_KVM_ATOMIC_HW(cause, 32, MIPS_CP0_CAUSE, 0)
680a27660f3SJames Hogan __BUILD_KVM_SET_HW(ebase, l, MIPS_CP0_PRID, 1)
681a27660f3SJames Hogan
682c992a4f6SJames Hogan /* Bitwise operations (on saved state) */
683c992a4f6SJames Hogan __BUILD_KVM_SET_SAVED(config, 32, MIPS_CP0_CONFIG, 0)
684c992a4f6SJames Hogan __BUILD_KVM_SET_SAVED(config1, 32, MIPS_CP0_CONFIG, 1)
685c992a4f6SJames Hogan __BUILD_KVM_SET_SAVED(config2, 32, MIPS_CP0_CONFIG, 2)
686c992a4f6SJames Hogan __BUILD_KVM_SET_SAVED(config3, 32, MIPS_CP0_CONFIG, 3)
687c992a4f6SJames Hogan __BUILD_KVM_SET_SAVED(config4, 32, MIPS_CP0_CONFIG, 4)
688c992a4f6SJames Hogan __BUILD_KVM_SET_SAVED(config5, 32, MIPS_CP0_CONFIG, 5)
689c992a4f6SJames Hogan
69098e91b84SJames Hogan /* Helpers */
69198e91b84SJames Hogan
kvm_mips_guest_can_have_fpu(struct kvm_vcpu_arch * vcpu)69298e91b84SJames Hogan static inline bool kvm_mips_guest_can_have_fpu(struct kvm_vcpu_arch *vcpu)
69398e91b84SJames Hogan {
69419451e51SJames Hogan return (!__builtin_constant_p(raw_cpu_has_fpu) || raw_cpu_has_fpu) &&
69598e91b84SJames Hogan vcpu->fpu_enabled;
69698e91b84SJames Hogan }
69798e91b84SJames Hogan
kvm_mips_guest_has_fpu(struct kvm_vcpu_arch * vcpu)69898e91b84SJames Hogan static inline bool kvm_mips_guest_has_fpu(struct kvm_vcpu_arch *vcpu)
69998e91b84SJames Hogan {
70098e91b84SJames Hogan return kvm_mips_guest_can_have_fpu(vcpu) &&
701e4de2057SHuacai Chen kvm_read_c0_guest_config1(&vcpu->cop0) & MIPS_CONF1_FP;
70298e91b84SJames Hogan }
703740765ceSSanjay Lal
kvm_mips_guest_can_have_msa(struct kvm_vcpu_arch * vcpu)704539cb89fSJames Hogan static inline bool kvm_mips_guest_can_have_msa(struct kvm_vcpu_arch *vcpu)
705539cb89fSJames Hogan {
706539cb89fSJames Hogan return (!__builtin_constant_p(cpu_has_msa) || cpu_has_msa) &&
707539cb89fSJames Hogan vcpu->msa_enabled;
708539cb89fSJames Hogan }
709539cb89fSJames Hogan
kvm_mips_guest_has_msa(struct kvm_vcpu_arch * vcpu)710539cb89fSJames Hogan static inline bool kvm_mips_guest_has_msa(struct kvm_vcpu_arch *vcpu)
711539cb89fSJames Hogan {
712539cb89fSJames Hogan return kvm_mips_guest_can_have_msa(vcpu) &&
713e4de2057SHuacai Chen kvm_read_c0_guest_config3(&vcpu->cop0) & MIPS_CONF3_MSA;
714539cb89fSJames Hogan }
715539cb89fSJames Hogan
716740765ceSSanjay Lal struct kvm_mips_callbacks {
717740765ceSSanjay Lal int (*handle_cop_unusable)(struct kvm_vcpu *vcpu);
718740765ceSSanjay Lal int (*handle_tlb_mod)(struct kvm_vcpu *vcpu);
719740765ceSSanjay Lal int (*handle_tlb_ld_miss)(struct kvm_vcpu *vcpu);
720740765ceSSanjay Lal int (*handle_tlb_st_miss)(struct kvm_vcpu *vcpu);
721740765ceSSanjay Lal int (*handle_addr_err_st)(struct kvm_vcpu *vcpu);
722740765ceSSanjay Lal int (*handle_addr_err_ld)(struct kvm_vcpu *vcpu);
723740765ceSSanjay Lal int (*handle_syscall)(struct kvm_vcpu *vcpu);
724740765ceSSanjay Lal int (*handle_res_inst)(struct kvm_vcpu *vcpu);
725740765ceSSanjay Lal int (*handle_break)(struct kvm_vcpu *vcpu);
7260a560427SJames Hogan int (*handle_trap)(struct kvm_vcpu *vcpu);
727c2537ed9SJames Hogan int (*handle_msa_fpe)(struct kvm_vcpu *vcpu);
7281c0cd66aSJames Hogan int (*handle_fpe)(struct kvm_vcpu *vcpu);
72998119ad5SJames Hogan int (*handle_msa_disabled)(struct kvm_vcpu *vcpu);
73028c1e762SJames Hogan int (*handle_guest_exit)(struct kvm_vcpu *vcpu);
731edab4fe1SJames Hogan int (*hardware_enable)(void);
732edab4fe1SJames Hogan void (*hardware_disable)(void);
733607ef2fdSJames Hogan int (*check_extension)(struct kvm *kvm, long ext);
734740765ceSSanjay Lal int (*vcpu_init)(struct kvm_vcpu *vcpu);
735630766b3SJames Hogan void (*vcpu_uninit)(struct kvm_vcpu *vcpu);
736740765ceSSanjay Lal int (*vcpu_setup)(struct kvm_vcpu *vcpu);
7375194552fSPaolo Bonzini void (*prepare_flush_shadow)(struct kvm *kvm);
738740765ceSSanjay Lal gpa_t (*gva_to_gpa)(gva_t gva);
739740765ceSSanjay Lal void (*queue_timer_int)(struct kvm_vcpu *vcpu);
740740765ceSSanjay Lal void (*dequeue_timer_int)(struct kvm_vcpu *vcpu);
741740765ceSSanjay Lal void (*queue_io_int)(struct kvm_vcpu *vcpu,
742740765ceSSanjay Lal struct kvm_mips_interrupt *irq);
743740765ceSSanjay Lal void (*dequeue_io_int)(struct kvm_vcpu *vcpu,
744740765ceSSanjay Lal struct kvm_mips_interrupt *irq);
745740765ceSSanjay Lal int (*irq_deliver)(struct kvm_vcpu *vcpu, unsigned int priority,
746bdb7ed86SJames Hogan u32 cause);
747740765ceSSanjay Lal int (*irq_clear)(struct kvm_vcpu *vcpu, unsigned int priority,
748bdb7ed86SJames Hogan u32 cause);
749f5c43bd4SJames Hogan unsigned long (*num_regs)(struct kvm_vcpu *vcpu);
750f5c43bd4SJames Hogan int (*copy_reg_indices)(struct kvm_vcpu *vcpu, u64 __user *indices);
751f8be02daSJames Hogan int (*get_one_reg)(struct kvm_vcpu *vcpu,
752f8be02daSJames Hogan const struct kvm_one_reg *reg, s64 *v);
753f8be02daSJames Hogan int (*set_one_reg)(struct kvm_vcpu *vcpu,
754f8be02daSJames Hogan const struct kvm_one_reg *reg, s64 v);
755a60b8438SJames Hogan int (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
756a60b8438SJames Hogan int (*vcpu_put)(struct kvm_vcpu *vcpu, int cpu);
757c34b26b9STianjia Zhang int (*vcpu_run)(struct kvm_vcpu *vcpu);
758c34b26b9STianjia Zhang void (*vcpu_reenter)(struct kvm_vcpu *vcpu);
759740765ceSSanjay Lal };
7607ffc2e89SSean Christopherson extern const struct kvm_mips_callbacks * const kvm_mips_callbacks;
7611cfc1c7bSSean Christopherson int kvm_mips_emulation_init(void);
762740765ceSSanjay Lal
763740765ceSSanjay Lal /* Debug: dump vcpu state */
764740765ceSSanjay Lal int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu);
765740765ceSSanjay Lal
7660b7aa583STianjia Zhang extern int kvm_mips_handle_exit(struct kvm_vcpu *vcpu);
76790e9311aSJames Hogan
76890e9311aSJames Hogan /* Building of entry/exception code */
7691e5217f5SJames Hogan int kvm_mips_entry_setup(void);
77090e9311aSJames Hogan void *kvm_mips_build_vcpu_run(void *addr);
771a7cfa7acSJames Hogan void *kvm_mips_build_tlb_refill_exception(void *addr, void *handler);
7721f9ca62cSJames Hogan void *kvm_mips_build_exception(void *addr, void *handler);
77390e9311aSJames Hogan void *kvm_mips_build_exit(void *addr);
774740765ceSSanjay Lal
775539cb89fSJames Hogan /* FPU/MSA context management */
77698e91b84SJames Hogan void __kvm_save_fpu(struct kvm_vcpu_arch *vcpu);
77798e91b84SJames Hogan void __kvm_restore_fpu(struct kvm_vcpu_arch *vcpu);
77898e91b84SJames Hogan void __kvm_restore_fcsr(struct kvm_vcpu_arch *vcpu);
779539cb89fSJames Hogan void __kvm_save_msa(struct kvm_vcpu_arch *vcpu);
780539cb89fSJames Hogan void __kvm_restore_msa(struct kvm_vcpu_arch *vcpu);
781539cb89fSJames Hogan void __kvm_restore_msa_upper(struct kvm_vcpu_arch *vcpu);
782539cb89fSJames Hogan void __kvm_restore_msacsr(struct kvm_vcpu_arch *vcpu);
78398e91b84SJames Hogan void kvm_own_fpu(struct kvm_vcpu *vcpu);
784539cb89fSJames Hogan void kvm_own_msa(struct kvm_vcpu *vcpu);
78598e91b84SJames Hogan void kvm_drop_fpu(struct kvm_vcpu *vcpu);
78698e91b84SJames Hogan void kvm_lose_fpu(struct kvm_vcpu *vcpu);
78798e91b84SJames Hogan
788740765ceSSanjay Lal /* TLB handling */
789c992a4f6SJames Hogan int kvm_mips_handle_vz_root_tlb_fault(unsigned long badvaddr,
790c992a4f6SJames Hogan struct kvm_vcpu *vcpu, bool write_fault);
791740765ceSSanjay Lal
792372582a6SJames Hogan int kvm_vz_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long entryhi);
793372582a6SJames Hogan int kvm_vz_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long gva,
794372582a6SJames Hogan unsigned long *gpa);
795372582a6SJames Hogan void kvm_vz_local_flush_roottlb_all_guests(void);
796372582a6SJames Hogan void kvm_vz_local_flush_guesttlb_all(void);
797372582a6SJames Hogan void kvm_vz_save_guesttlb(struct kvm_mips_tlb *buf, unsigned int index,
798372582a6SJames Hogan unsigned int count);
799372582a6SJames Hogan void kvm_vz_load_guesttlb(const struct kvm_mips_tlb *buf, unsigned int index,
800372582a6SJames Hogan unsigned int count);
8018a5097eeSHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
8028a5097eeSHuacai Chen void kvm_loongson_clear_guest_vtlb(void);
8038a5097eeSHuacai Chen void kvm_loongson_clear_guest_ftlb(void);
8048a5097eeSHuacai Chen #endif
805a7ebb2e4SJames Hogan
806a31b50d7SJames Hogan /* MMU handling */
807a31b50d7SJames Hogan
80806c158c9SJames Hogan bool kvm_mips_flush_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn);
809f0c0c330SJames Hogan int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn);
81006c158c9SJames Hogan pgd_t *kvm_pgd_alloc(void);
811aba85929SJames Hogan void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
812740765ceSSanjay Lal
813411740f5SJames Hogan #define KVM_ARCH_WANT_MMU_NOTIFIER
814411740f5SJames Hogan
815740765ceSSanjay Lal /* Emulation */
816bdb7ed86SJames Hogan enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause);
8176a97c775SJames Hogan int kvm_get_badinstr(u32 *opc, struct kvm_vcpu *vcpu, u32 *out);
8186a97c775SJames Hogan int kvm_get_badinstrp(u32 *opc, struct kvm_vcpu *vcpu, u32 *out);
819740765ceSSanjay Lal
820a1ecc54dSJames Hogan /**
821a1ecc54dSJames Hogan * kvm_is_ifetch_fault() - Find whether a TLBL exception is due to ifetch fault.
822a1ecc54dSJames Hogan * @vcpu: Virtual CPU.
823a1ecc54dSJames Hogan *
824a1ecc54dSJames Hogan * Returns: Whether the TLBL exception was likely due to an instruction
825a1ecc54dSJames Hogan * fetch fault rather than a data load fault.
826a1ecc54dSJames Hogan */
kvm_is_ifetch_fault(struct kvm_vcpu_arch * vcpu)827a1ecc54dSJames Hogan static inline bool kvm_is_ifetch_fault(struct kvm_vcpu_arch *vcpu)
828a1ecc54dSJames Hogan {
829a1ecc54dSJames Hogan unsigned long badvaddr = vcpu->host_cp0_badvaddr;
830a1ecc54dSJames Hogan unsigned long epc = msk_isa16_mode(vcpu->pc);
831a1ecc54dSJames Hogan u32 cause = vcpu->host_cp0_cause;
832a1ecc54dSJames Hogan
833a1ecc54dSJames Hogan if (epc == badvaddr)
834a1ecc54dSJames Hogan return true;
835a1ecc54dSJames Hogan
836a1ecc54dSJames Hogan /*
837a1ecc54dSJames Hogan * Branches may be 32-bit or 16-bit instructions.
838a1ecc54dSJames Hogan * This isn't exact, but we don't really support MIPS16 or microMIPS yet
839a1ecc54dSJames Hogan * in KVM anyway.
840a1ecc54dSJames Hogan */
841a1ecc54dSJames Hogan if ((cause & CAUSEF_BD) && badvaddr - epc <= 4)
842a1ecc54dSJames Hogan return true;
843a1ecc54dSJames Hogan
844a1ecc54dSJames Hogan return false;
845a1ecc54dSJames Hogan }
846a1ecc54dSJames Hogan
847c34b26b9STianjia Zhang extern enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu);
848740765ceSSanjay Lal
849bdb7ed86SJames Hogan u32 kvm_mips_read_count(struct kvm_vcpu *vcpu);
850bdb7ed86SJames Hogan void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count);
851bdb7ed86SJames Hogan void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack);
852a517c1adSJames Hogan void kvm_mips_init_count(struct kvm_vcpu *vcpu, unsigned long count_hz);
853f8239342SJames Hogan int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl);
854f8239342SJames Hogan int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume);
855f74a8e22SJames Hogan int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz);
856e30492bbSJames Hogan void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu);
857e30492bbSJames Hogan void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu);
858e30492bbSJames Hogan enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu);
859740765ceSSanjay Lal
860f4474d50SJames Hogan /* fairly internal functions requiring some care to use */
861f4474d50SJames Hogan int kvm_mips_count_disabled(struct kvm_vcpu *vcpu);
862f4474d50SJames Hogan ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count);
863f4474d50SJames Hogan int kvm_mips_restore_hrtimer(struct kvm_vcpu *vcpu, ktime_t before,
864f4474d50SJames Hogan u32 count, int min_drift);
865f4474d50SJames Hogan
866f4474d50SJames Hogan void kvm_vz_acquire_htimer(struct kvm_vcpu *vcpu);
867f4474d50SJames Hogan void kvm_vz_lose_htimer(struct kvm_vcpu *vcpu);
868f4474d50SJames Hogan
869258f3a2eSJames Hogan enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
870bdb7ed86SJames Hogan u32 cause,
871740765ceSSanjay Lal struct kvm_vcpu *vcpu);
872258f3a2eSJames Hogan enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
873bdb7ed86SJames Hogan u32 cause,
874740765ceSSanjay Lal struct kvm_vcpu *vcpu);
875740765ceSSanjay Lal
876c992a4f6SJames Hogan /* COP0 */
877c992a4f6SJames Hogan enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu);
878c992a4f6SJames Hogan
879955d8dc3SJames Hogan /* Hypercalls (hypcall.c) */
880955d8dc3SJames Hogan
881955d8dc3SJames Hogan enum emulation_result kvm_mips_emul_hypcall(struct kvm_vcpu *vcpu,
882955d8dc3SJames Hogan union mips_instruction inst);
883955d8dc3SJames Hogan int kvm_mips_handle_hypcall(struct kvm_vcpu *vcpu);
884955d8dc3SJames Hogan
885740765ceSSanjay Lal /* Misc */
886d98403a5SDeng-Cheng Zhu extern void kvm_mips_dump_stats(struct kvm_vcpu *vcpu);
887740765ceSSanjay Lal extern unsigned long kvm_mips_get_ramsize(struct kvm *kvm);
888f21db309SHuacai Chen extern int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
889f21db309SHuacai Chen struct kvm_mips_interrupt *irq);
890740765ceSSanjay Lal
kvm_arch_sync_events(struct kvm * kvm)8910865e636SRadim Krčmář static inline void kvm_arch_sync_events(struct kvm *kvm) {}
kvm_arch_free_memslot(struct kvm * kvm,struct kvm_memory_slot * slot)8920865e636SRadim Krčmář static inline void kvm_arch_free_memslot(struct kvm *kvm,
893e96c81eeSSean Christopherson struct kvm_memory_slot *slot) {}
kvm_arch_memslots_updated(struct kvm * kvm,u64 gen)89415248258SSean Christopherson static inline void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) {}
kvm_arch_sched_in(struct kvm_vcpu * vcpu,int cpu)8950865e636SRadim Krčmář static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
kvm_arch_vcpu_blocking(struct kvm_vcpu * vcpu)8963217f7c2SChristoffer Dall static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}
kvm_arch_vcpu_unblocking(struct kvm_vcpu * vcpu)8973217f7c2SChristoffer Dall static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {}
898740765ceSSanjay Lal
899*a1342c80SDavid Matlack #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS
900566a0beeSPaolo Bonzini
901740765ceSSanjay Lal #endif /* __MIPS_KVM_HOST_H__ */
902