The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Eliot Courtney" <ecourtney@nvidia.com>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>
Cc: "John Hubbard" <jhubbard@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Timur Tabi" <ttabi@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>, <nova-gpu@lists.linux.dev>,
	<dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>,
	"dri-devel" <dri-devel-bounces@lists.freedesktop.org>
Subject: Re: [PATCH v2 01/11] gpu: nova-core: gsp: add the r000 queue element header
Date: Mon, 30 Nov 2026 11:47:11 +0900	[thread overview]
Message-ID: <DN87AC8SCKQQ.EZH8X47KAA6X@nvidia.com> (raw)
In-Reply-To: <20261130-nova-r000-staging-v2-1-afd079026a61@nvidia.com>

On Mon Nov 30, 2026 at 12:11 AM JST, Alexandre Courbot wrote:
> 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>
> ---
>  drivers/gpu/nova-core/gsp/fw.rs                  | 183 ++++++++++++++++++++++-
>  drivers/gpu/nova-core/gsp/fw/r000_00/bindings.rs |  97 ++++++++++++
>  drivers/gpu/nova-core/mctp.rs                    |  27 +++-
>  3 files changed, 304 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/nova-core/gsp/fw.rs b/drivers/gpu/nova-core/gsp/fw.rs
> index 5c216d777dff..937cbbc24e93 100644
> --- a/drivers/gpu/nova-core/gsp/fw.rs
> +++ b/drivers/gpu/nova-core/gsp/fw.rs
> @@ -33,6 +33,11 @@
>      },
>  };
>  
> +use zerocopy_derive::{
> +    Immutable,
> +    KnownLayout, //
> +};
> +
>  use crate::{
>      bounded_enum,
>      fb::{
> @@ -48,6 +53,11 @@
>          cmdq::Cmdq, //
>          GSP_PAGE_SIZE,
>      },
> +    mctp::{
> +        MctpHeader,
> +        NvdmHeader,
> +        NvdmType, //
> +    },
>      num::{
>          self,
>          FromSafeCast, //
> @@ -835,7 +845,7 @@ pub(crate) fn msg_length(&self) -> usize {
>          size_of::<Self>() + self.rpc_header().rpc_length()
>      }
>  
> -    /// Returns the number of elements (i.e. memory pages) used by this message.
> +    /// Returns the number of slots (i.e. memory pages) used by this message.
>      pub(crate) fn element_count(&self) -> u32 {
>          self.inner.elemCount
>      }
> @@ -901,6 +911,177 @@ pub(crate) fn function(&self) -> Result<MsgFunction, u32> {
>      }
>  }
>  
> +/// First word of every r000 queue element: `"MCTP"` in ASCII.
> +const MCTP_MAGIC: u32 = 0x4D43_5450;

nit: make this a const of impl QueueElementHeader?

> +
> +/// 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)]
> +#[derive(FromBytes, IntoBytes, Immutable, KnownLayout)]
> +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.
> +const _: () = {
> +    use core::mem::offset_of;
> +
> +    // Offsets of `QueueElementHeader`'s fields
> +
> +    static_assert!(
> +        offset_of!(QueueElementHeader, mctp_magic)
> +            == offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, mctpMagic)
> +    );
> +    static_assert!(
> +        offset_of!(QueueElementHeader, mctp_payload_size)
> +            == offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, mctpPayloadSize)
> +    );
> +    static_assert!(
> +        offset_of!(QueueElementHeader, mctp_header)
> +            == offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, mctpHeader)
> +    );
> +    static_assert!(
> +        offset_of!(QueueElementHeader, nvdm_header)
> +            == 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)]

we aren't using static_assert! in this version anymore so i think these
dead_codes can go

> +    const QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET: usize =
> +        offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT, __bindgen_anon_1)
> +            + offset_of!(r000_00::GSP_MSG_QUEUE_ELEMENT__bindgen_ty_1, noEncryption);
> +
> +    static_assert!(
> +        offset_of!(QueueElementHeader, nvdm_payload_size)
> +            == QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET
> +                + offset_of!(QueueElementNoEncryption, nvdmPayloadSize)
> +    );
> +    static_assert!(
> +        offset_of!(QueueElementHeader, reserved)
> +            == QUEUE_ELEMENT_NO_ENCRYPTION_OFFSET + 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 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())

