mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] gpu: nova-core: gsp: add the r000 queue element header
@ 2026-11-16 15:03 Alexandre Courbot
  0 siblings, 0 replies; only message in thread
From: Alexandre Courbot @ 2026-11-16 15:03 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, David Airlie, Simona Vetter
  Cc: John Hubbard, Alistair Popple, Timur Tabi, Eliot Courtney,
	Zhi Wang, nova-gpu, linux-kernel, Alexandre Courbot

From: John Hubbard <jhubbard@nvidia.com>

The upcoming r000 firmware uses a new queue element header that includes
MCTP and NVDM headers describing the type of message (RPC or GMC) that
follows.

Add that header as the `QueueElementHeader` type, along with the two
NVDM types that the GSP queues use and the MCTP version and NVDM vendor
checks that validating it requires.

The type is written out field by field rather than wrapped around the
generated binding, as `GSP_MSG_QUEUE_ELEMENT` keeps a 16-byte encryption
tag, only used with confidential computing, in a union with its last two
fields, making the binding 16 bytes larger than what the GSP expects on
the queues. Static assertions check every field offset and the size of
the type against the binding.

The header has no user yet: it replaces `GspMsgElement` at the transport
level in a later patch that switches to the r000 firmware, and is built
as dead code until then.

Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Co-developed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
This patch is extracted from John's r000 series [1], as a dependency for
the rest of the series and the following vGPU work.

Based on drm-rust-next.

[1] https://lore.kernel.org/20260918010719.1176945-1-jhubbard@nvidia.com
---
 drivers/gpu/nova-core/gsp/fw.rs                  | 187 ++++++++++++++++++++++-
 drivers/gpu/nova-core/gsp/fw/r000_00/bindings.rs |  97 ++++++++++++
 drivers/gpu/nova-core/mctp.rs                    |  20 ++-
 3 files changed, 301 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/nova-core/gsp/fw.rs b/drivers/gpu/nova-core/gsp/fw.rs
index 5c216d777dff..0db998192ccc 100644
--- a/drivers/gpu/nova-core/gsp/fw.rs
+++ b/drivers/gpu/nova-core/gsp/fw.rs
@@ -48,6 +48,11 @@
         cmdq::Cmdq, //
         GSP_PAGE_SIZE,
     },
+    mctp::{
+        MctpHeader,
+        NvdmHeader,
+        NvdmType, //
+    },
     num::{
         self,
         FromSafeCast, //
@@ -901,6 +906,186 @@ pub(crate) fn function(&self) -> Result<MsgFunction, u32> {
     }
 }
 
