1 // SPDX-License-Identifier: GPL-2.0
2 /* visemul.c: Emulation of VIS instructions.
4 * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/errno.h>
8 #include <linux/thread_info.h>
9 #include <linux/perf_event.h>
11 #include <asm/ptrace.h>
12 #include <asm/pstate.h>
13 #include <asm/fpumacro.h>
14 #include <linux/uaccess.h>
15 #include <asm/cacheflush.h>
17 /* OPF field of various VIS instructions. */
19 /* 000111011 - four 16-bit packs */
20 #define FPACK16_OPF 0x03b
22 /* 000111010 - two 32-bit packs */
23 #define FPACK32_OPF 0x03a
25 /* 000111101 - four 16-bit packs */
26 #define FPACKFIX_OPF 0x03d
28 /* 001001101 - four 16-bit expands */
29 #define FEXPAND_OPF 0x04d
31 /* 001001011 - two 32-bit merges */
32 #define FPMERGE_OPF 0x04b
34 /* 000110001 - 8-by-16-bit partitioned product */
35 #define FMUL8x16_OPF 0x031
37 /* 000110011 - 8-by-16-bit upper alpha partitioned product */
38 #define FMUL8x16AU_OPF 0x033
40 /* 000110101 - 8-by-16-bit lower alpha partitioned product */
41 #define FMUL8x16AL_OPF 0x035
43 /* 000110110 - upper 8-by-16-bit partitioned product */
44 #define FMUL8SUx16_OPF 0x036
46 /* 000110111 - lower 8-by-16-bit partitioned product */
47 #define FMUL8ULx16_OPF 0x037
49 /* 000111000 - upper 8-by-16-bit partitioned product */
50 #define FMULD8SUx16_OPF 0x038
52 /* 000111001 - lower unsigned 8-by-16-bit partitioned product */
53 #define FMULD8ULx16_OPF 0x039
55 /* 000101000 - four 16-bit compare; set rd if src1 > src2 */
56 #define FCMPGT16_OPF 0x028
58 /* 000101100 - two 32-bit compare; set rd if src1 > src2 */
59 #define FCMPGT32_OPF 0x02c
61 /* 000100000 - four 16-bit compare; set rd if src1 <= src2 */
62 #define FCMPLE16_OPF 0x020
64 /* 000100100 - two 32-bit compare; set rd if src1 <= src2 */
65 #define FCMPLE32_OPF 0x024
67 /* 000100010 - four 16-bit compare; set rd if src1 != src2 */
68 #define FCMPNE16_OPF 0x022
70 /* 000100110 - two 32-bit compare; set rd if src1 != src2 */
71 #define FCMPNE32_OPF 0x026
73 /* 000101010 - four 16-bit compare; set rd if src1 == src2 */
74 #define FCMPEQ16_OPF 0x02a
76 /* 000101110 - two 32-bit compare; set rd if src1 == src2 */
77 #define FCMPEQ32_OPF 0x02e
79 /* 000000000 - Eight 8-bit edge boundary processing */
80 #define EDGE8_OPF 0x000
82 /* 000000001 - Eight 8-bit edge boundary processing, no CC */
83 #define EDGE8N_OPF 0x001
85 /* 000000010 - Eight 8-bit edge boundary processing, little-endian */
86 #define EDGE8L_OPF 0x002
88 /* 000000011 - Eight 8-bit edge boundary processing, little-endian, no CC */
89 #define EDGE8LN_OPF 0x003
91 /* 000000100 - Four 16-bit edge boundary processing */
92 #define EDGE16_OPF 0x004
94 /* 000000101 - Four 16-bit edge boundary processing, no CC */
95 #define EDGE16N_OPF 0x005
97 /* 000000110 - Four 16-bit edge boundary processing, little-endian */
98 #define EDGE16L_OPF 0x006
100 /* 000000111 - Four 16-bit edge boundary processing, little-endian, no CC */
101 #define EDGE16LN_OPF 0x007
103 /* 000001000 - Two 32-bit edge boundary processing */
104 #define EDGE32_OPF 0x008
106 /* 000001001 - Two 32-bit edge boundary processing, no CC */
107 #define EDGE32N_OPF 0x009
109 /* 000001010 - Two 32-bit edge boundary processing, little-endian */
110 #define EDGE32L_OPF 0x00a
112 /* 000001011 - Two 32-bit edge boundary processing, little-endian, no CC */
113 #define EDGE32LN_OPF 0x00b
115 /* 000111110 - distance between 8 8-bit components */
116 #define PDIST_OPF 0x03e
118 /* 000010000 - convert 8-bit 3-D address to blocked byte address */
119 #define ARRAY8_OPF 0x010
121 /* 000010010 - convert 16-bit 3-D address to blocked byte address */
122 #define ARRAY16_OPF 0x012
124 /* 000010100 - convert 32-bit 3-D address to blocked byte address */
125 #define ARRAY32_OPF 0x014
127 /* 000011001 - Set the GSR.MASK field in preparation for a BSHUFFLE */
128 #define BMASK_OPF 0x019
130 /* 001001100 - Permute bytes as specified by GSR.MASK */
131 #define BSHUFFLE_OPF 0x04c
133 #define VIS_OPF_SHIFT 5
134 #define VIS_OPF_MASK (0x1ff << VIS_OPF_SHIFT)
136 #define RS1(INSN) (((INSN) >> 14) & 0x1f)
137 #define RS2(INSN) (((INSN) >> 0) & 0x1f)
138 #define RD(INSN) (((INSN) >> 25) & 0x1f)
140 static inline void maybe_flush_windows(unsigned int rs1
, unsigned int rs2
,
141 unsigned int rd
, int from_kernel
)
143 if (rs2
>= 16 || rs1
>= 16 || rd
>= 16) {
144 if (from_kernel
!= 0)
145 __asm__
__volatile__("flushw");
151 static unsigned long fetch_reg(unsigned int reg
, struct pt_regs
*regs
)
153 unsigned long value
, fp
;
156 return (!reg
? 0 : regs
->u_regs
[reg
]);
158 fp
= regs
->u_regs
[UREG_FP
];
160 if (regs
->tstate
& TSTATE_PRIV
) {
161 struct reg_window
*win
;
162 win
= (struct reg_window
*)(fp
+ STACK_BIAS
);
163 value
= win
->locals
[reg
- 16];
164 } else if (!test_thread_64bit_stack(fp
)) {
165 struct reg_window32 __user
*win32
;
166 win32
= (struct reg_window32 __user
*)((unsigned long)((u32
)fp
));
167 get_user(value
, &win32
->locals
[reg
- 16]);
169 struct reg_window __user
*win
;
170 win
= (struct reg_window __user
*)(fp
+ STACK_BIAS
);
171 get_user(value
, &win
->locals
[reg
- 16]);
176 static inline unsigned long __user
*__fetch_reg_addr_user(unsigned int reg
,
177 struct pt_regs
*regs
)
179 unsigned long fp
= regs
->u_regs
[UREG_FP
];
182 BUG_ON(regs
->tstate
& TSTATE_PRIV
);
184 if (!test_thread_64bit_stack(fp
)) {
185 struct reg_window32 __user
*win32
;
186 win32
= (struct reg_window32 __user
*)((unsigned long)((u32
)fp
));
187 return (unsigned long __user
*)&win32
->locals
[reg
- 16];
189 struct reg_window __user
*win
;
190 win
= (struct reg_window __user
*)(fp
+ STACK_BIAS
);
191 return &win
->locals
[reg
- 16];
195 static inline unsigned long *__fetch_reg_addr_kern(unsigned int reg
,
196 struct pt_regs
*regs
)
199 BUG_ON(regs
->tstate
& TSTATE_PRIV
);
201 return ®s
->u_regs
[reg
];
204 static void store_reg(struct pt_regs
*regs
, unsigned long val
, unsigned long rd
)
207 unsigned long *rd_kern
= __fetch_reg_addr_kern(rd
, regs
);
211 unsigned long __user
*rd_user
= __fetch_reg_addr_user(rd
, regs
);
213 if (!test_thread_64bit_stack(regs
->u_regs
[UREG_FP
]))
214 __put_user((u32
)val
, (u32 __user
*)rd_user
);
216 __put_user(val
, rd_user
);
220 static inline unsigned long fpd_regval(struct fpustate
*f
,
221 unsigned int insn_regnum
)
223 insn_regnum
= (((insn_regnum
& 1) << 5) |
224 (insn_regnum
& 0x1e));
226 return *(unsigned long *) &f
->regs
[insn_regnum
];
229 static inline unsigned long *fpd_regaddr(struct fpustate
*f
,
230 unsigned int insn_regnum
)
232 insn_regnum
= (((insn_regnum
& 1) << 5) |
233 (insn_regnum
& 0x1e));
235 return (unsigned long *) &f
->regs
[insn_regnum
];
238 static inline unsigned int fps_regval(struct fpustate
*f
,
239 unsigned int insn_regnum
)
241 return f
->regs
[insn_regnum
];
244 static inline unsigned int *fps_regaddr(struct fpustate
*f
,
245 unsigned int insn_regnum
)
247 return &f
->regs
[insn_regnum
];
253 static struct edge_tab edge8_tab
[8] = {
263 static struct edge_tab edge8_tab_l
[8] = {
273 static struct edge_tab edge16_tab
[4] = {
279 static struct edge_tab edge16_tab_l
[4] = {
285 static struct edge_tab edge32_tab
[2] = {
289 static struct edge_tab edge32_tab_l
[2] = {
294 static void edge(struct pt_regs
*regs
, unsigned int insn
, unsigned int opf
)
296 unsigned long orig_rs1
, rs1
, orig_rs2
, rs2
, rd_val
;
299 maybe_flush_windows(RS1(insn
), RS2(insn
), RD(insn
), 0);
300 orig_rs1
= rs1
= fetch_reg(RS1(insn
), regs
);
301 orig_rs2
= rs2
= fetch_reg(RS2(insn
), regs
);
303 if (test_thread_flag(TIF_32BIT
)) {
304 rs1
= rs1
& 0xffffffff;
305 rs2
= rs2
& 0xffffffff;
311 left
= edge8_tab
[rs1
& 0x7].left
;
312 right
= edge8_tab
[rs2
& 0x7].right
;
316 left
= edge8_tab_l
[rs1
& 0x7].left
;
317 right
= edge8_tab_l
[rs2
& 0x7].right
;
322 left
= edge16_tab
[(rs1
>> 1) & 0x3].left
;
323 right
= edge16_tab
[(rs2
>> 1) & 0x3].right
;
328 left
= edge16_tab_l
[(rs1
>> 1) & 0x3].left
;
329 right
= edge16_tab_l
[(rs2
>> 1) & 0x3].right
;
334 left
= edge32_tab
[(rs1
>> 2) & 0x1].left
;
335 right
= edge32_tab
[(rs2
>> 2) & 0x1].right
;
340 left
= edge32_tab_l
[(rs1
>> 2) & 0x1].left
;
341 right
= edge32_tab_l
[(rs2
>> 2) & 0x1].right
;
345 if ((rs1
& ~0x7UL
) == (rs2
& ~0x7UL
))
346 rd_val
= right
& left
;
350 store_reg(regs
, rd_val
, RD(insn
));
359 unsigned long ccr
, tstate
;
361 __asm__
__volatile__("subcc %1, %2, %%g0\n\t"
364 : "r" (orig_rs1
), "r" (orig_rs2
)
366 tstate
= regs
->tstate
& ~(TSTATE_XCC
| TSTATE_ICC
);
367 regs
->tstate
= tstate
| (ccr
<< 32UL);
372 static void array(struct pt_regs
*regs
, unsigned int insn
, unsigned int opf
)
374 unsigned long rs1
, rs2
, rd_val
;
375 unsigned int bits
, bits_mask
;
377 maybe_flush_windows(RS1(insn
), RS2(insn
), RD(insn
), 0);
378 rs1
= fetch_reg(RS1(insn
), regs
);
379 rs2
= fetch_reg(RS2(insn
), regs
);
381 bits
= (rs2
> 5 ? 5 : rs2
);
382 bits_mask
= (1UL << bits
) - 1UL;
384 rd_val
= ((((rs1
>> 11) & 0x3) << 0) |
385 (((rs1
>> 33) & 0x3) << 2) |
386 (((rs1
>> 55) & 0x1) << 4) |
387 (((rs1
>> 13) & 0xf) << 5) |
388 (((rs1
>> 35) & 0xf) << 9) |
389 (((rs1
>> 56) & 0xf) << 13) |
390 (((rs1
>> 17) & bits_mask
) << 17) |
391 (((rs1
>> 39) & bits_mask
) << (17 + bits
)) |
392 (((rs1
>> 60) & 0xf) << (17 + (2*bits
))));
403 store_reg(regs
, rd_val
, RD(insn
));
406 static void bmask(struct pt_regs
*regs
, unsigned int insn
)
408 unsigned long rs1
, rs2
, rd_val
, gsr
;
410 maybe_flush_windows(RS1(insn
), RS2(insn
), RD(insn
), 0);
411 rs1
= fetch_reg(RS1(insn
), regs
);
412 rs2
= fetch_reg(RS2(insn
), regs
);
415 store_reg(regs
, rd_val
, RD(insn
));
417 gsr
= current_thread_info()->gsr
[0] & 0xffffffff;
418 gsr
|= rd_val
<< 32UL;
419 current_thread_info()->gsr
[0] = gsr
;
422 static void bshuffle(struct pt_regs
*regs
, unsigned int insn
)
424 struct fpustate
*f
= FPUSTATE
;
425 unsigned long rs1
, rs2
, rd_val
;
426 unsigned long bmask
, i
;
428 bmask
= current_thread_info()->gsr
[0] >> 32UL;
430 rs1
= fpd_regval(f
, RS1(insn
));
431 rs2
= fpd_regval(f
, RS2(insn
));
434 for (i
= 0; i
< 8; i
++) {
435 unsigned long which
= (bmask
>> (i
* 4)) & 0xf;
439 byte
= (rs1
>> (which
* 8)) & 0xff;
441 byte
= (rs2
>> ((which
-8)*8)) & 0xff;
442 rd_val
|= (byte
<< (i
* 8));
445 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
448 static void pdist(struct pt_regs
*regs
, unsigned int insn
)
450 struct fpustate
*f
= FPUSTATE
;
451 unsigned long rs1
, rs2
, *rd
, rd_val
;
454 rs1
= fpd_regval(f
, RS1(insn
));
455 rs2
= fpd_regval(f
, RS2(insn
));
456 rd
= fpd_regaddr(f
, RD(insn
));
460 for (i
= 0; i
< 8; i
++) {
463 s1
= (rs1
>> (56 - (i
* 8))) & 0xff;
464 s2
= (rs2
>> (56 - (i
* 8))) & 0xff;
466 /* Absolute value of difference. */
477 static void pformat(struct pt_regs
*regs
, unsigned int insn
, unsigned int opf
)
479 struct fpustate
*f
= FPUSTATE
;
480 unsigned long rs1
, rs2
, gsr
, scale
, rd_val
;
482 gsr
= current_thread_info()->gsr
[0];
483 scale
= (gsr
>> 3) & (opf
== FPACK16_OPF
? 0xf : 0x1f);
488 rs2
= fpd_regval(f
, RS2(insn
));
490 for (byte
= 0; byte
< 4; byte
++) {
492 s16 src
= (rs2
>> (byte
* 16UL)) & 0xffffUL
;
493 int scaled
= src
<< scale
;
494 int from_fixed
= scaled
>> 7;
496 val
= ((from_fixed
< 0) ?
501 rd_val
|= (val
<< (8 * byte
));
503 *fps_regaddr(f
, RD(insn
)) = rd_val
;
510 rs1
= fpd_regval(f
, RS1(insn
));
511 rs2
= fpd_regval(f
, RS2(insn
));
512 rd_val
= (rs1
<< 8) & ~(0x000000ff000000ffUL
);
513 for (word
= 0; word
< 2; word
++) {
515 s32 src
= (rs2
>> (word
* 32UL));
516 s64 scaled
= src
<< scale
;
517 s64 from_fixed
= scaled
>> 23;
519 val
= ((from_fixed
< 0) ?
524 rd_val
|= (val
<< (32 * word
));
526 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
533 rs2
= fpd_regval(f
, RS2(insn
));
536 for (word
= 0; word
< 2; word
++) {
538 s32 src
= (rs2
>> (word
* 32UL));
539 s64 scaled
= src
<< scale
;
540 s64 from_fixed
= scaled
>> 16;
542 val
= ((from_fixed
< -32768) ?
544 (from_fixed
> 32767) ?
547 rd_val
|= ((val
& 0xffff) << (word
* 16));
549 *fps_regaddr(f
, RD(insn
)) = rd_val
;
556 rs2
= fps_regval(f
, RS2(insn
));
559 for (byte
= 0; byte
< 4; byte
++) {
561 u8 src
= (rs2
>> (byte
* 8)) & 0xff;
565 rd_val
|= (val
<< (byte
* 16));
567 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
572 rs1
= fps_regval(f
, RS1(insn
));
573 rs2
= fps_regval(f
, RS2(insn
));
575 rd_val
= (((rs2
& 0x000000ff) << 0) |
576 ((rs1
& 0x000000ff) << 8) |
577 ((rs2
& 0x0000ff00) << 8) |
578 ((rs1
& 0x0000ff00) << 16) |
579 ((rs2
& 0x00ff0000) << 16) |
580 ((rs1
& 0x00ff0000) << 24) |
581 ((rs2
& 0xff000000) << 24) |
582 ((rs1
& 0xff000000) << 32));
583 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
589 static void pmul(struct pt_regs
*regs
, unsigned int insn
, unsigned int opf
)
591 struct fpustate
*f
= FPUSTATE
;
592 unsigned long rs1
, rs2
, rd_val
;
598 rs1
= fps_regval(f
, RS1(insn
));
599 rs2
= fpd_regval(f
, RS2(insn
));
602 for (byte
= 0; byte
< 4; byte
++) {
603 u16 src1
= (rs1
>> (byte
* 8)) & 0x00ff;
604 s16 src2
= (rs2
>> (byte
* 16)) & 0xffff;
605 u32 prod
= src1
* src2
;
606 u16 scaled
= ((prod
& 0x00ffff00) >> 8);
611 rd_val
|= ((scaled
& 0xffffUL
) << (byte
* 16UL));
614 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
619 case FMUL8x16AL_OPF
: {
623 rs1
= fps_regval(f
, RS1(insn
));
624 rs2
= fps_regval(f
, RS2(insn
));
627 src2
= rs2
>> (opf
== FMUL8x16AU_OPF
? 16 : 0);
628 for (byte
= 0; byte
< 4; byte
++) {
629 u16 src1
= (rs1
>> (byte
* 8)) & 0x00ff;
630 u32 prod
= src1
* src2
;
631 u16 scaled
= ((prod
& 0x00ffff00) >> 8);
636 rd_val
|= ((scaled
& 0xffffUL
) << (byte
* 16UL));
639 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
644 case FMUL8ULx16_OPF
: {
645 unsigned long byte
, ushift
;
647 rs1
= fpd_regval(f
, RS1(insn
));
648 rs2
= fpd_regval(f
, RS2(insn
));
651 ushift
= (opf
== FMUL8SUx16_OPF
) ? 8 : 0;
652 for (byte
= 0; byte
< 4; byte
++) {
658 src1
= ((rs1
>> ((16 * byte
) + ushift
)) & 0x00ff);
659 src2
= ((rs2
>> (16 * byte
)) & 0xffff);
661 scaled
= ((prod
& 0x00ffff00) >> 8);
666 rd_val
|= ((scaled
& 0xffffUL
) << (byte
* 16UL));
669 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
673 case FMULD8SUx16_OPF
:
674 case FMULD8ULx16_OPF
: {
675 unsigned long byte
, ushift
;
677 rs1
= fps_regval(f
, RS1(insn
));
678 rs2
= fps_regval(f
, RS2(insn
));
681 ushift
= (opf
== FMULD8SUx16_OPF
) ? 8 : 0;
682 for (byte
= 0; byte
< 2; byte
++) {
688 src1
= ((rs1
>> ((16 * byte
) + ushift
)) & 0x00ff);
689 src2
= ((rs2
>> (16 * byte
)) & 0xffff);
691 scaled
= ((prod
& 0x00ffff00) >> 8);
696 rd_val
|= ((scaled
& 0xffffUL
) <<
697 ((byte
* 32UL) + 7UL));
699 *fpd_regaddr(f
, RD(insn
)) = rd_val
;
705 static void pcmp(struct pt_regs
*regs
, unsigned int insn
, unsigned int opf
)
707 struct fpustate
*f
= FPUSTATE
;
708 unsigned long rs1
, rs2
, rd_val
, i
;
710 rs1
= fpd_regval(f
, RS1(insn
));
711 rs2
= fpd_regval(f
, RS2(insn
));
717 for (i
= 0; i
< 4; i
++) {
718 s16 a
= (rs1
>> (i
* 16)) & 0xffff;
719 s16 b
= (rs2
>> (i
* 16)) & 0xffff;
727 for (i
= 0; i
< 2; i
++) {
728 s32 a
= (rs1
>> (i
* 32)) & 0xffffffff;
729 s32 b
= (rs2
>> (i
* 32)) & 0xffffffff;
737 for (i
= 0; i
< 4; i
++) {
738 s16 a
= (rs1
>> (i
* 16)) & 0xffff;
739 s16 b
= (rs2
>> (i
* 16)) & 0xffff;
747 for (i
= 0; i
< 2; i
++) {
748 s32 a
= (rs1
>> (i
* 32)) & 0xffffffff;
749 s32 b
= (rs2
>> (i
* 32)) & 0xffffffff;
757 for (i
= 0; i
< 4; i
++) {
758 s16 a
= (rs1
>> (i
* 16)) & 0xffff;
759 s16 b
= (rs2
>> (i
* 16)) & 0xffff;
767 for (i
= 0; i
< 2; i
++) {
768 s32 a
= (rs1
>> (i
* 32)) & 0xffffffff;
769 s32 b
= (rs2
>> (i
* 32)) & 0xffffffff;
777 for (i
= 0; i
< 4; i
++) {
778 s16 a
= (rs1
>> (i
* 16)) & 0xffff;
779 s16 b
= (rs2
>> (i
* 16)) & 0xffff;
787 for (i
= 0; i
< 2; i
++) {
788 s32 a
= (rs1
>> (i
* 32)) & 0xffffffff;
789 s32 b
= (rs2
>> (i
* 32)) & 0xffffffff;
797 maybe_flush_windows(0, 0, RD(insn
), 0);
798 store_reg(regs
, rd_val
, RD(insn
));
801 /* Emulate the VIS instructions which are not implemented in
802 * hardware on Niagara.
804 int vis_emul(struct pt_regs
*regs
, unsigned int insn
)
806 unsigned long pc
= regs
->tpc
;
809 BUG_ON(regs
->tstate
& TSTATE_PRIV
);
811 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS
, 1, regs
, 0);
813 if (test_thread_flag(TIF_32BIT
))
816 if (get_user(insn
, (u32 __user
*) pc
))
819 save_and_clear_fpu();
821 opf
= (insn
& VIS_OPF_MASK
) >> VIS_OPF_SHIFT
;
826 /* Pixel Formatting Instructions. */
832 pformat(regs
, insn
, opf
);
835 /* Partitioned Multiply Instructions */
841 case FMULD8SUx16_OPF
:
842 case FMULD8ULx16_OPF
:
843 pmul(regs
, insn
, opf
);
846 /* Pixel Compare Instructions */
855 pcmp(regs
, insn
, opf
);
858 /* Edge Handling Instructions */
871 edge(regs
, insn
, opf
);
874 /* Pixel Component Distance */
879 /* Three-Dimensional Array Addressing Instructions */
883 array(regs
, insn
, opf
);
886 /* Byte Mask and Shuffle Instructions */
892 bshuffle(regs
, insn
);
896 regs
->tpc
= regs
->tnpc
;