1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2012 ARM Ltd.
4 */
5 #ifndef __ASM_FP_H
6 #define __ASM_FP_H
7
8 #include <asm/errno.h>
9 #include <asm/ptrace.h>
10 #include <asm/processor.h>
11 #include <asm/sigcontext.h>
12 #include <asm/sysreg.h>
13
14 #ifndef __ASSEMBLY__
15
16 #include <linux/bitmap.h>
17 #include <linux/build_bug.h>
18 #include <linux/bug.h>
19 #include <linux/cache.h>
20 #include <linux/init.h>
21 #include <linux/stddef.h>
22 #include <linux/types.h>
23
24 /* Masks for extracting the FPSR and FPCR from the FPSCR */
25 #define VFP_FPSCR_STAT_MASK 0xf800009f
26 #define VFP_FPSCR_CTRL_MASK 0x07f79f00
27 /*
28 * The VFP state has 32x64-bit registers and a single 32-bit
29 * control/status register.
30 */
31 #define VFP_STATE_SIZE ((32 * 8) + 4)
32
cpacr_save_enable_kernel_sve(void)33 static inline unsigned long cpacr_save_enable_kernel_sve(void)
34 {
35 unsigned long old = read_sysreg(cpacr_el1);
36 unsigned long set = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_ZEN_EL1EN;
37
38 write_sysreg(old | set, cpacr_el1);
39 isb();
40 return old;
41 }
42
cpacr_save_enable_kernel_sme(void)43 static inline unsigned long cpacr_save_enable_kernel_sme(void)
44 {
45 unsigned long old = read_sysreg(cpacr_el1);
46 unsigned long set = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_SMEN_EL1EN;
47
48 write_sysreg(old | set, cpacr_el1);
49 isb();
50 return old;
51 }
52
cpacr_restore(unsigned long cpacr)53 static inline void cpacr_restore(unsigned long cpacr)
54 {
55 write_sysreg(cpacr, cpacr_el1);
56 isb();
57 }
58
59 /*
60 * When we defined the maximum SVE vector length we defined the ABI so
61 * that the maximum vector length included all the reserved for future
62 * expansion bits in ZCR rather than those just currently defined by
63 * the architecture. Using this length to allocate worst size buffers
64 * results in excessively large allocations, and this effect is even
65 * more pronounced for SME due to ZA. Define more suitable VLs for
66 * these situations.
67 */
68 #define ARCH_SVE_VQ_MAX ((ZCR_ELx_LEN_MASK >> ZCR_ELx_LEN_SHIFT) + 1)
69 #define SME_VQ_MAX ((SMCR_ELx_LEN_MASK >> SMCR_ELx_LEN_SHIFT) + 1)
70
71 struct task_struct;
72
73 extern void fpsimd_save_state(struct user_fpsimd_state *state);
74 extern void fpsimd_load_state(struct user_fpsimd_state *state);
75
76 extern void fpsimd_thread_switch(struct task_struct *next);
77 extern void fpsimd_flush_thread(void);
78
79 extern void fpsimd_signal_preserve_current_state(void);
80 extern void fpsimd_preserve_current_state(void);
81 extern void fpsimd_restore_current_state(void);
82 extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
83
84 struct cpu_fp_state {
85 struct user_fpsimd_state *st;
86 void *sve_state;
87 void *sme_state;
88 u64 *svcr;
89 u64 *fpmr;
90 unsigned int sve_vl;
91 unsigned int sme_vl;
92 enum fp_type *fp_type;
93 enum fp_type to_save;
94 };
95
96 extern void fpsimd_bind_state_to_cpu(struct cpu_fp_state *fp_state);
97
98 extern void fpsimd_flush_task_state(struct task_struct *target);
99 extern void fpsimd_save_and_flush_cpu_state(void);
100
thread_sm_enabled(struct thread_struct * thread)101 static inline bool thread_sm_enabled(struct thread_struct *thread)
102 {
103 return system_supports_sme() && (thread->svcr & SVCR_SM_MASK);
104 }
105
thread_za_enabled(struct thread_struct * thread)106 static inline bool thread_za_enabled(struct thread_struct *thread)
107 {
108 return system_supports_sme() && (thread->svcr & SVCR_ZA_MASK);
109 }
110
111 /* Maximum VL that SVE/SME VL-agnostic software can transparently support */
112 #define VL_ARCH_MAX 0x100
113
114 /* Offset of FFR in the SVE register dump */
sve_ffr_offset(int vl)115 static inline size_t sve_ffr_offset(int vl)
116 {
117 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
118 }
119
sve_pffr(struct thread_struct * thread)120 static inline void *sve_pffr(struct thread_struct *thread)
121 {
122 unsigned int vl;
123
124 if (system_supports_sme() && thread_sm_enabled(thread))
125 vl = thread_get_sme_vl(thread);
126 else
127 vl = thread_get_sve_vl(thread);
128
129 return (char *)thread->sve_state + sve_ffr_offset(vl);
130 }
131
thread_zt_state(struct thread_struct * thread)132 static inline void *thread_zt_state(struct thread_struct *thread)
133 {
134 /* The ZT register state is stored immediately after the ZA state */
135 unsigned int sme_vq = sve_vq_from_vl(thread_get_sme_vl(thread));
136 return thread->sme_state + ZA_SIG_REGS_SIZE(sme_vq);
137 }
138
139 extern void sve_save_state(void *state, u32 *pfpsr, int save_ffr);
140 extern void sve_load_state(void const *state, u32 const *pfpsr,
141 int restore_ffr);
142 extern void sve_flush_live(bool flush_ffr, unsigned long vq_minus_1);
143 extern unsigned int sve_get_vl(void);
144 extern void sve_set_vq(unsigned long vq_minus_1);
145 extern void sme_set_vq(unsigned long vq_minus_1);
146 extern void sme_save_state(void *state, int zt);
147 extern void sme_load_state(void const *state, int zt);
148
149 struct arm64_cpu_capabilities;
150 extern void cpu_enable_fpsimd(const struct arm64_cpu_capabilities *__unused);
151 extern void cpu_enable_sve(const struct arm64_cpu_capabilities *__unused);
152 extern void cpu_enable_sme(const struct arm64_cpu_capabilities *__unused);
153 extern void cpu_enable_sme2(const struct arm64_cpu_capabilities *__unused);
154 extern void cpu_enable_fa64(const struct arm64_cpu_capabilities *__unused);
155 extern void cpu_enable_fpmr(const struct arm64_cpu_capabilities *__unused);
156
157 /*
158 * Helpers to translate bit indices in sve_vq_map to VQ values (and
159 * vice versa). This allows find_next_bit() to be used to find the
160 * _maximum_ VQ not exceeding a certain value.
161 */
__vq_to_bit(unsigned int vq)162 static inline unsigned int __vq_to_bit(unsigned int vq)
163 {
164 return SVE_VQ_MAX - vq;
165 }
166
__bit_to_vq(unsigned int bit)167 static inline unsigned int __bit_to_vq(unsigned int bit)
168 {
169 return SVE_VQ_MAX - bit;
170 }
171
172
173 struct vl_info {
174 enum vec_type type;
175 const char *name; /* For display purposes */
176
177 /* Minimum supported vector length across all CPUs */
178 int min_vl;
179
180 /* Maximum supported vector length across all CPUs */
181 int max_vl;
182 int max_virtualisable_vl;
183
184 /*
185 * Set of available vector lengths,
186 * where length vq encoded as bit __vq_to_bit(vq):
187 */
188 DECLARE_BITMAP(vq_map, SVE_VQ_MAX);
189
190 /* Set of vector lengths present on at least one cpu: */
191 DECLARE_BITMAP(vq_partial_map, SVE_VQ_MAX);
192 };
193
194 #ifdef CONFIG_ARM64_SVE
195
196 extern void sve_alloc(struct task_struct *task, bool flush);
197 extern void fpsimd_release_task(struct task_struct *task);
198 extern void fpsimd_sync_to_sve(struct task_struct *task);
199 extern void fpsimd_force_sync_to_sve(struct task_struct *task);
200 extern void sve_sync_to_fpsimd(struct task_struct *task);
201 extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
202
203 extern int vec_set_vector_length(struct task_struct *task, enum vec_type type,
204 unsigned long vl, unsigned long flags);
205
206 extern int sve_set_current_vl(unsigned long arg);
207 extern int sve_get_current_vl(void);
208
sve_user_disable(void)209 static inline void sve_user_disable(void)
210 {
211 sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
212 }
213
sve_user_enable(void)214 static inline void sve_user_enable(void)
215 {
216 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
217 }
218
219 #define sve_cond_update_zcr_vq(val, reg) \
220 do { \
221 u64 __zcr = read_sysreg_s((reg)); \
222 u64 __new = __zcr & ~ZCR_ELx_LEN_MASK; \
223 __new |= (val) & ZCR_ELx_LEN_MASK; \
224 if (__zcr != __new) \
225 write_sysreg_s(__new, (reg)); \
226 } while (0)
227
228 /*
229 * Probing and setup functions.
230 * Calls to these functions must be serialised with one another.
231 */
232 enum vec_type;
233
234 extern void __init vec_init_vq_map(enum vec_type type);
235 extern void vec_update_vq_map(enum vec_type type);
236 extern int vec_verify_vq_map(enum vec_type type);
237 extern void __init sve_setup(void);
238
239 extern __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX];
240
write_vl(enum vec_type type,u64 val)241 static inline void write_vl(enum vec_type type, u64 val)
242 {
243 u64 tmp;
244
245 switch (type) {
246 #ifdef CONFIG_ARM64_SVE
247 case ARM64_VEC_SVE:
248 tmp = read_sysreg_s(SYS_ZCR_EL1) & ~ZCR_ELx_LEN_MASK;
249 write_sysreg_s(tmp | val, SYS_ZCR_EL1);
250 break;
251 #endif
252 #ifdef CONFIG_ARM64_SME
253 case ARM64_VEC_SME:
254 tmp = read_sysreg_s(SYS_SMCR_EL1) & ~SMCR_ELx_LEN_MASK;
255 write_sysreg_s(tmp | val, SYS_SMCR_EL1);
256 break;
257 #endif
258 default:
259 WARN_ON_ONCE(1);
260 break;
261 }
262 }
263
vec_max_vl(enum vec_type type)264 static inline int vec_max_vl(enum vec_type type)
265 {
266 return vl_info[type].max_vl;
267 }
268
vec_max_virtualisable_vl(enum vec_type type)269 static inline int vec_max_virtualisable_vl(enum vec_type type)
270 {
271 return vl_info[type].max_virtualisable_vl;
272 }
273
sve_max_vl(void)274 static inline int sve_max_vl(void)
275 {
276 return vec_max_vl(ARM64_VEC_SVE);
277 }
278
sve_max_virtualisable_vl(void)279 static inline int sve_max_virtualisable_vl(void)
280 {
281 return vec_max_virtualisable_vl(ARM64_VEC_SVE);
282 }
283
284 /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
vq_available(enum vec_type type,unsigned int vq)285 static inline bool vq_available(enum vec_type type, unsigned int vq)
286 {
287 return test_bit(__vq_to_bit(vq), vl_info[type].vq_map);
288 }
289
sve_vq_available(unsigned int vq)290 static inline bool sve_vq_available(unsigned int vq)
291 {
292 return vq_available(ARM64_VEC_SVE, vq);
293 }
294
295 size_t sve_state_size(struct task_struct const *task);
296
297 #else /* ! CONFIG_ARM64_SVE */
298
sve_alloc(struct task_struct * task,bool flush)299 static inline void sve_alloc(struct task_struct *task, bool flush) { }
fpsimd_release_task(struct task_struct * task)300 static inline void fpsimd_release_task(struct task_struct *task) { }
sve_sync_to_fpsimd(struct task_struct * task)301 static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
sve_sync_from_fpsimd_zeropad(struct task_struct * task)302 static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
303
sve_max_virtualisable_vl(void)304 static inline int sve_max_virtualisable_vl(void)
305 {
306 return 0;
307 }
308
sve_set_current_vl(unsigned long arg)309 static inline int sve_set_current_vl(unsigned long arg)
310 {
311 return -EINVAL;
312 }
313
sve_get_current_vl(void)314 static inline int sve_get_current_vl(void)
315 {
316 return -EINVAL;
317 }
318
sve_max_vl(void)319 static inline int sve_max_vl(void)
320 {
321 return -EINVAL;
322 }
323
sve_vq_available(unsigned int vq)324 static inline bool sve_vq_available(unsigned int vq) { return false; }
325
sve_user_disable(void)326 static inline void sve_user_disable(void) { BUILD_BUG(); }
sve_user_enable(void)327 static inline void sve_user_enable(void) { BUILD_BUG(); }
328
329 #define sve_cond_update_zcr_vq(val, reg) do { } while (0)
330
vec_init_vq_map(enum vec_type t)331 static inline void vec_init_vq_map(enum vec_type t) { }
vec_update_vq_map(enum vec_type t)332 static inline void vec_update_vq_map(enum vec_type t) { }
vec_verify_vq_map(enum vec_type t)333 static inline int vec_verify_vq_map(enum vec_type t) { return 0; }
sve_setup(void)334 static inline void sve_setup(void) { }
335
sve_state_size(struct task_struct const * task)336 static inline size_t sve_state_size(struct task_struct const *task)
337 {
338 return 0;
339 }
340
341 #endif /* ! CONFIG_ARM64_SVE */
342
343 #ifdef CONFIG_ARM64_SME
344
sme_user_disable(void)345 static inline void sme_user_disable(void)
346 {
347 sysreg_clear_set(cpacr_el1, CPACR_EL1_SMEN_EL0EN, 0);
348 }
349
sme_user_enable(void)350 static inline void sme_user_enable(void)
351 {
352 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_SMEN_EL0EN);
353 }
354
sme_smstart_sm(void)355 static inline void sme_smstart_sm(void)
356 {
357 asm volatile(__msr_s(SYS_SVCR_SMSTART_SM_EL0, "xzr"));
358 }
359
sme_smstop_sm(void)360 static inline void sme_smstop_sm(void)
361 {
362 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SM_EL0, "xzr"));
363 }
364
sme_smstop(void)365 static inline void sme_smstop(void)
366 {
367 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SMZA_EL0, "xzr"));
368 }
369
370 extern void __init sme_setup(void);
371
sme_max_vl(void)372 static inline int sme_max_vl(void)
373 {
374 return vec_max_vl(ARM64_VEC_SME);
375 }
376
sme_max_virtualisable_vl(void)377 static inline int sme_max_virtualisable_vl(void)
378 {
379 return vec_max_virtualisable_vl(ARM64_VEC_SME);
380 }
381
382 extern void sme_alloc(struct task_struct *task, bool flush);
383 extern unsigned int sme_get_vl(void);
384 extern int sme_set_current_vl(unsigned long arg);
385 extern int sme_get_current_vl(void);
386 extern void sme_suspend_exit(void);
387
388 /*
389 * Return how many bytes of memory are required to store the full SME
390 * specific state for task, given task's currently configured vector
391 * length.
392 */
sme_state_size(struct task_struct const * task)393 static inline size_t sme_state_size(struct task_struct const *task)
394 {
395 unsigned int vl = task_get_sme_vl(task);
396 size_t size;
397
398 size = ZA_SIG_REGS_SIZE(sve_vq_from_vl(vl));
399
400 if (system_supports_sme2())
401 size += ZT_SIG_REG_SIZE;
402
403 return size;
404 }
405
406 #else
407
sme_user_disable(void)408 static inline void sme_user_disable(void) { BUILD_BUG(); }
sme_user_enable(void)409 static inline void sme_user_enable(void) { BUILD_BUG(); }
410
sme_smstart_sm(void)411 static inline void sme_smstart_sm(void) { }
sme_smstop_sm(void)412 static inline void sme_smstop_sm(void) { }
sme_smstop(void)413 static inline void sme_smstop(void) { }
414
sme_alloc(struct task_struct * task,bool flush)415 static inline void sme_alloc(struct task_struct *task, bool flush) { }
sme_setup(void)416 static inline void sme_setup(void) { }
sme_get_vl(void)417 static inline unsigned int sme_get_vl(void) { return 0; }
sme_max_vl(void)418 static inline int sme_max_vl(void) { return 0; }
sme_max_virtualisable_vl(void)419 static inline int sme_max_virtualisable_vl(void) { return 0; }
sme_set_current_vl(unsigned long arg)420 static inline int sme_set_current_vl(unsigned long arg) { return -EINVAL; }
sme_get_current_vl(void)421 static inline int sme_get_current_vl(void) { return -EINVAL; }
sme_suspend_exit(void)422 static inline void sme_suspend_exit(void) { }
423
sme_state_size(struct task_struct const * task)424 static inline size_t sme_state_size(struct task_struct const *task)
425 {
426 return 0;
427 }
428
429 #endif /* ! CONFIG_ARM64_SME */
430
431 /* For use by EFI runtime services calls only */
432 extern void __efi_fpsimd_begin(void);
433 extern void __efi_fpsimd_end(void);
434
435 #endif
436
437 #endif
438