riscv_encoding.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #ifndef __RISCV_ENCODING_H__
  10. #define __RISCV_ENCODING_H__
  11. #include <sbi/sbi_const.h>
  12. /* clang-format off */
  13. #define MSTATUS_SIE _UL(0x00000002)
  14. #define MSTATUS_MIE _UL(0x00000008)
  15. #define MSTATUS_SPIE_SHIFT 5
  16. #define MSTATUS_SPIE (_UL(1) << MSTATUS_SPIE_SHIFT)
  17. #define MSTATUS_UBE _UL(0x00000040)
  18. #define MSTATUS_MPIE _UL(0x00000080)
  19. #define MSTATUS_SPP_SHIFT 8
  20. #define MSTATUS_SPP (_UL(1) << MSTATUS_SPP_SHIFT)
  21. #define MSTATUS_MPP_SHIFT 11
  22. #define MSTATUS_MPP (_UL(3) << MSTATUS_MPP_SHIFT)
  23. #define MSTATUS_FS _UL(0x00006000)
  24. #define MSTATUS_XS _UL(0x00018000)
  25. #define MSTATUS_VS _UL(0x01800000)
  26. #define MSTATUS_MPRV _UL(0x00020000)
  27. #define MSTATUS_SUM _UL(0x00040000)
  28. #define MSTATUS_MXR _UL(0x00080000)
  29. #define MSTATUS_TVM _UL(0x00100000)
  30. #define MSTATUS_TW _UL(0x00200000)
  31. #define MSTATUS_TSR _UL(0x00400000)
  32. #define MSTATUS32_SD _UL(0x80000000)
  33. #if __riscv_xlen == 64
  34. #define MSTATUS_UXL _ULL(0x0000000300000000)
  35. #define MSTATUS_SXL _ULL(0x0000000C00000000)
  36. #define MSTATUS_SBE _ULL(0x0000001000000000)
  37. #define MSTATUS_MBE _ULL(0x0000002000000000)
  38. #define MSTATUS_MPV _ULL(0x0000008000000000)
  39. #else
  40. #define MSTATUSH_SBE _UL(0x00000010)
  41. #define MSTATUSH_MBE _UL(0x00000020)
  42. #define MSTATUSH_MPV _UL(0x00000080)
  43. #endif
  44. #define MSTATUS32_SD _UL(0x80000000)
  45. #define MSTATUS64_SD _ULL(0x8000000000000000)
  46. #define SSTATUS_SIE MSTATUS_SIE
  47. #define SSTATUS_SPIE_SHIFT MSTATUS_SPIE_SHIFT
  48. #define SSTATUS_SPIE MSTATUS_SPIE
  49. #define SSTATUS_SPP_SHIFT MSTATUS_SPP_SHIFT
  50. #define SSTATUS_SPP MSTATUS_SPP
  51. #define SSTATUS_FS MSTATUS_FS
  52. #define SSTATUS_XS MSTATUS_XS
  53. #define SSTATUS_VS MSTATUS_VS
  54. #define SSTATUS_SUM MSTATUS_SUM
  55. #define SSTATUS_MXR MSTATUS_MXR
  56. #define SSTATUS32_SD MSTATUS32_SD
  57. #define SSTATUS64_UXL MSTATUS_UXL
  58. #define SSTATUS64_SD MSTATUS64_SD
  59. #if __riscv_xlen == 64
  60. #define HSTATUS_VSXL _UL(0x300000000)
  61. #define HSTATUS_VSXL_SHIFT 32
  62. #endif
  63. #define HSTATUS_VTSR _UL(0x00400000)
  64. #define HSTATUS_VTW _UL(0x00200000)
  65. #define HSTATUS_VTVM _UL(0x00100000)
  66. #define HSTATUS_VGEIN _UL(0x0003f000)
  67. #define HSTATUS_VGEIN_SHIFT 12
  68. #define HSTATUS_HU _UL(0x00000200)
  69. #define HSTATUS_SPVP _UL(0x00000100)
  70. #define HSTATUS_SPV _UL(0x00000080)
  71. #define HSTATUS_GVA _UL(0x00000040)
  72. #define HSTATUS_VSBE _UL(0x00000020)
  73. #define IRQ_S_SOFT 1
  74. #define IRQ_VS_SOFT 2
  75. #define IRQ_M_SOFT 3
  76. #define IRQ_S_TIMER 5
  77. #define IRQ_VS_TIMER 6
  78. #define IRQ_M_TIMER 7
  79. #define IRQ_S_EXT 9
  80. #define IRQ_VS_EXT 10
  81. #define IRQ_M_EXT 11
  82. #define IRQ_S_GEXT 12
  83. #define MIP_SSIP (_UL(1) << IRQ_S_SOFT)
  84. #define MIP_VSSIP (_UL(1) << IRQ_VS_SOFT)
  85. #define MIP_MSIP (_UL(1) << IRQ_M_SOFT)
  86. #define MIP_STIP (_UL(1) << IRQ_S_TIMER)
  87. #define MIP_VSTIP (_UL(1) << IRQ_VS_TIMER)
  88. #define MIP_MTIP (_UL(1) << IRQ_M_TIMER)
  89. #define MIP_SEIP (_UL(1) << IRQ_S_EXT)
  90. #define MIP_VSEIP (_UL(1) << IRQ_VS_EXT)
  91. #define MIP_MEIP (_UL(1) << IRQ_M_EXT)
  92. #define MIP_SGEIP (_UL(1) << IRQ_S_GEXT)
  93. #define SIP_SSIP MIP_SSIP
  94. #define SIP_STIP MIP_STIP
  95. #define PRV_U _UL(0)
  96. #define PRV_S _UL(1)
  97. #define PRV_M _UL(3)
  98. #define SATP32_MODE _UL(0x80000000)
  99. #define SATP32_ASID _UL(0x7FC00000)
  100. #define SATP32_PPN _UL(0x003FFFFF)
  101. #define SATP64_MODE _ULL(0xF000000000000000)
  102. #define SATP64_ASID _ULL(0x0FFFF00000000000)
  103. #define SATP64_PPN _ULL(0x00000FFFFFFFFFFF)
  104. #define SATP_MODE_OFF _UL(0)
  105. #define SATP_MODE_SV32 _UL(1)
  106. #define SATP_MODE_SV39 _UL(8)
  107. #define SATP_MODE_SV48 _UL(9)
  108. #define SATP_MODE_SV57 _UL(10)
  109. #define SATP_MODE_SV64 _UL(11)
  110. #define HGATP_MODE_OFF _UL(0)
  111. #define HGATP_MODE_SV32X4 _UL(1)
  112. #define HGATP_MODE_SV39X4 _UL(8)
  113. #define HGATP_MODE_SV48X4 _UL(9)
  114. #define HGATP32_MODE_SHIFT 31
  115. #define HGATP32_VMID_SHIFT 22
  116. #define HGATP32_VMID_MASK _UL(0x1FC00000)
  117. #define HGATP32_PPN _UL(0x003FFFFF)
  118. #define HGATP64_MODE_SHIFT 60
  119. #define HGATP64_VMID_SHIFT 44
  120. #define HGATP64_VMID_MASK _ULL(0x03FFF00000000000)
  121. #define HGATP64_PPN _ULL(0x00000FFFFFFFFFFF)
  122. #define PMP_R _UL(0x01)
  123. #define PMP_W _UL(0x02)
  124. #define PMP_X _UL(0x04)
  125. #define PMP_A _UL(0x18)
  126. #define PMP_A_TOR _UL(0x08)
  127. #define PMP_A_NA4 _UL(0x10)
  128. #define PMP_A_NAPOT _UL(0x18)
  129. #define PMP_L _UL(0x80)
  130. #define PMP_SHIFT 2
  131. #define PMP_COUNT 64
  132. #if __riscv_xlen == 64
  133. #define PMP_ADDR_MASK ((_ULL(0x1) << 54) - 1)
  134. #else
  135. #define PMP_ADDR_MASK _UL(0xFFFFFFFF)
  136. #endif
  137. #if __riscv_xlen == 64
  138. #define MSTATUS_SD MSTATUS64_SD
  139. #define SSTATUS_SD SSTATUS64_SD
  140. #define SATP_MODE SATP64_MODE
  141. #define HGATP_PPN HGATP64_PPN
  142. #define HGATP_VMID_SHIFT HGATP64_VMID_SHIFT
  143. #define HGATP_VMID_MASK HGATP64_VMID_MASK
  144. #define HGATP_MODE_SHIFT HGATP64_MODE_SHIFT
  145. #else
  146. #define MSTATUS_SD MSTATUS32_SD
  147. #define SSTATUS_SD SSTATUS32_SD
  148. #define SATP_MODE SATP32_MODE
  149. #define HGATP_PPN HGATP32_PPN
  150. #define HGATP_VMID_SHIFT HGATP32_VMID_SHIFT
  151. #define HGATP_VMID_MASK HGATP32_VMID_MASK
  152. #define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT
  153. #endif
  154. /* ===== User-level CSRs ===== */
  155. /* User Trap Setup (N-extension) */
  156. #define CSR_USTATUS 0x000
  157. #define CSR_UIE 0x004
  158. #define CSR_UTVEC 0x005
  159. /* User Trap Handling (N-extension) */
  160. #define CSR_USCRATCH 0x040
  161. #define CSR_UEPC 0x041
  162. #define CSR_UCAUSE 0x042
  163. #define CSR_UTVAL 0x043
  164. #define CSR_UIP 0x044
  165. /* User Floating-point CSRs */
  166. #define CSR_FFLAGS 0x001
  167. #define CSR_FRM 0x002
  168. #define CSR_FCSR 0x003
  169. /* User Counters/Timers */
  170. #define CSR_CYCLE 0xc00
  171. #define CSR_TIME 0xc01
  172. #define CSR_INSTRET 0xc02
  173. #define CSR_HPMCOUNTER3 0xc03
  174. #define CSR_HPMCOUNTER4 0xc04
  175. #define CSR_HPMCOUNTER5 0xc05
  176. #define CSR_HPMCOUNTER6 0xc06
  177. #define CSR_HPMCOUNTER7 0xc07
  178. #define CSR_HPMCOUNTER8 0xc08
  179. #define CSR_HPMCOUNTER9 0xc09
  180. #define CSR_HPMCOUNTER10 0xc0a
  181. #define CSR_HPMCOUNTER11 0xc0b
  182. #define CSR_HPMCOUNTER12 0xc0c
  183. #define CSR_HPMCOUNTER13 0xc0d
  184. #define CSR_HPMCOUNTER14 0xc0e
  185. #define CSR_HPMCOUNTER15 0xc0f
  186. #define CSR_HPMCOUNTER16 0xc10
  187. #define CSR_HPMCOUNTER17 0xc11
  188. #define CSR_HPMCOUNTER18 0xc12
  189. #define CSR_HPMCOUNTER19 0xc13
  190. #define CSR_HPMCOUNTER20 0xc14
  191. #define CSR_HPMCOUNTER21 0xc15
  192. #define CSR_HPMCOUNTER22 0xc16
  193. #define CSR_HPMCOUNTER23 0xc17
  194. #define CSR_HPMCOUNTER24 0xc18
  195. #define CSR_HPMCOUNTER25 0xc19
  196. #define CSR_HPMCOUNTER26 0xc1a
  197. #define CSR_HPMCOUNTER27 0xc1b
  198. #define CSR_HPMCOUNTER28 0xc1c
  199. #define CSR_HPMCOUNTER29 0xc1d
  200. #define CSR_HPMCOUNTER30 0xc1e
  201. #define CSR_HPMCOUNTER31 0xc1f
  202. #define CSR_CYCLEH 0xc80
  203. #define CSR_TIMEH 0xc81
  204. #define CSR_INSTRETH 0xc82
  205. #define CSR_HPMCOUNTER3H 0xc83
  206. #define CSR_HPMCOUNTER4H 0xc84
  207. #define CSR_HPMCOUNTER5H 0xc85
  208. #define CSR_HPMCOUNTER6H 0xc86
  209. #define CSR_HPMCOUNTER7H 0xc87
  210. #define CSR_HPMCOUNTER8H 0xc88
  211. #define CSR_HPMCOUNTER9H 0xc89
  212. #define CSR_HPMCOUNTER10H 0xc8a
  213. #define CSR_HPMCOUNTER11H 0xc8b
  214. #define CSR_HPMCOUNTER12H 0xc8c
  215. #define CSR_HPMCOUNTER13H 0xc8d
  216. #define CSR_HPMCOUNTER14H 0xc8e
  217. #define CSR_HPMCOUNTER15H 0xc8f
  218. #define CSR_HPMCOUNTER16H 0xc90
  219. #define CSR_HPMCOUNTER17H 0xc91
  220. #define CSR_HPMCOUNTER18H 0xc92
  221. #define CSR_HPMCOUNTER19H 0xc93
  222. #define CSR_HPMCOUNTER20H 0xc94
  223. #define CSR_HPMCOUNTER21H 0xc95
  224. #define CSR_HPMCOUNTER22H 0xc96
  225. #define CSR_HPMCOUNTER23H 0xc97
  226. #define CSR_HPMCOUNTER24H 0xc98
  227. #define CSR_HPMCOUNTER25H 0xc99
  228. #define CSR_HPMCOUNTER26H 0xc9a
  229. #define CSR_HPMCOUNTER27H 0xc9b
  230. #define CSR_HPMCOUNTER28H 0xc9c
  231. #define CSR_HPMCOUNTER29H 0xc9d
  232. #define CSR_HPMCOUNTER30H 0xc9e
  233. #define CSR_HPMCOUNTER31H 0xc9f
  234. /* ===== Supervisor-level CSRs ===== */
  235. /* Supervisor Trap Setup */
  236. #define CSR_SSTATUS 0x100
  237. #define CSR_SEDELEG 0x102
  238. #define CSR_SIDELEG 0x103
  239. #define CSR_SIE 0x104
  240. #define CSR_STVEC 0x105
  241. #define CSR_SCOUNTEREN 0x106
  242. /* Supervisor Trap Handling */
  243. #define CSR_SSCRATCH 0x140
  244. #define CSR_SEPC 0x141
  245. #define CSR_SCAUSE 0x142
  246. #define CSR_STVAL 0x143
  247. #define CSR_SIP 0x144
  248. /* Supervisor Protection and Translation */
  249. #define CSR_SATP 0x180
  250. /* ===== Hypervisor-level CSRs ===== */
  251. /* Hypervisor Trap Setup (H-extension) */
  252. #define CSR_HSTATUS 0x600
  253. #define CSR_HEDELEG 0x602
  254. #define CSR_HIDELEG 0x603
  255. #define CSR_HIE 0x604
  256. #define CSR_HCOUNTEREN 0x606
  257. #define CSR_HGEIE 0x607
  258. /* Hypervisor Trap Handling (H-extension) */
  259. #define CSR_HTVAL 0x643
  260. #define CSR_HIP 0x644
  261. #define CSR_HVIP 0x645
  262. #define CSR_HTINST 0x64a
  263. #define CSR_HGEIP 0xe12
  264. /* Hypervisor Protection and Translation (H-extension) */
  265. #define CSR_HGATP 0x680
  266. /* Hypervisor Counter/Timer Virtualization Registers (H-extension) */
  267. #define CSR_HTIMEDELTA 0x605
  268. #define CSR_HTIMEDELTAH 0x615
  269. /* Virtual Supervisor Registers (H-extension) */
  270. #define CSR_VSSTATUS 0x200
  271. #define CSR_VSIE 0x204
  272. #define CSR_VSTVEC 0x205
  273. #define CSR_VSSCRATCH 0x240
  274. #define CSR_VSEPC 0x241
  275. #define CSR_VSCAUSE 0x242
  276. #define CSR_VSTVAL 0x243
  277. #define CSR_VSIP 0x244
  278. #define CSR_VSATP 0x280
  279. /* ===== Machine-level CSRs ===== */
  280. /* Machine Information Registers */
  281. #define CSR_MVENDORID 0xf11
  282. #define CSR_MARCHID 0xf12
  283. #define CSR_MIMPID 0xf13
  284. #define CSR_MHARTID 0xf14
  285. /* Machine Trap Setup */
  286. #define CSR_MSTATUS 0x300
  287. #define CSR_MISA 0x301
  288. #define CSR_MEDELEG 0x302
  289. #define CSR_MIDELEG 0x303
  290. #define CSR_MIE 0x304
  291. #define CSR_MTVEC 0x305
  292. #define CSR_MCOUNTEREN 0x306
  293. #define CSR_MSTATUSH 0x310
  294. /* Machine Trap Handling */
  295. #define CSR_MSCRATCH 0x340
  296. #define CSR_MEPC 0x341
  297. #define CSR_MCAUSE 0x342
  298. #define CSR_MTVAL 0x343
  299. #define CSR_MIP 0x344
  300. #define CSR_MTINST 0x34a
  301. #define CSR_MTVAL2 0x34b
  302. /* Machine Memory Protection */
  303. #define CSR_PMPCFG0 0x3a0
  304. #define CSR_PMPCFG1 0x3a1
  305. #define CSR_PMPCFG2 0x3a2
  306. #define CSR_PMPCFG3 0x3a3
  307. #define CSR_PMPCFG4 0x3a4
  308. #define CSR_PMPCFG5 0x3a5
  309. #define CSR_PMPCFG6 0x3a6
  310. #define CSR_PMPCFG7 0x3a7
  311. #define CSR_PMPCFG8 0x3a8
  312. #define CSR_PMPCFG9 0x3a9
  313. #define CSR_PMPCFG10 0x3aa
  314. #define CSR_PMPCFG11 0x3ab
  315. #define CSR_PMPCFG12 0x3ac
  316. #define CSR_PMPCFG13 0x3ad
  317. #define CSR_PMPCFG14 0x3ae
  318. #define CSR_PMPCFG15 0x3af
  319. #define CSR_PMPADDR0 0x3b0
  320. #define CSR_PMPADDR1 0x3b1
  321. #define CSR_PMPADDR2 0x3b2
  322. #define CSR_PMPADDR3 0x3b3
  323. #define CSR_PMPADDR4 0x3b4
  324. #define CSR_PMPADDR5 0x3b5
  325. #define CSR_PMPADDR6 0x3b6
  326. #define CSR_PMPADDR7 0x3b7
  327. #define CSR_PMPADDR8 0x3b8
  328. #define CSR_PMPADDR9 0x3b9
  329. #define CSR_PMPADDR10 0x3ba
  330. #define CSR_PMPADDR11 0x3bb
  331. #define CSR_PMPADDR12 0x3bc
  332. #define CSR_PMPADDR13 0x3bd
  333. #define CSR_PMPADDR14 0x3be
  334. #define CSR_PMPADDR15 0x3bf
  335. #define CSR_PMPADDR16 0x3c0
  336. #define CSR_PMPADDR17 0x3c1
  337. #define CSR_PMPADDR18 0x3c2
  338. #define CSR_PMPADDR19 0x3c3
  339. #define CSR_PMPADDR20 0x3c4
  340. #define CSR_PMPADDR21 0x3c5
  341. #define CSR_PMPADDR22 0x3c6
  342. #define CSR_PMPADDR23 0x3c7
  343. #define CSR_PMPADDR24 0x3c8
  344. #define CSR_PMPADDR25 0x3c9
  345. #define CSR_PMPADDR26 0x3ca
  346. #define CSR_PMPADDR27 0x3cb
  347. #define CSR_PMPADDR28 0x3cc
  348. #define CSR_PMPADDR29 0x3cd
  349. #define CSR_PMPADDR30 0x3ce
  350. #define CSR_PMPADDR31 0x3cf
  351. #define CSR_PMPADDR32 0x3d0
  352. #define CSR_PMPADDR33 0x3d1
  353. #define CSR_PMPADDR34 0x3d2
  354. #define CSR_PMPADDR35 0x3d3
  355. #define CSR_PMPADDR36 0x3d4
  356. #define CSR_PMPADDR37 0x3d5
  357. #define CSR_PMPADDR38 0x3d6
  358. #define CSR_PMPADDR39 0x3d7
  359. #define CSR_PMPADDR40 0x3d8
  360. #define CSR_PMPADDR41 0x3d9
  361. #define CSR_PMPADDR42 0x3da
  362. #define CSR_PMPADDR43 0x3db
  363. #define CSR_PMPADDR44 0x3dc
  364. #define CSR_PMPADDR45 0x3dd
  365. #define CSR_PMPADDR46 0x3de
  366. #define CSR_PMPADDR47 0x3df
  367. #define CSR_PMPADDR48 0x3e0
  368. #define CSR_PMPADDR49 0x3e1
  369. #define CSR_PMPADDR50 0x3e2
  370. #define CSR_PMPADDR51 0x3e3
  371. #define CSR_PMPADDR52 0x3e4
  372. #define CSR_PMPADDR53 0x3e5
  373. #define CSR_PMPADDR54 0x3e6
  374. #define CSR_PMPADDR55 0x3e7
  375. #define CSR_PMPADDR56 0x3e8
  376. #define CSR_PMPADDR57 0x3e9
  377. #define CSR_PMPADDR58 0x3ea
  378. #define CSR_PMPADDR59 0x3eb
  379. #define CSR_PMPADDR60 0x3ec
  380. #define CSR_PMPADDR61 0x3ed
  381. #define CSR_PMPADDR62 0x3ee
  382. #define CSR_PMPADDR63 0x3ef
  383. /* Machine Counters/Timers */
  384. #define CSR_MCYCLE 0xb00
  385. #define CSR_MINSTRET 0xb02
  386. #define CSR_MHPMCOUNTER3 0xb03
  387. #define CSR_MHPMCOUNTER4 0xb04
  388. #define CSR_MHPMCOUNTER5 0xb05
  389. #define CSR_MHPMCOUNTER6 0xb06
  390. #define CSR_MHPMCOUNTER7 0xb07
  391. #define CSR_MHPMCOUNTER8 0xb08
  392. #define CSR_MHPMCOUNTER9 0xb09
  393. #define CSR_MHPMCOUNTER10 0xb0a
  394. #define CSR_MHPMCOUNTER11 0xb0b
  395. #define CSR_MHPMCOUNTER12 0xb0c
  396. #define CSR_MHPMCOUNTER13 0xb0d
  397. #define CSR_MHPMCOUNTER14 0xb0e
  398. #define CSR_MHPMCOUNTER15 0xb0f
  399. #define CSR_MHPMCOUNTER16 0xb10
  400. #define CSR_MHPMCOUNTER17 0xb11
  401. #define CSR_MHPMCOUNTER18 0xb12
  402. #define CSR_MHPMCOUNTER19 0xb13
  403. #define CSR_MHPMCOUNTER20 0xb14
  404. #define CSR_MHPMCOUNTER21 0xb15
  405. #define CSR_MHPMCOUNTER22 0xb16
  406. #define CSR_MHPMCOUNTER23 0xb17
  407. #define CSR_MHPMCOUNTER24 0xb18
  408. #define CSR_MHPMCOUNTER25 0xb19
  409. #define CSR_MHPMCOUNTER26 0xb1a
  410. #define CSR_MHPMCOUNTER27 0xb1b
  411. #define CSR_MHPMCOUNTER28 0xb1c
  412. #define CSR_MHPMCOUNTER29 0xb1d
  413. #define CSR_MHPMCOUNTER30 0xb1e
  414. #define CSR_MHPMCOUNTER31 0xb1f
  415. #define CSR_MCYCLEH 0xb80
  416. #define CSR_MINSTRETH 0xb82
  417. #define CSR_MHPMCOUNTER3H 0xb83
  418. #define CSR_MHPMCOUNTER4H 0xb84
  419. #define CSR_MHPMCOUNTER5H 0xb85
  420. #define CSR_MHPMCOUNTER6H 0xb86
  421. #define CSR_MHPMCOUNTER7H 0xb87
  422. #define CSR_MHPMCOUNTER8H 0xb88
  423. #define CSR_MHPMCOUNTER9H 0xb89
  424. #define CSR_MHPMCOUNTER10H 0xb8a
  425. #define CSR_MHPMCOUNTER11H 0xb8b
  426. #define CSR_MHPMCOUNTER12H 0xb8c
  427. #define CSR_MHPMCOUNTER13H 0xb8d
  428. #define CSR_MHPMCOUNTER14H 0xb8e
  429. #define CSR_MHPMCOUNTER15H 0xb8f
  430. #define CSR_MHPMCOUNTER16H 0xb90
  431. #define CSR_MHPMCOUNTER17H 0xb91
  432. #define CSR_MHPMCOUNTER18H 0xb92
  433. #define CSR_MHPMCOUNTER19H 0xb93
  434. #define CSR_MHPMCOUNTER20H 0xb94
  435. #define CSR_MHPMCOUNTER21H 0xb95
  436. #define CSR_MHPMCOUNTER22H 0xb96
  437. #define CSR_MHPMCOUNTER23H 0xb97
  438. #define CSR_MHPMCOUNTER24H 0xb98
  439. #define CSR_MHPMCOUNTER25H 0xb99
  440. #define CSR_MHPMCOUNTER26H 0xb9a
  441. #define CSR_MHPMCOUNTER27H 0xb9b
  442. #define CSR_MHPMCOUNTER28H 0xb9c
  443. #define CSR_MHPMCOUNTER29H 0xb9d
  444. #define CSR_MHPMCOUNTER30H 0xb9e
  445. #define CSR_MHPMCOUNTER31H 0xb9f
  446. /* Machine Counter Setup */
  447. #define CSR_MCOUNTINHIBIT 0x320
  448. #define CSR_MHPMEVENT3 0x323
  449. #define CSR_MHPMEVENT4 0x324
  450. #define CSR_MHPMEVENT5 0x325
  451. #define CSR_MHPMEVENT6 0x326
  452. #define CSR_MHPMEVENT7 0x327
  453. #define CSR_MHPMEVENT8 0x328
  454. #define CSR_MHPMEVENT9 0x329
  455. #define CSR_MHPMEVENT10 0x32a
  456. #define CSR_MHPMEVENT11 0x32b
  457. #define CSR_MHPMEVENT12 0x32c
  458. #define CSR_MHPMEVENT13 0x32d
  459. #define CSR_MHPMEVENT14 0x32e
  460. #define CSR_MHPMEVENT15 0x32f
  461. #define CSR_MHPMEVENT16 0x330
  462. #define CSR_MHPMEVENT17 0x331
  463. #define CSR_MHPMEVENT18 0x332
  464. #define CSR_MHPMEVENT19 0x333
  465. #define CSR_MHPMEVENT20 0x334
  466. #define CSR_MHPMEVENT21 0x335
  467. #define CSR_MHPMEVENT22 0x336
  468. #define CSR_MHPMEVENT23 0x337
  469. #define CSR_MHPMEVENT24 0x338
  470. #define CSR_MHPMEVENT25 0x339
  471. #define CSR_MHPMEVENT26 0x33a
  472. #define CSR_MHPMEVENT27 0x33b
  473. #define CSR_MHPMEVENT28 0x33c
  474. #define CSR_MHPMEVENT29 0x33d
  475. #define CSR_MHPMEVENT30 0x33e
  476. #define CSR_MHPMEVENT31 0x33f
  477. /* Debug/Trace Registers */
  478. #define CSR_TSELECT 0x7a0
  479. #define CSR_TDATA1 0x7a1
  480. #define CSR_TDATA2 0x7a2
  481. #define CSR_TDATA3 0x7a3
  482. /* Debug Mode Registers */
  483. #define CSR_DCSR 0x7b0
  484. #define CSR_DPC 0x7b1
  485. #define CSR_DSCRATCH0 0x7b2
  486. #define CSR_DSCRATCH1 0x7b3
  487. /* ===== Trap/Exception Causes ===== */
  488. #define CAUSE_MISALIGNED_FETCH 0x0
  489. #define CAUSE_FETCH_ACCESS 0x1
  490. #define CAUSE_ILLEGAL_INSTRUCTION 0x2
  491. #define CAUSE_BREAKPOINT 0x3
  492. #define CAUSE_MISALIGNED_LOAD 0x4
  493. #define CAUSE_LOAD_ACCESS 0x5
  494. #define CAUSE_MISALIGNED_STORE 0x6
  495. #define CAUSE_STORE_ACCESS 0x7
  496. #define CAUSE_USER_ECALL 0x8
  497. #define CAUSE_SUPERVISOR_ECALL 0x9
  498. #define CAUSE_VIRTUAL_SUPERVISOR_ECALL 0xa
  499. #define CAUSE_MACHINE_ECALL 0xb
  500. #define CAUSE_FETCH_PAGE_FAULT 0xc
  501. #define CAUSE_LOAD_PAGE_FAULT 0xd
  502. #define CAUSE_STORE_PAGE_FAULT 0xf
  503. #define CAUSE_FETCH_GUEST_PAGE_FAULT 0x14
  504. #define CAUSE_LOAD_GUEST_PAGE_FAULT 0x15
  505. #define CAUSE_VIRTUAL_INST_FAULT 0x16
  506. #define CAUSE_STORE_GUEST_PAGE_FAULT 0x17
  507. /* ===== Instruction Encodings ===== */
  508. #define INSN_MATCH_LB 0x3
  509. #define INSN_MASK_LB 0x707f
  510. #define INSN_MATCH_LH 0x1003
  511. #define INSN_MASK_LH 0x707f
  512. #define INSN_MATCH_LW 0x2003
  513. #define INSN_MASK_LW 0x707f
  514. #define INSN_MATCH_LD 0x3003
  515. #define INSN_MASK_LD 0x707f
  516. #define INSN_MATCH_LBU 0x4003
  517. #define INSN_MASK_LBU 0x707f
  518. #define INSN_MATCH_LHU 0x5003
  519. #define INSN_MASK_LHU 0x707f
  520. #define INSN_MATCH_LWU 0x6003
  521. #define INSN_MASK_LWU 0x707f
  522. #define INSN_MATCH_SB 0x23
  523. #define INSN_MASK_SB 0x707f
  524. #define INSN_MATCH_SH 0x1023
  525. #define INSN_MASK_SH 0x707f
  526. #define INSN_MATCH_SW 0x2023
  527. #define INSN_MASK_SW 0x707f
  528. #define INSN_MATCH_SD 0x3023
  529. #define INSN_MASK_SD 0x707f
  530. #define INSN_MATCH_FLW 0x2007
  531. #define INSN_MASK_FLW 0x707f
  532. #define INSN_MATCH_FLD 0x3007
  533. #define INSN_MASK_FLD 0x707f
  534. #define INSN_MATCH_FLQ 0x4007
  535. #define INSN_MASK_FLQ 0x707f
  536. #define INSN_MATCH_FSW 0x2027
  537. #define INSN_MASK_FSW 0x707f
  538. #define INSN_MATCH_FSD 0x3027
  539. #define INSN_MASK_FSD 0x707f
  540. #define INSN_MATCH_FSQ 0x4027
  541. #define INSN_MASK_FSQ 0x707f
  542. #define INSN_MATCH_C_LD 0x6000
  543. #define INSN_MASK_C_LD 0xe003
  544. #define INSN_MATCH_C_SD 0xe000
  545. #define INSN_MASK_C_SD 0xe003
  546. #define INSN_MATCH_C_LW 0x4000
  547. #define INSN_MASK_C_LW 0xe003
  548. #define INSN_MATCH_C_SW 0xc000
  549. #define INSN_MASK_C_SW 0xe003
  550. #define INSN_MATCH_C_LDSP 0x6002
  551. #define INSN_MASK_C_LDSP 0xe003
  552. #define INSN_MATCH_C_SDSP 0xe002
  553. #define INSN_MASK_C_SDSP 0xe003
  554. #define INSN_MATCH_C_LWSP 0x4002
  555. #define INSN_MASK_C_LWSP 0xe003
  556. #define INSN_MATCH_C_SWSP 0xc002
  557. #define INSN_MASK_C_SWSP 0xe003
  558. #define INSN_MATCH_C_FLD 0x2000
  559. #define INSN_MASK_C_FLD 0xe003
  560. #define INSN_MATCH_C_FLW 0x6000
  561. #define INSN_MASK_C_FLW 0xe003
  562. #define INSN_MATCH_C_FSD 0xa000
  563. #define INSN_MASK_C_FSD 0xe003
  564. #define INSN_MATCH_C_FSW 0xe000
  565. #define INSN_MASK_C_FSW 0xe003
  566. #define INSN_MATCH_C_FLDSP 0x2002
  567. #define INSN_MASK_C_FLDSP 0xe003
  568. #define INSN_MATCH_C_FSDSP 0xa002
  569. #define INSN_MASK_C_FSDSP 0xe003
  570. #define INSN_MATCH_C_FLWSP 0x6002
  571. #define INSN_MASK_C_FLWSP 0xe003
  572. #define INSN_MATCH_C_FSWSP 0xe002
  573. #define INSN_MASK_C_FSWSP 0xe003
  574. #define INSN_MASK_WFI 0xffffff00
  575. #define INSN_MATCH_WFI 0x10500000
  576. #define INSN_16BIT_MASK 0x3
  577. #define INSN_32BIT_MASK 0x1c
  578. #define INSN_IS_16BIT(insn) \
  579. (((insn) & INSN_16BIT_MASK) != INSN_16BIT_MASK)
  580. #define INSN_IS_32BIT(insn) \
  581. (((insn) & INSN_16BIT_MASK) == INSN_16BIT_MASK && \
  582. ((insn) & INSN_32BIT_MASK) != INSN_32BIT_MASK)
  583. #define INSN_LEN(insn) (INSN_IS_16BIT(insn) ? 2 : 4)
  584. #if __riscv_xlen == 64
  585. #define LOG_REGBYTES 3
  586. #else
  587. #define LOG_REGBYTES 2
  588. #endif
  589. #define REGBYTES (1 << LOG_REGBYTES)
  590. #define SH_RD 7
  591. #define SH_RS1 15
  592. #define SH_RS2 20
  593. #define SH_RS2C 2
  594. #define RV_X(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1))
  595. #define RVC_LW_IMM(x) ((RV_X(x, 6, 1) << 2) | \
  596. (RV_X(x, 10, 3) << 3) | \
  597. (RV_X(x, 5, 1) << 6))
  598. #define RVC_LD_IMM(x) ((RV_X(x, 10, 3) << 3) | \
  599. (RV_X(x, 5, 2) << 6))
  600. #define RVC_LWSP_IMM(x) ((RV_X(x, 4, 3) << 2) | \
  601. (RV_X(x, 12, 1) << 5) | \
  602. (RV_X(x, 2, 2) << 6))
  603. #define RVC_LDSP_IMM(x) ((RV_X(x, 5, 2) << 3) | \
  604. (RV_X(x, 12, 1) << 5) | \
  605. (RV_X(x, 2, 3) << 6))
  606. #define RVC_SWSP_IMM(x) ((RV_X(x, 9, 4) << 2) | \
  607. (RV_X(x, 7, 2) << 6))
  608. #define RVC_SDSP_IMM(x) ((RV_X(x, 10, 3) << 3) | \
  609. (RV_X(x, 7, 3) << 6))
  610. #define RVC_RS1S(insn) (8 + RV_X(insn, SH_RD, 3))
  611. #define RVC_RS2S(insn) (8 + RV_X(insn, SH_RS2C, 3))
  612. #define RVC_RS2(insn) RV_X(insn, SH_RS2C, 5)
  613. #define SHIFT_RIGHT(x, y) \
  614. ((y) < 0 ? ((x) << -(y)) : ((x) >> (y)))
  615. #define REG_MASK \
  616. ((1 << (5 + LOG_REGBYTES)) - (1 << LOG_REGBYTES))
  617. #define REG_OFFSET(insn, pos) \
  618. (SHIFT_RIGHT((insn), (pos) - LOG_REGBYTES) & REG_MASK)
  619. #define REG_PTR(insn, pos, regs) \
  620. (ulong *)((ulong)(regs) + REG_OFFSET(insn, pos))
  621. #define GET_RM(insn) (((insn) >> 12) & 7)
  622. #define GET_RS1(insn, regs) (*REG_PTR(insn, SH_RS1, regs))
  623. #define GET_RS2(insn, regs) (*REG_PTR(insn, SH_RS2, regs))
  624. #define GET_RS1S(insn, regs) (*REG_PTR(RVC_RS1S(insn), 0, regs))
  625. #define GET_RS2S(insn, regs) (*REG_PTR(RVC_RS2S(insn), 0, regs))
  626. #define GET_RS2C(insn, regs) (*REG_PTR(insn, SH_RS2C, regs))
  627. #define GET_SP(regs) (*REG_PTR(2, 0, regs))
  628. #define SET_RD(insn, regs, val) (*REG_PTR(insn, SH_RD, regs) = (val))
  629. #define IMM_I(insn) ((s32)(insn) >> 20)
  630. #define IMM_S(insn) (((s32)(insn) >> 25 << 5) | \
  631. (s32)(((insn) >> 7) & 0x1f))
  632. #define MASK_FUNCT3 0x7000
  633. /* clang-format on */
  634. #endif