GNU Linux-libre 4.9.333-gnu1
[releases.git] / arch / arm64 / kvm / hyp / tlb.c
1 /*
2  * Copyright (C) 2015 - ARM Ltd
3  * Author: Marc Zyngier <marc.zyngier@arm.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <asm/kvm_hyp.h>
19
20 static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm)
21 {
22         u64 val;
23
24         /*
25          * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
26          * most TLB operations target EL2/EL0. In order to affect the
27          * guest TLBs (EL1/EL0), we need to change one of these two
28          * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
29          * let's flip TGE before executing the TLB operation.
30          */
31         write_sysreg(kvm->arch.vttbr, vttbr_el2);
32         val = read_sysreg(hcr_el2);
33         val &= ~HCR_TGE;
34         write_sysreg(val, hcr_el2);
35         isb();
36 }
37
38 static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm)
39 {
40         write_sysreg(kvm->arch.vttbr, vttbr_el2);
41         isb();
42 }
43
44 static hyp_alternate_select(__tlb_switch_to_guest,
45                             __tlb_switch_to_guest_nvhe,
46                             __tlb_switch_to_guest_vhe,
47                             ARM64_HAS_VIRT_HOST_EXTN);
48
49 static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm)
50 {
51         /*
52          * We're done with the TLB operation, let's restore the host's
53          * view of HCR_EL2.
54          */
55         write_sysreg(0, vttbr_el2);
56         write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
57 }
58
59 static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm)
60 {
61         write_sysreg(0, vttbr_el2);
62 }
63
64 static hyp_alternate_select(__tlb_switch_to_host,
65                             __tlb_switch_to_host_nvhe,
66                             __tlb_switch_to_host_vhe,
67                             ARM64_HAS_VIRT_HOST_EXTN);
68
69 void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
70 {
71         dsb(ishst);
72
73         /* Switch to requested VMID */
74         kvm = kern_hyp_va(kvm);
75         __tlb_switch_to_guest()(kvm);
76
77         /*
78          * We could do so much better if we had the VA as well.
79          * Instead, we invalidate Stage-2 for this IPA, and the
80          * whole of Stage-1. Weep...
81          */
82         ipa >>= 12;
83         asm volatile("tlbi ipas2e1is, %0" : : "r" (ipa));
84
85         /*
86          * We have to ensure completion of the invalidation at Stage-2,
87          * since a table walk on another CPU could refill a TLB with a
88          * complete (S1 + S2) walk based on the old Stage-2 mapping if
89          * the Stage-1 invalidation happened first.
90          */
91         dsb(ish);
92         asm volatile("tlbi vmalle1is" : : );
93         dsb(ish);
94         isb();
95
96         __tlb_switch_to_host()(kvm);
97 }
98
99 void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
100 {
101         dsb(ishst);
102
103         /* Switch to requested VMID */
104         kvm = kern_hyp_va(kvm);
105         __tlb_switch_to_guest()(kvm);
106
107         asm volatile("tlbi vmalls12e1is" : : );
108         dsb(ish);
109         isb();
110
111         __tlb_switch_to_host()(kvm);
112 }
113
114 void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
115 {
116         struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
117
118         /* Switch to requested VMID */
119         __tlb_switch_to_guest()(kvm);
120
121         asm volatile("tlbi vmalle1" : : );
122         dsb(nsh);
123         isb();
124
125         __tlb_switch_to_host()(kvm);
126 }
127
128 void __hyp_text __kvm_flush_vm_context(void)
129 {
130         dsb(ishst);
131         asm volatile("tlbi alle1is      \n"
132                      "ic ialluis          ": : );
133         dsb(ish);
134 }