Re: [PATCH 7/9] rust: time: add arch_timer_get_rate wrapper

From: Gary Guo

Date: Tue Sep 22 2026 - 15:58:22 EST


On Tue Sep 15, 2026 at 11:57 AM BST, Laura Nao wrote:
> From: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
>
> Provide a safe Rust wrapper for arch_timer_get_rate().
>
> The Rust binding calls a C helper that returns 0 when the ARM
> architectural timer is not available or not yet initialized. Map this to
> Option<u32> to make the absence of a valid rate explicit to Rust callers.
>
> This allows Rust drivers to query the system timer frequency and
> select appropriate time sources when programming hardware timeouts.
>
> Signed-off-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
> Signed-off-by: Laura Nao <laura.nao@xxxxxxxxxxxxx>
> ---
> rust/helpers/time.c | 6 ++++++
> rust/kernel/time.rs | 30 ++++++++++++++++++++++++++++++
> 2 files changed, 36 insertions(+)
>
> diff --git a/rust/helpers/time.c b/rust/helpers/time.c
> index 32f495970493..70fda31bd758 100644
> --- a/rust/helpers/time.c
> +++ b/rust/helpers/time.c
> @@ -1,5 +1,6 @@
> // SPDX-License-Identifier: GPL-2.0
>
> +#include <clocksource/arm_arch_timer.h>

The header is from arm_arch_timer. I wonder if we want this to be exported from
somewhere like kernel::arch::arm::time?

Best,
Gary

> #include <linux/delay.h>
> #include <linux/ktime.h>
> #include <linux/timekeeping.h>
> @@ -38,3 +39,8 @@ __rust_helper void rust_helper_udelay(unsigned long usec)
> {
> udelay(usec);
> }
> +
> +__rust_helper u32 rust_helper_arch_timer_get_rate(void)
> +{
> + return arch_timer_get_rate();
> +}
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 6c0a5e8090d0..6c48ce21984a 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -423,6 +423,36 @@ fn div(self, rhs: Self) -> Self::Output {
> }
> }
>
> +/// Returns the ARM architecture timer frequency in Hz, if available.
> +///
> +/// This function queries the system-wide ARM architecture timer frequency.
> +/// The architecture timer provides a consistent time source across all CPU cores.
> +///
> +/// Returns `None` if:
> +/// - The ARM architecture timer is not available (`CONFIG_ARM_ARCH_TIMER` not enabled)
> +/// - The timer rate is zero (not initialized)
> +///
> +/// # Examples
> +///
> +/// ```
> +/// use kernel::time::arch_timer_get_rate;
> +///
> +/// if let Some(rate) = arch_timer_get_rate() {
> +/// // Use `rate`.
> +/// }
> +/// ```
> +pub fn arch_timer_get_rate() -> Option<u32> {
> + // SAFETY: The C helper is available in all configs; it calls
> + // `arch_timer_get_rate()`, which falls back to an inline stub returning 0
> + // when CONFIG_ARM_ARCH_TIMER is disabled.
> + let rate = unsafe { bindings::arch_timer_get_rate() };
> + if rate == 0 {
> + None
> + } else {
> + Some(rate)
> + }
> +}
> +
> impl Delta {
> /// A span of time equal to zero.
> pub const ZERO: Self = Self { value: 0 };