xref: /openbmc/linux/arch/riscv/include/asm/switch_to.h (revision 62e59c4e)
1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13 
14 #ifndef _ASM_RISCV_SWITCH_TO_H
15 #define _ASM_RISCV_SWITCH_TO_H
16 
17 #include <asm/processor.h>
18 #include <asm/ptrace.h>
19 #include <asm/csr.h>
20 
21 #ifdef CONFIG_FPU
22 extern void __fstate_save(struct task_struct *save_to);
23 extern void __fstate_restore(struct task_struct *restore_from);
24 
25 static inline void __fstate_clean(struct pt_regs *regs)
26 {
27 	regs->sstatus |= (regs->sstatus & ~(SR_FS)) | SR_FS_CLEAN;
28 }
29 
30 static inline void fstate_save(struct task_struct *task,
31 			       struct pt_regs *regs)
32 {
33 	if ((regs->sstatus & SR_FS) == SR_FS_DIRTY) {
34 		__fstate_save(task);
35 		__fstate_clean(regs);
36 	}
37 }
38 
39 static inline void fstate_restore(struct task_struct *task,
40 				  struct pt_regs *regs)
41 {
42 	if ((regs->sstatus & SR_FS) != SR_FS_OFF) {
43 		__fstate_restore(task);
44 		__fstate_clean(regs);
45 	}
46 }
47 
48 static inline void __switch_to_aux(struct task_struct *prev,
49 				   struct task_struct *next)
50 {
51 	struct pt_regs *regs;
52 
53 	regs = task_pt_regs(prev);
54 	if (unlikely(regs->sstatus & SR_SD))
55 		fstate_save(prev, regs);
56 	fstate_restore(next, task_pt_regs(next));
57 }
58 
59 extern bool has_fpu;
60 #else
61 #define has_fpu false
62 #define fstate_save(task, regs) do { } while (0)
63 #define fstate_restore(task, regs) do { } while (0)
64 #define __switch_to_aux(__prev, __next) do { } while (0)
65 #endif
66 
67 extern struct task_struct *__switch_to(struct task_struct *,
68 				       struct task_struct *);
69 
70 #define switch_to(prev, next, last)			\
71 do {							\
72 	struct task_struct *__prev = (prev);		\
73 	struct task_struct *__next = (next);		\
74 	if (has_fpu)					\
75 		__switch_to_aux(__prev, __next);	\
76 	((last) = __switch_to(__prev, __next));		\
77 } while (0)
78 
79 #endif /* _ASM_RISCV_SWITCH_TO_H */
80