2 * Copyright (C) 1996 Wolfgang Solfrank.
3 * Copyright (C) 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $NetBSD: fpu.c,v 1.5 2001/07/22 11:29:46 wiz Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/limits.h>
42 #include <machine/altivec.h>
43 #include <machine/fpu.h>
44 #include <machine/ieeefp.h>
45 #include <machine/pcb.h>
46 #include <machine/psl.h>
48 #include <powerpc/fpu/fpu_arith.h>
49 #include <powerpc/fpu/fpu_emu.h>
50 #include <powerpc/fpu/fpu_extern.h>
52 void spe_handle_fpdata(struct trapframe *);
53 void spe_handle_fpround(struct trapframe *);
54 static int spe_emu_instr(uint32_t, struct fpemu *, struct fpn **, uint32_t *);
57 save_vec_int(struct thread *td)
65 * Temporarily re-enable the vector unit during the save
71 * Save the vector registers and SPEFSCR to the PCB
73 #define EVSTDW(n) __asm ("evstdw %1,0(%0)" \
74 :: "b"(pcb->pcb_vec.vr[n]), "n"(n));
75 EVSTDW(0); EVSTDW(1); EVSTDW(2); EVSTDW(3);
76 EVSTDW(4); EVSTDW(5); EVSTDW(6); EVSTDW(7);
77 EVSTDW(8); EVSTDW(9); EVSTDW(10); EVSTDW(11);
78 EVSTDW(12); EVSTDW(13); EVSTDW(14); EVSTDW(15);
79 EVSTDW(16); EVSTDW(17); EVSTDW(18); EVSTDW(19);
80 EVSTDW(20); EVSTDW(21); EVSTDW(22); EVSTDW(23);
81 EVSTDW(24); EVSTDW(25); EVSTDW(26); EVSTDW(27);
82 EVSTDW(28); EVSTDW(29); EVSTDW(30); EVSTDW(31);
85 __asm ( "evxor 0,0,0\n"
87 "evstdd 0,0(%0)" :: "b"(&pcb->pcb_vec.spare[0]));
88 pcb->pcb_vec.vscr = mfspr(SPR_SPEFSCR);
91 * Disable vector unit again
99 enable_vec(struct thread *td)
103 struct trapframe *tf;
109 * Save the thread's SPE CPU number, and set the CPU's current
112 td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
113 PCPU_SET(vecthread, td);
116 * Enable the vector unit for when the thread returns from the
117 * exception. If this is the first time the unit has been used by
118 * the thread, initialise the vector registers and VSCR to 0, and
119 * set the flag to indicate that the vector unit is in use.
122 if (!(pcb->pcb_flags & PCB_VEC)) {
123 memset(&pcb->pcb_vec, 0, sizeof pcb->pcb_vec);
124 pcb->pcb_flags |= PCB_VEC;
125 pcb->pcb_vec.vscr = mfspr(SPR_SPEFSCR);
129 * Temporarily enable the vector unit so the registers
133 mtmsr(msr | PSL_VEC);
135 /* Restore SPEFSCR and ACC. Use %r0 as the scratch for ACC. */
136 mtspr(SPR_SPEFSCR, pcb->pcb_vec.vscr);
137 __asm __volatile("isync;evldd 0, 0(%0); evmra 0,0\n"
138 :: "b"(&pcb->pcb_vec.spare[0]));
141 * The lower half of each register will be restored on trap return. Use
142 * %r0 as a scratch register, and restore it last.
144 #define EVLDW(n) __asm __volatile("evldw 0, 0(%0); evmergehilo "#n",0,"#n \
145 :: "b"(&pcb->pcb_vec.vr[n]));
146 EVLDW(1); EVLDW(2); EVLDW(3); EVLDW(4);
147 EVLDW(5); EVLDW(6); EVLDW(7); EVLDW(8);
148 EVLDW(9); EVLDW(10); EVLDW(11); EVLDW(12);
149 EVLDW(13); EVLDW(14); EVLDW(15); EVLDW(16);
150 EVLDW(17); EVLDW(18); EVLDW(19); EVLDW(20);
151 EVLDW(21); EVLDW(22); EVLDW(23); EVLDW(24);
152 EVLDW(25); EVLDW(26); EVLDW(27); EVLDW(28);
153 EVLDW(29); EVLDW(30); EVLDW(31); EVLDW(0);
161 save_vec(struct thread *td)
170 * Clear the current vec thread and pcb's CPU id
171 * XXX should this be left clear to allow lazy save/restore ?
173 pcb->pcb_veccpu = INT_MAX;
174 PCPU_SET(vecthread, NULL);
178 * Save SPE state without dropping ownership. This will only save state if
179 * the current vector-thread is `td'. This is used for taking core dumps, so
180 * don't leak kernel information; overwrite the low words of each vector with
181 * their real value, taken from the thread's trap frame, unconditionally.
184 save_vec_nodrop(struct thread *td)
189 if (td == PCPU_GET(vecthread))
194 for (i = 0; i < 32; i++) {
195 pcb->pcb_vec.vr[i][1] =
196 td->td_frame ? td->td_frame->fixreg[i] : 0;
200 #define SPE_INST_MASK 0x31f
227 #define EVFSADD 0x280
228 #define EVFSSUB 0x281
229 #define EVFSABS 0x284
230 #define EVFSNABS 0x285
231 #define EVFSNEG 0x286
232 #define EVFSMUL 0x288
233 #define EVFSDIV 0x289
234 #define EVFSCMPGT 0x28c
235 #define EVFSCMPLT 0x28d
236 #define EVFSCMPEQ 0x28e
237 #define EVFSCFUI 0x290
238 #define EVFSCFSI 0x291
239 #define EVFSCTUI 0x294
240 #define EVFSCTSI 0x295
241 #define EVFSCTUF 0x296
242 #define EVFSCTSF 0x297
243 #define EVFSCTUIZ 0x298
244 #define EVFSCTSIZ 0x29a
249 #define EFSNABS 0x2c5
253 #define EFSCMPGT 0x2cc
254 #define EFSCMPLT 0x2cd
255 #define EFSCMPEQ 0x2ce
257 #define EFSCFUI 0x2d0
258 #define EFSCFSI 0x2d1
259 #define EFSCTUI 0x2d4
260 #define EFSCTSI 0x2d5
261 #define EFSCTUF 0x2d6
262 #define EFSCTSF 0x2d7
263 #define EFSCTUIZ 0x2d8
264 #define EFSCTSIZ 0x2da
269 #define EFDNABS 0x2e5
273 #define EFDCMPGT 0x2ec
274 #define EFDCMPLT 0x2ed
275 #define EFDCMPEQ 0x2ee
277 #define EFDCFUI 0x2f0
278 #define EFDCFSI 0x2f1
279 #define EFDCTUI 0x2f4
280 #define EFDCTSI 0x2f5
281 #define EFDCTUF 0x2f6
282 #define EFDCTSF 0x2f7
283 #define EFDCTUIZ 0x2f8
284 #define EFDCTSIZ 0x2fa
293 static uint32_t fpscr_to_spefscr(uint32_t fpscr)
299 if (fpscr & FPSCR_VX)
300 spefscr |= SPEFSCR_FINV;
301 if (fpscr & FPSCR_OX)
302 spefscr |= SPEFSCR_FOVF;
303 if (fpscr & FPSCR_UX)
304 spefscr |= SPEFSCR_FUNF;
305 if (fpscr & FPSCR_ZX)
306 spefscr |= SPEFSCR_FDBZ;
307 if (fpscr & FPSCR_XX)
308 spefscr |= SPEFSCR_FX;
313 /* Sign is 0 for unsigned, 1 for signed. */
315 spe_to_int(struct fpemu *fpemu, struct fpn *fpn, uint32_t *val, int sign)
319 res[0] = fpu_ftox(fpemu, fpn, res);
320 if (res[0] != UINT_MAX && res[0] != 0)
321 fpemu->fe_cx |= FPSCR_OX;
322 else if (sign == 0 && res[0] != 0)
323 fpemu->fe_cx |= FPSCR_UX;
330 /* Masked instruction */
332 * For compare instructions, returns 1 if success, 0 if not. For all others,
333 * returns -1, or -2 if no result needs recorded.
336 spe_emu_instr(uint32_t instr, struct fpemu *fpemu,
337 struct fpn **result, uint32_t *iresult)
339 switch (instr & SPE_INST_MASK) {
343 /* Taken care of elsewhere. */
346 fpemu->fe_cx &= ~FPSCR_RN;
347 fpemu->fe_cx |= FP_RZ;
349 spe_to_int(fpemu, &fpemu->fe_f2, iresult, 0);
352 fpemu->fe_cx &= ~FPSCR_RN;
353 fpemu->fe_cx |= FP_RZ;
355 spe_to_int(fpemu, &fpemu->fe_f2, iresult, 1);
358 *result = fpu_add(fpemu);
361 *result = fpu_sub(fpemu);
364 *result = fpu_mul(fpemu);
367 *result = fpu_div(fpemu);
370 fpu_compare(fpemu, 0);
371 if (fpemu->fe_cx & FPSCR_FG)
375 fpu_compare(fpemu, 0);
376 if (fpemu->fe_cx & FPSCR_FL)
380 fpu_compare(fpemu, 0);
381 if (fpemu->fe_cx & FPSCR_FE)
385 printf("Unknown instruction %x\n", instr);
392 spe_explode(struct fpemu *fe, struct fpn *fp, uint32_t type,
393 uint32_t hi, uint32_t lo)
397 fp->fp_sign = hi >> 31;
401 s = fpu_stof(fp, hi);
405 s = fpu_dtof(fp, hi, lo);
409 if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
411 * Input is a signalling NaN. All operations that return
412 * an input NaN operand put it through a ``NaN conversion'',
413 * which basically just means ``turn on the quiet bit''.
414 * We do this here so that all NaNs internally look quiet
415 * (we can tell signalling ones by their class).
417 fp->fp_mant[0] |= FP_QUIETBIT;
418 fe->fe_cx = FPSCR_VXSNAN; /* assert invalid operand */
427 * Save the high word of a 64-bit GPR for manipulation in the exception handler.
430 spe_save_reg_high(int reg)
433 #define EVSTDW(n) case n: __asm __volatile ("evstdw %1,0(%0)" \
434 :: "b"(vec), "n"(n) : "memory"); break;
436 EVSTDW(0); EVSTDW(1); EVSTDW(2); EVSTDW(3);
437 EVSTDW(4); EVSTDW(5); EVSTDW(6); EVSTDW(7);
438 EVSTDW(8); EVSTDW(9); EVSTDW(10); EVSTDW(11);
439 EVSTDW(12); EVSTDW(13); EVSTDW(14); EVSTDW(15);
440 EVSTDW(16); EVSTDW(17); EVSTDW(18); EVSTDW(19);
441 EVSTDW(20); EVSTDW(21); EVSTDW(22); EVSTDW(23);
442 EVSTDW(24); EVSTDW(25); EVSTDW(26); EVSTDW(27);
443 EVSTDW(28); EVSTDW(29); EVSTDW(30); EVSTDW(31);
451 * Load the given value into the high word of the requested register.
454 spe_load_reg_high(int reg, uint32_t val)
456 #define EVLDW(n) case n: __asm __volatile("evmergelo "#n",%0,"#n \
459 EVLDW(1); EVLDW(2); EVLDW(3); EVLDW(4);
460 EVLDW(5); EVLDW(6); EVLDW(7); EVLDW(8);
461 EVLDW(9); EVLDW(10); EVLDW(11); EVLDW(12);
462 EVLDW(13); EVLDW(14); EVLDW(15); EVLDW(16);
463 EVLDW(17); EVLDW(18); EVLDW(19); EVLDW(20);
464 EVLDW(21); EVLDW(22); EVLDW(23); EVLDW(24);
465 EVLDW(25); EVLDW(26); EVLDW(27); EVLDW(28);
466 EVLDW(29); EVLDW(30); EVLDW(31); EVLDW(0);
473 spe_handle_fpdata(struct trapframe *frame)
477 uint32_t instr, instr_sec_op;
478 uint32_t cr_shift, ra, rb, rd, src;
479 uint32_t high, low, res, tmp; /* For vector operations. */
480 uint32_t spefscr = 0;
481 uint32_t ftod_res[2];
482 int width; /* Single, Double, Vector, Integer */
486 err = fueword32((void *)frame->srr0, &instr);
492 if ((instr >> OPC_SHIFT) != SPE_OPC)
497 * 'cr' field is the upper 3 bits of rd. Magically, since a) rd is 5
498 * bits, b) each 'cr' field is 4 bits, and c) Only the 'GT' bit is
499 * modified for most compare operations, the full value of rd can be
500 * used as a shift value.
502 rd = (instr >> 21) & 0x1f;
503 ra = (instr >> 16) & 0x1f;
504 rb = (instr >> 11) & 0x1f;
505 src = (instr >> 5) & 0x7;
506 cr_shift = 28 - (rd & 0x1f);
508 instr_sec_op = (instr & 0x7ff);
510 memset(&fpemu, 0, sizeof(fpemu));
515 mtmsr(msr | PSL_VEC);
516 switch (instr_sec_op) {
518 high = spe_save_reg_high(ra) & ~(1U << 31);
519 frame->fixreg[rd] = frame->fixreg[ra] & ~(1U << 31);
520 spe_load_reg_high(rd, high);
523 high = spe_save_reg_high(ra) | (1U << 31);
524 frame->fixreg[rd] = frame->fixreg[ra] | (1U << 31);
525 spe_load_reg_high(rd, high);
528 high = spe_save_reg_high(ra) ^ (1U << 31);
529 frame->fixreg[rd] = frame->fixreg[ra] ^ (1U << 31);
530 spe_load_reg_high(rd, high);
534 spe_explode(&fpemu, &fpemu.fe_f1, SINGLE,
535 spe_save_reg_high(ra), 0);
536 spe_explode(&fpemu, &fpemu.fe_f2, SINGLE,
537 spe_save_reg_high(rb), 0);
538 high = spe_emu_instr(instr_sec_op, &fpemu, &result,
542 spe_load_reg_high(rd, tmp);
544 spefscr = fpscr_to_spefscr(fpemu.fe_cx) << 16;
545 /* Clear the fpemu to start over on the lower bits. */
546 memset(&fpemu, 0, sizeof(fpemu));
549 spe_explode(&fpemu, &fpemu.fe_f1, SINGLE,
550 frame->fixreg[ra], 0);
551 spe_explode(&fpemu, &fpemu.fe_f2, SINGLE,
552 frame->fixreg[rb], 0);
553 spefscr |= fpscr_to_spefscr(fpemu.fe_cx);
554 low = spe_emu_instr(instr_sec_op, &fpemu, &result,
556 if (instr_sec_op == EVFSCMPEQ ||
557 instr_sec_op == EVFSCMPGT ||
558 instr_sec_op == EVFSCMPLT) {
559 res = (high << 3) | (low << 2) |
560 ((high | low) << 1) | (high & low);
569 switch (instr_sec_op) {
571 frame->fixreg[rd] = frame->fixreg[ra] & ~(1U << 31);
574 frame->fixreg[rd] = frame->fixreg[ra] | (1U << 31);
577 frame->fixreg[rd] = frame->fixreg[ra] ^ (1U << 31);
580 mtmsr(msr | PSL_VEC);
581 spe_explode(&fpemu, &fpemu.fe_f3, DOUBLE,
582 spe_save_reg_high(rb), frame->fixreg[rb]);
583 result = &fpemu.fe_f3;
587 spe_explode(&fpemu, &fpemu.fe_f1, SINGLE,
588 frame->fixreg[ra], 0);
589 spe_explode(&fpemu, &fpemu.fe_f2, SINGLE,
590 frame->fixreg[rb], 0);
595 mtmsr(msr | PSL_VEC);
596 switch (instr_sec_op) {
598 high = spe_save_reg_high(ra) & ~(1U << 31);
599 frame->fixreg[rd] = frame->fixreg[ra];
600 spe_load_reg_high(rd, high);
603 high = spe_save_reg_high(ra) | (1U << 31);
604 frame->fixreg[rd] = frame->fixreg[ra];
605 spe_load_reg_high(rd, high);
608 high = spe_save_reg_high(ra) ^ (1U << 31);
609 frame->fixreg[rd] = frame->fixreg[ra];
610 spe_load_reg_high(rd, high);
613 spe_explode(&fpemu, &fpemu.fe_f3, SINGLE,
614 frame->fixreg[rb], 0);
615 result = &fpemu.fe_f3;
619 spe_explode(&fpemu, &fpemu.fe_f1, DOUBLE,
620 spe_save_reg_high(ra), frame->fixreg[ra]);
621 spe_explode(&fpemu, &fpemu.fe_f2, DOUBLE,
622 spe_save_reg_high(rb), frame->fixreg[rb]);
627 switch (instr_sec_op) {
630 /* Already handled. */
633 res = spe_emu_instr(instr_sec_op, &fpemu, &result,
640 switch (instr_sec_op & SPE_INST_MASK) {
644 frame->cr &= ~(0xf << cr_shift);
645 frame->cr |= (res << cr_shift);
658 frame->fixreg[rd] = fpu_ftos(&fpemu, result);
661 spe_load_reg_high(rd, fpu_ftod(&fpemu, result, ftod_res));
662 frame->fixreg[rd] = ftod_res[1];
665 panic("Unknown storage width %d", width);
671 spefscr |= (mfspr(SPR_SPEFSCR) & ~SPEFSCR_FINVS);
672 mtspr(SPR_SPEFSCR, spefscr);
680 spe_handle_fpround(struct trapframe *frame)
684 * Punt fpround exceptions for now. This leaves the truncated result in
685 * the register. We'll deal with overflow/underflow later.