hsm.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. use sbi_spec::binary::SbiRet;
  2. /// Hart State Management extension.
  3. ///
  4. /// The Hart State Management (HSM) Extension introduces a set hart states and a set of functions
  5. /// which allow the supervisor-mode software to request a hart state change.
  6. ///
  7. /// # Hart states
  8. ///
  9. /// The possible hart states along with a unique State ID are as follows:
  10. ///
  11. /// | State ID | State Name | Description
  12. /// |:---------|:-----------|:------------
  13. /// | 0 | `STARTED` | The hart is physically powered-up and executing normally.
  14. /// | 1 | `STOPPED` | The hart is not executing in supervisor-mode or any lower privilege mode. It is probably powered-down by the SBI implementation if the underlying platform has a mechanism to physically power-down harts.
  15. /// | 2 | `START_PENDING` | Some other hart has requested to start (or power-up) the hart from the **STOPPED** state and the SBI implementation is still working to get the hart in the **STARTED** state.
  16. /// | 3 | `STOP_PENDING` | The hart has requested to stop (or power-down) itself from the STARTED state and the SBI implementation is still working to get the hart in the **STOPPED** state.
  17. /// | 4 | `SUSPENDED` | This hart is in a platform specific suspend (or low power) state.
  18. /// | 5 | `SUSPEND_PENDING` | The hart has requestd to put itself in a platform specific low power state from the **STARTED** state and the SBI implementation is still working to get the hart in the platform specific **SUSPENDED** state.
  19. /// | 6 | `RESUME_PENDING` | An interrupt or platform specific hardware event has caused the hart to resume normal execution from the **SUSPENDED** state and the SBI implementation is still working to get the hart in the **STARTED** state.
  20. ///
  21. /// At any point in time, a hart should be in one of the above mentioned hart states.
  22. ///
  23. /// # Topology hart groups
  24. ///
  25. /// A platform can have multiple harts grouped into a hierarchical topology groups (namely cores, clusters, nodes, etc.)
  26. /// with separate platform specific low-power states for each hierarchical group.
  27. /// These platform specific low-power states of hierarchial topology groups can be represented as platform specific suspend states of a hart.
  28. /// A SBI implementation can utilize the suspend states of higher topology groups using one of the following approaches:
  29. ///
  30. /// 1. *Platform-coordinated:* In this approach, when a hart becomes idle the supervisor-mode power-managment software
  31. /// will request deepest suspend state for the hart and higher topology groups.
  32. /// A SBI implementation should choose a suspend state at higher topology group which is:
  33. /// - Not deeper than the specified suspend state
  34. /// - Wake-up latency is not higher than the wake-up latency of the specified suspend state
  35. /// 2. *OS-inititated:* In this approach, the supervisor-mode power-managment software will directly request a suspend state
  36. /// for higher topology group after the last hart in that group becomes idle.
  37. /// When a hart becomes idle, the supervisor-mode power-managment software will always select suspend state for the hart itself
  38. /// but it will select a suspend state for a higher topology group only if the hart is the last running hart in the group.
  39. /// A SBI implementation should:
  40. /// - Never choose a suspend state for higher topology group different from the specified suspend state
  41. /// - Always prefer most recent suspend state requested for higher topology group
  42. ///
  43. /// Ref: [Section 8, RISC-V Supervisor Binary Interface Specification](https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc#8-hart-state-management-extension-eid-0x48534d-hsm)
  44. pub trait Hsm {
  45. /// Request the SBI implementation to start executing the given hart at specified address in supervisor-mode.
  46. ///
  47. /// This call is asynchronous - more specifically, the `hart_start()` may return before target hart
  48. /// starts executing as long as the SBI implemenation is capable of ensuring the return code is accurate.
  49. ///
  50. /// It is recommended that if the SBI implementation is a platform runtime firmware executing in machine-mode (M-mode)
  51. /// then it MUST configure PMP and other the M-mode state before executing in supervisor-mode.
  52. ///
  53. /// # Parameters
  54. ///
  55. /// - The `hartid` parameter specifies the target hart which is to be started.
  56. /// - The `start_addr` parameter points to a runtime-specified physical address, where the hart can start executing in supervisor-mode.
  57. /// - The `opaque` parameter is a `usize` value which will be set in the `a1` register when the hart starts executing at `start_addr`.
  58. ///
  59. /// *NOTE:* A single `usize` parameter is sufficient as `start_addr`,
  60. /// because the hart will start execution in supervisor-mode with the MMU off,
  61. /// hence `start_addr` must be less than XLEN bits wide.
  62. ///
  63. /// # Behavior
  64. ///
  65. /// The target hart jumps to supervisor mode at address specified by `start_addr` with specific register values described as follows.
  66. ///
  67. /// | Register Name | Register Value
  68. /// |:--------------|:--------------
  69. /// | `satp` | 0
  70. /// | `sstatus.SIE` | 0
  71. /// | a0 | hartid
  72. /// | a1 | `opaque` parameter
  73. ///
  74. /// # Return value
  75. ///
  76. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  77. ///
  78. /// | Return code | Description
  79. /// |:------------------------------|:----------------------------------------------
  80. /// | `SbiRet::success()` | Hart was previously in stopped state. It will start executing from `start_addr`.
  81. /// | `SbiRet::invalid_address()` | `start_addr` is not valid, possibly due to the following reasons: it is not a valid physical address, or executable access to the address is prohibited by a physical memory protection mechanism or H-extension G-stage for supervisor-mode.
  82. /// | `SbiRet::invalid_param()` | `hartid` is not a valid hartid as corresponding hart cannot started in supervisor mode.
  83. /// | `SbiRet::already_available()` | The given hartid is already started.
  84. /// | `SbiRet::failed()` | The start request failed for unspecified or unknown other reasons.
  85. fn hart_start(&self, hartid: usize, start_addr: usize, opaque: usize) -> SbiRet;
  86. /// Request the SBI implementation to stop executing the calling hart in supervisor-mode
  87. /// and return its ownership to the SBI implementation.
  88. ///
  89. /// This call is not expected to return under normal conditions.
  90. /// The `hart_stop()` must be called with supervisor-mode interrupts disabled.
  91. ///
  92. /// # Return value
  93. ///
  94. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  95. ///
  96. /// | Error code | Description
  97. /// |:-------------------|:------------
  98. /// | `SbiRet::failed()` | Failed to stop execution of the current hart
  99. fn hart_stop(&self) -> SbiRet;
  100. /// Get the current status (or HSM state id) of the given hart.
  101. ///
  102. /// The harts may transition HSM states at any time due to any concurrent `hart_start()`
  103. /// or `hart_stop()` calls, the return value from this function may not represent the actual state
  104. /// of the hart at the time of return value verification.
  105. ///
  106. /// # Parameters
  107. ///
  108. /// The `hartid` parameter specifies the target hart which status is required.
  109. ///
  110. /// # Return value
  111. ///
  112. /// The possible status values returned in `SbiRet.value` are shown in the table below:
  113. ///
  114. /// | Name | Value | Description
  115. /// |:--------------|:------|:-------------------------
  116. /// | STARTED | 0 | Hart Started
  117. /// | STOPPED | 1 | Hart Stopped
  118. /// | START_PENDING | 2 | Hart start request pending
  119. /// | STOP_PENDING | 3 | Hart stop request pending
  120. ///
  121. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  122. ///
  123. /// | Error code | Description
  124. /// |:--------------------------|:------------
  125. /// | `SbiRet::invalid_param()` | The given `hartid` is not valid
  126. fn hart_get_status(&self, hartid: usize) -> SbiRet;
  127. /// Request the SBI implementation to put the calling hart in a platform specfic suspend (or low power) state
  128. /// specified by the `suspend_type` parameter.
  129. ///
  130. /// The hart will automatically come out of suspended state and resume normal execution
  131. /// when it recieves an interrupt or platform specific hardware event.
  132. ///
  133. /// # Suspend behavior
  134. ///
  135. /// The platform specific suspend states for a hart can be either retentive or non-rententive in nature.
  136. ///
  137. /// A retentive suspend state will preserve hart register and CSR values for all privilege modes,
  138. /// whereas a non-retentive suspend state will not preserve hart register and CSR values.
  139. ///
  140. /// # Resuming
  141. ///
  142. /// Resuming from a retentive suspend state is straight forward and the supervisor-mode software
  143. /// will see SBI suspend call return without any failures.
  144. ///
  145. /// Resuming from a non-retentive suspend state is relatively more involved and requires software
  146. /// to restore various hart registers and CSRs for all privilege modes.
  147. /// Upon resuming from non-retentive suspend state, the hart will jump to supervisor-mode at address
  148. /// specified by `resume_addr` with specific registers values described in the table below:
  149. ///
  150. /// | Register Name | Register Value
  151. /// |:--------------|:--------------
  152. /// | `satp` | 0
  153. /// | `sstatus.SIE` | 0
  154. /// | a0 | hartid
  155. /// | a1 | `opaque` parameter
  156. ///
  157. /// # Parameters
  158. ///
  159. /// The `suspend_type` parameter is 32 bits wide and the possible values are shown in the table below:
  160. ///
  161. /// | Value | Description
  162. /// |:------------------------|:--------------
  163. /// | 0x00000000 | Default retentive suspend
  164. /// | 0x00000001 - 0x0FFFFFFF | _Reserved for future use_
  165. /// | 0x10000000 - 0x7FFFFFFF | Platform specific retentive suspend
  166. /// | 0x80000000 | Default non-retentive suspend
  167. /// | 0x80000001 - 0x8FFFFFFF | _Reserved for future use_
  168. /// | 0x90000000 - 0xFFFFFFFF | Platform specific non-retentive suspend
  169. /// | > 0xFFFFFFFF | _Reserved_
  170. ///
  171. /// The `resume_addr` parameter points to a runtime-specified physical address,
  172. /// where the hart can resume execution in supervisor-mode after a non-retentive
  173. /// suspend.
  174. ///
  175. /// *NOTE:* A single `usize` parameter is sufficient as `resume_addr`,
  176. /// because the hart will resume execution in supervisor-mode with the MMU off,
  177. /// hence `resume_addr` must be less than XLEN bits wide.
  178. ///
  179. /// The `opaque` parameter is an XLEN-bit value which will be set in the `a1`
  180. /// register when the hart resumes exectution at `resume_addr` after a
  181. /// non-retentive suspend.
  182. ///
  183. /// # Return value
  184. ///
  185. /// The possible return error codes returned in `SbiRet.error` are shown in the table below:
  186. ///
  187. /// | Error code | Description
  188. /// |:----------------------------|:------------
  189. /// | `SbiRet::success()` | Hart has suspended and resumed back successfully from a retentive suspend state.
  190. /// | `SbiRet::invalid_param()` | `suspend_type` is not valid.
  191. /// | `SbiRet::not_supported()` | `suspend_type` is valid but not implemented.
  192. /// | `SbiRet::invalid_address()` | `resume_addr` is not valid, possibly due to the following reasons: it is not a valid physical address, or executable access to the address is prohibited by a physical memory protection mechanism or H-extension G-stage for supervisor-mode.
  193. /// | `SbiRet::failed()` | The suspend request failed for unspecified or unknown other reasons.
  194. fn hart_suspend(&self, suspend_type: u32, resume_addr: usize, opaque: usize) -> SbiRet {
  195. let _ = (suspend_type, resume_addr, opaque);
  196. SbiRet::not_supported()
  197. }
  198. }
  199. impl<T: Hsm> Hsm for &T {
  200. #[inline]
  201. fn hart_start(&self, hartid: usize, start_addr: usize, opaque: usize) -> SbiRet {
  202. T::hart_start(self, hartid, start_addr, opaque)
  203. }
  204. #[inline]
  205. fn hart_stop(&self) -> SbiRet {
  206. T::hart_stop(self)
  207. }
  208. #[inline]
  209. fn hart_get_status(&self, hartid: usize) -> SbiRet {
  210. T::hart_get_status(self, hartid)
  211. }
  212. #[inline]
  213. fn hart_suspend(&self, suspend_type: u32, resume_addr: usize, opaque: usize) -> SbiRet {
  214. T::hart_suspend(self, suspend_type, resume_addr, opaque)
  215. }
  216. }