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