[PATCH 02/15] gpu: nova-core: gsp: add the GSP_INIT command and its reply
From: Alexandre Courbot
Date: Thu Dec 03 2026 - 07:12:14 EST
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())
+ }
+}
+
+impl CommandToGsp for GspInit {
+ const COMMAND: GmcCommandId = GmcCommandId::GspInit;
+ /// Largest reply that the driver accepts, in bytes.
+ const MAX_REPLY_SIZE: u32 = 48 * 1024;
+ /// `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)?;
+ }
+
+ 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 {
+ pub(crate) struct GspInitRequest {
pci_device_id: Key<u32, 0x0001>,
pci_sub_device_id: Key<u32, 0x0002>,
pci_revision_id: Key<u32, 0x0003>,
@@ -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();
+ 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());
+
+ Ok(Self {
+ pci_device_id: device_id.into(),
+ pci_sub_device_id: sub_device_id.into(),
+ pci_revision_id: u32::from(dev.revision_id()).into(),
+ pci_config_mirror_base: mirror.start.into(),
+ pci_config_mirror_size: (mirror.end - mirror.start).into(),
+ host_arch: HostArch::host().into(),
+ bus_device_func: u64::from(DomainBusDevice::new(dev)).into(),
+ regkeys,
+ vf_info: None,
+ })
+ }
+}
+
// Decode:
// Should decode with UnknownKeyPolicy::Ignore.
nvkv_decode! {
/// Schema for the `GSP_INIT` response.
- // TODO: expect() doesn't work here before rustc 1.97.0
- // https://github.com/rust-lang/rust/pull/154377
- #[allow(dead_code)]
- struct GspInitResponseSchema => GspInitResponse {
+ pub(crate) struct GspInitResponseSchema => GspInitResponse {
gpu_name: TruncatingArray<u8, { GspStaticInfo::MAX_GPU_NAME_LEN }, 0x2000>,
gpu_short_name: TruncatingArray<u8, { GspStaticInfo::MAX_GPU_NAME_LEN }, 0x2001>,
gpu_gid: TruncatingArray<u8, { GspStaticInfo::GID_LEN }, 0x2002>,
@@ -425,10 +493,7 @@ struct GspInitResponseSchema => GspInitResponse {
}
/// Payload of the `GSP_INIT` response.
-// TODO: expect() doesn't work here before rustc 1.97.0
-// https://github.com/rust-lang/rust/pull/154377
-#[allow(dead_code)]
-struct GspInitResponse {
+pub(crate) struct GspInitResponse {
gpu_name: ArrayVec<u8, { GspStaticInfo::MAX_GPU_NAME_LEN }>,
gpu_short_name: ArrayVec<u8, { GspStaticInfo::MAX_GPU_NAME_LEN }>,
gpu_gid: ArrayVec<u8, { GspStaticInfo::GID_LEN }>,
@@ -437,6 +502,72 @@ struct GspInitResponse {
vmmu_segment_size: u64,
}
+/// Copies `src` to the start of a zero-initialized array.
+fn zero_padded_array<const N: usize>(src: &ArrayVec<u8, N>) -> [u8; N] {
+ let mut dst = [0u8; N];
+ // PANIC: `src.len()` never exceeds `N` per the invariant of `ArrayVec`.
+ dst[..src.len()].copy_from_slice(src);
+ dst
+}
+
+impl GspInitResponse {
+ /// Tag of a general-purpose region. Any other tag marks a region that GSP-RM reserves for the
+ /// use that the tag names.
+ const FB_REGION_TAG_NONE: u32 = 0;
+
+ /// Returns the GPU name, zero-padded to [`GspStaticInfo::MAX_GPU_NAME_LEN`] bytes.
+ pub(crate) fn gpu_name(&self) -> [u8; GspStaticInfo::MAX_GPU_NAME_LEN] {
+ zero_padded_array(&self.gpu_name)
+ }
+
+ /// Returns the short GPU name, zero-padded to [`GspStaticInfo::MAX_GPU_NAME_LEN`] bytes.
+ pub(crate) fn gpu_short_name(&self) -> [u8; GspStaticInfo::MAX_GPU_NAME_LEN] {
+ zero_padded_array(&self.gpu_short_name)
+ }
+
+ /// Returns the SHA-1 based GPU identifier (GID), zero-padded to [`GspStaticInfo::GID_LEN`]
+ /// bytes.
+ pub(crate) fn gpu_gid(&self) -> [u8; GspStaticInfo::GID_LEN] {
+ zero_padded_array(&self.gpu_gid)
+ }
+
+ /// Returns an iterator over the FB regions from which the driver may allocate: the
+ /// general-purpose regions that are not protected and that support both compression and
+ /// isochronous access.
+ pub(crate) fn usable_fb_regions(&self) -> impl Iterator<Item = Range<u64>> + '_ {
+ self.fb_regions.iter().filter_map(|region| {
+ if region.limit >= region.base
+ && region.tag == Self::FB_REGION_TAG_NONE
+ && !region.flags.protected()
+ && region.flags.support_compressed()
+ && region.flags.support_iso()
+ {
+ region.limit.checked_add(1).map(|end| region.base..end)
+ } else {
+ None
+ }
+ })
+ }
+
+ /// Returns the exclusive end of the FB physical address space, which spans every region
+ /// including the ones that [`Self::usable_fb_regions`] leaves out.
+ ///
+ /// Returns `None` if no region that GSP-RM reported has a limit at or above its base.
+ pub(crate) fn total_fb_end(&self) -> Option<u64> {
+ self.fb_regions
+ .iter()
+ .filter(|region| region.limit >= region.base)
+ .map(|region| region.limit)
+ .max()?
+ .checked_add(1)
+ }
+
+ /// Returns the BAR1 page directory entry base address.
+ pub(crate) fn bar1_pde_base(&self) -> u64 {
+ self.bar1_pde_base
+ }
+}
+
nvkv_decode! {
/// Schema for one FB region of the `GSP_INIT` response.
struct FbRegionSchema => FbRegion {
--
2.56.1