[RFC 24/29] vfio/vgpu_mgr: allocate mgmt heap when creating vGPUs

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

The mgmt heap is a block of shared FBMEM between the GSP firmware and
the vGPU host. It is used for supporting vGPU RPCs, vGPU logging.

Creating a vGPU requires allocating a mgmt heap from the FBMEM. The size
of the mgmt heap that a vGPU requires is from the vGPU type.

Acquire the size of mgmt heap from the vGPU type. Allocate the mgmt
heap 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 | 32 +++++++++++++++++++++++++
drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h | 6 +++++
3 files changed, 44 insertions(+)

diff --git a/drivers/vfio/pci/nvidia-vgpu/nvkm.h b/drivers/vfio/pci/nvidia-vgpu/nvkm.h
index b95b48edeb03..50b860e7967d 100644
--- a/drivers/vfio/pci/nvidia-vgpu/nvkm.h
+++ b/drivers/vfio/pci/nvidia-vgpu/nvkm.h
@@ -76,4 +76,10 @@ static inline int nvidia_vgpu_mgr_get_handle(struct pci_dev *pdev,
#define nvidia_vgpu_mgr_free_chids(m, o, s) \
m->handle.ops->free_chids(m->handle.pf_drvdata, o, s)

+#define nvidia_vgpu_mgr_alloc_fbmem(m, s) \
+ m->handle.ops->alloc_fbmem(m->handle.pf_drvdata, s, false)
+
+#define nvidia_vgpu_mgr_free_fbmem(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 c48c22d8fbb4..4b04b13944d5 100644
--- a/drivers/vfio/pci/nvidia-vgpu/vgpu.c
+++ b/drivers/vfio/pci/nvidia-vgpu/vgpu.c
@@ -87,6 +87,31 @@ static int setup_chids(struct nvidia_vgpu *vgpu)
return 0;
}

+static void clean_mgmt_heap(struct nvidia_vgpu *vgpu)
+{
+ struct nvidia_vgpu_mgr *vgpu_mgr = vgpu->vgpu_mgr;
+ struct nvidia_vgpu_mgmt *mgmt = &vgpu->mgmt;
+
+ nvidia_vgpu_mgr_free_fbmem(vgpu_mgr, mgmt->heap_mem);
+ mgmt->heap_mem = NULL;
+}
+
+static int setup_mgmt_heap(struct nvidia_vgpu *vgpu)
+{
+ struct nvidia_vgpu_mgr *vgpu_mgr = vgpu->vgpu_mgr;
+ struct nvidia_vgpu_mgmt *mgmt = &vgpu->mgmt;
+ NVA081_CTRL_VGPU_INFO *info =
+ (NVA081_CTRL_VGPU_INFO *)vgpu->vgpu_type;
+ struct nvidia_vgpu_mem *mem;
+
+ mem = nvidia_vgpu_mgr_alloc_fbmem(vgpu_mgr, info->gspHeapSize);
+ if (IS_ERR(mem))
+ return PTR_ERR(mem);
+
+ mgmt->heap_mem = mem;
+ return 0;
+}
+
/**
* nvidia_vgpu_mgr_destroy_vgpu - destroy a vGPU instance
* @vgpu: the vGPU instance going to be destroyed.
@@ -98,6 +123,7 @@ int nvidia_vgpu_mgr_destroy_vgpu(struct nvidia_vgpu *vgpu)
if (!atomic_cmpxchg(&vgpu->status, 1, 0))
return -ENODEV;

+ clean_mgmt_heap(vgpu);
clean_chids(vgpu);
clean_fbmem_heap(vgpu);
unregister_vgpu(vgpu);
@@ -139,10 +165,16 @@ int nvidia_vgpu_mgr_create_vgpu(struct nvidia_vgpu *vgpu, u8 *vgpu_type)
if (ret)
goto err_setup_chids;

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

return 0;

+err_setup_mgmt_heap:
+ clean_chids(vgpu);
err_setup_chids:
clean_fbmem_heap(vgpu);
err_setup_fbmem_heap:
diff --git a/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h b/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
index 0918823fdde7..f4ebeadb2b86 100644
--- a/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
+++ b/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
@@ -21,6 +21,11 @@ struct nvidia_vgpu_chid {
int num_plugin_channels;
};

+struct nvidia_vgpu_mgmt {
+ struct nvidia_vgpu_mem *heap_mem;
+ /* more to come */
+};
+
struct nvidia_vgpu {
struct mutex lock;
atomic_t status;
@@ -32,6 +37,7 @@ struct nvidia_vgpu {

struct nvidia_vgpu_mem *fbmem_heap;
struct nvidia_vgpu_chid chid;
+ struct nvidia_vgpu_mgmt mgmt;
};

struct nvidia_vgpu_mgr {
--
2.34.1