#!/usr/bin/env python3 """Generate phoebus-core's NVIDIA PCI device-ID -> generation table. The table is derived from the public PCI ID database (`pci.ids`), vendor `10de` (NVIDIA). Each entry's NVIDIA chip codename prefix (for example `TU117`, `GA104`, `AD102`, `GB202`) determines the hardware generation. Only display-class device IDs are ever looked up at runtime (the probe feeds the classifier the IDs of `VGA compatible controller` / `3D controller` / `Display controller` devices), so stray non-display entries in the database are harmless. Usage: scripts/generate-nvidia-devices.py [pci.ids] > nvidia_devices.rs With no argument the database is fetched from . The generated file is committed; it is never generated during a build. """ from __future__ import annotations import hashlib import re import sys import urllib.request PCI_IDS_URL = "https://pci-ids.ucw.cz/v2.2/pci.ids" # Codename prefix (first token of the PCI name) -> generation. The prefixes # are NVIDIA's public architecture codenames (Tegra parts share the same # families and are mapped accordingly). Ordered so the longest/most specific # prefixes are matched first. GENERATIONS: list[tuple[str, str]] = [ ("Fermi", "GF"), ("Kepler", "GK"), ("Maxwell", "GM"), ("Pascal", "GP"), ("Volta", "GV"), ("Turing", "TU"), ("Ampere", "GA"), ("Ada", "AD"), ("Hopper", "GH"), ("Blackwell", "GB"), ] CODENAME_RE = re.compile(r"^([A-Z]{2}\d{2,3}[A-Z]?)\b") def fetch(url: str) -> bytes: with urllib.request.urlopen(url, timeout=60) as resp: # noqa: S310 return resp.read() def nvidia_devices(text: str) -> list[tuple[int, str]]: lines = text.splitlines() start = None for i, line in enumerate(lines): if re.match(r"^10de\s", line): start = i break if start is None: raise SystemExit("NVIDIA vendor (10de) not found in pci.ids") devices: list[tuple[int, str]] = [] for line in lines[start + 1 :]: if re.match(r"^[0-9a-fA-F]{4}\s", line): break m = re.match(r"^\t([0-9a-fA-F]{4})\s+(.*)", line) if m: devices.append((int(m.group(1), 16), m.group(2).strip())) return devices def generation_for(name: str) -> str | None: m = CODENAME_RE.match(name) if not m: return None codename = m.group(1) for generation, prefix in GENERATIONS: if codename.startswith(prefix): return generation return None def main() -> int: if len(sys.argv) > 1: data = open(sys.argv[1], "rb").read() source = sys.argv[1] else: data = fetch(PCI_IDS_URL) source = PCI_IDS_URL sha = hashlib.sha256(data).hexdigest() devices = nvidia_devices(data.decode("utf-8", "replace")) buckets: dict[str, set[int]] = {} for device_id, name in devices: generation = generation_for(name) if generation: buckets.setdefault(generation, set()).add(device_id) # Detect an ID that would classify as two generations; the database does # not normally contain one, and a duplicate means the codename rule is # ambiguous and must be fixed here rather than silently resolved. seen: dict[int, str] = {} for generation, ids in buckets.items(): for device_id in ids: if device_id in seen: raise SystemExit( f"device {device_id:#06x} classified as both " f"{seen[device_id]} and {generation}" ) seen[device_id] = generation out = [] out.append("//! NVIDIA PCI device-ID classification table.") out.append("//!") out.append("//! GENERATED FILE - do not edit by hand. Regenerate with") out.append("//! `scripts/generate-nvidia-devices.py`. The table is derived") out.append("//! from the public PCI ID database (`pci.ids`, NVIDIA vendor `10de`)") out.append("//! by mapping each device's NVIDIA chip codename to its generation.") out.append("//!") out.append(f"//! Source: {source}") out.append(f"//! Source SHA-256: {sha}") out.append("") out.append("use super::nvidia::NvidiaGeneration;") out.append("") out.append("/// The PCI ID database revision this table was generated from.") out.append(f'pub const SOURCE_SHA256: &str = "{sha}";') out.append("") out.append("/// Classify an NVIDIA PCI device ID (the `device` half of the") out.append("/// `vendor:device` pair, fully qualified).") out.append("///") out.append("/// `None` means the database has no recognised NVIDIA chip codename for") out.append("/// the device: it is reported as unknown and never claimed as supported.") out.append("pub fn generation_for_device(device_id: u16) -> Option {") out.append(" use NvidiaGeneration::*;") out.append(" match device_id {") for generation, _ in GENERATIONS: ids = sorted(buckets.get(generation, set())) if not ids: continue joined = " | ".join(f"0x{i:04x}" for i in ids) out.append(f" {joined} => Some({generation}),") out.append(" _ => None,") out.append(" }") out.append("}") out.append("") out.append("#[cfg(test)]") out.append("mod tests {") out.append(" use super::*;") out.append("") out.append(" #[test]") out.append(" fn representative_devices_classify() {") out.append(" assert_eq!(generation_for_device(0x1380), Some(NvidiaGeneration::Maxwell));") out.append(" assert_eq!(generation_for_device(0x1b81), Some(NvidiaGeneration::Pascal));") out.append(" assert_eq!(generation_for_device(0x1d81), Some(NvidiaGeneration::Volta));") out.append(" assert_eq!(generation_for_device(0x1e04), Some(NvidiaGeneration::Turing));") out.append(" assert_eq!(generation_for_device(0x2482), Some(NvidiaGeneration::Ampere));") out.append(" assert_eq!(generation_for_device(0x2684), Some(NvidiaGeneration::Ada));") out.append(" assert_eq!(generation_for_device(0x2330), Some(NvidiaGeneration::Hopper));") out.append(" assert_eq!(generation_for_device(0x2b85), Some(NvidiaGeneration::Blackwell));") out.append(" assert_eq!(generation_for_device(0x0fc0), Some(NvidiaGeneration::Kepler));") out.append(" }") out.append("") out.append(" #[test]") out.append(" fn unknown_device_is_not_classified() {") out.append(" assert_eq!(generation_for_device(0x0000), None);") out.append(" }") out.append("}") sys.stdout.write("\n".join(out) + "\n") return 0 if __name__ == "__main__": raise SystemExit(main())