api.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * In-kernel FPU support functions
  3. *
  4. *
  5. * Consider these guidelines before using in-kernel FPU functions:
  6. *
  7. * 1. Use kernel_fpu_begin() and kernel_fpu_end() to enclose all in-kernel
  8. * use of floating-point or vector registers and instructions.
  9. *
  10. * 2. For kernel_fpu_begin(), specify the vector register range you want to
  11. * use with the KERNEL_VXR_* constants. Consider these usage guidelines:
  12. *
  13. * a) If your function typically runs in process-context, use the lower
  14. * half of the vector registers, for example, specify KERNEL_VXR_LOW.
  15. * b) If your function typically runs in soft-irq or hard-irq context,
  16. * prefer using the upper half of the vector registers, for example,
  17. * specify KERNEL_VXR_HIGH.
  18. *
  19. * If you adhere to these guidelines, an interrupted process context
  20. * does not require to save and restore vector registers because of
  21. * disjoint register ranges.
  22. *
  23. * Also note that the __kernel_fpu_begin()/__kernel_fpu_end() functions
  24. * includes logic to save and restore up to 16 vector registers at once.
  25. *
  26. * 3. You can nest kernel_fpu_begin()/kernel_fpu_end() by using different
  27. * struct kernel_fpu states. Vector registers that are in use by outer
  28. * levels are saved and restored. You can minimize the save and restore
  29. * effort by choosing disjoint vector register ranges.
  30. *
  31. * 5. To use vector floating-point instructions, specify the KERNEL_FPC
  32. * flag to save and restore floating-point controls in addition to any
  33. * vector register range.
  34. *
  35. * 6. To use floating-point registers and instructions only, specify the
  36. * KERNEL_FPR flag. This flag triggers a save and restore of vector
  37. * registers V0 to V15 and floating-point controls.
  38. *
  39. * Copyright IBM Corp. 2015
  40. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  41. */
  42. #ifndef _ASM_S390_FPU_API_H
  43. #define _ASM_S390_FPU_API_H
  44. #include <linux/preempt.h>
  45. void save_fpu_regs(void);
  46. static inline int test_fp_ctl(u32 fpc)
  47. {
  48. u32 orig_fpc;
  49. int rc;
  50. asm volatile(
  51. " efpc %1\n"
  52. " sfpc %2\n"
  53. "0: sfpc %1\n"
  54. " la %0,0\n"
  55. "1:\n"
  56. EX_TABLE(0b,1b)
  57. : "=d" (rc), "=&d" (orig_fpc)
  58. : "d" (fpc), "0" (-EINVAL));
  59. return rc;
  60. }
  61. #define KERNEL_FPC 1
  62. #define KERNEL_VXR_V0V7 2
  63. #define KERNEL_VXR_V8V15 4
  64. #define KERNEL_VXR_V16V23 8
  65. #define KERNEL_VXR_V24V31 16
  66. #define KERNEL_VXR_LOW (KERNEL_VXR_V0V7|KERNEL_VXR_V8V15)
  67. #define KERNEL_VXR_MID (KERNEL_VXR_V8V15|KERNEL_VXR_V16V23)
  68. #define KERNEL_VXR_HIGH (KERNEL_VXR_V16V23|KERNEL_VXR_V24V31)
  69. #define KERNEL_VXR (KERNEL_VXR_LOW|KERNEL_VXR_HIGH)
  70. #define KERNEL_FPR (KERNEL_FPC|KERNEL_VXR_V0V7)
  71. struct kernel_fpu;
  72. /*
  73. * Note the functions below must be called with preemption disabled.
  74. * Do not enable preemption before calling __kernel_fpu_end() to prevent
  75. * an corruption of an existing kernel FPU state.
  76. *
  77. * Prefer using the kernel_fpu_begin()/kernel_fpu_end() pair of functions.
  78. */
  79. void __kernel_fpu_begin(struct kernel_fpu *state, u32 flags);
  80. void __kernel_fpu_end(struct kernel_fpu *state, u32 flags);
  81. static inline void kernel_fpu_begin(struct kernel_fpu *state, u32 flags)
  82. {
  83. preempt_disable();
  84. state->mask = S390_lowcore.fpu_flags;
  85. if (!test_cpu_flag(CIF_FPU))
  86. /* Save user space FPU state and register contents */
  87. save_fpu_regs();
  88. else if (state->mask & flags)
  89. /* Save FPU/vector register in-use by the kernel */
  90. __kernel_fpu_begin(state, flags);
  91. S390_lowcore.fpu_flags |= flags;
  92. }
  93. static inline void kernel_fpu_end(struct kernel_fpu *state, u32 flags)
  94. {
  95. S390_lowcore.fpu_flags = state->mask;
  96. if (state->mask & flags)
  97. /* Restore FPU/vector register in-use by the kernel */
  98. __kernel_fpu_end(state, flags);
  99. preempt_enable();
  100. }
  101. #endif /* _ASM_S390_FPU_API_H */