garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /kernel/src/timer.rs

raw

//! The timer: the generic timer's virtual counter, which EL1 can always
//! use. It is tickless: each time a core switches, the scheduler sets the
//! core's timer to what is due next there ([`arm`]): the end of the time
//! slice while another task waits to run, the earliest sleeper's deadline,
//! on core 0 the network's next retry; and turns it off when nothing is.
//! A core with nothing to do sleeps in WFI until an interrupt, however long.

use core::sync::atomic::{AtomicU64, Ordering};

/// Time slices a second, while tasks share a core.
pub const HZ: u64 = 100;
/// The virtual timer's PPI: INTID 27 on every GICv3 system.
pub const INTID: u32 = 27;

/// Timer interrupts taken, by core: what [`crate::selfcheck::idle`] counts.
static FIRED: [AtomicU64; crate::smp::MAX_CORES] = [const { AtomicU64::new(0) }; crate::smp::MAX_CORES];

pub fn frequency() -> u64 {
    let f: u64;
    // SAFETY: reading CNTFRQ_EL0 has no side effects.
    unsafe { core::arch::asm!("mrs {}, cntfrq_el0", out(reg) f, options(nomem, nostack)) };
    f
}

/// A time slice, in ms.
pub const SLICE_MS: u64 = 1000 / HZ;

pub fn start() {
    crate::device::mark_compatible("arm,armv8-timer", "generic timer", crate::device::Class::Clock);
    // Programs may read the virtual counter and its frequency themselves
    // (CNTKCTL_EL1.EL0VCTEN): a clock in nanoseconds with no system call,
    // which Rust's `Instant` reads on every call.
    // SAFETY: only opens EL0's read of CNTVCT_EL0 and CNTFRQ_EL0.
    unsafe {
        core::arch::asm!(
            "mrs {k}, cntkctl_el1",
            "orr {k}, {k}, #2",
            "msr cntkctl_el1, {k}",
            "isb",
            k = out(reg) _,
            options(nomem, nostack),
        );
    }
    arm(Some(now_ms() + SLICE_MS));
    crate::gic::enable_private(INTID);
    crate::gic::enable_private(crate::sched::RESCHEDULE);
}

/// Sets this core's timer to fire at `deadline` (ms since boot; at once if
/// that has passed), or turns it off.
pub fn arm(deadline: Option<u64>) {
    match deadline {
        // SAFETY: the virtual timer's registers: deadline, then enable with
        // its interrupt unmasked.
        Some(ms) => unsafe {
            core::arch::asm!(
                "msr cntv_cval_el0, {at}",
                "mov {at}, #1",
                "msr cntv_ctl_el0, {at}",
                "isb",
                at = inout(reg) ms.saturating_mul(frequency() / 1000) => _,
                options(nomem, nostack),
            );
        },
        // SAFETY: disabling the timer also lowers its interrupt.
        None => unsafe { core::arch::asm!("msr cntv_ctl_el0, xzr", "isb", options(nomem, nostack)) },
    }
}

/// The timer's interrupt: counted. The switch that follows sets the next.
pub fn tick() {
    FIRED[crate::arch::core_id()].fetch_add(1, Ordering::Relaxed);
}

/// Timer interrupts core `core` has taken.
pub fn fired(core: usize) -> u64 {
    FIRED[core].load(Ordering::Relaxed)
}

/// Core 0's timer interrupts: while its boot task runs, which keeps it
/// ticking, a tick each [`SLICE_MS`].
pub fn ticks() -> u64 {
    fired(0)
}

/// Milliseconds since the counter started.
pub fn now_ms() -> u64 {
    let count: u64;
    // SAFETY: reading the virtual counter has no side effects.
    unsafe { core::arch::asm!("isb", "mrs {}, cntvct_el0", out(reg) count, options(nomem, nostack)) };
    count / (frequency() / 1000)
}