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