mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chunfeng Yun <chunfeng.yun@mediatek.com>
To: Mika Westerberg <westeri@kernel.org>
Cc: Andreas Noever <andreas.noever@gmail.com>,
	Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>,
	Sven Peter <sven@kernel.org>,
	Yehezkel Bernat <YehezkelShB@gmail.com>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	<linux-kernel@vger.kernel.org>, <linux-usb@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>,
	Chunfeng Yun <chunfeng.yun@mediatek.com>
Subject: [PATCH v4 03/14] thunderbolt: Internalize non-MSI-X interrupt request and work in NHI core
Date: Sat, 5 Dec 2026 16:33:28 +0800	[thread overview]
Message-ID: <20261205083342.4009278-4-chunfeng.yun@mediatek.com> (raw)
In-Reply-To: <20261205083342.4009278-1-chunfeng.yun@mediatek.com>

Currently, the primary interrupt request and work initialization are
handled inside the backend PCIe driver (pci.c), requiring nhi_msi(),
nhi_interrupt_work(), and nhi_disable_interrupts() to be exposed in nhi.h,
and requiring struct tb_nhi_ops to provide an .init_interrupts hook.

Since the interrupt work and ISR only interact with NHI core rings and
locks, they can be made entirely private to nhi.c. Backend drivers only
need to discover and provide the IRQ number in struct tb_nhi.

Move the non-MSI-X interrupt handling into the NHI core, drop the
.init_interrupts callback from struct tb_nhi_ops, and clean up the
declarations in nhi.h.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
new patch
---
 drivers/thunderbolt/nhi.c   | 31 ++++++++++++++++-------
 drivers/thunderbolt/nhi.h   |  6 -----
 drivers/thunderbolt/pci.c   | 50 ++++++++++++++-----------------------
 include/linux/thunderbolt.h |  2 ++
 4 files changed, 43 insertions(+), 46 deletions(-)

diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index 01311ad1f693..16562a8cd4a7 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -177,7 +177,7 @@ static void ring_interrupt_active(struct tb_ring *ring, bool active)
  *
  * Use only during init and shutdown.
  */
-void nhi_disable_interrupts(struct tb_nhi *nhi)
+static void nhi_disable_interrupts(struct tb_nhi *nhi)
 {
 	int i = 0;
 	/* disable interrupts */
@@ -1022,7 +1022,7 @@ 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)
+static void nhi_interrupt_work(struct work_struct *work)
 {
 	struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
 	int value = 0; /* Suppress uninitialized usage warning. */
@@ -1072,7 +1072,7 @@ void nhi_interrupt_work(struct work_struct *work)
 	spin_unlock_irq(&nhi->lock);
 }
 
-irqreturn_t nhi_msi(int irq, void *data)
+static irqreturn_t nhi_isr(int irq, void *data)
 {
 	struct tb_nhi *nhi = data;
 	schedule_work(&nhi->interrupt_work);
@@ -1232,6 +1232,15 @@ void nhi_shutdown(struct tb_nhi *nhi)
 	}
 	nhi_disable_interrupts(nhi);
 
+	/*
+	 * We have to release the irq before calling flush_work. Otherwise an
+	 * already executing IRQ handler could call schedule_work again.
+	 */
+	if (nhi->irq > 0) {
+		devm_free_irq(nhi->dev, nhi->irq, nhi);
+		flush_work(&nhi->interrupt_work);
+	}
+
 	if (nhi->ops->shutdown)
 		nhi->ops->shutdown(nhi);
 }
@@ -1315,9 +1324,6 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 	if (!nhi->ops)
 		return dev_err_probe(dev, -EINVAL, "NHI ops not set\n");
 
-	if (!nhi->ops->init_interrupts)
-		return dev_err_probe(dev, -EINVAL, "missing required NHI ops\n");
-
 	res = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
 	if (res)
 		return dev_err_probe(dev, res, "failed to set DMA mask\n");
