1 // SPDX-License-Identifier: GPL-2.0 2 3 use core::ops::Range; 4 5 use kernel::{ 6 device, 7 dma::Device, 8 fmt, 9 io::Io, 10 num::Bounded, 11 pci, 12 prelude::*, 13 sizes::SizeConstants, // 14 }; 15 16 use crate::{ 17 bounded_enum, 18 driver::Bar0, 19 falcon::{ 20 gsp::Gsp as GspFalcon, 21 sec2::Sec2 as Sec2Falcon, 22 Falcon, // 23 }, 24 fb::SysmemFlush, 25 gsp::{ 26 self, 27 commands::GetGspStaticInfoReply, 28 Gsp, 29 GspBootContext, // 30 }, 31 regs, 32 }; 33 34 mod hal; 35 36 macro_rules! define_chipset { 37 ({ $($variant:ident = $value:expr),* $(,)* }) => 38 { 39 /// Enum representation of the GPU chipset. 40 #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)] 41 pub(crate) enum Chipset { 42 $($variant = $value),*, 43 } 44 45 impl Chipset { 46 pub(crate) const ALL: &'static [Chipset] = &[ 47 $( Chipset::$variant, )* 48 ]; 49 50 ::kernel::macros::paste!( 51 /// Returns the name of this chipset, in lowercase. 52 /// 53 /// # Examples 54 /// 55 /// ``` 56 /// let chipset = Chipset::GA102; 57 /// assert_eq!(chipset.name(), "ga102"); 58 /// ``` 59 pub(crate) const fn name(&self) -> &'static str { 60 match *self { 61 $( 62 Chipset::$variant => stringify!([<$variant:lower>]), 63 )* 64 } 65 } 66 ); 67 } 68 69 // TODO[FPRI]: replace with something like derive(FromPrimitive) 70 impl TryFrom<u32> for Chipset { 71 type Error = kernel::error::Error; 72 73 fn try_from(value: u32) -> Result<Self, Self::Error> { 74 match value { 75 $( $value => Ok(Chipset::$variant), )* 76 _ => Err(ENODEV), 77 } 78 } 79 } 80 } 81 } 82 83 define_chipset!({ 84 // Turing 85 TU102 = 0x162, 86 TU104 = 0x164, 87 TU106 = 0x166, 88 TU117 = 0x167, 89 TU116 = 0x168, 90 // Ampere 91 GA100 = 0x170, 92 GA102 = 0x172, 93 GA103 = 0x173, 94 GA104 = 0x174, 95 GA106 = 0x176, 96 GA107 = 0x177, 97 // Hopper 98 GH100 = 0x180, 99 // Ada 100 AD102 = 0x192, 101 AD103 = 0x193, 102 AD104 = 0x194, 103 AD106 = 0x196, 104 AD107 = 0x197, 105 // Blackwell GB10x 106 GB100 = 0x1a0, 107 GB102 = 0x1a2, 108 // Blackwell GB20x 109 GB202 = 0x1b2, 110 GB203 = 0x1b3, 111 GB205 = 0x1b5, 112 GB206 = 0x1b6, 113 GB207 = 0x1b7, 114 }); 115 116 impl Chipset { 117 pub(crate) const fn arch(self) -> Architecture { 118 match self { 119 Self::TU102 | Self::TU104 | Self::TU106 | Self::TU117 | Self::TU116 => { 120 Architecture::Turing 121 } 122 Self::GA100 | Self::GA102 | Self::GA103 | Self::GA104 | Self::GA106 | Self::GA107 => { 123 Architecture::Ampere 124 } 125 Self::GH100 => Architecture::Hopper, 126 Self::AD102 | Self::AD103 | Self::AD104 | Self::AD106 | Self::AD107 => { 127 Architecture::Ada 128 } 129 Self::GB100 | Self::GB102 => Architecture::BlackwellGB10x, 130 Self::GB202 | Self::GB203 | Self::GB205 | Self::GB206 | Self::GB207 => { 131 Architecture::BlackwellGB20x 132 } 133 } 134 } 135 136 /// Returns `true` if this chipset requires the PIO-loaded bootloader in order to boot FWSEC. 137 /// 138 /// This includes all chipsets < GA102. 139 pub(crate) const fn needs_fwsec_bootloader(self) -> bool { 140 matches!(self.arch(), Architecture::Turing) || matches!(self, Self::GA100) 141 } 142 143 /// Returns `true` if this chipset boots via FSP (Hopper and later), which requires the FMC 144 /// firmware image. 145 pub(crate) const fn uses_fsp(self) -> bool { 146 matches!( 147 self.arch(), 148 Architecture::Hopper | Architecture::BlackwellGB10x | Architecture::BlackwellGB20x 149 ) 150 } 151 152 /// Returns the address range of the PCI config mirror space. 153 pub(crate) fn pci_config_mirror_range(self) -> Range<u32> { 154 hal::gpu_hal(self).pci_config_mirror_range() 155 } 156 } 157 158 // TODO 159 // 160 // The resulting strings are used to generate firmware paths, hence the 161 // generated strings have to be stable. 162 // 163 // Hence, replace with something like strum_macros derive(Display). 164 // 165 // For now, redirect to fmt::Debug for convenience. 166 impl fmt::Display for Chipset { 167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 168 write!(f, "{self:?}") 169 } 170 } 171 172 bounded_enum! { 173 /// Enum representation of the GPU generation. 174 #[derive(fmt::Debug, Copy, Clone)] 175 pub(crate) enum Architecture with TryFrom<Bounded<u32, 6>> { 176 Turing = 0x16, 177 Ampere = 0x17, 178 Hopper = 0x18, 179 Ada = 0x19, 180 BlackwellGB10x = 0x1a, 181 BlackwellGB20x = 0x1b, 182 } 183 } 184 185 #[derive(Clone, Copy)] 186 pub(crate) struct Revision { 187 major: Bounded<u8, 4>, 188 minor: Bounded<u8, 4>, 189 } 190 191 impl From<regs::NV_PMC_BOOT_42> for Revision { 192 fn from(boot0: regs::NV_PMC_BOOT_42) -> Self { 193 Self { 194 major: boot0.major_revision().cast(), 195 minor: boot0.minor_revision().cast(), 196 } 197 } 198 } 199 200 impl fmt::Display for Revision { 201 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 202 write!(f, "{:x}.{:x}", self.major, self.minor) 203 } 204 } 205 206 /// Structure holding a basic description of the GPU: `Chipset` and `Revision`. 207 #[derive(Clone, Copy)] 208 pub(crate) struct Spec { 209 chipset: Chipset, 210 revision: Revision, 211 } 212 213 impl Spec { 214 fn new(dev: &device::Device, bar: Bar0<'_>) -> Result<Spec> { 215 // Some brief notes about boot0 and boot42, in chronological order: 216 // 217 // NV04 through NV50: 218 // 219 // Not supported by Nova. boot0 is necessary and sufficient to identify these GPUs. 220 // boot42 may not even exist on some of these GPUs. 221 // 222 // Fermi through Volta: 223 // 224 // Not supported by Nova. boot0 is still sufficient to identify these GPUs, but boot42 225 // is also guaranteed to be both present and accurate. 226 // 227 // Turing and later: 228 // 229 // Supported by Nova. Identified by first checking boot0 to ensure that the GPU is not 230 // from an earlier (pre-Fermi) era, and then using boot42 to precisely identify the GPU. 231 // Somewhere in the Rubin timeframe, boot0 will no longer have space to add new GPU IDs. 232 233 let boot0 = bar.read(regs::NV_PMC_BOOT_0); 234 235 if boot0.is_older_than_fermi() { 236 return Err(ENODEV); 237 } 238 239 let boot42 = bar.read(regs::NV_PMC_BOOT_42); 240 Spec::try_from(boot42).inspect_err(|_| { 241 dev_err!(dev, "Unsupported chipset: {}\n", boot42); 242 }) 243 } 244 } 245 246 impl TryFrom<regs::NV_PMC_BOOT_42> for Spec { 247 type Error = Error; 248 249 fn try_from(boot42: regs::NV_PMC_BOOT_42) -> Result<Self> { 250 Ok(Self { 251 chipset: boot42.chipset()?, 252 revision: boot42.into(), 253 }) 254 } 255 } 256 257 impl fmt::Display for Spec { 258 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 259 f.write_fmt(fmt!( 260 "Chipset: {}, Architecture: {:?}, Revision: {}", 261 self.chipset, 262 self.chipset.arch(), 263 self.revision 264 )) 265 } 266 } 267 268 /// Self-contained resources to operate and drop the GSP. 269 #[pin_data(PinnedDrop)] 270 struct GspResources<'gpu> { 271 /// Device owning the GPU. 272 device: &'gpu pci::Device<device::Bound>, 273 /// Details about the chipset. 274 spec: Spec, 275 /// MMIO mapping of PCI BAR 0. 276 bar: Bar0<'gpu>, 277 /// GSP falcon instance, used for GSP boot up and cleanup. 278 gsp_falcon: Falcon<'gpu, GspFalcon>, 279 /// SEC2 falcon instance, used for GSP boot up and cleanup. 280 sec2_falcon: Falcon<'gpu, Sec2Falcon>, 281 /// GSP runtime data. 282 #[pin] 283 gsp: Gsp, 284 /// GSP unload firmware bundle, if any. 285 unload_bundle: Option<gsp::UnloadBundle>, 286 } 287 288 /// Structure holding the resources required to operate the GPU. 289 #[pin_data] 290 pub(crate) struct Gpu<'gpu> { 291 spec: Spec, 292 /// Static GPU information as provided by the GSP. 293 gsp_static_info: GetGspStaticInfoReply, 294 /// GSP and its resources. 295 #[pin] 296 gsp_resources: GspResources<'gpu>, 297 /// System memory page required for flushing all pending GPU-side memory writes done through 298 /// PCIE into system memory, via sysmembar (A GPU-initiated HW memory-barrier operation). 299 /// 300 /// Must be kept declared *after* `gsp_resources`, as the latter's `PinnedDrop` implementation 301 /// requires the sysmem flush page to be in place. 302 sysmem_flush: SysmemFlush<'gpu>, 303 } 304 305 #[pinned_drop] 306 impl PinnedDrop for GspResources<'_> { 307 fn drop(self: Pin<&mut Self>) { 308 let this = self.project(); 309 let device = *this.device; 310 let bar = *this.bar; 311 let bundle = this.unload_bundle.take(); 312 313 let _ = this 314 .gsp 315 .as_ref() 316 .get_ref() 317 .unload( 318 GspBootContext { 319 pdev: device, 320 bar, 321 chipset: this.spec.chipset, 322 gsp_falcon: &*this.gsp_falcon, 323 sec2_falcon: &*this.sec2_falcon, 324 }, 325 bundle, 326 ) 327 .inspect_err(|e| dev_err!(device, "failed to unload GSP: {:?}\n", e)); 328 } 329 } 330 331 impl<'gpu> Gpu<'gpu> { 332 pub(crate) fn new( 333 pdev: &'gpu pci::Device<device::Core<'_>>, 334 bar: Bar0<'gpu>, 335 ) -> impl PinInit<Self, Error> + 'gpu { 336 try_pin_init!(Self { 337 spec: Spec::new(pdev.as_ref(), bar).inspect(|spec| { 338 dev_info!(pdev,"NVIDIA ({})\n", spec); 339 })?, 340 341 // We must wait for GFW_BOOT completion before doing any significant setup on the GPU. 342 _: { 343 let hal = hal::gpu_hal(spec.chipset); 344 let dma_mask = hal.dma_mask(); 345 346 // SAFETY: `Gpu` owns all DMA allocations for this device, and we are 347 // still constructing it, so no concurrent DMA allocations can exist. 348 unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? }; 349 350 hal.wait_gfw_boot_completion(bar) 351 .inspect_err(|_| dev_err!(pdev, "GFW boot did not complete\n"))?; 352 }, 353 354 // Initialize this early because `gsp_resources` depends on it. 355 sysmem_flush: SysmemFlush::register(pdev.as_ref(), bar, spec.chipset)?, 356 357 gsp_resources <- try_pin_init!(GspResources { 358 device: pdev, 359 360 spec: *spec, 361 362 bar, 363 364 gsp_falcon: Falcon::new( 365 pdev.as_ref(), 366 spec.chipset, 367 bar 368 ) 369 .inspect(|falcon| falcon.clear_swgen0_intr())?, 370 371 sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset, bar)?, 372 373 gsp <- Gsp::new(pdev), 374 375 // This member must be initialized last, so the `UnloadBundle` can never be dropped 376 // from outside of the constructed `GspResources`, ensuring that the unload sequence 377 // is properly run in case of failure. 378 unload_bundle: gsp.boot(GspBootContext { 379 pdev, 380 bar, 381 chipset: spec.chipset, 382 gsp_falcon, 383 sec2_falcon, 384 })?, 385 }), 386 387 gsp_static_info: { 388 // Obtain and display basic GPU information. 389 let info = gsp_resources.gsp.get_static_info(bar)?; 390 match info.gpu_name() { 391 Ok(name) => dev_info!(pdev, "GPU name: {}\n", name), 392 Err(e) => dev_warn!(pdev, "GPU name unavailable: {:?}\n", e), 393 } 394 395 if !info.usable_fb_regions.is_empty() { 396 dev_dbg!(pdev, "Usable FB regions:\n"); 397 for region in &info.usable_fb_regions { 398 dev_dbg!(pdev, " - {:#x?}\n", region); 399 } 400 401 dev_dbg!( 402 pdev, 403 "Total usable VRAM: {} MiB\n", 404 info.usable_fb_regions.iter().fold(0u64, |res, region| res 405 .saturating_add(region.end - region.start)) 406 / u64::SZ_1M 407 ); 408 } 409 410 info 411 } 412 }) 413 } 414 } 415