From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Paolo Bonzini <pbonzini@redhat.com>, qemu-devel@nongnu.org
Cc: junjie.mao@hotmail.com, zhao1.liu@intel.com, qemu-rust@nongnu.org
Subject: Re: [PATCH 2/2] rust: add bindings for interrupt sources
Date: Fri, 22 Nov 2024 09:28:13 +0100 [thread overview]
Message-ID: <70f44731-6879-4adf-a71b-a781af48fe99@linaro.org> (raw)
In-Reply-To: <20241122074756.282142-3-pbonzini@redhat.com>
Hi Paolo,
On 22/11/24 08:47, Paolo Bonzini wrote:
> The InterruptSource bindings let us call qemu_set_irq() and sysbus_init_irq()
> as safe code.
>
> Interrupt sources, qemu_irq in C code, are pointers to IRQState objects.
> They are QOM link properties and can be written to outside the control
> of the device (i.e. from a shared reference); therefore they must be
> interior-mutable in Rust. Since thread-safety is provided by the BQL,
> what we want here is the newly-introduced BqlCell. A pointer to the
> contents of the BqlCell (an IRQState**, or equivalently qemu_irq*)
> is then passed to the C sysbus_init_irq function.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> rust/hw/char/pl011/src/device.rs | 22 ++++++-----
> rust/qemu-api/meson.build | 2 +
> rust/qemu-api/src/irq.rs | 66 ++++++++++++++++++++++++++++++++
> rust/qemu-api/src/lib.rs | 2 +
> rust/qemu-api/src/sysbus.rs | 26 +++++++++++++
> 5 files changed, 108 insertions(+), 10 deletions(-)
> create mode 100644 rust/qemu-api/src/irq.rs
> create mode 100644 rust/qemu-api/src/sysbus.rs
> diff --git a/rust/qemu-api/src/irq.rs b/rust/qemu-api/src/irq.rs
> new file mode 100644
> index 00000000000..7dbff007995
> --- /dev/null
> +++ b/rust/qemu-api/src/irq.rs
> @@ -0,0 +1,66 @@
> +// Copyright 2024 Red Hat, Inc.
> +// Author(s): Paolo Bonzini <pbonzini@redhat.com>
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +//! Bindings for interrupt sources
> +
> +use core::ptr;
> +
> +use crate::{
> + bindings::{qemu_set_irq, IRQState},
> + cell::BqlCell,
> +};
> +
> +/// Interrupt sources are used by devices to pass changes to a boolean value to
> +/// other devices (typically interrupt or GPIO controllers). QEMU interrupt
> +/// sources are always active-high.
So 'always active-high' = true below? (Wondering about pulsation, if the
true -> false transition is always correct).
I understand polarity is not part of this interrupt description, so for
GPIO it has to be modelled elsewhere.
Note the C API allows using qemu_set_irq() for vectored interrupts,
which is why the prototype takes an integer argument and not a boolean.
Is this deliberate to restrict the Rust binding to boolean? (Maybe you
envision a VectoredInterruptSource implementation for that).
> +///
> +/// Interrupts are implemented as a pointer to the interrupt "sink", which has
> +/// type [`IRQState`]. A device exposes its source as a QOM link property using
> +/// a function such as
> +/// [`SysBusDevice::init_irq`](crate::sysbus::SysBusDevice::init_irq), and
> +/// initially leaves the pointer to a NULL value, representing an unconnected
> +/// interrupt. To connect it, whoever creates the device fills the pointer with
> +/// the sink's `IRQState *`, for example using `sysbus_connect_irq`. Because
> +/// devices are generally shared objects, interrupt sources are an example of
> +/// the interior mutability pattern.
> +///
> +/// Interrupt sources can only be triggered under the Big QEMU Lock; they are
> +/// neither `Send` nor `Sync`.
> +#[derive(Debug)]
> +pub struct InterruptSource(BqlCell<*mut IRQState>);
> +
> +impl InterruptSource {
> + /// Send a low (`false`) value to the interrupt sink.
> + pub fn lower(&self) {
> + self.set(false);
> + }
> +
> + /// Send a high-low pulse to the interrupt sink.
> + pub fn pulse(&self) {
> + self.set(true);
> + self.set(false);
> + }
> +
> + /// Send a high (`true`) value to the interrupt sink.
> + pub fn raise(&self) {
> + self.set(true);
> + }
> +
> + /// Send `level` to the interrupt sink.
> + pub fn set(&self, level: bool) {
> + unsafe {
> + qemu_set_irq(self.0.get(), level.into());
> + }
> + }
> +
> + pub(crate) const fn as_ptr(&self) -> *mut *mut IRQState {
> + self.0.as_ptr()
> + }
> +}
> +
> +impl Default for InterruptSource {
> + fn default() -> Self {
> + InterruptSource(BqlCell::new(ptr::null_mut()))
> + }
> +}
next prev parent reply other threads:[~2024-11-22 8:28 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-22 7:47 [PATCH 0/2] rust: safe wrappers for interrupt sources Paolo Bonzini
2024-11-22 7:47 ` [PATCH 1/2] rust: add BQL-enforcing Cell variant Paolo Bonzini
2024-11-26 14:56 ` Zhao Liu
2024-11-26 16:11 ` Paolo Bonzini
2024-11-27 6:35 ` Zhao Liu
2024-11-27 8:31 ` Paolo Bonzini
2024-11-27 6:54 ` Junjie Mao
2024-11-22 7:47 ` [PATCH 2/2] rust: add bindings for interrupt sources Paolo Bonzini
2024-11-22 8:28 ` Philippe Mathieu-Daudé [this message]
2024-11-22 8:32 ` Paolo Bonzini
2024-11-22 10:30 ` Philippe Mathieu-Daudé
2024-11-22 10:53 ` Paolo Bonzini
2024-11-22 11:07 ` Philippe Mathieu-Daudé
2024-11-26 13:45 ` Zhao Liu
2024-11-26 13:35 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=70f44731-6879-4adf-a71b-a781af48fe99@linaro.org \
--to=philmd@linaro.org \
--cc=junjie.mao@hotmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).