2 * arch/powerpc/math-emu/math_efp.c
4 * Copyright (C) 2006-2008 Freescale Semiconductor, Inc. All rights reserved.
6 * Author: Ebony Zhu, <ebony.zhu@freescale.com>
7 * Yu Liu, <yu.liu@freescale.com>
9 * Derived from arch/alpha/math-emu/math.c
10 * arch/powerpc/math-emu/math.c
13 * This file is the exception handler to make E500 SPE instructions
14 * fully comply with IEEE-754 floating point standard.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
22 #include <linux/types.h>
24 #include <asm/uaccess.h>
27 #define FP_EX_BOOKE_E500_SPE
28 #include <asm/sfp-machine.h>
30 #include <math-emu/soft-fp.h>
31 #include <math-emu/single.h>
32 #include <math-emu/double.h>
47 #define EFSCMPGT 0x2cc
48 #define EFSCMPLT 0x2cd
49 #define EFSCMPEQ 0x2ce
56 #define EFSCTUIZ 0x2d8
57 #define EFSCTSIZ 0x2da
62 #define EVFSNABS 0x285
66 #define EVFSCMPGT 0x28c
67 #define EVFSCMPLT 0x28d
68 #define EVFSCMPEQ 0x28e
69 #define EVFSCTUI 0x294
70 #define EVFSCTSI 0x295
71 #define EVFSCTUF 0x296
72 #define EVFSCTSF 0x297
73 #define EVFSCTUIZ 0x298
74 #define EVFSCTSIZ 0x29a
83 #define EFDCTUIDZ 0x2ea
84 #define EFDCTSIDZ 0x2eb
85 #define EFDCMPGT 0x2ec
86 #define EFDCMPLT 0x2ed
87 #define EFDCMPEQ 0x2ee
93 #define EFDCTUIZ 0x2f8
94 #define EFDCTSIZ 0x2fa
102 #define SIGN_BIT_S (1UL << 31)
103 #define SIGN_BIT_D (1ULL << 63)
104 #define FP_EX_MASK (FP_EX_INEXACT | FP_EX_INVALID | FP_EX_DIVZERO | \
105 FP_EX_UNDERFLOW | FP_EX_OVERFLOW)
112 static unsigned long insn_type(unsigned long speinsn
)
114 unsigned long ret
= NOTYPE
;
116 switch (speinsn
& 0x7ff) {
117 case EFSABS
: ret
= XA
; break;
118 case EFSADD
: ret
= AB
; break;
119 case EFSCFD
: ret
= XB
; break;
120 case EFSCMPEQ
: ret
= XCR
; break;
121 case EFSCMPGT
: ret
= XCR
; break;
122 case EFSCMPLT
: ret
= XCR
; break;
123 case EFSCTSF
: ret
= XB
; break;
124 case EFSCTSI
: ret
= XB
; break;
125 case EFSCTSIZ
: ret
= XB
; break;
126 case EFSCTUF
: ret
= XB
; break;
127 case EFSCTUI
: ret
= XB
; break;
128 case EFSCTUIZ
: ret
= XB
; break;
129 case EFSDIV
: ret
= AB
; break;
130 case EFSMUL
: ret
= AB
; break;
131 case EFSNABS
: ret
= XA
; break;
132 case EFSNEG
: ret
= XA
; break;
133 case EFSSUB
: ret
= AB
; break;
134 case EFSCFSI
: ret
= XB
; break;
136 case EVFSABS
: ret
= XA
; break;
137 case EVFSADD
: ret
= AB
; break;
138 case EVFSCMPEQ
: ret
= XCR
; break;
139 case EVFSCMPGT
: ret
= XCR
; break;
140 case EVFSCMPLT
: ret
= XCR
; break;
141 case EVFSCTSF
: ret
= XB
; break;
142 case EVFSCTSI
: ret
= XB
; break;
143 case EVFSCTSIZ
: ret
= XB
; break;
144 case EVFSCTUF
: ret
= XB
; break;
145 case EVFSCTUI
: ret
= XB
; break;
146 case EVFSCTUIZ
: ret
= XB
; break;
147 case EVFSDIV
: ret
= AB
; break;
148 case EVFSMUL
: ret
= AB
; break;
149 case EVFSNABS
: ret
= XA
; break;
150 case EVFSNEG
: ret
= XA
; break;
151 case EVFSSUB
: ret
= AB
; break;
153 case EFDABS
: ret
= XA
; break;
154 case EFDADD
: ret
= AB
; break;
155 case EFDCFS
: ret
= XB
; break;
156 case EFDCMPEQ
: ret
= XCR
; break;
157 case EFDCMPGT
: ret
= XCR
; break;
158 case EFDCMPLT
: ret
= XCR
; break;
159 case EFDCTSF
: ret
= XB
; break;
160 case EFDCTSI
: ret
= XB
; break;
161 case EFDCTSIDZ
: ret
= XB
; break;
162 case EFDCTSIZ
: ret
= XB
; break;
163 case EFDCTUF
: ret
= XB
; break;
164 case EFDCTUI
: ret
= XB
; break;
165 case EFDCTUIDZ
: ret
= XB
; break;
166 case EFDCTUIZ
: ret
= XB
; break;
167 case EFDDIV
: ret
= AB
; break;
168 case EFDMUL
: ret
= AB
; break;
169 case EFDNABS
: ret
= XA
; break;
170 case EFDNEG
: ret
= XA
; break;
171 case EFDSUB
: ret
= AB
; break;
174 printk(KERN_ERR
"\nOoops! SPE instruction no type found.");
175 printk(KERN_ERR
"\ninst code: %08lx\n", speinsn
);
181 int do_spe_mathemu(struct pt_regs
*regs
)
186 unsigned long type
, func
, fc
, fa
, fb
, src
, speinsn
;
187 union dw_union vc
, va
, vb
;
189 if (get_user(speinsn
, (unsigned int __user
*) regs
->nip
))
191 if ((speinsn
>> 26) != EFAPU
)
192 return -EINVAL
; /* not an spe instruction */
194 type
= insn_type(speinsn
);
198 func
= speinsn
& 0x7ff;
199 fc
= (speinsn
>> 21) & 0x1f;
200 fa
= (speinsn
>> 16) & 0x1f;
201 fb
= (speinsn
>> 11) & 0x1f;
202 src
= (speinsn
>> 5) & 0x7;
204 vc
.wp
[0] = current
->thread
.evr
[fc
];
205 vc
.wp
[1] = regs
->gpr
[fc
];
206 va
.wp
[0] = current
->thread
.evr
[fa
];
207 va
.wp
[1] = regs
->gpr
[fa
];
208 vb
.wp
[0] = current
->thread
.evr
[fb
];
209 vb
.wp
[1] = regs
->gpr
[fb
];
211 __FPU_FPSCR
= mfspr(SPRN_SPEFSCR
);
214 printk("speinsn:%08lx spefscr:%08lx\n", speinsn
, __FPU_FPSCR
);
215 printk("vc: %08x %08x\n", vc
.wp
[0], vc
.wp
[1]);
216 printk("va: %08x %08x\n", va
.wp
[0], va
.wp
[1]);
217 printk("vb: %08x %08x\n", vb
.wp
[0], vb
.wp
[1]);
222 FP_DECL_S(SA
); FP_DECL_S(SB
); FP_DECL_S(SR
);
227 FP_UNPACK_SP(SA
, va
.wp
+ 1);
229 FP_UNPACK_SP(SB
, vb
.wp
+ 1);
232 FP_UNPACK_SP(SA
, va
.wp
+ 1);
237 printk("SA: %ld %08lx %ld (%ld)\n", SA_s
, SA_f
, SA_e
, SA_c
);
238 printk("SB: %ld %08lx %ld (%ld)\n", SB_s
, SB_f
, SB_e
, SB_c
);
243 vc
.wp
[1] = va
.wp
[1] & ~SIGN_BIT_S
;
247 vc
.wp
[1] = va
.wp
[1] | SIGN_BIT_S
;
251 vc
.wp
[1] = va
.wp
[1] ^ SIGN_BIT_S
;
255 FP_ADD_S(SR
, SA
, SB
);
259 FP_SUB_S(SR
, SA
, SB
);
263 FP_MUL_S(SR
, SA
, SB
);
267 FP_DIV_S(SR
, SA
, SB
);
284 if (!((vb
.wp
[1] >> 23) == 0xff && ((vb
.wp
[1] & 0x7fffff) > 0))) {
286 if (((vb
.wp
[1] >> 23) & 0xff) == 0) {
289 } else if ((vb
.wp
[1] >> 31) == 0) {
290 /* positive normal */
291 vc
.wp
[1] = (func
== EFSCTSF
) ?
292 0x7fffffff : 0xffffffff;
293 } else { /* negative normal */
294 vc
.wp
[1] = (func
== EFSCTSF
) ?
297 } else { /* rB is NaN */
305 FP_UNPACK_DP(DB
, vb
.dp
);
307 printk("DB: %ld %08lx %08lx %ld (%ld)\n",
308 DB_s
, DB_f1
, DB_f0
, DB_e
, DB_c
);
310 FP_CONV(S
, D
, 1, 2, SR
, DB
);
321 _FP_ROUND_ZERO(1, SB
);
323 FP_TO_INT_S(vc
.wp
[1], SB
, 32, ((func
& 0x3) != 0));
333 printk("SR: %ld %08lx %ld (%ld)\n", SR_s
, SR_f
, SR_e
, SR_c
);
335 FP_PACK_SP(vc
.wp
+ 1, SR
);
339 FP_CMP_S(IR
, SA
, SB
, 3);
340 if (IR
== 3 && (FP_ISSIGNAN_S(SA
) || FP_ISSIGNAN_S(SB
)))
341 FP_SET_EXCEPTION(FP_EX_INVALID
);
351 FP_DECL_D(DA
); FP_DECL_D(DB
); FP_DECL_D(DR
);
356 FP_UNPACK_DP(DA
, va
.dp
);
358 FP_UNPACK_DP(DB
, vb
.dp
);
361 FP_UNPACK_DP(DA
, va
.dp
);
366 printk("DA: %ld %08lx %08lx %ld (%ld)\n",
367 DA_s
, DA_f1
, DA_f0
, DA_e
, DA_c
);
368 printk("DB: %ld %08lx %08lx %ld (%ld)\n",
369 DB_s
, DB_f1
, DB_f0
, DB_e
, DB_c
);
374 vc
.dp
[0] = va
.dp
[0] & ~SIGN_BIT_D
;
378 vc
.dp
[0] = va
.dp
[0] | SIGN_BIT_D
;
382 vc
.dp
[0] = va
.dp
[0] ^ SIGN_BIT_D
;
386 FP_ADD_D(DR
, DA
, DB
);
390 FP_SUB_D(DR
, DA
, DB
);
394 FP_MUL_D(DR
, DA
, DB
);
398 FP_DIV_D(DR
, DA
, DB
);
415 if (!((vb
.wp
[0] >> 20) == 0x7ff &&
416 ((vb
.wp
[0] & 0xfffff) > 0 || (vb
.wp
[1] > 0)))) {
418 if (((vb
.wp
[0] >> 20) & 0x7ff) == 0) {
421 } else if ((vb
.wp
[0] >> 31) == 0) {
422 /* positive normal */
423 vc
.wp
[1] = (func
== EFDCTSF
) ?
424 0x7fffffff : 0xffffffff;
425 } else { /* negative normal */
426 vc
.wp
[1] = (func
== EFDCTSF
) ?
437 FP_UNPACK_SP(SB
, vb
.wp
+ 1);
439 printk("SB: %ld %08lx %ld (%ld)\n",
440 SB_s
, SB_f
, SB_e
, SB_c
);
442 FP_CONV(D
, S
, 2, 1, DR
, SB
);
448 _FP_ROUND_ZERO(2, DB
);
449 FP_TO_INT_D(vc
.dp
[0], DB
, 64, ((func
& 0x1) == 0));
459 _FP_ROUND_ZERO(2, DB
);
461 FP_TO_INT_D(vc
.wp
[1], DB
, 32, ((func
& 0x3) != 0));
471 printk("DR: %ld %08lx %08lx %ld (%ld)\n",
472 DR_s
, DR_f1
, DR_f0
, DR_e
, DR_c
);
474 FP_PACK_DP(vc
.dp
, DR
);
478 FP_CMP_D(IR
, DA
, DB
, 3);
479 if (IR
== 3 && (FP_ISSIGNAN_D(DA
) || FP_ISSIGNAN_D(DB
)))
480 FP_SET_EXCEPTION(FP_EX_INVALID
);
491 FP_DECL_S(SA0
); FP_DECL_S(SB0
); FP_DECL_S(SR0
);
492 FP_DECL_S(SA1
); FP_DECL_S(SB1
); FP_DECL_S(SR1
);
498 FP_UNPACK_SP(SA0
, va
.wp
);
499 FP_UNPACK_SP(SA1
, va
.wp
+ 1);
501 FP_UNPACK_SP(SB0
, vb
.wp
);
502 FP_UNPACK_SP(SB1
, vb
.wp
+ 1);
505 FP_UNPACK_SP(SA0
, va
.wp
);
506 FP_UNPACK_SP(SA1
, va
.wp
+ 1);
511 printk("SA0: %ld %08lx %ld (%ld)\n", SA0_s
, SA0_f
, SA0_e
, SA0_c
);
512 printk("SA1: %ld %08lx %ld (%ld)\n", SA1_s
, SA1_f
, SA1_e
, SA1_c
);
513 printk("SB0: %ld %08lx %ld (%ld)\n", SB0_s
, SB0_f
, SB0_e
, SB0_c
);
514 printk("SB1: %ld %08lx %ld (%ld)\n", SB1_s
, SB1_f
, SB1_e
, SB1_c
);
519 vc
.wp
[0] = va
.wp
[0] & ~SIGN_BIT_S
;
520 vc
.wp
[1] = va
.wp
[1] & ~SIGN_BIT_S
;
524 vc
.wp
[0] = va
.wp
[0] | SIGN_BIT_S
;
525 vc
.wp
[1] = va
.wp
[1] | SIGN_BIT_S
;
529 vc
.wp
[0] = va
.wp
[0] ^ SIGN_BIT_S
;
530 vc
.wp
[1] = va
.wp
[1] ^ SIGN_BIT_S
;
534 FP_ADD_S(SR0
, SA0
, SB0
);
535 FP_ADD_S(SR1
, SA1
, SB1
);
539 FP_SUB_S(SR0
, SA0
, SB0
);
540 FP_SUB_S(SR1
, SA1
, SB1
);
544 FP_MUL_S(SR0
, SA0
, SB0
);
545 FP_MUL_S(SR1
, SA1
, SB1
);
549 FP_DIV_S(SR0
, SA0
, SB0
);
550 FP_DIV_S(SR1
, SA1
, SB1
);
566 __asm__
__volatile__ ("mtspr 512, %4\n"
569 : "=r" (vc
.wp
[0]), "=r" (vc
.wp
[1])
570 : "r" (vb
.wp
[0]), "r" (vb
.wp
[1]), "r" (0));
574 __asm__
__volatile__ ("mtspr 512, %4\n"
577 : "=r" (vc
.wp
[0]), "=r" (vc
.wp
[1])
578 : "r" (vb
.wp
[0]), "r" (vb
.wp
[1]), "r" (0));
589 _FP_ROUND_ZERO(1, SB0
);
590 _FP_ROUND_ZERO(1, SB1
);
592 FP_TO_INT_S(vc
.wp
[0], SB0
, 32, ((func
& 0x3) != 0));
593 FP_TO_INT_S(vc
.wp
[1], SB1
, 32, ((func
& 0x3) != 0));
603 printk("SR0: %ld %08lx %ld (%ld)\n", SR0_s
, SR0_f
, SR0_e
, SR0_c
);
604 printk("SR1: %ld %08lx %ld (%ld)\n", SR1_s
, SR1_f
, SR1_e
, SR1_c
);
606 FP_PACK_SP(vc
.wp
, SR0
);
607 FP_PACK_SP(vc
.wp
+ 1, SR1
);
614 FP_CMP_S(IR0
, SA0
, SB0
, 3);
615 FP_CMP_S(IR1
, SA1
, SB1
, 3);
616 if (IR0
== 3 && (FP_ISSIGNAN_S(SA0
) || FP_ISSIGNAN_S(SB0
)))
617 FP_SET_EXCEPTION(FP_EX_INVALID
);
618 if (IR1
== 3 && (FP_ISSIGNAN_S(SA1
) || FP_ISSIGNAN_S(SB1
)))
619 FP_SET_EXCEPTION(FP_EX_INVALID
);
620 ch
= (IR0
== cmp
) ? 1 : 0;
621 cl
= (IR1
== cmp
) ? 1 : 0;
622 IR
= (ch
<< 3) | (cl
<< 2) | ((ch
| cl
) << 1) |
632 regs
->ccr
&= ~(15 << ((7 - ((speinsn
>> 23) & 0x7)) << 2));
633 regs
->ccr
|= (IR
<< ((7 - ((speinsn
>> 23) & 0x7)) << 2));
636 __FPU_FPSCR
&= ~FP_EX_MASK
;
637 __FPU_FPSCR
|= (FP_CUR_EXCEPTIONS
& FP_EX_MASK
);
638 mtspr(SPRN_SPEFSCR
, __FPU_FPSCR
);
640 current
->thread
.evr
[fc
] = vc
.wp
[0];
641 regs
->gpr
[fc
] = vc
.wp
[1];
644 printk("ccr = %08lx\n", regs
->ccr
);
645 printk("cur exceptions = %08x spefscr = %08lx\n",
646 FP_CUR_EXCEPTIONS
, __FPU_FPSCR
);
647 printk("vc: %08x %08x\n", vc
.wp
[0], vc
.wp
[1]);
648 printk("va: %08x %08x\n", va
.wp
[0], va
.wp
[1]);
649 printk("vb: %08x %08x\n", vb
.wp
[0], vb
.wp
[1]);
655 printk(KERN_ERR
"\nOoops! IEEE-754 compliance handler encountered un-supported instruction.\ninst code: %08lx\n", speinsn
);
659 int speround_handler(struct pt_regs
*regs
)
663 unsigned long speinsn
, type
, fc
;
665 if (get_user(speinsn
, (unsigned int __user
*) regs
->nip
))
667 if ((speinsn
>> 26) != 4)
668 return -EINVAL
; /* not an spe instruction */
670 type
= insn_type(speinsn
& 0x7ff);
671 if (type
== XCR
) return -ENOSYS
;
673 fc
= (speinsn
>> 21) & 0x1f;
674 s_lo
= regs
->gpr
[fc
] & SIGN_BIT_S
;
675 s_hi
= current
->thread
.evr
[fc
] & SIGN_BIT_S
;
676 fgpr
.wp
[0] = current
->thread
.evr
[fc
];
677 fgpr
.wp
[1] = regs
->gpr
[fc
];
679 __FPU_FPSCR
= mfspr(SPRN_SPEFSCR
);
681 switch ((speinsn
>> 5) & 0x7) {
682 /* Since SPE instructions on E500 core can handle round to nearest
683 * and round toward zero with IEEE-754 complied, we just need
684 * to handle round toward +Inf and round toward -Inf by software.
687 if ((FP_ROUNDMODE
) == FP_RND_PINF
) {
688 if (!s_lo
) fgpr
.wp
[1]++; /* Z > 0, choose Z1 */
689 } else { /* round to -Inf */
690 if (s_lo
) fgpr
.wp
[1]++; /* Z < 0, choose Z2 */
695 if (FP_ROUNDMODE
== FP_RND_PINF
) {
696 if (!s_hi
) fgpr
.dp
[0]++; /* Z > 0, choose Z1 */
697 } else { /* round to -Inf */
698 if (s_hi
) fgpr
.dp
[0]++; /* Z < 0, choose Z2 */
703 if (FP_ROUNDMODE
== FP_RND_PINF
) {
704 if (!s_lo
) fgpr
.wp
[1]++; /* Z_low > 0, choose Z1 */
705 if (!s_hi
) fgpr
.wp
[0]++; /* Z_high word > 0, choose Z1 */
706 } else { /* round to -Inf */
707 if (s_lo
) fgpr
.wp
[1]++; /* Z_low < 0, choose Z2 */
708 if (s_hi
) fgpr
.wp
[0]++; /* Z_high < 0, choose Z2 */
716 current
->thread
.evr
[fc
] = fgpr
.wp
[0];
717 regs
->gpr
[fc
] = fgpr
.wp
[1];