Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4980867pxb; Mon, 15 Feb 2021 06:34:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJyprmtQb1GvB8PCiRPBTLvgt9t5Y+dnIXybV5lJZy6eNhk83bS0YA/p26/FH8r1D5fSyDLf X-Received: by 2002:aa7:d54a:: with SMTP id u10mr10337604edr.316.1613399653609; Mon, 15 Feb 2021 06:34:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613399653; cv=none; d=google.com; s=arc-20160816; b=0arZ7w9GAAqApWvIGK3N4ADXl8xyS44NnxhHchl/gUFKQH8q7uZZ1a/NPP+1VDYpKd Ib9UacVR7agEFt3/7wYPBMr+eYcVK0DglfB8nTzJ06zW3FF4AABxp1JSn/alE9d7haMg qtllWEahquLfM8pznp+klipivWI1AhM0FEePyUIBXAJnLgFuJ46uaKjUA+0OLW2HBOyM CPVPGmWG3Za9U3xCWxHHrYXjAVRmPOk98j5v2rDbMtEqZZG3oXkfGA62yxIYrcVueH5X fKz3/C6W0g313ClqnIWQRhX5OGDASoyOse/mxlIlCEG4EBADyNH03hbBHtewby4Amxqn u+pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=x8/iADeoBv/M1fSOKvBMoQ0b63OXmo/xzUa//kz/sI0=; b=h6BND0n2+Y8kCrAwI0XmSjjZ0fgFYygvTO4FwkZSRYb8+P4X0YQxMAXjnMAoWlrdty evCcMQveTtmgeIr70p4+tGWBfJwmILOO/ZJE2gaDAR+rjsTIX9C7d7X36w+8S8By3c2B 0Wi+XnL9LGHFQY1C3Ui2wZ5wM+lxqeaoR/2fVShJcFivSGtkldKAge2IJ0P6VsEpWZcO GLApv5xNJOwPLLM0BhMTNFrk2f/FWVYCStIiqogha0xAuTIaHnOsIABbiI32qpOekwhb ZRV2TPQ7jHTfvGJv+3FcvJLVVuSCPmAgy39r+gFvASuO2N/aEtiGHpD3hMiDKrfh7n3a 7Pmg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=analog.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m23si12490889ejc.322.2021.02.15.06.33.49; Mon, 15 Feb 2021 06:34:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=analog.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230133AbhBOOby (ORCPT + 99 others); Mon, 15 Feb 2021 09:31:54 -0500 Received: from mx0a-00128a01.pphosted.com ([148.163.135.77]:44990 "EHLO mx0a-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229933AbhBOObU (ORCPT ); Mon, 15 Feb 2021 09:31:20 -0500 Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 11FEJl1t031251; Mon, 15 Feb 2021 09:30:28 -0500 Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com with ESMTP id 36pcjamst1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 15 Feb 2021 09:30:28 -0500 Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 11FEURKB011830 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=FAIL); Mon, 15 Feb 2021 09:30:27 -0500 Received: from ASHBCASHYB5.ad.analog.com (10.64.17.133) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Mon, 15 Feb 2021 09:30:26 -0500 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBCASHYB5.ad.analog.com (10.64.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.721.2; Mon, 15 Feb 2021 09:30:26 -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.721.2 via Frontend Transport; Mon, 15 Feb 2021 09:30:26 -0500 Received: from localhost.localdomain ([10.48.65.12]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 11FEUGbm027532; Mon, 15 Feb 2021 09:30:24 -0500 From: Alexandru Ardelean To: , CC: , , , , , Alexandru Ardelean Subject: [PATCH v3 5/5] tools: iio: add example for high-speed buffer support Date: Mon, 15 Feb 2021 16:32:34 +0200 Message-ID: <20210215143234.3248-6-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210215143234.3248-1-alexandru.ardelean@analog.com> References: <20210215143234.3248-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 Content-Type: text/plain X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-02-15_08:2021-02-12,2021-02-15 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 priorityscore=1501 impostorscore=0 spamscore=0 bulkscore=0 mlxscore=0 malwarescore=0 suspectscore=0 adultscore=0 clxscore=1015 phishscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102150116 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Following a recent update to the IIO buffer infrastructure, this change adds a basic example on how to access an IIO buffer via the new mmap() interface. The ioctl() for the high-speed mode needs to be enabled right from the start, before setting any parameters via sysfs (length, enable, etc), to make sure that the mmap mode is used and not the fileio mode. Signed-off-by: Alexandru Ardelean --- tools/iio/iio_generic_buffer.c | 184 +++++++++++++++++++++++++++++++-- 1 file changed, 178 insertions(+), 6 deletions(-) diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c index 2491c54a5e4f..99dcc3b339b1 100644 --- a/tools/iio/iio_generic_buffer.c +++ b/tools/iio/iio_generic_buffer.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "iio_utils.h" @@ -239,6 +240,133 @@ static int enable_disable_all_channels(char *dev_dir_name, int buffer_idx, int e return 0; } +struct mmap_block { + struct iio_buffer_block block; + void *addr; +}; + +static struct mmap_block *enable_high_speed(int buf_fd, unsigned int block_size, + int nblocks) +{ + struct iio_buffer_block_alloc_req req = { 0 }; + struct mmap_block *mmaps = NULL; + int mmaps_cnt = 0; + int i, ret; + + /** + * Validate we can do high-speed by issuing BLOCK_FREE ioctl. + * If using just BLOCK_ALLOC it's distinguish between ENOSYS + * and other error types. + */ + ret = ioctl(buf_fd, IIO_BUFFER_BLOCK_FREE_IOCTL, 0); + if (ret < 0) { + errno = ENOSYS; + return NULL; + } + + /* for now, this */ + req.id = 0; + req.type = 0; + req.size = block_size; + req.count = nblocks; + + ret = ioctl(buf_fd, IIO_BUFFER_BLOCK_ALLOC_IOCTL, &req); + if (ret < 0) + return NULL; + + if (req.count == 0) { + errno = ENOMEM; + return NULL; + } + + if (req.count < nblocks) { + fprintf(stderr, "Requested %d blocks, got %d\n", + nblocks, req.count); + errno = ENOMEM; + return NULL; + } + + mmaps = calloc(req.count, sizeof(*mmaps)); + if (!mmaps) { + errno = ENOMEM; + return NULL; + } + + for (i = 0; i < req.count; i++) { + mmaps[i].block.id = i; + ret = ioctl(buf_fd, IIO_BUFFER_BLOCK_QUERY_IOCTL, &mmaps[i].block); + if (ret < 0) + goto error; + + ret = ioctl(buf_fd, IIO_BUFFER_BLOCK_ENQUEUE_IOCTL, &mmaps[i].block); + if (ret < 0) + goto error; + + mmaps[i].addr = mmap(0, mmaps[i].block.size, + PROT_READ | PROT_WRITE, MAP_SHARED, + buf_fd, mmaps[i].block.data.offset); + + if (mmaps[i].addr == MAP_FAILED) + goto error; + + mmaps_cnt++; + } + + return mmaps; + +error: + for (i = 0; i < mmaps_cnt; i++) + munmap(mmaps[i].addr, mmaps[i].block.size); + free(mmaps); + ioctl(buf_fd, IIO_BUFFER_BLOCK_FREE_IOCTL, 0); + return NULL; +} + +static int read_high_speed(int buf_fd, char *data, unsigned int block_size, + struct mmap_block *mmaps, unsigned int mmaps_cnt) +{ + struct iio_buffer_block block; + int ret; + + /** + * This is where some buffer-pool management can do wonders, + * but for the sake of this sample-code, we're just going to + * copy the data and re-enqueue it back + */ + memset(&block, 0, sizeof(block)); + ret = ioctl(buf_fd, IIO_BUFFER_BLOCK_DEQUEUE_IOCTL, &block); + if (ret < 0) + return ret; + + /* check for weird conditions */ + if (block.bytes_used > block_size) { + fprintf(stderr, + "Got a bigger block (%u) than expected (%u)\n", + block.bytes_used, block_size); + return -EFBIG; + } + + if (block.bytes_used < block_size) { + /** + * This can be normal, with some real-world data + * terminating abruptly. But log it. + */ + fprintf(stderr, + "Got a smaller block (%u) than expected (%u)\n", + block.bytes_used, block_size); + } + + /* memcpy() the data, we lose some more performance here :p */ + memcpy(data, mmaps[block.id].addr, block.bytes_used); + + /* and re-queue this back */ + ret = ioctl(buf_fd, IIO_BUFFER_BLOCK_ENQUEUE_IOCTL, &mmaps[block.id].block); + if (ret < 0) + return ret; + + return block.bytes_used; +} + static void print_usage(void) { fprintf(stderr, "Usage: generic_buffer [options]...\n" @@ -249,6 +377,7 @@ static void print_usage(void) " -c Do n conversions, or loop forever if n < 0\n" " -e Disable wait for event (new data)\n" " -g Use trigger-less mode\n" + " -h Use high-speed buffer access\n" " -l Set buffer length to n samples\n" " --device-name -n \n" " --device-num -N \n" @@ -356,9 +485,15 @@ int main(int argc, char **argv) struct iio_channel_info *channels = NULL; + static bool use_high_speed = false; + unsigned int block_size; + int nblocks = 16; /* default */ + int mmaps_cnt = 0; + struct mmap_block *mmaps = NULL; + register_cleanup(); - while ((c = getopt_long(argc, argv, "aAb:c:egl:n:N:t:T:w:?", longopts, + while ((c = getopt_long(argc, argv, "aAb:c:eghl:n:N:t:T:w:?", longopts, NULL)) != -1) { switch (c) { case 'a': @@ -396,6 +531,9 @@ int main(int argc, char **argv) case 'g': notrigger = 1; break; + case 'h': + use_high_speed = true; + break; case 'l': errno = 0; buf_len = strtoul(optarg, &dummy, 10); @@ -659,6 +797,29 @@ int main(int argc, char **argv) goto error; } + scan_size = size_from_channelarray(channels, num_channels); + block_size = scan_size * buf_len; + /** + * Need to enable high-speed before configuring length/enable. + * Otherwise, the DMA buffer will work in fileio mode, + * and mmap won't work. + */ + if (use_high_speed) { + /** + * The block_size for one block is the same as 'data', but it + * doesn't need to be the same size. It is easier for the sake + * of this example. + */ + mmaps = enable_high_speed(buf_fd, block_size, nblocks); + if (!mmaps) { + fprintf(stderr, "Could not enable high-speed mode\n"); + ret = -errno; + goto error; + } + mmaps_cnt = nblocks; + printf("Using high-speed mode\n"); + } + /* Setup ring buffer parameters */ ret = write_sysfs_int("length", buf_dir_name, buf_len); if (ret < 0) @@ -673,8 +834,7 @@ int main(int argc, char **argv) goto error; } - scan_size = size_from_channelarray(channels, num_channels); - data = malloc(scan_size * buf_len); + data = malloc(block_size); if (!data) { ret = -ENOMEM; goto error; @@ -721,7 +881,13 @@ int main(int argc, char **argv) toread = 64; } - read_size = read(buf_fd, data, toread * scan_size); + if (use_high_speed) { + read_size = read_high_speed(buf_fd, data, block_size, + mmaps, mmaps_cnt); + } else { + read_size = read(buf_fd, data, toread * scan_size); + } + if (read_size < 0) { if (errno == EAGAIN) { fprintf(stderr, "nothing available\n"); @@ -740,8 +906,14 @@ int main(int argc, char **argv) if (fd >= 0 && close(fd) == -1) perror("Failed to close character device"); - if (buf_fd >= 0 && close(buf_fd) == -1) - perror("Failed to close buffer"); + for (i = 0; i < mmaps_cnt; i++) + munmap(mmaps[i].addr, mmaps[i].block.size); + free(mmaps); + if (buf_fd >= 0) { + ioctl(buf_fd, IIO_BUFFER_BLOCK_FREE_IOCTL, 0); + if (close(buf_fd) == -1) + perror("Failed to close buffer"); + } free(buffer_access); free(data); free(buf_dir_name); -- 2.17.1