Skip to contentSkip to conversation

hw/pci-bridge: Configurable apparent bandwidth for the PCIe topology

PATCH Superseded David Vrabel 2 participants 7 messages Sep 8 – Sep 9 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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.

Some open questions:

- The ergonomics of the speed/width options doesn't seem great as they're enum properties. In particular, 2.5 GT/s is requested with the string "2_5". Should we introduce the ability to have floating-point properties? Or should the units for "speed" be MT/s (renaming the option to "speed_mts") instead and both properties should be integers?

- PCIe 6.0 removed support for the x32 link width. Should the generic root and downstream ports default to 64 GT/s and x16 instead?

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 +-
 hw/pci-bridge/gen_pcie_downstream_port.c    | 206 ++++++++++++++++++++
 hw/pci-bridge/gen_pcie_root_port.c          |   6 +-
 hw/pci-bridge/gen_pcie_upstream_port.c      | 182 +++++++++++++++++
 hw/pci-bridge/meson.build                   |   3 +-
 hw/pci/pcie.c                               |  26 ++-
 tests/functional/x86_64/meson.build         |   1 +
 tests/functional/x86_64/test_pcie_switch.py |  98 ++++++++++
 8 files changed, 517 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
#1 lore raw

PATCH v2 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 device IDs are placeholders and will need to be properly allocated from the 0x1b36 (Red Hat, Inc.) vendor ID space.

---
 docs/pcie.txt                               |   6 +-
 hw/pci-bridge/gen_pcie_downstream_port.c    | 206 ++++++++++++++++++++
 hw/pci-bridge/gen_pcie_upstream_port.c      | 182 +++++++++++++++++
 hw/pci-bridge/meson.build                   |   3 +-
 tests/functional/x86_64/meson.build         |   1 +
 tests/functional/x86_64/test_pcie_switch.py |  98 ++++++++++
 6 files changed, 492 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
index df49178311..f12a9daaad 100644
@@ -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:
hw/pci-bridge/gen_pcie_downstream_port.c new file +206 -0

206 lines not on this page · raw message

hw/pci-bridge/gen_pcie_upstream_port.c new file +182 -0

182 lines not on this page · raw message

hw/pci-bridge/meson.build +2 -1
index 2e0eb0d233..fd0ad188bc 100644
@@ -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'))
tests/functional/x86_64/meson.build +1 -0
index 27b31f2e96..8a83b3188e 100644
@@ -39,6 +39,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
#2 lore raw

Re: [PATCH v2 1/4] hw/pci-bridge: Add generic PCIe switch upstream and downstream ports

Quoted 9 lines · David Vrabel

The TI XIO3130 PCIe switch is a vintage Gen1 device, so we add generic PCIe upstream and downstream port devices that are Gen6.

[...]

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.

Hi Gerd,

These two new devices will require PCI Device IDs to be allocated. I have suggested (and used in this patch):

1b36:0015 QEMU PCIe Upstream Switch Port
1b36:0016 QEMU PCIe Downstream Switch Port

Matching the vendor ID and name with the existing QEMU PCIe Root Port (1b36:000c) device.

David

#3 lore raw

Re: [PATCH v2 1/4] hw/pci-bridge: Add generic PCIe switch upstream and downstream ports

In reply to David Vrabel

Hi,

hw/pci-bridge/gen_pcie_downstream_port.c line 31

Quoted 2 lines
hw/pci-bridge/gen_pcie_downstream_port.c new file +2 -0
+#define GEN_PCIE_DOWNSTREAM_PORT_VENDOR_ID       0x1b36
+#define GEN_PCIE_DOWNSTREAM_PORT_DEVICE_ID       0x0016

These #defines are maintained in include/hw/pci/pci.h, please add them there next to the existing ones.

Also make sure to update docs/specs/pci-ids.rst
when allocating a new ID from the range for qemu.

take care,
Gerd

#4 lore raw

PATCH v2 2/4 hw/pci-bridge: Make speed and width for PCIe ports non-experimental

In reply to David Vrabel

For 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
index d593846ac1..8fe71382aa 100644
@@ -163,9 +163,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
index 5434d693d9..d3ace7f0db 100644
@@ -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
index 57d7e09485..edd5bfcafc 100644
@@ -139,9 +139,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
#5 lore raw

PATCH v2 3/4 hw/pci-bridge: Default the generic PCIe root port as Gen6 capable

In reply to David Vrabel

As 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/pci-bridge/gen_pcie_root_port.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/pci-bridge/gen_pcie_root_port.c +1 -1
index d3ace7f0db..8cabeadac8 100644
@@ -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
#6 lore raw

PATCH v2 4/4 hw/pci: Ensure PCIe link speed/width is consistent between both ends

In reply to David Vrabel

If 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
index 4622c75e48..66d778413d 100644
@@ -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