4
0

pmu.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. use crate::ecall::SbiRet;
  2. use crate::util::AmoOnceRef;
  3. /// Performance Monitoring Unit Extension
  4. ///
  5. /// The RISC-V hardware performance counters such as `mcycle`, `minstret`, and `mhpmcounterX` CSRs
  6. /// are accessible as read-only from supervisor-mode using `cycle`, `instret`, and `hpmcounterX` CSRs.
  7. /// The SBI performance monitoring unit (PMU) extension is an interface for supervisor-mode to configure
  8. /// and use the RISC-V hardware performance counters with assistance from the machine-mode (or hypervisor-mode).
  9. /// These hardware performance counters can only be started, stopped, or configured from machine-mode
  10. /// using `mcountinhibit` and `mhpmeventX` CSRs.
  11. /// Due to this, a machine-mode SBI implementation may choose to disallow SBI PMU extension
  12. /// if `mcountinhibit` CSR is not implemented by the RISC-V platform.
  13. ///
  14. /// A RISC-V platform generally supports monitoring of various hardware events using a limited number
  15. /// of hardware performance counters which are up to 64 bits wide.
  16. /// In addition, a SBI implementation can also provide firmware performance counters which can monitor firmware events
  17. /// such as number of misaligned load/store instructions, number of RFENCEs, number of IPIs, etc.
  18. /// The firmware counters are always 64 bits wide.
  19. ///
  20. /// The SBI PMU extension provides:
  21. ///
  22. /// 1. An interface for supervisor-mode software to discover and configure per-HART hardware/firmware counters
  23. /// 2. A typical perf compatible interface for hardware/firmware performance counters and events
  24. /// 3. Full access to microarchitecture’s raw event encodings
  25. ///
  26. /// To define SBI PMU extension calls, we first define important entities `counter_idx`, `event_idx`, and `event_data`.
  27. /// The `counter_idx` is a logical number assigned to each hardware/firmware counter.
  28. /// The `event_idx `represents a hardware (or firmware) event whereas
  29. /// the `event_data` is 64 bits wide and represents additional configuration (or parameters) for
  30. /// a hardware (or firmware) event.
  31. ///
  32. /// The event_idx is a 20 bits wide number encoded as follows:
  33. ///
  34. /// ```text
  35. /// event_idx[19:16] = type;
  36. /// event_idx[15:0] = code;
  37. /// ```
  38. pub trait Pmu: Send + Sync {
  39. /// Returns the number of counters (both hardware and firmware).
  40. ///
  41. /// The value is returned in `SbiRet.value`; this call always returns SBI_SUCCESS in `SbiRet.error`.
  42. fn num_counters(&self) -> usize;
  43. /// Get details about the specified counter such as underlying CSR number, width of the counter,
  44. /// type of counter hardware/firmware, etc.
  45. ///
  46. /// The `counter_info` returned by this SBI call is encoded as follows:
  47. ///
  48. /// ```text
  49. /// counter_info[11:0] = CSR; // (12bit CSR number)
  50. /// counter_info[17:12] = Width; // (One less than number of bits in CSR)
  51. /// counter_info[XLEN-2:18] = Reserved; // Reserved for future use
  52. /// counter_info[XLEN-1] = Type; // (0 = hardware and 1 = firmware)
  53. /// ```
  54. /// If `counter_info.type` == `1` then `counter_info.csr` and `counter_info.width` should be ignored.
  55. ///
  56. /// # Return value
  57. ///
  58. /// Returns the `counter_info` described above in `SbiRet.value`.
  59. ///
  60. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  61. ///
  62. /// | Return code | Description
  63. /// |:------------------------|:----------------------------------------------
  64. /// | SBI_SUCCESS | `counter_info` read successfully.
  65. /// | SBI_ERR_INVALID_PARAM | `counter_idx` points to an invalid counter.
  66. fn counter_get_info(&self, counter_idx: usize) -> SbiRet;
  67. /// Find and configure a counter from a set of counters which is not started (or enabled)
  68. /// and can monitor the specified event.
  69. ///
  70. /// # Parameters
  71. ///
  72. /// The `counter_idx_base` and `counter_idx_mask` parameters represent the set of counters,
  73. /// whereas the `event_idx` represent the event to be monitored
  74. /// and `event_data` represents any additional event configuration.
  75. ///
  76. /// The `config_flags` parameter represent additional counter configuration and filter flags.
  77. /// The bit definitions of the `config_flags` parameter are shown in the table below:
  78. ///
  79. /// | Flag Name | Bits | Description
  80. /// |:-----------------------------|:-----------|:------------
  81. /// | SBI_PMU_CFG_FLAG_SKIP_MATCH | 0:0 | Skip the counter matching
  82. /// | SBI_PMU_CFG_FLAG_CLEAR_VALUE | 1:1 | Clear (or zero) the counter value in counter configuration
  83. /// | SBI_PMU_CFG_FLAG_AUTO_START | 2:2 | Start the counter after configuring a matching counter
  84. /// | SBI_PMU_CFG_FLAG_SET_VUINH | 3:3 | Event counting inhibited in VU-mode
  85. /// | SBI_PMU_CFG_FLAG_SET_VSINH | 4:4 | Event counting inhibited in VS-mode
  86. /// | SBI_PMU_CFG_FLAG_SET_UINH | 5:5 | Event counting inhibited in U-mode
  87. /// | SBI_PMU_CFG_FLAG_SET_SINH | 6:6 | Event counting inhibited in S-mode
  88. /// | SBI_PMU_CFG_FLAG_SET_MINH | 7:7 | Event counting inhibited in M-mode
  89. /// | _RESERVED_ | 8:(XLEN-1) | _All non-zero values are reserved for future use._
  90. ///
  91. /// *NOTE:* When *SBI_PMU_CFG_FLAG_SKIP_MATCH* is set in `config_flags`, the
  92. /// SBI implementation will unconditionally select the first counter from the
  93. /// set of counters specified by the `counter_idx_base` and `counter_idx_mask`.
  94. ///
  95. /// *NOTE:* The *SBI_PMU_CFG_FLAG_AUTO_START* flag in `config_flags` has no
  96. /// impact on the counter value.
  97. ///
  98. /// *NOTE:* The `config_flags[3:7]` bits are event filtering hints so these
  99. /// can be ignored or overridden by the SBI implementation for security concerns
  100. /// or due to lack of event filtering support in the underlying RISC-V platform.
  101. ///
  102. /// # Return value
  103. ///
  104. /// Returns the `counter_idx` in `sbiret.value` upon success.
  105. ///
  106. /// In case of failure, the possible error codes returned in `sbiret.error` are shown in the table below:
  107. ///
  108. /// | Return code | Description
  109. /// |:----------------------|:----------------------------------------------
  110. /// | SBI_SUCCESS | counter found and configured successfully.
  111. /// | SBI_ERR_INVALID_PARAM | set of counters has an invalid counter.
  112. /// | SBI_ERR_NOT_SUPPORTED | none of the counters can monitor specified event.
  113. fn counter_config_matching(
  114. &self,
  115. counter_idx_base: usize,
  116. counter_idx_mask: usize,
  117. config_flags: usize,
  118. event_idx: usize,
  119. event_data: u64,
  120. ) -> SbiRet;
  121. /// Start or enable a set of counters on the calling HART with the specified initial value.
  122. ///
  123. /// # Parameters
  124. ///
  125. /// The `counter_idx_base` and `counter_idx_mask` parameters represent the set of counters.
  126. /// whereas the `initial_value` parameter specifies the initial value of the counter.
  127. ///
  128. /// The bit definitions of the `start_flags` parameter are shown in the table below:
  129. ///
  130. /// | Flag Name | Bits | Description
  131. /// |:-----------------------------|:-----------|:------------
  132. /// | SBI_PMU_START_SET_INIT_VALUE | 0:0 | Set the value of counters based on the `initial_value` parameter.
  133. /// | _RESERVED_ | 1:(XLEN-1) | _All non-zero values are reserved for future use._
  134. ///
  135. /// *NOTE*: When `SBI_PMU_START_SET_INIT_VALUE` is not set in `start_flags`, the counter value will
  136. /// not be modified and event counting will start from current counter value.
  137. ///
  138. /// # Return value
  139. ///
  140. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  141. ///
  142. /// | Return code | Description
  143. /// |:------------------------|:----------------------------------------------
  144. /// | SBI_SUCCESS | counter started successfully.
  145. /// | SBI_ERR_INVALID_PARAM | some of the counters specified in parameters are invalid.
  146. /// | SBI_ERR_ALREADY_STARTED | some of the counters specified in parameters are already started.
  147. fn counter_start(
  148. &self,
  149. counter_idx_base: usize,
  150. counter_idx_mask: usize,
  151. start_flags: usize,
  152. initial_value: u64,
  153. ) -> SbiRet;
  154. /// Stop or disable a set of counters on the calling HART.
  155. ///
  156. /// # Parameters
  157. ///
  158. /// The `counter_idx_base` and `counter_idx_mask` parameters represent the set of counters.
  159. /// The bit definitions of the `stop_flags` parameter are shown in the table below:
  160. ///
  161. /// | Flag Name | Bits | Description
  162. /// |:------------------------|:-----------|:------------
  163. /// | SBI_PMU_STOP_FLAG_RESET | 0:0 | Reset the counter to event mapping.
  164. /// | _RESERVED_ | 1:(XLEN-1) | *All non-zero values are reserved for future use.*
  165. ///
  166. /// # Return value
  167. ///
  168. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  169. ///
  170. /// | Return code | Description
  171. /// |:------------------------|:----------------------------------------------
  172. /// | SBI_SUCCESS | counter stopped successfully.
  173. /// | SBI_ERR_INVALID_PARAM | some of the counters specified in parameters are invalid.
  174. /// | SBI_ERR_ALREADY_STOPPED | some of the counters specified in parameters are already stopped.
  175. fn counter_stop(
  176. &self,
  177. counter_idx_base: usize,
  178. counter_idx_mask: usize,
  179. stop_flags: usize,
  180. ) -> SbiRet;
  181. /// Provide the current value of a firmware counter in `SbiRet.value`.
  182. ///
  183. /// # Parameters
  184. ///
  185. /// This function should be only used to read a firmware counter. It will return an error
  186. /// when user provides a hardware counter in `counter_idx` parameter.
  187. ///
  188. /// # Return value
  189. ///
  190. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  191. ///
  192. /// | Return code | Description
  193. /// |:------------------------|:----------------------------------------------
  194. /// | SBI_SUCCESS | firmware counter read successfully.
  195. /// | SBI_ERR_INVALID_PARAM | `counter_idx` points to a hardware counter or an invalid counter.
  196. fn counter_fw_read(&self, counter_idx: usize) -> SbiRet;
  197. }
  198. // TODO: all the events here
  199. static PMU: AmoOnceRef<dyn Pmu> = AmoOnceRef::new();
  200. pub fn init_pmu(pmu: &'static dyn Pmu) {
  201. if !PMU.try_call_once(pmu) {
  202. panic!("load sbi module when already loaded")
  203. }
  204. }
  205. #[inline]
  206. pub(crate) fn probe_pmu() -> bool {
  207. PMU.get().is_some()
  208. }
  209. #[inline]
  210. pub(crate) fn num_counters() -> SbiRet {
  211. if let Some(obj) = PMU.get() {
  212. // Returns the number of counters (both hardware and firmware) in sbiret.value
  213. // and always returns SBI_SUCCESS in sbiret.error.
  214. return SbiRet::ok(obj.num_counters());
  215. }
  216. SbiRet::not_supported()
  217. }
  218. #[inline]
  219. pub(crate) fn counter_get_info(counter_idx: usize) -> SbiRet {
  220. if let Some(obj) = PMU.get() {
  221. return obj.counter_get_info(counter_idx);
  222. }
  223. SbiRet::not_supported()
  224. }
  225. #[inline]
  226. pub(crate) fn counter_config_matching(
  227. counter_idx_base: usize,
  228. counter_idx_mask: usize,
  229. config_flags: usize,
  230. event_idx: usize,
  231. event_data: u64,
  232. ) -> SbiRet {
  233. if let Some(obj) = PMU.get() {
  234. return obj.counter_config_matching(
  235. counter_idx_base,
  236. counter_idx_mask,
  237. config_flags,
  238. event_idx,
  239. event_data,
  240. );
  241. }
  242. SbiRet::not_supported()
  243. }
  244. #[inline]
  245. pub(crate) fn counter_start(
  246. counter_idx_base: usize,
  247. counter_idx_mask: usize,
  248. start_flags: usize,
  249. initial_value: u64,
  250. ) -> SbiRet {
  251. if let Some(obj) = PMU.get() {
  252. return obj.counter_start(
  253. counter_idx_base,
  254. counter_idx_mask,
  255. start_flags,
  256. initial_value,
  257. );
  258. }
  259. SbiRet::not_supported()
  260. }
  261. #[inline]
  262. pub(crate) fn counter_stop(
  263. counter_idx_base: usize,
  264. counter_idx_mask: usize,
  265. stop_flags: usize,
  266. ) -> SbiRet {
  267. if let Some(obj) = PMU.get() {
  268. return obj.counter_stop(counter_idx_base, counter_idx_mask, stop_flags);
  269. }
  270. SbiRet::not_supported()
  271. }
  272. #[inline]
  273. pub(crate) fn counter_fw_read(counter_idx: usize) -> SbiRet {
  274. if let Some(obj) = PMU.get() {
  275. return obj.counter_fw_read(counter_idx);
  276. }
  277. SbiRet::not_supported()
  278. }