riscv: switch has_fpu() to the unified static key mechanism
[linux-2.6-microblaze.git] / arch / riscv / include / asm / switch_to.h
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 static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_FPU]);
63 }
64 #else
65 static __always_inline bool has_fpu(void) { return false; }
66 #define fstate_save(task, regs) do { } while (0)
67 #define fstate_restore(task, regs) do { } while (0)
68 #define __switch_to_aux(__prev, __next) do { } while (0)
69 #endif
70
71 extern struct task_struct *__switch_to(struct task_struct *,
72                                        struct task_struct *);
73
74 #define switch_to(prev, next, last)                     \
75 do {                                                    \
76         struct task_struct *__prev = (prev);            \
77         struct task_struct *__next = (next);            \
78         if (has_fpu())                                  \
79                 __switch_to_aux(__prev, __next);        \
80         ((last) = __switch_to(__prev, __next));         \
81 } while (0)
82
83 #endif /* _ASM_RISCV_SWITCH_TO_H */