Skip to contentSkip to conversation

vPCI capabilities filtering

PATCH Superseded Stewart Hildebrand 2 participants 12 messages Aug 22, 2023 – Aug 24, 2023

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v3 cover vPCI capabilities filtering

This small series enables vPCI to filter which PCI capabilities we expose to a domU. This series adds vPCI register handlers within xen/drivers/vpci/header.c:init_bars(), along with some supporting functions.

Note there are minor rebase conflicts with the in-progress vPCI series [1]. These conflicts fall into the category of functions and code being added adjacent to one another, so are easily resolved. I did not identify any dependency on the vPCI locking work, and the two series deal with different aspects of emulating the PCI header.

Future work may involve adding handlers for more registers in the vPCI header, such as VID/DID, etc. Future work may also involve exposing additional capabilities to the guest for broader device/driver support.

v2->v3:
* drop RFC "xen/vpci: header: avoid cast for value passed to vpci_read_val"
* minor misra C violation fixup in preparatory patch
* switch to pci_sbdf_t in preparatory patch
* introduce status handler

v1->v2:
* squash helper functions into the patch where they are used to avoid transient dead code situation
* add new RFC patch, possibly throwaway, to get an idea of what it would look like to get rid of the (void *)(uintptr_t) cast by introducing a new memory allocation

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg01281.html

Stewart Hildebrand (4):
xen/pci: address a violation of MISRA C:2012 Rule 8.3 xen/pci: convert pci_find_*cap* to pci_sbdf_t xen/vpci: header: filter PCI capabilities xen/vpci: header: status register handler

 xen/arch/x86/msi.c                         | 47 +++--------
 xen/drivers/char/ehci-dbgp.c               |  3 +-
 xen/drivers/passthrough/amd/iommu_detect.c |  2 +-
 xen/drivers/passthrough/ats.c              |  4 +-
 xen/drivers/passthrough/ats.h              |  6 +-
 xen/drivers/passthrough/msi.c              |  6 +-
 xen/drivers/passthrough/pci.c              | 20 ++---
 xen/drivers/passthrough/vtd/quirks.c       | 10 +--
 xen/drivers/passthrough/vtd/x86/ats.c      |  3 +-
 xen/drivers/pci/pci.c                      | 50 +++++++-----
 xen/drivers/vpci/header.c                  | 93 ++++++++++++++++++++++
 xen/drivers/vpci/msi.c                     |  4 +-
 xen/drivers/vpci/msix.c                    |  4 +-
 xen/drivers/vpci/vpci.c                    | 53 +++++++++---
 xen/include/xen/pci.h                      | 11 ++-
 xen/include/xen/vpci.h                     | 14 ++++
 16 files changed, 221 insertions(+), 109 deletions(-)

base-commit: 3fae7c56b313a12288a05e0a8c14c47bfd4dc40e

Signature · 1 line
2.41.0
#1 lore raw

PATCH v3 1/4 xen/pci: address a violation of MISRA C:2012 Rule 8.3

Make the paramater names of the prototype match the definition. No functional change.

---
v2->v3:
* new patch

---
 xen/drivers/pci/pci.c | 2 +-
 xen/include/xen/pci.h | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
