xref: /openbmc/linux/arch/arm64/kvm/hyp/vhe/tlb.c (revision 6354d150)
1e03fa291SDavid Brazdil // SPDX-License-Identifier: GPL-2.0-only
2e03fa291SDavid Brazdil /*
3e03fa291SDavid Brazdil  * Copyright (C) 2015 - ARM Ltd
4e03fa291SDavid Brazdil  * Author: Marc Zyngier <marc.zyngier@arm.com>
5e03fa291SDavid Brazdil  */
6e03fa291SDavid Brazdil 
7e03fa291SDavid Brazdil #include <linux/irqflags.h>
8e03fa291SDavid Brazdil 
9e03fa291SDavid Brazdil #include <asm/kvm_hyp.h>
10e03fa291SDavid Brazdil #include <asm/kvm_mmu.h>
11e03fa291SDavid Brazdil #include <asm/tlbflush.h>
12e03fa291SDavid Brazdil 
13e03fa291SDavid Brazdil struct tlb_inv_context {
14e03fa291SDavid Brazdil 	unsigned long	flags;
15e03fa291SDavid Brazdil 	u64		tcr;
16e03fa291SDavid Brazdil 	u64		sctlr;
17e03fa291SDavid Brazdil };
18e03fa291SDavid Brazdil 
__tlb_switch_to_guest(struct kvm_s2_mmu * mmu,struct tlb_inv_context * cxt)19a0e50aa3SChristoffer Dall static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,
20a0e50aa3SChristoffer Dall 				  struct tlb_inv_context *cxt)
21e03fa291SDavid Brazdil {
22e03fa291SDavid Brazdil 	u64 val;
23e03fa291SDavid Brazdil 
24e03fa291SDavid Brazdil 	local_irq_save(cxt->flags);
25e03fa291SDavid Brazdil 
26e03fa291SDavid Brazdil 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
27e03fa291SDavid Brazdil 		/*
28e03fa291SDavid Brazdil 		 * For CPUs that are affected by ARM errata 1165522 or 1530923,
29e03fa291SDavid Brazdil 		 * we cannot trust stage-1 to be in a correct state at that
30e03fa291SDavid Brazdil 		 * point. Since we do not want to force a full load of the
31e03fa291SDavid Brazdil 		 * vcpu state, we prevent the EL1 page-table walker to
32e03fa291SDavid Brazdil 		 * allocate new TLBs. This is done by setting the EPD bits
33e03fa291SDavid Brazdil 		 * in the TCR_EL1 register. We also need to prevent it to
34e03fa291SDavid Brazdil 		 * allocate IPA->PA walks, so we enable the S1 MMU...
35e03fa291SDavid Brazdil 		 */
36e03fa291SDavid Brazdil 		val = cxt->tcr = read_sysreg_el1(SYS_TCR);
37e03fa291SDavid Brazdil 		val |= TCR_EPD1_MASK | TCR_EPD0_MASK;
38e03fa291SDavid Brazdil 		write_sysreg_el1(val, SYS_TCR);
39e03fa291SDavid Brazdil 		val = cxt->sctlr = read_sysreg_el1(SYS_SCTLR);
40e03fa291SDavid Brazdil 		val |= SCTLR_ELx_M;
41e03fa291SDavid Brazdil 		write_sysreg_el1(val, SYS_SCTLR);
42e03fa291SDavid Brazdil 	}
43e03fa291SDavid Brazdil 
44e03fa291SDavid Brazdil 	/*
45e03fa291SDavid Brazdil 	 * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
46e03fa291SDavid Brazdil 	 * most TLB operations target EL2/EL0. In order to affect the
47e03fa291SDavid Brazdil 	 * guest TLBs (EL1/EL0), we need to change one of these two
48e03fa291SDavid Brazdil 	 * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
49e03fa291SDavid Brazdil 	 * let's flip TGE before executing the TLB operation.
50e03fa291SDavid Brazdil 	 *
51e03fa291SDavid Brazdil 	 * ARM erratum 1165522 requires some special handling (again),
52e03fa291SDavid Brazdil 	 * as we need to make sure both stages of translation are in
534efc0edeSMarc Zyngier 	 * place before clearing TGE. __load_stage2() already
54e03fa291SDavid Brazdil 	 * has an ISB in order to deal with this.
55e03fa291SDavid Brazdil 	 */
564efc0edeSMarc Zyngier 	__load_stage2(mmu, mmu->arch);
57e03fa291SDavid Brazdil 	val = read_sysreg(hcr_el2);
58e03fa291SDavid Brazdil 	val &= ~HCR_TGE;
59e03fa291SDavid Brazdil 	write_sysreg(val, hcr_el2);
60e03fa291SDavid Brazdil 	isb();
61e03fa291SDavid Brazdil }
62e03fa291SDavid Brazdil 
__tlb_switch_to_host(struct tlb_inv_context * cxt)63a0e50aa3SChristoffer Dall static void __tlb_switch_to_host(struct tlb_inv_context *cxt)
64e03fa291SDavid Brazdil {
65e03fa291SDavid Brazdil 	/*
66e03fa291SDavid Brazdil 	 * We're done with the TLB operation, let's restore the host's
67e03fa291SDavid Brazdil 	 * view of HCR_EL2.
68e03fa291SDavid Brazdil 	 */
69e03fa291SDavid Brazdil 	write_sysreg(0, vttbr_el2);
70e03fa291SDavid Brazdil 	write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
71e03fa291SDavid Brazdil 	isb();
72e03fa291SDavid Brazdil 
73e03fa291SDavid Brazdil 	if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {
74e03fa291SDavid Brazdil 		/* Restore the registers to what they were */
75e03fa291SDavid Brazdil 		write_sysreg_el1(cxt->tcr, SYS_TCR);
76e03fa291SDavid Brazdil 		write_sysreg_el1(cxt->sctlr, SYS_SCTLR);
77e03fa291SDavid Brazdil 	}
78e03fa291SDavid Brazdil 
79e03fa291SDavid Brazdil 	local_irq_restore(cxt->flags);
80e03fa291SDavid Brazdil }
81e03fa291SDavid Brazdil 
__kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu * mmu,phys_addr_t ipa,int level)82efaa5b93SMarc Zyngier void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,
83efaa5b93SMarc Zyngier 			      phys_addr_t ipa, int level)
84e03fa291SDavid Brazdil {
85e03fa291SDavid Brazdil 	struct tlb_inv_context cxt;
86e03fa291SDavid Brazdil 
87e03fa291SDavid Brazdil 	dsb(ishst);
88e03fa291SDavid Brazdil 
89e03fa291SDavid Brazdil 	/* Switch to requested VMID */
90a0e50aa3SChristoffer Dall 	__tlb_switch_to_guest(mmu, &cxt);
91e03fa291SDavid Brazdil 
92e03fa291SDavid Brazdil 	/*
93e03fa291SDavid Brazdil 	 * We could do so much better if we had the VA as well.
94e03fa291SDavid Brazdil 	 * Instead, we invalidate Stage-2 for this IPA, and the
95e03fa291SDavid Brazdil 	 * whole of Stage-1. Weep...
96e03fa291SDavid Brazdil 	 */
97e03fa291SDavid Brazdil 	ipa >>= 12;
98efaa5b93SMarc Zyngier 	__tlbi_level(ipas2e1is, ipa, level);
99e03fa291SDavid Brazdil 
100e03fa291SDavid Brazdil 	/*
101e03fa291SDavid Brazdil 	 * We have to ensure completion of the invalidation at Stage-2,
102e03fa291SDavid Brazdil 	 * since a table walk on another CPU could refill a TLB with a
103e03fa291SDavid Brazdil 	 * complete (S1 + S2) walk based on the old Stage-2 mapping if
104e03fa291SDavid Brazdil 	 * the Stage-1 invalidation happened first.
105e03fa291SDavid Brazdil 	 */
106e03fa291SDavid Brazdil 	dsb(ish);
107e03fa291SDavid Brazdil 	__tlbi(vmalle1is);
108e03fa291SDavid Brazdil 	dsb(ish);
109e03fa291SDavid Brazdil 	isb();
110e03fa291SDavid Brazdil 
111a0e50aa3SChristoffer Dall 	__tlb_switch_to_host(&cxt);
112e03fa291SDavid Brazdil }
113e03fa291SDavid Brazdil 
__kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu * mmu,phys_addr_t ipa,int level)114a12ab137SMarc Zyngier void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,
115a12ab137SMarc Zyngier 				  phys_addr_t ipa, int level)
116a12ab137SMarc Zyngier {
117a12ab137SMarc Zyngier 	struct tlb_inv_context cxt;
118a12ab137SMarc Zyngier 
119a12ab137SMarc Zyngier 	dsb(nshst);
120a12ab137SMarc Zyngier 
121a12ab137SMarc Zyngier 	/* Switch to requested VMID */
122a12ab137SMarc Zyngier 	__tlb_switch_to_guest(mmu, &cxt);
123a12ab137SMarc Zyngier 
124a12ab137SMarc Zyngier 	/*
125a12ab137SMarc Zyngier 	 * We could do so much better if we had the VA as well.
126a12ab137SMarc Zyngier 	 * Instead, we invalidate Stage-2 for this IPA, and the
127a12ab137SMarc Zyngier 	 * whole of Stage-1. Weep...
128a12ab137SMarc Zyngier 	 */
129a12ab137SMarc Zyngier 	ipa >>= 12;
130a12ab137SMarc Zyngier 	__tlbi_level(ipas2e1, ipa, level);
131a12ab137SMarc Zyngier 
132a12ab137SMarc Zyngier 	/*
133a12ab137SMarc Zyngier 	 * We have to ensure completion of the invalidation at Stage-2,
134a12ab137SMarc Zyngier 	 * since a table walk on another CPU could refill a TLB with a
135a12ab137SMarc Zyngier 	 * complete (S1 + S2) walk based on the old Stage-2 mapping if
136a12ab137SMarc Zyngier 	 * the Stage-1 invalidation happened first.
137a12ab137SMarc Zyngier 	 */
138a12ab137SMarc Zyngier 	dsb(nsh);
139a12ab137SMarc Zyngier 	__tlbi(vmalle1);
140a12ab137SMarc Zyngier 	dsb(nsh);
141a12ab137SMarc Zyngier 	isb();
142a12ab137SMarc Zyngier 
143a12ab137SMarc Zyngier 	__tlb_switch_to_host(&cxt);
144a12ab137SMarc Zyngier }
145a12ab137SMarc Zyngier 
__kvm_tlb_flush_vmid_range(struct kvm_s2_mmu * mmu,phys_addr_t start,unsigned long pages)146*6354d150SRaghavendra Rao Ananta void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
147*6354d150SRaghavendra Rao Ananta 				phys_addr_t start, unsigned long pages)
148*6354d150SRaghavendra Rao Ananta {
149*6354d150SRaghavendra Rao Ananta 	struct tlb_inv_context cxt;
150*6354d150SRaghavendra Rao Ananta 	unsigned long stride;
151*6354d150SRaghavendra Rao Ananta 
152*6354d150SRaghavendra Rao Ananta 	/*
153*6354d150SRaghavendra Rao Ananta 	 * Since the range of addresses may not be mapped at
154*6354d150SRaghavendra Rao Ananta 	 * the same level, assume the worst case as PAGE_SIZE
155*6354d150SRaghavendra Rao Ananta 	 */
156*6354d150SRaghavendra Rao Ananta 	stride = PAGE_SIZE;
157*6354d150SRaghavendra Rao Ananta 	start = round_down(start, stride);
158*6354d150SRaghavendra Rao Ananta 
159*6354d150SRaghavendra Rao Ananta 	dsb(ishst);
160*6354d150SRaghavendra Rao Ananta 
161*6354d150SRaghavendra Rao Ananta 	/* Switch to requested VMID */
162*6354d150SRaghavendra Rao Ananta 	__tlb_switch_to_guest(mmu, &cxt);
163*6354d150SRaghavendra Rao Ananta 
164*6354d150SRaghavendra Rao Ananta 	__flush_s2_tlb_range_op(ipas2e1is, start, pages, stride, 0);
165*6354d150SRaghavendra Rao Ananta 
166*6354d150SRaghavendra Rao Ananta 	dsb(ish);
167*6354d150SRaghavendra Rao Ananta 	__tlbi(vmalle1is);
168*6354d150SRaghavendra Rao Ananta 	dsb(ish);
169*6354d150SRaghavendra Rao Ananta 	isb();
170*6354d150SRaghavendra Rao Ananta 
171*6354d150SRaghavendra Rao Ananta 	__tlb_switch_to_host(&cxt);
172*6354d150SRaghavendra Rao Ananta }
173*6354d150SRaghavendra Rao Ananta 
__kvm_tlb_flush_vmid(struct kvm_s2_mmu * mmu)174a0e50aa3SChristoffer Dall void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu)
175e03fa291SDavid Brazdil {
176e03fa291SDavid Brazdil 	struct tlb_inv_context cxt;
177e03fa291SDavid Brazdil 
178e03fa291SDavid Brazdil 	dsb(ishst);
179e03fa291SDavid Brazdil 
180e03fa291SDavid Brazdil 	/* Switch to requested VMID */
181a0e50aa3SChristoffer Dall 	__tlb_switch_to_guest(mmu, &cxt);
182e03fa291SDavid Brazdil 
183e03fa291SDavid Brazdil 	__tlbi(vmalls12e1is);
184e03fa291SDavid Brazdil 	dsb(ish);
185e03fa291SDavid Brazdil 	isb();
186e03fa291SDavid Brazdil 
187a0e50aa3SChristoffer Dall 	__tlb_switch_to_host(&cxt);
188e03fa291SDavid Brazdil }
189e03fa291SDavid Brazdil 
__kvm_flush_cpu_context(struct kvm_s2_mmu * mmu)19001dc9262SMarc Zyngier void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu)
191e03fa291SDavid Brazdil {
192e03fa291SDavid Brazdil 	struct tlb_inv_context cxt;
193e03fa291SDavid Brazdil 
194e03fa291SDavid Brazdil 	/* Switch to requested VMID */
195a0e50aa3SChristoffer Dall 	__tlb_switch_to_guest(mmu, &cxt);
196e03fa291SDavid Brazdil 
197e03fa291SDavid Brazdil 	__tlbi(vmalle1);
19801dc9262SMarc Zyngier 	asm volatile("ic iallu");
199e03fa291SDavid Brazdil 	dsb(nsh);
200e03fa291SDavid Brazdil 	isb();
201e03fa291SDavid Brazdil 
202a0e50aa3SChristoffer Dall 	__tlb_switch_to_host(&cxt);
203e03fa291SDavid Brazdil }
204e03fa291SDavid Brazdil 
__kvm_flush_vm_context(void)205e03fa291SDavid Brazdil void __kvm_flush_vm_context(void)
206e03fa291SDavid Brazdil {
207e03fa291SDavid Brazdil 	dsb(ishst);
208e03fa291SDavid Brazdil 	__tlbi(alle1is);
209e03fa291SDavid Brazdil 
210e03fa291SDavid Brazdil 	/*
211e03fa291SDavid Brazdil 	 * VIPT and PIPT caches are not affected by VMID, so no maintenance
212e03fa291SDavid Brazdil 	 * is necessary across a VMID rollover.
213e03fa291SDavid Brazdil 	 *
214e03fa291SDavid Brazdil 	 * VPIPT caches constrain lookup and maintenance to the active VMID,
215e03fa291SDavid Brazdil 	 * so we need to invalidate lines with a stale VMID to avoid an ABA
216e03fa291SDavid Brazdil 	 * race after multiple rollovers.
217e03fa291SDavid Brazdil 	 *
218e03fa291SDavid Brazdil 	 */
219e03fa291SDavid Brazdil 	if (icache_is_vpipt())
220e03fa291SDavid Brazdil 		asm volatile("ic ialluis");
221e03fa291SDavid Brazdil 
222e03fa291SDavid Brazdil 	dsb(ish);
223e03fa291SDavid Brazdil }
224