1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#[cfg(feature = "singleton")]
use crate::util::AmoOnceRef;
/// Timer programmer support
pub trait Timer: Send + Sync {
/// Programs the clock for next event after `stime_value` time.
///
/// `stime_value` is in absolute time. This function must clear the pending timer interrupt bit as well.
///
/// If the supervisor wishes to clear the timer interrupt without scheduling the next timer event,
/// it can either request a timer interrupt infinitely far into the future (i.e., (uint64_t)-1),
/// or it can instead mask the timer interrupt by clearing `sie.STIE` CSR bit.
fn set_timer(&self, stime_value: u64);
}
impl<T: Timer> Timer for &T {
#[inline]
fn set_timer(&self, stime_value: u64) {
T::set_timer(self, stime_value)
}
}
#[cfg(feature = "singleton")]
static TIMER: AmoOnceRef<dyn Timer> = AmoOnceRef::new();
#[cfg(feature = "singleton")]
/// Init TIMER module
pub fn init_timer(timer: &'static dyn Timer) {
if !TIMER.try_call_once(timer) {
panic!("load sbi module when already loaded")
}
}
#[cfg(feature = "singleton")]
#[inline]
pub(crate) fn probe_timer() -> bool {
TIMER.get().is_some()
}
#[cfg(feature = "singleton")]
#[inline]
pub(crate) fn set_timer(time_value: u64) -> bool {
if let Some(timer) = TIMER.get() {
timer.set_timer(time_value);
true
} else {
false
}
}