cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
c687bc
From 43a460bde62359c3fa2b1fc6c90d9e13ee7b9a6c Mon Sep 17 00:00:00 2001
c687bc
From: eperezma <eperezma@redhat.com>
c687bc
Date: Tue, 12 Jan 2021 14:36:35 -0500
c687bc
Subject: [PATCH 11/17] memory: Add IOMMUTLBEvent
c687bc
MIME-Version: 1.0
c687bc
Content-Type: text/plain; charset=UTF-8
c687bc
Content-Transfer-Encoding: 8bit
c687bc
c687bc
RH-Author: eperezma <eperezma@redhat.com>
c687bc
Message-id: <20210112143638.374060-11-eperezma@redhat.com>
c687bc
Patchwork-id: 100603
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 10/13] memory: Add IOMMUTLBEvent
c687bc
Bugzilla: 1843852
c687bc
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
c687bc
RH-Acked-by: Peter Xu <peterx@redhat.com>
c687bc
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
c687bc
c687bc
This way we can tell between regular IOMMUTLBEntry (entry of IOMMU
c687bc
hardware) and notifications.
c687bc
c687bc
In the notifications, we set explicitly if it is a MAPs or an UNMAP,
c687bc
instead of trusting in entry permissions to differentiate them.
c687bc
c687bc
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
c687bc
Reviewed-by: Peter Xu <peterx@redhat.com>
c687bc
Reviewed-by: Juan Quintela <quintela@redhat.com>
c687bc
Acked-by: Jason Wang <jasowang@redhat.com>
c687bc
Message-Id: <20201116165506.31315-3-eperezma@redhat.com>
c687bc
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
c687bc
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
c687bc
Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com>
c687bc
Acked-by: David Gibson <david@gibson.dropbear.id.au>
c687bc
(cherry picked from commit 5039caf3c449c49e625d34e134463260cf8e00e0)
c687bc
c687bc
Conflicts:
c687bc
	hw/s390x/s390-pci-inst.c: Context because of the lack of commit
c687bc
		("37fa32de707 s390x/pci: Honor DMA limits set by vfio").
c687bc
	hw/virtio/virtio-iommu.c: It does not exist in rhel.
c687bc
c687bc
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
c687bc
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
c687bc
---
c687bc
 hw/arm/smmu-common.c     | 13 +++---
c687bc
 hw/arm/smmuv3.c          | 13 +++---
c687bc
 hw/i386/intel_iommu.c    | 88 ++++++++++++++++++++++------------------
c687bc
 hw/misc/tz-mpc.c         | 32 ++++++++-------
c687bc
 hw/ppc/spapr_iommu.c     | 15 +++----
c687bc
 hw/s390x/s390-pci-inst.c | 27 +++++++-----
c687bc
 include/exec/memory.h    | 27 ++++++------
c687bc
 memory.c                 | 20 ++++-----
c687bc
 8 files changed, 127 insertions(+), 108 deletions(-)
c687bc
c687bc
diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c
c687bc
index dfabe381182..a519c97614a 100644
c687bc
--- a/hw/arm/smmu-common.c
c687bc
+++ b/hw/arm/smmu-common.c
c687bc
@@ -464,14 +464,15 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid)
c687bc
 /* Unmap the whole notifier's range */
c687bc
 static void smmu_unmap_notifier_range(IOMMUNotifier *n)
c687bc
 {
c687bc
-    IOMMUTLBEntry entry;
c687bc
+    IOMMUTLBEvent event;
c687bc
 
c687bc
-    entry.target_as = &address_space_memory;
c687bc
-    entry.iova = n->start;
c687bc
-    entry.perm = IOMMU_NONE;
c687bc
-    entry.addr_mask = n->end - n->start;
c687bc
+    event.type = IOMMU_NOTIFIER_UNMAP;
c687bc
+    event.entry.target_as = &address_space_memory;
c687bc
+    event.entry.iova = n->start;
c687bc
+    event.entry.perm = IOMMU_NONE;
c687bc
+    event.entry.addr_mask = n->end - n->start;
c687bc
 
c687bc
-    memory_region_notify_iommu_one(n, &entry);
c687bc
+    memory_region_notify_iommu_one(n, &event);
c687bc
 }
c687bc
 
c687bc
 /* Unmap all notifiers attached to @mr */
