From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
Cc: Vinod Koul <vkoul@kernel.org>,
Neil Armstrong <neil.armstrong@linaro.org>,
Manivannan Sadhasivam <mani@kernel.org>,
Bjorn Andersson <andersson@kernel.org>,
Konrad Dybcio <konradybcio@kernel.org>,
Abel Vesa <abelvesa@kernel.org>, Stephen Boyd <sboyd@kernel.org>,
Brian Masney <bmasney+clk@redhat.com>,
Jerome Brunet <jbrunet+clk@baylibre.com>,
Andreas Noever <andreas.noever@gmail.com>,
Mika Westerberg <westeri@kernel.org>,
Yehezkel Bernat <YehezkelShB@gmail.com>,
Mario Limonciello <mario.limonciello@amd.com>,
Philipp Zabel <p.zabel@pengutronix.de>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
linux-arm-msm@vger.kernel.org, linux-phy@lists.infradead.org,
linux-clk@vger.kernel.org, linux-usb@vger.kernel.org,
linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
usb4-upstream@oss.qualcomm.com
Subject: Re: [PATCH RFC DNM 07/15] thunderbolt: nhi: Process ring completions in the threaded IRQ
Date: Tue, 1 Dec 2026 08:33:20 +0100 [thread overview]
Message-ID: <20261201073320.GR953667@black.igk.intel.com> (raw)
In-Reply-To: <20261124-topic-next-20261116-fsm-mvp-v1-7-a9b45c69cd38@oss.qualcomm.com>
Hi,
On Tue, Nov 24, 2026 at 09:01:36PM +0100, Konrad Dybcio wrote:
> The Qualcomm USB4 router uses a shared IRQ for all NHI rings.
> Scheduling a work item from its threaded handler adds another delay
> before completed RX frames are consumed and replacement buffers are
> posted.
>
> Process completed descriptors directly from the threaded IRQ instead.
> Harvest all completed frames while holding the NHI and ring locks,
> replenish RX rings immediately, then invoke frame callbacks after
> dropping the locks. Preserve the existing polling-ring path, which must
> continue to mask the interrupt and start polling instead.
>
> This removes the workqueue round trip from the common IRQ path without
> calling ring consumers under NHI locking. Since the callbacks no longer
> run only from ring->work, have tb_ring_stop() and tb_ring_free()
> synchronize with the ring interrupt, so they keep returning only after
> all callbacks of the ring have finished.
>
> Switch the single MSI fallback of the PCI glue over to the same threaded
> handler, which allows dropping nhi_interrupt_work() and the work item
> that backed it.
The MSI code can go. All the hardware supports MSI-X so that code path is
really not needed. I think the MediaTek folks are doing that already
though.
> Based on related work by Wesley Cheng.
>
> Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
> ---
> drivers/thunderbolt/nhi.c | 133 +++++++++++++++++++++++++++++++++++---------
> drivers/thunderbolt/nhi.h | 2 +-
> drivers/thunderbolt/pci.c | 15 ++---
> include/linux/thunderbolt.h | 8 +--
> 4 files changed, 115 insertions(+), 43 deletions(-)
>
> diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
> index e48521482c021c7f5f73349ce16519d6088d3250..bcc2b959e3b1a6b014c13a4938ed6f68cb0d0fd8 100644
> --- a/drivers/thunderbolt/nhi.c
> +++ b/drivers/thunderbolt/nhi.c
> @@ -310,6 +310,41 @@ static void ring_write_descriptors(struct tb_ring *ring, bool notify)
> __ring_notify(ring);
> }
>
> +/*
Use /** for kernel doc
> + * ring_harvest() - collect completed frames
> + * @ring: Ring to collect the frames from
> + * @done: List to move the completed frames to
> + *
> + * Moves all completed frames from the ring buffer to @done and writes new
> + * frames to the ring buffer.
> + *
> + * ring->lock is held.
You can drop this comment if you have the lockdep_assert below.
> + */
> +static void ring_harvest(struct tb_ring *ring, struct list_head *done)
> +{
> + struct ring_frame *frame;
> +
> + lockdep_assert_held(&ring->lock);
> +
> + while (!ring_empty(ring)) {
> + if (!(ring->descriptors[ring->tail].flags
> + & RING_DESC_COMPLETED))
> + break;
> + frame = list_first_entry(&ring->in_flight, typeof(*frame),
> + list);
> + list_move_tail(&frame->list, done);
> + if (!ring->is_tx) {
> + frame->size = ring->descriptors[ring->tail].length;
> + frame->eof = ring->descriptors[ring->tail].eof;
> + frame->sof = ring->descriptors[ring->tail].sof;
> + frame->flags = ring->descriptors[ring->tail].flags;
> + }
> + frame->ring = ring;
> + ring->tail = (ring->tail + 1) % ring->size;
> + }
> + ring_write_descriptors(ring, true);
Please check if tb_ring_poll() can be made to use this as well.
> +}
> +
> /*
> * ring_work() - progress completed frames
> *
> @@ -337,22 +372,7 @@ static void ring_work(struct work_struct *work)
> goto invoke_callback;
> }
>
> - while (!ring_empty(ring)) {
> - if (!(ring->descriptors[ring->tail].flags
> - & RING_DESC_COMPLETED))
> - break;
> - frame = list_first_entry(&ring->in_flight, typeof(*frame),
> - list);
> - list_move_tail(&frame->list, &done);
> - if (!ring->is_tx) {
> - frame->size = ring->descriptors[ring->tail].length;
> - frame->eof = ring->descriptors[ring->tail].eof;
> - frame->sof = ring->descriptors[ring->tail].sof;
> - frame->flags = ring->descriptors[ring->tail].flags;
> - }
> - ring->tail = (ring->tail + 1) % ring->size;
> - }
> - ring_write_descriptors(ring, true);
> + ring_harvest(ring, &done);
>
> invoke_callback:
> /* allow callbacks to schedule new work */
> @@ -371,6 +391,35 @@ static void ring_work(struct work_struct *work)
> wake_up(&ring->wait);
> }
>
> +/*
Here too /**
> + * ring_process_completions() - invoke the callbacks of completed frames
> + * @done: Frames collected by ring_harvest(), possibly from several rings
> + *
> + * The frames of each ring are contiguous in @done. Like ring_work(), wake
> + * up the waiters of a ring once the callbacks of all its frames have run.
> + */
> +static void ring_process_completions(struct list_head *done)
> +{
> + struct ring_frame *frame;
> + struct tb_ring *ring;
> +
> + while (!list_empty(done)) {
struct ring_frame *frame;
struct tb_ring *ring;
> + frame = list_first_entry(done, typeof(*frame), list);
> + ring = frame->ring;
> + /*
> + * The callback may reenqueue or delete frame.
re-enqueue?
> + * Do not hold on to it.
> + */
> + list_del_init(&frame->list);
> + if (frame->callback)
> + frame->callback(ring, frame, false);
> +
This could use a comment. I think we can do the wake_up() outside of the
loop except when ring is changed. Whichever looks more readable.
> + if (list_empty(done) ||
> + list_first_entry(done, typeof(*frame), list)->ring != ring)
> + wake_up(&ring->wait);
> + }
> +}
> +
> int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame, bool more)
> {
> unsigned long flags;
> @@ -819,6 +868,19 @@ static bool tb_ring_empty(struct tb_ring *ring)
> return list_empty(&ring->in_flight);
> }
>
> +/*
Same here /**
ditto everywhere.
> + * ring_sync_irq() - wait for the interrupt handler of a ring
> + *
> + * nhi_msi_thread() invokes the callbacks of the frames it has harvested
> + * after dropping the locks, so make sure it has finished with any frames
> + * of a ring that has been stopped or dissociated from the NHI.
> + */
> +static void ring_sync_irq(struct tb_ring *ring)
> +{
> + if (ring->irq > 0)
> + synchronize_irq(ring->irq);
> +}
> +
> /**
> * tb_ring_flush() - Waits for a ring to be empty
> * @ring: Ring to wait
> @@ -881,6 +943,8 @@ void tb_ring_stop(struct tb_ring *ring)
> spin_unlock(&ring->lock);
> spin_unlock_irq(&ring->nhi->lock);
>
> + ring_sync_irq(ring);
> +
> /*
> * schedule ring->work to invoke callbacks on all remaining frames.
> */
> @@ -906,7 +970,7 @@ void tb_ring_free(struct tb_ring *ring)
> spin_lock_irq(&ring->nhi->lock);
> /*
> * Dissociate the ring from the NHI. This also ensures that
> - * nhi_interrupt_work cannot reschedule ring->work.
> + * nhi_msi_thread() cannot harvest its frames anymore.
> */
> if (ring->is_tx)
> ring->nhi->tx_rings[ring->hop] = NULL;
> @@ -919,6 +983,8 @@ void tb_ring_free(struct tb_ring *ring)
> }
> spin_unlock_irq(&ring->nhi->lock);
>
> + ring_sync_irq(ring);
> +
> if (nhi->ops->release_ring_irq)
> nhi->ops->release_ring_irq(ring);
>
> @@ -935,8 +1001,8 @@ void tb_ring_free(struct tb_ring *ring)
>
> /*
> * ring->work can no longer be scheduled (it is scheduled only
> - * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
> - * to finish before freeing the ring.
> + * by ring_stop and ring_msix). Wait for it to finish before
> + * freeing the ring.
> */
> flush_work(&ring->work);
> lockdep_unregister_key(&ring->lock_key);
> @@ -1022,16 +1088,18 @@ enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
> return (enum nhi_fw_mode)val;
> }
>
> -void nhi_interrupt_work(struct work_struct *work)
> +irqreturn_t nhi_msi_thread(int irq, void *data)
> {
> - struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
> + struct tb_nhi *nhi = data;
> + LIST_HEAD(completed_frames);
> + unsigned long flags;
> int value = 0; /* Suppress uninitialized usage warning. */
> int bit;
> int hop = -1;
> int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
> struct tb_ring *ring;
>
> - spin_lock_irq(&nhi->lock);
> + spin_lock_irqsave(&nhi->lock, flags);
>
> /*
> * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
> @@ -1073,17 +1141,28 @@ void nhi_interrupt_work(struct work_struct *work)
> }
>
> spin_lock(&ring->lock);
> - __ring_interrupt(ring);
> + /*
> + * Harvest the completions right away instead of deferring
> + * to ring->work, so that the RX buffers are reposted without
> + * another scheduling delay during a burst.
> + */
> + if (ring->start_poll)
> + __ring_interrupt(ring);
> + else if (ring->running)
> + ring_harvest(ring, &completed_frames);
> spin_unlock(&ring->lock);
> }
> - spin_unlock_irq(&nhi->lock);
> + spin_unlock_irqrestore(&nhi->lock, flags);
> +
> + if (!list_empty(&completed_frames))
> + ring_process_completions(&completed_frames);
> +
> + return IRQ_HANDLED;
> }
>
> irqreturn_t nhi_msi(int irq, void *data)
> {
> - struct tb_nhi *nhi = data;
> - schedule_work(&nhi->interrupt_work);
> - return IRQ_HANDLED;
> + return IRQ_WAKE_THREAD;
> }
>
> static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
> diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
> index 88af9ab9fde7afc8af6da412b668663e67380e53..71f750451aacae6ca2f8cb37bd8ae7fc5a029818 100644
> --- a/drivers/thunderbolt/nhi.h
> +++ b/drivers/thunderbolt/nhi.h
> @@ -31,8 +31,8 @@ int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data);
> enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi);
> void nhi_enable_int_throttling(struct tb_nhi *nhi);
> void nhi_disable_interrupts(struct tb_nhi *nhi);
> -void nhi_interrupt_work(struct work_struct *work);
> irqreturn_t nhi_msi(int irq, void *data);
> +irqreturn_t nhi_msi_thread(int irq, void *data);
> irqreturn_t ring_msix(int irq, void *data);
> int nhi_probe(struct tb_nhi *nhi);
> void nhi_shutdown(struct tb_nhi *nhi);
> diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
> index 9fe2ef43d8ac8ac867c6e37d7a77518ad2221ed9..6388f77c67ac6d51831667abbffcfb8f52881af6 100644
> --- a/drivers/thunderbolt/pci.c
> +++ b/drivers/thunderbolt/pci.c
> @@ -206,14 +206,13 @@ static int nhi_pci_init_msi(struct tb_nhi *nhi)
> if (nvec < 0)
> return nvec;
>
> - INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
> -
> irq = pci_irq_vector(pdev, 0);
> if (irq < 0)
> return irq;
>
> - res = devm_request_irq(&pdev->dev, irq, nhi_msi,
> - IRQF_NO_SUSPEND, "thunderbolt", nhi);
> + res = devm_request_threaded_irq(&pdev->dev, irq, nhi_msi,
> + nhi_msi_thread,
> + IRQF_NO_SUSPEND, "thunderbolt", nhi);
> if (res)
> return res;
> }
> @@ -311,14 +310,8 @@ static void nhi_pci_release_irq(struct tb_nhi *nhi)
> struct tb_nhi_pci *nhi_pci = nhi_to_pci(nhi);
> struct pci_dev *pdev = to_pci_dev(nhi->dev);
>
> - /*
> - * We have to release the irq before calling flush_work. Otherwise an
> - * already executing IRQ handler could call schedule_work again.
> - */
> - if (!pdev->msix_enabled) {
> + if (!pdev->msix_enabled)
> devm_free_irq(nhi->dev, pdev->irq, nhi);
> - flush_work(&nhi->interrupt_work);
> - }
> ida_destroy(&nhi_pci->msix_ida);
> }
>
> diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
> index 57502da290805ed6ba627578da060afe290d4137..4faec9246d175d08063b7881ed1d379b9a7bdafd 100644
> --- a/include/linux/thunderbolt.h
> +++ b/include/linux/thunderbolt.h
> @@ -501,7 +501,8 @@ void tb_service_properties_changed(struct tb_service *svc);
> /**
> * struct tb_nhi - thunderbolt native host interface
> * @lock: Must be held during ring creation/destruction. Is acquired by
> - * interrupt_work when dispatching interrupts to individual rings.
> + * the interrupt thread when dispatching interrupts to individual
> + * rings.
> * @dev: Device associated with this NHI instance
> * @ops: NHI specific optional ops
> * @iobase: MMIO space of the NHI
> @@ -511,8 +512,6 @@ void tb_service_properties_changed(struct tb_service *svc);
> * @going_away: The host controller device is about to disappear so when
> * this flag is set, avoid touching the hardware anymore.
> * @iommu_dma_protection: An IOMMU will isolate external-facing ports.
> - * @interrupt_work: Work scheduled to handle ring interrupt when no
> - * MSI-X is used.
> * @hop_count: Number of rings (end point hops) supported by NHI.
> * @quirks: NHI specific quirks if any
> * @domain_released: Completed when domain has been fully released
> @@ -532,7 +531,6 @@ struct tb_nhi {
> u32 *interrupt_mask;
> bool going_away;
> bool iommu_dma_protection;
> - struct work_struct interrupt_work;
> u32 hop_count;
> unsigned long quirks;
> struct completion domain_released;
> @@ -639,11 +637,13 @@ enum ring_desc_flags {
> * @flags: Flags for the frame (see &enum ring_desc_flags)
> * @eof: End of frame protocol defined field
> * @sof: Start of frame protocol defined field
> + * @ring: Ring the frame was completed on
> */
> struct ring_frame {
> dma_addr_t buffer_phy;
> ring_cb callback;
> struct list_head list;
> + struct tb_ring *ring;
> u32 size:12;
> u32 flags:12;
> u32 eof:4;
>
> --
> 2.55.0
next prev parent reply other threads:[~2026-12-01 7:33 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 20:01 [RFC DNM PATCH 00/15] Qualcomm USB4 host router Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 01/15] phy: qualcomm: qmp-combo: Add Glymur USB4v1 PHY sequences Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 02/15] phy: qualcomm: qmp-combo: Add USB4 power_on/off to de/assert USB4 clamps Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 03/15] soc: qcom: smem: Add and export qcom_smem_get_serial_no() Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 04/15] soc: qcom: pmic_glink_altmode: Enable TBT/USB4 negotiation Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 05/15] clk: qcom: gcc-glymur: Don't poll the status bit of some USB4 clocks Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 06/15] thunderbolt: nhi: Clear ring interrupts when not using MSI-X Konrad Dybcio
2026-12-01 7:16 ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 07/15] thunderbolt: nhi: Process ring completions in the threaded IRQ Konrad Dybcio
2026-12-01 7:33 ` Mika Westerberg [this message]
2026-11-24 20:01 ` [PATCH RFC DNM 08/15] thunderbolt: nhi: Add pre_switch_suspend hook Konrad Dybcio
2026-12-01 7:41 ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 09/15] thunderbolt: nhi: Resume the domain if the NHI suspend op fails Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 10/15] thunderbolt: nhi: Resume a runtime suspended NHI for system suspend Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 11/15] thunderbolt: Raise TB_TIMEOUT to 500 ms Konrad Dybcio
2026-11-24 20:01 ` [PATCH HACK RFC DNM 12/15] thunderbolt: Refuse to activate DP/USB3/PCIe tunnels Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 13/15] thunderbolt: Introduce Qualcomm USB4 router driver Konrad Dybcio
2026-11-28 13:54 ` Troels Vognbjerg
2026-11-24 20:01 ` [PATCH RFC DNM 14/15] arm64: dts: qcom: glymur: Add USB4 topology Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 15/15] arm64: dts: qcom: glymur-crd: Enable the tertiary Type-C Konrad Dybcio
2026-12-01 6:21 ` [RFC DNM PATCH 00/15] Qualcomm USB4 host router Felipe Infante de Castro
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=20261201073320.GR953667@black.igk.intel.com \
--to=mika.westerberg@linux.intel.com \
--cc=YehezkelShB@gmail.com \
--cc=abelvesa@kernel.org \
--cc=andersson@kernel.org \
--cc=andreas.noever@gmail.com \
--cc=bmasney+clk@redhat.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jbrunet+clk@baylibre.com \
--cc=konrad.dybcio@oss.qualcomm.com \
--cc=konradybcio@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-phy@lists.infradead.org \
--cc=linux-usb@vger.kernel.org \
--cc=mani@kernel.org \
--cc=mario.limonciello@amd.com \
--cc=neil.armstrong@linaro.org \
--cc=p.zabel@pengutronix.de \
--cc=robh@kernel.org \
--cc=sboyd@kernel.org \
--cc=usb4-upstream@oss.qualcomm.com \
--cc=vkoul@kernel.org \
--cc=westeri@kernel.org \
/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