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