Skip to contentSkip to conversation

drm/v3d: Add drm core helper, use shmem helpers

PATCH In review Eric Anholt 2 participants 11 messages Mar 8, 2019 – Mar 14, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover drm/v3d: Add drm core helper, use shmem helpers

This was inspired by Rob's respin of the shmem helpers patch for panfrost (which the final patch depends on). And, looking at panfrost and lima, recently I realized that we all had some nasty cargo-cult code for BO reservations that we can share now that the reservation object is in the core GEM struct.

Eric Anholt (4):
drm: Add helpers for locking an array of BO reservations. drm/v3d: Use drm_gem_lock_reservations()/drm_gem_unlock_reservations() drm/v3d: Remove some dead members of struct v3d_bo. drm/v3d: Use the new shmem helpers to reduce driver boilerplate.

 drivers/gpu/drm/drm_gem.c     |  76 +++++++++
 drivers/gpu/drm/v3d/Kconfig   |   1 +
 drivers/gpu/drm/v3d/v3d_bo.c  | 309 ++++++++++------------------------
 drivers/gpu/drm/v3d/v3d_drv.c |  27 +--
 drivers/gpu/drm/v3d/v3d_drv.h |  25 +--
 drivers/gpu/drm/v3d/v3d_gem.c |  67 ++------
 drivers/gpu/drm/v3d/v3d_irq.c |   8 +-
 drivers/gpu/drm/v3d/v3d_mmu.c |  34 +++-
 include/drm/drm_gem.h         |   4 +
 9 files changed, 220 insertions(+), 331 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/4 drm: Add helpers for locking an array of BO reservations.

Now that we have the reservation object in the GEM object, it's easy to provide a helper for this common case. Noticed while reviewing panfrost and lima drivers. This particular version came out of v3d, which in turn was a copy from vc4.

---
 drivers/gpu/drm/drm_gem.c | 76 +++++++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  4 +++
 2 files changed, 80 insertions(+)
drivers/gpu/drm/drm_gem.c +76 -0
index ad124f5a6f4d..9fd804f7d7ca 100644
@@ -1233,3 +1233,79 @@ void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr)
 		obj->dev->driver->gem_prime_vunmap(obj, vaddr);
 }
 EXPORT_SYMBOL(drm_gem_vunmap);
+
+/**
+ * drm_gem_lock_bo_reservations - Sets up the ww context and acquires
+ * the lock on an array of GEM objects.
+ *
+ * Once you've locked your reservations, you'll want to set up space
+ * for your shared fences (if applicable), submit your job, then
+ * drm_gem_unlock_reservations().
+ *
+ * @acquire_ctx - struct ww_acquire_ctx that will be initialized as
+ * part of tracking this set of locked reservations.
+ */
+int
+drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
+			  struct ww_acquire_ctx *acquire_ctx)
+{
+	int contended = -1;
+	int i, ret;
+
+	ww_acquire_init(acquire_ctx, &reservation_ww_class);
+
+retry:
+	if (contended != -1) {
+		struct drm_gem_object *obj = objs[contended];
+
+		ret = ww_mutex_lock_slow_interruptible(&obj->resv->lock,
+						       acquire_ctx);
+		if (ret) {
+			ww_acquire_done(acquire_ctx);
+			return ret;
+		}
+	}
+
+	for (i = 0; i < count; i++) {
+		if (i == contended)
+			continue;
+
+		ret = ww_mutex_lock_interruptible(&objs[i]->resv->lock,
+						  acquire_ctx);
+		if (ret) {
+			int j;
+
+			for (j = 0; j < i; j++)
+				ww_mutex_unlock(&objs[j]->resv->lock);
+
+			if (contended != -1 && contended >= i)
+				ww_mutex_unlock(&objs[contended]->resv->lock);
+
+			if (ret == -EDEADLK) {
+				contended = i;
+				goto retry;
+			}
+
+			ww_acquire_done(acquire_ctx);
+			return ret;
+		}
+	}
+
+	ww_acquire_done(acquire_ctx);
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_gem_lock_reservations);
+
+void
+drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
+			    struct ww_acquire_ctx *acquire_ctx)
+{
+	int i;
+
+	for (i = 0; i < count; i++)
+		ww_mutex_unlock(&objs[i]->resv->lock);
+
+	ww_acquire_fini(acquire_ctx);
+}
+EXPORT_SYMBOL(drm_gem_unlock_reservations);
include/drm/drm_gem.h +4 -0
index 25f1ff2df464..2955aaab3dca 100644
@@ -384,6 +384,10 @@ void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
 long drm_gem_reservation_object_wait(struct drm_file *filep, u32 handle,
 				    bool wait_all, unsigned long timeout);
