On Tue, 15 Sep 2026 12:57:40 +0200 Laura Nao <[email protected]> wrote:
> From: Deborah Brouwer <[email protected]> > > 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 <[email protected]> > Signed-off-by: Laura Nao <[email protected]> > --- > rust/helpers/time.c | 6 ++++++ > rust/kernel/time.rs | 30 ++++++++++++++++++++++++++++++ > 2 files changed, 36 insertions(+) (snip) > +/// 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. A SAFETY comment states why the unsafe call is sound. This one explains why the helper is available in all configs. How about: // SAFETY: It is always safe to call `arch_timer_get_rate()`. It just returns a variable without any lock. rust/kernel/time.rs already does this for `ktime_get()` and `ktime_to_us()`. > + 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 }; > This puts the new function between `impl ops::Div for Delta` and `impl Delta`, so it splits the `Delta` blocks. `msecs_to_jiffies()` is the only other free function in this file and it is at the top, next to the type aliases. Please put `arch_timer_get_rate()` there,
