[PATCH v2 01/31] rust: pci: add domain_nr() accessor

John Hubbard <[email protected]>
Newsgroups dev.linux.lists.nova-gpu,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
The Rust PCI abstraction exposes a device's bus and device numbers
through dev_id(), which returns the packed PCI_DEVID value, but it does
not provide an accessor for the PCI domain number.

Add domain_nr(). Because pci_domain_nr() is a static inline rather than
an exported function, the accessor needs a helper as well.

The nova-core GPU driver needs the domain to build the domain/bus/device
word that GSP-RM firmware requires.

Assisted-by: Cursor:claude-opus-5
Signed-off-by: John Hubbard <[email protected]>
---
 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 a714cc2bfb7a..e11ddb4de54e 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 287a4d259f13..c661829434a3 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -405,6 +405,18 @@ pub fn dev_id(&self) -> u16 {
         unsafe { bindings::pci_dev_id(self.as_raw()) }
     }
 
+    /// Returns the PCI domain number of the bus 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()) };
+
+        // CAST: The C function returns `int`, but a PCI domain number is always
+        // non-negative, so this cast will not lose any information.
+        domain_nr as u32
+    }
+
     /// Returns the PCI subsystem vendor ID.
     #[inline]
     pub fn subsystem_vendor_id(&self) -> u16 {
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.