blob: bcde88e2d98186373c420f0106cc2da25d0ce022 [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
Janet Liu32365e62015-06-11 12:02:45 +080020#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010021#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000022#include <linux/kernel.h>
23#include <linux/init.h>
Dave Martin4328825d2017-08-03 17:23:22 +010024#include <linux/preempt.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010025#include <linux/sched/signal.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000026#include <linux/signal.h>
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +010027#include <linux/hardirq.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000028
29#include <asm/fpsimd.h>
30#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010031#include <asm/neon.h>
32#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 *
68 * For a certain task, the sequence may look something like this:
69 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
70 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
71 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
72 * cleared, otherwise it is set;
73 *
74 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
75 * userland FPSIMD state is copied from memory to the registers, the task's
76 * fpsimd_state.cpu field is set to the id of the current CPU, the current
77 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
78 * TIF_FOREIGN_FPSTATE flag is cleared;
79 *
80 * - the task executes an ordinary syscall; upon return to userland, the
81 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
82 * restored;
83 *
84 * - the task executes a syscall which executes some NEON instructions; this is
85 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
86 * register contents to memory, clears the fpsimd_last_state per-cpu variable
87 * and sets the TIF_FOREIGN_FPSTATE flag;
88 *
89 * - the task gets preempted after kernel_neon_end() is called; as we have not
90 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
91 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
92 */
93static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
94
95/*
Catalin Marinas53631b52012-03-05 11:49:32 +000096 * Trapped FP/ASIMD access.
97 */
98void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
99{
100 /* TODO: implement lazy context saving/restoring */
101 WARN_ON(1);
102}
103
104/*
105 * Raise a SIGFPE for the current process.
106 */
107void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
108{
109 siginfo_t info;
110 unsigned int si_code = 0;
111
112 if (esr & FPEXC_IOF)
113 si_code = FPE_FLTINV;
114 else if (esr & FPEXC_DZF)
115 si_code = FPE_FLTDIV;
116 else if (esr & FPEXC_OFF)
117 si_code = FPE_FLTOVF;
118 else if (esr & FPEXC_UFF)
119 si_code = FPE_FLTUND;
120 else if (esr & FPEXC_IXF)
121 si_code = FPE_FLTRES;
122
123 memset(&info, 0, sizeof(info));
124 info.si_signo = SIGFPE;
125 info.si_code = si_code;
126 info.si_addr = (void __user *)instruction_pointer(regs);
127
128 send_sig_info(SIGFPE, &info, current);
129}
130
131void fpsimd_thread_switch(struct task_struct *next)
132{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000133 if (!system_supports_fpsimd())
134 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200135 /*
136 * Save the current FPSIMD state to memory, but only if whatever is in
137 * the registers is in fact the most recent userland FPSIMD state of
138 * 'current'.
139 */
140 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
Catalin Marinas53631b52012-03-05 11:49:32 +0000141 fpsimd_save_state(&current->thread.fpsimd_state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200142
143 if (next->mm) {
144 /*
145 * If we are switching to a task whose most recent userland
146 * FPSIMD state is already in the registers of *this* cpu,
147 * we can skip loading the state from memory. Otherwise, set
148 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
149 * upon the next return to userland.
150 */
151 struct fpsimd_state *st = &next->thread.fpsimd_state;
152
153 if (__this_cpu_read(fpsimd_last_state) == st
154 && st->cpu == smp_processor_id())
155 clear_ti_thread_flag(task_thread_info(next),
156 TIF_FOREIGN_FPSTATE);
157 else
158 set_ti_thread_flag(task_thread_info(next),
159 TIF_FOREIGN_FPSTATE);
160 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000161}
162
163void fpsimd_flush_thread(void)
164{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000165 if (!system_supports_fpsimd())
166 return;
Catalin Marinas53631b52012-03-05 11:49:32 +0000167 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100168 fpsimd_flush_task_state(current);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200169 set_thread_flag(TIF_FOREIGN_FPSTATE);
Catalin Marinas53631b52012-03-05 11:49:32 +0000170}
171
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100172/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200173 * Save the userland FPSIMD state of 'current' to memory, but only if the state
174 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100175 */
176void fpsimd_preserve_current_state(void)
177{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000178 if (!system_supports_fpsimd())
179 return;
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100180 preempt_disable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200181 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
182 fpsimd_save_state(&current->thread.fpsimd_state);
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100183 preempt_enable();
184}
185
186/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200187 * Load the userland FPSIMD state of 'current' from memory, but only if the
188 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
189 * state of 'current'
190 */
191void fpsimd_restore_current_state(void)
192{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000193 if (!system_supports_fpsimd())
194 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200195 preempt_disable();
196 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
197 struct fpsimd_state *st = &current->thread.fpsimd_state;
198
199 fpsimd_load_state(st);
Dave Martin50464182017-08-03 17:23:21 +0100200 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200201 st->cpu = smp_processor_id();
202 }
203 preempt_enable();
204}
205
206/*
207 * Load an updated userland FPSIMD state for 'current' from memory and set the
208 * flag that indicates that the FPSIMD register contents are the most recent
209 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100210 */
211void fpsimd_update_current_state(struct fpsimd_state *state)
212{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000213 if (!system_supports_fpsimd())
214 return;
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100215 preempt_disable();
216 fpsimd_load_state(state);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200217 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
218 struct fpsimd_state *st = &current->thread.fpsimd_state;
219
Dave Martin50464182017-08-03 17:23:21 +0100220 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200221 st->cpu = smp_processor_id();
222 }
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100223 preempt_enable();
224}
225
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200226/*
227 * Invalidate live CPU copies of task t's FPSIMD state
228 */
229void fpsimd_flush_task_state(struct task_struct *t)
230{
231 t->thread.fpsimd_state.cpu = NR_CPUS;
232}
233
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100234#ifdef CONFIG_KERNEL_MODE_NEON
235
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100236static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
237static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
238
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100239/*
240 * Kernel-side NEON support functions
241 */
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100242void kernel_neon_begin_partial(u32 num_regs)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100243{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000244 if (WARN_ON(!system_supports_fpsimd()))
245 return;
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100246 if (in_interrupt()) {
247 struct fpsimd_partial_state *s = this_cpu_ptr(
248 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100249
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100250 BUG_ON(num_regs > 32);
251 fpsimd_save_partial_state(s, roundup(num_regs, 2));
252 } else {
253 /*
254 * Save the userland FPSIMD state if we have one and if we
255 * haven't done so already. Clear fpsimd_last_state to indicate
256 * that there is no longer userland FPSIMD state in the
257 * registers.
258 */
259 preempt_disable();
260 if (current->mm &&
261 !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
262 fpsimd_save_state(&current->thread.fpsimd_state);
263 this_cpu_write(fpsimd_last_state, NULL);
264 }
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100265}
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100266EXPORT_SYMBOL(kernel_neon_begin_partial);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100267
268void kernel_neon_end(void)
269{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000270 if (!system_supports_fpsimd())
271 return;
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100272 if (in_interrupt()) {
273 struct fpsimd_partial_state *s = this_cpu_ptr(
274 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
275 fpsimd_load_partial_state(s);
276 } else {
277 preempt_enable();
278 }
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100279}
280EXPORT_SYMBOL(kernel_neon_end);
281
Dave Martin4328825d2017-08-03 17:23:22 +0100282DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
283DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
284
285/*
286 * EFI runtime services support functions
287 *
288 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
289 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
290 * is always used rather than being an optional accelerator.
291 *
292 * These functions provide the necessary support for ensuring FPSIMD
293 * save/restore in the contexts from which EFI is used.
294 *
295 * Do not use them for any other purpose -- if tempted to do so, you are
296 * either doing something wrong or you need to propose some refactoring.
297 */
298
299/*
300 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
301 */
302void __efi_fpsimd_begin(void)
303{
304 if (!system_supports_fpsimd())
305 return;
306
307 WARN_ON(preemptible());
308
309 if (may_use_simd())
310 kernel_neon_begin();
311 else {
312 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
313 __this_cpu_write(efi_fpsimd_state_used, true);
314 }
315}
316
317/*
318 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
319 */
320void __efi_fpsimd_end(void)
321{
322 if (!system_supports_fpsimd())
323 return;
324
325 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
326 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
327 else
328 kernel_neon_end();
329}
330
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100331#endif /* CONFIG_KERNEL_MODE_NEON */
332
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100333#ifdef CONFIG_CPU_PM
334static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
335 unsigned long cmd, void *v)
336{
337 switch (cmd) {
338 case CPU_PM_ENTER:
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200339 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100340 fpsimd_save_state(&current->thread.fpsimd_state);
Leo Yan7c68a9c2014-09-01 11:09:51 +0800341 this_cpu_write(fpsimd_last_state, NULL);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100342 break;
343 case CPU_PM_EXIT:
344 if (current->mm)
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200345 set_thread_flag(TIF_FOREIGN_FPSTATE);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100346 break;
347 case CPU_PM_ENTER_FAILED:
348 default:
349 return NOTIFY_DONE;
350 }
351 return NOTIFY_OK;
352}
353
354static struct notifier_block fpsimd_cpu_pm_notifier_block = {
355 .notifier_call = fpsimd_cpu_pm_notifier,
356};
357
Jisheng Zhanga7c61a32015-11-20 17:59:10 +0800358static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100359{
360 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
361}
362
363#else
364static inline void fpsimd_pm_init(void) { }
365#endif /* CONFIG_CPU_PM */
366
Janet Liu32365e62015-06-11 12:02:45 +0800367#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200368static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +0800369{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200370 per_cpu(fpsimd_last_state, cpu) = NULL;
371 return 0;
Janet Liu32365e62015-06-11 12:02:45 +0800372}
373
Janet Liu32365e62015-06-11 12:02:45 +0800374static inline void fpsimd_hotplug_init(void)
375{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +0200376 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
377 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +0800378}
379
380#else
381static inline void fpsimd_hotplug_init(void) { }
382#endif
383
Catalin Marinas53631b52012-03-05 11:49:32 +0000384/*
385 * FP/SIMD support code initialisation.
386 */
387static int __init fpsimd_init(void)
388{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100389 if (elf_hwcap & HWCAP_FP) {
390 fpsimd_pm_init();
391 fpsimd_hotplug_init();
392 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +0000393 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +0000394 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000395
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +0100396 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +0000397 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100398
Catalin Marinas53631b52012-03-05 11:49:32 +0000399 return 0;
400}
401late_initcall(fpsimd_init);