Add PCIe Endpoint emulation for the Linux PCI Endpoint framework
cover Add PCIe Endpoint emulation for the Linux PCI Endpoint framework
The Linux PCI Endpoint framework lets a Linux system act as the peripheral side of a PCI link instead of the host. It runs on SoCs whose PCIe controller supports Endpoint mode, with Linux running on the Endpoint device itself. The kernel there uses the framework to set up the PCI config space (vendor and device IDs, class), expose BARs backed by local memory, negotiate MSI and MSI-X, map host memory, and raise interrupts to the host. The personality the Endpoint presents is provided by an Endpoint Function driver, for example pci-epf-test or pci-epf-nvme, bound to an Endpoint Controller (EPC) driver through configfs. This is how a Linux SoC can be turned into a device such as an NVMe drive or a test function that a separate host enumerates as an ordinary PCI device.
Working on this code today requires real Endpoint-capable hardware and a second host machine connected over a physical PCIe link. Such setups are not widely available and are impractical for continuous integration, so the framework, EPC drivers, and Endpoint Function drivers cannot be exercised in a plain QEMU or CI environment.
This series closes that gap by emulating a PCIe Endpoint Controller and a PCIe Endpoint Function in QEMU. It lets developers run the Linux Endpoint stack, both EPC drivers and Endpoint Function drivers such as pci-epf-test, entirely inside QEMU without real Endpoint hardware.
Two devices are added:
- pcie-ep-ctrl (hw/misc): a SysBus platform device that emulates the Endpoint Controller. It exposes an MMIO register interface compatible with the pci-ep-generic Linux EPC driver and orchestrates the Endpoint Function.
- pcie-ep-generic (hw/pci): the PCIe Endpoint Function. The controller builds it from the staged registers and hotplugs it onto a Root Port when the Endpoint driver starts the link.
Everything runs in a single QEMU instance. One guest kernel acts as both the Endpoint Controller and the PCI host that enumerates the resulting Function, sharing one guest address space. Outbound maps are memory region aliases onto guest RAM and interrupts are raised directly on the Function, so no second QEMU instance or inter-process protocol is needed.
The controller is wired into the ARM virt machine, which emits the device tree node the pci-ep-generic EPC driver binds to.
Testing
-------
The pci-ep-generic EPC driver that binds to this controller is not upstream yet. The kernel used for testing includes this out-of-tree commit:
https://github.com/Mani-Sadhasivam/linux/commit/d23bc106b79579ddb979be6b141e71712a70acfa
Built for aarch64 and run under TCG on the virt machine with a single guest kernel that provides both Endpoint and host support (CONFIG_PCI_EP_GENERIC, CONFIG_PCI_EPF_TEST, CONFIG_PCI_ENDPOINT_TEST):
qemu-system-aarch64 -machine virt -cpu cortex-a57 -m 1G \
-kernel Image -initrd rootfs.cpio.gz -display none -no-reboot \
-device pcie-root-port,id=rp0,bus=pcie.0,chassis=1,slot=1,hotplug=on \
-device pcie-ep-ctrl,target-bus=rp0 \
-append "console=ttyAMA0"Bring up the Function through configfs, binding pci_epf_test to the controller, then start the link:
cd /sys/kernel/config/pci_ep CTRL=$(ls controllers/ | head -n 1) mkdir functions/pci_epf_test/ep0 echo 0x104c > functions/pci_epf_test/ep0/vendorid echo 0xb00d > functions/pci_epf_test/ep0/deviceid echo 1 > functions/pci_epf_test/ep0/msi_interrupts echo 1 > functions/pci_epf_test/ep0/msix_interrupts ln -s functions/pci_epf_test/ep0 controllers/$CTRL/ echo 1 > controllers/$CTRL/start
Once /dev/pci-endpoint-test.0 appears, run the kernel selftest at tools/testing/selftests/pci_endpoint/pci_endpoint_test. Result on this setup:
Totals: pass:10 fail:10 skip:3
The passing cases include all six BAR tests and the memcpy READ, WRITE and COPY transfers, which exercise the memory-aliasing data path. The failures are environmental: the DMA subtests need a DMA engine that TCG does not emulate, INTx is not routed on the virt machine, and the doorbell test is not implemented.
---
Manivannan Sadhasivam (4):
docs/system/devices: Add PCIe Endpoint emulation documentation hw/pci: Add PCIe Endpoint Function device (pcie-ep-generic) hw/misc: Add PCIe Endpoint Controller emulation (pcie-ep-ctrl) hw/arm/virt: Add PCIe Endpoint Controller support
MAINTAINERS | 9 + docs/system/device-emulation.rst | 1 + docs/system/devices/pcie-ep.rst | 140 +++++++++ hw/arm/Kconfig | 1 + hw/arm/virt.c | 57 ++++ hw/core/sysbus-fdt.c | 3 + hw/misc/Kconfig | 4 + hw/misc/meson.build | 1 + hw/misc/pcie-ep-ctrl.c | 662 +++++++++++++++++++++++++++++++++++++++ hw/pci/Kconfig | 4 + hw/pci/meson.build | 1 + hw/pci/pcie-ep-generic.c | 255 +++++++++++++++ include/hw/misc/pcie-ep-ctrl.h | 68 ++++ include/hw/pci/pcie-ep-generic.h | 71 +++++ 14 files changed, 1277 insertions(+)
---
base-commit: 193963218accbcf65f28ab1efa773ac6d1cc634c
change-id: 20260913-pcie-ep-emulation-d7d0722677eb
Best regards,
Signature · 1 line
மணிவண்ணன் சதாசிவம்
PATCH 1/4 docs/system/devices: Add PCIe Endpoint emulation documentation
From: Manivannan Sadhasivam <[email protected]>
Document the PCIe Endpoint Controller (pcie-ep-ctrl) and the Endpoint Function (pcie-ep-generic), including how they share a single guest address space.
The documentation walks through the QEMU invocation and the configfs bring-up of an Endpoint Function. It also covers the kernel configuration needed for a single guest to act as both the Endpoint Controller and the PCI host that enumerates the Function. The current limitations are listed at the end.
--- MAINTAINERS | 5 ++ docs/system/device-emulation.rst | 1 + docs/system/devices/pcie-ep.rst | 140 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+)
MAINTAINERS +5 -0
@@ -2170,6 +2170,11 @@ F: docs/pci* F: docs/specs/*pci* F: docs/system/sriov.rst +PCIe Endpoint Emulation +M: Manivannan Sadhasivam <[email protected]> +S: Maintained +F: docs/system/devices/pcie-ep.rst + ARM PCI Hotplug M: Gustavo Romero <[email protected]> L: [email protected]
docs/system/device-emulation.rst +1 -0
@@ -95,6 +95,7 @@ Emulated Devices devices/keyboard.rst devices/net.rst devices/nvme.rst + devices/pcie-ep.rst devices/scsi/index.rst devices/usb-u2f.rst devices/usb.rst
docs/system/devices/pcie-ep.rst new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/4 hw/pci: Add PCIe Endpoint Function device (pcie-ep-generic)
In reply to Manivannan SadhasivamFrom: Manivannan Sadhasivam <[email protected]>
Add the PCIe Endpoint Function device driven by the pcie-ep-ctrl Endpoint Controller.
The device is created programmatically by the controller and hotplugged onto a PCIe Root Port when the Link comes up. It is not instantiable with -device.
Its PCI identity, BAR layout and interrupt capabilities all come from the controller, which programs them from the register writes issued by the pci-ep-generic Linux EPC driver.
Each enabled BAR is a memory region alias onto guest RAM at the address the controller was given. This way, the Endpoint Function and the host share the same backing memory without any copying.
Interrupts raised by the controller are delivered to the host through msi_notify(), msix_notify() or a legacy INTx toggle.
--- MAINTAINERS | 2 + hw/pci/Kconfig | 4 + hw/pci/meson.build | 1 + hw/pci/pcie-ep-generic.c | 255 +++++++++++++++++++++++++++++++++++++++ include/hw/pci/pcie-ep-generic.h | 71 +++++++++++ 5 files changed, 333 insertions(+)
MAINTAINERS +2 -0
@@ -2174,6 +2174,8 @@ PCIe Endpoint Emulation M: Manivannan Sadhasivam <[email protected]> S: Maintained F: docs/system/devices/pcie-ep.rst +F: hw/pci/pcie-ep-generic.c +F: include/hw/pci/pcie-ep-generic.h ARM PCI Hotplug M: Gustavo Romero <[email protected]>
hw/pci/Kconfig +4 -0
@@ -11,6 +11,10 @@ config PCI_DEVICES config PCIE_DEVICES bool +config PCIE_EP_GENERIC + bool + select PCI_EXPRESS + config MSI_NONBROKEN # selected by interrupt controllers that do not support MSI, # or support it and have a good implementation. See commit
hw/pci/meson.build +1 -0
@@ -17,6 +17,7 @@ pci_ss.add(files( pci_ss.add(files('pcie.c', 'pcie_aer.c')) pci_ss.add(files('pcie_doe.c')) system_ss.add(when: 'CONFIG_PCI_EXPRESS', if_true: files('pcie_port.c', 'pcie_host.c')) +system_ss.add(when: 'CONFIG_PCIE_EP_GENERIC', if_true: files('pcie-ep-generic.c')) system_ss.add_all(when: 'CONFIG_PCI', if_true: pci_ss) stub_ss.add(files('pci-stub.c'))
hw/pci/pcie-ep-generic.c new file +255 -0
255 lines not on this page · raw message
include/hw/pci/pcie-ep-generic.h new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 3/4 hw/misc: Add PCIe Endpoint Controller emulation (pcie-ep-ctrl)
In reply to Manivannan SadhasivamFrom: Manivannan Sadhasivam <[email protected]>
Add a SysBus platform device that emulates a PCIe Endpoint Controller (EPC) for use with the Linux PCI Endpoint framework.
It exposes an MMIO register interface compatible with the pci-ep-generic Linux EPC driver. The register file is a plain scratchpad. A command register at offset 0xf0 accepts opcodes (WRITE_HEADER, SET_BAR, CLEAR_BAR, MAP, UNMAP, SET_MSI, SET_MSIX, RAISE_IRQ, START, STOP) and the controller only acts when the driver writes an opcode to it. The driver stages every field it needs and issues one command write to commit. A small set of read-only registers (CAPS, MAX_FUNC, LINKUP, MSI/MSI-X readback and MSI address/data) reports firmware-configured or live-negotiated state.
The controller also acts as the orchestrator for the Endpoint Function. On CMD_START it builds a pcie-ep-generic Function from the staged registers and hotplugs it onto the Root Port named by the target-bus property. CMD_STOP unplugs it again. Because qdev_realize must not run inline from an MMIO write handler, the hotplug work is deferred to a bottom half.
Everything runs inside a single QEMU instance. The controller drives the Endpoint with direct in-process calls instead of any inter-process protocol. Outbound address maps are memory region aliases onto guest RAM. Interrupts are raised straight on the Endpoint Function. This allows one guest kernel to act as both the Endpoint Controller and the PCI host that enumerates the resulting Function.
--- MAINTAINERS | 2 + hw/misc/Kconfig | 4 + hw/misc/meson.build | 1 + hw/misc/pcie-ep-ctrl.c | 662 +++++++++++++++++++++++++++++++++++++++++ include/hw/misc/pcie-ep-ctrl.h | 68 +++++ 5 files changed, 737 insertions(+)
MAINTAINERS +2 -0
@@ -2176,6 +2176,8 @@ S: Maintained F: docs/system/devices/pcie-ep.rst F: hw/pci/pcie-ep-generic.c F: include/hw/pci/pcie-ep-generic.h +F: hw/misc/pcie-ep-ctrl.c +F: include/hw/misc/pcie-ep-ctrl.h ARM PCI Hotplug M: Gustavo Romero <[email protected]>
hw/misc/Kconfig +4 -0
@@ -224,6 +224,10 @@ config SIFIVE_U_PRCI config VIRT_CTRL bool +config PCIE_EP_CTRL + bool + select PCIE_EP_GENERIC + config LASI bool select LASI_82596
hw/misc/meson.build +1 -0
@@ -26,6 +26,7 @@ system_ss.add(when: 'CONFIG_IOSB', if_true: files('iosb.c')) # virt devices system_ss.add(when: 'CONFIG_VIRT_CTRL', if_true: files('virt_ctrl.c')) +system_ss.add(when: 'CONFIG_PCIE_EP_CTRL', if_true: files('pcie-ep-ctrl.c')) # RISC-V devices system_ss.add(when: 'CONFIG_MCHP_PFSOC_DMC', if_true: files('mchp_pfsoc_dmc.c'))
hw/misc/pcie-ep-ctrl.c new file +662 -0
662 lines not on this page · raw message
include/hw/misc/pcie-ep-ctrl.h new file +68 -0
68 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 4/4 hw/arm/virt: Add PCIe Endpoint Controller support
In reply to Manivannan SadhasivamFrom: Manivannan Sadhasivam <[email protected]>
Wire the pcie-ep-ctrl controller into the ARM virt machine as a dynamic sysbus device on the platform bus.
-device pcie-ep-ctrl,target-bus=ID
The platform bus assigns the MMIO windows, so the machine emits the device tree node (compatible "pci-ep-generic") with the resolved addresses. The node is also marked dma-coherent. This allows the Endpoint Controller driver to allocate coherent DMA memory from system RAM for the Endpoint Function BARs.
Allow the device on the platform bus with machine_class_allow_dynamic_sysbus_dev(). Also register a no_fdt_node binding in hw/core/sysbus-fdt.c so the generic platform bus code leaves the node to the machine.
The target-bus property names the PCIe Root Port the Endpoint Function is hotplugged onto when the Link comes up.
A single guest kernel then drives the controller through the pci-ep-generic EPC driver. In the same kernel, it enumerates the resulting Function as a PCI host. This allows running Linux EPF drivers such as pci-epf-test against the matching host driver without a second QEMU instance.
--- hw/arm/Kconfig | 1 + hw/arm/virt.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/core/sysbus-fdt.c | 3 +++ 3 files changed, 61 insertions(+)
hw/arm/Kconfig +1 -0
@@ -36,6 +36,7 @@ config ARM_VIRT select VIRTIO_MEM_SUPPORTED select ACPI_CXL select ACPI_HMAT + select PCIE_EP_CTRL config CUBIEBOARD bool
hw/arm/virt.c +57 -0
81 lines not on this page · raw message
hw/core/sysbus-fdt.c +3 -0
@@ -36,6 +36,7 @@#include "hw/display/ramfb.h" #include "hw/uefi/var-service-api.h" #include "hw/arm/fdt.h" +#include "hw/misc/pcie-ep-ctrl.h" /* * internal struct that contains the information to create dynamic
@@ -140,6 +141,8 @@ static const BindingEntry bindings[] = {TYPE_BINDING (TYPE_ARM_SMMUV3 , no_fdt_node),TYPE_BINDING (TYPE_RAMFB_DEVICE , no_fdt_node),TYPE_BINDING (TYPE_UEFI_VARS_SYSBUS , add_uefi_vars_node), +/* Node emitted by the arm virt machine (create_pcie_ep_ctrl_dtb) */ +TYPE_BINDING (TYPE_PCIE_EP_CTRL , no_fdt_node), TYPE_BINDING("", NULL), /* last element */ };
Signature · 1 line
2.43.0