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/cpu_pm.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/signal.h>
26 #include <asm/fpsimd.h>
27 #include <asm/cputype.h>
29 #define FPEXC_IOF (1 << 0)
30 #define FPEXC_DZF (1 << 1)
31 #define FPEXC_OFF (1 << 2)
32 #define FPEXC_UFF (1 << 3)
33 #define FPEXC_IXF (1 << 4)
34 #define FPEXC_IDF (1 << 7)
37 * In order to reduce the number of times the FPSIMD state is needlessly saved
38 * and restored, we need to keep track of two things:
39 * (a) for each task, we need to remember which CPU was the last one to have
40 * the task's FPSIMD state loaded into its FPSIMD registers;
41 * (b) for each CPU, we need to remember which task's userland FPSIMD state has
42 * been loaded into its FPSIMD registers most recently, or whether it has
43 * been used to perform kernel mode NEON in the meantime.
45 * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
46 * the id of the current CPU everytime the state is loaded onto a CPU. For (b),
47 * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
48 * address of the userland FPSIMD state of the task that was loaded onto the CPU
49 * the most recently, or NULL if kernel mode NEON has been performed after that.
51 * With this in place, we no longer have to restore the next FPSIMD state right
52 * when switching between tasks. Instead, we can defer this check to userland
53 * resume, at which time we verify whether the CPU's fpsimd_last_state and the
54 * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
55 * can omit the FPSIMD restore.
57 * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
58 * indicate whether or not the userland FPSIMD state of the current task is
59 * present in the registers. The flag is set unless the FPSIMD registers of this
60 * CPU currently contain the most recent userland FPSIMD state of the current
63 * For a certain task, the sequence may look something like this:
64 * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
65 * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
66 * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
67 * cleared, otherwise it is set;
69 * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
70 * userland FPSIMD state is copied from memory to the registers, the task's
71 * fpsimd_state.cpu field is set to the id of the current CPU, the current
72 * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
73 * TIF_FOREIGN_FPSTATE flag is cleared;
75 * - the task executes an ordinary syscall; upon return to userland, the
76 * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
79 * - the task executes a syscall which executes some NEON instructions; this is
80 * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
81 * register contents to memory, clears the fpsimd_last_state per-cpu variable
82 * and sets the TIF_FOREIGN_FPSTATE flag;
84 * - the task gets preempted after kernel_neon_end() is called; as we have not
85 * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
86 * whatever is in the FPSIMD registers is not saved to memory, but discarded.
88 static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
91 * Trapped FP/ASIMD access.
93 void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
95 /* TODO: implement lazy context saving/restoring */
100 * Raise a SIGFPE for the current process.
102 void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
105 unsigned int si_code = 0;
108 si_code = FPE_FLTINV;
109 else if (esr & FPEXC_DZF)
110 si_code = FPE_FLTDIV;
111 else if (esr & FPEXC_OFF)
112 si_code = FPE_FLTOVF;
113 else if (esr & FPEXC_UFF)
114 si_code = FPE_FLTUND;
115 else if (esr & FPEXC_IXF)
116 si_code = FPE_FLTRES;
118 memset(&info, 0, sizeof(info));
119 info.si_signo = SIGFPE;
120 info.si_code = si_code;
121 info.si_addr = (void __user *)instruction_pointer(regs);
123 send_sig_info(SIGFPE, &info, current);
126 void fpsimd_thread_switch(struct task_struct *next)
129 * Save the current FPSIMD state to memory, but only if whatever is in
130 * the registers is in fact the most recent userland FPSIMD state of
133 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
134 fpsimd_save_state(¤t->thread.fpsimd_state);
138 * If we are switching to a task whose most recent userland
139 * FPSIMD state is already in the registers of *this* cpu,
140 * we can skip loading the state from memory. Otherwise, set
141 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
142 * upon the next return to userland.
144 struct fpsimd_state *st = &next->thread.fpsimd_state;
146 if (__this_cpu_read(fpsimd_last_state) == st
147 && st->cpu == smp_processor_id())
148 clear_ti_thread_flag(task_thread_info(next),
149 TIF_FOREIGN_FPSTATE);
151 set_ti_thread_flag(task_thread_info(next),
152 TIF_FOREIGN_FPSTATE);
156 void fpsimd_flush_thread(void)
159 memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
160 set_thread_flag(TIF_FOREIGN_FPSTATE);
165 * Save the userland FPSIMD state of 'current' to memory, but only if the state
166 * currently held in the registers does in fact belong to 'current'
168 void fpsimd_preserve_current_state(void)
171 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
172 fpsimd_save_state(¤t->thread.fpsimd_state);
177 * Load the userland FPSIMD state of 'current' from memory, but only if the
178 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
181 void fpsimd_restore_current_state(void)
184 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
185 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
187 fpsimd_load_state(st);
188 this_cpu_write(fpsimd_last_state, st);
189 st->cpu = smp_processor_id();
195 * Load an updated userland FPSIMD state for 'current' from memory and set the
196 * flag that indicates that the FPSIMD register contents are the most recent
197 * FPSIMD state of 'current'
199 void fpsimd_update_current_state(struct fpsimd_state *state)
202 fpsimd_load_state(state);
203 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
204 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
206 this_cpu_write(fpsimd_last_state, st);
207 st->cpu = smp_processor_id();
213 * Invalidate live CPU copies of task t's FPSIMD state
215 void fpsimd_flush_task_state(struct task_struct *t)
217 t->thread.fpsimd_state.cpu = NR_CPUS;
220 #ifdef CONFIG_KERNEL_MODE_NEON
222 static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
223 static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
226 * Kernel-side NEON support functions
228 void kernel_neon_begin_partial(u32 num_regs)
230 if (in_interrupt()) {
231 struct fpsimd_partial_state *s = this_cpu_ptr(
232 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
234 BUG_ON(num_regs > 32);
235 fpsimd_save_partial_state(s, roundup(num_regs, 2));
238 * Save the userland FPSIMD state if we have one and if we
239 * haven't done so already. Clear fpsimd_last_state to indicate
240 * that there is no longer userland FPSIMD state in the
245 !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
246 fpsimd_save_state(¤t->thread.fpsimd_state);
247 this_cpu_write(fpsimd_last_state, NULL);
250 EXPORT_SYMBOL(kernel_neon_begin_partial);
252 void kernel_neon_end(void)
254 if (in_interrupt()) {
255 struct fpsimd_partial_state *s = this_cpu_ptr(
256 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
257 fpsimd_load_partial_state(s);
262 EXPORT_SYMBOL(kernel_neon_end);
264 #endif /* CONFIG_KERNEL_MODE_NEON */
267 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
268 unsigned long cmd, void *v)
272 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
273 fpsimd_save_state(¤t->thread.fpsimd_state);
274 this_cpu_write(fpsimd_last_state, NULL);
278 set_thread_flag(TIF_FOREIGN_FPSTATE);
280 case CPU_PM_ENTER_FAILED:
287 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
288 .notifier_call = fpsimd_cpu_pm_notifier,
291 static void fpsimd_pm_init(void)
293 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
297 static inline void fpsimd_pm_init(void) { }
298 #endif /* CONFIG_CPU_PM */
301 * FP/SIMD support code initialisation.
303 static int __init fpsimd_init(void)
305 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
307 if (pfr & (0xf << 16)) {
308 pr_notice("Floating-point is not implemented\n");
311 elf_hwcap |= HWCAP_FP;
313 if (pfr & (0xf << 20))
314 pr_notice("Advanced SIMD is not implemented\n");
316 elf_hwcap |= HWCAP_ASIMD;
322 late_initcall(fpsimd_init);