Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Binbin Zhou" <zhoubinbin@loongson.cn>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v2 4/5] spi: loongson: Add DMA support to SPI-IO driver
Date: Mon, 30 Nov 2026 02:53:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-240273@kernel.org> (raw)
In-Reply-To: <b9c7cf7a7214521f495dd787aef312d86c7ba457.1796005812.git.zhoubinbin@loongson.cn>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] spi: loongson: premature RX DMA termination in
  loongson_spi_dma_transfer()
- [Medium] spi: loongson: mapped buffer PIO fallback in
  loongson_spi_io_transfer_one_dma()
- [Medium] spi: loongson: re-evaluating can_dma() in
  loongson_spi_io_transfer_one()
- [Medium] spi: loongson: early IRQ/DMA enable and W1C RMW in
  loongson_spi_dma_transfer()
- [Medium] spi: loongson: probe failure on single channel in
  loongson_spi_io_probe()
- [Medium] spi: loongson: race with IRQ handler on timeout in
  loongson_spi_dma_transfer()
- [Medium] spi: loongson: 64K split in
  loongson_spi_io_optimize_message() defeats DMA loop
--

commit 4d011885c7facd92535ffe39b1f4dcba7ecfea2a
Author: Binbin Zhou <zhoubinbin@loongson.cn>

spi: loongson: Add DMA support to SPI-IO driver

This commit adds optional TX and RX DMA channel support to the Loongson
SPI-IO controller driver for four-byte-aligned full-duplex transfers at or
above dma_threshold.

> diff --git a/drivers/spi/spi-loongson-io.c b/drivers/spi/spi-loongson-io.c
> index 2ca9ee7e2d1da..4e67c40518cb7 100644
> --- a/drivers/spi/spi-loongson-io.c
> +++ b/drivers/spi/spi-loongson-io.c
[ ... ]
> @@ -479,14 +557,195 @@ static void loongson_spi_io_start_batch(struct loongson_spi_priv *priv)
>  				 LOONGSON_SPI_IO_CR1_START_MASK);
>  }
>  
[ ... ]
> +static int loongson_spi_dma_transfer(struct loongson_spi_priv *priv,
> +				     struct spi_transfer *xfer,
> +				     bool *bus_started)
> +{
> +	u32 frames = xfer->len / priv->frame_bytes;
> +	u32 done, chunk_frames;
> +	int ret;
> +
> +	*bus_started = false;
> +
> +	ret = loongson_spi_dma_config_and_submit(priv, xfer);
> +	if (ret)
> +		goto out_stop;
> +
> +	priv->xfer_ret = 0;
> +	priv->xfer_mode = XFER_DMA;
> +
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> +				 LOONGSON_SPI_IO_CR1_AUTOSUS,
> +				 LOONGSON_SPI_IO_CR1_AUTOSUS);
> +
> +	/* RX must be ready before TX can start filling the controller FIFO. */
> +	dma_async_issue_pending(priv->dma_rx);
> +	dma_async_issue_pending(priv->dma_tx);
> +
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
> +				 LOONGSON_SPI_IO_CR2_DMAEN_MASK,
> +				 LOONGSON_SPI_IO_CR2_DMAEN_MASK);
> +
> +	for (done = 0; done < frames; done += chunk_frames) {
> +		chunk_frames = min_t(u32, frames - done,
> +				     LOONGSON_SPI_IO_MAX_FRAMES);

[Severity: Medium]
Can this loop in loongson_spi_dma_transfer() ever run more than one
iteration?  loongson_spi_io_optimize_message() unconditionally splits all
transfers at LOONGSON_SPI_IO_XFER_CHUNK_SIZE (64 KiB) before DMA mapping:

drivers/spi/spi-loongson-io.c:loongson_spi_io_optimize_message() {
	struct spi_controller *ctrl = msg->spi->controller;

	return spi_split_transfers_maxsize(ctrl, msg,
					   LOONGSON_SPI_IO_XFER_CHUNK_SIZE);
}

Because xfer->len is at most 65536 and priv->frame_bytes is 1, 2 or 4,
frames never exceeds LOONGSON_SPI_IO_MAX_FRAMES (65536).  Transfers larger
than 64 KiB are split into separate 64 KiB spi_transfer entries, each paying
for separate scatter-gather mapping, DMA descriptor setup and synchronous
dmaengine_terminate_sync() calls instead of using a single pair of DMA
descriptors across chunks.

> +
> +		priv->xfer_ret = 0;
> +		reinit_completion(&priv->xfer_completion);
> +		loongson_spi_writel(priv, LOONGSON_SPI_IO_CR3_REG,
> +				    chunk_frames - 1);
> +		loongson_spi_writel(priv, LOONGSON_SPI_IO_CR4_REG, 0);
> +		loongson_spi_writel(priv, LOONGSON_SPI_IO_SR1_REG,
> +				    LOONGSON_SPI_IO_SR1_EOT);
> +
> +		*bus_started = true;
> +		loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> +					 LOONGSON_SPI_IO_CR1_CSTART,
> +					 LOONGSON_SPI_IO_CR1_CSTART);
> +
> +		ret = loongson_spi_dma_wait_eot(priv, xfer);
> +		if (ret)
> +			break;
> +	}

