Implement virtualized handling for PCIe TPH capability control register writes. Validate and mediate user write requests to accept only valid TPH mode configurations. Synchronize shadow steering tag table to hardware when TPH gets enabled successfully. Automatically disable TPH feature during device ownership handover and userspace file descriptor closing, ensuring hardware state consistency after device release. Signed-off-by: Chengwen Feng --- drivers/vfio/pci/vfio_pci_config.c | 38 ++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 8 ++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index a10ed733f0e3..188845f81626 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -20,8 +20,10 @@ * must be negotiated with the underlying OS. */ +#include #include #include +#include #include #include #include @@ -35,6 +37,8 @@ ((offset >= PCI_BASE_ADDRESS_0 && offset < PCI_BASE_ADDRESS_5 + 4) || \ (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4)) +extern bool enable_unsafe_tph; + /* * Lengths of PCI Config Capabilities * 0: Removed from the user visible capability list @@ -313,6 +317,39 @@ static int vfio_virt_config_read(struct vfio_pci_core_device *vdev, int pos, return count; } +static int vfio_pci_tph_config_write(struct vfio_pci_core_device *vdev, int pos, + int count, struct perm_bits *perm, + int offset, __le32 val) +{ + struct pci_dev *pdev = vdev->pdev; + u32 data = le32_to_cpu(val); + u8 mode, req_en; + int i, ret; + + if (!enable_unsafe_tph) + return count; + + if (offset != PCI_TPH_CTRL || count < 2) + return count; + + guard(mutex)(&vdev->tph_lock); + + mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, data); + req_en = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, data); + if (req_en) { + ret = pcie_enable_tph(pdev, mode); + if (ret == 0 && vdev->tph_st_shadow) { + for (i = 0; i < vdev->tph_st_entries; i++) + pcie_tph_set_st_entry(pdev, i, + vdev->tph_st_shadow[i]); + } + } else { + pcie_disable_tph(vdev->pdev); + } + + return count; +} + static struct perm_bits direct_ro_perms = { .readfn = vfio_direct_config_read, }; @@ -1121,6 +1158,7 @@ int __init vfio_pci_init_perm_bits(void) ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]); ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]); ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write; + ecap_perms[PCI_EXT_CAP_ID_TPH].writefn = vfio_pci_tph_config_write; ecap_perms[PCI_EXT_CAP_ID_DVSEC].writefn = vfio_raw_config_write; if (ret) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 6fc7496cb8dd..1a9de22202de 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -42,7 +42,7 @@ static bool nointxmask; static bool disable_vga; static bool disable_idle_d3; -static bool enable_unsafe_tph; +bool enable_unsafe_tph; static void vfio_pci_eventfd_rcu_free(struct rcu_head *rcu) { @@ -543,6 +543,9 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) return ret; } + /* Clear TPH status when taking over ownership */ + pcie_disable_tph(pdev); + /* Don't allow our initial saved state to include busmaster */ pci_clear_master(pdev); @@ -656,6 +659,9 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) */ vfio_pci_set_power_state(vdev, PCI_D0); + /* Clear TPH status when releasing ownership */ + pcie_disable_tph(pdev); + /* Stop the device from further DMA */ pci_clear_master(pdev); -- 2.17.1