Skip to contentSkip to conversation

hw/pci: Add acs-ctrl property for root and downstream ports

PATCH New Tushar Dave 1 participant 1 message Nov 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/pci: Add acs-ctrl property for root and downstream ports

For P2P to work between devices assigned to a guest, the ACS Control register that the guest sees must match the ACS routing that the host administrator configured. A mismatch can prevent P2P between the devices from working as expected.

Add an acs-ctrl property to the generic PCIe root port and the generic PCIe downstream port, so that the administrator can configure the ACS Control register.

---
This replaces my earlier ACS series:
https://lore.kernel.org/qemu-devel/[email protected]/

The patch that made ACS Control read-only for the guest is dropped, along with the patch that adds acs-ctrl for x3130/xio3130. Instead, acs-ctrl is added for the generic root port and the generic downstream port.

 hw/pci-bridge/gen_pcie_downstream_port.c | 11 ++++++++++-
 hw/pci-bridge/pcie_root_port.c           | 13 +++++++++++--
 hw/pci/pcie.c                            | 17 ++++++++++++++---
 include/hw/pci/pcie.h                    |  5 +++--
 include/hw/pci/pcie_port.h               |  1 +
 5 files changed, 39 insertions(+), 8 deletions(-)
hw/pci-bridge/gen_pcie_downstream_port.c +10 -1
index 3084a208a8..e5d1f642ba 100644
@@ -72,10 +72,12 @@ static void gen_downstream_write_config(PCIDevice *d, uint32_t address,
 static void gen_downstream_reset(DeviceState *qdev)
 {
     PCIDevice *d = PCI_DEVICE(qdev);
+    PCIEPort *p = PCIE_PORT(d);
 
     pcie_cap_deverr_reset(d);
     pcie_cap_slot_reset(d);
     pcie_cap_arifwd_reset(d);
+    pcie_acs_reset(d, p->acs_ctrl);
     pci_bridge_reset(qdev);
 }
 
@@ -115,10 +117,16 @@ static void gen_downstream_realize(PCIDevice *d, Error **errp)
         goto err_chassis;
     }
 
-    pcie_acs_init(d, GEN_PCIE_DOWNSTREAM_PORT_ACS_OFFSET);
+    rc = pcie_acs_init(d, GEN_PCIE_DOWNSTREAM_PORT_ACS_OFFSET, p->acs_ctrl,
+                       errp);
+    if (rc < 0) {
+        goto err_aer;
+    }
 
     return;
 
+err_aer:
+    pcie_aer_exit(d);
 err_chassis:
     pcie_chassis_del_slot(s);
 err_cap:
@@ -163,6 +171,7 @@ static const Property gen_pcie_downstream_port_props[] = {
                                 speed, PCIE_LINK_SPEED_64),
     DEFINE_PROP_PCIE_LINK_WIDTH("width", PCIESlot,
                                 width, PCIE_LINK_WIDTH_32),
