fpu.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include <asm/ptrace.h>
  26. #ifdef CONFIG_VSX
  27. #define __REST_32FPVSRS(n,c,base) \
  28. BEGIN_FTR_SECTION \
  29. b 2f; \
  30. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  31. REST_32FPRS(n,base); \
  32. b 3f; \
  33. 2: REST_32VSRS(n,c,base); \
  34. 3:
  35. #define __SAVE_32FPVSRS(n,c,base) \
  36. BEGIN_FTR_SECTION \
  37. b 2f; \
  38. END_FTR_SECTION_IFSET(CPU_FTR_VSX); \
  39. SAVE_32FPRS(n,base); \
  40. b 3f; \
  41. 2: SAVE_32VSRS(n,c,base); \
  42. 3:
  43. #else
  44. #define __REST_32FPVSRS(n,b,base) REST_32FPRS(n, base)
  45. #define __SAVE_32FPVSRS(n,b,base) SAVE_32FPRS(n, base)
  46. #endif
  47. #define REST_32FPVSRS(n,c,base) __REST_32FPVSRS(n,__REG_##c,__REG_##base)
  48. #define SAVE_32FPVSRS(n,c,base) __SAVE_32FPVSRS(n,__REG_##c,__REG_##base)
  49. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  50. /* void do_load_up_transact_fpu(struct thread_struct *thread)
  51. *
  52. * This is similar to load_up_fpu but for the transactional version of the FP
  53. * register set. It doesn't mess with the task MSR or valid flags.
  54. * Furthermore, we don't do lazy FP with TM currently.
  55. */
  56. _GLOBAL(do_load_up_transact_fpu)
  57. mfmsr r6
  58. ori r5,r6,MSR_FP
  59. #ifdef CONFIG_VSX
  60. BEGIN_FTR_SECTION
  61. oris r5,r5,MSR_VSX@h
  62. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  63. #endif
  64. SYNC
  65. MTMSRD(r5)
  66. addi r7,r3,THREAD_TRANSACT_FPSTATE
  67. lfd fr0,FPSTATE_FPSCR(r7)
  68. MTFSF_L(fr0)
  69. REST_32FPVSRS(0, R4, R7)
  70. /* FP/VSX off again */
  71. MTMSRD(r6)
  72. SYNC
  73. blr
  74. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  75. /*
  76. * Enable use of the FPU, and VSX if possible, for the caller.
  77. */
  78. _GLOBAL(fp_enable)
  79. mfmsr r3
  80. ori r3,r3,MSR_FP
  81. #ifdef CONFIG_VSX
  82. BEGIN_FTR_SECTION
  83. oris r3,r3,MSR_VSX@h
  84. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  85. #endif
  86. SYNC
  87. MTMSRD(r3)
  88. isync /* (not necessary for arch 2.02 and later) */
  89. blr
  90. /*
  91. * Load state from memory into FP registers including FPSCR.
  92. * Assumes the caller has enabled FP in the MSR.
  93. */
  94. _GLOBAL(load_fp_state)
  95. lfd fr0,FPSTATE_FPSCR(r3)
  96. MTFSF_L(fr0)
  97. REST_32FPVSRS(0, R4, R3)
  98. blr
  99. /*
  100. * Store FP state into memory, including FPSCR
  101. * Assumes the caller has enabled FP in the MSR.
  102. */
  103. _GLOBAL(store_fp_state)
  104. SAVE_32FPVSRS(0, R4, R3)
  105. mffs fr0
  106. stfd fr0,FPSTATE_FPSCR(r3)
  107. blr
  108. /*
  109. * This task wants to use the FPU now.
  110. * On UP, disable FP for the task which had the FPU previously,
  111. * and save its floating-point registers in its thread_struct.
  112. * Load up this task's FP registers from its thread_struct,
  113. * enable the FPU for the current task and return to the task.
  114. * Note that on 32-bit this can only use registers that will be
  115. * restored by fast_exception_return, i.e. r3 - r6, r10 and r11.
  116. */
  117. _GLOBAL(load_up_fpu)
  118. mfmsr r5
  119. ori r5,r5,MSR_FP
  120. #ifdef CONFIG_VSX
  121. BEGIN_FTR_SECTION
  122. oris r5,r5,MSR_VSX@h
  123. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  124. #endif
  125. SYNC
  126. MTMSRD(r5) /* enable use of fpu now */
  127. isync
  128. /*
  129. * For SMP, we don't do lazy FPU switching because it just gets too
  130. * horrendously complex, especially when a task switches from one CPU
  131. * to another. Instead we call giveup_fpu in switch_to.
  132. */
  133. #ifndef CONFIG_SMP
  134. LOAD_REG_ADDRBASE(r3, last_task_used_math)
  135. toreal(r3)
  136. PPC_LL r4,ADDROFF(last_task_used_math)(r3)
  137. PPC_LCMPI 0,r4,0
  138. beq 1f
  139. toreal(r4)
  140. addi r4,r4,THREAD /* want last_task_used_math->thread */
  141. addi r10,r4,THREAD_FPSTATE
  142. SAVE_32FPVSRS(0, R5, R10)
  143. mffs fr0
  144. stfd fr0,FPSTATE_FPSCR(r10)
  145. PPC_LL r5,PT_REGS(r4)
  146. toreal(r5)
  147. PPC_LL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  148. li r10,MSR_FP|MSR_FE0|MSR_FE1
  149. andc r4,r4,r10 /* disable FP for previous task */
  150. PPC_STL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  151. 1:
  152. #endif /* CONFIG_SMP */
  153. /* enable use of FP after return */
  154. #ifdef CONFIG_PPC32
  155. mfspr r5,SPRN_SPRG_THREAD /* current task's THREAD (phys) */
  156. lwz r4,THREAD_FPEXC_MODE(r5)
  157. ori r9,r9,MSR_FP /* enable FP for current */
  158. or r9,r9,r4
  159. #else
  160. ld r4,PACACURRENT(r13)
  161. addi r5,r4,THREAD /* Get THREAD */
  162. lwz r4,THREAD_FPEXC_MODE(r5)
  163. ori r12,r12,MSR_FP
  164. or r12,r12,r4
  165. std r12,_MSR(r1)
  166. #endif
  167. addi r10,r5,THREAD_FPSTATE
  168. lfd fr0,FPSTATE_FPSCR(r10)
  169. MTFSF_L(fr0)
  170. REST_32FPVSRS(0, R4, R10)
  171. #ifndef CONFIG_SMP
  172. subi r4,r5,THREAD
  173. fromreal(r4)
  174. PPC_STL r4,ADDROFF(last_task_used_math)(r3)
  175. #endif /* CONFIG_SMP */
  176. /* restore registers and return */
  177. /* we haven't used ctr or xer or lr */
  178. blr
  179. /*
  180. * giveup_fpu(tsk)
  181. * Disable FP for the task given as the argument,
  182. * and save the floating-point registers in its thread_struct.
  183. * Enables the FPU for use in the kernel on return.
  184. */
  185. _GLOBAL(giveup_fpu)
  186. mfmsr r5
  187. ori r5,r5,MSR_FP
  188. #ifdef CONFIG_VSX
  189. BEGIN_FTR_SECTION
  190. oris r5,r5,MSR_VSX@h
  191. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  192. #endif
  193. SYNC_601
  194. ISYNC_601
  195. MTMSRD(r5) /* enable use of fpu now */
  196. SYNC_601
  197. isync
  198. PPC_LCMPI 0,r3,0
  199. beqlr- /* if no previous owner, done */
  200. addi r3,r3,THREAD /* want THREAD of task */
  201. PPC_LL r6,THREAD_FPSAVEAREA(r3)
  202. PPC_LL r5,PT_REGS(r3)
  203. PPC_LCMPI 0,r6,0
  204. bne 2f
  205. addi r6,r3,THREAD_FPSTATE
  206. 2: PPC_LCMPI 0,r5,0
  207. SAVE_32FPVSRS(0, R4, R6)
  208. mffs fr0
  209. stfd fr0,FPSTATE_FPSCR(r6)
  210. beq 1f
  211. PPC_LL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  212. li r3,MSR_FP|MSR_FE0|MSR_FE1
  213. #ifdef CONFIG_VSX
  214. BEGIN_FTR_SECTION
  215. oris r3,r3,MSR_VSX@h
  216. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  217. #endif
  218. andc r4,r4,r3 /* disable FP for previous task */
  219. PPC_STL r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  220. 1:
  221. #ifndef CONFIG_SMP
  222. li r5,0
  223. LOAD_REG_ADDRBASE(r4,last_task_used_math)
  224. PPC_STL r5,ADDROFF(last_task_used_math)(r4)
  225. #endif /* CONFIG_SMP */
  226. blr
  227. /*
  228. * These are used in the alignment trap handler when emulating
  229. * single-precision loads and stores.
  230. */
  231. _GLOBAL(cvt_fd)
  232. lfs 0,0(r3)
  233. stfd 0,0(r4)
  234. blr
  235. _GLOBAL(cvt_df)
  236. lfd 0,0(r3)
  237. stfs 0,0(r4)
  238. blr