vfp.c revision 283366
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 283366 2015-05-24 12:20:11Z andrew $"); 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> 41239268Sgonzo#include <machine/pcb.h> 42239268Sgonzo#include <machine/undefined.h> 43239268Sgonzo#include <machine/vfp.h> 44239268Sgonzo 45239268Sgonzo/* function prototypes */ 46261563Sandrewstatic int vfp_bounce(u_int, u_int, struct trapframe *, int); 47261563Sandrewstatic void vfp_restore(struct vfp_state *); 48239268Sgonzo 49249176Sandrewextern int vfp_exists; 50239268Sgonzostatic struct undefined_handler vfp10_uh, vfp11_uh; 51251712Sandrew/* If true the VFP unit has 32 double registers, otherwise it has 16 */ 52251712Sandrewstatic int is_d32; 53239268Sgonzo 54276518Sian/* 55276518Sian * About .fpu directives in this file... 56276518Sian * 57276518Sian * We should need simply .fpu vfpv3, but clang 3.5 has a quirk where setting 58276518Sian * vfpv3 doesn't imply that vfp2 features are also available -- both have to be 59276518Sian * explicitly set to get all the features of both. This is probably a bug in 60276518Sian * clang, so it may get fixed and require changes here some day. Other changes 61276518Sian * are probably coming in clang too, because there is email and open PRs 62276518Sian * indicating they want to completely disable the ability to use .fpu and 63276518Sian * similar directives in inline asm. That would be catastrophic for us, 64276518Sian * hopefully they come to their senses. There was also some discusion of a new 65276518Sian * syntax such as .push fpu=vfpv3; ...; .pop fpu; and that would be ideal for 66276518Sian * us, better than what we have now really. 67276518Sian * 68276518Sian * For gcc, each .fpu directive completely overrides the prior directive, unlike 69276518Sian * with clang, but luckily on gcc saying v3 implies all the v2 features as well. 70276518Sian */ 71276518Sian 72239268Sgonzo#define fmxr(reg, val) \ 73276518Sian __asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n" \ 74276518Sian " vmsr " __STRING(reg) ", %0" :: "r"(val)); 75239268Sgonzo 76239268Sgonzo#define fmrx(reg) \ 77239268Sgonzo({ u_int val = 0;\ 78276518Sian __asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n" \ 79276518Sian " vmrs %0, " __STRING(reg) : "=r"(val)); \ 80251712Sandrew val; \ 81239268Sgonzo}) 82239268Sgonzo 83262941Sianstatic u_int 84239268Sgonzoget_coprocessorACR(void) 85239268Sgonzo{ 86239268Sgonzo u_int val; 87239268Sgonzo __asm __volatile("mrc p15, 0, %0, c1, c0, 2" : "=r" (val) : : "cc"); 88239268Sgonzo return val; 89239268Sgonzo} 90239268Sgonzo 91262941Sianstatic void 92239268Sgonzoset_coprocessorACR(u_int val) 93239268Sgonzo{ 94239268Sgonzo __asm __volatile("mcr p15, 0, %0, c1, c0, 2\n\t" 95239268Sgonzo : : "r" (val) : "cc"); 96247340Scognet isb(); 97239268Sgonzo} 98239268Sgonzo 99239268Sgonzo 100239268Sgonzo /* called for each cpu */ 101239268Sgonzovoid 102239268Sgonzovfp_init(void) 103239268Sgonzo{ 104239268Sgonzo u_int fpsid, fpexc, tmp; 105251712Sandrew u_int coproc, vfp_arch; 106239268Sgonzo 107239268Sgonzo coproc = get_coprocessorACR(); 108239268Sgonzo coproc |= COPROC10 | COPROC11; 109239268Sgonzo set_coprocessorACR(coproc); 110283366Sandrew 111276518Sian fpsid = fmrx(fpsid); /* read the vfp system id */ 112276518Sian fpexc = fmrx(fpexc); /* read the vfp exception reg */ 113239268Sgonzo 114239268Sgonzo if (!(fpsid & VFPSID_HARDSOFT_IMP)) { 115239268Sgonzo vfp_exists = 1; 116251712Sandrew is_d32 = 0; 117276518Sian PCPU_SET(vfpsid, fpsid); /* save the fpsid */ 118251712Sandrew 119251712Sandrew vfp_arch = 120251712Sandrew (fpsid & VFPSID_SUBVERSION2_MASK) >> VFPSID_SUBVERSION_OFF; 121251712Sandrew 122251712Sandrew if (vfp_arch >= VFP_ARCH3) { 123276518Sian tmp = fmrx(mvfr0); 124239268Sgonzo PCPU_SET(vfpmvfr0, tmp); 125251712Sandrew 126251712Sandrew if ((tmp & VMVFR0_RB_MASK) == 2) 127251712Sandrew is_d32 = 1; 128251712Sandrew 129276518Sian tmp = fmrx(mvfr1); 130239268Sgonzo PCPU_SET(vfpmvfr1, tmp); 131239268Sgonzo } 132251712Sandrew 133239268Sgonzo /* initialize the coprocess 10 and 11 calls 134239268Sgonzo * These are called to restore the registers and enable 135239268Sgonzo * the VFP hardware. 136239268Sgonzo */ 137239268Sgonzo if (vfp10_uh.uh_handler == NULL) { 138239268Sgonzo vfp10_uh.uh_handler = vfp_bounce; 139239268Sgonzo vfp11_uh.uh_handler = vfp_bounce; 140239268Sgonzo install_coproc_handler_static(10, &vfp10_uh); 141239268Sgonzo install_coproc_handler_static(11, &vfp11_uh); 142239268Sgonzo } 143239268Sgonzo } 144239268Sgonzo} 145239268Sgonzo 146239268SgonzoSYSINIT(vfp, SI_SUB_CPU, SI_ORDER_ANY, vfp_init, NULL); 147239268Sgonzo 148239268Sgonzo 149239268Sgonzo/* start VFP unit, restore the vfp registers from the PCB and retry 150239268Sgonzo * the instruction 151239268Sgonzo */ 152261563Sandrewstatic int 153239268Sgonzovfp_bounce(u_int addr, u_int insn, struct trapframe *frame, int code) 154239268Sgonzo{ 155262941Sian u_int cpu, fpexc; 156239268Sgonzo struct pcb *curpcb; 157263914Sandrew ksiginfo_t ksi; 158239268Sgonzo 159262941Sian if ((code & FAULT_USER) == 0) 160262941Sian panic("undefined floating point instruction in supervisor mode"); 161262941Sian 162262941Sian critical_enter(); 163262941Sian 164262941Sian /* 165262941Sian * If the VFP is already on and we got an undefined instruction, then 166262941Sian * something tried to executate a truly invalid instruction that maps to 167262941Sian * the VFP. 168262941Sian */ 169276518Sian fpexc = fmrx(fpexc); 170239268Sgonzo if (fpexc & VFPEXC_EN) { 171263914Sandrew /* Clear any exceptions */ 172276518Sian fmxr(fpexc, fpexc & ~(VFPEXC_EX | VFPEXC_FP2V)); 173263914Sandrew 174262941Sian /* kill the process - we do not handle emulation */ 175262941Sian critical_exit(); 176263914Sandrew 177263914Sandrew if (fpexc & VFPEXC_EX) { 178263914Sandrew /* We have an exception, signal a SIGFPE */ 179263914Sandrew ksiginfo_init_trap(&ksi); 180263914Sandrew ksi.ksi_signo = SIGFPE; 181263914Sandrew if (fpexc & VFPEXC_UFC) 182263914Sandrew ksi.ksi_code = FPE_FLTUND; 183263914Sandrew else if (fpexc & VFPEXC_OFC) 184263914Sandrew ksi.ksi_code = FPE_FLTOVF; 185263914Sandrew else if (fpexc & VFPEXC_IOC) 186263914Sandrew ksi.ksi_code = FPE_FLTINV; 187263914Sandrew ksi.ksi_addr = (void *)addr; 188263914Sandrew trapsignal(curthread, &ksi); 189263914Sandrew return 0; 190263914Sandrew } 191263914Sandrew 192262941Sian return 1; 193262941Sian } 194239268Sgonzo 195262941Sian /* 196262941Sian * If the last time this thread used the VFP it was on this core, and 197262941Sian * the last thread to use the VFP on this core was this thread, then the 198262941Sian * VFP state is valid, otherwise restore this thread's state to the VFP. 199262941Sian */ 200276518Sian fmxr(fpexc, fpexc | VFPEXC_EN); 201262941Sian curpcb = curthread->td_pcb; 202262941Sian cpu = PCPU_GET(cpu); 203262941Sian if (curpcb->pcb_vfpcpu != cpu || curthread != PCPU_GET(fpcurthread)) { 204262941Sian vfp_restore(&curpcb->pcb_vfpstate); 205262941Sian curpcb->pcb_vfpcpu = cpu; 206262941Sian PCPU_SET(fpcurthread, curthread); 207239268Sgonzo } 208262941Sian 209262941Sian critical_exit(); 210262941Sian return (0); 211239268Sgonzo} 212239268Sgonzo 213262941Sian/* 214262941Sian * Restore the given state to the VFP hardware. 215239268Sgonzo */ 216261563Sandrewstatic void 217239268Sgonzovfp_restore(struct vfp_state *vfpsave) 218239268Sgonzo{ 219263914Sandrew uint32_t fpexc; 220239268Sgonzo 221276518Sian /* On vfpv3 we may need to restore FPINST and FPINST2 */ 222263914Sandrew fpexc = vfpsave->fpexec; 223263914Sandrew if (fpexc & VFPEXC_EX) { 224276518Sian fmxr(fpinst, vfpsave->fpinst); 225263914Sandrew if (fpexc & VFPEXC_FP2V) 226276518Sian fmxr(fpinst2, vfpsave->fpinst2); 227263914Sandrew } 228276518Sian fmxr(fpscr, vfpsave->fpscr); 229263914Sandrew 230276518Sian __asm __volatile( 231276518Sian " .fpu vfpv2\n" 232276518Sian " .fpu vfpv3\n" 233276518Sian " vldmia %0!, {d0-d15}\n" /* d0-d15 */ 234276518Sian " cmp %1, #0\n" /* -D16 or -D32? */ 235276518Sian " vldmiane %0!, {d16-d31}\n" /* d16-d31 */ 236276518Sian " addeq %0, %0, #128\n" /* skip missing regs */ 237276518Sian : "+&r" (vfpsave) : "r" (is_d32) : "cc" 238276518Sian ); 239263914Sandrew 240276518Sian fmxr(fpexc, fpexc); 241239268Sgonzo} 242239268Sgonzo 243262941Sian/* 244262941Sian * If the VFP is on, save its current state and turn it off if requested to do 245262941Sian * so. If the VFP is not on, does not change the values at *vfpsave. Caller is 246262941Sian * responsible for preventing a context switch while this is running. 247239268Sgonzo */ 248239268Sgonzovoid 249262941Sianvfp_store(struct vfp_state *vfpsave, boolean_t disable_vfp) 250239268Sgonzo{ 251263914Sandrew uint32_t fpexc; 252239268Sgonzo 253276518Sian fpexc = fmrx(fpexc); /* Is the vfp enabled? */ 254263914Sandrew if (fpexc & VFPEXC_EN) { 255263914Sandrew vfpsave->fpexec = fpexc; 256276518Sian vfpsave->fpscr = fmrx(fpscr); 257263914Sandrew 258276518Sian /* On vfpv3 we may need to save FPINST and FPINST2 */ 259263914Sandrew if (fpexc & VFPEXC_EX) { 260276518Sian vfpsave->fpinst = fmrx(fpinst); 261263914Sandrew if (fpexc & VFPEXC_FP2V) 262276518Sian vfpsave->fpinst2 = fmrx(fpinst2); 263263914Sandrew fpexc &= ~VFPEXC_EX; 264263914Sandrew } 265263914Sandrew 266262941Sian __asm __volatile( 267276518Sian " .fpu vfpv2\n" 268276518Sian " .fpu vfpv3\n" 269276518Sian " vstmia %0!, {d0-d15}\n" /* d0-d15 */ 270276518Sian " cmp %1, #0\n" /* -D16 or -D32? */ 271276518Sian " vstmiane r0!, {d16-d31}\n" /* d16-d31 */ 272276518Sian " addeq %0, %0, #128\n" /* skip missing regs */ 273276518Sian : "+&r" (vfpsave) : "r" (is_d32) : "cc" 274276518Sian ); 275263914Sandrew 276262941Sian if (disable_vfp) 277276518Sian fmxr(fpexc , fpexc & ~VFPEXC_EN); 278239268Sgonzo } 279239268Sgonzo} 280239268Sgonzo 281262941Sian/* 282262948Sian * The current thread is dying. If the state currently in the hardware belongs 283262948Sian * to the current thread, set fpcurthread to NULL to indicate that the VFP 284262948Sian * hardware state does not belong to any thread. If the VFP is on, turn it off. 285262948Sian * Called only from cpu_throw(), so we don't have to worry about a context 286262948Sian * switch here. 287239268Sgonzo */ 288239268Sgonzovoid 289262948Sianvfp_discard(struct thread *td) 290239268Sgonzo{ 291262941Sian u_int tmp; 292239268Sgonzo 293262948Sian if (PCPU_GET(fpcurthread) == td) 294262948Sian PCPU_SET(fpcurthread, NULL); 295262948Sian 296276518Sian tmp = fmrx(fpexc); 297262948Sian if (tmp & VFPEXC_EN) 298276518Sian fmxr(fpexc, tmp & ~VFPEXC_EN); 299239268Sgonzo} 300239268Sgonzo 301254461Sandrew#endif 302254461Sandrew 303