blob: 65fc87645ec66541afb773a4a54430e095cb3536 [file] [log] [blame]
Catalin Marinas53631b52012-03-05 11:49:32 +00001/*
2 * FP/SIMD context switching and fault handling
3 *
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Dave Martin7582e222017-10-31 15:51:08 +000020#include <linux/bitmap.h>
Dave Martincb84d112017-08-03 17:23:23 +010021#include <linux/bottom_half.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000022#include <linux/bug.h>
Dave Martin7582e222017-10-31 15:51:08 +000023#include <linux/cache.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000024#include <linux/compat.h>
Janet Liu32365e62015-06-11 12:02:45 +080025#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010026#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000027#include <linux/kernel.h>
Dave Martin94ef7ec2017-10-31 15:50:54 +000028#include <linux/linkage.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000029#include <linux/irqflags.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000030#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010031#include <linux/percpu.h>
Dave Martin2d2123b2017-10-31 15:51:14 +000032#include <linux/prctl.h>
Dave Martin4328825d2017-08-03 17:23:22 +010033#include <linux/preempt.h>
Dave Martin7582e222017-10-31 15:51:08 +000034#include <linux/prctl.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000035#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010036#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000037#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000038#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000039#include <linux/slab.h>
Dave Martin4ffa09a2017-10-31 15:51:15 +000040#include <linux/sysctl.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000041
42#include <asm/fpsimd.h>
43#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010044#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000045#include <asm/sigcontext.h>
46#include <asm/sysreg.h>
47#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000048
49#define FPEXC_IOF (1 << 0)
50#define FPEXC_DZF (1 << 1)
51#define FPEXC_OFF (1 << 2)
52#define FPEXC_UFF (1 << 3)
53#define FPEXC_IXF (1 << 4)
54#define FPEXC_IDF (1 << 7)
55
56/*
Dave Martinbc0ee472017-10-31 15:51:05 +000057 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
58 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020059 * In order to reduce the number of times the FPSIMD state is needlessly saved
60 * and restored, we need to keep track of two things:
61 * (a) for each task, we need to remember which CPU was the last one to have
62 * the task's FPSIMD state loaded into its FPSIMD registers;
63 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
64 * been loaded into its FPSIMD registers most recently, or whether it has
65 * been used to perform kernel mode NEON in the meantime.
66 *
67 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080068 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020069 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
70 * address of the userland FPSIMD state of the task that was loaded onto the CPU
71 * the most recently, or NULL if kernel mode NEON has been performed after that.
72 *
73 * With this in place, we no longer have to restore the next FPSIMD state right
74 * when switching between tasks. Instead, we can defer this check to userland
75 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
76 * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
77 * can omit the FPSIMD restore.
78 *
79 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
80 * indicate whether or not the userland FPSIMD state of the current task is
81 * present in the registers. The flag is set unless the FPSIMD registers of this
82 * CPU currently contain the most recent userland FPSIMD state of the current
83 * task.
84 *
Dave Martincb84d112017-08-03 17:23:23 +010085 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
86 * save the task's FPSIMD context back to task_struct from softirq context.
87 * To prevent this from racing with the manipulation of the task's FPSIMD state
88 * from task context and thereby corrupting the state, it is necessary to
89 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
90 * flag with local_bh_disable() unless softirqs are already masked.
91 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020092 * For a certain task, the sequence may look something like this:
93 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
94 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
95 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
96 * cleared, otherwise it is set;
97 *
98 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
99 * userland FPSIMD state is copied from memory to the registers, the task's
100 * fpsimd_state.cpu field is set to the id of the current CPU, the current
101 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
102 * TIF_FOREIGN_FPSTATE flag is cleared;
103 *
104 * - the task executes an ordinary syscall; upon return to userland, the
105 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
106 * restored;
107 *
108 * - the task executes a syscall which executes some NEON instructions; this is
109 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
110 * register contents to memory, clears the fpsimd_last_state per-cpu variable
111 * and sets the TIF_FOREIGN_FPSTATE flag;
112 *
113 * - the task gets preempted after kernel_neon_end() is called; as we have not
114 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
115 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
116 */
Dave Martincb968af2017-12-06 16:45:47 +0000117struct fpsimd_last_state_struct {
118 struct fpsimd_state *st;
119 bool sve_in_use;
120};
121
122static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200123
Dave Martin79ab0472017-10-31 15:51:06 +0000124/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000125static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000126
Dave Martin7582e222017-10-31 15:51:08 +0000127#ifdef CONFIG_ARM64_SVE
128
129/* Maximum supported vector length across all CPUs (initially poisoned) */
130int __ro_after_init sve_max_vl = -1;
131/* Set of available vector lengths, as vq_to_bit(vq): */
Dave Martin2e0f2472017-10-31 15:51:10 +0000132static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000133static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000134
135#else /* ! CONFIG_ARM64_SVE */
136
137/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000138extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000139extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000140
141#endif /* ! CONFIG_ARM64_SVE */
142
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200143/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000144 * Call __sve_free() directly only if you know task can't be scheduled
145 * or preempted.
146 */
147static void __sve_free(struct task_struct *task)
148{
149 kfree(task->thread.sve_state);
150 task->thread.sve_state = NULL;
151}
152
153static void sve_free(struct task_struct *task)
154{
155 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
156
157 __sve_free(task);
158}
159
160
161/* Offset of FFR in the SVE register dump */
162static size_t sve_ffr_offset(int vl)
163{
164 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
165}
166
167static void *sve_pffr(struct task_struct *task)
168{
169 return (char *)task->thread.sve_state +
170 sve_ffr_offset(task->thread.sve_vl);
171}
172
173static void change_cpacr(u64 val, u64 mask)
174{
175 u64 cpacr = read_sysreg(CPACR_EL1);
176 u64 new = (cpacr & ~mask) | val;
177
178 if (new != cpacr)
179 write_sysreg(new, CPACR_EL1);
180}
181
182static void sve_user_disable(void)
183{
184 change_cpacr(0, CPACR_EL1_ZEN_EL0EN);
185}
186
187static void sve_user_enable(void)
188{
189 change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN);
190}
191
192/*
193 * TIF_SVE controls whether a task can use SVE without trapping while
194 * in userspace, and also the way a task's FPSIMD/SVE state is stored
195 * in thread_struct.
196 *
197 * The kernel uses this flag to track whether a user task is actively
198 * using SVE, and therefore whether full SVE register state needs to
199 * be tracked. If not, the cheaper FPSIMD context handling code can
200 * be used instead of the more costly SVE equivalents.
201 *
202 * * TIF_SVE set:
203 *
204 * The task can execute SVE instructions while in userspace without
205 * trapping to the kernel.
206 *
207 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
208 * corresponding Zn), P0-P15 and FFR are encoded in in
209 * task->thread.sve_state, formatted appropriately for vector
210 * length task->thread.sve_vl.
211 *
212 * task->thread.sve_state must point to a valid buffer at least
213 * sve_state_size(task) bytes in size.
214 *
215 * During any syscall, the kernel may optionally clear TIF_SVE and
216 * discard the vector state except for the FPSIMD subset.
217 *
218 * * TIF_SVE clear:
219 *
220 * An attempt by the user task to execute an SVE instruction causes
221 * do_sve_acc() to be called, which does some preparation and then
222 * sets TIF_SVE.
223 *
224 * When stored, FPSIMD registers V0-V31 are encoded in
225 * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are
226 * logically zero but not stored anywhere; P0-P15 and FFR are not
227 * stored and have unspecified values from userspace's point of
228 * view. For hygiene purposes, the kernel zeroes them on next use,
229 * but userspace is discouraged from relying on this.
230 *
231 * task->thread.sve_state does not need to be non-NULL, valid or any
232 * particular size: it must not be dereferenced.
233 *
234 * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of
235 * whether TIF_SVE is clear or set, since these are not vector length
236 * dependent.
237 */
238
239/*
240 * Update current's FPSIMD/SVE registers from thread_struct.
241 *
242 * This function should be called only when the FPSIMD/SVE state in
243 * thread_struct is known to be up to date, when preparing to enter
244 * userspace.
245 *
246 * Softirqs (and preemption) must be disabled.
247 */
248static void task_fpsimd_load(void)
249{
250 WARN_ON(!in_softirq() && !irqs_disabled());
251
252 if (system_supports_sve() && test_thread_flag(TIF_SVE))
253 sve_load_state(sve_pffr(current),
254 &current->thread.fpsimd_state.fpsr,
255 sve_vq_from_vl(current->thread.sve_vl) - 1);
256 else
257 fpsimd_load_state(&current->thread.fpsimd_state);
258
259 if (system_supports_sve()) {
260 /* Toggle SVE trapping for userspace if needed */
261 if (test_thread_flag(TIF_SVE))
262 sve_user_enable();
263 else
264 sve_user_disable();
265
266 /* Serialised by exception return to user */
267 }
268}
269
270/*
271 * Ensure current's FPSIMD/SVE storage in thread_struct is up to date
272 * with respect to the CPU registers.
273 *
274 * Softirqs (and preemption) must be disabled.
275 */
276static void task_fpsimd_save(void)
277{
278 WARN_ON(!in_softirq() && !irqs_disabled());
279
280 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
281 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
282 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
283 /*
284 * Can't save the user regs, so current would
285 * re-enter user with corrupt state.
286 * There's no way to recover, so kill it:
287 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000288 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000289 return;
290 }
291
292 sve_save_state(sve_pffr(current),
293 &current->thread.fpsimd_state.fpsr);
294 } else
295 fpsimd_save_state(&current->thread.fpsimd_state);
296 }
297}
298
Dave Martin7582e222017-10-31 15:51:08 +0000299/*
300 * Helpers to translate bit indices in sve_vq_map to VQ values (and
301 * vice versa). This allows find_next_bit() to be used to find the
302 * _maximum_ VQ not exceeding a certain value.
303 */
304
305static unsigned int vq_to_bit(unsigned int vq)
306{
307 return SVE_VQ_MAX - vq;
308}
309
310static unsigned int bit_to_vq(unsigned int bit)
311{
312 if (WARN_ON(bit >= SVE_VQ_MAX))
313 bit = SVE_VQ_MAX - 1;
314
315 return SVE_VQ_MAX - bit;
316}
317
318/*
319 * All vector length selection from userspace comes through here.
320 * We're on a slow path, so some sanity-checks are included.
321 * If things go wrong there's a bug somewhere, but try to fall back to a
322 * safe choice.
323 */
324static unsigned int find_supported_vector_length(unsigned int vl)
325{
326 int bit;
327 int max_vl = sve_max_vl;
328
329 if (WARN_ON(!sve_vl_valid(vl)))
330 vl = SVE_VL_MIN;
331
332 if (WARN_ON(!sve_vl_valid(max_vl)))
333 max_vl = SVE_VL_MIN;
334
335 if (vl > max_vl)
336 vl = max_vl;
337
338 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
339 vq_to_bit(sve_vq_from_vl(vl)));
340 return sve_vl_from_vq(bit_to_vq(bit));
341}
342
Dave Martin4ffa09a2017-10-31 15:51:15 +0000343#ifdef CONFIG_SYSCTL
344
345static int sve_proc_do_default_vl(struct ctl_table *table, int write,
346 void __user *buffer, size_t *lenp,
347 loff_t *ppos)
348{
349 int ret;
350 int vl = sve_default_vl;
351 struct ctl_table tmp_table = {
352 .data = &vl,
353 .maxlen = sizeof(vl),
354 };
355
356 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
357 if (ret || !write)
358 return ret;
359
360 /* Writing -1 has the special meaning "set to max": */
361 if (vl == -1) {
362 /* Fail safe if sve_max_vl wasn't initialised */
363 if (WARN_ON(!sve_vl_valid(sve_max_vl)))
364 vl = SVE_VL_MIN;
365 else
366 vl = sve_max_vl;
367
368 goto chosen;
369 }
370
371 if (!sve_vl_valid(vl))
372 return -EINVAL;
373
374 vl = find_supported_vector_length(vl);
375chosen:
376 sve_default_vl = vl;
377 return 0;
378}
379
380static struct ctl_table sve_default_vl_table[] = {
381 {
382 .procname = "sve_default_vector_length",
383 .mode = 0644,
384 .proc_handler = sve_proc_do_default_vl,
385 },
386 { }
387};
388
389static int __init sve_sysctl_init(void)
390{
391 if (system_supports_sve())
392 if (!register_sysctl("abi", sve_default_vl_table))
393 return -EINVAL;
394
395 return 0;
396}
397
398#else /* ! CONFIG_SYSCTL */
399static int __init sve_sysctl_init(void) { return 0; }
400#endif /* ! CONFIG_SYSCTL */
401
Dave Martinbc0ee472017-10-31 15:51:05 +0000402#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
403 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
404
405/*
406 * Transfer the FPSIMD state in task->thread.fpsimd_state to
407 * task->thread.sve_state.
408 *
409 * Task can be a non-runnable task, or current. In the latter case,
410 * softirqs (and preemption) must be disabled.
411 * task->thread.sve_state must point to at least sve_state_size(task)
412 * bytes of allocated kernel memory.
413 * task->thread.fpsimd_state must be up to date before calling this function.
414 */
415static void fpsimd_to_sve(struct task_struct *task)
416{
417 unsigned int vq;
418 void *sst = task->thread.sve_state;
419 struct fpsimd_state const *fst = &task->thread.fpsimd_state;
420 unsigned int i;
421
422 if (!system_supports_sve())
423 return;
424
425 vq = sve_vq_from_vl(task->thread.sve_vl);
426 for (i = 0; i < 32; ++i)
427 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
428 sizeof(fst->vregs[i]));
429}
430
Dave Martin8cd969d2017-10-31 15:51:07 +0000431/*
432 * Transfer the SVE state in task->thread.sve_state to
433 * task->thread.fpsimd_state.
434 *
435 * Task can be a non-runnable task, or current. In the latter case,
436 * softirqs (and preemption) must be disabled.
437 * task->thread.sve_state must point to at least sve_state_size(task)
438 * bytes of allocated kernel memory.
439 * task->thread.sve_state must be up to date before calling this function.
440 */
441static void sve_to_fpsimd(struct task_struct *task)
442{
443 unsigned int vq;
444 void const *sst = task->thread.sve_state;
445 struct fpsimd_state *fst = &task->thread.fpsimd_state;
446 unsigned int i;
447
448 if (!system_supports_sve())
449 return;
450
451 vq = sve_vq_from_vl(task->thread.sve_vl);
452 for (i = 0; i < 32; ++i)
453 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
454 sizeof(fst->vregs[i]));
455}
456
Dave Martinbc0ee472017-10-31 15:51:05 +0000457#ifdef CONFIG_ARM64_SVE
458
459/*
460 * Return how many bytes of memory are required to store the full SVE
461 * state for task, given task's currently configured vector length.
462 */
463size_t sve_state_size(struct task_struct const *task)
464{
465 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
466}
467
468/*
469 * Ensure that task->thread.sve_state is allocated and sufficiently large.
470 *
471 * This function should be used only in preparation for replacing
472 * task->thread.sve_state with new data. The memory is always zeroed
473 * here to prevent stale data from showing through: this is done in
474 * the interest of testability and predictability: except in the
475 * do_sve_acc() case, there is no ABI requirement to hide stale data
476 * written previously be task.
477 */
478void sve_alloc(struct task_struct *task)
479{
480 if (task->thread.sve_state) {
481 memset(task->thread.sve_state, 0, sve_state_size(current));
482 return;
483 }
484
485 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
486 task->thread.sve_state =
487 kzalloc(sve_state_size(task), GFP_KERNEL);
488
489 /*
490 * If future SVE revisions can have larger vectors though,
491 * this may cease to be true:
492 */
493 BUG_ON(!task->thread.sve_state);
494}
495
Dave Martin43d4da2c42017-10-31 15:51:13 +0000496
497/*
498 * Ensure that task->thread.sve_state is up to date with respect to
499 * the user task, irrespective of when SVE is in use or not.
500 *
501 * This should only be called by ptrace. task must be non-runnable.
502 * task->thread.sve_state must point to at least sve_state_size(task)
503 * bytes of allocated kernel memory.
504 */
505void fpsimd_sync_to_sve(struct task_struct *task)
506{
507 if (!test_tsk_thread_flag(task, TIF_SVE))
508 fpsimd_to_sve(task);
509}
510
511/*
512 * Ensure that task->thread.fpsimd_state is up to date with respect to
513 * the user task, irrespective of whether SVE is in use or not.
514 *
515 * This should only be called by ptrace. task must be non-runnable.
516 * task->thread.sve_state must point to at least sve_state_size(task)
517 * bytes of allocated kernel memory.
518 */
519void sve_sync_to_fpsimd(struct task_struct *task)
520{
521 if (test_tsk_thread_flag(task, TIF_SVE))
522 sve_to_fpsimd(task);
523}
524
525/*
526 * Ensure that task->thread.sve_state is up to date with respect to
527 * the task->thread.fpsimd_state.
528 *
529 * This should only be called by ptrace to merge new FPSIMD register
530 * values into a task for which SVE is currently active.
531 * task must be non-runnable.
532 * task->thread.sve_state must point to at least sve_state_size(task)
533 * bytes of allocated kernel memory.
534 * task->thread.fpsimd_state must already have been initialised with
535 * the new FPSIMD register values to be merged in.
536 */
537void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
538{
539 unsigned int vq;
540 void *sst = task->thread.sve_state;
541 struct fpsimd_state const *fst = &task->thread.fpsimd_state;
542 unsigned int i;
543
544 if (!test_tsk_thread_flag(task, TIF_SVE))
545 return;
546
547 vq = sve_vq_from_vl(task->thread.sve_vl);
548
549 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
550
551 for (i = 0; i < 32; ++i)
552 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
553 sizeof(fst->vregs[i]));
554}
555
Dave Martin7582e222017-10-31 15:51:08 +0000556int sve_set_vector_length(struct task_struct *task,
557 unsigned long vl, unsigned long flags)
558{
559 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
560 PR_SVE_SET_VL_ONEXEC))
561 return -EINVAL;
562
563 if (!sve_vl_valid(vl))
564 return -EINVAL;
565
566 /*
567 * Clamp to the maximum vector length that VL-agnostic SVE code can
568 * work with. A flag may be assigned in the future to allow setting
569 * of larger vector lengths without confusing older software.
570 */
571 if (vl > SVE_VL_ARCH_MAX)
572 vl = SVE_VL_ARCH_MAX;
573
574 vl = find_supported_vector_length(vl);
575
576 if (flags & (PR_SVE_VL_INHERIT |
577 PR_SVE_SET_VL_ONEXEC))
578 task->thread.sve_vl_onexec = vl;
579 else
580 /* Reset VL to system default on next exec: */
581 task->thread.sve_vl_onexec = 0;
582
583 /* Only actually set the VL if not deferred: */
584 if (flags & PR_SVE_SET_VL_ONEXEC)
585 goto out;
586
587 if (vl == task->thread.sve_vl)
588 goto out;
589
590 /*
591 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
592 * write any live register state back to task_struct, and convert to a
593 * non-SVE thread.
594 */
595 if (task == current) {
596 local_bh_disable();
597
598 task_fpsimd_save();
599 set_thread_flag(TIF_FOREIGN_FPSTATE);
600 }
601
602 fpsimd_flush_task_state(task);
603 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
604 sve_to_fpsimd(task);
605
606 if (task == current)
607 local_bh_enable();
608
609 /*
610 * Force reallocation of task SVE state to the correct size
611 * on next use:
612 */
613 sve_free(task);
614
615 task->thread.sve_vl = vl;
616
617out:
618 if (flags & PR_SVE_VL_INHERIT)
619 set_tsk_thread_flag(task, TIF_SVE_VL_INHERIT);
620 else
621 clear_tsk_thread_flag(task, TIF_SVE_VL_INHERIT);
622
623 return 0;
624}
625
Dave Martinbc0ee472017-10-31 15:51:05 +0000626/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000627 * Encode the current vector length and flags for return.
628 * This is only required for prctl(): ptrace has separate fields
629 *
630 * flags are as for sve_set_vector_length().
631 */
632static int sve_prctl_status(unsigned long flags)
633{
634 int ret;
635
636 if (flags & PR_SVE_SET_VL_ONEXEC)
637 ret = current->thread.sve_vl_onexec;
638 else
639 ret = current->thread.sve_vl;
640
641 if (test_thread_flag(TIF_SVE_VL_INHERIT))
642 ret |= PR_SVE_VL_INHERIT;
643
644 return ret;
645}
646
647/* PR_SVE_SET_VL */
648int sve_set_current_vl(unsigned long arg)
649{
650 unsigned long vl, flags;
651 int ret;
652
653 vl = arg & PR_SVE_VL_LEN_MASK;
654 flags = arg & ~vl;
655
656 if (!system_supports_sve())
657 return -EINVAL;
658
659 ret = sve_set_vector_length(current, vl, flags);
660 if (ret)
661 return ret;
662
663 return sve_prctl_status(flags);
664}
665
666/* PR_SVE_GET_VL */
667int sve_get_current_vl(void)
668{
669 if (!system_supports_sve())
670 return -EINVAL;
671
672 return sve_prctl_status(0);
673}
674
675/*
Dave Martin2e0f2472017-10-31 15:51:10 +0000676 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
677 * during secondary boot.
678 */
679static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
680
681static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
682{
683 unsigned int vq, vl;
684 unsigned long zcr;
685
686 bitmap_zero(map, SVE_VQ_MAX);
687
688 zcr = ZCR_ELx_LEN_MASK;
689 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
690
691 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
692 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
693 vl = sve_get_vl();
694 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
695 set_bit(vq_to_bit(vq), map);
696 }
697}
698
699void __init sve_init_vq_map(void)
700{
701 sve_probe_vqs(sve_vq_map);
702}
703
704/*
705 * If we haven't committed to the set of supported VQs yet, filter out
706 * those not supported by the current CPU.
707 */
708void sve_update_vq_map(void)
709{
710 sve_probe_vqs(sve_secondary_vq_map);
711 bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
712}
713
714/* Check whether the current CPU supports all VQs in the committed set */
715int sve_verify_vq_map(void)
716{
717 int ret = 0;
718
719 sve_probe_vqs(sve_secondary_vq_map);
720 bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
721 SVE_VQ_MAX);
722 if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
723 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
724 smp_processor_id());
725 ret = -EINVAL;
726 }
727
728 return ret;
729}
730
Dave Martinfdfa9762017-10-31 15:51:12 +0000731static void __init sve_efi_setup(void)
732{
733 if (!IS_ENABLED(CONFIG_EFI))
734 return;
735
736 /*
737 * alloc_percpu() warns and prints a backtrace if this goes wrong.
738 * This is evidence of a crippled system and we are returning void,
739 * so no attempt is made to handle this situation here.
740 */
741 if (!sve_vl_valid(sve_max_vl))
742 goto fail;
743
744 efi_sve_state = __alloc_percpu(
745 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
746 if (!efi_sve_state)
747 goto fail;
748
749 return;
750
751fail:
752 panic("Cannot allocate percpu memory for EFI SVE save/restore");
753}
754
Dave Martin2e0f2472017-10-31 15:51:10 +0000755/*
756 * Enable SVE for EL1.
757 * Intended for use by the cpufeatures code during CPU boot.
758 */
759int sve_kernel_enable(void *__always_unused p)
760{
761 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
762 isb();
763
764 return 0;
765}
766
767void __init sve_setup(void)
768{
769 u64 zcr;
770
771 if (!system_supports_sve())
772 return;
773
774 /*
775 * The SVE architecture mandates support for 128-bit vectors,
776 * so sve_vq_map must have at least SVE_VQ_MIN set.
777 * If something went wrong, at least try to patch it up:
778 */
779 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
780 set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
781
782 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
783 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
784
785 /*
786 * Sanity-check that the max VL we determined through CPU features
787 * corresponds properly to sve_vq_map. If not, do our best:
788 */
789 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
790 sve_max_vl = find_supported_vector_length(sve_max_vl);
791
792 /*
793 * For the default VL, pick the maximum supported value <= 64.
794 * VL == 64 is guaranteed not to grow the signal frame.
795 */
796 sve_default_vl = find_supported_vector_length(64);
797
798 pr_info("SVE: maximum available vector length %u bytes per vector\n",
799 sve_max_vl);
800 pr_info("SVE: default vector length %u bytes per vector\n",
801 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000802
803 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000804}
805
806/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000807 * Called from the put_task_struct() path, which cannot get here
808 * unless dead_task is really dead and not schedulable.
809 */
810void fpsimd_release_task(struct task_struct *dead_task)
811{
812 __sve_free(dead_task);
813}
814
815#endif /* CONFIG_ARM64_SVE */
816
817/*
818 * Trapped SVE access
819 *
820 * Storage is allocated for the full SVE state, the current FPSIMD
821 * register contents are migrated across, and TIF_SVE is set so that
822 * the SVE access trap will be disabled the next time this task
823 * reaches ret_to_user.
824 *
825 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
826 * would have disabled the SVE access trap for userspace during
827 * ret_to_user, making an SVE access trap impossible in that case.
828 */
829asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
830{
831 /* Even if we chose not to use SVE, the hardware could still trap: */
832 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000833 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000834 return;
835 }
836
837 sve_alloc(current);
838
839 local_bh_disable();
840
841 task_fpsimd_save();
842 fpsimd_to_sve(current);
843
844 /* Force ret_to_user to reload the registers: */
845 fpsimd_flush_task_state(current);
846 set_thread_flag(TIF_FOREIGN_FPSTATE);
847
848 if (test_and_set_thread_flag(TIF_SVE))
849 WARN_ON(1); /* SVE access shouldn't have trapped */
850
851 local_bh_enable();
852}
853
Catalin Marinas53631b52012-03-05 11:49:32 +0000854/*
855 * Trapped FP/ASIMD access.
856 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000857asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000858{
859 /* TODO: implement lazy context saving/restoring */
860 WARN_ON(1);
861}
862
863/*
864 * Raise a SIGFPE for the current process.
865 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000866asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000867{
868 siginfo_t info;
Eric W. Biederman526c3dd2018-01-03 18:07:12 -0600869 unsigned int si_code = FPE_FIXME;
Catalin Marinas53631b52012-03-05 11:49:32 +0000870
871 if (esr & FPEXC_IOF)
872 si_code = FPE_FLTINV;
873 else if (esr & FPEXC_DZF)
874 si_code = FPE_FLTDIV;
875 else if (esr & FPEXC_OFF)
876 si_code = FPE_FLTOVF;
877 else if (esr & FPEXC_UFF)
878 si_code = FPE_FLTUND;
879 else if (esr & FPEXC_IXF)
880 si_code = FPE_FLTRES;
881
882 memset(&info, 0, sizeof(info));
883 info.si_signo = SIGFPE;
884 info.si_code = si_code;
885 info.si_addr = (void __user *)instruction_pointer(regs);
886
887 send_sig_info(SIGFPE, &info, current);
888}
889
890void fpsimd_thread_switch(struct task_struct *next)
891{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000892 if (!system_supports_fpsimd())
893 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200894 /*
895 * Save the current FPSIMD state to memory, but only if whatever is in
896 * the registers is in fact the most recent userland FPSIMD state of
897 * 'current'.
898 */
Dave Martinbc0ee472017-10-31 15:51:05 +0000899 if (current->mm)
900 task_fpsimd_save();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200901
902 if (next->mm) {
903 /*
904 * If we are switching to a task whose most recent userland
905 * FPSIMD state is already in the registers of *this* cpu,
906 * we can skip loading the state from memory. Otherwise, set
907 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
908 * upon the next return to userland.
909 */
910 struct fpsimd_state *st = &next->thread.fpsimd_state;
911
Dave Martincb968af2017-12-06 16:45:47 +0000912 if (__this_cpu_read(fpsimd_last_state.st) == st
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200913 && st->cpu == smp_processor_id())
Dave Martin9cf5b542017-10-31 15:50:59 +0000914 clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200915 else
Dave Martin9cf5b542017-10-31 15:50:59 +0000916 set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200917 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000918}
919
920void fpsimd_flush_thread(void)
921{
Dave Martin7582e222017-10-31 15:51:08 +0000922 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000923
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000924 if (!system_supports_fpsimd())
925 return;
Dave Martincb84d112017-08-03 17:23:23 +0100926
927 local_bh_disable();
928
Catalin Marinas53631b52012-03-05 11:49:32 +0000929 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100930 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000931
932 if (system_supports_sve()) {
933 clear_thread_flag(TIF_SVE);
934 sve_free(current);
935
936 /*
937 * Reset the task vector length as required.
938 * This is where we ensure that all user tasks have a valid
939 * vector length configured: no kernel task can become a user
940 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000941 * By the time the first call to this function is made, all
942 * early hardware probing is complete, so sve_default_vl
943 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000944 * If a bug causes this to go wrong, we make some noise and
945 * try to fudge thread.sve_vl to a safe value here.
946 */
Dave Martin79ab0472017-10-31 15:51:06 +0000947 vl = current->thread.sve_vl_onexec ?
948 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000949
950 if (WARN_ON(!sve_vl_valid(vl)))
951 vl = SVE_VL_MIN;
952
Dave Martin7582e222017-10-31 15:51:08 +0000953 supported_vl = find_supported_vector_length(vl);
954 if (WARN_ON(supported_vl != vl))
955 vl = supported_vl;
956
Dave Martinbc0ee472017-10-31 15:51:05 +0000957 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000958
959 /*
960 * If the task is not set to inherit, ensure that the vector
961 * length will be reset by a subsequent exec:
962 */
963 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
964 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000965 }
966
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200967 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100968
969 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000970}
971
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100972/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200973 * Save the userland FPSIMD state of 'current' to memory, but only if the state
974 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100975 */
976void fpsimd_preserve_current_state(void)
977{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000978 if (!system_supports_fpsimd())
979 return;
Dave Martincb84d112017-08-03 17:23:23 +0100980
981 local_bh_disable();
Dave Martin8cd969d2017-10-31 15:51:07 +0000982 task_fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100983 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100984}
985
986/*
Dave Martin8cd969d2017-10-31 15:51:07 +0000987 * Like fpsimd_preserve_current_state(), but ensure that
988 * current->thread.fpsimd_state is updated so that it can be copied to
989 * the signal frame.
990 */
991void fpsimd_signal_preserve_current_state(void)
992{
993 fpsimd_preserve_current_state();
994 if (system_supports_sve() && test_thread_flag(TIF_SVE))
995 sve_to_fpsimd(current);
996}
997
998/*
Dave Martin8884b7b2017-12-06 16:45:46 +0000999 * Associate current's FPSIMD context with this cpu
1000 * Preemption must be disabled when calling this function.
1001 */
1002static void fpsimd_bind_to_cpu(void)
1003{
Dave Martincb968af2017-12-06 16:45:47 +00001004 struct fpsimd_last_state_struct *last =
1005 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +00001006 struct fpsimd_state *st = &current->thread.fpsimd_state;
1007
Dave Martincb968af2017-12-06 16:45:47 +00001008 last->st = st;
1009 last->sve_in_use = test_thread_flag(TIF_SVE);
Dave Martin8884b7b2017-12-06 16:45:46 +00001010 st->cpu = smp_processor_id();
1011}
1012
1013/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001014 * Load the userland FPSIMD state of 'current' from memory, but only if the
1015 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1016 * state of 'current'
1017 */
1018void fpsimd_restore_current_state(void)
1019{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001020 if (!system_supports_fpsimd())
1021 return;
Dave Martincb84d112017-08-03 17:23:23 +01001022
1023 local_bh_disable();
1024
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001025 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001026 task_fpsimd_load();
Dave Martin8884b7b2017-12-06 16:45:46 +00001027 fpsimd_bind_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001028 }
Dave Martincb84d112017-08-03 17:23:23 +01001029
1030 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001031}
1032
1033/*
1034 * Load an updated userland FPSIMD state for 'current' from memory and set the
1035 * flag that indicates that the FPSIMD register contents are the most recent
1036 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001037 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001038void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001039{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001040 if (!system_supports_fpsimd())
1041 return;
Dave Martincb84d112017-08-03 17:23:23 +01001042
1043 local_bh_disable();
1044
Dave Martin0abdeff2017-12-15 18:34:38 +00001045 current->thread.fpsimd_state.user_fpsimd = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001046 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001047 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001048
Dave Martin8cd969d2017-10-31 15:51:07 +00001049 task_fpsimd_load();
1050
Dave Martin8884b7b2017-12-06 16:45:46 +00001051 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE))
1052 fpsimd_bind_to_cpu();
Dave Martincb84d112017-08-03 17:23:23 +01001053
1054 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001055}
1056
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001057/*
1058 * Invalidate live CPU copies of task t's FPSIMD state
1059 */
1060void fpsimd_flush_task_state(struct task_struct *t)
1061{
1062 t->thread.fpsimd_state.cpu = NR_CPUS;
1063}
1064
Dave Martin17eed272017-10-31 15:51:16 +00001065static inline void fpsimd_flush_cpu_state(void)
1066{
Dave Martincb968af2017-12-06 16:45:47 +00001067 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martin17eed272017-10-31 15:51:16 +00001068}
1069
1070/*
1071 * Invalidate any task SVE state currently held in this CPU's regs.
1072 *
1073 * This is used to prevent the kernel from trying to reuse SVE register data
1074 * that is detroyed by KVM guest enter/exit. This function should go away when
1075 * KVM SVE support is implemented. Don't use it for anything else.
1076 */
1077#ifdef CONFIG_ARM64_SVE
1078void sve_flush_cpu_state(void)
1079{
Dave Martincb968af2017-12-06 16:45:47 +00001080 struct fpsimd_last_state_struct const *last =
1081 this_cpu_ptr(&fpsimd_last_state);
Dave Martin17eed272017-10-31 15:51:16 +00001082
Dave Martincb968af2017-12-06 16:45:47 +00001083 if (last->st && last->sve_in_use)
Dave Martin17eed272017-10-31 15:51:16 +00001084 fpsimd_flush_cpu_state();
1085}
1086#endif /* CONFIG_ARM64_SVE */
1087
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001088#ifdef CONFIG_KERNEL_MODE_NEON
1089
Dave Martincb84d112017-08-03 17:23:23 +01001090DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001091EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001092
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001093/*
1094 * Kernel-side NEON support functions
1095 */
Dave Martincb84d112017-08-03 17:23:23 +01001096
1097/*
1098 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1099 * context
1100 *
1101 * Must not be called unless may_use_simd() returns true.
1102 * Task context in the FPSIMD registers is saved back to memory as necessary.
1103 *
1104 * A matching call to kernel_neon_end() must be made before returning from the
1105 * calling context.
1106 *
1107 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1108 * called.
1109 */
1110void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001111{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001112 if (WARN_ON(!system_supports_fpsimd()))
1113 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001114
Dave Martincb84d112017-08-03 17:23:23 +01001115 BUG_ON(!may_use_simd());
1116
1117 local_bh_disable();
1118
1119 __this_cpu_write(kernel_neon_busy, true);
1120
1121 /* Save unsaved task fpsimd state, if any: */
Dave Martin1bd3f932017-10-31 15:51:11 +00001122 if (current->mm) {
1123 task_fpsimd_save();
1124 set_thread_flag(TIF_FOREIGN_FPSTATE);
1125 }
Dave Martincb84d112017-08-03 17:23:23 +01001126
1127 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001128 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001129
1130 preempt_disable();
1131
1132 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001133}
Dave Martincb84d112017-08-03 17:23:23 +01001134EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001135
Dave Martincb84d112017-08-03 17:23:23 +01001136/*
1137 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1138 *
1139 * Must be called from a context in which kernel_neon_begin() was previously
1140 * called, with no call to kernel_neon_end() in the meantime.
1141 *
1142 * The caller must not use the FPSIMD registers after this function is called,
1143 * unless kernel_neon_begin() is called again in the meantime.
1144 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001145void kernel_neon_end(void)
1146{
Dave Martincb84d112017-08-03 17:23:23 +01001147 bool busy;
1148
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001149 if (!system_supports_fpsimd())
1150 return;
Dave Martincb84d112017-08-03 17:23:23 +01001151
1152 busy = __this_cpu_xchg(kernel_neon_busy, false);
1153 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1154
1155 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001156}
1157EXPORT_SYMBOL(kernel_neon_end);
1158
Dave Martine580b8b2017-09-18 09:40:12 +01001159#ifdef CONFIG_EFI
1160
Dave Martin3b660232017-08-18 14:53:47 +01001161static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
1162static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001163static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001164
1165/*
1166 * EFI runtime services support functions
1167 *
1168 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1169 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1170 * is always used rather than being an optional accelerator.
1171 *
1172 * These functions provide the necessary support for ensuring FPSIMD
1173 * save/restore in the contexts from which EFI is used.
1174 *
1175 * Do not use them for any other purpose -- if tempted to do so, you are
1176 * either doing something wrong or you need to propose some refactoring.
1177 */
1178
1179/*
1180 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1181 */
1182void __efi_fpsimd_begin(void)
1183{
1184 if (!system_supports_fpsimd())
1185 return;
1186
1187 WARN_ON(preemptible());
1188
Dave Martinfdfa9762017-10-31 15:51:12 +00001189 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001190 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001191 } else {
1192 /*
1193 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1194 * preserving:
1195 */
1196 if (system_supports_sve() && likely(efi_sve_state)) {
1197 char *sve_state = this_cpu_ptr(efi_sve_state);
1198
1199 __this_cpu_write(efi_sve_state_used, true);
1200
1201 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1202 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1203 } else {
1204 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1205 }
1206
Dave Martin4328825d2017-08-03 17:23:22 +01001207 __this_cpu_write(efi_fpsimd_state_used, true);
1208 }
1209}
1210
1211/*
1212 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1213 */
1214void __efi_fpsimd_end(void)
1215{
1216 if (!system_supports_fpsimd())
1217 return;
1218
Dave Martinfdfa9762017-10-31 15:51:12 +00001219 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001220 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001221 } else {
1222 if (system_supports_sve() &&
1223 likely(__this_cpu_read(efi_sve_state_used))) {
1224 char const *sve_state = this_cpu_ptr(efi_sve_state);
1225
1226 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1227 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1228 sve_vq_from_vl(sve_get_vl()) - 1);
1229
1230 __this_cpu_write(efi_sve_state_used, false);
1231 } else {
1232 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1233 }
1234 }
Dave Martin4328825d2017-08-03 17:23:22 +01001235}
1236
Dave Martine580b8b2017-09-18 09:40:12 +01001237#endif /* CONFIG_EFI */
1238
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001239#endif /* CONFIG_KERNEL_MODE_NEON */
1240
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001241#ifdef CONFIG_CPU_PM
1242static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1243 unsigned long cmd, void *v)
1244{
1245 switch (cmd) {
1246 case CPU_PM_ENTER:
Dave Martinbc0ee472017-10-31 15:51:05 +00001247 if (current->mm)
1248 task_fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001249 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001250 break;
1251 case CPU_PM_EXIT:
1252 if (current->mm)
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001253 set_thread_flag(TIF_FOREIGN_FPSTATE);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001254 break;
1255 case CPU_PM_ENTER_FAILED:
1256 default:
1257 return NOTIFY_DONE;
1258 }
1259 return NOTIFY_OK;
1260}
1261
1262static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1263 .notifier_call = fpsimd_cpu_pm_notifier,
1264};
1265
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001266static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001267{
1268 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1269}
1270
1271#else
1272static inline void fpsimd_pm_init(void) { }
1273#endif /* CONFIG_CPU_PM */
1274
Janet Liu32365e62015-06-11 12:02:45 +08001275#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001276static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001277{
Dave Martincb968af2017-12-06 16:45:47 +00001278 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001279 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001280}
1281
Janet Liu32365e62015-06-11 12:02:45 +08001282static inline void fpsimd_hotplug_init(void)
1283{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001284 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1285 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001286}
1287
1288#else
1289static inline void fpsimd_hotplug_init(void) { }
1290#endif
1291
Catalin Marinas53631b52012-03-05 11:49:32 +00001292/*
1293 * FP/SIMD support code initialisation.
1294 */
1295static int __init fpsimd_init(void)
1296{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001297 if (elf_hwcap & HWCAP_FP) {
1298 fpsimd_pm_init();
1299 fpsimd_hotplug_init();
1300 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001301 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001302 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001303
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001304 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001305 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001306
Dave Martin4ffa09a2017-10-31 15:51:15 +00001307 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001308}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001309core_initcall(fpsimd_init);