blob: 8a97163debc77e9e9c0d7aaf7bfbda0ec6e392fe [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
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010020#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000021#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/sched.h>
24#include <linux/signal.h>
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +010025#include <linux/hardirq.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000026
27#include <asm/fpsimd.h>
28#include <asm/cputype.h>
29
30#define FPEXC_IOF (1 << 0)
31#define FPEXC_DZF (1 << 1)
32#define FPEXC_OFF (1 << 2)
33#define FPEXC_UFF (1 << 3)
34#define FPEXC_IXF (1 << 4)
35#define FPEXC_IDF (1 << 7)
36
37/*
38 * Trapped FP/ASIMD access.
39 */
40void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
41{
42 /* TODO: implement lazy context saving/restoring */
43 WARN_ON(1);
44}
45
46/*
47 * Raise a SIGFPE for the current process.
48 */
49void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
50{
51 siginfo_t info;
52 unsigned int si_code = 0;
53
54 if (esr & FPEXC_IOF)
55 si_code = FPE_FLTINV;
56 else if (esr & FPEXC_DZF)
57 si_code = FPE_FLTDIV;
58 else if (esr & FPEXC_OFF)
59 si_code = FPE_FLTOVF;
60 else if (esr & FPEXC_UFF)
61 si_code = FPE_FLTUND;
62 else if (esr & FPEXC_IXF)
63 si_code = FPE_FLTRES;
64
65 memset(&info, 0, sizeof(info));
66 info.si_signo = SIGFPE;
67 info.si_code = si_code;
68 info.si_addr = (void __user *)instruction_pointer(regs);
69
70 send_sig_info(SIGFPE, &info, current);
71}
72
73void fpsimd_thread_switch(struct task_struct *next)
74{
75 /* check if not kernel threads */
76 if (current->mm)
77 fpsimd_save_state(&current->thread.fpsimd_state);
78 if (next->mm)
79 fpsimd_load_state(&next->thread.fpsimd_state);
80}
81
82void fpsimd_flush_thread(void)
83{
Jiang Liu6db83ce2013-09-27 09:04:41 +010084 preempt_disable();
Catalin Marinas53631b52012-03-05 11:49:32 +000085 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
86 fpsimd_load_state(&current->thread.fpsimd_state);
Jiang Liu6db83ce2013-09-27 09:04:41 +010087 preempt_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +000088}
89
Ard Biesheuvelc51f9262014-02-24 15:26:27 +010090/*
91 * Save the userland FPSIMD state of 'current' to memory
92 */
93void fpsimd_preserve_current_state(void)
94{
95 preempt_disable();
96 fpsimd_save_state(&current->thread.fpsimd_state);
97 preempt_enable();
98}
99
100/*
101 * Load an updated userland FPSIMD state for 'current' from memory
102 */
103void fpsimd_update_current_state(struct fpsimd_state *state)
104{
105 preempt_disable();
106 fpsimd_load_state(state);
107 preempt_enable();
108}
109
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100110#ifdef CONFIG_KERNEL_MODE_NEON
111
112/*
113 * Kernel-side NEON support functions
114 */
115void kernel_neon_begin(void)
116{
117 /* Avoid using the NEON in interrupt context */
118 BUG_ON(in_interrupt());
119 preempt_disable();
120
121 if (current->mm)
122 fpsimd_save_state(&current->thread.fpsimd_state);
123}
124EXPORT_SYMBOL(kernel_neon_begin);
125
126void kernel_neon_end(void)
127{
128 if (current->mm)
129 fpsimd_load_state(&current->thread.fpsimd_state);
130
131 preempt_enable();
132}
133EXPORT_SYMBOL(kernel_neon_end);
134
135#endif /* CONFIG_KERNEL_MODE_NEON */
136
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100137#ifdef CONFIG_CPU_PM
138static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
139 unsigned long cmd, void *v)
140{
141 switch (cmd) {
142 case CPU_PM_ENTER:
143 if (current->mm)
144 fpsimd_save_state(&current->thread.fpsimd_state);
145 break;
146 case CPU_PM_EXIT:
147 if (current->mm)
148 fpsimd_load_state(&current->thread.fpsimd_state);
149 break;
150 case CPU_PM_ENTER_FAILED:
151 default:
152 return NOTIFY_DONE;
153 }
154 return NOTIFY_OK;
155}
156
157static struct notifier_block fpsimd_cpu_pm_notifier_block = {
158 .notifier_call = fpsimd_cpu_pm_notifier,
159};
160
161static void fpsimd_pm_init(void)
162{
163 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
164}
165
166#else
167static inline void fpsimd_pm_init(void) { }
168#endif /* CONFIG_CPU_PM */
169
Catalin Marinas53631b52012-03-05 11:49:32 +0000170/*
171 * FP/SIMD support code initialisation.
172 */
173static int __init fpsimd_init(void)
174{
175 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
176
177 if (pfr & (0xf << 16)) {
178 pr_notice("Floating-point is not implemented\n");
179 return 0;
180 }
181 elf_hwcap |= HWCAP_FP;
182
183 if (pfr & (0xf << 20))
184 pr_notice("Advanced SIMD is not implemented\n");
185 else
186 elf_hwcap |= HWCAP_ASIMD;
187
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +0100188 fpsimd_pm_init();
189
Catalin Marinas53631b52012-03-05 11:49:32 +0000190 return 0;
191}
192late_initcall(fpsimd_init);