]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/mips/include/asm/fpu.h
2a631adb1fa863e013635e4e95b84df02604ccfc
[linux.git] / arch / mips / include / asm / fpu.h
1 /*
2  * Copyright (C) 2002 MontaVista Software Inc.
3  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  */
10 #ifndef _ASM_FPU_H
11 #define _ASM_FPU_H
12
13 #include <linux/sched.h>
14 #include <linux/sched/task_stack.h>
15 #include <linux/ptrace.h>
16 #include <linux/thread_info.h>
17 #include <linux/bitops.h>
18
19 #include <asm/mipsregs.h>
20 #include <asm/cpu.h>
21 #include <asm/cpu-features.h>
22 #include <asm/fpu_emulator.h>
23 #include <asm/hazards.h>
24 #include <asm/ptrace.h>
25 #include <asm/processor.h>
26 #include <asm/current.h>
27 #include <asm/msa.h>
28
29 #ifdef CONFIG_MIPS_MT_FPAFF
30 #include <asm/mips_mt.h>
31 #endif
32
33 extern void _save_fp(struct task_struct *);
34 extern void _restore_fp(struct task_struct *);
35
36 /*
37  * This enum specifies a mode in which we want the FPU to operate, for cores
38  * which implement the Status.FR bit. Note that the bottom bit of the value
39  * purposefully matches the desired value of the Status.FR bit.
40  */
41 enum fpu_mode {
42         FPU_32BIT = 0,          /* FR = 0 */
43         FPU_64BIT,              /* FR = 1, FRE = 0 */
44         FPU_AS_IS,
45         FPU_HYBRID,             /* FR = 1, FRE = 1 */
46
47 #define FPU_FR_MASK             0x1
48 };
49
50 #define __disable_fpu()                                                 \
51 do {                                                                    \
52         clear_c0_status(ST0_CU1);                                       \
53         disable_fpu_hazard();                                           \
54 } while (0)
55
56 static inline int __enable_fpu(enum fpu_mode mode)
57 {
58         int fr;
59
60         switch (mode) {
61         case FPU_AS_IS:
62                 /* just enable the FPU in its current mode */
63                 set_c0_status(ST0_CU1);
64                 enable_fpu_hazard();
65                 return 0;
66
67         case FPU_HYBRID:
68                 if (!cpu_has_fre)
69                         return SIGFPE;
70
71                 /* set FRE */
72                 set_c0_config5(MIPS_CONF5_FRE);
73                 goto fr_common;
74
75         case FPU_64BIT:
76 #if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) \
77       || defined(CONFIG_64BIT))
78                 /* we only have a 32-bit FPU */
79                 return SIGFPE;
80 #endif
81                 /* fall through */
82         case FPU_32BIT:
83                 if (cpu_has_fre) {
84                         /* clear FRE */
85                         clear_c0_config5(MIPS_CONF5_FRE);
86                 }
87 fr_common:
88                 /* set CU1 & change FR appropriately */
89                 fr = (int)mode & FPU_FR_MASK;
90                 change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
91                 enable_fpu_hazard();
92
93                 /* check FR has the desired value */
94                 if (!!(read_c0_status() & ST0_FR) == !!fr)
95                         return 0;
96
97                 /* unsupported FR value */
98                 __disable_fpu();
99                 return SIGFPE;
100
101         default:
102                 BUG();
103         }
104
105         return SIGFPE;
106 }
107
108 #define clear_fpu_owner()       clear_thread_flag(TIF_USEDFPU)
109
110 static inline int __is_fpu_owner(void)
111 {
112         return test_thread_flag(TIF_USEDFPU);
113 }
114
115 static inline int is_fpu_owner(void)
116 {
117         return cpu_has_fpu && __is_fpu_owner();
118 }
119
120 static inline int __own_fpu(void)
121 {
122         enum fpu_mode mode;
123         int ret;
124
125         if (test_thread_flag(TIF_HYBRID_FPREGS))
126                 mode = FPU_HYBRID;
127         else
128                 mode = !test_thread_flag(TIF_32BIT_FPREGS);
129
130         ret = __enable_fpu(mode);
131         if (ret)
132                 return ret;
133
134         KSTK_STATUS(current) |= ST0_CU1;
135         if (mode == FPU_64BIT || mode == FPU_HYBRID)
136                 KSTK_STATUS(current) |= ST0_FR;
137         else /* mode == FPU_32BIT */
138                 KSTK_STATUS(current) &= ~ST0_FR;
139
140         set_thread_flag(TIF_USEDFPU);
141         return 0;
142 }
143
144 static inline int own_fpu_inatomic(int restore)
145 {
146         int ret = 0;
147
148         if (cpu_has_fpu && !__is_fpu_owner()) {
149                 ret = __own_fpu();
150                 if (restore && !ret)
151                         _restore_fp(current);
152         }
153         return ret;
154 }
155
156 static inline int own_fpu(int restore)
157 {
158         int ret;
159
160         preempt_disable();
161         ret = own_fpu_inatomic(restore);
162         preempt_enable();
163         return ret;
164 }
165
166 static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
167 {
168         if (is_msa_enabled()) {
169                 if (save) {
170                         save_msa(tsk);
171                         tsk->thread.fpu.fcr31 =
172                                         read_32bit_cp1_register(CP1_STATUS);
173                 }
174                 disable_msa();
175                 clear_tsk_thread_flag(tsk, TIF_USEDMSA);
176                 __disable_fpu();
177         } else if (is_fpu_owner()) {
178                 if (save)
179                         _save_fp(tsk);
180                 __disable_fpu();
181         } else {
182                 /* FPU should not have been left enabled with no owner */
183                 WARN(read_c0_status() & ST0_CU1,
184                      "Orphaned FPU left enabled");
185         }
186         KSTK_STATUS(tsk) &= ~ST0_CU1;
187         clear_tsk_thread_flag(tsk, TIF_USEDFPU);
188 }
189
190 static inline void lose_fpu(int save)
191 {
192         preempt_disable();
193         lose_fpu_inatomic(save, current);
194         preempt_enable();
195 }
196
197 /**
198  * init_fp_ctx() - Initialize task FP context
199  * @target: The task whose FP context should be initialized.
200  *
201  * Initializes the FP context of the target task to sane default values if that
202  * target task does not already have valid FP context. Once the context has
203  * been initialized, the task will be marked as having used FP & thus having
204  * valid FP context.
205  *
206  * Returns: true if context is initialized, else false.
207  */
208 static inline bool init_fp_ctx(struct task_struct *target)
209 {
210         /* If FP has been used then the target already has context */
211         if (tsk_used_math(target))
212                 return false;
213
214         /* Begin with data registers set to all 1s... */
215         memset(&target->thread.fpu.fpr, ~0, sizeof(target->thread.fpu.fpr));
216
217         /* FCSR has been preset by `mips_set_personality_nan'.  */
218
219         /*
220          * Record that the target has "used" math, such that the context
221          * just initialised, and any modifications made by the caller,
222          * aren't discarded.
223          */
224         set_stopped_child_used_math(target);
225
226         return true;
227 }
228
229 static inline void save_fp(struct task_struct *tsk)
230 {
231         if (cpu_has_fpu)
232                 _save_fp(tsk);
233 }
234
235 static inline void restore_fp(struct task_struct *tsk)
236 {
237         if (cpu_has_fpu)
238                 _restore_fp(tsk);
239 }
240
241 static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
242 {
243         if (tsk == current) {
244                 preempt_disable();
245                 if (is_fpu_owner())
246                         _save_fp(current);
247                 preempt_enable();
248         }
249
250         return tsk->thread.fpu.fpr;
251 }
252
253 #endif /* _ASM_FPU_H */