xref: /linux/drivers/gpu/nova-core/firmware.rs (revision 50b3e0c7c82f32e6ac3ead30f0e0ba96d36a4ff6)
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::ops::Deref;
8 
9 use kernel::{
10     device,
11     firmware,
12     prelude::*,
13     str::CString,
14     transmute::FromBytes, //
15 };
16 
17 use crate::{
18     falcon::{
19         FalconDmaLoadTarget,
20         FalconFirmware, //
21     },
22     gpu,
23     num::{
24         FromSafeCast,
25         IntoSafeCast, //
26     },
27 };
28 
29 pub(crate) mod booter;
30 pub(crate) mod fwsec;
31 pub(crate) mod gsp;
32 pub(crate) mod riscv;
33 
34 pub(crate) const FIRMWARE_VERSION: &str = "570.144";
35 
36 /// Requests the GPU firmware `name` suitable for `chipset`, with version `ver`.
37 fn request_firmware(
38     dev: &device::Device,
39     chipset: gpu::Chipset,
40     name: &str,
41     ver: &str,
42 ) -> Result<firmware::Firmware> {
43     let chip_name = chipset.name();
44 
45     CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}-{ver}.bin"))
46         .and_then(|path| firmware::Firmware::request(&path, dev))
47 }
48 
49 /// Structure used to describe some firmwares, notably FWSEC-FRTS.
50 #[repr(C)]
51 #[derive(Debug, Clone)]
52 pub(crate) struct FalconUCodeDescV2 {
53     /// Header defined by 'NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*' in OpenRM.
54     hdr: u32,
55     /// Stored size of the ucode after the header, compressed or uncompressed
56     stored_size: u32,
57     /// Uncompressed size of the ucode.  If store_size == uncompressed_size, then the ucode
58     /// is not compressed.
59     pub(crate) uncompressed_size: u32,
60     /// Code entry point
61     pub(crate) virtual_entry: u32,
62     /// Offset after the code segment at which the Application Interface Table headers are located.
63     pub(crate) interface_offset: u32,
64     /// Base address at which to load the code segment into 'IMEM'.
65     pub(crate) imem_phys_base: u32,
66     /// Size in bytes of the code to copy into 'IMEM'.
67     pub(crate) imem_load_size: u32,
68     /// Virtual 'IMEM' address (i.e. 'tag') at which the code should start.
69     pub(crate) imem_virt_base: u32,
70     /// Virtual address of secure IMEM segment.
71     pub(crate) imem_sec_base: u32,
72     /// Size of secure IMEM segment.
73     pub(crate) imem_sec_size: u32,
74     /// Offset into stored (uncompressed) image at which DMEM begins.
75     pub(crate) dmem_offset: u32,
76     /// Base address at which to load the data segment into 'DMEM'.
77     pub(crate) dmem_phys_base: u32,
78     /// Size in bytes of the data to copy into 'DMEM'.
79     pub(crate) dmem_load_size: u32,
80     /// "Alternate" Size of data to load into IMEM.
81     pub(crate) alt_imem_load_size: u32,
82     /// "Alternate" Size of data to load into DMEM.
83     pub(crate) alt_dmem_load_size: u32,
84 }
85 
86 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
87 unsafe impl FromBytes for FalconUCodeDescV2 {}
88 
89 /// Structure used to describe some firmwares, notably FWSEC-FRTS.
90 #[repr(C)]
91 #[derive(Debug, Clone)]
92 pub(crate) struct FalconUCodeDescV3 {
93     /// Header defined by `NV_BIT_FALCON_UCODE_DESC_HEADER_VDESC*` in OpenRM.
94     hdr: u32,
95     /// Stored size of the ucode after the header.
96     stored_size: u32,
97     /// Offset in `DMEM` at which the signature is expected to be found.
98     pub(crate) pkc_data_offset: u32,
99     /// Offset after the code segment at which the app headers are located.
100     pub(crate) interface_offset: u32,
101     /// Base address at which to load the code segment into `IMEM`.
102     pub(crate) imem_phys_base: u32,
103     /// Size in bytes of the code to copy into `IMEM`.
104     pub(crate) imem_load_size: u32,
105     /// Virtual `IMEM` address (i.e. `tag`) at which the code should start.
106     pub(crate) imem_virt_base: u32,
107     /// Base address at which to load the data segment into `DMEM`.
108     pub(crate) dmem_phys_base: u32,
109     /// Size in bytes of the data to copy into `DMEM`.
110     pub(crate) dmem_load_size: u32,
111     /// Mask of the falcon engines on which this firmware can run.
112     pub(crate) engine_id_mask: u16,
113     /// ID of the ucode used to infer a fuse register to validate the signature.
114     pub(crate) ucode_id: u8,
115     /// Number of signatures in this firmware.
116     pub(crate) signature_count: u8,
117     /// Versions of the signatures, used to infer a valid signature to use.
118     pub(crate) signature_versions: u16,
119     _reserved: u16,
120 }
121 
122 // SAFETY: all bit patterns are valid for this type, and it doesn't use
123 // interior mutability.
124 unsafe impl FromBytes for FalconUCodeDescV3 {}
125 
126 /// Enum wrapping the different versions of Falcon microcode descriptors.
127 ///
128 /// This allows handling both V2 and V3 descriptor formats through a
129 /// unified type, providing version-agnostic access to firmware metadata
130 /// via the [`FalconUCodeDescriptor`] trait.
131 #[derive(Debug, Clone)]
132 pub(crate) enum FalconUCodeDesc {
133     V2(FalconUCodeDescV2),
134     V3(FalconUCodeDescV3),
135 }
136 
137 impl Deref for FalconUCodeDesc {
138     type Target = dyn FalconUCodeDescriptor;
139 
140     fn deref(&self) -> &Self::Target {
141         match self {
142             FalconUCodeDesc::V2(v2) => v2,
143             FalconUCodeDesc::V3(v3) => v3,
144         }
145     }
146 }
147 
148 /// Trait providing a common interface for accessing Falcon microcode descriptor fields.
149 ///
150 /// This trait abstracts over the different descriptor versions ([`FalconUCodeDescV2`] and
151 /// [`FalconUCodeDescV3`]), allowing code to work with firmware metadata without needing to
152 /// know the specific descriptor version. Fields not present return zero.
153 pub(crate) trait FalconUCodeDescriptor {
154     fn hdr(&self) -> u32;
155     fn imem_load_size(&self) -> u32;
156     fn interface_offset(&self) -> u32;
157     fn dmem_load_size(&self) -> u32;
158     fn pkc_data_offset(&self) -> u32;
159     fn engine_id_mask(&self) -> u16;
160     fn ucode_id(&self) -> u8;
161     fn signature_count(&self) -> u8;
162     fn signature_versions(&self) -> u16;
163 
164     /// Returns the size in bytes of the header.
165     fn size(&self) -> usize {
166         let hdr = self.hdr();
167 
168         const HDR_SIZE_SHIFT: u32 = 16;
169         const HDR_SIZE_MASK: u32 = 0xffff0000;
170         ((hdr & HDR_SIZE_MASK) >> HDR_SIZE_SHIFT).into_safe_cast()
171     }
172 
173     fn imem_sec_load_params(&self) -> FalconDmaLoadTarget;
174     fn imem_ns_load_params(&self) -> Option<FalconDmaLoadTarget>;
175     fn dmem_load_params(&self) -> FalconDmaLoadTarget;
176 }
177 
178 impl FalconUCodeDescriptor for FalconUCodeDescV2 {
179     fn hdr(&self) -> u32 {
180         self.hdr
181     }
182     fn imem_load_size(&self) -> u32 {
183         self.imem_load_size
184     }
185     fn interface_offset(&self) -> u32 {
186         self.interface_offset
187     }
188     fn dmem_load_size(&self) -> u32 {
189         self.dmem_load_size
190     }
191     fn pkc_data_offset(&self) -> u32 {
192         0
193     }
194     fn engine_id_mask(&self) -> u16 {
195         0
196     }
197     fn ucode_id(&self) -> u8 {
198         0
199     }
200     fn signature_count(&self) -> u8 {
201         0
202     }
203     fn signature_versions(&self) -> u16 {
204         0
205     }
206 
207     fn imem_sec_load_params(&self) -> FalconDmaLoadTarget {
208         FalconDmaLoadTarget {
209             src_start: 0,
210             dst_start: self.imem_sec_base,
211             len: self.imem_sec_size,
212         }
213     }
214 
215     fn imem_ns_load_params(&self) -> Option<FalconDmaLoadTarget> {
216         Some(FalconDmaLoadTarget {
217             src_start: 0,
218             dst_start: self.imem_phys_base,
219             len: self.imem_load_size.checked_sub(self.imem_sec_size)?,
220         })
221     }
222 
223     fn dmem_load_params(&self) -> FalconDmaLoadTarget {
224         FalconDmaLoadTarget {
225             src_start: self.dmem_offset,
226             dst_start: self.dmem_phys_base,
227             len: self.dmem_load_size,
228         }
229     }
230 }
231 
232 impl FalconUCodeDescriptor for FalconUCodeDescV3 {
233     fn hdr(&self) -> u32 {
234         self.hdr
235     }
236     fn imem_load_size(&self) -> u32 {
237         self.imem_load_size
238     }
239     fn interface_offset(&self) -> u32 {
240         self.interface_offset
241     }
242     fn dmem_load_size(&self) -> u32 {
243         self.dmem_load_size
244     }
245     fn pkc_data_offset(&self) -> u32 {
246         self.pkc_data_offset
247     }
248     fn engine_id_mask(&self) -> u16 {
249         self.engine_id_mask
250     }
251     fn ucode_id(&self) -> u8 {
252         self.ucode_id
253     }
254     fn signature_count(&self) -> u8 {
255         self.signature_count
256     }
257     fn signature_versions(&self) -> u16 {
258         self.signature_versions
259     }
260 
261     fn imem_sec_load_params(&self) -> FalconDmaLoadTarget {
262         FalconDmaLoadTarget {
263             src_start: 0,
264             dst_start: self.imem_phys_base,
265             len: self.imem_load_size,
266         }
267     }
268 
269     fn imem_ns_load_params(&self) -> Option<FalconDmaLoadTarget> {
270         // Not used on V3 platforms
271         None
272     }
273 
274     fn dmem_load_params(&self) -> FalconDmaLoadTarget {
275         FalconDmaLoadTarget {
276             src_start: self.imem_load_size,
277             dst_start: self.dmem_phys_base,
278             len: self.dmem_load_size,
279         }
280     }
281 }
282 
283 /// Trait implemented by types defining the signed state of a firmware.
284 trait SignedState {}
285 
286 /// Type indicating that the firmware must be signed before it can be used.
287 struct Unsigned;
288 impl SignedState for Unsigned {}
289 
290 /// Type indicating that the firmware is signed and ready to be loaded.
291 struct Signed;
292 impl SignedState for Signed {}
293 
294 /// Microcode to be loaded into a specific falcon.
295 ///
296 /// This is module-local and meant for sub-modules to use internally.
297 ///
298 /// After construction, a firmware is [`Unsigned`], and must generally be patched with a signature
299 /// before it can be loaded (with an exception for development hardware). The
300 /// [`Self::patch_signature`] and [`Self::no_patch_signature`] methods are used to transition the
301 /// firmware to its [`Signed`] state.
302 // TODO: Consider replacing this with a coherent memory object once `CoherentAllocation` supports
303 // temporary CPU-exclusive access to the object without unsafe methods.
304 struct FirmwareObject<F: FalconFirmware, S: SignedState>(KVVec<u8>, PhantomData<(F, S)>);
305 
306 /// Trait for signatures to be patched directly into a given firmware.
307 ///
308 /// This is module-local and meant for sub-modules to use internally.
309 trait FirmwareSignature<F: FalconFirmware>: AsRef<[u8]> {}
310 
311 impl<F: FalconFirmware> FirmwareObject<F, Unsigned> {
312     /// Patches the firmware at offset `signature_start` with `signature`.
313     fn patch_signature<S: FirmwareSignature<F>>(
314         mut self,
315         signature: &S,
316         signature_start: usize,
317     ) -> Result<FirmwareObject<F, Signed>> {
318         let signature_bytes = signature.as_ref();
319         let signature_end = signature_start
320             .checked_add(signature_bytes.len())
321             .ok_or(EOVERFLOW)?;
322         let dst = self
323             .0
324             .get_mut(signature_start..signature_end)
325             .ok_or(EINVAL)?;
326 
327         // PANIC: `dst` and `signature_bytes` have the same length.
328         dst.copy_from_slice(signature_bytes);
329 
330         Ok(FirmwareObject(self.0, PhantomData))
331     }
332 
333     /// Mark the firmware as signed without patching it.
334     ///
335     /// This method is used to explicitly confirm that we do not need to sign the firmware, while
336     /// allowing us to continue as if it was. This is typically only needed for development
337     /// hardware.
338     fn no_patch_signature(self) -> FirmwareObject<F, Signed> {
339         FirmwareObject(self.0, PhantomData)
340     }
341 }
342 
343 /// Header common to most firmware files.
344 #[repr(C)]
345 #[derive(Debug, Clone)]
346 struct BinHdr {
347     /// Magic number, must be `0x10de`.
348     bin_magic: u32,
349     /// Version of the header.
350     bin_ver: u32,
351     /// Size in bytes of the binary (to be ignored).
352     bin_size: u32,
353     /// Offset of the start of the application-specific header.
354     header_offset: u32,
355     /// Offset of the start of the data payload.
356     data_offset: u32,
357     /// Size in bytes of the data payload.
358     data_size: u32,
359 }
360 
361 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability.
362 unsafe impl FromBytes for BinHdr {}
363 
364 // A firmware blob starting with a `BinHdr`.
365 struct BinFirmware<'a> {
366     hdr: BinHdr,
367     fw: &'a [u8],
368 }
369 
370 impl<'a> BinFirmware<'a> {
371     /// Interpret `fw` as a firmware image starting with a [`BinHdr`], and returns the
372     /// corresponding [`BinFirmware`] that can be used to extract its payload.
373     fn new(fw: &'a firmware::Firmware) -> Result<Self> {
374         const BIN_MAGIC: u32 = 0x10de;
375         let fw = fw.data();
376 
377         fw.get(0..size_of::<BinHdr>())
378             // Extract header.
379             .and_then(BinHdr::from_bytes_copy)
380             // Validate header.
381             .and_then(|hdr| {
382                 if hdr.bin_magic == BIN_MAGIC {
383                     Some(hdr)
384                 } else {
385                     None
386                 }
387             })
388             .map(|hdr| Self { hdr, fw })
389             .ok_or(EINVAL)
390     }
391 
392     /// Returns the data payload of the firmware, or `None` if the data range is out of bounds of
393     /// the firmware image.
394     fn data(&self) -> Option<&[u8]> {
395         let fw_start = usize::from_safe_cast(self.hdr.data_offset);
396         let fw_size = usize::from_safe_cast(self.hdr.data_size);
397         let fw_end = fw_start.checked_add(fw_size)?;
398 
399         self.fw.get(fw_start..fw_end)
400     }
401 }
402 
403 pub(crate) struct ModInfoBuilder<const N: usize>(firmware::ModInfoBuilder<N>);
404 
405 impl<const N: usize> ModInfoBuilder<N> {
406     const fn make_entry_file(self, chipset: &str, fw: &str) -> Self {
407         ModInfoBuilder(
408             self.0
409                 .new_entry()
410                 .push("nvidia/")
411                 .push(chipset)
412                 .push("/gsp/")
413                 .push(fw)
414                 .push("-")
415                 .push(FIRMWARE_VERSION)
416                 .push(".bin"),
417         )
418     }
419 
420     const fn make_entry_chipset(self, chipset: gpu::Chipset) -> Self {
421         let name = chipset.name();
422 
423         let this = self
424             .make_entry_file(name, "booter_load")
425             .make_entry_file(name, "booter_unload")
426             .make_entry_file(name, "bootloader")
427             .make_entry_file(name, "gsp");
428 
429         if chipset.needs_fwsec_bootloader() {
430             this.make_entry_file(name, "gen_bootloader")
431         } else {
432             this
433         }
434     }
435 
436     pub(crate) const fn create(
437         module_name: &'static core::ffi::CStr,
438     ) -> firmware::ModInfoBuilder<N> {
439         let mut this = Self(firmware::ModInfoBuilder::new(module_name));
440         let mut i = 0;
441 
442         while i < gpu::Chipset::ALL.len() {
443             this = this.make_entry_chipset(gpu::Chipset::ALL[i]);
444             i += 1;
445         }
446 
447         this.0
448     }
449 }
450