2 * Copyright (C) 2002 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
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.
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>
19 #include <asm/mipsregs.h>
21 #include <asm/cpu-features.h>
22 #include <asm/fpu_emulator.h>
23 #include <asm/hazards.h>
24 #include <asm/processor.h>
25 #include <asm/current.h>
28 #ifdef CONFIG_MIPS_MT_FPAFF
29 #include <asm/mips_mt.h>
35 extern void _init_fpu(unsigned int);
36 extern void _save_fp(struct task_struct *);
37 extern void _restore_fp(struct task_struct *);
40 * This enum specifies a mode in which we want the FPU to operate, for cores
41 * which implement the Status.FR bit. Note that the bottom bit of the value
42 * purposefully matches the desired value of the Status.FR bit.
45 FPU_32BIT = 0, /* FR = 0 */
46 FPU_64BIT, /* FR = 1, FRE = 0 */
48 FPU_HYBRID, /* FR = 1, FRE = 1 */
50 #define FPU_FR_MASK 0x1
53 #define __disable_fpu() \
55 clear_c0_status(ST0_CU1); \
56 disable_fpu_hazard(); \
59 static inline int __enable_fpu(enum fpu_mode mode)
65 /* just enable the FPU in its current mode */
66 set_c0_status(ST0_CU1);
75 set_c0_config5(MIPS_CONF5_FRE);
79 #if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR6) \
80 || defined(CONFIG_64BIT))
81 /* we only have a 32-bit FPU */
88 clear_c0_config5(MIPS_CONF5_FRE);
91 /* set CU1 & change FR appropriately */
92 fr = (int)mode & FPU_FR_MASK;
93 change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
96 /* check FR has the desired value */
97 if (!!(read_c0_status() & ST0_FR) == !!fr)
100 /* unsupported FR value */
111 #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
113 static inline int __is_fpu_owner(void)
115 return test_thread_flag(TIF_USEDFPU);
118 static inline int is_fpu_owner(void)
120 return cpu_has_fpu && __is_fpu_owner();
123 static inline int __own_fpu(void)
128 if (test_thread_flag(TIF_HYBRID_FPREGS))
131 mode = !test_thread_flag(TIF_32BIT_FPREGS);
133 ret = __enable_fpu(mode);
137 KSTK_STATUS(current) |= ST0_CU1;
138 if (mode == FPU_64BIT || mode == FPU_HYBRID)
139 KSTK_STATUS(current) |= ST0_FR;
140 else /* mode == FPU_32BIT */
141 KSTK_STATUS(current) &= ~ST0_FR;
143 set_thread_flag(TIF_USEDFPU);
147 static inline int own_fpu_inatomic(int restore)
151 if (cpu_has_fpu && !__is_fpu_owner()) {
154 _restore_fp(current);
159 static inline int own_fpu(int restore)
164 ret = own_fpu_inatomic(restore);
169 static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
171 if (is_msa_enabled()) {
174 tsk->thread.fpu.fcr31 =
175 read_32bit_cp1_register(CP1_STATUS);
178 clear_tsk_thread_flag(tsk, TIF_USEDMSA);
180 } else if (is_fpu_owner()) {
185 /* FPU should not have been left enabled with no owner */
186 WARN(read_c0_status() & ST0_CU1,
187 "Orphaned FPU left enabled");
189 KSTK_STATUS(tsk) &= ~ST0_CU1;
190 clear_tsk_thread_flag(tsk, TIF_USEDFPU);
193 static inline void lose_fpu(int save)
196 lose_fpu_inatomic(save, current);
200 static inline int init_fpu(void)
202 unsigned int fcr31 = current->thread.fpu.fcr31;
206 unsigned int config5;
219 * Ensure FRE is clear whilst running _init_fpu, since
220 * single precision FP instructions are used. If FRE
221 * was set then we'll just end up initialising all 32
224 config5 = clear_c0_config5(MIPS_CONF5_FRE);
230 write_c0_config5(config5);
233 fpu_emulator_init_fpu();
238 static inline void save_fp(struct task_struct *tsk)
244 static inline void restore_fp(struct task_struct *tsk)
250 static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
252 if (tsk == current) {
259 return tsk->thread.fpu.fpr;
262 #endif /* _ASM_FPU_H */