1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
11 #include <linux/ptrace.h>
12 #include <linux/thread_info.h>
13 #include <linux/bitops.h>
16 #include <asm/cpu-features.h>
17 #include <asm/current.h>
18 #include <asm/loongarch.h>
19 #include <asm/processor.h>
20 #include <asm/ptrace.h>
24 extern void kernel_fpu_begin(void);
25 extern void kernel_fpu_end(void);
27 extern void _init_fpu(unsigned int);
28 extern void _save_fp(struct loongarch_fpu *);
29 extern void _restore_fp(struct loongarch_fpu *);
32 * Mask the FCSR Cause bits according to the Enable bits, observing
33 * that Unimplemented is always enabled.
35 static inline unsigned long mask_fcsr_x(unsigned long fcsr)
37 return fcsr & ((fcsr & FPU_CSR_ALL_E) <<
38 (ffs(FPU_CSR_ALL_X) - ffs(FPU_CSR_ALL_E)));
41 static inline int is_fp_enabled(void)
43 return (csr_read32(LOONGARCH_CSR_EUEN) & CSR_EUEN_FPEN) ?
47 #define enable_fpu() set_csr_euen(CSR_EUEN_FPEN)
49 #define disable_fpu() clear_csr_euen(CSR_EUEN_FPEN)
51 #define clear_fpu_owner() clear_thread_flag(TIF_USEDFPU)
53 static inline int is_fpu_owner(void)
55 return test_thread_flag(TIF_USEDFPU);
58 static inline void __own_fpu(void)
61 set_thread_flag(TIF_USEDFPU);
62 KSTK_EUEN(current) |= CSR_EUEN_FPEN;
65 static inline void own_fpu_inatomic(int restore)
67 if (cpu_has_fpu && !is_fpu_owner()) {
70 _restore_fp(¤t->thread.fpu);
74 static inline void own_fpu(int restore)
77 own_fpu_inatomic(restore);
81 static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
85 _save_fp(&tsk->thread.fpu);
87 clear_tsk_thread_flag(tsk, TIF_USEDFPU);
89 KSTK_EUEN(tsk) &= ~(CSR_EUEN_FPEN | CSR_EUEN_LSXEN | CSR_EUEN_LASXEN);
92 static inline void lose_fpu(int save)
95 lose_fpu_inatomic(save, current);
99 static inline void init_fpu(void)
101 unsigned int fcsr = current->thread.fpu.fcsr;
108 static inline void save_fp(struct task_struct *tsk)
111 _save_fp(&tsk->thread.fpu);
114 static inline void restore_fp(struct task_struct *tsk)
117 _restore_fp(&tsk->thread.fpu);
120 static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
122 if (tsk == current) {
125 _save_fp(¤t->thread.fpu);
129 return tsk->thread.fpu.fpr;
132 #endif /* _ASM_FPU_H */