Skip to contentSkip to conversation

gpu: nova-core: gsp: fix the PCI location sent to GSP-RM

PATCH In review Alexandre Courbot 4 participants 7 messages Nov 27

Subsystems (8)
DRM drivers PCI Rust DRM Rust +4 more subsystems
lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

cover gpu: nova-core: gsp: fix the PCI location sent to GSP-RM

nova-core sends the PCI device ID of the GPU (bus, device, function) where GSP-RM expects the layout built by OpenRM's gpuEncodeDomainBusDevice() (domain, bus, device). Both only match for a GPU at device 0, function 0 of domain 0. Patch 3 fixes this.

Doing so requires extracting the bus and device numbers from the value returned by pci::Device::dev_id. Rather than doing this in the driver (and in every other driver that needs that information), patch 2 adds a Bdf bitfield to the PCI abstractions and makes dev_id return it.

Patch 1 documents the constants that bitfield! generates for each field, which otherwise trigger the missing_docs lint on public bitfields like Bdf.

This series is based on drm-rust-next.

---
Alexandre Courbot (2):
rust: bitfield: add missing doccomments for associated constants
rust: pci: return a bitfield type for dev_id

John Hubbard (1):
gpu: nova-core: gsp: send the PCI domain, bus and device to GSP-RM

 drivers/gpu/nova-core/gsp/fw/commands.rs | 30 ++++++++++++++++++++++++++++-
 rust/kernel/bitfield.rs                  |  3 +++
 rust/kernel/pci.rs                       | 33 ++++++++++++++++++++++++++++++--
 samples/rust/rust_driver_sriov/pf.rs     |  2 +-
 4 files changed, 64 insertions(+), 4 deletions(-)

---
base-commit: abfb640b2374e26de8ff81d488c39d8254db538e
change-id: 20261127-nova-gsp-dbd-4f3d26be980e

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
author maintainer
#1 lore raw

PATCH 1/3 rust: bitfield: add missing doccomments for associated constants

The associated constants describing the range, mask and shift of each field do not carry any doccomment. This triggers the missing_docs lint on bitfields declared with pub visibility.

Add a simple doccomment to get rid of that warning.

---
 rust/kernel/bitfield.rs | 3 +++
 1 file changed, 3 insertions(+)
rust/kernel/bitfield.rs +3 -0
index 8d7d78175336..22b1575c177c 100644
@@ -386,9 +386,12 @@ impl $name {
         @private_field_accessors $vis:vis $name:ident $storage:ty : $hi:tt:$lo:tt $field:ident
     ) => {
         ::kernel::macros::paste!(
+        #[doc = concat!("Bit range of the `", stringify!($field), "` field.")]
         $vis const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive<u8> = $lo..=$hi;
+        #[doc = concat!("Bit mask of the `", stringify!($field), "` field.")]
         $vis const [<$field:upper _MASK>]: $storage =
             ((((1 << $hi) - 1) << 1) + 1) - ((1 << $lo) - 1);
+        #[doc = concat!("Bit shift of the `", stringify!($field), "` field.")]
         $vis const [<$field:upper _SHIFT>]: u32 = $lo;
         );
 
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 2/3 rust: pci: return a bitfield type for dev_id

In reply to Alexandre Courbot

The return type of dev_id has a well-defined bit layout encoding the bus, device and function IDs of the PCI device. Add a Bdf bitfield defining that layout and make dev_id return it so users can benefit from it and are not tempted to define their own BDF type.

Also update the current two users of dev_id.

---
 drivers/gpu/nova-core/gsp/fw/commands.rs |  2 +-
 rust/kernel/pci.rs                       | 33 ++++++++++++++++++++++++++++++--
 samples/rust/rust_driver_sriov/pf.rs     |  2 +-
 3 files changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/fw/commands.rs +1 -1
index b4bdbddad79f..37b89e62387f 100644
@@ -88,7 +88,7 @@ pub(crate) fn init<'a>(
             gpuPhysAddr: dev.resource_start(0)?,
             gpuPhysFbAddr: dev.resource_start(1)?,
             gpuPhysInstAddr: dev.resource_start(3)?,
-            nvDomainBusDeviceFunc: u64::from(dev.dev_id()),
+            nvDomainBusDeviceFunc: u64::from(dev.dev_id().into_raw()),
 
             // Using TASK_SIZE in r535_gsp_rpc_set_system_info() seems wrong because
             // TASK_SIZE is per-task. That's probably a design issue in GSP-RM though.
rust/kernel/pci.rs +31 -2
index 04a6d240afb2..0592b220767c 100644
@@ -6,6 +6,7 @@
 
 use crate::{
     bindings,
+    bitfield,
     container_of,
     device,
     device_id::{
@@ -17,6 +18,7 @@
         from_result,
         to_result, //
     },
+    fmt,
     io::resource,
     prelude::*,
     str::CStr,
@@ -279,6 +281,33 @@ macro_rules! module_pci_driver {
 };
 }
 
+bitfield! {
+    /// Bus-Device-Function ID of a PCI device. Equivalent of C's `PCI_DEVID`.
+    pub struct Bdf(u16) {
+        /// Bus number.
+        15:8 bus;
+        /// Device number.
+        7:3 device;
+        /// Function number.
+        2:0 function;
+        /// Aggregation of `device` and `function`, which are commonly used together in code.
+        7:0 devfn;
+    }
+}
+
+/// Formats the ID as `bus:device.function`, like the corresponding part of C's `pci_name()`.
+impl fmt::Display for Bdf {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(
+            f,
+            "{:02x}:{:02x}.{}",
+            self.bus(),
+            self.device(),
+            self.function()
+        )
+    }
+}
+
 /// Abstraction for the PCI device ID structure ([`struct pci_device_id`]).
 ///
 /// [`struct pci_device_id`]: https://docs.kernel.org/PCI/pci.html#c.pci_device_id
