Skip to contentSkip to conversation

rust: cell: add full example of declaring a SysBusDevice

PATCH In review Paolo Bonzini 2 participants 3 messages Feb 28, 2025 – Mar 4, 2025 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: cell: add full example of declaring a SysBusDevice

---
 rust/qemu-api/src/cell.rs | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
rust/qemu-api/src/cell.rs +30 -0
index 6db678161ff..13e267ad355 100644
@@ -73,6 +73,36 @@
 //! QEMU device implementations is usually incorrect and can lead to
 //! thread-safety issues.
 //!
+//! ### Example
+//!
+//! ```
+//! # use qemu_api::prelude::*;
+//! # use qemu_api::{c_str, cell::BqlRefCell, irq::InterruptSource, irq::IRQState};
+//! # use qemu_api::{sysbus::SysBusDevice, qom::Owned, qom::ParentField};
+//! # const N_GPIOS: usize = 8;
+//! # struct PL061Registers { /* ... */ }
+//! # unsafe impl ObjectType for PL061Device {
+//! #     type Class = <SysBusDevice as ObjectType>::Class;
+//! #     const TYPE_NAME: &'static std::ffi::CStr = c_str!("pl061");
+//! # }
+//! struct PL061Device {
+//!     parent_obj: ParentField<SysBusDevice>,
+//!
+//!     // Configuration is read-only after initialization
+//!     pullups: u32,
+//!     pulldowns: u32,
+//!
+//!     // Same for sub-objects of the device
+//!     out: [Owned<IRQState>; N_GPIOS],
+//!
+//!     // Single values shared with C code use BqlCell, in this case via InterruptSource
+//!     interrupt: InterruptSource,
+//!
+//!     // Larger state accessed by device methods uses BqlRefCell or Mutex
+//!     registers: BqlRefCell<PL061Registers>,
+//! }
+//! ```
+//!
 //! ### `BqlCell<T>`
 //!
 //! [`BqlCell<T>`] implements interior mutability by moving values in and out of
Signature · 1 line
2.48.1
Reviewed-by on v1 1/1
#1 lore raw

rust/qemu-api/src/cell.rs line 88

Quoted 13 lines
rust/qemu-api/src/cell.rs +13 -0
+//! ### Example
+//!
+//! ```
+//! # use qemu_api::prelude::*;
+//! # use qemu_api::{c_str, cell::BqlRefCell, irq::InterruptSource, irq::IRQState};
+//! # use qemu_api::{sysbus::SysBusDevice, qom::Owned, qom::ParentField};
+//! # const N_GPIOS: usize = 8;
+//! # struct PL061Registers { /* ... */ }
+//! # unsafe impl ObjectType for PL061Device {
+//! #     type Class = <SysBusDevice as ObjectType>::Class;
+//! #     const TYPE_NAME: &'static std::ffi::CStr = c_str!("pl061");
+//! # }
+//! struct PL061Device {

Maybe PL061State?

rust/qemu-api/src/cell.rs line 96

Quoted 8 lines
rust/qemu-api/src/cell.rs +8 -0
+//!     parent_obj: ParentField<SysBusDevice>,
+//!
+//!     // Configuration is read-only after initialization
+//!     pullups: u32,
+//!     pulldowns: u32,
+//!
+//!     // Same for sub-objects of the device
+//!     out: [Owned<IRQState>; N_GPIOS],

Should this be InterruptSource type?

Just have a quick look at pl061.c:

qdev_init_gpio_out(dev, s->out, N_GPIOS);

So in Rust side, out would be initialized by
DeviceMethods::init_gpio_out().

Others are fine for me ~ with above nits fixed,

#2 lore raw
Quoted 19 lines · Zhao Liu
Quoted 1 line

+//! struct PL061Device {

Maybe PL061State?

rust/qemu-api/src/cell.rs line 96

Quoted 8 lines
rust/qemu-api/src/cell.rs +8 -0
+//!     parent_obj: ParentField<SysBusDevice>,
+//!
+//!     // Configuration is read-only after initialization
+//!     pullups: u32,
+//!     pulldowns: u32,
+//!
+//!     // Same for sub-objects of the device
+//!     out: [Owned<IRQState>; N_GPIOS],

Should this be InterruptSource type?

Just have a quick look at pl061.c:

qdev_init_gpio_out(dev, s->out, N_GPIOS);

Quoted 7 lines

So in Rust side, out would be initialized by
DeviceMethods::init_gpio_out().

Others are fine for me ~ with above nits fixed,

Yes, all good points. Looks like we're set for 10.0 soft freeze. :)

Paolo