Files
nvdriver/kernel-open/nvidia/pci_driver.c
Eric Hartford 3ada318df4 Drop-in UX (#133), Maxwell/Pascal ACR FW, Hopper/Blackwell split-RM, and #142 Phases 3-4
- Zero-friction first-run UX (#133): default runtime_engines=1 and pci_target=*
  in nvidia_rs.ko (with explicit bringup=N precedence via resolve_bringup_mode);
  install.sh auto-extracts pre-GSP firmware packs + GSP blobs, installs
  /etc/modprobe.d/nvdriver.conf (install nvidia /bin/false, char-major-195-*
  alias), /etc/modules-load.d/nvdriver.conf, and /etc/udev/rules.d/70-nvdriver.rules,
  and enables BIND_DEV_NODES=1 by default.
- Maxwell & Pascal real firmware loading (GM10x-GP108): extend
  tools/extract_gv100_firmware.py (--family gm20x|gp100|gp10x|all) using
  REA-verified 580.178.04 ACR/PMU bundles (_nv016827rm/_nv016828rm/_nv016888rm)
  with a shared pack_container() builder; wire LoadedPack::request_legacy and
  HS Falcon IMEM/DMEM upload + execution into boot_maxwell_pascal_pre_gsp.
- Unified Hopper/Blackwell split-RM routing: add GspFirmwareChipFamily::boot_mode()
  (FwsecBooter vs Fmc) as a single source of truth; integrate GH100/GB10x/GB20x
  FMC BootROM handshake into Ga10xGsp::boot/unload via PciRegIo so every GSP-RM
  GPU from Turing through Blackwell runs on the unified gsp_route.rs split-RM.
- Issue #142 Phases 3-4: add FileMappingLedger and GpuSlotLedger (+ unit tests)
  to src/nvidia/src/rmapi/alloc_registry.rs + rmapi_memory.rs FFI, removing
  struct nv_rs_rmapi_mapping and redundant bar0/bar1/numa_status fields from
  kernel-open/nvidia/rmapi_node.c.

Validated: cargo fmt/clippy/test (957+2), NV_RS_KERNEL_IBT=0 & 1 make modules +
check_kernel_module.py (largest_stack=1000), test_gate_cases.py (18/18),
extract_gv100_firmware.py --check, and QEMU EDU verify_rust_guest.py (non-IOMMU
and --iommu, maxframe=984).
2026-12-01 23:03:43 +00:00

415 lines
14 KiB
C

// SPDX-License-Identifier: MIT
/* Opt-in ownership path. A bound PCI device is not a CUDA-ready GPU. */
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "pci_adapter.h"
#include "pci_driver.h"
#include "gpu_owner.h"
#include "rmapi_node.h"
#include "hwmon_adapter.h"
extern bool nv_rs_pci_admitted(u8 class, u32 subclass, u16 vendor, u16 device,
u16 subvendor, u16 subdevice);
extern int nv_rs_gpu_pm_suspend_begin(void *storage, u32 action,
bool runtime_was_active, u32 *out_gpu_id);
extern void nv_rs_gpu_pm_suspend_rollback(void *storage);
extern int nv_rs_gpu_pm_resume_begin(void *storage, u32 *out_flags,
u32 *out_gpu_id);
extern void nv_rs_gpu_pm_resume_finish(void *storage);
extern int nv_rs_gpu_pm_runtime_suspend(void *storage, bool runtime_was_active);
extern int nv_rs_gpu_pm_runtime_resume(void *storage, u32 *out_flags);
extern int nv_rs_gpu_pm_verify_alive(void *storage);
extern void nv_rs_modeset_notify_suspend(u32 gpu_id);
extern void nv_rs_modeset_notify_resume(u32 gpu_id);
#define NV_RS_PM_ACTION_HIBERNATE 0u
#define NV_RS_PM_ACTION_STANDBY 1u
#define NV_RS_PM_RESUME_RESTART_RUNTIME (1u << 1)
#ifdef NV_RS_PCI_TEST_EDU
static char *pci_target = "";
#else
static char *pci_target = "*";
#endif
module_param(pci_target, charp, 0400);
MODULE_PARM_DESC(pci_target, "Explicit BDF (or comma-separated BDF list, or * [default]) for ownership/bring-up; empty acquires no GPUs");
static bool nv_rs_pci_target_matches(const char *target, const char *bdf)
{
size_t bdf_len;
const char *p;
if (!target || !target[0] || !bdf)
return false;
if (target[0] == '*' && target[1] == '\0')
return true;
bdf_len = strlen(bdf);
p = target;
while (*p) {
const char *comma = strchr(p, ',');
size_t tok_len = comma ? (size_t)(comma - p) : strlen(p);
if (tok_len == bdf_len && strncmp(p, bdf, bdf_len) == 0)
return true;
if (!comma)
break;
p = comma + 1;
}
return false;
}
/* Register reads for the RM API device node: the locked BAR0 read of the PCI lease. The
* node only reads (rmapi_node.h); the lease reference taken at probe is dropped by it. */
static int nv_rs_rmapi_read32(void *cookie, u64 offset, u32 *value)
{
return nv_rs_pci_read32(cookie, offset, value);
}
static void nv_rs_rmapi_release(void *cookie)
{
nv_rs_pci_put(cookie);
}
static int nv_rs_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
struct nv_rs_gpu_owner *owner;
struct nv_rs_pci *lease;
struct nv_rs_probe_info info;
int error;
(void)id;
if (!nv_rs_pci_target_matches(pci_target, pci_name(pdev))) return -ENODEV;
if (!nv_rs_pci_admitted((pdev->class >> 16) & 0xff,
(pdev->class >> 8) & 0xff, pdev->vendor, pdev->device,
pdev->subsystem_vendor, pdev->subsystem_device)) {
dev_info(&pdev->dev,
"nvdriver: pci_admitted rejected class=0x%02x subclass=0x%02x vendor=0x%04x device=0x%04x subsys=0x%04x:0x%04x\n",
(pdev->class >> 16) & 0xff, (pdev->class >> 8) & 0xff,
pdev->vendor, pdev->device,
pdev->subsystem_vendor, pdev->subsystem_device);
return -ENODEV;
}
error = nv_rs_pci_open(pdev, &lease);
if (error) return error;
error = nv_rs_pci_read32(lease, 0, &info.boot0);
if (error) goto fail_lease;
error = nv_rs_pci_read32(lease, 4, &info.boot1);
if (error) goto fail_lease;
error = nv_rs_pci_read32(lease, 0xa00, &info.boot42);
if (error) goto fail_lease;
if ((info.boot0 & info.boot42) == U32_MAX) {
error = -ENODEV; goto fail_lease;
}
info.vendor = pdev->vendor; info.device = pdev->device;
info.subvendor = pdev->subsystem_vendor; info.subdevice = pdev->subsystem_device;
error = nv_rs_gpu_owner_create(lease, &info, &owner);
if (error) goto fail_lease;
error = nv_rs_gpu_owner_register_open(owner);
if (error) {
nv_rs_gpu_owner_remove(owner);
return error;
}
pci_set_drvdata(pdev, owner);
/* RM API inventory (NV_ESC_CARD_INFO, probed ids). Failure only hides the GPU there. */
error = nv_rs_rmapi_node_gpu_add(pdev, info.boot0);
if (error < 0)
dev_warn(&pdev->dev, "not listed on the RM API control node (%d)\n", error);
else {
if (nv_rs_pci_get(lease) &&
nv_rs_rmapi_node_gpu_reader(pdev, nv_rs_rmapi_read32, nv_rs_rmapi_release, lease))
nv_rs_pci_put(lease);
nv_rs_rmapi_node_gpu_runtime(pdev, owner);
}
if (nv_rs_hwmon_register(pdev) < 0)
dev_warn(&pdev->dev, "failed to register /sys/class/hwmon sensor node\n");
dev_info(&pdev->dev, "Rust PCI/DMA preparation complete; engine initialization and CUDA unavailable\n");
return 0;
fail_lease:
nv_rs_pci_mark_removed(lease);
nv_rs_pci_put(lease);
return error;
}
static void nv_rs_remove(struct pci_dev *pdev)
{
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
if (!owner) return;
/* No engines/IRQs are active during current preparation. Future runtime
* must quiesce them before this final driver-reference handoff. */
nv_rs_hwmon_unregister(pdev);
nv_rs_rmapi_node_gpu_remove(pdev);
pci_set_drvdata(pdev, NULL);
nv_rs_gpu_owner_remove(owner);
}
#ifdef CONFIG_PM
/* Faithful 580.178.04 S3/S4 & RTD3 power-management lifecycle matching
* nvidia/nv.c (nv_pmops_*, nvidia_suspend/resume, nvidia_transition_dynamic_power)
* and nv-kernel.o_binary (rm_power_management @ 0x6ee4bd0,
* rm_transition_dynamic_power @ 0x6ee4ee0). */
static int nv_rs_pm_do_suspend(struct device *dev, u32 action)
{
struct pci_dev *pdev = to_pci_dev(dev);
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
void *gpu;
u32 gpu_id = 0;
int decision;
if (!owner)
return 0;
gpu = nv_rs_gpu_owner_state(owner);
decision = nv_rs_gpu_pm_suspend_begin(gpu, action,
nv_rs_runtime_engines(), &gpu_id);
if (decision < 0)
return -EIO;
if (decision == 0) {
if (action == NV_RS_PM_ACTION_STANDBY)
pci_save_state(pdev);
return 0;
}
/* Notify modeset first (nv.c:4527), then quiesce runtime engines/channels. */
nv_rs_modeset_notify_suspend(gpu_id);
if (nv_rs_gpu_pm_verify_alive(gpu) == 0)
nv_rs_gpu_owner_runtime_stop(owner);
else
dev_warn(dev, "GPU appears lost during PM suspend; skipping engine teardown\n");
pci_save_state(pdev);
pci_disable_device(pdev);
pci_set_power_state(pdev, PCI_D3hot);
return 0;
}
static int nv_rs_pm_do_resume(struct device *dev)
{
struct pci_dev *pdev = to_pci_dev(dev);
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
void *gpu;
u32 flags = 0;
u32 gpu_id = 0;
int decision, error;
if (!owner)
return 0;
gpu = nv_rs_gpu_owner_state(owner);
decision = nv_rs_gpu_pm_resume_begin(gpu, &flags, &gpu_id);
if (decision < 0)
return -EIO;
if (decision == 0)
return 0;
pci_set_power_state(pdev, PCI_D0);
pci_restore_state(pdev);
error = pci_enable_device_mem(pdev);
if (error) {
dev_err(dev, "pci_enable_device_mem failed on PM resume (%d)\n", error);
return -EIO;
}
pci_set_master(pdev);
error = nv_rs_gpu_pm_verify_alive(gpu);
if (error) {
dev_err(dev, "GPU BAR0 PMC_BOOT_0 verification failed on PM resume (%d)\n", error);
return -EIO;
}
if ((flags & NV_RS_PM_RESUME_RESTART_RUNTIME) && nv_rs_runtime_engines()) {
error = nv_rs_gpu_owner_runtime_start(owner);
if (error) {
dev_err(dev, "runtime engine bring-up failed on PM resume (%d)\n", error);
return -EIO;
}
}
nv_rs_gpu_pm_resume_finish(gpu);
/* Notify modeset after GPU and runtime engines are restored (nv.c:4590). */
nv_rs_modeset_notify_resume(gpu_id);
return 0;
}
static int nv_rs_pmops_suspend(struct device *dev)
{
int error = nv_rs_pm_do_suspend(dev, NV_RS_PM_ACTION_STANDBY);
if (error) {
struct nv_rs_gpu_owner *owner = pci_get_drvdata(to_pci_dev(dev));
if (owner)
nv_rs_gpu_pm_suspend_rollback(nv_rs_gpu_owner_state(owner));
nv_rs_pm_do_resume(dev);
}
return error ? -EIO : 0;
}
static int nv_rs_pmops_resume(struct device *dev)
{
return nv_rs_pm_do_resume(dev) ? -EIO : 0;
}
static int nv_rs_pmops_freeze(struct device *dev)
{
int error = nv_rs_pm_do_suspend(dev, NV_RS_PM_ACTION_HIBERNATE);
if (error) {
struct nv_rs_gpu_owner *owner = pci_get_drvdata(to_pci_dev(dev));
if (owner)
nv_rs_gpu_pm_suspend_rollback(nv_rs_gpu_owner_state(owner));
nv_rs_pm_do_resume(dev);
}
return error ? -EIO : 0;
}
static int nv_rs_pmops_thaw(struct device *dev)
{
(void)dev;
return 0;
}
static int nv_rs_pmops_poweroff(struct device *dev)
{
(void)dev;
return 0;
}
static int nv_rs_pmops_restore(struct device *dev)
{
return nv_rs_pm_do_resume(dev) ? -EIO : 0;
}
static int nv_rs_pmops_runtime_suspend(struct device *dev)
{
struct pci_dev *pdev = to_pci_dev(dev);
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
int decision;
if (!owner)
return 0;
decision = nv_rs_gpu_pm_runtime_suspend(nv_rs_gpu_owner_state(owner),
nv_rs_runtime_engines());
if (decision == -EAGAIN)
return -EAGAIN;
if (decision < 0)
return -EIO;
if (decision == 1) {
nv_rs_gpu_owner_runtime_stop(owner);
pci_save_state(pdev);
pci_disable_device(pdev);
pci_set_power_state(pdev, PCI_D3hot);
}
return 0;
}
static int nv_rs_pmops_runtime_resume(struct device *dev)
{
struct pci_dev *pdev = to_pci_dev(dev);
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
void *gpu;
u32 flags = 0;
int decision, error;
if (!owner)
return 0;
gpu = nv_rs_gpu_owner_state(owner);
decision = nv_rs_gpu_pm_runtime_resume(gpu, &flags);
if (decision < 0)
return -EIO;
if (decision == 1) {
pci_set_power_state(pdev, PCI_D0);
pci_restore_state(pdev);
error = pci_enable_device_mem(pdev);
if (error)
return -EIO;
pci_set_master(pdev);
if (nv_rs_gpu_pm_verify_alive(gpu))
return -EIO;
if ((flags & NV_RS_PM_RESUME_RESTART_RUNTIME) && nv_rs_runtime_engines()) {
if (nv_rs_gpu_owner_runtime_start(owner))
return -EIO;
}
}
return 0;
}
static const struct dev_pm_ops nv_rs_pm_ops = {
.suspend = nv_rs_pmops_suspend,
.resume = nv_rs_pmops_resume,
.freeze = nv_rs_pmops_freeze,
.thaw = nv_rs_pmops_thaw,
.poweroff = nv_rs_pmops_poweroff,
.restore = nv_rs_pmops_restore,
.runtime_suspend = nv_rs_pmops_runtime_suspend,
.runtime_resume = nv_rs_pmops_runtime_resume,
};
#endif /* CONFIG_PM */
/* Match stock 580.178.04 nv_pci_shutdown (nvidia/nv-pci.c:786-816): quiesce
* runtime engines and clear Bus Master Enable so kexec/reboot cannot observe
* stray GPU DMA writes into host memory. */
static void nv_rs_shutdown(struct pci_dev *pdev)
{
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
if (owner)
nv_rs_gpu_owner_runtime_stop(owner);
pci_clear_master(pdev);
udelay(1);
}
static pci_ers_result_t nv_rs_aer_error_detected(struct pci_dev *pdev,
pci_channel_state_t state)
{
struct nv_rs_gpu_owner *owner = pci_get_drvdata(pdev);
dev_err(&pdev->dev, "nvidia_rs: PCIe AER error detected (state=%d)\n", state);
if (owner)
nv_rs_gpu_owner_runtime_stop(owner);
pci_clear_master(pdev);
if (state == pci_channel_io_perm_failure)
return PCI_ERS_RESULT_DISCONNECT;
return PCI_ERS_RESULT_NEED_RESET;
}
static pci_ers_result_t nv_rs_aer_slot_reset(struct pci_dev *pdev)
{
if (pci_enable_device_mem(pdev))
return PCI_ERS_RESULT_DISCONNECT;
pci_set_master(pdev);
return PCI_ERS_RESULT_RECOVERED;
}
static void nv_rs_aer_resume(struct pci_dev *pdev)
{
dev_info(&pdev->dev, "nvidia_rs: PCIe AER resume complete\n");
}
static const struct pci_error_handlers nv_rs_err_handlers = {
.error_detected = nv_rs_aer_error_detected,
.slot_reset = nv_rs_aer_slot_reset,
.resume = nv_rs_aer_resume,
};
static const struct pci_device_id nv_rs_ids[] = {
{ .vendor = 0x10de, .device = PCI_ANY_ID, .subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID, .class = PCI_BASE_CLASS_DISPLAY << 16,
.class_mask = 0xff0000 },
{0}
};
/* No MODULE_DEVICE_TABLE: do not autoload/compete for devices by product list. */
static struct pci_driver nv_rs_driver = {
.name = "nvidia_rs", .id_table = nv_rs_ids,
.probe = nv_rs_probe, .remove = nv_rs_remove,
.shutdown = nv_rs_shutdown,
.err_handler = &nv_rs_err_handlers,
#ifdef CONFIG_PM
.driver.pm = &nv_rs_pm_ops,
#endif
};
int nv_rs_pci_driver_register(void)
{
int error;
error = nv_rs_gpu_owner_queue_init();
if (error) return error;
error = pci_register_driver(&nv_rs_driver);
if (error) nv_rs_gpu_owner_queue_destroy();
return error;
}
void nv_rs_pci_driver_unregister(void)
{
pci_unregister_driver(&nv_rs_driver);
/* External GPU leases pin the module. Base-only removal may queue cleanup
* during exit; the owner queue waits for every pending timer/worker owner. */
nv_rs_gpu_owner_queue_destroy();
}