Home | History | Annotate | Line # | Download | only in vfp
vfp_init.c revision 1.25
      1 /*      $NetBSD: vfp_init.c,v 1.25 2013/08/23 05:22:01 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(!vfp_used_p())) {
    195 		pcb->pcb_vfp.vfp_fpscr =
    196 		    (VFP_FPSCR_DN | VFP_FPSCR_FZ);	/* Runfast */
    197 	}
    198 
    199 	/*
    200 	 * We know know the pcb has the saved copy.
    201 	 */
    202 	register_t * const regp = &frame->tf_r0 + regno;
    203 	if (insn & 0x00100000) {
    204 		*regp = pcb->pcb_vfp.vfp_fpscr;
    205 	} else {
    206 		register_t tmp = *regp;
    207 		if (!(cpu_media_and_vfp_features[0] & ARM_MVFR0_EXCEPT_MASK))
    208 			tmp &= ~VFP_FPSCR_ESUM;
    209 		pcb->pcb_vfp.vfp_fpscr = tmp;
    210 	}
    211 
    212 	vfp_fpscr_ev.ev_count++;
    213 
    214 	frame->tf_pc += INSN_SIZE;
    215 	return 0;
    216 }
    217 
    218 #ifndef FPU_VFP
    219 /*
    220  * If we don't want VFP support, we still need to handle emulating VFP FPSCR
    221  * instructions.
    222  */
    223 void
    224 vfp_attach(void)
    225 {
    226 	install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    227 }
    228 
    229 #else
    230 #if 0
    231 static bool
    232 vfp_patch_branch(uintptr_t code, uintptr_t func, uintptr_t newfunc)
    233 {
    234 	for (;; code += sizeof(uint32_t)) {
    235 		uint32_t insn = *(uint32_t *)code;
    236 		if ((insn & 0xffd08000) == 0xe8908000)	/* ldm ... { pc } */
    237 			return false;
    238 		if ((insn & 0xfffffff0) == 0xe12fff10)	/* bx rN */
    239 			return false;
    240 		if ((insn & 0xf1a0f000) == 0xe1a0f000)	/* mov pc, ... */
    241 			return false;
    242 		if ((insn >> 25) != 0x75)		/* not b/bl insn */
    243 			continue;
    244 		intptr_t imm26 = ((int32_t)insn << 8) >> 6;
    245 		if (code + imm26 + 8 == func) {
    246 			int32_t imm24 = (newfunc - (code + 8)) >> 2;
    247 			uint32_t new_insn = (insn & 0xff000000)
    248 			   | (imm24 & 0xffffff);
    249 			KASSERTMSG((uint32_t)((imm24 >> 24) + 1) <= 1, "%x",
    250 			    ((imm24 >> 24) + 1));
    251 			*(uint32_t *)code = new_insn;
    252 			cpu_idcache_wbinv_range(code, sizeof(uint32_t));
    253 			return true;
    254 		}
    255 	}
    256 }
    257 #endif
    258 
    259 void
    260 vfp_attach(void)
    261 {
    262 	struct cpu_info * const ci = curcpu();
    263 	const char *model = NULL;
    264 	bool vfp_p = false;
    265 
    266 	if (CPU_ID_ARM11_P(curcpu()->ci_arm_cpuid)
    267 	    || CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)) {
    268 		const uint32_t cpacr_vfp = CPACR_CPn(VFP_COPROC);
    269 		const uint32_t cpacr_vfp2 = CPACR_CPn(VFP_COPROC2);
    270 
    271 		/*
    272 		 * We first need to enable access to the coprocessors.
    273 		 */
    274 		uint32_t cpacr = armreg_cpacr_read();
    275 		cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp);
    276 		cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp2);
    277 #if 0
    278 		if (CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid)) {
    279 			/*
    280 			 * Disable access to the upper 16 FP registers and NEON.
    281 			 */
    282 			cpacr |= CPACR_V7_D32DIS;
    283 			cpacr |= CPACR_V7_ASEDIS;
    284 		}
    285 #endif
    286 		armreg_cpacr_write(cpacr);
    287 
    288 		/*
    289 		 * If we could enable them, then they exist.
    290 		 */
    291 		cpacr = armreg_cpacr_read();
    292 		vfp_p = __SHIFTOUT(cpacr, cpacr_vfp2) != CPACR_NOACCESS
    293 		    || __SHIFTOUT(cpacr, cpacr_vfp) != CPACR_NOACCESS;
    294 	}
    295 
    296 	void *uh = install_coproc_handler(VFP_COPROC, vfp_test);
    297 
    298 	undefined_test = 0;
    299 
    300 	const uint32_t fpsid = armreg_fpsid_read();
    301 
    302 	remove_coproc_handler(uh);
    303 
    304 	if (undefined_test != 0) {
    305 		aprint_normal_dev(ci->ci_dev, "No VFP detected\n");
    306 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    307 		ci->ci_vfp_id = 0;
    308 		return;
    309 	}
    310 
    311 	ci->ci_vfp_id = fpsid;
    312 	switch (fpsid & ~ VFP_FPSID_REV_MSK) {
    313 	case FPU_VFP10_ARM10E:
    314 		model = "VFP10 R1";
    315 		break;
    316 	case FPU_VFP11_ARM11:
    317 		model = "VFP11";
    318 		break;
    319 	case FPU_VFP_CORTEXA5:
    320 	case FPU_VFP_CORTEXA7:
    321 	case FPU_VFP_CORTEXA8:
    322 	case FPU_VFP_CORTEXA9:
    323 	case FPU_VFP_CORTEXA15:
    324 		model = "NEON MPE (VFP 3.0+)";
    325 		cpu_neon_present = 1;
    326 		break;
    327 	default:
    328 		aprint_normal_dev(ci->ci_dev, "unrecognized VFP version %x\n",
    329 		    fpsid);
    330 		install_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
    331 		return;
    332 	}
    333 
    334 	cpu_fpu_present = 1;
    335 	cpu_media_and_vfp_features[0] = armreg_mvfr0_read();
    336 	cpu_media_and_vfp_features[1] = armreg_mvfr1_read();
    337 	if (fpsid != 0) {
    338 		aprint_normal("vfp%d at %s: %s\n",
    339 		    device_unit(curcpu()->ci_dev),
    340 		    device_xname(curcpu()->ci_dev),
    341 		    model);
    342 		aprint_verbose("vfp%d: mvfr: [0]=%#x [1]=%#x\n",
    343 		    device_unit(curcpu()->ci_dev),
    344 		    cpu_media_and_vfp_features[0],
    345 		    cpu_media_and_vfp_features[1]);
    346 	}
    347 	evcnt_attach_dynamic(&vfpevent_use, EVCNT_TYPE_MISC, NULL,
    348 	    "VFP", "coproc use");
    349 	evcnt_attach_dynamic(&vfpevent_reuse, EVCNT_TYPE_MISC, NULL,
    350 	    "VFP", "coproc re-use");
    351 	evcnt_attach_dynamic(&vfpevent_fpe, EVCNT_TYPE_TRAP, NULL,
    352 	    "VFP", "coproc fault");
    353 	install_coproc_handler(VFP_COPROC, vfp_handler);
    354 	install_coproc_handler(VFP_COPROC2, vfp_handler);
    355 #ifdef CPU_CORTEX
    356 	install_coproc_handler(CORE_UNKNOWN_HANDLER, neon_handler);
    357 #endif
    358 
    359 #if 0
    360 	vfp_patch_branch((uintptr_t)pmap_copy_page_generic,
    361 	   (uintptr_t)bcopy_page, (uintptr_t)bcopy_page_vfp);
    362 	vfp_patch_branch((uintptr_t)pmap_zero_page_generic,
    363 	   (uintptr_t)bzero_page, (uintptr_t)bzero_page_vfp);
    364 #endif
    365 }
    366 
    367 /* The real handler for VFP bounces.  */
    368 static int
    369 vfp_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    370 {
    371 	struct cpu_info * const ci = curcpu();
    372 
    373 	/* This shouldn't ever happen.  */
    374 	if (fault_code != FAULT_USER)
    375 		panic("VFP fault at %#x in non-user mode", frame->tf_pc);
    376 
    377 	if (ci->ci_vfp_id == 0)
    378 		/* No VFP detected, just fault.  */
    379 		return 1;
    380 
    381 	uint32_t fpexc = armreg_fpexc_read();
    382 	if (fpexc & VFP_FPEXC_EX) {
    383 		ksiginfo_t ksi;
    384 		KASSERT(fpexc & VFP_FPEXC_EN);
    385 
    386 		vfpevent_fpe.ev_count++;
    387 
    388 		pcu_save(&arm_vfp_ops);
    389 
    390 		/*
    391 		 * Need the clear the exception condition so any signal
    392 		 * can run.
    393 		 */
    394 		armreg_fpexc_write(fpexc & ~(VFP_FPEXC_EX|VFP_FPEXE_FSUM));
    395 
    396 		KSI_INIT_TRAP(&ksi);
    397 		ksi.ksi_signo = SIGFPE;
    398 		if (fpexc & VFP_FPEXC_IXF)
    399 			ksi.ksi_code = FPE_FLTRES;
    400 		else if (fpexc & VFP_FPEXC_UFF)
    401 			ksi.ksi_code = FPE_FLTUND;
    402 		else if (fpexc & VFP_FPEXC_OFF)
    403 			ksi.ksi_code = FPE_FLTOVF;
    404 		else if (fpexc & VFP_FPEXC_DZF)
    405 			ksi.ksi_code = FPE_FLTDIV;
    406 		else if (fpexc & VFP_FPEXC_IOF)
    407 			ksi.ksi_code = FPE_FLTINV;
    408 		ksi.ksi_addr = (uint32_t *)address;
    409 		ksi.ksi_trap = 0;
    410 		trapsignal(curlwp, &ksi);
    411 		return 0;
    412 	}
    413 
    414 	/*
    415 	 * If we are just changing/fetching FPSCR, don't bother loading it.
    416 	 */
    417 	if (!vfp_fpscr_handler(address, insn, frame, fault_code))
    418 		return 0;
    419 
    420 	pcu_load(&arm_vfp_ops);
    421 
    422 	/* Need to restart the faulted instruction.  */
    423 //	frame->tf_pc -= INSN_SIZE;
    424 	return 0;
    425 }
    426 
    427 #ifdef CPU_CORTEX
    428 /* The real handler for NEON bounces.  */
    429 static int
    430 neon_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
    431 {
    432 	struct cpu_info * const ci = curcpu();
    433 
    434 	if (ci->ci_vfp_id == 0)
    435 		/* No VFP detected, just fault.  */
    436 		return 1;
    437 
    438 	if ((insn & 0xfe000000) != 0xf2000000
    439 	    && (insn & 0xfe000000) != 0xf4000000)
    440 		/* Not NEON instruction, just fault.  */
    441 		return 1;
    442 
    443 	/* This shouldn't ever happen.  */
    444 	if (fault_code != FAULT_USER)
    445 		panic("NEON fault in non-user mode");
    446 
    447 	pcu_load(&arm_vfp_ops);
    448 
    449 	/* Need to restart the faulted instruction.  */
    450 //	frame->tf_pc -= INSN_SIZE;
    451 	return 0;
    452 }
    453 #endif
    454 
    455 static void
    456 vfp_state_load(lwp_t *l, u_int flags)
    457 {
    458 	struct pcb * const pcb = lwp_getpcb(l);
    459 
    460 	KASSERT(flags & PCU_ENABLE);
    461 
    462 	if (flags & PCU_KERNEL) {
    463 		if ((flags & PCU_LOADED) == 0) {
    464 			pcb->pcb_kernel_vfp.vfp_fpexc = pcb->pcb_vfp.vfp_fpexc;
    465 		}
    466 		pcb->pcb_vfp.vfp_fpexc = VFP_FPEXC_EN;
    467 		armreg_fpexc_write(pcb->pcb_vfp.vfp_fpexc);
    468 		/*
    469 		 * Load the kernel registers (just the first 16) if they've
    470 		 * been used..
    471 		 */
    472 		if (flags & PCU_LOADED) {
    473 			load_vfpregs_lo(pcb->pcb_kernel_vfp.vfp_regs);
    474 		}
    475 		return;
    476 	}
    477 	struct vfpreg * const fregs = &pcb->pcb_vfp;
    478 
    479 	/*
    480 	 * Instrument VFP usage -- if a process has not previously
    481 	 * used the VFP, mark it as having used VFP for the first time,
    482 	 * and count this event.
    483 	 *
    484 	 * If a process has used the VFP, count a "used VFP, and took
    485 	 * a trap to use it again" event.
    486 	 */
    487 	if (__predict_false((flags & PCU_LOADED) == 0)) {
    488 		vfpevent_use.ev_count++;
    489 		pcb->pcb_vfp.vfp_fpscr =	/* Runfast */
    490 		    (VFP_FPSCR_DN | VFP_FPSCR_FZ | VFP_FPSCR_RN);
    491 	} else {
    492 		vfpevent_reuse.ev_count++;
    493 	}
    494 
    495 	if (fregs->vfp_fpexc & VFP_FPEXC_EN) {
    496 		/*
    497 		 * If we think the VFP is enabled, it must have be disabled by
    498 		 * vfp_state_release for another LWP so we can just restore
    499 		 * FPEXC and return since our VFP state is still loaded.
    500 		 */
    501 		armreg_fpexc_write(fregs->vfp_fpexc);
    502 		return;
    503 	}
    504 
    505 	/* Load and Enable the VFP (so that we can write the registers).  */
    506 	if (flags & PCU_RELOAD) {
    507 		uint32_t fpexc = armreg_fpexc_read();
    508 		KDASSERT((fpexc & VFP_FPEXC_EX) == 0);
    509 		armreg_fpexc_write(fpexc | VFP_FPEXC_EN);
    510 
    511 		load_vfpregs(fregs);
    512 		armreg_fpscr_write(fregs->vfp_fpscr);
    513 
    514 		if (fregs->vfp_fpexc & VFP_FPEXC_EX) {
    515 			/* Need to restore the exception handling state.  */
    516 			armreg_fpinst2_write(fregs->vfp_fpinst2);
    517 			if (fregs->vfp_fpexc & VFP_FPEXC_FP2V)
    518 				armreg_fpinst_write(fregs->vfp_fpinst);
    519 		}
    520 	}
    521 
    522 	/* Finally, restore the FPEXC but don't enable the VFP. */
    523 	fregs->vfp_fpexc |= VFP_FPEXC_EN;
    524 	armreg_fpexc_write(fregs->vfp_fpexc);
    525 }
    526 
    527 void
    528 vfp_state_save(lwp_t *l, u_int flags)
    529 {
    530 	struct pcb * const pcb = lwp_getpcb(l);
    531 	uint32_t fpexc = armreg_fpexc_read();
    532 	armreg_fpexc_write((fpexc | VFP_FPEXC_EN) & ~VFP_FPEXC_EX);
    533 
    534 	if (flags & PCU_KERNEL) {
    535 		/*
    536 		 * Save the kernel set of VFP registers.
    537 		 * (just the first 16).
    538 		 */
    539 		save_vfpregs_lo(pcb->pcb_kernel_vfp.vfp_regs);
    540 		return;
    541 	}
    542 
    543 	struct vfpreg * const fregs = &pcb->pcb_vfp;
    544 
    545 	/*
    546 	 * Enable the VFP (so we can read the registers).
    547 	 * Make sure the exception bit is cleared so that we can
    548 	 * safely dump the registers.
    549 	 */
    550 	fregs->vfp_fpexc = fpexc;
    551 	if (fpexc & VFP_FPEXC_EX) {
    552 		/* Need to save the exception handling state */
    553 		fregs->vfp_fpinst = armreg_fpinst_read();
    554 		if (fpexc & VFP_FPEXC_FP2V)
    555 			fregs->vfp_fpinst2 = armreg_fpinst2_read();
    556 	}
    557 	fregs->vfp_fpscr = armreg_fpscr_read();
    558 	save_vfpregs(fregs);
    559 
    560 	/* Disable the VFP.  */
    561 	armreg_fpexc_write(fpexc);
    562 }
    563 
    564 void
    565 vfp_state_release(lwp_t *l, u_int flags)
    566 {
    567 	struct pcb * const pcb = lwp_getpcb(l);
    568 
    569 	if (flags & PCU_KERNEL) {
    570 		/*
    571 		 * Restore the FPEXC since we borrowed that field.
    572 		 */
    573 		pcb->pcb_vfp.vfp_fpexc = pcb->pcb_kernel_vfp.vfp_fpexc;
    574 	} else {
    575 		/*
    576 		 * Now mark the VFP as disabled (and our state
    577 		 * has been already saved or is being discarded).
    578 		 */
    579 		pcb->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
    580 	}
    581 
    582 	/*
    583 	 * Turn off the FPU so the next time a VFP instruction is issued
    584 	 * an exception happens.  We don't know if this LWP's state was
    585 	 * loaded but if we turned off the FPU for some other LWP, when
    586 	 * pcu_load invokes vfp_state_load it will see that VFP_FPEXC_EN
    587 	 * is still set so it just restore fpexc and return since its
    588 	 * contents are still sitting in the VFP.
    589 	 */
    590 	armreg_fpexc_write(armreg_fpexc_read() & ~VFP_FPEXC_EN);
    591 }
    592 
    593 void
    594 vfp_savecontext(void)
    595 {
    596 	pcu_save(&arm_vfp_ops);
    597 }
    598 
    599 void
    600 vfp_discardcontext(bool used_p)
    601 {
    602 	pcu_discard(&arm_vfp_ops, used_p);
    603 }
    604 
    605 bool
    606 vfp_used_p(void)
    607 {
    608 	return pcu_used_p(&arm_vfp_ops);
    609 }
    610 
    611 void
    612 vfp_kernel_acquire(void)
    613 {
    614 	if (__predict_false(cpu_intr_p())) {
    615 		armreg_fpexc_write(VFP_FPEXC_EN);
    616 		if (curcpu()->ci_data.cpu_pcu_curlwp[PCU_FPU] != NULL) {
    617 			lwp_t * const l = curlwp;
    618 			struct pcb * const pcb = lwp_getpcb(l);
    619 			KASSERT((l->l_md.md_flags & MDLWP_VFPINTR) == 0);
    620 			l->l_md.md_flags |= MDLWP_VFPINTR;
    621 			save_vfpregs_lo(&pcb->pcb_kernel_vfp.vfp_regs[16]);
    622 		}
    623 	} else {
    624 		pcu_kernel_acquire(&arm_vfp_ops);
    625 	}
    626 }
    627 
    628 void
    629 vfp_kernel_release(void)
    630 {
    631 	if (__predict_false(cpu_intr_p())) {
    632 		uint32_t fpexc = 0;
    633 		if (curcpu()->ci_data.cpu_pcu_curlwp[PCU_FPU] != NULL) {
    634 			lwp_t * const l = curlwp;
    635 			struct pcb * const pcb = lwp_getpcb(l);
    636 			KASSERT(l->l_md.md_flags & MDLWP_VFPINTR);
    637 			load_vfpregs_lo(&pcb->pcb_kernel_vfp.vfp_regs[16]);
    638 			l->l_md.md_flags &= ~MDLWP_VFPINTR;
    639 			fpexc = pcb->pcb_vfp.vfp_fpexc;
    640 		}
    641 		armreg_fpexc_write(fpexc);
    642 	} else {
    643 		pcu_kernel_release(&arm_vfp_ops);
    644 	}
    645 }
    646 
    647 void
    648 vfp_getcontext(struct lwp *l, mcontext_t *mcp, int *flagsp)
    649 {
    650 	if (vfp_used_p()) {
    651 		const struct pcb * const pcb = lwp_getpcb(l);
    652 		pcu_save(&arm_vfp_ops);
    653 		mcp->__fpu.__vfpregs.__vfp_fpscr = pcb->pcb_vfp.vfp_fpscr;
    654 		memcpy(mcp->__fpu.__vfpregs.__vfp_fstmx, pcb->pcb_vfp.vfp_regs,
    655 		    sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
    656 		*flagsp |= _UC_FPU|_UC_ARM_VFP;
    657 	}
    658 }
    659 
    660 void
    661 vfp_setcontext(struct lwp *l, const mcontext_t *mcp)
    662 {
    663 	pcu_discard(&arm_vfp_ops, true);
    664 	struct pcb * const pcb = lwp_getpcb(l);
    665 	pcb->pcb_vfp.vfp_fpscr = mcp->__fpu.__vfpregs.__vfp_fpscr;
    666 	memcpy(pcb->pcb_vfp.vfp_regs, mcp->__fpu.__vfpregs.__vfp_fstmx,
    667 	    sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
    668 }
    669 
    670 #endif /* FPU_VFP */
    671