+int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
+			      struct ww_acquire_ctx *acquire_ctx);
+void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
+				 struct ww_acquire_ctx *acquire_ctx);
 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
 			    u32 handle, u64 *offset);
 int drm_gem_dumb_destroy(struct drm_file *file,
Signature · 6 lines
2.20.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Acked-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] drm: Add helpers for locking an array of BO reservations.

Quoted 11 lines · Eric Anholt

Now that we have the reservation object in the GEM object, it's easy to provide a helper for this common case. Noticed while reviewing panfrost and lima drivers. This particular version came out of v3d, which in turn was a copy from vc4.

---
 drivers/gpu/drm/drm_gem.c | 76 +++++++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  4 +++
 2 files changed, 80 insertions(+)

Sweet! I was about to go write this same patch. You are changing the license from GPL to MIT copying the v3d version, but I guess you have rights to do that.

FWIW,

Rob

#3 lore raw

Re: [PATCH 1/4] drm: Add helpers for locking an array of BO reservations.

Quoted 13 lines · Rob Herring
Quoted 11 lines · Eric Anholt

Now that we have the reservation object in the GEM object, it's easy to provide a helper for this common case. Noticed while reviewing panfrost and lima drivers. This particular version came out of v3d, which in turn was a copy from vc4.

---
 drivers/gpu/drm/drm_gem.c | 76 +++++++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  4 +++
 2 files changed, 80 insertions(+)
Quoted 3 lines

Sweet! I was about to go write this same patch. You are changing the license from GPL to MIT copying the v3d version, but I guess you have rights to do that.

The original vc4 copy I had was MIT, anyway.

#4 lore raw

Re: [PATCH 1/4] drm: Add helpers for locking an array of BO reservations.

In reply to Rob Herring
Quoted 13 lines · Rob Herring
Quoted 11 lines · Eric Anholt

Now that we have the reservation object in the GEM object, it's easy to provide a helper for this common case. Noticed while reviewing panfrost and lima drivers. This particular version came out of v3d, which in turn was a copy from vc4.

---
 drivers/gpu/drm/drm_gem.c | 76 +++++++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  4 +++
 2 files changed, 80 insertions(+)
Quoted 7 lines

Sweet! I was about to go write this same patch. You are changing the license from GPL to MIT copying the v3d version, but I guess you have rights to do that.

FWIW,

Was this just for this one patch, or the series? I don't think I can merge without a consumer.

#5 lore raw

Re: [PATCH 1/4] drm: Add helpers for locking an array of BO reservations.

Quoted 20 lines · Eric Anholt
Quoted 17 lines · Rob Herring
Quoted 11 lines · Eric Anholt

Now that we have the reservation object in the GEM object, it's easy to provide a helper for this common case. Noticed while reviewing panfrost and lima drivers. This particular version came out of v3d, which in turn was a copy from vc4.

---
 drivers/gpu/drm/drm_gem.c | 76 +++++++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  4 +++
 2 files changed, 80 insertions(+)

Sweet! I was about to go write this same patch. You are changing the license from GPL to MIT copying the v3d version, but I guess you have rights to do that.

Quoted 6 lines
Quoted 3 lines

FWIW,

Was this just for this one patch, or the series? I don't think I can merge without a consumer.

Sure, it can be for patches 1-3. Patch 4 is dependent on me sending out the shmem helpers again.

Rob

#6 lore raw

