Re: [PATCH 02/15] gpu: nova-core: gsp: add the GSP_INIT command and its reply
From: Eliot Courtney
Date: Fri Dec 04 2026 - 02:24:48 EST
On Thu Dec 3, 2026 at 9:09 PM JST, Alexandre Courbot wrote:
> From: John Hubbard <jhubbard@xxxxxxxxxx>
>
> The r000 boot protocol replaces the system-info, registry and
> static-info RPC commands with one GMC command, GSP_INIT, whose payload
> is an NVKV stream of key-value pairs. GSP-RM answers it once it has
> finished starting, with the static GPU configuration.
>
> Nova-core has the NVKV codec and the GSP_INIT schemas. It has no code
> that fills the request schema in from the PCI device, and no code that
> decodes the reply into the static configuration type that the boot
> sequence returns.
>
> Add the GSP_INIT command, whose variable payload is the encoded request,
> and decode its reply into the static configuration type, so that the
> rest of the driver reads the same configuration whichever firmware
> reported it.
>
> The request carries the registry keys that the r570 registry command
> sends, and it reports the architecture that the kernel is built for.
>
> Signed-off-by: John Hubbard <jhubbard@xxxxxxxxxx>
> [acourbot: declare `REGISTRY_ENTRIES` as const `Key`s.]
> [acourbot: avoid double-copy in `read`.]
> [acourbot: use `CStr` in `RegKey::new`.]
> [acourbot: return fixed-size arrays from `GspInitResponse`.]
> Co-developed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
> ---
> drivers/gpu/nova-core/gsp/commands.rs | 3 +
> drivers/gpu/nova-core/gsp/commands/gmc.rs | 133 +++++++++++++++++++++++++
> drivers/gpu/nova-core/gsp/fw/commands.rs | 155 +++++++++++++++++++++++++++---
> 3 files changed, 279 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/gpu/nova-core/gsp/commands.rs b/drivers/gpu/nova-core/gsp/commands.rs
> index 91f63f704e55..b48cf30978cf 100644
> --- a/drivers/gpu/nova-core/gsp/commands.rs
> +++ b/drivers/gpu/nova-core/gsp/commands.rs
> @@ -1,6 +1,9 @@
> // SPDX-License-Identifier: GPL-2.0
> // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
>
> +// The r570 firmware does not use GMC.
> +#[expect(dead_code)]
> +pub(crate) mod gmc;
> pub(crate) mod rpc;
>
> use core::{
> diff --git a/drivers/gpu/nova-core/gsp/commands/gmc.rs b/drivers/gpu/nova-core/gsp/commands/gmc.rs
> new file mode 100644
> index 000000000000..f74ada78f7ac
> --- /dev/null
> +++ b/drivers/gpu/nova-core/gsp/commands/gmc.rs
> @@ -0,0 +1,133 @@
> +// SPDX-License-Identifier: GPL-2.0
> +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
> +
> +//! GMC (GPU Management Controller) API commands.
> +
> +use core::convert::Infallible;
> +
> +use kernel::{
> + device,
> + pci,
> + prelude::*,
> + time::Delta,
> + transmute::AsBytes, //
> +};
> +
> +use crate::{
> + gpu::Chipset,
> + gsp::{
> + cmdq::gmc::{
> + CommandToGsp,
> + MessageFromGsp, //
> + },
> + commands::GspStaticInfo,
> + fw::{
> + commands::{
> + GspInitRequest,
> + GspInitResponseSchema, //
> + },
> + GmcCommandId, //
> + },
> + nvkv::{
> + Decoder,
> + Encodable,
> + Encoder,
> + Schema,
> + UnknownKeyPolicy, //
> + },
> + },
> + sbuffer::SBufferIter,
> +};
> +
> +/// The `GSP_INIT` command, which carries the system information and registry keys to GSP-RM.
> +///
> +/// GSP-RM answers it with the static GPU configuration once it has finished starting.
> +pub(crate) struct GspInit {
> + /// The NVKV-encoded request.
> + payload: KVVec<u64>,
> +}
> +
> +impl GspInit {
> + /// Builds the request for `pdev`.
> + ///
> + /// # Errors
> + ///
> + /// - `ENOMEM` if the request or the encoder buffer cannot be allocated.
> + pub(crate) fn new(pdev: &pci::Device<device::Bound>, chipset: Chipset) -> Result<Self> {
> + let mut encoder = Encoder::new();
> + GspInitRequest::new(pdev, chipset)?.encode(&mut encoder)?;
> +
> + Ok(Self {
> + payload: encoder.finish(),
> + })
> + }
> +
> + /// Returns the request as the bytes that the GSP reads.
> + fn payload_bytes(&self) -> &[u8] {
> + AsBytes::as_bytes(self.payload.as_slice())
Can we use zerocopy intobytes stuff here instead?
> + }
> +}
> +
> +impl CommandToGsp for GspInit {
> + const COMMAND: GmcCommandId = GmcCommandId::GspInit;
> + /// Largest reply that the driver accepts, in bytes.
> + const MAX_REPLY_SIZE: u32 = 48 * 1024;
I know this is what OpenRM uses, but for stability purposes it might be
safer to use a larger value. I guess the largest value we can use would
be
`GSP_MSG_QUEUE_ELEMENT_SIZE_MAX - size_of::<QueueElementHeader>() - size_of::<GmcApiHeader>()`
Perhaps as a new constant (GMC_MAX_PAYLOAD_SIZE, for example).
> + /// `GspInit` can take longer than 5 seconds to complete on some SKUs.
> + const COMPLETION_TIMEOUT: Delta = Delta::from_secs(15);
> + type Command = ();
> + type Reply = GspStaticInfo;
> + type InitError = Infallible;
> +
> + fn init(&self) -> impl Init<Self::Command, Self::InitError> {
> + pin_init::init_zeroed()
> + }
> +
> + fn variable_payload_len(&self) -> usize {
> + self.payload_bytes().len()
> + }
> +
> + fn init_variable_payload(
> + &self,
> + dst: &mut SBufferIter<core::array::IntoIter<&mut [u8], 2>>,
> + ) -> Result {
> + dst.write_all(self.payload_bytes())
> + }
> +}
> +
> +impl MessageFromGsp for GspStaticInfo {
> + type InitError = Error;
> +
> + /// Decodes the `GSP_INIT` reply, an NVKV stream, into the static configuration.
> + ///
> + /// # Errors
> + ///
> + /// - `EINVAL` if the payload is not a whole number of NVKV words, if the stream is malformed
> + /// or omits a required key, or if GSP-RM reported no framebuffer region.
> + /// - `ENOMEM` if the words or the decoded regions cannot be allocated.
> + fn read(payload: &mut SBufferIter<core::array::IntoIter<&[u8], 2>>) -> Result<Self> {
> + let mut words = KVVec::new();
> + while !payload.is_empty() {
> + let mut word = [0u8; size_of::<u64>()];
> + payload.read_exact(&mut word)?;
> + words.push(u64::from_le_bytes(word), GFP_KERNEL)?;
> + }
This code will probably get repeated, so some options could be adding a
try_into_u64_vec function SBufferIter (ala flush_into_kvec), or exposing
a len() method so we can read_exact into a preallocated KVVec<u64>.
Unfortunately we can't easily let the decoder run directly on
SBufferIter, because DecoderValue takes direct references to NVKV
arrays, so it requires the memory to be contiguous. OTOH it may be
better for us to quickly copy out of the command queue and return the
lock anyway. So I think that is compatible with the direction of
adding a kvvec<u64> readout to SBufferIter. WDYT?
> +
> + let decoder = Decoder::new(&words, UnknownKeyPolicy::Ignore);
> + let mut schema = KBox::init(GspInitResponseSchema::init(), GFP_KERNEL)?;
> + let reply = KBox::try_init(decoder.decode(&mut *schema)?, GFP_KERNEL)?;
> +
> + let mut usable_fb_regions = KVec::new();
> + for region in reply.usable_fb_regions() {
> + usable_fb_regions.push(region, GFP_KERNEL)?;
> + }
> +
> + Ok(GspStaticInfo {
> + gpu_name: reply.gpu_name(),
> + gpu_short_name: reply.gpu_short_name(),
> + gpu_gid: reply.gpu_gid(),
> + bar1_pde_base: reply.bar1_pde_base(),
> + usable_fb_regions,
> + total_fb_end: reply.total_fb_end().ok_or(EINVAL)?,
> + })
> + }
> +}
> diff --git a/drivers/gpu/nova-core/gsp/fw/commands.rs b/drivers/gpu/nova-core/gsp/fw/commands.rs
> index 76f8d4a75562..c7f544775ce9 100644
> --- a/drivers/gpu/nova-core/gsp/fw/commands.rs
> +++ b/drivers/gpu/nova-core/gsp/fw/commands.rs
> @@ -331,6 +331,25 @@ fn from(value: HostArch) -> Self {
> }
> }
>
> +impl HostArch {
> + /// Returns the variant that names the architecture for which this kernel is built.
> + fn host() -> Self {
> + if cfg!(target_arch = "x86_64") {
> + Self::X86_64
> + } else if cfg!(target_arch = "aarch64") {
> + Self::Aarch64
> + } else if cfg!(all(target_arch = "powerpc64", target_endian = "little")) {
> + Self::Ppc64le
> + } else if cfg!(target_arch = "arm") {
> + Self::Arm
> + } else if cfg!(target_arch = "riscv64") {
> + Self::Riscv64
> + } else {
> + Self::None
> + }
> + }
> +}
> +
> nvkv_encode! {
> /// A GSP registry entry.
> struct RegKey {
> @@ -339,6 +358,18 @@ struct RegKey {
> }
> }
>
> +impl RegKey {
> + /// Creates a registry entry.
> + ///
> + /// GSP-RM counts the NUL terminator in the encoded name length.
> + const fn new(key_name: &'static CStr, key_value: u32) -> Self {
> + Self {
> + key_name: Key::new(key_name.to_bytes_with_nul()),
> + key_value: Key::new(key_value),
> + }
> + }
> +}
> +
> impl Encodable for KVVec<RegKey> {
> fn encode(&self, encoder: &mut Encoder) -> Result {
> for regkey in self {
> @@ -362,10 +393,7 @@ struct VfInfo {
>
> nvkv_encode! {
> /// Payload of the `GSP_INIT` command.
> - // TODO: expect() doesn't work here before rustc 1.97.0
> - // https://github.com/rust-lang/rust/pull/154377
> - #[allow(dead_code)]
> - struct GspInitRequest {
Unfortunately this still causes errors on some versions of rustc. I have
sent a prereq patch that should fix this [1].
[1]: https://lore.kernel.org/all/20261204-nvkv-build-fix-v1-1-72b42a776511@xxxxxxxxxx/
> + pub(crate) struct GspInitRequest {
> pci_device_id: Key<u32, 0x0001>,
> pci_sub_device_id: Key<u32, 0x0002>,
> pci_revision_id: Key<u32, 0x0003>,
I noticed that openrm additionally sends HOST_OS_TYPE, HOST_PAGE_SIZE,
SYS_TIMER_OFFSET_NS; and
DEFAULT_TIMEOUT_US_{VGPU,GRAPHICS_MODE,COMPUTE_MODE} in r620. We even
have, in timer.rs, a comment saying that it should be passed to GspInit.
Should we be passing these values too?
I see that Alok has sent one adding the default timeouts [2].
We also appear to have dropped sending these which were sent in r570:
NVGMC_SI_GPU_PHYS_ADDR
NVGMC_SI_GPU_PHYS_FB_ADDR
NVGMC_SI_GPU_PHYS_INST_ADDR
NVGMC_SI_MAX_USER_VA
[2]: https://lore.kernel.org/all/20261117061053.915254-1-alkumar@xxxxxxxxxx/
> @@ -406,15 +434,55 @@ fn new(dev: &pci::Device<device::Bound>) -> Self {
> }
> }
>
> +/// Registry entries that the driver sends to GSP-RM on every boot.
> +const REGISTRY_ENTRIES: [RegKey; 3] = [
> + // Enables PCI secondary bus reset.
> + RegKey::new(c"RMSecBusResetEnable", 1),
> + // Makes GSP-RM preserve PCI configuration registers across any PCI reset.
> + RegKey::new(c"RMForcePcieConfigSave", 1),
> + // Lets GSP-RM boot when the PCI device id is absent from its product name database.
> + RegKey::new(c"RMDevidCheckIgnore", 1),
> +];
> +
> +impl GspInitRequest {
> + /// Creates the request for `dev`.
> + ///
> + /// The registry keys are [`REGISTRY_ENTRIES`].
> + ///
> + /// # Errors
> + ///
> + /// - `ENOMEM` if the registry list cannot be allocated.
> + pub(crate) fn new(dev: &pci::Device<device::Bound>, chipset: Chipset) -> Result<Self> {
> + let mut regkeys = KVVec::new();
> + for entry in REGISTRY_ENTRIES {
> + regkeys.push(entry, GFP_KERNEL)?;
> + }
> +
> + let mirror = chipset.pci_config_mirror_range();
openrm sends (0, 0) for this for gb100,102. It's not an error in this
patch but let me send a follow up to fix this.
> + let device_id = (u32::from(dev.device_id()) << 16) | u32::from(dev.vendor_id().as_raw());
> + let sub_device_id =
> + (u32::from(dev.subsystem_device_id()) << 16) | u32::from(dev.subsystem_vendor_id());
optional nit: We could define a new bitfield (could be used for both of
these) instead of manually bitpacking. WDYT?