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/sched/task_stack.h>
10 #include <asm/processor.h>
11 #include <asm/ptrace.h>
12 #include <asm/csr.h>
13
14 #ifdef CONFIG_FPU
15 extern void __fstate_save(struct task_struct *save_to);
16 extern void __fstate_restore(struct task_struct *restore_from);
17
__fstate_clean(struct pt_regs * regs)18 static inline void __fstate_clean(struct pt_regs *regs)
19 {
20 regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
21 }
22
fstate_off(struct task_struct * task,struct pt_regs * regs)23 static inline void fstate_off(struct task_struct *task,
24 struct pt_regs *regs)
25 {
26 regs->status = (regs->status & ~SR_FS) | SR_FS_OFF;
27 }
28
fstate_save(struct task_struct * task,struct pt_regs * regs)29 static inline void fstate_save(struct task_struct *task,
30 struct pt_regs *regs)
31 {
32 if ((regs->status & SR_FS) == SR_FS_DIRTY) {
33 __fstate_save(task);
34 __fstate_clean(regs);
35 }
36 }
37
fstate_restore(struct task_struct * task,struct pt_regs * regs)38 static inline void fstate_restore(struct task_struct *task,
39 struct pt_regs *regs)
40 {
41 if ((regs->status & SR_FS) != SR_FS_OFF) {
42 __fstate_restore(task);
43 __fstate_clean(regs);
44 }
45 }
46
__switch_to_aux(struct task_struct * prev,struct task_struct * next)47 static inline void __switch_to_aux(struct task_struct *prev,
48 struct task_struct *next)
49 {
50 struct pt_regs *regs;
51
52 regs = task_pt_regs(prev);
53 if (unlikely(regs->status & SR_SD))
54 fstate_save(prev, regs);
55 fstate_restore(next, task_pt_regs(next));
56 }
57
58 extern bool has_fpu;
59 #else
60 #define has_fpu false
61 #define fstate_save(task, regs) do { } while (0)
62 #define fstate_restore(task, regs) do { } while (0)
63 #define __switch_to_aux(__prev, __next) do { } while (0)
64 #endif
65
66 extern struct task_struct *__switch_to(struct task_struct *,
67 struct task_struct *);
68
69 #define switch_to(prev, next, last) \
70 do { \
71 struct task_struct *__prev = (prev); \
72 struct task_struct *__next = (next); \
73 if (has_fpu) \
74 __switch_to_aux(__prev, __next); \
75 ((last) = __switch_to(__prev, __next)); \
76 } while (0)
77
78 #endif /* _ASM_RISCV_SWITCH_TO_H */
79