|
| 1 | +//! The implementation of the SBI-based timer driver. |
| 2 | +use core::arch::asm; |
| 3 | +use r3_core::kernel::{traits, Cfg, StaticInterruptHandler}; |
| 4 | +use r3_kernel::{KernelTraits, PortToKernel, System, UTicks}; |
| 5 | +use r3_portkit::tickless::{TicklessCfg, TicklessStateTrait}; |
| 6 | + |
| 7 | +use crate::sbi_timer::cfg::SbiTimerOptions; |
| 8 | + |
| 9 | +/// Implemented on a system type by [`use_sbi_timer!`]. |
| 10 | +/// |
| 11 | +/// # Safety |
| 12 | +/// |
| 13 | +/// Only meant to be implemented by [`use_sbi_timer!`]. |
| 14 | +pub unsafe trait TimerInstance: KernelTraits + SbiTimerOptions { |
| 15 | + // FIXME: Specifying `TicklessCfg::new(...)` here causes a "cycle |
| 16 | + // detected" error |
| 17 | + const TICKLESS_CFG: TicklessCfg; |
| 18 | + |
| 19 | + // FIXME: Specifying `TicklessState<{ Self::TICKLESS_CFG }>` here |
| 20 | + // fails with an error message similar to |
| 21 | + // <https://github.com/rust-lang/rust/issues/72821> |
| 22 | + type TicklessState: TicklessStateTrait; |
| 23 | + |
| 24 | + fn tickless_state() -> *mut Self::TicklessState; |
| 25 | +} |
| 26 | + |
| 27 | +#[cfg(any( |
| 28 | + target_arch = "riscv32", |
| 29 | + target_arch = "riscv64", |
| 30 | + target_arch = "riscv128" |
| 31 | +))] |
| 32 | +trait TimerInstanceExt: TimerInstance { |
| 33 | + #[inline(always)] |
| 34 | + fn time_lo() -> usize { |
| 35 | + let read: usize; |
| 36 | + unsafe { asm!("csrr {read}, time", read = lateout(reg) read) }; |
| 37 | + read |
| 38 | + } |
| 39 | + |
| 40 | + #[cfg(target_arch = "riscv32")] |
| 41 | + #[inline(always)] |
| 42 | + fn time_hi() -> usize { |
| 43 | + let read: usize; |
| 44 | + unsafe { asm!("csrr {read}, timeh", read = lateout(reg) read) }; |
| 45 | + read |
| 46 | + } |
| 47 | + |
| 48 | + #[inline(always)] |
| 49 | + #[cfg(target_arch = "riscv32")] |
| 50 | + fn set_timecmp(value: u64) { |
| 51 | + unsafe { |
| 52 | + asm!( |
| 53 | + "ecall", |
| 54 | + inout("a0") value as u32 => _, // param0 => error |
| 55 | + inout("a1") (value >> 32) as u32 => _, // param => value |
| 56 | + out("a2") _, |
| 57 | + out("a3") _, |
| 58 | + out("a4") _, |
| 59 | + out("a5") _, |
| 60 | + inout("a6") 0 => _, //fid |
| 61 | + inout("a7") 0x54494D45 => _, // eid |
| 62 | + ) |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + #[inline(always)] |
| 67 | + #[cfg(not(target_arch = "riscv32"))] |
| 68 | + fn set_timecmp(value: u64) { |
| 69 | + unsafe { |
| 70 | + asm!( |
| 71 | + "ecall", |
| 72 | + inout("a0") value as usize => _, // param0 => error |
| 73 | + out("a1") _, |
| 74 | + out("a2") _, |
| 75 | + out("a3") _, |
| 76 | + out("a4") _, |
| 77 | + out("a5") _, |
| 78 | + inout("a6") 0 => _, //fid |
| 79 | + inout("a7") 0x54494D45 => _, // eid |
| 80 | + ) |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + #[cfg(not(target_arch = "riscv32"))] |
| 85 | + #[inline(always)] |
| 86 | + fn time() -> u64 { |
| 87 | + Self::time_lo() as u64 |
| 88 | + } |
| 89 | + |
| 90 | + #[cfg(target_arch = "riscv32")] |
| 91 | + #[inline(always)] |
| 92 | + fn time() -> u64 { |
| 93 | + loop { |
| 94 | + let hi1 = Self::time_hi(); |
| 95 | + let lo = Self::time_lo(); |
| 96 | + let hi2 = Self::time_hi(); |
| 97 | + if hi1 == hi2 { |
| 98 | + return lo as u64 | ((hi2 as u64) << 32); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +#[cfg(not(any( |
| 104 | + target_arch = "riscv32", |
| 105 | + target_arch = "riscv64", |
| 106 | + target_arch = "riscv128" |
| 107 | +)))] |
| 108 | +trait TimerInstanceExt: TimerInstance { |
| 109 | + fn time_lo() -> usize { |
| 110 | + unimplemented!("target mismatch") |
| 111 | + } |
| 112 | + |
| 113 | + fn set_timecmp(value: u64) { |
| 114 | + unimplemented!("target mismatch") |
| 115 | + } |
| 116 | + |
| 117 | + fn time() -> u64 { |
| 118 | + unimplemented!("target mismatch") |
| 119 | + } |
| 120 | +} |
| 121 | +impl<T: TimerInstance> TimerInstanceExt for T {} |
| 122 | + |
| 123 | +/// The configuration function. |
| 124 | +pub const fn configure<C, Traits: TimerInstance>(b: &mut Cfg<C>) |
| 125 | +where |
| 126 | + C: ~const traits::CfgInterruptLine<System = System<Traits>>, |
| 127 | +{ |
| 128 | + StaticInterruptHandler::define() |
| 129 | + .line(Traits::INTERRUPT_NUM) |
| 130 | + .start(handle_tick::<Traits>) |
| 131 | + .finish(b); |
| 132 | +} |
| 133 | + |
| 134 | +/// Implements [`crate::Timer::init`] |
| 135 | +#[inline] |
| 136 | +pub fn init<Traits: TimerInstance>() { |
| 137 | + let tcfg = &Traits::TICKLESS_CFG; |
| 138 | + |
| 139 | + // Safety: No context switching during boot |
| 140 | + let tstate = unsafe { &mut *Traits::tickless_state() }; |
| 141 | + |
| 142 | + tstate.reset(tcfg, Traits::time_lo() as u32); |
| 143 | +} |
| 144 | + |
| 145 | +/// Implements [`r3_kernel::PortTimer::tick_count`] |
| 146 | +/// |
| 147 | +/// # Safety |
| 148 | +/// |
| 149 | +/// Only meant to be referenced by `use_sbi_timer!`. |
| 150 | +pub unsafe fn tick_count<Traits: TimerInstance>() -> UTicks { |
| 151 | + let tcfg = &Traits::TICKLESS_CFG; |
| 152 | + |
| 153 | + let hw_tick_count = Traits::time_lo() as u32; |
| 154 | + |
| 155 | + // Safety: CPU Lock protects it from concurrent access |
| 156 | + let tstate = unsafe { &mut *Traits::tickless_state() }; |
| 157 | + tstate.tick_count(tcfg, hw_tick_count) |
| 158 | +} |
| 159 | + |
| 160 | +/// Implements [`r3_kernel::PortTimer::pend_tick`] |
| 161 | +/// |
| 162 | +/// # Safety |
| 163 | +/// |
| 164 | +/// Only meant to be referenced by `use_sbi_timer!`. |
| 165 | +pub unsafe fn pend_tick<Traits: TimerInstance>() { |
| 166 | + Traits::set_timecmp(0); |
| 167 | +} |
| 168 | + |
| 169 | +/// Implements [`r3_kernel::PortTimer::pend_tick_after`] |
| 170 | +/// |
| 171 | +/// # Safety |
| 172 | +/// |
| 173 | +/// Only meant to be referenced by `use_sbi_timer!`. |
| 174 | +pub unsafe fn pend_tick_after<Traits: TimerInstance>(tick_count_delta: UTicks) { |
| 175 | + let tcfg = &Traits::TICKLESS_CFG; |
| 176 | + // Safety: CPU Lock protects it from concurrent access |
| 177 | + let tstate = unsafe { &mut *Traits::tickless_state() }; |
| 178 | + |
| 179 | + let cur_hw_tick_count = Traits::time(); |
| 180 | + let hw_ticks = tstate |
| 181 | + .mark_reference_and_measure(tcfg, cur_hw_tick_count as u32, tick_count_delta) |
| 182 | + .hw_ticks; |
| 183 | + |
| 184 | + let next_hw_tick_count = cur_hw_tick_count + hw_ticks as u64; |
| 185 | + |
| 186 | + Traits::set_timecmp(next_hw_tick_count); |
| 187 | +} |
| 188 | + |
| 189 | +#[inline] |
| 190 | +fn handle_tick<Traits: TimerInstance>(_: usize) { |
| 191 | + let tcfg = &Traits::TICKLESS_CFG; |
| 192 | + |
| 193 | + // Safety: CPU Lock protects it from concurrent access |
| 194 | + let tstate = unsafe { &mut *Traits::tickless_state() }; |
| 195 | + |
| 196 | + let cur_hw_tick_count = Traits::time_lo() as u32; |
| 197 | + tstate.mark_reference(tcfg, cur_hw_tick_count); |
| 198 | + |
| 199 | + // Safety: CPU Lock inactive, an interrupt context |
| 200 | + unsafe { Traits::timer_tick() }; |
| 201 | +} |
0 commit comments