Home | History | Annotate | Line # | Download | only in vfp
vfp_init.c revision 1.51
      1  1.51       chs /*      $NetBSD: vfp_init.c,v 1.51 2017/03/16 16:13:20 chs Exp $ */
      2   1.1  rearnsha 
      3   1.1  rearnsha /*
      4   1.1  rearnsha  * Copyright (c) 2008 ARM Ltd
      5   1.1  rearnsha  * All rights reserved.
      6   1.1  rearnsha  *
      7   1.1  rearnsha  * Redistribution and use in source and binary forms, with or without
      8   1.1  rearnsha  * modification, are permitted provided that the following conditions
      9   1.1  rearnsha  * are met:
     10   1.1  rearnsha  * 1. Redistributions of source code must retain the above copyright
     11   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer.
     12   1.1  rearnsha  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  rearnsha  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  rearnsha  *    documentation and/or other materials provided with the distribution.
     15   1.1  rearnsha  * 3. The name of the company may not be used to endorse or promote
     16   1.1  rearnsha  *    products derived from this software without specific prior written
     17   1.1  rearnsha  *    permission.
     18   1.1  rearnsha  *
     19   1.1  rearnsha  * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR
     20   1.1  rearnsha  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21   1.1  rearnsha  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1  rearnsha  * ARE DISCLAIMED.  IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY
     23   1.1  rearnsha  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1  rearnsha  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     25   1.1  rearnsha  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  rearnsha  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27   1.1  rearnsha  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28   1.1  rearnsha  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29   1.1  rearnsha  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.1  rearnsha  */
     31   1.1  rearnsha 
     32   1.1  rearnsha #include <sys/param.h>
     33   1.1  rearnsha #include <sys/types.h>
     34   1.1  rearnsha #include <sys/systm.h>
     35   1.1  rearnsha #include <sys/device.h>
     36   1.1  rearnsha #include <sys/proc.h>
     37   1.4      matt #include <sys/cpu.h>
     38   1.1  rearnsha 
     39  1.23      matt #include <arm/locore.h>
     40   1.5      matt #include <arm/pcb.h>
     41   1.1  rearnsha #include <arm/undefined.h>
     42   1.1  rearnsha #include <arm/vfpreg.h>
     43   1.8      matt #include <arm/mcontext.h>
     44   1.1  rearnsha 
     45  1.12      matt #include <uvm/uvm_extern.h>		/* for pmap.h */
     46  1.12      matt 
     47  1.11      matt #ifdef FPU_VFP
     48  1.11      matt 
     49  1.29      matt #ifdef CPU_CORTEX
     50  1.29      matt __asm(".fpu\tvfpv4");
     51  1.29      matt #else
     52  1.29      matt __asm(".fpu\tvfp");
     53  1.29      matt #endif
     54  1.29      matt 
     55   1.1  rearnsha /* FLDMD <X>, {d0-d15} */
     56  1.11      matt static inline void
     57  1.13      matt load_vfpregs_lo(const uint64_t *p)
     58  1.10      matt {
     59  1.29      matt 	__asm __volatile("vldmia %0, {d0-d15}" :: "r" (p) : "memory");
     60  1.10      matt }
     61  1.10      matt 
     62  1.10      matt /* FSTMD <X>, {d0-d15} */
     63  1.11      matt static inline void
     64  1.10      matt save_vfpregs_lo(uint64_t *p)
     65  1.10      matt {
     66  1.29      matt 	__asm __volatile("vstmia %0, {d0-d15}" :: "r" (p) : "memory");
     67  1.10      matt }
     68  1.10      matt 
     69  1.10      matt #ifdef CPU_CORTEX
     70  1.10      matt /* FLDMD <X>, {d16-d31} */
     71  1.11      matt static inline void
     72  1.13      matt load_vfpregs_hi(const uint64_t *p)
     73  1.10      matt {
     74  1.29      matt 	__asm __volatile("vldmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
     75  1.10      matt }
     76  1.10      matt 
     77  1.10      matt /* FLDMD <X>, {d16-d31} */
     78  1.11      matt static inline void
     79  1.10      matt save_vfpregs_hi(uint64_t *p)
     80  1.10      matt {
     81  1.29      matt 	__asm __volatile("vstmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
     82  1.10      matt }
     83  1.10      matt #endif
     84   1.1  rearnsha 
     85  1.13      matt static inline void
     86  1.13      matt load_vfpregs(const struct vfpreg *fregs)
     87  1.13      matt {
     88  1.13      matt 	load_vfpregs_lo(fregs->vfp_regs);
     89  1.13      matt #ifdef CPU_CORTEX
     90  1.13      matt #ifdef CPU_ARM11
     91  1.13      matt 	switch (curcpu()->ci_vfp_id) {
     92  1.13      matt 	case FPU_VFP_CORTEXA5:
     93  1.13      matt 	case FPU_VFP_CORTEXA7:
     94  1.13      matt 	case FPU_VFP_CORTEXA8:
     95  1.13      matt 	case FPU_VFP_CORTEXA9:
     96  1.20      matt 	case FPU_VFP_CORTEXA15:
     97  1.42       slp 	case FPU_VFP_CORTEXA15_QEMU:
     98  1.50     skrll 	case FPU_VFP_CORTEXA53:
     99  1.13      matt #endif
    100  1.13      matt 		load_vfpregs_hi(fregs->vfp_regs);
    101  1.13      matt #ifdef CPU_ARM11
    102  1.13      matt 		break;
    103  1.13      matt 	}
    104  1.13      matt #endif
    105  1.13      matt #endif
    106  1.13      matt }
    107  1.13      matt 
    108  1.13      matt static inline void
    109  1.13      matt save_vfpregs(struct vfpreg *fregs)
    110  1.13      matt {
    111  1.13      matt 	save_vfpregs_lo(fregs->vfp_regs);
    112  1.13      matt #ifdef CPU_CORTEX
    113  1.13      matt #ifdef CPU_ARM11
    114  1.13      matt 	switch (curcpu()->ci_vfp_id) {
    115  1.13      matt 	case FPU_VFP_CORTEXA5:
    116  1.13      matt 	case FPU_VFP_CORTEXA7:
    117  1.13      matt 	case FPU_VFP_CORTEXA8:
    118  1.13      matt 	case FPU_VFP_CORTEXA9:
    119  1.20      matt 	case FPU_VFP_CORTEXA15:
    120  1.42       slp 	case FPU_VFP_CORTEXA15_QEMU:
    121  1.50     skrll 	case FPU_VFP_CORTEXA53:
    122  1.13      matt #endif
    123  1.13      matt 		save_vfpregs_hi(fregs->vfp_regs);
    124  1.13      matt #ifdef CPU_ARM11
    125  1.13      matt 		break;
    126  1.13      matt 	}
    127  1.13      matt #endif
    128  1.13      matt #endif
    129  1.13      matt }
    130  1.13      matt 
    131   1.1  rearnsha /* The real handler for VFP bounces.  */
    132   1.1  rearnsha static int vfp_handler(u_int, u_int, trapframe_t *, int);
    133  1.13      matt #ifdef CPU_CORTEX
    134  1.13      matt static int neon_handler(u_int, u_int, trapframe_t *, int);
    135  1.13      matt #endif
    136   1.1  rearnsha 
    137  1.13      matt static void vfp_state_load(lwp_t *, u_int);
    138  1.39     rmind static void vfp_state_save(lwp_t *);
    139  1.39     rmind static void vfp_state_release(lwp_t *);
    140   1.4      matt 
    141   1.4      matt const pcu_ops_t arm_vfp_ops = {
    142   1.4      matt 	.pcu_id = PCU_FPU,
    143  1.13      matt 	.pcu_state_save = vfp_state_save,
    144   1.4      matt 	.pcu_state_load = vfp_state_load,
    145   1.4      matt 	.pcu_state_release = vfp_state_release,
    146   1.4      matt };
    147   1.1  rearnsha 
    148  1.34      matt /* determine what bits can be changed */
    149  1.34      matt uint32_t vfp_fpscr_changable = VFP_FPSCR_CSUM;
    150  1.34      matt /* default to run fast */
    151  1.34      matt uint32_t vfp_fpscr_default = (VFP_FPSCR_DN | VFP_FPSCR_FZ | VFP_FPSCR_RN);
    152  1.34      matt 
    153   1.1  rearnsha /*
    154   1.1  rearnsha  * Used to test for a VFP. The following function is installed as a coproc10
    155   1.1  rearnsha  * handler on the undefined instruction vector and then we issue a VFP
    156   1.1  rearnsha  * instruction. If undefined_test is non zero then the VFP did not handle
    157   1.1  rearnsha  * the instruction so must be absent, or disabled.
    158   1.1  rearnsha  */
    159   1.1  rearnsha 
    160   1.1  rearnsha static int undefined_test;
    161   1.1  rearnsha 
    162   1.1  rearnsha static int
    163   1.4      matt vfp_test(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    164   1.1  rearnsha {
    165   1.1  rearnsha 
    166   1.1  rearnsha 	frame->tf_pc += INSN_SIZE;
    167   1.1  rearnsha 	++undefined_test;
    168   1.4      matt 	return 0;
    169   1.4      matt }
    170   1.4      matt 
    171  1.35      matt #else
    172  1.35      matt /* determine what bits can be changed */
    173  1.35      matt uint32_t vfp_fpscr_changable = VFP_FPSCR_CSUM|VFP_FPSCR_ESUM|VFP_FPSCR_RMODE;
    174   1.4      matt #endif /* FPU_VFP */
    175   1.4      matt 
    176   1.4      matt static int
    177   1.4      matt vfp_fpscr_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    178   1.4      matt {
    179   1.4      matt 	struct lwp * const l = curlwp;
    180   1.4      matt 	const u_int regno = (insn >> 12) & 0xf;
    181   1.4      matt 	/*
    182   1.4      matt 	 * Only match move to/from the FPSCR register and we
    183   1.4      matt 	 * can't be using the SP,LR,PC as a source.
    184   1.4      matt 	 */
    185   1.4      matt 	if ((insn & 0xffef0fff) != 0xeee10a10 || regno > 12)
    186   1.4      matt 		return 1;
    187   1.4      matt 
    188   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    189   1.4      matt 
    190   1.4      matt #ifdef FPU_VFP
    191   1.4      matt 	/*
    192   1.4      matt 	 * If FPU is valid somewhere, let's just reenable VFP and
    193   1.4      matt 	 * retry the instruction (only safe thing to do since the
    194   1.4      matt 	 * pcb has a stale copy).
    195   1.4      matt 	 */
    196   1.4      matt 	if (pcb->pcb_vfp.vfp_fpexc & VFP_FPEXC_EN)
    197   1.4      matt 		return 1;
    198   1.4      matt 
    199  1.51       chs 	if (__predict_false(!vfp_used_p(l))) {
    200  1.35      matt 		pcb->pcb_vfp.vfp_fpscr = vfp_fpscr_default;
    201   1.4      matt 	}
    202  1.26      matt #endif
    203   1.4      matt 
    204   1.4      matt 	/*
    205  1.30     skrll 	 * We now know the pcb has the saved copy.
    206   1.4      matt 	 */
    207   1.4      matt 	register_t * const regp = &frame->tf_r0 + regno;
    208   1.4      matt 	if (insn & 0x00100000) {
    209   1.4      matt 		*regp = pcb->pcb_vfp.vfp_fpscr;
    210   1.4      matt 	} else {
    211  1.34      matt 		pcb->pcb_vfp.vfp_fpscr &= ~vfp_fpscr_changable;
    212  1.34      matt 		pcb->pcb_vfp.vfp_fpscr |= *regp & vfp_fpscr_changable;
    213   1.4      matt 	}
    214   1.4      matt 
    215  1.37      matt 	curcpu()->ci_vfp_evs[0].ev_count++;
    216   1.4      matt 
    217   1.4      matt 	frame->tf_pc += INSN_SIZE;
    218   1.4      matt 	return 0;
    219   1.1  rearnsha }
    220   1.1  rearnsha 
    221   1.4      matt #ifndef FPU_VFP
    222   1.4      matt /*
    223   1.4      matt  * If we don't want VFP support, we still need to handle emulating VFP FPSCR
    224   1.4      matt  * instructions.
    225   1.4      matt  */
    226   1.4      matt void
    227  1.37      matt vfp_attach(struct cpu_info *ci)
    228   1.4      matt {
    229  1.37      matt 	if (CPU_IS_PRIMARY(ci)) {
    230  1.37      matt 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    231  1.37      matt 	}
    232  1.37      matt 	evcnt_attach_dynamic(&ci->ci_vfp_evs[0], EVCNT_TYPE_TRAP, NULL,
    233  1.37      matt 	    ci->ci_cpuname, "vfp fpscr traps");
    234   1.4      matt }
    235   1.4      matt 
    236   1.4      matt #else
    237   1.1  rearnsha void
    238  1.37      matt vfp_attach(struct cpu_info *ci)
    239   1.1  rearnsha {
    240   1.4      matt 	const char *model = NULL;
    241   1.1  rearnsha 
    242  1.37      matt 	if (CPU_ID_ARM11_P(ci->ci_arm_cpuid)
    243  1.37      matt 	    || CPU_ID_MV88SV58XX_P(ci->ci_arm_cpuid)
    244  1.37      matt 	    || CPU_ID_CORTEX_P(ci->ci_arm_cpuid)) {
    245  1.37      matt #if 0
    246  1.37      matt 		const uint32_t nsacr = armreg_nsacr_read();
    247  1.37      matt 		const uint32_t nsacr_vfp = __BITS(VFP_COPROC,VFP_COPROC2);
    248  1.37      matt 		if ((nsacr & nsacr_vfp) != nsacr_vfp) {
    249  1.40      matt 			aprint_normal_dev(ci->ci_dev,
    250  1.40      matt 			    "VFP access denied (NSACR=%#x)\n", nsacr);
    251  1.37      matt 			install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    252  1.37      matt 			ci->ci_vfp_id = 0;
    253  1.37      matt 			evcnt_attach_dynamic(&ci->ci_vfp_evs[0],
    254  1.37      matt 			    EVCNT_TYPE_TRAP, NULL, ci->ci_cpuname,
    255  1.37      matt 			    "vfp fpscr traps");
    256  1.37      matt 			return;
    257  1.37      matt 		}
    258  1.37      matt #endif
    259   1.7      matt 		const uint32_t cpacr_vfp = CPACR_CPn(VFP_COPROC);
    260   1.7      matt 		const uint32_t cpacr_vfp2 = CPACR_CPn(VFP_COPROC2);
    261   1.1  rearnsha 
    262   1.7      matt 		/*
    263   1.7      matt 		 * We first need to enable access to the coprocessors.
    264   1.7      matt 		 */
    265   1.7      matt 		uint32_t cpacr = armreg_cpacr_read();
    266   1.7      matt 		cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp);
    267   1.7      matt 		cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp2);
    268   1.7      matt 		armreg_cpacr_write(cpacr);
    269   1.1  rearnsha 
    270  1.48  jmcneill 		arm_isb();
    271  1.48  jmcneill 
    272   1.7      matt 		/*
    273   1.7      matt 		 * If we could enable them, then they exist.
    274   1.7      matt 		 */
    275   1.7      matt 		cpacr = armreg_cpacr_read();
    276  1.40      matt 		bool vfp_p = __SHIFTOUT(cpacr, cpacr_vfp2) == CPACR_ALL
    277  1.40      matt 		    && __SHIFTOUT(cpacr, cpacr_vfp) == CPACR_ALL;
    278  1.28      matt 		if (!vfp_p) {
    279  1.40      matt 			aprint_normal_dev(ci->ci_dev,
    280  1.40      matt 			    "VFP access denied (CPACR=%#x)\n", cpacr);
    281  1.28      matt 			install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    282  1.28      matt 			ci->ci_vfp_id = 0;
    283  1.37      matt 			evcnt_attach_dynamic(&ci->ci_vfp_evs[0],
    284  1.37      matt 			    EVCNT_TYPE_TRAP, NULL, ci->ci_cpuname,
    285  1.37      matt 			    "vfp fpscr traps");
    286  1.28      matt 			return;
    287  1.28      matt 		}
    288   1.6      matt 	}
    289   1.6      matt 
    290   1.7      matt 	void *uh = install_coproc_handler(VFP_COPROC, vfp_test);
    291   1.7      matt 
    292   1.7      matt 	undefined_test = 0;
    293   1.7      matt 
    294  1.21      matt 	const uint32_t fpsid = armreg_fpsid_read();
    295   1.1  rearnsha 
    296   1.1  rearnsha 	remove_coproc_handler(uh);
    297   1.1  rearnsha 
    298   1.1  rearnsha 	if (undefined_test != 0) {
    299   1.4      matt 		aprint_normal_dev(ci->ci_dev, "No VFP detected\n");
    300   1.4      matt 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    301   1.4      matt 		ci->ci_vfp_id = 0;
    302   1.1  rearnsha 		return;
    303   1.1  rearnsha 	}
    304   1.1  rearnsha 
    305   1.4      matt 	ci->ci_vfp_id = fpsid;
    306   1.4      matt 	switch (fpsid & ~ VFP_FPSID_REV_MSK) {
    307   1.4      matt 	case FPU_VFP10_ARM10E:
    308   1.4      matt 		model = "VFP10 R1";
    309   1.4      matt 		break;
    310   1.4      matt 	case FPU_VFP11_ARM11:
    311   1.4      matt 		model = "VFP11";
    312   1.4      matt 		break;
    313  1.36      matt 	case FPU_VFP_MV88SV58XX:
    314  1.36      matt 		model = "VFP3";
    315  1.36      matt 		break;
    316   1.7      matt 	case FPU_VFP_CORTEXA5:
    317   1.7      matt 	case FPU_VFP_CORTEXA7:
    318   1.7      matt 	case FPU_VFP_CORTEXA8:
    319   1.7      matt 	case FPU_VFP_CORTEXA9:
    320  1.20      matt 	case FPU_VFP_CORTEXA15:
    321  1.42       slp 	case FPU_VFP_CORTEXA15_QEMU:
    322  1.50     skrll 	case FPU_VFP_CORTEXA53:
    323  1.37      matt 		if (armreg_cpacr_read() & CPACR_V7_ASEDIS) {
    324  1.37      matt 			model = "VFP 4.0+";
    325  1.37      matt 		} else {
    326  1.37      matt 			model = "NEON MPE (VFP 3.0+)";
    327  1.37      matt 			cpu_neon_present = 1;
    328  1.37      matt 		}
    329   1.6      matt 		break;
    330   1.4      matt 	default:
    331  1.36      matt 		aprint_normal_dev(ci->ci_dev, "unrecognized VFP version %#x\n",
    332   1.4      matt 		    fpsid);
    333   1.4      matt 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    334  1.35      matt 		vfp_fpscr_changable = VFP_FPSCR_CSUM|VFP_FPSCR_ESUM
    335  1.35      matt 		    |VFP_FPSCR_RMODE;
    336  1.35      matt 		vfp_fpscr_default = 0;
    337   1.4      matt 		return;
    338   1.4      matt 	}
    339   1.1  rearnsha 
    340  1.17      matt 	cpu_fpu_present = 1;
    341  1.21      matt 	cpu_media_and_vfp_features[0] = armreg_mvfr0_read();
    342  1.21      matt 	cpu_media_and_vfp_features[1] = armreg_mvfr1_read();
    343   1.1  rearnsha 	if (fpsid != 0) {
    344  1.34      matt 		uint32_t f0 = armreg_mvfr0_read();
    345  1.41      matt 		uint32_t f1 = armreg_mvfr1_read();
    346  1.34      matt 		aprint_normal("vfp%d at %s: %s%s%s%s%s\n",
    347  1.37      matt 		    device_unit(ci->ci_dev),
    348  1.37      matt 		    device_xname(ci->ci_dev),
    349  1.34      matt 		    model,
    350  1.34      matt 		    ((f0 & ARM_MVFR0_ROUNDING_MASK) ? ", rounding" : ""),
    351  1.34      matt 		    ((f0 & ARM_MVFR0_EXCEPT_MASK) ? ", exceptions" : ""),
    352  1.38      matt 		    ((f1 & ARM_MVFR1_D_NAN_MASK) ? ", NaN propagation" : ""),
    353  1.34      matt 		    ((f1 & ARM_MVFR1_FTZ_MASK) ? ", denormals" : ""));
    354  1.49  jmcneill 		aprint_debug("vfp%d: mvfr: [0]=%#x [1]=%#x\n",
    355  1.37      matt 		    device_unit(ci->ci_dev), f0, f1);
    356  1.37      matt 		if (CPU_IS_PRIMARY(ci)) {
    357  1.37      matt 			if (f0 & ARM_MVFR0_ROUNDING_MASK) {
    358  1.37      matt 				vfp_fpscr_changable |= VFP_FPSCR_RMODE;
    359  1.37      matt 			}
    360  1.37      matt 			if (f1 & ARM_MVFR0_EXCEPT_MASK) {
    361  1.37      matt 				vfp_fpscr_changable |= VFP_FPSCR_ESUM;
    362  1.37      matt 			}
    363  1.38      matt 			// If hardware supports propagation of NaNs, select it.
    364  1.37      matt 			if (f1 & ARM_MVFR1_D_NAN_MASK) {
    365  1.37      matt 				vfp_fpscr_default &= ~VFP_FPSCR_DN;
    366  1.37      matt 				vfp_fpscr_changable |= VFP_FPSCR_DN;
    367  1.37      matt 			}
    368  1.37      matt 			// If hardware supports denormalized numbers, use it.
    369  1.37      matt 			if (cpu_media_and_vfp_features[1] & ARM_MVFR1_FTZ_MASK) {
    370  1.37      matt 				vfp_fpscr_default &= ~VFP_FPSCR_FZ;
    371  1.37      matt 				vfp_fpscr_changable |= VFP_FPSCR_FZ;
    372  1.37      matt 			}
    373  1.37      matt 		}
    374  1.37      matt 	}
    375  1.37      matt 	evcnt_attach_dynamic(&ci->ci_vfp_evs[0], EVCNT_TYPE_MISC, NULL,
    376  1.37      matt 	    ci->ci_cpuname, "vfp coproc use");
    377  1.37      matt 	evcnt_attach_dynamic(&ci->ci_vfp_evs[1], EVCNT_TYPE_MISC, NULL,
    378  1.37      matt 	    ci->ci_cpuname, "vfp coproc re-use");
    379  1.37      matt 	evcnt_attach_dynamic(&ci->ci_vfp_evs[2], EVCNT_TYPE_TRAP, NULL,
    380  1.37      matt 	    ci->ci_cpuname, "vfp coproc fault");
    381   1.1  rearnsha 	install_coproc_handler(VFP_COPROC, vfp_handler);
    382   1.1  rearnsha 	install_coproc_handler(VFP_COPROC2, vfp_handler);
    383  1.13      matt #ifdef CPU_CORTEX
    384  1.43      matt 	if (cpu_neon_present)
    385  1.43      matt 		install_coproc_handler(CORE_UNKNOWN_HANDLER, neon_handler);
    386  1.13      matt #endif
    387   1.1  rearnsha }
    388   1.1  rearnsha 
    389   1.1  rearnsha /* The real handler for VFP bounces.  */
    390   1.4      matt static int
    391  1.21      matt vfp_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    392   1.1  rearnsha {
    393   1.4      matt 	struct cpu_info * const ci = curcpu();
    394   1.1  rearnsha 
    395   1.1  rearnsha 	/* This shouldn't ever happen.  */
    396   1.1  rearnsha 	if (fault_code != FAULT_USER)
    397  1.14      matt 		panic("VFP fault at %#x in non-user mode", frame->tf_pc);
    398   1.1  rearnsha 
    399  1.27      matt 	if (ci->ci_vfp_id == 0) {
    400   1.1  rearnsha 		/* No VFP detected, just fault.  */
    401   1.1  rearnsha 		return 1;
    402  1.27      matt 	}
    403  1.27      matt 
    404  1.27      matt 	/*
    405  1.47      matt 	 * If we are just changing/fetching FPSCR, don't bother loading it
    406  1.47      matt 	 * just emulate the instruction.
    407  1.27      matt 	 */
    408  1.27      matt 	if (!vfp_fpscr_handler(address, insn, frame, fault_code))
    409  1.47      matt 		return 0;
    410  1.27      matt 
    411  1.47      matt 	/*
    412  1.47      matt 	 * If we already own the FPU and it's enabled (and no exception), raise
    413  1.47      matt 	 * SIGILL.  If there is an exception, drop through to raise a SIGFPE.
    414  1.47      matt 	 */
    415  1.46      matt 	if (curcpu()->ci_pcu_curlwp[PCU_FPU] == curlwp
    416  1.47      matt 	    && (armreg_fpexc_read() & (VFP_FPEXC_EX|VFP_FPEXC_EN)) == VFP_FPEXC_EN)
    417  1.47      matt 		return 1;
    418  1.44      matt 
    419  1.27      matt 	/*
    420  1.27      matt 	 * Make sure we own the FP.
    421  1.27      matt 	 */
    422  1.27      matt 	pcu_load(&arm_vfp_ops);
    423   1.1  rearnsha 
    424  1.21      matt 	uint32_t fpexc = armreg_fpexc_read();
    425  1.21      matt 	if (fpexc & VFP_FPEXC_EX) {
    426  1.21      matt 		ksiginfo_t ksi;
    427  1.21      matt 		KASSERT(fpexc & VFP_FPEXC_EN);
    428  1.21      matt 
    429  1.37      matt 		curcpu()->ci_vfp_evs[2].ev_count++;
    430  1.21      matt 
    431  1.21      matt 		/*
    432  1.21      matt 		 * Need the clear the exception condition so any signal
    433  1.33     skrll 		 * and future use can proceed.
    434  1.21      matt 		 */
    435  1.31     skrll 		armreg_fpexc_write(fpexc & ~(VFP_FPEXC_EX|VFP_FPEXC_FSUM));
    436  1.21      matt 
    437  1.51       chs 		pcu_save(&arm_vfp_ops, curlwp);
    438  1.33     skrll 
    439  1.33     skrll 		/*
    440  1.33     skrll 		 * XXX Need to emulate bounce instructions here to get correct
    441  1.33     skrll 		 * XXX exception codes, etc.
    442  1.33     skrll 		 */
    443  1.21      matt 		KSI_INIT_TRAP(&ksi);
    444  1.21      matt 		ksi.ksi_signo = SIGFPE;
    445  1.21      matt 		if (fpexc & VFP_FPEXC_IXF)
    446  1.21      matt 			ksi.ksi_code = FPE_FLTRES;
    447  1.21      matt 		else if (fpexc & VFP_FPEXC_UFF)
    448  1.21      matt 			ksi.ksi_code = FPE_FLTUND;
    449  1.21      matt 		else if (fpexc & VFP_FPEXC_OFF)
    450  1.21      matt 			ksi.ksi_code = FPE_FLTOVF;
    451  1.21      matt 		else if (fpexc & VFP_FPEXC_DZF)
    452  1.21      matt 			ksi.ksi_code = FPE_FLTDIV;
    453  1.21      matt 		else if (fpexc & VFP_FPEXC_IOF)
    454  1.21      matt 			ksi.ksi_code = FPE_FLTINV;
    455  1.21      matt 		ksi.ksi_addr = (uint32_t *)address;
    456  1.21      matt 		ksi.ksi_trap = 0;
    457  1.21      matt 		trapsignal(curlwp, &ksi);
    458  1.21      matt 		return 0;
    459  1.21      matt 	}
    460  1.21      matt 
    461   1.4      matt 	/* Need to restart the faulted instruction.  */
    462   1.4      matt //	frame->tf_pc -= INSN_SIZE;
    463   1.4      matt 	return 0;
    464   1.4      matt }
    465   1.1  rearnsha 
    466  1.13      matt #ifdef CPU_CORTEX
    467  1.13      matt /* The real handler for NEON bounces.  */
    468  1.13      matt static int
    469  1.21      matt neon_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    470  1.13      matt {
    471  1.13      matt 	struct cpu_info * const ci = curcpu();
    472  1.13      matt 
    473  1.13      matt 	if (ci->ci_vfp_id == 0)
    474  1.13      matt 		/* No VFP detected, just fault.  */
    475  1.13      matt 		return 1;
    476  1.13      matt 
    477  1.13      matt 	if ((insn & 0xfe000000) != 0xf2000000
    478  1.13      matt 	    && (insn & 0xfe000000) != 0xf4000000)
    479  1.13      matt 		/* Not NEON instruction, just fault.  */
    480  1.13      matt 		return 1;
    481  1.13      matt 
    482  1.13      matt 	/* This shouldn't ever happen.  */
    483  1.13      matt 	if (fault_code != FAULT_USER)
    484  1.13      matt 		panic("NEON fault in non-user mode");
    485  1.13      matt 
    486  1.45      matt 	/* if we already own the FPU and it's enabled, raise SIGILL */
    487  1.45      matt 	if (curcpu()->ci_pcu_curlwp[PCU_FPU] == curlwp
    488  1.45      matt 	    && (armreg_fpexc_read() & VFP_FPEXC_EN) != 0)
    489  1.47      matt 		return 1;
    490  1.43      matt 
    491  1.13      matt 	pcu_load(&arm_vfp_ops);
    492  1.13      matt 
    493  1.13      matt 	/* Need to restart the faulted instruction.  */
    494  1.13      matt //	frame->tf_pc -= INSN_SIZE;
    495  1.13      matt 	return 0;
    496  1.13      matt }
    497  1.13      matt #endif
    498  1.13      matt 
    499   1.4      matt static void
    500  1.13      matt vfp_state_load(lwp_t *l, u_int flags)
    501   1.4      matt {
    502   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    503   1.4      matt 	struct vfpreg * const fregs = &pcb->pcb_vfp;
    504   1.1  rearnsha 
    505   1.1  rearnsha 	/*
    506   1.1  rearnsha 	 * Instrument VFP usage -- if a process has not previously
    507   1.1  rearnsha 	 * used the VFP, mark it as having used VFP for the first time,
    508   1.1  rearnsha 	 * and count this event.
    509   1.1  rearnsha 	 *
    510   1.1  rearnsha 	 * If a process has used the VFP, count a "used VFP, and took
    511   1.1  rearnsha 	 * a trap to use it again" event.
    512   1.1  rearnsha 	 */
    513  1.39     rmind 	if (__predict_false((flags & PCU_VALID) == 0)) {
    514  1.37      matt 		curcpu()->ci_vfp_evs[0].ev_count++;
    515  1.34      matt 		pcb->pcb_vfp.vfp_fpscr = vfp_fpscr_default;
    516   1.4      matt 	} else {
    517  1.37      matt 		curcpu()->ci_vfp_evs[1].ev_count++;
    518   1.4      matt 	}
    519   1.1  rearnsha 
    520  1.39     rmind 	/*
    521  1.39     rmind 	 * If the VFP is already enabled we must be bouncing an instruction.
    522  1.39     rmind 	 */
    523  1.39     rmind 	if (flags & PCU_REENABLE) {
    524  1.39     rmind 		uint32_t fpexc = armreg_fpexc_read();
    525  1.39     rmind 		armreg_fpexc_write(fpexc | VFP_FPEXC_EN);
    526  1.39     rmind 		return;
    527  1.39     rmind 	}
    528  1.33     skrll 
    529  1.39     rmind 	/*
    530  1.39     rmind 	 * Load and Enable the VFP (so that we can write the registers).
    531  1.39     rmind 	 */
    532  1.39     rmind 	bool enabled = fregs->vfp_fpexc & VFP_FPEXC_EN;
    533  1.39     rmind 	fregs->vfp_fpexc |= VFP_FPEXC_EN;
    534  1.39     rmind 	armreg_fpexc_write(fregs->vfp_fpexc);
    535  1.39     rmind 	if (enabled) {
    536   1.4      matt 		/*
    537  1.39     rmind 		 * If we think the VFP is enabled, it must have be
    538  1.39     rmind 		 * disabled by vfp_state_release for another LWP so
    539  1.39     rmind 		 * we can now just return.
    540   1.4      matt 		 */
    541  1.39     rmind 		return;
    542  1.39     rmind 	}
    543  1.13      matt 
    544  1.39     rmind 	load_vfpregs(fregs);
    545  1.39     rmind 	armreg_fpscr_write(fregs->vfp_fpscr);
    546  1.13      matt 
    547  1.39     rmind 	if (fregs->vfp_fpexc & VFP_FPEXC_EX) {
    548  1.39     rmind 		/* Need to restore the exception handling state.  */
    549  1.39     rmind 		armreg_fpinst2_write(fregs->vfp_fpinst2);
    550  1.39     rmind 		if (fregs->vfp_fpexc & VFP_FPEXC_FP2V)
    551  1.39     rmind 			armreg_fpinst_write(fregs->vfp_fpinst);
    552   1.1  rearnsha 	}
    553   1.1  rearnsha }
    554   1.1  rearnsha 
    555   1.1  rearnsha void
    556  1.39     rmind vfp_state_save(lwp_t *l)
    557   1.1  rearnsha {
    558   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    559  1.39     rmind 	struct vfpreg * const fregs = &pcb->pcb_vfp;
    560  1.21      matt 	uint32_t fpexc = armreg_fpexc_read();
    561  1.33     skrll 
    562  1.33     skrll 	/*
    563  1.33     skrll 	 * Enable the VFP (so we can read the registers).
    564  1.33     skrll 	 * Make sure the exception bit is cleared so that we can
    565  1.33     skrll 	 * safely dump the registers.
    566  1.33     skrll 	 */
    567  1.21      matt 	armreg_fpexc_write((fpexc | VFP_FPEXC_EN) & ~VFP_FPEXC_EX);
    568   1.1  rearnsha 
    569   1.4      matt 	fregs->vfp_fpexc = fpexc;
    570   1.4      matt 	if (fpexc & VFP_FPEXC_EX) {
    571   1.4      matt 		/* Need to save the exception handling state */
    572  1.21      matt 		fregs->vfp_fpinst = armreg_fpinst_read();
    573  1.21      matt 		if (fpexc & VFP_FPEXC_FP2V)
    574  1.21      matt 			fregs->vfp_fpinst2 = armreg_fpinst2_read();
    575   1.1  rearnsha 	}
    576  1.21      matt 	fregs->vfp_fpscr = armreg_fpscr_read();
    577  1.13      matt 	save_vfpregs(fregs);
    578   1.4      matt 
    579   1.1  rearnsha 	/* Disable the VFP.  */
    580  1.33     skrll 	armreg_fpexc_write(fpexc & ~VFP_FPEXC_EN);
    581   1.1  rearnsha }
    582   1.1  rearnsha 
    583   1.1  rearnsha void
    584  1.39     rmind vfp_state_release(lwp_t *l)
    585   1.1  rearnsha {
    586   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    587   1.1  rearnsha 
    588  1.39     rmind 	/*
    589  1.39     rmind 	 * Now mark the VFP as disabled (and our state
    590  1.39     rmind 	 * has been already saved or is being discarded).
    591  1.39     rmind 	 */
    592  1.39     rmind 	pcb->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
    593   1.1  rearnsha 
    594   1.1  rearnsha 	/*
    595   1.4      matt 	 * Turn off the FPU so the next time a VFP instruction is issued
    596   1.4      matt 	 * an exception happens.  We don't know if this LWP's state was
    597   1.4      matt 	 * loaded but if we turned off the FPU for some other LWP, when
    598   1.4      matt 	 * pcu_load invokes vfp_state_load it will see that VFP_FPEXC_EN
    599  1.13      matt 	 * is still set so it just restore fpexc and return since its
    600   1.4      matt 	 * contents are still sitting in the VFP.
    601   1.1  rearnsha 	 */
    602  1.21      matt 	armreg_fpexc_write(armreg_fpexc_read() & ~VFP_FPEXC_EN);
    603   1.1  rearnsha }
    604   1.1  rearnsha 
    605   1.1  rearnsha void
    606  1.51       chs vfp_savecontext(lwp_t *l)
    607   1.1  rearnsha {
    608  1.51       chs 	pcu_save(&arm_vfp_ops, l);
    609   1.1  rearnsha }
    610   1.1  rearnsha 
    611   1.1  rearnsha void
    612  1.51       chs vfp_discardcontext(lwp_t *l, bool used_p)
    613   1.1  rearnsha {
    614  1.51       chs 	pcu_discard(&arm_vfp_ops, l, used_p);
    615  1.25      matt }
    616  1.25      matt 
    617  1.25      matt bool
    618  1.51       chs vfp_used_p(const lwp_t *l)
    619  1.25      matt {
    620  1.51       chs 	return pcu_valid_p(&arm_vfp_ops, l);
    621  1.13      matt }
    622  1.13      matt 
    623  1.13      matt void
    624   1.8      matt vfp_getcontext(struct lwp *l, mcontext_t *mcp, int *flagsp)
    625   1.8      matt {
    626  1.51       chs 	if (vfp_used_p(l)) {
    627   1.8      matt 		const struct pcb * const pcb = lwp_getpcb(l);
    628  1.51       chs 
    629  1.51       chs 		pcu_save(&arm_vfp_ops, l);
    630   1.8      matt 		mcp->__fpu.__vfpregs.__vfp_fpscr = pcb->pcb_vfp.vfp_fpscr;
    631   1.8      matt 		memcpy(mcp->__fpu.__vfpregs.__vfp_fstmx, pcb->pcb_vfp.vfp_regs,
    632   1.8      matt 		    sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
    633  1.10      matt 		*flagsp |= _UC_FPU|_UC_ARM_VFP;
    634   1.8      matt 	}
    635   1.8      matt }
    636   1.8      matt 
    637   1.8      matt void
    638   1.8      matt vfp_setcontext(struct lwp *l, const mcontext_t *mcp)
    639   1.8      matt {
    640   1.8      matt 	struct pcb * const pcb = lwp_getpcb(l);
    641  1.51       chs 
    642  1.51       chs 	pcu_discard(&arm_vfp_ops, l, true);
    643   1.8      matt 	pcb->pcb_vfp.vfp_fpscr = mcp->__fpu.__vfpregs.__vfp_fpscr;
    644   1.8      matt 	memcpy(pcb->pcb_vfp.vfp_regs, mcp->__fpu.__vfpregs.__vfp_fstmx,
    645   1.8      matt 	    sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
    646   1.8      matt }
    647   1.8      matt 
    648   1.4      matt #endif /* FPU_VFP */
    649