fpu.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 FPU_32BIT & FPU_64BIT
  34. * purposefully have the values 0 & 1 respectively, so that an integer value
  35. * of Status.FR can be trivially casted to the corresponding enum fpu_mode.
  36. */
  37. enum fpu_mode {
  38. FPU_32BIT = 0, /* FR = 0 */
  39. FPU_64BIT, /* FR = 1 */
  40. FPU_AS_IS,
  41. };
  42. static inline int __enable_fpu(enum fpu_mode mode)
  43. {
  44. int fr;
  45. switch (mode) {
  46. case FPU_AS_IS:
  47. /* just enable the FPU in its current mode */
  48. set_c0_status(ST0_CU1);
  49. enable_fpu_hazard();
  50. return 0;
  51. case FPU_64BIT:
  52. #if !(defined(CONFIG_CPU_MIPS32_R2) || defined(CONFIG_64BIT))
  53. /* we only have a 32-bit FPU */
  54. return SIGFPE;
  55. #endif
  56. /* fall through */
  57. case FPU_32BIT:
  58. /* set CU1 & change FR appropriately */
  59. fr = (int)mode;
  60. change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
  61. enable_fpu_hazard();
  62. /* check FR has the desired value */
  63. return (!!(read_c0_status() & ST0_FR) == !!fr) ? 0 : SIGFPE;
  64. default:
  65. BUG();
  66. }
  67. return SIGFPE;
  68. }
  69. #define __disable_fpu() \
  70. do { \
  71. clear_c0_status(ST0_CU1); \
  72. disable_fpu_hazard(); \
  73. } while (0)
  74. #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
  75. static inline int __is_fpu_owner(void)
  76. {
  77. return test_thread_flag(TIF_USEDFPU);
  78. }
  79. static inline int is_fpu_owner(void)
  80. {
  81. return cpu_has_fpu && __is_fpu_owner();
  82. }
  83. static inline int __own_fpu(void)
  84. {
  85. enum fpu_mode mode;
  86. int ret;
  87. mode = !test_thread_flag(TIF_32BIT_FPREGS);
  88. ret = __enable_fpu(mode);
  89. if (ret)
  90. return ret;
  91. KSTK_STATUS(current) |= ST0_CU1;
  92. if (mode == FPU_64BIT)
  93. KSTK_STATUS(current) |= ST0_FR;
  94. else /* mode == FPU_32BIT */
  95. KSTK_STATUS(current) &= ~ST0_FR;
  96. set_thread_flag(TIF_USEDFPU);
  97. return 0;
  98. }
  99. static inline int own_fpu_inatomic(int restore)
  100. {
  101. int ret = 0;
  102. if (cpu_has_fpu && !__is_fpu_owner()) {
  103. ret = __own_fpu();
  104. if (restore && !ret)
  105. _restore_fp(current);
  106. }
  107. return ret;
  108. }
  109. static inline int own_fpu(int restore)
  110. {
  111. int ret;
  112. preempt_disable();
  113. ret = own_fpu_inatomic(restore);
  114. preempt_enable();
  115. return ret;
  116. }
  117. static inline void lose_fpu(int save)
  118. {
  119. preempt_disable();
  120. if (is_msa_enabled()) {
  121. if (save) {
  122. save_msa(current);
  123. current->thread.fpu.fcr31 =
  124. read_32bit_cp1_register(CP1_STATUS);
  125. }
  126. disable_msa();
  127. clear_thread_flag(TIF_USEDMSA);
  128. __disable_fpu();
  129. } else if (is_fpu_owner()) {
  130. if (save)
  131. _save_fp(current);
  132. __disable_fpu();
  133. }
  134. KSTK_STATUS(current) &= ~ST0_CU1;
  135. clear_thread_flag(TIF_USEDFPU);
  136. preempt_enable();
  137. }
  138. static inline int init_fpu(void)
  139. {
  140. int ret = 0;
  141. if (cpu_has_fpu) {
  142. ret = __own_fpu();
  143. if (!ret)
  144. _init_fpu();
  145. } else
  146. fpu_emulator_init_fpu();
  147. return ret;
  148. }
  149. static inline void save_fp(struct task_struct *tsk)
  150. {
  151. if (cpu_has_fpu)
  152. _save_fp(tsk);
  153. }
  154. static inline void restore_fp(struct task_struct *tsk)
  155. {
  156. if (cpu_has_fpu)
  157. _restore_fp(tsk);
  158. }
  159. static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
  160. {
  161. if (tsk == current) {
  162. preempt_disable();
  163. if (is_fpu_owner())
  164. _save_fp(current);
  165. preempt_enable();
  166. }
  167. return tsk->thread.fpu.fpr;
  168. }
  169. #endif /* _ASM_FPU_H */