1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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>
26
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
90
91
92
93
94void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
95{
96
97 WARN_ON(1);
98}
99
100
101
102
103void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
104{
105 siginfo_t info;
106 unsigned int si_code = 0;
107
108 if (esr & FPEXC_IOF)
109 si_code = FPE_FLTINV;
110 else if (esr & FPEXC_DZF)
111 si_code = FPE_FLTDIV;
112 else if (esr & FPEXC_OFF)
113 si_code = FPE_FLTOVF;
114 else if (esr & FPEXC_UFF)
115 si_code = FPE_FLTUND;
116 else if (esr & FPEXC_IXF)
117 si_code = FPE_FLTRES;
118
119 memset(&info, 0, sizeof(info));
120 info.si_signo = SIGFPE;
121 info.si_code = si_code;
122 info.si_addr = (void __user *)instruction_pointer(regs);
123
124 send_sig_info(SIGFPE, &info, current);
125}
126
127void fpsimd_thread_switch(struct task_struct *next)
128{
129
130
131
132
133
134 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
135 fpsimd_save_state(¤t->thread.fpsimd_state);
136
137 if (next->mm) {
138
139
140
141
142
143
144
145 struct fpsimd_state *st = &next->thread.fpsimd_state;
146
147 if (__this_cpu_read(fpsimd_last_state) == st
148 && st->cpu == smp_processor_id())
149 clear_ti_thread_flag(task_thread_info(next),
150 TIF_FOREIGN_FPSTATE);
151 else
152 set_ti_thread_flag(task_thread_info(next),
153 TIF_FOREIGN_FPSTATE);
154 }
155}
156
157void fpsimd_flush_thread(void)
158{
159 memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
160 set_thread_flag(TIF_FOREIGN_FPSTATE);
161}
162
163
164
165
166
167void fpsimd_preserve_current_state(void)
168{
169 preempt_disable();
170 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
171 fpsimd_save_state(¤t->thread.fpsimd_state);
172 preempt_enable();
173}
174
175
176
177
178
179
180void fpsimd_restore_current_state(void)
181{
182 preempt_disable();
183 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
184 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
185
186 fpsimd_load_state(st);
187 this_cpu_write(fpsimd_last_state, st);
188 st->cpu = smp_processor_id();
189 }
190 preempt_enable();
191}
192
193
194
195
196
197
198void fpsimd_update_current_state(struct fpsimd_state *state)
199{
200 preempt_disable();
201 fpsimd_load_state(state);
202 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
203 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
204
205 this_cpu_write(fpsimd_last_state, st);
206 st->cpu = smp_processor_id();
207 }
208 preempt_enable();
209}
210
211
212
213
214void fpsimd_flush_task_state(struct task_struct *t)
215{
216 t->thread.fpsimd_state.cpu = NR_CPUS;
217}
218
219#ifdef CONFIG_KERNEL_MODE_NEON
220
221static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
222static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
223
224
225
226
227void kernel_neon_begin_partial(u32 num_regs)
228{
229 if (in_interrupt()) {
230 struct fpsimd_partial_state *s = this_cpu_ptr(
231 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
232
233 BUG_ON(num_regs > 32);
234 fpsimd_save_partial_state(s, roundup(num_regs, 2));
235 } else {
236
237
238
239
240
241
242 preempt_disable();
243 if (current->mm &&
244 !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
245 fpsimd_save_state(¤t->thread.fpsimd_state);
246 this_cpu_write(fpsimd_last_state, NULL);
247 }
248}
249EXPORT_SYMBOL(kernel_neon_begin_partial);
250
251void kernel_neon_end(void)
252{
253 if (in_interrupt()) {
254 struct fpsimd_partial_state *s = this_cpu_ptr(
255 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
256 fpsimd_load_partial_state(s);
257 } else {
258 preempt_enable();
259 }
260}
261EXPORT_SYMBOL(kernel_neon_end);
262
263#endif
264
265#ifdef CONFIG_CPU_PM
266static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
267 unsigned long cmd, void *v)
268{
269 switch (cmd) {
270 case CPU_PM_ENTER:
271 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
272 fpsimd_save_state(¤t->thread.fpsimd_state);
273 this_cpu_write(fpsimd_last_state, NULL);
274 break;
275 case CPU_PM_EXIT:
276 if (current->mm)
277 set_thread_flag(TIF_FOREIGN_FPSTATE);
278 break;
279 case CPU_PM_ENTER_FAILED:
280 default:
281 return NOTIFY_DONE;
282 }
283 return NOTIFY_OK;
284}
285
286static struct notifier_block fpsimd_cpu_pm_notifier_block = {
287 .notifier_call = fpsimd_cpu_pm_notifier,
288};
289
290static void fpsimd_pm_init(void)
291{
292 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
293}
294
295#else
296static inline void fpsimd_pm_init(void) { }
297#endif
298
299
300
301
302static int __init fpsimd_init(void)
303{
304 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
305
306 if (pfr & (0xf << 16)) {
307 pr_notice("Floating-point is not implemented\n");
308 return 0;
309 }
310 elf_hwcap |= HWCAP_FP;
311
312 if (pfr & (0xf << 20))
313 pr_notice("Advanced SIMD is not implemented\n");
314 else
315 elf_hwcap |= HWCAP_ASIMD;
316
317 fpsimd_pm_init();
318
319 return 0;
320}
321late_initcall(fpsimd_init);
322