mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Koichiro Den <den@valinux.co.jp>
To: ntb@lists.linux.dev, linux-pci@vger.kernel.org,
	dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: mani@kernel.org, kwilczynski@kernel.org, kishon@kernel.org,
	bhelgaas@google.com, corbet@lwn.net, vkoul@kernel.org,
	jdmason@kudzu.us, dave.jiang@intel.com, allenbh@gmail.com,
	Basavaraj.Natikar@amd.com, Shyam-sundar.S-k@amd.com,
	kurt.schwemmer@microsemi.com, logang@deltatee.com,
	jingoohan1@gmail.com, lpieralisi@kernel.org, robh@kernel.org,
	jbrunet@baylibre.com, Frank.Li@nxp.com, fancer.lancer@gmail.com,
	arnd@arndb.de, pstanner@redhat.com,
	elfring@users.sourceforge.net
Subject: [RFC PATCH 23/25] NTB: intr_dw_edma: Add DW eDMA emulated interrupt backend
Date: Thu, 23 Oct 2025 16:19:14 +0900	[thread overview]
Message-ID: <20251023071916.901355-24-den@valinux.co.jp> (raw)
In-Reply-To: <20251023071916.901355-1-den@valinux.co.jp>

Introduce a new NTB interrupt backend using DW eDMA's emulated interrupt
mechanism. Enables interrupt-based signaling from RC to EP where MSI is
impossible due to security restrictions on the platform.

Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
 drivers/ntb/Kconfig        |  10 ++
 drivers/ntb/Makefile       |   1 +
 drivers/ntb/intr_common.c  |   8 +-
 drivers/ntb/intr_dw_edma.c | 253 +++++++++++++++++++++++++++++++++++++
 include/linux/ntb.h        |  10 ++
 5 files changed, 281 insertions(+), 1 deletion(-)
 create mode 100644 drivers/ntb/intr_dw_edma.c

diff --git a/drivers/ntb/Kconfig b/drivers/ntb/Kconfig
index 2f22f44245b3..5b7e1563e639 100644
--- a/drivers/ntb/Kconfig
+++ b/drivers/ntb/Kconfig
@@ -29,6 +29,16 @@ config NTB_MSI
 
 	 If unsure, say N.
 
+config NTB_DW_EDMA
+	bool "DW eDMA test-interrupt backend"
+	depends on PCI_ENDPOINT && PCIE_DW_EP && DW_EDMA
+	select NTB_INTR_COMMON
+	help
+	 Use DW eDMA v0 test interrupt as a doorbell-like backend
+	 for NTB transports when MSI is not available on EPF side.
+
+	 If unsure, say N.
+
 source "drivers/ntb/hw/Kconfig"
 
 source "drivers/ntb/test/Kconfig"
diff --git a/drivers/ntb/Makefile b/drivers/ntb/Makefile
index feaa2a77cbf6..cae84d132b78 100644
--- a/drivers/ntb/Makefile
+++ b/drivers/ntb/Makefile
@@ -5,3 +5,4 @@ obj-$(CONFIG_NTB_TRANSPORT) += ntb_transport.o
 ntb-y				:= core.o
 ntb-$(CONFIG_NTB_INTR_COMMON)	+= intr_common.o
 ntb-$(CONFIG_NTB_MSI)		+= msi.o
+ntb-$(CONFIG_NTB_DW_EDMA)	+= intr_dw_edma.o
diff --git a/drivers/ntb/intr_common.c b/drivers/ntb/intr_common.c
index e0e296fd3e3c..41b2752c6d03 100644
--- a/drivers/ntb/intr_common.c
+++ b/drivers/ntb/intr_common.c
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
 
-#include <linux/ntb.h>
 #include <linux/module.h>
+#include <linux/ntb.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 
@@ -13,6 +13,12 @@ int ntb_intr_init(struct ntb_dev *ntb,
 		ntb->intr_backend = ntb_intr_msi_backend();
 		dev_info(&ntb->dev, "NTB interrupt MSI backend selected.\n");
 	}
+#endif
+#ifdef CONFIG_NTB_DW_EDMA
+	if (!ntb->intr_backend) {
+		ntb->intr_backend = ntb_intr_dw_edma_backend();
+		dev_info(&ntb->dev, "NTB interrupt DW eDMA backend selected.\n");
+	}
 #endif
 	if (!ntb->intr_backend)
 		return -ENODEV;