c687bc
diff --git a/hw/arm/smmuv3.c b/hw/arm/smmuv3.c
c687bc
index ef8a877c5d8..10b8393beeb 100644
c687bc
--- a/hw/arm/smmuv3.c
c687bc
+++ b/hw/arm/smmuv3.c
c687bc
@@ -783,7 +783,7 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr,
c687bc
                                uint8_t tg, uint64_t num_pages)
c687bc
 {
c687bc
     SMMUDevice *sdev = container_of(mr, SMMUDevice, iommu);
c687bc
-    IOMMUTLBEntry entry;
c687bc
+    IOMMUTLBEvent event;
c687bc
     uint8_t granule = tg;
c687bc
 
c687bc
     if (!tg) {
c687bc
@@ -806,12 +806,13 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr,
c687bc
         granule = tt->granule_sz;
c687bc
     }
c687bc
 
c687bc
-    entry.target_as = &address_space_memory;
c687bc
-    entry.iova = iova;
c687bc
-    entry.addr_mask = num_pages * (1 << granule) - 1;
c687bc
-    entry.perm = IOMMU_NONE;
c687bc
+    event.type = IOMMU_NOTIFIER_UNMAP;
c687bc
+    event.entry.target_as = &address_space_memory;
c687bc
+    event.entry.iova = iova;
c687bc
+    event.entry.addr_mask = num_pages * (1 << granule) - 1;
c687bc
+    event.entry.perm = IOMMU_NONE;
c687bc
 
c687bc
-    memory_region_notify_iommu_one(n, &entry);
c687bc
+    memory_region_notify_iommu_one(n, &event);
c687bc
 }
c687bc
 
c687bc
 /* invalidate an asid/iova range tuple in all mr's */
c687bc
diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
c687bc
index 463f107ad12..9fedbac82de 100644
c687bc
--- a/hw/i386/intel_iommu.c
c687bc
+++ b/hw/i386/intel_iommu.c
c687bc
@@ -1016,7 +1016,7 @@ static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce,
c687bc
     }
c687bc
 }
c687bc
 
c687bc
-typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private);
c687bc
+typedef int (*vtd_page_walk_hook)(IOMMUTLBEvent *event, void *private);
c687bc
 
c687bc
 /**
c687bc
  * Constant information used during page walking
c687bc
@@ -1037,11 +1037,12 @@ typedef struct {
c687bc
     uint16_t domain_id;
c687bc
 } vtd_page_walk_info;
c687bc
 
c687bc
-static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
c687bc
+static int vtd_page_walk_one(IOMMUTLBEvent *event, vtd_page_walk_info *info)
c687bc
 {
c687bc
     VTDAddressSpace *as = info->as;
c687bc
     vtd_page_walk_hook hook_fn = info->hook_fn;
c687bc
     void *private = info->private;
c687bc
+    IOMMUTLBEntry *entry = &event->entry;
c687bc
     DMAMap target = {
c687bc
         .iova = entry->iova,
c687bc
         .size = entry->addr_mask,
c687bc
@@ -1050,7 +1051,7 @@ static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
c687bc
     };
c687bc
     DMAMap *mapped = iova_tree_find(as->iova_tree, &target);
c687bc
 
c687bc
-    if (entry->perm == IOMMU_NONE && !info->notify_unmap) {
c687bc
+    if (event->type == IOMMU_NOTIFIER_UNMAP && !info->notify_unmap) {
c687bc
         trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
c687bc
         return 0;
c687bc
     }
c687bc
@@ -1058,7 +1059,7 @@ static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
c687bc
     assert(hook_fn);
c687bc
 
c687bc
     /* Update local IOVA mapped ranges */
