Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp702267lqc; Fri, 8 Mar 2024 09:12:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXeu7D1tu6qDjlKUoTuTE4aFY+Od8W2XC0k40sBxc664u9cZks2cKTN40T5m9T+5rcwEtVQOC5Ze+oCxDbSVwPv1OPyBR++zzjIyqDO7A== X-Google-Smtp-Source: AGHT+IGk0DJ/mHllIOZujgtUj86XVh/PLGRIoP9K+qlhjl1hGr28BeColKMDiZ0HJYCoe5KuT67g X-Received: by 2002:a05:6e02:18cc:b0:365:245c:6c99 with SMTP id s12-20020a056e0218cc00b00365245c6c99mr28198108ilu.29.1709917938721; Fri, 08 Mar 2024 09:12:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709917938; cv=pass; d=google.com; s=arc-20160816; b=WO4rj8m8MH/A18DQG6cOW1kPUKGkSlH2CWsv1qgyew5GB+oWkIE4PU1Fg3kUE0KnUM nP6ziiSck61F+tCR8a/xMVZ0uBNVnSH78EkqxvZI5cyQR3rGtTa+XpmubfzVoHx++pEa cxk8bfurgTbnWaVuGDfsD0/rEYq7xzbHEO23IYlwPgNeyvLAMYHsxsnFLYYHQDdJ006P L4SwHsTpyj99KUO/7b4Qphp1+BB+nIeAT6bY8Lxj8mtijkLtiea5O9o081KLRH4sWZBo Q0Z3ZLz6gLY1oSvkmbs+oSTKpHR2rlh2gWM+G0yT2v7E175Yrf6xtyFNck4Np4kZwfi/ D13Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=diVnTKGYmGTXMK2fWu9xl+JrtOVoNzvMtMZ/RN8kb8E=; fh=yZS2g+QQKdMrQ3jorRH5H6dPNDM7GRj3yrDB9sYJ3mc=; b=Rv1giX13Pm7J0XfqwOje+hG8jqgV+HWTl8xpbB38SBo3JDkrg5IfqYHLrST1FswM04 fxPceU5LwJdCsebrrN786/SG2uYAKaQz1V/88kQwPjdZTWReabtHZk9pYhURkRFdS4YC kDQKFCKLksg756hDhXne9XjOMLv3+4XiazqZZtLs2CAEhLSVrbvk/r1eAtC5JyVC0oxA KJtQUJVOT+tfsKiL/BRTgCfkkGqtYhxej9VPvcvmP1Xcpz6DNMJk4ALxbwYOncPTMZCt Lwpa1zNuft7hYJXeVXc2wAx58Whac58LW7+UmbPQX9jy6IuuLBxOYP4L5PwCklS5FSQ1 agcw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=asYXEOeO; arc=pass (i=1 spf=pass spfdomain=crapouillou.net dkim=pass dkdomain=crapouillou.net dmarc=pass fromdomain=crapouillou.net); spf=pass (google.com: domain of linux-kernel+bounces-97344-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97344-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id i8-20020a63cd08000000b005dc8fe5ef05si16501163pgg.96.2024.03.08.09.12.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 09:12:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97344-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=asYXEOeO; arc=pass (i=1 spf=pass spfdomain=crapouillou.net dkim=pass dkdomain=crapouillou.net dmarc=pass fromdomain=crapouillou.net); spf=pass (google.com: domain of linux-kernel+bounces-97344-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97344-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 78096286734 for ; Fri, 8 Mar 2024 17:02:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 877D724B24; Fri, 8 Mar 2024 17:02:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="asYXEOeO" Received: from aposti.net (aposti.net [89.234.176.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2175C24A06; Fri, 8 Mar 2024 17:01:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=89.234.176.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709917319; cv=none; b=BI+YJRueqfPlmNSS93v3ZTKbpkIp6Manyi8+VmoOYwe7NOdI8oNBOpevnUDYtxp9LZZN0PsIxiKFaXxP5PMoyJysSxmRQgTH6G25E/lets9KMFI+RZwnkq0sAbNWlgKhR3wl/mGwnySQmXrKs3hD41ziY+aZ/YuiLCwulza3nhg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709917319; c=relaxed/simple; bh=v8F5DoNlHt0mR7veIGDJ45cx2qJsTw433pwoyOQmQcg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pCdQBq0oUDyULOfsMf4Zo1tKq4/cCuRXT1+VRCRbJ4pY3wQnEU1dE6OpfBWzgOugFhnd9g0Gbnn+jHlZVkHyKR4GTvNvbJNEZ2wU/V2VIr1S9vWGTf9NIus7S6ylcnzXgQXBlvWjPfkIdFMHogiQHydS3JpzzKnQFn6HAAWPkeQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=crapouillou.net; spf=pass smtp.mailfrom=crapouillou.net; dkim=pass (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b=asYXEOeO; arc=none smtp.client-ip=89.234.176.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=crapouillou.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=crapouillou.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1709917272; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=diVnTKGYmGTXMK2fWu9xl+JrtOVoNzvMtMZ/RN8kb8E=; b=asYXEOeOdL6jGQoMvpH6GdoOvPbLs6Oe+mVG5gFHm6wp7SdIe+qgLPa7N8kq8+/JPpjQSC RwuF9qGk9yGOLcB1IWwOu8VanOIdG9KqFHrdKB4cgTNHmV1FDPEjWK30Q1OMmz6SCHzGsg GX4hHQky6fk+lU7IUyv/oWuXmJjHRLA= From: Paul Cercueil To: Jonathan Cameron , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jonathan Corbet , Lars-Peter Clausen , Vinod Koul , Sumit Semwal Cc: Nuno Sa , Michael Hennerich , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dmaengine@vger.kernel.org, linux-iio@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Paul Cercueil Subject: [PATCH v8 5/6] iio: buffer-dmaengine: Support new DMABUF based userspace API Date: Fri, 8 Mar 2024 18:00:45 +0100 Message-ID: <20240308170046.92899-6-paul@crapouillou.net> In-Reply-To: <20240308170046.92899-1-paul@crapouillou.net> References: <20240308170046.92899-1-paul@crapouillou.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Use the functions provided by the buffer-dma core to implement the DMABUF userspace API in the buffer-dmaengine IIO buffer implementation. Since we want to be able to transfer an arbitrary number of bytes and not necesarily the full DMABUF, the associated scatterlist is converted to an array of DMA addresses + lengths, which is then passed to dmaengine_prep_slave_dma_array(). Signed-off-by: Paul Cercueil Signed-off-by: Nuno Sa --- v3: Use the new dmaengine_prep_slave_dma_array(), and adapt the code to work with the new functions introduced in industrialio-buffer-dma.c. v5: - Use the new dmaengine_prep_slave_dma_vec(). - Restrict to input buffers, since output buffers are not yet supported by IIO buffers. v6: - Populate .lock_queue / .unlock_queue callbacks - Switch to atomic memory allocations in .submit_queue, because of the dma_fence critical section - Make sure that the size of the scatterlist is enough v7: Adapted patch for the changes made in patch 1. --- .../buffer/industrialio-buffer-dmaengine.c | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/iio/buffer/industrialio-buffer-dmaengine.c index a18c1da292af..3b7b649f0a89 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -64,15 +64,55 @@ static int iio_dmaengine_buffer_submit_block(struct iio_dma_buffer_queue *queue, struct dmaengine_buffer *dmaengine_buffer = iio_buffer_to_dmaengine_buffer(&queue->buffer); struct dma_async_tx_descriptor *desc; + struct scatterlist *sgl; + struct dma_vec *vecs; dma_cookie_t cookie; + size_t len_total; + size_t max_size; + unsigned int i; + int nents; - block->bytes_used = min(block->size, dmaengine_buffer->max_size); - block->bytes_used = round_down(block->bytes_used, - dmaengine_buffer->align); + if (queue->buffer.direction != IIO_BUFFER_DIRECTION_IN) { + /* We do not yet support output buffers. */ + return -EINVAL; + } - desc = dmaengine_prep_slave_single(dmaengine_buffer->chan, - block->phys_addr, block->bytes_used, DMA_DEV_TO_MEM, - DMA_PREP_INTERRUPT); + if (block->sg_table) { + sgl = block->sg_table->sgl; + nents = sg_nents_for_len(sgl, block->bytes_used); + if (nents < 0) + return nents; + + vecs = kmalloc_array(nents, sizeof(*vecs), GFP_ATOMIC); + if (!vecs) + return -ENOMEM; + + len_total = block->bytes_used; + + for (i = 0; i < nents; i++) { + vecs[i].addr = sg_dma_address(sgl); + vecs[i].len = min(sg_dma_len(sgl), len_total); + len_total -= vecs[i].len; + + sgl = sg_next(sgl); + } + + desc = dmaengine_prep_peripheral_dma_vec(dmaengine_buffer->chan, + vecs, nents, + DMA_DEV_TO_MEM, + DMA_PREP_INTERRUPT, 0); + kfree(vecs); + } else { + max_size = min(block->size, dmaengine_buffer->max_size); + max_size = round_down(max_size, dmaengine_buffer->align); + block->bytes_used = max_size; + + desc = dmaengine_prep_slave_single(dmaengine_buffer->chan, + block->phys_addr, + block->bytes_used, + DMA_DEV_TO_MEM, + DMA_PREP_INTERRUPT); + } if (!desc) return -ENOMEM; @@ -120,6 +160,13 @@ static const struct iio_buffer_access_funcs iio_dmaengine_buffer_ops = { .data_available = iio_dma_buffer_data_available, .release = iio_dmaengine_buffer_release, + .enqueue_dmabuf = iio_dma_buffer_enqueue_dmabuf, + .attach_dmabuf = iio_dma_buffer_attach_dmabuf, + .detach_dmabuf = iio_dma_buffer_detach_dmabuf, + + .lock_queue = iio_dma_buffer_lock_queue, + .unlock_queue = iio_dma_buffer_unlock_queue, + .modes = INDIO_BUFFER_HARDWARE, .flags = INDIO_BUFFER_FLAG_FIXED_WATERMARK, }; -- 2.43.0