All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tomasz Stanislawski <t.stanislaws@samsung.com>
To: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org
Cc: airlied@redhat.com, m.szyprowski@samsung.com,
	t.stanislaws@samsung.com, kyungmin.park@samsung.com,
	laurent.pinchart@ideasonboard.com, sumit.semwal@ti.com,
	daeinki@gmail.com, daniel.vetter@ffwll.ch, robdclark@gmail.com,
	pawel@osciak.com, linaro-mm-sig@lists.linaro.org,
	hverkuil@xs4all.nl, remi@remlab.net, subashrp@gmail.com,
	mchehab@redhat.com, zhangfei.gao@gmail.com,
	s.nawrocki@samsung.com, k.debski@samsung.com,
	Sumit Semwal <sumit.semwal@linaro.org>
Subject: [PATCHv9 11/25] v4l: vb2-dma-contig: add support for dma_buf importing
Date: Tue, 02 Oct 2012 16:27:22 +0200	[thread overview]
Message-ID: <1349188056-4886-12-git-send-email-t.stanislaws@samsung.com> (raw)
In-Reply-To: <1349188056-4886-1-git-send-email-t.stanislaws@samsung.com>

From: Sumit Semwal <sumit.semwal@ti.com>

This patch makes changes for adding dma-contig as a dma_buf user. It provides
function implementations for the {attach, detach, map, unmap}_dmabuf()
mem_ops of DMABUF memory type.

Signed-off-by: Sumit Semwal <sumit.semwal@ti.com>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
	[author of the original patch]
Signed-off-by: Tomasz Stanislawski <t.stanislaws@samsung.com>
	[integration with refactored dma-contig allocator]
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/media/video/Kconfig                |    1 +
 drivers/media/video/videobuf2-dma-contig.c |  120 +++++++++++++++++++++++++++-
 2 files changed, 119 insertions(+), 2 deletions(-)

diff --git a/drivers/media/video/Kconfig b/drivers/media/video/Kconfig
index c558d37..ec089da 100644
--- a/drivers/media/video/Kconfig
+++ b/drivers/media/video/Kconfig
@@ -54,6 +54,7 @@ config VIDEOBUF2_MEMOPS
 config VIDEOBUF2_DMA_CONTIG
 	select VIDEOBUF2_CORE
 	select VIDEOBUF2_MEMOPS
+	select DMA_SHARED_BUFFER
 	tristate
 
 config VIDEOBUF2_VMALLOC
diff --git a/drivers/media/video/videobuf2-dma-contig.c b/drivers/media/video/videobuf2-dma-contig.c
index 494a824..a5804cf 100644
--- a/drivers/media/video/videobuf2-dma-contig.c
+++ b/drivers/media/video/videobuf2-dma-contig.c
@@ -10,6 +10,7 @@
  * the Free Software Foundation.
  */
 
+#include <linux/dma-buf.h>
 #include <linux/module.h>
 #include <linux/scatterlist.h>
 #include <linux/sched.h>
@@ -38,6 +39,9 @@ struct vb2_dc_buf {
 
 	/* USERPTR related */
 	struct vm_area_struct		*vma;
+
+	/* DMABUF related */
+	struct dma_buf_attachment	*db_attach;
 };
 
 /*********************************************/
@@ -108,7 +112,8 @@ static void vb2_dc_prepare(void *buf_priv)
 	struct vb2_dc_buf *buf = buf_priv;
 	struct sg_table *sgt = buf->dma_sgt;
 
-	if (!sgt)
+	/* DMABUF exporter will flush the cache for us */
+	if (!sgt || buf->db_attach)
 		return;
 
 	dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
@@ -119,7 +124,8 @@ static void vb2_dc_finish(void *buf_priv)
 	struct vb2_dc_buf *buf = buf_priv;
 	struct sg_table *sgt = buf->dma_sgt;
 
-	if (!sgt)
+	/* DMABUF exporter will flush the cache for us */
+	if (!sgt || buf->db_attach)
 		return;
 
 	dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
@@ -377,6 +383,112 @@ fail_buf:
 }
 
 /*********************************************/
