diff --git a/crates/aienos-accel/src/lib.rs b/crates/aienos-accel/src/lib.rs index fe548acb..c112d7a5 100644 --- a/crates/aienos-accel/src/lib.rs +++ b/crates/aienos-accel/src/lib.rs @@ -1,5 +1,7 @@ #![no_std] +use core::fmt::Write; + /// PCI identity observed for the GB10 GPU in the DGX Spark. pub const NVIDIA_VENDOR_ID: u16 = 0x10de; pub const GB10_DEVICE_ID: u16 = 0x2e12; @@ -58,9 +60,72 @@ pub struct Gb10Identity { pub pmc_boot_42: u32, } +/// Outcome for one PCI root bridge during GB10 discovery, carried into the +/// boot reports so a missed discovery says why. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RootBridgeOutcome { + /// The firmware refused the open of `PciRootBridgeIo` (status name). + OpenRefused(&'static str), + Scanned(RootBridgeScan), +} + +/// What the scan of one PCI root bridge observed. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct RootBridgeScan { + pub segment: u32, + pub devices: u16, + pub bridges: u16, + /// Bridges with a valid secondary..subordinate bus window. + pub windows: u16, + /// A GB10 vendor/device match that failed the BAR check: raw + /// command/status and BAR0 low/high, so the report shows the BAR state. + pub rejected_candidate: Option<(u32, u32, u32)>, +} + +/// Report lines explaining why discovery produced no GB10 identity: +/// bridges seen, opens refused, segments, bridge windows, and BAR state. +/// `handles` is the number of `PciRootBridgeIo` handles, if known. +pub fn write_discovery_diagnostics( + out: &mut impl Write, + handles: Option, + roots: &[Option], +) { + match handles { + Some(n) => { + let _ = writeln!(out, "gb10_pci_root_bridges: {n}"); + } + None => { + let _ = writeln!(out, "gb10_pci_root_bridges: lookup failed"); + return; + } + } + for (index, outcome) in roots.iter().flatten().enumerate() { + match outcome { + RootBridgeOutcome::OpenRefused(status) => { + let _ = writeln!(out, "gb10_pci_root_open[{index}]: refused ({status})"); + } + RootBridgeOutcome::Scanned(scan) => { + let _ = writeln!( + out, + "gb10_pci_root[{index}]: segment {} devices {} bridges {} windows {}", + scan.segment, scan.devices, scan.bridges, scan.windows + ); + if let Some((command_status, bar0_low, bar0_high)) = scan.rejected_candidate { + let _ = writeln!( + out, + "gb10_pci_candidate[{index}]: command_status {command_status:#010x} bar0 {bar0_high:#010x}:{bar0_low:#010x}" + ); + } + } + } + } +} + #[cfg(test)] mod tests { use super::*; + extern crate std; + use std::string::String; #[test] fn validates_gb10_memory_bar() { @@ -76,4 +141,37 @@ mod tests { assert!(Gb10Bar::from_config(location, 0x2e12_10de, 0x4, 0x2400_0004, 0).is_none()); assert!(Gb10Bar::from_config(location, 0x2e12_10de, 0x6, 0x2400_0000, 0).is_none()); } + + #[test] + fn diagnostics_report_refused_opens_segments_and_bar_state() { + let roots = [ + Some(RootBridgeOutcome::OpenRefused("ACCESS_DENIED")), + Some(RootBridgeOutcome::Scanned(RootBridgeScan { + segment: 15, + devices: 4, + bridges: 1, + windows: 1, + rejected_candidate: Some((0x10_0004, 0, 0)), + })), + None, + ]; + let mut text = String::new(); + write_discovery_diagnostics(&mut text, Some(2), &roots); + for line in [ + "gb10_pci_root_bridges: 2", + "gb10_pci_root_open[0]: refused (ACCESS_DENIED)", + "gb10_pci_root[1]: segment 15 devices 4 bridges 1 windows 1", + "gb10_pci_candidate[1]: command_status 0x00100004 bar0 0x00000000:0x00000000", + ] { + assert!(text.lines().any(|l| l == line), "missing {line:?} in:\n{text}"); + } + + let mut text = String::new(); + write_discovery_diagnostics(&mut text, None, &[None]); + assert_eq!(text, "gb10_pci_root_bridges: lookup failed\n"); + + let mut text = String::new(); + write_discovery_diagnostics(&mut text, Some(0), &[None]); + assert_eq!(text, "gb10_pci_root_bridges: 0\n"); + } } diff --git a/crates/aienos-boot/src/handoff.rs b/crates/aienos-boot/src/handoff.rs index bc0b3571..68f02dc8 100644 --- a/crates/aienos-boot/src/handoff.rs +++ b/crates/aienos-boot/src/handoff.rs @@ -38,6 +38,7 @@ use uefi::mem::memory_map::{MemoryMap, MemoryType}; use uefi::prelude::*; use uefi::proto::console::gop::{GraphicsOutput, PixelFormat}; use uefi::proto::media::file::{File, FileAttribute, FileMode}; +use uefi::proto::pci::configuration::ResourceRangeType; use uefi::proto::pci::root_bridge::PciRootBridgeIo; use uefi::proto::pci::PciIoAddress; use uefi::runtime::{ResetType, VariableAttributes, VariableVendor}; @@ -140,10 +141,55 @@ fn save_report_var(bytes: &[u8]) -> Result { .map_err(|_| "firmware refused the write") } -fn probe_gb10( +type BridgeScan = aienos_accel::RootBridgeScan; +type RootOutcome = aienos_accel::RootBridgeOutcome; + +/// Outcome of pre-exit GB10 discovery: the identity when found, plus a +/// bounded record of why it was not (bridges seen, opens refused, segments, +/// BAR state) so the pre-exit and final reports can say what happened. +struct Gb10Discovery { + identity: Option, + /// Number of handles carrying the `PciRootBridgeIo` protocol, when the + /// handle database answered. + handles: Option, + roots: [Option; MAX_ROOT_BRIDGES], +} + +/// At most this many root bridges are recorded; extras are still searched. +const MAX_ROOT_BRIDGES: usize = 4; + +/// Compact status names for the report (no `Debug` tables in the image). +fn status_name(status: Status) -> &'static str { + match status { + Status::ACCESS_DENIED => "ACCESS_DENIED", + Status::NOT_FOUND => "NOT_FOUND", + Status::INVALID_PARAMETER => "INVALID_PARAMETER", + Status::OUT_OF_RESOURCES => "OUT_OF_RESOURCES", + Status::UNSUPPORTED => "UNSUPPORTED", + _ => "OTHER", + } +} + +impl Gb10Discovery { + /// Record the outcome of one root bridge, bounded by `MAX_ROOT_BRIDGES`. + fn record(&mut self, outcome: RootOutcome) { + if let Some(slot) = self.roots.iter_mut().find(|s| s.is_none()) { + *slot = Some(outcome); + } + } + + /// Report lines explaining why no GB10 identity was produced. + fn write_diagnostics(&self, out: &mut Report) { + aienos_accel::write_discovery_diagnostics(out, self.handles, &self.roots); + } +} + +/// Read the config registers needed for the BAR check. `None` when the +/// vendor/device pair does not match the GB10 or a config read failed. +fn read_candidate( root: &mut PciRootBridgeIo, address: PciIoAddress, -) -> Option { +) -> Option<(u32, u32, u32, u32)> { let config = |offset| address.with_register(offset); let vendor_device = root.pci().read_one::(config(0)).ok()?; if vendor_device != 0x2e12_10de { @@ -152,7 +198,16 @@ fn probe_gb10( let command_status = root.pci().read_one::(config(4)).ok()?; let bar0_low = root.pci().read_one::(config(0x10)).ok()?; let bar0_high = root.pci().read_one::(config(0x14)).ok()?; - let bar = aienos_accel::Gb10Bar::from_config( + Some((vendor_device, command_status, bar0_low, bar0_high)) +} + +fn probe_gb10( + root: &mut PciRootBridgeIo, + address: PciIoAddress, + scan: &mut BridgeScan, +) -> Option { + let (vendor_device, command_status, bar0_low, bar0_high) = read_candidate(root, address)?; + let bar = match aienos_accel::Gb10Bar::from_config( aienos_accel::PciLocation { segment: root.segment_nr(), bus: address.bus, @@ -163,7 +218,15 @@ fn probe_gb10( command_status, bar0_low, bar0_high, - )?; + ) { + Some(bar) => bar, + None => { + // The GB10 was found but its BAR or command register was not + // usable; keep the raw values so the report shows the BAR state. + scan.rejected_candidate = Some((command_status, bar0_low, bar0_high)); + return None; + } + }; let pmc_boot_0 = root .memory() .read_one::(bar.physical_base + aienos_accel::PMC_BOOT_0) @@ -179,31 +242,138 @@ fn probe_gb10( }) } -fn discover_gb10() -> Option { - let handles = uefi::boot::find_handles::().ok()?; - for handle in handles { - let Ok(mut root) = uefi::boot::open_protocol_exclusive::(handle) else { - continue; - }; - // Config A puts GB10 at segment 15, bus 1, device 0, function 0. - // Try that first so the common boot path does not enumerate every bus. - if root.segment_nr() == 15 { - if let Some(identity) = probe_gb10(&mut root, PciIoAddress::new(1, 0, 0)) { - return Some(identity); +/// Walk every function on one bus of this root bridge. +/// Returns the GB10 identity when the GB10 is found here. +fn scan_bus(root: &mut PciRootBridgeIo, bus: u8, scan: &mut BridgeScan) -> Option { + for dev in 0..32u8 { + for fun in 0..8u8 { + let address = PciIoAddress::new(bus, dev, fun); + let config = |offset| address.with_register(offset); + let Ok(vendor_device) = root.pci().read_one::(config(0)) else { + continue; + }; + if vendor_device & 0xffff == 0xffff { + if fun == 0 { + break; // no function 0: no further functions on this device + } + continue; } - } - let Ok(tree) = root.enumerate() else { - continue; - }; - for address in tree { - if let Some(identity) = probe_gb10(&mut root, address) { - return Some(identity); + scan.devices += 1; + if vendor_device == 0x2e12_10de { + if let Some(identity) = probe_gb10(root, address, scan) { + return Some(identity); + } + } + let header_type = root + .pci() + .read_one::(config(0x0c)) + .map(|v| ((v >> 16) & 0xff) as u8) + .unwrap_or(0); + if header_type & 0x7f == 0x01 { + scan.bridges += 1; + let window = root + .pci() + .read_one::(config(0x18)) + .map(|v| (((v >> 8) & 0xff) as u8, ((v >> 16) & 0xff) as u8)) + .unwrap_or((0, 0)); + if window.0 != 0 && window.1 >= window.0 { + scan.windows += 1; + } + } + if fun == 0 && header_type & 0x80 == 0 { + break; // single-function device: skip functions 1..8 } } } None } +/// Read the bus-range resource descriptors of this root bridge, or `None`. +fn bus_ranges(root: &mut PciRootBridgeIo) -> Option<(u8, u8)> { + let mut range: Option<(u8, u8)> = None; + for descriptor in root.configuration().ok()? { + if descriptor.resource_range_type == ResourceRangeType::Bus { + let min = u8::try_from(descriptor.address_min).unwrap_or(u8::MAX); + let max = u8::try_from(descriptor.address_max).unwrap_or(u8::MAX); + range = Some(match range { + Some((lo, hi)) => (lo.min(min), hi.max(max)), + None => (min, max), + }); + } + } + range +} + +/// Scan one root bridge. Returns the GB10 identity and the outcome to record. +fn scan_root(root: &mut PciRootBridgeIo) -> (Option, RootOutcome) { + let mut scan = BridgeScan { + segment: root.segment_nr(), + devices: 0, + bridges: 0, + windows: 0, + rejected_candidate: None, + }; + // Config A puts GB10 at segment 15, bus 1, device 0, function 0. + // Try that first so the common boot path does not walk every bus. + if scan.segment == 15 { + if let Some(identity) = probe_gb10(root, PciIoAddress::new(1, 0, 0), &mut scan) { + return (Some(identity), RootOutcome::Scanned(scan)); + } + } + // No firmware bus descriptors: fall back to the root bus so the scan + // still sees devices whose bus numbers firmware never published. + let (bus_min, bus_max) = bus_ranges(root).unwrap_or((0, 0)); + for bus in bus_min..=bus_max { + if let Some(identity) = scan_bus(root, bus, &mut scan) { + return (Some(identity), RootOutcome::Scanned(scan)); + } + } + (None, RootOutcome::Scanned(scan)) +} + +fn discover_gb10() -> Gb10Discovery { + let mut discovery = Gb10Discovery { + identity: None, + handles: None, + roots: [const { None }; MAX_ROOT_BRIDGES], + }; + let handles = match uefi::boot::find_handles::() { + Ok(handles) => handles, + Err(_) => return discovery, + }; + discovery.handles = Some(u16::try_from(handles.len()).unwrap_or(u16::MAX)); + for handle in handles { + // Shared (GetProtocol) open: the firmware PCI bus driver holds this + // protocol open ByDriver, so an exclusive open is refused with + // ACCESS_DENIED. Shared reads are safe here: this boot code only + // reads through the protocol, and firmware keeps the protocol + // installed until boot services are exited. + let mut root = match unsafe { + uefi::boot::open_protocol::( + OpenProtocolParams { + handle, + agent: uefi::boot::image_handle(), + controller: None, + }, + OpenProtocolAttributes::GetProtocol, + ) + } { + Ok(root) => root, + Err(e) => { + discovery.record(RootOutcome::OpenRefused(status_name(e.status()))); + continue; + } + }; + let (found, outcome) = scan_root(&mut root); + discovery.record(outcome); + if let Some(identity) = found { + discovery.identity = Some(identity); + return discovery; + } + } + discovery +} + /// Reads a firmware ACPI table whose header is at `addr`. /// /// # Safety @@ -523,7 +693,8 @@ fn main() -> Status { stage(FIRMWARE_ENTRY); stage(GB10_DISCOVERY); - let gb10 = discover_gb10(); + let gb10_discovery = discover_gb10(); + let gb10 = gb10_discovery.identity; stage(ACPI_TOPOLOGY); let boot_midr = midr_el1(); @@ -565,6 +736,7 @@ fn main() -> Status { } None => { let _ = writeln!(pre, "gb10: unavailable"); + gb10_discovery.write_diagnostics(&mut pre); } } write_cpu(&mut pre, cpu, boot_midr, boot_mpidr); @@ -636,6 +808,9 @@ fn main() -> Status { header(&mut report, "final"); write_index_line(&mut report); let _ = write!(report, "{}", kernel_report.as_str()); + if gb10.is_none() { + gb10_discovery.write_diagnostics(&mut report); + } match progress_saved { Ok(_) => { let _ = writeln!(report, "progress_record: saved"); diff --git a/docs/GB10_NATIVE_STATUS.md b/docs/GB10_NATIVE_STATUS.md index 2a0fb184..b103d4f0 100644 --- a/docs/GB10_NATIVE_STATUS.md +++ b/docs/GB10_NATIVE_STATUS.md @@ -1,16 +1,32 @@ # GB10 native execution status The `aienos-handoff` AArch64 UEFI image now checks the DGX Spark's observed -GB10 PCI location first, then searches other root bridges if needed. It accepts -only NVIDIA `10de:2e12` with a programmed 64-bit memory BAR and PCI memory +GB10 PCI location first, then walks every bus in each root bridge's +firmware-declared bus range. It opens the UEFI PCI root bridge protocol with a +shared (`GetProtocol`) open. The leading hypothesis for the first native +boot's `gb10: unavailable` is that the firmware PCI bus driver holds the +protocol `ByDriver` and refuses an exclusive open with `ACCESS_DENIED`; this is +unconfirmed on hardware, and the next native boot's `gb10_pci_root_open[..]` +lines will confirm or rule it out. It accepts only +NVIDIA `10de:2e12` with a programmed 64-bit memory BAR and PCI memory decoding enabled. Through the UEFI PCI root bridge protocol, it reads the read-only PMC `BOOT_0` and `BOOT_42` registers from BAR0. It carries the location, BAR address, and register values across `ExitBootServices` into the AIENOS kernel's serial boot report. No Linux service is involved in that image. -This image has been built but **has not been booted on hardware**. These -register reads still use firmware services before handoff. They establish a -device identity path; they do not initialize the GPU or execute a GPU command. +When the GB10 is not found, the pre-exit report and the final +firmware-variable report record why: how many root bridges carry the protocol +(`gb10_pci_root_bridges`), any refused opens (`gb10_pci_root_open[i]`), and +per scanned bridge the segment number, device count, bridge count, and valid +bridge bus windows (`gb10_pci_root[i]`). If the GB10 vendor/device pair was +seen but rejected, its command/status and BAR0 values are recorded +(`gb10_pci_candidate[i]`), distinguishing an unprogrammed BAR from a missed +match. + +This image has been built but the fixed discovery path **has not been booted +on hardware**. These register reads still use firmware services before +handoff. They establish a device identity path; they do not initialize the +GPU or execute a GPU command. To reach independent GPU execution, AIENOS still needs a native MMU mapping for GPU MMIO, GPU resource and interrupt management, GB10/GSP firmware loading and