12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2384740dcSRalf Baechle /*
3384740dcSRalf Baechle * Copyright (C) 2002 MontaVista Software Inc.
4384740dcSRalf Baechle * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
5384740dcSRalf Baechle */
6384740dcSRalf Baechle #ifndef _ASM_FPU_H
7384740dcSRalf Baechle #define _ASM_FPU_H
8384740dcSRalf Baechle
9384740dcSRalf Baechle #include <linux/sched.h>
1068db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
11fc69910fSArnd Bergmann #include <linux/ptrace.h>
12384740dcSRalf Baechle #include <linux/thread_info.h>
13384740dcSRalf Baechle #include <linux/bitops.h>
14384740dcSRalf Baechle
15384740dcSRalf Baechle #include <asm/mipsregs.h>
16384740dcSRalf Baechle #include <asm/cpu.h>
17384740dcSRalf Baechle #include <asm/cpu-features.h>
18e0cc3a42SRalf Baechle #include <asm/fpu_emulator.h>
19384740dcSRalf Baechle #include <asm/hazards.h>
200c7e2bc8SJames Hogan #include <asm/ptrace.h>
21384740dcSRalf Baechle #include <asm/processor.h>
22384740dcSRalf Baechle #include <asm/current.h>
2333c771baSPaul Burton #include <asm/msa.h>
24384740dcSRalf Baechle
25384740dcSRalf Baechle #ifdef CONFIG_MIPS_MT_FPAFF
26384740dcSRalf Baechle #include <asm/mips_mt.h>
27384740dcSRalf Baechle #endif
28384740dcSRalf Baechle
29597ce172SPaul Burton /*
30597ce172SPaul Burton * This enum specifies a mode in which we want the FPU to operate, for cores
314227a2d4SPaul Burton * which implement the Status.FR bit. Note that the bottom bit of the value
324227a2d4SPaul Burton * purposefully matches the desired value of the Status.FR bit.
33597ce172SPaul Burton */
34597ce172SPaul Burton enum fpu_mode {
35597ce172SPaul Burton FPU_32BIT = 0, /* FR = 0 */
364227a2d4SPaul Burton FPU_64BIT, /* FR = 1, FRE = 0 */
37597ce172SPaul Burton FPU_AS_IS,
384227a2d4SPaul Burton FPU_HYBRID, /* FR = 1, FRE = 1 */
394227a2d4SPaul Burton
404227a2d4SPaul Burton #define FPU_FR_MASK 0x1
41597ce172SPaul Burton };
42597ce172SPaul Burton
439ec55930SPaul Burton #ifdef CONFIG_MIPS_FP_SUPPORT
449ec55930SPaul Burton
459ec55930SPaul Burton extern void _save_fp(struct task_struct *);
469ec55930SPaul Burton extern void _restore_fp(struct task_struct *);
479ec55930SPaul Burton
4884ab45b3SPaul Burton #define __disable_fpu() \
4984ab45b3SPaul Burton do { \
5084ab45b3SPaul Burton clear_c0_status(ST0_CU1); \
5184ab45b3SPaul Burton disable_fpu_hazard(); \
5284ab45b3SPaul Burton } while (0)
5384ab45b3SPaul Burton
__enable_fpu(enum fpu_mode mode)54597ce172SPaul Burton static inline int __enable_fpu(enum fpu_mode mode)
55597ce172SPaul Burton {
56597ce172SPaul Burton int fr;
57597ce172SPaul Burton
58597ce172SPaul Burton switch (mode) {
59597ce172SPaul Burton case FPU_AS_IS:
60597ce172SPaul Burton /* just enable the FPU in its current mode */
61597ce172SPaul Burton set_c0_status(ST0_CU1);
62597ce172SPaul Burton enable_fpu_hazard();
63597ce172SPaul Burton return 0;
64597ce172SPaul Burton
654227a2d4SPaul Burton case FPU_HYBRID:
664227a2d4SPaul Burton if (!cpu_has_fre)
674227a2d4SPaul Burton return SIGFPE;
684227a2d4SPaul Burton
694227a2d4SPaul Burton /* set FRE */
70d33e6fe3SRalf Baechle set_c0_config5(MIPS_CONF5_FRE);
714227a2d4SPaul Burton goto fr_common;
724227a2d4SPaul Burton
73597ce172SPaul Burton case FPU_64BIT:
74ab7c01fdSSerge Semin #if !(defined(CONFIG_CPU_MIPSR2) || defined(CONFIG_CPU_MIPSR5) || \
75ab7c01fdSSerge Semin defined(CONFIG_CPU_MIPSR6) || defined(CONFIG_64BIT))
76597ce172SPaul Burton /* we only have a 32-bit FPU */
77597ce172SPaul Burton return SIGFPE;
78597ce172SPaul Burton #endif
79cf6678aeSGustavo A. R. Silva /* fallthrough */
80597ce172SPaul Burton case FPU_32BIT:
81b0c34f61SRalf Baechle if (cpu_has_fre) {
824227a2d4SPaul Burton /* clear FRE */
83d33e6fe3SRalf Baechle clear_c0_config5(MIPS_CONF5_FRE);
84b0c34f61SRalf Baechle }
854227a2d4SPaul Burton fr_common:
86597ce172SPaul Burton /* set CU1 & change FR appropriately */
874227a2d4SPaul Burton fr = (int)mode & FPU_FR_MASK;
88597ce172SPaul Burton change_c0_status(ST0_CU1 | ST0_FR, ST0_CU1 | (fr ? ST0_FR : 0));
89597ce172SPaul Burton enable_fpu_hazard();
90597ce172SPaul Burton
91597ce172SPaul Burton /* check FR has the desired value */
9284ab45b3SPaul Burton if (!!(read_c0_status() & ST0_FR) == !!fr)
9384ab45b3SPaul Burton return 0;
9484ab45b3SPaul Burton
9584ab45b3SPaul Burton /* unsupported FR value */
9684ab45b3SPaul Burton __disable_fpu();
9784ab45b3SPaul Burton return SIGFPE;
98597ce172SPaul Burton
99597ce172SPaul Burton default:
100597ce172SPaul Burton BUG();
101597ce172SPaul Burton }
10297b8b16bSAaro Koskinen
10397b8b16bSAaro Koskinen return SIGFPE;
104597ce172SPaul Burton }
105384740dcSRalf Baechle
106384740dcSRalf Baechle #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
107384740dcSRalf Baechle
__is_fpu_owner(void)108384740dcSRalf Baechle static inline int __is_fpu_owner(void)
109384740dcSRalf Baechle {
110384740dcSRalf Baechle return test_thread_flag(TIF_USEDFPU);
111384740dcSRalf Baechle }
112384740dcSRalf Baechle
is_fpu_owner(void)113384740dcSRalf Baechle static inline int is_fpu_owner(void)
114384740dcSRalf Baechle {
115384740dcSRalf Baechle return cpu_has_fpu && __is_fpu_owner();
116384740dcSRalf Baechle }
117384740dcSRalf Baechle
__own_fpu(void)118597ce172SPaul Burton static inline int __own_fpu(void)
119384740dcSRalf Baechle {
120597ce172SPaul Burton enum fpu_mode mode;
121597ce172SPaul Burton int ret;
122597ce172SPaul Burton
1234227a2d4SPaul Burton if (test_thread_flag(TIF_HYBRID_FPREGS))
1244227a2d4SPaul Burton mode = FPU_HYBRID;
1254227a2d4SPaul Burton else
126597ce172SPaul Burton mode = !test_thread_flag(TIF_32BIT_FPREGS);
1274227a2d4SPaul Burton
128597ce172SPaul Burton ret = __enable_fpu(mode);
129597ce172SPaul Burton if (ret)
130597ce172SPaul Burton return ret;
131597ce172SPaul Burton
132*59649de9SJiaxun Yang if (current->thread.fpu.fcr31 & FPU_CSR_NAN2008) {
133*59649de9SJiaxun Yang if (!cpu_has_nan_2008) {
134*59649de9SJiaxun Yang ret = SIGFPE;
135*59649de9SJiaxun Yang goto failed;
136*59649de9SJiaxun Yang }
137*59649de9SJiaxun Yang } else {
138*59649de9SJiaxun Yang if (!cpu_has_nan_legacy) {
139*59649de9SJiaxun Yang ret = SIGFPE;
140*59649de9SJiaxun Yang goto failed;
141*59649de9SJiaxun Yang }
142*59649de9SJiaxun Yang }
143*59649de9SJiaxun Yang
144384740dcSRalf Baechle KSTK_STATUS(current) |= ST0_CU1;
1454227a2d4SPaul Burton if (mode == FPU_64BIT || mode == FPU_HYBRID)
146597ce172SPaul Burton KSTK_STATUS(current) |= ST0_FR;
147597ce172SPaul Burton else /* mode == FPU_32BIT */
148597ce172SPaul Burton KSTK_STATUS(current) &= ~ST0_FR;
149597ce172SPaul Burton
150384740dcSRalf Baechle set_thread_flag(TIF_USEDFPU);
151597ce172SPaul Burton return 0;
152*59649de9SJiaxun Yang failed:
153*59649de9SJiaxun Yang __disable_fpu();
154*59649de9SJiaxun Yang return ret;
155384740dcSRalf Baechle }
156384740dcSRalf Baechle
own_fpu_inatomic(int restore)157597ce172SPaul Burton static inline int own_fpu_inatomic(int restore)
158384740dcSRalf Baechle {
159597ce172SPaul Burton int ret = 0;
160597ce172SPaul Burton
161384740dcSRalf Baechle if (cpu_has_fpu && !__is_fpu_owner()) {
162597ce172SPaul Burton ret = __own_fpu();
163597ce172SPaul Burton if (restore && !ret)
164384740dcSRalf Baechle _restore_fp(current);
165384740dcSRalf Baechle }
166597ce172SPaul Burton return ret;
167384740dcSRalf Baechle }
168384740dcSRalf Baechle
own_fpu(int restore)169597ce172SPaul Burton static inline int own_fpu(int restore)
170384740dcSRalf Baechle {
171597ce172SPaul Burton int ret;
172597ce172SPaul Burton
173384740dcSRalf Baechle preempt_disable();
174597ce172SPaul Burton ret = own_fpu_inatomic(restore);
175384740dcSRalf Baechle preempt_enable();
176597ce172SPaul Burton return ret;
177384740dcSRalf Baechle }
178384740dcSRalf Baechle
lose_fpu_inatomic(int save,struct task_struct * tsk)1791a3d5957SPaul Burton static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
180384740dcSRalf Baechle {
18133c771baSPaul Burton if (is_msa_enabled()) {
18233c771baSPaul Burton if (save) {
1831a3d5957SPaul Burton save_msa(tsk);
1841a3d5957SPaul Burton tsk->thread.fpu.fcr31 =
185842dfc11SManuel Lauss read_32bit_cp1_register(CP1_STATUS);
18633c771baSPaul Burton }
18733c771baSPaul Burton disable_msa();
1881a3d5957SPaul Burton clear_tsk_thread_flag(tsk, TIF_USEDMSA);
189acaf6a97SJames Hogan __disable_fpu();
19033c771baSPaul Burton } else if (is_fpu_owner()) {
191384740dcSRalf Baechle if (save)
1921a3d5957SPaul Burton _save_fp(tsk);
193384740dcSRalf Baechle __disable_fpu();
19400fe56dcSJames Hogan } else {
19500fe56dcSJames Hogan /* FPU should not have been left enabled with no owner */
19600fe56dcSJames Hogan WARN(read_c0_status() & ST0_CU1,
19700fe56dcSJames Hogan "Orphaned FPU left enabled");
198384740dcSRalf Baechle }
1991a3d5957SPaul Burton KSTK_STATUS(tsk) &= ~ST0_CU1;
2001a3d5957SPaul Burton clear_tsk_thread_flag(tsk, TIF_USEDFPU);
2011a3d5957SPaul Burton }
2021a3d5957SPaul Burton
lose_fpu(int save)2031a3d5957SPaul Burton static inline void lose_fpu(int save)
2041a3d5957SPaul Burton {
2051a3d5957SPaul Burton preempt_disable();
2061a3d5957SPaul Burton lose_fpu_inatomic(save, current);
207384740dcSRalf Baechle preempt_enable();
208384740dcSRalf Baechle }
209384740dcSRalf Baechle
210cc97ab23SPaul Burton /**
211cc97ab23SPaul Burton * init_fp_ctx() - Initialize task FP context
212cc97ab23SPaul Burton * @target: The task whose FP context should be initialized.
213cc97ab23SPaul Burton *
214cc97ab23SPaul Burton * Initializes the FP context of the target task to sane default values if that
215cc97ab23SPaul Burton * target task does not already have valid FP context. Once the context has
216cc97ab23SPaul Burton * been initialized, the task will be marked as having used FP & thus having
217cc97ab23SPaul Burton * valid FP context.
218cc97ab23SPaul Burton *
219cc97ab23SPaul Burton * Returns: true if context is initialized, else false.
220cc97ab23SPaul Burton */
init_fp_ctx(struct task_struct * target)221cc97ab23SPaul Burton static inline bool init_fp_ctx(struct task_struct *target)
222384740dcSRalf Baechle {
223cc97ab23SPaul Burton /* If FP has been used then the target already has context */
224cc97ab23SPaul Burton if (tsk_used_math(target))
225cc97ab23SPaul Burton return false;
226597ce172SPaul Burton
227cc97ab23SPaul Burton /* Begin with data registers set to all 1s... */
228cc97ab23SPaul Burton memset(&target->thread.fpu.fpr, ~0, sizeof(target->thread.fpu.fpr));
229b0c34f61SRalf Baechle
230cc97ab23SPaul Burton /* FCSR has been preset by `mips_set_personality_nan'. */
231b0c34f61SRalf Baechle
2324227a2d4SPaul Burton /*
233cc97ab23SPaul Burton * Record that the target has "used" math, such that the context
234cc97ab23SPaul Burton * just initialised, and any modifications made by the caller,
235cc97ab23SPaul Burton * aren't discarded.
2364227a2d4SPaul Burton */
237cc97ab23SPaul Burton set_stopped_child_used_math(target);
2384227a2d4SPaul Burton
239cc97ab23SPaul Burton return true;
240384740dcSRalf Baechle }
241384740dcSRalf Baechle
save_fp(struct task_struct * tsk)242384740dcSRalf Baechle static inline void save_fp(struct task_struct *tsk)
243384740dcSRalf Baechle {
244384740dcSRalf Baechle if (cpu_has_fpu)
245384740dcSRalf Baechle _save_fp(tsk);
246384740dcSRalf Baechle }
247384740dcSRalf Baechle
restore_fp(struct task_struct * tsk)248384740dcSRalf Baechle static inline void restore_fp(struct task_struct *tsk)
249384740dcSRalf Baechle {
250384740dcSRalf Baechle if (cpu_has_fpu)
251384740dcSRalf Baechle _restore_fp(tsk);
252384740dcSRalf Baechle }
253384740dcSRalf Baechle
get_fpu_regs(struct task_struct * tsk)254bbd426f5SPaul Burton static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
255384740dcSRalf Baechle {
256384740dcSRalf Baechle if (tsk == current) {
257384740dcSRalf Baechle preempt_disable();
258384740dcSRalf Baechle if (is_fpu_owner())
259384740dcSRalf Baechle _save_fp(current);
260384740dcSRalf Baechle preempt_enable();
261384740dcSRalf Baechle }
262384740dcSRalf Baechle
263384740dcSRalf Baechle return tsk->thread.fpu.fpr;
264384740dcSRalf Baechle }
265384740dcSRalf Baechle
2669ec55930SPaul Burton #else /* !CONFIG_MIPS_FP_SUPPORT */
2679ec55930SPaul Burton
2689ec55930SPaul Burton /*
2699ec55930SPaul Burton * When FP support is disabled we provide only a minimal set of stub functions
2709ec55930SPaul Burton * to avoid callers needing to care too much about CONFIG_MIPS_FP_SUPPORT.
2719ec55930SPaul Burton */
2729ec55930SPaul Burton
__enable_fpu(enum fpu_mode mode)2739ec55930SPaul Burton static inline int __enable_fpu(enum fpu_mode mode)
2749ec55930SPaul Burton {
2759ec55930SPaul Burton return SIGILL;
2769ec55930SPaul Burton }
2779ec55930SPaul Burton
__disable_fpu(void)2789ec55930SPaul Burton static inline void __disable_fpu(void)
2799ec55930SPaul Burton {
2809ec55930SPaul Burton /* no-op */
2819ec55930SPaul Burton }
2829ec55930SPaul Burton
2839ec55930SPaul Burton
is_fpu_owner(void)2849ec55930SPaul Burton static inline int is_fpu_owner(void)
2859ec55930SPaul Burton {
2869ec55930SPaul Burton return 0;
2879ec55930SPaul Burton }
2889ec55930SPaul Burton
clear_fpu_owner(void)2899ec55930SPaul Burton static inline void clear_fpu_owner(void)
2909ec55930SPaul Burton {
2919ec55930SPaul Burton /* no-op */
2929ec55930SPaul Burton }
2939ec55930SPaul Burton
own_fpu_inatomic(int restore)2949ec55930SPaul Burton static inline int own_fpu_inatomic(int restore)
2959ec55930SPaul Burton {
2969ec55930SPaul Burton return SIGILL;
2979ec55930SPaul Burton }
2989ec55930SPaul Burton
own_fpu(int restore)2999ec55930SPaul Burton static inline int own_fpu(int restore)
3009ec55930SPaul Burton {
3019ec55930SPaul Burton return SIGILL;
3029ec55930SPaul Burton }
3039ec55930SPaul Burton
lose_fpu_inatomic(int save,struct task_struct * tsk)3049ec55930SPaul Burton static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
3059ec55930SPaul Burton {
3069ec55930SPaul Burton /* no-op */
3079ec55930SPaul Burton }
3089ec55930SPaul Burton
lose_fpu(int save)3099ec55930SPaul Burton static inline void lose_fpu(int save)
3109ec55930SPaul Burton {
3119ec55930SPaul Burton /* no-op */
3129ec55930SPaul Burton }
3139ec55930SPaul Burton
init_fp_ctx(struct task_struct * target)3149ec55930SPaul Burton static inline bool init_fp_ctx(struct task_struct *target)
3159ec55930SPaul Burton {
3169ec55930SPaul Burton return false;
3179ec55930SPaul Burton }
3189ec55930SPaul Burton
3199ec55930SPaul Burton /*
3209ec55930SPaul Burton * The following functions should only be called in paths where we know that FP
3219ec55930SPaul Burton * support is enabled, typically a path where own_fpu() or __enable_fpu() have
3229ec55930SPaul Burton * returned successfully. When CONFIG_MIPS_FP_SUPPORT=n it is known at compile
3239ec55930SPaul Burton * time that this should never happen, so calls to these functions should be
3249ec55930SPaul Burton * optimized away & never actually be emitted.
3259ec55930SPaul Burton */
3269ec55930SPaul Burton
3279ec55930SPaul Burton extern void save_fp(struct task_struct *tsk)
3289ec55930SPaul Burton __compiletime_error("save_fp() should not be called when CONFIG_MIPS_FP_SUPPORT=n");
3299ec55930SPaul Burton
3309ec55930SPaul Burton extern void _save_fp(struct task_struct *)
3319ec55930SPaul Burton __compiletime_error("_save_fp() should not be called when CONFIG_MIPS_FP_SUPPORT=n");
3329ec55930SPaul Burton
3339ec55930SPaul Burton extern void restore_fp(struct task_struct *tsk)
3349ec55930SPaul Burton __compiletime_error("restore_fp() should not be called when CONFIG_MIPS_FP_SUPPORT=n");
3359ec55930SPaul Burton
3369ec55930SPaul Burton extern void _restore_fp(struct task_struct *)
3379ec55930SPaul Burton __compiletime_error("_restore_fp() should not be called when CONFIG_MIPS_FP_SUPPORT=n");
3389ec55930SPaul Burton
3399ec55930SPaul Burton extern union fpureg *get_fpu_regs(struct task_struct *tsk)
3409ec55930SPaul Burton __compiletime_error("get_fpu_regs() should not be called when CONFIG_MIPS_FP_SUPPORT=n");
3419ec55930SPaul Burton
3429ec55930SPaul Burton #endif /* !CONFIG_MIPS_FP_SUPPORT */
343384740dcSRalf Baechle #endif /* _ASM_FPU_H */
344