aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-inference-abi / src / pci_gpu.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 | //! GPU discovery from kernel sysfs (`/sys/bus/pci/devices`), read-only. |
| 17 | //! Replaces `nvidia-smi --query-gpu=name` probes: no NVIDIA userspace is run. |
| 18 | //! |
| 19 | //! MIGRATION-PENDING: Rust stop-gap. Its successor is a seeded-Omega (.om) |
| 20 | //! sysfs reader of the same files (PROPOSED, not yet written). Delete this |
| 21 | //! module when that lands. |
| 22 | |
| 23 | use std::fs; |
| 24 | use std::path::{Path, PathBuf}; |
| 25 | |
| 26 | pub const NVIDIA_VENDOR: u16 = 0x10de; |
| 27 | |
| 28 | /// PCI (vendor, device) IDs whose name has been checked against nvidia-smi |
| 29 | /// on real hardware. pci.ids does not list 10de:2e12. |
| 30 | const KNOWN_GPUS: &[(u16, u16, &str)] = &[(NVIDIA_VENDOR, 0x2e12, "NVIDIA GB10")]; |
| 31 | |
| 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 33 | pub struct PciGpu { |
| 34 | pub path: PathBuf, |
| 35 | pub vendor: u16, |
| 36 | pub device: u16, |
| 37 | /// Name of the bound kernel driver (`driver` symlink target), if any |
| 38 | /// can be read. |
| 39 | pub driver: Option<String>, |
| 40 | } |
| 41 | |
| 42 | impl PciGpu { |
| 43 | /// Checked name for known IDs, else `PCI vvvv:dddd`. Never guessed. |
| 44 | pub fn name(&self) -> String { |
| 45 | KNOWN_GPUS |
| 46 | .iter() |
| 47 | .find(|(v, d, _)| *v == self.vendor && *d == self.device) |
| 48 | .map(|(_, _, n)| (*n).to_string()) |
| 49 | .unwrap_or_else(|| format!("PCI {:04x}:{:04x}", self.vendor, self.device)) |
| 50 | } |
| 51 | |
| 52 | pub fn is_gb10(&self) -> bool { |
| 53 | self.vendor == NVIDIA_VENDOR && self.device == 0x2e12 |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// First driver-bound PCI display controller (class 0x03xxxx) under |
| 58 | /// `pci_devices`, NVIDIA first, then by PCI address so the choice is |
| 59 | /// deterministic. |
| 60 | pub fn find_display_gpu(pci_devices: &Path) -> Option<PciGpu> { |
| 61 | let mut found: Vec<PciGpu> = fs::read_dir(pci_devices) |
| 62 | .ok()? |
| 63 | .filter_map(|e| e.ok()) |
| 64 | .map(|e| e.path()) |
| 65 | .filter(|p| read_hex(&p.join("class")).is_some_and(|c| c >> 16 == 0x03)) |
| 66 | .filter(|p| p.join("driver").exists()) |
| 67 | .filter_map(|p| { |
| 68 | let vendor = u16::try_from(read_hex(&p.join("vendor"))?).ok()?; |
| 69 | let device = u16::try_from(read_hex(&p.join("device"))?).ok()?; |
| 70 | let driver = fs::read_link(p.join("driver")) |
| 71 | .ok() |
| 72 | .and_then(|l| l.file_name().map(|n| n.to_string_lossy().into_owned())); |
| 73 | Some(PciGpu { |
| 74 | path: p, |
| 75 | vendor, |
| 76 | device, |
| 77 | driver, |
| 78 | }) |
| 79 | }) |
| 80 | .collect(); |
| 81 | found.sort_by(|a, b| { |
| 82 | (a.vendor != NVIDIA_VENDOR, &a.path).cmp(&(b.vendor != NVIDIA_VENDOR, &b.path)) |
| 83 | }); |
| 84 | found.into_iter().next() |
| 85 | } |
| 86 | |
| 87 | fn read_hex(p: &Path) -> Option<u32> { |
| 88 | let s = fs::read_to_string(p).ok()?; |
| 89 | let s = s.trim(); |
| 90 | u32::from_str_radix(s.strip_prefix("0x").unwrap_or(s), 16).ok() |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use super::*; |
| 96 | use crate::ExecutionSurface; |
| 97 | use std::os::unix::fs::symlink; |
| 98 | |
| 99 | struct Tree(PathBuf); |
| 100 | impl Drop for Tree { |
| 101 | fn drop(&mut self) { |
| 102 | let _ = fs::remove_dir_all(&self.0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn tree(tag: &str) -> Tree { |
| 107 | let p = std::env::temp_dir().join(format!("pci_gpu-{}-{}", std::process::id(), tag)); |
| 108 | let _ = fs::remove_dir_all(&p); |
| 109 | fs::create_dir_all(p.join("drivers/nvidia")).unwrap(); |
| 110 | fs::create_dir_all(p.join("drivers/vfio-pci")).unwrap(); |
| 111 | fs::create_dir_all(p.join("devices")).unwrap(); |
| 112 | Tree(p) |
| 113 | } |
| 114 | |
| 115 | fn dev(t: &Tree, addr: &str, class: &str, vendor: &str, device: &str, driver: Option<&str>) { |
| 116 | let d = t.0.join("devices").join(addr); |
| 117 | fs::create_dir_all(&d).unwrap(); |
| 118 | fs::write(d.join("class"), format!("{class}\n")).unwrap(); |
| 119 | fs::write(d.join("vendor"), format!("{vendor}\n")).unwrap(); |
| 120 | fs::write(d.join("device"), format!("{device}\n")).unwrap(); |
| 121 | if let Some(drv) = driver { |
| 122 | symlink(t.0.join("drivers").join(drv), d.join("driver")).unwrap(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// DGX Spark as read on 2026-10-27: GB10 at 000f:01:00.0 bound to |
| 127 | /// `nvidia`, plus a bound non-display NIC. |
| 128 | fn spark(tag: &str) -> Tree { |
| 129 | let t = tree(tag); |
| 130 | dev( |
| 131 | &t, |
| 132 | "000f:01:00.0", |
| 133 | "0x030000", |
| 134 | "0x10de", |
| 135 | "0x2e12", |
| 136 | Some("nvidia"), |
| 137 | ); |
| 138 | dev( |
| 139 | &t, |
| 140 | "0000:01:00.0", |
| 141 | "0x020000", |
| 142 | "0x15b3", |
| 143 | "0x1021", |
| 144 | Some("vfio-pci"), |
| 145 | ); |
| 146 | t |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn spark_gb10_found_with_driver_name() { |
| 151 | let t = spark("found"); |
| 152 | let g = find_display_gpu(&t.0.join("devices")).unwrap(); |
| 153 | assert_eq!((g.vendor, g.device), (0x10de, 0x2e12)); |
| 154 | assert_eq!(g.driver.as_deref(), Some("nvidia")); |
| 155 | assert_eq!(g.name(), "NVIDIA GB10"); |
| 156 | assert!(g.is_gb10()); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn spark_surface_is_grace_blackwell() { |
| 161 | let t = spark("surface"); |
| 162 | assert_eq!( |
| 163 | ExecutionSurface::nvidia_surface(&t.0.join("devices")), |
| 164 | Some(ExecutionSurface::NvidiaGraceBlackwell { |
| 165 | model: "NVIDIA GB10".into(), |
| 166 | unified_memory_gb: 121, |
| 167 | }) |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn gpu_on_vfio_is_not_an_accelerated_surface() { |
| 173 | let t = tree("vfio"); |
| 174 | dev( |
| 175 | &t, |
| 176 | "000f:01:00.0", |
| 177 | "0x030000", |
| 178 | "0x10de", |
| 179 | "0x2e12", |
| 180 | Some("vfio-pci"), |
| 181 | ); |
| 182 | assert!(find_display_gpu(&t.0.join("devices")).is_some()); |
| 183 | assert_eq!(ExecutionSurface::nvidia_surface(&t.0.join("devices")), None); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn unbound_or_absent_gpu_is_none() { |
| 188 | let t = tree("unbound"); |
| 189 | dev(&t, "000f:01:00.0", "0x030000", "0x10de", "0x2e12", None); |
| 190 | assert_eq!(find_display_gpu(&t.0.join("devices")), None); |
| 191 | assert_eq!(find_display_gpu(&t.0.join("missing")), None); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn unknown_nvidia_gpu_has_unknown_vram_not_a_guess() { |
| 196 | let t = tree("unknown"); |
| 197 | dev( |
| 198 | &t, |
| 199 | "0000:41:00.0", |
| 200 | "0x030000", |
| 201 | "0x10de", |
| 202 | "0x2b85", |
| 203 | Some("nvidia"), |
| 204 | ); |
| 205 | let s = ExecutionSurface::nvidia_surface(&t.0.join("devices")).unwrap(); |
| 206 | assert_eq!( |
| 207 | s, |
| 208 | ExecutionSurface::NvidiaCuda { |
| 209 | gpu_name: "PCI 10de:2b85".into(), |
| 210 | vram_gb: None, |
| 211 | } |
| 212 | ); |
| 213 | assert_eq!(s.display_name(), "PCI 10de:2b85 (VRAM size unknown)"); |
| 214 | assert!(s.is_accelerated_gpu()); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn nvidia_preferred_over_lower_addressed_display_device() { |
| 219 | let t = spark("prefer"); |
| 220 | dev( |
| 221 | &t, |
| 222 | "0000:00:01.0", |
| 223 | "0x030000", |
| 224 | "0x1a03", |
| 225 | "0x2000", |
| 226 | Some("vfio-pci"), |
| 227 | ); |
| 228 | assert_eq!( |
| 229 | find_display_gpu(&t.0.join("devices")).unwrap().vendor, |
| 230 | NVIDIA_VENDOR |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | #[test] |
| 235 | fn detection_never_spawns_nvidia_smi() { |
| 236 | // Pieces are joined at run time so this test's own text cannot match. |
| 237 | let needle = ["Command::", "new(\"nvidia", "-smi\")"].concat(); |
| 238 | assert!(!include_str!("lib.rs").contains(&needle)); |
| 239 | assert!(!include_str!("pci_gpu.rs").contains(&["Command", "::new("].concat())); |
| 240 | } |
| 241 | } |