rvv_context.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-10-10 RT-Thread the first version,
  9. * compatible to riscv-v-spec-1.0
  10. */
  11. #ifndef __RVV_CONTEXT_H__
  12. #define __RVV_CONTEXT_H__
  13. #include "cpuport.h"
  14. #include "encoding.h"
  15. #if defined(ARCH_VECTOR_VLEN_128)
  16. #define CTX_VECTOR_REGS 64
  17. #elif defined(ARCH_VECTOR_VLEN_256)
  18. #define CTX_VECTOR_REGS 128
  19. #else
  20. #error "No supported VLEN"
  21. #endif /* VLEN */
  22. #define CTX_VECTOR_REG_NR (CTX_VECTOR_REGS + 4)
  23. /**
  24. * ==================================
  25. * VECTOR EXTENSION
  26. * ==================================
  27. */
  28. #define VEC_FRAME_VSTART (0 * REGBYTES)
  29. #define VEC_FRAME_VTYPE (1 * REGBYTES)
  30. #define VEC_FRAME_VL (2 * REGBYTES)
  31. #define VEC_FRAME_VCSR (3 * REGBYTES)
  32. #define VEC_FRAME_V0 (4 * REGBYTES)
  33. .macro GET_VEC_FRAME_LEN, xreg
  34. csrr \xreg, vlenb
  35. slli \xreg, \xreg, 5
  36. addi \xreg, \xreg, 4 * REGBYTES
  37. .endm
  38. /**
  39. * @brief save vector extension hardware state
  40. *
  41. * @param dst register storing bottom of storage block
  42. *
  43. */
  44. .macro SAVE_VECTOR, dst
  45. mv t1, \dst
  46. csrr t0, vstart
  47. STORE t0, VEC_FRAME_VSTART(t1)
  48. csrr t0, vtype
  49. STORE t0, VEC_FRAME_VTYPE(t1)
  50. csrr t0, vl
  51. STORE t0, VEC_FRAME_VL(t1)
  52. csrr t0, vcsr
  53. STORE t0, VEC_FRAME_VCSR(t1)
  54. addi t1, t1, VEC_FRAME_V0
  55. // config vector setting,
  56. // t2 is updated to length of a vector group in bytes
  57. VEC_CONFIG_SETVLI(t2, x0, VEC_IMM_SEW_8, VEC_IMM_LMUL_8)
  58. vse8.v v0, (t1)
  59. add t1, t1, t2
  60. vse8.v v8, (t1)
  61. add t1, t1, t2
  62. vse8.v v16, (t1)
  63. add t1, t1, t2
  64. vse8.v v24, (t1)
  65. .endm
  66. /**
  67. * @brief restore vector extension hardware states
  68. *
  69. * @param dst register storing bottom of storage block
  70. *
  71. */
  72. .macro RESTORE_VECTOR, dst
  73. // restore vector registers first since it will modify vector states
  74. mv t0, \dst
  75. addi t1, t0, VEC_FRAME_V0
  76. VEC_CONFIG_SETVLI(t2, x0, VEC_IMM_SEW_8, VEC_IMM_LMUL_8)
  77. vle8.v v0, (t1)
  78. add t1, t1, t2
  79. vle8.v v8, (t1)
  80. add t1, t1, t2
  81. vle8.v v16, (t1)
  82. add t1, t1, t2
  83. vle8.v v24, (t1)
  84. mv t1, t0
  85. LOAD t0, VEC_FRAME_VSTART(t1)
  86. csrw vstart, t0
  87. LOAD t0, VEC_FRAME_VCSR(t1)
  88. csrw vcsr, t0
  89. LOAD t0, VEC_FRAME_VTYPE(t1)
  90. LOAD t3, VEC_FRAME_VL(t1)
  91. VEC_CONFIG_SET_VL_VTYPE(t3, t0)
  92. .endm
  93. #endif /* __RVV_CONTEXT_H__ */