Skip to content

Commit 1f11f42

Browse files
TobleMinerbroonie
authored andcommitted
spi: sun6i: fix race between DMA RX transfer completion and RX FIFO drain
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 171f8a4 commit 1f11f42

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

drivers/spi/spi-sun6i.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct sun6i_spi {
102102
struct reset_control *rstc;
103103

104104
struct completion done;
105+
struct completion dma_rx_done;
105106

106107
const u8 *tx_buf;
107108
u8 *rx_buf;
@@ -196,6 +197,13 @@ static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
196197
return SUN6I_MAX_XFER_SIZE - 1;
197198
}
198199

200+
static void sun6i_spi_dma_rx_cb(void *param)
201+
{
202+
struct sun6i_spi *sspi = param;
203+
204+
complete(&sspi->dma_rx_done);
205+
}
206+
199207
static int sun6i_spi_prepare_dma(struct sun6i_spi *sspi,
200208
struct spi_transfer *tfr)
201209
{
@@ -220,6 +228,8 @@ static int sun6i_spi_prepare_dma(struct sun6i_spi *sspi,
220228
DMA_PREP_INTERRUPT);
221229
if (!rxdesc)
222230
return -EINVAL;
231+
rxdesc->callback_param = sspi;
232+
rxdesc->callback = sun6i_spi_dma_rx_cb;
223233
}
224234

225235
txdesc = NULL;
@@ -275,6 +285,7 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
275285
return -EINVAL;
276286

277287
reinit_completion(&sspi->done);
288+
reinit_completion(&sspi->dma_rx_done);
278289
sspi->tx_buf = tfr->tx_buf;
279290
sspi->rx_buf = tfr->rx_buf;
280291
sspi->len = tfr->len;
@@ -459,6 +470,22 @@ static int sun6i_spi_transfer_one(struct spi_master *master,
459470
start = jiffies;
460471
timeout = wait_for_completion_timeout(&sspi->done,
461472
msecs_to_jiffies(tx_time));
473+
474+
if (!use_dma) {
475+
sun6i_spi_drain_fifo(sspi);
476+
} else {
477+
if (timeout && rx_len) {
478+
/*
479+
* Even though RX on the peripheral side has finished
480+
* RX DMA might still be in flight
481+
*/
482+
timeout = wait_for_completion_timeout(&sspi->dma_rx_done,
483+
timeout);
484+
if (!timeout)
485+
dev_warn(&master->dev, "RX DMA timeout\n");
486+
}
487+
}
488+
462489
end = jiffies;
463490
if (!timeout) {
464491
dev_warn(&master->dev,
@@ -486,7 +513,6 @@ static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
486513
/* Transfer complete */
487514
if (status & SUN6I_INT_CTL_TC) {
488515
sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
489-
sun6i_spi_drain_fifo(sspi);
490516
complete(&sspi->done);
491517
return IRQ_HANDLED;
492518
}
@@ -644,6 +670,7 @@ static int sun6i_spi_probe(struct platform_device *pdev)
644670
}
645671

646672
init_completion(&sspi->done);
673+
init_completion(&sspi->dma_rx_done);
647674

648675
sspi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
649676
if (IS_ERR(sspi->rstc)) {

0 commit comments

Comments
 (0)