Re: [PATCH 1/4] drm: Add helpers for locking an array of BO reservations.

Quoted 23 lines · Rob Herring
Quoted 22 lines · Eric Anholt
Quoted 19 lines · Rob Herring
Quoted 11 lines · Eric Anholt

Now that we have the reservation object in the GEM object, it's easy to provide a helper for this common case. Noticed while reviewing panfrost and lima drivers. This particular version came out of v3d, which in turn was a copy from vc4.

---
 drivers/gpu/drm/drm_gem.c | 76 +++++++++++++++++++++++++++++++++++++++
 include/drm/drm_gem.h     |  4 +++
 2 files changed, 80 insertions(+)

Sweet! I was about to go write this same patch. You are changing the license from GPL to MIT copying the v3d version, but I guess you have rights to do that.

FWIW,

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Was this just for this one patch, or the series? I don't think I can merge without a consumer.

Sure, it can be for patches 1-3. Patch 4 is dependent on me sending out the shmem helpers again.

Thanks. Pushed the first 3 now.

#7 lore raw

PATCH 2/4 drm/v3d: Use drm_gem_lock_reservations()/drm_gem_unlock_reservations()

In reply to Eric Anholt

Now that we have core helpers, this gets rid of a lot of boilerplate.

---
 drivers/gpu/drm/v3d/v3d_gem.c | 56 ++++-------------------------------
 1 file changed, 6 insertions(+), 50 deletions(-)
