Re: [PATCH 1/4] gpu: nova-core: gsp: add the r000 queue element header
From: Alexandre Courbot
Date: Sun Nov 29 2026 - 10:00:46 EST
On Fri Nov 27, 2026 at 11:52 PM JST, Gary Guo wrote:
<...>
>> +/// 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 | QueueElementHeader: MCTP magic, element length,
>> +/// | | MCTP header, NVDM header, message length.
>> +/// +------------------------------------+
>> +/// | Message header | RPC header or 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 the queue element header, so wrapping the binding would make every element
>> +/// 16 bytes larger than what the GSP expects. The fields are therefore declared again here, with
>> +/// their offsets checked against the binding.
>> +#[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 transport header of the element.
>> + 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.
>
> If you just want "grouping" then
>
> const _: () = {
> };
>
> is usually the way and you don't need `super::*` with it.
Much better indeed, thanks!
>
>> +mod queue_element_header_validation {
>> + use super::*;
>> +
>> + // Offsets of `QueueElementHeader`'s fields
>> +
>> + static_assert!(
>> + core::mem::offset_of!(QueueElementHeader, mctp_magic)
>
> Perhaps import offset_of in the scope?
Good idea.
<...>
>> +#[expect(dead_code)]
>> +impl QueueElementHeader {
>> + /// Creates the queue element header of an element carrying a message of NVDM type `nvdm_type`
>> + /// and `message_len` bytes, message header included.
>> + ///
>> + /// # Errors
>> + ///
>> + /// - `EOVERFLOW` if a 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 element.
>> + 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 {}
>
> Use zerocopy derives instead perhaps?
That works yes. Most of the `fw` module still uses the old `transmute`
traits, but we will convert them as well eventually so we might as well
get the new ones right from the get-go.