diff --git a/drivers/ntb/intr_dw_edma.c b/drivers/ntb/intr_dw_edma.c
new file mode 100644
index 000000000000..0e408ecfaf61
--- /dev/null
+++ b/drivers/ntb/intr_dw_edma.c
@@ -0,0 +1,253 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+
+#include <linux/dma/edma.h>
+#include <linux/ntb.h>
+#include <linux/pci-epc.h>
+
+struct ntb_intr_dw {
+	u64 base_addr;
+	u64 end_addr;
+
+	struct dw_edma *edma;
+	resource_size_t rd_status_off;
+	resource_size_t rd_clear_off;
+
+	u32 __iomem *peer_mws[];
+};
+
+struct ntb_intr_dw_ctx {
+	irq_handler_t handler;
+	void *dev;
+	struct dw_edma *edma;
+};
+
+static void dw_edma_selfirq_handler(struct dw_edma *dw, void *data)
+{
+	struct ntb_intr_dw_ctx *ctx = data;
+
+	ctx->handler(0, ctx->dev);
+}
+
+static int dw_edma_find_backend_for_ntb(struct ntb_dev *ntb, struct ntb_intr_dw *intr_dw)
+{
+	struct pci_epc *epc = NULL;
+
+	epc = ntb_get_pci_epc(ntb);
+	if (!epc)
+		return -ENODEV;
+	intr_dw->edma = dw_edma_find_by_child(&epc->dev);
+	if (!intr_dw->edma)
+		return -ENODEV;
+	dw_edma_selfirq_offsets(intr_dw->edma, &intr_dw->rd_status_off, &intr_dw->rd_clear_off);
+	return 0;
+}
+
+static int dw_intr_init(struct ntb_dev *ntb, void (*desc_changed)(void *ctx))
+{
+	struct ntb_intr_dw *intr_dw;
+	phys_addr_t mw_phys_addr;
+	resource_size_t mw_size;
+	int peer_widx;
+	int peers;
+	int ret;
+	int i;
+
+	peers = ntb_peer_port_count(ntb);
+	if (peers <= 0)
+		return -EINVAL;
+
+	intr_dw = devm_kzalloc(&ntb->dev, struct_size(intr_dw, peer_mws, peers),
+			       GFP_KERNEL);
+	if (!intr_dw)
+		return -ENOMEM;
+
+	ret = dw_edma_find_backend_for_ntb(ntb, intr_dw);
+	if (ret) {
+		devm_kfree(&ntb->dev, intr_dw);
+		return ret;
+	}
+
+	for (i = 0; i < peers; i++) {
+		peer_widx = ntb_peer_mw_count(ntb) - 1 - i;
+
+		ret = ntb_peer_mw_get_addr(ntb, peer_widx, &mw_phys_addr,
+					   &mw_size);
+		if (ret)
+			goto unroll;
+
+		intr_dw->peer_mws[i] = devm_ioremap(&ntb->dev, mw_phys_addr,
+						    mw_size);
+		if (!intr_dw->peer_mws[i]) {
+			ret = -EFAULT;
+			goto unroll;
+		}
+	}
+
+	ntb->intr_priv = intr_dw;
+
+	return 0;
+
+unroll:
+	for (i = 0; i < peers; i++)
+		if (intr_dw->peer_mws[i])
+			devm_iounmap(&ntb->dev, intr_dw->peer_mws[i]);
+
+	devm_kfree(&ntb->dev, intr_dw);
+	return ret;
+}
+
+static int dw_intr_setup_mws(struct ntb_dev *ntb)
+{
+	struct ntb_intr_dw *dwc = ntb->intr_priv;
+	resource_size_t addr_align, size_align, offset;
+	resource_size_t mw_size = SZ_32K;
+	resource_size_t mw_min_size = mw_size;
+	u64 addr = dwc->rd_status_off;
+	int peer, peer_widx, ret;
+	int i;
+
+	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
+		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
+		if (peer_widx < 0)
+			return peer_widx;
+
+		ret = ntb_mw_get_align(ntb, peer, peer_widx, &addr_align,
+				       NULL, NULL, NULL);
+		if (ret)
+			return ret;
+
+		addr &= ~(addr_align - 1);
+	}
+
+	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
+		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
+		if (peer_widx < 0) {
+			ret = peer_widx;
+			goto error_out;
+		}
+
+		ret = ntb_mw_get_align(ntb, peer, peer_widx, NULL,
+				       &size_align, NULL, &offset);
+		if (ret)
+			goto error_out;
+
+		mw_size = round_up(mw_size, size_align);
+		if (mw_size < mw_min_size)
+			mw_min_size = mw_size;
+
+		ret = ntb_mw_set_trans(ntb, peer, peer_widx,
+				       addr, mw_size, offset);
+		if (ret)
+			goto error_out;
+	}
+
+	dwc->base_addr = addr;
+	dwc->end_addr = addr + mw_min_size;
+
+	return 0;
+
+error_out:
+	for (i = 0; i < peer; i++) {
+		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
+		if (peer_widx < 0)
+			continue;
+
+		ntb_mw_clear_trans(ntb, i, peer_widx);
+	}
+
+	return ret;
+}
+
+static void dw_intr_clear_mws(struct ntb_dev *ntb)
+{
+	int peer, peer_widx;
+
+	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
+		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
+		if (peer_widx < 0)
+			continue;
+
+		ntb_mw_clear_trans(ntb, peer, peer_widx);
+	}
+}
+
+static void dw_intr_release_irq(void *data)
+{
+	struct ntb_intr_dw_ctx *ctx = data;
+
+	dw_edma_unregister_selfirq(ctx->edma, dw_edma_selfirq_handler, ctx);
+	kfree(ctx);
+}
+
+static int dw_intr_request_irq(struct ntb_dev *ntb, irq_handler_t h,
+			       const char *name, void *dev_id,
+			       struct ntb_intr_desc *intr_desc)
+{
+	struct ntb_intr_dw *dwc = ntb->intr_priv;
+	struct dw_edma *edma = dwc->edma;
+	int ret;
+
+	if (intr_desc->ctx)
+		return 1;
+
+	struct ntb_intr_dw_ctx *ctx __free(kfree) = kzalloc(
+						sizeof(*ctx), GFP_KERNEL);
+	if (!ctx)
+		return -ENOMEM;
+	ctx->handler = h;
+	ctx->dev = dev_id;
+	ctx->edma = edma;
+
+	ret = dw_edma_register_selfirq(edma, dw_edma_selfirq_handler, ctx);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(&ntb->dev, dw_intr_release_irq, ctx);
+	if (ret)
+		return ret;
+
+	intr_desc->addr_offset = dwc->rd_status_off - dwc->base_addr;
+	intr_desc->data = 0x0;
+	intr_desc->ctx = no_free_ptr(ctx);
+	return 1;
+}
+
+static void dw_intr_free_irq(struct ntb_dev *ntb, int irq, void *dev_id,
+			     struct ntb_intr_desc *intr_desc)
+{
+	struct ntb_intr_dw *dwc = ntb->intr_priv;
+	struct dw_edma *edma = dwc->edma;
+	struct ntb_intr_dw_ctx *ctx;
+
+	ctx = intr_desc->ctx;
+	dw_edma_unregister_selfirq(edma, dw_edma_selfirq_handler, ctx);
+	devm_remove_action(&ntb->dev, dw_intr_release_irq, ctx);
+	kfree(ctx);
+}
+
+static int dw_intr_peer_trigger(struct ntb_dev *ntb, int peer, struct ntb_intr_desc *desc)
+{
+	struct ntb_intr_dw *intr_dw = ntb->intr_priv;
+	int idx;
+
+	idx = desc->addr_offset / sizeof(*intr_dw->peer_mws[peer]);
+
+	iowrite32(desc->data, &intr_dw->peer_mws[peer][idx]);
+
+	return 0;
+}
+
+static const struct ntb_intr_backend ntb_intr_backend_dw_edma = {
+	.name = "dw-edma-testirq",
+	.init = dw_intr_init,
+	.setup_mws = dw_intr_setup_mws,
+	.clear_mws = dw_intr_clear_mws,
+	.request_irq = dw_intr_request_irq,
+	.free_irq = dw_intr_free_irq,
+	.peer_trigger = dw_intr_peer_trigger,
+};
+
+const struct ntb_intr_backend *ntb_intr_dw_edma_backend(void)
+{
+	return &ntb_intr_backend_dw_edma;
+}
diff --git a/include/linux/ntb.h b/include/linux/ntb.h
index 1a88fe45471e..7daba67928e9 100644
--- a/include/linux/ntb.h
+++ b/include/linux/ntb.h
@@ -1664,6 +1664,7 @@ struct ntb_intr_desc {
 	u32 addr_offset;
 	u32 data;
 	u16 vector_offset;
+	void *ctx;
 };
 
 struct ntb_intr_backend {
@@ -1734,4 +1735,13 @@ static inline const struct ntb_intr_backend *ntb_intr_msi_backend(void)
 }
 #endif /* CONFIG_NTB_MSI */
 
