Home | History | Annotate | Line # | Download | only in vfp
vfp_init.c revision 1.33
      1  1.33     skrll /*      $NetBSD: vfp_init.c,v 1.33 2014/01/25 17:30:56 skrll 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.18      matt extern int cpu_media_and_vfp_features[];
     48  1.18      matt extern int cpu_neon_present;
     49  1.18      matt 
     50  1.11      matt #ifdef FPU_VFP
     51  1.11      matt 
     52  1.29      matt #ifdef CPU_CORTEX
     53  1.29      matt __asm(".fpu\tvfpv4");
     54  1.29      matt #else
     55  1.29      matt __asm(".fpu\tvfp");
     56  1.29      matt #endif
     57  1.29      matt 
     58   1.1  rearnsha /* FLDMD <X>, {d0-d15} */
     59  1.11      matt static inline void
     60  1.13      matt load_vfpregs_lo(const uint64_t *p)
     61  1.10      matt {
     62  1.29      matt 	__asm __volatile("vldmia %0, {d0-d15}" :: "r" (p) : "memory");
     63  1.10      matt }
     64  1.10      matt 
     65  1.10      matt /* FSTMD <X>, {d0-d15} */
     66  1.11      matt static inline void
     67  1.10      matt save_vfpregs_lo(uint64_t *p)
     68  1.10      matt {
     69  1.29      matt 	__asm __volatile("vstmia %0, {d0-d15}" :: "r" (p) : "memory");
     70  1.10      matt }
     71  1.10      matt 
     72  1.10      matt #ifdef CPU_CORTEX
     73  1.10      matt /* FLDMD <X>, {d16-d31} */
     74  1.11      matt static inline void
     75  1.13      matt load_vfpregs_hi(const uint64_t *p)
     76  1.10      matt {
     77  1.29      matt 	__asm __volatile("vldmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
     78  1.10      matt }
     79  1.10      matt 
     80  1.10      matt /* FLDMD <X>, {d16-d31} */
     81  1.11      matt static inline void
     82  1.10      matt save_vfpregs_hi(uint64_t *p)
     83  1.10      matt {
     84  1.29      matt 	__asm __volatile("vstmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
     85  1.10      matt }
     86  1.10      matt #endif
     87   1.1  rearnsha 
     88  1.13      matt static inline void
     89  1.13      matt load_vfpregs(const struct vfpreg *fregs)
     90  1.13      matt {
     91  1.13      matt 	load_vfpregs_lo(fregs->vfp_regs);
     92  1.13      matt #ifdef CPU_CORTEX
     93  1.13      matt #ifdef CPU_ARM11
     94  1.13      matt 	switch (curcpu()->ci_vfp_id) {
     95  1.13      matt 	case FPU_VFP_CORTEXA5:
     96  1.13      matt 	case FPU_VFP_CORTEXA7:
     97  1.13      matt 	case FPU_VFP_CORTEXA8:
     98  1.13      matt 	case FPU_VFP_CORTEXA9:
     99  1.20      matt 	case FPU_VFP_CORTEXA15:
    100  1.13      matt #endif
    101  1.13      matt 		load_vfpregs_hi(fregs->vfp_regs);
    102  1.13      matt #ifdef CPU_ARM11
    103  1.13      matt 		break;
    104  1.13      matt 	}
    105  1.13      matt #endif
    106  1.13      matt #endif
    107  1.13      matt }
    108  1.13      matt 
    109  1.13      matt static inline void
    110  1.13      matt save_vfpregs(struct vfpreg *fregs)
    111  1.13      matt {
    112  1.13      matt 	save_vfpregs_lo(fregs->vfp_regs);
    113  1.13      matt #ifdef CPU_CORTEX
    114  1.13      matt #ifdef CPU_ARM11
    115  1.13      matt 	switch (curcpu()->ci_vfp_id) {
    116  1.13      matt 	case FPU_VFP_CORTEXA5:
    117  1.13      matt 	case FPU_VFP_CORTEXA7:
    118  1.13      matt 	case FPU_VFP_CORTEXA8:
    119  1.13      matt 	case FPU_VFP_CORTEXA9:
    120  1.20      matt 	case FPU_VFP_CORTEXA15:
    121  1.13      matt #endif
    122  1.13      matt 		save_vfpregs_hi(fregs->vfp_regs);
    123  1.13      matt #ifdef CPU_ARM11
    124  1.13      matt 		break;
    125  1.13      matt 	}
    126  1.13      matt #endif
    127  1.13      matt #endif
    128  1.13      matt }
    129  1.13      matt 
    130   1.1  rearnsha /* The real handler for VFP bounces.  */
    131   1.1  rearnsha static int vfp_handler(u_int, u_int, trapframe_t *, int);
    132  1.13      matt #ifdef CPU_CORTEX
    133  1.13      matt static int neon_handler(u_int, u_int, trapframe_t *, int);
    134  1.13      matt #endif
    135   1.1  rearnsha 
    136  1.13      matt static void vfp_state_load(lwp_t *, u_int);
    137  1.13      matt static void vfp_state_save(lwp_t *, u_int);
    138  1.13      matt static void vfp_state_release(lwp_t *, u_int);
    139   1.4      matt 
    140   1.4      matt const pcu_ops_t arm_vfp_ops = {
    141   1.4      matt 	.pcu_id = PCU_FPU,
    142  1.13      matt 	.pcu_state_save = vfp_state_save,
    143   1.4      matt 	.pcu_state_load = vfp_state_load,
    144   1.4      matt 	.pcu_state_release = vfp_state_release,
    145   1.4      matt };
    146   1.1  rearnsha 
    147   1.1  rearnsha struct evcnt vfpevent_use;
    148   1.1  rearnsha struct evcnt vfpevent_reuse;
    149  1.21      matt struct evcnt vfpevent_fpe;
    150   1.1  rearnsha 
    151   1.1  rearnsha /*
    152   1.1  rearnsha  * Used to test for a VFP. The following function is installed as a coproc10
    153   1.1  rearnsha  * handler on the undefined instruction vector and then we issue a VFP
    154   1.1  rearnsha  * instruction. If undefined_test is non zero then the VFP did not handle
    155   1.1  rearnsha  * the instruction so must be absent, or disabled.
    156   1.1  rearnsha  */
    157   1.1  rearnsha 
    158   1.1  rearnsha static int undefined_test;
    159   1.1  rearnsha 
    160   1.1  rearnsha static int
    161   1.4      matt vfp_test(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    162   1.1  rearnsha {
    163   1.1  rearnsha 
    164   1.1  rearnsha 	frame->tf_pc += INSN_SIZE;
    165   1.1  rearnsha 	++undefined_test;
    166   1.4      matt 	return 0;
    167   1.4      matt }
    168   1.4      matt 
    169   1.4      matt #endif /* FPU_VFP */
    170   1.4      matt 
    171   1.4      matt struct evcnt vfp_fpscr_ev =
    172   1.4      matt     EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "VFP", "FPSCR traps");
    173   1.4      matt EVCNT_ATTACH_STATIC(vfp_fpscr_ev);
    174   1.4      matt 
    175   1.4      matt static int
    176   1.4      matt vfp_fpscr_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    177   1.4      matt {
    178   1.4      matt 	struct lwp * const l = curlwp;
    179   1.4      matt 	const u_int regno = (insn >> 12) & 0xf;
    180   1.4      matt 	/*
    181   1.4      matt 	 * Only match move to/from the FPSCR register and we
    182   1.4      matt 	 * can't be using the SP,LR,PC as a source.
    183   1.4      matt 	 */
    184   1.4      matt 	if ((insn & 0xffef0fff) != 0xeee10a10 || regno > 12)
    185   1.4      matt 		return 1;
    186   1.4      matt 
    187   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    188   1.4      matt 
    189   1.4      matt #ifdef FPU_VFP
    190   1.4      matt 	/*
    191   1.4      matt 	 * If FPU is valid somewhere, let's just reenable VFP and
    192   1.4      matt 	 * retry the instruction (only safe thing to do since the
    193   1.4      matt 	 * pcb has a stale copy).
    194   1.4      matt 	 */
    195   1.4      matt 	if (pcb->pcb_vfp.vfp_fpexc & VFP_FPEXC_EN)
    196   1.4      matt 		return 1;
    197   1.4      matt 
    198  1.25      matt 	if (__predict_false(!vfp_used_p())) {
    199   1.4      matt 		pcb->pcb_vfp.vfp_fpscr =
    200  1.32     skrll 		    (VFP_FPSCR_DN | VFP_FPSCR_FZ | VFP_FPSCR_RN); /* Runfast */
    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.21      matt 		register_t tmp = *regp;
    212  1.21      matt 		if (!(cpu_media_and_vfp_features[0] & ARM_MVFR0_EXCEPT_MASK))
    213  1.21      matt 			tmp &= ~VFP_FPSCR_ESUM;
    214  1.21      matt 		pcb->pcb_vfp.vfp_fpscr = tmp;
    215   1.4      matt 	}
    216   1.4      matt 
    217   1.4      matt 	vfp_fpscr_ev.ev_count++;
    218   1.4      matt 
    219   1.4      matt 	frame->tf_pc += INSN_SIZE;
    220   1.4      matt 	return 0;
    221   1.1  rearnsha }
    222   1.1  rearnsha 
    223   1.4      matt #ifndef FPU_VFP
    224   1.4      matt /*
    225   1.4      matt  * If we don't want VFP support, we still need to handle emulating VFP FPSCR
    226   1.4      matt  * instructions.
    227   1.4      matt  */
    228   1.4      matt void
    229   1.4      matt vfp_attach(void)
    230   1.4      matt {
    231   1.4      matt 	install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    232   1.4      matt }
    233   1.4      matt 
    234   1.4      matt #else
    235  1.16      matt #if 0
    236  1.12      matt static bool
    237  1.12      matt vfp_patch_branch(uintptr_t code, uintptr_t func, uintptr_t newfunc)
    238  1.12      matt {
    239  1.12      matt 	for (;; code += sizeof(uint32_t)) {
    240  1.12      matt 		uint32_t insn = *(uint32_t *)code;
    241  1.12      matt 		if ((insn & 0xffd08000) == 0xe8908000)	/* ldm ... { pc } */
    242  1.12      matt 			return false;
    243  1.12      matt 		if ((insn & 0xfffffff0) == 0xe12fff10)	/* bx rN */
    244  1.12      matt 			return false;
    245  1.12      matt 		if ((insn & 0xf1a0f000) == 0xe1a0f000)	/* mov pc, ... */
    246  1.12      matt 			return false;
    247  1.12      matt 		if ((insn >> 25) != 0x75)		/* not b/bl insn */
    248  1.12      matt 			continue;
    249  1.12      matt 		intptr_t imm26 = ((int32_t)insn << 8) >> 6;
    250  1.12      matt 		if (code + imm26 + 8 == func) {
    251  1.12      matt 			int32_t imm24 = (newfunc - (code + 8)) >> 2;
    252  1.12      matt 			uint32_t new_insn = (insn & 0xff000000)
    253  1.12      matt 			   | (imm24 & 0xffffff);
    254  1.12      matt 			KASSERTMSG((uint32_t)((imm24 >> 24) + 1) <= 1, "%x",
    255  1.12      matt 			    ((imm24 >> 24) + 1));
    256  1.12      matt 			*(uint32_t *)code = new_insn;
    257  1.12      matt 			cpu_idcache_wbinv_range(code, sizeof(uint32_t));
    258  1.12      matt 			return true;
    259  1.12      matt 		}
    260  1.12      matt 	}
    261  1.12      matt }
    262  1.16      matt #endif
    263  1.12      matt 
    264   1.1  rearnsha void
    265   1.2    cegger vfp_attach(void)
    266   1.1  rearnsha {
    267   1.4      matt 	struct cpu_info * const ci = curcpu();
    268   1.4      matt 	const char *model = NULL;
    269   1.1  rearnsha 
    270   1.7      matt 	if (CPU_ID_ARM11_P(curcpu()->ci_arm_cpuid)
    271   1.7      matt 	    || CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)) {
    272   1.7      matt 		const uint32_t cpacr_vfp = CPACR_CPn(VFP_COPROC);
    273   1.7      matt 		const uint32_t cpacr_vfp2 = CPACR_CPn(VFP_COPROC2);
    274   1.1  rearnsha 
    275   1.7      matt 		/*
    276   1.7      matt 		 * We first need to enable access to the coprocessors.
    277   1.7      matt 		 */
    278   1.7      matt 		uint32_t cpacr = armreg_cpacr_read();
    279   1.7      matt 		cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp);
    280   1.7      matt 		cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp2);
    281  1.10      matt #if 0
    282   1.9      matt 		if (CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)) {
    283   1.9      matt 			/*
    284  1.10      matt 			 * Disable access to the upper 16 FP registers and NEON.
    285   1.9      matt 			 */
    286   1.9      matt 			cpacr |= CPACR_V7_D32DIS;
    287  1.10      matt 			cpacr |= CPACR_V7_ASEDIS;
    288   1.9      matt 		}
    289  1.10      matt #endif
    290   1.7      matt 		armreg_cpacr_write(cpacr);
    291   1.1  rearnsha 
    292   1.7      matt 		/*
    293   1.7      matt 		 * If we could enable them, then they exist.
    294   1.7      matt 		 */
    295   1.7      matt 		cpacr = armreg_cpacr_read();
    296  1.28      matt 		bool vfp_p = __SHIFTOUT(cpacr, cpacr_vfp2) != CPACR_NOACCESS
    297   1.7      matt 		    || __SHIFTOUT(cpacr, cpacr_vfp) != CPACR_NOACCESS;
    298  1.28      matt 		if (!vfp_p) {
    299  1.28      matt 			aprint_normal_dev(ci->ci_dev, "No VFP detected\n");
    300  1.28      matt 			install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    301  1.28      matt 			ci->ci_vfp_id = 0;
    302  1.28      matt 			return;
    303  1.28      matt 		}
    304   1.6      matt 	}
    305   1.6      matt 
    306   1.7      matt 	void *uh = install_coproc_handler(VFP_COPROC, vfp_test);
    307   1.7      matt 
    308   1.7      matt 	undefined_test = 0;
    309   1.7      matt 
    310  1.21      matt 	const uint32_t fpsid = armreg_fpsid_read();
    311   1.1  rearnsha 
    312   1.1  rearnsha 	remove_coproc_handler(uh);
    313   1.1  rearnsha 
    314   1.1  rearnsha 	if (undefined_test != 0) {
    315   1.4      matt 		aprint_normal_dev(ci->ci_dev, "No VFP detected\n");
    316   1.4      matt 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    317   1.4      matt 		ci->ci_vfp_id = 0;
    318   1.1  rearnsha 		return;
    319   1.1  rearnsha 	}
    320   1.1  rearnsha 
    321   1.4      matt 	ci->ci_vfp_id = fpsid;
    322   1.4      matt 	switch (fpsid & ~ VFP_FPSID_REV_MSK) {
    323   1.4      matt 	case FPU_VFP10_ARM10E:
    324   1.4      matt 		model = "VFP10 R1";
    325   1.4      matt 		break;
    326   1.4      matt 	case FPU_VFP11_ARM11:
    327   1.4      matt 		model = "VFP11";
    328   1.4      matt 		break;
    329   1.7      matt 	case FPU_VFP_CORTEXA5:
    330   1.7      matt 	case FPU_VFP_CORTEXA7:
    331   1.7      matt 	case FPU_VFP_CORTEXA8:
    332   1.7      matt 	case FPU_VFP_CORTEXA9:
    333  1.20      matt 	case FPU_VFP_CORTEXA15:
    334   1.7      matt 		model = "NEON MPE (VFP 3.0+)";
    335  1.18      matt 		cpu_neon_present = 1;
    336   1.6      matt 		break;
    337   1.4      matt 	default:
    338   1.4      matt 		aprint_normal_dev(ci->ci_dev, "unrecognized VFP version %x\n",
    339   1.4      matt 		    fpsid);
    340   1.4      matt 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    341   1.4      matt 		return;
    342   1.4      matt 	}
    343   1.1  rearnsha 
    344  1.17      matt 	cpu_fpu_present = 1;
    345  1.21      matt 	cpu_media_and_vfp_features[0] = armreg_mvfr0_read();
    346  1.21      matt 	cpu_media_and_vfp_features[1] = armreg_mvfr1_read();
    347   1.1  rearnsha 	if (fpsid != 0) {
    348   1.1  rearnsha 		aprint_normal("vfp%d at %s: %s\n",
    349  1.21      matt 		    device_unit(curcpu()->ci_dev),
    350  1.21      matt 		    device_xname(curcpu()->ci_dev),
    351   1.1  rearnsha 		    model);
    352  1.21      matt 		aprint_verbose("vfp%d: mvfr: [0]=%#x [1]=%#x\n",
    353  1.21      matt 		    device_unit(curcpu()->ci_dev),
    354  1.21      matt 		    cpu_media_and_vfp_features[0],
    355  1.21      matt 		    cpu_media_and_vfp_features[1]);
    356   1.1  rearnsha 	}
    357   1.1  rearnsha 	evcnt_attach_dynamic(&vfpevent_use, EVCNT_TYPE_MISC, NULL,
    358  1.12      matt 	    "VFP", "coproc use");
    359   1.1  rearnsha 	evcnt_attach_dynamic(&vfpevent_reuse, EVCNT_TYPE_MISC, NULL,
    360  1.12      matt 	    "VFP", "coproc re-use");
    361  1.21      matt 	evcnt_attach_dynamic(&vfpevent_fpe, EVCNT_TYPE_TRAP, NULL,
    362  1.21      matt 	    "VFP", "coproc fault");
    363   1.1  rearnsha 	install_coproc_handler(VFP_COPROC, vfp_handler);
    364   1.1  rearnsha 	install_coproc_handler(VFP_COPROC2, vfp_handler);
    365  1.13      matt #ifdef CPU_CORTEX
    366  1.13      matt 	install_coproc_handler(CORE_UNKNOWN_HANDLER, neon_handler);
    367  1.13      matt #endif
    368  1.12      matt 
    369  1.16      matt #if 0
    370  1.12      matt 	vfp_patch_branch((uintptr_t)pmap_copy_page_generic,
    371  1.12      matt 	   (uintptr_t)bcopy_page, (uintptr_t)bcopy_page_vfp);
    372  1.12      matt 	vfp_patch_branch((uintptr_t)pmap_zero_page_generic,
    373  1.12      matt 	   (uintptr_t)bzero_page, (uintptr_t)bzero_page_vfp);
    374  1.16      matt #endif
    375   1.1  rearnsha }
    376   1.1  rearnsha 
    377   1.1  rearnsha /* The real handler for VFP bounces.  */
    378   1.4      matt static int
    379  1.21      matt vfp_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    380   1.1  rearnsha {
    381   1.4      matt 	struct cpu_info * const ci = curcpu();
    382   1.1  rearnsha 
    383   1.1  rearnsha 	/* This shouldn't ever happen.  */
    384   1.1  rearnsha 	if (fault_code != FAULT_USER)
    385  1.14      matt 		panic("VFP fault at %#x in non-user mode", frame->tf_pc);
    386   1.1  rearnsha 
    387  1.27      matt 	if (ci->ci_vfp_id == 0) {
    388   1.1  rearnsha 		/* No VFP detected, just fault.  */
    389   1.1  rearnsha 		return 1;
    390  1.27      matt 	}
    391  1.27      matt 
    392  1.27      matt 	/*
    393  1.27      matt 	 * If we are just changing/fetching FPSCR, don't bother loading it.
    394  1.27      matt 	 */
    395  1.27      matt 	if (!vfp_fpscr_handler(address, insn, frame, fault_code))
    396  1.27      matt 		return 0;
    397  1.27      matt 
    398  1.27      matt 	/*
    399  1.27      matt 	 * Make sure we own the FP.
    400  1.27      matt 	 */
    401  1.27      matt 	pcu_load(&arm_vfp_ops);
    402   1.1  rearnsha 
    403  1.21      matt 	uint32_t fpexc = armreg_fpexc_read();
    404  1.21      matt 	if (fpexc & VFP_FPEXC_EX) {
    405  1.21      matt 		ksiginfo_t ksi;
    406  1.21      matt 		KASSERT(fpexc & VFP_FPEXC_EN);
    407  1.21      matt 
    408  1.21      matt 		vfpevent_fpe.ev_count++;
    409  1.21      matt 
    410  1.21      matt 		/*
    411  1.21      matt 		 * Need the clear the exception condition so any signal
    412  1.33     skrll 		 * and future use can proceed.
    413  1.21      matt 		 */
    414  1.31     skrll 		armreg_fpexc_write(fpexc & ~(VFP_FPEXC_EX|VFP_FPEXC_FSUM));
    415  1.21      matt 
    416  1.33     skrll 		pcu_save(&arm_vfp_ops);
    417  1.33     skrll 
    418  1.33     skrll 		/*
    419  1.33     skrll 		 * XXX Need to emulate bounce instructions here to get correct
    420  1.33     skrll 		 * XXX exception codes, etc.
    421  1.33     skrll 		 */
    422  1.21      matt 		KSI_INIT_TRAP(&ksi);
    423  1.21      matt 		ksi.ksi_signo = SIGFPE;
    424  1.21      matt 		if (fpexc & VFP_FPEXC_IXF)
    425  1.21      matt 			ksi.ksi_code = FPE_FLTRES;
    426  1.21      matt 		else if (fpexc & VFP_FPEXC_UFF)
    427  1.21      matt 			ksi.ksi_code = FPE_FLTUND;
    428  1.21      matt 		else if (fpexc & VFP_FPEXC_OFF)
    429  1.21      matt 			ksi.ksi_code = FPE_FLTOVF;
    430  1.21      matt 		else if (fpexc & VFP_FPEXC_DZF)
    431  1.21      matt 			ksi.ksi_code = FPE_FLTDIV;
    432  1.21      matt 		else if (fpexc & VFP_FPEXC_IOF)
    433  1.21      matt 			ksi.ksi_code = FPE_FLTINV;
    434  1.21      matt 		ksi.ksi_addr = (uint32_t *)address;
    435  1.21      matt 		ksi.ksi_trap = 0;
    436  1.21      matt 		trapsignal(curlwp, &ksi);
    437  1.21      matt 		return 0;
    438  1.21      matt 	}
    439  1.21      matt 
    440   1.4      matt 	/* Need to restart the faulted instruction.  */
    441   1.4      matt //	frame->tf_pc -= INSN_SIZE;
    442   1.4      matt 	return 0;
    443   1.4      matt }
    444   1.1  rearnsha 
    445  1.13      matt #ifdef CPU_CORTEX
    446  1.13      matt /* The real handler for NEON bounces.  */
    447  1.13      matt static int
    448  1.21      matt neon_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    449  1.13      matt {
    450  1.13      matt 	struct cpu_info * const ci = curcpu();
    451  1.13      matt 
    452  1.13      matt 	if (ci->ci_vfp_id == 0)
    453  1.13      matt 		/* No VFP detected, just fault.  */
    454  1.13      matt 		return 1;
    455  1.13      matt 
    456  1.13      matt 	if ((insn & 0xfe000000) != 0xf2000000
    457  1.13      matt 	    && (insn & 0xfe000000) != 0xf4000000)
    458  1.13      matt 		/* Not NEON instruction, just fault.  */
    459  1.13      matt 		return 1;
    460  1.13      matt 
    461  1.13      matt 	/* This shouldn't ever happen.  */
    462  1.13      matt 	if (fault_code != FAULT_USER)
    463  1.13      matt 		panic("NEON fault in non-user mode");
    464  1.13      matt 
    465  1.13      matt 	pcu_load(&arm_vfp_ops);
    466  1.13      matt 
    467  1.13      matt 	/* Need to restart the faulted instruction.  */
    468  1.13      matt //	frame->tf_pc -= INSN_SIZE;
    469  1.13      matt 	return 0;
    470  1.13      matt }
    471  1.13      matt #endif
    472  1.13      matt 
    473   1.4      matt static void
    474  1.13      matt vfp_state_load(lwp_t *l, u_int flags)
    475   1.4      matt {
    476   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    477  1.13      matt 
    478  1.13      matt 	KASSERT(flags & PCU_ENABLE);
    479  1.13      matt 
    480  1.13      matt 	if (flags & PCU_KERNEL) {
    481  1.13      matt 		if ((flags & PCU_LOADED) == 0) {
    482  1.13      matt 			pcb->pcb_kernel_vfp.vfp_fpexc = pcb->pcb_vfp.vfp_fpexc;
    483  1.13      matt 		}
    484  1.15      matt 		pcb->pcb_vfp.vfp_fpexc = VFP_FPEXC_EN;
    485  1.21      matt 		armreg_fpexc_write(pcb->pcb_vfp.vfp_fpexc);
    486  1.13      matt 		/*
    487  1.13      matt 		 * Load the kernel registers (just the first 16) if they've
    488  1.13      matt 		 * been used..
    489  1.13      matt 		 */
    490  1.13      matt 		if (flags & PCU_LOADED) {
    491  1.13      matt 			load_vfpregs_lo(pcb->pcb_kernel_vfp.vfp_regs);
    492  1.13      matt 		}
    493  1.13      matt 		return;
    494  1.13      matt 	}
    495   1.4      matt 	struct vfpreg * const fregs = &pcb->pcb_vfp;
    496   1.1  rearnsha 
    497   1.1  rearnsha 	/*
    498   1.1  rearnsha 	 * Instrument VFP usage -- if a process has not previously
    499   1.1  rearnsha 	 * used the VFP, mark it as having used VFP for the first time,
    500   1.1  rearnsha 	 * and count this event.
    501   1.1  rearnsha 	 *
    502   1.1  rearnsha 	 * If a process has used the VFP, count a "used VFP, and took
    503   1.1  rearnsha 	 * a trap to use it again" event.
    504   1.1  rearnsha 	 */
    505  1.25      matt 	if (__predict_false((flags & PCU_LOADED) == 0)) {
    506  1.33     skrll 		KASSERT(flags & PCU_RELOAD);
    507   1.1  rearnsha 		vfpevent_use.ev_count++;
    508  1.32     skrll 		pcb->pcb_vfp.vfp_fpscr =
    509  1.32     skrll 		    (VFP_FPSCR_DN | VFP_FPSCR_FZ | VFP_FPSCR_RN); /* Runfast */
    510   1.4      matt 	} else {
    511   1.1  rearnsha 		vfpevent_reuse.ev_count++;
    512   1.4      matt 	}
    513   1.1  rearnsha 
    514  1.33     skrll 	uint32_t fpexc = armreg_fpexc_read();
    515  1.33     skrll 	if (flags & PCU_RELOAD) {
    516  1.33     skrll 		bool enabled = fregs->vfp_fpexc & VFP_FPEXC_EN;
    517  1.33     skrll 
    518   1.4      matt 		/*
    519  1.33     skrll 		 * Load and Enable the VFP (so that we can write the
    520  1.33     skrll 		 * registers).
    521   1.4      matt 		 */
    522  1.33     skrll 		fregs->vfp_fpexc |= VFP_FPEXC_EN;
    523  1.21      matt 		armreg_fpexc_write(fregs->vfp_fpexc);
    524  1.33     skrll 		if (enabled) {
    525  1.33     skrll 			/*
    526  1.33     skrll 			 * If we think the VFP is enabled, it must have be
    527  1.33     skrll 			 * disabled by vfp_state_release for another LWP so
    528  1.33     skrll 			 * we can now just return.
    529  1.33     skrll 			 */
    530  1.33     skrll 			return;
    531  1.33     skrll 		}
    532  1.13      matt 
    533  1.13      matt 		load_vfpregs(fregs);
    534  1.21      matt 		armreg_fpscr_write(fregs->vfp_fpscr);
    535  1.13      matt 
    536  1.13      matt 		if (fregs->vfp_fpexc & VFP_FPEXC_EX) {
    537  1.13      matt 			/* Need to restore the exception handling state.  */
    538  1.21      matt 			armreg_fpinst2_write(fregs->vfp_fpinst2);
    539  1.21      matt 			if (fregs->vfp_fpexc & VFP_FPEXC_FP2V)
    540  1.21      matt 				armreg_fpinst_write(fregs->vfp_fpinst);
    541   1.1  rearnsha 		}
    542  1.33     skrll 	} else {
    543  1.33     skrll 		/*
    544  1.33     skrll 		 * If the VFP is already enabled we must be bouncing an
    545  1.33     skrll 		 * instruction.
    546  1.33     skrll 		 */
    547  1.33     skrll 		armreg_fpexc_write(fpexc | VFP_FPEXC_EN);
    548   1.1  rearnsha 	}
    549   1.1  rearnsha }
    550   1.1  rearnsha 
    551   1.1  rearnsha void
    552  1.13      matt vfp_state_save(lwp_t *l, u_int flags)
    553   1.1  rearnsha {
    554   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    555  1.21      matt 	uint32_t fpexc = armreg_fpexc_read();
    556  1.33     skrll 
    557  1.33     skrll 	/*
    558  1.33     skrll 	 * Enable the VFP (so we can read the registers).
    559  1.33     skrll 	 * Make sure the exception bit is cleared so that we can
    560  1.33     skrll 	 * safely dump the registers.
    561  1.33     skrll 	 */
    562  1.21      matt 	armreg_fpexc_write((fpexc | VFP_FPEXC_EN) & ~VFP_FPEXC_EX);
    563   1.1  rearnsha 
    564  1.13      matt 	if (flags & PCU_KERNEL) {
    565  1.13      matt 		/*
    566  1.13      matt 		 * Save the kernel set of VFP registers.
    567  1.13      matt 		 * (just the first 16).
    568  1.13      matt 		 */
    569  1.13      matt 		save_vfpregs_lo(pcb->pcb_kernel_vfp.vfp_regs);
    570   1.1  rearnsha 		return;
    571  1.13      matt 	}
    572  1.13      matt 
    573  1.13      matt 	struct vfpreg * const fregs = &pcb->pcb_vfp;
    574   1.1  rearnsha 
    575   1.4      matt 	fregs->vfp_fpexc = fpexc;
    576   1.4      matt 	if (fpexc & VFP_FPEXC_EX) {
    577   1.4      matt 		/* Need to save the exception handling state */
    578  1.21      matt 		fregs->vfp_fpinst = armreg_fpinst_read();
    579  1.21      matt 		if (fpexc & VFP_FPEXC_FP2V)
    580  1.21      matt 			fregs->vfp_fpinst2 = armreg_fpinst2_read();
    581   1.1  rearnsha 	}
    582  1.21      matt 	fregs->vfp_fpscr = armreg_fpscr_read();
    583  1.13      matt 	save_vfpregs(fregs);
    584   1.4      matt 
    585   1.1  rearnsha 	/* Disable the VFP.  */
    586  1.33     skrll 	armreg_fpexc_write(fpexc & ~VFP_FPEXC_EN);
    587   1.1  rearnsha }
    588   1.1  rearnsha 
    589   1.1  rearnsha void
    590  1.13      matt vfp_state_release(lwp_t *l, u_int flags)
    591   1.1  rearnsha {
    592   1.4      matt 	struct pcb * const pcb = lwp_getpcb(l);
    593   1.1  rearnsha 
    594  1.13      matt 	if (flags & PCU_KERNEL) {
    595  1.13      matt 		/*
    596  1.13      matt 		 * Restore the FPEXC since we borrowed that field.
    597  1.13      matt 		 */
    598  1.13      matt 		pcb->pcb_vfp.vfp_fpexc = pcb->pcb_kernel_vfp.vfp_fpexc;
    599  1.13      matt 	} else {
    600  1.13      matt 		/*
    601  1.13      matt 		 * Now mark the VFP as disabled (and our state
    602  1.13      matt 		 * has been already saved or is being discarded).
    603  1.13      matt 		 */
    604  1.13      matt 		pcb->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
    605  1.13      matt 	}
    606   1.1  rearnsha 
    607   1.1  rearnsha 	/*
    608   1.4      matt 	 * Turn off the FPU so the next time a VFP instruction is issued
    609   1.4      matt 	 * an exception happens.  We don't know if this LWP's state was
    610   1.4      matt 	 * loaded but if we turned off the FPU for some other LWP, when
    611   1.4      matt 	 * pcu_load invokes vfp_state_load it will see that VFP_FPEXC_EN
    612  1.13      matt 	 * is still set so it just restore fpexc and return since its
    613   1.4      matt 	 * contents are still sitting in the VFP.
    614   1.1  rearnsha 	 */
    615  1.21      matt 	armreg_fpexc_write(armreg_fpexc_read() & ~VFP_FPEXC_EN);
    616   1.1  rearnsha }
    617   1.1  rearnsha 
    618   1.1  rearnsha void
    619   1.2    cegger vfp_savecontext(void)
    620   1.1  rearnsha {
    621   1.4      matt 	pcu_save(&arm_vfp_ops);
    622   1.1  rearnsha }
    623   1.1  rearnsha 
    624   1.1  rearnsha void
    625  1.25      matt vfp_discardcontext(bool used_p)
    626   1.1  rearnsha {
    627  1.25      matt 	pcu_discard(&arm_vfp_ops, used_p);
    628  1.25      matt }
    629  1.25      matt 
    630  1.25      matt bool
    631  1.25      matt vfp_used_p(void)
    632  1.25      matt {
    633  1.25      matt 	return pcu_used_p(&arm_vfp_ops);
    634   1.4      matt }
    635   1.1  rearnsha 
    636   1.8      matt void
    637  1.13      matt vfp_kernel_acquire(void)
    638  1.13      matt {
    639  1.13      matt 	if (__predict_false(cpu_intr_p())) {
    640  1.21      matt 		armreg_fpexc_write(VFP_FPEXC_EN);
    641  1.13      matt 		if (curcpu()->ci_data.cpu_pcu_curlwp[PCU_FPU] != NULL) {
    642  1.13      matt 			lwp_t * const l = curlwp;
    643  1.13      matt 			struct pcb * const pcb = lwp_getpcb(l);
    644  1.13      matt 			KASSERT((l->l_md.md_flags & MDLWP_VFPINTR) == 0);
    645  1.13      matt 			l->l_md.md_flags |= MDLWP_VFPINTR;
    646  1.13      matt 			save_vfpregs_lo(&pcb->pcb_kernel_vfp.vfp_regs[16]);
    647  1.13      matt 		}
    648  1.13      matt 	} else {
    649  1.13      matt 		pcu_kernel_acquire(&arm_vfp_ops);
    650  1.13      matt 	}
    651  1.13      matt }
    652  1.13      matt 
    653  1.13      matt void
    654  1.13      matt vfp_kernel_release(void)
    655  1.13      matt {
    656  1.13      matt 	if (__predict_false(cpu_intr_p())) {
    657  1.13      matt 		uint32_t fpexc = 0;
    658  1.13      matt 		if (curcpu()->ci_data.cpu_pcu_curlwp[PCU_FPU] != NULL) {
    659  1.13      matt 			lwp_t * const l = curlwp;
    660  1.13      matt 			struct pcb * const pcb = lwp_getpcb(l);
    661  1.13      matt 			KASSERT(l->l_md.md_flags & MDLWP_VFPINTR);
    662  1.13      matt 			load_vfpregs_lo(&pcb->pcb_kernel_vfp.vfp_regs[16]);
    663  1.13      matt 			l->l_md.md_flags &= ~MDLWP_VFPINTR;
    664  1.13      matt 			fpexc = pcb->pcb_vfp.vfp_fpexc;
    665  1.13      matt 		}
    666  1.21      matt 		armreg_fpexc_write(fpexc);
    667  1.13      matt 	} else {
    668  1.13      matt 		pcu_kernel_release(&arm_vfp_ops);
    669  1.13      matt 	}
    670  1.13      matt }
    671  1.13      matt 
    672  1.13      matt void
    673   1.8      matt vfp_getcontext(struct lwp *l, mcontext_t *mcp, int *flagsp)
    674   1.8      matt {
    675  1.25      matt 	if (vfp_used_p()) {
    676   1.8      matt 		const struct pcb * const pcb = lwp_getpcb(l);
    677   1.8      matt 		pcu_save(&arm_vfp_ops);
    678   1.8      matt 		mcp->__fpu.__vfpregs.__vfp_fpscr = pcb->pcb_vfp.vfp_fpscr;
    679   1.8      matt 		memcpy(mcp->__fpu.__vfpregs.__vfp_fstmx, pcb->pcb_vfp.vfp_regs,
    680   1.8      matt 		    sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
    681  1.10      matt 		*flagsp |= _UC_FPU|_UC_ARM_VFP;
    682   1.8      matt 	}
    683   1.8      matt }
    684   1.8      matt 
    685   1.8      matt void
    686   1.8      matt vfp_setcontext(struct lwp *l, const mcontext_t *mcp)
    687   1.8      matt {
    688  1.24  drochner 	pcu_discard(&arm_vfp_ops, true);
    689   1.8      matt 	struct pcb * const pcb = lwp_getpcb(l);
    690   1.8      matt 	pcb->pcb_vfp.vfp_fpscr = mcp->__fpu.__vfpregs.__vfp_fpscr;
    691   1.8      matt 	memcpy(pcb->pcb_vfp.vfp_regs, mcp->__fpu.__vfpregs.__vfp_fstmx,
    692   1.8      matt 	    sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
    693   1.8      matt }
    694   1.8      matt 
    695   1.4      matt #endif /* FPU_VFP */
    696