From nobody Thu Nov 14 05:27:07 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 933ABE784BB for ; Mon, 2 Oct 2023 12:06:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236966AbjJBMGo (ORCPT ); Mon, 2 Oct 2023 08:06:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236927AbjJBMGa (ORCPT ); Mon, 2 Oct 2023 08:06:30 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3423D3; Mon, 2 Oct 2023 05:06:26 -0700 (PDT) Received: from benjamin-XPS-13-9310.. (unknown [IPv6:2a01:e0a:120:3210:9537:67ca:c85e:d0ae]) (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 madras.collabora.co.uk (Postfix) with ESMTPSA id F2A8D6607243; Mon, 2 Oct 2023 13:06:24 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696248385; bh=8xj+A2BpzIDj7xwabS651YXoIHIdIQ3kHPb3/Idtc2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lsWfn7aKtK9coLq3dZZhJ0CQoy25PkH8wBnF0N/97O9Q7VvxuzihaW73qT7uFb+WT 2NGy1/KPl5aNfz80nNt43jYQofJMl+bm6tf+iSwkwp5YrzU1fLkuj4WLvb+fdEA+sa CrU9QZjlH2LhWWjkPibeRgGGnMCP3HtdIoalqmECsUwbrufRPsei0f/U+zdJNF93Tg uUu4DEjnjyOJDha8Nt9WGjyrigR/70YS9RV5XToRgp3S2WQucLP4LM9fnn+utExQ04 iwdtbOurUjkfVXdR8wxVPL4CODAqHs47GPFW93o4/36EA/5HOrlNCHuWJz7HZVG9U0 qQXrqOFtj3zVg== From: Benjamin Gaignard To: mchehab@kernel.org, tfiga@chromium.org, m.szyprowski@samsung.com, ming.qian@nxp.com, ezequiel@vanguardiasur.com.ar, p.zabel@pengutronix.de, gregkh@linuxfoundation.org, hverkuil-cisco@xs4all.nl, nicolas.dufresne@collabora.com Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-staging@lists.linux.dev, kernel@collabora.com, Benjamin Gaignard Subject: [PATCH v9 01/53] media: videobuf2: Rework offset 'cookie' encoding pattern Date: Mon, 2 Oct 2023 14:05:25 +0200 Message-Id: <20231002120617.119602-2-benjamin.gaignard@collabora.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231002120617.119602-1-benjamin.gaignard@collabora.com> References: <20231002120617.119602-1-benjamin.gaignard@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Change how offset 'cookie' field value is computed to make possible to use more buffers. The maximum number of buffers depends of PAGE_SHIFT value and can go up to 0x7fff when PAGE_SHIFT =3D 12. With this encoding pattern we know the maximum number that a queue could store so we can check it at queue init time. It also make easier and faster to find buffer and plane from using the offset field. Change __find_plane_by_offset() prototype to return the video buffer itself rather than it index. Signed-off-by: Benjamin Gaignard --- changes in version 9: - BUFFER_INDEX_MASK now depends on PAGE_SHIFT value to match architectures requirements. .../media/common/videobuf2/videobuf2-core.c | 71 +++++++++---------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/medi= a/common/videobuf2/videobuf2-core.c index cf6727d9c81f..a0540b2e461f 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -31,6 +31,10 @@ =20 #include =20 +#define PLANE_INDEX_SHIFT (PAGE_SHIFT + 3) +#define PLANE_INDEX_MASK 0x7 +#define BUFFER_INDEX_MASK (BIT_MASK(30 - PLANE_INDEX_SHIFT) - 1) + static int debug; module_param(debug, int, 0644); =20 @@ -358,21 +362,24 @@ static void __setup_offsets(struct vb2_buffer *vb) unsigned int plane; unsigned long off =3D 0; =20 - if (vb->index) { - struct vb2_buffer *prev =3D q->bufs[vb->index - 1]; - struct vb2_plane *p =3D &prev->planes[prev->num_planes - 1]; - - off =3D PAGE_ALIGN(p->m.offset + p->length); - } + /* + * Offsets cookies value have the following constraints: + * - a buffer could have up to 8 planes. + * - v4l2 mem2mem use bit 30 to distinguish between source and destinatio= n buffers. + * - must be page aligned + * That led to this bit mapping when PAGE_SHIFT =3D 12: + * |30 |29 15|14 12|11 0| + * |DST_QUEUE_OFF_BASE|buffer index|plane index| 0 | + * where there are 15 bits to store buffer index. + * Depending on PAGE_SHIFT value we can have else bits to store buffer in= dex. + */ + off =3D vb->index << PLANE_INDEX_SHIFT; =20 for (plane =3D 0; plane < vb->num_planes; ++plane) { - vb->planes[plane].m.offset =3D off; + vb->planes[plane].m.offset =3D off + (plane << PAGE_SHIFT); =20 dprintk(q, 3, "buffer %d, plane %d offset 0x%08lx\n", vb->index, plane, off); - - off +=3D vb->planes[plane].length; - off =3D PAGE_ALIGN(off); } } =20 @@ -2185,13 +2192,12 @@ int vb2_core_streamoff(struct vb2_queue *q, unsigne= d int type) EXPORT_SYMBOL_GPL(vb2_core_streamoff); =20 /* - * __find_plane_by_offset() - find plane associated with the given offset = off + * __find_plane_by_offset() - find video buffer and plane associated with = the given offset off */ static int __find_plane_by_offset(struct vb2_queue *q, unsigned long off, - unsigned int *_buffer, unsigned int *_plane) + struct vb2_buffer **vb, unsigned int *plane) { - struct vb2_buffer *vb; - unsigned int buffer, plane; + unsigned int buffer; =20 /* * Sanity checks to ensure the lock is held, MEMORY_MMAP is @@ -2209,24 +2215,15 @@ static int __find_plane_by_offset(struct vb2_queue = *q, unsigned long off, return -EBUSY; } =20 - /* - * Go over all buffers and their planes, comparing the given offset - * with an offset assigned to each plane. If a match is found, - * return its buffer and plane numbers. - */ - for (buffer =3D 0; buffer < q->num_buffers; ++buffer) { - vb =3D q->bufs[buffer]; + /* Get buffer and plane from the offset */ + buffer =3D (off >> PLANE_INDEX_SHIFT) & BUFFER_INDEX_MASK; + *plane =3D (off >> PAGE_SHIFT) & PLANE_INDEX_MASK; =20 - for (plane =3D 0; plane < vb->num_planes; ++plane) { - if (vb->planes[plane].m.offset =3D=3D off) { - *_buffer =3D buffer; - *_plane =3D plane; - return 0; - } - } - } + if (buffer >=3D q->num_buffers || *plane >=3D q->bufs[buffer]->num_planes) + return -EINVAL; =20 - return -EINVAL; + *vb =3D q->bufs[buffer]; + return 0; } =20 int vb2_core_expbuf(struct vb2_queue *q, int *fd, unsigned int type, @@ -2306,7 +2303,7 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_stru= ct *vma) { unsigned long off =3D vma->vm_pgoff << PAGE_SHIFT; struct vb2_buffer *vb; - unsigned int buffer =3D 0, plane =3D 0; + unsigned int plane =3D 0; int ret; unsigned long length; =20 @@ -2335,12 +2332,10 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_st= ruct *vma) * Find the plane corresponding to the offset passed by userspace. This * will return an error if not MEMORY_MMAP or file I/O is in progress. */ - ret =3D __find_plane_by_offset(q, off, &buffer, &plane); + ret =3D __find_plane_by_offset(q, off, &vb, &plane); if (ret) goto unlock; =20 - vb =3D q->bufs[buffer]; - /* * MMAP requires page_aligned buffers. * The buffer length was page_aligned at __vb2_buf_mem_alloc(), @@ -2368,7 +2363,7 @@ int vb2_mmap(struct vb2_queue *q, struct vm_area_stru= ct *vma) if (ret) return ret; =20 - dprintk(q, 3, "buffer %d, plane %d successfully mapped\n", buffer, plane); + dprintk(q, 3, "buffer %u, plane %d successfully mapped\n", vb->index, pla= ne); return 0; } EXPORT_SYMBOL_GPL(vb2_mmap); @@ -2382,7 +2377,7 @@ unsigned long vb2_get_unmapped_area(struct vb2_queue = *q, { unsigned long off =3D pgoff << PAGE_SHIFT; struct vb2_buffer *vb; - unsigned int buffer, plane; + unsigned int plane; void *vaddr; int ret; =20 @@ -2392,12 +2387,10 @@ unsigned long vb2_get_unmapped_area(struct vb2_queu= e *q, * Find the plane corresponding to the offset passed by userspace. This * will return an error if not MEMORY_MMAP or file I/O is in progress. */ - ret =3D __find_plane_by_offset(q, off, &buffer, &plane); + ret =3D __find_plane_by_offset(q, off, &vb, &plane); if (ret) goto unlock; =20 - vb =3D q->bufs[buffer]; - vaddr =3D vb2_plane_vaddr(vb, plane); mutex_unlock(&q->mmap_lock); return vaddr ? (unsigned long)vaddr : -EINVAL; --=20 2.39.2