Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/gpu/nova-core/firmware.rs
29278 views
1
// SPDX-License-Identifier: GPL-2.0
2
3
//! Contains structures and functions dedicated to the parsing, building and patching of firmwares
4
//! to be loaded into a given execution unit.
5
6
use core::marker::PhantomData;
7
use core::mem::size_of;
8
9
use kernel::device;
10
use kernel::firmware;
11
use kernel::prelude::*;
12
use kernel::str::CString;
13
use kernel::transmute::FromBytes;
14
15
use crate::dma::DmaObject;
16
use crate::falcon::FalconFirmware;
17
use crate::gpu;
18
19
pub(crate) mod booter;
20
pub(crate) mod fwsec;
21
pub(crate) mod gsp;
22
pub(crate) mod riscv;
23
24
pub(crate) const FIRMWARE_VERSION: &str = "570.144";
25
26
/// Requests the GPU firmware `name` suitable for `chipset`, with version `ver`.
27
fn request_firmware(
28
dev: &device::Device,
29
chipset: gpu::Chipset,
30
name: &str,
31
ver: &str,
32
) -> Result<firmware::Firmware> {
33
let chip_name = chipset.name();
34
35
CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}-{ver}.bin"))
36
.and_then(|path| firmware::Firmware::request(&path, dev))
37
}
38
39
/// Structure used to describe some firmwares, notably FWSEC-FRTS.
40
#[repr(C)]
41
#[derive(Debug, Clone)]
42
pub(crate) struct FalconUCodeDescV3 {
43
/// Header defined by `NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*` in OpenRM.
44
hdr: u32,
45
/// Stored size of the ucode after the header.
46
stored_size: u32,
47
/// Offset in `DMEM` at which the signature is expected to be found.
48
pub(crate) pkc_data_offset: u32,
49
/// Offset after the code segment at which the app headers are located.
50
pub(crate) interface_offset: u32,
51
/// Base address at which to load the code segment into `IMEM`.
52
pub(crate) imem_phys_base: u32,
53
/// Size in bytes of the code to copy into `IMEM`.
54
pub(crate) imem_load_size: u32,
55
/// Virtual `IMEM` address (i.e. `tag`) at which the code should start.
56
pub(crate) imem_virt_base: u32,
57
/// Base address at which to load the data segment into `DMEM`.
58
pub(crate) dmem_phys_base: u32,
59
/// Size in bytes of the data to copy into `DMEM`.
60
pub(crate) dmem_load_size: u32,
61
/// Mask of the falcon engines on which this firmware can run.
62
pub(crate) engine_id_mask: u16,
63
/// ID of the ucode used to infer a fuse register to validate the signature.
64
pub(crate) ucode_id: u8,
65
/// Number of signatures in this firmware.
66
pub(crate) signature_count: u8,
67
/// Versions of the signatures, used to infer a valid signature to use.
68
pub(crate) signature_versions: u16,
69
_reserved: u16,
70
}
71
72
impl FalconUCodeDescV3 {
73
/// Returns the size in bytes of the header.
74
pub(crate) fn size(&self) -> usize {
75
const HDR_SIZE_SHIFT: u32 = 16;
76
const HDR_SIZE_MASK: u32 = 0xffff0000;
77
78
((self.hdr & HDR_SIZE_MASK) >> HDR_SIZE_SHIFT) as usize
79
}
80
}
81
82
/// Trait implemented by types defining the signed state of a firmware.
83
trait SignedState {}
84
85
/// Type indicating that the firmware must be signed before it can be used.
86
struct Unsigned;
87
impl SignedState for Unsigned {}
88
89
/// Type indicating that the firmware is signed and ready to be loaded.
90
struct Signed;
91
impl SignedState for Signed {}
92
93
/// A [`DmaObject`] containing a specific microcode ready to be loaded into a falcon.
94
///
95
/// This is module-local and meant for sub-modules to use internally.
96
///
97
/// After construction, a firmware is [`Unsigned`], and must generally be patched with a signature
98
/// before it can be loaded (with an exception for development hardware). The
99
/// [`Self::patch_signature`] and [`Self::no_patch_signature`] methods are used to transition the
100
/// firmware to its [`Signed`] state.
101
struct FirmwareDmaObject<F: FalconFirmware, S: SignedState>(DmaObject, PhantomData<(F, S)>);
102
103
/// Trait for signatures to be patched directly into a given firmware.
104
///
105
/// This is module-local and meant for sub-modules to use internally.
106
trait FirmwareSignature<F: FalconFirmware>: AsRef<[u8]> {}
107
108
impl<F: FalconFirmware> FirmwareDmaObject<F, Unsigned> {
109
/// Patches the firmware at offset `sig_base_img` with `signature`.
110
fn patch_signature<S: FirmwareSignature<F>>(
111
mut self,
112
signature: &S,
113
sig_base_img: usize,
114
) -> Result<FirmwareDmaObject<F, Signed>> {
115
let signature_bytes = signature.as_ref();
116
if sig_base_img + signature_bytes.len() > self.0.size() {
117
return Err(EINVAL);
118
}
119
120
// SAFETY: We are the only user of this object, so there cannot be any race.
121
let dst = unsafe { self.0.start_ptr_mut().add(sig_base_img) };
122
123
// SAFETY: `signature` and `dst` are valid, properly aligned, and do not overlap.
124
unsafe {
125
core::ptr::copy_nonoverlapping(signature_bytes.as_ptr(), dst, signature_bytes.len())
126
};
127
128
Ok(FirmwareDmaObject(self.0, PhantomData))
129
}
130
131
/// Mark the firmware as signed without patching it.
132
///
133
/// This method is used to explicitly confirm that we do not need to sign the firmware, while
134
/// allowing us to continue as if it was. This is typically only needed for development
135
/// hardware.
136
fn no_patch_signature(self) -> FirmwareDmaObject<F, Signed> {
137
FirmwareDmaObject(self.0, PhantomData)
138
}
139
}
140
141
/// Header common to most firmware files.
142
#[repr(C)]
143
#[derive(Debug, Clone)]
144
struct BinHdr {
145
/// Magic number, must be `0x10de`.
146
bin_magic: u32,
147
/// Version of the header.
148
bin_ver: u32,
149
/// Size in bytes of the binary (to be ignored).
150
bin_size: u32,
151
/// Offset of the start of the application-specific header.
152
header_offset: u32,
153
/// Offset of the start of the data payload.
154
data_offset: u32,
155
/// Size in bytes of the data payload.
156
data_size: u32,
157
}
158
159
// SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
160
unsafe impl FromBytes for BinHdr {}
161
162
// A firmware blob starting with a `BinHdr`.
163
struct BinFirmware<'a> {
164
hdr: BinHdr,
165
fw: &'a [u8],
166
}
167
168
impl<'a> BinFirmware<'a> {
169
/// Interpret `fw` as a firmware image starting with a [`BinHdr`], and returns the
170
/// corresponding [`BinFirmware`] that can be used to extract its payload.
171
fn new(fw: &'a firmware::Firmware) -> Result<Self> {
172
const BIN_MAGIC: u32 = 0x10de;
173
let fw = fw.data();
174
175
fw.get(0..size_of::<BinHdr>())
176
// Extract header.
177
.and_then(BinHdr::from_bytes_copy)
178
// Validate header.
179
.and_then(|hdr| {
180
if hdr.bin_magic == BIN_MAGIC {
181
Some(hdr)
182
} else {
183
None
184
}
185
})
186
.map(|hdr| Self { hdr, fw })
187
.ok_or(EINVAL)
188
}
189
190
/// Returns the data payload of the firmware, or `None` if the data range is out of bounds of
191
/// the firmware image.
192
fn data(&self) -> Option<&[u8]> {
193
let fw_start = self.hdr.data_offset as usize;
194
let fw_size = self.hdr.data_size as usize;
195
196
self.fw.get(fw_start..fw_start + fw_size)
197
}
198
}
199
200
pub(crate) struct ModInfoBuilder<const N: usize>(firmware::ModInfoBuilder<N>);
201
202
impl<const N: usize> ModInfoBuilder<N> {
203
const fn make_entry_file(self, chipset: &str, fw: &str) -> Self {
204
ModInfoBuilder(
205
self.0
206
.new_entry()
207
.push("nvidia/")
208
.push(chipset)
209
.push("/gsp/")
210
.push(fw)
211
.push("-")
212
.push(FIRMWARE_VERSION)
213
.push(".bin"),
214
)
215
}
216
217
const fn make_entry_chipset(self, chipset: &str) -> Self {
218
self.make_entry_file(chipset, "booter_load")
219
.make_entry_file(chipset, "booter_unload")
220
.make_entry_file(chipset, "bootloader")
221
.make_entry_file(chipset, "gsp")
222
}
223
224
pub(crate) const fn create(
225
module_name: &'static kernel::str::CStr,
226
) -> firmware::ModInfoBuilder<N> {
227
let mut this = Self(firmware::ModInfoBuilder::new(module_name));
228
let mut i = 0;
229
230
while i < gpu::Chipset::ALL.len() {
231
this = this.make_entry_chipset(gpu::Chipset::ALL[i].name());
232
i += 1;
233
}
234
235
this.0
236
}
237
}
238
239