Setup RISC-V AP timer

This commit is contained in:
Zhang Junyang 2025-08-25 22:49:33 +08:00 committed by Tate, Hongliang Tian
parent 7d21144da6
commit 7e7f6741c6
2 changed files with 18 additions and 5 deletions

View File

@ -48,14 +48,14 @@ pub(crate) unsafe fn late_init_on_bsp() {
// SAFETY: This is called before any IPI-related operation is performed.
unsafe { irq::ipi::init() };
// SAFETY: We're on the BSP and we're ready to boot all APs.
unsafe { crate::boot::smp::boot_all_aps() };
// SAFETY: This function is called once and at most once at a proper timing
// in the boot context of the BSP, with no timer-related operations having
// been performed.
unsafe { timer::init() };
// SAFETY: We're on the BSP and we're ready to boot all APs.
unsafe { crate::boot::smp::boot_all_aps() };
// SAFETY:
// 1. All the system device memory have been removed from the builder.
// 2. RISC-V platforms do not have port I/O.
@ -82,6 +82,9 @@ pub(crate) unsafe fn init_on_ap() {
// SAFETY: This is called before any IPI-related operation is performed.
unsafe { irq::ipi::init_current_hart() };
// SAFETY: The caller ensures that this function is only called once here.
unsafe { timer::init_current_hart() };
}
/// Returns the frequency of TSC. The unit is Hz.

View File

@ -26,8 +26,8 @@ static TIMER_INTERVAL: AtomicU64 = AtomicU64::new(0);
/// # Safety
///
/// This function is safe to call on the following conditions:
/// 1. It is called once and at most once at a proper timing in the boot context.
/// 2. It is called before any other public functions of this module is called.
/// 1. It is called once and at most once at a proper timing in the boot context.
/// 2. It is called before any other public functions of this module is called.
pub(super) unsafe fn init() {
TIMEBASE_FREQ.store(
DEVICE_TREE
@ -60,6 +60,16 @@ pub(super) unsafe fn init() {
timer_irq
});
// SAFETY: The caller ensures that this is only called once on the boot hart.
unsafe { init_current_hart() };
}
/// Initializes the timer on the current hart.
///
/// # Safety
///
/// This function must be called in a hart that hasn't called this function.
pub(super) unsafe fn init_current_hart() {
set_next_timer();
// SAFETY: Accessing the `sie` CSR to enable the timer interrupt is safe
// here because this function is only called during timer initialization,