diff mbox

[RESEND] drm/rockchip: import dma_buf to gem

Message ID 1447146717-11423-1-git-send-email-mark.yao@rock-chips.com (mailing list archive)
State New, archived
Headers show

Commit Message

yao mark Nov. 10, 2015, 9:11 a.m. UTC
We want to display a buffer allocated by other driver, need import
the buffer to gem.

Signed-off-by: Mark Yao <mark.yao@rock-chips.com>
---
Changes in resend:
- rebase to this patch to newest origin/drm-next 4.3-rc5
Changes in v2:
Adviced by Russell King
- check sgt-nents == 1 when get iova from scatterlist
- remove unused dma_map_sg/dma_unmap_sg, dma_buf_map_attachment->map_dma_buf
  should do the dma_map

 drivers/gpu/drm/rockchip/rockchip_drm_drv.c |    1 +
 drivers/gpu/drm/rockchip/rockchip_drm_gem.c |   40 +++++++++++++++++++++++++--
 drivers/gpu/drm/rockchip/rockchip_drm_gem.h |    5 +++-
 3 files changed, 42 insertions(+), 4 deletions(-)

Comments

Daniel Vetter Nov. 16, 2015, 4:25 p.m. UTC | #1
On Tue, Nov 10, 2015 at 05:11:57PM +0800, Mark Yao wrote:
> We want to display a buffer allocated by other driver, need import
> the buffer to gem.

Does this work with some open-source driver/userspace or is this for the
proprietary stack only? A bit a grey area I guess, but if it's only for
the proprietary stack I think it shouldn't land in upstream - making sure
it works with open-source (e.g. Xorg prime) first would be good.
-Daniel

