1 /* visemul.c: Emulation of VIS instructions.
3 * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
5 #include <linux/kernel.h>
6 #include <linux/errno.h>
7 #include <linux/thread_info.h>
9 #include <asm/ptrace.h>
10 #include <asm/pstate.h>
11 #include <asm/system.h>
12 #include <asm/fpumacro.h>
13 #include <asm/uaccess.h>
15 /* OPF field of various VIS instructions. */
17 /* 000111011 - four 16-bit packs */
18 #define FPACK16_OPF 0x03b
20 /* 000111010 - two 32-bit packs */
21 #define FPACK32_OPF 0x03a
23 /* 000111101 - four 16-bit packs */
24 #define FPACKFIX_OPF 0x03d
26 /* 001001101 - four 16-bit expands */
27 #define FEXPAND_OPF 0x04d
29 /* 001001011 - two 32-bit merges */
30 #define FPMERGE_OPF 0x04b
32 /* 000110001 - 8-by-16-bit partitoned product */
33 #define FMUL8x16_OPF 0x031
35 /* 000110011 - 8-by-16-bit upper alpha partitioned product */
36 #define FMUL8x16AU_OPF 0x033
38 /* 000110101 - 8-by-16-bit lower alpha partitioned product */
39 #define FMUL8x16AL_OPF 0x035
41 /* 000110110 - upper 8-by-16-bit partitioned product */
42 #define FMUL8SUx16_OPF 0x036
44 /* 000110111 - lower 8-by-16-bit partitioned product */
45 #define FMUL8ULx16_OPF 0x037
47 /* 000111000 - upper 8-by-16-bit partitioned product */
48 #define FMULD8SUx16_OPF 0x038
50 /* 000111001 - lower unsigned 8-by-16-bit partitioned product */
51 #define FMULD8ULx16_OPF 0x039
53 /* 000101000 - four 16-bit compare; set rd if src1 > src2 */
54 #define FCMPGT16_OPF 0x028
56 /* 000101100 - two 32-bit compare; set rd if src1 > src2 */
57 #define FCMPGT32_OPF 0x02c
59 /* 000100000 - four 16-bit compare; set rd if src1 <= src2 */
60 #define FCMPLE16_OPF 0x020
62 /* 000100100 - two 32-bit compare; set rd if src1 <= src2 */
63 #define FCMPLE32_OPF 0x024
65 /* 000100010 - four 16-bit compare; set rd if src1 != src2 */
66 #define FCMPNE16_OPF 0x022
68 /* 000100110 - two 32-bit compare; set rd if src1 != src2 */
69 #define FCMPNE32_OPF 0x026
71 /* 000101010 - four 16-bit compare; set rd if src1 == src2 */
72 #define FCMPEQ16_OPF 0x02a
74 /* 000101110 - two 32-bit compare; set rd if src1 == src2 */
75 #define FCMPEQ32_OPF 0x02e
77 /* 000000000 - Eight 8-bit edge boundary processing */
78 #define EDGE8_OPF 0x000
80 /* 000000001 - Eight 8-bit edge boundary processing, no CC */
81 #define EDGE8N_OPF 0x001
83 /* 000000010 - Eight 8-bit edge boundary processing, little-endian */
84 #define EDGE8L_OPF 0x002
86 /* 000000011 - Eight 8-bit edge boundary processing, little-endian, no CC */
87 #define EDGE8LN_OPF 0x003
89 /* 000000100 - Four 16-bit edge boundary processing */
90 #define EDGE16_OPF 0x004
92 /* 000000101 - Four 16-bit edge boundary processing, no CC */
93 #define EDGE16N_OPF 0x005
95 /* 000000110 - Four 16-bit edge boundary processing, little-endian */
96 #define EDGE16L_OPF 0x006
98 /* 000000111 - Four 16-bit edge boundary processing, little-endian, no CC */
99 #define EDGE16LN_OPF 0x007
101 /* 000001000 - Two 32-bit edge boundary processing */
102 #define EDGE32_OPF 0x008
104 /* 000001001 - Two 32-bit edge boundary processing, no CC */
105 #define EDGE32N_OPF 0x009
107 /* 000001010 - Two 32-bit edge boundary processing, little-endian */
108 #define EDGE32L_OPF 0x00a
110 /* 000001011 - Two 32-bit edge boundary processing, little-endian, no CC */
111 #define EDGE32LN_OPF 0x00b
113 /* 000111110 - distance between 8 8-bit components */
114 #define PDIST_OPF 0x03e
116 /* 000010000 - convert 8-bit 3-D address to blocked byte address */
117 #define ARRAY8_OPF 0x010
119 /* 000010010 - convert 16-bit 3-D address to blocked byte address */
120 #define ARRAY16_OPF 0x012
122 /* 000010100 - convert 32-bit 3-D address to blocked byte address */
123 #define ARRAY32_OPF 0x014
125 /* 000011001 - Set the GSR.MASK field in preparation for a BSHUFFLE */
126 #define BMASK_OPF 0x019
128 /* 001001100 - Permute bytes as specified by GSR.MASK */
129 #define BSHUFFLE_OPF 0x04c
131 #define VIS_OPF_SHIFT 5
132 #define VIS_OPF_MASK (0x1ff << VIS_OPF_SHIFT)
134 #define RS1(INSN) (((INSN) >> 24) & 0x1f)
135 #define RS2(INSN) (((INSN) >> 0) & 0x1f)
136 #define RD(INSN) (((INSN) >> 25) & 0x1f)
138 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
139 unsigned int rd, int from_kernel)
141 if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
142 if (from_kernel != 0)
143 __asm__ __volatile__("flushw");
149 static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
154 return (!reg ? 0 : regs->u_regs[reg]);
155 if (regs->tstate & TSTATE_PRIV) {
156 struct reg_window *win;
157 win = (struct reg_window *)(regs->u_regs[UREG_FP] + STACK_BIAS);
158 value = win->locals[reg - 16];
159 } else if (test_thread_flag(TIF_32BIT)) {
160 struct reg_window32 __user *win32;
161 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
162 get_user(value, &win32->locals[reg - 16]);
164 struct reg_window __user *win;
165 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
166 get_user(value, &win->locals[reg - 16]);
171 static inline unsigned long __user *__fetch_reg_addr_user(unsigned int reg,
172 struct pt_regs *regs)
175 BUG_ON(regs->tstate & TSTATE_PRIV);
177 if (test_thread_flag(TIF_32BIT)) {
178 struct reg_window32 __user *win32;
179 win32 = (struct reg_window32 __user *)((unsigned long)((u32)regs->u_regs[UREG_FP]));
180 return (unsigned long __user *)&win32->locals[reg - 16];
182 struct reg_window __user *win;
183 win = (struct reg_window __user *)(regs->u_regs[UREG_FP] + STACK_BIAS);
184 return &win->locals[reg - 16];
188 static inline unsigned long *__fetch_reg_addr_kern(unsigned int reg,
189 struct pt_regs *regs)
192 BUG_ON(regs->tstate & TSTATE_PRIV);
194 return ®s->u_regs[reg];
197 static void store_reg(struct pt_regs *regs, unsigned long val, unsigned long rd)
200 unsigned long *rd_kern = __fetch_reg_addr_kern(rd, regs);
204 unsigned long __user *rd_user = __fetch_reg_addr_user(rd, regs);
206 if (test_thread_flag(TIF_32BIT))
207 __put_user((u32)val, (u32 __user *)rd_user);
209 __put_user(val, rd_user);
213 static inline unsigned long fpd_regval(struct fpustate *f,
214 unsigned int insn_regnum)
216 insn_regnum = (((insn_regnum & 1) << 5) |
217 (insn_regnum & 0x1e));
219 return *(unsigned long *) &f->regs[insn_regnum];
222 static inline unsigned long *fpd_regaddr(struct fpustate *f,
223 unsigned int insn_regnum)
225 insn_regnum = (((insn_regnum & 1) << 5) |
226 (insn_regnum & 0x1e));
228 return (unsigned long *) &f->regs[insn_regnum];
231 static inline unsigned int fps_regval(struct fpustate *f,
232 unsigned int insn_regnum)
234 return f->regs[insn_regnum];
237 static inline unsigned int *fps_regaddr(struct fpustate *f,
238 unsigned int insn_regnum)
240 return &f->regs[insn_regnum];
246 static struct edge_tab edge8_tab[8] = {
256 static struct edge_tab edge8_tab_l[8] = {
266 static struct edge_tab edge16_tab[4] = {
272 static struct edge_tab edge16_tab_l[4] = {
278 static struct edge_tab edge32_tab[2] = {
282 static struct edge_tab edge32_tab_l[2] = {
287 static void edge(struct pt_regs *regs, unsigned int insn, unsigned int opf)
289 unsigned long orig_rs1, rs1, orig_rs2, rs2, rd_val;
292 maybe_flush_windows(RS1(insn), RS2(insn), RD(insn), 0);
293 orig_rs1 = rs1 = fetch_reg(RS1(insn), regs);
294 orig_rs2 = rs2 = fetch_reg(RS2(insn), regs);
296 if (test_thread_flag(TIF_32BIT)) {
297 rs1 = rs1 & 0xffffffff;
298 rs2 = rs2 & 0xffffffff;
304 left = edge8_tab[rs1 & 0x7].left;
305 right = edge8_tab[rs2 & 0x7].right;
309 left = edge8_tab_l[rs1 & 0x7].left;
310 right = edge8_tab_l[rs2 & 0x7].right;
315 left = edge16_tab[(rs1 >> 1) & 0x3].left;
316 right = edge16_tab[(rs2 >> 1) & 0x3].right;
321 left = edge16_tab_l[(rs1 >> 1) & 0x3].left;
322 right = edge16_tab_l[(rs2 >> 1) & 0x3].right;
327 left = edge32_tab[(rs1 >> 2) & 0x1].left;
328 right = edge32_tab[(rs2 >> 2) & 0x1].right;
333 left = edge32_tab_l[(rs1 >> 2) & 0x1].left;
334 right = edge32_tab_l[(rs2 >> 2) & 0x1].right;
338 if ((rs1 & ~0x7UL) == (rs2 & ~0x7UL))
339 rd_val = right & left;
343 store_reg(regs, rd_val, RD(insn));
352 unsigned long ccr, tstate;
354 __asm__ __volatile__("subcc %1, %2, %%g0\n\t"
357 : "r" (orig_rs1), "r" (orig_rs2)
359 tstate = regs->tstate & ~(TSTATE_XCC | TSTATE_ICC);
360 regs->tstate = tstate | (ccr << 32UL);
365 static void array(struct pt_regs *regs, unsigned int insn, unsigned int opf)
367 unsigned long rs1, rs2, rd_val;
368 unsigned int bits, bits_mask;
370 maybe_flush_windows(RS1(insn), RS2(insn), RD(insn), 0);
371 rs1 = fetch_reg(RS1(insn), regs);
372 rs2 = fetch_reg(RS2(insn), regs);
374 bits = (rs2 > 5 ? 5 : rs2);
375 bits_mask = (1UL << bits) - 1UL;
377 rd_val = ((((rs1 >> 11) & 0x3) << 0) |
378 (((rs1 >> 33) & 0x3) << 2) |
379 (((rs1 >> 55) & 0x1) << 4) |
380 (((rs1 >> 13) & 0xf) << 5) |
381 (((rs1 >> 35) & 0xf) << 9) |
382 (((rs1 >> 56) & 0xf) << 13) |
383 (((rs1 >> 17) & bits_mask) << 17) |
384 (((rs1 >> 39) & bits_mask) << (17 + bits)) |
385 (((rs1 >> 60) & 0xf) << (17 + (2*bits))));
396 store_reg(regs, rd_val, RD(insn));
399 static void bmask(struct pt_regs *regs, unsigned int insn)
401 unsigned long rs1, rs2, rd_val, gsr;
403 maybe_flush_windows(RS1(insn), RS2(insn), RD(insn), 0);
404 rs1 = fetch_reg(RS1(insn), regs);
405 rs2 = fetch_reg(RS2(insn), regs);
408 store_reg(regs, rd_val, RD(insn));
410 gsr = current_thread_info()->gsr[0] & 0xffffffff;
411 gsr |= rd_val << 32UL;
412 current_thread_info()->gsr[0] = gsr;
415 static void bshuffle(struct pt_regs *regs, unsigned int insn)
417 struct fpustate *f = FPUSTATE;
418 unsigned long rs1, rs2, rd_val;
419 unsigned long bmask, i;
421 bmask = current_thread_info()->gsr[0] >> 32UL;
423 rs1 = fpd_regval(f, RS1(insn));
424 rs2 = fpd_regval(f, RS2(insn));
427 for (i = 0; i < 8; i++) {
428 unsigned long which = (bmask >> (i * 4)) & 0xf;
432 byte = (rs1 >> (which * 8)) & 0xff;
434 byte = (rs2 >> ((which-8)*8)) & 0xff;
435 rd_val |= (byte << (i * 8));
438 *fpd_regaddr(f, RD(insn)) = rd_val;
441 static void pdist(struct pt_regs *regs, unsigned int insn)
443 struct fpustate *f = FPUSTATE;
444 unsigned long rs1, rs2, *rd, rd_val;
447 rs1 = fpd_regval(f, RS1(insn));
448 rs2 = fpd_regval(f, RS1(insn));
449 rd = fpd_regaddr(f, RD(insn));
453 for (i = 0; i < 8; i++) {
456 s1 = (rs1 >> (56 - (i * 8))) & 0xff;
457 s2 = (rs2 >> (56 - (i * 8))) & 0xff;
459 /* Absolute value of difference. */
470 static void pformat(struct pt_regs *regs, unsigned int insn, unsigned int opf)
472 struct fpustate *f = FPUSTATE;
473 unsigned long rs1, rs2, gsr, scale, rd_val;
475 gsr = current_thread_info()->gsr[0];
476 scale = (gsr >> 3) & (opf == FPACK16_OPF ? 0xf : 0x1f);
481 rs2 = fpd_regval(f, RS2(insn));
483 for (byte = 0; byte < 4; byte++) {
485 s16 src = (rs2 >> (byte * 16UL)) & 0xffffUL;
486 int scaled = src << scale;
487 int from_fixed = scaled >> 7;
489 val = ((from_fixed < 0) ?
494 rd_val |= (val << (8 * byte));
496 *fps_regaddr(f, RD(insn)) = rd_val;
503 rs1 = fpd_regval(f, RS1(insn));
504 rs2 = fpd_regval(f, RS2(insn));
505 rd_val = (rs1 << 8) & ~(0x000000ff000000ffUL);
506 for (word = 0; word < 2; word++) {
508 s32 src = (rs2 >> (word * 32UL));
509 s64 scaled = src << scale;
510 s64 from_fixed = scaled >> 23;
512 val = ((from_fixed < 0) ?
517 rd_val |= (val << (32 * word));
519 *fpd_regaddr(f, RD(insn)) = rd_val;
526 rs2 = fpd_regval(f, RS2(insn));
529 for (word = 0; word < 2; word++) {
531 s32 src = (rs2 >> (word * 32UL));
532 s64 scaled = src << scale;
533 s64 from_fixed = scaled >> 16;
535 val = ((from_fixed < -32768) ?
537 (from_fixed > 32767) ?
540 rd_val |= ((val & 0xffff) << (word * 16));
542 *fps_regaddr(f, RD(insn)) = rd_val;
549 rs2 = fps_regval(f, RS2(insn));
552 for (byte = 0; byte < 4; byte++) {
554 u8 src = (rs2 >> (byte * 8)) & 0xff;
558 rd_val |= (val << (byte * 16));
560 *fpd_regaddr(f, RD(insn)) = rd_val;
565 rs1 = fps_regval(f, RS1(insn));
566 rs2 = fps_regval(f, RS2(insn));
568 rd_val = (((rs2 & 0x000000ff) << 0) |
569 ((rs1 & 0x000000ff) << 8) |
570 ((rs2 & 0x0000ff00) << 8) |
571 ((rs1 & 0x0000ff00) << 16) |
572 ((rs2 & 0x00ff0000) << 16) |
573 ((rs1 & 0x00ff0000) << 24) |
574 ((rs2 & 0xff000000) << 24) |
575 ((rs1 & 0xff000000) << 32));
576 *fpd_regaddr(f, RD(insn)) = rd_val;
582 static void pmul(struct pt_regs *regs, unsigned int insn, unsigned int opf)
584 struct fpustate *f = FPUSTATE;
585 unsigned long rs1, rs2, rd_val;
591 rs1 = fps_regval(f, RS1(insn));
592 rs2 = fpd_regval(f, RS2(insn));
595 for (byte = 0; byte < 4; byte++) {
596 u16 src1 = (rs1 >> (byte * 8)) & 0x00ff;
597 s16 src2 = (rs2 >> (byte * 16)) & 0xffff;
598 u32 prod = src1 * src2;
599 u16 scaled = ((prod & 0x00ffff00) >> 8);
604 rd_val |= ((scaled & 0xffffUL) << (byte * 16UL));
607 *fpd_regaddr(f, RD(insn)) = rd_val;
612 case FMUL8x16AL_OPF: {
616 rs1 = fps_regval(f, RS1(insn));
617 rs2 = fps_regval(f, RS2(insn));
620 src2 = (rs2 >> (opf == FMUL8x16AU_OPF) ? 16 : 0);
621 for (byte = 0; byte < 4; byte++) {
622 u16 src1 = (rs1 >> (byte * 8)) & 0x00ff;
623 u32 prod = src1 * src2;
624 u16 scaled = ((prod & 0x00ffff00) >> 8);
629 rd_val |= ((scaled & 0xffffUL) << (byte * 16UL));
632 *fpd_regaddr(f, RD(insn)) = rd_val;
637 case FMUL8ULx16_OPF: {
638 unsigned long byte, ushift;
640 rs1 = fpd_regval(f, RS1(insn));
641 rs2 = fpd_regval(f, RS2(insn));
644 ushift = (opf == FMUL8SUx16_OPF) ? 8 : 0;
645 for (byte = 0; byte < 4; byte++) {
651 src1 = ((rs1 >> ((16 * byte) + ushift)) & 0x00ff);
652 src2 = ((rs2 >> (16 * byte)) & 0xffff);
654 scaled = ((prod & 0x00ffff00) >> 8);
659 rd_val |= ((scaled & 0xffffUL) << (byte * 16UL));
662 *fpd_regaddr(f, RD(insn)) = rd_val;
666 case FMULD8SUx16_OPF:
667 case FMULD8ULx16_OPF: {
668 unsigned long byte, ushift;
670 rs1 = fps_regval(f, RS1(insn));
671 rs2 = fps_regval(f, RS2(insn));
674 ushift = (opf == FMULD8SUx16_OPF) ? 8 : 0;
675 for (byte = 0; byte < 2; byte++) {
681 src1 = ((rs1 >> ((16 * byte) + ushift)) & 0x00ff);
682 src2 = ((rs2 >> (16 * byte)) & 0xffff);
684 scaled = ((prod & 0x00ffff00) >> 8);
689 rd_val |= ((scaled & 0xffffUL) <<
690 ((byte * 32UL) + 7UL));
692 *fpd_regaddr(f, RD(insn)) = rd_val;
698 static void pcmp(struct pt_regs *regs, unsigned int insn, unsigned int opf)
700 struct fpustate *f = FPUSTATE;
701 unsigned long rs1, rs2, rd_val, i;
703 rs1 = fpd_regval(f, RS1(insn));
704 rs2 = fpd_regval(f, RS2(insn));
710 for (i = 0; i < 4; i++) {
711 s16 a = (rs1 >> (i * 16)) & 0xffff;
712 s16 b = (rs2 >> (i * 16)) & 0xffff;
720 for (i = 0; i < 2; i++) {
721 s32 a = (rs1 >> (i * 32)) & 0xffff;
722 s32 b = (rs2 >> (i * 32)) & 0xffff;
730 for (i = 0; i < 4; i++) {
731 s16 a = (rs1 >> (i * 16)) & 0xffff;
732 s16 b = (rs2 >> (i * 16)) & 0xffff;
740 for (i = 0; i < 2; i++) {
741 s32 a = (rs1 >> (i * 32)) & 0xffff;
742 s32 b = (rs2 >> (i * 32)) & 0xffff;
750 for (i = 0; i < 4; i++) {
751 s16 a = (rs1 >> (i * 16)) & 0xffff;
752 s16 b = (rs2 >> (i * 16)) & 0xffff;
760 for (i = 0; i < 2; i++) {
761 s32 a = (rs1 >> (i * 32)) & 0xffff;
762 s32 b = (rs2 >> (i * 32)) & 0xffff;
770 for (i = 0; i < 4; i++) {
771 s16 a = (rs1 >> (i * 16)) & 0xffff;
772 s16 b = (rs2 >> (i * 16)) & 0xffff;
780 for (i = 0; i < 2; i++) {
781 s32 a = (rs1 >> (i * 32)) & 0xffff;
782 s32 b = (rs2 >> (i * 32)) & 0xffff;
790 maybe_flush_windows(0, 0, RD(insn), 0);
791 store_reg(regs, rd_val, RD(insn));
794 /* Emulate the VIS instructions which are not implemented in
795 * hardware on Niagara.
797 int vis_emul(struct pt_regs *regs, unsigned int insn)
799 unsigned long pc = regs->tpc;
802 BUG_ON(regs->tstate & TSTATE_PRIV);
804 if (test_thread_flag(TIF_32BIT))
807 if (get_user(insn, (u32 __user *) pc))
810 opf = (insn & VIS_OPF_MASK) >> VIS_OPF_SHIFT;
815 /* Pixel Formatting Instructions. */
821 pformat(regs, insn, opf);
824 /* Partitioned Multiply Instructions */
830 case FMULD8SUx16_OPF:
831 case FMULD8ULx16_OPF:
832 pmul(regs, insn, opf);
835 /* Pixel Compare Instructions */
844 pcmp(regs, insn, opf);
847 /* Edge Handling Instructions */
860 edge(regs, insn, opf);
863 /* Pixel Component Distance */
868 /* Three-Dimensional Array Addressing Instructions */
872 array(regs, insn, opf);
875 /* Byte Mask and Shuffle Instructions */
881 bshuffle(regs, insn);
885 regs->tpc = regs->tnpc;