[RFC PATCH v2 4/5] rust/hw/core: Provide some interfaces for the GPIO device

3 views
Skip to first unread message

chenmiao

unread,
Oct 28, 2025, 4:03:59 AMOct 28
to chao...@openatom.club, luo...@openatom.club, dz...@openatom.club, plu...@openatom.club, hust-os-ker...@googlegroups.com
In irq.rs, we added a new get method for the InterruptSource type to determine
whether an InterruptSource is null. This eliminates the need to repeatedly
call self.cell.get().is_null() for null checks during comparisons.
Additionally, we exposed the slice_as_ptrmethod to support external usage with
the &[InterruptSource]type.

In qdev.rs, we implemented the init_gpio_out_namedfunction, which corresponds
to the C function qdev_init_gpio_out_named. We also refactored the
init_gpio_outfunction to reuse the init_gpio_out_namedinterface.

Signed-off-by: chenmiao <chen...@openatom.club>
---
rust/hw/core/src/irq.rs | 6 +++++-
rust/hw/core/src/qdev.rs | 12 +++++++++---
2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/rust/hw/core/src/irq.rs b/rust/hw/core/src/irq.rs
index e0d7784d97..dd5d0cadbc 100644
--- a/rust/hw/core/src/irq.rs
+++ b/rust/hw/core/src/irq.rs
@@ -71,6 +71,10 @@ pub fn pulse(&self) {
pub fn raise(&self) {
self.set(true);
}
+
+ pub fn get(&self) -> bool {
+ !self.cell.get().is_null()
+ }
}

impl<T> InterruptSource<T>
@@ -91,7 +95,7 @@ pub(crate) const fn as_ptr(&self) -> *mut *mut bindings::IRQState {
self.cell.as_ptr()
}

- pub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
+ pub const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
assert!(!slice.is_empty());
slice[0].as_ptr()
}
diff --git a/rust/hw/core/src/qdev.rs b/rust/hw/core/src/qdev.rs
index c3097a284d..1d7a0b7c1a 100644
--- a/rust/hw/core/src/qdev.rs
+++ b/rust/hw/core/src/qdev.rs
@@ -17,7 +17,7 @@

pub use crate::bindings::{ClockEvent, DeviceClass, Property, ResetType};
use crate::{
- bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out, ResettableClass},
+ bindings::{self, qdev_init_gpio_in, qdev_init_gpio_out_named, ResettableClass},
irq::InterruptSource,
};

@@ -399,11 +399,17 @@ fn do_init_gpio_in(
}

fn init_gpio_out(&self, pins: &[InterruptSource]) {
+ self.init_gpio_out_named(pins, "", pins.len());
+ }
+
+ fn init_gpio_out_named(&self, pins: &[InterruptSource], name: &str, n: usize) {
+ let c_name = CString::new(name).expect("gpio name new failed");
unsafe {
- qdev_init_gpio_out(
+ qdev_init_gpio_out_named(
self.upcast().as_mut_ptr(),
InterruptSource::slice_as_ptr(pins),
- pins.len() as c_int,
+ c_name.as_ptr(),
+ n as c_int,
);
}
}
--
2.43.0

chenmiao

unread,
Oct 28, 2025, 4:18:05 AMOct 28
to chao...@openatom.club, luo...@openatom.club, dz...@openatom.club, plu...@openatom.club, hust-os-ker...@googlegroups.com

chenmiao

unread,
Oct 28, 2025, 6:18:46 AMOct 28
to zhao...@intel.com, pbon...@redhat.com, manos.pit...@linaro.org, richard....@linaro.org, phi...@linaro.org, chao...@openatom.club, qemu...@nongnu.org, qemu-...@nongnu.org, hust-os-ker...@googlegroups.com

Paolo Bonzini

unread,
Oct 28, 2025, 6:49:57 AMOct 28
to chenmiao, zhao...@intel.com, manos.pit...@linaro.org, richard....@linaro.org, phi...@linaro.org, chao...@openatom.club, qemu...@nongnu.org, qemu-...@nongnu.org, hust-os-ker...@googlegroups.com
This should not be get(), but "is_connected()". Also it should be
implemented for any T, therefore in the "impl<T> InterruptSource<T>"
block below.

> }
>
> impl<T> InterruptSource<T>
> @@ -91,7 +95,7 @@ pub(crate) const fn as_ptr(&self) -> *mut *mut bindings::IRQState {
> self.cell.as_ptr()
> }
>
> - pub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
> + pub const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
> assert!(!slice.is_empty());
> slice[0].as_ptr()
> }

