Skip to contentSkip to conversation

rust: hw: core: Add the BusState of rust version

RFC Superseded chenmiao 3 participants 4 messages Oct 25, 2025 – Oct 27, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

chenmiao

author
#0 lore raw

RFC 1/5 rust: hw: core: Add the BusState of rust version

A Rust version implementation has been designed for BusState, which will be used for the subsequent I2CBus implementation.

---
 rust/hw/core/meson.build |  1 +
 rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
 rust/hw/core/src/lib.rs  |  3 +++
 3 files changed, 55 insertions(+)
 create mode 100644 rust/hw/core/src/bus.rs
rust/hw/core/meson.build +1 -0
index 1560dd2..efcda50 100644
@@ -50,6 +50,7 @@ _hwcore_rs = static_library(
     [
       'src/lib.rs',
       'src/bindings.rs',
+      'src/bus.rs',
       'src/irq.rs',
       'src/qdev.rs',
       'src/sysbus.rs',
rust/hw/core/src/bus.rs new file +51 -0

51 lines not on this page · raw message

rust/hw/core/src/lib.rs +3 -0
index b40801e..10cc516 100644
@@ -13,3 +13,6 @@
 
 mod sysbus;
 pub use sysbus::*;
+
+mod bus;
+pub use bus::*;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · chenmiao

Date: Sat, 25 Oct 2025 12:38:49 +0000
From: chenmiao <[email protected]>
Subject: [RFC 1/5] rust: hw: core: Add the BusState of rust version
X-Mailer: git-send-email 2.43.0

A Rust version implementation has been designed for BusState, which will be used for the subsequent I2CBus implementation.

Quoted 7 lines
---
 rust/hw/core/meson.build |  1 +
 rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
 rust/hw/core/src/lib.rs  |  3 +++
 3 files changed, 55 insertions(+)
 create mode 100644 rust/hw/core/src/bus.rs

After a quick glance, I think this BusState is implemented quite well.

Only a few minor nits inline:

...

rust/hw/core/src/bus.rs line 36

Quoted 9 lines
rust/hw/core/src/bus.rs new file +9 -0
+pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
+
+impl BusClass {
+    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
+        self.parent_class.class_init::<T>();
+    }
+}
+
+pub trait BusStateMethods: ObjectDeref

This can be named BusMethods - just like DeviceMethods did.

rust/hw/core/src/bus.rs line 40

Quoted 4 lines
rust/hw/core/src/bus.rs new file +4 -0
+where
+    Self::Target: IsA<BusState>,
+{
+    fn bus_realize(&self) {

"bus_" prefix is not needed:

dummy_bus.realize() is clear and enough.

Quoted 1 line

+ assert!(bql::is_locked());

It's better to add safety comment from beginning (// SAFETY: xxx).

rust/hw/core/src/bus.rs line 49

Quoted 8 lines
rust/hw/core/src/bus.rs new file +8 -0
+        unsafe {
+            bindings::qbus_realize(
+                self.upcast().as_mut_ptr(),
+                addr_of_mut!(util::bindings::error_fatal),
+            );
+        }
+    }
+}

Regards,
Zhao

author
#2 lore raw
Quoted 52 lines · Zhao Liu
Quoted 8 lines · chenmiao

Date: Sat, 25 Oct 2025 12:38:49 +0000
From: chenmiao <[email protected]>
Subject: [RFC 1/5] rust: hw: core: Add the BusState of rust version
X-Mailer: git-send-email 2.43.0

A Rust version implementation has been designed for BusState, which will be used for the subsequent I2CBus implementation.

Quoted 7 lines
---
  rust/hw/core/meson.build |  1 +
  rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
  rust/hw/core/src/lib.rs  |  3 +++
  3 files changed, 55 insertions(+)

create mode 100644 rust/hw/core/src/bus.rs

After a quick glance, I think this BusState is implemented quite well.

Only a few minor nits inline:

...

rust/hw/core/src/bus.rs line 36

Quoted 9 lines
rust/hw/core/src/bus.rs new file +9 -0
+pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
+
+impl BusClass {
+    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
+        self.parent_class.class_init::<T>();
+    }
+}
+
+pub trait BusStateMethods: ObjectDeref