@@ -700,10 +729,10 @@ pub fn revision_id(&self) -> u8 {
 
     /// Returns the PCI bus device/function.
     #[inline]
-    pub fn dev_id(&self) -> u16 {
+    pub fn dev_id(&self) -> Bdf {
         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
         // `struct pci_dev`.
-        unsafe { bindings::pci_dev_id(self.as_raw()) }
+        Bdf::from_raw(unsafe { bindings::pci_dev_id(self.as_raw()) })
     }
 
     /// Returns the PCI subsystem vendor ID.
samples/rust/rust_driver_sriov/pf.rs +1 -1
index 8f9958683528..c367c891abf4 100644
@@ -34,7 +34,7 @@ pub(crate) fn submit(vf: &pci::Device<Bound>) -> Result<u64> {
 
         dev_info!(
             pf.pdev,
-            "Handle PF request {} from VF devfn {:#x}.\n",
+            "Handle PF request {} from VF {}.\n",
             request,
             vf.dev_id()
         );
Signature · 1 line
2.56.1
maintainer Acked-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] rust: pci: return a bitfield type for dev_id

rust/kernel/pci.rs macro_rules! module_pci_driver { · line 285

Quoted 2 lines · Alexandre Courbot
rust/kernel/pci.rs +2 -0
+bitfield! {
+    /// Bus-Device-Function ID of a PCI device. Equivalent of C's `PCI_DEVID`.

Please make the reference to the C API a separate line.

Additional nit, I'm not sure I'd use "equivalent" as Bdf is a type that can do a few more things.

Quoted 29 lines
rust/kernel/pci.rs +25 -0
+    pub struct Bdf(u16) {
+        /// Bus number.
+        15:8 bus;
+        /// Device number.
+        7:3 device;
+        /// Function number.
+        2:0 function;
+        /// Aggregation of `device` and `function`, which are commonly used together in code.
+        7:0 devfn;
+    }
+}
+
+/// Formats the ID as `bus:device.function`, like the corresponding part of C's `pci_name()`.
+impl fmt::Display for Bdf {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(
+            f,
+            "{:02x}:{:02x}.{}",
+            self.bus(),
+            self.device(),
+            self.function()
+        )
+    }
+}
+
 /// Abstraction for the PCI device ID structure ([`struct pci_device_id`]).
 ///
 /// [`struct pci_device_id`]: https://docs.kernel.org/PCI/pci.html#c.pci_device_id

rust/kernel/pci.rs pub fn revision_id(&self) -> u8 { · line 732

Quoted 5 lines
rust/kernel/pci.rs +1 -1
@@ -700,10 +729,10 @@ pub fn revision_id(&self) -> u8 {
 
     /// Returns the PCI bus device/function.
     #[inline]
-    pub fn dev_id(&self) -> u16 {
+    pub fn dev_id(&self) -> Bdf {

At a first glance I wasn't sure if the naming should align, i.e. whether Bdf should be DevId instead, but I think this strikes a good balance: dev_id() remains aligned with the C API and Bdf as a type is more on point than DevId.

With the above nit fixed,

reviewer
#4 lore raw

Re: [PATCH 2/3] rust: pci: return a bitfield type for dev_id

In reply to Alexandre Courbot
Quoted 48 lines · Alexandre Courbot

The return type of dev_id has a well-defined bit layout encoding the bus, device and function IDs of the PCI device. Add a Bdf bitfield defining that layout and make dev_id return it so users can benefit from it and are not tempted to define their own BDF type.

Also update the current two users of dev_id.

---
 drivers/gpu/nova-core/gsp/fw/commands.rs |  2 +-
 rust/kernel/pci.rs                       | 33 ++++++++++++++++++++++++++++++--
 samples/rust/rust_driver_sriov/pf.rs     |  2 +-
 3 files changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/fw/commands.rs +1 -1
index b4bdbddad79f..37b89e62387f 100644
@@ -88,7 +88,7 @@ pub(crate) fn init<'a>(
             gpuPhysAddr: dev.resource_start(0)?,
             gpuPhysFbAddr: dev.resource_start(1)?,
             gpuPhysInstAddr: dev.resource_start(3)?,
-            nvDomainBusDeviceFunc: u64::from(dev.dev_id()),
+            nvDomainBusDeviceFunc: u64::from(dev.dev_id().into_raw()),
 
             // Using TASK_SIZE in r535_gsp_rpc_set_system_info() seems wrong because
             // TASK_SIZE is per-task. That's probably a design issue in GSP-RM though.
rust/kernel/pci.rs +2 -0
index 04a6d240afb2..0592b220767c 100644
@@ -6,6 +6,7 @@
 
 use crate::{
     bindings,
+    bitfield,
     container_of,
     device,
     device_id::{
@@ -17,6 +18,7 @@
         from_result,
         to_result, //
     },
+    fmt,
     io::resource,
     prelude::*,
     str::CStr,

rust/kernel/pci.rs macro_rules! module_pci_driver { · line 285

Quoted 5 lines
rust/kernel/pci.rs +2 -0
@@ -279,6 +281,33 @@ macro_rules! module_pci_driver {
 };
 }
 
+bitfield! {
+    /// Bus-Device-Function ID of a PCI device. Equivalent of C's `PCI_DEVID`.

You can use

    #[doc(alias = "PCI_DEVID")]

for doc searchability.

rust/kernel/pci.rs macro_rules! module_pci_driver { · line 293

Quoted 8 lines
rust/kernel/pci.rs +8 -0
+    pub struct Bdf(u16) {
+        /// Bus number.
+        15:8 bus;
+        /// Device number.
+        7:3 device;
+        /// Function number.
+        2:0 function;
+        /// Aggregation of `device` and `function`, which are commonly used together in code.

I wonder if this is actually true, or is this just a result of existing API being designed to bundle these together? I quick grep shows that the combined value is most commonly used to make API happy and packing/unpacking happens immediately before/after the API call.

For C code it is done this way before it avoids having bitfields. But given that we're using bitfields here already, we can just use device and function and omit devfn.

Best,
Gary

Quoted 46 lines
rust/kernel/pci.rs +19 -2
+        7:0 devfn;
+    }
+}
+
+/// Formats the ID as `bus:device.function`, like the corresponding part of C's `pci_name()`.
+impl fmt::Display for Bdf {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(
+            f,
+            "{:02x}:{:02x}.{}",
+            self.bus(),
+            self.device(),
+            self.function()
+        )
+    }
+}
+
 /// Abstraction for the PCI device ID structure ([`struct pci_device_id`]).
 ///
 /// [`struct pci_device_id`]: https://docs.kernel.org/PCI/pci.html#c.pci_device_id
@@ -700,10 +729,10 @@ pub fn revision_id(&self) -> u8 {
 
     /// Returns the PCI bus device/function.
     #[inline]
-    pub fn dev_id(&self) -> u16 {
+    pub fn dev_id(&self) -> Bdf {
         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
         // `struct pci_dev`.
-        unsafe { bindings::pci_dev_id(self.as_raw()) }
+        Bdf::from_raw(unsafe { bindings::pci_dev_id(self.as_raw()) })
     }
 
     /// Returns the PCI subsystem vendor ID.
samples/rust/rust_driver_sriov/pf.rs +1 -1
index 8f9958683528..c367c891abf4 100644
@@ -34,7 +34,7 @@ pub(crate) fn submit(vf: &pci::Device<Bound>) -> Result<u64> {
 
         dev_info!(
             pf.pdev,
-            "Handle PF request {} from VF devfn {:#x}.\n",
+            "Handle PF request {} from VF {}.\n",
             request,
             vf.dev_id()
         );
author maintainer
#5 lore raw

PATCH 3/3 gpu: nova-core: gsp: send the PCI domain, bus and device to GSP-RM

In reply to Alexandre Courbot

From: John Hubbard <[email protected]>

Open RM sends the PCI location of the GPU to GSP-RM as one word, which gpuEncodeDomainBusDevice() builds: the domain in bits 63:32, the bus number in bits 15:8 and the device number in bits 7:0.

Nova-core's system-info command sends the PCI device ID in that word
instead: the bus number, the device number and the function number, with no domain. The two words are equal only for a GPU at device 0, function 0 of domain 0, so GSP-RM receives a wrong location for a GPU anywhere else.

Send the domain, the bus number and the device number instead, in the layout that Open RM uses.

Fixes edcb134264f7 ("gpu: nova-core: gsp: Add SetSystemInfo command")
---
 drivers/gpu/nova-core/gsp/fw/commands.rs | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/fw/commands.rs +29 -1
index 37b89e62387f..a1a5e0d5569e 100644
@@ -88,7 +88,7 @@ pub(crate) fn init<'a>(
             gpuPhysAddr: dev.resource_start(0)?,
             gpuPhysFbAddr: dev.resource_start(1)?,
             gpuPhysInstAddr: dev.resource_start(3)?,
-            nvDomainBusDeviceFunc: u64::from(dev.dev_id().into_raw()),
+            nvDomainBusDeviceFunc: u64::from(DomainBusDevice::new(dev)),
 
             // Using TASK_SIZE in r535_gsp_rpc_set_system_info() seems wrong because
             // TASK_SIZE is per-task. That's probably a design issue in GSP-RM though.
@@ -377,6 +377,34 @@ struct GspInitRequest {
     }
 }
 
+bitfield! {
+    /// A GPU's PCI location as GSP-RM reads it: the domain, the bus number and the device number.
+    ///
+    /// The function is omitted on purpose.
+    struct DomainBusDevice(u64) {
+        /// PCI domain number.
+        63:32 domain;
+        /// PCI bus number.
+        15:8 bus;
+        /// PCI device number.
+        ///
+        /// Note: this field is larger than the equivalent one in [`pci::Bdf`]. This follows OpenRM.
+        7:0 device;
+    }
+}
+
+impl DomainBusDevice {
+    /// Returns the PCI location of `dev`.
+    fn new(dev: &pci::Device<device::Bound>) -> Self {
+        let dev_id = dev.dev_id();
+
+        Self::zeroed()
+            .with_domain(dev.domain_nr())
+            .with_bus(u8::from(dev_id.bus()))
+            .with_device(u8::from(dev_id.device()))
+    }
+}
+
 // Decode:
 
 // Should decode with UnknownKeyPolicy::Ignore.
Signature · 1 line
2.56.1