[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250708060451.398323-2-apopple@nvidia.com>
Date: Tue, 8 Jul 2025 16:04:51 +1000
From: Alistair Popple <apopple@...dia.com>
To: rust-for-linux@...r.kernel.org
Cc: Alistair Popple <apopple@...dia.com>,
Danilo Krummrich <dakr@...nel.org>,
Bjorn Helgaas <bhelgaas@...gle.com>,
Krzysztof Wilczyński <kwilczynski@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>,
John Hubbard <jhubbard@...dia.com>,
Alexandre Courbot <acourbot@...dia.com>,
linux-pci@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH 2/2] rust: Add several miscellaneous PCI helpers
Add bindings to obtain a PCI device's resource start address, bus/
device function, revision ID and subsystem device and vendor IDs.
Signed-off-by: Alistair Popple <apopple@...dia.com>
Cc: Danilo Krummrich <dakr@...nel.org>
Cc: Bjorn Helgaas <bhelgaas@...gle.com>
Cc: "Krzysztof Wilczyński" <kwilczynski@...nel.org>
Cc: Miguel Ojeda <ojeda@...nel.org>
Cc: Alex Gaynor <alex.gaynor@...il.com>
Cc: Boqun Feng <boqun.feng@...il.com>
Cc: Gary Guo <gary@...yguo.net>
Cc: "Björn Roy Baron" <bjorn3_gh@...tonmail.com>
Cc: Benno Lossin <lossin@...nel.org>
Cc: Andreas Hindborg <a.hindborg@...nel.org>
Cc: Alice Ryhl <aliceryhl@...gle.com>
Cc: Trevor Gross <tmgross@...ch.edu>
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael@...nel.org>
Cc: John Hubbard <jhubbard@...dia.com>
Cc: Alexandre Courbot <acourbot@...dia.com>
Cc: linux-pci@...r.kernel.org
Cc: linux-kernel@...r.kernel.org
---
rust/helpers/pci.c | 10 ++++++++++
rust/kernel/pci.rs | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c
index cd0e6bf2cc4d..59d15bd4bdb1 100644
--- a/rust/helpers/pci.c
+++ b/rust/helpers/pci.c
@@ -12,6 +12,16 @@ void *rust_helper_pci_get_drvdata(struct pci_dev *pdev)
return pci_get_drvdata(pdev);
}
+u16 rust_helper_pci_dev_id(struct pci_dev *dev)
+{
+ return PCI_DEVID(dev->bus->number, dev->devfn);
+}
+
+resource_size_t rust_helper_pci_resource_start(struct pci_dev *pdev, int bar)
+{
+ return pci_resource_start(pdev, bar);
+}
+
resource_size_t rust_helper_pci_resource_len(struct pci_dev *pdev, int bar)
{
return pci_resource_len(pdev, bar);
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 7f640ba8f19c..f41fd9facb90 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -393,6 +393,42 @@ pub fn device_id(&self) -> u16 {
unsafe { (*self.as_raw()).device }
}
+ /// Returns the PCI revision ID.
+ pub fn revision_id(&self) -> u8 {
+ // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+ unsafe { (*self.as_raw()).revision }
+ }
+
+ /// Returns the PCI bus device/function.
+ pub fn dev_id(&self) -> u16 {
+ // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+ unsafe { bindings::pci_dev_id(self.as_raw()) }
+ }
+
+ /// Returns the PCI subsystem vendor ID.
+ pub fn subsystem_vendor_id(&self) -> u16 {
+ // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+ unsafe { (*self.as_raw()).subsystem_vendor }
+ }
+
+ /// Returns the PCI subsystem device ID.
+ pub fn subsystem_device_id(&self) -> u16 {
+ // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+ unsafe { (*self.as_raw()).subsystem_device }
+ }
+
+ /// Returns the start of the given PCI bar resource.
+ pub fn resource_start(&self, bar: u32) -> Result<bindings::resource_size_t> {
+ if !Bar::index_is_valid(bar) {
+ return Err(EINVAL);
+ }
+
+ // SAFETY:
+ // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`,
+ // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`.
+ Ok(unsafe { bindings::pci_resource_start(self.as_raw(), bar.try_into()?) })
+ }
+
/// 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) {
--
2.47.2
Powered by blists - more mailing lists