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