Blame SOURCES/kvm-virtio-iommu-Add-bypass-mode-support-to-assigned-dev.patch

586cba
From d60774ee3168eefb21a4120a38107cd36ae17e07 Mon Sep 17 00:00:00 2001
586cba
From: Zhenzhong Duan <zhenzhong.duan@intel.com>
586cba
Date: Mon, 13 Jun 2022 14:10:08 +0800
586cba
Subject: [PATCH 01/17] virtio-iommu: Add bypass mode support to assigned
586cba
 device
586cba
586cba
RH-Author: Eric Auger <eric.auger@redhat.com>
586cba
RH-MergeRequest: 105: virtio-iommu: Fix bypass mode for assigned devices
586cba
RH-Commit: [1/5] 4777815533b31c7f4f09af8902e378fd3fc1186a (eauger1/centos-qemu-kvm)
586cba
RH-Bugzilla: 2100106
586cba
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
586cba
RH-Acked-by: Peter Xu <peterx@redhat.com>
586cba
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
586cba
586cba
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2100106
586cba
586cba
Currently assigned devices can not work in virtio-iommu bypass mode.
586cba
Guest driver fails to probe the device due to DMA failure. And the
586cba
reason is because of lacking GPA -> HPA mappings when VM is created.
586cba
586cba
Add a root container memory region to hold both bypass memory region
586cba
and iommu memory region, so the switch between them is supported
586cba
just like the implementation in virtual VT-d.
586cba
586cba
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
586cba
Message-Id: <20220613061010.2674054-2-zhenzhong.duan@intel.com>
586cba
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
586cba
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
586cba
(cherry picked from commit 90519b90539b16258d1d52b908b199f44877dc18)
586cba
Signed-off-by: Eric Auger <eric.auger@redhat.com>
586cba
---
586cba
 hw/virtio/trace-events           |   1 +
586cba
 hw/virtio/virtio-iommu.c         | 115 ++++++++++++++++++++++++++++++-
586cba
 include/hw/virtio/virtio-iommu.h |   2 +
586cba
 3 files changed, 116 insertions(+), 2 deletions(-)
586cba
586cba
diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
586cba
index a5102eac9e..2ab5881b88 100644
586cba
--- a/hw/virtio/trace-events
586cba
+++ b/hw/virtio/trace-events
586cba
@@ -114,6 +114,7 @@ virtio_iommu_remap(const char *name, uint64_t virt_start, uint64_t virt_end, uin
586cba
 virtio_iommu_set_page_size_mask(const char *name, uint64_t old, uint64_t new) "mr=%s old_mask=0x%"PRIx64" new_mask=0x%"PRIx64
586cba
 virtio_iommu_notify_flag_add(const char *name) "add notifier to mr %s"
586cba
 virtio_iommu_notify_flag_del(const char *name) "del notifier from mr %s"
586cba
+virtio_iommu_switch_address_space(uint8_t bus, uint8_t slot, uint8_t fn, bool on) "Device %02x:%02x.%x switching address space (iommu enabled=%d)"
586cba
 
586cba
 # virtio-mem.c
586cba
 virtio_mem_send_response(uint16_t type) "type=%" PRIu16
586cba
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
586cba
index 6d5ea0bdf1..5e99e6c62b 100644
586cba
--- a/hw/virtio/virtio-iommu.c
586cba
+++ b/hw/virtio/virtio-iommu.c
586cba
@@ -70,6 +70,77 @@ static inline uint16_t virtio_iommu_get_bdf(IOMMUDevice *dev)
586cba
     return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn);
586cba
 }
586cba
 
