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