amd-gfx.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2 0/1] Release xcp drm memory after unplug xcp device
@ 2025-06-18  5:52 Meng Li
  2025-06-18  5:52 ` [PATCH V2 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug Meng Li
  0 siblings, 1 reply; 4+ messages in thread
From: Meng Li @ 2025-06-18  5:52 UTC (permalink / raw)
  To: amd-gfx
  Cc: Perry Yuan, Shimmer Huang, Koenig Christian, Lazar Lijo,
	Alexander Deucher, limeng12

From: limeng12 <li.meng@amd.com>

Fix xcp drm memory release after executing PCIe remove.
Fix xcp drm memory alloc after executing PCIe insert.

limeng12 (1):
  drm/amd/amdgpu: Release xcp drm memory after unplug

 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 63 ++++++++++++++++++---
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 56 insertions(+), 9 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH V2 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug
  2025-06-18  5:52 [PATCH V2 0/1] Release xcp drm memory after unplug xcp device Meng Li
@ 2025-06-18  5:52 ` Meng Li
  2025-06-18  6:08   ` Lazar, Lijo
  0 siblings, 1 reply; 4+ messages in thread
From: Meng Li @ 2025-06-18  5:52 UTC (permalink / raw)
  To: amd-gfx
  Cc: Perry Yuan, Shimmer Huang, Koenig Christian, Lazar Lijo,
	Alexander Deucher, limeng12

From: limeng12 <li.meng@amd.com>

Add a new API amdgpu_xcp_drm_dev_free().
After unplug xcp device, need to release xcp drm memory etc.

Signed-off-by: Meng Li <li.meng@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 63 ++++++++++++++++++---
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
index 322816805bfb..70c44961af95 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
@@ -394,6 +394,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);
 	}
 }
 
diff --git a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
index faed84172dd4..8714458878cf 100644
--- a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
+++ b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
@@ -45,18 +45,30 @@ 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);
+	//find a unused xcp_dev[]
+	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);
@@ -72,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++;
 
@@ -88,16 +100,49 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
 }
 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 = MAX_XCP_PLATFORM_DEVICE;
+
+	guard(mutex)(&xcp_mutex);
+
+	//find the drm device
+	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+		if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
+			//free
+			free_xcp_dev(i);
+			break;
+		}
+	}
+}
+EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
+
+void amdgpu_xcp_drv_release(void)
+{
+	int8_t i = 0;
+
+	guard(mutex)(&xcp_mutex);
+
+	if (pdev_num > 0) {
+		for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
+			free_xcp_dev(i);
+			if (pdev_num == 0)
+				break;
+		}
 	}
-	pdev_num = 0;
 }
 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
 
diff --git a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h
index c1c4b679bf95..580a1602c8e3 100644
--- a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h
+++ b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h
@@ -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_ */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH V2 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug
  2025-06-18  5:52 ` [PATCH V2 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug Meng Li
@ 2025-06-18  6:08   ` Lazar, Lijo
  0 siblings, 0 replies; 4+ messages in thread
From: Lazar, Lijo @ 2025-06-18  6:08 UTC (permalink / raw)
  To: Meng Li, amd-gfx
  Cc: Perry Yuan, Shimmer Huang, Koenig Christian, Alexander Deucher



On 6/18/2025 11:22 AM, Meng Li wrote:
> From: limeng12 <li.meng@amd.com>
> 
> Add a new API amdgpu_xcp_drm_dev_free().
> After unplug xcp device, need to release xcp drm memory etc.
> 
> Signed-off-by: Meng Li <li.meng@amd.com>

Suggest to keep Jiang Liu <gerry@linux.alibaba.com> as co-developer -
https://patchwork.freedesktop.org/patch/633450/

Also, please remove // comments.

Thanks,
Lijo

> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
>  drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 63 ++++++++++++++++++---
>  drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
>  3 files changed, 56 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
> index 322816805bfb..70c44961af95 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
> @@ -394,6 +394,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);
>  	}
>  }
>  
> diff --git a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
> index faed84172dd4..8714458878cf 100644
> --- a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
> +++ b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
> @@ -45,18 +45,30 @@ 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);
> +	//find a unused xcp_dev[]
> +	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);
> @@ -72,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++;
>  
> @@ -88,16 +100,49 @@ int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
>  }
>  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 = MAX_XCP_PLATFORM_DEVICE;
> +
> +	guard(mutex)(&xcp_mutex);
> +
> +	//find the drm device
> +	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
> +		if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
> +			//free
> +			free_xcp_dev(i);
> +			break;
> +		}
> +	}
> +}
> +EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
> +
> +void amdgpu_xcp_drv_release(void)
> +{
> +	int8_t i = 0;
> +
> +	guard(mutex)(&xcp_mutex);
> +
> +	if (pdev_num > 0) {
> +		for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
> +			free_xcp_dev(i);
> +			if (pdev_num == 0)
> +				break;
> +		}
>  	}
> -	pdev_num = 0;
>  }
>  EXPORT_SYMBOL(amdgpu_xcp_drv_release);
>  
> diff --git a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h
> index c1c4b679bf95..580a1602c8e3 100644
> --- a/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h
> +++ b/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h
> @@ -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_ */


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH V2 0/1] Release xcp drm memory after unplug xcp device
@ 2025-06-19  7:29 Meng Li
  0 siblings, 0 replies; 4+ messages in thread
From: Meng Li @ 2025-06-19  7:29 UTC (permalink / raw)
  To: amd-gfx
  Cc: Perry Yuan, Shimmer Huang, Koenig Christian, Lazar Lijo,
	Alexander Deucher, limeng12

From: limeng12 <li.meng@amd.com>

Fix xcp drm memory release after executing PCIe remove.
Fix xcp drm memory alloc after executing PCIe insert.

Changes from V1->V2:
- cover-letter:
- - Revise title version number.
- amdgpu:
- - Remove // comments.
- - Add Gerry as a co-developr.

limeng12 (1):
  drm/amd/amdgpu: Release xcp drm memory after unplug

 drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c     |  1 +
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c | 60 +++++++++++++++++----
 drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.h |  1 +
 3 files changed, 53 insertions(+), 9 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-19  7:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-18  5:52 [PATCH V2 0/1] Release xcp drm memory after unplug xcp device Meng Li
2025-06-18  5:52 ` [PATCH V2 1/1] drm/amd/amdgpu: Release xcp drm memory after unplug Meng Li
2025-06-18  6:08   ` Lazar, Lijo
  -- strict thread matches above, loose matches on Subject: below --
2025-06-19  7:29 [PATCH V2 0/1] Release xcp drm memory after unplug xcp device Meng Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).