[Severity: Medium]
Does enabling LOONGSON_SPI_IO_IER_REG in loongson_spi_io_transfer_one_dma()
and LOONGSON_SPI_IO_CR2_DMAEN_MASK above before reinit_completion() and
LOONGSON_SPI_IO_CR3_REG leave a window where an error interrupt is lost and
TX DMA writes to LOONGSON_SPI_IO_DR_REG before TSIZE is programmed?

CPU0                                  CPU1 (<hardirq>)
loongson_spi_io_transfer_one_dma()
  loongson_spi_update_bits(...,
    LOONGSON_SPI_IO_IER_REG, ...)
  loongson_spi_dma_transfer()
    priv->xfer_mode = XFER_DMA;
    dma_async_issue_pending(...)
    loongson_spi_update_bits(...,
      LOONGSON_SPI_IO_CR2_REG,
      LOONGSON_SPI_IO_CR2_DMAEN_MASK)
                                      loongson_spi_io_irq_handler()
                                        loongson_spi_writel(...,
                                          LOONGSON_SPI_IO_IER_REG, 0);
                                        priv->xfer_ret = -EIO;
                                        complete(&priv->xfer_completion);
    priv->xfer_ret = 0;
    reinit_completion(&priv->xfer_completion);
    ...
    loongson_spi_dma_wait_eot()       <-- waits with IER_REG == 0 until timeout

Also, loongson_spi_io_transfer_one_dma() clears LOONGSON_SPI_IO_SR1_EOT
with loongson_spi_update_bits() on the write-1-to-clear
LOONGSON_SPI_IO_SR1_REG register, which writes back 1s to any other set
status bits in SR1_REG.

> +
> +out_stop:
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_IER_REG,
> +				 LOONGSON_SPI_IO_IER_EOTIE |
> +				 LOONGSON_SPI_IO_IER_ERR_MASK, 0);
> +	priv->xfer_mode = XFER_IDLE;
> +
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG,
> +				 LOONGSON_SPI_IO_CR1_CSTART, 0);
> +	loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR2_REG,
> +				 LOONGSON_SPI_IO_CR2_DMAEN_MASK, 0);
> +
> +	/* Quiesce both DMA channels before returning. */
> +	dmaengine_terminate_sync(priv->dma_tx);
> +	dmaengine_terminate_sync(priv->dma_rx);
> +
> +	return ret;
> +}

[Severity: High]
Does terminating priv->dma_rx immediately after LOONGSON_SPI_IO_SR1_EOT
truncate the trailing bytes of xfer->rx_buf?

The SPI controller raises LOONGSON_SPI_IO_SR1_EOT when the last frame is
shifted from the wire into the SPI RX FIFO, which is when the final RX DMA
request is asserted to drain the FIFO into memory.  loongson_spi_dma_config_and_submit()
does not set a completion callback (or DMA_PREP_INTERRUPT) on rx_desc, and
out_stop immediately clears LOONGSON_SPI_IO_CR2_DMAEN_MASK and calls
dmaengine_terminate_sync(priv->dma_rx) upon EOT.  Any frames still in the RX
FIFO or in flight in the RX DMA channel are never written to xfer->rx_buf,
and because loongson_spi_io_drain_rx() only runs at message boundaries,
leftover RX FIFO bytes corrupt the start of the next transfer in a
multi-transfer message.

[Severity: Medium]
Can out_stop race with loongson_spi_io_irq_handler() when
loongson_spi_dma_wait_eot() times out?

CPU0                                  CPU1 (<hardirq>)
loongson_spi_dma_transfer()
  loongson_spi_dma_wait_eot()
    /* timeout */
  loongson_spi_update_bits(...,
    LOONGSON_SPI_IO_IER_REG, ..., 0)
                                      loongson_spi_io_irq_handler()
                                        /* error branch reads CR1 */
  priv->xfer_mode = XFER_IDLE;
  loongson_spi_update_bits(...,
    LOONGSON_SPI_IO_CR1_REG,
    LOONGSON_SPI_IO_CR1_CSTART, 0)
                                        loongson_spi_update_bits(...,
                                          LOONGSON_SPI_IO_CR1_REG,
                                          LOONGSON_SPI_IO_CR1_START_MASK, 0)
                                          <-- RMW races on CR1_REG

