Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

These will be used by the nova-core GPU driver which is currently in
development.

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Krzysztof Wilczyński <kwilczynski@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Alex Gaynor <alex.gaynor@gmail.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Gary Guo <gary@garyguo.net>
Cc: Björn Roy Baron <bjorn3_gh@protonmail.com>
Cc: Benno Lossin <lossin@kernel.org>
Cc: Andreas Hindborg <a.hindborg@kernel.org>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Trevor Gross <tmgross@umich.edu>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Rafael J. Wysocki <rafael@kernel.org>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Alexandre Courbot <acourbot@nvidia.com>
Cc: linux-pci@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Alistair Popple <apopple@nvidia.com>
Link: https://lore.kernel.org/r/20250730013417.640593-2-apopple@nvidia.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

authored by

Alistair Popple and committed by
Danilo Krummrich
b6a37d1d cd58b0b1

+54
+10
rust/helpers/pci.c
··· 2 2 3 3 #include <linux/pci.h> 4 4 5 + u16 rust_helper_pci_dev_id(struct pci_dev *dev) 6 + { 7 + return PCI_DEVID(dev->bus->number, dev->devfn); 8 + } 9 + 10 + resource_size_t rust_helper_pci_resource_start(struct pci_dev *pdev, int bar) 11 + { 12 + return pci_resource_start(pdev, bar); 13 + } 14 + 5 15 resource_size_t rust_helper_pci_resource_len(struct pci_dev *pdev, int bar) 6 16 { 7 17 return pci_resource_len(pdev, bar);
+44
rust/kernel/pci.rs
··· 403 403 unsafe { (*self.as_raw()).device } 404 404 } 405 405 406 + /// Returns the PCI revision ID. 407 + #[inline] 408 + pub fn revision_id(&self) -> u8 { 409 + // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a 410 + // `struct pci_dev`. 411 + unsafe { (*self.as_raw()).revision } 412 + } 413 + 414 + /// Returns the PCI bus device/function. 415 + #[inline] 416 + pub fn dev_id(&self) -> u16 { 417 + // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a 418 + // `struct pci_dev`. 419 + unsafe { bindings::pci_dev_id(self.as_raw()) } 420 + } 421 + 422 + /// Returns the PCI subsystem vendor ID. 423 + #[inline] 424 + pub fn subsystem_vendor_id(&self) -> u16 { 425 + // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a 426 + // `struct pci_dev`. 427 + unsafe { (*self.as_raw()).subsystem_vendor } 428 + } 429 + 430 + /// Returns the PCI subsystem device ID. 431 + #[inline] 432 + pub fn subsystem_device_id(&self) -> u16 { 433 + // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a 434 + // `struct pci_dev`. 435 + unsafe { (*self.as_raw()).subsystem_device } 436 + } 437 + 438 + /// Returns the start of the given PCI bar resource. 439 + pub fn resource_start(&self, bar: u32) -> Result<bindings::resource_size_t> { 440 + if !Bar::index_is_valid(bar) { 441 + return Err(EINVAL); 442 + } 443 + 444 + // SAFETY: 445 + // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`, 446 + // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`. 447 + Ok(unsafe { bindings::pci_resource_start(self.as_raw(), bar.try_into()?) }) 448 + } 449 + 406 450 /// Returns the size of the given PCI bar resource. 407 451 pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> { 408 452 if !Bar::index_is_valid(bar) {