rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Hubbard <jhubbard@nvidia.com>
To: "Alexandre Courbot" <acourbot@nvidia.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"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>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>
Cc: Alistair Popple <apopple@nvidia.com>,
	Joel Fernandes <joelagnelf@nvidia.com>,
	Timur Tabi <ttabi@nvidia.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2 4/8] gpu: nova-core: firmware: process the GSP bootloader
Date: Wed, 27 Aug 2025 20:09:50 -0700	[thread overview]
Message-ID: <8cb256a0-bf25-4d14-8847-9c5e8f02533a@nvidia.com> (raw)
In-Reply-To: <20250826-nova_firmware-v2-4-93566252fe3a@nvidia.com>

On 8/25/25 9:07 PM, Alexandre Courbot wrote:
> The GSP bootloader is a small RISC-V firmware that is loaded by Booter
> onto the GSP core and is in charge of loading, validating, and starting
> the actual GSP firmware.
> 
> It is a regular binary firmware file containing a specific header.
> Create a type holding the DMA-mapped firmware as well as useful
> information extracted from the header, and hook it into our firmware
> structure for later use.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
>  drivers/gpu/nova-core/firmware.rs       |  7 ++-
>  drivers/gpu/nova-core/firmware/riscv.rs | 89 +++++++++++++++++++++++++++++++++
>  2 files changed, 94 insertions(+), 2 deletions(-)
> 

I've once again failed to find any problems with the code itself, and
have instead listed one or two trivial documentation suggestions, 
below. :)

Reviewed-by: John Hubbard <jhubbard@nvidia.com>


> diff --git a/drivers/gpu/nova-core/firmware.rs b/drivers/gpu/nova-core/firmware.rs
> index be190af1e11aec26c18c85324a185d135a16eabe..9bee0e0a0ab99d10be7e56d366970fdf4c813fc4 100644
> --- a/drivers/gpu/nova-core/firmware.rs
> +++ b/drivers/gpu/nova-core/firmware.rs
> @@ -12,6 +12,7 @@
>  use kernel::prelude::*;
>  use kernel::str::CString;
>  use kernel::transmute::FromBytes;
> +use riscv::RiscvFirmware;
>  
>  use crate::dma::DmaObject;
>  use crate::driver::Bar0;
> @@ -22,6 +23,7 @@
>  
>  pub(crate) mod booter;
>  pub(crate) mod fwsec;
> +pub(crate) mod riscv;
>  
>  pub(crate) const FIRMWARE_VERSION: &str = "535.113.01";
>  
> @@ -32,7 +34,8 @@ pub(crate) struct Firmware {
>      booter_loader: BooterFirmware,
>      /// Runs on the sec2 falcon engine to stop and unload a running GSP firmware.
>      booter_unloader: BooterFirmware,
> -    bootloader: firmware::Firmware,
> +    /// GSP bootloader, verifies the GSP firmware before loading and running it.
> +    gsp_bootloader: RiscvFirmware,
>      gsp: firmware::Firmware,
>  }
>  
> @@ -58,7 +61,7 @@ pub(crate) fn new(
>                  .and_then(|fw| BooterFirmware::new(dev, &fw, sec2, bar))?,
>              booter_unloader: request("booter_unload")
>                  .and_then(|fw| BooterFirmware::new(dev, &fw, sec2, bar))?,
> -            bootloader: request("bootloader")?,
> +            gsp_bootloader: request("bootloader").and_then(|fw| RiscvFirmware::new(dev, &fw))?,
>              gsp: request("gsp")?,
>          })
>      }
> diff --git a/drivers/gpu/nova-core/firmware/riscv.rs b/drivers/gpu/nova-core/firmware/riscv.rs
> new file mode 100644
> index 0000000000000000000000000000000000000000..926883230f2fe4e3327713e28b7fae31ebee60bb
> --- /dev/null
> +++ b/drivers/gpu/nova-core/firmware/riscv.rs
> @@ -0,0 +1,89 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Support for firmware binaries designed to run on a RISC-V cores. Such firmwares have a

typo: s/cores/core/

Also, as long as we're here: "Such firmwares" is understandable, but
"Such firmware files" is better.