c687bc
-    if (entry->perm) {
c687bc
+    if (event->type == IOMMU_NOTIFIER_MAP) {
c687bc
         if (mapped) {
c687bc
             /* If it's exactly the same translation, skip */
c687bc
             if (!memcmp(mapped, &target, sizeof(target))) {
c687bc
@@ -1084,19 +1085,21 @@ static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
c687bc
                 int ret;
c687bc
 
c687bc
                 /* Emulate an UNMAP */
c687bc
+                event->type = IOMMU_NOTIFIER_UNMAP;
c687bc
                 entry->perm = IOMMU_NONE;
c687bc
                 trace_vtd_page_walk_one(info->domain_id,
c687bc
                                         entry->iova,
c687bc
                                         entry->translated_addr,
c687bc
                                         entry->addr_mask,
c687bc
                                         entry->perm);
c687bc
-                ret = hook_fn(entry, private);
c687bc
+                ret = hook_fn(event, private);
c687bc
                 if (ret) {
c687bc
                     return ret;
c687bc
                 }
c687bc
                 /* Drop any existing mapping */
c687bc
                 iova_tree_remove(as->iova_tree, &target);
c687bc
-                /* Recover the correct permission */
c687bc
+                /* Recover the correct type */
c687bc
+                event->type = IOMMU_NOTIFIER_MAP;
c687bc
                 entry->perm = cache_perm;
c687bc
             }
c687bc
         }
c687bc
@@ -1113,7 +1116,7 @@ static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
c687bc
     trace_vtd_page_walk_one(info->domain_id, entry->iova,
c687bc
                             entry->translated_addr, entry->addr_mask,
c687bc
                             entry->perm);
c687bc
-    return hook_fn(entry, private);
c687bc
+    return hook_fn(event, private);
c687bc
 }
c687bc
 
c687bc
 /**
c687bc
@@ -1134,7 +1137,7 @@ static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
c687bc
     uint32_t offset;
c687bc
     uint64_t slpte;
c687bc
     uint64_t subpage_size, subpage_mask;
c687bc
-    IOMMUTLBEntry entry;
c687bc
+    IOMMUTLBEvent event;
c687bc
     uint64_t iova = start;
c687bc
     uint64_t iova_next;
c687bc
     int ret = 0;
c687bc
@@ -1188,13 +1191,15 @@ static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
c687bc
              *
c687bc
              * In either case, we send an IOTLB notification down.
c687bc
              */
c687bc
-            entry.target_as = &address_space_memory;
c687bc
-            entry.iova = iova & subpage_mask;
c687bc
-            entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
c687bc
-            entry.addr_mask = ~subpage_mask;
c687bc
+            event.entry.target_as = &address_space_memory;
c687bc
+            event.entry.iova = iova & subpage_mask;
c687bc
+            event.entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
c687bc
+            event.entry.addr_mask = ~subpage_mask;
c687bc
             /* NOTE: this is only meaningful if entry_valid == true */
c687bc
-            entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw);
c687bc
-            ret = vtd_page_walk_one(&entry, info);
c687bc
+            event.entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw);
c687bc
+            event.type = event.entry.perm ? IOMMU_NOTIFIER_MAP :
c687bc
+                                            IOMMU_NOTIFIER_UNMAP;
c687bc
+            ret = vtd_page_walk_one(&event, info);
c687bc
         }
c687bc
 
c687bc
         if (ret < 0) {
c687bc
@@ -1373,10 +1378,10 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
c687bc
     return 0;
c687bc
 }
c687bc
 
c687bc
-static int vtd_sync_shadow_page_hook(IOMMUTLBEntry *entry,
c687bc
+static int vtd_sync_shadow_page_hook(IOMMUTLBEvent *event,
c687bc
                                      void *private)
c687bc
 {
c687bc
-    memory_region_notify_iommu((IOMMUMemoryRegion *)private, 0, *entry);
c687bc
+    memory_region_notify_iommu(private, 0, *event);
c687bc
     return 0;
c687bc
 }
c687bc
 
c687bc
@@ -1936,14 +1941,17 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
c687bc
                  * page tables.  We just deliver the PSI down to
c687bc
                  * invalidate caches.
c687bc
                  */
c687bc
-                IOMMUTLBEntry entry = {
c687bc
-                    .target_as = &address_space_memory,
c687bc
-                    .iova = addr,
c687bc
-                    .translated_addr = 0,
c687bc
-                    .addr_mask = size - 1,
c687bc
-                    .perm = IOMMU_NONE,
c687bc
+                IOMMUTLBEvent event = {
c687bc
+                    .type = IOMMU_NOTIFIER_UNMAP,
c687bc
+                    .entry = {
c687bc
+                        .target_as = &address_space_memory,
c687bc
+                        .iova = addr,
c687bc
+                        .translated_addr = 0,
c687bc
+                        .addr_mask = size - 1,
c687bc
+                        .perm = IOMMU_NONE,
c687bc
+                    },
c687bc
                 };
c687bc
-                memory_region_notify_iommu(&vtd_as->iommu, 0, entry);
c687bc
+                memory_region_notify_iommu(&vtd_as->iommu, 0, event);
c687bc
             }
c687bc
         }
c687bc
     }
