xref: /openbmc/linux/arch/riscv/include/asm/switch_to.h (revision 1edd0337)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5 
6 #ifndef _ASM_RISCV_SWITCH_TO_H
7 #define _ASM_RISCV_SWITCH_TO_H
8 
9 #include <linux/jump_label.h>
10 #include <linux/sched/task_stack.h>
11 #include <asm/hwcap.h>
12 #include <asm/processor.h>
13 #include <asm/ptrace.h>
14 #include <asm/csr.h>
15 
16 #ifdef CONFIG_FPU
17 extern void __fstate_save(struct task_struct *save_to);
18 extern void __fstate_restore(struct task_struct *restore_from);
19 
20 static inline void __fstate_clean(struct pt_regs *regs)
21 {
22 	regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
23 }
24 
25 static inline void fstate_off(struct task_struct *task,
26 			      struct pt_regs *regs)
27 {
28 	regs->status = (regs->status & ~SR_FS) | SR_FS_OFF;
29 }
30 
31 static inline void fstate_save(struct task_struct *task,
32 			       struct pt_regs *regs)
33 {
34 	if ((regs->status & SR_FS) == SR_FS_DIRTY) {
35 		__fstate_save(task);
36 		__fstate_clean(regs);
37 	}
38 }
39 
40 static inline void fstate_restore(struct task_struct *task,
41 				  struct pt_regs *regs)
42 {
43 	if ((regs->status & SR_FS) != SR_FS_OFF) {
44 		__fstate_restore(task);
45 		__fstate_clean(regs);
46 	}
47 }
48 
49 static inline void __switch_to_aux(struct task_struct *prev,
50 				   struct task_struct *next)
51 {
52 	struct pt_regs *regs;
53 
54 	regs = task_pt_regs(prev);
55 	if (unlikely(regs->status & SR_SD))
56 		fstate_save(prev, regs);
57 	fstate_restore(next, task_pt_regs(next));
58 }
59 
60 static __always_inline bool has_fpu(void)
61 {
62 	return riscv_has_extension_likely(RISCV_ISA_EXT_f) ||
63 		riscv_has_extension_likely(RISCV_ISA_EXT_d);
64 }
65 #else
66 static __always_inline bool has_fpu(void) { return false; }
67 #define fstate_save(task, regs) do { } while (0)
68 #define fstate_restore(task, regs) do { } while (0)
69 #define __switch_to_aux(__prev, __next) do { } while (0)
70 #endif
71 
72 extern struct task_struct *__switch_to(struct task_struct *,
73 				       struct task_struct *);
74 
75 #define switch_to(prev, next, last)			\
76 do {							\
77 	struct task_struct *__prev = (prev);		\
78 	struct task_struct *__next = (next);		\
79 	if (has_fpu())					\
80 		__switch_to_aux(__prev, __next);	\
81 	((last) = __switch_to(__prev, __next));		\
82 } while (0)
83 
84 #endif /* _ASM_RISCV_SWITCH_TO_H */
85