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