586cba
+static bool virtio_iommu_device_bypassed(IOMMUDevice *sdev)
586cba
+{
586cba
+    uint32_t sid;
586cba
+    bool bypassed;
586cba
+    VirtIOIOMMU *s = sdev->viommu;
586cba
+    VirtIOIOMMUEndpoint *ep;
586cba
+
586cba
+    sid = virtio_iommu_get_bdf(sdev);
586cba
+
586cba
+    qemu_mutex_lock(&s->mutex);
586cba
+    /* need to check bypass before system reset */
586cba
+    if (!s->endpoints) {
586cba
+        bypassed = s->config.bypass;
586cba
+        goto unlock;
586cba
+    }
586cba
+
586cba
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(sid));
586cba
+    if (!ep || !ep->domain) {
586cba
+        bypassed = s->config.bypass;
586cba
+    } else {
586cba
+        bypassed = ep->domain->bypass;
586cba
+    }
586cba
+
586cba
+unlock:
586cba
+    qemu_mutex_unlock(&s->mutex);
586cba
+    return bypassed;
586cba
+}
586cba
+
586cba
+/* Return whether the device is using IOMMU translation. */
586cba
+static bool virtio_iommu_switch_address_space(IOMMUDevice *sdev)
586cba
+{
586cba
+    bool use_remapping;
586cba
+
586cba
+    assert(sdev);
586cba
+
586cba
+    use_remapping = !virtio_iommu_device_bypassed(sdev);
586cba
+
586cba
+    trace_virtio_iommu_switch_address_space(pci_bus_num(sdev->bus),
586cba
+                                            PCI_SLOT(sdev->devfn),
586cba
+                                            PCI_FUNC(sdev->devfn),
586cba
+                                            use_remapping);
586cba
+
586cba
+    /* Turn off first then on the other */
586cba
+    if (use_remapping) {
586cba
+        memory_region_set_enabled(&sdev->bypass_mr, false);
586cba
+        memory_region_set_enabled(MEMORY_REGION(&sdev->iommu_mr), true);
586cba
+    } else {
586cba
+        memory_region_set_enabled(MEMORY_REGION(&sdev->iommu_mr), false);
586cba
+        memory_region_set_enabled(&sdev->bypass_mr, true);
586cba
+    }
586cba
+
586cba
+    return use_remapping;
586cba
+}
586cba
+
586cba
+static void virtio_iommu_switch_address_space_all(VirtIOIOMMU *s)
586cba
+{
586cba
+    GHashTableIter iter;
586cba
+    IOMMUPciBus *iommu_pci_bus;
586cba
+    int i;
586cba
+
586cba
+    g_hash_table_iter_init(&iter, s->as_by_busptr);
586cba
+    while (g_hash_table_iter_next(&iter, NULL, (void **)&iommu_pci_bus)) {
586cba
+        for (i = 0; i < PCI_DEVFN_MAX; i++) {
586cba
+            if (!iommu_pci_bus->pbdev[i]) {
586cba
+                continue;
586cba
+            }
586cba
+            virtio_iommu_switch_address_space(iommu_pci_bus->pbdev[i]);
586cba
+        }
586cba
+    }
586cba
+}
586cba
+
586cba
 /**
586cba
  * The bus number is used for lookup when SID based operations occur.
586cba
  * In that case we lazily populate the IOMMUPciBus array from the bus hash
586cba
@@ -214,6 +285,7 @@ static gboolean virtio_iommu_notify_map_cb(gpointer key, gpointer value,
586cba
 static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *ep)
586cba
 {
586cba
     VirtIOIOMMUDomain *domain = ep->domain;
586cba
+    IOMMUDevice *sdev = container_of(ep->iommu_mr, IOMMUDevice, iommu_mr);
586cba
 
586cba
     if (!ep->domain) {
586cba
         return;
586cba
@@ -222,6 +294,7 @@ static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *ep)
586cba
                    ep->iommu_mr);
586cba
     QLIST_REMOVE(ep, next);
586cba
     ep->domain = NULL;
586cba
+    virtio_iommu_switch_address_space(sdev);
586cba
 }
586cba
 
586cba
 static VirtIOIOMMUEndpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s,
586cba
@@ -324,12 +397,39 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,
586cba
 
586cba
         trace_virtio_iommu_init_iommu_mr(name);
586cba
 
586cba
+        memory_region_init(&sdev->root, OBJECT(s), name, UINT64_MAX);
586cba
+        address_space_init(&sdev->as, &sdev->root, TYPE_VIRTIO_IOMMU);
586cba
+
586cba
+        /*
586cba
+         * Build the IOMMU disabled container with aliases to the
586cba
+         * shared MRs.  Note that aliasing to a shared memory region
586cba
+         * could help the memory API to detect same FlatViews so we
586cba
+         * can have devices to share the same FlatView when in bypass
586cba
+         * mode. (either by not configuring virtio-iommu driver or with
586cba
+         * "iommu=pt").  It will greatly reduce the total number of
586cba
+         * FlatViews of the system hence VM runs faster.
586cba
+         */
586cba
+        memory_region_init_alias(&sdev->bypass_mr, OBJECT(s),
586cba
+                                 "system", get_system_memory(), 0,
586cba
+                                 memory_region_size(get_system_memory()));
586cba
+
586cba
         memory_region_init_iommu(&sdev->iommu_mr, sizeof(sdev->iommu_mr),
586cba
                                  TYPE_VIRTIO_IOMMU_MEMORY_REGION,
586cba
                                  OBJECT(s), name,
586cba
                                  UINT64_MAX);
