Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / arch / arm64 / kernel / fpsimd.c
blob4aef42a04bdcc38baa84c2090ffc537d67770854
1 /*
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>
25 #include <linux/hardirq.h>
27 #include <asm/fpsimd.h>
28 #include <asm/cputype.h>
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)
38 * Trapped FP/ASIMD access.
40 void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
42 /* TODO: implement lazy context saving/restoring */
43 WARN_ON(1);
47 * Raise a SIGFPE for the current process.
49 void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
51 siginfo_t info;
52 unsigned int si_code = 0;
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;
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);
70 send_sig_info(SIGFPE, &info, current);
73 void fpsimd_thread_switch(struct task_struct *next)
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);
82 void fpsimd_flush_thread(void)
84 preempt_disable();
85 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
86 fpsimd_load_state(&current->thread.fpsimd_state);
87 preempt_enable();
90 #ifdef CONFIG_KERNEL_MODE_NEON
93 * Kernel-side NEON support functions
95 void kernel_neon_begin(void)
97 /* Avoid using the NEON in interrupt context */
98 BUG_ON(in_interrupt());
99 preempt_disable();
101 if (current->mm)
102 fpsimd_save_state(&current->thread.fpsimd_state);
104 EXPORT_SYMBOL(kernel_neon_begin);
106 void kernel_neon_end(void)
108 if (current->mm)
109 fpsimd_load_state(&current->thread.fpsimd_state);
111 preempt_enable();
113 EXPORT_SYMBOL(kernel_neon_end);
115 #endif /* CONFIG_KERNEL_MODE_NEON */
117 #ifdef CONFIG_CPU_PM
118 static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
119 unsigned long cmd, void *v)
121 switch (cmd) {
122 case CPU_PM_ENTER:
123 if (current->mm)
124 fpsimd_save_state(&current->thread.fpsimd_state);
125 break;
126 case CPU_PM_EXIT:
127 if (current->mm)
128 fpsimd_load_state(&current->thread.fpsimd_state);
129 break;
130 case CPU_PM_ENTER_FAILED:
131 default:
132 return NOTIFY_DONE;
134 return NOTIFY_OK;
137 static struct notifier_block fpsimd_cpu_pm_notifier_block = {
138 .notifier_call = fpsimd_cpu_pm_notifier,
141 static void fpsimd_pm_init(void)
143 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
146 #else
147 static inline void fpsimd_pm_init(void) { }
148 #endif /* CONFIG_CPU_PM */
151 * FP/SIMD support code initialisation.
153 static int __init fpsimd_init(void)
155 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
157 if (pfr & (0xf << 16)) {
158 pr_notice("Floating-point is not implemented\n");
159 return 0;
161 elf_hwcap |= HWCAP_FP;
163 if (pfr & (0xf << 20))
164 pr_notice("Advanced SIMD is not implemented\n");
165 else
166 elf_hwcap |= HWCAP_ASIMD;
168 fpsimd_pm_init();
170 return 0;
172 late_initcall(fpsimd_init);