> 
> Signed-off-by: Mark Yao <mark.yao@rock-chips.com>
> ---
> Changes in resend:
> - rebase to this patch to newest origin/drm-next 4.3-rc5
> Changes in v2:
> Adviced by Russell King
> - check sgt-nents == 1 when get iova from scatterlist
> - remove unused dma_map_sg/dma_unmap_sg, dma_buf_map_attachment->map_dma_buf
>   should do the dma_map
> 
>  drivers/gpu/drm/rockchip/rockchip_drm_drv.c |    1 +
>  drivers/gpu/drm/rockchip/rockchip_drm_gem.c |   40 +++++++++++++++++++++++++--
>  drivers/gpu/drm/rockchip/rockchip_drm_gem.h |    5 +++-
>  3 files changed, 42 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
> index f22e1e1..ea5b19e 100644
> --- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
> +++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
> @@ -290,6 +290,7 @@ static struct drm_driver rockchip_drm_driver = {
>  	.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_import_sg_table = rockchip_gem_prime_import_sg_table,
>  	.gem_prime_export	= drm_gem_prime_export,
>  	.gem_prime_get_sg_table	= rockchip_gem_prime_get_sg_table,
>  	.gem_prime_vmap		= rockchip_gem_prime_vmap,
> diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c
> index 8caea0a..1c94a30 100644
> --- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c
> +++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c
> @@ -18,6 +18,7 @@
>  #include <drm/drm_vma_manager.h>
>  
>  #include <linux/dma-attrs.h>
> +#include <linux/dma-buf.h>
>  
>  #include "rockchip_drm_drv.h"
>  #include "rockchip_drm_gem.h"
> @@ -103,6 +104,36 @@ int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
>  	return rockchip_drm_gem_object_mmap(obj, vma);
>  }
>  
> +struct drm_gem_object *
> +rockchip_gem_prime_import_sg_table(struct drm_device *drm,
> +				   struct dma_buf_attachment *attach,
> +				   struct sg_table *sgt)
> +{
> +	struct rockchip_gem_object *rk_obj;
> +	struct drm_gem_object *obj;
> +
> +	/*
> +	 * Todo: only support continuous buffer now, plan to found a method
> +	 * to import non continuous with iommu.
> +	 */
> +	if (sgt->nents != 1)
> +		return ERR_PTR(-EINVAL);
> +
> +	rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
> +	if (!rk_obj)
> +		return ERR_PTR(-ENOMEM);
> +
> +	obj = &rk_obj->base;
> +
> +	drm_gem_private_object_init(drm, obj, attach->dmabuf->size);
> +
> +	rk_obj->dma_addr = sg_dma_address(sgt->sgl);
> +	rk_obj->sgt = sgt;
> +	obj->size = sg_dma_len(sgt->sgl);
> +
> +	return obj;
> +}
> +
>  struct rockchip_gem_object *
>  	rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
>  				   bool alloc_kmap)
> @@ -140,11 +171,14 @@ void rockchip_gem_free_object(struct drm_gem_object *obj)
>  {
>  	struct rockchip_gem_object *rk_obj;
>  
> -	drm_gem_free_mmap_offset(obj);
> -
>  	rk_obj = to_rockchip_obj(obj);
>  
> -	rockchip_gem_free_buf(rk_obj);
> +	if (obj->import_attach) {
> +		drm_prime_gem_destroy(obj, rk_obj->sgt);
> +	} else {
> +		drm_gem_free_mmap_offset(obj);
> +		rockchip_gem_free_buf(rk_obj);
> +	}
>  
>  	kfree(rk_obj);
>  }
> diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h
> index ad22618..9fd58e3 100644
> --- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h
> +++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h
> @@ -21,6 +21,7 @@ struct rockchip_gem_object {
>  	struct drm_gem_object base;
>  	unsigned int flags;
>  
> +	struct sg_table *sgt;
>  	void *kvaddr;
>  	dma_addr_t dma_addr;
>  	struct dma_attrs dma_attrs;
> @@ -28,8 +29,10 @@ struct rockchip_gem_object {
>  
>  struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj);
>  struct drm_gem_object *
> -rockchip_gem_prime_import_sg_table(struct drm_device *dev, size_t size,
> +rockchip_gem_prime_import_sg_table(struct drm_device *drm,
> +				   struct dma_buf_attachment *attach,
>  				   struct sg_table *sgt);
> +
>  void *rockchip_gem_prime_vmap(struct drm_gem_object *obj);
>  void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
>  
> -- 
> 1.7.9.5
> 
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel
yao mark Nov. 17, 2015, 8:01 a.m. UTC | #2
On 2015?11?17? 00:25, Daniel Vetter wrote:
> On Tue, Nov 10, 2015 at 05:11:57PM +0800, Mark Yao wrote:
>> >We want to display a buffer allocated by other driver, need import
>> >the buffer to gem.
> Does this work with some open-source driver/userspace or is this for the
> proprietary stack only? A bit a grey area I guess, but if it's only for
> the proprietary stack I think it shouldn't land in upstream - making sure
> it works with open-source (e.g. Xorg prime) first would be good.
> -Daniel
>
Hi Daniel
     It works on open-source with following stack:
         ion -> dma_fd  -> prime fd to handle -> drm -> vop

     ion is the android ION memory allocator, its driver at 
upstream-kernel/drivers/staging/android/ion.

     We have some application use ion on android, and want to port to 
Xorg side.

Thanks
diff mbox

Patch

diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
index f22e1e1..ea5b19e 100644
--- a/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
+++ b/drivers/gpu/drm/rockchip/rockchip_drm_drv.c
@@ -290,6 +290,7 @@  static struct drm_driver rockchip_drm_driver = {
 	.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_import_sg_table = rockchip_gem_prime_import_sg_table,
 	.gem_prime_export	= drm_gem_prime_export,
 	.gem_prime_get_sg_table	= rockchip_gem_prime_get_sg_table,
 	.gem_prime_vmap		= rockchip_gem_prime_vmap,
diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c
index 8caea0a..1c94a30 100644
--- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.c
+++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.c
@@ -18,6 +18,7 @@ 
 #include <drm/drm_vma_manager.h>
 
 #include <linux/dma-attrs.h>
+#include <linux/dma-buf.h>
 
 #include "rockchip_drm_drv.h"
 #include "rockchip_drm_gem.h"
@@ -103,6 +104,36 @@  int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 	return rockchip_drm_gem_object_mmap(obj, vma);
 }
 
+struct drm_gem_object *
+rockchip_gem_prime_import_sg_table(struct drm_device *drm,
+				   struct dma_buf_attachment *attach,
+				   struct sg_table *sgt)
+{
+	struct rockchip_gem_object *rk_obj;
+	struct drm_gem_object *obj;
+
+	/*
+	 * Todo: only support continuous buffer now, plan to found a method
+	 * to import non continuous with iommu.
+	 */
+	if (sgt->nents != 1)
+		return ERR_PTR(-EINVAL);
+
+	rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
+	if (!rk_obj)
+		return ERR_PTR(-ENOMEM);
+
+	obj = &rk_obj->base;
+
+	drm_gem_private_object_init(drm, obj, attach->dmabuf->size);
+
+	rk_obj->dma_addr = sg_dma_address(sgt->sgl);
+	rk_obj->sgt = sgt;
+	obj->size = sg_dma_len(sgt->sgl);
+
+	return obj;
+}
+
 struct rockchip_gem_object *
 	rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
 				   bool alloc_kmap)
@@ -140,11 +171,14 @@  void rockchip_gem_free_object(struct drm_gem_object *obj)
 {
 	struct rockchip_gem_object *rk_obj;
 
-	drm_gem_free_mmap_offset(obj);
-
 	rk_obj = to_rockchip_obj(obj);
 
-	rockchip_gem_free_buf(rk_obj);
+	if (obj->import_attach) {
+		drm_prime_gem_destroy(obj, rk_obj->sgt);
+	} else {
+		drm_gem_free_mmap_offset(obj);
+		rockchip_gem_free_buf(rk_obj);
+	}
 
 	kfree(rk_obj);
 }
diff --git a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h
index ad22618..9fd58e3 100644
--- a/drivers/gpu/drm/rockchip/rockchip_drm_gem.h
+++ b/drivers/gpu/drm/rockchip/rockchip_drm_gem.h
@@ -21,6 +21,7 @@  struct rockchip_gem_object {
 	struct drm_gem_object base;
 	unsigned int flags;
 
+	struct sg_table *sgt;
 	void *kvaddr;
 	dma_addr_t dma_addr;
 	struct dma_attrs dma_attrs;
@@ -28,8 +29,10 @@  struct rockchip_gem_object {
 
 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj);
 struct drm_gem_object *
-rockchip_gem_prime_import_sg_table(struct drm_device *dev, size_t size,
+rockchip_gem_prime_import_sg_table(struct drm_device *drm,
+				   struct dma_buf_attachment *attach,
 				   struct sg_table *sgt);
+
 void *rockchip_gem_prime_vmap(struct drm_gem_object *obj);
 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);