fpu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * In-kernel vector facility support functions
  4. *
  5. * Copyright IBM Corp. 2015
  6. * Author(s): Hendrik Brueckner <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/cpu.h>
  10. #include <linux/sched.h>
  11. #include <asm/fpu/types.h>
  12. #include <asm/fpu/api.h>
  13. asm(".include \"asm/vx-insn.h\"\n");
  14. void __kernel_fpu_begin(struct kernel_fpu *state, u32 flags)
  15. {
  16. /*
  17. * Limit the save to the FPU/vector registers already
  18. * in use by the previous context
  19. */
  20. flags &= state->mask;
  21. if (flags & KERNEL_FPC)
  22. /* Save floating point control */
  23. asm volatile("stfpc %0" : "=Q" (state->fpc));
  24. if (!MACHINE_HAS_VX) {
  25. if (flags & KERNEL_VXR_V0V7) {
  26. /* Save floating-point registers */
  27. asm volatile("std 0,%0" : "=Q" (state->fprs[0]));
  28. asm volatile("std 1,%0" : "=Q" (state->fprs[1]));
  29. asm volatile("std 2,%0" : "=Q" (state->fprs[2]));
  30. asm volatile("std 3,%0" : "=Q" (state->fprs[3]));
  31. asm volatile("std 4,%0" : "=Q" (state->fprs[4]));
  32. asm volatile("std 5,%0" : "=Q" (state->fprs[5]));
  33. asm volatile("std 6,%0" : "=Q" (state->fprs[6]));
  34. asm volatile("std 7,%0" : "=Q" (state->fprs[7]));
  35. asm volatile("std 8,%0" : "=Q" (state->fprs[8]));
  36. asm volatile("std 9,%0" : "=Q" (state->fprs[9]));
  37. asm volatile("std 10,%0" : "=Q" (state->fprs[10]));
  38. asm volatile("std 11,%0" : "=Q" (state->fprs[11]));
  39. asm volatile("std 12,%0" : "=Q" (state->fprs[12]));
  40. asm volatile("std 13,%0" : "=Q" (state->fprs[13]));
  41. asm volatile("std 14,%0" : "=Q" (state->fprs[14]));
  42. asm volatile("std 15,%0" : "=Q" (state->fprs[15]));
  43. }
  44. return;
  45. }
  46. /* Test and save vector registers */
  47. asm volatile (
  48. /*
  49. * Test if any vector register must be saved and, if so,
  50. * test if all register can be saved.
  51. */
  52. " la 1,%[vxrs]\n" /* load save area */
  53. " tmll %[m],30\n" /* KERNEL_VXR */
  54. " jz 7f\n" /* no work -> done */
  55. " jo 5f\n" /* -> save V0..V31 */
  56. /*
  57. * Test for special case KERNEL_FPU_MID only. In this
  58. * case a vstm V8..V23 is the best instruction
  59. */
  60. " chi %[m],12\n" /* KERNEL_VXR_MID */
  61. " jne 0f\n" /* -> save V8..V23 */
  62. " VSTM 8,23,128,1\n" /* vstm %v8,%v23,128(%r1) */
  63. " j 7f\n"
  64. /* Test and save the first half of 16 vector registers */
  65. "0: tmll %[m],6\n" /* KERNEL_VXR_LOW */
  66. " jz 3f\n" /* -> KERNEL_VXR_HIGH */
  67. " jo 2f\n" /* 11 -> save V0..V15 */
  68. " brc 2,1f\n" /* 10 -> save V8..V15 */
  69. " VSTM 0,7,0,1\n" /* vstm %v0,%v7,0(%r1) */
  70. " j 3f\n"
  71. "1: VSTM 8,15,128,1\n" /* vstm %v8,%v15,128(%r1) */
  72. " j 3f\n"
  73. "2: VSTM 0,15,0,1\n" /* vstm %v0,%v15,0(%r1) */
  74. /* Test and save the second half of 16 vector registers */
  75. "3: tmll %[m],24\n" /* KERNEL_VXR_HIGH */
  76. " jz 7f\n"
  77. " jo 6f\n" /* 11 -> save V16..V31 */
  78. " brc 2,4f\n" /* 10 -> save V24..V31 */
  79. " VSTM 16,23,256,1\n" /* vstm %v16,%v23,256(%r1) */
  80. " j 7f\n"
  81. "4: VSTM 24,31,384,1\n" /* vstm %v24,%v31,384(%r1) */
  82. " j 7f\n"
  83. "5: VSTM 0,15,0,1\n" /* vstm %v0,%v15,0(%r1) */
  84. "6: VSTM 16,31,256,1\n" /* vstm %v16,%v31,256(%r1) */
  85. "7:"
  86. : [vxrs] "=Q" (*(struct vx_array *) &state->vxrs)
  87. : [m] "d" (flags)
  88. : "1", "cc");
  89. }
  90. EXPORT_SYMBOL(__kernel_fpu_begin);
  91. void __kernel_fpu_end(struct kernel_fpu *state, u32 flags)
  92. {
  93. /*
  94. * Limit the restore to the FPU/vector registers of the
  95. * previous context that have been overwritte by the
  96. * current context
  97. */
  98. flags &= state->mask;
  99. if (flags & KERNEL_FPC)
  100. /* Restore floating-point controls */
  101. asm volatile("lfpc %0" : : "Q" (state->fpc));
  102. if (!MACHINE_HAS_VX) {
  103. if (flags & KERNEL_VXR_V0V7) {
  104. /* Restore floating-point registers */
  105. asm volatile("ld 0,%0" : : "Q" (state->fprs[0]));
  106. asm volatile("ld 1,%0" : : "Q" (state->fprs[1]));
  107. asm volatile("ld 2,%0" : : "Q" (state->fprs[2]));
  108. asm volatile("ld 3,%0" : : "Q" (state->fprs[3]));
  109. asm volatile("ld 4,%0" : : "Q" (state->fprs[4]));
  110. asm volatile("ld 5,%0" : : "Q" (state->fprs[5]));
  111. asm volatile("ld 6,%0" : : "Q" (state->fprs[6]));
  112. asm volatile("ld 7,%0" : : "Q" (state->fprs[7]));
  113. asm volatile("ld 8,%0" : : "Q" (state->fprs[8]));
  114. asm volatile("ld 9,%0" : : "Q" (state->fprs[9]));
  115. asm volatile("ld 10,%0" : : "Q" (state->fprs[10]));
  116. asm volatile("ld 11,%0" : : "Q" (state->fprs[11]));
  117. asm volatile("ld 12,%0" : : "Q" (state->fprs[12]));
  118. asm volatile("ld 13,%0" : : "Q" (state->fprs[13]));
  119. asm volatile("ld 14,%0" : : "Q" (state->fprs[14]));
  120. asm volatile("ld 15,%0" : : "Q" (state->fprs[15]));
  121. }
  122. return;
  123. }
  124. /* Test and restore (load) vector registers */
  125. asm volatile (
  126. /*
  127. * Test if any vector register must be loaded and, if so,
  128. * test if all registers can be loaded at once.
  129. */
  130. " la 1,%[vxrs]\n" /* load restore area */
  131. " tmll %[m],30\n" /* KERNEL_VXR */
  132. " jz 7f\n" /* no work -> done */
  133. " jo 5f\n" /* -> restore V0..V31 */
  134. /*
  135. * Test for special case KERNEL_FPU_MID only. In this
  136. * case a vlm V8..V23 is the best instruction
  137. */
  138. " chi %[m],12\n" /* KERNEL_VXR_MID */
  139. " jne 0f\n" /* -> restore V8..V23 */
  140. " VLM 8,23,128,1\n" /* vlm %v8,%v23,128(%r1) */
  141. " j 7f\n"
  142. /* Test and restore the first half of 16 vector registers */
  143. "0: tmll %[m],6\n" /* KERNEL_VXR_LOW */
  144. " jz 3f\n" /* -> KERNEL_VXR_HIGH */
  145. " jo 2f\n" /* 11 -> restore V0..V15 */
  146. " brc 2,1f\n" /* 10 -> restore V8..V15 */
  147. " VLM 0,7,0,1\n" /* vlm %v0,%v7,0(%r1) */
  148. " j 3f\n"
  149. "1: VLM 8,15,128,1\n" /* vlm %v8,%v15,128(%r1) */
  150. " j 3f\n"
  151. "2: VLM 0,15,0,1\n" /* vlm %v0,%v15,0(%r1) */
  152. /* Test and restore the second half of 16 vector registers */
  153. "3: tmll %[m],24\n" /* KERNEL_VXR_HIGH */
  154. " jz 7f\n"
  155. " jo 6f\n" /* 11 -> restore V16..V31 */
  156. " brc 2,4f\n" /* 10 -> restore V24..V31 */
  157. " VLM 16,23,256,1\n" /* vlm %v16,%v23,256(%r1) */
  158. " j 7f\n"
  159. "4: VLM 24,31,384,1\n" /* vlm %v24,%v31,384(%r1) */
  160. " j 7f\n"
  161. "5: VLM 0,15,0,1\n" /* vlm %v0,%v15,0(%r1) */
  162. "6: VLM 16,31,256,1\n" /* vlm %v16,%v31,256(%r1) */
  163. "7:"
  164. : [vxrs] "=Q" (*(struct vx_array *) &state->vxrs)
  165. : [m] "d" (flags)
  166. : "1", "cc");
  167. }
  168. EXPORT_SYMBOL(__kernel_fpu_end);
  169. void __load_fpu_regs(void)
  170. {
  171. struct fpu *state = &current->thread.fpu;
  172. unsigned long *regs = current->thread.fpu.regs;
  173. asm volatile("lfpc %0" : : "Q" (state->fpc));
  174. if (likely(MACHINE_HAS_VX)) {
  175. asm volatile("lgr 1,%0\n"
  176. "VLM 0,15,0,1\n"
  177. "VLM 16,31,256,1\n"
  178. :
  179. : "d" (regs)
  180. : "1", "cc", "memory");
  181. } else {
  182. asm volatile("ld 0,%0" : : "Q" (regs[0]));
  183. asm volatile("ld 1,%0" : : "Q" (regs[1]));
  184. asm volatile("ld 2,%0" : : "Q" (regs[2]));
  185. asm volatile("ld 3,%0" : : "Q" (regs[3]));
  186. asm volatile("ld 4,%0" : : "Q" (regs[4]));
  187. asm volatile("ld 5,%0" : : "Q" (regs[5]));
  188. asm volatile("ld 6,%0" : : "Q" (regs[6]));
  189. asm volatile("ld 7,%0" : : "Q" (regs[7]));
  190. asm volatile("ld 8,%0" : : "Q" (regs[8]));
  191. asm volatile("ld 9,%0" : : "Q" (regs[9]));
  192. asm volatile("ld 10,%0" : : "Q" (regs[10]));
  193. asm volatile("ld 11,%0" : : "Q" (regs[11]));
  194. asm volatile("ld 12,%0" : : "Q" (regs[12]));
  195. asm volatile("ld 13,%0" : : "Q" (regs[13]));
  196. asm volatile("ld 14,%0" : : "Q" (regs[14]));
  197. asm volatile("ld 15,%0" : : "Q" (regs[15]));
  198. }
  199. clear_cpu_flag(CIF_FPU);
  200. }
  201. EXPORT_SYMBOL(__load_fpu_regs);
  202. void load_fpu_regs(void)
  203. {
  204. raw_local_irq_disable();
  205. __load_fpu_regs();
  206. raw_local_irq_enable();
  207. }
  208. EXPORT_SYMBOL(load_fpu_regs);
  209. void save_fpu_regs(void)
  210. {
  211. unsigned long flags, *regs;
  212. struct fpu *state;
  213. local_irq_save(flags);
  214. if (test_cpu_flag(CIF_FPU))
  215. goto out;
  216. state = &current->thread.fpu;
  217. regs = current->thread.fpu.regs;
  218. asm volatile("stfpc %0" : "=Q" (state->fpc));
  219. if (likely(MACHINE_HAS_VX)) {
  220. asm volatile("lgr 1,%0\n"
  221. "VSTM 0,15,0,1\n"
  222. "VSTM 16,31,256,1\n"
  223. :
  224. : "d" (regs)
  225. : "1", "cc", "memory");
  226. } else {
  227. asm volatile("std 0,%0" : "=Q" (regs[0]));
  228. asm volatile("std 1,%0" : "=Q" (regs[1]));
  229. asm volatile("std 2,%0" : "=Q" (regs[2]));
  230. asm volatile("std 3,%0" : "=Q" (regs[3]));
  231. asm volatile("std 4,%0" : "=Q" (regs[4]));
  232. asm volatile("std 5,%0" : "=Q" (regs[5]));
  233. asm volatile("std 6,%0" : "=Q" (regs[6]));
  234. asm volatile("std 7,%0" : "=Q" (regs[7]));
  235. asm volatile("std 8,%0" : "=Q" (regs[8]));
  236. asm volatile("std 9,%0" : "=Q" (regs[9]));
  237. asm volatile("std 10,%0" : "=Q" (regs[10]));
  238. asm volatile("std 11,%0" : "=Q" (regs[11]));
  239. asm volatile("std 12,%0" : "=Q" (regs[12]));
  240. asm volatile("std 13,%0" : "=Q" (regs[13]));
  241. asm volatile("std 14,%0" : "=Q" (regs[14]));
  242. asm volatile("std 15,%0" : "=Q" (regs[15]));
  243. }
  244. set_cpu_flag(CIF_FPU);
  245. out:
  246. local_irq_restore(flags);
  247. }
  248. EXPORT_SYMBOL(save_fpu_regs);