@@ -1337,14 +1343,21 @@ int tb_nhi_probe(struct tb_nhi *nhi)
 
 	spin_lock_init(&nhi->lock);
 
+	INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
+
 	nhi_reset(nhi);
 
 	/* In case someone left them on. */
 	nhi_disable_interrupts(nhi);
 
-	res = nhi->ops->init_interrupts(nhi);
-	if (res)
-		return dev_err_probe(dev, res, "cannot enable interrupts, aborting\n");
+	if (nhi->irq > 0) {
+		res = devm_request_irq(dev, nhi->irq, nhi_isr, IRQF_NO_SUSPEND,
+				       dev_name(dev), nhi);
+		if (res) {
+			nhi->irq = 0;
+			goto err;
+		}
+	}
 
 	if (nhi->ops->init) {
 		res = nhi->ops->init(nhi);
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 594a38b4225d..24a52f5f3fca 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -29,10 +29,6 @@ enum nhi_mailbox_cmd {
 
 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 tb_ring_msix(int irq, void *data);
 int tb_nhi_probe(struct tb_nhi *nhi);
 void nhi_shutdown(struct tb_nhi *nhi);
@@ -52,7 +48,6 @@ extern const struct dev_pm_ops tb_nhi_pm_ops;
  * @request_ring_irq: NHI specific interrupt retrieval hook
  * @release_ring_irq: NHI specific interrupt release hook
  * @is_present: Whether the device is currently present on the parent bus
- * @init_interrupts: NHI specific interrupt initialization hook
  */
 struct tb_nhi_ops {
 	int (*init)(struct tb_nhi *nhi);
@@ -67,7 +62,6 @@ struct tb_nhi_ops {
 	int (*request_ring_irq)(struct tb_ring *ring, bool no_suspend);
 	void (*release_ring_irq)(struct tb_ring *ring);
 	bool (*is_present)(struct tb_nhi *nhi);
-	int (*init_interrupts)(struct tb_nhi *nhi);
 };
 
 /*
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 9cdfadf8b1b7..11543bcb5634 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -185,11 +185,11 @@ static bool nhi_pci_add_links(struct tb_nhi *nhi)
 	return ret;
 }
 
-static int nhi_pci_init_msi(struct tb_nhi *nhi)
+static int nhi_pci_init_msi(struct tb_nhi_pci *nhi_pci)
 {
-	struct tb_nhi_pci *nhi_pci = nhi_to_pci(nhi);
+	struct tb_nhi *nhi = &nhi_pci->nhi;
 	struct pci_dev *pdev = to_pci_dev(nhi->dev);
-	int res, irq, nvec;
+	int irq, nvec;
 
 	ida_init(&nhi_pci->msix_ida);
 
@@ -206,21 +206,21 @@ 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);
-		if (res)
-			return res;
+		nhi->irq = irq;
 	}
 
 	return 0;
 }
 
+static void nhi_pci_release_msi(struct tb_nhi_pci *nhi_pci)
+{
+	ida_destroy(&nhi_pci->msix_ida);
+}
+
 static bool nhi_pci_imr_valid(struct pci_dev *pdev)
 {
 	u8 val;
@@ -306,22 +306,6 @@ static void nhi_pci_ring_release_msix(struct tb_ring *ring)
 	ring->irq = 0;
 }
 
-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) {
-		devm_free_irq(nhi->dev, pdev->irq, nhi);
-		flush_work(&nhi->interrupt_work);
-	}
-	ida_destroy(&nhi_pci->msix_ida);
-}
-
 static bool nhi_pci_is_present(struct tb_nhi *nhi)
 {
 	return pci_device_is_present(to_pci_dev(nhi->dev));
@@ -333,9 +317,7 @@ static const struct tb_nhi_ops pci_nhi_default_ops = {
 	.post_nvm_auth = nhi_pci_complete_dma_port,
 	.request_ring_irq = nhi_pci_ring_request_msix,
 	.release_ring_irq = nhi_pci_ring_release_msix,
-	.shutdown = nhi_pci_release_irq,
 	.is_present = nhi_pci_is_present,
-	.init_interrupts = nhi_pci_init_msi,
 };
 
 /* Ice Lake specific NHI operations */
@@ -535,8 +517,6 @@ static bool icl_nhi_add_links(struct tb_nhi *nhi)
 
 static void icl_nhi_shutdown(struct tb_nhi *nhi)
 {
-	nhi_pci_release_irq(nhi);
-
 	icl_nhi_force_power(nhi, false);
 }
 
@@ -553,7 +533,6 @@ static const struct tb_nhi_ops icl_nhi_ops = {
 	.request_ring_irq = nhi_pci_ring_request_msix,
 	.release_ring_irq = nhi_pci_ring_release_msix,
 	.is_present = nhi_pci_is_present,
-	.init_interrupts = nhi_pci_init_msi,
 };
 
 static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
@@ -588,7 +567,15 @@ static int nhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	pci_set_master(pdev);
 
-	return tb_nhi_probe(&nhi_pci->nhi);
+	res = nhi_pci_init_msi(nhi_pci);
+	if (res)
+		return dev_err_probe(dev, res, "cannot initialize msi, aborting\n");
+
+	res = tb_nhi_probe(nhi);
+	if (res)
+		nhi_pci_release_msi(nhi_pci);
+
+	return res;
 }
 
 static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
@@ -611,6 +598,7 @@ static void nhi_pci_do_remove(struct pci_dev *pdev, bool reset)
 	tb_domain_remove(tb);
 	wait_for_completion(&nhi->domain_released);
 	nhi_shutdown(nhi);
+	nhi_pci_release_msi(nhi_to_pci(nhi));
 }
 
 static void nhi_pci_remove(struct pci_dev *pdev)
diff --git a/include/linux/thunderbolt.h b/include/linux/thunderbolt.h
index 57502da29080..412fcc6adcf7 100644
--- a/include/linux/thunderbolt.h
+++ b/include/linux/thunderbolt.h
@@ -513,6 +513,7 @@ void tb_service_properties_changed(struct tb_service *svc);
  * @iommu_dma_protection: An IOMMU will isolate external-facing ports.
  * @interrupt_work: Work scheduled to handle ring interrupt when no
  *		    MSI-X is used.
+ * @irq: Interrupt number of NHI when MSI-X is not 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
@@ -533,6 +534,7 @@ struct tb_nhi {
 	bool going_away;
 	bool iommu_dma_protection;
 	struct work_struct interrupt_work;
+	int irq;
 	u32 hop_count;
 	unsigned long quirks;
 	struct completion domain_released;
-- 
2.45.2


  parent reply	other threads:[~2026-12-05  8:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  8:33 [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 01/14] thunderbolt: Fix error handling and unwind paths in nhi_probe() Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 02/14] thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_ Chunfeng Yun
2026-12-05  8:33 ` Chunfeng Yun [this message]
2026-12-05  8:33 ` [PATCH v4 04/14] thunderbolt: Encapsulate router device type checks in switch.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 05/14] thunderbolt: Move rpm_complete bus walk to domain.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 06/14] thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove() Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 08/14] thunderbolt: Decouple ICM probe from NHI core via .select_cm hook Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 10/14] thunderbolt: Make nhi->ops optional Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 11/14] thunderbolt: Add platform NHI driver Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 12/14] thunderbolt: Increase the lane adapter link-up timeout value Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 13/14] thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and TB_PORT_UP Chunfeng Yun
2026-12-05  8:33 ` [PATCH v4 14/14] thunderbolt: Drop WARN_ON() in tb_disconnect_pci() Chunfeng Yun

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=20261205083342.4009278-4-chunfeng.yun@mediatek.com \
    --to=chunfeng.yun@mediatek.com \
    --cc=YehezkelShB@gmail.com \
    --cc=andreas.noever@gmail.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=konrad.dybcio@oss.qualcomm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=matthias.bgg@gmail.com \
    --cc=sven@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

all inboxes | Powered by JetHome®