xref: /openbmc/linux/arch/loongarch/include/asm/fpu.h (revision 3def3387)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Author: Huacai Chen <chenhuacai@loongson.cn>
4  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
5  */
6 #ifndef _ASM_FPU_H
7 #define _ASM_FPU_H
8 
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>
14 
15 #include <asm/cpu.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>
21 
22 struct sigcontext;
23 
24 extern void kernel_fpu_begin(void);
25 extern void kernel_fpu_end(void);
26 
27 extern void _init_fpu(unsigned int);
28 extern void _save_fp(struct loongarch_fpu *);
29 extern void _restore_fp(struct loongarch_fpu *);
30 
31 /*
32  * Mask the FCSR Cause bits according to the Enable bits, observing
33  * that Unimplemented is always enabled.
34  */
35 static inline unsigned long mask_fcsr_x(unsigned long fcsr)
36 {
37 	return fcsr & ((fcsr & FPU_CSR_ALL_E) <<
38 			(ffs(FPU_CSR_ALL_X) - ffs(FPU_CSR_ALL_E)));
39 }
40 
41 static inline int is_fp_enabled(void)
42 {
43 	return (csr_read32(LOONGARCH_CSR_EUEN) & CSR_EUEN_FPEN) ?
44 		1 : 0;
45 }
46 
47 #define enable_fpu()		set_csr_euen(CSR_EUEN_FPEN)
48 
49 #define disable_fpu()		clear_csr_euen(CSR_EUEN_FPEN)
50 
51 #define clear_fpu_owner()	clear_thread_flag(TIF_USEDFPU)
52 
53 static inline int is_fpu_owner(void)
54 {
55 	return test_thread_flag(TIF_USEDFPU);
56 }
57 
58 static inline void __own_fpu(void)
59 {
60 	enable_fpu();
61 	set_thread_flag(TIF_USEDFPU);
62 	KSTK_EUEN(current) |= CSR_EUEN_FPEN;
63 }
64 
65 static inline void own_fpu_inatomic(int restore)
66 {
67 	if (cpu_has_fpu && !is_fpu_owner()) {
68 		__own_fpu();
69 		if (restore)
70 			_restore_fp(&current->thread.fpu);
71 	}
72 }
73 
74 static inline void own_fpu(int restore)
75 {
76 	preempt_disable();
77 	own_fpu_inatomic(restore);
78 	preempt_enable();
79 }
80 
81 static inline void lose_fpu_inatomic(int save, struct task_struct *tsk)
82 {
83 	if (is_fpu_owner()) {
84 		if (save)
85 			_save_fp(&tsk->thread.fpu);
86 		disable_fpu();
87 		clear_tsk_thread_flag(tsk, TIF_USEDFPU);
88 	}
89 	KSTK_EUEN(tsk) &= ~(CSR_EUEN_FPEN | CSR_EUEN_LSXEN | CSR_EUEN_LASXEN);
90 }
91 
92 static inline void lose_fpu(int save)
93 {
94 	preempt_disable();
95 	lose_fpu_inatomic(save, current);
96 	preempt_enable();
97 }
98 
99 static inline void init_fpu(void)
100 {
101 	unsigned int fcsr = current->thread.fpu.fcsr;
102 
103 	__own_fpu();
104 	_init_fpu(fcsr);
105 	set_used_math();
106 }
107 
108 static inline void save_fp(struct task_struct *tsk)
109 {
110 	if (cpu_has_fpu)
111 		_save_fp(&tsk->thread.fpu);
112 }
113 
114 static inline void restore_fp(struct task_struct *tsk)
115 {
116 	if (cpu_has_fpu)
117 		_restore_fp(&tsk->thread.fpu);
118 }
119 
120 static inline union fpureg *get_fpu_regs(struct task_struct *tsk)
121 {
122 	if (tsk == current) {
123 		preempt_disable();
124 		if (is_fpu_owner())
125 			_save_fp(&current->thread.fpu);
126 		preempt_enable();
127 	}
128 
129 	return tsk->thread.fpu.fpr;
130 }
131 
132 #endif /* _ASM_FPU_H */
133