summary refs log tree commit diff stats
path: root/rust
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-09-29 17:49:32 +0200
committerStefan Hajnoczi <stefanha@redhat.com>2025-10-01 11:22:07 -0400
commitfe791b4004cbdc64bee6cb69ba4a98513879945d (patch)
tree54650b554a790e7809ee0cc1ccd386e05044bc2c /rust
parent54140102d2e57bd2e34823e4537d2191cdcd5f08 (diff)
downloadfocaccia-qemu-fe791b4004cbdc64bee6cb69ba4a98513879945d.tar.gz
focaccia-qemu-fe791b4004cbdc64bee6cb69ba4a98513879945d.zip
rust: qdev: add minimal clock bindings
Add the minimal support that is needed by pl011's event and tracepoint.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-ID: <20250929154938.594389-11-pbonzini@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Diffstat (limited to 'rust')
-rw-r--r--rust/hw/core/src/qdev.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/rust/hw/core/src/qdev.rs b/rust/hw/core/src/qdev.rs
index a4493dbf01..c3097a284d 100644
--- a/rust/hw/core/src/qdev.rs
+++ b/rust/hw/core/src/qdev.rs
@@ -411,6 +411,39 @@ where
 
 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 period(&self) -> u64 {
+        // SAFETY: Clock is returned by init_clock_in with zero value for period
+        unsafe { &*self.0.as_ptr() }.period
+    }
+
+    pub const fn hz(&self) -> u64 {
+        Self::period_to_hz(self.period())
+    }
+}
+
 unsafe impl ObjectType for Clock {
     type Class = ObjectClass;
     const TYPE_NAME: &'static CStr =