Skip to contentSkip to conversation

spi: omap2-mcspi: skip the TX drain wait in duplex

PATCH New Markus Klotzbuecher 2 participants 2 messages Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: omap2-mcspi: skip the TX drain wait in duplex

k3-udma completes a transfer towards a PDMA peer only once the peer has drained (udma_is_desc_really_done()). If it has not at the ring interrupt, k3-udma defers that check to a delayed work on the system workqueue. The McSPI depends on this: it prepares TX with DMA_PREP_INTERRUPT, and its TX callback clears the DMA write request, which has to stay set until the PDMA has passed on every byte. On PREEMPT_RT the kworker runs at normal priority, so each DMA transfer waits behind other tasks, for several ms under load.

Commit e8e2f92b1553 ("dmaengine: ti: k3-udma: Respond TX done if DMA_PREP_INTERRUPT is not requested") lets a consumer skip that check when it knows by other means that TX is done. Its first version was posted together with a McSPI change that waited for EOW instead [1], which was not applied.

In host mode full duplex the RX completion proves that every TX word has been shifted out, so once omap2_mcspi_rx_dma() has returned the PDMA holds no TX data. That is what k3-udma requires of a consumer that does not set DMA_PREP_INTERRUPT. Prepare TX there with DMA_CTRL_ACK only and without a callback, clear the write request after RX, and do not wait for the TX completion. Target mode and TX-only transfers keep the callback and the drain check.

On an AM625 with PREEMPT_RT, a device on McSPI2 at 50 MHz and four transfers every 2 ms, two of them, 344 and 367 bytes, via DMA: the cyclic thread missed its deadline 217 times in 10 minutes idle and 1864 times in 30 minutes under load without this change, and 0 and 1 times with it. No data errors in about 893k checked loopback updates.

---
Notes:
- Tested on 6.18.56 PREEMPT_RT with commit c126e07bde21 ("spi:
omap2-mcspi: handle DMA setup failures") backported and with the DMA request bit lock patch:
https://lore.kernel.org/r/[email protected]
On spi for-next the callback is set through dmaengine_desc_set_callback() (commit 29fcd5d436c4); compile-tested there.
- Independent of that lock patch; applies to 266ae6df1980 alone.
- The 2022 thread asked about TX data left in the PDMA when the IP stops transmitting (externally clocked bus, delays). In host full duplex the RX completion means every word was shifted out, whatever the delay; an externally clocked bus is target mode, which keeps the drain check. RX EOW is not used as the signal because RX data may still be in flight to memory then.
- omap-dma and edma complete a slave_sg descriptor regardless of DMA_PREP_INTERRUPT; there the only change is that the write request is cleared after RX instead of from the TX callback. Not tested on those.
- As far as I can tell this is the first user of the k3-udma opt-out from e8e2f92b1553, whose message gives the McSPI as the example.
- On AM625, PEER_BCNT wraps below zero after the early decrement and is back at 0 once the PDMA has drained, so a following transfer starts from clean counters.

 drivers/spi/spi-omap2-mcspi.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)
drivers/spi/spi-omap2-mcspi.c +28 -6
index 061d50c65dfd..1eaa86c8d892 100644
@@ -413,6 +413,18 @@ static void omap2_mcspi_tx_callback(void *data)
 	complete(&mcspi_dma->dma_tx_completion);
 }
 
+/*
+ * In host full duplex the RX completion proves that every TX word was
+ * shifted out, so TX needs neither a callback nor k3-udma's peer drain
+ * check, which it defers to a kworker.
+ */
+static bool omap2_mcspi_tx_duplex(struct spi_device *spi,
+				  struct spi_transfer *xfer)
+{
+	return xfer->tx_buf && xfer->rx_buf &&
+	       !spi_controller_is_target(spi->controller);
+}
+
 static int omap2_mcspi_tx_dma(struct spi_device *spi,
 			      struct spi_transfer *xfer,
 			      struct dma_slave_config cfg)
