1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * FP/SIMD context switching and fault handling
4 *
5 * Copyright (C) 2012 ARM Ltd.
6 * Author: Catalin Marinas <catalin.marinas@arm.com>
7 */
8
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/bottom_half.h>
12 #include <linux/bug.h>
13 #include <linux/cache.h>
14 #include <linux/compat.h>
15 #include <linux/compiler.h>
16 #include <linux/cpu.h>
17 #include <linux/cpu_pm.h>
18 #include <linux/ctype.h>
19 #include <linux/kernel.h>
20 #include <linux/linkage.h>
21 #include <linux/irqflags.h>
22 #include <linux/init.h>
23 #include <linux/percpu.h>
24 #include <linux/prctl.h>
25 #include <linux/preempt.h>
26 #include <linux/ptrace.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sched/task_stack.h>
29 #include <linux/signal.h>
30 #include <linux/slab.h>
31 #include <linux/stddef.h>
32 #include <linux/sysctl.h>
33 #include <linux/swab.h>
34
35 #include <asm/esr.h>
36 #include <asm/exception.h>
37 #include <asm/fpsimd.h>
38 #include <asm/cpufeature.h>
39 #include <asm/cputype.h>
40 #include <asm/neon.h>
41 #include <asm/processor.h>
42 #include <asm/simd.h>
43 #include <asm/sigcontext.h>
44 #include <asm/sysreg.h>
45 #include <asm/traps.h>
46 #include <asm/virt.h>
47
48 #define FPEXC_IOF (1 << 0)
49 #define FPEXC_DZF (1 << 1)
50 #define FPEXC_OFF (1 << 2)
51 #define FPEXC_UFF (1 << 3)
52 #define FPEXC_IXF (1 << 4)
53 #define FPEXC_IDF (1 << 7)
54
55 /*
56 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
57 *
58 * In order to reduce the number of times the FPSIMD state is needlessly saved
59 * and restored, we need to keep track of two things:
60 * (a) for each task, we need to remember which CPU was the last one to have
61 * the task's FPSIMD state loaded into its FPSIMD registers;
62 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
63 * been loaded into its FPSIMD registers most recently, or whether it has
64 * been used to perform kernel mode NEON in the meantime.
65 *
66 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
67 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
68 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
69 * address of the userland FPSIMD state of the task that was loaded onto the CPU
70 * the most recently, or NULL if kernel mode NEON has been performed after that.
71 *
72 * With this in place, we no longer have to restore the next FPSIMD state right
73 * when switching between tasks. Instead, we can defer this check to userland
74 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
75 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
76 * can omit the FPSIMD restore.
77 *
78 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
79 * indicate whether or not the userland FPSIMD state of the current task is
80 * present in the registers. The flag is set unless the FPSIMD registers of this
81 * CPU currently contain the most recent userland FPSIMD state of the current
82 * task. If the task is behaving as a VMM, then this is will be managed by
83 * KVM which will clear it to indicate that the vcpu FPSIMD state is currently
84 * loaded on the CPU, allowing the state to be saved if a FPSIMD-aware
85 * softirq kicks in. Upon vcpu_put(), KVM will save the vcpu FP state and
86 * flag the register state as invalid.
87 *
88 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
89 * save the task's FPSIMD context back to task_struct from softirq context.
90 * To prevent this from racing with the manipulation of the task's FPSIMD state
91 * from task context and thereby corrupting the state, it is necessary to
92 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
93 * flag with {, __}get_cpu_fpsimd_context(). This will still allow softirqs to
94 * run but prevent them to use FPSIMD.
95 *
96 * For a certain task, the sequence may look something like this:
97 * - the task gets scheduled in; if both the task's fpsimd_cpu field
98 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
99 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
100 * cleared, otherwise it is set;
101 *
102 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
103 * userland FPSIMD state is copied from memory to the registers, the task's
104 * fpsimd_cpu field is set to the id of the current CPU, the current
105 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
106 * TIF_FOREIGN_FPSTATE flag is cleared;
107 *
108 * - the task executes an ordinary syscall; upon return to userland, the
109 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
110 * restored;
111 *
112 * - the task executes a syscall which executes some NEON instructions; this is
113 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
114 * register contents to memory, clears the fpsimd_last_state per-cpu variable
115 * and sets the TIF_FOREIGN_FPSTATE flag;
116 *
117 * - the task gets preempted after kernel_neon_end() is called; as we have not
118 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
119 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
120 */
121 struct fpsimd_last_state_struct {
122 struct user_fpsimd_state *st;
123 void *sve_state;
124 void *za_state;
125 u64 *svcr;
126 unsigned int sve_vl;
127 unsigned int sme_vl;
128 };
129
130 static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
131
132 __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX] = {
133 #ifdef CONFIG_ARM64_SVE
134 [ARM64_VEC_SVE] = {
135 .type = ARM64_VEC_SVE,
136 .name = "SVE",
137 .min_vl = SVE_VL_MIN,
138 .max_vl = SVE_VL_MIN,
139 .max_virtualisable_vl = SVE_VL_MIN,
140 },
141 #endif
142 #ifdef CONFIG_ARM64_SME
143 [ARM64_VEC_SME] = {
144 .type = ARM64_VEC_SME,
145 .name = "SME",
146 },
147 #endif
148 };
149
vec_vl_inherit_flag(enum vec_type type)150 static unsigned int vec_vl_inherit_flag(enum vec_type type)
151 {
152 switch (type) {
153 case ARM64_VEC_SVE:
154 return TIF_SVE_VL_INHERIT;
155 case ARM64_VEC_SME:
156 return TIF_SME_VL_INHERIT;
157 default:
158 WARN_ON_ONCE(1);
159 return 0;
160 }
161 }
162
163 struct vl_config {
164 int __default_vl; /* Default VL for tasks */
165 };
166
167 static struct vl_config vl_config[ARM64_VEC_MAX];
168
get_default_vl(enum vec_type type)169 static inline int get_default_vl(enum vec_type type)
170 {
171 return READ_ONCE(vl_config[type].__default_vl);
172 }
173
174 #ifdef CONFIG_ARM64_SVE
175
get_sve_default_vl(void)176 static inline int get_sve_default_vl(void)
177 {
178 return get_default_vl(ARM64_VEC_SVE);
179 }
180
set_default_vl(enum vec_type type,int val)181 static inline void set_default_vl(enum vec_type type, int val)
182 {
183 WRITE_ONCE(vl_config[type].__default_vl, val);
184 }
185
set_sve_default_vl(int val)186 static inline void set_sve_default_vl(int val)
187 {
188 set_default_vl(ARM64_VEC_SVE, val);
189 }
190
191 static void __percpu *efi_sve_state;
192
193 #else /* ! CONFIG_ARM64_SVE */
194
195 /* Dummy declaration for code that will be optimised out: */
196 extern void __percpu *efi_sve_state;
197
198 #endif /* ! CONFIG_ARM64_SVE */
199
200 #ifdef CONFIG_ARM64_SME
201
get_sme_default_vl(void)202 static int get_sme_default_vl(void)
203 {
204 return get_default_vl(ARM64_VEC_SME);
205 }
206
set_sme_default_vl(int val)207 static void set_sme_default_vl(int val)
208 {
209 set_default_vl(ARM64_VEC_SME, val);
210 }
211
212 static void sme_free(struct task_struct *);
213
214 #else
215
sme_free(struct task_struct * t)216 static inline void sme_free(struct task_struct *t) { }
217
218 #endif
219
220 DEFINE_PER_CPU(bool, fpsimd_context_busy);
221 EXPORT_PER_CPU_SYMBOL(fpsimd_context_busy);
222
223 static void fpsimd_bind_task_to_cpu(void);
224
__get_cpu_fpsimd_context(void)225 static void __get_cpu_fpsimd_context(void)
226 {
227 bool busy = __this_cpu_xchg(fpsimd_context_busy, true);
228
229 WARN_ON(busy);
230 }
231
232 /*
233 * Claim ownership of the CPU FPSIMD context for use by the calling context.
234 *
235 * The caller may freely manipulate the FPSIMD context metadata until
236 * put_cpu_fpsimd_context() is called.
237 *
238 * The double-underscore version must only be called if you know the task
239 * can't be preempted.
240 *
241 * On RT kernels local_bh_disable() is not sufficient because it only
242 * serializes soft interrupt related sections via a local lock, but stays
243 * preemptible. Disabling preemption is the right choice here as bottom
244 * half processing is always in thread context on RT kernels so it
245 * implicitly prevents bottom half processing as well.
246 */
get_cpu_fpsimd_context(void)247 static void get_cpu_fpsimd_context(void)
248 {
249 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
250 local_bh_disable();
251 else
252 preempt_disable();
253 __get_cpu_fpsimd_context();
254 }
255
__put_cpu_fpsimd_context(void)256 static void __put_cpu_fpsimd_context(void)
257 {
258 bool busy = __this_cpu_xchg(fpsimd_context_busy, false);
259
260 WARN_ON(!busy); /* No matching get_cpu_fpsimd_context()? */
261 }
262
263 /*
264 * Release the CPU FPSIMD context.
265 *
266 * Must be called from a context in which get_cpu_fpsimd_context() was
267 * previously called, with no call to put_cpu_fpsimd_context() in the
268 * meantime.
269 */
put_cpu_fpsimd_context(void)270 static void put_cpu_fpsimd_context(void)
271 {
272 __put_cpu_fpsimd_context();
273 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
274 local_bh_enable();
275 else
276 preempt_enable();
277 }
278
have_cpu_fpsimd_context(void)279 static bool have_cpu_fpsimd_context(void)
280 {
281 return !preemptible() && __this_cpu_read(fpsimd_context_busy);
282 }
283
task_get_vl(const struct task_struct * task,enum vec_type type)284 unsigned int task_get_vl(const struct task_struct *task, enum vec_type type)
285 {
286 return task->thread.vl[type];
287 }
288
task_set_vl(struct task_struct * task,enum vec_type type,unsigned long vl)289 void task_set_vl(struct task_struct *task, enum vec_type type,
290 unsigned long vl)
291 {
292 task->thread.vl[type] = vl;
293 }
294
task_get_vl_onexec(const struct task_struct * task,enum vec_type type)295 unsigned int task_get_vl_onexec(const struct task_struct *task,
296 enum vec_type type)
297 {
298 return task->thread.vl_onexec[type];
299 }
300
task_set_vl_onexec(struct task_struct * task,enum vec_type type,unsigned long vl)301 void task_set_vl_onexec(struct task_struct *task, enum vec_type type,
302 unsigned long vl)
303 {
304 task->thread.vl_onexec[type] = vl;
305 }
306
307 /*
308 * TIF_SME controls whether a task can use SME without trapping while
309 * in userspace, when TIF_SME is set then we must have storage
310 * alocated in sve_state and za_state to store the contents of both ZA
311 * and the SVE registers for both streaming and non-streaming modes.
312 *
313 * If both SVCR.ZA and SVCR.SM are disabled then at any point we
314 * may disable TIF_SME and reenable traps.
315 */
316
317
318 /*
319 * TIF_SVE controls whether a task can use SVE without trapping while
320 * in userspace, and also (together with TIF_SME) the way a task's
321 * FPSIMD/SVE state is stored in thread_struct.
322 *
323 * The kernel uses this flag to track whether a user task is actively
324 * using SVE, and therefore whether full SVE register state needs to
325 * be tracked. If not, the cheaper FPSIMD context handling code can
326 * be used instead of the more costly SVE equivalents.
327 *
328 * * TIF_SVE or SVCR.SM set:
329 *
330 * The task can execute SVE instructions while in userspace without
331 * trapping to the kernel.
332 *
333 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
334 * corresponding Zn), P0-P15 and FFR are encoded in
335 * task->thread.sve_state, formatted appropriately for vector
336 * length task->thread.sve_vl or, if SVCR.SM is set,
337 * task->thread.sme_vl.
338 *
339 * task->thread.sve_state must point to a valid buffer at least
340 * sve_state_size(task) bytes in size.
341 *
342 * During any syscall, the kernel may optionally clear TIF_SVE and
343 * discard the vector state except for the FPSIMD subset.
344 *
345 * * TIF_SVE clear:
346 *
347 * An attempt by the user task to execute an SVE instruction causes
348 * do_sve_acc() to be called, which does some preparation and then
349 * sets TIF_SVE.
350 *
351 * When stored, FPSIMD registers V0-V31 are encoded in
352 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
353 * logically zero but not stored anywhere; P0-P15 and FFR are not
354 * stored and have unspecified values from userspace's point of
355 * view. For hygiene purposes, the kernel zeroes them on next use,
356 * but userspace is discouraged from relying on this.
357 *
358 * task->thread.sve_state does not need to be non-NULL, valid or any
359 * particular size: it must not be dereferenced.
360 *
361 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
362 * irrespective of whether TIF_SVE is clear or set, since these are
363 * not vector length dependent.
364 */
365
366 /*
367 * Update current's FPSIMD/SVE registers from thread_struct.
368 *
369 * This function should be called only when the FPSIMD/SVE state in
370 * thread_struct is known to be up to date, when preparing to enter
371 * userspace.
372 */
task_fpsimd_load(void)373 static void task_fpsimd_load(void)
374 {
375 bool restore_sve_regs = false;
376 bool restore_ffr;
377
378 WARN_ON(!system_supports_fpsimd());
379 WARN_ON(!have_cpu_fpsimd_context());
380
381 /* Check if we should restore SVE first */
382 if (IS_ENABLED(CONFIG_ARM64_SVE) && test_thread_flag(TIF_SVE)) {
383 sve_set_vq(sve_vq_from_vl(task_get_sve_vl(current)) - 1);
384 restore_sve_regs = true;
385 restore_ffr = true;
386 }
387
388 /* Restore SME, override SVE register configuration if needed */
389 if (system_supports_sme()) {
390 unsigned long sme_vl = task_get_sme_vl(current);
391
392 /* Ensure VL is set up for restoring data */
393 if (test_thread_flag(TIF_SME))
394 sme_set_vq(sve_vq_from_vl(sme_vl) - 1);
395
396 write_sysreg_s(current->thread.svcr, SYS_SVCR);
397
398 if (thread_za_enabled(¤t->thread))
399 za_load_state(current->thread.za_state);
400
401 if (thread_sm_enabled(¤t->thread)) {
402 restore_sve_regs = true;
403 restore_ffr = system_supports_fa64();
404 }
405 }
406
407 if (restore_sve_regs)
408 sve_load_state(sve_pffr(¤t->thread),
409 ¤t->thread.uw.fpsimd_state.fpsr,
410 restore_ffr);
411 else
412 fpsimd_load_state(¤t->thread.uw.fpsimd_state);
413 }
414
415 /*
416 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
417 * date with respect to the CPU registers. Note carefully that the
418 * current context is the context last bound to the CPU stored in
419 * last, if KVM is involved this may be the guest VM context rather
420 * than the host thread for the VM pointed to by current. This means
421 * that we must always reference the state storage via last rather
422 * than via current, other than the TIF_ flags which KVM will
423 * carefully maintain for us.
424 */
fpsimd_save(void)425 static void fpsimd_save(void)
426 {
427 struct fpsimd_last_state_struct const *last =
428 this_cpu_ptr(&fpsimd_last_state);
429 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
430 bool save_sve_regs = false;
431 bool save_ffr;
432 unsigned int vl;
433
434 WARN_ON(!system_supports_fpsimd());
435 WARN_ON(!have_cpu_fpsimd_context());
436
437 if (test_thread_flag(TIF_FOREIGN_FPSTATE))
438 return;
439
440 if (test_thread_flag(TIF_SVE)) {
441 save_sve_regs = true;
442 save_ffr = true;
443 vl = last->sve_vl;
444 }
445
446 if (system_supports_sme()) {
447 u64 *svcr = last->svcr;
448 *svcr = read_sysreg_s(SYS_SVCR);
449
450 *svcr = read_sysreg_s(SYS_SVCR);
451
452 if (*svcr & SVCR_ZA_MASK)
453 za_save_state(last->za_state);
454
455 /* If we are in streaming mode override regular SVE. */
456 if (*svcr & SVCR_SM_MASK) {
457 save_sve_regs = true;
458 save_ffr = system_supports_fa64();
459 vl = last->sme_vl;
460 }
461 }
462
463 if (IS_ENABLED(CONFIG_ARM64_SVE) && save_sve_regs) {
464 /* Get the configured VL from RDVL, will account for SM */
465 if (WARN_ON(sve_get_vl() != vl)) {
466 /*
467 * Can't save the user regs, so current would
468 * re-enter user with corrupt state.
469 * There's no way to recover, so kill it:
470 */
471 force_signal_inject(SIGKILL, SI_KERNEL, 0, 0);
472 return;
473 }
474
475 sve_save_state((char *)last->sve_state +
476 sve_ffr_offset(vl),
477 &last->st->fpsr, save_ffr);
478 } else {
479 fpsimd_save_state(last->st);
480 }
481 }
482
483 /*
484 * All vector length selection from userspace comes through here.
485 * We're on a slow path, so some sanity-checks are included.
486 * If things go wrong there's a bug somewhere, but try to fall back to a
487 * safe choice.
488 */
find_supported_vector_length(enum vec_type type,unsigned int vl)489 static unsigned int find_supported_vector_length(enum vec_type type,
490 unsigned int vl)
491 {
492 struct vl_info *info = &vl_info[type];
493 int bit;
494 int max_vl = info->max_vl;
495
496 if (WARN_ON(!sve_vl_valid(vl)))
497 vl = info->min_vl;
498
499 if (WARN_ON(!sve_vl_valid(max_vl)))
500 max_vl = info->min_vl;
501
502 if (vl > max_vl)
503 vl = max_vl;
504 if (vl < info->min_vl)
505 vl = info->min_vl;
506
507 bit = find_next_bit(info->vq_map, SVE_VQ_MAX,
508 __vq_to_bit(sve_vq_from_vl(vl)));
509 return sve_vl_from_vq(__bit_to_vq(bit));
510 }
511
512 #if defined(CONFIG_ARM64_SVE) && defined(CONFIG_SYSCTL)
513
vec_proc_do_default_vl(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)514 static int vec_proc_do_default_vl(struct ctl_table *table, int write,
515 void *buffer, size_t *lenp, loff_t *ppos)
516 {
517 struct vl_info *info = table->extra1;
518 enum vec_type type = info->type;
519 int ret;
520 int vl = get_default_vl(type);
521 struct ctl_table tmp_table = {
522 .data = &vl,
523 .maxlen = sizeof(vl),
524 };
525
526 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
527 if (ret || !write)
528 return ret;
529
530 /* Writing -1 has the special meaning "set to max": */
531 if (vl == -1)
532 vl = info->max_vl;
533
534 if (!sve_vl_valid(vl))
535 return -EINVAL;
536
537 set_default_vl(type, find_supported_vector_length(type, vl));
538 return 0;
539 }
540
541 static struct ctl_table sve_default_vl_table[] = {
542 {
543 .procname = "sve_default_vector_length",
544 .mode = 0644,
545 .proc_handler = vec_proc_do_default_vl,
546 .extra1 = &vl_info[ARM64_VEC_SVE],
547 },
548 { }
549 };
550
sve_sysctl_init(void)551 static int __init sve_sysctl_init(void)
552 {
553 if (system_supports_sve())
554 if (!register_sysctl("abi", sve_default_vl_table))
555 return -EINVAL;
556
557 return 0;
558 }
559
560 #else /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
sve_sysctl_init(void)561 static int __init sve_sysctl_init(void) { return 0; }
562 #endif /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
563
564 #if defined(CONFIG_ARM64_SME) && defined(CONFIG_SYSCTL)
565 static struct ctl_table sme_default_vl_table[] = {
566 {
567 .procname = "sme_default_vector_length",
568 .mode = 0644,
569 .proc_handler = vec_proc_do_default_vl,
570 .extra1 = &vl_info[ARM64_VEC_SME],
571 },
572 { }
573 };
574
sme_sysctl_init(void)575 static int __init sme_sysctl_init(void)
576 {
577 if (system_supports_sme())
578 if (!register_sysctl("abi", sme_default_vl_table))
579 return -EINVAL;
580
581 return 0;
582 }
583
584 #else /* ! (CONFIG_ARM64_SME && CONFIG_SYSCTL) */
sme_sysctl_init(void)585 static int __init sme_sysctl_init(void) { return 0; }
586 #endif /* ! (CONFIG_ARM64_SME && CONFIG_SYSCTL) */
587
588 #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
589 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
590
591 #ifdef CONFIG_CPU_BIG_ENDIAN
arm64_cpu_to_le128(__uint128_t x)592 static __uint128_t arm64_cpu_to_le128(__uint128_t x)
593 {
594 u64 a = swab64(x);
595 u64 b = swab64(x >> 64);
596
597 return ((__uint128_t)a << 64) | b;
598 }
599 #else
arm64_cpu_to_le128(__uint128_t x)600 static __uint128_t arm64_cpu_to_le128(__uint128_t x)
601 {
602 return x;
603 }
604 #endif
605
606 #define arm64_le128_to_cpu(x) arm64_cpu_to_le128(x)
607
__fpsimd_to_sve(void * sst,struct user_fpsimd_state const * fst,unsigned int vq)608 static void __fpsimd_to_sve(void *sst, struct user_fpsimd_state const *fst,
609 unsigned int vq)
610 {
611 unsigned int i;
612 __uint128_t *p;
613
614 for (i = 0; i < SVE_NUM_ZREGS; ++i) {
615 p = (__uint128_t *)ZREG(sst, vq, i);
616 *p = arm64_cpu_to_le128(fst->vregs[i]);
617 }
618 }
619
620 /*
621 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
622 * task->thread.sve_state.
623 *
624 * Task can be a non-runnable task, or current. In the latter case,
625 * the caller must have ownership of the cpu FPSIMD context before calling
626 * this function.
627 * task->thread.sve_state must point to at least sve_state_size(task)
628 * bytes of allocated kernel memory.
629 * task->thread.uw.fpsimd_state must be up to date before calling this
630 * function.
631 */
fpsimd_to_sve(struct task_struct * task)632 static void fpsimd_to_sve(struct task_struct *task)
633 {
634 unsigned int vq;
635 void *sst = task->thread.sve_state;
636 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
637
638 if (!system_supports_sve())
639 return;
640
641 vq = sve_vq_from_vl(thread_get_cur_vl(&task->thread));
642 __fpsimd_to_sve(sst, fst, vq);
643 }
644
645 /*
646 * Transfer the SVE state in task->thread.sve_state to
647 * task->thread.uw.fpsimd_state.
648 *
649 * Task can be a non-runnable task, or current. In the latter case,
650 * the caller must have ownership of the cpu FPSIMD context before calling
651 * this function.
652 * task->thread.sve_state must point to at least sve_state_size(task)
653 * bytes of allocated kernel memory.
654 * task->thread.sve_state must be up to date before calling this function.
655 */
sve_to_fpsimd(struct task_struct * task)656 static void sve_to_fpsimd(struct task_struct *task)
657 {
658 unsigned int vq, vl;
659 void const *sst = task->thread.sve_state;
660 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
661 unsigned int i;
662 __uint128_t const *p;
663
664 if (!system_supports_sve())
665 return;
666
667 vl = thread_get_cur_vl(&task->thread);
668 vq = sve_vq_from_vl(vl);
669 for (i = 0; i < SVE_NUM_ZREGS; ++i) {
670 p = (__uint128_t const *)ZREG(sst, vq, i);
671 fst->vregs[i] = arm64_le128_to_cpu(*p);
672 }
673 }
674
675 #ifdef CONFIG_ARM64_SVE
676 /*
677 * Call __sve_free() directly only if you know task can't be scheduled
678 * or preempted.
679 */
__sve_free(struct task_struct * task)680 static void __sve_free(struct task_struct *task)
681 {
682 kfree(task->thread.sve_state);
683 task->thread.sve_state = NULL;
684 }
685
sve_free(struct task_struct * task)686 static void sve_free(struct task_struct *task)
687 {
688 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
689
690 __sve_free(task);
691 }
692
693 /*
694 * Return how many bytes of memory are required to store the full SVE
695 * state for task, given task's currently configured vector length.
696 */
sve_state_size(struct task_struct const * task)697 size_t sve_state_size(struct task_struct const *task)
698 {
699 unsigned int vl = 0;
700
701 if (system_supports_sve())
702 vl = task_get_sve_vl(task);
703 if (system_supports_sme())
704 vl = max(vl, task_get_sme_vl(task));
705
706 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(vl));
707 }
708
709 /*
710 * Ensure that task->thread.sve_state is allocated and sufficiently large.
711 *
712 * This function should be used only in preparation for replacing
713 * task->thread.sve_state with new data. The memory is always zeroed
714 * here to prevent stale data from showing through: this is done in
715 * the interest of testability and predictability: except in the
716 * do_sve_acc() case, there is no ABI requirement to hide stale data
717 * written previously be task.
718 */
sve_alloc(struct task_struct * task,bool flush)719 void sve_alloc(struct task_struct *task, bool flush)
720 {
721 if (task->thread.sve_state) {
722 if (flush)
723 memset(task->thread.sve_state, 0,
724 sve_state_size(task));
725 return;
726 }
727
728 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
729 task->thread.sve_state =
730 kzalloc(sve_state_size(task), GFP_KERNEL);
731 }
732
733
734 /*
735 * Force the FPSIMD state shared with SVE to be updated in the SVE state
736 * even if the SVE state is the current active state.
737 *
738 * This should only be called by ptrace. task must be non-runnable.
739 * task->thread.sve_state must point to at least sve_state_size(task)
740 * bytes of allocated kernel memory.
741 */
fpsimd_force_sync_to_sve(struct task_struct * task)742 void fpsimd_force_sync_to_sve(struct task_struct *task)
743 {
744 fpsimd_to_sve(task);
745 }
746
747 /*
748 * Ensure that task->thread.sve_state is up to date with respect to
749 * the user task, irrespective of when SVE is in use or not.
750 *
751 * This should only be called by ptrace. task must be non-runnable.
752 * task->thread.sve_state must point to at least sve_state_size(task)
753 * bytes of allocated kernel memory.
754 */
fpsimd_sync_to_sve(struct task_struct * task)755 void fpsimd_sync_to_sve(struct task_struct *task)
756 {
757 if (!test_tsk_thread_flag(task, TIF_SVE) &&
758 !thread_sm_enabled(&task->thread))
759 fpsimd_to_sve(task);
760 }
761
762 /*
763 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
764 * the user task, irrespective of whether SVE is in use or not.
765 *
766 * This should only be called by ptrace. task must be non-runnable.
767 * task->thread.sve_state must point to at least sve_state_size(task)
768 * bytes of allocated kernel memory.
769 */
sve_sync_to_fpsimd(struct task_struct * task)770 void sve_sync_to_fpsimd(struct task_struct *task)
771 {
772 if (test_tsk_thread_flag(task, TIF_SVE) ||
773 thread_sm_enabled(&task->thread))
774 sve_to_fpsimd(task);
775 }
776
777 /*
778 * Ensure that task->thread.sve_state is up to date with respect to
779 * the task->thread.uw.fpsimd_state.
780 *
781 * This should only be called by ptrace to merge new FPSIMD register
782 * values into a task for which SVE is currently active.
783 * task must be non-runnable.
784 * task->thread.sve_state must point to at least sve_state_size(task)
785 * bytes of allocated kernel memory.
786 * task->thread.uw.fpsimd_state must already have been initialised with
787 * the new FPSIMD register values to be merged in.
788 */
sve_sync_from_fpsimd_zeropad(struct task_struct * task)789 void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
790 {
791 unsigned int vq;
792 void *sst = task->thread.sve_state;
793 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
794
795 if (!test_tsk_thread_flag(task, TIF_SVE))
796 return;
797
798 vq = sve_vq_from_vl(thread_get_cur_vl(&task->thread));
799
800 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
801 __fpsimd_to_sve(sst, fst, vq);
802 }
803
vec_set_vector_length(struct task_struct * task,enum vec_type type,unsigned long vl,unsigned long flags)804 int vec_set_vector_length(struct task_struct *task, enum vec_type type,
805 unsigned long vl, unsigned long flags)
806 {
807 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
808 PR_SVE_SET_VL_ONEXEC))
809 return -EINVAL;
810
811 if (!sve_vl_valid(vl))
812 return -EINVAL;
813
814 /*
815 * Clamp to the maximum vector length that VL-agnostic code
816 * can work with. A flag may be assigned in the future to
817 * allow setting of larger vector lengths without confusing
818 * older software.
819 */
820 if (vl > VL_ARCH_MAX)
821 vl = VL_ARCH_MAX;
822
823 vl = find_supported_vector_length(type, vl);
824
825 if (flags & (PR_SVE_VL_INHERIT |
826 PR_SVE_SET_VL_ONEXEC))
827 task_set_vl_onexec(task, type, vl);
828 else
829 /* Reset VL to system default on next exec: */
830 task_set_vl_onexec(task, type, 0);
831
832 /* Only actually set the VL if not deferred: */
833 if (flags & PR_SVE_SET_VL_ONEXEC)
834 goto out;
835
836 if (vl == task_get_vl(task, type))
837 goto out;
838
839 /*
840 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
841 * write any live register state back to task_struct, and convert to a
842 * regular FPSIMD thread.
843 */
844 if (task == current) {
845 get_cpu_fpsimd_context();
846
847 fpsimd_save();
848 }
849
850 fpsimd_flush_task_state(task);
851 if (test_and_clear_tsk_thread_flag(task, TIF_SVE) ||
852 thread_sm_enabled(&task->thread))
853 sve_to_fpsimd(task);
854
855 if (system_supports_sme() && type == ARM64_VEC_SME) {
856 task->thread.svcr &= ~(SVCR_SM_MASK |
857 SVCR_ZA_MASK);
858 clear_thread_flag(TIF_SME);
859 }
860
861 if (task == current)
862 put_cpu_fpsimd_context();
863
864 /*
865 * Force reallocation of task SVE and SME state to the correct
866 * size on next use:
867 */
868 sve_free(task);
869 if (system_supports_sme() && type == ARM64_VEC_SME)
870 sme_free(task);
871
872 task_set_vl(task, type, vl);
873
874 out:
875 update_tsk_thread_flag(task, vec_vl_inherit_flag(type),
876 flags & PR_SVE_VL_INHERIT);
877
878 return 0;
879 }
880
881 /*
882 * Encode the current vector length and flags for return.
883 * This is only required for prctl(): ptrace has separate fields.
884 * SVE and SME use the same bits for _ONEXEC and _INHERIT.
885 *
886 * flags are as for vec_set_vector_length().
887 */
vec_prctl_status(enum vec_type type,unsigned long flags)888 static int vec_prctl_status(enum vec_type type, unsigned long flags)
889 {
890 int ret;
891
892 if (flags & PR_SVE_SET_VL_ONEXEC)
893 ret = task_get_vl_onexec(current, type);
894 else
895 ret = task_get_vl(current, type);
896
897 if (test_thread_flag(vec_vl_inherit_flag(type)))
898 ret |= PR_SVE_VL_INHERIT;
899
900 return ret;
901 }
902
903 /* PR_SVE_SET_VL */
sve_set_current_vl(unsigned long arg)904 int sve_set_current_vl(unsigned long arg)
905 {
906 unsigned long vl, flags;
907 int ret;
908
909 vl = arg & PR_SVE_VL_LEN_MASK;
910 flags = arg & ~vl;
911
912 if (!system_supports_sve() || is_compat_task())
913 return -EINVAL;
914
915 ret = vec_set_vector_length(current, ARM64_VEC_SVE, vl, flags);
916 if (ret)
917 return ret;
918
919 return vec_prctl_status(ARM64_VEC_SVE, flags);
920 }
921
922 /* PR_SVE_GET_VL */
sve_get_current_vl(void)923 int sve_get_current_vl(void)
924 {
925 if (!system_supports_sve() || is_compat_task())
926 return -EINVAL;
927
928 return vec_prctl_status(ARM64_VEC_SVE, 0);
929 }
930
931 #ifdef CONFIG_ARM64_SME
932 /* PR_SME_SET_VL */
sme_set_current_vl(unsigned long arg)933 int sme_set_current_vl(unsigned long arg)
934 {
935 unsigned long vl, flags;
936 int ret;
937
938 vl = arg & PR_SME_VL_LEN_MASK;
939 flags = arg & ~vl;
940
941 if (!system_supports_sme() || is_compat_task())
942 return -EINVAL;
943
944 ret = vec_set_vector_length(current, ARM64_VEC_SME, vl, flags);
945 if (ret)
946 return ret;
947
948 return vec_prctl_status(ARM64_VEC_SME, flags);
949 }
950
951 /* PR_SME_GET_VL */
sme_get_current_vl(void)952 int sme_get_current_vl(void)
953 {
954 if (!system_supports_sme() || is_compat_task())
955 return -EINVAL;
956
957 return vec_prctl_status(ARM64_VEC_SME, 0);
958 }
959 #endif /* CONFIG_ARM64_SME */
960
vec_probe_vqs(struct vl_info * info,DECLARE_BITMAP (map,SVE_VQ_MAX))961 static void vec_probe_vqs(struct vl_info *info,
962 DECLARE_BITMAP(map, SVE_VQ_MAX))
963 {
964 unsigned int vq, vl;
965
966 bitmap_zero(map, SVE_VQ_MAX);
967
968 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
969 write_vl(info->type, vq - 1); /* self-syncing */
970
971 switch (info->type) {
972 case ARM64_VEC_SVE:
973 vl = sve_get_vl();
974 break;
975 case ARM64_VEC_SME:
976 vl = sme_get_vl();
977 break;
978 default:
979 vl = 0;
980 break;
981 }
982
983 /* Minimum VL identified? */
984 if (sve_vq_from_vl(vl) > vq)
985 break;
986
987 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
988 set_bit(__vq_to_bit(vq), map);
989 }
990 }
991
992 /*
993 * Initialise the set of known supported VQs for the boot CPU.
994 * This is called during kernel boot, before secondary CPUs are brought up.
995 */
vec_init_vq_map(enum vec_type type)996 void __init vec_init_vq_map(enum vec_type type)
997 {
998 struct vl_info *info = &vl_info[type];
999 vec_probe_vqs(info, info->vq_map);
1000 bitmap_copy(info->vq_partial_map, info->vq_map, SVE_VQ_MAX);
1001 }
1002
1003 /*
1004 * If we haven't committed to the set of supported VQs yet, filter out
1005 * those not supported by the current CPU.
1006 * This function is called during the bring-up of early secondary CPUs only.
1007 */
vec_update_vq_map(enum vec_type type)1008 void vec_update_vq_map(enum vec_type type)
1009 {
1010 struct vl_info *info = &vl_info[type];
1011 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
1012
1013 vec_probe_vqs(info, tmp_map);
1014 bitmap_and(info->vq_map, info->vq_map, tmp_map, SVE_VQ_MAX);
1015 bitmap_or(info->vq_partial_map, info->vq_partial_map, tmp_map,
1016 SVE_VQ_MAX);
1017 }
1018
1019 /*
1020 * Check whether the current CPU supports all VQs in the committed set.
1021 * This function is called during the bring-up of late secondary CPUs only.
1022 */
vec_verify_vq_map(enum vec_type type)1023 int vec_verify_vq_map(enum vec_type type)
1024 {
1025 struct vl_info *info = &vl_info[type];
1026 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
1027 unsigned long b;
1028
1029 vec_probe_vqs(info, tmp_map);
1030
1031 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
1032 if (bitmap_intersects(tmp_map, info->vq_map, SVE_VQ_MAX)) {
1033 pr_warn("%s: cpu%d: Required vector length(s) missing\n",
1034 info->name, smp_processor_id());
1035 return -EINVAL;
1036 }
1037
1038 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
1039 return 0;
1040
1041 /*
1042 * For KVM, it is necessary to ensure that this CPU doesn't
1043 * support any vector length that guests may have probed as
1044 * unsupported.
1045 */
1046
1047 /* Recover the set of supported VQs: */
1048 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
1049 /* Find VQs supported that are not globally supported: */
1050 bitmap_andnot(tmp_map, tmp_map, info->vq_map, SVE_VQ_MAX);
1051
1052 /* Find the lowest such VQ, if any: */
1053 b = find_last_bit(tmp_map, SVE_VQ_MAX);
1054 if (b >= SVE_VQ_MAX)
1055 return 0; /* no mismatches */
1056
1057 /*
1058 * Mismatches above sve_max_virtualisable_vl are fine, since
1059 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
1060 */
1061 if (sve_vl_from_vq(__bit_to_vq(b)) <= info->max_virtualisable_vl) {
1062 pr_warn("%s: cpu%d: Unsupported vector length(s) present\n",
1063 info->name, smp_processor_id());
1064 return -EINVAL;
1065 }
1066
1067 return 0;
1068 }
1069
sve_efi_setup(void)1070 static void __init sve_efi_setup(void)
1071 {
1072 int max_vl = 0;
1073 int i;
1074
1075 if (!IS_ENABLED(CONFIG_EFI))
1076 return;
1077
1078 for (i = 0; i < ARRAY_SIZE(vl_info); i++)
1079 max_vl = max(vl_info[i].max_vl, max_vl);
1080
1081 /*
1082 * alloc_percpu() warns and prints a backtrace if this goes wrong.
1083 * This is evidence of a crippled system and we are returning void,
1084 * so no attempt is made to handle this situation here.
1085 */
1086 if (!sve_vl_valid(max_vl))
1087 goto fail;
1088
1089 efi_sve_state = __alloc_percpu(
1090 SVE_SIG_REGS_SIZE(sve_vq_from_vl(max_vl)), SVE_VQ_BYTES);
1091 if (!efi_sve_state)
1092 goto fail;
1093
1094 return;
1095
1096 fail:
1097 panic("Cannot allocate percpu memory for EFI SVE save/restore");
1098 }
1099
1100 /*
1101 * Enable SVE for EL1.
1102 * Intended for use by the cpufeatures code during CPU boot.
1103 */
sve_kernel_enable(const struct arm64_cpu_capabilities * __always_unused p)1104 void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
1105 {
1106 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
1107 isb();
1108 }
1109
1110 /*
1111 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
1112 * vector length.
1113 *
1114 * Use only if SVE is present.
1115 * This function clobbers the SVE vector length.
1116 */
read_zcr_features(void)1117 u64 read_zcr_features(void)
1118 {
1119 u64 zcr;
1120 unsigned int vq_max;
1121
1122 /*
1123 * Set the maximum possible VL, and write zeroes to all other
1124 * bits to see if they stick.
1125 */
1126 sve_kernel_enable(NULL);
1127 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
1128
1129 zcr = read_sysreg_s(SYS_ZCR_EL1);
1130 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
1131 vq_max = sve_vq_from_vl(sve_get_vl());
1132 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
1133
1134 return zcr;
1135 }
1136
sve_setup(void)1137 void __init sve_setup(void)
1138 {
1139 struct vl_info *info = &vl_info[ARM64_VEC_SVE];
1140 u64 zcr;
1141 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
1142 unsigned long b;
1143
1144 if (!system_supports_sve())
1145 return;
1146
1147 /*
1148 * The SVE architecture mandates support for 128-bit vectors,
1149 * so sve_vq_map must have at least SVE_VQ_MIN set.
1150 * If something went wrong, at least try to patch it up:
1151 */
1152 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), info->vq_map)))
1153 set_bit(__vq_to_bit(SVE_VQ_MIN), info->vq_map);
1154
1155 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
1156 info->max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
1157
1158 /*
1159 * Sanity-check that the max VL we determined through CPU features
1160 * corresponds properly to sve_vq_map. If not, do our best:
1161 */
1162 if (WARN_ON(info->max_vl != find_supported_vector_length(ARM64_VEC_SVE,
1163 info->max_vl)))
1164 info->max_vl = find_supported_vector_length(ARM64_VEC_SVE,
1165 info->max_vl);
1166
1167 /*
1168 * For the default VL, pick the maximum supported value <= 64.
1169 * VL == 64 is guaranteed not to grow the signal frame.
1170 */
1171 set_sve_default_vl(find_supported_vector_length(ARM64_VEC_SVE, 64));
1172
1173 bitmap_andnot(tmp_map, info->vq_partial_map, info->vq_map,
1174 SVE_VQ_MAX);
1175
1176 b = find_last_bit(tmp_map, SVE_VQ_MAX);
1177 if (b >= SVE_VQ_MAX)
1178 /* No non-virtualisable VLs found */
1179 info->max_virtualisable_vl = SVE_VQ_MAX;
1180 else if (WARN_ON(b == SVE_VQ_MAX - 1))
1181 /* No virtualisable VLs? This is architecturally forbidden. */
1182 info->max_virtualisable_vl = SVE_VQ_MIN;
1183 else /* b + 1 < SVE_VQ_MAX */
1184 info->max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
1185
1186 if (info->max_virtualisable_vl > info->max_vl)
1187 info->max_virtualisable_vl = info->max_vl;
1188
1189 pr_info("%s: maximum available vector length %u bytes per vector\n",
1190 info->name, info->max_vl);
1191 pr_info("%s: default vector length %u bytes per vector\n",
1192 info->name, get_sve_default_vl());
1193
1194 /* KVM decides whether to support mismatched systems. Just warn here: */
1195 if (sve_max_virtualisable_vl() < sve_max_vl())
1196 pr_warn("%s: unvirtualisable vector lengths present\n",
1197 info->name);
1198
1199 sve_efi_setup();
1200 }
1201
1202 /*
1203 * Called from the put_task_struct() path, which cannot get here
1204 * unless dead_task is really dead and not schedulable.
1205 */
fpsimd_release_task(struct task_struct * dead_task)1206 void fpsimd_release_task(struct task_struct *dead_task)
1207 {
1208 __sve_free(dead_task);
1209 sme_free(dead_task);
1210 }
1211
1212 #endif /* CONFIG_ARM64_SVE */
1213
1214 #ifdef CONFIG_ARM64_SME
1215
1216 /*
1217 * Ensure that task->thread.za_state is allocated and sufficiently large.
1218 *
1219 * This function should be used only in preparation for replacing
1220 * task->thread.za_state with new data. The memory is always zeroed
1221 * here to prevent stale data from showing through: this is done in
1222 * the interest of testability and predictability, the architecture
1223 * guarantees that when ZA is enabled it will be zeroed.
1224 */
sme_alloc(struct task_struct * task)1225 void sme_alloc(struct task_struct *task)
1226 {
1227 if (task->thread.za_state) {
1228 memset(task->thread.za_state, 0, za_state_size(task));
1229 return;
1230 }
1231
1232 /* This could potentially be up to 64K. */
1233 task->thread.za_state =
1234 kzalloc(za_state_size(task), GFP_KERNEL);
1235 }
1236
sme_free(struct task_struct * task)1237 static void sme_free(struct task_struct *task)
1238 {
1239 kfree(task->thread.za_state);
1240 task->thread.za_state = NULL;
1241 }
1242
sme_kernel_enable(const struct arm64_cpu_capabilities * __always_unused p)1243 void sme_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
1244 {
1245 /* Set priority for all PEs to architecturally defined minimum */
1246 write_sysreg_s(read_sysreg_s(SYS_SMPRI_EL1) & ~SMPRI_EL1_PRIORITY_MASK,
1247 SYS_SMPRI_EL1);
1248
1249 /* Allow SME in kernel */
1250 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_SMEN_EL1EN, CPACR_EL1);
1251 isb();
1252
1253 /* Allow EL0 to access TPIDR2 */
1254 write_sysreg(read_sysreg(SCTLR_EL1) | SCTLR_ELx_ENTP2, SCTLR_EL1);
1255 isb();
1256 }
1257
1258 /*
1259 * This must be called after sme_kernel_enable(), we rely on the
1260 * feature table being sorted to ensure this.
1261 */
fa64_kernel_enable(const struct arm64_cpu_capabilities * __always_unused p)1262 void fa64_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
1263 {
1264 /* Allow use of FA64 */
1265 write_sysreg_s(read_sysreg_s(SYS_SMCR_EL1) | SMCR_ELx_FA64_MASK,
1266 SYS_SMCR_EL1);
1267 }
1268
1269 /*
1270 * Read the pseudo-SMCR used by cpufeatures to identify the supported
1271 * vector length.
1272 *
1273 * Use only if SME is present.
1274 * This function clobbers the SME vector length.
1275 */
read_smcr_features(void)1276 u64 read_smcr_features(void)
1277 {
1278 u64 smcr;
1279 unsigned int vq_max;
1280
1281 sme_kernel_enable(NULL);
1282 sme_smstart_sm();
1283
1284 /*
1285 * Set the maximum possible VL.
1286 */
1287 write_sysreg_s(read_sysreg_s(SYS_SMCR_EL1) | SMCR_ELx_LEN_MASK,
1288 SYS_SMCR_EL1);
1289
1290 smcr = read_sysreg_s(SYS_SMCR_EL1);
1291 smcr &= ~(u64)SMCR_ELx_LEN_MASK; /* Only the LEN field */
1292 vq_max = sve_vq_from_vl(sve_get_vl());
1293 smcr |= vq_max - 1; /* set LEN field to maximum effective value */
1294
1295 sme_smstop_sm();
1296
1297 return smcr;
1298 }
1299
sme_setup(void)1300 void __init sme_setup(void)
1301 {
1302 struct vl_info *info = &vl_info[ARM64_VEC_SME];
1303 u64 smcr;
1304 int min_bit;
1305
1306 if (!system_supports_sme())
1307 return;
1308
1309 /*
1310 * SME doesn't require any particular vector length be
1311 * supported but it does require at least one. We should have
1312 * disabled the feature entirely while bringing up CPUs but
1313 * let's double check here.
1314 */
1315 WARN_ON(bitmap_empty(info->vq_map, SVE_VQ_MAX));
1316
1317 min_bit = find_last_bit(info->vq_map, SVE_VQ_MAX);
1318 info->min_vl = sve_vl_from_vq(__bit_to_vq(min_bit));
1319
1320 smcr = read_sanitised_ftr_reg(SYS_SMCR_EL1);
1321 info->max_vl = sve_vl_from_vq((smcr & SMCR_ELx_LEN_MASK) + 1);
1322
1323 /*
1324 * Sanity-check that the max VL we determined through CPU features
1325 * corresponds properly to sme_vq_map. If not, do our best:
1326 */
1327 if (WARN_ON(info->max_vl != find_supported_vector_length(ARM64_VEC_SME,
1328 info->max_vl)))
1329 info->max_vl = find_supported_vector_length(ARM64_VEC_SME,
1330 info->max_vl);
1331
1332 WARN_ON(info->min_vl > info->max_vl);
1333
1334 /*
1335 * For the default VL, pick the maximum supported value <= 32
1336 * (256 bits) if there is one since this is guaranteed not to
1337 * grow the signal frame when in streaming mode, otherwise the
1338 * minimum available VL will be used.
1339 */
1340 set_sme_default_vl(find_supported_vector_length(ARM64_VEC_SME, 32));
1341
1342 pr_info("SME: minimum available vector length %u bytes per vector\n",
1343 info->min_vl);
1344 pr_info("SME: maximum available vector length %u bytes per vector\n",
1345 info->max_vl);
1346 pr_info("SME: default vector length %u bytes per vector\n",
1347 get_sme_default_vl());
1348 }
1349
1350 #endif /* CONFIG_ARM64_SME */
1351
sve_init_regs(void)1352 static void sve_init_regs(void)
1353 {
1354 /*
1355 * Convert the FPSIMD state to SVE, zeroing all the state that
1356 * is not shared with FPSIMD. If (as is likely) the current
1357 * state is live in the registers then do this there and
1358 * update our metadata for the current task including
1359 * disabling the trap, otherwise update our in-memory copy.
1360 * We are guaranteed to not be in streaming mode, we can only
1361 * take a SVE trap when not in streaming mode and we can't be
1362 * in streaming mode when taking a SME trap.
1363 */
1364 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
1365 unsigned long vq_minus_one =
1366 sve_vq_from_vl(task_get_sve_vl(current)) - 1;
1367 sve_set_vq(vq_minus_one);
1368 sve_flush_live(true, vq_minus_one);
1369 fpsimd_bind_task_to_cpu();
1370 } else {
1371 fpsimd_to_sve(current);
1372 }
1373 }
1374
1375 /*
1376 * Trapped SVE access
1377 *
1378 * Storage is allocated for the full SVE state, the current FPSIMD
1379 * register contents are migrated across, and the access trap is
1380 * disabled.
1381 *
1382 * TIF_SVE should be clear on entry: otherwise, fpsimd_restore_current_state()
1383 * would have disabled the SVE access trap for userspace during
1384 * ret_to_user, making an SVE access trap impossible in that case.
1385 */
do_sve_acc(unsigned long esr,struct pt_regs * regs)1386 void do_sve_acc(unsigned long esr, struct pt_regs *regs)
1387 {
1388 /* Even if we chose not to use SVE, the hardware could still trap: */
1389 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
1390 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1391 return;
1392 }
1393
1394 sve_alloc(current, true);
1395 if (!current->thread.sve_state) {
1396 force_sig(SIGKILL);
1397 return;
1398 }
1399
1400 get_cpu_fpsimd_context();
1401
1402 if (test_and_set_thread_flag(TIF_SVE))
1403 WARN_ON(1); /* SVE access shouldn't have trapped */
1404
1405 /*
1406 * Even if the task can have used streaming mode we can only
1407 * generate SVE access traps in normal SVE mode and
1408 * transitioning out of streaming mode may discard any
1409 * streaming mode state. Always clear the high bits to avoid
1410 * any potential errors tracking what is properly initialised.
1411 */
1412 sve_init_regs();
1413
1414 put_cpu_fpsimd_context();
1415 }
1416
1417 /*
1418 * Trapped SME access
1419 *
1420 * Storage is allocated for the full SVE and SME state, the current
1421 * FPSIMD register contents are migrated to SVE if SVE is not already
1422 * active, and the access trap is disabled.
1423 *
1424 * TIF_SME should be clear on entry: otherwise, fpsimd_restore_current_state()
1425 * would have disabled the SME access trap for userspace during
1426 * ret_to_user, making an SVE access trap impossible in that case.
1427 */
do_sme_acc(unsigned long esr,struct pt_regs * regs)1428 void do_sme_acc(unsigned long esr, struct pt_regs *regs)
1429 {
1430 /* Even if we chose not to use SME, the hardware could still trap: */
1431 if (unlikely(!system_supports_sme()) || WARN_ON(is_compat_task())) {
1432 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1433 return;
1434 }
1435
1436 /*
1437 * If this not a trap due to SME being disabled then something
1438 * is being used in the wrong mode, report as SIGILL.
1439 */
1440 if (ESR_ELx_ISS(esr) != ESR_ELx_SME_ISS_SME_DISABLED) {
1441 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1442 return;
1443 }
1444
1445 sve_alloc(current, false);
1446 sme_alloc(current);
1447 if (!current->thread.sve_state || !current->thread.za_state) {
1448 force_sig(SIGKILL);
1449 return;
1450 }
1451
1452 get_cpu_fpsimd_context();
1453
1454 /* With TIF_SME userspace shouldn't generate any traps */
1455 if (test_and_set_thread_flag(TIF_SME))
1456 WARN_ON(1);
1457
1458 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
1459 unsigned long vq_minus_one =
1460 sve_vq_from_vl(task_get_sme_vl(current)) - 1;
1461 sme_set_vq(vq_minus_one);
1462
1463 fpsimd_bind_task_to_cpu();
1464 }
1465
1466 put_cpu_fpsimd_context();
1467 }
1468
1469 /*
1470 * Trapped FP/ASIMD access.
1471 */
do_fpsimd_acc(unsigned long esr,struct pt_regs * regs)1472 void do_fpsimd_acc(unsigned long esr, struct pt_regs *regs)
1473 {
1474 /* TODO: implement lazy context saving/restoring */
1475 WARN_ON(1);
1476 }
1477
1478 /*
1479 * Raise a SIGFPE for the current process.
1480 */
do_fpsimd_exc(unsigned long esr,struct pt_regs * regs)1481 void do_fpsimd_exc(unsigned long esr, struct pt_regs *regs)
1482 {
1483 unsigned int si_code = FPE_FLTUNK;
1484
1485 if (esr & ESR_ELx_FP_EXC_TFV) {
1486 if (esr & FPEXC_IOF)
1487 si_code = FPE_FLTINV;
1488 else if (esr & FPEXC_DZF)
1489 si_code = FPE_FLTDIV;
1490 else if (esr & FPEXC_OFF)
1491 si_code = FPE_FLTOVF;
1492 else if (esr & FPEXC_UFF)
1493 si_code = FPE_FLTUND;
1494 else if (esr & FPEXC_IXF)
1495 si_code = FPE_FLTRES;
1496 }
1497
1498 send_sig_fault(SIGFPE, si_code,
1499 (void __user *)instruction_pointer(regs),
1500 current);
1501 }
1502
fpsimd_thread_switch(struct task_struct * next)1503 void fpsimd_thread_switch(struct task_struct *next)
1504 {
1505 bool wrong_task, wrong_cpu;
1506
1507 if (!system_supports_fpsimd())
1508 return;
1509
1510 __get_cpu_fpsimd_context();
1511
1512 /* Save unsaved fpsimd state, if any: */
1513 fpsimd_save();
1514
1515 /*
1516 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
1517 * state. For kernel threads, FPSIMD registers are never loaded
1518 * and wrong_task and wrong_cpu will always be true.
1519 */
1520 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
1521 &next->thread.uw.fpsimd_state;
1522 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
1523
1524 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
1525 wrong_task || wrong_cpu);
1526
1527 __put_cpu_fpsimd_context();
1528 }
1529
fpsimd_flush_thread_vl(enum vec_type type)1530 static void fpsimd_flush_thread_vl(enum vec_type type)
1531 {
1532 int vl, supported_vl;
1533
1534 /*
1535 * Reset the task vector length as required. This is where we
1536 * ensure that all user tasks have a valid vector length
1537 * configured: no kernel task can become a user task without
1538 * an exec and hence a call to this function. By the time the
1539 * first call to this function is made, all early hardware
1540 * probing is complete, so __sve_default_vl should be valid.
1541 * If a bug causes this to go wrong, we make some noise and
1542 * try to fudge thread.sve_vl to a safe value here.
1543 */
1544 vl = task_get_vl_onexec(current, type);
1545 if (!vl)
1546 vl = get_default_vl(type);
1547
1548 if (WARN_ON(!sve_vl_valid(vl)))
1549 vl = vl_info[type].min_vl;
1550
1551 supported_vl = find_supported_vector_length(type, vl);
1552 if (WARN_ON(supported_vl != vl))
1553 vl = supported_vl;
1554
1555 task_set_vl(current, type, vl);
1556
1557 /*
1558 * If the task is not set to inherit, ensure that the vector
1559 * length will be reset by a subsequent exec:
1560 */
1561 if (!test_thread_flag(vec_vl_inherit_flag(type)))
1562 task_set_vl_onexec(current, type, 0);
1563 }
1564
fpsimd_flush_thread(void)1565 void fpsimd_flush_thread(void)
1566 {
1567 void *sve_state = NULL;
1568 void *za_state = NULL;
1569
1570 if (!system_supports_fpsimd())
1571 return;
1572
1573 get_cpu_fpsimd_context();
1574
1575 fpsimd_flush_task_state(current);
1576 memset(¤t->thread.uw.fpsimd_state, 0,
1577 sizeof(current->thread.uw.fpsimd_state));
1578
1579 if (system_supports_sve()) {
1580 clear_thread_flag(TIF_SVE);
1581
1582 /* Defer kfree() while in atomic context */
1583 sve_state = current->thread.sve_state;
1584 current->thread.sve_state = NULL;
1585
1586 fpsimd_flush_thread_vl(ARM64_VEC_SVE);
1587 }
1588
1589 if (system_supports_sme()) {
1590 clear_thread_flag(TIF_SME);
1591
1592 /* Defer kfree() while in atomic context */
1593 za_state = current->thread.za_state;
1594 current->thread.za_state = NULL;
1595
1596 fpsimd_flush_thread_vl(ARM64_VEC_SME);
1597 current->thread.svcr = 0;
1598 }
1599
1600 put_cpu_fpsimd_context();
1601 kfree(sve_state);
1602 kfree(za_state);
1603 }
1604
1605 /*
1606 * Save the userland FPSIMD state of 'current' to memory, but only if the state
1607 * currently held in the registers does in fact belong to 'current'
1608 */
fpsimd_preserve_current_state(void)1609 void fpsimd_preserve_current_state(void)
1610 {
1611 if (!system_supports_fpsimd())
1612 return;
1613
1614 get_cpu_fpsimd_context();
1615 fpsimd_save();
1616 put_cpu_fpsimd_context();
1617 }
1618
1619 /*
1620 * Like fpsimd_preserve_current_state(), but ensure that
1621 * current->thread.uw.fpsimd_state is updated so that it can be copied to
1622 * the signal frame.
1623 */
fpsimd_signal_preserve_current_state(void)1624 void fpsimd_signal_preserve_current_state(void)
1625 {
1626 fpsimd_preserve_current_state();
1627 if (test_thread_flag(TIF_SVE))
1628 sve_to_fpsimd(current);
1629 }
1630
1631 /*
1632 * Associate current's FPSIMD context with this cpu
1633 * The caller must have ownership of the cpu FPSIMD context before calling
1634 * this function.
1635 */
fpsimd_bind_task_to_cpu(void)1636 static void fpsimd_bind_task_to_cpu(void)
1637 {
1638 struct fpsimd_last_state_struct *last =
1639 this_cpu_ptr(&fpsimd_last_state);
1640
1641 WARN_ON(!system_supports_fpsimd());
1642 last->st = ¤t->thread.uw.fpsimd_state;
1643 last->sve_state = current->thread.sve_state;
1644 last->za_state = current->thread.za_state;
1645 last->sve_vl = task_get_sve_vl(current);
1646 last->sme_vl = task_get_sme_vl(current);
1647 last->svcr = ¤t->thread.svcr;
1648 current->thread.fpsimd_cpu = smp_processor_id();
1649
1650 /*
1651 * Toggle SVE and SME trapping for userspace if needed, these
1652 * are serialsied by ret_to_user().
1653 */
1654 if (system_supports_sme()) {
1655 if (test_thread_flag(TIF_SME))
1656 sme_user_enable();
1657 else
1658 sme_user_disable();
1659 }
1660
1661 if (system_supports_sve()) {
1662 if (test_thread_flag(TIF_SVE))
1663 sve_user_enable();
1664 else
1665 sve_user_disable();
1666 }
1667 }
1668
fpsimd_bind_state_to_cpu(struct user_fpsimd_state * st,void * sve_state,unsigned int sve_vl,void * za_state,unsigned int sme_vl,u64 * svcr)1669 void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1670 unsigned int sve_vl, void *za_state,
1671 unsigned int sme_vl, u64 *svcr)
1672 {
1673 struct fpsimd_last_state_struct *last =
1674 this_cpu_ptr(&fpsimd_last_state);
1675
1676 WARN_ON(!system_supports_fpsimd());
1677 WARN_ON(!in_softirq() && !irqs_disabled());
1678
1679 last->st = st;
1680 last->svcr = svcr;
1681 last->sve_state = sve_state;
1682 last->za_state = za_state;
1683 last->sve_vl = sve_vl;
1684 last->sme_vl = sme_vl;
1685 }
1686
1687 /*
1688 * Load the userland FPSIMD state of 'current' from memory, but only if the
1689 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1690 * state of 'current'. This is called when we are preparing to return to
1691 * userspace to ensure that userspace sees a good register state.
1692 */
fpsimd_restore_current_state(void)1693 void fpsimd_restore_current_state(void)
1694 {
1695 /*
1696 * For the tasks that were created before we detected the absence of
1697 * FP/SIMD, the TIF_FOREIGN_FPSTATE could be set via fpsimd_thread_switch(),
1698 * e.g, init. This could be then inherited by the children processes.
1699 * If we later detect that the system doesn't support FP/SIMD,
1700 * we must clear the flag for all the tasks to indicate that the
1701 * FPSTATE is clean (as we can't have one) to avoid looping for ever in
1702 * do_notify_resume().
1703 */
1704 if (!system_supports_fpsimd()) {
1705 clear_thread_flag(TIF_FOREIGN_FPSTATE);
1706 return;
1707 }
1708
1709 get_cpu_fpsimd_context();
1710
1711 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
1712 task_fpsimd_load();
1713 fpsimd_bind_task_to_cpu();
1714 }
1715
1716 put_cpu_fpsimd_context();
1717 }
1718
1719 /*
1720 * Load an updated userland FPSIMD state for 'current' from memory and set the
1721 * flag that indicates that the FPSIMD register contents are the most recent
1722 * FPSIMD state of 'current'. This is used by the signal code to restore the
1723 * register state when returning from a signal handler in FPSIMD only cases,
1724 * any SVE context will be discarded.
1725 */
fpsimd_update_current_state(struct user_fpsimd_state const * state)1726 void fpsimd_update_current_state(struct user_fpsimd_state const *state)
1727 {
1728 if (WARN_ON(!system_supports_fpsimd()))
1729 return;
1730
1731 get_cpu_fpsimd_context();
1732
1733 current->thread.uw.fpsimd_state = *state;
1734 if (test_thread_flag(TIF_SVE))
1735 fpsimd_to_sve(current);
1736
1737 task_fpsimd_load();
1738 fpsimd_bind_task_to_cpu();
1739
1740 clear_thread_flag(TIF_FOREIGN_FPSTATE);
1741
1742 put_cpu_fpsimd_context();
1743 }
1744
1745 /*
1746 * Invalidate live CPU copies of task t's FPSIMD state
1747 *
1748 * This function may be called with preemption enabled. The barrier()
1749 * ensures that the assignment to fpsimd_cpu is visible to any
1750 * preemption/softirq that could race with set_tsk_thread_flag(), so
1751 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1752 *
1753 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1754 * subsequent code.
1755 */
fpsimd_flush_task_state(struct task_struct * t)1756 void fpsimd_flush_task_state(struct task_struct *t)
1757 {
1758 t->thread.fpsimd_cpu = NR_CPUS;
1759 /*
1760 * If we don't support fpsimd, bail out after we have
1761 * reset the fpsimd_cpu for this task and clear the
1762 * FPSTATE.
1763 */
1764 if (!system_supports_fpsimd())
1765 return;
1766 barrier();
1767 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1768
1769 barrier();
1770 }
1771
1772 /*
1773 * Invalidate any task's FPSIMD state that is present on this cpu.
1774 * The FPSIMD context should be acquired with get_cpu_fpsimd_context()
1775 * before calling this function.
1776 */
fpsimd_flush_cpu_state(void)1777 static void fpsimd_flush_cpu_state(void)
1778 {
1779 WARN_ON(!system_supports_fpsimd());
1780 __this_cpu_write(fpsimd_last_state.st, NULL);
1781
1782 /*
1783 * Leaving streaming mode enabled will cause issues for any kernel
1784 * NEON and leaving streaming mode or ZA enabled may increase power
1785 * consumption.
1786 */
1787 if (system_supports_sme())
1788 sme_smstop();
1789
1790 set_thread_flag(TIF_FOREIGN_FPSTATE);
1791 }
1792
1793 /*
1794 * Save the FPSIMD state to memory and invalidate cpu view.
1795 * This function must be called with preemption disabled.
1796 */
fpsimd_save_and_flush_cpu_state(void)1797 void fpsimd_save_and_flush_cpu_state(void)
1798 {
1799 if (!system_supports_fpsimd())
1800 return;
1801 WARN_ON(preemptible());
1802 __get_cpu_fpsimd_context();
1803 fpsimd_save();
1804 fpsimd_flush_cpu_state();
1805 __put_cpu_fpsimd_context();
1806 }
1807
1808 #ifdef CONFIG_KERNEL_MODE_NEON
1809
1810 /*
1811 * Kernel-side NEON support functions
1812 */
1813
1814 /*
1815 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1816 * context
1817 *
1818 * Must not be called unless may_use_simd() returns true.
1819 * Task context in the FPSIMD registers is saved back to memory as necessary.
1820 *
1821 * A matching call to kernel_neon_end() must be made before returning from the
1822 * calling context.
1823 *
1824 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1825 * called.
1826 */
kernel_neon_begin(void)1827 void kernel_neon_begin(void)
1828 {
1829 if (WARN_ON(!system_supports_fpsimd()))
1830 return;
1831
1832 BUG_ON(!may_use_simd());
1833
1834 get_cpu_fpsimd_context();
1835
1836 /* Save unsaved fpsimd state, if any: */
1837 fpsimd_save();
1838
1839 /* Invalidate any task state remaining in the fpsimd regs: */
1840 fpsimd_flush_cpu_state();
1841 }
1842 EXPORT_SYMBOL(kernel_neon_begin);
1843
1844 /*
1845 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1846 *
1847 * Must be called from a context in which kernel_neon_begin() was previously
1848 * called, with no call to kernel_neon_end() in the meantime.
1849 *
1850 * The caller must not use the FPSIMD registers after this function is called,
1851 * unless kernel_neon_begin() is called again in the meantime.
1852 */
kernel_neon_end(void)1853 void kernel_neon_end(void)
1854 {
1855 if (!system_supports_fpsimd())
1856 return;
1857
1858 put_cpu_fpsimd_context();
1859 }
1860 EXPORT_SYMBOL(kernel_neon_end);
1861
1862 #ifdef CONFIG_EFI
1863
1864 static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
1865 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
1866 static DEFINE_PER_CPU(bool, efi_sve_state_used);
1867 static DEFINE_PER_CPU(bool, efi_sm_state);
1868
1869 /*
1870 * EFI runtime services support functions
1871 *
1872 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1873 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1874 * is always used rather than being an optional accelerator.
1875 *
1876 * These functions provide the necessary support for ensuring FPSIMD
1877 * save/restore in the contexts from which EFI is used.
1878 *
1879 * Do not use them for any other purpose -- if tempted to do so, you are
1880 * either doing something wrong or you need to propose some refactoring.
1881 */
1882
1883 /*
1884 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1885 */
__efi_fpsimd_begin(void)1886 void __efi_fpsimd_begin(void)
1887 {
1888 if (!system_supports_fpsimd())
1889 return;
1890
1891 WARN_ON(preemptible());
1892
1893 if (may_use_simd()) {
1894 kernel_neon_begin();
1895 } else {
1896 /*
1897 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1898 * preserving:
1899 */
1900 if (system_supports_sve() && likely(efi_sve_state)) {
1901 char *sve_state = this_cpu_ptr(efi_sve_state);
1902 bool ffr = true;
1903 u64 svcr;
1904
1905 __this_cpu_write(efi_sve_state_used, true);
1906
1907 if (system_supports_sme()) {
1908 svcr = read_sysreg_s(SYS_SVCR);
1909
1910 __this_cpu_write(efi_sm_state,
1911 svcr & SVCR_SM_MASK);
1912
1913 /*
1914 * Unless we have FA64 FFR does not
1915 * exist in streaming mode.
1916 */
1917 if (!system_supports_fa64())
1918 ffr = !(svcr & SVCR_SM_MASK);
1919 }
1920
1921 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl()),
1922 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1923 ffr);
1924
1925 if (system_supports_sme())
1926 sysreg_clear_set_s(SYS_SVCR,
1927 SVCR_SM_MASK, 0);
1928
1929 } else {
1930 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1931 }
1932
1933 __this_cpu_write(efi_fpsimd_state_used, true);
1934 }
1935 }
1936
1937 /*
1938 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1939 */
__efi_fpsimd_end(void)1940 void __efi_fpsimd_end(void)
1941 {
1942 if (!system_supports_fpsimd())
1943 return;
1944
1945 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
1946 kernel_neon_end();
1947 } else {
1948 if (system_supports_sve() &&
1949 likely(__this_cpu_read(efi_sve_state_used))) {
1950 char const *sve_state = this_cpu_ptr(efi_sve_state);
1951 bool ffr = true;
1952
1953 /*
1954 * Restore streaming mode; EFI calls are
1955 * normal function calls so should not return in
1956 * streaming mode.
1957 */
1958 if (system_supports_sme()) {
1959 if (__this_cpu_read(efi_sm_state)) {
1960 sysreg_clear_set_s(SYS_SVCR,
1961 0,
1962 SVCR_SM_MASK);
1963
1964 /*
1965 * Unless we have FA64 FFR does not
1966 * exist in streaming mode.
1967 */
1968 if (!system_supports_fa64())
1969 ffr = false;
1970 }
1971 }
1972
1973 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl()),
1974 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1975 ffr);
1976
1977 __this_cpu_write(efi_sve_state_used, false);
1978 } else {
1979 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1980 }
1981 }
1982 }
1983
1984 #endif /* CONFIG_EFI */
1985
1986 #endif /* CONFIG_KERNEL_MODE_NEON */
1987
1988 #ifdef CONFIG_CPU_PM
fpsimd_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)1989 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1990 unsigned long cmd, void *v)
1991 {
1992 switch (cmd) {
1993 case CPU_PM_ENTER:
1994 fpsimd_save_and_flush_cpu_state();
1995 break;
1996 case CPU_PM_EXIT:
1997 break;
1998 case CPU_PM_ENTER_FAILED:
1999 default:
2000 return NOTIFY_DONE;
2001 }
2002 return NOTIFY_OK;
2003 }
2004
2005 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
2006 .notifier_call = fpsimd_cpu_pm_notifier,
2007 };
2008
fpsimd_pm_init(void)2009 static void __init fpsimd_pm_init(void)
2010 {
2011 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
2012 }
2013
2014 #else
fpsimd_pm_init(void)2015 static inline void fpsimd_pm_init(void) { }
2016 #endif /* CONFIG_CPU_PM */
2017
2018 #ifdef CONFIG_HOTPLUG_CPU
fpsimd_cpu_dead(unsigned int cpu)2019 static int fpsimd_cpu_dead(unsigned int cpu)
2020 {
2021 per_cpu(fpsimd_last_state.st, cpu) = NULL;
2022 return 0;
2023 }
2024
fpsimd_hotplug_init(void)2025 static inline void fpsimd_hotplug_init(void)
2026 {
2027 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
2028 NULL, fpsimd_cpu_dead);
2029 }
2030
2031 #else
fpsimd_hotplug_init(void)2032 static inline void fpsimd_hotplug_init(void) { }
2033 #endif
2034
2035 /*
2036 * FP/SIMD support code initialisation.
2037 */
fpsimd_init(void)2038 static int __init fpsimd_init(void)
2039 {
2040 if (cpu_have_named_feature(FP)) {
2041 fpsimd_pm_init();
2042 fpsimd_hotplug_init();
2043 } else {
2044 pr_notice("Floating-point is not implemented\n");
2045 }
2046
2047 if (!cpu_have_named_feature(ASIMD))
2048 pr_notice("Advanced SIMD is not implemented\n");
2049
2050
2051 if (cpu_have_named_feature(SME) && !cpu_have_named_feature(SVE))
2052 pr_notice("SME is implemented but not SVE\n");
2053
2054 sve_sysctl_init();
2055 sme_sysctl_init();
2056
2057 return 0;
2058 }
2059 core_initcall(fpsimd_init);
2060