xref: /openbmc/linux/arch/mips/kvm/vz.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1c992a4f6SJames Hogan /*
2c992a4f6SJames Hogan  * This file is subject to the terms and conditions of the GNU General Public
3c992a4f6SJames Hogan  * License.  See the file "COPYING" in the main directory of this archive
4c992a4f6SJames Hogan  * for more details.
5c992a4f6SJames Hogan  *
6c992a4f6SJames Hogan  * KVM/MIPS: Support for hardware virtualization extensions
7c992a4f6SJames Hogan  *
8c992a4f6SJames Hogan  * Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
9c992a4f6SJames Hogan  * Authors: Yann Le Du <ledu@kymasys.com>
10c992a4f6SJames Hogan  */
11c992a4f6SJames Hogan 
12c992a4f6SJames Hogan #include <linux/errno.h>
13c992a4f6SJames Hogan #include <linux/err.h>
14c992a4f6SJames Hogan #include <linux/module.h>
15c992a4f6SJames Hogan #include <linux/preempt.h>
16c992a4f6SJames Hogan #include <linux/vmalloc.h>
17c992a4f6SJames Hogan #include <asm/cacheflush.h>
18c992a4f6SJames Hogan #include <asm/cacheops.h>
19c992a4f6SJames Hogan #include <asm/cmpxchg.h>
20c992a4f6SJames Hogan #include <asm/fpu.h>
21c992a4f6SJames Hogan #include <asm/hazards.h>
22c992a4f6SJames Hogan #include <asm/inst.h>
23c992a4f6SJames Hogan #include <asm/mmu_context.h>
24c992a4f6SJames Hogan #include <asm/r4kcache.h>
25c992a4f6SJames Hogan #include <asm/time.h>
26c992a4f6SJames Hogan #include <asm/tlb.h>
27c992a4f6SJames Hogan #include <asm/tlbex.h>
28c992a4f6SJames Hogan 
29c992a4f6SJames Hogan #include <linux/kvm_host.h>
30c992a4f6SJames Hogan 
31c992a4f6SJames Hogan #include "interrupt.h"
32cf99c505SHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
337f2a83f1SHuacai Chen #include "loongson_regs.h"
34cf99c505SHuacai Chen #endif
35c992a4f6SJames Hogan 
36c992a4f6SJames Hogan #include "trace.h"
37c992a4f6SJames Hogan 
38c992a4f6SJames Hogan /* Pointers to last VCPU loaded on each physical CPU */
39c992a4f6SJames Hogan static struct kvm_vcpu *last_vcpu[NR_CPUS];
40c992a4f6SJames Hogan /* Pointers to last VCPU executed on each physical CPU */
41c992a4f6SJames Hogan static struct kvm_vcpu *last_exec_vcpu[NR_CPUS];
42c992a4f6SJames Hogan 
43c992a4f6SJames Hogan /*
44c992a4f6SJames Hogan  * Number of guest VTLB entries to use, so we can catch inconsistency between
45c992a4f6SJames Hogan  * CPUs.
46c992a4f6SJames Hogan  */
47c992a4f6SJames Hogan static unsigned int kvm_vz_guest_vtlb_size;
48c992a4f6SJames Hogan 
kvm_vz_read_gc0_ebase(void)49c992a4f6SJames Hogan static inline long kvm_vz_read_gc0_ebase(void)
50c992a4f6SJames Hogan {
51c992a4f6SJames Hogan 	if (sizeof(long) == 8 && cpu_has_ebase_wg)
52c992a4f6SJames Hogan 		return read_gc0_ebase_64();
53c992a4f6SJames Hogan 	else
54c992a4f6SJames Hogan 		return read_gc0_ebase();
55c992a4f6SJames Hogan }
56c992a4f6SJames Hogan 
kvm_vz_write_gc0_ebase(long v)57c992a4f6SJames Hogan static inline void kvm_vz_write_gc0_ebase(long v)
58c992a4f6SJames Hogan {
59c992a4f6SJames Hogan 	/*
60c992a4f6SJames Hogan 	 * First write with WG=1 to write upper bits, then write again in case
61c992a4f6SJames Hogan 	 * WG should be left at 0.
62c992a4f6SJames Hogan 	 * write_gc0_ebase_64() is no longer UNDEFINED since R6.
63c992a4f6SJames Hogan 	 */
64c992a4f6SJames Hogan 	if (sizeof(long) == 8 &&
65c992a4f6SJames Hogan 	    (cpu_has_mips64r6 || cpu_has_ebase_wg)) {
66c992a4f6SJames Hogan 		write_gc0_ebase_64(v | MIPS_EBASE_WG);
67c992a4f6SJames Hogan 		write_gc0_ebase_64(v);
68c992a4f6SJames Hogan 	} else {
69c992a4f6SJames Hogan 		write_gc0_ebase(v | MIPS_EBASE_WG);
70c992a4f6SJames Hogan 		write_gc0_ebase(v);
71c992a4f6SJames Hogan 	}
72c992a4f6SJames Hogan }
73c992a4f6SJames Hogan 
74c992a4f6SJames Hogan /*
75c992a4f6SJames Hogan  * These Config bits may be writable by the guest:
76c992a4f6SJames Hogan  * Config:	[K23, KU] (!TLB), K0
77c992a4f6SJames Hogan  * Config1:	(none)
78c992a4f6SJames Hogan  * Config2:	[TU, SU] (impl)
79c992a4f6SJames Hogan  * Config3:	ISAOnExc
80c992a4f6SJames Hogan  * Config4:	FTLBPageSize
81c992a4f6SJames Hogan  * Config5:	K, CV, MSAEn, UFE, FRE, SBRI, UFR
82c992a4f6SJames Hogan  */
83c992a4f6SJames Hogan 
kvm_vz_config_guest_wrmask(struct kvm_vcpu * vcpu)84c992a4f6SJames Hogan static inline unsigned int kvm_vz_config_guest_wrmask(struct kvm_vcpu *vcpu)
85c992a4f6SJames Hogan {
86c992a4f6SJames Hogan 	return CONF_CM_CMASK;
87c992a4f6SJames Hogan }
88c992a4f6SJames Hogan 
kvm_vz_config1_guest_wrmask(struct kvm_vcpu * vcpu)89c992a4f6SJames Hogan static inline unsigned int kvm_vz_config1_guest_wrmask(struct kvm_vcpu *vcpu)
90c992a4f6SJames Hogan {
91c992a4f6SJames Hogan 	return 0;
92c992a4f6SJames Hogan }
93c992a4f6SJames Hogan 
kvm_vz_config2_guest_wrmask(struct kvm_vcpu * vcpu)94c992a4f6SJames Hogan static inline unsigned int kvm_vz_config2_guest_wrmask(struct kvm_vcpu *vcpu)
95c992a4f6SJames Hogan {
96c992a4f6SJames Hogan 	return 0;
97c992a4f6SJames Hogan }
98c992a4f6SJames Hogan 
kvm_vz_config3_guest_wrmask(struct kvm_vcpu * vcpu)99c992a4f6SJames Hogan static inline unsigned int kvm_vz_config3_guest_wrmask(struct kvm_vcpu *vcpu)
100c992a4f6SJames Hogan {
101c992a4f6SJames Hogan 	return MIPS_CONF3_ISA_OE;
102c992a4f6SJames Hogan }
103c992a4f6SJames Hogan 
kvm_vz_config4_guest_wrmask(struct kvm_vcpu * vcpu)104c992a4f6SJames Hogan static inline unsigned int kvm_vz_config4_guest_wrmask(struct kvm_vcpu *vcpu)
105c992a4f6SJames Hogan {
106c992a4f6SJames Hogan 	/* no need to be exact */
107c992a4f6SJames Hogan 	return MIPS_CONF4_VFTLBPAGESIZE;
108c992a4f6SJames Hogan }
109c992a4f6SJames Hogan 
kvm_vz_config5_guest_wrmask(struct kvm_vcpu * vcpu)110c992a4f6SJames Hogan static inline unsigned int kvm_vz_config5_guest_wrmask(struct kvm_vcpu *vcpu)
111c992a4f6SJames Hogan {
112c992a4f6SJames Hogan 	unsigned int mask = MIPS_CONF5_K | MIPS_CONF5_CV | MIPS_CONF5_SBRI;
113c992a4f6SJames Hogan 
114c992a4f6SJames Hogan 	/* Permit MSAEn changes if MSA supported and enabled */
115c992a4f6SJames Hogan 	if (kvm_mips_guest_has_msa(&vcpu->arch))
116c992a4f6SJames Hogan 		mask |= MIPS_CONF5_MSAEN;
117c992a4f6SJames Hogan 
118c992a4f6SJames Hogan 	/*
119c992a4f6SJames Hogan 	 * Permit guest FPU mode changes if FPU is enabled and the relevant
120c992a4f6SJames Hogan 	 * feature exists according to FIR register.
121c992a4f6SJames Hogan 	 */
122c992a4f6SJames Hogan 	if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
123c992a4f6SJames Hogan 		if (cpu_has_ufr)
124c992a4f6SJames Hogan 			mask |= MIPS_CONF5_UFR;
125c992a4f6SJames Hogan 		if (cpu_has_fre)
126c992a4f6SJames Hogan 			mask |= MIPS_CONF5_FRE | MIPS_CONF5_UFE;
127c992a4f6SJames Hogan 	}
128c992a4f6SJames Hogan 
129c992a4f6SJames Hogan 	return mask;
130c992a4f6SJames Hogan }
131c992a4f6SJames Hogan 
kvm_vz_config6_guest_wrmask(struct kvm_vcpu * vcpu)1328a5097eeSHuacai Chen static inline unsigned int kvm_vz_config6_guest_wrmask(struct kvm_vcpu *vcpu)
1338a5097eeSHuacai Chen {
13404ef32afSHuacai Chen 	return LOONGSON_CONF6_INTIMER | LOONGSON_CONF6_EXTIMER;
1358a5097eeSHuacai Chen }
1368a5097eeSHuacai Chen 
137c992a4f6SJames Hogan /*
138c992a4f6SJames Hogan  * VZ optionally allows these additional Config bits to be written by root:
139c992a4f6SJames Hogan  * Config:	M, [MT]
140c992a4f6SJames Hogan  * Config1:	M, [MMUSize-1, C2, MD, PC, WR, CA], FP
141c992a4f6SJames Hogan  * Config2:	M
142dffe042fSJames Hogan  * Config3:	M, MSAP, [BPG], ULRI, [DSP2P, DSPP], CTXTC, [ITL, LPA, VEIC,
143c992a4f6SJames Hogan  *		VInt, SP, CDMM, MT, SM, TL]
144c992a4f6SJames Hogan  * Config4:	M, [VTLBSizeExt, MMUSizeExt]
145d42a008fSJames Hogan  * Config5:	MRP
146c992a4f6SJames Hogan  */
147c992a4f6SJames Hogan 
kvm_vz_config_user_wrmask(struct kvm_vcpu * vcpu)148c992a4f6SJames Hogan static inline unsigned int kvm_vz_config_user_wrmask(struct kvm_vcpu *vcpu)
149c992a4f6SJames Hogan {
150c992a4f6SJames Hogan 	return kvm_vz_config_guest_wrmask(vcpu) | MIPS_CONF_M;
151c992a4f6SJames Hogan }
152c992a4f6SJames Hogan 
kvm_vz_config1_user_wrmask(struct kvm_vcpu * vcpu)153c992a4f6SJames Hogan static inline unsigned int kvm_vz_config1_user_wrmask(struct kvm_vcpu *vcpu)
154c992a4f6SJames Hogan {
155c992a4f6SJames Hogan 	unsigned int mask = kvm_vz_config1_guest_wrmask(vcpu) | MIPS_CONF_M;
156c992a4f6SJames Hogan 
157c992a4f6SJames Hogan 	/* Permit FPU to be present if FPU is supported */
158c992a4f6SJames Hogan 	if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
159c992a4f6SJames Hogan 		mask |= MIPS_CONF1_FP;
160c992a4f6SJames Hogan 
161c992a4f6SJames Hogan 	return mask;
162c992a4f6SJames Hogan }
163c992a4f6SJames Hogan 
kvm_vz_config2_user_wrmask(struct kvm_vcpu * vcpu)164c992a4f6SJames Hogan static inline unsigned int kvm_vz_config2_user_wrmask(struct kvm_vcpu *vcpu)
165c992a4f6SJames Hogan {
166c992a4f6SJames Hogan 	return kvm_vz_config2_guest_wrmask(vcpu) | MIPS_CONF_M;
167c992a4f6SJames Hogan }
168c992a4f6SJames Hogan 
kvm_vz_config3_user_wrmask(struct kvm_vcpu * vcpu)169c992a4f6SJames Hogan static inline unsigned int kvm_vz_config3_user_wrmask(struct kvm_vcpu *vcpu)
170c992a4f6SJames Hogan {
171c992a4f6SJames Hogan 	unsigned int mask = kvm_vz_config3_guest_wrmask(vcpu) | MIPS_CONF_M |
172dffe042fSJames Hogan 		MIPS_CONF3_ULRI | MIPS_CONF3_CTXTC;
173c992a4f6SJames Hogan 
174c992a4f6SJames Hogan 	/* Permit MSA to be present if MSA is supported */
175c992a4f6SJames Hogan 	if (kvm_mips_guest_can_have_msa(&vcpu->arch))
176c992a4f6SJames Hogan 		mask |= MIPS_CONF3_MSA;
177c992a4f6SJames Hogan 
178c992a4f6SJames Hogan 	return mask;
179c992a4f6SJames Hogan }
180c992a4f6SJames Hogan 
kvm_vz_config4_user_wrmask(struct kvm_vcpu * vcpu)181c992a4f6SJames Hogan static inline unsigned int kvm_vz_config4_user_wrmask(struct kvm_vcpu *vcpu)
182c992a4f6SJames Hogan {
183c992a4f6SJames Hogan 	return kvm_vz_config4_guest_wrmask(vcpu) | MIPS_CONF_M;
184c992a4f6SJames Hogan }
185c992a4f6SJames Hogan 
kvm_vz_config5_user_wrmask(struct kvm_vcpu * vcpu)186c992a4f6SJames Hogan static inline unsigned int kvm_vz_config5_user_wrmask(struct kvm_vcpu *vcpu)
187c992a4f6SJames Hogan {
188d42a008fSJames Hogan 	return kvm_vz_config5_guest_wrmask(vcpu) | MIPS_CONF5_MRP;
189c992a4f6SJames Hogan }
190c992a4f6SJames Hogan 
kvm_vz_config6_user_wrmask(struct kvm_vcpu * vcpu)1918a5097eeSHuacai Chen static inline unsigned int kvm_vz_config6_user_wrmask(struct kvm_vcpu *vcpu)
1928a5097eeSHuacai Chen {
1938a5097eeSHuacai Chen 	return kvm_vz_config6_guest_wrmask(vcpu) |
19404ef32afSHuacai Chen 		LOONGSON_CONF6_SFBEN | LOONGSON_CONF6_FTLBDIS;
1958a5097eeSHuacai Chen }
1968a5097eeSHuacai Chen 
kvm_vz_gva_to_gpa_cb(gva_t gva)197c992a4f6SJames Hogan static gpa_t kvm_vz_gva_to_gpa_cb(gva_t gva)
198c992a4f6SJames Hogan {
199c992a4f6SJames Hogan 	/* VZ guest has already converted gva to gpa */
200c992a4f6SJames Hogan 	return gva;
201c992a4f6SJames Hogan }
202c992a4f6SJames Hogan 
kvm_vz_queue_irq(struct kvm_vcpu * vcpu,unsigned int priority)203c992a4f6SJames Hogan static void kvm_vz_queue_irq(struct kvm_vcpu *vcpu, unsigned int priority)
204c992a4f6SJames Hogan {
205c992a4f6SJames Hogan 	set_bit(priority, &vcpu->arch.pending_exceptions);
206c992a4f6SJames Hogan 	clear_bit(priority, &vcpu->arch.pending_exceptions_clr);
207c992a4f6SJames Hogan }
208c992a4f6SJames Hogan 
kvm_vz_dequeue_irq(struct kvm_vcpu * vcpu,unsigned int priority)209c992a4f6SJames Hogan static void kvm_vz_dequeue_irq(struct kvm_vcpu *vcpu, unsigned int priority)
210c992a4f6SJames Hogan {
211c992a4f6SJames Hogan 	clear_bit(priority, &vcpu->arch.pending_exceptions);
212c992a4f6SJames Hogan 	set_bit(priority, &vcpu->arch.pending_exceptions_clr);
213c992a4f6SJames Hogan }
214c992a4f6SJames Hogan 
kvm_vz_queue_timer_int_cb(struct kvm_vcpu * vcpu)215c992a4f6SJames Hogan static void kvm_vz_queue_timer_int_cb(struct kvm_vcpu *vcpu)
216c992a4f6SJames Hogan {
217c992a4f6SJames Hogan 	/*
218c992a4f6SJames Hogan 	 * timer expiry is asynchronous to vcpu execution therefore defer guest
219c992a4f6SJames Hogan 	 * cp0 accesses
220c992a4f6SJames Hogan 	 */
221c992a4f6SJames Hogan 	kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
222c992a4f6SJames Hogan }
223c992a4f6SJames Hogan 
kvm_vz_dequeue_timer_int_cb(struct kvm_vcpu * vcpu)224c992a4f6SJames Hogan static void kvm_vz_dequeue_timer_int_cb(struct kvm_vcpu *vcpu)
225c992a4f6SJames Hogan {
226c992a4f6SJames Hogan 	/*
227c992a4f6SJames Hogan 	 * timer expiry is asynchronous to vcpu execution therefore defer guest
228c992a4f6SJames Hogan 	 * cp0 accesses
229c992a4f6SJames Hogan 	 */
230c992a4f6SJames Hogan 	kvm_vz_dequeue_irq(vcpu, MIPS_EXC_INT_TIMER);
231c992a4f6SJames Hogan }
232c992a4f6SJames Hogan 
kvm_vz_queue_io_int_cb(struct kvm_vcpu * vcpu,struct kvm_mips_interrupt * irq)233c992a4f6SJames Hogan static void kvm_vz_queue_io_int_cb(struct kvm_vcpu *vcpu,
234c992a4f6SJames Hogan 				   struct kvm_mips_interrupt *irq)
235c992a4f6SJames Hogan {
236c992a4f6SJames Hogan 	int intr = (int)irq->irq;
237c992a4f6SJames Hogan 
238c992a4f6SJames Hogan 	/*
239c992a4f6SJames Hogan 	 * interrupts are asynchronous to vcpu execution therefore defer guest
240c992a4f6SJames Hogan 	 * cp0 accesses
241c992a4f6SJames Hogan 	 */
2423f51d8fcSHuacai Chen 	kvm_vz_queue_irq(vcpu, kvm_irq_to_priority(intr));
243c992a4f6SJames Hogan }
244c992a4f6SJames Hogan 
kvm_vz_dequeue_io_int_cb(struct kvm_vcpu * vcpu,struct kvm_mips_interrupt * irq)245c992a4f6SJames Hogan static void kvm_vz_dequeue_io_int_cb(struct kvm_vcpu *vcpu,
246c992a4f6SJames Hogan 				     struct kvm_mips_interrupt *irq)
247c992a4f6SJames Hogan {
248c992a4f6SJames Hogan 	int intr = (int)irq->irq;
249c992a4f6SJames Hogan 
250c992a4f6SJames Hogan 	/*
251c992a4f6SJames Hogan 	 * interrupts are asynchronous to vcpu execution therefore defer guest
252c992a4f6SJames Hogan 	 * cp0 accesses
253c992a4f6SJames Hogan 	 */
2543f51d8fcSHuacai Chen 	kvm_vz_dequeue_irq(vcpu, kvm_irq_to_priority(-intr));
255c992a4f6SJames Hogan }
256c992a4f6SJames Hogan 
kvm_vz_irq_deliver_cb(struct kvm_vcpu * vcpu,unsigned int priority,u32 cause)257c992a4f6SJames Hogan static int kvm_vz_irq_deliver_cb(struct kvm_vcpu *vcpu, unsigned int priority,
258c992a4f6SJames Hogan 				 u32 cause)
259c992a4f6SJames Hogan {
260c992a4f6SJames Hogan 	u32 irq = (priority < MIPS_EXC_MAX) ?
2613f51d8fcSHuacai Chen 		kvm_priority_to_irq[priority] : 0;
262c992a4f6SJames Hogan 
263c992a4f6SJames Hogan 	switch (priority) {
264c992a4f6SJames Hogan 	case MIPS_EXC_INT_TIMER:
265c992a4f6SJames Hogan 		set_gc0_cause(C_TI);
266c992a4f6SJames Hogan 		break;
267c992a4f6SJames Hogan 
2683f51d8fcSHuacai Chen 	case MIPS_EXC_INT_IO_1:
2693f51d8fcSHuacai Chen 	case MIPS_EXC_INT_IO_2:
270c992a4f6SJames Hogan 	case MIPS_EXC_INT_IPI_1:
271c992a4f6SJames Hogan 	case MIPS_EXC_INT_IPI_2:
272c992a4f6SJames Hogan 		if (cpu_has_guestctl2)
273c992a4f6SJames Hogan 			set_c0_guestctl2(irq);
274c992a4f6SJames Hogan 		else
275c992a4f6SJames Hogan 			set_gc0_cause(irq);
276c992a4f6SJames Hogan 		break;
277c992a4f6SJames Hogan 
278c992a4f6SJames Hogan 	default:
279c992a4f6SJames Hogan 		break;
280c992a4f6SJames Hogan 	}
281c992a4f6SJames Hogan 
282c992a4f6SJames Hogan 	clear_bit(priority, &vcpu->arch.pending_exceptions);
283c992a4f6SJames Hogan 	return 1;
284c992a4f6SJames Hogan }
285c992a4f6SJames Hogan 
kvm_vz_irq_clear_cb(struct kvm_vcpu * vcpu,unsigned int priority,u32 cause)286c992a4f6SJames Hogan static int kvm_vz_irq_clear_cb(struct kvm_vcpu *vcpu, unsigned int priority,
287c992a4f6SJames Hogan 			       u32 cause)
288c992a4f6SJames Hogan {
289c992a4f6SJames Hogan 	u32 irq = (priority < MIPS_EXC_MAX) ?
2903f51d8fcSHuacai Chen 		kvm_priority_to_irq[priority] : 0;
291c992a4f6SJames Hogan 
292c992a4f6SJames Hogan 	switch (priority) {
293c992a4f6SJames Hogan 	case MIPS_EXC_INT_TIMER:
294c992a4f6SJames Hogan 		/*
29545c7e8afSThomas Bogendoerfer 		 * Explicitly clear irq associated with Cause.IP[IPTI]
29645c7e8afSThomas Bogendoerfer 		 * if GuestCtl2 virtual interrupt register not
297c992a4f6SJames Hogan 		 * supported or if not using GuestCtl2 Hardware Clear.
298c992a4f6SJames Hogan 		 */
299c992a4f6SJames Hogan 		if (cpu_has_guestctl2) {
300c992a4f6SJames Hogan 			if (!(read_c0_guestctl2() & (irq << 14)))
301c992a4f6SJames Hogan 				clear_c0_guestctl2(irq);
302c992a4f6SJames Hogan 		} else {
303c992a4f6SJames Hogan 			clear_gc0_cause(irq);
304c992a4f6SJames Hogan 		}
305c992a4f6SJames Hogan 		break;
306c992a4f6SJames Hogan 
3073f51d8fcSHuacai Chen 	case MIPS_EXC_INT_IO_1:
3083f51d8fcSHuacai Chen 	case MIPS_EXC_INT_IO_2:
309c992a4f6SJames Hogan 	case MIPS_EXC_INT_IPI_1:
310c992a4f6SJames Hogan 	case MIPS_EXC_INT_IPI_2:
311c992a4f6SJames Hogan 		/* Clear GuestCtl2.VIP irq if not using Hardware Clear */
312c992a4f6SJames Hogan 		if (cpu_has_guestctl2) {
313c992a4f6SJames Hogan 			if (!(read_c0_guestctl2() & (irq << 14)))
314c992a4f6SJames Hogan 				clear_c0_guestctl2(irq);
315c992a4f6SJames Hogan 		} else {
316c992a4f6SJames Hogan 			clear_gc0_cause(irq);
317c992a4f6SJames Hogan 		}
318c992a4f6SJames Hogan 		break;
319c992a4f6SJames Hogan 
320c992a4f6SJames Hogan 	default:
321c992a4f6SJames Hogan 		break;
322c992a4f6SJames Hogan 	}
323c992a4f6SJames Hogan 
324c992a4f6SJames Hogan 	clear_bit(priority, &vcpu->arch.pending_exceptions_clr);
325c992a4f6SJames Hogan 	return 1;
326c992a4f6SJames Hogan }
327c992a4f6SJames Hogan 
328c992a4f6SJames Hogan /*
329c992a4f6SJames Hogan  * VZ guest timer handling.
330c992a4f6SJames Hogan  */
331c992a4f6SJames Hogan 
332c992a4f6SJames Hogan /**
333f4474d50SJames Hogan  * kvm_vz_should_use_htimer() - Find whether to use the VZ hard guest timer.
334f4474d50SJames Hogan  * @vcpu:	Virtual CPU.
335f4474d50SJames Hogan  *
336f4474d50SJames Hogan  * Returns:	true if the VZ GTOffset & real guest CP0_Count should be used
337f4474d50SJames Hogan  *		instead of software emulation of guest timer.
338f4474d50SJames Hogan  *		false otherwise.
339f4474d50SJames Hogan  */
kvm_vz_should_use_htimer(struct kvm_vcpu * vcpu)340f4474d50SJames Hogan static bool kvm_vz_should_use_htimer(struct kvm_vcpu *vcpu)
341f4474d50SJames Hogan {
342f4474d50SJames Hogan 	if (kvm_mips_count_disabled(vcpu))
343f4474d50SJames Hogan 		return false;
344f4474d50SJames Hogan 
345f4474d50SJames Hogan 	/* Chosen frequency must match real frequency */
346f4474d50SJames Hogan 	if (mips_hpt_frequency != vcpu->arch.count_hz)
347f4474d50SJames Hogan 		return false;
348f4474d50SJames Hogan 
349f4474d50SJames Hogan 	/* We don't support a CP0_GTOffset with fewer bits than CP0_Count */
350f4474d50SJames Hogan 	if (current_cpu_data.gtoffset_mask != 0xffffffff)
351f4474d50SJames Hogan 		return false;
352f4474d50SJames Hogan 
353f4474d50SJames Hogan 	return true;
354f4474d50SJames Hogan }
355f4474d50SJames Hogan 
356f4474d50SJames Hogan /**
357c992a4f6SJames Hogan  * _kvm_vz_restore_stimer() - Restore soft timer state.
358c992a4f6SJames Hogan  * @vcpu:	Virtual CPU.
359c992a4f6SJames Hogan  * @compare:	CP0_Compare register value, restored by caller.
360c992a4f6SJames Hogan  * @cause:	CP0_Cause register to restore.
361c992a4f6SJames Hogan  *
362f4474d50SJames Hogan  * Restore VZ state relating to the soft timer. The hard timer can be enabled
363f4474d50SJames Hogan  * later.
364c992a4f6SJames Hogan  */
_kvm_vz_restore_stimer(struct kvm_vcpu * vcpu,u32 compare,u32 cause)365c992a4f6SJames Hogan static void _kvm_vz_restore_stimer(struct kvm_vcpu *vcpu, u32 compare,
366c992a4f6SJames Hogan 				   u32 cause)
367c992a4f6SJames Hogan {
368c992a4f6SJames Hogan 	/*
369c992a4f6SJames Hogan 	 * Avoid spurious counter interrupts by setting Guest CP0_Count to just
370c992a4f6SJames Hogan 	 * after Guest CP0_Compare.
371c992a4f6SJames Hogan 	 */
372c992a4f6SJames Hogan 	write_c0_gtoffset(compare - read_c0_count());
373c992a4f6SJames Hogan 
374c992a4f6SJames Hogan 	back_to_back_c0_hazard();
375c992a4f6SJames Hogan 	write_gc0_cause(cause);
376c992a4f6SJames Hogan }
377c992a4f6SJames Hogan 
378c992a4f6SJames Hogan /**
379f4474d50SJames Hogan  * _kvm_vz_restore_htimer() - Restore hard timer state.
380f4474d50SJames Hogan  * @vcpu:	Virtual CPU.
381f4474d50SJames Hogan  * @compare:	CP0_Compare register value, restored by caller.
382f4474d50SJames Hogan  * @cause:	CP0_Cause register to restore.
383f4474d50SJames Hogan  *
384f4474d50SJames Hogan  * Restore hard timer Guest.Count & Guest.Cause taking care to preserve the
385f4474d50SJames Hogan  * value of Guest.CP0_Cause.TI while restoring Guest.CP0_Cause.
386f4474d50SJames Hogan  */
_kvm_vz_restore_htimer(struct kvm_vcpu * vcpu,u32 compare,u32 cause)387f4474d50SJames Hogan static void _kvm_vz_restore_htimer(struct kvm_vcpu *vcpu,
388f4474d50SJames Hogan 				   u32 compare, u32 cause)
389f4474d50SJames Hogan {
390f4474d50SJames Hogan 	u32 start_count, after_count;
391f4474d50SJames Hogan 	unsigned long flags;
392f4474d50SJames Hogan 
393f4474d50SJames Hogan 	/*
394f4474d50SJames Hogan 	 * Freeze the soft-timer and sync the guest CP0_Count with it. We do
395f4474d50SJames Hogan 	 * this with interrupts disabled to avoid latency.
396f4474d50SJames Hogan 	 */
397f4474d50SJames Hogan 	local_irq_save(flags);
398a3cf527eSHuacai Chen 	kvm_mips_freeze_hrtimer(vcpu, &start_count);
399f4474d50SJames Hogan 	write_c0_gtoffset(start_count - read_c0_count());
400f4474d50SJames Hogan 	local_irq_restore(flags);
401f4474d50SJames Hogan 
402f4474d50SJames Hogan 	/* restore guest CP0_Cause, as TI may already be set */
403f4474d50SJames Hogan 	back_to_back_c0_hazard();
404f4474d50SJames Hogan 	write_gc0_cause(cause);
405f4474d50SJames Hogan 
406f4474d50SJames Hogan 	/*
407f4474d50SJames Hogan 	 * The above sequence isn't atomic and would result in lost timer
408f4474d50SJames Hogan 	 * interrupts if we're not careful. Detect if a timer interrupt is due
409f4474d50SJames Hogan 	 * and assert it.
410f4474d50SJames Hogan 	 */
411f4474d50SJames Hogan 	back_to_back_c0_hazard();
412f4474d50SJames Hogan 	after_count = read_gc0_count();
413f4474d50SJames Hogan 	if (after_count - start_count > compare - start_count - 1)
414f4474d50SJames Hogan 		kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
415f4474d50SJames Hogan }
416f4474d50SJames Hogan 
417f4474d50SJames Hogan /**
418f4474d50SJames Hogan  * kvm_vz_restore_timer() - Restore timer state.
419c992a4f6SJames Hogan  * @vcpu:	Virtual CPU.
420c992a4f6SJames Hogan  *
421c992a4f6SJames Hogan  * Restore soft timer state from saved context.
422c992a4f6SJames Hogan  */
kvm_vz_restore_timer(struct kvm_vcpu * vcpu)423c992a4f6SJames Hogan static void kvm_vz_restore_timer(struct kvm_vcpu *vcpu)
424c992a4f6SJames Hogan {
425c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
426c992a4f6SJames Hogan 	u32 cause, compare;
427c992a4f6SJames Hogan 
428c992a4f6SJames Hogan 	compare = kvm_read_sw_gc0_compare(cop0);
429c992a4f6SJames Hogan 	cause = kvm_read_sw_gc0_cause(cop0);
430c992a4f6SJames Hogan 
431c992a4f6SJames Hogan 	write_gc0_compare(compare);
432c992a4f6SJames Hogan 	_kvm_vz_restore_stimer(vcpu, compare, cause);
433c992a4f6SJames Hogan }
434c992a4f6SJames Hogan 
435c992a4f6SJames Hogan /**
436f4474d50SJames Hogan  * kvm_vz_acquire_htimer() - Switch to hard timer state.
437f4474d50SJames Hogan  * @vcpu:	Virtual CPU.
438f4474d50SJames Hogan  *
439f4474d50SJames Hogan  * Restore hard timer state on top of existing soft timer state if possible.
440f4474d50SJames Hogan  *
441f4474d50SJames Hogan  * Since hard timer won't remain active over preemption, preemption should be
442f4474d50SJames Hogan  * disabled by the caller.
443f4474d50SJames Hogan  */
kvm_vz_acquire_htimer(struct kvm_vcpu * vcpu)444f4474d50SJames Hogan void kvm_vz_acquire_htimer(struct kvm_vcpu *vcpu)
445f4474d50SJames Hogan {
446f4474d50SJames Hogan 	u32 gctl0;
447f4474d50SJames Hogan 
448f4474d50SJames Hogan 	gctl0 = read_c0_guestctl0();
449f4474d50SJames Hogan 	if (!(gctl0 & MIPS_GCTL0_GT) && kvm_vz_should_use_htimer(vcpu)) {
450f4474d50SJames Hogan 		/* enable guest access to hard timer */
451f4474d50SJames Hogan 		write_c0_guestctl0(gctl0 | MIPS_GCTL0_GT);
452f4474d50SJames Hogan 
453f4474d50SJames Hogan 		_kvm_vz_restore_htimer(vcpu, read_gc0_compare(),
454f4474d50SJames Hogan 				       read_gc0_cause());
455f4474d50SJames Hogan 	}
456f4474d50SJames Hogan }
457f4474d50SJames Hogan 
458f4474d50SJames Hogan /**
459f4474d50SJames Hogan  * _kvm_vz_save_htimer() - Switch to software emulation of guest timer.
460f4474d50SJames Hogan  * @vcpu:	Virtual CPU.
4612161ba07SRandy Dunlap  * @out_compare: Pointer to write compare value to.
4622161ba07SRandy Dunlap  * @out_cause:	Pointer to write cause value to.
463f4474d50SJames Hogan  *
464f4474d50SJames Hogan  * Save VZ guest timer state and switch to software emulation of guest CP0
465f4474d50SJames Hogan  * timer. The hard timer must already be in use, so preemption should be
466f4474d50SJames Hogan  * disabled.
467f4474d50SJames Hogan  */
_kvm_vz_save_htimer(struct kvm_vcpu * vcpu,u32 * out_compare,u32 * out_cause)468f4474d50SJames Hogan static void _kvm_vz_save_htimer(struct kvm_vcpu *vcpu,
469f4474d50SJames Hogan 				u32 *out_compare, u32 *out_cause)
470f4474d50SJames Hogan {
471f4474d50SJames Hogan 	u32 cause, compare, before_count, end_count;
472f4474d50SJames Hogan 	ktime_t before_time;
473f4474d50SJames Hogan 
474f4474d50SJames Hogan 	compare = read_gc0_compare();
475f4474d50SJames Hogan 	*out_compare = compare;
476f4474d50SJames Hogan 
477f4474d50SJames Hogan 	before_time = ktime_get();
478f4474d50SJames Hogan 
479f4474d50SJames Hogan 	/*
480f4474d50SJames Hogan 	 * Record the CP0_Count *prior* to saving CP0_Cause, so we have a time
481f4474d50SJames Hogan 	 * at which no pending timer interrupt is missing.
482f4474d50SJames Hogan 	 */
483f4474d50SJames Hogan 	before_count = read_gc0_count();
484f4474d50SJames Hogan 	back_to_back_c0_hazard();
485f4474d50SJames Hogan 	cause = read_gc0_cause();
486f4474d50SJames Hogan 	*out_cause = cause;
487f4474d50SJames Hogan 
488f4474d50SJames Hogan 	/*
489f4474d50SJames Hogan 	 * Record a final CP0_Count which we will transfer to the soft-timer.
490f4474d50SJames Hogan 	 * This is recorded *after* saving CP0_Cause, so we don't get any timer
491f4474d50SJames Hogan 	 * interrupts from just after the final CP0_Count point.
492f4474d50SJames Hogan 	 */
493f4474d50SJames Hogan 	back_to_back_c0_hazard();
494f4474d50SJames Hogan 	end_count = read_gc0_count();
495f4474d50SJames Hogan 
496f4474d50SJames Hogan 	/*
497f4474d50SJames Hogan 	 * The above sequence isn't atomic, so we could miss a timer interrupt
498f4474d50SJames Hogan 	 * between reading CP0_Cause and end_count. Detect and record any timer
499f4474d50SJames Hogan 	 * interrupt due between before_count and end_count.
500f4474d50SJames Hogan 	 */
501f4474d50SJames Hogan 	if (end_count - before_count > compare - before_count - 1)
502f4474d50SJames Hogan 		kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
503f4474d50SJames Hogan 
504f4474d50SJames Hogan 	/*
505f4474d50SJames Hogan 	 * Restore soft-timer, ignoring a small amount of negative drift due to
506f4474d50SJames Hogan 	 * delay between freeze_hrtimer and setting CP0_GTOffset.
507f4474d50SJames Hogan 	 */
508f4474d50SJames Hogan 	kvm_mips_restore_hrtimer(vcpu, before_time, end_count, -0x10000);
509f4474d50SJames Hogan }
510f4474d50SJames Hogan 
511f4474d50SJames Hogan /**
512c992a4f6SJames Hogan  * kvm_vz_save_timer() - Save guest timer state.
513c992a4f6SJames Hogan  * @vcpu:	Virtual CPU.
514c992a4f6SJames Hogan  *
515f4474d50SJames Hogan  * Save VZ guest timer state and switch to soft guest timer if hard timer was in
516f4474d50SJames Hogan  * use.
517c992a4f6SJames Hogan  */
kvm_vz_save_timer(struct kvm_vcpu * vcpu)518c992a4f6SJames Hogan static void kvm_vz_save_timer(struct kvm_vcpu *vcpu)
519c992a4f6SJames Hogan {
520c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
521f4474d50SJames Hogan 	u32 gctl0, compare, cause;
522c992a4f6SJames Hogan 
523f4474d50SJames Hogan 	gctl0 = read_c0_guestctl0();
524f4474d50SJames Hogan 	if (gctl0 & MIPS_GCTL0_GT) {
525f4474d50SJames Hogan 		/* disable guest use of hard timer */
526f4474d50SJames Hogan 		write_c0_guestctl0(gctl0 & ~MIPS_GCTL0_GT);
527f4474d50SJames Hogan 
528f4474d50SJames Hogan 		/* save hard timer state */
529f4474d50SJames Hogan 		_kvm_vz_save_htimer(vcpu, &compare, &cause);
530f4474d50SJames Hogan 	} else {
531c992a4f6SJames Hogan 		compare = read_gc0_compare();
532c992a4f6SJames Hogan 		cause = read_gc0_cause();
533f4474d50SJames Hogan 	}
534c992a4f6SJames Hogan 
535c992a4f6SJames Hogan 	/* save timer-related state to VCPU context */
536c992a4f6SJames Hogan 	kvm_write_sw_gc0_cause(cop0, cause);
537c992a4f6SJames Hogan 	kvm_write_sw_gc0_compare(cop0, compare);
538c992a4f6SJames Hogan }
539c992a4f6SJames Hogan 
540c992a4f6SJames Hogan /**
541f4474d50SJames Hogan  * kvm_vz_lose_htimer() - Ensure hard guest timer is not in use.
542f4474d50SJames Hogan  * @vcpu:	Virtual CPU.
543f4474d50SJames Hogan  *
544f4474d50SJames Hogan  * Transfers the state of the hard guest timer to the soft guest timer, leaving
545f4474d50SJames Hogan  * guest state intact so it can continue to be used with the soft timer.
546f4474d50SJames Hogan  */
kvm_vz_lose_htimer(struct kvm_vcpu * vcpu)547f4474d50SJames Hogan void kvm_vz_lose_htimer(struct kvm_vcpu *vcpu)
548f4474d50SJames Hogan {
549f4474d50SJames Hogan 	u32 gctl0, compare, cause;
550f4474d50SJames Hogan 
551f4474d50SJames Hogan 	preempt_disable();
552f4474d50SJames Hogan 	gctl0 = read_c0_guestctl0();
553f4474d50SJames Hogan 	if (gctl0 & MIPS_GCTL0_GT) {
554f4474d50SJames Hogan 		/* disable guest use of timer */
555f4474d50SJames Hogan 		write_c0_guestctl0(gctl0 & ~MIPS_GCTL0_GT);
556f4474d50SJames Hogan 
557f4474d50SJames Hogan 		/* switch to soft timer */
558f4474d50SJames Hogan 		_kvm_vz_save_htimer(vcpu, &compare, &cause);
559f4474d50SJames Hogan 
560f4474d50SJames Hogan 		/* leave soft timer in usable state */
561f4474d50SJames Hogan 		_kvm_vz_restore_stimer(vcpu, compare, cause);
562f4474d50SJames Hogan 	}
563f4474d50SJames Hogan 	preempt_enable();
564f4474d50SJames Hogan }
565f4474d50SJames Hogan 
566f4474d50SJames Hogan /**
5674b7de028SJames Hogan  * is_eva_access() - Find whether an instruction is an EVA memory accessor.
5684b7de028SJames Hogan  * @inst:	32-bit instruction encoding.
5694b7de028SJames Hogan  *
5704b7de028SJames Hogan  * Finds whether @inst encodes an EVA memory access instruction, which would
5714b7de028SJames Hogan  * indicate that emulation of it should access the user mode address space
5724b7de028SJames Hogan  * instead of the kernel mode address space. This matters for MUSUK segments
5734b7de028SJames Hogan  * which are TLB mapped for user mode but unmapped for kernel mode.
5744b7de028SJames Hogan  *
5754b7de028SJames Hogan  * Returns:	Whether @inst encodes an EVA accessor instruction.
5764b7de028SJames Hogan  */
is_eva_access(union mips_instruction inst)5774b7de028SJames Hogan static bool is_eva_access(union mips_instruction inst)
5784b7de028SJames Hogan {
5794b7de028SJames Hogan 	if (inst.spec3_format.opcode != spec3_op)
5804b7de028SJames Hogan 		return false;
5814b7de028SJames Hogan 
5824b7de028SJames Hogan 	switch (inst.spec3_format.func) {
5834b7de028SJames Hogan 	case lwle_op:
5844b7de028SJames Hogan 	case lwre_op:
5854b7de028SJames Hogan 	case cachee_op:
5864b7de028SJames Hogan 	case sbe_op:
5874b7de028SJames Hogan 	case she_op:
5884b7de028SJames Hogan 	case sce_op:
5894b7de028SJames Hogan 	case swe_op:
5904b7de028SJames Hogan 	case swle_op:
5914b7de028SJames Hogan 	case swre_op:
5924b7de028SJames Hogan 	case prefe_op:
5934b7de028SJames Hogan 	case lbue_op:
5944b7de028SJames Hogan 	case lhue_op:
5954b7de028SJames Hogan 	case lbe_op:
5964b7de028SJames Hogan 	case lhe_op:
5974b7de028SJames Hogan 	case lle_op:
5984b7de028SJames Hogan 	case lwe_op:
5994b7de028SJames Hogan 		return true;
6004b7de028SJames Hogan 	default:
6014b7de028SJames Hogan 		return false;
6024b7de028SJames Hogan 	}
6034b7de028SJames Hogan }
6044b7de028SJames Hogan 
6054b7de028SJames Hogan /**
6064b7de028SJames Hogan  * is_eva_am_mapped() - Find whether an access mode is mapped.
6074b7de028SJames Hogan  * @vcpu:	KVM VCPU state.
6084b7de028SJames Hogan  * @am:		3-bit encoded access mode.
6094b7de028SJames Hogan  * @eu:		Segment becomes unmapped and uncached when Status.ERL=1.
6104b7de028SJames Hogan  *
6114b7de028SJames Hogan  * Decode @am to find whether it encodes a mapped segment for the current VCPU
6124b7de028SJames Hogan  * state. Where necessary @eu and the actual instruction causing the fault are
6134b7de028SJames Hogan  * taken into account to make the decision.
6144b7de028SJames Hogan  *
6154b7de028SJames Hogan  * Returns:	Whether the VCPU faulted on a TLB mapped address.
6164b7de028SJames Hogan  */
is_eva_am_mapped(struct kvm_vcpu * vcpu,unsigned int am,bool eu)6174b7de028SJames Hogan static bool is_eva_am_mapped(struct kvm_vcpu *vcpu, unsigned int am, bool eu)
6184b7de028SJames Hogan {
6194b7de028SJames Hogan 	u32 am_lookup;
6204b7de028SJames Hogan 	int err;
6214b7de028SJames Hogan 
6224b7de028SJames Hogan 	/*
6234b7de028SJames Hogan 	 * Interpret access control mode. We assume address errors will already
6244b7de028SJames Hogan 	 * have been caught by the guest, leaving us with:
6254b7de028SJames Hogan 	 *      AM      UM  SM  KM  31..24 23..16
6264b7de028SJames Hogan 	 * UK    0 000          Unm   0      0
6274b7de028SJames Hogan 	 * MK    1 001          TLB   1
6284b7de028SJames Hogan 	 * MSK   2 010      TLB TLB   1
6294b7de028SJames Hogan 	 * MUSK  3 011  TLB TLB TLB   1
6304b7de028SJames Hogan 	 * MUSUK 4 100  TLB TLB Unm   0      1
6314b7de028SJames Hogan 	 * USK   5 101      Unm Unm   0      0
6324b7de028SJames Hogan 	 * -     6 110                0      0
6334b7de028SJames Hogan 	 * UUSK  7 111  Unm Unm Unm   0      0
6344b7de028SJames Hogan 	 *
6354b7de028SJames Hogan 	 * We shift a magic value by AM across the sign bit to find if always
6364b7de028SJames Hogan 	 * TLB mapped, and if not shift by 8 again to find if it depends on KM.
6374b7de028SJames Hogan 	 */
6384b7de028SJames Hogan 	am_lookup = 0x70080000 << am;
6394b7de028SJames Hogan 	if ((s32)am_lookup < 0) {
6404b7de028SJames Hogan 		/*
6414b7de028SJames Hogan 		 * MK, MSK, MUSK
6424b7de028SJames Hogan 		 * Always TLB mapped, unless SegCtl.EU && ERL
6434b7de028SJames Hogan 		 */
6444b7de028SJames Hogan 		if (!eu || !(read_gc0_status() & ST0_ERL))
6454b7de028SJames Hogan 			return true;
6464b7de028SJames Hogan 	} else {
6474b7de028SJames Hogan 		am_lookup <<= 8;
6484b7de028SJames Hogan 		if ((s32)am_lookup < 0) {
6494b7de028SJames Hogan 			union mips_instruction inst;
6504b7de028SJames Hogan 			unsigned int status;
6514b7de028SJames Hogan 			u32 *opc;
6524b7de028SJames Hogan 
6534b7de028SJames Hogan 			/*
6544b7de028SJames Hogan 			 * MUSUK
6554b7de028SJames Hogan 			 * TLB mapped if not in kernel mode
6564b7de028SJames Hogan 			 */
6574b7de028SJames Hogan 			status = read_gc0_status();
6584b7de028SJames Hogan 			if (!(status & (ST0_EXL | ST0_ERL)) &&
6594b7de028SJames Hogan 			    (status & ST0_KSU))
6604b7de028SJames Hogan 				return true;
6614b7de028SJames Hogan 			/*
6624b7de028SJames Hogan 			 * EVA access instructions in kernel
6634b7de028SJames Hogan 			 * mode access user address space.
6644b7de028SJames Hogan 			 */
6654b7de028SJames Hogan 			opc = (u32 *)vcpu->arch.pc;
6664b7de028SJames Hogan 			if (vcpu->arch.host_cp0_cause & CAUSEF_BD)
6674b7de028SJames Hogan 				opc += 1;
6684b7de028SJames Hogan 			err = kvm_get_badinstr(opc, vcpu, &inst.word);
6694b7de028SJames Hogan 			if (!err && is_eva_access(inst))
6704b7de028SJames Hogan 				return true;
6714b7de028SJames Hogan 		}
6724b7de028SJames Hogan 	}
6734b7de028SJames Hogan 
6744b7de028SJames Hogan 	return false;
6754b7de028SJames Hogan }
6764b7de028SJames Hogan 
6774b7de028SJames Hogan /**
678c992a4f6SJames Hogan  * kvm_vz_gva_to_gpa() - Convert valid GVA to GPA.
679c992a4f6SJames Hogan  * @vcpu:	KVM VCPU state.
680c992a4f6SJames Hogan  * @gva:	Guest virtual address to convert.
681c992a4f6SJames Hogan  * @gpa:	Output guest physical address.
682c992a4f6SJames Hogan  *
683c992a4f6SJames Hogan  * Convert a guest virtual address (GVA) which is valid according to the guest
684c992a4f6SJames Hogan  * context, to a guest physical address (GPA).
685c992a4f6SJames Hogan  *
686c992a4f6SJames Hogan  * Returns:	0 on success.
687c992a4f6SJames Hogan  *		-errno on failure.
688c992a4f6SJames Hogan  */
kvm_vz_gva_to_gpa(struct kvm_vcpu * vcpu,unsigned long gva,unsigned long * gpa)689c992a4f6SJames Hogan static int kvm_vz_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
690c992a4f6SJames Hogan 			     unsigned long *gpa)
691c992a4f6SJames Hogan {
692c992a4f6SJames Hogan 	u32 gva32 = gva;
6934b7de028SJames Hogan 	unsigned long segctl;
694c992a4f6SJames Hogan 
695c992a4f6SJames Hogan 	if ((long)gva == (s32)gva32) {
696c992a4f6SJames Hogan 		/* Handle canonical 32-bit virtual address */
6974b7de028SJames Hogan 		if (cpu_guest_has_segments) {
6984b7de028SJames Hogan 			unsigned long mask, pa;
6994b7de028SJames Hogan 
7004b7de028SJames Hogan 			switch (gva32 >> 29) {
7014b7de028SJames Hogan 			case 0:
7024b7de028SJames Hogan 			case 1: /* CFG5 (1GB) */
7034b7de028SJames Hogan 				segctl = read_gc0_segctl2() >> 16;
7044b7de028SJames Hogan 				mask = (unsigned long)0xfc0000000ull;
7054b7de028SJames Hogan 				break;
7064b7de028SJames Hogan 			case 2:
7074b7de028SJames Hogan 			case 3: /* CFG4 (1GB) */
7084b7de028SJames Hogan 				segctl = read_gc0_segctl2();
7094b7de028SJames Hogan 				mask = (unsigned long)0xfc0000000ull;
7104b7de028SJames Hogan 				break;
7114b7de028SJames Hogan 			case 4: /* CFG3 (512MB) */
7124b7de028SJames Hogan 				segctl = read_gc0_segctl1() >> 16;
7134b7de028SJames Hogan 				mask = (unsigned long)0xfe0000000ull;
7144b7de028SJames Hogan 				break;
7154b7de028SJames Hogan 			case 5: /* CFG2 (512MB) */
7164b7de028SJames Hogan 				segctl = read_gc0_segctl1();
7174b7de028SJames Hogan 				mask = (unsigned long)0xfe0000000ull;
7184b7de028SJames Hogan 				break;
7194b7de028SJames Hogan 			case 6: /* CFG1 (512MB) */
7204b7de028SJames Hogan 				segctl = read_gc0_segctl0() >> 16;
7214b7de028SJames Hogan 				mask = (unsigned long)0xfe0000000ull;
7224b7de028SJames Hogan 				break;
7234b7de028SJames Hogan 			case 7: /* CFG0 (512MB) */
7244b7de028SJames Hogan 				segctl = read_gc0_segctl0();
7254b7de028SJames Hogan 				mask = (unsigned long)0xfe0000000ull;
7264b7de028SJames Hogan 				break;
7274b7de028SJames Hogan 			default:
7284b7de028SJames Hogan 				/*
7294b7de028SJames Hogan 				 * GCC 4.9 isn't smart enough to figure out that
7304b7de028SJames Hogan 				 * segctl and mask are always initialised.
7314b7de028SJames Hogan 				 */
7324b7de028SJames Hogan 				unreachable();
7334b7de028SJames Hogan 			}
7344b7de028SJames Hogan 
7354b7de028SJames Hogan 			if (is_eva_am_mapped(vcpu, (segctl >> 4) & 0x7,
7364b7de028SJames Hogan 					     segctl & 0x0008))
7374b7de028SJames Hogan 				goto tlb_mapped;
7384b7de028SJames Hogan 
7394b7de028SJames Hogan 			/* Unmapped, find guest physical address */
7404b7de028SJames Hogan 			pa = (segctl << 20) & mask;
7414b7de028SJames Hogan 			pa |= gva32 & ~mask;
7424b7de028SJames Hogan 			*gpa = pa;
7434b7de028SJames Hogan 			return 0;
7444b7de028SJames Hogan 		} else if ((s32)gva32 < (s32)0xc0000000) {
745c992a4f6SJames Hogan 			/* legacy unmapped KSeg0 or KSeg1 */
746c992a4f6SJames Hogan 			*gpa = gva32 & 0x1fffffff;
747c992a4f6SJames Hogan 			return 0;
748c992a4f6SJames Hogan 		}
749c992a4f6SJames Hogan #ifdef CONFIG_64BIT
750c992a4f6SJames Hogan 	} else if ((gva & 0xc000000000000000) == 0x8000000000000000) {
751c992a4f6SJames Hogan 		/* XKPHYS */
7524b7de028SJames Hogan 		if (cpu_guest_has_segments) {
7534b7de028SJames Hogan 			/*
7544b7de028SJames Hogan 			 * Each of the 8 regions can be overridden by SegCtl2.XR
7554b7de028SJames Hogan 			 * to use SegCtl1.XAM.
7564b7de028SJames Hogan 			 */
7574b7de028SJames Hogan 			segctl = read_gc0_segctl2();
7584b7de028SJames Hogan 			if (segctl & (1ull << (56 + ((gva >> 59) & 0x7)))) {
7594b7de028SJames Hogan 				segctl = read_gc0_segctl1();
7604b7de028SJames Hogan 				if (is_eva_am_mapped(vcpu, (segctl >> 59) & 0x7,
7614b7de028SJames Hogan 						     0))
7624b7de028SJames Hogan 					goto tlb_mapped;
7634b7de028SJames Hogan 			}
7644b7de028SJames Hogan 
7654b7de028SJames Hogan 		}
766c992a4f6SJames Hogan 		/*
767c992a4f6SJames Hogan 		 * Traditionally fully unmapped.
768c992a4f6SJames Hogan 		 * Bits 61:59 specify the CCA, which we can just mask off here.
769c992a4f6SJames Hogan 		 * Bits 58:PABITS should be zero, but we shouldn't have got here
770c992a4f6SJames Hogan 		 * if it wasn't.
771c992a4f6SJames Hogan 		 */
772c992a4f6SJames Hogan 		*gpa = gva & 0x07ffffffffffffff;
773c992a4f6SJames Hogan 		return 0;
774c992a4f6SJames Hogan #endif
775c992a4f6SJames Hogan 	}
776c992a4f6SJames Hogan 
7774b7de028SJames Hogan tlb_mapped:
778c992a4f6SJames Hogan 	return kvm_vz_guest_tlb_lookup(vcpu, gva, gpa);
779c992a4f6SJames Hogan }
780c992a4f6SJames Hogan 
781c992a4f6SJames Hogan /**
782c992a4f6SJames Hogan  * kvm_vz_badvaddr_to_gpa() - Convert GVA BadVAddr from root exception to GPA.
783c992a4f6SJames Hogan  * @vcpu:	KVM VCPU state.
784c992a4f6SJames Hogan  * @badvaddr:	Root BadVAddr.
785c992a4f6SJames Hogan  * @gpa:	Output guest physical address.
786c992a4f6SJames Hogan  *
787c992a4f6SJames Hogan  * VZ implementations are permitted to report guest virtual addresses (GVA) in
788c992a4f6SJames Hogan  * BadVAddr on a root exception during guest execution, instead of the more
789c992a4f6SJames Hogan  * convenient guest physical addresses (GPA). When we get a GVA, this function
790c992a4f6SJames Hogan  * converts it to a GPA, taking into account guest segmentation and guest TLB
791c992a4f6SJames Hogan  * state.
792c992a4f6SJames Hogan  *
793c992a4f6SJames Hogan  * Returns:	0 on success.
794c992a4f6SJames Hogan  *		-errno on failure.
795c992a4f6SJames Hogan  */
kvm_vz_badvaddr_to_gpa(struct kvm_vcpu * vcpu,unsigned long badvaddr,unsigned long * gpa)796c992a4f6SJames Hogan static int kvm_vz_badvaddr_to_gpa(struct kvm_vcpu *vcpu, unsigned long badvaddr,
797c992a4f6SJames Hogan 				  unsigned long *gpa)
798c992a4f6SJames Hogan {
799c992a4f6SJames Hogan 	unsigned int gexccode = (vcpu->arch.host_cp0_guestctl0 &
800c992a4f6SJames Hogan 				 MIPS_GCTL0_GEXC) >> MIPS_GCTL0_GEXC_SHIFT;
801c992a4f6SJames Hogan 
802c992a4f6SJames Hogan 	/* If BadVAddr is GPA, then all is well in the world */
803c992a4f6SJames Hogan 	if (likely(gexccode == MIPS_GCTL0_GEXC_GPA)) {
804c992a4f6SJames Hogan 		*gpa = badvaddr;
805c992a4f6SJames Hogan 		return 0;
806c992a4f6SJames Hogan 	}
807c992a4f6SJames Hogan 
808c992a4f6SJames Hogan 	/* Otherwise we'd expect it to be GVA ... */
809c992a4f6SJames Hogan 	if (WARN(gexccode != MIPS_GCTL0_GEXC_GVA,
810c992a4f6SJames Hogan 		 "Unexpected gexccode %#x\n", gexccode))
811c992a4f6SJames Hogan 		return -EINVAL;
812c992a4f6SJames Hogan 
813c992a4f6SJames Hogan 	/* ... and we need to perform the GVA->GPA translation in software */
814c992a4f6SJames Hogan 	return kvm_vz_gva_to_gpa(vcpu, badvaddr, gpa);
815c992a4f6SJames Hogan }
816c992a4f6SJames Hogan 
kvm_trap_vz_no_handler(struct kvm_vcpu * vcpu)817c992a4f6SJames Hogan static int kvm_trap_vz_no_handler(struct kvm_vcpu *vcpu)
818c992a4f6SJames Hogan {
819c992a4f6SJames Hogan 	u32 *opc = (u32 *) vcpu->arch.pc;
820c992a4f6SJames Hogan 	u32 cause = vcpu->arch.host_cp0_cause;
821c992a4f6SJames Hogan 	u32 exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
822c992a4f6SJames Hogan 	unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
823c992a4f6SJames Hogan 	u32 inst = 0;
824c992a4f6SJames Hogan 
825c992a4f6SJames Hogan 	/*
826c992a4f6SJames Hogan 	 *  Fetch the instruction.
827c992a4f6SJames Hogan 	 */
828c992a4f6SJames Hogan 	if (cause & CAUSEF_BD)
829c992a4f6SJames Hogan 		opc += 1;
830c992a4f6SJames Hogan 	kvm_get_badinstr(opc, vcpu, &inst);
831c992a4f6SJames Hogan 
832c992a4f6SJames Hogan 	kvm_err("Exception Code: %d not handled @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#x\n",
833c992a4f6SJames Hogan 		exccode, opc, inst, badvaddr,
834c992a4f6SJames Hogan 		read_gc0_status());
835c992a4f6SJames Hogan 	kvm_arch_vcpu_dump_regs(vcpu);
836c992a4f6SJames Hogan 	vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
837c992a4f6SJames Hogan 	return RESUME_HOST;
838c992a4f6SJames Hogan }
839c992a4f6SJames Hogan 
mips_process_maar(unsigned int op,unsigned long val)840d42a008fSJames Hogan static unsigned long mips_process_maar(unsigned int op, unsigned long val)
841d42a008fSJames Hogan {
842d42a008fSJames Hogan 	/* Mask off unused bits */
843d42a008fSJames Hogan 	unsigned long mask = 0xfffff000 | MIPS_MAAR_S | MIPS_MAAR_VL;
844d42a008fSJames Hogan 
845d42a008fSJames Hogan 	if (read_gc0_pagegrain() & PG_ELPA)
846d42a008fSJames Hogan 		mask |= 0x00ffffff00000000ull;
847d42a008fSJames Hogan 	if (cpu_guest_has_mvh)
848d42a008fSJames Hogan 		mask |= MIPS_MAAR_VH;
849d42a008fSJames Hogan 
850d42a008fSJames Hogan 	/* Set or clear VH */
851d42a008fSJames Hogan 	if (op == mtc_op) {
852d42a008fSJames Hogan 		/* clear VH */
853d42a008fSJames Hogan 		val &= ~MIPS_MAAR_VH;
854d42a008fSJames Hogan 	} else if (op == dmtc_op) {
855d42a008fSJames Hogan 		/* set VH to match VL */
856d42a008fSJames Hogan 		val &= ~MIPS_MAAR_VH;
857d42a008fSJames Hogan 		if (val & MIPS_MAAR_VL)
858d42a008fSJames Hogan 			val |= MIPS_MAAR_VH;
859d42a008fSJames Hogan 	}
860d42a008fSJames Hogan 
861d42a008fSJames Hogan 	return val & mask;
862d42a008fSJames Hogan }
863d42a008fSJames Hogan 
kvm_write_maari(struct kvm_vcpu * vcpu,unsigned long val)864d42a008fSJames Hogan static void kvm_write_maari(struct kvm_vcpu *vcpu, unsigned long val)
865d42a008fSJames Hogan {
866d42a008fSJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
867d42a008fSJames Hogan 
868d42a008fSJames Hogan 	val &= MIPS_MAARI_INDEX;
869d42a008fSJames Hogan 	if (val == MIPS_MAARI_INDEX)
870d42a008fSJames Hogan 		kvm_write_sw_gc0_maari(cop0, ARRAY_SIZE(vcpu->arch.maar) - 1);
871d42a008fSJames Hogan 	else if (val < ARRAY_SIZE(vcpu->arch.maar))
872d42a008fSJames Hogan 		kvm_write_sw_gc0_maari(cop0, val);
873d42a008fSJames Hogan }
874d42a008fSJames Hogan 
kvm_vz_gpsi_cop0(union mips_instruction inst,u32 * opc,u32 cause,struct kvm_vcpu * vcpu)875c992a4f6SJames Hogan static enum emulation_result kvm_vz_gpsi_cop0(union mips_instruction inst,
876c992a4f6SJames Hogan 					      u32 *opc, u32 cause,
877c992a4f6SJames Hogan 					      struct kvm_vcpu *vcpu)
878c992a4f6SJames Hogan {
879c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
880c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
881c992a4f6SJames Hogan 	u32 rt, rd, sel;
882c992a4f6SJames Hogan 	unsigned long curr_pc;
883c992a4f6SJames Hogan 	unsigned long val;
884c992a4f6SJames Hogan 
885c992a4f6SJames Hogan 	/*
886c992a4f6SJames Hogan 	 * Update PC and hold onto current PC in case there is
887c992a4f6SJames Hogan 	 * an error and we want to rollback the PC
888c992a4f6SJames Hogan 	 */
889c992a4f6SJames Hogan 	curr_pc = vcpu->arch.pc;
890c992a4f6SJames Hogan 	er = update_pc(vcpu, cause);
891c992a4f6SJames Hogan 	if (er == EMULATE_FAIL)
892c992a4f6SJames Hogan 		return er;
893c992a4f6SJames Hogan 
894c992a4f6SJames Hogan 	if (inst.co_format.co) {
895c992a4f6SJames Hogan 		switch (inst.co_format.func) {
896c992a4f6SJames Hogan 		case wait_op:
897c992a4f6SJames Hogan 			er = kvm_mips_emul_wait(vcpu);
898c992a4f6SJames Hogan 			break;
899c992a4f6SJames Hogan 		default:
900c992a4f6SJames Hogan 			er = EMULATE_FAIL;
901c992a4f6SJames Hogan 		}
902c992a4f6SJames Hogan 	} else {
903c992a4f6SJames Hogan 		rt = inst.c0r_format.rt;
904c992a4f6SJames Hogan 		rd = inst.c0r_format.rd;
905c992a4f6SJames Hogan 		sel = inst.c0r_format.sel;
906c992a4f6SJames Hogan 
907c992a4f6SJames Hogan 		switch (inst.c0r_format.rs) {
908c992a4f6SJames Hogan 		case dmfc_op:
909c992a4f6SJames Hogan 		case mfc_op:
910c992a4f6SJames Hogan #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
911c992a4f6SJames Hogan 			cop0->stat[rd][sel]++;
912c992a4f6SJames Hogan #endif
913c992a4f6SJames Hogan 			if (rd == MIPS_CP0_COUNT &&
914c992a4f6SJames Hogan 			    sel == 0) {			/* Count */
915c992a4f6SJames Hogan 				val = kvm_mips_read_count(vcpu);
916c992a4f6SJames Hogan 			} else if (rd == MIPS_CP0_COMPARE &&
917c992a4f6SJames Hogan 				   sel == 0) {		/* Compare */
918c992a4f6SJames Hogan 				val = read_gc0_compare();
919273819a6SJames Hogan 			} else if (rd == MIPS_CP0_LLADDR &&
920273819a6SJames Hogan 				   sel == 0) {		/* LLAddr */
921273819a6SJames Hogan 				if (cpu_guest_has_rw_llb)
922273819a6SJames Hogan 					val = read_gc0_lladdr() &
923273819a6SJames Hogan 						MIPS_LLADDR_LLB;
924273819a6SJames Hogan 				else
925273819a6SJames Hogan 					val = 0;
926d42a008fSJames Hogan 			} else if (rd == MIPS_CP0_LLADDR &&
927d42a008fSJames Hogan 				   sel == 1 &&		/* MAAR */
928d42a008fSJames Hogan 				   cpu_guest_has_maar &&
929d42a008fSJames Hogan 				   !cpu_guest_has_dyn_maar) {
930d42a008fSJames Hogan 				/* MAARI must be in range */
931d42a008fSJames Hogan 				BUG_ON(kvm_read_sw_gc0_maari(cop0) >=
932d42a008fSJames Hogan 						ARRAY_SIZE(vcpu->arch.maar));
933d42a008fSJames Hogan 				val = vcpu->arch.maar[
934d42a008fSJames Hogan 					kvm_read_sw_gc0_maari(cop0)];
935c992a4f6SJames Hogan 			} else if ((rd == MIPS_CP0_PRID &&
936c992a4f6SJames Hogan 				    (sel == 0 ||	/* PRid */
937c992a4f6SJames Hogan 				     sel == 2 ||	/* CDMMBase */
938c992a4f6SJames Hogan 				     sel == 3)) ||	/* CMGCRBase */
939c992a4f6SJames Hogan 				   (rd == MIPS_CP0_STATUS &&
940c992a4f6SJames Hogan 				    (sel == 2 ||	/* SRSCtl */
941c992a4f6SJames Hogan 				     sel == 3)) ||	/* SRSMap */
942c992a4f6SJames Hogan 				   (rd == MIPS_CP0_CONFIG &&
9438a5097eeSHuacai Chen 				    (sel == 6 ||	/* Config6 */
9448a5097eeSHuacai Chen 				     sel == 7)) ||	/* Config7 */
945d42a008fSJames Hogan 				   (rd == MIPS_CP0_LLADDR &&
946d42a008fSJames Hogan 				    (sel == 2) &&	/* MAARI */
947d42a008fSJames Hogan 				    cpu_guest_has_maar &&
948d42a008fSJames Hogan 				    !cpu_guest_has_dyn_maar) ||
949c992a4f6SJames Hogan 				   (rd == MIPS_CP0_ERRCTL &&
950c992a4f6SJames Hogan 				    (sel == 0))) {	/* ErrCtl */
951c992a4f6SJames Hogan 				val = cop0->reg[rd][sel];
9528a5097eeSHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
9538a5097eeSHuacai Chen 			} else if (rd == MIPS_CP0_DIAG &&
9548a5097eeSHuacai Chen 				   (sel == 0)) {	/* Diag */
9558a5097eeSHuacai Chen 				val = cop0->reg[rd][sel];
9568a5097eeSHuacai Chen #endif
957c992a4f6SJames Hogan 			} else {
958c992a4f6SJames Hogan 				val = 0;
959c992a4f6SJames Hogan 				er = EMULATE_FAIL;
960c992a4f6SJames Hogan 			}
961c992a4f6SJames Hogan 
962c992a4f6SJames Hogan 			if (er != EMULATE_FAIL) {
963c992a4f6SJames Hogan 				/* Sign extend */
964c992a4f6SJames Hogan 				if (inst.c0r_format.rs == mfc_op)
965c992a4f6SJames Hogan 					val = (int)val;
966c992a4f6SJames Hogan 				vcpu->arch.gprs[rt] = val;
967c992a4f6SJames Hogan 			}
968c992a4f6SJames Hogan 
969c992a4f6SJames Hogan 			trace_kvm_hwr(vcpu, (inst.c0r_format.rs == mfc_op) ?
970c992a4f6SJames Hogan 					KVM_TRACE_MFC0 : KVM_TRACE_DMFC0,
971c992a4f6SJames Hogan 				      KVM_TRACE_COP0(rd, sel), val);
972c992a4f6SJames Hogan 			break;
973c992a4f6SJames Hogan 
974c992a4f6SJames Hogan 		case dmtc_op:
975c992a4f6SJames Hogan 		case mtc_op:
976c992a4f6SJames Hogan #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
977c992a4f6SJames Hogan 			cop0->stat[rd][sel]++;
978c992a4f6SJames Hogan #endif
979c992a4f6SJames Hogan 			val = vcpu->arch.gprs[rt];
980c992a4f6SJames Hogan 			trace_kvm_hwr(vcpu, (inst.c0r_format.rs == mtc_op) ?
981c992a4f6SJames Hogan 					KVM_TRACE_MTC0 : KVM_TRACE_DMTC0,
982c992a4f6SJames Hogan 				      KVM_TRACE_COP0(rd, sel), val);
983c992a4f6SJames Hogan 
984c992a4f6SJames Hogan 			if (rd == MIPS_CP0_COUNT &&
985c992a4f6SJames Hogan 			    sel == 0) {			/* Count */
986f4474d50SJames Hogan 				kvm_vz_lose_htimer(vcpu);
987c992a4f6SJames Hogan 				kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
988c992a4f6SJames Hogan 			} else if (rd == MIPS_CP0_COMPARE &&
989c992a4f6SJames Hogan 				   sel == 0) {		/* Compare */
990c992a4f6SJames Hogan 				kvm_mips_write_compare(vcpu,
991c992a4f6SJames Hogan 						       vcpu->arch.gprs[rt],
992c992a4f6SJames Hogan 						       true);
993273819a6SJames Hogan 			} else if (rd == MIPS_CP0_LLADDR &&
994273819a6SJames Hogan 				   sel == 0) {		/* LLAddr */
995273819a6SJames Hogan 				/*
996273819a6SJames Hogan 				 * P5600 generates GPSI on guest MTC0 LLAddr.
997273819a6SJames Hogan 				 * Only allow the guest to clear LLB.
998273819a6SJames Hogan 				 */
999273819a6SJames Hogan 				if (cpu_guest_has_rw_llb &&
1000273819a6SJames Hogan 				    !(val & MIPS_LLADDR_LLB))
1001273819a6SJames Hogan 					write_gc0_lladdr(0);
1002d42a008fSJames Hogan 			} else if (rd == MIPS_CP0_LLADDR &&
1003d42a008fSJames Hogan 				   sel == 1 &&		/* MAAR */
1004d42a008fSJames Hogan 				   cpu_guest_has_maar &&
1005d42a008fSJames Hogan 				   !cpu_guest_has_dyn_maar) {
1006d42a008fSJames Hogan 				val = mips_process_maar(inst.c0r_format.rs,
1007d42a008fSJames Hogan 							val);
1008d42a008fSJames Hogan 
1009d42a008fSJames Hogan 				/* MAARI must be in range */
1010d42a008fSJames Hogan 				BUG_ON(kvm_read_sw_gc0_maari(cop0) >=
1011d42a008fSJames Hogan 						ARRAY_SIZE(vcpu->arch.maar));
1012d42a008fSJames Hogan 				vcpu->arch.maar[kvm_read_sw_gc0_maari(cop0)] =
1013d42a008fSJames Hogan 									val;
1014d42a008fSJames Hogan 			} else if (rd == MIPS_CP0_LLADDR &&
1015d42a008fSJames Hogan 				   (sel == 2) &&	/* MAARI */
1016d42a008fSJames Hogan 				   cpu_guest_has_maar &&
1017d42a008fSJames Hogan 				   !cpu_guest_has_dyn_maar) {
1018d42a008fSJames Hogan 				kvm_write_maari(vcpu, val);
10198a5097eeSHuacai Chen 			} else if (rd == MIPS_CP0_CONFIG &&
10208a5097eeSHuacai Chen 				   (sel == 6)) {
10218a5097eeSHuacai Chen 				cop0->reg[rd][sel] = (int)val;
1022c992a4f6SJames Hogan 			} else if (rd == MIPS_CP0_ERRCTL &&
1023c992a4f6SJames Hogan 				   (sel == 0)) {	/* ErrCtl */
1024c992a4f6SJames Hogan 				/* ignore the written value */
10258a5097eeSHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
10268a5097eeSHuacai Chen 			} else if (rd == MIPS_CP0_DIAG &&
10278a5097eeSHuacai Chen 				   (sel == 0)) {	/* Diag */
10288a5097eeSHuacai Chen 				unsigned long flags;
10298a5097eeSHuacai Chen 
10308a5097eeSHuacai Chen 				local_irq_save(flags);
10318a5097eeSHuacai Chen 				if (val & LOONGSON_DIAG_BTB) {
10328a5097eeSHuacai Chen 					/* Flush BTB */
10338a5097eeSHuacai Chen 					set_c0_diag(LOONGSON_DIAG_BTB);
10348a5097eeSHuacai Chen 				}
10358a5097eeSHuacai Chen 				if (val & LOONGSON_DIAG_ITLB) {
10368a5097eeSHuacai Chen 					/* Flush ITLB */
10378a5097eeSHuacai Chen 					set_c0_diag(LOONGSON_DIAG_ITLB);
10388a5097eeSHuacai Chen 				}
10398a5097eeSHuacai Chen 				if (val & LOONGSON_DIAG_DTLB) {
10408a5097eeSHuacai Chen 					/* Flush DTLB */
10418a5097eeSHuacai Chen 					set_c0_diag(LOONGSON_DIAG_DTLB);
10428a5097eeSHuacai Chen 				}
10438a5097eeSHuacai Chen 				if (val & LOONGSON_DIAG_VTLB) {
10448a5097eeSHuacai Chen 					/* Flush VTLB */
10458a5097eeSHuacai Chen 					kvm_loongson_clear_guest_vtlb();
10468a5097eeSHuacai Chen 				}
10478a5097eeSHuacai Chen 				if (val & LOONGSON_DIAG_FTLB) {
10488a5097eeSHuacai Chen 					/* Flush FTLB */
10498a5097eeSHuacai Chen 					kvm_loongson_clear_guest_ftlb();
10508a5097eeSHuacai Chen 				}
10518a5097eeSHuacai Chen 				local_irq_restore(flags);
10528a5097eeSHuacai Chen #endif
1053c992a4f6SJames Hogan 			} else {
1054c992a4f6SJames Hogan 				er = EMULATE_FAIL;
1055c992a4f6SJames Hogan 			}
1056c992a4f6SJames Hogan 			break;
1057c992a4f6SJames Hogan 
1058c992a4f6SJames Hogan 		default:
1059c992a4f6SJames Hogan 			er = EMULATE_FAIL;
1060c992a4f6SJames Hogan 			break;
1061c992a4f6SJames Hogan 		}
1062c992a4f6SJames Hogan 	}
1063c992a4f6SJames Hogan 	/* Rollback PC only if emulation was unsuccessful */
1064c992a4f6SJames Hogan 	if (er == EMULATE_FAIL) {
1065c992a4f6SJames Hogan 		kvm_err("[%#lx]%s: unsupported cop0 instruction 0x%08x\n",
1066c992a4f6SJames Hogan 			curr_pc, __func__, inst.word);
1067c992a4f6SJames Hogan 
1068c992a4f6SJames Hogan 		vcpu->arch.pc = curr_pc;
1069c992a4f6SJames Hogan 	}
1070c992a4f6SJames Hogan 
1071c992a4f6SJames Hogan 	return er;
1072c992a4f6SJames Hogan }
1073c992a4f6SJames Hogan 
kvm_vz_gpsi_cache(union mips_instruction inst,u32 * opc,u32 cause,struct kvm_vcpu * vcpu)1074c992a4f6SJames Hogan static enum emulation_result kvm_vz_gpsi_cache(union mips_instruction inst,
1075c992a4f6SJames Hogan 					       u32 *opc, u32 cause,
1076c992a4f6SJames Hogan 					       struct kvm_vcpu *vcpu)
1077c992a4f6SJames Hogan {
1078c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
1079c992a4f6SJames Hogan 	u32 cache, op_inst, op, base;
1080c992a4f6SJames Hogan 	s16 offset;
1081c992a4f6SJames Hogan 	struct kvm_vcpu_arch *arch = &vcpu->arch;
1082c992a4f6SJames Hogan 	unsigned long va, curr_pc;
1083c992a4f6SJames Hogan 
1084c992a4f6SJames Hogan 	/*
1085c992a4f6SJames Hogan 	 * Update PC and hold onto current PC in case there is
1086c992a4f6SJames Hogan 	 * an error and we want to rollback the PC
1087c992a4f6SJames Hogan 	 */
1088c992a4f6SJames Hogan 	curr_pc = vcpu->arch.pc;
1089c992a4f6SJames Hogan 	er = update_pc(vcpu, cause);
1090c992a4f6SJames Hogan 	if (er == EMULATE_FAIL)
1091c992a4f6SJames Hogan 		return er;
1092c992a4f6SJames Hogan 
1093c992a4f6SJames Hogan 	base = inst.i_format.rs;
1094c992a4f6SJames Hogan 	op_inst = inst.i_format.rt;
1095c992a4f6SJames Hogan 	if (cpu_has_mips_r6)
1096c992a4f6SJames Hogan 		offset = inst.spec3_format.simmediate;
1097c992a4f6SJames Hogan 	else
1098c992a4f6SJames Hogan 		offset = inst.i_format.simmediate;
1099c992a4f6SJames Hogan 	cache = op_inst & CacheOp_Cache;
1100c992a4f6SJames Hogan 	op = op_inst & CacheOp_Op;
1101c992a4f6SJames Hogan 
1102c992a4f6SJames Hogan 	va = arch->gprs[base] + offset;
1103c992a4f6SJames Hogan 
1104c992a4f6SJames Hogan 	kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1105c992a4f6SJames Hogan 		  cache, op, base, arch->gprs[base], offset);
1106c992a4f6SJames Hogan 
1107c992a4f6SJames Hogan 	/* Secondary or tirtiary cache ops ignored */
1108c992a4f6SJames Hogan 	if (cache != Cache_I && cache != Cache_D)
1109c992a4f6SJames Hogan 		return EMULATE_DONE;
1110c992a4f6SJames Hogan 
1111c992a4f6SJames Hogan 	switch (op_inst) {
1112c992a4f6SJames Hogan 	case Index_Invalidate_I:
1113c992a4f6SJames Hogan 		flush_icache_line_indexed(va);
1114c992a4f6SJames Hogan 		return EMULATE_DONE;
1115c992a4f6SJames Hogan 	case Index_Writeback_Inv_D:
1116c992a4f6SJames Hogan 		flush_dcache_line_indexed(va);
1117c992a4f6SJames Hogan 		return EMULATE_DONE;
11183ba731daSJames Hogan 	case Hit_Invalidate_I:
11193ba731daSJames Hogan 	case Hit_Invalidate_D:
11203ba731daSJames Hogan 	case Hit_Writeback_Inv_D:
11213ba731daSJames Hogan 		if (boot_cpu_type() == CPU_CAVIUM_OCTEON3) {
11223ba731daSJames Hogan 			/* We can just flush entire icache */
11233ba731daSJames Hogan 			local_flush_icache_range(0, 0);
11243ba731daSJames Hogan 			return EMULATE_DONE;
11253ba731daSJames Hogan 		}
11263ba731daSJames Hogan 
11273ba731daSJames Hogan 		/* So far, other platforms support guest hit cache ops */
11283ba731daSJames Hogan 		break;
1129c992a4f6SJames Hogan 	default:
1130c992a4f6SJames Hogan 		break;
11318d345097SZou Wei 	}
1132c992a4f6SJames Hogan 
1133c992a4f6SJames Hogan 	kvm_err("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
1134c992a4f6SJames Hogan 		curr_pc, vcpu->arch.gprs[31], cache, op, base, arch->gprs[base],
1135c992a4f6SJames Hogan 		offset);
1136c992a4f6SJames Hogan 	/* Rollback PC */
1137c992a4f6SJames Hogan 	vcpu->arch.pc = curr_pc;
1138c992a4f6SJames Hogan 
1139c992a4f6SJames Hogan 	return EMULATE_FAIL;
1140c992a4f6SJames Hogan }
1141c992a4f6SJames Hogan 
11427f2a83f1SHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
kvm_vz_gpsi_lwc2(union mips_instruction inst,u32 * opc,u32 cause,struct kvm_vcpu * vcpu)11437f2a83f1SHuacai Chen static enum emulation_result kvm_vz_gpsi_lwc2(union mips_instruction inst,
11447f2a83f1SHuacai Chen 					      u32 *opc, u32 cause,
11457f2a83f1SHuacai Chen 					      struct kvm_vcpu *vcpu)
11467f2a83f1SHuacai Chen {
11477f2a83f1SHuacai Chen 	unsigned int rs, rd;
11487f2a83f1SHuacai Chen 	unsigned int hostcfg;
11497f2a83f1SHuacai Chen 	unsigned long curr_pc;
11507f2a83f1SHuacai Chen 	enum emulation_result er = EMULATE_DONE;
11517f2a83f1SHuacai Chen 
11527f2a83f1SHuacai Chen 	/*
11537f2a83f1SHuacai Chen 	 * Update PC and hold onto current PC in case there is
11547f2a83f1SHuacai Chen 	 * an error and we want to rollback the PC
11557f2a83f1SHuacai Chen 	 */
11567f2a83f1SHuacai Chen 	curr_pc = vcpu->arch.pc;
11577f2a83f1SHuacai Chen 	er = update_pc(vcpu, cause);
11587f2a83f1SHuacai Chen 	if (er == EMULATE_FAIL)
11597f2a83f1SHuacai Chen 		return er;
11607f2a83f1SHuacai Chen 
11617f2a83f1SHuacai Chen 	rs = inst.loongson3_lscsr_format.rs;
11627f2a83f1SHuacai Chen 	rd = inst.loongson3_lscsr_format.rd;
11637f2a83f1SHuacai Chen 	switch (inst.loongson3_lscsr_format.fr) {
11647f2a83f1SHuacai Chen 	case 0x8:  /* Read CPUCFG */
11657f2a83f1SHuacai Chen 		++vcpu->stat.vz_cpucfg_exits;
11667f2a83f1SHuacai Chen 		hostcfg = read_cpucfg(vcpu->arch.gprs[rs]);
11677f2a83f1SHuacai Chen 
11687f2a83f1SHuacai Chen 		switch (vcpu->arch.gprs[rs]) {
11697f2a83f1SHuacai Chen 		case LOONGSON_CFG0:
11707f2a83f1SHuacai Chen 			vcpu->arch.gprs[rd] = 0x14c000;
11717f2a83f1SHuacai Chen 			break;
11727f2a83f1SHuacai Chen 		case LOONGSON_CFG1:
11737f2a83f1SHuacai Chen 			hostcfg &= (LOONGSON_CFG1_FP | LOONGSON_CFG1_MMI |
11747f2a83f1SHuacai Chen 				    LOONGSON_CFG1_MSA1 | LOONGSON_CFG1_MSA2 |
11757f2a83f1SHuacai Chen 				    LOONGSON_CFG1_SFBP);
11767f2a83f1SHuacai Chen 			vcpu->arch.gprs[rd] = hostcfg;
11777f2a83f1SHuacai Chen 			break;
11787f2a83f1SHuacai Chen 		case LOONGSON_CFG2:
11797f2a83f1SHuacai Chen 			hostcfg &= (LOONGSON_CFG2_LEXT1 | LOONGSON_CFG2_LEXT2 |
11807f2a83f1SHuacai Chen 				    LOONGSON_CFG2_LEXT3 | LOONGSON_CFG2_LSPW);
11817f2a83f1SHuacai Chen 			vcpu->arch.gprs[rd] = hostcfg;
11827f2a83f1SHuacai Chen 			break;
11837f2a83f1SHuacai Chen 		case LOONGSON_CFG3:
11847f2a83f1SHuacai Chen 			vcpu->arch.gprs[rd] = hostcfg;
11857f2a83f1SHuacai Chen 			break;
11867f2a83f1SHuacai Chen 		default:
11877f2a83f1SHuacai Chen 			/* Don't export any other advanced features to guest */
11887f2a83f1SHuacai Chen 			vcpu->arch.gprs[rd] = 0;
11897f2a83f1SHuacai Chen 			break;
11907f2a83f1SHuacai Chen 		}
11917f2a83f1SHuacai Chen 		break;
11927f2a83f1SHuacai Chen 
11937f2a83f1SHuacai Chen 	default:
11947f2a83f1SHuacai Chen 		kvm_err("lwc2 emulate not impl %d rs %lx @%lx\n",
11957f2a83f1SHuacai Chen 			inst.loongson3_lscsr_format.fr, vcpu->arch.gprs[rs], curr_pc);
11967f2a83f1SHuacai Chen 		er = EMULATE_FAIL;
11977f2a83f1SHuacai Chen 		break;
11987f2a83f1SHuacai Chen 	}
11997f2a83f1SHuacai Chen 
12007f2a83f1SHuacai Chen 	/* Rollback PC only if emulation was unsuccessful */
12017f2a83f1SHuacai Chen 	if (er == EMULATE_FAIL) {
12027f2a83f1SHuacai Chen 		kvm_err("[%#lx]%s: unsupported lwc2 instruction 0x%08x 0x%08x\n",
12037f2a83f1SHuacai Chen 			curr_pc, __func__, inst.word, inst.loongson3_lscsr_format.fr);
12047f2a83f1SHuacai Chen 
12057f2a83f1SHuacai Chen 		vcpu->arch.pc = curr_pc;
12067f2a83f1SHuacai Chen 	}
12077f2a83f1SHuacai Chen 
12087f2a83f1SHuacai Chen 	return er;
12097f2a83f1SHuacai Chen }
12107f2a83f1SHuacai Chen #endif
12117f2a83f1SHuacai Chen 
kvm_trap_vz_handle_gpsi(u32 cause,u32 * opc,struct kvm_vcpu * vcpu)1212c992a4f6SJames Hogan static enum emulation_result kvm_trap_vz_handle_gpsi(u32 cause, u32 *opc,
1213c992a4f6SJames Hogan 						     struct kvm_vcpu *vcpu)
1214c992a4f6SJames Hogan {
1215c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
1216c992a4f6SJames Hogan 	struct kvm_vcpu_arch *arch = &vcpu->arch;
1217c992a4f6SJames Hogan 	union mips_instruction inst;
1218c992a4f6SJames Hogan 	int rd, rt, sel;
1219c992a4f6SJames Hogan 	int err;
1220c992a4f6SJames Hogan 
1221c992a4f6SJames Hogan 	/*
1222c992a4f6SJames Hogan 	 *  Fetch the instruction.
1223c992a4f6SJames Hogan 	 */
1224c992a4f6SJames Hogan 	if (cause & CAUSEF_BD)
1225c992a4f6SJames Hogan 		opc += 1;
1226c992a4f6SJames Hogan 	err = kvm_get_badinstr(opc, vcpu, &inst.word);
1227c992a4f6SJames Hogan 	if (err)
1228c992a4f6SJames Hogan 		return EMULATE_FAIL;
1229c992a4f6SJames Hogan 
1230c992a4f6SJames Hogan 	switch (inst.r_format.opcode) {
1231c992a4f6SJames Hogan 	case cop0_op:
1232c34b26b9STianjia Zhang 		er = kvm_vz_gpsi_cop0(inst, opc, cause, vcpu);
1233c992a4f6SJames Hogan 		break;
1234c992a4f6SJames Hogan #ifndef CONFIG_CPU_MIPSR6
1235c992a4f6SJames Hogan 	case cache_op:
1236c992a4f6SJames Hogan 		trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1237c34b26b9STianjia Zhang 		er = kvm_vz_gpsi_cache(inst, opc, cause, vcpu);
1238c992a4f6SJames Hogan 		break;
1239c992a4f6SJames Hogan #endif
12407f2a83f1SHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
12417f2a83f1SHuacai Chen 	case lwc2_op:
1242e792415cSXingxing Su 		er = kvm_vz_gpsi_lwc2(inst, opc, cause, vcpu);
12437f2a83f1SHuacai Chen 		break;
12447f2a83f1SHuacai Chen #endif
1245c992a4f6SJames Hogan 	case spec3_op:
1246c992a4f6SJames Hogan 		switch (inst.spec3_format.func) {
1247c992a4f6SJames Hogan #ifdef CONFIG_CPU_MIPSR6
1248c992a4f6SJames Hogan 		case cache6_op:
1249c992a4f6SJames Hogan 			trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
1250c34b26b9STianjia Zhang 			er = kvm_vz_gpsi_cache(inst, opc, cause, vcpu);
1251c992a4f6SJames Hogan 			break;
1252c992a4f6SJames Hogan #endif
1253c992a4f6SJames Hogan 		case rdhwr_op:
1254c992a4f6SJames Hogan 			if (inst.r_format.rs || (inst.r_format.re >> 3))
1255c992a4f6SJames Hogan 				goto unknown;
1256c992a4f6SJames Hogan 
1257c992a4f6SJames Hogan 			rd = inst.r_format.rd;
1258c992a4f6SJames Hogan 			rt = inst.r_format.rt;
1259c992a4f6SJames Hogan 			sel = inst.r_format.re & 0x7;
1260c992a4f6SJames Hogan 
1261c992a4f6SJames Hogan 			switch (rd) {
1262c992a4f6SJames Hogan 			case MIPS_HWR_CC:	/* Read count register */
1263c992a4f6SJames Hogan 				arch->gprs[rt] =
1264c992a4f6SJames Hogan 					(long)(int)kvm_mips_read_count(vcpu);
1265c992a4f6SJames Hogan 				break;
1266c992a4f6SJames Hogan 			default:
1267c992a4f6SJames Hogan 				trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR,
1268c992a4f6SJames Hogan 					      KVM_TRACE_HWR(rd, sel), 0);
1269c992a4f6SJames Hogan 				goto unknown;
12708d345097SZou Wei 			}
1271c992a4f6SJames Hogan 
1272c992a4f6SJames Hogan 			trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR,
1273c992a4f6SJames Hogan 				      KVM_TRACE_HWR(rd, sel), arch->gprs[rt]);
1274c992a4f6SJames Hogan 
1275c992a4f6SJames Hogan 			er = update_pc(vcpu, cause);
1276c992a4f6SJames Hogan 			break;
1277c992a4f6SJames Hogan 		default:
1278c992a4f6SJames Hogan 			goto unknown;
12798d345097SZou Wei 		}
1280c992a4f6SJames Hogan 		break;
1281c992a4f6SJames Hogan unknown:
1282c992a4f6SJames Hogan 
1283c992a4f6SJames Hogan 	default:
1284c992a4f6SJames Hogan 		kvm_err("GPSI exception not supported (%p/%#x)\n",
1285c992a4f6SJames Hogan 				opc, inst.word);
1286c992a4f6SJames Hogan 		kvm_arch_vcpu_dump_regs(vcpu);
1287c992a4f6SJames Hogan 		er = EMULATE_FAIL;
1288c992a4f6SJames Hogan 		break;
1289c992a4f6SJames Hogan 	}
1290c992a4f6SJames Hogan 
1291c992a4f6SJames Hogan 	return er;
1292c992a4f6SJames Hogan }
1293c992a4f6SJames Hogan 
kvm_trap_vz_handle_gsfc(u32 cause,u32 * opc,struct kvm_vcpu * vcpu)1294c992a4f6SJames Hogan static enum emulation_result kvm_trap_vz_handle_gsfc(u32 cause, u32 *opc,
1295c992a4f6SJames Hogan 						     struct kvm_vcpu *vcpu)
1296c992a4f6SJames Hogan {
1297c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
1298c992a4f6SJames Hogan 	struct kvm_vcpu_arch *arch = &vcpu->arch;
1299c992a4f6SJames Hogan 	union mips_instruction inst;
1300c992a4f6SJames Hogan 	int err;
1301c992a4f6SJames Hogan 
1302c992a4f6SJames Hogan 	/*
1303c992a4f6SJames Hogan 	 *  Fetch the instruction.
1304c992a4f6SJames Hogan 	 */
1305c992a4f6SJames Hogan 	if (cause & CAUSEF_BD)
1306c992a4f6SJames Hogan 		opc += 1;
1307c992a4f6SJames Hogan 	err = kvm_get_badinstr(opc, vcpu, &inst.word);
1308c992a4f6SJames Hogan 	if (err)
1309c992a4f6SJames Hogan 		return EMULATE_FAIL;
1310c992a4f6SJames Hogan 
1311c992a4f6SJames Hogan 	/* complete MTC0 on behalf of guest and advance EPC */
1312c992a4f6SJames Hogan 	if (inst.c0r_format.opcode == cop0_op &&
1313c992a4f6SJames Hogan 	    inst.c0r_format.rs == mtc_op &&
1314c992a4f6SJames Hogan 	    inst.c0r_format.z == 0) {
1315c992a4f6SJames Hogan 		int rt = inst.c0r_format.rt;
1316c992a4f6SJames Hogan 		int rd = inst.c0r_format.rd;
1317c992a4f6SJames Hogan 		int sel = inst.c0r_format.sel;
1318c992a4f6SJames Hogan 		unsigned int val = arch->gprs[rt];
1319c992a4f6SJames Hogan 		unsigned int old_val, change;
1320c992a4f6SJames Hogan 
1321c992a4f6SJames Hogan 		trace_kvm_hwr(vcpu, KVM_TRACE_MTC0, KVM_TRACE_COP0(rd, sel),
1322c992a4f6SJames Hogan 			      val);
1323c992a4f6SJames Hogan 
1324c992a4f6SJames Hogan 		if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
1325c992a4f6SJames Hogan 			/* FR bit should read as zero if no FPU */
1326c992a4f6SJames Hogan 			if (!kvm_mips_guest_has_fpu(&vcpu->arch))
1327c992a4f6SJames Hogan 				val &= ~(ST0_CU1 | ST0_FR);
1328c992a4f6SJames Hogan 
1329c992a4f6SJames Hogan 			/*
1330c992a4f6SJames Hogan 			 * Also don't allow FR to be set if host doesn't support
1331c992a4f6SJames Hogan 			 * it.
1332c992a4f6SJames Hogan 			 */
1333c992a4f6SJames Hogan 			if (!(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
1334c992a4f6SJames Hogan 				val &= ~ST0_FR;
1335c992a4f6SJames Hogan 
1336c992a4f6SJames Hogan 			old_val = read_gc0_status();
1337c992a4f6SJames Hogan 			change = val ^ old_val;
1338c992a4f6SJames Hogan 
1339c992a4f6SJames Hogan 			if (change & ST0_FR) {
1340c992a4f6SJames Hogan 				/*
1341c992a4f6SJames Hogan 				 * FPU and Vector register state is made
1342c992a4f6SJames Hogan 				 * UNPREDICTABLE by a change of FR, so don't
1343c992a4f6SJames Hogan 				 * even bother saving it.
1344c992a4f6SJames Hogan 				 */
1345c992a4f6SJames Hogan 				kvm_drop_fpu(vcpu);
1346c992a4f6SJames Hogan 			}
1347c992a4f6SJames Hogan 
1348c992a4f6SJames Hogan 			/*
1349c992a4f6SJames Hogan 			 * If MSA state is already live, it is undefined how it
1350c992a4f6SJames Hogan 			 * interacts with FR=0 FPU state, and we don't want to
1351c992a4f6SJames Hogan 			 * hit reserved instruction exceptions trying to save
1352c992a4f6SJames Hogan 			 * the MSA state later when CU=1 && FR=1, so play it
1353c992a4f6SJames Hogan 			 * safe and save it first.
1354c992a4f6SJames Hogan 			 */
1355c992a4f6SJames Hogan 			if (change & ST0_CU1 && !(val & ST0_FR) &&
1356c992a4f6SJames Hogan 			    vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
1357c992a4f6SJames Hogan 				kvm_lose_fpu(vcpu);
1358c992a4f6SJames Hogan 
1359c992a4f6SJames Hogan 			write_gc0_status(val);
1360c992a4f6SJames Hogan 		} else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
1361c992a4f6SJames Hogan 			u32 old_cause = read_gc0_cause();
1362c992a4f6SJames Hogan 			u32 change = old_cause ^ val;
1363c992a4f6SJames Hogan 
1364c992a4f6SJames Hogan 			/* DC bit enabling/disabling timer? */
1365c992a4f6SJames Hogan 			if (change & CAUSEF_DC) {
1366f4474d50SJames Hogan 				if (val & CAUSEF_DC) {
1367f4474d50SJames Hogan 					kvm_vz_lose_htimer(vcpu);
1368c992a4f6SJames Hogan 					kvm_mips_count_disable_cause(vcpu);
1369f4474d50SJames Hogan 				} else {
1370c992a4f6SJames Hogan 					kvm_mips_count_enable_cause(vcpu);
1371c992a4f6SJames Hogan 				}
1372f4474d50SJames Hogan 			}
1373c992a4f6SJames Hogan 
1374c992a4f6SJames Hogan 			/* Only certain bits are RW to the guest */
1375c992a4f6SJames Hogan 			change &= (CAUSEF_DC | CAUSEF_IV | CAUSEF_WP |
1376c992a4f6SJames Hogan 				   CAUSEF_IP0 | CAUSEF_IP1);
1377c992a4f6SJames Hogan 
1378c992a4f6SJames Hogan 			/* WP can only be cleared */
1379c992a4f6SJames Hogan 			change &= ~CAUSEF_WP | old_cause;
1380c992a4f6SJames Hogan 
1381c992a4f6SJames Hogan 			write_gc0_cause(old_cause ^ change);
1382c992a4f6SJames Hogan 		} else if ((rd == MIPS_CP0_STATUS) && (sel == 1)) { /* IntCtl */
1383c992a4f6SJames Hogan 			write_gc0_intctl(val);
1384c992a4f6SJames Hogan 		} else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
1385c992a4f6SJames Hogan 			old_val = read_gc0_config5();
1386c992a4f6SJames Hogan 			change = val ^ old_val;
1387c992a4f6SJames Hogan 			/* Handle changes in FPU/MSA modes */
1388c992a4f6SJames Hogan 			preempt_disable();
1389c992a4f6SJames Hogan 
1390c992a4f6SJames Hogan 			/*
1391c992a4f6SJames Hogan 			 * Propagate FRE changes immediately if the FPU
1392c992a4f6SJames Hogan 			 * context is already loaded.
1393c992a4f6SJames Hogan 			 */
1394c992a4f6SJames Hogan 			if (change & MIPS_CONF5_FRE &&
1395c992a4f6SJames Hogan 			    vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
1396c992a4f6SJames Hogan 				change_c0_config5(MIPS_CONF5_FRE, val);
1397c992a4f6SJames Hogan 
1398c992a4f6SJames Hogan 			preempt_enable();
1399c992a4f6SJames Hogan 
1400c992a4f6SJames Hogan 			val = old_val ^
1401c992a4f6SJames Hogan 				(change & kvm_vz_config5_guest_wrmask(vcpu));
1402c992a4f6SJames Hogan 			write_gc0_config5(val);
1403c992a4f6SJames Hogan 		} else {
1404c992a4f6SJames Hogan 			kvm_err("Handle GSFC, unsupported field change @ %p: %#x\n",
1405c992a4f6SJames Hogan 			    opc, inst.word);
1406c992a4f6SJames Hogan 			er = EMULATE_FAIL;
1407c992a4f6SJames Hogan 		}
1408c992a4f6SJames Hogan 
1409c992a4f6SJames Hogan 		if (er != EMULATE_FAIL)
1410c992a4f6SJames Hogan 			er = update_pc(vcpu, cause);
1411c992a4f6SJames Hogan 	} else {
1412c992a4f6SJames Hogan 		kvm_err("Handle GSFC, unrecognized instruction @ %p: %#x\n",
1413c992a4f6SJames Hogan 			opc, inst.word);
1414c992a4f6SJames Hogan 		er = EMULATE_FAIL;
1415c992a4f6SJames Hogan 	}
1416c992a4f6SJames Hogan 
1417c992a4f6SJames Hogan 	return er;
1418c992a4f6SJames Hogan }
1419c992a4f6SJames Hogan 
kvm_trap_vz_handle_ghfc(u32 cause,u32 * opc,struct kvm_vcpu * vcpu)1420edec9d7bSJames Hogan static enum emulation_result kvm_trap_vz_handle_ghfc(u32 cause, u32 *opc,
1421edec9d7bSJames Hogan 						     struct kvm_vcpu *vcpu)
1422edec9d7bSJames Hogan {
1423edec9d7bSJames Hogan 	/*
1424edec9d7bSJames Hogan 	 * Presumably this is due to MC (guest mode change), so lets trace some
1425edec9d7bSJames Hogan 	 * relevant info.
1426edec9d7bSJames Hogan 	 */
1427edec9d7bSJames Hogan 	trace_kvm_guest_mode_change(vcpu);
1428edec9d7bSJames Hogan 
1429edec9d7bSJames Hogan 	return EMULATE_DONE;
1430edec9d7bSJames Hogan }
1431edec9d7bSJames Hogan 
kvm_trap_vz_handle_hc(u32 cause,u32 * opc,struct kvm_vcpu * vcpu)1432c992a4f6SJames Hogan static enum emulation_result kvm_trap_vz_handle_hc(u32 cause, u32 *opc,
1433c992a4f6SJames Hogan 						   struct kvm_vcpu *vcpu)
1434c992a4f6SJames Hogan {
1435c992a4f6SJames Hogan 	enum emulation_result er;
1436c992a4f6SJames Hogan 	union mips_instruction inst;
1437c992a4f6SJames Hogan 	unsigned long curr_pc;
1438c992a4f6SJames Hogan 	int err;
1439c992a4f6SJames Hogan 
1440c992a4f6SJames Hogan 	if (cause & CAUSEF_BD)
1441c992a4f6SJames Hogan 		opc += 1;
1442c992a4f6SJames Hogan 	err = kvm_get_badinstr(opc, vcpu, &inst.word);
1443c992a4f6SJames Hogan 	if (err)
1444c992a4f6SJames Hogan 		return EMULATE_FAIL;
1445c992a4f6SJames Hogan 
1446c992a4f6SJames Hogan 	/*
1447c992a4f6SJames Hogan 	 * Update PC and hold onto current PC in case there is
1448c992a4f6SJames Hogan 	 * an error and we want to rollback the PC
1449c992a4f6SJames Hogan 	 */
1450c992a4f6SJames Hogan 	curr_pc = vcpu->arch.pc;
1451c992a4f6SJames Hogan 	er = update_pc(vcpu, cause);
1452c992a4f6SJames Hogan 	if (er == EMULATE_FAIL)
1453c992a4f6SJames Hogan 		return er;
1454c992a4f6SJames Hogan 
1455c992a4f6SJames Hogan 	er = kvm_mips_emul_hypcall(vcpu, inst);
1456c992a4f6SJames Hogan 	if (er == EMULATE_FAIL)
1457c992a4f6SJames Hogan 		vcpu->arch.pc = curr_pc;
1458c992a4f6SJames Hogan 
1459c992a4f6SJames Hogan 	return er;
1460c992a4f6SJames Hogan }
1461c992a4f6SJames Hogan 
kvm_trap_vz_no_handler_guest_exit(u32 gexccode,u32 cause,u32 * opc,struct kvm_vcpu * vcpu)1462c992a4f6SJames Hogan static enum emulation_result kvm_trap_vz_no_handler_guest_exit(u32 gexccode,
1463c992a4f6SJames Hogan 							u32 cause,
1464c992a4f6SJames Hogan 							u32 *opc,
1465c992a4f6SJames Hogan 							struct kvm_vcpu *vcpu)
1466c992a4f6SJames Hogan {
1467c992a4f6SJames Hogan 	u32 inst;
1468c992a4f6SJames Hogan 
1469c992a4f6SJames Hogan 	/*
1470c992a4f6SJames Hogan 	 *  Fetch the instruction.
1471c992a4f6SJames Hogan 	 */
1472c992a4f6SJames Hogan 	if (cause & CAUSEF_BD)
1473c992a4f6SJames Hogan 		opc += 1;
1474c992a4f6SJames Hogan 	kvm_get_badinstr(opc, vcpu, &inst);
1475c992a4f6SJames Hogan 
1476c992a4f6SJames Hogan 	kvm_err("Guest Exception Code: %d not yet handled @ PC: %p, inst: 0x%08x  Status: %#x\n",
1477c992a4f6SJames Hogan 		gexccode, opc, inst, read_gc0_status());
1478c992a4f6SJames Hogan 
1479c992a4f6SJames Hogan 	return EMULATE_FAIL;
1480c992a4f6SJames Hogan }
1481c992a4f6SJames Hogan 
kvm_trap_vz_handle_guest_exit(struct kvm_vcpu * vcpu)1482c992a4f6SJames Hogan static int kvm_trap_vz_handle_guest_exit(struct kvm_vcpu *vcpu)
1483c992a4f6SJames Hogan {
1484c992a4f6SJames Hogan 	u32 *opc = (u32 *) vcpu->arch.pc;
1485c992a4f6SJames Hogan 	u32 cause = vcpu->arch.host_cp0_cause;
1486c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
1487c992a4f6SJames Hogan 	u32 gexccode = (vcpu->arch.host_cp0_guestctl0 &
1488c992a4f6SJames Hogan 			MIPS_GCTL0_GEXC) >> MIPS_GCTL0_GEXC_SHIFT;
1489c992a4f6SJames Hogan 	int ret = RESUME_GUEST;
1490c992a4f6SJames Hogan 
1491c992a4f6SJames Hogan 	trace_kvm_exit(vcpu, KVM_TRACE_EXIT_GEXCCODE_BASE + gexccode);
1492c992a4f6SJames Hogan 	switch (gexccode) {
1493c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_GPSI:
1494c992a4f6SJames Hogan 		++vcpu->stat.vz_gpsi_exits;
1495c992a4f6SJames Hogan 		er = kvm_trap_vz_handle_gpsi(cause, opc, vcpu);
1496c992a4f6SJames Hogan 		break;
1497c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_GSFC:
1498c992a4f6SJames Hogan 		++vcpu->stat.vz_gsfc_exits;
1499c992a4f6SJames Hogan 		er = kvm_trap_vz_handle_gsfc(cause, opc, vcpu);
1500c992a4f6SJames Hogan 		break;
1501c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_HC:
1502c992a4f6SJames Hogan 		++vcpu->stat.vz_hc_exits;
1503c992a4f6SJames Hogan 		er = kvm_trap_vz_handle_hc(cause, opc, vcpu);
1504c992a4f6SJames Hogan 		break;
1505c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_GRR:
1506c992a4f6SJames Hogan 		++vcpu->stat.vz_grr_exits;
1507c992a4f6SJames Hogan 		er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
1508c992a4f6SJames Hogan 						       vcpu);
1509c992a4f6SJames Hogan 		break;
1510c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_GVA:
1511c992a4f6SJames Hogan 		++vcpu->stat.vz_gva_exits;
1512c992a4f6SJames Hogan 		er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
1513c992a4f6SJames Hogan 						       vcpu);
1514c992a4f6SJames Hogan 		break;
1515c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_GHFC:
1516c992a4f6SJames Hogan 		++vcpu->stat.vz_ghfc_exits;
1517edec9d7bSJames Hogan 		er = kvm_trap_vz_handle_ghfc(cause, opc, vcpu);
1518c992a4f6SJames Hogan 		break;
1519c992a4f6SJames Hogan 	case MIPS_GCTL0_GEXC_GPA:
1520c992a4f6SJames Hogan 		++vcpu->stat.vz_gpa_exits;
1521c992a4f6SJames Hogan 		er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
1522c992a4f6SJames Hogan 						       vcpu);
1523c992a4f6SJames Hogan 		break;
1524c992a4f6SJames Hogan 	default:
1525c992a4f6SJames Hogan 		++vcpu->stat.vz_resvd_exits;
1526c992a4f6SJames Hogan 		er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
1527c992a4f6SJames Hogan 						       vcpu);
1528c992a4f6SJames Hogan 		break;
1529c992a4f6SJames Hogan 
1530c992a4f6SJames Hogan 	}
1531c992a4f6SJames Hogan 
1532c992a4f6SJames Hogan 	if (er == EMULATE_DONE) {
1533c992a4f6SJames Hogan 		ret = RESUME_GUEST;
1534c992a4f6SJames Hogan 	} else if (er == EMULATE_HYPERCALL) {
1535c992a4f6SJames Hogan 		ret = kvm_mips_handle_hypcall(vcpu);
1536c992a4f6SJames Hogan 	} else {
1537c992a4f6SJames Hogan 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1538c992a4f6SJames Hogan 		ret = RESUME_HOST;
1539c992a4f6SJames Hogan 	}
1540c992a4f6SJames Hogan 	return ret;
1541c992a4f6SJames Hogan }
1542c992a4f6SJames Hogan 
1543c992a4f6SJames Hogan /**
15442161ba07SRandy Dunlap  * kvm_trap_vz_handle_cop_unusable() - Guest used unusable coprocessor.
1545c992a4f6SJames Hogan  * @vcpu:	Virtual CPU context.
1546c992a4f6SJames Hogan  *
1547c992a4f6SJames Hogan  * Handle when the guest attempts to use a coprocessor which hasn't been allowed
1548c992a4f6SJames Hogan  * by the root context.
15492161ba07SRandy Dunlap  *
15502161ba07SRandy Dunlap  * Return: value indicating whether to resume the host or the guest
15512161ba07SRandy Dunlap  * 	   (RESUME_HOST or RESUME_GUEST)
1552c992a4f6SJames Hogan  */
kvm_trap_vz_handle_cop_unusable(struct kvm_vcpu * vcpu)1553c992a4f6SJames Hogan static int kvm_trap_vz_handle_cop_unusable(struct kvm_vcpu *vcpu)
1554c992a4f6SJames Hogan {
1555c992a4f6SJames Hogan 	u32 cause = vcpu->arch.host_cp0_cause;
1556c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_FAIL;
1557c992a4f6SJames Hogan 	int ret = RESUME_GUEST;
1558c992a4f6SJames Hogan 
1559c992a4f6SJames Hogan 	if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
1560c992a4f6SJames Hogan 		/*
1561c992a4f6SJames Hogan 		 * If guest FPU not present, the FPU operation should have been
1562c992a4f6SJames Hogan 		 * treated as a reserved instruction!
1563c992a4f6SJames Hogan 		 * If FPU already in use, we shouldn't get this at all.
1564c992a4f6SJames Hogan 		 */
1565c992a4f6SJames Hogan 		if (WARN_ON(!kvm_mips_guest_has_fpu(&vcpu->arch) ||
1566c992a4f6SJames Hogan 			    vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
1567c992a4f6SJames Hogan 			preempt_enable();
1568c992a4f6SJames Hogan 			return EMULATE_FAIL;
1569c992a4f6SJames Hogan 		}
1570c992a4f6SJames Hogan 
1571c992a4f6SJames Hogan 		kvm_own_fpu(vcpu);
1572c992a4f6SJames Hogan 		er = EMULATE_DONE;
1573c992a4f6SJames Hogan 	}
1574c992a4f6SJames Hogan 	/* other coprocessors not handled */
1575c992a4f6SJames Hogan 
1576c992a4f6SJames Hogan 	switch (er) {
1577c992a4f6SJames Hogan 	case EMULATE_DONE:
1578c992a4f6SJames Hogan 		ret = RESUME_GUEST;
1579c992a4f6SJames Hogan 		break;
1580c992a4f6SJames Hogan 
1581c992a4f6SJames Hogan 	case EMULATE_FAIL:
1582c34b26b9STianjia Zhang 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1583c992a4f6SJames Hogan 		ret = RESUME_HOST;
1584c992a4f6SJames Hogan 		break;
1585c992a4f6SJames Hogan 
1586c992a4f6SJames Hogan 	default:
1587c992a4f6SJames Hogan 		BUG();
1588c992a4f6SJames Hogan 	}
1589c992a4f6SJames Hogan 	return ret;
1590c992a4f6SJames Hogan }
1591c992a4f6SJames Hogan 
1592c992a4f6SJames Hogan /**
1593c992a4f6SJames Hogan  * kvm_trap_vz_handle_msa_disabled() - Guest used MSA while disabled in root.
1594c992a4f6SJames Hogan  * @vcpu:	Virtual CPU context.
1595c992a4f6SJames Hogan  *
1596c992a4f6SJames Hogan  * Handle when the guest attempts to use MSA when it is disabled in the root
1597c992a4f6SJames Hogan  * context.
15982161ba07SRandy Dunlap  *
15992161ba07SRandy Dunlap  * Return: value indicating whether to resume the host or the guest
16002161ba07SRandy Dunlap  * 	   (RESUME_HOST or RESUME_GUEST)
1601c992a4f6SJames Hogan  */
kvm_trap_vz_handle_msa_disabled(struct kvm_vcpu * vcpu)1602c992a4f6SJames Hogan static int kvm_trap_vz_handle_msa_disabled(struct kvm_vcpu *vcpu)
1603c992a4f6SJames Hogan {
1604c992a4f6SJames Hogan 	/*
1605c992a4f6SJames Hogan 	 * If MSA not present or not exposed to guest or FR=0, the MSA operation
1606c992a4f6SJames Hogan 	 * should have been treated as a reserved instruction!
1607c992a4f6SJames Hogan 	 * Same if CU1=1, FR=0.
1608c992a4f6SJames Hogan 	 * If MSA already in use, we shouldn't get this at all.
1609c992a4f6SJames Hogan 	 */
1610c992a4f6SJames Hogan 	if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
1611c992a4f6SJames Hogan 	    (read_gc0_status() & (ST0_CU1 | ST0_FR)) == ST0_CU1 ||
1612c992a4f6SJames Hogan 	    !(read_gc0_config5() & MIPS_CONF5_MSAEN) ||
1613c992a4f6SJames Hogan 	    vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
1614c34b26b9STianjia Zhang 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1615c992a4f6SJames Hogan 		return RESUME_HOST;
1616c992a4f6SJames Hogan 	}
1617c992a4f6SJames Hogan 
1618c992a4f6SJames Hogan 	kvm_own_msa(vcpu);
1619c992a4f6SJames Hogan 
1620c992a4f6SJames Hogan 	return RESUME_GUEST;
1621c992a4f6SJames Hogan }
1622c992a4f6SJames Hogan 
kvm_trap_vz_handle_tlb_ld_miss(struct kvm_vcpu * vcpu)1623c992a4f6SJames Hogan static int kvm_trap_vz_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
1624c992a4f6SJames Hogan {
1625c992a4f6SJames Hogan 	struct kvm_run *run = vcpu->run;
1626c992a4f6SJames Hogan 	u32 *opc = (u32 *) vcpu->arch.pc;
1627c992a4f6SJames Hogan 	u32 cause = vcpu->arch.host_cp0_cause;
1628c992a4f6SJames Hogan 	ulong badvaddr = vcpu->arch.host_cp0_badvaddr;
1629c992a4f6SJames Hogan 	union mips_instruction inst;
1630c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
1631c992a4f6SJames Hogan 	int err, ret = RESUME_GUEST;
1632c992a4f6SJames Hogan 
1633c992a4f6SJames Hogan 	if (kvm_mips_handle_vz_root_tlb_fault(badvaddr, vcpu, false)) {
1634c992a4f6SJames Hogan 		/* A code fetch fault doesn't count as an MMIO */
1635c992a4f6SJames Hogan 		if (kvm_is_ifetch_fault(&vcpu->arch)) {
1636c992a4f6SJames Hogan 			run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1637c992a4f6SJames Hogan 			return RESUME_HOST;
1638c992a4f6SJames Hogan 		}
1639c992a4f6SJames Hogan 
1640c992a4f6SJames Hogan 		/* Fetch the instruction */
1641c992a4f6SJames Hogan 		if (cause & CAUSEF_BD)
1642c992a4f6SJames Hogan 			opc += 1;
1643c992a4f6SJames Hogan 		err = kvm_get_badinstr(opc, vcpu, &inst.word);
1644c992a4f6SJames Hogan 		if (err) {
1645c992a4f6SJames Hogan 			run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1646c992a4f6SJames Hogan 			return RESUME_HOST;
1647c992a4f6SJames Hogan 		}
1648c992a4f6SJames Hogan 
1649c992a4f6SJames Hogan 		/* Treat as MMIO */
1650c34b26b9STianjia Zhang 		er = kvm_mips_emulate_load(inst, cause, vcpu);
1651c992a4f6SJames Hogan 		if (er == EMULATE_FAIL) {
1652c992a4f6SJames Hogan 			kvm_err("Guest Emulate Load from MMIO space failed: PC: %p, BadVaddr: %#lx\n",
1653c992a4f6SJames Hogan 				opc, badvaddr);
1654c992a4f6SJames Hogan 			run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1655c992a4f6SJames Hogan 		}
1656c992a4f6SJames Hogan 	}
1657c992a4f6SJames Hogan 
1658c992a4f6SJames Hogan 	if (er == EMULATE_DONE) {
1659c992a4f6SJames Hogan 		ret = RESUME_GUEST;
1660c992a4f6SJames Hogan 	} else if (er == EMULATE_DO_MMIO) {
1661c992a4f6SJames Hogan 		run->exit_reason = KVM_EXIT_MMIO;
1662c992a4f6SJames Hogan 		ret = RESUME_HOST;
1663c992a4f6SJames Hogan 	} else {
1664c992a4f6SJames Hogan 		run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1665c992a4f6SJames Hogan 		ret = RESUME_HOST;
1666c992a4f6SJames Hogan 	}
1667c992a4f6SJames Hogan 	return ret;
1668c992a4f6SJames Hogan }
1669c992a4f6SJames Hogan 
kvm_trap_vz_handle_tlb_st_miss(struct kvm_vcpu * vcpu)1670c992a4f6SJames Hogan static int kvm_trap_vz_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
1671c992a4f6SJames Hogan {
1672c992a4f6SJames Hogan 	struct kvm_run *run = vcpu->run;
1673c992a4f6SJames Hogan 	u32 *opc = (u32 *) vcpu->arch.pc;
1674c992a4f6SJames Hogan 	u32 cause = vcpu->arch.host_cp0_cause;
1675c992a4f6SJames Hogan 	ulong badvaddr = vcpu->arch.host_cp0_badvaddr;
1676c992a4f6SJames Hogan 	union mips_instruction inst;
1677c992a4f6SJames Hogan 	enum emulation_result er = EMULATE_DONE;
1678c992a4f6SJames Hogan 	int err;
1679c992a4f6SJames Hogan 	int ret = RESUME_GUEST;
1680c992a4f6SJames Hogan 
1681c992a4f6SJames Hogan 	/* Just try the access again if we couldn't do the translation */
1682c992a4f6SJames Hogan 	if (kvm_vz_badvaddr_to_gpa(vcpu, badvaddr, &badvaddr))
1683c992a4f6SJames Hogan 		return RESUME_GUEST;
1684c992a4f6SJames Hogan 	vcpu->arch.host_cp0_badvaddr = badvaddr;
1685c992a4f6SJames Hogan 
1686c992a4f6SJames Hogan 	if (kvm_mips_handle_vz_root_tlb_fault(badvaddr, vcpu, true)) {
1687c992a4f6SJames Hogan 		/* Fetch the instruction */
1688c992a4f6SJames Hogan 		if (cause & CAUSEF_BD)
1689c992a4f6SJames Hogan 			opc += 1;
1690c992a4f6SJames Hogan 		err = kvm_get_badinstr(opc, vcpu, &inst.word);
1691c992a4f6SJames Hogan 		if (err) {
1692c992a4f6SJames Hogan 			run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1693c992a4f6SJames Hogan 			return RESUME_HOST;
1694c992a4f6SJames Hogan 		}
1695c992a4f6SJames Hogan 
1696c992a4f6SJames Hogan 		/* Treat as MMIO */
1697c34b26b9STianjia Zhang 		er = kvm_mips_emulate_store(inst, cause, vcpu);
1698c992a4f6SJames Hogan 		if (er == EMULATE_FAIL) {
1699c992a4f6SJames Hogan 			kvm_err("Guest Emulate Store to MMIO space failed: PC: %p, BadVaddr: %#lx\n",
1700c992a4f6SJames Hogan 				opc, badvaddr);
1701c992a4f6SJames Hogan 			run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1702c992a4f6SJames Hogan 		}
1703c992a4f6SJames Hogan 	}
1704c992a4f6SJames Hogan 
1705c992a4f6SJames Hogan 	if (er == EMULATE_DONE) {
1706c992a4f6SJames Hogan 		ret = RESUME_GUEST;
1707c992a4f6SJames Hogan 	} else if (er == EMULATE_DO_MMIO) {
1708c992a4f6SJames Hogan 		run->exit_reason = KVM_EXIT_MMIO;
1709c992a4f6SJames Hogan 		ret = RESUME_HOST;
1710c992a4f6SJames Hogan 	} else {
1711c992a4f6SJames Hogan 		run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1712c992a4f6SJames Hogan 		ret = RESUME_HOST;
1713c992a4f6SJames Hogan 	}
1714c992a4f6SJames Hogan 	return ret;
1715c992a4f6SJames Hogan }
1716c992a4f6SJames Hogan 
1717c992a4f6SJames Hogan static u64 kvm_vz_get_one_regs[] = {
1718c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_INDEX,
1719c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_ENTRYLO0,
1720c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_ENTRYLO1,
1721c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONTEXT,
1722c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_PAGEMASK,
1723c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_PAGEGRAIN,
1724c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_WIRED,
1725c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_HWRENA,
1726c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_BADVADDR,
1727c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_COUNT,
1728c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_ENTRYHI,
1729c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_COMPARE,
1730c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_STATUS,
1731c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_INTCTL,
1732c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CAUSE,
1733c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_EPC,
1734c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_PRID,
1735c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_EBASE,
1736c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONFIG,
1737c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONFIG1,
1738c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONFIG2,
1739c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONFIG3,
1740c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONFIG4,
1741c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_CONFIG5,
17428a5097eeSHuacai Chen 	KVM_REG_MIPS_CP0_CONFIG6,
1743c992a4f6SJames Hogan #ifdef CONFIG_64BIT
1744c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_XCONTEXT,
1745c992a4f6SJames Hogan #endif
1746c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_ERROREPC,
1747c992a4f6SJames Hogan 
1748c992a4f6SJames Hogan 	KVM_REG_MIPS_COUNT_CTL,
1749c992a4f6SJames Hogan 	KVM_REG_MIPS_COUNT_RESUME,
1750c992a4f6SJames Hogan 	KVM_REG_MIPS_COUNT_HZ,
1751c992a4f6SJames Hogan };
1752c992a4f6SJames Hogan 
1753dffe042fSJames Hogan static u64 kvm_vz_get_one_regs_contextconfig[] = {
1754dffe042fSJames Hogan 	KVM_REG_MIPS_CP0_CONTEXTCONFIG,
1755dffe042fSJames Hogan #ifdef CONFIG_64BIT
1756dffe042fSJames Hogan 	KVM_REG_MIPS_CP0_XCONTEXTCONFIG,
1757dffe042fSJames Hogan #endif
1758dffe042fSJames Hogan };
1759dffe042fSJames Hogan 
17604b7de028SJames Hogan static u64 kvm_vz_get_one_regs_segments[] = {
17614b7de028SJames Hogan 	KVM_REG_MIPS_CP0_SEGCTL0,
17624b7de028SJames Hogan 	KVM_REG_MIPS_CP0_SEGCTL1,
17634b7de028SJames Hogan 	KVM_REG_MIPS_CP0_SEGCTL2,
17644b7de028SJames Hogan };
17654b7de028SJames Hogan 
17665a2f352fSJames Hogan static u64 kvm_vz_get_one_regs_htw[] = {
17675a2f352fSJames Hogan 	KVM_REG_MIPS_CP0_PWBASE,
17685a2f352fSJames Hogan 	KVM_REG_MIPS_CP0_PWFIELD,
17695a2f352fSJames Hogan 	KVM_REG_MIPS_CP0_PWSIZE,
17705a2f352fSJames Hogan 	KVM_REG_MIPS_CP0_PWCTL,
17715a2f352fSJames Hogan };
17725a2f352fSJames Hogan 
1773c992a4f6SJames Hogan static u64 kvm_vz_get_one_regs_kscratch[] = {
1774c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_KSCRATCH1,
1775c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_KSCRATCH2,
1776c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_KSCRATCH3,
1777c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_KSCRATCH4,
1778c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_KSCRATCH5,
1779c992a4f6SJames Hogan 	KVM_REG_MIPS_CP0_KSCRATCH6,
1780c992a4f6SJames Hogan };
1781c992a4f6SJames Hogan 
kvm_vz_num_regs(struct kvm_vcpu * vcpu)1782c992a4f6SJames Hogan static unsigned long kvm_vz_num_regs(struct kvm_vcpu *vcpu)
1783c992a4f6SJames Hogan {
1784c992a4f6SJames Hogan 	unsigned long ret;
1785c992a4f6SJames Hogan 
1786c992a4f6SJames Hogan 	ret = ARRAY_SIZE(kvm_vz_get_one_regs);
1787c992a4f6SJames Hogan 	if (cpu_guest_has_userlocal)
1788c992a4f6SJames Hogan 		++ret;
1789edc89260SJames Hogan 	if (cpu_guest_has_badinstr)
1790edc89260SJames Hogan 		++ret;
1791edc89260SJames Hogan 	if (cpu_guest_has_badinstrp)
1792edc89260SJames Hogan 		++ret;
1793dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig)
1794dffe042fSJames Hogan 		ret += ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig);
17954b7de028SJames Hogan 	if (cpu_guest_has_segments)
17964b7de028SJames Hogan 		ret += ARRAY_SIZE(kvm_vz_get_one_regs_segments);
17973210e2c2SHuacai Chen 	if (cpu_guest_has_htw || cpu_guest_has_ldpte)
17985a2f352fSJames Hogan 		ret += ARRAY_SIZE(kvm_vz_get_one_regs_htw);
1799d42a008fSJames Hogan 	if (cpu_guest_has_maar && !cpu_guest_has_dyn_maar)
1800d42a008fSJames Hogan 		ret += 1 + ARRAY_SIZE(vcpu->arch.maar);
1801c992a4f6SJames Hogan 	ret += __arch_hweight8(cpu_data[0].guest.kscratch_mask);
1802c992a4f6SJames Hogan 
1803c992a4f6SJames Hogan 	return ret;
1804c992a4f6SJames Hogan }
1805c992a4f6SJames Hogan 
kvm_vz_copy_reg_indices(struct kvm_vcpu * vcpu,u64 __user * indices)1806c992a4f6SJames Hogan static int kvm_vz_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
1807c992a4f6SJames Hogan {
1808c992a4f6SJames Hogan 	u64 index;
1809c992a4f6SJames Hogan 	unsigned int i;
1810c992a4f6SJames Hogan 
1811c992a4f6SJames Hogan 	if (copy_to_user(indices, kvm_vz_get_one_regs,
1812c992a4f6SJames Hogan 			 sizeof(kvm_vz_get_one_regs)))
1813c992a4f6SJames Hogan 		return -EFAULT;
1814c992a4f6SJames Hogan 	indices += ARRAY_SIZE(kvm_vz_get_one_regs);
1815c992a4f6SJames Hogan 
1816c992a4f6SJames Hogan 	if (cpu_guest_has_userlocal) {
1817c992a4f6SJames Hogan 		index = KVM_REG_MIPS_CP0_USERLOCAL;
1818c992a4f6SJames Hogan 		if (copy_to_user(indices, &index, sizeof(index)))
1819c992a4f6SJames Hogan 			return -EFAULT;
1820c992a4f6SJames Hogan 		++indices;
1821c992a4f6SJames Hogan 	}
1822edc89260SJames Hogan 	if (cpu_guest_has_badinstr) {
1823edc89260SJames Hogan 		index = KVM_REG_MIPS_CP0_BADINSTR;
1824edc89260SJames Hogan 		if (copy_to_user(indices, &index, sizeof(index)))
1825edc89260SJames Hogan 			return -EFAULT;
1826edc89260SJames Hogan 		++indices;
1827edc89260SJames Hogan 	}
1828edc89260SJames Hogan 	if (cpu_guest_has_badinstrp) {
1829edc89260SJames Hogan 		index = KVM_REG_MIPS_CP0_BADINSTRP;
1830edc89260SJames Hogan 		if (copy_to_user(indices, &index, sizeof(index)))
1831edc89260SJames Hogan 			return -EFAULT;
1832edc89260SJames Hogan 		++indices;
1833edc89260SJames Hogan 	}
1834dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig) {
1835dffe042fSJames Hogan 		if (copy_to_user(indices, kvm_vz_get_one_regs_contextconfig,
1836dffe042fSJames Hogan 				 sizeof(kvm_vz_get_one_regs_contextconfig)))
1837dffe042fSJames Hogan 			return -EFAULT;
1838dffe042fSJames Hogan 		indices += ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig);
1839dffe042fSJames Hogan 	}
18404b7de028SJames Hogan 	if (cpu_guest_has_segments) {
18414b7de028SJames Hogan 		if (copy_to_user(indices, kvm_vz_get_one_regs_segments,
18424b7de028SJames Hogan 				 sizeof(kvm_vz_get_one_regs_segments)))
18434b7de028SJames Hogan 			return -EFAULT;
18444b7de028SJames Hogan 		indices += ARRAY_SIZE(kvm_vz_get_one_regs_segments);
18454b7de028SJames Hogan 	}
18463210e2c2SHuacai Chen 	if (cpu_guest_has_htw || cpu_guest_has_ldpte) {
18475a2f352fSJames Hogan 		if (copy_to_user(indices, kvm_vz_get_one_regs_htw,
18485a2f352fSJames Hogan 				 sizeof(kvm_vz_get_one_regs_htw)))
18495a2f352fSJames Hogan 			return -EFAULT;
18505a2f352fSJames Hogan 		indices += ARRAY_SIZE(kvm_vz_get_one_regs_htw);
18515a2f352fSJames Hogan 	}
1852d42a008fSJames Hogan 	if (cpu_guest_has_maar && !cpu_guest_has_dyn_maar) {
1853d42a008fSJames Hogan 		for (i = 0; i < ARRAY_SIZE(vcpu->arch.maar); ++i) {
1854d42a008fSJames Hogan 			index = KVM_REG_MIPS_CP0_MAAR(i);
1855d42a008fSJames Hogan 			if (copy_to_user(indices, &index, sizeof(index)))
1856d42a008fSJames Hogan 				return -EFAULT;
1857d42a008fSJames Hogan 			++indices;
1858d42a008fSJames Hogan 		}
1859d42a008fSJames Hogan 
1860d42a008fSJames Hogan 		index = KVM_REG_MIPS_CP0_MAARI;
1861d42a008fSJames Hogan 		if (copy_to_user(indices, &index, sizeof(index)))
1862d42a008fSJames Hogan 			return -EFAULT;
1863d42a008fSJames Hogan 		++indices;
1864d42a008fSJames Hogan 	}
1865c992a4f6SJames Hogan 	for (i = 0; i < 6; ++i) {
1866c992a4f6SJames Hogan 		if (!cpu_guest_has_kscr(i + 2))
1867c992a4f6SJames Hogan 			continue;
1868c992a4f6SJames Hogan 
1869c992a4f6SJames Hogan 		if (copy_to_user(indices, &kvm_vz_get_one_regs_kscratch[i],
1870c992a4f6SJames Hogan 				 sizeof(kvm_vz_get_one_regs_kscratch[i])))
1871c992a4f6SJames Hogan 			return -EFAULT;
1872c992a4f6SJames Hogan 		++indices;
1873c992a4f6SJames Hogan 	}
1874c992a4f6SJames Hogan 
1875c992a4f6SJames Hogan 	return 0;
1876c992a4f6SJames Hogan }
1877c992a4f6SJames Hogan 
entrylo_kvm_to_user(unsigned long v)1878c992a4f6SJames Hogan static inline s64 entrylo_kvm_to_user(unsigned long v)
1879c992a4f6SJames Hogan {
1880c992a4f6SJames Hogan 	s64 mask, ret = v;
1881c992a4f6SJames Hogan 
1882c992a4f6SJames Hogan 	if (BITS_PER_LONG == 32) {
1883c992a4f6SJames Hogan 		/*
1884c992a4f6SJames Hogan 		 * KVM API exposes 64-bit version of the register, so move the
1885c992a4f6SJames Hogan 		 * RI/XI bits up into place.
1886c992a4f6SJames Hogan 		 */
1887c992a4f6SJames Hogan 		mask = MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI;
1888c992a4f6SJames Hogan 		ret &= ~mask;
1889c992a4f6SJames Hogan 		ret |= ((s64)v & mask) << 32;
1890c992a4f6SJames Hogan 	}
1891c992a4f6SJames Hogan 	return ret;
1892c992a4f6SJames Hogan }
1893c992a4f6SJames Hogan 
entrylo_user_to_kvm(s64 v)1894c992a4f6SJames Hogan static inline unsigned long entrylo_user_to_kvm(s64 v)
1895c992a4f6SJames Hogan {
1896c992a4f6SJames Hogan 	unsigned long mask, ret = v;
1897c992a4f6SJames Hogan 
1898c992a4f6SJames Hogan 	if (BITS_PER_LONG == 32) {
1899c992a4f6SJames Hogan 		/*
1900c992a4f6SJames Hogan 		 * KVM API exposes 64-bit versiono of the register, so move the
1901c992a4f6SJames Hogan 		 * RI/XI bits down into place.
1902c992a4f6SJames Hogan 		 */
1903c992a4f6SJames Hogan 		mask = MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI;
1904c992a4f6SJames Hogan 		ret &= ~mask;
1905c992a4f6SJames Hogan 		ret |= (v >> 32) & mask;
1906c992a4f6SJames Hogan 	}
1907c992a4f6SJames Hogan 	return ret;
1908c992a4f6SJames Hogan }
1909c992a4f6SJames Hogan 
kvm_vz_get_one_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg,s64 * v)1910c992a4f6SJames Hogan static int kvm_vz_get_one_reg(struct kvm_vcpu *vcpu,
1911c992a4f6SJames Hogan 			      const struct kvm_one_reg *reg,
1912c992a4f6SJames Hogan 			      s64 *v)
1913c992a4f6SJames Hogan {
1914c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
1915c992a4f6SJames Hogan 	unsigned int idx;
1916c992a4f6SJames Hogan 
1917c992a4f6SJames Hogan 	switch (reg->id) {
1918c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_INDEX:
1919c992a4f6SJames Hogan 		*v = (long)read_gc0_index();
1920c992a4f6SJames Hogan 		break;
1921c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ENTRYLO0:
1922c992a4f6SJames Hogan 		*v = entrylo_kvm_to_user(read_gc0_entrylo0());
1923c992a4f6SJames Hogan 		break;
1924c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ENTRYLO1:
1925c992a4f6SJames Hogan 		*v = entrylo_kvm_to_user(read_gc0_entrylo1());
1926c992a4f6SJames Hogan 		break;
1927c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONTEXT:
1928c992a4f6SJames Hogan 		*v = (long)read_gc0_context();
1929c992a4f6SJames Hogan 		break;
1930dffe042fSJames Hogan 	case KVM_REG_MIPS_CP0_CONTEXTCONFIG:
1931dffe042fSJames Hogan 		if (!cpu_guest_has_contextconfig)
1932dffe042fSJames Hogan 			return -EINVAL;
1933dffe042fSJames Hogan 		*v = read_gc0_contextconfig();
1934dffe042fSJames Hogan 		break;
1935c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_USERLOCAL:
1936c992a4f6SJames Hogan 		if (!cpu_guest_has_userlocal)
1937c992a4f6SJames Hogan 			return -EINVAL;
1938c992a4f6SJames Hogan 		*v = read_gc0_userlocal();
1939c992a4f6SJames Hogan 		break;
1940dffe042fSJames Hogan #ifdef CONFIG_64BIT
1941dffe042fSJames Hogan 	case KVM_REG_MIPS_CP0_XCONTEXTCONFIG:
1942dffe042fSJames Hogan 		if (!cpu_guest_has_contextconfig)
1943dffe042fSJames Hogan 			return -EINVAL;
1944dffe042fSJames Hogan 		*v = read_gc0_xcontextconfig();
1945dffe042fSJames Hogan 		break;
1946dffe042fSJames Hogan #endif
1947c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_PAGEMASK:
1948c992a4f6SJames Hogan 		*v = (long)read_gc0_pagemask();
1949c992a4f6SJames Hogan 		break;
1950c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_PAGEGRAIN:
1951c992a4f6SJames Hogan 		*v = (long)read_gc0_pagegrain();
1952c992a4f6SJames Hogan 		break;
19534b7de028SJames Hogan 	case KVM_REG_MIPS_CP0_SEGCTL0:
19544b7de028SJames Hogan 		if (!cpu_guest_has_segments)
19554b7de028SJames Hogan 			return -EINVAL;
19564b7de028SJames Hogan 		*v = read_gc0_segctl0();
19574b7de028SJames Hogan 		break;
19584b7de028SJames Hogan 	case KVM_REG_MIPS_CP0_SEGCTL1:
19594b7de028SJames Hogan 		if (!cpu_guest_has_segments)
19604b7de028SJames Hogan 			return -EINVAL;
19614b7de028SJames Hogan 		*v = read_gc0_segctl1();
19624b7de028SJames Hogan 		break;
19634b7de028SJames Hogan 	case KVM_REG_MIPS_CP0_SEGCTL2:
19644b7de028SJames Hogan 		if (!cpu_guest_has_segments)
19654b7de028SJames Hogan 			return -EINVAL;
19664b7de028SJames Hogan 		*v = read_gc0_segctl2();
19674b7de028SJames Hogan 		break;
19685a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWBASE:
19693210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
19705a2f352fSJames Hogan 			return -EINVAL;
19715a2f352fSJames Hogan 		*v = read_gc0_pwbase();
19725a2f352fSJames Hogan 		break;
19735a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWFIELD:
19743210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
19755a2f352fSJames Hogan 			return -EINVAL;
19765a2f352fSJames Hogan 		*v = read_gc0_pwfield();
19775a2f352fSJames Hogan 		break;
19785a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWSIZE:
19793210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
19805a2f352fSJames Hogan 			return -EINVAL;
19815a2f352fSJames Hogan 		*v = read_gc0_pwsize();
19825a2f352fSJames Hogan 		break;
1983c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_WIRED:
1984c992a4f6SJames Hogan 		*v = (long)read_gc0_wired();
1985c992a4f6SJames Hogan 		break;
19865a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWCTL:
19873210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
19885a2f352fSJames Hogan 			return -EINVAL;
19895a2f352fSJames Hogan 		*v = read_gc0_pwctl();
19905a2f352fSJames Hogan 		break;
1991c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_HWRENA:
1992c992a4f6SJames Hogan 		*v = (long)read_gc0_hwrena();
1993c992a4f6SJames Hogan 		break;
1994c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_BADVADDR:
1995c992a4f6SJames Hogan 		*v = (long)read_gc0_badvaddr();
1996c992a4f6SJames Hogan 		break;
1997edc89260SJames Hogan 	case KVM_REG_MIPS_CP0_BADINSTR:
1998edc89260SJames Hogan 		if (!cpu_guest_has_badinstr)
1999edc89260SJames Hogan 			return -EINVAL;
2000edc89260SJames Hogan 		*v = read_gc0_badinstr();
2001edc89260SJames Hogan 		break;
2002edc89260SJames Hogan 	case KVM_REG_MIPS_CP0_BADINSTRP:
2003edc89260SJames Hogan 		if (!cpu_guest_has_badinstrp)
2004edc89260SJames Hogan 			return -EINVAL;
2005edc89260SJames Hogan 		*v = read_gc0_badinstrp();
2006edc89260SJames Hogan 		break;
2007c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_COUNT:
2008c992a4f6SJames Hogan 		*v = kvm_mips_read_count(vcpu);
2009c992a4f6SJames Hogan 		break;
2010c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ENTRYHI:
2011c992a4f6SJames Hogan 		*v = (long)read_gc0_entryhi();
2012c992a4f6SJames Hogan 		break;
2013c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_COMPARE:
2014c992a4f6SJames Hogan 		*v = (long)read_gc0_compare();
2015c992a4f6SJames Hogan 		break;
2016c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_STATUS:
2017c992a4f6SJames Hogan 		*v = (long)read_gc0_status();
2018c992a4f6SJames Hogan 		break;
2019c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_INTCTL:
2020c992a4f6SJames Hogan 		*v = read_gc0_intctl();
2021c992a4f6SJames Hogan 		break;
2022c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CAUSE:
2023c992a4f6SJames Hogan 		*v = (long)read_gc0_cause();
2024c992a4f6SJames Hogan 		break;
2025c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_EPC:
2026c992a4f6SJames Hogan 		*v = (long)read_gc0_epc();
2027c992a4f6SJames Hogan 		break;
2028c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_PRID:
20291f48f9beSJames Hogan 		switch (boot_cpu_type()) {
20301f48f9beSJames Hogan 		case CPU_CAVIUM_OCTEON3:
20311f48f9beSJames Hogan 			/* Octeon III has a read-only guest.PRid */
20321f48f9beSJames Hogan 			*v = read_gc0_prid();
20331f48f9beSJames Hogan 			break;
20341f48f9beSJames Hogan 		default:
2035c992a4f6SJames Hogan 			*v = (long)kvm_read_c0_guest_prid(cop0);
2036c992a4f6SJames Hogan 			break;
20378d345097SZou Wei 		}
20381f48f9beSJames Hogan 		break;
2039c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_EBASE:
2040c992a4f6SJames Hogan 		*v = kvm_vz_read_gc0_ebase();
2041c992a4f6SJames Hogan 		break;
2042c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG:
2043c992a4f6SJames Hogan 		*v = read_gc0_config();
2044c992a4f6SJames Hogan 		break;
2045c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG1:
2046c992a4f6SJames Hogan 		if (!cpu_guest_has_conf1)
2047c992a4f6SJames Hogan 			return -EINVAL;
2048c992a4f6SJames Hogan 		*v = read_gc0_config1();
2049c992a4f6SJames Hogan 		break;
2050c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG2:
2051c992a4f6SJames Hogan 		if (!cpu_guest_has_conf2)
2052c992a4f6SJames Hogan 			return -EINVAL;
2053c992a4f6SJames Hogan 		*v = read_gc0_config2();
2054c992a4f6SJames Hogan 		break;
2055c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG3:
2056c992a4f6SJames Hogan 		if (!cpu_guest_has_conf3)
2057c992a4f6SJames Hogan 			return -EINVAL;
2058c992a4f6SJames Hogan 		*v = read_gc0_config3();
2059c992a4f6SJames Hogan 		break;
2060c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG4:
2061c992a4f6SJames Hogan 		if (!cpu_guest_has_conf4)
2062c992a4f6SJames Hogan 			return -EINVAL;
2063c992a4f6SJames Hogan 		*v = read_gc0_config4();
2064c992a4f6SJames Hogan 		break;
2065c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG5:
2066c992a4f6SJames Hogan 		if (!cpu_guest_has_conf5)
2067c992a4f6SJames Hogan 			return -EINVAL;
2068c992a4f6SJames Hogan 		*v = read_gc0_config5();
2069c992a4f6SJames Hogan 		break;
20708a5097eeSHuacai Chen 	case KVM_REG_MIPS_CP0_CONFIG6:
20718a5097eeSHuacai Chen 		*v = kvm_read_sw_gc0_config6(cop0);
20728a5097eeSHuacai Chen 		break;
2073d42a008fSJames Hogan 	case KVM_REG_MIPS_CP0_MAAR(0) ... KVM_REG_MIPS_CP0_MAAR(0x3f):
2074d42a008fSJames Hogan 		if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
2075d42a008fSJames Hogan 			return -EINVAL;
2076d42a008fSJames Hogan 		idx = reg->id - KVM_REG_MIPS_CP0_MAAR(0);
2077d42a008fSJames Hogan 		if (idx >= ARRAY_SIZE(vcpu->arch.maar))
2078d42a008fSJames Hogan 			return -EINVAL;
2079d42a008fSJames Hogan 		*v = vcpu->arch.maar[idx];
2080d42a008fSJames Hogan 		break;
2081d42a008fSJames Hogan 	case KVM_REG_MIPS_CP0_MAARI:
2082d42a008fSJames Hogan 		if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
2083d42a008fSJames Hogan 			return -EINVAL;
2084d42a008fSJames Hogan 		*v = kvm_read_sw_gc0_maari(&vcpu->arch.cop0);
2085d42a008fSJames Hogan 		break;
2086c992a4f6SJames Hogan #ifdef CONFIG_64BIT
2087c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_XCONTEXT:
2088c992a4f6SJames Hogan 		*v = read_gc0_xcontext();
2089c992a4f6SJames Hogan 		break;
2090c992a4f6SJames Hogan #endif
2091c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ERROREPC:
2092c992a4f6SJames Hogan 		*v = (long)read_gc0_errorepc();
2093c992a4f6SJames Hogan 		break;
2094c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
2095c992a4f6SJames Hogan 		idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
2096c992a4f6SJames Hogan 		if (!cpu_guest_has_kscr(idx))
2097c992a4f6SJames Hogan 			return -EINVAL;
2098c992a4f6SJames Hogan 		switch (idx) {
2099c992a4f6SJames Hogan 		case 2:
2100c992a4f6SJames Hogan 			*v = (long)read_gc0_kscratch1();
2101c992a4f6SJames Hogan 			break;
2102c992a4f6SJames Hogan 		case 3:
2103c992a4f6SJames Hogan 			*v = (long)read_gc0_kscratch2();
2104c992a4f6SJames Hogan 			break;
2105c992a4f6SJames Hogan 		case 4:
2106c992a4f6SJames Hogan 			*v = (long)read_gc0_kscratch3();
2107c992a4f6SJames Hogan 			break;
2108c992a4f6SJames Hogan 		case 5:
2109c992a4f6SJames Hogan 			*v = (long)read_gc0_kscratch4();
2110c992a4f6SJames Hogan 			break;
2111c992a4f6SJames Hogan 		case 6:
2112c992a4f6SJames Hogan 			*v = (long)read_gc0_kscratch5();
2113c992a4f6SJames Hogan 			break;
2114c992a4f6SJames Hogan 		case 7:
2115c992a4f6SJames Hogan 			*v = (long)read_gc0_kscratch6();
2116c992a4f6SJames Hogan 			break;
2117c992a4f6SJames Hogan 		}
2118c992a4f6SJames Hogan 		break;
2119c992a4f6SJames Hogan 	case KVM_REG_MIPS_COUNT_CTL:
2120c992a4f6SJames Hogan 		*v = vcpu->arch.count_ctl;
2121c992a4f6SJames Hogan 		break;
2122c992a4f6SJames Hogan 	case KVM_REG_MIPS_COUNT_RESUME:
2123c992a4f6SJames Hogan 		*v = ktime_to_ns(vcpu->arch.count_resume);
2124c992a4f6SJames Hogan 		break;
2125c992a4f6SJames Hogan 	case KVM_REG_MIPS_COUNT_HZ:
2126c992a4f6SJames Hogan 		*v = vcpu->arch.count_hz;
2127c992a4f6SJames Hogan 		break;
2128c992a4f6SJames Hogan 	default:
2129c992a4f6SJames Hogan 		return -EINVAL;
2130c992a4f6SJames Hogan 	}
2131c992a4f6SJames Hogan 	return 0;
2132c992a4f6SJames Hogan }
2133c992a4f6SJames Hogan 
kvm_vz_set_one_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg,s64 v)2134c992a4f6SJames Hogan static int kvm_vz_set_one_reg(struct kvm_vcpu *vcpu,
2135c992a4f6SJames Hogan 			      const struct kvm_one_reg *reg,
2136c992a4f6SJames Hogan 			      s64 v)
2137c992a4f6SJames Hogan {
2138c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
2139c992a4f6SJames Hogan 	unsigned int idx;
2140c992a4f6SJames Hogan 	int ret = 0;
2141c992a4f6SJames Hogan 	unsigned int cur, change;
2142c992a4f6SJames Hogan 
2143c992a4f6SJames Hogan 	switch (reg->id) {
2144c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_INDEX:
2145c992a4f6SJames Hogan 		write_gc0_index(v);
2146c992a4f6SJames Hogan 		break;
2147c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ENTRYLO0:
2148c992a4f6SJames Hogan 		write_gc0_entrylo0(entrylo_user_to_kvm(v));
2149c992a4f6SJames Hogan 		break;
2150c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ENTRYLO1:
2151c992a4f6SJames Hogan 		write_gc0_entrylo1(entrylo_user_to_kvm(v));
2152c992a4f6SJames Hogan 		break;
2153c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONTEXT:
2154c992a4f6SJames Hogan 		write_gc0_context(v);
2155c992a4f6SJames Hogan 		break;
2156dffe042fSJames Hogan 	case KVM_REG_MIPS_CP0_CONTEXTCONFIG:
2157dffe042fSJames Hogan 		if (!cpu_guest_has_contextconfig)
2158dffe042fSJames Hogan 			return -EINVAL;
2159dffe042fSJames Hogan 		write_gc0_contextconfig(v);
2160dffe042fSJames Hogan 		break;
2161c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_USERLOCAL:
2162c992a4f6SJames Hogan 		if (!cpu_guest_has_userlocal)
2163c992a4f6SJames Hogan 			return -EINVAL;
2164c992a4f6SJames Hogan 		write_gc0_userlocal(v);
2165c992a4f6SJames Hogan 		break;
2166dffe042fSJames Hogan #ifdef CONFIG_64BIT
2167dffe042fSJames Hogan 	case KVM_REG_MIPS_CP0_XCONTEXTCONFIG:
2168dffe042fSJames Hogan 		if (!cpu_guest_has_contextconfig)
2169dffe042fSJames Hogan 			return -EINVAL;
2170dffe042fSJames Hogan 		write_gc0_xcontextconfig(v);
2171dffe042fSJames Hogan 		break;
2172dffe042fSJames Hogan #endif
2173c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_PAGEMASK:
2174c992a4f6SJames Hogan 		write_gc0_pagemask(v);
2175c992a4f6SJames Hogan 		break;
2176c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_PAGEGRAIN:
2177c992a4f6SJames Hogan 		write_gc0_pagegrain(v);
2178c992a4f6SJames Hogan 		break;
21794b7de028SJames Hogan 	case KVM_REG_MIPS_CP0_SEGCTL0:
21804b7de028SJames Hogan 		if (!cpu_guest_has_segments)
21814b7de028SJames Hogan 			return -EINVAL;
21824b7de028SJames Hogan 		write_gc0_segctl0(v);
21834b7de028SJames Hogan 		break;
21844b7de028SJames Hogan 	case KVM_REG_MIPS_CP0_SEGCTL1:
21854b7de028SJames Hogan 		if (!cpu_guest_has_segments)
21864b7de028SJames Hogan 			return -EINVAL;
21874b7de028SJames Hogan 		write_gc0_segctl1(v);
21884b7de028SJames Hogan 		break;
21894b7de028SJames Hogan 	case KVM_REG_MIPS_CP0_SEGCTL2:
21904b7de028SJames Hogan 		if (!cpu_guest_has_segments)
21914b7de028SJames Hogan 			return -EINVAL;
21924b7de028SJames Hogan 		write_gc0_segctl2(v);
21934b7de028SJames Hogan 		break;
21945a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWBASE:
21953210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
21965a2f352fSJames Hogan 			return -EINVAL;
21975a2f352fSJames Hogan 		write_gc0_pwbase(v);
21985a2f352fSJames Hogan 		break;
21995a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWFIELD:
22003210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
22015a2f352fSJames Hogan 			return -EINVAL;
22025a2f352fSJames Hogan 		write_gc0_pwfield(v);
22035a2f352fSJames Hogan 		break;
22045a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWSIZE:
22053210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
22065a2f352fSJames Hogan 			return -EINVAL;
22075a2f352fSJames Hogan 		write_gc0_pwsize(v);
22085a2f352fSJames Hogan 		break;
2209c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_WIRED:
2210c992a4f6SJames Hogan 		change_gc0_wired(MIPSR6_WIRED_WIRED, v);
2211c992a4f6SJames Hogan 		break;
22125a2f352fSJames Hogan 	case KVM_REG_MIPS_CP0_PWCTL:
22133210e2c2SHuacai Chen 		if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
22145a2f352fSJames Hogan 			return -EINVAL;
22155a2f352fSJames Hogan 		write_gc0_pwctl(v);
22165a2f352fSJames Hogan 		break;
2217c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_HWRENA:
2218c992a4f6SJames Hogan 		write_gc0_hwrena(v);
2219c992a4f6SJames Hogan 		break;
2220c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_BADVADDR:
2221c992a4f6SJames Hogan 		write_gc0_badvaddr(v);
2222c992a4f6SJames Hogan 		break;
2223edc89260SJames Hogan 	case KVM_REG_MIPS_CP0_BADINSTR:
2224edc89260SJames Hogan 		if (!cpu_guest_has_badinstr)
2225edc89260SJames Hogan 			return -EINVAL;
2226edc89260SJames Hogan 		write_gc0_badinstr(v);
2227edc89260SJames Hogan 		break;
2228edc89260SJames Hogan 	case KVM_REG_MIPS_CP0_BADINSTRP:
2229edc89260SJames Hogan 		if (!cpu_guest_has_badinstrp)
2230edc89260SJames Hogan 			return -EINVAL;
2231edc89260SJames Hogan 		write_gc0_badinstrp(v);
2232edc89260SJames Hogan 		break;
2233c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_COUNT:
2234c992a4f6SJames Hogan 		kvm_mips_write_count(vcpu, v);
2235c992a4f6SJames Hogan 		break;
2236c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ENTRYHI:
2237c992a4f6SJames Hogan 		write_gc0_entryhi(v);
2238c992a4f6SJames Hogan 		break;
2239c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_COMPARE:
2240c992a4f6SJames Hogan 		kvm_mips_write_compare(vcpu, v, false);
2241c992a4f6SJames Hogan 		break;
2242c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_STATUS:
2243c992a4f6SJames Hogan 		write_gc0_status(v);
2244c992a4f6SJames Hogan 		break;
2245c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_INTCTL:
2246c992a4f6SJames Hogan 		write_gc0_intctl(v);
2247c992a4f6SJames Hogan 		break;
2248c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CAUSE:
2249c992a4f6SJames Hogan 		/*
2250c992a4f6SJames Hogan 		 * If the timer is stopped or started (DC bit) it must look
2251c992a4f6SJames Hogan 		 * atomic with changes to the timer interrupt pending bit (TI).
2252c992a4f6SJames Hogan 		 * A timer interrupt should not happen in between.
2253c992a4f6SJames Hogan 		 */
2254c992a4f6SJames Hogan 		if ((read_gc0_cause() ^ v) & CAUSEF_DC) {
2255c992a4f6SJames Hogan 			if (v & CAUSEF_DC) {
2256c992a4f6SJames Hogan 				/* disable timer first */
2257c992a4f6SJames Hogan 				kvm_mips_count_disable_cause(vcpu);
2258c992a4f6SJames Hogan 				change_gc0_cause((u32)~CAUSEF_DC, v);
2259c992a4f6SJames Hogan 			} else {
2260c992a4f6SJames Hogan 				/* enable timer last */
2261c992a4f6SJames Hogan 				change_gc0_cause((u32)~CAUSEF_DC, v);
2262c992a4f6SJames Hogan 				kvm_mips_count_enable_cause(vcpu);
2263c992a4f6SJames Hogan 			}
2264c992a4f6SJames Hogan 		} else {
2265c992a4f6SJames Hogan 			write_gc0_cause(v);
2266c992a4f6SJames Hogan 		}
2267c992a4f6SJames Hogan 		break;
2268c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_EPC:
2269c992a4f6SJames Hogan 		write_gc0_epc(v);
2270c992a4f6SJames Hogan 		break;
2271c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_PRID:
22721f48f9beSJames Hogan 		switch (boot_cpu_type()) {
22731f48f9beSJames Hogan 		case CPU_CAVIUM_OCTEON3:
22741f48f9beSJames Hogan 			/* Octeon III has a guest.PRid, but its read-only */
22751f48f9beSJames Hogan 			break;
22761f48f9beSJames Hogan 		default:
2277c992a4f6SJames Hogan 			kvm_write_c0_guest_prid(cop0, v);
2278c992a4f6SJames Hogan 			break;
22798d345097SZou Wei 		}
22801f48f9beSJames Hogan 		break;
2281c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_EBASE:
2282c992a4f6SJames Hogan 		kvm_vz_write_gc0_ebase(v);
2283c992a4f6SJames Hogan 		break;
2284c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG:
2285c992a4f6SJames Hogan 		cur = read_gc0_config();
2286c992a4f6SJames Hogan 		change = (cur ^ v) & kvm_vz_config_user_wrmask(vcpu);
2287c992a4f6SJames Hogan 		if (change) {
2288c992a4f6SJames Hogan 			v = cur ^ change;
2289c992a4f6SJames Hogan 			write_gc0_config(v);
2290c992a4f6SJames Hogan 		}
2291c992a4f6SJames Hogan 		break;
2292c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG1:
2293c992a4f6SJames Hogan 		if (!cpu_guest_has_conf1)
2294c992a4f6SJames Hogan 			break;
2295c992a4f6SJames Hogan 		cur = read_gc0_config1();
2296c992a4f6SJames Hogan 		change = (cur ^ v) & kvm_vz_config1_user_wrmask(vcpu);
2297c992a4f6SJames Hogan 		if (change) {
2298c992a4f6SJames Hogan 			v = cur ^ change;
2299c992a4f6SJames Hogan 			write_gc0_config1(v);
2300c992a4f6SJames Hogan 		}
2301c992a4f6SJames Hogan 		break;
2302c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG2:
2303c992a4f6SJames Hogan 		if (!cpu_guest_has_conf2)
2304c992a4f6SJames Hogan 			break;
2305c992a4f6SJames Hogan 		cur = read_gc0_config2();
2306c992a4f6SJames Hogan 		change = (cur ^ v) & kvm_vz_config2_user_wrmask(vcpu);
2307c992a4f6SJames Hogan 		if (change) {
2308c992a4f6SJames Hogan 			v = cur ^ change;
2309c992a4f6SJames Hogan 			write_gc0_config2(v);
2310c992a4f6SJames Hogan 		}
2311c992a4f6SJames Hogan 		break;
2312c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG3:
2313c992a4f6SJames Hogan 		if (!cpu_guest_has_conf3)
2314c992a4f6SJames Hogan 			break;
2315c992a4f6SJames Hogan 		cur = read_gc0_config3();
2316c992a4f6SJames Hogan 		change = (cur ^ v) & kvm_vz_config3_user_wrmask(vcpu);
2317c992a4f6SJames Hogan 		if (change) {
2318c992a4f6SJames Hogan 			v = cur ^ change;
2319c992a4f6SJames Hogan 			write_gc0_config3(v);
2320c992a4f6SJames Hogan 		}
2321c992a4f6SJames Hogan 		break;
2322c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG4:
2323c992a4f6SJames Hogan 		if (!cpu_guest_has_conf4)
2324c992a4f6SJames Hogan 			break;
2325c992a4f6SJames Hogan 		cur = read_gc0_config4();
2326c992a4f6SJames Hogan 		change = (cur ^ v) & kvm_vz_config4_user_wrmask(vcpu);
2327c992a4f6SJames Hogan 		if (change) {
2328c992a4f6SJames Hogan 			v = cur ^ change;
2329c992a4f6SJames Hogan 			write_gc0_config4(v);
2330c992a4f6SJames Hogan 		}
2331c992a4f6SJames Hogan 		break;
2332c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_CONFIG5:
2333c992a4f6SJames Hogan 		if (!cpu_guest_has_conf5)
2334c992a4f6SJames Hogan 			break;
2335c992a4f6SJames Hogan 		cur = read_gc0_config5();
2336c992a4f6SJames Hogan 		change = (cur ^ v) & kvm_vz_config5_user_wrmask(vcpu);
2337c992a4f6SJames Hogan 		if (change) {
2338c992a4f6SJames Hogan 			v = cur ^ change;
2339c992a4f6SJames Hogan 			write_gc0_config5(v);
2340c992a4f6SJames Hogan 		}
2341c992a4f6SJames Hogan 		break;
23428a5097eeSHuacai Chen 	case KVM_REG_MIPS_CP0_CONFIG6:
23438a5097eeSHuacai Chen 		cur = kvm_read_sw_gc0_config6(cop0);
23448a5097eeSHuacai Chen 		change = (cur ^ v) & kvm_vz_config6_user_wrmask(vcpu);
23458a5097eeSHuacai Chen 		if (change) {
23468a5097eeSHuacai Chen 			v = cur ^ change;
23478a5097eeSHuacai Chen 			kvm_write_sw_gc0_config6(cop0, (int)v);
23488a5097eeSHuacai Chen 		}
23498a5097eeSHuacai Chen 		break;
2350d42a008fSJames Hogan 	case KVM_REG_MIPS_CP0_MAAR(0) ... KVM_REG_MIPS_CP0_MAAR(0x3f):
2351d42a008fSJames Hogan 		if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
2352d42a008fSJames Hogan 			return -EINVAL;
2353d42a008fSJames Hogan 		idx = reg->id - KVM_REG_MIPS_CP0_MAAR(0);
2354d42a008fSJames Hogan 		if (idx >= ARRAY_SIZE(vcpu->arch.maar))
2355d42a008fSJames Hogan 			return -EINVAL;
2356d42a008fSJames Hogan 		vcpu->arch.maar[idx] = mips_process_maar(dmtc_op, v);
2357d42a008fSJames Hogan 		break;
2358d42a008fSJames Hogan 	case KVM_REG_MIPS_CP0_MAARI:
2359d42a008fSJames Hogan 		if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
2360d42a008fSJames Hogan 			return -EINVAL;
2361d42a008fSJames Hogan 		kvm_write_maari(vcpu, v);
2362d42a008fSJames Hogan 		break;
2363c992a4f6SJames Hogan #ifdef CONFIG_64BIT
2364c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_XCONTEXT:
2365c992a4f6SJames Hogan 		write_gc0_xcontext(v);
2366c992a4f6SJames Hogan 		break;
2367c992a4f6SJames Hogan #endif
2368c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_ERROREPC:
2369c992a4f6SJames Hogan 		write_gc0_errorepc(v);
2370c992a4f6SJames Hogan 		break;
2371c992a4f6SJames Hogan 	case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
2372c992a4f6SJames Hogan 		idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
2373c992a4f6SJames Hogan 		if (!cpu_guest_has_kscr(idx))
2374c992a4f6SJames Hogan 			return -EINVAL;
2375c992a4f6SJames Hogan 		switch (idx) {
2376c992a4f6SJames Hogan 		case 2:
2377c992a4f6SJames Hogan 			write_gc0_kscratch1(v);
2378c992a4f6SJames Hogan 			break;
2379c992a4f6SJames Hogan 		case 3:
2380c992a4f6SJames Hogan 			write_gc0_kscratch2(v);
2381c992a4f6SJames Hogan 			break;
2382c992a4f6SJames Hogan 		case 4:
2383c992a4f6SJames Hogan 			write_gc0_kscratch3(v);
2384c992a4f6SJames Hogan 			break;
2385c992a4f6SJames Hogan 		case 5:
2386c992a4f6SJames Hogan 			write_gc0_kscratch4(v);
2387c992a4f6SJames Hogan 			break;
2388c992a4f6SJames Hogan 		case 6:
2389c992a4f6SJames Hogan 			write_gc0_kscratch5(v);
2390c992a4f6SJames Hogan 			break;
2391c992a4f6SJames Hogan 		case 7:
2392c992a4f6SJames Hogan 			write_gc0_kscratch6(v);
2393c992a4f6SJames Hogan 			break;
2394c992a4f6SJames Hogan 		}
2395c992a4f6SJames Hogan 		break;
2396c992a4f6SJames Hogan 	case KVM_REG_MIPS_COUNT_CTL:
2397c992a4f6SJames Hogan 		ret = kvm_mips_set_count_ctl(vcpu, v);
2398c992a4f6SJames Hogan 		break;
2399c992a4f6SJames Hogan 	case KVM_REG_MIPS_COUNT_RESUME:
2400c992a4f6SJames Hogan 		ret = kvm_mips_set_count_resume(vcpu, v);
2401c992a4f6SJames Hogan 		break;
2402c992a4f6SJames Hogan 	case KVM_REG_MIPS_COUNT_HZ:
2403c992a4f6SJames Hogan 		ret = kvm_mips_set_count_hz(vcpu, v);
2404c992a4f6SJames Hogan 		break;
2405c992a4f6SJames Hogan 	default:
2406c992a4f6SJames Hogan 		return -EINVAL;
2407c992a4f6SJames Hogan 	}
2408c992a4f6SJames Hogan 	return ret;
2409c992a4f6SJames Hogan }
2410c992a4f6SJames Hogan 
2411c992a4f6SJames Hogan #define guestid_cache(cpu)	(cpu_data[cpu].guestid_cache)
kvm_vz_get_new_guestid(unsigned long cpu,struct kvm_vcpu * vcpu)2412c992a4f6SJames Hogan static void kvm_vz_get_new_guestid(unsigned long cpu, struct kvm_vcpu *vcpu)
2413c992a4f6SJames Hogan {
2414c992a4f6SJames Hogan 	unsigned long guestid = guestid_cache(cpu);
2415c992a4f6SJames Hogan 
2416c992a4f6SJames Hogan 	if (!(++guestid & GUESTID_MASK)) {
2417c992a4f6SJames Hogan 		if (cpu_has_vtag_icache)
2418c992a4f6SJames Hogan 			flush_icache_all();
2419c992a4f6SJames Hogan 
2420c992a4f6SJames Hogan 		if (!guestid)		/* fix version if needed */
2421c992a4f6SJames Hogan 			guestid = GUESTID_FIRST_VERSION;
2422c992a4f6SJames Hogan 
2423c992a4f6SJames Hogan 		++guestid;		/* guestid 0 reserved for root */
2424c992a4f6SJames Hogan 
2425c992a4f6SJames Hogan 		/* start new guestid cycle */
2426c992a4f6SJames Hogan 		kvm_vz_local_flush_roottlb_all_guests();
2427c992a4f6SJames Hogan 		kvm_vz_local_flush_guesttlb_all();
2428c992a4f6SJames Hogan 	}
2429c992a4f6SJames Hogan 
2430c992a4f6SJames Hogan 	guestid_cache(cpu) = guestid;
2431c992a4f6SJames Hogan }
2432c992a4f6SJames Hogan 
2433c992a4f6SJames Hogan /* Returns 1 if the guest TLB may be clobbered */
kvm_vz_check_requests(struct kvm_vcpu * vcpu,int cpu)2434c992a4f6SJames Hogan static int kvm_vz_check_requests(struct kvm_vcpu *vcpu, int cpu)
2435c992a4f6SJames Hogan {
2436c992a4f6SJames Hogan 	int ret = 0;
2437c992a4f6SJames Hogan 	int i;
2438c992a4f6SJames Hogan 
24392fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
2440c992a4f6SJames Hogan 		return 0;
2441c992a4f6SJames Hogan 
2442c992a4f6SJames Hogan 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2443c992a4f6SJames Hogan 		if (cpu_has_guestid) {
2444c992a4f6SJames Hogan 			/* Drop all GuestIDs for this VCPU */
2445c992a4f6SJames Hogan 			for_each_possible_cpu(i)
2446c992a4f6SJames Hogan 				vcpu->arch.vzguestid[i] = 0;
2447c992a4f6SJames Hogan 			/* This will clobber guest TLB contents too */
2448c992a4f6SJames Hogan 			ret = 1;
2449c992a4f6SJames Hogan 		}
2450c992a4f6SJames Hogan 		/*
2451c992a4f6SJames Hogan 		 * For Root ASID Dealias (RAD) we don't do anything here, but we
2452c992a4f6SJames Hogan 		 * still need the request to ensure we recheck asid_flush_mask.
2453c992a4f6SJames Hogan 		 * We can still return 0 as only the root TLB will be affected
2454c992a4f6SJames Hogan 		 * by a root ASID flush.
2455c992a4f6SJames Hogan 		 */
2456c992a4f6SJames Hogan 	}
2457c992a4f6SJames Hogan 
2458c992a4f6SJames Hogan 	return ret;
2459c992a4f6SJames Hogan }
2460c992a4f6SJames Hogan 
kvm_vz_vcpu_save_wired(struct kvm_vcpu * vcpu)2461c992a4f6SJames Hogan static void kvm_vz_vcpu_save_wired(struct kvm_vcpu *vcpu)
2462c992a4f6SJames Hogan {
2463c992a4f6SJames Hogan 	unsigned int wired = read_gc0_wired();
2464c992a4f6SJames Hogan 	struct kvm_mips_tlb *tlbs;
2465c992a4f6SJames Hogan 	int i;
2466c992a4f6SJames Hogan 
2467c992a4f6SJames Hogan 	/* Expand the wired TLB array if necessary */
2468c992a4f6SJames Hogan 	wired &= MIPSR6_WIRED_WIRED;
2469c992a4f6SJames Hogan 	if (wired > vcpu->arch.wired_tlb_limit) {
2470c992a4f6SJames Hogan 		tlbs = krealloc(vcpu->arch.wired_tlb, wired *
2471c992a4f6SJames Hogan 				sizeof(*vcpu->arch.wired_tlb), GFP_ATOMIC);
2472c992a4f6SJames Hogan 		if (WARN_ON(!tlbs)) {
2473c992a4f6SJames Hogan 			/* Save whatever we can */
2474c992a4f6SJames Hogan 			wired = vcpu->arch.wired_tlb_limit;
2475c992a4f6SJames Hogan 		} else {
2476c992a4f6SJames Hogan 			vcpu->arch.wired_tlb = tlbs;
2477c992a4f6SJames Hogan 			vcpu->arch.wired_tlb_limit = wired;
2478c992a4f6SJames Hogan 		}
2479c992a4f6SJames Hogan 	}
2480c992a4f6SJames Hogan 
2481c992a4f6SJames Hogan 	if (wired)
2482c992a4f6SJames Hogan 		/* Save wired entries from the guest TLB */
2483c992a4f6SJames Hogan 		kvm_vz_save_guesttlb(vcpu->arch.wired_tlb, 0, wired);
2484c992a4f6SJames Hogan 	/* Invalidate any dropped entries since last time */
2485c992a4f6SJames Hogan 	for (i = wired; i < vcpu->arch.wired_tlb_used; ++i) {
2486c992a4f6SJames Hogan 		vcpu->arch.wired_tlb[i].tlb_hi = UNIQUE_GUEST_ENTRYHI(i);
2487c992a4f6SJames Hogan 		vcpu->arch.wired_tlb[i].tlb_lo[0] = 0;
2488c992a4f6SJames Hogan 		vcpu->arch.wired_tlb[i].tlb_lo[1] = 0;
2489c992a4f6SJames Hogan 		vcpu->arch.wired_tlb[i].tlb_mask = 0;
2490c992a4f6SJames Hogan 	}
2491c992a4f6SJames Hogan 	vcpu->arch.wired_tlb_used = wired;
2492c992a4f6SJames Hogan }
2493c992a4f6SJames Hogan 
kvm_vz_vcpu_load_wired(struct kvm_vcpu * vcpu)2494c992a4f6SJames Hogan static void kvm_vz_vcpu_load_wired(struct kvm_vcpu *vcpu)
2495c992a4f6SJames Hogan {
2496c992a4f6SJames Hogan 	/* Load wired entries into the guest TLB */
2497c992a4f6SJames Hogan 	if (vcpu->arch.wired_tlb)
2498c992a4f6SJames Hogan 		kvm_vz_load_guesttlb(vcpu->arch.wired_tlb, 0,
2499c992a4f6SJames Hogan 				     vcpu->arch.wired_tlb_used);
2500c992a4f6SJames Hogan }
2501c992a4f6SJames Hogan 
kvm_vz_vcpu_load_tlb(struct kvm_vcpu * vcpu,int cpu)2502c992a4f6SJames Hogan static void kvm_vz_vcpu_load_tlb(struct kvm_vcpu *vcpu, int cpu)
2503c992a4f6SJames Hogan {
2504c992a4f6SJames Hogan 	struct kvm *kvm = vcpu->kvm;
2505c992a4f6SJames Hogan 	struct mm_struct *gpa_mm = &kvm->arch.gpa_mm;
2506c992a4f6SJames Hogan 	bool migrated;
2507c992a4f6SJames Hogan 
2508c992a4f6SJames Hogan 	/*
2509c992a4f6SJames Hogan 	 * Are we entering guest context on a different CPU to last time?
2510c992a4f6SJames Hogan 	 * If so, the VCPU's guest TLB state on this CPU may be stale.
2511c992a4f6SJames Hogan 	 */
2512c992a4f6SJames Hogan 	migrated = (vcpu->arch.last_exec_cpu != cpu);
2513c992a4f6SJames Hogan 	vcpu->arch.last_exec_cpu = cpu;
2514c992a4f6SJames Hogan 
2515c992a4f6SJames Hogan 	/*
2516c992a4f6SJames Hogan 	 * A vcpu's GuestID is set in GuestCtl1.ID when the vcpu is loaded and
2517c992a4f6SJames Hogan 	 * remains set until another vcpu is loaded in.  As a rule GuestRID
2518c992a4f6SJames Hogan 	 * remains zeroed when in root context unless the kernel is busy
2519c992a4f6SJames Hogan 	 * manipulating guest tlb entries.
2520c992a4f6SJames Hogan 	 */
2521c992a4f6SJames Hogan 	if (cpu_has_guestid) {
2522c992a4f6SJames Hogan 		/*
2523c992a4f6SJames Hogan 		 * Check if our GuestID is of an older version and thus invalid.
2524c992a4f6SJames Hogan 		 *
2525c992a4f6SJames Hogan 		 * We also discard the stored GuestID if we've executed on
2526c992a4f6SJames Hogan 		 * another CPU, as the guest mappings may have changed without
2527c992a4f6SJames Hogan 		 * hypervisor knowledge.
2528c992a4f6SJames Hogan 		 */
2529c992a4f6SJames Hogan 		if (migrated ||
2530c992a4f6SJames Hogan 		    (vcpu->arch.vzguestid[cpu] ^ guestid_cache(cpu)) &
2531c992a4f6SJames Hogan 					GUESTID_VERSION_MASK) {
2532c992a4f6SJames Hogan 			kvm_vz_get_new_guestid(cpu, vcpu);
2533c992a4f6SJames Hogan 			vcpu->arch.vzguestid[cpu] = guestid_cache(cpu);
2534c992a4f6SJames Hogan 			trace_kvm_guestid_change(vcpu,
2535c992a4f6SJames Hogan 						 vcpu->arch.vzguestid[cpu]);
2536c992a4f6SJames Hogan 		}
2537c992a4f6SJames Hogan 
2538c992a4f6SJames Hogan 		/* Restore GuestID */
2539c992a4f6SJames Hogan 		change_c0_guestctl1(GUESTID_MASK, vcpu->arch.vzguestid[cpu]);
2540c992a4f6SJames Hogan 	} else {
2541c992a4f6SJames Hogan 		/*
2542c992a4f6SJames Hogan 		 * The Guest TLB only stores a single guest's TLB state, so
2543c992a4f6SJames Hogan 		 * flush it if another VCPU has executed on this CPU.
2544c992a4f6SJames Hogan 		 *
2545c992a4f6SJames Hogan 		 * We also flush if we've executed on another CPU, as the guest
2546c992a4f6SJames Hogan 		 * mappings may have changed without hypervisor knowledge.
2547c992a4f6SJames Hogan 		 */
2548c992a4f6SJames Hogan 		if (migrated || last_exec_vcpu[cpu] != vcpu)
2549c992a4f6SJames Hogan 			kvm_vz_local_flush_guesttlb_all();
2550c992a4f6SJames Hogan 		last_exec_vcpu[cpu] = vcpu;
2551c992a4f6SJames Hogan 
2552c992a4f6SJames Hogan 		/*
2553c992a4f6SJames Hogan 		 * Root ASID dealiases guest GPA mappings in the root TLB.
2554c992a4f6SJames Hogan 		 * Allocate new root ASID if needed.
2555c992a4f6SJames Hogan 		 */
255642d5b846SPaul Burton 		if (cpumask_test_and_clear_cpu(cpu, &kvm->arch.asid_flush_mask))
25574739f7ddSPaul Burton 			get_new_mmu_context(gpa_mm);
255842d5b846SPaul Burton 		else
255942d5b846SPaul Burton 			check_mmu_context(gpa_mm);
2560c992a4f6SJames Hogan 	}
2561c992a4f6SJames Hogan }
2562c992a4f6SJames Hogan 
kvm_vz_vcpu_load(struct kvm_vcpu * vcpu,int cpu)2563c992a4f6SJames Hogan static int kvm_vz_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2564c992a4f6SJames Hogan {
2565c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
2566c992a4f6SJames Hogan 	bool migrated, all;
2567c992a4f6SJames Hogan 
2568c992a4f6SJames Hogan 	/*
2569c992a4f6SJames Hogan 	 * Have we migrated to a different CPU?
2570c992a4f6SJames Hogan 	 * If so, any old guest TLB state may be stale.
2571c992a4f6SJames Hogan 	 */
2572c992a4f6SJames Hogan 	migrated = (vcpu->arch.last_sched_cpu != cpu);
2573c992a4f6SJames Hogan 
2574c992a4f6SJames Hogan 	/*
2575c992a4f6SJames Hogan 	 * Was this the last VCPU to run on this CPU?
2576c992a4f6SJames Hogan 	 * If not, any old guest state from this VCPU will have been clobbered.
2577c992a4f6SJames Hogan 	 */
2578c992a4f6SJames Hogan 	all = migrated || (last_vcpu[cpu] != vcpu);
2579c992a4f6SJames Hogan 	last_vcpu[cpu] = vcpu;
2580c992a4f6SJames Hogan 
2581c992a4f6SJames Hogan 	/*
2582c992a4f6SJames Hogan 	 * Restore CP0_Wired unconditionally as we clear it after use, and
2583c992a4f6SJames Hogan 	 * restore wired guest TLB entries (while in guest context).
2584c992a4f6SJames Hogan 	 */
2585c992a4f6SJames Hogan 	kvm_restore_gc0_wired(cop0);
2586c992a4f6SJames Hogan 	if (current->flags & PF_VCPU) {
2587c992a4f6SJames Hogan 		tlbw_use_hazard();
2588c992a4f6SJames Hogan 		kvm_vz_vcpu_load_tlb(vcpu, cpu);
2589c992a4f6SJames Hogan 		kvm_vz_vcpu_load_wired(vcpu);
2590c992a4f6SJames Hogan 	}
2591c992a4f6SJames Hogan 
2592c992a4f6SJames Hogan 	/*
2593c992a4f6SJames Hogan 	 * Restore timer state regardless, as e.g. Cause.TI can change over time
2594c992a4f6SJames Hogan 	 * if left unmaintained.
2595c992a4f6SJames Hogan 	 */
2596c992a4f6SJames Hogan 	kvm_vz_restore_timer(vcpu);
2597c992a4f6SJames Hogan 
2598edec9d7bSJames Hogan 	/* Set MC bit if we want to trace guest mode changes */
2599edec9d7bSJames Hogan 	if (kvm_trace_guest_mode_change)
2600edec9d7bSJames Hogan 		set_c0_guestctl0(MIPS_GCTL0_MC);
2601edec9d7bSJames Hogan 	else
2602edec9d7bSJames Hogan 		clear_c0_guestctl0(MIPS_GCTL0_MC);
2603edec9d7bSJames Hogan 
2604c992a4f6SJames Hogan 	/* Don't bother restoring registers multiple times unless necessary */
2605c992a4f6SJames Hogan 	if (!all)
2606c992a4f6SJames Hogan 		return 0;
2607c992a4f6SJames Hogan 
2608c992a4f6SJames Hogan 	/*
2609c992a4f6SJames Hogan 	 * Restore config registers first, as some implementations restrict
2610c992a4f6SJames Hogan 	 * writes to other registers when the corresponding feature bits aren't
2611c992a4f6SJames Hogan 	 * set. For example Status.CU1 cannot be set unless Config1.FP is set.
2612c992a4f6SJames Hogan 	 */
2613c992a4f6SJames Hogan 	kvm_restore_gc0_config(cop0);
2614c992a4f6SJames Hogan 	if (cpu_guest_has_conf1)
2615c992a4f6SJames Hogan 		kvm_restore_gc0_config1(cop0);
2616c992a4f6SJames Hogan 	if (cpu_guest_has_conf2)
2617c992a4f6SJames Hogan 		kvm_restore_gc0_config2(cop0);
2618c992a4f6SJames Hogan 	if (cpu_guest_has_conf3)
2619c992a4f6SJames Hogan 		kvm_restore_gc0_config3(cop0);
2620c992a4f6SJames Hogan 	if (cpu_guest_has_conf4)
2621c992a4f6SJames Hogan 		kvm_restore_gc0_config4(cop0);
2622c992a4f6SJames Hogan 	if (cpu_guest_has_conf5)
2623c992a4f6SJames Hogan 		kvm_restore_gc0_config5(cop0);
2624c992a4f6SJames Hogan 	if (cpu_guest_has_conf6)
2625c992a4f6SJames Hogan 		kvm_restore_gc0_config6(cop0);
2626c992a4f6SJames Hogan 	if (cpu_guest_has_conf7)
2627c992a4f6SJames Hogan 		kvm_restore_gc0_config7(cop0);
2628c992a4f6SJames Hogan 
2629c992a4f6SJames Hogan 	kvm_restore_gc0_index(cop0);
2630c992a4f6SJames Hogan 	kvm_restore_gc0_entrylo0(cop0);
2631c992a4f6SJames Hogan 	kvm_restore_gc0_entrylo1(cop0);
2632c992a4f6SJames Hogan 	kvm_restore_gc0_context(cop0);
2633dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig)
2634dffe042fSJames Hogan 		kvm_restore_gc0_contextconfig(cop0);
2635c992a4f6SJames Hogan #ifdef CONFIG_64BIT
2636c992a4f6SJames Hogan 	kvm_restore_gc0_xcontext(cop0);
2637dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig)
2638dffe042fSJames Hogan 		kvm_restore_gc0_xcontextconfig(cop0);
2639c992a4f6SJames Hogan #endif
2640c992a4f6SJames Hogan 	kvm_restore_gc0_pagemask(cop0);
2641c992a4f6SJames Hogan 	kvm_restore_gc0_pagegrain(cop0);
2642c992a4f6SJames Hogan 	kvm_restore_gc0_hwrena(cop0);
2643c992a4f6SJames Hogan 	kvm_restore_gc0_badvaddr(cop0);
2644c992a4f6SJames Hogan 	kvm_restore_gc0_entryhi(cop0);
2645c992a4f6SJames Hogan 	kvm_restore_gc0_status(cop0);
2646c992a4f6SJames Hogan 	kvm_restore_gc0_intctl(cop0);
2647c992a4f6SJames Hogan 	kvm_restore_gc0_epc(cop0);
2648c992a4f6SJames Hogan 	kvm_vz_write_gc0_ebase(kvm_read_sw_gc0_ebase(cop0));
2649c992a4f6SJames Hogan 	if (cpu_guest_has_userlocal)
2650c992a4f6SJames Hogan 		kvm_restore_gc0_userlocal(cop0);
2651c992a4f6SJames Hogan 
2652c992a4f6SJames Hogan 	kvm_restore_gc0_errorepc(cop0);
2653c992a4f6SJames Hogan 
2654c992a4f6SJames Hogan 	/* restore KScratch registers if enabled in guest */
2655c992a4f6SJames Hogan 	if (cpu_guest_has_conf4) {
2656c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(2))
2657c992a4f6SJames Hogan 			kvm_restore_gc0_kscratch1(cop0);
2658c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(3))
2659c992a4f6SJames Hogan 			kvm_restore_gc0_kscratch2(cop0);
2660c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(4))
2661c992a4f6SJames Hogan 			kvm_restore_gc0_kscratch3(cop0);
2662c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(5))
2663c992a4f6SJames Hogan 			kvm_restore_gc0_kscratch4(cop0);
2664c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(6))
2665c992a4f6SJames Hogan 			kvm_restore_gc0_kscratch5(cop0);
2666c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(7))
2667c992a4f6SJames Hogan 			kvm_restore_gc0_kscratch6(cop0);
2668c992a4f6SJames Hogan 	}
2669c992a4f6SJames Hogan 
2670edc89260SJames Hogan 	if (cpu_guest_has_badinstr)
2671edc89260SJames Hogan 		kvm_restore_gc0_badinstr(cop0);
2672edc89260SJames Hogan 	if (cpu_guest_has_badinstrp)
2673edc89260SJames Hogan 		kvm_restore_gc0_badinstrp(cop0);
2674edc89260SJames Hogan 
26754b7de028SJames Hogan 	if (cpu_guest_has_segments) {
26764b7de028SJames Hogan 		kvm_restore_gc0_segctl0(cop0);
26774b7de028SJames Hogan 		kvm_restore_gc0_segctl1(cop0);
26784b7de028SJames Hogan 		kvm_restore_gc0_segctl2(cop0);
26794b7de028SJames Hogan 	}
26804b7de028SJames Hogan 
26815a2f352fSJames Hogan 	/* restore HTW registers */
26823210e2c2SHuacai Chen 	if (cpu_guest_has_htw || cpu_guest_has_ldpte) {
26835a2f352fSJames Hogan 		kvm_restore_gc0_pwbase(cop0);
26845a2f352fSJames Hogan 		kvm_restore_gc0_pwfield(cop0);
26855a2f352fSJames Hogan 		kvm_restore_gc0_pwsize(cop0);
26865a2f352fSJames Hogan 		kvm_restore_gc0_pwctl(cop0);
26875a2f352fSJames Hogan 	}
26885a2f352fSJames Hogan 
2689c992a4f6SJames Hogan 	/* restore Root.GuestCtl2 from unused Guest guestctl2 register */
2690c992a4f6SJames Hogan 	if (cpu_has_guestctl2)
2691c992a4f6SJames Hogan 		write_c0_guestctl2(
2692c992a4f6SJames Hogan 			cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL]);
2693c992a4f6SJames Hogan 
2694273819a6SJames Hogan 	/*
2695273819a6SJames Hogan 	 * We should clear linked load bit to break interrupted atomics. This
2696273819a6SJames Hogan 	 * prevents a SC on the next VCPU from succeeding by matching a LL on
2697273819a6SJames Hogan 	 * the previous VCPU.
2698273819a6SJames Hogan 	 */
26990f78355cSHuacai Chen 	if (vcpu->kvm->created_vcpus > 1)
2700273819a6SJames Hogan 		write_gc0_lladdr(0);
2701273819a6SJames Hogan 
2702c992a4f6SJames Hogan 	return 0;
2703c992a4f6SJames Hogan }
2704c992a4f6SJames Hogan 
kvm_vz_vcpu_put(struct kvm_vcpu * vcpu,int cpu)2705c992a4f6SJames Hogan static int kvm_vz_vcpu_put(struct kvm_vcpu *vcpu, int cpu)
2706c992a4f6SJames Hogan {
2707c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
2708c992a4f6SJames Hogan 
2709c992a4f6SJames Hogan 	if (current->flags & PF_VCPU)
2710c992a4f6SJames Hogan 		kvm_vz_vcpu_save_wired(vcpu);
2711c992a4f6SJames Hogan 
2712c992a4f6SJames Hogan 	kvm_lose_fpu(vcpu);
2713c992a4f6SJames Hogan 
2714c992a4f6SJames Hogan 	kvm_save_gc0_index(cop0);
2715c992a4f6SJames Hogan 	kvm_save_gc0_entrylo0(cop0);
2716c992a4f6SJames Hogan 	kvm_save_gc0_entrylo1(cop0);
2717c992a4f6SJames Hogan 	kvm_save_gc0_context(cop0);
2718dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig)
2719dffe042fSJames Hogan 		kvm_save_gc0_contextconfig(cop0);
2720c992a4f6SJames Hogan #ifdef CONFIG_64BIT
2721c992a4f6SJames Hogan 	kvm_save_gc0_xcontext(cop0);
2722dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig)
2723dffe042fSJames Hogan 		kvm_save_gc0_xcontextconfig(cop0);
2724c992a4f6SJames Hogan #endif
2725c992a4f6SJames Hogan 	kvm_save_gc0_pagemask(cop0);
2726c992a4f6SJames Hogan 	kvm_save_gc0_pagegrain(cop0);
2727c992a4f6SJames Hogan 	kvm_save_gc0_wired(cop0);
2728c992a4f6SJames Hogan 	/* allow wired TLB entries to be overwritten */
2729c992a4f6SJames Hogan 	clear_gc0_wired(MIPSR6_WIRED_WIRED);
2730c992a4f6SJames Hogan 	kvm_save_gc0_hwrena(cop0);
2731c992a4f6SJames Hogan 	kvm_save_gc0_badvaddr(cop0);
2732c992a4f6SJames Hogan 	kvm_save_gc0_entryhi(cop0);
2733c992a4f6SJames Hogan 	kvm_save_gc0_status(cop0);
2734c992a4f6SJames Hogan 	kvm_save_gc0_intctl(cop0);
2735c992a4f6SJames Hogan 	kvm_save_gc0_epc(cop0);
2736c992a4f6SJames Hogan 	kvm_write_sw_gc0_ebase(cop0, kvm_vz_read_gc0_ebase());
2737c992a4f6SJames Hogan 	if (cpu_guest_has_userlocal)
2738c992a4f6SJames Hogan 		kvm_save_gc0_userlocal(cop0);
2739c992a4f6SJames Hogan 
2740c992a4f6SJames Hogan 	/* only save implemented config registers */
2741c992a4f6SJames Hogan 	kvm_save_gc0_config(cop0);
2742c992a4f6SJames Hogan 	if (cpu_guest_has_conf1)
2743c992a4f6SJames Hogan 		kvm_save_gc0_config1(cop0);
2744c992a4f6SJames Hogan 	if (cpu_guest_has_conf2)
2745c992a4f6SJames Hogan 		kvm_save_gc0_config2(cop0);
2746c992a4f6SJames Hogan 	if (cpu_guest_has_conf3)
2747c992a4f6SJames Hogan 		kvm_save_gc0_config3(cop0);
2748c992a4f6SJames Hogan 	if (cpu_guest_has_conf4)
2749c992a4f6SJames Hogan 		kvm_save_gc0_config4(cop0);
2750c992a4f6SJames Hogan 	if (cpu_guest_has_conf5)
2751c992a4f6SJames Hogan 		kvm_save_gc0_config5(cop0);
2752c992a4f6SJames Hogan 	if (cpu_guest_has_conf6)
2753c992a4f6SJames Hogan 		kvm_save_gc0_config6(cop0);
2754c992a4f6SJames Hogan 	if (cpu_guest_has_conf7)
2755c992a4f6SJames Hogan 		kvm_save_gc0_config7(cop0);
2756c992a4f6SJames Hogan 
2757c992a4f6SJames Hogan 	kvm_save_gc0_errorepc(cop0);
2758c992a4f6SJames Hogan 
2759c992a4f6SJames Hogan 	/* save KScratch registers if enabled in guest */
2760c992a4f6SJames Hogan 	if (cpu_guest_has_conf4) {
2761c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(2))
2762c992a4f6SJames Hogan 			kvm_save_gc0_kscratch1(cop0);
2763c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(3))
2764c992a4f6SJames Hogan 			kvm_save_gc0_kscratch2(cop0);
2765c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(4))
2766c992a4f6SJames Hogan 			kvm_save_gc0_kscratch3(cop0);
2767c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(5))
2768c992a4f6SJames Hogan 			kvm_save_gc0_kscratch4(cop0);
2769c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(6))
2770c992a4f6SJames Hogan 			kvm_save_gc0_kscratch5(cop0);
2771c992a4f6SJames Hogan 		if (cpu_guest_has_kscr(7))
2772c992a4f6SJames Hogan 			kvm_save_gc0_kscratch6(cop0);
2773c992a4f6SJames Hogan 	}
2774c992a4f6SJames Hogan 
2775edc89260SJames Hogan 	if (cpu_guest_has_badinstr)
2776edc89260SJames Hogan 		kvm_save_gc0_badinstr(cop0);
2777edc89260SJames Hogan 	if (cpu_guest_has_badinstrp)
2778edc89260SJames Hogan 		kvm_save_gc0_badinstrp(cop0);
2779edc89260SJames Hogan 
27804b7de028SJames Hogan 	if (cpu_guest_has_segments) {
27814b7de028SJames Hogan 		kvm_save_gc0_segctl0(cop0);
27824b7de028SJames Hogan 		kvm_save_gc0_segctl1(cop0);
27834b7de028SJames Hogan 		kvm_save_gc0_segctl2(cop0);
27844b7de028SJames Hogan 	}
27854b7de028SJames Hogan 
27865a2f352fSJames Hogan 	/* save HTW registers if enabled in guest */
27873210e2c2SHuacai Chen 	if (cpu_guest_has_ldpte || (cpu_guest_has_htw &&
27883210e2c2SHuacai Chen 	    kvm_read_sw_gc0_config3(cop0) & MIPS_CONF3_PW)) {
27895a2f352fSJames Hogan 		kvm_save_gc0_pwbase(cop0);
27905a2f352fSJames Hogan 		kvm_save_gc0_pwfield(cop0);
27915a2f352fSJames Hogan 		kvm_save_gc0_pwsize(cop0);
27925a2f352fSJames Hogan 		kvm_save_gc0_pwctl(cop0);
27935a2f352fSJames Hogan 	}
27945a2f352fSJames Hogan 
2795c992a4f6SJames Hogan 	kvm_vz_save_timer(vcpu);
2796c992a4f6SJames Hogan 
2797c992a4f6SJames Hogan 	/* save Root.GuestCtl2 in unused Guest guestctl2 register */
2798c992a4f6SJames Hogan 	if (cpu_has_guestctl2)
2799c992a4f6SJames Hogan 		cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL] =
2800c992a4f6SJames Hogan 			read_c0_guestctl2();
2801c992a4f6SJames Hogan 
2802c992a4f6SJames Hogan 	return 0;
2803c992a4f6SJames Hogan }
2804c992a4f6SJames Hogan 
2805c992a4f6SJames Hogan /**
2806c992a4f6SJames Hogan  * kvm_vz_resize_guest_vtlb() - Attempt to resize guest VTLB.
2807c992a4f6SJames Hogan  * @size:	Number of guest VTLB entries (0 < @size <= root VTLB entries).
2808c992a4f6SJames Hogan  *
2809c992a4f6SJames Hogan  * Attempt to resize the guest VTLB by writing guest Config registers. This is
2810c992a4f6SJames Hogan  * necessary for cores with a shared root/guest TLB to avoid overlap with wired
2811c992a4f6SJames Hogan  * entries in the root VTLB.
2812c992a4f6SJames Hogan  *
2813c992a4f6SJames Hogan  * Returns:	The resulting guest VTLB size.
2814c992a4f6SJames Hogan  */
kvm_vz_resize_guest_vtlb(unsigned int size)2815c992a4f6SJames Hogan static unsigned int kvm_vz_resize_guest_vtlb(unsigned int size)
2816c992a4f6SJames Hogan {
2817c992a4f6SJames Hogan 	unsigned int config4 = 0, ret = 0, limit;
2818c992a4f6SJames Hogan 
2819c992a4f6SJames Hogan 	/* Write MMUSize - 1 into guest Config registers */
2820c992a4f6SJames Hogan 	if (cpu_guest_has_conf1)
2821c992a4f6SJames Hogan 		change_gc0_config1(MIPS_CONF1_TLBS,
2822c992a4f6SJames Hogan 				   (size - 1) << MIPS_CONF1_TLBS_SHIFT);
2823c992a4f6SJames Hogan 	if (cpu_guest_has_conf4) {
2824c992a4f6SJames Hogan 		config4 = read_gc0_config4();
2825c992a4f6SJames Hogan 		if (cpu_has_mips_r6 || (config4 & MIPS_CONF4_MMUEXTDEF) ==
2826c992a4f6SJames Hogan 		    MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT) {
2827c992a4f6SJames Hogan 			config4 &= ~MIPS_CONF4_VTLBSIZEEXT;
2828c992a4f6SJames Hogan 			config4 |= ((size - 1) >> MIPS_CONF1_TLBS_SIZE) <<
2829c992a4f6SJames Hogan 				MIPS_CONF4_VTLBSIZEEXT_SHIFT;
2830c992a4f6SJames Hogan 		} else if ((config4 & MIPS_CONF4_MMUEXTDEF) ==
2831c992a4f6SJames Hogan 			   MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT) {
2832c992a4f6SJames Hogan 			config4 &= ~MIPS_CONF4_MMUSIZEEXT;
2833c992a4f6SJames Hogan 			config4 |= ((size - 1) >> MIPS_CONF1_TLBS_SIZE) <<
2834c992a4f6SJames Hogan 				MIPS_CONF4_MMUSIZEEXT_SHIFT;
2835c992a4f6SJames Hogan 		}
2836c992a4f6SJames Hogan 		write_gc0_config4(config4);
2837c992a4f6SJames Hogan 	}
2838c992a4f6SJames Hogan 
2839c992a4f6SJames Hogan 	/*
2840c992a4f6SJames Hogan 	 * Set Guest.Wired.Limit = 0 (no limit up to Guest.MMUSize-1), unless it
2841c992a4f6SJames Hogan 	 * would exceed Root.Wired.Limit (clearing Guest.Wired.Wired so write
2842c992a4f6SJames Hogan 	 * not dropped)
2843c992a4f6SJames Hogan 	 */
2844c992a4f6SJames Hogan 	if (cpu_has_mips_r6) {
2845c992a4f6SJames Hogan 		limit = (read_c0_wired() & MIPSR6_WIRED_LIMIT) >>
2846c992a4f6SJames Hogan 						MIPSR6_WIRED_LIMIT_SHIFT;
2847c992a4f6SJames Hogan 		if (size - 1 <= limit)
2848c992a4f6SJames Hogan 			limit = 0;
2849c992a4f6SJames Hogan 		write_gc0_wired(limit << MIPSR6_WIRED_LIMIT_SHIFT);
2850c992a4f6SJames Hogan 	}
2851c992a4f6SJames Hogan 
2852c992a4f6SJames Hogan 	/* Read back MMUSize - 1 */
2853c992a4f6SJames Hogan 	back_to_back_c0_hazard();
2854c992a4f6SJames Hogan 	if (cpu_guest_has_conf1)
2855c992a4f6SJames Hogan 		ret = (read_gc0_config1() & MIPS_CONF1_TLBS) >>
2856c992a4f6SJames Hogan 						MIPS_CONF1_TLBS_SHIFT;
2857c992a4f6SJames Hogan 	if (config4) {
2858c992a4f6SJames Hogan 		if (cpu_has_mips_r6 || (config4 & MIPS_CONF4_MMUEXTDEF) ==
2859c992a4f6SJames Hogan 		    MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT)
2860c992a4f6SJames Hogan 			ret |= ((config4 & MIPS_CONF4_VTLBSIZEEXT) >>
2861c992a4f6SJames Hogan 				MIPS_CONF4_VTLBSIZEEXT_SHIFT) <<
2862c992a4f6SJames Hogan 				MIPS_CONF1_TLBS_SIZE;
2863c992a4f6SJames Hogan 		else if ((config4 & MIPS_CONF4_MMUEXTDEF) ==
2864c992a4f6SJames Hogan 			 MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT)
2865c992a4f6SJames Hogan 			ret |= ((config4 & MIPS_CONF4_MMUSIZEEXT) >>
2866c992a4f6SJames Hogan 				MIPS_CONF4_MMUSIZEEXT_SHIFT) <<
2867c992a4f6SJames Hogan 				MIPS_CONF1_TLBS_SIZE;
2868c992a4f6SJames Hogan 	}
2869c992a4f6SJames Hogan 	return ret + 1;
2870c992a4f6SJames Hogan }
2871c992a4f6SJames Hogan 
kvm_vz_hardware_enable(void)2872c992a4f6SJames Hogan static int kvm_vz_hardware_enable(void)
2873c992a4f6SJames Hogan {
2874c992a4f6SJames Hogan 	unsigned int mmu_size, guest_mmu_size, ftlb_size;
2875824533adSJames Hogan 	u64 guest_cvmctl, cvmvmconfig;
2876c992a4f6SJames Hogan 
2877824533adSJames Hogan 	switch (current_cpu_type()) {
2878824533adSJames Hogan 	case CPU_CAVIUM_OCTEON3:
2879824533adSJames Hogan 		/* Set up guest timer/perfcount IRQ lines */
2880824533adSJames Hogan 		guest_cvmctl = read_gc0_cvmctl();
2881824533adSJames Hogan 		guest_cvmctl &= ~CVMCTL_IPTI;
2882824533adSJames Hogan 		guest_cvmctl |= 7ull << CVMCTL_IPTI_SHIFT;
2883824533adSJames Hogan 		guest_cvmctl &= ~CVMCTL_IPPCI;
2884824533adSJames Hogan 		guest_cvmctl |= 6ull << CVMCTL_IPPCI_SHIFT;
2885824533adSJames Hogan 		write_gc0_cvmctl(guest_cvmctl);
2886824533adSJames Hogan 
2887824533adSJames Hogan 		cvmvmconfig = read_c0_cvmvmconfig();
2888824533adSJames Hogan 		/* No I/O hole translation. */
2889824533adSJames Hogan 		cvmvmconfig |= CVMVMCONF_DGHT;
2890824533adSJames Hogan 		/* Halve the root MMU size */
2891824533adSJames Hogan 		mmu_size = ((cvmvmconfig & CVMVMCONF_MMUSIZEM1)
2892824533adSJames Hogan 			    >> CVMVMCONF_MMUSIZEM1_S) + 1;
2893824533adSJames Hogan 		guest_mmu_size = mmu_size / 2;
2894824533adSJames Hogan 		mmu_size -= guest_mmu_size;
2895824533adSJames Hogan 		cvmvmconfig &= ~CVMVMCONF_RMMUSIZEM1;
2896824533adSJames Hogan 		cvmvmconfig |= mmu_size - 1;
2897824533adSJames Hogan 		write_c0_cvmvmconfig(cvmvmconfig);
2898824533adSJames Hogan 
2899824533adSJames Hogan 		/* Update our records */
2900824533adSJames Hogan 		current_cpu_data.tlbsize = mmu_size;
2901824533adSJames Hogan 		current_cpu_data.tlbsizevtlb = mmu_size;
2902824533adSJames Hogan 		current_cpu_data.guest.tlbsize = guest_mmu_size;
2903824533adSJames Hogan 
2904824533adSJames Hogan 		/* Flush moved entries in new (guest) context */
2905824533adSJames Hogan 		kvm_vz_local_flush_guesttlb_all();
2906824533adSJames Hogan 		break;
2907824533adSJames Hogan 	default:
2908c992a4f6SJames Hogan 		/*
2909824533adSJames Hogan 		 * ImgTec cores tend to use a shared root/guest TLB. To avoid
2910824533adSJames Hogan 		 * overlap of root wired and guest entries, the guest TLB may
2911824533adSJames Hogan 		 * need resizing.
2912c992a4f6SJames Hogan 		 */
2913c992a4f6SJames Hogan 		mmu_size = current_cpu_data.tlbsizevtlb;
2914c992a4f6SJames Hogan 		ftlb_size = current_cpu_data.tlbsize - mmu_size;
2915c992a4f6SJames Hogan 
2916c992a4f6SJames Hogan 		/* Try switching to maximum guest VTLB size for flush */
2917c992a4f6SJames Hogan 		guest_mmu_size = kvm_vz_resize_guest_vtlb(mmu_size);
2918c992a4f6SJames Hogan 		current_cpu_data.guest.tlbsize = guest_mmu_size + ftlb_size;
2919c992a4f6SJames Hogan 		kvm_vz_local_flush_guesttlb_all();
2920c992a4f6SJames Hogan 
2921c992a4f6SJames Hogan 		/*
2922824533adSJames Hogan 		 * Reduce to make space for root wired entries and at least 2
2923824533adSJames Hogan 		 * root non-wired entries. This does assume that long-term wired
2924824533adSJames Hogan 		 * entries won't be added later.
2925c992a4f6SJames Hogan 		 */
2926c992a4f6SJames Hogan 		guest_mmu_size = mmu_size - num_wired_entries() - 2;
2927c992a4f6SJames Hogan 		guest_mmu_size = kvm_vz_resize_guest_vtlb(guest_mmu_size);
2928c992a4f6SJames Hogan 		current_cpu_data.guest.tlbsize = guest_mmu_size + ftlb_size;
2929c992a4f6SJames Hogan 
2930c992a4f6SJames Hogan 		/*
2931824533adSJames Hogan 		 * Write the VTLB size, but if another CPU has already written,
2932824533adSJames Hogan 		 * check it matches or we won't provide a consistent view to the
2933824533adSJames Hogan 		 * guest. If this ever happens it suggests an asymmetric number
2934824533adSJames Hogan 		 * of wired entries.
2935c992a4f6SJames Hogan 		 */
2936c992a4f6SJames Hogan 		if (cmpxchg(&kvm_vz_guest_vtlb_size, 0, guest_mmu_size) &&
2937c992a4f6SJames Hogan 		    WARN(guest_mmu_size != kvm_vz_guest_vtlb_size,
2938c992a4f6SJames Hogan 			 "Available guest VTLB size mismatch"))
2939c992a4f6SJames Hogan 			return -EINVAL;
2940824533adSJames Hogan 		break;
2941824533adSJames Hogan 	}
2942c992a4f6SJames Hogan 
2943c992a4f6SJames Hogan 	/*
2944c992a4f6SJames Hogan 	 * Enable virtualization features granting guest direct control of
2945c992a4f6SJames Hogan 	 * certain features:
2946c992a4f6SJames Hogan 	 * CP0=1:	Guest coprocessor 0 context.
2947c992a4f6SJames Hogan 	 * AT=Guest:	Guest MMU.
2948c992a4f6SJames Hogan 	 * CG=1:	Hit (virtual address) CACHE operations (optional).
2949c992a4f6SJames Hogan 	 * CF=1:	Guest Config registers.
2950c992a4f6SJames Hogan 	 * CGI=1:	Indexed flush CACHE operations (optional).
2951c992a4f6SJames Hogan 	 */
2952c992a4f6SJames Hogan 	write_c0_guestctl0(MIPS_GCTL0_CP0 |
2953c992a4f6SJames Hogan 			   (MIPS_GCTL0_AT_GUEST << MIPS_GCTL0_AT_SHIFT) |
2954c992a4f6SJames Hogan 			   MIPS_GCTL0_CG | MIPS_GCTL0_CF);
295549bb9600SHuacai Chen 	if (cpu_has_guestctl0ext) {
295649bb9600SHuacai Chen 		if (current_cpu_type() != CPU_LOONGSON64)
2957c992a4f6SJames Hogan 			set_c0_guestctl0ext(MIPS_GCTL0EXT_CGI);
295849bb9600SHuacai Chen 		else
295949bb9600SHuacai Chen 			clear_c0_guestctl0ext(MIPS_GCTL0EXT_CGI);
296049bb9600SHuacai Chen 	}
2961c992a4f6SJames Hogan 
2962c992a4f6SJames Hogan 	if (cpu_has_guestid) {
2963c992a4f6SJames Hogan 		write_c0_guestctl1(0);
2964c992a4f6SJames Hogan 		kvm_vz_local_flush_roottlb_all_guests();
2965c992a4f6SJames Hogan 
2966c992a4f6SJames Hogan 		GUESTID_MASK = current_cpu_data.guestid_mask;
2967c992a4f6SJames Hogan 		GUESTID_FIRST_VERSION = GUESTID_MASK + 1;
2968c992a4f6SJames Hogan 		GUESTID_VERSION_MASK = ~GUESTID_MASK;
2969c992a4f6SJames Hogan 
2970c992a4f6SJames Hogan 		current_cpu_data.guestid_cache = GUESTID_FIRST_VERSION;
2971c992a4f6SJames Hogan 	}
2972c992a4f6SJames Hogan 
2973c992a4f6SJames Hogan 	/* clear any pending injected virtual guest interrupts */
2974c992a4f6SJames Hogan 	if (cpu_has_guestctl2)
2975c992a4f6SJames Hogan 		clear_c0_guestctl2(0x3f << 10);
2976c992a4f6SJames Hogan 
297752c07e1cSHuacai Chen #ifdef CONFIG_CPU_LOONGSON64
297852c07e1cSHuacai Chen 	/* Control guest CCA attribute */
297952c07e1cSHuacai Chen 	if (cpu_has_csr())
298052c07e1cSHuacai Chen 		csr_writel(csr_readl(0xffffffec) | 0x1, 0xffffffec);
298152c07e1cSHuacai Chen #endif
298252c07e1cSHuacai Chen 
2983c992a4f6SJames Hogan 	return 0;
2984c992a4f6SJames Hogan }
2985c992a4f6SJames Hogan 
kvm_vz_hardware_disable(void)2986c992a4f6SJames Hogan static void kvm_vz_hardware_disable(void)
2987c992a4f6SJames Hogan {
2988824533adSJames Hogan 	u64 cvmvmconfig;
2989824533adSJames Hogan 	unsigned int mmu_size;
2990824533adSJames Hogan 
2991824533adSJames Hogan 	/* Flush any remaining guest TLB entries */
2992c992a4f6SJames Hogan 	kvm_vz_local_flush_guesttlb_all();
2993c992a4f6SJames Hogan 
2994824533adSJames Hogan 	switch (current_cpu_type()) {
2995824533adSJames Hogan 	case CPU_CAVIUM_OCTEON3:
2996824533adSJames Hogan 		/*
2997824533adSJames Hogan 		 * Allocate whole TLB for root. Existing guest TLB entries will
2998824533adSJames Hogan 		 * change ownership to the root TLB. We should be safe though as
2999824533adSJames Hogan 		 * they've already been flushed above while in guest TLB.
3000824533adSJames Hogan 		 */
3001824533adSJames Hogan 		cvmvmconfig = read_c0_cvmvmconfig();
3002824533adSJames Hogan 		mmu_size = ((cvmvmconfig & CVMVMCONF_MMUSIZEM1)
3003824533adSJames Hogan 			    >> CVMVMCONF_MMUSIZEM1_S) + 1;
3004824533adSJames Hogan 		cvmvmconfig &= ~CVMVMCONF_RMMUSIZEM1;
3005824533adSJames Hogan 		cvmvmconfig |= mmu_size - 1;
3006824533adSJames Hogan 		write_c0_cvmvmconfig(cvmvmconfig);
3007824533adSJames Hogan 
3008824533adSJames Hogan 		/* Update our records */
3009824533adSJames Hogan 		current_cpu_data.tlbsize = mmu_size;
3010824533adSJames Hogan 		current_cpu_data.tlbsizevtlb = mmu_size;
3011824533adSJames Hogan 		current_cpu_data.guest.tlbsize = 0;
3012824533adSJames Hogan 
3013824533adSJames Hogan 		/* Flush moved entries in new (root) context */
3014824533adSJames Hogan 		local_flush_tlb_all();
3015824533adSJames Hogan 		break;
3016824533adSJames Hogan 	}
3017824533adSJames Hogan 
3018c992a4f6SJames Hogan 	if (cpu_has_guestid) {
3019c992a4f6SJames Hogan 		write_c0_guestctl1(0);
3020c992a4f6SJames Hogan 		kvm_vz_local_flush_roottlb_all_guests();
3021c992a4f6SJames Hogan 	}
3022c992a4f6SJames Hogan }
3023c992a4f6SJames Hogan 
kvm_vz_check_extension(struct kvm * kvm,long ext)3024c992a4f6SJames Hogan static int kvm_vz_check_extension(struct kvm *kvm, long ext)
3025c992a4f6SJames Hogan {
3026c992a4f6SJames Hogan 	int r;
3027c992a4f6SJames Hogan 
3028c992a4f6SJames Hogan 	switch (ext) {
3029c992a4f6SJames Hogan 	case KVM_CAP_MIPS_VZ:
3030c992a4f6SJames Hogan 		/* we wouldn't be here unless cpu_has_vz */
3031c992a4f6SJames Hogan 		r = 1;
3032c992a4f6SJames Hogan 		break;
3033c992a4f6SJames Hogan #ifdef CONFIG_64BIT
3034c992a4f6SJames Hogan 	case KVM_CAP_MIPS_64BIT:
3035c992a4f6SJames Hogan 		/* We support 64-bit registers/operations and addresses */
3036c992a4f6SJames Hogan 		r = 2;
3037c992a4f6SJames Hogan 		break;
3038c992a4f6SJames Hogan #endif
3039bf10efbbSHuacai Chen 	case KVM_CAP_IOEVENTFD:
3040bf10efbbSHuacai Chen 		r = 1;
3041bf10efbbSHuacai Chen 		break;
3042c992a4f6SJames Hogan 	default:
3043c992a4f6SJames Hogan 		r = 0;
3044c992a4f6SJames Hogan 		break;
3045c992a4f6SJames Hogan 	}
3046c992a4f6SJames Hogan 
3047c992a4f6SJames Hogan 	return r;
3048c992a4f6SJames Hogan }
3049c992a4f6SJames Hogan 
kvm_vz_vcpu_init(struct kvm_vcpu * vcpu)3050c992a4f6SJames Hogan static int kvm_vz_vcpu_init(struct kvm_vcpu *vcpu)
3051c992a4f6SJames Hogan {
3052c992a4f6SJames Hogan 	int i;
3053c992a4f6SJames Hogan 
3054c992a4f6SJames Hogan 	for_each_possible_cpu(i)
3055c992a4f6SJames Hogan 		vcpu->arch.vzguestid[i] = 0;
3056c992a4f6SJames Hogan 
3057c992a4f6SJames Hogan 	return 0;
3058c992a4f6SJames Hogan }
3059c992a4f6SJames Hogan 
kvm_vz_vcpu_uninit(struct kvm_vcpu * vcpu)3060c992a4f6SJames Hogan static void kvm_vz_vcpu_uninit(struct kvm_vcpu *vcpu)
3061c992a4f6SJames Hogan {
3062c992a4f6SJames Hogan 	int cpu;
3063c992a4f6SJames Hogan 
3064c992a4f6SJames Hogan 	/*
3065c992a4f6SJames Hogan 	 * If the VCPU is freed and reused as another VCPU, we don't want the
3066c992a4f6SJames Hogan 	 * matching pointer wrongly hanging around in last_vcpu[] or
3067c992a4f6SJames Hogan 	 * last_exec_vcpu[].
3068c992a4f6SJames Hogan 	 */
3069c992a4f6SJames Hogan 	for_each_possible_cpu(cpu) {
3070c992a4f6SJames Hogan 		if (last_vcpu[cpu] == vcpu)
3071c992a4f6SJames Hogan 			last_vcpu[cpu] = NULL;
3072c992a4f6SJames Hogan 		if (last_exec_vcpu[cpu] == vcpu)
3073c992a4f6SJames Hogan 			last_exec_vcpu[cpu] = NULL;
3074c992a4f6SJames Hogan 	}
3075c992a4f6SJames Hogan }
3076c992a4f6SJames Hogan 
kvm_vz_vcpu_setup(struct kvm_vcpu * vcpu)3077c992a4f6SJames Hogan static int kvm_vz_vcpu_setup(struct kvm_vcpu *vcpu)
3078c992a4f6SJames Hogan {
3079c992a4f6SJames Hogan 	struct mips_coproc *cop0 = &vcpu->arch.cop0;
3080c992a4f6SJames Hogan 	unsigned long count_hz = 100*1000*1000; /* default to 100 MHz */
3081c992a4f6SJames Hogan 
3082c992a4f6SJames Hogan 	/*
3083c992a4f6SJames Hogan 	 * Start off the timer at the same frequency as the host timer, but the
3084c992a4f6SJames Hogan 	 * soft timer doesn't handle frequencies greater than 1GHz yet.
3085c992a4f6SJames Hogan 	 */
3086c992a4f6SJames Hogan 	if (mips_hpt_frequency && mips_hpt_frequency <= NSEC_PER_SEC)
3087c992a4f6SJames Hogan 		count_hz = mips_hpt_frequency;
3088c992a4f6SJames Hogan 	kvm_mips_init_count(vcpu, count_hz);
3089c992a4f6SJames Hogan 
3090c992a4f6SJames Hogan 	/*
3091c992a4f6SJames Hogan 	 * Initialize guest register state to valid architectural reset state.
3092c992a4f6SJames Hogan 	 */
3093c992a4f6SJames Hogan 
3094c992a4f6SJames Hogan 	/* PageGrain */
3095ab7c01fdSSerge Semin 	if (cpu_has_mips_r5 || cpu_has_mips_r6)
3096c992a4f6SJames Hogan 		kvm_write_sw_gc0_pagegrain(cop0, PG_RIE | PG_XIE | PG_IEC);
3097c992a4f6SJames Hogan 	/* Wired */
3098c992a4f6SJames Hogan 	if (cpu_has_mips_r6)
3099c992a4f6SJames Hogan 		kvm_write_sw_gc0_wired(cop0,
3100c992a4f6SJames Hogan 				       read_gc0_wired() & MIPSR6_WIRED_LIMIT);
3101c992a4f6SJames Hogan 	/* Status */
3102c992a4f6SJames Hogan 	kvm_write_sw_gc0_status(cop0, ST0_BEV | ST0_ERL);
3103ab7c01fdSSerge Semin 	if (cpu_has_mips_r5 || cpu_has_mips_r6)
3104c992a4f6SJames Hogan 		kvm_change_sw_gc0_status(cop0, ST0_FR, read_gc0_status());
3105c992a4f6SJames Hogan 	/* IntCtl */
3106c992a4f6SJames Hogan 	kvm_write_sw_gc0_intctl(cop0, read_gc0_intctl() &
3107c992a4f6SJames Hogan 				(INTCTLF_IPFDC | INTCTLF_IPPCI | INTCTLF_IPTI));
3108c992a4f6SJames Hogan 	/* PRId */
3109c992a4f6SJames Hogan 	kvm_write_sw_gc0_prid(cop0, boot_cpu_data.processor_id);
3110c992a4f6SJames Hogan 	/* EBase */
3111c992a4f6SJames Hogan 	kvm_write_sw_gc0_ebase(cop0, (s32)0x80000000 | vcpu->vcpu_id);
3112c992a4f6SJames Hogan 	/* Config */
3113c992a4f6SJames Hogan 	kvm_save_gc0_config(cop0);
3114c992a4f6SJames Hogan 	/* architecturally writable (e.g. from guest) */
3115c992a4f6SJames Hogan 	kvm_change_sw_gc0_config(cop0, CONF_CM_CMASK,
3116c992a4f6SJames Hogan 				 _page_cachable_default >> _CACHE_SHIFT);
3117c992a4f6SJames Hogan 	/* architecturally read only, but maybe writable from root */
3118c992a4f6SJames Hogan 	kvm_change_sw_gc0_config(cop0, MIPS_CONF_MT, read_c0_config());
3119c992a4f6SJames Hogan 	if (cpu_guest_has_conf1) {
3120c992a4f6SJames Hogan 		kvm_set_sw_gc0_config(cop0, MIPS_CONF_M);
3121c992a4f6SJames Hogan 		/* Config1 */
3122c992a4f6SJames Hogan 		kvm_save_gc0_config1(cop0);
3123c992a4f6SJames Hogan 		/* architecturally read only, but maybe writable from root */
3124c992a4f6SJames Hogan 		kvm_clear_sw_gc0_config1(cop0, MIPS_CONF1_C2	|
3125c992a4f6SJames Hogan 					       MIPS_CONF1_MD	|
3126c992a4f6SJames Hogan 					       MIPS_CONF1_PC	|
3127c992a4f6SJames Hogan 					       MIPS_CONF1_WR	|
3128c992a4f6SJames Hogan 					       MIPS_CONF1_CA	|
3129c992a4f6SJames Hogan 					       MIPS_CONF1_FP);
3130c992a4f6SJames Hogan 	}
3131c992a4f6SJames Hogan 	if (cpu_guest_has_conf2) {
3132c992a4f6SJames Hogan 		kvm_set_sw_gc0_config1(cop0, MIPS_CONF_M);
3133c992a4f6SJames Hogan 		/* Config2 */
3134c992a4f6SJames Hogan 		kvm_save_gc0_config2(cop0);
3135c992a4f6SJames Hogan 	}
3136c992a4f6SJames Hogan 	if (cpu_guest_has_conf3) {
3137c992a4f6SJames Hogan 		kvm_set_sw_gc0_config2(cop0, MIPS_CONF_M);
3138c992a4f6SJames Hogan 		/* Config3 */
3139c992a4f6SJames Hogan 		kvm_save_gc0_config3(cop0);
3140c992a4f6SJames Hogan 		/* architecturally writable (e.g. from guest) */
3141c992a4f6SJames Hogan 		kvm_clear_sw_gc0_config3(cop0, MIPS_CONF3_ISA_OE);
3142c992a4f6SJames Hogan 		/* architecturally read only, but maybe writable from root */
3143c992a4f6SJames Hogan 		kvm_clear_sw_gc0_config3(cop0, MIPS_CONF3_MSA	|
3144c992a4f6SJames Hogan 					       MIPS_CONF3_BPG	|
3145c992a4f6SJames Hogan 					       MIPS_CONF3_ULRI	|
3146c992a4f6SJames Hogan 					       MIPS_CONF3_DSP	|
3147c992a4f6SJames Hogan 					       MIPS_CONF3_CTXTC	|
3148c992a4f6SJames Hogan 					       MIPS_CONF3_ITL	|
3149c992a4f6SJames Hogan 					       MIPS_CONF3_LPA	|
3150c992a4f6SJames Hogan 					       MIPS_CONF3_VEIC	|
3151c992a4f6SJames Hogan 					       MIPS_CONF3_VINT	|
3152c992a4f6SJames Hogan 					       MIPS_CONF3_SP	|
3153c992a4f6SJames Hogan 					       MIPS_CONF3_CDMM	|
3154c992a4f6SJames Hogan 					       MIPS_CONF3_MT	|
3155c992a4f6SJames Hogan 					       MIPS_CONF3_SM	|
3156c992a4f6SJames Hogan 					       MIPS_CONF3_TL);
3157c992a4f6SJames Hogan 	}
3158c992a4f6SJames Hogan 	if (cpu_guest_has_conf4) {
3159c992a4f6SJames Hogan 		kvm_set_sw_gc0_config3(cop0, MIPS_CONF_M);
3160c992a4f6SJames Hogan 		/* Config4 */
3161c992a4f6SJames Hogan 		kvm_save_gc0_config4(cop0);
3162c992a4f6SJames Hogan 	}
3163c992a4f6SJames Hogan 	if (cpu_guest_has_conf5) {
3164c992a4f6SJames Hogan 		kvm_set_sw_gc0_config4(cop0, MIPS_CONF_M);
3165c992a4f6SJames Hogan 		/* Config5 */
3166c992a4f6SJames Hogan 		kvm_save_gc0_config5(cop0);
3167c992a4f6SJames Hogan 		/* architecturally writable (e.g. from guest) */
3168c992a4f6SJames Hogan 		kvm_clear_sw_gc0_config5(cop0, MIPS_CONF5_K	|
3169c992a4f6SJames Hogan 					       MIPS_CONF5_CV	|
3170c992a4f6SJames Hogan 					       MIPS_CONF5_MSAEN	|
3171c992a4f6SJames Hogan 					       MIPS_CONF5_UFE	|
3172c992a4f6SJames Hogan 					       MIPS_CONF5_FRE	|
3173c992a4f6SJames Hogan 					       MIPS_CONF5_SBRI	|
3174c992a4f6SJames Hogan 					       MIPS_CONF5_UFR);
3175c992a4f6SJames Hogan 		/* architecturally read only, but maybe writable from root */
3176c992a4f6SJames Hogan 		kvm_clear_sw_gc0_config5(cop0, MIPS_CONF5_MRP);
3177c992a4f6SJames Hogan 	}
3178c992a4f6SJames Hogan 
3179dffe042fSJames Hogan 	if (cpu_guest_has_contextconfig) {
3180dffe042fSJames Hogan 		/* ContextConfig */
3181dffe042fSJames Hogan 		kvm_write_sw_gc0_contextconfig(cop0, 0x007ffff0);
3182dffe042fSJames Hogan #ifdef CONFIG_64BIT
3183dffe042fSJames Hogan 		/* XContextConfig */
3184dffe042fSJames Hogan 		/* bits SEGBITS-13+3:4 set */
3185dffe042fSJames Hogan 		kvm_write_sw_gc0_xcontextconfig(cop0,
3186dffe042fSJames Hogan 					((1ull << (cpu_vmbits - 13)) - 1) << 4);
3187dffe042fSJames Hogan #endif
3188dffe042fSJames Hogan 	}
3189dffe042fSJames Hogan 
31904b7de028SJames Hogan 	/* Implementation dependent, use the legacy layout */
31914b7de028SJames Hogan 	if (cpu_guest_has_segments) {
31924b7de028SJames Hogan 		/* SegCtl0, SegCtl1, SegCtl2 */
31934b7de028SJames Hogan 		kvm_write_sw_gc0_segctl0(cop0, 0x00200010);
31944b7de028SJames Hogan 		kvm_write_sw_gc0_segctl1(cop0, 0x00000002 |
31954b7de028SJames Hogan 				(_page_cachable_default >> _CACHE_SHIFT) <<
31964b7de028SJames Hogan 						(16 + MIPS_SEGCFG_C_SHIFT));
31974b7de028SJames Hogan 		kvm_write_sw_gc0_segctl2(cop0, 0x00380438);
31984b7de028SJames Hogan 	}
31994b7de028SJames Hogan 
32005a2f352fSJames Hogan 	/* reset HTW registers */
3201ab7c01fdSSerge Semin 	if (cpu_guest_has_htw && (cpu_has_mips_r5 || cpu_has_mips_r6)) {
32025a2f352fSJames Hogan 		/* PWField */
32035a2f352fSJames Hogan 		kvm_write_sw_gc0_pwfield(cop0, 0x0c30c302);
32045a2f352fSJames Hogan 		/* PWSize */
32055a2f352fSJames Hogan 		kvm_write_sw_gc0_pwsize(cop0, 1 << MIPS_PWSIZE_PTW_SHIFT);
32065a2f352fSJames Hogan 	}
32075a2f352fSJames Hogan 
3208c992a4f6SJames Hogan 	/* start with no pending virtual guest interrupts */
3209c992a4f6SJames Hogan 	if (cpu_has_guestctl2)
3210c992a4f6SJames Hogan 		cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL] = 0;
3211c992a4f6SJames Hogan 
3212c992a4f6SJames Hogan 	/* Put PC at reset vector */
3213c992a4f6SJames Hogan 	vcpu->arch.pc = CKSEG1ADDR(0x1fc00000);
3214c992a4f6SJames Hogan 
3215c992a4f6SJames Hogan 	return 0;
3216c992a4f6SJames Hogan }
3217c992a4f6SJames Hogan 
kvm_vz_prepare_flush_shadow(struct kvm * kvm)32185194552fSPaolo Bonzini static void kvm_vz_prepare_flush_shadow(struct kvm *kvm)
3219c992a4f6SJames Hogan {
32205194552fSPaolo Bonzini 	if (!cpu_has_guestid) {
3221c992a4f6SJames Hogan 		/*
3222c992a4f6SJames Hogan 		 * For each CPU there is a single GPA ASID used by all VCPUs in
3223c992a4f6SJames Hogan 		 * the VM, so it doesn't make sense for the VCPUs to handle
3224c992a4f6SJames Hogan 		 * invalidation of these ASIDs individually.
3225c992a4f6SJames Hogan 		 *
3226c992a4f6SJames Hogan 		 * Instead mark all CPUs as needing ASID invalidation in
32275194552fSPaolo Bonzini 		 * asid_flush_mask, and kvm_flush_remote_tlbs(kvm) will
3228c992a4f6SJames Hogan 		 * kick any running VCPUs so they check asid_flush_mask.
3229c992a4f6SJames Hogan 		 */
3230c992a4f6SJames Hogan 		cpumask_setall(&kvm->arch.asid_flush_mask);
3231c992a4f6SJames Hogan 	}
3232c992a4f6SJames Hogan }
3233c992a4f6SJames Hogan 
kvm_vz_vcpu_reenter(struct kvm_vcpu * vcpu)3234c34b26b9STianjia Zhang static void kvm_vz_vcpu_reenter(struct kvm_vcpu *vcpu)
3235c992a4f6SJames Hogan {
3236c992a4f6SJames Hogan 	int cpu = smp_processor_id();
3237c992a4f6SJames Hogan 	int preserve_guest_tlb;
3238c992a4f6SJames Hogan 
3239c992a4f6SJames Hogan 	preserve_guest_tlb = kvm_vz_check_requests(vcpu, cpu);
3240c992a4f6SJames Hogan 
3241c992a4f6SJames Hogan 	if (preserve_guest_tlb)
3242c992a4f6SJames Hogan 		kvm_vz_vcpu_save_wired(vcpu);
3243c992a4f6SJames Hogan 
3244c992a4f6SJames Hogan 	kvm_vz_vcpu_load_tlb(vcpu, cpu);
3245c992a4f6SJames Hogan 
3246c992a4f6SJames Hogan 	if (preserve_guest_tlb)
3247c992a4f6SJames Hogan 		kvm_vz_vcpu_load_wired(vcpu);
3248c992a4f6SJames Hogan }
3249c992a4f6SJames Hogan 
kvm_vz_vcpu_run(struct kvm_vcpu * vcpu)3250c34b26b9STianjia Zhang static int kvm_vz_vcpu_run(struct kvm_vcpu *vcpu)
3251c992a4f6SJames Hogan {
3252c992a4f6SJames Hogan 	int cpu = smp_processor_id();
3253c992a4f6SJames Hogan 	int r;
3254c992a4f6SJames Hogan 
3255f4474d50SJames Hogan 	kvm_vz_acquire_htimer(vcpu);
3256c992a4f6SJames Hogan 	/* Check if we have any exceptions/interrupts pending */
3257c992a4f6SJames Hogan 	kvm_mips_deliver_interrupts(vcpu, read_gc0_cause());
3258c992a4f6SJames Hogan 
3259c992a4f6SJames Hogan 	kvm_vz_check_requests(vcpu, cpu);
3260c992a4f6SJames Hogan 	kvm_vz_vcpu_load_tlb(vcpu, cpu);
3261c992a4f6SJames Hogan 	kvm_vz_vcpu_load_wired(vcpu);
3262c992a4f6SJames Hogan 
32630b7aa583STianjia Zhang 	r = vcpu->arch.vcpu_run(vcpu);
3264c992a4f6SJames Hogan 
3265c992a4f6SJames Hogan 	kvm_vz_vcpu_save_wired(vcpu);
3266c992a4f6SJames Hogan 
3267c992a4f6SJames Hogan 	return r;
3268c992a4f6SJames Hogan }
3269c992a4f6SJames Hogan 
3270c992a4f6SJames Hogan static struct kvm_mips_callbacks kvm_vz_callbacks = {
3271c992a4f6SJames Hogan 	.handle_cop_unusable = kvm_trap_vz_handle_cop_unusable,
3272c992a4f6SJames Hogan 	.handle_tlb_mod = kvm_trap_vz_handle_tlb_st_miss,
3273c992a4f6SJames Hogan 	.handle_tlb_ld_miss = kvm_trap_vz_handle_tlb_ld_miss,
3274c992a4f6SJames Hogan 	.handle_tlb_st_miss = kvm_trap_vz_handle_tlb_st_miss,
3275c992a4f6SJames Hogan 	.handle_addr_err_st = kvm_trap_vz_no_handler,
3276c992a4f6SJames Hogan 	.handle_addr_err_ld = kvm_trap_vz_no_handler,
3277c992a4f6SJames Hogan 	.handle_syscall = kvm_trap_vz_no_handler,
3278c992a4f6SJames Hogan 	.handle_res_inst = kvm_trap_vz_no_handler,
3279c992a4f6SJames Hogan 	.handle_break = kvm_trap_vz_no_handler,
3280c992a4f6SJames Hogan 	.handle_msa_disabled = kvm_trap_vz_handle_msa_disabled,
3281c992a4f6SJames Hogan 	.handle_guest_exit = kvm_trap_vz_handle_guest_exit,
3282c992a4f6SJames Hogan 
3283c992a4f6SJames Hogan 	.hardware_enable = kvm_vz_hardware_enable,
3284c992a4f6SJames Hogan 	.hardware_disable = kvm_vz_hardware_disable,
3285c992a4f6SJames Hogan 	.check_extension = kvm_vz_check_extension,
3286c992a4f6SJames Hogan 	.vcpu_init = kvm_vz_vcpu_init,
3287c992a4f6SJames Hogan 	.vcpu_uninit = kvm_vz_vcpu_uninit,
3288c992a4f6SJames Hogan 	.vcpu_setup = kvm_vz_vcpu_setup,
32895194552fSPaolo Bonzini 	.prepare_flush_shadow = kvm_vz_prepare_flush_shadow,
3290c992a4f6SJames Hogan 	.gva_to_gpa = kvm_vz_gva_to_gpa_cb,
3291c992a4f6SJames Hogan 	.queue_timer_int = kvm_vz_queue_timer_int_cb,
3292c992a4f6SJames Hogan 	.dequeue_timer_int = kvm_vz_dequeue_timer_int_cb,
3293c992a4f6SJames Hogan 	.queue_io_int = kvm_vz_queue_io_int_cb,
3294c992a4f6SJames Hogan 	.dequeue_io_int = kvm_vz_dequeue_io_int_cb,
3295c992a4f6SJames Hogan 	.irq_deliver = kvm_vz_irq_deliver_cb,
3296c992a4f6SJames Hogan 	.irq_clear = kvm_vz_irq_clear_cb,
3297c992a4f6SJames Hogan 	.num_regs = kvm_vz_num_regs,
3298c992a4f6SJames Hogan 	.copy_reg_indices = kvm_vz_copy_reg_indices,
3299c992a4f6SJames Hogan 	.get_one_reg = kvm_vz_get_one_reg,
3300c992a4f6SJames Hogan 	.set_one_reg = kvm_vz_set_one_reg,
3301c992a4f6SJames Hogan 	.vcpu_load = kvm_vz_vcpu_load,
3302c992a4f6SJames Hogan 	.vcpu_put = kvm_vz_vcpu_put,
3303c992a4f6SJames Hogan 	.vcpu_run = kvm_vz_vcpu_run,
3304c992a4f6SJames Hogan 	.vcpu_reenter = kvm_vz_vcpu_reenter,
3305c992a4f6SJames Hogan };
3306c992a4f6SJames Hogan 
33071cfc1c7bSSean Christopherson /* FIXME: Get rid of the callbacks now that trap-and-emulate is gone. */
3308*7ffc2e89SSean Christopherson const struct kvm_mips_callbacks * const kvm_mips_callbacks = &kvm_vz_callbacks;
33091cfc1c7bSSean Christopherson 
kvm_mips_emulation_init(void)33101cfc1c7bSSean Christopherson int kvm_mips_emulation_init(void)
3311c992a4f6SJames Hogan {
3312c992a4f6SJames Hogan 	if (!cpu_has_vz)
3313c992a4f6SJames Hogan 		return -ENODEV;
3314c992a4f6SJames Hogan 
3315c992a4f6SJames Hogan 	/*
3316c992a4f6SJames Hogan 	 * VZ requires at least 2 KScratch registers, so it should have been
3317c992a4f6SJames Hogan 	 * possible to allocate pgd_reg.
3318c992a4f6SJames Hogan 	 */
3319c992a4f6SJames Hogan 	if (WARN(pgd_reg == -1,
3320c992a4f6SJames Hogan 		 "pgd_reg not allocated even though cpu_has_vz\n"))
3321c992a4f6SJames Hogan 		return -ENODEV;
3322c992a4f6SJames Hogan 
3323c992a4f6SJames Hogan 	pr_info("Starting KVM with MIPS VZ extensions\n");
3324c992a4f6SJames Hogan 	return 0;
3325c992a4f6SJames Hogan }
3326