fpu.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <asm/mipsregs.h>
  15. #include <asm/cpu.h>
  16. #include <asm/cpu-features.h>
  17. #include <asm/bitops.h>
  18. #include <asm/processor.h>
  19. #include <asm/current.h>
  20. #ifdef CONFIG_MIPS_MT_FPAFF
  21. #include <asm/mips_mt.h>
  22. #endif
  23. struct sigcontext;
  24. struct sigcontext32;
  25. extern asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
  26. extern asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
  27. extern asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
  28. extern asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
  29. extern void fpu_emulator_init_fpu(void);
  30. extern void _init_fpu(void);
  31. extern void _save_fp(struct task_struct *);
  32. extern void _restore_fp(struct task_struct *);
  33. #if defined(CONFIG_CPU_SB1)
  34. #define __enable_fpu_hazard() \
  35. do { \
  36. asm(".set push \n\t" \
  37. ".set mips64 \n\t" \
  38. ".set noreorder \n\t" \
  39. "ssnop \n\t" \
  40. "bnezl $0, .+4 \n\t" \
  41. "ssnop \n\t" \
  42. ".set pop"); \
  43. } while (0)
  44. #else
  45. #define __enable_fpu_hazard() \
  46. do { \
  47. asm("nop;nop;nop;nop"); /* max. hazard */ \
  48. } while (0)
  49. #endif
  50. #define __enable_fpu() \
  51. do { \
  52. set_c0_status(ST0_CU1); \
  53. __enable_fpu_hazard(); \
  54. } while (0)
  55. #define __disable_fpu() \
  56. do { \
  57. clear_c0_status(ST0_CU1); \
  58. /* We don't care about the c0 hazard here */ \
  59. } while (0)
  60. #define __fpu_enabled() (read_c0_status() & ST0_CU1)
  61. #define enable_fpu() \
  62. do { \
  63. if (cpu_has_fpu) \
  64. __enable_fpu(); \
  65. } while (0)
  66. #define disable_fpu() \
  67. do { \
  68. if (cpu_has_fpu) \
  69. __disable_fpu(); \
  70. } while (0)
  71. #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
  72. static inline int __is_fpu_owner(void)
  73. {
  74. return test_thread_flag(TIF_USEDFPU);
  75. }
  76. static inline int is_fpu_owner(void)
  77. {
  78. return cpu_has_fpu && __is_fpu_owner();
  79. }
  80. static inline void __own_fpu(void)
  81. {
  82. __enable_fpu();
  83. KSTK_STATUS(current) |= ST0_CU1;
  84. set_thread_flag(TIF_USEDFPU);
  85. }
  86. static inline void own_fpu(int restore)
  87. {
  88. preempt_disable();
  89. if (cpu_has_fpu && !__is_fpu_owner()) {
  90. __own_fpu();
  91. if (restore)
  92. _restore_fp(current);
  93. }
  94. preempt_enable();
  95. }
  96. static inline void lose_fpu(int save)
  97. {
  98. preempt_disable();
  99. if (is_fpu_owner()) {
  100. if (save)
  101. _save_fp(current);
  102. KSTK_STATUS(current) &= ~ST0_CU1;
  103. clear_thread_flag(TIF_USEDFPU);
  104. __disable_fpu();
  105. }
  106. preempt_enable();
  107. }
  108. static inline void init_fpu(void)
  109. {
  110. preempt_disable();
  111. if (cpu_has_fpu) {
  112. __own_fpu();
  113. _init_fpu();
  114. } else {
  115. fpu_emulator_init_fpu();
  116. }
  117. preempt_enable();
  118. }
  119. static inline void save_fp(struct task_struct *tsk)
  120. {
  121. if (cpu_has_fpu)
  122. _save_fp(tsk);
  123. }
  124. static inline void restore_fp(struct task_struct *tsk)
  125. {
  126. if (cpu_has_fpu)
  127. _restore_fp(tsk);
  128. }
  129. static inline fpureg_t *get_fpu_regs(struct task_struct *tsk)
  130. {
  131. if (tsk == current) {
  132. preempt_disable();
  133. if (is_fpu_owner())
  134. _save_fp(current);
  135. preempt_enable();
  136. }
  137. return tsk->thread.fpu.fpr;
  138. }
  139. static inline void enable_fp_in_kernel(void)
  140. {
  141. set_thread_flag(TIF_ALLOW_FP_IN_KERNEL);
  142. /* make sure CU1 and FPU ownership are consistent */
  143. if (!__is_fpu_owner() && __fpu_enabled())
  144. __disable_fpu();
  145. }
  146. static inline void disable_fp_in_kernel(void)
  147. {
  148. BUG_ON(!__is_fpu_owner() && __fpu_enabled());
  149. clear_thread_flag(TIF_ALLOW_FP_IN_KERNEL);
  150. }
  151. #endif /* _ASM_FPU_H */