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 08/11] rust: pci: make Bar generic over Io
Date: Thu, 8 May 2025 22:15:21 -0500 [thread overview]
Message-ID: <20250509031524.2604087-9-andrewjballance@gmail.com> (raw)
In-Reply-To: <20250509031524.2604087-1-andrewjballance@gmail.com>
renames `Bar` to `RawBar` and makes it generic over `IoAccess`.
a user can give a compile time suggestion when mapping a bar so
that the type of io can be known.
Suggested-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
rust/kernel/pci.rs | 88 ++++++++++++++++++++++++++++++++++++----------
1 file changed, 69 insertions(+), 19 deletions(-)
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index c97d6d470b28..7e592db99073 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -11,8 +11,7 @@
devres::Devres,
driver,
error::{to_result, Result},
- io::Io,
- io::IoRaw,
+ io::{Io, IoAccess, IoRaw, MMIo, PortIo},
str::CStr,
types::{ARef, ForeignOwnable, Opaque},
ThisModule,
@@ -259,15 +258,25 @@ pub struct Device<Ctx: device::DeviceContext = device::Normal>(
///
/// # Invariants
///
-/// `Bar` always holds an `IoRaw` inststance that holds a valid pointer to the start of the I/O
+/// `Bar` always holds an `I` inststance that holds a valid pointer to the start of the I/O
/// memory mapped PCI bar and its size.
-pub struct Bar<const SIZE: usize = 0> {
+pub struct RawBar<const SIZE: usize = 0, I: IoAccess<SIZE> = Io<SIZE>> {
pdev: ARef<Device>,
- io: IoRaw<SIZE>,
+ original_ioptr: usize,
+ io: I,
num: i32,
}
-impl<const SIZE: usize> Bar<SIZE> {
+/// a pci bar that can be either PortIo or MMIo
+pub type IoBar<const SIZE: usize = 0> = RawBar<SIZE, Io<SIZE>>;
+
+/// a pci bar that maps a [`PortIo`].
+pub type MMIoBar<const SIZE: usize = 0> = RawBar<SIZE, MMIo<SIZE>>;
+
+/// a pci bar that maps a [`MMIo`].
+pub type PIoBar<const SIZE: usize = 0> = RawBar<SIZE, PortIo<SIZE>>;
+
+impl<const SIZE: usize, I: IoAccess<SIZE>> RawBar<SIZE, I> {
fn new(pdev: &Device, num: u32, name: &CStr) -> Result<Self> {
let len = pdev.resource_len(num)?;
if len == 0 {
@@ -299,7 +308,22 @@ fn new(pdev: &Device, num: u32, name: &CStr) -> Result<Self> {
return Err(ENOMEM);
}
- let io = match IoRaw::new(ioptr, len as usize) {
+ let raw = match IoRaw::new(ioptr, len as usize) {
+ Ok(io) => io,
+ Err(err) => {
+ // SAFETY:
+ // `pdev` is valid by the invariants of `Device`.
+ // `ioptr` is guaranteed to be the start of a valid I/O mapped memory region.
+ // `num` is checked for validity by a previous call to `Device::resource_len`.
+ unsafe { Self::do_release(pdev, ioptr, num) };
+ return Err(err);
+ }
+ };
+
+ // SAFETY:
+ // - `raw` is from `pci_iomap`
+ // - addresses from `pci_iomap` should be accesed through ioread/iowrite
+ let io = match unsafe { I::from_raw_cookie(raw) } {
Ok(io) => io,
Err(err) => {
// SAFETY:
@@ -311,8 +335,9 @@ fn new(pdev: &Device, num: u32, name: &CStr) -> Result<Self> {
}
};
- Ok(Bar {
+ Ok(RawBar {
pdev: pdev.into(),
+ original_ioptr: ioptr,
io,
num,
})
@@ -334,29 +359,28 @@ unsafe fn do_release(pdev: &Device, ioptr: usize, num: i32) {
fn release(&self) {
// SAFETY: The safety requirements are guaranteed by the type invariant of `self.pdev`.
- unsafe { Self::do_release(&self.pdev, self.io.addr(), self.num) };
+ unsafe { Self::do_release(&self.pdev, self.original_ioptr, self.num) };
}
}
-impl Bar {
+impl RawBar {
fn index_is_valid(index: u32) -> bool {
// A `struct pci_dev` owns an array of resources with at most `PCI_NUM_RESOURCES` entries.
index < bindings::PCI_NUM_RESOURCES
}
}
-impl<const SIZE: usize> Drop for Bar<SIZE> {
+impl<const SIZE: usize, I: IoAccess<SIZE>> Drop for RawBar<SIZE, I> {
fn drop(&mut self) {
self.release();
}
}
-impl<const SIZE: usize> Deref for Bar<SIZE> {
- type Target = Io<SIZE>;
+impl<const SIZE: usize, I: IoAccess<SIZE>> Deref for RawBar<SIZE, I> {
+ type Target = I;
fn deref(&self) -> &Self::Target {
- // SAFETY: By the type invariant of `Self`, the MMIO range in `self.io` is properly mapped.
- unsafe { Io::from_raw(&self.io) }
+ &self.io
}
}
@@ -379,7 +403,7 @@ pub fn device_id(&self) -> u16 {
/// Returns the size of the given PCI bar resource.
pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> {
- if !Bar::index_is_valid(bar) {
+ if !RawBar::index_is_valid(bar) {
return Err(EINVAL);
}
@@ -395,17 +419,43 @@ pub fn iomap_region_sized<const SIZE: usize>(
&self,
bar: u32,
name: &CStr,
- ) -> Result<Devres<Bar<SIZE>>> {
- let bar = Bar::<SIZE>::new(self, bar, name)?;
+ ) -> Result<Devres<IoBar<SIZE>>> {
+ let bar = RawBar::<SIZE, _>::new(self, bar, name)?;
let devres = Devres::new(self.as_ref(), bar, GFP_KERNEL)?;
Ok(devres)
}
/// Mapps an entire PCI-BAR after performing a region-request on it.
- pub fn iomap_region(&self, bar: u32, name: &CStr) -> Result<Devres<Bar>> {
+ pub fn iomap_region(&self, bar: u32, name: &CStr) -> Result<Devres<IoBar>> {
self.iomap_region_sized::<0>(bar, name)
}
+
+ /// Maps an entire PCI-BAR after performing a region-request` where the
+ /// type of Io backend is known at compile time.
+ pub fn iomap_region_hint<I: IoAccess>(
+ &self,
+ bar: u32,
+ name: &CStr,
+ ) -> Result<Devres<RawBar<0, I>>> {
+ let bar = RawBar::<0, I>::new(self, bar, name)?;
+ let devres = Devres::new(self.as_ref(), bar, GFP_KERNEL)?;
+
+ Ok(devres)
+ }
+ /// Maps an entire PCI-BAR after performing a region-request` where the
+ /// type of Io backend is known at compile time. I/O operation bound checks
+ /// can be performed on compile time for offsets (plus the requested type size) < SIZE.
+ pub fn iomap_region_sized_hint<const SIZE: usize, I: IoAccess<SIZE>>(
+ &self,
+ bar: u32,
+ name: &CStr,
+ ) -> Result<Devres<RawBar<SIZE, I>>> {
+ let bar = RawBar::<SIZE, I>::new(self, bar, name)?;
+ let devres = Devres::new(self.as_ref(), bar, GFP_KERNEL)?;
+
+ Ok(devres)
+ }
}
impl Device<device::Core> {
--
2.49.0
next prev 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 ` [PATCH 04/11] rust: io: add PortIo Andrew Ballance
2025-05-09 6:05 ` 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 ` Andrew Ballance [this message]
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-9-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
all inboxes | Powered by JetHome®