From patchwork Fri Feb 23 12:14:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nuno Sa X-Patchwork-Id: 13568980 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DBD0E7B3EC; Fri, 23 Feb 2024 12:11:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.135.77 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708690295; cv=none; b=tMeW5c/JN5Y688Yp5uGe2/hcHiqvRaa2igRsRQDOWsjn6TunRJZkO0+pgcuSYzvwXOQxmo63bdtR2mdujb64WhEWP88v09Su/070kYWw+p6sDu1RWRkfyMXOBIGngC5MSxkU6+WnnoIOK1gQMw4aaBEwSDc7VuIuYN0uMIrRBYg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708690295; c=relaxed/simple; bh=SwYo8BQlQReCdoBZkN0oRQuckLkXIUt9M0zHp08EkNA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=aCh+Qv6Mw9RfEiSoODb7wvAspfTzjFjVZY6c1ubzQ8zODXnfEuTDVn3s1cY7TsX5MK546eLVo3NQIDFqxkvDDFmQCoKHo52ntT+DOGD0YnIlrD4kCwUN6mt5tesEvqJMfsLonRf5AW029czB2NV3SFeglEIuq3Cd1Ul28cz49jw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=analog.com; spf=pass smtp.mailfrom=analog.com; dkim=pass (2048-bit key) header.d=analog.com header.i=@analog.com header.b=JQvsz7BZ; arc=none smtp.client-ip=148.163.135.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=analog.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=analog.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=analog.com header.i=@analog.com header.b="JQvsz7BZ" Received: from pps.filterd (m0167088.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41N6VQ5w002016; Fri, 23 Feb 2024 07:11:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.com; h= from:date:subject:mime-version:content-type :content-transfer-encoding:message-id:references:in-reply-to:to :cc; s=DKIM; bh=5a7n4SpiB+g7Oy9l9NWszRo6H+kOBtRSNu5LOhckpu4=; b= JQvsz7BZSCUqO4dDq4WPCCugf6eGzBI/id9iNFQ+mAlzmOs+VFOi6qZpN+Vd2Peb qdFIn010YeYedRBtzxTb6AAao9F/uwCL9jWEEQl5bGwnvdPpkYd6tuWDhfJShEgl J5nj34ptAmr8kr6aKjeHmCcK22BDVN6wGQ4J4hkWIzemxHT6mEhjyeVeKGPdWI6v cBCq5qQEzzon2PbTA50QU9N+YNVtGP8YLnZV1ZEOXaPxIwnzPHMeYG4SZbju0Zaw uT4cPbFgp0udPFlmOk/865FUdZPesz5YJqXuEXUJIYWXBZJt3KrmbOjRN6x2viL0 pxziUQfjltMfD6yDTXbVeg== Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3wep5mh41u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 23 Feb 2024 07:11:14 -0500 (EST) Received: from ASHBMBX8.ad.analog.com (ASHBMBX8.ad.analog.com [10.64.17.5]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 41NCB9Kv033133 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 23 Feb 2024 07:11:09 -0500 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBMBX8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Fri, 23 Feb 2024 07:11:08 -0500 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Fri, 23 Feb 2024 07:11:08 -0500 Received: from [127.0.0.1] ([10.44.3.55]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 41NCAb2W032246; Fri, 23 Feb 2024 07:10:59 -0500 From: Nuno Sa Date: Fri, 23 Feb 2024 13:14:03 +0100 Subject: [PATCH v7 5/6] iio: buffer-dmaengine: Support new DMABUF based userspace API Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <20240223-iio-dmabuf-v7-5-78cfaad117b9@analog.com> References: <20240223-iio-dmabuf-v7-0-78cfaad117b9@analog.com> In-Reply-To: <20240223-iio-dmabuf-v7-0-78cfaad117b9@analog.com> To: Vinod Koul , Lars-Peter Clausen , Jonathan Cameron , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Jonathan Corbet , Paul Cercueil CC: Daniel Vetter , Michael Hennerich , , , , , , X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1708690439; l=3326; i=nuno.sa@analog.com; s=20231116; h=from:subject:message-id; bh=7UZBjwkHqj39BUr5O4B3LRfoGCW+O2PrXk4l6mTzKJg=; b=TGljtzZdiBQ4fAVMB+H28H2JoT3ee7yAb4WCy4R8eP+0c3VA+7MIBvqV3xJS6L8eObBZ57MSn 6okMo5snmjAAWTfYgHum/YtL5yZrMisP39fDmlqNWSQJxKiFNXtnkKC X-Developer-Key: i=nuno.sa@analog.com; a=ed25519; pk=3NQwYA013OUYZsmDFBf8rmyyr5iQlxV/9H4/Df83o1E= X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: mNOh_fBXvtKkLclg9ac4Dg_E7p-fs2WJ X-Proofpoint-GUID: mNOh_fBXvtKkLclg9ac4Dg_E7p-fs2WJ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-22_15,2024-02-23_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 spamscore=0 malwarescore=0 adultscore=0 phishscore=0 lowpriorityscore=0 clxscore=1015 impostorscore=0 priorityscore=1501 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2402120000 definitions=main-2402230088 From: Paul Cercueil 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 --- drivers/iio/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, };