ce6a75e9cf624766b81133cb7f4ef0845f43a4b1
[platform/kernel/linux-starfive.git] / arch / riscv / include / asm / vector.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2020 SiFive
4  */
5
6 #ifndef __ASM_RISCV_VECTOR_H
7 #define __ASM_RISCV_VECTOR_H
8
9 #include <linux/types.h>
10 #include <uapi/asm-generic/errno.h>
11
12 #ifdef CONFIG_RISCV_ISA_V
13
14 #include <linux/stringify.h>
15 #include <linux/sched.h>
16 #include <linux/sched/task_stack.h>
17 #include <asm/ptrace.h>
18 #include <asm/hwcap.h>
19 #include <asm/csr.h>
20 #include <asm/asm.h>
21
22 extern unsigned long riscv_v_vsize;
23 int riscv_v_setup_vsize(void);
24
25 static __always_inline bool has_vector(void)
26 {
27         return riscv_has_extension_unlikely(RISCV_ISA_EXT_v);
28 }
29
30 static inline void __riscv_v_vstate_clean(struct pt_regs *regs)
31 {
32         regs->status = (regs->status & ~SR_VS) | SR_VS_CLEAN;
33 }
34
35 static inline void riscv_v_vstate_off(struct pt_regs *regs)
36 {
37         regs->status = (regs->status & ~SR_VS) | SR_VS_OFF;
38 }
39
40 static inline void riscv_v_vstate_on(struct pt_regs *regs)
41 {
42         regs->status = (regs->status & ~SR_VS) | SR_VS_INITIAL;
43 }
44
45 static inline bool riscv_v_vstate_query(struct pt_regs *regs)
46 {
47         return (regs->status & SR_VS) != 0;
48 }
49
50 static __always_inline void riscv_v_enable(void)
51 {
52         csr_set(CSR_SSTATUS, SR_VS);
53 }
54
55 static __always_inline void riscv_v_disable(void)
56 {
57         csr_clear(CSR_SSTATUS, SR_VS);
58 }
59
60 static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest)
61 {
62         asm volatile (
63                 "csrr   %0, " __stringify(CSR_VSTART) "\n\t"
64                 "csrr   %1, " __stringify(CSR_VTYPE) "\n\t"
65                 "csrr   %2, " __stringify(CSR_VL) "\n\t"
66                 "csrr   %3, " __stringify(CSR_VCSR) "\n\t"
67                 : "=r" (dest->vstart), "=r" (dest->vtype), "=r" (dest->vl),
68                   "=r" (dest->vcsr) : :);
69 }
70
71 static __always_inline void __vstate_csr_restore(struct __riscv_v_ext_state *src)
72 {
73         asm volatile (
74                 ".option push\n\t"
75                 ".option arch, +v\n\t"
76                 "vsetvl  x0, %2, %1\n\t"
77                 ".option pop\n\t"
78                 "csrw   " __stringify(CSR_VSTART) ", %0\n\t"
79                 "csrw   " __stringify(CSR_VCSR) ", %3\n\t"
80                 : : "r" (src->vstart), "r" (src->vtype), "r" (src->vl),
81                     "r" (src->vcsr) :);
82 }
83
84 static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to,
85                                          void *datap)
86 {
87         unsigned long vl;
88
89         riscv_v_enable();
90         __vstate_csr_save(save_to);
91         asm volatile (
92                 ".option push\n\t"
93                 ".option arch, +v\n\t"
94                 "vsetvli        %0, x0, e8, m8, ta, ma\n\t"
95                 "vse8.v         v0, (%1)\n\t"
96                 "add            %1, %1, %0\n\t"
97                 "vse8.v         v8, (%1)\n\t"
98                 "add            %1, %1, %0\n\t"
99                 "vse8.v         v16, (%1)\n\t"
100                 "add            %1, %1, %0\n\t"
101                 "vse8.v         v24, (%1)\n\t"
102                 ".option pop\n\t"
103                 : "=&r" (vl) : "r" (datap) : "memory");
104         riscv_v_disable();
105 }
106
107 static inline void __riscv_v_vstate_restore(struct __riscv_v_ext_state *restore_from,
108                                             void *datap)
109 {
110         unsigned long vl;
111
112         riscv_v_enable();
113         asm volatile (
114                 ".option push\n\t"
115                 ".option arch, +v\n\t"
116                 "vsetvli        %0, x0, e8, m8, ta, ma\n\t"
117                 "vle8.v         v0, (%1)\n\t"
118                 "add            %1, %1, %0\n\t"
119                 "vle8.v         v8, (%1)\n\t"
120                 "add            %1, %1, %0\n\t"
121                 "vle8.v         v16, (%1)\n\t"
122                 "add            %1, %1, %0\n\t"
123                 "vle8.v         v24, (%1)\n\t"
124                 ".option pop\n\t"
125                 : "=&r" (vl) : "r" (datap) : "memory");
126         __vstate_csr_restore(restore_from);
127         riscv_v_disable();
128 }
129
130 static inline void riscv_v_vstate_save(struct task_struct *task,
131                                        struct pt_regs *regs)
132 {
133         if ((regs->status & SR_VS) == SR_VS_DIRTY) {
134                 struct __riscv_v_ext_state *vstate = &task->thread.vstate;
135
136                 __riscv_v_vstate_save(vstate, vstate->datap);
137                 __riscv_v_vstate_clean(regs);
138         }
139 }
140
141 static inline void riscv_v_vstate_restore(struct task_struct *task,
142                                           struct pt_regs *regs)
143 {
144         if ((regs->status & SR_VS) != SR_VS_OFF) {
145                 struct __riscv_v_ext_state *vstate = &task->thread.vstate;
146
147                 __riscv_v_vstate_restore(vstate, vstate->datap);
148                 __riscv_v_vstate_clean(regs);
149         }
150 }
151
152 static inline void __switch_to_vector(struct task_struct *prev,
153                                       struct task_struct *next)
154 {
155         struct pt_regs *regs;
156
157         regs = task_pt_regs(prev);
158         riscv_v_vstate_save(prev, regs);
159         riscv_v_vstate_restore(next, task_pt_regs(next));
160 }
161
162 #else /* ! CONFIG_RISCV_ISA_V  */
163
164 struct pt_regs;
165
166 static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; }
167 static __always_inline bool has_vector(void) { return false; }
168 static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
169 #define riscv_v_vsize (0)
170 #define riscv_v_vstate_save(task, regs)         do {} while (0)
171 #define riscv_v_vstate_restore(task, regs)      do {} while (0)
172 #define __switch_to_vector(__prev, __next)      do {} while (0)
173 #define riscv_v_vstate_off(regs)                do {} while (0)
174 #define riscv_v_vstate_on(regs)                 do {} while (0)
175
176 #endif /* CONFIG_RISCV_ISA_V */
177
178 #endif /* ! __ASM_RISCV_VECTOR_H */