Unlike loongson_spi_io_handle_err(), out_stop does not call
synchronize_irq(priv->irq) after masking LOONGSON_SPI_IO_IER_REG before
modifying priv->xfer_mode and LOONGSON_SPI_IO_CR1_REG, so a concurrent
interrupt can hit the XFER_IDLE warning without clearing CSTART or race on
the unlocked read-modify-write of LOONGSON_SPI_IO_CR1_REG.

[ ... ]
> @@ -623,13 +888,54 @@ static int loongson_spi_io_transfer_one_irq(struct spi_device *spi, struct spi_t
>  	return priv->xfer_ret;
>  }
>  
> +/**
> + * loongson_spi_io_transfer_one_dma() - Execute one DMA-driven SPI transfer
> + * @spi: SPI target device
> + * @xfer: transfer to execute
> + *
> + * Enable EOT and error interrupts, then submit both DMA channels via
> + * loongson_spi_dma_transfer(). The transfer is retried in interrupt mode on
> + * configuration failure before the bus has started; runtime DMA errors are
> + * returned to SPI core without replay.
> + *
> + * Return: 0 on success, or a negative error code.
> + */
> +static int loongson_spi_io_transfer_one_dma(struct spi_device *spi, struct spi_transfer *xfer)
> +{
[ ... ]
> +	ret = loongson_spi_dma_transfer(priv, xfer, &bus_started);
> +	if (!ret)
> +		return 0;
> +
> +	if (bus_started) {
> +		dev_err(&spi->dev, "DMA transfer failed: %d", ret);
> +		return ret;
> +	}
> +
> +	dev_dbg(&spi->dev, "DMA setup failed with %d, using interrupt mode", ret);
> +
> +	return loongson_spi_io_transfer_one_irq(spi, xfer);
> +}

[Severity: Medium]
When loongson_spi_dma_transfer() fails before bus_started, does falling back
directly to loongson_spi_io_transfer_one_irq() corrupt xfer->rx_buf while
xfer->rx_sg is still mapped for DMA?

Before calling transfer_one(), spi_transfer_one_message() calls
spi_dma_sync_for_device(), giving ownership of xfer->rx_sg to the DMA
device.  If loongson_spi_io_transfer_one_irq() writes received data into
xfer->rx_buf with the CPU and returns 0, spi_transfer_one_message() then
calls spi_dma_sync_for_cpu() on the still-mapped xfer->rx_sg, which
overwrites or invalidates the PIO-written data when bounce buffers or
non-coherent caches are used.  Setting xfer->error |=
SPI_TRANS_FAIL_NO_START and returning ret lets spi_transfer_one_message()
unmap the scatterlists and retry via its fallback_pio path.

> +
>  /**
>   * loongson_spi_io_transfer_one() - Execute one SPI transfer
>   * @ctrl: SPI controller
>   * @spi: SPI target device
>   * @xfer: transfer to execute
>   *
> - * Apply clock and mode state before running interrupt-driven FIFO batches.
> + * Apply clock and mode state, then run the transfer via DMA or
> + * interrupt-driven FIFO batches.
>   *
>   * Return: 0 on success, or a negative error code.
>   */
> @@ -645,6 +951,9 @@ static int loongson_spi_io_transfer_one(struct spi_controller *ctrl, struct spi_
>  	if (ret)
>  		return ret;
>  
> +	if (ctrl->can_dma && ctrl->can_dma(ctrl, spi, xfer))
> +		return loongson_spi_io_transfer_one_dma(spi, xfer);
> +
>  	return loongson_spi_io_transfer_one_irq(spi, xfer);
>  }

[Severity: Medium]
What happens if dma_threshold (0644 in sysfs) changes between
__spi_map_msg() and loongson_spi_io_transfer_one()?

If dma_threshold is lowered after __spi_map_msg() decided not to map the
transfer, ctrl->can_dma() returns true here with an unmapped scatterlist
(xfer->rx_sg.sgl == NULL, xfer->rx_sg.nents == 0);
loongson2_cmc_dma_prep_slave_sg() then allocates a 0-element desc->sg_req[]
flexible array and loongson2_cmc_dma_start_transfer() reads
desc->sg_req[0] out of bounds.  Conversely, if dma_threshold is raised after
__spi_map_msg() mapped the transfer, this falls back to
loongson_spi_io_transfer_one_irq() on a buffer synced for DMA_FROM_DEVICE.
Checking xfer->tx_sg_mapped && xfer->rx_sg_mapped (or
spi_xfer_is_dma_mapped()) avoids re-evaluating can_dma().