+    DEFINE_PROP_UINT16("acs-ctrl", PCIEPort, acs_ctrl, 0),
 };
 
 static void gen_pcie_downstream_port_class_init(ObjectClass *klass,
hw/pci-bridge/pcie_root_port.c +11 -2
index 7c3e78010b..e7cce8c0ee 100644
@@ -46,6 +46,7 @@ static void rp_write_config(PCIDevice *d, uint32_t address,
 static void rp_reset_hold(Object *obj, ResetType type)
 {
     PCIDevice *d = PCI_DEVICE(obj);
+    PCIEPort *p = PCIE_PORT(d);
     DeviceState *qdev = DEVICE(obj);
 
     rp_aer_vector_update(d);
@@ -53,7 +54,7 @@ static void rp_reset_hold(Object *obj, ResetType type)
     pcie_cap_deverr_reset(d);
     pcie_cap_slot_reset(d);
     pcie_cap_arifwd_reset(d);
-    pcie_acs_reset(d);
+    pcie_acs_reset(d, p->acs_ctrl);
     pcie_aer_root_reset(d);
     pci_bridge_reset(qdev);
     pci_bridge_disable_base_limit(d);
@@ -118,11 +119,18 @@ static void rp_realize(PCIDevice *d, Error **errp)
     rp_aer_vector_update(d);
 
     if (rpc->acs_offset) {
-        pcie_acs_init(d, rpc->acs_offset);
+        rc = pcie_acs_init(d, rpc->acs_offset, p->acs_ctrl, errp);
+        if (rc < 0) {
+            goto err;
+        }
+    } else if (p->acs_ctrl) {
+        error_setg(errp, "acs-ctrl is not supported by this root port");
+        goto err;
     }
     return;
 
 err:
+    pcie_aer_exit(d);
     pcie_chassis_del_slot(s);
 err_pcie_cap:
     pcie_cap_exit(d);
@@ -151,6 +159,7 @@ static void rp_exit(PCIDevice *d)
 static const Property rp_props[] = {
     DEFINE_PROP_BIT(COMPAT_PROP_PCP, PCIDevice, cap_present,
                     QEMU_PCIE_SLTCAP_PCP_BITNR, true),
+    DEFINE_PROP_UINT16("acs-ctrl", PCIEPort, acs_ctrl, 0),
 };
 
 static void rp_instance_post_init(Object *obj)
hw/pci/pcie.c +14 -3
index bd00305ecc..57dd873eb9 100644
@@ -1278,7 +1278,8 @@ void pcie_ats_init(PCIDevice *dev, uint16_t offset, bool aligned)
 }
 
 /* ACS (Access Control Services) */
-void pcie_acs_init(PCIDevice *dev, uint16_t offset)
+int pcie_acs_init(PCIDevice *dev, uint16_t offset, uint16_t ctrl_bits,
+                  Error **errp)
 {
     bool is_downstream = pci_is_express_downstream_port(dev);
     uint16_t cap_bits = 0;
@@ -1302,16 +1303,26 @@ void pcie_acs_init(PCIDevice *dev, uint16_t offset)
          */
         cap_bits = PCI_ACS_SV | PCI_ACS_TB | PCI_ACS_RR |
             PCI_ACS_CR | PCI_ACS_UF | PCI_ACS_DT;
+
+        if (ctrl_bits & ~cap_bits) {
+            error_setg(errp,
+                       "acs-ctrl 0x%hx has bits not supported by this port "
+                       "(supported: 0x%hx)", ctrl_bits, cap_bits);
+            return -EINVAL;
+        }
     }
 
     pci_set_word(dev->config + offset + PCI_ACS_CAP, cap_bits);
     pci_set_word(dev->wmask + offset + PCI_ACS_CTRL, cap_bits);
+    pci_set_word(dev->config + offset + PCI_ACS_CTRL, ctrl_bits);
+
+    return 0;
 }
 
-void pcie_acs_reset(PCIDevice *dev)
+void pcie_acs_reset(PCIDevice *dev, uint16_t val)
 {
     if (dev->exp.acs_cap) {
-        pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, 0);
+        pci_set_word(dev->config + dev->exp.acs_cap + PCI_ACS_CTRL, val);
     }
 }
 
include/hw/pci/pcie.h +3 -2
index ec25e7a7de..678ad73f7f 100644
@@ -137,8 +137,9 @@ bool pcie_insert_capability(PCIDevice *dev, uint16_t cap_id, uint8_t cap_ver,
                             uint16_t offset, uint16_t size);
 void pcie_sync_bridge_lnk(PCIDevice *dev);
 
-void pcie_acs_init(PCIDevice *dev, uint16_t offset);
-void pcie_acs_reset(PCIDevice *dev);
+int pcie_acs_init(PCIDevice *dev, uint16_t offset, uint16_t ctrl_bits,
+                  Error **errp);
+void pcie_acs_reset(PCIDevice *dev, uint16_t val);
 
 void pcie_ari_init(PCIDevice *dev, uint16_t offset);
 void pcie_dev_ser_num_init(PCIDevice *dev, uint16_t offset, uint64_t ser_num);
include/hw/pci/pcie_port.h +1 -0
index b28af067a6..2ad9a563d9 100644
@@ -36,6 +36,7 @@ struct PCIEPort {
 
     /* pci express switch port */
     uint8_t     port;
+    uint16_t    acs_ctrl;
 };
 
 void pcie_port_init_reg(PCIDevice *d);
Signature · 1 line
2.34.1