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