From patchwork Tue Sep 6 10:44:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 12967275 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 15F8DECAAA1 for ; Tue, 6 Sep 2022 10:46:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=kkDNN58zmTa3PaFm2NYu+dNwDl18WXPsWHzeexPExOo=; b=ir1HURslfm6zuX aPqidZRi4z3Y0CP5f6RToh+JeV0oPE4/8wrCibUQl+yxNow3wfQwZtNQX3gYOVrcsVARvRGATKl2U qEcQTJ/CD5hlDTp0cDeQZzWM7z7GIEmt1vYCepppguqKLJYpDA/85Eod3QkEb8B165V7bNlCf/1it 2lh0B2LUmG1N+dZa2YHUSiFom4wUdiOtSA7Z+dw9krzEhry+65lTJgDAZmSAAYieW6c4DgIaToLuS G0ISiV0zA3VCgI2NRYYvK81q1vom5XUhDDfKkGazU5rlabzAzwchmhrRtdCH8K1A2rCWsp/BN0B0M cPv54io5JK9CXOj/4Kgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oVW53-00CYhK-4d; Tue, 06 Sep 2022 10:45:21 +0000 Received: from perceval.ideasonboard.com ([213.167.242.64]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oVW4t-00CYcJ-H4 for linux-arm-kernel@lists.infradead.org; Tue, 06 Sep 2022 10:45:13 +0000 Received: from pyrite.rasen.tech (h175-177-042-159.catv02.itscom.jp [175.177.42.159]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1839425B; Tue, 6 Sep 2022 12:44:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1662461102; bh=BsC/oYZMlsU0AfsdsrR9QSz2oK4VIzDllo5FeG24Gr4=; h=From:To:Cc:Subject:Date:From; b=YSpBUFbsYKtRJgJCOHldFTmoGOA3QaSUl28vxA1uT3F7eTXuuVoBYAh/jWVuImJux F5Ko9VPNesLXMs7UAowUvglpjWyJZ/Z7a4OOMsxd7Oxp5zU0Fu7c9FYiN8IEVgmGFG m5rvbmP69kqcwAuCOZmQt0XaIz18IwVmqR22swsk= From: Paul Elder To: Rui Miguel Silva , Steve Longerbeam , linux-media@vger.kernel.org Cc: Paul Elder , Laurent Pinchart , Philipp Zabel , Mauro Carvalho Chehab , Greg Kroah-Hartman , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , linux-staging@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] media: imx7-media-csi: Add support for fast-tracking queued buffers Date: Tue, 6 Sep 2022 19:44:37 +0900 Message-Id: <20220906104437.4095745-1-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220906_034511_762955_455B7B8F X-CRM114-Status: GOOD ( 22.43 ) 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 The CSI hardware compatible with this driver handles buffers using a ping-pong mechanism with two sets of destination addresses. Normally, when an interrupt comes in to signal the completion of one buffer, say FB0, it assigns the next buffer in the queue to the next FB0, and the hardware starts to capture into FB1 in the meantime. In a buffer underrun situation, in the above example without loss of generality, if a new buffer is queued before the interrupt for FB0 comes in, we can program the buffer into FB1 (which is programmed with a dummy buffer, as there is a buffer underrun). This of course races with the interrupt that signals FB0 completion, as once that interrupt comes in, we are no longer guaranteed that the programming of FB1 was in time and must assume it was too late. This race is resolved by locking the programming of FB1. If it came after the interrupt for FB0, then the variable that is used to determine which FB to program would have been swapped by the interrupt handler, thus resolving the race. Signed-off-by: Paul Elder Acked-by: Rui Miguel Silva --- drivers/staging/media/imx/imx7-media-csi.c | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/drivers/staging/media/imx/imx7-media-csi.c b/drivers/staging/media/imx/imx7-media-csi.c index a0553c24cce4..06e50080ed31 100644 --- a/drivers/staging/media/imx/imx7-media-csi.c +++ b/drivers/staging/media/imx/imx7-media-csi.c @@ -1296,11 +1296,60 @@ static int imx7_csi_video_buf_prepare(struct vb2_buffer *vb) return 0; } +static int imx7_csi_fast_track_buffer(struct imx7_csi *csi, + struct imx7_csi_vb2_buffer *buf) +{ + unsigned long flags; + dma_addr_t phys; + int buf_num; + int ret = -EBUSY; + + if (!csi->is_streaming) + return ret; + + phys = vb2_dma_contig_plane_dma_addr(&buf->vbuf.vb2_buf, 0); + + /* + * buf_num holds the fb id of the most recently (*not* the next + * anticipated) triggered interrupt. Without loss of generality, if + * buf_num is 0 and we get to this section before the irq for fb1, the + * buffer that we are fast-tracking into fb0 should be programmed in + * time to be captured into. If the irq for fb1 already happened, then + * buf_num would be 1, and we would fast-track the buffer into fb1 + * instead. This guarantees that we won't try to fast-track into fb0 + * and race against the start-of-capture into fb0. + * + * We only fast-track the buffer if the currently programmed buffer is + * a dummy buffer. We can check the active_vb2_buf instead as it is + * always modified along with programming the fb[0,1] registers via the + * lock (besides setup and cleanup). If it is not a dummy buffer then + * we queue it normally, as fast-tracking is not an option. + */ + + spin_lock_irqsave(&csi->irqlock, flags); + + buf_num = csi->buf_num; + if (csi->active_vb2_buf[buf_num] == NULL) { + csi->active_vb2_buf[buf_num] = buf; + imx7_csi_update_buf(csi, phys, buf_num); + ret = 0; + } + + spin_unlock_irqrestore(&csi->irqlock, flags); + + return ret; +} + static void imx7_csi_video_buf_queue(struct vb2_buffer *vb) { struct imx7_csi *csi = vb2_get_drv_priv(vb->vb2_queue); struct imx7_csi_vb2_buffer *buf = to_imx7_csi_vb2_buffer(vb); unsigned long flags; + int ret; + + ret = imx7_csi_fast_track_buffer(csi, buf); + if (!ret) + return; spin_lock_irqsave(&csi->q_lock, flags);