ARM: 7417/1: vfp: ensure preemption is disabled when enabling VFP access
[firefly-linux-kernel-4.4.55.git] / arch / arm / vfp / vfpmodule.c
1 /*
2  *  linux/arch/arm/vfp/vfpmodule.c
3  *
4  *  Copyright (C) 2004 ARM Limited.
5  *  Written by Deep Blue Solutions Limited.
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 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/cpu.h>
14 #include <linux/cpu_pm.h>
15 #include <linux/hardirq.h>
16 #include <linux/kernel.h>
17 #include <linux/notifier.h>
18 #include <linux/signal.h>
19 #include <linux/sched.h>
20 #include <linux/smp.h>
21 #include <linux/init.h>
22
23 #include <asm/cputype.h>
24 #include <asm/thread_notify.h>
25 #include <asm/vfp.h>
26
27 #include "vfpinstr.h"
28 #include "vfp.h"
29
30 /*
31  * Our undef handlers (in entry.S)
32  */
33 void vfp_testing_entry(void);
34 void vfp_support_entry(void);
35 void vfp_null_entry(void);
36
37 void (*vfp_vector)(void) = vfp_null_entry;
38
39 /*
40  * The pointer to the vfpstate structure of the thread which currently
41  * owns the context held in the VFP hardware, or NULL if the hardware
42  * context is invalid.
43  */
44 union vfp_state *vfp_current_hw_state[NR_CPUS];
45
46 /*
47  * Dual-use variable.
48  * Used in startup: set to non-zero if VFP checks fail
49  * After startup, holds VFP architecture
50  */
51 unsigned int VFP_arch;
52
53 /*
54  * Per-thread VFP initialization.
55  */
56 static void vfp_thread_flush(struct thread_info *thread)
57 {
58         union vfp_state *vfp = &thread->vfpstate;
59         unsigned int cpu;
60
61         memset(vfp, 0, sizeof(union vfp_state));
62
63         vfp->hard.fpexc = FPEXC_EN;
64         vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
65
66         /*
67          * Disable VFP to ensure we initialize it first.  We must ensure
68          * that the modification of vfp_current_hw_state[] and hardware disable
69          * are done for the same CPU and without preemption.
70          */
71         cpu = get_cpu();
72         if (vfp_current_hw_state[cpu] == vfp)
73                 vfp_current_hw_state[cpu] = NULL;
74         fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
75         put_cpu();
76 }
77
78 static void vfp_thread_exit(struct thread_info *thread)
79 {
80         /* release case: Per-thread VFP cleanup. */
81         union vfp_state *vfp = &thread->vfpstate;
82         unsigned int cpu = get_cpu();
83
84         if (vfp_current_hw_state[cpu] == vfp)
85                 vfp_current_hw_state[cpu] = NULL;
86         put_cpu();
87 }
88
89 static void vfp_thread_copy(struct thread_info *thread)
90 {
91         struct thread_info *parent = current_thread_info();
92
93         vfp_sync_hwstate(parent);
94         thread->vfpstate = parent->vfpstate;
95 }
96
97 /*
98  * When this function is called with the following 'cmd's, the following
99  * is true while this function is being run:
100  *  THREAD_NOFTIFY_SWTICH:
101  *   - the previously running thread will not be scheduled onto another CPU.
102  *   - the next thread to be run (v) will not be running on another CPU.
103  *   - thread->cpu is the local CPU number
104  *   - not preemptible as we're called in the middle of a thread switch
105  *  THREAD_NOTIFY_FLUSH:
106  *   - the thread (v) will be running on the local CPU, so
107  *      v === current_thread_info()
108  *   - thread->cpu is the local CPU number at the time it is accessed,
109  *      but may change at any time.
110  *   - we could be preempted if tree preempt rcu is enabled, so
111  *      it is unsafe to use thread->cpu.
112  *  THREAD_NOTIFY_EXIT
113  *   - the thread (v) will be running on the local CPU, so
114  *      v === current_thread_info()
115  *   - thread->cpu is the local CPU number at the time it is accessed,
116  *      but may change at any time.
117  *   - we could be preempted if tree preempt rcu is enabled, so
118  *      it is unsafe to use thread->cpu.
119  */
120 static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
121 {
122         struct thread_info *thread = v;
123         u32 fpexc;
124 #ifdef CONFIG_SMP
125         unsigned int cpu;
126 #endif
127
128         switch (cmd) {
129         case THREAD_NOTIFY_SWITCH:
130                 fpexc = fmrx(FPEXC);
131
132 #ifdef CONFIG_SMP
133                 cpu = thread->cpu;
134
135                 /*
136                  * On SMP, if VFP is enabled, save the old state in
137                  * case the thread migrates to a different CPU. The
138                  * restoring is done lazily.
139                  */
140                 if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu]) {
141                         vfp_save_state(vfp_current_hw_state[cpu], fpexc);
142                         vfp_current_hw_state[cpu]->hard.cpu = cpu;
143                 }
144                 /*
145                  * Thread migration, just force the reloading of the
146                  * state on the new CPU in case the VFP registers
147                  * contain stale data.
148                  */
149                 if (thread->vfpstate.hard.cpu != cpu)
150                         vfp_current_hw_state[cpu] = NULL;
151 #endif
152
153                 /*
154                  * Always disable VFP so we can lazily save/restore the
155                  * old state.
156                  */
157                 fmxr(FPEXC, fpexc & ~FPEXC_EN);
158                 break;
159
160         case THREAD_NOTIFY_FLUSH:
161                 vfp_thread_flush(thread);
162                 break;
163
164         case THREAD_NOTIFY_EXIT:
165                 vfp_thread_exit(thread);
166                 break;
167
168         case THREAD_NOTIFY_COPY:
169                 vfp_thread_copy(thread);
170                 break;
171         }
172
173         return NOTIFY_DONE;
174 }
175
176 static struct notifier_block vfp_notifier_block = {
177         .notifier_call  = vfp_notifier,
178 };
179
180 /*
181  * Raise a SIGFPE for the current process.
182  * sicode describes the signal being raised.
183  */
184 static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
185 {
186         siginfo_t info;
187
188         memset(&info, 0, sizeof(info));
189
190         info.si_signo = SIGFPE;
191         info.si_code = sicode;
192         info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
193
194         /*
195          * This is the same as NWFPE, because it's not clear what
196          * this is used for
197          */
198         current->thread.error_code = 0;
199         current->thread.trap_no = 6;
200
201         send_sig_info(SIGFPE, &info, current);
202 }
203
204 static void vfp_panic(char *reason, u32 inst)
205 {
206         int i;
207
208         printk(KERN_ERR "VFP: Error: %s\n", reason);
209         printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
210                 fmrx(FPEXC), fmrx(FPSCR), inst);
211         for (i = 0; i < 32; i += 2)
212                 printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
213                        i, vfp_get_float(i), i+1, vfp_get_float(i+1));
214 }
215
216 /*
217  * Process bitmask of exception conditions.
218  */
219 static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
220 {
221         int si_code = 0;
222
223         pr_debug("VFP: raising exceptions %08x\n", exceptions);
224
225         if (exceptions == VFP_EXCEPTION_ERROR) {
226                 vfp_panic("unhandled bounce", inst);
227                 vfp_raise_sigfpe(0, regs);
228                 return;
229         }
230
231         /*
232          * If any of the status flags are set, update the FPSCR.
233          * Comparison instructions always return at least one of
234          * these flags set.
235          */
236         if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
237                 fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
238
239         fpscr |= exceptions;
240
241         fmxr(FPSCR, fpscr);
242
243 #define RAISE(stat,en,sig)                              \
244         if (exceptions & stat && fpscr & en)            \
245                 si_code = sig;
246
247         /*
248          * These are arranged in priority order, least to highest.
249          */
250         RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
251         RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
252         RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
253         RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
254         RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
255
256         if (si_code)
257                 vfp_raise_sigfpe(si_code, regs);
258 }
259
260 /*
261  * Emulate a VFP instruction.
262  */
263 static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
264 {
265         u32 exceptions = VFP_EXCEPTION_ERROR;
266
267         pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
268
269         if (INST_CPRTDO(inst)) {
270                 if (!INST_CPRT(inst)) {
271                         /*
272                          * CPDO
273                          */
274                         if (vfp_single(inst)) {
275                                 exceptions = vfp_single_cpdo(inst, fpscr);
276                         } else {
277                                 exceptions = vfp_double_cpdo(inst, fpscr);
278                         }
279                 } else {
280                         /*
281                          * A CPRT instruction can not appear in FPINST2, nor
282                          * can it cause an exception.  Therefore, we do not
283                          * have to emulate it.
284                          */
285                 }
286         } else {
287                 /*
288                  * A CPDT instruction can not appear in FPINST2, nor can
289                  * it cause an exception.  Therefore, we do not have to
290                  * emulate it.
291                  */
292         }
293         return exceptions & ~VFP_NAN_FLAG;
294 }
295
296 /*
297  * Package up a bounce condition.
298  */
299 void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
300 {
301         u32 fpscr, orig_fpscr, fpsid, exceptions;
302
303         pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
304
305         /*
306          * At this point, FPEXC can have the following configuration:
307          *
308          *  EX DEX IXE
309          *  0   1   x   - synchronous exception
310          *  1   x   0   - asynchronous exception
311          *  1   x   1   - sychronous on VFP subarch 1 and asynchronous on later
312          *  0   0   1   - synchronous on VFP9 (non-standard subarch 1
313          *                implementation), undefined otherwise
314          *
315          * Clear various bits and enable access to the VFP so we can
316          * handle the bounce.
317          */
318         fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
319
320         fpsid = fmrx(FPSID);
321         orig_fpscr = fpscr = fmrx(FPSCR);
322
323         /*
324          * Check for the special VFP subarch 1 and FPSCR.IXE bit case
325          */
326         if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
327             && (fpscr & FPSCR_IXE)) {
328                 /*
329                  * Synchronous exception, emulate the trigger instruction
330                  */
331                 goto emulate;
332         }
333
334         if (fpexc & FPEXC_EX) {
335 #ifndef CONFIG_CPU_FEROCEON
336                 /*
337                  * Asynchronous exception. The instruction is read from FPINST
338                  * and the interrupted instruction has to be restarted.
339                  */
340                 trigger = fmrx(FPINST);
341                 regs->ARM_pc -= 4;
342 #endif
343         } else if (!(fpexc & FPEXC_DEX)) {
344                 /*
345                  * Illegal combination of bits. It can be caused by an
346                  * unallocated VFP instruction but with FPSCR.IXE set and not
347                  * on VFP subarch 1.
348                  */
349                  vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
350                 goto exit;
351         }
352
353         /*
354          * Modify fpscr to indicate the number of iterations remaining.
355          * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
356          * whether FPEXC.VECITR or FPSCR.LEN is used.
357          */
358         if (fpexc & (FPEXC_EX | FPEXC_VV)) {
359                 u32 len;
360
361                 len = fpexc + (1 << FPEXC_LENGTH_BIT);
362
363                 fpscr &= ~FPSCR_LENGTH_MASK;
364                 fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
365         }
366
367         /*
368          * Handle the first FP instruction.  We used to take note of the
369          * FPEXC bounce reason, but this appears to be unreliable.
370          * Emulate the bounced instruction instead.
371          */
372         exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
373         if (exceptions)
374                 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
375
376         /*
377          * If there isn't a second FP instruction, exit now. Note that
378          * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
379          */
380         if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
381                 goto exit;
382
383         /*
384          * The barrier() here prevents fpinst2 being read
385          * before the condition above.
386          */
387         barrier();
388         trigger = fmrx(FPINST2);
389
390  emulate:
391         exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
392         if (exceptions)
393                 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
394  exit:
395         preempt_enable();
396 }
397
398 static void vfp_enable(void *unused)
399 {
400         u32 access;
401
402         BUG_ON(preemptible());
403         access = get_copro_access();
404
405         /*
406          * Enable full access to VFP (cp10 and cp11)
407          */
408         set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
409 }
410
411 #ifdef CONFIG_CPU_PM
412 static int vfp_pm_suspend(void)
413 {
414         struct thread_info *ti = current_thread_info();
415         u32 fpexc = fmrx(FPEXC);
416
417         /* if vfp is on, then save state for resumption */
418         if (fpexc & FPEXC_EN) {
419                 printk(KERN_DEBUG "%s: saving vfp state\n", __func__);
420                 vfp_save_state(&ti->vfpstate, fpexc);
421
422                 /* disable, just in case */
423                 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
424         } else if (vfp_current_hw_state[ti->cpu]) {
425 #ifndef CONFIG_SMP
426                 fmxr(FPEXC, fpexc | FPEXC_EN);
427                 vfp_save_state(vfp_current_hw_state[ti->cpu], fpexc);
428                 fmxr(FPEXC, fpexc);
429 #endif
430         }
431
432         /* clear any information we had about last context state */
433         vfp_current_hw_state[ti->cpu] = NULL;
434
435         return 0;
436 }
437
438 static void vfp_pm_resume(void)
439 {
440         /* ensure we have access to the vfp */
441         vfp_enable(NULL);
442
443         /* and disable it to ensure the next usage restores the state */
444         fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
445 }
446
447 static int vfp_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
448         void *v)
449 {
450         switch (cmd) {
451         case CPU_PM_ENTER:
452                 vfp_pm_suspend();
453                 break;
454         case CPU_PM_ENTER_FAILED:
455         case CPU_PM_EXIT:
456                 vfp_pm_resume();
457                 break;
458         }
459         return NOTIFY_OK;
460 }
461
462 static struct notifier_block vfp_cpu_pm_notifier_block = {
463         .notifier_call = vfp_cpu_pm_notifier,
464 };
465
466 static void vfp_pm_init(void)
467 {
468         cpu_pm_register_notifier(&vfp_cpu_pm_notifier_block);
469 }
470
471 #else
472 static inline void vfp_pm_init(void) { }
473 #endif /* CONFIG_CPU_PM */
474
475 void vfp_sync_hwstate(struct thread_info *thread)
476 {
477         unsigned int cpu = get_cpu();
478
479         /*
480          * If the thread we're interested in is the current owner of the
481          * hardware VFP state, then we need to save its state.
482          */
483         if (vfp_current_hw_state[cpu] == &thread->vfpstate) {
484                 u32 fpexc = fmrx(FPEXC);
485
486                 /*
487                  * Save the last VFP state on this CPU.
488                  */
489                 fmxr(FPEXC, fpexc | FPEXC_EN);
490                 vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
491                 fmxr(FPEXC, fpexc);
492         }
493
494         put_cpu();
495 }
496
497 void vfp_flush_hwstate(struct thread_info *thread)
498 {
499         unsigned int cpu = get_cpu();
500
501         /*
502          * If the thread we're interested in is the current owner of the
503          * hardware VFP state, then we need to save its state.
504          */
505         if (vfp_current_hw_state[cpu] == &thread->vfpstate) {
506                 u32 fpexc = fmrx(FPEXC);
507
508                 fmxr(FPEXC, fpexc & ~FPEXC_EN);
509
510                 /*
511                  * Set the context to NULL to force a reload the next time
512                  * the thread uses the VFP.
513                  */
514                 vfp_current_hw_state[cpu] = NULL;
515         }
516
517 #ifdef CONFIG_SMP
518         /*
519          * For SMP we still have to take care of the case where the thread
520          * migrates to another CPU and then back to the original CPU on which
521          * the last VFP user is still the same thread. Mark the thread VFP
522          * state as belonging to a non-existent CPU so that the saved one will
523          * be reloaded in the above case.
524          */
525         thread->vfpstate.hard.cpu = NR_CPUS;
526 #endif
527         put_cpu();
528 }
529
530 /*
531  * VFP hardware can lose all context when a CPU goes offline.
532  * As we will be running in SMP mode with CPU hotplug, we will save the
533  * hardware state at every thread switch.  We clear our held state when
534  * a CPU has been killed, indicating that the VFP hardware doesn't contain
535  * a threads VFP state.  When a CPU starts up, we re-enable access to the
536  * VFP hardware.
537  *
538  * Both CPU_DYING and CPU_STARTING are called on the CPU which
539  * is being offlined/onlined.
540  */
541 static int vfp_hotplug(struct notifier_block *b, unsigned long action,
542         void *hcpu)
543 {
544         if (action == CPU_DYING || action == CPU_DYING_FROZEN) {
545                 unsigned int cpu = (long)hcpu;
546                 vfp_current_hw_state[cpu] = NULL;
547         } else if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
548                 vfp_enable(NULL);
549         return NOTIFY_OK;
550 }
551
552 /*
553  * VFP support code initialisation.
554  */
555 static int __init vfp_init(void)
556 {
557         unsigned int vfpsid;
558         unsigned int cpu_arch = cpu_architecture();
559
560         if (cpu_arch >= CPU_ARCH_ARMv6)
561                 on_each_cpu(vfp_enable, NULL, 1);
562
563         /*
564          * First check that there is a VFP that we can use.
565          * The handler is already setup to just log calls, so
566          * we just need to read the VFPSID register.
567          */
568         vfp_vector = vfp_testing_entry;
569         barrier();
570         vfpsid = fmrx(FPSID);
571         barrier();
572         vfp_vector = vfp_null_entry;
573
574         printk(KERN_INFO "VFP support v0.3: ");
575         if (VFP_arch)
576                 printk("not present\n");
577         else if (vfpsid & FPSID_NODOUBLE) {
578                 printk("no double precision support\n");
579         } else {
580                 hotcpu_notifier(vfp_hotplug, 0);
581
582                 VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT;  /* Extract the architecture version */
583                 printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
584                         (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
585                         (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
586                         (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
587                         (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
588                         (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
589
590                 vfp_vector = vfp_support_entry;
591
592                 thread_register_notifier(&vfp_notifier_block);
593                 vfp_pm_init();
594
595                 /*
596                  * We detected VFP, and the support code is
597                  * in place; report VFP support to userspace.
598                  */
599                 elf_hwcap |= HWCAP_VFP;
600 #ifdef CONFIG_VFPv3
601                 if (VFP_arch >= 2) {
602                         elf_hwcap |= HWCAP_VFPv3;
603
604                         /*
605                          * Check for VFPv3 D16 and VFPv4 D16.  CPUs in
606                          * this configuration only have 16 x 64bit
607                          * registers.
608                          */
609                         if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
610                                 elf_hwcap |= HWCAP_VFPv3D16; /* also v4-D16 */
611                         else
612                                 elf_hwcap |= HWCAP_VFPD32;
613                 }
614 #endif
615 #ifdef CONFIG_NEON
616                 /*
617                  * Check for the presence of the Advanced SIMD
618                  * load/store instructions, integer and single
619                  * precision floating point operations. Only check
620                  * for NEON if the hardware has the MVFR registers.
621                  */
622                 if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
623                         if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
624                                 elf_hwcap |= HWCAP_NEON;
625                 }
626 #endif
627         }
628         return 0;
629 }
630
631 late_initcall(vfp_init);