Merge branches 'acpi-scan', 'acpi-tad', 'acpi-extlog' and 'acpi-misc'
[linux-block.git] / arch / arm64 / include / asm / fpsimd.h
CommitLineData
caab277b 1/* SPDX-License-Identifier: GPL-2.0-only */
53631b54
CM
2/*
3 * Copyright (C) 2012 ARM Ltd.
53631b54
CM
4 */
5#ifndef __ASM_FP_H
6#define __ASM_FP_H
7
2d2123bc 8#include <asm/errno.h>
f9209e26 9#include <asm/ptrace.h>
9a6e5948
DM
10#include <asm/processor.h>
11#include <asm/sigcontext.h>
f9209e26 12#include <asm/sysreg.h>
53631b54
CM
13
14#ifndef __ASSEMBLY__
15
ead9e430 16#include <linux/bitmap.h>
f9209e26 17#include <linux/build_bug.h>
ead9e430 18#include <linux/bug.h>
7582e220 19#include <linux/cache.h>
b4f9b390 20#include <linux/init.h>
bc0ee476 21#include <linux/stddef.h>
ead9e430 22#include <linux/types.h>
bc0ee476 23
b907b80d 24#ifdef CONFIG_COMPAT
53631b54
CM
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
28/*
29 * The VFP state has 32x64-bit registers and a single 32-bit
30 * control/status register.
31 */
32#define VFP_STATE_SIZE ((32 * 8) + 4)
33#endif
34
d158a060
MB
35/*
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.
44 */
45#define SME_VQ_MAX 16
46
53631b54
CM
47struct task_struct;
48
20b85472
DM
49extern void fpsimd_save_state(struct user_fpsimd_state *state);
50extern void fpsimd_load_state(struct user_fpsimd_state *state);
53631b54
CM
51
52extern void fpsimd_thread_switch(struct task_struct *next);
53extern void fpsimd_flush_thread(void);
54
8cd969d2 55extern void fpsimd_signal_preserve_current_state(void);
c51f9269 56extern void fpsimd_preserve_current_state(void);
005f78cd 57extern void fpsimd_restore_current_state(void);
0abdeff5 58extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
93ae6b01 59extern void fpsimd_kvm_prepare(void);
c51f9269 60
1192b93b
MB
61struct cpu_fp_state {
62 struct user_fpsimd_state *st;
63 void *sve_state;
ce514000 64 void *sme_state;
1192b93b
MB
65 u64 *svcr;
66 unsigned int sve_vl;
67 unsigned int sme_vl;
68 enum fp_type *fp_type;
69 enum fp_type to_save;
70};
71
72extern void fpsimd_bind_state_to_cpu(struct cpu_fp_state *fp_state);
e6b673b7 73
005f78cd 74extern void fpsimd_flush_task_state(struct task_struct *target);
54b8c7cb 75extern void fpsimd_save_and_flush_cpu_state(void);
005f78cd 76
af7167d6
MB
77static inline bool thread_sm_enabled(struct thread_struct *thread)
78{
ec0067a6 79 return system_supports_sme() && (thread->svcr & SVCR_SM_MASK);
af7167d6
MB
80}
81
82static inline bool thread_za_enabled(struct thread_struct *thread)
83{
ec0067a6 84 return system_supports_sme() && (thread->svcr & SVCR_ZA_MASK);
af7167d6
MB
85}
86
30c43e73
MB
87/* Maximum VL that SVE/SME VL-agnostic software can transparently support */
88#define VL_ARCH_MAX 0x100
7582e220 89
9a6e5948
DM
90/* Offset of FFR in the SVE register dump */
91static inline size_t sve_ffr_offset(int vl)
92{
93 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
94}
95
96static inline void *sve_pffr(struct thread_struct *thread)
97{
af7167d6
MB
98 unsigned int vl;
99
100 if (system_supports_sme() && thread_sm_enabled(thread))
101 vl = thread_get_sme_vl(thread);
102 else
103 vl = thread_get_sve_vl(thread);
104
105 return (char *)thread->sve_state + sve_ffr_offset(vl);
9a6e5948
DM
106}
107
d6138b4a
MB
108static inline void *thread_zt_state(struct thread_struct *thread)
109{
110 /* The ZT register state is stored immediately after the ZA state */
111 unsigned int sme_vq = sve_vq_from_vl(thread_get_sme_vl(thread));
112 return thread->sme_state + ZA_SIG_REGS_SIZE(sme_vq);
113}
114
9f584866 115extern void sve_save_state(void *state, u32 *pfpsr, int save_ffr);
1fc5dce7 116extern void sve_load_state(void const *state, u32 const *pfpsr,
ddc806b5 117 int restore_ffr);
9f584866 118extern void sve_flush_live(bool flush_ffr, unsigned long vq_minus_1);
1fc5dce7 119extern unsigned int sve_get_vl(void);
cccb78ce 120extern void sve_set_vq(unsigned long vq_minus_1);
af7167d6 121extern void sme_set_vq(unsigned long vq_minus_1);
95fcec71
MB
122extern void sme_save_state(void *state, int zt);
123extern void sme_load_state(void const *state, int zt);
c0cda3b8
DM
124
125struct arm64_cpu_capabilities;
126extern void sve_kernel_enable(const struct arm64_cpu_capabilities *__unused);
5e64b862 127extern void sme_kernel_enable(const struct arm64_cpu_capabilities *__unused);
d4913eee 128extern void sme2_kernel_enable(const struct arm64_cpu_capabilities *__unused);
5e64b862 129extern void fa64_kernel_enable(const struct arm64_cpu_capabilities *__unused);
1fc5dce7 130
31dc52b3 131extern u64 read_zcr_features(void);
b42990d3 132extern u64 read_smcr_features(void);
31dc52b3 133
ead9e430
DM
134/*
135 * Helpers to translate bit indices in sve_vq_map to VQ values (and
136 * vice versa). This allows find_next_bit() to be used to find the
137 * _maximum_ VQ not exceeding a certain value.
138 */
139static inline unsigned int __vq_to_bit(unsigned int vq)
140{
141 return SVE_VQ_MAX - vq;
142}
143
144static inline unsigned int __bit_to_vq(unsigned int bit)
145{
ead9e430
DM
146 return SVE_VQ_MAX - bit;
147}
148
b5bc00ff
MB
149
150struct vl_info {
151 enum vec_type type;
152 const char *name; /* For display purposes */
153
154 /* Minimum supported vector length across all CPUs */
155 int min_vl;
156
157 /* Maximum supported vector length across all CPUs */
158 int max_vl;
159 int max_virtualisable_vl;
160
161 /*
162 * Set of available vector lengths,
163 * where length vq encoded as bit __vq_to_bit(vq):
164 */
165 DECLARE_BITMAP(vq_map, SVE_VQ_MAX);
166
167 /* Set of vector lengths present on at least one cpu: */
168 DECLARE_BITMAP(vq_partial_map, SVE_VQ_MAX);
169};
7582e220 170
bc0ee476
DM
171#ifdef CONFIG_ARM64_SVE
172
826a4fdd 173extern void sve_alloc(struct task_struct *task, bool flush);
bc0ee476 174extern void fpsimd_release_task(struct task_struct *task);
43d4da2c 175extern void fpsimd_sync_to_sve(struct task_struct *task);
e12310a0 176extern void fpsimd_force_sync_to_sve(struct task_struct *task);
43d4da2c
DM
177extern void sve_sync_to_fpsimd(struct task_struct *task);
178extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
179
30c43e73 180extern int vec_set_vector_length(struct task_struct *task, enum vec_type type,
7582e220 181 unsigned long vl, unsigned long flags);
bc0ee476 182
2d2123bc
DM
183extern int sve_set_current_vl(unsigned long arg);
184extern int sve_get_current_vl(void);
185
f9209e26
MR
186static inline void sve_user_disable(void)
187{
188 sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
189}
190
191static inline void sve_user_enable(void)
192{
193 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
194}
195
71ce1ae5
MZ
196#define sve_cond_update_zcr_vq(val, reg) \
197 do { \
198 u64 __zcr = read_sysreg_s((reg)); \
199 u64 __new = __zcr & ~ZCR_ELx_LEN_MASK; \
200 __new |= (val) & ZCR_ELx_LEN_MASK; \
201 if (__zcr != __new) \
202 write_sysreg_s(__new, (reg)); \
203 } while (0)
204
2e0f2478
DM
205/*
206 * Probing and setup functions.
207 * Calls to these functions must be serialised with one another.
208 */
b5bc00ff
MB
209enum vec_type;
210
211extern void __init vec_init_vq_map(enum vec_type type);
212extern void vec_update_vq_map(enum vec_type type);
213extern int vec_verify_vq_map(enum vec_type type);
2e0f2478
DM
214extern void __init sve_setup(void);
215
b5bc00ff
MB
216extern __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX];
217
218static inline void write_vl(enum vec_type type, u64 val)
219{
220 u64 tmp;
221
222 switch (type) {
223#ifdef CONFIG_ARM64_SVE
224 case ARM64_VEC_SVE:
225 tmp = read_sysreg_s(SYS_ZCR_EL1) & ~ZCR_ELx_LEN_MASK;
226 write_sysreg_s(tmp | val, SYS_ZCR_EL1);
227 break;
b42990d3
MB
228#endif
229#ifdef CONFIG_ARM64_SME
230 case ARM64_VEC_SME:
231 tmp = read_sysreg_s(SYS_SMCR_EL1) & ~SMCR_ELx_LEN_MASK;
232 write_sysreg_s(tmp | val, SYS_SMCR_EL1);
233 break;
b5bc00ff
MB
234#endif
235 default:
236 WARN_ON_ONCE(1);
237 break;
238 }
239}
240
241static inline int vec_max_vl(enum vec_type type)
242{
243 return vl_info[type].max_vl;
244}
245
246static inline int vec_max_virtualisable_vl(enum vec_type type)
247{
248 return vl_info[type].max_virtualisable_vl;
249}
250
251static inline int sve_max_vl(void)
252{
253 return vec_max_vl(ARM64_VEC_SVE);
254}
255
256static inline int sve_max_virtualisable_vl(void)
257{
258 return vec_max_virtualisable_vl(ARM64_VEC_SVE);
259}
260
261/* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
262static inline bool vq_available(enum vec_type type, unsigned int vq)
263{
264 return test_bit(__vq_to_bit(vq), vl_info[type].vq_map);
265}
266
267static inline bool sve_vq_available(unsigned int vq)
268{
269 return vq_available(ARM64_VEC_SVE, vq);
270}
271
8bd7f91c
MB
272size_t sve_state_size(struct task_struct const *task);
273
bc0ee476
DM
274#else /* ! CONFIG_ARM64_SVE */
275
826a4fdd 276static inline void sve_alloc(struct task_struct *task, bool flush) { }
bc0ee476 277static inline void fpsimd_release_task(struct task_struct *task) { }
43d4da2c
DM
278static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
279static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
280
49ed9204
MB
281static inline int sve_max_virtualisable_vl(void)
282{
283 return 0;
284}
285
2d2123bc
DM
286static inline int sve_set_current_vl(unsigned long arg)
287{
288 return -EINVAL;
289}
290
291static inline int sve_get_current_vl(void)
292{
293 return -EINVAL;
294}
295
b5bc00ff
MB
296static inline int sve_max_vl(void)
297{
298 return -EINVAL;
299}
300
301static inline bool sve_vq_available(unsigned int vq) { return false; }
302
f9209e26
MR
303static inline void sve_user_disable(void) { BUILD_BUG(); }
304static inline void sve_user_enable(void) { BUILD_BUG(); }
305
a9f8696d
XT
306#define sve_cond_update_zcr_vq(val, reg) do { } while (0)
307
b5bc00ff
MB
308static inline void vec_init_vq_map(enum vec_type t) { }
309static inline void vec_update_vq_map(enum vec_type t) { }
310static inline int vec_verify_vq_map(enum vec_type t) { return 0; }
2e0f2478 311static inline void sve_setup(void) { }
bc0ee476 312
8bd7f91c
MB
313static inline size_t sve_state_size(struct task_struct const *task)
314{
315 return 0;
316}
317
bc0ee476
DM
318#endif /* ! CONFIG_ARM64_SVE */
319
ca8a4ebc
MB
320#ifdef CONFIG_ARM64_SME
321
8bd7f91c
MB
322static inline void sme_user_disable(void)
323{
324 sysreg_clear_set(cpacr_el1, CPACR_EL1_SMEN_EL0EN, 0);
325}
326
327static inline void sme_user_enable(void)
328{
329 sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_SMEN_EL0EN);
330}
331
ca8a4ebc
MB
332static inline void sme_smstart_sm(void)
333{
334 asm volatile(__msr_s(SYS_SVCR_SMSTART_SM_EL0, "xzr"));
335}
336
337static inline void sme_smstop_sm(void)
338{
339 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SM_EL0, "xzr"));
340}
341
342static inline void sme_smstop(void)
343{
344 asm volatile(__msr_s(SYS_SVCR_SMSTOP_SMZA_EL0, "xzr"));
345}
346
b42990d3
MB
347extern void __init sme_setup(void);
348
349static inline int sme_max_vl(void)
350{
351 return vec_max_vl(ARM64_VEC_SME);
352}
353
354static inline int sme_max_virtualisable_vl(void)
355{
356 return vec_max_virtualisable_vl(ARM64_VEC_SME);
357}
358
5d0a8d2f 359extern void sme_alloc(struct task_struct *task, bool flush);
b42990d3 360extern unsigned int sme_get_vl(void);
9e4ab6c8
MB
361extern int sme_set_current_vl(unsigned long arg);
362extern int sme_get_current_vl(void);
b42990d3 363
8bd7f91c
MB
364/*
365 * Return how many bytes of memory are required to store the full SME
ce514000
MB
366 * specific state for task, given task's currently configured vector
367 * length.
8bd7f91c 368 */
ce514000 369static inline size_t sme_state_size(struct task_struct const *task)
8bd7f91c
MB
370{
371 unsigned int vl = task_get_sme_vl(task);
ce514000 372 size_t size;
8bd7f91c 373
ce514000
MB
374 size = ZA_SIG_REGS_SIZE(sve_vq_from_vl(vl));
375
d6138b4a
MB
376 if (system_supports_sme2())
377 size += ZT_SIG_REG_SIZE;
378
ce514000 379 return size;
8bd7f91c
MB
380}
381
ca8a4ebc
MB
382#else
383
8bd7f91c
MB
384static inline void sme_user_disable(void) { BUILD_BUG(); }
385static inline void sme_user_enable(void) { BUILD_BUG(); }
386
ca8a4ebc
MB
387static inline void sme_smstart_sm(void) { }
388static inline void sme_smstop_sm(void) { }
389static inline void sme_smstop(void) { }
390
5d0a8d2f 391static inline void sme_alloc(struct task_struct *task, bool flush) { }
b42990d3
MB
392static inline void sme_setup(void) { }
393static inline unsigned int sme_get_vl(void) { return 0; }
394static inline int sme_max_vl(void) { return 0; }
395static inline int sme_max_virtualisable_vl(void) { return 0; }
9e4ab6c8
MB
396static inline int sme_set_current_vl(unsigned long arg) { return -EINVAL; }
397static inline int sme_get_current_vl(void) { return -EINVAL; }
b42990d3 398
ce514000 399static inline size_t sme_state_size(struct task_struct const *task)
8bd7f91c
MB
400{
401 return 0;
402}
403
ca8a4ebc
MB
404#endif /* ! CONFIG_ARM64_SME */
405
4328825d
DM
406/* For use by EFI runtime services calls only */
407extern void __efi_fpsimd_begin(void);
408extern void __efi_fpsimd_end(void);
409
53631b54
CM
410#endif
411
412#endif