s390/kernel: lazy restore fpu registers
[linux-2.6-block.git] / arch / s390 / include / asm / fpu-internal.h
1 /*
2  * General floating pointer and vector register helpers
3  *
4  * Copyright IBM Corp. 2015
5  * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
6  */
7
8 #ifndef _ASM_S390_FPU_INTERNAL_H
9 #define _ASM_S390_FPU_INTERNAL_H
10
11 #define FPU_USE_VX              1       /* Vector extension is active */
12
13 #ifndef __ASSEMBLY__
14
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <asm/linkage.h>
18 #include <asm/ctl_reg.h>
19 #include <asm/sigcontext.h>
20
21 struct fpu {
22         __u32 fpc;                      /* Floating-point control */
23         __u32 flags;
24         union {
25                 void *regs;
26                 freg_t *fprs;           /* Floating-point register save area */
27                 __vector128 *vxrs;      /* Vector register save area */
28         };
29 };
30
31 void save_fpu_regs(struct fpu *fpu);
32
33 #define is_vx_fpu(fpu) (!!((fpu)->flags & FPU_USE_VX))
34 #define is_vx_task(tsk) (!!((tsk)->thread.fpu.flags & FPU_USE_VX))
35
36 /* VX array structure for address operand constraints in inline assemblies */
37 struct vx_array { __vector128 _[__NUM_VXRS]; };
38
39 static inline int test_fp_ctl(u32 fpc)
40 {
41         u32 orig_fpc;
42         int rc;
43
44         asm volatile(
45                 "       efpc    %1\n"
46                 "       sfpc    %2\n"
47                 "0:     sfpc    %1\n"
48                 "       la      %0,0\n"
49                 "1:\n"
50                 EX_TABLE(0b,1b)
51                 : "=d" (rc), "=d" (orig_fpc)
52                 : "d" (fpc), "0" (-EINVAL));
53         return rc;
54 }
55
56 static inline void save_vx_regs_safe(__vector128 *vxrs)
57 {
58         unsigned long cr0, flags;
59
60         flags = arch_local_irq_save();
61         __ctl_store(cr0, 0, 0);
62         __ctl_set_bit(0, 17);
63         __ctl_set_bit(0, 18);
64         asm volatile(
65                 "       la      1,%0\n"
66                 "       .word   0xe70f,0x1000,0x003e\n" /* vstm 0,15,0(1) */
67                 "       .word   0xe70f,0x1100,0x0c3e\n" /* vstm 16,31,256(1) */
68                 : "=Q" (*(struct vx_array *) vxrs) : : "1");
69         __ctl_load(cr0, 0, 0);
70         arch_local_irq_restore(flags);
71 }
72
73 static inline void convert_vx_to_fp(freg_t *fprs, __vector128 *vxrs)
74 {
75         int i;
76
77         for (i = 0; i < __NUM_FPRS; i++)
78                 fprs[i] = *(freg_t *)(vxrs + i);
79 }
80
81 static inline void convert_fp_to_vx(__vector128 *vxrs, freg_t *fprs)
82 {
83         int i;
84
85         for (i = 0; i < __NUM_FPRS; i++)
86                 *(freg_t *)(vxrs + i) = fprs[i];
87 }
88
89 static inline void fpregs_store(_s390_fp_regs *fpregs, struct fpu *fpu)
90 {
91         fpregs->pad = 0;
92         if (is_vx_fpu(fpu))
93                 convert_vx_to_fp((freg_t *)&fpregs->fprs, fpu->vxrs);
94         else
95                 memcpy((freg_t *)&fpregs->fprs, fpu->fprs,
96                        sizeof(fpregs->fprs));
97 }
98
99 static inline void fpregs_load(_s390_fp_regs *fpregs, struct fpu *fpu)
100 {
101         if (is_vx_fpu(fpu))
102                 convert_fp_to_vx(fpu->vxrs, (freg_t *)&fpregs->fprs);
103         else
104                 memcpy(fpu->fprs, (freg_t *)&fpregs->fprs,
105                        sizeof(fpregs->fprs));
106 }
107
108 #endif
109
110 #endif /* _ASM_S390_FPU_INTERNAL_H */