Skip to main content

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]
34#[doc(alias = "sbi_system_reset")]
35pub fn system_reset<T, R>(reset_type: T, reset_reason: R) -> SbiRet
36where
37    T: ResetType,
38    R: ResetReason,
39{
40    sbi_call_2(
41        EID_SRST,
42        SYSTEM_RESET,
43        reset_type.raw() as _,
44        reset_reason.raw() as _,
45    )
46}
47
48/// A valid type for system reset.
49pub trait ResetType {
50    /// Get a raw value to pass to SBI environment.
51    fn raw(&self) -> u32;
52}
53
54#[cfg(feature = "integer-impls")]
55impl ResetType for u32 {
56    #[inline]
57    fn raw(&self) -> u32 {
58        *self
59    }
60}
61
62#[cfg(feature = "integer-impls")]
63impl ResetType for i32 {
64    #[inline]
65    fn raw(&self) -> u32 {
66        u32::from_ne_bytes(i32::to_ne_bytes(*self))
67    }
68}
69
70/// A valid reason for system reset.
71pub trait ResetReason {
72    /// Get a raw value to pass to SBI environment.
73    fn raw(&self) -> u32;
74}
75
76#[cfg(feature = "integer-impls")]
77impl ResetReason for u32 {
78    #[inline]
79    fn raw(&self) -> u32 {
80        *self
81    }
82}
83
84#[cfg(feature = "integer-impls")]
85impl ResetReason for i32 {
86    #[inline]
87    fn raw(&self) -> u32 {
88        u32::from_ne_bytes(i32::to_ne_bytes(*self))
89    }
90}
91
92macro_rules! define_reset_param {
93    ($($struct:ident($value:expr): $trait:ident #[$doc:meta])*) => {
94        $(
95            #[derive(Clone, Copy, Debug)]
96            #[$doc]
97            pub struct $struct;
98            impl $trait for $struct {
99                #[inline]
100                fn raw(&self) -> u32 {
101                    $value
102                }
103            }
104        )*
105    };
106}
107
108define_reset_param! {
109    Shutdown(RESET_TYPE_SHUTDOWN): ResetType /// Shutdown as a reset type.
110    ColdReboot(RESET_TYPE_COLD_REBOOT): ResetType /// Cold reboot as a reset type.
111    WarmReboot(RESET_TYPE_WARM_REBOOT): ResetType /// Warm reboot as a reset type.
112    NoReason(RESET_REASON_NO_REASON): ResetReason /// No reason as a reset reason.
113    SystemFailure(RESET_REASON_SYSTEM_FAILURE): ResetReason /// System failure as a reset reason.
114}