mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
nv_rs_uvm_ioctl() and nv_rs_uvm_release() took nv_rs_uvm_lock and then
nv_rs_rmapi_lock. Two other orders already exist:
rmapi -> mmap_lock nv_rs_rmapi_os_lock_user_pages() calls
pin_user_pages_fast() with the RM API lock held
(NV01_MEMORY_SYSTEM_OS_DESCRIPTOR, cudaHostRegister)
mmap_lock -> uvm the kernel calls nv_rs_uvm_mmap() and
nv_rs_uvm_vma_close() with mmap_lock held
so uvm -> rmapi closes a cycle. lockdep reports it as soon as a CUDA
process pins host memory while another thread mmaps the UVM node, and a
multi-threaded process can really deadlock on it:
WARNING: possible circular locking dependency detected
cuda_dmabuf_exp/4377 is trying to acquire lock:
(&mm->mmap_lock){++++}-{4:4}, at: gup_fast_fallback+0x51f/0x2bc0
but task is already holding lock:
(nv_rs_rmapi_lock){+.+.}-{4:4}, at: nv_rs_rmapi_ioctl+0x64c/0x2f50
Chain exists of:
&mm->mmap_lock --> nv_rs_uvm_lock --> nv_rs_rmapi_lock
Take the RM API lock first in both places. Nothing under the RM API lock
takes the UVM lock (only the UVM file operations and module init/exit
do), so the order is now rmapi -> mmap_lock -> uvm everywhere. The fault
worker only trylocks, so it could not deadlock, but it now tries the two
locks in the same order as well.
The other way out would be to pin the pages before taking the RM API
lock in the OS-descriptor path; this keeps the change local to the UVM
node.
Observed on an RTX 3050 Laptop (GA107) under a lockdep kernel
(7.4.0-rc2) running a CUDA dma-buf test.
2146 lines
82 KiB
C
2146 lines
82 KiB
C
// SPDX-License-Identifier: MIT
|
|
/* Character-device adapter of the Rust RM API nodes: one control node and one device node
|
|
* per GPU. User memory is copied here; every rule of the protocol lives in Rust
|
|
* (src/nvidia/src/rmapi). */
|
|
#include <linux/capability.h>
|
|
#include <linux/device.h>
|
|
#include <linux/dma-mapping.h>
|
|
#include <linux/fdtable.h>
|
|
#include <linux/file.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/highmem.h>
|
|
#include <linux/io.h>
|
|
#include <linux/list.h>
|
|
#include <linux/miscdevice.h>
|
|
#include <linux/version.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/module.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/namei.h>
|
|
#include <linux/pci.h>
|
|
#include <linux/poll.h>
|
|
#include <linux/proc_fs.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/workqueue.h>
|
|
#include <linux/version.h>
|
|
#include <linux/vmalloc.h>
|
|
#ifdef CONFIG_COMPAT
|
|
#include <linux/compat.h>
|
|
#endif
|
|
#include "dmabuf_p2p.h"
|
|
#include "gpu_owner.h"
|
|
#include "ioctl_policy.h"
|
|
#include "pci_adapter.h"
|
|
#include "rmapi_node.h"
|
|
#include "uvm_node.h"
|
|
|
|
struct nv_rs_rmapi_gpu {
|
|
u32 domain;
|
|
u8 bus, slot, function, reserved;
|
|
u16 vendor, device;
|
|
u32 irq;
|
|
u64 bar0, bar0_size, bar1, bar1_size, bar2, bar2_size;
|
|
u16 subsystem_vendor, subsystem;
|
|
u8 revision;
|
|
u32 boot0;
|
|
};
|
|
struct nv_rs_rmapi_write { u64 address; const u8 *data; size_t length; };
|
|
extern size_t nv_rs_rmapi_state_size(void);
|
|
extern size_t nv_rs_rmapi_state_align(void);
|
|
extern int nv_rs_rmapi_init(void *node);
|
|
extern int nv_rs_rmapi_gpu_add(void *node, const struct nv_rs_rmapi_gpu *gpu);
|
|
extern int nv_rs_rmapi_gpu_remove(void *node, u32 domain, u8 bus, u8 slot);
|
|
extern int nv_rs_rmapi_gpu_minor(void *node, u32 domain, u8 bus, u8 slot);
|
|
extern int nv_rs_rmapi_prepare(void *node, u64 owner, bool admin, bool control_node,
|
|
u32 gpu_access, u32 escape, u8 *top, size_t top_length,
|
|
u64 *address, size_t *length, int *result);
|
|
extern int nv_rs_rmapi_nested_plan(const u8 *top, size_t top_length, const u8 *params,
|
|
size_t params_length, u64 *input, u64 *output, size_t *length);
|
|
extern int nv_rs_rmapi_execute(void *node, u64 owner, bool admin, bool control_node,
|
|
u32 gpu_access, u32 escape, u8 *top, size_t top_length,
|
|
u8 *params, size_t params_length, u8 *nested, size_t nested_length,
|
|
struct nv_rs_rmapi_write *writes, u32 *write_count);
|
|
extern int nv_rs_rmapi_gpu_open(void *node, u32 minor);
|
|
extern u32 nv_rs_rmapi_attach_register_count(void);
|
|
extern u32 nv_rs_rmapi_attach_register(u32 index);
|
|
extern bool nv_rs_rmapi_gpu_needs_registers(void *node, u32 minor);
|
|
extern int nv_rs_rmapi_gpu_attach_registers(void *node, u32 minor, const u32 *values, u32 count);
|
|
extern int nv_rs_rmapi_set_host_bridge(void *node, u16 vendor, u16 device);
|
|
extern int nv_rs_rmapi_set_force_p2p(void *node, u32 force_p2p_mask);
|
|
extern bool nv_rs_rmapi_set_gsp_abi(u32 gsp_abi_ver);
|
|
extern void nv_rs_rmapi_set_driver_version(const char *version, size_t len);
|
|
extern int nv_rs_rmapi_gpu_release(void *node, u32 minor);
|
|
extern u32 nv_rs_rmapi_register_fd(bool caller_registered, bool target_control,
|
|
bool target_registered, bool same_file);
|
|
extern int nv_rs_rmapi_copy_fault(u32 escape, u8 *top, size_t top_length, bool output);
|
|
extern int nv_rs_rmapi_close(void *node, u64 owner);
|
|
extern u32 nv_rs_rmapi_rc_recover_fault(u32 gpu_id, u64 inst_ptr, u32 xid_code,
|
|
u32 *ch_out, u32 *tsg_out, u32 *diag_out);
|
|
extern bool nv_rs_modeset_register_gpu(u32 gpu_id, u32 domain, u8 bus, u8 slot, u8 function,
|
|
void *os_device_ptr);
|
|
extern bool nv_rs_modeset_unregister_gpu(u32 gpu_id);
|
|
|
|
/* One lock for the whole node: it stands in for the original's RM API lock. User copies
|
|
* are made outside it. */
|
|
static DEFINE_MUTEX(nv_rs_rmapi_lock);
|
|
/* The UVM node (uvm_node.c) and the GA10x code (src/nvidia/kernel/ga10x.rs) take this lock too where they change GPU
|
|
* state: the object tables, GSP-RM's RPC channel, video memory through the BAR0 window. Order: this lock, then the UVM
|
|
* lock. This lock is held across pin_user_pages (mmap_lock) and the UVM lock is taken under mmap_lock (the UVM .mmap
|
|
* and vm_ops->close), so the UVM-first order closed a cycle through mmap_lock (see uvm_node.c). */
|
|
void nv_rs_rmapi_node_lock(void);
|
|
void nv_rs_rmapi_node_lock(void)
|
|
{
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
}
|
|
int nv_rs_rmapi_node_trylock(void);
|
|
int nv_rs_rmapi_node_trylock(void)
|
|
{
|
|
return mutex_trylock(&nv_rs_rmapi_lock);
|
|
}
|
|
void nv_rs_rmapi_node_unlock(void);
|
|
void nv_rs_rmapi_node_unlock(void)
|
|
{
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
}
|
|
static void *nv_rs_rmapi_state;
|
|
#define NV_RS_RMAPI_MAX_GPUS 32
|
|
#define NV_RS_ESC_RM_GET_EVENT_DATA 0x52
|
|
#define NV_RS_ESC_REGISTER_FD 0xc9
|
|
#define NV_RS_ESC_ALLOC_OS_EVENT 0xce
|
|
#define NV_RS_ESC_FREE_OS_EVENT 0xcf
|
|
#define NV_RS_ESC_QUERY_DEVICE_INTR 0xd5
|
|
#define NV_RS_ESC_SET_NUMA_STATUS 0xd8
|
|
#define NV_RS_ESC_EXPORT_TO_DMABUF_FD 0xd9
|
|
#define NV_RS_DMABUF_EXPORT_IOCTL_SIZE 2608U
|
|
struct nv_rs_file_map_record {
|
|
u64 owner;
|
|
void *alloc_ptr;
|
|
u64 page_index;
|
|
u64 io_phys;
|
|
u64 io_bytes;
|
|
u32 protection;
|
|
u32 caching;
|
|
bool is_io;
|
|
};
|
|
extern int nv_rs_file_map_insert_alloc(u64 owner, void *alloc_ptr, u64 page_index,
|
|
u32 protection, u32 caching);
|
|
extern int nv_rs_file_map_insert_io(u64 owner, u64 io_phys, u64 io_bytes,
|
|
u32 protection, u32 caching);
|
|
extern bool nv_rs_file_map_get(u64 owner, struct nv_rs_file_map_record *out);
|
|
extern void *nv_rs_file_map_remove(u64 owner);
|
|
extern u32 nv_rs_gpu_slot_activate(u32 minor, u32 gpu_id, u64 bar0, u64 bar0_size,
|
|
u64 bar1, u64 bar1_size);
|
|
extern u32 nv_rs_gpu_slot_deactivate(u32 minor);
|
|
extern bool nv_rs_gpu_slot_is_live_gen(u32 minor, u32 generation);
|
|
extern bool nv_rs_gpu_slot_set_numa(u32 minor, s32 status);
|
|
extern bool nv_rs_gpu_slot_validate_bar0(u32 minor, u32 generation, u64 offset, u64 length,
|
|
u64 *out_phys);
|
|
extern bool nv_rs_gpu_slot_validate_bar1(u32 minor, u32 generation, u64 aligned_off,
|
|
u64 aligned_len, u64 raw_off, u64 *out_io_phys,
|
|
u64 *out_user_addr);
|
|
|
|
/* The opened file is the owner of the clients allocated through it. `gpu` is the device
|
|
* node's minor number in the Rust GPU table, or -1 for the control node. `control` is the
|
|
* control file NV_ESC_REGISTER_FD associated with this file; the reference is held until
|
|
* release, as the original holds the control file's reference. Per-file mmap contexts live
|
|
* in Rust's `FileMappingLedger` (`src/nvidia/src/rmapi/alloc_registry.rs`). */
|
|
struct nv_rs_rmapi_file {
|
|
u64 owner;
|
|
int gpu;
|
|
/* Generation of the device node the file was opened on: a file that outlives its GPU
|
|
* must not count for a later GPU that reuses the minor number. */
|
|
unsigned int generation;
|
|
struct file *control;
|
|
wait_queue_head_t wait;
|
|
/* bit 0 = a dataless OS event was posted to this file (nv_rs_rmapi_file_post) */
|
|
unsigned long events;
|
|
};
|
|
struct nv_rs_rmapi_gpu_node {
|
|
struct miscdevice misc;
|
|
char name[32];
|
|
char bdf[32];
|
|
bool live;
|
|
unsigned int generation;
|
|
/* Register access of the GPU (nv_rs_rmapi_node_gpu_reader); NULL without one. */
|
|
int (*read32)(void *cookie, u64 offset, u32 *value);
|
|
void (*release)(void *cookie);
|
|
void *cookie;
|
|
struct nv_rs_gpu_owner *owner;
|
|
struct pci_dev *pdev;
|
|
u32 gpu_id;
|
|
u32 runtime_start_users;
|
|
int minor_index;
|
|
#ifdef CONFIG_PROC_FS
|
|
struct proc_dir_entry *proc_dir;
|
|
#endif
|
|
};
|
|
static DECLARE_WAIT_QUEUE_HEAD(nv_rs_rmapi_owner_wq);
|
|
#define NV_RS_RMAPI_ATTACH_REGISTERS 32
|
|
/* Indexed by the minor number the Rust table gave the GPU; guarded by nv_rs_rmapi_lock. */
|
|
static struct nv_rs_rmapi_gpu_node nv_rs_rmapi_gpu_nodes[NV_RS_RMAPI_MAX_GPUS];
|
|
static DEFINE_SPINLOCK(nv_rs_rmapi_gpu_dev_lock);
|
|
static DEFINE_MUTEX(nv_rs_rmapi_alloc_lock);
|
|
static const struct file_operations nv_rs_rmapi_operations;
|
|
static const struct file_operations nv_rs_rmapi_gpu_operations;
|
|
|
|
static struct nv_rs_rmapi_file *nv_rs_rmapi_context(int gpu)
|
|
{
|
|
struct nv_rs_rmapi_file *context = kzalloc(sizeof(*context), GFP_KERNEL);
|
|
if (!context)
|
|
return NULL;
|
|
context->owner = (u64)(unsigned long)context;
|
|
context->gpu = gpu;
|
|
init_waitqueue_head(&context->wait);
|
|
return context;
|
|
}
|
|
|
|
/* System memory of the RM API (class 0x3e): the operating-system side the Rust provider
|
|
* src/nvidia/kernel/rmapi_memory.rs calls. Every function below runs under
|
|
* nv_rs_rmapi_lock, from an ioctl or a release of one of this driver's RM API nodes; none
|
|
* touches a device. The pages are ordinary kernel pages with the kernel's cached
|
|
* attribute: the original also changes the page attributes for the uncached and
|
|
* write-combined cache types and maps the pages for the GPU's DMA, neither is done here.
|
|
*
|
|
* An allocation lives while it has users: the RM object (until nv_rs_rmapi_os_free_pages)
|
|
* and every file that carries a mapping context of it (until the file is released; the
|
|
* original defers its nv_free_pages the same way). User mappings do not count: the page
|
|
* references vm_insert_page took keep their pages after the record is gone. */
|
|
#define NV_RS_RMAPI_MEMORY_MAX_PAGES ((256ull << 20) >> PAGE_SHIFT)
|
|
static u64 nv_rs_rmapi_memory_max_pages(void)
|
|
{
|
|
u64 live_gpus = 0;
|
|
u64 base_pages;
|
|
int i;
|
|
for (i = 0; i < NV_RS_RMAPI_MAX_GPUS; i++) {
|
|
if (nv_rs_rmapi_gpu_nodes[i].live)
|
|
live_gpus++;
|
|
}
|
|
if (live_gpus == 0)
|
|
live_gpus = 1;
|
|
base_pages = live_gpus * NV_RS_RMAPI_MEMORY_MAX_PAGES;
|
|
if (nv_rs_runtime_engines()) {
|
|
u64 ram_half = (u64)totalram_pages() / 2;
|
|
if (ram_half > base_pages)
|
|
return ram_half;
|
|
}
|
|
return base_pages;
|
|
}
|
|
/* Ceiling for every page this file holds, allocated or pinned user memory together: 7/8 of
|
|
* RAM, so the two budgets cannot add up past what the host has. */
|
|
static u64 nv_rs_rmapi_total_max_pages(void)
|
|
{
|
|
u64 ram = (u64)totalram_pages();
|
|
return ram - ram / 8;
|
|
}
|
|
#define NV_RS_STATUS_INVALID_ADDRESS 0x1e
|
|
#define NV_RS_STATUS_INVALID_ARGUMENT 0x1f
|
|
#define NV_RS_STATUS_NO_MEMORY 0x51
|
|
#define NV_RS_STATUS_NOT_SUPPORTED 0x56
|
|
#define NV_RS_STATUS_OBJECT_NOT_FOUND 0x57
|
|
#define NV_RS_STATUS_STATE_IN_USE 0x63
|
|
/* Rust-owned allocation/refcount/budget registry (src/nvidia/kernel/rmapi_memory.rs). */
|
|
bool nv_rs_alloc_reg_can_admit(u64 count, bool user_pinned, u64 pool_pages,
|
|
u64 total_max, u64 mem_max);
|
|
bool nv_rs_alloc_reg_insert(void *alloc_ptr, u64 count, bool user_pinned, u64 *out_token);
|
|
void *nv_rs_alloc_reg_find(u64 token);
|
|
void *nv_rs_alloc_reg_retain(u64 token);
|
|
bool nv_rs_alloc_reg_mark_freed_and_put(u64 token, void **out_ptr);
|
|
bool nv_rs_alloc_reg_put_ptr(void *alloc_ptr);
|
|
void nv_rs_alloc_reg_for_each(void (*cb)(void *alloc_ptr, void *ctx), void *ctx);
|
|
u64 nv_rs_alloc_reg_total_pages(void);
|
|
void *nv_rs_alloc_reg_pop_any(void);
|
|
void nv_rs_alloc_reg_release_storage(void);
|
|
void *nv_rs_dma_reg_find(void *alloc_ptr, u32 gpu_id, void *dev_ptr);
|
|
bool nv_rs_dma_reg_insert(void *alloc_ptr, u32 gpu_id, void *dev_ptr, void *dm_ptr);
|
|
void *nv_rs_dma_reg_pop_for_alloc(void *alloc_ptr);
|
|
void *nv_rs_dma_reg_pop_for_dev(void *dev_ptr);
|
|
|
|
struct nv_rs_rmapi_dma_map {
|
|
struct device *dev;
|
|
u32 gpu_id;
|
|
u64 count;
|
|
u64 encoded[]; /* 0 = unmapped; otherwise (u64)dma_addr + 1 */
|
|
};
|
|
struct nv_rs_rmapi_alloc {
|
|
u64 token, count;
|
|
u32 cache;
|
|
bool user_pinned;
|
|
bool writable;
|
|
void *kernel;
|
|
struct page *pages[];
|
|
};
|
|
static unsigned long nv_rs_rmapi_gpu_cache_flushes;
|
|
static atomic64_t nv_rs_rmapi_kvalloc_live = ATOMIC64_INIT(0);
|
|
|
|
/*
|
|
* System memory page pool with client-close trimming (ported from
|
|
* NVreg_SystemMemoryPoolRetainMB).
|
|
*
|
|
* Pooled pages are subtracted from MemAvailable until reclaimed, so on
|
|
* unified-memory systems (where every GPU allocation is system memory) a
|
|
* large pool left behind after a CUDA process exits looks like a leak.
|
|
* 0 (default): return all pooled pages to the OS immediately / on client close.
|
|
* -1: disable trimming on client close (pool grows for reuse until unload).
|
|
* >0: retain up to this many MiB across client closes.
|
|
*/
|
|
static int system_memory_pool_retain_mb;
|
|
module_param(system_memory_pool_retain_mb, int, 0644);
|
|
MODULE_PARM_DESC(system_memory_pool_retain_mb,
|
|
"MiB of freed system memory pages to retain in pool on client close (0=trim all, -1=disable trim)");
|
|
module_param_named(NVreg_SystemMemoryPoolRetainMB, system_memory_pool_retain_mb, int, 0644);
|
|
|
|
/*
|
|
* Configurable PCIe P2P override bitmask (default 0x11 = PCIe BAR1 P2P Read+Write
|
|
* unlocked; pass 0x211 to also opt into PCIe P2P Atomics).
|
|
*/
|
|
static unsigned int force_p2p = 0x11;
|
|
module_param(force_p2p, uint, 0444);
|
|
MODULE_PARM_DESC(force_p2p,
|
|
"PCIe P2P override bitmask (default 0x11 = Read+Write; 0x211 = Read+Write+Atomics)");
|
|
module_param_named(NVreg_ForceP2P, force_p2p, uint, 0444);
|
|
|
|
/*
|
|
* GSP-RM wire protocol ABI version for Turing-Blackwell-Rubin GPUs:
|
|
* 620 (default) = 620.70.06 MCTP+NVDM+GMCAPI framing & firmware paths
|
|
* 580 = 580.178.04 legacy 0x30 XOR-checksum framing & firmware paths
|
|
*/
|
|
static unsigned int gsp_abi = 620;
|
|
module_param(gsp_abi, uint, 0444);
|
|
MODULE_PARM_DESC(gsp_abi,
|
|
"GSP-RM wire protocol ABI for Turing-Blackwell-Rubin (620=620.70.06/620.73.04 MCTP+NVDM default, 580=580.178.04); independent of driver_version");
|
|
|
|
static char *driver_version = "580.178.04";
|
|
module_param(driver_version, charp, 0444);
|
|
MODULE_PARM_DESC(driver_version,
|
|
"Reported NVIDIA userspace driver version string for NVML/libcuda matching (default 580.178.04, independent of gsp_abi; set e.g. 620.73.04 on 620 userspace hosts)");
|
|
|
|
static DEFINE_MUTEX(nv_rs_sysmem_pool_lock);
|
|
static LIST_HEAD(nv_rs_sysmem_pool);
|
|
static unsigned long nv_rs_sysmem_pool_pages;
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
static struct proc_dir_entry *nv_rs_proc_nvidia;
|
|
static struct proc_dir_entry *nv_rs_proc_gpus;
|
|
static struct proc_dir_entry *nv_rs_proc_caps;
|
|
static struct proc_dir_entry *nv_rs_proc_caps_mig;
|
|
|
|
static int nv_rs_proc_params_show(struct seq_file *m, void *v)
|
|
{
|
|
(void)v;
|
|
seq_puts(m, "ModifyDeviceFiles: 0\nDeviceFileUID: 0\nDeviceFileGID: 0\nDeviceFileMode: 438\n");
|
|
return 0;
|
|
}
|
|
|
|
static int nv_rs_proc_version_show(struct seq_file *m, void *v)
|
|
{
|
|
const char *ver = (driver_version && *driver_version) ? driver_version : "580.178.04";
|
|
(void)v;
|
|
seq_printf(m, "NVRM version: nvdriver (clean-room Rust) %s (GSP-RM 620.70.06)\n", ver);
|
|
return 0;
|
|
}
|
|
|
|
static int nv_rs_proc_cap_show(struct seq_file *m, void *v)
|
|
{
|
|
(void)v;
|
|
seq_puts(m, "DeviceFileMinor: 0\nDeviceFileModify: 0\n");
|
|
return 0;
|
|
}
|
|
|
|
static int nv_rs_proc_gpu_info_show(struct seq_file *m, void *v)
|
|
{
|
|
struct nv_rs_rmapi_gpu_node *node = m->private;
|
|
u16 did = node && node->pdev ? node->pdev->device : 0;
|
|
u32 irq = node && node->pdev ? node->pdev->irq : 0;
|
|
|
|
(void)v;
|
|
seq_printf(m, "Model: \t\t NVIDIA GPU (0x%04x)\n", did);
|
|
seq_printf(m, "IRQ: \t\t %u\n", irq);
|
|
seq_printf(m, "Bus Location: \t %s\n", node ? node->bdf : "unknown");
|
|
seq_printf(m, "Device Minor: \t %d\n", node ? node->minor_index : -1);
|
|
seq_puts(m, "Bus Type: \t PCIe\n");
|
|
seq_puts(m, "DMA Size: \t 47 bits\n");
|
|
seq_puts(m, "DMA Mask: \t 0x7fffffffffff\n");
|
|
seq_printf(m, "Live PCI IRQ: \t %s\n", nv_rs_live_pci_irq() ? "Enabled" : "Disabled");
|
|
return 0;
|
|
}
|
|
|
|
static int nv_rs_proc_gpu_power_show(struct seq_file *m, void *v)
|
|
{
|
|
(void)v;
|
|
seq_puts(m, "Runtime D3 status: Not supported\n");
|
|
seq_puts(m, "Video Memory: Active\n\n");
|
|
seq_puts(m, "GPU Hardware Support:\n");
|
|
seq_puts(m, " Video Memory Self Refresh: Supported\n");
|
|
seq_puts(m, " Video Memory Off: Supported\n");
|
|
return 0;
|
|
}
|
|
|
|
static int nv_rs_proc_gpu_registry_show(struct seq_file *m, void *v)
|
|
{
|
|
(void)v;
|
|
seq_printf(m, "RuntimeEngines=%u\nLivePciIrq=%u\nDynBar1P2P=%u\nUnlockAdaTensor=%u\nDisableEcc=%u\nUnprivProfiling=%u\n",
|
|
nv_rs_runtime_engines() ? 1u : 0u,
|
|
nv_rs_live_pci_irq() ? 1u : 0u,
|
|
nv_rs_dyn_bar1_p2p() ? 1u : 0u,
|
|
nv_rs_unlock_ada_tensor() ? 1u : 0u,
|
|
nv_rs_disable_ecc() ? 1u : 0u,
|
|
nv_rs_unpriv_profiling() ? 1u : 0u);
|
|
return 0;
|
|
}
|
|
|
|
static void nv_rs_proc_init(void)
|
|
{
|
|
struct path existing;
|
|
if (!nv_rs_runtime_engines())
|
|
return;
|
|
if (kern_path("/proc/driver/nvidia", LOOKUP_FOLLOW, &existing) == 0) {
|
|
path_put(&existing);
|
|
pr_info("nvidia_rs: /proc/driver/nvidia already exists; skipping procfs tree\n");
|
|
return;
|
|
}
|
|
nv_rs_proc_nvidia = proc_mkdir("driver/nvidia", NULL);
|
|
if (!nv_rs_proc_nvidia)
|
|
return;
|
|
proc_create_single("params", 0444, nv_rs_proc_nvidia, nv_rs_proc_params_show);
|
|
proc_create_single("version", 0444, nv_rs_proc_nvidia, nv_rs_proc_version_show);
|
|
nv_rs_proc_gpus = proc_mkdir("gpus", nv_rs_proc_nvidia);
|
|
nv_rs_proc_caps = proc_mkdir("capabilities", nv_rs_proc_nvidia);
|
|
if (nv_rs_proc_caps) {
|
|
proc_create_single("fabric-imex-mgmt", 0444, nv_rs_proc_caps, nv_rs_proc_cap_show);
|
|
nv_rs_proc_caps_mig = proc_mkdir("mig", nv_rs_proc_caps);
|
|
if (nv_rs_proc_caps_mig) {
|
|
proc_create_single("config", 0444, nv_rs_proc_caps_mig, nv_rs_proc_cap_show);
|
|
proc_create_single("monitor", 0444, nv_rs_proc_caps_mig, nv_rs_proc_cap_show);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void nv_rs_proc_exit(void)
|
|
{
|
|
if (nv_rs_proc_nvidia) {
|
|
proc_remove(nv_rs_proc_nvidia);
|
|
nv_rs_proc_nvidia = NULL;
|
|
nv_rs_proc_gpus = NULL;
|
|
nv_rs_proc_caps = NULL;
|
|
nv_rs_proc_caps_mig = NULL;
|
|
}
|
|
}
|
|
#else
|
|
static inline void nv_rs_proc_init(void) {}
|
|
static inline void nv_rs_proc_exit(void) {}
|
|
#endif
|
|
|
|
static struct page *nv_rs_sysmem_pool_pop(void)
|
|
{
|
|
struct page *page = NULL;
|
|
mutex_lock(&nv_rs_sysmem_pool_lock);
|
|
if (!list_empty(&nv_rs_sysmem_pool)) {
|
|
page = list_first_entry(&nv_rs_sysmem_pool, struct page, lru);
|
|
list_del_init(&page->lru);
|
|
nv_rs_sysmem_pool_pages--;
|
|
}
|
|
mutex_unlock(&nv_rs_sysmem_pool_lock);
|
|
if (page)
|
|
clear_highpage(page);
|
|
return page;
|
|
}
|
|
|
|
static void nv_rs_sysmem_pool_push(struct page *page)
|
|
{
|
|
mutex_lock(&nv_rs_sysmem_pool_lock);
|
|
list_add(&page->lru, &nv_rs_sysmem_pool);
|
|
nv_rs_sysmem_pool_pages++;
|
|
mutex_unlock(&nv_rs_sysmem_pool_lock);
|
|
}
|
|
|
|
/*
|
|
* Trim pooled system memory pages down to at most retain_pages order-0 pages.
|
|
* Runs outside nv_rs_rmapi_lock in a sleepable context.
|
|
*/
|
|
static void nv_rs_rmapi_trim_page_pools(unsigned long retain_pages)
|
|
{
|
|
LIST_HEAD(reclaim);
|
|
struct page *page, *tmp;
|
|
|
|
mutex_lock(&nv_rs_sysmem_pool_lock);
|
|
while (nv_rs_sysmem_pool_pages > retain_pages && !list_empty(&nv_rs_sysmem_pool)) {
|
|
page = list_last_entry(&nv_rs_sysmem_pool, struct page, lru);
|
|
list_move(&page->lru, &reclaim);
|
|
nv_rs_sysmem_pool_pages--;
|
|
}
|
|
mutex_unlock(&nv_rs_sysmem_pool_lock);
|
|
|
|
list_for_each_entry_safe(page, tmp, &reclaim, lru) {
|
|
list_del_init(&page->lru);
|
|
__free_page(page);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Called on both control-fd and per-GPU device-fd close after nv_rs_rmapi_lock
|
|
* is dropped, returning pooled system pages to the OS per
|
|
* system_memory_pool_retain_mb.
|
|
*/
|
|
static void nv_rs_rmapi_trim_page_pools_on_client_close(void)
|
|
{
|
|
int retain_mb = READ_ONCE(system_memory_pool_retain_mb);
|
|
u64 retain_pages;
|
|
|
|
if (retain_mb < 0)
|
|
return;
|
|
|
|
retain_pages = ((u64)(unsigned int)retain_mb << 20) >> PAGE_SHIFT;
|
|
nv_rs_rmapi_trim_page_pools((unsigned long)retain_pages);
|
|
}
|
|
|
|
void *nv_rs_rmapi_kvzalloc(size_t size);
|
|
void nv_rs_rmapi_kvfree(void *ptr);
|
|
|
|
void *nv_rs_rmapi_kvzalloc(size_t size)
|
|
{
|
|
void *ptr = kvzalloc(size, GFP_KERNEL);
|
|
if (ptr)
|
|
atomic64_inc(&nv_rs_rmapi_kvalloc_live);
|
|
return ptr;
|
|
}
|
|
|
|
void nv_rs_rmapi_kvfree(void *ptr)
|
|
{
|
|
if (!ptr)
|
|
return;
|
|
atomic64_dec(&nv_rs_rmapi_kvalloc_live);
|
|
kvfree(ptr);
|
|
}
|
|
|
|
u32 nv_rs_rmapi_os_alloc_pages(u64 count, u64 page, bool contiguous, u32 cache, u64 *token);
|
|
void nv_rs_rmapi_os_free_pages(u64 token);
|
|
u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *token);
|
|
void nv_rs_rmapi_os_unlock_user_pages(u64 token);
|
|
u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size);
|
|
void nv_rs_rmapi_os_kernel_unmap(u64 token);
|
|
u32 nv_rs_rmapi_os_user_map(u64 token, u64 page_index, u32 page_offset, u64 *address);
|
|
u32 nv_rs_rmapi_os_match_offset(u64 token, u64 address, u64 *page_index);
|
|
u32 nv_rs_rmapi_os_add_context(s32 descriptor, u32 protection, u64 token, u64 page_index,
|
|
u32 caching);
|
|
u32 nv_rs_rmapi_os_map_register(u64 offset, u64 length, bool write_only, s32 descriptor,
|
|
u64 *address);
|
|
u32 nv_rs_rmapi_os_map_video(u64 offset, u64 length, s32 descriptor, u64 *address);
|
|
u32 nv_rs_rmapi_os_kernel_write(u64 token, u64 offset, const u8 *bytes, size_t length);
|
|
u32 nv_rs_rmapi_os_page_phys(u64 token, u64 page_index, u64 *phys);
|
|
u32 nv_rs_rmapi_os_page_dma(u32 gpu_id, u64 token, u64 page_index, u64 *dma_out);
|
|
void nv_rs_rmapi_os_flush_gpu_cache(void);
|
|
|
|
struct device *nv_rs_rmapi_gpu_device_get(u32 gpu_id)
|
|
{
|
|
unsigned long flags;
|
|
struct device *dev = NULL;
|
|
int i;
|
|
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
for (i = 0; i < NV_RS_RMAPI_MAX_GPUS; i++) {
|
|
if (nv_rs_rmapi_gpu_nodes[i].live &&
|
|
nv_rs_rmapi_gpu_nodes[i].gpu_id == gpu_id &&
|
|
nv_rs_rmapi_gpu_nodes[i].pdev) {
|
|
dev = get_device(&nv_rs_rmapi_gpu_nodes[i].pdev->dev);
|
|
break;
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
return dev;
|
|
}
|
|
|
|
static bool nv_rs_rmapi_gpu_device_live(u32 gpu_id, struct device *dev)
|
|
{
|
|
unsigned long flags;
|
|
bool live = false;
|
|
int i;
|
|
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
for (i = 0; i < NV_RS_RMAPI_MAX_GPUS; i++) {
|
|
if (nv_rs_rmapi_gpu_nodes[i].live &&
|
|
nv_rs_rmapi_gpu_nodes[i].gpu_id == gpu_id &&
|
|
nv_rs_rmapi_gpu_nodes[i].pdev &&
|
|
&nv_rs_rmapi_gpu_nodes[i].pdev->dev == dev) {
|
|
live = true;
|
|
break;
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
return live;
|
|
}
|
|
|
|
/* Raise the device's streaming DMA mask through the PCI adapter's access lock;
|
|
* the leases, their width (`dma_bits`) and the coherent mask stay as they are.
|
|
* The GPU's probe holds a 32-bit lease for its lifetime, so a plain
|
|
* renegotiation fails, and every page above 4 GiB that the DMA API then maps for
|
|
* the GPU is bounced through swiotlb (the GPU's writes never reach the page the
|
|
* CPU reads: libcuda waits forever at context creation). */
|
|
void nv_rs_rmapi_gpu_negotiate_dma(u32 gpu_id, unsigned int bits)
|
|
{
|
|
unsigned long flags;
|
|
struct nv_rs_pci *pci = NULL;
|
|
int i;
|
|
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
for (i = 0; i < NV_RS_RMAPI_MAX_GPUS; i++) {
|
|
if (nv_rs_rmapi_gpu_nodes[i].live &&
|
|
nv_rs_rmapi_gpu_nodes[i].gpu_id == gpu_id &&
|
|
nv_rs_rmapi_gpu_nodes[i].owner) {
|
|
pci = nv_rs_gpu_owner_pci_get(nv_rs_rmapi_gpu_nodes[i].owner);
|
|
break;
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
if (pci) {
|
|
(void)nv_rs_pci_widen_dma(pci, bits);
|
|
nv_rs_pci_put(pci);
|
|
}
|
|
}
|
|
|
|
static void nv_rs_rmapi_dma_map_destroy(struct nv_rs_rmapi_dma_map *dm)
|
|
{
|
|
u64 i;
|
|
for (i = 0; i < dm->count; i++) {
|
|
if (dm->encoded[i]) {
|
|
dma_unmap_page(dm->dev, (dma_addr_t)(dm->encoded[i] - 1),
|
|
PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
}
|
|
}
|
|
put_device(dm->dev);
|
|
kvfree(dm);
|
|
}
|
|
|
|
static void nv_rs_rmapi_alloc_unmap_dma(struct nv_rs_rmapi_alloc *alloc)
|
|
{
|
|
struct nv_rs_rmapi_dma_map *dm;
|
|
for (;;) {
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
dm = nv_rs_dma_reg_pop_for_alloc(alloc);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!dm)
|
|
break;
|
|
nv_rs_rmapi_dma_map_destroy(dm);
|
|
}
|
|
}
|
|
|
|
/* Unmap all retained per-GPU DMA records for `dev` before its PCI driver
|
|
* tears down the device's DMA context on removal. */
|
|
static void nv_rs_rmapi_allocs_unmap_dev(struct device *dev)
|
|
{
|
|
struct nv_rs_rmapi_dma_map *dm;
|
|
if (!dev)
|
|
return;
|
|
for (;;) {
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
dm = nv_rs_dma_reg_pop_for_dev(dev);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!dm)
|
|
break;
|
|
nv_rs_rmapi_dma_map_destroy(dm);
|
|
}
|
|
}
|
|
|
|
static struct nv_rs_rmapi_alloc *nv_rs_rmapi_alloc_find(u64 token)
|
|
{
|
|
return nv_rs_alloc_reg_find(token);
|
|
}
|
|
|
|
static void nv_rs_rmapi_alloc_unmap(struct nv_rs_rmapi_alloc *alloc)
|
|
{
|
|
if (alloc->kernel && alloc->count > 1)
|
|
vunmap(alloc->kernel);
|
|
alloc->kernel = NULL;
|
|
}
|
|
|
|
/* Releases DMA maps, kernel mapping, OS page references, and the record itself
|
|
* after Rust's AllocLedger has dropped its final reference. */
|
|
static void nv_rs_rmapi_alloc_destroy(struct nv_rs_rmapi_alloc *alloc)
|
|
{
|
|
u64 index;
|
|
nv_rs_rmapi_alloc_unmap_dma(alloc);
|
|
nv_rs_rmapi_alloc_unmap(alloc);
|
|
if (alloc->user_pinned) {
|
|
unpin_user_pages_dirty_lock(alloc->pages, alloc->count, alloc->writable);
|
|
} else {
|
|
int retain_mb = READ_ONCE(system_memory_pool_retain_mb);
|
|
for (index = 0; index < alloc->count; index++) {
|
|
if (retain_mb != 0)
|
|
nv_rs_sysmem_pool_push(alloc->pages[index]);
|
|
else
|
|
__free_page(alloc->pages[index]);
|
|
}
|
|
}
|
|
kvfree(alloc);
|
|
}
|
|
|
|
static void nv_rs_rmapi_alloc_put(struct nv_rs_rmapi_alloc *alloc)
|
|
{
|
|
bool destroy;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
destroy = nv_rs_alloc_reg_put_ptr(alloc);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (destroy)
|
|
nv_rs_rmapi_alloc_destroy(alloc);
|
|
}
|
|
|
|
/* Pins `count` user-space 4 KiB pages starting at `address` for class 0x71
|
|
* (NV01_MEMORY_SYSTEM_OS_DESCRIPTOR) so UVM/GPU page tables can reference their physical
|
|
* addresses directly. */
|
|
u32 nv_rs_rmapi_os_lock_user_pages(u64 address, u64 count, bool writable, u64 *token)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
u64 pinned = 0;
|
|
u64 pool;
|
|
u64 tok = 0;
|
|
bool ok;
|
|
unsigned int gup_flags = writable ? FOLL_WRITE : 0;
|
|
#ifdef FOLL_LONGTERM
|
|
/* Match stock 580.178.04 os-mlock.c: long-term GPU DMA pins must migrate out of CMA/movable zones. */
|
|
gup_flags |= FOLL_LONGTERM;
|
|
#endif
|
|
|
|
if (!count || !token || (address & (PAGE_SIZE - 1)))
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
pool = READ_ONCE(nv_rs_sysmem_pool_pages);
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
ok = nv_rs_alloc_reg_can_admit(count, true, pool,
|
|
nv_rs_rmapi_total_max_pages(),
|
|
nv_rs_rmapi_memory_max_pages());
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!ok)
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
alloc = kvzalloc(struct_size(alloc, pages, count), GFP_KERNEL);
|
|
if (!alloc)
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
|
|
while (pinned < count) {
|
|
u64 remain = count - pinned;
|
|
int chunk = remain > 1024 ? 1024 : (int)remain;
|
|
int got = pin_user_pages_fast((unsigned long)(address + (pinned << PAGE_SHIFT)),
|
|
chunk, gup_flags, &alloc->pages[pinned]);
|
|
if (got <= 0) {
|
|
if (pinned > 0)
|
|
unpin_user_pages(alloc->pages, pinned);
|
|
kvfree(alloc);
|
|
return NV_RS_STATUS_INVALID_ADDRESS;
|
|
}
|
|
pinned += (u64)got;
|
|
}
|
|
|
|
alloc->count = count;
|
|
alloc->cache = 0;
|
|
alloc->user_pinned = true;
|
|
alloc->writable = writable;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
ok = nv_rs_alloc_reg_insert(alloc, count, true, &tok);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!ok) {
|
|
unpin_user_pages(alloc->pages, count);
|
|
kvfree(alloc);
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
alloc->token = tok;
|
|
*token = tok;
|
|
return 0;
|
|
}
|
|
|
|
void nv_rs_rmapi_os_unlock_user_pages(u64 token)
|
|
{
|
|
void *ptr = NULL;
|
|
bool destroy;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
destroy = nv_rs_alloc_reg_mark_freed_and_put(token, &ptr);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (destroy && ptr)
|
|
nv_rs_rmapi_alloc_destroy(ptr);
|
|
}
|
|
|
|
/* nv_alloc_pages: `count` zeroed 4 KiB pages. Granules other than 4 KiB and contiguous
|
|
* runs of more than one page are not provided. */
|
|
u32 nv_rs_rmapi_os_alloc_pages(u64 count, u64 page, bool contiguous, u32 cache, u64 *token)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
u64 index;
|
|
u64 pool;
|
|
u64 tok = 0;
|
|
bool ok;
|
|
if (!count || cache > 2)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
if (page != PAGE_SIZE || (contiguous && count != 1))
|
|
return NV_RS_STATUS_NOT_SUPPORTED;
|
|
pool = READ_ONCE(nv_rs_sysmem_pool_pages);
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
ok = nv_rs_alloc_reg_can_admit(count, false, pool,
|
|
nv_rs_rmapi_total_max_pages(),
|
|
nv_rs_rmapi_memory_max_pages());
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!ok)
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
alloc = kvzalloc(struct_size(alloc, pages, count), GFP_KERNEL);
|
|
if (!alloc)
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
for (index = 0; index < count; index++) {
|
|
alloc->pages[index] = nv_rs_sysmem_pool_pop();
|
|
if (!alloc->pages[index])
|
|
alloc->pages[index] = alloc_page(GFP_KERNEL | __GFP_ZERO);
|
|
if (!alloc->pages[index]) {
|
|
while (index--)
|
|
__free_page(alloc->pages[index]);
|
|
kvfree(alloc);
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
}
|
|
alloc->count = count;
|
|
alloc->cache = cache;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
ok = nv_rs_alloc_reg_insert(alloc, count, false, &tok);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!ok) {
|
|
for (index = 0; index < count; index++)
|
|
__free_page(alloc->pages[index]);
|
|
kvfree(alloc);
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
alloc->token = tok;
|
|
*token = tok;
|
|
return 0;
|
|
}
|
|
|
|
/* nv_free_pages: the RM object is gone; the token names nothing from here on,
|
|
* while remaining in the Rust registry until users == 0 so hot-unplug DMA
|
|
* teardown never misses a record still held by a file mapping context. */
|
|
void nv_rs_rmapi_os_free_pages(u64 token)
|
|
{
|
|
void *ptr = NULL;
|
|
bool destroy;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
destroy = nv_rs_alloc_reg_mark_freed_and_put(token, &ptr);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!ptr)
|
|
return;
|
|
nv_rs_rmapi_alloc_unmap(ptr);
|
|
if (destroy)
|
|
nv_rs_rmapi_alloc_destroy(ptr);
|
|
}
|
|
|
|
/* nv_alloc_kernel_mapping of the whole allocation: the page's own address for one page,
|
|
* vmap for more. */
|
|
u32 nv_rs_rmapi_os_kernel_map(u64 token, u64 size)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (!alloc || size > alloc->count << PAGE_SHIFT)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
if (alloc->kernel)
|
|
return 0;
|
|
alloc->kernel = alloc->count == 1 ? page_address(alloc->pages[0])
|
|
: vmap(alloc->pages, alloc->count, VM_MAP, PAGE_KERNEL);
|
|
return alloc->kernel ? 0 : NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
|
|
void nv_rs_rmapi_os_kernel_unmap(u64 token)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (alloc)
|
|
nv_rs_rmapi_alloc_unmap(alloc);
|
|
}
|
|
|
|
/* Kernel write into a system-memory allocation (e.g. 0xc36f0108 notifier update). */
|
|
u32 nv_rs_rmapi_os_kernel_write(u64 token, u64 offset, const u8 *bytes, size_t length)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc = nv_rs_rmapi_alloc_find(token);
|
|
u64 total;
|
|
if (!alloc || !bytes)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
total = alloc->count << PAGE_SHIFT;
|
|
if (offset > total || length > total - offset)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
if (!alloc->kernel) {
|
|
u32 status = nv_rs_rmapi_os_kernel_map(token, total);
|
|
if (status)
|
|
return status;
|
|
}
|
|
memcpy((u8 *)alloc->kernel + offset, bytes, length);
|
|
return 0;
|
|
}
|
|
|
|
/* Resolves the physical address of one 4 KiB page of a 0x3e system-memory allocation. */
|
|
u32 nv_rs_rmapi_os_page_phys(u64 token, u64 page_index, u64 *phys)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
u32 rc = 0;
|
|
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (!alloc || !phys || page_index >= alloc->count)
|
|
rc = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
else
|
|
*phys = (u64)page_to_phys(alloc->pages[page_index]);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
return rc;
|
|
}
|
|
|
|
/* Resolves (and lazily IOMMU/DMA-maps) one 4 KiB page of a system-memory allocation for `gpu_id`. */
|
|
u32 nv_rs_rmapi_os_page_dma(u32 gpu_id, u64 token, u64 page_index, u64 *dma_out)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
struct nv_rs_rmapi_dma_map *dm = NULL;
|
|
struct device *dev;
|
|
dma_addr_t dma;
|
|
u32 rc = 0;
|
|
|
|
if (!dma_out)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
dev = nv_rs_rmapi_gpu_device_get(gpu_id);
|
|
if (dev)
|
|
nv_rs_rmapi_gpu_negotiate_dma(gpu_id, 47);
|
|
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (!alloc || page_index >= alloc->count) {
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (dev)
|
|
put_device(dev);
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
if (!dev) {
|
|
*dma_out = (u64)page_to_phys(alloc->pages[page_index]);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
return 0;
|
|
}
|
|
if (!nv_rs_rmapi_gpu_device_live(gpu_id, dev)) {
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
put_device(dev);
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
dm = nv_rs_dma_reg_find(alloc, gpu_id, dev);
|
|
if (dm) {
|
|
put_device(dev);
|
|
} else {
|
|
dm = kvzalloc(struct_size(dm, encoded, alloc->count), GFP_KERNEL);
|
|
if (!dm) {
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
put_device(dev);
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
dm->dev = dev;
|
|
dm->gpu_id = gpu_id;
|
|
dm->count = alloc->count;
|
|
if (!nv_rs_dma_reg_insert(alloc, gpu_id, dev, dm)) {
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
put_device(dev);
|
|
kvfree(dm);
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
}
|
|
if (!dm->encoded[page_index]) {
|
|
dma = dma_map_page(dm->dev, alloc->pages[page_index], 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
|
|
if (dma_mapping_error(dm->dev, dma)) {
|
|
rc = NV_RS_STATUS_NO_MEMORY;
|
|
} else {
|
|
dm->encoded[page_index] = (u64)dma + 1;
|
|
*dma_out = (u64)dma;
|
|
}
|
|
} else {
|
|
*dma_out = dm->encoded[page_index] - 1;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
return rc;
|
|
}
|
|
|
|
/* nv_alloc_user_mapping: the physical address of the page plus the offset in it. */
|
|
u32 nv_rs_rmapi_os_user_map(u64 token, u64 page_index, u32 page_offset, u64 *address)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (!alloc || page_index >= alloc->count || page_offset >= PAGE_SIZE)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
*address = (u64)page_to_phys(alloc->pages[page_index]) + page_offset;
|
|
return 0;
|
|
}
|
|
|
|
/* os_match_mmap_offset: the page of the allocation at this physical address. */
|
|
u32 nv_rs_rmapi_os_match_offset(u64 token, u64 address, u64 *page_index)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc = nv_rs_rmapi_alloc_find(token);
|
|
u64 index;
|
|
for (index = 0; alloc && index < alloc->count; index++) {
|
|
if ((u64)page_to_phys(alloc->pages[index]) == address) {
|
|
*page_index = index;
|
|
return 0;
|
|
}
|
|
}
|
|
return NV_RS_STATUS_OBJECT_NOT_FOUND;
|
|
}
|
|
|
|
/* nv_add_mapping_context_to_file for system memory: the descriptor must name a file of
|
|
* this driver's control node (INVALID_ARGUMENT otherwise, as for a closed descriptor), and
|
|
* a file takes one context for its lifetime (STATE_IN_USE). The file is only looked at
|
|
* here; fput of a user task defers the final release, which takes nv_rs_rmapi_lock. */
|
|
u32 nv_rs_rmapi_os_add_context(s32 descriptor, u32 protection, u64 token, u64 page_index,
|
|
u32 caching)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc = nv_rs_rmapi_alloc_find(token);
|
|
struct nv_rs_rmapi_file *context;
|
|
struct file *target = fget(descriptor);
|
|
u32 status = 0;
|
|
if (!target)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
context = target->private_data;
|
|
if (target->f_op != &nv_rs_rmapi_operations || !alloc || page_index >= alloc->count) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else {
|
|
struct nv_rs_rmapi_alloc *retained;
|
|
int rc;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
retained = nv_rs_alloc_reg_retain(token);
|
|
if (!retained) {
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else {
|
|
rc = nv_rs_file_map_insert_alloc(context->owner, retained, page_index,
|
|
protection, caching);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (rc == 1) {
|
|
nv_rs_rmapi_alloc_put(retained);
|
|
status = NV_RS_STATUS_STATE_IN_USE;
|
|
} else if (rc < 0) {
|
|
nv_rs_rmapi_alloc_put(retained);
|
|
status = NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
}
|
|
}
|
|
fput(target);
|
|
return status;
|
|
}
|
|
|
|
/* Attaches a BAR0 IO mapping context (0xc361 VOLTA_USERMODE_A or 0x90e6
|
|
* GF100_SUBDEVICE_MASTER) to a device-node file and returns BAR0 + offset. */
|
|
u32 nv_rs_rmapi_os_map_register(u64 offset, u64 length, bool write_only, s32 descriptor,
|
|
u64 *address)
|
|
{
|
|
struct file *target;
|
|
struct nv_rs_rmapi_file *context;
|
|
u64 io_phys = 0;
|
|
u32 status = 0;
|
|
if (!address || !length || ((offset | length) & ~PAGE_MASK))
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
target = fget(descriptor);
|
|
if (!target)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
context = target->private_data;
|
|
if (target->f_op != &nv_rs_rmapi_gpu_operations || !context || context->gpu < 0 ||
|
|
context->gpu >= NV_RS_RMAPI_MAX_GPUS) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else if (!nv_rs_gpu_slot_validate_bar0((u32)context->gpu, context->generation,
|
|
offset, length, &io_phys)) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else {
|
|
int rc;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
rc = nv_rs_file_map_insert_io(context->owner, io_phys, length,
|
|
write_only ? 2 : 1, 6);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (rc == 0)
|
|
*address = io_phys;
|
|
else if (rc == 1)
|
|
status = NV_RS_STATUS_STATE_IN_USE;
|
|
else
|
|
status = NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
fput(target);
|
|
return status;
|
|
}
|
|
|
|
/* Attaches a BAR1 video-memory IO mapping context (0x40 video memory / USERD) to a
|
|
* device-node file and returns BAR1 + offset. Sub-page lengths/offsets (e.g. 0x200-byte
|
|
* Volta USERD) are rounded to enclosing pages for remap_pfn_range while returning the exact
|
|
* BAR1 physical offset to the caller. */
|
|
u32 nv_rs_rmapi_os_map_video(u64 offset, u64 length, s32 descriptor, u64 *address)
|
|
{
|
|
struct file *target;
|
|
struct nv_rs_rmapi_file *context;
|
|
u64 aligned_off, aligned_len, io_phys = 0, user_addr = 0;
|
|
u32 status = 0;
|
|
if (!address || !length)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
aligned_off = offset & PAGE_MASK;
|
|
aligned_len = PAGE_ALIGN((offset & ~PAGE_MASK) + length);
|
|
if (!aligned_len)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
target = fget(descriptor);
|
|
if (!target)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
context = target->private_data;
|
|
if (target->f_op != &nv_rs_rmapi_gpu_operations || !context || context->gpu < 0 ||
|
|
context->gpu >= NV_RS_RMAPI_MAX_GPUS) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else if (!nv_rs_gpu_slot_validate_bar1((u32)context->gpu, context->generation,
|
|
aligned_off, aligned_len, offset,
|
|
&io_phys, &user_addr)) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else {
|
|
int rc;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
rc = nv_rs_file_map_insert_io(context->owner, io_phys, aligned_len, 3, 2);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (rc == 0)
|
|
*address = user_addr;
|
|
else if (rc == 1)
|
|
status = NV_RS_STATUS_STATE_IN_USE;
|
|
else
|
|
status = NV_RS_STATUS_NO_MEMORY;
|
|
}
|
|
fput(target);
|
|
return status;
|
|
}
|
|
|
|
/* The release of GPU-cacheable system memory. The original writes 1 to the BAR0
|
|
* registers 0x70000, 0x70010 and 0x70004 and polls each; that needs the GPU's register
|
|
* mapping, which these nodes do not have. Counted, nothing else. */
|
|
void nv_rs_rmapi_os_flush_gpu_cache(void)
|
|
{
|
|
nv_rs_rmapi_gpu_cache_flushes++;
|
|
}
|
|
|
|
/* Resolves a POSIX fd belonging to the calling process to the `owner` token of the
|
|
* `/dev/nvidiactl` file it refers to, or 0 if invalid or not a control node file.
|
|
* Caller holds nv_rs_rmapi_lock, preventing concurrent release of that owner. */
|
|
u64 nv_rs_rmapi_os_ctl_fd_owner(s32 descriptor)
|
|
{
|
|
struct file *target;
|
|
struct nv_rs_rmapi_file *context;
|
|
u64 owner = 0;
|
|
if (descriptor < 0)
|
|
return 0;
|
|
target = fget(descriptor);
|
|
if (target) {
|
|
context = target->private_data;
|
|
if (target->f_op == &nv_rs_rmapi_operations && context && context->gpu < 0)
|
|
owner = context->owner;
|
|
fput(target);
|
|
}
|
|
return owner;
|
|
}
|
|
|
|
static int nv_rs_rmapi_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct nv_rs_rmapi_file *context = nv_rs_rmapi_context(-1);
|
|
if (!context)
|
|
return -ENOMEM;
|
|
file->private_data = context;
|
|
return nonseekable_open(inode, file);
|
|
}
|
|
|
|
/* The registers the original's attach and copy-engine load read for the controls this node
|
|
* answers (the Rust side lists them: the fuse and Jtag status words and the eleven PCE-LCE
|
|
* mapping words of the copy engines). Read once per GPU, at the first
|
|
* open of its device node, through the reader the PCI driver registered. Nothing is
|
|
* written. A failed or missing read leaves the dependent control unavailable. Caller
|
|
* holds nv_rs_rmapi_lock, which also keeps the GPU from being removed meanwhile. */
|
|
static void nv_rs_rmapi_read_attach_registers(struct nv_rs_rmapi_gpu_node *node, int gpu)
|
|
{
|
|
u32 values[NV_RS_RMAPI_ATTACH_REGISTERS];
|
|
u32 count = nv_rs_rmapi_attach_register_count(), index;
|
|
if (!node->read32 || count > NV_RS_RMAPI_ATTACH_REGISTERS ||
|
|
!nv_rs_rmapi_gpu_needs_registers(nv_rs_rmapi_state, gpu))
|
|
return;
|
|
for (index = 0; index < count; index++) {
|
|
if (node->read32(node->cookie, nv_rs_rmapi_attach_register(index), &values[index]))
|
|
return;
|
|
}
|
|
nv_rs_rmapi_gpu_attach_registers(nv_rs_rmapi_state, gpu, values, count);
|
|
}
|
|
|
|
/* Device node. The open succeeds without engine initialization and attaches the GPU: it is
|
|
* then listed as attached and device objects can be made on it, but only the controls that
|
|
* need no engine state answer (see the implementation notes). The original runs the whole
|
|
* adapter initialization here and fails the open without it. */
|
|
static int nv_rs_rmapi_gpu_open_file(struct inode *inode, struct file *file)
|
|
{
|
|
/* misc_open left the miscdevice in private_data. */
|
|
struct nv_rs_rmapi_gpu_node *node =
|
|
container_of(file->private_data, struct nv_rs_rmapi_gpu_node, misc);
|
|
int gpu = (int)(node - nv_rs_rmapi_gpu_nodes);
|
|
struct nv_rs_rmapi_file *context = nv_rs_rmapi_context(gpu);
|
|
struct nv_rs_gpu_owner *owner = NULL;
|
|
int result;
|
|
if (!context)
|
|
return -ENOMEM;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (node->live && node->owner) {
|
|
owner = node->owner;
|
|
node->runtime_start_users++;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (owner && nv_rs_runtime_engines()) {
|
|
result = nv_rs_gpu_owner_runtime_start(owner);
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (node->runtime_start_users > 0)
|
|
node->runtime_start_users--;
|
|
if (node->runtime_start_users == 0)
|
|
wake_up_all(&nv_rs_rmapi_owner_wq);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (result) {
|
|
kfree(context);
|
|
return result;
|
|
}
|
|
} else if (owner) {
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (node->runtime_start_users > 0)
|
|
node->runtime_start_users--;
|
|
if (node->runtime_start_users == 0)
|
|
wake_up_all(&nv_rs_rmapi_owner_wq);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
}
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
result = node->live ? nv_rs_rmapi_gpu_open(nv_rs_rmapi_state, gpu) : -ENODEV;
|
|
context->generation = node->generation;
|
|
if (!result)
|
|
nv_rs_rmapi_read_attach_registers(node, gpu);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (result) {
|
|
kfree(context);
|
|
return result;
|
|
}
|
|
file->private_data = context;
|
|
return nonseekable_open(inode, file);
|
|
}
|
|
|
|
static int nv_rs_rmapi_release(struct inode *inode, struct file *file)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
struct nv_rs_rmapi_alloc *mapped_alloc;
|
|
(void)inode;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
nv_rs_rmapi_close(nv_rs_rmapi_state, context->owner);
|
|
if (context->gpu >= 0 &&
|
|
nv_rs_gpu_slot_is_live_gen((u32)context->gpu, context->generation))
|
|
nv_rs_rmapi_gpu_release(nv_rs_rmapi_state, context->gpu);
|
|
/* No mapping of this file is left (a mapping holds the file): its context goes. */
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
mapped_alloc = nv_rs_file_map_remove(context->owner);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (mapped_alloc)
|
|
nv_rs_rmapi_alloc_put(mapped_alloc);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (context->control)
|
|
fput(context->control);
|
|
kfree(context);
|
|
/* Return pooled system pages to the OS after client teardown (both control-fd
|
|
* and per-GPU device-fd closes run through here), outside nv_rs_rmapi_lock. */
|
|
nv_rs_rmapi_trim_page_pools_on_client_close();
|
|
return 0;
|
|
}
|
|
|
|
/* nv_is_gpu_accessible of the original's open layer: the device nodes the calling process
|
|
* holds open. Runs under the files lock (no sleeping), so it only collects the files'
|
|
* (minor, generation) pairs; nv_rs_rmapi_gpu_access checks them under the node lock. */
|
|
struct nv_rs_rmapi_access { u32 seen; unsigned int generation[NV_RS_RMAPI_MAX_GPUS]; };
|
|
static int nv_rs_rmapi_match_gpu(const void *cookie, struct file *file, unsigned int fd)
|
|
{
|
|
struct nv_rs_rmapi_access *access = (struct nv_rs_rmapi_access *)cookie;
|
|
(void)fd;
|
|
if (file->f_op == &nv_rs_rmapi_gpu_operations) {
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
/* private_data still holds the miscdevice while the open is in progress. */
|
|
if (context && context->owner == (u64)(unsigned long)context &&
|
|
!(access->seen & (1u << context->gpu))) {
|
|
access->seen |= 1u << context->gpu;
|
|
access->generation[context->gpu] = context->generation;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Mask of the minor numbers of live device nodes the calling process holds open. Call with
|
|
* nv_rs_rmapi_lock held. */
|
|
static u32 nv_rs_rmapi_gpu_access(const struct nv_rs_rmapi_access *access)
|
|
{
|
|
u32 mask = 0;
|
|
int gpu;
|
|
for (gpu = 0; gpu < NV_RS_RMAPI_MAX_GPUS; gpu++)
|
|
if ((access->seen & (1u << gpu)) &&
|
|
nv_rs_gpu_slot_is_live_gen((u32)gpu, access->generation[gpu]))
|
|
mask |= 1u << gpu;
|
|
return mask;
|
|
}
|
|
|
|
/* NV_ESC_REGISTER_FD: associates a control file with the calling file. */
|
|
static int nv_rs_rmapi_register(struct file *file, const u8 *top, size_t size)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data, *target_context = NULL;
|
|
struct file *target;
|
|
s32 fd;
|
|
u32 status;
|
|
if (size != sizeof(fd))
|
|
return -EINVAL;
|
|
memcpy(&fd, top, sizeof(fd));
|
|
target = fget(fd);
|
|
if (target && target->f_op == &nv_rs_rmapi_operations)
|
|
target_context = target->private_data;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
status = nv_rs_rmapi_register_fd(context->control != NULL, target_context != NULL,
|
|
target_context && target_context->control != NULL,
|
|
target == file);
|
|
if (!status) {
|
|
context->control = target;
|
|
target = NULL;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (target)
|
|
fput(target);
|
|
/* rm_ioctl's non-zero result becomes -EINVAL; the block goes back unchanged. */
|
|
return status ? -EINVAL : 0;
|
|
}
|
|
|
|
/* NV_ESC_QUERY_DEVICE_INTR (0xd5): 8-byte { u32 intrStatus; u32 status; } — device node only. */
|
|
static int nv_rs_rmapi_query_device_intr(struct file *file, u8 *top, size_t size)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
u32 intr = 0, status = 0;
|
|
|
|
if (context->gpu < 0 || size != 8)
|
|
return -EINVAL;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (context->gpu < NV_RS_RMAPI_MAX_GPUS) {
|
|
struct nv_rs_rmapi_gpu_node *node = &nv_rs_rmapi_gpu_nodes[context->gpu];
|
|
if (node->live && node->read32)
|
|
(void)node->read32(node->cookie, 0x100, &intr);
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
memcpy(top, &intr, sizeof(intr));
|
|
memcpy(top + 4, &status, sizeof(status));
|
|
return 0;
|
|
}
|
|
|
|
/* NV_ESC_SET_NUMA_STATUS (0xd8): 4-byte { s32 status; } — device node only. */
|
|
static int nv_rs_rmapi_set_numa_status(struct file *file, const u8 *top, size_t size)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
s32 st = 0;
|
|
|
|
if (context->gpu < 0 || size != 4)
|
|
return -EINVAL;
|
|
memcpy(&st, top, sizeof(st));
|
|
if (st < 0 || st > 5)
|
|
return -EINVAL;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (context->gpu >= 0 && context->gpu < NV_RS_RMAPI_MAX_GPUS)
|
|
(void)nv_rs_gpu_slot_set_numa((u32)context->gpu, st);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return 0;
|
|
}
|
|
|
|
/* NV_ESC_EXPORT_TO_DMABUF_FD (0xd9): stock-ABI dma-buf export backed by BAR1 P2PDMA.
|
|
* Layout verified against stock 580.178.04 nv_ioctl_export_to_dma_buf_fd_t (2608 bytes):
|
|
* off 0: s32 fd (OUT)
|
|
* off 24: u64 totalSize (IN)
|
|
* off 552: u64 offsets[128] (IN)
|
|
* off 1576: u64 sizes[128] (IN)
|
|
* off 2600: u32 status (OUT) */
|
|
static int nv_rs_rmapi_export_dmabuf(struct file *file, u8 *top, size_t size)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
struct pci_dev *pdev = NULL;
|
|
u64 total_size = 0, offset0 = 0, size0 = 0, export_size;
|
|
s32 fd = -1;
|
|
u32 status = 0x0000001fu; /* NV_ERR_INVALID_ARGUMENT */
|
|
int gpu;
|
|
|
|
/* Raw BAR1 range export bypasses per-client RM allocation ownership checks,
|
|
* so require CAP_SYS_ADMIN now that RMAPI device nodes are 0666. */
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
return -EPERM;
|
|
|
|
if (size != NV_RS_DMABUF_EXPORT_IOCTL_SIZE)
|
|
return -EINVAL;
|
|
|
|
memcpy(&total_size, top + 24, sizeof(total_size));
|
|
memcpy(&offset0, top + 552, sizeof(offset0));
|
|
memcpy(&size0, top + 1576, sizeof(size0));
|
|
export_size = size0 ? size0 : total_size;
|
|
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
gpu = context->gpu;
|
|
if (gpu >= 0 && gpu < NV_RS_RMAPI_MAX_GPUS && nv_rs_rmapi_gpu_nodes[gpu].live) {
|
|
pdev = nv_rs_rmapi_gpu_nodes[gpu].pdev;
|
|
} else {
|
|
for (gpu = 0; gpu < NV_RS_RMAPI_MAX_GPUS; gpu++) {
|
|
if (nv_rs_rmapi_gpu_nodes[gpu].live && nv_rs_rmapi_gpu_nodes[gpu].pdev) {
|
|
pdev = nv_rs_rmapi_gpu_nodes[gpu].pdev;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (pdev)
|
|
pci_dev_get(pdev);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
|
|
if (pdev && export_size > 0) {
|
|
int res = nv_rs_dmabuf_export_bar1_fd(pdev, offset0, export_size);
|
|
if (res >= 0) {
|
|
fd = res;
|
|
status = 0;
|
|
}
|
|
}
|
|
if (pdev)
|
|
pci_dev_put(pdev);
|
|
|
|
memcpy(top, &fd, sizeof(fd));
|
|
memcpy(top + 2600, &status, sizeof(status));
|
|
return 0;
|
|
}
|
|
|
|
static long nv_rs_rmapi_ioctl(struct file *file, unsigned int command, unsigned long argument)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
struct nv_rs_request request;
|
|
struct nv_rs_xfer xfer;
|
|
struct nv_rs_rmapi_write writes[3];
|
|
void __user *user;
|
|
u8 *top, *params = NULL, *nested = NULL;
|
|
u64 address = 0, owner = context->owner, nested_in = 0, nested_out = 0;
|
|
size_t length = 0, nested_len = 0;
|
|
u32 write_count = 0, index;
|
|
bool admin = capable(CAP_SYS_ADMIN), control_node = context->gpu < 0;
|
|
struct nv_rs_rmapi_access access = { 0 };
|
|
u32 gpu_access;
|
|
int result, step;
|
|
|
|
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 = u64_to_user_ptr(request.pointer);
|
|
top = kmalloc(request.size ? request.size : 1, GFP_KERNEL);
|
|
if (!top)
|
|
return -ENOMEM;
|
|
if (copy_from_user(top, user, request.size)) {
|
|
kfree(top);
|
|
return -EFAULT;
|
|
}
|
|
if (request.command == NV_RS_ESC_REGISTER_FD) {
|
|
result = nv_rs_rmapi_register(file, top, request.size);
|
|
goto copy_top;
|
|
}
|
|
if (request.command == NV_RS_ESC_QUERY_DEVICE_INTR) {
|
|
result = nv_rs_rmapi_query_device_intr(file, top, request.size);
|
|
goto copy_top;
|
|
}
|
|
if (request.command == NV_RS_ESC_SET_NUMA_STATUS) {
|
|
result = nv_rs_rmapi_set_numa_status(file, top, request.size);
|
|
goto copy_top;
|
|
}
|
|
if (request.command == NV_RS_ESC_EXPORT_TO_DMABUF_FD) {
|
|
result = nv_rs_rmapi_export_dmabuf(file, top, request.size);
|
|
goto copy_top;
|
|
}
|
|
if (request.command == NV_RS_ESC_RM_GET_EVENT_DATA) {
|
|
if (request.size != 16) {
|
|
result = -EINVAL;
|
|
goto out;
|
|
}
|
|
u32 more = 0;
|
|
u32 st = test_and_clear_bit(0, &context->events) ? 0u : 0x57u;
|
|
memcpy(top + 8, &more, sizeof(more));
|
|
memcpy(top + 12, &st, sizeof(st));
|
|
result = 0;
|
|
goto copy_top;
|
|
}
|
|
iterate_fd(current->files, 0, nv_rs_rmapi_match_gpu, &access);
|
|
if (mutex_lock_interruptible(&nv_rs_rmapi_lock)) {
|
|
kfree(top);
|
|
return -ERESTARTSYS;
|
|
}
|
|
/* A file that registered a control file acts for it (RmIoctl: clientOSInfo is
|
|
* nvfp->ctl_nvfp when set), so that an event object can be made through a device file
|
|
* under a client of the control file. The association is written once under this lock
|
|
* and the control file is held until release. The OS event escapes are handled in
|
|
* front of that (rm_ioctl) and record the calling file itself. */
|
|
if (context->control && request.command != NV_RS_ESC_ALLOC_OS_EVENT &&
|
|
request.command != NV_RS_ESC_FREE_OS_EVENT)
|
|
owner = ((struct nv_rs_rmapi_file *)context->control->private_data)->owner;
|
|
gpu_access = nv_rs_rmapi_gpu_access(&access);
|
|
step = nv_rs_rmapi_prepare(nv_rs_rmapi_state, owner, admin, control_node,
|
|
gpu_access, request.command, top, request.size, &address,
|
|
&length, &result);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (step < 0) {
|
|
result = step;
|
|
goto out;
|
|
}
|
|
if (step == 1)
|
|
goto copy_top;
|
|
if (length) {
|
|
params = kvmalloc(length, GFP_KERNEL);
|
|
if (!params) {
|
|
result = -ENOMEM;
|
|
goto out;
|
|
}
|
|
if (copy_from_user(params, u64_to_user_ptr(address), length)) {
|
|
result = nv_rs_rmapi_copy_fault(request.command, top, request.size, false);
|
|
length = 0;
|
|
goto copy_top;
|
|
}
|
|
if (nv_rs_rmapi_nested_plan(top, request.size, params, length, &nested_in, &nested_out,
|
|
&nested_len) == 1 && nested_len) {
|
|
nested = kvmalloc(nested_len, GFP_KERNEL);
|
|
if (!nested) {
|
|
result = -ENOMEM;
|
|
goto out;
|
|
}
|
|
if (copy_from_user(nested, u64_to_user_ptr(nested_in), nested_len)) {
|
|
result = nv_rs_rmapi_copy_fault(request.command, top, request.size, false);
|
|
length = 0;
|
|
nested_len = 0;
|
|
goto copy_top;
|
|
}
|
|
}
|
|
}
|
|
if (mutex_lock_interruptible(&nv_rs_rmapi_lock)) {
|
|
result = -ERESTARTSYS;
|
|
goto out;
|
|
}
|
|
/* The lock was dropped for the user copy: a GPU may have left meanwhile. */
|
|
if (!admin && nv_rs_unpriv_profiling()) {
|
|
if (request.command == 0x2a && request.size >= 12) {
|
|
u32 ctrl_cmd = *(u32 *)(top + 8);
|
|
u32 cls = ctrl_cmd >> 16;
|
|
u32 cat = ctrl_cmd >> 8;
|
|
if (cat == 0x208020u || cat == 0x208030u ||
|
|
cls == 0xb0ccu || cls == 0xb1ccu || cls == 0xb2ccu)
|
|
admin = true;
|
|
} else if (request.command == 0x2b && request.size >= 16) {
|
|
u32 hclass = *(u32 *)(top + 12);
|
|
if (hclass == 0xb0ccu || hclass == 0xb1ccu || hclass == 0xb2ccu)
|
|
admin = true;
|
|
}
|
|
}
|
|
gpu_access = nv_rs_rmapi_gpu_access(&access);
|
|
result = nv_rs_rmapi_execute(nv_rs_rmapi_state, owner, admin, control_node,
|
|
gpu_access, request.command, top, request.size, params, length,
|
|
nested, nested_len, writes, &write_count);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (nv_rs_runtime_engines() && result != 0)
|
|
pr_info_ratelimited("nvidia_rs: rt RM_ESC cmd=0x%x size=0x%x rc=%d\n",
|
|
request.command, request.size, result);
|
|
if (nv_rs_runtime_engines() && result == 0) {
|
|
if (request.command == 0x2b && request.size >= 0x20) {
|
|
u32 hclass = *(u32 *)(top + 12);
|
|
u32 st = request.size >= 0x30 ? *(u32 *)(top + 40) : *(u32 *)(top + 28);
|
|
if (st != 0)
|
|
pr_info_ratelimited("nvidia_rs: rt RM_ALLOC class=0x%x status=0x%x\n", hclass, st);
|
|
} else if (request.command == 0x2a && request.size >= 0x20) {
|
|
u32 cmd = *(u32 *)(top + 8);
|
|
u32 st = *(u32 *)(top + 28);
|
|
if (st != 0 && cmd != 0x2080182a)
|
|
pr_info_ratelimited("nvidia_rs: rt RM_CONTROL cmd=0x%x status=0x%x\n", cmd, st);
|
|
}
|
|
}
|
|
for (index = 0; index < write_count; index++) {
|
|
if (copy_to_user(u64_to_user_ptr(writes[index].address), writes[index].data,
|
|
writes[index].length)) {
|
|
nv_rs_rmapi_copy_fault(request.command, top, request.size, true);
|
|
break;
|
|
}
|
|
}
|
|
if (nested_len && copy_to_user(u64_to_user_ptr(nested_out), nested, nested_len))
|
|
nv_rs_rmapi_copy_fault(request.command, top, request.size, true);
|
|
if (length && copy_to_user(u64_to_user_ptr(address), params, length))
|
|
nv_rs_rmapi_copy_fault(request.command, top, request.size, true);
|
|
copy_top:
|
|
/* As nvidia_ioctl: the block goes back whatever the result is. */
|
|
if (copy_to_user(user, top, request.size))
|
|
result = -EFAULT;
|
|
out:
|
|
kvfree(nested);
|
|
kvfree(params);
|
|
kfree(top);
|
|
return result;
|
|
}
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
static long nv_rs_rmapi_compat_ioctl(struct file *file, unsigned int command, unsigned long arg)
|
|
{
|
|
return nv_rs_rmapi_ioctl(file, command, (unsigned long)compat_ptr(arg));
|
|
}
|
|
#endif
|
|
|
|
/* All pages are inserted by mmap; nothing is ever faulted in (nvidia_fault on the
|
|
* control node). */
|
|
static vm_fault_t nv_rs_rmapi_fault(struct vm_fault *fault)
|
|
{
|
|
(void)fault;
|
|
return VM_FAULT_SIGBUS;
|
|
}
|
|
static const struct vm_operations_struct nv_rs_rmapi_vm_operations = {
|
|
.fault = nv_rs_rmapi_fault,
|
|
};
|
|
|
|
/* nvidia_mmap_helper for the control node (nv-mmap.c): a file without a mapping context
|
|
* is refused with EINVAL (always the case for a device-node file here: BAR mappings do
|
|
* not exist), the offset must be 0, the mapping starts at the context's page and must end
|
|
* inside the allocation (ERANGE). The pages are inserted at once with vm_insert_page,
|
|
* whose page references keep them alive while mapped.
|
|
*
|
|
* Differences from the original: a private mapping is refused with EINVAL (the original
|
|
* turns it into a shared one by setting VM_SHARED afterwards); the pages are always mapped
|
|
* cached (the original applies the allocation's cache type to the protection, after
|
|
* changing the pages' kernel attributes at allocation); a read-only context is applied
|
|
* before the pages are inserted, not after.
|
|
*
|
|
* The context is immutable once valid and holds a use of the allocation until the file is
|
|
* released, so nothing is locked here. */
|
|
static inline void nv_rs_vm_flags_set(struct vm_area_struct *vma, vm_flags_t flags)
|
|
{
|
|
#if defined(NV_RS_HAVE_VMA_FLAGS_SET_WORD)
|
|
vma_flags_set_word(&vma->flags, flags);
|
|
#elif defined(NV_RS_HAVE_VM_FLAGS_SET) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
|
|
vm_flags_set(vma, flags);
|
|
#else
|
|
vma->vm_flags |= flags;
|
|
#endif
|
|
}
|
|
|
|
static inline void nv_rs_vm_flags_clear(struct vm_area_struct *vma, vm_flags_t flags)
|
|
{
|
|
#if defined(NV_RS_HAVE_VMA_FLAGS_SET_WORD)
|
|
vma_flags_clear_word(&vma->flags, flags);
|
|
#elif defined(NV_RS_HAVE_VM_FLAGS_SET) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
|
|
vm_flags_clear(vma, flags);
|
|
#else
|
|
vma->vm_flags &= ~flags;
|
|
#endif
|
|
}
|
|
|
|
static int nv_rs_rmapi_mmap(struct file *file, struct vm_area_struct *vma)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
struct nv_rs_file_map_record mapping;
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
unsigned long address = vma->vm_start;
|
|
u64 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT, index;
|
|
bool has_map;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
has_map = nv_rs_file_map_get(context->owner, &mapping);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!has_map)
|
|
return -EINVAL;
|
|
if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_SHARED))
|
|
return -EINVAL;
|
|
if (mapping.is_io) {
|
|
unsigned long size = vma->vm_end - vma->vm_start;
|
|
struct nv_rs_pci *pci = NULL;
|
|
struct pci_dev *pdev = NULL;
|
|
unsigned long flags;
|
|
int rc;
|
|
if (!size || size > mapping.io_bytes)
|
|
return -ERANGE;
|
|
if (context->gpu < 0 || context->gpu >= NV_RS_RMAPI_MAX_GPUS)
|
|
return -ENODEV;
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
if (nv_rs_rmapi_gpu_nodes[context->gpu].live &&
|
|
nv_rs_rmapi_gpu_nodes[context->gpu].generation == context->generation) {
|
|
if (nv_rs_rmapi_gpu_nodes[context->gpu].owner)
|
|
pci = nv_rs_gpu_owner_pci_get(nv_rs_rmapi_gpu_nodes[context->gpu].owner);
|
|
else if (nv_rs_rmapi_gpu_nodes[context->gpu].pdev)
|
|
pdev = pci_dev_get(nv_rs_rmapi_gpu_nodes[context->gpu].pdev);
|
|
}
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
if (!pci && !pdev)
|
|
return -ENODEV;
|
|
if (!(mapping.protection & 2)) {
|
|
nv_rs_vm_flags_clear(vma, VM_WRITE | VM_MAYWRITE);
|
|
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
|
|
}
|
|
if (mapping.caching == 2)
|
|
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
|
|
else
|
|
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
|
|
nv_rs_vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
|
|
rc = remap_pfn_range(vma, vma->vm_start, mapping.io_phys >> PAGE_SHIFT,
|
|
size, vma->vm_page_prot);
|
|
if (pci)
|
|
nv_rs_pci_put(pci);
|
|
if (pdev)
|
|
pci_dev_put(pdev);
|
|
return rc;
|
|
}
|
|
alloc = (struct nv_rs_rmapi_alloc *)mapping.alloc_ptr;
|
|
if (!alloc || mapping.page_index + pages > alloc->count)
|
|
return -ERANGE;
|
|
if (!(mapping.protection & 2)) {
|
|
nv_rs_vm_flags_clear(vma, VM_WRITE | VM_MAYWRITE);
|
|
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
|
|
}
|
|
nv_rs_vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
|
|
vma->vm_ops = &nv_rs_rmapi_vm_operations;
|
|
for (index = mapping.page_index; index < mapping.page_index + pages; index++) {
|
|
/* On failure the caller removes the area and with it the pages inserted so far. */
|
|
if (vm_insert_page(vma, address, alloc->pages[index]))
|
|
return -EAGAIN;
|
|
address += PAGE_SIZE;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Post a dataless OS event to `owner` (matching stock `nv_post_event` without data).
|
|
* Caller must hold `nv_rs_rmapi_lock` so the file context stays alive. */
|
|
void nv_rs_rmapi_file_post(u64 owner)
|
|
{
|
|
struct nv_rs_rmapi_file *context = (struct nv_rs_rmapi_file *)(unsigned long)owner;
|
|
if (!context)
|
|
return;
|
|
set_bit(0, &context->events);
|
|
wake_up_interruptible(&context->wait);
|
|
}
|
|
|
|
/* The engines' non-stall interrupts of `gpu` whose event slots are in `mask` happened (ga10x_events.rs, from an IRQ
|
|
* thread): the OS-event files registered for them are posted (nv_rs_gpu_events). The lock is only tried: -EBUSY while
|
|
* the node is busy (the caller tries again; it never waits, so stopping it under the lock cannot deadlock); otherwise
|
|
* the number of non-stall events registered on the GPU. */
|
|
#ifndef NV_RS_PCI_TEST_EDU /* the guest test module has no GA10x / GV100 event code */
|
|
int nv_rs_rmapi_engine_events(u32 gpu, u32 mask);
|
|
int nv_rs_rmapi_engine_events(u32 gpu, u32 mask)
|
|
{
|
|
extern u32 nv_rs_gpu_events(void *node, u32 gpu, u32 mask);
|
|
int n;
|
|
|
|
if (!mutex_trylock(&nv_rs_rmapi_lock))
|
|
return -EBUSY;
|
|
n = nv_rs_rmapi_state ? (int)nv_rs_gpu_events(nv_rs_rmapi_state, gpu, mask) : 0;
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return n;
|
|
}
|
|
|
|
/* An IRQ thread ran out of tries (the node busy for ~200 ms in one go): the slots stay pending in the event code and
|
|
* this work posts them once the lock is free (nv_rs_gpu_events_retry), without waiting for another interrupt. It
|
|
* only tries the lock too (again every 5 ms), so a teardown holding the lock never waits for it; cancelled at node
|
|
* exit. */
|
|
static void nv_rs_rmapi_events_retry_fn(struct work_struct *work);
|
|
static DECLARE_DELAYED_WORK(nv_rs_rmapi_events_retry, nv_rs_rmapi_events_retry_fn);
|
|
static void nv_rs_rmapi_events_retry_fn(struct work_struct *work)
|
|
{
|
|
extern void nv_rs_gpu_events_retry(void *node);
|
|
|
|
if (!mutex_trylock(&nv_rs_rmapi_lock)) {
|
|
schedule_delayed_work(&nv_rs_rmapi_events_retry, msecs_to_jiffies(5));
|
|
return;
|
|
}
|
|
if (nv_rs_rmapi_state)
|
|
nv_rs_gpu_events_retry(nv_rs_rmapi_state);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
}
|
|
|
|
void nv_rs_rmapi_engine_events_later(void);
|
|
void nv_rs_rmapi_engine_events_later(void)
|
|
{
|
|
schedule_delayed_work(&nv_rs_rmapi_events_retry, msecs_to_jiffies(1));
|
|
}
|
|
#endif
|
|
|
|
static __poll_t nv_rs_rmapi_poll(struct file *file, poll_table *wait)
|
|
{
|
|
struct nv_rs_rmapi_file *context = file->private_data;
|
|
if (!(file->f_flags & O_NONBLOCK))
|
|
poll_wait(file, &context->wait, wait);
|
|
/* Report a posted dataless event once, matching stock `nvidia_poll`. */
|
|
if (test_and_clear_bit(0, &context->events))
|
|
return EPOLLPRI | EPOLLIN;
|
|
return 0;
|
|
}
|
|
|
|
static const struct file_operations nv_rs_rmapi_operations = {
|
|
.owner = THIS_MODULE, .open = nv_rs_rmapi_open, .release = nv_rs_rmapi_release,
|
|
.unlocked_ioctl = nv_rs_rmapi_ioctl,
|
|
#ifdef CONFIG_COMPAT
|
|
.compat_ioctl = nv_rs_rmapi_compat_ioctl,
|
|
#endif
|
|
.mmap = nv_rs_rmapi_mmap, .poll = nv_rs_rmapi_poll,
|
|
/* Match stock 580.178.04 nvidia_fops: omit .llseek (NULL -> -ESPIPE on all kernels). */
|
|
};
|
|
static const struct file_operations nv_rs_rmapi_gpu_operations = {
|
|
.owner = THIS_MODULE, .open = nv_rs_rmapi_gpu_open_file, .release = nv_rs_rmapi_release,
|
|
.unlocked_ioctl = nv_rs_rmapi_ioctl,
|
|
#ifdef CONFIG_COMPAT
|
|
.compat_ioctl = nv_rs_rmapi_compat_ioctl,
|
|
#endif
|
|
.mmap = nv_rs_rmapi_mmap, .poll = nv_rs_rmapi_poll,
|
|
/* Match stock 580.178.04 nvidia_fops: omit .llseek (NULL -> -ESPIPE on all kernels). */
|
|
};
|
|
static struct miscdevice nv_rs_rmapi_device = {
|
|
.minor = MISC_DYNAMIC_MINOR, .name = "nvidia-rs-rmapi-ctl",
|
|
.fops = &nv_rs_rmapi_operations, .mode = 0666,
|
|
};
|
|
|
|
/* Lists the GPU on the control node and creates its device node
|
|
* /dev/nvidia-rs-rmapi-gpu<minor>. Returns the minor number or a negative errno. */
|
|
int nv_rs_rmapi_node_gpu_add(struct pci_dev *pdev, u32 boot0)
|
|
{
|
|
struct nv_rs_rmapi_gpu gpu = {
|
|
.domain = pci_domain_nr(pdev->bus), .bus = pdev->bus->number,
|
|
.slot = PCI_SLOT(pdev->devfn), .function = PCI_FUNC(pdev->devfn),
|
|
.vendor = pdev->vendor, .device = pdev->device, .irq = pdev->irq,
|
|
.bar0 = pci_resource_start(pdev, 0), .bar0_size = pci_resource_len(pdev, 0),
|
|
.bar1 = pci_resource_start(pdev, 1), .bar1_size = pci_resource_len(pdev, 1),
|
|
.bar2 = pci_resource_start(pdev, 3), .bar2_size = pci_resource_len(pdev, 3),
|
|
.subsystem_vendor = pdev->subsystem_vendor, .subsystem = pdev->subsystem_device,
|
|
.revision = pdev->revision, .boot0 = boot0,
|
|
};
|
|
struct nv_rs_rmapi_gpu_node *node;
|
|
int minor, result;
|
|
if (!nv_rs_rmapi_state)
|
|
return -ENODEV;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
minor = nv_rs_rmapi_gpu_add(nv_rs_rmapi_state, &gpu);
|
|
if (minor >= NV_RS_RMAPI_MAX_GPUS || (minor >= 0 && nv_rs_rmapi_gpu_nodes[minor].live)) {
|
|
nv_rs_rmapi_gpu_remove(nv_rs_rmapi_state, gpu.domain, gpu.bus, gpu.slot);
|
|
minor = -ENOSPC;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (minor < 0)
|
|
return minor;
|
|
/* Registered outside the lock: an open may arrive at once and takes the lock. */
|
|
node = &nv_rs_rmapi_gpu_nodes[minor];
|
|
memset(&node->misc, 0, sizeof(node->misc));
|
|
snprintf(node->name, sizeof(node->name), "nvidia-rs-rmapi-gpu%d", minor);
|
|
node->misc.minor = MISC_DYNAMIC_MINOR;
|
|
node->misc.name = node->name;
|
|
node->misc.fops = &nv_rs_rmapi_gpu_operations;
|
|
node->misc.mode = 0666;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
node->read32 = NULL;
|
|
node->release = NULL;
|
|
node->cookie = NULL;
|
|
node->minor_index = minor;
|
|
{
|
|
unsigned long flags;
|
|
u32 gpu_id = (((u32)gpu.domain & 0xffffU) << 16) | ((u32)gpu.bus << 8) | (u32)gpu.slot;
|
|
node->generation = nv_rs_gpu_slot_activate((u32)minor, gpu_id,
|
|
gpu.bar0, gpu.bar0_size,
|
|
gpu.bar1, gpu.bar1_size);
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
node->pdev = pdev;
|
|
node->gpu_id = gpu_id;
|
|
node->live = true;
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
result = misc_register(&node->misc);
|
|
if (result) {
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
node->generation = nv_rs_gpu_slot_deactivate((u32)minor);
|
|
node->live = false;
|
|
nv_rs_rmapi_gpu_remove(nv_rs_rmapi_state, gpu.domain, gpu.bus, gpu.slot);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return result;
|
|
}
|
|
nv_rs_modeset_register_gpu(((gpu.domain & 0xffffU) << 16) | ((u32)gpu.bus << 8) | (u32)gpu.slot,
|
|
gpu.domain, gpu.bus, gpu.slot, gpu.function, &pdev->dev);
|
|
#ifdef CONFIG_PROC_FS
|
|
if (nv_rs_proc_gpus) {
|
|
snprintf(node->bdf, sizeof(node->bdf), "%04x:%02x:%02x.%u",
|
|
gpu.domain, gpu.bus, gpu.slot, gpu.function);
|
|
node->proc_dir = proc_mkdir(node->bdf, nv_rs_proc_gpus);
|
|
if (node->proc_dir) {
|
|
proc_create_single_data("information", 0444, node->proc_dir,
|
|
nv_rs_proc_gpu_info_show, node);
|
|
proc_create_single_data("power", 0444, node->proc_dir,
|
|
nv_rs_proc_gpu_power_show, node);
|
|
proc_create_single_data("registry", 0444, node->proc_dir,
|
|
nv_rs_proc_gpu_registry_show, node);
|
|
}
|
|
}
|
|
#endif
|
|
return minor;
|
|
}
|
|
|
|
int nv_rs_rmapi_node_gpu_reader(struct pci_dev *pdev,
|
|
int (*read32)(void *cookie, u64 offset, u32 *value),
|
|
void (*release)(void *cookie), void *cookie)
|
|
{
|
|
int minor;
|
|
if (!nv_rs_rmapi_state)
|
|
return -ENODEV;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
minor = nv_rs_rmapi_gpu_minor(nv_rs_rmapi_state, pci_domain_nr(pdev->bus),
|
|
pdev->bus->number, PCI_SLOT(pdev->devfn));
|
|
if (minor >= 0 && minor < NV_RS_RMAPI_MAX_GPUS && nv_rs_rmapi_gpu_nodes[minor].live) {
|
|
nv_rs_rmapi_gpu_nodes[minor].read32 = read32;
|
|
nv_rs_rmapi_gpu_nodes[minor].release = release;
|
|
nv_rs_rmapi_gpu_nodes[minor].cookie = cookie;
|
|
minor = 0;
|
|
} else {
|
|
minor = -ENOENT;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return minor;
|
|
}
|
|
|
|
int nv_rs_rmapi_node_gpu_runtime(struct pci_dev *pdev, struct nv_rs_gpu_owner *owner)
|
|
{
|
|
int minor;
|
|
if (!nv_rs_rmapi_state)
|
|
return -ENODEV;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
minor = nv_rs_rmapi_gpu_minor(nv_rs_rmapi_state, pci_domain_nr(pdev->bus),
|
|
pdev->bus->number, PCI_SLOT(pdev->devfn));
|
|
if (minor >= 0 && minor < NV_RS_RMAPI_MAX_GPUS && nv_rs_rmapi_gpu_nodes[minor].live) {
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
nv_rs_rmapi_gpu_nodes[minor].owner = owner;
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
minor = 0;
|
|
} else {
|
|
minor = -ENOENT;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return minor;
|
|
}
|
|
|
|
static bool nv_rs_rmapi_node_start_idle(int minor)
|
|
{
|
|
bool idle;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
idle = nv_rs_rmapi_gpu_nodes[minor].runtime_start_users == 0;
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return idle;
|
|
}
|
|
|
|
/* The PCI function is leaving. Files of its device node may still be open: they keep
|
|
* working as files (their clients are freed at release), but the GPU is gone from the
|
|
* table, its devices are freed here and new opens fail. */
|
|
void nv_rs_rmapi_node_gpu_remove(struct pci_dev *pdev)
|
|
{
|
|
void (*release)(void *cookie) = NULL;
|
|
void *cookie = NULL;
|
|
struct nv_rs_gpu_owner *owner = NULL;
|
|
int minor;
|
|
if (!nv_rs_rmapi_state)
|
|
return;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
minor = nv_rs_rmapi_gpu_minor(nv_rs_rmapi_state, pci_domain_nr(pdev->bus),
|
|
pdev->bus->number, PCI_SLOT(pdev->devfn));
|
|
nv_rs_rmapi_gpu_remove(nv_rs_rmapi_state, pci_domain_nr(pdev->bus), pdev->bus->number,
|
|
PCI_SLOT(pdev->devfn));
|
|
if (minor >= 0 && minor < NV_RS_RMAPI_MAX_GPUS && nv_rs_rmapi_gpu_nodes[minor].live) {
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
nv_rs_rmapi_gpu_nodes[minor].live = false;
|
|
nv_rs_rmapi_gpu_nodes[minor].pdev = NULL;
|
|
owner = nv_rs_rmapi_gpu_nodes[minor].owner;
|
|
nv_rs_rmapi_gpu_nodes[minor].owner = NULL;
|
|
spin_unlock_irqrestore(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
nv_rs_rmapi_gpu_nodes[minor].generation = nv_rs_gpu_slot_deactivate((u32)minor);
|
|
release = nv_rs_rmapi_gpu_nodes[minor].release;
|
|
cookie = nv_rs_rmapi_gpu_nodes[minor].cookie;
|
|
nv_rs_rmapi_gpu_nodes[minor].read32 = NULL;
|
|
nv_rs_rmapi_gpu_nodes[minor].release = NULL;
|
|
nv_rs_rmapi_gpu_nodes[minor].cookie = NULL;
|
|
} else {
|
|
minor = -1;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (minor >= 0)
|
|
wait_event(nv_rs_rmapi_owner_wq, nv_rs_rmapi_node_start_idle(minor));
|
|
if (owner)
|
|
nv_rs_gpu_owner_runtime_stop(owner);
|
|
nv_rs_rmapi_allocs_unmap_dev(&pdev->dev);
|
|
nv_rs_uvm_unmap_gpu_dev(&pdev->dev);
|
|
if (minor >= 0) {
|
|
#ifdef CONFIG_PROC_FS
|
|
if (nv_rs_rmapi_gpu_nodes[minor].proc_dir) {
|
|
proc_remove(nv_rs_rmapi_gpu_nodes[minor].proc_dir);
|
|
nv_rs_rmapi_gpu_nodes[minor].proc_dir = NULL;
|
|
}
|
|
#endif
|
|
misc_deregister(&nv_rs_rmapi_gpu_nodes[minor].misc);
|
|
}
|
|
nv_rs_modeset_unregister_gpu((((u32)pci_domain_nr(pdev->bus) & 0xffffU) << 16) |
|
|
((u32)pdev->bus->number << 8) | (u32)PCI_SLOT(pdev->devfn));
|
|
if (release)
|
|
release(cookie);
|
|
}
|
|
|
|
int nv_rs_rmapi_kernel_open_gpu(u32 gpu_id)
|
|
{
|
|
u32 domain = (gpu_id >> 16) & 0xffffU;
|
|
u8 bus = (u8)((gpu_id >> 8) & 0xffU);
|
|
u8 slot = (u8)(gpu_id & 0xffU);
|
|
struct nv_rs_gpu_owner *owner = NULL;
|
|
struct nv_rs_rmapi_gpu_node *node;
|
|
int minor, result;
|
|
|
|
if (!nv_rs_rmapi_state)
|
|
return -ENODEV;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
minor = nv_rs_rmapi_gpu_minor(nv_rs_rmapi_state, domain, bus, slot);
|
|
if (minor >= 0 && minor < NV_RS_RMAPI_MAX_GPUS && nv_rs_rmapi_gpu_nodes[minor].live) {
|
|
owner = nv_rs_rmapi_gpu_nodes[minor].owner;
|
|
if (owner)
|
|
nv_rs_rmapi_gpu_nodes[minor].runtime_start_users++;
|
|
} else {
|
|
minor = -ENODEV;
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (minor < 0)
|
|
return minor;
|
|
if (owner && nv_rs_runtime_engines()) {
|
|
result = nv_rs_gpu_owner_runtime_start(owner);
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (nv_rs_rmapi_gpu_nodes[minor].runtime_start_users > 0)
|
|
nv_rs_rmapi_gpu_nodes[minor].runtime_start_users--;
|
|
if (nv_rs_rmapi_gpu_nodes[minor].runtime_start_users == 0)
|
|
wake_up_all(&nv_rs_rmapi_owner_wq);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (result)
|
|
return result;
|
|
} else if (owner) {
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (nv_rs_rmapi_gpu_nodes[minor].runtime_start_users > 0)
|
|
nv_rs_rmapi_gpu_nodes[minor].runtime_start_users--;
|
|
if (nv_rs_rmapi_gpu_nodes[minor].runtime_start_users == 0)
|
|
wake_up_all(&nv_rs_rmapi_owner_wq);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
}
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
node = &nv_rs_rmapi_gpu_nodes[minor];
|
|
result = node->live ? nv_rs_rmapi_gpu_open(nv_rs_rmapi_state, (u32)minor) : -ENODEV;
|
|
if (!result)
|
|
nv_rs_rmapi_read_attach_registers(node, (u32)minor);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
return result;
|
|
}
|
|
|
|
void nv_rs_rmapi_kernel_close_gpu(u32 gpu_id)
|
|
{
|
|
u32 domain = (gpu_id >> 16) & 0xffffU;
|
|
u8 bus = (u8)((gpu_id >> 8) & 0xffU);
|
|
u8 slot = (u8)(gpu_id & 0xffU);
|
|
int minor;
|
|
|
|
if (!nv_rs_rmapi_state)
|
|
return;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
minor = nv_rs_rmapi_gpu_minor(nv_rs_rmapi_state, domain, bus, slot);
|
|
if (minor >= 0 && minor < NV_RS_RMAPI_MAX_GPUS)
|
|
nv_rs_rmapi_gpu_release(nv_rs_rmapi_state, (u32)minor);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
}
|
|
|
|
u32 nv_rs_rmapi_handle_rc_fault(u32 gpu_id, u64 inst_ptr, u64 fault_va, u32 xid_code)
|
|
{
|
|
u32 chid = 0;
|
|
u32 tsg_id = 0;
|
|
u32 diag[3] = {0, 0, 0};
|
|
u32 notified = 0;
|
|
char bdf[32];
|
|
|
|
if (!nv_rs_rmapi_state || !inst_ptr)
|
|
return 0;
|
|
snprintf(bdf, sizeof(bdf), "%04x:%02x:%02x.0",
|
|
(gpu_id >> 16) & 0xffffu, (gpu_id >> 8) & 0xffu, gpu_id & 0xffu);
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
notified = nv_rs_rmapi_rc_recover_fault(gpu_id, inst_ptr, xid_code, &chid, &tsg_id, diag);
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
pr_err("NVRM: Xid (PCI:%s): %u, pid=%d, name=%s, Channel %08x, TSG %u, FaultVA 0x%llx (inst=0x%llx notified=%u gr_st=0x%x ctx=0x%x eng=0x%x)\n",
|
|
bdf, xid_code, task_pid_nr(current), current->comm,
|
|
chid, tsg_id, (unsigned long long)fault_va,
|
|
(unsigned long long)inst_ptr, notified, diag[0], diag[1], diag[2]);
|
|
return notified;
|
|
}
|
|
|
|
int nv_rs_rmapi_node_init(void)
|
|
{
|
|
void *state;
|
|
int result;
|
|
if (nv_rs_rmapi_state_align() > ARCH_KMALLOC_MINALIGN ||
|
|
nv_rs_rmapi_state_size() > 8 * 1024 * 1024)
|
|
return -EINVAL;
|
|
state = kvzalloc(nv_rs_rmapi_state_size(), GFP_KERNEL);
|
|
if (!state)
|
|
return -ENOMEM;
|
|
result = nv_rs_rmapi_init(state);
|
|
if (result) {
|
|
kvfree(state);
|
|
return result;
|
|
}
|
|
{
|
|
/* The chipset input of the host capabilities: the first host bridge the PCI core
|
|
* lists (configuration ids only; nothing of a GPU is touched). */
|
|
struct pci_dev *bridge = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL);
|
|
if (bridge) {
|
|
nv_rs_rmapi_set_host_bridge(state, bridge->vendor, bridge->device);
|
|
pci_dev_put(bridge);
|
|
}
|
|
nv_rs_rmapi_set_force_p2p(state, force_p2p);
|
|
if (!nv_rs_rmapi_set_gsp_abi(gsp_abi))
|
|
pr_warn("nvidia_rs: gsp_abi=%u is not 580 or 620, keeping the default GSP-RM ABI\n",
|
|
gsp_abi);
|
|
if (driver_version && *driver_version)
|
|
nv_rs_rmapi_set_driver_version(driver_version, strlen(driver_version));
|
|
}
|
|
nv_rs_rmapi_state = state;
|
|
result = misc_register(&nv_rs_rmapi_device);
|
|
if (result) {
|
|
nv_rs_rmapi_state = NULL;
|
|
kvfree(state);
|
|
} else {
|
|
nv_rs_proc_init();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void nv_rs_rmapi_node_exit(void)
|
|
{
|
|
if (!nv_rs_rmapi_state)
|
|
return;
|
|
#ifndef NV_RS_PCI_TEST_EDU
|
|
/* the GPUs (and their interrupts) are gone: no IRQ thread schedules the RM-event retry any more */
|
|
cancel_delayed_work_sync(&nv_rs_rmapi_events_retry);
|
|
#endif
|
|
/* Deregistration waits for no one: open files pin the module, so none exist here. The
|
|
* PCI driver removes its GPUs' device nodes before this runs; a node that is still
|
|
* live is removed here. */
|
|
{
|
|
int gpu;
|
|
for (gpu = 0; gpu < NV_RS_RMAPI_MAX_GPUS; gpu++) {
|
|
if (nv_rs_rmapi_gpu_nodes[gpu].live) {
|
|
nv_rs_rmapi_gpu_nodes[gpu].live = false;
|
|
nv_rs_rmapi_gpu_nodes[gpu].generation++;
|
|
misc_deregister(&nv_rs_rmapi_gpu_nodes[gpu].misc);
|
|
if (nv_rs_rmapi_gpu_nodes[gpu].release)
|
|
nv_rs_rmapi_gpu_nodes[gpu].release(nv_rs_rmapi_gpu_nodes[gpu].cookie);
|
|
nv_rs_rmapi_gpu_nodes[gpu].read32 = NULL;
|
|
nv_rs_rmapi_gpu_nodes[gpu].release = NULL;
|
|
}
|
|
}
|
|
}
|
|
nv_rs_proc_exit();
|
|
misc_deregister(&nv_rs_rmapi_device);
|
|
/* Every file is released by now, and with it every client and its memory objects: the
|
|
* Rust registry is empty and no record exists. Anything else is a leak: report and drain. */
|
|
{
|
|
u64 leaked = nv_rs_alloc_reg_total_pages();
|
|
void *rem;
|
|
if (leaked)
|
|
pr_err("nvidia_rs: rmapi memory: %llu pages still allocated at node exit\n",
|
|
leaked);
|
|
while ((rem = nv_rs_alloc_reg_pop_any()) != NULL)
|
|
nv_rs_rmapi_alloc_destroy(rem);
|
|
nv_rs_alloc_reg_release_storage();
|
|
}
|
|
nv_rs_rmapi_close(nv_rs_rmapi_state, U64_MAX);
|
|
nv_rs_rmapi_trim_page_pools(0);
|
|
kvfree(nv_rs_rmapi_state);
|
|
nv_rs_rmapi_state = NULL;
|
|
if (atomic64_read(&nv_rs_rmapi_kvalloc_live) != 0)
|
|
pr_err("nvidia_rs: rmapi slab: %lld dynamic allocations still live at node exit\n",
|
|
(long long)atomic64_read(&nv_rs_rmapi_kvalloc_live));
|
|
}
|