Files
nvdriver/kernel-open/nvidia/gpu_owner.c
morebrackets f3838d692d gv100/hbm: survey only the FBPAs the FBIO floorsweep fuse leaves enabled (#399) (#583)
* gv100/hbm: survey only the FBPAs the FBIO floorsweep fuse leaves enabled

survey_gv100_hbm() demanded all 16 FBPA PLLs readable, locked and equal.
A CMP 100-210 (10de:1df4, 12 GB, three HBM2 stacks) has FBPA 0..=3 fused
off: their PLL cfg/coeff registers answer 0xbadf2010 (PRI bus error)
while FBPA 4..=15 read cfg 0x29, coeff 0x14102 (877.5 MHz). The survey
refused the card (CfgUnreadable at FBPA 0), so the HBM2 session never got
a baseline and v100_hbm_ndiv was a no-op. program_coeff() made the same
assumption twice more: its post-write probe read FBPA 0, and its lock
wait polled all 16.

1. Survey mask. 580.178.04 does not probe for bus errors; it computes the
   enabled-FBPA mask from the fuse. _nv030402rm (GV100 GPU HAL slot
   +0x3a0) [D]:
     n    = RD32(0x2243c)                      NV_PTOP_SCAL_NUM_FBPAS
     fuse = OBJFUSE+0x118(0xff000033)          _nv029006rm
            -> OBJFUSE+0x110 _nv028950rm: fuse word 0xc14, shift 0,
               mask 0xffff, i.e. BAR0 0x21c14
     mask = zext64(~fuse); if n <= 64: mask &= ~0 >> (64 - n); n == 0: 0
   A stock mmiotrace of a Tesla V100 shows that pair once, 0x2243c = 0x10
   then 0x21c14 = 0 [O] (reports/original-mmiotrace-b3-first/b3-bar0.txt),
   which gives 0xffff. On the #399 card the reporter read 0x2243c = 0x10
   and 0x21c14 = 0xf [O], which gives 0xfff0 and matches the partitions
   that answer.
2. Post-write probe on the first enabled FBPA instead of FBPA 0.
3. Lock wait polls only the enabled FBPAs.
   Stock never retunes the HBM2 PLL, so 2 and 3 have no stock evidence;
   they follow from 1. On silicon [O]: the reporter ran 23db150 with the
   equivalent change (presence taken from the bus-error word rather than
   the fuse) on the #399 card and got a verified 877.5 -> 1107 MHz unlock
   (NDIV 65 -> 82) on FBPA 4..=15, D2D 618 -> 781 GB/s, then adaptive
   backoff on real ECC DBE/SBE and a clean return to NDIV 65
   (https://github.com/QuixiAI/nvdriver/issues/399#issuecomment-7386631509).
   This exact commit has not run on silicon.

fbio_enabled_mask() is the stock arithmetic and gv100_enabled_fbpa_mask()
the two reads, in stock order. An enabled partition must still be
readable, locked and in agreement (the reference coefficient is the first
enabled partition's), so a card whose fuse says 16 but whose FBPA 0 does
not answer is refused as before. The mask is resolved before the
multicast write; if 0x2243c is unreadable, 0 or above 16, or the fuse is
unreadable or enables nothing, the survey reports the new fault kinds
FbpaCountUnusable (6) / FbioFuseUnusable (7) and nothing is written. A
mask lost between the survey and the write is a refusal before the target
write, like the PMC_BOOT_0 canary: logged as SurveyFailed, not RolledBack.
gpu_owner.c logs kinds 6 and 7, and the CoeffMismatch text now says "the
first enabled FBPA" instead of "FBPA 0". A 16-partition card does two
more BAR0 reads per survey and per program_coeff and otherwise behaves as
before. The ECC poll and the backoff logic are unchanged.

Tests: fbio_enabled_mask_matches_nv030402rm_arithmetic,
gv100_hbm_survey_and_unlock_skip_fused_off_fbpas (mock with FBPA 0..=3
reading 0xbadf2010, fuse 0xf: survey, unlock 65 -> 82, backoff step,
restore; refusals with zero writes for unusable mask inputs), and
gv100_hbm_mask_lost_before_the_target_write_is_not_a_rollback. Without
the change the first survey assertion fails; reverting only the probe or
only the lock wait makes the unlock fail.

Refs #399

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* gv100/hbm: fall back to cached floorsweep mask on rollback/restore and log teardown failure

Addresses CodeRabbit review finding on PR #583:
- Cache the static silicon enabled-FBPA mask resolved during baseline survey
  (HbmSession::enabled_fbpa_mask and inside unlock/set rollback paths).
- If re-reading PTOP_SCAL_NUM_FBPAS (0x2243c) or FUSE_OPT_FBIO (0x21c14)
  glitches during a rollback or teardown restore, fall back to the cached
  mask so the baseline HBM2 coefficient write is not blocked while PMC_BOOT_0
  canary remains strictly enforced.
- Log failed HBM2 teardown restores in pmu_bringup.rs via log_hbm_outcome
  instead of discarding the result.
- Add unit test covering fuse-glitch teardown restore and canary refusal.

* gv100/hbm: use cached floorsweep mask for downward safety steps (ECC backoff / baseline return)

Addresses follow-up CodeRabbit finding on PR #583:
- In HbmSession::step_to, downward safety steps (target <= current clock:
  live ECC backoff via on_instability and sysfs return to baseline) fall
  back to the cached static silicon enabled-FBPA mask on a transient read
  failure of 0x2243c/0x21c14 so an elevated clock is never stranded while
  the GPU remains in service.
- Upward overclock steps continue to require a fresh fuse read and refuse
  before writing when unreadable.
- Extend unit test to cover on_instability and on_user_write(0) under a
  fuse-read glitch.

* gv100/hbm: skip rollback write on pre-write mask refusal

Addresses CodeRabbit trivial finding on PR #583:
- When program_coeff refuses before the target write due to an unreadable
  FBPA count (FbpaCountUnusable) or FBIO fuse (FbioFuseUnusable), skip the
  unnecessary rollback multicast write so the PLL stays completely untouched
  (writes == 0) and a clean refusal can never degrade into FailedUnverified.
- Preserve existing pre-write canary refusal behavior unchanged.

---------

Co-authored-by: Scottcjn <121303252+Scottcjn@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: morebrackets <19673572+morebrackets@users.noreply.github.com>
2026-12-05 00:18:44 -08:00

1265 lines
51 KiB
C

// SPDX-License-Identifier: MIT
/* OS allocation/workqueue adapters for Rust's final-reference cleanup protocol. */
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/kthread.h>
#include <linux/sched/task.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/preempt.h>
#include <linux/spinlock.h>
#include <linux/pci.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/version.h>
#include <linux/firmware.h>
#include "pci_adapter.h"
#include "gpu_owner.h"
struct nv_rs_gpu_owner {
struct nv_rs_pci *pci;
void *gpu;
struct delayed_work retry;
struct mutex operation;
atomic_t operation_waiters;
struct nv_rs_gpu_key key;
struct miscdevice open_device;
bool open_registered;
bool registered,owns_pci;
struct mutex runtime_lock;
struct completion runtime_ready;
struct completion runtime_stop;
struct completion runtime_stopped;
struct completion runtime_gr_reset_done;
struct completion runtime_hshub_done;
wait_queue_head_t runtime_wq;
atomic_t runtime_starting;
struct task_struct *runtime_task;
bool runtime_started;
bool runtime_parked;
bool runtime_stop_req;
bool runtime_stopping;
bool aer_recovering;
bool runtime_gr_reset_req;
int runtime_gr_reset_rc;
bool runtime_hshub_req;
u32 runtime_hshub_cfg[4];
int runtime_hshub_rc;
int runtime_error;
};
static DEFINE_SPINLOCK(registry_lock);
static DEFINE_MUTEX(rm_api);
static bool hardware_inventory;
module_param(hardware_inventory, bool, 0400);
MODULE_PARM_DESC(hardware_inventory, "Log a read-only register inventory at first open");
bool nv_rs_hardware_inventory(void) { return hardware_inventory; }
static unsigned int bringup;
module_param(bringup, uint, 0400);
MODULE_PARM_DESC(bringup, "Faithful first-open stage bring-up bound (stage::pre_init -> stage::init -> stage::load): 0 software-only gate (no register writes), 1 MC engine enable, 2 adds timer init, 3 pre-stage engine reset first, 4 adds BAR2 setup, 5 adds NVDEC HS Falcon run + reset, 6 adds PFIFO host load + unload, 7 adds Graphics InitLocked/StateLoad/StateUnload with BAR2 bound, 8 adds OBJACR Load/Unload on SEC2/PMU, 9 adds LS PMU/SEC2/FECS/GPCCS + golden GR context + reverse-order unload");
u32 nv_rs_bringup_level(void) { return bringup; }
static bool bringup_bar1;
module_param(bringup_bar1, bool, 0400);
MODULE_PARM_DESC(bringup_bar1, "With bringup >= 4: during first-open stage::load while BAR2 is bound, run BAR1 KernelBus PostLoad setup, proof mapping, and unbind (notes 4231xxxx). Off: levels 1 to 9 make no BAR1 access");
bool nv_rs_bringup_bar1(void) { return bringup_bar1; }
#ifdef NV_RS_PCI_TEST_EDU
static bool runtime_engines = false;
#else
static bool runtime_engines = true;
#endif
module_param(runtime_engines, bool, 0400);
MODULE_PARM_DESC(runtime_engines, "Run faithful first-open stages through Level 9 + BAR1 and keep engines live across RM API / UVM sessions (default: 1); tears down on remove");
bool nv_rs_runtime_engines(void) { return runtime_engines; }
static bool live_pci_irq = true;
module_param(live_pci_irq, bool, 0444);
MODULE_PARM_DESC(live_pci_irq, "Storm-guarded PCI IRQ arming in LiveEngine (default on; pass 0 to disable)");
bool nv_rs_live_pci_irq(void) { return live_pci_irq; }
static unsigned int v100_hbm_ndiv = 0;
static atomic_t v100_hbm_ndiv_gen = ATOMIC_INIT(0);
static int v100_hbm_ndiv_set(const char *val, const struct kernel_param *kp)
{
int ret = param_set_uint(val, kp);
if (ret == 0) {
smp_wmb();
atomic_inc(&v100_hbm_ndiv_gen);
}
return ret;
}
static const struct kernel_param_ops v100_hbm_ndiv_ops = {
.set = v100_hbm_ndiv_set,
.get = param_get_uint,
};
module_param_cb(v100_hbm_ndiv, &v100_hbm_ndiv_ops, &v100_hbm_ndiv, 0644);
MODULE_PARM_DESC(v100_hbm_ndiv, "Live GV100/V100 HBM2 PLL NDIV target (runtime-writable via sysfs; default 0=stock 877.5MHz safe across all silicon bins; opt-in 82=1107MHz V100S +26%, 80=1080MHz +23%, 78=1053MHz +20%, 67=905MHz)");
u32 nv_rs_v100_hbm_ndiv(void) { return READ_ONCE(v100_hbm_ndiv); }
u32 nv_rs_v100_hbm_ndiv_gen(void)
{
u32 gen = (u32)atomic_read(&v100_hbm_ndiv_gen);
smp_rmb();
return gen;
}
static char v100_hbm_ndiv_map[256] = "";
static atomic_t v100_hbm_ndiv_map_gen = ATOMIC_INIT(0);
static int v100_hbm_ndiv_map_set(const char *val, const struct kernel_param *kp)
{
int ret = param_set_copystring(val, kp);
if (ret == 0) {
smp_wmb();
atomic_inc(&v100_hbm_ndiv_map_gen);
}
return ret;
}
static const struct kernel_param_ops v100_hbm_ndiv_map_ops = {
.set = v100_hbm_ndiv_map_set,
.get = param_get_string,
};
static struct kparam_string v100_hbm_ndiv_map_kps = {
.maxlen = sizeof(v100_hbm_ndiv_map),
.string = v100_hbm_ndiv_map,
};
module_param_cb(v100_hbm_ndiv_map, &v100_hbm_ndiv_map_ops, &v100_hbm_ndiv_map_kps, 0644);
MODULE_PARM_DESC(v100_hbm_ndiv_map, "Optional per-PCI-BDF GV100 HBM2 NDIV overrides, e.g. '0000:81:00.0=67,82:00.0=82' (runtime-writable via sysfs)");
u32 nv_rs_v100_hbm_ndiv_map_gen(void)
{
u32 gen = (u32)atomic_read(&v100_hbm_ndiv_map_gen);
smp_rmb();
return gen;
}
size_t nv_rs_v100_hbm_ndiv_map_copy(u8 *out, size_t cap)
{
size_t n;
if (!out || !cap)
return 0;
kernel_param_lock(THIS_MODULE);
n = strnlen(v100_hbm_ndiv_map, sizeof(v100_hbm_ndiv_map));
if (n > cap)
n = cap;
memcpy(out, v100_hbm_ndiv_map, n);
kernel_param_unlock(THIS_MODULE);
return n;
}
void nv_rs_hbm_clock_log(u32 from_khz, u32 to_khz, u32 from_ndiv, u32 to_ndiv, u32 lock_us)
{
pr_info("nvidia_rs: GV100 HBM2 clock unlocked: %u.%u MHz -> %u.%u MHz (NDIV %u -> %u, locked in %u us)\n",
from_khz / 1000, (from_khz % 1000) / 100,
to_khz / 1000, (to_khz % 1000) / 100,
from_ndiv, to_ndiv, lock_us);
}
static unsigned int p100_hbm_ndiv = 0;
module_param(p100_hbm_ndiv, uint, 0644);
MODULE_PARM_DESC(p100_hbm_ndiv, "Opt-in GP100 (Tesla P100 / CMP 100-100) HBM2 PLL NDIV target (default 0=stock 715.5MHz; range 45..66 at 13.5MHz/step; restored on teardown, #1 #170)");
u32 nv_rs_p100_hbm_ndiv(void) { return READ_ONCE(p100_hbm_ndiv); }
static char p100_hbm_ndiv_map[256] = "";
module_param_string(p100_hbm_ndiv_map, p100_hbm_ndiv_map, sizeof(p100_hbm_ndiv_map), 0644);
MODULE_PARM_DESC(p100_hbm_ndiv_map, "Optional per-PCI-BDF GP100 HBM2 NDIV overrides, e.g. '0000:01:00.0=58,02:00.0=0' (runtime-writable via sysfs, #1 #170)");
size_t nv_rs_p100_hbm_ndiv_map_copy(u8 *out, size_t cap)
{
size_t n;
size_t i;
if (!out || !cap)
return 0;
kernel_param_lock(THIS_MODULE);
n = strnlen(p100_hbm_ndiv_map, sizeof(p100_hbm_ndiv_map));
if (n > cap)
n = cap;
for (i = 0; i < n; i++) {
WRITE_ONCE(out[i], (u8)p100_hbm_ndiv_map[i]);
barrier();
}
kernel_param_unlock(THIS_MODULE);
return n;
}
static unsigned int ga100_hbm_ndiv = 0;
module_param(ga100_hbm_ndiv, uint, 0644);
MODULE_PARM_DESC(ga100_hbm_ndiv, "Opt-in GA100 (A100 / CMP 170HX) HBM2e PLL NDIV target (default 0=leave the device clock unchanged; requires unlocked PLM WRITE_L0; range 30..80 at 27MHz/step; restored on teardown, #170)");
u32 nv_rs_ga100_hbm_ndiv(void) { return READ_ONCE(ga100_hbm_ndiv); }
static char ga100_hbm_ndiv_map[256] = "";
module_param_string(ga100_hbm_ndiv_map, ga100_hbm_ndiv_map, sizeof(ga100_hbm_ndiv_map), 0644);
MODULE_PARM_DESC(ga100_hbm_ndiv_map, "Optional per-PCI-BDF GA100 HBM2e NDIV overrides, e.g. '0000:01:00.0=70,02:00.0=0' (runtime-writable via sysfs, #170)");
size_t nv_rs_ga100_hbm_ndiv_map_copy(u8 *out, size_t cap)
{
size_t n;
size_t i;
if (!out || !cap)
return 0;
kernel_param_lock(THIS_MODULE);
n = strnlen(ga100_hbm_ndiv_map, sizeof(ga100_hbm_ndiv_map));
if (n > cap)
n = cap;
for (i = 0; i < n; i++) {
WRITE_ONCE(out[i], (u8)ga100_hbm_ndiv_map[i]);
barrier();
}
kernel_param_unlock(THIS_MODULE);
return n;
}
void nv_rs_hbm_clock_arch_log(u32 arch_id, u32 from_khz, u32 to_khz, u32 from_ndiv, u32 to_ndiv, u32 lock_us)
{
const char *name = (arch_id == 1) ? "GP100 HBM2" : (arch_id == 3) ? "GA100 HBM2e" : "GV100 HBM2";
pr_info("nvidia_rs: %s clock tuned: %u.%u MHz -> %u.%u MHz (NDIV %u -> %u, locked in %u us)\n",
name,
from_khz / 1000, (from_khz % 1000) / 100,
to_khz / 1000, (to_khz % 1000) / 100,
from_ndiv, to_ndiv, 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)
{
char reasons[96];
int pos = 0;
reasons[0] = '\0';
if (reason_mask & (1u << 1))
pos += scnprintf(reasons + pos, sizeof(reasons) - pos, "%sFBPA DRAM ECC DBE",
pos ? ", " : "");
if (reason_mask & (1u << 0))
pos += scnprintf(reasons + pos, sizeof(reasons) - pos, "%sFBPA DRAM ECC SBE",
pos ? ", " : "");
if (reason_mask & (1u << 2))
pos += scnprintf(reasons + pos, sizeof(reasons) - pos, "%sCE scrub timeout",
pos ? ", " : "");
if (reason_mask & (1u << 3))
pos += scnprintf(reasons + pos, sizeof(reasons) - pos, "%sECC poison Xid",
pos ? ", " : "");
if (pos == 0)
scnprintf(reasons, sizeof(reasons), "unknown (0x%x)", reason_mask);
pr_warn("nvidia_rs: GV100 HBM2 adaptive backoff: %u.%u MHz -> %u.%u MHz (NDIV %u -> %u, locked in %u us, reason: %s)\n",
from_khz / 1000, (from_khz % 1000) / 100,
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 the first enabled FBPA",
fault_fbpa, fault_value);
break;
case 6:
scnprintf(fault, sizeof(fault), "FBPA count 0x2243c unusable (0x%08x)", fault_value);
break;
case 7:
scnprintf(fault, sizeof(fault), "FBIO floorsweep fuse 0x21c14 unusable (0x%08x)",
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)");
u32 nv_rs_v100_p0_gpc_ndiv(void) { return READ_ONCE(v100_p0_gpc_ndiv); }
static char v100_p0_gpc_ndiv_map[256] = "";
module_param_string(v100_p0_gpc_ndiv_map, v100_p0_gpc_ndiv_map, sizeof(v100_p0_gpc_ndiv_map), 0644);
MODULE_PARM_DESC(v100_p0_gpc_ndiv_map, "Optional per-PCI-BDF GV100 P0 GPC NDIV overrides, e.g. '0000:81:00.0=172,82:00.0=190' (runtime-writable via sysfs)");
size_t nv_rs_v100_p0_gpc_ndiv_map_copy(u8 *out, size_t cap)
{
size_t n;
if (!out || !cap)
return 0;
kernel_param_lock(THIS_MODULE);
n = strnlen(v100_p0_gpc_ndiv_map, sizeof(v100_p0_gpc_ndiv_map));
if (n > cap)
n = cap;
memcpy(out, v100_p0_gpc_ndiv_map, n);
kernel_param_unlock(THIS_MODULE);
return n;
}
static unsigned int v100_p0_volt_uv = 0;
module_param(v100_p0_volt_uv, uint, 0444);
MODULE_PARM_DESC(v100_p0_volt_uv, "Live GV100/V100 PMU VOLT LOGIC rail target voltage in uV (0=auto V/F curve, clamped to [800000, 1050000] uV)");
u32 nv_rs_v100_p0_volt_uv(void) {
if (v100_p0_volt_uv == 0)
return 0;
if (v100_p0_volt_uv < 800000u)
return 800000u;
if (v100_p0_volt_uv > 1050000u)
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)");
u32 nv_rs_force_pcie_gen(void) { return READ_ONCE(force_pcie_gen); }
static char force_pcie_gen_map[256] = "";
module_param_string(force_pcie_gen_map, force_pcie_gen_map, sizeof(force_pcie_gen_map), 0644);
MODULE_PARM_DESC(force_pcie_gen_map, "Optional per-PCI-BDF PCIe link-speed overrides, e.g. '0000:01:00.0=2,02:00.0=3' (0 disables on that BDF, #168)");
size_t nv_rs_force_pcie_gen_map_copy(u8 *out, size_t cap)
{
size_t n, i;
if (!out || cap == 0)
return 0;
kernel_param_lock(THIS_MODULE);
n = strnlen(force_pcie_gen_map, sizeof(force_pcie_gen_map));
if (n > cap)
n = cap;
for (i = 0; i < n; i++)
out[i] = (u8)force_pcie_gen_map[i];
kernel_param_unlock(THIS_MODULE);
return n;
}
u32 nv_rs_pci_upstream_max_gen(struct nv_rs_pci *pci)
{
struct device *device = pci ? nv_rs_pci_device(pci) : NULL;
struct pci_dev *pdev, *bridge;
u32 lnkcap = 0;
if (!device)
return 0;
pdev = to_pci_dev(device);
bridge = pci_upstream_bridge(pdev);
if (!bridge || !pci_is_pcie(bridge))
return 0;
if (pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &lnkcap) != 0)
return 0;
return lnkcap & PCI_EXP_LNKCAP_SLS;
}
u32 nv_rs_pci_upstream_get_target_gen(struct nv_rs_pci *pci)
{
struct device *device = pci ? nv_rs_pci_device(pci) : NULL;
struct pci_dev *pdev, *bridge;
u16 lnkctl2 = 0;
if (!device)
return 0;
pdev = to_pci_dev(device);
bridge = pci_upstream_bridge(pdev);
if (!bridge || !pci_is_pcie(bridge))
return 0;
if (pcie_capability_read_word(bridge, PCI_EXP_LNKCTL2, &lnkctl2) != 0)
return 0;
return lnkctl2 & PCI_EXP_LNKCTL2_TLS;
}
int nv_rs_pci_upstream_set_target_gen(struct nv_rs_pci *pci, u32 target_gen)
{
struct device *device = pci ? nv_rs_pci_device(pci) : NULL;
struct pci_dev *pdev, *bridge;
u32 lnkcap = 0, max_gen;
u16 lnkctl2 = 0;
if (!device || target_gen < 1 || target_gen > 5)
return -EINVAL;
pdev = to_pci_dev(device);
bridge = pci_upstream_bridge(pdev);
if (!bridge || !pci_is_pcie(bridge))
return 0;
if (pcie_capability_read_dword(bridge, PCI_EXP_LNKCAP, &lnkcap) != 0)
return 0;
max_gen = lnkcap & PCI_EXP_LNKCAP_SLS;
if (max_gen == 0 || target_gen > max_gen)
return -EINVAL;
if (pcie_capability_read_word(bridge, PCI_EXP_LNKCTL2, &lnkctl2) != 0)
return 0;
lnkctl2 = (u16)((lnkctl2 & ~PCI_EXP_LNKCTL2_TLS) | (target_gen & PCI_EXP_LNKCTL2_TLS));
return pcie_capability_write_word(bridge, PCI_EXP_LNKCTL2, lnkctl2);
}
static bool idle_pstate = false;
module_param(idle_pstate, bool, 0644);
MODULE_PARM_DESC(idle_pstate, "Opt-in built-in headless idle P-state power governor (default 0; 1 steps GPC to low-power idle after idle_pstate_delay_s, #169)");
bool nv_rs_idle_pstate(void) { return READ_ONCE(idle_pstate); }
static unsigned int idle_pstate_delay_s = 5;
module_param(idle_pstate_delay_s, uint, 0644);
MODULE_PARM_DESC(idle_pstate_delay_s, "Consecutive idle seconds before idle_pstate steps down clocks (default 5, #169)");
u32 nv_rs_idle_pstate_delay_s(void) { return READ_ONCE(idle_pstate_delay_s); }
static unsigned int idle_pstate_gpc_khz = 0;
module_param(idle_pstate_gpc_khz, uint, 0644);
MODULE_PARM_DESC(idle_pstate_gpc_khz, "Explicit idle GPC clock floor in kHz for idle_pstate=1 (0=safe 555 MHz arch default, #169)");
u32 nv_rs_idle_pstate_gpc_khz(void) { return READ_ONCE(idle_pstate_gpc_khz); }
static unsigned int vram_limit_mb = 0;
module_param(vram_limit_mb, uint, 0444);
MODULE_PARM_DESC(vram_limit_mb, "Opt-in downward VRAM ceiling in MiB (0=full detected hardware VRAM; clamps unlocked CMP/bad-upper-HBM boards safely, #174)");
u32 nv_rs_vram_limit_mb(void) { return vram_limit_mb; }
static char vram_limit_mb_map[256] = "";
module_param_string(vram_limit_mb_map, vram_limit_mb_map, sizeof(vram_limit_mb_map), 0444);
MODULE_PARM_DESC(vram_limit_mb_map, "Optional per-PCI-BDF downward VRAM ceiling in MiB, e.g. '0000:41:00.0=40960,42:00.0=0' (0=full detected hardware VRAM, #174)");
size_t nv_rs_vram_limit_mb_map_copy(u8 *out, size_t cap)
{
size_t n, i;
if (!out || !cap)
return 0;
n = strnlen(vram_limit_mb_map, sizeof(vram_limit_mb_map));
if (n > cap)
n = cap;
for (i = 0; i < n; i++)
out[i] = (u8)vram_limit_mb_map[i];
return n;
}
static unsigned int gddr5x_timing_profile = 0;
module_param(gddr5x_timing_profile, uint, 0644);
MODULE_PARM_DESC(gddr5x_timing_profile, "Opt-in Pascal GP102/GP104 GDDR5X runtime memory-timing profile (0=stock default, 1=mild t0=20/t1=5, 2=aggressive t0=16/t1=4; restored on teardown, #171)");
u32 nv_rs_gddr5x_timing_profile(void) { return READ_ONCE(gddr5x_timing_profile); }
static char gddr5x_timing_map[256] = "";
module_param_string(gddr5x_timing_map, gddr5x_timing_map, sizeof(gddr5x_timing_map), 0644);
MODULE_PARM_DESC(gddr5x_timing_map, "Optional per-PCI-BDF Pascal GDDR5X timing profile overrides, e.g. '0000:01:00.0=1,02:00.0=0' (runtime-writable via sysfs, #171)");
size_t nv_rs_gddr5x_timing_map_copy(u8 *out, size_t cap)
{
size_t n;
size_t i;
if (!out || !cap)
return 0;
kernel_param_lock(THIS_MODULE);
n = strnlen(gddr5x_timing_map, sizeof(gddr5x_timing_map));
if (n > cap)
n = cap;
for (i = 0; i < n; i++) {
WRITE_ONCE(out[i], (u8)gddr5x_timing_map[i]);
barrier();
}
kernel_param_unlock(THIS_MODULE);
return n;
}
void nv_rs_gddr5x_timing_log(u32 profile, u32 from_t0, u32 to_t0, u32 from_t1, u32 to_t1)
{
pr_info("nvidia_rs: Pascal GDDR5X timings applied (profile=%u): t0 %u -> %u, t1 %u -> %u\n",
profile, from_t0, to_t0, from_t1, to_t1);
}
void nv_rs_p0_boost_log(u32 gpc_from_khz, u32 gpc_to_khz, u32 xbar_to_khz)
{
pr_info("nvidia_rs: GV100 P0 boost active: GPC %u.%u -> %u.%u MHz, XBAR %u.%u MHz\n",
gpc_from_khz / 1000, (gpc_from_khz % 1000) / 100,
gpc_to_khz / 1000, (gpc_to_khz % 1000) / 100,
xbar_to_khz / 1000, (xbar_to_khz % 1000) / 100);
}
void nv_rs_p0_gpc_clamp_log(u32 gpu_id, u32 req_ndiv, u32 req_khz, u32 applied_ndiv, u32 applied_khz)
{
pr_info("nvidia_rs: GV100 P0 GPC NDIV request capped gpu=0x%08x: requested %u (%u.%u MHz), applied %u (%u.%u MHz); boot-voltage cap active (LOGIC rail raise not confirmed)\n",
gpu_id,
req_ndiv, req_khz / 1000, (req_khz % 1000) / 100,
applied_ndiv, applied_khz / 1000, (applied_khz % 1000) / 100);
}
void nv_rs_volt_scale_log(u32 gpu_id, u32 load_st, u32 get_st, u32 set_st,
u32 boot_uv, u32 target_uv, u32 pwm_before, u32 pwm_after)
{
pr_info("nvidia_rs: GV100 PMU VOLT gpu=0x%08x load=0x%x get=0x%x set=0x%x uv=%u->%u vid0_pwm1=0x%08x->0x%08x\n",
gpu_id, load_st, get_st, set_st, boot_uv, target_uv, pwm_before, pwm_after);
}
void nv_rs_pmgr_power_log(u32 gpu_id, u32 load_st, u32 poll_st, u32 ch0_mw, u32 ch7_mw)
{
pr_info("nvidia_rs: GV100 PMU PMGR gpu=0x%08x load=0x%x poll=0x%x ch0=%u mW ch7=%u mW\n",
gpu_id, load_st, poll_st, ch0_mw, ch7_mw);
}
static bool nvlink = true;
module_param(nvlink, bool, 0444);
MODULE_PARM_DESC(nvlink, "Automatically bring up GV100 NVLink 2.0 (MINION + AN0 discovery + ACTIVE + HSHUB) on all NVLink GPUs (default: 1)");
bool nv_rs_nvlink_enabled(void) { return nvlink; }
static bool unlock_ada_tensor;
module_param(unlock_ada_tensor, bool, 0444);
MODULE_PARM_DESC(unlock_ada_tensor, "Opt-in Turing/Ampere/Ada pre-GSP SM speed-select FP32-accumulate Tensor Core unlock (alias: unlock_tensor_fp32_acc; default: 0)");
module_param_named(unlock_tensor_fp32_acc, unlock_ada_tensor, bool, 0444);
MODULE_PARM_DESC(unlock_tensor_fp32_acc, "Opt-in Turing/Ampere/Ada pre-GSP SM speed-select FP32-accumulate Tensor Core unlock (SS0=0x88888888, SS1=0x8; default: 0)");
bool nv_rs_unlock_ada_tensor(void) { return unlock_ada_tensor; }
void nv_rs_ada_tensor_unlock_log(u32 gpu_id, u8 status, u32 plm, u32 ss0, u32 ss1)
{
pr_info("nvidia_rs: Tensor FP32-acc speed-select gpu=0x%08x status=%u plm=0x%08x ss0=0x%08x ss1=0x%08x\n",
gpu_id, status, plm, ss0, ss1);
}
static bool dyn_bar1_p2p = true;
module_param(dyn_bar1_p2p, bool, 0444);
MODULE_PARM_DESC(dyn_bar1_p2p, "Enable Dynamic BAR1 P2P (RMDynBar1P2PEnable=1 + RMDisableFeatureDisablement=1) in GSP-RM registry (default: 1)");
bool nv_rs_dyn_bar1_p2p(void) { return dyn_bar1_p2p; }
static bool disable_ecc;
module_param(disable_ecc, bool, 0444);
MODULE_PARM_DESC(disable_ecc, "Zero-reboot dynamic ECC disable toggle for RMAPI/UVM/GSP-RM without flashing InfoROM (default: 0)");
bool nv_rs_disable_ecc(void) { return disable_ecc; }
static bool unpriv_profiling = true;
module_param(unpriv_profiling, bool, 0444);
MODULE_PARM_DESC(unpriv_profiling, "Allow non-root userspace CUPTI/ncu/nsys hardware performance counters (RMRestrictProfilingToAdminUsers=0; default: 1)");
bool nv_rs_unpriv_profiling(void) { return unpriv_profiling; }
void nv_rs_nvlink_boot_log(u32 gpu_id, bool booted, u32 status, u32 dlcmd_mask, u32 safe_mask,
u32 cmd0_pll, u32 cmd0_phy)
{
pr_info("nvidia_rs: GV100 NVLink MINION gpu=0x%08x booted=%u status=0x%08x dlcmd_mask=0x%02x safe_mask=0x%02x pll0=0x%08x phy0=0x%08x\n",
gpu_id, booted ? 1u : 0u, status, dlcmd_mask, safe_mask, cmd0_pll, cmd0_phy);
}
void nv_rs_nvlink_link_log(u32 gpu_a, u32 link_a, u32 gpu_b, u32 link_b, u32 state_a, u32 state_b)
{
pr_info("nvidia_rs: GV100 NVLink ACTIVE gpu=0x%08x:L%u <-> gpu=0x%08x:L%u (state=%u/%u)\n",
gpu_a, link_a, gpu_b, link_b, state_a, state_b);
}
void nv_rs_nvlink_hshub_log(u32 gpu_id, u32 peer_id, u32 link_mask, u32 cfg0, u32 cfg1, u32 cfg2)
{
pr_info("nvidia_rs: GV100 NVLink HSHUB gpu=0x%08x peer=%u mask=0x%02x cfg0=0x%08x cfg1=0x%08x cfg2=0x%08x\n",
gpu_id, peer_id, link_mask, cfg0, cfg1, cfg2);
}
#define NV_RS_MAX_RUNTIME_OWNERS 32
static DEFINE_MUTEX(runtime_owner_lock);
static struct nv_rs_gpu_owner *nv_rs_runtime_owners[NV_RS_MAX_RUNTIME_OWNERS];
static struct task_struct *nv_rs_runtime_tasks[NV_RS_MAX_RUNTIME_OWNERS];
bool nv_rs_is_runtime_kthread(void)
{
int i;
for (i = 0; i < NV_RS_MAX_RUNTIME_OWNERS; i++) {
if (READ_ONCE(nv_rs_runtime_tasks[i]) == current)
return true;
}
return false;
}
u32 nv_rs_pci_gpu_id(struct nv_rs_pci *pci)
{
struct device *device = pci ? nv_rs_pci_device(pci) : NULL;
struct pci_dev *pdev;
if (!device)
return U32_MAX;
pdev = to_pci_dev(device);
return (((u32)pci_domain_nr(pdev->bus) & 0xffffu) << 16) |
((u32)pdev->bus->number << 8) |
(u32)PCI_SLOT(pdev->devfn);
}
bool nv_rs_pci_bdf(struct nv_rs_pci *pci, u16 *domain, u8 *bus, u8 *dev, u8 *func)
{
struct device *device = pci ? nv_rs_pci_device(pci) : NULL;
struct pci_dev *pdev;
if (!device || !domain || !bus || !dev || !func)
return false;
pdev = to_pci_dev(device);
*domain = (u16)pci_domain_nr(pdev->bus);
*bus = pdev->bus->number;
*dev = (u8)PCI_SLOT(pdev->devfn);
*func = (u8)PCI_FUNC(pdev->devfn);
return true;
}
u64 nv_rs_pci_dsn(struct nv_rs_pci *pci)
{
struct device *device = pci ? nv_rs_pci_device(pci) : NULL;
if (!device)
return 0;
return pci_get_dsn(to_pci_dev(device));
}
static int runtime_kthread_fn(void *data)
{
struct nv_rs_gpu_owner *owner = data;
int err = nv_rs_gpu_first_open(&owner->key);
int i;
if (!owner->runtime_parked) {
owner->runtime_error = err ? err : -EIO;
complete(&owner->runtime_ready);
}
mutex_lock(&runtime_owner_lock);
for (i = 0; i < NV_RS_MAX_RUNTIME_OWNERS; i++) {
if (nv_rs_runtime_owners[i] == owner) {
nv_rs_runtime_owners[i] = NULL;
WRITE_ONCE(nv_rs_runtime_tasks[i], NULL);
break;
}
}
mutex_unlock(&runtime_owner_lock);
complete(&owner->runtime_stopped);
return 0;
}
static struct nv_rs_gpu_owner *find_runtime_owner(struct nv_rs_pci *pci)
{
struct nv_rs_gpu_owner *owner = NULL;
int i;
mutex_lock(&runtime_owner_lock);
for (i = 0; i < NV_RS_MAX_RUNTIME_OWNERS; i++) {
if (nv_rs_runtime_owners[i] && nv_rs_runtime_owners[i]->pci == pci) {
owner = nv_rs_runtime_owners[i];
break;
}
}
mutex_unlock(&runtime_owner_lock);
return owner;
}
int nv_rs_runtime_park_wait(struct nv_rs_pci *pci)
{
struct nv_rs_gpu_owner *owner = find_runtime_owner(pci);
/* Only the dedicated nvidia_rs_rtN kthread may park here. A direct VFS
* open (/dev/nvidia-rs-<bdf>) holds misc_mtx and nv_rs_rm_api_lock and
* must never sleep waiting on runtime_stop_req (#57). */
if (!owner || READ_ONCE(owner->runtime_task) != current)
return 0;
if (!owner->runtime_parked) {
owner->runtime_parked = true;
owner->runtime_error = 0;
/* Release the global RM API lock while parked so additional GPUs can initialize
* and run concurrently; re-acquire before teardown so ApiGuard stays balanced. */
nv_rs_rm_api_unlock();
complete(&owner->runtime_ready);
}
wait_event_timeout(owner->runtime_wq,
READ_ONCE(owner->runtime_stop_req) ||
READ_ONCE(owner->runtime_gr_reset_req) ||
READ_ONCE(owner->runtime_hshub_req),
msecs_to_jiffies(1000));
if (READ_ONCE(owner->runtime_gr_reset_req) && !READ_ONCE(owner->runtime_stop_req)) {
WRITE_ONCE(owner->runtime_gr_reset_req, false);
return 1;
}
if (READ_ONCE(owner->runtime_hshub_req) && !READ_ONCE(owner->runtime_stop_req)) {
WRITE_ONCE(owner->runtime_hshub_req, false);
return 3;
}
if (!READ_ONCE(owner->runtime_stop_req))
return 2;
nv_rs_rm_api_lock();
return 0;
}
void nv_rs_runtime_park(struct nv_rs_pci *pci)
{
int ev;
while ((ev = nv_rs_runtime_park_wait(pci)) != 0) {
if (ev == 1)
nv_rs_runtime_gr_reset_complete(pci, -ENOSYS);
else if (ev == 3)
nv_rs_runtime_hshub_complete(pci, -ENOSYS);
}
}
void nv_rs_runtime_hshub_get_args(struct nv_rs_pci *pci, u32 *out_cfg4)
{
struct nv_rs_gpu_owner *owner = find_runtime_owner(pci);
if (!owner || !out_cfg4)
return;
out_cfg4[0] = owner->runtime_hshub_cfg[0];
out_cfg4[1] = owner->runtime_hshub_cfg[1];
out_cfg4[2] = owner->runtime_hshub_cfg[2];
out_cfg4[3] = owner->runtime_hshub_cfg[3];
}
void nv_rs_runtime_hshub_complete(struct nv_rs_pci *pci, int rc)
{
struct nv_rs_gpu_owner *owner = find_runtime_owner(pci);
if (!owner)
return;
owner->runtime_hshub_rc = rc;
complete(&owner->runtime_hshub_done);
}
int nv_rs_runtime_request_hshub_update(struct nv_rs_pci *pci, u32 cfg0, u32 cfg1, u32 cfg2, u32 cfg6)
{
struct nv_rs_gpu_owner *owner = find_runtime_owner(pci);
unsigned long left;
if (!owner || !owner->runtime_parked || READ_ONCE(owner->runtime_stop_req))
return -ENODEV;
reinit_completion(&owner->runtime_hshub_done);
owner->runtime_hshub_cfg[0] = cfg0;
owner->runtime_hshub_cfg[1] = cfg1;
owner->runtime_hshub_cfg[2] = cfg2;
owner->runtime_hshub_cfg[3] = cfg6;
owner->runtime_hshub_rc = -EIO;
WRITE_ONCE(owner->runtime_hshub_req, true);
wake_up(&owner->runtime_wq);
left = wait_for_completion_timeout(&owner->runtime_hshub_done, msecs_to_jiffies(5000));
if (left == 0)
return -ETIMEDOUT;
return owner->runtime_hshub_rc;
}
void nv_rs_runtime_gr_reset_complete(struct nv_rs_pci *pci, int rc)
{
struct nv_rs_gpu_owner *owner = find_runtime_owner(pci);
if (!owner)
return;
owner->runtime_gr_reset_rc = rc;
complete(&owner->runtime_gr_reset_done);
}
int nv_rs_runtime_request_gr_reset(struct nv_rs_pci *pci)
{
struct nv_rs_gpu_owner *owner = find_runtime_owner(pci);
unsigned long left;
if (!owner || !owner->runtime_parked || READ_ONCE(owner->runtime_stop_req))
return -ENODEV;
reinit_completion(&owner->runtime_gr_reset_done);
owner->runtime_gr_reset_rc = -EIO;
WRITE_ONCE(owner->runtime_gr_reset_req, true);
wake_up(&owner->runtime_wq);
left = wait_for_completion_timeout(&owner->runtime_gr_reset_done, msecs_to_jiffies(5000));
if (left == 0)
return -ETIMEDOUT;
return owner->runtime_gr_reset_rc;
}
/* Must be called with owner->runtime_lock held. If a previously timed-out
* bounded stop left a retired worker pending join, finish kthread_stop()
* and clear runtime_task / runtime_stopping so startup can proceed. */
static bool nv_rs_gpu_owner_runtime_reclaim_locked(struct nv_rs_gpu_owner *owner)
{
struct task_struct *task = READ_ONCE(owner->runtime_task);
if (!task && !smp_load_acquire(&owner->runtime_stopping))
return true;
if (!try_wait_for_completion(&owner->runtime_stopped))
return false;
if (task) {
kthread_stop(task);
put_task_struct(task);
WRITE_ONCE(owner->runtime_task, NULL);
}
owner->runtime_parked = false;
smp_store_release(&owner->runtime_stopping, false);
return true;
}
int nv_rs_gpu_owner_runtime_start(struct nv_rs_gpu_owner *owner)
{
struct nv_rs_pci *pci;
struct task_struct *task;
int err, slot = -1, i;
if (!owner || !runtime_engines)
return 0;
/* Acquire a runtime-PM power reference BEFORE taking runtime_lock so any
* concurrent runtime suspend finishes first and no new runtime suspend can
* start while startup waits on runtime_ready (breaks the AB-BA deadlock
* between Linux PM runtime and runtime_lock). Inside a PM resume callback
* (pm_resuming > 0), nv_rs_pci_power_get takes a non-blocking reference. */
pci = nv_rs_gpu_owner_pci_get(owner);
if (!pci)
return -ENODEV;
err = nv_rs_pci_power_get(pci);
if (err) {
nv_rs_pci_put(pci);
return err;
}
mutex_lock(&owner->runtime_lock);
if (READ_ONCE(owner->runtime_started)) {
err = owner->runtime_error;
mutex_unlock(&owner->runtime_lock);
nv_rs_pci_power_put(pci);
nv_rs_pci_put(pci);
return err;
}
if (!nv_rs_gpu_owner_runtime_reclaim_locked(owner)) {
mutex_unlock(&owner->runtime_lock);
nv_rs_pci_power_put(pci);
nv_rs_pci_put(pci);
return -EBUSY;
}
atomic_inc(&owner->runtime_starting);
smp_mb__after_atomic();
init_completion(&owner->runtime_ready);
init_completion(&owner->runtime_stop);
init_completion(&owner->runtime_stopped);
init_completion(&owner->runtime_gr_reset_done);
init_completion(&owner->runtime_hshub_done);
init_waitqueue_head(&owner->runtime_wq);
owner->runtime_parked = false;
owner->runtime_stop_req = false;
WRITE_ONCE(owner->runtime_stopping, false);
owner->runtime_gr_reset_req = false;
owner->runtime_gr_reset_rc = 0;
owner->runtime_hshub_req = false;
owner->runtime_hshub_rc = 0;
owner->runtime_error = 0;
mutex_lock(&runtime_owner_lock);
for (i = 0; i < NV_RS_MAX_RUNTIME_OWNERS; i++) {
if (nv_rs_runtime_owners[i] == owner) {
slot = i;
break;
}
if (slot < 0 && !nv_rs_runtime_owners[i])
slot = i;
}
if (slot < 0) {
mutex_unlock(&runtime_owner_lock);
atomic_dec(&owner->runtime_starting);
mutex_unlock(&owner->runtime_lock);
nv_rs_pci_power_put(pci);
nv_rs_pci_put(pci);
return -ENOSPC;
}
nv_rs_runtime_owners[slot] = owner;
mutex_unlock(&runtime_owner_lock);
task = kthread_create(runtime_kthread_fn, owner, "nvidia_rs_rt%d", slot);
if (IS_ERR(task)) {
mutex_lock(&runtime_owner_lock);
if (nv_rs_runtime_owners[slot] == owner)
nv_rs_runtime_owners[slot] = NULL;
mutex_unlock(&runtime_owner_lock);
err = PTR_ERR(task);
atomic_dec(&owner->runtime_starting);
mutex_unlock(&owner->runtime_lock);
nv_rs_pci_power_put(pci);
nv_rs_pci_put(pci);
return err;
}
get_task_struct(task);
mutex_lock(&runtime_owner_lock);
WRITE_ONCE(nv_rs_runtime_tasks[slot], task);
mutex_unlock(&runtime_owner_lock);
WRITE_ONCE(owner->runtime_task, task);
wake_up_process(task);
wait_for_completion(&owner->runtime_ready);
err = owner->runtime_error;
if (err) {
wait_for_completion(&owner->runtime_stopped);
kthread_stop(task);
put_task_struct(task);
WRITE_ONCE(owner->runtime_task, NULL);
} else {
smp_store_release(&owner->runtime_started, true);
}
atomic_dec(&owner->runtime_starting);
mutex_unlock(&owner->runtime_lock);
nv_rs_pci_power_put(pci);
nv_rs_pci_put(pci);
return err;
}
bool nv_rs_gpu_owner_runtime_active(struct nv_rs_gpu_owner *owner)
{
if (!owner)
return false;
/* Lockless read so PM runtime suspend callbacks never block on runtime_lock
* while startup holds it waiting for the worker's power reference. */
return smp_load_acquire(&owner->runtime_started) ||
atomic_read(&owner->runtime_starting) > 0 ||
smp_load_acquire(&owner->runtime_stopping);
}
static bool nv_rs_gpu_owner_runtime_stop_impl(struct nv_rs_gpu_owner *owner,
unsigned int timeout_ms, bool wait_forever)
{
bool retired = true;
unsigned long deadline = 0;
if (!owner)
return true;
if (wait_forever) {
mutex_lock(&owner->runtime_lock);
} else {
deadline = jiffies + msecs_to_jiffies(timeout_ms);
while (!mutex_trylock(&owner->runtime_lock)) {
if (time_after_eq(jiffies, deadline)) {
dev_warn(nv_rs_pci_device(owner->pci),
"nvidia_rs: bounded runtime stop timed out after %ums waiting for runtime_lock\n",
timeout_ms);
return false;
}
msleep(10);
}
}
if (READ_ONCE(owner->runtime_started) || READ_ONCE(owner->runtime_task)) {
struct task_struct *task = READ_ONCE(owner->runtime_task);
/* Mark teardown in progress and drop runtime_started before waking the
* worker so concurrent diagnostic open_gpu calls see -EBUSY rather than
* reusing a dying session or racing into first-open before retirement. */
smp_store_release(&owner->runtime_stopping, true);
smp_store_release(&owner->runtime_started, false);
WRITE_ONCE(owner->runtime_stop_req, true);
wake_up(&owner->runtime_wq);
complete(&owner->runtime_stop);
if (wait_forever) {
/* The runtime worker does not pin THIS_MODULE (to avoid the #491/#528
* rmmod self-pin deadlock), so PCI removal / module exit must wait
* until it retires and join via kthread_stop() before returning. */
while (!wait_for_completion_timeout(&owner->runtime_stopped,
msecs_to_jiffies(10000))) {
dev_warn(nv_rs_pci_device(owner->pci),
"nvidia_rs: runtime stop still waiting for worker exit\n");
}
} else {
unsigned long rem = time_before(jiffies, deadline) ? (deadline - jiffies) : 0;
retired = rem ? (wait_for_completion_timeout(&owner->runtime_stopped, rem) != 0)
: try_wait_for_completion(&owner->runtime_stopped);
if (!retired) {
/* Preserve runtime_task and runtime_stopping so a later
* nv_rs_gpu_owner_runtime_stop() on removal or reclaim on
* restart/AER resume still finishes the join safely. */
dev_warn(nv_rs_pci_device(owner->pci),
"nvidia_rs: bounded runtime stop timed out after %ums; deferring worker join\n",
timeout_ms);
}
}
if (retired) {
if (task) {
kthread_stop(task);
put_task_struct(task);
WRITE_ONCE(owner->runtime_task, NULL);
}
owner->runtime_parked = false;
smp_store_release(&owner->runtime_stopping, false);
}
}
mutex_unlock(&owner->runtime_lock);
return retired;
}
void nv_rs_gpu_owner_runtime_stop(struct nv_rs_gpu_owner *owner)
{
(void)nv_rs_gpu_owner_runtime_stop_impl(owner, 0, true);
}
bool nv_rs_gpu_owner_runtime_stop_bounded(struct nv_rs_gpu_owner *owner, unsigned int timeout_ms)
{
return nv_rs_gpu_owner_runtime_stop_impl(owner, timeout_ms, false);
}
void nv_rs_gpu_owner_set_aer_recovering(struct nv_rs_gpu_owner *owner, bool recovering)
{
if (!owner)
return;
smp_store_release(&owner->aer_recovering, recovering);
}
void nv_rs_bringup_note(struct nv_rs_pci *pci, u32 code, u32 value)
{
if (!pci)
return;
dev_info(nv_rs_pci_device(pci), "bringup note %08x %08x\n", code, value);
}
void nv_rs_hardware_inventory_log(struct nv_rs_pci *pci, u32 offset, u32 value)
{
if (!pci)
return;
dev_info(nv_rs_pci_device(pci), "inventory %06x %08x\n", offset, value);
}
void nv_rs_hardware_inventory_firmware(struct nv_rs_pci *pci, int error, u32 records, u32 bytes)
{
if (!pci)
return;
if (error)
dev_info(nv_rs_pci_device(pci), "inventory firmware container unavailable: %d\n", error);
else
dev_info(nv_rs_pci_device(pci), "inventory firmware container: %u records, %u bytes\n",
records, bytes);
}
/* Process context. The loader owns the buffer until the matching release. */
int nv_rs_firmware_request(struct nv_rs_pci *pci, const char *name, const u8 **data,
size_t *size, void **handle)
{
const struct firmware *firmware;
int error;
if (!pci || !name || !data || !size || !handle)
return -EINVAL;
might_sleep();
error = request_firmware(&firmware, name, nv_rs_pci_device(pci));
if (error)
return error;
*data = firmware->data;
*size = firmware->size;
*handle = (void *)firmware;
return 0;
}
void nv_rs_firmware_release(void *handle)
{
release_firmware(handle);
}
void nv_rs_rm_api_lock(void) { mutex_lock(&rm_api); }
void nv_rs_rm_api_unlock(void) { mutex_unlock(&rm_api); }
unsigned long nv_rs_gpu_registry_lock(void)
{ unsigned long flags;spin_lock_irqsave(&registry_lock,flags);return flags; }
void nv_rs_gpu_registry_unlock(unsigned long flags)
{ spin_unlock_irqrestore(&registry_lock,flags); }
static struct workqueue_struct *cleanup_queue;
static atomic_t pending_owners = ATOMIC_INIT(0);
static atomic_t cleanup_retries = ATOMIC_INIT(0);
static DECLARE_WAIT_QUEUE_HEAD(owner_wait);
bool nv_rs_gpu_module_get(void) { return try_module_get(THIS_MODULE); }
void nv_rs_gpu_module_put(void) { module_put(THIS_MODULE); }
unsigned int nv_rs_gpu_owner_pending(void) { return atomic_read(&pending_owners); }
unsigned int nv_rs_gpu_owner_retries(void) { return atomic_read(&cleanup_retries); }
unsigned int nv_rs_gpu_owner_waiters(struct nv_rs_gpu_owner *owner) { return owner ? atomic_read(&owner->operation_waiters) : 0; }
static void wait_operation(struct nv_rs_gpu_owner *owner)
{
atomic_inc(&owner->operation_waiters);
mutex_lock(&owner->operation);
atomic_dec(&owner->operation_waiters);
}
static int lock_operation(void *opaque)
{
struct nv_rs_gpu_owner *owner=opaque;
if (in_interrupt() || irqs_disabled() || in_atomic()) return -EWOULDBLOCK;
wait_operation(owner);
return 0;
}
static void unlock_operation(void *opaque)
{
struct nv_rs_gpu_owner *owner=opaque;
mutex_unlock(&owner->operation);
}
static void destroy_owner(void *opaque)
{
struct nv_rs_gpu_owner *owner = opaque;
kfree(owner->gpu);
kfree(owner->open_device.name);
/* Probe failures do not consume the caller's PCI lease. The DMA context
* has its own retained PCI reference through any delayed cleanup. */
if (owner->owns_pci) nv_rs_pci_put(owner->pci);
if (atomic_dec_and_test(&pending_owners)) wake_up_all(&owner_wait);
kfree(owner);
}
static void schedule_retry(void *opaque)
{
struct nv_rs_gpu_owner *owner = opaque;
atomic_inc(&cleanup_retries);
pr_warn_ratelimited("nvidia_rs: retaining removed GPU for cleanup retry\n");
queue_delayed_work(cleanup_queue, &owner->retry, msecs_to_jiffies(100));
}
static void retry_worker(struct work_struct *work)
{
struct nv_rs_gpu_owner *owner = container_of(to_delayed_work(work), struct nv_rs_gpu_owner, retry);
/* This call may destroy owner and work; no access after handoff. */
nv_rs_gpu_retry_cleanup(owner->gpu);
}
int nv_rs_gpu_owner_queue_init(void)
{
cleanup_queue = alloc_ordered_workqueue("nvidia_rs_cleanup", WQ_MEM_RECLAIM);
return cleanup_queue ? 0 : -ENOMEM;
}
void nv_rs_gpu_owner_queue_destroy(void)
{
/* External GPU leases pin this module, but the driver's base reference
* must not: idle bound devices still need to reach PCI remove at unload.
* PCI unregister has stopped new owners before this call. Retained base
* cleanup can still have a delayed timer during MODULE_STATE_GOING, so
* wait for owners before destroying the queue; destroy_workqueue alone
* cannot drain delayed work whose timer has not yet queued the work.
* The final destroy also joins a worker finishing after the zero wakeup. */
if (cleanup_queue) {
wait_event(owner_wait, atomic_read(&pending_owners) == 0);
destroy_workqueue(cleanup_queue);
cleanup_queue = NULL;
}
}
int nv_rs_gpu_owner_create(struct nv_rs_pci *pci, const struct nv_rs_probe_info *info,
struct nv_rs_gpu_owner **result)
{
struct nv_rs_gpu_owner *owner;
struct device *device;
struct pci_dev *pdev;
int error;
if (!result || !pci || !info || !cleanup_queue) return -EINVAL;
*result = NULL;
if (nv_rs_gpu_state_align() > ARCH_KMALLOC_MINALIGN || nv_rs_gpu_state_size() > 65536) return -EINVAL;
owner = kzalloc(sizeof(*owner), GFP_KERNEL);
if (!owner) return -ENOMEM;
owner->pci = pci;
mutex_init(&owner->operation);
mutex_init(&owner->runtime_lock);
atomic_set(&owner->operation_waiters,0);
INIT_DELAYED_WORK(&owner->retry, retry_worker);
owner->gpu = kzalloc(nv_rs_gpu_state_size(), GFP_KERNEL);
if (!owner->gpu) { kfree(owner); return -ENOMEM; }
error = nv_rs_gpu_prepare_owned(pci, info, owner->gpu, owner, destroy_owner, schedule_retry, lock_operation, unlock_operation);
if (error) { kfree(owner->gpu); kfree(owner); return error; }
atomic_inc(&pending_owners);
device=nv_rs_pci_device(pci);
if (!device) error=-ENODEV;
else {
pdev=to_pci_dev(device);
error=nv_rs_gpu_set_probe_identity(owner->gpu,(u32)pci_domain_nr(pdev->bus),
pdev->bus->number,PCI_SLOT(pdev->devfn),PCI_FUNC(pdev->devfn),pdev->is_virtfn?1:0);
}
if (!error) error=nv_rs_gpu_registry_publish(owner->gpu,pci,&owner->key);
if (error) {
/* No registry publication on failure. Retain partial cleanup as usual,
* while leaving the incoming PCI reference owned by the caller. */
nv_rs_gpu_release_driver(owner->gpu);
return error;
}
owner->registered=true;owner->owns_pci=true;
*result = owner;
return 0;
}
void *nv_rs_gpu_owner_state(struct nv_rs_gpu_owner *owner) { return owner ? owner->gpu : NULL; }
struct nv_rs_pci *nv_rs_gpu_owner_pci_get(struct nv_rs_gpu_owner *owner)
{
if (!owner || !owner->pci)
return NULL;
return nv_rs_pci_get(owner->pci) ? owner->pci : NULL;
}
static int open_gpu(struct inode *inode, struct file *file)
{
struct miscdevice *device = file->private_data;
struct nv_rs_gpu_owner *owner;
if (!device)
return -ENODEV;
owner = container_of(device, struct nv_rs_gpu_owner, open_device);
/* If the background runtime worker already owns a live, ready session,
* reuse it instead of re-entering first-open under misc_mtx (#57).
* While bring-up, teardown, or AER recovery is in flight, return -EBUSY
* instead of reporting false success or racing into first-open. Once a
* stop and any AER recovery complete cleanly, allow first-open again. */
if (smp_load_acquire(&owner->runtime_stopping) ||
smp_load_acquire(&owner->aer_recovering))
return -EBUSY;
if (smp_load_acquire(&owner->runtime_started)) {
if (smp_load_acquire(&owner->runtime_stopping) ||
smp_load_acquire(&owner->aer_recovering))
return -EBUSY;
return 0;
}
if (READ_ONCE(owner->runtime_task) != NULL ||
smp_load_acquire(&owner->runtime_stopping) ||
smp_load_acquire(&owner->aer_recovering))
return -EBUSY;
/* misc_open holds its registration lock through this callback.
* Remove joins the callback before retiring and freeing the owner. */
return nv_rs_gpu_first_open(&owner->key);
}
static const struct file_operations gpu_open_operations = {
.owner = THIS_MODULE,
.open = open_gpu,
/* Match stock 580.178.04 nvidia_fops: omit .llseek (NULL -> -ESPIPE on all kernels). */
};
int nv_rs_gpu_owner_register_open(struct nv_rs_gpu_owner *owner)
{
struct device *device;
int error;
if (!owner || !owner->pci)
return -EINVAL;
device = nv_rs_pci_device(owner->pci);
if (!device)
return -ENODEV;
if (!owner->registered || owner->open_device.name)
return -EINVAL;
owner->open_device.name = kasprintf(GFP_KERNEL, "nvidia-rs-%s", dev_name(device));
if (!owner->open_device.name)
return -ENOMEM;
owner->open_device.parent = device;
owner->open_device.minor = MISC_DYNAMIC_MINOR;
owner->open_device.mode = 0600;
owner->open_device.fops = &gpu_open_operations;
error = misc_register(&owner->open_device);
if (!error) {
owner->open_registered = true;
} else {
kfree(owner->open_device.name);
owner->open_device.name = NULL;
}
return error;
}
void nv_rs_gpu_owner_remove(struct nv_rs_gpu_owner *owner)
{
if (!owner)
return;
nv_rs_gpu_mark_removed(owner->gpu);
if (owner->open_registered) {
misc_deregister(&owner->open_device);
owner->open_registered = false;
}
if (owner->registered) {
int error=nv_rs_gpu_registry_retire(owner->gpu,&owner->key);
if (error) {
/* A live slot may still reference this storage. Preserve its
* driver base rather than releasing an inconsistent registration. */
pr_err("nvidia_rs: retaining GPU after registry retirement error %d\n",error);
return;
}
owner->registered=false;
}
/* Registry entries have been unpublished. Abort new Rust operations and
* drain an existing sequence before PCI removal; no registry lock needed
* by a running sequence may be held here. Hardware quiescence is a separate
* prerequisite, not a consequence of this process-context mutex. */
wait_operation(owner);
{
int hbm_rc = nv_rs_gpu_retry_gp100_hbm_restore(owner->gpu);
if (hbm_rc)
pr_err("nvidia_rs: GP100 HBM baseline restore failed (%d) during removal; retaining stock baseline\n",
hbm_rc);
}
nv_rs_pci_mark_removed(owner->pci);
unlock_operation(owner);
nv_rs_gpu_release_driver(owner->gpu);
}