rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Ballance <andrewjballance@gmail.com>
To: dakr@kernel.org, airlied@gmail.com, simona@ffwll.ch,
	akpm@linux-foundation.org, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, benno.lossin@proton.me,
	a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
	gregkh@linuxfoundation.org, rafael@kernel.org,
	bhelgaas@google.com, kwilczynski@kernel.org,
	raag.jadav@intel.com, andriy.shevchenko@linux.intel.com,
	arnd@arndb.de, me@kloenk.dev, andrewjballance@gmail.com,
	fujita.tomonori@gmail.com, daniel.almeida@collabora.com
Cc: nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-pci@vger.kernel.org
Subject: [PATCH 04/11] rust: io: add PortIo
Date: Thu,  8 May 2025 22:15:17 -0500	[thread overview]
Message-ID: <20250509031524.2604087-5-andrewjballance@gmail.com> (raw)
In-Reply-To: <20250509031524.2604087-1-andrewjballance@gmail.com>

From: Fiona Behrens <me@kloenk.dev>

Add `rust::io::PortIo` implementing the `IoAccess` trait.

Signed-off-by: Fiona Behrens <me@kloenk.dev>
Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
 rust/helpers/io.c | 20 +++++++++++
 rust/kernel/io.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)

diff --git a/rust/helpers/io.c b/rust/helpers/io.c
index 525af02f209e..d439b61c672e 100644
--- a/rust/helpers/io.c
+++ b/rust/helpers/io.c
@@ -51,3 +51,23 @@ define_rust_mmio_write_helper(writel_relaxed, u32);
 #ifdef CONFIG_64BIT
 define_rust_mmio_write_helper(writeq_relaxed, u64);
 #endif
+
+#define define_rust_pio_read_helper(name, type)     \
+	type rust_helper_##name(unsigned long port) \
+	{                                           \
+		return name(port);                  \
+	}
+
+#define define_rust_pio_write_helper(name, type)                \
+	void rust_helper_##name(type value, unsigned long port) \
+	{                                                       \
+		name(value, port);                              \
+	}
+
+define_rust_pio_read_helper(inb, u8);
+define_rust_pio_read_helper(inw, u16);
+define_rust_pio_read_helper(inl, u32);
+
+define_rust_pio_write_helper(outb, u8);
+define_rust_pio_write_helper(outw, u16);
+define_rust_pio_write_helper(outl, u32);
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 09440dd3e73b..70621a016a87 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -395,3 +395,91 @@ impl<const SIZE: usize> IoAccess64Relaxed<SIZE> for MMIo<SIZE> {
         read64_relaxed_unchecked, readq_relaxed, write64_relaxed_unchecked, writeq_relaxed, u64;
     );
 }
