1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/arch/arm/vfp/vfpmodule.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2004 ARM Limited.
61da177e4SLinus Torvalds * Written by Deep Blue Solutions Limited.
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds #include <linux/types.h>
990b44199SRussell King #include <linux/cpu.h>
10746a9d19SColin Cross #include <linux/cpu_pm.h>
11998de4acSWill Deacon #include <linux/hardirq.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
1390b44199SRussell King #include <linux/notifier.h>
141da177e4SLinus Torvalds #include <linux/signal.h>
153f07c014SIngo Molnar #include <linux/sched/signal.h>
1690b44199SRussell King #include <linux/smp.h>
171da177e4SLinus Torvalds #include <linux/init.h>
182498814fSWill Deacon #include <linux/uaccess.h>
192498814fSWill Deacon #include <linux/user.h>
2073c132c1SArd Biesheuvel #include <linux/export.h>
2187691776SArd Biesheuvel #include <linux/perf_event.h>
22d6551e88SRussell King
2315d07dc9SRussell King #include <asm/cp15.h>
245aaf2544STony Lindgren #include <asm/cputype.h>
259f97da78SDavid Howells #include <asm/system_info.h>
26d6551e88SRussell King #include <asm/thread_notify.h>
27f77ac2e3SArd Biesheuvel #include <asm/traps.h>
281da177e4SLinus Torvalds #include <asm/vfp.h>
29*2332c615SArnd Bergmann #include <asm/neon.h>
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds #include "vfpinstr.h"
321da177e4SLinus Torvalds #include "vfp.h"
331da177e4SLinus Torvalds
34c76c6c4eSArd Biesheuvel static bool have_vfp __ro_after_init;
35af61bdf0SRussell King
36af61bdf0SRussell King /*
371da177e4SLinus Torvalds * Dual-use variable.
381da177e4SLinus Torvalds * Used in startup: set to non-zero if VFP checks fail
391da177e4SLinus Torvalds * After startup, holds VFP architecture
401da177e4SLinus Torvalds */
414a0548c6SArd Biesheuvel static unsigned int VFP_arch;
424a0548c6SArd Biesheuvel
434a0548c6SArd Biesheuvel #ifdef CONFIG_CPU_FEROCEON
444a0548c6SArd Biesheuvel extern unsigned int VFP_arch_feroceon __alias(VFP_arch);
454a0548c6SArd Biesheuvel #endif
461da177e4SLinus Torvalds
470d782dc4SRussell King /*
48f8f2a852SRussell King * The pointer to the vfpstate structure of the thread which currently
49f8f2a852SRussell King * owns the context held in the VFP hardware, or NULL if the hardware
50f8f2a852SRussell King * context is invalid.
51f8f2a852SRussell King *
52f8f2a852SRussell King * For UP, this is sufficient to tell which thread owns the VFP context.
53f8f2a852SRussell King * However, for SMP, we also need to check the CPU number stored in the
54f8f2a852SRussell King * saved state too to catch migrations.
55f8f2a852SRussell King */
56f8f2a852SRussell King union vfp_state *vfp_current_hw_state[NR_CPUS];
57f8f2a852SRussell King
58f8f2a852SRussell King /*
59f8f2a852SRussell King * Is 'thread's most up to date state stored in this CPUs hardware?
60f8f2a852SRussell King * Must be called from non-preemptible context.
61f8f2a852SRussell King */
vfp_state_in_hw(unsigned int cpu,struct thread_info * thread)62f8f2a852SRussell King static bool vfp_state_in_hw(unsigned int cpu, struct thread_info *thread)
63f8f2a852SRussell King {
64f8f2a852SRussell King #ifdef CONFIG_SMP
65f8f2a852SRussell King if (thread->vfpstate.hard.cpu != cpu)
66f8f2a852SRussell King return false;
67f8f2a852SRussell King #endif
68f8f2a852SRussell King return vfp_current_hw_state[cpu] == &thread->vfpstate;
69f8f2a852SRussell King }
70f8f2a852SRussell King
71f8f2a852SRussell King /*
72f8f2a852SRussell King * Force a reload of the VFP context from the thread structure. We do
73f8f2a852SRussell King * this by ensuring that access to the VFP hardware is disabled, and
7448af9feaSSantosh Shilimkar * clear vfp_current_hw_state. Must be called from non-preemptible context.
75f8f2a852SRussell King */
vfp_force_reload(unsigned int cpu,struct thread_info * thread)76f8f2a852SRussell King static void vfp_force_reload(unsigned int cpu, struct thread_info *thread)
77f8f2a852SRussell King {
78f8f2a852SRussell King if (vfp_state_in_hw(cpu, thread)) {
79f8f2a852SRussell King fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
80f8f2a852SRussell King vfp_current_hw_state[cpu] = NULL;
81f8f2a852SRussell King }
82f8f2a852SRussell King #ifdef CONFIG_SMP
83f8f2a852SRussell King thread->vfpstate.hard.cpu = NR_CPUS;
84f8f2a852SRussell King #endif
85f8f2a852SRussell King }
86f8f2a852SRussell King
87f8f2a852SRussell King /*
880d782dc4SRussell King * Per-thread VFP initialization.
890d782dc4SRussell King */
vfp_thread_flush(struct thread_info * thread)900d782dc4SRussell King static void vfp_thread_flush(struct thread_info *thread)
910d782dc4SRussell King {
920d782dc4SRussell King union vfp_state *vfp = &thread->vfpstate;
930d782dc4SRussell King unsigned int cpu;
940d782dc4SRussell King
9519dad35fSRussell King /*
9619dad35fSRussell King * Disable VFP to ensure we initialize it first. We must ensure
9719dad35fSRussell King * that the modification of vfp_current_hw_state[] and hardware
9819dad35fSRussell King * disable are done for the same CPU and without preemption.
9919dad35fSRussell King *
10019dad35fSRussell King * Do this first to ensure that preemption won't overwrite our
10119dad35fSRussell King * state saving should access to the VFP be enabled at this point.
10219dad35fSRussell King */
10319dad35fSRussell King cpu = get_cpu();
10419dad35fSRussell King if (vfp_current_hw_state[cpu] == vfp)
10519dad35fSRussell King vfp_current_hw_state[cpu] = NULL;
10619dad35fSRussell King fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
10719dad35fSRussell King put_cpu();
10819dad35fSRussell King
1090d782dc4SRussell King memset(vfp, 0, sizeof(union vfp_state));
1100d782dc4SRussell King
1110d782dc4SRussell King vfp->hard.fpexc = FPEXC_EN;
1120d782dc4SRussell King vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
113f8f2a852SRussell King #ifdef CONFIG_SMP
114f8f2a852SRussell King vfp->hard.cpu = NR_CPUS;
115f8f2a852SRussell King #endif
1160d782dc4SRussell King }
1170d782dc4SRussell King
vfp_thread_exit(struct thread_info * thread)118797245f5SRussell King static void vfp_thread_exit(struct thread_info *thread)
1190d782dc4SRussell King {
1200d782dc4SRussell King /* release case: Per-thread VFP cleanup. */
1210d782dc4SRussell King union vfp_state *vfp = &thread->vfpstate;
122797245f5SRussell King unsigned int cpu = get_cpu();
1230d782dc4SRussell King
124af61bdf0SRussell King if (vfp_current_hw_state[cpu] == vfp)
125af61bdf0SRussell King vfp_current_hw_state[cpu] = NULL;
126797245f5SRussell King put_cpu();
1270d782dc4SRussell King }
1280d782dc4SRussell King
vfp_thread_copy(struct thread_info * thread)129c98c0977SCatalin Marinas static void vfp_thread_copy(struct thread_info *thread)
130c98c0977SCatalin Marinas {
131c98c0977SCatalin Marinas struct thread_info *parent = current_thread_info();
132c98c0977SCatalin Marinas
133c98c0977SCatalin Marinas vfp_sync_hwstate(parent);
134c98c0977SCatalin Marinas thread->vfpstate = parent->vfpstate;
135f8f2a852SRussell King #ifdef CONFIG_SMP
136f8f2a852SRussell King thread->vfpstate.hard.cpu = NR_CPUS;
137f8f2a852SRussell King #endif
138c98c0977SCatalin Marinas }
139c98c0977SCatalin Marinas
1400d782dc4SRussell King /*
1410d782dc4SRussell King * When this function is called with the following 'cmd's, the following
1420d782dc4SRussell King * is true while this function is being run:
1430d782dc4SRussell King * THREAD_NOFTIFY_SWTICH:
1440d782dc4SRussell King * - the previously running thread will not be scheduled onto another CPU.
1450d782dc4SRussell King * - the next thread to be run (v) will not be running on another CPU.
1460d782dc4SRussell King * - thread->cpu is the local CPU number
1470d782dc4SRussell King * - not preemptible as we're called in the middle of a thread switch
1480d782dc4SRussell King * THREAD_NOTIFY_FLUSH:
1490d782dc4SRussell King * - the thread (v) will be running on the local CPU, so
1500d782dc4SRussell King * v === current_thread_info()
1510d782dc4SRussell King * - thread->cpu is the local CPU number at the time it is accessed,
1520d782dc4SRussell King * but may change at any time.
1530d782dc4SRussell King * - we could be preempted if tree preempt rcu is enabled, so
1540d782dc4SRussell King * it is unsafe to use thread->cpu.
155797245f5SRussell King * THREAD_NOTIFY_EXIT
156797245f5SRussell King * - we could be preempted if tree preempt rcu is enabled, so
157797245f5SRussell King * it is unsafe to use thread->cpu.
1580d782dc4SRussell King */
vfp_notifier(struct notifier_block * self,unsigned long cmd,void * v)159d6551e88SRussell King static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
160d6551e88SRussell King {
161d6551e88SRussell King struct thread_info *thread = v;
1622e82669aSCatalin Marinas u32 fpexc;
1632e82669aSCatalin Marinas #ifdef CONFIG_SMP
1642e82669aSCatalin Marinas unsigned int cpu;
1652e82669aSCatalin Marinas #endif
166d6551e88SRussell King
1672e82669aSCatalin Marinas switch (cmd) {
1682e82669aSCatalin Marinas case THREAD_NOTIFY_SWITCH:
1692e82669aSCatalin Marinas fpexc = fmrx(FPEXC);
170c6428464SCatalin Marinas
171c6428464SCatalin Marinas #ifdef CONFIG_SMP
1722e82669aSCatalin Marinas cpu = thread->cpu;
1730d782dc4SRussell King
174c6428464SCatalin Marinas /*
175c6428464SCatalin Marinas * On SMP, if VFP is enabled, save the old state in
176c6428464SCatalin Marinas * case the thread migrates to a different CPU. The
177c6428464SCatalin Marinas * restoring is done lazily.
178c6428464SCatalin Marinas */
179f8f2a852SRussell King if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu])
180af61bdf0SRussell King vfp_save_state(vfp_current_hw_state[cpu], fpexc);
181c6428464SCatalin Marinas #endif
182c6428464SCatalin Marinas
183681a4991SRussell King /*
184681a4991SRussell King * Always disable VFP so we can lazily save/restore the
185681a4991SRussell King * old state.
186681a4991SRussell King */
187228adef1SRussell King fmxr(FPEXC, fpexc & ~FPEXC_EN);
1882e82669aSCatalin Marinas break;
189681a4991SRussell King
1902e82669aSCatalin Marinas case THREAD_NOTIFY_FLUSH:
1910d782dc4SRussell King vfp_thread_flush(thread);
1922e82669aSCatalin Marinas break;
1932e82669aSCatalin Marinas
1942e82669aSCatalin Marinas case THREAD_NOTIFY_EXIT:
195797245f5SRussell King vfp_thread_exit(thread);
1962e82669aSCatalin Marinas break;
197c98c0977SCatalin Marinas
198c98c0977SCatalin Marinas case THREAD_NOTIFY_COPY:
199c98c0977SCatalin Marinas vfp_thread_copy(thread);
200c98c0977SCatalin Marinas break;
2012e82669aSCatalin Marinas }
2021da177e4SLinus Torvalds
203d6551e88SRussell King return NOTIFY_DONE;
204d6551e88SRussell King }
205d6551e88SRussell King
206d6551e88SRussell King static struct notifier_block vfp_notifier_block = {
207d6551e88SRussell King .notifier_call = vfp_notifier,
208d6551e88SRussell King };
209d6551e88SRussell King
2101da177e4SLinus Torvalds /*
2111da177e4SLinus Torvalds * Raise a SIGFPE for the current process.
2121da177e4SLinus Torvalds * sicode describes the signal being raised.
2131da177e4SLinus Torvalds */
vfp_raise_sigfpe(unsigned int sicode,struct pt_regs * regs)2142bbd7e9bSRussell King static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds * This is the same as NWFPE, because it's not clear what
2181da177e4SLinus Torvalds * this is used for
2191da177e4SLinus Torvalds */
2201da177e4SLinus Torvalds current->thread.error_code = 0;
2211da177e4SLinus Torvalds current->thread.trap_no = 6;
2221da177e4SLinus Torvalds
223b0594548SEric W. Biederman send_sig_fault(SIGFPE, sicode,
224b0594548SEric W. Biederman (void __user *)(instruction_pointer(regs) - 4),
225b0594548SEric W. Biederman current);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds
vfp_panic(char * reason,u32 inst)228c98929c0SCatalin Marinas static void vfp_panic(char *reason, u32 inst)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds int i;
2311da177e4SLinus Torvalds
232dc457078SNicolas Pitre pr_err("VFP: Error: %s\n", reason);
233dc457078SNicolas Pitre pr_err("VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
234c98929c0SCatalin Marinas fmrx(FPEXC), fmrx(FPSCR), inst);
2351da177e4SLinus Torvalds for (i = 0; i < 32; i += 2)
236dc457078SNicolas Pitre pr_err("VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
2371da177e4SLinus Torvalds i, vfp_get_float(i), i+1, vfp_get_float(i+1));
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds
2401da177e4SLinus Torvalds /*
2411da177e4SLinus Torvalds * Process bitmask of exception conditions.
2421da177e4SLinus Torvalds */
vfp_raise_exceptions(u32 exceptions,u32 inst,u32 fpscr,struct pt_regs * regs)2431da177e4SLinus Torvalds static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds int si_code = 0;
2461da177e4SLinus Torvalds
2471da177e4SLinus Torvalds pr_debug("VFP: raising exceptions %08x\n", exceptions);
2481da177e4SLinus Torvalds
2497c6f2514SDaniel Jacobowitz if (exceptions == VFP_EXCEPTION_ERROR) {
250c98929c0SCatalin Marinas vfp_panic("unhandled bounce", inst);
25192d44a42SRussell King vfp_raise_sigfpe(FPE_FLTINV, regs);
2521da177e4SLinus Torvalds return;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds
2551da177e4SLinus Torvalds /*
256dbead405SCatalin Marinas * If any of the status flags are set, update the FPSCR.
2571da177e4SLinus Torvalds * Comparison instructions always return at least one of
2581da177e4SLinus Torvalds * these flags set.
2591da177e4SLinus Torvalds */
260dbead405SCatalin Marinas if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
261dbead405SCatalin Marinas fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
262dbead405SCatalin Marinas
2631da177e4SLinus Torvalds fpscr |= exceptions;
2641da177e4SLinus Torvalds
2651da177e4SLinus Torvalds fmxr(FPSCR, fpscr);
2661da177e4SLinus Torvalds
2671da177e4SLinus Torvalds #define RAISE(stat,en,sig) \
2681da177e4SLinus Torvalds if (exceptions & stat && fpscr & en) \
2691da177e4SLinus Torvalds si_code = sig;
2701da177e4SLinus Torvalds
2711da177e4SLinus Torvalds /*
2721da177e4SLinus Torvalds * These are arranged in priority order, least to highest.
2731da177e4SLinus Torvalds */
274e0f205d9STakashi Ohmasa RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
2751da177e4SLinus Torvalds RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
2761da177e4SLinus Torvalds RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
2771da177e4SLinus Torvalds RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
2781da177e4SLinus Torvalds RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
2791da177e4SLinus Torvalds
2801da177e4SLinus Torvalds if (si_code)
2811da177e4SLinus Torvalds vfp_raise_sigfpe(si_code, regs);
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds * Emulate a VFP instruction.
2861da177e4SLinus Torvalds */
vfp_emulate_instruction(u32 inst,u32 fpscr,struct pt_regs * regs)2871da177e4SLinus Torvalds static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
2881da177e4SLinus Torvalds {
2897c6f2514SDaniel Jacobowitz u32 exceptions = VFP_EXCEPTION_ERROR;
2901da177e4SLinus Torvalds
2911da177e4SLinus Torvalds pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
2921da177e4SLinus Torvalds
2931da177e4SLinus Torvalds if (INST_CPRTDO(inst)) {
2941da177e4SLinus Torvalds if (!INST_CPRT(inst)) {
2951da177e4SLinus Torvalds /*
2961da177e4SLinus Torvalds * CPDO
2971da177e4SLinus Torvalds */
2981da177e4SLinus Torvalds if (vfp_single(inst)) {
2991da177e4SLinus Torvalds exceptions = vfp_single_cpdo(inst, fpscr);
3001da177e4SLinus Torvalds } else {
3011da177e4SLinus Torvalds exceptions = vfp_double_cpdo(inst, fpscr);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds } else {
3041da177e4SLinus Torvalds /*
3051da177e4SLinus Torvalds * A CPRT instruction can not appear in FPINST2, nor
3061da177e4SLinus Torvalds * can it cause an exception. Therefore, we do not
3071da177e4SLinus Torvalds * have to emulate it.
3081da177e4SLinus Torvalds */
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds } else {
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds * A CPDT instruction can not appear in FPINST2, nor can
3131da177e4SLinus Torvalds * it cause an exception. Therefore, we do not have to
3141da177e4SLinus Torvalds * emulate it.
3151da177e4SLinus Torvalds */
3161da177e4SLinus Torvalds }
31787691776SArd Biesheuvel perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
318928bd1b4SRussell King return exceptions & ~VFP_NAN_FLAG;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds
3211da177e4SLinus Torvalds /*
3221da177e4SLinus Torvalds * Package up a bounce condition.
3231da177e4SLinus Torvalds */
VFP_bounce(u32 trigger,u32 fpexc,struct pt_regs * regs)3244708fb04SArd Biesheuvel static void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
3251da177e4SLinus Torvalds {
326c98929c0SCatalin Marinas u32 fpscr, orig_fpscr, fpsid, exceptions;
3271da177e4SLinus Torvalds
3281da177e4SLinus Torvalds pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
3291da177e4SLinus Torvalds
3301da177e4SLinus Torvalds /*
331c98929c0SCatalin Marinas * At this point, FPEXC can have the following configuration:
332c98929c0SCatalin Marinas *
333c98929c0SCatalin Marinas * EX DEX IXE
334c98929c0SCatalin Marinas * 0 1 x - synchronous exception
335c98929c0SCatalin Marinas * 1 x 0 - asynchronous exception
336c98929c0SCatalin Marinas * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
337c98929c0SCatalin Marinas * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
338c98929c0SCatalin Marinas * implementation), undefined otherwise
339c98929c0SCatalin Marinas *
340c98929c0SCatalin Marinas * Clear various bits and enable access to the VFP so we can
341c98929c0SCatalin Marinas * handle the bounce.
3421da177e4SLinus Torvalds */
343c98929c0SCatalin Marinas fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
3441da177e4SLinus Torvalds
345c98929c0SCatalin Marinas fpsid = fmrx(FPSID);
3461da177e4SLinus Torvalds orig_fpscr = fpscr = fmrx(FPSCR);
3471da177e4SLinus Torvalds
3481da177e4SLinus Torvalds /*
349c98929c0SCatalin Marinas * Check for the special VFP subarch 1 and FPSCR.IXE bit case
3501da177e4SLinus Torvalds */
351c98929c0SCatalin Marinas if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
352c98929c0SCatalin Marinas && (fpscr & FPSCR_IXE)) {
353c98929c0SCatalin Marinas /*
354c98929c0SCatalin Marinas * Synchronous exception, emulate the trigger instruction
355c98929c0SCatalin Marinas */
3561da177e4SLinus Torvalds goto emulate;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds
359c98929c0SCatalin Marinas if (fpexc & FPEXC_EX) {
360c98929c0SCatalin Marinas /*
361c98929c0SCatalin Marinas * Asynchronous exception. The instruction is read from FPINST
362c98929c0SCatalin Marinas * and the interrupted instruction has to be restarted.
363c98929c0SCatalin Marinas */
364c98929c0SCatalin Marinas trigger = fmrx(FPINST);
365c98929c0SCatalin Marinas regs->ARM_pc -= 4;
366c98929c0SCatalin Marinas } else if (!(fpexc & FPEXC_DEX)) {
367c98929c0SCatalin Marinas /*
368c98929c0SCatalin Marinas * Illegal combination of bits. It can be caused by an
369c98929c0SCatalin Marinas * unallocated VFP instruction but with FPSCR.IXE set and not
370c98929c0SCatalin Marinas * on VFP subarch 1.
371c98929c0SCatalin Marinas */
372c98929c0SCatalin Marinas vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
3734708fb04SArd Biesheuvel return;
374c98929c0SCatalin Marinas }
3751da177e4SLinus Torvalds
3761da177e4SLinus Torvalds /*
377c98929c0SCatalin Marinas * Modify fpscr to indicate the number of iterations remaining.
378c98929c0SCatalin Marinas * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
379c98929c0SCatalin Marinas * whether FPEXC.VECITR or FPSCR.LEN is used.
3801da177e4SLinus Torvalds */
381c98929c0SCatalin Marinas if (fpexc & (FPEXC_EX | FPEXC_VV)) {
3821da177e4SLinus Torvalds u32 len;
3831da177e4SLinus Torvalds
3841da177e4SLinus Torvalds len = fpexc + (1 << FPEXC_LENGTH_BIT);
3851da177e4SLinus Torvalds
3861da177e4SLinus Torvalds fpscr &= ~FPSCR_LENGTH_MASK;
3871da177e4SLinus Torvalds fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds
3901da177e4SLinus Torvalds /*
3911da177e4SLinus Torvalds * Handle the first FP instruction. We used to take note of the
3921da177e4SLinus Torvalds * FPEXC bounce reason, but this appears to be unreliable.
3931da177e4SLinus Torvalds * Emulate the bounced instruction instead.
3941da177e4SLinus Torvalds */
395c98929c0SCatalin Marinas exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
3961da177e4SLinus Torvalds if (exceptions)
397c98929c0SCatalin Marinas vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
3981da177e4SLinus Torvalds
3991da177e4SLinus Torvalds /*
400c98929c0SCatalin Marinas * If there isn't a second FP instruction, exit now. Note that
401c98929c0SCatalin Marinas * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
4021da177e4SLinus Torvalds */
4035e4ba617SRussell King if ((fpexc & (FPEXC_EX | FPEXC_FP2V)) != (FPEXC_EX | FPEXC_FP2V))
4044708fb04SArd Biesheuvel return;
4051da177e4SLinus Torvalds
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds * The barrier() here prevents fpinst2 being read
4081da177e4SLinus Torvalds * before the condition above.
4091da177e4SLinus Torvalds */
4101da177e4SLinus Torvalds barrier();
4111da177e4SLinus Torvalds trigger = fmrx(FPINST2);
4121da177e4SLinus Torvalds
4131da177e4SLinus Torvalds emulate:
414c98929c0SCatalin Marinas exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
4151da177e4SLinus Torvalds if (exceptions)
4161da177e4SLinus Torvalds vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds
vfp_enable(void * unused)4198e140362SRussell King static void vfp_enable(void *unused)
4208e140362SRussell King {
421998de4acSWill Deacon u32 access;
422998de4acSWill Deacon
423998de4acSWill Deacon BUG_ON(preemptible());
424998de4acSWill Deacon access = get_copro_access();
4258e140362SRussell King
4268e140362SRussell King /*
4278e140362SRussell King * Enable full access to VFP (cp10 and cp11)
4288e140362SRussell King */
4298e140362SRussell King set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
4308e140362SRussell King }
4318e140362SRussell King
4327d7d7a41SFlorian Fainelli /* Called by platforms on which we want to disable VFP because it may not be
4337d7d7a41SFlorian Fainelli * present on all CPUs within a SMP complex. Needs to be called prior to
4347d7d7a41SFlorian Fainelli * vfp_init().
4357d7d7a41SFlorian Fainelli */
vfp_disable(void)4363cce9d44SArd Biesheuvel void __init vfp_disable(void)
4377d7d7a41SFlorian Fainelli {
4387d7d7a41SFlorian Fainelli if (VFP_arch) {
4397d7d7a41SFlorian Fainelli pr_debug("%s: should be called prior to vfp_init\n", __func__);
4407d7d7a41SFlorian Fainelli return;
4417d7d7a41SFlorian Fainelli }
4427d7d7a41SFlorian Fainelli VFP_arch = 1;
4437d7d7a41SFlorian Fainelli }
4447d7d7a41SFlorian Fainelli
445746a9d19SColin Cross #ifdef CONFIG_CPU_PM
vfp_pm_suspend(void)446328f5cc3SRafael J. Wysocki static int vfp_pm_suspend(void)
447fc0b7a20SBen Dooks {
448fc0b7a20SBen Dooks struct thread_info *ti = current_thread_info();
449fc0b7a20SBen Dooks u32 fpexc = fmrx(FPEXC);
450fc0b7a20SBen Dooks
451fc0b7a20SBen Dooks /* if vfp is on, then save state for resumption */
452fc0b7a20SBen Dooks if (fpexc & FPEXC_EN) {
453dc457078SNicolas Pitre pr_debug("%s: saving vfp state\n", __func__);
454fc0b7a20SBen Dooks vfp_save_state(&ti->vfpstate, fpexc);
455fc0b7a20SBen Dooks
456fc0b7a20SBen Dooks /* disable, just in case */
457fc0b7a20SBen Dooks fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
45824b35521SColin Cross } else if (vfp_current_hw_state[ti->cpu]) {
45924b35521SColin Cross #ifndef CONFIG_SMP
46024b35521SColin Cross fmxr(FPEXC, fpexc | FPEXC_EN);
46124b35521SColin Cross vfp_save_state(vfp_current_hw_state[ti->cpu], fpexc);
46224b35521SColin Cross fmxr(FPEXC, fpexc);
46324b35521SColin Cross #endif
464fc0b7a20SBen Dooks }
465fc0b7a20SBen Dooks
466fc0b7a20SBen Dooks /* clear any information we had about last context state */
467a84b895aSColin Cross vfp_current_hw_state[ti->cpu] = NULL;
468fc0b7a20SBen Dooks
469fc0b7a20SBen Dooks return 0;
470fc0b7a20SBen Dooks }
471fc0b7a20SBen Dooks
vfp_pm_resume(void)472328f5cc3SRafael J. Wysocki static void vfp_pm_resume(void)
473fc0b7a20SBen Dooks {
474fc0b7a20SBen Dooks /* ensure we have access to the vfp */
475fc0b7a20SBen Dooks vfp_enable(NULL);
476fc0b7a20SBen Dooks
477fc0b7a20SBen Dooks /* and disable it to ensure the next usage restores the state */
478fc0b7a20SBen Dooks fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
479fc0b7a20SBen Dooks }
480fc0b7a20SBen Dooks
vfp_cpu_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)481746a9d19SColin Cross static int vfp_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
482746a9d19SColin Cross void *v)
483746a9d19SColin Cross {
484746a9d19SColin Cross switch (cmd) {
485746a9d19SColin Cross case CPU_PM_ENTER:
486746a9d19SColin Cross vfp_pm_suspend();
487746a9d19SColin Cross break;
488746a9d19SColin Cross case CPU_PM_ENTER_FAILED:
489746a9d19SColin Cross case CPU_PM_EXIT:
490746a9d19SColin Cross vfp_pm_resume();
491746a9d19SColin Cross break;
492746a9d19SColin Cross }
493746a9d19SColin Cross return NOTIFY_OK;
494746a9d19SColin Cross }
495746a9d19SColin Cross
496746a9d19SColin Cross static struct notifier_block vfp_cpu_pm_notifier_block = {
497746a9d19SColin Cross .notifier_call = vfp_cpu_pm_notifier,
498fc0b7a20SBen Dooks };
499fc0b7a20SBen Dooks
vfp_pm_init(void)500fc0b7a20SBen Dooks static void vfp_pm_init(void)
501fc0b7a20SBen Dooks {
502746a9d19SColin Cross cpu_pm_register_notifier(&vfp_cpu_pm_notifier_block);
503fc0b7a20SBen Dooks }
504fc0b7a20SBen Dooks
505fc0b7a20SBen Dooks #else
vfp_pm_init(void)506fc0b7a20SBen Dooks static inline void vfp_pm_init(void) { }
507746a9d19SColin Cross #endif /* CONFIG_CPU_PM */
508fc0b7a20SBen Dooks
509f8f2a852SRussell King /*
510f8f2a852SRussell King * Ensure that the VFP state stored in 'thread->vfpstate' is up to date
511f8f2a852SRussell King * with the hardware state.
512f8f2a852SRussell King */
vfp_sync_hwstate(struct thread_info * thread)513ad187f95SRussell King void vfp_sync_hwstate(struct thread_info *thread)
5143d1228eaSCatalin Marinas {
5153d1228eaSCatalin Marinas unsigned int cpu = get_cpu();
5163d1228eaSCatalin Marinas
51762b95a7bSArd Biesheuvel local_bh_disable();
51862b95a7bSArd Biesheuvel
519f8f2a852SRussell King if (vfp_state_in_hw(cpu, thread)) {
52054cb3dbbSRussell King u32 fpexc = fmrx(FPEXC);
5213d1228eaSCatalin Marinas
5223d1228eaSCatalin Marinas /*
5233d1228eaSCatalin Marinas * Save the last VFP state on this CPU.
5243d1228eaSCatalin Marinas */
5253d1228eaSCatalin Marinas fmxr(FPEXC, fpexc | FPEXC_EN);
52654cb3dbbSRussell King vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
5273d1228eaSCatalin Marinas fmxr(FPEXC, fpexc);
528ad187f95SRussell King }
529ad187f95SRussell King
53062b95a7bSArd Biesheuvel local_bh_enable();
531ad187f95SRussell King put_cpu();
532ad187f95SRussell King }
533ad187f95SRussell King
534f8f2a852SRussell King /* Ensure that the thread reloads the hardware VFP state on the next use. */
vfp_flush_hwstate(struct thread_info * thread)535ad187f95SRussell King void vfp_flush_hwstate(struct thread_info *thread)
536ad187f95SRussell King {
537ad187f95SRussell King unsigned int cpu = get_cpu();
5383d1228eaSCatalin Marinas
539f8f2a852SRussell King vfp_force_reload(cpu, thread);
540ad187f95SRussell King
5413d1228eaSCatalin Marinas put_cpu();
5423d1228eaSCatalin Marinas }
5433d1228eaSCatalin Marinas
54490b44199SRussell King /*
5452498814fSWill Deacon * Save the current VFP state into the provided structures and prepare
5462498814fSWill Deacon * for entry into a new function (signal handler).
5472498814fSWill Deacon */
vfp_preserve_user_clear_hwstate(struct user_vfp * ufp,struct user_vfp_exc * ufp_exc)5483aa2df6eSJulien Thierry int vfp_preserve_user_clear_hwstate(struct user_vfp *ufp,
5493aa2df6eSJulien Thierry struct user_vfp_exc *ufp_exc)
5502498814fSWill Deacon {
5512498814fSWill Deacon struct thread_info *thread = current_thread_info();
5522498814fSWill Deacon struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
5532498814fSWill Deacon
5542498814fSWill Deacon /* Ensure that the saved hwstate is up-to-date. */
5552498814fSWill Deacon vfp_sync_hwstate(thread);
5562498814fSWill Deacon
5572498814fSWill Deacon /*
5582498814fSWill Deacon * Copy the floating point registers. There can be unused
5592498814fSWill Deacon * registers see asm/hwcap.h for details.
5602498814fSWill Deacon */
5613aa2df6eSJulien Thierry memcpy(&ufp->fpregs, &hwstate->fpregs, sizeof(hwstate->fpregs));
5623aa2df6eSJulien Thierry
5632498814fSWill Deacon /*
5642498814fSWill Deacon * Copy the status and control register.
5652498814fSWill Deacon */
5663aa2df6eSJulien Thierry ufp->fpscr = hwstate->fpscr;
5672498814fSWill Deacon
5682498814fSWill Deacon /*
5692498814fSWill Deacon * Copy the exception registers.
5702498814fSWill Deacon */
5713aa2df6eSJulien Thierry ufp_exc->fpexc = hwstate->fpexc;
5723aa2df6eSJulien Thierry ufp_exc->fpinst = hwstate->fpinst;
5735df7a99bSJulien Thierry ufp_exc->fpinst2 = hwstate->fpinst2;
574ff9a184cSWill Deacon
575ff9a184cSWill Deacon /* Ensure that VFP is disabled. */
576ff9a184cSWill Deacon vfp_flush_hwstate(thread);
577ff9a184cSWill Deacon
578ff9a184cSWill Deacon /*
579ff9a184cSWill Deacon * As per the PCS, clear the length and stride bits for function
580ff9a184cSWill Deacon * entry.
581ff9a184cSWill Deacon */
582ff9a184cSWill Deacon hwstate->fpscr &= ~(FPSCR_LENGTH_MASK | FPSCR_STRIDE_MASK);
5832498814fSWill Deacon return 0;
5842498814fSWill Deacon }
5852498814fSWill Deacon
5862498814fSWill Deacon /* Sanitise and restore the current VFP state from the provided structures. */
vfp_restore_user_hwstate(struct user_vfp * ufp,struct user_vfp_exc * ufp_exc)58742019fc5SRussell King int vfp_restore_user_hwstate(struct user_vfp *ufp, struct user_vfp_exc *ufp_exc)
5882498814fSWill Deacon {
5892498814fSWill Deacon struct thread_info *thread = current_thread_info();
5902498814fSWill Deacon struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
5912498814fSWill Deacon unsigned long fpexc;
5922498814fSWill Deacon
59356cb2484SWill Deacon /* Disable VFP to avoid corrupting the new thread state. */
5942498814fSWill Deacon vfp_flush_hwstate(thread);
5952498814fSWill Deacon
5962498814fSWill Deacon /*
5972498814fSWill Deacon * Copy the floating point registers. There can be unused
5982498814fSWill Deacon * registers see asm/hwcap.h for details.
5992498814fSWill Deacon */
60042019fc5SRussell King memcpy(&hwstate->fpregs, &ufp->fpregs, sizeof(hwstate->fpregs));
6012498814fSWill Deacon /*
6022498814fSWill Deacon * Copy the status and control register.
6032498814fSWill Deacon */
60442019fc5SRussell King hwstate->fpscr = ufp->fpscr;
6052498814fSWill Deacon
6062498814fSWill Deacon /*
6072498814fSWill Deacon * Sanitise and restore the exception registers.
6082498814fSWill Deacon */
60942019fc5SRussell King fpexc = ufp_exc->fpexc;
6102498814fSWill Deacon
6112498814fSWill Deacon /* Ensure the VFP is enabled. */
6122498814fSWill Deacon fpexc |= FPEXC_EN;
6132498814fSWill Deacon
6142498814fSWill Deacon /* Ensure FPINST2 is invalid and the exception flag is cleared. */
6152498814fSWill Deacon fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
6162498814fSWill Deacon hwstate->fpexc = fpexc;
6172498814fSWill Deacon
61842019fc5SRussell King hwstate->fpinst = ufp_exc->fpinst;
61942019fc5SRussell King hwstate->fpinst2 = ufp_exc->fpinst2;
6202498814fSWill Deacon
62142019fc5SRussell King return 0;
6222498814fSWill Deacon }
6232498814fSWill Deacon
6242498814fSWill Deacon /*
62590b44199SRussell King * VFP hardware can lose all context when a CPU goes offline.
62674c25beeSRussell King * As we will be running in SMP mode with CPU hotplug, we will save the
62774c25beeSRussell King * hardware state at every thread switch. We clear our held state when
62874c25beeSRussell King * a CPU has been killed, indicating that the VFP hardware doesn't contain
62974c25beeSRussell King * a threads VFP state. When a CPU starts up, we re-enable access to the
630e5b61bafSThomas Gleixner * VFP hardware. The callbacks below are called on the CPU which
63190b44199SRussell King * is being offlined/onlined.
63290b44199SRussell King */
vfp_dying_cpu(unsigned int cpu)633e5b61bafSThomas Gleixner static int vfp_dying_cpu(unsigned int cpu)
63490b44199SRussell King {
6351328f020SFabio Estevam vfp_current_hw_state[cpu] = NULL;
636e5b61bafSThomas Gleixner return 0;
637e5b61bafSThomas Gleixner }
638e5b61bafSThomas Gleixner
vfp_starting_cpu(unsigned int unused)639e5b61bafSThomas Gleixner static int vfp_starting_cpu(unsigned int unused)
640e5b61bafSThomas Gleixner {
64190b44199SRussell King vfp_enable(NULL);
642e5b61bafSThomas Gleixner return 0;
64390b44199SRussell King }
6448e140362SRussell King
vfp_kmode_exception(struct pt_regs * regs,unsigned int instr)645f77ac2e3SArd Biesheuvel static int vfp_kmode_exception(struct pt_regs *regs, unsigned int instr)
646ab3da156SArd Biesheuvel {
647ab3da156SArd Biesheuvel /*
648ab3da156SArd Biesheuvel * If we reach this point, a floating point exception has been raised
649ab3da156SArd Biesheuvel * while running in kernel mode. If the NEON/VFP unit was enabled at the
650ab3da156SArd Biesheuvel * time, it means a VFP instruction has been issued that requires
651ab3da156SArd Biesheuvel * software assistance to complete, something which is not currently
652ab3da156SArd Biesheuvel * supported in kernel mode.
653ab3da156SArd Biesheuvel * If the NEON/VFP unit was disabled, and the location pointed to below
654ab3da156SArd Biesheuvel * is properly preceded by a call to kernel_neon_begin(), something has
655ab3da156SArd Biesheuvel * caused the task to be scheduled out and back in again. In this case,
656ab3da156SArd Biesheuvel * rebuilding and running with CONFIG_DEBUG_ATOMIC_SLEEP enabled should
657ab3da156SArd Biesheuvel * be helpful in localizing the problem.
658ab3da156SArd Biesheuvel */
659ab3da156SArd Biesheuvel if (fmrx(FPEXC) & FPEXC_EN)
660ab3da156SArd Biesheuvel pr_crit("BUG: unsupported FP instruction in kernel mode\n");
661ab3da156SArd Biesheuvel else
662ab3da156SArd Biesheuvel pr_crit("BUG: FP instruction issued in kernel mode with FP unit disabled\n");
663f77ac2e3SArd Biesheuvel pr_crit("FPEXC == 0x%08x\n", fmrx(FPEXC));
664f77ac2e3SArd Biesheuvel return 1;
665ab3da156SArd Biesheuvel }
666ab3da156SArd Biesheuvel
667cdd87465SArd Biesheuvel /*
668cdd87465SArd Biesheuvel * vfp_support_entry - Handle VFP exception
6697279dc3eSCatalin Marinas *
6704708fb04SArd Biesheuvel * @regs: pt_regs structure holding the register state at exception entry
6714708fb04SArd Biesheuvel * @trigger: The opcode of the instruction that triggered the exception
6724708fb04SArd Biesheuvel *
6734708fb04SArd Biesheuvel * Returns 0 if the exception was handled, or an error code otherwise.
674b9338a78STzachi Perelstein */
vfp_support_entry(struct pt_regs * regs,u32 trigger)675cdd87465SArd Biesheuvel static int vfp_support_entry(struct pt_regs *regs, u32 trigger)
6765d4cae5fSRussell King {
6774708fb04SArd Biesheuvel struct thread_info *ti = current_thread_info();
6784708fb04SArd Biesheuvel u32 fpexc;
6794708fb04SArd Biesheuvel
6801da177e4SLinus Torvalds if (unlikely(!have_vfp))
6814708fb04SArd Biesheuvel return -ENODEV;
6821da177e4SLinus Torvalds
683cdd87465SArd Biesheuvel if (!user_mode(regs))
684cdd87465SArd Biesheuvel return vfp_kmode_exception(regs, trigger);
685cdd87465SArd Biesheuvel
6861da177e4SLinus Torvalds local_bh_disable();
6874708fb04SArd Biesheuvel fpexc = fmrx(FPEXC);
6884708fb04SArd Biesheuvel
6894708fb04SArd Biesheuvel /*
6904708fb04SArd Biesheuvel * If the VFP unit was not enabled yet, we have to check whether the
6914708fb04SArd Biesheuvel * VFP state in the CPU's registers is the most recent VFP state
6924708fb04SArd Biesheuvel * associated with the process. On UP systems, we don't save the VFP
6934708fb04SArd Biesheuvel * state eagerly on a context switch, so we may need to save the
6944708fb04SArd Biesheuvel * VFP state to memory first, as it may belong to another process.
6954708fb04SArd Biesheuvel */
6964708fb04SArd Biesheuvel if (!(fpexc & FPEXC_EN)) {
6974708fb04SArd Biesheuvel /*
6984708fb04SArd Biesheuvel * Enable the VFP unit but mask the FP exception flag for the
6994708fb04SArd Biesheuvel * time being, so we can access all the registers.
7004708fb04SArd Biesheuvel */
7014708fb04SArd Biesheuvel fpexc |= FPEXC_EN;
7024708fb04SArd Biesheuvel fmxr(FPEXC, fpexc & ~FPEXC_EX);
7034708fb04SArd Biesheuvel
7044708fb04SArd Biesheuvel /*
7054708fb04SArd Biesheuvel * Check whether or not the VFP state in the CPU's registers is
7064708fb04SArd Biesheuvel * the most recent VFP state associated with this task. On SMP,
7074708fb04SArd Biesheuvel * migration may result in multiple CPUs holding VFP states
7084708fb04SArd Biesheuvel * that belong to the same task, but only the most recent one
7094708fb04SArd Biesheuvel * is valid.
7104708fb04SArd Biesheuvel */
7114708fb04SArd Biesheuvel if (!vfp_state_in_hw(ti->cpu, ti)) {
7124708fb04SArd Biesheuvel if (!IS_ENABLED(CONFIG_SMP) &&
7134708fb04SArd Biesheuvel vfp_current_hw_state[ti->cpu] != NULL) {
7144708fb04SArd Biesheuvel /*
7154708fb04SArd Biesheuvel * This CPU is currently holding the most
7164708fb04SArd Biesheuvel * recent VFP state associated with another
7174708fb04SArd Biesheuvel * task, and we must save that to memory first.
7184708fb04SArd Biesheuvel */
7194708fb04SArd Biesheuvel vfp_save_state(vfp_current_hw_state[ti->cpu],
7204708fb04SArd Biesheuvel fpexc);
7214708fb04SArd Biesheuvel }
7224708fb04SArd Biesheuvel
7234708fb04SArd Biesheuvel /*
7244708fb04SArd Biesheuvel * We can now proceed with loading the task's VFP state
7254708fb04SArd Biesheuvel * from memory into the CPU registers.
7264708fb04SArd Biesheuvel */
7274708fb04SArd Biesheuvel fpexc = vfp_load_state(&ti->vfpstate);
7284708fb04SArd Biesheuvel vfp_current_hw_state[ti->cpu] = &ti->vfpstate;
7294708fb04SArd Biesheuvel #ifdef CONFIG_SMP
7304708fb04SArd Biesheuvel /*
7314708fb04SArd Biesheuvel * Record that this CPU is now the one holding the most
7324708fb04SArd Biesheuvel * recent VFP state of the task.
7334708fb04SArd Biesheuvel */
7344708fb04SArd Biesheuvel ti->vfpstate.hard.cpu = ti->cpu;
7354708fb04SArd Biesheuvel #endif
7364708fb04SArd Biesheuvel }
7374708fb04SArd Biesheuvel
7384708fb04SArd Biesheuvel if (fpexc & FPEXC_EX)
7394708fb04SArd Biesheuvel /*
7404708fb04SArd Biesheuvel * Might as well handle the pending exception before
7414708fb04SArd Biesheuvel * retrying branch out before setting an FPEXC that
7424708fb04SArd Biesheuvel * stops us reading stuff.
7434708fb04SArd Biesheuvel */
7444708fb04SArd Biesheuvel goto bounce;
7454708fb04SArd Biesheuvel
7464708fb04SArd Biesheuvel /*
7474708fb04SArd Biesheuvel * No FP exception is pending: just enable the VFP and
7484708fb04SArd Biesheuvel * replay the instruction that trapped.
7494708fb04SArd Biesheuvel */
7504708fb04SArd Biesheuvel fmxr(FPEXC, fpexc);
7514708fb04SArd Biesheuvel } else {
7524708fb04SArd Biesheuvel /* Check for synchronous or asynchronous exceptions */
7534708fb04SArd Biesheuvel if (!(fpexc & (FPEXC_EX | FPEXC_DEX))) {
7544708fb04SArd Biesheuvel u32 fpscr = fmrx(FPSCR);
7554708fb04SArd Biesheuvel
7564708fb04SArd Biesheuvel /*
7574708fb04SArd Biesheuvel * On some implementations of the VFP subarch 1,
7584708fb04SArd Biesheuvel * setting FPSCR.IXE causes all the CDP instructions to
7594708fb04SArd Biesheuvel * be bounced synchronously without setting the
7604708fb04SArd Biesheuvel * FPEXC.EX bit
7614708fb04SArd Biesheuvel */
7624708fb04SArd Biesheuvel if (!(fpscr & FPSCR_IXE)) {
7634708fb04SArd Biesheuvel if (!(fpscr & FPSCR_LENGTH_MASK)) {
7644708fb04SArd Biesheuvel pr_debug("not VFP\n");
7654708fb04SArd Biesheuvel local_bh_enable();
7664708fb04SArd Biesheuvel return -ENOEXEC;
7674708fb04SArd Biesheuvel }
7684708fb04SArd Biesheuvel fpexc |= FPEXC_DEX;
7694708fb04SArd Biesheuvel }
7704708fb04SArd Biesheuvel }
771cdd87465SArd Biesheuvel bounce: regs->ARM_pc += 4;
772cdd87465SArd Biesheuvel VFP_bounce(trigger, fpexc, regs);
7734708fb04SArd Biesheuvel }
7744708fb04SArd Biesheuvel
7754708fb04SArd Biesheuvel local_bh_enable();
7764708fb04SArd Biesheuvel return 0;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds
779cdd87465SArd Biesheuvel static struct undef_hook neon_support_hook[] = {{
780f77ac2e3SArd Biesheuvel .instr_mask = 0xfe000000,
781f77ac2e3SArd Biesheuvel .instr_val = 0xf2000000,
782cdd87465SArd Biesheuvel .cpsr_mask = PSR_T_BIT,
783cdd87465SArd Biesheuvel .cpsr_val = 0,
784cdd87465SArd Biesheuvel .fn = vfp_support_entry,
785f77ac2e3SArd Biesheuvel }, {
786f77ac2e3SArd Biesheuvel .instr_mask = 0xff100000,
787f77ac2e3SArd Biesheuvel .instr_val = 0xf4000000,
788cdd87465SArd Biesheuvel .cpsr_mask = PSR_T_BIT,
789cdd87465SArd Biesheuvel .cpsr_val = 0,
790cdd87465SArd Biesheuvel .fn = vfp_support_entry,
791f77ac2e3SArd Biesheuvel }, {
792f77ac2e3SArd Biesheuvel .instr_mask = 0xef000000,
793f77ac2e3SArd Biesheuvel .instr_val = 0xef000000,
794cdd87465SArd Biesheuvel .cpsr_mask = PSR_T_BIT,
795cdd87465SArd Biesheuvel .cpsr_val = PSR_T_BIT,
796cdd87465SArd Biesheuvel .fn = vfp_support_entry,
797f77ac2e3SArd Biesheuvel }, {
798f77ac2e3SArd Biesheuvel .instr_mask = 0xff100000,
799f77ac2e3SArd Biesheuvel .instr_val = 0xf9000000,
800cdd87465SArd Biesheuvel .cpsr_mask = PSR_T_BIT,
801cdd87465SArd Biesheuvel .cpsr_val = PSR_T_BIT,
802cdd87465SArd Biesheuvel .fn = vfp_support_entry,
803f77ac2e3SArd Biesheuvel }};
804f77ac2e3SArd Biesheuvel
805cdd87465SArd Biesheuvel static struct undef_hook vfp_support_hook = {
806cdd87465SArd Biesheuvel .instr_mask = 0x0c000e00,
807cdd87465SArd Biesheuvel .instr_val = 0x0c000a00,
808cdd87465SArd Biesheuvel .fn = vfp_support_entry,
809cdd87465SArd Biesheuvel };
810f77ac2e3SArd Biesheuvel
811cdd87465SArd Biesheuvel #ifdef CONFIG_KERNEL_MODE_NEON
81273c132c1SArd Biesheuvel
81373c132c1SArd Biesheuvel /*
81473c132c1SArd Biesheuvel * Kernel-side NEON support functions
81573c132c1SArd Biesheuvel */
kernel_neon_begin(void)81673c132c1SArd Biesheuvel void kernel_neon_begin(void)
81773c132c1SArd Biesheuvel {
81873c132c1SArd Biesheuvel struct thread_info *thread = current_thread_info();
81973c132c1SArd Biesheuvel unsigned int cpu;
82073c132c1SArd Biesheuvel u32 fpexc;
82173c132c1SArd Biesheuvel
82262b95a7bSArd Biesheuvel local_bh_disable();
82362b95a7bSArd Biesheuvel
82473c132c1SArd Biesheuvel /*
825c79f8163SArd Biesheuvel * Kernel mode NEON is only allowed outside of hardirq context with
826c79f8163SArd Biesheuvel * preemption and softirq processing disabled. This will make sure that
827c79f8163SArd Biesheuvel * the kernel mode NEON register contents never need to be preserved.
82873c132c1SArd Biesheuvel */
829c79f8163SArd Biesheuvel BUG_ON(in_hardirq());
830c79f8163SArd Biesheuvel cpu = __smp_processor_id();
83173c132c1SArd Biesheuvel
83273c132c1SArd Biesheuvel fpexc = fmrx(FPEXC) | FPEXC_EN;
83373c132c1SArd Biesheuvel fmxr(FPEXC, fpexc);
83473c132c1SArd Biesheuvel
83573c132c1SArd Biesheuvel /*
83673c132c1SArd Biesheuvel * Save the userland NEON/VFP state. Under UP,
83773c132c1SArd Biesheuvel * the owner could be a task other than 'current'
83873c132c1SArd Biesheuvel */
83973c132c1SArd Biesheuvel if (vfp_state_in_hw(cpu, thread))
84073c132c1SArd Biesheuvel vfp_save_state(&thread->vfpstate, fpexc);
84173c132c1SArd Biesheuvel #ifndef CONFIG_SMP
84273c132c1SArd Biesheuvel else if (vfp_current_hw_state[cpu] != NULL)
84373c132c1SArd Biesheuvel vfp_save_state(vfp_current_hw_state[cpu], fpexc);
84473c132c1SArd Biesheuvel #endif
84573c132c1SArd Biesheuvel vfp_current_hw_state[cpu] = NULL;
84673c132c1SArd Biesheuvel }
84773c132c1SArd Biesheuvel EXPORT_SYMBOL(kernel_neon_begin);
84873c132c1SArd Biesheuvel
kernel_neon_end(void)84973c132c1SArd Biesheuvel void kernel_neon_end(void)
85073c132c1SArd Biesheuvel {
85173c132c1SArd Biesheuvel /* Disable the NEON/VFP unit. */
85273c132c1SArd Biesheuvel fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
853c79f8163SArd Biesheuvel local_bh_enable();
85473c132c1SArd Biesheuvel }
85573c132c1SArd Biesheuvel EXPORT_SYMBOL(kernel_neon_end);
85673c132c1SArd Biesheuvel
85773c132c1SArd Biesheuvel #endif /* CONFIG_KERNEL_MODE_NEON */
85873c132c1SArd Biesheuvel
vfp_detect(struct pt_regs * regs,unsigned int instr)8593cce9d44SArd Biesheuvel static int __init vfp_detect(struct pt_regs *regs, unsigned int instr)
8603cce9d44SArd Biesheuvel {
8613cce9d44SArd Biesheuvel VFP_arch = UINT_MAX; /* mark as not present */
8623cce9d44SArd Biesheuvel regs->ARM_pc += 4;
8633cce9d44SArd Biesheuvel return 0;
8643cce9d44SArd Biesheuvel }
8653cce9d44SArd Biesheuvel
8663cce9d44SArd Biesheuvel static struct undef_hook vfp_detect_hook __initdata = {
8673cce9d44SArd Biesheuvel .instr_mask = 0x0c000e00,
8683cce9d44SArd Biesheuvel .instr_val = 0x0c000a00,
8693cce9d44SArd Biesheuvel .cpsr_mask = MODE_MASK,
8703cce9d44SArd Biesheuvel .cpsr_val = SVC_MODE,
8713cce9d44SArd Biesheuvel .fn = vfp_detect,
8723cce9d44SArd Biesheuvel };
8733cce9d44SArd Biesheuvel
8741da177e4SLinus Torvalds /*
8751da177e4SLinus Torvalds * VFP support code initialisation.
876efe90d27SRussell King */
vfp_init(void)877c98929c0SCatalin Marinas static int __init vfp_init(void)
8781da177e4SLinus Torvalds {
879c98929c0SCatalin Marinas unsigned int vfpsid;
8801da177e4SLinus Torvalds unsigned int cpu_arch = cpu_architecture();
88162ea0d87SAmit Daniel Kachhap unsigned int isar6;
8821da177e4SLinus Torvalds
883e5b61bafSThomas Gleixner /*
884e5b61bafSThomas Gleixner * Enable the access to the VFP on all online CPUs so the
885e5b61bafSThomas Gleixner * following test on FPSID will succeed.
886e5b61bafSThomas Gleixner */
8878691e5a8SJens Axboe if (cpu_arch >= CPU_ARCH_ARMv6)
888998de4acSWill Deacon on_each_cpu(vfp_enable, NULL, 1);
8891da177e4SLinus Torvalds
8901da177e4SLinus Torvalds /*
8911da177e4SLinus Torvalds * First check that there is a VFP that we can use.
8921da177e4SLinus Torvalds * The handler is already setup to just log calls, so
8931da177e4SLinus Torvalds * we just need to read the VFPSID register.
8941da177e4SLinus Torvalds */
8953cce9d44SArd Biesheuvel register_undef_hook(&vfp_detect_hook);
896efe90d27SRussell King barrier();
8971da177e4SLinus Torvalds vfpsid = fmrx(FPSID);
898d6551e88SRussell King barrier();
8993cce9d44SArd Biesheuvel unregister_undef_hook(&vfp_detect_hook);
900fc0b7a20SBen Dooks
901dc457078SNicolas Pitre pr_info("VFP support v0.3: ");
9026c96a4a6SStephen Boyd if (VFP_arch) {
903dc457078SNicolas Pitre pr_cont("not present\n");
9046c96a4a6SStephen Boyd return 0;
9056c96a4a6SStephen Boyd /* Extract the architecture on CPUID scheme */
9066c96a4a6SStephen Boyd } else if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
9076c96a4a6SStephen Boyd VFP_arch = vfpsid & FPSID_CPUID_ARCH_MASK;
9086c96a4a6SStephen Boyd VFP_arch >>= FPSID_ARCH_BIT;
9096c96a4a6SStephen Boyd /*
9106c96a4a6SStephen Boyd * Check for the presence of the Advanced SIMD
9116c96a4a6SStephen Boyd * load/store instructions, integer and single
9126c96a4a6SStephen Boyd * precision floating point operations. Only check
9136c96a4a6SStephen Boyd * for NEON if the hardware has the MVFR registers.
9146c96a4a6SStephen Boyd */
9152b94fe2aSStephen Boyd if (IS_ENABLED(CONFIG_NEON) &&
916cdd87465SArd Biesheuvel (fmrx(MVFR1) & 0x000fff00) == 0x00011100) {
9176c96a4a6SStephen Boyd elf_hwcap |= HWCAP_NEON;
918cdd87465SArd Biesheuvel for (int i = 0; i < ARRAY_SIZE(neon_support_hook); i++)
919cdd87465SArd Biesheuvel register_undef_hook(&neon_support_hook[i]);
920cdd87465SArd Biesheuvel }
9212b94fe2aSStephen Boyd
9222b94fe2aSStephen Boyd if (IS_ENABLED(CONFIG_VFPv3)) {
9232b94fe2aSStephen Boyd u32 mvfr0 = fmrx(MVFR0);
9246c96a4a6SStephen Boyd if (((mvfr0 & MVFR0_DP_MASK) >> MVFR0_DP_BIT) == 0x2 ||
9256c96a4a6SStephen Boyd ((mvfr0 & MVFR0_SP_MASK) >> MVFR0_SP_BIT) == 0x2) {
9266c96a4a6SStephen Boyd elf_hwcap |= HWCAP_VFPv3;
9276c96a4a6SStephen Boyd /*
9286c96a4a6SStephen Boyd * Check for VFPv3 D16 and VFPv4 D16. CPUs in
9296c96a4a6SStephen Boyd * this configuration only have 16 x 64bit
9306c96a4a6SStephen Boyd * registers.
9316c96a4a6SStephen Boyd */
9326c96a4a6SStephen Boyd if ((mvfr0 & MVFR0_A_SIMD_MASK) == 1)
9336c96a4a6SStephen Boyd /* also v4-D16 */
9346c96a4a6SStephen Boyd elf_hwcap |= HWCAP_VFPv3D16;
9356c96a4a6SStephen Boyd else
9366c96a4a6SStephen Boyd elf_hwcap |= HWCAP_VFPD32;
9376c96a4a6SStephen Boyd }
9387279dc3eSCatalin Marinas
9396c96a4a6SStephen Boyd if ((fmrx(MVFR1) & 0xf0000000) == 0x10000000)
9406c96a4a6SStephen Boyd elf_hwcap |= HWCAP_VFPv4;
941c00a19c8SAmit Daniel Kachhap if (((fmrx(MVFR1) & MVFR1_ASIMDHP_MASK) >> MVFR1_ASIMDHP_BIT) == 0x2)
942c00a19c8SAmit Daniel Kachhap elf_hwcap |= HWCAP_ASIMDHP;
943c00a19c8SAmit Daniel Kachhap if (((fmrx(MVFR1) & MVFR1_FPHP_MASK) >> MVFR1_FPHP_BIT) == 0x3)
944c00a19c8SAmit Daniel Kachhap elf_hwcap |= HWCAP_FPHP;
9452b94fe2aSStephen Boyd }
94662ea0d87SAmit Daniel Kachhap
94762ea0d87SAmit Daniel Kachhap /*
94862ea0d87SAmit Daniel Kachhap * Check for the presence of Advanced SIMD Dot Product
94962ea0d87SAmit Daniel Kachhap * instructions.
95062ea0d87SAmit Daniel Kachhap */
95162ea0d87SAmit Daniel Kachhap isar6 = read_cpuid_ext(CPUID_EXT_ISAR6);
95262ea0d87SAmit Daniel Kachhap if (cpuid_feature_extract_field(isar6, 4) == 0x1)
95362ea0d87SAmit Daniel Kachhap elf_hwcap |= HWCAP_ASIMDDP;
954ce483549SAmit Daniel Kachhap /*
955ce483549SAmit Daniel Kachhap * Check for the presence of Advanced SIMD Floating point
956ce483549SAmit Daniel Kachhap * half-precision multiplication instructions.
957ce483549SAmit Daniel Kachhap */
958ce483549SAmit Daniel Kachhap if (cpuid_feature_extract_field(isar6, 8) == 0x1)
959ce483549SAmit Daniel Kachhap elf_hwcap |= HWCAP_ASIMDFHM;
96023b6d4adSAmit Daniel Kachhap /*
96123b6d4adSAmit Daniel Kachhap * Check for the presence of Advanced SIMD Bfloat16
96223b6d4adSAmit Daniel Kachhap * floating point instructions.
96323b6d4adSAmit Daniel Kachhap */
96423b6d4adSAmit Daniel Kachhap if (cpuid_feature_extract_field(isar6, 20) == 0x1)
96523b6d4adSAmit Daniel Kachhap elf_hwcap |= HWCAP_ASIMDBF16;
966956ca3a4SAmit Daniel Kachhap /*
967956ca3a4SAmit Daniel Kachhap * Check for the presence of Advanced SIMD and floating point
968956ca3a4SAmit Daniel Kachhap * Int8 matrix multiplication instructions instructions.
969956ca3a4SAmit Daniel Kachhap */
970956ca3a4SAmit Daniel Kachhap if (cpuid_feature_extract_field(isar6, 24) == 0x1)
971956ca3a4SAmit Daniel Kachhap elf_hwcap |= HWCAP_I8MM;
97262ea0d87SAmit Daniel Kachhap
9736c96a4a6SStephen Boyd /* Extract the architecture version on pre-cpuid scheme */
9746c96a4a6SStephen Boyd } else {
9756c96a4a6SStephen Boyd if (vfpsid & FPSID_NODOUBLE) {
9766c96a4a6SStephen Boyd pr_cont("no double precision support\n");
9776c96a4a6SStephen Boyd return 0;
9786c96a4a6SStephen Boyd }
9796c96a4a6SStephen Boyd
9806c96a4a6SStephen Boyd VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT;
9816c96a4a6SStephen Boyd }
9826c96a4a6SStephen Boyd
983e5b61bafSThomas Gleixner cpuhp_setup_state_nocalls(CPUHP_AP_ARM_VFP_STARTING,
98473c1b41eSThomas Gleixner "arm/vfp:starting", vfp_starting_cpu,
985e5b61bafSThomas Gleixner vfp_dying_cpu);
9861da177e4SLinus Torvalds
987c76c6c4eSArd Biesheuvel have_vfp = true;
9881da177e4SLinus Torvalds
989cdd87465SArd Biesheuvel register_undef_hook(&vfp_support_hook);
9901da177e4SLinus Torvalds thread_register_notifier(&vfp_notifier_block);
9911da177e4SLinus Torvalds vfp_pm_init();
9921da177e4SLinus Torvalds
9931da177e4SLinus Torvalds /*
9941da177e4SLinus Torvalds * We detected VFP, and the support code is
9951da177e4SLinus Torvalds * in place; report VFP support to userspace.
9961da177e4SLinus Torvalds */
9971da177e4SLinus Torvalds elf_hwcap |= HWCAP_VFP;
9987279dc3eSCatalin Marinas
9996c96a4a6SStephen Boyd pr_cont("implementor %02x architecture %d part %02x variant %x rev %x\n",
10006c96a4a6SStephen Boyd (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
10016c96a4a6SStephen Boyd VFP_arch,
10026c96a4a6SStephen Boyd (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
10036c96a4a6SStephen Boyd (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
10046c96a4a6SStephen Boyd (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
10056c96a4a6SStephen Boyd
10061da177e4SLinus Torvalds return 0;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds
10090773d73dSArd Biesheuvel core_initcall(vfp_init);
1010