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
vec_vl_inherit_flag(enum vec_type type)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
get_default_vl(enum vec_type type)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
get_sve_default_vl(void)170 static inline int get_sve_default_vl(void)
171 {
172 return get_default_vl(ARM64_VEC_SVE);
173 }
174
set_default_vl(enum vec_type type,int val)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
set_sve_default_vl(int val)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
get_sme_default_vl(void)189 static int get_sme_default_vl(void)
190 {
191 return get_default_vl(ARM64_VEC_SME);
192 }
193
set_sme_default_vl(int val)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
sme_free(struct task_struct * t)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 */
get_cpu_fpsimd_context(void)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 */
put_cpu_fpsimd_context(void)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
task_get_vl(const struct task_struct * task,enum vec_type type)257 unsigned int task_get_vl(const struct task_struct *task, enum vec_type type)
258 {
259 return task->thread.vl[type];
260 }
261
task_set_vl(struct task_struct * task,enum vec_type type,unsigned long vl)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
task_get_vl_onexec(const struct task_struct * task,enum vec_type type)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
task_set_vl_onexec(struct task_struct * task,enum vec_type type,unsigned long vl)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 */
task_fpsimd_load(void)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(¤t->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(¤t->thread))
416 sme_load_state(current->thread.sme_state,
417 system_supports_sme2());
418
419 if (thread_sm_enabled(¤t->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(¤t->thread.uw.fpsimd_state);
430 } else {
431 WARN_ON_ONCE(current->thread.fp_type != FP_STATE_FPSIMD);
432 fpsimd_load_state(¤t->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 */
fpsimd_save_user_state(void)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 */
find_supported_vector_length(enum vec_type type,unsigned int vl)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
vec_proc_do_default_vl(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)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
sve_sysctl_init(void)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) */
sve_sysctl_init(void)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
sme_sysctl_init(void)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) */
sme_sysctl_init(void)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
arm64_cpu_to_le128(__uint128_t x)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
arm64_cpu_to_le128(__uint128_t x)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
__fpsimd_to_sve(struct arm64_sve_state * sst,struct user_fpsimd_state const * fst,unsigned int vq)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 */
fpsimd_to_sve(struct task_struct * task)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 */
sve_to_fpsimd(struct task_struct * task)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
__fpsimd_zero_vregs(struct user_fpsimd_state * fpsimd)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 */
task_smstop_sm(struct task_struct * task)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
cpu_enable_fpmr(const struct arm64_cpu_capabilities * __always_unused p)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
sve_free(struct task_struct * task)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 */
sve_alloc(struct task_struct * task,bool flush)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 */
fpsimd_sync_from_effective_state(struct task_struct * task)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 */
fpsimd_sync_to_effective_state_zeropad(struct task_struct * task)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
change_live_vector_length(struct task_struct * task,enum vec_type type,unsigned long vl)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
vec_set_vector_length(struct task_struct * task,enum vec_type type,unsigned long vl,unsigned long flags)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 */
vec_prctl_status(enum vec_type type,unsigned long flags)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 */
sve_set_current_vl(unsigned long arg)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 */
sve_get_current_vl(void)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 */
sme_set_current_vl(unsigned long arg)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 */
sme_get_current_vl(void)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
vec_probe_vqs(struct vl_info * info,DECLARE_BITMAP (map,SVE_VQ_MAX))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 */
vec_init_vq_map(enum vec_type type)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 */
vec_update_vq_map(enum vec_type type)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 */
vec_verify_vq_map(enum vec_type type)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
cpu_enable_sve(const struct arm64_cpu_capabilities * __always_unused p)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
sve_setup(void)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 */
fpsimd_release_task(struct task_struct * dead_task)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 */
sme_alloc(struct task_struct * task,bool flush)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
sme_free(struct task_struct * task)1196 static void sme_free(struct task_struct *task)
1197 {
1198 kfree(task->thread.sme_state);
1199 task->thread.sme_state = NULL;
1200 }
1201
cpu_enable_sme(const struct arm64_cpu_capabilities * __always_unused p)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
cpu_enable_sme2(const struct arm64_cpu_capabilities * __always_unused p)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
cpu_enable_fa64(const struct arm64_cpu_capabilities * __always_unused p)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
sme_setup(void)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
sme_suspend_exit(void)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 */
do_sve_acc(unsigned long esr,struct pt_regs * regs)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 cpumask_t sme_active_cpus;
1359
1360 /*
1361 * These helpers are only called from non-preemptible contexts, so
1362 * smp_processor_id() is safe here.
1363 */
sme_set_active(void)1364 void sme_set_active(void)
1365 {
1366 unsigned int cpu = smp_processor_id();
1367
1368 if (!cpumask_test_cpu(cpu, &sme_dvmsync_cpus))
1369 return;
1370
1371 cpumask_set_cpu(cpu, mm_cpumask(current->mm));
1372 cpumask_set_cpu(cpu, &sme_active_cpus);
1373
1374 /*
1375 * A subsequent (post ERET) SME access may use a stale address
1376 * translation. On C1-Pro, a TLBI+DSB on a different CPU will wait for
1377 * the completion of the cpumask_set_cpu() operations above as they
1378 * appear in program order before the SME access. The post-TLBI+DSB
1379 * read of mm_cpumask() or sme_active_cpus will lead to the IPI being
1380 * issued.
1381 *
1382 * https://lore.kernel.org/r/ablEXwhfKyJW1i7l@J2N7QTR9R3
1383 */
1384 }
1385
sme_clear_active(void)1386 void sme_clear_active(void)
1387 {
1388 unsigned int cpu = smp_processor_id();
1389
1390 if (!cpumask_test_cpu(cpu, &sme_dvmsync_cpus))
1391 return;
1392
1393 /*
1394 * With SCTLR_EL1.IESB enabled, the SME memory transactions are
1395 * completed on entering EL1.
1396 */
1397 cpumask_clear_cpu(cpu, mm_cpumask(current->mm));
1398 cpumask_clear_cpu(cpu, &sme_active_cpus);
1399 }
1400
sme_dvmsync_ipi(void * unused)1401 static void sme_dvmsync_ipi(void *unused)
1402 {
1403 /*
1404 * With SCTLR_EL1.IESB on, taking an exception is sufficient to ensure
1405 * the completion of the SME memory accesses, so no need for an
1406 * explicit DSB.
1407 */
1408 }
1409
sme_do_dvmsync(const struct cpumask * mask)1410 void sme_do_dvmsync(const struct cpumask *mask)
1411 {
1412 /*
1413 * This is called from the TLB maintenance functions after the DSB ISH
1414 * to send the hardware DVMSync message. If this CPU sees the mask as
1415 * empty, the remote CPU executing sme_set_active() would have seen
1416 * the DVMSync and no IPI required.
1417 */
1418 if (cpumask_empty(mask))
1419 return;
1420
1421 preempt_disable();
1422 smp_call_function_many(mask, sme_dvmsync_ipi, NULL, true);
1423 preempt_enable();
1424 }
1425
sme_enable_dvmsync(void)1426 void sme_enable_dvmsync(void)
1427 {
1428 cpumask_set_cpu(smp_processor_id(), &sme_dvmsync_cpus);
1429 }
1430
1431 #endif /* CONFIG_ARM64_ERRATUM_4193714 */
1432
1433 /*
1434 * Trapped SME access
1435 *
1436 * Storage is allocated for the full SVE and SME state, the current
1437 * FPSIMD register contents are migrated to SVE if SVE is not already
1438 * active, and the access trap is disabled.
1439 *
1440 * TIF_SME should be clear on entry: otherwise, fpsimd_restore_current_state()
1441 * would have disabled the SME access trap for userspace during
1442 * ret_to_user, making an SME access trap impossible in that case.
1443 */
do_sme_acc(unsigned long esr,struct pt_regs * regs)1444 void do_sme_acc(unsigned long esr, struct pt_regs *regs)
1445 {
1446 /* Even if we chose not to use SME, the hardware could still trap: */
1447 if (unlikely(!system_supports_sme()) || WARN_ON(is_compat_task())) {
1448 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1449 return;
1450 }
1451
1452 /*
1453 * If this not a trap due to SME being disabled then something
1454 * is being used in the wrong mode, report as SIGILL.
1455 */
1456 if (ESR_ELx_SME_ISS_SMTC(esr) != ESR_ELx_SME_ISS_SMTC_SME_DISABLED) {
1457 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1458 return;
1459 }
1460
1461 sve_alloc(current, false);
1462 sme_alloc(current, true);
1463 if (!current->thread.sve_state || !current->thread.sme_state) {
1464 force_sig(SIGKILL);
1465 return;
1466 }
1467
1468 get_cpu_fpsimd_context();
1469
1470 /* With TIF_SME userspace shouldn't generate any traps */
1471 if (test_and_set_thread_flag(TIF_SME))
1472 WARN_ON(1);
1473
1474 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
1475 unsigned long vq = sve_vq_from_vl(task_get_sme_vl(current));
1476 sysreg_clear_set_s(SYS_SMCR_EL1, SMCR_ELx_LEN, vq - 1);
1477
1478 fpsimd_bind_task_to_cpu();
1479 } else {
1480 fpsimd_flush_task_state(current);
1481 }
1482
1483 put_cpu_fpsimd_context();
1484 }
1485
1486 /*
1487 * Trapped FP/ASIMD access.
1488 */
do_fpsimd_acc(unsigned long esr,struct pt_regs * regs)1489 void do_fpsimd_acc(unsigned long esr, struct pt_regs *regs)
1490 {
1491 /* Even if we chose not to use FPSIMD, the hardware could still trap: */
1492 if (!system_supports_fpsimd()) {
1493 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
1494 return;
1495 }
1496
1497 /*
1498 * When FPSIMD is enabled, we should never take a trap unless something
1499 * has gone very wrong.
1500 */
1501 BUG();
1502 }
1503
1504 /*
1505 * Raise a SIGFPE for the current process.
1506 */
do_fpsimd_exc(unsigned long esr,struct pt_regs * regs)1507 void do_fpsimd_exc(unsigned long esr, struct pt_regs *regs)
1508 {
1509 unsigned int si_code = FPE_FLTUNK;
1510
1511 if (esr & ESR_ELx_FP_EXC_TFV) {
1512 if (esr & FPEXC_IOF)
1513 si_code = FPE_FLTINV;
1514 else if (esr & FPEXC_DZF)
1515 si_code = FPE_FLTDIV;
1516 else if (esr & FPEXC_OFF)
1517 si_code = FPE_FLTOVF;
1518 else if (esr & FPEXC_UFF)
1519 si_code = FPE_FLTUND;
1520 else if (esr & FPEXC_IXF)
1521 si_code = FPE_FLTRES;
1522 }
1523
1524 send_sig_fault(SIGFPE, si_code,
1525 (void __user *)instruction_pointer(regs),
1526 current);
1527 }
1528
fpsimd_load_kernel_state(struct task_struct * task)1529 static void fpsimd_load_kernel_state(struct task_struct *task)
1530 {
1531 struct cpu_fp_state *last = this_cpu_ptr(&fpsimd_last_state);
1532
1533 /*
1534 * Elide the load if this CPU holds the most recent kernel mode
1535 * FPSIMD context of the current task.
1536 */
1537 if (last->st == task->thread.kernel_fpsimd_state &&
1538 task->thread.kernel_fpsimd_cpu == smp_processor_id())
1539 return;
1540
1541 fpsimd_load_state(task->thread.kernel_fpsimd_state);
1542 }
1543
fpsimd_save_kernel_state(struct task_struct * task)1544 static void fpsimd_save_kernel_state(struct task_struct *task)
1545 {
1546 struct cpu_fp_state cpu_fp_state = {
1547 .st = task->thread.kernel_fpsimd_state,
1548 .to_save = FP_STATE_FPSIMD,
1549 };
1550
1551 BUG_ON(!cpu_fp_state.st);
1552
1553 fpsimd_save_state(task->thread.kernel_fpsimd_state);
1554 fpsimd_bind_state_to_cpu(&cpu_fp_state);
1555
1556 task->thread.kernel_fpsimd_cpu = smp_processor_id();
1557 }
1558
1559 /*
1560 * Invalidate any task's FPSIMD state that is present on this cpu.
1561 * The FPSIMD context should be acquired with get_cpu_fpsimd_context()
1562 * before calling this function.
1563 */
fpsimd_flush_cpu_state(void)1564 static void fpsimd_flush_cpu_state(void)
1565 {
1566 WARN_ON(!system_supports_fpsimd());
1567 __this_cpu_write(fpsimd_last_state.st, NULL);
1568
1569 /*
1570 * Leaving streaming mode enabled will cause issues for any kernel
1571 * NEON and leaving streaming mode or ZA enabled may increase power
1572 * consumption.
1573 */
1574 if (system_supports_sme())
1575 sme_smstop();
1576
1577 set_thread_flag(TIF_FOREIGN_FPSTATE);
1578 }
1579
fpsimd_thread_switch(struct task_struct * next)1580 void fpsimd_thread_switch(struct task_struct *next)
1581 {
1582 bool wrong_task, wrong_cpu;
1583
1584 if (!system_supports_fpsimd())
1585 return;
1586
1587 WARN_ON_ONCE(!irqs_disabled());
1588
1589 /* Save unsaved fpsimd state, if any: */
1590 if (test_thread_flag(TIF_KERNEL_FPSTATE))
1591 fpsimd_save_kernel_state(current);
1592 else
1593 fpsimd_save_user_state();
1594
1595 if (test_tsk_thread_flag(next, TIF_KERNEL_FPSTATE)) {
1596 fpsimd_flush_cpu_state();
1597 fpsimd_load_kernel_state(next);
1598 } else {
1599 /*
1600 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
1601 * state. For kernel threads, FPSIMD registers are never
1602 * loaded with user mode FPSIMD state and so wrong_task and
1603 * wrong_cpu will always be true.
1604 */
1605 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
1606 &next->thread.uw.fpsimd_state;
1607 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
1608
1609 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
1610 wrong_task || wrong_cpu);
1611 }
1612 }
1613
fpsimd_flush_thread_vl(enum vec_type type)1614 static void fpsimd_flush_thread_vl(enum vec_type type)
1615 {
1616 int vl, supported_vl;
1617
1618 /*
1619 * Reset the task vector length as required. This is where we
1620 * ensure that all user tasks have a valid vector length
1621 * configured: no kernel task can become a user task without
1622 * an exec and hence a call to this function. By the time the
1623 * first call to this function is made, all early hardware
1624 * probing is complete, so __sve_default_vl should be valid.
1625 * If a bug causes this to go wrong, we make some noise and
1626 * try to fudge thread.sve_vl to a safe value here.
1627 */
1628 vl = task_get_vl_onexec(current, type);
1629 if (!vl)
1630 vl = get_default_vl(type);
1631
1632 if (WARN_ON(!sve_vl_valid(vl)))
1633 vl = vl_info[type].min_vl;
1634
1635 supported_vl = find_supported_vector_length(type, vl);
1636 if (WARN_ON(supported_vl != vl))
1637 vl = supported_vl;
1638
1639 task_set_vl(current, type, vl);
1640
1641 /*
1642 * If the task is not set to inherit, ensure that the vector
1643 * length will be reset by a subsequent exec:
1644 */
1645 if (!test_thread_flag(vec_vl_inherit_flag(type)))
1646 task_set_vl_onexec(current, type, 0);
1647 }
1648
fpsimd_flush_thread(void)1649 void fpsimd_flush_thread(void)
1650 {
1651 struct arm64_sve_state *sve_state = NULL;
1652 struct arm64_sme_state *sme_state = NULL;
1653
1654 if (!system_supports_fpsimd())
1655 return;
1656
1657 get_cpu_fpsimd_context();
1658
1659 fpsimd_flush_task_state(current);
1660 memset(¤t->thread.uw.fpsimd_state, 0,
1661 sizeof(current->thread.uw.fpsimd_state));
1662
1663 if (system_supports_sve()) {
1664 clear_thread_flag(TIF_SVE);
1665
1666 /* Defer kfree() while in atomic context */
1667 sve_state = current->thread.sve_state;
1668 current->thread.sve_state = NULL;
1669
1670 fpsimd_flush_thread_vl(ARM64_VEC_SVE);
1671 }
1672
1673 if (system_supports_sme()) {
1674 clear_thread_flag(TIF_SME);
1675
1676 /* Defer kfree() while in atomic context */
1677 sme_state = current->thread.sme_state;
1678 current->thread.sme_state = NULL;
1679
1680 fpsimd_flush_thread_vl(ARM64_VEC_SME);
1681 current->thread.svcr = 0;
1682 }
1683
1684 if (system_supports_fpmr())
1685 current->thread.uw.fpmr = 0;
1686
1687 current->thread.fp_type = FP_STATE_FPSIMD;
1688
1689 put_cpu_fpsimd_context();
1690 kfree(sve_state);
1691 kfree(sme_state);
1692 }
1693
1694 /*
1695 * Save the userland FPSIMD state of 'current' to memory, but only if the state
1696 * currently held in the registers does in fact belong to 'current'
1697 */
fpsimd_preserve_current_state(void)1698 void fpsimd_preserve_current_state(void)
1699 {
1700 if (!system_supports_fpsimd())
1701 return;
1702
1703 get_cpu_fpsimd_context();
1704 fpsimd_save_user_state();
1705 put_cpu_fpsimd_context();
1706 }
1707
1708 /*
1709 * Associate current's FPSIMD context with this cpu
1710 * The caller must have ownership of the cpu FPSIMD context before calling
1711 * this function.
1712 */
fpsimd_bind_task_to_cpu(void)1713 static void fpsimd_bind_task_to_cpu(void)
1714 {
1715 struct cpu_fp_state *last = this_cpu_ptr(&fpsimd_last_state);
1716
1717 WARN_ON(!system_supports_fpsimd());
1718 last->st = ¤t->thread.uw.fpsimd_state;
1719 last->sve_state = current->thread.sve_state;
1720 last->sme_state = current->thread.sme_state;
1721 last->sve_vl = task_get_sve_vl(current);
1722 last->sme_vl = task_get_sme_vl(current);
1723 last->svcr = ¤t->thread.svcr;
1724 last->fpmr = ¤t->thread.uw.fpmr;
1725 last->fp_type = ¤t->thread.fp_type;
1726 last->to_save = FP_STATE_CURRENT;
1727 current->thread.fpsimd_cpu = smp_processor_id();
1728
1729 /*
1730 * Toggle SVE and SME trapping for userspace if needed, these
1731 * are serialsied by ret_to_user().
1732 */
1733 if (system_supports_sme()) {
1734 if (test_thread_flag(TIF_SME))
1735 sme_user_enable();
1736 else
1737 sme_user_disable();
1738 }
1739
1740 if (system_supports_sve()) {
1741 if (test_thread_flag(TIF_SVE))
1742 sve_user_enable();
1743 else
1744 sve_user_disable();
1745 }
1746 }
1747
fpsimd_bind_state_to_cpu(struct cpu_fp_state * state)1748 void fpsimd_bind_state_to_cpu(struct cpu_fp_state *state)
1749 {
1750 struct cpu_fp_state *last = this_cpu_ptr(&fpsimd_last_state);
1751
1752 WARN_ON(!system_supports_fpsimd());
1753 WARN_ON(!in_softirq() && !irqs_disabled());
1754
1755 *last = *state;
1756 }
1757
1758 /*
1759 * Load the userland FPSIMD state of 'current' from memory, but only if the
1760 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1761 * state of 'current'. This is called when we are preparing to return to
1762 * userspace to ensure that userspace sees a good register state.
1763 */
fpsimd_restore_current_state(void)1764 void fpsimd_restore_current_state(void)
1765 {
1766 /*
1767 * TIF_FOREIGN_FPSTATE is set on the init task and copied by
1768 * arch_dup_task_struct() regardless of whether FP/SIMD is detected.
1769 * Thus user threads can have this set even when FP/SIMD hasn't been
1770 * detected.
1771 *
1772 * When FP/SIMD is detected, begin_new_exec() will set
1773 * TIF_FOREIGN_FPSTATE via flush_thread() -> fpsimd_flush_thread(),
1774 * and fpsimd_thread_switch() will set TIF_FOREIGN_FPSTATE when
1775 * switching tasks. We detect FP/SIMD before we exec the first user
1776 * process, ensuring this has TIF_FOREIGN_FPSTATE set and
1777 * do_notify_resume() will call fpsimd_restore_current_state() to
1778 * install the user FP/SIMD context.
1779 *
1780 * When FP/SIMD is not detected, nothing else will clear or set
1781 * TIF_FOREIGN_FPSTATE prior to the first return to userspace, and
1782 * we must clear TIF_FOREIGN_FPSTATE to avoid do_notify_resume()
1783 * looping forever calling fpsimd_restore_current_state().
1784 */
1785 if (!system_supports_fpsimd()) {
1786 clear_thread_flag(TIF_FOREIGN_FPSTATE);
1787 return;
1788 }
1789
1790 get_cpu_fpsimd_context();
1791
1792 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
1793 task_fpsimd_load();
1794 fpsimd_bind_task_to_cpu();
1795 }
1796
1797 put_cpu_fpsimd_context();
1798 }
1799
fpsimd_update_current_state(struct user_fpsimd_state const * state)1800 void fpsimd_update_current_state(struct user_fpsimd_state const *state)
1801 {
1802 if (WARN_ON(!system_supports_fpsimd()))
1803 return;
1804
1805 current->thread.uw.fpsimd_state = *state;
1806 if (current->thread.fp_type == FP_STATE_SVE)
1807 fpsimd_to_sve(current);
1808 }
1809
1810 /*
1811 * Invalidate live CPU copies of task t's FPSIMD state
1812 *
1813 * This function may be called with preemption enabled. The barrier()
1814 * ensures that the assignment to fpsimd_cpu is visible to any
1815 * preemption/softirq that could race with set_tsk_thread_flag(), so
1816 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1817 *
1818 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1819 * subsequent code.
1820 */
fpsimd_flush_task_state(struct task_struct * t)1821 void fpsimd_flush_task_state(struct task_struct *t)
1822 {
1823 t->thread.fpsimd_cpu = NR_CPUS;
1824 t->thread.kernel_fpsimd_state = NULL;
1825 /*
1826 * If we don't support fpsimd, bail out after we have
1827 * reset the fpsimd_cpu for this task and clear the
1828 * FPSTATE.
1829 */
1830 if (!system_supports_fpsimd())
1831 return;
1832 barrier();
1833 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1834
1835 barrier();
1836 }
1837
fpsimd_save_and_flush_current_state(void)1838 void fpsimd_save_and_flush_current_state(void)
1839 {
1840 if (!system_supports_fpsimd())
1841 return;
1842
1843 get_cpu_fpsimd_context();
1844 fpsimd_save_user_state();
1845 fpsimd_flush_task_state(current);
1846 put_cpu_fpsimd_context();
1847 }
1848
1849 /*
1850 * Save the FPSIMD state to memory and invalidate cpu view.
1851 * This function must be called with preemption disabled.
1852 */
fpsimd_save_and_flush_cpu_state(void)1853 void fpsimd_save_and_flush_cpu_state(void)
1854 {
1855 unsigned long flags;
1856
1857 if (!system_supports_fpsimd())
1858 return;
1859 WARN_ON(preemptible());
1860 local_irq_save(flags);
1861 fpsimd_save_user_state();
1862 fpsimd_flush_cpu_state();
1863 local_irq_restore(flags);
1864 }
1865
1866 #ifdef CONFIG_KERNEL_MODE_NEON
1867
1868 /*
1869 * Kernel-side NEON support functions
1870 */
1871
1872 /*
1873 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1874 * context
1875 *
1876 * Must not be called unless may_use_simd() returns true.
1877 * Task context in the FPSIMD registers is saved back to memory as necessary.
1878 *
1879 * A matching call to kernel_neon_end() must be made before returning from the
1880 * calling context.
1881 *
1882 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1883 * called.
1884 *
1885 * Unless called from non-preemptible task context, @state must point to a
1886 * caller provided buffer that will be used to preserve the task's kernel mode
1887 * FPSIMD context when it is scheduled out, or if it is interrupted by kernel
1888 * mode FPSIMD occurring in softirq context. May be %NULL otherwise.
1889 */
kernel_neon_begin(struct user_fpsimd_state * state)1890 void kernel_neon_begin(struct user_fpsimd_state *state)
1891 {
1892 if (WARN_ON(!system_supports_fpsimd()))
1893 return;
1894
1895 WARN_ON((preemptible() || in_serving_softirq()) && !state);
1896
1897 BUG_ON(!may_use_simd());
1898
1899 get_cpu_fpsimd_context();
1900
1901 /* Save unsaved fpsimd state, if any: */
1902 if (test_thread_flag(TIF_KERNEL_FPSTATE)) {
1903 BUG_ON(IS_ENABLED(CONFIG_PREEMPT_RT) || !in_serving_softirq());
1904 fpsimd_save_state(state);
1905 } else {
1906 fpsimd_save_user_state();
1907
1908 /*
1909 * Set the thread flag so that the kernel mode FPSIMD state
1910 * will be context switched along with the rest of the task
1911 * state.
1912 *
1913 * On non-PREEMPT_RT, softirqs may interrupt task level kernel
1914 * mode FPSIMD, but the task will not be preemptible so setting
1915 * TIF_KERNEL_FPSTATE for those would be both wrong (as it
1916 * would mark the task context FPSIMD state as requiring a
1917 * context switch) and unnecessary.
1918 *
1919 * On PREEMPT_RT, softirqs are serviced from a separate thread,
1920 * which is scheduled as usual, and this guarantees that these
1921 * softirqs are not interrupting use of the FPSIMD in kernel
1922 * mode in task context. So in this case, setting the flag here
1923 * is always appropriate.
1924 */
1925 if (IS_ENABLED(CONFIG_PREEMPT_RT) || !in_serving_softirq()) {
1926 /*
1927 * Record the caller provided buffer as the kernel mode
1928 * FP/SIMD buffer for this task, so that the state can
1929 * be preserved and restored on a context switch.
1930 */
1931 WARN_ON(current->thread.kernel_fpsimd_state != NULL);
1932 current->thread.kernel_fpsimd_state = state;
1933 set_thread_flag(TIF_KERNEL_FPSTATE);
1934 }
1935 }
1936
1937 /* Invalidate any task state remaining in the fpsimd regs: */
1938 fpsimd_flush_cpu_state();
1939
1940 put_cpu_fpsimd_context();
1941 }
1942 EXPORT_SYMBOL_GPL(kernel_neon_begin);
1943
1944 /*
1945 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1946 *
1947 * Must be called from a context in which kernel_neon_begin() was previously
1948 * called, with no call to kernel_neon_end() in the meantime.
1949 *
1950 * The caller must not use the FPSIMD registers after this function is called,
1951 * unless kernel_neon_begin() is called again in the meantime.
1952 *
1953 * The value of @state must match the value passed to the preceding call to
1954 * kernel_neon_begin().
1955 */
kernel_neon_end(struct user_fpsimd_state * state)1956 void kernel_neon_end(struct user_fpsimd_state *state)
1957 {
1958 if (!system_supports_fpsimd())
1959 return;
1960
1961 if (!test_thread_flag(TIF_KERNEL_FPSTATE))
1962 return;
1963
1964 /*
1965 * If we are returning from a nested use of kernel mode FPSIMD, restore
1966 * the task context kernel mode FPSIMD state. This can only happen when
1967 * running in softirq context on non-PREEMPT_RT.
1968 */
1969 if (!IS_ENABLED(CONFIG_PREEMPT_RT) && in_serving_softirq()) {
1970 fpsimd_load_state(state);
1971 } else {
1972 clear_thread_flag(TIF_KERNEL_FPSTATE);
1973 WARN_ON(current->thread.kernel_fpsimd_state != state);
1974 current->thread.kernel_fpsimd_state = NULL;
1975 }
1976 }
1977 EXPORT_SYMBOL_GPL(kernel_neon_end);
1978
1979 #ifdef CONFIG_EFI
1980
1981 static struct user_fpsimd_state efi_fpsimd_state;
1982
1983 /*
1984 * EFI runtime services support functions
1985 *
1986 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1987 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1988 * is always used rather than being an optional accelerator.
1989 *
1990 * These functions provide the necessary support for ensuring FPSIMD
1991 * save/restore in the contexts from which EFI is used.
1992 *
1993 * Do not use them for any other purpose -- if tempted to do so, you are
1994 * either doing something wrong or you need to propose some refactoring.
1995 */
1996
1997 /*
1998 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1999 */
__efi_fpsimd_begin(void)2000 void __efi_fpsimd_begin(void)
2001 {
2002 if (!system_supports_fpsimd())
2003 return;
2004
2005 if (may_use_simd()) {
2006 kernel_neon_begin(&efi_fpsimd_state);
2007 } else {
2008 /*
2009 * We are running in hardirq or NMI context, and the only
2010 * legitimate case where this might happen is when EFI pstore
2011 * is attempting to record the system's dying gasps into EFI
2012 * variables. This could be due to an oops, a panic or a call
2013 * to emergency_restart(), and in none of those cases, we can
2014 * expect the current task to ever return to user space again,
2015 * or for the kernel to resume any normal execution, for that
2016 * matter (an oops in hardirq context triggers a panic too).
2017 *
2018 * Therefore, there is no point in attempting to preserve any
2019 * SVE/SME state here. On the off chance that we might have
2020 * ended up here for a different reason inadvertently, kill the
2021 * task and preserve/restore the base FP/SIMD state, which
2022 * might belong to kernel mode FP/SIMD.
2023 */
2024 pr_warn_ratelimited("Calling EFI runtime from %s context\n",
2025 in_nmi() ? "NMI" : "hardirq");
2026 force_signal_inject(SIGKILL, SI_KERNEL, 0, 0);
2027 fpsimd_save_state(&efi_fpsimd_state);
2028 }
2029 }
2030
2031 /*
2032 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
2033 */
__efi_fpsimd_end(void)2034 void __efi_fpsimd_end(void)
2035 {
2036 if (!system_supports_fpsimd())
2037 return;
2038
2039 if (may_use_simd()) {
2040 kernel_neon_end(&efi_fpsimd_state);
2041 } else {
2042 fpsimd_load_state(&efi_fpsimd_state);
2043 }
2044 }
2045
2046 #endif /* CONFIG_EFI */
2047
2048 #endif /* CONFIG_KERNEL_MODE_NEON */
2049
2050 #ifdef CONFIG_CPU_PM
fpsimd_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)2051 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
2052 unsigned long cmd, void *v)
2053 {
2054 switch (cmd) {
2055 case CPU_PM_ENTER:
2056 fpsimd_save_and_flush_cpu_state();
2057 break;
2058 case CPU_PM_EXIT:
2059 break;
2060 case CPU_PM_ENTER_FAILED:
2061 default:
2062 return NOTIFY_DONE;
2063 }
2064 return NOTIFY_OK;
2065 }
2066
2067 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
2068 .notifier_call = fpsimd_cpu_pm_notifier,
2069 };
2070
fpsimd_pm_init(void)2071 static void __init fpsimd_pm_init(void)
2072 {
2073 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
2074 }
2075
2076 #else
fpsimd_pm_init(void)2077 static inline void fpsimd_pm_init(void) { }
2078 #endif /* CONFIG_CPU_PM */
2079
2080 #ifdef CONFIG_HOTPLUG_CPU
fpsimd_cpu_dead(unsigned int cpu)2081 static int fpsimd_cpu_dead(unsigned int cpu)
2082 {
2083 per_cpu(fpsimd_last_state.st, cpu) = NULL;
2084 return 0;
2085 }
2086
fpsimd_hotplug_init(void)2087 static inline void fpsimd_hotplug_init(void)
2088 {
2089 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
2090 NULL, fpsimd_cpu_dead);
2091 }
2092
2093 #else
fpsimd_hotplug_init(void)2094 static inline void fpsimd_hotplug_init(void) { }
2095 #endif
2096
cpu_enable_fpsimd(const struct arm64_cpu_capabilities * __always_unused p)2097 void cpu_enable_fpsimd(const struct arm64_cpu_capabilities *__always_unused p)
2098 {
2099 unsigned long enable = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_FPEN_EL0EN;
2100 write_sysreg(read_sysreg(CPACR_EL1) | enable, CPACR_EL1);
2101 isb();
2102 }
2103
2104 /*
2105 * FP/SIMD support code initialisation.
2106 */
fpsimd_init(void)2107 static int __init fpsimd_init(void)
2108 {
2109 if (cpu_have_named_feature(FP)) {
2110 fpsimd_pm_init();
2111 fpsimd_hotplug_init();
2112 } else {
2113 pr_notice("Floating-point is not implemented\n");
2114 }
2115
2116 if (!cpu_have_named_feature(ASIMD))
2117 pr_notice("Advanced SIMD is not implemented\n");
2118
2119
2120 sve_sysctl_init();
2121 sme_sysctl_init();
2122
2123 return 0;
2124 }
2125 core_initcall(fpsimd_init);
2126