vfp.c revision 288983
1262941Sian/*- 2262941Sian * Copyright (c) 2014 Ian Lepore <ian@freebsd.org> 3239268Sgonzo * Copyright (c) 2012 Mark Tinguely 4239268Sgonzo * 5239268Sgonzo * All rights reserved. 6239268Sgonzo * 7239268Sgonzo * Redistribution and use in source and binary forms, with or without 8239268Sgonzo * modification, are permitted provided that the following conditions 9239268Sgonzo * are met: 10239268Sgonzo * 1. Redistributions of source code must retain the above copyright 11239268Sgonzo * notice, this list of conditions and the following disclaimer. 12239268Sgonzo * 2. Redistributions in binary form must reproduce the above copyright 13239268Sgonzo * notice, this list of conditions and the following disclaimer in the 14239268Sgonzo * documentation and/or other materials provided with the distribution. 15239268Sgonzo * 16239268Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17239268Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18239268Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19239268Sgonzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20239268Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21239268Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22239268Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23239268Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24239268Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25239268Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26239268Sgonzo * SUCH DAMAGE. 27239268Sgonzo */ 28254461Sandrew 29239268Sgonzo#include <sys/cdefs.h> 30239268Sgonzo__FBSDID("$FreeBSD: head/sys/arm/arm/vfp.c 288983 2015-10-07 09:12:49Z kib $"); 31239268Sgonzo 32254461Sandrew#ifdef VFP 33239268Sgonzo#include <sys/param.h> 34239268Sgonzo#include <sys/systm.h> 35239268Sgonzo#include <sys/proc.h> 36239268Sgonzo#include <sys/kernel.h> 37239268Sgonzo 38262941Sian#include <machine/armreg.h> 39257200Sian#include <machine/frame.h> 40239268Sgonzo#include <machine/fp.h> 41288983Skib#include <machine/md_var.h> 42239268Sgonzo#include <machine/pcb.h> 43239268Sgonzo#include <machine/undefined.h> 44239268Sgonzo#include <machine/vfp.h> 45239268Sgonzo 46239268Sgonzo/* function prototypes */ 47261563Sandrewstatic int vfp_bounce(u_int, u_int, struct trapframe *, int); 48261563Sandrewstatic void vfp_restore(struct vfp_state *); 49239268Sgonzo 50249176Sandrewextern int vfp_exists; 51239268Sgonzostatic struct undefined_handler vfp10_uh, vfp11_uh; 52251712Sandrew/* If true the VFP unit has 32 double registers, otherwise it has 16 */ 53251712Sandrewstatic int is_d32; 54239268Sgonzo 55276518Sian/* 56276518Sian * About .fpu directives in this file... 57276518Sian * 58276518Sian * We should need simply .fpu vfpv3, but clang 3.5 has a quirk where setting 59276518Sian * vfpv3 doesn't imply that vfp2 features are also available -- both have to be 60276518Sian * explicitly set to get all the features of both. This is probably a bug in 61276518Sian * clang, so it may get fixed and require changes here some day. Other changes 62276518Sian * are probably coming in clang too, because there is email and open PRs 63276518Sian * indicating they want to completely disable the ability to use .fpu and 64276518Sian * similar directives in inline asm. That would be catastrophic for us, 65276518Sian * hopefully they come to their senses. There was also some discusion of a new 66276518Sian * syntax such as .push fpu=vfpv3; ...; .pop fpu; and that would be ideal for 67276518Sian * us, better than what we have now really. 68276518Sian * 69276518Sian * For gcc, each .fpu directive completely overrides the prior directive, unlike 70276518Sian * with clang, but luckily on gcc saying v3 implies all the v2 features as well. 71276518Sian */ 72276518Sian 73239268Sgonzo#define fmxr(reg, val) \ 74276518Sian __asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n" \ 75276518Sian " vmsr " __STRING(reg) ", %0" :: "r"(val)); 76239268Sgonzo 77239268Sgonzo#define fmrx(reg) \ 78239268Sgonzo({ u_int val = 0;\ 79276518Sian __asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n" \ 80276518Sian " vmrs %0, " __STRING(reg) : "=r"(val)); \ 81251712Sandrew val; \ 82239268Sgonzo}) 83239268Sgonzo 84262941Sianstatic u_int 85239268Sgonzoget_coprocessorACR(void) 86239268Sgonzo{ 87239268Sgonzo u_int val; 88239268Sgonzo __asm __volatile("mrc p15, 0, %0, c1, c0, 2" : "=r" (val) : : "cc"); 89239268Sgonzo return val; 90239268Sgonzo} 91239268Sgonzo 92262941Sianstatic void 93239268Sgonzoset_coprocessorACR(u_int val) 94239268Sgonzo{ 95239268Sgonzo __asm __volatile("mcr p15, 0, %0, c1, c0, 2\n\t" 96239268Sgonzo : : "r" (val) : "cc"); 97247340Scognet isb(); 98239268Sgonzo} 99239268Sgonzo 100239268Sgonzo 101239268Sgonzo /* called for each cpu */ 102239268Sgonzovoid 103239268Sgonzovfp_init(void) 104239268Sgonzo{ 105239268Sgonzo u_int fpsid, fpexc, tmp; 106251712Sandrew u_int coproc, vfp_arch; 107239268Sgonzo 108239268Sgonzo coproc = get_coprocessorACR(); 109239268Sgonzo coproc |= COPROC10 | COPROC11; 110239268Sgonzo set_coprocessorACR(coproc); 111283366Sandrew 112276518Sian fpsid = fmrx(fpsid); /* read the vfp system id */ 113276518Sian fpexc = fmrx(fpexc); /* read the vfp exception reg */ 114239268Sgonzo 115239268Sgonzo if (!(fpsid & VFPSID_HARDSOFT_IMP)) { 116239268Sgonzo vfp_exists = 1; 117251712Sandrew is_d32 = 0; 118276518Sian PCPU_SET(vfpsid, fpsid); /* save the fpsid */ 119251712Sandrew 120251712Sandrew vfp_arch = 121251712Sandrew (fpsid & VFPSID_SUBVERSION2_MASK) >> VFPSID_SUBVERSION_OFF; 122251712Sandrew 123251712Sandrew if (vfp_arch >= VFP_ARCH3) { 124276518Sian tmp = fmrx(mvfr0); 125239268Sgonzo PCPU_SET(vfpmvfr0, tmp); 126251712Sandrew 127251712Sandrew if ((tmp & VMVFR0_RB_MASK) == 2) 128251712Sandrew is_d32 = 1; 129251712Sandrew 130276518Sian tmp = fmrx(mvfr1); 131239268Sgonzo PCPU_SET(vfpmvfr1, tmp); 132288983Skib 133288983Skib if (PCPU_GET(cpuid) == 0) { 134288983Skib if ((tmp & VMVFR1_FZ_MASK) == 0x1) { 135288983Skib /* Denormals arithmetic support */ 136288983Skib initial_fpscr &= ~VFPSCR_FZ; 137288983Skib thread0.td_pcb->pcb_vfpstate.fpscr = 138288983Skib initial_fpscr; 139288983Skib } 140288983Skib } 141239268Sgonzo } 142251712Sandrew 143239268Sgonzo /* initialize the coprocess 10 and 11 calls 144239268Sgonzo * These are called to restore the registers and enable 145239268Sgonzo * the VFP hardware. 146239268Sgonzo */ 147239268Sgonzo if (vfp10_uh.uh_handler == NULL) { 148239268Sgonzo vfp10_uh.uh_handler = vfp_bounce; 149239268Sgonzo vfp11_uh.uh_handler = vfp_bounce; 150239268Sgonzo install_coproc_handler_static(10, &vfp10_uh); 151239268Sgonzo install_coproc_handler_static(11, &vfp11_uh); 152239268Sgonzo } 153239268Sgonzo } 154239268Sgonzo} 155239268Sgonzo 156239268SgonzoSYSINIT(vfp, SI_SUB_CPU, SI_ORDER_ANY, vfp_init, NULL); 157239268Sgonzo 158239268Sgonzo 159239268Sgonzo/* start VFP unit, restore the vfp registers from the PCB and retry 160239268Sgonzo * the instruction 161239268Sgonzo */ 162261563Sandrewstatic int 163239268Sgonzovfp_bounce(u_int addr, u_int insn, struct trapframe *frame, int code) 164239268Sgonzo{ 165262941Sian u_int cpu, fpexc; 166239268Sgonzo struct pcb *curpcb; 167263914Sandrew ksiginfo_t ksi; 168239268Sgonzo 169262941Sian if ((code & FAULT_USER) == 0) 170262941Sian panic("undefined floating point instruction in supervisor mode"); 171262941Sian 172262941Sian critical_enter(); 173262941Sian 174262941Sian /* 175262941Sian * If the VFP is already on and we got an undefined instruction, then 176262941Sian * something tried to executate a truly invalid instruction that maps to 177262941Sian * the VFP. 178262941Sian */ 179276518Sian fpexc = fmrx(fpexc); 180239268Sgonzo if (fpexc & VFPEXC_EN) { 181263914Sandrew /* Clear any exceptions */ 182276518Sian fmxr(fpexc, fpexc & ~(VFPEXC_EX | VFPEXC_FP2V)); 183263914Sandrew 184262941Sian /* kill the process - we do not handle emulation */ 185262941Sian critical_exit(); 186263914Sandrew 187263914Sandrew if (fpexc & VFPEXC_EX) { 188263914Sandrew /* We have an exception, signal a SIGFPE */ 189263914Sandrew ksiginfo_init_trap(&ksi); 190263914Sandrew ksi.ksi_signo = SIGFPE; 191263914Sandrew if (fpexc & VFPEXC_UFC) 192263914Sandrew ksi.ksi_code = FPE_FLTUND; 193263914Sandrew else if (fpexc & VFPEXC_OFC) 194263914Sandrew ksi.ksi_code = FPE_FLTOVF; 195263914Sandrew else if (fpexc & VFPEXC_IOC) 196263914Sandrew ksi.ksi_code = FPE_FLTINV; 197263914Sandrew ksi.ksi_addr = (void *)addr; 198263914Sandrew trapsignal(curthread, &ksi); 199263914Sandrew return 0; 200263914Sandrew } 201263914Sandrew 202262941Sian return 1; 203262941Sian } 204239268Sgonzo 205262941Sian /* 206262941Sian * If the last time this thread used the VFP it was on this core, and 207262941Sian * the last thread to use the VFP on this core was this thread, then the 208262941Sian * VFP state is valid, otherwise restore this thread's state to the VFP. 209262941Sian */ 210276518Sian fmxr(fpexc, fpexc | VFPEXC_EN); 211262941Sian curpcb = curthread->td_pcb; 212284109Sandrew cpu = PCPU_GET(cpuid); 213262941Sian if (curpcb->pcb_vfpcpu != cpu || curthread != PCPU_GET(fpcurthread)) { 214262941Sian vfp_restore(&curpcb->pcb_vfpstate); 215262941Sian curpcb->pcb_vfpcpu = cpu; 216262941Sian PCPU_SET(fpcurthread, curthread); 217239268Sgonzo } 218262941Sian 219262941Sian critical_exit(); 220262941Sian return (0); 221239268Sgonzo} 222239268Sgonzo 223262941Sian/* 224262941Sian * Restore the given state to the VFP hardware. 225239268Sgonzo */ 226261563Sandrewstatic void 227239268Sgonzovfp_restore(struct vfp_state *vfpsave) 228239268Sgonzo{ 229263914Sandrew uint32_t fpexc; 230239268Sgonzo 231276518Sian /* On vfpv3 we may need to restore FPINST and FPINST2 */ 232263914Sandrew fpexc = vfpsave->fpexec; 233263914Sandrew if (fpexc & VFPEXC_EX) { 234276518Sian fmxr(fpinst, vfpsave->fpinst); 235263914Sandrew if (fpexc & VFPEXC_FP2V) 236276518Sian fmxr(fpinst2, vfpsave->fpinst2); 237263914Sandrew } 238276518Sian fmxr(fpscr, vfpsave->fpscr); 239263914Sandrew 240276518Sian __asm __volatile( 241276518Sian " .fpu vfpv2\n" 242276518Sian " .fpu vfpv3\n" 243276518Sian " vldmia %0!, {d0-d15}\n" /* d0-d15 */ 244276518Sian " cmp %1, #0\n" /* -D16 or -D32? */ 245276518Sian " vldmiane %0!, {d16-d31}\n" /* d16-d31 */ 246276518Sian " addeq %0, %0, #128\n" /* skip missing regs */ 247276518Sian : "+&r" (vfpsave) : "r" (is_d32) : "cc" 248276518Sian ); 249263914Sandrew 250276518Sian fmxr(fpexc, fpexc); 251239268Sgonzo} 252239268Sgonzo 253262941Sian/* 254262941Sian * If the VFP is on, save its current state and turn it off if requested to do 255262941Sian * so. If the VFP is not on, does not change the values at *vfpsave. Caller is 256262941Sian * responsible for preventing a context switch while this is running. 257239268Sgonzo */ 258239268Sgonzovoid 259262941Sianvfp_store(struct vfp_state *vfpsave, boolean_t disable_vfp) 260239268Sgonzo{ 261263914Sandrew uint32_t fpexc; 262239268Sgonzo 263276518Sian fpexc = fmrx(fpexc); /* Is the vfp enabled? */ 264263914Sandrew if (fpexc & VFPEXC_EN) { 265263914Sandrew vfpsave->fpexec = fpexc; 266276518Sian vfpsave->fpscr = fmrx(fpscr); 267263914Sandrew 268276518Sian /* On vfpv3 we may need to save FPINST and FPINST2 */ 269263914Sandrew if (fpexc & VFPEXC_EX) { 270276518Sian vfpsave->fpinst = fmrx(fpinst); 271263914Sandrew if (fpexc & VFPEXC_FP2V) 272276518Sian vfpsave->fpinst2 = fmrx(fpinst2); 273263914Sandrew fpexc &= ~VFPEXC_EX; 274263914Sandrew } 275263914Sandrew 276262941Sian __asm __volatile( 277276518Sian " .fpu vfpv2\n" 278276518Sian " .fpu vfpv3\n" 279276518Sian " vstmia %0!, {d0-d15}\n" /* d0-d15 */ 280276518Sian " cmp %1, #0\n" /* -D16 or -D32? */ 281276518Sian " vstmiane r0!, {d16-d31}\n" /* d16-d31 */ 282276518Sian " addeq %0, %0, #128\n" /* skip missing regs */ 283276518Sian : "+&r" (vfpsave) : "r" (is_d32) : "cc" 284276518Sian ); 285263914Sandrew 286262941Sian if (disable_vfp) 287276518Sian fmxr(fpexc , fpexc & ~VFPEXC_EN); 288239268Sgonzo } 289239268Sgonzo} 290239268Sgonzo 291262941Sian/* 292262948Sian * The current thread is dying. If the state currently in the hardware belongs 293262948Sian * to the current thread, set fpcurthread to NULL to indicate that the VFP 294262948Sian * hardware state does not belong to any thread. If the VFP is on, turn it off. 295262948Sian * Called only from cpu_throw(), so we don't have to worry about a context 296262948Sian * switch here. 297239268Sgonzo */ 298239268Sgonzovoid 299262948Sianvfp_discard(struct thread *td) 300239268Sgonzo{ 301262941Sian u_int tmp; 302239268Sgonzo 303262948Sian if (PCPU_GET(fpcurthread) == td) 304262948Sian PCPU_SET(fpcurthread, NULL); 305262948Sian 306276518Sian tmp = fmrx(fpexc); 307262948Sian if (tmp & VFPEXC_EN) 308276518Sian fmxr(fpexc, tmp & ~VFPEXC_EN); 309239268Sgonzo} 310239268Sgonzo 311254461Sandrew#endif 312254461Sandrew 313