Since you are not using this, you don't need to expose it outside the crate.

Paolo

Chen Miao

unread,
Oct 28, 2025, 7:23:57 AMOct 28
to Paolo Bonzini, zhao...@intel.com, manos.pit...@linaro.org, richard....@linaro.org, phi...@linaro.org, chao...@openatom.club, qemu...@nongnu.org, qemu-...@nongnu.org, hust-os-ker...@googlegroups.com
I'll fix it later.
>> }
>>
>> impl<T> InterruptSource<T>
>> @@ -91,7 +95,7 @@ pub(crate) const fn as_ptr(&self) -> *mut *mut bindings::IRQState {
>> self.cell.as_ptr()
>> }
>>
>> - pub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
>> + pub const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
>> assert!(!slice.is_empty());
>> slice[0].as_ptr()
>> }
> Since you are not using this, you don't need to expose it outside the crate.
>
> Paolo

I have used this function in the next patch.

Chen Miao

Paolo Bonzini

unread,
Oct 28, 2025, 7:30:40 AMOct 28
to Chen Miao, zhao...@intel.com, manos.pit...@linaro.org, richard....@linaro.org, phi...@linaro.org, chao...@openatom.club, qemu...@nongnu.org, qemu-...@nongnu.org, hust-os-ker...@googlegroups.com
You can drop it actually (see review of 5/5).

> >> }
> >>
> >> impl<T> InterruptSource<T>
> >> @@ -91,7 +95,7 @@ pub(crate) const fn as_ptr(&self) -> *mut *mut bindings::IRQState {
> >> self.cell.as_ptr()
> >> }
> >>
> >> - pub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
> >> + pub const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
> >> assert!(!slice.is_empty());
> >> slice[0].as_ptr()
> >> }
> > Since you are not using this, you don't need to expose it outside the crate.
>
> I have used this function in the next patch.

Isn't it commented out? The code that is used is:

+ self.init_gpio_in(self.handler_size(), PCF8574State::gpio_set);
+ self.init_gpio_out(from_ref(&self.handler[0]));
+ self.init_gpio_out_named(from_ref(&self.intrq), "nINT", 1);

Chen Miao

unread,
Oct 28, 2025, 7:38:51 AMOct 28
to Paolo Bonzini, zhao...@intel.com, manos.pit...@linaro.org, richard....@linaro.org, phi...@linaro.org, chao...@openatom.club, qemu...@nongnu.org, qemu-...@nongnu.org, hust-os-ker...@googlegroups.com
Ok!
>>>> }
>>>>
>>>> impl<T> InterruptSource<T>
>>>> @@ -91,7 +95,7 @@ pub(crate) const fn as_ptr(&self) -> *mut *mut bindings::IRQState {
>>>> self.cell.as_ptr()
>>>> }
>>>>
>>>> - pub(crate) const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
>>>> + pub const fn slice_as_ptr(slice: &[Self]) -> *mut *mut bindings::IRQState {
>>>> assert!(!slice.is_empty());
>>>> slice[0].as_ptr()
>>>> }
>>> Since you are not using this, you don't need to expose it outside the crate.
>> I have used this function in the next patch.
> Isn't it commented out? The code that is used is:
>
> + self.init_gpio_in(self.handler_size(), PCF8574State::gpio_set);
> + self.init_gpio_out(from_ref(&self.handler[0]));
> + self.init_gpio_out_named(from_ref(&self.intrq), "nINT", 1);

Oh, sorry, I was referring to my initial version. Initially, this function was
used externally, but later I implemented init_gpio_out_named, and now it's
internal again.

You're right, this function should not be exposed.

Chen Miao

>

Chao Liu

unread,
Oct 28, 2025, 8:17:45 AMOct 28
to chenmiao, luo...@openatom.club, dz...@openatom.club, plu...@openatom.club, hust-os-ker...@googlegroups.com
Reviewed-by: Chao Liu <chao...@openatom.club>
Reply all
Reply to author
Forward
0 new messages