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 device::Device<device::Bound>, 273 /// MMIO mapping of PCI BAR 0. 274 bar: Bar0<'gpu>, 275 /// GSP falcon instance, used for GSP boot up and cleanup. 276 gsp_falcon: Falcon<'gpu, GspFalcon>, 277 /// SEC2 falcon instance, used for GSP boot up and cleanup. 278 sec2_falcon: Falcon<'gpu, Sec2Falcon>, 279 /// GSP runtime data. 280 #[pin] 281 gsp: Gsp, 282 /// GSP unload firmware bundle, if any. 283 unload_bundle: Option<gsp::UnloadBundle>, 284 } 285 286 /// Structure holding the resources required to operate the GPU. 287 #[pin_data] 288 pub(crate) struct Gpu<'gpu> { 289 spec: Spec, 290 /// Static GPU information as provided by the GSP. 291 gsp_static_info: GetGspStaticInfoReply, 292 /// GSP and its resources. 293 #[pin] 294 gsp_resources: GspResources<'gpu>, 295 /// System memory page required for flushing all pending GPU-side memory writes done through 296 /// PCIE into system memory, via sysmembar (A GPU-initiated HW memory-barrier operation). 297 /// 298 /// Must be kept declared *after* `gsp_resources`, as the latter's `PinnedDrop` implementation 299 /// requires the sysmem flush page to be in place. 300 sysmem_flush: SysmemFlush<'gpu>, 301 } 302 303 #[pinned_drop] 304 impl PinnedDrop for GspResources<'_> { 305 fn drop(self: Pin<&mut Self>) { 306 let this = self.project(); 307 let device = *this.device; 308 let bar = *this.bar; 309 let bundle = this.unload_bundle.take(); 310 311 let _ = this 312 .gsp 313 .as_ref() 314 .get_ref() 315 .unload(device, bar, &*this.gsp_falcon, &*this.sec2_falcon, bundle) 316 .inspect_err(|e| dev_err!(device, "failed to unload GSP: {:?}\n", e)); 317 } 318 } 319 320 impl<'gpu> Gpu<'gpu> { 321 pub(crate) fn new( 322 pdev: &'gpu pci::Device<device::Core<'_>>, 323 bar: Bar0<'gpu>, 324 ) -> impl PinInit<Self, Error> + 'gpu { 325 try_pin_init!(Self { 326 spec: Spec::new(pdev.as_ref(), bar).inspect(|spec| { 327 dev_info!(pdev,"NVIDIA ({})\n", spec); 328 })?, 329 330 // We must wait for GFW_BOOT completion before doing any significant setup on the GPU. 331 _: { 332 let hal = hal::gpu_hal(spec.chipset); 333 let dma_mask = hal.dma_mask(); 334 335 // SAFETY: `Gpu` owns all DMA allocations for this device, and we are 336 // still constructing it, so no concurrent DMA allocations can exist. 337 unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? }; 338 339 hal.wait_gfw_boot_completion(bar) 340 .inspect_err(|_| dev_err!(pdev, "GFW boot did not complete\n"))?; 341 }, 342 343 // Initialize this early because `gsp_resources` depends on it. 344 sysmem_flush: SysmemFlush::register(pdev.as_ref(), bar, spec.chipset)?, 345 346 gsp_resources <- try_pin_init!(GspResources { 347 device: pdev.as_ref(), 348 349 bar, 350 351 gsp_falcon: Falcon::new( 352 pdev.as_ref(), 353 spec.chipset, 354 bar 355 ) 356 .inspect(|falcon| falcon.clear_swgen0_intr())?, 357 358 sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset, bar)?, 359 360 gsp <- Gsp::new(pdev), 361 362 // This member must be initialized last, so the `UnloadBundle` can never be dropped 363 // from outside of the constructed `GspResources`, ensuring that the unload sequence 364 // is properly run in case of failure. 365 unload_bundle: gsp.boot(GspBootContext { 366 pdev, 367 bar, 368 chipset: spec.chipset, 369 gsp_falcon, 370 sec2_falcon, 371 })?, 372 }), 373 374 gsp_static_info: { 375 // Obtain and display basic GPU information. 376 let info = gsp_resources.gsp.get_static_info(bar)?; 377 match info.gpu_name() { 378 Ok(name) => dev_info!(pdev, "GPU name: {}\n", name), 379 Err(e) => dev_warn!(pdev, "GPU name unavailable: {:?}\n", e), 380 } 381 382 if !info.usable_fb_regions.is_empty() { 383 dev_dbg!(pdev, "Usable FB regions:\n"); 384 for region in &info.usable_fb_regions { 385 dev_dbg!(pdev, " - {:#x?}\n", region); 386 } 387 388 dev_dbg!( 389 pdev, 390 "Total usable VRAM: {} MiB\n", 391 info.usable_fb_regions.iter().fold(0u64, |res, region| res 392 .saturating_add(region.end - region.start)) 393 / u64::SZ_1M 394 ); 395 } 396 397 info 398 } 399 }) 400 } 401 } 402