[PATCH rfcv1 21/23] intel_iommu: invalidate piotlb when flush pasid

Zhenzhong Duan posted 23 patches 10 months, 2 weeks ago
[PATCH rfcv1 21/23] intel_iommu: invalidate piotlb when flush pasid
Posted by Zhenzhong Duan 10 months, 2 weeks ago
From: Yi Sun <yi.y.sun@linux.intel.com>

When bind/unbind emulated devices, we should invalidate QEMU
piotlb. Host will flush piotlb for passthrough devices so we
don't handle passthrough devices.

Signed-off-by: Yi Sun <yi.y.sun@linux.intel.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 hw/i386/intel_iommu.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index 6a6478e865..2f3d3a28b0 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -84,6 +84,8 @@ static VTDPASIDAddressSpace *vtd_add_find_pasid_as(IntelIOMMUState *s,
                                                    uint32_t pasid);
 static int vtd_dev_get_rid2pasid(IntelIOMMUState *s, uint8_t bus_num,
                                  uint8_t devfn, uint32_t *rid_pasid);
+static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer value,
+                                         gpointer user_data);
 
 static void vtd_panic_require_caching_mode(void)
 {
@@ -3667,14 +3669,21 @@ static gboolean vtd_flush_pasid(gpointer key, gpointer value,
     VTDPASIDCacheEntry *pc_entry = &vtd_pasid_as->pasid_cache_entry;
     PCIBus *bus = vtd_pasid_as->bus;
     VTDPASIDEntry pe;
+    VTDIOMMUFDDevice *vtd_idev;
+    VTDIOTLBPageInvInfo info;
     uint16_t did;
     uint32_t pasid;
     uint16_t devfn;
     int ret;
+    struct vtd_as_key as_key = {
+        .bus = vtd_pasid_as->bus,
+        .devfn = vtd_pasid_as->devfn,
+    };
 
     did = vtd_pe_get_domain_id(&pc_entry->pasid_entry);
     pasid = vtd_pasid_as->pasid;
     devfn = vtd_pasid_as->devfn;
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &as_key);
 
     switch (pc_info->type) {
     case VTD_PASID_CACHE_FORCE_RESET:
@@ -3702,6 +3711,13 @@ static gboolean vtd_flush_pasid(gpointer key, gpointer value,
         abort();
     }
 
+    info.domain_id = did;
+    info.pasid = pasid;
+    /* For passthrough device, we don't need invalidate QEMU piotlb */
+    if (s->root_scalable && likely(s->dmar_enabled) && !vtd_idev)
+        g_hash_table_foreach_remove(s->p_iotlb, vtd_hash_remove_by_pasid,
+                                    &info);
+
     /*
      * pasid cache invalidation may indicate a present pasid
      * entry to present pasid entry modification. To cover such
@@ -3725,18 +3741,9 @@ static gboolean vtd_flush_pasid(gpointer key, gpointer value,
         return true;
     }
 
-    /*
-     * TODO:
-     * - when pasid-base-iotlb(piotlb) infrastructure is ready,
-     *   should invalidate QEMU piotlb togehter with this change.
-     */
     return false;
+
 remove:
-    /*
-     * TODO:
-     * - when pasid-base-iotlb(piotlb) infrastructure is ready,
-     *   should invalidate QEMU piotlb togehter with this change.
-     */
     if (vtd_bind_guest_pasid(vtd_pasid_as,
                              NULL, VTD_PASID_UNBIND)) {
         pasid_cache_info_set_error(pc_info);
-- 
2.34.1