1*040f3e2bSriastradh /* $NetBSD: vfp_init.c,v 1.78 2022/08/20 11:34:08 riastradh Exp $ */
2b45167b6Srearnsha
3b45167b6Srearnsha /*
4b45167b6Srearnsha * Copyright (c) 2008 ARM Ltd
5b45167b6Srearnsha * All rights reserved.
6b45167b6Srearnsha *
7b45167b6Srearnsha * Redistribution and use in source and binary forms, with or without
8b45167b6Srearnsha * modification, are permitted provided that the following conditions
9b45167b6Srearnsha * are met:
10b45167b6Srearnsha * 1. Redistributions of source code must retain the above copyright
11b45167b6Srearnsha * notice, this list of conditions and the following disclaimer.
12b45167b6Srearnsha * 2. Redistributions in binary form must reproduce the above copyright
13b45167b6Srearnsha * notice, this list of conditions and the following disclaimer in the
14b45167b6Srearnsha * documentation and/or other materials provided with the distribution.
15b45167b6Srearnsha * 3. The name of the company may not be used to endorse or promote
16b45167b6Srearnsha * products derived from this software without specific prior written
17b45167b6Srearnsha * permission.
18b45167b6Srearnsha *
19b45167b6Srearnsha * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR
20b45167b6Srearnsha * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21b45167b6Srearnsha * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22b45167b6Srearnsha * ARE DISCLAIMED. IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY
23b45167b6Srearnsha * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24b45167b6Srearnsha * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25b45167b6Srearnsha * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b45167b6Srearnsha * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27b45167b6Srearnsha * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28b45167b6Srearnsha * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29b45167b6Srearnsha * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30b45167b6Srearnsha */
31b45167b6Srearnsha
32fee210abSskrll #include "opt_cputypes.h"
33fee210abSskrll
3409029ecdSskrll #include <sys/cdefs.h>
35*040f3e2bSriastradh __KERNEL_RCSID(0, "$NetBSD: vfp_init.c,v 1.78 2022/08/20 11:34:08 riastradh Exp $");
3609029ecdSskrll
37b45167b6Srearnsha #include <sys/param.h>
38b45167b6Srearnsha #include <sys/types.h>
39b45167b6Srearnsha #include <sys/systm.h>
40b45167b6Srearnsha #include <sys/device.h>
41b451bcfcSriastradh #include <sys/kernel.h>
42764da9e9Sriastradh #include <sys/kthread.h>
43b45167b6Srearnsha #include <sys/proc.h>
44c58ada7bSmatt #include <sys/cpu.h>
45b45167b6Srearnsha
46ab152917Smatt #include <arm/locore.h>
47440251beSmatt #include <arm/pcb.h>
48b45167b6Srearnsha #include <arm/undefined.h>
49b45167b6Srearnsha #include <arm/vfpreg.h>
50cb72f657Smatt #include <arm/mcontext.h>
51c41eed1fSriastradh #include <arm/fpu.h>
52b45167b6Srearnsha
53bc0ae315Smatt #include <uvm/uvm_extern.h> /* for pmap.h */
54bc0ae315Smatt
55518b19bbSriastradh #include <crypto/aes/aes_impl.h>
560a776e17Sriastradh #include <crypto/aes/arch/arm/aes_neon.h>
57f7b532ddSriastradh #include <crypto/chacha/arch/arm/chacha_neon.h>
58f7b532ddSriastradh #include <crypto/chacha/chacha_impl.h>
590a776e17Sriastradh
60e34a352fSmatt #ifdef FPU_VFP
61e34a352fSmatt
62e1771516Smatt #ifdef CPU_CORTEX
632176986cSchristos #define SETFPU __asm(".fpu\tvfpv4")
64e1771516Smatt #else
652176986cSchristos #define SETFPU __asm(".fpu\tvfp")
66e1771516Smatt #endif
672176986cSchristos SETFPU;
68e1771516Smatt
69b45167b6Srearnsha /* FLDMD <X>, {d0-d15} */
70e34a352fSmatt static inline void
load_vfpregs_lo(const uint64_t * p)71adeb5a52Smatt load_vfpregs_lo(const uint64_t *p)
72a431007dSmatt {
732176986cSchristos SETFPU;
74b45d78c7Sjoerg __asm __volatile(".fpu vfp\n vldmia\t%0, {d0-d15}" :: "r" (p) : "memory");
75a431007dSmatt }
76a431007dSmatt
77a431007dSmatt /* FSTMD <X>, {d0-d15} */
78e34a352fSmatt static inline void
save_vfpregs_lo(uint64_t * p)79a431007dSmatt save_vfpregs_lo(uint64_t *p)
80a431007dSmatt {
812176986cSchristos SETFPU;
82b45d78c7Sjoerg __asm __volatile(".fpu vfp\n vstmia\t%0, {d0-d15}" :: "r" (p) : "memory");
83a431007dSmatt }
84a431007dSmatt
85a431007dSmatt #ifdef CPU_CORTEX
86a431007dSmatt /* FLDMD <X>, {d16-d31} */
87e34a352fSmatt static inline void
load_vfpregs_hi(const uint64_t * p)88adeb5a52Smatt load_vfpregs_hi(const uint64_t *p)
89a431007dSmatt {
902176986cSchristos SETFPU;
91b45d78c7Sjoerg __asm __volatile(".fpu neon-vfpv4\n vldmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
92a431007dSmatt }
93a431007dSmatt
94a431007dSmatt /* FLDMD <X>, {d16-d31} */
95e34a352fSmatt static inline void
save_vfpregs_hi(uint64_t * p)96a431007dSmatt save_vfpregs_hi(uint64_t *p)
97a431007dSmatt {
982176986cSchristos SETFPU;
99b45d78c7Sjoerg __asm __volatile(".fpu neon-vfpv4\nvstmia\t%0, {d16-d31}" :: "r" (&p[16]) : "memory");
100a431007dSmatt }
101a431007dSmatt #endif
102b45167b6Srearnsha
103adeb5a52Smatt static inline void
load_vfpregs(const struct vfpreg * fregs)104adeb5a52Smatt load_vfpregs(const struct vfpreg *fregs)
105adeb5a52Smatt {
106adeb5a52Smatt load_vfpregs_lo(fregs->vfp_regs);
107adeb5a52Smatt #ifdef CPU_CORTEX
108adeb5a52Smatt #ifdef CPU_ARM11
109adeb5a52Smatt switch (curcpu()->ci_vfp_id) {
110adeb5a52Smatt case FPU_VFP_CORTEXA5:
111adeb5a52Smatt case FPU_VFP_CORTEXA7:
112adeb5a52Smatt case FPU_VFP_CORTEXA8:
113adeb5a52Smatt case FPU_VFP_CORTEXA9:
114222a05d7Smatt case FPU_VFP_CORTEXA15:
115cddbe280Sslp case FPU_VFP_CORTEXA15_QEMU:
1168142c3d9Sskrll case FPU_VFP_CORTEXA53:
117da90d24cSjmcneill case FPU_VFP_CORTEXA57:
118adeb5a52Smatt #endif
119adeb5a52Smatt load_vfpregs_hi(fregs->vfp_regs);
120adeb5a52Smatt #ifdef CPU_ARM11
121adeb5a52Smatt break;
122adeb5a52Smatt }
123adeb5a52Smatt #endif
124adeb5a52Smatt #endif
125adeb5a52Smatt }
126adeb5a52Smatt
127adeb5a52Smatt static inline void
save_vfpregs(struct vfpreg * fregs)128adeb5a52Smatt save_vfpregs(struct vfpreg *fregs)
129adeb5a52Smatt {
130adeb5a52Smatt save_vfpregs_lo(fregs->vfp_regs);
131adeb5a52Smatt #ifdef CPU_CORTEX
132adeb5a52Smatt #ifdef CPU_ARM11
133adeb5a52Smatt switch (curcpu()->ci_vfp_id) {
134adeb5a52Smatt case FPU_VFP_CORTEXA5:
135adeb5a52Smatt case FPU_VFP_CORTEXA7:
136adeb5a52Smatt case FPU_VFP_CORTEXA8:
137adeb5a52Smatt case FPU_VFP_CORTEXA9:
138222a05d7Smatt case FPU_VFP_CORTEXA15:
139cddbe280Sslp case FPU_VFP_CORTEXA15_QEMU:
1408142c3d9Sskrll case FPU_VFP_CORTEXA53:
141da90d24cSjmcneill case FPU_VFP_CORTEXA57:
142adeb5a52Smatt #endif
143adeb5a52Smatt save_vfpregs_hi(fregs->vfp_regs);
144adeb5a52Smatt #ifdef CPU_ARM11
145adeb5a52Smatt break;
146adeb5a52Smatt }
147adeb5a52Smatt #endif
148adeb5a52Smatt #endif
149adeb5a52Smatt }
150adeb5a52Smatt
151b45167b6Srearnsha /* The real handler for VFP bounces. */
152b45167b6Srearnsha static int vfp_handler(u_int, u_int, trapframe_t *, int);
153adeb5a52Smatt #ifdef CPU_CORTEX
154adeb5a52Smatt static int neon_handler(u_int, u_int, trapframe_t *, int);
155adeb5a52Smatt #endif
156b45167b6Srearnsha
157adeb5a52Smatt static void vfp_state_load(lwp_t *, u_int);
158d67ab12cSrmind static void vfp_state_save(lwp_t *);
159d67ab12cSrmind static void vfp_state_release(lwp_t *);
160c58ada7bSmatt
161c58ada7bSmatt const pcu_ops_t arm_vfp_ops = {
162c58ada7bSmatt .pcu_id = PCU_FPU,
163c58ada7bSmatt .pcu_state_save = vfp_state_save,
164adeb5a52Smatt .pcu_state_load = vfp_state_load,
165c58ada7bSmatt .pcu_state_release = vfp_state_release,
166c58ada7bSmatt };
167b45167b6Srearnsha
16882ca3739Smatt /* determine what bits can be changed */
16982ca3739Smatt uint32_t vfp_fpscr_changable = VFP_FPSCR_CSUM;
17082ca3739Smatt /* default to run fast */
17182ca3739Smatt uint32_t vfp_fpscr_default = (VFP_FPSCR_DN | VFP_FPSCR_FZ | VFP_FPSCR_RN);
17282ca3739Smatt
17393bd3693Smatt #else
17493bd3693Smatt /* determine what bits can be changed */
17593bd3693Smatt uint32_t vfp_fpscr_changable = VFP_FPSCR_CSUM|VFP_FPSCR_ESUM|VFP_FPSCR_RMODE;
176c58ada7bSmatt #endif /* FPU_VFP */
177c58ada7bSmatt
178c58ada7bSmatt static int
vfp_fpscr_handler(u_int address,u_int insn,trapframe_t * frame,int fault_code)179c58ada7bSmatt vfp_fpscr_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
180c58ada7bSmatt {
181c58ada7bSmatt struct lwp * const l = curlwp;
182c58ada7bSmatt const u_int regno = (insn >> 12) & 0xf;
183c58ada7bSmatt /*
184c58ada7bSmatt * Only match move to/from the FPSCR register and we
185c58ada7bSmatt * can't be using the SP,LR,PC as a source.
186c58ada7bSmatt */
187c58ada7bSmatt if ((insn & 0xffef0fff) != 0xeee10a10 || regno > 12)
188c58ada7bSmatt return 1;
189c58ada7bSmatt
190c58ada7bSmatt struct pcb * const pcb = lwp_getpcb(l);
191c58ada7bSmatt
192c58ada7bSmatt #ifdef FPU_VFP
193c58ada7bSmatt /*
194c58ada7bSmatt * If FPU is valid somewhere, let's just reenable VFP and
195c58ada7bSmatt * retry the instruction (only safe thing to do since the
196c58ada7bSmatt * pcb has a stale copy).
197c58ada7bSmatt */
198c58ada7bSmatt if (pcb->pcb_vfp.vfp_fpexc & VFP_FPEXC_EN)
199c58ada7bSmatt return 1;
200c58ada7bSmatt
201877a3ccfSchs if (__predict_false(!vfp_used_p(l))) {
20293bd3693Smatt pcb->pcb_vfp.vfp_fpscr = vfp_fpscr_default;
203c58ada7bSmatt }
2040138e9e5Smatt #endif
205c58ada7bSmatt
206c58ada7bSmatt /*
207194648c1Sskrll * We now know the pcb has the saved copy.
208c58ada7bSmatt */
209c58ada7bSmatt register_t * const regp = &frame->tf_r0 + regno;
210c58ada7bSmatt if (insn & 0x00100000) {
211c58ada7bSmatt *regp = pcb->pcb_vfp.vfp_fpscr;
212c58ada7bSmatt } else {
21382ca3739Smatt pcb->pcb_vfp.vfp_fpscr &= ~vfp_fpscr_changable;
21482ca3739Smatt pcb->pcb_vfp.vfp_fpscr |= *regp & vfp_fpscr_changable;
215c58ada7bSmatt }
216c58ada7bSmatt
217d6e299a9Smatt curcpu()->ci_vfp_evs[0].ev_count++;
218c58ada7bSmatt
219c58ada7bSmatt frame->tf_pc += INSN_SIZE;
220c58ada7bSmatt return 0;
221c58ada7bSmatt }
222c58ada7bSmatt
223c58ada7bSmatt #ifndef FPU_VFP
224e677f16fSskrll void
vfp_detect(struct cpu_info * ci)225e677f16fSskrll vfp_detect(struct cpu_info *ci)
226e677f16fSskrll {
227e677f16fSskrll ci->ci_vfp_id = 0;
228e677f16fSskrll return;
229e677f16fSskrll }
230c58ada7bSmatt /*
231c58ada7bSmatt * If we don't want VFP support, we still need to handle emulating VFP FPSCR
232c58ada7bSmatt * instructions.
233c58ada7bSmatt */
234b45167b6Srearnsha void
vfp_attach(struct cpu_info * ci)235d6e299a9Smatt vfp_attach(struct cpu_info *ci)
236b45167b6Srearnsha {
237d6e299a9Smatt if (CPU_IS_PRIMARY(ci)) {
238e677f16fSskrll replace_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
239c58ada7bSmatt }
240d6e299a9Smatt evcnt_attach_dynamic(&ci->ci_vfp_evs[0], EVCNT_TYPE_TRAP, NULL,
241d6e299a9Smatt ci->ci_cpuname, "vfp fpscr traps");
242d6e299a9Smatt }
243c58ada7bSmatt
244c58ada7bSmatt #else
245c58ada7bSmatt void
vfp_detect(struct cpu_info * ci)246e677f16fSskrll vfp_detect(struct cpu_info *ci)
247c58ada7bSmatt {
248b45167b6Srearnsha
249d6e299a9Smatt if (CPU_ID_ARM11_P(ci->ci_arm_cpuid)
250d6e299a9Smatt || CPU_ID_MV88SV58XX_P(ci->ci_arm_cpuid)
251d6e299a9Smatt || CPU_ID_CORTEX_P(ci->ci_arm_cpuid)) {
252d6e299a9Smatt #if 0
253d6e299a9Smatt const uint32_t nsacr = armreg_nsacr_read();
254d6e299a9Smatt const uint32_t nsacr_vfp = __BITS(VFP_COPROC,VFP_COPROC2);
255d6e299a9Smatt if ((nsacr & nsacr_vfp) != nsacr_vfp) {
256e677f16fSskrll ci->ci_fp_id = 0;
257d6e299a9Smatt return;
258d6e299a9Smatt }
259d6e299a9Smatt #endif
260c872068cSmatt const uint32_t cpacr_vfp = CPACR_CPn(VFP_COPROC);
261c872068cSmatt const uint32_t cpacr_vfp2 = CPACR_CPn(VFP_COPROC2);
262df4c4d2cSmatt
263c872068cSmatt /*
264c872068cSmatt * We first need to enable access to the coprocessors.
265c872068cSmatt */
266c872068cSmatt uint32_t cpacr = armreg_cpacr_read();
267c872068cSmatt cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp);
268c872068cSmatt cpacr |= __SHIFTIN(CPACR_ALL, cpacr_vfp2);
269df4c4d2cSmatt armreg_cpacr_write(cpacr);
270c872068cSmatt
271ce993bccSskrll isb();
272fd24bc2eSjmcneill
273c872068cSmatt /*
274c872068cSmatt * If we could enable them, then they exist.
275c872068cSmatt */
276df4c4d2cSmatt cpacr = armreg_cpacr_read();
2773397f5bdSmatt bool vfp_p = __SHIFTOUT(cpacr, cpacr_vfp2) == CPACR_ALL
2783397f5bdSmatt && __SHIFTOUT(cpacr, cpacr_vfp) == CPACR_ALL;
2796dbb00d8Smatt if (!vfp_p) {
2806dbb00d8Smatt ci->ci_vfp_id = 0;
2816dbb00d8Smatt return;
2826dbb00d8Smatt }
283df4c4d2cSmatt }
284df4c4d2cSmatt
285e677f16fSskrll /* borrow the ci_vfd_id field for VFP detection */
286e677f16fSskrll ci->ci_vfp_id = -1;
287c872068cSmatt
2881eb5d070Smatt const uint32_t fpsid = armreg_fpsid_read();
289e677f16fSskrll if (ci->ci_vfp_id == 0) {
290b45167b6Srearnsha return;
291b45167b6Srearnsha }
292b45167b6Srearnsha
293c58ada7bSmatt ci->ci_vfp_id = fpsid;
294e677f16fSskrll
295e677f16fSskrll ci->ci_mvfr[0] = armreg_mvfr0_read();
296e677f16fSskrll ci->ci_mvfr[1] = armreg_mvfr1_read();
297e677f16fSskrll
298e677f16fSskrll }
299e677f16fSskrll
300e677f16fSskrll void
vfp_attach(struct cpu_info * ci)301e677f16fSskrll vfp_attach(struct cpu_info *ci)
302e677f16fSskrll {
303e677f16fSskrll const char *model = NULL;
304e677f16fSskrll
305e677f16fSskrll switch (ci->ci_vfp_id & ~ VFP_FPSID_REV_MSK) {
306b45167b6Srearnsha case FPU_VFP10_ARM10E:
307b45167b6Srearnsha model = "VFP10 R1";
308b45167b6Srearnsha break;
309b45167b6Srearnsha case FPU_VFP11_ARM11:
310b45167b6Srearnsha model = "VFP11";
311b45167b6Srearnsha break;
312df768d50Smatt case FPU_VFP_MV88SV58XX:
313df768d50Smatt model = "VFP3";
314df768d50Smatt break;
315c872068cSmatt case FPU_VFP_CORTEXA5:
316c872068cSmatt case FPU_VFP_CORTEXA7:
317c872068cSmatt case FPU_VFP_CORTEXA8:
318c872068cSmatt case FPU_VFP_CORTEXA9:
31926001a86Stnn case FPU_VFP_CORTEXA12:
320222a05d7Smatt case FPU_VFP_CORTEXA15:
321cddbe280Sslp case FPU_VFP_CORTEXA15_QEMU:
32226001a86Stnn case FPU_VFP_CORTEXA17:
3238142c3d9Sskrll case FPU_VFP_CORTEXA53:
324da90d24cSjmcneill case FPU_VFP_CORTEXA57:
325d6e299a9Smatt if (armreg_cpacr_read() & CPACR_V7_ASEDIS) {
326d6e299a9Smatt model = "VFP 4.0+";
327d6e299a9Smatt } else {
328c872068cSmatt model = "NEON MPE (VFP 3.0+)";
32985a02f38Smatt cpu_neon_present = 1;
330d6e299a9Smatt }
331df4c4d2cSmatt break;
332b45167b6Srearnsha default:
333df768d50Smatt aprint_normal_dev(ci->ci_dev, "unrecognized VFP version %#x\n",
334e677f16fSskrll ci->ci_vfp_id);
335a4e709f7Sskrll if (CPU_IS_PRIMARY(ci))
336e677f16fSskrll replace_coproc_handler(VFP_COPROC, vfp_fpscr_handler);
33793bd3693Smatt vfp_fpscr_changable = VFP_FPSCR_CSUM|VFP_FPSCR_ESUM
33893bd3693Smatt |VFP_FPSCR_RMODE;
33993bd3693Smatt vfp_fpscr_default = 0;
340b45167b6Srearnsha return;
341b45167b6Srearnsha }
342b45167b6Srearnsha
3432d523d35Smatt cpu_fpu_present = 1;
344e677f16fSskrll
345e677f16fSskrll const uint32_t f0 = ci->ci_mvfr[0];
346e677f16fSskrll const uint32_t f1 = ci->ci_mvfr[1];
34782ca3739Smatt aprint_normal("vfp%d at %s: %s%s%s%s%s\n",
348d6e299a9Smatt device_unit(ci->ci_dev),
349d6e299a9Smatt device_xname(ci->ci_dev),
35082ca3739Smatt model,
35182ca3739Smatt ((f0 & ARM_MVFR0_ROUNDING_MASK) ? ", rounding" : ""),
35282ca3739Smatt ((f0 & ARM_MVFR0_EXCEPT_MASK) ? ", exceptions" : ""),
35307ddce23Smatt ((f1 & ARM_MVFR1_D_NAN_MASK) ? ", NaN propagation" : ""),
35482ca3739Smatt ((f1 & ARM_MVFR1_FTZ_MASK) ? ", denormals" : ""));
355e677f16fSskrll
3567017cd79Sjmcneill aprint_debug("vfp%d: mvfr: [0]=%#x [1]=%#x\n",
357d6e299a9Smatt device_unit(ci->ci_dev), f0, f1);
358e677f16fSskrll
359d6e299a9Smatt if (CPU_IS_PRIMARY(ci)) {
360e677f16fSskrll cpu_media_and_vfp_features[0] = f0;
361e677f16fSskrll cpu_media_and_vfp_features[1] = f1;
362e677f16fSskrll
363d6e299a9Smatt if (f0 & ARM_MVFR0_ROUNDING_MASK) {
36482ca3739Smatt vfp_fpscr_changable |= VFP_FPSCR_RMODE;
36582ca3739Smatt }
366d6e299a9Smatt if (f1 & ARM_MVFR0_EXCEPT_MASK) {
36782ca3739Smatt vfp_fpscr_changable |= VFP_FPSCR_ESUM;
36882ca3739Smatt }
36907ddce23Smatt // If hardware supports propagation of NaNs, select it.
370d6e299a9Smatt if (f1 & ARM_MVFR1_D_NAN_MASK) {
37182ca3739Smatt vfp_fpscr_default &= ~VFP_FPSCR_DN;
37282ca3739Smatt vfp_fpscr_changable |= VFP_FPSCR_DN;
37382ca3739Smatt }
37482ca3739Smatt // If hardware supports denormalized numbers, use it.
375e677f16fSskrll if (f1 & ARM_MVFR1_FTZ_MASK) {
37682ca3739Smatt vfp_fpscr_default &= ~VFP_FPSCR_FZ;
37782ca3739Smatt vfp_fpscr_changable |= VFP_FPSCR_FZ;
37882ca3739Smatt }
379e677f16fSskrll
380e677f16fSskrll replace_coproc_handler(VFP_COPROC, vfp_handler);
381b45167b6Srearnsha install_coproc_handler(VFP_COPROC2, vfp_handler);
382adeb5a52Smatt #ifdef CPU_CORTEX
3830a776e17Sriastradh if (cpu_neon_present) {
3840a776e17Sriastradh install_coproc_handler(CORE_UNKNOWN_HANDLER,
3850a776e17Sriastradh neon_handler);
3860a776e17Sriastradh aes_md_init(&aes_neon_impl);
387f7b532ddSriastradh chacha_md_init(&chacha_neon_impl);
3880a776e17Sriastradh }
389adeb5a52Smatt #endif
390b45167b6Srearnsha }
391e677f16fSskrll
392e677f16fSskrll evcnt_attach_dynamic(&ci->ci_vfp_evs[0], EVCNT_TYPE_MISC, NULL,
393e677f16fSskrll ci->ci_cpuname, "vfp coproc use");
394e677f16fSskrll evcnt_attach_dynamic(&ci->ci_vfp_evs[1], EVCNT_TYPE_MISC, NULL,
395e677f16fSskrll ci->ci_cpuname, "vfp coproc re-use");
396e677f16fSskrll evcnt_attach_dynamic(&ci->ci_vfp_evs[2], EVCNT_TYPE_TRAP, NULL,
397e677f16fSskrll ci->ci_cpuname, "vfp coproc fault");
398a4e709f7Sskrll }
399b45167b6Srearnsha
400b45167b6Srearnsha /* The real handler for VFP bounces. */
401c58ada7bSmatt static int
vfp_handler(u_int address,u_int insn,trapframe_t * frame,int fault_code)4021eb5d070Smatt vfp_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
403b45167b6Srearnsha {
404c58ada7bSmatt struct cpu_info * const ci = curcpu();
40578e37e73Srin uint32_t fpexc;
406b45167b6Srearnsha
407b45167b6Srearnsha /* This shouldn't ever happen. */
408764da9e9Sriastradh if (fault_code != FAULT_USER &&
409764da9e9Sriastradh (curlwp->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) == LW_SYSTEM)
41008e4b731Smatt panic("VFP fault at %#x in non-user mode", frame->tf_pc);
411b45167b6Srearnsha
412d1068917Smatt if (ci->ci_vfp_id == 0) {
413b45167b6Srearnsha /* No VFP detected, just fault. */
414b45167b6Srearnsha return 1;
415d1068917Smatt }
416d1068917Smatt
417d1068917Smatt /*
41871dae66cSmatt * If we already own the FPU and it's enabled (and no exception), raise
41978e37e73Srin * SIGILL. If there is an exception, raise SIGFPE.
42071dae66cSmatt */
42178e37e73Srin if (curlwp->l_pcu_cpu[PCU_FPU] == ci) {
422257ebab6Srin KASSERT(ci->ci_pcu_curlwp[PCU_FPU] == curlwp);
42378e37e73Srin
42478e37e73Srin fpexc = armreg_fpexc_read();
42578e37e73Srin if (fpexc & VFP_FPEXC_EN) {
42678e37e73Srin if ((fpexc & VFP_FPEXC_EX) == 0) {
42778e37e73Srin return 1; /* SIGILL */
42878e37e73Srin } else {
42978e37e73Srin goto fpe; /* SIGFPE; skip pcu_load(9) */
43078e37e73Srin }
43178e37e73Srin }
432257ebab6Srin }
43371dae66cSmatt
43471dae66cSmatt /*
435d1068917Smatt * Make sure we own the FP.
436d1068917Smatt */
437d1068917Smatt pcu_load(&arm_vfp_ops);
438b45167b6Srearnsha
43978e37e73Srin fpexc = armreg_fpexc_read();
4401eb5d070Smatt if (fpexc & VFP_FPEXC_EX) {
4411eb5d070Smatt ksiginfo_t ksi;
4421eb5d070Smatt KASSERT(fpexc & VFP_FPEXC_EN);
4431eb5d070Smatt
44478e37e73Srin fpe:
445d6e299a9Smatt curcpu()->ci_vfp_evs[2].ev_count++;
4461eb5d070Smatt
4471eb5d070Smatt /*
4481eb5d070Smatt * Need the clear the exception condition so any signal
4499bcaadefSskrll * and future use can proceed.
4501eb5d070Smatt */
4517ca5df3eSskrll armreg_fpexc_write(fpexc & ~(VFP_FPEXC_EX|VFP_FPEXC_FSUM));
4521eb5d070Smatt
453877a3ccfSchs pcu_save(&arm_vfp_ops, curlwp);
4549bcaadefSskrll
4559bcaadefSskrll /*
4569bcaadefSskrll * XXX Need to emulate bounce instructions here to get correct
4579bcaadefSskrll * XXX exception codes, etc.
4589bcaadefSskrll */
4591eb5d070Smatt KSI_INIT_TRAP(&ksi);
4601eb5d070Smatt ksi.ksi_signo = SIGFPE;
4611eb5d070Smatt if (fpexc & VFP_FPEXC_IXF)
4621eb5d070Smatt ksi.ksi_code = FPE_FLTRES;
4631eb5d070Smatt else if (fpexc & VFP_FPEXC_UFF)
4641eb5d070Smatt ksi.ksi_code = FPE_FLTUND;
4651eb5d070Smatt else if (fpexc & VFP_FPEXC_OFF)
4661eb5d070Smatt ksi.ksi_code = FPE_FLTOVF;
4671eb5d070Smatt else if (fpexc & VFP_FPEXC_DZF)
4681eb5d070Smatt ksi.ksi_code = FPE_FLTDIV;
4691eb5d070Smatt else if (fpexc & VFP_FPEXC_IOF)
4701eb5d070Smatt ksi.ksi_code = FPE_FLTINV;
4711eb5d070Smatt ksi.ksi_addr = (uint32_t *)address;
4721eb5d070Smatt ksi.ksi_trap = 0;
4731eb5d070Smatt trapsignal(curlwp, &ksi);
4741eb5d070Smatt return 0;
4751eb5d070Smatt }
4761eb5d070Smatt
477c58ada7bSmatt /* Need to restart the faulted instruction. */
478c58ada7bSmatt // frame->tf_pc -= INSN_SIZE;
479c58ada7bSmatt return 0;
480b45167b6Srearnsha }
481b45167b6Srearnsha
482adeb5a52Smatt #ifdef CPU_CORTEX
483adeb5a52Smatt /* The real handler for NEON bounces. */
484adeb5a52Smatt static int
neon_handler(u_int address,u_int insn,trapframe_t * frame,int fault_code)4851eb5d070Smatt neon_handler(u_int address, u_int insn, trapframe_t *frame, int fault_code)
486adeb5a52Smatt {
487adeb5a52Smatt struct cpu_info * const ci = curcpu();
488adeb5a52Smatt
489adeb5a52Smatt if (ci->ci_vfp_id == 0)
490adeb5a52Smatt /* No VFP detected, just fault. */
491adeb5a52Smatt return 1;
492adeb5a52Smatt
493adeb5a52Smatt if ((insn & 0xfe000000) != 0xf2000000
494adeb5a52Smatt && (insn & 0xfe000000) != 0xf4000000)
495adeb5a52Smatt /* Not NEON instruction, just fault. */
496adeb5a52Smatt return 1;
497adeb5a52Smatt
498adeb5a52Smatt /* This shouldn't ever happen. */
499764da9e9Sriastradh if (fault_code != FAULT_USER &&
500764da9e9Sriastradh (curlwp->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) == LW_SYSTEM)
501adeb5a52Smatt panic("NEON fault in non-user mode");
502adeb5a52Smatt
503f50d0b7fSmatt /* if we already own the FPU and it's enabled, raise SIGILL */
504f50d0b7fSmatt if (curcpu()->ci_pcu_curlwp[PCU_FPU] == curlwp
505f50d0b7fSmatt && (armreg_fpexc_read() & VFP_FPEXC_EN) != 0)
50671dae66cSmatt return 1;
507fd405b92Smatt
508adeb5a52Smatt pcu_load(&arm_vfp_ops);
509adeb5a52Smatt
510adeb5a52Smatt /* Need to restart the faulted instruction. */
511adeb5a52Smatt // frame->tf_pc -= INSN_SIZE;
512adeb5a52Smatt return 0;
513adeb5a52Smatt }
514adeb5a52Smatt #endif
515adeb5a52Smatt
516c58ada7bSmatt static void
vfp_state_load(lwp_t * l,u_int flags)517adeb5a52Smatt vfp_state_load(lwp_t *l, u_int flags)
518c58ada7bSmatt {
519c58ada7bSmatt struct pcb * const pcb = lwp_getpcb(l);
520c58ada7bSmatt struct vfpreg * const fregs = &pcb->pcb_vfp;
521b45167b6Srearnsha
522b45167b6Srearnsha /*
523b45167b6Srearnsha * Instrument VFP usage -- if a process has not previously
524b45167b6Srearnsha * used the VFP, mark it as having used VFP for the first time,
525b45167b6Srearnsha * and count this event.
526b45167b6Srearnsha *
527b45167b6Srearnsha * If a process has used the VFP, count a "used VFP, and took
528b45167b6Srearnsha * a trap to use it again" event.
529b45167b6Srearnsha */
530d67ab12cSrmind if (__predict_false((flags & PCU_VALID) == 0)) {
531d6e299a9Smatt curcpu()->ci_vfp_evs[0].ev_count++;
53282ca3739Smatt pcb->pcb_vfp.vfp_fpscr = vfp_fpscr_default;
533c58ada7bSmatt } else {
534d6e299a9Smatt curcpu()->ci_vfp_evs[1].ev_count++;
535b45167b6Srearnsha }
536b45167b6Srearnsha
53736e1796fSbouyer KASSERT((armreg_fpexc_read() & VFP_FPEXC_EN) == 0);
538d67ab12cSrmind /*
539d67ab12cSrmind * If the VFP is already enabled we must be bouncing an instruction.
540d67ab12cSrmind */
541d67ab12cSrmind if (flags & PCU_REENABLE) {
5429bcaadefSskrll uint32_t fpexc = armreg_fpexc_read();
543d67ab12cSrmind armreg_fpexc_write(fpexc | VFP_FPEXC_EN);
54436e1796fSbouyer fregs->vfp_fpexc |= VFP_FPEXC_EN;
545d67ab12cSrmind return;
546d67ab12cSrmind }
54736e1796fSbouyer KASSERT((fregs->vfp_fpexc & VFP_FPEXC_EN) == 0);
5489bcaadefSskrll
549c58ada7bSmatt /*
550d67ab12cSrmind * Load and Enable the VFP (so that we can write the registers).
551c58ada7bSmatt */
5529bcaadefSskrll fregs->vfp_fpexc |= VFP_FPEXC_EN;
5531eb5d070Smatt armreg_fpexc_write(fregs->vfp_fpexc);
55436e1796fSbouyer KASSERT(curcpu()->ci_pcu_curlwp[PCU_FPU] == NULL);
55536e1796fSbouyer KASSERT(l->l_pcu_cpu[PCU_FPU] == NULL);
556b45167b6Srearnsha
557adeb5a52Smatt load_vfpregs(fregs);
5581eb5d070Smatt armreg_fpscr_write(fregs->vfp_fpscr);
559c58ada7bSmatt
560b45167b6Srearnsha if (fregs->vfp_fpexc & VFP_FPEXC_EX) {
561b45167b6Srearnsha /* Need to restore the exception handling state. */
5621eb5d070Smatt armreg_fpinst_write(fregs->vfp_fpinst);
563ea6bb78fSchs if (fregs->vfp_fpexc & VFP_FPEXC_FP2V)
564ea6bb78fSchs armreg_fpinst2_write(fregs->vfp_fpinst2);
565b45167b6Srearnsha }
566b45167b6Srearnsha }
567b45167b6Srearnsha
568b45167b6Srearnsha void
vfp_state_save(lwp_t * l)569d67ab12cSrmind vfp_state_save(lwp_t *l)
570b45167b6Srearnsha {
571c58ada7bSmatt struct pcb * const pcb = lwp_getpcb(l);
572d67ab12cSrmind struct vfpreg * const fregs = &pcb->pcb_vfp;
5731eb5d070Smatt uint32_t fpexc = armreg_fpexc_read();
5749bcaadefSskrll
57536e1796fSbouyer KASSERT(curcpu()->ci_pcu_curlwp[PCU_FPU] == l);
57636e1796fSbouyer KASSERT(curcpu() == l->l_pcu_cpu[PCU_FPU]);
57736e1796fSbouyer KASSERT(curlwp == l || curlwp->l_pcu_cpu[PCU_FPU] != curcpu());
5789bcaadefSskrll /*
5799bcaadefSskrll * Enable the VFP (so we can read the registers).
5809bcaadefSskrll * Make sure the exception bit is cleared so that we can
5819bcaadefSskrll * safely dump the registers.
5829bcaadefSskrll */
5831eb5d070Smatt armreg_fpexc_write((fpexc | VFP_FPEXC_EN) & ~VFP_FPEXC_EX);
584b45167b6Srearnsha
585b45167b6Srearnsha fregs->vfp_fpexc = fpexc;
586b45167b6Srearnsha if (fpexc & VFP_FPEXC_EX) {
587b45167b6Srearnsha /* Need to save the exception handling state */
5881eb5d070Smatt fregs->vfp_fpinst = armreg_fpinst_read();
5891eb5d070Smatt if (fpexc & VFP_FPEXC_FP2V)
5901eb5d070Smatt fregs->vfp_fpinst2 = armreg_fpinst2_read();
591b45167b6Srearnsha }
5921eb5d070Smatt fregs->vfp_fpscr = armreg_fpscr_read();
593adeb5a52Smatt save_vfpregs(fregs);
594b45167b6Srearnsha
595c58ada7bSmatt /* Disable the VFP. */
5969bcaadefSskrll armreg_fpexc_write(fpexc & ~VFP_FPEXC_EN);
597b45167b6Srearnsha }
598b45167b6Srearnsha
599b45167b6Srearnsha void
vfp_state_release(lwp_t * l)600d67ab12cSrmind vfp_state_release(lwp_t *l)
601b45167b6Srearnsha {
602c58ada7bSmatt struct pcb * const pcb = lwp_getpcb(l);
603b45167b6Srearnsha
604adeb5a52Smatt /*
605adeb5a52Smatt * Now mark the VFP as disabled (and our state
606adeb5a52Smatt * has been already saved or is being discarded).
607b45167b6Srearnsha */
608c58ada7bSmatt pcb->pcb_vfp.vfp_fpexc &= ~VFP_FPEXC_EN;
609c58ada7bSmatt
610c58ada7bSmatt /*
611c58ada7bSmatt * Turn off the FPU so the next time a VFP instruction is issued
612c58ada7bSmatt * an exception happens. We don't know if this LWP's state was
613c58ada7bSmatt * loaded but if we turned off the FPU for some other LWP, when
614c58ada7bSmatt * pcu_load invokes vfp_state_load it will see that VFP_FPEXC_EN
615adeb5a52Smatt * is still set so it just restore fpexc and return since its
616c58ada7bSmatt * contents are still sitting in the VFP.
617c58ada7bSmatt */
6181eb5d070Smatt armreg_fpexc_write(armreg_fpexc_read() & ~VFP_FPEXC_EN);
619b45167b6Srearnsha }
620b45167b6Srearnsha
621b45167b6Srearnsha void
vfp_savecontext(lwp_t * l)622877a3ccfSchs vfp_savecontext(lwp_t *l)
623b45167b6Srearnsha {
624877a3ccfSchs pcu_save(&arm_vfp_ops, l);
625b45167b6Srearnsha }
626b45167b6Srearnsha
627b45167b6Srearnsha void
vfp_discardcontext(lwp_t * l,bool used_p)628877a3ccfSchs vfp_discardcontext(lwp_t *l, bool used_p)
629b45167b6Srearnsha {
630877a3ccfSchs pcu_discard(&arm_vfp_ops, l, used_p);
6312efaa8f8Smatt }
6322efaa8f8Smatt
6332efaa8f8Smatt bool
vfp_used_p(const lwp_t * l)634877a3ccfSchs vfp_used_p(const lwp_t *l)
6352efaa8f8Smatt {
636877a3ccfSchs return pcu_valid_p(&arm_vfp_ops, l);
637adeb5a52Smatt }
638adeb5a52Smatt
639adeb5a52Smatt void
vfp_getcontext(struct lwp * l,mcontext_t * mcp,int * flagsp)640cb72f657Smatt vfp_getcontext(struct lwp *l, mcontext_t *mcp, int *flagsp)
641cb72f657Smatt {
642877a3ccfSchs if (vfp_used_p(l)) {
643cb72f657Smatt const struct pcb * const pcb = lwp_getpcb(l);
644877a3ccfSchs
645877a3ccfSchs pcu_save(&arm_vfp_ops, l);
646cb72f657Smatt mcp->__fpu.__vfpregs.__vfp_fpscr = pcb->pcb_vfp.vfp_fpscr;
647cb72f657Smatt memcpy(mcp->__fpu.__vfpregs.__vfp_fstmx, pcb->pcb_vfp.vfp_regs,
648cb72f657Smatt sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
649a431007dSmatt *flagsp |= _UC_FPU|_UC_ARM_VFP;
650cb72f657Smatt }
651cb72f657Smatt }
652cb72f657Smatt
653cb72f657Smatt void
vfp_setcontext(struct lwp * l,const mcontext_t * mcp)654cb72f657Smatt vfp_setcontext(struct lwp *l, const mcontext_t *mcp)
655cb72f657Smatt {
656cb72f657Smatt struct pcb * const pcb = lwp_getpcb(l);
657877a3ccfSchs
658877a3ccfSchs pcu_discard(&arm_vfp_ops, l, true);
659cb72f657Smatt pcb->pcb_vfp.vfp_fpscr = mcp->__fpu.__vfpregs.__vfp_fpscr;
660cb72f657Smatt memcpy(pcb->pcb_vfp.vfp_regs, mcp->__fpu.__vfpregs.__vfp_fstmx,
661cb72f657Smatt sizeof(mcp->__fpu.__vfpregs.__vfp_fstmx));
662cb72f657Smatt }
663cb72f657Smatt
664764da9e9Sriastradh /*
665764da9e9Sriastradh * True if this is a system thread with its own private FPU state.
666764da9e9Sriastradh */
667764da9e9Sriastradh static inline bool
lwp_system_fpu_p(struct lwp * l)668764da9e9Sriastradh lwp_system_fpu_p(struct lwp *l)
669764da9e9Sriastradh {
670764da9e9Sriastradh
671764da9e9Sriastradh return (l->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) ==
672764da9e9Sriastradh (LW_SYSTEM|LW_SYSTEM_FPU);
673764da9e9Sriastradh }
674764da9e9Sriastradh
675764da9e9Sriastradh static const struct vfpreg zero_vfpreg;
676764da9e9Sriastradh
677c41eed1fSriastradh void
fpu_kern_enter(void)678c41eed1fSriastradh fpu_kern_enter(void)
679c41eed1fSriastradh {
680c41eed1fSriastradh struct cpu_info *ci;
681c41eed1fSriastradh uint32_t fpexc;
682c41eed1fSriastradh int s;
683c41eed1fSriastradh
684764da9e9Sriastradh if (lwp_system_fpu_p(curlwp) && !cpu_intr_p()) {
685764da9e9Sriastradh KASSERT(!cpu_softintr_p());
686764da9e9Sriastradh return;
687764da9e9Sriastradh }
688764da9e9Sriastradh
689c41eed1fSriastradh /*
690a555d8eaSriastradh * Block interrupts up to IPL_VM. We must block preemption
691a555d8eaSriastradh * since -- if this is a user thread -- there is nowhere to
692a555d8eaSriastradh * save the kernel fpu state, and if we want this to be usable
693a555d8eaSriastradh * in interrupts, we can't let interrupts interfere with the
694a555d8eaSriastradh * fpu state in use since there's nowhere for them to save it.
695c41eed1fSriastradh */
696a555d8eaSriastradh s = splvm();
697c41eed1fSriastradh ci = curcpu();
698*040f3e2bSriastradh #if 0
699*040f3e2bSriastradh /*
700*040f3e2bSriastradh * Can't assert this because if the caller holds a spin lock at
701*040f3e2bSriastradh * IPL_VM, and previously held and released a spin lock at
702*040f3e2bSriastradh * higher IPL, the IPL remains raised above IPL_VM.
703*040f3e2bSriastradh */
704b451bcfcSriastradh KASSERTMSG(ci->ci_cpl <= IPL_VM || cold, "cpl=%d", ci->ci_cpl);
705*040f3e2bSriastradh #endif
706c41eed1fSriastradh KASSERT(ci->ci_kfpu_spl == -1);
707c41eed1fSriastradh ci->ci_kfpu_spl = s;
708c41eed1fSriastradh
70950e0ab0aSriastradh /* Save any fpu state on the current CPU. */
71050e0ab0aSriastradh pcu_save_all_on_cpu();
711c41eed1fSriastradh
712c41eed1fSriastradh /* Enable the fpu. */
713c41eed1fSriastradh fpexc = armreg_fpexc_read();
714c41eed1fSriastradh fpexc |= VFP_FPEXC_EN;
715c41eed1fSriastradh fpexc &= ~VFP_FPEXC_EX;
716c41eed1fSriastradh armreg_fpexc_write(fpexc);
717c41eed1fSriastradh }
718c41eed1fSriastradh
719c41eed1fSriastradh void
fpu_kern_leave(void)720c41eed1fSriastradh fpu_kern_leave(void)
721c41eed1fSriastradh {
722c41eed1fSriastradh struct cpu_info *ci = curcpu();
723c41eed1fSriastradh int s;
724c41eed1fSriastradh uint32_t fpexc;
725c41eed1fSriastradh
726764da9e9Sriastradh if (lwp_system_fpu_p(curlwp) && !cpu_intr_p()) {
727764da9e9Sriastradh KASSERT(!cpu_softintr_p());
728764da9e9Sriastradh return;
729764da9e9Sriastradh }
730764da9e9Sriastradh
731*040f3e2bSriastradh #if 0
732*040f3e2bSriastradh /*
733*040f3e2bSriastradh * Can't assert this because if the caller holds a spin lock at
734*040f3e2bSriastradh * IPL_VM, and previously held and released a spin lock at
735*040f3e2bSriastradh * higher IPL, the IPL remains raised above IPL_VM.
736*040f3e2bSriastradh */
737b451bcfcSriastradh KASSERT(ci->ci_cpl == IPL_VM || cold);
738*040f3e2bSriastradh #endif
739c41eed1fSriastradh KASSERT(ci->ci_kfpu_spl != -1);
740c41eed1fSriastradh
741c41eed1fSriastradh /*
742c41eed1fSriastradh * Zero the fpu registers; otherwise we might leak secrets
743c41eed1fSriastradh * through Spectre-class attacks to userland, even if there are
744c41eed1fSriastradh * no bugs in fpu state management.
745c41eed1fSriastradh */
746c41eed1fSriastradh load_vfpregs(&zero_vfpreg);
747c41eed1fSriastradh
748c41eed1fSriastradh /*
749c41eed1fSriastradh * Disable the fpu so that the kernel can't accidentally use
750c41eed1fSriastradh * it again.
751c41eed1fSriastradh */
752c41eed1fSriastradh fpexc = armreg_fpexc_read();
753c41eed1fSriastradh fpexc &= ~VFP_FPEXC_EN;
754c41eed1fSriastradh armreg_fpexc_write(fpexc);
755c41eed1fSriastradh
756c41eed1fSriastradh /* Restore interrupts. */
757c41eed1fSriastradh s = ci->ci_kfpu_spl;
758c41eed1fSriastradh ci->ci_kfpu_spl = -1;
759c41eed1fSriastradh splx(s);
760c41eed1fSriastradh }
761c41eed1fSriastradh
762764da9e9Sriastradh void
kthread_fpu_enter_md(void)763764da9e9Sriastradh kthread_fpu_enter_md(void)
764764da9e9Sriastradh {
765764da9e9Sriastradh
766764da9e9Sriastradh pcu_load(&arm_vfp_ops);
767764da9e9Sriastradh }
768764da9e9Sriastradh
769764da9e9Sriastradh void
kthread_fpu_exit_md(void)770764da9e9Sriastradh kthread_fpu_exit_md(void)
771764da9e9Sriastradh {
772764da9e9Sriastradh
773764da9e9Sriastradh /* XXX Should vfp_state_release zero the registers itself? */
774764da9e9Sriastradh load_vfpregs(&zero_vfpreg);
775764da9e9Sriastradh vfp_discardcontext(curlwp, 0);
776764da9e9Sriastradh }
777764da9e9Sriastradh
778c58ada7bSmatt #endif /* FPU_VFP */
779