Skip to contentSkip to conversation

spi: axi-spi-engine: fix stale SYNC IRQ pending

PATCH Superseded Jonathan Santos 4 participants 8 messages Sep 30 – Oct 1

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover spi: axi-spi-engine: fix stale SYNC IRQ pending

Both spi_engine_setup() and spi_engine_trigger_enable() send a SYNC(1) command and poll SYNC_ID, but never clear INT_PENDING[SYNC] afterward. This leaves a stale pending bit that fires spuriously when INT_SYNC is enabled later by some driver, causing premature transfer completion at low SPI clock frequencies (more likely). Fix both functions by clearing the pending bit after polling.

v2 summary:
* Replaced "Link" tag for "Closes" tag.
* Included "Fixes" tag.
* Added new patch applying similar fix for the spi_engine_trigger_enable(),
as pointed out by David.
* While at it, replaced plain "1000" by "1 * USEC_PER_MSEC" for better
clarity.
* Link to v1: https://lore.kernel.org/linux-spi/04d51e99b8cddce51113db933d806e83848f04f5.1788313558.git.Jonathan.Santos@analog.com/T/#t

Jonathan Santos (2):
spi: axi-spi-engine: fix stale SYNC IRQ pending
spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

 drivers/spi/spi-axi-spi-engine.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

base-commit: a3b3580713f3ac5a32dc2874ee546828977a1d68

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 spi: axi-spi-engine: fix stale SYNC IRQ pending

spi_engine_setup() sends a SYNC(1) command and polls SYNC_ID to confirm it was parsed by the FPGA, but never clears the corresponding interrupt pending bit (INT_PENDING[SYNC]). When the first real SPI transfer starts and INT_SYNC is enabled, that stale pending bit fires immediately, causing the IRQ handler to see the leftover SYNC_ID from setup, match it against the current transfer's ID, and prematurely signal completion before the hardware finishes.

This race manifests at low SPI clock frequencies (~2-3 MHz), where the FPGA takes long enough to execute the transfer that handler is parsed before it finishes. At higher SCLK rates the transfer completes fast enough that the issue is masked.

Fix this by clearing INT_PENDING[SYNC] after the polled SYNC, ensuring no stale interrupt is left pending.

---
 drivers/spi/spi-axi-spi-engine.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/spi/spi-axi-spi-engine.c +8 -2
index 02bbc5d0cfc5..0de3af5c429c 100644
@@ -887,6 +887,7 @@ static int spi_engine_setup(struct spi_device *device)
 	struct spi_controller *host = device->controller;
 	struct spi_engine *spi_engine = spi_controller_get_devdata(host);
 	unsigned int reg;
+	int ret;
 
 	if (device->mode & SPI_CS_HIGH)
 		spi_engine->cs_inv |= BIT(spi_get_chipselect(device, 0));
@@ -922,8 +923,13 @@ static int spi_engine_setup(struct spi_device *device)
 	writel_relaxed(SPI_ENGINE_CMD_SYNC(1),
 		       spi_engine->base + SPI_ENGINE_REG_CMD_FIFO);
 
-	return readl_relaxed_poll_timeout(spi_engine->base + SPI_ENGINE_REG_SYNC_ID,
-					  reg, reg == 1, 1, 1000);
+	ret = readl_relaxed_poll_timeout(spi_engine->base + SPI_ENGINE_REG_SYNC_ID,
+					 reg, reg == 1, 1, 1 * USEC_PER_MSEC);
+
+	/* Clear the stale SYNC pending bit so it doesn't fire when the IRQ is later enabled */
+	writel_relaxed(SPI_ENGINE_INT_SYNC, spi_engine->base + SPI_ENGINE_REG_INT_PENDING);
+
+	return ret;
 }
 
 static int spi_engine_transfer_one_message(struct spi_controller *host,
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/2 spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

In reply to Jonathan Santos

spi_engine_trigger_enable() sends SYNC(1) and polls SYNC_ID, it never clears the INT_PENDING[SYNC] bit afterward. This leaves a stale pending interrupt that can fire spuriously when INT_SYNC is later enabled.

Simillarly to the fix on spi_engine_setup(), clear INT_PENDING[SYNC] after polling the SYNC_ID.

Fixes 8fc13b822c74 ("spi: axi-spi-engine: don't repeat mode config for offload")
---
 drivers/spi/spi-axi-spi-engine.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/spi/spi-axi-spi-engine.c +5 -1
index 0de3af5c429c..d0e5b7bccf55 100644
@@ -1034,7 +1034,11 @@ static int spi_engine_trigger_enable(struct spi_offload *offload)
 		spi_engine->base + SPI_ENGINE_REG_CMD_FIFO);
 
 	ret = readl_relaxed_poll_timeout(spi_engine->base + SPI_ENGINE_REG_SYNC_ID,
-					 reg, reg == 1, 1, 1000);
+					 reg, reg == 1, 1, 1 * USEC_PER_MSEC);
+
+	/* Clear the stale SYNC pending bit so it doesn't fire when the IRQ is later enabled */
+	writel_relaxed(SPI_ENGINE_INT_SYNC, spi_engine->base + SPI_ENGINE_REG_INT_PENDING);
+
 	if (ret)
 		return ret;
 
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v2 2/2] spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

