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