mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
- 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).
262 lines
11 KiB
C
262 lines
11 KiB
C
// SPDX-License-Identifier: MIT
|
|
/* Native control protocol with opt-in owned PCI/GPU preparation. */
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/types.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/miscdevice.h>
|
|
#include <linux/version.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/uaccess.h>
|
|
#ifdef CONFIG_COMPAT
|
|
#include <linux/compat.h>
|
|
#endif
|
|
#include "ioctl_policy.h"
|
|
#include "pci_driver.h"
|
|
#include "file_cleanup.h"
|
|
#include "event_work_adapter.h"
|
|
#include "rmapi_node.h"
|
|
#include "uvm_node.h"
|
|
extern u32 nv_rs_protocol_version(void);
|
|
extern u64 nv_rs_gpu_capabilities(void);
|
|
extern int nv_rs_event_selftest(void);
|
|
extern size_t nv_rs_device_size(void);
|
|
extern size_t nv_rs_file_size(void);
|
|
extern size_t nv_rs_state_align(void);
|
|
extern int nv_rs_device_init(void *state);
|
|
extern int nv_rs_file_open(void *device, void *file);
|
|
extern int nv_rs_file_close(void *device, void *file);
|
|
extern int nv_rs_nested_copy_error(u32 command, u8 *bytes, size_t length, bool output);
|
|
extern int nv_rs_rm_preflight(void *device, void *file, u32 command, u8 *bytes, size_t length);
|
|
extern int nv_rs_nested_plan(u32 command, const u8 *bytes, size_t length, u64 *pointer, size_t *size);
|
|
extern int nv_rs_rm_ioctl(void *device, void *file, u32 command, u8 *bytes, size_t length, u8 *nested, size_t nested_length);
|
|
extern int nv_rs_ioctl(void *device, void *file, u32 command, u8 *bytes, size_t length);
|
|
static unsigned int protocol_version;
|
|
module_param(protocol_version, uint, 0444);
|
|
MODULE_PARM_DESC(protocol_version, "Control protocol version; no GPU/CUDA support");
|
|
static bool run_selftest = true;
|
|
module_param(run_selftest, bool, 0444);
|
|
static DEFINE_MUTEX(nv_rs_device_lock);
|
|
static void *nv_rs_device_state;
|
|
static struct nv_rs_file_cleanup_queue nv_rs_file_cleanup_queue;
|
|
struct nv_rs_file { struct mutex lock; void *state; struct nv_rs_file_cleanup cleanup; };
|
|
static int nv_rs_close_context(void *opaque)
|
|
{
|
|
struct nv_rs_file *context=opaque;
|
|
int result;
|
|
mutex_lock(&context->lock);
|
|
mutex_lock(&nv_rs_device_lock);
|
|
result=nv_rs_file_close(nv_rs_device_state,context->state);
|
|
mutex_unlock(&nv_rs_device_lock);
|
|
mutex_unlock(&context->lock);
|
|
return result;
|
|
}
|
|
static void nv_rs_destroy_context(void *opaque)
|
|
{
|
|
struct nv_rs_file *context=opaque;
|
|
kfree(context->state);
|
|
kfree(context);
|
|
}
|
|
static int nv_rs_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct nv_rs_file *context;
|
|
int result;
|
|
context = kzalloc(sizeof(*context), GFP_KERNEL);
|
|
if (!context)
|
|
return -ENOMEM;
|
|
context->state = kzalloc(nv_rs_file_size(), GFP_KERNEL);
|
|
if (!context->state) { kfree(context); return -ENOMEM; }
|
|
mutex_init(&context->lock);
|
|
nv_rs_file_cleanup_init(&context->cleanup,&nv_rs_file_cleanup_queue,context,nv_rs_close_context,nv_rs_destroy_context);
|
|
mutex_lock(&nv_rs_device_lock);
|
|
result = nv_rs_file_open(nv_rs_device_state, context->state);
|
|
mutex_unlock(&nv_rs_device_lock);
|
|
if (result) { kfree(context->state); kfree(context); return result; }
|
|
file->private_data = context;
|
|
return nonseekable_open(inode, file);
|
|
}
|
|
static int nv_rs_release(struct inode *inode, struct file *file)
|
|
{
|
|
struct nv_rs_file *context=file->private_data;
|
|
(void)inode;
|
|
file->private_data=NULL;
|
|
return nv_rs_file_cleanup_retire(&context->cleanup);
|
|
}
|
|
static long nv_rs_unlocked_ioctl(struct file *file, unsigned int command,
|
|
unsigned long argument)
|
|
{
|
|
struct nv_rs_file *context = file->private_data;
|
|
struct nv_rs_request request;
|
|
struct nv_rs_xfer xfer;
|
|
void __user *user_pointer;
|
|
u8 *bytes, *nested = NULL;
|
|
u64 nested_pointer = 0;
|
|
size_t nested_size = 0;
|
|
bool rm_request;
|
|
int result = nv_rs_plan(command, argument, NULL, &request);
|
|
if (result == 1) {
|
|
if (copy_from_user(&xfer, (void __user *)argument, sizeof(xfer)))
|
|
return -EFAULT;
|
|
result = nv_rs_plan(command, argument, &xfer, &request);
|
|
}
|
|
if (result)
|
|
return result;
|
|
user_pointer = u64_to_user_ptr(request.pointer);
|
|
bytes = kmalloc(request.size ? request.size : 1, GFP_KERNEL);
|
|
if (!bytes)
|
|
return -ENOMEM;
|
|
if (copy_from_user(bytes, user_pointer, request.size)) {
|
|
kfree(bytes);
|
|
return -EFAULT;
|
|
}
|
|
rm_request = request.command == 0x29 || request.command == 0x2a || request.command == 0x2b;
|
|
if (rm_request) {
|
|
/* Match original invalid-client/metadata error ordering before reading
|
|
* nested userspace. Dispatch rechecks after copies because another
|
|
* thread can mutate file state while these locks are released. */
|
|
if (mutex_lock_interruptible(&context->lock)) { result = -ERESTARTSYS; goto out; }
|
|
if (mutex_lock_interruptible(&nv_rs_device_lock)) {
|
|
mutex_unlock(&context->lock); result = -ERESTARTSYS; goto out;
|
|
}
|
|
result = nv_rs_rm_preflight(nv_rs_device_state, context->state, request.command, bytes, request.size);
|
|
mutex_unlock(&nv_rs_device_lock);
|
|
mutex_unlock(&context->lock);
|
|
if (result < 0) goto out;
|
|
if (result == 1) { result = 0; goto copy_output; }
|
|
result = nv_rs_nested_plan(request.command, bytes, request.size, &nested_pointer, &nested_size);
|
|
if (result == -EOVERFLOW) {
|
|
result = nv_rs_nested_copy_error(request.command, bytes, request.size, false);
|
|
goto copy_output;
|
|
}
|
|
if (result) goto out;
|
|
/* Explicit adapter allocation bound, not a claimed hardware ABI limit. */
|
|
if (nested_size > 1024 * 1024) { result = -ENOMEM; goto out; }
|
|
if (nested_size) {
|
|
nested = kmalloc(nested_size, GFP_KERNEL);
|
|
if (!nested) { result = -ENOMEM; goto out; }
|
|
if (copy_from_user(nested, u64_to_user_ptr(nested_pointer), nested_size)) {
|
|
result = nv_rs_nested_copy_error(request.command, bytes, request.size, false);
|
|
nested_size = 0; /* Never expose uninitialized failed-input data. */
|
|
goto copy_output;
|
|
}
|
|
}
|
|
}
|
|
/* Copy occurs before locks; no userspace pointer reaches Rust. Lock order
|
|
* is always per-file -> device. No pointer-bearing RM payload is followed. */
|
|
if (mutex_lock_interruptible(&context->lock)) { result = -ERESTARTSYS; goto out; }
|
|
if (mutex_lock_interruptible(&nv_rs_device_lock)) {
|
|
mutex_unlock(&context->lock); result = -ERESTARTSYS; goto out;
|
|
}
|
|
if (rm_request)
|
|
result = nv_rs_rm_ioctl(nv_rs_device_state, context->state, request.command,
|
|
bytes, request.size, nested, nested_size);
|
|
else
|
|
result = nv_rs_ioctl(nv_rs_device_state, context->state, request.command,
|
|
bytes, request.size);
|
|
mutex_unlock(&nv_rs_device_lock);
|
|
mutex_unlock(&context->lock);
|
|
/* Match native version mismatch behavior: reply/version copy back even
|
|
* when the protocol returns EINVAL. A copy-out fault takes precedence. */
|
|
copy_output:
|
|
if (nested_size && copy_to_user(u64_to_user_ptr(nested_pointer), nested, nested_size))
|
|
result = nv_rs_nested_copy_error(request.command, bytes, request.size, true);
|
|
if (copy_to_user(user_pointer, bytes, request.size))
|
|
result = -EFAULT;
|
|
out:
|
|
kfree(nested);
|
|
kfree(bytes);
|
|
return result;
|
|
}
|
|
#ifdef CONFIG_COMPAT
|
|
static long nv_rs_compat_ioctl(struct file *file, unsigned int command, unsigned long arg)
|
|
{
|
|
/* Exact shipped structs use fixed-width/aligned NvP64 fields. Top-level
|
|
* compat syscall argument is a compat pointer; nested wire pointers remain
|
|
* 64-bit fields and are checked by normal copy_from_user access rules. */
|
|
return nv_rs_unlocked_ioctl(file, command, (unsigned long)compat_ptr(arg));
|
|
}
|
|
#endif
|
|
static const struct file_operations nv_rs_operations = {
|
|
.owner = THIS_MODULE, .open = nv_rs_open, .release = nv_rs_release,
|
|
.unlocked_ioctl = nv_rs_unlocked_ioctl,
|
|
#ifdef CONFIG_COMPAT
|
|
.compat_ioctl = nv_rs_compat_ioctl,
|
|
#endif
|
|
/* Match stock 580.178.04 nvidia_fops: omit .llseek (NULL -> -ESPIPE on all kernels). */
|
|
};
|
|
static struct miscdevice nv_rs_control = {
|
|
.minor = MISC_DYNAMIC_MINOR, .name = "nvidia-rsctl",
|
|
.fops = &nv_rs_operations, .mode = 0600,
|
|
};
|
|
extern int nv_rs_software_manager_init(void);
|
|
extern int nv_rs_software_manager_shutdown(void);
|
|
static void drain_software_manager(void)
|
|
{
|
|
int error;
|
|
while ((error = nv_rs_software_manager_shutdown()) != 0) {
|
|
pr_warn_ratelimited("nvidia_rs: retaining software manager until cleanup succeeds (%d)\n", error);
|
|
msleep(20);
|
|
}
|
|
}
|
|
static int __init nvidia_rs_init(void)
|
|
{
|
|
int status;
|
|
protocol_version = nv_rs_protocol_version();
|
|
if (nv_rs_gpu_capabilities() != 0 || nv_rs_state_align() > ARCH_KMALLOC_MINALIGN ||
|
|
nv_rs_device_size() > 1048576 || nv_rs_file_size() > 65536)
|
|
return -EINVAL;
|
|
if (run_selftest && (status = nv_rs_event_selftest()))
|
|
return status;
|
|
nv_rs_device_state = kzalloc(nv_rs_device_size(), GFP_KERNEL);
|
|
if (!nv_rs_device_state)
|
|
return -ENOMEM;
|
|
status = nv_rs_device_init(nv_rs_device_state);
|
|
if (status) { kfree(nv_rs_device_state); return status; }
|
|
status=nv_rs_file_cleanup_queue_init(&nv_rs_file_cleanup_queue);
|
|
if (status) { kfree(nv_rs_device_state); return status; }
|
|
status=nv_rs_event_workqueue_init();
|
|
if(status)goto fail_queue;
|
|
status = nv_rs_software_manager_init();
|
|
if (status) goto fail_software_manager;
|
|
/* RM API control node and UVM node: registered before the PCI driver. */
|
|
status = nv_rs_rmapi_node_init();
|
|
if (status) goto fail_software_manager;
|
|
status = nv_rs_uvm_node_init();
|
|
if (status) { nv_rs_rmapi_node_exit(); goto fail_software_manager; }
|
|
status = nv_rs_pci_driver_register();
|
|
if (status) { nv_rs_uvm_node_exit(); nv_rs_rmapi_node_exit(); goto fail_software_manager; }
|
|
/* Publish fops only after every owning subsystem is initialized. */
|
|
status=misc_register(&nv_rs_control);
|
|
if (status) { nv_rs_pci_driver_unregister(); nv_rs_uvm_node_exit(); nv_rs_rmapi_node_exit(); goto fail_software_manager; }
|
|
pr_info("nvidia_rs: /dev/nvidia-rsctl protocol %u; GPU/CUDA unsupported\n", protocol_version);
|
|
return 0;
|
|
fail_software_manager:
|
|
drain_software_manager();
|
|
nv_rs_event_workqueue_destroy();
|
|
fail_queue:
|
|
nv_rs_file_cleanup_queue_destroy(&nv_rs_file_cleanup_queue);
|
|
kfree(nv_rs_device_state);
|
|
return status;
|
|
}
|
|
static void __exit nvidia_rs_exit(void)
|
|
{
|
|
misc_deregister(&nv_rs_control);
|
|
nv_rs_pci_driver_unregister();
|
|
nv_rs_uvm_node_exit();
|
|
nv_rs_rmapi_node_exit();
|
|
nv_rs_event_workqueue_destroy();
|
|
/* No retained delayed item can coexist with module exit: each owns a pin.
|
|
* Join a last worker that has put its pin but has not returned yet. */
|
|
nv_rs_file_cleanup_queue_destroy(&nv_rs_file_cleanup_queue);
|
|
drain_software_manager();
|
|
kfree(nv_rs_device_state);
|
|
}
|
|
module_init(nvidia_rs_init);
|
|
module_exit(nvidia_rs_exit);
|
|
MODULE_LICENSE("Dual MIT/GPL");
|
|
MODULE_DESCRIPTION("Clean-room Rust NVIDIA GPU driver (Maxwell-Blackwell)");
|
|
MODULE_AUTHOR("nvidia-driver-rs reconstruction project");
|
|
MODULE_VERSION("0.2.0");
|