[RFC 22/29] vfio/vgpu_mgr: allocate vGPU FB memory when creating vGPUs

Zhi Wang
2024-09-22T12:50:51Z

Creating a vGPU requires allocating a portion of the FB memory from the
NVKM. The size of the FB memory that a vGPU requires is from the vGPU
type.

Acquire the size of the required FB memory from the vGPU type. Allocate
the FB memory from NVKM when creating a vGPU.

Signed-off-by: Zhi Wang <zhiw@nvidia.com>
---
drivers/vfio/pci/nvidia-vgpu/nvkm.h | 6 ++++
drivers/vfio/pci/nvidia-vgpu/vgpu.c | 38 +++++++++++++++++++++++++
drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h | 2 ++
3 files changed, 46 insertions(+)

diff --git a/drivers/vfio/pci/nvidia-vgpu/nvkm.h b/drivers/vfio/pci/nvidia-vgpu/nvkm.h
index 065bb7aa55f8..d3c77d26c734 100644
--- a/drivers/vfio/pci/nvidia-vgpu/nvkm.h
+++ b/drivers/vfio/pci/nvidia-vgpu/nvkm.h
@@ -64,4 +64,10 @@ static inline int nvidia_vgpu_mgr_get_handle(struct pci_dev *pdev,
#define nvidia_vgpu_mgr_rm_ctrl_done(m, g, c) \
m->handle.ops->rm_ctrl_done(, c)

+#define nvidia_vgpu_mgr_alloc_fbmem_heap(m, s) \
+ m->handle.ops->alloc_fbmem(m->handle.pf_drvdata, s, true)
+
+#define nvidia_vgpu_mgr_free_fbmem_heap(m, h) \
+ m->handle.ops->free_fbmem(h)
+
#endif
diff --git a/drivers/vfio/pci/nvidia-vgpu/vgpu.c b/drivers/vfio/pci/nvidia-vgpu/vgpu.c
index 34f6adb9dfe4..54e27823820e 100644
--- a/drivers/vfio/pci/nvidia-vgpu/vgpu.c
+++ b/drivers/vfio/pci/nvidia-vgpu/vgpu.c
@@ -3,6 +3,11 @@
* Copyright © 2024 NVIDIA Corporation
*/

+#include <linux/kernel.h>
+
+#include <nvrm/nvtypes.h>
+#include <nvrm/common/sdk/nvidia/inc/ctrl/ctrla081.h>
+
#include "vgpu_mgr.h"

static void unregister_vgpu(struct nvidia_vgpu *vgpu)
@@ -34,6 +39,29 @@ static int register_vgpu(struct nvidia_vgpu *vgpu)
return 0;
}

+static void clean_fbmem_heap(struct nvidia_vgpu *vgpu)
+{
+ struct nvidia_vgpu_mgr *vgpu_mgr = vgpu->vgpu_mgr;
+
+ nvidia_vgpu_mgr_free_fbmem_heap(vgpu_mgr, vgpu->fbmem_heap);
+ vgpu->fbmem_heap = NULL;
+}
+
+static int setup_fbmem_heap(struct nvidia_vgpu *vgpu)
+{
+ struct nvidia_vgpu_mgr *vgpu_mgr = vgpu->vgpu_mgr;
+ NVA081_CTRL_VGPU_INFO *info =
+ (NVA081_CTRL_VGPU_INFO *)vgpu->vgpu_type;
+ struct nvidia_vgpu_mem *mem;
+
+ mem = nvidia_vgpu_mgr_alloc_fbmem_heap(vgpu_mgr, info->fbLength);
+ if (IS_ERR(mem))
+ return PTR_ERR(mem);
+
+ vgpu->fbmem_heap = mem;
+ return 0;
+}
+
/**
* nvidia_vgpu_mgr_destroy_vgpu - destroy a vGPU instance
* @vgpu: the vGPU instance going to be destroyed.
@@ -45,6 +73,7 @@ int nvidia_vgpu_mgr_destroy_vgpu(struct nvidia_vgpu *vgpu)
if (!atomic_cmpxchg(&vgpu->status, 1, 0))
return -ENODEV;

+ clean_fbmem_heap(vgpu);
unregister_vgpu(vgpu);
return 0;
}
@@ -76,8 +105,17 @@ int nvidia_vgpu_mgr_create_vgpu(struct nvidia_vgpu *vgpu, u8 *vgpu_type)
if (ret)
return ret;

+ ret = setup_fbmem_heap(vgpu);
+ if (ret)
+ goto err_setup_fbmem_heap;
+
atomic_set(&vgpu->status, 1);

return 0;
+
+err_setup_fbmem_heap:
+ unregister_vgpu(vgpu);
+
+ return ret;
}
EXPORT_SYMBOL(nvidia_vgpu_mgr_create_vgpu);
diff --git a/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h b/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
index eb2df9f0fe07..35312d814996 100644
--- a/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
+++ b/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
@@ -23,6 +23,8 @@ struct nvidia_vgpu {
u8 *vgpu_type;
struct nvidia_vgpu_info info;
struct nvidia_vgpu_mgr *vgpu_mgr;
+
+ struct nvidia_vgpu_mem *fbmem_heap;
};

struct nvidia_vgpu_mgr {
--
2.34.1