fpu.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 enable_fpu() \
  61. do { \
  62. if (cpu_has_fpu) \
  63. __enable_fpu(); \
  64. } while (0)
  65. #define disable_fpu() \
  66. do { \
  67. if (cpu_has_fpu) \
  68. __disable_fpu(); \
  69. } while (0)
  70. #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
  71. static inline int __is_fpu_owner(void)
  72. {
  73. return test_thread_flag(TIF_USEDFPU);
  74. }
  75. static inline int is_fpu_owner(void)
  76. {
  77. return cpu_has_fpu && __is_fpu_owner();
  78. }
  79. static inline void __own_fpu(void)
  80. {
  81. __enable_fpu();
  82. KSTK_STATUS(current) |= ST0_CU1;
  83. set_thread_flag(TIF_USEDFPU);
  84. }
  85. static inline void own_fpu_inatomic(int restore)
  86. {
  87. if (cpu_has_fpu && !__is_fpu_owner()) {
  88. __own_fpu();
  89. if (restore)
  90. _restore_fp(current);
  91. }
  92. }
  93. static inline void own_fpu(int restore)
  94. {
  95. preempt_disable();
  96. own_fpu_inatomic(restore);
  97. preempt_enable();
  98. }
  99. static inline void lose_fpu(int save)
  100. {
  101. preempt_disable();
  102. if (is_fpu_owner()) {
  103. if (save)
  104. _save_fp(current);
  105. KSTK_STATUS(current) &= ~ST0_CU1;
  106. clear_thread_flag(TIF_USEDFPU);
  107. __disable_fpu();
  108. }
  109. preempt_enable();
  110. }
  111. static inline void init_fpu(void)
  112. {
  113. preempt_disable();
  114. if (cpu_has_fpu) {
  115. __own_fpu();
  116. _init_fpu();
  117. } else {
  118. fpu_emulator_init_fpu();
  119. }
  120. preempt_enable();
  121. }
  122. static inline void save_fp(struct task_struct *tsk)
  123. {
  124. if (cpu_has_fpu)
  125. _save_fp(tsk);
  126. }
  127. static inline void restore_fp(struct task_struct *tsk)
  128. {
  129. if (cpu_has_fpu)
  130. _restore_fp(tsk);
  131. }
  132. static inline fpureg_t *get_fpu_regs(struct task_struct *tsk)
  133. {
  134. if (tsk == current) {
  135. preempt_disable();
  136. if (is_fpu_owner())
  137. _save_fp(current);
  138. preempt_enable();
  139. }
  140. return tsk->thread.fpu.fpr;
  141. }
  142. #endif /* _ASM_FPU_H */