xen/drivers/pci/pci.c +1 -1
index e411876a1518..c73a8c4124af 100644
@@ -80,7 +80,7 @@ int pci_find_ext_capability(int seg, int bus, int devfn, int cap)
 /**
  * pci_find_next_ext_capability - Find another extended capability
  * @seg/@bus/@devfn: PCI device to query
- * @pos: starting position
+ * @start: starting position
  * @cap: capability code
  *
  * Returns the address of the requested extended capability structure
xen/include/xen/pci.h +2 -1
index 5975ca2f3032..a8c8c4ff11c3 100644
@@ -196,7 +196,8 @@ int pci_mmcfg_write(unsigned int seg, unsigned int bus,
 int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap);
 int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap);
 int pci_find_ext_capability(int seg, int bus, int devfn, int cap);
-int pci_find_next_ext_capability(int seg, int bus, int devfn, int pos, int cap);
+int pci_find_next_ext_capability(int seg, int bus, int devfn, int start,
+                                 int cap);
 const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
                       unsigned int *dev, unsigned int *func);
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
Signature · 1 line
2.41.0
#3 lore raw

PATCH v3 2/4 xen/pci: convert pci_find_*cap* to pci_sbdf_t

In reply to Stewart Hildebrand

Convert pci_find_*cap* functions and call sites to pci_sbdf_t, and remove some now unused local variables. No functional change.

---
I built with EXTRA_CFLAGS_XEN_CORE="-Wunused-but-set-variable" (and unfortunately -Wno-error=unused-but-set-variable too) to identify locations of unneeded local variables as a result of the change to pci_sbdf_t.

v2->v3:
* new patch

---
 xen/arch/x86/msi.c                         | 47 ++++++----------------
 xen/drivers/char/ehci-dbgp.c               |  3 +-
 xen/drivers/passthrough/amd/iommu_detect.c |  2 +-
 xen/drivers/passthrough/ats.c              |  4 +-
 xen/drivers/passthrough/ats.h              |  6 ++-
 xen/drivers/passthrough/msi.c              |  6 +--
 xen/drivers/passthrough/pci.c              | 20 ++++-----
 xen/drivers/passthrough/vtd/quirks.c       | 10 ++---
 xen/drivers/passthrough/vtd/x86/ats.c      |  3 +-
 xen/drivers/pci/pci.c                      | 28 ++++++-------
 xen/drivers/vpci/msi.c                     |  4 +-
 xen/drivers/vpci/msix.c                    |  4 +-
 xen/include/xen/pci.h                      |  9 ++---
 13 files changed, 53 insertions(+), 93 deletions(-)
xen/arch/x86/msi.c +12 -35
index d0bf63df1def..13a1f0319a8a 100644
@@ -283,7 +283,7 @@ static void msi_set_enable(struct pci_dev *dev, int enable)
     u8 slot = PCI_SLOT(dev->devfn);
     u8 func = PCI_FUNC(dev->devfn);
 
-    pos = pci_find_cap_offset(seg, bus, slot, func, PCI_CAP_ID_MSI);
+    pos = pci_find_cap_offset(dev->sbdf, PCI_CAP_ID_MSI);
     if ( pos )
         __msi_set_enable(seg, bus, slot, func, pos, enable);
 }
@@ -291,12 +291,9 @@ static void msi_set_enable(struct pci_dev *dev, int enable)
 static void msix_set_enable(struct pci_dev *dev, int enable)
 {
     int pos;
-    u16 control, seg = dev->seg;
-    u8 bus = dev->bus;
-    u8 slot = PCI_SLOT(dev->devfn);
-    u8 func = PCI_FUNC(dev->devfn);
+    u16 control;
 
-    pos = pci_find_cap_offset(seg, bus, slot, func, PCI_CAP_ID_MSIX);
+    pos = pci_find_cap_offset(dev->sbdf, PCI_CAP_ID_MSIX);
     if ( pos )
     {
         control = pci_conf_read16(dev->sbdf, msix_control_reg(pos));
@@ -318,17 +315,12 @@ static bool msi_set_mask_bit(struct irq_desc *desc, bool host, bool guest)
 {
     struct msi_desc *entry = desc->msi_desc;
     struct pci_dev *pdev;
-    u16 seg, control;
-    u8 bus, slot, func;
+    u16 control;
     bool flag = host || guest, maskall;
 
     ASSERT(spin_is_locked(&desc->lock));
     BUG_ON(!entry || !entry->dev);
     pdev = entry->dev;
-    seg = pdev->seg;
-    bus = pdev->bus;
-    slot = PCI_SLOT(pdev->devfn);
-    func = PCI_FUNC(pdev->devfn);
     switch ( entry->msi_attrib.type )
     {
     case PCI_CAP_ID_MSI:
@@ -608,13 +600,10 @@ static int msi_capability_init(struct pci_dev *dev,
     struct msi_desc *entry;
     int pos;
     unsigned int i, mpos;
-    u16 control, seg = dev->seg;
-    u8 bus = dev->bus;
-    u8 slot = PCI_SLOT(dev->devfn);
-    u8 func = PCI_FUNC(dev->devfn);
+    u16 control;
 
     ASSERT(pcidevs_locked());
-    pos = pci_find_cap_offset(seg, bus, slot, func, PCI_CAP_ID_MSI);
+    pos = pci_find_cap_offset(dev->sbdf, PCI_CAP_ID_MSI);
     if ( !pos )
         return -ENODEV;
     control = pci_conf_read16(dev->sbdf, msi_control_reg(pos));
@@ -685,8 +674,8 @@ static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf)
     {
         struct pci_dev *pdev = pci_get_pdev(NULL,
                                             PCI_SBDF(seg, bus, slot, func));
-        unsigned int pos = pci_find_ext_capability(seg, bus,
-                                                   PCI_DEVFN(slot, func),
+        unsigned int pos = pci_find_ext_capability(PCI_SBDF(seg, bus, slot,
+                                                            func),
                                                    PCI_EXT_CAP_ID_SRIOV);
         uint16_t ctrl = pci_conf_read16(PCI_SBDF(seg, bus, slot, func),
                                         pos + PCI_SRIOV_CTRL);
@@ -777,8 +766,7 @@ static int msix_capability_init(struct pci_dev *dev,
     u8 slot = PCI_SLOT(dev->devfn);
     u8 func = PCI_FUNC(dev->devfn);
     bool maskall = msix->host_maskall, zap_on_error = false;
-    unsigned int pos = pci_find_cap_offset(seg, bus, slot, func,
-                                           PCI_CAP_ID_MSIX);
+    unsigned int pos = pci_find_cap_offset(dev->sbdf, PCI_CAP_ID_MSIX);
 
     if ( !pos )
         return -ENODEV;
@@ -1102,12 +1090,7 @@ static void _pci_cleanup_msix(struct arch_msix *msix)
 static void __pci_disable_msix(struct msi_desc *entry)
 {
     struct pci_dev *dev = entry->dev;
-    u16 seg = dev->seg;
-    u8 bus = dev->bus;
-    u8 slot = PCI_SLOT(dev->devfn);
-    u8 func = PCI_FUNC(dev->devfn);
-    unsigned int pos = pci_find_cap_offset(seg, bus, slot, func,
-                                           PCI_CAP_ID_MSIX);
+    unsigned int pos = pci_find_cap_offset(dev->sbdf, PCI_CAP_ID_MSIX);
     u16 control = pci_conf_read16(dev->sbdf,
                                   msix_control_reg(entry->msi_attrib.pos));
     bool maskall = dev->msix->host_maskall;
@@ -1211,8 +1194,7 @@ void pci_cleanup_msi(struct pci_dev *pdev)
 
 int pci_reset_msix_state(struct pci_dev *pdev)
 {
-    unsigned int pos = pci_find_cap_offset(pdev->seg, pdev->bus, pdev->sbdf.dev,
-                                           pdev->sbdf.fn, PCI_CAP_ID_MSIX);
+    unsigned int pos = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_MSIX);
 
     ASSERT(pos);
     /*
@@ -1234,10 +1216,6 @@ int pci_reset_msix_state(struct pci_dev *pdev)
 int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg,
                                  unsigned int size, uint32_t *data)
 {
-    u16 seg = pdev->seg;
-    u8 bus = pdev->bus;
-    u8 slot = PCI_SLOT(pdev->devfn);
-    u8 func = PCI_FUNC(pdev->devfn);
     struct msi_desc *entry;
     unsigned int pos;
 
@@ -1245,8 +1223,7 @@ int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg,
     {
         entry = find_msi_entry(pdev, -1, PCI_CAP_ID_MSIX);
         pos = entry ? entry->msi_attrib.pos
-                    : pci_find_cap_offset(seg, bus, slot, func,
-                                          PCI_CAP_ID_MSIX);
+                    : pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_MSIX);
         ASSERT(pos);
 
         if ( reg >= pos && reg < msix_pba_offset_reg(pos) + 4 )
xen/drivers/char/ehci-dbgp.c +2 -1
index 72be4d9cc970..00cbdd5454dd 100644
@@ -687,7 +687,8 @@ static unsigned int __init __find_dbgp(u8 bus, u8 slot, u8 func)
     if ( (class >> 8) != PCI_CLASS_SERIAL_USB_EHCI )
         return 0;
 
-    return pci_find_cap_offset(0, bus, slot, func, PCI_CAP_ID_EHCI_DEBUG);
+    return pci_find_cap_offset(PCI_SBDF(0, bus, slot, func),
+                               PCI_CAP_ID_EHCI_DEBUG);
 }
 
 static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp,
xen/drivers/passthrough/amd/iommu_detect.c +1 -1
index 2317fa6a7d8d..cede44e6518f 100644
@@ -27,7 +27,7 @@ static int __init get_iommu_msi_capabilities(
 {
     int pos;
 
-    pos = pci_find_cap_offset(seg, bus, dev, func, PCI_CAP_ID_MSI);
+    pos = pci_find_cap_offset(PCI_SBDF(seg, bus, dev, func), PCI_CAP_ID_MSI);
 
     if ( !pos )
         return -ENODEV;
xen/drivers/passthrough/ats.c +1 -3
index 7f7b16dc490c..eec6eec00043 100644
@@ -24,11 +24,9 @@ boolean_param("ats", ats_enabled);
 int enable_ats_device(struct pci_dev *pdev, struct list_head *ats_list)
 {
     u32 value;
-    u16 seg = pdev->seg;
-    u8 bus = pdev->bus, devfn = pdev->devfn;
     int pos;
 
-    pos = pci_find_ext_capability(seg, bus, devfn, PCI_EXT_CAP_ID_ATS);
+    pos = pci_find_ext_capability(pdev->sbdf, PCI_EXT_CAP_ID_ATS);
     BUG_ON(!pos);
 
     if ( iommu_verbose )
xen/drivers/passthrough/ats.h +4 -2
index c202f4ecdd67..08a901187c54 100644
@@ -32,7 +32,8 @@ static inline int pci_ats_enabled(int seg, int bus, int devfn)
     u32 value;
     int pos;
 
-    pos = pci_find_ext_capability(seg, bus, devfn, PCI_EXT_CAP_ID_ATS);
+    pos = pci_find_ext_capability(PCI_SBDF(seg, bus, devfn),
+                                  PCI_EXT_CAP_ID_ATS);
     BUG_ON(!pos);
 
     value = pci_conf_read16(PCI_SBDF(seg, bus, devfn), pos + ATS_REG_CTL);
@@ -45,7 +46,8 @@ static inline int pci_ats_device(int seg, int bus, int devfn)
     if ( !ats_enabled )
         return 0;
 
-    return pci_find_ext_capability(seg, bus, devfn, PCI_EXT_CAP_ID_ATS);
+    return pci_find_ext_capability(PCI_SBDF(seg, bus, devfn),
+                                   PCI_EXT_CAP_ID_ATS);
 }
 
 #endif /* _ATS_H_ */
xen/drivers/passthrough/msi.c +2 -4
index fb78e2ebe8a4..13d904692ef8 100644
@@ -24,8 +24,7 @@ int pdev_msi_init(struct pci_dev *pdev)
 
     INIT_LIST_HEAD(&pdev->msi_list);
 
