Rust for Linux List
 help / color / mirror / Atom feed
From: John Hubbard <jhubbard@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>
Cc: "Timur Tabi" <ttabi@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Eliot Courtney" <ecourtney@nvidia.com>,
	"Zhi Wang" <zhiw@nvidia.com>, "David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
	LKML <linux-kernel@vger.kernel.org>,
	linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"John Hubbard" <jhubbard@nvidia.com>
Subject: [PATCH 7/9] gpu: nova-core: gsp: add the r000 load-and-execute HS binary handler
Date: Thu, 19 Nov 2026 20:47:02 -0800	[thread overview]
Message-ID: <20261120044704.165223-8-jhubbard@nvidia.com> (raw)
In-Reply-To: <20261120044704.165223-1-jhubbard@nvidia.com>

The GSP falcon is a microcontroller with two cores, a falcon core and a
RISC-V core, and only one of the two is active at a time. GSP-RM runs on
the RISC-V core.

On GA102 and later, the r000 GSP-RM requires a Heavy-Secured (HS) binary
to run on the falcon core during boot. GSP-RM is suspended while the
binary runs.

The driver runs the binary on behalf of GSP-RM, in the following steps:

1. GSP-RM places the binary in the framebuffer.
2. GSP-RM raises a load-and-execute event.
3. GSP-RM suspends the RISC-V core.
4. The driver waits until the RISC-V core is suspended.
5. The driver resets the GSP falcon, which selects the falcon core.
6. The driver loads the binary from the framebuffer into the GSP falcon.
7. The driver starts the binary and waits for the falcon core to halt.
8. The driver resumes GSP-RM on the RISC-V core.

Nova-core has no handler for the load-and-execute event.

Add the handler, which performs steps 4 through 8.

Co-developed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
 drivers/gpu/nova-core/falcon.rs        |   5 +-
 drivers/gpu/nova-core/gsp/load_exec.rs | 203 ++++++++++++++++++++++++-
 drivers/gpu/nova-core/sbuffer.rs       |   1 -
 3 files changed, 199 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
index 75954856ad7c..459a0a88146a 100644
--- a/drivers/gpu/nova-core/falcon.rs
+++ b/drivers/gpu/nova-core/falcon.rs
@@ -145,7 +145,6 @@ pub(crate) enum FalconDmaSource<'a> {
     /// A DMA object in system memory.
     Sysmem(&'a Coherent<'a, [u8]>),
     /// GPU framebuffer memory at physical address `base`.
-    #[expect(dead_code)]
     Framebuffer { base: u64 },
 }
 
@@ -563,7 +562,7 @@ pub(crate) fn with_fbif_transcfg<R>(
     }
 
     /// Perform a DMA write according to `load_offsets` from `src` into the falcon's `target_mem`.
