From nobody Thu Dec 18 16:55:12 2025 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 80169C83F16 for ; Sun, 27 Aug 2023 15:38:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230458AbjH0PiO (ORCPT ); Sun, 27 Aug 2023 11:38:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230113AbjH0PiL (ORCPT ); Sun, 27 Aug 2023 11:38:11 -0400 Received: from mail.manjaro.org (mail.manjaro.org [IPv6:2a01:4f8:c0c:51f3::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1E2A106; Sun, 27 Aug 2023 08:38:07 -0700 (PDT) From: Tobias Schramm DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=manjaro.org; s=2021; t=1693150299; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hP3D0sv+Pnl8mmbV8npJb2MtmhDHanchUwfByKnsoeI=; b=PBLpY3qvDytsg6qTLjboVR3FaL0LRQKH/KQkfFQTtpdpdfX0k3tVLS5j7qJLcAEumouwHc RCvK7pZXnrrnFtiMBXOdd+QvaIrNAGKEkiAIa/f31v3OdA4xarGys8l+iI5D7S+mTVeNvb iClKHjUlnqiSNjRi/aiSMgswo5sKZCwv91pFROySEmVTzZLOotFjaJLr5jWCtzsX58x2tv rVhBUYQ0JLTw8Fe6+Klps4Oi5eNkQzHWtsXsPqNwOryrQVq1QrCkKtozjUp68FZ7H5k/9S vzMCoOxjW5kzYtdUUjscNGzNcn+dfkY2KkLcAfjkpyeDG/TxQJAEaIQ5oo4xQw== To: Mark Brown , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland Cc: linux-spi@vger.kernel.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Tobias Schramm Subject: [PATCH 2/2] spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain Date: Sun, 27 Aug 2023 17:25:58 +0200 Message-ID: <20230827152558.5368-3-t.schramm@manjaro.org> In-Reply-To: <20230827152558.5368-1-t.schramm@manjaro.org> References: <20230827152558.5368-1-t.schramm@manjaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Authentication-Results: ORIGINATING; auth=pass smtp.auth=t.schramm@manjaro.org smtp.mailfrom=t.schramm@manjaro.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Previously the transfer complete IRQ immediately drained to RX FIFO to read any data remaining in FIFO to the RX buffer. This behaviour is correct when dealing with SPI in interrupt mode. However in DMA mode the transfer complete interrupt still fires as soon as all bytes to be transferred have been stored in the FIFO. At that point data in the FIFO still needs to be picked up by the DMA engine. Thus the drain procedure and DMA engine end up racing to read from RX FIFO, corrupting any data read. Additionally the RX buffer pointer is never adjusted according to DMA progress in DMA mode, thus calling the RX FIFO drain procedure in DMA mode is a bug. Fix corruptions in DMA RX mode by draining RX FIFO only in interrupt mode. Also wait for completion of RX DMA when in DMA mode before returning to ensure all data has been copied to the supplied memory buffer. Signed-off-by: Tobias Schramm --- drivers/spi/spi-sun6i.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c index 8fcb2696ec09..57c828e73c44 100644 --- a/drivers/spi/spi-sun6i.c +++ b/drivers/spi/spi-sun6i.c @@ -102,6 +102,7 @@ struct sun6i_spi { struct reset_control *rstc; =20 struct completion done; + struct completion dma_rx_done; =20 const u8 *tx_buf; u8 *rx_buf; @@ -196,6 +197,13 @@ static size_t sun6i_spi_max_transfer_size(struct spi_d= evice *spi) return SUN6I_MAX_XFER_SIZE - 1; } =20 +static void sun6i_spi_dma_rx_cb(void *param) +{ + struct sun6i_spi *sspi =3D param; + + complete(&sspi->dma_rx_done); +} + static int sun6i_spi_prepare_dma(struct sun6i_spi *sspi, struct spi_transfer *tfr) { @@ -220,6 +228,8 @@ static int sun6i_spi_prepare_dma(struct sun6i_spi *sspi, DMA_PREP_INTERRUPT); if (!rxdesc) return -EINVAL; + rxdesc->callback_param =3D sspi; + rxdesc->callback =3D sun6i_spi_dma_rx_cb; } =20 txdesc =3D NULL; @@ -275,6 +285,7 @@ static int sun6i_spi_transfer_one(struct spi_master *ma= ster, return -EINVAL; =20 reinit_completion(&sspi->done); + reinit_completion(&sspi->dma_rx_done); sspi->tx_buf =3D tfr->tx_buf; sspi->rx_buf =3D tfr->rx_buf; sspi->len =3D tfr->len; @@ -459,6 +470,22 @@ static int sun6i_spi_transfer_one(struct spi_master *m= aster, start =3D jiffies; timeout =3D wait_for_completion_timeout(&sspi->done, msecs_to_jiffies(tx_time)); + + if (!use_dma) { + sun6i_spi_drain_fifo(sspi); + } else { + if (timeout && rx_len) { + /* + * Even though RX on the peripheral side has finished + * RX DMA might still be in flight + */ + timeout =3D wait_for_completion_timeout(&sspi->dma_rx_done, + timeout); + if (!timeout) + dev_warn(&master->dev, "RX DMA timeout\n"); + } + } + end =3D jiffies; if (!timeout) { dev_warn(&master->dev, @@ -486,7 +513,6 @@ static irqreturn_t sun6i_spi_handler(int irq, void *dev= _id) /* Transfer complete */ if (status & SUN6I_INT_CTL_TC) { sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC); - sun6i_spi_drain_fifo(sspi); complete(&sspi->done); return IRQ_HANDLED; } @@ -644,6 +670,7 @@ static int sun6i_spi_probe(struct platform_device *pdev) } =20 init_completion(&sspi->done); + init_completion(&sspi->dma_rx_done); =20 sspi->rstc =3D devm_reset_control_get_exclusive(&pdev->dev, NULL); if (IS_ERR(sspi->rstc)) { --=20 2.42.0