blob: 000b5f9215c6b58f5a2addb9637dd7ddb19fbe53 [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 Martincb84d112017-08-03 17:23:23 +010020#include <linux/bottom_half.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000021#include <linux/bug.h>
22#include <linux/compat.h>
Janet Liu32365e62015-06-11 12:02:45 +080023#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010024#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000025#include <linux/kernel.h>
Dave Martin94ef7ec2017-10-31 15:50:54 +000026#include <linux/linkage.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000027#include <linux/irqflags.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000028#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010029#include <linux/percpu.h>
Dave Martin4328825d2017-08-03 17:23:22 +010030#include <linux/preempt.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000031#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010032#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000033#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000034#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000035#include <linux/slab.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000036
37#include <asm/fpsimd.h>
38#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010039#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000040#include <asm/sigcontext.h>
41#include <asm/sysreg.h>
42#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000043
44#define FPEXC_IOF (1 << 0)
45#define FPEXC_DZF (1 << 1)
46#define FPEXC_OFF (1 << 2)
47#define FPEXC_UFF (1 << 3)
48#define FPEXC_IXF (1 << 4)
49#define FPEXC_IDF (1 << 7)
50
51/*
Dave Martinbc0ee472017-10-31 15:51:05 +000052 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
53 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020054 * In order to reduce the number of times the FPSIMD state is needlessly saved
55 * and restored, we need to keep track of two things:
56 * (a) for each task, we need to remember which CPU was the last one to have
57 * the task's FPSIMD state loaded into its FPSIMD registers;
58 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
59 * been loaded into its FPSIMD registers most recently, or whether it has
60 * been used to perform kernel mode NEON in the meantime.
61 *
62 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080063 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020064 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
65 * address of the userland FPSIMD state of the task that was loaded onto the CPU
66 * the most recently, or NULL if kernel mode NEON has been performed after that.
67 *
68 * With this in place, we no longer have to restore the next FPSIMD state right
69 * when switching between tasks. Instead, we can defer this check to userland
70 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
71 * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
72 * can omit the FPSIMD restore.
73 *
74 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
75 * indicate whether or not the userland FPSIMD state of the current task is
76 * present in the registers. The flag is set unless the FPSIMD registers of this
77 * CPU currently contain the most recent userland FPSIMD state of the current
78 * task.
79 *
Dave Martincb84d112017-08-03 17:23:23 +010080 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
81 * save the task's FPSIMD context back to task_struct from softirq context.
82 * To prevent this from racing with the manipulation of the task's FPSIMD state
83 * from task context and thereby corrupting the state, it is necessary to
84 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
85 * flag with local_bh_disable() unless softirqs are already masked.
86 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020087 * For a certain task, the sequence may look something like this:
88 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
89 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
90 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
91 * cleared, otherwise it is set;
92 *
93 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
94 * userland FPSIMD state is copied from memory to the registers, the task's
95 * fpsimd_state.cpu field is set to the id of the current CPU, the current
96 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
97 * TIF_FOREIGN_FPSTATE flag is cleared;
98 *
99 * - the task executes an ordinary syscall; upon return to userland, the
100 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
101 * restored;
102 *
103 * - the task executes a syscall which executes some NEON instructions; this is
104 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
105 * register contents to memory, clears the fpsimd_last_state per-cpu variable
106 * and sets the TIF_FOREIGN_FPSTATE flag;
107 *
108 * - the task gets preempted after kernel_neon_end() is called; as we have not
109 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
110 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
111 */
112static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
113
114/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000115 * Call __sve_free() directly only if you know task can't be scheduled
116 * or preempted.
117 */
118static void __sve_free(struct task_struct *task)
119{
120 kfree(task->thread.sve_state);
121 task->thread.sve_state = NULL;
122}
123
124static void sve_free(struct task_struct *task)
125{
126 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
127
128 __sve_free(task);
129}
130
131
132/* Offset of FFR in the SVE register dump */
133static size_t sve_ffr_offset(int vl)
134{
135 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
136}
137
138static void *sve_pffr(struct task_struct *task)
139{
140 return (char *)task->thread.sve_state +
141 sve_ffr_offset(task->thread.sve_vl);
142}
143
144static void change_cpacr(u64 val, u64 mask)
145{
146 u64 cpacr = read_sysreg(CPACR_EL1);
147 u64 new = (cpacr & ~mask) | val;
148
149 if (new != cpacr)
150 write_sysreg(new, CPACR_EL1);
151}
152
153static void sve_user_disable(void)
154{
155 change_cpacr(0, CPACR_EL1_ZEN_EL0EN);
156}
157
158static void sve_user_enable(void)
159{
160 change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN);
161}
162
163/*
164 * TIF_SVE controls whether a task can use SVE without trapping while
165 * in userspace, and also the way a task's FPSIMD/SVE state is stored
166 * in thread_struct.
167 *
168 * The kernel uses this flag to track whether a user task is actively
169 * using SVE, and therefore whether full SVE register state needs to
170 * be tracked. If not, the cheaper FPSIMD context handling code can
171 * be used instead of the more costly SVE equivalents.
172 *
173 * * TIF_SVE set:
174 *
175 * The task can execute SVE instructions while in userspace without
176 * trapping to the kernel.
177 *
178 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
179 * corresponding Zn), P0-P15 and FFR are encoded in in
180 * task->thread.sve_state, formatted appropriately for vector
181 * length task->thread.sve_vl.
182 *
183 * task->thread.sve_state must point to a valid buffer at least
184 * sve_state_size(task) bytes in size.
185 *
186 * During any syscall, the kernel may optionally clear TIF_SVE and
187 * discard the vector state except for the FPSIMD subset.
188 *
189 * * TIF_SVE clear:
190 *
191 * An attempt by the user task to execute an SVE instruction causes
192 * do_sve_acc() to be called, which does some preparation and then
193 * sets TIF_SVE.
194 *
195 * When stored, FPSIMD registers V0-V31 are encoded in
196 * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are
197 * logically zero but not stored anywhere; P0-P15 and FFR are not
198 * stored and have unspecified values from userspace's point of
199 * view. For hygiene purposes, the kernel zeroes them on next use,
200 * but userspace is discouraged from relying on this.
201 *
202 * task->thread.sve_state does not need to be non-NULL, valid or any
203 * particular size: it must not be dereferenced.
204 *
205 * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of
206 * whether TIF_SVE is clear or set, since these are not vector length
207 * dependent.
208 */
209
210/*
211 * Update current's FPSIMD/SVE registers from thread_struct.
212 *
213 * This function should be called only when the FPSIMD/SVE state in
214 * thread_struct is known to be up to date, when preparing to enter
215 * userspace.
216 *
217 * Softirqs (and preemption) must be disabled.
218 */
219static void task_fpsimd_load(void)
220{
221 WARN_ON(!in_softirq() && !irqs_disabled());
222
223 if (system_supports_sve() && test_thread_flag(TIF_SVE))
224 sve_load_state(sve_pffr(current),
225 &current->thread.fpsimd_state.fpsr,
226 sve_vq_from_vl(current->thread.sve_vl) - 1);
227 else
228 fpsimd_load_state(&current->thread.fpsimd_state);
229
230 if (system_supports_sve()) {
231 /* Toggle SVE trapping for userspace if needed */
232 if (test_thread_flag(TIF_SVE))
233 sve_user_enable();
234 else
235 sve_user_disable();
236
237 /* Serialised by exception return to user */
238 }
239}
240
241/*
242 * Ensure current's FPSIMD/SVE storage in thread_struct is up to date
243 * with respect to the CPU registers.
244 *
245 * Softirqs (and preemption) must be disabled.
246 */
247static void task_fpsimd_save(void)
248{
249 WARN_ON(!in_softirq() && !irqs_disabled());
250
251 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
252 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
253 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
254 /*
255 * Can't save the user regs, so current would
256 * re-enter user with corrupt state.
257 * There's no way to recover, so kill it:
258 */
259 force_signal_inject(
260 SIGKILL, 0, current_pt_regs(), 0);
261 return;
262 }
263
264 sve_save_state(sve_pffr(current),
265 &current->thread.fpsimd_state.fpsr);
266 } else
267 fpsimd_save_state(&current->thread.fpsimd_state);
268 }
269}
270
271#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
272 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
273
274/*
275 * Transfer the FPSIMD state in task->thread.fpsimd_state to
276 * task->thread.sve_state.
277 *
278 * Task can be a non-runnable task, or current. In the latter case,
279 * softirqs (and preemption) must be disabled.
280 * task->thread.sve_state must point to at least sve_state_size(task)
281 * bytes of allocated kernel memory.
282 * task->thread.fpsimd_state must be up to date before calling this function.
283 */
284static void fpsimd_to_sve(struct task_struct *task)
285{
286 unsigned int vq;
287 void *sst = task->thread.sve_state;
288 struct fpsimd_state const *fst = &task->thread.fpsimd_state;
289 unsigned int i;
290
291 if (!system_supports_sve())
292 return;
293
294 vq = sve_vq_from_vl(task->thread.sve_vl);
295 for (i = 0; i < 32; ++i)
296 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
297 sizeof(fst->vregs[i]));
298}
299
300#ifdef CONFIG_ARM64_SVE
301
302/*
303 * Return how many bytes of memory are required to store the full SVE
304 * state for task, given task's currently configured vector length.
305 */
306size_t sve_state_size(struct task_struct const *task)
307{
308 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
309}
310
311/*
312 * Ensure that task->thread.sve_state is allocated and sufficiently large.
313 *
314 * This function should be used only in preparation for replacing
315 * task->thread.sve_state with new data. The memory is always zeroed
316 * here to prevent stale data from showing through: this is done in
317 * the interest of testability and predictability: except in the
318 * do_sve_acc() case, there is no ABI requirement to hide stale data
319 * written previously be task.
320 */
321void sve_alloc(struct task_struct *task)
322{
323 if (task->thread.sve_state) {
324 memset(task->thread.sve_state, 0, sve_state_size(current));
325 return;
326 }
327
328 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
329 task->thread.sve_state =
330 kzalloc(sve_state_size(task), GFP_KERNEL);
331
332 /*
333 * If future SVE revisions can have larger vectors though,
334 * this may cease to be true:
335 */
336 BUG_ON(!task->thread.sve_state);
337}
338
339/*
340 * Called from the put_task_struct() path, which cannot get here
341 * unless dead_task is really dead and not schedulable.
342 */
343void fpsimd_release_task(struct task_struct *dead_task)
344{
345 __sve_free(dead_task);
346}
347
348#endif /* CONFIG_ARM64_SVE */
349
350/*
351 * Trapped SVE access
352 *
353 * Storage is allocated for the full SVE state, the current FPSIMD
354 * register contents are migrated across, and TIF_SVE is set so that
355 * the SVE access trap will be disabled the next time this task
356 * reaches ret_to_user.
357 *
358 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
359 * would have disabled the SVE access trap for userspace during
360 * ret_to_user, making an SVE access trap impossible in that case.
361 */
362asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
363{
364 /* Even if we chose not to use SVE, the hardware could still trap: */
365 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
366 force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
367 return;
368 }
369
370 sve_alloc(current);
371
372 local_bh_disable();
373
374 task_fpsimd_save();
375 fpsimd_to_sve(current);
376
377 /* Force ret_to_user to reload the registers: */
378 fpsimd_flush_task_state(current);
379 set_thread_flag(TIF_FOREIGN_FPSTATE);
380
381 if (test_and_set_thread_flag(TIF_SVE))
382 WARN_ON(1); /* SVE access shouldn't have trapped */
383
384 local_bh_enable();
385}
386
387/*
Catalin Marinas53631b52012-03-05 11:49:32 +0000388 * Trapped FP/ASIMD access.
389 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000390asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000391{
392 /* TODO: implement lazy context saving/restoring */
393 WARN_ON(1);
394}
395
396/*
397 * Raise a SIGFPE for the current process.
398 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000399asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000400{
401 siginfo_t info;
402 unsigned int si_code = 0;
403
404 if (esr & FPEXC_IOF)
405 si_code = FPE_FLTINV;
406 else if (esr & FPEXC_DZF)
407 si_code = FPE_FLTDIV;
408 else if (esr & FPEXC_OFF)
409 si_code = FPE_FLTOVF;
410 else if (esr & FPEXC_UFF)
411 si_code = FPE_FLTUND;
412 else if (esr & FPEXC_IXF)
413 si_code = FPE_FLTRES;
414
415 memset(&info, 0, sizeof(info));
416 info.si_signo = SIGFPE;
417 info.si_code = si_code;
418 info.si_addr = (void __user *)instruction_pointer(regs);
419
420 send_sig_info(SIGFPE, &info, current);
421}
422
423void fpsimd_thread_switch(struct task_struct *next)
424{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000425 if (!system_supports_fpsimd())
426 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200427 /*
428 * Save the current FPSIMD state to memory, but only if whatever is in
429 * the registers is in fact the most recent userland FPSIMD state of
430 * 'current'.
431 */
Dave Martinbc0ee472017-10-31 15:51:05 +0000432 if (current->mm)
433 task_fpsimd_save();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200434
435 if (next->mm) {
436 /*
437 * If we are switching to a task whose most recent userland
438 * FPSIMD state is already in the registers of *this* cpu,
439 * we can skip loading the state from memory. Otherwise, set
440 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
441 * upon the next return to userland.
442 */
443 struct fpsimd_state *st = &next->thread.fpsimd_state;
444
445 if (__this_cpu_read(fpsimd_last_state) == st
446 && st->cpu == smp_processor_id())
Dave Martin9cf5b542017-10-31 15:50:59 +0000447 clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200448 else
Dave Martin9cf5b542017-10-31 15:50:59 +0000449 set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200450 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000451}
452
453void fpsimd_flush_thread(void)
454{
Dave Martinbc0ee472017-10-31 15:51:05 +0000455 int vl;
456
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000457 if (!system_supports_fpsimd())
458 return;
Dave Martincb84d112017-08-03 17:23:23 +0100459
460 local_bh_disable();
461
Catalin Marinas53631b52012-03-05 11:49:32 +0000462 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100463 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000464
465 if (system_supports_sve()) {
466 clear_thread_flag(TIF_SVE);
467 sve_free(current);
468
469 /*
470 * Reset the task vector length as required.
471 * This is where we ensure that all user tasks have a valid
472 * vector length configured: no kernel task can become a user
473 * task without an exec and hence a call to this function.
474 * If a bug causes this to go wrong, we make some noise and
475 * try to fudge thread.sve_vl to a safe value here.
476 */
477 vl = current->thread.sve_vl;
478
479 if (vl == 0)
480 vl = SVE_VL_MIN;
481
482 if (WARN_ON(!sve_vl_valid(vl)))
483 vl = SVE_VL_MIN;
484
485 current->thread.sve_vl = vl;
486 }
487
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200488 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100489
490 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000491}
492
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100493/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200494 * Save the userland FPSIMD state of 'current' to memory, but only if the state
495 * currently held in the registers does in fact belong to 'current'
Dave Martinbc0ee472017-10-31 15:51:05 +0000496 *
497 * Currently, SVE tasks can't exist, so just WARN in that case.
498 * Subsequent patches will add full SVE support here.
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100499 */
500void fpsimd_preserve_current_state(void)
501{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000502 if (!system_supports_fpsimd())
503 return;
Dave Martincb84d112017-08-03 17:23:23 +0100504
505 local_bh_disable();
506
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200507 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
508 fpsimd_save_state(&current->thread.fpsimd_state);
Dave Martincb84d112017-08-03 17:23:23 +0100509
Dave Martinbc0ee472017-10-31 15:51:05 +0000510 WARN_ON_ONCE(test_and_clear_thread_flag(TIF_SVE));
511
Dave Martincb84d112017-08-03 17:23:23 +0100512 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100513}
514
515/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200516 * Load the userland FPSIMD state of 'current' from memory, but only if the
517 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
518 * state of 'current'
519 */
520void fpsimd_restore_current_state(void)
521{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000522 if (!system_supports_fpsimd())
523 return;
Dave Martincb84d112017-08-03 17:23:23 +0100524
525 local_bh_disable();
526
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200527 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
528 struct fpsimd_state *st = &current->thread.fpsimd_state;
529
Dave Martinbc0ee472017-10-31 15:51:05 +0000530 task_fpsimd_load();
Dave Martin50464182017-08-03 17:23:21 +0100531 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200532 st->cpu = smp_processor_id();
533 }
Dave Martincb84d112017-08-03 17:23:23 +0100534
535 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200536}
537
538/*
539 * Load an updated userland FPSIMD state for 'current' from memory and set the
540 * flag that indicates that the FPSIMD register contents are the most recent
541 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100542 */
543void fpsimd_update_current_state(struct fpsimd_state *state)
544{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000545 if (!system_supports_fpsimd())
546 return;
Dave Martincb84d112017-08-03 17:23:23 +0100547
548 local_bh_disable();
549
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100550 fpsimd_load_state(state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200551 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
552 struct fpsimd_state *st = &current->thread.fpsimd_state;
553
Dave Martin50464182017-08-03 17:23:21 +0100554 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200555 st->cpu = smp_processor_id();
556 }
Dave Martincb84d112017-08-03 17:23:23 +0100557
558 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100559}
560
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200561/*
562 * Invalidate live CPU copies of task t's FPSIMD state
563 */
564void fpsimd_flush_task_state(struct task_struct *t)
565{
566 t->thread.fpsimd_state.cpu = NR_CPUS;
567}
568
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100569#ifdef CONFIG_KERNEL_MODE_NEON
570
Dave Martincb84d112017-08-03 17:23:23 +0100571DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +0100572EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100573
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100574/*
575 * Kernel-side NEON support functions
576 */
Dave Martincb84d112017-08-03 17:23:23 +0100577
578/*
579 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
580 * context
581 *
582 * Must not be called unless may_use_simd() returns true.
583 * Task context in the FPSIMD registers is saved back to memory as necessary.
584 *
585 * A matching call to kernel_neon_end() must be made before returning from the
586 * calling context.
587 *
588 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
589 * called.
590 */
591void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100592{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000593 if (WARN_ON(!system_supports_fpsimd()))
594 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100595
Dave Martincb84d112017-08-03 17:23:23 +0100596 BUG_ON(!may_use_simd());
597
598 local_bh_disable();
599
600 __this_cpu_write(kernel_neon_busy, true);
601
602 /* Save unsaved task fpsimd state, if any: */
603 if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
604 fpsimd_save_state(&current->thread.fpsimd_state);
605
606 /* Invalidate any task state remaining in the fpsimd regs: */
607 __this_cpu_write(fpsimd_last_state, NULL);
608
609 preempt_disable();
610
611 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100612}
Dave Martincb84d112017-08-03 17:23:23 +0100613EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100614
Dave Martincb84d112017-08-03 17:23:23 +0100615/*
616 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
617 *
618 * Must be called from a context in which kernel_neon_begin() was previously
619 * called, with no call to kernel_neon_end() in the meantime.
620 *
621 * The caller must not use the FPSIMD registers after this function is called,
622 * unless kernel_neon_begin() is called again in the meantime.
623 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100624void kernel_neon_end(void)
625{
Dave Martincb84d112017-08-03 17:23:23 +0100626 bool busy;
627
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000628 if (!system_supports_fpsimd())
629 return;
Dave Martincb84d112017-08-03 17:23:23 +0100630
631 busy = __this_cpu_xchg(kernel_neon_busy, false);
632 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
633
634 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100635}
636EXPORT_SYMBOL(kernel_neon_end);
637
Dave Martine580b8b2017-09-18 09:40:12 +0100638#ifdef CONFIG_EFI
639
Dave Martin3b660232017-08-18 14:53:47 +0100640static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
641static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +0100642
643/*
644 * EFI runtime services support functions
645 *
646 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
647 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
648 * is always used rather than being an optional accelerator.
649 *
650 * These functions provide the necessary support for ensuring FPSIMD
651 * save/restore in the contexts from which EFI is used.
652 *
653 * Do not use them for any other purpose -- if tempted to do so, you are
654 * either doing something wrong or you need to propose some refactoring.
655 */
656
657/*
658 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
659 */
660void __efi_fpsimd_begin(void)
661{
662 if (!system_supports_fpsimd())
663 return;
664
665 WARN_ON(preemptible());
666
667 if (may_use_simd())
668 kernel_neon_begin();
669 else {
670 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
671 __this_cpu_write(efi_fpsimd_state_used, true);
672 }
673}
674
675/*
676 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
677 */
678void __efi_fpsimd_end(void)
679{
680 if (!system_supports_fpsimd())
681 return;
682
683 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
684 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
685 else
686 kernel_neon_end();
687}
688
Dave Martine580b8b2017-09-18 09:40:12 +0100689#endif /* CONFIG_EFI */
690
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100691#endif /* CONFIG_KERNEL_MODE_NEON */
692
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100693#ifdef CONFIG_CPU_PM
694static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
695 unsigned long cmd, void *v)
696{
697 switch (cmd) {
698 case CPU_PM_ENTER:
Dave Martinbc0ee472017-10-31 15:51:05 +0000699 if (current->mm)
700 task_fpsimd_save();
Leo Yan7c68a9c2014-09-01 11:09:51 +0800701 this_cpu_write(fpsimd_last_state, NULL);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100702 break;
703 case CPU_PM_EXIT:
704 if (current->mm)
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200705 set_thread_flag(TIF_FOREIGN_FPSTATE);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100706 break;
707 case CPU_PM_ENTER_FAILED:
708 default:
709 return NOTIFY_DONE;
710 }
711 return NOTIFY_OK;
712}
713
714static struct notifier_block fpsimd_cpu_pm_notifier_block = {
715 .notifier_call = fpsimd_cpu_pm_notifier,
716};
717
Jisheng Zhanga7c61a32015-11-20 17:59:10 +0800718static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100719{
720 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
721}
722
723#else
724static inline void fpsimd_pm_init(void) { }
725#endif /* CONFIG_CPU_PM */
726
Janet Liu32365e62015-06-11 12:02:45 +0800727#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200728static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +0800729{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200730 per_cpu(fpsimd_last_state, cpu) = NULL;
731 return 0;
Janet Liu32365e62015-06-11 12:02:45 +0800732}
733
Janet Liu32365e62015-06-11 12:02:45 +0800734static inline void fpsimd_hotplug_init(void)
735{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200736 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
737 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +0800738}
739
740#else
741static inline void fpsimd_hotplug_init(void) { }
742#endif
743
Catalin Marinas53631b52012-03-05 11:49:32 +0000744/*
745 * FP/SIMD support code initialisation.
746 */
747static int __init fpsimd_init(void)
748{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100749 if (elf_hwcap & HWCAP_FP) {
750 fpsimd_pm_init();
751 fpsimd_hotplug_init();
752 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +0000753 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +0000754 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000755
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100756 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +0000757 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100758
Catalin Marinas53631b52012-03-05 11:49:32 +0000759 return 0;
760}
761late_initcall(fpsimd_init);