hw/pci-bridge: Configurable apparent bandwidth for the PCIe topology
v3 cover hw/pci-bridge: Configurable apparent bandwidth for the PCIe topology
When a VM has been assigned a number of PCIe devices behind a common PCIe switch and the VM intends to use peer-to-peer (P2P) transfers between the devices, it is useful to be able to more report the apparent PCIe bandwidth so it reflects the real handwidth of the underlying hardware. This allows software in the VM (such as NCCL[1]) to make better decisions about whether to use P2P or not.
The existing emulated PCIe switch is a vintage TI XIO3130 and only support 2.5 GT/s and x1, so we introduces new generic upstream and downstream port devices (pcie-upstream-port and pcie-downstream-port), inspired by the existing generic pcie-root-port device.
The existing experimental "x-speed" and "x-width" options are made non-experimental, allowing the pcie-root-port and pcie-downstream-port capabilities to be configured to match the hardware.
The pcie-root-port device is updated to default to a Gen6 capable device.
Finally, where a root or upstream port's bandwidth has been limited, we also ensure the other end of the link reports the same link status.
Note that the device IDs for the new port devices are currently placeholders and will need to be allocated.
Changes in v3:
- Correct PCIe device ID allocation.
- Compat value for pcie-root-port.speed for QEMU 11.1 and earlier.
- Trivial style changes.
Changes in v2:
- Initial public version.
David
[1] https://developer.nvidia.com/nccl
David Vrabel (4):
hw/pci-bridge: Add generic PCIe switch upstream and downstream ports hw/pci-bridge: Make speed and width for PCIe ports non-experimental hw/pci-bridge: Default the generic PCIe root port as Gen6 capable hw/pci: Ensure PCIe link speed/width is consistent between both ends
docs/pcie.txt | 6 +- docs/specs/pci-ids.rst | 4 + hw/core/machine.c | 1 + hw/pci-bridge/gen_pcie_downstream_port.c | 202 ++++++++++++++++++++ hw/pci-bridge/gen_pcie_root_port.c | 6 +- hw/pci-bridge/gen_pcie_upstream_port.c | 177 +++++++++++++++++ hw/pci-bridge/meson.build | 3 +- hw/pci/pcie.c | 26 ++- include/hw/pci/pci.h | 2 + tests/functional/x86_64/meson.build | 1 + tests/functional/x86_64/test_pcie_switch.py | 98 ++++++++++ 11 files changed, 515 insertions(+), 11 deletions(-) create mode 100644 hw/pci-bridge/gen_pcie_downstream_port.c create mode 100644 hw/pci-bridge/gen_pcie_upstream_port.c create mode 100755 tests/functional/x86_64/test_pcie_switch.py
Signature · 1 line
2.43.0
PATCH v3 1/4 hw/pci-bridge: Add generic PCIe switch upstream and downstream ports
The TI XIO3130 PCIe switch is a vintage Gen1 device, so we add generic PCIe upstream and downstream port devices that are Gen6.
In guests with pairs of passthrough PCIe devices (e.g, a GPU+NIC pair) often need to place the devices behind a common (emulated) PCIe switch so in-guest software (such as NCCL) can correctly determine that P2P transactions between the devices is possible.
NCCL also needs to be able to determine the available PCIe bandwidth. If such a guest sees the TI XIO3130 PCIe switch, then it incorrectly calculates only 2.5 GT/s and x1 is available, but the real hardware might be 32 GT/s and x16.
Instead of enhancing the existing switch and claiming it is more capable than the real hardware we add additional generic PCIe upstream and downstream port devices, which matches the existing generic PCIe root port device.
The speed and width of the ports are configurable.
---
Cc: Gerd Hoffmann <[email protected]>
--- docs/pcie.txt | 6 +- docs/specs/pci-ids.rst | 4 + hw/pci-bridge/gen_pcie_downstream_port.c | 202 ++++++++++++++++++++ hw/pci-bridge/gen_pcie_upstream_port.c | 177 +++++++++++++++++ hw/pci-bridge/meson.build | 3 +- include/hw/pci/pci.h | 2 + tests/functional/x86_64/meson.build | 1 + tests/functional/x86_64/test_pcie_switch.py | 98 ++++++++++ 8 files changed, 489 insertions(+), 4 deletions(-) create mode 100644 hw/pci-bridge/gen_pcie_downstream_port.c create mode 100644 hw/pci-bridge/gen_pcie_upstream_port.c create mode 100755 tests/functional/x86_64/test_pcie_switch.py
docs/pcie.txt +3 -3
@@ -85,7 +85,7 @@ number of PCI Express Root Ports per PCI Express Root Bus is 256. Prefer grouping PCI Express Root Ports into multi-function devices to keep a simple flat hierarchy that is enough for most scenarios. -Only use PCI Express Switches (x3130-upstream, xio3130-downstream) +Only use PCI Express Switches (pcie-upstream-port, pcie-downstream-port) if there is no more room for PCI Express Root Ports. Please see section 4. for further justifications.
@@ -120,8 +120,8 @@ Plug only PCI Express devices into PCI Express Ports. -device pcie-root-port,id=root_port3,chassis=x2,addr=z.2[,slot=y2][,bus=pcie.0] \ 2.2.3 Plugging a PCI Express device into a Switch: -device pcie-root-port,id=root_port1,chassis=x,slot=y[,bus=pcie.0][,addr=z] \ - -device x3130-upstream,id=upstream_port1,bus=root_port1[,addr=x] \ - -device xio3130-downstream,id=downstream_port1,bus=upstream_port1,chassis=x1,slot=y1[,addr=z1]] \ + -device pcie-upstream-port,id=upstream_port1,bus=root_port1[,addr=x] \ + -device pcie-downstream-port,id=downstream_port1,bus=upstream_port1,chassis=x1,slot=y1[,addr=z1]] \ -device <dev>,bus=downstream_port1 Notes:
docs/specs/pci-ids.rst +4 -0
@@ -100,6 +100,10 @@ PCI devices (other than virtio): PCI UFS device (``-device ufs``) 1b36:0014 PCI RISC-V IOMMU device +1b36:0015 + PCIe Upstream Switch Port (``-device pcie-upstream-port``) +1b36:0016 + PCIe Downstream Switch Port (``-device pcie-downstream-port``) All these devices are documented in :doc:`index`.
hw/pci-bridge/gen_pcie_downstream_port.c new file +202 -0
202 lines not on this page · raw message
hw/pci-bridge/gen_pcie_upstream_port.c new file +177 -0
177 lines not on this page · raw message
hw/pci-bridge/meson.build +2 -1
@@ -2,7 +2,8 @@ pci_ss = ss.source_set() pci_ss.add(when: 'CONFIG_PCI_BRIDGE', if_true: files('pci_bridge_dev.c')) pci_ss.add(when: 'CONFIG_I82801B11', if_true: files('i82801b11.c')) pci_ss.add(when: 'CONFIG_IOH3420', if_true: files('ioh3420.c')) -pci_ss.add(when: 'CONFIG_PCIE_PORT', if_true: files('pcie_root_port.c', 'gen_pcie_root_port.c')) +pci_ss.add(when: 'CONFIG_PCIE_PORT', if_true: files('pcie_root_port.c', 'gen_pcie_root_port.c', + 'gen_pcie_upstream_port.c', 'gen_pcie_downstream_port.c')) pci_ss.add(when: 'CONFIG_PCIE_PCI_BRIDGE', if_true: files('pcie_pci_bridge.c')) pci_ss.add(when: 'CONFIG_PXB', if_true: files('pci_expander_bridge.c'), if_false: files('pci_expander_bridge_stubs.c'))
include/hw/pci/pci.h +2 -0
@@ -121,6 +121,8 @@ extern bool pci_available;#define PCI_DEVICE_ID_REDHAT_ACPI_ERST 0x0012#define PCI_DEVICE_ID_REDHAT_UFS 0x0013#define PCI_DEVICE_ID_REDHAT_RISCV_IOMMU 0x0014 +#define PCI_DEVICE_ID_REDHAT_PCIE_USP 0x0015 +#define PCI_DEVICE_ID_REDHAT_PCIE_DSP 0x0016#define PCI_DEVICE_ID_REDHAT_QXL 0x0100#define FMT_PCIBUS PRIx64
tests/functional/x86_64/meson.build +1 -0
@@ -43,6 +43,7 @@ tests_x86_64_system_thorough = [ 'multiprocess', 'netdev_ethtool', 'nvme_migration', + 'pcie_switch', 'replay', 'reverse_debug', 'tuxrun',
tests/functional/x86_64/test_pcie_switch.py new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 2/4 hw/pci-bridge: Make speed and width for PCIe ports non-experimental
In reply to David VrabelFor guests with passthrough PCIe devices, in order to make the guest PCIe topology have the same apparent bandwidth as the underlying hardware we need to be able to set the capabilities (speed and width) of the virtual root, upstream, and downstream ports.
Promote the existing experimental x-speed and x-width options to non-experimental speed and width.
--- hw/pci-bridge/gen_pcie_downstream_port.c | 4 ++-- hw/pci-bridge/gen_pcie_root_port.c | 4 ++-- hw/pci-bridge/gen_pcie_upstream_port.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
hw/pci-bridge/gen_pcie_downstream_port.c +2 -2
@@ -159,9 +159,9 @@ static const VMStateDescription vmstate_gen_pcie_downstream_port = {static const Property gen_pcie_downstream_port_props[] = {DEFINE_PROP_BIT (COMPAT_PROP_PCP , PCIDevice, cap_present,QEMU_PCIE_SLTCAP_PCP_BITNR , true), -DEFINE_PROP_PCIE_LINK_SPEED ("x-speed" , PCIESlot, +DEFINE_PROP_PCIE_LINK_SPEED ("speed" , PCIESlot, speed,PCIE_LINK_SPEED_64 ), -DEFINE_PROP_PCIE_LINK_WIDTH ("x-width" , PCIESlot, +DEFINE_PROP_PCIE_LINK_WIDTH ("width" , PCIESlot, width,PCIE_LINK_WIDTH_32 ), };
hw/pci-bridge/gen_pcie_root_port.c +2 -2
@@ -129,9 +129,9 @@ static const Property gen_rp_props[] = { res_reserve.mem_pref_32, -1),DEFINE_PROP_SIZE ("pref64-reserve" , GenPCIERootPort, res_reserve.mem_pref_64,-1 ), -DEFINE_PROP_PCIE_LINK_SPEED ("x-speed" , PCIESlot, +DEFINE_PROP_PCIE_LINK_SPEED ("speed" , PCIESlot, speed,PCIE_LINK_SPEED_16 ), -DEFINE_PROP_PCIE_LINK_WIDTH ("x-width" , PCIESlot, +DEFINE_PROP_PCIE_LINK_WIDTH ("width" , PCIESlot, width, PCIE_LINK_WIDTH_32), };
hw/pci-bridge/gen_pcie_upstream_port.c +2 -2
@@ -134,9 +134,9 @@ static const VMStateDescription vmstate_gen_pcie_upstream_port = { };static const Property gen_pcie_upstream_port_props[] = { -DEFINE_PROP_PCIE_LINK_SPEED ("x-speed" , GenPCIEUpstreamPort, +DEFINE_PROP_PCIE_LINK_SPEED ("speed" , GenPCIEUpstreamPort, speed,PCIE_LINK_SPEED_64 ), -DEFINE_PROP_PCIE_LINK_WIDTH ("x-width" , GenPCIEUpstreamPort, +DEFINE_PROP_PCIE_LINK_WIDTH ("width" , GenPCIEUpstreamPort, width,PCIE_LINK_WIDTH_32 ), };
Signature · 1 line
2.43.0
PATCH v3 3/4 hw/pci-bridge: Default the generic PCIe root port as Gen6 capable
In reply to David VrabelAs part of making virtual PCIe topology having the same apparent bandwidth as a the underlying hardware, make the generic PCIe root port support maximum speed so by default it doesn't appear to be a bottleneck.
This also makes the three generic PCIe ports (root, upstream, and downstream) have the same link speed/width capabilities.
--- hw/core/machine.c | 1 + hw/pci-bridge/gen_pcie_root_port.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
hw/core/machine.c +1 -0
@@ -43,6 +43,7 @@GlobalProperty hw_compat_11_1[] = { {"sysbus-ehci-usb" ,"x-migrate-fetch-addr-64bit" ,"off" }, {"pci-ehci-usb" ,"x-migrate-fetch-addr-64bit" ,"off" }, + {"pcie-root-port" ,"speed" ,"16" }, };const size_t hw_compat_11_1_len =G_N_ELEMENTS (hw_compat_11_1);
hw/pci-bridge/gen_pcie_root_port.c +1 -1
@@ -130,7 +130,7 @@ static const Property gen_rp_props[] = {DEFINE_PROP_SIZE ("pref64-reserve" , GenPCIERootPort, res_reserve.mem_pref_64,-1 ),DEFINE_PROP_PCIE_LINK_SPEED ("speed" , PCIESlot, - speed,PCIE_LINK_SPEED_16 ), + speed,PCIE_LINK_SPEED_64 ),DEFINE_PROP_PCIE_LINK_WIDTH ("width" , PCIESlot, width, PCIE_LINK_WIDTH_32), };
Signature · 1 line
2.43.0
PATCH v3 4/4 hw/pci: Ensure PCIe link speed/width is consistent between both ends
In reply to David VrabelIf an emulated PCIe endpoint has higher link speed or width capabilities than the upstream port it is connected to, ensure that the link status on both ends is consistent by using the link status computed for the upstream port.
If the PCIe endpoint is a passthrough device, the link status continues to match the real status and consistent values requires the emulated upstream port to be at least as capable as the endpoint.
--- hw/pci/pcie.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
hw/pci/pcie.c +22 -4
@@ -169,18 +169,36 @@ void pcie_cap_fill_link_ep_usp(PCIDevice *dev, PCIExpLinkWidth width, PCIExpLinkSpeed speed, bool flitmode) { uint8_t *exp_cap = dev->config + dev->exp.exp_cap; +PCIDevice *upstream;/* -* For an end point or USP need to set the current status as well -* as the capabilities. +* For an endpoint or USP, set Link Status as well as capabilities. +* Provisionally use this device's width/speed, then adopt the upstream +* port's values so both ends of an emulated link agree. The upstream +* LNKSTA is already clamped to the negotiated link capabilities via +* pcie_sync_bridge_lnk(). */ -pci_long_test_and_clear_mask (exp_cap +PCI_EXP_LNKSTA , +pci_word_test_and_clear_mask (exp_cap +PCI_EXP_LNKSTA ,PCI_EXP_LNKSTA_CLS |PCI_EXP_LNKSTA_NLW ); -pci_long_test_and_set_mask (exp_cap +PCI_EXP_LNKSTA , +pci_word_test_and_set_mask (exp_cap +PCI_EXP_LNKSTA ,QEMU_PCI_EXP_LNKSTA_NLW (width) |QEMU_PCI_EXP_LNKSTA_CLS (speed));pcie_cap_fill_lnk (exp_cap, width, speed, flitmode); + + upstream =pci_bridge_get_device (pci_get_bus (dev)); +if (upstream &&pci_is_express_downstream_port (upstream)) { +uint8_t *up_exp = upstream->config + upstream->exp.exp_cap; +uint16_t lnksta; + +pcie_sync_bridge_lnk (upstream); + lnksta =pci_get_word (up_exp +PCI_EXP_LNKSTA ); +pci_word_test_and_clear_mask (exp_cap +PCI_EXP_LNKSTA , +PCI_EXP_LNKSTA_CLS |PCI_EXP_LNKSTA_NLW ); +pci_word_test_and_set_mask (exp_cap +PCI_EXP_LNKSTA , + lnksta & (PCI_EXP_LNKSTA_CLS | +PCI_EXP_LNKSTA_NLW )); + } }static void pcie_cap_fill_slot_lnk (PCIDevice *dev)
Signature · 1 line
2.43.0
ping?
Quoted 21 lines · David Vrabel
When a VM has been assigned a number of PCIe devices behind a common PCIe switch and the VM intends to use peer-to-peer (P2P) transfers between the devices, it is useful to be able to more report the apparent PCIe bandwidth so it reflects the real handwidth of the underlying hardware. This allows software in the VM (such as NCCL[1]) to make better decisions about whether to use P2P or not.
The existing emulated PCIe switch is a vintage TI XIO3130 and only support 2.5 GT/s and x1, so we introduces new generic upstream and downstream port devices (pcie-upstream-port and pcie-downstream-port), inspired by the existing generic pcie-root-port device.
The existing experimental "x-speed" and "x-width" options are made non-experimental, allowing the pcie-root-port and pcie-downstream-port capabilities to be configured to match the hardware.
The pcie-root-port device is updated to default to a Gen6 capable device.
Finally, where a root or upstream port's bandwidth has been limited, we also ensure the other end of the link reports the same link status.
Quoted 24 lines · David Vrabel
ping?
Quoted 21 lines · David Vrabel
When a VM has been assigned a number of PCIe devices behind a common PCIe switch and the VM intends to use peer-to-peer (P2P) transfers between the devices, it is useful to be able to more report the apparent PCIe bandwidth so it reflects the real handwidth of the underlying hardware. This allows software in the VM (such as NCCL[1]) to make better decisions about whether to use P2P or not.
The existing emulated PCIe switch is a vintage TI XIO3130 and only support 2.5 GT/s and x1, so we introduces new generic upstream and downstream port devices (pcie-upstream-port and pcie-downstream-port), inspired by the existing generic pcie-root-port device.
The existing experimental "x-speed" and "x-width" options are made non-experimental, allowing the pcie-root-port and pcie-downstream-port capabilities to be configured to match the hardware.
The pcie-root-port device is updated to default to a Gen6 capable device.
Finally, where a root or upstream port's bandwidth has been limited, we also ensure the other end of the link reports the same link status.
will merge, thanks!
Hi Michael,
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · David Vrabel
ping?
On 28/09/2026 16:24, David Vrabel wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
When a VM has been assigned a number of PCIe devices behind a common PCIe switch and the VM intends to use peer-to-peer (P2P) transfers between the devices, it is useful to be able to more report the apparent PCIe bandwidth so it reflects the real handwidth of the underlying hardware. This allows software in the VM (such as NCCL[1]) to make better decisions about whether to use P2P or not.
will merge, thanks!
I appreciate you might be swamped by a pile of AI-generated security work, or perhaps I'm missing some QEMU-specific process (merge window?), but can you estimate when this might be merged, thanks?
If it helps I can prepare a gitlab pull request.
David
Quoted 17 lines · David Vrabel
Hi Michael,
Quoted 5 lines · Michael S. Tsirkin
Quoted 3 lines · David Vrabel
ping?
On 28/09/2026 16:24, David Vrabel wrote:Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
When a VM has been assigned a number of PCIe devices behind a common PCIe switch and the VM intends to use peer-to-peer (P2P) transfers between the devices, it is useful to be able to more report the apparent PCIe bandwidth so it reflects the real handwidth of the underlying hardware. This allows software in the VM (such as NCCL[1]) to make better decisions about whether to use P2P or not.
will merge, thanks!
Quoted 7 lines
I appreciate you might be swamped by a pile of AI-generated security work, or perhaps I'm missing some QEMU-specific process (merge window?), but can you estimate when this might be merged, thanks?
If it helps I can prepare a gitlab pull request.
David
This week, sorry about the delay.
Signature · 1 line
MST