This can be named BusMethods - just like DeviceMethods did.

rust/hw/core/src/bus.rs line 40

Quoted 4 lines
rust/hw/core/src/bus.rs new file +4 -0
+where
+    Self::Target: IsA<BusState>,
+{
+    fn bus_realize(&self) {

"bus_" prefix is not needed:

dummy_bus.realize() is clear and enough.

Quoted 1 line

+ assert!(bql::is_locked());

It's better to add safety comment from beginning (// SAFETY: xxx).

rust/hw/core/src/bus.rs line 49

Quoted 8 lines
rust/hw/core/src/bus.rs new file +8 -0
+        unsafe {
+            bindings::qbus_realize(
+                self.upcast().as_mut_ptr(),
+                addr_of_mut!(util::bindings::error_fatal),
+            );
+        }
+    }
+}
Quoted 2 lines

Regards,
Zhao

Hi Zhao,

Thanks for your review, I'll revisions based on your suggestions after all the patches reviewed.

Regards,

Chen Miao

#3 lore raw
In reply to Zhao Liu
Quoted 9 lines · Zhao Liu
Quoted 8 lines · chenmiao

Date: Sat, 25 Oct 2025 12:38:49 +0000
From: chenmiao <[email protected]>
Subject: [RFC 1/5] rust: hw: core: Add the BusState of rust version
X-Mailer: git-send-email 2.43.0

A Rust version implementation has been designed for BusState, which will be used for the subsequent I2CBus implementation.

Quoted 9 lines
Quoted 7 lines
---
  rust/hw/core/meson.build |  1 +
  rust/hw/core/src/bus.rs  | 51 ++++++++++++++++++++++++++++++++++++++++
  rust/hw/core/src/lib.rs  |  3 +++
  3 files changed, 55 insertions(+)

create mode 100644 rust/hw/core/src/bus.rs

After a quick glance, I think this BusState is implemented quite well.

Thanks, hh. Paolo also provided many good suggestions regarding the implementation of BusState.

Quoted 4 lines

Only a few minor nits inline:

...

Quoted 12 lines

rust/hw/core/src/bus.rs line 36

Quoted 9 lines
rust/hw/core/src/bus.rs new file +9 -0
+pub trait BusStateImpl: DeviceImpl + IsA<BusState> {}
+
+impl BusClass {
+    pub fn class_init<T: BusStateImpl>(self: &mut BusClass) {
+        self.parent_class.class_init::<T>();
+    }
+}
+
+pub trait BusStateMethods: ObjectDeref

This can be named BusMethods - just like DeviceMethods did.

Thank you for pointing this out. I’ve noted the Rust In QEMU docs also include relevant coding styles, and we’ll update the related naming conventions in the next version.

Quoted 9 lines

rust/hw/core/src/bus.rs line 40

Quoted 4 lines
rust/hw/core/src/bus.rs new file +4 -0
+where
+    Self::Target: IsA<BusState>,
+{
+    fn bus_realize(&self) {

"bus_" prefix is not needed:

dummy_bus.realize() is clear and enough.

Ok, qbus_realize() is internally called by the C part during object instantiation, so there is indeed no need for additional exporting.

Quoted 4 lines
Quoted 1 line

+ assert!(bql::is_locked());

It's better to add safety comment from beginning (// SAFETY: xxx).

Get~

Quoted 9 lines

rust/hw/core/src/bus.rs line 49

Quoted 8 lines
rust/hw/core/src/bus.rs new file +8 -0
+        unsafe {
+            bindings::qbus_realize(
+                self.upcast().as_mut_ptr(),
+                addr_of_mut!(util::bindings::error_fatal),
+            );
+        }
+    }
+}
Quoted 3 lines

Regards,
Zhao

Thanks,
Chao