[PATCH 04/11] rust: io: add PortIo

Andrew Ballance posted 11 patches 9 months ago
[PATCH 04/11] rust: io: add PortIo
Posted by Andrew Ballance 9 months ago
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
Re: [PATCH 04/11] rust: io: add PortIo
Posted by kernel test robot 9 months ago
Hi Andrew,

kernel test robot noticed the following build errors:

[auto build test ERROR on 92a09c47464d040866cf2b4cd052bc60555185fb]

url:    https://github.com/intel-lab-lkp/linux/commits/Andrew-Ballance/rust-helpers-io-use-macro-to-generate-io-accessor-functions/20250509-111818
base:   92a09c47464d040866cf2b4cd052bc60555185fb
patch link:    https://lore.kernel.org/r/20250509031524.2604087-5-andrewjballance%40gmail.com
patch subject: [PATCH 04/11] rust: io: add PortIo
config: x86_64-randconfig-073-20250512 (https://download.01.org/0day-ci/archive/20250513/202505131314.ozIwFdR4-lkp@intel.com/config)
compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247)
rustc: rustc 1.78.0 (9b00956e5 2024-04-29)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250513/202505131314.ozIwFdR4-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202505131314.ozIwFdR4-lkp@intel.com/

All errors (new ones prefixed by >>):

>> error[E0425]: cannot find function `inb` in crate `bindings`
   --> rust/kernel/io.rs:481:26
   |
   481 |         read8_unchecked, inb, write8_unchecked, outb, u8;
   |                          ^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `outb` in crate `bindings`
   --> rust/kernel/io.rs:481:49
   |
   481 |         read8_unchecked, inb, write8_unchecked, outb, u8;
   |                                                 ^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `inw` in crate `bindings`
   --> rust/kernel/io.rs:482:27
   |
   482 |         read16_unchecked, inw, write16_unchecked, outw, u16;
   |                           ^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `outw` in crate `bindings`
   --> rust/kernel/io.rs:482:51
   |
   482 |         read16_unchecked, inw, write16_unchecked, outw, u16;
   |                                                   ^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `inl` in crate `bindings`
   --> rust/kernel/io.rs:483:27
   |
   483 |         read32_unchecked, inl, write32_unchecked, outl, u32;
   |                           ^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `outl` in crate `bindings`
   --> rust/kernel/io.rs:483:51
   |
   483 |         read32_unchecked, inl, write32_unchecked, outl, u32;
   |                                                   ^^^^ not found in `bindings`

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 04/11] rust: io: add PortIo
Posted by Arnd Bergmann 9 months ago
On Fri, May 9, 2025, at 05:15, Andrew Ballance wrote:
> +
> +#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);

These have to be guarded with "#ifdef CONFIG_HAS_PIO", since
most modern machines no longer support PIO at all, even behind
PCI.

The option is still enabled by default for a number of
architectures that normally don't have PIO, but it should
eventually become optional on pretty much anything but
x86 and a few 1990s-era workstations that implement x86-style
on-board devices.

     Arnd