+/*       callbacks for DMABUF buffers        */
+/*********************************************/
+
+static int vb2_dc_map_dmabuf(void *mem_priv)
+{
+	struct vb2_dc_buf *buf = mem_priv;
+	struct sg_table *sgt;
+	unsigned long contig_size;
+
+	if (WARN_ON(!buf->db_attach)) {
+		pr_err("trying to pin a non attached buffer\n");
+		return -EINVAL;
+	}
+
+	if (WARN_ON(buf->dma_sgt)) {
+		pr_err("dmabuf buffer is already pinned\n");
+		return 0;
+	}
+
+	/* get the associated scatterlist for this buffer */
+	sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
+	if (IS_ERR_OR_NULL(sgt)) {
+		pr_err("Error getting dmabuf scatterlist\n");
+		return -EINVAL;
+	}
+
+	/* checking if dmabuf is big enough to store contiguous chunk */
+	contig_size = vb2_dc_get_contiguous_size(sgt);
+	if (contig_size < buf->size) {
+		pr_err("contiguous chunk is too small %lu/%lu b\n",
+			contig_size, buf->size);
+		dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
+		return -EFAULT;
+	}
+
+	buf->dma_addr = sg_dma_address(sgt->sgl);
+	buf->dma_sgt = sgt;
+
+	return 0;
+}
+
+static void vb2_dc_unmap_dmabuf(void *mem_priv)
+{
+	struct vb2_dc_buf *buf = mem_priv;
+	struct sg_table *sgt = buf->dma_sgt;
+
+	if (WARN_ON(!buf->db_attach)) {
+		pr_err("trying to unpin a not attached buffer\n");
+		return;
+	}
+
+	if (WARN_ON(!sgt)) {
+		pr_err("dmabuf buffer is already unpinned\n");
+		return;
+	}
+
+	dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
+
+	buf->dma_addr = 0;
+	buf->dma_sgt = NULL;
+}
+
+static void vb2_dc_detach_dmabuf(void *mem_priv)
+{
+	struct vb2_dc_buf *buf = mem_priv;
+
+	/* if vb2 works correctly you should never detach mapped buffer */
+	if (WARN_ON(buf->dma_addr))
+		vb2_dc_unmap_dmabuf(buf);
+
+	/* detach this attachment */
+	dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
+	kfree(buf);
+}
+
+static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
+	unsigned long size, int write)
+{
+	struct vb2_dc_conf *conf = alloc_ctx;
+	struct vb2_dc_buf *buf;
+	struct dma_buf_attachment *dba;
+
+	if (dbuf->size < size)
+		return ERR_PTR(-EFAULT);
+
+	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
+	if (!buf)
+		return ERR_PTR(-ENOMEM);
+
+	buf->dev = conf->dev;
+	/* create attachment for the dmabuf with the user device */
+	dba = dma_buf_attach(dbuf, buf->dev);
+	if (IS_ERR(dba)) {
+		pr_err("failed to attach dmabuf\n");
+		kfree(buf);
+		return dba;
+	}
+
+	buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
+	buf->size = size;
+	buf->db_attach = dba;
+
+	return buf;
+}
+
+/*********************************************/
 /*       DMA CONTIG exported functions       */
 /*********************************************/
 
@@ -390,6 +502,10 @@ const struct vb2_mem_ops vb2_dma_contig_memops = {
 	.put_userptr	= vb2_dc_put_userptr,
 	.prepare	= vb2_dc_prepare,
 	.finish		= vb2_dc_finish,
+	.map_dmabuf	= vb2_dc_map_dmabuf,
+	.unmap_dmabuf	= vb2_dc_unmap_dmabuf,
+	.attach_dmabuf	= vb2_dc_attach_dmabuf,
+	.detach_dmabuf	= vb2_dc_detach_dmabuf,
 	.num_users	= vb2_dc_num_users,
 };
 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
-- 
1.7.9.5


  parent reply	other threads:[~2012-10-02 14:29 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-02 14:27 [PATCHv9 00/25] Integration of videobuf2 with DMABUF Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 01/25] v4l: Add DMABUF as a memory type Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 02/25] Documentation: media: description of DMABUF importing in V4L2 Tomasz Stanislawski