-    fn dma_wr(
+    pub(crate) fn dma_wr(
         &self,
         src: FalconDmaSource<'_>,
         target_mem: FalconMem,
@@ -699,7 +698,7 @@ fn dma_load<F: FalconFirmware<Target = E> + FalconDmaLoadable>(&self, fw: &F) ->
     }
 
     /// Programs the BROM registers with `brom_params` and the `BOOTVEC` register with `boot_addr`.
-    fn program_brom_and_bootvec(&self, brom_params: &FalconBromParams, boot_addr: u32) {
+    pub(crate) fn program_brom_and_bootvec(&self, brom_params: &FalconBromParams, boot_addr: u32) {
         self.hal.program_brom(self, brom_params);
 
         self.pfalcon
diff --git a/drivers/gpu/nova-core/gsp/load_exec.rs b/drivers/gpu/nova-core/gsp/load_exec.rs
index bf465ac1151f..d9a4ccd88289 100644
--- a/drivers/gpu/nova-core/gsp/load_exec.rs
+++ b/drivers/gpu/nova-core/gsp/load_exec.rs
@@ -1,21 +1,50 @@
 // SPDX-License-Identifier: GPL-2.0
 // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
-//! The core reset and the core resume of the GSP falcon.
+//! The load-and-execute events of the GSP boot.
+//!
+//! See `Documentation/gpu/nova/core/falcon.rst`:
+//!
+//! - "Falcon security levels" describes the Heavy-Secured (HS) mode of a falcon and the boot ROM
+//!   that verifies an HS binary. The note in "Root of trust establishment" says why the GSP falcon
+//!   stops running GSP-RM, runs an HS binary, and then resumes GSP-RM.
+//! - "Falcon memory subsystem and DMA engine" describes the instruction memory (IMEM) and the data
+//!   memory (DMEM) of a falcon, and the DMA engine that loads a binary into them.
+
+use core::array;
 
 use kernel::{
     device,
     dma::DmaAddress,
     prelude::*,
-    time::Delta, //
+    time::Delta,
+    transmute::FromBytes, //
 };
 
-use crate::falcon::{
-    gsp::Gsp,
-    sec2::Sec2,
-    Falcon, //
+use crate::{
+    falcon::{
+        gsp::Gsp,
+        sec2::Sec2,
+        Falcon,
+        FalconBromParams,
+        FalconDmaLoadTarget,
+        FalconDmaSource,
+        FalconMem, //
+    },
+    gsp::{
+        cmdq::gmc::MessageFromGsp,
+        GspBootContext, //
+    },
+    sbuffer::SBufferIter, //
 };
 
+/// DMEM virtual address value that means "no virtual address assigned".
+const FLCN_DMEM_VA_INVALID: u32 = 0xffff_ffff;
+
+/// `MAILBOX0` value that means that a falcon binary has not started. A binary that reports its
+/// status through `MAILBOX0` replaces it.
+const FLCN_ERR_BINARY_NOT_STARTED: u32 = 0xfe;
+
 /// Resets the GSP falcon and its DMA-related registers.
 pub(super) fn reset_gsp_falcon(gsp_falcon: &Falcon<'_, Gsp>) -> Result {
     gsp_falcon.reset()?;
@@ -80,3 +109,165 @@ pub(super) fn resume_gsp_rm(
     }
     Ok(())
 }
+
+/// Waits until GSP-RM has suspended the RISC-V core, and then resets the GSP falcon.
+fn reset_gsp_falcon_after_suspend(dev: &device::Device, gsp_falcon: &Falcon<'_, Gsp>) -> Result {
+    gsp_falcon.wait_for_processor_suspend().inspect_err(|_| {
+        dev_err!(
+            dev,
+            "Timeout waiting for GSP-RM (RISC-V core) to suspend (mbox0={:#x})\n",
+            gsp_falcon.read_mailbox0()
+        );
+    })?;
+
+    reset_gsp_falcon(gsp_falcon)
+}
+
+/// Runs the Heavy-Secured (HS) binary of a `GMCAPI_CMD_EXEC_HS_BINARY` event on the falcon core of
+/// the GSP falcon, and then resumes GSP-RM.
+///
+/// `params` describes the binary. The handler:
+///
+/// 1. Waits until GSP-RM has suspended the RISC-V core, for up to five seconds.
+/// 2. Resets the GSP falcon, which selects the falcon core.
+/// 3. Loads the code segment into IMEM and the data segment into DMEM, by DMA from the
+///    framebuffer.
+/// 4. Programs the boot ROM parameters and the boot vector, and writes
+///    [`FLCN_ERR_BINARY_NOT_STARTED`] to `MAILBOX0`.
+/// 5. Starts the falcon core, and waits until the falcon core halts, for up to two seconds. The
+///    binary halts the falcon core when it is done, so the handler learns from the halt that the
+///    binary has finished.
+/// 6. Logs an error if `MAILBOX0` still holds [`FLCN_ERR_BINARY_NOT_STARTED`], which means that
+///    the binary did not start.
+/// 7. Resumes GSP-RM on the RISC-V core, via [`resume_gsp_rm`].
+///
+/// A binary that reports a status writes the status to `MAILBOX0` before it halts. The handler
+/// does not check that status, so the result does not depend on it.
+///
+/// # Errors
+///
+/// - `EINVAL` if the engine ID mask or the ucode ID of `params` does not fit its
+///   [`FalconBromParams`] field.
+/// - `ETIMEDOUT` if GSP-RM has not suspended the RISC-V core within five seconds, or if the
+///   falcon core has not halted within two seconds of its start.
+///
+/// Errors from the reset of the GSP falcon, the DMA transfers and the resume of GSP-RM are
+/// propagated as-is.
+#[expect(dead_code)]
+fn handle_hs_binary(
+    ctx: &GspBootContext<'_, '_>,
+    libos_dma_address: DmaAddress,
+    bootloader_app_version: u32,
+    params: &HsBinaryParams,
+) -> Result {
+    let dev = ctx.dev();
+    let gsp_falcon = ctx.gsp_falcon;
+    let brom_params = FalconBromParams {
+        pkc_data_offset: params.hs_sig_dmem_addr,
+        engine_id_mask: u16::try_from(params.engine_id_mask).map_err(|_| EINVAL)?,
+        ucode_id: u8::try_from(params.ucode_id).map_err(|_| EINVAL)?,
+    };
+
+    reset_gsp_falcon_after_suspend(dev, gsp_falcon)?;
+
+    gsp_falcon.dma_wr(
+        FalconDmaSource::Framebuffer {
+            base: params.imem_dma_base,
+        },
+        FalconMem::ImemSecure,
+        FalconDmaLoadTarget {
+            src_start: params.imem_va,
+            dst_start: params.imem_pa,
+            len: params.imem_size,
+        },
+    )?;
+
+    // A data segment with a virtual address is tagged with it. One without starts at the DMA base.
+    let tag_dmem = params.dmem_va != FLCN_DMEM_VA_INVALID;
+
+    gsp_falcon.dma_wr(
+        FalconDmaSource::Framebuffer {
+            base: params.dmem_dma_base,
+        },
+        FalconMem::Dmem { tag: tag_dmem },
+        FalconDmaLoadTarget {
+            src_start: if tag_dmem { params.dmem_va } else { 0 },
+            dst_start: params.dmem_pa,
+            len: params.dmem_size,
+        },
+    )?;
+
+    gsp_falcon.program_brom_and_bootvec(&brom_params, params.imem_va);
+
+    let (mbox0, _) = gsp_falcon
+        .boot(Some(FLCN_ERR_BINARY_NOT_STARTED), None)
+        .inspect_err(|_| {
+            dev_err!(
+                dev,
+                "Timeout waiting for HS binary to halt (mbox0={:#x})\n",
+                gsp_falcon.read_mailbox0()
+            );
+        })?;
+    if mbox0 == FLCN_ERR_BINARY_NOT_STARTED {
+        dev_err!(dev, "HS binary did not start\n");
+    } else {
+        dev_dbg!(dev, "HS binary halted with mbox0={:#x}\n", mbox0);
+    }
+
+    resume_gsp_rm(
+        dev,
+        gsp_falcon,
+        ctx.sec2_falcon,
+        libos_dma_address,
+        bootloader_app_version,
+    )
+}
+
+/// Payload of a `GMCAPI_CMD_EXEC_HS_BINARY` event, which describes the HS binary to run.
+#[repr(C)]
+struct HsBinaryParams {
+    /// DMA base of the code segment, a physical address in the framebuffer. The code segment starts
+    /// at `imem_dma_base + imem_va`.
+    imem_dma_base: u64,
+    /// DMA base of the data segment, a physical address in the framebuffer. The data segment starts
+    /// at `dmem_dma_base + dmem_va`, or at `dmem_dma_base` when the data segment has no virtual
+    /// address.
+    dmem_dma_base: u64,
+    _reserved64: [u64; 2],
+    /// Virtual address of the code segment. The binary starts executing at this address.
+    imem_va: u32,
+    /// Physical address in IMEM at which the code segment is loaded.
+    imem_pa: u32,
+    /// Size in bytes of the code segment.
+    imem_size: u32,
+    /// Virtual address of the data segment, or [`FLCN_DMEM_VA_INVALID`] if the data segment has
+    /// no virtual address. The DMA transfer writes no tags in that case.
+    dmem_va: u32,
+    /// Physical address in DMEM at which the data segment is loaded.
+    dmem_pa: u32,
+    /// Size in bytes of the data segment.
+    dmem_size: u32,
+    /// Physical address in DMEM of the signature of the binary.
+    hs_sig_dmem_addr: u32,
+    /// Mask of the falcon engines on which the binary can run, with one bit per engine.
+    engine_id_mask: u32,
+    /// ID number of the binary. Each ID has a fuse register that holds a version number. The boot
+    /// ROM uses the ID and that version number as inputs of the signature verification.
+    ucode_id: u32,
+    _reserved32: [u32; 3],
+}
+
+// SAFETY: This struct only contains integer types for which all bit patterns are valid.
+unsafe impl FromBytes for HsBinaryParams {}
+
+impl MessageFromGsp for HsBinaryParams {
+    type InitError = Error;
+
+    fn read(payload: &mut SBufferIter<array::IntoIter<&[u8], 2>>) -> Result<Self> {
+        let mut bytes = [0u8; size_of::<Self>()];
+
+        payload.read_exact(&mut bytes)?;
+
+        Self::from_bytes_copy(&bytes).ok_or(EINVAL)
+    }
+}
diff --git a/drivers/gpu/nova-core/sbuffer.rs b/drivers/gpu/nova-core/sbuffer.rs
index 3a41d224c77a..b8c01104c255 100644
--- a/drivers/gpu/nova-core/sbuffer.rs
+++ b/drivers/gpu/nova-core/sbuffer.rs
@@ -146,7 +146,6 @@ fn get_slice(&mut self, len: usize) -> Option<&'a [u8]> {
 
     /// Ideally we would implement `Read`, but it is not available in `core`.
     /// So mimic `std::io::Read::read_exact`.
-    #[expect(unused)]
     pub(crate) fn read_exact(&mut self, mut dst: &mut [u8]) -> Result {
         while !dst.is_empty() {
             match self.get_slice(dst.len()) {
-- 
2.56.1


  parent reply	other threads:[~2026-11-20  4:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  4:46 [PATCH 0/9] gpu: nova-core: run falcon binaries for the r000 GSP-RM John Hubbard
2026-11-20  4:46 ` [PATCH 1/9] rust: pci: add domain_nr() accessor John Hubbard
2026-11-20  4:46 ` [PATCH 2/9] gpu: nova-core: falcon: factor out the wait for the GSP suspend John Hubbard
2026-11-20  4:46 ` [PATCH 3/9] gpu: nova-core: falcon: factor out the BROM and BOOTVEC programming John Hubbard
2026-11-20  4:46 ` [PATCH 4/9] gpu: nova-core: gsp: factor out the core reset and the core resume John Hubbard
2026-11-20  4:47 ` [PATCH 5/9] gpu: nova-core: gsp: fix resume_gsp_rm()'s error messages John Hubbard
2026-11-20  4:47 ` [PATCH 6/9] gpu: nova-core: falcon: select the BAR2 engine ID for framebuffer DMA John Hubbard
2026-11-20  4:47 ` John Hubbard [this message]
2026-11-20  4:47 ` [PATCH 8/9] gpu: nova-core: gsp: add the r000 load-and-execute bootloader handler John Hubbard
2026-11-20  4:47 ` [PATCH 9/9] gpu: nova-core: gsp: add the load-and-execute event dispatcher John Hubbard

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=20261120044704.165223-8-jhubbard@nvidia.com \
    --to=jhubbard@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ecourtney@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=work@onurozkan.dev \
    --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