mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes
@ 2026-11-16 13:31 Emfox Zhou
  2026-11-16 13:31 ` [PATCH 1/5] HID: intel-thc-hid: intel-thc: keep TxDMA write errors Emfox Zhou
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Emfox Zhou @ 2026-11-16 13:31 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

This series fixes the DMA lifetime problems a protocol driver above the
THC core cannot work around, plus two smaller issues next to them.

Patch 1 makes thc_dma_write() report the error it found instead of
overwriting it with the result of the interrupt restore, and stops a
signal from turning a committed transfer into a restartable one. The
completion interrupt has to wake the queue with wake_up(), because the
new wait is uninterruptible.

Patch 2 unwinds a failed allocation through thc_dma_release(), so the
channel whose setup failed is freed too, and frees a scatter-gather list
that was allocated but never mapped.

Patch 3 stops all four engines before thc_dma_unconfigure() clears their
PRD base addresses; only the two receive engines are stopped today.

Patch 4 adds the low frequency SPI gears, down to 2.23 MHz.

Patch 5 gives the receive engines a drain budget that covers a full frame
at that gear, and leaves the SWDMA path on the old one.

These apply to v7.4-rc3. They are independent of the PRD table capacity
fix posted separately; the two are both needed for the recovery path to be
complete, but neither depends on the other.

Emfox Zhou (5):
  HID: intel-thc-hid: intel-thc: keep TxDMA write errors
  HID: intel-thc-hid: intel-thc: free the failed channel
  HID: intel-thc-hid: intel-thc: stop all engines first
  HID: intel-thc-hid: intel-thc: add the slow SPI gears
  HID: intel-thc-hid: intel-thc: budget the receive drain

 .../intel-thc-hid/intel-thc/intel-thc-dev.c   | 45 ++++++++---
 .../intel-thc-hid/intel-thc/intel-thc-dma.c   | 77 +++++++++++--------
 .../intel-thc-hid/intel-thc/intel-thc-dma.h   |  5 ++
 3 files changed, 86 insertions(+), 41 deletions(-)


base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] HID: intel-thc-hid: intel-thc: keep TxDMA write errors
  2026-11-16 13:31 [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes Emfox Zhou
@ 2026-11-16 13:31 ` Emfox Zhou
  2026-11-16 13:31 ` [PATCH 2/5] HID: intel-thc-hid: intel-thc: free the failed channel Emfox Zhou
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Emfox Zhou @ 2026-11-16 13:31 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

thc_dma_write() reports success after a timeout: the cleanup at the end
overwrites the error with the result of thc_interrupt_quiesce(), so a
frame that never left the device is taken as sent.

That wait also ends on a signal, and the no-completion branch folds the
result into -ETIMEDOUT. There is nothing to restart here: the frame is
already in the engine, so a restarted write() would hand the same report
to the controller twice. Wait without interruption, the window is one
second, and report a failing interrupt restore instead of dropping it.

The completion interrupt wakes the queue with wake_up_interruptible(),
whose mask does not intersect TASK_UNINTERRUPTIBLE: a completion that
arrives after the task sleeps is dropped, and the write waits out its
timer with the bus lock held. Wake all waiters instead.

Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
 drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c | 2 +-
 drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c | 9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
index 7feda4160..7429f851c 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
@@ -990,7 +990,7 @@ int thc_interrupt_handler(struct thc_device *dev)
 			     THC_M_PRT_WRITE_INT_STS_OFFSET, write_sts);
 
 		dev->write_done = true;
-		wake_up_interruptible(&dev->write_complete_wait);
+		wake_up(&dev->write_complete_wait);
 
 		interrupt_type |= BIT(THC_TXDMA_INT);
 	}
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index c80ef8c20..c0dc44e26 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -1045,8 +1045,9 @@ int thc_dma_write(struct thc_device *dev, void *buffer, size_t buf_len)
 
 	dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_TXDMA]);
 
-	ret = wait_event_interruptible_timeout(dev->write_complete_wait, dev->write_done, 1 * HZ);
-	if (ret <= 0 || !dev->write_done) {
+	ret = wait_event_timeout(dev->write_complete_wait,
+				 dev->write_done, 1 * HZ);
+	if (!ret || !dev->write_done) {
 		dev_err_once(dev->dev, "timeout for waiting TxDMA completion\n");
 		ret = -ETIMEDOUT;
 		goto end;
@@ -1059,8 +1060,8 @@ int thc_dma_write(struct thc_device *dev, void *buffer, size_t buf_len)
 end:
 	mutex_unlock(&dev->thc_bus_lock);
 
-	if (restore_interrupts)
-		ret = thc_interrupt_quiesce(dev, false);
+	if (restore_interrupts && thc_interrupt_quiesce(dev, false))
+		dev_err(dev->dev, "failed to restore interrupts after TxDMA write\n");
 
 	return ret;
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/5] HID: intel-thc-hid: intel-thc: free the failed channel
  2026-11-16 13:31 [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes Emfox Zhou
  2026-11-16 13:31 ` [PATCH 1/5] HID: intel-thc-hid: intel-thc: keep TxDMA write errors Emfox Zhou
