1 /* SPDX-License-Identifier: GPL-2.0 */
3 * In-kernel FPU support functions
6 * Consider these guidelines before using in-kernel FPU functions:
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.
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:
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.
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.
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.
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.
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.
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.
40 * Copyright IBM Corp. 2015
41 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
44 #ifndef _ASM_S390_FPU_API_H
45 #define _ASM_S390_FPU_API_H
47 #include <linux/preempt.h>
49 void save_fpu_regs(void);
51 static inline int test_fp_ctl(u32 fpc
)
63 : "=d" (rc
), "=&d" (orig_fpc
)
64 : "d" (fpc
), "0" (-EINVAL
));
69 #define KERNEL_VXR_V0V7 2
70 #define KERNEL_VXR_V8V15 4
71 #define KERNEL_VXR_V16V23 8
72 #define KERNEL_VXR_V24V31 16
74 #define KERNEL_VXR_LOW (KERNEL_VXR_V0V7|KERNEL_VXR_V8V15)
75 #define KERNEL_VXR_MID (KERNEL_VXR_V8V15|KERNEL_VXR_V16V23)
76 #define KERNEL_VXR_HIGH (KERNEL_VXR_V16V23|KERNEL_VXR_V24V31)
78 #define KERNEL_VXR (KERNEL_VXR_LOW|KERNEL_VXR_HIGH)
79 #define KERNEL_FPR (KERNEL_FPC|KERNEL_VXR_V0V7)
84 * Note the functions below must be called with preemption disabled.
85 * Do not enable preemption before calling __kernel_fpu_end() to prevent
86 * an corruption of an existing kernel FPU state.
88 * Prefer using the kernel_fpu_begin()/kernel_fpu_end() pair of functions.
90 void __kernel_fpu_begin(struct kernel_fpu
*state
, u32 flags
);
91 void __kernel_fpu_end(struct kernel_fpu
*state
, u32 flags
);
94 static inline void kernel_fpu_begin(struct kernel_fpu
*state
, u32 flags
)
97 state
->mask
= S390_lowcore
.fpu_flags
;
98 if (!test_cpu_flag(CIF_FPU
))
99 /* Save user space FPU state and register contents */
101 else if (state
->mask
& flags
)
102 /* Save FPU/vector register in-use by the kernel */
103 __kernel_fpu_begin(state
, flags
);
104 S390_lowcore
.fpu_flags
|= flags
;
107 static inline void kernel_fpu_end(struct kernel_fpu
*state
, u32 flags
)
109 S390_lowcore
.fpu_flags
= state
->mask
;
110 if (state
->mask
& flags
)
111 /* Restore FPU/vector register in-use by the kernel */
112 __kernel_fpu_end(state
, flags
);
116 #endif /* _ASM_S390_FPU_API_H */