@@ -421,6 +433,7 @@ static int omap2_mcspi_tx_dma(struct spi_device *spi,
 	struct omap2_mcspi	*mcspi;
 	struct omap2_mcspi_dma  *mcspi_dma;
 	struct dma_async_tx_descriptor *tx;
+	unsigned long flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
 
 	mcspi = spi_controller_get_devdata(spi->controller);
 	mcspi_dma = &mcspi->dma_channels[spi_get_chipselect(spi, 0)];
@@ -429,14 +442,18 @@ static int omap2_mcspi_tx_dma(struct spi_device *spi,
 	if (ret)
 		return ret;
 
+	if (omap2_mcspi_tx_duplex(spi, xfer))
+		flags = DMA_CTRL_ACK;
+
 	tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
 				     xfer->tx_sg.nents,
-				     DMA_MEM_TO_DEV,
-				     DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+				     DMA_MEM_TO_DEV, flags);
 	if (!tx)
 		return -EIO;
 
-	dmaengine_desc_set_callback(tx, omap2_mcspi_tx_callback, NULL, spi);
+	if (flags & DMA_PREP_INTERRUPT)
+		dmaengine_desc_set_callback(tx, omap2_mcspi_tx_callback, NULL,
+					    spi);
 
 	return dma_submit_error(dmaengine_submit(tx));
 }
@@ -672,6 +689,8 @@ static int omap2_mcspi_txrx_dma(struct spi_device *spi,
 					 &rx_submitted, &dma_started);
 		if (ret)
 			goto out;
+		if (omap2_mcspi_tx_duplex(spi, xfer))
+			omap2_mcspi_set_dma_req(spi, 0, 0);
 	} else if (tx) {
 		if (mcspi->target_aborted) {
 			ret = -EINTR;
@@ -686,9 +705,12 @@ static int omap2_mcspi_txrx_dma(struct spi_device *spi,
 	}
 
 	if (tx) {
-		ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_tx_completion);
-		if (ret || mcspi->target_aborted)
-			goto out;
+		if (!omap2_mcspi_tx_duplex(spi, xfer)) {
+			ret = mcspi_wait_for_completion(mcspi,
+							&mcspi_dma->dma_tx_completion);
+			if (ret || mcspi->target_aborted)
+				goto out;
+		}
 
 		if (spi_controller_is_target(mcspi->ctlr)) {
 			ret = mcspi_wait_for_completion(mcspi, &mcspi->txdone);

base-commit: 266ae6df1980a5a5c58735ae114ae62a2acad8db

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] spi: omap2-mcspi: broken non-FIFO DMA handling in omap2_mcspi_tx_duplex() It also noticed 1 related…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] spi: omap2-mcspi: broken non-FIFO DMA handling in omap2_mcspi_tx_duplex()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [High] spi: omap2-mcspi: race condition in omap2_mcspi_set_dma_req()
[1]
--

commit be820d0974cd0a480f2639f996cc447a021feacc
Author: Markus Klotzbuecher <[email protected]>

spi: omap2-mcspi: skip the TX drain wait in duplex

This commit skips DMA_PREP_INTERRUPT and the TX completion wait for host full-duplex DMA transfers, clearing the TX DMA request after RX DMA completes instead of in the TX callback.

Quoted 9 lines
drivers/spi/spi-omap2-mcspi.c +1 -0
index 061d50c65dfd0..1eaa86c8d892e 100644
@@ -413,6 +413,18 @@ static void omap2_mcspi_tx_callback(void *data)
 	complete(&mcspi_dma->dma_tx_completion);
 }
 
+/*

drivers/spi/spi-omap2-mcspi.c static void omap2_mcspi_tx_callback(void *data) · line 426

Quoted 10 lines
drivers/spi/spi-omap2-mcspi.c +10 -0
+ * In host full duplex the RX completion proves that every TX word was
+ * shifted out, so TX needs neither a callback nor k3-udma's peer drain
+ * check, which it defers to a kworker.
+ */
+static bool omap2_mcspi_tx_duplex(struct spi_device *spi,
+				  struct spi_transfer *xfer)
+{
+	return xfer->tx_buf && xfer->rx_buf &&
+	       !spi_controller_is_target(spi->controller);
+}

