fpu.h 5.1 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/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(unsigned int);
  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. #define __disable_fpu() \
  44. do { \
  45. clear_c0_status(ST0_CU1); \
  46. disable_fpu_hazard(); \
  47. } while (0)
  48. static inline int __enable_fpu(enum fpu_mode mode)
  49. {
  50. int fr;
  51. switch (mode) {
  52. case FPU_AS_IS:
  53. /* just enable the FPU in its current mode */
  54. set_c0_status(ST0_CU1);
  55. enable_fpu_hazard();
  56. return 0;
  57. case FPU_HYBRID:
  58. if (!cpu_has_fre)
  59. return SIGFPE;
  60. /* set FRE */
  61. set_c0_config5(MIPS_CONF5_FRE);
  62. goto fr_common;
  63. case FPU_64BIT:
  64. #if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) \
  65. || defined(CONFIG_64BIT))
  66. /* we only have a 32-bit FPU */
  67. return SIGFPE;
  68. #endif
  69. /* fall through */
  70. case FPU_32BIT:
  71. if (cpu_has_fre) {
  72. /* clear FRE */
  73. clear_c0_config5(MIPS_CONF5_FRE);
  74. }
  75. fr_common:
  76. /* set CU1 & change FR appropriately */
  77. fr = (int)mode & FPU_FR_MASK;
  78. change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
  79. enable_fpu_hazard();
  80. /* check FR has the desired value */
  81. if (!!(read_c0_status() & ST0_FR) == !!fr)
  82. return 0;
  83. /* unsupported FR value */
  84. __disable_fpu();
  85. return SIGFPE;
  86. default:
  87. BUG();
  88. }
  89. return SIGFPE;
  90. }
  91. #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
  92. static inline int __is_fpu_owner(void)
  93. {
  94. return test_thread_flag(TIF_USEDFPU);
  95. }
  96. static inline int is_fpu_owner(void)
  97. {
  98. return cpu_has_fpu && __is_fpu_owner();
  99. }
  100. static inline int __own_fpu(void)
  101. {
  102. enum fpu_mode mode;
  103. int ret;
  104. if (test_thread_flag(TIF_HYBRID_FPREGS))
  105. mode = FPU_HYBRID;
  106. else
  107. mode = !test_thread_flag(TIF_32BIT_FPREGS);
  108. ret = __enable_fpu(mode);
  109. if (ret)
  110. return ret;
  111. KSTK_STATUS(current) |= ST0_CU1;
  112. if (mode == FPU_64BIT || mode == FPU_HYBRID)
  113. KSTK_STATUS(current) |= ST0_FR;
  114. else /* mode == FPU_32BIT */
  115. KSTK_STATUS(current) &= ~ST0_FR;
  116. set_thread_flag(TIF_USEDFPU);
  117. return 0;
  118. }
  119. static inline int own_fpu_inatomic(int restore)
  120. {
  121. int ret = 0;
  122. if (cpu_has_fpu && !__is_fpu_owner()) {
  123. ret = __own_fpu();
  124. if (restore && !ret)
  125. _restore_fp(current);
  126. }
  127. return ret;
  128. }
  129. static inline int own_fpu(int restore)
  130. {
  131. int ret;
  132. preempt_disable();
  133. ret = own_fpu_inatomic(restore);
  134. preempt_enable();
  135. return ret;
  136. }
  137. static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
  138. {
  139. if (is_msa_enabled()) {
  140. if (save) {
  141. save_msa(tsk);
  142. tsk->thread.fpu.fcr31 =
  143. read_32bit_cp1_register(CP1_STATUS);
  144. }
  145. disable_msa();
  146. clear_tsk_thread_flag(tsk, TIF_USEDMSA);
  147. __disable_fpu();
  148. } else if (is_fpu_owner()) {
  149. if (save)
  150. _save_fp(tsk);
  151. __disable_fpu();
  152. } else {
  153. /* FPU should not have been left enabled with no owner */
  154. WARN(read_c0_status() & ST0_CU1,
  155. "Orphaned FPU left enabled");
  156. }
  157. KSTK_STATUS(tsk) &= ~ST0_CU1;
  158. clear_tsk_thread_flag(tsk, TIF_USEDFPU);
  159. }
  160. static inline void lose_fpu(int save)
  161. {
  162. preempt_disable();
  163. lose_fpu_inatomic(save, current);
  164. preempt_enable();
  165. }
  166. static inline int init_fpu(void)
  167. {
  168. unsigned int fcr31 = current->thread.fpu.fcr31;
  169. int ret = 0;
  170. if (cpu_has_fpu) {
  171. unsigned int config5;
  172. ret = __own_fpu();
  173. if (ret)
  174. return ret;
  175. if (!cpu_has_fre) {
  176. _init_fpu(fcr31);
  177. return 0;
  178. }
  179. /*
  180. * Ensure FRE is clear whilst running _init_fpu, since
  181. * single precision FP instructions are used. If FRE
  182. * was set then we'll just end up initialising all 32
  183. * 64b registers.
  184. */
  185. config5 = clear_c0_config5(MIPS_CONF5_FRE);
  186. enable_fpu_hazard();
  187. _init_fpu(fcr31);
  188. /* Restore FRE */
  189. write_c0_config5(config5);
  190. enable_fpu_hazard();
  191. } else
  192. fpu_emulator_init_fpu();
  193. return ret;
  194. }
  195. static inline void save_fp(struct task_struct *tsk)
  196. {
  197. if (cpu_has_fpu)
  198. _save_fp(tsk);
  199. }
  200. static inline void restore_fp(struct task_struct *tsk)
  201. {
  202. if (cpu_has_fpu)
  203. _restore_fp(tsk);
  204. }
  205. static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
  206. {
  207. if (tsk == current) {
  208. preempt_disable();
  209. if (is_fpu_owner())
  210. _save_fp(current);
  211. preempt_enable();
  212. }
  213. return tsk->thread.fpu.fpr;
  214. }
  215. #endif /* _ASM_FPU_H */