visemul.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /* visemul.c: Emulation of VIS instructions.
  2. *
  3. * Copyright (C) 2006 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/errno.h>
  7. #include <linux/thread_info.h>
  8. #include <linux/perf_event.h>
  9. #include <asm/ptrace.h>
  10. #include <asm/pstate.h>
  11. #include <asm/fpumacro.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/cacheflush.h>
  14. /* OPF field of various VIS instructions. */
  15. /* 000111011 - four 16-bit packs */
  16. #define FPACK16_OPF 0x03b
  17. /* 000111010 - two 32-bit packs */
  18. #define FPACK32_OPF 0x03a
  19. /* 000111101 - four 16-bit packs */
  20. #define FPACKFIX_OPF 0x03d
  21. /* 001001101 - four 16-bit expands */
  22. #define FEXPAND_OPF 0x04d
  23. /* 001001011 - two 32-bit merges */
  24. #define FPMERGE_OPF 0x04b
  25. /* 000110001 - 8-by-16-bit partitoned product */
  26. #define FMUL8x16_OPF 0x031
  27. /* 000110011 - 8-by-16-bit upper alpha partitioned product */
  28. #define FMUL8x16AU_OPF 0x033
  29. /* 000110101 - 8-by-16-bit lower alpha partitioned product */
  30. #define FMUL8x16AL_OPF 0x035
  31. /* 000110110 - upper 8-by-16-bit partitioned product */
  32. #define FMUL8SUx16_OPF 0x036
  33. /* 000110111 - lower 8-by-16-bit partitioned product */
  34. #define FMUL8ULx16_OPF 0x037
  35. /* 000111000 - upper 8-by-16-bit partitioned product */
  36. #define FMULD8SUx16_OPF 0x038
  37. /* 000111001 - lower unsigned 8-by-16-bit partitioned product */
  38. #define FMULD8ULx16_OPF 0x039
  39. /* 000101000 - four 16-bit compare; set rd if src1 > src2 */
  40. #define FCMPGT16_OPF 0x028
  41. /* 000101100 - two 32-bit compare; set rd if src1 > src2 */
  42. #define FCMPGT32_OPF 0x02c
  43. /* 000100000 - four 16-bit compare; set rd if src1 <= src2 */
  44. #define FCMPLE16_OPF 0x020
  45. /* 000100100 - two 32-bit compare; set rd if src1 <= src2 */
  46. #define FCMPLE32_OPF 0x024
  47. /* 000100010 - four 16-bit compare; set rd if src1 != src2 */
  48. #define FCMPNE16_OPF 0x022
  49. /* 000100110 - two 32-bit compare; set rd if src1 != src2 */
  50. #define FCMPNE32_OPF 0x026
  51. /* 000101010 - four 16-bit compare; set rd if src1 == src2 */
  52. #define FCMPEQ16_OPF 0x02a
  53. /* 000101110 - two 32-bit compare; set rd if src1 == src2 */
  54. #define FCMPEQ32_OPF 0x02e
  55. /* 000000000 - Eight 8-bit edge boundary processing */
  56. #define EDGE8_OPF 0x000
  57. /* 000000001 - Eight 8-bit edge boundary processing, no CC */
  58. #define EDGE8N_OPF 0x001
  59. /* 000000010 - Eight 8-bit edge boundary processing, little-endian */
  60. #define EDGE8L_OPF 0x002
  61. /* 000000011 - Eight 8-bit edge boundary processing, little-endian, no CC */
  62. #define EDGE8LN_OPF 0x003
  63. /* 000000100 - Four 16-bit edge boundary processing */
  64. #define EDGE16_OPF 0x004
  65. /* 000000101 - Four 16-bit edge boundary processing, no CC */
  66. #define EDGE16N_OPF 0x005
  67. /* 000000110 - Four 16-bit edge boundary processing, little-endian */
  68. #define EDGE16L_OPF 0x006
  69. /* 000000111 - Four 16-bit edge boundary processing, little-endian, no CC */
  70. #define EDGE16LN_OPF 0x007
  71. /* 000001000 - Two 32-bit edge boundary processing */
  72. #define EDGE32_OPF 0x008
  73. /* 000001001 - Two 32-bit edge boundary processing, no CC */
  74. #define EDGE32N_OPF 0x009
  75. /* 000001010 - Two 32-bit edge boundary processing, little-endian */
  76. #define EDGE32L_OPF 0x00a
  77. /* 000001011 - Two 32-bit edge boundary processing, little-endian, no CC */
  78. #define EDGE32LN_OPF 0x00b
  79. /* 000111110 - distance between 8 8-bit components */
  80. #define PDIST_OPF 0x03e
  81. /* 000010000 - convert 8-bit 3-D address to blocked byte address */
  82. #define ARRAY8_OPF 0x010
  83. /* 000010010 - convert 16-bit 3-D address to blocked byte address */
  84. #define ARRAY16_OPF 0x012
  85. /* 000010100 - convert 32-bit 3-D address to blocked byte address */
  86. #define ARRAY32_OPF 0x014
  87. /* 000011001 - Set the GSR.MASK field in preparation for a BSHUFFLE */
  88. #define BMASK_OPF 0x019
  89. /* 001001100 - Permute bytes as specified by GSR.MASK */
  90. #define BSHUFFLE_OPF 0x04c
  91. #define VIS_OPF_SHIFT 5
  92. #define VIS_OPF_MASK (0x1ff << VIS_OPF_SHIFT)
  93. #define RS1(INSN) (((INSN) >> 14) & 0x1f)
  94. #define RS2(INSN) (((INSN) >> 0) & 0x1f)
  95. #define RD(INSN) (((INSN) >> 25) & 0x1f)
  96. static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
  97. unsigned int rd, int from_kernel)
  98. {
  99. if (rs2 >= 16 || rs1 >= 16 || rd >= 16) {
  100. if (from_kernel != 0)
  101. __asm__ __volatile__("flushw");
  102. else
  103. flushw_user();
  104. }
  105. }
  106. static unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
  107. {
  108. unsigned long value, fp;
  109. if (reg < 16)
  110. return (!reg ? 0 : regs->u_regs[reg]);
  111. fp = regs->u_regs[UREG_FP];
  112. if (regs->tstate & TSTATE_PRIV) {
  113. struct reg_window *win;
  114. win = (struct reg_window *)(fp + STACK_BIAS);
  115. value = win->locals[reg - 16];
  116. } else if (!test_thread_64bit_stack(fp)) {
  117. struct reg_window32 __user *win32;
  118. win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
  119. get_user(value, &win32->locals[reg - 16]);
  120. } else {
  121. struct reg_window __user *win;
  122. win = (struct reg_window __user *)(fp + STACK_BIAS);
  123. get_user(value, &win->locals[reg - 16]);
  124. }
  125. return value;
  126. }
  127. static inline unsigned long __user *__fetch_reg_addr_user(unsigned int reg,
  128. struct pt_regs *regs)
  129. {
  130. unsigned long fp = regs->u_regs[UREG_FP];
  131. BUG_ON(reg < 16);
  132. BUG_ON(regs->tstate & TSTATE_PRIV);
  133. if (!test_thread_64bit_stack(fp)) {
  134. struct reg_window32 __user *win32;
  135. win32 = (struct reg_window32 __user *)((unsigned long)((u32)fp));
  136. return (unsigned long __user *)&win32->locals[reg - 16];
  137. } else {
  138. struct reg_window __user *win;
  139. win = (struct reg_window __user *)(fp + STACK_BIAS);
  140. return &win->locals[reg - 16];
  141. }
  142. }
  143. static inline unsigned long *__fetch_reg_addr_kern(unsigned int reg,
  144. struct pt_regs *regs)
  145. {
  146. BUG_ON(reg >= 16);
  147. BUG_ON(regs->tstate & TSTATE_PRIV);
  148. return &regs->u_regs[reg];
  149. }
  150. static void store_reg(struct pt_regs *regs, unsigned long val, unsigned long rd)
  151. {
  152. if (rd < 16) {
  153. unsigned long *rd_kern = __fetch_reg_addr_kern(rd, regs);
  154. *rd_kern = val;
  155. } else {
  156. unsigned long __user *rd_user = __fetch_reg_addr_user(rd, regs);
  157. if (!test_thread_64bit_stack(regs->u_regs[UREG_FP]))
  158. __put_user((u32)val, (u32 __user *)rd_user);
  159. else
  160. __put_user(val, rd_user);
  161. }
  162. }
  163. static inline unsigned long fpd_regval(struct fpustate *f,
  164. unsigned int insn_regnum)
  165. {
  166. insn_regnum = (((insn_regnum & 1) << 5) |
  167. (insn_regnum & 0x1e));
  168. return *(unsigned long *) &f->regs[insn_regnum];
  169. }
  170. static inline unsigned long *fpd_regaddr(struct fpustate *f,
  171. unsigned int insn_regnum)
  172. {
  173. insn_regnum = (((insn_regnum & 1) << 5) |
  174. (insn_regnum & 0x1e));
  175. return (unsigned long *) &f->regs[insn_regnum];
  176. }
  177. static inline unsigned int fps_regval(struct fpustate *f,
  178. unsigned int insn_regnum)
  179. {
  180. return f->regs[insn_regnum];
  181. }
  182. static inline unsigned int *fps_regaddr(struct fpustate *f,
  183. unsigned int insn_regnum)
  184. {
  185. return &f->regs[insn_regnum];
  186. }
  187. struct edge_tab {
  188. u16 left, right;
  189. };
  190. static struct edge_tab edge8_tab[8] = {
  191. { 0xff, 0x80 },
  192. { 0x7f, 0xc0 },
  193. { 0x3f, 0xe0 },
  194. { 0x1f, 0xf0 },
  195. { 0x0f, 0xf8 },
  196. { 0x07, 0xfc },
  197. { 0x03, 0xfe },
  198. { 0x01, 0xff },
  199. };
  200. static struct edge_tab edge8_tab_l[8] = {
  201. { 0xff, 0x01 },
  202. { 0xfe, 0x03 },
  203. { 0xfc, 0x07 },
  204. { 0xf8, 0x0f },
  205. { 0xf0, 0x1f },
  206. { 0xe0, 0x3f },
  207. { 0xc0, 0x7f },
  208. { 0x80, 0xff },
  209. };
  210. static struct edge_tab edge16_tab[4] = {
  211. { 0xf, 0x8 },
  212. { 0x7, 0xc },
  213. { 0x3, 0xe },
  214. { 0x1, 0xf },
  215. };
  216. static struct edge_tab edge16_tab_l[4] = {
  217. { 0xf, 0x1 },
  218. { 0xe, 0x3 },
  219. { 0xc, 0x7 },
  220. { 0x8, 0xf },
  221. };
  222. static struct edge_tab edge32_tab[2] = {
  223. { 0x3, 0x2 },
  224. { 0x1, 0x3 },
  225. };
  226. static struct edge_tab edge32_tab_l[2] = {
  227. { 0x3, 0x1 },
  228. { 0x2, 0x3 },
  229. };
  230. static void edge(struct pt_regs *regs, unsigned int insn, unsigned int opf)
  231. {
  232. unsigned long orig_rs1, rs1, orig_rs2, rs2, rd_val;
  233. u16 left, right;
  234. maybe_flush_windows(RS1(insn), RS2(insn), RD(insn), 0);
  235. orig_rs1 = rs1 = fetch_reg(RS1(insn), regs);
  236. orig_rs2 = rs2 = fetch_reg(RS2(insn), regs);
  237. if (test_thread_flag(TIF_32BIT)) {
  238. rs1 = rs1 & 0xffffffff;
  239. rs2 = rs2 & 0xffffffff;
  240. }
  241. switch (opf) {
  242. default:
  243. case EDGE8_OPF:
  244. case EDGE8N_OPF:
  245. left = edge8_tab[rs1 & 0x7].left;
  246. right = edge8_tab[rs2 & 0x7].right;
  247. break;
  248. case EDGE8L_OPF:
  249. case EDGE8LN_OPF:
  250. left = edge8_tab_l[rs1 & 0x7].left;
  251. right = edge8_tab_l[rs2 & 0x7].right;
  252. break;
  253. case EDGE16_OPF:
  254. case EDGE16N_OPF:
  255. left = edge16_tab[(rs1 >> 1) & 0x3].left;
  256. right = edge16_tab[(rs2 >> 1) & 0x3].right;
  257. break;
  258. case EDGE16L_OPF:
  259. case EDGE16LN_OPF:
  260. left = edge16_tab_l[(rs1 >> 1) & 0x3].left;
  261. right = edge16_tab_l[(rs2 >> 1) & 0x3].right;
  262. break;
  263. case EDGE32_OPF:
  264. case EDGE32N_OPF:
  265. left = edge32_tab[(rs1 >> 2) & 0x1].left;
  266. right = edge32_tab[(rs2 >> 2) & 0x1].right;
  267. break;
  268. case EDGE32L_OPF:
  269. case EDGE32LN_OPF:
  270. left = edge32_tab_l[(rs1 >> 2) & 0x1].left;
  271. right = edge32_tab_l[(rs2 >> 2) & 0x1].right;
  272. break;
  273. }
  274. if ((rs1 & ~0x7UL) == (rs2 & ~0x7UL))
  275. rd_val = right & left;
  276. else
  277. rd_val = left;
  278. store_reg(regs, rd_val, RD(insn));
  279. switch (opf) {
  280. case EDGE8_OPF:
  281. case EDGE8L_OPF:
  282. case EDGE16_OPF:
  283. case EDGE16L_OPF:
  284. case EDGE32_OPF:
  285. case EDGE32L_OPF: {
  286. unsigned long ccr, tstate;
  287. __asm__ __volatile__("subcc %1, %2, %%g0\n\t"
  288. "rd %%ccr, %0"
  289. : "=r" (ccr)
  290. : "r" (orig_rs1), "r" (orig_rs2)
  291. : "cc");
  292. tstate = regs->tstate & ~(TSTATE_XCC | TSTATE_ICC);
  293. regs->tstate = tstate | (ccr << 32UL);
  294. }
  295. }
  296. }
  297. static void array(struct pt_regs *regs, unsigned int insn, unsigned int opf)
  298. {
  299. unsigned long rs1, rs2, rd_val;
  300. unsigned int bits, bits_mask;
  301. maybe_flush_windows(RS1(insn), RS2(insn), RD(insn), 0);
  302. rs1 = fetch_reg(RS1(insn), regs);
  303. rs2 = fetch_reg(RS2(insn), regs);
  304. bits = (rs2 > 5 ? 5 : rs2);
  305. bits_mask = (1UL << bits) - 1UL;
  306. rd_val = ((((rs1 >> 11) & 0x3) << 0) |
  307. (((rs1 >> 33) & 0x3) << 2) |
  308. (((rs1 >> 55) & 0x1) << 4) |
  309. (((rs1 >> 13) & 0xf) << 5) |
  310. (((rs1 >> 35) & 0xf) << 9) |
  311. (((rs1 >> 56) & 0xf) << 13) |
  312. (((rs1 >> 17) & bits_mask) << 17) |
  313. (((rs1 >> 39) & bits_mask) << (17 + bits)) |
  314. (((rs1 >> 60) & 0xf) << (17 + (2*bits))));
  315. switch (opf) {
  316. case ARRAY16_OPF:
  317. rd_val <<= 1;
  318. break;
  319. case ARRAY32_OPF:
  320. rd_val <<= 2;
  321. }
  322. store_reg(regs, rd_val, RD(insn));
  323. }
  324. static void bmask(struct pt_regs *regs, unsigned int insn)
  325. {
  326. unsigned long rs1, rs2, rd_val, gsr;
  327. maybe_flush_windows(RS1(insn), RS2(insn), RD(insn), 0);
  328. rs1 = fetch_reg(RS1(insn), regs);
  329. rs2 = fetch_reg(RS2(insn), regs);
  330. rd_val = rs1 + rs2;
  331. store_reg(regs, rd_val, RD(insn));
  332. gsr = current_thread_info()->gsr[0] & 0xffffffff;
  333. gsr |= rd_val << 32UL;
  334. current_thread_info()->gsr[0] = gsr;
  335. }
  336. static void bshuffle(struct pt_regs *regs, unsigned int insn)
  337. {
  338. struct fpustate *f = FPUSTATE;
  339. unsigned long rs1, rs2, rd_val;
  340. unsigned long bmask, i;
  341. bmask = current_thread_info()->gsr[0] >> 32UL;
  342. rs1 = fpd_regval(f, RS1(insn));
  343. rs2 = fpd_regval(f, RS2(insn));
  344. rd_val = 0UL;
  345. for (i = 0; i < 8; i++) {
  346. unsigned long which = (bmask >> (i * 4)) & 0xf;
  347. unsigned long byte;
  348. if (which < 8)
  349. byte = (rs1 >> (which * 8)) & 0xff;
  350. else
  351. byte = (rs2 >> ((which-8)*8)) & 0xff;
  352. rd_val |= (byte << (i * 8));
  353. }
  354. *fpd_regaddr(f, RD(insn)) = rd_val;
  355. }
  356. static void pdist(struct pt_regs *regs, unsigned int insn)
  357. {
  358. struct fpustate *f = FPUSTATE;
  359. unsigned long rs1, rs2, *rd, rd_val;
  360. unsigned long i;
  361. rs1 = fpd_regval(f, RS1(insn));
  362. rs2 = fpd_regval(f, RS2(insn));
  363. rd = fpd_regaddr(f, RD(insn));
  364. rd_val = *rd;
  365. for (i = 0; i < 8; i++) {
  366. s16 s1, s2;
  367. s1 = (rs1 >> (56 - (i * 8))) & 0xff;
  368. s2 = (rs2 >> (56 - (i * 8))) & 0xff;
  369. /* Absolute value of difference. */
  370. s1 -= s2;
  371. if (s1 < 0)
  372. s1 = ~s1 + 1;
  373. rd_val += s1;
  374. }
  375. *rd = rd_val;
  376. }
  377. static void pformat(struct pt_regs *regs, unsigned int insn, unsigned int opf)
  378. {
  379. struct fpustate *f = FPUSTATE;
  380. unsigned long rs1, rs2, gsr, scale, rd_val;
  381. gsr = current_thread_info()->gsr[0];
  382. scale = (gsr >> 3) & (opf == FPACK16_OPF ? 0xf : 0x1f);
  383. switch (opf) {
  384. case FPACK16_OPF: {
  385. unsigned long byte;
  386. rs2 = fpd_regval(f, RS2(insn));
  387. rd_val = 0;
  388. for (byte = 0; byte < 4; byte++) {
  389. unsigned int val;
  390. s16 src = (rs2 >> (byte * 16UL)) & 0xffffUL;
  391. int scaled = src << scale;
  392. int from_fixed = scaled >> 7;
  393. val = ((from_fixed < 0) ?
  394. 0 :
  395. (from_fixed > 255) ?
  396. 255 : from_fixed);
  397. rd_val |= (val << (8 * byte));
  398. }
  399. *fps_regaddr(f, RD(insn)) = rd_val;
  400. break;
  401. }
  402. case FPACK32_OPF: {
  403. unsigned long word;
  404. rs1 = fpd_regval(f, RS1(insn));
  405. rs2 = fpd_regval(f, RS2(insn));
  406. rd_val = (rs1 << 8) & ~(0x000000ff000000ffUL);
  407. for (word = 0; word < 2; word++) {
  408. unsigned long val;
  409. s32 src = (rs2 >> (word * 32UL));
  410. s64 scaled = src << scale;
  411. s64 from_fixed = scaled >> 23;
  412. val = ((from_fixed < 0) ?
  413. 0 :
  414. (from_fixed > 255) ?
  415. 255 : from_fixed);
  416. rd_val |= (val << (32 * word));
  417. }
  418. *fpd_regaddr(f, RD(insn)) = rd_val;
  419. break;
  420. }
  421. case FPACKFIX_OPF: {
  422. unsigned long word;
  423. rs2 = fpd_regval(f, RS2(insn));
  424. rd_val = 0;
  425. for (word = 0; word < 2; word++) {
  426. long val;
  427. s32 src = (rs2 >> (word * 32UL));
  428. s64 scaled = src << scale;
  429. s64 from_fixed = scaled >> 16;
  430. val = ((from_fixed < -32768) ?
  431. -32768 :
  432. (from_fixed > 32767) ?
  433. 32767 : from_fixed);
  434. rd_val |= ((val & 0xffff) << (word * 16));
  435. }
  436. *fps_regaddr(f, RD(insn)) = rd_val;
  437. break;
  438. }
  439. case FEXPAND_OPF: {
  440. unsigned long byte;
  441. rs2 = fps_regval(f, RS2(insn));
  442. rd_val = 0;
  443. for (byte = 0; byte < 4; byte++) {
  444. unsigned long val;
  445. u8 src = (rs2 >> (byte * 8)) & 0xff;
  446. val = src << 4;
  447. rd_val |= (val << (byte * 16));
  448. }
  449. *fpd_regaddr(f, RD(insn)) = rd_val;
  450. break;
  451. }
  452. case FPMERGE_OPF: {
  453. rs1 = fps_regval(f, RS1(insn));
  454. rs2 = fps_regval(f, RS2(insn));
  455. rd_val = (((rs2 & 0x000000ff) << 0) |
  456. ((rs1 & 0x000000ff) << 8) |
  457. ((rs2 & 0x0000ff00) << 8) |
  458. ((rs1 & 0x0000ff00) << 16) |
  459. ((rs2 & 0x00ff0000) << 16) |
  460. ((rs1 & 0x00ff0000) << 24) |
  461. ((rs2 & 0xff000000) << 24) |
  462. ((rs1 & 0xff000000) << 32));
  463. *fpd_regaddr(f, RD(insn)) = rd_val;
  464. break;
  465. }
  466. }
  467. }
  468. static void pmul(struct pt_regs *regs, unsigned int insn, unsigned int opf)
  469. {
  470. struct fpustate *f = FPUSTATE;
  471. unsigned long rs1, rs2, rd_val;
  472. switch (opf) {
  473. case FMUL8x16_OPF: {
  474. unsigned long byte;
  475. rs1 = fps_regval(f, RS1(insn));
  476. rs2 = fpd_regval(f, RS2(insn));
  477. rd_val = 0;
  478. for (byte = 0; byte < 4; byte++) {
  479. u16 src1 = (rs1 >> (byte * 8)) & 0x00ff;
  480. s16 src2 = (rs2 >> (byte * 16)) & 0xffff;
  481. u32 prod = src1 * src2;
  482. u16 scaled = ((prod & 0x00ffff00) >> 8);
  483. /* Round up. */
  484. if (prod & 0x80)
  485. scaled++;
  486. rd_val |= ((scaled & 0xffffUL) << (byte * 16UL));
  487. }
  488. *fpd_regaddr(f, RD(insn)) = rd_val;
  489. break;
  490. }
  491. case FMUL8x16AU_OPF:
  492. case FMUL8x16AL_OPF: {
  493. unsigned long byte;
  494. s16 src2;
  495. rs1 = fps_regval(f, RS1(insn));
  496. rs2 = fps_regval(f, RS2(insn));
  497. rd_val = 0;
  498. src2 = rs2 >> (opf == FMUL8x16AU_OPF ? 16 : 0);
  499. for (byte = 0; byte < 4; byte++) {
  500. u16 src1 = (rs1 >> (byte * 8)) & 0x00ff;
  501. u32 prod = src1 * src2;
  502. u16 scaled = ((prod & 0x00ffff00) >> 8);
  503. /* Round up. */
  504. if (prod & 0x80)
  505. scaled++;
  506. rd_val |= ((scaled & 0xffffUL) << (byte * 16UL));
  507. }
  508. *fpd_regaddr(f, RD(insn)) = rd_val;
  509. break;
  510. }
  511. case FMUL8SUx16_OPF:
  512. case FMUL8ULx16_OPF: {
  513. unsigned long byte, ushift;
  514. rs1 = fpd_regval(f, RS1(insn));
  515. rs2 = fpd_regval(f, RS2(insn));
  516. rd_val = 0;
  517. ushift = (opf == FMUL8SUx16_OPF) ? 8 : 0;
  518. for (byte = 0; byte < 4; byte++) {
  519. u16 src1;
  520. s16 src2;
  521. u32 prod;
  522. u16 scaled;
  523. src1 = ((rs1 >> ((16 * byte) + ushift)) & 0x00ff);
  524. src2 = ((rs2 >> (16 * byte)) & 0xffff);
  525. prod = src1 * src2;
  526. scaled = ((prod & 0x00ffff00) >> 8);
  527. /* Round up. */
  528. if (prod & 0x80)
  529. scaled++;
  530. rd_val |= ((scaled & 0xffffUL) << (byte * 16UL));
  531. }
  532. *fpd_regaddr(f, RD(insn)) = rd_val;
  533. break;
  534. }
  535. case FMULD8SUx16_OPF:
  536. case FMULD8ULx16_OPF: {
  537. unsigned long byte, ushift;
  538. rs1 = fps_regval(f, RS1(insn));
  539. rs2 = fps_regval(f, RS2(insn));
  540. rd_val = 0;
  541. ushift = (opf == FMULD8SUx16_OPF) ? 8 : 0;
  542. for (byte = 0; byte < 2; byte++) {
  543. u16 src1;
  544. s16 src2;
  545. u32 prod;
  546. u16 scaled;
  547. src1 = ((rs1 >> ((16 * byte) + ushift)) & 0x00ff);
  548. src2 = ((rs2 >> (16 * byte)) & 0xffff);
  549. prod = src1 * src2;
  550. scaled = ((prod & 0x00ffff00) >> 8);
  551. /* Round up. */
  552. if (prod & 0x80)
  553. scaled++;
  554. rd_val |= ((scaled & 0xffffUL) <<
  555. ((byte * 32UL) + 7UL));
  556. }
  557. *fpd_regaddr(f, RD(insn)) = rd_val;
  558. break;
  559. }
  560. }
  561. }
  562. static void pcmp(struct pt_regs *regs, unsigned int insn, unsigned int opf)
  563. {
  564. struct fpustate *f = FPUSTATE;
  565. unsigned long rs1, rs2, rd_val, i;
  566. rs1 = fpd_regval(f, RS1(insn));
  567. rs2 = fpd_regval(f, RS2(insn));
  568. rd_val = 0;
  569. switch (opf) {
  570. case FCMPGT16_OPF:
  571. for (i = 0; i < 4; i++) {
  572. s16 a = (rs1 >> (i * 16)) & 0xffff;
  573. s16 b = (rs2 >> (i * 16)) & 0xffff;
  574. if (a > b)
  575. rd_val |= 8 >> i;
  576. }
  577. break;
  578. case FCMPGT32_OPF:
  579. for (i = 0; i < 2; i++) {
  580. s32 a = (rs1 >> (i * 32)) & 0xffffffff;
  581. s32 b = (rs2 >> (i * 32)) & 0xffffffff;
  582. if (a > b)
  583. rd_val |= 2 >> i;
  584. }
  585. break;
  586. case FCMPLE16_OPF:
  587. for (i = 0; i < 4; i++) {
  588. s16 a = (rs1 >> (i * 16)) & 0xffff;
  589. s16 b = (rs2 >> (i * 16)) & 0xffff;
  590. if (a <= b)
  591. rd_val |= 8 >> i;
  592. }
  593. break;
  594. case FCMPLE32_OPF:
  595. for (i = 0; i < 2; i++) {
  596. s32 a = (rs1 >> (i * 32)) & 0xffffffff;
  597. s32 b = (rs2 >> (i * 32)) & 0xffffffff;
  598. if (a <= b)
  599. rd_val |= 2 >> i;
  600. }
  601. break;
  602. case FCMPNE16_OPF:
  603. for (i = 0; i < 4; i++) {
  604. s16 a = (rs1 >> (i * 16)) & 0xffff;
  605. s16 b = (rs2 >> (i * 16)) & 0xffff;
  606. if (a != b)
  607. rd_val |= 8 >> i;
  608. }
  609. break;
  610. case FCMPNE32_OPF:
  611. for (i = 0; i < 2; i++) {
  612. s32 a = (rs1 >> (i * 32)) & 0xffffffff;
  613. s32 b = (rs2 >> (i * 32)) & 0xffffffff;
  614. if (a != b)
  615. rd_val |= 2 >> i;
  616. }
  617. break;
  618. case FCMPEQ16_OPF:
  619. for (i = 0; i < 4; i++) {
  620. s16 a = (rs1 >> (i * 16)) & 0xffff;
  621. s16 b = (rs2 >> (i * 16)) & 0xffff;
  622. if (a == b)
  623. rd_val |= 8 >> i;
  624. }
  625. break;
  626. case FCMPEQ32_OPF:
  627. for (i = 0; i < 2; i++) {
  628. s32 a = (rs1 >> (i * 32)) & 0xffffffff;
  629. s32 b = (rs2 >> (i * 32)) & 0xffffffff;
  630. if (a == b)
  631. rd_val |= 2 >> i;
  632. }
  633. break;
  634. }
  635. maybe_flush_windows(0, 0, RD(insn), 0);
  636. store_reg(regs, rd_val, RD(insn));
  637. }
  638. /* Emulate the VIS instructions which are not implemented in
  639. * hardware on Niagara.
  640. */
  641. int vis_emul(struct pt_regs *regs, unsigned int insn)
  642. {
  643. unsigned long pc = regs->tpc;
  644. unsigned int opf;
  645. BUG_ON(regs->tstate & TSTATE_PRIV);
  646. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  647. if (test_thread_flag(TIF_32BIT))
  648. pc = (u32)pc;
  649. if (get_user(insn, (u32 __user *) pc))
  650. return -EFAULT;
  651. save_and_clear_fpu();
  652. opf = (insn & VIS_OPF_MASK) >> VIS_OPF_SHIFT;
  653. switch (opf) {
  654. default:
  655. return -EINVAL;
  656. /* Pixel Formatting Instructions. */
  657. case FPACK16_OPF:
  658. case FPACK32_OPF:
  659. case FPACKFIX_OPF:
  660. case FEXPAND_OPF:
  661. case FPMERGE_OPF:
  662. pformat(regs, insn, opf);
  663. break;
  664. /* Partitioned Multiply Instructions */
  665. case FMUL8x16_OPF:
  666. case FMUL8x16AU_OPF:
  667. case FMUL8x16AL_OPF:
  668. case FMUL8SUx16_OPF:
  669. case FMUL8ULx16_OPF:
  670. case FMULD8SUx16_OPF:
  671. case FMULD8ULx16_OPF:
  672. pmul(regs, insn, opf);
  673. break;
  674. /* Pixel Compare Instructions */
  675. case FCMPGT16_OPF:
  676. case FCMPGT32_OPF:
  677. case FCMPLE16_OPF:
  678. case FCMPLE32_OPF:
  679. case FCMPNE16_OPF:
  680. case FCMPNE32_OPF:
  681. case FCMPEQ16_OPF:
  682. case FCMPEQ32_OPF:
  683. pcmp(regs, insn, opf);
  684. break;
  685. /* Edge Handling Instructions */
  686. case EDGE8_OPF:
  687. case EDGE8N_OPF:
  688. case EDGE8L_OPF:
  689. case EDGE8LN_OPF:
  690. case EDGE16_OPF:
  691. case EDGE16N_OPF:
  692. case EDGE16L_OPF:
  693. case EDGE16LN_OPF:
  694. case EDGE32_OPF:
  695. case EDGE32N_OPF:
  696. case EDGE32L_OPF:
  697. case EDGE32LN_OPF:
  698. edge(regs, insn, opf);
  699. break;
  700. /* Pixel Component Distance */
  701. case PDIST_OPF:
  702. pdist(regs, insn);
  703. break;
  704. /* Three-Dimensional Array Addressing Instructions */
  705. case ARRAY8_OPF:
  706. case ARRAY16_OPF:
  707. case ARRAY32_OPF:
  708. array(regs, insn, opf);
  709. break;
  710. /* Byte Mask and Shuffle Instructions */
  711. case BMASK_OPF:
  712. bmask(regs, insn);
  713. break;
  714. case BSHUFFLE_OPF:
  715. bshuffle(regs, insn);
  716. break;
  717. }
  718. regs->tpc = regs->tnpc;
  719. regs->tnpc += 4;
  720. return 0;
  721. }