1 // SPDX-License-Identifier: GPL-2.0 2 3 use core::mem::size_of_val; 4 5 use kernel::{ 6 device, 7 dma::{ 8 DataDirection, 9 DmaAddress, // 10 }, 11 kvec, 12 prelude::*, 13 scatterlist::{ 14 Owned, 15 SGTable, // 16 }, 17 }; 18 19 use crate::{ 20 dma::DmaObject, 21 firmware::riscv::RiscvFirmware, 22 gpu::{ 23 Architecture, 24 Chipset, // 25 }, 26 gsp::GSP_PAGE_SIZE, 27 }; 28 29 /// Ad-hoc and temporary module to extract sections from ELF images. 30 /// 31 /// Some firmware images are currently packaged as ELF files, where sections names are used as keys 32 /// to specific and related bits of data. Future firmware versions are scheduled to move away from 33 /// that scheme before nova-core becomes stable, which means this module will eventually be 34 /// removed. 35 mod elf { 36 use core::mem::size_of; 37 38 use kernel::bindings; 39 use kernel::str::CStr; 40 use kernel::transmute::FromBytes; 41 42 /// Newtype to provide a [`FromBytes`] implementation. 43 #[repr(transparent)] 44 struct Elf64Hdr(bindings::elf64_hdr); 45 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability. 46 unsafe impl FromBytes for Elf64Hdr {} 47 48 #[repr(transparent)] 49 struct Elf64SHdr(bindings::elf64_shdr); 50 // SAFETY: all bit patterns are valid for this type, and it doesn't use interior mutability. 51 unsafe impl FromBytes for Elf64SHdr {} 52 53 /// Tries to extract section with name `name` from the ELF64 image `elf`, and returns it. 54 pub(super) fn elf64_section<'a, 'b>(elf: &'a [u8], name: &'b str) -> Option<&'a [u8]> { 55 let hdr = &elf 56 .get(0..size_of::<bindings::elf64_hdr>()) 57 .and_then(Elf64Hdr::from_bytes)? 58 .0; 59 60 // Get all the section headers. 61 let mut shdr = { 62 let shdr_num = usize::from(hdr.e_shnum); 63 let shdr_start = usize::try_from(hdr.e_shoff).ok()?; 64 let shdr_end = shdr_num 65 .checked_mul(size_of::<Elf64SHdr>()) 66 .and_then(|v| v.checked_add(shdr_start))?; 67 68 elf.get(shdr_start..shdr_end) 69 .map(|slice| slice.chunks_exact(size_of::<Elf64SHdr>()))? 70 }; 71 72 // Get the strings table. 73 let strhdr = shdr 74 .clone() 75 .nth(usize::from(hdr.e_shstrndx)) 76 .and_then(Elf64SHdr::from_bytes)?; 77 78 // Find the section which name matches `name` and return it. 79 shdr.find(|&sh| { 80 let Some(hdr) = Elf64SHdr::from_bytes(sh) else { 81 return false; 82 }; 83 84 let Some(name_idx) = strhdr 85 .0 86 .sh_offset 87 .checked_add(u64::from(hdr.0.sh_name)) 88 .and_then(|idx| usize::try_from(idx).ok()) 89 else { 90 return false; 91 }; 92 93 // Get the start of the name. 94 elf.get(name_idx..) 95 // Stop at the first `0`. 96 .and_then(|nstr| nstr.get(0..=nstr.iter().position(|b| *b == 0)?)) 97 // Convert into CStr. This should never fail because of the line above. 98 .and_then(|nstr| CStr::from_bytes_with_nul(nstr).ok()) 99 // Convert into str. 100 .and_then(|c_str| c_str.to_str().ok()) 101 // Check that the name matches. 102 .map(|str| str == name) 103 .unwrap_or(false) 104 }) 105 // Return the slice containing the section. 106 .and_then(|sh| { 107 let hdr = Elf64SHdr::from_bytes(sh)?; 108 let start = usize::try_from(hdr.0.sh_offset).ok()?; 109 let end = usize::try_from(hdr.0.sh_size) 110 .ok() 111 .and_then(|sh_size| start.checked_add(sh_size))?; 112 113 elf.get(start..end) 114 }) 115 } 116 } 117 118 /// GSP firmware with 3-level radix page tables for the GSP bootloader. 119 /// 120 /// The bootloader expects firmware to be mapped starting at address 0 in GSP's virtual address 121 /// space: 122 /// 123 /// ```text 124 /// Level 0: 1 page, 1 entry -> points to first level 1 page 125 /// Level 1: Multiple pages/entries -> each entry points to a level 2 page 126 /// Level 2: Multiple pages/entries -> each entry points to a firmware page 127 /// ``` 128 /// 129 /// Each page is 4KB, each entry is 8 bytes (64-bit DMA address). 130 /// Also known as "Radix3" firmware. 131 #[pin_data] 132 pub(crate) struct GspFirmware { 133 /// The GSP firmware inside a [`VVec`], device-mapped via a SG table. 134 #[pin] 135 fw: SGTable<Owned<VVec<u8>>>, 136 /// Level 2 page table whose entries contain DMA addresses of firmware pages. 137 #[pin] 138 level2: SGTable<Owned<VVec<u8>>>, 139 /// Level 1 page table whose entries contain DMA addresses of level 2 pages. 140 #[pin] 141 level1: SGTable<Owned<VVec<u8>>>, 142 /// Level 0 page table (single 4KB page) with one entry: DMA address of first level 1 page. 143 level0: DmaObject, 144 /// Size in bytes of the firmware contained in [`Self::fw`]. 145 size: usize, 146 /// Device-mapped GSP signatures matching the GPU's [`Chipset`]. 147 signatures: DmaObject, 148 /// GSP bootloader, verifies the GSP firmware before loading and running it. 149 bootloader: RiscvFirmware, 150 } 151 152 impl GspFirmware { 153 /// Loads the GSP firmware binaries, map them into `dev`'s address-space, and creates the page 154 /// tables expected by the GSP bootloader to load it. 155 pub(crate) fn new<'a, 'b>( 156 dev: &'a device::Device<device::Bound>, 157 chipset: Chipset, 158 ver: &'b str, 159 ) -> Result<impl PinInit<Self, Error> + 'a> { 160 let fw = super::request_firmware(dev, chipset, "gsp", ver)?; 161 162 let fw_section = elf::elf64_section(fw.data(), ".fwimage").ok_or(EINVAL)?; 163 164 let sigs_section = match chipset.arch() { 165 Architecture::Ampere => ".fwsignature_ga10x", 166 Architecture::Ada => ".fwsignature_ad10x", 167 _ => return Err(ENOTSUPP), 168 }; 169 let signatures = elf::elf64_section(fw.data(), sigs_section) 170 .ok_or(EINVAL) 171 .and_then(|data| DmaObject::from_data(dev, data))?; 172 173 let size = fw_section.len(); 174 175 // Move the firmware into a vmalloc'd vector and map it into the device address 176 // space. 177 let fw_vvec = VVec::with_capacity(fw_section.len(), GFP_KERNEL) 178 .and_then(|mut v| { 179 v.extend_from_slice(fw_section, GFP_KERNEL)?; 180 Ok(v) 181 }) 182 .map_err(|_| ENOMEM)?; 183 184 let bl = super::request_firmware(dev, chipset, "bootloader", ver)?; 185 let bootloader = RiscvFirmware::new(dev, &bl)?; 186 187 Ok(try_pin_init!(Self { 188 fw <- SGTable::new(dev, fw_vvec, DataDirection::ToDevice, GFP_KERNEL), 189 level2 <- { 190 // Allocate the level 2 page table, map the firmware onto it, and map it into the 191 // device address space. 192 VVec::<u8>::with_capacity( 193 fw.iter().count() * core::mem::size_of::<u64>(), 194 GFP_KERNEL, 195 ) 196 .map_err(|_| ENOMEM) 197 .and_then(|level2| map_into_lvl(&fw, level2)) 198 .map(|level2| SGTable::new(dev, level2, DataDirection::ToDevice, GFP_KERNEL))? 199 }, 200 level1 <- { 201 // Allocate the level 1 page table, map the level 2 page table onto it, and map it 202 // into the device address space. 203 VVec::<u8>::with_capacity( 204 level2.iter().count() * core::mem::size_of::<u64>(), 205 GFP_KERNEL, 206 ) 207 .map_err(|_| ENOMEM) 208 .and_then(|level1| map_into_lvl(&level2, level1)) 209 .map(|level1| SGTable::new(dev, level1, DataDirection::ToDevice, GFP_KERNEL))? 210 }, 211 level0: { 212 // Allocate the level 0 page table as a device-visible DMA object, and map the 213 // level 1 page table onto it. 214 215 // Level 0 page table data. 216 let mut level0_data = kvec![0u8; GSP_PAGE_SIZE]?; 217 218 // Fill level 1 page entry. 219 let level1_entry = level1.iter().next().ok_or(EINVAL)?; 220 let level1_entry_addr = level1_entry.dma_address(); 221 let dst = &mut level0_data[..size_of_val(&level1_entry_addr)]; 222 dst.copy_from_slice(&level1_entry_addr.to_le_bytes()); 223 224 // Turn the level0 page table into a [`DmaObject`]. 225 DmaObject::from_data(dev, &level0_data)? 226 }, 227 size, 228 signatures, 229 bootloader, 230 })) 231 } 232 233 #[expect(unused)] 234 /// Returns the DMA handle of the radix3 level 0 page table. 235 pub(crate) fn radix3_dma_handle(&self) -> DmaAddress { 236 self.level0.dma_handle() 237 } 238 } 239 240 /// Build a page table from a scatter-gather list. 241 /// 242 /// Takes each DMA-mapped region from `sg_table` and writes page table entries 243 /// for all 4KB pages within that region. For example, a 16KB SG entry becomes 244 /// 4 consecutive page table entries. 245 fn map_into_lvl(sg_table: &SGTable<Owned<VVec<u8>>>, mut dst: VVec<u8>) -> Result<VVec<u8>> { 246 for sg_entry in sg_table.iter() { 247 // Number of pages we need to map. 248 let num_pages = (sg_entry.dma_len() as usize).div_ceil(GSP_PAGE_SIZE); 249 250 for i in 0..num_pages { 251 let entry = sg_entry.dma_address() + (i as u64 * GSP_PAGE_SIZE as u64); 252 dst.extend_from_slice(&entry.to_le_bytes(), GFP_KERNEL)?; 253 } 254 } 255 256 Ok(dst) 257 } 258