xref: /linux/arch/mips/kvm/tlb.c (revision e6207bbea16c60942cdc1492af4feed5aed77389)
1d7d5b05fSDeng-Cheng Zhu /*
2d7d5b05fSDeng-Cheng Zhu  * This file is subject to the terms and conditions of the GNU General Public
3d7d5b05fSDeng-Cheng Zhu  * License.  See the file "COPYING" in the main directory of this archive
4d7d5b05fSDeng-Cheng Zhu  * for more details.
5d7d5b05fSDeng-Cheng Zhu  *
6d7d5b05fSDeng-Cheng Zhu  * KVM/MIPS TLB handling, this file is part of the Linux host kernel so that
7d7d5b05fSDeng-Cheng Zhu  * TLB handlers run from KSEG0
8d7d5b05fSDeng-Cheng Zhu  *
9d7d5b05fSDeng-Cheng Zhu  * Copyright (C) 2012  MIPS Technologies, Inc.  All rights reserved.
10d7d5b05fSDeng-Cheng Zhu  * Authors: Sanjay Lal <sanjayl@kymasys.com>
11d7d5b05fSDeng-Cheng Zhu  */
12d7d5b05fSDeng-Cheng Zhu 
13d7d5b05fSDeng-Cheng Zhu #include <linux/sched.h>
14d7d5b05fSDeng-Cheng Zhu #include <linux/smp.h>
15d7d5b05fSDeng-Cheng Zhu #include <linux/mm.h>
16d7d5b05fSDeng-Cheng Zhu #include <linux/delay.h>
17403015b3SJames Hogan #include <linux/export.h>
18d7d5b05fSDeng-Cheng Zhu #include <linux/kvm_host.h>
19d7d5b05fSDeng-Cheng Zhu #include <linux/srcu.h>
20d7d5b05fSDeng-Cheng Zhu 
21d7d5b05fSDeng-Cheng Zhu #include <asm/cpu.h>
22d7d5b05fSDeng-Cheng Zhu #include <asm/bootinfo.h>
23d7d5b05fSDeng-Cheng Zhu #include <asm/mmu_context.h>
24d7d5b05fSDeng-Cheng Zhu #include <asm/pgtable.h>
25d7d5b05fSDeng-Cheng Zhu #include <asm/cacheflush.h>
26d7d5b05fSDeng-Cheng Zhu #include <asm/tlb.h>
27e922a4cbSJames Hogan #include <asm/tlbdebug.h>
28d7d5b05fSDeng-Cheng Zhu 
29d7d5b05fSDeng-Cheng Zhu #undef CONFIG_MIPS_MT
30d7d5b05fSDeng-Cheng Zhu #include <asm/r4kcache.h>
31d7d5b05fSDeng-Cheng Zhu #define CONFIG_MIPS_MT
32d7d5b05fSDeng-Cheng Zhu 
33d7d5b05fSDeng-Cheng Zhu #define KVM_GUEST_PC_TLB    0
34d7d5b05fSDeng-Cheng Zhu #define KVM_GUEST_SP_TLB    1
35d7d5b05fSDeng-Cheng Zhu 
36d7d5b05fSDeng-Cheng Zhu atomic_t kvm_mips_instance;
37cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_instance);
38d7d5b05fSDeng-Cheng Zhu 
39403015b3SJames Hogan static u32 kvm_mips_get_kernel_asid(struct kvm_vcpu *vcpu)
40d7d5b05fSDeng-Cheng Zhu {
414edf00a4SPaul Burton 	int cpu = smp_processor_id();
424edf00a4SPaul Burton 
434edf00a4SPaul Burton 	return vcpu->arch.guest_kernel_asid[cpu] &
444edf00a4SPaul Burton 			cpu_asid_mask(&cpu_data[cpu]);
45d7d5b05fSDeng-Cheng Zhu }
46d7d5b05fSDeng-Cheng Zhu 
47403015b3SJames Hogan static u32 kvm_mips_get_user_asid(struct kvm_vcpu *vcpu)
48d7d5b05fSDeng-Cheng Zhu {
494edf00a4SPaul Burton 	int cpu = smp_processor_id();
504edf00a4SPaul Burton 
514edf00a4SPaul Burton 	return vcpu->arch.guest_user_asid[cpu] &
524edf00a4SPaul Burton 			cpu_asid_mask(&cpu_data[cpu]);
53d7d5b05fSDeng-Cheng Zhu }
54d7d5b05fSDeng-Cheng Zhu 
55bdb7ed86SJames Hogan inline u32 kvm_mips_get_commpage_asid(struct kvm_vcpu *vcpu)
56d7d5b05fSDeng-Cheng Zhu {
57d7d5b05fSDeng-Cheng Zhu 	return vcpu->kvm->arch.commpage_tlb;
58d7d5b05fSDeng-Cheng Zhu }
59d7d5b05fSDeng-Cheng Zhu 
60d7d5b05fSDeng-Cheng Zhu /* Structure defining an tlb entry data set. */
61d7d5b05fSDeng-Cheng Zhu 
62d7d5b05fSDeng-Cheng Zhu void kvm_mips_dump_host_tlbs(void)
63d7d5b05fSDeng-Cheng Zhu {
64d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
65d7d5b05fSDeng-Cheng Zhu 
66d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
67d7d5b05fSDeng-Cheng Zhu 
68d7d5b05fSDeng-Cheng Zhu 	kvm_info("HOST TLBs:\n");
69e922a4cbSJames Hogan 	dump_tlb_regs();
70e922a4cbSJames Hogan 	pr_info("\n");
71e922a4cbSJames Hogan 	dump_tlb_all();
72d7d5b05fSDeng-Cheng Zhu 
73d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
74d7d5b05fSDeng-Cheng Zhu }
75cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_dump_host_tlbs);
76d7d5b05fSDeng-Cheng Zhu 
77d7d5b05fSDeng-Cheng Zhu void kvm_mips_dump_guest_tlbs(struct kvm_vcpu *vcpu)
78d7d5b05fSDeng-Cheng Zhu {
79d7d5b05fSDeng-Cheng Zhu 	struct mips_coproc *cop0 = vcpu->arch.cop0;
80d7d5b05fSDeng-Cheng Zhu 	struct kvm_mips_tlb tlb;
81d7d5b05fSDeng-Cheng Zhu 	int i;
82d7d5b05fSDeng-Cheng Zhu 
83d7d5b05fSDeng-Cheng Zhu 	kvm_info("Guest TLBs:\n");
84d7d5b05fSDeng-Cheng Zhu 	kvm_info("Guest EntryHi: %#lx\n", kvm_read_c0_guest_entryhi(cop0));
85d7d5b05fSDeng-Cheng Zhu 
86d7d5b05fSDeng-Cheng Zhu 	for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
87d7d5b05fSDeng-Cheng Zhu 		tlb = vcpu->arch.guest_tlb[i];
88d7d5b05fSDeng-Cheng Zhu 		kvm_info("TLB%c%3d Hi 0x%08lx ",
89*e6207bbeSJames Hogan 			 (tlb.tlb_lo[0] | tlb.tlb_lo[1]) & ENTRYLO_V
909fbfb06aSJames Hogan 							? ' ' : '*',
91d7d5b05fSDeng-Cheng Zhu 			 i, tlb.tlb_hi);
928cffd197SJames Hogan 		kvm_info("Lo0=0x%09llx %c%c attr %lx ",
939fbfb06aSJames Hogan 			 (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[0]),
94*e6207bbeSJames Hogan 			 (tlb.tlb_lo[0] & ENTRYLO_D) ? 'D' : ' ',
95*e6207bbeSJames Hogan 			 (tlb.tlb_lo[0] & ENTRYLO_G) ? 'G' : ' ',
96*e6207bbeSJames Hogan 			 (tlb.tlb_lo[0] & ENTRYLO_C) >> ENTRYLO_C_SHIFT);
978cffd197SJames Hogan 		kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n",
989fbfb06aSJames Hogan 			 (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo[1]),
99*e6207bbeSJames Hogan 			 (tlb.tlb_lo[1] & ENTRYLO_D) ? 'D' : ' ',
100*e6207bbeSJames Hogan 			 (tlb.tlb_lo[1] & ENTRYLO_G) ? 'G' : ' ',
101*e6207bbeSJames Hogan 			 (tlb.tlb_lo[1] & ENTRYLO_C) >> ENTRYLO_C_SHIFT,
102*e6207bbeSJames Hogan 			 tlb.tlb_mask);
103d7d5b05fSDeng-Cheng Zhu 	}
104d7d5b05fSDeng-Cheng Zhu }
105cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs);
106d7d5b05fSDeng-Cheng Zhu 
107d7d5b05fSDeng-Cheng Zhu /* XXXKYMA: Must be called with interrupts disabled */
108d7d5b05fSDeng-Cheng Zhu /* set flush_dcache_mask == 0 if no dcache flush required */
109d7d5b05fSDeng-Cheng Zhu int kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
110d7d5b05fSDeng-Cheng Zhu 			    unsigned long entrylo0, unsigned long entrylo1,
111d7d5b05fSDeng-Cheng Zhu 			    int flush_dcache_mask)
112d7d5b05fSDeng-Cheng Zhu {
113d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
114d7d5b05fSDeng-Cheng Zhu 	unsigned long old_entryhi;
115d7d5b05fSDeng-Cheng Zhu 	int idx;
116d7d5b05fSDeng-Cheng Zhu 
117d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
118d7d5b05fSDeng-Cheng Zhu 
119d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
120d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(entryhi);
121d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
122d7d5b05fSDeng-Cheng Zhu 
123d7d5b05fSDeng-Cheng Zhu 	tlb_probe();
124d7d5b05fSDeng-Cheng Zhu 	tlb_probe_hazard();
125d7d5b05fSDeng-Cheng Zhu 	idx = read_c0_index();
126d7d5b05fSDeng-Cheng Zhu 
127d7d5b05fSDeng-Cheng Zhu 	if (idx > current_cpu_data.tlbsize) {
128d7d5b05fSDeng-Cheng Zhu 		kvm_err("%s: Invalid Index: %d\n", __func__, idx);
129d7d5b05fSDeng-Cheng Zhu 		kvm_mips_dump_host_tlbs();
130cfec0e75STapasweni Pathak 		local_irq_restore(flags);
131d7d5b05fSDeng-Cheng Zhu 		return -1;
132d7d5b05fSDeng-Cheng Zhu 	}
133d7d5b05fSDeng-Cheng Zhu 
134d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo0(entrylo0);
135d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo1(entrylo1);
136d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
137d7d5b05fSDeng-Cheng Zhu 
138d7d5b05fSDeng-Cheng Zhu 	if (idx < 0)
139d7d5b05fSDeng-Cheng Zhu 		tlb_write_random();
140d7d5b05fSDeng-Cheng Zhu 	else
141d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
142d7d5b05fSDeng-Cheng Zhu 	tlbw_use_hazard();
143d7d5b05fSDeng-Cheng Zhu 
144d7d5b05fSDeng-Cheng Zhu 	kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
145d7d5b05fSDeng-Cheng Zhu 		  vcpu->arch.pc, idx, read_c0_entryhi(),
146d7d5b05fSDeng-Cheng Zhu 		  read_c0_entrylo0(), read_c0_entrylo1());
147d7d5b05fSDeng-Cheng Zhu 
148d7d5b05fSDeng-Cheng Zhu 	/* Flush D-cache */
149d7d5b05fSDeng-Cheng Zhu 	if (flush_dcache_mask) {
150*e6207bbeSJames Hogan 		if (entrylo0 & ENTRYLO_V) {
151d7d5b05fSDeng-Cheng Zhu 			++vcpu->stat.flush_dcache_exits;
152d7d5b05fSDeng-Cheng Zhu 			flush_data_cache_page((entryhi & VPN2_MASK) &
153d7d5b05fSDeng-Cheng Zhu 					      ~flush_dcache_mask);
154d7d5b05fSDeng-Cheng Zhu 		}
155*e6207bbeSJames Hogan 		if (entrylo1 & ENTRYLO_V) {
156d7d5b05fSDeng-Cheng Zhu 			++vcpu->stat.flush_dcache_exits;
157d7d5b05fSDeng-Cheng Zhu 			flush_data_cache_page(((entryhi & VPN2_MASK) &
158d7d5b05fSDeng-Cheng Zhu 					       ~flush_dcache_mask) |
159d7d5b05fSDeng-Cheng Zhu 					      (0x1 << PAGE_SHIFT));
160d7d5b05fSDeng-Cheng Zhu 		}
161d7d5b05fSDeng-Cheng Zhu 	}
162d7d5b05fSDeng-Cheng Zhu 
163d7d5b05fSDeng-Cheng Zhu 	/* Restore old ASID */
164d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
165d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
166d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
167d7d5b05fSDeng-Cheng Zhu 	return 0;
168d7d5b05fSDeng-Cheng Zhu }
169403015b3SJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_write);
170d7d5b05fSDeng-Cheng Zhu 
171d7d5b05fSDeng-Cheng Zhu int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
172d7d5b05fSDeng-Cheng Zhu 	struct kvm_vcpu *vcpu)
173d7d5b05fSDeng-Cheng Zhu {
174ba049e93SDan Williams 	kvm_pfn_t pfn0, pfn1;
175d7d5b05fSDeng-Cheng Zhu 	unsigned long flags, old_entryhi = 0, vaddr = 0;
176d7d5b05fSDeng-Cheng Zhu 	unsigned long entrylo0 = 0, entrylo1 = 0;
177d7d5b05fSDeng-Cheng Zhu 
178d7d5b05fSDeng-Cheng Zhu 	pfn0 = CPHYSADDR(vcpu->arch.kseg0_commpage) >> PAGE_SHIFT;
179d7d5b05fSDeng-Cheng Zhu 	pfn1 = 0;
180*e6207bbeSJames Hogan 	entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) |
181*e6207bbeSJames Hogan 		   (0x3 << ENTRYLO_C_SHIFT) | ENTRYLO_D | ENTRYLO_V;
182d7d5b05fSDeng-Cheng Zhu 	entrylo1 = 0;
183d7d5b05fSDeng-Cheng Zhu 
184d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
185d7d5b05fSDeng-Cheng Zhu 
186d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
187d7d5b05fSDeng-Cheng Zhu 	vaddr = badvaddr & (PAGE_MASK << 1);
188d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(vaddr | kvm_mips_get_kernel_asid(vcpu));
189d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo0(entrylo0);
190d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo1(entrylo1);
191d7d5b05fSDeng-Cheng Zhu 	write_c0_index(kvm_mips_get_commpage_asid(vcpu));
192d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
193d7d5b05fSDeng-Cheng Zhu 	tlb_write_indexed();
194d7d5b05fSDeng-Cheng Zhu 	tlbw_use_hazard();
195d7d5b05fSDeng-Cheng Zhu 
196d7d5b05fSDeng-Cheng Zhu 	kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
197d7d5b05fSDeng-Cheng Zhu 		  vcpu->arch.pc, read_c0_index(), read_c0_entryhi(),
198d7d5b05fSDeng-Cheng Zhu 		  read_c0_entrylo0(), read_c0_entrylo1());
199d7d5b05fSDeng-Cheng Zhu 
200d7d5b05fSDeng-Cheng Zhu 	/* Restore old ASID */
201d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
202d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
203d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
204d7d5b05fSDeng-Cheng Zhu 
205d7d5b05fSDeng-Cheng Zhu 	return 0;
206d7d5b05fSDeng-Cheng Zhu }
207cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_handle_commpage_tlb_fault);
208d7d5b05fSDeng-Cheng Zhu 
209d7d5b05fSDeng-Cheng Zhu int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
210d7d5b05fSDeng-Cheng Zhu {
211d7d5b05fSDeng-Cheng Zhu 	int i;
212d7d5b05fSDeng-Cheng Zhu 	int index = -1;
213d7d5b05fSDeng-Cheng Zhu 	struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
214d7d5b05fSDeng-Cheng Zhu 
215d7d5b05fSDeng-Cheng Zhu 	for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
216d7d5b05fSDeng-Cheng Zhu 		if (TLB_HI_VPN2_HIT(tlb[i], entryhi) &&
217d7d5b05fSDeng-Cheng Zhu 		    TLB_HI_ASID_HIT(tlb[i], entryhi)) {
218d7d5b05fSDeng-Cheng Zhu 			index = i;
219d7d5b05fSDeng-Cheng Zhu 			break;
220d7d5b05fSDeng-Cheng Zhu 		}
221d7d5b05fSDeng-Cheng Zhu 	}
222d7d5b05fSDeng-Cheng Zhu 
223d7d5b05fSDeng-Cheng Zhu 	kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
2249fbfb06aSJames Hogan 		  __func__, entryhi, index, tlb[i].tlb_lo[0], tlb[i].tlb_lo[1]);
225d7d5b05fSDeng-Cheng Zhu 
226d7d5b05fSDeng-Cheng Zhu 	return index;
227d7d5b05fSDeng-Cheng Zhu }
228cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup);
229d7d5b05fSDeng-Cheng Zhu 
230d7d5b05fSDeng-Cheng Zhu int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr)
231d7d5b05fSDeng-Cheng Zhu {
232d7d5b05fSDeng-Cheng Zhu 	unsigned long old_entryhi, flags;
233d7d5b05fSDeng-Cheng Zhu 	int idx;
234d7d5b05fSDeng-Cheng Zhu 
235d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
236d7d5b05fSDeng-Cheng Zhu 
237d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
238d7d5b05fSDeng-Cheng Zhu 
239d7d5b05fSDeng-Cheng Zhu 	if (KVM_GUEST_KERNEL_MODE(vcpu))
240d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi((vaddr & VPN2_MASK) |
241d7d5b05fSDeng-Cheng Zhu 				 kvm_mips_get_kernel_asid(vcpu));
242d7d5b05fSDeng-Cheng Zhu 	else {
243d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi((vaddr & VPN2_MASK) |
244d7d5b05fSDeng-Cheng Zhu 				 kvm_mips_get_user_asid(vcpu));
245d7d5b05fSDeng-Cheng Zhu 	}
246d7d5b05fSDeng-Cheng Zhu 
247d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
248d7d5b05fSDeng-Cheng Zhu 
249d7d5b05fSDeng-Cheng Zhu 	tlb_probe();
250d7d5b05fSDeng-Cheng Zhu 	tlb_probe_hazard();
251d7d5b05fSDeng-Cheng Zhu 	idx = read_c0_index();
252d7d5b05fSDeng-Cheng Zhu 
253d7d5b05fSDeng-Cheng Zhu 	/* Restore old ASID */
254d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
255d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
256d7d5b05fSDeng-Cheng Zhu 
257d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
258d7d5b05fSDeng-Cheng Zhu 
259d7d5b05fSDeng-Cheng Zhu 	kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr, idx);
260d7d5b05fSDeng-Cheng Zhu 
261d7d5b05fSDeng-Cheng Zhu 	return idx;
262d7d5b05fSDeng-Cheng Zhu }
263cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_lookup);
264d7d5b05fSDeng-Cheng Zhu 
265d7d5b05fSDeng-Cheng Zhu int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
266d7d5b05fSDeng-Cheng Zhu {
267d7d5b05fSDeng-Cheng Zhu 	int idx;
268d7d5b05fSDeng-Cheng Zhu 	unsigned long flags, old_entryhi;
269d7d5b05fSDeng-Cheng Zhu 
270d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
271d7d5b05fSDeng-Cheng Zhu 
272d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
273d7d5b05fSDeng-Cheng Zhu 
274d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi((va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu));
275d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
276d7d5b05fSDeng-Cheng Zhu 
277d7d5b05fSDeng-Cheng Zhu 	tlb_probe();
278d7d5b05fSDeng-Cheng Zhu 	tlb_probe_hazard();
279d7d5b05fSDeng-Cheng Zhu 	idx = read_c0_index();
280d7d5b05fSDeng-Cheng Zhu 
281d7d5b05fSDeng-Cheng Zhu 	if (idx >= current_cpu_data.tlbsize)
282d7d5b05fSDeng-Cheng Zhu 		BUG();
283d7d5b05fSDeng-Cheng Zhu 
284d7d5b05fSDeng-Cheng Zhu 	if (idx > 0) {
285d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi(UNIQUE_ENTRYHI(idx));
286d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo0(0);
287d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo1(0);
288d7d5b05fSDeng-Cheng Zhu 		mtc0_tlbw_hazard();
289d7d5b05fSDeng-Cheng Zhu 
290d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
291138f7ad9SJames Hogan 		tlbw_use_hazard();
292d7d5b05fSDeng-Cheng Zhu 	}
293d7d5b05fSDeng-Cheng Zhu 
294d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
295d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
296d7d5b05fSDeng-Cheng Zhu 
297d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
298d7d5b05fSDeng-Cheng Zhu 
299d7d5b05fSDeng-Cheng Zhu 	if (idx > 0)
300d7d5b05fSDeng-Cheng Zhu 		kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__,
301d7d5b05fSDeng-Cheng Zhu 			  (va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu), idx);
302d7d5b05fSDeng-Cheng Zhu 
303d7d5b05fSDeng-Cheng Zhu 	return 0;
304d7d5b05fSDeng-Cheng Zhu }
305cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv);
306d7d5b05fSDeng-Cheng Zhu 
307d7d5b05fSDeng-Cheng Zhu void kvm_mips_flush_host_tlb(int skip_kseg0)
308d7d5b05fSDeng-Cheng Zhu {
309d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
310d7d5b05fSDeng-Cheng Zhu 	unsigned long old_entryhi, entryhi;
311d7d5b05fSDeng-Cheng Zhu 	unsigned long old_pagemask;
312d7d5b05fSDeng-Cheng Zhu 	int entry = 0;
313d7d5b05fSDeng-Cheng Zhu 	int maxentry = current_cpu_data.tlbsize;
314d7d5b05fSDeng-Cheng Zhu 
315d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
316d7d5b05fSDeng-Cheng Zhu 
317d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
318d7d5b05fSDeng-Cheng Zhu 	old_pagemask = read_c0_pagemask();
319d7d5b05fSDeng-Cheng Zhu 
320d7d5b05fSDeng-Cheng Zhu 	/* Blast 'em all away. */
321d7d5b05fSDeng-Cheng Zhu 	for (entry = 0; entry < maxentry; entry++) {
322d7d5b05fSDeng-Cheng Zhu 		write_c0_index(entry);
323d7d5b05fSDeng-Cheng Zhu 
324d7d5b05fSDeng-Cheng Zhu 		if (skip_kseg0) {
325138f7ad9SJames Hogan 			mtc0_tlbr_hazard();
326d7d5b05fSDeng-Cheng Zhu 			tlb_read();
327138f7ad9SJames Hogan 			tlb_read_hazard();
328d7d5b05fSDeng-Cheng Zhu 
329d7d5b05fSDeng-Cheng Zhu 			entryhi = read_c0_entryhi();
330d7d5b05fSDeng-Cheng Zhu 
331d7d5b05fSDeng-Cheng Zhu 			/* Don't blow away guest kernel entries */
332d7d5b05fSDeng-Cheng Zhu 			if (KVM_GUEST_KSEGX(entryhi) == KVM_GUEST_KSEG0)
333d7d5b05fSDeng-Cheng Zhu 				continue;
334d7d5b05fSDeng-Cheng Zhu 		}
335d7d5b05fSDeng-Cheng Zhu 
336d7d5b05fSDeng-Cheng Zhu 		/* Make sure all entries differ. */
337d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi(UNIQUE_ENTRYHI(entry));
338d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo0(0);
339d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo1(0);
340d7d5b05fSDeng-Cheng Zhu 		mtc0_tlbw_hazard();
341d7d5b05fSDeng-Cheng Zhu 
342d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
343d7d5b05fSDeng-Cheng Zhu 		tlbw_use_hazard();
344138f7ad9SJames Hogan 	}
345d7d5b05fSDeng-Cheng Zhu 
346d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
347d7d5b05fSDeng-Cheng Zhu 	write_c0_pagemask(old_pagemask);
348d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
349d7d5b05fSDeng-Cheng Zhu 
350d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
351d7d5b05fSDeng-Cheng Zhu }
352cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_flush_host_tlb);
353d7d5b05fSDeng-Cheng Zhu 
354d7d5b05fSDeng-Cheng Zhu void kvm_local_flush_tlb_all(void)
355d7d5b05fSDeng-Cheng Zhu {
356d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
357d7d5b05fSDeng-Cheng Zhu 	unsigned long old_ctx;
358d7d5b05fSDeng-Cheng Zhu 	int entry = 0;
359d7d5b05fSDeng-Cheng Zhu 
360d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
361d7d5b05fSDeng-Cheng Zhu 	/* Save old context and create impossible VPN2 value */
362d7d5b05fSDeng-Cheng Zhu 	old_ctx = read_c0_entryhi();
363d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo0(0);
364d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo1(0);
365d7d5b05fSDeng-Cheng Zhu 
366d7d5b05fSDeng-Cheng Zhu 	/* Blast 'em all away. */
367d7d5b05fSDeng-Cheng Zhu 	while (entry < current_cpu_data.tlbsize) {
368d7d5b05fSDeng-Cheng Zhu 		/* Make sure all entries differ. */
369d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi(UNIQUE_ENTRYHI(entry));
370d7d5b05fSDeng-Cheng Zhu 		write_c0_index(entry);
371d7d5b05fSDeng-Cheng Zhu 		mtc0_tlbw_hazard();
372d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
373138f7ad9SJames Hogan 		tlbw_use_hazard();
374d7d5b05fSDeng-Cheng Zhu 		entry++;
375d7d5b05fSDeng-Cheng Zhu 	}
376d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_ctx);
377d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
378d7d5b05fSDeng-Cheng Zhu 
379d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
380d7d5b05fSDeng-Cheng Zhu }
381cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_local_flush_tlb_all);
382