sbi_rt/
srst.rs

1//! Chapter 10. System Reset Extension (EID #0x53525354 "SRST")
2
3use crate::binary::sbi_call_2;
4
5use sbi_spec::{
6    binary::SbiRet,
7    srst::{
8        EID_SRST, RESET_REASON_NO_REASON, RESET_REASON_SYSTEM_FAILURE, RESET_TYPE_COLD_REBOOT,
9        RESET_TYPE_SHUTDOWN, RESET_TYPE_WARM_REBOOT, SYSTEM_RESET,
10    },
11};
12
13/// Reset the system based on provided `reset_type` and `reset_reason`.
14///
15/// This is a synchronous call and does not return if it succeeds.
16///
17/// # Warm reboot and cold reboot
18///
19/// When supervisor software is running natively, the SBI implementation is machine mode firmware.
20/// In this case, shutdown is equivalent to physical power down of the entire system, and
21/// cold reboot is equivalent to a physical power cycle of the entire system.
22/// Further, warm reboot is equivalent to a power cycle of the main processor and parts of the system
23/// but not the entire system.
24///
25/// For example, on a server class system with a BMC (board management controller),
26/// a warm reboot will not power cycle the BMC whereas a cold reboot will definitely power cycle the BMC.
27///
28/// When supervisor software is running inside a virtual machine, the SBI implementation is a hypervisor.
29/// The shutdown, cold reboot and warm reboot will behave functionally the same as the native case but might
30/// not result in any physical power changes.
31///
32/// This function is defined in RISC-V SBI Specification chapter 10.1.
33#[inline]
34pub fn system_reset<T, R>(reset_type: T, reset_reason: R) -> SbiRet
35where
36    T: ResetType,
37    R: ResetReason,
38{
39    sbi_call_2(
40        EID_SRST,
41        SYSTEM_RESET,
42        reset_type.raw() as _,
43        reset_reason.raw() as _,
44    )
45}
46
47/// A valid type for system reset.
48pub trait ResetType {
49    /// Get a raw value to pass to SBI environment.
50    fn raw(&self) -> u32;
51}
52
53#[cfg(feature = "integer-impls")]
54impl ResetType for u32 {
55    #[inline]
56    fn raw(&self) -> u32 {
57        *self
58    }
59}
60
61#[cfg(feature = "integer-impls")]
62impl ResetType for i32 {
63    #[inline]
64    fn raw(&self) -> u32 {
65        u32::from_ne_bytes(i32::to_ne_bytes(*self))
66    }
67}
68
69/// A valid reason for system reset.
70pub trait ResetReason {
71    /// Get a raw value to pass to SBI environment.
72    fn raw(&self) -> u32;
73}
74
75#[cfg(feature = "integer-impls")]
76impl ResetReason for u32 {
77    #[inline]
78    fn raw(&self) -> u32 {
79        *self
80    }
81}
82
83#[cfg(feature = "integer-impls")]
84impl ResetReason for i32 {
85    #[inline]
86    fn raw(&self) -> u32 {
87        u32::from_ne_bytes(i32::to_ne_bytes(*self))
88    }
89}
90
91macro_rules! define_reset_param {
92    ($($struct:ident($value:expr): $trait:ident #[$doc:meta])*) => {
93        $(
94            #[derive(Clone, Copy, Debug)]
95            #[$doc]
96            pub struct $struct;
97            impl $trait for $struct {
98                #[inline]
99                fn raw(&self) -> u32 {
100                    $value
101                }
102            }
103        )*
104    };
105}
106
107define_reset_param! {
108    Shutdown(RESET_TYPE_SHUTDOWN): ResetType /// Shutdown as a reset type.
109    ColdReboot(RESET_TYPE_COLD_REBOOT): ResetType /// Cold reboot as a reset type.
110    WarmReboot(RESET_TYPE_WARM_REBOOT): ResetType /// Warm reboot as a reset type.
111    NoReason(RESET_REASON_NO_REASON): ResetReason /// No reason as a reset reason.
112    SystemFailure(RESET_REASON_SYSTEM_FAILURE): ResetReason /// System failure as a reset reason.
113}