You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.8 KiB
Rust
119 lines
3.8 KiB
Rust
5 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
5 years ago
|
//
|
||
4 years ago
|
// Copyright (c) 2018-2021 Andre Richter <andre.o.richter@gmail.com>
|
||
5 years ago
|
|
||
5 years ago
|
//! Architectural timer primitives.
|
||
4 years ago
|
//!
|
||
|
//! # Orientation
|
||
|
//!
|
||
|
//! Since arch modules are imported into generic modules using the path attribute, the path of this
|
||
|
//! file is:
|
||
|
//!
|
||
|
//! crate::time::arch_time
|
||
5 years ago
|
|
||
5 years ago
|
use crate::{time, warn};
|
||
5 years ago
|
use core::time::Duration;
|
||
4 years ago
|
use cortex_a::{barrier, regs::*};
|
||
5 years ago
|
|
||
5 years ago
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Private Definitions
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
5 years ago
|
const NS_PER_S: u64 = 1_000_000_000;
|
||
|
|
||
5 years ago
|
/// ARMv8 Generic Timer.
|
||
4 years ago
|
struct GenericTimer;
|
||
5 years ago
|
|
||
5 years ago
|
//--------------------------------------------------------------------------------------------------
|
||
5 years ago
|
// Global instances
|
||
5 years ago
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
5 years ago
|
static TIME_MANAGER: GenericTimer = GenericTimer;
|
||
|
|
||
4 years ago
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Private Code
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
impl GenericTimer {
|
||
|
#[inline(always)]
|
||
|
fn read_cntpct(&self) -> u64 {
|
||
|
// Prevent that the counter is read ahead of time due to out-of-order execution.
|
||
|
unsafe { barrier::isb(barrier::SY) };
|
||
|
CNTPCT_EL0.get()
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Public Code
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
/// Return a reference to the time manager.
|
||
|
pub fn time_manager() -> &'static impl time::interface::TimeManager {
|
||
|
&TIME_MANAGER
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// OS Interface Code
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
impl time::interface::TimeManager for GenericTimer {
|
||
5 years ago
|
fn resolution(&self) -> Duration {
|
||
5 years ago
|
Duration::from_nanos(NS_PER_S / (CNTFRQ_EL0.get() as u64))
|
||
|
}
|
||
|
|
||
|
fn uptime(&self) -> Duration {
|
||
4 years ago
|
let current_count: u64 = self.read_cntpct() * NS_PER_S;
|
||
5 years ago
|
let frq: u64 = CNTFRQ_EL0.get() as u64;
|
||
|
|
||
|
Duration::from_nanos(current_count / frq)
|
||
|
}
|
||
|
|
||
|
fn spin_for(&self, duration: Duration) {
|
||
|
// Instantly return on zero.
|
||
|
if duration.as_nanos() == 0 {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Calculate the register compare value.
|
||
4 years ago
|
let frq = CNTFRQ_EL0.get();
|
||
5 years ago
|
let x = match frq.checked_mul(duration.as_nanos() as u64) {
|
||
|
None => {
|
||
|
warn!("Spin duration too long, skipping");
|
||
|
return;
|
||
|
}
|
||
|
Some(val) => val,
|
||
|
};
|
||
|
let tval = x / NS_PER_S;
|
||
|
|
||
|
// Check if it is within supported bounds.
|
||
|
let warn: Option<&str> = if tval == 0 {
|
||
|
Some("smaller")
|
||
4 years ago
|
// The upper 32 bits of CNTP_TVAL_EL0 are reserved.
|
||
5 years ago
|
} else if tval > u32::max_value().into() {
|
||
|
Some("bigger")
|
||
|
} else {
|
||
|
None
|
||
|
};
|
||
|
|
||
|
if let Some(w) = warn {
|
||
|
warn!(
|
||
|
"Spin duration {} than architecturally supported, skipping",
|
||
|
w
|
||
|
);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Set the compare value register.
|
||
4 years ago
|
CNTP_TVAL_EL0.set(tval);
|
||
5 years ago
|
|
||
|
// Kick off the counting. // Disable timer interrupt.
|
||
|
CNTP_CTL_EL0.modify(CNTP_CTL_EL0::ENABLE::SET + CNTP_CTL_EL0::IMASK::SET);
|
||
|
|
||
5 years ago
|
// ISTATUS will be '1' when cval ticks have passed. Busy-check it.
|
||
|
while !CNTP_CTL_EL0.matches_all(CNTP_CTL_EL0::ISTATUS::SET) {}
|
||
5 years ago
|
|
||
|
// Disable counting again.
|
||
|
CNTP_CTL_EL0.modify(CNTP_CTL_EL0::ENABLE::CLEAR);
|
||
|
}
|
||
|
}
|