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 the address range of the PCI config mirror space. 137 pub(crate) fn pci_config_mirror_range(self) -> Range<u32> { 138 hal::gpu_hal(self).pci_config_mirror_range() 139 } 140 } 141 142 // TODO 143 // 144 // The resulting strings are used to generate firmware paths, hence the 145 // generated strings have to be stable. 146 // 147 // Hence, replace with something like strum_macros derive(Display). 148 // 149 // For now, redirect to fmt::Debug for convenience. 150 impl fmt::Display for Chipset { 151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 152 write!(f, "{self:?}") 153 } 154 } 155 156 bounded_enum! { 157 /// Enum representation of the GPU generation. 158 #[derive(fmt::Debug, Copy, Clone)] 159 pub(crate) enum Architecture with TryFrom<Bounded<u32, 6>> { 160 Turing = 0x16, 161 Ampere = 0x17, 162 Hopper = 0x18, 163 Ada = 0x19, 164 BlackwellGB10x = 0x1a, 165 BlackwellGB20x = 0x1b, 166 } 167 } 168 169 #[derive(Clone, Copy)] 170 pub(crate) struct Revision { 171 major: Bounded<u8, 4>, 172 minor: Bounded<u8, 4>, 173 } 174 175 impl From<regs::NV_PMC_BOOT_42> for Revision { 176 fn from(boot0: regs::NV_PMC_BOOT_42) -> Self { 177 Self { 178 major: boot0.major_revision().cast(), 179 minor: boot0.minor_revision().cast(), 180 } 181 } 182 } 183 184 impl fmt::Display for Revision { 185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 186 write!(f, "{:x}.{:x}", self.major, self.minor) 187 } 188 } 189 190 /// Structure holding a basic description of the GPU: `Chipset` and `Revision`. 191 #[derive(Clone, Copy)] 192 pub(crate) struct Spec { 193 chipset: Chipset, 194 revision: Revision, 195 } 196 197 impl Spec { 198 fn new(dev: &device::Device, bar: Bar0<'_>) -> Result<Spec> { 199 // Some brief notes about boot0 and boot42, in chronological order: 200 // 201 // NV04 through NV50: 202 // 203 // Not supported by Nova. boot0 is necessary and sufficient to identify these GPUs. 204 // boot42 may not even exist on some of these GPUs. 205 // 206 // Fermi through Volta: 207 // 208 // Not supported by Nova. boot0 is still sufficient to identify these GPUs, but boot42 209 // is also guaranteed to be both present and accurate. 210 // 211 // Turing and later: 212 // 213 // Supported by Nova. Identified by first checking boot0 to ensure that the GPU is not 214 // from an earlier (pre-Fermi) era, and then using boot42 to precisely identify the GPU. 215 // Somewhere in the Rubin timeframe, boot0 will no longer have space to add new GPU IDs. 216 217 let boot0 = bar.read(regs::NV_PMC_BOOT_0); 218 219 if boot0.is_older_than_fermi() { 220 return Err(ENODEV); 221 } 222 223 let boot42 = bar.read(regs::NV_PMC_BOOT_42); 224 Spec::try_from(boot42).inspect_err(|_| { 225 dev_err!(dev, "Unsupported chipset: {}\n", boot42); 226 }) 227 } 228 } 229 230 impl TryFrom<regs::NV_PMC_BOOT_42> for Spec { 231 type Error = Error; 232 233 fn try_from(boot42: regs::NV_PMC_BOOT_42) -> Result<Self> { 234 Ok(Self { 235 chipset: boot42.chipset()?, 236 revision: boot42.into(), 237 }) 238 } 239 } 240 241 impl fmt::Display for Spec { 242 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 243 f.write_fmt(fmt!( 244 "Chipset: {}, Architecture: {:?}, Revision: {}", 245 self.chipset, 246 self.chipset.arch(), 247 self.revision 248 )) 249 } 250 } 251 252 /// Self-contained resources to operate and drop the GSP. 253 #[pin_data(PinnedDrop)] 254 struct GspResources<'gpu> { 255 /// Device owning the GPU. 256 device: &'gpu pci::Device<device::Bound>, 257 /// Details about the chipset. 258 spec: Spec, 259 /// MMIO mapping of PCI BAR 0. 260 bar: Bar0<'gpu>, 261 /// GSP falcon instance, used for GSP boot up and cleanup. 262 gsp_falcon: Falcon<'gpu, GspFalcon>, 263 /// SEC2 falcon instance, used for GSP boot up and cleanup. 264 sec2_falcon: Falcon<'gpu, Sec2Falcon>, 265 /// GSP runtime data. 266 #[pin] 267 gsp: Gsp, 268 /// GSP unload firmware bundle, if any. 269 unload_bundle: Option<gsp::UnloadBundle>, 270 } 271 272 /// Structure holding the resources required to operate the GPU. 273 #[pin_data] 274 pub(crate) struct Gpu<'gpu> { 275 spec: Spec, 276 /// Static GPU information as provided by the GSP. 277 gsp_static_info: GetGspStaticInfoReply, 278 /// GSP and its resources. 279 #[pin] 280 gsp_resources: GspResources<'gpu>, 281 /// System memory page required for flushing all pending GPU-side memory writes done through 282 /// PCIE into system memory, via sysmembar (A GPU-initiated HW memory-barrier operation). 283 /// 284 /// Must be kept declared *after* `gsp_resources`, as the latter's `PinnedDrop` implementation 285 /// requires the sysmem flush page to be in place. 286 sysmem_flush: SysmemFlush<'gpu>, 287 } 288 289 #[pinned_drop] 290 impl PinnedDrop for GspResources<'_> { 291 fn drop(self: Pin<&mut Self>) { 292 let this = self.project(); 293 let device = *this.device; 294 let bar = *this.bar; 295 let bundle = this.unload_bundle.take(); 296 297 let _ = this 298 .gsp 299 .as_ref() 300 .get_ref() 301 .unload( 302 GspBootContext { 303 pdev: device, 304 bar, 305 chipset: this.spec.chipset, 306 gsp_falcon: &*this.gsp_falcon, 307 sec2_falcon: &*this.sec2_falcon, 308 }, 309 bundle, 310 ) 311 .inspect_err(|e| dev_err!(device, "failed to unload GSP: {:?}\n", e)); 312 } 313 } 314 315 impl<'gpu> Gpu<'gpu> { 316 pub(crate) fn new( 317 pdev: &'gpu pci::Device<device::Core<'_>>, 318 bar: Bar0<'gpu>, 319 ) -> impl PinInit<Self, Error> + 'gpu { 320 try_pin_init!(Self { 321 spec: Spec::new(pdev.as_ref(), bar).inspect(|spec| { 322 dev_info!(pdev,"NVIDIA ({})\n", spec); 323 })?, 324 325 // We must wait for GFW_BOOT completion before doing any significant setup on the GPU. 326 _: { 327 let hal = hal::gpu_hal(spec.chipset); 328 let dma_mask = hal.dma_mask(); 329 330 // SAFETY: `Gpu` owns all DMA allocations for this device, and we are 331 // still constructing it, so no concurrent DMA allocations can exist. 332 unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? }; 333 334 hal.wait_gfw_boot_completion(bar) 335 .inspect_err(|_| dev_err!(pdev, "GFW boot did not complete\n"))?; 336 }, 337 338 // Initialize this early because `gsp_resources` depends on it. 339 sysmem_flush: SysmemFlush::register(pdev.as_ref(), bar, spec.chipset)?, 340 341 gsp_resources <- try_pin_init!(GspResources { 342 device: pdev, 343 344 spec: *spec, 345 346 bar, 347 348 gsp_falcon: Falcon::new( 349 pdev.as_ref(), 350 spec.chipset, 351 bar 352 ) 353 .inspect(|falcon| falcon.clear_swgen0_intr())?, 354 355 sec2_falcon: Falcon::new(pdev.as_ref(), spec.chipset, bar)?, 356 357 gsp <- Gsp::new(pdev), 358 359 // This member must be initialized last, so the `UnloadBundle` can never be dropped 360 // from outside of the constructed `GspResources`, ensuring that the unload sequence 361 // is properly run in case of failure. 362 unload_bundle: gsp.boot(GspBootContext { 363 pdev, 364 bar, 365 chipset: spec.chipset, 366 gsp_falcon, 367 sec2_falcon, 368 })?, 369 }), 370 371 gsp_static_info: { 372 // Obtain and display basic GPU information. 373 let info = gsp_resources.gsp.get_static_info(bar)?; 374 match info.gpu_name() { 375 Ok(name) => dev_info!(pdev, "GPU name: {}\n", name), 376 Err(e) => dev_warn!(pdev, "GPU name unavailable: {:?}\n", e), 377 } 378 379 if !info.usable_fb_regions.is_empty() { 380 dev_dbg!(pdev, "Usable FB regions:\n"); 381 for region in &info.usable_fb_regions { 382 dev_dbg!(pdev, " - {:#x?}\n", region); 383 } 384 385 dev_dbg!( 386 pdev, 387 "Total usable VRAM: {} MiB\n", 388 info.usable_fb_regions.iter().fold(0u64, |res, region| res 389 .saturating_add(region.end - region.start)) 390 / u64::SZ_1M 391 ); 392 } 393 394 info 395 } 396 }) 397 } 398 } 399