162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * linux/arch/arm/vfp/vfpmodule.c 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2004 ARM Limited. 662306a36Sopenharmony_ci * Written by Deep Blue Solutions Limited. 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci#include <linux/types.h> 962306a36Sopenharmony_ci#include <linux/cpu.h> 1062306a36Sopenharmony_ci#include <linux/cpu_pm.h> 1162306a36Sopenharmony_ci#include <linux/hardirq.h> 1262306a36Sopenharmony_ci#include <linux/kernel.h> 1362306a36Sopenharmony_ci#include <linux/notifier.h> 1462306a36Sopenharmony_ci#include <linux/signal.h> 1562306a36Sopenharmony_ci#include <linux/sched/signal.h> 1662306a36Sopenharmony_ci#include <linux/smp.h> 1762306a36Sopenharmony_ci#include <linux/init.h> 1862306a36Sopenharmony_ci#include <linux/uaccess.h> 1962306a36Sopenharmony_ci#include <linux/user.h> 2062306a36Sopenharmony_ci#include <linux/export.h> 2162306a36Sopenharmony_ci#include <linux/perf_event.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#include <asm/cp15.h> 2462306a36Sopenharmony_ci#include <asm/cputype.h> 2562306a36Sopenharmony_ci#include <asm/system_info.h> 2662306a36Sopenharmony_ci#include <asm/thread_notify.h> 2762306a36Sopenharmony_ci#include <asm/traps.h> 2862306a36Sopenharmony_ci#include <asm/vfp.h> 2962306a36Sopenharmony_ci#include <asm/neon.h> 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#include "vfpinstr.h" 3262306a36Sopenharmony_ci#include "vfp.h" 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_cistatic bool have_vfp __ro_after_init; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci/* 3762306a36Sopenharmony_ci * Dual-use variable. 3862306a36Sopenharmony_ci * Used in startup: set to non-zero if VFP checks fail 3962306a36Sopenharmony_ci * After startup, holds VFP architecture 4062306a36Sopenharmony_ci */ 4162306a36Sopenharmony_cistatic unsigned int VFP_arch; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci#ifdef CONFIG_CPU_FEROCEON 4462306a36Sopenharmony_ciextern unsigned int VFP_arch_feroceon __alias(VFP_arch); 4562306a36Sopenharmony_ci#endif 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* 4862306a36Sopenharmony_ci * The pointer to the vfpstate structure of the thread which currently 4962306a36Sopenharmony_ci * owns the context held in the VFP hardware, or NULL if the hardware 5062306a36Sopenharmony_ci * context is invalid. 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * For UP, this is sufficient to tell which thread owns the VFP context. 5362306a36Sopenharmony_ci * However, for SMP, we also need to check the CPU number stored in the 5462306a36Sopenharmony_ci * saved state too to catch migrations. 5562306a36Sopenharmony_ci */ 5662306a36Sopenharmony_ciunion vfp_state *vfp_current_hw_state[NR_CPUS]; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci/* 5962306a36Sopenharmony_ci * Is 'thread's most up to date state stored in this CPUs hardware? 6062306a36Sopenharmony_ci * Must be called from non-preemptible context. 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_cistatic bool vfp_state_in_hw(unsigned int cpu, struct thread_info *thread) 6362306a36Sopenharmony_ci{ 6462306a36Sopenharmony_ci#ifdef CONFIG_SMP 6562306a36Sopenharmony_ci if (thread->vfpstate.hard.cpu != cpu) 6662306a36Sopenharmony_ci return false; 6762306a36Sopenharmony_ci#endif 6862306a36Sopenharmony_ci return vfp_current_hw_state[cpu] == &thread->vfpstate; 6962306a36Sopenharmony_ci} 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci/* 7262306a36Sopenharmony_ci * Force a reload of the VFP context from the thread structure. We do 7362306a36Sopenharmony_ci * this by ensuring that access to the VFP hardware is disabled, and 7462306a36Sopenharmony_ci * clear vfp_current_hw_state. Must be called from non-preemptible context. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_cistatic void vfp_force_reload(unsigned int cpu, struct thread_info *thread) 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci if (vfp_state_in_hw(cpu, thread)) { 7962306a36Sopenharmony_ci fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); 8062306a36Sopenharmony_ci vfp_current_hw_state[cpu] = NULL; 8162306a36Sopenharmony_ci } 8262306a36Sopenharmony_ci#ifdef CONFIG_SMP 8362306a36Sopenharmony_ci thread->vfpstate.hard.cpu = NR_CPUS; 8462306a36Sopenharmony_ci#endif 8562306a36Sopenharmony_ci} 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci/* 8862306a36Sopenharmony_ci * Per-thread VFP initialization. 8962306a36Sopenharmony_ci */ 9062306a36Sopenharmony_cistatic void vfp_thread_flush(struct thread_info *thread) 9162306a36Sopenharmony_ci{ 9262306a36Sopenharmony_ci union vfp_state *vfp = &thread->vfpstate; 9362306a36Sopenharmony_ci unsigned int cpu; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci /* 9662306a36Sopenharmony_ci * Disable VFP to ensure we initialize it first. We must ensure 9762306a36Sopenharmony_ci * that the modification of vfp_current_hw_state[] and hardware 9862306a36Sopenharmony_ci * disable are done for the same CPU and without preemption. 9962306a36Sopenharmony_ci * 10062306a36Sopenharmony_ci * Do this first to ensure that preemption won't overwrite our 10162306a36Sopenharmony_ci * state saving should access to the VFP be enabled at this point. 10262306a36Sopenharmony_ci */ 10362306a36Sopenharmony_ci cpu = get_cpu(); 10462306a36Sopenharmony_ci if (vfp_current_hw_state[cpu] == vfp) 10562306a36Sopenharmony_ci vfp_current_hw_state[cpu] = NULL; 10662306a36Sopenharmony_ci fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); 10762306a36Sopenharmony_ci put_cpu(); 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci memset(vfp, 0, sizeof(union vfp_state)); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci vfp->hard.fpexc = FPEXC_EN; 11262306a36Sopenharmony_ci vfp->hard.fpscr = FPSCR_ROUND_NEAREST; 11362306a36Sopenharmony_ci#ifdef CONFIG_SMP 11462306a36Sopenharmony_ci vfp->hard.cpu = NR_CPUS; 11562306a36Sopenharmony_ci#endif 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_cistatic void vfp_thread_exit(struct thread_info *thread) 11962306a36Sopenharmony_ci{ 12062306a36Sopenharmony_ci /* release case: Per-thread VFP cleanup. */ 12162306a36Sopenharmony_ci union vfp_state *vfp = &thread->vfpstate; 12262306a36Sopenharmony_ci unsigned int cpu = get_cpu(); 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci if (vfp_current_hw_state[cpu] == vfp) 12562306a36Sopenharmony_ci vfp_current_hw_state[cpu] = NULL; 12662306a36Sopenharmony_ci put_cpu(); 12762306a36Sopenharmony_ci} 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_cistatic void vfp_thread_copy(struct thread_info *thread) 13062306a36Sopenharmony_ci{ 13162306a36Sopenharmony_ci struct thread_info *parent = current_thread_info(); 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci vfp_sync_hwstate(parent); 13462306a36Sopenharmony_ci thread->vfpstate = parent->vfpstate; 13562306a36Sopenharmony_ci#ifdef CONFIG_SMP 13662306a36Sopenharmony_ci thread->vfpstate.hard.cpu = NR_CPUS; 13762306a36Sopenharmony_ci#endif 13862306a36Sopenharmony_ci} 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci/* 14162306a36Sopenharmony_ci * When this function is called with the following 'cmd's, the following 14262306a36Sopenharmony_ci * is true while this function is being run: 14362306a36Sopenharmony_ci * THREAD_NOFTIFY_SWTICH: 14462306a36Sopenharmony_ci * - the previously running thread will not be scheduled onto another CPU. 14562306a36Sopenharmony_ci * - the next thread to be run (v) will not be running on another CPU. 14662306a36Sopenharmony_ci * - thread->cpu is the local CPU number 14762306a36Sopenharmony_ci * - not preemptible as we're called in the middle of a thread switch 14862306a36Sopenharmony_ci * THREAD_NOTIFY_FLUSH: 14962306a36Sopenharmony_ci * - the thread (v) will be running on the local CPU, so 15062306a36Sopenharmony_ci * v === current_thread_info() 15162306a36Sopenharmony_ci * - thread->cpu is the local CPU number at the time it is accessed, 15262306a36Sopenharmony_ci * but may change at any time. 15362306a36Sopenharmony_ci * - we could be preempted if tree preempt rcu is enabled, so 15462306a36Sopenharmony_ci * it is unsafe to use thread->cpu. 15562306a36Sopenharmony_ci * THREAD_NOTIFY_EXIT 15662306a36Sopenharmony_ci * - we could be preempted if tree preempt rcu is enabled, so 15762306a36Sopenharmony_ci * it is unsafe to use thread->cpu. 15862306a36Sopenharmony_ci */ 15962306a36Sopenharmony_cistatic int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v) 16062306a36Sopenharmony_ci{ 16162306a36Sopenharmony_ci struct thread_info *thread = v; 16262306a36Sopenharmony_ci u32 fpexc; 16362306a36Sopenharmony_ci#ifdef CONFIG_SMP 16462306a36Sopenharmony_ci unsigned int cpu; 16562306a36Sopenharmony_ci#endif 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci switch (cmd) { 16862306a36Sopenharmony_ci case THREAD_NOTIFY_SWITCH: 16962306a36Sopenharmony_ci fpexc = fmrx(FPEXC); 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci#ifdef CONFIG_SMP 17262306a36Sopenharmony_ci cpu = thread->cpu; 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci /* 17562306a36Sopenharmony_ci * On SMP, if VFP is enabled, save the old state in 17662306a36Sopenharmony_ci * case the thread migrates to a different CPU. The 17762306a36Sopenharmony_ci * restoring is done lazily. 17862306a36Sopenharmony_ci */ 17962306a36Sopenharmony_ci if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu]) 18062306a36Sopenharmony_ci vfp_save_state(vfp_current_hw_state[cpu], fpexc); 18162306a36Sopenharmony_ci#endif 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci /* 18462306a36Sopenharmony_ci * Always disable VFP so we can lazily save/restore the 18562306a36Sopenharmony_ci * old state. 18662306a36Sopenharmony_ci */ 18762306a36Sopenharmony_ci fmxr(FPEXC, fpexc & ~FPEXC_EN); 18862306a36Sopenharmony_ci break; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci case THREAD_NOTIFY_FLUSH: 19162306a36Sopenharmony_ci vfp_thread_flush(thread); 19262306a36Sopenharmony_ci break; 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci case THREAD_NOTIFY_EXIT: 19562306a36Sopenharmony_ci vfp_thread_exit(thread); 19662306a36Sopenharmony_ci break; 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci case THREAD_NOTIFY_COPY: 19962306a36Sopenharmony_ci vfp_thread_copy(thread); 20062306a36Sopenharmony_ci break; 20162306a36Sopenharmony_ci } 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci return NOTIFY_DONE; 20462306a36Sopenharmony_ci} 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_cistatic struct notifier_block vfp_notifier_block = { 20762306a36Sopenharmony_ci .notifier_call = vfp_notifier, 20862306a36Sopenharmony_ci}; 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci/* 21162306a36Sopenharmony_ci * Raise a SIGFPE for the current process. 21262306a36Sopenharmony_ci * sicode describes the signal being raised. 21362306a36Sopenharmony_ci */ 21462306a36Sopenharmony_cistatic void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs) 21562306a36Sopenharmony_ci{ 21662306a36Sopenharmony_ci /* 21762306a36Sopenharmony_ci * This is the same as NWFPE, because it's not clear what 21862306a36Sopenharmony_ci * this is used for 21962306a36Sopenharmony_ci */ 22062306a36Sopenharmony_ci current->thread.error_code = 0; 22162306a36Sopenharmony_ci current->thread.trap_no = 6; 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci send_sig_fault(SIGFPE, sicode, 22462306a36Sopenharmony_ci (void __user *)(instruction_pointer(regs) - 4), 22562306a36Sopenharmony_ci current); 22662306a36Sopenharmony_ci} 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_cistatic void vfp_panic(char *reason, u32 inst) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci int i; 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci pr_err("VFP: Error: %s\n", reason); 23362306a36Sopenharmony_ci pr_err("VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n", 23462306a36Sopenharmony_ci fmrx(FPEXC), fmrx(FPSCR), inst); 23562306a36Sopenharmony_ci for (i = 0; i < 32; i += 2) 23662306a36Sopenharmony_ci pr_err("VFP: s%2u: 0x%08x s%2u: 0x%08x\n", 23762306a36Sopenharmony_ci i, vfp_get_float(i), i+1, vfp_get_float(i+1)); 23862306a36Sopenharmony_ci} 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci/* 24162306a36Sopenharmony_ci * Process bitmask of exception conditions. 24262306a36Sopenharmony_ci */ 24362306a36Sopenharmony_cistatic void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs) 24462306a36Sopenharmony_ci{ 24562306a36Sopenharmony_ci int si_code = 0; 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci pr_debug("VFP: raising exceptions %08x\n", exceptions); 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci if (exceptions == VFP_EXCEPTION_ERROR) { 25062306a36Sopenharmony_ci vfp_panic("unhandled bounce", inst); 25162306a36Sopenharmony_ci vfp_raise_sigfpe(FPE_FLTINV, regs); 25262306a36Sopenharmony_ci return; 25362306a36Sopenharmony_ci } 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci /* 25662306a36Sopenharmony_ci * If any of the status flags are set, update the FPSCR. 25762306a36Sopenharmony_ci * Comparison instructions always return at least one of 25862306a36Sopenharmony_ci * these flags set. 25962306a36Sopenharmony_ci */ 26062306a36Sopenharmony_ci if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V)) 26162306a36Sopenharmony_ci fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V); 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci fpscr |= exceptions; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci fmxr(FPSCR, fpscr); 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci#define RAISE(stat,en,sig) \ 26862306a36Sopenharmony_ci if (exceptions & stat && fpscr & en) \ 26962306a36Sopenharmony_ci si_code = sig; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci /* 27262306a36Sopenharmony_ci * These are arranged in priority order, least to highest. 27362306a36Sopenharmony_ci */ 27462306a36Sopenharmony_ci RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV); 27562306a36Sopenharmony_ci RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES); 27662306a36Sopenharmony_ci RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND); 27762306a36Sopenharmony_ci RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF); 27862306a36Sopenharmony_ci RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV); 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci if (si_code) 28162306a36Sopenharmony_ci vfp_raise_sigfpe(si_code, regs); 28262306a36Sopenharmony_ci} 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci/* 28562306a36Sopenharmony_ci * Emulate a VFP instruction. 28662306a36Sopenharmony_ci */ 28762306a36Sopenharmony_cistatic u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs) 28862306a36Sopenharmony_ci{ 28962306a36Sopenharmony_ci u32 exceptions = VFP_EXCEPTION_ERROR; 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr); 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci if (INST_CPRTDO(inst)) { 29462306a36Sopenharmony_ci if (!INST_CPRT(inst)) { 29562306a36Sopenharmony_ci /* 29662306a36Sopenharmony_ci * CPDO 29762306a36Sopenharmony_ci */ 29862306a36Sopenharmony_ci if (vfp_single(inst)) { 29962306a36Sopenharmony_ci exceptions = vfp_single_cpdo(inst, fpscr); 30062306a36Sopenharmony_ci } else { 30162306a36Sopenharmony_ci exceptions = vfp_double_cpdo(inst, fpscr); 30262306a36Sopenharmony_ci } 30362306a36Sopenharmony_ci } else { 30462306a36Sopenharmony_ci /* 30562306a36Sopenharmony_ci * A CPRT instruction can not appear in FPINST2, nor 30662306a36Sopenharmony_ci * can it cause an exception. Therefore, we do not 30762306a36Sopenharmony_ci * have to emulate it. 30862306a36Sopenharmony_ci */ 30962306a36Sopenharmony_ci } 31062306a36Sopenharmony_ci } else { 31162306a36Sopenharmony_ci /* 31262306a36Sopenharmony_ci * A CPDT instruction can not appear in FPINST2, nor can 31362306a36Sopenharmony_ci * it cause an exception. Therefore, we do not have to 31462306a36Sopenharmony_ci * emulate it. 31562306a36Sopenharmony_ci */ 31662306a36Sopenharmony_ci } 31762306a36Sopenharmony_ci perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc); 31862306a36Sopenharmony_ci return exceptions & ~VFP_NAN_FLAG; 31962306a36Sopenharmony_ci} 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci/* 32262306a36Sopenharmony_ci * Package up a bounce condition. 32362306a36Sopenharmony_ci */ 32462306a36Sopenharmony_cistatic void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs) 32562306a36Sopenharmony_ci{ 32662306a36Sopenharmony_ci u32 fpscr, orig_fpscr, fpsid, exceptions; 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc); 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci /* 33162306a36Sopenharmony_ci * At this point, FPEXC can have the following configuration: 33262306a36Sopenharmony_ci * 33362306a36Sopenharmony_ci * EX DEX IXE 33462306a36Sopenharmony_ci * 0 1 x - synchronous exception 33562306a36Sopenharmony_ci * 1 x 0 - asynchronous exception 33662306a36Sopenharmony_ci * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later 33762306a36Sopenharmony_ci * 0 0 1 - synchronous on VFP9 (non-standard subarch 1 33862306a36Sopenharmony_ci * implementation), undefined otherwise 33962306a36Sopenharmony_ci * 34062306a36Sopenharmony_ci * Clear various bits and enable access to the VFP so we can 34162306a36Sopenharmony_ci * handle the bounce. 34262306a36Sopenharmony_ci */ 34362306a36Sopenharmony_ci fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK)); 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci fpsid = fmrx(FPSID); 34662306a36Sopenharmony_ci orig_fpscr = fpscr = fmrx(FPSCR); 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci /* 34962306a36Sopenharmony_ci * Check for the special VFP subarch 1 and FPSCR.IXE bit case 35062306a36Sopenharmony_ci */ 35162306a36Sopenharmony_ci if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT) 35262306a36Sopenharmony_ci && (fpscr & FPSCR_IXE)) { 35362306a36Sopenharmony_ci /* 35462306a36Sopenharmony_ci * Synchronous exception, emulate the trigger instruction 35562306a36Sopenharmony_ci */ 35662306a36Sopenharmony_ci goto emulate; 35762306a36Sopenharmony_ci } 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci if (fpexc & FPEXC_EX) { 36062306a36Sopenharmony_ci /* 36162306a36Sopenharmony_ci * Asynchronous exception. The instruction is read from FPINST 36262306a36Sopenharmony_ci * and the interrupted instruction has to be restarted. 36362306a36Sopenharmony_ci */ 36462306a36Sopenharmony_ci trigger = fmrx(FPINST); 36562306a36Sopenharmony_ci regs->ARM_pc -= 4; 36662306a36Sopenharmony_ci } else if (!(fpexc & FPEXC_DEX)) { 36762306a36Sopenharmony_ci /* 36862306a36Sopenharmony_ci * Illegal combination of bits. It can be caused by an 36962306a36Sopenharmony_ci * unallocated VFP instruction but with FPSCR.IXE set and not 37062306a36Sopenharmony_ci * on VFP subarch 1. 37162306a36Sopenharmony_ci */ 37262306a36Sopenharmony_ci vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs); 37362306a36Sopenharmony_ci return; 37462306a36Sopenharmony_ci } 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci /* 37762306a36Sopenharmony_ci * Modify fpscr to indicate the number of iterations remaining. 37862306a36Sopenharmony_ci * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates 37962306a36Sopenharmony_ci * whether FPEXC.VECITR or FPSCR.LEN is used. 38062306a36Sopenharmony_ci */ 38162306a36Sopenharmony_ci if (fpexc & (FPEXC_EX | FPEXC_VV)) { 38262306a36Sopenharmony_ci u32 len; 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ci len = fpexc + (1 << FPEXC_LENGTH_BIT); 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci fpscr &= ~FPSCR_LENGTH_MASK; 38762306a36Sopenharmony_ci fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT); 38862306a36Sopenharmony_ci } 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci /* 39162306a36Sopenharmony_ci * Handle the first FP instruction. We used to take note of the 39262306a36Sopenharmony_ci * FPEXC bounce reason, but this appears to be unreliable. 39362306a36Sopenharmony_ci * Emulate the bounced instruction instead. 39462306a36Sopenharmony_ci */ 39562306a36Sopenharmony_ci exceptions = vfp_emulate_instruction(trigger, fpscr, regs); 39662306a36Sopenharmony_ci if (exceptions) 39762306a36Sopenharmony_ci vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs); 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci /* 40062306a36Sopenharmony_ci * If there isn't a second FP instruction, exit now. Note that 40162306a36Sopenharmony_ci * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1. 40262306a36Sopenharmony_ci */ 40362306a36Sopenharmony_ci if ((fpexc & (FPEXC_EX | FPEXC_FP2V)) != (FPEXC_EX | FPEXC_FP2V)) 40462306a36Sopenharmony_ci return; 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci /* 40762306a36Sopenharmony_ci * The barrier() here prevents fpinst2 being read 40862306a36Sopenharmony_ci * before the condition above. 40962306a36Sopenharmony_ci */ 41062306a36Sopenharmony_ci barrier(); 41162306a36Sopenharmony_ci trigger = fmrx(FPINST2); 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_ci emulate: 41462306a36Sopenharmony_ci exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs); 41562306a36Sopenharmony_ci if (exceptions) 41662306a36Sopenharmony_ci vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs); 41762306a36Sopenharmony_ci} 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_cistatic void vfp_enable(void *unused) 42062306a36Sopenharmony_ci{ 42162306a36Sopenharmony_ci u32 access; 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ci BUG_ON(preemptible()); 42462306a36Sopenharmony_ci access = get_copro_access(); 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci /* 42762306a36Sopenharmony_ci * Enable full access to VFP (cp10 and cp11) 42862306a36Sopenharmony_ci */ 42962306a36Sopenharmony_ci set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11)); 43062306a36Sopenharmony_ci} 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci/* Called by platforms on which we want to disable VFP because it may not be 43362306a36Sopenharmony_ci * present on all CPUs within a SMP complex. Needs to be called prior to 43462306a36Sopenharmony_ci * vfp_init(). 43562306a36Sopenharmony_ci */ 43662306a36Sopenharmony_civoid __init vfp_disable(void) 43762306a36Sopenharmony_ci{ 43862306a36Sopenharmony_ci if (VFP_arch) { 43962306a36Sopenharmony_ci pr_debug("%s: should be called prior to vfp_init\n", __func__); 44062306a36Sopenharmony_ci return; 44162306a36Sopenharmony_ci } 44262306a36Sopenharmony_ci VFP_arch = 1; 44362306a36Sopenharmony_ci} 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci#ifdef CONFIG_CPU_PM 44662306a36Sopenharmony_cistatic int vfp_pm_suspend(void) 44762306a36Sopenharmony_ci{ 44862306a36Sopenharmony_ci struct thread_info *ti = current_thread_info(); 44962306a36Sopenharmony_ci u32 fpexc = fmrx(FPEXC); 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci /* if vfp is on, then save state for resumption */ 45262306a36Sopenharmony_ci if (fpexc & FPEXC_EN) { 45362306a36Sopenharmony_ci pr_debug("%s: saving vfp state\n", __func__); 45462306a36Sopenharmony_ci vfp_save_state(&ti->vfpstate, fpexc); 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci /* disable, just in case */ 45762306a36Sopenharmony_ci fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); 45862306a36Sopenharmony_ci } else if (vfp_current_hw_state[ti->cpu]) { 45962306a36Sopenharmony_ci#ifndef CONFIG_SMP 46062306a36Sopenharmony_ci fmxr(FPEXC, fpexc | FPEXC_EN); 46162306a36Sopenharmony_ci vfp_save_state(vfp_current_hw_state[ti->cpu], fpexc); 46262306a36Sopenharmony_ci fmxr(FPEXC, fpexc); 46362306a36Sopenharmony_ci#endif 46462306a36Sopenharmony_ci } 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci /* clear any information we had about last context state */ 46762306a36Sopenharmony_ci vfp_current_hw_state[ti->cpu] = NULL; 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci return 0; 47062306a36Sopenharmony_ci} 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_cistatic void vfp_pm_resume(void) 47362306a36Sopenharmony_ci{ 47462306a36Sopenharmony_ci /* ensure we have access to the vfp */ 47562306a36Sopenharmony_ci vfp_enable(NULL); 47662306a36Sopenharmony_ci 47762306a36Sopenharmony_ci /* and disable it to ensure the next usage restores the state */ 47862306a36Sopenharmony_ci fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); 47962306a36Sopenharmony_ci} 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_cistatic int vfp_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd, 48262306a36Sopenharmony_ci void *v) 48362306a36Sopenharmony_ci{ 48462306a36Sopenharmony_ci switch (cmd) { 48562306a36Sopenharmony_ci case CPU_PM_ENTER: 48662306a36Sopenharmony_ci vfp_pm_suspend(); 48762306a36Sopenharmony_ci break; 48862306a36Sopenharmony_ci case CPU_PM_ENTER_FAILED: 48962306a36Sopenharmony_ci case CPU_PM_EXIT: 49062306a36Sopenharmony_ci vfp_pm_resume(); 49162306a36Sopenharmony_ci break; 49262306a36Sopenharmony_ci } 49362306a36Sopenharmony_ci return NOTIFY_OK; 49462306a36Sopenharmony_ci} 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_cistatic struct notifier_block vfp_cpu_pm_notifier_block = { 49762306a36Sopenharmony_ci .notifier_call = vfp_cpu_pm_notifier, 49862306a36Sopenharmony_ci}; 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_cistatic void vfp_pm_init(void) 50162306a36Sopenharmony_ci{ 50262306a36Sopenharmony_ci cpu_pm_register_notifier(&vfp_cpu_pm_notifier_block); 50362306a36Sopenharmony_ci} 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci#else 50662306a36Sopenharmony_cistatic inline void vfp_pm_init(void) { } 50762306a36Sopenharmony_ci#endif /* CONFIG_CPU_PM */ 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci/* 51062306a36Sopenharmony_ci * Ensure that the VFP state stored in 'thread->vfpstate' is up to date 51162306a36Sopenharmony_ci * with the hardware state. 51262306a36Sopenharmony_ci */ 51362306a36Sopenharmony_civoid vfp_sync_hwstate(struct thread_info *thread) 51462306a36Sopenharmony_ci{ 51562306a36Sopenharmony_ci unsigned int cpu = get_cpu(); 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ci local_bh_disable(); 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ci if (vfp_state_in_hw(cpu, thread)) { 52062306a36Sopenharmony_ci u32 fpexc = fmrx(FPEXC); 52162306a36Sopenharmony_ci 52262306a36Sopenharmony_ci /* 52362306a36Sopenharmony_ci * Save the last VFP state on this CPU. 52462306a36Sopenharmony_ci */ 52562306a36Sopenharmony_ci fmxr(FPEXC, fpexc | FPEXC_EN); 52662306a36Sopenharmony_ci vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN); 52762306a36Sopenharmony_ci fmxr(FPEXC, fpexc); 52862306a36Sopenharmony_ci } 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci local_bh_enable(); 53162306a36Sopenharmony_ci put_cpu(); 53262306a36Sopenharmony_ci} 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ci/* Ensure that the thread reloads the hardware VFP state on the next use. */ 53562306a36Sopenharmony_civoid vfp_flush_hwstate(struct thread_info *thread) 53662306a36Sopenharmony_ci{ 53762306a36Sopenharmony_ci unsigned int cpu = get_cpu(); 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci vfp_force_reload(cpu, thread); 54062306a36Sopenharmony_ci 54162306a36Sopenharmony_ci put_cpu(); 54262306a36Sopenharmony_ci} 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci/* 54562306a36Sopenharmony_ci * Save the current VFP state into the provided structures and prepare 54662306a36Sopenharmony_ci * for entry into a new function (signal handler). 54762306a36Sopenharmony_ci */ 54862306a36Sopenharmony_ciint vfp_preserve_user_clear_hwstate(struct user_vfp *ufp, 54962306a36Sopenharmony_ci struct user_vfp_exc *ufp_exc) 55062306a36Sopenharmony_ci{ 55162306a36Sopenharmony_ci struct thread_info *thread = current_thread_info(); 55262306a36Sopenharmony_ci struct vfp_hard_struct *hwstate = &thread->vfpstate.hard; 55362306a36Sopenharmony_ci 55462306a36Sopenharmony_ci /* Ensure that the saved hwstate is up-to-date. */ 55562306a36Sopenharmony_ci vfp_sync_hwstate(thread); 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci /* 55862306a36Sopenharmony_ci * Copy the floating point registers. There can be unused 55962306a36Sopenharmony_ci * registers see asm/hwcap.h for details. 56062306a36Sopenharmony_ci */ 56162306a36Sopenharmony_ci memcpy(&ufp->fpregs, &hwstate->fpregs, sizeof(hwstate->fpregs)); 56262306a36Sopenharmony_ci 56362306a36Sopenharmony_ci /* 56462306a36Sopenharmony_ci * Copy the status and control register. 56562306a36Sopenharmony_ci */ 56662306a36Sopenharmony_ci ufp->fpscr = hwstate->fpscr; 56762306a36Sopenharmony_ci 56862306a36Sopenharmony_ci /* 56962306a36Sopenharmony_ci * Copy the exception registers. 57062306a36Sopenharmony_ci */ 57162306a36Sopenharmony_ci ufp_exc->fpexc = hwstate->fpexc; 57262306a36Sopenharmony_ci ufp_exc->fpinst = hwstate->fpinst; 57362306a36Sopenharmony_ci ufp_exc->fpinst2 = hwstate->fpinst2; 57462306a36Sopenharmony_ci 57562306a36Sopenharmony_ci /* Ensure that VFP is disabled. */ 57662306a36Sopenharmony_ci vfp_flush_hwstate(thread); 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci /* 57962306a36Sopenharmony_ci * As per the PCS, clear the length and stride bits for function 58062306a36Sopenharmony_ci * entry. 58162306a36Sopenharmony_ci */ 58262306a36Sopenharmony_ci hwstate->fpscr &= ~(FPSCR_LENGTH_MASK | FPSCR_STRIDE_MASK); 58362306a36Sopenharmony_ci return 0; 58462306a36Sopenharmony_ci} 58562306a36Sopenharmony_ci 58662306a36Sopenharmony_ci/* Sanitise and restore the current VFP state from the provided structures. */ 58762306a36Sopenharmony_ciint vfp_restore_user_hwstate(struct user_vfp *ufp, struct user_vfp_exc *ufp_exc) 58862306a36Sopenharmony_ci{ 58962306a36Sopenharmony_ci struct thread_info *thread = current_thread_info(); 59062306a36Sopenharmony_ci struct vfp_hard_struct *hwstate = &thread->vfpstate.hard; 59162306a36Sopenharmony_ci unsigned long fpexc; 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci /* Disable VFP to avoid corrupting the new thread state. */ 59462306a36Sopenharmony_ci vfp_flush_hwstate(thread); 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci /* 59762306a36Sopenharmony_ci * Copy the floating point registers. There can be unused 59862306a36Sopenharmony_ci * registers see asm/hwcap.h for details. 59962306a36Sopenharmony_ci */ 60062306a36Sopenharmony_ci memcpy(&hwstate->fpregs, &ufp->fpregs, sizeof(hwstate->fpregs)); 60162306a36Sopenharmony_ci /* 60262306a36Sopenharmony_ci * Copy the status and control register. 60362306a36Sopenharmony_ci */ 60462306a36Sopenharmony_ci hwstate->fpscr = ufp->fpscr; 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci /* 60762306a36Sopenharmony_ci * Sanitise and restore the exception registers. 60862306a36Sopenharmony_ci */ 60962306a36Sopenharmony_ci fpexc = ufp_exc->fpexc; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci /* Ensure the VFP is enabled. */ 61262306a36Sopenharmony_ci fpexc |= FPEXC_EN; 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci /* Ensure FPINST2 is invalid and the exception flag is cleared. */ 61562306a36Sopenharmony_ci fpexc &= ~(FPEXC_EX | FPEXC_FP2V); 61662306a36Sopenharmony_ci hwstate->fpexc = fpexc; 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_ci hwstate->fpinst = ufp_exc->fpinst; 61962306a36Sopenharmony_ci hwstate->fpinst2 = ufp_exc->fpinst2; 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci return 0; 62262306a36Sopenharmony_ci} 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ci/* 62562306a36Sopenharmony_ci * VFP hardware can lose all context when a CPU goes offline. 62662306a36Sopenharmony_ci * As we will be running in SMP mode with CPU hotplug, we will save the 62762306a36Sopenharmony_ci * hardware state at every thread switch. We clear our held state when 62862306a36Sopenharmony_ci * a CPU has been killed, indicating that the VFP hardware doesn't contain 62962306a36Sopenharmony_ci * a threads VFP state. When a CPU starts up, we re-enable access to the 63062306a36Sopenharmony_ci * VFP hardware. The callbacks below are called on the CPU which 63162306a36Sopenharmony_ci * is being offlined/onlined. 63262306a36Sopenharmony_ci */ 63362306a36Sopenharmony_cistatic int vfp_dying_cpu(unsigned int cpu) 63462306a36Sopenharmony_ci{ 63562306a36Sopenharmony_ci vfp_current_hw_state[cpu] = NULL; 63662306a36Sopenharmony_ci return 0; 63762306a36Sopenharmony_ci} 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_cistatic int vfp_starting_cpu(unsigned int unused) 64062306a36Sopenharmony_ci{ 64162306a36Sopenharmony_ci vfp_enable(NULL); 64262306a36Sopenharmony_ci return 0; 64362306a36Sopenharmony_ci} 64462306a36Sopenharmony_ci 64562306a36Sopenharmony_cistatic int vfp_kmode_exception(struct pt_regs *regs, unsigned int instr) 64662306a36Sopenharmony_ci{ 64762306a36Sopenharmony_ci /* 64862306a36Sopenharmony_ci * If we reach this point, a floating point exception has been raised 64962306a36Sopenharmony_ci * while running in kernel mode. If the NEON/VFP unit was enabled at the 65062306a36Sopenharmony_ci * time, it means a VFP instruction has been issued that requires 65162306a36Sopenharmony_ci * software assistance to complete, something which is not currently 65262306a36Sopenharmony_ci * supported in kernel mode. 65362306a36Sopenharmony_ci * If the NEON/VFP unit was disabled, and the location pointed to below 65462306a36Sopenharmony_ci * is properly preceded by a call to kernel_neon_begin(), something has 65562306a36Sopenharmony_ci * caused the task to be scheduled out and back in again. In this case, 65662306a36Sopenharmony_ci * rebuilding and running with CONFIG_DEBUG_ATOMIC_SLEEP enabled should 65762306a36Sopenharmony_ci * be helpful in localizing the problem. 65862306a36Sopenharmony_ci */ 65962306a36Sopenharmony_ci if (fmrx(FPEXC) & FPEXC_EN) 66062306a36Sopenharmony_ci pr_crit("BUG: unsupported FP instruction in kernel mode\n"); 66162306a36Sopenharmony_ci else 66262306a36Sopenharmony_ci pr_crit("BUG: FP instruction issued in kernel mode with FP unit disabled\n"); 66362306a36Sopenharmony_ci pr_crit("FPEXC == 0x%08x\n", fmrx(FPEXC)); 66462306a36Sopenharmony_ci return 1; 66562306a36Sopenharmony_ci} 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci/* 66862306a36Sopenharmony_ci * vfp_support_entry - Handle VFP exception 66962306a36Sopenharmony_ci * 67062306a36Sopenharmony_ci * @regs: pt_regs structure holding the register state at exception entry 67162306a36Sopenharmony_ci * @trigger: The opcode of the instruction that triggered the exception 67262306a36Sopenharmony_ci * 67362306a36Sopenharmony_ci * Returns 0 if the exception was handled, or an error code otherwise. 67462306a36Sopenharmony_ci */ 67562306a36Sopenharmony_cistatic int vfp_support_entry(struct pt_regs *regs, u32 trigger) 67662306a36Sopenharmony_ci{ 67762306a36Sopenharmony_ci struct thread_info *ti = current_thread_info(); 67862306a36Sopenharmony_ci u32 fpexc; 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci if (unlikely(!have_vfp)) 68162306a36Sopenharmony_ci return -ENODEV; 68262306a36Sopenharmony_ci 68362306a36Sopenharmony_ci if (!user_mode(regs)) 68462306a36Sopenharmony_ci return vfp_kmode_exception(regs, trigger); 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci local_bh_disable(); 68762306a36Sopenharmony_ci fpexc = fmrx(FPEXC); 68862306a36Sopenharmony_ci 68962306a36Sopenharmony_ci /* 69062306a36Sopenharmony_ci * If the VFP unit was not enabled yet, we have to check whether the 69162306a36Sopenharmony_ci * VFP state in the CPU's registers is the most recent VFP state 69262306a36Sopenharmony_ci * associated with the process. On UP systems, we don't save the VFP 69362306a36Sopenharmony_ci * state eagerly on a context switch, so we may need to save the 69462306a36Sopenharmony_ci * VFP state to memory first, as it may belong to another process. 69562306a36Sopenharmony_ci */ 69662306a36Sopenharmony_ci if (!(fpexc & FPEXC_EN)) { 69762306a36Sopenharmony_ci /* 69862306a36Sopenharmony_ci * Enable the VFP unit but mask the FP exception flag for the 69962306a36Sopenharmony_ci * time being, so we can access all the registers. 70062306a36Sopenharmony_ci */ 70162306a36Sopenharmony_ci fpexc |= FPEXC_EN; 70262306a36Sopenharmony_ci fmxr(FPEXC, fpexc & ~FPEXC_EX); 70362306a36Sopenharmony_ci 70462306a36Sopenharmony_ci /* 70562306a36Sopenharmony_ci * Check whether or not the VFP state in the CPU's registers is 70662306a36Sopenharmony_ci * the most recent VFP state associated with this task. On SMP, 70762306a36Sopenharmony_ci * migration may result in multiple CPUs holding VFP states 70862306a36Sopenharmony_ci * that belong to the same task, but only the most recent one 70962306a36Sopenharmony_ci * is valid. 71062306a36Sopenharmony_ci */ 71162306a36Sopenharmony_ci if (!vfp_state_in_hw(ti->cpu, ti)) { 71262306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_SMP) && 71362306a36Sopenharmony_ci vfp_current_hw_state[ti->cpu] != NULL) { 71462306a36Sopenharmony_ci /* 71562306a36Sopenharmony_ci * This CPU is currently holding the most 71662306a36Sopenharmony_ci * recent VFP state associated with another 71762306a36Sopenharmony_ci * task, and we must save that to memory first. 71862306a36Sopenharmony_ci */ 71962306a36Sopenharmony_ci vfp_save_state(vfp_current_hw_state[ti->cpu], 72062306a36Sopenharmony_ci fpexc); 72162306a36Sopenharmony_ci } 72262306a36Sopenharmony_ci 72362306a36Sopenharmony_ci /* 72462306a36Sopenharmony_ci * We can now proceed with loading the task's VFP state 72562306a36Sopenharmony_ci * from memory into the CPU registers. 72662306a36Sopenharmony_ci */ 72762306a36Sopenharmony_ci fpexc = vfp_load_state(&ti->vfpstate); 72862306a36Sopenharmony_ci vfp_current_hw_state[ti->cpu] = &ti->vfpstate; 72962306a36Sopenharmony_ci#ifdef CONFIG_SMP 73062306a36Sopenharmony_ci /* 73162306a36Sopenharmony_ci * Record that this CPU is now the one holding the most 73262306a36Sopenharmony_ci * recent VFP state of the task. 73362306a36Sopenharmony_ci */ 73462306a36Sopenharmony_ci ti->vfpstate.hard.cpu = ti->cpu; 73562306a36Sopenharmony_ci#endif 73662306a36Sopenharmony_ci } 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci if (fpexc & FPEXC_EX) 73962306a36Sopenharmony_ci /* 74062306a36Sopenharmony_ci * Might as well handle the pending exception before 74162306a36Sopenharmony_ci * retrying branch out before setting an FPEXC that 74262306a36Sopenharmony_ci * stops us reading stuff. 74362306a36Sopenharmony_ci */ 74462306a36Sopenharmony_ci goto bounce; 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_ci /* 74762306a36Sopenharmony_ci * No FP exception is pending: just enable the VFP and 74862306a36Sopenharmony_ci * replay the instruction that trapped. 74962306a36Sopenharmony_ci */ 75062306a36Sopenharmony_ci fmxr(FPEXC, fpexc); 75162306a36Sopenharmony_ci } else { 75262306a36Sopenharmony_ci /* Check for synchronous or asynchronous exceptions */ 75362306a36Sopenharmony_ci if (!(fpexc & (FPEXC_EX | FPEXC_DEX))) { 75462306a36Sopenharmony_ci u32 fpscr = fmrx(FPSCR); 75562306a36Sopenharmony_ci 75662306a36Sopenharmony_ci /* 75762306a36Sopenharmony_ci * On some implementations of the VFP subarch 1, 75862306a36Sopenharmony_ci * setting FPSCR.IXE causes all the CDP instructions to 75962306a36Sopenharmony_ci * be bounced synchronously without setting the 76062306a36Sopenharmony_ci * FPEXC.EX bit 76162306a36Sopenharmony_ci */ 76262306a36Sopenharmony_ci if (!(fpscr & FPSCR_IXE)) { 76362306a36Sopenharmony_ci if (!(fpscr & FPSCR_LENGTH_MASK)) { 76462306a36Sopenharmony_ci pr_debug("not VFP\n"); 76562306a36Sopenharmony_ci local_bh_enable(); 76662306a36Sopenharmony_ci return -ENOEXEC; 76762306a36Sopenharmony_ci } 76862306a36Sopenharmony_ci fpexc |= FPEXC_DEX; 76962306a36Sopenharmony_ci } 77062306a36Sopenharmony_ci } 77162306a36Sopenharmony_cibounce: regs->ARM_pc += 4; 77262306a36Sopenharmony_ci VFP_bounce(trigger, fpexc, regs); 77362306a36Sopenharmony_ci } 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_ci local_bh_enable(); 77662306a36Sopenharmony_ci return 0; 77762306a36Sopenharmony_ci} 77862306a36Sopenharmony_ci 77962306a36Sopenharmony_cistatic struct undef_hook neon_support_hook[] = {{ 78062306a36Sopenharmony_ci .instr_mask = 0xfe000000, 78162306a36Sopenharmony_ci .instr_val = 0xf2000000, 78262306a36Sopenharmony_ci .cpsr_mask = PSR_T_BIT, 78362306a36Sopenharmony_ci .cpsr_val = 0, 78462306a36Sopenharmony_ci .fn = vfp_support_entry, 78562306a36Sopenharmony_ci}, { 78662306a36Sopenharmony_ci .instr_mask = 0xff100000, 78762306a36Sopenharmony_ci .instr_val = 0xf4000000, 78862306a36Sopenharmony_ci .cpsr_mask = PSR_T_BIT, 78962306a36Sopenharmony_ci .cpsr_val = 0, 79062306a36Sopenharmony_ci .fn = vfp_support_entry, 79162306a36Sopenharmony_ci}, { 79262306a36Sopenharmony_ci .instr_mask = 0xef000000, 79362306a36Sopenharmony_ci .instr_val = 0xef000000, 79462306a36Sopenharmony_ci .cpsr_mask = PSR_T_BIT, 79562306a36Sopenharmony_ci .cpsr_val = PSR_T_BIT, 79662306a36Sopenharmony_ci .fn = vfp_support_entry, 79762306a36Sopenharmony_ci}, { 79862306a36Sopenharmony_ci .instr_mask = 0xff100000, 79962306a36Sopenharmony_ci .instr_val = 0xf9000000, 80062306a36Sopenharmony_ci .cpsr_mask = PSR_T_BIT, 80162306a36Sopenharmony_ci .cpsr_val = PSR_T_BIT, 80262306a36Sopenharmony_ci .fn = vfp_support_entry, 80362306a36Sopenharmony_ci}}; 80462306a36Sopenharmony_ci 80562306a36Sopenharmony_cistatic struct undef_hook vfp_support_hook = { 80662306a36Sopenharmony_ci .instr_mask = 0x0c000e00, 80762306a36Sopenharmony_ci .instr_val = 0x0c000a00, 80862306a36Sopenharmony_ci .fn = vfp_support_entry, 80962306a36Sopenharmony_ci}; 81062306a36Sopenharmony_ci 81162306a36Sopenharmony_ci#ifdef CONFIG_KERNEL_MODE_NEON 81262306a36Sopenharmony_ci 81362306a36Sopenharmony_ci/* 81462306a36Sopenharmony_ci * Kernel-side NEON support functions 81562306a36Sopenharmony_ci */ 81662306a36Sopenharmony_civoid kernel_neon_begin(void) 81762306a36Sopenharmony_ci{ 81862306a36Sopenharmony_ci struct thread_info *thread = current_thread_info(); 81962306a36Sopenharmony_ci unsigned int cpu; 82062306a36Sopenharmony_ci u32 fpexc; 82162306a36Sopenharmony_ci 82262306a36Sopenharmony_ci local_bh_disable(); 82362306a36Sopenharmony_ci 82462306a36Sopenharmony_ci /* 82562306a36Sopenharmony_ci * Kernel mode NEON is only allowed outside of hardirq context with 82662306a36Sopenharmony_ci * preemption and softirq processing disabled. This will make sure that 82762306a36Sopenharmony_ci * the kernel mode NEON register contents never need to be preserved. 82862306a36Sopenharmony_ci */ 82962306a36Sopenharmony_ci BUG_ON(in_hardirq()); 83062306a36Sopenharmony_ci cpu = __smp_processor_id(); 83162306a36Sopenharmony_ci 83262306a36Sopenharmony_ci fpexc = fmrx(FPEXC) | FPEXC_EN; 83362306a36Sopenharmony_ci fmxr(FPEXC, fpexc); 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_ci /* 83662306a36Sopenharmony_ci * Save the userland NEON/VFP state. Under UP, 83762306a36Sopenharmony_ci * the owner could be a task other than 'current' 83862306a36Sopenharmony_ci */ 83962306a36Sopenharmony_ci if (vfp_state_in_hw(cpu, thread)) 84062306a36Sopenharmony_ci vfp_save_state(&thread->vfpstate, fpexc); 84162306a36Sopenharmony_ci#ifndef CONFIG_SMP 84262306a36Sopenharmony_ci else if (vfp_current_hw_state[cpu] != NULL) 84362306a36Sopenharmony_ci vfp_save_state(vfp_current_hw_state[cpu], fpexc); 84462306a36Sopenharmony_ci#endif 84562306a36Sopenharmony_ci vfp_current_hw_state[cpu] = NULL; 84662306a36Sopenharmony_ci} 84762306a36Sopenharmony_ciEXPORT_SYMBOL(kernel_neon_begin); 84862306a36Sopenharmony_ci 84962306a36Sopenharmony_civoid kernel_neon_end(void) 85062306a36Sopenharmony_ci{ 85162306a36Sopenharmony_ci /* Disable the NEON/VFP unit. */ 85262306a36Sopenharmony_ci fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); 85362306a36Sopenharmony_ci local_bh_enable(); 85462306a36Sopenharmony_ci} 85562306a36Sopenharmony_ciEXPORT_SYMBOL(kernel_neon_end); 85662306a36Sopenharmony_ci 85762306a36Sopenharmony_ci#endif /* CONFIG_KERNEL_MODE_NEON */ 85862306a36Sopenharmony_ci 85962306a36Sopenharmony_cistatic int __init vfp_detect(struct pt_regs *regs, unsigned int instr) 86062306a36Sopenharmony_ci{ 86162306a36Sopenharmony_ci VFP_arch = UINT_MAX; /* mark as not present */ 86262306a36Sopenharmony_ci regs->ARM_pc += 4; 86362306a36Sopenharmony_ci return 0; 86462306a36Sopenharmony_ci} 86562306a36Sopenharmony_ci 86662306a36Sopenharmony_cistatic struct undef_hook vfp_detect_hook __initdata = { 86762306a36Sopenharmony_ci .instr_mask = 0x0c000e00, 86862306a36Sopenharmony_ci .instr_val = 0x0c000a00, 86962306a36Sopenharmony_ci .cpsr_mask = MODE_MASK, 87062306a36Sopenharmony_ci .cpsr_val = SVC_MODE, 87162306a36Sopenharmony_ci .fn = vfp_detect, 87262306a36Sopenharmony_ci}; 87362306a36Sopenharmony_ci 87462306a36Sopenharmony_ci/* 87562306a36Sopenharmony_ci * VFP support code initialisation. 87662306a36Sopenharmony_ci */ 87762306a36Sopenharmony_cistatic int __init vfp_init(void) 87862306a36Sopenharmony_ci{ 87962306a36Sopenharmony_ci unsigned int vfpsid; 88062306a36Sopenharmony_ci unsigned int cpu_arch = cpu_architecture(); 88162306a36Sopenharmony_ci unsigned int isar6; 88262306a36Sopenharmony_ci 88362306a36Sopenharmony_ci /* 88462306a36Sopenharmony_ci * Enable the access to the VFP on all online CPUs so the 88562306a36Sopenharmony_ci * following test on FPSID will succeed. 88662306a36Sopenharmony_ci */ 88762306a36Sopenharmony_ci if (cpu_arch >= CPU_ARCH_ARMv6) 88862306a36Sopenharmony_ci on_each_cpu(vfp_enable, NULL, 1); 88962306a36Sopenharmony_ci 89062306a36Sopenharmony_ci /* 89162306a36Sopenharmony_ci * First check that there is a VFP that we can use. 89262306a36Sopenharmony_ci * The handler is already setup to just log calls, so 89362306a36Sopenharmony_ci * we just need to read the VFPSID register. 89462306a36Sopenharmony_ci */ 89562306a36Sopenharmony_ci register_undef_hook(&vfp_detect_hook); 89662306a36Sopenharmony_ci barrier(); 89762306a36Sopenharmony_ci vfpsid = fmrx(FPSID); 89862306a36Sopenharmony_ci barrier(); 89962306a36Sopenharmony_ci unregister_undef_hook(&vfp_detect_hook); 90062306a36Sopenharmony_ci 90162306a36Sopenharmony_ci pr_info("VFP support v0.3: "); 90262306a36Sopenharmony_ci if (VFP_arch) { 90362306a36Sopenharmony_ci pr_cont("not present\n"); 90462306a36Sopenharmony_ci return 0; 90562306a36Sopenharmony_ci /* Extract the architecture on CPUID scheme */ 90662306a36Sopenharmony_ci } else if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) { 90762306a36Sopenharmony_ci VFP_arch = vfpsid & FPSID_CPUID_ARCH_MASK; 90862306a36Sopenharmony_ci VFP_arch >>= FPSID_ARCH_BIT; 90962306a36Sopenharmony_ci /* 91062306a36Sopenharmony_ci * Check for the presence of the Advanced SIMD 91162306a36Sopenharmony_ci * load/store instructions, integer and single 91262306a36Sopenharmony_ci * precision floating point operations. Only check 91362306a36Sopenharmony_ci * for NEON if the hardware has the MVFR registers. 91462306a36Sopenharmony_ci */ 91562306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_NEON) && 91662306a36Sopenharmony_ci (fmrx(MVFR1) & 0x000fff00) == 0x00011100) { 91762306a36Sopenharmony_ci elf_hwcap |= HWCAP_NEON; 91862306a36Sopenharmony_ci for (int i = 0; i < ARRAY_SIZE(neon_support_hook); i++) 91962306a36Sopenharmony_ci register_undef_hook(&neon_support_hook[i]); 92062306a36Sopenharmony_ci } 92162306a36Sopenharmony_ci 92262306a36Sopenharmony_ci if (IS_ENABLED(CONFIG_VFPv3)) { 92362306a36Sopenharmony_ci u32 mvfr0 = fmrx(MVFR0); 92462306a36Sopenharmony_ci if (((mvfr0 & MVFR0_DP_MASK) >> MVFR0_DP_BIT) == 0x2 || 92562306a36Sopenharmony_ci ((mvfr0 & MVFR0_SP_MASK) >> MVFR0_SP_BIT) == 0x2) { 92662306a36Sopenharmony_ci elf_hwcap |= HWCAP_VFPv3; 92762306a36Sopenharmony_ci /* 92862306a36Sopenharmony_ci * Check for VFPv3 D16 and VFPv4 D16. CPUs in 92962306a36Sopenharmony_ci * this configuration only have 16 x 64bit 93062306a36Sopenharmony_ci * registers. 93162306a36Sopenharmony_ci */ 93262306a36Sopenharmony_ci if ((mvfr0 & MVFR0_A_SIMD_MASK) == 1) 93362306a36Sopenharmony_ci /* also v4-D16 */ 93462306a36Sopenharmony_ci elf_hwcap |= HWCAP_VFPv3D16; 93562306a36Sopenharmony_ci else 93662306a36Sopenharmony_ci elf_hwcap |= HWCAP_VFPD32; 93762306a36Sopenharmony_ci } 93862306a36Sopenharmony_ci 93962306a36Sopenharmony_ci if ((fmrx(MVFR1) & 0xf0000000) == 0x10000000) 94062306a36Sopenharmony_ci elf_hwcap |= HWCAP_VFPv4; 94162306a36Sopenharmony_ci if (((fmrx(MVFR1) & MVFR1_ASIMDHP_MASK) >> MVFR1_ASIMDHP_BIT) == 0x2) 94262306a36Sopenharmony_ci elf_hwcap |= HWCAP_ASIMDHP; 94362306a36Sopenharmony_ci if (((fmrx(MVFR1) & MVFR1_FPHP_MASK) >> MVFR1_FPHP_BIT) == 0x3) 94462306a36Sopenharmony_ci elf_hwcap |= HWCAP_FPHP; 94562306a36Sopenharmony_ci } 94662306a36Sopenharmony_ci 94762306a36Sopenharmony_ci /* 94862306a36Sopenharmony_ci * Check for the presence of Advanced SIMD Dot Product 94962306a36Sopenharmony_ci * instructions. 95062306a36Sopenharmony_ci */ 95162306a36Sopenharmony_ci isar6 = read_cpuid_ext(CPUID_EXT_ISAR6); 95262306a36Sopenharmony_ci if (cpuid_feature_extract_field(isar6, 4) == 0x1) 95362306a36Sopenharmony_ci elf_hwcap |= HWCAP_ASIMDDP; 95462306a36Sopenharmony_ci /* 95562306a36Sopenharmony_ci * Check for the presence of Advanced SIMD Floating point 95662306a36Sopenharmony_ci * half-precision multiplication instructions. 95762306a36Sopenharmony_ci */ 95862306a36Sopenharmony_ci if (cpuid_feature_extract_field(isar6, 8) == 0x1) 95962306a36Sopenharmony_ci elf_hwcap |= HWCAP_ASIMDFHM; 96062306a36Sopenharmony_ci /* 96162306a36Sopenharmony_ci * Check for the presence of Advanced SIMD Bfloat16 96262306a36Sopenharmony_ci * floating point instructions. 96362306a36Sopenharmony_ci */ 96462306a36Sopenharmony_ci if (cpuid_feature_extract_field(isar6, 20) == 0x1) 96562306a36Sopenharmony_ci elf_hwcap |= HWCAP_ASIMDBF16; 96662306a36Sopenharmony_ci /* 96762306a36Sopenharmony_ci * Check for the presence of Advanced SIMD and floating point 96862306a36Sopenharmony_ci * Int8 matrix multiplication instructions instructions. 96962306a36Sopenharmony_ci */ 97062306a36Sopenharmony_ci if (cpuid_feature_extract_field(isar6, 24) == 0x1) 97162306a36Sopenharmony_ci elf_hwcap |= HWCAP_I8MM; 97262306a36Sopenharmony_ci 97362306a36Sopenharmony_ci /* Extract the architecture version on pre-cpuid scheme */ 97462306a36Sopenharmony_ci } else { 97562306a36Sopenharmony_ci if (vfpsid & FPSID_NODOUBLE) { 97662306a36Sopenharmony_ci pr_cont("no double precision support\n"); 97762306a36Sopenharmony_ci return 0; 97862306a36Sopenharmony_ci } 97962306a36Sopenharmony_ci 98062306a36Sopenharmony_ci VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; 98162306a36Sopenharmony_ci } 98262306a36Sopenharmony_ci 98362306a36Sopenharmony_ci cpuhp_setup_state_nocalls(CPUHP_AP_ARM_VFP_STARTING, 98462306a36Sopenharmony_ci "arm/vfp:starting", vfp_starting_cpu, 98562306a36Sopenharmony_ci vfp_dying_cpu); 98662306a36Sopenharmony_ci 98762306a36Sopenharmony_ci have_vfp = true; 98862306a36Sopenharmony_ci 98962306a36Sopenharmony_ci register_undef_hook(&vfp_support_hook); 99062306a36Sopenharmony_ci thread_register_notifier(&vfp_notifier_block); 99162306a36Sopenharmony_ci vfp_pm_init(); 99262306a36Sopenharmony_ci 99362306a36Sopenharmony_ci /* 99462306a36Sopenharmony_ci * We detected VFP, and the support code is 99562306a36Sopenharmony_ci * in place; report VFP support to userspace. 99662306a36Sopenharmony_ci */ 99762306a36Sopenharmony_ci elf_hwcap |= HWCAP_VFP; 99862306a36Sopenharmony_ci 99962306a36Sopenharmony_ci pr_cont("implementor %02x architecture %d part %02x variant %x rev %x\n", 100062306a36Sopenharmony_ci (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT, 100162306a36Sopenharmony_ci VFP_arch, 100262306a36Sopenharmony_ci (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT, 100362306a36Sopenharmony_ci (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT, 100462306a36Sopenharmony_ci (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT); 100562306a36Sopenharmony_ci 100662306a36Sopenharmony_ci return 0; 100762306a36Sopenharmony_ci} 100862306a36Sopenharmony_ci 100962306a36Sopenharmony_cicore_initcall(vfp_init); 1010