Skip to contentSkip to conversation

accel/amdxdna: Add BO import and export

PATCH Superseded Lizhi Hou 2 participants 4 messages Mar 6, 2025 – Mar 25, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

PATCH 1/1 accel/amdxdna: Add BO import and export

Add amdxdna_gem_prime_export() and amdxdna_gem_prime_import() for BO import and export. Register mmu notifier for imported BO as well. When MMU_NOTIFIER_UNMAP event is received, queue work to remove the notifier.

The same BO could be mapped multiple times if it is exported and imported by an application. Use a link list to track VMAs the BO been mapped.

---
 drivers/accel/amdxdna/TODO              |   1 -
 drivers/accel/amdxdna/aie2_ctx.c        |  65 +++--
 drivers/accel/amdxdna/amdxdna_gem.c     | 320 ++++++++++++++++++++----
 drivers/accel/amdxdna/amdxdna_gem.h     |  21 +-
 drivers/accel/amdxdna/amdxdna_pci_drv.c |  11 +-
 drivers/accel/amdxdna/amdxdna_pci_drv.h |   2 +
 6 files changed, 341 insertions(+), 79 deletions(-)
drivers/accel/amdxdna/TODO +0 -1
index 5119bccd1917..ad8ac6e315b6 100644
@@ -1,3 +1,2 @@
-- Add import and export BO support
 - Add debugfs support
 - Add debug BO support
drivers/accel/amdxdna/aie2_ctx.c +41 -24

99 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_gem.c +269 -51

442 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_gem.h +19 -2
index 8ccc0375dd9d..5101d632c800 100644
@@ -6,6 +6,19 @@
 #ifndef _AMDXDNA_GEM_H_
 #define _AMDXDNA_GEM_H_
 
+#include <linux/hmm.h>
+
+struct amdxdna_umap {
+	struct vm_area_struct		*vma;
+	struct mmu_interval_notifier	notifier;
+	struct hmm_range		range;
+	struct work_struct		hmm_unreg_work;
+	struct amdxdna_gem_obj		*abo;
+	struct list_head		node;
+	struct kref			refcnt;
+	bool				invalid;
+};
+
 struct amdxdna_mem {
 	u64				userptr;
 	void				*kva;
@@ -13,8 +26,7 @@ struct amdxdna_mem {
 	size_t				size;
 	struct page			**pages;
 	u32				nr_pages;
-	struct mmu_interval_notifier	notifier;
-	unsigned long			*pfns;
+	struct list_head		umap_list;
 	bool				map_invalid;
 };
 
@@ -34,6 +46,7 @@ struct amdxdna_gem_obj {
 };
 
 #define to_gobj(obj)    (&(obj)->base.base)
+#define is_import_bo(obj) (to_gobj(obj)->import_attach)
 
 static inline struct amdxdna_gem_obj *to_xdna_obj(struct drm_gem_object *gobj)
 {
@@ -47,8 +60,12 @@ static inline void amdxdna_gem_put_obj(struct amdxdna_gem_obj *abo)
 	drm_gem_object_put(to_gobj(abo));
 }
 
+void amdxdna_umap_put(struct amdxdna_umap *mapp);
+
 struct drm_gem_object *
 amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size);
+struct drm_gem_object *
+amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf);
 struct amdxdna_gem_obj *
 amdxdna_drm_alloc_dev_bo(struct drm_device *dev,
 			 struct amdxdna_drm_create_bo *args,
drivers/accel/amdxdna/amdxdna_pci_drv.c +10 -1
index f5b8497cf5ad..f2bf1d374cc7 100644
@@ -226,6 +226,7 @@ const struct drm_driver amdxdna_drm_drv = {
 	.num_ioctls = ARRAY_SIZE(amdxdna_drm_ioctls),
 
 	.gem_create_object = amdxdna_gem_create_object_cb,
+	.gem_prime_import = amdxdna_gem_prime_import,
 };
 
 static const struct amdxdna_dev_info *
@@ -266,12 +267,16 @@ static int amdxdna_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		fs_reclaim_release(GFP_KERNEL);
 	}
 
+	xdna->notifier_wq = alloc_ordered_workqueue("notifier_wq", 0);
+	if (!xdna->notifier_wq)
+		return -ENOMEM;
+
 	mutex_lock(&xdna->dev_lock);
 	ret = xdna->dev_info->ops->init(xdna);
 	mutex_unlock(&xdna->dev_lock);
 	if (ret) {
 		XDNA_ERR(xdna, "Hardware init failed, ret %d", ret);
-		return ret;
+		goto destroy_notifier_wq;
 	}
 
 	ret = amdxdna_sysfs_init(xdna);
@@ -301,6 +306,8 @@ static int amdxdna_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	mutex_lock(&xdna->dev_lock);
 	xdna->dev_info->ops->fini(xdna);
 	mutex_unlock(&xdna->dev_lock);
+destroy_notifier_wq:
+	destroy_workqueue(xdna->notifier_wq);
 	return ret;
 }
 
@@ -310,6 +317,8 @@ static void amdxdna_remove(struct pci_dev *pdev)
 	struct device *dev = &pdev->dev;
 	struct amdxdna_client *client;
 
+	destroy_workqueue(xdna->notifier_wq);
+
 	pm_runtime_get_noresume(dev);
 	pm_runtime_forbid(dev);
 
drivers/accel/amdxdna/amdxdna_pci_drv.h +2 -0
index 37848a8d8031..ab79600911aa 100644
@@ -6,6 +6,7 @@
 #ifndef _AMDXDNA_PCI_DRV_H_
 #define _AMDXDNA_PCI_DRV_H_
 
