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