blob: 138fcfaeadc1d55400a83d23e6e6e09d506c00fb [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>
Janet Liu32365e62015-06-11 12:02:45 +080021#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010022#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000023#include <linux/kernel.h>
24#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010025#include <linux/percpu.h>
Dave Martin4328825d2017-08-03 17:23:22 +010026#include <linux/preempt.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010027#include <linux/sched/signal.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000028#include <linux/signal.h>
29
30#include <asm/fpsimd.h>
31#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010032#include <asm/simd.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000033
34#define FPEXC_IOF (1 << 0)
35#define FPEXC_DZF (1 << 1)
36#define FPEXC_OFF (1 << 2)
37#define FPEXC_UFF (1 << 3)
38#define FPEXC_IXF (1 << 4)
39#define FPEXC_IDF (1 << 7)
40
41/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020042 * In order to reduce the number of times the FPSIMD state is needlessly saved
43 * and restored, we need to keep track of two things:
44 * (a) for each task, we need to remember which CPU was the last one to have
45 * the task's FPSIMD state loaded into its FPSIMD registers;
46 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
47 * been loaded into its FPSIMD registers most recently, or whether it has
48 * been used to perform kernel mode NEON in the meantime.
49 *
50 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
Adam Buchbinderef769e32016-02-24 09:52:41 -080051 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020052 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
53 * address of the userland FPSIMD state of the task that was loaded onto the CPU
54 * the most recently, or NULL if kernel mode NEON has been performed after that.
55 *
56 * With this in place, we no longer have to restore the next FPSIMD state right
57 * when switching between tasks. Instead, we can defer this check to userland
58 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
59 * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
60 * can omit the FPSIMD restore.
61 *
62 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
63 * indicate whether or not the userland FPSIMD state of the current task is
64 * present in the registers. The flag is set unless the FPSIMD registers of this
65 * CPU currently contain the most recent userland FPSIMD state of the current
66 * task.
67 *
Dave Martincb84d112017-08-03 17:23:23 +010068 * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
69 * save the task's FPSIMD context back to task_struct from softirq context.
70 * To prevent this from racing with the manipulation of the task's FPSIMD state
71 * from task context and thereby corrupting the state, it is necessary to
72 * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
73 * flag with local_bh_disable() unless softirqs are already masked.
74 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020075 * For a certain task, the sequence may look something like this:
76 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
77 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
78 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
79 * cleared, otherwise it is set;
80 *
81 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
82 * userland FPSIMD state is copied from memory to the registers, the task's
83 * fpsimd_state.cpu field is set to the id of the current CPU, the current
84 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
85 * TIF_FOREIGN_FPSTATE flag is cleared;
86 *
87 * - the task executes an ordinary syscall; upon return to userland, the
88 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
89 * restored;
90 *
91 * - the task executes a syscall which executes some NEON instructions; this is
92 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
93 * register contents to memory, clears the fpsimd_last_state per-cpu variable
94 * and sets the TIF_FOREIGN_FPSTATE flag;
95 *
96 * - the task gets preempted after kernel_neon_end() is called; as we have not
97 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
98 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
99 */
100static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
101
102/*
Catalin Marinas53631b52012-03-05 11:49:32 +0000103 * Trapped FP/ASIMD access.
104 */
105void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
106{
107 /* TODO: implement lazy context saving/restoring */
108 WARN_ON(1);
109}
110
111/*
112 * Raise a SIGFPE for the current process.
113 */
114void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
115{
116 siginfo_t info;
117 unsigned int si_code = 0;
118
119 if (esr & FPEXC_IOF)
120 si_code = FPE_FLTINV;
121 else if (esr & FPEXC_DZF)
122 si_code = FPE_FLTDIV;
123 else if (esr & FPEXC_OFF)
124 si_code = FPE_FLTOVF;
125 else if (esr & FPEXC_UFF)
126 si_code = FPE_FLTUND;
127 else if (esr & FPEXC_IXF)
128 si_code = FPE_FLTRES;
129
130 memset(&info, 0, sizeof(info));
131 info.si_signo = SIGFPE;
132 info.si_code = si_code;
133 info.si_addr = (void __user *)instruction_pointer(regs);
134
135 send_sig_info(SIGFPE, &info, current);
136}
137
138void fpsimd_thread_switch(struct task_struct *next)
139{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000140 if (!system_supports_fpsimd())
141 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200142 /*
143 * Save the current FPSIMD state to memory, but only if whatever is in
144 * the registers is in fact the most recent userland FPSIMD state of
145 * 'current'.
146 */
147 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
Catalin Marinas53631b52012-03-05 11:49:32 +0000148 fpsimd_save_state(&current->thread.fpsimd_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200149
150 if (next->mm) {
151 /*
152 * If we are switching to a task whose most recent userland
153 * FPSIMD state is already in the registers of *this* cpu,
154 * we can skip loading the state from memory. Otherwise, set
155 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
156 * upon the next return to userland.
157 */
158 struct fpsimd_state *st = &next->thread.fpsimd_state;
159
160 if (__this_cpu_read(fpsimd_last_state) == st
161 && st->cpu == smp_processor_id())
162 clear_ti_thread_flag(task_thread_info(next),
163 TIF_FOREIGN_FPSTATE);
164 else
165 set_ti_thread_flag(task_thread_info(next),
166 TIF_FOREIGN_FPSTATE);
167 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000168}
169
170void fpsimd_flush_thread(void)
171{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000172 if (!system_supports_fpsimd())
173 return;
Dave Martincb84d112017-08-03 17:23:23 +0100174
175 local_bh_disable();
176
Catalin Marinas53631b52012-03-05 11:49:32 +0000177 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100178 fpsimd_flush_task_state(current);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200179 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100180
181 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000182}
183
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100184/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200185 * Save the userland FPSIMD state of 'current' to memory, but only if the state
186 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100187 */
188void fpsimd_preserve_current_state(void)
189{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000190 if (!system_supports_fpsimd())
191 return;
Dave Martincb84d112017-08-03 17:23:23 +0100192
193 local_bh_disable();
194
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200195 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
196 fpsimd_save_state(&current->thread.fpsimd_state);
Dave Martincb84d112017-08-03 17:23:23 +0100197
198 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100199}
200
201/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200202 * Load the userland FPSIMD state of 'current' from memory, but only if the
203 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
204 * state of 'current'
205 */
206void fpsimd_restore_current_state(void)
207{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000208 if (!system_supports_fpsimd())
209 return;
Dave Martincb84d112017-08-03 17:23:23 +0100210
211 local_bh_disable();
212
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200213 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
214 struct fpsimd_state *st = &current->thread.fpsimd_state;
215
216 fpsimd_load_state(st);
Dave Martin50464182017-08-03 17:23:21 +0100217 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200218 st->cpu = smp_processor_id();
219 }
Dave Martincb84d112017-08-03 17:23:23 +0100220
221 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200222}
223
224/*
225 * Load an updated userland FPSIMD state for 'current' from memory and set the
226 * flag that indicates that the FPSIMD register contents are the most recent
227 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100228 */
229void fpsimd_update_current_state(struct fpsimd_state *state)
230{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000231 if (!system_supports_fpsimd())
232 return;
Dave Martincb84d112017-08-03 17:23:23 +0100233
234 local_bh_disable();
235
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100236 fpsimd_load_state(state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200237 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
238 struct fpsimd_state *st = &current->thread.fpsimd_state;
239
Dave Martin50464182017-08-03 17:23:21 +0100240 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200241 st->cpu = smp_processor_id();
242 }
Dave Martincb84d112017-08-03 17:23:23 +0100243
244 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100245}
246
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200247/*
248 * Invalidate live CPU copies of task t's FPSIMD state
249 */
250void fpsimd_flush_task_state(struct task_struct *t)
251{
252 t->thread.fpsimd_state.cpu = NR_CPUS;
253}
254
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100255#ifdef CONFIG_KERNEL_MODE_NEON
256
Dave Martincb84d112017-08-03 17:23:23 +0100257DEFINE_PER_CPU(bool, kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100258
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100259/*
260 * Kernel-side NEON support functions
261 */
Dave Martincb84d112017-08-03 17:23:23 +0100262
263/*
264 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
265 * context
266 *
267 * Must not be called unless may_use_simd() returns true.
268 * Task context in the FPSIMD registers is saved back to memory as necessary.
269 *
270 * A matching call to kernel_neon_end() must be made before returning from the
271 * calling context.
272 *
273 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
274 * called.
275 */
276void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100277{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000278 if (WARN_ON(!system_supports_fpsimd()))
279 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100280
Dave Martincb84d112017-08-03 17:23:23 +0100281 BUG_ON(!may_use_simd());
282
283 local_bh_disable();
284
285 __this_cpu_write(kernel_neon_busy, true);
286
287 /* Save unsaved task fpsimd state, if any: */
288 if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
289 fpsimd_save_state(&current->thread.fpsimd_state);
290
291 /* Invalidate any task state remaining in the fpsimd regs: */
292 __this_cpu_write(fpsimd_last_state, NULL);
293
294 preempt_disable();
295
296 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100297}
Dave Martincb84d112017-08-03 17:23:23 +0100298EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100299
Dave Martincb84d112017-08-03 17:23:23 +0100300/*
301 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
302 *
303 * Must be called from a context in which kernel_neon_begin() was previously
304 * called, with no call to kernel_neon_end() in the meantime.
305 *
306 * The caller must not use the FPSIMD registers after this function is called,
307 * unless kernel_neon_begin() is called again in the meantime.
308 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100309void kernel_neon_end(void)
310{
Dave Martincb84d112017-08-03 17:23:23 +0100311 bool busy;
312
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000313 if (!system_supports_fpsimd())
314 return;
Dave Martincb84d112017-08-03 17:23:23 +0100315
316 busy = __this_cpu_xchg(kernel_neon_busy, false);
317 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
318
319 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100320}
321EXPORT_SYMBOL(kernel_neon_end);
322
Dave Martin4328825d2017-08-03 17:23:22 +0100323DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
324DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
325
326/*
327 * EFI runtime services support functions
328 *
329 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
330 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
331 * is always used rather than being an optional accelerator.
332 *
333 * These functions provide the necessary support for ensuring FPSIMD
334 * save/restore in the contexts from which EFI is used.
335 *
336 * Do not use them for any other purpose -- if tempted to do so, you are
337 * either doing something wrong or you need to propose some refactoring.
338 */
339
340/*
341 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
342 */
343void __efi_fpsimd_begin(void)
344{
345 if (!system_supports_fpsimd())
346 return;
347
348 WARN_ON(preemptible());
349
350 if (may_use_simd())
351 kernel_neon_begin();
352 else {
353 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
354 __this_cpu_write(efi_fpsimd_state_used, true);
355 }
356}
357
358/*
359 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
360 */
361void __efi_fpsimd_end(void)
362{
363 if (!system_supports_fpsimd())
364 return;
365
366 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
367 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
368 else
369 kernel_neon_end();
370}
371
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100372#endif /* CONFIG_KERNEL_MODE_NEON */
373
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100374#ifdef CONFIG_CPU_PM
375static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
376 unsigned long cmd, void *v)
377{
378 switch (cmd) {
379 case CPU_PM_ENTER:
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200380 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100381 fpsimd_save_state(&current->thread.fpsimd_state);
Leo Yan7c68a9c2014-09-01 11:09:51 +0800382 this_cpu_write(fpsimd_last_state, NULL);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100383 break;
384 case CPU_PM_EXIT:
385 if (current->mm)
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200386 set_thread_flag(TIF_FOREIGN_FPSTATE);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100387 break;
388 case CPU_PM_ENTER_FAILED:
389 default:
390 return NOTIFY_DONE;
391 }
392 return NOTIFY_OK;
393}
394
395static struct notifier_block fpsimd_cpu_pm_notifier_block = {
396 .notifier_call = fpsimd_cpu_pm_notifier,
397};
398
Jisheng Zhanga7c61a32015-11-20 17:59:10 +0800399static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100400{
401 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
402}
403
404#else
405static inline void fpsimd_pm_init(void) { }
406#endif /* CONFIG_CPU_PM */
407
Janet Liu32365e62015-06-11 12:02:45 +0800408#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200409static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +0800410{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200411 per_cpu(fpsimd_last_state, cpu) = NULL;
412 return 0;
Janet Liu32365e62015-06-11 12:02:45 +0800413}
414
Janet Liu32365e62015-06-11 12:02:45 +0800415static inline void fpsimd_hotplug_init(void)
416{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200417 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
418 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +0800419}
420
421#else
422static inline void fpsimd_hotplug_init(void) { }
423#endif
424
Catalin Marinas53631b52012-03-05 11:49:32 +0000425/*
426 * FP/SIMD support code initialisation.
427 */
428static int __init fpsimd_init(void)
429{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100430 if (elf_hwcap & HWCAP_FP) {
431 fpsimd_pm_init();
432 fpsimd_hotplug_init();
433 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +0000434 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +0000435 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000436
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100437 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +0000438 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100439
Catalin Marinas53631b52012-03-05 11:49:32 +0000440 return 0;
441}
442late_initcall(fpsimd_init);