1
0

ext_context.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2022, 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 __EXT_CONTEXT_H__
  12. #define __EXT_CONTEXT_H__
  13. #ifdef __ASSEMBLY__
  14. /**
  15. * extension context maintenance
  16. */
  17. #include "cpuport.h"
  18. #include "encoding.h"
  19. #include "vector_encoding.h"
  20. /**
  21. * ==================================
  22. * FPU EXTENSION
  23. * ==================================
  24. */
  25. #ifdef ENABLE_FPU
  26. #define FPU_CTX_F0_OFF 0 /* offsetof(fpu_context_t, fpustatus.f[0]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  27. #define FPU_CTX_F1_OFF 8 /* offsetof(fpu_context_t, fpustatus.f[1]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  28. #define FPU_CTX_F2_OFF 16 /* offsetof(fpu_context_t, fpustatus.f[2]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  29. #define FPU_CTX_F3_OFF 24 /* offsetof(fpu_context_t, fpustatus.f[3]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  30. #define FPU_CTX_F4_OFF 32 /* offsetof(fpu_context_t, fpustatus.f[4]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  31. #define FPU_CTX_F5_OFF 40 /* offsetof(fpu_context_t, fpustatus.f[5]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  32. #define FPU_CTX_F6_OFF 48 /* offsetof(fpu_context_t, fpustatus.f[6]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  33. #define FPU_CTX_F7_OFF 56 /* offsetof(fpu_context_t, fpustatus.f[7]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  34. #define FPU_CTX_F8_OFF 64 /* offsetof(fpu_context_t, fpustatus.f[8]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  35. #define FPU_CTX_F9_OFF 72 /* offsetof(fpu_context_t, fpustatus.f[9]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  36. #define FPU_CTX_F10_OFF 80 /* offsetof(fpu_context_t, fpustatus.f[10]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  37. #define FPU_CTX_F11_OFF 88 /* offsetof(fpu_context_t, fpustatus.f[11]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  38. #define FPU_CTX_F12_OFF 96 /* offsetof(fpu_context_t, fpustatus.f[12]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  39. #define FPU_CTX_F13_OFF 104 /* offsetof(fpu_context_t, fpustatus.f[13]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  40. #define FPU_CTX_F14_OFF 112 /* offsetof(fpu_context_t, fpustatus.f[14]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  41. #define FPU_CTX_F15_OFF 120 /* offsetof(fpu_context_t, fpustatus.f[15]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  42. #define FPU_CTX_F16_OFF 128 /* offsetof(fpu_context_t, fpustatus.f[16]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  43. #define FPU_CTX_F17_OFF 136 /* offsetof(fpu_context_t, fpustatus.f[17]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  44. #define FPU_CTX_F18_OFF 144 /* offsetof(fpu_context_t, fpustatus.f[18]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  45. #define FPU_CTX_F19_OFF 152 /* offsetof(fpu_context_t, fpustatus.f[19]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  46. #define FPU_CTX_F20_OFF 160 /* offsetof(fpu_context_t, fpustatus.f[20]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  47. #define FPU_CTX_F21_OFF 168 /* offsetof(fpu_context_t, fpustatus.f[21]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  48. #define FPU_CTX_F22_OFF 176 /* offsetof(fpu_context_t, fpustatus.f[22]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  49. #define FPU_CTX_F23_OFF 184 /* offsetof(fpu_context_t, fpustatus.f[23]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  50. #define FPU_CTX_F24_OFF 192 /* offsetof(fpu_context_t, fpustatus.f[24]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  51. #define FPU_CTX_F25_OFF 200 /* offsetof(fpu_context_t, fpustatus.f[25]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  52. #define FPU_CTX_F26_OFF 208 /* offsetof(fpu_context_t, fpustatus.f[26]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  53. #define FPU_CTX_F27_OFF 216 /* offsetof(fpu_context_t, fpustatus.f[27]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  54. #define FPU_CTX_F28_OFF 224 /* offsetof(fpu_context_t, fpustatus.f[28]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  55. #define FPU_CTX_F29_OFF 232 /* offsetof(fpu_context_t, fpustatus.f[29]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  56. #define FPU_CTX_F30_OFF 240 /* offsetof(fpu_context_t, fpustatus.f[30]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  57. #define FPU_CTX_F31_OFF 248 /* offsetof(fpu_context_t, fpustatus.f[31]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  58. #endif /* ENABLE_FPU */
  59. /**
  60. * ==================================
  61. * VECTOR EXTENSION
  62. * ==================================
  63. */
  64. #ifdef ENABLE_VECTOR
  65. #define VEC_FRAME_VSTART (0 * REGBYTES)
  66. #define VEC_FRAME_VTYPE (1 * REGBYTES)
  67. #define VEC_FRAME_VL (2 * REGBYTES)
  68. #define VEC_FRAME_VCSR (3 * REGBYTES)
  69. #define VEC_FRAME_V0 (4 * REGBYTES)
  70. .macro GET_VEC_FRAME_LEN, xreg
  71. csrr \xreg, vlenb
  72. slli \xreg, \xreg, 5
  73. addi \xreg, \xreg, 4 * REGBYTES
  74. .endm
  75. /**
  76. * @brief save vector extension hardware state
  77. *
  78. * @param dst register storing bottom of storage block
  79. *
  80. */
  81. .macro SAVE_VECTOR, dst
  82. mv t1, \dst
  83. csrr t0, vstart
  84. STORE t0, VEC_FRAME_VSTART(t1)
  85. csrr t0, vtype
  86. STORE t0, VEC_FRAME_VTYPE(t1)
  87. csrr t0, vl
  88. STORE t0, VEC_FRAME_VL(t1)
  89. csrr t0, vcsr
  90. STORE t0, VEC_FRAME_VCSR(t1)
  91. addi t1, t1, VEC_FRAME_V0
  92. // config vector setting,
  93. // t2 is updated to length of a vector group in bytes
  94. VEC_CONFIG_SETVLI(t2, x0, VEC_IMM_SEW_8, VEC_IMM_LMUL_8)
  95. vse8.v v0, (t1)
  96. add t1, t1, t2
  97. vse8.v v8, (t1)
  98. add t1, t1, t2
  99. vse8.v v16, (t1)
  100. add t1, t1, t2
  101. vse8.v v24, (t1)
  102. .endm
  103. /**
  104. * @brief restore vector extension hardware states
  105. *
  106. * @param dst register storing bottom of storage block
  107. *
  108. */
  109. .macro RESTORE_VECTOR, dst
  110. // restore vector registers first since it will modify vector states
  111. mv t0, \dst
  112. addi t1, t0, VEC_FRAME_V0
  113. VEC_CONFIG_SETVLI(t2, x0, VEC_IMM_SEW_8, VEC_IMM_LMUL_8)
  114. vle8.v v0, (t1)
  115. add t1, t1, t2
  116. vle8.v v8, (t1)
  117. add t1, t1, t2
  118. vle8.v v16, (t1)
  119. add t1, t1, t2
  120. vle8.v v24, (t1)
  121. mv t1, t0
  122. LOAD t0, VEC_FRAME_VSTART(t1)
  123. csrw vstart, t0
  124. LOAD t0, VEC_FRAME_VCSR(t1)
  125. csrw vcsr, t0
  126. LOAD t0, VEC_FRAME_VTYPE(t1)
  127. LOAD t3, VEC_FRAME_VL(t1)
  128. VEC_CONFIG_SET_VL_VTYPE(t3, t0)
  129. .endm
  130. #endif /* ENABLE_VECTOR */
  131. #endif /* __ASSEMBLY__ */
  132. #endif /* __EXT_CONTEXT_H__ */