2012-10-05  8:05   ` Hans Verkuil
2012-10-02 14:27 ` [PATCHv9 03/25] v4l: vb2: add support for shared buffer (dma_buf) Tomasz Stanislawski
2012-10-05  8:17   ` Hans Verkuil
2012-10-02 14:27 ` [PATCHv9 04/25] v4l: vb: remove warnings about MEMORY_DMABUF Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 05/25] v4l: vb2-dma-contig: shorten vb2_dma_contig prefix to vb2_dc Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 06/25] v4l: vb2-dma-contig: remove reference of alloc_ctx from a buffer Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 07/25] v4l: vb2-dma-contig: reorder functions Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 08/25] v4l: vb2-dma-contig: add support for scatterlist in userptr mode Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 09/25] v4l: vb2: add prepare/finish callbacks to allocators Tomasz Stanislawski
2012-10-05  8:22   ` Hans Verkuil
2012-10-02 14:27 ` [PATCHv9 10/25] v4l: vb2-dma-contig: add prepare/finish to dma-contig allocator Tomasz Stanislawski
2012-10-02 14:27 ` Tomasz Stanislawski [this message]
2012-10-02 14:27 ` [PATCHv9 12/25] v4l: vb2-vmalloc: add support for dmabuf importing Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 13/25] v4l: vivi: " Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 14/25] v4l: s5p-tv: mixer: " Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 15/25] v4l: s5p-fimc: " Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 16/25] v4l: vb2-dma-contig: let mmap method to use dma_mmap_coherent call Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 17/25] Documentation: media: description of DMABUF exporting in V4L2 Tomasz Stanislawski
2012-10-05  8:44   ` Hans Verkuil
2012-10-02 14:27 ` [PATCHv9 18/25] v4l: add buffer exporting via dmabuf Tomasz Stanislawski
2012-10-05  8:55   ` Hans Verkuil
2012-10-07 13:38     ` Laurent Pinchart
2012-10-07 14:17       ` Hans Verkuil
2012-10-08  9:40         ` Tomasz Stanislawski
2012-10-08  9:54           ` Hans Verkuil
2012-10-08 10:41             ` Tomasz Stanislawski
2012-10-08 11:15               ` Hans Verkuil
2012-10-02 14:27 ` [PATCHv9 19/25] v4l: vb2: " Tomasz Stanislawski
2012-10-06 12:22   ` Hans Verkuil
2012-10-10 14:05     ` Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 20/25] v4l: vb2-dma-contig: add support for DMABUF exporting Tomasz Stanislawski
2012-10-07 13:38   ` Laurent Pinchart
2012-10-02 14:27 ` [PATCHv9 21/25] v4l: vb2-dma-contig: add reference counting for a device from allocator context Tomasz Stanislawski
2012-10-07 13:38   ` Laurent Pinchart
2012-10-02 14:27 ` [PATCHv9 22/25] v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned Tomasz Stanislawski
2012-10-07 13:38   ` Laurent Pinchart
2012-10-02 14:27 ` [PATCHv9 23/25] v4l: s5p-fimc: support for dmabuf exporting Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 24/25] v4l: s5p-tv: mixer: " Tomasz Stanislawski
2012-10-02 14:27 ` [PATCHv9 25/25] v4l: s5p-mfc: " Tomasz Stanislawski
2012-10-05  9:24 ` [PATCHv9 00/25] Integration of videobuf2 with DMABUF Hans Verkuil
2012-10-07 10:03 ` Hans Verkuil
2012-10-10 10:54 ` Mauro Carvalho Chehab
2012-10-10 11:11   ` [Linaro-mm-sig] " Kyungmin Park
2012-10-10 11:11     ` Kyungmin Park
2012-10-10 13:34   ` Laurent Pinchart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1349188056-4886-12-git-send-email-t.stanislaws@samsung.com \
    --to=t.stanislaws@samsung.com \
    --cc=airlied@redhat.com \
    --cc=daeinki@gmail.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hverkuil@xs4all.nl \
    --cc=k.debski@samsung.com \
    --cc=kyungmin.park@samsung.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-media@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=mchehab@redhat.com \
    --cc=pawel@osciak.com \
    --cc=remi@remlab.net \
    --cc=robdclark@gmail.com \
    --cc=s.nawrocki@samsung.com \
    --cc=subashrp@gmail.com \
    --cc=sumit.semwal@linaro.org \
    --cc=sumit.semwal@ti.com \
    --cc=zhangfei.gao@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.