From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.8 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796110415; b=N9UzetBjrVkm0z4TlIAzSZ9t9WCTExqkt3Nb5tDcMdbydrqwc0M4JGa5F/9rv+jzG/sY absdO1pEvom8PNDTsGe/1F+16aiiJkGIBdaYF8iyeqIWQBalSsU7fDBD+RNNcA7XoZBVS KlfsHT5X38vmB+Eu3l0VoTK9DixXkBEtY/55Z/BZwp+E0iXb9L3HgxRkx50g1xQQkGnkI LytVSI3MB7bAZp5EcMoMIxu1Hgb7CO5CSBhLsWC1r8kndrTXK7Ixrc21TK6N//ahX9pSn ubkqtcGRD0Onrw6Cd1dUzumCuQLUYTMHonl+s/FaFW87IcLdPAYfiUjLXNqu2h0HYhw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796110415; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Received:Date:From:To: Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=9aPxhwcQvTBI4jVp4UQSBmNwlmZQ47fPDJ/lWwS4adE=; b=Tm0WSnnSwe6dqxvQMvkmXemNvnQBKbKTwiKe2QN5aUqIsxYMBQyYnk1SrQHYK43e3zew OWrL6yEe9hjiEQ/mSbtOjcfLOxUEb5dTWEb92z4f7As3BtqZrXlYKA8kJvWA1/RuprsmE n4xjqLZhlZwIcA97G5bvEf2EJm3N+br+pXm6YCgthQpGT/TLzyvhcO67WR7jyRQo2LmkT HU0JgzuHP3TPP//9v4Me1UITHlwbEC90k04OxZ7tJ4ETOQGIEPHqQUXSvuFbCb1YrInvx AtAxLI2jg/y3iB9fiM78PzeEr4+cMVYhhnB9XfMB3vrh8Xew14W0YHvZF7HvGWa495g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=NutIfP7o; arc=none smtp.remote-ip=192.198.163.8 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A71F606A339; Tue, 1 Dec 2026 07:33:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5A71F606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=NutIfP7o DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796110409; x=1827646409; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=9aPxhwcQvTBI4jVp4UQSBmNwlmZQ47fPDJ/lWwS4adE=; b=NutIfP7o2dL81635ytCqWC/LuV8/MRRtTZ+J1LUSjWvXq9TlKt37PejO 1L+vV9DShGWaE2IaavrRFqw5j3no8iwB2uW3aNACCqkdeiWbNBR8grphG iCUkFTDkqnImj5Cd3OCq5ZEjinexCIHM//8J0kcW+1B4RoHrSgWArdtIa 4bso+7HY7NiHI8emtuPUSDxYuoyRc3qMe/Jzz0Loz9WFLJf/2l7kV7ZYS rXCoLJ81clpR0h/GTO7Pqnl31Hn6dDgNqOuncMADkXrASkL1Fp9/VCEZq rUrh47WBcCHhm0BfSxDDWa7krNbGZudXVlUjLt3uDxZ8BV27qFUyBmHjo g==; X-CSE-ConnectionGUID: iCEX9akgTp6haCgFBtjzkg== X-CSE-MsgGUID: uKKGGrTFTZe3gSm24fEP8g== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4856956" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="4856956" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 23:33:28 -0800 X-CSE-ConnectionGUID: n0SpJztVR5GTu3r4Hdnb/A== X-CSE-MsgGUID: NdlE9yeHSniYPCAhDapQ7w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13535109" Received: from black.igk.intel.com ([10.91.253.5]) by orviesa009.jf.intel.com with ESMTP; 30 Nov 2026 23:33:23 -0800 Received: by black.igk.intel.com (Postfix, from userid 1001) id E9E8698; Tue, 01 Dec 2026 08:33:20 +0100 (CET) Date: Tue, 1 Dec 2026 08:33:20 +0100 From: Mika Westerberg To: Konrad Dybcio Cc: Vinod Koul , Neil Armstrong , Manivannan Sadhasivam , Bjorn Andersson , Konrad Dybcio , Abel Vesa , Stephen Boyd , Brian Masney , Jerome Brunet , Andreas Noever , Mika Westerberg , Yehezkel Bernat , Mario Limonciello , Philipp Zabel , Rob Herring , Krzysztof Kozlowski , Conor Dooley , 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 Message-ID: <20261201073320.GR953667@black.igk.intel.com> References: <20261124-topic-next-20261116-fsm-mvp-v1-0-a9b45c69cd38@oss.qualcomm.com> <20261124-topic-next-20261116-fsm-mvp-v1-7-a9b45c69cd38@oss.qualcomm.com> X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 > --- > 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