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