-    pos = pci_find_cap_offset(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
-                              PCI_FUNC(pdev->devfn), PCI_CAP_ID_MSI);
+    pos = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_MSI);
     if ( pos )
     {
         uint16_t ctrl = pci_conf_read16(pdev->sbdf, msi_control_reg(pos));
@@ -33,8 +32,7 @@ int pdev_msi_init(struct pci_dev *pdev)
         pdev->msi_maxvec = multi_msi_capable(ctrl);
     }
 
-    pos = pci_find_cap_offset(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
-                              PCI_FUNC(pdev->devfn), PCI_CAP_ID_MSIX);
+    pos = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_MSIX);
     if ( pos )
     {
         struct arch_msix *msix = xzalloc(struct arch_msix);
xen/drivers/passthrough/pci.c +7 -13
index 33452791a8e0..219b357efb14 100644
@@ -361,8 +361,7 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
             break;
 
         case DEV_TYPE_PCIe_ENDPOINT:
-            pos = pci_find_cap_offset(pseg->nr, bus, PCI_SLOT(devfn),
-                                      PCI_FUNC(devfn), PCI_CAP_ID_EXP);
+            pos = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_EXP);
             BUG_ON(!pos);
             cap = pci_conf_read16(pdev->sbdf, pos + PCI_EXP_DEVCAP);
             if ( cap & PCI_EXP_DEVCAP_PHANTOM )