c687bc
@@ -2355,7 +2363,7 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
c687bc
                                           VTDInvDesc *inv_desc)
c687bc
 {
c687bc
     VTDAddressSpace *vtd_dev_as;
c687bc
-    IOMMUTLBEntry entry;
c687bc
+    IOMMUTLBEvent event;
c687bc
     struct VTDBus *vtd_bus;
c687bc
     hwaddr addr;
c687bc
     uint64_t sz;
c687bc
@@ -2403,12 +2411,13 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
c687bc
         sz = VTD_PAGE_SIZE;
c687bc
     }
c687bc
 
c687bc
-    entry.target_as = &vtd_dev_as->as;
c687bc
-    entry.addr_mask = sz - 1;
c687bc
-    entry.iova = addr;
c687bc
-    entry.perm = IOMMU_NONE;
c687bc
-    entry.translated_addr = 0;
c687bc
-    memory_region_notify_iommu(&vtd_dev_as->iommu, 0, entry);
c687bc
+    event.type = IOMMU_NOTIFIER_UNMAP;
c687bc
+    event.entry.target_as = &vtd_dev_as->as;
c687bc
+    event.entry.addr_mask = sz - 1;
c687bc
+    event.entry.iova = addr;
c687bc
+    event.entry.perm = IOMMU_NONE;
c687bc
+    event.entry.translated_addr = 0;
c687bc
+    memory_region_notify_iommu(&vtd_dev_as->iommu, 0, event);
c687bc
 
c687bc
 done:
c687bc
     return true;
c687bc
@@ -3419,19 +3428,20 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
c687bc
     size = remain = end - start + 1;
c687bc
 
c687bc
     while (remain >= VTD_PAGE_SIZE) {
c687bc
-        IOMMUTLBEntry entry;
c687bc
+        IOMMUTLBEvent event;
c687bc
         uint64_t mask = get_naturally_aligned_size(start, remain, s->aw_bits);
c687bc
 
c687bc
         assert(mask);
c687bc
 
c687bc
-        entry.iova = start;
c687bc
-        entry.addr_mask = mask - 1;
c687bc
-        entry.target_as = &address_space_memory;
c687bc
-        entry.perm = IOMMU_NONE;
c687bc
+        event.type = IOMMU_NOTIFIER_UNMAP;
c687bc
+        event.entry.iova = start;
c687bc
+        event.entry.addr_mask = mask - 1;
c687bc
+        event.entry.target_as = &address_space_memory;
c687bc
+        event.entry.perm = IOMMU_NONE;
c687bc
         /* This field is meaningless for unmap */
c687bc
-        entry.translated_addr = 0;
c687bc
+        event.entry.translated_addr = 0;
c687bc
 
c687bc
-        memory_region_notify_iommu_one(n, &entry);
c687bc
+        memory_region_notify_iommu_one(n, &event);
c687bc
 
c687bc
         start += mask;
c687bc
         remain -= mask;
c687bc
@@ -3467,9 +3477,9 @@ static void vtd_address_space_refresh_all(IntelIOMMUState *s)
c687bc
     vtd_switch_address_space_all(s);
c687bc
 }
c687bc
 
c687bc
-static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private)
c687bc
+static int vtd_replay_hook(IOMMUTLBEvent *event, void *private)
c687bc
 {
c687bc
-    memory_region_notify_iommu_one((IOMMUNotifier *)private, entry);
c687bc
+    memory_region_notify_iommu_one(private, event);
c687bc
     return 0;
c687bc
 }
c687bc
 
