From ae09b121dfd9d538af87ce9804e5c8f7ed84d29a Mon Sep 17 00:00:00 2001 From: AutoJanitor Date: Fri, 4 Dec 2026 18:17:10 -0600 Subject: [PATCH] gv100/hbm: log every HBM2 session outcome, not only a verified unlock (#451) * gv100/hbm: log every HBM2 session outcome, not only a verified unlock On a CMP 100-210 (10de:1df4, 12 GB) FBPA 0..=3 answer with a PRI bus error, so survey_gv100_hbm() refuses the card. HbmSession::begin() then returns FailedRolledBack in state SurveyFailed, and pmu_bringup only logged the Unlocked case: v100_hbm_ndiv=82 did nothing and dmesg said nothing. The session is write-inhibited from then on, so a later sysfs write to v100_hbm_ndiv was dropped without a line as well. A PLL that would not take the new NDIV and was rolled back was equally silent, and indistinguishable from the survey failure in HbmUnlockStatus. Core (hbm_clock.rs): - survey_gv100_hbm_checked() does the same reads in the same order as survey_gv100_hbm() and returns the first failed check as an HbmSurveyFault { kind, fbpa, value }: canary mismatch, cfg or coeff unreadable, PLL not locked, coeff mismatch. survey_gv100_hbm() is now its .ok(); HbmSession keeps the fault of the start survey. - HbmSession::outcome() names what a start or live step did: Disabled, NonStockPll, AlreadyAtTarget, Unlocked, SurveyFailed, RolledBack, ClockUnknown, WriteInhibited. Kernel glue (pmu_bringup.rs, gpu_owner.c): every start outcome other than Unlocked, every live step that does not end in a verified new clock, and a sysfs write dropped by a write-inhibited session now go to nv_rs_hbm_clock_outcome_log(). The survey line carries the partition and raw word, e.g. "survey failed (FBPA 0 PLL config unreadable (0xbadf2010))". Requested-but-failed outcomes are pr_warn, an unverified rollback is pr_err, the rest pr_info. The existing unlock and backoff lines are unchanged. No register is read or written that was not read or written before. This reports the failure; it does not make the survey accept a card with fused-off FBPAs. The GP100/GA100 MultiGenHbmSession path is not touched. Tests (host): hbm_survey_reports_the_first_failed_check and hbm_session_outcome_names_every_start_and_step_result, the latter with the #399 register values (FBPA 0..=3 = 0xbadf2010, 4..=15 cfg 0x29, coeff 0x14102) and a PLL mock that drops or loses lock on a write. On master they do not compile: HbmOutcome, HbmSurveyFault and HbmSession::outcome do not exist. Not run: QEMU EDU guest tests (need sudo here), any GPU. Refs #399 Co-Authored-By: Claude Fable 5.2 * gv100/hbm: keep the failed phase in the logged HBM2 outcome Review of #451 found three session results that were logged as something else. A start or step whose target did not lock, whose rollback was verified, and whose survey after the rollback then failed was reported as "the rollback could not be verified". A survey that refused the card before any PLL write was reported as "did not lock or read back; rolled back" when the survey after it passed. And a start whose PMC_BOOT_0 canary read failed right after a good baseline survey was reported as "PLL is not at a stock coefficient". The unlock and step helpers now hand the session the read that stopped them before a write, taken from the reads they already do, so the register access order is unchanged. A refusal before any write puts the session in SurveyFailed with that fault. A verified rollback followed by a failed survey is the new outcome RolledBackSurveyLost (8): the clock stays unknown and writes stay inhibited, and the log names the survey fault. A failed canary is the new outcome CanaryFailed (9); NonStockPll is left for a coefficient the unlock really rejected. The public unlock_gv100_hbm and set_gv100_hbm_ndiv signatures and results are the same as before. Three host tests script the failing read with a mock and fail on the previous commit. Refs #399 Co-Authored-By: Claude Fable 5.2 * gv100/hbm: keep a canary refusal before the target write out of RolledBack program_coeff checks the PMC_BOOT_0 canary before it writes the target coefficient. When that check failed, the caller still ran the rollback (which rewrites the unchanged baseline), and a verified rollback was then logged as "did not lock or read back; rolled back", although the target was never written. program_coeff now returns the canary fault when it refuses before the write. unlock_gv100_hbm_noted and set_gv100_hbm_ndiv_noted run the same rollback as before and pass that fault on, so HbmSession reports the step as SurveyFailed with the CanaryMismatch fault (the existing "refused before the target write" path) instead of RolledBack. Register accesses and their order are unchanged; the public functions keep their signatures. A host test fails the canary once, just before the target write, for both the initial unlock and a live step, and checks that the rollback succeeds and the outcome is SurveyFailed, not RolledBack. Co-Authored-By: Claude Opus 5.5 * gv100/hbm: say "target coefficient not written" for a survey refusal The SurveyFailed log line said "clock not touched". Since a canary refusal just before the target write is reported as SurveyFailed, and the rollback in that path still rewrites the unchanged baseline coefficient, that was true of the clock value but not of every PLL register write. The line now says "target coefficient not written", which holds in every SurveyFailed case, and the HbmOutcome::SurveyFailed doc comment says the same. Log text and a doc comment only; no change to register access, session state or outcome mapping. Refs #399 Co-Authored-By: Claude Opus 5.5 * gv100/hbm: classify a refused baseline restoration by its skip reason outcome() reported Disabled whenever the v100_hbm_ndiv value was 0, but on_user_write(0) with a surveyed baseline steps to the baseline NDIV; a non-stock coefficient refuses that with Skipped and no fault, and the log then said "disabled" instead of the refusal. The session now records the NDIV each start or step acted on (target_ndiv), and outcome() reserves Disabled for a zero effective target; its requested_ndiv argument, which only approximated that, is gone. Test: start at MDIV 3 / NDIV 65, request 80 (rejected), then 0 (baseline restore, rejected) -> NonStockPll. It fails if step_to stops recording the target. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Fable 5.2 Co-authored-by: Scott --- kernel-open/nvidia/gpu_owner.c | 84 ++++ kernel-open/nvidia/gpu_owner.h | 2 + src/nvidia/kernel/pmu_bringup.rs | 61 +++ src/nvidia/src/gpu/hbm_clock.rs | 812 +++++++++++++++++++++++++++---- 4 files changed, 872 insertions(+), 87 deletions(-) diff --git a/kernel-open/nvidia/gpu_owner.c b/kernel-open/nvidia/gpu_owner.c index fa74f15a..c2ca7bf2 100644 --- a/kernel-open/nvidia/gpu_owner.c +++ b/kernel-open/nvidia/gpu_owner.c @@ -232,6 +232,90 @@ void nv_rs_hbm_clock_backoff_log(u32 from_khz, u32 to_khz, u32 from_ndiv, u32 to to_khz / 1000, (to_khz % 1000) / 100, from_ndiv, to_ndiv, lock_us, reasons); } +/* + * Every GV100 HBM2 session outcome other than a verified unlock, which the two functions above + * report. outcome is HbmOutcome and fault_kind is HbmSurveyFaultKind from hbm_clock.rs; the + * fault triple is only meaningful for a failed survey. current_khz is 0 when the live clock is + * unknown. + */ +void nv_rs_hbm_clock_outcome_log(u32 outcome, u32 requested_ndiv, u32 current_khz, + u32 fault_kind, u32 fault_fbpa, u32 fault_value) +{ + char fault[80]; + u32 mhz = current_khz / 1000, mhz_tenth = (current_khz % 1000) / 100; + + switch (fault_kind) { + case 1: + scnprintf(fault, sizeof(fault), "PMC_BOOT_0 read 0x%08x, not GV100", fault_value); + break; + case 2: + scnprintf(fault, sizeof(fault), "FBPA %u PLL config unreadable (0x%08x)", + fault_fbpa, fault_value); + break; + case 3: + scnprintf(fault, sizeof(fault), "FBPA %u PLL coefficient unreadable (0x%08x)", + fault_fbpa, fault_value); + break; + case 4: + scnprintf(fault, sizeof(fault), "FBPA %u PLL not locked (config 0x%08x)", + fault_fbpa, fault_value); + break; + case 5: + scnprintf(fault, sizeof(fault), "FBPA %u PLL coefficient 0x%08x differs from FBPA 0", + fault_fbpa, fault_value); + break; + default: + scnprintf(fault, sizeof(fault), "unknown fault %u", fault_kind); + break; + } + + switch (outcome) { + case 0: + pr_info("nvidia_rs: GV100 HBM2 clock: left at %u.%u MHz (v100_hbm_ndiv=0)\n", + mhz, mhz_tenth); + break; + case 1: + pr_warn("nvidia_rs: GV100 HBM2 clock: v100_hbm_ndiv=%u not applied, PLL is not at a stock coefficient; left at %u.%u MHz\n", + requested_ndiv, mhz, mhz_tenth); + break; + case 2: + pr_info("nvidia_rs: GV100 HBM2 clock: already at %u.%u MHz, v100_hbm_ndiv=%u needs no change\n", + mhz, mhz_tenth, requested_ndiv); + break; + case 4: + if (requested_ndiv) + pr_warn("nvidia_rs: GV100 HBM2 clock: survey failed (%s); target coefficient not written, v100_hbm_ndiv=%u ignored until the next bring-up\n", + fault, requested_ndiv); + else + pr_info("nvidia_rs: GV100 HBM2 clock: survey failed (%s); target coefficient not written, v100_hbm_ndiv writes are ignored until the next bring-up\n", + fault); + break; + case 5: + pr_warn("nvidia_rs: GV100 HBM2 clock: v100_hbm_ndiv=%u did not lock or read back; rolled back to %u.%u MHz, further changes ignored until the next bring-up\n", + requested_ndiv, mhz, mhz_tenth); + break; + case 6: + pr_err("nvidia_rs: GV100 HBM2 clock: v100_hbm_ndiv=%u failed and the rollback could not be verified; live HBM2 clock unknown, further changes ignored until the next bring-up\n", + requested_ndiv); + break; + case 7: + pr_warn("nvidia_rs: GV100 HBM2 clock: v100_hbm_ndiv=%u ignored, an earlier survey or lock failure disabled HBM2 clock changes until the next bring-up\n", + requested_ndiv); + break; + case 8: + pr_err("nvidia_rs: GV100 HBM2 clock: v100_hbm_ndiv=%u did not lock or read back and was rolled back, but the survey after the rollback failed (%s); live HBM2 clock unknown, further changes ignored until the next bring-up\n", + requested_ndiv, fault); + break; + case 9: + pr_warn("nvidia_rs: GV100 HBM2 clock: v100_hbm_ndiv=%u not applied (%s); left at %u.%u MHz\n", + requested_ndiv, fault, mhz, mhz_tenth); + break; + default: + pr_warn("nvidia_rs: GV100 HBM2 clock: unexpected outcome %u (v100_hbm_ndiv=%u)\n", + outcome, requested_ndiv); + break; + } +} static unsigned int v100_p0_gpc_ndiv = 190; module_param(v100_p0_gpc_ndiv, uint, 0644); MODULE_PARM_DESC(v100_p0_gpc_ndiv, "Live GV100/V100 dynamic P0 NAFLL GPC NDIV target (runtime-writable via sysfs; default 190=1425MHz w/ PMU VOLT V/F scaling, auto-caps to 172=1290MHz if VOLT HS scaling unavailable; 204=1530MHz; 0=boot 555MHz)"); diff --git a/kernel-open/nvidia/gpu_owner.h b/kernel-open/nvidia/gpu_owner.h index f0e4e0ce..c97e6471 100644 --- a/kernel-open/nvidia/gpu_owner.h +++ b/kernel-open/nvidia/gpu_owner.h @@ -41,6 +41,8 @@ size_t nv_rs_v100_hbm_ndiv_map_copy(u8 *out, size_t cap); void nv_rs_hbm_clock_log(u32 from_khz, u32 to_khz, u32 from_ndiv, u32 to_ndiv, u32 lock_us); void nv_rs_hbm_clock_backoff_log(u32 from_khz, u32 to_khz, u32 from_ndiv, u32 to_ndiv, u32 lock_us, u32 reason_mask); +void nv_rs_hbm_clock_outcome_log(u32 outcome, u32 requested_ndiv, u32 current_khz, + u32 fault_kind, u32 fault_fbpa, u32 fault_value); u32 nv_rs_v100_hbm_ndiv_gen(void); u32 nv_rs_v100_hbm_ndiv_map_gen(void); u32 nv_rs_v100_p0_gpc_ndiv(void); diff --git a/src/nvidia/kernel/pmu_bringup.rs b/src/nvidia/kernel/pmu_bringup.rs index caefa4db..0ada0f76 100644 --- a/src/nvidia/kernel/pmu_bringup.rs +++ b/src/nvidia/kernel/pmu_bringup.rs @@ -98,6 +98,14 @@ unsafe extern "C" { lock_us: u32, reason_mask: u32, ); + fn nv_rs_hbm_clock_outcome_log( + outcome: u32, + requested_ndiv: u32, + current_khz: u32, + fault_kind: u32, + fault_fbpa: u32, + fault_value: u32, + ); fn nv_rs_v100_p0_gpc_ndiv() -> u32; fn nv_rs_v100_p0_gpc_ndiv_map_copy(out: *mut u8, cap: usize) -> usize; fn nv_rs_v100_p0_volt_uv() -> u32; @@ -2700,6 +2708,13 @@ impl gr_bringup::WhileLoaded for Start<'_, '_> { lock_us, ); } + } else { + // Every other start outcome used to be silent (#399). + log_hbm_outcome( + &hbm_session, + hbm_session.outcome(init_hbm_st), + init_hbm_snap.ndiv, + ); } let mut hbm_write_tracker = nv_rs_core::gpu::hbm_clock::HbmWriteTracker::new( @@ -2839,6 +2854,29 @@ impl gr_bringup::WhileLoaded for Start<'_, '_> { None }; live_mclk_khz = hbm_session.current_khz; + // A live step that did not end in a verified new clock, and a + // sysfs write dropped by a write-inhibited session, are logged + // instead of vanishing (#399). + match step_status { + Some( + nv_rs_core::gpu::hbm_clock::HbmUnlockStatus::Unlocked { + .. + }, + ) => {} + Some(st) => log_hbm_outcome( + &hbm_session, + hbm_session.outcome(st), + cur_hbm_snap.ndiv, + ), + None if user_wrote_hbm && hbm_session.write_inhibited() => { + log_hbm_outcome( + &hbm_session, + nv_rs_core::gpu::hbm_clock::HbmOutcome::WriteInhibited, + cur_hbm_snap.ndiv, + ) + } + None => {} + } if let Some( nv_rs_core::gpu::hbm_clock::HbmUnlockStatus::Unlocked { from, @@ -3586,6 +3624,29 @@ fn cleanup(registers: &mut LockedGpuRegisters, parts: &mut Parts<'_>, pmu_stoppe complete } +/// Logs a GV100 HBM2 session outcome other than a verified unlock (which has its own lines). +#[cfg(not(nv_rs_edu_guest))] +fn log_hbm_outcome( + session: &nv_rs_core::gpu::hbm_clock::HbmSession, + outcome: nv_rs_core::gpu::hbm_clock::HbmOutcome, + requested_ndiv: u8, +) { + let (kind, fbpa, value) = match session.survey_fault { + Some(fault) => (fault.kind as u32, u32::from(fault.fbpa), fault.value), + None => (0, 0, 0), + }; + unsafe { + nv_rs_hbm_clock_outcome_log( + outcome as u32, + u32::from(requested_ndiv), + session.current_khz, + kind, + fbpa, + value, + ); + } +} + #[cfg(not(nv_rs_edu_guest))] #[inline(never)] fn effective_v100_hbm_ndiv(pci: *mut c_void) -> nv_rs_core::gpu::hbm_clock::HbmNdivSnapshot { diff --git a/src/nvidia/src/gpu/hbm_clock.rs b/src/nvidia/src/gpu/hbm_clock.rs index 23a786a2..3f6cf0f5 100644 --- a/src/nvidia/src/gpu/hbm_clock.rs +++ b/src/nvidia/src/gpu/hbm_clock.rs @@ -139,58 +139,119 @@ pub enum HbmUnlockStatus { FailedUnverified { from: HbmCoeff }, } -fn safe_read(io: &mut impl RegisterIo, off: u32) -> Option { +/// Which check of the GV100 HBM2 baseline survey refused the card (#399). +/// +/// Discriminants are shared with `nv_rs_hbm_clock_outcome_log` in `gpu_owner.c`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(u32)] +pub enum HbmSurveyFaultKind { + /// `PMC_BOOT_0` was unreadable or is not the GV100 chip ID. + CanaryMismatch = 1, + /// An FBPA PLL config register read failed or returned a PRI bus-error word. + CfgUnreadable = 2, + /// An FBPA PLL coefficient register read failed or returned a PRI bus-error word. + CoeffUnreadable = 3, + /// An FBPA PLL config register reads back without `PLL_LOCK`. + PllUnlocked = 4, + /// An FBPA PLL coefficient differs from the first partition's. + CoeffMismatch = 5, +} + +/// First check that made [`survey_gv100_hbm_checked`] refuse the card, with the partition +/// index and the raw BAR0 word that failed it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct HbmSurveyFault { + pub kind: HbmSurveyFaultKind, + /// FBPA partition index `0..FBPA_COUNT`; [`HbmSurveyFault::NO_FBPA`] for the canary. + pub fbpa: u8, + /// Raw word read from the register; `u32::MAX` when the read itself failed. + pub value: u32, +} + +impl HbmSurveyFault { + pub const NO_FBPA: u8 = FBPA_COUNT as u8; +} + +/// Guarded BAR0 read. `Err` carries the refused raw word (a PRI bus-error pattern), or +/// `u32::MAX` when the offset is in the no-read window or the read itself failed. +fn read_or_raw(io: &mut impl RegisterIo, off: u32) -> Result { if is_forbidden_read(off) { - return None; + return Err(u32::MAX); } - let v = io.read32(off).ok()?; - if is_bus_error(v) { - return None; + match io.read32(off) { + Ok(v) if is_bus_error(v) => Err(v), + Ok(v) => Ok(v), + Err(_) => Err(u32::MAX), } - Some(v) +} + +fn safe_read(io: &mut impl RegisterIo, off: u32) -> Option { + read_or_raw(io, off).ok() } fn check_canary(io: &mut impl RegisterIo) -> bool { safe_read(io, PMC_BOOT_0) == Some(BOOT0_GV100) } +fn canary_fault(io: &mut impl RegisterIo) -> Result<(), HbmSurveyFault> { + let value = match read_or_raw(io, PMC_BOOT_0) { + Ok(BOOT0_GV100) => return Ok(()), + Ok(v) | Err(v) => v, + }; + Err(HbmSurveyFault { + kind: HbmSurveyFaultKind::CanaryMismatch, + fbpa: HbmSurveyFault::NO_FBPA, + value, + }) +} + /// Surveys all 16 GV100 FBPA partitions; returns `Some(coeff)` iff all 16 are locked, /// agree on the same coefficient, and pass `PMC_BOOT_0` canary before and after. pub fn survey_gv100_hbm(io: &mut impl RegisterIo) -> Option { - if !check_canary(io) { - return None; - } + survey_gv100_hbm_checked(io).ok() +} + +/// [`survey_gv100_hbm`] with the reason for a refusal: the same reads in the same order, +/// reporting the first check that failed instead of collapsing every failure to `None`. +pub fn survey_gv100_hbm_checked(io: &mut impl RegisterIo) -> Result { + canary_fault(io)?; let mut first = 0u32; for i in 0..FBPA_COUNT { - let cfg = safe_read(io, fbpa_cfg(i))?; - let coeff = safe_read(io, fbpa_coeff(i))?; + let fault = |kind, value| HbmSurveyFault { + kind, + fbpa: i as u8, + value, + }; + let cfg = read_or_raw(io, fbpa_cfg(i)) + .map_err(|raw| fault(HbmSurveyFaultKind::CfgUnreadable, raw))?; + let coeff = read_or_raw(io, fbpa_coeff(i)) + .map_err(|raw| fault(HbmSurveyFaultKind::CoeffUnreadable, raw))?; if (cfg & CFG_PLL_LOCK) == 0 { - return None; + return Err(fault(HbmSurveyFaultKind::PllUnlocked, cfg)); } if i == 0 { first = coeff; } else if coeff != first { - return None; + return Err(fault(HbmSurveyFaultKind::CoeffMismatch, coeff)); } } - if !check_canary(io) { - return None; - } - Some(HbmCoeff(first)) + canary_fault(io)?; + Ok(HbmCoeff(first)) } +/// Writes `coeff` and waits for every partition to lock and read it back. `Err(Some(fault))` +/// means the `PMC_BOOT_0` canary refused the card before the write, so `coeff` was never +/// written; `Err(None)` means it failed at or after the write. fn program_coeff( io: &mut impl RegisterIo, coeff: HbmCoeff, mut delay_us: impl FnMut(u32), -) -> Option { +) -> Result> { + canary_fault(io).map_err(Some)?; + io.write32(HBMPLL_COEFF_MC, coeff.0).map_err(|_| None)?; + safe_read(io, fbpa_coeff(0)).ok_or(None)?; if !check_canary(io) { - return None; - } - io.write32(HBMPLL_COEFF_MC, coeff.0).ok()?; - let _ = safe_read(io, fbpa_coeff(0))?; - if !check_canary(io) { - return None; + return Err(None); } let mut elapsed_us = 0u32; for _ in 0..LOCK_TIMEOUT_POLLS { @@ -205,16 +266,15 @@ fn program_coeff( } } if all_locked { - let confirmed = survey_gv100_hbm(io)?; - if confirmed == coeff { - return Some(elapsed_us); - } - return None; + return match survey_gv100_hbm(io) { + Some(confirmed) if confirmed == coeff => Ok(elapsed_us), + _ => Err(None), + }; } delay_us(LOCK_POLL_US); elapsed_us = elapsed_us.saturating_add(LOCK_POLL_US); } - None + Err(None) } /// Attempts to raise a GV100 card's HBM2 clock to `target_ndiv` (default `78` = 1053 MHz). @@ -226,35 +286,52 @@ fn program_coeff( pub fn unlock_gv100_hbm( io: &mut impl RegisterIo, target_ndiv: u8, - mut delay_us: impl FnMut(u32), + delay_us: impl FnMut(u32), ) -> HbmUnlockStatus { - if target_ndiv == 0 || !check_canary(io) { - return HbmUnlockStatus::Skipped; + unlock_gv100_hbm_noted(io, target_ndiv, delay_us).0 +} + +/// [`unlock_gv100_hbm`] plus the read that stopped it before the target write, when one did: +/// with `Skipped` the failed `PMC_BOOT_0` canary, with `FailedRolledBack` the refused survey +/// or the `PMC_BOOT_0` canary just before the target write (the rollback then ran and was +/// verified). `None` beside `FailedRolledBack` means the target was written and the rollback +/// verified. +/// Same register accesses in the same order as before; the fault is taken from them. +fn unlock_gv100_hbm_noted( + io: &mut impl RegisterIo, + target_ndiv: u8, + mut delay_us: impl FnMut(u32), +) -> (HbmUnlockStatus, Option) { + if target_ndiv == 0 { + return (HbmUnlockStatus::Skipped, None); + } + if let Err(fault) = canary_fault(io) { + return (HbmUnlockStatus::Skipped, Some(fault)); } let target = target_ndiv.min(MAX_SAFE_NDIV); - let Some(from) = survey_gv100_hbm(io) else { - return HbmUnlockStatus::FailedRolledBack; + let from = match survey_gv100_hbm_checked(io) { + Ok(from) => from, + Err(fault) => return (HbmUnlockStatus::FailedRolledBack, Some(fault)), }; if from.mdiv() != STOCK_MDIV || from.pldiv() != STOCK_PLDIV || from.ndiv() < MIN_STOCK_NDIV || from.ndiv() > MAX_SAFE_NDIV { - return HbmUnlockStatus::Skipped; + return (HbmUnlockStatus::Skipped, None); } if from.ndiv() >= target { - return HbmUnlockStatus::AlreadyAtOrAbove { current: from }; + return (HbmUnlockStatus::AlreadyAtOrAbove { current: from }, None); } let to = from.with_ndiv(target); match program_coeff(io, to, &mut delay_us) { - Some(lock_us) => HbmUnlockStatus::Unlocked { from, to, lock_us }, - None => { - if program_coeff(io, from, &mut delay_us).is_some() { - HbmUnlockStatus::FailedRolledBack - } else { - HbmUnlockStatus::FailedUnverified { from } - } - } + Ok(lock_us) => (HbmUnlockStatus::Unlocked { from, to, lock_us }, None), + // The rollback runs either way; a canary refusal before the target write is kept so + // a verified rollback of a target that was never written is not logged as one. + Err(refused) => match program_coeff(io, from, &mut delay_us) { + Ok(_) => (HbmUnlockStatus::FailedRolledBack, refused), + Err(_) => (HbmUnlockStatus::FailedUnverified { from }, refused), + }, } } @@ -264,7 +341,7 @@ pub fn restore_gv100_hbm( original: HbmCoeff, delay_us: impl FnMut(u32), ) -> bool { - program_coeff(io, original, delay_us).is_some() + program_coeff(io, original, delay_us).is_ok() } /// Programs GV100 HBM2 PLL to `target_ndiv` in `[MIN_STOCK_NDIV, MAX_SAFE_NDIV]` @@ -272,35 +349,46 @@ pub fn restore_gv100_hbm( pub fn set_gv100_hbm_ndiv( io: &mut impl RegisterIo, target_ndiv: u8, - mut delay_us: impl FnMut(u32), + delay_us: impl FnMut(u32), ) -> HbmUnlockStatus { + set_gv100_hbm_ndiv_noted(io, target_ndiv, delay_us).0 +} + +/// [`set_gv100_hbm_ndiv`] plus the survey or `PMC_BOOT_0` canary fault that stopped it +/// before the target write; `None` beside `FailedRolledBack` means the target was written and +/// the rollback verified. +fn set_gv100_hbm_ndiv_noted( + io: &mut impl RegisterIo, + target_ndiv: u8, + mut delay_us: impl FnMut(u32), +) -> (HbmUnlockStatus, Option) { if target_ndiv == 0 { - return HbmUnlockStatus::Skipped; + return (HbmUnlockStatus::Skipped, None); } let target = target_ndiv.clamp(MIN_STOCK_NDIV, MAX_SAFE_NDIV); - let Some(from) = survey_gv100_hbm(io) else { - return HbmUnlockStatus::FailedRolledBack; + let from = match survey_gv100_hbm_checked(io) { + Ok(from) => from, + Err(fault) => return (HbmUnlockStatus::FailedRolledBack, Some(fault)), }; if from.mdiv() != STOCK_MDIV || from.pldiv() != STOCK_PLDIV || from.ndiv() < MIN_STOCK_NDIV || from.ndiv() > MAX_SAFE_NDIV { - return HbmUnlockStatus::Skipped; + return (HbmUnlockStatus::Skipped, None); } if from.ndiv() == target { - return HbmUnlockStatus::AlreadyAtOrAbove { current: from }; + return (HbmUnlockStatus::AlreadyAtOrAbove { current: from }, None); } let to = from.with_ndiv(target); match program_coeff(io, to, &mut delay_us) { - Some(lock_us) => HbmUnlockStatus::Unlocked { from, to, lock_us }, - None => { - if program_coeff(io, from, &mut delay_us).is_some() { - HbmUnlockStatus::FailedRolledBack - } else { - HbmUnlockStatus::FailedUnverified { from } - } - } + Ok(lock_us) => (HbmUnlockStatus::Unlocked { from, to, lock_us }, None), + // The rollback runs either way; a canary refusal before the target write is kept so + // a verified rollback of a target that was never written is not logged as one. + Err(refused) => match program_coeff(io, from, &mut delay_us) { + Ok(_) => (HbmUnlockStatus::FailedRolledBack, refused), + Err(_) => (HbmUnlockStatus::FailedUnverified { from }, refused), + }, } } @@ -535,6 +623,49 @@ pub struct HbmSession { pub baseline: Option, pub current_khz: u32, pub backoff: HbmAutoBackoff, + /// The read that refused the card in the latest start or step, when one did: the + /// baseline survey, the step's own survey or canary, or the survey after a rollback. + pub survey_fault: Option, + /// The NDIV the latest start or step acted on: the `v100_hbm_ndiv` request at start, + /// or the effective target of a step (for `v100_hbm_ndiv = 0` with a baseline, the + /// baseline NDIV being restored). Zero only when no target was requested. + pub target_ndiv: u8, +} + +/// What an [`HbmSession`] step did to the HBM2 clock, for the kernel log (#399). +/// +/// [`HbmUnlockStatus`] alone cannot be logged: `FailedRolledBack` covers both "the survey +/// refused the card, nothing was written" and "the PLL did not take the new NDIV and was +/// rolled back", and `Skipped` covers both "no target requested" and "PLL is not at a stock +/// coefficient". Discriminants are shared with `nv_rs_hbm_clock_outcome_log` in `gpu_owner.c`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[repr(u32)] +pub enum HbmOutcome { + /// No target requested (`v100_hbm_ndiv = 0`); baseline surveyed, clock untouched. + Disabled = 0, + /// Target requested but the surveyed PLL is not a stock coefficient; clock untouched. + NonStockPll = 1, + /// Clock already at (or, for the initial unlock, above) the target; clock untouched. + AlreadyAtTarget = 2, + /// New NDIV programmed, locked and read back on every partition. + Unlocked = 3, + /// A survey refused the card before the target write (the baseline survey, the one a + /// start or step runs first, or the `PMC_BOOT_0` canary just before the target write); + /// the target coefficient was never written and writes stay inhibited. A rollback may + /// still have rewritten the unchanged baseline, so this is not "no PLL write at all". + SurveyFailed = 4, + /// PLL did not lock or read back at the target; verified back at the previous clock. + RolledBack = 5, + /// Lock/readback failed and the rollback could not be verified; live clock unknown. + ClockUnknown = 6, + /// Request ignored because an earlier failure left the session write-inhibited. + WriteInhibited = 7, + /// PLL did not lock or read back at the target and the rollback was verified, but the + /// survey after it failed; live clock unknown. + RolledBackSurveyLost = 8, + /// Target requested but `PMC_BOOT_0` did not read as GV100 just before the attempt; + /// clock untouched. Says nothing about the PLL coefficient. + CanaryFailed = 9, } impl HbmSession { @@ -545,22 +676,27 @@ impl HbmSession { target_ndiv: u8, mut delay_us: impl FnMut(u32), ) -> (Self, HbmUnlockStatus) { - let Some(base) = survey_gv100_hbm(io) else { - let mut backoff = HbmAutoBackoff::new(STOCK_V100_NDIV); - backoff.user_ndiv = target_ndiv; - return ( - Self { - state: HbmSessionState::SurveyFailed, - baseline: None, - current_khz: 0, - backoff, - }, - HbmUnlockStatus::FailedRolledBack, - ); + let base = match survey_gv100_hbm_checked(io) { + Ok(base) => base, + Err(fault) => { + let mut backoff = HbmAutoBackoff::new(STOCK_V100_NDIV); + backoff.user_ndiv = target_ndiv; + return ( + Self { + state: HbmSessionState::SurveyFailed, + baseline: None, + current_khz: 0, + backoff, + survey_fault: Some(fault), + target_ndiv, + }, + HbmUnlockStatus::FailedRolledBack, + ); + } }; let mut backoff = HbmAutoBackoff::new(base.ndiv()); backoff.user_ndiv = target_ndiv; - let st = unlock_gv100_hbm(io, target_ndiv, &mut delay_us); + let (st, fault) = unlock_gv100_hbm_noted(io, target_ndiv, &mut delay_us); match st { HbmUnlockStatus::Unlocked { from, to, .. } => { backoff.active_ndiv = to.ndiv(); @@ -570,6 +706,8 @@ impl HbmSession { baseline: Some(from), current_khz: to.khz(), backoff, + survey_fault: None, + target_ndiv, }, st, ) @@ -582,6 +720,8 @@ impl HbmSession { baseline: Some(base), current_khz: current.khz(), backoff, + survey_fault: None, + target_ndiv, }, st, ) @@ -592,13 +732,22 @@ impl HbmSession { baseline: Some(base), current_khz: base.khz(), backoff, + survey_fault: fault, + target_ndiv, }, st, ), HbmUnlockStatus::FailedRolledBack => { - let (state, khz) = match survey_gv100_hbm(io) { - Some(cur) => (HbmSessionState::RolledBack, cur.khz()), - None => (HbmSessionState::ClockUnknown, 0), + let after = survey_gv100_hbm_checked(io); + let (state, khz, survey_fault) = match (fault, after) { + // Refused before the target write: not a rollback, whatever the survey after says. + (Some(refused), Ok(cur)) => { + (HbmSessionState::SurveyFailed, cur.khz(), Some(refused)) + } + (Some(refused), Err(_)) => (HbmSessionState::SurveyFailed, 0, Some(refused)), + (None, Ok(cur)) => (HbmSessionState::RolledBack, cur.khz(), None), + // Rollback verified by the unlock, then lost sight of the clock. + (None, Err(lost)) => (HbmSessionState::ClockUnknown, 0, Some(lost)), }; ( Self { @@ -606,6 +755,8 @@ impl HbmSession { baseline: Some(base), current_khz: khz, backoff, + survey_fault, + target_ndiv, }, st, ) @@ -616,6 +767,8 @@ impl HbmSession { baseline: Some(from), current_khz: 0, backoff, + survey_fault: fault, + target_ndiv, }, st, ), @@ -627,6 +780,36 @@ impl HbmSession { !matches!(self.state, HbmSessionState::Ready) } + /// Classifies `status` — as just returned by [`HbmSession::begin`], [`HbmSession::step_to`] + /// or one of the `on_*` steps of this session — for the kernel log. `Disabled` is + /// reserved for a start or step whose effective target was zero: a refused baseline + /// restoration (`v100_hbm_ndiv = 0`) is classified by its actual skip reason. + pub const fn outcome(&self, status: HbmUnlockStatus) -> HbmOutcome { + match status { + HbmUnlockStatus::Unlocked { .. } => HbmOutcome::Unlocked, + HbmUnlockStatus::AlreadyAtOrAbove { .. } => HbmOutcome::AlreadyAtTarget, + HbmUnlockStatus::FailedUnverified { .. } => HbmOutcome::ClockUnknown, + HbmUnlockStatus::FailedRolledBack => match self.state { + HbmSessionState::SurveyFailed => HbmOutcome::SurveyFailed, + // `FailedRolledBack` means the rollback itself was verified (an unverified + // one is `FailedUnverified`); the clock was lost by the survey after it. + HbmSessionState::ClockUnknown => HbmOutcome::RolledBackSurveyLost, + HbmSessionState::Ready | HbmSessionState::RolledBack => HbmOutcome::RolledBack, + }, + HbmUnlockStatus::Skipped => { + if self.write_inhibited() { + HbmOutcome::WriteInhibited + } else if self.target_ndiv == 0 { + HbmOutcome::Disabled + } else if self.survey_fault.is_some() { + HbmOutcome::CanaryFailed + } else { + HbmOutcome::NonStockPll + } + } + } + } + /// Programs a new NDIV target if not write-inhibited. On lock failure, rolls back /// and inhibits further writes (`RolledBack` or `ClockUnknown`). pub fn step_to( @@ -638,7 +821,9 @@ impl HbmSession { if self.write_inhibited() { return HbmUnlockStatus::Skipped; } - let st = set_gv100_hbm_ndiv(io, target_ndiv, &mut delay_us); + self.target_ndiv = target_ndiv; + let (st, fault) = set_gv100_hbm_ndiv_noted(io, target_ndiv, &mut delay_us); + self.survey_fault = fault; match st { HbmUnlockStatus::Unlocked { to, .. } => { self.backoff.active_ndiv = to.ndiv(); @@ -648,17 +833,27 @@ impl HbmSession { self.backoff.active_ndiv = current.ndiv(); self.current_khz = current.khz(); } - HbmUnlockStatus::FailedRolledBack => match survey_gv100_hbm(io) { - Some(cur) => { - self.state = HbmSessionState::RolledBack; - self.backoff.active_ndiv = cur.ndiv(); - self.current_khz = cur.khz(); + HbmUnlockStatus::FailedRolledBack => { + let after = survey_gv100_hbm_checked(io); + // Refused before the target write: a survey failure, not a rollback. + self.state = match (fault, &after) { + (Some(_), _) => HbmSessionState::SurveyFailed, + (None, Ok(_)) => HbmSessionState::RolledBack, + (None, Err(_)) => HbmSessionState::ClockUnknown, + }; + match after { + Ok(cur) => { + self.backoff.active_ndiv = cur.ndiv(); + self.current_khz = cur.khz(); + } + Err(lost) => { + self.current_khz = 0; + if fault.is_none() { + self.survey_fault = Some(lost); + } + } } - None => { - self.state = HbmSessionState::ClockUnknown; - self.current_khz = 0; - } - }, + } HbmUnlockStatus::FailedUnverified { from } => { if self.baseline.is_none() { self.baseline = Some(from); @@ -1793,6 +1988,449 @@ mod tests { )); } + /// GV100 whose multicast PLL write can be made to misbehave, for the outcome tests. + struct MockStubbornGv100 { + gpu: MockGv100, + /// Writes of this NDIV are dropped: the partitions keep the previous coefficient. + refuse_ndiv: Option, + /// Every write drops `PLL_LOCK` on all partitions for good. + writes_kill_lock: bool, + } + + impl MockStubbornGv100 { + fn new(ndiv: u8) -> Self { + Self { + gpu: MockGv100::new(ndiv), + refuse_ndiv: None, + writes_kill_lock: false, + } + } + } + + impl RegisterIo for MockStubbornGv100 { + fn read32(&mut self, offset: u32) -> Result { + self.gpu.read32(offset) + } + + fn write32(&mut self, offset: u32, value: u32) -> Result<(), NvStatus> { + if offset == HBMPLL_COEFF_MC { + if self.writes_kill_lock { + self.gpu.cfg = [0; FBPA_COUNT]; + } + if self.refuse_ndiv == Some(HbmCoeff(value).ndiv()) { + self.gpu.writes += 1; + return Ok(()); + } + } + self.gpu.write32(offset, value) + } + } + + /// Issue #399: the BAR0 words a CMP 100-210 (10de:1df4, 12 GB) reports for its 16 FBPA + /// PLLs. Partitions 0..=3 answer with a PRI bus error, 4..=15 are locked at NDIV 65. + fn cmp_100_210_three_stack() -> MockGv100 { + let mut gpu = MockGv100::new(65); + assert_eq!(gpu.coeff[4], 0x0001_4102); + gpu.cfg = [0x0000_0029; FBPA_COUNT]; + gpu.cfg[..4].fill(0xbadf_2010); + gpu.coeff[..4].fill(0xbadf_2010); + gpu + } + + #[test] + fn hbm_survey_reports_the_first_failed_check() { + let mut ok = MockGv100::new(65); + assert_eq!(survey_gv100_hbm_checked(&mut ok), Ok(HbmCoeff(0x0001_4102))); + + let mut swept = cmp_100_210_three_stack(); + assert_eq!( + survey_gv100_hbm_checked(&mut swept), + Err(HbmSurveyFault { + kind: HbmSurveyFaultKind::CfgUnreadable, + fbpa: 0, + value: 0xbadf_2010, + }) + ); + assert_eq!(survey_gv100_hbm(&mut swept), None); + + let mut coeff_bad = MockGv100::new(65); + coeff_bad.coeff[9] = 0xbadf_1100; + assert_eq!( + survey_gv100_hbm_checked(&mut coeff_bad), + Err(HbmSurveyFault { + kind: HbmSurveyFaultKind::CoeffUnreadable, + fbpa: 9, + value: 0xbadf_1100, + }) + ); + + let mut unlocked = MockGv100::new(65); + unlocked.cfg[7] = 0x0000_0009; + assert_eq!( + survey_gv100_hbm_checked(&mut unlocked), + Err(HbmSurveyFault { + kind: HbmSurveyFaultKind::PllUnlocked, + fbpa: 7, + value: 0x0000_0009, + }) + ); + + let mut split = MockGv100::new(65); + split.coeff[15] = 0x0001_5202; + assert_eq!( + survey_gv100_hbm_checked(&mut split), + Err(HbmSurveyFault { + kind: HbmSurveyFaultKind::CoeffMismatch, + fbpa: 15, + value: 0x0001_5202, + }) + ); + + let mut other = MockGv100::new(65); + other.boot0 = 0x1620_00a1; // TU102 + assert_eq!( + survey_gv100_hbm_checked(&mut other), + Err(HbmSurveyFault { + kind: HbmSurveyFaultKind::CanaryMismatch, + fbpa: HbmSurveyFault::NO_FBPA, + value: 0x1620_00a1, + }) + ); + for gpu in [&ok, &swept, &coeff_bad, &unlocked, &split, &other] { + assert_eq!(gpu.writes, 0); + assert!(!gpu.read_forbidden); + } + } + + #[test] + fn hbm_session_outcome_names_every_start_and_step_result() { + // Values are shared with nv_rs_hbm_clock_outcome_log in kernel-open/nvidia/gpu_owner.c. + assert_eq!(HbmOutcome::Disabled as u32, 0); + assert_eq!(HbmOutcome::NonStockPll as u32, 1); + assert_eq!(HbmOutcome::AlreadyAtTarget as u32, 2); + assert_eq!(HbmOutcome::Unlocked as u32, 3); + assert_eq!(HbmOutcome::SurveyFailed as u32, 4); + assert_eq!(HbmOutcome::RolledBack as u32, 5); + assert_eq!(HbmOutcome::ClockUnknown as u32, 6); + assert_eq!(HbmOutcome::WriteInhibited as u32, 7); + assert_eq!(HbmSurveyFaultKind::CanaryMismatch as u32, 1); + assert_eq!(HbmSurveyFaultKind::CfgUnreadable as u32, 2); + assert_eq!(HbmSurveyFaultKind::CoeffUnreadable as u32, 3); + assert_eq!(HbmSurveyFaultKind::PllUnlocked as u32, 4); + assert_eq!(HbmSurveyFaultKind::CoeffMismatch as u32, 5); + + // Issue #399: with FBPA 0..=3 unreadable the start fails in the survey, for a requested + // target and for the default 0 alike, and the session carries the reason. Before, this + // start and a PLL that would not lock were the same silent `FailedRolledBack`. + for target in [82u8, 0u8] { + let mut swept = cmp_100_210_three_stack(); + let (sess, st) = HbmSession::begin(&mut swept, target, |_| {}); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!(sess.outcome(st), HbmOutcome::SurveyFailed); + assert_eq!( + sess.survey_fault, + Some(HbmSurveyFault { + kind: HbmSurveyFaultKind::CfgUnreadable, + fbpa: 0, + value: 0xbadf_2010, + }) + ); + assert_eq!(sess.current_khz, 0); + assert!(sess.write_inhibited()); + assert_eq!(swept.writes, 0); + } + + // No target: baseline surveyed, nothing written. + let mut gpu = MockGv100::new(65); + let (sess, st) = HbmSession::begin(&mut gpu, 0, |_| {}); + assert_eq!(sess.outcome(st), HbmOutcome::Disabled); + assert_eq!( + (sess.current_khz, sess.survey_fault, gpu.writes), + (877_500, None, 0) + ); + + // Verified unlock. + let mut gpu = MockGv100::new(65); + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(sess.outcome(st), HbmOutcome::Unlocked); + + // V100S already at 1107 MHz. + let mut gpu = MockGv100::new(82); + let (sess, st) = HbmSession::begin(&mut gpu, 78, |_| {}); + assert_eq!(sess.outcome(st), HbmOutcome::AlreadyAtTarget); + assert_eq!((sess.current_khz, gpu.writes), (1_107_000, 0)); + + // Target requested but the PLL is not at a stock M divider. + let mut gpu = MockGv100::new(65); + gpu.coeff = [0x0001_4103; FBPA_COUNT]; + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(st, HbmUnlockStatus::Skipped); + assert_eq!(sess.outcome(st), HbmOutcome::NonStockPll); + assert_eq!(gpu.writes, 0); + + // The PLL does not take NDIV 82 and is verified back at 877.5 MHz. + let mut gpu = MockStubbornGv100::new(65); + gpu.refuse_ndiv = Some(82); + let (mut sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!(sess.state, HbmSessionState::RolledBack); + assert_eq!(sess.outcome(st), HbmOutcome::RolledBack); + assert_eq!((sess.current_khz, sess.survey_fault), (877_500, None)); + // A later request is dropped by the write-inhibit; step_to reports it as such. + assert!(sess.on_user_write(&mut gpu, 78, |_| {}).is_none()); + let st = sess.step_to(&mut gpu, 78, |_| {}); + assert_eq!(st, HbmUnlockStatus::Skipped); + assert_eq!(sess.outcome(st), HbmOutcome::WriteInhibited); + + // The write loses PLL lock and the rollback cannot be verified. + let mut gpu = MockStubbornGv100::new(65); + gpu.writes_kill_lock = true; + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert!(matches!(st, HbmUnlockStatus::FailedUnverified { .. })); + assert_eq!(sess.outcome(st), HbmOutcome::ClockUnknown); + assert_eq!(sess.current_khz, 0); + + // A live step from a healthy session that rolls back is named the same way. + let mut gpu = MockStubbornGv100::new(65); + let (mut sess, _) = HbmSession::begin(&mut gpu, 0, |_| {}); + gpu.refuse_ndiv = Some(80); + let st = sess + .on_user_write(&mut gpu, 80, |_| {}) + .expect("step attempted"); + assert_eq!(sess.outcome(st), HbmOutcome::RolledBack); + assert_eq!(sess.current_khz, 877_500); + } + + /// [`MockStubbornGv100`] that answers `bad_value` for reads number `bad_from..bad_until` + /// (counted from 1) of one watched register, to fail a chosen read of a session step. + struct MockGlitchGv100 { + gpu: MockStubbornGv100, + watch: u32, + watch_reads: usize, + bad_from: usize, + bad_until: usize, + bad_value: u32, + } + + impl MockGlitchGv100 { + fn new(watch: u32, bad_from: usize, bad_until: usize, bad_value: u32) -> Self { + Self { + gpu: MockStubbornGv100::new(65), + watch, + watch_reads: 0, + bad_from, + bad_until, + bad_value, + } + } + } + + impl RegisterIo for MockGlitchGv100 { + fn read32(&mut self, offset: u32) -> Result { + if offset == self.watch { + self.watch_reads += 1; + if (self.bad_from..self.bad_until).contains(&self.watch_reads) { + return Ok(self.bad_value); + } + } + self.gpu.read32(offset) + } + + fn write32(&mut self, offset: u32, value: u32) -> Result<(), NvStatus> { + self.gpu.write32(offset, value) + } + } + + fn pll_unlocked(fbpa: u8) -> HbmSurveyFault { + HbmSurveyFault { + kind: HbmSurveyFaultKind::PllUnlocked, + fbpa, + value: 0x0000_0009, + } + } + + #[test] + fn hbm_verified_rollback_survives_a_later_survey_failure() { + assert_eq!(HbmOutcome::RolledBackSurveyLost as u32, 8); + + // A verified rollback stays a verified rollback when the survey after it fails. + // FBPA 5 cfg reads: 1 start survey, 2 unlock survey, 3 lock wait + 4 readback at + // NDIV 82 (refused), 5 lock wait + 6 readback of the rollback, 7 the survey after. + let mut gpu = MockGlitchGv100::new(fbpa_cfg(5), 7, usize::MAX, 0x0000_0009); + gpu.gpu.refuse_ndiv = Some(82); + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!(gpu.gpu.gpu.writes, 2); + assert_eq!(HbmCoeff(gpu.gpu.gpu.coeff[0]).ndiv(), 65); + assert_ne!(sess.outcome(st), HbmOutcome::ClockUnknown); + assert_eq!(sess.outcome(st), HbmOutcome::RolledBackSurveyLost); + assert_eq!(sess.survey_fault, Some(pll_unlocked(5))); + // The live clock is unknown after that later failure, and writes stay inhibited. + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::ClockUnknown, 0) + ); + + // A live step whose rollback is verified but whose survey after it fails. + // FBPA 5 cfg reads: 1 start, 2 step survey, 3-4 NDIV 80 refused, 5-6 rollback, 7 after. + let mut gpu = MockGlitchGv100::new(fbpa_cfg(5), 7, usize::MAX, 0x0000_0009); + let (mut sess, _) = HbmSession::begin(&mut gpu, 0, |_| {}); + gpu.gpu.refuse_ndiv = Some(80); + let st = sess + .on_user_write(&mut gpu, 80, |_| {}) + .expect("step attempted"); + assert_eq!(gpu.gpu.gpu.writes, 2); + assert_ne!(sess.outcome(st), HbmOutcome::ClockUnknown); + assert_eq!(sess.outcome(st), HbmOutcome::RolledBackSurveyLost); + assert_eq!(sess.survey_fault, Some(pll_unlocked(5))); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::ClockUnknown, 0) + ); + } + + #[test] + fn hbm_survey_refusal_before_a_write_is_not_a_rollback() { + // The survey inside the unlock refuses the card before any write. That is a survey + // failure, not a rollback, whether the survey after it passes (read 3 good) ... + let mut gpu = MockGlitchGv100::new(fbpa_cfg(5), 2, 3, 0x0000_0009); + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!(gpu.gpu.gpu.writes, 0); + assert_eq!(sess.outcome(st), HbmOutcome::SurveyFailed); + assert_eq!(sess.survey_fault, Some(pll_unlocked(5))); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::SurveyFailed, 877_500) + ); + assert_eq!(sess.baseline, Some(HbmCoeff(0x0001_4102))); + assert!(sess.write_inhibited()); + // ... or fails too. + let mut gpu = MockGlitchGv100::new(fbpa_cfg(5), 2, usize::MAX, 0x0000_0009); + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(gpu.gpu.gpu.writes, 0); + assert_eq!(sess.outcome(st), HbmOutcome::SurveyFailed); + assert_eq!(sess.survey_fault, Some(pll_unlocked(5))); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::SurveyFailed, 0) + ); + + // Same for a live step: start (read 1) is fine, the step's survey (read 2) is not. + let mut gpu = MockGlitchGv100::new(fbpa_cfg(5), 2, 3, 0x0000_0009); + let (mut sess, st) = HbmSession::begin(&mut gpu, 0, |_| {}); + assert_eq!(sess.outcome(st), HbmOutcome::Disabled); + let st = sess + .on_user_write(&mut gpu, 80, |_| {}) + .expect("step attempted"); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!(gpu.gpu.gpu.writes, 0); + assert_eq!(sess.outcome(st), HbmOutcome::SurveyFailed); + assert_eq!(sess.survey_fault, Some(pll_unlocked(5))); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::SurveyFailed, 877_500) + ); + } + + #[test] + fn hbm_canary_refusal_before_the_target_write_is_not_a_rollback() { + let canary = Some(HbmSurveyFault { + kind: HbmSurveyFaultKind::CanaryMismatch, + fbpa: HbmSurveyFault::NO_FBPA, + value: u32::MAX, + }); + // PMC_BOOT_0 reads: 1-2 start survey, 3 unlock canary, 4-5 unlock survey, 6 the + // canary just before the NDIV 82 write (fails once), 7-10 the rollback to NDIV 65 + // (canary, canary after the write, readback survey), 11-12 the survey after. + // NDIV 82 was never written, so the verified rollback must not be logged as one. + let mut gpu = MockGlitchGv100::new(PMC_BOOT_0, 6, 7, u32::MAX); + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!((gpu.watch_reads, gpu.gpu.gpu.writes), (12, 1)); + assert_eq!(HbmCoeff(gpu.gpu.gpu.coeff[0]).ndiv(), 65); + assert_ne!(sess.outcome(st), HbmOutcome::RolledBack); + assert_eq!(sess.outcome(st), HbmOutcome::SurveyFailed); + assert_eq!(sess.survey_fault, canary); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::SurveyFailed, 877_500) + ); + + // Same for a live step: reads 1-2 start survey, 3-4 step survey, 5 the canary just + // before the NDIV 80 write (fails once), then a verified rollback and a good survey. + let mut gpu = MockGlitchGv100::new(PMC_BOOT_0, 5, 6, u32::MAX); + let (mut sess, _) = HbmSession::begin(&mut gpu, 0, |_| {}); + let st = sess + .on_user_write(&mut gpu, 80, |_| {}) + .expect("step attempted"); + assert_eq!(st, HbmUnlockStatus::FailedRolledBack); + assert_eq!(gpu.gpu.gpu.writes, 1); + assert_ne!(sess.outcome(st), HbmOutcome::RolledBack); + assert_eq!(sess.outcome(st), HbmOutcome::SurveyFailed); + assert_eq!(sess.survey_fault, canary); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::SurveyFailed, 877_500) + ); + } + + #[test] + fn hbm_skip_on_a_failed_canary_is_not_a_non_stock_pll() { + assert_eq!(HbmOutcome::CanaryFailed as u32, 9); + // The start survey reads a stock coefficient (canary reads 1 and 2), then the + // unlock's own canary read (3) fails. The PLL is stock; the skip is a canary failure. + let mut gpu = MockGlitchGv100::new(PMC_BOOT_0, 3, 4, u32::MAX); + let (sess, st) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert_eq!(st, HbmUnlockStatus::Skipped); + assert_eq!((gpu.watch_reads, gpu.gpu.gpu.writes), (3, 0)); + assert_ne!(sess.outcome(st), HbmOutcome::NonStockPll); + assert_eq!(sess.outcome(st), HbmOutcome::CanaryFailed); + assert_eq!( + sess.survey_fault, + Some(HbmSurveyFault { + kind: HbmSurveyFaultKind::CanaryMismatch, + fbpa: HbmSurveyFault::NO_FBPA, + value: u32::MAX, + }) + ); + assert_eq!( + (sess.state, sess.current_khz), + (HbmSessionState::Ready, 877_500) + ); + + // A coefficient the unlock rejects is still NonStockPll, with no fault recorded, and + // a fault from an earlier step does not leak into the next one. + let mut gpu = MockGlitchGv100::new(PMC_BOOT_0, 3, 4, u32::MAX); + let (mut sess, _) = HbmSession::begin(&mut gpu, 82, |_| {}); + assert!(sess.survey_fault.is_some()); + gpu.gpu.gpu.coeff = [0x0001_4103; FBPA_COUNT]; + let st = sess.step_to(&mut gpu, 80, |_| {}); + assert_eq!(st, HbmUnlockStatus::Skipped); + assert_eq!(sess.outcome(st), HbmOutcome::NonStockPll); + assert_eq!(sess.survey_fault, None); + + // A refused baseline restoration (`v100_hbm_ndiv = 0` after a rejected request) is + // reported as the refusal, not as Disabled: start at MDIV 3 / NDIV 65, request 80 + // (rejected, but it moves the active NDIV), then 0 (restore to 65, also rejected). + let mut gpu = MockGv100::new(65); + gpu.coeff = [0x0001_4103; FBPA_COUNT]; + let (mut sess, st) = HbmSession::begin(&mut gpu, 0, |_| {}); + assert_eq!(sess.outcome(st), HbmOutcome::Disabled); + let st = sess + .on_user_write(&mut gpu, 80, |_| {}) + .expect("step attempted"); + assert_eq!(st, HbmUnlockStatus::Skipped); + assert_eq!(sess.outcome(st), HbmOutcome::NonStockPll); + let st = sess + .on_user_write(&mut gpu, 0, |_| {}) + .expect("baseline restoration attempted"); + assert_eq!(st, HbmUnlockStatus::Skipped); + assert_eq!(sess.target_ndiv, 65); + assert_eq!(sess.outcome(st), HbmOutcome::NonStockPll); + assert_eq!(gpu.writes, 0); + } + #[test] fn hbm_session_survey_backoff_inhibit_and_fbpa_ecc_poll() { // 1. Normal session: survey 65 -> unlock 82 -> instability step-down 78 -> verified restore 65.