vfp.c revision 266160
1/*
2 * Copyright (c) 2012 Mark Tinguely
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: stable/10/sys/arm/arm/vfp.c 266160 2014-05-15 17:30:16Z ian $");
30
31#ifdef VFP
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/proc.h>
35#include <sys/kernel.h>
36
37#include <machine/frame.h>
38#include <machine/fp.h>
39#include <machine/pcb.h>
40#include <machine/undefined.h>
41#include <machine/vfp.h>
42
43/* function prototypes */
44unsigned int get_coprocessorACR(void);
45static int vfp_bounce(u_int, u_int, struct trapframe *, int);
46static void vfp_restore(struct vfp_state *);
47void	vfp_discard(void);
48void	vfp_store(struct vfp_state *);
49void	set_coprocessorACR(u_int);
50
51extern int vfp_exists;
52static struct undefined_handler vfp10_uh, vfp11_uh;
53/* If true the VFP unit has 32 double registers, otherwise it has 16 */
54static int is_d32;
55
56/* The VFMXR command using coprocessor commands */
57#define fmxr(reg, val) \
58    __asm __volatile("mcr p10, 7, %0, " __STRING(reg) " , c0, 0" :: "r"(val));
59
60/* The VFMRX command using coprocessor commands */
61#define fmrx(reg) \
62({ u_int val = 0;\
63    __asm __volatile("mrc p10, 7, %0, " __STRING(reg) " , c0, 0" : "=r"(val));\
64    val; \
65})
66
67u_int
68get_coprocessorACR(void)
69{
70	u_int val;
71	__asm __volatile("mrc p15, 0, %0, c1, c0, 2" : "=r" (val) : : "cc");
72	return val;
73}
74
75void
76set_coprocessorACR(u_int val)
77{
78	__asm __volatile("mcr p15, 0, %0, c1, c0, 2\n\t"
79	 : : "r" (val) : "cc");
80	isb();
81}
82
83
84	/* called for each cpu */
85void
86vfp_init(void)
87{
88	u_int fpsid, fpexc, tmp;
89	u_int coproc, vfp_arch;
90
91	coproc = get_coprocessorACR();
92	coproc |= COPROC10 | COPROC11;
93	set_coprocessorACR(coproc);
94
95	fpsid = fmrx(VFPSID);		/* read the vfp system id */
96	fpexc = fmrx(VFPEXC);		/* read the vfp exception reg */
97
98	if (!(fpsid & VFPSID_HARDSOFT_IMP)) {
99		vfp_exists = 1;
100		is_d32 = 0;
101		PCPU_SET(vfpsid, fpsid);	/* save the VFPSID */
102
103		vfp_arch =
104		    (fpsid & VFPSID_SUBVERSION2_MASK) >> VFPSID_SUBVERSION_OFF;
105
106		if (vfp_arch >= VFP_ARCH3) {
107			tmp = fmrx(VMVFR0);
108			PCPU_SET(vfpmvfr0, tmp);
109
110			if ((tmp & VMVFR0_RB_MASK) == 2)
111				is_d32 = 1;
112
113			tmp = fmrx(VMVFR1);
114			PCPU_SET(vfpmvfr1, tmp);
115		}
116
117		/* initialize the coprocess 10 and 11 calls
118		 * These are called to restore the registers and enable
119		 * the VFP hardware.
120		 */
121		if (vfp10_uh.uh_handler == NULL) {
122			vfp10_uh.uh_handler = vfp_bounce;
123			vfp11_uh.uh_handler = vfp_bounce;
124			install_coproc_handler_static(10, &vfp10_uh);
125			install_coproc_handler_static(11, &vfp11_uh);
126		}
127	}
128}
129
130SYSINIT(vfp, SI_SUB_CPU, SI_ORDER_ANY, vfp_init, NULL);
131
132
133/* start VFP unit, restore the vfp registers from the PCB  and retry
134 * the instruction
135 */
136static int
137vfp_bounce(u_int addr, u_int insn, struct trapframe *frame, int code)
138{
139	u_int fpexc;
140	struct pcb *curpcb;
141	struct thread *vfptd;
142	int i;
143
144	if (!vfp_exists)
145		return 1;		/* vfp does not exist */
146	i = disable_interrupts(I32_bit|F32_bit);
147	fpexc = fmrx(VFPEXC);		/* read the vfp exception reg */
148	if (fpexc & VFPEXC_EN) {
149		vfptd = PCPU_GET(vfpcthread);
150		/* did the kernel call the vfp or exception that expect us
151		 * to emulate the command. Newer hardware does not require
152		 * emulation, so we don't emulate yet.
153		 */
154#ifdef SMP
155		/* don't save if newer registers are on another processor */
156		if (vfptd /* && (vfptd == curthread) */ &&
157		   (vfptd->td_pcb->pcb_vfpcpu == PCPU_GET(cpu)))
158#else
159		/* someone did not save their registers, */
160		if (vfptd /* && (vfptd == curthread) */)
161#endif
162			vfp_store(&vfptd->td_pcb->pcb_vfpstate);
163
164		fpexc &= ~VFPEXC_EN;
165		fmxr(VFPEXC, fpexc);	/* turn vfp hardware off */
166		if (vfptd == curthread) {
167			/* kill the process - we do not handle emulation */
168			restore_interrupts(i);
169			killproc(curthread->td_proc, "vfp emulation");
170			return 1;
171		}
172		/* should not happen. someone did not save their context */
173		printf("vfp_bounce: vfpcthread: %p curthread: %p\n",
174			vfptd, curthread);
175	}
176	fpexc |= VFPEXC_EN;
177	fmxr(VFPEXC, fpexc);	/* enable the vfp and repeat command */
178	curpcb = curthread->td_pcb;
179	/* If we were the last process to use the VFP, the process did not
180	 * use a VFP on another processor, then the registers in the VFP
181	 * will still be ours and are current. Eventually, we will make the
182	 * restore smarter.
183	 */
184	vfp_restore(&curpcb->pcb_vfpstate);
185#ifdef SMP
186	curpcb->pcb_vfpcpu = PCPU_GET(cpu);
187#endif
188	PCPU_SET(vfpcthread, curthread);
189	restore_interrupts(i);
190	return 0;
191}
192
193/* vfs_store is called from from a VFP command to restore the registers and
194 * turn on the VFP hardware.
195 * Eventually we will use the information that this process was the last
196 * to use the VFP hardware and bypass the restore, just turn on the hardware.
197 */
198static void
199vfp_restore(struct vfp_state *vfpsave)
200{
201	u_int vfpscr = 0;
202
203	/*
204	 * Work around an issue with GCC where the asm it generates is
205	 * not unified syntax and fails to assemble because it expects
206	 * the ldcleq instruction in the form ldc<c>l, not in the UAL
207	 * form ldcl<c>, and similar for stcleq.
208	 */
209#ifdef __clang__
210#define	ldclne	"ldclne"
211#define	stclne	"stclne"
212#else
213#define	ldclne	"ldcnel"
214#define	stclne	"stcnel"
215#endif
216	if (vfpsave) {
217		__asm __volatile("ldc	p10, c0, [%1], #128\n" /* d0-d15 */
218			"cmp	%2, #0\n"		/* -D16 or -D32? */
219			ldclne"	p11, c0, [%1], #128\n"	/* d16-d31 */
220			"addeq	%1, %1, #128\n"		/* skip missing regs */
221			"ldr	%0, [%1]\n"		/* set old vfpscr */
222			"mcr	p10, 7, %0, cr1, c0, 0\n"
223			: "=&r" (vfpscr) : "r" (vfpsave), "r" (is_d32) : "cc");
224	}
225}
226
227/* vfs_store is called from switch to save the vfp hardware registers
228 * into the pcb before switching to another process.
229 * we already know that the new process is different from this old
230 * process and that this process last used the VFP registers.
231 * Below we check to see if the VFP has been enabled since the last
232 * register save.
233 * This routine will exit with the VFP turned off. The next VFP user
234 * will trap to restore its registers and turn on the VFP hardware.
235 */
236void
237vfp_store(struct vfp_state *vfpsave)
238{
239	u_int tmp, vfpscr = 0;
240
241	tmp = fmrx(VFPEXC);		/* Is the vfp enabled? */
242	if (vfpsave && (tmp & VFPEXC_EN)) {
243		__asm __volatile("stc	p11, c0, [%1], #128\n" /* d0-d15 */
244			"cmp	%2, #0\n"		/* -D16 or -D32? */
245			stclne"	p11, c0, [%1], #128\n"	/* d16-d31 */
246			"addeq	%1, %1, #128\n"		/* skip missing regs */
247			"mrc	p10, 7, %0, cr1, c0, 0\n" /* fmxr(VFPSCR) */
248			"str	%0, [%1]\n"		/* save vfpscr */
249			: "=&r" (vfpscr) : "r" (vfpsave), "r" (is_d32) : "cc");
250	}
251#undef ldcleq
252#undef stcleq
253
254#ifndef SMP
255		/* eventually we will use this information for UP also */
256	PCPU_SET(vfpcthread, 0);
257#endif
258	tmp &= ~VFPEXC_EN;	/* disable the vfp hardware */
259	fmxr(VFPEXC , tmp);
260}
261
262/* discard the registers at cpu_thread_free() when fpcurthread == td.
263 * Turn off the VFP hardware.
264 */
265void
266vfp_discard()
267{
268	u_int tmp = 0;
269
270	/*
271	 * No need to protect the access to vfpcthread by disabling
272	 * interrupts, since it's called from cpu_throw(), who is called
273	 * with interrupts disabled.
274	 */
275
276	PCPU_SET(vfpcthread, 0);	/* permanent forget about reg */
277	tmp = fmrx(VFPEXC);
278	tmp &= ~VFPEXC_EN;		/* turn off VFP hardware */
279	fmxr(VFPEXC, tmp);
280}
281
282#endif
283
284