From patchwork Wed Oct 10 14:46:30 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Stanislawski X-Patchwork-Id: 12120 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 5E10023EFB for ; Wed, 10 Oct 2012 14:55:29 +0000 (UTC) Received: from mail-ie0-f180.google.com (mail-ie0-f180.google.com [209.85.223.180]) by fiordland.canonical.com (Postfix) with ESMTP id D9E34A18890 for ; Wed, 10 Oct 2012 14:55:28 +0000 (UTC) Received: by mail-ie0-f180.google.com with SMTP id e10so1007385iej.11 for ; Wed, 10 Oct 2012 07:55:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:x-auditid :from:to:date:message-id:x-mailer:in-reply-to:references :x-brightmail-tracker:cc:subject:x-beenthere:x-mailman-version :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:sender:errors-to:x-gm-message-state; bh=dLBl6ARktvGgN1QcVfa+r5GzE1IZTMooU9nNK0qgriU=; b=TeHJvtKsLedxs7nMy3AC/aSgGvp+Gu5NaBRKlbQyg0zncWoYNKA6MaEgVRz/cfZzy9 Hdy+FjylGOOIH1t/29LsR+WkJmfJTFcQLRqLRutFUF303bgyoNmjwZSmV6B+AVdJc6Pe xYJbZ4wpy9O5ryOKjNR5NNpqHyjb2SvBTKBtAL2QTZLyQ3AWVWibkR9PwKyJP9SRETee MydUxFDSwZlU1bp7hx0X/s9xsvvVh5gygQWhCe4VWPBx/pEZe5Y9YjulESLsJ6xHwg6K zqu3sSKXhMmTaYE82ggJ6XyMUfeWmdG2s9xDW4gvgR/uAK6NSK7NmSjkG13SQbds/tJo vjAg== Received: by 10.50.91.195 with SMTP id cg3mr5483075igb.57.1349880928315; Wed, 10 Oct 2012 07:55:28 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.50.67.148 with SMTP id n20csp236468igt; Wed, 10 Oct 2012 07:55:27 -0700 (PDT) Received: by 10.14.224.135 with SMTP id x7mr33398281eep.34.1349880926876; Wed, 10 Oct 2012 07:55:26 -0700 (PDT) Received: from mombin.canonical.com (mombin.canonical.com. [91.189.95.16]) by mx.google.com with ESMTP id f43si1064408eem.66.2012.10.10.07.55.25; Wed, 10 Oct 2012 07:55:26 -0700 (PDT) Received-SPF: neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) client-ip=91.189.95.16; Authentication-Results: mx.google.com; spf=neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) smtp.mail=linaro-mm-sig-bounces@lists.linaro.org Received: from localhost ([127.0.0.1] helo=mombin.canonical.com) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1TLxgx-0001oX-JG; Wed, 10 Oct 2012 14:55:23 +0000 Received: from mailout1.samsung.com ([203.254.224.24]) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1TLxgv-0001oS-LV for linaro-mm-sig@lists.linaro.org; Wed, 10 Oct 2012 14:55:22 +0000 Received: from epcpsbgm1.samsung.com (epcpsbgm1 [203.254.230.26]) by mailout1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MBO00M3WMRVT830@mailout1.samsung.com> for linaro-mm-sig@lists.linaro.org; Wed, 10 Oct 2012 23:55:20 +0900 (KST) X-AuditID: cbfee61a-b7f726d000000ec7-cd-50758c57a79d Received: from epmmp1.local.host ( [203.254.227.16]) by epcpsbgm1.samsung.com (EPCPMTA) with SMTP id 9D.E9.03783.75C85705; Wed, 10 Oct 2012 23:55:20 +0900 (KST) Received: from mcdsrvbld02.digital.local ([106.116.37.23]) by mmp1.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MBO002YDME0EC70@mmp1.samsung.com> for linaro-mm-sig@lists.linaro.org; Wed, 10 Oct 2012 23:55:19 +0900 (KST) From: Tomasz Stanislawski To: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Date: Wed, 10 Oct 2012 16:46:30 +0200 Message-id: <1349880405-26049-12-git-send-email-t.stanislaws@samsung.com> X-Mailer: git-send-email 1.7.10 In-reply-to: <1349880405-26049-1-git-send-email-t.stanislaws@samsung.com> References: <1349880405-26049-1-git-send-email-t.stanislaws@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrEJMWRmVeSWpSXmKPExsVy+t9jAd2IntIAg/3LWSy+XHnI5MDocfvf Y+YAxigum5TUnMyy1CJ9uwSujK9LdzEXfNGpuN09h72B8Z9KFyMnh4SAiUTDtH9MELaYxIV7 69lAbCGBRYwSJ39FdTFyAdlrmSTO7dsOVsQG1HBsyWdGEFtEwEHi9N1ZzCBFzALXmCW+T//M DJIQFgiQmDXrGDuIzSKgKtG1dAVrFyMHB6+Ap8Tp/X4Qy+Qlnt7vA1vGCRS+MG86M8RiD4n/ 6zczTWDkXcDIsIpRNLUguaA4KT3XUK84Mbe4NC9dLzk/dxMj2OfPpHYwrmywOMQowMGoxMOb EVUSIMSaWFZcmXuIUYKDWUmE19SnNECINyWxsiq1KD++qDQntfgQozQHi5I4b7NHSoCQQHpi SWp2ampBahFMlomDU6qB8UzROXWtOVuslh1i6oyqev99lkm8nsn9PJutuiVnD7n1z+4+qlRd MsPF+xAT7zWZ0MAjqkrLqs+umnRQ3pz3ydJGTevQwM3vT+2ZdnYdn+3dAua/0mpp11UF3d1j vuZtvrnlUfoTLU13y4f1p0VWebTwr5xwMmOyvMvrV1kTLmV4HZnt5qrJqsRSnJFoqMVcVJwI ADZRicj1AQAA Cc: k.debski@samsung.com, s.nawrocki@samsung.com, pawel@osciak.com, sumit.semwal@ti.com, mchehab@redhat.com, robdclark@gmail.com, linaro-mm-sig@lists.linaro.org, kyungmin.park@samsung.com, laurent.pinchart@ideasonboard.com, airlied@redhat.com, remi@remlab.net Subject: [Linaro-mm-sig] [PATCHv10 11/26] v4l: vb2-dma-contig: add support for dma_buf importing X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Unified memory management interest group." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linaro-mm-sig-bounces@lists.linaro.org Errors-To: linaro-mm-sig-bounces@lists.linaro.org X-Gm-Message-State: ALoCoQm1KgWSb+7xJLBD15AVf27f8m372XTLRh835qcf26S2Bz0M/ryA45zBmDd+jE4/vr2zYiJi From: Sumit Semwal 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 Signed-off-by: Sumit Semwal [author of the original patch] Signed-off-by: Tomasz Stanislawski [integration with refactored dma-contig allocator] Acked-by: Laurent Pinchart --- drivers/media/v4l2-core/Kconfig | 1 + drivers/media/v4l2-core/videobuf2-dma-contig.c | 120 +++++++++++++++++++++++- 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/drivers/media/v4l2-core/Kconfig b/drivers/media/v4l2-core/Kconfig index 2e787cc..e30583b 100644 --- a/drivers/media/v4l2-core/Kconfig +++ b/drivers/media/v4l2-core/Kconfig @@ -69,6 +69,7 @@ config VIDEOBUF2_DMA_CONTIG tristate select VIDEOBUF2_CORE select VIDEOBUF2_MEMOPS + select DMA_SHARED_BUFFER config VIDEOBUF2_VMALLOC tristate diff --git a/drivers/media/v4l2-core/videobuf2-dma-contig.c b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 494a824..a5804cf 100644 --- a/drivers/media/v4l2-core/videobuf2-dma-contig.c +++ b/drivers/media/v4l2-core/videobuf2-dma-contig.c @@ -10,6 +10,7 @@ * the Free Software Foundation. */ +#include #include #include #include @@ -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);