mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
Proactive 100% parity Wave 1 (REA-grounded vs stock 580.178.04): - Kernel/build compat: Kbuild header probes for timer_delete_sync, vm_flags_set, vma_flags_set_word; remove broken UTS_UBUNTU_RELEASE_ABI guard on pci_resize_resource; guard x86 set_memory_uc/wb under CONFIG_X86; remove illegal ARM64 dma_sync_single_* on coherent DMA; gate nvidia_rs_drm on CONFIG_DRM; default LD=ld.lld/OBJCOPY=llvm-objcopy under LLVM=1; fix dkms.conf (KDIR support, all 4 modules, 0.2.0) and install.sh (all 4 modules, non-0000 PCI domains, KREL override). - IOMMU & PCI lifecycle: switch gsp_route error notifier to per-device DMA (nv_rs_rmapi_os_page_dma); add PCI .shutdown (pci_clear_master + udelay(1)) and AER .err_handler; keep freed rmapi_allocs on list until users==0 for hot-unplug DMA teardown; drain nv_rs_uvm_cpu_dma_ht on exit; lock BAR0 fill under p->access; raise MAX_BRIDGE_GPUS to 32. - Security & isolation (REA _nv000655rm / _nv000663rm): enforce cross-process share policy on NV_ESC_RM_DUP_OBJECT & NV_ESC_RM_SHARE; refcount shared memory backings on release (eliminating dup UAF); scope UVM ManagedFaultMapper by faulting GPU & SysmemPageTree.owner; pass real task_tgid_vnr into Caller.pid.
2028 lines
78 KiB
C
2028 lines
78 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/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: the UVM lock, then this
|
|
* one, never the other way round (nothing under this lock takes the UVM lock). */
|
|
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_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
|
|
/* 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. */
|
|
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;
|
|
/* The mapping context NV_ESC_RM_MAP_MEMORY attached to this (control) file. Written
|
|
* once under nv_rs_rmapi_lock and published through `valid`; it then stays for the
|
|
* life of the file, as the original's nv_alloc_mapping_context_t does, and holds one
|
|
* use of `alloc`. */
|
|
struct nv_rs_rmapi_mapping {
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
u64 page_index;
|
|
u64 io_phys, io_bytes;
|
|
u32 protection, caching;
|
|
bool is_io;
|
|
int valid;
|
|
} mapping;
|
|
};
|
|
struct nv_rs_rmapi_gpu_node {
|
|
struct miscdevice misc;
|
|
char name[32];
|
|
char bdf[32];
|
|
bool live;
|
|
unsigned int generation;
|
|
u64 bar0, bar0_size, bar1, bar1_size;
|
|
/* 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;
|
|
s32 numa_status;
|
|
int minor_index;
|
|
#ifdef CONFIG_PROC_FS
|
|
struct proc_dir_entry *proc_dir;
|
|
#endif
|
|
};
|
|
#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
|
|
struct nv_rs_rmapi_dma_map {
|
|
struct list_head link;
|
|
struct device *dev;
|
|
u32 gpu_id;
|
|
u64 count;
|
|
u64 encoded[]; /* 0 = unmapped; otherwise (u64)dma_addr + 1 */
|
|
};
|
|
struct nv_rs_rmapi_alloc {
|
|
struct list_head link; /* on nv_rs_rmapi_allocs while the RM object holds it */
|
|
struct list_head dma_maps;
|
|
u64 token, count;
|
|
u32 cache;
|
|
unsigned int users;
|
|
bool user_pinned;
|
|
bool writable;
|
|
bool freed;
|
|
void *kernel;
|
|
struct page *pages[];
|
|
};
|
|
static LIST_HEAD(nv_rs_rmapi_allocs);
|
|
static u64 nv_rs_rmapi_alloc_token;
|
|
/* Pages of all records that still exist, including those only a file context holds. */
|
|
static u64 nv_rs_rmapi_alloc_total;
|
|
/* Pages of nv_rs_rmapi_alloc_total that are pinned user memory (class 0x71). */
|
|
static u64 nv_rs_rmapi_user_pinned_total;
|
|
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)
|
|
{
|
|
if (!nv_rs_runtime_engines())
|
|
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_alloc_unmap_dma(struct nv_rs_rmapi_alloc *alloc)
|
|
{
|
|
struct nv_rs_rmapi_dma_map *dm, *tmp;
|
|
u64 i;
|
|
|
|
list_for_each_entry_safe(dm, tmp, &alloc->dma_maps, link) {
|
|
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);
|
|
}
|
|
}
|
|
list_del(&dm->link);
|
|
put_device(dm->dev);
|
|
kvfree(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_alloc *alloc;
|
|
struct nv_rs_rmapi_dma_map *dm, *tmp;
|
|
u64 i;
|
|
|
|
if (!dev)
|
|
return;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
list_for_each_entry(alloc, &nv_rs_rmapi_allocs, link) {
|
|
list_for_each_entry_safe(dm, tmp, &alloc->dma_maps, link) {
|
|
if (dm->dev != dev)
|
|
continue;
|
|
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);
|
|
}
|
|
}
|
|
list_del(&dm->link);
|
|
put_device(dm->dev);
|
|
kvfree(dm);
|
|
}
|
|
}
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
}
|
|
|
|
static struct nv_rs_rmapi_alloc *nv_rs_rmapi_alloc_find(u64 token)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
list_for_each_entry(alloc, &nv_rs_rmapi_allocs, link)
|
|
if (!alloc->freed && alloc->token == token)
|
|
return alloc;
|
|
return NULL;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/* Drops one use; the last one unlinks, frees the record, and releases page references. */
|
|
static void nv_rs_rmapi_alloc_put(struct nv_rs_rmapi_alloc *alloc)
|
|
{
|
|
u64 index;
|
|
if (--alloc->users)
|
|
return;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
if (!list_empty(&alloc->link))
|
|
list_del_init(&alloc->link);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
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]);
|
|
}
|
|
}
|
|
nv_rs_rmapi_alloc_total -= alloc->count;
|
|
if (alloc->user_pinned)
|
|
nv_rs_rmapi_user_pinned_total -= alloc->count;
|
|
kvfree(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;
|
|
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;
|
|
/* The pages are the caller's own memory, not allocations of this driver, so they do
|
|
* not count against the RAM/2 budget of nv_rs_rmapi_memory_max_pages(). That budget
|
|
* made cudaHostRegister of a 72 GiB host arena on a 125 GiB host fail with NO_MEMORY,
|
|
* where the stock driver registers it. They stay under the combined ceiling. */
|
|
/* Pages retained in the sysmem pool (system_memory_pool_retain_mb=-1) are still held by
|
|
* the driver and a pin cannot reuse them, so they count in the held total. */
|
|
pool = READ_ONCE(nv_rs_sysmem_pool_pages);
|
|
if (count > nv_rs_rmapi_total_max_pages() ||
|
|
nv_rs_rmapi_alloc_total + pool > nv_rs_rmapi_total_max_pages() - count)
|
|
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;
|
|
}
|
|
|
|
INIT_LIST_HEAD(&alloc->dma_maps);
|
|
alloc->token = ++nv_rs_rmapi_alloc_token;
|
|
alloc->count = count;
|
|
alloc->cache = 0;
|
|
alloc->users = 1;
|
|
alloc->user_pinned = true;
|
|
alloc->writable = writable;
|
|
nv_rs_rmapi_alloc_total += count;
|
|
nv_rs_rmapi_user_pinned_total += count;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
list_add_tail(&alloc->link, &nv_rs_rmapi_allocs);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
*token = alloc->token;
|
|
return 0;
|
|
}
|
|
|
|
void nv_rs_rmapi_os_unlock_user_pages(u64 token)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (alloc)
|
|
list_del(&alloc->link);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!alloc)
|
|
return;
|
|
nv_rs_rmapi_alloc_put(alloc);
|
|
}
|
|
|
|
/* 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;
|
|
if (!count || cache > 2)
|
|
return NV_RS_STATUS_INVALID_ARGUMENT;
|
|
if (page != PAGE_SIZE || (contiguous && count != 1))
|
|
return NV_RS_STATUS_NOT_SUPPORTED;
|
|
if (count > nv_rs_rmapi_memory_max_pages() -
|
|
(nv_rs_rmapi_alloc_total - nv_rs_rmapi_user_pinned_total))
|
|
return NV_RS_STATUS_NO_MEMORY;
|
|
/* Driver pages held after this call: the pool is reused first, so it counts once, as
|
|
* max(pool, count) beside the live allocations. */
|
|
pool = READ_ONCE(nv_rs_sysmem_pool_pages);
|
|
if (count > nv_rs_rmapi_total_max_pages() ||
|
|
nv_rs_rmapi_alloc_total + (pool > count ? pool : count) > nv_rs_rmapi_total_max_pages())
|
|
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;
|
|
}
|
|
}
|
|
INIT_LIST_HEAD(&alloc->dma_maps);
|
|
alloc->token = ++nv_rs_rmapi_alloc_token;
|
|
alloc->count = count;
|
|
alloc->cache = cache;
|
|
alloc->users = 1;
|
|
nv_rs_rmapi_alloc_total += count;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
list_add_tail(&alloc->link, &nv_rs_rmapi_allocs);
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
*token = alloc->token;
|
|
return 0;
|
|
}
|
|
|
|
/* nv_free_pages: the RM object is gone; the token names nothing from here on,
|
|
* while remaining on nv_rs_rmapi_allocs 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)
|
|
{
|
|
struct nv_rs_rmapi_alloc *alloc;
|
|
mutex_lock(&nv_rs_rmapi_alloc_lock);
|
|
alloc = nv_rs_rmapi_alloc_find(token);
|
|
if (alloc)
|
|
alloc->freed = true;
|
|
mutex_unlock(&nv_rs_rmapi_alloc_lock);
|
|
if (!alloc)
|
|
return;
|
|
nv_rs_rmapi_alloc_unmap(alloc);
|
|
nv_rs_rmapi_alloc_put(alloc);
|
|
}
|
|
|
|
/* 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, *cur;
|
|
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;
|
|
}
|
|
list_for_each_entry(cur, &alloc->dma_maps, link) {
|
|
if (cur->gpu_id == gpu_id && cur->dev == dev) {
|
|
dm = cur;
|
|
break;
|
|
}
|
|
}
|
|
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;
|
|
list_add_tail(&dm->link, &alloc->dma_maps);
|
|
}
|
|
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 if (context->mapping.valid) {
|
|
status = NV_RS_STATUS_STATE_IN_USE;
|
|
} else {
|
|
alloc->users++;
|
|
context->mapping.alloc = alloc;
|
|
context->mapping.page_index = page_index;
|
|
context->mapping.protection = protection;
|
|
context->mapping.caching = caching;
|
|
smp_store_release(&context->mapping.valid, 1);
|
|
}
|
|
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;
|
|
struct nv_rs_rmapi_gpu_node *node;
|
|
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 (context->mapping.valid) {
|
|
status = NV_RS_STATUS_STATE_IN_USE;
|
|
} else {
|
|
node = &nv_rs_rmapi_gpu_nodes[context->gpu];
|
|
if (!node->live || node->generation != context->generation || !node->bar0 ||
|
|
offset > node->bar0_size || length > node->bar0_size - offset) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else {
|
|
context->mapping.alloc = NULL;
|
|
context->mapping.is_io = true;
|
|
context->mapping.io_phys = node->bar0 + offset;
|
|
context->mapping.io_bytes = length;
|
|
context->mapping.protection = write_only ? 2 : 1;
|
|
context->mapping.caching = 6;
|
|
*address = node->bar0 + offset;
|
|
smp_store_release(&context->mapping.valid, 1);
|
|
}
|
|
}
|
|
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;
|
|
struct nv_rs_rmapi_gpu_node *node;
|
|
u64 aligned_off, aligned_len;
|
|
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 (context->mapping.valid) {
|
|
status = NV_RS_STATUS_STATE_IN_USE;
|
|
} else {
|
|
node = &nv_rs_rmapi_gpu_nodes[context->gpu];
|
|
if (!node->live || node->generation != context->generation || !node->bar1 ||
|
|
aligned_off > node->bar1_size || aligned_len > node->bar1_size - aligned_off) {
|
|
status = NV_RS_STATUS_INVALID_ARGUMENT;
|
|
} else {
|
|
context->mapping.alloc = NULL;
|
|
context->mapping.is_io = true;
|
|
context->mapping.io_phys = node->bar1 + aligned_off;
|
|
context->mapping.io_bytes = aligned_len;
|
|
context->mapping.protection = 3;
|
|
context->mapping.caching = 2;
|
|
*address = node->bar1 + offset;
|
|
smp_store_release(&context->mapping.valid, 1);
|
|
}
|
|
}
|
|
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)
|
|
owner = node->owner;
|
|
mutex_unlock(&nv_rs_rmapi_lock);
|
|
if (owner && nv_rs_runtime_engines()) {
|
|
result = nv_rs_gpu_owner_runtime_start(owner);
|
|
if (result) {
|
|
kfree(context);
|
|
return result;
|
|
}
|
|
}
|
|
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;
|
|
(void)inode;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
nv_rs_rmapi_close(nv_rs_rmapi_state, context->owner);
|
|
if (context->gpu >= 0 && nv_rs_rmapi_gpu_nodes[context->gpu].live &&
|
|
nv_rs_rmapi_gpu_nodes[context->gpu].generation == 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. */
|
|
if (context->mapping.valid && !context->mapping.is_io && context->mapping.alloc)
|
|
nv_rs_rmapi_alloc_put(context->mapping.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_rmapi_gpu_nodes[gpu].live &&
|
|
nv_rs_rmapi_gpu_nodes[gpu].generation == 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; } */
|
|
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 (size != 8)
|
|
return -EINVAL;
|
|
mutex_lock(&nv_rs_rmapi_lock);
|
|
if (context->gpu >= 0 && 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; } */
|
|
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 (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 &&
|
|
nv_rs_rmapi_gpu_nodes[context->gpu].live)
|
|
nv_rs_rmapi_gpu_nodes[context->gpu].numa_status = 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;
|
|
}
|
|
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;
|
|
const struct nv_rs_rmapi_mapping *mapping = &context->mapping;
|
|
unsigned long address = vma->vm_start;
|
|
u64 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT, index;
|
|
if (!smp_load_acquire(&mapping->valid))
|
|
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;
|
|
if (!size || size > mapping->io_bytes)
|
|
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);
|
|
}
|
|
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);
|
|
return remap_pfn_range(vma, vma->vm_start, mapping->io_phys >> PAGE_SHIFT,
|
|
size, vma->vm_page_prot);
|
|
}
|
|
if (mapping->page_index + pages > mapping->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, mapping->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->bar0 = gpu.bar0;
|
|
node->bar0_size = gpu.bar0_size;
|
|
node->bar1 = gpu.bar1;
|
|
node->bar1_size = gpu.bar1_size;
|
|
node->read32 = NULL;
|
|
node->release = NULL;
|
|
node->cookie = NULL;
|
|
node->numa_status = 0;
|
|
node->minor_index = minor;
|
|
{
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&nv_rs_rmapi_gpu_dev_lock, flags);
|
|
node->pdev = pdev;
|
|
node->gpu_id = (((u32)gpu.domain & 0xffffU) << 16) | ((u32)gpu.bus << 8) | (u32)gpu.slot;
|
|
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->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;
|
|
}
|
|
|
|
/* 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++;
|
|
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 (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;
|
|
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);
|
|
if (result)
|
|
return result;
|
|
}
|
|
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
|
|
* list is empty and no record exists. Anything else is a leak: it is reported, and
|
|
* what is still on the list is freed. */
|
|
if (nv_rs_rmapi_alloc_total)
|
|
pr_err("nvidia_rs: rmapi memory: %llu pages still allocated at node exit\n",
|
|
nv_rs_rmapi_alloc_total);
|
|
while (!list_empty(&nv_rs_rmapi_allocs)) {
|
|
struct nv_rs_rmapi_alloc *alloc =
|
|
list_first_entry(&nv_rs_rmapi_allocs, struct nv_rs_rmapi_alloc, link);
|
|
list_del(&alloc->link);
|
|
alloc->users = 1;
|
|
nv_rs_rmapi_alloc_put(alloc);
|
|
}
|
|
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));
|
|
}
|