drivers/spi/spi-axi-spi-engine.c static int spi_engine_trigger_enable(struct spi_offload *offload) · line 1043

Quoted 10 lines · Jonathan Santos

spi_engine_trigger_enable() sends SYNC(1) and polls SYNC_ID, it never clears the INT_PENDING[SYNC] bit afterward. This leaves a stale pending interrupt that can fire spuriously when INT_SYNC is later enabled.

drivers/spi/spi-axi-spi-engine.c +4 -0
+
+	/* Clear the stale SYNC pending bit so it doesn't fire when the IRQ is later enabled */
+	writel_relaxed(SPI_ENGINE_INT_SYNC, spi_engine->base + SPI_ENGINE_REG_INT_PENDING);
+
 	if (ret)
 		return ret;

I'm not seeing anything here for the issue with ensuring posted writes are completed before we drop the lock? The write may not be visible on enother CPU, we need a read from the controller or something.

#4 lore raw

Re: [PATCH v2 2/2] spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

Quoted 5 lines · Mark Brown
Quoted 3 lines · Jonathan Santos

spi_engine_trigger_enable() sends SYNC(1) and polls SYNC_ID, it never clears the INT_PENDING[SYNC] bit afterward. This leaves a stale pending interrupt that can fire spuriously when INT_SYNC is later enabled.

Quoted 12 lines
Quoted 8 lines
(quoted diff) +2 -0
+
+	/* Clear the stale SYNC pending bit so it doesn't fire when the IRQ is

later enabled */

(quoted diff) +1 -0
+	writel_relaxed(SPI_ENGINE_INT_SYNC, spi_engine->base +

SPI_ENGINE_REG_INT_PENDING);
+
  if (ret)
  return ret;

I'm not seeing anything here for the issue with ensuring posted writes are completed before we drop the lock?  The write may not be visible on enother CPU, we need a read from the controller or something.

Just for my understanding, why would it be an issue? The trigger register seems to only be accessible from the trigger callbacks which are protected by a lock where we have implied barriers. Im sure I'm missing what can go wrong here :)

- Nuno Sá

maintainer
#5 lore raw

Re: [PATCH v2 2/2] spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

Quoted 9 lines · Nuno Sá
Quoted 3 lines · Mark Brown

I'm not seeing anything here for the issue with ensuring posted writes are completed before we drop the lock?  The write may not be visible on enother CPU, we need a read from the controller or something.

Just for my understanding, why would it be an issue? The trigger register seems to only be accessible from the trigger callbacks which are protected by a lock where we have implied barriers. Im sure I'm missing what can go wrong here :)

Are you sure that's true for mutexes? I thought they only covered memory, not device stuff, but ICBW.

maintainer
#6 lore raw

Re: [PATCH v2 2/2] spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

Quoted 7 lines · Mark Brown
Quoted 5 lines · Nuno Sá
Quoted 3 lines · Mark Brown

I'm not seeing anything here for the issue with ensuring posted writes are completed before we drop the lock?  The write may not be visible on enother CPU, we need a read from the controller or something.

Quoted 6 lines
Quoted 3 lines

Just for my understanding, why would it be an issue? The trigger register seems to only be accessible from the trigger callbacks which are protected by a lock where we have implied barriers. Im sure I'm missing what can go wrong here :)

Are you sure that's true for mutexes? I thought they only covered memory, not device stuff, but ICBW.

Oh right, likely not a full barrier

- Nuno Sá

maintainer Reviewed-by on the v2 series
#7 lore raw
In reply to Jonathan Santos
Quoted 20 lines · Jonathan Santos

Both spi_engine_setup() and spi_engine_trigger_enable() send a SYNC(1) command and poll SYNC_ID, but never clear INT_PENDING[SYNC] afterward. This leaves a stale pending bit that fires spuriously when INT_SYNC is enabled later by some driver, causing premature transfer completion at low SPI clock frequencies (more likely). Fix both functions by clearing the pending bit after polling.

v2 summary:
* Replaced "Link" tag for "Closes" tag.
* Included "Fixes" tag.
* Added new patch applying similar fix for the spi_engine_trigger_enable(),
as pointed out by David.
* While at it, replaced plain "1000" by "1 * USEC_PER_MSEC" for better
clarity.
* Link to v1: https://lore.kernel.org/linux-spi/04d51e99b8cddce51113db933d806e83848f04f5.1788313558.git.Jonathan.Santos@analog.com/T/#t

Jonathan Santos (2):
spi: axi-spi-engine: fix stale SYNC IRQ pending
spi: axi-spi-engine: fix stale SYNC IRQ pending in trigger_enable

Quoted 8 lines
 drivers/spi/spi-axi-spi-engine.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

base-commit: a3b3580713f3ac5a32dc2874ee546828977a1d68

Signature · 1 line
2.34.1