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