Files
nvdriver/kernel-open/nvidia/gpu_owner.c

683 lines
28 KiB
C

// SPDX-License-Identifier: MIT
/* OS allocation/workqueue adapters for Rust's final-reference cleanup protocol. */
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/kthread.h>
#include <linux/wait.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;
bool runtime_started;
bool runtime_parked;
bool runtime_stop_req;
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;
module_param(v100_hbm_ndiv, uint, 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); }
static char v100_hbm_ndiv_map[256] = "";
module_param_string(v100_hbm_ndiv_map, v100_hbm_ndiv_map, sizeof(v100_hbm_ndiv_map), 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)");
size_t nv_rs_v100_hbm_ndiv_map_copy(u8 *out, size_t cap)
{
size_t n;
if (!out || !cap)
return 0;
n = strnlen(v100_hbm_ndiv_map, sizeof(v100_hbm_ndiv_map));
if (n > cap)
n = cap;
memcpy(out, v100_hbm_ndiv_map, n);
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 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;
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);
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;
}
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 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 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; }
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_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 Ada Lovelace (RTX 40-series) 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: Ada Tensor unlock 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];
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;
}
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;
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);
if (!owner)
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;
}
int nv_rs_gpu_owner_runtime_start(struct nv_rs_gpu_owner *owner)
{
struct task_struct *task;
int err, slot = -1, i;
if (!owner || !runtime_engines)
return 0;
mutex_lock(&owner->runtime_lock);
if (owner->runtime_started) {
err = owner->runtime_error;
mutex_unlock(&owner->runtime_lock);
return err;
}
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;
owner->runtime_gr_reset_req = false;
owner->runtime_gr_reset_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);
mutex_unlock(&owner->runtime_lock);
return -ENOSPC;
}
nv_rs_runtime_owners[slot] = owner;
mutex_unlock(&runtime_owner_lock);
task = kthread_run(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);
mutex_unlock(&owner->runtime_lock);
return err;
}
wait_for_completion(&owner->runtime_ready);
err = owner->runtime_error;
if (err) {
wait_for_completion(&owner->runtime_stopped);
} else {
owner->runtime_started = true;
}
mutex_unlock(&owner->runtime_lock);
return err;
}
void nv_rs_gpu_owner_runtime_stop(struct nv_rs_gpu_owner *owner)
{
if (!owner)
return;
mutex_lock(&owner->runtime_lock);
if (owner->runtime_started) {
WRITE_ONCE(owner->runtime_stop_req, true);
wake_up(&owner->runtime_wq);
complete(&owner->runtime_stop);
if (!wait_for_completion_timeout(&owner->runtime_stopped, msecs_to_jiffies(10000))) {
dev_warn(nv_rs_pci_device(owner->pci),
"nvidia_rs: runtime stop timed out after 10s; continuing teardown\n");
}
owner->runtime_started = false;
owner->runtime_parked = false;
}
mutex_unlock(&owner->runtime_lock);
}
void nv_rs_bringup_note(struct nv_rs_pci *pci, u32 code, u32 value)
{
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)
{
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 (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 atomic_read(&owner->operation_waiters); }
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->gpu; }
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 = container_of(device, struct nv_rs_gpu_owner, open_device);
/* 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 = nv_rs_pci_device(owner->pci);
int error;
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;
return error;
}
void nv_rs_gpu_owner_remove(struct nv_rs_gpu_owner *owner)
{
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);
nv_rs_pci_mark_removed(owner->pci);
unlock_operation(owner);
nv_rs_gpu_release_driver(owner->gpu);
}