fpu.h 3.8 KB

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