+#ifdef CONFIG_NTB_DW_EDMA
+extern const struct ntb_intr_backend *ntb_intr_dw_edma_backend(void);
+#else
+static inline const struct ntb_intr_backend *ntb_intr_dw_edma_backend(void)
+{
+	return NULL;
+}
+#endif /* CONFIG_NTB_DW_EDMA */
+
 #endif
-- 
2.48.1


  parent reply	other threads:[~2025-10-23  7:20 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-23  7:18 [RFC PATCH 00/25] NTB/PCI: Add DW eDMA intr fallback and BAR MW offsets Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 01/25] PCI: endpoint: pci-epf-vntb: Use array_index_nospec() on mws_size[] access Koichiro Den
2025-10-24  0:06   ` Frank Li
2025-10-24 16:24     ` Koichiro Den
2025-10-24 18:40       ` Frank Li
2025-10-23  7:18 ` [RFC PATCH 02/25] PCI: endpoint: pci-epf-vntb: Add mwN_offset configfs attributes Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 03/25] NTB: epf: Handle mwN_offset for inbound MW regions Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 04/25] PCI: endpoint: Add inbound mapping ops to EPC core Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 05/25] PCI: dwc: ep: Implement EPC inbound mapping support Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 06/25] PCI: endpoint: pci-epf-vntb: Use pci_epc_map_inbound() for MW mapping Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 07/25] NTB: Add offset parameter to MW translation APIs Koichiro Den
2025-10-23  7:18 ` [RFC PATCH 08/25] PCI: endpoint: pci-epf-vntb: Propagate MW offset from configfs when present Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 09/25] NTB: ntb_transport: Support offsetted partial memory windows Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 10/25] NTB/msi: Support offsetted partial memory window for MSI Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 11/25] NTB/msi: Do not force MW to its maximum possible size Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 12/25] NTB: ntb_transport: Stricter checks for peer-reported interrupt values Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 13/25] NTB/msi: Skip mw_set_trans() if already configured Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 14/25] NTB/msi: Add a inner loop for PCI-MSI cases Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 15/25] dmaengine: dw-edma: Add self-interrupt registration API Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 16/25] dmaengine: dw-edma: Expose self-IRQ register offsets Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 17/25] dmaengine: dw-edma: Add dw_edma_find_by_child() helper Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 18/25] NTB: core: Add .get_pci_epc() to ntb_dev_ops Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 19/25] NTB: epf: vntb: Implement .get_pci_epc() callback Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 20/25] NTB: ntb_transport: Rename use_msi to use_intr (keep alias) Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 21/25] NTB: Introduce generic interrupt backend abstraction and convert MSI Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 22/25] NTB: ntb_transport: Rename MSI symbols to generic interrupt form Koichiro Den
2025-10-23  7:19 ` Koichiro Den [this message]
2025-10-23  7:19 ` [RFC PATCH 24/25] NTB: epf: Add MW2 for interrupt use on Renesas R-Car Koichiro Den
2025-10-23  7:19 ` [RFC PATCH 25/25] Documentation: PCI: endpoint: pci-epf-vntb: Update and add mwN_offset usage Koichiro Den
2025-10-23  7:55 ` [RFC PATCH 00/25] NTB/PCI: Add DW eDMA intr fallback and BAR MW offsets Jerome Brunet
2025-10-24 16:11   ` Koichiro Den
2025-10-24  3:27 ` Frank Li
2025-10-24 16:04   ` Koichiro Den
2025-10-24 16:43     ` Frank Li
2025-10-27  5:29       ` Koichiro Den
2025-10-28 20:50         ` Frank Li
2025-10-29  7:13           ` Koichiro Den

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=20251023071916.901355-24-den@valinux.co.jp \
    --to=den@valinux.co.jp \
    --cc=Basavaraj.Natikar@amd.com \
    --cc=Frank.Li@nxp.com \
    --cc=Shyam-sundar.S-k@amd.com \
    --cc=allenbh@gmail.com \
    --cc=arnd@arndb.de \
    --cc=bhelgaas@google.com \
    --cc=corbet@lwn.net \
    --cc=dave.jiang@intel.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=elfring@users.sourceforge.net \
    --cc=fancer.lancer@gmail.com \
    --cc=jbrunet@baylibre.com \
    --cc=jdmason@kudzu.us \
    --cc=jingoohan1@gmail.com \
    --cc=kishon@kernel.org \
    --cc=kurt.schwemmer@microsemi.com \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=logang@deltatee.com \
    --cc=lpieralisi@kernel.org \
    --cc=mani@kernel.org \
    --cc=ntb@lists.linux.dev \
    --cc=pstanner@redhat.com \
    --cc=robh@kernel.org \
    --cc=vkoul@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®