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