From patchwork Tue Oct 2 14:27:22 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Stanislawski X-Patchwork-Id: 11932 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 BFC631A52FC for ; Tue, 2 Oct 2012 14:29:21 +0000 (UTC) Received: from mail-ia0-f180.google.com (mail-ia0-f180.google.com [209.85.210.180]) by fiordland.canonical.com (Postfix) with ESMTP id 70615A186EC for ; Tue, 2 Oct 2012 14:29:21 +0000 (UTC) Received: by mail-ia0-f180.google.com with SMTP id f6so796283iag.11 for ; Tue, 02 Oct 2012 07:29:21 -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=Fjn83x89cbYM7qot/5UHRhol9O2Ug7yL/Z2dWRQNGFc=; b=JVDEAuRtkupPWF/C6nCPez3KD7AVQKzB6X7+GQiaoOAjHhQ/UQMSBG/12GKexfF5op zIMdrOiPFoAAnA91au+2PAE00PaKxyMKL4odyeQvcRogmn6Ic0qlBtZfPmUTsbtE0DfI xyDfVyFKfvaNkmH48LNprsQetM2rp+pFo8mcZ+Nsi2lAUHLsY6adxrE/b0+SunbF5dbv RyVxXBmtjMUAUZ+bNX6/+VxqEQvYVPsu0MLt/paZrd3MrcamqRBLSXNOd+vPrgitassc ohf7/Idvf2EsUR33iQSreY89pw+dfhe7U7xEHDvzNnSJUIy4vAI0P+AyuJQ3pgwf7OTS 17Yw== Received: by 10.50.184.129 with SMTP id eu1mr9096405igc.0.1349188161184; Tue, 02 Oct 2012 07:29:21 -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.184.232 with SMTP id ex8csp84983igc; Tue, 2 Oct 2012 07:29:20 -0700 (PDT) Received: by 10.204.4.149 with SMTP id 21mr6656667bkr.122.1349188159418; Tue, 02 Oct 2012 07:29:19 -0700 (PDT) Received: from mombin.canonical.com (mombin.canonical.com. [91.189.95.16]) by mx.google.com with ESMTP id go18si1701572bkc.69.2012.10.02.07.29.17; Tue, 02 Oct 2012 07:29:19 -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 1TJ3TI-0004gs-UT; Tue, 02 Oct 2012 14:29:17 +0000 Received: from mailout3.samsung.com ([203.254.224.33]) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1TJ3TG-0004gD-5j for linaro-mm-sig@lists.linaro.org; Tue, 02 Oct 2012 14:29:14 +0000 Received: from epcpsbgm2.samsung.com (epcpsbgm2 [203.254.230.27]) by mailout3.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MB900FNZS8A6O90@mailout3.samsung.com> for linaro-mm-sig@lists.linaro.org; Tue, 02 Oct 2012 23:29:13 +0900 (KST) X-AuditID: cbfee61b-b7f2b6d000000f14-02-506afa38459c Received: from epmmp2 ( [203.254.227.17]) by epcpsbgm2.samsung.com (EPCPMTA) with SMTP id 96.91.03860.83AFA605; Tue, 02 Oct 2012 23:29:13 +0900 (KST) Received: from mcdsrvbld02.digital.local ([106.116.37.23]) by mmp2.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MB9005A7S65K790@mmp2.samsung.com> for linaro-mm-sig@lists.linaro.org; Tue, 02 Oct 2012 23:29:12 +0900 (KST) From: Tomasz Stanislawski To: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Date: Tue, 02 Oct 2012 16:27:22 +0200 Message-id: <1349188056-4886-12-git-send-email-t.stanislaws@samsung.com> X-Mailer: git-send-email 1.7.10 In-reply-to: <1349188056-4886-1-git-send-email-t.stanislaws@samsung.com> References: <1349188056-4886-1-git-send-email-t.stanislaws@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrCJMWRmVeSWpSXmKPExsVy+t9jQV3LX1kBBs0bmSy+XHnI5MDocfvf Y+YAxigum5TUnMyy1CJ9uwSujIa1h1kL7ulU3N67iL2B8YVKFyMnh4SAicTpQzdZIWwxiQv3 1rN1MXJxCAlMZ5R4NPc9O4Szlkni4/sljCBVbEAdx5Z8BrNFBBwkTt+dxQxSxCxwjVni+/TP zCAJYQF/iat/3jOB2CwCqhKzepexg9i8Ah4SE379YYJYJy/x9H4fG4jNCRT/+eQCC4gtJOAu sWd5E9sERt4FjAyrGEVTC5ILipPSc430ihNzi0vz0vWS83M3MYL9/kx6B+OqBotDjAIcjEo8 vLtOZgYIsSaWFVfmHmKU4GBWEuG1vp8VIMSbklhZlVqUH19UmpNafIhRmoNFSZy32SMlQEgg PbEkNTs1tSC1CCbLxMEp1cBokXzL6t7kV5bLXh3a4NZjNe3BYTuFfIMD3ib/Aw/GHZG06rac 4t5yKbLm3L8INf4r2bHRj4WnrA7y+CPj2mYS09Nat7vyde/cqH+JtUq+TxhFz4ff87h10dA8 +GDDO4HqpXvkI+QrFX+o7f6yZNtiOS0npU3sn1lZXsqrBMdulZl5TnpjPr8SS3FGoqEWc1Fx IgCl9bRy9wEAAA== 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] [PATCHv9 11/25] 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: ALoCoQkOHxAl7+aHPN2/VaeKuqkdA0Ur14unnnOv2AQ2lq5ltMbc/vKr0sNy555vzGtrOkkIRlO1 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/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 #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);