optional nit: it's fairly obvious that this add can never saturate so it
reads noisily to me

> +            || element_len > GSP_MSG_QUEUE_ELEMENT_SIZE_MAX
> +        {
> +            return Err(EIO);
> +        }
> +
> +        Ok(())
> +    }
> +}
> +
>  bounded_enum! {
>      /// Commands of the GMC API.
>      #[derive(Debug, Clone, Copy, PartialEq, Eq)]

[...]

> diff --git a/drivers/gpu/nova-core/mctp.rs b/drivers/gpu/nova-core/mctp.rs
> index f62e79f56b05..2058303dfbb9 100644
> --- a/drivers/gpu/nova-core/mctp.rs
> +++ b/drivers/gpu/nova-core/mctp.rs
> @@ -13,6 +13,11 @@
>      prelude::*, //
>  };
>  
> +use zerocopy_derive::{
> +    Immutable,
> +    KnownLayout, //
> +};
> +
>  use crate::{
>      bounded_enum,
>      num, //
> @@ -28,11 +33,16 @@ 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,
>      }
>  }
>  
>  bitfield! {
>      /// MCTP transport header for NVIDIA firmware messages.
> +    #[derive(FromBytes, IntoBytes, Immutable, KnownLayout)]
>      pub(crate) struct MctpHeader(u32) {
>          /// Start-of-message bit.
>          31:31 som;
> @@ -67,6 +77,11 @@ pub(crate) fn single_packet() -> Self {
>      pub(crate) fn is_single_packet(self) -> bool {
>          self.som().into_bool() && self.eom().into_bool()
>      }
> +
> +    /// Returns `true` if this MCTP header carries the version that this driver uses.
> +    pub(crate) fn has_expected_version(self) -> bool {
> +        u32::from(self.version()) == Self::VERSION

nit: unnecessary u32::from?

Reviewed-by: Eliot Courtney <ecourtney@nvidia.com>


  reply	other threads:[~2026-11-30  2:47 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 15:10 [PATCH v2 00/11] gpu: nova-core: gsp: preparatory patches for the r000 firmware Alexandre Courbot
2026-11-29 15:11 ` [PATCH v2 01/11] gpu: nova-core: gsp: add the r000 queue element header Alexandre Courbot
2026-11-30  2:47   ` Eliot Courtney [this message]
2026-11-29 15:11 ` [PATCH v2 02/11] gpu: nova-core: gsp: rename and move the static configuration type Alexandre Courbot
2026-11-30  2:20   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 03/11] gpu: nova-core: gsp: return the static GPU configuration from boot Alexandre Courbot
2026-11-30  2:22   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 04/11] gpu: nova-core: gsp: add the short name and GID to the GSP_INIT reply Alexandre Courbot
2026-11-30  2:23   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 05/11] gpu: nova-core: gsp: logbuffer: add LogBuffers::init_arguments() Alexandre Courbot
2026-11-30  2:27   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 06/11] gpu: nova-core: gsp: logbuffer: make LogBuffer generic over its size Alexandre Courbot
2026-11-30  2:28   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 07/11] gpu: nova-core: gsp: logbuffer: add RetainedLogBuffers::is_empty() Alexandre Courbot
2026-11-30  2:32   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 08/11] gpu: nova-core: gsp: cmdq: reduce visibility of gsp-level constants Alexandre Courbot
2026-11-30  2:32   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 09/11] gpu: nova-core: gsp: compute the msgq entry offset in MsgqTxHeader::new() Alexandre Courbot
2026-11-30  2:33   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 10/11] gpu: nova-core: gsp: take all the msgq layout values from Cmdq " Alexandre Courbot
2026-11-30  2:34   ` Eliot Courtney
2026-11-29 15:11 ` [PATCH v2 11/11] gpu: nova-core: firmware: tlv: add method to load the file referenced by a TLV Alexandre Courbot
2026-11-30  2:38   ` Eliot Courtney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DN87AC8SCKQQ.EZH8X47KAA6X@nvidia.com \
    --to=ecourtney@nvidia.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel-bounces@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jhubbard@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=simona@ffwll.ch \
    --cc=ttabi@nvidia.com \
    --cc=zhiw@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox