vector.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2020 SiFive
  4. */
  5. #ifndef __ASM_RISCV_VECTOR_H
  6. #define __ASM_RISCV_VECTOR_H
  7. #include <linux/types.h>
  8. #include <uapi/asm-generic/errno.h>
  9. #ifdef CONFIG_RISCV_ISA_V
  10. #include <linux/stringify.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/task_stack.h>
  13. #include <asm/ptrace.h>
  14. #include <asm/cpufeature.h>
  15. #include <asm/csr.h>
  16. #include <asm/asm.h>
  17. extern unsigned long riscv_v_vsize;
  18. int riscv_v_setup_vsize(void);
  19. bool riscv_v_first_use_handler(struct pt_regs *regs);
  20. void kernel_vector_begin(void);
  21. void kernel_vector_end(void);
  22. void get_cpu_vector_context(void);
  23. void put_cpu_vector_context(void);
  24. void riscv_v_thread_free(struct task_struct *tsk);
  25. void __init riscv_v_setup_ctx_cache(void);
  26. void riscv_v_thread_alloc(struct task_struct *tsk);
  27. static inline u32 riscv_v_flags(void)
  28. {
  29. return READ_ONCE(current->thread.riscv_v_flags);
  30. }
  31. static __always_inline bool has_vector(void)
  32. {
  33. return riscv_has_extension_unlikely(RISCV_ISA_EXT_ZVE32X);
  34. }
  35. static inline void __riscv_v_vstate_clean(struct pt_regs *regs)
  36. {
  37. regs->status = (regs->status & ~SR_VS) | SR_VS_CLEAN;
  38. }
  39. static inline void __riscv_v_vstate_dirty(struct pt_regs *regs)
  40. {
  41. regs->status = (regs->status & ~SR_VS) | SR_VS_DIRTY;
  42. }
  43. static inline void riscv_v_vstate_off(struct pt_regs *regs)
  44. {
  45. regs->status = (regs->status & ~SR_VS) | SR_VS_OFF;
  46. }
  47. static inline void riscv_v_vstate_on(struct pt_regs *regs)
  48. {
  49. regs->status = (regs->status & ~SR_VS) | SR_VS_INITIAL;
  50. }
  51. static inline bool riscv_v_vstate_query(struct pt_regs *regs)
  52. {
  53. return (regs->status & SR_VS) != 0;
  54. }
  55. static __always_inline void riscv_v_enable(void)
  56. {
  57. csr_set(CSR_SSTATUS, SR_VS);
  58. }
  59. static __always_inline void riscv_v_disable(void)
  60. {
  61. csr_clear(CSR_SSTATUS, SR_VS);
  62. }
  63. static __always_inline void __vstate_csr_save(struct __riscv_v_ext_state *dest)
  64. {
  65. asm volatile (
  66. "csrr %0, " __stringify(CSR_VSTART) "\n\t"
  67. "csrr %1, " __stringify(CSR_VTYPE) "\n\t"
  68. "csrr %2, " __stringify(CSR_VL) "\n\t"
  69. "csrr %3, " __stringify(CSR_VCSR) "\n\t"
  70. "csrr %4, " __stringify(CSR_VLENB) "\n\t"
  71. : "=r" (dest->vstart), "=r" (dest->vtype), "=r" (dest->vl),
  72. "=r" (dest->vcsr), "=r" (dest->vlenb) : :);
  73. }
  74. static __always_inline void __vstate_csr_restore(struct __riscv_v_ext_state *src)
  75. {
  76. asm volatile (
  77. ".option push\n\t"
  78. ".option arch, +zve32x\n\t"
  79. "vsetvl x0, %2, %1\n\t"
  80. ".option pop\n\t"
  81. "csrw " __stringify(CSR_VSTART) ", %0\n\t"
  82. "csrw " __stringify(CSR_VCSR) ", %3\n\t"
  83. : : "r" (src->vstart), "r" (src->vtype), "r" (src->vl),
  84. "r" (src->vcsr) :);
  85. }
  86. static inline void __riscv_v_vstate_save(struct __riscv_v_ext_state *save_to,
  87. void *datap)
  88. {
  89. unsigned long vl;
  90. riscv_v_enable();
  91. __vstate_csr_save(save_to);
  92. asm volatile (
  93. ".option push\n\t"
  94. ".option arch, +zve32x\n\t"
  95. "vsetvli %0, x0, e8, m8, ta, ma\n\t"
  96. "vse8.v v0, (%1)\n\t"
  97. "add %1, %1, %0\n\t"
  98. "vse8.v v8, (%1)\n\t"
  99. "add %1, %1, %0\n\t"
  100. "vse8.v v16, (%1)\n\t"
  101. "add %1, %1, %0\n\t"
  102. "vse8.v v24, (%1)\n\t"
  103. ".option pop\n\t"
  104. : "=&r" (vl) : "r" (datap) : "memory");
  105. riscv_v_disable();
  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. riscv_v_enable();
  112. asm volatile (
  113. ".option push\n\t"
  114. ".option arch, +zve32x\n\t"
  115. "vsetvli %0, x0, e8, m8, ta, ma\n\t"
  116. "vle8.v v0, (%1)\n\t"
  117. "add %1, %1, %0\n\t"
  118. "vle8.v v8, (%1)\n\t"
  119. "add %1, %1, %0\n\t"
  120. "vle8.v v16, (%1)\n\t"
  121. "add %1, %1, %0\n\t"
  122. "vle8.v v24, (%1)\n\t"
  123. ".option pop\n\t"
  124. : "=&r" (vl) : "r" (datap) : "memory");
  125. __vstate_csr_restore(restore_from);
  126. riscv_v_disable();
  127. }
  128. static inline void __riscv_v_vstate_discard(void)
  129. {
  130. unsigned long vl, vtype_inval = 1UL << (BITS_PER_LONG - 1);
  131. riscv_v_enable();
  132. asm volatile (
  133. ".option push\n\t"
  134. ".option arch, +zve32x\n\t"
  135. "vsetvli %0, x0, e8, m8, ta, ma\n\t"
  136. "vmv.v.i v0, -1\n\t"
  137. "vmv.v.i v8, -1\n\t"
  138. "vmv.v.i v16, -1\n\t"
  139. "vmv.v.i v24, -1\n\t"
  140. "vsetvl %0, x0, %1\n\t"
  141. ".option pop\n\t"
  142. : "=&r" (vl) : "r" (vtype_inval) : "memory");
  143. riscv_v_disable();
  144. }
  145. static inline void riscv_v_vstate_discard(struct pt_regs *regs)
  146. {
  147. if ((regs->status & SR_VS) == SR_VS_OFF)
  148. return;
  149. __riscv_v_vstate_discard();
  150. __riscv_v_vstate_dirty(regs);
  151. }
  152. static inline void riscv_v_vstate_save(struct __riscv_v_ext_state *vstate,
  153. struct pt_regs *regs)
  154. {
  155. if ((regs->status & SR_VS) == SR_VS_DIRTY) {
  156. __riscv_v_vstate_save(vstate, vstate->datap);
  157. __riscv_v_vstate_clean(regs);
  158. }
  159. }
  160. static inline void riscv_v_vstate_restore(struct __riscv_v_ext_state *vstate,
  161. struct pt_regs *regs)
  162. {
  163. if ((regs->status & SR_VS) != SR_VS_OFF) {
  164. __riscv_v_vstate_restore(vstate, vstate->datap);
  165. __riscv_v_vstate_clean(regs);
  166. }
  167. }
  168. static inline void riscv_v_vstate_set_restore(struct task_struct *task,
  169. struct pt_regs *regs)
  170. {
  171. if ((regs->status & SR_VS) != SR_VS_OFF) {
  172. set_tsk_thread_flag(task, TIF_RISCV_V_DEFER_RESTORE);
  173. riscv_v_vstate_on(regs);
  174. }
  175. }
  176. #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE
  177. static inline bool riscv_preempt_v_dirty(struct task_struct *task)
  178. {
  179. return !!(task->thread.riscv_v_flags & RISCV_PREEMPT_V_DIRTY);
  180. }
  181. static inline bool riscv_preempt_v_restore(struct task_struct *task)
  182. {
  183. return !!(task->thread.riscv_v_flags & RISCV_PREEMPT_V_NEED_RESTORE);
  184. }
  185. static inline void riscv_preempt_v_clear_dirty(struct task_struct *task)
  186. {
  187. barrier();
  188. task->thread.riscv_v_flags &= ~RISCV_PREEMPT_V_DIRTY;
  189. }
  190. static inline void riscv_preempt_v_set_restore(struct task_struct *task)
  191. {
  192. barrier();
  193. task->thread.riscv_v_flags |= RISCV_PREEMPT_V_NEED_RESTORE;
  194. }
  195. static inline bool riscv_preempt_v_started(struct task_struct *task)
  196. {
  197. return !!(task->thread.riscv_v_flags & RISCV_PREEMPT_V);
  198. }
  199. #else /* !CONFIG_RISCV_ISA_V_PREEMPTIVE */
  200. static inline bool riscv_preempt_v_dirty(struct task_struct *task) { return false; }
  201. static inline bool riscv_preempt_v_restore(struct task_struct *task) { return false; }
  202. static inline bool riscv_preempt_v_started(struct task_struct *task) { return false; }
  203. #define riscv_preempt_v_clear_dirty(tsk) do {} while (0)
  204. #define riscv_preempt_v_set_restore(tsk) do {} while (0)
  205. #endif /* CONFIG_RISCV_ISA_V_PREEMPTIVE */
  206. static inline void __switch_to_vector(struct task_struct *prev,
  207. struct task_struct *next)
  208. {
  209. struct pt_regs *regs;
  210. if (riscv_preempt_v_started(prev)) {
  211. if (riscv_preempt_v_dirty(prev)) {
  212. __riscv_v_vstate_save(&prev->thread.kernel_vstate,
  213. prev->thread.kernel_vstate.datap);
  214. riscv_preempt_v_clear_dirty(prev);
  215. }
  216. } else {
  217. regs = task_pt_regs(prev);
  218. riscv_v_vstate_save(&prev->thread.vstate, regs);
  219. }
  220. if (riscv_preempt_v_started(next))
  221. riscv_preempt_v_set_restore(next);
  222. else
  223. riscv_v_vstate_set_restore(next, task_pt_regs(next));
  224. }
  225. void riscv_v_vstate_ctrl_init(struct task_struct *tsk);
  226. bool riscv_v_vstate_ctrl_user_allowed(void);
  227. #else /* ! CONFIG_RISCV_ISA_V */
  228. struct pt_regs;
  229. static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; }
  230. static __always_inline bool has_vector(void) { return false; }
  231. static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; }
  232. static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; }
  233. static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; }
  234. #define riscv_v_vsize (0)
  235. #define riscv_v_vstate_discard(regs) do {} while (0)
  236. #define riscv_v_vstate_save(vstate, regs) do {} while (0)
  237. #define riscv_v_vstate_restore(vstate, regs) do {} while (0)
  238. #define __switch_to_vector(__prev, __next) do {} while (0)
  239. #define riscv_v_vstate_off(regs) do {} while (0)
  240. #define riscv_v_vstate_on(regs) do {} while (0)
  241. #define riscv_v_thread_free(tsk) do {} while (0)
  242. #define riscv_v_setup_ctx_cache() do {} while (0)
  243. #define riscv_v_thread_alloc(tsk) do {} while (0)
  244. #endif /* CONFIG_RISCV_ISA_V */
  245. /*
  246. * Return the implementation's vlen value.
  247. *
  248. * riscv_v_vsize contains the value of "32 vector registers with vlenb length"
  249. * so rebuild the vlen value in bits from it.
  250. */
  251. static inline int riscv_vector_vlen(void)
  252. {
  253. return riscv_v_vsize / 32 * 8;
  254. }
  255. #endif /* ! __ASM_RISCV_VECTOR_H */