Skip to content

Commit 015b1d3

Browse files
committed
gpu: nova-core: firmware: process the GSP bootloader
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. The GSP bootloader is stored into the `GspFirmware` structure, since it is part of the GSP firmware package. This makes the `Firmware` structure empty, so remove it. Reviewed-by: John Hubbard <jhubbard@nvidia.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250913-nova_firmware-v6-8-9007079548b0@nvidia.com Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
1 parent a841614 commit 015b1d3

4 files changed

Lines changed: 99 additions & 21 deletions

File tree

drivers/gpu/nova-core/firmware.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use kernel::transmute::FromBytes;
1515
use crate::dma::DmaObject;
1616
use crate::falcon::FalconFirmware;
1717
use crate::gpu;
18-
use crate::gpu::Chipset;
1918

2019
pub(crate) mod booter;
2120
pub(crate) mod fwsec;
2221
pub(crate) mod gsp;
22+
pub(crate) mod riscv;
2323

2424
pub(crate) const FIRMWARE_VERSION: &str = "535.113.01";
2525

@@ -36,22 +36,6 @@ fn request_firmware(
3636
.and_then(|path| firmware::Firmware::request(&path, dev))
3737
}
3838

39-
/// Structure encapsulating the firmware blobs required for the GPU to operate.
40-
#[expect(dead_code)]
41-
pub(crate) struct Firmware {
42-
bootloader: firmware::Firmware,
43-
}
44-
45-
impl Firmware {
46-
pub(crate) fn new(dev: &device::Device, chipset: Chipset, ver: &str) -> Result<Firmware> {
47-
let request = |name| request_firmware(dev, chipset, name, ver);
48-
49-
Ok(Firmware {
50-
bootloader: request("bootloader")?,
51-
})
52-
}
53-
}
54-
5539
/// Structure used to describe some firmwares, notably FWSEC-FRTS.
5640
#[repr(C)]
5741
#[derive(Debug, Clone)]

drivers/gpu/nova-core/firmware/gsp.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use kernel::prelude::*;
99
use kernel::scatterlist::{Owned, SGTable};
1010

1111
use crate::dma::DmaObject;
12+
use crate::firmware::riscv::RiscvFirmware;
1213
use crate::gpu::{Architecture, Chipset};
1314
use crate::gsp::GSP_PAGE_SIZE;
1415

@@ -131,6 +132,8 @@ pub(crate) struct GspFirmware {
131132
size: usize,
132133
/// Device-mapped GSP signatures matching the GPU's [`Chipset`].
133134
signatures: DmaObject,
135+
/// GSP bootloader, verifies the GSP firmware before loading and running it.
136+
bootloader: RiscvFirmware,
134137
}
135138

136139
impl GspFirmware {
@@ -164,6 +167,9 @@ impl GspFirmware {
164167
})
165168
.map_err(|_| ENOMEM)?;
166169

