[RFC 23/29] vfio/vgpu_mgr: allocate vGPU channels when creating vGPUs

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

Creating a vGPU requires allocating a portion of the channels from the
reserved channel pool.

Allocate the channels from the reserved channel pool 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 | 7 ++++++
3 files changed, 45 insertions(+)

diff --git a/drivers/vfio/pci/nvidia-vgpu/nvkm.h b/drivers/vfio/pci/nvidia-vgpu/nvkm.h
index d3c77d26c734..b95b48edeb03 100644
--- a/drivers/vfio/pci/nvidia-vgpu/nvkm.h
+++ b/drivers/vfio/pci/nvidia-vgpu/nvkm.h
@@ -70,4 +70,10 @@ static inline int nvidia_vgpu_mgr_get_handle(struct pci_dev *pdev,
#define nvidia_vgpu_mgr_free_fbmem_heap(m, h) \
m->handle.ops->free_fbmem(h)

+#define nvidia_vgpu_mgr_alloc_chids(m, s) \
+ m->handle.ops->alloc_chids(m->handle.pf_drvdata, s)
+
+#define nvidia_vgpu_mgr_free_chids(m, o, s) \
+ m->handle.ops->free_chids(m->handle.pf_drvdata, o, s)
+
#endif
diff --git a/drivers/vfio/pci/nvidia-vgpu/vgpu.c b/drivers/vfio/pci/nvidia-vgpu/vgpu.c
index 54e27823820e..c48c22d8fbb4 100644
--- a/drivers/vfio/pci/nvidia-vgpu/vgpu.c
+++ b/drivers/vfio/pci/nvidia-vgpu/vgpu.c
@@ -62,6 +62,31 @@ static int setup_fbmem_heap(struct nvidia_vgpu *vgpu)
return 0;
}

+static void clean_chids(struct nvidia_vgpu *vgpu)
+{
+ struct nvidia_vgpu_mgr *vgpu_mgr = vgpu->vgpu_mgr;
+ struct nvidia_vgpu_chid *chid = &vgpu->chid;
+
+ nvidia_vgpu_mgr_free_chids(vgpu_mgr, chid->chid_offset, chid->num_chid);
+}
+
+static int setup_chids(struct nvidia_vgpu *vgpu)
+{
+ struct nvidia_vgpu_mgr *vgpu_mgr = vgpu->vgpu_mgr;
+ struct nvidia_vgpu_chid *chid = &vgpu->chid;
+ int ret;
+
+ ret = nvidia_vgpu_mgr_alloc_chids(vgpu_mgr, 512);
+ if (ret < 0)
+ return ret;
+
+ chid->chid_offset = ret;
+ chid->num_chid = 512;
+ chid->num_plugin_channels = 0;
+
+ return 0;
+}
+
/**
* nvidia_vgpu_mgr_destroy_vgpu - destroy a vGPU instance
* @vgpu: the vGPU instance going to be destroyed.
@@ -73,6 +98,7 @@ int nvidia_vgpu_mgr_destroy_vgpu(struct nvidia_vgpu *vgpu)
if (!atomic_cmpxchg(&vgpu->status, 1, 0))
return -ENODEV;

+ clean_chids(vgpu);
clean_fbmem_heap(vgpu);
unregister_vgpu(vgpu);
return 0;
@@ -109,10 +135,16 @@ int nvidia_vgpu_mgr_create_vgpu(struct nvidia_vgpu *vgpu, u8 *vgpu_type)
if (ret)
goto err_setup_fbmem_heap;

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

return 0;

+err_setup_chids:
+ clean_fbmem_heap(vgpu);
err_setup_fbmem_heap:
unregister_vgpu(vgpu);

diff --git a/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h b/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
index 35312d814996..0918823fdde7 100644
--- a/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
+++ b/drivers/vfio/pci/nvidia-vgpu/vgpu_mgr.h
@@ -15,6 +15,12 @@ struct nvidia_vgpu_info {
u32 dbdf;
};

+struct nvidia_vgpu_chid {
+ int chid_offset;
+ int num_chid;
+ int num_plugin_channels;
+};
+
struct nvidia_vgpu {
struct mutex lock;
atomic_t status;
@@ -25,6 +31,7 @@ struct nvidia_vgpu {
struct nvidia_vgpu_mgr *vgpu_mgr;

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

struct nvidia_vgpu_mgr {
--
2.34.1