586cba
-        address_space_init(&sdev->as,
586cba
-                           MEMORY_REGION(&sdev->iommu_mr), TYPE_VIRTIO_IOMMU);
586cba
+
586cba
+        /*
586cba
+         * Hook both the containers under the root container, we
586cba
+         * switch between iommu & bypass MRs by enable/disable
586cba
+         * corresponding sub-containers
586cba
+         */
586cba
+        memory_region_add_subregion_overlap(&sdev->root, 0,
586cba
+                                            MEMORY_REGION(&sdev->iommu_mr),
586cba
+                                            0);
586cba
+        memory_region_add_subregion_overlap(&sdev->root, 0,
586cba
+                                            &sdev->bypass_mr, 0);
586cba
+
586cba
+        virtio_iommu_switch_address_space(sdev);
586cba
         g_free(name);
586cba
     }
586cba
     return &sdev->as;
586cba
@@ -343,6 +443,7 @@ static int virtio_iommu_attach(VirtIOIOMMU *s,
586cba
     uint32_t flags = le32_to_cpu(req->flags);
586cba
     VirtIOIOMMUDomain *domain;
586cba
     VirtIOIOMMUEndpoint *ep;
586cba
+    IOMMUDevice *sdev;
586cba
 
586cba
     trace_virtio_iommu_attach(domain_id, ep_id);
586cba
 
586cba
@@ -376,6 +477,8 @@ static int virtio_iommu_attach(VirtIOIOMMU *s,
586cba
     QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next);
586cba
 
586cba
     ep->domain = domain;
586cba
+    sdev = container_of(ep->iommu_mr, IOMMUDevice, iommu_mr);
586cba
+    virtio_iommu_switch_address_space(sdev);
586cba
 
586cba
     /* Replay domain mappings on the associated memory region */
586cba
     g_tree_foreach(domain->mappings, virtio_iommu_notify_map_cb,
586cba
@@ -888,6 +991,7 @@ static void virtio_iommu_set_config(VirtIODevice *vdev,
586cba
             return;
586cba
         }
586cba
         dev_config->bypass = in_config->bypass;
586cba
+        virtio_iommu_switch_address_space_all(dev);
586cba
     }
586cba
 
586cba
     trace_virtio_iommu_set_config(in_config->bypass);
586cba
@@ -1027,6 +1131,8 @@ static void virtio_iommu_system_reset(void *opaque)
586cba
      * system reset
586cba
      */
586cba
     s->config.bypass = s->boot_bypass;
586cba
+    virtio_iommu_switch_address_space_all(s);
586cba
+
586cba
 }
586cba
 
586cba
 static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
586cba
@@ -1043,6 +1149,11 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
586cba
                              virtio_iommu_handle_command);
586cba
     s->event_vq = virtio_add_queue(vdev, VIOMMU_DEFAULT_QUEUE_SIZE, NULL);
586cba
 
586cba
+    /*
586cba
+     * config.bypass is needed to get initial address space early, such as
586cba
+     * in vfio realize
586cba
+     */
586cba
+    s->config.bypass = s->boot_bypass;
586cba
     s->config.page_size_mask = TARGET_PAGE_MASK;
586cba
     s->config.input_range.end = UINT64_MAX;
586cba
     s->config.domain_range.end = UINT32_MAX;
586cba
diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-iommu.h
586cba
index 84391f8448..102eeefa73 100644
586cba
--- a/include/hw/virtio/virtio-iommu.h
586cba
+++ b/include/hw/virtio/virtio-iommu.h
586cba
@@ -37,6 +37,8 @@ typedef struct IOMMUDevice {
586cba
     int           devfn;
586cba
     IOMMUMemoryRegion  iommu_mr;
586cba
     AddressSpace  as;
586cba
+    MemoryRegion root;          /* The root container of the device */
586cba
+    MemoryRegion bypass_mr;     /* The alias of shared memory MR */
586cba
 } IOMMUDevice;
586cba
 
586cba
 typedef struct IOMMUPciBus {
586cba
-- 
586cba
2.31.1
586cba