aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-accel / src / profile.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Typed, provenance-tagged GB10 hardware characterization facts. |
| 17 | //! |
| 18 | //! The profile keeps three states apart instead of collapsing them: |
| 19 | //! |
| 20 | //! - `Observed`: a value read from a named source. |
| 21 | //! - `Derived`: a value computed from observed values, not read directly. |
| 22 | //! - `Unknown`: no value was obtained. Absence of evidence is never a zero or |
| 23 | //! a default hardware property. |
| 24 | //! |
| 25 | //! Every value also carries its source, and a flag for whether native AIENOS |
| 26 | //! has independently verified it. A value observed through Linux sysfs is not |
| 27 | //! the same claim as a value decoded by native AIENOS PCI enumeration, and the |
| 28 | //! type keeps them distinct. |
| 29 | |
| 30 | use crate::pci::{ |
| 31 | Bar, CapabilityList, Header, MsiCapability, MsixCapability, PciError, PcieCapability, PcieLink, |
| 32 | CAP_ID_MSI, CAP_ID_MSIX, CAP_ID_PCIE, |
| 33 | }; |
| 34 | use crate::{Gb10Bar, PciLocation, GB10_DEVICE_ID, NVIDIA_VENDOR_ID}; |
| 35 | use core::fmt::{Debug, Write}; |
| 36 | |
| 37 | /// Where a fact came from. The source is part of the fact, not a footnote. |
| 38 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 39 | pub enum Source { |
| 40 | /// Read through UEFI firmware services before `ExitBootServices`. |
| 41 | UefiFirmwareService, |
| 42 | /// Decoded by native AIENOS PCI enumeration. |
| 43 | AienosNativePci, |
| 44 | /// Read from a Linux sysfs attribute. |
| 45 | LinuxSysfs, |
| 46 | /// Read from a Linux procfs attribute. |
| 47 | LinuxProcfs, |
| 48 | /// Parsed from an ACPI table exposed by the kernel. |
| 49 | LinuxAcpi, |
| 50 | /// Built by a test from synthetic bytes. |
| 51 | SyntheticFixture, |
| 52 | /// Computed from other facts rather than read from hardware. |
| 53 | Derived, |
| 54 | } |
| 55 | |
| 56 | impl Source { |
| 57 | pub const fn name(self) -> &'static str { |
| 58 | match self { |
| 59 | Source::UefiFirmwareService => "uefi-firmware-service", |
| 60 | Source::AienosNativePci => "aienos-native-pci", |
| 61 | Source::LinuxSysfs => "linux-sysfs", |
| 62 | Source::LinuxProcfs => "linux-procfs", |
| 63 | Source::LinuxAcpi => "linux-acpi", |
| 64 | Source::SyntheticFixture => "synthetic-fixture", |
| 65 | Source::Derived => "derived", |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// True when Linux, not AIENOS, interpreted the value. |
| 70 | pub const fn linux_interpreted(self) -> bool { |
| 71 | matches!( |
| 72 | self, |
| 73 | Source::LinuxSysfs | Source::LinuxProcfs | Source::LinuxAcpi |
| 74 | ) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 79 | pub enum State { |
| 80 | Observed, |
| 81 | Derived, |
| 82 | Unknown, |
| 83 | } |
| 84 | |
| 85 | /// One immutable characterization fact. |
| 86 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 87 | pub struct Fact<T: Copy> { |
| 88 | state: State, |
| 89 | value: Option<T>, |
| 90 | source: Option<Source>, |
| 91 | native_verified: bool, |
| 92 | } |
| 93 | |
| 94 | impl<T: Copy> Fact<T> { |
| 95 | pub const fn unknown() -> Self { |
| 96 | Self { |
| 97 | state: State::Unknown, |
| 98 | value: None, |
| 99 | source: None, |
| 100 | native_verified: false, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pub const fn observed(value: T, source: Source) -> Self { |
| 105 | Self { |
| 106 | state: State::Observed, |
| 107 | value: Some(value), |
| 108 | source: Some(source), |
| 109 | native_verified: matches!(source, Source::AienosNativePci), |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub const fn derived(value: T, source: Source) -> Self { |
| 114 | Self { |
| 115 | state: State::Derived, |
| 116 | value: Some(value), |
| 117 | source: Some(source), |
| 118 | native_verified: false, |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | pub const fn marked_native_verified(self) -> Self { |
| 123 | Self { |
| 124 | state: self.state, |
| 125 | value: self.value, |
| 126 | source: self.source, |
| 127 | native_verified: true, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | pub const fn with_source(self, source: Source) -> Self { |
| 132 | Self { |
| 133 | state: self.state, |
| 134 | value: self.value, |
| 135 | source: Some(source), |
| 136 | native_verified: self.native_verified, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | pub fn state(&self) -> State { |
| 141 | self.state |
| 142 | } |
| 143 | |
| 144 | pub fn value(&self) -> Option<T> { |
| 145 | self.value |
| 146 | } |
| 147 | |
| 148 | pub fn source(&self) -> Option<Source> { |
| 149 | self.source |
| 150 | } |
| 151 | |
| 152 | pub fn native_verified(&self) -> bool { |
| 153 | self.native_verified |
| 154 | } |
| 155 | |
| 156 | pub fn is_known(&self) -> bool { |
| 157 | self.value.is_some() |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 162 | pub struct ClassCode { |
| 163 | pub base: u8, |
| 164 | pub subclass: u8, |
| 165 | pub prog_if: u8, |
| 166 | } |
| 167 | |
| 168 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 169 | pub enum BarKind { |
| 170 | None, |
| 171 | Memory, |
| 172 | Io, |
| 173 | } |
| 174 | |
| 175 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 176 | pub struct BarFacts { |
| 177 | pub index: u8, |
| 178 | pub kind: BarKind, |
| 179 | pub base: u64, |
| 180 | pub is_64_bit: bool, |
| 181 | pub prefetchable: bool, |
| 182 | } |
| 183 | |
| 184 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 185 | pub struct PcieFacts { |
| 186 | pub present: bool, |
| 187 | pub max_payload_bytes: u16, |
| 188 | pub link: Option<PcieLink>, |
| 189 | } |
| 190 | |
| 191 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 192 | pub struct MsiFacts { |
| 193 | pub present: bool, |
| 194 | pub enabled: bool, |
| 195 | pub is_64_bit: bool, |
| 196 | pub maskable: bool, |
| 197 | pub capable_vectors: u16, |
| 198 | } |
| 199 | |
| 200 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 201 | pub struct MsixFacts { |
| 202 | pub present: bool, |
| 203 | pub enabled: bool, |
| 204 | pub function_masked: bool, |
| 205 | pub vectors: u16, |
| 206 | pub table_bir: u8, |
| 207 | pub table_offset: u32, |
| 208 | pub pba_bir: u8, |
| 209 | pub pba_offset: u32, |
| 210 | } |
| 211 | |
| 212 | const NO_PCIE: PcieFacts = PcieFacts { |
| 213 | present: false, |
| 214 | max_payload_bytes: 0, |
| 215 | link: None, |
| 216 | }; |
| 217 | |
| 218 | const NO_MSI: MsiFacts = MsiFacts { |
| 219 | present: false, |
| 220 | enabled: false, |
| 221 | is_64_bit: false, |
| 222 | maskable: false, |
| 223 | capable_vectors: 0, |
| 224 | }; |
| 225 | |
| 226 | const NO_MSIX: MsixFacts = MsixFacts { |
| 227 | present: false, |
| 228 | enabled: false, |
| 229 | function_masked: false, |
| 230 | vectors: 0, |
| 231 | table_bir: 0, |
| 232 | table_offset: 0, |
| 233 | pba_bir: 0, |
| 234 | pba_offset: 0, |
| 235 | }; |
| 236 | |
| 237 | /// The immutable profile. PCI-derived fields come from a configuration-space |
| 238 | /// read; firmware, IOMMU, NUMA, and PMC fields are filled only from sources |
| 239 | /// that actually expose them. |
| 240 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 241 | pub struct Gb10Profile { |
| 242 | pub location: PciLocation, |
| 243 | pub vendor_id: Fact<u16>, |
| 244 | pub device_id: Fact<u16>, |
| 245 | pub subsystem_vendor_id: Fact<u16>, |
| 246 | pub subsystem_id: Fact<u16>, |
| 247 | pub revision: Fact<u8>, |
| 248 | pub class: Fact<ClassCode>, |
| 249 | pub command: Fact<u16>, |
| 250 | pub status: Fact<u16>, |
| 251 | pub bars: [Fact<BarFacts>; 6], |
| 252 | pub bar_sizes: [Fact<u64>; 6], |
| 253 | pub pcie: Fact<PcieFacts>, |
| 254 | pub msi: Fact<MsiFacts>, |
| 255 | pub msix: Fact<MsixFacts>, |
| 256 | pub iommu_group: Fact<u32>, |
| 257 | pub numa_node: Fact<i32>, |
| 258 | pub firmware_path: Fact<&'static str>, |
| 259 | pub pmc_boot_0: Fact<u32>, |
| 260 | pub pmc_boot_42: Fact<u32>, |
| 261 | } |
| 262 | |
| 263 | impl Gb10Profile { |
| 264 | /// A profile with every fact unknown. Use this when discovery has not run. |
| 265 | pub const fn unknown(location: PciLocation) -> Self { |
| 266 | Self { |
| 267 | location, |
| 268 | vendor_id: Fact::unknown(), |
| 269 | device_id: Fact::unknown(), |
| 270 | subsystem_vendor_id: Fact::unknown(), |
| 271 | subsystem_id: Fact::unknown(), |
| 272 | revision: Fact::unknown(), |
| 273 | class: Fact::unknown(), |
| 274 | command: Fact::unknown(), |
| 275 | status: Fact::unknown(), |
| 276 | bars: [Fact::unknown(); 6], |
| 277 | bar_sizes: [Fact::unknown(); 6], |
| 278 | pcie: Fact::unknown(), |
| 279 | msi: Fact::unknown(), |
| 280 | msix: Fact::unknown(), |
| 281 | iommu_group: Fact::unknown(), |
| 282 | numa_node: Fact::unknown(), |
| 283 | firmware_path: Fact::unknown(), |
| 284 | pmc_boot_0: Fact::unknown(), |
| 285 | pmc_boot_42: Fact::unknown(), |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// Decode the PCI-derived facts from raw configuration-space bytes. Facts |
| 290 | /// that configuration space cannot answer stay `Unknown`. |
| 291 | pub fn from_pci_config( |
| 292 | location: PciLocation, |
| 293 | bytes: &[u8], |
| 294 | source: Source, |
| 295 | ) -> Result<Self, PciError> { |
| 296 | let header = Header::parse(bytes)?; |
| 297 | let mut profile = Self::unknown(location); |
| 298 | profile.vendor_id = Fact::observed(header.vendor_id, source); |
| 299 | profile.device_id = Fact::observed(header.device_id, source); |
| 300 | profile.subsystem_vendor_id = Fact::observed(header.subsystem_vendor_id, source); |
| 301 | profile.subsystem_id = Fact::observed(header.subsystem_id, source); |
| 302 | profile.revision = Fact::observed(header.revision, source); |
| 303 | profile.class = Fact::observed( |
| 304 | ClassCode { |
| 305 | base: header.class_code, |
| 306 | subclass: header.subclass, |
| 307 | prog_if: header.prog_if, |
| 308 | }, |
| 309 | source, |
| 310 | ); |
| 311 | profile.command = Fact::observed(header.command, source); |
| 312 | profile.status = Fact::observed(header.status, source); |
| 313 | for (index, bar) in header.bars.iter().enumerate() { |
| 314 | profile.bars[index] = Fact::observed(bar_facts(index, *bar), source); |
| 315 | } |
| 316 | |
| 317 | let capabilities = crate::pci::walk_capabilities(bytes)?; |
| 318 | profile.pcie = Fact::observed(decode_pcie(&capabilities, bytes), source); |
| 319 | profile.msi = Fact::observed(decode_msi(&capabilities, bytes), source); |
| 320 | profile.msix = Fact::observed(decode_msix(&capabilities, bytes), source); |
| 321 | Ok(profile) |
| 322 | } |
| 323 | |
| 324 | pub fn write(&self, out: &mut impl Write) { |
| 325 | let _ = writeln!( |
| 326 | out, |
| 327 | "gb10.profile.location: segment {} bus {} device {} function {}", |
| 328 | self.location.segment, self.location.bus, self.location.device, self.location.function |
| 329 | ); |
| 330 | write_fact(out, "gb10.profile.vendor_id", self.vendor_id); |
| 331 | write_fact(out, "gb10.profile.device_id", self.device_id); |
| 332 | write_fact( |
| 333 | out, |
| 334 | "gb10.profile.subsystem_vendor_id", |
| 335 | self.subsystem_vendor_id, |
| 336 | ); |
| 337 | write_fact(out, "gb10.profile.subsystem_id", self.subsystem_id); |
| 338 | write_fact(out, "gb10.profile.revision", self.revision); |
| 339 | write_fact(out, "gb10.profile.class", self.class); |
| 340 | write_fact(out, "gb10.profile.command", self.command); |
| 341 | write_fact(out, "gb10.profile.status", self.status); |
| 342 | for (index, bar) in self.bars.iter().enumerate() { |
| 343 | write_indexed_fact(out, "gb10.profile.bar", index, *bar); |
| 344 | } |
| 345 | for (index, size) in self.bar_sizes.iter().enumerate() { |
| 346 | write_indexed_fact(out, "gb10.profile.bar_size", index, *size); |
| 347 | } |
| 348 | write_fact(out, "gb10.profile.pcie", self.pcie); |
| 349 | write_fact(out, "gb10.profile.msi", self.msi); |
| 350 | write_fact(out, "gb10.profile.msix", self.msix); |
| 351 | write_fact(out, "gb10.profile.iommu_group", self.iommu_group); |
| 352 | write_fact(out, "gb10.profile.numa_node", self.numa_node); |
| 353 | write_fact(out, "gb10.profile.firmware_path", self.firmware_path); |
| 354 | write_fact(out, "gb10.profile.pmc_boot_0", self.pmc_boot_0); |
| 355 | write_fact(out, "gb10.profile.pmc_boot_42", self.pmc_boot_42); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | fn bar_facts(index: usize, bar: Bar) -> BarFacts { |
| 360 | match bar { |
| 361 | Bar::None => BarFacts { |
| 362 | index: index as u8, |
| 363 | kind: BarKind::None, |
| 364 | base: 0, |
| 365 | is_64_bit: false, |
| 366 | prefetchable: false, |
| 367 | }, |
| 368 | Bar::Memory(memory) => BarFacts { |
| 369 | index: index as u8, |
| 370 | kind: BarKind::Memory, |
| 371 | base: memory.base, |
| 372 | is_64_bit: memory.is_64_bit, |
| 373 | prefetchable: memory.prefetchable, |
| 374 | }, |
| 375 | Bar::Io(io) => BarFacts { |
| 376 | index: index as u8, |
| 377 | kind: BarKind::Io, |
| 378 | base: u64::from(io.base), |
| 379 | is_64_bit: false, |
| 380 | prefetchable: false, |
| 381 | }, |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | fn decode_pcie(capabilities: &CapabilityList, bytes: &[u8]) -> PcieFacts { |
| 386 | match capabilities.find(CAP_ID_PCIE) { |
| 387 | Some(capability) => match PcieCapability::decode(bytes, capability) { |
| 388 | Ok(pcie) => PcieFacts { |
| 389 | present: true, |
| 390 | max_payload_bytes: pcie.max_payload_bytes, |
| 391 | link: pcie.link, |
| 392 | }, |
| 393 | Err(_) => NO_PCIE, |
| 394 | }, |
| 395 | None => NO_PCIE, |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | fn decode_msi(capabilities: &CapabilityList, bytes: &[u8]) -> MsiFacts { |
| 400 | match capabilities.find(CAP_ID_MSI) { |
| 401 | Some(capability) => match MsiCapability::decode(bytes, capability) { |
| 402 | Ok(msi) => MsiFacts { |
| 403 | present: true, |
| 404 | enabled: msi.enabled, |
| 405 | is_64_bit: msi.is_64_bit, |
| 406 | maskable: msi.maskable, |
| 407 | capable_vectors: msi.capable_vectors, |
| 408 | }, |
| 409 | Err(_) => NO_MSI, |
| 410 | }, |
| 411 | None => NO_MSI, |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | fn decode_msix(capabilities: &CapabilityList, bytes: &[u8]) -> MsixFacts { |
| 416 | match capabilities.find(CAP_ID_MSIX) { |
| 417 | Some(capability) => match MsixCapability::decode(bytes, capability) { |
| 418 | Ok(msix) => MsixFacts { |
| 419 | present: true, |
| 420 | enabled: msix.enabled, |
| 421 | function_masked: msix.function_masked, |
| 422 | vectors: msix.vectors, |
| 423 | table_bir: msix.table_bir, |
| 424 | table_offset: msix.table_offset, |
| 425 | pba_bir: msix.pba_bir, |
| 426 | pba_offset: msix.pba_offset, |
| 427 | }, |
| 428 | Err(_) => NO_MSIX, |
| 429 | }, |
| 430 | None => NO_MSIX, |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | fn write_indexed_fact<T: Debug + Copy>( |
| 435 | out: &mut impl Write, |
| 436 | prefix: &str, |
| 437 | index: usize, |
| 438 | fact: Fact<T>, |
| 439 | ) { |
| 440 | match (fact.state(), fact.value(), fact.source()) { |
| 441 | (State::Observed, Some(value), Some(source)) => { |
| 442 | let _ = writeln!( |
| 443 | out, |
| 444 | "{prefix}[{index}]: {value:?} [observed source={} native_verified={}]", |
| 445 | source.name(), |
| 446 | fact.native_verified() |
| 447 | ); |
| 448 | } |
| 449 | (State::Derived, Some(value), Some(source)) => { |
| 450 | let _ = writeln!( |
| 451 | out, |
| 452 | "{prefix}[{index}]: {value:?} [derived source={} native_verified={}]", |
| 453 | source.name(), |
| 454 | fact.native_verified() |
| 455 | ); |
| 456 | } |
| 457 | _ => { |
| 458 | let _ = writeln!(out, "{prefix}[{index}]: unknown"); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | fn write_fact<T: Debug + Copy>(out: &mut impl Write, name: &str, fact: Fact<T>) { |
| 464 | match (fact.state(), fact.value(), fact.source()) { |
| 465 | (State::Observed, Some(value), Some(source)) => { |
| 466 | let _ = writeln!( |
| 467 | out, |
| 468 | "{name}: {value:?} [observed source={} native_verified={}]", |
| 469 | source.name(), |
| 470 | fact.native_verified() |
| 471 | ); |
| 472 | } |
| 473 | (State::Derived, Some(value), Some(source)) => { |
| 474 | let _ = writeln!( |
| 475 | out, |
| 476 | "{name}: {value:?} [derived source={} native_verified={}]", |
| 477 | source.name(), |
| 478 | fact.native_verified() |
| 479 | ); |
| 480 | } |
| 481 | _ => { |
| 482 | let _ = writeln!(out, "{name}: unknown"); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /// Why a GB10 candidate did not satisfy the contract. Each variant names the |
| 488 | /// exact property that failed so a report can distinguish a wrong device from |
| 489 | /// a device whose memory decoding or BAR0 is not yet usable. |
| 490 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 491 | pub enum Gb10Rejection { |
| 492 | NotNvidia, |
| 493 | WrongDevice, |
| 494 | MemoryDecodeDisabled, |
| 495 | Bar0Missing, |
| 496 | Bar0NotMemory, |
| 497 | Bar0Unprogrammed, |
| 498 | Bar0Not64Bit, |
| 499 | } |
| 500 | |
| 501 | /// A GB10 endpoint that satisfies the typed contract. |
| 502 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 503 | pub struct Gb10Match { |
| 504 | pub location: PciLocation, |
| 505 | pub bar0: crate::pci::MemoryBar, |
| 506 | } |
| 507 | |
| 508 | /// Validate a decoded endpoint header against the GB10 contract using device |
| 509 | /// properties only. No bus, device, or function number is assumed: a relocated |
| 510 | /// GB10 matches exactly as the observed one does. |
| 511 | pub fn match_gb10(location: PciLocation, header: &Header) -> Result<Gb10Match, Gb10Rejection> { |
| 512 | if header.vendor_id != NVIDIA_VENDOR_ID { |
| 513 | return Err(Gb10Rejection::NotNvidia); |
| 514 | } |
| 515 | if header.device_id != GB10_DEVICE_ID { |
| 516 | return Err(Gb10Rejection::WrongDevice); |
| 517 | } |
| 518 | if !header.memory_enabled() { |
| 519 | return Err(Gb10Rejection::MemoryDecodeDisabled); |
| 520 | } |
| 521 | match header.bars[0] { |
| 522 | Bar::None => Err(Gb10Rejection::Bar0Missing), |
| 523 | Bar::Io(_) => Err(Gb10Rejection::Bar0NotMemory), |
| 524 | Bar::Memory(memory) => { |
| 525 | if memory.base == 0 { |
| 526 | Err(Gb10Rejection::Bar0Unprogrammed) |
| 527 | } else if !memory.is_64_bit { |
| 528 | Err(Gb10Rejection::Bar0Not64Bit) |
| 529 | } else { |
| 530 | Ok(Gb10Match { |
| 531 | location, |
| 532 | bar0: memory, |
| 533 | }) |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | impl Gb10Bar { |
| 540 | /// Validate the legacy handoff register values against the GB10 contract |
| 541 | /// and name why a candidate was rejected. Keeping the contract here means |
| 542 | /// the boot handoff and the host profile check the same properties. |
| 543 | pub fn from_header( |
| 544 | location: PciLocation, |
| 545 | vendor_device: u32, |
| 546 | command_status: u32, |
| 547 | bar0_low: u32, |
| 548 | bar0_high: u32, |
| 549 | ) -> Result<Self, Gb10Rejection> { |
| 550 | if (vendor_device & 0xffff) as u16 != NVIDIA_VENDOR_ID { |
| 551 | return Err(Gb10Rejection::NotNvidia); |
| 552 | } |
| 553 | if (vendor_device >> 16) as u16 != GB10_DEVICE_ID { |
| 554 | return Err(Gb10Rejection::WrongDevice); |
| 555 | } |
| 556 | if command_status & 0x0002 == 0 { |
| 557 | return Err(Gb10Rejection::MemoryDecodeDisabled); |
| 558 | } |
| 559 | if bar0_low & 0x1 == 0x1 { |
| 560 | return Err(Gb10Rejection::Bar0NotMemory); |
| 561 | } |
| 562 | if bar0_low == 0 && bar0_high == 0 { |
| 563 | return Err(Gb10Rejection::Bar0Unprogrammed); |
| 564 | } |
| 565 | if bar0_low & 0x7 != 0x4 { |
| 566 | return Err(Gb10Rejection::Bar0Not64Bit); |
| 567 | } |
| 568 | let physical_base = (u64::from(bar0_high) << 32) | u64::from(bar0_low & !0xf); |
| 569 | if physical_base == 0 { |
| 570 | return Err(Gb10Rejection::Bar0Unprogrammed); |
| 571 | } |
| 572 | Ok(Self { |
| 573 | location, |
| 574 | physical_base, |
| 575 | }) |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | #[cfg(test)] |
| 580 | mod tests { |
| 581 | use super::*; |
| 582 | |
| 583 | pub(crate) fn gb10_config() -> [u8; crate::pci::CONFIG_SPACE_SIZE] { |
| 584 | let mut bytes = [0u8; crate::pci::CONFIG_SPACE_SIZE]; |
| 585 | bytes[0x00..0x02].copy_from_slice(&0x10deu16.to_le_bytes()); |
| 586 | bytes[0x02..0x04].copy_from_slice(&0x2e12u16.to_le_bytes()); |
| 587 | bytes[0x04..0x06].copy_from_slice(&0x0007u16.to_le_bytes()); |
| 588 | bytes[0x06..0x08].copy_from_slice(&0x0010u16.to_le_bytes()); |
| 589 | bytes[0x08] = 0xa1; |
| 590 | bytes[0x0b] = 0x03; |
| 591 | bytes[0x10..0x14].copy_from_slice(&0x2400_000cu32.to_le_bytes()); |
| 592 | bytes[0x14..0x18].copy_from_slice(&0x0000_0000u32.to_le_bytes()); |
| 593 | bytes[0x34] = 0x40; |
| 594 | bytes[0x40] = CAP_ID_MSI; |
| 595 | bytes[0x41] = 0x50; |
| 596 | bytes[0x42..0x44].copy_from_slice(&0x0388u16.to_le_bytes()); |
| 597 | bytes[0x50] = CAP_ID_PCIE; |
| 598 | bytes[0x51] = 0x80; |
| 599 | bytes[0x52..0x54].copy_from_slice(&0x0002u16.to_le_bytes()); |
| 600 | bytes[0x54..0x58].copy_from_slice(&0x0000_0003u32.to_le_bytes()); |
| 601 | bytes[0x5c..0x60].copy_from_slice(&0x0045_7901u32.to_le_bytes()); |
| 602 | bytes[0x62..0x64].copy_from_slice(&0x8010u16.to_le_bytes()); |
| 603 | bytes[0x80] = CAP_ID_MSIX; |
| 604 | bytes[0x81] = 0x00; |
| 605 | bytes[0x82..0x84].copy_from_slice(&0x8008u16.to_le_bytes()); |
| 606 | bytes[0x84..0x88].copy_from_slice(&0x00b9_0000u32.to_le_bytes()); |
| 607 | bytes[0x88..0x8c].copy_from_slice(&0x00ba_0000u32.to_le_bytes()); |
| 608 | bytes |
| 609 | } |
| 610 | |
| 611 | fn location() -> PciLocation { |
| 612 | PciLocation { |
| 613 | segment: 15, |
| 614 | bus: 1, |
| 615 | device: 0, |
| 616 | function: 0, |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | #[test] |
| 621 | fn profile_classifies_source_and_native_verification() { |
| 622 | let bytes = gb10_config(); |
| 623 | let linux = Gb10Profile::from_pci_config(location(), &bytes, Source::LinuxSysfs).unwrap(); |
| 624 | assert_eq!(linux.vendor_id.value(), Some(0x10de)); |
| 625 | assert_eq!(linux.vendor_id.source(), Some(Source::LinuxSysfs)); |
| 626 | assert!(!linux.vendor_id.native_verified()); |
| 627 | assert!(Source::LinuxSysfs.linux_interpreted()); |
| 628 | assert!(!Source::AienosNativePci.linux_interpreted()); |
| 629 | |
| 630 | let native = |
| 631 | Gb10Profile::from_pci_config(location(), &bytes, Source::AienosNativePci).unwrap(); |
| 632 | assert!(native.vendor_id.native_verified()); |
| 633 | assert_eq!(native.msix.value().unwrap().vectors, 9); |
| 634 | assert!(native.pcie.value().unwrap().link.unwrap().downgraded); |
| 635 | // Sources configuration space cannot answer stay unknown. |
| 636 | assert_eq!(native.iommu_group.state(), State::Unknown); |
| 637 | assert_eq!(native.numa_node.state(), State::Unknown); |
| 638 | assert_eq!(native.bar_sizes[0].state(), State::Unknown); |
| 639 | } |
| 640 | |
| 641 | #[test] |
| 642 | fn profile_renders_deterministically() { |
| 643 | extern crate std; |
| 644 | use std::string::String; |
| 645 | let bytes = gb10_config(); |
| 646 | let profile = |
| 647 | Gb10Profile::from_pci_config(location(), &bytes, Source::AienosNativePci).unwrap(); |
| 648 | let mut text = String::new(); |
| 649 | profile.write(&mut text); |
| 650 | assert!(text.contains( |
| 651 | "gb10.profile.vendor_id: 4318 [observed source=aienos-native-pci native_verified=true]" |
| 652 | )); |
| 653 | assert!(text.contains("gb10.profile.iommu_group: unknown")); |
| 654 | assert!(text.contains("gb10.profile.bar_size[0]: unknown")); |
| 655 | } |
| 656 | |
| 657 | #[test] |
| 658 | fn match_gb10_is_property_based_not_bdf_based() { |
| 659 | let bytes = gb10_config(); |
| 660 | let header = Header::parse(&bytes).unwrap(); |
| 661 | // Same device at a completely different location still matches. |
| 662 | let relocated = PciLocation { |
| 663 | segment: 0, |
| 664 | bus: 0x44, |
| 665 | device: 0x1f, |
| 666 | function: 7, |
| 667 | }; |
| 668 | let matched = match_gb10(relocated, &header).unwrap(); |
| 669 | assert_eq!(matched.location, relocated); |
| 670 | assert_eq!(matched.bar0.base, 0x2400_0000); |
| 671 | |
| 672 | let mut wrong_device = bytes; |
| 673 | wrong_device[0x02..0x04].copy_from_slice(&0x2684u16.to_le_bytes()); |
| 674 | assert_eq!( |
| 675 | match_gb10(location(), &Header::parse(&wrong_device).unwrap()), |
| 676 | Err(Gb10Rejection::WrongDevice) |
| 677 | ); |
| 678 | |
| 679 | let mut forbidden = bytes; |
| 680 | forbidden[0x00..0x02].copy_from_slice(&0x1234u16.to_le_bytes()); |
| 681 | assert_eq!( |
| 682 | match_gb10(location(), &Header::parse(&forbidden).unwrap()), |
| 683 | Err(Gb10Rejection::NotNvidia) |
| 684 | ); |
| 685 | |
| 686 | let mut no_mem = bytes; |
| 687 | no_mem[0x04..0x06].copy_from_slice(&0x0005u16.to_le_bytes()); |
| 688 | assert_eq!( |
| 689 | match_gb10(location(), &Header::parse(&no_mem).unwrap()), |
| 690 | Err(Gb10Rejection::MemoryDecodeDisabled) |
| 691 | ); |
| 692 | |
| 693 | let mut no_bar = bytes; |
| 694 | no_bar[0x10..0x14].copy_from_slice(&0u32.to_le_bytes()); |
| 695 | no_bar[0x14..0x18].copy_from_slice(&0u32.to_le_bytes()); |
| 696 | assert_eq!( |
| 697 | match_gb10(location(), &Header::parse(&no_bar).unwrap()), |
| 698 | Err(Gb10Rejection::Bar0Missing) |
| 699 | ); |
| 700 | |
| 701 | let mut io_bar = bytes; |
| 702 | io_bar[0x10..0x14].copy_from_slice(&0x0000_c001u32.to_le_bytes()); |
| 703 | assert_eq!( |
| 704 | match_gb10(location(), &Header::parse(&io_bar).unwrap()), |
| 705 | Err(Gb10Rejection::Bar0NotMemory) |
| 706 | ); |
| 707 | |
| 708 | let mut bar32 = bytes; |
| 709 | bar32[0x10..0x14].copy_from_slice(&0x2400_0000u32.to_le_bytes()); |
| 710 | bar32[0x14..0x18].copy_from_slice(&0u32.to_le_bytes()); |
| 711 | assert_eq!( |
| 712 | match_gb10(location(), &Header::parse(&bar32).unwrap()), |
| 713 | Err(Gb10Rejection::Bar0Not64Bit) |
| 714 | ); |
| 715 | } |
| 716 | } |