1
2
3
4
5
6
7
8#include <linux/irqflags.h>
9#include <linux/sched.h>
10#include <linux/thread_info.h>
11#include <linux/kvm_host.h>
12#include <asm/fpsimd.h>
13#include <asm/kvm_asm.h>
14#include <asm/kvm_hyp.h>
15#include <asm/kvm_mmu.h>
16#include <asm/sysreg.h>
17
18
19
20
21
22
23
24
25
26
27int kvm_arch_vcpu_run_map_fp(struct kvm_vcpu *vcpu)
28{
29 int ret;
30
31 struct thread_info *ti = ¤t->thread_info;
32 struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state;
33
34
35
36
37
38 ret = create_hyp_mappings(ti, ti + 1, PAGE_HYP);
39 if (ret)
40 goto error;
41
42 ret = create_hyp_mappings(fpsimd, fpsimd + 1, PAGE_HYP);
43 if (ret)
44 goto error;
45
46 if (vcpu->arch.sve_state) {
47 void *sve_end;
48
49 sve_end = vcpu->arch.sve_state + vcpu_sve_state_size(vcpu);
50
51 ret = create_hyp_mappings(vcpu->arch.sve_state, sve_end,
52 PAGE_HYP);
53 if (ret)
54 goto error;
55 }
56
57 vcpu->arch.host_thread_info = kern_hyp_va(ti);
58 vcpu->arch.host_fpsimd_state = kern_hyp_va(fpsimd);
59error:
60 return ret;
61}
62
63
64
65
66
67
68
69
70
71
72
73void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
74{
75 BUG_ON(!current->mm);
76
77 vcpu->arch.flags &= ~(KVM_ARM64_FP_ENABLED |
78 KVM_ARM64_HOST_SVE_IN_USE |
79 KVM_ARM64_HOST_SVE_ENABLED);
80 vcpu->arch.flags |= KVM_ARM64_FP_HOST;
81
82 if (test_thread_flag(TIF_SVE))
83 vcpu->arch.flags |= KVM_ARM64_HOST_SVE_IN_USE;
84
85 if (read_sysreg(cpacr_el1) & CPACR_EL1_ZEN_EL0EN)
86 vcpu->arch.flags |= KVM_ARM64_HOST_SVE_ENABLED;
87}
88
89
90
91
92
93
94
95void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
96{
97 WARN_ON_ONCE(!irqs_disabled());
98
99 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
100 fpsimd_bind_state_to_cpu(&vcpu->arch.ctxt.fp_regs,
101 vcpu->arch.sve_state,
102 vcpu->arch.sve_max_vl);
103
104 clear_thread_flag(TIF_FOREIGN_FPSTATE);
105 update_thread_flag(TIF_SVE, vcpu_has_sve(vcpu));
106 }
107}
108
109
110
111
112
113
114
115void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
116{
117 unsigned long flags;
118 bool host_has_sve = system_supports_sve();
119 bool guest_has_sve = vcpu_has_sve(vcpu);
120
121 local_irq_save(flags);
122
123 if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) {
124 if (guest_has_sve) {
125 __vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
126
127
128 if (!has_vhe())
129 sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1,
130 SYS_ZCR_EL1);
131 }
132
133 fpsimd_save_and_flush_cpu_state();
134 } else if (has_vhe() && host_has_sve) {
135
136
137
138
139
140
141
142 if (vcpu->arch.flags & KVM_ARM64_HOST_SVE_ENABLED)
143 sysreg_clear_set(CPACR_EL1, 0, CPACR_EL1_ZEN_EL0EN);
144 else
145 sysreg_clear_set(CPACR_EL1, CPACR_EL1_ZEN_EL0EN, 0);
146 }
147
148 update_thread_flag(TIF_SVE,
149 vcpu->arch.flags & KVM_ARM64_HOST_SVE_IN_USE);
150
151 local_irq_restore(flags);
152}
153