mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
* gv100: live board power from the PMU, groups built from the VBIOS (#136) On GV100 boards where nothing else reads the INA3221 (seen on SXM2-to-PCIe adapter boards), nvidia_rs never populated the PMU's PERF/PMGR board-object groups, so the PMU never sampled board power and `nvidia-smi` showed the placeholder. Stock 580.178.04, before its first power read, sends nine BOARDOBJ_GRP_CMD SETs (PERF unit 0x13: pstates 0x1c, vpstates 0x1b, VFE variables 0x19, VFE equations 0x1a; PMGR unit 0x18: I2C devices 0x2a, power equations 0x26, sensors 0x27, devices 0x28, policies 0x29), each after writing its group into the super-surface, then reads power with a PWR_DEVICE (0x28) copy-out. No LOAD is involved. New `gpu::gv100_pmu_tables` builds all nine groups, the twelve requests and the copy-out region from the card's own VBIOS (BIT 'P' v2 tables and the DCB I2C table). Every VBIOS read is bounds-checked; a table it does not understand yields None, never a panic. The kernel side captures the VBIOS before the PMU starts, sends the built sequence once after PMU bring-up, and the monitor tick then reads device 0's power through the copy-out when no CPU-side INA3221 was found, so the existing power governor (`nvidia-smi -pl`) has a real reading. Opt-in: `v100_pmu_power=1` (default off), GV100 only (PMC_BOOT_0 chip 0x140). Evidence: [O]. Every mapping was matched against one adapter-board V100-SXM2-16GB's stock super-surface (read back through BAR2 after a stock init in a VFIO VM) and its VBIOS: all nine groups byte-identical. Not yet REA-derived; the fields that are observed constants are marked in the module. Tests: request and copy-out layouts against the recorded stock requests, bounds on hostile tables, and (with NV_RS_V100_VBIOS / NV_RS_V100_SURF set; stock bytes are not in the repo) 0 differing bytes against the stock surface. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * gv100 PMU power: address review (per-GPU state, failure paths, VBIOS bounds) Per-GPU state: the request buffer, the copy-out shape and the captured VBIOS move from module statics into `Persist`, initialised in place like the RPC scratch buffers. Two GPUs no longer share one buffer or one shape, and a session publishes its shape only after its own sequence completed. Lifetimes: `cleanup` drops a VBIOS image the bring-up never took (startup ended early) and clears the shape. Power read failure: a failed live read (including a reply timeout, after which the transport refuses later commands as the original does) now turns power polling off for the session and logs once (stage 8), instead of issuing a command on every tick. Boot sequence: a failed copy-out is checked before the 1 s wait and record read; only completed commands are counted. PROM selection failure on a GV100 gets its own code (7) so it is logged; a non-GV100 (2) stays silent. Table builder: - BIT 'P' pointer slots are read only within the token's declared data size. - Sparse VFE variable/equation tables (a non-zero type after a type-0 one) are refused: the serializers index 0..count, and how stock lays out a sparse table is unknown. - A pstate sub-entry count that would spill past one 0x420 record is refused. Each has a test; each test fails with its guard removed. The reference card's nine groups are still 0 bytes different from stock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * gv100 PMU tables: one PWR_DEVICE reading, a bounds test that reaches the serializers - pwr_devices() reads the PWR_DEVICE table once (mask, input mask, input count); the SET group and the copy-out shape both use it, so they cannot disagree. Output on the reference VBIOS is unchanged (0 bytes differ, all nine groups). - writes_stay_inside_each_region now runs 4000 seeded junk VBIOS images whose BIT 'P' slots and DCB I2C table point at in-ROM tables, and asserts every serializer writes on some input. Dropping the PWR_DEVICE input bound panics it; a serializer that never writes fails it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * pmu_bringup: gate the #136 power-path imports outside the EDU guest gv100_pmu_tables, rom, rom_bit, rom_descriptors and RuntimeVariant are only used by #[cfg(not(nv_rs_edu_guest))] code, so the guest build (tools/vm/build_guest_test.sh) warned five unused_imports. They now sit in their own cfg-gated use; the main import list is back to master's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Cool3reeze <193058586+Cool3reeze@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -264,6 +264,20 @@ u32 nv_rs_v100_p0_volt_uv(void) {
|
||||
return 1050000u;
|
||||
return v100_p0_volt_uv;
|
||||
}
|
||||
/* #136: live board power + `nvidia-smi -pl` on GV100 boards whose PMU PMGR groups are
|
||||
* otherwise never populated (seen on SXM2-to-PCIe adapter boards). Builds the nine
|
||||
* PERF/PMGR board-object groups from the card's own VBIOS, sends them to the PMU, then
|
||||
* reads power through the PWR_DEVICE status copy-out. Opt-in: the field mappings are
|
||||
* observed on one card, not yet REA-confirmed. */
|
||||
static bool v100_pmu_power = false;
|
||||
module_param(v100_pmu_power, bool, 0444);
|
||||
MODULE_PARM_DESC(v100_pmu_power, "GV100: build the PMU PERF/PMGR groups from the VBIOS for live board power and power capping (#136; default: 0)");
|
||||
bool nv_rs_v100_pmu_power(void) { return v100_pmu_power; }
|
||||
void nv_rs_pmu_power_log(u32 gpu_id, u32 stage, u32 a, u32 b, u32 c)
|
||||
{
|
||||
pr_info("nvidia_rs: GV100 PMU power gpu=0x%08x stage=%u a=0x%x b=0x%x c=0x%x\n",
|
||||
gpu_id, stage, a, b, c);
|
||||
}
|
||||
static unsigned int force_pcie_gen = 0;
|
||||
module_param(force_pcie_gen, uint, 0644);
|
||||
MODULE_PARM_DESC(force_pcie_gen, "Opt-in PCIe link-speed override (0=untouched default, 2=Gen2 5GT/s, 3=Gen3 8GT/s, 4=Gen4 16GT/s for CMP/P10x/legacy hosts, #168)");
|
||||
|
||||
@ -46,6 +46,8 @@ u32 nv_rs_v100_hbm_ndiv_map_gen(void);
|
||||
u32 nv_rs_v100_p0_gpc_ndiv(void);
|
||||
size_t nv_rs_v100_p0_gpc_ndiv_map_copy(u8 *out, size_t cap);
|
||||
u32 nv_rs_v100_p0_volt_uv(void);
|
||||
bool nv_rs_v100_pmu_power(void);
|
||||
void nv_rs_pmu_power_log(u32 gpu_id, u32 stage, u32 a, u32 b, u32 c);
|
||||
u32 nv_rs_force_pcie_gen(void);
|
||||
bool nv_rs_idle_pstate(void);
|
||||
u32 nv_rs_idle_pstate_delay_s(void);
|
||||
|
||||
@ -75,6 +75,9 @@ use nv_rs_core::{
|
||||
},
|
||||
mmu::{bar2_setup, engine_discovery::EngineRecord, placement},
|
||||
};
|
||||
// #136 power path: only built outside the EDU guest, like the code that uses it.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
use nv_rs_core::gpu::{gv100_pmu_tables, rom, rom_bit, rom_descriptors, RuntimeVariant};
|
||||
|
||||
unsafe extern "C" {
|
||||
fn nv_rs_os_busy_us(microseconds: u32);
|
||||
@ -127,6 +130,8 @@ unsafe extern "C" {
|
||||
pwm_after: u32,
|
||||
);
|
||||
fn nv_rs_pmgr_power_log(gpu_id: u32, load_st: u32, poll_st: u32, ch0_mw: u32, ch7_mw: u32);
|
||||
fn nv_rs_v100_pmu_power() -> bool;
|
||||
fn nv_rs_pmu_power_log(gpu_id: u32, stage: u32, a: u32, b: u32, c: u32);
|
||||
fn nv_rs_nvlink_enabled() -> bool;
|
||||
fn nv_rs_nvlink_boot_log(
|
||||
gpu_id: u32,
|
||||
@ -482,6 +487,18 @@ struct Persist {
|
||||
/// RPCs so kernel stack frames stay strictly <= 1,024 bytes.
|
||||
pmgr_rpc_buf: [u8; pmu_pmgr::RPC_SIZE_PWR_MONITOR_GET_STATUS],
|
||||
volt_rpc_buf: [u8; pmu_volt::RPC_SIZE_VOLT_SET_VOLTAGE],
|
||||
/// #136 (`v100_pmu_power`): this GPU's request buffer for the built PMU commands.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
power_req_buf: [u8; POWER_REQ_BYTES],
|
||||
/// #136: PWR_DEVICE (mask, sub-group count) for the power copy-out, set only once
|
||||
/// this session's boot sequence completed; cleared again by `cleanup` and after the
|
||||
/// first failed power read.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
status_shape: Option<(u32, u16)>,
|
||||
/// #136: this GPU's VBIOS, captured in `init_locked` before the PMU starts, taken by
|
||||
/// `runtime_pmu_power_bringup`, and dropped by `cleanup` if that never ran.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
pmu_vbios: Option<PmuVbios>,
|
||||
}
|
||||
|
||||
impl Persist {
|
||||
@ -516,6 +533,11 @@ impl Persist {
|
||||
(&raw mut (*place).fecs_start).write(None);
|
||||
(&raw mut (*place).gpccs_start).write(None);
|
||||
(&raw mut (*place).budget).write(None);
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
{
|
||||
(&raw mut (*place).status_shape).write(None);
|
||||
(&raw mut (*place).pmu_vbios).write(None);
|
||||
}
|
||||
Self::initialise_rpc_scratch(place);
|
||||
}
|
||||
}
|
||||
@ -526,6 +548,13 @@ impl Persist {
|
||||
for i in 0..pmu_pmgr::RPC_SIZE_PWR_MONITOR_GET_STATUS {
|
||||
core::ptr::write_volatile(pmgr_ptr.add(i), 0);
|
||||
}
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
{
|
||||
let power_ptr = (&raw mut (*place).power_req_buf) as *mut u8;
|
||||
for i in 0..POWER_REQ_BYTES {
|
||||
core::ptr::write_volatile(power_ptr.add(i), 0);
|
||||
}
|
||||
}
|
||||
let volt_ptr = (&raw mut (*place).volt_rpc_buf) as *mut u8;
|
||||
for i in 0..pmu_volt::RPC_SIZE_VOLT_SET_VOLTAGE {
|
||||
core::ptr::write_volatile(volt_ptr.add(i), 0);
|
||||
@ -1985,6 +2014,21 @@ fn init_locked(registers: &mut LockedGpuRegisters, parts: &mut Parts<'_>) -> boo
|
||||
// Nothing reads the state before this.
|
||||
unsafe { Persist::initialise(persist.as_ptr(), 0) };
|
||||
let mut io = PmuIo { registers, parts };
|
||||
// #136 (`v100_pmu_power`): capture the VBIOS now, while the PMU is stopped (reading the
|
||||
// PROM while it runs halted it on its next command, VFIO VM).
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
if unsafe { nv_rs_v100_pmu_power() } {
|
||||
let captured = pmu_vbios_capture(&mut io);
|
||||
// Stage 1: VBIOS capture failed (error code, see `pmu_vbios_capture`); 2 = not a
|
||||
// GV100, which is expected and not logged.
|
||||
if let Err(e) = captured.as_ref() {
|
||||
if *e != 2 {
|
||||
let gpu_id = unsafe { nv_rs_pci_gpu_id(io.registers.pci_token()) };
|
||||
unsafe { nv_rs_pmu_power_log(gpu_id, 1, *e, 0, 0) };
|
||||
}
|
||||
}
|
||||
*field!(io, pmu_vbios) = captured.ok();
|
||||
}
|
||||
// KernelFifo+170 (core `gr_fecs_start::subcontext_count`): the original
|
||||
// reads it once, before the FIFO load, and keeps the value.
|
||||
let subcontexts = match fecs::subcontext_count(&mut io, GV100_KFIFO_44D) {
|
||||
@ -2598,6 +2642,11 @@ impl gr_bringup::WhileLoaded for Start<'_, '_> {
|
||||
// 3) Probe live hardware board power via GV100 PMGR HW I2C INA3221
|
||||
// (scan active pull-up ports 0 & 2, 7-bit addrs 0x40..=0x43).
|
||||
let ina3221_loc = runtime_ina3221_init(io.registers);
|
||||
// #136: PMU-owned board power (opt-in, `v100_pmu_power`).
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
if unsafe { nv_rs_v100_pmu_power() } {
|
||||
runtime_pmu_power_bringup(&mut io);
|
||||
}
|
||||
let target_gpc = if req_gpc > 172 && !volt_raised {
|
||||
172
|
||||
} else {
|
||||
@ -2871,9 +2920,24 @@ impl gr_bringup::WhileLoaded for Start<'_, '_> {
|
||||
);
|
||||
}
|
||||
}
|
||||
let hw_power_mw = ina3221_loc.and_then(|(port, addr)| {
|
||||
runtime_ina3221_poll(regs, port, addr)
|
||||
});
|
||||
let hw_power_mw = ina3221_loc
|
||||
.and_then(|(port, addr)| {
|
||||
runtime_ina3221_poll(regs, port, addr)
|
||||
})
|
||||
.or_else(|| {
|
||||
// #136: the PMU's PWR_DEVICE copy-out, once
|
||||
// `runtime_pmu_power_bringup` succeeded.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
{
|
||||
let mut pio = PmuIo {
|
||||
registers: &mut *regs,
|
||||
parts: unsafe { &mut *parts_ptr },
|
||||
};
|
||||
pmu_power_read(&mut pio)
|
||||
}
|
||||
#[cfg(nv_rs_edu_guest)]
|
||||
None
|
||||
});
|
||||
// Live `nvidia-smi -pl` power-limit enforcement (#136):
|
||||
// closed-loop GPC NAFLL stepping with INA3221 power, or
|
||||
// proportional open-loop GPC cap when no sensor is present.
|
||||
@ -3452,6 +3516,14 @@ fn pmu_unload_step(registers: &mut LockedGpuRegisters, parts: &mut Parts<'_>) ->
|
||||
/// them once bus mastering is confirmed off, or reports the leak.
|
||||
#[inline(never)]
|
||||
fn cleanup(registers: &mut LockedGpuRegisters, parts: &mut Parts<'_>, pmu_stopped: bool) -> bool {
|
||||
// #136: the VBIOS image if startup ended before the power bring-up took it, and the
|
||||
// session's power copy-out shape.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
unsafe {
|
||||
let persist = parts.persist.as_ptr();
|
||||
drop((*persist).pmu_vbios.take());
|
||||
(*persist).status_shape = None;
|
||||
}
|
||||
let mut io = PmuIo { registers, parts };
|
||||
*field!(io, reset_pending) = false;
|
||||
let deferred = core::mem::take(field!(io, deferred));
|
||||
@ -3619,6 +3691,317 @@ fn runtime_ina3221_poll(regs: &mut LockedGpuRegisters, port: u32, addr: u8) -> O
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------
|
||||
// #136: PMU-owned board power on GV100 (opt-in, `v100_pmu_power`).
|
||||
//
|
||||
// Stock 580.178.04, before its first power read, sends nine BOARDOBJ_GRP_CMD SETs
|
||||
// (PERF unit 0x13: pstates, vpstates, VFE variables, VFE equations; PMGR unit 0x18: I2C
|
||||
// devices, power equations, sensors, devices, policies), each after writing its group
|
||||
// into the PMU super-surface, then reads power with a PWR_DEVICE copy-out. nvidia_rs
|
||||
// never populated those groups, so the PMU never sampled the board's INA3221. Here the
|
||||
// groups, requests and copy-out region are built from this GPU's VBIOS by
|
||||
// `gv100_pmu_tables`. No LOAD is sent. Evidence: [O] on one adapter-board
|
||||
// V100-SXM2-16GB (byte-identical to stock there); see the module docs.
|
||||
//
|
||||
// Log stages (`nv_rs_pmu_power_log`): 1 VBIOS capture error; 2 (0xffff = VBIOS not
|
||||
// understood | mask, top-group count); 3 (command, error) group write failed;
|
||||
// 4 (command, status, CPUCTL) per command; 5 (copy-out, mW, mA); 6 (PC, SP, r14) PMU
|
||||
// halted; 7 (commands completed, mW) summary; 8 (status, CPUCTL) a live power read
|
||||
// failed and polling stopped for this session.
|
||||
// ---------------------------------------------------------------------------------------
|
||||
|
||||
/// Size of `Persist::power_req_buf`: the largest built request
|
||||
/// (`gv100_pmu_tables::REQUEST_MAX`, 0x434), rounded up.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
const POWER_REQ_BYTES: usize = 0x440;
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
const _: () = assert!(gv100_pmu_tables::REQUEST_MAX <= POWER_REQ_BYTES);
|
||||
|
||||
/// This GPU's VBIOS image (`Persist::pmu_vbios`).
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
struct PmuVbios {
|
||||
image: Storage,
|
||||
len: usize,
|
||||
bit: u32,
|
||||
base: u32,
|
||||
expansion: u32,
|
||||
}
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
impl PmuVbios {
|
||||
fn bytes(&self) -> &[u8] {
|
||||
// Exclusively owned and at least GV100_MAX_BYTES >= len long.
|
||||
unsafe { core::slice::from_raw_parts(self.image.pointer.cast::<u8>().as_ptr(), self.len) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Captures the PROM the way `gr_bringup::vbios_overrides` does. Err codes: 1 boot
|
||||
/// register, 2 not a GV100 (expected, not logged), 3 allocation, 4 PROM read, 5 image
|
||||
/// list, 6 BIT header, 7 no PROM source for this chip.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn pmu_vbios_capture(io: &mut PmuIo<'_, '_>) -> Result<PmuVbios, u32> {
|
||||
let boot0 = io.registers.read32(0).map_err(|_| 1u32)?;
|
||||
// GV100 only (PMC_BOOT_0 chip id 0x140): the group layouts and super-surface
|
||||
// offsets in `gv100_pmu_tables` are GV100's. Other chips are left untouched.
|
||||
if (boot0 >> 20) & 0x1ff != 0x140 {
|
||||
return Err(2);
|
||||
}
|
||||
// chip_arch: legacy PMC_BOOT_0 architecture and implementation.
|
||||
let architecture = (boot0 >> 3 & 0x20) | (boot0 >> 24 & 0x1f);
|
||||
let implementation = (boot0 >> 20) & 0xf;
|
||||
let source =
|
||||
rom::PromSelection::select(architecture, implementation, RuntimeVariant::PfMonolithic)
|
||||
.map_err(|_| 7u32)?;
|
||||
let image = Storage::zeroed(rom::GV100_MAX_BYTES).ok_or(3u32)?;
|
||||
// Exclusively owned, GV100_MAX_BYTES long and zeroed until `image` drops.
|
||||
let buf = unsafe {
|
||||
core::slice::from_raw_parts_mut(image.pointer.cast::<u8>().as_ptr(), rom::GV100_MAX_BYTES)
|
||||
};
|
||||
source.capture(&mut *io.registers, buf).map_err(|_| 4u32)?;
|
||||
let parsed = rom_descriptors::parse_owned_gv100::<7>(buf);
|
||||
let Ok(list) = &parsed else {
|
||||
return Err(5);
|
||||
};
|
||||
let mut end = 0u32;
|
||||
for entry in list.entries() {
|
||||
end = end.max(entry.relative_offset.saturating_add(entry.bytes));
|
||||
}
|
||||
let (expansion, base) = gr_level::vbios_expansion(
|
||||
list.entries()
|
||||
.iter()
|
||||
.map(|e| (e.relative_offset, e.bytes, e.code_type)),
|
||||
);
|
||||
let len = (end as usize).min(rom::GV100_MAX_BYTES);
|
||||
let bit = match rom_bit::locate(&buf[..len], 0) {
|
||||
Ok(found) if found.status == NvStatus::OK => found.offset,
|
||||
_ => return Err(6),
|
||||
};
|
||||
Ok(PmuVbios {
|
||||
image,
|
||||
len,
|
||||
bit,
|
||||
base,
|
||||
expansion,
|
||||
})
|
||||
}
|
||||
|
||||
/// Zero-fills group `index`'s super-surface region and writes it from the VBIOS.
|
||||
/// Err codes: 1 index, 2 surface, 3 zero fill, 4 table not understood, 5 BAR2 write.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn pmu_write_group(
|
||||
io: &mut PmuIo<'_, '_>,
|
||||
vb: &gv100_pmu_tables::Vbios<'_>,
|
||||
index: usize,
|
||||
) -> Result<(), u32> {
|
||||
let g = gv100_pmu_tables::GROUPS.get(index).ok_or(1u32)?;
|
||||
let at = io
|
||||
.surface_at(g.surface, g.region as usize)
|
||||
.map_err(|_| 2u32)?;
|
||||
// black_box: a constant byte lets LLVM emit memset (refused in the module).
|
||||
io.bar2_bytes(at, g.region as usize, |_| black_box(0))
|
||||
.map_err(|_| 3u32)?;
|
||||
let mut failed = false;
|
||||
let built = gv100_pmu_tables::emit(vb, index, &mut |off, byte| {
|
||||
if io.bar2_bytes(at + off, 1, |_| black_box(byte)).is_err() {
|
||||
failed = true;
|
||||
}
|
||||
});
|
||||
if built.is_none() {
|
||||
return Err(4);
|
||||
}
|
||||
if failed {
|
||||
return Err(5);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes the PWR_DEVICE copy-out region before a power read.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn pmu_write_copy_out(io: &mut PmuIo<'_, '_>, mask: u32, count: u16) -> Result<(), ()> {
|
||||
let g = gv100_pmu_tables::COPY_OUT;
|
||||
let len = g.region as usize;
|
||||
io.surface_at(g.surface, len)
|
||||
.and_then(|at| {
|
||||
io.bar2_bytes(at, len, |i| {
|
||||
black_box(gv100_pmu_tables::copy_out_byte(mask, count, i))
|
||||
})
|
||||
})
|
||||
.map(|_| ())
|
||||
.map_err(|_| ())
|
||||
}
|
||||
|
||||
/// Sends built request `cmd` on the low queue, as stock does. Returns (status, or
|
||||
/// 0x8000_0000 | status on a transport error; PMU CPUCTL).
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn pmu_send_built(io: &mut PmuIo<'_, '_>, cmd: usize) -> (u32, u32) {
|
||||
let Some(len) = gv100_pmu_tables::request_len(cmd) else {
|
||||
return (0x8000_ffff, 0);
|
||||
};
|
||||
let buf = field!(io, power_req_buf);
|
||||
if len > buf.len() {
|
||||
return (0x8000_fffe, 0);
|
||||
}
|
||||
let mut i = 0usize;
|
||||
while i < len {
|
||||
buf[i] = black_box(gv100_pmu_tables::request_byte(cmd, i));
|
||||
i += 1;
|
||||
}
|
||||
let st = match {
|
||||
let transport = field!(io, transport);
|
||||
transport.execute_with_scratch(io, pmu_fbq::Queue::Low, &mut buf[..len], 0)
|
||||
} {
|
||||
Ok(st) => st.0,
|
||||
Err(st) => 0x8000_0000 | st.0,
|
||||
};
|
||||
(st, io.registers.read32(0x0010_a100).unwrap_or(0xffff_ffff))
|
||||
}
|
||||
|
||||
/// Device 0's (mW, mA) from the copy-out record the PMU fills (+0x1c mW, +0x20 mA,
|
||||
/// +0x24 uV).
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
fn pmu_status_record(io: &mut PmuIo<'_, '_>) -> Option<(u32, u32)> {
|
||||
let mut rec = [0u8; 8];
|
||||
io.surface_at(gv100_pmu_tables::COPY_OUT.surface + 0x1c, 8)
|
||||
.and_then(|at| io.bar2_read_bytes(at, &mut rec))
|
||||
.ok()?;
|
||||
Some((
|
||||
u32::from_le_bytes([rec[0], rec[1], rec[2], rec[3]]),
|
||||
u32::from_le_bytes([rec[4], rec[5], rec[6], rec[7]]),
|
||||
))
|
||||
}
|
||||
|
||||
/// One live board-power read (mW) for the monitor tick. None until this session's boot
|
||||
/// sequence completed.
|
||||
///
|
||||
/// A failed read (including a reply timeout, after which the transport refuses every
|
||||
/// later command, as the original does: a timed-out request may still act in the
|
||||
/// firmware) turns polling off for the rest of the session and is logged once (stage 8:
|
||||
/// status, CPUCTL), so the tick never keeps issuing commands to a PMU that stopped
|
||||
/// answering. The governor then falls back to its no-sensor behaviour.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn pmu_power_read(io: &mut PmuIo<'_, '_>) -> Option<u32> {
|
||||
let (mask, count) = (*field!(io, status_shape))?;
|
||||
let (st, cpuctl) = if pmu_write_copy_out(io, mask, count).is_ok() {
|
||||
let saved = *field!(io, budget);
|
||||
*field!(io, budget) = None;
|
||||
let sent = pmu_send_built(io, gv100_pmu_tables::COPY_OUT_CMD);
|
||||
*field!(io, budget) = saved;
|
||||
sent
|
||||
} else {
|
||||
(0x8000_00c0, 0)
|
||||
};
|
||||
let record = if st == 0 && cpuctl & 0x10 == 0 {
|
||||
pmu_status_record(io)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if record.is_none() {
|
||||
*field!(io, status_shape) = None;
|
||||
let gpu_id = unsafe { nv_rs_pci_gpu_id(io.registers.pci_token()) };
|
||||
unsafe { nv_rs_pmu_power_log(gpu_id, 8, st, cpuctl, 0) };
|
||||
}
|
||||
record.map(|(mw, _)| mw)
|
||||
}
|
||||
|
||||
/// Stock's boot sequence, built: the nine groups, each followed by its SET request, then
|
||||
/// three PWR_DEVICE copy-outs 1 s apart (as stock). Stops at the first failed command or
|
||||
/// halted PMU. Returns the commands completed; on success the copy-out shape is stored
|
||||
/// for `pmu_power_read`.
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn pmu_built_sequence(
|
||||
io: &mut PmuIo<'_, '_>,
|
||||
gpu_id: u32,
|
||||
vb: &gv100_pmu_tables::Vbios<'_>,
|
||||
) -> (u32, u32) {
|
||||
let log = |stage: u32, a: u32, b: u32, c: u32| unsafe {
|
||||
nv_rs_pmu_power_log(gpu_id, stage, a, b, c);
|
||||
};
|
||||
let Some((mask, count)) = gv100_pmu_tables::pwr_device_status_shape(vb) else {
|
||||
log(2, 0xffff, 0, 0);
|
||||
return (0, 0);
|
||||
};
|
||||
log(2, mask, u32::from(count), 0);
|
||||
let sets = gv100_pmu_tables::GROUPS.len() as u32;
|
||||
let mut k = 0u32;
|
||||
let mut mw = 0u32;
|
||||
while k < sets + 3 {
|
||||
let cmd = if k < sets {
|
||||
if let Err(e) = pmu_write_group(io, vb, k as usize) {
|
||||
log(3, k + 1, e, 0);
|
||||
return (k, 0);
|
||||
}
|
||||
k as usize
|
||||
} else {
|
||||
if pmu_write_copy_out(io, mask, count).is_err() {
|
||||
log(3, k + 1, 0xc0, 0);
|
||||
return (k, 0);
|
||||
}
|
||||
gv100_pmu_tables::COPY_OUT_CMD
|
||||
};
|
||||
let (st, cpuctl) = pmu_send_built(io, cmd);
|
||||
// Stage 4: (command number attempted, status, CPUCTL).
|
||||
log(4, k + 1, st, cpuctl);
|
||||
if st != 0 || cpuctl & 0x10 != 0 {
|
||||
// PMU ICD: PC, SP, r14. A halt here is usually the ucode rejecting a group
|
||||
// (it executes `exit` on bad input); the PC locates the check.
|
||||
if cpuctl & 0x10 != 0 {
|
||||
let mut icd = |i: u32| {
|
||||
let _ = io.registers.write32(0x0010_a200, (i << 8) | 8);
|
||||
io.registers.read32(0x0010_a20c).unwrap_or(0xffff_ffff)
|
||||
};
|
||||
let (pc, sp, r14) = (icd(0x15), icd(0x14), icd(0xe));
|
||||
log(6, pc, sp, r14);
|
||||
}
|
||||
// Only the commands that completed are counted.
|
||||
return (k, 0);
|
||||
}
|
||||
k += 1;
|
||||
if k > sets {
|
||||
// Stock reads the record about 1 s after each copy-out.
|
||||
let mut t = 0;
|
||||
while t < 1000 {
|
||||
unsafe { nv_rs_os_busy_us(1_000) };
|
||||
t += 1;
|
||||
}
|
||||
let (p, a) = pmu_status_record(io).unwrap_or((0, 0));
|
||||
mw = p;
|
||||
log(5, k, p, a);
|
||||
}
|
||||
}
|
||||
*field!(io, status_shape) = Some((mask, count));
|
||||
(k, mw)
|
||||
}
|
||||
|
||||
/// Runs once per GPU after the PMU is up (only with `v100_pmu_power=1`, only on GV100).
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[inline(never)]
|
||||
fn runtime_pmu_power_bringup(io: &mut PmuIo<'_, '_>) {
|
||||
let gpu_id = unsafe { nv_rs_pci_gpu_id(io.registers.pci_token()) };
|
||||
// Taken here, so the image is freed when this returns.
|
||||
let Some(captured) = field!(io, pmu_vbios).take() else {
|
||||
return;
|
||||
};
|
||||
let Some(vb) = gv100_pmu_tables::Vbios::new(
|
||||
captured.bytes(),
|
||||
captured.bit,
|
||||
captured.base,
|
||||
captured.expansion,
|
||||
) else {
|
||||
unsafe { nv_rs_pmu_power_log(gpu_id, 2, 0xffff, 1, 0) };
|
||||
return;
|
||||
};
|
||||
let saved = *field!(io, budget);
|
||||
*field!(io, budget) = None;
|
||||
let (done, mw) = pmu_built_sequence(io, gpu_id, &vb);
|
||||
*field!(io, budget) = saved;
|
||||
unsafe { nv_rs_pmu_power_log(gpu_id, 7, done, mw, 0) };
|
||||
}
|
||||
|
||||
#[cfg(not(nv_rs_edu_guest))]
|
||||
#[allow(dead_code)]
|
||||
#[inline(never)]
|
||||
|
||||
@ -102,6 +102,7 @@ pub mod gsp_ucode;
|
||||
pub mod gv100_ce_scrub;
|
||||
pub mod gv100_irq_service;
|
||||
pub mod gv100_p0_boost;
|
||||
pub mod gv100_pmu_tables;
|
||||
pub mod hbm_clock;
|
||||
pub mod hdcp;
|
||||
pub mod host_time;
|
||||
|
||||
1119
src/nvidia/src/gpu/gv100_pmu_tables.rs
Normal file
1119
src/nvidia/src/gpu/gv100_pmu_tables.rs
Normal file
File diff suppressed because it is too large
Load Diff
271
src/nvidia/src/gpu/gv100_pmu_tables_tests.rs
Normal file
271
src/nvidia/src/gpu/gv100_pmu_tables_tests.rs
Normal file
@ -0,0 +1,271 @@
|
||||
use super::*;
|
||||
extern crate std;
|
||||
use std::vec;
|
||||
use std::vec::Vec;
|
||||
|
||||
/// Builds every group into a buffer laid out like the super-surface.
|
||||
fn build(vb: &Vbios<'_>) -> Vec<(usize, Vec<u8>)> {
|
||||
let mut out = Vec::new();
|
||||
for (i, g) in GROUPS.iter().enumerate() {
|
||||
let mut buf = vec![0u8; g.region as usize];
|
||||
emit(vb, i, &mut |at, byte| buf[at as usize] = byte).expect("group builds");
|
||||
out.push((g.surface as usize, buf));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Bytes that differ from stock on the reference card: none since 3 Dec (all nine
|
||||
/// groups byte-identical; several fields are [O] constants, see the module).
|
||||
const UNRESOLVED_BYTES: usize = 0;
|
||||
|
||||
/// Needs the reference card's VBIOS and stock super-surface (not in the repo; stock bytes
|
||||
/// are not redistributed): NV_RS_V100_VBIOS=<vbios.rom> NV_RS_V100_SURF=<surface.bin>,
|
||||
/// the surface being the super-surface read back after stock's ninth group SET.
|
||||
/// The legacy image is 0xe400 bytes and the extended image starts at 0x1fc00 there.
|
||||
#[test]
|
||||
fn matches_reference_stock_surface_when_fixture_present() {
|
||||
let (Ok(rom), Ok(surf)) = (
|
||||
std::env::var("NV_RS_V100_VBIOS"),
|
||||
std::env::var("NV_RS_V100_SURF"),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
let rom = std::fs::read(rom).unwrap();
|
||||
let surf = std::fs::read(surf).unwrap();
|
||||
let bit = (0..rom.len() - 4)
|
||||
.find(|&i| &rom[i..i + 4] == b"BIT\0")
|
||||
.unwrap() as u32
|
||||
- 2;
|
||||
let vb = Vbios::new(&rom, bit, 0xe400, 0x1fc00 - 0xe400).expect("BIT 'P' found");
|
||||
let mut differ = 0;
|
||||
for ((surface, buf), g) in build(&vb).iter().zip(GROUPS.iter()) {
|
||||
let d = buf
|
||||
.iter()
|
||||
.zip(&surf[*surface..*surface + buf.len()])
|
||||
.filter(|(a, b)| a != b)
|
||||
.count();
|
||||
std::println!("class {:02x}: {} bytes differ of {}", g.class, d, buf.len());
|
||||
differ += d;
|
||||
}
|
||||
assert_eq!(differ, UNRESOLVED_BYTES);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_an_empty_image_without_panicking() {
|
||||
let rom = [0u8; 64];
|
||||
assert!(Vbios::new(&rom, 0, 0, 0).is_none());
|
||||
}
|
||||
|
||||
/// A pseudo-random VBIOS whose BIT 'P' slots (and the DCB I2C device table) point at
|
||||
/// in-ROM tables with small headers, so the serializers run on junk entries. Bytes lean
|
||||
/// towards small values so entry types hit the cases the serializers handle.
|
||||
fn random_rom(seed: u64) -> Vec<u8> {
|
||||
const LEN: usize = 0x4000;
|
||||
let mut x = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1;
|
||||
let mut next = move || {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
x
|
||||
};
|
||||
let mut rom = vec![0u8; LEN];
|
||||
for b in rom.iter_mut() {
|
||||
let r = next();
|
||||
*b = if r & 1 == 0 {
|
||||
(r >> 8) as u8 % 8
|
||||
} else {
|
||||
(r >> 8) as u8
|
||||
};
|
||||
}
|
||||
// BIT header at 0 (12 bytes, 6-byte tokens, one token): 'P' v2, 0x70 bytes at 0x40.
|
||||
rom[0..12].copy_from_slice(&[0xff, 0xb8, b'B', b'I', b'T', 0, 0, 0, 12, 6, 1, 0]);
|
||||
rom[12..18].copy_from_slice(&[0x50, 2, 0x70, 0, 0x40, 0]);
|
||||
// DCB at 0x20; its I2C device table pointer (+0x12) is the eleventh table below.
|
||||
rom[0x36..0x38].copy_from_slice(&0x20u16.to_le_bytes());
|
||||
let slots = [0x00, 0x20, 0x28, 0x2c, 0x38, 0x3c, 0x40, 0x44, 0x4c, 0x6c];
|
||||
let mut k = 0;
|
||||
while k <= slots.len() {
|
||||
let at = 0x100 + k * 0x600;
|
||||
if k < slots.len() {
|
||||
let p = 0x40 + slots[k];
|
||||
rom[p..p + 4].copy_from_slice(&(at as u32).to_le_bytes());
|
||||
} else {
|
||||
rom[0x32..0x34].copy_from_slice(&(at as u16).to_le_bytes());
|
||||
}
|
||||
// Header: size 6..=9, then sizes and counts that pass the guards most of the time.
|
||||
let r = next();
|
||||
rom[at + 1] = 6 + (r % 4) as u8;
|
||||
let mut j = 2;
|
||||
while j < 6 {
|
||||
rom[at + j] = (next() % 48) as u8;
|
||||
j += 1;
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
rom
|
||||
}
|
||||
|
||||
/// Junk VBIOS tables: every group is refused or written inside its region, never a
|
||||
/// panic (an overflowing offset or shift panics here, as tests build with overflow
|
||||
/// checks), and every serializer really writes on some inputs, so the check is live.
|
||||
#[test]
|
||||
fn writes_stay_inside_each_region() {
|
||||
let mut written = [0usize; GROUPS.len()];
|
||||
let mut seed = 1u64;
|
||||
while seed <= 4000 {
|
||||
let rom = random_rom(seed);
|
||||
let vb = Vbios::new(&rom, 0, rom.len() as u32, 0).expect("token found");
|
||||
for (i, g) in GROUPS.iter().enumerate() {
|
||||
let mut n = 0usize;
|
||||
let _ = emit(&vb, i, &mut |at, _| {
|
||||
assert!(at < g.region, "class {:02x} wrote {at:#x}", g.class);
|
||||
n += 1;
|
||||
});
|
||||
written[i] += n;
|
||||
}
|
||||
// The copy-out shape and the SET group come from one reading of the table.
|
||||
if let Some((mask, _)) = pwr_device_status_shape(&vb) {
|
||||
let mut hdr = [0u8; 12];
|
||||
let idx = GROUPS.iter().position(|g| g.class == 0x28).unwrap();
|
||||
if emit(&vb, idx, &mut |at, b| {
|
||||
if (at as usize) < hdr.len() {
|
||||
hdr[at as usize] = b;
|
||||
}
|
||||
})
|
||||
.is_some()
|
||||
{
|
||||
assert_eq!(hdr[8..12], mask.to_le_bytes());
|
||||
}
|
||||
}
|
||||
seed += 1;
|
||||
}
|
||||
for (g, n) in GROUPS.iter().zip(written) {
|
||||
assert!(n > 0, "class {:02x} never wrote a byte", g.class);
|
||||
}
|
||||
}
|
||||
|
||||
/// Stock's twelve recorded requests (the nine SETs and three PWR_DEVICE copy-outs) have
|
||||
/// these non-zero bytes and lengths, nothing else. [O]
|
||||
#[test]
|
||||
fn requests_match_the_recorded_stock_requests() {
|
||||
let stock: [(usize, u8, u8, u8); 10] = [
|
||||
(0x434, 0x13, 0x1c, 0),
|
||||
(0x124, 0x13, 0x1b, 0),
|
||||
(0x1f4, 0x13, 0x19, 0),
|
||||
(0x124, 0x13, 0x1a, 0),
|
||||
(0x24, 0x18, 0x2a, 0),
|
||||
(0x44, 0x18, 0x26, 0),
|
||||
(0x404, 0x18, 0x27, 0),
|
||||
(0x164, 0x18, 0x28, 0),
|
||||
(0x1f4, 0x18, 0x29, 0),
|
||||
(0x74, 0x18, 0x28, 1),
|
||||
];
|
||||
for (cmd, &(len, unit, class, copy_out)) in stock.iter().enumerate() {
|
||||
assert_eq!(request_len(cmd), Some(len), "cmd {cmd}");
|
||||
assert!(len <= REQUEST_MAX);
|
||||
for i in 0..len {
|
||||
let want = match i {
|
||||
1 => unit,
|
||||
4 => 0x1b,
|
||||
0x10 => class,
|
||||
0x11 => copy_out,
|
||||
_ => 0,
|
||||
};
|
||||
assert_eq!(request_byte(cmd, i), want, "cmd {cmd} byte {i:#x}");
|
||||
}
|
||||
}
|
||||
assert_eq!(request_len(COPY_OUT_CMD + 1), None);
|
||||
}
|
||||
|
||||
/// Stock's copy-out region on the reference card has exactly nine non-zero bytes. [O]
|
||||
#[test]
|
||||
fn copy_out_region_matches_the_recorded_stock_span() {
|
||||
let mut nonzero = Vec::new();
|
||||
for i in 0..COPY_OUT.region as usize {
|
||||
let b = copy_out_byte(0x83, 7, i);
|
||||
if b != 0 {
|
||||
nonzero.push((i, b));
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
nonzero,
|
||||
vec![
|
||||
(0, 0x01),
|
||||
(1, 0xff),
|
||||
(2, 0x28),
|
||||
(4, 0x08),
|
||||
(8, 0x83),
|
||||
(0xc10, 0x01),
|
||||
(0xc11, 0xff),
|
||||
(0xc12, 0xff),
|
||||
(0xc14, 0x07),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_shape_matches_reference_card_when_fixture_present() {
|
||||
let Ok(rom) = std::env::var("NV_RS_V100_VBIOS") else {
|
||||
return;
|
||||
};
|
||||
let rom = std::fs::read(rom).unwrap();
|
||||
let bit = (0..rom.len() - 4)
|
||||
.find(|&i| &rom[i..i + 4] == b"BIT\0")
|
||||
.unwrap() as u32
|
||||
- 2;
|
||||
let vb = Vbios::new(&rom, bit, 0xe400, 0x1fc00 - 0xe400).unwrap();
|
||||
assert_eq!(pwr_device_status_shape(&vb), Some((0x83, 7)));
|
||||
}
|
||||
|
||||
/// A BIT header at 0 with one 'P' v2 token whose data (at 0x40) is `p_size` bytes long.
|
||||
fn rom_with_p(p_size: u16) -> Vec<u8> {
|
||||
let mut rom = vec![0u8; 0x400];
|
||||
rom[0..12].copy_from_slice(&[0xff, 0xb8, b'B', b'I', b'T', 0, 0, 0, 12, 6, 1, 0]);
|
||||
let [lo, hi] = p_size.to_le_bytes();
|
||||
rom[12..18].copy_from_slice(&[0x50, 2, lo, hi, 0x40, 0]);
|
||||
rom
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_slots_past_the_declared_p_data_are_absent() {
|
||||
let mut rom = rom_with_p(4);
|
||||
// Slot 0x3c holds a plausible in-ROM pointer, but the token declares 4 data bytes.
|
||||
rom[0x40 + 0x3c..0x40 + 0x40].copy_from_slice(&0x100u32.to_le_bytes());
|
||||
let vb = Vbios::new(&rom, 0, 0x400, 0).expect("token found");
|
||||
assert_eq!(vb.table(0x3c), None);
|
||||
assert_eq!(vb.table(0x00), Some(0));
|
||||
let mut rom = rom_with_p(0x40);
|
||||
rom[0x40 + 0x3c..0x40 + 0x40].copy_from_slice(&0x100u32.to_le_bytes());
|
||||
let vb = Vbios::new(&rom, 0, 0x400, 0).expect("token found");
|
||||
assert_eq!(vb.table(0x3c), Some(0x100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparse_type_tables_are_refused() {
|
||||
let t = |types: &'static [u8]| move |k: usize| types.get(k).copied();
|
||||
assert_eq!(contiguous_count(3, t(&[1, 1, 0])), Some(2));
|
||||
assert_eq!(contiguous_count(3, t(&[2, 2, 2])), Some(3));
|
||||
assert_eq!(contiguous_count(3, t(&[0, 0, 0])), Some(0));
|
||||
assert_eq!(contiguous_count(3, t(&[1, 0, 1])), None);
|
||||
assert_eq!(contiguous_count(3, t(&[2, 0, 2])), None);
|
||||
assert_eq!(contiguous_count(3, t(&[1, 1])), None, "short table");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pstate_sub_entries_that_spill_into_the_next_record_are_refused() {
|
||||
// P slot 0x00 -> pstate table at 0x100: header 6 bytes, base 3, 1 entry, sub 6 bytes.
|
||||
let pstate = |sn: u8| {
|
||||
let mut rom = rom_with_p(0x80);
|
||||
rom[0x40..0x44].copy_from_slice(&0x100u32.to_le_bytes());
|
||||
rom[0x100..0x106].copy_from_slice(&[0x10, 6, 3, 1, 6, sn]);
|
||||
rom
|
||||
};
|
||||
let pstate_index = GROUPS.iter().position(|g| g.class == 0x1c).unwrap();
|
||||
for (sn, fits) in [(86u8, true), (87, false)] {
|
||||
let mut rom = pstate(sn);
|
||||
rom.resize(0x2000, 0);
|
||||
let vb = Vbios::new(&rom, 0, 0x2000, 0).expect("token found");
|
||||
let built = emit(&vb, pstate_index, &mut |_, _| {});
|
||||
assert_eq!(built.is_some(), fits, "sn = {sn}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user