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)
37 extern void fpsimd_save_state(struct user_fpsimd_state *state);
38 extern void fpsimd_load_state(struct user_fpsimd_state *state);
40 extern void fpsimd_thread_switch(struct task_struct *next);
41 extern void fpsimd_flush_thread(void);
43 extern void fpsimd_signal_preserve_current_state(void);
44 extern void fpsimd_preserve_current_state(void);
45 extern void fpsimd_restore_current_state(void);
46 extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
48 extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state,
49 void *sve_state, unsigned int sve_vl);
51 extern void fpsimd_flush_task_state(struct task_struct *target);
52 extern void fpsimd_save_and_flush_cpu_state(void);
54 /* Maximum VL that SVE/SME VL-agnostic software can transparently support */
55 #define VL_ARCH_MAX 0x100
57 /* Offset of FFR in the SVE register dump */
58 static inline size_t sve_ffr_offset(int vl)
60 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
63 static inline void *sve_pffr(struct thread_struct *thread)
65 return (char *)thread->sve_state + sve_ffr_offset(thread_get_sve_vl(thread));
68 extern void sve_save_state(void *state, u32 *pfpsr, int save_ffr);
69 extern void sve_load_state(void const *state, u32 const *pfpsr,
71 extern void sve_flush_live(bool flush_ffr, unsigned long vq_minus_1);
72 extern unsigned int sve_get_vl(void);
73 extern void sve_set_vq(unsigned long vq_minus_1);
75 struct arm64_cpu_capabilities;
76 extern void sve_kernel_enable(const struct arm64_cpu_capabilities *__unused);
78 extern u64 read_zcr_features(void);
81 * Helpers to translate bit indices in sve_vq_map to VQ values (and
82 * vice versa). This allows find_next_bit() to be used to find the
83 * _maximum_ VQ not exceeding a certain value.
85 static inline unsigned int __vq_to_bit(unsigned int vq)
87 return SVE_VQ_MAX - vq;
90 static inline unsigned int __bit_to_vq(unsigned int bit)
92 return SVE_VQ_MAX - bit;
98 const char *name; /* For display purposes */
100 /* Minimum supported vector length across all CPUs */
103 /* Maximum supported vector length across all CPUs */
105 int max_virtualisable_vl;
108 * Set of available vector lengths,
109 * where length vq encoded as bit __vq_to_bit(vq):
111 DECLARE_BITMAP(vq_map, SVE_VQ_MAX);
113 /* Set of vector lengths present on at least one cpu: */
114 DECLARE_BITMAP(vq_partial_map, SVE_VQ_MAX);
117 #ifdef CONFIG_ARM64_SVE
119 extern void sve_alloc(struct task_struct *task);
120 extern void fpsimd_release_task(struct task_struct *task);
121 extern void fpsimd_sync_to_sve(struct task_struct *task);
122 extern void sve_sync_to_fpsimd(struct task_struct *task);
123 extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
125 extern int vec_set_vector_length(struct task_struct *task, enum vec_type type,
126 unsigned long vl, unsigned long flags);
128 extern int sve_set_current_vl(unsigned long arg);
129 extern int sve_get_current_vl(void);
131 static inline void sve_user_disable(void)
133 sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
136 static inline void sve_user_enable(void)
138 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
141 #define sve_cond_update_zcr_vq(val, reg) \
143 u64 __zcr = read_sysreg_s((reg)); \
144 u64 __new = __zcr & ~ZCR_ELx_LEN_MASK; \
145 __new |= (val) & ZCR_ELx_LEN_MASK; \
146 if (__zcr != __new) \
147 write_sysreg_s(__new, (reg)); \
151 * Probing and setup functions.
152 * Calls to these functions must be serialised with one another.
156 extern void __init vec_init_vq_map(enum vec_type type);
157 extern void vec_update_vq_map(enum vec_type type);
158 extern int vec_verify_vq_map(enum vec_type type);
159 extern void __init sve_setup(void);
161 extern __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX];
163 static inline void write_vl(enum vec_type type, u64 val)
168 #ifdef CONFIG_ARM64_SVE
170 tmp = read_sysreg_s(SYS_ZCR_EL1) & ~ZCR_ELx_LEN_MASK;
171 write_sysreg_s(tmp | val, SYS_ZCR_EL1);
180 static inline int vec_max_vl(enum vec_type type)
182 return vl_info[type].max_vl;
185 static inline int vec_max_virtualisable_vl(enum vec_type type)
187 return vl_info[type].max_virtualisable_vl;
190 static inline int sve_max_vl(void)
192 return vec_max_vl(ARM64_VEC_SVE);
195 static inline int sve_max_virtualisable_vl(void)
197 return vec_max_virtualisable_vl(ARM64_VEC_SVE);
200 /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
201 static inline bool vq_available(enum vec_type type, unsigned int vq)
203 return test_bit(__vq_to_bit(vq), vl_info[type].vq_map);
206 static inline bool sve_vq_available(unsigned int vq)
208 return vq_available(ARM64_VEC_SVE, vq);
211 #else /* ! CONFIG_ARM64_SVE */
213 static inline void sve_alloc(struct task_struct *task) { }
214 static inline void fpsimd_release_task(struct task_struct *task) { }
215 static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
216 static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
218 static inline int sve_max_virtualisable_vl(void)
223 static inline int sve_set_current_vl(unsigned long arg)
228 static inline int sve_get_current_vl(void)
233 static inline int sve_max_vl(void)
238 static inline bool sve_vq_available(unsigned int vq) { return false; }
240 static inline void sve_user_disable(void) { BUILD_BUG(); }
241 static inline void sve_user_enable(void) { BUILD_BUG(); }
243 #define sve_cond_update_zcr_vq(val, reg) do { } while (0)
245 static inline void vec_init_vq_map(enum vec_type t) { }
246 static inline void vec_update_vq_map(enum vec_type t) { }
247 static inline int vec_verify_vq_map(enum vec_type t) { return 0; }
248 static inline void sve_setup(void) { }
250 #endif /* ! CONFIG_ARM64_SVE */
252 /* For use by EFI runtime services calls only */
253 extern void __efi_fpsimd_begin(void);
254 extern void __efi_fpsimd_end(void);