Skip to contentSkip to conversation

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

PATCH Applied Alexandre Courbot 3 participants 7 messages Dec 1

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

Alexandre Courbot

author maintainer
#0 lore raw

v3 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.

---
Changes in v3:
- Remove comment redundant with bitfield declaration (Gary).
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Move PCI_DEVID mention to its own doccomment line and reword it (Danilo).
- Add #[doc(alias = "PCI_DEVID")] (Gary).
- Remove devfn aggregate field (Gary).
- Link to v1: https://patch.msgid.link/[email protected]

---
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                       | 34 ++++++++++++++++++++++++++++++--
 samples/rust/rust_driver_sriov/pf.rs     |  2 +-
 4 files changed, 65 insertions(+), 4 deletions(-)

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

Best regards,

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

PATCH v3 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 v3 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                       | 34 ++++++++++++++++++++++++++++++--
 samples/rust/rust_driver_sriov/pf.rs     |  2 +-
 3 files changed, 34 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/fw/commands.rs +1 -1
index d6926709a368..dd658c5fa429 100644
@@ -89,7 +89,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 +32 -2
index 04a6d240afb2..078da469d98a 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,34 @@ macro_rules! module_pci_driver {
 };
 }
 
+bitfield! {
+    /// Bus-Device-Function ID of a PCI device.
+    ///
+    /// Uses the same layout as the C `PCI_DEVID` macro.
+    #[doc(alias = "PCI_DEVID")]
+    pub struct Bdf(u16) {
+        /// Bus number.
+        15:8 bus;
+        /// Device number.
+        7:3 device;
+        /// Function number.
+        2:0 function;
+    }
+}
+
+/// 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 +730,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
author maintainer
#3 lore raw

PATCH v3 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 dd658c5fa429..76f8d4a75562 100644
@@ -89,7 +89,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.
@@ -378,6 +378,34 @@ struct GspInitRequest {
     }
 }
 
+bitfield! {
+    /// A GPU's PCI location as GSP-RM reads it.
+    ///
+    /// 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
reviewer Reviewed-by on v3 3/3
#4 lore raw

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

Quoted 15 lines · 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.

Quoted 6 lines
Fixes edcb134264f7 ("gpu: nova-core: gsp: Add SetSystemInfo command")
Quoted 3 lines
---
 drivers/gpu/nova-core/gsp/fw/commands.rs | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
author maintainer Applied
#6 lore raw
In reply to Alexandre Courbot
Quoted 11 lines · Alexandre Courbot

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.

[...]

Applied to drm-rust-next, thanks!

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

      commit: d2abe8631d272ae5661613a4defe03fdc7317a6c
[2/3] rust: pci: return a bitfield type for dev_id
      commit: 76f1a24fac86482088d1d7d9e18e0951729e0466
[3/3] gpu: nova-core: gsp: send the PCI domain, bus and device to GSP-RM
      commit: c9a623455da336120f2aa54176e81e42c19c6020