Merge branches 'acpi-tables', 'acpi-osl', 'acpi-misc' and 'acpi-tools'
[sfrench/cifs-2.6.git] / arch / arm64 / kvm / hyp / tlb.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6
7 #include <linux/irqflags.h>
8
9 #include <asm/kvm_hyp.h>
10 #include <asm/kvm_mmu.h>
11 #include <asm/tlbflush.h>
12
13 struct tlb_inv_context {
14         unsigned long   flags;
15         u64             tcr;
16         u64             sctlr;
17 };
18
19 static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm,
20                                                  struct tlb_inv_context *cxt)
21 {
22         u64 val;
23
24         local_irq_save(cxt->flags);
25
26         if (cpus_have_const_cap(ARM64_WORKAROUND_1165522)) {
27                 /*
28                  * For CPUs that are affected by ARM erratum 1165522, we
29                  * cannot trust stage-1 to be in a correct state at that
30                  * point. Since we do not want to force a full load of the
31                  * vcpu state, we prevent the EL1 page-table walker to
32                  * allocate new TLBs. This is done by setting the EPD bits
33                  * in the TCR_EL1 register. We also need to prevent it to
34                  * allocate IPA->PA walks, so we enable the S1 MMU...
35                  */
36                 val = cxt->tcr = read_sysreg_el1(tcr);
37                 val |= TCR_EPD1_MASK | TCR_EPD0_MASK;
38                 write_sysreg_el1(val, tcr);
39                 val = cxt->sctlr = read_sysreg_el1(sctlr);
40                 val |= SCTLR_ELx_M;
41                 write_sysreg_el1(val, sctlr);
42         }
43
44         /*
45          * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
46          * most TLB operations target EL2/EL0. In order to affect the
47          * guest TLBs (EL1/EL0), we need to change one of these two
48          * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
49          * let's flip TGE before executing the TLB operation.
50          *
51          * ARM erratum 1165522 requires some special handling (again),
52          * as we need to make sure both stages of translation are in
53          * place before clearing TGE. __load_guest_stage2() already
54          * has an ISB in order to deal with this.
55          */
56         __load_guest_stage2(kvm);
57         val = read_sysreg(hcr_el2);
58         val &= ~HCR_TGE;
59         write_sysreg(val, hcr_el2);
60         isb();
61 }
62
63 static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm,
64                                                   struct tlb_inv_context *cxt)
65 {
66         __load_guest_stage2(kvm);
67         isb();
68 }
69
70 static hyp_alternate_select(__tlb_switch_to_guest,
71                             __tlb_switch_to_guest_nvhe,
72                             __tlb_switch_to_guest_vhe,
73                             ARM64_HAS_VIRT_HOST_EXTN);
74
75 static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm,
76                                                 struct tlb_inv_context *cxt)
77 {
78         /*
79          * We're done with the TLB operation, let's restore the host's
80          * view of HCR_EL2.
81          */
82         write_sysreg(0, vttbr_el2);
83         write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
84         isb();
85
86         if (cpus_have_const_cap(ARM64_WORKAROUND_1165522)) {
87                 /* Restore the registers to what they were */
88                 write_sysreg_el1(cxt->tcr, tcr);
89                 write_sysreg_el1(cxt->sctlr, sctlr);
90         }
91
92         local_irq_restore(cxt->flags);
93 }
94
95 static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm,
96                                                  struct tlb_inv_context *cxt)
97 {
98         write_sysreg(0, vttbr_el2);
99 }
100
101 static hyp_alternate_select(__tlb_switch_to_host,
102                             __tlb_switch_to_host_nvhe,
103                             __tlb_switch_to_host_vhe,
104                             ARM64_HAS_VIRT_HOST_EXTN);
105
106 void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
107 {
108         struct tlb_inv_context cxt;
109
110         dsb(ishst);
111
112         /* Switch to requested VMID */
113         kvm = kern_hyp_va(kvm);
114         __tlb_switch_to_guest()(kvm, &cxt);
115
116         /*
117          * We could do so much better if we had the VA as well.
118          * Instead, we invalidate Stage-2 for this IPA, and the
119          * whole of Stage-1. Weep...
120          */
121         ipa >>= 12;
122         __tlbi(ipas2e1is, ipa);
123
124         /*
125          * We have to ensure completion of the invalidation at Stage-2,
126          * since a table walk on another CPU could refill a TLB with a
127          * complete (S1 + S2) walk based on the old Stage-2 mapping if
128          * the Stage-1 invalidation happened first.
129          */
130         dsb(ish);
131         __tlbi(vmalle1is);
132         dsb(ish);
133         isb();
134
135         /*
136          * If the host is running at EL1 and we have a VPIPT I-cache,
137          * then we must perform I-cache maintenance at EL2 in order for
138          * it to have an effect on the guest. Since the guest cannot hit
139          * I-cache lines allocated with a different VMID, we don't need
140          * to worry about junk out of guest reset (we nuke the I-cache on
141          * VMID rollover), but we do need to be careful when remapping
142          * executable pages for the same guest. This can happen when KSM
143          * takes a CoW fault on an executable page, copies the page into
144          * a page that was previously mapped in the guest and then needs
145          * to invalidate the guest view of the I-cache for that page
146          * from EL1. To solve this, we invalidate the entire I-cache when
147          * unmapping a page from a guest if we have a VPIPT I-cache but
148          * the host is running at EL1. As above, we could do better if
149          * we had the VA.
150          *
151          * The moral of this story is: if you have a VPIPT I-cache, then
152          * you should be running with VHE enabled.
153          */
154         if (!has_vhe() && icache_is_vpipt())
155                 __flush_icache_all();
156
157         __tlb_switch_to_host()(kvm, &cxt);
158 }
159
160 void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
161 {
162         struct tlb_inv_context cxt;
163
164         dsb(ishst);
165
166         /* Switch to requested VMID */
167         kvm = kern_hyp_va(kvm);
168         __tlb_switch_to_guest()(kvm, &cxt);
169
170         __tlbi(vmalls12e1is);
171         dsb(ish);
172         isb();
173
174         __tlb_switch_to_host()(kvm, &cxt);
175 }
176
177 void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
178 {
179         struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
180         struct tlb_inv_context cxt;
181
182         /* Switch to requested VMID */
183         __tlb_switch_to_guest()(kvm, &cxt);
184
185         __tlbi(vmalle1);
186         dsb(nsh);
187         isb();
188
189         __tlb_switch_to_host()(kvm, &cxt);
190 }
191
192 void __hyp_text __kvm_flush_vm_context(void)
193 {
194         dsb(ishst);
195         __tlbi(alle1is);
196         asm volatile("ic ialluis" : : );
197         dsb(ish);
198 }