Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp184625lqb; Thu, 14 Mar 2024 08:33:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUEYj29hLOHya/Vb2fbG5c7Qq9x0vX2DZY2fFlnyVPYS44trQlo0F5WePp98JoPGWNBV7RdGo/U5rR614+dqlbPDlQieozhv2l0VtV0ig== X-Google-Smtp-Source: AGHT+IEuURDG6QlzEJTqnsutCMmQ5II7CE6I/zvynfMjk9A1NqKkYpvxGrUgC/xsxwnK9RM8nC+i X-Received: by 2002:a17:90a:d315:b0:299:a69:1f8b with SMTP id p21-20020a17090ad31500b002990a691f8bmr352811pju.23.1710430397493; Thu, 14 Mar 2024 08:33:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710430397; cv=pass; d=google.com; s=arc-20160816; b=yo8/wpECO+KunwiG4NgBUTkdqAorFcMdCmwc83qFQf3SI5D6x80Teu/tnYysGnC8o8 7xu6B5C0Li8uUgUEk6/QZ5O+gVsSYWvgnECxgWFiXRW6K+2J0hfUIBLtfzpi2tSDHFOP zqO+VjPOT2u6dpZ5th5VeXyiyEvB9e9PpQD3rwBnGFy3TKXKThHNqZnW36RjYRJhTYvo kF1W2JpxPpjmlAbJ3wRz3lldH+jfB1+KnlZ05YMqI7GxWm6JtkXQd3RmPPbmPwaoQFl9 8gDOT8/91SVWXoGbipR9iEGMZxKo4CtRxGPc8JEAuyWMZ0yeAn1bB4ENU/Ti4vbyxORR CFtA== 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=UbgkAzzcIMh0reNCVSTmPKNYzE26L4jjeo9xIHkGlsg=; fh=zOBpKhMTeRbrgfYir35sIvSkMQrxOs+orb+lNlEOd4k=; b=TNU058vVLllwc/tsO+JAwu6pFQloU2Wvy2+Hm97HPy/Er3wozbZyzasRB+zmJ+yZoo 0zx8LgOf6YUCZaeKqbt6uI5ff5kM966ft5sku7y8FrRj4ZoBHl33ICHGqvnYUivxwXpM HqxPRtFPtma1pQu5/Ry51VY0UFlME6aksOPQCh7XXljH2H8P3XC6c3S0QQh4RHM9Vg1/ to+LHO4soyrKvSCbcvSnruWT+lyToWp75gHZ0/bVykbzxEmHELWdV5Up4FfCsOvylr1p hR0x7w3ef/Vr4z8nhCpmv1I8uYYVGbxy1iDSFOkIvUf7PtqMlcM7+e4K1sQCQtLJX+fy uafw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=sVTs57DZ; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-103499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103499-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id t15-20020a17090a4e4f00b0029c649242f6si842435pjl.42.2024.03.14.08.33.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 08:33:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-103499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=sVTs57DZ; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-103499-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103499-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com 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 BC787283AE1 for ; Thu, 14 Mar 2024 15:33:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1258C73514; Thu, 14 Mar 2024 15:32:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="sVTs57DZ" Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (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 538D371749; Thu, 14 Mar 2024 15:32:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430358; cv=none; b=VmyYXW2VLGAtjSiLcXawpNeneQ4Xf9z/op1fqYfu6h+kPuFB8REyvQASePjgop0QvnR7frJjphLpeK/mEV/HQlo9GVxkIMdWNs5WKm0FtD8tPkPdI0jpgmXggJhg91eEa6yN826c+AP2QN8x622coyzuAAWQk6Vwm8jiXYsf9Qw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710430358; c=relaxed/simple; bh=N3rnu1TmY3k6wbtelOYee/CdzF0xUD+mi+uZbUMnoKk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=STRfkCixiTxNgjcEYGDls8U0V45ryVZLCM8MGlwp0me0IPqMiOx+vxUz5kUgJxt8j0EKhonKMKP/bWxOOIo8b0/UO9HJOphY7JvkT1k9qGi19jzp1zKUR6TyrVmchAjB5gtGLGsg1Imnm9eiY5P3orrlff95sYpaG9cIlkge33I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=sVTs57DZ; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1710430354; bh=N3rnu1TmY3k6wbtelOYee/CdzF0xUD+mi+uZbUMnoKk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sVTs57DZ9qUSowSL1icrzBSodQAEwsbR0SiVRw9FaNKa2MQ9FfzQCfLd3ieLvCWuB nTHyOLXLT6Mz4a9egPEzjrHXe9SXI9LRLUHkojKeMqD+pWJwGmEeW3+pZVP984BcGU oEcpsVQBAHCc/vO5fO0o2gsBPlElvvXv5znRmjbyWyWWZzR/aDp4EESmTtXGEP/TJI JOo6ZQCRYiR5EhBQfcBqCDc/GnoKjtGA+hrrt0h0Sui4Su6RZrkEEPJaKv5t81hn0S BIT4Dr8yliQTm2GYQaO3n/aDmbMLUlKAwikRLPepMkfLzs51iRqRps5LyyrNtg49IK RLzsuxwA0Lv9Q== Received: from benjamin-XPS-13-9310.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: benjamin.gaignard) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 0CAAD37820F7; Thu, 14 Mar 2024 15:32:34 +0000 (UTC) From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, hverkuil-cisco@xs4all.nl, nicolas@ndufresne.ca Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v21 2/9] videobuf2: Add min_reqbufs_allocation field to vb2_queue structure Date: Thu, 14 Mar 2024 16:32:19 +0100 Message-Id: <20240314153226.197445-3-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240314153226.197445-1-benjamin.gaignard@collabora.com> References: <20240314153226.197445-1-benjamin.gaignard@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add 'min_reqbufs_allocation' field in the vb2_queue structure so drivers can specify the minimum number of buffers to allocate when calling VIDIOC_REQBUFS. When initializing the queue, v4l2 core makes sure that the following constraints are respected: - the minimum number of buffers to allocate must be at least 2 because one buffer is used by the hardware while the other is being processed by userspace. -if the driver needs 'min_queued_buffers' in the queue before calling start_streaming(), then the minimum requirement is 'min_queued_buffers + 1' to keep at least one buffer available for userspace. Simplify __vb2_init_fileio() by using 'min_reqbufs_allocation' directly to avoid duplicating the minimum number of buffers to allocate computation. Signed-off-by: Benjamin Gaignard --- .../media/common/videobuf2/videobuf2-core.c | 38 +++++++++++-------- include/media/videobuf2-core.h | 15 +++++++- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index d8b3c04cb3b5..58c495b253ce 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -866,7 +866,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, /* * Make sure the requested values and current defaults are sane. */ - num_buffers = max_t(unsigned int, *count, q->min_queued_buffers); + num_buffers = max_t(unsigned int, *count, q->min_reqbufs_allocation); num_buffers = min_t(unsigned int, num_buffers, q->max_num_buffers); memset(q->alloc_devs, 0, sizeof(q->alloc_devs)); /* @@ -918,7 +918,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, * There is no point in continuing if we can't allocate the minimum * number of buffers needed by this vb2_queue. */ - if (allocated_buffers < q->min_queued_buffers) + if (allocated_buffers < q->min_reqbufs_allocation) ret = -ENOMEM; /* @@ -2524,6 +2524,25 @@ int vb2_core_queue_init(struct vb2_queue *q) if (WARN_ON(q->supports_requests && q->min_queued_buffers)) return -EINVAL; + /* + * The minimum requirement is 2: one buffer is used + * by the hardware while the other is being processed by userspace. + */ + if (q->min_reqbufs_allocation < 2) + q->min_reqbufs_allocation = 2; + + /* + * If the driver needs 'min_queued_buffers' in the queue before + * calling start_streaming() then the minimum requirement is + * 'min_queued_buffers + 1' to keep at least one buffer available + * for userspace. + */ + if (q->min_reqbufs_allocation < q->min_queued_buffers + 1) + q->min_reqbufs_allocation = q->min_queued_buffers + 1; + + if (WARN_ON(q->min_reqbufs_allocation > q->max_num_buffers)) + return -EINVAL; + INIT_LIST_HEAD(&q->queued_list); INIT_LIST_HEAD(&q->done_list); spin_lock_init(&q->done_lock); @@ -2717,7 +2736,6 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) struct vb2_fileio_data *fileio; struct vb2_buffer *vb; int i, ret; - unsigned int count = 0; /* * Sanity check @@ -2738,18 +2756,8 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) if (q->streaming || vb2_get_num_buffers(q) > 0) return -EBUSY; - /* - * Start with q->min_queued_buffers + 1, driver can increase it in - * queue_setup() - * - * 'min_queued_buffers' buffers need to be queued up before you - * can start streaming, plus 1 for userspace (or in this case, - * kernelspace) processing. - */ - count = max(2, q->min_queued_buffers + 1); - dprintk(q, 3, "setting up file io: mode %s, count %d, read_once %d, write_immediately %d\n", - (read) ? "read" : "write", count, q->fileio_read_once, + (read) ? "read" : "write", q->min_reqbufs_allocation, q->fileio_read_once, q->fileio_write_immediately); fileio = kzalloc(sizeof(*fileio), GFP_KERNEL); @@ -2763,7 +2771,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) * Request buffers and use MMAP type to force driver * to allocate buffers by itself. */ - fileio->count = count; + fileio->count = q->min_reqbufs_allocation; fileio->memory = VB2_MEMORY_MMAP; fileio->type = q->type; q->fileio = fileio; diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 667bf9ee1101..4a8b9135cec8 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -549,9 +549,21 @@ struct vb2_buf_ops { * @start_streaming can be called. Used when a DMA engine * cannot be started unless at least this number of buffers * have been queued into the driver. - * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + * VIDIOC_REQBUFS will ensure at least @min_queued_buffers + 1 * buffers will be allocated. Note that VIDIOC_CREATE_BUFS will not * modify the requested buffer count. + * @min_reqbufs_allocation: the minimum number of buffers to be allocated when + * calling VIDIOC_REQBUFS. Note that VIDIOC_CREATE_BUFS will *not* + * modify the requested buffer count and does not use this field. + * Drivers can set this if there has to be a certain number of + * buffers available for the hardware to work effectively. + * This allows calling VIDIOC_REQBUFS with a buffer count of 1 and + * it will be automatically adjusted to a workable buffer count. + * If set, then @min_reqbufs_allocation must be larger than + * @min_queued_buffers + 1. + * If this field is > 3, then it is highly recommended that the + * driver implements the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE/OUTPUT + * control. * @alloc_devs: &struct device memory type/allocator-specific per-plane device */ /* @@ -622,6 +634,7 @@ struct vb2_queue { u32 timestamp_flags; gfp_t gfp_flags; u32 min_queued_buffers; + u32 min_reqbufs_allocation; struct device *alloc_devs[VB2_MAX_PLANES]; -- 2.40.1