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