drivers/gpu/drm/v3d/v3d_gem.c +6 -50
index 0d1e5e0b8042..a4338d56ff9e 100644
@@ -199,12 +199,8 @@ v3d_unlock_bo_reservations(struct v3d_bo **bos,
 			   int bo_count,
 			   struct ww_acquire_ctx *acquire_ctx)
 {
-	int i;
-
-	for (i = 0; i < bo_count; i++)
-		ww_mutex_unlock(&bos[i]->base.resv->lock);
-
-	ww_acquire_fini(acquire_ctx);
+	drm_gem_unlock_reservations((struct drm_gem_object **)bos, bo_count,
+				    acquire_ctx);
 }
 
 /* Takes the reservation lock on all the BOs being referenced, so that
@@ -219,52 +215,12 @@ v3d_lock_bo_reservations(struct v3d_bo **bos,
 			 int bo_count,
 			 struct ww_acquire_ctx *acquire_ctx)
 {
-	int contended_lock = -1;
 	int i, ret;
 
-	ww_acquire_init(acquire_ctx, &reservation_ww_class);
-
-retry:
-	if (contended_lock != -1) {
-		struct v3d_bo *bo = bos[contended_lock];
-
-		ret = ww_mutex_lock_slow_interruptible(&bo->base.resv->lock,
-						       acquire_ctx);
-		if (ret) {
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	for (i = 0; i < bo_count; i++) {
-		if (i == contended_lock)
-			continue;
-
-		ret = ww_mutex_lock_interruptible(&bos[i]->base.resv->lock,
-						  acquire_ctx);
-		if (ret) {
-			int j;
-
-			for (j = 0; j < i; j++)
-				ww_mutex_unlock(&bos[j]->base.resv->lock);
-
-			if (contended_lock != -1 && contended_lock >= i) {
-				struct v3d_bo *bo = bos[contended_lock];
-
-				ww_mutex_unlock(&bo->base.resv->lock);
-			}
-
-			if (ret == -EDEADLK) {
-				contended_lock = i;
-				goto retry;
-			}
-
-			ww_acquire_done(acquire_ctx);
-			return ret;
-		}
-	}
-
-	ww_acquire_done(acquire_ctx);
+	ret = drm_gem_lock_reservations((struct drm_gem_object **)bos,
+					bo_count, acquire_ctx);
+	if (ret)
+		return ret;
 
 	/* Reserve space for our shared (read-only) fence references,
 	 * before we commit the CL to the hardware.
Signature · 1 line
2.20.1
#8 lore raw

PATCH 3/4 drm/v3d: Remove some dead members of struct v3d_bo.

In reply to Eric Anholt

vmas was from the previous model of page table management (one per fd), and vaddr was left over from vc4.

---
 drivers/gpu/drm/v3d/v3d_bo.c  | 1 -
 drivers/gpu/drm/v3d/v3d_drv.h | 9 ---------
 2 files changed, 10 deletions(-)
drivers/gpu/drm/v3d/v3d_bo.c +0 -1
index 5c83b392b20a..dff38bf36c50 100644
@@ -117,7 +117,6 @@ static struct v3d_bo *v3d_bo_create_struct(struct drm_device *dev,
 		return ERR_PTR(-ENOMEM);
 	obj = &bo->base;
 
-	INIT_LIST_HEAD(&bo->vmas);
 	INIT_LIST_HEAD(&bo->unref_head);
 	mutex_init(&bo->lock);
 
drivers/gpu/drm/v3d/v3d_drv.h +0 -9
index d856159bd007..f5ed915a0f15 100644
@@ -108,12 +108,6 @@ struct v3d_file_priv {
 	struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
 };
 
-/* Tracks a mapping of a BO into a per-fd address space */
-struct v3d_vma {
-	struct v3d_page_table *pt;
-	struct list_head list; /* entry in v3d_bo.vmas */
-};
-
 struct v3d_bo {
 	struct drm_gem_object base;
 
@@ -124,9 +118,6 @@ struct v3d_bo {
 	u32 pages_refcount;
 	struct page **pages;
 	struct sg_table *sgt;
-	void *vaddr;
-
-	struct list_head vmas;    /* list of v3d_vma */
 
 	/* List entry for the BO's position in
 	 * v3d_exec_info->unref_list
Signature · 6 lines
2.20.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

PATCH 4/4 drm/v3d: Use the new shmem helpers to reduce driver boilerplate.

In reply to Eric Anholt

Depends on
https://patchwork.freedesktop.org/patch/290754/?series=57669&rev=1 -- Rob and I have been talking about adding some more help for the dma_map_sg() code in v3d_mmu.c (which panfrost has a similar version of), but this already seems like a good cleanup.

---
 drivers/gpu/drm/v3d/Kconfig   |   1 +
 drivers/gpu/drm/v3d/v3d_bo.c  | 308 ++++++++++------------------------
 drivers/gpu/drm/v3d/v3d_drv.c |  27 +--
 drivers/gpu/drm/v3d/v3d_drv.h |  16 +-
 drivers/gpu/drm/v3d/v3d_gem.c |  11 +-
 drivers/gpu/drm/v3d/v3d_irq.c |   8 +-
 drivers/gpu/drm/v3d/v3d_mmu.c |  34 +++-
 7 files changed, 134 insertions(+), 271 deletions(-)
drivers/gpu/drm/v3d/Kconfig +1 -0
index 1552bf552c94..75a74c45f109 100644
@@ -5,6 +5,7 @@ config DRM_V3D
 	depends on COMMON_CLK
 	depends on MMU
 	select DRM_SCHED
+	select DRM_GEM_SHMEM_HELPER
 	help
 	  Choose this option if you have a system that has a Broadcom
 	  V3D 3.x or newer GPU, such as BCM7268.
drivers/gpu/drm/v3d/v3d_bo.c +87 -221

382 lines not on this page · raw message

drivers/gpu/drm/v3d/v3d_drv.c +3 -24
index 3680ebd229f2..537ee2b44e42 100644
@@ -160,17 +160,7 @@ v3d_postclose(struct drm_device *dev, struct drm_file *file)
 	kfree(v3d_priv);
 }
 
-static const struct file_operations v3d_drm_fops = {
-	.owner = THIS_MODULE,
-	.open = drm_open,
-	.release = drm_release,
-	.unlocked_ioctl = drm_ioctl,
-	.mmap = v3d_mmap,
-	.poll = drm_poll,
-	.read = drm_read,
-	.compat_ioctl = drm_compat_ioctl,
-	.llseek = noop_llseek,
-};
+DEFINE_DRM_GEM_SHMEM_FOPS(v3d_drm_fops);
 
 /* DRM_AUTH is required on SUBMIT_CL for now, while we don't have GMP
  * protection between clients.  Note that render nodes would be be
@@ -188,12 +178,6 @@ static const struct drm_ioctl_desc v3d_drm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(V3D_SUBMIT_TFU, v3d_submit_tfu_ioctl, DRM_RENDER_ALLOW | DRM_AUTH),
 };
 
-static const struct vm_operations_struct v3d_vm_ops = {
-	.fault = v3d_gem_fault,
-	.open = drm_gem_vm_open,
-	.close = drm_gem_vm_close,
-};
-
 static struct drm_driver v3d_drm_driver = {
 	.driver_features = (DRIVER_GEM |
 			    DRIVER_RENDER |
@@ -207,16 +191,11 @@ static struct drm_driver v3d_drm_driver = {
 	.debugfs_init = v3d_debugfs_init,
 #endif
 
-	.gem_free_object_unlocked = v3d_free_object,
-	.gem_vm_ops = &v3d_vm_ops,
-
+	.gem_create_object = v3d_create_object,
 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
-	.gem_prime_import = drm_gem_prime_import,
-	.gem_prime_export = drm_gem_prime_export,
-	.gem_prime_get_sg_table	= v3d_prime_get_sg_table,
 	.gem_prime_import_sg_table = v3d_prime_import_sg_table,
-	.gem_prime_mmap = v3d_prime_mmap,
+	.gem_prime_mmap = drm_gem_prime_mmap,
 
 	.ioctls = v3d_drm_ioctls,
 	.num_ioctls = ARRAY_SIZE(v3d_drm_ioctls),
drivers/gpu/drm/v3d/v3d_drv.h +4 -12
index f5ed915a0f15..1d3419e367a8 100644
@@ -5,6 +5,7 @@
 #include <drm/drmP.h>
 #include <drm/drm_encoder.h>
 #include <drm/drm_gem.h>
+#include <drm/drm_gem_shmem_helper.h>
 #include <drm/gpu_scheduler.h>
 #include "uapi/drm/v3d_drm.h"
 
@@ -109,16 +110,10 @@ struct v3d_file_priv {
 };
 
 struct v3d_bo {
-	struct drm_gem_object base;
-
-	struct mutex lock;
+	struct drm_gem_shmem_object base;
 
 	struct drm_mm_node node;
 
-	u32 pages_refcount;
-	struct page **pages;
-	struct sg_table *sgt;
-
 	/* List entry for the BO's position in
 	 * v3d_exec_info->unref_list
 	 */
@@ -256,6 +251,7 @@ static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
 }
 
 /* v3d_bo.c */
+struct drm_gem_object *v3d_create_object(struct drm_device *dev, size_t size);
 void v3d_free_object(struct drm_gem_object *gem_obj);
 struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
 			     size_t size);
@@ -265,10 +261,6 @@ int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data,
 		      struct drm_file *file_priv);
 int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
 			    struct drm_file *file_priv);
-vm_fault_t v3d_gem_fault(struct vm_fault *vmf);
-int v3d_mmap(struct file *filp, struct vm_area_struct *vma);
-int v3d_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
-struct sg_table *v3d_prime_get_sg_table(struct drm_gem_object *obj);
 struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
 						 struct dma_buf_attachment *attach,
 						 struct sg_table *sgt);
@@ -304,7 +296,7 @@ void v3d_irq_reset(struct v3d_dev *v3d);
 int v3d_mmu_get_offset(struct drm_file *file_priv, struct v3d_bo *bo,
 		       u32 *offset);
 int v3d_mmu_set_page_table(struct v3d_dev *v3d);
-void v3d_mmu_insert_ptes(struct v3d_bo *bo);
+int v3d_mmu_insert_ptes(struct v3d_bo *bo);
 void v3d_mmu_remove_ptes(struct v3d_bo *bo);
 
 /* v3d_sched.c */
drivers/gpu/drm/v3d/v3d_gem.c +6 -5
index a4338d56ff9e..e61afb09214b 100644
@@ -190,7 +190,7 @@ v3d_attach_object_fences(struct v3d_bo **bos, int bo_count,
 
 	for (i = 0; i < bo_count; i++) {
 		/* XXX: Use shared fences for read-only objects. */
-		reservation_object_add_excl_fence(bos[i]->base.resv, fence);
+		reservation_object_add_excl_fence(bos[i]->base.base.resv, fence);
 	}
 }
 
@@ -226,7 +226,8 @@ v3d_lock_bo_reservations(struct v3d_bo **bos,
 	 * before we commit the CL to the hardware.
 	 */
 	for (i = 0; i < bo_count; i++) {
-		ret = reservation_object_reserve_shared(bos[i]->base.resv, 1);
+		ret = reservation_object_reserve_shared(bos[i]->base.base.resv,
+							1);
 		if (ret) {
 			v3d_unlock_bo_reservations(bos, bo_count,
 						   acquire_ctx);
@@ -334,11 +335,11 @@ v3d_exec_cleanup(struct kref *ref)
 	dma_fence_put(exec->render_done_fence);
 
 	for (i = 0; i < exec->bo_count; i++)
-		drm_gem_object_put_unlocked(&exec->bo[i]->base);
+		drm_gem_object_put_unlocked(&exec->bo[i]->base.base);
 	kvfree(exec->bo);
 
 	list_for_each_entry_safe(bo, save, &exec->unref_list, unref_head) {
-		drm_gem_object_put_unlocked(&bo->base);
+		drm_gem_object_put_unlocked(&bo->base.base);
 	}
 
 	pm_runtime_mark_last_busy(v3d->dev);
@@ -365,7 +366,7 @@ v3d_tfu_job_cleanup(struct kref *ref)
 
 	for (i = 0; i < ARRAY_SIZE(job->bo); i++) {
 		if (job->bo[i])
-			drm_gem_object_put_unlocked(&job->bo[i]->base);
+			drm_gem_object_put_unlocked(&job->bo[i]->base.base);
 	}
 
 	pm_runtime_mark_last_busy(v3d->dev);
drivers/gpu/drm/v3d/v3d_irq.c +5 -3
index 69338da70ddc..a59f87275bf6 100644
@@ -34,12 +34,14 @@ v3d_overflow_mem_work(struct work_struct *work)
 		container_of(work, struct v3d_dev, overflow_mem_work);
 	struct drm_device *dev = &v3d->drm;
 	struct v3d_bo *bo = v3d_bo_create(dev, NULL /* XXX: GMP */, 256 * 1024);
+	struct drm_gem_object *obj;
 	unsigned long irqflags;
 
 	if (IS_ERR(bo)) {
 		DRM_ERROR("Couldn't allocate binner overflow mem\n");
 		return;
 	}
+	obj = &bo->base.base;
 
 	/* We lost a race, and our work task came in after the bin job
 	 * completed and exited.  This can happen because the HW
@@ -56,15 +58,15 @@ v3d_overflow_mem_work(struct work_struct *work)
 		goto out;
 	}
 
-	drm_gem_object_get(&bo->base);
+	drm_gem_object_get(obj);
 	list_add_tail(&bo->unref_head, &v3d->bin_job->unref_list);
 	spin_unlock_irqrestore(&v3d->job_lock, irqflags);
 
 	V3D_CORE_WRITE(0, V3D_PTB_BPOA, bo->node.start << PAGE_SHIFT);
-	V3D_CORE_WRITE(0, V3D_PTB_BPOS, bo->base.size);
+	V3D_CORE_WRITE(0, V3D_PTB_BPOS, obj->size);
 
 out:
-	drm_gem_object_put_unlocked(&bo->base);
+	drm_gem_object_put_unlocked(obj);
 }
 
 static irqreturn_t
drivers/gpu/drm/v3d/v3d_mmu.c +28 -6
index b00f97c31b70..aa351899afda 100644
@@ -81,15 +81,27 @@ int v3d_mmu_set_page_table(struct v3d_dev *v3d)
 	return v3d_mmu_flush_all(v3d);
 }
 
-void v3d_mmu_insert_ptes(struct v3d_bo *bo)
+int v3d_mmu_insert_ptes(struct v3d_bo *bo)
 {
-	struct v3d_dev *v3d = to_v3d_dev(bo->base.dev);
+	struct drm_gem_shmem_object *shmem_obj = &bo->base;
+	struct v3d_dev *v3d = to_v3d_dev(shmem_obj->base.dev);
 	u32 page = bo->node.start;
 	u32 page_prot = V3D_PTE_WRITEABLE | V3D_PTE_VALID;
 	unsigned int count;
 	struct scatterlist *sgl;
 
-	for_each_sg(bo->sgt->sgl, sgl, bo->sgt->nents, count) {
+	if (!shmem_obj->base.import_attach) {
+		WARN_ON_ONCE(shmem_obj->sgt);
+		shmem_obj->sgt = drm_prime_pages_to_sg(shmem_obj->pages,
+						       shmem_obj->base.size >> PAGE_SHIFT);
+		if (IS_ERR(shmem_obj->sgt))
+			return PTR_ERR(shmem_obj->sgt);
+		/* Map the pages for use by the GPU. */
+		dma_map_sg(v3d->dev, shmem_obj->sgt->sgl,
+			   shmem_obj->sgt->nents, DMA_BIDIRECTIONAL);
+	}
+
+	for_each_sg(shmem_obj->sgt->sgl, sgl, shmem_obj->sgt->nents, count) {
 		u32 page_address = sg_dma_address(sgl) >> V3D_MMU_PAGE_SHIFT;
 		u32 pte = page_prot | page_address;
 		u32 i;
@@ -102,16 +114,19 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo)
 	}
 
 	WARN_ON_ONCE(page - bo->node.start !=
-		     bo->base.size >> V3D_MMU_PAGE_SHIFT);
+		     shmem_obj->base.size >> V3D_MMU_PAGE_SHIFT);
 
 	if (v3d_mmu_flush_all(v3d))
 		dev_err(v3d->dev, "MMU flush timeout\n");
+
+	return 0;
 }
 
 void v3d_mmu_remove_ptes(struct v3d_bo *bo)
 {
-	struct v3d_dev *v3d = to_v3d_dev(bo->base.dev);
-	u32 npages = bo->base.size >> V3D_MMU_PAGE_SHIFT;
+	struct drm_gem_shmem_object *shmem_obj = &bo->base;
+	struct v3d_dev *v3d = to_v3d_dev(shmem_obj->base.dev);
+	u32 npages = shmem_obj->base.size >> V3D_MMU_PAGE_SHIFT;
 	u32 page;
 
 	for (page = bo->node.start; page < bo->node.start + npages; page++)
@@ -119,4 +134,11 @@ void v3d_mmu_remove_ptes(struct v3d_bo *bo)
 
 	if (v3d_mmu_flush_all(v3d))
 		dev_err(v3d->dev, "MMU flush timeout\n");
+
+	if (!shmem_obj->base.import_attach) {
+		dma_unmap_sg(v3d->dev, shmem_obj->sgt->sgl,
+			     shmem_obj->sgt->nents, DMA_BIDIRECTIONAL);
+		sg_free_table(shmem_obj->sgt);
+		kfree(shmem_obj->sgt);
+	}
 }
Signature · 6 lines
2.20.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#10 lore raw

Re: [PATCH 4/4] drm/v3d: Use the new shmem helpers to reduce driver boilerplate.

Quoted 6 lines · Eric Anholt

Depends on
https://patchwork.freedesktop.org/patch/290754/?series=57669&rev=1 -- Rob and I have been talking about adding some more help for the dma_map_sg() code in v3d_mmu.c (which panfrost has a similar version of), but this already seems like a good cleanup.

There's an updated version here with the helper[1].

Rob

[1] https://gitlab.freedesktop.org/robh/linux-panfrost/commit/403e85012e9ee0a2a113bec757f545df193cfb7a