xref: /linux/arch/mips/kvm/tlb.c (revision e922a4cb71e745e53e64446d792c4603df43643a)
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>
27*e922a4cbSJames 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");
69*e922a4cbSJames Hogan 	dump_tlb_regs();
70*e922a4cbSJames Hogan 	pr_info("\n");
71*e922a4cbSJames 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 ",
89d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo0 | tlb.tlb_lo1) & MIPS3_PG_V ? ' ' : '*',
90d7d5b05fSDeng-Cheng Zhu 			 i, tlb.tlb_hi);
918cffd197SJames Hogan 		kvm_info("Lo0=0x%09llx %c%c attr %lx ",
928cffd197SJames Hogan 			 (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo0),
93d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo0 & MIPS3_PG_D) ? 'D' : ' ',
94d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo0 & MIPS3_PG_G) ? 'G' : ' ',
95d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo0 >> 3) & 7);
968cffd197SJames Hogan 		kvm_info("Lo1=0x%09llx %c%c attr %lx sz=%lx\n",
978cffd197SJames Hogan 			 (u64) mips3_tlbpfn_to_paddr(tlb.tlb_lo1),
98d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo1 & MIPS3_PG_D) ? 'D' : ' ',
99d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo1 & MIPS3_PG_G) ? 'G' : ' ',
100d7d5b05fSDeng-Cheng Zhu 			 (tlb.tlb_lo1 >> 3) & 7, tlb.tlb_mask);
101d7d5b05fSDeng-Cheng Zhu 	}
102d7d5b05fSDeng-Cheng Zhu }
103cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_dump_guest_tlbs);
104d7d5b05fSDeng-Cheng Zhu 
105d7d5b05fSDeng-Cheng Zhu /* XXXKYMA: Must be called with interrupts disabled */
106d7d5b05fSDeng-Cheng Zhu /* set flush_dcache_mask == 0 if no dcache flush required */
107d7d5b05fSDeng-Cheng Zhu int kvm_mips_host_tlb_write(struct kvm_vcpu *vcpu, unsigned long entryhi,
108d7d5b05fSDeng-Cheng Zhu 			    unsigned long entrylo0, unsigned long entrylo1,
109d7d5b05fSDeng-Cheng Zhu 			    int flush_dcache_mask)
110d7d5b05fSDeng-Cheng Zhu {
111d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
112d7d5b05fSDeng-Cheng Zhu 	unsigned long old_entryhi;
113d7d5b05fSDeng-Cheng Zhu 	int idx;
114d7d5b05fSDeng-Cheng Zhu 
115d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
116d7d5b05fSDeng-Cheng Zhu 
117d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
118d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(entryhi);
119d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
120d7d5b05fSDeng-Cheng Zhu 
121d7d5b05fSDeng-Cheng Zhu 	tlb_probe();
122d7d5b05fSDeng-Cheng Zhu 	tlb_probe_hazard();
123d7d5b05fSDeng-Cheng Zhu 	idx = read_c0_index();
124d7d5b05fSDeng-Cheng Zhu 
125d7d5b05fSDeng-Cheng Zhu 	if (idx > current_cpu_data.tlbsize) {
126d7d5b05fSDeng-Cheng Zhu 		kvm_err("%s: Invalid Index: %d\n", __func__, idx);
127d7d5b05fSDeng-Cheng Zhu 		kvm_mips_dump_host_tlbs();
128cfec0e75STapasweni Pathak 		local_irq_restore(flags);
129d7d5b05fSDeng-Cheng Zhu 		return -1;
130d7d5b05fSDeng-Cheng Zhu 	}
131d7d5b05fSDeng-Cheng Zhu 
132d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo0(entrylo0);
133d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo1(entrylo1);
134d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
135d7d5b05fSDeng-Cheng Zhu 
136d7d5b05fSDeng-Cheng Zhu 	if (idx < 0)
137d7d5b05fSDeng-Cheng Zhu 		tlb_write_random();
138d7d5b05fSDeng-Cheng Zhu 	else
139d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
140d7d5b05fSDeng-Cheng Zhu 	tlbw_use_hazard();
141d7d5b05fSDeng-Cheng Zhu 
142d7d5b05fSDeng-Cheng Zhu 	kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0(R): 0x%08lx, entrylo1(R): 0x%08lx\n",
143d7d5b05fSDeng-Cheng Zhu 		  vcpu->arch.pc, idx, read_c0_entryhi(),
144d7d5b05fSDeng-Cheng Zhu 		  read_c0_entrylo0(), read_c0_entrylo1());
145d7d5b05fSDeng-Cheng Zhu 
146d7d5b05fSDeng-Cheng Zhu 	/* Flush D-cache */
147d7d5b05fSDeng-Cheng Zhu 	if (flush_dcache_mask) {
148d7d5b05fSDeng-Cheng Zhu 		if (entrylo0 & MIPS3_PG_V) {
149d7d5b05fSDeng-Cheng Zhu 			++vcpu->stat.flush_dcache_exits;
150d7d5b05fSDeng-Cheng Zhu 			flush_data_cache_page((entryhi & VPN2_MASK) &
151d7d5b05fSDeng-Cheng Zhu 					      ~flush_dcache_mask);
152d7d5b05fSDeng-Cheng Zhu 		}
153d7d5b05fSDeng-Cheng Zhu 		if (entrylo1 & MIPS3_PG_V) {
154d7d5b05fSDeng-Cheng Zhu 			++vcpu->stat.flush_dcache_exits;
155d7d5b05fSDeng-Cheng Zhu 			flush_data_cache_page(((entryhi & VPN2_MASK) &
156d7d5b05fSDeng-Cheng Zhu 					       ~flush_dcache_mask) |
157d7d5b05fSDeng-Cheng Zhu 					      (0x1 << PAGE_SHIFT));
158d7d5b05fSDeng-Cheng Zhu 		}
159d7d5b05fSDeng-Cheng Zhu 	}
160d7d5b05fSDeng-Cheng Zhu 
161d7d5b05fSDeng-Cheng Zhu 	/* Restore old ASID */
162d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
163d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
164d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
165d7d5b05fSDeng-Cheng Zhu 	return 0;
166d7d5b05fSDeng-Cheng Zhu }
167403015b3SJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_write);
168d7d5b05fSDeng-Cheng Zhu 
169d7d5b05fSDeng-Cheng Zhu int kvm_mips_handle_commpage_tlb_fault(unsigned long badvaddr,
170d7d5b05fSDeng-Cheng Zhu 	struct kvm_vcpu *vcpu)
171d7d5b05fSDeng-Cheng Zhu {
172ba049e93SDan Williams 	kvm_pfn_t pfn0, pfn1;
173d7d5b05fSDeng-Cheng Zhu 	unsigned long flags, old_entryhi = 0, vaddr = 0;
174d7d5b05fSDeng-Cheng Zhu 	unsigned long entrylo0 = 0, entrylo1 = 0;
175d7d5b05fSDeng-Cheng Zhu 
176d7d5b05fSDeng-Cheng Zhu 	pfn0 = CPHYSADDR(vcpu->arch.kseg0_commpage) >> PAGE_SHIFT;
177d7d5b05fSDeng-Cheng Zhu 	pfn1 = 0;
178d7d5b05fSDeng-Cheng Zhu 	entrylo0 = mips3_paddr_to_tlbpfn(pfn0 << PAGE_SHIFT) | (0x3 << 3) |
179d7d5b05fSDeng-Cheng Zhu 		   (1 << 2) | (0x1 << 1);
180d7d5b05fSDeng-Cheng Zhu 	entrylo1 = 0;
181d7d5b05fSDeng-Cheng Zhu 
182d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
183d7d5b05fSDeng-Cheng Zhu 
184d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
185d7d5b05fSDeng-Cheng Zhu 	vaddr = badvaddr & (PAGE_MASK << 1);
186d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(vaddr | kvm_mips_get_kernel_asid(vcpu));
187d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo0(entrylo0);
188d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo1(entrylo1);
189d7d5b05fSDeng-Cheng Zhu 	write_c0_index(kvm_mips_get_commpage_asid(vcpu));
190d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
191d7d5b05fSDeng-Cheng Zhu 	tlb_write_indexed();
192d7d5b05fSDeng-Cheng Zhu 	tlbw_use_hazard();
193d7d5b05fSDeng-Cheng Zhu 
194d7d5b05fSDeng-Cheng Zhu 	kvm_debug("@ %#lx idx: %2d [entryhi(R): %#lx] entrylo0 (R): 0x%08lx, entrylo1(R): 0x%08lx\n",
195d7d5b05fSDeng-Cheng Zhu 		  vcpu->arch.pc, read_c0_index(), read_c0_entryhi(),
196d7d5b05fSDeng-Cheng Zhu 		  read_c0_entrylo0(), read_c0_entrylo1());
197d7d5b05fSDeng-Cheng Zhu 
198d7d5b05fSDeng-Cheng Zhu 	/* Restore old ASID */
199d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
200d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
201d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
202d7d5b05fSDeng-Cheng Zhu 
203d7d5b05fSDeng-Cheng Zhu 	return 0;
204d7d5b05fSDeng-Cheng Zhu }
205cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_handle_commpage_tlb_fault);
206d7d5b05fSDeng-Cheng Zhu 
207d7d5b05fSDeng-Cheng Zhu int kvm_mips_guest_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long entryhi)
208d7d5b05fSDeng-Cheng Zhu {
209d7d5b05fSDeng-Cheng Zhu 	int i;
210d7d5b05fSDeng-Cheng Zhu 	int index = -1;
211d7d5b05fSDeng-Cheng Zhu 	struct kvm_mips_tlb *tlb = vcpu->arch.guest_tlb;
212d7d5b05fSDeng-Cheng Zhu 
213d7d5b05fSDeng-Cheng Zhu 	for (i = 0; i < KVM_MIPS_GUEST_TLB_SIZE; i++) {
214d7d5b05fSDeng-Cheng Zhu 		if (TLB_HI_VPN2_HIT(tlb[i], entryhi) &&
215d7d5b05fSDeng-Cheng Zhu 		    TLB_HI_ASID_HIT(tlb[i], entryhi)) {
216d7d5b05fSDeng-Cheng Zhu 			index = i;
217d7d5b05fSDeng-Cheng Zhu 			break;
218d7d5b05fSDeng-Cheng Zhu 		}
219d7d5b05fSDeng-Cheng Zhu 	}
220d7d5b05fSDeng-Cheng Zhu 
221d7d5b05fSDeng-Cheng Zhu 	kvm_debug("%s: entryhi: %#lx, index: %d lo0: %#lx, lo1: %#lx\n",
222d7d5b05fSDeng-Cheng Zhu 		  __func__, entryhi, index, tlb[i].tlb_lo0, tlb[i].tlb_lo1);
223d7d5b05fSDeng-Cheng Zhu 
224d7d5b05fSDeng-Cheng Zhu 	return index;
225d7d5b05fSDeng-Cheng Zhu }
226cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_guest_tlb_lookup);
227d7d5b05fSDeng-Cheng Zhu 
228d7d5b05fSDeng-Cheng Zhu int kvm_mips_host_tlb_lookup(struct kvm_vcpu *vcpu, unsigned long vaddr)
229d7d5b05fSDeng-Cheng Zhu {
230d7d5b05fSDeng-Cheng Zhu 	unsigned long old_entryhi, flags;
231d7d5b05fSDeng-Cheng Zhu 	int idx;
232d7d5b05fSDeng-Cheng Zhu 
233d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
234d7d5b05fSDeng-Cheng Zhu 
235d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
236d7d5b05fSDeng-Cheng Zhu 
237d7d5b05fSDeng-Cheng Zhu 	if (KVM_GUEST_KERNEL_MODE(vcpu))
238d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi((vaddr & VPN2_MASK) |
239d7d5b05fSDeng-Cheng Zhu 				 kvm_mips_get_kernel_asid(vcpu));
240d7d5b05fSDeng-Cheng Zhu 	else {
241d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi((vaddr & VPN2_MASK) |
242d7d5b05fSDeng-Cheng Zhu 				 kvm_mips_get_user_asid(vcpu));
243d7d5b05fSDeng-Cheng Zhu 	}
244d7d5b05fSDeng-Cheng Zhu 
245d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
246d7d5b05fSDeng-Cheng Zhu 
247d7d5b05fSDeng-Cheng Zhu 	tlb_probe();
248d7d5b05fSDeng-Cheng Zhu 	tlb_probe_hazard();
249d7d5b05fSDeng-Cheng Zhu 	idx = read_c0_index();
250d7d5b05fSDeng-Cheng Zhu 
251d7d5b05fSDeng-Cheng Zhu 	/* Restore old ASID */
252d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
253d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
254d7d5b05fSDeng-Cheng Zhu 
255d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
256d7d5b05fSDeng-Cheng Zhu 
257d7d5b05fSDeng-Cheng Zhu 	kvm_debug("Host TLB lookup, %#lx, idx: %2d\n", vaddr, idx);
258d7d5b05fSDeng-Cheng Zhu 
259d7d5b05fSDeng-Cheng Zhu 	return idx;
260d7d5b05fSDeng-Cheng Zhu }
261cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_lookup);
262d7d5b05fSDeng-Cheng Zhu 
263d7d5b05fSDeng-Cheng Zhu int kvm_mips_host_tlb_inv(struct kvm_vcpu *vcpu, unsigned long va)
264d7d5b05fSDeng-Cheng Zhu {
265d7d5b05fSDeng-Cheng Zhu 	int idx;
266d7d5b05fSDeng-Cheng Zhu 	unsigned long flags, old_entryhi;
267d7d5b05fSDeng-Cheng Zhu 
268d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
269d7d5b05fSDeng-Cheng Zhu 
270d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
271d7d5b05fSDeng-Cheng Zhu 
272d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi((va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu));
273d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
274d7d5b05fSDeng-Cheng Zhu 
275d7d5b05fSDeng-Cheng Zhu 	tlb_probe();
276d7d5b05fSDeng-Cheng Zhu 	tlb_probe_hazard();
277d7d5b05fSDeng-Cheng Zhu 	idx = read_c0_index();
278d7d5b05fSDeng-Cheng Zhu 
279d7d5b05fSDeng-Cheng Zhu 	if (idx >= current_cpu_data.tlbsize)
280d7d5b05fSDeng-Cheng Zhu 		BUG();
281d7d5b05fSDeng-Cheng Zhu 
282d7d5b05fSDeng-Cheng Zhu 	if (idx > 0) {
283d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi(UNIQUE_ENTRYHI(idx));
284d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo0(0);
285d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo1(0);
286d7d5b05fSDeng-Cheng Zhu 		mtc0_tlbw_hazard();
287d7d5b05fSDeng-Cheng Zhu 
288d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
289138f7ad9SJames Hogan 		tlbw_use_hazard();
290d7d5b05fSDeng-Cheng Zhu 	}
291d7d5b05fSDeng-Cheng Zhu 
292d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
293d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
294d7d5b05fSDeng-Cheng Zhu 
295d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
296d7d5b05fSDeng-Cheng Zhu 
297d7d5b05fSDeng-Cheng Zhu 	if (idx > 0)
298d7d5b05fSDeng-Cheng Zhu 		kvm_debug("%s: Invalidated entryhi %#lx @ idx %d\n", __func__,
299d7d5b05fSDeng-Cheng Zhu 			  (va & VPN2_MASK) | kvm_mips_get_user_asid(vcpu), idx);
300d7d5b05fSDeng-Cheng Zhu 
301d7d5b05fSDeng-Cheng Zhu 	return 0;
302d7d5b05fSDeng-Cheng Zhu }
303cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_host_tlb_inv);
304d7d5b05fSDeng-Cheng Zhu 
305d7d5b05fSDeng-Cheng Zhu void kvm_mips_flush_host_tlb(int skip_kseg0)
306d7d5b05fSDeng-Cheng Zhu {
307d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
308d7d5b05fSDeng-Cheng Zhu 	unsigned long old_entryhi, entryhi;
309d7d5b05fSDeng-Cheng Zhu 	unsigned long old_pagemask;
310d7d5b05fSDeng-Cheng Zhu 	int entry = 0;
311d7d5b05fSDeng-Cheng Zhu 	int maxentry = current_cpu_data.tlbsize;
312d7d5b05fSDeng-Cheng Zhu 
313d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
314d7d5b05fSDeng-Cheng Zhu 
315d7d5b05fSDeng-Cheng Zhu 	old_entryhi = read_c0_entryhi();
316d7d5b05fSDeng-Cheng Zhu 	old_pagemask = read_c0_pagemask();
317d7d5b05fSDeng-Cheng Zhu 
318d7d5b05fSDeng-Cheng Zhu 	/* Blast 'em all away. */
319d7d5b05fSDeng-Cheng Zhu 	for (entry = 0; entry < maxentry; entry++) {
320d7d5b05fSDeng-Cheng Zhu 		write_c0_index(entry);
321d7d5b05fSDeng-Cheng Zhu 
322d7d5b05fSDeng-Cheng Zhu 		if (skip_kseg0) {
323138f7ad9SJames Hogan 			mtc0_tlbr_hazard();
324d7d5b05fSDeng-Cheng Zhu 			tlb_read();
325138f7ad9SJames Hogan 			tlb_read_hazard();
326d7d5b05fSDeng-Cheng Zhu 
327d7d5b05fSDeng-Cheng Zhu 			entryhi = read_c0_entryhi();
328d7d5b05fSDeng-Cheng Zhu 
329d7d5b05fSDeng-Cheng Zhu 			/* Don't blow away guest kernel entries */
330d7d5b05fSDeng-Cheng Zhu 			if (KVM_GUEST_KSEGX(entryhi) == KVM_GUEST_KSEG0)
331d7d5b05fSDeng-Cheng Zhu 				continue;
332d7d5b05fSDeng-Cheng Zhu 		}
333d7d5b05fSDeng-Cheng Zhu 
334d7d5b05fSDeng-Cheng Zhu 		/* Make sure all entries differ. */
335d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi(UNIQUE_ENTRYHI(entry));
336d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo0(0);
337d7d5b05fSDeng-Cheng Zhu 		write_c0_entrylo1(0);
338d7d5b05fSDeng-Cheng Zhu 		mtc0_tlbw_hazard();
339d7d5b05fSDeng-Cheng Zhu 
340d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
341d7d5b05fSDeng-Cheng Zhu 		tlbw_use_hazard();
342138f7ad9SJames Hogan 	}
343d7d5b05fSDeng-Cheng Zhu 
344d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_entryhi);
345d7d5b05fSDeng-Cheng Zhu 	write_c0_pagemask(old_pagemask);
346d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
347d7d5b05fSDeng-Cheng Zhu 
348d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
349d7d5b05fSDeng-Cheng Zhu }
350cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_mips_flush_host_tlb);
351d7d5b05fSDeng-Cheng Zhu 
352d7d5b05fSDeng-Cheng Zhu void kvm_local_flush_tlb_all(void)
353d7d5b05fSDeng-Cheng Zhu {
354d7d5b05fSDeng-Cheng Zhu 	unsigned long flags;
355d7d5b05fSDeng-Cheng Zhu 	unsigned long old_ctx;
356d7d5b05fSDeng-Cheng Zhu 	int entry = 0;
357d7d5b05fSDeng-Cheng Zhu 
358d7d5b05fSDeng-Cheng Zhu 	local_irq_save(flags);
359d7d5b05fSDeng-Cheng Zhu 	/* Save old context and create impossible VPN2 value */
360d7d5b05fSDeng-Cheng Zhu 	old_ctx = read_c0_entryhi();
361d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo0(0);
362d7d5b05fSDeng-Cheng Zhu 	write_c0_entrylo1(0);
363d7d5b05fSDeng-Cheng Zhu 
364d7d5b05fSDeng-Cheng Zhu 	/* Blast 'em all away. */
365d7d5b05fSDeng-Cheng Zhu 	while (entry < current_cpu_data.tlbsize) {
366d7d5b05fSDeng-Cheng Zhu 		/* Make sure all entries differ. */
367d7d5b05fSDeng-Cheng Zhu 		write_c0_entryhi(UNIQUE_ENTRYHI(entry));
368d7d5b05fSDeng-Cheng Zhu 		write_c0_index(entry);
369d7d5b05fSDeng-Cheng Zhu 		mtc0_tlbw_hazard();
370d7d5b05fSDeng-Cheng Zhu 		tlb_write_indexed();
371138f7ad9SJames Hogan 		tlbw_use_hazard();
372d7d5b05fSDeng-Cheng Zhu 		entry++;
373d7d5b05fSDeng-Cheng Zhu 	}
374d7d5b05fSDeng-Cheng Zhu 	write_c0_entryhi(old_ctx);
375d7d5b05fSDeng-Cheng Zhu 	mtc0_tlbw_hazard();
376d7d5b05fSDeng-Cheng Zhu 
377d7d5b05fSDeng-Cheng Zhu 	local_irq_restore(flags);
378d7d5b05fSDeng-Cheng Zhu }
379cb1b447fSJames Hogan EXPORT_SYMBOL_GPL(kvm_local_flush_tlb_all);
380