[PATCH v3 01/33] rust: pci: add domain_nr() accessor
From: John Hubbard
Date: Thu Sep 17 2026 - 21:07:37 EST
The nova-core driver has to send the GPU's PCI location to the GPU's
firmware as one word that holds the domain, the bus and the device
number.
The PCI abstraction has dev_id(), which packs the bus, the device and
the function into one value. It has no accessor that reports the domain.
Add an accessor for the domain number. pci_domain_nr() is a static
inline, so it needs a C helper. The bus of a bound device always has a
domain assigned, so a negative value would be a kernel bug rather than a
condition to report. A debug assertion checks for it before the cast to
u32, and the accessor returns no error.
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Assisted-by: LLM
Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>
---
rust/helpers/pci.c | 5 +++++
rust/kernel/pci.rs | 12 ++++++++++++
2 files changed, 17 insertions(+)
diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c
index 3686e405160d..53cdc7f36fef 100644
--- a/rust/helpers/pci.c
+++ b/rust/helpers/pci.c
@@ -7,6 +7,11 @@ __rust_helper u16 rust_helper_pci_dev_id(struct pci_dev *dev)
return PCI_DEVID(dev->bus->number, dev->devfn);
}
+__rust_helper int rust_helper_pci_domain_nr(struct pci_dev *dev)
+{
+ return pci_domain_nr(dev->bus);
+}
+
__rust_helper resource_size_t
rust_helper_pci_resource_start(struct pci_dev *pdev, int bar)
{
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 19a219847c17..8d379a2d7c71 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -400,6 +400,18 @@ pub fn dev_id(&self) -> u16 {
unsafe { bindings::pci_dev_id(self.as_raw()) }
}
+ /// Returns the PCI domain number of the bus that this device is on.
+ #[inline]
+ pub fn domain_nr(&self) -> u32 {
+ // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
+ // `struct pci_dev`.
+ let domain_nr = unsafe { bindings::pci_domain_nr(self.as_raw()) };
+ debug_assert!(domain_nr >= 0);
+
+ // CAST: `pci_domain_nr` returns a non-negative domain number in an `int`.
+ domain_nr as u32
+ }
+
/// Returns the PCI subsystem vendor ID.
#[inline]
pub fn subsystem_vendor_id(&self) -> u16 {
--
2.55.0