msa.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2013 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@mips.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #ifndef _ASM_MSA_H
  11. #define _ASM_MSA_H
  12. #include <asm/mipsregs.h>
  13. #ifndef __ASSEMBLY__
  14. #include <asm/inst.h>
  15. extern void _save_msa(struct task_struct *);
  16. extern void _restore_msa(struct task_struct *);
  17. extern void _init_msa_upper(void);
  18. extern void read_msa_wr_b(unsigned idx, union fpureg *to);
  19. extern void read_msa_wr_h(unsigned idx, union fpureg *to);
  20. extern void read_msa_wr_w(unsigned idx, union fpureg *to);
  21. extern void read_msa_wr_d(unsigned idx, union fpureg *to);
  22. /**
  23. * read_msa_wr() - Read a single MSA vector register
  24. * @idx: The index of the vector register to read
  25. * @to: The FPU register union to store the registers value in
  26. * @fmt: The format of the data in the vector register
  27. *
  28. * Read the value of MSA vector register idx into the FPU register
  29. * union to, using the format fmt.
  30. */
  31. static inline void read_msa_wr(unsigned idx, union fpureg *to,
  32. enum msa_2b_fmt fmt)
  33. {
  34. switch (fmt) {
  35. case msa_fmt_b:
  36. read_msa_wr_b(idx, to);
  37. break;
  38. case msa_fmt_h:
  39. read_msa_wr_h(idx, to);
  40. break;
  41. case msa_fmt_w:
  42. read_msa_wr_w(idx, to);
  43. break;
  44. case msa_fmt_d:
  45. read_msa_wr_d(idx, to);
  46. break;
  47. default:
  48. BUG();
  49. }
  50. }
  51. extern void write_msa_wr_b(unsigned idx, union fpureg *from);
  52. extern void write_msa_wr_h(unsigned idx, union fpureg *from);
  53. extern void write_msa_wr_w(unsigned idx, union fpureg *from);
  54. extern void write_msa_wr_d(unsigned idx, union fpureg *from);
  55. /**
  56. * write_msa_wr() - Write a single MSA vector register
  57. * @idx: The index of the vector register to write
  58. * @from: The FPU register union to take the registers value from
  59. * @fmt: The format of the data in the vector register
  60. *
  61. * Write the value from the FPU register union from into MSA vector
  62. * register idx, using the format fmt.
  63. */
  64. static inline void write_msa_wr(unsigned idx, union fpureg *from,
  65. enum msa_2b_fmt fmt)
  66. {
  67. switch (fmt) {
  68. case msa_fmt_b:
  69. write_msa_wr_b(idx, from);
  70. break;
  71. case msa_fmt_h:
  72. write_msa_wr_h(idx, from);
  73. break;
  74. case msa_fmt_w:
  75. write_msa_wr_w(idx, from);
  76. break;
  77. case msa_fmt_d:
  78. write_msa_wr_d(idx, from);
  79. break;
  80. default:
  81. BUG();
  82. }
  83. }
  84. static inline void enable_msa(void)
  85. {
  86. if (cpu_has_msa) {
  87. set_c0_config5(MIPS_CONF5_MSAEN);
  88. enable_fpu_hazard();
  89. }
  90. }
  91. static inline void disable_msa(void)
  92. {
  93. if (cpu_has_msa) {
  94. clear_c0_config5(MIPS_CONF5_MSAEN);
  95. disable_fpu_hazard();
  96. }
  97. }
  98. static inline int is_msa_enabled(void)
  99. {
  100. if (!cpu_has_msa)
  101. return 0;
  102. return read_c0_config5() & MIPS_CONF5_MSAEN;
  103. }
  104. static inline int thread_msa_context_live(void)
  105. {
  106. /*
  107. * Check cpu_has_msa only if it's a constant. This will allow the
  108. * compiler to optimise out code for CPUs without MSA without adding
  109. * an extra redundant check for CPUs with MSA.
  110. */
  111. if (__builtin_constant_p(cpu_has_msa) && !cpu_has_msa)
  112. return 0;
  113. return test_thread_flag(TIF_MSA_CTX_LIVE);
  114. }
  115. static inline void save_msa(struct task_struct *t)
  116. {
  117. if (cpu_has_msa)
  118. _save_msa(t);
  119. }
  120. static inline void restore_msa(struct task_struct *t)
  121. {
  122. if (cpu_has_msa)
  123. _restore_msa(t);
  124. }
  125. static inline void init_msa_upper(void)
  126. {
  127. /*
  128. * Check cpu_has_msa only if it's a constant. This will allow the
  129. * compiler to optimise out code for CPUs without MSA without adding
  130. * an extra redundant check for CPUs with MSA.
  131. */
  132. if (__builtin_constant_p(cpu_has_msa) && !cpu_has_msa)
  133. return;
  134. _init_msa_upper();
  135. }
  136. #ifndef TOOLCHAIN_SUPPORTS_MSA
  137. /*
  138. * Define assembler macros using .word for the c[ft]cmsa instructions in order
  139. * to allow compilation with toolchains that do not support MSA. Once all
  140. * toolchains in use support MSA these can be removed.
  141. */
  142. _ASM_MACRO_2R(cfcmsa, rd, cs,
  143. _ASM_INSN_IF_MIPS(0x787e0019 | __cs << 11 | __rd << 6)
  144. _ASM_INSN32_IF_MM(0x587e0016 | __cs << 11 | __rd << 6));
  145. _ASM_MACRO_2R(ctcmsa, cd, rs,
  146. _ASM_INSN_IF_MIPS(0x783e0019 | __rs << 11 | __cd << 6)
  147. _ASM_INSN32_IF_MM(0x583e0016 | __rs << 11 | __cd << 6));
  148. #define _ASM_SET_MSA ""
  149. #else /* TOOLCHAIN_SUPPORTS_MSA */
  150. #define _ASM_SET_MSA ".set\tfp=64\n\t" \
  151. ".set\tmsa\n\t"
  152. #endif
  153. #define __BUILD_MSA_CTL_REG(name, cs) \
  154. static inline unsigned int read_msa_##name(void) \
  155. { \
  156. unsigned int reg; \
  157. __asm__ __volatile__( \
  158. " .set push\n" \
  159. _ASM_SET_MSA \
  160. " cfcmsa %0, $" #cs "\n" \
  161. " .set pop\n" \
  162. : "=r"(reg)); \
  163. return reg; \
  164. } \
  165. \
  166. static inline void write_msa_##name(unsigned int val) \
  167. { \
  168. __asm__ __volatile__( \
  169. " .set push\n" \
  170. _ASM_SET_MSA \
  171. " ctcmsa $" #cs ", %0\n" \
  172. " .set pop\n" \
  173. : : "r"(val)); \
  174. }
  175. __BUILD_MSA_CTL_REG(ir, 0)
  176. __BUILD_MSA_CTL_REG(csr, 1)
  177. __BUILD_MSA_CTL_REG(access, 2)
  178. __BUILD_MSA_CTL_REG(save, 3)
  179. __BUILD_MSA_CTL_REG(modify, 4)
  180. __BUILD_MSA_CTL_REG(request, 5)
  181. __BUILD_MSA_CTL_REG(map, 6)
  182. __BUILD_MSA_CTL_REG(unmap, 7)
  183. #endif /* !__ASSEMBLY__ */
  184. #define MSA_IR 0
  185. #define MSA_CSR 1
  186. #define MSA_ACCESS 2
  187. #define MSA_SAVE 3
  188. #define MSA_MODIFY 4
  189. #define MSA_REQUEST 5
  190. #define MSA_MAP 6
  191. #define MSA_UNMAP 7
  192. /* MSA Implementation Register (MSAIR) */
  193. #define MSA_IR_REVB 0
  194. #define MSA_IR_REVF (_ULCAST_(0xff) << MSA_IR_REVB)
  195. #define MSA_IR_PROCB 8
  196. #define MSA_IR_PROCF (_ULCAST_(0xff) << MSA_IR_PROCB)
  197. #define MSA_IR_WRPB 16
  198. #define MSA_IR_WRPF (_ULCAST_(0x1) << MSA_IR_WRPB)
  199. /* MSA Control & Status Register (MSACSR) */
  200. #define MSA_CSR_RMB 0
  201. #define MSA_CSR_RMF (_ULCAST_(0x3) << MSA_CSR_RMB)
  202. #define MSA_CSR_RM_NEAREST 0
  203. #define MSA_CSR_RM_TO_ZERO 1
  204. #define MSA_CSR_RM_TO_POS 2
  205. #define MSA_CSR_RM_TO_NEG 3
  206. #define MSA_CSR_FLAGSB 2
  207. #define MSA_CSR_FLAGSF (_ULCAST_(0x1f) << MSA_CSR_FLAGSB)
  208. #define MSA_CSR_FLAGS_IB 2
  209. #define MSA_CSR_FLAGS_IF (_ULCAST_(0x1) << MSA_CSR_FLAGS_IB)
  210. #define MSA_CSR_FLAGS_UB 3
  211. #define MSA_CSR_FLAGS_UF (_ULCAST_(0x1) << MSA_CSR_FLAGS_UB)
  212. #define MSA_CSR_FLAGS_OB 4
  213. #define MSA_CSR_FLAGS_OF (_ULCAST_(0x1) << MSA_CSR_FLAGS_OB)
  214. #define MSA_CSR_FLAGS_ZB 5
  215. #define MSA_CSR_FLAGS_ZF (_ULCAST_(0x1) << MSA_CSR_FLAGS_ZB)
  216. #define MSA_CSR_FLAGS_VB 6
  217. #define MSA_CSR_FLAGS_VF (_ULCAST_(0x1) << MSA_CSR_FLAGS_VB)
  218. #define MSA_CSR_ENABLESB 7
  219. #define MSA_CSR_ENABLESF (_ULCAST_(0x1f) << MSA_CSR_ENABLESB)
  220. #define MSA_CSR_ENABLES_IB 7
  221. #define MSA_CSR_ENABLES_IF (_ULCAST_(0x1) << MSA_CSR_ENABLES_IB)
  222. #define MSA_CSR_ENABLES_UB 8
  223. #define MSA_CSR_ENABLES_UF (_ULCAST_(0x1) << MSA_CSR_ENABLES_UB)
  224. #define MSA_CSR_ENABLES_OB 9
  225. #define MSA_CSR_ENABLES_OF (_ULCAST_(0x1) << MSA_CSR_ENABLES_OB)
  226. #define MSA_CSR_ENABLES_ZB 10
  227. #define MSA_CSR_ENABLES_ZF (_ULCAST_(0x1) << MSA_CSR_ENABLES_ZB)
  228. #define MSA_CSR_ENABLES_VB 11
  229. #define MSA_CSR_ENABLES_VF (_ULCAST_(0x1) << MSA_CSR_ENABLES_VB)
  230. #define MSA_CSR_CAUSEB 12
  231. #define MSA_CSR_CAUSEF (_ULCAST_(0x3f) << MSA_CSR_CAUSEB)
  232. #define MSA_CSR_CAUSE_IB 12
  233. #define MSA_CSR_CAUSE_IF (_ULCAST_(0x1) << MSA_CSR_CAUSE_IB)
  234. #define MSA_CSR_CAUSE_UB 13
  235. #define MSA_CSR_CAUSE_UF (_ULCAST_(0x1) << MSA_CSR_CAUSE_UB)
  236. #define MSA_CSR_CAUSE_OB 14
  237. #define MSA_CSR_CAUSE_OF (_ULCAST_(0x1) << MSA_CSR_CAUSE_OB)
  238. #define MSA_CSR_CAUSE_ZB 15
  239. #define MSA_CSR_CAUSE_ZF (_ULCAST_(0x1) << MSA_CSR_CAUSE_ZB)
  240. #define MSA_CSR_CAUSE_VB 16
  241. #define MSA_CSR_CAUSE_VF (_ULCAST_(0x1) << MSA_CSR_CAUSE_VB)
  242. #define MSA_CSR_CAUSE_EB 17
  243. #define MSA_CSR_CAUSE_EF (_ULCAST_(0x1) << MSA_CSR_CAUSE_EB)
  244. #define MSA_CSR_NXB 18
  245. #define MSA_CSR_NXF (_ULCAST_(0x1) << MSA_CSR_NXB)
  246. #define MSA_CSR_FSB 24
  247. #define MSA_CSR_FSF (_ULCAST_(0x1) << MSA_CSR_FSB)
  248. #endif /* _ASM_MSA_H */