staging: erofs: fix warning Comparison to bool
[linux/fpc-iii.git] / arch / arm64 / kernel / fpsimd.c
bloba38bf74bcca8c5c1732cf161718b497e59160853
1 /*
2 * FP/SIMD context switching and fault handling
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/bitmap.h>
21 #include <linux/bitops.h>
22 #include <linux/bottom_half.h>
23 #include <linux/bug.h>
24 #include <linux/cache.h>
25 #include <linux/compat.h>
26 #include <linux/cpu.h>
27 #include <linux/cpu_pm.h>
28 #include <linux/kernel.h>
29 #include <linux/linkage.h>
30 #include <linux/irqflags.h>
31 #include <linux/init.h>
32 #include <linux/percpu.h>
33 #include <linux/prctl.h>
34 #include <linux/preempt.h>
35 #include <linux/ptrace.h>
36 #include <linux/sched/signal.h>
37 #include <linux/sched/task_stack.h>
38 #include <linux/signal.h>
39 #include <linux/slab.h>
40 #include <linux/stddef.h>
41 #include <linux/sysctl.h>
43 #include <asm/esr.h>
44 #include <asm/fpsimd.h>
45 #include <asm/cpufeature.h>
46 #include <asm/cputype.h>
47 #include <asm/processor.h>
48 #include <asm/simd.h>
49 #include <asm/sigcontext.h>
50 #include <asm/sysreg.h>
51 #include <asm/traps.h>
52 #include <asm/virt.h>
54 #define FPEXC_IOF (1 << 0)
55 #define FPEXC_DZF (1 << 1)
56 #define FPEXC_OFF (1 << 2)
57 #define FPEXC_UFF (1 << 3)
58 #define FPEXC_IXF (1 << 4)
59 #define FPEXC_IDF (1 << 7)
62 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
64 * In order to reduce the number of times the FPSIMD state is needlessly saved
65 * and restored, we need to keep track of two things:
66 * (a) for each task, we need to remember which CPU was the last one to have
67 * the task's FPSIMD state loaded into its FPSIMD registers;
68 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
69 * been loaded into its FPSIMD registers most recently, or whether it has
70 * been used to perform kernel mode NEON in the meantime.
72 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
73 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
74 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
75 * address of the userland FPSIMD state of the task that was loaded onto the CPU
76 * the most recently, or NULL if kernel mode NEON has been performed after that.
78 * With this in place, we no longer have to restore the next FPSIMD state right
79 * when switching between tasks. Instead, we can defer this check to userland
80 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
81 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
82 * can omit the FPSIMD restore.
84 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
85 * indicate whether or not the userland FPSIMD state of the current task is
86 * present in the registers. The flag is set unless the FPSIMD registers of this
87 * CPU currently contain the most recent userland FPSIMD state of the current
88 * task.
90 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
91 * save the task's FPSIMD context back to task_struct from softirq context.
92 * To prevent this from racing with the manipulation of the task's FPSIMD state
93 * from task context and thereby corrupting the state, it is necessary to
94 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
95 * flag with local_bh_disable() unless softirqs are already masked.
97 * For a certain task, the sequence may look something like this:
98 * - the task gets scheduled in; if both the task's fpsimd_cpu field
99 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
100 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
101 * cleared, otherwise it is set;
103 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
104 * userland FPSIMD state is copied from memory to the registers, the task's
105 * fpsimd_cpu field is set to the id of the current CPU, the current
106 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
107 * TIF_FOREIGN_FPSTATE flag is cleared;
109 * - the task executes an ordinary syscall; upon return to userland, the
110 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
111 * restored;
113 * - the task executes a syscall which executes some NEON instructions; this is
114 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
115 * register contents to memory, clears the fpsimd_last_state per-cpu variable
116 * and sets the TIF_FOREIGN_FPSTATE flag;
118 * - the task gets preempted after kernel_neon_end() is called; as we have not
119 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
120 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
122 struct fpsimd_last_state_struct {
123 struct user_fpsimd_state *st;
124 void *sve_state;
125 unsigned int sve_vl;
128 static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
130 /* Default VL for tasks that don't set it explicitly: */
131 static int sve_default_vl = -1;
133 #ifdef CONFIG_ARM64_SVE
135 /* Maximum supported vector length across all CPUs (initially poisoned) */
136 int __ro_after_init sve_max_vl = SVE_VL_MIN;
137 int __ro_after_init sve_max_virtualisable_vl = SVE_VL_MIN;
140 * Set of available vector lengths,
141 * where length vq encoded as bit __vq_to_bit(vq):
143 __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
144 /* Set of vector lengths present on at least one cpu: */
145 static __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
147 static void __percpu *efi_sve_state;
149 #else /* ! CONFIG_ARM64_SVE */
151 /* Dummy declaration for code that will be optimised out: */
152 extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
153 extern __ro_after_init DECLARE_BITMAP(sve_vq_partial_map, SVE_VQ_MAX);
154 extern void __percpu *efi_sve_state;
156 #endif /* ! CONFIG_ARM64_SVE */
159 * Call __sve_free() directly only if you know task can't be scheduled
160 * or preempted.
162 static void __sve_free(struct task_struct *task)
164 kfree(task->thread.sve_state);
165 task->thread.sve_state = NULL;
168 static void sve_free(struct task_struct *task)
170 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
172 __sve_free(task);
176 * TIF_SVE controls whether a task can use SVE without trapping while
177 * in userspace, and also the way a task's FPSIMD/SVE state is stored
178 * in thread_struct.
180 * The kernel uses this flag to track whether a user task is actively
181 * using SVE, and therefore whether full SVE register state needs to
182 * be tracked. If not, the cheaper FPSIMD context handling code can
183 * be used instead of the more costly SVE equivalents.
185 * * TIF_SVE set:
187 * The task can execute SVE instructions while in userspace without
188 * trapping to the kernel.
190 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
191 * corresponding Zn), P0-P15 and FFR are encoded in in
192 * task->thread.sve_state, formatted appropriately for vector
193 * length task->thread.sve_vl.
195 * task->thread.sve_state must point to a valid buffer at least
196 * sve_state_size(task) bytes in size.
198 * During any syscall, the kernel may optionally clear TIF_SVE and
199 * discard the vector state except for the FPSIMD subset.
201 * * TIF_SVE clear:
203 * An attempt by the user task to execute an SVE instruction causes
204 * do_sve_acc() to be called, which does some preparation and then
205 * sets TIF_SVE.
207 * When stored, FPSIMD registers V0-V31 are encoded in
208 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
209 * logically zero but not stored anywhere; P0-P15 and FFR are not
210 * stored and have unspecified values from userspace's point of
211 * view. For hygiene purposes, the kernel zeroes them on next use,
212 * but userspace is discouraged from relying on this.
214 * task->thread.sve_state does not need to be non-NULL, valid or any
215 * particular size: it must not be dereferenced.
217 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
218 * irrespective of whether TIF_SVE is clear or set, since these are
219 * not vector length dependent.
223 * Update current's FPSIMD/SVE registers from thread_struct.
225 * This function should be called only when the FPSIMD/SVE state in
226 * thread_struct is known to be up to date, when preparing to enter
227 * userspace.
229 * Softirqs (and preemption) must be disabled.
231 static void task_fpsimd_load(void)
233 WARN_ON(!in_softirq() && !irqs_disabled());
235 if (system_supports_sve() && test_thread_flag(TIF_SVE))
236 sve_load_state(sve_pffr(&current->thread),
237 &current->thread.uw.fpsimd_state.fpsr,
238 sve_vq_from_vl(current->thread.sve_vl) - 1);
239 else
240 fpsimd_load_state(&current->thread.uw.fpsimd_state);
244 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
245 * date with respect to the CPU registers.
247 * Softirqs (and preemption) must be disabled.
249 void fpsimd_save(void)
251 struct fpsimd_last_state_struct const *last =
252 this_cpu_ptr(&fpsimd_last_state);
253 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
255 WARN_ON(!in_softirq() && !irqs_disabled());
257 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
258 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
259 if (WARN_ON(sve_get_vl() != last->sve_vl)) {
261 * Can't save the user regs, so current would
262 * re-enter user with corrupt state.
263 * There's no way to recover, so kill it:
265 force_signal_inject(SIGKILL, SI_KERNEL, 0);
266 return;
269 sve_save_state((char *)last->sve_state +
270 sve_ffr_offset(last->sve_vl),
271 &last->st->fpsr);
272 } else
273 fpsimd_save_state(last->st);
278 * All vector length selection from userspace comes through here.
279 * We're on a slow path, so some sanity-checks are included.
280 * If things go wrong there's a bug somewhere, but try to fall back to a
281 * safe choice.
283 static unsigned int find_supported_vector_length(unsigned int vl)
285 int bit;
286 int max_vl = sve_max_vl;
288 if (WARN_ON(!sve_vl_valid(vl)))
289 vl = SVE_VL_MIN;
291 if (WARN_ON(!sve_vl_valid(max_vl)))
292 max_vl = SVE_VL_MIN;
294 if (vl > max_vl)
295 vl = max_vl;
297 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
298 __vq_to_bit(sve_vq_from_vl(vl)));
299 return sve_vl_from_vq(__bit_to_vq(bit));
302 #ifdef CONFIG_SYSCTL
304 static int sve_proc_do_default_vl(struct ctl_table *table, int write,
305 void __user *buffer, size_t *lenp,
306 loff_t *ppos)
308 int ret;
309 int vl = sve_default_vl;
310 struct ctl_table tmp_table = {
311 .data = &vl,
312 .maxlen = sizeof(vl),
315 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
316 if (ret || !write)
317 return ret;
319 /* Writing -1 has the special meaning "set to max": */
320 if (vl == -1)
321 vl = sve_max_vl;
323 if (!sve_vl_valid(vl))
324 return -EINVAL;
326 sve_default_vl = find_supported_vector_length(vl);
327 return 0;
330 static struct ctl_table sve_default_vl_table[] = {
332 .procname = "sve_default_vector_length",
333 .mode = 0644,
334 .proc_handler = sve_proc_do_default_vl,
339 static int __init sve_sysctl_init(void)
341 if (system_supports_sve())
342 if (!register_sysctl("abi", sve_default_vl_table))
343 return -EINVAL;
345 return 0;
348 #else /* ! CONFIG_SYSCTL */
349 static int __init sve_sysctl_init(void) { return 0; }
350 #endif /* ! CONFIG_SYSCTL */
352 #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
353 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
356 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
357 * task->thread.sve_state.
359 * Task can be a non-runnable task, or current. In the latter case,
360 * softirqs (and preemption) must be disabled.
361 * task->thread.sve_state must point to at least sve_state_size(task)
362 * bytes of allocated kernel memory.
363 * task->thread.uw.fpsimd_state must be up to date before calling this
364 * function.
366 static void fpsimd_to_sve(struct task_struct *task)
368 unsigned int vq;
369 void *sst = task->thread.sve_state;
370 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
371 unsigned int i;
373 if (!system_supports_sve())
374 return;
376 vq = sve_vq_from_vl(task->thread.sve_vl);
377 for (i = 0; i < 32; ++i)
378 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
379 sizeof(fst->vregs[i]));
383 * Transfer the SVE state in task->thread.sve_state to
384 * task->thread.uw.fpsimd_state.
386 * Task can be a non-runnable task, or current. In the latter case,
387 * softirqs (and preemption) must be disabled.
388 * task->thread.sve_state must point to at least sve_state_size(task)
389 * bytes of allocated kernel memory.
390 * task->thread.sve_state must be up to date before calling this function.
392 static void sve_to_fpsimd(struct task_struct *task)
394 unsigned int vq;
395 void const *sst = task->thread.sve_state;
396 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
397 unsigned int i;
399 if (!system_supports_sve())
400 return;
402 vq = sve_vq_from_vl(task->thread.sve_vl);
403 for (i = 0; i < 32; ++i)
404 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
405 sizeof(fst->vregs[i]));
408 #ifdef CONFIG_ARM64_SVE
411 * Return how many bytes of memory are required to store the full SVE
412 * state for task, given task's currently configured vector length.
414 size_t sve_state_size(struct task_struct const *task)
416 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
420 * Ensure that task->thread.sve_state is allocated and sufficiently large.
422 * This function should be used only in preparation for replacing
423 * task->thread.sve_state with new data. The memory is always zeroed
424 * here to prevent stale data from showing through: this is done in
425 * the interest of testability and predictability: except in the
426 * do_sve_acc() case, there is no ABI requirement to hide stale data
427 * written previously be task.
429 void sve_alloc(struct task_struct *task)
431 if (task->thread.sve_state) {
432 memset(task->thread.sve_state, 0, sve_state_size(current));
433 return;
436 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
437 task->thread.sve_state =
438 kzalloc(sve_state_size(task), GFP_KERNEL);
441 * If future SVE revisions can have larger vectors though,
442 * this may cease to be true:
444 BUG_ON(!task->thread.sve_state);
449 * Ensure that task->thread.sve_state is up to date with respect to
450 * the user task, irrespective of when SVE is in use or not.
452 * This should only be called by ptrace. task must be non-runnable.
453 * task->thread.sve_state must point to at least sve_state_size(task)
454 * bytes of allocated kernel memory.
456 void fpsimd_sync_to_sve(struct task_struct *task)
458 if (!test_tsk_thread_flag(task, TIF_SVE))
459 fpsimd_to_sve(task);
463 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
464 * the user task, irrespective of whether SVE is in use or not.
466 * This should only be called by ptrace. task must be non-runnable.
467 * task->thread.sve_state must point to at least sve_state_size(task)
468 * bytes of allocated kernel memory.
470 void sve_sync_to_fpsimd(struct task_struct *task)
472 if (test_tsk_thread_flag(task, TIF_SVE))
473 sve_to_fpsimd(task);
477 * Ensure that task->thread.sve_state is up to date with respect to
478 * the task->thread.uw.fpsimd_state.
480 * This should only be called by ptrace to merge new FPSIMD register
481 * values into a task for which SVE is currently active.
482 * task must be non-runnable.
483 * task->thread.sve_state must point to at least sve_state_size(task)
484 * bytes of allocated kernel memory.
485 * task->thread.uw.fpsimd_state must already have been initialised with
486 * the new FPSIMD register values to be merged in.
488 void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
490 unsigned int vq;
491 void *sst = task->thread.sve_state;
492 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
493 unsigned int i;
495 if (!test_tsk_thread_flag(task, TIF_SVE))
496 return;
498 vq = sve_vq_from_vl(task->thread.sve_vl);
500 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
502 for (i = 0; i < 32; ++i)
503 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
504 sizeof(fst->vregs[i]));
507 int sve_set_vector_length(struct task_struct *task,
508 unsigned long vl, unsigned long flags)
510 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
511 PR_SVE_SET_VL_ONEXEC))
512 return -EINVAL;
514 if (!sve_vl_valid(vl))
515 return -EINVAL;
518 * Clamp to the maximum vector length that VL-agnostic SVE code can
519 * work with. A flag may be assigned in the future to allow setting
520 * of larger vector lengths without confusing older software.
522 if (vl > SVE_VL_ARCH_MAX)
523 vl = SVE_VL_ARCH_MAX;
525 vl = find_supported_vector_length(vl);
527 if (flags & (PR_SVE_VL_INHERIT |
528 PR_SVE_SET_VL_ONEXEC))
529 task->thread.sve_vl_onexec = vl;
530 else
531 /* Reset VL to system default on next exec: */
532 task->thread.sve_vl_onexec = 0;
534 /* Only actually set the VL if not deferred: */
535 if (flags & PR_SVE_SET_VL_ONEXEC)
536 goto out;
538 if (vl == task->thread.sve_vl)
539 goto out;
542 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
543 * write any live register state back to task_struct, and convert to a
544 * non-SVE thread.
546 if (task == current) {
547 local_bh_disable();
549 fpsimd_save();
552 fpsimd_flush_task_state(task);
553 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
554 sve_to_fpsimd(task);
556 if (task == current)
557 local_bh_enable();
560 * Force reallocation of task SVE state to the correct size
561 * on next use:
563 sve_free(task);
565 task->thread.sve_vl = vl;
567 out:
568 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
569 flags & PR_SVE_VL_INHERIT);
571 return 0;
575 * Encode the current vector length and flags for return.
576 * This is only required for prctl(): ptrace has separate fields
578 * flags are as for sve_set_vector_length().
580 static int sve_prctl_status(unsigned long flags)
582 int ret;
584 if (flags & PR_SVE_SET_VL_ONEXEC)
585 ret = current->thread.sve_vl_onexec;
586 else
587 ret = current->thread.sve_vl;
589 if (test_thread_flag(TIF_SVE_VL_INHERIT))
590 ret |= PR_SVE_VL_INHERIT;
592 return ret;
595 /* PR_SVE_SET_VL */
596 int sve_set_current_vl(unsigned long arg)
598 unsigned long vl, flags;
599 int ret;
601 vl = arg & PR_SVE_VL_LEN_MASK;
602 flags = arg & ~vl;
604 if (!system_supports_sve())
605 return -EINVAL;
607 ret = sve_set_vector_length(current, vl, flags);
608 if (ret)
609 return ret;
611 return sve_prctl_status(flags);
614 /* PR_SVE_GET_VL */
615 int sve_get_current_vl(void)
617 if (!system_supports_sve())
618 return -EINVAL;
620 return sve_prctl_status(0);
623 static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
625 unsigned int vq, vl;
626 unsigned long zcr;
628 bitmap_zero(map, SVE_VQ_MAX);
630 zcr = ZCR_ELx_LEN_MASK;
631 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
633 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
634 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
635 vl = sve_get_vl();
636 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
637 set_bit(__vq_to_bit(vq), map);
642 * Initialise the set of known supported VQs for the boot CPU.
643 * This is called during kernel boot, before secondary CPUs are brought up.
645 void __init sve_init_vq_map(void)
647 sve_probe_vqs(sve_vq_map);
648 bitmap_copy(sve_vq_partial_map, sve_vq_map, SVE_VQ_MAX);
652 * If we haven't committed to the set of supported VQs yet, filter out
653 * those not supported by the current CPU.
654 * This function is called during the bring-up of early secondary CPUs only.
656 void sve_update_vq_map(void)
658 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
660 sve_probe_vqs(tmp_map);
661 bitmap_and(sve_vq_map, sve_vq_map, tmp_map, SVE_VQ_MAX);
662 bitmap_or(sve_vq_partial_map, sve_vq_partial_map, tmp_map, SVE_VQ_MAX);
666 * Check whether the current CPU supports all VQs in the committed set.
667 * This function is called during the bring-up of late secondary CPUs only.
669 int sve_verify_vq_map(void)
671 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
672 unsigned long b;
674 sve_probe_vqs(tmp_map);
676 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
677 if (bitmap_intersects(tmp_map, sve_vq_map, SVE_VQ_MAX)) {
678 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
679 smp_processor_id());
680 return -EINVAL;
683 if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
684 return 0;
687 * For KVM, it is necessary to ensure that this CPU doesn't
688 * support any vector length that guests may have probed as
689 * unsupported.
692 /* Recover the set of supported VQs: */
693 bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
694 /* Find VQs supported that are not globally supported: */
695 bitmap_andnot(tmp_map, tmp_map, sve_vq_map, SVE_VQ_MAX);
697 /* Find the lowest such VQ, if any: */
698 b = find_last_bit(tmp_map, SVE_VQ_MAX);
699 if (b >= SVE_VQ_MAX)
700 return 0; /* no mismatches */
703 * Mismatches above sve_max_virtualisable_vl are fine, since
704 * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
706 if (sve_vl_from_vq(__bit_to_vq(b)) <= sve_max_virtualisable_vl) {
707 pr_warn("SVE: cpu%d: Unsupported vector length(s) present\n",
708 smp_processor_id());
709 return -EINVAL;
712 return 0;
715 static void __init sve_efi_setup(void)
717 if (!IS_ENABLED(CONFIG_EFI))
718 return;
721 * alloc_percpu() warns and prints a backtrace if this goes wrong.
722 * This is evidence of a crippled system and we are returning void,
723 * so no attempt is made to handle this situation here.
725 if (!sve_vl_valid(sve_max_vl))
726 goto fail;
728 efi_sve_state = __alloc_percpu(
729 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
730 if (!efi_sve_state)
731 goto fail;
733 return;
735 fail:
736 panic("Cannot allocate percpu memory for EFI SVE save/restore");
740 * Enable SVE for EL1.
741 * Intended for use by the cpufeatures code during CPU boot.
743 void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
745 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
746 isb();
750 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
751 * vector length.
753 * Use only if SVE is present.
754 * This function clobbers the SVE vector length.
756 u64 read_zcr_features(void)
758 u64 zcr;
759 unsigned int vq_max;
762 * Set the maximum possible VL, and write zeroes to all other
763 * bits to see if they stick.
765 sve_kernel_enable(NULL);
766 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
768 zcr = read_sysreg_s(SYS_ZCR_EL1);
769 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
770 vq_max = sve_vq_from_vl(sve_get_vl());
771 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
773 return zcr;
776 void __init sve_setup(void)
778 u64 zcr;
779 DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
780 unsigned long b;
782 if (!system_supports_sve())
783 return;
786 * The SVE architecture mandates support for 128-bit vectors,
787 * so sve_vq_map must have at least SVE_VQ_MIN set.
788 * If something went wrong, at least try to patch it up:
790 if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
791 set_bit(__vq_to_bit(SVE_VQ_MIN), sve_vq_map);
793 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
794 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
797 * Sanity-check that the max VL we determined through CPU features
798 * corresponds properly to sve_vq_map. If not, do our best:
800 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
801 sve_max_vl = find_supported_vector_length(sve_max_vl);
804 * For the default VL, pick the maximum supported value <= 64.
805 * VL == 64 is guaranteed not to grow the signal frame.
807 sve_default_vl = find_supported_vector_length(64);
809 bitmap_andnot(tmp_map, sve_vq_partial_map, sve_vq_map,
810 SVE_VQ_MAX);
812 b = find_last_bit(tmp_map, SVE_VQ_MAX);
813 if (b >= SVE_VQ_MAX)
814 /* No non-virtualisable VLs found */
815 sve_max_virtualisable_vl = SVE_VQ_MAX;
816 else if (WARN_ON(b == SVE_VQ_MAX - 1))
817 /* No virtualisable VLs? This is architecturally forbidden. */
818 sve_max_virtualisable_vl = SVE_VQ_MIN;
819 else /* b + 1 < SVE_VQ_MAX */
820 sve_max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
822 if (sve_max_virtualisable_vl > sve_max_vl)
823 sve_max_virtualisable_vl = sve_max_vl;
825 pr_info("SVE: maximum available vector length %u bytes per vector\n",
826 sve_max_vl);
827 pr_info("SVE: default vector length %u bytes per vector\n",
828 sve_default_vl);
830 /* KVM decides whether to support mismatched systems. Just warn here: */
831 if (sve_max_virtualisable_vl < sve_max_vl)
832 pr_warn("SVE: unvirtualisable vector lengths present\n");
834 sve_efi_setup();
838 * Called from the put_task_struct() path, which cannot get here
839 * unless dead_task is really dead and not schedulable.
841 void fpsimd_release_task(struct task_struct *dead_task)
843 __sve_free(dead_task);
846 #endif /* CONFIG_ARM64_SVE */
849 * Trapped SVE access
851 * Storage is allocated for the full SVE state, the current FPSIMD
852 * register contents are migrated across, and TIF_SVE is set so that
853 * the SVE access trap will be disabled the next time this task
854 * reaches ret_to_user.
856 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
857 * would have disabled the SVE access trap for userspace during
858 * ret_to_user, making an SVE access trap impossible in that case.
860 asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
862 /* Even if we chose not to use SVE, the hardware could still trap: */
863 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
864 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
865 return;
868 sve_alloc(current);
870 local_bh_disable();
872 fpsimd_save();
874 /* Force ret_to_user to reload the registers: */
875 fpsimd_flush_task_state(current);
877 fpsimd_to_sve(current);
878 if (test_and_set_thread_flag(TIF_SVE))
879 WARN_ON(1); /* SVE access shouldn't have trapped */
881 local_bh_enable();
885 * Trapped FP/ASIMD access.
887 asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
889 /* TODO: implement lazy context saving/restoring */
890 WARN_ON(1);
894 * Raise a SIGFPE for the current process.
896 asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
898 unsigned int si_code = FPE_FLTUNK;
900 if (esr & ESR_ELx_FP_EXC_TFV) {
901 if (esr & FPEXC_IOF)
902 si_code = FPE_FLTINV;
903 else if (esr & FPEXC_DZF)
904 si_code = FPE_FLTDIV;
905 else if (esr & FPEXC_OFF)
906 si_code = FPE_FLTOVF;
907 else if (esr & FPEXC_UFF)
908 si_code = FPE_FLTUND;
909 else if (esr & FPEXC_IXF)
910 si_code = FPE_FLTRES;
913 send_sig_fault(SIGFPE, si_code,
914 (void __user *)instruction_pointer(regs),
915 current);
918 void fpsimd_thread_switch(struct task_struct *next)
920 bool wrong_task, wrong_cpu;
922 if (!system_supports_fpsimd())
923 return;
925 /* Save unsaved fpsimd state, if any: */
926 fpsimd_save();
929 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
930 * state. For kernel threads, FPSIMD registers are never loaded
931 * and wrong_task and wrong_cpu will always be true.
933 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
934 &next->thread.uw.fpsimd_state;
935 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
937 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
938 wrong_task || wrong_cpu);
941 void fpsimd_flush_thread(void)
943 int vl, supported_vl;
945 if (!system_supports_fpsimd())
946 return;
948 local_bh_disable();
950 fpsimd_flush_task_state(current);
951 memset(&current->thread.uw.fpsimd_state, 0,
952 sizeof(current->thread.uw.fpsimd_state));
954 if (system_supports_sve()) {
955 clear_thread_flag(TIF_SVE);
956 sve_free(current);
959 * Reset the task vector length as required.
960 * This is where we ensure that all user tasks have a valid
961 * vector length configured: no kernel task can become a user
962 * task without an exec and hence a call to this function.
963 * By the time the first call to this function is made, all
964 * early hardware probing is complete, so sve_default_vl
965 * should be valid.
966 * If a bug causes this to go wrong, we make some noise and
967 * try to fudge thread.sve_vl to a safe value here.
969 vl = current->thread.sve_vl_onexec ?
970 current->thread.sve_vl_onexec : sve_default_vl;
972 if (WARN_ON(!sve_vl_valid(vl)))
973 vl = SVE_VL_MIN;
975 supported_vl = find_supported_vector_length(vl);
976 if (WARN_ON(supported_vl != vl))
977 vl = supported_vl;
979 current->thread.sve_vl = vl;
982 * If the task is not set to inherit, ensure that the vector
983 * length will be reset by a subsequent exec:
985 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
986 current->thread.sve_vl_onexec = 0;
989 local_bh_enable();
993 * Save the userland FPSIMD state of 'current' to memory, but only if the state
994 * currently held in the registers does in fact belong to 'current'
996 void fpsimd_preserve_current_state(void)
998 if (!system_supports_fpsimd())
999 return;
1001 local_bh_disable();
1002 fpsimd_save();
1003 local_bh_enable();
1007 * Like fpsimd_preserve_current_state(), but ensure that
1008 * current->thread.uw.fpsimd_state is updated so that it can be copied to
1009 * the signal frame.
1011 void fpsimd_signal_preserve_current_state(void)
1013 fpsimd_preserve_current_state();
1014 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1015 sve_to_fpsimd(current);
1019 * Associate current's FPSIMD context with this cpu
1020 * Preemption must be disabled when calling this function.
1022 void fpsimd_bind_task_to_cpu(void)
1024 struct fpsimd_last_state_struct *last =
1025 this_cpu_ptr(&fpsimd_last_state);
1027 last->st = &current->thread.uw.fpsimd_state;
1028 last->sve_state = current->thread.sve_state;
1029 last->sve_vl = current->thread.sve_vl;
1030 current->thread.fpsimd_cpu = smp_processor_id();
1032 if (system_supports_sve()) {
1033 /* Toggle SVE trapping for userspace if needed */
1034 if (test_thread_flag(TIF_SVE))
1035 sve_user_enable();
1036 else
1037 sve_user_disable();
1039 /* Serialised by exception return to user */
1043 void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1044 unsigned int sve_vl)
1046 struct fpsimd_last_state_struct *last =
1047 this_cpu_ptr(&fpsimd_last_state);
1049 WARN_ON(!in_softirq() && !irqs_disabled());
1051 last->st = st;
1052 last->sve_state = sve_state;
1053 last->sve_vl = sve_vl;
1057 * Load the userland FPSIMD state of 'current' from memory, but only if the
1058 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1059 * state of 'current'
1061 void fpsimd_restore_current_state(void)
1063 if (!system_supports_fpsimd())
1064 return;
1066 local_bh_disable();
1068 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
1069 task_fpsimd_load();
1070 fpsimd_bind_task_to_cpu();
1073 local_bh_enable();
1077 * Load an updated userland FPSIMD state for 'current' from memory and set the
1078 * flag that indicates that the FPSIMD register contents are the most recent
1079 * FPSIMD state of 'current'
1081 void fpsimd_update_current_state(struct user_fpsimd_state const *state)
1083 if (!system_supports_fpsimd())
1084 return;
1086 local_bh_disable();
1088 current->thread.uw.fpsimd_state = *state;
1089 if (system_supports_sve() && test_thread_flag(TIF_SVE))
1090 fpsimd_to_sve(current);
1092 task_fpsimd_load();
1093 fpsimd_bind_task_to_cpu();
1095 clear_thread_flag(TIF_FOREIGN_FPSTATE);
1097 local_bh_enable();
1101 * Invalidate live CPU copies of task t's FPSIMD state
1103 * This function may be called with preemption enabled. The barrier()
1104 * ensures that the assignment to fpsimd_cpu is visible to any
1105 * preemption/softirq that could race with set_tsk_thread_flag(), so
1106 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1108 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1109 * subsequent code.
1111 void fpsimd_flush_task_state(struct task_struct *t)
1113 t->thread.fpsimd_cpu = NR_CPUS;
1115 barrier();
1116 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1118 barrier();
1122 * Invalidate any task's FPSIMD state that is present on this cpu.
1123 * This function must be called with softirqs disabled.
1125 void fpsimd_flush_cpu_state(void)
1127 __this_cpu_write(fpsimd_last_state.st, NULL);
1128 set_thread_flag(TIF_FOREIGN_FPSTATE);
1131 #ifdef CONFIG_KERNEL_MODE_NEON
1133 DEFINE_PER_CPU(bool, kernel_neon_busy);
1134 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
1137 * Kernel-side NEON support functions
1141 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1142 * context
1144 * Must not be called unless may_use_simd() returns true.
1145 * Task context in the FPSIMD registers is saved back to memory as necessary.
1147 * A matching call to kernel_neon_end() must be made before returning from the
1148 * calling context.
1150 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1151 * called.
1153 void kernel_neon_begin(void)
1155 if (WARN_ON(!system_supports_fpsimd()))
1156 return;
1158 BUG_ON(!may_use_simd());
1160 local_bh_disable();
1162 __this_cpu_write(kernel_neon_busy, true);
1164 /* Save unsaved fpsimd state, if any: */
1165 fpsimd_save();
1167 /* Invalidate any task state remaining in the fpsimd regs: */
1168 fpsimd_flush_cpu_state();
1170 preempt_disable();
1172 local_bh_enable();
1174 EXPORT_SYMBOL(kernel_neon_begin);
1177 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1179 * Must be called from a context in which kernel_neon_begin() was previously
1180 * called, with no call to kernel_neon_end() in the meantime.
1182 * The caller must not use the FPSIMD registers after this function is called,
1183 * unless kernel_neon_begin() is called again in the meantime.
1185 void kernel_neon_end(void)
1187 bool busy;
1189 if (!system_supports_fpsimd())
1190 return;
1192 busy = __this_cpu_xchg(kernel_neon_busy, false);
1193 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1195 preempt_enable();
1197 EXPORT_SYMBOL(kernel_neon_end);
1199 #ifdef CONFIG_EFI
1201 static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
1202 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
1203 static DEFINE_PER_CPU(bool, efi_sve_state_used);
1206 * EFI runtime services support functions
1208 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1209 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1210 * is always used rather than being an optional accelerator.
1212 * These functions provide the necessary support for ensuring FPSIMD
1213 * save/restore in the contexts from which EFI is used.
1215 * Do not use them for any other purpose -- if tempted to do so, you are
1216 * either doing something wrong or you need to propose some refactoring.
1220 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1222 void __efi_fpsimd_begin(void)
1224 if (!system_supports_fpsimd())
1225 return;
1227 WARN_ON(preemptible());
1229 if (may_use_simd()) {
1230 kernel_neon_begin();
1231 } else {
1233 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1234 * preserving:
1236 if (system_supports_sve() && likely(efi_sve_state)) {
1237 char *sve_state = this_cpu_ptr(efi_sve_state);
1239 __this_cpu_write(efi_sve_state_used, true);
1241 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1242 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1243 } else {
1244 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1247 __this_cpu_write(efi_fpsimd_state_used, true);
1252 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1254 void __efi_fpsimd_end(void)
1256 if (!system_supports_fpsimd())
1257 return;
1259 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
1260 kernel_neon_end();
1261 } else {
1262 if (system_supports_sve() &&
1263 likely(__this_cpu_read(efi_sve_state_used))) {
1264 char const *sve_state = this_cpu_ptr(efi_sve_state);
1266 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1267 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1268 sve_vq_from_vl(sve_get_vl()) - 1);
1270 __this_cpu_write(efi_sve_state_used, false);
1271 } else {
1272 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1277 #endif /* CONFIG_EFI */
1279 #endif /* CONFIG_KERNEL_MODE_NEON */
1281 #ifdef CONFIG_CPU_PM
1282 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1283 unsigned long cmd, void *v)
1285 switch (cmd) {
1286 case CPU_PM_ENTER:
1287 fpsimd_save();
1288 fpsimd_flush_cpu_state();
1289 break;
1290 case CPU_PM_EXIT:
1291 break;
1292 case CPU_PM_ENTER_FAILED:
1293 default:
1294 return NOTIFY_DONE;
1296 return NOTIFY_OK;
1299 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1300 .notifier_call = fpsimd_cpu_pm_notifier,
1303 static void __init fpsimd_pm_init(void)
1305 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1308 #else
1309 static inline void fpsimd_pm_init(void) { }
1310 #endif /* CONFIG_CPU_PM */
1312 #ifdef CONFIG_HOTPLUG_CPU
1313 static int fpsimd_cpu_dead(unsigned int cpu)
1315 per_cpu(fpsimd_last_state.st, cpu) = NULL;
1316 return 0;
1319 static inline void fpsimd_hotplug_init(void)
1321 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1322 NULL, fpsimd_cpu_dead);
1325 #else
1326 static inline void fpsimd_hotplug_init(void) { }
1327 #endif
1330 * FP/SIMD support code initialisation.
1332 static int __init fpsimd_init(void)
1334 if (cpu_have_named_feature(FP)) {
1335 fpsimd_pm_init();
1336 fpsimd_hotplug_init();
1337 } else {
1338 pr_notice("Floating-point is not implemented\n");
1341 if (!cpu_have_named_feature(ASIMD))
1342 pr_notice("Advanced SIMD is not implemented\n");
1344 return sve_sysctl_init();
1346 core_initcall(fpsimd_init);