+#include <linux/workqueue.h>
 #include <linux/xarray.h>
 
 #define XDNA_INFO(xdna, fmt, args...)	drm_info(&(xdna)->ddev, fmt, ##args)
@@ -98,6 +99,7 @@ struct amdxdna_dev {
 	struct list_head		client_list;
 	struct amdxdna_fw_ver		fw_ver;
 	struct rw_semaphore		notifier_lock; /* for mmu notifier*/
+	struct workqueue_struct		*notifier_wq;
 };
 
 /*
Signature · 1 line
2.34.1
#1 lore raw
Quoted 26 lines · Lizhi Hou
drivers/accel/amdxdna/amdxdna_gem.c +26 -0
+struct drm_gem_object *
+amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
+{
+	struct dma_buf_attachment *attach;
+	struct drm_gem_object *gobj;
+	struct sg_table *sgt;
+	int ret;
+
+	attach = dma_buf_attach(dma_buf, dev->dev);
+	if (IS_ERR(attach))
+		return ERR_CAST(attach);
+
+	get_dma_buf(dma_buf);
+
+	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+	if (IS_ERR(sgt)) {
+		ret = PTR_ERR(sgt);
+		goto fail_detach;
+	}
+
+	gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
+	if (IS_ERR(gobj)) {
+		ret = PTR_ERR(gobj);
+		goto fail_unmap;
+	}
+

drivers/accel/amdxdna/amdxdna_gem.c amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size) · line 466

Quoted 10 lines
drivers/accel/amdxdna/amdxdna_gem.c +10 -0
+	gobj->import_attach = attach;
+	gobj->resv = dma_buf->resv;
+
+	return gobj;
+
+fail_unmap:
+	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+fail_detach:
+	dma_buf_detach(dma_buf, attach);
+	dma_buf_put(dma_buf);

You attach() and then get(), so normal "reverse order" cleanup would be put(), then detach(). That is not what you do here. Should this be reordered, or should you get() then attach() first?

author maintainer
#2 lore raw
Quoted 32 lines · Jeff Hugo
Quoted 31 lines · Lizhi Hou
(quoted diff) +2 -0
+struct drm_gem_object *
+amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf 

*dma_buf)

drivers/accel/amdxdna/amdxdna_gem.c +28 -0
+{
+    struct dma_buf_attachment *attach;
+    struct drm_gem_object *gobj;
+    struct sg_table *sgt;
+    int ret;
+
+    attach = dma_buf_attach(dma_buf, dev->dev);
+    if (IS_ERR(attach))
+        return ERR_CAST(attach);
+
+    get_dma_buf(dma_buf);
+
+    sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+    if (IS_ERR(sgt)) {
+        ret = PTR_ERR(sgt);
+        goto fail_detach;
+    }
+
+    gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
+    if (IS_ERR(gobj)) {
+        ret = PTR_ERR(gobj);
+        goto fail_unmap;
+    }
+
+    gobj->import_attach = attach;
+    gobj->resv = dma_buf->resv;
+
+    return gobj;
Quoted 8 lines

drivers/accel/amdxdna/amdxdna_gem.c amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size) · line 466

Quoted 6 lines
drivers/accel/amdxdna/amdxdna_gem.c +6 -0
+
+fail_unmap:
+    dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+fail_detach:
+    dma_buf_detach(dma_buf, attach);
+    dma_buf_put(dma_buf);

You attach() and then get(), so normal "reverse order" cleanup would be put(), then detach(). That is not what you do here. Should this be reordered, or should you get() then attach() first?

I referred drm_gem_prime_import_dev(). And I agree with you. It looks better to get() before attach(). I will respin V2 which will also contain another small update for this patch.

Thanks,

Lizhi

Quoted 2 lines
author maintainer
#3 lore raw

Hi Jeff,

Just noticed that the driver should not use import_attach. https://lore.kernel.org/all/[email protected]/

I will remove the import_attach usage and send V3 patch.

Thanks

Lizhi

Quoted 52 lines · Lizhi Hou
Quoted 32 lines · Jeff Hugo
Quoted 31 lines · Lizhi Hou
(quoted diff) +2 -0
+struct drm_gem_object *
+amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf 

*dma_buf)

drivers/accel/amdxdna/amdxdna_gem.c +28 -0
+{
+    struct dma_buf_attachment *attach;
+    struct drm_gem_object *gobj;
+    struct sg_table *sgt;
+    int ret;
+
+    attach = dma_buf_attach(dma_buf, dev->dev);
+    if (IS_ERR(attach))
+        return ERR_CAST(attach);
+
+    get_dma_buf(dma_buf);
+
+    sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+    if (IS_ERR(sgt)) {
+        ret = PTR_ERR(sgt);
+        goto fail_detach;
+    }
+
+    gobj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
+    if (IS_ERR(gobj)) {
+        ret = PTR_ERR(gobj);
+        goto fail_unmap;
+    }
+
+    gobj->import_attach = attach;
+    gobj->resv = dma_buf->resv;
+
+    return gobj;
Quoted 8 lines

drivers/accel/amdxdna/amdxdna_gem.c amdxdna_gem_create_object_cb(struct drm_device *dev, size_t size) · line 466

Quoted 6 lines
drivers/accel/amdxdna/amdxdna_gem.c +6 -0
+
+fail_unmap:
+    dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+fail_detach:
+    dma_buf_detach(dma_buf, attach);
+    dma_buf_put(dma_buf);

You attach() and then get(), so normal "reverse order" cleanup would be put(), then detach(). That is not what you do here. Should this be reordered, or should you get() then attach() first?

I referred drm_gem_prime_import_dev(). And I agree with you. It looks better to get() before attach(). I will respin V2 which will also contain another small update for this patch.

Thanks,

Lizhi

Quoted 2 lines