From: Manish Honap Since commit 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list."), vfio-pci passes user writes to every DVSEC through to the device. Before that commit DVSECs were hidden. For a function that is CXL.cache and CXL.mem capable, the user can then: - clear Cache_Enable or Mem_Enable in DVSEC Control, - set Initiate CXL Reset or Initiate Cache Writeback and Invalidation in DVSEC Control2, which the host does not see or handle, - write the DVSEC Range registers, - set Config Lock, after which the host cannot change the locked registers. This change keeps the CXL Device DVSEC visible and makes it read-only: reads return the device values and writes are dropped. The capability is matched by its start, so a CXL DVSEC that directly follows another DVSEC relies on the earlier patch to correctly calculate start of abutting capabilities. Fixes: 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list.") Signed-off-by: Manish Honap --- drivers/vfio/pci/vfio_pci_config.c | 23 +++++++++++++++++++++++ include/linux/vfio_pci_core.h | 1 + 2 files changed, 24 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index dd0b899fbdfd..2bc0c7cbc4b7 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -1746,6 +1746,23 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev) return 0; } +static u16 vfio_pci_cxl_dvsec(struct pci_dev *pdev) +{ + u16 dvsec, cap; + + dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL, + PCI_DVSEC_CXL_DEVICE); + if (!dvsec || + pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CAP, &cap)) + return 0; + + if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || + !(cap & PCI_DVSEC_CXL_MEM_CAPABLE)) + return 0; + + return dvsec; +} + /* * Nag about hardware bugs, hopefully to have vendors fix them, but at least * to collect a list of dependencies for the VF INTx pin quirk below. @@ -1863,6 +1880,8 @@ int vfio_config_init(struct vfio_pci_core_device *vdev) if (ret) goto out; + vdev->cxl_dvsec = vfio_pci_cxl_dvsec(vdev->pdev); + return 0; out: @@ -1953,6 +1972,10 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev, perm = &ecap_perms[cap_id]; cap_start = vfio_find_cap_start(vdev, *ppos); + + if (cap_id == PCI_EXT_CAP_ID_DVSEC && + cap_start == vdev->cxl_dvsec) + perm = &direct_ro_perms; } else { WARN_ON(cap_id > PCI_CAP_ID_MAX); diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 2f3fa2787737..dc25f737fe93 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -109,6 +109,7 @@ struct vfio_pci_core_device { u8 *pci_config_map; u8 *vconfig; struct perm_bits *msi_perm; + u16 cxl_dvsec; spinlock_t irqlock; struct mutex igate; struct xarray ctx; -- 2.53.0