yeahuh / rpms / qemu-kvm

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