From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D77BD1359E for ; Mon, 28 Oct 2024 12:15:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Q0vccx0ix4CwGAznIv4tlIgkiSx3QLmga1/mZY6CUMk=; b=30xt5qmM2IvlIQuB4HMI5luQRu ijaiOVqq5LTOP3Zak5imhm8Fvcgo9ET03sGk0K9xmVCGRiHJ8+0IBN5yBOHmiH9Ah2NknzHKBRXbx Id0+W77eDqa4ZiXIIVsuWLoa3mPODHfBQ2OnsNF2rmEeVpLLZkAhBlvNV9U7t5SJENuvKtJ1JBca/ iVTd7U1TsqVJzfrakVA6Y7a01ikOfAkM1SIuoB6/caFB7ssHoqchgb5aAsdVo/qLEKuBLGvCh07gH Nk3EZOHf96/rkN95zQOhHZW1bv/bDD6EFT7NVOPot9aqq4fLVqNuRwWnl2IUZZUo/qvszYpAWvFvg /jrJ4vnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t5OeV-0000000AhxM-356P; Mon, 28 Oct 2024 12:15:19 +0000 Received: from perceval.ideasonboard.com ([213.167.242.64]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t5NYp-0000000AVq1-1Vdx; Mon, 28 Oct 2024 11:05:24 +0000 Received: from [192.168.88.20] (91-157-155-49.elisa-laajakaista.fi [91.157.155.49]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A05461AFB; Mon, 28 Oct 2024 12:05:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1730113518; bh=IkZzhbQGxGn5UP2zkNF/LVFrX4ehLInSK1KVjeXp9q0=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=VUrSvW5jYyoMpCShkwTlpb4tUgzrogwnrGPX15F+3lTxk2pWQyZgLwJUWcQq/1M+y HDgKihdPLAyC2BuVAejB3N1Qy9G7kOOJ8lyUtChoSq7dSljDt4D3MYYOAnJY6RyPT5 sGHNu/8eJD+mm9+3dvv6RQROv+F/dB1oYSlOLYoY= Message-ID: <505c502e-b67a-4dca-8420-eb87eae4e170@ideasonboard.com> Date: Mon, 28 Oct 2024 13:05:14 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 3/4] media: raspberrypi: Add support for RP1-CFE To: Hans Verkuil Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-rpi-kernel@lists.infradead.org, linux-arm-kernel@lists.infradead.org, Naushir Patuck , Laurent Pinchart , Sakari Ailus , Jacopo Mondi , Kieran Bingham , Mauro Carvalho Chehab , Raspberry Pi Kernel Maintenance , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Florian Fainelli , Broadcom internal kernel review list , Rob Herring , Krzysztof Kozlowski References: <20241003-rp1-cfe-v6-0-d6762edd98a8@ideasonboard.com> <20241003-rp1-cfe-v6-3-d6762edd98a8@ideasonboard.com> <4d9e340e-2ae7-495b-8623-0d10398e1c3d@xs4all.nl> <02f05b61-08e7-45f8-8d59-f79bc20d076f@ideasonboard.com> <74286a86-51b9-4742-bb0c-583d70b1b0a7@xs4all.nl> Content-Language: en-US From: Tomi Valkeinen Autocrypt: addr=tomi.valkeinen@ideasonboard.com; keydata= xsFNBE6ms0cBEACyizowecZqXfMZtnBniOieTuFdErHAUyxVgtmr0f5ZfIi9Z4l+uUN4Zdw2 wCEZjx3o0Z34diXBaMRJ3rAk9yB90UJAnLtb8A97Oq64DskLF81GCYB2P1i0qrG7UjpASgCA Ru0lVvxsWyIwSfoYoLrazbT1wkWRs8YBkkXQFfL7Mn3ZMoGPcpfwYH9O7bV1NslbmyJzRCMO eYV258gjCcwYlrkyIratlHCek4GrwV8Z9NQcjD5iLzrONjfafrWPwj6yn2RlL0mQEwt1lOvn LnI7QRtB3zxA3yB+FLsT1hx0va6xCHpX3QO2gBsyHCyVafFMrg3c/7IIWkDLngJxFgz6DLiA G4ld1QK/jsYqfP2GIMH1mFdjY+iagG4DqOsjip479HCWAptpNxSOCL6z3qxCU8MCz8iNOtZk DYXQWVscM5qgYSn+fmMM2qN+eoWlnCGVURZZLDjg387S2E1jT/dNTOsM/IqQj+ZROUZuRcF7 0RTtuU5q1HnbRNwy+23xeoSGuwmLQ2UsUk7Q5CnrjYfiPo3wHze8avK95JBoSd+WIRmV3uoO rXCoYOIRlDhg9XJTrbnQ3Ot5zOa0Y9c4IpyAlut6mDtxtKXr4+8OzjSVFww7tIwadTK3wDQv Bus4jxHjS6dz1g2ypT65qnHen6mUUH63lhzewqO9peAHJ0SLrQARAQABzTBUb21pIFZhbGtl aW5lbiA8dG9taS52YWxrZWluZW5AaWRlYXNvbmJvYXJkLmNvbT7CwY4EEwEIADgWIQTEOAw+ ll79gQef86f6PaqMvJYe9QUCX/HruAIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD6 PaqMvJYe9WmFD/99NGoD5lBJhlFDHMZvO+Op8vCwnIRZdTsyrtGl72rVh9xRfcSgYPZUvBuT VDxE53mY9HaZyu1eGMccYRBaTLJSfCXl/g317CrMNdY0k40b9YeIX10feiRYEWoD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iBdaqa5+CjyFs8ua01c 9HmyfkuhXG2OLjfQuK+Ygd56mV3lq0aFdwbaX16DG22c6flkkBSjyWXYepFtHz9KsBS0DaZb 4IkLmZwEXpZcIOQjQ71fqlpiXkXSIaQ6YMEs8WjBbpP81h7QxWIfWtp+VnwNGc6nq5IQDESH mvQcsFS7d3eGVI6eyjCFdcAO8eMAEQEAAcLBXwQYAQIACQUCTqazRwIbDAAKCRD6PaqMvJYe 9fA7EACS6exUedsBKmt4pT7nqXBcRsqm6YzT6DeCM8PWMTeaVGHiR4TnNFiT3otD5UpYQI7S suYxoTdHrrrBzdlKe5rUWpzoZkVK6p0s9OIvGzLT0lrb0HC9iNDWT3JgpYDnk4Z2mFi6tTbq xKMtpVFRA6FjviGDRsfkfoURZI51nf2RSAk/A8BEDDZ7lgJHskYoklSpwyrXhkp9FHGMaYII m9EKuUTX9JPDG2FTthCBrdsgWYPdJQvM+zscq09vFMQ9Fykbx5N8z/oFEUy3ACyPqW2oyfvU CH5WDpWBG0s5BALp1gBJPytIAd/pY/5ZdNoi0Cx3+Z7jaBFEyYJdWy1hGddpkgnMjyOfLI7B CFrdecTZbR5upjNSDvQ7RG85SnpYJTIin+SAUazAeA2nS6gTZzumgtdw8XmVXZwdBfF+ICof 92UkbYcYNbzWO/GHgsNT1WnM4sa9lwCSWH8Fw1o/3bX1VVPEsnESOfxkNdu+gAF5S6+I6n3a ueeIlwJl5CpT5l8RpoZXEOVtXYn8zzOJ7oGZYINRV9Pf8qKGLf3Dft7zKBP832I3PQjeok7F yjt+9S+KgSFSHP3Pa4E7lsSdWhSlHYNdG/czhoUkSCN09C0rEK93wxACx3vtxPLjXu6RptBw 3dRq7n+mQChEB1am0BueV1JZaBboIL0AGlSJkm23kw== In-Reply-To: <74286a86-51b9-4742-bb0c-583d70b1b0a7@xs4all.nl> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241028_040523_569907_1AB6B3C4 X-CRM114-Status: GOOD ( 37.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Hans, On 28/10/2024 12:11, Hans Verkuil wrote: > On 28/10/2024 10:21, Tomi Valkeinen wrote: >> Hi, >> >> On 24/10/2024 11:20, Hans Verkuil wrote: >>> Hi Tomi, >>> >>> I know this driver is already merged, but while checking for drivers that use >>> q->max_num_buffers I stumbled on this cfe code: >>> >>> >>> >>>> +/* >>>> + * vb2 ops >>>> + */ >>>> + >>>> +static int cfe_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, >>>> +               unsigned int *nplanes, unsigned int sizes[], >>>> +               struct device *alloc_devs[]) >>>> +{ >>>> +    struct cfe_node *node = vb2_get_drv_priv(vq); >>>> +    struct cfe_device *cfe = node->cfe; >>>> +    unsigned int size = is_image_node(node) ? >>>> +                    node->vid_fmt.fmt.pix.sizeimage : >>>> +                    node->meta_fmt.fmt.meta.buffersize; >>>> + >>>> +    cfe_dbg(cfe, "%s: [%s] type:%u\n", __func__, node_desc[node->id].name, >>>> +        node->buffer_queue.type); >>>> + >>>> +    if (vq->max_num_buffers + *nbuffers < 3) >>>> +        *nbuffers = 3 - vq->max_num_buffers; >>> >>> This makes no sense: max_num_buffers is 32, unless explicitly set when vb2_queue_init >>> is called. So 32 + *nbuffers is never < 3. >>> >>> If the idea is that at least 3 buffers should be allocated by REQBUFS, then set >>> q->min_reqbufs_allocation = 3; before calling vb2_queue_init and vb2 will handle this >>> for you. >>> >>> Drivers shouldn't modify *nbuffers, except in very rare circumstances, especially >>> since the code is almost always wrong. >> >> Looking at this, the original code in the old BSP tree was, which somehow, along the long way, got turned into the above: >> >> if (vq->num_buffers + *nbuffers < 3) >>         *nbuffers = 3 - vq->num_buffers; >> >> So... I think that is the same as "q->min_reqbufs_allocation = 3"? >> >> The distinction between min_queued_buffers and min_reqbufs_allocation, or rather the need for the latter, still escapes me. If the HW/SW requires N buffers to be queued, why would we require >> allocating more than N buffers? > > min_queued_buffers is easiest to explain: that represents the requirements of the DMA > engine, i.e. how many buffers much be queued before the DMA engine can be started. > Typically it is 0, 1 or 2. > > min_reqbufs_allocation is the minimum number of buffers that will be allocated when > calling VIDIOC_REQBUFS in order for userspace to be able to stream without blocking > or dropping frames. > > Typically this is 3 for video capture: one buffer is being DMAed, another is queued up > and the third is being processed by userspace. But sometimes drivers have other > requirements. > > The reason is that some applications will just call VIDIOC_REQBUFS with count=1 and > expect it to be rounded up to whatever makes sense. See the VIDIOC_REQBUFS doc in > https://hverkuil.home.xs4all.nl/spec/userspace-api/v4l/vidioc-reqbufs.html > > "It can be smaller than the number requested, even zero, when the driver runs out of > free memory. A larger number is also possible when the driver requires more buffers > to function correctly." > > How drivers implement this is a mess, and usually the code in the driver is wrong as > well. In particular they often did not take VIDIOC_CREATE_BUFS into account, i.e. > instead of 'if (vq->num_buffers + *nbuffers < 3)' they would do 'if (*nbuffers < 3)'. Thanks, this was educational! So. If I have a driver that has min_queued_buffers = 1, I can use VIDIOC_CREATE_BUFS to allocate a single buffer, and then capture just one buffer, right? Whereas VIDIOC_REQBUFS would give me (probably) three (or two, if the driver does not set min_reqbufs_allocation). Three buffers makes sense for full streaming, of course. > When we worked on the support for more than 32 buffers we added min_reqbufs_allocation > to let the core take care of this. In addition, this only applies to VIDIOC_REQBUFS, > if you want full control over the number of allocated buffers, then use VIDIOC_CREATE_BUFS, > with this ioctl the number of buffers will never be more than requested, although it > may be less if you run out of memory. > > I really should go through all existing drivers and fix them up if they try to > handle this in the queue_setup function, I suspect a lot of them are quite messy. > > One thing that is missing in the V4L2 uAPI is a way to report the minimum number of > buffers that need to be allocated, i.e. min_queued_buffers + 1. Since if you want Hmm, so what I wrote above is not correct? One needs min_queued_buffers + 1? Why is that? > to use CREATE_BUFS you need that information so you know that you have to create > at least that number of buffers. We have the V4L2_CID_MIN_BUFFERS_FOR_CAPTURE control, > but it is effectively codec specific. This probably should be clarified. > > I wonder if it wouldn't be better to add a min_num_buffers field to > struct v4l2_create_buffers and set it to min_queued_buffers + 1. I think this makes sense (although I still don't get the +1). However, based on the experiences from adding the streams features to various ioctls, let's be very careful =). The new 'min_num_buffers' can be filled with garbage by the userspace. If we define the 'min_num_buffers' field to be always filled by the kernel, and any value provided from the userspace to be ignored, I think it should work. Tomi