fpu.h 4.8 KB

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