> +//! dedicated header.
> +
> +use kernel::device;
> +use kernel::firmware::Firmware;
> +use kernel::prelude::*;
> +use kernel::transmute::FromBytes;
> +
> +use crate::dma::DmaObject;
> +use crate::firmware::BinFirmware;
> +
> +/// Descriptor for microcode running on a RISC-V core.
> +#[repr(C)]
> +#[derive(Debug)]
> +struct RmRiscvUCodeDesc {
> +    version: u32,
> +    bootloader_offset: u32,
> +    bootloader_size: u32,
> +    bootloader_param_offset: u32,
> +    bootloader_param_size: u32,
> +    riscv_elf_offset: u32,
> +    riscv_elf_size: u32,
> +    app_version: u32,
> +    manifest_offset: u32,
> +    manifest_size: u32,
> +    monitor_data_offset: u32,
> +    monitor_data_size: u32,
> +    monitor_code_offset: u32,
> +    monitor_code_size: u32,
> +}
> +
> +// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
> +unsafe impl FromBytes for RmRiscvUCodeDesc {}
> +
> +impl RmRiscvUCodeDesc {
> +    /// Interprets the header of `bin_fw` as a [`RmRiscvUCodeDesc`] and returns it.
> +    ///
> +    /// Fails if the header pointed at by `bin_fw` is not within the bounds of the firmware image.
> +    fn new(bin_fw: &BinFirmware<'_>) -> Result<Self> {
> +        let offset = bin_fw.hdr.header_offset as usize;
> +
> +        bin_fw
> +            .fw
> +            .get(offset..offset + size_of::<Self>())
> +            .and_then(Self::from_bytes_copy)
> +            .ok_or(EINVAL)
> +    }
> +}
> +
> +/// A parsed firmware for a RISC-V core, ready to be loaded and run.
> +#[expect(unused)]
> +pub(crate) struct RiscvFirmware {
> +    /// Offset at which the code starts in the firmware image.
> +    code_offset: u32,
> +    /// Offset at which the data starts in the firmware image.
> +    data_offset: u32,
> +    /// Offset at which the manifest starts in the firmware image.
> +    manifest_offset: u32,
> +    /// Application version.
> +    app_version: u32,
> +    /// Device-mapped firmware image.
> +    ucode: DmaObject,
> +}
> +
> +impl RiscvFirmware {
> +    // Parses the RISC-V firmware image contained in `fw`.

Should this be a "///" comment?

> +    pub(crate) fn new(dev: &device::Device<device::Bound>, fw: &Firmware) -> Result<Self> {
> +        let bin_fw = BinFirmware::new(fw)?;
> +
> +        let riscv_desc = RmRiscvUCodeDesc::new(&bin_fw)?;
> +
> +        let ucode = {
> +            let start = bin_fw.hdr.data_offset as usize;
> +            let len = bin_fw.hdr.data_size as usize;
> +
> +            DmaObject::from_data(dev, fw.data().get(start..start + len).ok_or(EINVAL)?)?
> +        };
> +
> +        Ok(Self {
> +            ucode,
> +            code_offset: riscv_desc.monitor_code_offset,
> +            data_offset: riscv_desc.monitor_data_offset,
> +            manifest_offset: riscv_desc.manifest_offset,
> +            app_version: riscv_desc.app_version,
> +        })
> +    }
> +}
> 

thanks,
-- 
John Hubbard


  reply	other threads:[~2025-08-28  3:09 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-26  4:07 [PATCH v2 0/8] gpu: nova-core: process and prepare more firmwares to boot GSP Alexandre Courbot
2025-08-26  4:07 ` [PATCH v2 1/8] rust: transmute: add `from_bytes_copy` method to `FromBytes` trait Alexandre Courbot
2025-08-26  6:50   ` Benno Lossin
2025-08-27  0:51   ` John Hubbard
2025-08-28  7:05     ` Alexandre Courbot
2025-08-28 11:26   ` Alexandre Courbot
2025-08-28 11:45     ` Miguel Ojeda
2025-08-29  1:51       ` Alexandre Courbot
2025-08-26  4:07 ` [PATCH v2 2/8] gpu: nova-core: firmware: add support for common firmware header Alexandre Courbot
2025-08-27  1:34   ` John Hubbard
2025-08-27  8:47     ` Alexandre Courbot
2025-08-27 21:50       ` John Hubbard
2025-08-28  7:08         ` Alexandre Courbot
2025-08-29  0:21           ` John Hubbard
2025-08-28 11:26       ` Miguel Ojeda
2025-08-26  4:07 ` [PATCH v2 3/8] gpu: nova-core: firmware: process Booter and patch its signature Alexandre Courbot
2025-08-27  2:29   ` John Hubbard
2025-08-28  7:19     ` Alexandre Courbot
2025-08-29  0:26       ` John Hubbard
2025-08-28 20:58   ` Timur Tabi
2025-08-26  4:07 ` [PATCH v2 4/8] gpu: nova-core: firmware: process the GSP bootloader Alexandre Courbot
2025-08-28  3:09   ` John Hubbard [this message]
2025-08-26  4:07 ` [PATCH v2 5/8] gpu: nova-core: firmware: process and prepare the GSP firmware Alexandre Courbot
2025-08-28  4:01   ` John Hubbard
2025-08-28 11:13     ` Alexandre Courbot
2025-08-29  0:27       ` John Hubbard
2025-08-28 11:27   ` Danilo Krummrich
2025-08-29 11:16     ` Alexandre Courbot
2025-08-30 12:56       ` Danilo Krummrich
2025-09-01  7:11         ` Alexandre Courbot
2025-08-26  4:07 ` [PATCH v2 6/8] gpu: nova-core: firmware: use 570.144 firmware Alexandre Courbot
2025-08-28  4:07   ` John Hubbard
2025-08-26  4:07 ` [PATCH v2 7/8] gpu: nova-core: Add base files for r570.144 firmware bindings Alexandre Courbot
2025-08-28  4:08   ` John Hubbard
2025-08-26  4:07 ` [PATCH v2 8/8] gpu: nova-core: compute layout of more framebuffer regions required for GSP Alexandre Courbot
2025-08-29 23:30   ` John Hubbard
2025-08-30  0:59     ` Alexandre Courbot
2025-08-30  5:46       ` John Hubbard
2025-08-27  0:29 ` [PATCH v2 0/8] gpu: nova-core: process and prepare more firmwares to boot GSP John Hubbard
2025-08-27  8:39   ` Alexandre Courbot
2025-08-27 21:56     ` John Hubbard
2025-08-28 20:44       ` Konstantin Ryabitsev
2025-08-29  0:33         ` 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=8cb256a0-bf25-4d14-8847-9c5e8f02533a@nvidia.com \
    --to=jhubbard@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=nouveau@lists.freedesktop.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=tzimmermann@suse.de \
    /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;
as well as URLs for NNTP newsgroup(s).