4
0

macros.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. macro_rules! read_csr {
  2. ($csr_number:expr, $asm_fn: ident) => {
  3. /// Reads the CSR
  4. #[inline]
  5. unsafe fn _read() -> usize {
  6. match () {
  7. #[cfg(all(riscv, feature = "inline-asm"))]
  8. () => {
  9. let r: usize;
  10. core::arch::asm!("csrrs {0}, {1}, x0", out(reg) r, const $csr_number);
  11. r
  12. }
  13. #[cfg(all(riscv, not(feature = "inline-asm")))]
  14. () => {
  15. extern "C" {
  16. fn $asm_fn() -> usize;
  17. }
  18. $asm_fn()
  19. }
  20. #[cfg(not(riscv))]
  21. () => unimplemented!(),
  22. }
  23. }
  24. };
  25. }
  26. macro_rules! read_csr_rv32 {
  27. ($csr_number:expr, $asm_fn: ident) => {
  28. /// Reads the CSR
  29. #[inline]
  30. unsafe fn _read() -> usize {
  31. match () {
  32. #[cfg(all(riscv32, feature = "inline-asm"))]
  33. () => {
  34. let r: usize;
  35. core::arch::asm!("csrrs {0}, {1}, x0", out(reg) r, const $csr_number);
  36. r
  37. }
  38. #[cfg(all(riscv32, not(feature = "inline-asm")))]
  39. () => {
  40. extern "C" {
  41. fn $asm_fn() -> usize;
  42. }
  43. $asm_fn()
  44. }
  45. #[cfg(not(riscv32))]
  46. () => unimplemented!(),
  47. }
  48. }
  49. };
  50. }
  51. macro_rules! read_csr_as {
  52. ($register:ident, $csr_number:expr, $asm_fn: ident) => {
  53. read_csr!($csr_number, $asm_fn);
  54. /// Reads the CSR
  55. #[inline]
  56. pub fn read() -> $register {
  57. $register {
  58. bits: unsafe { _read() },
  59. }
  60. }
  61. };
  62. }
  63. macro_rules! read_csr_as_usize {
  64. ($csr_number:expr, $asm_fn: ident) => {
  65. read_csr!($csr_number, $asm_fn);
  66. /// Reads the CSR
  67. #[inline]
  68. pub fn read() -> usize {
  69. unsafe { _read() }
  70. }
  71. };
  72. }
  73. macro_rules! read_csr_as_usize_rv32 {
  74. ($csr_number:expr, $asm_fn: ident) => {
  75. read_csr_rv32!($csr_number, $asm_fn);
  76. /// Reads the CSR
  77. #[inline]
  78. pub fn read() -> usize {
  79. unsafe { _read() }
  80. }
  81. };
  82. }
  83. macro_rules! write_csr {
  84. ($csr_number:expr, $asm_fn: ident) => {
  85. /// Writes the CSR
  86. #[inline]
  87. #[allow(unused_variables)]
  88. unsafe fn _write(bits: usize) {
  89. match () {
  90. #[cfg(all(riscv, feature = "inline-asm"))]
  91. () => core::arch::asm!("csrrw x0, {1}, {0}", in(reg) bits, const $csr_number),
  92. #[cfg(all(riscv, not(feature = "inline-asm")))]
  93. () => {
  94. extern "C" {
  95. fn $asm_fn(bits: usize);
  96. }
  97. $asm_fn(bits);
  98. }
  99. #[cfg(not(riscv))]
  100. () => unimplemented!(),
  101. }
  102. }
  103. };
  104. }
  105. macro_rules! write_csr_rv32 {
  106. ($csr_number:expr, $asm_fn: ident) => {
  107. /// Writes the CSR
  108. #[inline]
  109. #[allow(unused_variables)]
  110. unsafe fn _write(bits: usize) {
  111. match () {
  112. #[cfg(all(riscv32, feature = "inline-asm"))]
  113. () => core::arch::asm!("csrrw x0, {1}, {0}", in(reg) bits, const $csr_number),
  114. #[cfg(all(riscv32, not(feature = "inline-asm")))]
  115. () => {
  116. extern "C" {
  117. fn $asm_fn(bits: usize);
  118. }
  119. $asm_fn(bits);
  120. }
  121. #[cfg(not(riscv32))]
  122. () => unimplemented!(),
  123. }
  124. }
  125. };
  126. }
  127. macro_rules! write_csr_as_usize {
  128. ($csr_number:expr, $asm_fn: ident) => {
  129. write_csr!($csr_number, $asm_fn);
  130. /// Writes the CSR
  131. #[inline]
  132. pub fn write(bits: usize) {
  133. unsafe { _write(bits) }
  134. }
  135. };
  136. }
  137. macro_rules! write_csr_as_usize_rv32 {
  138. ($csr_number:expr, $asm_fn: ident) => {
  139. write_csr_rv32!($csr_number, $asm_fn);
  140. /// Writes the CSR
  141. #[inline]
  142. pub fn write(bits: usize) {
  143. unsafe { _write(bits) }
  144. }
  145. };
  146. }
  147. macro_rules! set {
  148. ($csr_number:expr, $asm_fn: ident) => {
  149. /// Set the CSR
  150. #[inline]
  151. #[allow(unused_variables)]
  152. unsafe fn _set(bits: usize) {
  153. match () {
  154. #[cfg(all(riscv, feature = "inline-asm"))]
  155. () => core::arch::asm!("csrrs x0, {1}, {0}", in(reg) bits, const $csr_number),
  156. #[cfg(all(riscv, not(feature = "inline-asm")))]
  157. () => {
  158. extern "C" {
  159. fn $asm_fn(bits: usize);
  160. }
  161. $asm_fn(bits);
  162. }
  163. #[cfg(not(riscv))]
  164. () => unimplemented!(),
  165. }
  166. }
  167. };
  168. }
  169. macro_rules! clear {
  170. ($csr_number:expr, $asm_fn: ident) => {
  171. /// Clear the CSR
  172. #[inline]
  173. #[allow(unused_variables)]
  174. unsafe fn _clear(bits: usize) {
  175. match () {
  176. #[cfg(all(riscv, feature = "inline-asm"))]
  177. () => core::arch::asm!("csrrc x0, {1}, {0}", in(reg) bits, const $csr_number),
  178. #[cfg(all(riscv, not(feature = "inline-asm")))]
  179. () => {
  180. extern "C" {
  181. fn $asm_fn(bits: usize);
  182. }
  183. $asm_fn(bits);
  184. }
  185. #[cfg(not(riscv))]
  186. () => unimplemented!(),
  187. }
  188. }
  189. };
  190. }
  191. macro_rules! set_csr {
  192. ($(#[$attr:meta])*, $set_field:ident, $e:expr) => {
  193. $(#[$attr])*
  194. #[inline]
  195. pub unsafe fn $set_field() {
  196. _set($e);
  197. }
  198. };
  199. }
  200. macro_rules! clear_csr {
  201. ($(#[$attr:meta])*, $clear_field:ident, $e:expr) => {
  202. $(#[$attr])*
  203. #[inline]
  204. pub unsafe fn $clear_field() {
  205. _clear($e);
  206. }
  207. };
  208. }
  209. macro_rules! set_clear_csr {
  210. ($(#[$attr:meta])*, $set_field:ident, $clear_field:ident, $e:expr) => {
  211. set_csr!($(#[$attr])*, $set_field, $e);
  212. clear_csr!($(#[$attr])*, $clear_field, $e);
  213. }
  214. }
  215. macro_rules! read_composite_csr {
  216. ($hi:expr, $lo:expr) => {
  217. /// Reads the CSR as a 64-bit value
  218. #[inline]
  219. pub fn read64() -> u64 {
  220. match () {
  221. #[cfg(riscv32)]
  222. () => loop {
  223. let hi = $hi;
  224. let lo = $lo;
  225. if hi == $hi {
  226. return ((hi as u64) << 32) | lo as u64;
  227. }
  228. },
  229. #[cfg(not(riscv32))]
  230. () => $lo as u64,
  231. }
  232. }
  233. };
  234. }
  235. macro_rules! set_pmp {
  236. () => {
  237. /// Set the pmp configuration corresponding to the index
  238. #[inline]
  239. pub unsafe fn set_pmp(index: usize, range: Range, permission: Permission, locked: bool) {
  240. #[cfg(riscv32)]
  241. assert!(index < 4);
  242. #[cfg(riscv64)]
  243. assert!(index < 8);
  244. let mut value = _read();
  245. let byte = (locked as usize) << 7 | (range as usize) << 3 | (permission as usize);
  246. value.set_bits(8 * index..=8 * index + 7, byte);
  247. _write(value);
  248. }
  249. };
  250. }
  251. macro_rules! clear_pmp {
  252. () => {
  253. /// Clear the pmp configuration corresponding to the index
  254. #[inline]
  255. pub unsafe fn clear_pmp(index: usize) {
  256. #[cfg(riscv32)]
  257. assert!(index < 4);
  258. #[cfg(riscv64)]
  259. assert!(index < 8);
  260. let mut value = _read();
  261. value.set_bits(8 * index..=8 * index + 7, 0);
  262. _write(value);
  263. }
  264. };
  265. }