fpu.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Author: Huacai Chen <chenhuacai@loongson.cn>
  4. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  5. */
  6. #ifndef _ASM_FPU_H
  7. #define _ASM_FPU_H
  8. #include <linux/sched.h>
  9. #include <linux/sched/task_stack.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/thread_info.h>
  12. #include <linux/bitops.h>
  13. #include <asm/cpu.h>
  14. #include <asm/cpu-features.h>
  15. #include <asm/current.h>
  16. #include <asm/loongarch.h>
  17. #include <asm/processor.h>
  18. #include <asm/ptrace.h>
  19. struct sigcontext;
  20. #define kernel_fpu_available() cpu_has_fpu
  21. void kernel_fpu_begin(void);
  22. void kernel_fpu_end(void);
  23. asmlinkage void _init_fpu(unsigned int);
  24. asmlinkage void _save_fp(struct loongarch_fpu *);
  25. asmlinkage void _restore_fp(struct loongarch_fpu *);
  26. asmlinkage int _save_fp_context(void __user *fpregs, void __user *fcc, void __user *csr);
  27. asmlinkage int _restore_fp_context(void __user *fpregs, void __user *fcc, void __user *csr);
  28. asmlinkage void _save_lsx(struct loongarch_fpu *fpu);
  29. asmlinkage void _restore_lsx(struct loongarch_fpu *fpu);
  30. asmlinkage void _init_lsx_upper(void);
  31. asmlinkage void _restore_lsx_upper(struct loongarch_fpu *fpu);
  32. asmlinkage int _save_lsx_context(void __user *fpregs, void __user *fcc, void __user *fcsr);
  33. asmlinkage int _restore_lsx_context(void __user *fpregs, void __user *fcc, void __user *fcsr);
  34. asmlinkage void _save_lasx(struct loongarch_fpu *fpu);
  35. asmlinkage void _restore_lasx(struct loongarch_fpu *fpu);
  36. asmlinkage void _init_lasx_upper(void);
  37. asmlinkage void _restore_lasx_upper(struct loongarch_fpu *fpu);
  38. asmlinkage int _save_lasx_context(void __user *fpregs, void __user *fcc, void __user *fcsr);
  39. asmlinkage int _restore_lasx_context(void __user *fpregs, void __user *fcc, void __user *fcsr);
  40. static inline void enable_lsx(void);
  41. static inline void disable_lsx(void);
  42. static inline void save_lsx(struct task_struct *t);
  43. static inline void restore_lsx(struct task_struct *t);
  44. static inline void enable_lasx(void);
  45. static inline void disable_lasx(void);
  46. static inline void save_lasx(struct task_struct *t);
  47. static inline void restore_lasx(struct task_struct *t);
  48. /*
  49. * Mask the FCSR Cause bits according to the Enable bits, observing
  50. * that Unimplemented is always enabled.
  51. */
  52. static inline unsigned long mask_fcsr_x(unsigned long fcsr)
  53. {
  54. return fcsr & ((fcsr & FPU_CSR_ALL_E) <<
  55. (ffs(FPU_CSR_ALL_X) - ffs(FPU_CSR_ALL_E)));
  56. }
  57. static inline int is_fp_enabled(void)
  58. {
  59. return (csr_read32(LOONGARCH_CSR_EUEN) & CSR_EUEN_FPEN) ?
  60. 1 : 0;
  61. }
  62. static inline int is_lsx_enabled(void)
  63. {
  64. if (!cpu_has_lsx)
  65. return 0;
  66. return (csr_read32(LOONGARCH_CSR_EUEN) & CSR_EUEN_LSXEN) ?
  67. 1 : 0;
  68. }
  69. static inline int is_lasx_enabled(void)
  70. {
  71. if (!cpu_has_lasx)
  72. return 0;
  73. return (csr_read32(LOONGARCH_CSR_EUEN) & CSR_EUEN_LASXEN) ?
  74. 1 : 0;
  75. }
  76. static inline int is_simd_enabled(void)
  77. {
  78. return is_lsx_enabled() | is_lasx_enabled();
  79. }
  80. #define enable_fpu() set_csr_euen(CSR_EUEN_FPEN)
  81. #define disable_fpu() clear_csr_euen(CSR_EUEN_FPEN)
  82. #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
  83. static inline int is_fpu_owner(void)
  84. {
  85. return test_thread_flag(TIF_USEDFPU);
  86. }
  87. static inline void __own_fpu(void)
  88. {
  89. enable_fpu();
  90. set_thread_flag(TIF_USEDFPU);
  91. KSTK_EUEN(current) |= CSR_EUEN_FPEN;
  92. }
  93. static inline void own_fpu_inatomic(int restore)
  94. {
  95. if (cpu_has_fpu && !is_fpu_owner()) {
  96. __own_fpu();
  97. if (restore)
  98. _restore_fp(&current->thread.fpu);
  99. }
  100. }
  101. static inline void own_fpu(int restore)
  102. {
  103. preempt_disable();
  104. own_fpu_inatomic(restore);
  105. preempt_enable();
  106. }
  107. static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
  108. {
  109. if (is_fpu_owner()) {
  110. if (!is_simd_enabled()) {
  111. if (save)
  112. _save_fp(&tsk->thread.fpu);
  113. disable_fpu();
  114. } else {
  115. if (save) {
  116. if (!is_lasx_enabled())
  117. save_lsx(tsk);
  118. else
  119. save_lasx(tsk);
  120. }
  121. disable_fpu();
  122. disable_lsx();
  123. disable_lasx();
  124. clear_tsk_thread_flag(tsk, TIF_USEDSIMD);
  125. }
  126. clear_tsk_thread_flag(tsk, TIF_USEDFPU);
  127. }
  128. KSTK_EUEN(tsk) &= ~(CSR_EUEN_FPEN | CSR_EUEN_LSXEN | CSR_EUEN_LASXEN);
  129. }
  130. static inline void lose_fpu(int save)
  131. {
  132. preempt_disable();
  133. lose_fpu_inatomic(save, current);
  134. preempt_enable();
  135. }
  136. static inline void init_fpu(void)
  137. {
  138. unsigned int fcsr = current->thread.fpu.fcsr;
  139. __own_fpu();
  140. _init_fpu(fcsr);
  141. set_used_math();
  142. }
  143. static inline void save_fp(struct task_struct *tsk)
  144. {
  145. if (cpu_has_fpu)
  146. _save_fp(&tsk->thread.fpu);
  147. }
  148. static inline void restore_fp(struct task_struct *tsk)
  149. {
  150. if (cpu_has_fpu)
  151. _restore_fp(&tsk->thread.fpu);
  152. }
  153. static inline void save_fpu_regs(struct task_struct *tsk)
  154. {
  155. unsigned int euen;
  156. if (tsk == current) {
  157. preempt_disable();
  158. euen = csr_read32(LOONGARCH_CSR_EUEN);
  159. #ifdef CONFIG_CPU_HAS_LASX
  160. if (euen & CSR_EUEN_LASXEN)
  161. _save_lasx(&current->thread.fpu);
  162. else
  163. #endif
  164. #ifdef CONFIG_CPU_HAS_LSX
  165. if (euen & CSR_EUEN_LSXEN)
  166. _save_lsx(&current->thread.fpu);
  167. else
  168. #endif
  169. if (euen & CSR_EUEN_FPEN)
  170. _save_fp(&current->thread.fpu);
  171. preempt_enable();
  172. }
  173. }
  174. static inline int is_simd_owner(void)
  175. {
  176. return test_thread_flag(TIF_USEDSIMD);
  177. }
  178. #ifdef CONFIG_CPU_HAS_LSX
  179. static inline void enable_lsx(void)
  180. {
  181. if (cpu_has_lsx)
  182. csr_xchg32(CSR_EUEN_LSXEN, CSR_EUEN_LSXEN, LOONGARCH_CSR_EUEN);
  183. }
  184. static inline void disable_lsx(void)
  185. {
  186. if (cpu_has_lsx)
  187. csr_xchg32(0, CSR_EUEN_LSXEN, LOONGARCH_CSR_EUEN);
  188. }
  189. static inline void save_lsx(struct task_struct *t)
  190. {
  191. if (cpu_has_lsx)
  192. _save_lsx(&t->thread.fpu);
  193. }
  194. static inline void restore_lsx(struct task_struct *t)
  195. {
  196. if (cpu_has_lsx)
  197. _restore_lsx(&t->thread.fpu);
  198. }
  199. static inline void init_lsx_upper(void)
  200. {
  201. if (cpu_has_lsx)
  202. _init_lsx_upper();
  203. }
  204. static inline void restore_lsx_upper(struct task_struct *t)
  205. {
  206. if (cpu_has_lsx)
  207. _restore_lsx_upper(&t->thread.fpu);
  208. }
  209. #else
  210. static inline void enable_lsx(void) {}
  211. static inline void disable_lsx(void) {}
  212. static inline void save_lsx(struct task_struct *t) {}
  213. static inline void restore_lsx(struct task_struct *t) {}
  214. static inline void init_lsx_upper(void) {}
  215. static inline void restore_lsx_upper(struct task_struct *t) {}
  216. #endif
  217. #ifdef CONFIG_CPU_HAS_LASX
  218. static inline void enable_lasx(void)
  219. {
  220. if (cpu_has_lasx)
  221. csr_xchg32(CSR_EUEN_LASXEN, CSR_EUEN_LASXEN, LOONGARCH_CSR_EUEN);
  222. }
  223. static inline void disable_lasx(void)
  224. {
  225. if (cpu_has_lasx)
  226. csr_xchg32(0, CSR_EUEN_LASXEN, LOONGARCH_CSR_EUEN);
  227. }
  228. static inline void save_lasx(struct task_struct *t)
  229. {
  230. if (cpu_has_lasx)
  231. _save_lasx(&t->thread.fpu);
  232. }
  233. static inline void restore_lasx(struct task_struct *t)
  234. {
  235. if (cpu_has_lasx)
  236. _restore_lasx(&t->thread.fpu);
  237. }
  238. static inline void init_lasx_upper(void)
  239. {
  240. if (cpu_has_lasx)
  241. _init_lasx_upper();
  242. }
  243. static inline void restore_lasx_upper(struct task_struct *t)
  244. {
  245. if (cpu_has_lasx)
  246. _restore_lasx_upper(&t->thread.fpu);
  247. }
  248. #else
  249. static inline void enable_lasx(void) {}
  250. static inline void disable_lasx(void) {}
  251. static inline void save_lasx(struct task_struct *t) {}
  252. static inline void restore_lasx(struct task_struct *t) {}
  253. static inline void init_lasx_upper(void) {}
  254. static inline void restore_lasx_upper(struct task_struct *t) {}
  255. #endif
  256. static inline int thread_lsx_context_live(void)
  257. {
  258. if (!cpu_has_lsx)
  259. return 0;
  260. return test_thread_flag(TIF_LSX_CTX_LIVE);
  261. }
  262. static inline int thread_lasx_context_live(void)
  263. {
  264. if (!cpu_has_lasx)
  265. return 0;
  266. return test_thread_flag(TIF_LASX_CTX_LIVE);
  267. }
  268. #endif /* _ASM_FPU_H */