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 / tests / gb10_fixtures.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 | //! Synthetic PCI configuration fixtures for GB10 discovery. |
| 17 | //! |
| 18 | //! These tests prove that discovery identifies the GB10 from device |
| 19 | //! properties, not from a hard-coded bus/device/function. They use only |
| 20 | //! in-memory byte fixtures and pure decoders: no physical device, no PCI |
| 21 | //! configuration write, no MMIO, no GPU execution. |
| 22 | |
| 23 | use aienos_accel::pci::{ |
| 24 | BridgeHeader, Header, MsiCapability, MsixCapability, PciError, PcieCapability, CAP_ID_MSI, |
| 25 | CAP_ID_MSIX, CAP_ID_PCIE, CONFIG_EXTENDED_SIZE, CONFIG_SPACE_SIZE, |
| 26 | }; |
| 27 | use aienos_accel::profile::{match_gb10, Gb10Profile, Gb10Rejection, Source}; |
| 28 | use aienos_accel::PciLocation; |
| 29 | |
| 30 | const CONFIG_HEADER: usize = 64; |
| 31 | |
| 32 | fn location(segment: u32, bus: u8, device: u8, function: u8) -> PciLocation { |
| 33 | PciLocation { |
| 34 | segment, |
| 35 | bus, |
| 36 | device, |
| 37 | function, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn write_u16(bytes: &mut [u8], offset: usize, value: u16) { |
| 42 | bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes()); |
| 43 | } |
| 44 | |
| 45 | fn write_u32(bytes: &mut [u8], offset: usize, value: u32) { |
| 46 | bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); |
| 47 | } |
| 48 | |
| 49 | /// A GB10-like endpoint: NVIDIA 10de:2e12, 64-bit prefetchable BAR0, PCIe, |
| 50 | /// MSI and MSI-X capabilities. |
| 51 | fn gb10_endpoint() -> [u8; CONFIG_SPACE_SIZE] { |
| 52 | let mut bytes = [0u8; CONFIG_SPACE_SIZE]; |
| 53 | write_u16(&mut bytes, 0x00, 0x10de); |
| 54 | write_u16(&mut bytes, 0x02, 0x2e12); |
| 55 | write_u16(&mut bytes, 0x04, 0x0007); // IO + memory + bus master |
| 56 | write_u16(&mut bytes, 0x06, 0x0010); // capabilities list present |
| 57 | bytes[0x08] = 0xa1; // revision |
| 58 | bytes[0x0b] = 0x03; // class: display controller |
| 59 | write_u32(&mut bytes, 0x10, 0x2400_000c); // BAR0 low: 64-bit prefetchable |
| 60 | write_u32(&mut bytes, 0x14, 0x0000_0000); // BAR0 high |
| 61 | write_u16(&mut bytes, 0x2c, 0x10de); // subsystem vendor |
| 62 | write_u16(&mut bytes, 0x2e, 0x0000); // subsystem id |
| 63 | bytes[0x34] = 0x40; // capability pointer |
| 64 | |
| 65 | bytes[0x40] = CAP_ID_MSI; |
| 66 | bytes[0x41] = 0x50; |
| 67 | write_u16(&mut bytes, 0x42, 0x0388); // 64-bit, maskable, 16 vectors |
| 68 | write_u32(&mut bytes, 0x44, 0xfee0_0000); |
| 69 | |
| 70 | bytes[0x50] = CAP_ID_PCIE; |
| 71 | bytes[0x51] = 0x80; |
| 72 | write_u16(&mut bytes, 0x52, 0x0002); // version 2, endpoint |
| 73 | write_u32(&mut bytes, 0x54, 0x0000_0003); // max payload 1024 |
| 74 | write_u32(&mut bytes, 0x5c, 0x0045_7901); // max speed gen1, width x16 |
| 75 | write_u16(&mut bytes, 0x62, 0x8010); // current speed gen1, width x1 |
| 76 | |
| 77 | bytes[0x80] = CAP_ID_MSIX; |
| 78 | bytes[0x81] = 0x00; |
| 79 | write_u16(&mut bytes, 0x82, 0x8008); // enabled, 9 vectors |
| 80 | write_u32(&mut bytes, 0x84, 0x00b9_0000); // table in BAR0 |
| 81 | write_u32(&mut bytes, 0x88, 0x00ba_0000); // PBA in BAR0 |
| 82 | |
| 83 | bytes |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn identifies_gb10_from_properties_under_bdf_relocation() { |
| 88 | let bytes = gb10_endpoint(); |
| 89 | let header = Header::parse(&bytes).unwrap(); |
| 90 | let locations = [ |
| 91 | location(15, 1, 0, 0), |
| 92 | location(0, 0, 0, 0), |
| 93 | location(3, 0x44, 0x1f, 7), |
| 94 | location(0xffff, 0xff, 0x1f, 7), |
| 95 | ]; |
| 96 | for relocated in locations { |
| 97 | let matched = match_gb10(relocated, &header).unwrap(); |
| 98 | assert_eq!(matched.location, relocated); |
| 99 | assert_eq!(matched.bar0.base, 0x2400_0000); |
| 100 | assert!(matched.bar0.is_64_bit); |
| 101 | assert!(matched.bar0.prefetchable); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn rejects_wrong_vendor_and_wrong_device() { |
| 107 | let mut wrong_device = gb10_endpoint(); |
| 108 | write_u16(&mut wrong_device, 0x02, 0x2684); |
| 109 | assert_eq!( |
| 110 | match_gb10( |
| 111 | location(15, 1, 0, 0), |
| 112 | &Header::parse(&wrong_device).unwrap() |
| 113 | ), |
| 114 | Err(Gb10Rejection::WrongDevice) |
| 115 | ); |
| 116 | |
| 117 | let mut wrong_vendor = gb10_endpoint(); |
| 118 | write_u16(&mut wrong_vendor, 0x00, 0x1234); |
| 119 | assert_eq!( |
| 120 | match_gb10( |
| 121 | location(15, 1, 0, 0), |
| 122 | &Header::parse(&wrong_vendor).unwrap() |
| 123 | ), |
| 124 | Err(Gb10Rejection::NotNvidia) |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn rejects_disabled_memory_decoding_unprogrammed_and_32_bit_bar() { |
| 130 | let mut no_mem = gb10_endpoint(); |
| 131 | write_u16(&mut no_mem, 0x04, 0x0005); // memory bit clear |
| 132 | assert_eq!( |
| 133 | match_gb10(location(15, 1, 0, 0), &Header::parse(&no_mem).unwrap()), |
| 134 | Err(Gb10Rejection::MemoryDecodeDisabled) |
| 135 | ); |
| 136 | |
| 137 | let mut unprogrammed = gb10_endpoint(); |
| 138 | write_u32(&mut unprogrammed, 0x10, 0x0000_0000); |
| 139 | write_u32(&mut unprogrammed, 0x14, 0x0000_0000); |
| 140 | assert_eq!( |
| 141 | match_gb10( |
| 142 | location(15, 1, 0, 0), |
| 143 | &Header::parse(&unprogrammed).unwrap() |
| 144 | ), |
| 145 | Err(Gb10Rejection::Bar0Missing) |
| 146 | ); |
| 147 | |
| 148 | let mut only_high = gb10_endpoint(); |
| 149 | write_u32(&mut only_high, 0x10, 0x0000_0004); |
| 150 | write_u32(&mut only_high, 0x14, 0x0000_0000); |
| 151 | assert_eq!( |
| 152 | match_gb10(location(15, 1, 0, 0), &Header::parse(&only_high).unwrap()), |
| 153 | Err(Gb10Rejection::Bar0Unprogrammed) |
| 154 | ); |
| 155 | |
| 156 | let mut bar32 = gb10_endpoint(); |
| 157 | write_u32(&mut bar32, 0x10, 0x2400_0000); // 32-bit memory BAR |
| 158 | write_u32(&mut bar32, 0x14, 0x0000_0000); |
| 159 | assert_eq!( |
| 160 | match_gb10(location(15, 1, 0, 0), &Header::parse(&bar32).unwrap()), |
| 161 | Err(Gb10Rejection::Bar0Not64Bit) |
| 162 | ); |
| 163 | |
| 164 | let mut io_bar = gb10_endpoint(); |
| 165 | write_u32(&mut io_bar, 0x10, 0x0000_c001); |
| 166 | assert_eq!( |
| 167 | match_gb10(location(15, 1, 0, 0), &Header::parse(&io_bar).unwrap()), |
| 168 | Err(Gb10Rejection::Bar0NotMemory) |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn selects_the_gb10_among_multiple_nvidia_devices() { |
| 174 | // A fixture set with a wrong NVIDIA function and the GB10. Only the GB10 |
| 175 | // must satisfy the contract. |
| 176 | let mut other = gb10_endpoint(); |
| 177 | write_u16(&mut other, 0x02, 0x2684); |
| 178 | let gb10 = gb10_endpoint(); |
| 179 | let candidates = [ |
| 180 | Header::parse(&other).unwrap(), |
| 181 | Header::parse(&gb10).unwrap(), |
| 182 | ]; |
| 183 | let matches: Vec<_> = candidates |
| 184 | .iter() |
| 185 | .filter_map(|header| match_gb10(location(15, 1, 0, 0), header).ok()) |
| 186 | .collect(); |
| 187 | assert_eq!(matches.len(), 1); |
| 188 | assert_eq!(matches[0].bar0.base, 0x2400_0000); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn follows_a_secondary_bus_window_to_a_downstream_gb10() { |
| 193 | // Root bridge bus 0, bridge secondary 1..0x0f, GB10 behind bus 1. |
| 194 | let mut bridge = [0u8; CONFIG_HEADER]; |
| 195 | write_u16(&mut bridge, 0x00, 0x10de); |
| 196 | write_u16(&mut bridge, 0x02, 0x22d1); |
| 197 | bridge[0x0b] = 0x06; // bridge class |
| 198 | bridge[0x0e] = 0x01; // type 1 header |
| 199 | bridge[0x19] = 0x01; // secondary bus |
| 200 | bridge[0x1a] = 0x0f; // subordinate bus |
| 201 | let bridge = BridgeHeader::parse(&bridge).unwrap(); |
| 202 | assert!(bridge.has_bus_window()); |
| 203 | assert!(bridge.secondary_bus <= 1 && 1 <= bridge.subordinate_bus); |
| 204 | |
| 205 | let downstream = gb10_endpoint(); |
| 206 | let on_bus_one = match_gb10(location(15, 1, 0, 0), &Header::parse(&downstream).unwrap()); |
| 207 | assert!(on_bus_one.is_ok()); |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn rejects_malformed_capability_loops() { |
| 212 | let mut bytes = gb10_endpoint(); |
| 213 | bytes[0x41] = 0x40; // MSI next points at itself |
| 214 | assert_eq!( |
| 215 | aienos_accel::pci::walk_capabilities(&bytes), |
| 216 | Err(PciError::CapabilityLoop) |
| 217 | ); |
| 218 | |
| 219 | let mut bytes = gb10_endpoint(); |
| 220 | bytes[0x34] = 0x40; |
| 221 | bytes[0x40] = CAP_ID_MSI; |
| 222 | bytes[0x41] = 0x30; // below 0x40 header boundary |
| 223 | assert_eq!( |
| 224 | aienos_accel::pci::walk_capabilities(&bytes), |
| 225 | Err(PciError::OutOfBounds) |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn reports_missing_msi_and_msix_as_absent_not_zero() { |
| 231 | // Skip the MSI capability: the header pointer goes straight to PCIe. |
| 232 | let mut no_msi = gb10_endpoint(); |
| 233 | no_msi[0x34] = 0x50; |
| 234 | let list = aienos_accel::pci::walk_capabilities(&no_msi).unwrap(); |
| 235 | assert!(list.find(CAP_ID_MSI).is_none()); |
| 236 | assert!(list.find(CAP_ID_PCIE).is_some()); |
| 237 | let profile = |
| 238 | Gb10Profile::from_pci_config(location(15, 1, 0, 0), &no_msi, Source::SyntheticFixture) |
| 239 | .unwrap(); |
| 240 | assert!(!profile.msi.value().unwrap().present); |
| 241 | assert_eq!(profile.msix.value().unwrap().vectors, 9); |
| 242 | |
| 243 | // Drop MSI-X: PCIe becomes the last capability in the chain. |
| 244 | let mut no_msix = gb10_endpoint(); |
| 245 | no_msix[0x51] = 0x00; |
| 246 | let profile = |
| 247 | Gb10Profile::from_pci_config(location(15, 1, 0, 0), &no_msix, Source::SyntheticFixture) |
| 248 | .unwrap(); |
| 249 | assert!(!profile.msix.value().unwrap().present); |
| 250 | assert!(profile.msi.value().unwrap().present); |
| 251 | assert_eq!(profile.msix.value().unwrap().vectors, 0); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn decodes_capabilities_from_full_config_space() { |
| 256 | let bytes = gb10_endpoint(); |
| 257 | let list = aienos_accel::pci::walk_capabilities(&bytes).unwrap(); |
| 258 | assert_eq!(list.entries().len(), 3); |
| 259 | |
| 260 | let msi = MsiCapability::decode(&bytes, list.find(CAP_ID_MSI).unwrap()).unwrap(); |
| 261 | assert!(msi.is_64_bit && msi.maskable); |
| 262 | assert_eq!(msi.capable_vectors, 16); |
| 263 | |
| 264 | let pcie = PcieCapability::decode(&bytes, list.find(CAP_ID_PCIE).unwrap()).unwrap(); |
| 265 | let link = pcie.link.unwrap(); |
| 266 | assert_eq!(link.max_width, 16); |
| 267 | assert_eq!(link.current_width, 1); |
| 268 | assert!(link.downgraded); |
| 269 | assert_eq!(pcie.max_payload_bytes, 1024); |
| 270 | |
| 271 | let msix = MsixCapability::decode(&bytes, list.find(CAP_ID_MSIX).unwrap()).unwrap(); |
| 272 | assert!(msix.enabled); |
| 273 | assert_eq!(msix.vectors, 9); |
| 274 | assert_eq!(msix.table_bir, 0); |
| 275 | assert_eq!(msix.pba_offset, 0x00ba_0000); |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn handles_large_bar_and_extended_space_bounds() { |
| 280 | // A 64 GB BAR: probe readback low 0x00000004, high 0xFFFFFFF0. |
| 281 | let size = aienos_accel::pci::memory_bar_size(0x0000_0004, 0xffff_fff0, true); |
| 282 | assert_eq!(size, Some(0x10_0000_0000)); |
| 283 | |
| 284 | // Extended capability space: a valid chain and a loop. |
| 285 | let mut extended = [0u8; CONFIG_EXTENDED_SIZE]; |
| 286 | let first = 0x19u32 | (1 << 16) | (0x12cu32 << 20); |
| 287 | write_u32(&mut extended, 0x100, first); |
| 288 | let second = 0x01u32 | (2 << 16); |
| 289 | write_u32(&mut extended, 0x12c, second); |
| 290 | let list = aienos_accel::pci::walk_extended_capabilities(&extended).unwrap(); |
| 291 | assert_eq!(list.entries().len(), 2); |
| 292 | |
| 293 | let mut looped = [0u8; CONFIG_EXTENDED_SIZE]; |
| 294 | write_u32(&mut looped, 0x100, 0x19u32 | (0x100u32 << 20)); |
| 295 | assert_eq!( |
| 296 | aienos_accel::pci::walk_extended_capabilities(&looped), |
| 297 | Err(PciError::CapabilityLoop) |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn profile_decodes_bar_kinds_and_keeps_sizes_unknown() { |
| 303 | let bytes = gb10_endpoint(); |
| 304 | let profile = |
| 305 | Gb10Profile::from_pci_config(location(15, 1, 0, 0), &bytes, Source::SyntheticFixture) |
| 306 | .unwrap(); |
| 307 | let bar0 = profile.bars[0].value().unwrap(); |
| 308 | assert_eq!(bar0.kind, aienos_accel::profile::BarKind::Memory); |
| 309 | assert!(bar0.is_64_bit); |
| 310 | assert!(bar0.prefetchable); |
| 311 | assert_eq!(bar0.base, 0x2400_0000); |
| 312 | // Size requires a probe write, so it must stay unknown. |
| 313 | for size in profile.bar_sizes { |
| 314 | assert!(!size.is_known()); |
| 315 | } |
| 316 | // A 64-bit BAR consumed slot 1; it is not an independent BAR. |
| 317 | assert_eq!( |
| 318 | profile.bars[1].value().unwrap().kind, |
| 319 | aienos_accel::profile::BarKind::None |
| 320 | ); |
| 321 | } |