xref: /plan9-contrib/sys/src/9/bcm/vfp3.c (revision 5c47fe09a0cc86dfb02c0ea4a2b6aec7eda2361f)
1 /*
2  * VFPv2 or VFPv3 floating point unit
3  */
4 #include "u.h"
5 #include "../port/lib.h"
6 #include "mem.h"
7 #include "dat.h"
8 #include "fns.h"
9 #include "ureg.h"
10 #include "arm.h"
11 
12 /* subarchitecture code in m->havefp */
13 enum {
14 	VFPv2	= 2,
15 	VFPv3	= 3,
16 };
17 
18 /* fp control regs.  most are read-only */
19 enum {
20 	Fpsid =	0,
21 	Fpscr =	1,			/* rw */
22 	Mvfr1 =	6,
23 	Mvfr0 =	7,
24 	Fpexc =	8,			/* rw */
25 	Fpinst= 9,			/* optional, for exceptions */
26 	Fpinst2=10,
27 };
28 enum {
29 	/* Fpexc bits */
30 	Fpex =		1u << 31,
31 	Fpenabled =	1 << 30,
32 	Fpdex =		1 << 29,	/* defined synch exception */
33 //	Fp2v =		1 << 28,	/* Fpinst2 reg is valid */
34 //	Fpvv =		1 << 27,	/* if Fpdex, vecitr is valid */
35 //	Fptfv = 	1 << 26,	/* trapped fault is valid */
36 //	Fpvecitr =	MASK(3) << 8,
37 	/* FSR bits appear here */
38 	Fpmbc =		Fpdex,		/* bits exception handler must clear */
39 
40 	/* Fpscr bits; see u.h for more */
41 	Stride =	MASK(2) << 20,
42 	Len =		MASK(3) << 16,
43 	Dn=		1 << 25,
44 	Fz=		1 << 24,
45 	/* trap exception enables (not allowed in vfp3) */
46 	FPIDNRM =	1 << 15,	/* input denormal */
47 	Alltraps = FPIDNRM | FPINEX | FPUNFL | FPOVFL | FPZDIV | FPINVAL,
48 	/* pending exceptions */
49 	FPAIDNRM =	1 << 7,		/* input denormal */
50 	Allexc = FPAIDNRM | FPAINEX | FPAUNFL | FPAOVFL | FPAZDIV | FPAINVAL,
51 	/* condition codes */
52 	Allcc =		MASK(4) << 28,
53 };
54 enum {
55 	/* CpCPaccess bits */
56 	Cpaccnosimd =	1u << 31,
57 	Cpaccd16 =	1 << 30,
58 };
59 
60 static char *
subarch(int impl,uint sa)61 subarch(int impl, uint sa)
62 {
63 	static char *armarchs[] = {
64 		"VFPv1 (unsupported)",
65 		"VFPv2",
66 		"VFPv3+ with common VFP subarch v2",
67 		"VFPv3+ with null subarch",
68 		"VFPv3+ with common VFP subarch v3",
69 	};
70 
71 	if (impl != 'A' || sa >= nelem(armarchs))
72 		return "GOK";
73 	else
74 		return armarchs[sa];
75 }
76 
77 static char *
implement(uchar impl)78 implement(uchar impl)
79 {
80 	if (impl == 'A')
81 		return "arm";
82 	else
83 		return "unknown";
84 }
85 
86 static int
havefp(void)87 havefp(void)
88 {
89 	int gotfp;
90 	ulong acc, sid;
91 
92 	if (m->havefpvalid)
93 		return m->havefp;
94 
95 	m->havefp = 0;
96 	gotfp = 1 << CpFP | 1 << CpDFP;
97 	cpwrcpaccess(MASK(28));
98 	acc = cprdcpaccess();
99 	if ((acc & (MASK(2) << (2*CpFP))) == 0) {
100 		gotfp &= ~(1 << CpFP);
101 		print("fpon: no single FP coprocessor\n");
102 	}
103 	if ((acc & (MASK(2) << (2*CpDFP))) == 0) {
104 		gotfp &= ~(1 << CpDFP);
105 		print("fpon: no double FP coprocessor\n");
106 	}
107 	if (!gotfp) {
108 		print("fpon: no FP coprocessors\n");
109 		m->havefpvalid = 1;
110 		return 0;
111 	}
112 	m->fpon = 1;			/* don't panic */
113 	sid = fprdsid();
114 	m->fpon = 0;
115 	switch((sid >> 16) & MASK(7)){
116 	case 0:				/* VFPv1 */
117 		break;
118 	case 1:				/* VFPv2 */
119 		m->havefp = VFPv2;
120 		m->fpnregs = 16;
121 		break;
122 	default:			/* VFPv3 or later */
123 		m->havefp = VFPv3;
124 		m->fpnregs = (acc & Cpaccd16) ? 16 : 32;
125 		break;
126 	}
127 	if (m->machno == 0)
128 		print("fp: %d registers, %s simd\n", m->fpnregs,
129 			(acc & Cpaccnosimd? " no": ""));
130 	m->havefpvalid = 1;
131 	return 1;
132 }
133 
134 /*
135  * these can be called to turn the fpu on or off for user procs,
136  * not just at system start up or shutdown.
137  */
138 
139 void
fpoff(void)140 fpoff(void)
141 {
142 	if (m->fpon) {
143 		fpwrexc(0);
144 		m->fpon = 0;
145 	}
146 }
147 
148 void
fpononly(void)149 fpononly(void)
150 {
151 	if (!m->fpon && havefp()) {
152 		/* enable fp.  must be first operation on the FPUs. */
153 		fpwrexc(Fpenabled);
154 		m->fpon = 1;
155 	}
156 }
157 
158 static void
fpcfg(void)159 fpcfg(void)
160 {
161 	int impl;
162 	ulong sid;
163 	static int printed;
164 
165 	/* clear pending exceptions; no traps in vfp3; all v7 ops are scalar */
166 	m->fpscr = Dn | FPRNR | (FPINVAL | FPZDIV | FPOVFL) & ~Alltraps;
167 	/* VFPv2 needs software support for underflows, so force them to zero */
168 	if(m->havefp == VFPv2)
169 		m->fpscr |= Fz;
170 	fpwrscr(m->fpscr);
171 	m->fpconfiged = 1;
172 
173 	if (printed)
174 		return;
175 	sid = fprdsid();
176 	impl = sid >> 24;
177 	print("fp: %s arch %s; rev %ld\n", implement(impl),
178 		subarch(impl, (sid >> 16) & MASK(7)), sid & MASK(4));
179 	printed = 1;
180 }
181 
182 void
fpinit(void)183 fpinit(void)
184 {
185 	if (havefp()) {
186 		fpononly();
187 		fpcfg();
188 	}
189 }
190 
191 void
fpon(void)192 fpon(void)
193 {
194 	if (havefp()) {
195 	 	fpononly();
196 		if (m->fpconfiged)
197 			fpwrscr((fprdscr() & Allcc) | m->fpscr);
198 		else
199 			fpcfg();	/* 1st time on this fpu; configure it */
200 	}
201 }
202 
203 void
fpclear(void)204 fpclear(void)
205 {
206 //	ulong scr;
207 
208 	fpon();
209 //	scr = fprdscr();
210 //	m->fpscr = scr & ~Allexc;
211 //	fpwrscr(m->fpscr);
212 
213 	fpwrexc(fprdexc() & ~Fpmbc);
214 }
215 
216 
217 /*
218  * Called when a note is about to be delivered to a
219  * user process, usually at the end of a system call.
220  * Note handlers are not allowed to use the FPU so
221  * the state is marked (after saving if necessary) and
222  * checked in the Device Not Available handler.
223  */
224 void
fpunotify(Ureg *)225 fpunotify(Ureg*)
226 {
227 	if(up->fpstate == FPactive){
228 		fpsave(&up->fpsave);
229 		up->fpstate = FPinactive;
230 	}
231 	up->fpstate |= FPillegal;
232 }
233 
234 /*
235  * Called from sysnoted() via the machine-dependent
236  * noted() routine.
237  * Clear the flag set above in fpunotify().
238  */
239 void
fpunoted(void)240 fpunoted(void)
241 {
242 	up->fpstate &= ~FPillegal;
243 }
244 
245 /*
246  * Called early in the non-interruptible path of
247  * sysrfork() via the machine-dependent syscall() routine.
248  * Save the state so that it can be easily copied
249  * to the child process later.
250  */
251 void
fpusysrfork(Ureg *)252 fpusysrfork(Ureg*)
253 {
254 	if(up->fpstate == FPactive){
255 		fpsave(&up->fpsave);
256 		up->fpstate = FPinactive;
257 	}
258 }
259 
260 /*
261  * Called later in sysrfork() via the machine-dependent
262  * sysrforkchild() routine.
263  * Copy the parent FPU state to the child.
264  */
265 void
fpusysrforkchild(Proc * p,Ureg *,Proc * up)266 fpusysrforkchild(Proc *p, Ureg *, Proc *up)
267 {
268 	/* don't penalize the child, it hasn't done FP in a note handler. */
269 	p->fpstate = up->fpstate & ~FPillegal;
270 }
271 
272 /* should only be called if p->fpstate == FPactive */
273 void
fpsave(FPsave * fps)274 fpsave(FPsave *fps)
275 {
276 	fpon();
277 	fps->control = fps->status = fprdscr();
278 	assert(m->fpnregs);
279 	fpsaveregs((uvlong*)fps->regs, m->fpnregs);
280 	fpoff();
281 }
282 
283 static void
fprestore(Proc * p)284 fprestore(Proc *p)
285 {
286 	fpon();
287 	fpwrscr(p->fpsave.control);
288 	m->fpscr = fprdscr() & ~Allcc;
289 	assert(m->fpnregs);
290 	fprestregs((uvlong*)p->fpsave.regs, m->fpnregs);
291 }
292 
293 /*
294  * Called from sched() and sleep() via the machine-dependent
295  * procsave() routine.
296  * About to go in to the scheduler.
297  * If the process wasn't using the FPU
298  * there's nothing to do.
299  */
300 void
fpuprocsave(Proc * p)301 fpuprocsave(Proc *p)
302 {
303 	if(p->fpstate == FPactive){
304 		if(p->state == Moribund)
305 			fpoff();
306 		else{
307 			/*
308 			 * Fpsave() stores without handling pending
309 			 * unmasked exeptions. Postnote() can't be called
310 			 * here as sleep() already has up->rlock, so
311 			 * the handling of pending exceptions is delayed
312 			 * until the process runs again and generates an
313 			 * emulation fault to activate the FPU.
314 			 */
315 			fpsave(&p->fpsave);
316 		}
317 		p->fpstate = FPinactive;
318 	}
319 }
320 
321 /*
322  * The process has been rescheduled and is about to run.
323  * Nothing to do here right now. If the process tries to use
324  * the FPU again it will cause a Device Not Available
325  * exception and the state will then be restored.
326  */
327 void
fpuprocrestore(Proc *)328 fpuprocrestore(Proc *)
329 {
330 }
331 
332 /*
333  * Disable the FPU.
334  * Called from sysexec() via sysprocsetup() to
335  * set the FPU for the new process.
336  */
337 void
fpusysprocsetup(Proc * p)338 fpusysprocsetup(Proc *p)
339 {
340 	p->fpstate = FPinit;
341 	fpoff();
342 }
343 
344 static void
mathnote(void)345 mathnote(void)
346 {
347 	ulong status;
348 	char *msg, note[ERRMAX];
349 
350 	status = up->fpsave.status;
351 
352 	/*
353 	 * Some attention should probably be paid here to the
354 	 * exception masks and error summary.
355 	 */
356 	if (status & FPAINEX)
357 		msg = "inexact";
358 	else if (status & FPAOVFL)
359 		msg = "overflow";
360 	else if (status & FPAUNFL)
361 		msg = "underflow";
362 	else if (status & FPAZDIV)
363 		msg = "divide by zero";
364 	else if (status & FPAINVAL)
365 		msg = "bad operation";
366 	else
367 		msg = "spurious";
368 	snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=%#lux",
369 		msg, up->fpsave.pc, status);
370 	postnote(up, 1, note, NDebug);
371 }
372 
373 static void
mathemu(Ureg *)374 mathemu(Ureg *)
375 {
376 	switch(up->fpstate){
377 	case FPemu:
378 		error("illegal instruction: VFP opcode in emulated mode");
379 	case FPinit:
380 		fpinit();
381 		up->fpstate = FPactive;
382 		break;
383 	case FPinactive:
384 		/*
385 		 * Before restoring the state, check for any pending
386 		 * exceptions.  There's no way to restore the state without
387 		 * generating an unmasked exception.
388 		 * More attention should probably be paid here to the
389 		 * exception masks and error summary.
390 		 */
391 		if(up->fpsave.status & (FPAINEX|FPAUNFL|FPAOVFL|FPAZDIV|FPAINVAL)){
392 			mathnote();
393 			break;
394 		}
395 		fprestore(up);
396 		up->fpstate = FPactive;
397 		break;
398 	case FPactive:
399 		error("sys: illegal instruction: bad vfp fpu opcode");
400 		break;
401 	}
402 	fpclear();
403 }
404 
405 void
fpstuck(uintptr pc)406 fpstuck(uintptr pc)
407 {
408 	if (m->fppc == pc && m->fppid == up->pid) {
409 		m->fpcnt++;
410 		if (m->fpcnt > 4)
411 			panic("fpuemu: cpu%d stuck at pid %ld %s pc %#p "
412 				"instr %#8.8lux", m->machno, up->pid, up->text,
413 				pc, *(ulong *)pc);
414 	} else {
415 		m->fppid = up->pid;
416 		m->fppc = pc;
417 		m->fpcnt = 0;
418 	}
419 }
420 
421 enum {
422 	N = 1<<31,
423 	Z = 1<<30,
424 	C = 1<<29,
425 	V = 1<<28,
426 	REGPC = 15,
427 };
428 
429 static int
condok(int cc,int c)430 condok(int cc, int c)
431 {
432 	switch(c){
433 	case 0:	/* Z set */
434 		return cc&Z;
435 	case 1:	/* Z clear */
436 		return (cc&Z) == 0;
437 	case 2:	/* C set */
438 		return cc&C;
439 	case 3:	/* C clear */
440 		return (cc&C) == 0;
441 	case 4:	/* N set */
442 		return cc&N;
443 	case 5:	/* N clear */
444 		return (cc&N) == 0;
445 	case 6:	/* V set */
446 		return cc&V;
447 	case 7:	/* V clear */
448 		return (cc&V) == 0;
449 	case 8:	/* C set and Z clear */
450 		return cc&C && (cc&Z) == 0;
451 	case 9:	/* C clear or Z set */
452 		return (cc&C) == 0 || cc&Z;
453 	case 10:	/* N set and V set, or N clear and V clear */
454 		return (~cc&(N|V))==0 || (cc&(N|V)) == 0;
455 	case 11:	/* N set and V clear, or N clear and V set */
456 		return (cc&(N|V))==N || (cc&(N|V))==V;
457 	case 12:	/* Z clear, and either N set and V set or N clear and V clear */
458 		return (cc&Z) == 0 && ((~cc&(N|V))==0 || (cc&(N|V))==0);
459 	case 13:	/* Z set, or N set and V clear or N clear and V set */
460 		return (cc&Z) || (cc&(N|V))==N || (cc&(N|V))==V;
461 	case 14:	/* always */
462 		return 1;
463 	case 15:	/* never (reserved) */
464 		return 0;
465 	}
466 	return 0;	/* not reached */
467 }
468 
469 /* only called to deal with user-mode instruction faults */
470 int
fpuemu(Ureg * ureg)471 fpuemu(Ureg* ureg)
472 {
473 	int s, nfp, cop, op;
474 	uintptr pc;
475 	static int already;
476 
477 	if(waserror()){
478 		postnote(up, 1, up->errstr, NDebug);
479 		return 1;
480 	}
481 
482 	if(up->fpstate & FPillegal)
483 		error("floating point in note handler");
484 
485 	nfp = 0;
486 	pc = ureg->pc;
487 	validaddr(pc, 4, 0);
488 	op  = (*(ulong *)pc >> 24) & MASK(4);
489 	cop = (*(ulong *)pc >>  8) & MASK(4);
490 	if(m->fpon)
491 		fpstuck(pc);		/* debugging; could move down 1 line */
492 	if (ISFPAOP(cop, op)) {		/* old arm 7500 fpa opcode? */
493 		s = spllo();
494 		if(!already++)
495 			pprint("warning: emulated arm7500 fpa instr %#8.8lux at %#p\n", *(ulong *)pc, pc);
496 		if(waserror()){
497 			splx(s);
498 			nexterror();
499 		}
500 		nfp = fpiarm(ureg);	/* advances pc past emulated instr(s) */
501 		if (nfp > 1)		/* could adjust this threshold */
502 			m->fppc = m->fpcnt = 0;
503 		splx(s);
504 		poperror();
505 	} else if (ISVFPOP(cop, op)) {	/* if vfp, fpu off or unsupported instruction */
506 		mathemu(ureg);		/* enable fpu & retry */
507 		nfp = 1;
508 	}
509 
510 	poperror();
511 	return nfp;
512 }
513