[PATCH 08/14] rust: qdev: add minimal clock bindings

Paolo Bonzini posted 14 patches 2 months, 3 weeks ago
There is a newer version of this series
[PATCH 08/14] rust: qdev: add minimal clock bindings
Posted by Paolo Bonzini 2 months, 3 weeks ago
Add the minimal support that is needed by pl011's event and tracepoint.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/qemu-api/src/qdev.rs | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/rust/qemu-api/src/qdev.rs b/rust/qemu-api/src/qdev.rs
index 6d619661ba4..64b961136b2 100644
--- a/rust/qemu-api/src/qdev.rs
+++ b/rust/qemu-api/src/qdev.rs
@@ -449,6 +449,39 @@ fn init_gpio_out(&self, pins: &[InterruptSource]) {
 
 impl<R: ObjectDeref> DeviceMethods for R where R::Target: IsA<DeviceState> {}
 
+impl Clock {
+    pub const PERIOD_1SEC: u64 = bindings::CLOCK_PERIOD_1SEC;
+
+    pub const fn period_from_ns(ns: u64) -> u64 {
+        ns * Self::PERIOD_1SEC / 1_000_000_000
+    }
+
+    pub const fn period_from_hz(hz: u64) -> u64 {
+        if hz == 0 {
+            0
+        } else {
+            Self::PERIOD_1SEC / hz
+        }
+    }
+
+    pub const fn period_to_hz(period: u64) -> u64 {
+        if period == 0 {
+            0
+        } else {
+            Self::PERIOD_1SEC / period
+        }
+    }
+
+    pub const fn get(&self) -> u64 {
+        // SAFETY: Clock is returned by init_clock_in with zero value for period
+        unsafe { &*self.0.as_ptr() }.period
+    }
+
+    pub const fn get_hz(&self) -> u64 {
+        Self::period_to_hz(self.get())
+    }
+}
+
 unsafe impl ObjectType for Clock {
     type Class = ObjectClass;
     const TYPE_NAME: &'static CStr =
-- 
2.50.1
Re: [PATCH 08/14] rust: qdev: add minimal clock bindings
Posted by Zhao Liu 2 months, 3 weeks ago
On Fri, Aug 22, 2025 at 02:26:49PM +0200, Paolo Bonzini wrote:
> Date: Fri, 22 Aug 2025 14:26:49 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 08/14] rust: qdev: add minimal clock bindings
> X-Mailer: git-send-email 2.50.1
> 
> Add the minimal support that is needed by pl011's event and tracepoint.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  rust/qemu-api/src/qdev.rs | 33 +++++++++++++++++++++++++++++++++
>  1 file changed, 33 insertions(+) 

...

> +    pub const fn get(&self) -> u64 {

get() sounds too general for Clock obj...maybe get_period()?

> +        // SAFETY: Clock is returned by init_clock_in with zero value for period
> +        unsafe { &*self.0.as_ptr() }.period
> +    }

Otherwise,

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Re: [PATCH 08/14] rust: qdev: add minimal clock bindings
Posted by Manos Pitsidianakis 2 months, 3 weeks ago
On Mon, Aug 25, 2025 at 10:30 AM Zhao Liu <zhao1.liu@intel.com> wrote:
>
> On Fri, Aug 22, 2025 at 02:26:49PM +0200, Paolo Bonzini wrote:
> > Date: Fri, 22 Aug 2025 14:26:49 +0200
> > From: Paolo Bonzini <pbonzini@redhat.com>
> > Subject: [PATCH 08/14] rust: qdev: add minimal clock bindings
> > X-Mailer: git-send-email 2.50.1
> >
> > Add the minimal support that is needed by pl011's event and tracepoint.
> >
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> >  rust/qemu-api/src/qdev.rs | 33 +++++++++++++++++++++++++++++++++
> >  1 file changed, 33 insertions(+)
>
> ...
>
> > +    pub const fn get(&self) -> u64 {
>
> get() sounds too general for Clock obj...maybe get_period()?

(Or just clock.period())

>
> > +        // SAFETY: Clock is returned by init_clock_in with zero value for period
> > +        unsafe { &*self.0.as_ptr() }.period
> > +    }
>
> Otherwise,
>
> Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
>
>
Re: [PATCH 08/14] rust: qdev: add minimal clock bindings
Posted by Paolo Bonzini 2 months, 3 weeks ago
On Mon, Aug 25, 2025 at 9:33 AM Manos Pitsidianakis
<manos.pitsidianakis@linaro.org> wrote:
> > get() sounds too general for Clock obj...maybe get_period()?
>
> (Or just clock.period())

Will do period() and hz().

Paolo