From: John Hubbard <[email protected]> The GSP firmware TLV names a separate file in its FILE tag and gives that file's length in its SIZE tag. The GSP firmware wrapper reads the two tags and loads the file itself, so no other loader can share that code.
Move it into the TLV parser as `Tlv::load_file()`, which also fails with ENODATA when SIZE is zero. The r000 ucodes loader will be the second user of this. Signed-off-by: John Hubbard <[email protected]> Signed-off-by: Alexandre Courbot <[email protected]> --- drivers/gpu/nova-core/firmware/gsp.rs | 15 +++---------- drivers/gpu/nova-core/firmware/tlv.rs | 40 ++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/drivers/gpu/nova-core/firmware/gsp.rs b/drivers/gpu/nova-core/firmware/gsp.rs index 341a8b19aa38..a2db7b6ba131 100644 --- a/drivers/gpu/nova-core/firmware/gsp.rs +++ b/drivers/gpu/nova-core/firmware/gsp.rs @@ -6,9 +6,7 @@ Coherent, DmaAddress, // }, - firmware, - prelude::*, - str::CString, + prelude::*, // }; use crate::{ @@ -20,8 +18,7 @@ Tlv, }, }, - gpu::Chipset, - num::FromSafeCast, + gpu::Chipset, // }; /// The GSP firmware image, its signatures, and the GSP bootloader. @@ -48,13 +45,7 @@ pub(crate) fn new( let tlv = Tlv::new(firmware.data())?; dev_dbg!(dev, "loaded gsp firmware v{}\n", tlv.get_string(b"VERS")?); - let size = usize::from_safe_cast(tlv.get_u32(b"SIZE")?); - let mut fw_vvec = VVec::zeroed(size, GFP_KERNEL).map_err(|_| ENOMEM)?; - - let chip_name = chipset.name(); - let file = tlv.get_string(b"FILE")?; - let filename = CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{file}"))?; - firmware::request_into_buf(&filename, dev, fw_vvec.as_mut_slice())?; + let fw_vvec = tlv.load_file(dev, chipset)?; let signatures = Coherent::from_slice(dev, tlv.get_bytes(b"SIGN")?, GFP_KERNEL)?; diff --git a/drivers/gpu/nova-core/firmware/tlv.rs b/drivers/gpu/nova-core/firmware/tlv.rs index 7b879f13a61e..59807ca702ba 100644 --- a/drivers/gpu/nova-core/firmware/tlv.rs +++ b/drivers/gpu/nova-core/firmware/tlv.rs @@ -4,6 +4,7 @@ use kernel::{ device, firmware, + fmt, prelude::*, str::CString, // }; @@ -13,15 +14,18 @@ num::*, // }; +/// Returns the path of `file` in `chipset`'s GSP firmware directory. +fn gsp_firmware_path(chipset: gpu::Chipset, file: fmt::Arguments<'_>) -> Result<CString> { + CString::try_from_fmt(fmt!("nvidia/{}/gsp/{}", chipset.name(), file)) +} + /// Requests the GPU firmware TLV `name` suitable for `chipset`. pub(crate) fn request_tlv( dev: &device::Device, chipset: gpu::Chipset, name: &str, ) -> Result<firmware::Firmware> { - let chip_name = chipset.name(); - - let filename = CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{name}.tlv"))?; + let filename = gsp_firmware_path(chipset, fmt!("{name}.tlv"))?; dev_dbg!(dev, "loading firmware image {:?}\n", &filename); @@ -198,6 +202,36 @@ fn iter(&self) -> TlvIter<'_, 'a> { self.iter().find(|b| b.tag == *tag).ok_or(EINVAL) } + /// Loads the file that the `FILE` tag names from `chipset`'s GSP firmware directory. + /// + /// The `SIZE` tag gives the file's length, and the returned buffer is that long. + /// + /// # Errors + /// + /// - `EINVAL` if `FILE` or `SIZE` is absent, or `FILE` does not hold a valid string. + /// - `ENODATA` if `SIZE` is zero. + /// - `ENOMEM` if the buffer cannot be allocated. + /// + /// Errors from the firmware request are propagated as-is. + pub(crate) fn load_file( + &self, + dev: &device::Device, + chipset: gpu::Chipset, + ) -> Result<VVec<u8>> { + let file = self.get_string(b"FILE")?; + let path = gsp_firmware_path(chipset, fmt!("{file}"))?; + + let size = usize::from_safe_cast(self.get_u32(b"SIZE")?); + if size == 0 { + return Err(ENODATA); + } + + let mut data = VVec::zeroed(size, GFP_KERNEL).map_err(|_| ENOMEM)?; + firmware::request_into_buf(&path, dev, data.as_mut_slice())?; + + Ok(data) + } + /// Return a slice of bytes. /// /// Returns `EINVAL` if the value is empty. -- 2.56.1
