From: Emfox Zhou <emfox@debian.org>
To: Even Xu <even.xu@intel.com>, Xinpeng Sun <xinpeng.sun@intel.com>,
Jiri Kosina <jikos@kernel.org>,
Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v7 1/2] HID: intel-thc-hid: intel-thc: restore PRD table capacity
Date: Tue, 17 Nov 2026 18:54:26 +0800 [thread overview]
Message-ID: <20261117105427.319242-2-emfox@debian.org> (raw)
In-Reply-To: <20261117105427.319242-1-emfox@debian.org>
The controller writes the frame length into the PRD entry it filled and
marks the end of the frame there; only a read puts the capacity back.
thc_reset_dma_settings() rewinds the read pointers but not those lengths,
and stops the engines: a frame still queued when it runs is dropped, the
next frame that does not fit runs past the end of its entry, and the
controller reports a buffer overrun, a transfer error that leads back
into the same reset with the table still short.
Rebuild a table before the engine that reads it is started: on probe,
resume and the RxDMA reset, on the SWDMA start once that engine is idle,
and where an SWDMA round trip restarts the receive engine.
A drain in the threaded handler reads a table without the bus lock. The
rebuild, the arming, and every path that stops an engine or moves its
pointers run without that lock as well, so a drain can land in the middle
of any of them and read an entry of a ring that was already rewound. Give
the tables a mutex taken by all of them, inside the bus lock where the
caller holds it; no path takes the two in the other order.
The rewind leaves the read pointer at the start and the write pointer
where the last frame left it, so the ring reads as non-empty while the
engine is stopped and a drain would take an entry that was never written.
Record whether an engine runs, and read the ring only while it is set: the
flag is published under that same lock, after the control register is read
back, so a drain sees either a stopped engine or an armed one.
thc_dma_read() clears *read_len when it takes nothing, because callers
read that even when the read succeeds, and thc_copy_sgls_to_prd() returns
early for a channel whose tables are gone.
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 | 89 ++++++++++++++++++-
.../intel-thc-hid/intel-thc/intel-thc-dma.h | 8 ++
2 files changed, 93 insertions(+), 4 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 c80ef8c20..944db6150 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
@@ -30,10 +30,20 @@ static void dma_set_start_bit(struct thc_device *dev,
struct thc_dma_configuration *dma_config)
{
u32 ctrl, mask, mbits, data, offset;
+ bool reads_ring;
if (!dma_config->is_enabled)
return;
+ /*
+ * Arm and publish under the lock the read path takes: a ring that was
+ * just rewound still reads as non-empty, so a reader in between would
+ * copy an entry the engine never wrote. TxDMA reads no ring.
+ */
+ reads_ring = dma_config->dma_channel != THC_TXDMA;
+ if (reads_ring)
+ mutex_lock(&dev->dma_ctx->prd_lock);
+
switch (dma_config->dma_channel) {
case THC_RXDMA1:
case THC_RXDMA2:
@@ -92,6 +102,17 @@ static void dma_set_start_bit(struct thc_device *dev,
default:
break;
}
+
+ if (reads_ring) {
+ /*
+ * The mutex orders the flag, not the write to the device:
+ * read the register back so the arming lands first.
+ */
+ regmap_read(dev->thc_regmap, dma_config->dma_cntrl, &ctrl);
+
+ WRITE_ONCE(dma_config->is_started, true);
+ mutex_unlock(&dev->dma_ctx->prd_lock);
+ }
}
static void dma_set_prd_control(struct thc_device *dev, u8 entry_count, u8 cb_depth,
@@ -211,12 +232,27 @@ static void thc_copy_sgls_to_prd(struct thc_device *dev,
{
unsigned int i;
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
+ if (!config->is_enabled || !config->prd_tbls)
+ return;
+
memset(config->prd_tbls, 0, array_size(PRD_TABLE_SIZE, config->prd_tbl_num));
for (i = 0; i < config->prd_tbl_num; i++)
thc_copy_one_sgl_to_prd(dev, config, i);
}
+/*
+ * A frame that is never read leaves its entry short, and the engine reads
+ * the table again on every start: put the capacity back.
+ */
+static void thc_rebuild_rx_prd(struct thc_device *dev)
+{
+ thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA1]);
+ thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
+}
+
static int setup_dma_buffers(struct thc_device *dev,
struct thc_dma_configuration *config,
enum dma_data_direction dir)
@@ -266,6 +302,15 @@ static int setup_dma_buffers(struct thc_device *dev,
static void thc_reset_dma_settings(struct thc_device *dev)
{
+ struct thc_dma_context *dma_ctx = dev->dma_ctx;
+ int chan;
+
+ guard(mutex)(&dma_ctx->prd_lock);
+
+ /* The rewind below discards the queue, so no read may start. */
+ for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++)
+ WRITE_ONCE(dma_ctx->dma_config[chan].is_started, false);
+
/* 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);
@@ -327,6 +372,7 @@ struct thc_dma_context *thc_dma_init(struct thc_device *dev)
return NULL;
dev->dma_ctx = dma_ctx;
+ mutex_init(&dma_ctx->prd_lock);
dma_ctx->dma_config[THC_RXDMA1].dma_channel = THC_RXDMA1;
dma_ctx->dma_config[THC_RXDMA2].dma_channel = THC_RXDMA2;
@@ -441,6 +487,8 @@ void thc_dma_release(struct thc_device *dev)
{
int chan;
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++)
release_dma_buffers(dev, &dev->dma_ctx->dma_config[chan]);
}
@@ -484,13 +532,15 @@ int thc_dma_configure(struct thc_device *dev)
struct thc_dma_context *dma_ctx = dev->dma_ctx;
int chan;
- thc_reset_dma_settings(dev);
-
if (!dma_ctx) {
dev_err_once(dev->dev, "Cannot do DMA configure because DMA context is NULL\n");
return -EINVAL;
}
+ thc_reset_dma_settings(dev);
+
+ thc_rebuild_rx_prd(dev);
+
for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
dma_set_prd_base_addr(dev,
dma_ctx->dma_config[chan].prd_tbls_dma_handle,
@@ -519,7 +569,10 @@ void thc_dma_unconfigure(struct thc_device *dev)
{
int chan;
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
for (chan = 0; chan < MAX_THC_DMA_CHANNEL; chan++) {
+ WRITE_ONCE(dev->dma_ctx->dma_config[chan].is_started, false);
dma_set_prd_base_addr(dev, 0, &dev->dma_ctx->dma_config[chan]);
dma_clear_prd_control(dev, &dev->dma_ctx->dma_config[chan]);
}
@@ -541,7 +594,13 @@ static int thc_wait_for_dma_pause(struct thc_device *dev, enum thc_dma_channel c
((channel == THC_RXDMA2) ? THC_M_PRT_READ_DMA_CNTRL_2_OFFSET :
THC_M_PRT_READ_DMA_CNTRL_SW_OFFSET);
- regmap_write_bits(dev->thc_regmap, ctrl_reg, THC_M_PRT_READ_DMA_CNTRL_START, 0);
+ {
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
+ WRITE_ONCE(dev->dma_ctx->dma_config[channel].is_started, false);
+ regmap_write_bits(dev->thc_regmap, ctrl_reg,
+ THC_M_PRT_READ_DMA_CNTRL_START, 0);
+ }
sts_reg = (channel == THC_RXDMA1) ? THC_M_PRT_READ_DMA_INT_STS_1_OFFSET :
((channel == THC_RXDMA2) ? THC_M_PRT_READ_DMA_INT_STS_2_OFFSET :
@@ -685,6 +744,21 @@ static int thc_dma_read(struct thc_device *dev,
u8 read_ptr, write_ptr, prd_table_index;
int status;
+ /* The caller may look at this when nothing was read. */
+ *read_len = 0;
+
+ guard(mutex)(&dev->dma_ctx->prd_lock);
+
+ /*
+ * Nothing in the ring is ours until the engine is started: after a
+ * reset the pointers alone say nothing about what was written.
+ */
+ if (!READ_ONCE(read_config->is_started)) {
+ if (read_finished)
+ *read_finished = 1;
+ return 0;
+ }
+
if (!is_dma_buf_empty(dev, read_config, &read_ptr, &write_ptr)) {
prd_table_index = write_ptr & THC_POINTER_MASK;
@@ -765,11 +839,16 @@ 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_wait_for_dma_pause(dev, THC_RXDMA2) ||
+ thc_wait_for_dma_pause(dev, THC_SWDMA))
return -EIO;
thc_reset_dma_settings(dev);
+ /* A transfer that timed out left its frame length in the entry. */
+ thc_copy_sgls_to_prd(dev, &dev->dma_ctx->dma_config[THC_SWDMA]);
+
/*
* Max input size control feature is only available for RxDMA, it must keep disabled
* during SWDMA operation, and restore to previous state after SWDMA is done.
@@ -854,6 +933,8 @@ static int thc_swdma_read_completion(struct thc_device *dev)
thc_reset_dma_settings(dev);
+ thc_rebuild_rx_prd(dev);
+
dma_set_start_bit(dev, &dev->dma_ctx->dma_config[THC_RXDMA2]);
ret = thc_interrupt_quiesce(dev, false);
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..d05d16ea1 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
@@ -6,6 +6,7 @@
#include <linux/bits.h>
#include <linux/dma-mapping.h>
+#include <linux/mutex.h>
#include <linux/sizes.h>
#include <linux/time64.h>
#include <linux/types.h>
@@ -89,6 +90,7 @@ struct thc_prd_table {
* @dma_channel: DMA channel for current DMA configuration
* @prd_tbls_dma_handle: DMA buffer handle
* @dir: Direction of DMA for this config
+ * @is_started: The engine is running, so the ring may be read
* @prd_tbls: PRD tables for current DMA
* @sgls: Array of pointers to scatter-gather lists
* @sgls_nent_pages: Number of pages per scatter-gather list
@@ -105,6 +107,7 @@ struct thc_dma_configuration {
dma_addr_t prd_tbls_dma_handle;
enum dma_data_direction dir;
bool is_enabled;
+ bool is_started;
struct thc_prd_table *prd_tbls;
struct scatterlist *sgls[PRD_TABLES_NUM];
@@ -123,6 +126,8 @@ struct thc_dma_configuration {
* struct thc_dma_context - THC DMA context
* @thc_dma_configuration: Array of all THC Channel configures
* @use_write_interrupts: Indicate TxDMA using interrupt or polling
+ * @prd_lock: Keeps the DMA read path off the tables while they are
+ * rebuilt, armed, stopped or released
* @rx_max_size_en: Temp flag to indicate THC I2C Rx max input size control feature
* enabled or not, only be used during SWDMA operation.
* @rx_int_delay_en: Temp flag to indicate THC I2C Rx interrupt delay feature
@@ -132,6 +137,9 @@ struct thc_dma_context {
struct thc_dma_configuration dma_config[MAX_THC_DMA_CHANNEL];
u8 use_write_interrupts;
+ /* Taken inside the bus lock, never the other way round. */
+ struct mutex prd_lock;
+
bool rx_max_size_en;
bool rx_int_delay_en;
};
next prev parent reply other threads:[~2026-11-17 10:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 10:54 [PATCH v7 0/2] HID: intel-thc-hid: PRD tables under a drain Emfox Zhou
2026-11-17 10:54 ` Emfox Zhou [this message]
2026-11-17 10:54 ` [PATCH v7 2/2] HID: intel-thc-hid: quickspi, quicki2c: skip an empty read Emfox 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=20261117105427.319242-2-emfox@debian.org \
--to=emfox@debian.org \
--cc=bentiss@kernel.org \
--cc=even.xu@intel.com \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=xinpeng.sun@intel.com \
/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
all inboxes | Powered by JetHome®