Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * FP/SIMD context switching and fault handling
0004  *
0005  * Copyright (C) 2012 ARM Ltd.
0006  * Author: Catalin Marinas <catalin.marinas@arm.com>
0007  */
0008 
0009 #include <linux/bitmap.h>
0010 #include <linux/bitops.h>
0011 #include <linux/bottom_half.h>
0012 #include <linux/bug.h>
0013 #include <linux/cache.h>
0014 #include <linux/compat.h>
0015 #include <linux/compiler.h>
0016 #include <linux/cpu.h>
0017 #include <linux/cpu_pm.h>
0018 #include <linux/ctype.h>
0019 #include <linux/kernel.h>
0020 #include <linux/linkage.h>
0021 #include <linux/irqflags.h>
0022 #include <linux/init.h>
0023 #include <linux/percpu.h>
0024 #include <linux/prctl.h>
0025 #include <linux/preempt.h>
0026 #include <linux/ptrace.h>
0027 #include <linux/sched/signal.h>
0028 #include <linux/sched/task_stack.h>
0029 #include <linux/signal.h>
0030 #include <linux/slab.h>
0031 #include <linux/stddef.h>
0032 #include <linux/sysctl.h>
0033 #include <linux/swab.h>
0034 
0035 #include <asm/esr.h>
0036 #include <asm/exception.h>
0037 #include <asm/fpsimd.h>
0038 #include <asm/cpufeature.h>
0039 #include <asm/cputype.h>
0040 #include <asm/neon.h>
0041 #include <asm/processor.h>
0042 #include <asm/simd.h>
0043 #include <asm/sigcontext.h>
0044 #include <asm/sysreg.h>
0045 #include <asm/traps.h>
0046 #include <asm/virt.h>
0047 
0048 #define FPEXC_IOF   (1 << 0)
0049 #define FPEXC_DZF   (1 << 1)
0050 #define FPEXC_OFF   (1 << 2)
0051 #define FPEXC_UFF   (1 << 3)
0052 #define FPEXC_IXF   (1 << 4)
0053 #define FPEXC_IDF   (1 << 7)
0054 
0055 /*
0056  * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
0057  *
0058  * In order to reduce the number of times the FPSIMD state is needlessly saved
0059  * and restored, we need to keep track of two things:
0060  * (a) for each task, we need to remember which CPU was the last one to have
0061  *     the task's FPSIMD state loaded into its FPSIMD registers;
0062  * (b) for each CPU, we need to remember which task's userland FPSIMD state has
0063  *     been loaded into its FPSIMD registers most recently, or whether it has
0064  *     been used to perform kernel mode NEON in the meantime.
0065  *
0066  * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
0067  * the id of the current CPU every time the state is loaded onto a CPU. For (b),
0068  * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
0069  * address of the userland FPSIMD state of the task that was loaded onto the CPU
0070  * the most recently, or NULL if kernel mode NEON has been performed after that.
0071  *
0072  * With this in place, we no longer have to restore the next FPSIMD state right
0073  * when switching between tasks. Instead, we can defer this check to userland
0074  * resume, at which time we verify whether the CPU's fpsimd_last_state and the
0075  * task's fpsimd_cpu are still mutually in sync. If this is the case, we
0076  * can omit the FPSIMD restore.
0077  *
0078  * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
0079  * indicate whether or not the userland FPSIMD state of the current task is
0080  * present in the registers. The flag is set unless the FPSIMD registers of this
0081  * CPU currently contain the most recent userland FPSIMD state of the current
0082  * task. If the task is behaving as a VMM, then this is will be managed by
0083  * KVM which will clear it to indicate that the vcpu FPSIMD state is currently
0084  * loaded on the CPU, allowing the state to be saved if a FPSIMD-aware
0085  * softirq kicks in. Upon vcpu_put(), KVM will save the vcpu FP state and
0086  * flag the register state as invalid.
0087  *
0088  * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
0089  * save the task's FPSIMD context back to task_struct from softirq context.
0090  * To prevent this from racing with the manipulation of the task's FPSIMD state
0091  * from task context and thereby corrupting the state, it is necessary to
0092  * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
0093  * flag with {, __}get_cpu_fpsimd_context(). This will still allow softirqs to
0094  * run but prevent them to use FPSIMD.
0095  *
0096  * For a certain task, the sequence may look something like this:
0097  * - the task gets scheduled in; if both the task's fpsimd_cpu field
0098  *   contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
0099  *   variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
0100  *   cleared, otherwise it is set;
0101  *
0102  * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
0103  *   userland FPSIMD state is copied from memory to the registers, the task's
0104  *   fpsimd_cpu field is set to the id of the current CPU, the current
0105  *   CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
0106  *   TIF_FOREIGN_FPSTATE flag is cleared;
0107  *
0108  * - the task executes an ordinary syscall; upon return to userland, the
0109  *   TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
0110  *   restored;
0111  *
0112  * - the task executes a syscall which executes some NEON instructions; this is
0113  *   preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
0114  *   register contents to memory, clears the fpsimd_last_state per-cpu variable
0115  *   and sets the TIF_FOREIGN_FPSTATE flag;
0116  *
0117  * - the task gets preempted after kernel_neon_end() is called; as we have not
0118  *   returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
0119  *   whatever is in the FPSIMD registers is not saved to memory, but discarded.
0120  */
0121 struct fpsimd_last_state_struct {
0122     struct user_fpsimd_state *st;
0123     void *sve_state;
0124     void *za_state;
0125     u64 *svcr;
0126     unsigned int sve_vl;
0127     unsigned int sme_vl;
0128 };
0129 
0130 static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
0131 
0132 __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX] = {
0133 #ifdef CONFIG_ARM64_SVE
0134     [ARM64_VEC_SVE] = {
0135         .type           = ARM64_VEC_SVE,
0136         .name           = "SVE",
0137         .min_vl         = SVE_VL_MIN,
0138         .max_vl         = SVE_VL_MIN,
0139         .max_virtualisable_vl   = SVE_VL_MIN,
0140     },
0141 #endif
0142 #ifdef CONFIG_ARM64_SME
0143     [ARM64_VEC_SME] = {
0144         .type           = ARM64_VEC_SME,
0145         .name           = "SME",
0146     },
0147 #endif
0148 };
0149 
0150 static unsigned int vec_vl_inherit_flag(enum vec_type type)
0151 {
0152     switch (type) {
0153     case ARM64_VEC_SVE:
0154         return TIF_SVE_VL_INHERIT;
0155     case ARM64_VEC_SME:
0156         return TIF_SME_VL_INHERIT;
0157     default:
0158         WARN_ON_ONCE(1);
0159         return 0;
0160     }
0161 }
0162 
0163 struct vl_config {
0164     int __default_vl;       /* Default VL for tasks */
0165 };
0166 
0167 static struct vl_config vl_config[ARM64_VEC_MAX];
0168 
0169 static inline int get_default_vl(enum vec_type type)
0170 {
0171     return READ_ONCE(vl_config[type].__default_vl);
0172 }
0173 
0174 #ifdef CONFIG_ARM64_SVE
0175 
0176 static inline int get_sve_default_vl(void)
0177 {
0178     return get_default_vl(ARM64_VEC_SVE);
0179 }
0180 
0181 static inline void set_default_vl(enum vec_type type, int val)
0182 {
0183     WRITE_ONCE(vl_config[type].__default_vl, val);
0184 }
0185 
0186 static inline void set_sve_default_vl(int val)
0187 {
0188     set_default_vl(ARM64_VEC_SVE, val);
0189 }
0190 
0191 static void __percpu *efi_sve_state;
0192 
0193 #else /* ! CONFIG_ARM64_SVE */
0194 
0195 /* Dummy declaration for code that will be optimised out: */
0196 extern void __percpu *efi_sve_state;
0197 
0198 #endif /* ! CONFIG_ARM64_SVE */
0199 
0200 #ifdef CONFIG_ARM64_SME
0201 
0202 static int get_sme_default_vl(void)
0203 {
0204     return get_default_vl(ARM64_VEC_SME);
0205 }
0206 
0207 static void set_sme_default_vl(int val)
0208 {
0209     set_default_vl(ARM64_VEC_SME, val);
0210 }
0211 
0212 static void sme_free(struct task_struct *);
0213 
0214 #else
0215 
0216 static inline void sme_free(struct task_struct *t) { }
0217 
0218 #endif
0219 
0220 DEFINE_PER_CPU(bool, fpsimd_context_busy);
0221 EXPORT_PER_CPU_SYMBOL(fpsimd_context_busy);
0222 
0223 static void fpsimd_bind_task_to_cpu(void);
0224 
0225 static void __get_cpu_fpsimd_context(void)
0226 {
0227     bool busy = __this_cpu_xchg(fpsimd_context_busy, true);
0228 
0229     WARN_ON(busy);
0230 }
0231 
0232 /*
0233  * Claim ownership of the CPU FPSIMD context for use by the calling context.
0234  *
0235  * The caller may freely manipulate the FPSIMD context metadata until
0236  * put_cpu_fpsimd_context() is called.
0237  *
0238  * The double-underscore version must only be called if you know the task
0239  * can't be preempted.
0240  *
0241  * On RT kernels local_bh_disable() is not sufficient because it only
0242  * serializes soft interrupt related sections via a local lock, but stays
0243  * preemptible. Disabling preemption is the right choice here as bottom
0244  * half processing is always in thread context on RT kernels so it
0245  * implicitly prevents bottom half processing as well.
0246  */
0247 static void get_cpu_fpsimd_context(void)
0248 {
0249     if (!IS_ENABLED(CONFIG_PREEMPT_RT))
0250         local_bh_disable();
0251     else
0252         preempt_disable();
0253     __get_cpu_fpsimd_context();
0254 }
0255 
0256 static void __put_cpu_fpsimd_context(void)
0257 {
0258     bool busy = __this_cpu_xchg(fpsimd_context_busy, false);
0259 
0260     WARN_ON(!busy); /* No matching get_cpu_fpsimd_context()? */
0261 }
0262 
0263 /*
0264  * Release the CPU FPSIMD context.
0265  *
0266  * Must be called from a context in which get_cpu_fpsimd_context() was
0267  * previously called, with no call to put_cpu_fpsimd_context() in the
0268  * meantime.
0269  */
0270 static void put_cpu_fpsimd_context(void)
0271 {
0272     __put_cpu_fpsimd_context();
0273     if (!IS_ENABLED(CONFIG_PREEMPT_RT))
0274         local_bh_enable();
0275     else
0276         preempt_enable();
0277 }
0278 
0279 static bool have_cpu_fpsimd_context(void)
0280 {
0281     return !preemptible() && __this_cpu_read(fpsimd_context_busy);
0282 }
0283 
0284 unsigned int task_get_vl(const struct task_struct *task, enum vec_type type)
0285 {
0286     return task->thread.vl[type];
0287 }
0288 
0289 void task_set_vl(struct task_struct *task, enum vec_type type,
0290          unsigned long vl)
0291 {
0292     task->thread.vl[type] = vl;
0293 }
0294 
0295 unsigned int task_get_vl_onexec(const struct task_struct *task,
0296                 enum vec_type type)
0297 {
0298     return task->thread.vl_onexec[type];
0299 }
0300 
0301 void task_set_vl_onexec(struct task_struct *task, enum vec_type type,
0302             unsigned long vl)
0303 {
0304     task->thread.vl_onexec[type] = vl;
0305 }
0306 
0307 /*
0308  * TIF_SME controls whether a task can use SME without trapping while
0309  * in userspace, when TIF_SME is set then we must have storage
0310  * alocated in sve_state and za_state to store the contents of both ZA
0311  * and the SVE registers for both streaming and non-streaming modes.
0312  *
0313  * If both SVCR.ZA and SVCR.SM are disabled then at any point we
0314  * may disable TIF_SME and reenable traps.
0315  */
0316 
0317 
0318 /*
0319  * TIF_SVE controls whether a task can use SVE without trapping while
0320  * in userspace, and also (together with TIF_SME) the way a task's
0321  * FPSIMD/SVE state is stored in thread_struct.
0322  *
0323  * The kernel uses this flag to track whether a user task is actively
0324  * using SVE, and therefore whether full SVE register state needs to
0325  * be tracked.  If not, the cheaper FPSIMD context handling code can
0326  * be used instead of the more costly SVE equivalents.
0327  *
0328  *  * TIF_SVE or SVCR.SM set:
0329  *
0330  *    The task can execute SVE instructions while in userspace without
0331  *    trapping to the kernel.
0332  *
0333  *    When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
0334  *    corresponding Zn), P0-P15 and FFR are encoded in
0335  *    task->thread.sve_state, formatted appropriately for vector
0336  *    length task->thread.sve_vl or, if SVCR.SM is set,
0337  *    task->thread.sme_vl.
0338  *
0339  *    task->thread.sve_state must point to a valid buffer at least
0340  *    sve_state_size(task) bytes in size.
0341  *
0342  *    During any syscall, the kernel may optionally clear TIF_SVE and
0343  *    discard the vector state except for the FPSIMD subset.
0344  *
0345  *  * TIF_SVE clear:
0346  *
0347  *    An attempt by the user task to execute an SVE instruction causes
0348  *    do_sve_acc() to be called, which does some preparation and then
0349  *    sets TIF_SVE.
0350  *
0351  *    When stored, FPSIMD registers V0-V31 are encoded in
0352  *    task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
0353  *    logically zero but not stored anywhere; P0-P15 and FFR are not
0354  *    stored and have unspecified values from userspace's point of
0355  *    view.  For hygiene purposes, the kernel zeroes them on next use,
0356  *    but userspace is discouraged from relying on this.
0357  *
0358  *    task->thread.sve_state does not need to be non-NULL, valid or any
0359  *    particular size: it must not be dereferenced.
0360  *
0361  *  * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
0362  *    irrespective of whether TIF_SVE is clear or set, since these are
0363  *    not vector length dependent.
0364  */
0365 
0366 /*
0367  * Update current's FPSIMD/SVE registers from thread_struct.
0368  *
0369  * This function should be called only when the FPSIMD/SVE state in
0370  * thread_struct is known to be up to date, when preparing to enter
0371  * userspace.
0372  */
0373 static void task_fpsimd_load(void)
0374 {
0375     bool restore_sve_regs = false;
0376     bool restore_ffr;
0377 
0378     WARN_ON(!system_supports_fpsimd());
0379     WARN_ON(!have_cpu_fpsimd_context());
0380 
0381     /* Check if we should restore SVE first */
0382     if (IS_ENABLED(CONFIG_ARM64_SVE) && test_thread_flag(TIF_SVE)) {
0383         sve_set_vq(sve_vq_from_vl(task_get_sve_vl(current)) - 1);
0384         restore_sve_regs = true;
0385         restore_ffr = true;
0386     }
0387 
0388     /* Restore SME, override SVE register configuration if needed */
0389     if (system_supports_sme()) {
0390         unsigned long sme_vl = task_get_sme_vl(current);
0391 
0392         /* Ensure VL is set up for restoring data */
0393         if (test_thread_flag(TIF_SME))
0394             sme_set_vq(sve_vq_from_vl(sme_vl) - 1);
0395 
0396         write_sysreg_s(current->thread.svcr, SYS_SVCR);
0397 
0398         if (thread_za_enabled(&current->thread))
0399             za_load_state(current->thread.za_state);
0400 
0401         if (thread_sm_enabled(&current->thread)) {
0402             restore_sve_regs = true;
0403             restore_ffr = system_supports_fa64();
0404         }
0405     }
0406 
0407     if (restore_sve_regs)
0408         sve_load_state(sve_pffr(&current->thread),
0409                    &current->thread.uw.fpsimd_state.fpsr,
0410                    restore_ffr);
0411     else
0412         fpsimd_load_state(&current->thread.uw.fpsimd_state);
0413 }
0414 
0415 /*
0416  * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
0417  * date with respect to the CPU registers. Note carefully that the
0418  * current context is the context last bound to the CPU stored in
0419  * last, if KVM is involved this may be the guest VM context rather
0420  * than the host thread for the VM pointed to by current. This means
0421  * that we must always reference the state storage via last rather
0422  * than via current, other than the TIF_ flags which KVM will
0423  * carefully maintain for us.
0424  */
0425 static void fpsimd_save(void)
0426 {
0427     struct fpsimd_last_state_struct const *last =
0428         this_cpu_ptr(&fpsimd_last_state);
0429     /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
0430     bool save_sve_regs = false;
0431     bool save_ffr;
0432     unsigned int vl;
0433 
0434     WARN_ON(!system_supports_fpsimd());
0435     WARN_ON(!have_cpu_fpsimd_context());
0436 
0437     if (test_thread_flag(TIF_FOREIGN_FPSTATE))
0438         return;
0439 
0440     if (test_thread_flag(TIF_SVE)) {
0441         save_sve_regs = true;
0442         save_ffr = true;
0443         vl = last->sve_vl;
0444     }
0445 
0446     if (system_supports_sme()) {
0447         u64 *svcr = last->svcr;
0448 
0449         *svcr = read_sysreg_s(SYS_SVCR);
0450 
0451         if (*svcr & SVCR_ZA_MASK)
0452             za_save_state(last->za_state);
0453 
0454         /* If we are in streaming mode override regular SVE. */
0455         if (*svcr & SVCR_SM_MASK) {
0456             save_sve_regs = true;
0457             save_ffr = system_supports_fa64();
0458             vl = last->sme_vl;
0459         }
0460     }
0461 
0462     if (IS_ENABLED(CONFIG_ARM64_SVE) && save_sve_regs) {
0463         /* Get the configured VL from RDVL, will account for SM */
0464         if (WARN_ON(sve_get_vl() != vl)) {
0465             /*
0466              * Can't save the user regs, so current would
0467              * re-enter user with corrupt state.
0468              * There's no way to recover, so kill it:
0469              */
0470             force_signal_inject(SIGKILL, SI_KERNEL, 0, 0);
0471             return;
0472         }
0473 
0474         sve_save_state((char *)last->sve_state +
0475                     sve_ffr_offset(vl),
0476                    &last->st->fpsr, save_ffr);
0477     } else {
0478         fpsimd_save_state(last->st);
0479     }
0480 }
0481 
0482 /*
0483  * All vector length selection from userspace comes through here.
0484  * We're on a slow path, so some sanity-checks are included.
0485  * If things go wrong there's a bug somewhere, but try to fall back to a
0486  * safe choice.
0487  */
0488 static unsigned int find_supported_vector_length(enum vec_type type,
0489                          unsigned int vl)
0490 {
0491     struct vl_info *info = &vl_info[type];
0492     int bit;
0493     int max_vl = info->max_vl;
0494 
0495     if (WARN_ON(!sve_vl_valid(vl)))
0496         vl = info->min_vl;
0497 
0498     if (WARN_ON(!sve_vl_valid(max_vl)))
0499         max_vl = info->min_vl;
0500 
0501     if (vl > max_vl)
0502         vl = max_vl;
0503     if (vl < info->min_vl)
0504         vl = info->min_vl;
0505 
0506     bit = find_next_bit(info->vq_map, SVE_VQ_MAX,
0507                 __vq_to_bit(sve_vq_from_vl(vl)));
0508     return sve_vl_from_vq(__bit_to_vq(bit));
0509 }
0510 
0511 #if defined(CONFIG_ARM64_SVE) && defined(CONFIG_SYSCTL)
0512 
0513 static int vec_proc_do_default_vl(struct ctl_table *table, int write,
0514                   void *buffer, size_t *lenp, loff_t *ppos)
0515 {
0516     struct vl_info *info = table->extra1;
0517     enum vec_type type = info->type;
0518     int ret;
0519     int vl = get_default_vl(type);
0520     struct ctl_table tmp_table = {
0521         .data = &vl,
0522         .maxlen = sizeof(vl),
0523     };
0524 
0525     ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
0526     if (ret || !write)
0527         return ret;
0528 
0529     /* Writing -1 has the special meaning "set to max": */
0530     if (vl == -1)
0531         vl = info->max_vl;
0532 
0533     if (!sve_vl_valid(vl))
0534         return -EINVAL;
0535 
0536     set_default_vl(type, find_supported_vector_length(type, vl));
0537     return 0;
0538 }
0539 
0540 static struct ctl_table sve_default_vl_table[] = {
0541     {
0542         .procname   = "sve_default_vector_length",
0543         .mode       = 0644,
0544         .proc_handler   = vec_proc_do_default_vl,
0545         .extra1     = &vl_info[ARM64_VEC_SVE],
0546     },
0547     { }
0548 };
0549 
0550 static int __init sve_sysctl_init(void)
0551 {
0552     if (system_supports_sve())
0553         if (!register_sysctl("abi", sve_default_vl_table))
0554             return -EINVAL;
0555 
0556     return 0;
0557 }
0558 
0559 #else /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
0560 static int __init sve_sysctl_init(void) { return 0; }
0561 #endif /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
0562 
0563 #if defined(CONFIG_ARM64_SME) && defined(CONFIG_SYSCTL)
0564 static struct ctl_table sme_default_vl_table[] = {
0565     {
0566         .procname   = "sme_default_vector_length",
0567         .mode       = 0644,
0568         .proc_handler   = vec_proc_do_default_vl,
0569         .extra1     = &vl_info[ARM64_VEC_SME],
0570     },
0571     { }
0572 };
0573 
0574 static int __init sme_sysctl_init(void)
0575 {
0576     if (system_supports_sme())
0577         if (!register_sysctl("abi", sme_default_vl_table))
0578             return -EINVAL;
0579 
0580     return 0;
0581 }
0582 
0583 #else /* ! (CONFIG_ARM64_SME && CONFIG_SYSCTL) */
0584 static int __init sme_sysctl_init(void) { return 0; }
0585 #endif /* ! (CONFIG_ARM64_SME && CONFIG_SYSCTL) */
0586 
0587 #define ZREG(sve_state, vq, n) ((char *)(sve_state) +       \
0588     (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
0589 
0590 #ifdef CONFIG_CPU_BIG_ENDIAN
0591 static __uint128_t arm64_cpu_to_le128(__uint128_t x)
0592 {
0593     u64 a = swab64(x);
0594     u64 b = swab64(x >> 64);
0595 
0596     return ((__uint128_t)a << 64) | b;
0597 }
0598 #else
0599 static __uint128_t arm64_cpu_to_le128(__uint128_t x)
0600 {
0601     return x;
0602 }
0603 #endif
0604 
0605 #define arm64_le128_to_cpu(x) arm64_cpu_to_le128(x)
0606 
0607 static void __fpsimd_to_sve(void *sst, struct user_fpsimd_state const *fst,
0608                 unsigned int vq)
0609 {
0610     unsigned int i;
0611     __uint128_t *p;
0612 
0613     for (i = 0; i < SVE_NUM_ZREGS; ++i) {
0614         p = (__uint128_t *)ZREG(sst, vq, i);
0615         *p = arm64_cpu_to_le128(fst->vregs[i]);
0616     }
0617 }
0618 
0619 /*
0620  * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
0621  * task->thread.sve_state.
0622  *
0623  * Task can be a non-runnable task, or current.  In the latter case,
0624  * the caller must have ownership of the cpu FPSIMD context before calling
0625  * this function.
0626  * task->thread.sve_state must point to at least sve_state_size(task)
0627  * bytes of allocated kernel memory.
0628  * task->thread.uw.fpsimd_state must be up to date before calling this
0629  * function.
0630  */
0631 static void fpsimd_to_sve(struct task_struct *task)
0632 {
0633     unsigned int vq;
0634     void *sst = task->thread.sve_state;
0635     struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
0636 
0637     if (!system_supports_sve())
0638         return;
0639 
0640     vq = sve_vq_from_vl(thread_get_cur_vl(&task->thread));
0641     __fpsimd_to_sve(sst, fst, vq);
0642 }
0643 
0644 /*
0645  * Transfer the SVE state in task->thread.sve_state to
0646  * task->thread.uw.fpsimd_state.
0647  *
0648  * Task can be a non-runnable task, or current.  In the latter case,
0649  * the caller must have ownership of the cpu FPSIMD context before calling
0650  * this function.
0651  * task->thread.sve_state must point to at least sve_state_size(task)
0652  * bytes of allocated kernel memory.
0653  * task->thread.sve_state must be up to date before calling this function.
0654  */
0655 static void sve_to_fpsimd(struct task_struct *task)
0656 {
0657     unsigned int vq, vl;
0658     void const *sst = task->thread.sve_state;
0659     struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
0660     unsigned int i;
0661     __uint128_t const *p;
0662 
0663     if (!system_supports_sve())
0664         return;
0665 
0666     vl = thread_get_cur_vl(&task->thread);
0667     vq = sve_vq_from_vl(vl);
0668     for (i = 0; i < SVE_NUM_ZREGS; ++i) {
0669         p = (__uint128_t const *)ZREG(sst, vq, i);
0670         fst->vregs[i] = arm64_le128_to_cpu(*p);
0671     }
0672 }
0673 
0674 #ifdef CONFIG_ARM64_SVE
0675 /*
0676  * Call __sve_free() directly only if you know task can't be scheduled
0677  * or preempted.
0678  */
0679 static void __sve_free(struct task_struct *task)
0680 {
0681     kfree(task->thread.sve_state);
0682     task->thread.sve_state = NULL;
0683 }
0684 
0685 static void sve_free(struct task_struct *task)
0686 {
0687     WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
0688 
0689     __sve_free(task);
0690 }
0691 
0692 /*
0693  * Return how many bytes of memory are required to store the full SVE
0694  * state for task, given task's currently configured vector length.
0695  */
0696 size_t sve_state_size(struct task_struct const *task)
0697 {
0698     unsigned int vl = 0;
0699 
0700     if (system_supports_sve())
0701         vl = task_get_sve_vl(task);
0702     if (system_supports_sme())
0703         vl = max(vl, task_get_sme_vl(task));
0704 
0705     return SVE_SIG_REGS_SIZE(sve_vq_from_vl(vl));
0706 }
0707 
0708 /*
0709  * Ensure that task->thread.sve_state is allocated and sufficiently large.
0710  *
0711  * This function should be used only in preparation for replacing
0712  * task->thread.sve_state with new data.  The memory is always zeroed
0713  * here to prevent stale data from showing through: this is done in
0714  * the interest of testability and predictability: except in the
0715  * do_sve_acc() case, there is no ABI requirement to hide stale data
0716  * written previously be task.
0717  */
0718 void sve_alloc(struct task_struct *task, bool flush)
0719 {
0720     if (task->thread.sve_state) {
0721         if (flush)
0722             memset(task->thread.sve_state, 0,
0723                    sve_state_size(task));
0724         return;
0725     }
0726 
0727     /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
0728     task->thread.sve_state =
0729         kzalloc(sve_state_size(task), GFP_KERNEL);
0730 }
0731 
0732 
0733 /*
0734  * Force the FPSIMD state shared with SVE to be updated in the SVE state
0735  * even if the SVE state is the current active state.
0736  *
0737  * This should only be called by ptrace.  task must be non-runnable.
0738  * task->thread.sve_state must point to at least sve_state_size(task)
0739  * bytes of allocated kernel memory.
0740  */
0741 void fpsimd_force_sync_to_sve(struct task_struct *task)
0742 {
0743     fpsimd_to_sve(task);
0744 }
0745 
0746 /*
0747  * Ensure that task->thread.sve_state is up to date with respect to
0748  * the user task, irrespective of when SVE is in use or not.
0749  *
0750  * This should only be called by ptrace.  task must be non-runnable.
0751  * task->thread.sve_state must point to at least sve_state_size(task)
0752  * bytes of allocated kernel memory.
0753  */
0754 void fpsimd_sync_to_sve(struct task_struct *task)
0755 {
0756     if (!test_tsk_thread_flag(task, TIF_SVE) &&
0757         !thread_sm_enabled(&task->thread))
0758         fpsimd_to_sve(task);
0759 }
0760 
0761 /*
0762  * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
0763  * the user task, irrespective of whether SVE is in use or not.
0764  *
0765  * This should only be called by ptrace.  task must be non-runnable.
0766  * task->thread.sve_state must point to at least sve_state_size(task)
0767  * bytes of allocated kernel memory.
0768  */
0769 void sve_sync_to_fpsimd(struct task_struct *task)
0770 {
0771     if (test_tsk_thread_flag(task, TIF_SVE) ||
0772         thread_sm_enabled(&task->thread))
0773         sve_to_fpsimd(task);
0774 }
0775 
0776 /*
0777  * Ensure that task->thread.sve_state is up to date with respect to
0778  * the task->thread.uw.fpsimd_state.
0779  *
0780  * This should only be called by ptrace to merge new FPSIMD register
0781  * values into a task for which SVE is currently active.
0782  * task must be non-runnable.
0783  * task->thread.sve_state must point to at least sve_state_size(task)
0784  * bytes of allocated kernel memory.
0785  * task->thread.uw.fpsimd_state must already have been initialised with
0786  * the new FPSIMD register values to be merged in.
0787  */
0788 void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
0789 {
0790     unsigned int vq;
0791     void *sst = task->thread.sve_state;
0792     struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
0793 
0794     if (!test_tsk_thread_flag(task, TIF_SVE))
0795         return;
0796 
0797     vq = sve_vq_from_vl(thread_get_cur_vl(&task->thread));
0798 
0799     memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
0800     __fpsimd_to_sve(sst, fst, vq);
0801 }
0802 
0803 int vec_set_vector_length(struct task_struct *task, enum vec_type type,
0804               unsigned long vl, unsigned long flags)
0805 {
0806     if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
0807                      PR_SVE_SET_VL_ONEXEC))
0808         return -EINVAL;
0809 
0810     if (!sve_vl_valid(vl))
0811         return -EINVAL;
0812 
0813     /*
0814      * Clamp to the maximum vector length that VL-agnostic code
0815      * can work with.  A flag may be assigned in the future to
0816      * allow setting of larger vector lengths without confusing
0817      * older software.
0818      */
0819     if (vl > VL_ARCH_MAX)
0820         vl = VL_ARCH_MAX;
0821 
0822     vl = find_supported_vector_length(type, vl);
0823 
0824     if (flags & (PR_SVE_VL_INHERIT |
0825              PR_SVE_SET_VL_ONEXEC))
0826         task_set_vl_onexec(task, type, vl);
0827     else
0828         /* Reset VL to system default on next exec: */
0829         task_set_vl_onexec(task, type, 0);
0830 
0831     /* Only actually set the VL if not deferred: */
0832     if (flags & PR_SVE_SET_VL_ONEXEC)
0833         goto out;
0834 
0835     if (vl == task_get_vl(task, type))
0836         goto out;
0837 
0838     /*
0839      * To ensure the FPSIMD bits of the SVE vector registers are preserved,
0840      * write any live register state back to task_struct, and convert to a
0841      * regular FPSIMD thread.
0842      */
0843     if (task == current) {
0844         get_cpu_fpsimd_context();
0845 
0846         fpsimd_save();
0847     }
0848 
0849     fpsimd_flush_task_state(task);
0850     if (test_and_clear_tsk_thread_flag(task, TIF_SVE) ||
0851         thread_sm_enabled(&task->thread))
0852         sve_to_fpsimd(task);
0853 
0854     if (system_supports_sme() && type == ARM64_VEC_SME) {
0855         task->thread.svcr &= ~(SVCR_SM_MASK |
0856                        SVCR_ZA_MASK);
0857         clear_thread_flag(TIF_SME);
0858     }
0859 
0860     if (task == current)
0861         put_cpu_fpsimd_context();
0862 
0863     /*
0864      * Force reallocation of task SVE and SME state to the correct
0865      * size on next use:
0866      */
0867     sve_free(task);
0868     if (system_supports_sme() && type == ARM64_VEC_SME)
0869         sme_free(task);
0870 
0871     task_set_vl(task, type, vl);
0872 
0873 out:
0874     update_tsk_thread_flag(task, vec_vl_inherit_flag(type),
0875                    flags & PR_SVE_VL_INHERIT);
0876 
0877     return 0;
0878 }
0879 
0880 /*
0881  * Encode the current vector length and flags for return.
0882  * This is only required for prctl(): ptrace has separate fields.
0883  * SVE and SME use the same bits for _ONEXEC and _INHERIT.
0884  *
0885  * flags are as for vec_set_vector_length().
0886  */
0887 static int vec_prctl_status(enum vec_type type, unsigned long flags)
0888 {
0889     int ret;
0890 
0891     if (flags & PR_SVE_SET_VL_ONEXEC)
0892         ret = task_get_vl_onexec(current, type);
0893     else
0894         ret = task_get_vl(current, type);
0895 
0896     if (test_thread_flag(vec_vl_inherit_flag(type)))
0897         ret |= PR_SVE_VL_INHERIT;
0898 
0899     return ret;
0900 }
0901 
0902 /* PR_SVE_SET_VL */
0903 int sve_set_current_vl(unsigned long arg)
0904 {
0905     unsigned long vl, flags;
0906     int ret;
0907 
0908     vl = arg & PR_SVE_VL_LEN_MASK;
0909     flags = arg & ~vl;
0910 
0911     if (!system_supports_sve() || is_compat_task())
0912         return -EINVAL;
0913 
0914     ret = vec_set_vector_length(current, ARM64_VEC_SVE, vl, flags);
0915     if (ret)
0916         return ret;
0917 
0918     return vec_prctl_status(ARM64_VEC_SVE, flags);
0919 }
0920 
0921 /* PR_SVE_GET_VL */
0922 int sve_get_current_vl(void)
0923 {
0924     if (!system_supports_sve() || is_compat_task())
0925         return -EINVAL;
0926 
0927     return vec_prctl_status(ARM64_VEC_SVE, 0);
0928 }
0929 
0930 #ifdef CONFIG_ARM64_SME
0931 /* PR_SME_SET_VL */
0932 int sme_set_current_vl(unsigned long arg)
0933 {
0934     unsigned long vl, flags;
0935     int ret;
0936 
0937     vl = arg & PR_SME_VL_LEN_MASK;
0938     flags = arg & ~vl;
0939 
0940     if (!system_supports_sme() || is_compat_task())
0941         return -EINVAL;
0942 
0943     ret = vec_set_vector_length(current, ARM64_VEC_SME, vl, flags);
0944     if (ret)
0945         return ret;
0946 
0947     return vec_prctl_status(ARM64_VEC_SME, flags);
0948 }
0949 
0950 /* PR_SME_GET_VL */
0951 int sme_get_current_vl(void)
0952 {
0953     if (!system_supports_sme() || is_compat_task())
0954         return -EINVAL;
0955 
0956     return vec_prctl_status(ARM64_VEC_SME, 0);
0957 }
0958 #endif /* CONFIG_ARM64_SME */
0959 
0960 static void vec_probe_vqs(struct vl_info *info,
0961               DECLARE_BITMAP(map, SVE_VQ_MAX))
0962 {
0963     unsigned int vq, vl;
0964 
0965     bitmap_zero(map, SVE_VQ_MAX);
0966 
0967     for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
0968         write_vl(info->type, vq - 1); /* self-syncing */
0969 
0970         switch (info->type) {
0971         case ARM64_VEC_SVE:
0972             vl = sve_get_vl();
0973             break;
0974         case ARM64_VEC_SME:
0975             vl = sme_get_vl();
0976             break;
0977         default:
0978             vl = 0;
0979             break;
0980         }
0981 
0982         /* Minimum VL identified? */
0983         if (sve_vq_from_vl(vl) > vq)
0984             break;
0985 
0986         vq = sve_vq_from_vl(vl); /* skip intervening lengths */
0987         set_bit(__vq_to_bit(vq), map);
0988     }
0989 }
0990 
0991 /*
0992  * Initialise the set of known supported VQs for the boot CPU.
0993  * This is called during kernel boot, before secondary CPUs are brought up.
0994  */
0995 void __init vec_init_vq_map(enum vec_type type)
0996 {
0997     struct vl_info *info = &vl_info[type];
0998     vec_probe_vqs(info, info->vq_map);
0999     bitmap_copy(info->vq_partial_map, info->vq_map, SVE_VQ_MAX);
1000 }
1001 
1002 /*
1003  * If we haven't committed to the set of supported VQs yet, filter out
1004  * those not supported by the current CPU.
1005  * This function is called during the bring-up of early secondary CPUs only.
1006  */
1007 void vec_update_vq_map(enum vec_type type)
1008 {
1009     struct vl_info *info = &vl_info[type];
1010     DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
1011 
1012     vec_probe_vqs(info, tmp_map);
1013     bitmap_and(info->vq_map, info->vq_map, tmp_map, SVE_VQ_MAX);
1014     bitmap_or(info->vq_partial_map, info->vq_partial_map, tmp_map,
1015           SVE_VQ_MAX);
1016 }
1017 
1018 /*
1019  * Check whether the current CPU supports all VQs in the committed set.
1020  * This function is called during the bring-up of late secondary CPUs only.
1021  */
1022 int vec_verify_vq_map(enum vec_type type)
1023 {
1024     struct vl_info *info = &vl_info[type];
1025     DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
1026     unsigned long b;
1027 
1028     vec_probe_vqs(info, tmp_map);
1029 
1030     bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
1031     if (bitmap_intersects(tmp_map, info->vq_map, SVE_VQ_MAX)) {
1032         pr_warn("%s: cpu%d: Required vector length(s) missing\n",
1033             info->name, smp_processor_id());
1034         return -EINVAL;
1035     }
1036 
1037     if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
1038         return 0;
1039 
1040     /*
1041      * For KVM, it is necessary to ensure that this CPU doesn't
1042      * support any vector length that guests may have probed as
1043      * unsupported.
1044      */
1045 
1046     /* Recover the set of supported VQs: */
1047     bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
1048     /* Find VQs supported that are not globally supported: */
1049     bitmap_andnot(tmp_map, tmp_map, info->vq_map, SVE_VQ_MAX);
1050 
1051     /* Find the lowest such VQ, if any: */
1052     b = find_last_bit(tmp_map, SVE_VQ_MAX);
1053     if (b >= SVE_VQ_MAX)
1054         return 0; /* no mismatches */
1055 
1056     /*
1057      * Mismatches above sve_max_virtualisable_vl are fine, since
1058      * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
1059      */
1060     if (sve_vl_from_vq(__bit_to_vq(b)) <= info->max_virtualisable_vl) {
1061         pr_warn("%s: cpu%d: Unsupported vector length(s) present\n",
1062             info->name, smp_processor_id());
1063         return -EINVAL;
1064     }
1065 
1066     return 0;
1067 }
1068 
1069 static void __init sve_efi_setup(void)
1070 {
1071     int max_vl = 0;
1072     int i;
1073 
1074     if (!IS_ENABLED(CONFIG_EFI))
1075         return;
1076 
1077     for (i = 0; i < ARRAY_SIZE(vl_info); i++)
1078         max_vl = max(vl_info[i].max_vl, max_vl);
1079 
1080     /*
1081      * alloc_percpu() warns and prints a backtrace if this goes wrong.
1082      * This is evidence of a crippled system and we are returning void,
1083      * so no attempt is made to handle this situation here.
1084      */
1085     if (!sve_vl_valid(max_vl))
1086         goto fail;
1087 
1088     efi_sve_state = __alloc_percpu(
1089         SVE_SIG_REGS_SIZE(sve_vq_from_vl(max_vl)), SVE_VQ_BYTES);
1090     if (!efi_sve_state)
1091         goto fail;
1092 
1093     return;
1094 
1095 fail:
1096     panic("Cannot allocate percpu memory for EFI SVE save/restore");
1097 }
1098 
1099 /*
1100  * Enable SVE for EL1.
1101  * Intended for use by the cpufeatures code during CPU boot.
1102  */
1103 void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
1104 {
1105     write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
1106     isb();
1107 }
1108 
1109 /*
1110  * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
1111  * vector length.
1112  *
1113  * Use only if SVE is present.
1114  * This function clobbers the SVE vector length.
1115  */
1116 u64 read_zcr_features(void)
1117 {
1118     u64 zcr;
1119     unsigned int vq_max;
1120 
1121     /*
1122      * Set the maximum possible VL, and write zeroes to all other
1123      * bits to see if they stick.
1124      */
1125     sve_kernel_enable(NULL);
1126     write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
1127 
1128     zcr = read_sysreg_s(SYS_ZCR_EL1);
1129     zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
1130     vq_max = sve_vq_from_vl(sve_get_vl());
1131     zcr |= vq_max - 1; /* set LEN field to maximum effective value */
1132 
1133     return zcr;
1134 }
1135 
1136 void __init sve_setup(void)
1137 {
1138     struct vl_info *info = &vl_info[ARM64_VEC_SVE];
1139     u64 zcr;
1140     DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
1141     unsigned long b;
1142 
1143     if (!system_supports_sve())
1144         return;
1145 
1146     /*
1147      * The SVE architecture mandates support for 128-bit vectors,
1148      * so sve_vq_map must have at least SVE_VQ_MIN set.
1149      * If something went wrong, at least try to patch it up:
1150      */
1151     if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), info->vq_map)))
1152         set_bit(__vq_to_bit(SVE_VQ_MIN), info->vq_map);
1153 
1154     zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
1155     info->max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
1156 
1157     /*
1158      * Sanity-check that the max VL we determined through CPU features
1159      * corresponds properly to sve_vq_map.  If not, do our best:
1160      */
1161     if (WARN_ON(info->max_vl != find_supported_vector_length(ARM64_VEC_SVE,
1162                                  info->max_vl)))
1163         info->max_vl = find_supported_vector_length(ARM64_VEC_SVE,
1164                                 info->max_vl);
1165 
1166     /*
1167      * For the default VL, pick the maximum supported value <= 64.
1168      * VL == 64 is guaranteed not to grow the signal frame.
1169      */
1170     set_sve_default_vl(find_supported_vector_length(ARM64_VEC_SVE, 64));
1171 
1172     bitmap_andnot(tmp_map, info->vq_partial_map, info->vq_map,
1173               SVE_VQ_MAX);
1174 
1175     b = find_last_bit(tmp_map, SVE_VQ_MAX);
1176     if (b >= SVE_VQ_MAX)
1177         /* No non-virtualisable VLs found */
1178         info->max_virtualisable_vl = SVE_VQ_MAX;
1179     else if (WARN_ON(b == SVE_VQ_MAX - 1))
1180         /* No virtualisable VLs?  This is architecturally forbidden. */
1181         info->max_virtualisable_vl = SVE_VQ_MIN;
1182     else /* b + 1 < SVE_VQ_MAX */
1183         info->max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
1184 
1185     if (info->max_virtualisable_vl > info->max_vl)
1186         info->max_virtualisable_vl = info->max_vl;
1187 
1188     pr_info("%s: maximum available vector length %u bytes per vector\n",
1189         info->name, info->max_vl);
1190     pr_info("%s: default vector length %u bytes per vector\n",
1191         info->name, get_sve_default_vl());
1192 
1193     /* KVM decides whether to support mismatched systems. Just warn here: */
1194     if (sve_max_virtualisable_vl() < sve_max_vl())
1195         pr_warn("%s: unvirtualisable vector lengths present\n",
1196             info->name);
1197 
1198     sve_efi_setup();
1199 }
1200 
1201 /*
1202  * Called from the put_task_struct() path, which cannot get here
1203  * unless dead_task is really dead and not schedulable.
1204  */
1205 void fpsimd_release_task(struct task_struct *dead_task)
1206 {
1207     __sve_free(dead_task);
1208     sme_free(dead_task);
1209 }
1210 
1211 #endif /* CONFIG_ARM64_SVE */
1212 
1213 #ifdef CONFIG_ARM64_SME
1214 
1215 /*
1216  * Ensure that task->thread.za_state is allocated and sufficiently large.
1217  *
1218  * This function should be used only in preparation for replacing
1219  * task->thread.za_state with new data.  The memory is always zeroed
1220  * here to prevent stale data from showing through: this is done in
1221  * the interest of testability and predictability, the architecture
1222  * guarantees that when ZA is enabled it will be zeroed.
1223  */
1224 void sme_alloc(struct task_struct *task)
1225 {
1226     if (task->thread.za_state) {
1227         memset(task->thread.za_state, 0, za_state_size(task));
1228         return;
1229     }
1230 
1231     /* This could potentially be up to 64K. */
1232     task->thread.za_state =
1233         kzalloc(za_state_size(task), GFP_KERNEL);
1234 }
1235 
1236 static void sme_free(struct task_struct *task)
1237 {
1238     kfree(task->thread.za_state);
1239     task->thread.za_state = NULL;
1240 }
1241 
1242 void sme_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
1243 {
1244     /* Set priority for all PEs to architecturally defined minimum */
1245     write_sysreg_s(read_sysreg_s(SYS_SMPRI_EL1) & ~SMPRI_EL1_PRIORITY_MASK,
1246                SYS_SMPRI_EL1);
1247 
1248     /* Allow SME in kernel */
1249     write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_SMEN_EL1EN, CPACR_EL1);
1250     isb();
1251 
1252     /* Allow EL0 to access TPIDR2 */
1253     write_sysreg(read_sysreg(SCTLR_EL1) | SCTLR_ELx_ENTP2, SCTLR_EL1);
1254     isb();
1255 }
1256 
1257 /*
1258  * This must be called after sme_kernel_enable(), we rely on the
1259  * feature table being sorted to ensure this.
1260  */
1261 void fa64_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
1262 {
1263     /* Allow use of FA64 */
1264     write_sysreg_s(read_sysreg_s(SYS_SMCR_EL1) | SMCR_ELx_FA64_MASK,
1265                SYS_SMCR_EL1);
1266 }
1267 
1268 /*
1269  * Read the pseudo-SMCR used by cpufeatures to identify the supported
1270  * vector length.
1271  *
1272  * Use only if SME is present.
1273  * This function clobbers the SME vector length.
1274  */
1275 u64 read_smcr_features(void)
1276 {
1277     u64 smcr;
1278     unsigned int vq_max;
1279 
1280     sme_kernel_enable(NULL);
1281     sme_smstart_sm();
1282 
1283     /*
1284      * Set the maximum possible VL.
1285      */
1286     write_sysreg_s(read_sysreg_s(SYS_SMCR_EL1) | SMCR_ELx_LEN_MASK,
1287                SYS_SMCR_EL1);
1288 
1289     smcr = read_sysreg_s(SYS_SMCR_EL1);
1290     smcr &= ~(u64)SMCR_ELx_LEN_MASK; /* Only the LEN field */
1291     vq_max = sve_vq_from_vl(sve_get_vl());
1292     smcr |= vq_max - 1; /* set LEN field to maximum effective value */
1293 
1294     sme_smstop_sm();
1295 
1296     return smcr;
1297 }
1298 
1299 void __init sme_setup(void)
1300 {
1301     struct vl_info *info = &vl_info[ARM64_VEC_SME];
1302     u64 smcr;
1303     int min_bit;
1304 
1305     if (!system_supports_sme())
1306         return;
1307 
1308     /*
1309      * SME doesn't require any particular vector length be
1310      * supported but it does require at least one.  We should have
1311      * disabled the feature entirely while bringing up CPUs but
1312      * let's double check here.
1313      */
1314     WARN_ON(bitmap_empty(info->vq_map, SVE_VQ_MAX));
1315 
1316     min_bit = find_last_bit(info->vq_map, SVE_VQ_MAX);
1317     info->min_vl = sve_vl_from_vq(__bit_to_vq(min_bit));
1318 
1319     smcr = read_sanitised_ftr_reg(SYS_SMCR_EL1);
1320     info->max_vl = sve_vl_from_vq((smcr & SMCR_ELx_LEN_MASK) + 1);
1321 
1322     /*
1323      * Sanity-check that the max VL we determined through CPU features
1324      * corresponds properly to sme_vq_map.  If not, do our best:
1325      */
1326     if (WARN_ON(info->max_vl != find_supported_vector_length(ARM64_VEC_SME,
1327                                  info->max_vl)))
1328         info->max_vl = find_supported_vector_length(ARM64_VEC_SME,
1329                                 info->max_vl);
1330 
1331     WARN_ON(info->min_vl > info->max_vl);
1332 
1333     /*
1334      * For the default VL, pick the maximum supported value <= 32
1335      * (256 bits) if there is one since this is guaranteed not to
1336      * grow the signal frame when in streaming mode, otherwise the
1337      * minimum available VL will be used.
1338      */
1339     set_sme_default_vl(find_supported_vector_length(ARM64_VEC_SME, 32));
1340 
1341     pr_info("SME: minimum available vector length %u bytes per vector\n",
1342         info->min_vl);
1343     pr_info("SME: maximum available vector length %u bytes per vector\n",
1344         info->max_vl);
1345     pr_info("SME: default vector length %u bytes per vector\n",
1346         get_sme_default_vl());
1347 }
1348 
1349 #endif /* CONFIG_ARM64_SME */
1350 
1351 static void sve_init_regs(void)
1352 {
1353     /*
1354      * Convert the FPSIMD state to SVE, zeroing all the state that
1355      * is not shared with FPSIMD. If (as is likely) the current
1356      * state is live in the registers then do this there and
1357      * update our metadata for the current task including
1358      * disabling the trap, otherwise update our in-memory copy.
1359      * We are guaranteed to not be in streaming mode, we can only
1360      * take a SVE trap when not in streaming mode and we can't be
1361      * in streaming mode when taking a SME trap.
1362      */
1363     if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
1364         unsigned long vq_minus_one =
1365             sve_vq_from_vl(task_get_sve_vl(current)) - 1;
1366         sve_set_vq(vq_minus_one);
1367         sve_flush_live(true, vq_minus_one);
1368         fpsimd_bind_task_to_cpu();
1369     } else {
1370         fpsimd_to_sve(current);
1371     }
1372 }
1373 
1374 /*
1375  * Trapped SVE access
1376  *
1377  * Storage is allocated for the full SVE state, the current FPSIMD
1378  * register contents are migrated across, and the access trap is
1379  * disabled.
1380  *
1381  * TIF_SVE should be clear on entry: otherwise, fpsimd_restore_current_state()
1382  * would have disabled the SVE access trap for userspace during
1383  * ret_to_user, making an SVE access trap impossible in that case.
1384  */
1385 void do_sve_acc(unsigned long esr, struct pt_regs *regs)
1386 {
1387     /* Even if we chose not to use SVE, the hardware could still trap: */
1388     if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
1389         force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1390         return;
1391     }
1392 
1393     sve_alloc(current, true);
1394     if (!current->thread.sve_state) {
1395         force_sig(SIGKILL);
1396         return;
1397     }
1398 
1399     get_cpu_fpsimd_context();
1400 
1401     if (test_and_set_thread_flag(TIF_SVE))
1402         WARN_ON(1); /* SVE access shouldn't have trapped */
1403 
1404     /*
1405      * Even if the task can have used streaming mode we can only
1406      * generate SVE access traps in normal SVE mode and
1407      * transitioning out of streaming mode may discard any
1408      * streaming mode state.  Always clear the high bits to avoid
1409      * any potential errors tracking what is properly initialised.
1410      */
1411     sve_init_regs();
1412 
1413     put_cpu_fpsimd_context();
1414 }
1415 
1416 /*
1417  * Trapped SME access
1418  *
1419  * Storage is allocated for the full SVE and SME state, the current
1420  * FPSIMD register contents are migrated to SVE if SVE is not already
1421  * active, and the access trap is disabled.
1422  *
1423  * TIF_SME should be clear on entry: otherwise, fpsimd_restore_current_state()
1424  * would have disabled the SME access trap for userspace during
1425  * ret_to_user, making an SVE access trap impossible in that case.
1426  */
1427 void do_sme_acc(unsigned long esr, struct pt_regs *regs)
1428 {
1429     /* Even if we chose not to use SME, the hardware could still trap: */
1430     if (unlikely(!system_supports_sme()) || WARN_ON(is_compat_task())) {
1431         force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1432         return;
1433     }
1434 
1435     /*
1436      * If this not a trap due to SME being disabled then something
1437      * is being used in the wrong mode, report as SIGILL.
1438      */
1439     if (ESR_ELx_ISS(esr) != ESR_ELx_SME_ISS_SME_DISABLED) {
1440         force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1441         return;
1442     }
1443 
1444     sve_alloc(current, false);
1445     sme_alloc(current);
1446     if (!current->thread.sve_state || !current->thread.za_state) {
1447         force_sig(SIGKILL);
1448         return;
1449     }
1450 
1451     get_cpu_fpsimd_context();
1452 
1453     /* With TIF_SME userspace shouldn't generate any traps */
1454     if (test_and_set_thread_flag(TIF_SME))
1455         WARN_ON(1);
1456 
1457     if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
1458         unsigned long vq_minus_one =
1459             sve_vq_from_vl(task_get_sme_vl(current)) - 1;
1460         sme_set_vq(vq_minus_one);
1461 
1462         fpsimd_bind_task_to_cpu();
1463     }
1464 
1465     put_cpu_fpsimd_context();
1466 }
1467 
1468 /*
1469  * Trapped FP/ASIMD access.
1470  */
1471 void do_fpsimd_acc(unsigned long esr, struct pt_regs *regs)
1472 {
1473     /* TODO: implement lazy context saving/restoring */
1474     WARN_ON(1);
1475 }
1476 
1477 /*
1478  * Raise a SIGFPE for the current process.
1479  */
1480 void do_fpsimd_exc(unsigned long esr, struct pt_regs *regs)
1481 {
1482     unsigned int si_code = FPE_FLTUNK;
1483 
1484     if (esr & ESR_ELx_FP_EXC_TFV) {
1485         if (esr & FPEXC_IOF)
1486             si_code = FPE_FLTINV;
1487         else if (esr & FPEXC_DZF)
1488             si_code = FPE_FLTDIV;
1489         else if (esr & FPEXC_OFF)
1490             si_code = FPE_FLTOVF;
1491         else if (esr & FPEXC_UFF)
1492             si_code = FPE_FLTUND;
1493         else if (esr & FPEXC_IXF)
1494             si_code = FPE_FLTRES;
1495     }
1496 
1497     send_sig_fault(SIGFPE, si_code,
1498                (void __user *)instruction_pointer(regs),
1499                current);
1500 }
1501 
1502 void fpsimd_thread_switch(struct task_struct *next)
1503 {
1504     bool wrong_task, wrong_cpu;
1505 
1506     if (!system_supports_fpsimd())
1507         return;
1508 
1509     __get_cpu_fpsimd_context();
1510 
1511     /* Save unsaved fpsimd state, if any: */
1512     fpsimd_save();
1513 
1514     /*
1515      * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
1516      * state.  For kernel threads, FPSIMD registers are never loaded
1517      * and wrong_task and wrong_cpu will always be true.
1518      */
1519     wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
1520                     &next->thread.uw.fpsimd_state;
1521     wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
1522 
1523     update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
1524                    wrong_task || wrong_cpu);
1525 
1526     __put_cpu_fpsimd_context();
1527 }
1528 
1529 static void fpsimd_flush_thread_vl(enum vec_type type)
1530 {
1531     int vl, supported_vl;
1532 
1533     /*
1534      * Reset the task vector length as required.  This is where we
1535      * ensure that all user tasks have a valid vector length
1536      * configured: no kernel task can become a user task without
1537      * an exec and hence a call to this function.  By the time the
1538      * first call to this function is made, all early hardware
1539      * probing is complete, so __sve_default_vl should be valid.
1540      * If a bug causes this to go wrong, we make some noise and
1541      * try to fudge thread.sve_vl to a safe value here.
1542      */
1543     vl = task_get_vl_onexec(current, type);
1544     if (!vl)
1545         vl = get_default_vl(type);
1546 
1547     if (WARN_ON(!sve_vl_valid(vl)))
1548         vl = vl_info[type].min_vl;
1549 
1550     supported_vl = find_supported_vector_length(type, vl);
1551     if (WARN_ON(supported_vl != vl))
1552         vl = supported_vl;
1553 
1554     task_set_vl(current, type, vl);
1555 
1556     /*
1557      * If the task is not set to inherit, ensure that the vector
1558      * length will be reset by a subsequent exec:
1559      */
1560     if (!test_thread_flag(vec_vl_inherit_flag(type)))
1561         task_set_vl_onexec(current, type, 0);
1562 }
1563 
1564 void fpsimd_flush_thread(void)
1565 {
1566     void *sve_state = NULL;
1567     void *za_state = NULL;
1568 
1569     if (!system_supports_fpsimd())
1570         return;
1571 
1572     get_cpu_fpsimd_context();
1573 
1574     fpsimd_flush_task_state(current);
1575     memset(&current->thread.uw.fpsimd_state, 0,
1576            sizeof(current->thread.uw.fpsimd_state));
1577 
1578     if (system_supports_sve()) {
1579         clear_thread_flag(TIF_SVE);
1580 
1581         /* Defer kfree() while in atomic context */
1582         sve_state = current->thread.sve_state;
1583         current->thread.sve_state = NULL;
1584 
1585         fpsimd_flush_thread_vl(ARM64_VEC_SVE);
1586     }
1587 
1588     if (system_supports_sme()) {
1589         clear_thread_flag(TIF_SME);
1590 
1591         /* Defer kfree() while in atomic context */
1592         za_state = current->thread.za_state;
1593         current->thread.za_state = NULL;
1594 
1595         fpsimd_flush_thread_vl(ARM64_VEC_SME);
1596         current->thread.svcr = 0;
1597     }
1598 
1599     put_cpu_fpsimd_context();
1600     kfree(sve_state);
1601     kfree(za_state);
1602 }
1603 
1604 /*
1605  * Save the userland FPSIMD state of 'current' to memory, but only if the state
1606  * currently held in the registers does in fact belong to 'current'
1607  */
1608 void fpsimd_preserve_current_state(void)
1609 {
1610     if (!system_supports_fpsimd())
1611         return;
1612 
1613     get_cpu_fpsimd_context();
1614     fpsimd_save();
1615     put_cpu_fpsimd_context();
1616 }
1617 
1618 /*
1619  * Like fpsimd_preserve_current_state(), but ensure that
1620  * current->thread.uw.fpsimd_state is updated so that it can be copied to
1621  * the signal frame.
1622  */
1623 void fpsimd_signal_preserve_current_state(void)
1624 {
1625     fpsimd_preserve_current_state();
1626     if (test_thread_flag(TIF_SVE))
1627         sve_to_fpsimd(current);
1628 }
1629 
1630 /*
1631  * Associate current's FPSIMD context with this cpu
1632  * The caller must have ownership of the cpu FPSIMD context before calling
1633  * this function.
1634  */
1635 static void fpsimd_bind_task_to_cpu(void)
1636 {
1637     struct fpsimd_last_state_struct *last =
1638         this_cpu_ptr(&fpsimd_last_state);
1639 
1640     WARN_ON(!system_supports_fpsimd());
1641     last->st = &current->thread.uw.fpsimd_state;
1642     last->sve_state = current->thread.sve_state;
1643     last->za_state = current->thread.za_state;
1644     last->sve_vl = task_get_sve_vl(current);
1645     last->sme_vl = task_get_sme_vl(current);
1646     last->svcr = &current->thread.svcr;
1647     current->thread.fpsimd_cpu = smp_processor_id();
1648 
1649     /*
1650      * Toggle SVE and SME trapping for userspace if needed, these
1651      * are serialsied by ret_to_user().
1652      */
1653     if (system_supports_sme()) {
1654         if (test_thread_flag(TIF_SME))
1655             sme_user_enable();
1656         else
1657             sme_user_disable();
1658     }
1659 
1660     if (system_supports_sve()) {
1661         if (test_thread_flag(TIF_SVE))
1662             sve_user_enable();
1663         else
1664             sve_user_disable();
1665     }
1666 }
1667 
1668 void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
1669                   unsigned int sve_vl, void *za_state,
1670                   unsigned int sme_vl, u64 *svcr)
1671 {
1672     struct fpsimd_last_state_struct *last =
1673         this_cpu_ptr(&fpsimd_last_state);
1674 
1675     WARN_ON(!system_supports_fpsimd());
1676     WARN_ON(!in_softirq() && !irqs_disabled());
1677 
1678     last->st = st;
1679     last->svcr = svcr;
1680     last->sve_state = sve_state;
1681     last->za_state = za_state;
1682     last->sve_vl = sve_vl;
1683     last->sme_vl = sme_vl;
1684 }
1685 
1686 /*
1687  * Load the userland FPSIMD state of 'current' from memory, but only if the
1688  * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1689  * state of 'current'.  This is called when we are preparing to return to
1690  * userspace to ensure that userspace sees a good register state.
1691  */
1692 void fpsimd_restore_current_state(void)
1693 {
1694     /*
1695      * For the tasks that were created before we detected the absence of
1696      * FP/SIMD, the TIF_FOREIGN_FPSTATE could be set via fpsimd_thread_switch(),
1697      * e.g, init. This could be then inherited by the children processes.
1698      * If we later detect that the system doesn't support FP/SIMD,
1699      * we must clear the flag for  all the tasks to indicate that the
1700      * FPSTATE is clean (as we can't have one) to avoid looping for ever in
1701      * do_notify_resume().
1702      */
1703     if (!system_supports_fpsimd()) {
1704         clear_thread_flag(TIF_FOREIGN_FPSTATE);
1705         return;
1706     }
1707 
1708     get_cpu_fpsimd_context();
1709 
1710     if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
1711         task_fpsimd_load();
1712         fpsimd_bind_task_to_cpu();
1713     }
1714 
1715     put_cpu_fpsimd_context();
1716 }
1717 
1718 /*
1719  * Load an updated userland FPSIMD state for 'current' from memory and set the
1720  * flag that indicates that the FPSIMD register contents are the most recent
1721  * FPSIMD state of 'current'. This is used by the signal code to restore the
1722  * register state when returning from a signal handler in FPSIMD only cases,
1723  * any SVE context will be discarded.
1724  */
1725 void fpsimd_update_current_state(struct user_fpsimd_state const *state)
1726 {
1727     if (WARN_ON(!system_supports_fpsimd()))
1728         return;
1729 
1730     get_cpu_fpsimd_context();
1731 
1732     current->thread.uw.fpsimd_state = *state;
1733     if (test_thread_flag(TIF_SVE))
1734         fpsimd_to_sve(current);
1735 
1736     task_fpsimd_load();
1737     fpsimd_bind_task_to_cpu();
1738 
1739     clear_thread_flag(TIF_FOREIGN_FPSTATE);
1740 
1741     put_cpu_fpsimd_context();
1742 }
1743 
1744 /*
1745  * Invalidate live CPU copies of task t's FPSIMD state
1746  *
1747  * This function may be called with preemption enabled.  The barrier()
1748  * ensures that the assignment to fpsimd_cpu is visible to any
1749  * preemption/softirq that could race with set_tsk_thread_flag(), so
1750  * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1751  *
1752  * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1753  * subsequent code.
1754  */
1755 void fpsimd_flush_task_state(struct task_struct *t)
1756 {
1757     t->thread.fpsimd_cpu = NR_CPUS;
1758     /*
1759      * If we don't support fpsimd, bail out after we have
1760      * reset the fpsimd_cpu for this task and clear the
1761      * FPSTATE.
1762      */
1763     if (!system_supports_fpsimd())
1764         return;
1765     barrier();
1766     set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1767 
1768     barrier();
1769 }
1770 
1771 /*
1772  * Invalidate any task's FPSIMD state that is present on this cpu.
1773  * The FPSIMD context should be acquired with get_cpu_fpsimd_context()
1774  * before calling this function.
1775  */
1776 static void fpsimd_flush_cpu_state(void)
1777 {
1778     WARN_ON(!system_supports_fpsimd());
1779     __this_cpu_write(fpsimd_last_state.st, NULL);
1780 
1781     /*
1782      * Leaving streaming mode enabled will cause issues for any kernel
1783      * NEON and leaving streaming mode or ZA enabled may increase power
1784      * consumption.
1785      */
1786     if (system_supports_sme())
1787         sme_smstop();
1788 
1789     set_thread_flag(TIF_FOREIGN_FPSTATE);
1790 }
1791 
1792 /*
1793  * Save the FPSIMD state to memory and invalidate cpu view.
1794  * This function must be called with preemption disabled.
1795  */
1796 void fpsimd_save_and_flush_cpu_state(void)
1797 {
1798     if (!system_supports_fpsimd())
1799         return;
1800     WARN_ON(preemptible());
1801     __get_cpu_fpsimd_context();
1802     fpsimd_save();
1803     fpsimd_flush_cpu_state();
1804     __put_cpu_fpsimd_context();
1805 }
1806 
1807 #ifdef CONFIG_KERNEL_MODE_NEON
1808 
1809 /*
1810  * Kernel-side NEON support functions
1811  */
1812 
1813 /*
1814  * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1815  * context
1816  *
1817  * Must not be called unless may_use_simd() returns true.
1818  * Task context in the FPSIMD registers is saved back to memory as necessary.
1819  *
1820  * A matching call to kernel_neon_end() must be made before returning from the
1821  * calling context.
1822  *
1823  * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1824  * called.
1825  */
1826 void kernel_neon_begin(void)
1827 {
1828     if (WARN_ON(!system_supports_fpsimd()))
1829         return;
1830 
1831     BUG_ON(!may_use_simd());
1832 
1833     get_cpu_fpsimd_context();
1834 
1835     /* Save unsaved fpsimd state, if any: */
1836     fpsimd_save();
1837 
1838     /* Invalidate any task state remaining in the fpsimd regs: */
1839     fpsimd_flush_cpu_state();
1840 }
1841 EXPORT_SYMBOL(kernel_neon_begin);
1842 
1843 /*
1844  * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1845  *
1846  * Must be called from a context in which kernel_neon_begin() was previously
1847  * called, with no call to kernel_neon_end() in the meantime.
1848  *
1849  * The caller must not use the FPSIMD registers after this function is called,
1850  * unless kernel_neon_begin() is called again in the meantime.
1851  */
1852 void kernel_neon_end(void)
1853 {
1854     if (!system_supports_fpsimd())
1855         return;
1856 
1857     put_cpu_fpsimd_context();
1858 }
1859 EXPORT_SYMBOL(kernel_neon_end);
1860 
1861 #ifdef CONFIG_EFI
1862 
1863 static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
1864 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
1865 static DEFINE_PER_CPU(bool, efi_sve_state_used);
1866 static DEFINE_PER_CPU(bool, efi_sm_state);
1867 
1868 /*
1869  * EFI runtime services support functions
1870  *
1871  * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1872  * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1873  * is always used rather than being an optional accelerator.
1874  *
1875  * These functions provide the necessary support for ensuring FPSIMD
1876  * save/restore in the contexts from which EFI is used.
1877  *
1878  * Do not use them for any other purpose -- if tempted to do so, you are
1879  * either doing something wrong or you need to propose some refactoring.
1880  */
1881 
1882 /*
1883  * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1884  */
1885 void __efi_fpsimd_begin(void)
1886 {
1887     if (!system_supports_fpsimd())
1888         return;
1889 
1890     WARN_ON(preemptible());
1891 
1892     if (may_use_simd()) {
1893         kernel_neon_begin();
1894     } else {
1895         /*
1896          * If !efi_sve_state, SVE can't be in use yet and doesn't need
1897          * preserving:
1898          */
1899         if (system_supports_sve() && likely(efi_sve_state)) {
1900             char *sve_state = this_cpu_ptr(efi_sve_state);
1901             bool ffr = true;
1902             u64 svcr;
1903 
1904             __this_cpu_write(efi_sve_state_used, true);
1905 
1906             if (system_supports_sme()) {
1907                 svcr = read_sysreg_s(SYS_SVCR);
1908 
1909                 __this_cpu_write(efi_sm_state,
1910                          svcr & SVCR_SM_MASK);
1911 
1912                 /*
1913                  * Unless we have FA64 FFR does not
1914                  * exist in streaming mode.
1915                  */
1916                 if (!system_supports_fa64())
1917                     ffr = !(svcr & SVCR_SM_MASK);
1918             }
1919 
1920             sve_save_state(sve_state + sve_ffr_offset(sve_max_vl()),
1921                        &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1922                        ffr);
1923 
1924             if (system_supports_sme())
1925                 sysreg_clear_set_s(SYS_SVCR,
1926                            SVCR_SM_MASK, 0);
1927 
1928         } else {
1929             fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1930         }
1931 
1932         __this_cpu_write(efi_fpsimd_state_used, true);
1933     }
1934 }
1935 
1936 /*
1937  * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1938  */
1939 void __efi_fpsimd_end(void)
1940 {
1941     if (!system_supports_fpsimd())
1942         return;
1943 
1944     if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
1945         kernel_neon_end();
1946     } else {
1947         if (system_supports_sve() &&
1948             likely(__this_cpu_read(efi_sve_state_used))) {
1949             char const *sve_state = this_cpu_ptr(efi_sve_state);
1950             bool ffr = true;
1951 
1952             /*
1953              * Restore streaming mode; EFI calls are
1954              * normal function calls so should not return in
1955              * streaming mode.
1956              */
1957             if (system_supports_sme()) {
1958                 if (__this_cpu_read(efi_sm_state)) {
1959                     sysreg_clear_set_s(SYS_SVCR,
1960                                0,
1961                                SVCR_SM_MASK);
1962 
1963                     /*
1964                      * Unless we have FA64 FFR does not
1965                      * exist in streaming mode.
1966                      */
1967                     if (!system_supports_fa64())
1968                         ffr = false;
1969                 }
1970             }
1971 
1972             sve_load_state(sve_state + sve_ffr_offset(sve_max_vl()),
1973                        &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1974                        ffr);
1975 
1976             __this_cpu_write(efi_sve_state_used, false);
1977         } else {
1978             fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1979         }
1980     }
1981 }
1982 
1983 #endif /* CONFIG_EFI */
1984 
1985 #endif /* CONFIG_KERNEL_MODE_NEON */
1986 
1987 #ifdef CONFIG_CPU_PM
1988 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1989                   unsigned long cmd, void *v)
1990 {
1991     switch (cmd) {
1992     case CPU_PM_ENTER:
1993         fpsimd_save_and_flush_cpu_state();
1994         break;
1995     case CPU_PM_EXIT:
1996         break;
1997     case CPU_PM_ENTER_FAILED:
1998     default:
1999         return NOTIFY_DONE;
2000     }
2001     return NOTIFY_OK;
2002 }
2003 
2004 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
2005     .notifier_call = fpsimd_cpu_pm_notifier,
2006 };
2007 
2008 static void __init fpsimd_pm_init(void)
2009 {
2010     cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
2011 }
2012 
2013 #else
2014 static inline void fpsimd_pm_init(void) { }
2015 #endif /* CONFIG_CPU_PM */
2016 
2017 #ifdef CONFIG_HOTPLUG_CPU
2018 static int fpsimd_cpu_dead(unsigned int cpu)
2019 {
2020     per_cpu(fpsimd_last_state.st, cpu) = NULL;
2021     return 0;
2022 }
2023 
2024 static inline void fpsimd_hotplug_init(void)
2025 {
2026     cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
2027                   NULL, fpsimd_cpu_dead);
2028 }
2029 
2030 #else
2031 static inline void fpsimd_hotplug_init(void) { }
2032 #endif
2033 
2034 /*
2035  * FP/SIMD support code initialisation.
2036  */
2037 static int __init fpsimd_init(void)
2038 {
2039     if (cpu_have_named_feature(FP)) {
2040         fpsimd_pm_init();
2041         fpsimd_hotplug_init();
2042     } else {
2043         pr_notice("Floating-point is not implemented\n");
2044     }
2045 
2046     if (!cpu_have_named_feature(ASIMD))
2047         pr_notice("Advanced SIMD is not implemented\n");
2048 
2049 
2050     if (cpu_have_named_feature(SME) && !cpu_have_named_feature(SVE))
2051         pr_notice("SME is implemented but not SVE\n");
2052 
2053     sve_sysctl_init();
2054     sme_sysctl_init();
2055 
2056     return 0;
2057 }
2058 core_initcall(fpsimd_init);