1 // SPDX-License-Identifier: GPL-2.0 2 3 use core::ops::{ 4 Deref, 5 Range, // 6 }; 7 8 use kernel::{ 9 device, 10 dma::CoherentHandle, 11 fmt, 12 io::Io, 13 prelude::*, 14 ptr::{ 15 Alignable, 16 Alignment, // 17 }, 18 sizes::*, // 19 }; 20 21 use crate::{ 22 driver::Bar0, 23 firmware::gsp::GspFirmware, 24 gpu::Chipset, 25 gsp, 26 num::FromSafeCast, 27 vgpu::VgpuState, // 28 }; 29 30 mod hal; 31 mod regs; 32 33 /// Type holding the sysmem flush memory page, a page of memory to be written into the 34 /// `NV_PFB_NISO_FLUSH_SYSMEM_ADDR*` registers and used to maintain memory coherency. 35 /// 36 /// A system memory page is required for `sysmembar`, which is a GPU-initiated hardware 37 /// memory-barrier operation that flushes all pending GPU-side memory writes that were done through 38 /// PCIE to system memory. It is required for falcons to be reset as the reset operation involves a 39 /// reset handshake. When the falcon acknowledges a reset, it writes into system memory. To ensure 40 /// this write is visible to the host and prevent driver timeouts, the falcon must perform a 41 /// sysmembar operation to flush its writes. 42 /// 43 /// Because of this, the sysmem flush memory page must be registered as early as possible during 44 /// driver initialization, and before any falcon is reset. 45 /// 46 pub(crate) struct SysmemFlush<'sys> { 47 /// Chipset we are operating on. 48 chipset: Chipset, 49 device: &'sys device::Device, 50 bar: Bar0<'sys>, 51 /// Keep the page alive as long as we need it. 52 page: CoherentHandle, 53 } 54 55 impl<'sys> SysmemFlush<'sys> { 56 /// Allocate a memory page and register it as the sysmem flush page. 57 pub(crate) fn register( 58 dev: &'sys device::Device<device::Bound>, 59 bar: Bar0<'sys>, 60 chipset: Chipset, 61 ) -> Result<Self> { 62 let page = CoherentHandle::alloc(dev, kernel::page::PAGE_SIZE, GFP_KERNEL)?; 63 64 hal::fb_hal(chipset).write_sysmem_flush_page(bar, page.dma_handle())?; 65 66 Ok(Self { 67 chipset, 68 device: dev, 69 bar, 70 page, 71 }) 72 } 73 } 74 75 impl Drop for SysmemFlush<'_> { 76 fn drop(&mut self) { 77 let hal = hal::fb_hal(self.chipset); 78 79 if hal.read_sysmem_flush_page(self.bar) == self.page.dma_handle() { 80 let _ = hal.write_sysmem_flush_page(self.bar, 0).inspect_err(|e| { 81 dev_warn!( 82 &self.device, 83 "failed to unregister sysmem flush page: {:?}\n", 84 e 85 ) 86 }); 87 } else { 88 // Another page has been registered after us for some reason - warn as this is a bug. 89 dev_warn!( 90 &self.device, 91 "attempt to unregister a sysmem flush page that is not active\n" 92 ); 93 } 94 } 95 } 96 97 pub(crate) struct FbRange(Range<u64>); 98 99 impl FbRange { 100 pub(crate) fn len(&self) -> u64 { 101 self.0.end - self.0.start 102 } 103 } 104 105 impl From<Range<u64>> for FbRange { 106 fn from(range: Range<u64>) -> Self { 107 Self(range) 108 } 109 } 110 111 impl Deref for FbRange { 112 type Target = Range<u64>; 113 114 fn deref(&self) -> &Self::Target { 115 &self.0 116 } 117 } 118 119 impl fmt::Debug for FbRange { 120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 121 // Use alternate format ({:#?}) to include size, compact format ({:?}) for just the range. 122 if f.alternate() { 123 let size = self.len(); 124 125 if size < u64::SZ_1M { 126 let size_kib = size / u64::SZ_1K; 127 f.write_fmt(fmt!( 128 "{:#x}..{:#x} ({} KiB)", 129 self.0.start, 130 self.0.end, 131 size_kib 132 )) 133 } else { 134 let size_mib = size / u64::SZ_1M; 135 f.write_fmt(fmt!( 136 "{:#x}..{:#x} ({} MiB)", 137 self.0.start, 138 self.0.end, 139 size_mib 140 )) 141 } 142 } else { 143 f.write_fmt(fmt!("{:#x}..{:#x}", self.0.start, self.0.end)) 144 } 145 } 146 } 147 148 /// Layout of the GPU framebuffer memory. 149 /// 150 /// Contains ranges of GPU memory reserved for a given purpose during the GSP boot process. 151 #[derive(Debug)] 152 pub(crate) struct FbLayout { 153 /// Range of the framebuffer. Starts at `0`. 154 pub(crate) fb: FbRange, 155 /// VGA workspace, small area of reserved memory at the end of the framebuffer. 156 pub(crate) vga_workspace: FbRange, 157 /// FRTS range. 158 pub(crate) frts: FbRange, 159 /// Memory area containing the GSP bootloader image. 160 pub(crate) boot: FbRange, 161 /// Memory area containing the GSP firmware image. 162 pub(crate) elf: FbRange, 163 /// WPR2 heap. 164 pub(crate) wpr2_heap: FbRange, 165 /// WPR2 region range, starting with an instance of `GspFwWprMeta`. 166 pub(crate) wpr2: FbRange, 167 pub(crate) heap: FbRange, 168 pub(crate) vf_partition_count: u8, 169 /// PMU reserved memory size, in bytes. 170 pub(crate) pmu_reserved_size: u32, 171 } 172 173 impl FbLayout { 174 /// Computes the FB layout for `chipset` required to run the `gsp_fw` GSP firmware. 175 pub(crate) fn new( 176 chipset: Chipset, 177 bar: Bar0<'_>, 178 gsp_fw: &GspFirmware, 179 vgpu_state: VgpuState, 180 ) -> Result<Self> { 181 let hal = hal::fb_hal(chipset); 182 183 let fb = { 184 let fb_size = hal.vidmem_size(bar); 185 186 FbRange(0..fb_size) 187 }; 188 189 let vga_workspace = { 190 let vga_base = { 191 const NV_PRAMIN_SIZE: u64 = u64::SZ_1M; 192 let base = fb.end - NV_PRAMIN_SIZE; 193 194 if hal.supports_display(bar) { 195 match bar 196 .read(regs::NV_PDISP_VGA_WORKSPACE_BASE) 197 .vga_workspace_addr() 198 { 199 Some(addr) => { 200 if addr < base { 201 const VBIOS_WORKSPACE_SIZE: u64 = u64::SZ_128K; 202 203 // Point workspace address to end of framebuffer. 204 fb.end - VBIOS_WORKSPACE_SIZE 205 } else { 206 addr 207 } 208 } 209 None => base, 210 } 211 } else { 212 base 213 } 214 }; 215 216 FbRange(vga_base..fb.end) 217 }; 218 219 let frts = { 220 const FRTS_DOWN_ALIGN: Alignment = Alignment::new::<SZ_128K>(); 221 let frts_size: u64 = hal.frts_size(); 222 let frts_base = vga_workspace.start.align_down(FRTS_DOWN_ALIGN) - frts_size; 223 224 FbRange(frts_base..frts_base + frts_size) 225 }; 226 227 let boot = { 228 const BOOTLOADER_DOWN_ALIGN: Alignment = Alignment::new::<SZ_4K>(); 229 let bootloader_size = u64::from_safe_cast(gsp_fw.bootloader.ucode.size()); 230 let bootloader_base = (frts.start - bootloader_size).align_down(BOOTLOADER_DOWN_ALIGN); 231 232 FbRange(bootloader_base..bootloader_base + bootloader_size) 233 }; 234 235 let elf = { 236 const ELF_DOWN_ALIGN: Alignment = Alignment::new::<SZ_64K>(); 237 let elf_size = u64::from_safe_cast(gsp_fw.size); 238 let elf_addr = (boot.start - elf_size).align_down(ELF_DOWN_ALIGN); 239 240 FbRange(elf_addr..elf_addr + elf_size) 241 }; 242 243 let (vf_partition_count, wpr2_heap_size) = match vgpu_state { 244 VgpuState::Disabled => ( 245 0, 246 gsp::LibosParams::from_chipset(chipset).wpr_heap_size(chipset, fb.end)?, 247 ), 248 VgpuState::Enabled { total_vfs } => ( 249 u8::try_from(total_vfs.get()).map_err(|_| EINVAL)?, 250 gsp::LibosParams::vgpu_wpr_heap_size(), 251 ), 252 }; 253 254 let wpr2_heap = { 255 const WPR2_HEAP_DOWN_ALIGN: Alignment = Alignment::new::<SZ_1M>(); 256 let wpr2_heap_addr = elf 257 .start 258 .checked_sub(wpr2_heap_size) 259 .ok_or(EOVERFLOW)? 260 .align_down(WPR2_HEAP_DOWN_ALIGN); 261 262 FbRange(wpr2_heap_addr..(elf.start).align_down(WPR2_HEAP_DOWN_ALIGN)) 263 }; 264 265 let wpr2 = { 266 const WPR2_DOWN_ALIGN: Alignment = Alignment::new::<SZ_1M>(); 267 let wpr2_addr = (wpr2_heap.start - u64::from_safe_cast(size_of::<gsp::GspFwWprMeta>())) 268 .align_down(WPR2_DOWN_ALIGN); 269 270 FbRange(wpr2_addr..frts.end) 271 }; 272 273 let heap = { 274 let heap_size = u64::from(hal.non_wpr_heap_size()); 275 FbRange(wpr2.start - heap_size..wpr2.start) 276 }; 277 278 Ok(Self { 279 fb, 280 vga_workspace, 281 frts, 282 boot, 283 elf, 284 wpr2_heap, 285 wpr2, 286 heap, 287 vf_partition_count, 288 pmu_reserved_size: hal.pmu_reserved_size(), 289 }) 290 } 291 } 292 293 /// Reads the WPR2 memory region registers and returns the range if set. 294 /// Returns `None` if the WPR2 region is not set. 295 pub(crate) fn wpr2_range(bar: Bar0<'_>) -> Option<Range<u64>> { 296 let wpr2_hi = bar.read(crate::regs::NV_PFB_PRI_MMU_WPR2_ADDR_HI); 297 298 if !wpr2_hi.is_wpr2_set() { 299 return None; 300 } 301 302 let wpr2_lo = bar.read(crate::regs::NV_PFB_PRI_MMU_WPR2_ADDR_LO); 303 304 Some(wpr2_lo.lower_bound()..wpr2_hi.higher_bound()) 305 } 306