@@ -565,13 +564,12 @@ struct pci_dev *pci_get_pdev(const struct domain *d, pci_sbdf_t sbdf)
 static void pci_enable_acs(struct pci_dev *pdev)
 {
     int pos;
-    u16 cap, ctrl, seg = pdev->seg;
-    u8 bus = pdev->bus;
+    u16 cap, ctrl;
 
     if ( !is_iommu_enabled(pdev->domain) )
         return;
 
-    pos = pci_find_ext_capability(seg, bus, pdev->devfn, PCI_EXT_CAP_ID_ACS);
+    pos = pci_find_ext_capability(pdev->sbdf, PCI_EXT_CAP_ID_ACS);
     if (!pos)
         return;
 
@@ -704,7 +702,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
 
     if ( !pdev->info.is_virtfn && !pdev->vf_rlen[0] )
     {
-        unsigned int pos = pci_find_ext_capability(seg, bus, devfn,
+        unsigned int pos = pci_find_ext_capability(pdev->sbdf,
                                                    PCI_EXT_CAP_ID_SRIOV);
         uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
 
@@ -916,7 +914,7 @@ enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn)
 {
     u16 class_device, creg;
     u8 d = PCI_SLOT(devfn), f = PCI_FUNC(devfn);
-    int pos = pci_find_cap_offset(seg, bus, d, f, PCI_CAP_ID_EXP);
+    int pos = pci_find_cap_offset(PCI_SBDF(seg, bus, devfn), PCI_CAP_ID_EXP);
 
     class_device = pci_conf_read16(PCI_SBDF(seg, bus, d, f), PCI_CLASS_DEVICE);
     switch ( class_device )
@@ -1184,10 +1182,7 @@ static int hest_match_pci(const struct acpi_hest_aer_common *p,
 static bool_t hest_match_type(const struct acpi_hest_header *hest_hdr,
                               const struct pci_dev *pdev)
 {
-    unsigned int pos = pci_find_cap_offset(pdev->seg, pdev->bus,
-                                           PCI_SLOT(pdev->devfn),
-                                           PCI_FUNC(pdev->devfn),
-                                           PCI_CAP_ID_EXP);
+    unsigned int pos = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_EXP);
     u8 pcie = MASK_EXTR(pci_conf_read16(pdev->sbdf, pos + PCI_EXP_FLAGS),
                         PCI_EXP_FLAGS_TYPE);
 
@@ -1258,8 +1253,7 @@ bool_t pcie_aer_get_firmware_first(const struct pci_dev *pdev)
 {
     struct aer_hest_parse_info info = { .pdev = pdev };
 
-    return pci_find_cap_offset(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
-                               PCI_FUNC(pdev->devfn), PCI_CAP_ID_EXP) &&
+    return pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_EXP) &&
            apei_hest_parse(aer_hest_parse, &info) >= 0 &&
            info.firmware_first;
 }
xen/drivers/passthrough/vtd/quirks.c +3 -7
index fcc8f73e8b90..e1946c268beb 100644
@@ -495,8 +495,6 @@ int me_wifi_quirk(struct domain *domain, uint8_t bus, uint8_t devfn,
 
 void pci_vtd_quirk(const struct pci_dev *pdev)
 {
-    int seg = pdev->seg;
-    int bus = pdev->bus;
     int pos;
     bool_t ff;
     u32 val, val2;
@@ -532,12 +530,10 @@ void pci_vtd_quirk(const struct pci_dev *pdev)
     /* Sandybridge-EP (Romley) */
     case 0x3c00: /* host bridge */
     case 0x3c01 ... 0x3c0b: /* root ports */
-        pos = pci_find_ext_capability(seg, bus, pdev->devfn,
-                                      PCI_EXT_CAP_ID_ERR);
+        pos = pci_find_ext_capability(pdev->sbdf, PCI_EXT_CAP_ID_ERR);
         if ( !pos )
         {
-            pos = pci_find_ext_capability(seg, bus, pdev->devfn,
-                                          PCI_EXT_CAP_ID_VNDR);
+            pos = pci_find_ext_capability(pdev->sbdf, PCI_EXT_CAP_ID_VNDR);
             while ( pos )
             {
                 val = pci_conf_read32(pdev->sbdf, pos + PCI_VNDR_HEADER);
@@ -546,7 +542,7 @@ void pci_vtd_quirk(const struct pci_dev *pdev)
                     pos += PCI_VNDR_HEADER;
                     break;
                 }
-                pos = pci_find_next_ext_capability(seg, bus, pdev->devfn, pos,
+                pos = pci_find_next_ext_capability(pdev->sbdf, pos,
                                                    PCI_EXT_CAP_ID_VNDR);
             }
             ff = 0;
xen/drivers/passthrough/vtd/x86/ats.c +1 -2
index 04d702b1d6b1..d9d93df0260f 100644
@@ -57,8 +57,7 @@ int ats_device(const struct pci_dev *pdev, const struct acpi_drhd_unit *drhd)
         return 0;
 
     ats_drhd = find_ats_dev_drhd(drhd->iommu);
-    pos = pci_find_ext_capability(pdev->seg, pdev->bus, pdev->devfn,
-                                  PCI_EXT_CAP_ID_ATS);
+    pos = pci_find_ext_capability(pdev->sbdf, PCI_EXT_CAP_ID_ATS);
 
     if ( pos && (ats_drhd == NULL) )
     {
xen/drivers/pci/pci.c +14 -14
index c73a8c4124af..3bcb74040284 100644
@@ -8,25 +8,25 @@
 #include <xen/pci.h>
 #include <xen/pci_regs.h>
 
-int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap)
+int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap)
 {
     u8 id;
     int max_cap = 48;
     u8 pos = PCI_CAPABILITY_LIST;
     u16 status;
 
-    status = pci_conf_read16(PCI_SBDF(seg, bus, dev, func), PCI_STATUS);
+    status = pci_conf_read16(sbdf, PCI_STATUS);
     if ( (status & PCI_STATUS_CAP_LIST) == 0 )
         return 0;
 
     while ( max_cap-- )
     {
-        pos = pci_conf_read8(PCI_SBDF(seg, bus, dev, func), pos);
+        pos = pci_conf_read8(sbdf, pos);
         if ( pos < 0x40 )
             break;
 
         pos &= ~3;
-        id = pci_conf_read8(PCI_SBDF(seg, bus, dev, func), pos + PCI_CAP_LIST_ID);
+        id = pci_conf_read8(sbdf, pos + PCI_CAP_LIST_ID);
 
         if ( id == 0xff )
             break;
@@ -39,19 +39,19 @@ int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap)
     return 0;
 }
 
-int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap)
+int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap)
 {
     u8 id;
     int ttl = 48;
 
     while ( ttl-- )
     {
-        pos = pci_conf_read8(PCI_SBDF(seg, bus, devfn), pos);
+        pos = pci_conf_read8(sbdf, pos);
         if ( pos < 0x40 )
             break;
 
         pos &= ~3;
-        id = pci_conf_read8(PCI_SBDF(seg, bus, devfn), pos + PCI_CAP_LIST_ID);
+        id = pci_conf_read8(sbdf, pos + PCI_CAP_LIST_ID);
 
         if ( id == 0xff )
             break;
@@ -65,21 +65,21 @@ int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap)
 
 /**
  * pci_find_ext_capability - Find an extended capability
- * @seg/@bus/@devfn: PCI device to query
+ * @sbdf: PCI device to query
  * @cap: capability code
  *
  * Returns the address of the requested extended capability structure
  * within the device's PCI configuration space or 0 if the device does
  * not support it.
  */
-int pci_find_ext_capability(int seg, int bus, int devfn, int cap)
+int pci_find_ext_capability(pci_sbdf_t sbdf, int cap)
 {
-    return pci_find_next_ext_capability(seg, bus, devfn, 0, cap);
+    return pci_find_next_ext_capability(sbdf, 0, cap);
 }
 
 /**
  * pci_find_next_ext_capability - Find another extended capability
- * @seg/@bus/@devfn: PCI device to query
+ * @sbdf: PCI device to query
  * @start: starting position
  * @cap: capability code
  *
@@ -87,13 +87,13 @@ int pci_find_ext_capability(int seg, int bus, int devfn, int cap)
  * within the device's PCI configuration space or 0 if the device does
  * not support it.
  */
-int pci_find_next_ext_capability(int seg, int bus, int devfn, int start, int cap)
+int pci_find_next_ext_capability(pci_sbdf_t sbdf, int start, int cap)
 {
     u32 header;
     int ttl = 480; /* 3840 bytes, minimum 8 bytes per capability */
     int pos = max(start, 0x100);
 
-    header = pci_conf_read32(PCI_SBDF(seg, bus, devfn), pos);
+    header = pci_conf_read32(sbdf, pos);
 
     /*
      * If we have no capabilities, this is indicated by cap ID,
@@ -109,7 +109,7 @@ int pci_find_next_ext_capability(int seg, int bus, int devfn, int start, int cap
         pos = PCI_EXT_CAP_NEXT(header);
         if ( pos < 0x100 )
             break;
-        header = pci_conf_read32(PCI_SBDF(seg, bus, devfn), pos);
+        header = pci_conf_read32(sbdf, pos);
     }
     return 0;
 }
xen/drivers/vpci/msi.c +1 -3
index 8f2b59e61aa4..78261c3f6e37 100644
@@ -184,9 +184,7 @@ static void cf_check mask_write(
 
 static int cf_check init_msi(struct pci_dev *pdev)
 {
-    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
-    unsigned int pos = pci_find_cap_offset(pdev->seg, pdev->bus, slot, func,
-                                           PCI_CAP_ID_MSI);
+    unsigned int pos = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_MSI);
     uint16_t control;
     int ret;
 
xen/drivers/vpci/msix.c +1 -3
index 25bde77586a4..626e7058c964 100644
@@ -659,14 +659,12 @@ int vpci_make_msix_hole(const struct pci_dev *pdev)
 static int cf_check init_msix(struct pci_dev *pdev)
 {
     struct domain *d = pdev->domain;
-    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
     unsigned int msix_offset, i, max_entries;
     uint16_t control;
     struct vpci_msix *msix;
     int rc;
 
-    msix_offset = pci_find_cap_offset(pdev->seg, pdev->bus, slot, func,
-                                      PCI_CAP_ID_MSIX);
+    msix_offset = pci_find_cap_offset(pdev->sbdf, PCI_CAP_ID_MSIX);
     if ( !msix_offset )
         return 0;
 
xen/include/xen/pci.h +4 -5
index a8c8c4ff11c3..8a482b15745c 100644
@@ -193,11 +193,10 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus,
                    unsigned int devfn, int reg, int len, u32 *value);
 int pci_mmcfg_write(unsigned int seg, unsigned int bus,
                     unsigned int devfn, int reg, int len, u32 value);
-int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap);
-int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap);
-int pci_find_ext_capability(int seg, int bus, int devfn, int cap);
-int pci_find_next_ext_capability(int seg, int bus, int devfn, int start,
-                                 int cap);
+int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap);
+int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap);
+int pci_find_ext_capability(pci_sbdf_t sbdf, int cap);
+int pci_find_next_ext_capability(pci_sbdf_t sbdf, int start, int cap);
 const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
                       unsigned int *dev, unsigned int *func);
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH v3 2/4] xen/pci: convert pci_find_*cap* to pci_sbdf_t

xen/arch/x86/msi.c static void msi_set_enable(struct pci_dev *dev, int enable) · line 294

Quoted 9 lines · Stewart Hildebrand
xen/arch/x86/msi.c +1 -4
@@ -291,12 +291,9 @@ static void msi_set_enable(struct pci_dev *dev, int enable)
 static void msix_set_enable(struct pci_dev *dev, int enable)
 {
     int pos;
-    u16 control, seg = dev->seg;
-    u8 bus = dev->bus;
-    u8 slot = PCI_SLOT(dev->devfn);
-    u8 func = PCI_FUNC(dev->devfn);
+    u16 control;

As you touch such places, it would be nice to also switch to uint16_t at the same time. (Similarly when you touch "pos" declarations anyway, converting them to unsigned int when they're wrongly plain int would also be nice.)

Quoted 9 lines
xen/arch/x86/msi.c +1 -2
@@ -318,17 +315,12 @@ static bool msi_set_mask_bit(struct irq_desc *desc, bool host, bool guest)
 {
     struct msi_desc *entry = desc->msi_desc;
     struct pci_dev *pdev;
-    u16 seg, control;
-    u8 bus, slot, func;
+    u16 control;
     bool flag = host || guest, maskall;
 

xen/arch/x86/msi.c static bool msi_set_mask_bit(struct irq_desc *desc, bool host, bool guest) · line 326

Quoted 10 lines
xen/arch/x86/msi.c +0 -4
     ASSERT(spin_is_locked(&desc->lock));
     BUG_ON(!entry || !entry->dev);
     pdev = entry->dev;
-    seg = pdev->seg;
-    bus = pdev->bus;
-    slot = PCI_SLOT(pdev->devfn);
-    func = PCI_FUNC(pdev->devfn);
     switch ( entry->msi_attrib.type )
     {
     case PCI_CAP_ID_MSI:

You don't further alter the function, so this looks like an unrelated (but still desirable for at least Misra) change.

xen/arch/x86/msi.c static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf) · line 676

Quoted 4 lines
xen/arch/x86/msi.c +0 -0
@@ -685,8 +674,8 @@ static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf)
     {
         struct pci_dev *pdev = pci_get_pdev(NULL,
                                             PCI_SBDF(seg, bus, slot, func));

With this, ...

xen/arch/x86/msi.c static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf) · line 679

Quoted 5 lines
xen/arch/x86/msi.c +2 -2
-        unsigned int pos = pci_find_ext_capability(seg, bus,
-                                                   PCI_DEVFN(slot, func),
+        unsigned int pos = pci_find_ext_capability(PCI_SBDF(seg, bus, slot,
+                                                            func),
                                                    PCI_EXT_CAP_ID_SRIOV);

... please use pdev->sbdf here. Albeit I guess some further re-arrangement is wanted here, to eliminate all of those PCI_SBDF() (and then also dealing with the otherwise necessary NULL check). IOW perhaps fine the way you have it, and then to be subject to a follow-on change.

Quoted 5 lines
xen/include/xen/pci.h +0 -0
@@ -193,11 +193,10 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus,
                    unsigned int devfn, int reg, int len, u32 *value);
 int pci_mmcfg_write(unsigned int seg, unsigned int bus,

xen/include/xen/pci.h int pci_mmcfg_read(unsigned int seg, unsigned int bus, · line 199

Quoted 10 lines
xen/include/xen/pci.h +4 -5
                     unsigned int devfn, int reg, int len, u32 value);
-int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap);
-int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap);
-int pci_find_ext_capability(int seg, int bus, int devfn, int cap);
-int pci_find_next_ext_capability(int seg, int bus, int devfn, int start,
-                                 int cap);
+int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap);
+int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap);
+int pci_find_ext_capability(pci_sbdf_t sbdf, int cap);
+int pci_find_next_ext_capability(pci_sbdf_t sbdf, int start, int cap);

The remaining types want converting, too: Neither fixed-width nor plain int are appropriate here. (It's a few too many type adjustments to make, for me to offer doing so while committing.)

Jan

#5 lore raw

Re: [PATCH v3 2/4] xen/pci: convert pci_find_*cap* to pci_sbdf_t

Quoted 15 lines · Jan Beulich

xen/arch/x86/msi.c static void msi_set_enable(struct pci_dev *dev, int enable) · line 294

Quoted 9 lines · Stewart Hildebrand
xen/arch/x86/msi.c +1 -4
@@ -291,12 +291,9 @@ static void msi_set_enable(struct pci_dev *dev, int enable)
 static void msix_set_enable(struct pci_dev *dev, int enable)
 {
     int pos;
-    u16 control, seg = dev->seg;
-    u8 bus = dev->bus;
-    u8 slot = PCI_SLOT(dev->devfn);
-    u8 func = PCI_FUNC(dev->devfn);
+    u16 control;

As you touch such places, it would be nice to also switch to uint16_t at the same time. (Similarly when you touch "pos" declarations anyway, converting them to unsigned int when they're wrongly plain int would also be nice.)

OK. For clarity's sake (and for my own sake), I'll bound the scope of these changes to lines/statements/declarations that are being modified anyway as a result of the switch to pci_sbdf_t. Also, with the s/int/unsigned int/ changes, I will remove "No functional change" from the commit description (not sure it should have been there in the first place).

Quoted 9 lines
Quoted 9 lines
xen/arch/x86/msi.c +1 -2
@@ -318,17 +315,12 @@ static bool msi_set_mask_bit(struct irq_desc *desc, bool host, bool guest)
 {
     struct msi_desc *entry = desc->msi_desc;
     struct pci_dev *pdev;
-    u16 seg, control;
-    u8 bus, slot, func;
+    u16 control;
     bool flag = host || guest, maskall;
 
Quoted 13 lines

xen/arch/x86/msi.c static bool msi_set_mask_bit(struct irq_desc *desc, bool host, bool guest) · line 326

Quoted 10 lines
xen/arch/x86/msi.c +0 -4
     ASSERT(spin_is_locked(&desc->lock));
     BUG_ON(!entry || !entry->dev);
     pdev = entry->dev;
-    seg = pdev->seg;
-    bus = pdev->bus;
-    slot = PCI_SLOT(pdev->devfn);
-    func = PCI_FUNC(pdev->devfn);
     switch ( entry->msi_attrib.type )
     {
     case PCI_CAP_ID_MSI:

You don't further alter the function, so this looks like an unrelated (but still desirable for at least Misra) change.

Right. These instances of -Wunused-but-set-variable warnings were the result of a previous pci_sbdf_t conversion. I'll split it into a separate patch, perhaps with a fixes tag:

Fixes 0c38c61aad21 ("pci: switch pci_conf_write32 to use pci_sbdf_t")
Quoted 13 lines

xen/arch/x86/msi.c static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf) · line 676

Quoted 4 lines
xen/arch/x86/msi.c +0 -0
@@ -685,8 +674,8 @@ static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf)
     {
         struct pci_dev *pdev = pci_get_pdev(NULL,
                                             PCI_SBDF(seg, bus, slot, func));

With this, ...

xen/arch/x86/msi.c static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf) · line 679

Quoted 5 lines
xen/arch/x86/msi.c +2 -2
-        unsigned int pos = pci_find_ext_capability(seg, bus,
-                                                   PCI_DEVFN(slot, func),
+        unsigned int pos = pci_find_ext_capability(PCI_SBDF(seg, bus, slot,
+                                                            func),
                                                    PCI_EXT_CAP_ID_SRIOV);
Quoted 4 lines

... please use pdev->sbdf here. Albeit I guess some further re-arrangement is wanted here, to eliminate all of those PCI_SBDF() (and then also dealing with the otherwise necessary NULL check). IOW perhaps fine the way you have it, and then to be subject to a follow-on change.

OK. I'll keep it as-is in this patch, then create a follow-on patch that: uses pdev->sbdf instead of PCI_SBDF inside this code block, and moves the NULL check earlier.

Quoted 11 lines
Quoted 11 lines
xen/include/xen/pci.h +0 -5
@@ -193,11 +193,10 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus,
                    unsigned int devfn, int reg, int len, u32 *value);
 int pci_mmcfg_write(unsigned int seg, unsigned int bus,
                     unsigned int devfn, int reg, int len, u32 value);
-int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap);
-int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap);
-int pci_find_ext_capability(int seg, int bus, int devfn, int cap);
-int pci_find_next_ext_capability(int seg, int bus, int devfn, int start,
-                                 int cap);
Quoted 8 lines

xen/include/xen/pci.h int pci_mmcfg_read(unsigned int seg, unsigned int bus, · line 199

Quoted 4 lines
xen/include/xen/pci.h +4 -0
+int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap);
+int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap);
+int pci_find_ext_capability(pci_sbdf_t sbdf, int cap);
+int pci_find_next_ext_capability(pci_sbdf_t sbdf, int start, int cap);

The remaining types want converting, too: Neither fixed-width nor plain int are appropriate here. (It's a few too many type adjustments to make, for me to offer doing so while committing.)

Understood, I'm happy to make the change for v4. Is the following what you'd expect it to look like?

unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap); unsigned int pci_find_next_cap(pci_sbdf_t sbdf, unsigned int pos,

                               unsigned int cap);
unsigned int pci_find_ext_capability(pci_sbdf_t sbdf, unsigned int cap);
unsigned int pci_find_next_ext_capability(pci_sbdf_t sbdf, unsigned int start,
                                          unsigned int cap);
#6 lore raw

Re: [PATCH v3 2/4] xen/pci: convert pci_find_*cap* to pci_sbdf_t

Quoted 22 lines · Stewart Hildebrand
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Stewart Hildebrand
xen/include/xen/pci.h +0 -5
@@ -193,11 +193,10 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus,
                    unsigned int devfn, int reg, int len, u32 *value);
 int pci_mmcfg_write(unsigned int seg, unsigned int bus,
                     unsigned int devfn, int reg, int len, u32 value);
-int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap);
-int pci_find_next_cap(u16 seg, u8 bus, unsigned int devfn, u8 pos, int cap);
-int pci_find_ext_capability(int seg, int bus, int devfn, int cap);
-int pci_find_next_ext_capability(int seg, int bus, int devfn, int start,
-                                 int cap);
Quoted 8 lines

xen/include/xen/pci.h int pci_mmcfg_read(unsigned int seg, unsigned int bus, · line 199

Quoted 4 lines
xen/include/xen/pci.h +4 -0
+int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap);
+int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap);
+int pci_find_ext_capability(pci_sbdf_t sbdf, int cap);
+int pci_find_next_ext_capability(pci_sbdf_t sbdf, int start, int cap);

The remaining types want converting, too: Neither fixed-width nor plain int are appropriate here. (It's a few too many type adjustments to make, for me to offer doing so while committing.)

Quoted 8 lines

Understood, I'm happy to make the change for v4. Is the following what you'd expect it to look like?

unsigned int pci_find_cap_offset(pci_sbdf_t sbdf, unsigned int cap); unsigned int pci_find_next_cap(pci_sbdf_t sbdf, unsigned int pos,

                               unsigned int cap);
unsigned int pci_find_ext_capability(pci_sbdf_t sbdf, unsigned int cap);
unsigned int pci_find_next_ext_capability(pci_sbdf_t sbdf, unsigned int start,
                                          unsigned int cap);

Yes, this looks correct. Thanks.

Jan

#7 lore raw

PATCH v3 3/4 xen/vpci: header: filter PCI capabilities

In reply to Stewart Hildebrand

Currently, Xen vPCI only supports virtualizing the MSI and MSI-X capabilities. Hide all other PCI capabilities (including extended capabilities) from domUs for now, even though there may be certain devices/drivers that depend on being able to discover certain capabilities.

We parse the physical PCI capabilities linked list and add vPCI register handlers for the next elements, inserting our own next value, thus presenting a modified linked list to the domU.

Introduce helper functions vpci_hw_read8 and vpci_read_val. The vpci_read_val helper function returns a fixed value, which may be used for RAZ registers, or registers whose value doesn't change.

Introduce pci_find_next_cap_ttl() helper while adapting the logic from pci_find_next_cap() to suit our needs, and implement the existing pci_find_next_cap() in terms of the new helper.

---
v2->v3:
* get rid of > 0 in loop condition
* implement pci_find_next_cap in terms of new pci_find_next_cap_ttl function so that hypothetical future callers wouldn't be required to pass &ttl.
* change NULL to (void *)0 for RAZ value passed to vpci_read_val
* change type of ttl to unsigned int
* remember to mask off the low 2 bits of next in the initial loop iteration
* change return type of pci_find_next_cap and pci_find_next_cap_ttl
* avoid wrapping the PCI_STATUS_CAP_LIST condition by using ! instead of == 0

v1->v2:
* change type of ttl to int
* use switch statement instead of if/else
* adapt existing pci_find_next_cap helper instead of rolling our own
* pass ttl as in/out
* "pass through" the lower 2 bits of the next pointer
* squash helper functions into this patch to avoid transient dead code situation
* extended capabilities RAZ/WI

---
 xen/drivers/pci/pci.c     | 24 +++++++++-----
 xen/drivers/vpci/header.c | 69 +++++++++++++++++++++++++++++++++++++++
 xen/drivers/vpci/vpci.c   | 12 +++++++
 xen/include/xen/pci.h     |  5 ++-
 xen/include/xen/vpci.h    |  5 +++
 5 files changed, 106 insertions(+), 9 deletions(-)
xen/drivers/pci/pci.c +16 -8
index 3bcb74040284..f60051694dc5 100644
@@ -39,30 +39,38 @@ int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap)
     return 0;
 }
 
-int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap)
+uint8_t pci_find_next_cap_ttl(pci_sbdf_t sbdf, uint8_t pos,
+                              bool (*is_match)(uint8_t), unsigned int *ttl)
 {
-    u8 id;
-    int ttl = 48;
+    uint8_t id;
 
-    while ( ttl-- )
+    while ( (*ttl)-- )
     {
         pos = pci_conf_read8(sbdf, pos);
         if ( pos < 0x40 )
             break;
 
-        pos &= ~3;
-        id = pci_conf_read8(sbdf, pos + PCI_CAP_LIST_ID);
+        id = pci_conf_read8(sbdf, (pos & ~3) + PCI_CAP_LIST_ID);
 
         if ( id == 0xff )
             break;
-        if ( id == cap )
+        if ( is_match(id) )
             return pos;
 
-        pos += PCI_CAP_LIST_NEXT;
+        pos = (pos & ~3) + PCI_CAP_LIST_NEXT;
     }
+
     return 0;
 }
 
+uint8_t pci_find_next_cap(pci_sbdf_t sbdf, uint8_t pos,
+                          bool (*is_match)(uint8_t))
+{
+    unsigned int ttl = 48;
+
+    return pci_find_next_cap_ttl(sbdf, pos, is_match, &ttl) & ~3;
+}
+
 /**
  * pci_find_ext_capability - Find an extended capability
  * @sbdf: PCI device to query
xen/drivers/vpci/header.c +69 -0

81 lines not on this page · raw message

xen/drivers/vpci/vpci.c +12 -0
index d73fa7630237..4a96aa50494d 100644
@@ -133,6 +133,18 @@ static void cf_check vpci_ignored_write(
 {
 }
 
+uint32_t cf_check vpci_read_val(
+    const struct pci_dev *pdev, unsigned int reg, void *data)
+{
+    return (uintptr_t)data;
+}
+
+uint32_t cf_check vpci_hw_read8(
+    const struct pci_dev *pdev, unsigned int reg, void *data)
+{
+    return pci_conf_read8(pdev->sbdf, reg);
+}
+
 uint32_t cf_check vpci_hw_read16(
     const struct pci_dev *pdev, unsigned int reg, void *data)
 {
xen/include/xen/pci.h +4 -1
index 8a482b15745c..b30034ecccba 100644
@@ -194,7 +194,10 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus,
 int pci_mmcfg_write(unsigned int seg, unsigned int bus,
                     unsigned int devfn, int reg, int len, u32 value);
 int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap);
-int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap);
+uint8_t pci_find_next_cap_ttl(pci_sbdf_t sbdf, uint8_t pos,
+                              bool (*is_match)(uint8_t), unsigned int *ttl);
+uint8_t pci_find_next_cap(pci_sbdf_t sbdf, uint8_t pos,
+                          bool (*is_match)(uint8_t));
 int pci_find_ext_capability(pci_sbdf_t sbdf, int cap);
 int pci_find_next_ext_capability(pci_sbdf_t sbdf, int start, int cap);
 const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
xen/include/xen/vpci.h +5 -0
index 0b8a2a3c745b..17fd252746ec 100644
@@ -45,7 +45,12 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+uint32_t cf_check vpci_read_val(
+    const struct pci_dev *pdev, unsigned int reg, void *data);
+
 /* Passthrough handlers. */
+uint32_t cf_check vpci_hw_read8(
+    const struct pci_dev *pdev, unsigned int reg, void *data);
 uint32_t cf_check vpci_hw_read16(
     const struct pci_dev *pdev, unsigned int reg, void *data);
 uint32_t cf_check vpci_hw_read32(
Signature · 1 line
2.41.0
#8 lore raw

Re: [PATCH v3 3/4] xen/vpci: header: filter PCI capabilities

Quoted 18 lines · Stewart Hildebrand

Currently, Xen vPCI only supports virtualizing the MSI and MSI-X capabilities. Hide all other PCI capabilities (including extended capabilities) from domUs for now, even though there may be certain devices/drivers that depend on being able to discover certain capabilities.

We parse the physical PCI capabilities linked list and add vPCI register handlers for the next elements, inserting our own next value, thus presenting a modified linked list to the domU.

Introduce helper functions vpci_hw_read8 and vpci_read_val. The vpci_read_val helper function returns a fixed value, which may be used for RAZ registers, or registers whose value doesn't change.

Introduce pci_find_next_cap_ttl() helper while adapting the logic from pci_find_next_cap() to suit our needs, and implement the existing pci_find_next_cap() in terms of the new helper.

Quoted 10 lines

---
v2->v3:
* get rid of > 0 in loop condition
* implement pci_find_next_cap in terms of new pci_find_next_cap_ttl function so that hypothetical future callers wouldn't be required to pass &ttl.
* change NULL to (void *)0 for RAZ value passed to vpci_read_val
* change type of ttl to unsigned int
* remember to mask off the low 2 bits of next in the initial loop iteration
* change return type of pci_find_next_cap and pci_find_next_cap_ttl
* avoid wrapping the PCI_STATUS_CAP_LIST condition by using ! instead of == 0

Looks mostly okay to me now, just two things (pointed out before):

Quoted 35 lines
xen/drivers/pci/pci.c +8 -8
@@ -39,30 +39,38 @@ int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap)
     return 0;
 }
 
-int pci_find_next_cap(pci_sbdf_t sbdf, u8 pos, int cap)
+uint8_t pci_find_next_cap_ttl(pci_sbdf_t sbdf, uint8_t pos,
+                              bool (*is_match)(uint8_t), unsigned int *ttl)
 {
-    u8 id;
-    int ttl = 48;
+    uint8_t id;
 
-    while ( ttl-- )
+    while ( (*ttl)-- )
     {
         pos = pci_conf_read8(sbdf, pos);
         if ( pos < 0x40 )
             break;
 
-        pos &= ~3;
-        id = pci_conf_read8(sbdf, pos + PCI_CAP_LIST_ID);
+        id = pci_conf_read8(sbdf, (pos & ~3) + PCI_CAP_LIST_ID);
 
         if ( id == 0xff )
             break;
-        if ( id == cap )
+        if ( is_match(id) )
             return pos;
 
-        pos += PCI_CAP_LIST_NEXT;
+        pos = (pos & ~3) + PCI_CAP_LIST_NEXT;
     }
+

xen/drivers/pci/pci.c int pci_find_cap_offset(pci_sbdf_t sbdf, u8 cap) · line 72

Quoted 10 lines
xen/drivers/pci/pci.c +7 -0
     return 0;
 }
 
+uint8_t pci_find_next_cap(pci_sbdf_t sbdf, uint8_t pos,
+                          bool (*is_match)(uint8_t))
+{
+    unsigned int ttl = 48;
+
+    return pci_find_next_cap_ttl(sbdf, pos, is_match, &ttl) & ~3;
+}

You still change the original function's signature. In this patch, the prototype for it should not need touching at all.

The other is the imo excessive use of fixed width types. "pos" has no business being uint8_t (but that'll be taken care of in the earlier patch for the case here), and similarly e.g. id doesn't need to be (in the earlier function). But I can see that at least some of the cases here are on the edge ...

Jan

#9 lore raw

PATCH v3 4/4 xen/vpci: header: status register handler

In reply to Stewart Hildebrand

Introduce a handler for the PCI status register, with ability to mask the capabilities bit. The status register is write-1-to-clear, so introduce handling for this type of register in vPCI.

---
v2->v3:
* new patch

---
 xen/drivers/vpci/header.c | 24 +++++++++++++++++++++++
 xen/drivers/vpci/vpci.c   | 41 +++++++++++++++++++++++++++++----------
 xen/include/xen/vpci.h    |  9 +++++++++
 3 files changed, 64 insertions(+), 10 deletions(-)
xen/drivers/vpci/header.c +24 -0
index b531ab03cec1..7061b85e337b 100644
@@ -413,6 +413,17 @@ static void cf_check cmd_write(
         pci_conf_write16(pdev->sbdf, reg, cmd);
 }
 
+static uint32_t cf_check status_read(const struct pci_dev *pdev,
+                                     unsigned int reg, void *data)
+{
+    struct vpci_header *header = data;
+
+    if ( header->mask_cap_list )
+        return pci_conf_read16(pdev->sbdf, reg) & ~(PCI_STATUS_CAP_LIST);
+
+    return pci_conf_read16(pdev->sbdf, reg);
+}
+
 static void cf_check bar_write(
     const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data)
 {
@@ -556,6 +567,11 @@ static int cf_check init_bars(struct pci_dev *pdev)
     if ( rc )
         return rc;
 
+    rc = vpci_add_rw1c_register(pdev->vpci, status_read, vpci_hw_write16,
+                                PCI_STATUS, 2, header);
+    if ( rc )
+        return rc;
+
     if ( !is_hardware_domain(pdev->domain) )
     {
         if ( !(pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST) )
@@ -583,6 +599,14 @@ static int cf_check init_bars(struct pci_dev *pdev)
 
             next &= ~3;
 
+            if ( !next )
+                /*
+                 * If we don't have any supported capabilities to expose to the
+                 * guest, mask the PCI_STATUS_CAP_LIST bit in the status
+                 * register.
+                 */
+                header->mask_cap_list = true;
+
             while ( next && ttl )
             {
                 uint8_t pos = next;
xen/drivers/vpci/vpci.c +31 -10

78 lines not on this page · raw message

xen/include/xen/vpci.h +9 -0
index 17fd252746ec..518d381b2df7 100644
@@ -37,6 +37,11 @@ int __must_check vpci_add_register(struct vpci *vpci,
                                    vpci_write_t *write_handler,
                                    unsigned int offset, unsigned int size,
                                    void *data);
+int __must_check vpci_add_rw1c_register(struct vpci *vpci,
+                                        vpci_read_t *read_handler,
+                                        vpci_write_t *write_handler,
+                                        unsigned int offset, unsigned int size,
+                                        void *data);
 int __must_check vpci_remove_register(struct vpci *vpci, unsigned int offset,
                                       unsigned int size);
 
@@ -55,6 +60,8 @@ uint32_t cf_check vpci_hw_read16(
     const struct pci_dev *pdev, unsigned int reg, void *data);
 uint32_t cf_check vpci_hw_read32(
     const struct pci_dev *pdev, unsigned int reg, void *data);
+void cf_check vpci_hw_write16(
+    const struct pci_dev *pdev, unsigned int reg, uint32_t val, void *data);
 
 /*
  * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
@@ -99,6 +106,8 @@ struct vpci {
          * upon to know whether BARs are mapped into the guest p2m.
          */
         bool bars_mapped      : 1;
+        /* Store whether to hide all capabilities from the guest. */
+        bool mask_cap_list : 1;
         /* FIXME: currently there's no support for SR-IOV. */
     } header;
 
Signature · 1 line
2.41.0
#10 lore raw

Re: [PATCH v3 4/4] xen/vpci: header: status register handler

Quoted 8 lines · Stewart Hildebrand

Introduce a handler for the PCI status register, with ability to mask the capabilities bit. The status register is write-1-to-clear, so introduce handling for this type of register in vPCI.

---
v2->v3:
* new patch

This being a prereq to the cap list filtering, I think the order of the patches wants to be inverted.

xen/drivers/vpci/header.c static void cf_check cmd_write( · line 422

Quoted 13 lines
xen/drivers/vpci/header.c +7 -0
@@ -413,6 +413,17 @@ static void cf_check cmd_write(
         pci_conf_write16(pdev->sbdf, reg, cmd);
 }
 
+static uint32_t cf_check status_read(const struct pci_dev *pdev,
+                                     unsigned int reg, void *data)
+{
+    struct vpci_header *header = data;
+
+    if ( header->mask_cap_list )
+        return pci_conf_read16(pdev->sbdf, reg) & ~(PCI_STATUS_CAP_LIST);

No need for parentheses around a constant.

xen/drivers/vpci/header.c static void cf_check cmd_write( · line 424

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+    return pci_conf_read16(pdev->sbdf, reg);

I think this function would better issue the read just in a single place, and then do any fiddling that may be needed.

xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 571

Quoted 6 lines
xen/drivers/vpci/header.c +2 -0
@@ -556,6 +567,11 @@ static int cf_check init_bars(struct pci_dev *pdev)
     if ( rc )
         return rc;
 
+    rc = vpci_add_rw1c_register(pdev->vpci, status_read, vpci_hw_write16,
+                                PCI_STATUS, 2, header);

Is it really correct to treat the entire register as rw1c, and with write- through to hardware for all bits? There are reserved bit there, and - however likely that may seem - it's guesswork whether they would also end up r/o or rw1c once getting assigned some meaning.

xen/drivers/vpci/vpci.c struct vpci_register { · line 33

Quoted 8 lines
xen/drivers/vpci/vpci.c +1 -0
@@ -29,6 +29,7 @@ struct vpci_register {
     unsigned int offset;
     void *private;
     struct list_head node;
+    bool rw1c : 1;
 };

I'm not the maintainer of this code, so what I say here may be void, but generally we have blanks to the left and/or right of colons in bitfield declarations only when we mean to pad for alignment with other nearby bitfields.

Quoted 9 lines
xen/drivers/vpci/vpci.c +5 -0
@@ -205,6 +213,22 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,
     return 0;
 }
 
+int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,
+                      vpci_write_t *write_handler, unsigned int offset,
+                      unsigned int size, void *data)
+{
+    return _vpci_add_register(vpci, read_handler, write_handler, offset, size,

xen/drivers/vpci/vpci.c int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, · line 230

Quoted 10 lines
xen/drivers/vpci/vpci.c +10 -0
+                              data, false);
+}
+
+int vpci_add_rw1c_register(struct vpci *vpci, vpci_read_t *read_handler,
+                           vpci_write_t *write_handler, unsigned int offset,
+                           unsigned int size, void *data)
+{
+    return _vpci_add_register(vpci, read_handler, write_handler, offset, size,
+                              data, true);
+}

I'm always a little irritated by local functions still retaining the subsystem prefix. Just add_register() for the now static helper would imo be enough here and overall shorter to read/type.

xen/drivers/vpci/vpci.c static void vpci_write_helper(const struct pci_dev *pdev, · line 458

Quoted 9 lines
xen/drivers/vpci/vpci.c +4 -1
@@ -433,9 +452,11 @@ static void vpci_write_helper(const struct pci_dev *pdev,
 
     if ( size != r->size )
     {
-        uint32_t val;
+        uint32_t val = 0;
+
+        if ( !r->rw1c )
+            val = r->read(pdev, r->offset, r->private);

Along with the earlier question: Doesn't rw1c need to be a bit mask, not a single boolean covering the entire register?

xen/drivers/vpci/vpci.c struct vpci { · line 110

Quoted 6 lines
xen/drivers/vpci/vpci.c +2 -0
@@ -99,6 +106,8 @@ struct vpci {
          * upon to know whether BARs are mapped into the guest p2m.
          */
         bool bars_mapped      : 1;
+        /* Store whether to hide all capabilities from the guest. */
+        bool mask_cap_list : 1;

I think the intention here is for the colons to align.

Jan

#11 lore raw

Re: [PATCH v3 4/4] xen/vpci: header: status register handler

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Stewart Hildebrand

Introduce a handler for the PCI status register, with ability to mask the capabilities bit. The status register is write-1-to-clear, so introduce handling for this type of register in vPCI.

Quoted 7 lines
Quoted 4 lines

---
v2->v3:
* new patch

This being a prereq to the cap list filtering, I think the order of the patches wants to be inverted.

Will do

Quoted 6 lines
Quoted 6 lines
xen/drivers/vpci/header.c +0 -0
@@ -413,6 +413,17 @@ static void cf_check cmd_write(
         pci_conf_write16(pdev->sbdf, reg, cmd);
 }
 
Quoted 9 lines

xen/drivers/vpci/header.c static void cf_check cmd_write( · line 422

Quoted 7 lines
xen/drivers/vpci/header.c +7 -0
+static uint32_t cf_check status_read(const struct pci_dev *pdev,
+                                     unsigned int reg, void *data)
+{
+    struct vpci_header *header = data;
+
+    if ( header->mask_cap_list )
+        return pci_conf_read16(pdev->sbdf, reg) & ~(PCI_STATUS_CAP_LIST);

No need for parentheses around a constant.

OK

Quoted 4 lines

xen/drivers/vpci/header.c static void cf_check cmd_write( · line 424

Quoted 1 line
xen/drivers/vpci/header.c +1 -0
+    return pci_conf_read16(pdev->sbdf, reg);

I think this function would better issue the read just in a single place, and then do any fiddling that may be needed.

OK

Quoted 11 lines

xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 571

Quoted 6 lines
xen/drivers/vpci/header.c +2 -0
@@ -556,6 +567,11 @@ static int cf_check init_bars(struct pci_dev *pdev)
     if ( rc )
         return rc;
 
+    rc = vpci_add_rw1c_register(pdev->vpci, status_read, vpci_hw_write16,
+                                PCI_STATUS, 2, header);

Is it really correct to treat the entire register as rw1c, and with write- through to hardware for all bits? There are reserved bit there, and - however likely that may seem - it's guesswork whether they would also end up r/o or rw1c once getting assigned some meaning.

A bit mask would make more sense, so I will change it to a bit mask. As another data point, qemu also uses a bit mask [1].

[1] https://gitlab.com/qemu-project/qemu/-/blob/v8.1.0/hw/xen/xen_pt_config_init.c?ref_type=tags#L645

Quoted 5 lines
Quoted 5 lines
xen/drivers/vpci/vpci.c +0 -0
@@ -29,6 +29,7 @@ struct vpci_register {
     unsigned int offset;
     void *private;
Quoted 8 lines

xen/drivers/vpci/vpci.c struct vpci_register { · line 33

Quoted 3 lines
xen/drivers/vpci/vpci.c +1 -0
     struct list_head node;
+    bool rw1c : 1;
 };

I'm not the maintainer of this code, so what I say here may be void, but generally we have blanks to the left and/or right of colons in bitfield declarations only when we mean to pad for alignment with other nearby bitfields.

OK. With the change to bit mask, this will become uint32_t rw1c_mask;

Quoted 15 lines
Quoted 15 lines
xen/drivers/vpci/vpci.c +11 -0
@@ -205,6 +213,22 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,
     return 0;
 }
 
+int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,
+                      vpci_write_t *write_handler, unsigned int offset,
+                      unsigned int size, void *data)
+{
+    return _vpci_add_register(vpci, read_handler, write_handler, offset, size,
+                              data, false);
+}
+
+int vpci_add_rw1c_register(struct vpci *vpci, vpci_read_t *read_handler,
+                           vpci_write_t *write_handler, unsigned int offset,
+                           unsigned int size, void *data)
Quoted 8 lines

xen/drivers/vpci/vpci.c int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler, · line 230

Quoted 4 lines
xen/drivers/vpci/vpci.c +4 -0
+{
+    return _vpci_add_register(vpci, read_handler, write_handler, offset, size,
+                              data, true);
+}

I'm always a little irritated by local functions still retaining the subsystem prefix. Just add_register() for the now static helper would imo be enough here and overall shorter to read/type.

I will change to add_register()

Quoted 12 lines

xen/drivers/vpci/vpci.c static void vpci_write_helper(const struct pci_dev *pdev, · line 458

Quoted 9 lines
xen/drivers/vpci/vpci.c +4 -1
@@ -433,9 +452,11 @@ static void vpci_write_helper(const struct pci_dev *pdev,
 
     if ( size != r->size )
     {
-        uint32_t val;
+        uint32_t val = 0;
+
+        if ( !r->rw1c )
+            val = r->read(pdev, r->offset, r->private);

Along with the earlier question: Doesn't rw1c need to be a bit mask, not a single boolean covering the entire register?

Yes

Quoted 8 lines

xen/drivers/vpci/vpci.c struct vpci { · line 110

Quoted 6 lines
xen/drivers/vpci/vpci.c +2 -0
@@ -99,6 +106,8 @@ struct vpci {
          * upon to know whether BARs are mapped into the guest p2m.
          */
         bool bars_mapped      : 1;
+        /* Store whether to hide all capabilities from the guest. */
+        bool mask_cap_list : 1;

I think the intention here is for the colons to align.

OK

Stew