2 * FP/SIMD context switching and fault handling
4 * Copyright (C) 2012 ARM Ltd.
5 * Author: Catalin Marinas <catalin.marinas@arm.com>
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.
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.
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/>.
20 #include <linux/bottom_half.h>
21 #include <linux/cpu.h>
22 #include <linux/cpu_pm.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/percpu.h>
26 #include <linux/preempt.h>
27 #include <linux/sched/signal.h>
28 #include <linux/signal.h>
30 #include <asm/fpsimd.h>
31 #include <asm/cputype.h>
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)
42 * 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.
50 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
51 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
52 * 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.
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.
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
68 * 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.
75 * 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;
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;
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
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;
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.
100 static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
103 * Trapped FP/ASIMD access.
105 void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
107 /* TODO: implement lazy context saving/restoring */
112 * Raise a SIGFPE for the current process.
114 void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
117 unsigned int si_code = 0;
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;
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);
135 send_sig_info(SIGFPE, &info, current);
138 void fpsimd_thread_switch(struct task_struct *next)
140 if (!system_supports_fpsimd())
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
147 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
148 fpsimd_save_state(¤t->thread.fpsimd_state);
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.
158 struct fpsimd_state *st = &next->thread.fpsimd_state;
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);
165 set_ti_thread_flag(task_thread_info(next),
166 TIF_FOREIGN_FPSTATE);
170 void fpsimd_flush_thread(void)
172 if (!system_supports_fpsimd())
177 memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
178 fpsimd_flush_task_state(current);
179 set_thread_flag(TIF_FOREIGN_FPSTATE);
185 * 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'
188 void fpsimd_preserve_current_state(void)
190 if (!system_supports_fpsimd())
195 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
196 fpsimd_save_state(¤t->thread.fpsimd_state);
202 * 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
206 void fpsimd_restore_current_state(void)
208 if (!system_supports_fpsimd())
213 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
214 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
216 fpsimd_load_state(st);
217 __this_cpu_write(fpsimd_last_state, st);
218 st->cpu = smp_processor_id();
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'
229 void fpsimd_update_current_state(struct fpsimd_state *state)
231 if (!system_supports_fpsimd())
236 fpsimd_load_state(state);
237 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
238 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
240 __this_cpu_write(fpsimd_last_state, st);
241 st->cpu = smp_processor_id();
248 * Invalidate live CPU copies of task t's FPSIMD state
250 void fpsimd_flush_task_state(struct task_struct *t)
252 t->thread.fpsimd_state.cpu = NR_CPUS;
255 #ifdef CONFIG_KERNEL_MODE_NEON
257 DEFINE_PER_CPU(bool, kernel_neon_busy);
258 EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
261 * Kernel-side NEON support functions
265 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
268 * Must not be called unless may_use_simd() returns true.
269 * Task context in the FPSIMD registers is saved back to memory as necessary.
271 * A matching call to kernel_neon_end() must be made before returning from the
274 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
277 void kernel_neon_begin(void)
279 if (WARN_ON(!system_supports_fpsimd()))
282 BUG_ON(!may_use_simd());
286 __this_cpu_write(kernel_neon_busy, true);
288 /* Save unsaved task fpsimd state, if any: */
289 if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
290 fpsimd_save_state(¤t->thread.fpsimd_state);
292 /* Invalidate any task state remaining in the fpsimd regs: */
293 __this_cpu_write(fpsimd_last_state, NULL);
299 EXPORT_SYMBOL(kernel_neon_begin);
302 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
304 * Must be called from a context in which kernel_neon_begin() was previously
305 * called, with no call to kernel_neon_end() in the meantime.
307 * The caller must not use the FPSIMD registers after this function is called,
308 * unless kernel_neon_begin() is called again in the meantime.
310 void kernel_neon_end(void)
314 if (!system_supports_fpsimd())
317 busy = __this_cpu_xchg(kernel_neon_busy, false);
318 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
322 EXPORT_SYMBOL(kernel_neon_end);
324 static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
325 static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
328 * EFI runtime services support functions
330 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
331 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
332 * is always used rather than being an optional accelerator.
334 * These functions provide the necessary support for ensuring FPSIMD
335 * save/restore in the contexts from which EFI is used.
337 * Do not use them for any other purpose -- if tempted to do so, you are
338 * either doing something wrong or you need to propose some refactoring.
342 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
344 void __efi_fpsimd_begin(void)
346 if (!system_supports_fpsimd())
349 WARN_ON(preemptible());
354 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
355 __this_cpu_write(efi_fpsimd_state_used, true);
360 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
362 void __efi_fpsimd_end(void)
364 if (!system_supports_fpsimd())
367 if (__this_cpu_xchg(efi_fpsimd_state_used, false))
368 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
373 #endif /* CONFIG_KERNEL_MODE_NEON */
376 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
377 unsigned long cmd, void *v)
381 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
382 fpsimd_save_state(¤t->thread.fpsimd_state);
383 this_cpu_write(fpsimd_last_state, NULL);
387 set_thread_flag(TIF_FOREIGN_FPSTATE);
389 case CPU_PM_ENTER_FAILED:
396 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
397 .notifier_call = fpsimd_cpu_pm_notifier,
400 static void __init fpsimd_pm_init(void)
402 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
406 static inline void fpsimd_pm_init(void) { }
407 #endif /* CONFIG_CPU_PM */
409 #ifdef CONFIG_HOTPLUG_CPU
410 static int fpsimd_cpu_dead(unsigned int cpu)
412 per_cpu(fpsimd_last_state, cpu) = NULL;
416 static inline void fpsimd_hotplug_init(void)
418 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
419 NULL, fpsimd_cpu_dead);
423 static inline void fpsimd_hotplug_init(void) { }
427 * FP/SIMD support code initialisation.
429 static int __init fpsimd_init(void)
431 if (elf_hwcap & HWCAP_FP) {
433 fpsimd_hotplug_init();
435 pr_notice("Floating-point is not implemented\n");
438 if (!(elf_hwcap & HWCAP_ASIMD))
439 pr_notice("Advanced SIMD is not implemented\n");
443 late_initcall(fpsimd_init);