From patchwork Mon Aug 7 11:21:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 132040 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1476365vqr; Mon, 7 Aug 2023 07:08:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHkCB85A3R1MOEcmd3bf34yfWK1s5EyO5bDP++fRarjt6ysd0Ij95j2tMS0wlqE+bKcvBck X-Received: by 2002:a17:90b:400b:b0:269:1c2:f5c7 with SMTP id ie11-20020a17090b400b00b0026901c2f5c7mr6653907pjb.21.1691417302226; Mon, 07 Aug 2023 07:08:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691417302; cv=none; d=google.com; s=arc-20160816; b=sRPP4GhqfsV+15UmQkpXndy607qQW4WcImzNGxOdfzdYf7roGTtctecfzQU1M7zFxn LH56W0V+dypQsxxf80RNzhZBgYy8aCh/K5Io5aHVVEGwV2I/oA+VnPimZwXkJ11tmjOc 0/sboZ5G/1iTWgRxPilJ9pEsEh2CAdXvDh1bCOAQHr70dth5vxyXNfXUD82X+vAUJrjw SwB1oN9mrj7Mr3deNWyWwYvsxlGa4ny4gcp8g4olc7om6j9PqbZmzCOAo0zRB1W21aAz SdnKA4VgYNS/yQMrPvzNw/PwO7xPj5jmQFMc9qhL8sY4uWKKHPfTbMQ6RQpv4PfZAxc5 gnvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5TsvBzqlVMpWiPu3QW4pi/wCyFZ4xiLd9O4EJVtN2Pg=; fh=8W62y9fbWgtHcFWjUVBVy0E9D9qHeioTODDkGZpY+zc=; b=k5bpx5QwHIR+vbwORlgiQa34+CW+c3JRYXi1nx9JORt5jOblQgCMMnKN0MwmVnvV10 PLW81d+YjMaTPVxD9e4m6dCYO7L91L4Re6N173a+00bq1FXlMaar7LvvBj0qrfvP0Ej9 iZViDd1uG3nqj54h64otS04mqX15dXbQkr1F2YAAwdkVIRQu+kTYL7zei/mS+cMcCgxK oxtzqfpkqNYG6hLcnK4V0J7GuS+tokI7hmCARCrCYeXXVjaPfkbFq2W/PLcQPTEe5Ony wzvbiRv2i/Jy/8Q0Qd5YKITlJfwDOGRUmNCL/U8Da7jAZgat/O8FDddfDeRweGmyV6vp 7S0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=PknnydrG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y35-20020a17090a53a600b0025eab041423si8948033pjh.189.2023.08.07.07.08.08; Mon, 07 Aug 2023 07:08:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=PknnydrG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233060AbjHGL7r (ORCPT + 99 others); Mon, 7 Aug 2023 07:59:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233103AbjHGL7p (ORCPT ); Mon, 7 Aug 2023 07:59:45 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A68B10C0; Mon, 7 Aug 2023 04:59:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1691407284; 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=5TsvBzqlVMpWiPu3QW4pi/wCyFZ4xiLd9O4EJVtN2Pg=; b=PknnydrG2RODBxGRCPwnIZ8YTvFhUPvRgHuJDURDjjZ2i3E1ZONa3W5sgnYGr3fLj8i0et jSZjmJ8TLPcjw3h6Kzc46GjYUD1o1mCfL2ckYpGtiMg6l+pzLI7AsYWii6teql1aDBJYHG hTYTQErU1Btx9PXmjWZ2bZxv4d9SST8= From: Paul Cercueil To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Cercueil Subject: [PATCH v4 1/6] iio: buffer-dma: Get rid of outgoing queue Date: Mon, 7 Aug 2023 13:21:08 +0200 Message-Id: <20230807112113.47157-2-paul@crapouillou.net> In-Reply-To: <20230807112113.47157-1-paul@crapouillou.net> References: <20230807112113.47157-1-paul@crapouillou.net> MIME-Version: 1.0 X-Spam: Yes X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773579589083903712 X-GMAIL-MSGID: 1773579589083903712 The buffer-dma code was using two queues, incoming and outgoing, to manage the state of the blocks in use. While this totally works, it adds some complexity to the code, especially since the code only manages 2 blocks. It is much easier to just check each block's state manually, and keep a counter for the next block to dequeue. Since the new DMABUF based API wouldn't use the outgoing queue anyway, getting rid of it now makes the upcoming changes simpler. With this change, the IIO_BLOCK_STATE_DEQUEUED is now useless, and can be removed. Signed-off-by: Paul Cercueil --- v2: - Only remove the outgoing queue, and keep the incoming queue, as we want the buffer to start streaming data as soon as it is enabled. - Remove IIO_BLOCK_STATE_DEQUEUED, since it is now functionally the same as IIO_BLOCK_STATE_DONE. --- drivers/iio/buffer/industrialio-buffer-dma.c | 44 ++++++++++---------- include/linux/iio/buffer-dma.h | 7 ++-- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dma.c b/drivers/iio/buffer/industrialio-buffer-dma.c index d348af8b9705..1fc91467d1aa 100644 --- a/drivers/iio/buffer/industrialio-buffer-dma.c +++ b/drivers/iio/buffer/industrialio-buffer-dma.c @@ -179,7 +179,7 @@ static struct iio_dma_buffer_block *iio_dma_buffer_alloc_block( } block->size = size; - block->state = IIO_BLOCK_STATE_DEQUEUED; + block->state = IIO_BLOCK_STATE_DONE; block->queue = queue; INIT_LIST_HEAD(&block->head); kref_init(&block->kref); @@ -191,16 +191,8 @@ static struct iio_dma_buffer_block *iio_dma_buffer_alloc_block( static void _iio_dma_buffer_block_done(struct iio_dma_buffer_block *block) { - struct iio_dma_buffer_queue *queue = block->queue; - - /* - * The buffer has already been freed by the application, just drop the - * reference. - */ - if (block->state != IIO_BLOCK_STATE_DEAD) { + if (block->state != IIO_BLOCK_STATE_DEAD) block->state = IIO_BLOCK_STATE_DONE; - list_add_tail(&block->head, &queue->outgoing); - } } /** @@ -261,7 +253,6 @@ static bool iio_dma_block_reusable(struct iio_dma_buffer_block *block) * not support abort and has not given back the block yet. */ switch (block->state) { - case IIO_BLOCK_STATE_DEQUEUED: case IIO_BLOCK_STATE_QUEUED: case IIO_BLOCK_STATE_DONE: return true; @@ -317,7 +308,6 @@ int iio_dma_buffer_request_update(struct iio_buffer *buffer) * dead. This means we can reset the lists without having to fear * corrution. */ - INIT_LIST_HEAD(&queue->outgoing); spin_unlock_irq(&queue->list_lock); INIT_LIST_HEAD(&queue->incoming); @@ -456,14 +446,20 @@ static struct iio_dma_buffer_block *iio_dma_buffer_dequeue( struct iio_dma_buffer_queue *queue) { struct iio_dma_buffer_block *block; + unsigned int idx; spin_lock_irq(&queue->list_lock); - block = list_first_entry_or_null(&queue->outgoing, struct - iio_dma_buffer_block, head); - if (block != NULL) { - list_del(&block->head); - block->state = IIO_BLOCK_STATE_DEQUEUED; + + idx = queue->fileio.next_dequeue; + block = queue->fileio.blocks[idx]; + + if (block->state == IIO_BLOCK_STATE_DONE) { + idx = (idx + 1) % ARRAY_SIZE(queue->fileio.blocks); + queue->fileio.next_dequeue = idx; + } else { + block = NULL; } + spin_unlock_irq(&queue->list_lock); return block; @@ -539,6 +535,7 @@ size_t iio_dma_buffer_data_available(struct iio_buffer *buf) struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buf); struct iio_dma_buffer_block *block; size_t data_available = 0; + unsigned int i; /* * For counting the available bytes we'll use the size of the block not @@ -552,8 +549,15 @@ size_t iio_dma_buffer_data_available(struct iio_buffer *buf) data_available += queue->fileio.active_block->size; spin_lock_irq(&queue->list_lock); - list_for_each_entry(block, &queue->outgoing, head) - data_available += block->size; + + for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { + block = queue->fileio.blocks[i]; + + if (block != queue->fileio.active_block + && block->state == IIO_BLOCK_STATE_DONE) + data_available += block->size; + } + spin_unlock_irq(&queue->list_lock); mutex_unlock(&queue->lock); @@ -617,7 +621,6 @@ int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue, queue->ops = ops; INIT_LIST_HEAD(&queue->incoming); - INIT_LIST_HEAD(&queue->outgoing); mutex_init(&queue->lock); spin_lock_init(&queue->list_lock); @@ -645,7 +648,6 @@ void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue) continue; queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD; } - INIT_LIST_HEAD(&queue->outgoing); spin_unlock_irq(&queue->list_lock); INIT_LIST_HEAD(&queue->incoming); diff --git a/include/linux/iio/buffer-dma.h b/include/linux/iio/buffer-dma.h index 6564bdcdac66..18d3702fa95d 100644 --- a/include/linux/iio/buffer-dma.h +++ b/include/linux/iio/buffer-dma.h @@ -19,14 +19,12 @@ struct device; /** * enum iio_block_state - State of a struct iio_dma_buffer_block - * @IIO_BLOCK_STATE_DEQUEUED: Block is not queued * @IIO_BLOCK_STATE_QUEUED: Block is on the incoming queue * @IIO_BLOCK_STATE_ACTIVE: Block is currently being processed by the DMA * @IIO_BLOCK_STATE_DONE: Block is on the outgoing queue * @IIO_BLOCK_STATE_DEAD: Block has been marked as to be freed */ enum iio_block_state { - IIO_BLOCK_STATE_DEQUEUED, IIO_BLOCK_STATE_QUEUED, IIO_BLOCK_STATE_ACTIVE, IIO_BLOCK_STATE_DONE, @@ -73,12 +71,15 @@ struct iio_dma_buffer_block { * @active_block: Block being used in read() * @pos: Read offset in the active block * @block_size: Size of each block + * @next_dequeue: index of next block that will be dequeued */ struct iio_dma_buffer_queue_fileio { struct iio_dma_buffer_block *blocks[2]; struct iio_dma_buffer_block *active_block; size_t pos; size_t block_size; + + unsigned int next_dequeue; }; /** @@ -93,7 +94,6 @@ struct iio_dma_buffer_queue_fileio { * list and typically also a list of active blocks in the part that handles * the DMA controller * @incoming: List of buffers on the incoming queue - * @outgoing: List of buffers on the outgoing queue * @active: Whether the buffer is currently active * @fileio: FileIO state */ @@ -105,7 +105,6 @@ struct iio_dma_buffer_queue { struct mutex lock; spinlock_t list_lock; struct list_head incoming; - struct list_head outgoing; bool active; From patchwork Mon Aug 7 11:21:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 132039 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1476209vqr; Mon, 7 Aug 2023 07:08:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpyYT1yd8sOQKnkDp+7rhcSngPn37Prae3Ximi8wbWzYZZlA8t1W4923rJWin2oAr1opDt X-Received: by 2002:a17:902:f693:b0:1bb:7d2f:7c19 with SMTP id l19-20020a170902f69300b001bb7d2f7c19mr11378625plg.64.1691417288544; Mon, 07 Aug 2023 07:08:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691417288; cv=none; d=google.com; s=arc-20160816; b=cjAI61fY9+TJu+vZiOApDxEVUSTu3X/hBVi6R3LWn65AUgxAVXGZJlX688lGeIx6zY bYkr3FyuebPJwKWD38n1GnD47uneVpYgNSs/NSuq9X9o3+lWp8zXcD/tYWiQvRjp7Ce4 3eHRm8jiaVS67VnvxZtPR9A/nxSVGt7RsRH8TXtNt7Z8UXuqXMaVePQ674I6OZTL9Zi8 Yg2a853lSNbcN/GuuCYaDMU6Y2fuAKyuk7MGp8dLJBf4hHb6d1o6WLu84sB13udJ2Zrv N6eYasVItzI88n5Mz3R2IOEPXg0xy7hqftisxUAYUIHeRvgRrIAZ8u3aKYxaf5ezjEHB R2Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=a4pvTV8kiMTltEAHfywRlUh754oJc8Eu6vcVcqGcW6o=; fh=8W62y9fbWgtHcFWjUVBVy0E9D9qHeioTODDkGZpY+zc=; b=Nh5RsPwuep5mkztFycJxhEPnqxqY8nOBm4N+KsIc9ykONrYcr9+bEoRz/Uk2CzMOHY EPkgozNNuJfxFRy0pxR4GuRQlyNXHTA2fwFzoI2/pX1sixEBjvlgK5cwRnioRYdTQyg4 04XlJ7hgvcgMsQIgieg8AE2faxY7S2ZLhrVa61o5Tw6cGWsIJfgOMROJr+cXLdHLWKVJ X7u1Y0QG3YgxW53kYvKHmhkn2mDKDsjt/Xb/lvA2bu3Gbi9zX6NVf6g+3EOG8BK6VYto QhG/LUhqaFYyCeVqYuz++FF8RQblSnr4JGwKr3XEDVFwDXc4yUuEn65xmd2T4Q/xqfIO SFng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=vNMOj8Wv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ld6-20020a170902fac600b001aaf1734d1dsi5574325plb.241.2023.08.07.07.07.55; Mon, 07 Aug 2023 07:08:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=vNMOj8Wv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233121AbjHGL7g (ORCPT + 99 others); Mon, 7 Aug 2023 07:59:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231616AbjHGL7b (ORCPT ); Mon, 7 Aug 2023 07:59:31 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D78F128; Mon, 7 Aug 2023 04:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1691407285; 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=a4pvTV8kiMTltEAHfywRlUh754oJc8Eu6vcVcqGcW6o=; b=vNMOj8WvW3JYZFCNQkHed9zt3EIiSGmnH6DG+UsV3nA1279zp/fAc3pju07WHfGZ3POze4 ErgsnZo/QoSKaVz8xr1KDdBpBFhznDwTAnbGGMDtqAUSiF+mnuWiPjR5natrt6bKi1aj/w 7A3i5pA+MuDDtVATaSxwKtgxpa10nVI= From: Paul Cercueil To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Cercueil Subject: [PATCH v4 2/6] iio: buffer-dma: Rename iio_dma_buffer_data_available() Date: Mon, 7 Aug 2023 13:21:09 +0200 Message-Id: <20230807112113.47157-3-paul@crapouillou.net> In-Reply-To: <20230807112113.47157-1-paul@crapouillou.net> References: <20230807112113.47157-1-paul@crapouillou.net> MIME-Version: 1.0 X-Spam: Yes X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773579574517357964 X-GMAIL-MSGID: 1773579574517357964 Change its name to iio_dma_buffer_usage(), as this function can be used both for the .data_available and the .space_available callbacks. Signed-off-by: Paul Cercueil --- v4: New patch --- drivers/iio/buffer/industrialio-buffer-dma.c | 11 ++++++----- drivers/iio/buffer/industrialio-buffer-dmaengine.c | 2 +- include/linux/iio/buffer-dma.h | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dma.c b/drivers/iio/buffer/industrialio-buffer-dma.c index 1fc91467d1aa..764f1400a545 100644 --- a/drivers/iio/buffer/industrialio-buffer-dma.c +++ b/drivers/iio/buffer/industrialio-buffer-dma.c @@ -524,13 +524,14 @@ int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, EXPORT_SYMBOL_GPL(iio_dma_buffer_read); /** - * iio_dma_buffer_data_available() - DMA buffer data_available callback + * iio_dma_buffer_usage() - DMA buffer data_available and + * space_available callback * @buf: Buffer to check for data availability * - * Should be used as the data_available callback for iio_buffer_access_ops - * struct for DMA buffers. + * Should be used as the data_available and space_available callbacks for + * iio_buffer_access_ops struct for DMA buffers. */ -size_t iio_dma_buffer_data_available(struct iio_buffer *buf) +size_t iio_dma_buffer_usage(struct iio_buffer *buf) { struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buf); struct iio_dma_buffer_block *block; @@ -563,7 +564,7 @@ size_t iio_dma_buffer_data_available(struct iio_buffer *buf) return data_available; } -EXPORT_SYMBOL_GPL(iio_dma_buffer_data_available); +EXPORT_SYMBOL_GPL(iio_dma_buffer_usage); /** * iio_dma_buffer_set_bytes_per_datum() - DMA buffer set_bytes_per_datum callback diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/iio/buffer/industrialio-buffer-dmaengine.c index 5f85ba38e6f6..7b49f85af064 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -117,7 +117,7 @@ static const struct iio_buffer_access_funcs iio_dmaengine_buffer_ops = { .request_update = iio_dma_buffer_request_update, .enable = iio_dma_buffer_enable, .disable = iio_dma_buffer_disable, - .data_available = iio_dma_buffer_data_available, + .data_available = iio_dma_buffer_usage, .release = iio_dmaengine_buffer_release, .modes = INDIO_BUFFER_HARDWARE, diff --git a/include/linux/iio/buffer-dma.h b/include/linux/iio/buffer-dma.h index 18d3702fa95d..52a838ec0e57 100644 --- a/include/linux/iio/buffer-dma.h +++ b/include/linux/iio/buffer-dma.h @@ -132,7 +132,7 @@ int iio_dma_buffer_disable(struct iio_buffer *buffer, struct iio_dev *indio_dev); int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, char __user *user_buffer); -size_t iio_dma_buffer_data_available(struct iio_buffer *buffer); +size_t iio_dma_buffer_usage(struct iio_buffer *buffer); int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd); int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length); int iio_dma_buffer_request_update(struct iio_buffer *buffer); From patchwork Mon Aug 7 11:21:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 131954 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1438138vqr; Mon, 7 Aug 2023 06:12:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFtyvLx8Y3vb0JHHUcQ61MccE2dx/GAmMNFxJ8sCSEJZw0aMlh035bAG7YvIEVhI9RcNFyd X-Received: by 2002:a05:6a00:248c:b0:63d:3339:e967 with SMTP id c12-20020a056a00248c00b0063d3339e967mr8794355pfv.19.1691413957681; Mon, 07 Aug 2023 06:12:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691413957; cv=none; d=google.com; s=arc-20160816; b=aTPGSBjKghGFx9V0rSEk0K1tTsrANN1qj08rH/loz+mF+aca9T02RIeJhqZqT6eRqp fQTnMMsowG4KinJbDhpLTt4+xCK0NRX5BQr82/WVKG4uE6gnIBMOapY5DbqUKsKz/sy1 VrThEJGkDx0UmF795ZgFzSTrSPxQWq8CVFcskDhhX/pOGGnBt9TAA5Cb2EXg3FSjuQfK CSkNUdqyFeo47L4ZMt27l7I/Z5dKh+Xgkxq+npxi24ibE2YREFCVraZipvTFZfK49m1L CTd4zQSk3D5U8WstAaKRo1EvmSg2yzZr19X8yLTZafCyRyTv3h956pPmLb7Y01+NBZNu ROzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LwOcH+rQmHN7pp/8QW0qcI7HUzfKr2ilYJPW1SB38kQ=; fh=QagBC1ic3VcVFskRmEqUJZ5VlGz5jYfCtms1OQ7G2VM=; b=r3jjB1zhgMx7Nx5dksCajZfVgQsPUWX/fMlnXa1s+Uhmv1Q3pjSsZ4I2f716YparQm i0dfD9cHdDfS8ui/vf5ILAMprW1tG8EC9Ls1K1Y0dM36wZKKb1HctabLs/G12D4V7N9s Sx0ukORLtmdjmy6a9qu0TWwXRqEkoI2ewow7VM/VPdhTmVBvx/M+NBt1j/qngnreNQWL B8U92U7h61Vr8bTA2qwG184V7jOQ/IiFFetnscc2bqV3/XxPRhGVzvFtTLoXPDC39sRY hwEKiADbUdcmoYjsAnOH3HYbReKIAVqn+VJlVy7kkWcg1hYgquFQh3ljfY/GveoTeKeo B5bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=Ez1ku2zW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv8-20020a056a00414800b006870a9e03e1si5744874pfb.47.2023.08.07.06.12.24; Mon, 07 Aug 2023 06:12:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=Ez1ku2zW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233103AbjHGMAM (ORCPT + 99 others); Mon, 7 Aug 2023 08:00:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232757AbjHGMAJ (ORCPT ); Mon, 7 Aug 2023 08:00:09 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20E2F199D; Mon, 7 Aug 2023 04:59:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1691407286; 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=LwOcH+rQmHN7pp/8QW0qcI7HUzfKr2ilYJPW1SB38kQ=; b=Ez1ku2zWi6MNM8/Xab4rEXTK/WNj8NQXkor16yAO9hVm6dxuj78PuQIsXpCgP8q0Y11Ttv yCJljsmej+ZUyy9G2v+WT/cFSG24w3fy7BBLF7ABkFQufIOFGJAy3nLSYy1tIKJ2HfbjJw QBO0XxH/MYKdyYbYFAvPJ+rGLKWKNO8= From: Paul Cercueil To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Cercueil , Alexandru Ardelean Subject: [PATCH v4 3/6] iio: buffer-dma: Enable buffer write support Date: Mon, 7 Aug 2023 13:21:10 +0200 Message-Id: <20230807112113.47157-4-paul@crapouillou.net> In-Reply-To: <20230807112113.47157-1-paul@crapouillou.net> References: <20230807112113.47157-1-paul@crapouillou.net> MIME-Version: 1.0 X-Spam: Yes X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773576082040115992 X-GMAIL-MSGID: 1773576082040115992 Adding write support to the buffer-dma code is easy - the write() function basically needs to do the exact same thing as the read() function: dequeue a block, read or write the data, enqueue the block when entirely processed. Therefore, the iio_buffer_dma_read() and the new iio_buffer_dma_write() now both call a function iio_buffer_dma_io(), which will perform this task. Note that we preemptively reset block->bytes_used to the buffer's size in iio_dma_buffer_request_update(), as in the future the iio_dma_buffer_enqueue() function won't reset it. Signed-off-by: Paul Cercueil Reviewed-by: Alexandru Ardelean --- v2: - Fix block->state not being reset in iio_dma_buffer_request_update() for output buffers. - Only update block->bytes_used once and add a comment about why we update it. - Add a comment about why we're setting a different state for output buffers in iio_dma_buffer_request_update() - Remove useless cast to bool (!!) in iio_dma_buffer_io() v3: - Reorganize arguments to iio_dma_buffer_io() - Change 'is_write' argument to 'is_from_user' - Change (__force char *) to (__force __user char *), in iio_dma_buffer_write(), since we only want to drop the "const". v4: Drop iio_dma_buffer_space_available() function, and update patch description accordingly. --- drivers/iio/buffer/industrialio-buffer-dma.c | 89 ++++++++++++++++---- include/linux/iio/buffer-dma.h | 2 + 2 files changed, 75 insertions(+), 16 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dma.c b/drivers/iio/buffer/industrialio-buffer-dma.c index 764f1400a545..e00b704941b6 100644 --- a/drivers/iio/buffer/industrialio-buffer-dma.c +++ b/drivers/iio/buffer/industrialio-buffer-dma.c @@ -195,6 +195,18 @@ static void _iio_dma_buffer_block_done(struct iio_dma_buffer_block *block) block->state = IIO_BLOCK_STATE_DONE; } +static void iio_dma_buffer_queue_wake(struct iio_dma_buffer_queue *queue) +{ + __poll_t flags; + + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) + flags = EPOLLIN | EPOLLRDNORM; + else + flags = EPOLLOUT | EPOLLWRNORM; + + wake_up_interruptible_poll(&queue->buffer.pollq, flags); +} + /** * iio_dma_buffer_block_done() - Indicate that a block has been completed * @block: The completed block @@ -212,7 +224,7 @@ void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block) spin_unlock_irqrestore(&queue->list_lock, flags); iio_buffer_block_put_atomic(block); - wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM); + iio_dma_buffer_queue_wake(queue); } EXPORT_SYMBOL_GPL(iio_dma_buffer_block_done); @@ -241,7 +253,7 @@ void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue, } spin_unlock_irqrestore(&queue->list_lock, flags); - wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM); + iio_dma_buffer_queue_wake(queue); } EXPORT_SYMBOL_GPL(iio_dma_buffer_block_list_abort); @@ -335,8 +347,24 @@ int iio_dma_buffer_request_update(struct iio_buffer *buffer) queue->fileio.blocks[i] = block; } - block->state = IIO_BLOCK_STATE_QUEUED; - list_add_tail(&block->head, &queue->incoming); + /* + * block->bytes_used may have been modified previously, e.g. by + * iio_dma_buffer_block_list_abort(). Reset it here to the + * block's so that iio_dma_buffer_io() will work. + */ + block->bytes_used = block->size; + + /* + * If it's an input buffer, mark the block as queued, and + * iio_dma_buffer_enable() will submit it. Otherwise mark it as + * done, which means it's ready to be dequeued. + */ + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) { + block->state = IIO_BLOCK_STATE_QUEUED; + list_add_tail(&block->head, &queue->incoming); + } else { + block->state = IIO_BLOCK_STATE_DONE; + } } out_unlock: @@ -465,20 +493,12 @@ static struct iio_dma_buffer_block *iio_dma_buffer_dequeue( return block; } -/** - * iio_dma_buffer_read() - DMA buffer read callback - * @buffer: Buffer to read form - * @n: Number of bytes to read - * @user_buffer: Userspace buffer to copy the data to - * - * Should be used as the read callback for iio_buffer_access_ops - * struct for DMA buffers. - */ -int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, - char __user *user_buffer) +static int iio_dma_buffer_io(struct iio_buffer *buffer, size_t n, + char __user *user_buffer, bool is_from_user) { struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer); struct iio_dma_buffer_block *block; + void *addr; int ret; if (n < buffer->bytes_per_datum) @@ -501,8 +521,13 @@ int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, n = rounddown(n, buffer->bytes_per_datum); if (n > block->bytes_used - queue->fileio.pos) n = block->bytes_used - queue->fileio.pos; + addr = block->vaddr + queue->fileio.pos; - if (copy_to_user(user_buffer, block->vaddr + queue->fileio.pos, n)) { + if (is_from_user) + ret = copy_from_user(addr, user_buffer, n); + else + ret = copy_to_user(user_buffer, addr, n); + if (ret) { ret = -EFAULT; goto out_unlock; } @@ -521,8 +546,40 @@ int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, return ret; } + +/** + * iio_dma_buffer_read() - DMA buffer read callback + * @buffer: Buffer to read form + * @n: Number of bytes to read + * @user_buffer: Userspace buffer to copy the data to + * + * Should be used as the read callback for iio_buffer_access_ops + * struct for DMA buffers. + */ +int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, + char __user *user_buffer) +{ + return iio_dma_buffer_io(buffer, n, user_buffer, false); +} EXPORT_SYMBOL_GPL(iio_dma_buffer_read); +/** + * iio_dma_buffer_write() - DMA buffer write callback + * @buffer: Buffer to read form + * @n: Number of bytes to read + * @user_buffer: Userspace buffer to copy the data from + * + * Should be used as the write callback for iio_buffer_access_ops + * struct for DMA buffers. + */ +int iio_dma_buffer_write(struct iio_buffer *buffer, size_t n, + const char __user *user_buffer) +{ + return iio_dma_buffer_io(buffer, n, + (__force __user char *)user_buffer, true); +} +EXPORT_SYMBOL_GPL(iio_dma_buffer_write); + /** * iio_dma_buffer_usage() - DMA buffer data_available and * space_available callback diff --git a/include/linux/iio/buffer-dma.h b/include/linux/iio/buffer-dma.h index 52a838ec0e57..6e27e47077d5 100644 --- a/include/linux/iio/buffer-dma.h +++ b/include/linux/iio/buffer-dma.h @@ -132,6 +132,8 @@ int iio_dma_buffer_disable(struct iio_buffer *buffer, struct iio_dev *indio_dev); int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, char __user *user_buffer); +int iio_dma_buffer_write(struct iio_buffer *buffer, size_t n, + const char __user *user_buffer); size_t iio_dma_buffer_usage(struct iio_buffer *buffer); int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd); int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length); From patchwork Mon Aug 7 11:21:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 131929 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1421161vqr; Mon, 7 Aug 2023 05:46:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGX8AIEaM6kHAxHAOdvJlNAxpN8P913i/lbUscDiAcrS80reTImjJZXs4+lOc4KhxrF4G8H X-Received: by 2002:a05:6a20:728f:b0:12f:952:11fb with SMTP id o15-20020a056a20728f00b0012f095211fbmr10108109pzk.52.1691412402070; Mon, 07 Aug 2023 05:46:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691412402; cv=none; d=google.com; s=arc-20160816; b=QxW8rK28LqfHLkPc7zH3blnydyFppW+Azu++hdAHDrxyqFB7m5IjBzLeuLA/dHS9IG brYU/jnU1k+z4Pv7z6im4rqxctVgdKi9GIO08Aay4Ag1LXVOaKSZb9eQEcKzPVojthXp s/2TMUxhPRM8TWwEWEFwDUe8nfboK6L9+rZLmKx+eN4mst/QEu9AO5REppC4rWlL91+k vJm74ZNbtSESjBs2K2kIlbc/oJ5u1Cf6ZLNQL7wXrudtj1a4NePPBsBotfg6UgUFBEMQ 0Ze+Z6cReeOK55YR6O0SogNr4kJkxCh1FdhAaDdi3GRz40Hi+AjNw6zA/Bo3ZWuCuBMA PCVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=59RJdK1+ws41tdfO+j5haJfTuP5dQDka8NHKQcBec5s=; fh=mCe5gZDj94snffp2ksEdJ6u7mYRVf8yu2E76ZWZiUxI=; b=S8tg5Q3jfdFJgzjHjHHlKRXVd/B5i1L5mxTZ+viQn1Oh4tZ9fhUfG61q4jYdTh1B2T PSnzU/yi/QgOGE4GBvfFvf3FFfNQJ3qNTXiV3FwwJi2jC4rK7gIEEicKMRORLI9L53Fe 2gFlz/leSy4QUFE0gqieJ1pxlbcVhSJSxGyuvCrVxEbEa2dB5XuSD3VtS1ip/4nDe+2L CyteZp8bioMYI/eLM6Id3o8rQOszj8SL7pH1mweqEnpcCNC9Hp9cJTxiKUa4rovnqAXX TgKbYU2m8cZIrvtUNJteoXbxUbyAwGR8UiZ3v9VNgaI/E0SFWp2D3T904bzl7Z/oQ4Mw psuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b="Xp/+pStH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w186-20020a6382c3000000b00563deb65f94si420019pgd.192.2023.08.07.05.46.28; Mon, 07 Aug 2023 05:46:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b="Xp/+pStH"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233067AbjHGL7X (ORCPT + 99 others); Mon, 7 Aug 2023 07:59:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233042AbjHGL7U (ORCPT ); Mon, 7 Aug 2023 07:59:20 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 399FD185; Mon, 7 Aug 2023 04:59:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1691407287; 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=59RJdK1+ws41tdfO+j5haJfTuP5dQDka8NHKQcBec5s=; b=Xp/+pStHF2iwSJUuwoX0amoG1nRS8h1avom4TtbjUHj3tOYKsP8CqURY8RLFL9lRHPaT5L tEvKdOUPpcaBah9jfwP1B3p0yhc80llZIWkoiG2zz4OPtNoyrOqG9I9ydn4+HRG9G2Hw+r 1ebcHk9R59aGnPTnnAsMhcbIoSYyVj4= From: Paul Cercueil To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandru Ardelean , Alexandru Ardelean , Paul Cercueil Subject: [PATCH v4 4/6] iio: buffer-dma: split iio_dma_buffer_fileio_free() function Date: Mon, 7 Aug 2023 13:21:11 +0200 Message-Id: <20230807112113.47157-5-paul@crapouillou.net> In-Reply-To: <20230807112113.47157-1-paul@crapouillou.net> References: <20230807112113.47157-1-paul@crapouillou.net> MIME-Version: 1.0 X-Spam: Yes X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773574450586900631 X-GMAIL-MSGID: 1773574450586900631 From: Alexandru Ardelean This change splits the logic into a separate function, which will be re-used later. Signed-off-by: Alexandru Ardelean Cc: Alexandru Ardelean Signed-off-by: Paul Cercueil --- drivers/iio/buffer/industrialio-buffer-dma.c | 43 +++++++++++--------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/drivers/iio/buffer/industrialio-buffer-dma.c b/drivers/iio/buffer/industrialio-buffer-dma.c index e00b704941b6..29cc3083cb7e 100644 --- a/drivers/iio/buffer/industrialio-buffer-dma.c +++ b/drivers/iio/buffer/industrialio-buffer-dma.c @@ -374,6 +374,29 @@ int iio_dma_buffer_request_update(struct iio_buffer *buffer) } EXPORT_SYMBOL_GPL(iio_dma_buffer_request_update); +static void iio_dma_buffer_fileio_free(struct iio_dma_buffer_queue *queue) +{ + unsigned int i; + + spin_lock_irq(&queue->list_lock); + for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { + if (!queue->fileio.blocks[i]) + continue; + queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD; + } + spin_unlock_irq(&queue->list_lock); + + INIT_LIST_HEAD(&queue->incoming); + + for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { + if (!queue->fileio.blocks[i]) + continue; + iio_buffer_block_put(queue->fileio.blocks[i]); + queue->fileio.blocks[i] = NULL; + } + queue->fileio.active_block = NULL; +} + static void iio_dma_buffer_submit_block(struct iio_dma_buffer_queue *queue, struct iio_dma_buffer_block *block) { @@ -696,27 +719,9 @@ EXPORT_SYMBOL_GPL(iio_dma_buffer_init); */ void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue) { - unsigned int i; - mutex_lock(&queue->lock); - spin_lock_irq(&queue->list_lock); - for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { - if (!queue->fileio.blocks[i]) - continue; - queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD; - } - spin_unlock_irq(&queue->list_lock); - - INIT_LIST_HEAD(&queue->incoming); - - for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) { - if (!queue->fileio.blocks[i]) - continue; - iio_buffer_block_put(queue->fileio.blocks[i]); - queue->fileio.blocks[i] = NULL; - } - queue->fileio.active_block = NULL; + iio_dma_buffer_fileio_free(queue); queue->ops = NULL; mutex_unlock(&queue->lock); From patchwork Mon Aug 7 11:21:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 132155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1524100vqr; Mon, 7 Aug 2023 08:21:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGreiUjj8iYrPWLSWeYhvwdzE/cWy3PK9RV35qU9I75cpI4wMv0jm6QjK/zWQ+1QaHOqJbu X-Received: by 2002:a17:903:182:b0:1b8:b382:f6c3 with SMTP id z2-20020a170903018200b001b8b382f6c3mr9817130plg.13.1691421692941; Mon, 07 Aug 2023 08:21:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691421692; cv=none; d=google.com; s=arc-20160816; b=s3Icl6Cmpa0Srt6RHZM64mqrQ2qTzl1FBvWm0dHgNp8Pmf+CngH2unYUxaj8VflZaA OR/bbRcv1OfwLxHKq616U4Ijs8TL9fDVbB33U6Kw2/cEXdIHFQPUeRDbkChAO/MVXSY+ L1Nrs2cojkDuk8M9DuQEbKqTzmbTqattL0Pms2I9cgP6vHHSAIqncd5i7utxL1GUzg1K e1KupG/gmddNmngFz0r5Z+FeTSn8KhbePn75UyOvLea3AumD5h5j4wRRfv8Z6A9Fievv z20UeoR7KJjL+6r18cpwrXuh1CO8FsdBwcmEx8YExfp5TG73YRYoa1VhCrgUxyX/NiYQ oKMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zaX0JNZAl6IZc89GKARBAqn5r+/uiF51q0xYvvqR4EE=; fh=QagBC1ic3VcVFskRmEqUJZ5VlGz5jYfCtms1OQ7G2VM=; b=aJ52sV+ODuq3s8OQ3QsrvAyoBG/elLm4MlL42tkpKRCMI5ud3BcudhsYEtyjb7mlUe oth7W60gWftUUAg9jO26uXcZoMMNyMOf8AjP3rYcohesM32S7gkoeKm/vBO5N16t2yv/ T3nmCibjRgMlIjSTzIvNvBJ6vZq5M4rL64BnHVjX96rk6AEpy75E+eJIOcHoIeLoOGi7 rjymWdfshsLrylEayeo4veWVAvRIn5zbV1lGSq1zRDf5zSO6h4k4HxSwK7RGre4Jx2gf MBAxrtJO96bG2iCO/MBuKW+CXW2gEbqT1aReRwuWc3PJRWVLR/p8YZBP8qTofR7AUalD M+UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=oLUAGrNH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x15-20020a170902ec8f00b001b8ae8ed8c3si6087324plg.535.2023.08.07.08.21.18; Mon, 07 Aug 2023 08:21:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=oLUAGrNH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233159AbjHGMBI (ORCPT + 99 others); Mon, 7 Aug 2023 08:01:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233163AbjHGMAt (ORCPT ); Mon, 7 Aug 2023 08:00:49 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 852D6128; Mon, 7 Aug 2023 05:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1691407287; 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=zaX0JNZAl6IZc89GKARBAqn5r+/uiF51q0xYvvqR4EE=; b=oLUAGrNHY0ZX2uhcnw1pz+EqM9Uh8XUsKVa3QJkD8Mqip0BUlz2WooZOheQ4vaO7rRCTfR M1SKXkuvV6lz0lsW7aQP7zpbEMnM6R7agGirbACo8TeLrtKnokb1s0pnRNh0MqbtNqPJOx bOJdzoda8wBIx8IUq2TZ9GBRS9B4JVA= From: Paul Cercueil To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Cercueil , Alexandru Ardelean Subject: [PATCH v4 5/6] iio: buffer-dmaengine: Support specifying buffer direction Date: Mon, 7 Aug 2023 13:21:12 +0200 Message-Id: <20230807112113.47157-6-paul@crapouillou.net> In-Reply-To: <20230807112113.47157-1-paul@crapouillou.net> References: <20230807112113.47157-1-paul@crapouillou.net> MIME-Version: 1.0 X-Spam: Yes X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773584192768346246 X-GMAIL-MSGID: 1773584192768346246 Update the devm_iio_dmaengine_buffer_setup() function to support specifying the buffer direction. Update the iio_dmaengine_buffer_submit() function to handle input buffers as well as output buffers. Signed-off-by: Paul Cercueil Reviewed-by: Alexandru Ardelean --- drivers/iio/adc/adi-axi-adc.c | 3 ++- .../buffer/industrialio-buffer-dmaengine.c | 24 +++++++++++++++---- include/linux/iio/buffer-dmaengine.h | 5 +++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/drivers/iio/adc/adi-axi-adc.c b/drivers/iio/adc/adi-axi-adc.c index e8a8ea4140f1..d33574b5417a 100644 --- a/drivers/iio/adc/adi-axi-adc.c +++ b/drivers/iio/adc/adi-axi-adc.c @@ -114,7 +114,8 @@ static int adi_axi_adc_config_dma_buffer(struct device *dev, dma_name = "rx"; return devm_iio_dmaengine_buffer_setup(indio_dev->dev.parent, - indio_dev, dma_name); + indio_dev, dma_name, + IIO_BUFFER_DIRECTION_IN); } static int adi_axi_adc_read_raw(struct iio_dev *indio_dev, diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/iio/buffer/industrialio-buffer-dmaengine.c index 7b49f85af064..deae5a4ac03d 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -64,14 +64,25 @@ 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; + enum dma_transfer_direction dma_dir; + size_t max_size; dma_cookie_t cookie; - block->bytes_used = min(block->size, dmaengine_buffer->max_size); - block->bytes_used = round_down(block->bytes_used, - dmaengine_buffer->align); + 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; + dma_dir = DMA_DEV_TO_MEM; + } else { + dma_dir = DMA_MEM_TO_DEV; + } + + 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_DEV_TO_MEM, + block->phys_addr, block->bytes_used, dma_dir, DMA_PREP_INTERRUPT); if (!desc) return -ENOMEM; @@ -275,7 +286,8 @@ static struct iio_buffer *devm_iio_dmaengine_buffer_alloc(struct device *dev, */ int devm_iio_dmaengine_buffer_setup(struct device *dev, struct iio_dev *indio_dev, - const char *channel) + const char *channel, + enum iio_buffer_direction dir) { struct iio_buffer *buffer; @@ -286,6 +298,8 @@ int devm_iio_dmaengine_buffer_setup(struct device *dev, indio_dev->modes |= INDIO_BUFFER_HARDWARE; + buffer->direction = dir; + return iio_device_attach_buffer(indio_dev, buffer); } EXPORT_SYMBOL_GPL(devm_iio_dmaengine_buffer_setup); diff --git a/include/linux/iio/buffer-dmaengine.h b/include/linux/iio/buffer-dmaengine.h index 5c355be89814..538d0479cdd6 100644 --- a/include/linux/iio/buffer-dmaengine.h +++ b/include/linux/iio/buffer-dmaengine.h @@ -7,11 +7,14 @@ #ifndef __IIO_DMAENGINE_H__ #define __IIO_DMAENGINE_H__ +#include + struct iio_dev; struct device; int devm_iio_dmaengine_buffer_setup(struct device *dev, struct iio_dev *indio_dev, - const char *channel); + const char *channel, + enum iio_buffer_direction dir); #endif From patchwork Mon Aug 7 11:21:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 132042 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1477494vqr; Mon, 7 Aug 2023 07:09:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH/yehYRcrNQaZ5QMTro8r91wgpxUYIgcnfZrQgWBD8GJXUiZjGehd4gkXkb3xfYhHL7H23 X-Received: by 2002:a05:6a20:38c:b0:122:cb38:636a with SMTP id 12-20020a056a20038c00b00122cb38636amr7377064pzt.33.1691417388503; Mon, 07 Aug 2023 07:09:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691417388; cv=none; d=google.com; s=arc-20160816; b=pr9qN2yWT11X36GZv5gY1v+jR+2Snhl5Vzl0ZxM/fz+2nffRVy09jGEwrJi+KzqsJf JEsSQ55dB9tyOFcVxldb3Mii7tOiKmTiJL9hoH53x9DPt62K+tu780ABIYyUjRw7TEfo yVnza8UvGF4dc7tqwEirxes7QGrX5ewcv5TEBitkh+hz0Gv8ZN1bt3RWT8OehsBMxKBE JL6jej/6L1376ywDLG/wUED8baglJSd7nkkl+KbRP+IMFz4gJpQjPblK17hy5vKWhRII VkrkWLdLx58nAg25BYCJme4iArdpmgYTwVFrUDlHwRjs5yyrVJ9AOIU/Qoj4Uz0wLx5i 8wGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=CjBcI7MOePyRNzsw/XKYD6tqM+t/1zi/Fz/Jzvd8lMc=; fh=QagBC1ic3VcVFskRmEqUJZ5VlGz5jYfCtms1OQ7G2VM=; b=N4pjHvKJW+Adhkm55CeQ9M657Ag3cpcf1lOOS7rfyQYTvVUFmREOI+MHm+66kK2Q0y 0Mfa+7ocSLmqnmhMpLGXeJ+3e8zWATyMAew9ecQ2p4q++lB2RdsXKh9pq9WZBS1M9bax KsKlGBffGXhJIal9DLUgYt2/G3ZkgkRP+KHWb229KzHFdxdkWZoAMbHSoj39gGaTw1mX NILSe0XykYtjfCMHkdH86X9Btlg5UdrPCL82/PTRrCR7/ZznlsUbsxv9mfU3CNdvYHtv hvXfy4zM7CZ/AXmH2yesFWp1GfvU4YKaNkkllm/MIz+gOQNxMWcVW0iKSwThPMwqevqs Wjkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=qpQpIBV9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y16-20020a056a00191000b006875bd6d8d9si5704473pfi.169.2023.08.07.07.09.35; Mon, 07 Aug 2023 07:09:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@crapouillou.net header.s=mail header.b=qpQpIBV9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232950AbjHGMAf (ORCPT + 99 others); Mon, 7 Aug 2023 08:00:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231436AbjHGMAc (ORCPT ); Mon, 7 Aug 2023 08:00:32 -0400 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCC30E5A; Mon, 7 Aug 2023 05:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1691407288; 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=CjBcI7MOePyRNzsw/XKYD6tqM+t/1zi/Fz/Jzvd8lMc=; b=qpQpIBV9gcJaLi7D/3d4QrQuOSYVMlgPvk68Fj4GQKkMmkBL8omXdJCzE5F4U+YrxwwntZ /NJfKOn0CfOyiXhwkaeqZR+kXwBB0g9P9PFxK4IiSsLqzX6h6BL4HAW3BgB9dEulvLyEH5 sKTlk51M3TdtW1wurLRJj9H9w7szE9U= From: Paul Cercueil To: Jonathan Cameron Cc: Lars-Peter Clausen , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Cercueil , Alexandru Ardelean Subject: [PATCH v4 6/6] iio: buffer-dmaengine: Enable write support Date: Mon, 7 Aug 2023 13:21:13 +0200 Message-Id: <20230807112113.47157-7-paul@crapouillou.net> In-Reply-To: <20230807112113.47157-1-paul@crapouillou.net> References: <20230807112113.47157-1-paul@crapouillou.net> MIME-Version: 1.0 X-Spam: Yes X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773579679332697998 X-GMAIL-MSGID: 1773579679332697998 Use the iio_dma_buffer_write() and iio_dma_buffer_space_available() functions provided by the buffer-dma core, to enable write support in the buffer-dmaengine code. Signed-off-by: Paul Cercueil Reviewed-by: Alexandru Ardelean --- v4: .space_available is now set to iio_dma_buffer_usage (which is functionally the exact same). --- drivers/iio/buffer/industrialio-buffer-dmaengine.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/iio/buffer/industrialio-buffer-dmaengine.c b/drivers/iio/buffer/industrialio-buffer-dmaengine.c index deae5a4ac03d..ef9d890ed3c9 100644 --- a/drivers/iio/buffer/industrialio-buffer-dmaengine.c +++ b/drivers/iio/buffer/industrialio-buffer-dmaengine.c @@ -123,12 +123,14 @@ static void iio_dmaengine_buffer_release(struct iio_buffer *buf) static const struct iio_buffer_access_funcs iio_dmaengine_buffer_ops = { .read = iio_dma_buffer_read, + .write = iio_dma_buffer_write, .set_bytes_per_datum = iio_dma_buffer_set_bytes_per_datum, .set_length = iio_dma_buffer_set_length, .request_update = iio_dma_buffer_request_update, .enable = iio_dma_buffer_enable, .disable = iio_dma_buffer_disable, .data_available = iio_dma_buffer_usage, + .space_available = iio_dma_buffer_usage, .release = iio_dmaengine_buffer_release, .modes = INDIO_BUFFER_HARDWARE,