api.h 3.8 KB

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