From 4317548a2f7cd2eeae53289b55f8a98605bb99fe Mon Sep 17 00:00:00 2001 From: rayylee Date: Mon, 29 Apr 2024 23:50:40 +0800 Subject: [PATCH 1/2] riscv: fix the timer and cpu frequency are not related Signed-off-by: rayylee --- modules/axhal/Cargo.toml | 1 + .../src/platform/riscv64_qemu_virt/mod.rs | 18 +++++++++++ .../src/platform/riscv64_qemu_virt/time.rs | 32 +++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/modules/axhal/Cargo.toml b/modules/axhal/Cargo.toml index 632b2b9063..4ed91e778c 100644 --- a/modules/axhal/Cargo.toml +++ b/modules/axhal/Cargo.toml @@ -49,6 +49,7 @@ raw-cpuid = "11.0" [target.'cfg(any(target_arch = "riscv32", target_arch = "riscv64"))'.dependencies] riscv = "0.10" sbi-rt = { version = "0.0.2", features = ["legacy"] } +of = { path = "../../crates/of"} [target.'cfg(target_arch = "aarch64")'.dependencies] aarch64-cpu = "9.3" diff --git a/modules/axhal/src/platform/riscv64_qemu_virt/mod.rs b/modules/axhal/src/platform/riscv64_qemu_virt/mod.rs index 24c14e5b38..b202a26349 100644 --- a/modules/axhal/src/platform/riscv64_qemu_virt/mod.rs +++ b/modules/axhal/src/platform/riscv64_qemu_virt/mod.rs @@ -11,6 +11,8 @@ pub mod irq; #[cfg(feature = "smp")] pub mod mp; +use of; + extern "C" { fn trap_vector_base(); fn rust_main(cpu_id: usize, dtb: usize); @@ -18,10 +20,26 @@ extern "C" { fn rust_main_secondary(cpu_id: usize); } +fn init_board_info(dtb: usize) { + unsafe { + of::init_fdt_ptr(dtb as *const u8); + } + let mut of_cpus = of::cpus(); + let freq = { + if let Some(cpu) = of_cpus.nth(0) { + cpu.timebase_frequency() + } else { + axconfig::TIMER_FREQUENCY + } + }; + self::time::init_cpu_freq(freq as u64); +} + unsafe extern "C" fn rust_entry(cpu_id: usize, dtb: usize) { crate::mem::clear_bss(); crate::cpu::init_primary(cpu_id); crate::arch::set_trap_vector_base(trap_vector_base as usize); + init_board_info(dtb); rust_main(cpu_id, dtb); } diff --git a/modules/axhal/src/platform/riscv64_qemu_virt/time.rs b/modules/axhal/src/platform/riscv64_qemu_virt/time.rs index f206785fb1..a679d79e25 100644 --- a/modules/axhal/src/platform/riscv64_qemu_virt/time.rs +++ b/modules/axhal/src/platform/riscv64_qemu_virt/time.rs @@ -1,11 +1,39 @@ +use lazy_init::LazyInit; use riscv::register::time; +static CPU_FREQ: LazyInit = LazyInit::new(); +static LOOPS_PRE_TICK: LazyInit = LazyInit::new(); const NANOS_PER_TICK: u64 = crate::time::NANOS_PER_SEC / axconfig::TIMER_FREQUENCY as u64; +// Initializes the frequency of cpu. +#[inline] +pub fn init_cpu_freq(freq: u64) { + if !CPU_FREQ.is_init() { + CPU_FREQ.init_by(freq); + } + + if !LOOPS_PRE_TICK.is_init() { + LOOPS_PRE_TICK.init_by(freq / axconfig::TIMER_FREQUENCY as u64); + } +} + +/// Returns the frequency of cpu. +#[inline] +#[allow(unused)] +pub fn cpu_freq() -> u64 { + *CPU_FREQ +} + +/// Returns loops per tick. +#[inline] +pub fn loops_pre_tick() -> u64 { + *LOOPS_PRE_TICK +} + /// Returns the current clock time in hardware ticks. #[inline] pub fn current_ticks() -> u64 { - time::read() as u64 + time::read() as u64 / loops_pre_tick() } /// Converts hardware ticks to nanoseconds. @@ -25,7 +53,7 @@ pub const fn nanos_to_ticks(nanos: u64) -> u64 { /// A timer interrupt will be triggered at the given deadline (in nanoseconds). #[cfg(feature = "irq")] pub fn set_oneshot_timer(deadline_ns: u64) { - sbi_rt::set_timer(nanos_to_ticks(deadline_ns)); + sbi_rt::set_timer(nanos_to_ticks(deadline_ns) * loops_pre_tick()); } pub(super) fn init_percpu() { From 7e150371f84bea8e4448265c971387e968f9d12a Mon Sep 17 00:00:00 2001 From: rayylee Date: Tue, 30 Apr 2024 17:31:31 +0800 Subject: [PATCH 2/2] test: change timeout to 120s for monolithic test Signed-off-by: rayylee --- scripts/test/app_test_for_monolithic.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/test/app_test_for_monolithic.sh b/scripts/test/app_test_for_monolithic.sh index 390d1eed02..28f81c94ac 100755 --- a/scripts/test/app_test_for_monolithic.sh +++ b/scripts/test/app_test_for_monolithic.sh @@ -2,7 +2,7 @@ APP= ROOT=$(realpath $(dirname $0))/../../ -TIMEOUT=60s +TIMEOUT=120s EXIT_STATUS=0 S_PASS=0 @@ -51,7 +51,7 @@ function run_and_compare() { EXIT_NORMALLY="System halted with exit code 0" if [ `grep -c "$EXIT_NORMALLY" $actual` -eq '0' ];then return $S_FAILED - else + else return $S_PASS fi }