vector.S 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <asm/processor.h>
  2. #include <asm/ppc_asm.h>
  3. #include <asm/reg.h>
  4. #include <asm/asm-offsets.h>
  5. #include <asm/cputable.h>
  6. #include <asm/thread_info.h>
  7. #include <asm/page.h>
  8. #include <asm/ptrace.h>
  9. #include <asm/export.h>
  10. /*
  11. * Load state from memory into VMX registers including VSCR.
  12. * Assumes the caller has enabled VMX in the MSR.
  13. */
  14. _GLOBAL(load_vr_state)
  15. li r4,VRSTATE_VSCR
  16. lvx v0,r4,r3
  17. mtvscr v0
  18. REST_32VRS(0,r4,r3)
  19. blr
  20. EXPORT_SYMBOL(load_vr_state)
  21. /*
  22. * Store VMX state into memory, including VSCR.
  23. * Assumes the caller has enabled VMX in the MSR.
  24. */
  25. _GLOBAL(store_vr_state)
  26. SAVE_32VRS(0, r4, r3)
  27. mfvscr v0
  28. li r4, VRSTATE_VSCR
  29. stvx v0, r4, r3
  30. blr
  31. EXPORT_SYMBOL(store_vr_state)
  32. /*
  33. * Disable VMX for the task which had it previously,
  34. * and save its vector registers in its thread_struct.
  35. * Enables the VMX for use in the kernel on return.
  36. * On SMP we know the VMX is free, since we give it up every
  37. * switch (ie, no lazy save of the vector registers).
  38. *
  39. * Note that on 32-bit this can only use registers that will be
  40. * restored by fast_exception_return, i.e. r3 - r6, r10 and r11.
  41. */
  42. _GLOBAL(load_up_altivec)
  43. mfmsr r5 /* grab the current MSR */
  44. oris r5,r5,MSR_VEC@h
  45. MTMSRD(r5) /* enable use of AltiVec now */
  46. isync
  47. /*
  48. * While userspace in general ignores VRSAVE, glibc uses it as a boolean
  49. * to optimise userspace context save/restore. Whenever we take an
  50. * altivec unavailable exception we must set VRSAVE to something non
  51. * zero. Set it to all 1s. See also the programming note in the ISA.
  52. */
  53. mfspr r4,SPRN_VRSAVE
  54. cmpwi 0,r4,0
  55. bne+ 1f
  56. li r4,-1
  57. mtspr SPRN_VRSAVE,r4
  58. 1:
  59. /* enable use of VMX after return */
  60. #ifdef CONFIG_PPC32
  61. mfspr r5,SPRN_SPRG_THREAD /* current task's THREAD (phys) */
  62. oris r9,r9,MSR_VEC@h
  63. #else
  64. ld r4,PACACURRENT(r13)
  65. addi r5,r4,THREAD /* Get THREAD */
  66. oris r12,r12,MSR_VEC@h
  67. std r12,_MSR(r1)
  68. #endif
  69. /* Don't care if r4 overflows, this is desired behaviour */
  70. lbz r4,THREAD_LOAD_VEC(r5)
  71. addi r4,r4,1
  72. stb r4,THREAD_LOAD_VEC(r5)
  73. addi r6,r5,THREAD_VRSTATE
  74. li r4,1
  75. li r10,VRSTATE_VSCR
  76. stw r4,THREAD_USED_VR(r5)
  77. lvx v0,r10,r6
  78. mtvscr v0
  79. REST_32VRS(0,r4,r6)
  80. /* restore registers and return */
  81. blr
  82. /*
  83. * save_altivec(tsk)
  84. * Save the vector registers to its thread_struct
  85. */
  86. _GLOBAL(save_altivec)
  87. addi r3,r3,THREAD /* want THREAD of task */
  88. PPC_LL r7,THREAD_VRSAVEAREA(r3)
  89. PPC_LL r5,PT_REGS(r3)
  90. PPC_LCMPI 0,r7,0
  91. bne 2f
  92. addi r7,r3,THREAD_VRSTATE
  93. 2: SAVE_32VRS(0,r4,r7)
  94. mfvscr v0
  95. li r4,VRSTATE_VSCR
  96. stvx v0,r4,r7
  97. blr
  98. #ifdef CONFIG_VSX
  99. #ifdef CONFIG_PPC32
  100. #error This asm code isn't ready for 32-bit kernels
  101. #endif
  102. /*
  103. * load_up_vsx(unused, unused, tsk)
  104. * Disable VSX for the task which had it previously,
  105. * and save its vector registers in its thread_struct.
  106. * Reuse the fp and vsx saves, but first check to see if they have
  107. * been saved already.
  108. */
  109. _GLOBAL(load_up_vsx)
  110. /* Load FP and VSX registers if they haven't been done yet */
  111. andi. r5,r12,MSR_FP
  112. beql+ load_up_fpu /* skip if already loaded */
  113. andis. r5,r12,MSR_VEC@h
  114. beql+ load_up_altivec /* skip if already loaded */
  115. ld r4,PACACURRENT(r13)
  116. addi r4,r4,THREAD /* Get THREAD */
  117. li r6,1
  118. stw r6,THREAD_USED_VSR(r4) /* ... also set thread used vsr */
  119. /* enable use of VSX after return */
  120. oris r12,r12,MSR_VSX@h
  121. std r12,_MSR(r1)
  122. b fast_exception_return
  123. #endif /* CONFIG_VSX */
  124. /*
  125. * The routines below are in assembler so we can closely control the
  126. * usage of floating-point registers. These routines must be called
  127. * with preempt disabled.
  128. */
  129. #ifdef CONFIG_PPC32
  130. .data
  131. fpzero:
  132. .long 0
  133. fpone:
  134. .long 0x3f800000 /* 1.0 in single-precision FP */
  135. fphalf:
  136. .long 0x3f000000 /* 0.5 in single-precision FP */
  137. #define LDCONST(fr, name) \
  138. lis r11,name@ha; \
  139. lfs fr,name@l(r11)
  140. #else
  141. .section ".toc","aw"
  142. fpzero:
  143. .tc FD_0_0[TC],0
  144. fpone:
  145. .tc FD_3ff00000_0[TC],0x3ff0000000000000 /* 1.0 */
  146. fphalf:
  147. .tc FD_3fe00000_0[TC],0x3fe0000000000000 /* 0.5 */
  148. #define LDCONST(fr, name) \
  149. lfd fr,name@toc(r2)
  150. #endif
  151. .text
  152. /*
  153. * Internal routine to enable floating point and set FPSCR to 0.
  154. * Don't call it from C; it doesn't use the normal calling convention.
  155. */
  156. fpenable:
  157. #ifdef CONFIG_PPC32
  158. stwu r1,-64(r1)
  159. #else
  160. stdu r1,-64(r1)
  161. #endif
  162. mfmsr r10
  163. ori r11,r10,MSR_FP
  164. mtmsr r11
  165. isync
  166. stfd fr0,24(r1)
  167. stfd fr1,16(r1)
  168. stfd fr31,8(r1)
  169. LDCONST(fr1, fpzero)
  170. mffs fr31
  171. MTFSF_L(fr1)
  172. blr
  173. fpdisable:
  174. mtlr r12
  175. MTFSF_L(fr31)
  176. lfd fr31,8(r1)
  177. lfd fr1,16(r1)
  178. lfd fr0,24(r1)
  179. mtmsr r10
  180. isync
  181. addi r1,r1,64
  182. blr
  183. /*
  184. * Vector add, floating point.
  185. */
  186. _GLOBAL(vaddfp)
  187. mflr r12
  188. bl fpenable
  189. li r0,4
  190. mtctr r0
  191. li r6,0
  192. 1: lfsx fr0,r4,r6
  193. lfsx fr1,r5,r6
  194. fadds fr0,fr0,fr1
  195. stfsx fr0,r3,r6
  196. addi r6,r6,4
  197. bdnz 1b
  198. b fpdisable
  199. /*
  200. * Vector subtract, floating point.
  201. */
  202. _GLOBAL(vsubfp)
  203. mflr r12
  204. bl fpenable
  205. li r0,4
  206. mtctr r0
  207. li r6,0
  208. 1: lfsx fr0,r4,r6
  209. lfsx fr1,r5,r6
  210. fsubs fr0,fr0,fr1
  211. stfsx fr0,r3,r6
  212. addi r6,r6,4
  213. bdnz 1b
  214. b fpdisable
  215. /*
  216. * Vector multiply and add, floating point.
  217. */
  218. _GLOBAL(vmaddfp)
  219. mflr r12
  220. bl fpenable
  221. stfd fr2,32(r1)
  222. li r0,4
  223. mtctr r0
  224. li r7,0
  225. 1: lfsx fr0,r4,r7
  226. lfsx fr1,r5,r7
  227. lfsx fr2,r6,r7
  228. fmadds fr0,fr0,fr2,fr1
  229. stfsx fr0,r3,r7
  230. addi r7,r7,4
  231. bdnz 1b
  232. lfd fr2,32(r1)
  233. b fpdisable
  234. /*
  235. * Vector negative multiply and subtract, floating point.
  236. */
  237. _GLOBAL(vnmsubfp)
  238. mflr r12
  239. bl fpenable
  240. stfd fr2,32(r1)
  241. li r0,4
  242. mtctr r0
  243. li r7,0
  244. 1: lfsx fr0,r4,r7
  245. lfsx fr1,r5,r7
  246. lfsx fr2,r6,r7
  247. fnmsubs fr0,fr0,fr2,fr1
  248. stfsx fr0,r3,r7
  249. addi r7,r7,4
  250. bdnz 1b
  251. lfd fr2,32(r1)
  252. b fpdisable
  253. /*
  254. * Vector reciprocal estimate. We just compute 1.0/x.
  255. * r3 -> destination, r4 -> source.
  256. */
  257. _GLOBAL(vrefp)
  258. mflr r12
  259. bl fpenable
  260. li r0,4
  261. LDCONST(fr1, fpone)
  262. mtctr r0
  263. li r6,0
  264. 1: lfsx fr0,r4,r6
  265. fdivs fr0,fr1,fr0
  266. stfsx fr0,r3,r6
  267. addi r6,r6,4
  268. bdnz 1b
  269. b fpdisable
  270. /*
  271. * Vector reciprocal square-root estimate, floating point.
  272. * We use the frsqrte instruction for the initial estimate followed
  273. * by 2 iterations of Newton-Raphson to get sufficient accuracy.
  274. * r3 -> destination, r4 -> source.
  275. */
  276. _GLOBAL(vrsqrtefp)
  277. mflr r12
  278. bl fpenable
  279. stfd fr2,32(r1)
  280. stfd fr3,40(r1)
  281. stfd fr4,48(r1)
  282. stfd fr5,56(r1)
  283. li r0,4
  284. LDCONST(fr4, fpone)
  285. LDCONST(fr5, fphalf)
  286. mtctr r0
  287. li r6,0
  288. 1: lfsx fr0,r4,r6
  289. frsqrte fr1,fr0 /* r = frsqrte(s) */
  290. fmuls fr3,fr1,fr0 /* r * s */
  291. fmuls fr2,fr1,fr5 /* r * 0.5 */
  292. fnmsubs fr3,fr1,fr3,fr4 /* 1 - s * r * r */
  293. fmadds fr1,fr2,fr3,fr1 /* r = r + 0.5 * r * (1 - s * r * r) */
  294. fmuls fr3,fr1,fr0 /* r * s */
  295. fmuls fr2,fr1,fr5 /* r * 0.5 */
  296. fnmsubs fr3,fr1,fr3,fr4 /* 1 - s * r * r */
  297. fmadds fr1,fr2,fr3,fr1 /* r = r + 0.5 * r * (1 - s * r * r) */
  298. stfsx fr1,r3,r6
  299. addi r6,r6,4
  300. bdnz 1b
  301. lfd fr5,56(r1)
  302. lfd fr4,48(r1)
  303. lfd fr3,40(r1)
  304. lfd fr2,32(r1)
  305. b fpdisable