fpu.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2002 MontaVista Software Inc.
  3. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  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_FPU_H
  11. #define _ASM_FPU_H
  12. #include <linux/sched.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <linux/thread_info.h>
  15. #include <linux/bitops.h>
  16. #include <asm/mipsregs.h>
  17. #include <asm/cpu.h>
  18. #include <asm/cpu-features.h>
  19. #include <asm/fpu_emulator.h>
  20. #include <asm/hazards.h>
  21. #include <asm/processor.h>
  22. #include <asm/current.h>
  23. #include <asm/msa.h>
  24. #ifdef CONFIG_MIPS_MT_FPAFF
  25. #include <asm/mips_mt.h>
  26. #endif
  27. struct sigcontext;
  28. struct sigcontext32;
  29. extern void _init_fpu(unsigned int);
  30. extern void _save_fp(struct task_struct *);
  31. extern void _restore_fp(struct task_struct *);
  32. /*
  33. * This enum specifies a mode in which we want the FPU to operate, for cores
  34. * which implement the Status.FR bit. Note that the bottom bit of the value
  35. * purposefully matches the desired value of the Status.FR bit.
  36. */
  37. enum fpu_mode {
  38. FPU_32BIT = 0, /* FR = 0 */
  39. FPU_64BIT, /* FR = 1, FRE = 0 */
  40. FPU_AS_IS,
  41. FPU_HYBRID, /* FR = 1, FRE = 1 */
  42. #define FPU_FR_MASK 0x1
  43. };
  44. #define __disable_fpu() \
  45. do { \
  46. clear_c0_status(ST0_CU1); \
  47. disable_fpu_hazard(); \
  48. } while (0)
  49. static inline int __enable_fpu(enum fpu_mode mode)
  50. {
  51. int fr;
  52. switch (mode) {
  53. case FPU_AS_IS:
  54. /* just enable the FPU in its current mode */
  55. set_c0_status(ST0_CU1);
  56. enable_fpu_hazard();
  57. return 0;
  58. case FPU_HYBRID:
  59. if (!cpu_has_fre)
  60. return SIGFPE;
  61. /* set FRE */
  62. set_c0_config5(MIPS_CONF5_FRE);
  63. goto fr_common;
  64. case FPU_64BIT:
  65. #if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) \
  66. || defined(CONFIG_64BIT))
  67. /* we only have a 32-bit FPU */
  68. return SIGFPE;
  69. #endif
  70. /* fall through */
  71. case FPU_32BIT:
  72. if (cpu_has_fre) {
  73. /* clear FRE */
  74. clear_c0_config5(MIPS_CONF5_FRE);
  75. }
  76. fr_common:
  77. /* set CU1 & change FR appropriately */
  78. fr = (int)mode & FPU_FR_MASK;
  79. change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
  80. enable_fpu_hazard();
  81. /* check FR has the desired value */
  82. if (!!(read_c0_status() & ST0_FR) == !!fr)
  83. return 0;
  84. /* unsupported FR value */
  85. __disable_fpu();
  86. return SIGFPE;
  87. default:
  88. BUG();
  89. }
  90. return SIGFPE;
  91. }
  92. #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
  93. static inline int __is_fpu_owner(void)
  94. {
  95. return test_thread_flag(TIF_USEDFPU);
  96. }
  97. static inline int is_fpu_owner(void)
  98. {
  99. return cpu_has_fpu && __is_fpu_owner();
  100. }
  101. static inline int __own_fpu(void)
  102. {
  103. enum fpu_mode mode;
  104. int ret;
  105. if (test_thread_flag(TIF_HYBRID_FPREGS))
  106. mode = FPU_HYBRID;
  107. else
  108. mode = !test_thread_flag(TIF_32BIT_FPREGS);
  109. ret = __enable_fpu(mode);
  110. if (ret)
  111. return ret;
  112. KSTK_STATUS(current) |= ST0_CU1;
  113. if (mode == FPU_64BIT || mode == FPU_HYBRID)
  114. KSTK_STATUS(current) |= ST0_FR;
  115. else /* mode == FPU_32BIT */
  116. KSTK_STATUS(current) &= ~ST0_FR;
  117. set_thread_flag(TIF_USEDFPU);
  118. return 0;
  119. }
  120. static inline int own_fpu_inatomic(int restore)
  121. {
  122. int ret = 0;
  123. if (cpu_has_fpu && !__is_fpu_owner()) {
  124. ret = __own_fpu();
  125. if (restore && !ret)
  126. _restore_fp(current);
  127. }
  128. return ret;
  129. }
  130. static inline int own_fpu(int restore)
  131. {
  132. int ret;
  133. preempt_disable();
  134. ret = own_fpu_inatomic(restore);
  135. preempt_enable();
  136. return ret;
  137. }
  138. static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
  139. {
  140. if (is_msa_enabled()) {
  141. if (save) {
  142. save_msa(tsk);
  143. tsk->thread.fpu.fcr31 =
  144. read_32bit_cp1_register(CP1_STATUS);
  145. }
  146. disable_msa();
  147. clear_tsk_thread_flag(tsk, TIF_USEDMSA);
  148. __disable_fpu();
  149. } else if (is_fpu_owner()) {
  150. if (save)
  151. _save_fp(tsk);
  152. __disable_fpu();
  153. } else {
  154. /* FPU should not have been left enabled with no owner */
  155. WARN(read_c0_status() & ST0_CU1,
  156. "Orphaned FPU left enabled");
  157. }
  158. KSTK_STATUS(tsk) &= ~ST0_CU1;
  159. clear_tsk_thread_flag(tsk, TIF_USEDFPU);
  160. }
  161. static inline void lose_fpu(int save)
  162. {
  163. preempt_disable();
  164. lose_fpu_inatomic(save, current);
  165. preempt_enable();
  166. }
  167. static inline int init_fpu(void)
  168. {
  169. unsigned int fcr31 = current->thread.fpu.fcr31;
  170. int ret = 0;
  171. if (cpu_has_fpu) {
  172. unsigned int config5;
  173. ret = __own_fpu();
  174. if (ret)
  175. return ret;
  176. if (!cpu_has_fre) {
  177. _init_fpu(fcr31);
  178. return 0;
  179. }
  180. /*
  181. * Ensure FRE is clear whilst running _init_fpu, since
  182. * single precision FP instructions are used. If FRE
  183. * was set then we'll just end up initialising all 32
  184. * 64b registers.
  185. */
  186. config5 = clear_c0_config5(MIPS_CONF5_FRE);
  187. enable_fpu_hazard();
  188. _init_fpu(fcr31);
  189. /* Restore FRE */
  190. write_c0_config5(config5);
  191. enable_fpu_hazard();
  192. } else
  193. fpu_emulator_init_fpu();
  194. return ret;
  195. }
  196. static inline void save_fp(struct task_struct *tsk)
  197. {
  198. if (cpu_has_fpu)
  199. _save_fp(tsk);
  200. }
  201. static inline void restore_fp(struct task_struct *tsk)
  202. {
  203. if (cpu_has_fpu)
  204. _restore_fp(tsk);
  205. }
  206. static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
  207. {
  208. if (tsk == current) {
  209. preempt_disable();
  210. if (is_fpu_owner())
  211. _save_fp(current);
  212. preempt_enable();
  213. }
  214. return tsk->thread.fpu.fpr;
  215. }
  216. #endif /* _ASM_FPU_H */