| 1 | |
| 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 | vgpu::VgpuManager, |
| 34 | };
|
| 35 |
|
| 36 | mod hal;
|
| 37 |
|
| 38 | macro_rules! define_chipset {
|
| 39 | ({ $($variant:ident = $value:expr),* $(,)* }) =>
|
| 40 | {
|
| 41 | |
| 42 | #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
|
| 43 | pub(crate) enum Chipset {
|
| 44 | $($variant = $value),*,
|
| 45 | }
|
| 46 |
|
| 47 | impl Chipset {
|
| 48 | pub(crate) const ALL: &'static [Chipset] = &[
|
| 49 | $( Chipset::$variant, )*
|
| 50 | ];
|
| 51 |
|
| 52 | ::kernel::macros::paste!(
|
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | pub(crate) const fn name(&self) -> &'static str {
|
| 62 | match *self {
|
| 63 | $(
|
| 64 | Chipset::$variant => stringify!([<$variant:lower>]),
|
| 65 | )*
|
| 66 | }
|
| 67 | }
|
| 68 | );
|
| 69 | }
|
| 70 |
|
| 71 | |
| 72 | impl TryFrom<u32> for Chipset {
|
| 73 | type Error = kernel::error::Error;
|
| 74 |
|
| 75 | fn try_from(value: u32) -> Result<Self, Self::Error> {
|
| 76 | match value {
|
| 77 | $( $value => Ok(Chipset::$variant), )*
|
| 78 | _ => Err(ENODEV),
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | define_chipset!({
|
| 86 | |
| 87 | TU102 = 0x162,
|
| 88 | TU104 = 0x164,
|
| 89 | TU106 = 0x166,
|
| 90 | TU117 = 0x167,
|
| 91 | TU116 = 0x168,
|
| 92 | |
| 93 | GA100 = 0x170,
|
| 94 | GA102 = 0x172,
|
| 95 | GA103 = 0x173,
|
| 96 | GA104 = 0x174,
|
| 97 | GA106 = 0x176,
|
| 98 | GA107 = 0x177,
|
| 99 | |
| 100 | GH100 = 0x180,
|
| 101 | |
| 102 | AD102 = 0x192,
|
| 103 | AD103 = 0x193,
|
| 104 | AD104 = 0x194,
|
| 105 | AD106 = 0x196,
|
| 106 | AD107 = 0x197,
|
| 107 | |
| 108 | GB100 = 0x1a0,
|
| 109 | GB102 = 0x1a2,
|
| 110 | |
| 111 | GB202 = 0x1b2,
|
| 112 | GB203 = 0x1b3,
|
| 113 | GB205 = 0x1b5,
|
| 114 | GB206 = 0x1b6,
|
| 115 | GB207 = 0x1b7,
|
| 116 | });
|
| 117 |
|
| 118 | impl Chipset {
|
| 119 | pub(crate) const fn arch(self) -> Architecture {
|
| 120 | match self {
|
| 121 | Self::TU102 | Self::TU104 | Self::TU106 | Self::TU117 | Self::TU116 => {
|
| 122 | Architecture::Turing
|
| 123 | }
|
| 124 | Self::GA100 | Self::GA102 | Self::GA103 | Self::GA104 | Self::GA106 | Self::GA107 => {
|
| 125 | Architecture::Ampere
|
| 126 | }
|
| 127 | Self::GH100 => Architecture::Hopper,
|
| 128 | Self::AD102 | Self::AD103 | Self::AD104 | Self::AD106 | Self::AD107 => {
|
| 129 | Architecture::Ada
|
| 130 | }
|
| 131 | Self::GB100 | Self::GB102 => Architecture::BlackwellGB10x,
|
| 132 | Self::GB202 | Self::GB203 | Self::GB205 | Self::GB206 | Self::GB207 => {
|
| 133 | Architecture::BlackwellGB20x
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | |
| 139 | pub(crate) fn pci_config_mirror_range(self) -> Range<u32> {
|
| 140 | hal::gpu_hal(self).pci_config_mirror_range()
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | impl fmt::Display for Chipset {
|
| 153 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 154 | write!(f, "{self:?}")
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | bounded_enum! {
|
| 159 | |
| 160 | #[derive(fmt::Debug, Copy, Clone)]
|
| 161 | pub(crate) enum Architecture with TryFrom<Bounded<u32, 6>> {
|
| 162 | Turing = 0x16,
|
| 163 | Ampere = 0x17,
|
| 164 | Hopper = 0x18,
|
| 165 | Ada = 0x19,
|
| 166 | BlackwellGB10x = 0x1a,
|
| 167 | BlackwellGB20x = 0x1b,
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | #[derive(Clone, Copy)]
|
| 172 | pub(crate) struct Revision {
|
| 173 | major: Bounded<u8, 4>,
|
| 174 | minor: Bounded<u8, 4>,
|
| 175 | }
|
| 176 |
|
| 177 | impl From<regs::NV_PMC_BOOT_42> for Revision {
|
| 178 | fn from(boot0: regs::NV_PMC_BOOT_42) -> Self {
|
| 179 | Self {
|
| 180 | major: boot0.major_revision().cast(),
|
| 181 | minor: boot0.minor_revision().cast(),
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | impl fmt::Display for Revision {
|
| 187 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 188 | write!(f, "{:x}.{:x}", self.major, self.minor)
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | |
| 193 | #[derive(Clone, Copy)]
|
| 194 | pub(crate) struct Spec {
|
| 195 | chipset: Chipset,
|
| 196 | revision: Revision,
|
| 197 | }
|
| 198 |
|
| 199 | impl Spec {
|
| 200 | fn new(dev: &device::Device, bar: Bar0<'_>) -> Result<Spec> {
|
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 |
|
| 219 | let boot0 = bar.read(regs::NV_PMC_BOOT_0);
|
| 220 |
|
| 221 | if boot0.is_older_than_fermi() {
|
| 222 | return Err(ENODEV);
|
| 223 | }
|
| 224 |
|
| 225 | let boot42 = bar.read(regs::NV_PMC_BOOT_42);
|
| 226 | Spec::try_from(boot42).inspect_err(|_| {
|
| 227 | dev_err!(dev, "Unsupported chipset: {}\n", boot42);
|
| 228 | })
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | impl TryFrom<regs::NV_PMC_BOOT_42> for Spec {
|
| 233 | type Error = Error;
|
| 234 |
|
| 235 | fn try_from(boot42: regs::NV_PMC_BOOT_42) -> Result<Self> {
|
| 236 | Ok(Self {
|
| 237 | chipset: boot42.chipset()?,
|
| 238 | revision: boot42.into(),
|
| 239 | })
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | impl fmt::Display for Spec {
|
| 244 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 245 | f.write_fmt(fmt!(
|
| 246 | "Chipset: {}, Architecture: {:?}, Revision: {}",
|
| 247 | self.chipset,
|
| 248 | self.chipset.arch(),
|
| 249 | self.revision
|
| 250 | ))
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | |
| 255 | #[pin_data(PinnedDrop)]
|
| 256 | struct GspResources<'gpu> {
|
| 257 | |
| 258 | device: &'gpu pci::Device<device::Bound>,
|
| 259 | |
| 260 | spec: Spec,
|
| 261 | |
| 262 | bar: Bar0<'gpu>,
|
| 263 | |
| 264 | gsp_falcon: Falcon<'gpu, GspFalcon>,
|
| 265 | |
| 266 | sec2_falcon: Falcon<'gpu, Sec2Falcon>,
|
| 267 | |
| 268 | |
| 269 | |
| 270 | fsp: Option<Fsp<'gpu>>,
|
| 271 | |
| 272 | vgpu: VgpuManager,
|
| 273 | |
| 274 | #[pin]
|
| 275 | gsp: Gsp,
|
| 276 | |
| 277 | unload_bundle: Option<gsp::UnloadBundle>,
|
| 278 | }
|
| 279 |
|
| 280 | |
| 281 | #[pin_data]
|
| 282 | pub(crate) struct Gpu<'gpu> {
|
| 283 | spec: Spec,
|
| 284 | |
| 285 | gsp_static_info: GetGspStaticInfoReply,
|
| 286 | |
| 287 | #[pin]
|
| 288 | gsp_resources: GspResources<'gpu>,
|
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | sysmem_flush: SysmemFlush<'gpu>,
|
| 295 | }
|
| 296 |
|
| 297 | #[pinned_drop]
|
| 298 | impl PinnedDrop for GspResources<'_> {
|
| 299 | fn drop(self: Pin<&mut Self>) {
|
| 300 | let this = self.project();
|
| 301 | let device = *this.device;
|
| 302 | let bar = *this.bar;
|
| 303 | let bundle = this.unload_bundle.take();
|
| 304 |
|
| 305 | let _ = this
|
| 306 | .gsp
|
| 307 | .as_ref()
|
| 308 | .get_ref()
|
| 309 | .unload(
|
| 310 | GspBootContext {
|
| 311 | pdev: device,
|
| 312 | bar,
|
| 313 | chipset: this.spec.chipset,
|
| 314 | gsp_falcon: &*this.gsp_falcon,
|
| 315 | sec2_falcon: &*this.sec2_falcon,
|
| 316 | fsp: this.fsp.as_mut(),
|
| 317 | vgpu: &*this.vgpu,
|
| 318 | },
|
| 319 | bundle,
|
| 320 | )
|
| 321 | .inspect_err(|e| dev_err!(device, "failed to unload GSP: {:?}\n", e));
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | impl<'gpu> Gpu<'gpu> {
|
| 326 | pub(crate) fn new<'a>(
|
| 327 | pdev: &'gpu pci::Device<device::Core<'a>>,
|
| 328 | bar: Bar0<'gpu>,
|
| 329 | ) -> impl PinInit<Self, Error> + use<'gpu, 'a> {
|
| 330 | let dev = pdev.as_ref();
|
| 331 |
|
| 332 | try_pin_init!(Self {
|
| 333 | spec: Spec::new(dev, bar).inspect(|spec| {
|
| 334 | dev_info!(dev,"NVIDIA ({})\n", spec);
|
| 335 | })?,
|
| 336 |
|
| 337 | |
| 338 | _: {
|
| 339 | let hal = hal::gpu_hal(spec.chipset);
|
| 340 | let dma_mask = hal.dma_mask();
|
| 341 |
|
| 342 | |
| 343 | |
| 344 | unsafe { pdev.dma_set_mask_and_coherent(dma_mask)? };
|
| 345 |
|
| 346 | hal.wait_gfw_boot_completion(bar)
|
| 347 | .inspect_err(|_| dev_err!(dev, "GFW boot did not complete\n"))?;
|
| 348 | },
|
| 349 |
|
| 350 | |
| 351 | sysmem_flush: SysmemFlush::register(dev, bar, spec.chipset)?,
|
| 352 |
|
| 353 | gsp_resources <- try_pin_init!(GspResources {
|
| 354 | device: pdev,
|
| 355 |
|
| 356 | spec: *spec,
|
| 357 |
|
| 358 | bar,
|
| 359 |
|
| 360 | gsp_falcon: Falcon::new(
|
| 361 | dev,
|
| 362 | spec.chipset,
|
| 363 | bar
|
| 364 | )
|
| 365 | .inspect(|falcon| falcon.clear_swgen0_intr())?,
|
| 366 |
|
| 367 | sec2_falcon: Falcon::new(dev, spec.chipset, bar)?,
|
| 368 |
|
| 369 | fsp: Fsp::try_new(dev, bar, spec.chipset)?,
|
| 370 |
|
| 371 | vgpu: VgpuManager::new(pdev, spec.chipset, fsp.as_mut()),
|
| 372 |
|
| 373 | gsp <- Gsp::new(pdev),
|
| 374 |
|
| 375 | |
| 376 | |
| 377 | |
| 378 | unload_bundle: gsp.boot(GspBootContext {
|
| 379 | pdev,
|
| 380 | bar,
|
| 381 | chipset: spec.chipset,
|
| 382 | gsp_falcon,
|
| 383 | sec2_falcon,
|
| 384 | fsp: fsp.as_mut(),
|
| 385 | vgpu,
|
| 386 | })?,
|
| 387 | }),
|
| 388 |
|
| 389 | gsp_static_info: {
|
| 390 | |
| 391 | let info = gsp_resources.gsp.get_static_info(bar)?;
|
| 392 | match info.gpu_name() {
|
| 393 | Ok(name) => dev_info!(dev, "GPU name: {}\n", name),
|
| 394 | Err(e) => dev_warn!(dev, "GPU name unavailable: {:?}\n", e),
|
| 395 | }
|
| 396 |
|
| 397 | if !info.usable_fb_regions.is_empty() {
|
| 398 | dev_dbg!(dev, "Usable FB regions:\n");
|
| 399 | for region in &info.usable_fb_regions {
|
| 400 | dev_dbg!(dev, " - {:#x?}\n", region);
|
| 401 | }
|
| 402 |
|
| 403 | dev_dbg!(
|
| 404 | dev,
|
| 405 | "Total usable VRAM: {} MiB\n",
|
| 406 | info.usable_fb_regions.iter().fold(0u64, |res, region| res
|
| 407 | .saturating_add(region.end - region.start))
|
| 408 | / u64::SZ_1M
|
| 409 | );
|
| 410 | }
|
| 411 |
|
| 412 | info
|
| 413 | }
|
| 414 | })
|
| 415 | }
|
| 416 | }
|