Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4253302pxb; Tue, 2 Mar 2021 10:16:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgL6+4Oxy+8v0vsrws+TnvhC5YcgoBZiWsvY5sX4Mth6DG7MgjVp/pHM5JqmDQKz6D5SEj X-Received: by 2002:a17:906:9515:: with SMTP id u21mr22401530ejx.86.1614708991185; Tue, 02 Mar 2021 10:16:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614708991; cv=none; d=google.com; s=arc-20160816; b=mbUnMtBrKrWaMIjazFdavljgGkHtLC1j/Vz7X26T0iZ7WWSQ0cFLoFETwu970368xM yqB5TfPWpjR0Dxw/h0VMyRjMNKukB+Obwkn0otQitApTDhgJolPdItUX/znJp/yV+4zW dj1PSju6F0VrYcMVqXazLHXVcSZI7wHAIis8EaU7BVRDQ4RxsbNzTNwlp88IhM7U2GlO fb+LPbELEBWhOViMK0cd/I0AZ0ObcZ6yCS+AHMYd8WQT2/THf9iVZeWvIliQc1HMTsnq ir6rbfWWTWcjjVxB8r2SQ1JTwMOP5l3SkEnQJBIEpuhvCOSW7wRoLPHG3Nr6JLQPDYz1 Srtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3ehy3NtZARWKqntzrYPlK6hSKlfy8OUUEgCoOD4fHSE=; b=OMxRRqeSPAckVvCm6hOP1HyDwQqMphIUMCahqg40NNnFea2xs5nLB/f2LxyZDM7WjI D+usxXaAaD5EL+HcsQIxQ0xMz1oP8XLylEf1dxVwiNw4SbeBizx/rHvBkfJnxWv4SlpV rfdJdcQHU7xUx9PWGlVo7ZvpFDhZC7ez0NtrPknSvMUDbB4RK/PRK4z00mDczAy0oELV Jr9OOrQtQr6q+s2glKp8/dOJZqadglXXk81a6HCAm5P/l0PQKwtOTnWw3X1GnbbnGGwN p9aw0dQz/gCRaPuUKWyImdBpQG5Nag5hbqTCSL6FPYWUimGz53D8cjT7RIA/vcmjO2Un nVWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HWdXl+wG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w5si14465428edv.61.2021.03.02.10.16.07; Tue, 02 Mar 2021 10:16:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HWdXl+wG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1576139AbhCBEYR (ORCPT + 99 others); Mon, 1 Mar 2021 23:24:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377656AbhCBAsQ (ORCPT ); Mon, 1 Mar 2021 19:48:16 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF271C061222 for ; Mon, 1 Mar 2021 16:47:04 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id 192so5684416pfv.0 for ; Mon, 01 Mar 2021 16:47:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3ehy3NtZARWKqntzrYPlK6hSKlfy8OUUEgCoOD4fHSE=; b=HWdXl+wGHfyAD0jSdQV4KKDcE36igEeT1w0wjKhgKk5SIvItzRKr1GujotMl+J0e82 PGhydmIoAsKJIeUM7230UpY9cZIH1i/4+9rm/Gu122fRJQd1Rl4w23h3kAWgCovzc5bY SSkOGozVvcJT3+9oBThl0XJPf6O8tvBf9vwUU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3ehy3NtZARWKqntzrYPlK6hSKlfy8OUUEgCoOD4fHSE=; b=IBLH1w4Zn4QOCbkplWTJoSFzkqJ3fFvLSxUXUaR70r0uDevi0KPYCXI4A36L/JqgTw WfroIqPLrrVf/W+pyClyYMIsEQzcgflKygC1tp02NjGf2GAXAsXRXfsj9uBuD+J7Y6Gd lFX5G0r4bXG1FxKVezR3AygeiFcd9VC9AcrVUY2atrRRnMdtv9ScCrF1UDnYOPsdo4wE iL4Ot2F9zDkrRpOR8+Svsro2M7KVqGifEFxhmIZ5+qFJipO1IMY7a6Tfz1uAaYbmhxsU 7/cSsb8OBdgbs6p2eAENgL2fzR3WqsLXXpM/nbcvygUMz/U+NjQtlMgPLdxlDHIenMcJ +KNA== X-Gm-Message-State: AOAM532sbaxbSZ9PkUwY/Cb5wDMeakKRLE1Z9DkP6D0vPvVr7lypDgSD buoDPnQRjYLeLS4hHBzPYGMiExKFr6NCcQ== X-Received: by 2002:a65:4c0b:: with SMTP id u11mr16102700pgq.409.1614646024411; Mon, 01 Mar 2021 16:47:04 -0800 (PST) Received: from senozhatsky.flets-east.jp ([2409:10:2e40:5100:d5d7:1a61:2cdf:273c]) by smtp.gmail.com with ESMTPSA id b14sm678881pji.14.2021.03.01.16.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Mar 2021 16:47:04 -0800 (PST) From: Sergey Senozhatsky To: Tomasz Figa , Hans Verkuil Cc: Christoph Hellwig , Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky Subject: [PATCH 8/8] videobuf2: handle non-contiguous DMA allocations Date: Tue, 2 Mar 2021 09:46:24 +0900 Message-Id: <20210302004624.31294-9-senozhatsky@chromium.org> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog In-Reply-To: <20210302004624.31294-1-senozhatsky@chromium.org> References: <20210302004624.31294-1-senozhatsky@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds support for new noncontiguous DMA API, which requires allocators to have two execution branches: one for the current API, and one for the new one. Signed-off-by: Sergey Senozhatsky [hch: untested conversion to the ne API] Signed-off-by: Christoph Hellwig --- .../common/videobuf2/videobuf2-dma-contig.c | 141 +++++++++++++++--- 1 file changed, 117 insertions(+), 24 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-dma-contig.c b/drivers/media/common/videobuf2/videobuf2-dma-contig.c index 1e218bc440c6..d6a9f7b682f3 100644 --- a/drivers/media/common/videobuf2/videobuf2-dma-contig.c +++ b/drivers/media/common/videobuf2/videobuf2-dma-contig.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -42,8 +43,14 @@ struct vb2_dc_buf { struct dma_buf_attachment *db_attach; struct vb2_buffer *vb; + unsigned int non_coherent_mem:1; }; +static bool vb2_dc_is_coherent(struct vb2_dc_buf *buf) +{ + return !buf->non_coherent_mem; +} + /*********************************************/ /* scatterlist table functions */ /*********************************************/ @@ -78,12 +85,21 @@ static void *vb2_dc_cookie(struct vb2_buffer *vb, void *buf_priv) static void *vb2_dc_vaddr(struct vb2_buffer *vb, void *buf_priv) { struct vb2_dc_buf *buf = buf_priv; - struct dma_buf_map map; - int ret; - if (!buf->vaddr && buf->db_attach) { - ret = dma_buf_vmap(buf->db_attach->dmabuf, &map); - buf->vaddr = ret ? NULL : map.vaddr; + if (buf->vaddr) + return buf->vaddr; + + if (buf->db_attach) { + struct dma_buf_map map; + + if (!dma_buf_vmap(buf->db_attach->dmabuf, &map)) + buf->vaddr = map.vaddr; + } + + if (!vb2_dc_is_coherent(buf)) { + buf->vaddr = dma_vmap_noncontiguous(buf->dev, + buf->size, + buf->dma_sgt); } return buf->vaddr; @@ -101,13 +117,26 @@ static void vb2_dc_prepare(void *buf_priv) struct vb2_dc_buf *buf = buf_priv; struct sg_table *sgt = buf->dma_sgt; + /* This takes care of DMABUF and user-enforced cache sync hint */ if (buf->vb->skip_cache_sync_on_prepare) return; + /* + * Coherent MMAP buffers do not need to be synced, unlike coherent + * USERPTR and non-coherent MMAP buffers. + */ + if (buf->vb->memory == V4L2_MEMORY_MMAP && vb2_dc_is_coherent(buf)) + return; + if (!sgt) return; + /* For both USERPTR and non-coherent MMAP */ dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir); + + /* Non-coherrent MMAP only */ + if (!vb2_dc_is_coherent(buf) && buf->vaddr) + flush_kernel_vmap_range(buf->vaddr, buf->size); } static void vb2_dc_finish(void *buf_priv) @@ -115,19 +144,46 @@ static void vb2_dc_finish(void *buf_priv) struct vb2_dc_buf *buf = buf_priv; struct sg_table *sgt = buf->dma_sgt; + /* This takes care of DMABUF and user-enforced cache sync hint */ if (buf->vb->skip_cache_sync_on_finish) return; + /* + * Coherent MMAP buffers do not need to be synced, unlike coherent + * USERPTR and non-coherent MMAP buffers. + */ + if (buf->vb->memory == V4L2_MEMORY_MMAP && vb2_dc_is_coherent(buf)) + return; + if (!sgt) return; + /* For both USERPTR and non-coherent MMAP */ dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir); + + /* Non-coherrent MMAP only */ + if (!vb2_dc_is_coherent(buf) && buf->vaddr) + invalidate_kernel_vmap_range(buf->vaddr, buf->size); } /*********************************************/ /* callbacks for MMAP buffers */ /*********************************************/ +static void __vb2_dc_put(struct vb2_dc_buf *buf) +{ + if (vb2_dc_is_coherent(buf)) { + dma_free_attrs(buf->dev, buf->size, buf->cookie, + buf->dma_addr, buf->attrs); + return; + } + + if (buf->vaddr) + dma_vunmap_noncontiguous(buf->dev, buf->vaddr); + dma_free_noncontiguous(buf->dev, buf->size, + buf->dma_sgt, buf->dma_addr); +} + static void vb2_dc_put(void *buf_priv) { struct vb2_dc_buf *buf = buf_priv; @@ -139,17 +195,47 @@ static void vb2_dc_put(void *buf_priv) sg_free_table(buf->sgt_base); kfree(buf->sgt_base); } - dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr, - buf->attrs); + __vb2_dc_put(buf); put_device(buf->dev); kfree(buf); } +static int vb2_dc_alloc_coherent(struct vb2_dc_buf *buf) +{ + struct vb2_queue *q = buf->vb->vb2_queue; + + buf->cookie = dma_alloc_attrs(buf->dev, + buf->size, + &buf->dma_addr, + GFP_KERNEL | q->gfp_flags, + buf->attrs); + if (!buf->cookie) + return -ENOMEM; + if ((q->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) + buf->vaddr = buf->cookie; + return 0; +} + +static int vb2_dc_alloc_non_coherent(struct vb2_dc_buf *buf) +{ + struct vb2_queue *q = buf->vb->vb2_queue; + + buf->dma_sgt = dma_alloc_noncontiguous(buf->dev, + buf->size, + buf->dma_dir, + GFP_KERNEL | q->gfp_flags, + buf->attrs); + if (!buf->dma_sgt) + return -ENOMEM; + return 0; +} + static void *vb2_dc_alloc(struct vb2_buffer *vb, struct device *dev, unsigned long size) { struct vb2_dc_buf *buf; + int ret; if (WARN_ON(!dev)) return ERR_PTR(-EINVAL); @@ -159,27 +245,28 @@ static void *vb2_dc_alloc(struct vb2_buffer *vb, return ERR_PTR(-ENOMEM); buf->attrs = vb->vb2_queue->dma_attrs; - buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr, - GFP_KERNEL | vb->vb2_queue->gfp_flags, - buf->attrs); - if (!buf->cookie) { - dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size); - kfree(buf); - return ERR_PTR(-ENOMEM); - } - - if ((buf->attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) - buf->vaddr = buf->cookie; + buf->dma_dir = vb->vb2_queue->dma_dir; + buf->vb = vb; + buf->non_coherent_mem = vb->vb2_queue->non_coherent_mem; + buf->size = size; /* Prevent the device from being released while the buffer is used */ buf->dev = get_device(dev); - buf->size = size; - buf->dma_dir = vb->vb2_queue->dma_dir; + + if (vb2_dc_is_coherent(buf)) + ret = vb2_dc_alloc_coherent(buf); + else + ret = vb2_dc_alloc_non_coherent(buf); + + if (ret) { + dev_err(dev, "dma alloc of size %ld failed\n", size); + kfree(buf); + return ERR_PTR(-ENOMEM); + } buf->handler.refcount = &buf->refcount; buf->handler.put = vb2_dc_put; buf->handler.arg = buf; - buf->vb = vb; refcount_set(&buf->refcount, 1); @@ -196,9 +283,12 @@ static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma) return -EINVAL; } - ret = dma_mmap_attrs(buf->dev, vma, buf->cookie, - buf->dma_addr, buf->size, buf->attrs); - + if (vb2_dc_is_coherent(buf)) + ret = dma_mmap_attrs(buf->dev, vma, buf->cookie, buf->dma_addr, + buf->size, buf->attrs); + else + ret = dma_mmap_noncontiguous(buf->dev, vma, buf->size, + buf->dma_sgt); if (ret) { pr_err("Remapping memory failed, error: %d\n", ret); return ret; @@ -390,6 +480,9 @@ static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf) int ret; struct sg_table *sgt; + if (!vb2_dc_is_coherent(buf)) + return buf->dma_sgt; + sgt = kmalloc(sizeof(*sgt), GFP_KERNEL); if (!sgt) { dev_err(buf->dev, "failed to alloc sg table\n"); -- 2.30.1.766.gb4fecdf3b7-goog