[Severity: Medium]
Should omap2_mcspi_tx_duplex() also require mcspi->fifo_depth > 0?

When the transfer exceeds OMAP2_MCSPI_MAX_FIFOWCNT on controllers with max_xfer_len == 0, omap2_mcspi_set_fifo() disables the FIFO and sets mcspi->fifo_depth = 0. In that mode, omap2_mcspi_rx_dma() only transfers elements - 1 words via DMA, disables the channel after dma_rx_completion, polls OMAP2_MCSPI_CHSTAT_RXS for the last word, and re-enables the channel:

drivers/spi/spi-omap2-mcspi.c:omap2_mcspi_rx_dma() {
	...
	omap2_mcspi_set_enable(spi, 0);

	elements = word_count - 1;

	if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
		...
	} else {
		dev_err(&spi->dev, "DMA RX last word empty\n");
		*count -= mcspi_bytes_per_word(word_len);
	}
	omap2_mcspi_set_enable(spi, 1);
	return 0;
}

Without omap2_mcspi_tx_callback() clearing OMAP2_MCSPI_CHCONF_DMAW when the last TX word is written, omap2_mcspi_rx_dma() re-enables the channel while DMAW is still set and TX0 is empty, asserting a spurious TX DMA request before omap2_mcspi_txrx_dma() clears DMAW.

In addition, if the last word has not shifted out when omap2_mcspi_rx_dma() disables the channel after elements - 1 RX words, the RXS poll times out, decrements *count, and returns 0. omap2_mcspi_txrx_dma() then returns 0 without waiting for dma_tx_completion or calling dmaengine_terminate_sync(mcspi_dma->dma_tx), and omap2_mcspi_transfer_one() returns -EIO on count != t->len, allowing the SPI core to unmap xfer->tx_sg while the TX DMA channel is still active.

[ ... ]

drivers/spi/spi-omap2-mcspi.c static int omap2_mcspi_txrx_dma(struct spi_device *spi, · line 693

Quoted 6 lines
drivers/spi/spi-omap2-mcspi.c +2 -0
@@ -672,6 +689,8 @@ static int omap2_mcspi_txrx_dma(struct spi_device *spi,
 					 &rx_submitted, &dma_started);
 		if (ret)
 			goto out;
+		if (omap2_mcspi_tx_duplex(spi, xfer))
+			omap2_mcspi_set_dma_req(spi, 0, 0);

[Severity: High]
This problem wasn't introduced by this patch, but nothing serializes
[1] the read-modify-write of the cached CHCONF0 register in omap2_mcspi_set_dma_req() when RX and TX DMA completion callbacks run concurrently on target full-duplex transfers:

CPU0 CPU1
omap2_mcspi_rx_callback() omap2_mcspi_tx_callback()
omap2_mcspi_set_dma_req(..., 1, 0) omap2_mcspi_set_dma_req(..., 0, 0)

    l = mcspi_cached_chconf0(spi);
                                          l = mcspi_cached_chconf0(spi);
    l &= ~OMAP2_MCSPI_CHCONF_DMAR;
                                          l &= ~OMAP2_MCSPI_CHCONF_DMAW;
    mcspi_write_chconf0(spi, l);
                                          mcspi_write_chconf0(spi, l);
                                            <-- overwrites DMAR clear

Can this lost update leave OMAP2_MCSPI_CHCONF_DMAR or OMAP2_MCSPI_CHCONF_DMAW enabled in CHCONF0 and hang subsequent DMA transfers?

[1] https://sashiko.dev/#/bug/linux-53ef0b8a-76a9-4654-bac2-e99f1ecedc21

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1