Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp226416lqr; Wed, 5 Jun 2024 04:28:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVVpOoptZnKuGZxf5+NZgziewCDaEDewkwaWSnI9vP/MNMKgN5cyRhuLtKGt4OSdC9OkW5OSe0In1WgESIy8sbMLMqLYdocXnkXxQV+ng== X-Google-Smtp-Source: AGHT+IETy6IVcTJ/Ig5piZJeOTVBm7g3XahR3tAK5vV48yq54tcpJiwna+4rU7hK7883wS9MrL8t X-Received: by 2002:a05:6808:1899:b0:3c9:68ba:6e14 with SMTP id 5614622812f47-3d2043d40f7mr2490597b6e.44.1717586908030; Wed, 05 Jun 2024 04:28:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717586908; cv=pass; d=google.com; s=arc-20160816; b=fSsGFNegS2oJGnnI51B/+iCWe1yVHFEFcJn73u0fF3CEX/gCoNT8JBYnYmoAwWfSUY a59ehaDeGHEcgx0rKB6t32otIMF67DnBqbZ3Iml0wLbecXYckqAvlvcXljlMxfBMHJf9 qCo/Q9LcUEFVj0LuMoQMY3/jPxQfASj3TcJGDe2xs/rJ2OWPQEq7QgBz9LmhTWITpSV1 jkVfO2bcUXjI7NLMkLfZyDlO4/TZ57xoFSw68lvAHuee6tfVD01DDAe1JG0SJTBR50iq zLOev4GHf1fnLQtkYF4aGZPU8C6p/M/QOhakoyUAPE1F/DdGjxWZqTWQ4jdkIPBaHsDz tfgw== 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=QSABxmTqsywGV191EjxiHZ1qFDB0JGwtzve6lD4D3Pk=; fh=gIPS46kF5oiVtxHOjYLqoXg82bH7WL+T2QnpDaGuRdA=; b=ElBUIhlVzGqvG1EaHuYC3R26qSL64icx45AiUUO3d4DY3zqh4a8AREAhHqj64VYTjj mJpLleXPuxOPRW6lZoMuZpVGjCcad3q4zHofW7Dad6SDjRcmh1wtTEUPboYIQfra4bl8 p6WcTjdyew88xvp0fdqw6F0nIlEKgFsV4+5qlnhWGmT9yp3GDcc0xYpddb6PeLm7MHDI m6vtZ6msmdKjvIpZoExmqCXWQozYV4GRtPnj9PahjwdPl2y1HuadSSG6U7hXP0tYQbc0 THedj/Kd8LuBbXqxKO6mmPuZuHo7gOdQmW7plko+OLopY7CcrSl4i6u2PvBA0rAe2/M1 r1ww==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=WtNPhX1v; 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-202238-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202238-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-794f3063341si1460741185a.330.2024.06.05.04.28.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 04:28:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202238-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=WtNPhX1v; 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-202238-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202238-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 383831C23C1E for ; Wed, 5 Jun 2024 11:11:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BAEF8194147; Wed, 5 Jun 2024 11:09:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="WtNPhX1v" 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 45A7B192B85; Wed, 5 Jun 2024 11:09:49 +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=1717585790; cv=none; b=fKfdPWHG0hgGiwq+gBXYSy2z/kELGiRAun6Tr11UNqQT6PNror4sUyTickjObndN+3lzrK91zknyamj/nIanB9kbyBQwVgzTd4fj4qsIi19I89zoZmdyru9n6eVnuC15WAWmYWwgyn13+aJkHdHABJjtCxd6JimIAHIi68VR9Yo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717585790; c=relaxed/simple; bh=u9+c6wVNF78e6GyDk+6bMnAW0JBJDD1opDEfpOd/3VM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tdv9J3juYQm1SYc06T5xBNBkTdnqpR6gI4LuCxpzqRU7eKXRhWXYuRB7if6y0oS1GjoqONrxIAV1aj/pxDLUWTonUQaiUIvievIEdRfgNbj1gYjCGH+VSt3LKZsRV6RNMVxWkjDc/VwIHBZxcpMKLtSnLiNQ+bLkm4FgthBaeqg= 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=WtNPhX1v; 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=1717585743; 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=QSABxmTqsywGV191EjxiHZ1qFDB0JGwtzve6lD4D3Pk=; b=WtNPhX1vq75wIUQwfWJmsw1rDYwhrWW/nFT5oO/gsR3cHbt/KhMQtfBnSN09LW9DoDX4Jk sOU44k5Es2Ms8IrSi01TmErzJHbt6oXkcS+4IYr4257kWgug9iYGr8uMQk2flYp34KDCD7 he3axQR5MD4AHKtPp0xoqmFNHWtLrdk= From: Paul Cercueil To: Jonathan Cameron , Lars-Peter Clausen , Vinod Koul , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= Cc: Jonathan Corbet , Nuno Sa , linux-iio@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, dmaengine@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Paul Cercueil Subject: [PATCH v10 5/6] iio: buffer-dmaengine: Support new DMABUF based userspace API Date: Wed, 5 Jun 2024 13:08:44 +0200 Message-ID: <20240605110845.86740-6-paul@crapouillou.net> In-Reply-To: <20240605110845.86740-1-paul@crapouillou.net> References: <20240605110845.86740-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. v10: - Remove extra flags parameter to dmaengine_prep_peripheral_dma_vec() - Add support for TX transfers --- .../buffer/industrialio-buffer-dmaengine.c | 62 ++++++++++++++++--- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/iio/buffer/industrialio-buffer-dmaengine.c index 918f6f8d65b6..12aa1412dfa0 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -65,25 +65,62 @@ static int iio_dmaengine_buffer_submit_block(struct iio_dma_buffer_queue *queue, iio_buffer_to_dmaengine_buffer(&queue->buffer); struct dma_async_tx_descriptor *desc; enum dma_transfer_direction dma_dir; + struct scatterlist *sgl; + struct dma_vec *vecs; size_t max_size; dma_cookie_t cookie; + size_t len_total; + unsigned int i; + int nents; max_size = min(block->size, dmaengine_buffer->max_size); max_size = round_down(max_size, dmaengine_buffer->align); - if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) { - block->bytes_used = max_size; + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) dma_dir = DMA_DEV_TO_MEM; - } else { + else dma_dir = DMA_MEM_TO_DEV; - } - if (!block->bytes_used || block->bytes_used > max_size) - return -EINVAL; + 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_slave_single(dmaengine_buffer->chan, - block->phys_addr, block->bytes_used, dma_dir, - DMA_PREP_INTERRUPT); + desc = dmaengine_prep_peripheral_dma_vec(dmaengine_buffer->chan, + vecs, nents, dma_dir, + DMA_PREP_INTERRUPT); + kfree(vecs); + } else { + max_size = min(block->size, dmaengine_buffer->max_size); + max_size = round_down(max_size, dmaengine_buffer->align); + + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) + block->bytes_used = max_size; + + if (!block->bytes_used || block->bytes_used > max_size) + return -EINVAL; + + desc = dmaengine_prep_slave_single(dmaengine_buffer->chan, + block->phys_addr, + block->bytes_used, + dma_dir, + DMA_PREP_INTERRUPT); + } if (!desc) return -ENOMEM; @@ -133,6 +170,13 @@ static const struct iio_buffer_access_funcs iio_dmaengine_buffer_ops = { .space_available = iio_dma_buffer_usage, .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