Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752778AbdHUJ31 (ORCPT ); Mon, 21 Aug 2017 05:29:27 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:41369 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751069AbdHUJ3Z (ORCPT ); Mon, 21 Aug 2017 05:29:25 -0400 X-AuditID: cbfec7f5-f79d06d0000031c7-d7-599aa7f10d8a Subject: Re: [PATCH 1/7 v2] media: vb2: add bidirectional flag in vb2_queue To: Stanimir Varbanov , Mauro Carvalho Chehab , Hans Verkuil Cc: Pawel Osciak , Kyungmin Park , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org From: Marek Szyprowski Message-id: Date: Mon, 21 Aug 2017 11:29:19 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-version: 1.0 In-reply-to: <20170821090909.32614-1-stanimir.varbanov@linaro.org> Content-type: text/plain; charset="utf-8"; format="flowed" Content-transfer-encoding: 7bit Content-language: en-US X-Brightmail-Tracker: H4sIAAAAAAAAA02Sa0hTYRjHebez7Wy4OE7LB7tokwitLMvgYCUaRoM+1FclnDMP3rZpmzPt Q5huokPyliirRCVNppjMuWqZl1lKmkuN7EKKYMxLztsYqYm07Sjs2+95nv9z+b+8OFNgZAXi 6fIcSiGXSIVsHmYa2vp8Zv2FLv7c9scwcqTaxiDHCpc5ZGXfGIf8Yn7KJss6u1lki2GHQT62 b3FIdfF7RgwuMuhL2aJfUz1s0dz2Ilv0yKhHIofhmGjE6eDcYifwLqdQ0vRcSnE2OomX9q8n Nnv2Qt7X+ueMAlQUqkVcHIhIMHc8ZNN8CMZnXrqYhwuIZgS96iYGHTgQ/DTNM/c7pkeXEF1o QdC2WofRwTyCD3UmzK3yI27AW2upp+BPaBCsN094BjOJdgQVVTsMt4pNRIDWrvVs5xP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5xYm5xaV66XnJ+7iZGYIxuO/Zz8w7GSxuDDzEKcDAq8fAaFM2K FGJNLCuuzD3EKMHBrCTCu2shUIg3JbGyKrUoP76oNCe1+BCjKdBzE5mlRJPzgekjryTe0MTQ 3NLQyNjCwtzISEmcV/1yU6SQQHpiSWp2ampBahFMHxMHp1QDo6ys/KSfV3IZr21pEtnAUVd6 7NDOiNC7U3pWL+9hst8d+u6nUUBPfVeYvF18oq/LzgS+hjn1z82uvPF5e/e7WmlESYBOyAbH ZfHLWl4wTV7z11JVb0vWlemefVN0T+Zqv7rs2LnVsKaUP1oxVCH0/UPrnomxO4RY2lbumyCU /Tqae9qPn6y9SizFGYmGWsxFxYkAptqmWOcCAAA= X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170821092921eucas1p2034fb4d79f77a3c955602a8fb675a84d X-Msg-Generator: CA X-Sender-IP: 182.198.249.179 X-Local-Sender: =?UTF-8?B?TWFyZWsgU3p5cHJvd3NraRtTUlBPTC1LZXJuZWwgKFRQKRs=?= =?UTF-8?B?7IK87ISx7KCE7J6QG1NlbmlvciBTb2Z0d2FyZSBFbmdpbmVlcg==?= X-Global-Sender: =?UTF-8?B?TWFyZWsgU3p5cHJvd3NraRtTUlBPTC1LZXJuZWwgKFRQKRtT?= =?UTF-8?B?YW1zdW5nIEVsZWN0cm9uaWNzG1NlbmlvciBTb2Z0d2FyZSBFbmdpbmVlcg==?= X-Sender-Code: =?UTF-8?B?QzEwG0VIURtDMTBDRDAyQ0QwMjczOTI=?= CMS-TYPE: 201P X-CMS-RootMailID: 20170821090953epcas3p1121178ff7dbd3125a423a807c79ee33b X-RootMTR: 20170821090953epcas3p1121178ff7dbd3125a423a807c79ee33b References: <20170818141606.4835-2-stanimir.varbanov@linaro.org> <20170821090909.32614-1-stanimir.varbanov@linaro.org> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 6470 Lines: 152 Hi Stanimir, On 2017-08-21 11:09, Stanimir Varbanov wrote: > This change is intended to give to the v4l2 drivers a choice to > change the default behavior of the v4l2-core DMA mapping direction > from DMA_TO/FROM_DEVICE (depending on the buffer type CAPTURE or > OUTPUT) to DMA_BIDIRECTIONAL during queue_init time. > > Initially the issue with DMA mapping direction has been found in > Venus encoder driver where the hardware (firmware side) adds few > lines padding on bottom of the image buffer, and the consequence > is triggering of IOMMU protection faults. > > This will help supporting venus encoder (and probably other drivers > in the future) which wants to map output type of buffers as > read/write. > > Signed-off-by: Stanimir Varbanov This has been already discussed about a year ago, but it got lost in meantime, probably due to lack of users. I hope that this time it finally will get into vb2. For the reference, see https://patchwork.kernel.org/patch/9388163/ > --- > v2: move dma_dir into private section. > > drivers/media/v4l2-core/videobuf2-core.c | 17 ++++++++--------- > include/media/videobuf2-core.h | 13 +++++++++++++ > 2 files changed, 21 insertions(+), 9 deletions(-) > > diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c > index 0924594989b4..cb115ba6a1d2 100644 > --- a/drivers/media/v4l2-core/videobuf2-core.c > +++ b/drivers/media/v4l2-core/videobuf2-core.c > @@ -194,8 +194,6 @@ static void __enqueue_in_driver(struct vb2_buffer *vb); > static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) > { > struct vb2_queue *q = vb->vb2_queue; > - enum dma_data_direction dma_dir = > - q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; > void *mem_priv; > int plane; > int ret = -ENOMEM; > @@ -209,7 +207,7 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) > > mem_priv = call_ptr_memop(vb, alloc, > q->alloc_devs[plane] ? : q->dev, > - q->dma_attrs, size, dma_dir, q->gfp_flags); > + q->dma_attrs, size, q->dma_dir, q->gfp_flags); > if (IS_ERR_OR_NULL(mem_priv)) { > if (mem_priv) > ret = PTR_ERR(mem_priv); > @@ -978,8 +976,6 @@ static int __prepare_userptr(struct vb2_buffer *vb, const void *pb) > void *mem_priv; > unsigned int plane; > int ret = 0; > - enum dma_data_direction dma_dir = > - q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; > bool reacquired = vb->planes[0].mem_priv == NULL; > > memset(planes, 0, sizeof(planes[0]) * vb->num_planes); > @@ -1030,7 +1026,7 @@ static int __prepare_userptr(struct vb2_buffer *vb, const void *pb) > mem_priv = call_ptr_memop(vb, get_userptr, > q->alloc_devs[plane] ? : q->dev, > planes[plane].m.userptr, > - planes[plane].length, dma_dir); > + planes[plane].length, q->dma_dir); > if (IS_ERR(mem_priv)) { > dprintk(1, "failed acquiring userspace memory for plane %d\n", > plane); > @@ -1096,8 +1092,6 @@ static int __prepare_dmabuf(struct vb2_buffer *vb, const void *pb) > void *mem_priv; > unsigned int plane; > int ret = 0; > - enum dma_data_direction dma_dir = > - q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; > bool reacquired = vb->planes[0].mem_priv == NULL; > > memset(planes, 0, sizeof(planes[0]) * vb->num_planes); > @@ -1156,7 +1150,7 @@ static int __prepare_dmabuf(struct vb2_buffer *vb, const void *pb) > /* Acquire each plane's memory */ > mem_priv = call_ptr_memop(vb, attach_dmabuf, > q->alloc_devs[plane] ? : q->dev, > - dbuf, planes[plane].length, dma_dir); > + dbuf, planes[plane].length, q->dma_dir); > if (IS_ERR(mem_priv)) { > dprintk(1, "failed to attach dmabuf\n"); > ret = PTR_ERR(mem_priv); > @@ -2003,6 +1997,11 @@ int vb2_core_queue_init(struct vb2_queue *q) > if (q->buf_struct_size == 0) > q->buf_struct_size = sizeof(struct vb2_buffer); > > + if (q->bidirectional) > + q->dma_dir = DMA_BIDIRECTIONAL; > + else > + q->dma_dir = q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; > + > return 0; > } > EXPORT_SYMBOL_GPL(vb2_core_queue_init); > diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h > index cb97c224be73..ef9b64398c8c 100644 > --- a/include/media/videobuf2-core.h > +++ b/include/media/videobuf2-core.h > @@ -427,6 +427,16 @@ struct vb2_buf_ops { > * @dev: device to use for the default allocation context if the driver > * doesn't fill in the @alloc_devs array. > * @dma_attrs: DMA attributes to use for the DMA. > + * @bidirectional: when this flag is set the DMA direction for the buffers of > + * this queue will be overridden with DMA_BIDIRECTIONAL direction. > + * This is useful in cases where the hardware (firmware) writes to > + * a buffer which is mapped as read (DMA_TO_DEVICE), or reads from > + * buffer which is mapped for write (DMA_FROM_DEVICE) in order > + * to satisfy some internal hardware restrictions or adds a padding > + * needed by the processing algorithm. In case the DMA mapping is > + * not bidirectional but the hardware (firmware) trying to access > + * the buffer (in the opposite direction) this could lead to an > + * IOMMU protection faults. > * @fileio_read_once: report EOF after reading the first buffer > * @fileio_write_immediately: queue buffer after each write() call > * @allow_zero_bytesused: allow bytesused == 0 to be passed to the driver > @@ -465,6 +475,7 @@ struct vb2_buf_ops { > * Private elements (won't appear at the uAPI book): > * @mmap_lock: private mutex used when buffers are allocated/freed/mmapped > * @memory: current memory type used > + * @dma_dir: DMA mapping direction. > * @bufs: videobuf buffer structures > * @num_buffers: number of allocated/used buffers > * @queued_list: list of buffers currently queued from userspace > @@ -495,6 +506,7 @@ struct vb2_queue { > unsigned int io_modes; > struct device *dev; > unsigned long dma_attrs; > + unsigned bidirectional:1; > unsigned fileio_read_once:1; > unsigned fileio_write_immediately:1; > unsigned allow_zero_bytesused:1; > @@ -516,6 +528,7 @@ struct vb2_queue { > /* private: internal use only */ > struct mutex mmap_lock; > unsigned int memory; > + enum dma_data_direction dma_dir; > struct vb2_buffer *bufs[VB2_MAX_FRAME]; > unsigned int num_buffers; > Best regards -- Marek Szyprowski, PhD Samsung R&D Institute Poland