fpu.S 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * FPU support code, moved here from head.S so that it can be used
  3. * by chips which use other head-whatever.S files.
  4. *
  5. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  6. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  7. * Copyright (C) 1996 Paul Mackerras.
  8. * Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. */
  16. #include <asm/reg.h>
  17. #include <asm/page.h>
  18. #include <asm/mmu.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/cputable.h>
  21. #include <asm/cache.h>
  22. #include <asm/thread_info.h>
  23. #include <asm/ppc_asm.h>
  24. #include <asm/asm-offsets.h>
  25. #ifdef CONFIG_VSX
  26. #define REST_32FPVSRS(n,c,base) \
  27. BEGIN_FTR_SECTION \
  28. b 2f; \
  29. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  30. REST_32FPRS(n,base); \
  31. b 3f; \
  32. 2: REST_32VSRS(n,c,base); \
  33. 3:
  34. #define SAVE_32FPVSRS(n,c,base) \
  35. BEGIN_FTR_SECTION \
  36. b 2f; \
  37. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  38. SAVE_32FPRS(n,base); \
  39. b 3f; \
  40. 2: SAVE_32VSRS(n,c,base); \
  41. 3:
  42. #else
  43. #define REST_32FPVSRS(n,b,base) REST_32FPRS(n, base)
  44. #define SAVE_32FPVSRS(n,b,base) SAVE_32FPRS(n, base)
  45. #endif
  46. /*
  47. * This task wants to use the FPU now.
  48. * On UP, disable FP for the task which had the FPU previously,
  49. * and save its floating-point registers in its thread_struct.
  50. * Load up this task's FP registers from its thread_struct,
  51. * enable the FPU for the current task and return to the task.
  52. */
  53. _GLOBAL(load_up_fpu)
  54. mfmsr r5
  55. ori r5,r5,MSR_FP
  56. #ifdef CONFIG_VSX
  57. BEGIN_FTR_SECTION
  58. oris r5,r5,MSR_VSX@h
  59. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  60. #endif
  61. SYNC
  62. MTMSRD(r5) /* enable use of fpu now */
  63. isync
  64. /*
  65. * For SMP, we don't do lazy FPU switching because it just gets too
  66. * horrendously complex, especially when a task switches from one CPU
  67. * to another. Instead we call giveup_fpu in switch_to.
  68. */
  69. #ifndef CONFIG_SMP
  70. LOAD_REG_ADDRBASE(r3, last_task_used_math)
  71. toreal(r3)
  72. PPC_LL r4,ADDROFF(last_task_used_math)(r3)
  73. PPC_LCMPI 0,r4,0
  74. beq 1f
  75. toreal(r4)
  76. addi r4,r4,THREAD /* want last_task_used_math->thread */
  77. SAVE_32FPVSRS(0, r5, r4)
  78. mffs fr0
  79. stfd fr0,THREAD_FPSCR(r4)
  80. PPC_LL r5,PT_REGS(r4)
  81. toreal(r5)
  82. PPC_LL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  83. li r10,MSR_FP|MSR_FE0|MSR_FE1
  84. andc r4,r4,r10 /* disable FP for previous task */
  85. PPC_STL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  86. 1:
  87. #endif /* CONFIG_SMP */
  88. /* enable use of FP after return */
  89. #ifdef CONFIG_PPC32
  90. mfspr r5,SPRN_SPRG3 /* current task's THREAD (phys) */
  91. lwz r4,THREAD_FPEXC_MODE(r5)
  92. ori r9,r9,MSR_FP /* enable FP for current */
  93. or r9,r9,r4
  94. #else
  95. ld r4,PACACURRENT(r13)
  96. addi r5,r4,THREAD /* Get THREAD */
  97. lwz r4,THREAD_FPEXC_MODE(r5)
  98. ori r12,r12,MSR_FP
  99. or r12,r12,r4
  100. std r12,_MSR(r1)
  101. #endif
  102. lfd fr0,THREAD_FPSCR(r5)
  103. MTFSF_L(fr0)
  104. REST_32FPVSRS(0, r4, r5)
  105. #ifndef CONFIG_SMP
  106. subi r4,r5,THREAD
  107. fromreal(r4)
  108. PPC_STL r4,ADDROFF(last_task_used_math)(r3)
  109. #endif /* CONFIG_SMP */
  110. /* restore registers and return */
  111. /* we haven't used ctr or xer or lr */
  112. blr
  113. /*
  114. * giveup_fpu(tsk)
  115. * Disable FP for the task given as the argument,
  116. * and save the floating-point registers in its thread_struct.
  117. * Enables the FPU for use in the kernel on return.
  118. */
  119. _GLOBAL(giveup_fpu)
  120. mfmsr r5
  121. ori r5,r5,MSR_FP
  122. #ifdef CONFIG_VSX
  123. BEGIN_FTR_SECTION
  124. oris r5,r5,MSR_VSX@h
  125. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  126. #endif
  127. SYNC_601
  128. ISYNC_601
  129. MTMSRD(r5) /* enable use of fpu now */
  130. SYNC_601
  131. isync
  132. PPC_LCMPI 0,r3,0
  133. beqlr- /* if no previous owner, done */
  134. addi r3,r3,THREAD /* want THREAD of task */
  135. PPC_LL r5,PT_REGS(r3)
  136. PPC_LCMPI 0,r5,0
  137. SAVE_32FPVSRS(0, r4 ,r3)
  138. mffs fr0
  139. stfd fr0,THREAD_FPSCR(r3)
  140. beq 1f
  141. PPC_LL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  142. li r3,MSR_FP|MSR_FE0|MSR_FE1
  143. #ifdef CONFIG_VSX
  144. BEGIN_FTR_SECTION
  145. oris r3,r3,MSR_VSX@h
  146. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  147. #endif
  148. andc r4,r4,r3 /* disable FP for previous task */
  149. PPC_STL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  150. 1:
  151. #ifndef CONFIG_SMP
  152. li r5,0
  153. LOAD_REG_ADDRBASE(r4,last_task_used_math)
  154. PPC_STL r5,ADDROFF(last_task_used_math)(r4)
  155. #endif /* CONFIG_SMP */
  156. blr
  157. /*
  158. * These are used in the alignment trap handler when emulating
  159. * single-precision loads and stores.
  160. * We restore and save the fpscr so the task gets the same result
  161. * and exceptions as if the cpu had performed the load or store.
  162. */
  163. _GLOBAL(cvt_fd)
  164. lfd 0,THREAD_FPSCR(r5) /* load up fpscr value */
  165. MTFSF_L(0)
  166. lfs 0,0(r3)
  167. stfd 0,0(r4)
  168. mffs 0
  169. stfd 0,THREAD_FPSCR(r5) /* save new fpscr value */
  170. blr
  171. _GLOBAL(cvt_df)
  172. lfd 0,THREAD_FPSCR(r5) /* load up fpscr value */
  173. MTFSF_L(0)
  174. lfd 0,0(r3)
  175. stfs 0,0(r4)
  176. mffs 0
  177. stfd 0,THREAD_FPSCR(r5) /* save new fpscr value */
  178. blr