From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796007200; b=KDnvYzGIJZAf6U+yub4lTPU1toAL4ZFbq64qAG8w8Wy0G79EqCPLIcokIlmXIuqRjf4N TpcQ7pW0BOjr8h7/c0K8F2Zs0DEAL6WpVrRCTk9LFaYVR6PTmMPm2pHefulFPxT1oql8I FilpwZziw7f9D5ICu2EqPvzUD0GcX4B2PWlEudVD6S6/0LouoULfEaaIFc4x+67Xoae3O vPM7zl6fj0EvNqSum+IuufvAPddyuQdsVsSSVjfnEhfp4fQlgI3jkk5CokCK1z4V8dyFh jeP9YUbf3g6wUy1fe9xnxbN8GtIwRBD/yWdzdFXC0GpI/snLogf2GIztxFRMuQSSL1Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796007200; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=T7W99XmctIYQQLw5WS5MAqNrJ4GqX6TIQWdsJOS2hMs=; b=f8zi42pBWbxoEKVUVz8/5ZbHOcwum+ovVL8r4//ML4/K7eEc9fh+Oe9oEPKDnoN9J4bc Nr+HVzvnfrI39lo7E4KOqof2xf+L9jXXYWwvtlVG3x/deKU/bg+LhsT2OLkv1s6I411hp QD3qGDxGFdqFE7iWsYkfnRdWsYe3KgOcXbb5gFFhoCBZC3O5rgCUGLsWOfwBNY6Z/0B/m /+TJhDKXmb9vNQZp09pMO2vFQeqc6cJSEFTirTtX+D61oCLP2T5ckDBFUByo6yKTzrv4d 66tbXyE4wcxlKP41KUtpU36YfZw2EeIBAl4aObCxXsRFgHth2prBp7Zh+oC103oKbOw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YrU0TZ8W; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C618E606A32A for ; Mon, 30 Nov 2026 02:53:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YrU0TZ8W Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7BCC1F00896; Mon, 30 Nov 2026 02:53:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796007200; bh=T7W99XmctIYQQLw5WS5MAqNrJ4GqX6TIQWdsJOS2hMs=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=YrU0TZ8WFooYWaZOVbPyyTddS8ht4y3w16r4NJDQQ3+JID5RXLaU0wqaQyH/T/1os 94silFit4R4R0cbwoS4QsyB1favA4DITngX0w+KPLUjCTRg8tXUpbZBHl9TfrmOB8z pwU5yXAVAiGQu5epoS5HH7Fv8IsBpwWijOYwKFpd2cCBgGJd5ra5Q6PxG+iR3zn/sR DNNs8qc56Nrz9i98/bC+gL18BRp5gS6LWlo7ynCYA0qVIEyVbzTZ3WA/VYj32hf2JU lXZx37bfmZApJ2VXTd6teyGFAv5ZEmln66VJktakm069qtaXAO7snvDH//h42KCbbK mwq3nFrCudlyQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2 4/5] spi: loongson: Add DMA support to SPI-IO driver Reply-To: sashiko-reviews@lists.linux.dev To: "Binbin Zhou" Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org In-Reply-To: References: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 02:53:19 +0000 X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 loo= ngson_spi_priv *priv) > LOONGSON_SPI_IO_CR1_START_MASK); > } > =20 [ ... ] > +static int loongson_spi_dma_transfer(struct loongson_spi_priv *priv, > + struct spi_transfer *xfer, > + bool *bus_started) > +{ > + u32 frames =3D xfer->len / priv->frame_bytes; > + u32 done, chunk_frames; > + int ret; > + > + *bus_started =3D false; > + > + ret =3D loongson_spi_dma_config_and_submit(priv, xfer); > + if (ret) > + goto out_stop; > + > + priv->xfer_ret =3D 0; > + priv->xfer_mode =3D 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 =3D 0; done < frames; done +=3D chunk_frames) { > + chunk_frames =3D 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 =3D 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 =3D 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 =3D true; > + loongson_spi_update_bits(priv, LOONGSON_SPI_IO_CR1_REG, > + LOONGSON_SPI_IO_CR1_CSTART, > + LOONGSON_SPI_IO_CR1_CSTART); > + > + ret =3D 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 () loongson_spi_io_transfer_one_dma() loongson_spi_update_bits(..., LOONGSON_SPI_IO_IER_REG, ...) loongson_spi_dma_transfer() priv->xfer_mode =3D 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 =3D -EIO; complete(&priv->xfer_completion); priv->xfer_ret =3D 0; reinit_completion(&priv->xfer_completion); ... loongson_spi_dma_wait_eot() <-- waits with IER_REG =3D=3D 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 =3D 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 () 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 =3D 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; > } > =20 > +/** > + * loongson_spi_io_transfer_one_dma() - Execute one DMA-driven SPI trans= fer > + * @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 mod= e 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, stru= ct spi_transfer *xfer) > +{ [ ... ] > + ret =3D 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", re= t); > + > + 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 |=3D 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 batch= es. > + * 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_co= ntroller *ctrl, struct spi_ > if (ret) > return ret; > =20 > + 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 =3D=3D NULL, xfer->rx_sg.nents =3D=3D 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(struc= t spi_controller *ctrl, > return 0; > } > =20 > +/** > + * 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 mod= e. > + * 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 =3D devm_dma_request_chan(dev, name); > + > + if (IS_ERR(chan)) { > + int ret =3D PTR_ERR(chan); > + > + if (ret =3D=3D -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_de= vice *pdev) > LOONGSON_SPI_IO_CFG2_MAX_BRINT), > U32_MAX); > =20 > + priv->dma_tx =3D loongson_spi_io_request_dma_chan(dev, "tx"); > + if (IS_ERR(priv->dma_tx)) > + return PTR_ERR(priv->dma_tx); > + > + priv->dma_rx =3D 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 =3D true; > + /* Let SPI core provide and map dummy buffers for full-duplex DMA. */ > + host->flags |=3D SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX; > + host->dma_tx =3D priv->dma_tx; > + host->dma_rx =3D priv->dma_rx; > + host->can_dma =3D 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()? --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/cover.1796005812.gi= t.zhoubinbin@loongson.cn?part=3D4