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

2019-03-08 Thread Rob Herring
On Fri, Mar 8, 2019 at 10:17 AM Eric Anholt  wrote:
>
> Depends on
> https://patchwork.freedesktop.org/patch/290754/?series=57669=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


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

2019-03-08 Thread Eric Anholt
Depends on
https://patchwork.freedesktop.org/patch/290754/?series=57669=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.

Signed-off-by: Eric Anholt 
---
 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(-)

diff --git a/drivers/gpu/drm/v3d/Kconfig b/drivers/gpu/drm/v3d/Kconfig
index 1552bf552c94..75a74c45f109 100644
--- a/drivers/gpu/drm/v3d/Kconfig
+++ b/drivers/gpu/drm/v3d/Kconfig
@@ -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.
diff --git a/drivers/gpu/drm/v3d/v3d_bo.c b/drivers/gpu/drm/v3d/v3d_bo.c
index dff38bf36c50..b631ade09853 100644
--- a/drivers/gpu/drm/v3d/v3d_bo.c
+++ b/drivers/gpu/drm/v3d/v3d_bo.c
@@ -25,158 +25,6 @@
 #include "v3d_drv.h"
 #include "uapi/drm/v3d_drm.h"
 
-/* Pins the shmem pages, fills in the .pages and .sgt fields of the BO, and 
maps
- * it for DMA.
- */
-static int
-v3d_bo_get_pages(struct v3d_bo *bo)
-{
-   struct drm_gem_object *obj = >base;
-   struct drm_device *dev = obj->dev;
-   int npages = obj->size >> PAGE_SHIFT;
-   int ret = 0;
-
-   mutex_lock(>lock);
-   if (bo->pages_refcount++ != 0)
-   goto unlock;
-
-   if (!obj->import_attach) {
-   bo->pages = drm_gem_get_pages(obj);
-   if (IS_ERR(bo->pages)) {
-   ret = PTR_ERR(bo->pages);
-   goto unlock;
-   }
-
-   bo->sgt = drm_prime_pages_to_sg(bo->pages, npages);
-   if (IS_ERR(bo->sgt)) {
-   ret = PTR_ERR(bo->sgt);
-   goto put_pages;
-   }
-
-   /* Map the pages for use by the GPU. */
-   dma_map_sg(dev->dev, bo->sgt->sgl,
-  bo->sgt->nents, DMA_BIDIRECTIONAL);
-   } else {
-   bo->pages = kcalloc(npages, sizeof(*bo->pages), GFP_KERNEL);
-   if (!bo->pages)
-   goto put_pages;
-
-   drm_prime_sg_to_page_addr_arrays(bo->sgt, bo->pages,
-NULL, npages);
-
-   /* Note that dma-bufs come in mapped. */
-   }
-
-   mutex_unlock(>lock);
-
-   return 0;
-
-put_pages:
-   drm_gem_put_pages(obj, bo->pages, true, true);
-   bo->pages = NULL;
-unlock:
-   bo->pages_refcount--;
-   mutex_unlock(>lock);
-   return ret;
-}
-
-static void
-v3d_bo_put_pages(struct v3d_bo *bo)
-{
-   struct drm_gem_object *obj = >base;
-
-   mutex_lock(>lock);
-   if (--bo->pages_refcount == 0) {
-   if (!obj->import_attach) {
-   dma_unmap_sg(obj->dev->dev, bo->sgt->sgl,
-bo->sgt->nents, DMA_BIDIRECTIONAL);
-   sg_free_table(bo->sgt);
-   kfree(bo->sgt);
-   drm_gem_put_pages(obj, bo->pages, true, true);
-   } else {
-   kfree(bo->pages);
-   }
-   }
-   mutex_unlock(>lock);
-}
-
-static struct v3d_bo *v3d_bo_create_struct(struct drm_device *dev,
-  size_t unaligned_size)
-{
-   struct v3d_dev *v3d = to_v3d_dev(dev);
-   struct drm_gem_object *obj;
-   struct v3d_bo *bo;
-   size_t size = roundup(unaligned_size, PAGE_SIZE);
-   int ret;
-
-   if (size == 0)
-   return ERR_PTR(-EINVAL);
-
-   bo = kzalloc(sizeof(*bo), GFP_KERNEL);
-   if (!bo)
-   return ERR_PTR(-ENOMEM);
-   obj = >base;
-
-   INIT_LIST_HEAD(>unref_head);
-   mutex_init(>lock);
-
-   ret = drm_gem_object_init(dev, obj, size);
-   if (ret)
-   goto free_bo;
-
-   spin_lock(>mm_lock);
-   ret = drm_mm_insert_node_generic(>mm, >node,
-obj->size >> PAGE_SHIFT,
-GMP_GRANULARITY >> PAGE_SHIFT, 0, 0);
-   spin_unlock(>mm_lock);
-   if (ret)
-   goto free_obj;
-
-   return bo;
-
-free_obj:
-   drm_gem_object_release(obj);
-free_bo:
-   kfree(bo);
-   return ERR_PTR(ret);
-}
-
-struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file 
*file_priv,
-size_t unaligned_size)
-{
-   struct v3d_dev *v3d = to_v3d_dev(dev);
-   struct drm_gem_object *obj;
-