+/// First word of every r000 queue element: `"MCTP"` in ASCII.
+const MCTP_MAGIC: u32 = 0x4D43_5450;
+
+/// Header opening every element of the r000 GSP queues.
+///
+/// A queue element is laid out as follows:
+///
+/// ```text
+///     +------------------------------------+
+///     | Queue element header               |  This type: MCTP magic, element length, MCTP
+///     |                                    |  header, NVDM header, message length.
+///     +------------------------------------+
+///     | Message header                     |  The RPC header or the GMC API header. The NVDM
+///     |                                    |  type selects between the two.
+///     +------------------------------------+
+///     | Payload                            |  Message-specific data.
+///     +------------------------------------+
+/// ```
+///
+/// This is the `noEncryption` layout of the `GSP_MSG_QUEUE_ELEMENT` binding. That binding keeps
+/// a 16-byte encryption tag, used only with confidential computing, in a union with the two
+/// trailing fields of this header, so wrapping it would make every element 16 bytes larger than
+/// what the GSP expects. The fields are thus declared again here, with their offsets checked
+/// against the binding. OpenRM has the same split and calls the size of this layout
+/// `GSP_MSG_QUEUE_ELEMENT_SIZE_NO_ENCRYPTION`.
+#[repr(C)]
+pub(crate) struct QueueElementHeader {
+    /// Always [`MCTP_MAGIC`].
+    mctp_magic: u32,
+    /// Length in bytes of the whole element, this header included.
+    ///
+    /// Under confidential computing the GSP pads elements out to whole queue slots, so this can
+    /// be larger than `nvdm_payload_size` plus the size of this header, but never smaller.
+    mctp_payload_size: u32,
+    /// MCTP header of the message.
+    mctp_header: MctpHeader,
+    /// NVDM header of the message.
+    nvdm_header: NvdmHeader,
+    /// Length in bytes of the message following this header: the message header and its
+    /// payload.
+    nvdm_payload_size: u32,
+    reserved: u32,
+}
+
+/// Static validation of [`QueueElementHeader`] against its bindings type. Kept in a sub-module to
+/// stay out of the way of the rest of the code.
+mod queue_element_header_validation {
+    use super::*;
+
+    // Offsets of `QueueElementHeader`'s fields
+
+    static_assert!(
+        core::mem::offset_of!(QueueElementHeader, mctp_magic)
+            == core::mem::offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, mctpMagic)
+    );
+    static_assert!(
+        core::mem::offset_of!(QueueElementHeader, mctp_payload_size)
+            == core::mem::offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, mctpPayloadSize)
+    );
+    static_assert!(
+        core::mem::offset_of!(QueueElementHeader, mctp_header)
+            == core::mem::offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, mctpHeader)
+    );
+    static_assert!(
+        core::mem::offset_of!(QueueElementHeader, nvdm_header)
+            == core::mem::offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, nvdmHeader)
+    );
+
+    /// The `noEncryption` member of the union of `GSP_MSG_QUEUE_ELEMENT`, which holds the two
+    /// trailing fields of [`QueueElementHeader`].
+    // Using `allow` instead of `expect` as rustc 1.85 ignores uses from `static_assert!` and
+    // reports this as dead code.
+    #[allow(dead_code)]
+    type QueueElementNoEncryption = r000_00::GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1__bindgen_ty_2;
+
+    // Offsets of fields under [`QueueElementNoEncryption`] within `GSP_MSG_QUEUE_ELEMENT`.
+
+    // Using `allow` instead of `expect` as rustc 1.85 ignores uses from `static_assert!` and
+    // reports this as dead code.
+    #[allow(dead_code)]
+    const QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET: usize =
+        core::mem::offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, __bindgen_anon_1)
+            + core::mem::offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1, noEncryption);
+
+    static_assert!(
+        core::mem::offset_of!(QueueElementHeader, nvdm_payload_size)
+            == QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET
+                + core::mem::offset_of!(QueueElementNoEncryption, nvdmPayloadSize)
+    );
+    static_assert!(
+        core::mem::offset_of!(QueueElementHeader, reserved)
+            == QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET
+                + core::mem::offset_of!(QueueElementNoEncryption, reserved)
+    );
+
+    // Ensure the size of `QueueElementHeader` matches the `noEncryption` layout.
+
+    static_assert!(
+        size_of::<QueueElementHeader>()
+            == QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET + size_of::<QueueElementNoEncryption>()
+    );
+}
+
+#[expect(dead_code)]
+impl QueueElementHeader {
+    /// Creates the header of an element carrying a message of NVDM type `nvdm_type` and
+    /// `message_len` bytes, message header included.
+    ///
+    /// # Errors
+    ///
+    /// - `EOVERFLOW` if the total length does not fit its 32-bit field.
+    pub(crate) fn init(nvdm_type: NvdmType, message_len: usize) -> impl Init<Self, Error> {
+        try_init!(QueueElementHeader {
+            mctp_magic: MCTP_MAGIC,
+            mctp_payload_size: size_of::<Self>()
+                .checked_add(message_len)
+                .ok_or(EOVERFLOW)?
+                .try_into()
+                .map_err(|_| EOVERFLOW)?,
+            mctp_header: MctpHeader::single_packet(),
+            nvdm_header: NvdmHeader::new(nvdm_type),
+            nvdm_payload_size: message_len.try_into().map_err(|_| EOVERFLOW)?,
+            reserved: 0,
+        })
+    }
+
+    /// Returns the length of the whole element, this header included.
+    pub(crate) fn element_len(&self) -> usize {
+        num::u32_as_usize(self.mctp_payload_size)
+    }
+
+    /// Returns the length of the message following this header, message header included.
+    pub(crate) fn message_len(&self) -> usize {
+        num::u32_as_usize(self.nvdm_payload_size)
+    }
+
+    /// Returns the number of slots (i.e. memory pages) used by this message.
+    pub(crate) fn element_count(&self) -> u32 {
+        self.mctp_payload_size.div_ceil(cv!(GSP_PAGE_SIZE))
+    }
+
+    /// Returns the NVDM type of the message following this header, or `EINVAL` if it is not a
+    /// known one.
+    pub(crate) fn nvdm_type(&self) -> Result<NvdmType> {
+        self.nvdm_header.nvdm_type()
+    }
+
+    /// Validates the framing of the element.
+    ///
+    /// # Errors
+    ///
+    /// - `EIO` if the magic, the MCTP version or the NVDM vendor is wrong, or if the element
+    ///   length is smaller than this header and the message together, or larger than the
+    ///   maximum element size.
+    pub(crate) fn validate(&self) -> Result {
+        if self.mctp_magic != MCTP_MAGIC
+            || !self.mctp_header.has_expected_version()
+            || !self.nvdm_header.has_nvidia_vendor()
+        {
+            return Err(EIO);
+        }
+
+        let element_len = self.element_len();
+        if element_len < size_of::<Self>().saturating_add(self.message_len())
+            || element_len > GSP_MSG_QUEUE_ELEMENT_SIZE_MAX
+        {
+            return Err(EIO);
+        }
+
+        Ok(())
+    }
+}
+
+// SAFETY: All fields are integer types or transparent wrappers over one, with no padding.
+unsafe impl AsBytes for QueueElementHeader {}
+
+// SAFETY: All fields are integer types or transparent wrappers over one, for which all bit
+// patterns are valid.
+unsafe impl FromBytes for QueueElementHeader {}
+
 bounded_enum! {
     /// Commands of the GMC API.
     #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -926,7 +1111,7 @@ pub(crate) struct GmcCommandWord(u32) {
 
 /// Header of a GMC (GPU Management Controller) API message.
 ///
-/// In a queue element, it follows the queue element header and precedes the payload of the
+/// In a queue element, it follows the [`QueueElementHeader`] and precedes the payload of the
 /// command.
 #[repr(transparent)]
 pub(crate) struct GmcApiHeader {
diff --git a/drivers/gpu/nova-core/gsp/fw/r000_00/bindings.rs b/drivers/gpu/nova-core/gsp/fw/r000_00/bindings.rs
index 552299c98169..959e397c26aa 100644
--- a/drivers/gpu/nova-core/gsp/fw/r000_00/bindings.rs
+++ b/drivers/gpu/nova-core/gsp/fw/r000_00/bindings.rs
@@ -30,6 +30,49 @@ fn fmt(&self, fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
         fmt.write_str("__IncompleteArrayField")
     }
 }
+#[repr(C)]
+pub struct __BindgenUnionField<T>(::core::marker::PhantomData<T>);
+impl<T> __BindgenUnionField<T> {
+    #[inline]
+    pub const fn new() -> Self {
+        __BindgenUnionField(::core::marker::PhantomData)
+    }
+    #[inline]
+    pub unsafe fn as_ref(&self) -> &T {
+        ::core::mem::transmute(self)
+    }
+    #[inline]
+    pub unsafe fn as_mut(&mut self) -> &mut T {
+        ::core::mem::transmute(self)
+    }
+}
+impl<T> ::core::default::Default for __BindgenUnionField<T> {
+    #[inline]
+    fn default() -> Self {
+        Self::new()
+    }
+}
+impl<T> ::core::clone::Clone for __BindgenUnionField<T> {
+    #[inline]
+    fn clone(&self) -> Self {
+        *self
+    }
+}
+impl<T> ::core::marker::Copy for __BindgenUnionField<T> {}
+impl<T> ::core::fmt::Debug for __BindgenUnionField<T> {
+    fn fmt(&self, fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
+        fmt.write_str("__BindgenUnionField")
+    }
+}
+impl<T> ::core::hash::Hash for __BindgenUnionField<T> {
+    fn hash<H: ::core::hash::Hasher>(&self, _state: &mut H) {}
+}
+impl<T> ::core::cmp::PartialEq for __BindgenUnionField<T> {
+    fn eq(&self, _other: &__BindgenUnionField<T>) -> bool {
+        true
+    }
+}
+impl<T> ::core::cmp::Eq for __BindgenUnionField<T> {}
 pub type __u8 = ffi::c_uchar;
 pub type __u16 = ffi::c_ushort;
 pub type __u32 = ffi::c_uint;
@@ -39,6 +82,60 @@ fn fmt(&self, fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
 pub type u32_ = __u32;
 pub type u64_ = __u64;
 #[repr(C)]
+#[derive(Debug, Default, Copy, Clone, MaybeZeroable)]
+pub struct GSP_MSG_QUEUE_ENCRYPTION_TAG {
+    pub authTagBuffer: [u8_; 16usize],
+}
+#[repr(C)]
+#[derive(MaybeZeroable)]
+pub struct GSP_MSG_QUEUE_ELEMENT {
+    pub mctpMagic: u32_,
+    pub mctpPayloadSize: u32_,
+    pub mctpHeader: u32_,
+    pub nvdmHeader: u32_,
+    pub __bindgen_anon_1: GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1,
+}
+#[repr(C)]
+#[derive(MaybeZeroable)]
+pub struct GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1 {
+    pub withEncryption: __BindgenUnionField<GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1__bindgen_ty_1>,
+    pub noEncryption: __BindgenUnionField<GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1__bindgen_ty_2>,
+    pub bindgen_union_field: [u32; 6usize],
+}
+#[repr(C)]
+#[derive(Debug, Default, MaybeZeroable)]
+pub struct GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1__bindgen_ty_1 {
+    pub encryptionTag: GSP_MSG_QUEUE_ENCRYPTION_TAG,
+    pub nvdmPayloadSize: u32_,
+    pub reserved: u32_,
+    pub payload: __IncompleteArrayField<u8_>,
+}
+#[repr(C)]
+#[derive(Debug, Default, MaybeZeroable)]
+pub struct GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1__bindgen_ty_2 {
+    pub nvdmPayloadSize: u32_,
+    pub reserved: u32_,
+    pub payload: __IncompleteArrayField<u8_>,
+}
+impl Default for GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1 {
+    fn default() -> Self {
+        let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
+        unsafe {
+            ::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
+            s.assume_init()
+        }
+    }
+}
+impl Default for GSP_MSG_QUEUE_ELEMENT {
+    fn default() -> Self {
+        let mut s = ::core::mem::MaybeUninit::<Self>::uninit();
+        unsafe {
+            ::core::ptr::write_bytes(s.as_mut_ptr(), 0, 1);
+            s.assume_init()
+        }
+    }
+}
+#[repr(C)]
 #[derive(MaybeZeroable)]
 pub struct GMCAPI_HEADER {
     pub command: u32_,
diff --git a/drivers/gpu/nova-core/mctp.rs b/drivers/gpu/nova-core/mctp.rs
index f62e79f56b05..392fa7c8baf6 100644
--- a/drivers/gpu/nova-core/mctp.rs
+++ b/drivers/gpu/nova-core/mctp.rs
@@ -28,6 +28,10 @@ pub(crate) enum NvdmType with TryFrom<Bounded<u32, 8>> {
         Cot = 0x14,
         /// FSP command response.
         FspResponse = 0x15,
+        /// RPC message to or from GSP-RM.
+        RmRpc = 0x25,
+        /// GMC (GPU Management Controller) API message to or from GSP-RM.
+        GmcApi = 0x26,
     }
 }
 
@@ -67,6 +71,11 @@ pub(crate) fn single_packet() -> Self {
     pub(crate) fn is_single_packet(self) -> bool {
         self.som().into_bool() && self.eom().into_bool()
     }
+
+    /// Returns whether this header carries the MCTP version this driver uses.
+    pub(crate) fn has_expected_version(self) -> bool {
+        u32::from(self.version()) == Self::VERSION
+    }
 }
 
 /// MCTP message type for PCI vendor-defined messages.
@@ -93,10 +102,17 @@ pub(crate) fn new(nvdm_type: NvdmType) -> Self {
             .with_nvdm_type(nvdm_type)
     }
 
-    /// Validates this header against the expected NVIDIA NVDM format and type.
-    pub(crate) fn validate(self, expected_type: NvdmType) -> bool {
+    /// Returns whether this is the header of a PCI vendor-defined message from NVIDIA.
+    ///
+    /// The GPU firmware only ever sends such messages, so a mismatch is a sign of corruption.
+    pub(crate) fn has_nvidia_vendor(self) -> bool {
         u8::from(self.msg_type()) == MSG_TYPE_VENDOR_PCI
             && u16::from(self.vendor_id()) == Vendor::NVIDIA.as_raw()
+    }
+
+    /// Validates this header against the expected NVIDIA NVDM format and type.
+    pub(crate) fn validate(self, expected_type: NvdmType) -> bool {
+        self.has_nvidia_vendor()
             && matches!(self.nvdm_type(), Ok(nvdm_type) if nvdm_type == expected_type)
     }
 }

---
base-commit: 8045d0e7d208ad389a387f1407ce0db9070805af
change-id: 20261116-msgelement-09c976928d64

Best regards,
--  
Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16 15:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 15:03 [PATCH] gpu: nova-core: gsp: add the r000 queue element header Alexandre Courbot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®