[PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver
From: Manivannan Sadhasivam via B4 Relay
Date: Fri Dec 04 2026 - 09:01:52 EST
From: Manivannan Sadhasivam <manivannan.sadhasivam@xxxxxxxxxxxxxxxx>
Add a platform driver for a generic firmware-initialised PCI Endpoint
Controller that speaks a simple MMIO register interface. The driver
programs the Endpoint configuration (BAR, header, outbound mapping, link
start/stop) through the MMIO register interface. A separate command
register at offset 0xf0 is used to issue command writes to the Endpoint
Controller for each operation.
This driver supports multiple physical functions, but doesn't support
SR-IOV yet. If a controller implementation supports more than one function,
it is expected to populate max supported functions in PCI_EPC_GEN_MAX_FUNC
register and expose the per-function MMIO registers in the 'ctrl' region
with a stride of PCI_EPC_GEN_REG_STRIDE (0x1000).
Capabilities such as MSI/MSI-X vector counts negotiated by the RC and the
MSI target address/data are firmware-driven and exposed through read-only
status registers.
This driver targets both firmware-initialised Endpoint controllers and the
emulated QEMU pcie-ep-ctrl device.
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@xxxxxxxxxxxxxxxx>
---
drivers/pci/controller/Kconfig | 12 +
drivers/pci/controller/Makefile | 1 +
drivers/pci/controller/pci-ep-generic.c | 387 ++++++++++++++++++++++++++++++++
3 files changed, 400 insertions(+)
diff --git a/drivers/pci/controller/Kconfig b/drivers/pci/controller/Kconfig
index d246bbe37948..77e9fd55f967 100644
--- a/drivers/pci/controller/Kconfig
+++ b/drivers/pci/controller/Kconfig
@@ -153,6 +153,18 @@ config PCI_HOST_GENERIC
Say Y here if you want to support a simple generic PCI host
controller, such as the one emulated by kvmtool.
+config PCI_EP_GENERIC
+ tristate "Generic PCI Endpoint Controller"
+ depends on OF
+ depends on PCI_ENDPOINT
+ help
+ Say Y here to enable support for the generic firmware-initialised
+ PCI Endpoint Controller driver. Targets virtual machines (e.g. QEMU)
+ and any firmware-initialised EPC that exposes the standard
+ MMIO register interface and Outbound Memory.
+
+ If unsure, say N.
+
config PCIE_HISI_ERR
depends on ACPI_APEI_GHES && (ARM64 || COMPILE_TEST)
bool "HiSilicon HIP PCIe controller error handling driver"
diff --git a/drivers/pci/controller/Makefile b/drivers/pci/controller/Makefile
index d478743b5142..19ca5f84423a 100644
--- a/drivers/pci/controller/Makefile
+++ b/drivers/pci/controller/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_PCIE_RCAR_EP) += pcie-rcar.o pcie-rcar-ep.o
obj-$(CONFIG_PCIE_RENESAS_RZG3S_HOST) += pcie-rzg3s-host.o
obj-$(CONFIG_PCI_HOST_COMMON) += pci-host-common.o
obj-$(CONFIG_PCI_HOST_GENERIC) += pci-host-generic.o
+obj-$(CONFIG_PCI_EP_GENERIC) += pci-ep-generic.o
obj-$(CONFIG_PCI_HOST_THUNDER_ECAM) += pci-thunder-ecam.o
obj-$(CONFIG_PCI_HOST_THUNDER_PEM) += pci-thunder-pem.o
obj-$(CONFIG_PCIE_XILINX) += pcie-xilinx.o
diff --git a/drivers/pci/controller/pci-ep-generic.c b/drivers/pci/controller/pci-ep-generic.c
new file mode 100644
index 000000000000..8ebed7cb8370
--- /dev/null
+++ b/drivers/pci/controller/pci-ep-generic.c
@@ -0,0 +1,387 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Generic PCI Endpoint Controller driver for firmware-initialised controllers.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * Author: Manivannan Sadhasivam <manivannan.sadhasivam@xxxxxxxxxxxxxxxx>
+ */
+
+#include <linux/dma-map-ops.h>
+#include <linux/iopoll.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/pci-epc.h>
+#include <linux/pci-epf.h>
+#include <linux/platform_device.h>
+
+/*
+ * Register map, relative to the "ctrl" MMIO region.
+ */
+
+/* Read-only capability registers */
+#define PCI_EPC_GEN_CAPS 0x00
+#define PCI_EPC_GEN_CAP_INTX BIT(0)
+#define PCI_EPC_GEN_CAP_MSI BIT(1)
+#define PCI_EPC_GEN_CAP_MSIX BIT(2)
+#define PCI_EPC_GEN_CAP_LINKUP_NOTIFY BIT(3)
+#define PCI_EPC_GEN_CAP_DYN_INBOUND_MAP BIT(4)
+#define PCI_EPC_GEN_CAP_SUBRANGE BIT(5)
+#define PCI_EPC_GEN_MAX_FUNC 0x04
+#define PCI_EPC_GEN_LINKUP 0x08
+
+#define PCI_EPC_GEN_BAR_NO 0x10
+#define PCI_EPC_GEN_IRQ_TYPE 0x14
+#define PCI_EPC_GEN_IRQ_NUM 0x18
+
+/* PCI configuration space header fields */
+#define PCI_EPC_GEN_HDR_VID 0x30
+#define PCI_EPC_GEN_HDR_DID 0x34
+#define PCI_EPC_GEN_HDR_SVID 0x38
+#define PCI_EPC_GEN_HDR_SSID 0x3c
+#define PCI_EPC_GEN_HDR_CLASS 0x40
+#define PCI_EPC_GEN_HDR_REV 0x44
+#define PCI_EPC_GEN_HDR_INTPIN 0x48
+
+/* BAR configuration */
+#define PCI_EPC_GEN_BAR_PHYS_LO 0x50
+#define PCI_EPC_GEN_BAR_PHYS_HI 0x54
+#define PCI_EPC_GEN_BAR_SIZE_LO 0x58
+#define PCI_EPC_GEN_BAR_SIZE_HI 0x5c
+#define PCI_EPC_GEN_BAR_FLAGS 0x60
+
+/* Outbound address mapping */
+#define PCI_EPC_GEN_MAP_PHYS_LO 0x70
+#define PCI_EPC_GEN_MAP_PHYS_HI 0x74
+#define PCI_EPC_GEN_MAP_PCI_LO 0x78
+#define PCI_EPC_GEN_MAP_PCI_HI 0x7c
+#define PCI_EPC_GEN_MAP_SIZE_LO 0x80
+#define PCI_EPC_GEN_MAP_SIZE_HI 0x84
+
+/* MSI/MSI-X configuration. IRQ_COUNT carries the MSI or the MSI-X count. */
+#define PCI_EPC_GEN_IRQ_COUNT 0x90
+#define PCI_EPC_GEN_MSIX_BAR 0x94
+#define PCI_EPC_GEN_MSIX_OFFSET 0x98
+
+/* RC-negotiated MSI/MSI-X state, read-only */
+#define PCI_EPC_GEN_MSI 0xa0
+#define PCI_EPC_GEN_MSIX 0xa4
+#define PCI_EPC_GEN_MSI_ADDR_LO 0xb0
+#define PCI_EPC_GEN_MSI_ADDR_HI 0xb4
+#define PCI_EPC_GEN_MSI_DATA 0xb8
+
+#define PCI_EPC_GEN_CMD 0xf0
+#define PCI_EPC_GEN_CMD_WRITE_HEADER 0x01
+#define PCI_EPC_GEN_CMD_SET_BAR 0x02
+#define PCI_EPC_GEN_CMD_CLEAR_BAR 0x03
+#define PCI_EPC_GEN_CMD_MAP 0x04
+#define PCI_EPC_GEN_CMD_UNMAP 0x05
+#define PCI_EPC_GEN_CMD_SET_MSI 0x06
+#define PCI_EPC_GEN_CMD_SET_MSIX 0x07
+#define PCI_EPC_GEN_CMD_RAISE_IRQ 0x08
+#define PCI_EPC_GEN_CMD_START 0x09
+#define PCI_EPC_GEN_CMD_STOP 0x0a
+
+#define PCI_EPC_GEN_REG_STRIDE 0x1000
+
+struct pci_ep_generic {
+ void __iomem *base;
+ struct pci_epc_features features;
+};
+
+static void pci_ep_generic_write(struct pci_ep_generic *epc_priv, u8 fn, u32 val, u32 reg)
+{
+ writel_relaxed(val, epc_priv->base + (fn * PCI_EPC_GEN_REG_STRIDE) + reg);
+}
+
+static u32 pci_ep_generic_read(struct pci_ep_generic *epc_priv, u8 fn, u32 reg)
+{
+ return readl_relaxed(epc_priv->base + (fn * PCI_EPC_GEN_REG_STRIDE) + reg);
+}
+
+static void pci_ep_generic_write_cmd(struct pci_ep_generic *epc_priv, u8 fn, u32 cmd)
+{
+ writel(cmd, epc_priv->base + (fn * PCI_EPC_GEN_REG_STRIDE) + PCI_EPC_GEN_CMD);
+}
+
+static int pci_ep_generic_write_header(struct pci_epc *epc, u8 fn, u8 vfn,
+ struct pci_epf_header *hdr)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, hdr->vendorid, PCI_EPC_GEN_HDR_VID);
+ pci_ep_generic_write(epc_priv, fn, hdr->deviceid, PCI_EPC_GEN_HDR_DID);
+ pci_ep_generic_write(epc_priv, fn, hdr->subsys_vendor_id, PCI_EPC_GEN_HDR_SVID);
+ pci_ep_generic_write(epc_priv, fn, hdr->subsys_id, PCI_EPC_GEN_HDR_SSID);
+ pci_ep_generic_write(epc_priv, fn, (hdr->baseclass_code << 16) |
+ (hdr->subclass_code << 8) | hdr->progif_code,
+ PCI_EPC_GEN_HDR_CLASS);
+ pci_ep_generic_write(epc_priv, fn, hdr->revid, PCI_EPC_GEN_HDR_REV);
+ pci_ep_generic_write(epc_priv, fn, hdr->interrupt_pin, PCI_EPC_GEN_HDR_INTPIN);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_WRITE_HEADER);
+
+ return 0;
+}
+
+static int pci_ep_generic_set_bar(struct pci_epc *epc, u8 fn, u8 vfn,
+ struct pci_epf_bar *epf_bar)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, epf_bar->barno, PCI_EPC_GEN_BAR_NO);
+ pci_ep_generic_write(epc_priv, fn, lower_32_bits(epf_bar->phys_addr),
+ PCI_EPC_GEN_BAR_PHYS_LO);
+ pci_ep_generic_write(epc_priv, fn, upper_32_bits(epf_bar->phys_addr),
+ PCI_EPC_GEN_BAR_PHYS_HI);
+ pci_ep_generic_write(epc_priv, fn, lower_32_bits(epf_bar->size),
+ PCI_EPC_GEN_BAR_SIZE_LO);
+ pci_ep_generic_write(epc_priv, fn, upper_32_bits(epf_bar->size),
+ PCI_EPC_GEN_BAR_SIZE_HI);
+ pci_ep_generic_write(epc_priv, fn, epf_bar->flags, PCI_EPC_GEN_BAR_FLAGS);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_SET_BAR);
+
+ return 0;
+}
+
+static void pci_ep_generic_clear_bar(struct pci_epc *epc, u8 fn, u8 vfn,
+ struct pci_epf_bar *epf_bar)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, epf_bar->barno, PCI_EPC_GEN_BAR_NO);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_CLEAR_BAR);
+}
+
+static int pci_ep_generic_map_addr(struct pci_epc *epc, u8 fn, u8 vfn,
+ phys_addr_t phys_addr, u64 pci_addr, size_t size)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, lower_32_bits(phys_addr), PCI_EPC_GEN_MAP_PHYS_LO);
+ pci_ep_generic_write(epc_priv, fn, upper_32_bits(phys_addr), PCI_EPC_GEN_MAP_PHYS_HI);
+ pci_ep_generic_write(epc_priv, fn, lower_32_bits(pci_addr), PCI_EPC_GEN_MAP_PCI_LO);
+ pci_ep_generic_write(epc_priv, fn, upper_32_bits(pci_addr), PCI_EPC_GEN_MAP_PCI_HI);
+ pci_ep_generic_write(epc_priv, fn, lower_32_bits((u64)size), PCI_EPC_GEN_MAP_SIZE_LO);
+ pci_ep_generic_write(epc_priv, fn, upper_32_bits((u64)size), PCI_EPC_GEN_MAP_SIZE_HI);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_MAP);
+
+ return 0;
+}
+
+static void pci_ep_generic_unmap_addr(struct pci_epc *epc, u8 fn, u8 vfn,
+ phys_addr_t phys_addr)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, lower_32_bits(phys_addr), PCI_EPC_GEN_MAP_PHYS_LO);
+ pci_ep_generic_write(epc_priv, fn, upper_32_bits(phys_addr), PCI_EPC_GEN_MAP_PHYS_HI);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_UNMAP);
+}
+
+static int pci_ep_generic_set_msi(struct pci_epc *epc, u8 fn, u8 vfn, u8 nr_irqs)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, nr_irqs, PCI_EPC_GEN_IRQ_COUNT);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_SET_MSI);
+
+ return 0;
+}
+
+static int pci_ep_generic_get_msi(struct pci_epc *epc, u8 fn, u8 vfn)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ return pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI);
+}
+
+static int pci_ep_generic_set_msix(struct pci_epc *epc, u8 fn, u8 vfn,
+ u16 nr_irqs, enum pci_barno bar, u32 offset)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, nr_irqs, PCI_EPC_GEN_IRQ_COUNT);
+ pci_ep_generic_write(epc_priv, fn, bar, PCI_EPC_GEN_MSIX_BAR);
+ pci_ep_generic_write(epc_priv, fn, offset, PCI_EPC_GEN_MSIX_OFFSET);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_SET_MSIX);
+
+ return 0;
+}
+
+static int pci_ep_generic_get_msix(struct pci_epc *epc, u8 fn, u8 vfn)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ return pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSIX);
+}
+
+static int pci_ep_generic_raise_irq(struct pci_epc *epc, u8 fn, u8 vfn,
+ unsigned int type, u16 irq_num)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ pci_ep_generic_write(epc_priv, fn, type, PCI_EPC_GEN_IRQ_TYPE);
+ pci_ep_generic_write(epc_priv, fn, irq_num, PCI_EPC_GEN_IRQ_NUM);
+ pci_ep_generic_write_cmd(epc_priv, fn, PCI_EPC_GEN_CMD_RAISE_IRQ);
+
+ return 0;
+}
+
+static int pci_ep_generic_map_msi_irq(struct pci_epc *epc, u8 fn, u8 vfn,
+ phys_addr_t phys_addr, u8 irq_num,
+ u32 entry_size, u32 *msi_data,
+ u32 *msi_addr_offset)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+ u64 msi_addr, aligned;
+
+ msi_addr = ((u64)pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_ADDR_HI) << 32 |
+ pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_ADDR_LO));
+
+ aligned = ALIGN_DOWN(msi_addr, entry_size);
+ *msi_addr_offset = (msi_addr - aligned) + (u32)irq_num * entry_size;
+ *msi_data = pci_ep_generic_read(epc_priv, fn, PCI_EPC_GEN_MSI_DATA) + irq_num;
+
+ return pci_ep_generic_map_addr(epc, fn, vfn, phys_addr, aligned, entry_size);
+}
+
+static int pci_ep_generic_start(struct pci_epc *epc)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+ u32 linkup;
+ int ret;
+
+ writel_relaxed(PCI_EPC_GEN_CMD_START, epc_priv->base + PCI_EPC_GEN_CMD);
+
+ ret = readl_relaxed_poll_timeout(epc_priv->base + PCI_EPC_GEN_LINKUP,
+ linkup, linkup, 1000, 1000000);
+ if (ret) {
+ writel_relaxed(PCI_EPC_GEN_CMD_STOP, epc_priv->base + PCI_EPC_GEN_CMD);
+ return ret;
+ }
+
+ pci_epc_linkup(epc);
+
+ return 0;
+}
+
+static void pci_ep_generic_stop(struct pci_epc *epc)
+{
+ struct pci_ep_generic *epc_priv = epc_get_drvdata(epc);
+
+ writel_relaxed(PCI_EPC_GEN_CMD_STOP, epc_priv->base + PCI_EPC_GEN_CMD);
+}
+
+static const struct pci_epc_features *
+pci_ep_generic_get_features(struct pci_epc *epc, u8 fn, u8 vfn)
+{
+ return &((struct pci_ep_generic *)epc_get_drvdata(epc))->features;
+}
+
+static const struct pci_epc_ops pci_ep_generic_ops = {
+ .write_header = pci_ep_generic_write_header,
+ .set_bar = pci_ep_generic_set_bar,
+ .clear_bar = pci_ep_generic_clear_bar,
+ .map_addr = pci_ep_generic_map_addr,
+ .unmap_addr = pci_ep_generic_unmap_addr,
+ .set_msi = pci_ep_generic_set_msi,
+ .get_msi = pci_ep_generic_get_msi,
+ .set_msix = pci_ep_generic_set_msix,
+ .get_msix = pci_ep_generic_get_msix,
+ .raise_irq = pci_ep_generic_raise_irq,
+ .map_msi_irq = pci_ep_generic_map_msi_irq,
+ .start = pci_ep_generic_start,
+ .stop = pci_ep_generic_stop,
+ .get_features = pci_ep_generic_get_features,
+};
+
+static void pci_ep_generic_init_features(struct pci_ep_generic *epc_priv)
+{
+ u32 caps = readl_relaxed(epc_priv->base + PCI_EPC_GEN_CAPS);
+ struct pci_epc_features *f = &epc_priv->features;
+
+ f->intx_capable = !!(caps & PCI_EPC_GEN_CAP_INTX);
+ f->msi_capable = !!(caps & PCI_EPC_GEN_CAP_MSI);
+ f->msix_capable = !!(caps & PCI_EPC_GEN_CAP_MSIX);
+ f->linkup_notifier = !!(caps & PCI_EPC_GEN_CAP_LINKUP_NOTIFY);
+ f->dynamic_inbound_mapping = !!(caps & PCI_EPC_GEN_CAP_DYN_INBOUND_MAP);
+ f->subrange_mapping = !!(caps & PCI_EPC_GEN_CAP_SUBRANGE);
+
+ /* All BARs are fully programmable */
+ for (int i = 0; i < PCI_STD_NUM_BARS; i++)
+ f->bar[i].type = BAR_PROGRAMMABLE;
+}
+
+static int pci_ep_generic_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct pci_ep_generic *epc_priv;
+ struct pci_epc *epc;
+ struct resource *res;
+ int ret;
+
+ epc_priv = devm_kzalloc_obj(dev, struct pci_ep_generic);
+ if (!epc_priv)
+ return -ENOMEM;
+
+ epc_priv->base = devm_platform_ioremap_resource_byname(pdev, "ctrl");
+ if (IS_ERR(epc_priv->base))
+ return PTR_ERR(epc_priv->base);
+
+ res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "outbound");
+ if (!res)
+ return dev_err_probe(dev, -ENODEV, "missing \"outbound\" memory resource\n");
+
+ if (!devm_request_mem_region(dev, res->start, resource_size(res),
+ dev_name(dev)))
+ return dev_err_probe(dev, -EBUSY, "outbound window already in use\n");
+
+ ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to set DMA mask\n");
+
+ pci_ep_generic_init_features(epc_priv);
+
+ epc = devm_pci_epc_create(dev, &pci_ep_generic_ops);
+ if (IS_ERR(epc))
+ return PTR_ERR(epc);
+
+ epc->max_functions = readl_relaxed(epc_priv->base + PCI_EPC_GEN_MAX_FUNC);
+ epc_set_drvdata(epc, epc_priv);
+ platform_set_drvdata(pdev, epc);
+
+ ret = pci_epc_mem_init(epc, res->start, resource_size(res), PAGE_SIZE);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to initialise outbound window\n");
+
+ pci_epc_init_notify(epc);
+
+ return 0;
+}
+
+static void pci_ep_generic_remove(struct platform_device *pdev)
+{
+ struct pci_epc *epc = platform_get_drvdata(pdev);
+
+ pci_epc_deinit_notify(epc);
+ pci_epc_mem_exit(epc);
+}
+
+static const struct of_device_id pci_ep_generic_of_match[] = {
+ { .compatible = "pci-ep-generic" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, pci_ep_generic_of_match);
+
+static struct platform_driver pci_ep_generic_driver = {
+ .driver = {
+ .name = "pci-ep-generic",
+ .of_match_table = pci_ep_generic_of_match,
+ },
+ .probe = pci_ep_generic_probe,
+ .remove = pci_ep_generic_remove,
+};
+module_platform_driver(pci_ep_generic_driver);
+
+MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@xxxxxxxxxxxxxxxx>");
+MODULE_DESCRIPTION("Generic PCI Endpoint Controller driver");
+MODULE_LICENSE("GPL");
--
2.53.0