c687bc
diff --git a/hw/misc/tz-mpc.c b/hw/misc/tz-mpc.c
c687bc
index 49dd6050bd3..e2fbd1065d8 100644
c687bc
--- a/hw/misc/tz-mpc.c
c687bc
+++ b/hw/misc/tz-mpc.c
c687bc
@@ -82,8 +82,10 @@ static void tz_mpc_iommu_notify(TZMPC *s, uint32_t lutidx,
c687bc
     /* Called when the LUT word at lutidx has changed from oldlut to newlut;
c687bc
      * must call the IOMMU notifiers for the changed blocks.
c687bc
      */
c687bc
-    IOMMUTLBEntry entry = {
c687bc
-        .addr_mask = s->blocksize - 1,
c687bc
+    IOMMUTLBEvent event = {
c687bc
+        .entry = {
c687bc
+            .addr_mask = s->blocksize - 1,
c687bc
+        }
c687bc
     };
c687bc
     hwaddr addr = lutidx * s->blocksize * 32;
c687bc
     int i;
c687bc
@@ -100,26 +102,28 @@ static void tz_mpc_iommu_notify(TZMPC *s, uint32_t lutidx,
c687bc
         block_is_ns = newlut & (1 << i);
c687bc
 
c687bc
         trace_tz_mpc_iommu_notify(addr);
c687bc
-        entry.iova = addr;
c687bc
-        entry.translated_addr = addr;
c687bc
+        event.entry.iova = addr;
c687bc
+        event.entry.translated_addr = addr;
c687bc
 
c687bc
-        entry.perm = IOMMU_NONE;
c687bc
-        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_S, entry);
c687bc
-        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_NS, entry);
c687bc
+        event.type = IOMMU_NOTIFIER_UNMAP;
c687bc
+        event.entry.perm = IOMMU_NONE;
c687bc
+        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_S, event);
c687bc
+        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_NS, event);
c687bc
 
c687bc
-        entry.perm = IOMMU_RW;
c687bc
+        event.type = IOMMU_NOTIFIER_MAP;
c687bc
+        event.entry.perm = IOMMU_RW;
c687bc
         if (block_is_ns) {
c687bc
-            entry.target_as = &s->blocked_io_as;
c687bc
+            event.entry.target_as = &s->blocked_io_as;
c687bc
         } else {
c687bc
-            entry.target_as = &s->downstream_as;
c687bc
+            event.entry.target_as = &s->downstream_as;
c687bc
         }
c687bc
-        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_S, entry);
c687bc
+        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_S, event);
c687bc
         if (block_is_ns) {
c687bc
-            entry.target_as = &s->downstream_as;
c687bc
+            event.entry.target_as = &s->downstream_as;
c687bc
         } else {
c687bc
-            entry.target_as = &s->blocked_io_as;
c687bc
+            event.entry.target_as = &s->blocked_io_as;
c687bc
         }
c687bc
-        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_NS, entry);
c687bc
+        memory_region_notify_iommu(&s->upstream, IOMMU_IDX_NS, event);
c687bc
     }
c687bc
 }
c687bc
 
c687bc
diff --git a/hw/ppc/spapr_iommu.c b/hw/ppc/spapr_iommu.c
c687bc
index 3d3bcc86496..9d3ec7e2c07 100644
c687bc
--- a/hw/ppc/spapr_iommu.c
c687bc
+++ b/hw/ppc/spapr_iommu.c
c687bc
@@ -445,7 +445,7 @@ static void spapr_tce_reset(DeviceState *dev)
c687bc
 static target_ulong put_tce_emu(SpaprTceTable *tcet, target_ulong ioba,
c687bc
                                 target_ulong tce)
c687bc
 {
c687bc
-    IOMMUTLBEntry entry;
c687bc
+    IOMMUTLBEvent event;
c687bc
     hwaddr page_mask = IOMMU_PAGE_MASK(tcet->page_shift);
c687bc
     unsigned long index = (ioba - tcet->bus_offset) >> tcet->page_shift;
c687bc
 
c687bc
@@ -457,12 +457,13 @@ static target_ulong put_tce_emu(SpaprTceTable *tcet, target_ulong ioba,
c687bc
 
c687bc
     tcet->table[index] = tce;
c687bc
 
c687bc
-    entry.target_as = &address_space_memory,
c687bc
-    entry.iova = (ioba - tcet->bus_offset) & page_mask;
c687bc
-    entry.translated_addr = tce & page_mask;
c687bc
-    entry.addr_mask = ~page_mask;
c687bc
-    entry.perm = spapr_tce_iommu_access_flags(tce);
c687bc
-    memory_region_notify_iommu(&tcet->iommu, 0, entry);
c687bc
+    event.entry.target_as = &address_space_memory,
c687bc
+    event.entry.iova = (ioba - tcet->bus_offset) & page_mask;
c687bc
+    event.entry.translated_addr = tce & page_mask;
c687bc
+    event.entry.addr_mask = ~page_mask;
c687bc
+    event.entry.perm = spapr_tce_iommu_access_flags(tce);
c687bc
+    event.type = event.entry.perm ? IOMMU_NOTIFIER_MAP : IOMMU_NOTIFIER_UNMAP;
c687bc
+    memory_region_notify_iommu(&tcet->iommu, 0, event);
c687bc
 
c687bc
     return H_SUCCESS;
c687bc
 }
c687bc
diff --git a/hw/s390x/s390-pci-inst.c b/hw/s390x/s390-pci-inst.c
c687bc
index 92c7e45df5f..27b189e6d75 100644
c687bc
--- a/hw/s390x/s390-pci-inst.c
c687bc
+++ b/hw/s390x/s390-pci-inst.c
c687bc
@@ -575,15 +575,18 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
c687bc
 static void s390_pci_update_iotlb(S390PCIIOMMU *iommu, S390IOTLBEntry *entry)
c687bc
 {
c687bc
     S390IOTLBEntry *cache = g_hash_table_lookup(iommu->iotlb, &entry->iova);
c687bc
-    IOMMUTLBEntry notify = {
c687bc
-        .target_as = &address_space_memory,
c687bc
-        .iova = entry->iova,
c687bc
-        .translated_addr = entry->translated_addr,
c687bc
-        .perm = entry->perm,
c687bc
-        .addr_mask = ~PAGE_MASK,
c687bc
+    IOMMUTLBEvent event = {
c687bc
+        .type = entry->perm ? IOMMU_NOTIFIER_MAP : IOMMU_NOTIFIER_UNMAP,
c687bc
+        .entry = {
c687bc
+            .target_as = &address_space_memory,
c687bc
+            .iova = entry->iova,
c687bc
+            .translated_addr = entry->translated_addr,
c687bc
+            .perm = entry->perm,
c687bc
+            .addr_mask = ~PAGE_MASK,
c687bc
+        },
c687bc
     };
c687bc
 
c687bc
-    if (entry->perm == IOMMU_NONE) {
c687bc
+    if (event.type == IOMMU_NOTIFIER_UNMAP) {
c687bc
         if (!cache) {
c687bc
             return;
c687bc
         }
c687bc
@@ -595,9 +598,11 @@ static void s390_pci_update_iotlb(S390PCIIOMMU *iommu, S390IOTLBEntry *entry)
c687bc
                 return;
c687bc
             }
c687bc
 
c687bc
-            notify.perm = IOMMU_NONE;
c687bc
-            memory_region_notify_iommu(&iommu->iommu_mr, 0, notify);
c687bc
-            notify.perm = entry->perm;
c687bc
+            event.type = IOMMU_NOTIFIER_UNMAP;
c687bc
+            event.entry.perm = IOMMU_NONE;
c687bc
+            memory_region_notify_iommu(&iommu->iommu_mr, 0, event);
c687bc
+            event.type = IOMMU_NOTIFIER_MAP;
c687bc
+            event.entry.perm = entry->perm;
c687bc
         }
c687bc
 
c687bc
         cache = g_new(S390IOTLBEntry, 1);
c687bc
@@ -608,7 +613,7 @@ static void s390_pci_update_iotlb(S390PCIIOMMU *iommu, S390IOTLBEntry *entry)
c687bc
         g_hash_table_replace(iommu->iotlb, &cache->iova, cache);
c687bc
     }
c687bc
 
c687bc
-    memory_region_notify_iommu(&iommu->iommu_mr, 0, notify);
c687bc
+    memory_region_notify_iommu(&iommu->iommu_mr, 0, event);
c687bc
 }
c687bc
 
c687bc
 int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
c687bc
diff --git a/include/exec/memory.h b/include/exec/memory.h
c687bc
index b6466ab6d57..80e36077cdb 100644
c687bc
--- a/include/exec/memory.h
c687bc
+++ b/include/exec/memory.h
c687bc
@@ -106,6 +106,11 @@ struct IOMMUNotifier {
c687bc
 };
c687bc
 typedef struct IOMMUNotifier IOMMUNotifier;
c687bc
 
c687bc
+typedef struct IOMMUTLBEvent {
c687bc
+    IOMMUNotifierFlag type;
c687bc
+    IOMMUTLBEntry entry;
c687bc
+} IOMMUTLBEvent;
c687bc
+
c687bc
 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
c687bc
 #define RAM_PREALLOC   (1 << 0)
c687bc
 
c687bc
@@ -1047,24 +1052,18 @@ uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
c687bc
 /**
c687bc
  * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
c687bc
  *
c687bc
- * The notification type will be decided by entry.perm bits:
c687bc
- *
c687bc
- * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
c687bc
- * - For MAP (newly added entry) notifies: set entry.perm to the
c687bc
- *   permission of the page (which is definitely !IOMMU_NONE).
c687bc
- *
c687bc
  * Note: for any IOMMU implementation, an in-place mapping change
c687bc
  * should be notified with an UNMAP followed by a MAP.
c687bc
  *
c687bc
  * @iommu_mr: the memory region that was changed
c687bc
  * @iommu_idx: the IOMMU index for the translation table which has changed
c687bc
- * @entry: the new entry in the IOMMU translation table.  The entry
c687bc
- *         replaces all old entries for the same virtual I/O address range.
c687bc
- *         Deleted entries have .@perm == 0.
c687bc
+ * @event: TLB event with the new entry in the IOMMU translation table.
c687bc
+ *         The entry replaces all old entries for the same virtual I/O address
c687bc
+ *         range.
c687bc
  */
c687bc
 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
c687bc
                                 int iommu_idx,
c687bc
-                                IOMMUTLBEntry entry);
c687bc
+                                IOMMUTLBEvent event);
c687bc
 
c687bc
 /**
c687bc
  * memory_region_notify_iommu_one: notify a change in an IOMMU translation
c687bc
@@ -1074,12 +1073,12 @@ void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
c687bc
  * notifies a specific notifier, not all of them.
c687bc
  *
c687bc
  * @notifier: the notifier to be notified
c687bc
- * @entry: the new entry in the IOMMU translation table.  The entry
c687bc
- *         replaces all old entries for the same virtual I/O address range.
c687bc
- *         Deleted entries have .@perm == 0.
c687bc
+ * @event: TLB event with the new entry in the IOMMU translation table.
c687bc
+ *         The entry replaces all old entries for the same virtual I/O address
c687bc
+ *         range.
c687bc
  */
c687bc
 void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
c687bc
-                              IOMMUTLBEntry *entry);
c687bc
+                                    IOMMUTLBEvent *event);
c687bc
 
c687bc
 /**
c687bc
  * memory_region_register_iommu_notifier: register a notifier for changes to
c687bc
diff --git a/memory.c b/memory.c
c687bc
index 43bd3359bf8..3bd99b8ac4a 100644
c687bc
--- a/memory.c
c687bc
+++ b/memory.c
c687bc
@@ -1912,11 +1912,15 @@ void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
c687bc
 }
c687bc
 
c687bc
 void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
c687bc
-                                    IOMMUTLBEntry *entry)
c687bc
+                                    IOMMUTLBEvent *event)
c687bc
 {
c687bc
-    IOMMUNotifierFlag request_flags;
c687bc
+    IOMMUTLBEntry *entry = &event->entry;
c687bc
     hwaddr entry_end = entry->iova + entry->addr_mask;
c687bc
 
c687bc
+    if (event->type == IOMMU_NOTIFIER_UNMAP) {
c687bc
+        assert(entry->perm == IOMMU_NONE);
c687bc
+    }
c687bc
+
c687bc
     /*
c687bc
      * Skip the notification if the notification does not overlap
c687bc
      * with registered range.
c687bc
@@ -1927,20 +1931,14 @@ void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
c687bc
 
c687bc
     assert(entry->iova >= notifier->start && entry_end <= notifier->end);
c687bc
 
c687bc
-    if (entry->perm & IOMMU_RW) {
c687bc
-        request_flags = IOMMU_NOTIFIER_MAP;
c687bc
-    } else {
c687bc
-        request_flags = IOMMU_NOTIFIER_UNMAP;
c687bc
-    }
c687bc
-
c687bc
-    if (notifier->notifier_flags & request_flags) {
c687bc
+    if (event->type & notifier->notifier_flags) {
c687bc
         notifier->notify(notifier, entry);
c687bc
     }
c687bc
 }
c687bc
 
c687bc
 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
c687bc
                                 int iommu_idx,
c687bc
-                                IOMMUTLBEntry entry)
c687bc
+                                IOMMUTLBEvent event)
c687bc
 {
c687bc
     IOMMUNotifier *iommu_notifier;
c687bc
 
c687bc
@@ -1948,7 +1946,7 @@ void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
c687bc
 
c687bc
     IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
c687bc
         if (iommu_notifier->iommu_idx == iommu_idx) {
c687bc
-            memory_region_notify_iommu_one(iommu_notifier, &entry);
c687bc
+            memory_region_notify_iommu_one(iommu_notifier, &event);
c687bc
         }
c687bc
     }
c687bc
 }
c687bc
-- 
c687bc
2.27.0
c687bc