170+
let bl = super::request_firmware(dev, chipset, "bootloader", ver)?;
171+
let bootloader = RiscvFirmware::new(dev, &bl)?;
172+
167173
Ok(try_pin_init!(Self {
168174
fw <- SGTable::new(dev, fw_vvec, DataDirection::ToDevice, GFP_KERNEL),
169175
level2 <- {
@@ -206,6 +212,7 @@ impl GspFirmware {
206212
},
207213
size,
208214
signatures,
215+
bootloader,
209216
}))
210217
}
211218

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Support for firmware binaries designed to run on a RISC-V core. Such firmwares files have a
4+
//! dedicated header.
5+
6+
use core::mem::size_of;
7+
8+
use kernel::device;
9+
use kernel::firmware::Firmware;
10+
use kernel::prelude::*;
11+
use kernel::transmute::FromBytes;
12+
13+
use crate::dma::DmaObject;
14+
use crate::firmware::BinFirmware;
15+
16+
/// Descriptor for microcode running on a RISC-V core.
17+
#[repr(C)]
18+
#[derive(Debug)]
19+
struct RmRiscvUCodeDesc {
20+
version: u32,
21+
bootloader_offset: u32,
22+
bootloader_size: u32,
23+
bootloader_param_offset: u32,
24+
bootloader_param_size: u32,
25+
riscv_elf_offset: u32,
26+
riscv_elf_size: u32,
27+
app_version: u32,
28+
manifest_offset: u32,
29+
manifest_size: u32,
30+
monitor_data_offset: u32,
31+
monitor_data_size: u32,
32+
monitor_code_offset: u32,
33+
monitor_code_size: u32,
34+
}
35+
36+
// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
37+
unsafe impl FromBytes for RmRiscvUCodeDesc {}
38+
39+
impl RmRiscvUCodeDesc {
40+
/// Interprets the header of `bin_fw` as a [`RmRiscvUCodeDesc`] and returns it.
41+
///
42+
/// Fails if the header pointed at by `bin_fw` is not within the bounds of the firmware image.
43+
fn new(bin_fw: &BinFirmware<'_>) -> Result<Self> {
44+
let offset = bin_fw.hdr.header_offset as usize;
45+
46+
bin_fw
47+
.fw
48+
.get(offset..offset + size_of::<Self>())
49+
.and_then(Self::from_bytes_copy)
50+
.ok_or(EINVAL)
51+
}
52+
}
53+
54+
/// A parsed firmware for a RISC-V core, ready to be loaded and run.
55+
#[expect(unused)]
56+
pub(crate) struct RiscvFirmware {
57+
/// Offset at which the code starts in the firmware image.
58+
code_offset: u32,
59+
/// Offset at which the data starts in the firmware image.
60+
data_offset: u32,
61+
/// Offset at which the manifest starts in the firmware image.
62+
manifest_offset: u32,
63+
/// Application version.
64+
app_version: u32,
65+
/// Device-mapped firmware image.
66+
ucode: DmaObject,
67+
}
68+
69+
impl RiscvFirmware {
70+
/// Parses the RISC-V firmware image contained in `fw`.
71+
pub(crate) fn new(dev: &device::Device<device::Bound>, fw: &Firmware) -> Result<Self> {
72+
let bin_fw = BinFirmware::new(fw)?;
73+
74+
let riscv_desc = RmRiscvUCodeDesc::new(&bin_fw)?;
75+
76+
let ucode = {
77+
let start = bin_fw.hdr.data_offset as usize;
78+
let len = bin_fw.hdr.data_size as usize;
79+
80+
DmaObject::from_data(dev, fw.data().get(start..start + len).ok_or(EINVAL)?)?
81+
};
82+
83+
Ok(Self {
84+
ucode,
85+
code_offset: riscv_desc.monitor_code_offset,
86+
data_offset: riscv_desc.monitor_data_offset,
87+
manifest_offset: riscv_desc.manifest_offset,
88+
app_version: riscv_desc.app_version,
89+
})
90+
}
91+
}

drivers/gpu/nova-core/gpu.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use kernel::{device, devres::Devres, error::code::*, pci, prelude::*, sync::Arc}
55
use crate::driver::Bar0;
66
use crate::falcon::{gsp::Gsp as GspFalcon, sec2::Sec2 as Sec2Falcon, Falcon};
77
use crate::fb::SysmemFlush;
8-
use crate::firmware::{Firmware, FIRMWARE_VERSION};
98
use crate::gfw;
109
use crate::gsp::Gsp;
1110
use crate::regs;
@@ -175,7 +174,6 @@ pub(crate) struct Gpu {
175174
spec: Spec,
176175
/// MMIO mapping of PCI BAR 0
177176
bar: Arc<Devres<Bar0>>,
178-
fw: Firmware,
179177
/// System memory page required for flushing all pending GPU-side memory writes done through
180178
/// PCIE into system memory, via sysmembar (A GPU-initiated HW memory-barrier operation).
181179
sysmem_flush: SysmemFlush,
@@ -211,8 +209,6 @@ impl Gpu {
211209
.inspect_err(|_| dev_err!(pdev.as_ref(), "GFW boot did not complete"))?;
212210
},
213211

214-
fw <- Firmware::new(pdev.as_ref(), spec.chipset, FIRMWARE_VERSION)?,
215-
216212
sysmem_flush: SysmemFlush::register(pdev.as_ref(), bar, spec.chipset)?,
217213

218214
gsp_falcon: Falcon::new(

0 commit comments

Comments
 (0)