rust, nova-core: PCI Class, Vendor support
Subsystems (8)
v8 cover rust, nova-core: PCI Class, Vendor support
Changes since v7:
* Applied changes from Danilo's and Alex's and reviews (thanks!):
* Removed a blank line, one each, from the Class and Vendor macros.
* Moved example code location from struct Vendor, to vendor_id(), and introduced it in a later commit, in its final form.
* Applied Alex's Reviewed-by tag to the series.
Changes since v6:
* Applied changes from Danilo's and Alex's and Elle's reviews (thanks!):
* Rebased onto driver-core-next, which is here:
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git
* Changed pci::Vendor to be a u16, instead of a u32.
* Inlined all of the tiniest functions.
* Changed from Class/Vendor new(), to from_raw().
* Made from_raw() only accessible to super, which in this case is the pci module.
* Restored infallible operations. That causes Alex's request for the following reasonable behavior to work once again:
from_raw(0x10de).as_raw() == 0x10de
* Added a new patch, to inline the remaining PCI operations. This provides consistent inline choices throughout pci.rs.
Changes since v5:
* Applied changes from Danilo's review (thanks!):
* Split the nova-core patch into two patches, for nova and pci.
* Added rust/kernel/pci/ to MAINTAINERS.
Changes since v4:
* Applied changes from Danilo's and Alex's review (thanks!):
* Reorganized the patches so that the Nova changes consume the results of Class and Vendor upgrades, all in one shot.
* Made Class and Vendor types get constructed infallibly.
* This was all somewhat disruptive, and also required one more patch in order to properly separate the various steps. But I think it is all correct now. And CLIPPY=1 builds cleanly too.
* Elle Rhumsaa provided a Reviewed-by for v4 (thanks!), but due to the churn in v5 here, I thought it best to not add that tag to v5 yet. Instead, I have directly Cc'd Elle on the patches for now.
Changes since v3:
* Applied changes from Danilo's review (thanks!):
* Moved Class and Vendor to a new pci/id.rs file.
* Added ClassMask, to constrain callers to use only the two valid masks.
* Removed pci_class_code_raw()
* Changed Class and Vendor .as_u32() to .as_raw(), because after Danilo's comment I looked around rust/kernel and learned that .as_raw() is the overwhelmingly used convention.
* Changed vendor_id() to return a Vendor instance directly.
* Also, validated Vendor during construction, just as is done with Class. Both of these items are expected to match known values, even for new devices, so that's a reasonable move.
Changes since v2:
* Applied changes from Danilo's and Alex's review (thanks!):
* Moved everything possible out of the new define_all_pci_classes!() and define_all_pci_vendors!() macros.
* Used "impl TryFrom<u32> for Class/Vendor", instead of .from_u32().
* Made the new DeviceId methods infallible.
* Upgraded DeviceId::from_id() to accept a Vendor struct.
* Changed the names to be a little clearer:
* class_code_raw() --> pci_class_code_raw()
* class_enum() --> pci_class()
* Added doctests for the items that are not yet used in real drivers.
v2 is here:
https://lore.kernel.org/[email protected]
Changes since v1:
1) Use the pci_device_table for filtering, instead of open-coding filters in the .probe() callback.
2) Add PCI Class (class, subclass, implementation) and PCI Vendor to Rust for Linux.
3) Rebased onto the latest nova-next branch, which is here:
https://gitlab.freedesktop.org/drm/nova.git
v1 is here:
https://lore.kernel.org/[email protected]
John Hubbard (6):
rust: pci: provide access to PCI Class and Class-related items
rust: pci: provide access to PCI Vendor values
rust: pci: add DeviceId::from_class_and_vendor() method
gpu: nova-core: avoid probing non-display/compute PCI functions
rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
rust: pci: inline several tiny functions
MAINTAINERS | 1 + drivers/gpu/nova-core/driver.rs | 33 +- rust/kernel/pci.rs | 72 +++- rust/kernel/pci/id.rs | 558 ++++++++++++++++++++++++++ samples/rust/rust_dma.rs | 6 +- samples/rust/rust_driver_auxiliary.rs | 12 +- samples/rust/rust_driver_pci.rs | 9 +- 7 files changed, 663 insertions(+), 28 deletions(-) create mode 100644 rust/kernel/pci/id.rs
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
Signature · 1 line
2.51.0
PATCH v8 1/6 rust: pci: provide access to PCI Class and Class-related items
Allow callers to write Class::STORAGE_SCSI instead of bindings::PCI_CLASS_STORAGE_SCSI, for example.
New APIs:
Class::STORAGE_SCSI, Class::NETWORK_ETHERNET, etc. Class::from_raw() -- Only callable from pci module. Class::as_raw()
ClassMask: Full, ClassSubclass Device::pci_class()
--- MAINTAINERS | 1 + rust/kernel/pci.rs | 11 ++ rust/kernel/pci/id.rs | 231 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 rust/kernel/pci/id.rs
MAINTAINERS +1 -0
@@ -19532,6 +19532,7 @@ C: irc://irc.oftc.net/linux-pci T: git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git F: rust/helpers/pci.c F: rust/kernel/pci.rs +F: rust/kernel/pci/ F: samples/rust/rust_driver_pci.rs PCIE BANDWIDTH CONTROLLER
rust/kernel/pci.rs +11 -0
@@ -23,6 +23,10 @@ };use kernel::prelude::*; +mod id; + +pub use self::id::{Class ,ClassMask }; +/// An adapter for the registration of PCI drivers. pub struct Adapter <T :Driver >(T );
@@ -410,6 +414,13 @@ pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> {// - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`. Ok (unsafe { bindings::pci_resource_len (self.as_raw (), bar.try_into ()?) }) } + +/// Returns the PCI class as a `Class` struct. +#[inline] +pub fn pci_class (&self) ->Class { +// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. +Class ::from_raw (unsafe { (*self.as_raw ()).class }) + } }impl Device <device::Bound > {
rust/kernel/pci/id.rs new file +231 -0
231 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v8 2/6 rust: pci: provide access to PCI Vendor values
In reply to John HubbardThis allows callers to write Vendor::SOME_COMPANY instead of bindings::PCI_VENDOR_ID_SOME_COMPANY.
New APIs:
Vendor::SOME_COMPANY
Vendor::from_raw() -- Only accessible from the pci (parent) module. Vendor::as_raw()
Vendor: fmt::Display for Vendor
--- rust/kernel/pci.rs | 2 +- rust/kernel/pci/id.rs | 330 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 330 insertions(+), 2 deletions(-)
rust/kernel/pci.rs +1 -1
@@ -25,7 +25,7 @@mod id; -pub use self::id::{Class ,ClassMask }; +pub use self::id::{Class ,ClassMask ,Vendor };/// An adapter for the registration of PCI drivers. pub struct Adapter <T :Driver >(T );
rust/kernel/pci/id.rs +329 -1
345 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v8 3/6 rust: pci: add DeviceId::from_class_and_vendor() method
In reply to John HubbardAdd a new method to create PCI DeviceIds that match both a specific vendor and PCI class. This is more targeted than the existing from_class() method as it filters on both vendor and class criteria.
--- rust/kernel/pci.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
rust/kernel/pci.rs +22 -0
@@ -161,6 +161,28 @@ pub const fn from_class(class: u32, class_mask: u32) -> Self { override_only: 0, }) } + +/// Create a new `pci::DeviceId` from a class number, mask, and specific vendor. +/// +/// This is more targeted than [`DeviceId::from_class`]: in addition to matching by Vendor, it +/// also matches the PCI Class (up to the entire 24 bits, depending on the mask). +#[inline] +pub const fn from_class_and_vendor ( + class:Class , + class_mask:ClassMask , + vendor:Vendor , + ) ->Self { +Self (bindings::pci_device_id { + vendor: vendor.as_raw ()as u32 , + device:DeviceId ::PCI_ANY_ID , + subvendor:DeviceId ::PCI_ANY_ID , + subdevice:DeviceId ::PCI_ANY_ID , + class: class.as_raw (), + class_mask: class_mask.as_raw (), + driver_data:0 , + override_only:0 , + }) + } }// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and does not add
Signature · 1 line
2.51.0
PATCH v8 4/6 gpu: nova-core: avoid probing non-display/compute PCI functions
In reply to John HubbardNovaCore has so far been too imprecise about figuring out if .probe() has found a supported PCI PF (Physical Function). By that I mean:
.probe() sets up BAR0 (which involves a lot of very careful devres and Device<Bound> details behind the scenes). And then if it is dealing with a non-supported device such as the .1 audio PF on many GPUs, it fails out due to an unexpected BAR0 size. We have been fortunate that the BAR0 sizes are different.
Really, we should be filtering on PCI class ID instead. These days I think we can confidently pick out Nova's supported PF's via PCI class ID. And if not, then we'll revisit.
The approach here is to filter on "Display VGA" or "Display 3D", which is how PCI class IDs express "this is a modern GPU's PF".
--- drivers/gpu/nova-core/driver.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/driver.rs +28 -5
@@ -1,6 +1,14 @@// SPDX-License-Identifier: GPL-2.0 -use kernel::{auxiliary, bindings, c_str, device::Core , pci, prelude::*, sizes::SZ_16M , sync::Arc }; +use kernel::{ + auxiliary, c_str, + device::Core , + pci, + pci::{Class ,ClassMask ,Vendor }, + prelude::*, + sizes::SZ_16M , + sync::Arc , +};use crate ::gpu::Gpu ;
@@ -18,10 +26,25 @@ pub(crate) struct NovaCore { PCI_TABLE, MODULE_PCI_TABLE, <NovaCore as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_NVIDIA , bindings::PCI_ANY_ID as u32 ), - () - )] + [ +// Modern NVIDIA GPUs will show up as either VGA or 3D controllers. + ( + pci::DeviceId::from_class_and_vendor( + Class::DISPLAY_VGA, + ClassMask::ClassSubclass, + Vendor::NVIDIA + ), + () + ), + ( + pci::DeviceId ::from_class_and_vendor ( +Class ::DISPLAY_3D , +ClassMask ::ClassSubclass , +Vendor ::NVIDIA + ), + () + ), + ] );impl pci::Driver for NovaCore {
Signature · 1 line
2.51.0
PATCH v8 5/6 rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
In reply to John HubbardChange Device::vendor_id() to return a Vendor type, and change DeviceId::from_id() to accept a Vendor type.
Use the new pci::Vendor in the various Rust for Linux callers who were previously using bindings::PCI_VENDOR_ID_*.
Doing so also allows removing "use kernel::bindings" entirely from most of the affected files here.
Also, mark vendor_id() as inline.
--- rust/kernel/pci.rs | 33 +++++++++++++++++++++------ rust/kernel/pci/id.rs | 1 - samples/rust/rust_dma.rs | 6 +---- samples/rust/rust_driver_auxiliary.rs | 12 ++++------ samples/rust/rust_driver_pci.rs | 9 ++++---- 5 files changed, 37 insertions(+), 24 deletions(-)
rust/kernel/pci.rs +26 -7
@@ -132,10 +132,10 @@ impl DeviceId {/// Equivalent to C's `PCI_DEVICE` macro. /// -/// Create a new `pci::DeviceId` from a vendor and device ID number. -pub const fn from_id (vendor:u32 , device:u32 ) ->Self { +/// Create a new `pci::DeviceId` from a vendor and device ID. +pub const fn from_id (vendor:Vendor , device:u32 ) ->Self {Self (bindings::pci_device_id { - vendor, + vendor: vendor.as_raw ()as u32 , device, subvendor:DeviceId ::PCI_ANY_ID , subdevice:DeviceId ::PCI_ANY_ID ,
@@ -232,7 +232,7 @@ macro_rules! pci_device_table {/// <MyDriver as pci::Driver>::IdInfo, /// [ /// ( -/// pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as u32), +/// pci::DeviceId::from_id(pci::Vendor::REDHAT, bindings::PCI_ANY_ID as u32), /// (), /// ) /// ]
@@ -413,10 +413,29 @@ fn as_raw(&self) -> *mut bindings::pci_dev { }impl Device { -/// Returns the PCI vendor ID. -pub fn vendor_id (&self) ->u16 { +/// Returns the PCI vendor ID as a validated Vendor. +/// +/// # Examples +/// +/// ``` +/// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; +/// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { +/// // Get the validated PCI vendor ID +/// let vendor = pdev.vendor_id(); +/// dev_info!( +/// pdev.as_ref(), +/// "Device: Vendor={}, Device=0x{:x}\n", +/// vendor, +/// pdev.device_id() +/// ); +/// Ok(()) +/// } +/// ``` +#[inline] +pub fn vendor_id (&self) ->Vendor {// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. -unsafe { (*self.as_raw ()).vendor } +let vendor_id =unsafe { (*self.as_raw ()).vendor }; +Vendor ::from_raw (vendor_id) }/// Returns the PCI device ID.
rust/kernel/pci/id.rs +0 -1
@@ -132,7 +132,6 @@ impl Vendor {impl Vendor {/// Create a Vendor from a raw 16-bit vendor ID. /// Only accessible from the parent pci module. -#[expect(dead_code)] #[inline] pub (super )fn from_raw (vendor_id:u16 ) ->Self {Self (vendor_id)
samples/rust/rust_dma.rs +1 -5
@@ -5,7 +5,6 @@//! To make this driver probe, QEMU must be run with `-device pci-testdev`. use kernel::{ - bindings, device::Core , dma::{CoherentAllocation ,Device ,DmaMask }, pci,
@@ -45,10 +44,7 @@ unsafe impl kernel::transmute::FromBytes for MyStruct {} PCI_TABLE, MODULE_PCI_TABLE, <DmaSampleDriver as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_REDHAT ,0x5 ), - () - )] + [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] );impl pci::Driver for DmaSampleDriver {
samples/rust/rust_driver_auxiliary.rs +5 -7
@@ -5,7 +5,7 @@//! To make this driver probe, QEMU must be run with `-device pci-testdev`. use kernel::{ - auxiliary, bindings, c_str, device::Core , driver, error::Error , pci, prelude::*,InPlaceModule , + auxiliary, c_str, device::Core , driver, error::Error , pci, prelude::*,InPlaceModule , };use pin_init::PinInit ;
@@ -50,10 +50,7 @@ struct ParentDriver { PCI_TABLE, MODULE_PCI_TABLE, <ParentDriver as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_REDHAT ,0x5 ), - () - )] + [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] );impl pci::Driver for ParentDriver {
@@ -81,11 +78,12 @@ fn connect(adev: &auxiliary::Device) -> Result<()> {let parent = adev.parent ().ok_or (EINVAL )?;let pdev: &pci::Device = parent.try_into ()?; +let vendor = pdev.vendor_id ();dev_info! ( adev.as_ref (), -"Connect auxiliary {} with parent: VendorID={:#x}, DeviceID={:#x}\n" , +"Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n" , adev.id (), - pdev.vendor_id (), + vendor, pdev.device_id () );
samples/rust/rust_driver_pci.rs +5 -4
@@ -4,7 +4,7 @@//! //! To make this driver probe, QEMU must be run with `-device pci-testdev`. -use kernel::{bindings, c_str, device::Core , devres::Devres , pci, prelude::*, types::ARef }; +use kernel::{c_str, device::Core , devres::Devres , pci, prelude::*, types::ARef };struct Regs ;
@@ -38,7 +38,7 @@ struct SampleDriver { MODULE_PCI_TABLE, <SampleDriver as pci::Driver>::IdInfo, [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_REDHAT ,0x5 ), + pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ),TestIndex ::NO_EVENTFD )] );
@@ -66,10 +66,11 @@ impl pci::Driver for SampleDriver {const ID_TABLE : pci::IdTable <Self ::IdInfo > = &PCI_TABLE ;fn probe (pdev: &pci::Device <Core >, info: &Self ::IdInfo ) ->Result <Pin <KBox <Self >>> { +let vendor = pdev.vendor_id ();dev_dbg! ( pdev.as_ref (), -"Probe Rust PCI driver sample (PCI ID: 0x{:x}, 0x{:x}).\n" , - pdev.vendor_id (), +"Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n" , + vendor, pdev.device_id () );
Signature · 1 line
2.51.0
PATCH v8 6/6 rust: pci: inline several tiny functions
In reply to John HubbardSeveral previous commits added Vendor and Class functionality. As part of that, the new functions were inlined where appropriate. But that left this file with inconsistent use of inlining. Fix that by inlining the remaining items that should be.
--- rust/kernel/pci.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/pci.rs +6 -0
@@ -133,6 +133,7 @@ impl DeviceId {/// Equivalent to C's `PCI_DEVICE` macro. /// /// Create a new `pci::DeviceId` from a vendor and device ID. +#[inline] pub const fn from_id (vendor:Vendor , device:u32 ) ->Self {Self (bindings::pci_device_id { vendor: vendor.as_raw ()as u32 ,
@@ -149,6 +150,7 @@ pub const fn from_id(vendor: Vendor, device: u32) -> Self {/// Equivalent to C's `PCI_DEVICE_CLASS` macro. /// /// Create a new `pci::DeviceId` from a class number and mask. +#[inline] pub const fn from_class (class:u32 , class_mask:u32 ) ->Self {Self (bindings::pci_device_id { vendor:DeviceId ::PCI_ANY_ID ,
@@ -385,6 +387,7 @@ fn release(&self) { }impl Bar { +#[inline] 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
@@ -407,6 +410,7 @@ fn deref(&self) -> &Self::Target { }impl <Ctx : device::DeviceContext >Device <Ctx > { +#[inline] fn as_raw (&self) -> *mut bindings::pci_dev { self.0 .get () }
@@ -439,6 +443,7 @@ pub fn vendor_id(&self) -> Vendor { }/// Returns the PCI device ID. +#[inline] pub fn device_id (&self) ->u16 {// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. unsafe { (*self.as_raw ()).device }
@@ -493,6 +498,7 @@ pub fn enable_device_mem(&self) -> Result { }/// Enable bus-mastering for this device. +#[inline] pub fn set_master (&self) {// SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`. unsafe { bindings::pci_set_master (self.as_raw ()) };
Signature · 1 line
2.51.0
Danilo Krummrich
maintainerQuoted 7 lines · John Hubbard
Changes since v7:
* Applied changes from Danilo's and Alex's and reviews (thanks!):
* Removed a blank line, one each, from the Class and Vendor macros.
* Moved example code location from struct Vendor, tovendor_id(), and introduced it in a later commit, in its final form.
* Applied Alex's Reviewed-by tag to the series.
I think you forgot to align Debug and Display, i.e. Debug still prints decimal values.
Is this intentional? If not, no worries, I can fix it up on apply (which a few minor doc-comment nits):
rust/kernel/pci/id.rs +8 -2
@@ -26,7 +26,7 @@/// Ok(()) /// } /// ``` -#[derive( Debug , Clone , Copy , PartialEq , Eq )] +#[derive( Clone , Copy , PartialEq , Eq )] #[repr(transparent)] pub struct Class (u32 );
@@ -81,12 +81,18 @@ const fn to_24bit_class(val: u32) -> u32 { } } -impl fmt::Display for Class { +impl fmt::Debug for Class {fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result {write! (f,"0x{:06x}" , self.0 ) } } +impl fmt::Display for Class { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + <Self as fmt::Debug >::fmt (self, f) + } +} +impl ClassMask {/// Get the raw mask value. #[inline]
Quoted 9 lines · Danilo Krummrich
Quoted 7 lines · John Hubbard
Changes since v7:
* Applied changes from Danilo's and Alex's and reviews (thanks!):
* Removed a blank line, one each, from the Class and Vendor macros.
* Moved example code location from struct Vendor, tovendor_id(), and introduced it in a later commit, in its final form.
* Applied Alex's Reviewed-by tag to the series.
Quoted 5 lines
I think you forgot to align Debug and Display, i.e. Debug still prints decimal values.
Is this intentional? If not, no worries, I can fix it up on apply (which a few minor doc-comment nits):
Yes, I missed that one. Fixup on apply would be great, yes.
thanks,
John Hubbard
Quoted 34 lines
rust/kernel/pci/id.rs +8
-2@@ -26,7 +26,7 @@/// Ok(()) /// } /// ``` -#[derive( Debug , Clone , Copy , PartialEq , Eq )] +#[derive( Clone , Copy , PartialEq , Eq )] #[repr(transparent)] pub struct Class (u32 );@@ -81,12 +81,18 @@ const fn to_24bit_class(val: u32) -> u32 { } } -impl fmt::Display for Class { +impl fmt::Debug for Class {fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result {write! (f,"0x{:06x}" , self.0 ) } } +impl fmt::Display for Class { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { + <Self as fmt::Debug >::fmt (self, f) + } +} +impl ClassMask {/// Get the raw mask value. #[inline]
Danilo Krummrich
maintainerQuoted 1 line · John Hubbard
John Hubbard (6):
Applied to driver-core-testing, thanks!
Quoted 1 line
rust: pci: provide access to PCI Class and Class-related items
[ Minor doc-comment improvements, align Debug and Display. - Danilo ]
Quoted 1 line
rust: pci: provide access to PCI Vendor values
[ Minor doc-comment improvements, align Debug and Display. - Danilo ]
Quoted 1 line
rust: pci: add
DeviceId::from_class_and_vendor()method
[ Minor doc-comment improvements. - Danilo ]
Quoted 2 lines
gpu: nova-core: avoid probing non-display/compute PCI functions
rust: pci: usepci::Vendorinstead of bindings::PCI_VENDOR_ID_*
[ Replace "as a validated vendor" with "as [Vendor]". - Danilo ]
Quoted 1 line
rust: pci: inline several tiny functions