blob: f59ea677cd42d759d0a61bba753b3db9eedf7b2e [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 Martinbc0ee472017-10-31 15:51:05 +000034#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010035#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000036#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000037#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000038#include <linux/slab.h>
Dave Martin31dc52b2018-04-12 16:47:20 +010039#include <linux/stddef.h>
Dave Martin4ffa09a2017-10-31 15:51:15 +000040#include <linux/sysctl.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000041
Dave Martinaf4a81b2018-03-01 17:44:07 +000042#include <asm/esr.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000043#include <asm/fpsimd.h>
Dave Martinc0cda3b2018-03-26 15:12:28 +010044#include <asm/cpufeature.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000045#include <asm/cputype.h>
Dave Martin2cf97d42018-04-12 17:04:39 +010046#include <asm/processor.h>
Dave Martin4328825d2017-08-03 17:23:22 +010047#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000048#include <asm/sigcontext.h>
49#include <asm/sysreg.h>
50#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000051
52#define FPEXC_IOF (1 << 0)
53#define FPEXC_DZF (1 << 1)
54#define FPEXC_OFF (1 << 2)
55#define FPEXC_UFF (1 << 3)
56#define FPEXC_IXF (1 << 4)
57#define FPEXC_IDF (1 << 7)
58
59/*
Dave Martinbc0ee472017-10-31 15:51:05 +000060 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
61 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020062 * In order to reduce the number of times the FPSIMD state is needlessly saved
63 * and restored, we need to keep track of two things:
64 * (a) for each task, we need to remember which CPU was the last one to have
65 * the task's FPSIMD state loaded into its FPSIMD registers;
66 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
67 * been loaded into its FPSIMD registers most recently, or whether it has
68 * been used to perform kernel mode NEON in the meantime.
69 *
Dave Martin20b85472018-03-28 10:50:48 +010070 * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080071 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020072 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
73 * address of the userland FPSIMD state of the task that was loaded onto the CPU
74 * the most recently, or NULL if kernel mode NEON has been performed after that.
75 *
76 * With this in place, we no longer have to restore the next FPSIMD state right
77 * when switching between tasks. Instead, we can defer this check to userland
78 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
Dave Martin20b85472018-03-28 10:50:48 +010079 * task's fpsimd_cpu are still mutually in sync. If this is the case, we
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020080 * can omit the FPSIMD restore.
81 *
82 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
83 * indicate whether or not the userland FPSIMD state of the current task is
84 * present in the registers. The flag is set unless the FPSIMD registers of this
85 * CPU currently contain the most recent userland FPSIMD state of the current
86 * task.
87 *
Dave Martincb84d112017-08-03 17:23:23 +010088 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
89 * save the task's FPSIMD context back to task_struct from softirq context.
90 * To prevent this from racing with the manipulation of the task's FPSIMD state
91 * from task context and thereby corrupting the state, it is necessary to
92 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
93 * flag with local_bh_disable() unless softirqs are already masked.
94 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020095 * For a certain task, the sequence may look something like this:
Dave Martin20b85472018-03-28 10:50:48 +010096 * - the task gets scheduled in; if both the task's fpsimd_cpu field
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020097 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
98 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
99 * cleared, otherwise it is set;
100 *
101 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
102 * userland FPSIMD state is copied from memory to the registers, the task's
Dave Martin20b85472018-03-28 10:50:48 +0100103 * fpsimd_cpu field is set to the id of the current CPU, the current
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200104 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
105 * TIF_FOREIGN_FPSTATE flag is cleared;
106 *
107 * - the task executes an ordinary syscall; upon return to userland, the
108 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
109 * restored;
110 *
111 * - the task executes a syscall which executes some NEON instructions; this is
112 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
113 * register contents to memory, clears the fpsimd_last_state per-cpu variable
114 * and sets the TIF_FOREIGN_FPSTATE flag;
115 *
116 * - the task gets preempted after kernel_neon_end() is called; as we have not
117 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
118 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
119 */
Dave Martincb968af2017-12-06 16:45:47 +0000120struct fpsimd_last_state_struct {
Dave Martin20b85472018-03-28 10:50:48 +0100121 struct user_fpsimd_state *st;
Dave Martincb968af2017-12-06 16:45:47 +0000122};
123
124static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200125
Dave Martin79ab0472017-10-31 15:51:06 +0000126/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000127static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000128
Dave Martin7582e222017-10-31 15:51:08 +0000129#ifdef CONFIG_ARM64_SVE
130
131/* Maximum supported vector length across all CPUs (initially poisoned) */
Dave Martin87c021a2018-06-01 11:10:13 +0100132int __ro_after_init sve_max_vl = SVE_VL_MIN;
Dave Martin7582e222017-10-31 15:51:08 +0000133/* Set of available vector lengths, as vq_to_bit(vq): */
Dave Martin2e0f2472017-10-31 15:51:10 +0000134static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000135static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000136
137#else /* ! CONFIG_ARM64_SVE */
138
139/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000140extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000141extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000142
143#endif /* ! CONFIG_ARM64_SVE */
144
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200145/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000146 * Call __sve_free() directly only if you know task can't be scheduled
147 * or preempted.
148 */
149static void __sve_free(struct task_struct *task)
150{
151 kfree(task->thread.sve_state);
152 task->thread.sve_state = NULL;
153}
154
155static void sve_free(struct task_struct *task)
156{
157 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
158
159 __sve_free(task);
160}
161
Dave Martinbc0ee472017-10-31 15:51:05 +0000162/*
163 * TIF_SVE controls whether a task can use SVE without trapping while
164 * in userspace, and also the way a task's FPSIMD/SVE state is stored
165 * in thread_struct.
166 *
167 * The kernel uses this flag to track whether a user task is actively
168 * using SVE, and therefore whether full SVE register state needs to
169 * be tracked. If not, the cheaper FPSIMD context handling code can
170 * be used instead of the more costly SVE equivalents.
171 *
172 * * TIF_SVE set:
173 *
174 * The task can execute SVE instructions while in userspace without
175 * trapping to the kernel.
176 *
177 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
178 * corresponding Zn), P0-P15 and FFR are encoded in in
179 * task->thread.sve_state, formatted appropriately for vector
180 * length task->thread.sve_vl.
181 *
182 * task->thread.sve_state must point to a valid buffer at least
183 * sve_state_size(task) bytes in size.
184 *
185 * During any syscall, the kernel may optionally clear TIF_SVE and
186 * discard the vector state except for the FPSIMD subset.
187 *
188 * * TIF_SVE clear:
189 *
190 * An attempt by the user task to execute an SVE instruction causes
191 * do_sve_acc() to be called, which does some preparation and then
192 * sets TIF_SVE.
193 *
194 * When stored, FPSIMD registers V0-V31 are encoded in
Dave Martin65896542018-03-28 10:50:49 +0100195 * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
Dave Martinbc0ee472017-10-31 15:51:05 +0000196 * logically zero but not stored anywhere; P0-P15 and FFR are not
197 * stored and have unspecified values from userspace's point of
198 * view. For hygiene purposes, the kernel zeroes them on next use,
199 * but userspace is discouraged from relying on this.
200 *
201 * task->thread.sve_state does not need to be non-NULL, valid or any
202 * particular size: it must not be dereferenced.
203 *
Dave Martin65896542018-03-28 10:50:49 +0100204 * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
205 * irrespective of whether TIF_SVE is clear or set, since these are
206 * not vector length dependent.
Dave Martinbc0ee472017-10-31 15:51:05 +0000207 */
208
209/*
210 * Update current's FPSIMD/SVE registers from thread_struct.
211 *
212 * This function should be called only when the FPSIMD/SVE state in
213 * thread_struct is known to be up to date, when preparing to enter
214 * userspace.
215 *
216 * Softirqs (and preemption) must be disabled.
217 */
218static void task_fpsimd_load(void)
219{
220 WARN_ON(!in_softirq() && !irqs_disabled());
221
222 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin2cf97d42018-04-12 17:04:39 +0100223 sve_load_state(sve_pffr(&current->thread),
Dave Martin65896542018-03-28 10:50:49 +0100224 &current->thread.uw.fpsimd_state.fpsr,
Dave Martinbc0ee472017-10-31 15:51:05 +0000225 sve_vq_from_vl(current->thread.sve_vl) - 1);
226 else
Dave Martin65896542018-03-28 10:50:49 +0100227 fpsimd_load_state(&current->thread.uw.fpsimd_state);
Dave Martinbc0ee472017-10-31 15:51:05 +0000228}
229
230/*
Dave Martind1797612018-04-06 14:55:59 +0100231 * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
232 * date with respect to the CPU registers.
Dave Martinbc0ee472017-10-31 15:51:05 +0000233 *
234 * Softirqs (and preemption) must be disabled.
235 */
Dave Martine6b673b2018-04-06 14:55:59 +0100236void fpsimd_save(void)
Dave Martinbc0ee472017-10-31 15:51:05 +0000237{
Dave Martind1797612018-04-06 14:55:59 +0100238 struct user_fpsimd_state *st = __this_cpu_read(fpsimd_last_state.st);
Dave Martine6b673b2018-04-06 14:55:59 +0100239 /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
Dave Martind1797612018-04-06 14:55:59 +0100240
Dave Martinbc0ee472017-10-31 15:51:05 +0000241 WARN_ON(!in_softirq() && !irqs_disabled());
242
243 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
244 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
245 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
246 /*
247 * Can't save the user regs, so current would
248 * re-enter user with corrupt state.
249 * There's no way to recover, so kill it:
250 */
Dave Martinaf40ff62018-03-08 17:41:05 +0000251 force_signal_inject(SIGKILL, SI_KERNEL, 0);
Dave Martinbc0ee472017-10-31 15:51:05 +0000252 return;
253 }
254
Dave Martin2cf97d42018-04-12 17:04:39 +0100255 sve_save_state(sve_pffr(&current->thread), &st->fpsr);
Dave Martinbc0ee472017-10-31 15:51:05 +0000256 } else
Dave Martind1797612018-04-06 14:55:59 +0100257 fpsimd_save_state(st);
Dave Martinbc0ee472017-10-31 15:51:05 +0000258 }
259}
260
Dave Martin7582e222017-10-31 15:51:08 +0000261/*
262 * Helpers to translate bit indices in sve_vq_map to VQ values (and
263 * vice versa). This allows find_next_bit() to be used to find the
264 * _maximum_ VQ not exceeding a certain value.
265 */
266
267static unsigned int vq_to_bit(unsigned int vq)
268{
269 return SVE_VQ_MAX - vq;
270}
271
272static unsigned int bit_to_vq(unsigned int bit)
273{
274 if (WARN_ON(bit >= SVE_VQ_MAX))
275 bit = SVE_VQ_MAX - 1;
276
277 return SVE_VQ_MAX - bit;
278}
279
280/*
281 * All vector length selection from userspace comes through here.
282 * We're on a slow path, so some sanity-checks are included.
283 * If things go wrong there's a bug somewhere, but try to fall back to a
284 * safe choice.
285 */
286static unsigned int find_supported_vector_length(unsigned int vl)
287{
288 int bit;
289 int max_vl = sve_max_vl;
290
291 if (WARN_ON(!sve_vl_valid(vl)))
292 vl = SVE_VL_MIN;
293
294 if (WARN_ON(!sve_vl_valid(max_vl)))
295 max_vl = SVE_VL_MIN;
296
297 if (vl > max_vl)
298 vl = max_vl;
299
300 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
301 vq_to_bit(sve_vq_from_vl(vl)));
302 return sve_vl_from_vq(bit_to_vq(bit));
303}
304
Dave Martin4ffa09a2017-10-31 15:51:15 +0000305#ifdef CONFIG_SYSCTL
306
307static int sve_proc_do_default_vl(struct ctl_table *table, int write,
308 void __user *buffer, size_t *lenp,
309 loff_t *ppos)
310{
311 int ret;
312 int vl = sve_default_vl;
313 struct ctl_table tmp_table = {
314 .data = &vl,
315 .maxlen = sizeof(vl),
316 };
317
318 ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
319 if (ret || !write)
320 return ret;
321
322 /* Writing -1 has the special meaning "set to max": */
Dave Martin87c021a2018-06-01 11:10:13 +0100323 if (vl == -1)
324 vl = sve_max_vl;
Dave Martin4ffa09a2017-10-31 15:51:15 +0000325
326 if (!sve_vl_valid(vl))
327 return -EINVAL;
328
Dave Martin87c021a2018-06-01 11:10:13 +0100329 sve_default_vl = find_supported_vector_length(vl);
Dave Martin4ffa09a2017-10-31 15:51:15 +0000330 return 0;
331}
332
333static struct ctl_table sve_default_vl_table[] = {
334 {
335 .procname = "sve_default_vector_length",
336 .mode = 0644,
337 .proc_handler = sve_proc_do_default_vl,
338 },
339 { }
340};
341
342static int __init sve_sysctl_init(void)
343{
344 if (system_supports_sve())
345 if (!register_sysctl("abi", sve_default_vl_table))
346 return -EINVAL;
347
348 return 0;
349}
350
351#else /* ! CONFIG_SYSCTL */
352static int __init sve_sysctl_init(void) { return 0; }
353#endif /* ! CONFIG_SYSCTL */
354
Dave Martinbc0ee472017-10-31 15:51:05 +0000355#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
356 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
357
358/*
Dave Martin65896542018-03-28 10:50:49 +0100359 * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
Dave Martinbc0ee472017-10-31 15:51:05 +0000360 * task->thread.sve_state.
361 *
362 * Task can be a non-runnable task, or current. In the latter case,
363 * softirqs (and preemption) must be disabled.
364 * task->thread.sve_state must point to at least sve_state_size(task)
365 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100366 * task->thread.uw.fpsimd_state must be up to date before calling this
367 * function.
Dave Martinbc0ee472017-10-31 15:51:05 +0000368 */
369static void fpsimd_to_sve(struct task_struct *task)
370{
371 unsigned int vq;
372 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100373 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martinbc0ee472017-10-31 15:51:05 +0000374 unsigned int i;
375
376 if (!system_supports_sve())
377 return;
378
379 vq = sve_vq_from_vl(task->thread.sve_vl);
380 for (i = 0; i < 32; ++i)
381 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
382 sizeof(fst->vregs[i]));
383}
384
Dave Martin8cd969d2017-10-31 15:51:07 +0000385/*
386 * Transfer the SVE state in task->thread.sve_state to
Dave Martin65896542018-03-28 10:50:49 +0100387 * task->thread.uw.fpsimd_state.
Dave Martin8cd969d2017-10-31 15:51:07 +0000388 *
389 * Task can be a non-runnable task, or current. In the latter case,
390 * softirqs (and preemption) must be disabled.
391 * task->thread.sve_state must point to at least sve_state_size(task)
392 * bytes of allocated kernel memory.
393 * task->thread.sve_state must be up to date before calling this function.
394 */
395static void sve_to_fpsimd(struct task_struct *task)
396{
397 unsigned int vq;
398 void const *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100399 struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
Dave Martin8cd969d2017-10-31 15:51:07 +0000400 unsigned int i;
401
402 if (!system_supports_sve())
403 return;
404
405 vq = sve_vq_from_vl(task->thread.sve_vl);
406 for (i = 0; i < 32; ++i)
407 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
408 sizeof(fst->vregs[i]));
409}
410
Dave Martinbc0ee472017-10-31 15:51:05 +0000411#ifdef CONFIG_ARM64_SVE
412
413/*
414 * Return how many bytes of memory are required to store the full SVE
415 * state for task, given task's currently configured vector length.
416 */
417size_t sve_state_size(struct task_struct const *task)
418{
419 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
420}
421
422/*
423 * Ensure that task->thread.sve_state is allocated and sufficiently large.
424 *
425 * This function should be used only in preparation for replacing
426 * task->thread.sve_state with new data. The memory is always zeroed
427 * here to prevent stale data from showing through: this is done in
428 * the interest of testability and predictability: except in the
429 * do_sve_acc() case, there is no ABI requirement to hide stale data
430 * written previously be task.
431 */
432void sve_alloc(struct task_struct *task)
433{
434 if (task->thread.sve_state) {
435 memset(task->thread.sve_state, 0, sve_state_size(current));
436 return;
437 }
438
439 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
440 task->thread.sve_state =
441 kzalloc(sve_state_size(task), GFP_KERNEL);
442
443 /*
444 * If future SVE revisions can have larger vectors though,
445 * this may cease to be true:
446 */
447 BUG_ON(!task->thread.sve_state);
448}
449
Dave Martin43d4da2c42017-10-31 15:51:13 +0000450
451/*
452 * Ensure that task->thread.sve_state is up to date with respect to
453 * the user task, irrespective of when SVE is in use or not.
454 *
455 * This should only be called by ptrace. task must be non-runnable.
456 * task->thread.sve_state must point to at least sve_state_size(task)
457 * bytes of allocated kernel memory.
458 */
459void fpsimd_sync_to_sve(struct task_struct *task)
460{
461 if (!test_tsk_thread_flag(task, TIF_SVE))
462 fpsimd_to_sve(task);
463}
464
465/*
Dave Martin65896542018-03-28 10:50:49 +0100466 * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
Dave Martin43d4da2c42017-10-31 15:51:13 +0000467 * the user task, irrespective of whether SVE is in use or not.
468 *
469 * This should only be called by ptrace. task must be non-runnable.
470 * task->thread.sve_state must point to at least sve_state_size(task)
471 * bytes of allocated kernel memory.
472 */
473void sve_sync_to_fpsimd(struct task_struct *task)
474{
475 if (test_tsk_thread_flag(task, TIF_SVE))
476 sve_to_fpsimd(task);
477}
478
479/*
480 * Ensure that task->thread.sve_state is up to date with respect to
Dave Martin65896542018-03-28 10:50:49 +0100481 * the task->thread.uw.fpsimd_state.
Dave Martin43d4da2c42017-10-31 15:51:13 +0000482 *
483 * This should only be called by ptrace to merge new FPSIMD register
484 * values into a task for which SVE is currently active.
485 * task must be non-runnable.
486 * task->thread.sve_state must point to at least sve_state_size(task)
487 * bytes of allocated kernel memory.
Dave Martin65896542018-03-28 10:50:49 +0100488 * task->thread.uw.fpsimd_state must already have been initialised with
Dave Martin43d4da2c42017-10-31 15:51:13 +0000489 * the new FPSIMD register values to be merged in.
490 */
491void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
492{
493 unsigned int vq;
494 void *sst = task->thread.sve_state;
Dave Martin65896542018-03-28 10:50:49 +0100495 struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
Dave Martin43d4da2c42017-10-31 15:51:13 +0000496 unsigned int i;
497
498 if (!test_tsk_thread_flag(task, TIF_SVE))
499 return;
500
501 vq = sve_vq_from_vl(task->thread.sve_vl);
502
503 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
504
505 for (i = 0; i < 32; ++i)
506 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
507 sizeof(fst->vregs[i]));
508}
509
Dave Martin7582e222017-10-31 15:51:08 +0000510int sve_set_vector_length(struct task_struct *task,
511 unsigned long vl, unsigned long flags)
512{
513 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
514 PR_SVE_SET_VL_ONEXEC))
515 return -EINVAL;
516
517 if (!sve_vl_valid(vl))
518 return -EINVAL;
519
520 /*
521 * Clamp to the maximum vector length that VL-agnostic SVE code can
522 * work with. A flag may be assigned in the future to allow setting
523 * of larger vector lengths without confusing older software.
524 */
525 if (vl > SVE_VL_ARCH_MAX)
526 vl = SVE_VL_ARCH_MAX;
527
528 vl = find_supported_vector_length(vl);
529
530 if (flags & (PR_SVE_VL_INHERIT |
531 PR_SVE_SET_VL_ONEXEC))
532 task->thread.sve_vl_onexec = vl;
533 else
534 /* Reset VL to system default on next exec: */
535 task->thread.sve_vl_onexec = 0;
536
537 /* Only actually set the VL if not deferred: */
538 if (flags & PR_SVE_SET_VL_ONEXEC)
539 goto out;
540
541 if (vl == task->thread.sve_vl)
542 goto out;
543
544 /*
545 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
546 * write any live register state back to task_struct, and convert to a
547 * non-SVE thread.
548 */
549 if (task == current) {
550 local_bh_disable();
551
Dave Martind1797612018-04-06 14:55:59 +0100552 fpsimd_save();
Dave Martin7582e222017-10-31 15:51:08 +0000553 }
554
555 fpsimd_flush_task_state(task);
556 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
557 sve_to_fpsimd(task);
558
559 if (task == current)
560 local_bh_enable();
561
562 /*
563 * Force reallocation of task SVE state to the correct size
564 * on next use:
565 */
566 sve_free(task);
567
568 task->thread.sve_vl = vl;
569
570out:
Dave Martin09d12232018-04-11 17:59:06 +0100571 update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
572 flags & PR_SVE_VL_INHERIT);
Dave Martin7582e222017-10-31 15:51:08 +0000573
574 return 0;
575}
576
Dave Martinbc0ee472017-10-31 15:51:05 +0000577/*
Dave Martin2d2123b2017-10-31 15:51:14 +0000578 * Encode the current vector length and flags for return.
579 * This is only required for prctl(): ptrace has separate fields
580 *
581 * flags are as for sve_set_vector_length().
582 */
583static int sve_prctl_status(unsigned long flags)
584{
585 int ret;
586
587 if (flags & PR_SVE_SET_VL_ONEXEC)
588 ret = current->thread.sve_vl_onexec;
589 else
590 ret = current->thread.sve_vl;
591
592 if (test_thread_flag(TIF_SVE_VL_INHERIT))
593 ret |= PR_SVE_VL_INHERIT;
594
595 return ret;
596}
597
598/* PR_SVE_SET_VL */
599int sve_set_current_vl(unsigned long arg)
600{
601 unsigned long vl, flags;
602 int ret;
603
604 vl = arg & PR_SVE_VL_LEN_MASK;
605 flags = arg & ~vl;
606
607 if (!system_supports_sve())
608 return -EINVAL;
609
610 ret = sve_set_vector_length(current, vl, flags);
611 if (ret)
612 return ret;
613
614 return sve_prctl_status(flags);
615}
616
617/* PR_SVE_GET_VL */
618int sve_get_current_vl(void)
619{
620 if (!system_supports_sve())
621 return -EINVAL;
622
623 return sve_prctl_status(0);
624}
625
626/*
Dave Martin2e0f2472017-10-31 15:51:10 +0000627 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
628 * during secondary boot.
629 */
630static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
631
632static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
633{
634 unsigned int vq, vl;
635 unsigned long zcr;
636
637 bitmap_zero(map, SVE_VQ_MAX);
638
639 zcr = ZCR_ELx_LEN_MASK;
640 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
641
642 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
643 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
644 vl = sve_get_vl();
645 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
646 set_bit(vq_to_bit(vq), map);
647 }
648}
649
Dave Martin8b08e842018-12-06 16:32:35 +0000650/*
651 * Initialise the set of known supported VQs for the boot CPU.
652 * This is called during kernel boot, before secondary CPUs are brought up.
653 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000654void __init sve_init_vq_map(void)
655{
656 sve_probe_vqs(sve_vq_map);
657}
658
659/*
660 * If we haven't committed to the set of supported VQs yet, filter out
661 * those not supported by the current CPU.
Dave Martin8b08e842018-12-06 16:32:35 +0000662 * This function is called during the bring-up of early secondary CPUs only.
Dave Martin2e0f2472017-10-31 15:51:10 +0000663 */
664void sve_update_vq_map(void)
665{
666 sve_probe_vqs(sve_secondary_vq_map);
667 bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
668}
669
Dave Martin8b08e842018-12-06 16:32:35 +0000670/*
671 * Check whether the current CPU supports all VQs in the committed set.
672 * This function is called during the bring-up of late secondary CPUs only.
673 */
Dave Martin2e0f2472017-10-31 15:51:10 +0000674int sve_verify_vq_map(void)
675{
676 int ret = 0;
677
678 sve_probe_vqs(sve_secondary_vq_map);
679 bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
680 SVE_VQ_MAX);
681 if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
682 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
683 smp_processor_id());
684 ret = -EINVAL;
685 }
686
687 return ret;
688}
689
Dave Martinfdfa9762017-10-31 15:51:12 +0000690static void __init sve_efi_setup(void)
691{
692 if (!IS_ENABLED(CONFIG_EFI))
693 return;
694
695 /*
696 * alloc_percpu() warns and prints a backtrace if this goes wrong.
697 * This is evidence of a crippled system and we are returning void,
698 * so no attempt is made to handle this situation here.
699 */
700 if (!sve_vl_valid(sve_max_vl))
701 goto fail;
702
703 efi_sve_state = __alloc_percpu(
704 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
705 if (!efi_sve_state)
706 goto fail;
707
708 return;
709
710fail:
711 panic("Cannot allocate percpu memory for EFI SVE save/restore");
712}
713
Dave Martin2e0f2472017-10-31 15:51:10 +0000714/*
715 * Enable SVE for EL1.
716 * Intended for use by the cpufeatures code during CPU boot.
717 */
Dave Martinc0cda3b2018-03-26 15:12:28 +0100718void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
Dave Martin2e0f2472017-10-31 15:51:10 +0000719{
720 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
721 isb();
Dave Martin2e0f2472017-10-31 15:51:10 +0000722}
723
Dave Martin31dc52b2018-04-12 16:47:20 +0100724/*
725 * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
726 * vector length.
727 *
728 * Use only if SVE is present.
729 * This function clobbers the SVE vector length.
730 */
731u64 read_zcr_features(void)
732{
733 u64 zcr;
734 unsigned int vq_max;
735
736 /*
737 * Set the maximum possible VL, and write zeroes to all other
738 * bits to see if they stick.
739 */
740 sve_kernel_enable(NULL);
741 write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
742
743 zcr = read_sysreg_s(SYS_ZCR_EL1);
744 zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
745 vq_max = sve_vq_from_vl(sve_get_vl());
746 zcr |= vq_max - 1; /* set LEN field to maximum effective value */
747
748 return zcr;
749}
750
Dave Martin2e0f2472017-10-31 15:51:10 +0000751void __init sve_setup(void)
752{
753 u64 zcr;
754
755 if (!system_supports_sve())
756 return;
757
758 /*
759 * The SVE architecture mandates support for 128-bit vectors,
760 * so sve_vq_map must have at least SVE_VQ_MIN set.
761 * If something went wrong, at least try to patch it up:
762 */
763 if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
764 set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
765
766 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
767 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
768
769 /*
770 * Sanity-check that the max VL we determined through CPU features
771 * corresponds properly to sve_vq_map. If not, do our best:
772 */
773 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
774 sve_max_vl = find_supported_vector_length(sve_max_vl);
775
776 /*
777 * For the default VL, pick the maximum supported value <= 64.
778 * VL == 64 is guaranteed not to grow the signal frame.
779 */
780 sve_default_vl = find_supported_vector_length(64);
781
782 pr_info("SVE: maximum available vector length %u bytes per vector\n",
783 sve_max_vl);
784 pr_info("SVE: default vector length %u bytes per vector\n",
785 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000786
787 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000788}
789
790/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000791 * Called from the put_task_struct() path, which cannot get here
792 * unless dead_task is really dead and not schedulable.
793 */
794void fpsimd_release_task(struct task_struct *dead_task)
795{
796 __sve_free(dead_task);
797}
798
799#endif /* CONFIG_ARM64_SVE */
800
801/*
802 * Trapped SVE access
803 *
804 * Storage is allocated for the full SVE state, the current FPSIMD
805 * register contents are migrated across, and TIF_SVE is set so that
806 * the SVE access trap will be disabled the next time this task
807 * reaches ret_to_user.
808 *
809 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
810 * would have disabled the SVE access trap for userspace during
811 * ret_to_user, making an SVE access trap impossible in that case.
812 */
813asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
814{
815 /* Even if we chose not to use SVE, the hardware could still trap: */
816 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
Will Deacon2c9120f32018-02-20 14:16:29 +0000817 force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
Dave Martinbc0ee472017-10-31 15:51:05 +0000818 return;
819 }
820
821 sve_alloc(current);
822
823 local_bh_disable();
824
Dave Martind1797612018-04-06 14:55:59 +0100825 fpsimd_save();
Dave Martinbc0ee472017-10-31 15:51:05 +0000826
827 /* Force ret_to_user to reload the registers: */
828 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000829
Dave Martinefbc2022018-09-28 14:39:05 +0100830 fpsimd_to_sve(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000831 if (test_and_set_thread_flag(TIF_SVE))
832 WARN_ON(1); /* SVE access shouldn't have trapped */
833
834 local_bh_enable();
835}
836
Catalin Marinas53631b52012-03-05 11:49:32 +0000837/*
838 * Trapped FP/ASIMD access.
839 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000840asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000841{
842 /* TODO: implement lazy context saving/restoring */
843 WARN_ON(1);
844}
845
846/*
847 * Raise a SIGFPE for the current process.
848 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000849asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000850{
Dave Martinaf4a81b2018-03-01 17:44:07 +0000851 unsigned int si_code = FPE_FLTUNK;
Catalin Marinas53631b52012-03-05 11:49:32 +0000852
Dave Martinaf4a81b2018-03-01 17:44:07 +0000853 if (esr & ESR_ELx_FP_EXC_TFV) {
854 if (esr & FPEXC_IOF)
855 si_code = FPE_FLTINV;
856 else if (esr & FPEXC_DZF)
857 si_code = FPE_FLTDIV;
858 else if (esr & FPEXC_OFF)
859 si_code = FPE_FLTOVF;
860 else if (esr & FPEXC_UFF)
861 si_code = FPE_FLTUND;
862 else if (esr & FPEXC_IXF)
863 si_code = FPE_FLTRES;
864 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000865
Eric W. Biedermanc8526802018-04-16 13:47:06 -0500866 send_sig_fault(SIGFPE, si_code,
867 (void __user *)instruction_pointer(regs),
868 current);
Catalin Marinas53631b52012-03-05 11:49:32 +0000869}
870
871void fpsimd_thread_switch(struct task_struct *next)
872{
Dave Martindf3fb962018-05-21 19:08:15 +0100873 bool wrong_task, wrong_cpu;
874
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000875 if (!system_supports_fpsimd())
876 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200877
Dave Martindf3fb962018-05-21 19:08:15 +0100878 /* Save unsaved fpsimd state, if any: */
879 fpsimd_save();
880
Catalin Marinas53631b52012-03-05 11:49:32 +0000881 /*
Dave Martindf3fb962018-05-21 19:08:15 +0100882 * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
883 * state. For kernel threads, FPSIMD registers are never loaded
884 * and wrong_task and wrong_cpu will always be true.
Catalin Marinas53631b52012-03-05 11:49:32 +0000885 */
Dave Martindf3fb962018-05-21 19:08:15 +0100886 wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
Dave Martin09d12232018-04-11 17:59:06 +0100887 &next->thread.uw.fpsimd_state;
Dave Martindf3fb962018-05-21 19:08:15 +0100888 wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
Dave Martin09d12232018-04-11 17:59:06 +0100889
Dave Martindf3fb962018-05-21 19:08:15 +0100890 update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
891 wrong_task || wrong_cpu);
Catalin Marinas53631b52012-03-05 11:49:32 +0000892}
893
894void fpsimd_flush_thread(void)
895{
Dave Martin7582e222017-10-31 15:51:08 +0000896 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000897
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000898 if (!system_supports_fpsimd())
899 return;
Dave Martincb84d112017-08-03 17:23:23 +0100900
901 local_bh_disable();
902
Dave Martinefbc2022018-09-28 14:39:05 +0100903 fpsimd_flush_task_state(current);
Dave Martin65896542018-03-28 10:50:49 +0100904 memset(&current->thread.uw.fpsimd_state, 0,
905 sizeof(current->thread.uw.fpsimd_state));
Dave Martinbc0ee472017-10-31 15:51:05 +0000906
907 if (system_supports_sve()) {
908 clear_thread_flag(TIF_SVE);
909 sve_free(current);
910
911 /*
912 * Reset the task vector length as required.
913 * This is where we ensure that all user tasks have a valid
914 * vector length configured: no kernel task can become a user
915 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000916 * By the time the first call to this function is made, all
917 * early hardware probing is complete, so sve_default_vl
918 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000919 * If a bug causes this to go wrong, we make some noise and
920 * try to fudge thread.sve_vl to a safe value here.
921 */
Dave Martin79ab0472017-10-31 15:51:06 +0000922 vl = current->thread.sve_vl_onexec ?
923 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000924
925 if (WARN_ON(!sve_vl_valid(vl)))
926 vl = SVE_VL_MIN;
927
Dave Martin7582e222017-10-31 15:51:08 +0000928 supported_vl = find_supported_vector_length(vl);
929 if (WARN_ON(supported_vl != vl))
930 vl = supported_vl;
931
Dave Martinbc0ee472017-10-31 15:51:05 +0000932 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000933
934 /*
935 * If the task is not set to inherit, ensure that the vector
936 * length will be reset by a subsequent exec:
937 */
938 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
939 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000940 }
941
Dave Martincb84d112017-08-03 17:23:23 +0100942 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000943}
944
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100945/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200946 * Save the userland FPSIMD state of 'current' to memory, but only if the state
947 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100948 */
949void fpsimd_preserve_current_state(void)
950{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000951 if (!system_supports_fpsimd())
952 return;
Dave Martincb84d112017-08-03 17:23:23 +0100953
954 local_bh_disable();
Dave Martind1797612018-04-06 14:55:59 +0100955 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100956 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100957}
958
959/*
Dave Martin8cd969d2017-10-31 15:51:07 +0000960 * Like fpsimd_preserve_current_state(), but ensure that
Dave Martin65896542018-03-28 10:50:49 +0100961 * current->thread.uw.fpsimd_state is updated so that it can be copied to
Dave Martin8cd969d2017-10-31 15:51:07 +0000962 * the signal frame.
963 */
964void fpsimd_signal_preserve_current_state(void)
965{
966 fpsimd_preserve_current_state();
967 if (system_supports_sve() && test_thread_flag(TIF_SVE))
968 sve_to_fpsimd(current);
969}
970
971/*
Dave Martin8884b7b2017-12-06 16:45:46 +0000972 * Associate current's FPSIMD context with this cpu
973 * Preemption must be disabled when calling this function.
974 */
Dave Martine6b673b2018-04-06 14:55:59 +0100975void fpsimd_bind_task_to_cpu(void)
Dave Martin8884b7b2017-12-06 16:45:46 +0000976{
Dave Martincb968af2017-12-06 16:45:47 +0000977 struct fpsimd_last_state_struct *last =
978 this_cpu_ptr(&fpsimd_last_state);
Dave Martin8884b7b2017-12-06 16:45:46 +0000979
Dave Martin65896542018-03-28 10:50:49 +0100980 last->st = &current->thread.uw.fpsimd_state;
Dave Martin20b85472018-03-28 10:50:48 +0100981 current->thread.fpsimd_cpu = smp_processor_id();
Dave Martin0cff8e72018-05-09 14:27:41 +0100982
983 if (system_supports_sve()) {
984 /* Toggle SVE trapping for userspace if needed */
985 if (test_thread_flag(TIF_SVE))
986 sve_user_enable();
987 else
988 sve_user_disable();
989
990 /* Serialised by exception return to user */
991 }
Dave Martin8884b7b2017-12-06 16:45:46 +0000992}
993
Dave Martine6b673b2018-04-06 14:55:59 +0100994void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st)
995{
996 struct fpsimd_last_state_struct *last =
997 this_cpu_ptr(&fpsimd_last_state);
998
999 WARN_ON(!in_softirq() && !irqs_disabled());
1000
1001 last->st = st;
Dave Martin8884b7b2017-12-06 16:45:46 +00001002}
1003
1004/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001005 * Load the userland FPSIMD state of 'current' from memory, but only if the
1006 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
1007 * state of 'current'
1008 */
1009void fpsimd_restore_current_state(void)
1010{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001011 if (!system_supports_fpsimd())
1012 return;
Dave Martincb84d112017-08-03 17:23:23 +01001013
1014 local_bh_disable();
1015
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001016 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
Dave Martinbc0ee472017-10-31 15:51:05 +00001017 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001018 fpsimd_bind_task_to_cpu();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001019 }
Dave Martincb84d112017-08-03 17:23:23 +01001020
1021 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001022}
1023
1024/*
1025 * Load an updated userland FPSIMD state for 'current' from memory and set the
1026 * flag that indicates that the FPSIMD register contents are the most recent
1027 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001028 */
Dave Martin0abdeff2017-12-15 18:34:38 +00001029void fpsimd_update_current_state(struct user_fpsimd_state const *state)
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001030{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001031 if (!system_supports_fpsimd())
1032 return;
Dave Martincb84d112017-08-03 17:23:23 +01001033
1034 local_bh_disable();
1035
Dave Martin65896542018-03-28 10:50:49 +01001036 current->thread.uw.fpsimd_state = *state;
Dave Martin9de52a72017-11-30 11:56:37 +00001037 if (system_supports_sve() && test_thread_flag(TIF_SVE))
Dave Martin8cd969d2017-10-31 15:51:07 +00001038 fpsimd_to_sve(current);
Dave Martin9de52a72017-11-30 11:56:37 +00001039
Dave Martin8cd969d2017-10-31 15:51:07 +00001040 task_fpsimd_load();
Dave Martin0cff8e72018-05-09 14:27:41 +01001041 fpsimd_bind_task_to_cpu();
Dave Martin8cd969d2017-10-31 15:51:07 +00001042
Dave Martin0cff8e72018-05-09 14:27:41 +01001043 clear_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +01001044
1045 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +01001046}
1047
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001048/*
1049 * Invalidate live CPU copies of task t's FPSIMD state
Dave Martinefbc2022018-09-28 14:39:05 +01001050 *
1051 * This function may be called with preemption enabled. The barrier()
1052 * ensures that the assignment to fpsimd_cpu is visible to any
1053 * preemption/softirq that could race with set_tsk_thread_flag(), so
1054 * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
1055 *
1056 * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
1057 * subsequent code.
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001058 */
1059void fpsimd_flush_task_state(struct task_struct *t)
1060{
Dave Martin20b85472018-03-28 10:50:48 +01001061 t->thread.fpsimd_cpu = NR_CPUS;
Dave Martinefbc2022018-09-28 14:39:05 +01001062
1063 barrier();
1064 set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
1065
1066 barrier();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001067}
1068
Dave Martinefbc2022018-09-28 14:39:05 +01001069/*
1070 * Invalidate any task's FPSIMD state that is present on this cpu.
1071 * This function must be called with softirqs disabled.
1072 */
Dave Martine6b673b2018-04-06 14:55:59 +01001073void fpsimd_flush_cpu_state(void)
Dave Martin17eed272017-10-31 15:51:16 +00001074{
Dave Martincb968af2017-12-06 16:45:47 +00001075 __this_cpu_write(fpsimd_last_state.st, NULL);
Dave Martind8ad71f2018-05-21 18:25:43 +01001076 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martin17eed272017-10-31 15:51:16 +00001077}
1078
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001079#ifdef CONFIG_KERNEL_MODE_NEON
1080
Dave Martincb84d112017-08-03 17:23:23 +01001081DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +01001082EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +01001083
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001084/*
1085 * Kernel-side NEON support functions
1086 */
Dave Martincb84d112017-08-03 17:23:23 +01001087
1088/*
1089 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
1090 * context
1091 *
1092 * Must not be called unless may_use_simd() returns true.
1093 * Task context in the FPSIMD registers is saved back to memory as necessary.
1094 *
1095 * A matching call to kernel_neon_end() must be made before returning from the
1096 * calling context.
1097 *
1098 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
1099 * called.
1100 */
1101void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001102{
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001103 if (WARN_ON(!system_supports_fpsimd()))
1104 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001105
Dave Martincb84d112017-08-03 17:23:23 +01001106 BUG_ON(!may_use_simd());
1107
1108 local_bh_disable();
1109
1110 __this_cpu_write(kernel_neon_busy, true);
1111
Dave Martindf3fb962018-05-21 19:08:15 +01001112 /* Save unsaved fpsimd state, if any: */
1113 fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +01001114
1115 /* Invalidate any task state remaining in the fpsimd regs: */
Dave Martin17eed272017-10-31 15:51:16 +00001116 fpsimd_flush_cpu_state();
Dave Martincb84d112017-08-03 17:23:23 +01001117
1118 preempt_disable();
1119
1120 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001121}
Dave Martincb84d112017-08-03 17:23:23 +01001122EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001123
Dave Martincb84d112017-08-03 17:23:23 +01001124/*
1125 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
1126 *
1127 * Must be called from a context in which kernel_neon_begin() was previously
1128 * called, with no call to kernel_neon_end() in the meantime.
1129 *
1130 * The caller must not use the FPSIMD registers after this function is called,
1131 * unless kernel_neon_begin() is called again in the meantime.
1132 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001133void kernel_neon_end(void)
1134{
Dave Martincb84d112017-08-03 17:23:23 +01001135 bool busy;
1136
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001137 if (!system_supports_fpsimd())
1138 return;
Dave Martincb84d112017-08-03 17:23:23 +01001139
1140 busy = __this_cpu_xchg(kernel_neon_busy, false);
1141 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1142
1143 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001144}
1145EXPORT_SYMBOL(kernel_neon_end);
1146
Dave Martine580b8b2017-09-18 09:40:12 +01001147#ifdef CONFIG_EFI
1148
Dave Martin20b85472018-03-28 10:50:48 +01001149static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
Dave Martin3b660232017-08-18 14:53:47 +01001150static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001151static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001152
1153/*
1154 * EFI runtime services support functions
1155 *
1156 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1157 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1158 * is always used rather than being an optional accelerator.
1159 *
1160 * These functions provide the necessary support for ensuring FPSIMD
1161 * save/restore in the contexts from which EFI is used.
1162 *
1163 * Do not use them for any other purpose -- if tempted to do so, you are
1164 * either doing something wrong or you need to propose some refactoring.
1165 */
1166
1167/*
1168 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1169 */
1170void __efi_fpsimd_begin(void)
1171{
1172 if (!system_supports_fpsimd())
1173 return;
1174
1175 WARN_ON(preemptible());
1176
Dave Martinfdfa9762017-10-31 15:51:12 +00001177 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001178 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001179 } else {
1180 /*
1181 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1182 * preserving:
1183 */
1184 if (system_supports_sve() && likely(efi_sve_state)) {
1185 char *sve_state = this_cpu_ptr(efi_sve_state);
1186
1187 __this_cpu_write(efi_sve_state_used, true);
1188
1189 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1190 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1191 } else {
1192 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1193 }
1194
Dave Martin4328825d2017-08-03 17:23:22 +01001195 __this_cpu_write(efi_fpsimd_state_used, true);
1196 }
1197}
1198
1199/*
1200 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1201 */
1202void __efi_fpsimd_end(void)
1203{
1204 if (!system_supports_fpsimd())
1205 return;
1206
Dave Martinfdfa9762017-10-31 15:51:12 +00001207 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001208 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001209 } else {
1210 if (system_supports_sve() &&
1211 likely(__this_cpu_read(efi_sve_state_used))) {
1212 char const *sve_state = this_cpu_ptr(efi_sve_state);
1213
1214 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1215 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1216 sve_vq_from_vl(sve_get_vl()) - 1);
1217
1218 __this_cpu_write(efi_sve_state_used, false);
1219 } else {
1220 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1221 }
1222 }
Dave Martin4328825d2017-08-03 17:23:22 +01001223}
1224
Dave Martine580b8b2017-09-18 09:40:12 +01001225#endif /* CONFIG_EFI */
1226
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001227#endif /* CONFIG_KERNEL_MODE_NEON */
1228
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001229#ifdef CONFIG_CPU_PM
1230static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1231 unsigned long cmd, void *v)
1232{
1233 switch (cmd) {
1234 case CPU_PM_ENTER:
Dave Martindf3fb962018-05-21 19:08:15 +01001235 fpsimd_save();
Dave Martin17eed272017-10-31 15:51:16 +00001236 fpsimd_flush_cpu_state();
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001237 break;
1238 case CPU_PM_EXIT:
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001239 break;
1240 case CPU_PM_ENTER_FAILED:
1241 default:
1242 return NOTIFY_DONE;
1243 }
1244 return NOTIFY_OK;
1245}
1246
1247static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1248 .notifier_call = fpsimd_cpu_pm_notifier,
1249};
1250
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001251static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001252{
1253 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1254}
1255
1256#else
1257static inline void fpsimd_pm_init(void) { }
1258#endif /* CONFIG_CPU_PM */
1259
Janet Liu32365e62015-06-11 12:02:45 +08001260#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001261static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001262{
Dave Martincb968af2017-12-06 16:45:47 +00001263 per_cpu(fpsimd_last_state.st, cpu) = NULL;
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001264 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001265}
1266
Janet Liu32365e62015-06-11 12:02:45 +08001267static inline void fpsimd_hotplug_init(void)
1268{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001269 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1270 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001271}
1272
1273#else
1274static inline void fpsimd_hotplug_init(void) { }
1275#endif
1276
Catalin Marinas53631b52012-03-05 11:49:32 +00001277/*
1278 * FP/SIMD support code initialisation.
1279 */
1280static int __init fpsimd_init(void)
1281{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001282 if (elf_hwcap & HWCAP_FP) {
1283 fpsimd_pm_init();
1284 fpsimd_hotplug_init();
1285 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001286 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001287 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001288
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001289 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001290 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001291
Dave Martin4ffa09a2017-10-31 15:51:15 +00001292 return sve_sysctl_init();
Catalin Marinas53631b52012-03-05 11:49:32 +00001293}
Suzuki K Pouloseae2e9722017-10-06 14:16:53 +01001294core_initcall(fpsimd_init);