+
+/// Port-IO, starting at the base address [`addr`] and spanning [`maxsize`] bytes.
+///
+/// The creator is responsible for performing an additional region request, etc.
+///
+/// # Invariants
+///
+/// [`addr`] is the start and [`maxsize`] the length of a valid port io region of size [`maxsize`].
+///
+/// [`addr`] is valid to access with the C [`in`]/[`out`] family of functions.
+///
+/// [`addr`]: IoAccess::addr
+/// [`maxsize`]: IoAccess::maxsize
+/// [`in`]: https://docs.kernel.org/driver-api/device-io.html#differences-between-i-o-access-functions
+/// [`out`]: https://docs.kernel.org/driver-api/device-io.html#differences-between-i-o-access-functions
+#[derive(Debug)]
+#[repr(transparent)]
+pub struct PortIo<const SIZE: usize = 0>(IoRaw<SIZE>);
+
+impl<const SIZE: usize> PortIo<SIZE> {
+    /// Convert a [`IoRaw`] into an [`PortIo`] instance, providing the accessors to the
+    /// PortIo mapping.
+    ///
+    /// # Safety
+    ///
+    /// Callers must ensure that `addr` is the start of a valid Port I/O region of size `maxsize`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::io::{IoRaw, PortIo, IoAccess};
+    ///
+    /// let raw = IoRaw::<2>::new(0xDEADBEEFC0DE, 2).unwrap();
+    /// // SAFETY: test, value is not actually written to.
+    /// let pio: PortIo<2> = unsafe { PortIo::from_raw(raw) };
+    /// # assert_eq!(0xDEADBEEFC0DE, pio.addr());
+    /// # assert_eq!(2, pio.maxsize());
+    /// ```
+    #[inline]
+    pub unsafe fn from_raw(raw: IoRaw<SIZE>) -> Self {
+        Self(raw)
+    }
+
+    /// Convert a ref to [`IoRaw`] into an [`PortIo`] instance, providing the accessors to
+    /// the PortIo mapping.
+    ///
+    /// # Safety
+    ///
+    /// Callers must ensure that `addr` is the start of a valid I/O mapped memory region of
+    /// size `maxsize`.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::io::{IoRaw, PortIo, IoAccess};
+    ///
+    /// let raw = IoRaw::<2>::new(0xDEADBEEFC0DE, 2).unwrap();
+    /// // SAFETY: test, value is not actually written to.
+    /// let pio: &PortIo<2> = unsafe { PortIo::from_raw_ref(&raw) };
+    /// # assert_eq!(raw.addr(), pio.addr());
+    /// # assert_eq!(raw.maxsize(), pio.maxsize());
+    /// ```
+    #[inline]
+    pub unsafe fn from_raw_ref(raw: &IoRaw<SIZE>) -> &Self {
+        // SAFETY: `PortIo` is a transparent wrapper around `IoRaw`.
+        unsafe { &*core::ptr::from_ref(raw).cast() }
+    }
+}
+
+// SAFETY: as per invariant `raw` is valid
+unsafe impl<const SIZE: usize> IoAccess<SIZE> for PortIo<SIZE> {
+    #[inline]
+    fn maxsize(&self) -> usize {
+        self.0.maxsize()
+    }
+
+    #[inline]
+    fn addr(&self) -> usize {
+        self.0.addr()
+    }
+
+    #[rustfmt::skip]
+    impl_accessor_fn!(
+        read8_unchecked, inb, write8_unchecked, outb, u8;
+        read16_unchecked, inw, write16_unchecked, outw, u16;
+        read32_unchecked, inl, write32_unchecked, outl, u32;
+    );
+}
-- 
2.49.0


  parent reply	other threads:[~2025-05-09  3:16 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-09  3:15 [PATCH 00/11] rust: add support for Port io Andrew Ballance
2025-05-09  3:15 ` [PATCH 01/11] rust: helpers: io: use macro to generate io accessor functions Andrew Ballance
2025-05-09  5:32   ` Arnd Bergmann
2025-05-13  1:59   ` kernel test robot
2025-05-09  3:15 ` [PATCH 02/11] rust: io: Replace Io with MMIo using IoAccess trait Andrew Ballance
2025-05-12 20:07   ` Bjorn Helgaas
2025-05-09  3:15 ` [PATCH 03/11] rust: io: implement Debug for IoRaw and add some doctests Andrew Ballance
2025-05-09  3:15 ` Andrew Ballance [this message]
2025-05-09  6:05   ` [PATCH 04/11] rust: io: add PortIo Arnd Bergmann
2025-05-13  6:09   ` kernel test robot
2025-05-09  3:15 ` [PATCH 05/11] rust: io: add new Io type Andrew Ballance
2025-05-09  3:15 ` [PATCH 06/11] io: move PIO_OFFSET to linux/io.h Andrew Ballance
2025-05-09  5:42   ` Arnd Bergmann
2025-05-09 11:35   ` Andy Shevchenko
2025-05-09  3:15 ` [PATCH 07/11] rust: io: add from_raw_cookie functions Andrew Ballance
2025-05-09  5:45   ` Arnd Bergmann
2025-05-09  3:15 ` [PATCH 08/11] rust: pci: make Bar generic over Io Andrew Ballance
2025-05-09  3:15 ` [PATCH 09/11] samples: rust: rust_driver_pci: update to use new bar and io api Andrew Ballance
2025-05-09  3:15 ` [PATCH 10/11] gpu: nova-core: update to use the " Andrew Ballance
2025-05-09  3:15 ` [PATCH 11/11] rust: devres: fix doctest Andrew Ballance
2025-05-09  5:53 ` [PATCH 00/11] rust: add support for Port io Arnd Bergmann
2025-05-13 15:15   ` Andrew Ballance
2025-05-13  8:32 ` Danilo Krummrich

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=20250509031524.2604087-5-andrewjballance@gmail.com \
    --to=andrewjballance@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=benno.lossin@proton.me \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=me@kloenk.dev \
    --cc=nouveau@lists.freedesktop.org \
    --cc=ojeda@kernel.org \
    --cc=raag.jadav@intel.com \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    /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).