fpu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * In-kernel vector facility support functions
  3. *
  4. * Copyright IBM Corp. 2015
  5. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/cpu.h>
  9. #include <linux/sched.h>
  10. #include <asm/fpu/types.h>
  11. #include <asm/fpu/api.h>
  12. asm(".include \"asm/vx-insn.h\"\n");
  13. void __kernel_fpu_begin(struct kernel_fpu *state, u32 flags)
  14. {
  15. /*
  16. * Limit the save to the FPU/vector registers already
  17. * in use by the previous context
  18. */
  19. flags &= state->mask;
  20. if (flags & KERNEL_FPC)
  21. /* Save floating point control */
  22. asm volatile("stfpc %0" : "=m" (state->fpc));
  23. if (!MACHINE_HAS_VX) {
  24. if (flags & KERNEL_VXR_V0V7) {
  25. /* Save floating-point registers */
  26. asm volatile("std 0,%0" : "=Q" (state->fprs[0]));
  27. asm volatile("std 1,%0" : "=Q" (state->fprs[1]));
  28. asm volatile("std 2,%0" : "=Q" (state->fprs[2]));
  29. asm volatile("std 3,%0" : "=Q" (state->fprs[3]));
  30. asm volatile("std 4,%0" : "=Q" (state->fprs[4]));
  31. asm volatile("std 5,%0" : "=Q" (state->fprs[5]));
  32. asm volatile("std 6,%0" : "=Q" (state->fprs[6]));
  33. asm volatile("std 7,%0" : "=Q" (state->fprs[7]));
  34. asm volatile("std 8,%0" : "=Q" (state->fprs[8]));
  35. asm volatile("std 9,%0" : "=Q" (state->fprs[9]));
  36. asm volatile("std 10,%0" : "=Q" (state->fprs[10]));
  37. asm volatile("std 11,%0" : "=Q" (state->fprs[11]));
  38. asm volatile("std 12,%0" : "=Q" (state->fprs[12]));
  39. asm volatile("std 13,%0" : "=Q" (state->fprs[13]));
  40. asm volatile("std 14,%0" : "=Q" (state->fprs[14]));
  41. asm volatile("std 15,%0" : "=Q" (state->fprs[15]));
  42. }
  43. return;
  44. }
  45. /* Test and save vector registers */
  46. asm volatile (
  47. /*
  48. * Test if any vector register must be saved and, if so,
  49. * test if all register can be saved.
  50. */
  51. " la 1,%[vxrs]\n" /* load save area */
  52. " tmll %[m],30\n" /* KERNEL_VXR */
  53. " jz 7f\n" /* no work -> done */
  54. " jo 5f\n" /* -> save V0..V31 */
  55. /*
  56. * Test for special case KERNEL_FPU_MID only. In this
  57. * case a vstm V8..V23 is the best instruction
  58. */
  59. " chi %[m],12\n" /* KERNEL_VXR_MID */
  60. " jne 0f\n" /* -> save V8..V23 */
  61. " VSTM 8,23,128,1\n" /* vstm %v8,%v23,128(%r1) */
  62. " j 7f\n"
  63. /* Test and save the first half of 16 vector registers */
  64. "0: tmll %[m],6\n" /* KERNEL_VXR_LOW */
  65. " jz 3f\n" /* -> KERNEL_VXR_HIGH */
  66. " jo 2f\n" /* 11 -> save V0..V15 */
  67. " brc 2,1f\n" /* 10 -> save V8..V15 */
  68. " VSTM 0,7,0,1\n" /* vstm %v0,%v7,0(%r1) */
  69. " j 3f\n"
  70. "1: VSTM 8,15,128,1\n" /* vstm %v8,%v15,128(%r1) */
  71. " j 3f\n"
  72. "2: VSTM 0,15,0,1\n" /* vstm %v0,%v15,0(%r1) */
  73. /* Test and save the second half of 16 vector registers */
  74. "3: tmll %[m],24\n" /* KERNEL_VXR_HIGH */
  75. " jz 7f\n"
  76. " jo 6f\n" /* 11 -> save V16..V31 */
  77. " brc 2,4f\n" /* 10 -> save V24..V31 */
  78. " VSTM 16,23,256,1\n" /* vstm %v16,%v23,256(%r1) */
  79. " j 7f\n"
  80. "4: VSTM 24,31,384,1\n" /* vstm %v24,%v31,384(%r1) */
  81. " j 7f\n"
  82. "5: VSTM 0,15,0,1\n" /* vstm %v0,%v15,0(%r1) */
  83. "6: VSTM 16,31,256,1\n" /* vstm %v16,%v31,256(%r1) */
  84. "7:"
  85. : [vxrs] "=Q" (*(struct vx_array *) &state->vxrs)
  86. : [m] "d" (flags)
  87. : "1", "cc");
  88. }
  89. EXPORT_SYMBOL(__kernel_fpu_begin);
  90. void __kernel_fpu_end(struct kernel_fpu *state, u32 flags)
  91. {
  92. /*
  93. * Limit the restore to the FPU/vector registers of the
  94. * previous context that have been overwritte by the
  95. * current context
  96. */
  97. flags &= state->mask;
  98. if (flags & KERNEL_FPC)
  99. /* Restore floating-point controls */
  100. asm volatile("lfpc %0" : : "Q" (state->fpc));
  101. if (!MACHINE_HAS_VX) {
  102. if (flags & KERNEL_VXR_V0V7) {
  103. /* Restore floating-point registers */
  104. asm volatile("ld 0,%0" : : "Q" (state->fprs[0]));
  105. asm volatile("ld 1,%0" : : "Q" (state->fprs[1]));
  106. asm volatile("ld 2,%0" : : "Q" (state->fprs[2]));
  107. asm volatile("ld 3,%0" : : "Q" (state->fprs[3]));
  108. asm volatile("ld 4,%0" : : "Q" (state->fprs[4]));
  109. asm volatile("ld 5,%0" : : "Q" (state->fprs[5]));
  110. asm volatile("ld 6,%0" : : "Q" (state->fprs[6]));
  111. asm volatile("ld 7,%0" : : "Q" (state->fprs[7]));
  112. asm volatile("ld 8,%0" : : "Q" (state->fprs[8]));
  113. asm volatile("ld 9,%0" : : "Q" (state->fprs[9]));
  114. asm volatile("ld 10,%0" : : "Q" (state->fprs[10]));
  115. asm volatile("ld 11,%0" : : "Q" (state->fprs[11]));
  116. asm volatile("ld 12,%0" : : "Q" (state->fprs[12]));
  117. asm volatile("ld 13,%0" : : "Q" (state->fprs[13]));
  118. asm volatile("ld 14,%0" : : "Q" (state->fprs[14]));
  119. asm volatile("ld 15,%0" : : "Q" (state->fprs[15]));
  120. }
  121. return;
  122. }
  123. /* Test and restore (load) vector registers */
  124. asm volatile (
  125. /*
  126. * Test if any vector register must be loaded and, if so,
  127. * test if all registers can be loaded at once.
  128. */
  129. " la 1,%[vxrs]\n" /* load restore area */
  130. " tmll %[m],30\n" /* KERNEL_VXR */
  131. " jz 7f\n" /* no work -> done */
  132. " jo 5f\n" /* -> restore V0..V31 */
  133. /*
  134. * Test for special case KERNEL_FPU_MID only. In this
  135. * case a vlm V8..V23 is the best instruction
  136. */
  137. " chi %[m],12\n" /* KERNEL_VXR_MID */
  138. " jne 0f\n" /* -> restore V8..V23 */
  139. " VLM 8,23,128,1\n" /* vlm %v8,%v23,128(%r1) */
  140. " j 7f\n"
  141. /* Test and restore the first half of 16 vector registers */
  142. "0: tmll %[m],6\n" /* KERNEL_VXR_LOW */
  143. " jz 3f\n" /* -> KERNEL_VXR_HIGH */
  144. " jo 2f\n" /* 11 -> restore V0..V15 */
  145. " brc 2,1f\n" /* 10 -> restore V8..V15 */
  146. " VLM 0,7,0,1\n" /* vlm %v0,%v7,0(%r1) */
  147. " j 3f\n"
  148. "1: VLM 8,15,128,1\n" /* vlm %v8,%v15,128(%r1) */
  149. " j 3f\n"
  150. "2: VLM 0,15,0,1\n" /* vlm %v0,%v15,0(%r1) */
  151. /* Test and restore the second half of 16 vector registers */
  152. "3: tmll %[m],24\n" /* KERNEL_VXR_HIGH */
  153. " jz 7f\n"
  154. " jo 6f\n" /* 11 -> restore V16..V31 */
  155. " brc 2,4f\n" /* 10 -> restore V24..V31 */
  156. " VLM 16,23,256,1\n" /* vlm %v16,%v23,256(%r1) */
  157. " j 7f\n"
  158. "4: VLM 24,31,384,1\n" /* vlm %v24,%v31,384(%r1) */
  159. " j 7f\n"
  160. "5: VLM 0,15,0,1\n" /* vlm %v0,%v15,0(%r1) */
  161. "6: VLM 16,31,256,1\n" /* vlm %v16,%v31,256(%r1) */
  162. "7:"
  163. : [vxrs] "=Q" (*(struct vx_array *) &state->vxrs)
  164. : [m] "d" (flags)
  165. : "1", "cc");
  166. }
  167. EXPORT_SYMBOL(__kernel_fpu_end);