@ 2026-11-16 13:31 ` Emfox Zhou
  2026-11-16 13:31 ` [PATCH 3/5] HID: intel-thc-hid: intel-thc: stop all engines first Emfox Zhou
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Emfox Zhou @ 2026-11-16 13:31 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

thc_dma_allocate() unwinds with "while (chan--)". The condition is read
before the decrement, so the first channel released is the one before the
failure, and the failed channel keeps the PRD tables and the
scatter-gather lists it did manage to allocate.

Unwind through thc_dma_release() instead, which walks every channel and
skips the ones that were never set up, and free a scatter-gather list that
was allocated but never mapped: sgl_alloc() leaves one behind when its
entry count is too large.

That leaves release_dma_buffers() with a configuration whose prd_tbls may
still be NULL, which its unconditional memset() does not survive. Clear the
tables only when there are any.

Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
 .../hid/intel-thc-hid/intel-thc/intel-thc-dma.c   | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index c0dc44e26..704c02822 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -297,20 +297,20 @@ static void release_dma_buffers(struct thc_device *dev,
 		return;
 
 	for (i = 0; i < config->prd_tbl_num; i++) {
-		if (!config->sgls[i] || !config->sgls_nent[i])
+		if (!config->sgls[i])
 			continue;
 
-		dma_unmap_sg(dev->dev, config->sgls[i],
-			     config->sgls_nent_pages[i],
-			     config->dir);
+		if (config->sgls_nent[i])
+			dma_unmap_sg(dev->dev, config->sgls[i],
+				     config->sgls_nent_pages[i],
+				     config->dir);
 
 		sgl_free(config->sgls[i]);
 		config->sgls[i] = NULL;
 	}
 
-	memset(config->prd_tbls, 0, prd_tbls_size);
-
 	if (config->prd_tbls) {
+		memset(config->prd_tbls, 0, prd_tbls_size);
 		dma_free_coherent(dev->dev, prd_tbls_size, config->prd_tbls,
 				  config->prd_tbls_dma_handle);
 		config->prd_tbls = NULL;
@@ -425,8 +425,7 @@ int thc_dma_allocate(struct thc_device *dev)
 	return 0;
 
 release_bufs:
-	while (chan--)
-		release_dma_buffers(dev, &dev->dma_ctx->dma_config[chan]);
+	thc_dma_release(dev);
 
 	return ret;
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/5] HID: intel-thc-hid: intel-thc: stop all engines first
  2026-11-16 13:31 [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes Emfox Zhou
  2026-11-16 13:31 ` [PATCH 1/5] HID: intel-thc-hid: intel-thc: keep TxDMA write errors Emfox Zhou
  2026-11-16 13:31 ` [PATCH 2/5] HID: intel-thc-hid: intel-thc: free the failed channel Emfox Zhou
@ 2026-11-16 13:31 ` Emfox Zhou
  2026-11-16 13:31 ` [PATCH 4/5] HID: intel-thc-hid: intel-thc: add the slow SPI gears Emfox Zhou
  2026-11-16 13:31 ` [PATCH 5/5] HID: intel-thc-hid: intel-thc: budget the receive drain Emfox Zhou
  4 siblings, 0 replies; 6+ messages in thread
From: Emfox Zhou @ 2026-11-16 13:31 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

thc_dma_unconfigure() clears the PRD base address and the control
registers of every channel, but stops only the two receive engines. The
write and software DMA engines keep their start bit, so an engine that is
still running is left pointing at a table base of zero.

Stop all four engines first, and clear the tables afterwards. The four
start clears move to thc_clear_start_bits(), next to the per channel
setter, and thc_reset_dma_settings() uses it as well, so the set of
engines is written down once.

Fixes: a688404b2e20 ("HID: intel-thc-hid: intel-thc: Add THC DMA interfaces")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
 .../intel-thc-hid/intel-thc/intel-thc-dma.c   | 33 +++++++++++--------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index 704c02822..41efdfaf0 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -94,6 +94,18 @@ static void dma_set_start_bit(struct thc_device *dev,
 	}
 }
 
+static void thc_clear_start_bits(struct thc_device *dev)
+{
+	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_1_OFFSET,
+			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
+	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_2_OFFSET,
+			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
+	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_SW_OFFSET,
+			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
+	regmap_write_bits(dev->thc_regmap, THC_M_PRT_WRITE_DMA_CNTRL_OFFSET,
+			  THC_M_PRT_WRITE_DMA_CNTRL_THC_WRDMA_START, 0);
+}
+
 static void dma_set_prd_control(struct thc_device *dev, u8 entry_count, u8 cb_depth,
 				struct thc_dma_configuration *dma_config)
 {
@@ -267,14 +279,7 @@ static int setup_dma_buffers(struct thc_device *dev,
 static void thc_reset_dma_settings(struct thc_device *dev)
 {
 	/* Stop all DMA channels and reset DMA read pointers */
-	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_1_OFFSET,
-			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
-	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_2_OFFSET,
-			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
-	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_SW_OFFSET,
-			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
-	regmap_write_bits(dev->thc_regmap, THC_M_PRT_WRITE_DMA_CNTRL_OFFSET,
-			  THC_M_PRT_WRITE_DMA_CNTRL_THC_WRDMA_START, 0);
+	thc_clear_start_bits(dev);
 
 	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_1_OFFSET,
 			  THC_M_PRT_READ_DMA_CNTRL_TPCPR,
@@ -518,16 +523,16 @@ void thc_dma_unconfigure(struct thc_device *dev)
 {
 	int chan;
 
+	/*
+	 * Stop the engines first: one that is running keeps writing through
+	 * a table whose base is already gone.
+	 */
+	thc_clear_start_bits(dev);
+
 	for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
 		dma_set_prd_base_addr(dev, 0, &dev->dma_ctx->dma_config[chan]);
 		dma_clear_prd_control(dev, &dev->dma_ctx->dma_config[chan]);
 	}
-
-	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_1_OFFSET,
-			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
-
-	regmap_write_bits(dev->thc_regmap, THC_M_PRT_READ_DMA_CNTRL_2_OFFSET,
-			  THC_M_PRT_READ_DMA_CNTRL_START, 0);
 }
 EXPORT_SYMBOL_NS_GPL(thc_dma_unconfigure, "INTEL_THC");
 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/5] HID: intel-thc-hid: intel-thc: add the slow SPI gears
  2026-11-16 13:31 [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes Emfox Zhou
                   ` (2 preceding siblings ...)
  2026-11-16 13:31 ` [PATCH 3/5] HID: intel-thc-hid: intel-thc: stop all engines first Emfox Zhou
@ 2026-11-16 13:31 ` Emfox Zhou
  2026-11-16 13:31 ` [PATCH 5/5] HID: intel-thc-hid: intel-thc: budget the receive drain Emfox Zhou
  4 siblings, 0 replies; 6+ messages in thread
From: Emfox Zhou @ 2026-11-16 13:31 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

The frequency table stops at 7.8125 MHz, so a device that asks for a
slower clock is rejected with -EINVAL. The divider enum already covers
that range: with the low frequency bit set the divider is multiplied by
eight (see intel-thc-hw.h), which brings DIV_7 down to 2.23 MHz.

Add the five missing gears. The half divider check exempts the new 5.2083
MHz gear, which is already DIV_3 with the eighth divider enabled, so the
extra half divider would land on top of it. The 10.4167 MHz gear is low
frequency as well but keeps the half divider; it is not part of this
change.

The read and write configuration carried identical copies of that check,
so move it to thc_spi_needs_half_div().

Signed-off-by: Emfox Zhou <emfox@debian.org>
---
 .../intel-thc-hid/intel-thc/intel-thc-dev.c   | 43 +++++++++++++++----
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
index 7429f851c..e9c3462e6 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dev.c
@@ -1111,6 +1111,12 @@ int thc_port_select(struct thc_device *dev, enum thc_port_type port_type)
 }
 EXPORT_SYMBOL_NS_GPL(thc_port_select, "INTEL_THC");
 
+/* 125 MHz / (8 * N), with the low frequency bit set. */
+#define THC_SPI_FREQUENCY_2M2	2232143
+#define THC_SPI_FREQUENCY_2M6	2604167
+#define THC_SPI_FREQUENCY_3M1	3125000
+#define THC_SPI_FREQUENCY_3M9	3906250
+#define THC_SPI_FREQUENCY_5M2	5208333
 #define THC_SPI_FREQUENCY_7M	7812500
 #define THC_SPI_FREQUENCY_10M	10416700
 #define THC_SPI_FREQUENCY_15M	15625000
@@ -1127,6 +1133,11 @@ EXPORT_SYMBOL_NS_GPL(thc_port_select, "INTEL_THC");
 static u8 thc_get_spi_freq_div_val(struct thc_device *dev, u32 spi_freq_val)
 {
 	static const int frequency[] = {
+		THC_SPI_FREQUENCY_2M2,
+		THC_SPI_FREQUENCY_2M6,
+		THC_SPI_FREQUENCY_3M1,
+		THC_SPI_FREQUENCY_3M9,
+		THC_SPI_FREQUENCY_5M2,
 		THC_SPI_FREQUENCY_7M,
 		THC_SPI_FREQUENCY_10M,
 		THC_SPI_FREQUENCY_15M,
@@ -1139,6 +1150,11 @@ static u8 thc_get_spi_freq_div_val(struct thc_device *dev, u32 spi_freq_val)
 		THC_SPI_FREQUENCY_50M,
 	};
 	static const u8 frequency_div[] = {
+		THC_SPI_FRQ_DIV_7,
+		THC_SPI_FRQ_DIV_6,
+		THC_SPI_FRQ_DIV_5,
+		THC_SPI_FRQ_DIV_4,
+		THC_SPI_FRQ_DIV_3,
 		THC_SPI_FRQ_DIV_2,
 		THC_SPI_FRQ_DIV_1,
 		THC_SPI_FRQ_DIV_1,
@@ -1174,6 +1190,23 @@ static u8 thc_get_spi_freq_div_val(struct thc_device *dev, u32 spi_freq_val)
 	return freq_div;
 }
 
+/*
+ * 10M, 35M and 50M take the half divider; 5.2083 MHz is already DIV_3, and
+ * 10.4167 MHz keeps it.
+ */
+static bool thc_spi_needs_half_div(u8 freq_div, u32 spi_freq_val,
+				   bool is_low_freq)
+{
+	if (freq_div == THC_SPI_FRQ_DIV_2)
+		return spi_freq_val >= THC_SPI_FREQUENCY_50M;
+
+	if (freq_div == THC_SPI_FRQ_DIV_3)
+		return spi_freq_val < THC_SPI_FREQUENCY_41M && !is_low_freq;
+
+	return freq_div == THC_SPI_FRQ_DIV_1 &&
+	       spi_freq_val < THC_SPI_FREQUENCY_15M;
+}
+
 /**
  * thc_spi_read_config - Configure SPI bus read attributes
  *
@@ -1199,10 +1232,7 @@ int thc_spi_read_config(struct thc_device *dev, u32 spi_freq_val,
 	if (spi_freq_val < THC_SPI_LOW_FREQUENCY)
 		is_low_freq = true;
 
-	/* 10M, 35M and 50M CLK need 1.5, 3.5 and 2.5 half divider */
-	if ((freq_div == THC_SPI_FRQ_DIV_2 && spi_freq_val >=  THC_SPI_FREQUENCY_50M) ||
-		(freq_div == THC_SPI_FRQ_DIV_3 && spi_freq_val <  THC_SPI_FREQUENCY_41M) ||
-		(freq_div == THC_SPI_FRQ_DIV_1 && spi_freq_val <  THC_SPI_FREQUENCY_15M)) {
+	if (thc_spi_needs_half_div(freq_div, spi_freq_val, is_low_freq)) {
 		regmap_write_bits(dev->thc_regmap, THC_M_PRT_SPI_DUTYC_CFG_OFFSET,
 				  THC_M_PRT_SPI_DUTYC_CFG_SPI_TCRF_HALF_DIV_EN,
 				  THC_M_PRT_SPI_DUTYC_CFG_SPI_TCRF_HALF_DIV_EN);
@@ -1271,10 +1301,7 @@ int thc_spi_write_config(struct thc_device *dev, u32 spi_freq_val,
 	if (spi_freq_val < THC_SPI_LOW_FREQUENCY)
 		is_low_freq = true;
 
-	/* 10M, 35M and 50M CLK need 1.5, 3.5 and 2.5 half divider */
-	if ((freq_div == THC_SPI_FRQ_DIV_2 && spi_freq_val >=  THC_SPI_FREQUENCY_50M) ||
-		(freq_div == THC_SPI_FRQ_DIV_3 && spi_freq_val <  THC_SPI_FREQUENCY_41M) ||
-		(freq_div == THC_SPI_FRQ_DIV_1 && spi_freq_val <  THC_SPI_FREQUENCY_15M)) {
+	if (thc_spi_needs_half_div(freq_div, spi_freq_val, is_low_freq)) {
 		regmap_write_bits(dev->thc_regmap, THC_M_PRT_SPI_DUTYC_CFG_OFFSET,
 				  THC_M_PRT_SPI_DUTYC_CFG_SPI_TCWF_HALF_DIV_EN,
 				  THC_M_PRT_SPI_DUTYC_CFG_SPI_TCWF_HALF_DIV_EN);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 5/5] HID: intel-thc-hid: intel-thc: budget the receive drain
  2026-11-16 13:31 [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes Emfox Zhou
                   ` (3 preceding siblings ...)
  2026-11-16 13:31 ` [PATCH 4/5] HID: intel-thc-hid: intel-thc: add the slow SPI gears Emfox Zhou
@ 2026-11-16 13:31 ` Emfox Zhou
  4 siblings, 0 replies; 6+ messages in thread
From: Emfox Zhou @ 2026-11-16 13:31 UTC (permalink / raw)
  To: Even Xu, Xinpeng Sun, Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-kernel

thc_wait_for_dma_pause() gives every engine 10 ms to report idle, but a
receive engine can be draining a whole frame. Its buffer comes from the
HIDSPI device descriptor's max_input_len, rounded up to 4 KB and capped at
16 PRD entries, so up to 64 KB; at the 2.23 MHz gear that takes about
235 ms, and a timed out pause makes thc_rxdma_reset() return before the
engines are reconfigured, leaving the controller quiesced with the
interrupts off.

Let the callers pass the budget. thc_rxdma_reset() gets one that covers a
full frame at the slowest gear. The SWDMA path keeps the old one: it
pauses the receive engines with the bus lock held and the interrupts
quiesced, and its frames are small.

Fixes: 1d46b8d40655 ("HID: Intel-thc-hid: Intel-thc: Add API to reset read DMA")
Signed-off-by: Emfox Zhou <emfox@debian.org>
---
 .../intel-thc-hid/intel-thc/intel-thc-dma.c   | 20 +++++++++++++------
 .../intel-thc-hid/intel-thc/intel-thc-dma.h   |  5 +++++
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
index 41efdfaf0..1e68b8845 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.c
@@ -536,7 +536,9 @@ void thc_dma_unconfigure(struct thc_device *dev)
 }
 EXPORT_SYMBOL_NS_GPL(thc_dma_unconfigure, "INTEL_THC");
 
-static int thc_wait_for_dma_pause(struct thc_device *dev, enum thc_dma_channel channel)
+static int thc_wait_for_dma_pause(struct thc_device *dev,
+				  enum thc_dma_channel channel,
+				  u32 timeout_us)
 {
 	u32 ctrl_reg, sts_reg, sts;
 	int ret;
@@ -554,7 +556,7 @@ static int thc_wait_for_dma_pause(struct thc_device *dev, enum thc_dma_channel c
 	ret = regmap_read_poll_timeout(dev->thc_regmap, sts_reg, sts,
 				       !(sts & THC_M_PRT_READ_DMA_INT_STS_ACTIVE),
 				       THC_DEFAULT_RXDMA_POLLING_US_INTERVAL,
-				       THC_DEFAULT_RXDMA_POLLING_US_TIMEOUT);
+				       timeout_us);
 
 	if (ret) {
 		dev_err_once(dev->dev,
@@ -588,13 +590,15 @@ int thc_rxdma_reset(struct thc_device *dev)
 		goto end;
 	}
 
-	ret = thc_wait_for_dma_pause(dev, THC_RXDMA1);
+	ret = thc_wait_for_dma_pause(dev, THC_RXDMA1,
+				     THC_RXDMA_DRAIN_US_TIMEOUT);
 	if (ret) {
 		dev_err(dev->dev, "Wait for RxDMA1 pause failed during RxDMA reset\n");
 		goto end;
 	}
 
-	ret = thc_wait_for_dma_pause(dev, THC_RXDMA2);
+	ret = thc_wait_for_dma_pause(dev, THC_RXDMA2,
+				     THC_RXDMA_DRAIN_US_TIMEOUT);
 	if (ret) {
 		dev_err(dev->dev, "Wait for RxDMA2 pause failed during RxDMA reset\n");
 		goto end;
@@ -769,7 +773,10 @@ static int thc_swdma_read_start(struct thc_device *dev, void *write_buff,
 	if (ret)
 		return ret;
 
-	if (thc_wait_for_dma_pause(dev, THC_RXDMA1) || thc_wait_for_dma_pause(dev, THC_RXDMA2))
+	if (thc_wait_for_dma_pause(dev, THC_RXDMA1,
+				   THC_DEFAULT_RXDMA_POLLING_US_TIMEOUT) ||
+	    thc_wait_for_dma_pause(dev, THC_RXDMA2,
+				   THC_DEFAULT_RXDMA_POLLING_US_TIMEOUT))
 		return -EIO;
 
 	thc_reset_dma_settings(dev);
@@ -834,7 +841,8 @@ static int thc_swdma_read_completion(struct thc_device *dev)
 {
 	int ret;
 
-	ret = thc_wait_for_dma_pause(dev, THC_SWDMA);
+	ret = thc_wait_for_dma_pause(dev, THC_SWDMA,
+				     THC_DEFAULT_RXDMA_POLLING_US_TIMEOUT);
 	if (ret)
 		return ret;
 
diff --git a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
index 715423453..fccaac122 100644
--- a/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
+++ b/drivers/hid/intel-thc-hid/intel-thc/intel-thc-dma.h
@@ -19,6 +19,11 @@
 #define THC_DEFAULT_RXDMA_POLLING_US_INTERVAL 100
 #define THC_DEFAULT_RXDMA_POLLING_US_TIMEOUT  (10 * USEC_PER_MSEC)
 
+/*
+ * 235 ms for a 64 KB frame at the slowest gear, so this covers a whole one.
+ */
+#define THC_RXDMA_DRAIN_US_TIMEOUT  (500 * USEC_PER_MSEC)
+
 /*
  * THC needs 1KB aligned address, dest_addr is 54 bits, not 64,
  * so don't need to send the lower 10-bits of address.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-16 13:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 13:31 [PATCH 0/5] HID: intel-thc-hid: intel-thc: DMA lifetime fixes Emfox Zhou
2026-11-16 13:31 ` [PATCH 1/5] HID: intel-thc-hid: intel-thc: keep TxDMA write errors Emfox Zhou
2026-11-16 13:31 ` [PATCH 2/5] HID: intel-thc-hid: intel-thc: free the failed channel Emfox Zhou
2026-11-16 13:31 ` [PATCH 3/5] HID: intel-thc-hid: intel-thc: stop all engines first Emfox Zhou
2026-11-16 13:31 ` [PATCH 4/5] HID: intel-thc-hid: intel-thc: add the slow SPI gears Emfox Zhou
2026-11-16 13:31 ` [PATCH 5/5] HID: intel-thc-hid: intel-thc: budget the receive drain Emfox Zhou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®