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

3 views
Skip to first unread message

Chao Liu

unread,
Oct 24, 2025, 8:12:27 AMOct 24
to chenmiao, luo...@openatom.club, dz...@openatom.club, plu...@openatom.club, hust-os-ker...@googlegroups.com
On 10/24/2025 3:51 PM, chenmiao wrote:
> 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 e0d7784..dd5d0ca 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 c3097a2..1d7a0b7 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,
> );
> }
> }

Reviewed-by: Chao Liu <chao...@openatom.club>

chenmiao

unread,
Oct 24, 2025, 8:12:27 AMOct 24
to chao...@openatom.club, luo...@openatom.club, dz...@openatom.club, plu...@openatom.club, hust-os-ker...@googlegroups.com
--
2.43.0

chenmiao

unread,
Oct 28, 2025, 1:22:55 AMOct 28
to pbon...@redhat.com, manos.pit...@linaro.org, richard....@linaro.org, phi...@linaro.org, qemu...@nongnu.org, qemu-...@nongnu.org, hust-os-ker...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages