Skip to contentSkip to conversation

VFIO integration

RFC PATCH Superseded Bharat Bhushan 2 participants 7 messages Jul 14, 2017 – Aug 21, 2017

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover VFIO integration

This patch series allows PCI pass-through using
virtio-iommu.

This series is based on:
- virtio-iommu specification written by Jean-Philippe Brucker [RFC 0/3] virtio-iommu: a paravirtualized IOMMU,

- virtio-iommu driver by Jean-Philippe Brucker [RFC PATCH linux] iommu: Add virtio-iommu driver

- virtio-iommu device emulation by Eric Augur.
[RFC v2 0/8] VIRTIO-IOMMU device

PCI device pass-through and virtio-net-pci is tested with these chages using dma-ops

This patch series does not implement RESV_MEM changes proposal by Jean-Philippe "https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg01796.html"

v1-v2:
- Added trace events
- removed vSMMU3 link in patch description

Bharat Bhushan (2):
target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route
virtio-iommu: vfio integration with virtio-iommu

 hw/virtio/trace-events           |   5 ++
 hw/virtio/virtio-iommu.c         | 133 +++++++++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-iommu.h |   6 ++
 target/arm/kvm.c                 |  25 ++++++++
 target/arm/trace-events          |   3 +
 5 files changed, 172 insertions(+)
Signature · 1 line
1.9.3
#1 lore raw

RFC PATCH v2 1/2 target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route

Translate msi address if device is behind virtio-iommu. This logic is similar to vSMMUv3/Intel iommu emulation.

This RFC patch does not handle the case where both vsmmuv3 and virtio-iommu are available.

---
v1-v2:
- Added trace events
- removed vSMMU3 link in patch description

 target/arm/kvm.c        | 25 +++++++++++++++++++++++++
 target/arm/trace-events |  3 +++
 2 files changed, 28 insertions(+)
target/arm/kvm.c +25 -0
index 4555468..5a28956 100644
@@ -21,7 +21,11 @@
 #include "kvm_arm.h"
 #include "cpu.h"
 #include "internals.h"
+#include "trace.h"
 #include "hw/arm/arm.h"
+#include "hw/pci/pci.h"
+#include "hw/pci/msi.h"
+#include "hw/virtio/virtio-iommu.h"
 #include "exec/memattrs.h"
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
@@ -611,6 +615,27 @@ int kvm_arm_vgic_probe(void)
 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
                              uint64_t address, uint32_t data, PCIDevice *dev)
 {
+    AddressSpace *as = pci_device_iommu_address_space(dev);
+    IOMMUTLBEntry entry;
+    IOMMUDevice *sdev;
+    VirtIOIOMMU *s;
+
+    if (as == &address_space_memory) {
+        return 0;
+    }
+
+    /* MSI doorbell address is translated by an IOMMU */
+    sdev = container_of(as, IOMMUDevice, as);
+    s = sdev->viommu;
+
+    entry = s->iommu_ops.translate(&sdev->iommu_mr, address, IOMMU_WO);
+
+    route->u.msi.address_lo = entry.translated_addr;
+    route->u.msi.address_hi = entry.translated_addr >> 32;
+
+    trace_kvm_arm_fixup_msi_route(address, sdev->devfn, sdev->iommu_mr.name,
+                                  entry.translated_addr);
+
     return 0;
 }
 
target/arm/trace-events +3 -0
index e21c84f..eff2822 100644
@@ -8,3 +8,6 @@ arm_gt_tval_write(int timer, uint64_t value) "gt_tval_write: timer %d value %" P
 arm_gt_ctl_write(int timer, uint64_t value) "gt_ctl_write: timer %d value %" PRIx64
 arm_gt_imask_toggle(int timer, int irqstate) "gt_ctl_write: timer %d IMASK toggle, new irqstate %d"
 arm_gt_cntvoff_write(uint64_t value) "gt_cntvoff_write: value %" PRIx64
+
+# target/arm/kvm.c
+kvm_arm_fixup_msi_route(uint64_t iova, uint32_t devid, const char *name, uint64_t gpa) "MSI addr = 0x%"PRIx64" is translated for devfn=%d through %s into 0x%"PRIx64
Signature · 1 line
1.9.3
#2 lore raw

Re: [Qemu-arm] [Qemu-devel] [RFC v2 PATCH 1/2] target/arm/kvm: Translate the MSI doorbell in kvm_arch_fixup_msi_route

Hi Bharat,

Quoted 2 lines · Bharat Bhushan

Translate msi address if device is behind virtio-iommu. This logic is similar to vSMMUv3/Intel iommu emulation.

Why Intel?

Quoted 3 lines

This RFC patch does not handle the case where both vsmmuv3 and virtio-iommu are available.

I think this should be hidden by the creation of a base vIOMMU object as initiated by Peter in: "[Qemu-devel] [RFC PATCH 0/8] IOMMU: introduce common IOMMUObject". I will try to rebase the virtio-iommu and vsmmuv3 series on this idea.

Thanks

Eric

Quoted 67 lines

---
v1-v2:
- Added trace events
- removed vSMMU3 link in patch description

 target/arm/kvm.c        | 25 +++++++++++++++++++++++++
 target/arm/trace-events |  3 +++
 2 files changed, 28 insertions(+)
target/arm/kvm.c +25 -0
index 4555468..5a28956 100644
@@ -21,7 +21,11 @@
 #include "kvm_arm.h"
 #include "cpu.h"
 #include "internals.h"
+#include "trace.h"
 #include "hw/arm/arm.h"
+#include "hw/pci/pci.h"
+#include "hw/pci/msi.h"
+#include "hw/virtio/virtio-iommu.h"
 #include "exec/memattrs.h"
 #include "exec/address-spaces.h"
 #include "hw/boards.h"
@@ -611,6 +615,27 @@ int kvm_arm_vgic_probe(void)
 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
                              uint64_t address, uint32_t data, PCIDevice *dev)
 {
+    AddressSpace *as = pci_device_iommu_address_space(dev);
+    IOMMUTLBEntry entry;
+    IOMMUDevice *sdev;
+    VirtIOIOMMU *s;
+
+    if (as == &address_space_memory) {
+        return 0;
+    }
+
+    /* MSI doorbell address is translated by an IOMMU */
+    sdev = container_of(as, IOMMUDevice, as);
+    s = sdev->viommu;
+
+    entry = s->iommu_ops.translate(&sdev->iommu_mr, address, IOMMU_WO);
+
+    route->u.msi.address_lo = entry.translated_addr;
+    route->u.msi.address_hi = entry.translated_addr >> 32;
+
+    trace_kvm_arm_fixup_msi_route(address, sdev->devfn, sdev->iommu_mr.name,
+                                  entry.translated_addr);
+
     return 0;
 }
 
target/arm/trace-events +3 -0
index e21c84f..eff2822 100644
@@ -8,3 +8,6 @@ arm_gt_tval_write(int timer, uint64_t value) "gt_tval_write: timer %d value %" P
 arm_gt_ctl_write(int timer, uint64_t value) "gt_ctl_write: timer %d value %" PRIx64
 arm_gt_imask_toggle(int timer, int irqstate) "gt_ctl_write: timer %d IMASK toggle, new irqstate %d"
 arm_gt_cntvoff_write(uint64_t value) "gt_cntvoff_write: value %" PRIx64
+
+# target/arm/kvm.c
+kvm_arm_fixup_msi_route(uint64_t iova, uint32_t devid, const char *name, uint64_t gpa) "MSI addr = 0x%"PRIx64" is translated for devfn=%d through %s into 0x%"PRIx64
#3 lore raw

RFC PATCH v2 2/2 virtio-iommu: vfio integration with virtio-iommu

In reply to Bharat Bhushan

This patch allows virtio-iommu protection for PCI
device-passthrough.

MSI region is mapped by current version of virtio-iommu driver. This MSI region mapping in not getting pushed on hw iommu vfio_get_vaddr() allows only ram-region. This RFC patch needed to be improved.

---
v1-v2:
- Added trace events

 hw/virtio/trace-events           |   5 ++
 hw/virtio/virtio-iommu.c         | 133 +++++++++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-iommu.h |   6 ++
 3 files changed, 144 insertions(+)
hw/virtio/trace-events +5 -0
index 9196b63..3a3968b 100644
@@ -39,3 +39,8 @@ virtio_iommu_unmap_left_interval(uint64_t low, uint64_t high, uint64_t next_low,
 virtio_iommu_unmap_right_interval(uint64_t low, uint64_t high, uint64_t next_low, uint64_t next_high) "Unmap right [0x%"PRIx64",0x%"PRIx64"], new interval=[0x%"PRIx64",0x%"PRIx64"]"
 virtio_iommu_unmap_inc_interval(uint64_t low, uint64_t high) "Unmap inc [0x%"PRIx64",0x%"PRIx64"]"
 virtio_iommu_translate_result(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
+virtio_iommu_notify_flag_add(const char *iommu) "Add virtio-iommu notifier node for memory region %s"
+virtio_iommu_notify_flag_del(const char *iommu) "Del virtio-iommu notifier node for memory region %s"
+virtio_iommu_remap(hwaddr iova, hwaddr pa, hwaddr size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""
+virtio_iommu_map_region(hwaddr iova, hwaddr paddr, hwaddr map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""
+virtio_iommu_unmap_region(hwaddr iova, hwaddr paddr, hwaddr map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""
hw/virtio/virtio-iommu.c +133 -0

185 lines not on this page · raw message

include/hw/virtio/virtio-iommu.h +6 -0
index 2259413..76c758d 100644
@@ -44,6 +44,11 @@ typedef struct IOMMUPciBus {
     IOMMUDevice  *pbdev[0]; /* Parent array is sparse, so dynamically alloc */
 } IOMMUPciBus;
 
+typedef struct VirtioIOMMUNotifierNode {
+    IOMMUDevice *iommu_dev;
+    QLIST_ENTRY(VirtioIOMMUNotifierNode) next;
+} VirtioIOMMUNotifierNode;
+
 typedef struct VirtIOIOMMU {
     VirtIODevice parent_obj;
     VirtQueue *vq;
@@ -55,6 +60,7 @@ typedef struct VirtIOIOMMU {
     GTree *address_spaces;
     QemuMutex mutex;
     GTree *devices;
+    QLIST_HEAD(, VirtioIOMMUNotifierNode) notifiers_list;
 } VirtIOIOMMU;
 
 #endif
Signature · 1 line
1.9.3
#4 lore raw

Re: [Qemu-arm] [Qemu-devel] [RFC v2 PATCH 2/2] virtio-iommu: vfio integration with virtio-iommu

Hi Bharat,

Quoted 6 lines · Bharat Bhushan

This patch allows virtio-iommu protection for PCI
device-passthrough.

MSI region is mapped by current version of virtio-iommu driver. This MSI region mapping in not getting pushed on hw iommu vfio_get_vaddr() allows only ram-region.

Why is it an issue. As far as I understand this is not needed actually as the guest MSI doorbell is not used by the host.
This RFC patch needed

Quoted 30 lines

to be improved.

---
v1-v2:
- Added trace events

 hw/virtio/trace-events           |   5 ++
 hw/virtio/virtio-iommu.c         | 133 +++++++++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-iommu.h |   6 ++
 3 files changed, 144 insertions(+)
hw/virtio/trace-events +5 -0
index 9196b63..3a3968b 100644
@@ -39,3 +39,8 @@ virtio_iommu_unmap_left_interval(uint64_t low, uint64_t high, uint64_t next_low,
 virtio_iommu_unmap_right_interval(uint64_t low, uint64_t high, uint64_t next_low, uint64_t next_high) "Unmap right [0x%"PRIx64",0x%"PRIx64"], new interval=[0x%"PRIx64",0x%"PRIx64"]"
 virtio_iommu_unmap_inc_interval(uint64_t low, uint64_t high) "Unmap inc [0x%"PRIx64",0x%"PRIx64"]"
 virtio_iommu_translate_result(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
+virtio_iommu_notify_flag_add(const char *iommu) "Add virtio-iommu notifier node for memory region %s"
+virtio_iommu_notify_flag_del(const char *iommu) "Del virtio-iommu notifier node for memory region %s"
+virtio_iommu_remap(hwaddr iova, hwaddr pa, hwaddr size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""
+virtio_iommu_map_region(hwaddr iova, hwaddr paddr, hwaddr map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""
+virtio_iommu_unmap_region(hwaddr iova, hwaddr paddr, hwaddr map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""
hw/virtio/virtio-iommu.c +0 -0
index cd188fc..61f33cb 100644

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 133

Quoted 5 lines
hw/virtio/virtio-iommu.c +2 -0
@@ -129,6 +129,48 @@ static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
     }
 }
 
+static void virtio_iommu_map_region(VirtIOIOMMU *s, hwaddr iova, hwaddr paddr,
+                                    hwaddr size, int map)

bool map?

the function name is a bit misleading to me and does not really explain what the function does. It "notifies" so why not using something like virtio_iommu_map_notify and virtio_iommu_unmap_notify. I tend to think having separate proto is cleaner and more standard.

Binding should happen on a specific IOMMUmemoryRegion (see next comment).

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 137

Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+{
+    VirtioIOMMUNotifierNode *node;
+    IOMMUTLBEntry entry;
+    uint64_t map_size = (1 << 12);

TODO: handle something else than 4K page.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 145

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+    int npages;
+    int i;
+
+    npages = size / map_size;
+    entry.target_as = &address_space_memory;
+    entry.addr_mask = map_size - 1;
+
+    for (i = 0; i < npages; i++) {

Although I understand we currently fail checking the consistency between pIOMMU and vIOMMU page sizes, this will be very slow for guest DPDK use case where hugepages are used.

Why not directly using the full size? vfio_iommu_map_notify will report errors if vfio_dma_map/unmap() fail.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 158

Quoted 13 lines
hw/virtio/virtio-iommu.c +13 -0
+        entry.iova = iova + (i * map_size);
+        if (map) {
+                trace_virtio_iommu_map_region(iova, paddr, map_size);
+                entry.perm = IOMMU_RW;
+                entry.translated_addr = paddr + (i * map_size);
+        } else {
+                trace_virtio_iommu_unmap_region(iova, paddr, map_size);
+                entry.perm = IOMMU_NONE;
+                entry.translated_addr = 0;
+        }
+
+        QLIST_FOREACH(node, &s->notifiers_list, next) {
+            memory_region_notify_iommu(&node->iommu_dev->iommu_mr, entry);

So as discussed this will notify *all* IOMMU memory regions and all their notifiers which is not what we want. You may have a look at vsmmuv3 v6 (or intel_iommu) where smmuv3_context_device_invalidate retrieves the mr from the sid.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 169

Quoted 11 lines
hw/virtio/virtio-iommu.c +11 -0
+        }
+    }
+}
+
+static gboolean virtio_iommu_unmap_single(gpointer key, gpointer value,
+                                          gpointer data)
+{
+    viommu_mapping *mapping = (viommu_mapping *) value;
+    VirtIOIOMMU *s = (VirtIOIOMMU *) data;
+
+    virtio_iommu_map_region(s, mapping->virt_addr, 0, mapping->size, 0);

paddr=0? mapping->phys_addr as the trace() will be misleading. But as mentioned earlier better use unmap() separate function.

Quoted 20 lines
hw/virtio/virtio-iommu.c +9 -0
+
+    return true;
+}
+
 static int virtio_iommu_attach(VirtIOIOMMU *s,
                                struct virtio_iommu_req_attach *req)
 {
@@ -170,10 +212,26 @@ static int virtio_iommu_detach(VirtIOIOMMU *s,
 {
     uint32_t devid = le32_to_cpu(req->device);
     uint32_t reserved = le32_to_cpu(req->reserved);
+    viommu_dev *dev;
     int ret;
 
     trace_virtio_iommu_detach(devid, reserved);
 
+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(devid));
+    if (!dev || !dev->as) {
+        return -EINVAL;
+    }

hw/virtio/virtio-iommu.c static int virtio_iommu_detach(VirtIOIOMMU *s, · line 233

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+
+    dev->as->nr_devices--;
+
+    /* Unmap all if this is last device detached */
+    if (dev->as->nr_devices == 0) {
+        g_tree_foreach(dev->as->mappings, virtio_iommu_unmap_single, s);
+
+        g_tree_remove(s->address_spaces, GUINT_TO_POINTER(dev->as->id));
+        g_tree_destroy(dev->as->mappings);
+    }

so this should be rebased on new ref count code.

Quoted 45 lines
hw/virtio/virtio-iommu.c +23 -0
+
     ret = g_tree_remove(s->devices, GUINT_TO_POINTER(devid));
 
     return ret ? VIRTIO_IOMMU_S_OK : VIRTIO_IOMMU_S_INVAL;
@@ -217,6 +275,7 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
 
     g_tree_insert(as->mappings, interval, mapping);
 
+    virtio_iommu_map_region(s, virt_addr, phys_addr, size, 1);
     return VIRTIO_IOMMU_S_OK;
 }
 
@@ -267,7 +326,9 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
         } else {
             break;
         }
+
         if (interval.low >= interval.high) {
+            virtio_iommu_map_region(s, virt_addr, 0, size, 0);
             return VIRTIO_IOMMU_S_OK;
         } else {
             mapping = g_tree_lookup(as->mappings, (gpointer)&interval);
@@ -410,6 +471,37 @@ static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq)
     }
 }
 
+static void virtio_iommu_notify_flag_changed(MemoryRegion *iommu,
+                                          IOMMUNotifierFlag old,
+                                          IOMMUNotifierFlag new)
+{
+    IOMMUDevice *sdev = container_of(iommu, IOMMUDevice, iommu_mr);
+    VirtIOIOMMU *s = sdev->viommu;
+    VirtioIOMMUNotifierNode *node = NULL;
+    VirtioIOMMUNotifierNode *next_node = NULL;
+
+    if (old == IOMMU_NOTIFIER_NONE) {
+        trace_virtio_iommu_notify_flag_add(iommu->name);
+        node = g_malloc0(sizeof(*node));
+        node->iommu_dev = sdev;
+        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
+        return;
+    }
+
+    /* update notifier node with new flags */
+    QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) {

hw/virtio/virtio-iommu.c static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) · line 502

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+        if (node->iommu_dev == sdev) {
+            if (new == IOMMU_NOTIFIER_NONE) {
+                trace_virtio_iommu_notify_flag_del(iommu->name);
+                QLIST_REMOVE(node, next);
+                g_free(node);
+            }
+            return;
+        }
+    }
+}

I think all that mechanics should be factorized somewhere else as all vIOMMUs use that but this goes beyond the scope of this series.

Quoted 35 lines
hw/virtio/virtio-iommu.c +28 -0
+
+
 static IOMMUTLBEntry virtio_iommu_translate(MemoryRegion *mr, hwaddr addr,
                                             IOMMUAccessFlags flag)
 {
@@ -523,11 +615,50 @@ static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
     return (ua > ub) - (ua < ub);
 }
 
+static gboolean virtio_iommu_remap(gpointer key, gpointer value, gpointer data)
+{
+    viommu_mapping *mapping = (viommu_mapping *) value;
+    VirtIOIOMMU *s = (VirtIOIOMMU *) data;
+
+    trace_virtio_iommu_remap(mapping->virt_addr, mapping->phys_addr,
+                             mapping->size);
+    /* unmap previous entry and map again */
+    virtio_iommu_map_region(s, mapping->virt_addr, 0, mapping->size, 0);
+
+    virtio_iommu_map_region(s, mapping->virt_addr, mapping->phys_addr,
+                            mapping->size, 1);
+    return true;
+}
+
+static void virtio_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n)
+{
+    IOMMUDevice *sdev = container_of(mr, IOMMUDevice, iommu_mr);
+    VirtIOIOMMU *s = sdev->viommu;
+    uint32_t sid;
+    viommu_dev *dev;
+
+    sid = smmu_get_sid(sdev);
+
+    qemu_mutex_lock(&s->mutex);
+

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 653

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(sid));
+    if (!dev) {
+        goto unlock;
+    }
+
+    g_tree_foreach(dev->as->mappings, virtio_iommu_remap, s);
+
+unlock:
+    qemu_mutex_unlock(&s->mutex);
+    return;

not needed

Thanks

Eric

Quoted 45 lines
hw/virtio/virtio-iommu.c +5 -0
+}
+
 static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOIOMMU *s = VIRTIO_IOMMU(dev);
 
+    QLIST_INIT(&s->notifiers_list);
     virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU,
                 sizeof(struct virtio_iommu_config));
 
@@ -538,6 +669,8 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
     s->config.input_range.end = -1UL;
 
     s->iommu_ops.translate = virtio_iommu_translate;
+    s->iommu_ops.notify_flag_changed = virtio_iommu_notify_flag_changed;
+    s->iommu_ops.replay = virtio_iommu_replay;
     memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num));
     s->as_by_busptr = g_hash_table_new_full(as_uint64_hash,
                                             as_uint64_equal,
include/hw/virtio/virtio-iommu.h +6 -0
index 2259413..76c758d 100644
@@ -44,6 +44,11 @@ typedef struct IOMMUPciBus {
     IOMMUDevice  *pbdev[0]; /* Parent array is sparse, so dynamically alloc */
 } IOMMUPciBus;
 
+typedef struct VirtioIOMMUNotifierNode {
+    IOMMUDevice *iommu_dev;
+    QLIST_ENTRY(VirtioIOMMUNotifierNode) next;
+} VirtioIOMMUNotifierNode;
+
 typedef struct VirtIOIOMMU {
     VirtIODevice parent_obj;
     VirtQueue *vq;
@@ -55,6 +60,7 @@ typedef struct VirtIOIOMMU {
     GTree *address_spaces;
     QemuMutex mutex;
     GTree *devices;
+    QLIST_HEAD(, VirtioIOMMUNotifierNode) notifiers_list;
 } VirtIOIOMMU;
 
 #endif
#5 lore raw

Re: [Qemu-arm] [Qemu-devel] [RFC v2 PATCH 2/2] virtio-iommu: vfio integration with virtio-iommu

Quoted 101 lines

-----Original Message-----
From: Auger Eric [mailto:[email protected]]
Sent: Thursday, August 17, 2017 9:03 PM
To: Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [Qemu-devel] [RFC v2 PATCH 2/2] virtio-iommu: vfio integration with virtio-iommu

Hi Bharat,

Quoted 5 lines · Bharat Bhushan

This patch allows virtio-iommu protection for PCI device-passthrough.

MSI region is mapped by current version of virtio-iommu driver. This MSI region mapping in not getting pushed on hw iommu vfio_get_vaddr() allows only ram-region.

Why is it an issue. As far as I understand this is not needed actually as the guest MSI doorbell is not used by the host.
This RFC patch needed

Quoted 9 lines

to be improved.

---
v1-v2:
- Added trace events

 hw/virtio/trace-events           |   5 ++
 hw/virtio/virtio-iommu.c         | 133

+++++++++++++++++++++++++++++++++++++++

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 39

Quoted 9 lines
 include/hw/virtio/virtio-iommu.h |   6 ++
 3 files changed, 144 insertions(+)
hw/virtio/trace-events index renamed from hw/virtio/trace-events +0 -0

9196b63..3a3968b 100644

hw/virtio/trace-events +0 -0
@@ -39,3 +39,8 @@ virtio_iommu_unmap_left_interval(uint64_t low,
 uint64_t high, uint64_t next_low,

virtio_iommu_unmap_right_interval(uint64_t low, uint64_t high, uint64_t next_low, uint64_t next_high) "Unmap right [0x%"PRIx64",0x%"PRIx64"], new interval=[0x%"PRIx64",0x%"PRIx64"]"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 40

Quoted 1 line
hw/virtio/trace-events +0 -0
 virtio_iommu_unmap_inc_interval(uint64_t low, uint64_t high) "Unmap

inc [0x%"PRIx64",0x%"PRIx64"]"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 41

Quoted 1 line
hw/virtio/trace-events +0 -0
 virtio_iommu_translate_result(uint64_t virt_addr, uint64_t phys_addr,

uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 42

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_notify_flag_add(const char *iommu) "Add virtio-iommu

notifier node for memory region %s"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 43

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_notify_flag_del(const char *iommu) "Del virtio-iommu

notifier node for memory region %s"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 44

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_remap(hwaddr iova, hwaddr pa, hwaddr size)

"iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 45

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_map_region(hwaddr iova, hwaddr paddr, hwaddr

map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 46

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_unmap_region(hwaddr iova, hwaddr paddr, hwaddr

map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a,

Quoted 5 lines
hw/virtio/virtio-iommu.c index renamed from hw/virtio/virtio-iommu.c +0 -0

cd188fc..61f33cb 100644

hw/virtio/virtio-iommu.c +0 -0
@@ -129,6 +129,48 @@ static gint interval_cmp(gconstpointer a,

gconstpointer b, gpointer user_data)

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, · line 132

Quoted 4 lines
hw/virtio/virtio-iommu.c +1 -0
     }
 }
 
+static void virtio_iommu_map_region(VirtIOIOMMU *s, hwaddr iova,

hwaddr paddr,

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 133

Quoted 1 line
hw/virtio/virtio-iommu.c +1 -0
+                                    hwaddr size, int map)

bool map?

the function name is a bit misleading to me and does not really explain what the function does. It "notifies" so why not using something like virtio_iommu_map_notify and virtio_iommu_unmap_notify. I tend to think having separate proto is cleaner and more standard.

Binding should happen on a specific IOMMUmemoryRegion (see next comment).

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 137

Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+{
+    VirtioIOMMUNotifierNode *node;
+    IOMMUTLBEntry entry;
+    uint64_t map_size = (1 << 12);

TODO: handle something else than 4K page.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 145

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+    int npages;
+    int i;
+
+    npages = size / map_size;
+    entry.target_as = &address_space_memory;
+    entry.addr_mask = map_size - 1;
+
+    for (i = 0; i < npages; i++) {

Although I understand we currently fail checking the consistency between pIOMMU and vIOMMU page sizes, this will be very slow for guest DPDK use case where hugepages are used.

Quoted 2 lines

Why not directly using the full size? vfio_iommu_map_notify will report errors if vfio_dma_map/unmap() fail.

Yes, just for understanding, VFIO/IOMMU will map at page granularity and QEMU rely on that?

Quoted 14 lines
Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+        entry.iova = iova + (i * map_size);
+        if (map) {
+                trace_virtio_iommu_map_region(iova, paddr, map_size);
+                entry.perm = IOMMU_RW;

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+                entry.translated_addr = paddr + (i * map_size);
+        } else {
+                trace_virtio_iommu_unmap_region(iova, paddr, map_size);
+                entry.perm = IOMMU_NONE;
+                entry.translated_addr = 0;
+        }
+
+        QLIST_FOREACH(node, &s->notifiers_list, next) {
+            memory_region_notify_iommu(&node->iommu_dev->iommu_mr,
+ entry);
Quoted 4 lines

So as discussed this will notify *all* IOMMU memory regions and all their notifiers which is not what we want. You may have a look at vsmmuv3 v6 (or intel_iommu) where smmuv3_context_device_invalidate retrieves the mr from the sid.

Yes, this is one of the reason why multiple interfaces fails to work ☹ Will fix this.

Quoted 148 lines

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 6 lines
hw/virtio/virtio-iommu.c +5 -0
+        }
+    }
+}
+
+static gboolean virtio_iommu_unmap_single(gpointer key, gpointer

value,

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 6 lines
hw/virtio/virtio-iommu.c +6 -0
+                                          gpointer data) {
+    viommu_mapping *mapping = (viommu_mapping *) value;
+    VirtIOIOMMU *s = (VirtIOIOMMU *) data;
+
+    virtio_iommu_map_region(s, mapping->virt_addr, 0, mapping->size,
+ 0);

paddr=0? mapping->phys_addr as the trace() will be misleading. But as mentioned earlier better use unmap() separate function.

Quoted 7 lines
hw/virtio/virtio-iommu.c +4 -0
+
+    return true;
+}
+
 static int virtio_iommu_attach(VirtIOIOMMU *s,
                                struct virtio_iommu_req_attach *req)

{ @@ -170,10 +212,26 @@ static int virtio_iommu_detach(VirtIOIOMMU

*s,

Quoted 22 lines
{
     uint32_t devid = le32_to_cpu(req->device);
     uint32_t reserved = le32_to_cpu(req->reserved);
hw/virtio/virtio-iommu.c +14 -0
+    viommu_dev *dev;
     int ret;
 
     trace_virtio_iommu_detach(devid, reserved);
 
+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(devid));
+    if (!dev || !dev->as) {
+        return -EINVAL;
+    }
+
+    dev->as->nr_devices--;
+
+    /* Unmap all if this is last device detached */
+    if (dev->as->nr_devices == 0) {
+        g_tree_foreach(dev->as->mappings, virtio_iommu_unmap_single,
+ s);
+
+        g_tree_remove(s->address_spaces, GUINT_TO_POINTER(dev->as-

id));

hw/virtio/virtio-iommu.c static int virtio_iommu_detach(VirtIOIOMMU *s, · line 233

Quoted 2 lines
hw/virtio/virtio-iommu.c +2 -0
+        g_tree_destroy(dev->as->mappings);
+    }

so this should be rebased on new ref count code.

Quoted 4 lines

+
ret = g_tree_remove(s->devices, GUINT_TO_POINTER(devid));

return ret ? VIRTIO_IOMMU_S_OK : VIRTIO_IOMMU_S_INVAL; @@ -

217,6

Quoted 18 lines

+275,7 @@ static int virtio_iommu_map(VirtIOIOMMU *s,

     g_tree_insert(as->mappings, interval, mapping);
hw/virtio/virtio-iommu.c +3 -0
+    virtio_iommu_map_region(s, virt_addr, phys_addr, size, 1);
     return VIRTIO_IOMMU_S_OK;
 }
@@ -267,7 +326,9 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
         } else {
             break;
         }
+
         if (interval.low >= interval.high) {
+            virtio_iommu_map_region(s, virt_addr, 0, size, 0);
             return VIRTIO_IOMMU_S_OK;
         } else {
             mapping = g_tree_lookup(as->mappings,
Quoted 1 line

(gpointer)&interval); @@ -410,6 +471,37 @@ static void

virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq)

hw/virtio/virtio-iommu.c static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq)

Quoted 7 lines
     }

}

hw/virtio/virtio-iommu.c +4 -0
+static void virtio_iommu_notify_flag_changed(MemoryRegion *iommu,
+                                          IOMMUNotifierFlag old,
+                                          IOMMUNotifierFlag new) {
+    IOMMUDevice *sdev = container_of(iommu, IOMMUDevice,

iommu_mr);

Quoted 14 lines
hw/virtio/virtio-iommu.c +14 -0
+    VirtIOIOMMU *s = sdev->viommu;
+    VirtioIOMMUNotifierNode *node = NULL;
+    VirtioIOMMUNotifierNode *next_node = NULL;
+
+    if (old == IOMMU_NOTIFIER_NONE) {
+        trace_virtio_iommu_notify_flag_add(iommu->name);
+        node = g_malloc0(sizeof(*node));
+        node->iommu_dev = sdev;
+        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
+        return;
+    }
+
+    /* update notifier node with new flags */
+    QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) {

hw/virtio/virtio-iommu.c static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) · line 502

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+        if (node->iommu_dev == sdev) {
+            if (new == IOMMU_NOTIFIER_NONE) {
+                trace_virtio_iommu_notify_flag_del(iommu->name);
+                QLIST_REMOVE(node, next);
+                g_free(node);
+            }
+            return;
+        }
+    }
+}

I think all that mechanics should be factorized somewhere else as all vIOMMUs use that but this goes beyond the scope of this series.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 3 lines
hw/virtio/virtio-iommu.c +2 -0
+
+
 static IOMMUTLBEntry virtio_iommu_translate(MemoryRegion *mr,

hwaddr addr,

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer

Quoted 2 lines
                                             IOMMUAccessFlags flag)  {
hw/virtio/virtio-iommu.c +0 -0
@@ -523,11 +615,50 @@ static gint int_cmp(gconstpointer a, gconstpointer

b, gpointer user_data)

Quoted 11 lines
hw/virtio/virtio-iommu.c +8 -0
     return (ua > ub) - (ua < ub);
 }
 
+static gboolean virtio_iommu_remap(gpointer key, gpointer value,
+gpointer data) {
+    viommu_mapping *mapping = (viommu_mapping *) value;
+    VirtIOIOMMU *s = (VirtIOIOMMU *) data;
+
+    trace_virtio_iommu_remap(mapping->virt_addr, mapping->phys_addr,
+                             mapping->size);
+    /* unmap previous entry and map again */

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 633

Quoted 10 lines
hw/virtio/virtio-iommu.c +9 -0
+    virtio_iommu_map_region(s, mapping->virt_addr, 0, mapping->size,
+ 0);
+
+    virtio_iommu_map_region(s, mapping->virt_addr, mapping-
 phys_addr,
+                            mapping->size, 1);
+    return true;
+}
+
+static void virtio_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n)

{

Quoted 12 lines
hw/virtio/virtio-iommu.c +12 -0
+    IOMMUDevice *sdev = container_of(mr, IOMMUDevice, iommu_mr);
+    VirtIOIOMMU *s = sdev->viommu;
+    uint32_t sid;
+    viommu_dev *dev;
+
+    sid = smmu_get_sid(sdev);
+
+    qemu_mutex_lock(&s->mutex);
+
+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(sid));
+    if (!dev) {
+        goto unlock;
Quoted 8 lines

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 653

Quoted 7 lines
hw/virtio/virtio-iommu.c +7 -0
+    }
+
+    g_tree_foreach(dev->as->mappings, virtio_iommu_remap, s);
+
+unlock:
+    qemu_mutex_unlock(&s->mutex);
+    return;

not needed

Thanks
-Bharat

Quoted 52 lines

Thanks

Eric

hw/virtio/virtio-iommu.c static void

Quoted 12 lines
hw/virtio/virtio-iommu.c +3 -0
+}
+
 static void virtio_iommu_device_realize(DeviceState *dev, Error
 **errp)  {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOIOMMU *s = VIRTIO_IOMMU(dev);
 
+    QLIST_INIT(&s->notifiers_list);
     virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU,
                 sizeof(struct virtio_iommu_config));
@@ -538,6 +669,8 @@ static void

virtio_iommu_device_realize(DeviceState *dev, Error **errp)

hw/virtio/virtio-iommu.c static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)

Quoted 4 lines
hw/virtio/virtio-iommu.c +1 -0
     s->config.input_range.end = -1UL;
 
     s->iommu_ops.translate = virtio_iommu_translate;
+    s->iommu_ops.notify_flag_changed =

virtio_iommu_notify_flag_changed;

Quoted 30 lines
hw/virtio/virtio-iommu.c +1 -1
+    s->iommu_ops.replay = virtio_iommu_replay;
     memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num));
     s->as_by_busptr = g_hash_table_new_full(as_uint64_hash,
                                             as_uint64_equal, diff
--git a/include/hw/virtio/virtio-iommu.h

b/include/hw/virtio/virtio-iommu.h
index 2259413..76c758d 100644

include/hw/virtio/virtio-iommu.h +6 -0
@@ -44,6 +44,11 @@ typedef struct IOMMUPciBus {
     IOMMUDevice  *pbdev[0]; /* Parent array is sparse, so dynamically
 alloc */  } IOMMUPciBus;
 
+typedef struct VirtioIOMMUNotifierNode {
+    IOMMUDevice *iommu_dev;
+    QLIST_ENTRY(VirtioIOMMUNotifierNode) next; }
+VirtioIOMMUNotifierNode;
+
 typedef struct VirtIOIOMMU {
     VirtIODevice parent_obj;
     VirtQueue *vq;
@@ -55,6 +60,7 @@ typedef struct VirtIOIOMMU {
     GTree *address_spaces;
     QemuMutex mutex;
     GTree *devices;
+    QLIST_HEAD(, VirtioIOMMUNotifierNode) notifiers_list;
 } VirtIOIOMMU;
 
 #endif
#6 lore raw

Re: [Qemu-arm] [Qemu-devel] [RFC v2 PATCH 2/2] virtio-iommu: vfio integration with virtio-iommu

In reply to Auger Eric

Hi Eric,

Quoted 115 lines

-----Original Message-----
From: Auger Eric [mailto:[email protected]]
Sent: Thursday, August 17, 2017 9:03 PM
To: Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [Qemu-devel] [RFC v2 PATCH 2/2] virtio-iommu: vfio integration with virtio-iommu

Hi Bharat,

Quoted 5 lines · Bharat Bhushan

This patch allows virtio-iommu protection for PCI device-passthrough.

MSI region is mapped by current version of virtio-iommu driver. This MSI region mapping in not getting pushed on hw iommu vfio_get_vaddr() allows only ram-region.

Why is it an issue. As far as I understand this is not needed actually as the guest MSI doorbell is not used by the host.
This RFC patch needed

Quoted 9 lines

to be improved.

---
v1-v2:
- Added trace events

 hw/virtio/trace-events           |   5 ++
 hw/virtio/virtio-iommu.c         | 133

+++++++++++++++++++++++++++++++++++++++

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 39

Quoted 9 lines
 include/hw/virtio/virtio-iommu.h |   6 ++
 3 files changed, 144 insertions(+)
hw/virtio/trace-events index renamed from hw/virtio/trace-events +0 -0

9196b63..3a3968b 100644

hw/virtio/trace-events +0 -0
@@ -39,3 +39,8 @@ virtio_iommu_unmap_left_interval(uint64_t low,
 uint64_t high, uint64_t next_low,

virtio_iommu_unmap_right_interval(uint64_t low, uint64_t high, uint64_t next_low, uint64_t next_high) "Unmap right [0x%"PRIx64",0x%"PRIx64"], new interval=[0x%"PRIx64",0x%"PRIx64"]"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 40

Quoted 1 line
hw/virtio/trace-events +0 -0
 virtio_iommu_unmap_inc_interval(uint64_t low, uint64_t high) "Unmap

inc [0x%"PRIx64",0x%"PRIx64"]"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 41

Quoted 1 line
hw/virtio/trace-events +0 -0
 virtio_iommu_translate_result(uint64_t virt_addr, uint64_t phys_addr,

uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 42

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_notify_flag_add(const char *iommu) "Add virtio-iommu

notifier node for memory region %s"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 43

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_notify_flag_del(const char *iommu) "Del virtio-iommu

notifier node for memory region %s"

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 44

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_remap(hwaddr iova, hwaddr pa, hwaddr size)

"iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 45

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_map_region(hwaddr iova, hwaddr paddr, hwaddr

map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""

hw/virtio/trace-events virtio_iommu_unmap_left_interval(uint64_t low, · line 46

Quoted 1 line
hw/virtio/trace-events +1 -0
+virtio_iommu_unmap_region(hwaddr iova, hwaddr paddr, hwaddr

map_size) "iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64""

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a,

Quoted 5 lines
hw/virtio/virtio-iommu.c index renamed from hw/virtio/virtio-iommu.c +0 -0

cd188fc..61f33cb 100644

hw/virtio/virtio-iommu.c +0 -0
@@ -129,6 +129,48 @@ static gint interval_cmp(gconstpointer a,

gconstpointer b, gpointer user_data)

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, · line 132

Quoted 4 lines
hw/virtio/virtio-iommu.c +1 -0
     }
 }
 
+static void virtio_iommu_map_region(VirtIOIOMMU *s, hwaddr iova,

hwaddr paddr,

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 133

Quoted 1 line
hw/virtio/virtio-iommu.c +1 -0
+                                    hwaddr size, int map)

bool map?

the function name is a bit misleading to me and does not really explain what the function does. It "notifies" so why not using something like virtio_iommu_map_notify and virtio_iommu_unmap_notify. I tend to think having separate proto is cleaner and more standard.

Binding should happen on a specific IOMMUmemoryRegion (see next comment).

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 137

Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+{
+    VirtioIOMMUNotifierNode *node;
+    IOMMUTLBEntry entry;
+    uint64_t map_size = (1 << 12);

TODO: handle something else than 4K page.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 145

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+    int npages;
+    int i;
+
+    npages = size / map_size;
+    entry.target_as = &address_space_memory;
+    entry.addr_mask = map_size - 1;
+
+    for (i = 0; i < npages; i++) {

Although I understand we currently fail checking the consistency between pIOMMU and vIOMMU page sizes, this will be very slow for guest DPDK use case where hugepages are used.

Why not directly using the full size? vfio_iommu_map_notify will report errors if vfio_dma_map/unmap() fail.

Quoted 4 lines
hw/virtio/virtio-iommu.c +4 -0
+        entry.iova = iova + (i * map_size);
+        if (map) {
+                trace_virtio_iommu_map_region(iova, paddr, map_size);
+                entry.perm = IOMMU_RW;

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+                entry.translated_addr = paddr + (i * map_size);
+        } else {
+                trace_virtio_iommu_unmap_region(iova, paddr, map_size);
+                entry.perm = IOMMU_NONE;
+                entry.translated_addr = 0;
+        }
+
+        QLIST_FOREACH(node, &s->notifiers_list, next) {
+            memory_region_notify_iommu(&node->iommu_dev->iommu_mr,
+ entry);
Quoted 4 lines

So as discussed this will notify *all* IOMMU memory regions and all their notifiers which is not what we want. You may have a look at vsmmuv3 v6 (or intel_iommu) where smmuv3_context_device_invalidate retrieves the mr from the sid.

I sent out next version of the patch and I took different approach, I assumed that device-id in virtio_iommu_attach is stream-id, and same as requested-id. This assumption is because the "iommu-map" property maps 1:1. Looking forward your view about this.

Hopefully I addressed other comments and fixed/code-rework planned.

Thanks
-Bharat

Quoted 208 lines

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 6 lines
hw/virtio/virtio-iommu.c +5 -0
+        }
+    }
+}
+
+static gboolean virtio_iommu_unmap_single(gpointer key, gpointer

value,

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 6 lines
hw/virtio/virtio-iommu.c +6 -0
+                                          gpointer data) {
+    viommu_mapping *mapping = (viommu_mapping *) value;
+    VirtIOIOMMU *s = (VirtIOIOMMU *) data;
+
+    virtio_iommu_map_region(s, mapping->virt_addr, 0, mapping->size,
+ 0);

paddr=0? mapping->phys_addr as the trace() will be misleading. But as mentioned earlier better use unmap() separate function.

Quoted 7 lines
hw/virtio/virtio-iommu.c +4 -0
+
+    return true;
+}
+
 static int virtio_iommu_attach(VirtIOIOMMU *s,
                                struct virtio_iommu_req_attach *req)

{ @@ -170,10 +212,26 @@ static int virtio_iommu_detach(VirtIOIOMMU

*s,

Quoted 22 lines
{
     uint32_t devid = le32_to_cpu(req->device);
     uint32_t reserved = le32_to_cpu(req->reserved);
hw/virtio/virtio-iommu.c +14 -0
+    viommu_dev *dev;
     int ret;
 
     trace_virtio_iommu_detach(devid, reserved);
 
+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(devid));
+    if (!dev || !dev->as) {
+        return -EINVAL;
+    }
+
+    dev->as->nr_devices--;
+
+    /* Unmap all if this is last device detached */
+    if (dev->as->nr_devices == 0) {
+        g_tree_foreach(dev->as->mappings, virtio_iommu_unmap_single,
+ s);
+
+        g_tree_remove(s->address_spaces, GUINT_TO_POINTER(dev->as-

id));

hw/virtio/virtio-iommu.c static int virtio_iommu_detach(VirtIOIOMMU *s, · line 233

Quoted 2 lines
hw/virtio/virtio-iommu.c +2 -0
+        g_tree_destroy(dev->as->mappings);
+    }

so this should be rebased on new ref count code.

Quoted 4 lines

+
ret = g_tree_remove(s->devices, GUINT_TO_POINTER(devid));

return ret ? VIRTIO_IOMMU_S_OK : VIRTIO_IOMMU_S_INVAL; @@ -

217,6

Quoted 18 lines

+275,7 @@ static int virtio_iommu_map(VirtIOIOMMU *s,

     g_tree_insert(as->mappings, interval, mapping);
hw/virtio/virtio-iommu.c +3 -0
+    virtio_iommu_map_region(s, virt_addr, phys_addr, size, 1);
     return VIRTIO_IOMMU_S_OK;
 }
@@ -267,7 +326,9 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
         } else {
             break;
         }
+
         if (interval.low >= interval.high) {
+            virtio_iommu_map_region(s, virt_addr, 0, size, 0);
             return VIRTIO_IOMMU_S_OK;
         } else {
             mapping = g_tree_lookup(as->mappings,
Quoted 1 line

(gpointer)&interval); @@ -410,6 +471,37 @@ static void

virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq)

hw/virtio/virtio-iommu.c static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq)

Quoted 7 lines
     }

}

hw/virtio/virtio-iommu.c +4 -0
+static void virtio_iommu_notify_flag_changed(MemoryRegion *iommu,
+                                          IOMMUNotifierFlag old,
+                                          IOMMUNotifierFlag new) {
+    IOMMUDevice *sdev = container_of(iommu, IOMMUDevice,

iommu_mr);

Quoted 14 lines
hw/virtio/virtio-iommu.c +14 -0
+    VirtIOIOMMU *s = sdev->viommu;
+    VirtioIOMMUNotifierNode *node = NULL;
+    VirtioIOMMUNotifierNode *next_node = NULL;
+
+    if (old == IOMMU_NOTIFIER_NONE) {
+        trace_virtio_iommu_notify_flag_add(iommu->name);
+        node = g_malloc0(sizeof(*node));
+        node->iommu_dev = sdev;
+        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
+        return;
+    }
+
+    /* update notifier node with new flags */
+    QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) {

hw/virtio/virtio-iommu.c static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) · line 502

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+        if (node->iommu_dev == sdev) {
+            if (new == IOMMU_NOTIFIER_NONE) {
+                trace_virtio_iommu_notify_flag_del(iommu->name);
+                QLIST_REMOVE(node, next);
+                g_free(node);
+            }
+            return;
+        }
+    }
+}

I think all that mechanics should be factorized somewhere else as all vIOMMUs use that but this goes beyond the scope of this series.

hw/virtio/virtio-iommu.c static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)

Quoted 3 lines
hw/virtio/virtio-iommu.c +2 -0
+
+
 static IOMMUTLBEntry virtio_iommu_translate(MemoryRegion *mr,

hwaddr addr,

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer

Quoted 2 lines
                                             IOMMUAccessFlags flag)  {
hw/virtio/virtio-iommu.c +0 -0
@@ -523,11 +615,50 @@ static gint int_cmp(gconstpointer a, gconstpointer

b, gpointer user_data)

Quoted 11 lines
hw/virtio/virtio-iommu.c +8 -0
     return (ua > ub) - (ua < ub);
 }
 
+static gboolean virtio_iommu_remap(gpointer key, gpointer value,
+gpointer data) {
+    viommu_mapping *mapping = (viommu_mapping *) value;
+    VirtIOIOMMU *s = (VirtIOIOMMU *) data;
+
+    trace_virtio_iommu_remap(mapping->virt_addr, mapping->phys_addr,
+                             mapping->size);
+    /* unmap previous entry and map again */

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 633

Quoted 10 lines
hw/virtio/virtio-iommu.c +9 -0
+    virtio_iommu_map_region(s, mapping->virt_addr, 0, mapping->size,
+ 0);
+
+    virtio_iommu_map_region(s, mapping->virt_addr, mapping-
 phys_addr,
+                            mapping->size, 1);
+    return true;
+}
+
+static void virtio_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n)

{

Quoted 9 lines
hw/virtio/virtio-iommu.c +9 -0
+    IOMMUDevice *sdev = container_of(mr, IOMMUDevice, iommu_mr);
+    VirtIOIOMMU *s = sdev->viommu;
+    uint32_t sid;
+    viommu_dev *dev;
+
+    sid = smmu_get_sid(sdev);
+
+    qemu_mutex_lock(&s->mutex);
+

hw/virtio/virtio-iommu.c static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) · line 653

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(sid));
+    if (!dev) {
+        goto unlock;
+    }
+
+    g_tree_foreach(dev->as->mappings, virtio_iommu_remap, s);
+
+unlock:
+    qemu_mutex_unlock(&s->mutex);
+    return;

not needed

Thanks

Eric

hw/virtio/virtio-iommu.c static void

Quoted 12 lines
hw/virtio/virtio-iommu.c +3 -0
+}
+
 static void virtio_iommu_device_realize(DeviceState *dev, Error
 **errp)  {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOIOMMU *s = VIRTIO_IOMMU(dev);
 
+    QLIST_INIT(&s->notifiers_list);
     virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU,
                 sizeof(struct virtio_iommu_config));
@@ -538,6 +669,8 @@ static void

virtio_iommu_device_realize(DeviceState *dev, Error **errp)

hw/virtio/virtio-iommu.c static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)

Quoted 4 lines
hw/virtio/virtio-iommu.c +1 -0
     s->config.input_range.end = -1UL;
 
     s->iommu_ops.translate = virtio_iommu_translate;
+    s->iommu_ops.notify_flag_changed =

virtio_iommu_notify_flag_changed;

Quoted 30 lines
hw/virtio/virtio-iommu.c +1 -1
+    s->iommu_ops.replay = virtio_iommu_replay;
     memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num));
     s->as_by_busptr = g_hash_table_new_full(as_uint64_hash,
                                             as_uint64_equal, diff
--git a/include/hw/virtio/virtio-iommu.h

b/include/hw/virtio/virtio-iommu.h
index 2259413..76c758d 100644

include/hw/virtio/virtio-iommu.h +6 -0
@@ -44,6 +44,11 @@ typedef struct IOMMUPciBus {
     IOMMUDevice  *pbdev[0]; /* Parent array is sparse, so dynamically
 alloc */  } IOMMUPciBus;
 
+typedef struct VirtioIOMMUNotifierNode {
+    IOMMUDevice *iommu_dev;
+    QLIST_ENTRY(VirtioIOMMUNotifierNode) next; }
+VirtioIOMMUNotifierNode;
+
 typedef struct VirtIOIOMMU {
     VirtIODevice parent_obj;
     VirtQueue *vq;
@@ -55,6 +60,7 @@ typedef struct VirtIOIOMMU {
     GTree *address_spaces;
     QemuMutex mutex;
     GTree *devices;
+    QLIST_HEAD(, VirtioIOMMUNotifierNode) notifiers_list;
 } VirtIOIOMMU;
 
 #endif