Skip to contentSkip to conversation

spi: rockchip: Disable local irq when pio write out of interrupt service

PATCH In review Jon Lin 2 participants 4 messages Jun 13, 2022 – Jun 17, 2022 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Jon Lin

author
#0 lore raw

PATCH 1/1 spi: rockchip: Disable local irq when pio write out of interrupt service

Avoid interrupt come and interrupt the pio_writer.

---

 drivers/spi/spi-rockchip.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/spi/spi-rockchip.c +10 -2
index a08215eb9e14..2184de146928 100644
@@ -199,6 +199,7 @@ struct rockchip_spi {
 	bool cs_high_supported; /* native CS supports active-high polarity */
 
 	struct spi_transfer *xfer; /* Store xfer temporarily */
+	spinlock_t lock; /* prevent I/O concurrent access */
 };
 
 static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
@@ -293,9 +294,13 @@ static void rockchip_spi_handle_err(struct spi_controller *ctlr,
 
 static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
 {
-	u32 tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
-	u32 words = min(rs->tx_left, tx_free);
+	u32 tx_free;
+	u32 words;
+	unsigned long flags;
 
+	spin_lock_irqsave(&rs->lock, flags);
+	tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
+	words = min(rs->tx_left, tx_free);
 	rs->tx_left -= words;
 	for (; words; words--) {
 		u32 txw;
@@ -308,6 +313,7 @@ static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
 		rs->tx += rs->n_bytes;
 	}
+	spin_unlock_irqrestore(&rs->lock, flags);
 }
 
 static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
@@ -779,6 +785,8 @@ static int rockchip_spi_probe(struct platform_device *pdev)
 		goto err_put_ctlr;
 	}
 
+	spin_lock_init(&rs->lock);
+
 	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
 	if (IS_ERR(rs->apb_pclk)) {
 		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
Signature · 7 lines
2.17.1


_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
maintainer
#1 lore raw

drivers/spi/spi-rockchip.c static void rockchip_spi_pio_writer(struct rockchip_spi *rs) · line 316

Quoted 13 lines · Jon Lin

Avoid interrupt come and interrupt the pio_writer.

drivers/spi/spi-rockchip.c +4 -0
+	spin_lock_irqsave(&rs->lock, flags);
+	tx_free = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
+	words = min(rs->tx_left, tx_free);
 	rs->tx_left -= words;
 	for (; words; words--) {
 		u32 txw;
@@ -308,6 +313,7 @@ static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
 		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
 		rs->tx += rs->n_bytes;
 	}
+	spin_unlock_irqrestore(&rs->lock, flags);

So this is effectively just disabling interrupts during PIO, there's no other users of the lock which is rather heavyweight. What's the actual issue here? We should also have something saying what's going on in the code since right now the lock just looks redundant.

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#3 lore raw
Quoted 10 lines · Jon Lin
Quoted 8 lines · Mark Brown

drivers/spi/spi-rockchip.c static void rockchip_spi_pio_writer(struct rockchip_spi *rs) · line 316

Quoted 1 line · Jon Lin
drivers/spi/spi-rockchip.c +1 -0
+	spin_unlock_irqrestore(&rs->lock, flags);

So this is effectively just disabling interrupts during PIO, there's no other users of the lock which is rather heavyweight. What's the actual issue here? We should also have something saying what's going on in the code since right now the lock just looks redundant.

Quoted 4 lines

For lock: In order to avoid special situations, such as when the CPU frequency drops to close to the IO rate, the water line interrupt is triggered during FIFO filling (triggered by other CPUs), resulting in critical resources still not being protected in place. For local IRQ

So essentially we're so slow in filling the FIFO when starting a transfer that the interrupt triggers in the middle of the initial FIFO fill? Something that tricky *really* needs a comment adding.

Ideally we'd just leave the interrupt masked until the FIFO is filled though, looking at the driver I see that there is an interrupt mask register which seems to have some level of masking available and I do note that in rockchip_spi_prepare_irq() we unmask interrupts before we start filling the FIFO rather than afterwards. Would reversing the unmask order there address the issue more cleanly?

Quoted 2 lines

disable: Turning off the local interrupt is mainly to prevent the CPU schedule from being interrupted when filling FIFO.

If it were just this then there's preempt_disable(), but what's the problem with being preempted during the FIFO fill?

author
#4 lore raw
Quoted 20 lines · Mark Brown
Quoted 10 lines · Jon Lin
Quoted 8 lines · Mark Brown

drivers/spi/spi-rockchip.c static void rockchip_spi_pio_writer(struct rockchip_spi *rs) · line 316

Quoted 1 line · Jon Lin
drivers/spi/spi-rockchip.c +1 -0
+	spin_unlock_irqrestore(&rs->lock, flags);

So this is effectively just disabling interrupts during PIO, there's no other users of the lock which is rather heavyweight. What's the actual issue here? We should also have something saying what's going on in the code since right now the lock just looks redundant.

Quoted 4 lines

For lock: In order to avoid special situations, such as when the CPU frequency drops to close to the IO rate, the water line interrupt is triggered during FIFO filling (triggered by other CPUs), resulting in critical resources still not being protected in place. For local IRQ

So essentially we're so slow in filling the FIFO when starting a transfer that the interrupt triggers in the middle of the initial FIFO fill? Something that tricky *really* needs a comment adding.

Quoted 6 lines

Ideally we'd just leave the interrupt masked until the FIFO is filled though, looking at the driver I see that there is an interrupt mask register which seems to have some level of masking available and I do note that in rockchip_spi_prepare_irq() we unmask interrupts before we start filling the FIFO rather than afterwards. Would reversing the unmask order there address the issue more cleanly?

This idea is workable, and it's more efficient than previous code, So I send a new commit:
https://patchwork.kernel.org/project/spi-devel-general/patch/[email protected]/

Quoted 6 lines
Quoted 3 lines

disable: Turning off the local interrupt is mainly to prevent the CPU schedule from being interrupted when filling FIFO.

If it were just this then there's preempt_disable(), but what's the problem with being preempted during the FIFO fill?

I think