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