1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/cpu.h>
21#include <linux/cpu_pm.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/sched.h>
25#include <linux/signal.h>
26#include <linux/hardirq.h>
27
28#include <asm/fpsimd.h>
29#include <asm/cputype.h>
30
31#define FPEXC_IOF (1 << 0)
32#define FPEXC_DZF (1 << 1)
33#define FPEXC_OFF (1 << 2)
34#define FPEXC_UFF (1 << 3)
35#define FPEXC_IXF (1 << 4)
36#define FPEXC_IDF (1 << 7)
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
89
90static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
91
92
93
94
95void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
96{
97
98 WARN_ON(1);
99}
100
101
102
103
104void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
105{
106 siginfo_t info;
107 unsigned int si_code = 0;
108
109 if (esr & FPEXC_IOF)
110 si_code = FPE_FLTINV;
111 else if (esr & FPEXC_DZF)
112 si_code = FPE_FLTDIV;
113 else if (esr & FPEXC_OFF)
114 si_code = FPE_FLTOVF;
115 else if (esr & FPEXC_UFF)
116 si_code = FPE_FLTUND;
117 else if (esr & FPEXC_IXF)
118 si_code = FPE_FLTRES;
119
120 memset(&info, 0, sizeof(info));
121 info.si_signo = SIGFPE;
122 info.si_code = si_code;
123 info.si_addr = (void __user *)instruction_pointer(regs);
124
125 send_sig_info(SIGFPE, &info, current);
126}
127
128void fpsimd_thread_switch(struct task_struct *next)
129{
130
131
132
133
134
135 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
136 fpsimd_save_state(¤t->thread.fpsimd_state);
137
138 if (next->mm) {
139
140
141
142
143
144
145
146 struct fpsimd_state *st = &next->thread.fpsimd_state;
147
148 if (__this_cpu_read(fpsimd_last_state) == st
149 && st->cpu == smp_processor_id())
150 clear_ti_thread_flag(task_thread_info(next),
151 TIF_FOREIGN_FPSTATE);
152 else
153 set_ti_thread_flag(task_thread_info(next),
154 TIF_FOREIGN_FPSTATE);
155 }
156}
157
158void fpsimd_flush_thread(void)
159{
160 memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
161 fpsimd_flush_task_state(current);
162 set_thread_flag(TIF_FOREIGN_FPSTATE);
163}
164
165
166
167
168
169void fpsimd_preserve_current_state(void)
170{
171 preempt_disable();
172 if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
173 fpsimd_save_state(¤t->thread.fpsimd_state);
174 preempt_enable();
175}
176
177
178
179
180
181
182void fpsimd_restore_current_state(void)
183{
184 preempt_disable();
185 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
186 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
187
188 fpsimd_load_state(st);
189 this_cpu_write(fpsimd_last_state, st);
190 st->cpu = smp_processor_id();
191 }
192 preempt_enable();
193}
194
195
196
197
198
199
200void fpsimd_update_current_state(struct fpsimd_state *state)
201{
202 preempt_disable();
203 fpsimd_load_state(state);
204 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
205 struct fpsimd_state *st = ¤t->thread.fpsimd_state;
206
207 this_cpu_write(fpsimd_last_state, st);
208 st->cpu = smp_processor_id();
209 }
210 preempt_enable();
211}
212
213
214
215
216void fpsimd_flush_task_state(struct task_struct *t)
217{
218 t->thread.fpsimd_state.cpu = NR_CPUS;
219}
220
221#ifdef CONFIG_KERNEL_MODE_NEON
222
223static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
224static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
225
226
227
228
229void kernel_neon_begin_partial(u32 num_regs)
230{
231 if (in_interrupt()) {
232 struct fpsimd_partial_state *s = this_cpu_ptr(
233 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
234
235 BUG_ON(num_regs > 32);
236 fpsimd_save_partial_state(s, roundup(num_regs, 2));
237 } else {
238
239
240
241
242
243
244 preempt_disable();
245 if (current->mm &&
246 !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
247 fpsimd_save_state(¤t->thread.fpsimd_state);
248 this_cpu_write(fpsimd_last_state, NULL);
249 }
250}
251EXPORT_SYMBOL(kernel_neon_begin_partial);
252
253void kernel_neon_end(void)
254{
255 if (in_interrupt()) {
256 struct fpsimd_partial_state *s = this_cpu_ptr(
257 in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
258 fpsimd_load_partial_state(s);
259 } else {
260 preempt_enable();
261 }
262}
263EXPORT_SYMBOL(kernel_neon_end);
264
265#endif
266
267#ifdef CONFIG_CPU_PM
268static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
269 unsigned long cmd, void *v)
270{
271 switch (cmd) {
272 case CPU_PM_ENTER:
273 if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
274 fpsimd_save_state(¤t->thread.fpsimd_state);
275 this_cpu_write(fpsimd_last_state, NULL);
276 break;
277 case CPU_PM_EXIT:
278 if (current->mm)
279 set_thread_flag(TIF_FOREIGN_FPSTATE);
280 break;
281 case CPU_PM_ENTER_FAILED:
282 default:
283 return NOTIFY_DONE;
284 }
285 return NOTIFY_OK;
286}
287
288static struct notifier_block fpsimd_cpu_pm_notifier_block = {
289 .notifier_call = fpsimd_cpu_pm_notifier,
290};
291
292static void __init fpsimd_pm_init(void)
293{
294 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
295}
296
297#else
298static inline void fpsimd_pm_init(void) { }
299#endif
300
301#ifdef CONFIG_HOTPLUG_CPU
302static int fpsimd_cpu_dead(unsigned int cpu)
303{
304 per_cpu(fpsimd_last_state, cpu) = NULL;
305 return 0;
306}
307
308static inline void fpsimd_hotplug_init(void)
309{
310 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
311 NULL, fpsimd_cpu_dead);
312}
313
314#else
315static inline void fpsimd_hotplug_init(void) { }
316#endif
317
318
319
320
321static int __init fpsimd_init(void)
322{
323 if (elf_hwcap & HWCAP_FP) {
324 fpsimd_pm_init();
325 fpsimd_hotplug_init();
326 } else {
327 pr_notice("Floating-point is not implemented\n");
328 }
329
330 if (!(elf_hwcap & HWCAP_ASIMD))
331 pr_notice("Advanced SIMD is not implemented\n");
332
333 return 0;
334}
335late_initcall(fpsimd_init);
336