]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/vfp.c
Merge ACPICA 20170929 (take 2).
[FreeBSD/FreeBSD.git] / sys / arm / arm / vfp.c
1 /*-
2  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
3  * Copyright (c) 2012 Mark Tinguely
4  *
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #ifdef VFP
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/imgact_elf.h>
37 #include <sys/kernel.h>
38
39 #include <machine/armreg.h>
40 #include <machine/elf.h>
41 #include <machine/frame.h>
42 #include <machine/md_var.h>
43 #include <machine/pcb.h>
44 #include <machine/undefined.h>
45 #include <machine/vfp.h>
46
47 /* function prototypes */
48 static int vfp_bounce(u_int, u_int, struct trapframe *, int);
49 static void vfp_restore(struct vfp_state *);
50
51 extern int vfp_exists;
52 static struct undefined_handler vfp10_uh, vfp11_uh;
53 /* If true the VFP unit has 32 double registers, otherwise it has 16 */
54 static int is_d32;
55
56 /*
57  * About .fpu directives in this file...
58  *
59  * We should need simply .fpu vfpv3, but clang 3.5 has a quirk where setting
60  * vfpv3 doesn't imply that vfp2 features are also available -- both have to be
61  * explicitly set to get all the features of both.  This is probably a bug in
62  * clang, so it may get fixed and require changes here some day.  Other changes
63  * are probably coming in clang too, because there is email and open PRs
64  * indicating they want to completely disable the ability to use .fpu and
65  * similar directives in inline asm.  That would be catastrophic for us,
66  * hopefully they come to their senses.  There was also some discusion of a new
67  * syntax such as .push fpu=vfpv3; ...; .pop fpu; and that would be ideal for
68  * us, better than what we have now really.
69  *
70  * For gcc, each .fpu directive completely overrides the prior directive, unlike
71  * with clang, but luckily on gcc saying v3 implies all the v2 features as well.
72  */
73
74 #define fmxr(reg, val) \
75     __asm __volatile("  .fpu vfpv2\n .fpu vfpv3\n"                      \
76                      "  vmsr    " __STRING(reg) ", %0"   :: "r"(val));
77
78 #define fmrx(reg) \
79 ({ u_int val = 0;\
80     __asm __volatile(" .fpu vfpv2\n .fpu vfpv3\n"                       \
81                      "  vmrs    %0, " __STRING(reg) : "=r"(val));       \
82     val; \
83 })
84
85 static u_int
86 get_coprocessorACR(void)
87 {
88         u_int val;
89         __asm __volatile("mrc p15, 0, %0, c1, c0, 2" : "=r" (val) : : "cc");
90         return val;
91 }
92
93 static void
94 set_coprocessorACR(u_int val)
95 {
96         __asm __volatile("mcr p15, 0, %0, c1, c0, 2\n\t"
97          : : "r" (val) : "cc");
98         isb();
99 }
100
101
102         /* called for each cpu */
103 void
104 vfp_init(void)
105 {
106         u_int fpsid, fpexc, tmp;
107         u_int coproc, vfp_arch;
108
109         coproc = get_coprocessorACR();
110         coproc |= COPROC10 | COPROC11;
111         set_coprocessorACR(coproc);
112
113         fpsid = fmrx(fpsid);            /* read the vfp system id */
114         fpexc = fmrx(fpexc);            /* read the vfp exception reg */
115
116         if (!(fpsid & VFPSID_HARDSOFT_IMP)) {
117                 vfp_exists = 1;
118                 is_d32 = 0;
119                 PCPU_SET(vfpsid, fpsid);        /* save the fpsid */
120                 elf_hwcap |= HWCAP_VFP;
121
122                 vfp_arch =
123                     (fpsid & VFPSID_SUBVERSION2_MASK) >> VFPSID_SUBVERSION_OFF;
124
125                 if (vfp_arch >= VFP_ARCH3) {
126                         tmp = fmrx(mvfr0);
127                         PCPU_SET(vfpmvfr0, tmp);
128                         elf_hwcap |= HWCAP_VFPv3;
129
130                         if ((tmp & VMVFR0_RB_MASK) == 2) {
131                                 elf_hwcap |= HWCAP_VFPD32;
132                                 is_d32 = 1;
133                         } else
134                                 elf_hwcap |= HWCAP_VFPv3D16;
135
136                         tmp = fmrx(mvfr1);
137                         PCPU_SET(vfpmvfr1, tmp);
138
139                         if (PCPU_GET(cpuid) == 0) {
140                                 if ((tmp & VMVFR1_FZ_MASK) == 0x1) {
141                                         /* Denormals arithmetic support */
142                                         initial_fpscr &= ~VFPSCR_FZ;
143                                         thread0.td_pcb->pcb_vfpstate.fpscr =
144                                             initial_fpscr;
145                                 }
146                         }
147
148                         if ((tmp & VMVFR1_LS_MASK) >> VMVFR1_LS_OFF == 1 &&
149                             (tmp & VMVFR1_I_MASK) >> VMVFR1_I_OFF == 1 &&
150                             (tmp & VMVFR1_SP_MASK) >> VMVFR1_SP_OFF == 1)
151                                 elf_hwcap |= HWCAP_NEON;
152                 }
153
154                 /* initialize the coprocess 10 and 11 calls
155                  * These are called to restore the registers and enable
156                  * the VFP hardware.
157                  */
158                 if (vfp10_uh.uh_handler == NULL) {
159                         vfp10_uh.uh_handler = vfp_bounce;
160                         vfp11_uh.uh_handler = vfp_bounce;
161                         install_coproc_handler_static(10, &vfp10_uh);
162                         install_coproc_handler_static(11, &vfp11_uh);
163                 }
164         }
165 }
166
167 SYSINIT(vfp, SI_SUB_CPU, SI_ORDER_ANY, vfp_init, NULL);
168
169
170 /* start VFP unit, restore the vfp registers from the PCB  and retry
171  * the instruction
172  */
173 static int
174 vfp_bounce(u_int addr, u_int insn, struct trapframe *frame, int code)
175 {
176         u_int cpu, fpexc;
177         struct pcb *curpcb;
178         ksiginfo_t ksi;
179
180         if ((code & FAULT_USER) == 0)
181                 panic("undefined floating point instruction in supervisor mode");
182
183         critical_enter();
184
185         /*
186          * If the VFP is already on and we got an undefined instruction, then
187          * something tried to executate a truly invalid instruction that maps to
188          * the VFP.
189          */
190         fpexc = fmrx(fpexc);
191         if (fpexc & VFPEXC_EN) {
192                 /* Clear any exceptions */
193                 fmxr(fpexc, fpexc & ~(VFPEXC_EX | VFPEXC_FP2V));
194
195                 /* kill the process - we do not handle emulation */
196                 critical_exit();
197
198                 if (fpexc & VFPEXC_EX) {
199                         /* We have an exception, signal a SIGFPE */
200                         ksiginfo_init_trap(&ksi);
201                         ksi.ksi_signo = SIGFPE;
202                         if (fpexc & VFPEXC_UFC)
203                                 ksi.ksi_code = FPE_FLTUND;
204                         else if (fpexc & VFPEXC_OFC)
205                                 ksi.ksi_code = FPE_FLTOVF;
206                         else if (fpexc & VFPEXC_IOC)
207                                 ksi.ksi_code = FPE_FLTINV;
208                         ksi.ksi_addr = (void *)addr;
209                         trapsignal(curthread, &ksi);
210                         return 0;
211                 }
212
213                 return 1;
214         }
215
216         /*
217          * If the last time this thread used the VFP it was on this core, and
218          * the last thread to use the VFP on this core was this thread, then the
219          * VFP state is valid, otherwise restore this thread's state to the VFP.
220          */
221         fmxr(fpexc, fpexc | VFPEXC_EN);
222         curpcb = curthread->td_pcb;
223         cpu = PCPU_GET(cpuid);
224         if (curpcb->pcb_vfpcpu != cpu || curthread != PCPU_GET(fpcurthread)) {
225                 vfp_restore(&curpcb->pcb_vfpstate);
226                 curpcb->pcb_vfpcpu = cpu;
227                 PCPU_SET(fpcurthread, curthread);
228         }
229
230         critical_exit();
231         return (0);
232 }
233
234 /*
235  * Restore the given state to the VFP hardware.
236  */
237 static void
238 vfp_restore(struct vfp_state *vfpsave)
239 {
240         uint32_t fpexc;
241
242         /* On vfpv3 we may need to restore FPINST and FPINST2 */
243         fpexc = vfpsave->fpexec;
244         if (fpexc & VFPEXC_EX) {
245                 fmxr(fpinst, vfpsave->fpinst);
246                 if (fpexc & VFPEXC_FP2V)
247                         fmxr(fpinst2, vfpsave->fpinst2);
248         }
249         fmxr(fpscr, vfpsave->fpscr);
250
251         __asm __volatile(
252             " .fpu      vfpv2\n"
253             " .fpu      vfpv3\n"
254             " vldmia    %0!, {d0-d15}\n"        /* d0-d15 */
255             " cmp       %1, #0\n"               /* -D16 or -D32? */
256             " vldmiane  %0!, {d16-d31}\n"       /* d16-d31 */
257             " addeq     %0, %0, #128\n"         /* skip missing regs */
258             : "+&r" (vfpsave) : "r" (is_d32) : "cc"
259             );
260
261         fmxr(fpexc, fpexc);
262 }
263
264 /*
265  * If the VFP is on, save its current state and turn it off if requested to do
266  * so.  If the VFP is not on, does not change the values at *vfpsave.  Caller is
267  * responsible for preventing a context switch while this is running.
268  */
269 void
270 vfp_store(struct vfp_state *vfpsave, boolean_t disable_vfp)
271 {
272         uint32_t fpexc;
273
274         fpexc = fmrx(fpexc);            /* Is the vfp enabled? */
275         if (fpexc & VFPEXC_EN) {
276                 vfpsave->fpexec = fpexc;
277                 vfpsave->fpscr = fmrx(fpscr);
278
279                 /* On vfpv3 we may need to save FPINST and FPINST2 */
280                 if (fpexc & VFPEXC_EX) {
281                         vfpsave->fpinst = fmrx(fpinst);
282                         if (fpexc & VFPEXC_FP2V)
283                                 vfpsave->fpinst2 = fmrx(fpinst2);
284                         fpexc &= ~VFPEXC_EX;
285                 }
286
287                 __asm __volatile(
288                     " .fpu      vfpv2\n"
289                     " .fpu      vfpv3\n"
290                     " vstmia    %0!, {d0-d15}\n"        /* d0-d15 */
291                     " cmp       %1, #0\n"               /* -D16 or -D32? */
292                     " vstmiane  r0!, {d16-d31}\n"       /* d16-d31 */
293                     " addeq     %0, %0, #128\n"         /* skip missing regs */
294                     : "+&r" (vfpsave) : "r" (is_d32) : "cc"
295                     );
296
297                 if (disable_vfp)
298                         fmxr(fpexc , fpexc & ~VFPEXC_EN);
299         }
300 }
301
302 /*
303  * The current thread is dying.  If the state currently in the hardware belongs
304  * to the current thread, set fpcurthread to NULL to indicate that the VFP
305  * hardware state does not belong to any thread.  If the VFP is on, turn it off.
306  * Called only from cpu_throw(), so we don't have to worry about a context
307  * switch here.
308  */
309 void
310 vfp_discard(struct thread *td)
311 {
312         u_int tmp;
313
314         if (PCPU_GET(fpcurthread) == td)
315                 PCPU_SET(fpcurthread, NULL);
316
317         tmp = fmrx(fpexc);
318         if (tmp & VFPEXC_EN)
319                 fmxr(fpexc, tmp & ~VFPEXC_EN);
320 }
321
322 #endif
323