]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
drm/amd/amdgpu: Release xcp drm memory after unplug
authorMeng Li <li.meng@amd.com>
Fri, 9 May 2025 05:44:24 +0000 (13:44 +0800)
committerAlex Deucher <alexander.deucher@amd.com>
Mon, 4 Aug 2025 18:35:48 +0000 (14:35 -0400)
Add a new API amdgpu_xcp_drm_dev_free().
After unplug xcp device, need to release xcp drm memory etc.

Co-developed-by: Jiang Liu <gerry@linux.alibaba.com>
Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
Signed-off-by: Meng Li <li.meng@amd.com>
Acked-by: Alex Deucher <alexander.deucher@amd.com>
Reviewed-by: Lijo Lazar <lijo.lazar@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h

index c417f868922077bcbe4fcff12fab1e3a15ac3289..699acc1b46b59a7bbc0581d2da885b4d4309545f 100644 (file)
@@ -406,6 +406,7 @@ void amdgpu_xcp_dev_unplug(struct amdgpu_device *adev)
                p_ddev->primary->dev = adev->xcp_mgr->xcp[i].pdev;
                p_ddev->driver =  adev->xcp_mgr->xcp[i].driver;
                p_ddev->vma_offset_manager = adev->xcp_mgr->xcp[i].vma_offset_manager;
+               amdgpu_xcp_drm_dev_free(p_ddev);
        }
 }
 
index 8bc36f04b1b712cb7bfc5d0fe6315e4b915b1711..44009aa8216ed005219b61dac399f40e691b80d2 100644 (file)
@@ -46,18 +46,29 @@ static const struct drm_driver amdgpu_xcp_driver = {
 
 static int8_t pdev_num;
 static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
+static DEFINE_MUTEX(xcp_mutex);
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 {
        struct platform_device *pdev;
        struct xcp_device *pxcp_dev;
        char dev_name[20];
-       int ret;
+       int ret, i;
+
+       guard(mutex)(&xcp_mutex);
 
        if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
                return -ENODEV;
 
-       snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", pdev_num);
+       for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+               if (!xcp_dev[i])
+                       break;
+       }
+
+       if (i >= MAX_XCP_PLATFORM_DEVICE)
+               return -ENODEV;
+
+       snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);
        pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
        if (IS_ERR(pdev))
                return PTR_ERR(pdev);
@@ -73,8 +84,8 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
                goto out_devres;
        }
 
-       xcp_dev[pdev_num] = pxcp_dev;
-       xcp_dev[pdev_num]->pdev = pdev;
+       xcp_dev[i] = pxcp_dev;
+       xcp_dev[i]->pdev = pdev;
        *ddev = &pxcp_dev->drm;
        pdev_num++;
 
@@ -89,16 +100,43 @@ out_unregister:
 }
 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
 
-void amdgpu_xcp_drv_release(void)
+static void free_xcp_dev(int8_t index)
 {
-       for (--pdev_num; pdev_num >= 0; --pdev_num) {
-               struct platform_device *pdev = xcp_dev[pdev_num]->pdev;
+       if ((index < MAX_XCP_PLATFORM_DEVICE) && (xcp_dev[index])) {
+               struct platform_device *pdev = xcp_dev[index]->pdev;
 
                devres_release_group(&pdev->dev, NULL);
                platform_device_unregister(pdev);
-               xcp_dev[pdev_num] = NULL;
+
+               xcp_dev[index] = NULL;
+               pdev_num--;
+       }
+}
+
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev)
+{
+       int8_t i;
+
+       guard(mutex)(&xcp_mutex);
+
+       for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+               if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
+                       free_xcp_dev(i);
+                       break;
+               }
+       }
+}
+EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
+
+void amdgpu_xcp_drv_release(void)
+{
+       int8_t i;
+
+       guard(mutex)(&xcp_mutex);
+
+       for (i = 0; pdev_num && i < MAX_XCP_PLATFORM_DEVICE; i++) {
+               free_xcp_dev(i);
        }
-       pdev_num = 0;
 }
 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
 
index c1c4b679bf95c875478a1204a32bba17d326af02..580a1602c8e366c545f41f3cdfe7665aafa87bbb 100644 (file)
@@ -25,5 +25,6 @@
 #define _AMDGPU_XCP_DRV_H_
 
 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev);
+void amdgpu_xcp_drm_dev_free(struct drm_device *ddev);
 void amdgpu_xcp_drv_release(void);
 #endif /* _AMDGPU_XCP_DRV_H_ */