* [PATCH 0/2] PCI: endpoint: Add a generic PCI Endpoint Controller driver
@ 2026-12-04 14:00 Manivannan Sadhasivam via B4 Relay
2026-12-04 14:00 ` [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller Manivannan Sadhasivam via B4 Relay
2026-12-04 14:00 ` [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay
0 siblings, 2 replies; 5+ messages in thread
From: Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:00 UTC (permalink / raw)
To: Lorenzo Pieralisi, Krzysztof Wilczyński,
Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas,
Krzysztof Kozlowski, Conor Dooley
Cc: linux-pci, devicetree, linux-kernel, Manivannan Sadhasivam
Hi,
This series adds a generic PCI Endpoint Controller driver for the
firmware-initialised and emulated PCI Endpoint controllers. For a firmware
initialised controller, all the resources such as Clocks, Regulators, PHY are
configured by the firmware. So the controller driver just need to program the
MMIO interface to configure the controller.
So this driver fetches 'ctrl' and 'outbound' MMIO regions from DT and configures
them based on the Function driver.
Testing
=======
This generic EP controller driver was tested with the below QEMU patchset:
https://lore.kernel.org/qemu-devel/20261204-pcie-ep-emulation-v2-0-ce3b3a393e7f@kernel.org/
Instructions to test this driver with QEMU is provided in the cover letter of
the above mentioned QEMU patchset.
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>
---
Manivannan Sadhasivam (2):
dt-bindings: PCI: Add generic PCI Endpoint Controller
PCI: endpoint: Add generic PCI Endpoint Controller driver
.../devicetree/bindings/pci/pci-ep-generic.yaml | 48 +++
drivers/pci/controller/Kconfig | 12 +
drivers/pci/controller/Makefile | 1 +
drivers/pci/controller/pci-ep-generic.c | 387 +++++++++++++++++++++
4 files changed, 448 insertions(+)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261203-pci-ep-generic-3c4f4979f8a8
Best regards,
--
மணிவண்ணன் சதாசிவம்
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller 2026-12-04 14:00 [PATCH 0/2] PCI: endpoint: Add a generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:00 ` Manivannan Sadhasivam via B4 Relay 2026-12-04 14:14 ` sashiko-bot 2026-12-04 14:00 ` [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay 1 sibling, 1 reply; 5+ messages in thread From: Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:00 UTC (permalink / raw) To: Lorenzo Pieralisi, Krzysztof Wilczyński, Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas, Krzysztof Kozlowski, Conor Dooley Cc: linux-pci, devicetree, linux-kernel, Manivannan Sadhasivam From: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com> Add a devicetree binding for PCI Endpoint Controllers that are initialised by firmware or provided by an emulation such as QEMU. These controllers need no clock, regulator or PHY as all resources are supposed to be initialised by the firmware. So the binding only describes the memory-mapped 'ctrl' region used to configure the Functions and the 'outbound' region used to communicate with the Root Complex. Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com> --- .../devicetree/bindings/pci/pci-ep-generic.yaml | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/Documentation/devicetree/bindings/pci/pci-ep-generic.yaml b/Documentation/devicetree/bindings/pci/pci-ep-generic.yaml new file mode 100644 index 000000000000..aa0ba619fe25 --- /dev/null +++ b/Documentation/devicetree/bindings/pci/pci-ep-generic.yaml @@ -0,0 +1,48 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pci/pci-ep-generic.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Generic PCI Endpoint Controller + +maintainers: + - Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com> + +description: + A PCI Endpoint Controller that is initialised by firmware or provided by an + emulation, and is configured through a memory-mapped control region. + +properties: + compatible: + const: pci-ep-generic + + reg: + items: + - description: control region + - description: outbound window + + reg-names: + items: + - const: ctrl + - const: outbound + +required: + - compatible + - reg + - reg-names + +allOf: + - $ref: pci-ep.yaml# + +unevaluatedProperties: false + +examples: + - | + pcie-ep@10000000 { + compatible = "pci-ep-generic"; + reg = <0x10000000 0x1000>, + <0x20000000 0x8000000>; + reg-names = "ctrl", "outbound"; + }; +... -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller 2026-12-04 14:00 ` [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:14 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-12-04 14:14 UTC (permalink / raw) To: Manivannan Sadhasivam; +Cc: linux-pci, devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dt-bindings: PCI: undefined register interface for pci-ep-generic - [Low] dt-bindings: PCI: pci-ep-generic schema rejects dma-coherent property -- commit 5b3ab239c0325679aaf0982e6f6dac40dd23a5b2 Author: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com> dt-bindings: PCI: Add generic PCI Endpoint Controller This commit adds a Devicetree binding schema for a generic PCI Endpoint Controller configured through memory-mapped ctrl and outbound regions. > diff --git a/Documentation/devicetree/bindings/pci/pci-ep-generic.yaml b/Documentation/devicetree/bindings/pci/pci-ep-generic.yaml > new file mode 100644 > index 0000000000000..aa0ba619fe251 > --- /dev/null > +++ b/Documentation/devicetree/bindings/pci/pci-ep-generic.yaml > @@ -0,0 +1,48 @@ [ ... ] > +description: > + A PCI Endpoint Controller that is initialised by firmware or provided by an > + emulation, and is configured through a memory-mapped control region. > + > +properties: > + compatible: > + const: pci-ep-generic > + > + reg: > + items: > + - description: control region > + - description: outbound window > + > + reg-names: > + items: > + - const: ctrl > + - const: outbound [Severity: Medium] Can firmware, emulators, and OS drivers agree on the programming interface for pci-ep-generic without a documented or standardized ctrl register layout? Unlike pci-host-cam-generic and pci-host-ecam-generic in host-generic-pci.yaml, which rely on PCI-SIG standardized CAM and ECAM layouts, PCI-SIG defines no standard MMIO register interface for PCI Endpoint Controllers. Even when firmware initializes clocks, regulators, and PHYs, existing endpoint controller IPs have mutually incompatible ctrl register layouts. A single vendorless compatible therefore conflates incompatible hardware with a custom emulated device. Consider documenting or referencing the ctrl MMIO register specification (using a vendor-prefixed compatible if this targets a QEMU virtual device) or using IP-specific compatibles for firmware-initialized hardware. [ ... ] > +allOf: > + - $ref: pci-ep.yaml# > + > +unevaluatedProperties: false [Severity: Low] Should this schema allow dma-coherent? Neither pci-ep-generic.yaml nor pci-ep.yaml declares dma-coherent, so unevaluatedProperties: false causes dtbs_check to reject any cache-coherent pci-ep-generic node. Endpoint function drivers allocate BAR memory via dma_alloc_coherent() in pci_epf_alloc_space(), so coherent controllers need dma-coherent on platforms where DMA is not coherent by default. Adding dma-coherent: true here (or in pci-ep.yaml) would match host-generic-pci.yaml. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261204-pci-ep-generic-v1-0-30c0e44e170f@oss.qualcomm.com?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver 2026-12-04 14:00 [PATCH 0/2] PCI: endpoint: Add a generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay 2026-12-04 14:00 ` [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:00 ` Manivannan Sadhasivam via B4 Relay 2026-12-04 14:43 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:00 UTC (permalink / raw) To: Lorenzo Pieralisi, Krzysztof Wilczyński, Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas, Krzysztof Kozlowski, Conor Dooley Cc: linux-pci, devicetree, linux-kernel, Manivannan Sadhasivam From: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com> 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@oss.qualcomm.com> --- 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@oss.qualcomm.com> + */ + +#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@oss.qualcomm.com>"); +MODULE_DESCRIPTION("Generic PCI Endpoint Controller driver"); +MODULE_LICENSE("GPL"); -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver 2026-12-04 14:00 ` [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay @ 2026-12-04 14:43 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-12-04 14:43 UTC (permalink / raw) To: Manivannan Sadhasivam; +Cc: linux-pci, devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 12 potential issues to consider: - [High] PCI: endpoint: deadlock calling pci_epc_linkup() under epc->lock in start - [High] PCI: endpoint: broken multi-slot mapping and offsets in map_msi_irq() - [High] PCI: endpoint: advertised subrange_mapping ignored in set_bar() - [Medium] PCI: endpoint: unvalidated max_functions and ctrl size in probe() - [Medium] PCI: endpoint: relaxed CMD writes omit write barrier in start and stop - [Medium] PCI: endpoint: hardcoded 32-bit DMA mask despite 64-bit address registers - [Medium] PCI: endpoint: pci_ep_generic_remove() omits stopping the endpoint link - [Medium] PCI: endpoint: premature pci_epc_mem_exit() UAF in pci_ep_generic_remove() - [Medium] PCI: endpoint: EPC exposed before epc_set_drvdata() in probe() - [Low] PCI: endpoint: raw Linux PCI_IRQ flags written unvalidated in raise_irq() - [Low] PCI: endpoint: driver includes internal linux/dma-map-ops.h header - [Low] PCI: endpoint: include linux/dma-mapping.h instead of internal dma-map-ops.h -- commit a1bc1c2d472582e2deeb1171bc1e667a9ff9e083 Author: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com> PCI: endpoint: Add generic PCI Endpoint Controller driver This commit adds a platform driver for a generic firmware-initialised PCI Endpoint Controller using a simple MMIO register interface and an outbound memory window. > diff --git a/drivers/pci/controller/pci-ep-generic.c b/drivers/pci/controller/pci-ep-generic.c > new file mode 100644 > index 0000000000000..8ebed7cb8370b > --- /dev/null > +++ b/drivers/pci/controller/pci-ep-generic.c > @@ -0,0 +1,387 @@ [ ... ] > +#include <linux/dma-map-ops.h> [Severity: Low] Should this include linux/dma-mapping.h instead of linux/dma-map-ops.h? include/linux/dma-map-ops.h is reserved for dma_map_ops implementations and states that drivers just using the DMA API should not include it. [ ... ] > +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; > +} [Severity: Low] Should pci_ep_generic_raise_irq() translate type into controller-defined constants and validate it against the controller's capabilities instead of writing raw Linux PCI_IRQ_* flags directly to PCI_EPC_GEN_IRQ_TYPE? This ties the MMIO register ABI to internal kernel allocation bitmasks and silently returns 0 instead of -EINVAL when an unsupported or invalid IRQ type is requested. > +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); > +} [Severity: High] Does pci_ep_generic_map_msi_irq() break the pci_epc_map_msi_irq() contract for multi-doorbell mapping? Its caller epf_ntb_configure_msi() passes the doorbell count as irq_num, sets peer_ctrl->db_data[i] = db_data | i, and the host driver ntb_epf_peer_db_set() writes to ndev->db_reg + (db_entry_size * interrupt_num) + db_offset. Here only a single entry_size window is mapped instead of looping irq_num times across consecutive entry_size slots (compare cdns_pcie_ep_map_msi_irq()), leaving slots 1..irq_num-1 unmapped. Adding (u32)irq_num * entry_size to *msi_addr_offset also pushes every host doorbell write past the mapped doorbell window, and adding irq_num to *msi_data instead of masking the lower vector bits corrupts the MSI vector numbers. It also does not check PCI_EPC_GEN_MSI to confirm MSI is enabled with 1 <= irq_num <= msi_count, nor that entry_size is a non-zero power of two before ALIGN_DOWN(). > +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; > +} [Severity: High] Can calling pci_epc_linkup() from pci_ep_generic_start() cause an ABBA deadlock? pci_epc_start() holds epc->lock across epc->ops->start(), while pci_epc_linkup() acquires epc->list_lock and then epf->lock. The rest of the PCI endpoint subsystem acquires these locks in the reverse order: CPU0 CPU1 ---- ---- lock(&epc->lock) lock(&epc->list_lock) lock(&epf->lock) lock(&epc->list_lock) lock(&epc->lock) *** DEADLOCK *** CPU0: pci_epc_start() -> pci_ep_generic_start() -> pci_epc_linkup() CPU1: pci_ep_generic_remove() -> pci_epc_deinit_notify() -> pci_epf_test_epc_deinit() -> pci_epc_clear_bar() Any EPF link_up() callback that calls a pci_epc_*() helper will also self-deadlock on epc->lock, and polling PCI_EPC_GEN_LINKUP for up to 1 second holds epc->lock across the entire controller. In addition, pci_epc_linkup() is called even when features.linkup_notifier is not set, and pci_ep_generic_stop() never notifies pci_epc_linkdown(). Deferring link polling and linkup notification to a work item outside epc->lock (gated on features.linkup_notifier and canceled on stop/remove) would avoid this. > +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); > +} [Severity: Medium] Should pci_ep_generic_start() and pci_ep_generic_stop() use pci_ep_generic_write_cmd() (ordered writel()) and readl_poll_timeout() instead of relaxed MMIO accesses? Using writel_relaxed() for PCI_EPC_GEN_CMD_START omits the write barrier (__io_bw()) needed on weakly-ordered architectures to ensure prior CPU writes to DMA-coherent BAR buffers are visible before the link comes up. [ ... ] > + f->dynamic_inbound_mapping = !!(caps & PCI_EPC_GEN_CAP_DYN_INBOUND_MAP); > + f->subrange_mapping = !!(caps & PCI_EPC_GEN_CAP_SUBRANGE); [Severity: High] Should f->subrange_mapping be left unset while pci_ep_generic_set_bar() does not implement subrange mapping? When both dynamic_inbound_mapping and subrange_mapping are advertised, pci_epc_set_bar() allows EPF drivers to pass epf_bar->num_submap > 0, which pci_ep_generic_set_bar() silently ignores while programming only the top-level epf_bar->phys_addr and returning 0. [ ... ] > + 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"); [Severity: Medium] Why is the DMA mask hardcoded to 32 bits when the register interface has 64-bit physical address registers (PCI_EPC_GEN_BAR_PHYS_LO/HI and PCI_EPC_GEN_MAP_PHYS_LO/HI)? EPF drivers allocate BAR buffers with dma_alloc_coherent(epc->dev.parent, ...) and map doorbells with dma_map_resource(epc->dev.parent, ...), so a 32-bit mask breaks allocations and mappings on 64-bit platforms with RAM or doorbells above 4 GiB and forces SWIOTLB bounce buffering. [ ... ] > + 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); [Severity: Medium] Should pci_ep_generic_probe() validate PCI_EPC_GEN_MAX_FUNC and the "ctrl" resource size? If PCI_EPC_GEN_MAX_FUNC exceeds the number of 0x1000-byte strides in the "ctrl" resource (or reads 0xffffffff on a fault, truncating to 255), per-function MMIO accesses at fn * PCI_EPC_GEN_REG_STRIDE will access past the end of the ioremapped region. Conversely, if it reads 0 (or truncates to u8 0), the check func_no > epc->max_functions - 1 in pci_epc_add_epf() underflows to 0xffffffffU and allows invalid EPF binding. [Severity: Medium] Can a concurrent configfs access hit a NULL pointer dereference between devm_pci_epc_create() and epc_set_drvdata()? devm_pci_epc_create() immediately publishes the controller in configfs via pci_ep_cfs_add_epc_group() while epc_get_drvdata(epc) is still NULL and outbound memory is uninitialized: CPU0 CPU1 pci_ep_generic_probe() devm_pci_epc_create() pci_ep_cfs_add_epc_group() pci_epc_start_store() pci_epc_start() pci_ep_generic_start() epc_priv->base <-- NULL deref epc_set_drvdata(epc, epc_priv) Setting epc->max_functions before epc_set_drvdata() and pci_epc_mem_init() also lets a concurrent EPF bind call pci_ep_generic_get_features() on a NULL epc_priv (returning an invalid pointer at offset 0x8) or fail outbound allocations. [ ... ] > +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); > +} [Severity: Medium] Should pci_ep_generic_remove() stop the endpoint link (via pci_epc_stop() or PCI_EPC_GEN_CMD_STOP) before tearing down EPF notifications and outbound memory? Because the driver is tristate and unbindable, removing it while the controller is started leaves the link active while BARs and DMA buffers are torn down. [Severity: Medium] Can calling pci_epc_mem_exit() in pci_ep_generic_remove() cause a use-after-free while the EPC is still registered? Because epc is created with devm_pci_epc_create(), pci_epc_destroy() only removes the configfs group and unregisters the device in devres_release_all() after pci_ep_generic_remove() returns. pci_epc_mem_exit() frees epc->windows and each mem without locking: CPU0 CPU1 pci_ep_generic_remove() pci_epc_mem_exit(epc) kfree(mem) pci_epc_mem_alloc_addr() mutex_lock(&mem->lock) <-- UAF devres_release_all() pci_epc_destroy() In addition, if an EPF without an epc_deinit callback calls pci_epc_mem_free_addr() after pci_epc_mem_exit() zeroes epc->num_windows, pci_epc_get_matching_window() returns NULL and leaks the ioremap mapping. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261204-pci-ep-generic-v1-0-30c0e44e170f@oss.qualcomm.com?part=2 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 14:43 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-04 14:00 [PATCH 0/2] PCI: endpoint: Add a generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay 2026-12-04 14:00 ` [PATCH 1/2] dt-bindings: PCI: Add generic PCI Endpoint Controller Manivannan Sadhasivam via B4 Relay 2026-12-04 14:14 ` sashiko-bot 2026-12-04 14:00 ` [PATCH 2/2] PCI: endpoint: Add generic PCI Endpoint Controller driver Manivannan Sadhasivam via B4 Relay 2026-12-04 14:43 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox