xref: /linux/drivers/gpu/nova-core/firmware/gsp.rs (revision e727d1c382a0cb2e10fd7aabef9217d1a37c80c7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use kernel::{
4     device,
5     dma::{
6         Coherent,
7         DmaAddress, //
8     },
9     firmware,
10     prelude::*,
11     str::CString,
12 };
13 
14 use crate::{
15     firmware::{
16         radix3::Radix3,
17         riscv::RiscvFirmware, //
18         tlv::{
19             request_tlv, //
20             Tlv,
21         },
22     },
23     gpu::Chipset,
24     num::FromSafeCast,
25 };
26 
27 /// The GSP firmware image, its signatures, and the GSP bootloader.
28 #[pin_data]
29 pub(crate) struct GspFirmware<'a> {
30     /// The firmware image and the radix3 table that maps it.
31     #[pin]
32     radix3: Radix3<'a>,
33     /// Device-mapped GSP signatures matching the GPU's [`Chipset`].
34     pub(crate) signatures: Coherent<'a, [u8]>,
35     /// GSP bootloader, verifies the GSP firmware before loading and running it.
36     pub(crate) bootloader: RiscvFirmware<'a>,
37 }
38 
39 impl<'a> GspFirmware<'a> {
40     /// Loads the GSP firmware binaries, map them into `dev`'s address-space, and creates the page
41     /// tables expected by the GSP bootloader to load it.
42     pub(crate) fn new(
43         dev: &'a device::Device<device::Bound>,
44         chipset: Chipset,
45     ) -> impl PinInit<Self, Error> + 'a {
46         pin_init::pin_init_scope(move || {
47             let firmware = request_tlv(dev, chipset, "gsp")?;
48             let tlv = Tlv::new(firmware.data())?;
49             dev_dbg!(dev, "loaded gsp firmware v{}\n", tlv.get_string(b"VERS")?);
50 
51             let size = usize::from_safe_cast(tlv.get_u32(b"SIZE")?);
52             let mut fw_vvec = VVec::zeroed(size, GFP_KERNEL).map_err(|_| ENOMEM)?;
53 
54             let chip_name = chipset.name();
55             let file = tlv.get_string(b"FILE")?;
56             let filename = CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{file}"))?;
57             firmware::request_into_buf(&filename, dev, fw_vvec.as_mut_slice())?;
58 
59             let signatures = Coherent::from_slice(dev, tlv.get_bytes(b"SIGN")?, GFP_KERNEL)?;
60 
61             Ok(try_pin_init!(Self {
62                 radix3 <- Radix3::new(dev, fw_vvec),
63                 signatures,
64                 bootloader: {
65                     let bl = request_tlv(dev, chipset, "gsp_bootloader")?;
66 
67                     RiscvFirmware::new(dev, &bl)?
68                 },
69             }))
70         })
71     }
72 
73     /// Returns the size of the firmware image, in bytes.
74     pub(crate) fn size(&self) -> usize {
75         self.radix3.size()
76     }
77 
78     /// Returns the DMA address of the radix3 table that maps the firmware image.
79     pub(crate) fn radix3_dma_address(&self) -> DmaAddress {
80         self.radix3.dma_address()
81     }
82 }
83