1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 ARM Ltd.
9 #include <asm/ptrace.h>
10 #include <asm/processor.h>
11 #include <asm/sigcontext.h>
12 #include <asm/sysreg.h>
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>
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
29 * The VFP state has 32x64-bit registers and a single 32-bit
30 * control/status register.
32 #define VFP_STATE_SIZE ((32 * 8) + 4)
36 * When we defined the maximum SVE vector length we defined the ABI so
37 * that the maximum vector length included all the reserved for future
38 * expansion bits in ZCR rather than those just currently defined by
39 * the architecture. While SME follows a similar pattern the fact that
40 * it includes a square matrix means that any allocations that attempt
41 * to cover the maximum potential vector length (such as happen with
42 * the regset used for ptrace) end up being extremely large. Define
43 * the much lower actual limit for use in such situations.
49 extern void fpsimd_save_state(struct user_fpsimd_state *state);
50 extern void fpsimd_load_state(struct user_fpsimd_state *state);
52 extern void fpsimd_thread_switch(struct task_struct *next);
53 extern void fpsimd_flush_thread(void);
55 extern void fpsimd_signal_preserve_current_state(void);
56 extern void fpsimd_preserve_current_state(void);
57 extern void fpsimd_restore_current_state(void);
58 extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
59 extern void fpsimd_kvm_prepare(void);
62 struct user_fpsimd_state *st;
68 enum fp_type *fp_type;
72 extern void fpsimd_bind_state_to_cpu(struct cpu_fp_state *fp_state);
74 extern void fpsimd_flush_task_state(struct task_struct *target);
75 extern void fpsimd_save_and_flush_cpu_state(void);
77 static inline bool thread_sm_enabled(struct thread_struct *thread)
79 return system_supports_sme() && (thread->svcr & SVCR_SM_MASK);
82 static inline bool thread_za_enabled(struct thread_struct *thread)
84 return system_supports_sme() && (thread->svcr & SVCR_ZA_MASK);
87 /* Maximum VL that SVE/SME VL-agnostic software can transparently support */
88 #define VL_ARCH_MAX 0x100
90 /* Offset of FFR in the SVE register dump */
91 static inline size_t sve_ffr_offset(int vl)
93 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
96 static inline void *sve_pffr(struct thread_struct *thread)
100 if (system_supports_sme() && thread_sm_enabled(thread))
101 vl = thread_get_sme_vl(thread);
103 vl = thread_get_sve_vl(thread);
105 return (char *)thread->sve_state + sve_ffr_offset(vl);
108 extern void sve_save_state(void *state, u32 *pfpsr, int save_ffr);
109 extern void sve_load_state(void const *state, u32 const *pfpsr,
111 extern void sve_flush_live(bool flush_ffr, unsigned long vq_minus_1);
112 extern unsigned int sve_get_vl(void);
113 extern void sve_set_vq(unsigned long vq_minus_1);
114 extern void sme_set_vq(unsigned long vq_minus_1);
115 extern void za_save_state(void *state);
116 extern void za_load_state(void const *state);
118 struct arm64_cpu_capabilities;
119 extern void sve_kernel_enable(const struct arm64_cpu_capabilities *__unused);
120 extern void sme_kernel_enable(const struct arm64_cpu_capabilities *__unused);
121 extern void fa64_kernel_enable(const struct arm64_cpu_capabilities *__unused);
123 extern u64 read_zcr_features(void);
124 extern u64 read_smcr_features(void);
127 * Helpers to translate bit indices in sve_vq_map to VQ values (and
128 * vice versa). This allows find_next_bit() to be used to find the
129 * _maximum_ VQ not exceeding a certain value.
131 static inline unsigned int __vq_to_bit(unsigned int vq)
133 return SVE_VQ_MAX - vq;
136 static inline unsigned int __bit_to_vq(unsigned int bit)
138 return SVE_VQ_MAX - bit;
144 const char *name; /* For display purposes */
146 /* Minimum supported vector length across all CPUs */
149 /* Maximum supported vector length across all CPUs */
151 int max_virtualisable_vl;
154 * Set of available vector lengths,
155 * where length vq encoded as bit __vq_to_bit(vq):
157 DECLARE_BITMAP(vq_map, SVE_VQ_MAX);
159 /* Set of vector lengths present on at least one cpu: */
160 DECLARE_BITMAP(vq_partial_map, SVE_VQ_MAX);
163 #ifdef CONFIG_ARM64_SVE
165 extern void sve_alloc(struct task_struct *task, bool flush);
166 extern void fpsimd_release_task(struct task_struct *task);
167 extern void fpsimd_sync_to_sve(struct task_struct *task);
168 extern void fpsimd_force_sync_to_sve(struct task_struct *task);
169 extern void sve_sync_to_fpsimd(struct task_struct *task);
170 extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
172 extern int vec_set_vector_length(struct task_struct *task, enum vec_type type,
173 unsigned long vl, unsigned long flags);
175 extern int sve_set_current_vl(unsigned long arg);
176 extern int sve_get_current_vl(void);
178 static inline void sve_user_disable(void)
180 sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
183 static inline void sve_user_enable(void)
185 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
188 #define sve_cond_update_zcr_vq(val, reg) \
190 u64 __zcr = read_sysreg_s((reg)); \
191 u64 __new = __zcr & ~ZCR_ELx_LEN_MASK; \
192 __new |= (val) & ZCR_ELx_LEN_MASK; \
193 if (__zcr != __new) \
194 write_sysreg_s(__new, (reg)); \
198 * Probing and setup functions.
199 * Calls to these functions must be serialised with one another.
203 extern void __init vec_init_vq_map(enum vec_type type);
204 extern void vec_update_vq_map(enum vec_type type);
205 extern int vec_verify_vq_map(enum vec_type type);
206 extern void __init sve_setup(void);
208 extern __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX];
210 static inline void write_vl(enum vec_type type, u64 val)
215 #ifdef CONFIG_ARM64_SVE
217 tmp = read_sysreg_s(SYS_ZCR_EL1) & ~ZCR_ELx_LEN_MASK;
218 write_sysreg_s(tmp | val, SYS_ZCR_EL1);
221 #ifdef CONFIG_ARM64_SME
223 tmp = read_sysreg_s(SYS_SMCR_EL1) & ~SMCR_ELx_LEN_MASK;
224 write_sysreg_s(tmp | val, SYS_SMCR_EL1);
233 static inline int vec_max_vl(enum vec_type type)
235 return vl_info[type].max_vl;
238 static inline int vec_max_virtualisable_vl(enum vec_type type)
240 return vl_info[type].max_virtualisable_vl;
243 static inline int sve_max_vl(void)
245 return vec_max_vl(ARM64_VEC_SVE);
248 static inline int sve_max_virtualisable_vl(void)
250 return vec_max_virtualisable_vl(ARM64_VEC_SVE);
253 /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
254 static inline bool vq_available(enum vec_type type, unsigned int vq)
256 return test_bit(__vq_to_bit(vq), vl_info[type].vq_map);
259 static inline bool sve_vq_available(unsigned int vq)
261 return vq_available(ARM64_VEC_SVE, vq);
264 size_t sve_state_size(struct task_struct const *task);
266 #else /* ! CONFIG_ARM64_SVE */
268 static inline void sve_alloc(struct task_struct *task, bool flush) { }
269 static inline void fpsimd_release_task(struct task_struct *task) { }
270 static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
271 static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
273 static inline int sve_max_virtualisable_vl(void)
278 static inline int sve_set_current_vl(unsigned long arg)
283 static inline int sve_get_current_vl(void)
288 static inline int sve_max_vl(void)
293 static inline bool sve_vq_available(unsigned int vq) { return false; }
295 static inline void sve_user_disable(void) { BUILD_BUG(); }
296 static inline void sve_user_enable(void) { BUILD_BUG(); }
298 #define sve_cond_update_zcr_vq(val, reg) do { } while (0)
300 static inline void vec_init_vq_map(enum vec_type t) { }
301 static inline void vec_update_vq_map(enum vec_type t) { }
302 static inline int vec_verify_vq_map(enum vec_type t) { return 0; }
303 static inline void sve_setup(void) { }
305 static inline size_t sve_state_size(struct task_struct const *task)
310 #endif /* ! CONFIG_ARM64_SVE */
312 #ifdef CONFIG_ARM64_SME
314 static inline void sme_user_disable(void)
316 sysreg_clear_set(cpacr_el1, CPACR_EL1_SMEN_EL0EN, 0);
319 static inline void sme_user_enable(void)
321 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_SMEN_EL0EN);
324 static inline void sme_smstart_sm(void)
326 asm volatile(__msr_s(SYS_SVCR_SMSTART_SM_EL0, "xzr"));
329 static inline void sme_smstop_sm(void)
331 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SM_EL0, "xzr"));
334 static inline void sme_smstop(void)
336 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SMZA_EL0, "xzr"));
339 extern void __init sme_setup(void);
341 static inline int sme_max_vl(void)
343 return vec_max_vl(ARM64_VEC_SME);
346 static inline int sme_max_virtualisable_vl(void)
348 return vec_max_virtualisable_vl(ARM64_VEC_SME);
351 extern void sme_alloc(struct task_struct *task);
352 extern unsigned int sme_get_vl(void);
353 extern int sme_set_current_vl(unsigned long arg);
354 extern int sme_get_current_vl(void);
357 * Return how many bytes of memory are required to store the full SME
358 * specific state (currently just ZA) for task, given task's currently
359 * configured vector length.
361 static inline size_t za_state_size(struct task_struct const *task)
363 unsigned int vl = task_get_sme_vl(task);
365 return ZA_SIG_REGS_SIZE(sve_vq_from_vl(vl));
370 static inline void sme_user_disable(void) { BUILD_BUG(); }
371 static inline void sme_user_enable(void) { BUILD_BUG(); }
373 static inline void sme_smstart_sm(void) { }
374 static inline void sme_smstop_sm(void) { }
375 static inline void sme_smstop(void) { }
377 static inline void sme_alloc(struct task_struct *task) { }
378 static inline void sme_setup(void) { }
379 static inline unsigned int sme_get_vl(void) { return 0; }
380 static inline int sme_max_vl(void) { return 0; }
381 static inline int sme_max_virtualisable_vl(void) { return 0; }
382 static inline int sme_set_current_vl(unsigned long arg) { return -EINVAL; }
383 static inline int sme_get_current_vl(void) { return -EINVAL; }
385 static inline size_t za_state_size(struct task_struct const *task)
390 #endif /* ! CONFIG_ARM64_SME */
392 /* For use by EFI runtime services calls only */
393 extern void __efi_fpsimd_begin(void);
394 extern void __efi_fpsimd_end(void);