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)
35 static inline unsigned long cpacr_save_enable_kernel_sve(void)
37 unsigned long old = read_sysreg(cpacr_el1);
38 unsigned long set = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_ZEN_EL1EN;
40 write_sysreg(old | set, cpacr_el1);
45 static inline unsigned long cpacr_save_enable_kernel_sme(void)
47 unsigned long old = read_sysreg(cpacr_el1);
48 unsigned long set = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_SMEN_EL1EN;
50 write_sysreg(old | set, cpacr_el1);
55 static inline void cpacr_restore(unsigned long cpacr)
57 write_sysreg(cpacr, cpacr_el1);
62 * When we defined the maximum SVE vector length we defined the ABI so
63 * that the maximum vector length included all the reserved for future
64 * expansion bits in ZCR rather than those just currently defined by
65 * the architecture. While SME follows a similar pattern the fact that
66 * it includes a square matrix means that any allocations that attempt
67 * to cover the maximum potential vector length (such as happen with
68 * the regset used for ptrace) end up being extremely large. Define
69 * the much lower actual limit for use in such situations.
75 extern void fpsimd_save_state(struct user_fpsimd_state *state);
76 extern void fpsimd_load_state(struct user_fpsimd_state *state);
78 extern void fpsimd_thread_switch(struct task_struct *next);
79 extern void fpsimd_flush_thread(void);
81 extern void fpsimd_signal_preserve_current_state(void);
82 extern void fpsimd_preserve_current_state(void);
83 extern void fpsimd_restore_current_state(void);
84 extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
85 extern void fpsimd_kvm_prepare(void);
88 struct user_fpsimd_state *st;
94 enum fp_type *fp_type;
98 extern void fpsimd_bind_state_to_cpu(struct cpu_fp_state *fp_state);
100 extern void fpsimd_flush_task_state(struct task_struct *target);
101 extern void fpsimd_save_and_flush_cpu_state(void);
103 static inline bool thread_sm_enabled(struct thread_struct *thread)
105 return system_supports_sme() && (thread->svcr & SVCR_SM_MASK);
108 static inline bool thread_za_enabled(struct thread_struct *thread)
110 return system_supports_sme() && (thread->svcr & SVCR_ZA_MASK);
113 /* Maximum VL that SVE/SME VL-agnostic software can transparently support */
114 #define VL_ARCH_MAX 0x100
116 /* Offset of FFR in the SVE register dump */
117 static inline size_t sve_ffr_offset(int vl)
119 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
122 static inline void *sve_pffr(struct thread_struct *thread)
126 if (system_supports_sme() && thread_sm_enabled(thread))
127 vl = thread_get_sme_vl(thread);
129 vl = thread_get_sve_vl(thread);
131 return (char *)thread->sve_state + sve_ffr_offset(vl);
134 static inline void *thread_zt_state(struct thread_struct *thread)
136 /* The ZT register state is stored immediately after the ZA state */
137 unsigned int sme_vq = sve_vq_from_vl(thread_get_sme_vl(thread));
138 return thread->sme_state + ZA_SIG_REGS_SIZE(sme_vq);
141 extern void sve_save_state(void *state, u32 *pfpsr, int save_ffr);
142 extern void sve_load_state(void const *state, u32 const *pfpsr,
144 extern void sve_flush_live(bool flush_ffr, unsigned long vq_minus_1);
145 extern unsigned int sve_get_vl(void);
146 extern void sve_set_vq(unsigned long vq_minus_1);
147 extern void sme_set_vq(unsigned long vq_minus_1);
148 extern void sme_save_state(void *state, int zt);
149 extern void sme_load_state(void const *state, int zt);
151 struct arm64_cpu_capabilities;
152 extern void cpu_enable_fpsimd(const struct arm64_cpu_capabilities *__unused);
153 extern void cpu_enable_sve(const struct arm64_cpu_capabilities *__unused);
154 extern void cpu_enable_sme(const struct arm64_cpu_capabilities *__unused);
155 extern void cpu_enable_sme2(const struct arm64_cpu_capabilities *__unused);
156 extern void cpu_enable_fa64(const struct arm64_cpu_capabilities *__unused);
158 extern u64 read_smcr_features(void);
161 * Helpers to translate bit indices in sve_vq_map to VQ values (and
162 * vice versa). This allows find_next_bit() to be used to find the
163 * _maximum_ VQ not exceeding a certain value.
165 static inline unsigned int __vq_to_bit(unsigned int vq)
167 return SVE_VQ_MAX - vq;
170 static inline unsigned int __bit_to_vq(unsigned int bit)
172 return SVE_VQ_MAX - bit;
178 const char *name; /* For display purposes */
180 /* Minimum supported vector length across all CPUs */
183 /* Maximum supported vector length across all CPUs */
185 int max_virtualisable_vl;
188 * Set of available vector lengths,
189 * where length vq encoded as bit __vq_to_bit(vq):
191 DECLARE_BITMAP(vq_map, SVE_VQ_MAX);
193 /* Set of vector lengths present on at least one cpu: */
194 DECLARE_BITMAP(vq_partial_map, SVE_VQ_MAX);
197 #ifdef CONFIG_ARM64_SVE
199 extern void sve_alloc(struct task_struct *task, bool flush);
200 extern void fpsimd_release_task(struct task_struct *task);
201 extern void fpsimd_sync_to_sve(struct task_struct *task);
202 extern void fpsimd_force_sync_to_sve(struct task_struct *task);
203 extern void sve_sync_to_fpsimd(struct task_struct *task);
204 extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
206 extern int vec_set_vector_length(struct task_struct *task, enum vec_type type,
207 unsigned long vl, unsigned long flags);
209 extern int sve_set_current_vl(unsigned long arg);
210 extern int sve_get_current_vl(void);
212 static inline void sve_user_disable(void)
214 sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
217 static inline void sve_user_enable(void)
219 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
222 #define sve_cond_update_zcr_vq(val, reg) \
224 u64 __zcr = read_sysreg_s((reg)); \
225 u64 __new = __zcr & ~ZCR_ELx_LEN_MASK; \
226 __new |= (val) & ZCR_ELx_LEN_MASK; \
227 if (__zcr != __new) \
228 write_sysreg_s(__new, (reg)); \
232 * Probing and setup functions.
233 * Calls to these functions must be serialised with one another.
237 extern void __init vec_init_vq_map(enum vec_type type);
238 extern void vec_update_vq_map(enum vec_type type);
239 extern int vec_verify_vq_map(enum vec_type type);
240 extern void __init sve_setup(void);
242 extern __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX];
244 static inline void write_vl(enum vec_type type, u64 val)
249 #ifdef CONFIG_ARM64_SVE
251 tmp = read_sysreg_s(SYS_ZCR_EL1) & ~ZCR_ELx_LEN_MASK;
252 write_sysreg_s(tmp | val, SYS_ZCR_EL1);
255 #ifdef CONFIG_ARM64_SME
257 tmp = read_sysreg_s(SYS_SMCR_EL1) & ~SMCR_ELx_LEN_MASK;
258 write_sysreg_s(tmp | val, SYS_SMCR_EL1);
267 static inline int vec_max_vl(enum vec_type type)
269 return vl_info[type].max_vl;
272 static inline int vec_max_virtualisable_vl(enum vec_type type)
274 return vl_info[type].max_virtualisable_vl;
277 static inline int sve_max_vl(void)
279 return vec_max_vl(ARM64_VEC_SVE);
282 static inline int sve_max_virtualisable_vl(void)
284 return vec_max_virtualisable_vl(ARM64_VEC_SVE);
287 /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
288 static inline bool vq_available(enum vec_type type, unsigned int vq)
290 return test_bit(__vq_to_bit(vq), vl_info[type].vq_map);
293 static inline bool sve_vq_available(unsigned int vq)
295 return vq_available(ARM64_VEC_SVE, vq);
298 size_t sve_state_size(struct task_struct const *task);
300 #else /* ! CONFIG_ARM64_SVE */
302 static inline void sve_alloc(struct task_struct *task, bool flush) { }
303 static inline void fpsimd_release_task(struct task_struct *task) { }
304 static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
305 static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
307 static inline int sve_max_virtualisable_vl(void)
312 static inline int sve_set_current_vl(unsigned long arg)
317 static inline int sve_get_current_vl(void)
322 static inline int sve_max_vl(void)
327 static inline bool sve_vq_available(unsigned int vq) { return false; }
329 static inline void sve_user_disable(void) { BUILD_BUG(); }
330 static inline void sve_user_enable(void) { BUILD_BUG(); }
332 #define sve_cond_update_zcr_vq(val, reg) do { } while (0)
334 static inline void vec_init_vq_map(enum vec_type t) { }
335 static inline void vec_update_vq_map(enum vec_type t) { }
336 static inline int vec_verify_vq_map(enum vec_type t) { return 0; }
337 static inline void sve_setup(void) { }
339 static inline size_t sve_state_size(struct task_struct const *task)
344 #endif /* ! CONFIG_ARM64_SVE */
346 #ifdef CONFIG_ARM64_SME
348 static inline void sme_user_disable(void)
350 sysreg_clear_set(cpacr_el1, CPACR_EL1_SMEN_EL0EN, 0);
353 static inline void sme_user_enable(void)
355 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_SMEN_EL0EN);
358 static inline void sme_smstart_sm(void)
360 asm volatile(__msr_s(SYS_SVCR_SMSTART_SM_EL0, "xzr"));
363 static inline void sme_smstop_sm(void)
365 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SM_EL0, "xzr"));
368 static inline void sme_smstop(void)
370 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SMZA_EL0, "xzr"));
373 extern void __init sme_setup(void);
375 static inline int sme_max_vl(void)
377 return vec_max_vl(ARM64_VEC_SME);
380 static inline int sme_max_virtualisable_vl(void)
382 return vec_max_virtualisable_vl(ARM64_VEC_SME);
385 extern void sme_alloc(struct task_struct *task, bool flush);
386 extern unsigned int sme_get_vl(void);
387 extern int sme_set_current_vl(unsigned long arg);
388 extern int sme_get_current_vl(void);
391 * Return how many bytes of memory are required to store the full SME
392 * specific state for task, given task's currently configured vector
395 static inline size_t sme_state_size(struct task_struct const *task)
397 unsigned int vl = task_get_sme_vl(task);
400 size = ZA_SIG_REGS_SIZE(sve_vq_from_vl(vl));
402 if (system_supports_sme2())
403 size += ZT_SIG_REG_SIZE;
410 static inline void sme_user_disable(void) { BUILD_BUG(); }
411 static inline void sme_user_enable(void) { BUILD_BUG(); }
413 static inline void sme_smstart_sm(void) { }
414 static inline void sme_smstop_sm(void) { }
415 static inline void sme_smstop(void) { }
417 static inline void sme_alloc(struct task_struct *task, bool flush) { }
418 static inline void sme_setup(void) { }
419 static inline unsigned int sme_get_vl(void) { return 0; }
420 static inline int sme_max_vl(void) { return 0; }
421 static inline int sme_max_virtualisable_vl(void) { return 0; }
422 static inline int sme_set_current_vl(unsigned long arg) { return -EINVAL; }
423 static inline int sme_get_current_vl(void) { return -EINVAL; }
425 static inline size_t sme_state_size(struct task_struct const *task)
430 #endif /* ! CONFIG_ARM64_SME */
432 /* For use by EFI runtime services calls only */
433 extern void __efi_fpsimd_begin(void);
434 extern void __efi_fpsimd_end(void);