arm64/sve: In-kernel vector length availability query interface
[linux-2.6-microblaze.git] / arch / arm64 / include / asm / fpsimd.h
1 /*
2  * Copyright (C) 2012 ARM Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #ifndef __ASM_FP_H
17 #define __ASM_FP_H
18
19 #include <asm/errno.h>
20 #include <asm/ptrace.h>
21 #include <asm/processor.h>
22 #include <asm/sigcontext.h>
23 #include <asm/sysreg.h>
24
25 #ifndef __ASSEMBLY__
26
27 #include <linux/bitmap.h>
28 #include <linux/build_bug.h>
29 #include <linux/bug.h>
30 #include <linux/cache.h>
31 #include <linux/init.h>
32 #include <linux/stddef.h>
33 #include <linux/types.h>
34
35 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
36 /* Masks for extracting the FPSR and FPCR from the FPSCR */
37 #define VFP_FPSCR_STAT_MASK     0xf800009f
38 #define VFP_FPSCR_CTRL_MASK     0x07f79f00
39 /*
40  * The VFP state has 32x64-bit registers and a single 32-bit
41  * control/status register.
42  */
43 #define VFP_STATE_SIZE          ((32 * 8) + 4)
44 #endif
45
46 struct task_struct;
47
48 extern void fpsimd_save_state(struct user_fpsimd_state *state);
49 extern void fpsimd_load_state(struct user_fpsimd_state *state);
50
51 extern void fpsimd_save(void);
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
61 extern void fpsimd_bind_task_to_cpu(void);
62 extern void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *state,
63                                      void *sve_state, unsigned int sve_vl);
64
65 extern void fpsimd_flush_task_state(struct task_struct *target);
66 extern void fpsimd_flush_cpu_state(void);
67 extern void sve_flush_cpu_state(void);
68
69 /* Maximum VL that SVE VL-agnostic software can transparently support */
70 #define SVE_VL_ARCH_MAX 0x100
71
72 /* Offset of FFR in the SVE register dump */
73 static inline size_t sve_ffr_offset(int vl)
74 {
75         return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
76 }
77
78 static inline void *sve_pffr(struct thread_struct *thread)
79 {
80         return (char *)thread->sve_state + sve_ffr_offset(thread->sve_vl);
81 }
82
83 extern void sve_save_state(void *state, u32 *pfpsr);
84 extern void sve_load_state(void const *state, u32 const *pfpsr,
85                            unsigned long vq_minus_1);
86 extern unsigned int sve_get_vl(void);
87
88 struct arm64_cpu_capabilities;
89 extern void sve_kernel_enable(const struct arm64_cpu_capabilities *__unused);
90
91 extern u64 read_zcr_features(void);
92
93 extern int __ro_after_init sve_max_vl;
94 extern int __ro_after_init sve_max_virtualisable_vl;
95 /* Set of available vector lengths, as vq_to_bit(vq): */
96 extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
97
98 /*
99  * Helpers to translate bit indices in sve_vq_map to VQ values (and
100  * vice versa).  This allows find_next_bit() to be used to find the
101  * _maximum_ VQ not exceeding a certain value.
102  */
103 static inline unsigned int __vq_to_bit(unsigned int vq)
104 {
105         return SVE_VQ_MAX - vq;
106 }
107
108 static inline unsigned int __bit_to_vq(unsigned int bit)
109 {
110         if (WARN_ON(bit >= SVE_VQ_MAX))
111                 bit = SVE_VQ_MAX - 1;
112
113         return SVE_VQ_MAX - bit;
114 }
115
116 /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
117 static inline bool sve_vq_available(unsigned int vq)
118 {
119         return test_bit(__vq_to_bit(vq), sve_vq_map);
120 }
121
122 #ifdef CONFIG_ARM64_SVE
123
124 extern size_t sve_state_size(struct task_struct const *task);
125
126 extern void sve_alloc(struct task_struct *task);
127 extern void fpsimd_release_task(struct task_struct *task);
128 extern void fpsimd_sync_to_sve(struct task_struct *task);
129 extern void sve_sync_to_fpsimd(struct task_struct *task);
130 extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
131
132 extern int sve_set_vector_length(struct task_struct *task,
133                                  unsigned long vl, unsigned long flags);
134
135 extern int sve_set_current_vl(unsigned long arg);
136 extern int sve_get_current_vl(void);
137
138 static inline void sve_user_disable(void)
139 {
140         sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
141 }
142
143 static inline void sve_user_enable(void)
144 {
145         sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
146 }
147
148 /*
149  * Probing and setup functions.
150  * Calls to these functions must be serialised with one another.
151  */
152 extern void __init sve_init_vq_map(void);
153 extern void sve_update_vq_map(void);
154 extern int sve_verify_vq_map(void);
155 extern void __init sve_setup(void);
156
157 #else /* ! CONFIG_ARM64_SVE */
158
159 static inline void sve_alloc(struct task_struct *task) { }
160 static inline void fpsimd_release_task(struct task_struct *task) { }
161 static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
162 static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
163
164 static inline int sve_set_current_vl(unsigned long arg)
165 {
166         return -EINVAL;
167 }
168
169 static inline int sve_get_current_vl(void)
170 {
171         return -EINVAL;
172 }
173
174 static inline void sve_user_disable(void) { BUILD_BUG(); }
175 static inline void sve_user_enable(void) { BUILD_BUG(); }
176
177 static inline void sve_init_vq_map(void) { }
178 static inline void sve_update_vq_map(void) { }
179 static inline int sve_verify_vq_map(void) { return 0; }
180 static inline void sve_setup(void) { }
181
182 #endif /* ! CONFIG_ARM64_SVE */
183
184 /* For use by EFI runtime services calls only */
185 extern void __efi_fpsimd_begin(void);
186 extern void __efi_fpsimd_end(void);
187
188 #endif
189
190 #endif