rvv_context.h 2.5 KB

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