[ ... ]
> @@ -741,11 +1062,40 @@ static int loongson_spi_io_unprepare_message(struct spi_controller *ctrl,
>  	return 0;
>  }
>  
> +/**
> + * loongson_spi_io_request_dma_chan() - Request an optional DMA channel
> + * @dev: controller device
> + * @name: DMA channel name, either "tx" or "rx"
> + *
> + * Absence of either channel is non-fatal and selects interrupt-only mode.
> + * Probe deferral is preserved so the DMA provider can be probed later.
> + *
> + * Return: a DMA channel on success, NULL when unavailable, or an ERR_PTR
> + * containing %-EPROBE_DEFER.
> + */
> +static struct dma_chan *loongson_spi_io_request_dma_chan(struct device *dev,
> +							 const char *name)
> +{
> +	struct dma_chan *chan = devm_dma_request_chan(dev, name);
> +
> +	if (IS_ERR(chan)) {
> +		int ret = PTR_ERR(chan);
> +
> +		if (ret == -EPROBE_DEFER)
> +			return chan;
> +		dev_info(dev, "%s DMA unavailable with %d, using interrupt mode", name, ret);
> +		return NULL;
> +	}
> +
> +	return chan;
> +}
[ ... ]
> @@ -794,6 +1146,29 @@ static int loongson_spi_io_probe(struct platform_device *pdev)
>  							 LOONGSON_SPI_IO_CFG2_MAX_BRINT),
>  					U32_MAX);
>  
> +	priv->dma_tx = loongson_spi_io_request_dma_chan(dev, "tx");
> +	if (IS_ERR(priv->dma_tx))
> +		return PTR_ERR(priv->dma_tx);
> +
> +	priv->dma_rx = loongson_spi_io_request_dma_chan(dev, "rx");
> +	if (IS_ERR(priv->dma_rx))
> +		return PTR_ERR(priv->dma_rx);
> +
> +	if (priv->dma_tx && priv->dma_rx) {
> +		priv->use_dma = true;
> +		/* Let SPI core provide and map dummy buffers for full-duplex DMA. */
> +		host->flags |= SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
> +		host->dma_tx = priv->dma_tx;
> +		host->dma_rx = priv->dma_rx;
> +		host->can_dma = loongson_spi_can_dma;
> +		dev_info(dev, "DMA channels acquired");
> +	} else if (priv->dma_tx || priv->dma_rx) {
> +		return dev_err_probe(dev, -EINVAL,
> +				     "Both 'tx' and 'rx' DMA channels are required");
> +	} else {
> +		dev_info(dev, "no DMA channels, using interrupt mode only");
> +	}

[Severity: Medium]
If one DMA channel is acquired and the second fails with a non-deferral
error (for example when the shared DMA controller has only one free channel
left), loongson_spi_io_request_dma_chan() logs "using interrupt mode" and
returns NULL, but the else-if branch here then fails probe with -EINVAL.
Should loongson_spi_io_probe() fall back to interrupt-only mode when only
one channel is available, matching the comment on
loongson_spi_io_request_dma_chan()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796005812.git.zhoubinbin@loongson.cn?part=4

  reply	other threads:[~2026-11-30  2:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  2:32 [PATCH v2 0/5] spi: loongson: SPI-IO driver for Loongson-2K0300 Binbin Zhou
2026-11-30  2:33 ` [PATCH v2 1/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi compatible Binbin Zhou
2026-11-30  2:33 ` [PATCH v2 2/5] spi: dt-bindings: loongson,ls2k-spi: Document loongson,ls2k0300-spi-io compatible Binbin Zhou
2026-11-30  2:33 ` [PATCH v2 3/5] spi: loongson: Add spi-io interrupt-driven driver Binbin Zhou
2026-11-30  2:48   ` sashiko-bot
2026-11-30  2:33 ` [PATCH v2 4/5] spi: loongson: Add DMA support to SPI-IO driver Binbin Zhou
2026-11-30  2:53   ` sashiko-bot [this message]
2026-11-30  2:33 ` [PATCH v2 5/5] MAINTAINERS: Add myself as Loongson SPI maintainer Binbin Zhou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-240273@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zhoubinbin@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox