linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode
@ 2023-08-27 15:25 Tobias Schramm
  2023-08-27 15:25 ` [PATCH 1/2] spi: sun6i: reduce DMA RX transfer width to single byte Tobias Schramm
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tobias Schramm @ 2023-08-27 15:25 UTC (permalink / raw)
  To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland
  Cc: linux-spi, linux-sunxi, linux-kernel, linux-arm-kernel, Tobias Schramm

Hey folks,

this set of patches fixes two bugs in the sun6i SPI driver that result in
corruption of received data in DMA RX mode.

The first bug seems to be related to an incompatibility of the SPI RX FIFO
with wider than single byte read accesses during SPI transfers. I'm not
sure if this bug affects all types of SPI controllers found in Allwinner
SoCs supported by this driver. However reducing the access width should
always be safe. I've tested this change on a V3s SoC. Further testing to
narrow down the set of affected SoCs in the future would be welcome.

The second bug is a race between SPI RX DMA and FIFO drain logic for
interrupt-based SPI operation. This bug affects all SPI controllers
supported by this driver. Once again this change has been tested on the
Allwinner V3s SoC.

Tobias Schramm (2):
  spi: sun6i: reduce DMA RX transfer width to single byte
  spi: sun6i: fix race between DMA RX transfer completion and RX FIFO
    drain

 drivers/spi/spi-sun6i.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

-- 
2.42.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] spi: sun6i: reduce DMA RX transfer width to single byte
  2023-08-27 15:25 [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Tobias Schramm
@ 2023-08-27 15:25 ` Tobias Schramm
  2023-08-27 15:25 ` [PATCH 2/2] spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain Tobias Schramm
  2023-09-04 18:52 ` [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Tobias Schramm @ 2023-08-27 15:25 UTC (permalink / raw)
  To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland
  Cc: linux-spi, linux-sunxi, linux-kernel, linux-arm-kernel, Tobias Schramm

Through empirical testing it has been determined that sometimes RX SPI
transfers with DMA enabled return corrupted data. This is down to single
or even multiple bytes lost during DMA transfer from SPI peripheral to
memory. It seems the RX FIFO within the SPI peripheral can become
confused when performing bus read accesses wider than a single byte to it
during an active SPI transfer.

This patch reduces the width of individual DMA read accesses to the
RX FIFO to a single byte to mitigate that issue.

Signed-off-by: Tobias Schramm <t.schramm@manjaro.org>
---
 drivers/spi/spi-sun6i.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c
index 30d541612253..8fcb2696ec09 100644
--- a/drivers/spi/spi-sun6i.c
+++ b/drivers/spi/spi-sun6i.c
@@ -207,7 +207,7 @@ static int sun6i_spi_prepare_dma(struct sun6i_spi *sspi,
 		struct dma_slave_config rxconf = {
 			.direction = DMA_DEV_TO_MEM,
 			.src_addr = sspi->dma_addr_rx,
-			.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
+			.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
 			.src_maxburst = 8,
 		};
 
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain
  2023-08-27 15:25 [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Tobias Schramm
  2023-08-27 15:25 ` [PATCH 1/2] spi: sun6i: reduce DMA RX transfer width to single byte Tobias Schramm
@ 2023-08-27 15:25 ` Tobias Schramm
  2023-09-04 18:52 ` [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Tobias Schramm @ 2023-08-27 15:25 UTC (permalink / raw)
  To: Mark Brown, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland
  Cc: linux-spi, linux-sunxi, linux-kernel, linux-arm-kernel, Tobias Schramm

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 <t.schramm@manjaro.org>
---
 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;
 
 	struct completion	done;
+	struct completion	dma_rx_done;
 
 	const u8		*tx_buf;
 	u8			*rx_buf;
@@ -196,6 +197,13 @@ static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
 	return SUN6I_MAX_XFER_SIZE - 1;
 }
 
+static void sun6i_spi_dma_rx_cb(void *param)
+{
+	struct sun6i_spi *sspi = 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 = sspi;
+		rxdesc->callback = sun6i_spi_dma_rx_cb;
 	}
 
 	txdesc = NULL;
@@ -275,6 +285,7 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
 		return -EINVAL;
 
 	reinit_completion(&sspi->done);
+	reinit_completion(&sspi->dma_rx_done);
 	sspi->tx_buf = tfr->tx_buf;
 	sspi->rx_buf = tfr->rx_buf;
 	sspi->len = tfr->len;
@@ -459,6 +470,22 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
 	start = jiffies;
 	timeout = 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 = wait_for_completion_timeout(&sspi->dma_rx_done,
+							      timeout);
+			if (!timeout)
+				dev_warn(&master->dev, "RX DMA timeout\n");
+		}
+	}
+
 	end = 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)
 	}
 
 	init_completion(&sspi->done);
+	init_completion(&sspi->dma_rx_done);
 
 	sspi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
 	if (IS_ERR(sspi->rstc)) {
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode
  2023-08-27 15:25 [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Tobias Schramm
  2023-08-27 15:25 ` [PATCH 1/2] spi: sun6i: reduce DMA RX transfer width to single byte Tobias Schramm
  2023-08-27 15:25 ` [PATCH 2/2] spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain Tobias Schramm
@ 2023-09-04 18:52 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2023-09-04 18:52 UTC (permalink / raw)
  To: Chen-Yu Tsai, Jernej Skrabec, Samuel Holland, Tobias Schramm
  Cc: linux-spi, linux-sunxi, linux-kernel, linux-arm-kernel

On Sun, 27 Aug 2023 17:25:56 +0200, Tobias Schramm wrote:
> Hey folks,
> 
> this set of patches fixes two bugs in the sun6i SPI driver that result in
> corruption of received data in DMA RX mode.
> 
> The first bug seems to be related to an incompatibility of the SPI RX FIFO
> with wider than single byte read accesses during SPI transfers. I'm not
> sure if this bug affects all types of SPI controllers found in Allwinner
> SoCs supported by this driver. However reducing the access width should
> always be safe. I've tested this change on a V3s SoC. Further testing to
> narrow down the set of affected SoCs in the future would be welcome.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/2] spi: sun6i: reduce DMA RX transfer width to single byte
      commit: 171f8a49f212e87a8b04087568e1b3d132e36a18
[2/2] spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain
      commit: 1f11f4202caf5710204d334fe63392052783876d

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-04 18:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-27 15:25 [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Tobias Schramm
2023-08-27 15:25 ` [PATCH 1/2] spi: sun6i: reduce DMA RX transfer width to single byte Tobias Schramm
2023-08-27 15:25 ` [PATCH 2/2] spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain Tobias Schramm
2023-09-04 18:52 ` [PATCH 0/2] spi: sun6i: fix RX data corruption in DMA mode Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).