From patchwork Mon May 2 10:59:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 67005 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1012730qge; Mon, 2 May 2016 03:59:25 -0700 (PDT) X-Received: by 10.98.97.71 with SMTP id v68mr37488562pfb.127.1462186765032; Mon, 02 May 2016 03:59:25 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bm3si41292261pad.35.2016.05.02.03.59.24; Mon, 02 May 2016 03:59:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-samsung-soc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-samsung-soc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-samsung-soc-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753516AbcEBK7X (ORCPT + 4 others); Mon, 2 May 2016 06:59:23 -0400 Received: from mailout1.w1.samsung.com ([210.118.77.11]:51463 "EHLO mailout1.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753378AbcEBK7W (ORCPT ); Mon, 2 May 2016 06:59:22 -0400 Received: from eucpsbgm2.samsung.com (unknown [203.254.199.245]) by mailout1.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0O6J00KNBQIV7M70@mailout1.w1.samsung.com>; Mon, 02 May 2016 11:59:19 +0100 (BST) X-AuditID: cbfec7f5-f792a6d000001302-3d-572733072f2e Received: from eusync1.samsung.com ( [203.254.199.211]) by eucpsbgm2.samsung.com (EUCPMTA) with SMTP id 50.41.04866.70337275; Mon, 2 May 2016 11:59:19 +0100 (BST) Received: from amdc1339.digital.local ([106.116.147.30]) by eusync1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0O6J009HLQISGA70@eusync1.samsung.com>; Mon, 02 May 2016 11:59:19 +0100 (BST) From: Marek Szyprowski To: linux-media@vger.kernel.org, linux-samsung-soc@vger.kernel.org Cc: Marek Szyprowski , Sylwester Nawrocki , Laurent Pinchart , Krzysztof Kozlowski , Bartlomiej Zolnierkiewicz , Hans Verkuil Subject: [PATCH v3] media: vb2-dma-contig: configure DMA max segment size properly Date: Mon, 02 May 2016 12:59:13 +0200 Message-id: <1462186753-4177-1-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.9.2 In-reply-to: <57271235.9030004@xs4all.nl> References: <57271235.9030004@xs4all.nl> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprMLMWRmVeSWpSXmKPExsVy+t/xy7rsxurhBj0dwhYbZ6xntVjycxeT xesXhhadE5ewW/Rs2MpqMeP8PiaLtUfuslscftPO6sDhMeX3RlaP2R0zWT36tqxi9Pi8SS6A JYrLJiU1J7MstUjfLoErY9qJl8wF/coVd3ZcZW9gfCXTxcjJISFgIvGp6y4bhC0mceHeeiCb i0NIYCmjxNNjX5ggnCYmiYdnr7CCVLEJGEp0ve0C6xARcJJYOOsvO0gRs8AKJom3cy+BJYQF QiR2PrsJ1sAioCrR+6SfEcTmFXCX2H/1PiPEOjmJ/y9XMIHYnAKaEv2fzgEN4gDapiGx93DM BEbeBYwMqxhFU0uTC4qT0nON9IoTc4tL89L1kvNzNzFCgurrDsalx6wOMQpwMCrx8Hqkq4UL sSaWFVfmHmKU4GBWEuHtMFQPF+JNSaysSi3Kjy8qzUktPsQozcGiJM47c9f7ECGB9MSS1OzU 1ILUIpgsEwenVANjwww10yOL/DtnNbEdsmp+ZnCl5FnlKb2dGXeWn/K88P0f3+suxpdqkuIh qubSNz//nF6TtXr5rDSG1V+uxGpMyv6xPqZC5d771YxFASa7ZvxubI3q0lu/w/esZfzXE6cX pzM6Zt850v7/5cXZ1zx/9Vy1qI/Jn3Dwlfum8kROL3lx483JbZduKLEUZyQaajEXFScCAGgy yBkmAgAA Sender: linux-samsung-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-samsung-soc@vger.kernel.org This patch lets vb2-dma-contig memory allocator to configure DMA max segment size properly for the client device. Setting it is needed to let DMA-mapping subsystem to create a single, contiguous mapping in DMA address space. This is essential for all devices, which use dma-contig videobuf2 memory allocator and shared buffers (in USERPTR or DMAbuf modes of operations). Signed-off-by: Marek Szyprowski --- Hello, This patch is a follow-up of my previous attempts to let Exynos multimedia devices to work properly with shared buffers when IOMMU is enabled: 1. https://www.mail-archive.com/linux-media@vger.kernel.org/msg96946.html 2. http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/97316 3. https://patchwork.linuxtv.org/patch/30870/ As sugested by Hans, configuring DMA max segment size should be done by videobuf2-dma-contig module instead of requiring all device drivers to do it on their own. Here is some backgroud why this is done in videobuf2-dc not in the respective generic bus code: http://lists.infradead.org/pipermail/linux-arm-kernel/2014-November/305913.html Best regards, Marek Szyprowski changelog: v3: - added FIXME note about possible memory leak v2: - fixes typos and other language issues in the comments v1: http://article.gmane.org/gmane.linux.kernel.samsung-soc/53690 --- drivers/media/v4l2-core/videobuf2-dma-contig.c | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) -- 1.9.2 -- To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/media/v4l2-core/videobuf2-dma-contig.c b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 461ae55eaa98..2ca7e798f394 100644 --- a/drivers/media/v4l2-core/videobuf2-dma-contig.c +++ b/drivers/media/v4l2-core/videobuf2-dma-contig.c @@ -443,6 +443,42 @@ static void vb2_dc_put_userptr(void *buf_priv) } /* + * To allow mapping the scatter-list into a single chunk in the DMA + * address space, the device is required to have the DMA max segment + * size parameter set to a value larger than the buffer size. Otherwise, + * the DMA-mapping subsystem will split the mapping into max segment + * size chunks. This function increases the DMA max segment size + * parameter to let DMA-mapping map a buffer as a single chunk in DMA + * address space. + * This code assumes that the DMA-mapping subsystem will merge all + * scatterlist segments if this is really possible (for example when + * an IOMMU is available and enabled). + * Ideally, this parameter should be set by the generic bus code, but it + * is left with the default 64KiB value due to historical litmiations in + * other subsystems (like limited USB host drivers) and there no good + * place to set it to the proper value. It is done here to avoid fixing + * all the vb2-dc client drivers. + * + * FIXME: the allocated dma_params structure is leaked because there + * is completely no way to determine when to free it (dma_params might have + * been also already allocated by the bus code). However in typical + * use cases this function will be called for platform devices, which are + * not how-plugged and exist all the time in the target system. + */ +static int vb2_dc_set_max_seg_size(struct device *dev, unsigned int size) +{ + if (!dev->dma_parms) { + dev->dma_parms = kzalloc(sizeof(dev->dma_parms), GFP_KERNEL); + if (!dev->dma_parms) + return -ENOMEM; + } + if (dma_get_max_seg_size(dev) < size) + return dma_set_max_seg_size(dev, size); + + return 0; +} + +/* * For some kind of reserved memory there might be no struct page available, * so all that can be done to support such 'pages' is to try to convert * pfn to dma address or at the last resort just assume that @@ -499,6 +535,10 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, return ERR_PTR(-EINVAL); } + ret = vb2_dc_set_max_seg_size(dev, PAGE_ALIGN(size + PAGE_SIZE)); + if (!ret) + return ERR_PTR(ret); + buf = kzalloc(sizeof *buf, GFP_KERNEL); if (!buf) return ERR_PTR(-ENOMEM); @@ -675,10 +715,15 @@ static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf, { struct vb2_dc_buf *buf; struct dma_buf_attachment *dba; + int ret; if (dbuf->size < size) return ERR_PTR(-EFAULT); + ret = vb2_dc_set_max_seg_size(dev, PAGE_ALIGN(size)); + if (!ret) + return ERR_PTR(ret); + buf = kzalloc(sizeof(*buf), GFP_KERNEL); if (!buf) return ERR_PTR(-ENOMEM);