Skip to contentSkip to conversation

IOMMU: intel_iommu support map and unmap notifications

PATCH RESEND Superseded Aviv B.D 6 participants 31 messages Oct 17, 2016 – Oct 31, 2016

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Aviv B.D

author
#0 lore raw

v4 cover IOMMU: intel_iommu support map and unmap notifications

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".

* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.

Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.
* memory_region_iommu_replay hangs QEMU on start up while it itterate over 64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

Changes from v1 to v2:
* remove assumption that the cache do not clears
* fix lockup on high load.

Changes from v2 to v3:
* remove debug leftovers
* split to sepearate commits
* change is_write to flags in vtd_do_iommu_translate, add IOMMU_NO_FAIL to suppress error propagating to guest.

Changes from v3 to v4:
* Add property to intel_iommu device to control the CM capability, default to False.
* Use s->iommu_ops.notify_flag_changed to register notifiers.

Changes from v4 to v4 RESEND:
* Fix codding style pointed by checkpatch.pl script.

Aviv Ben-David (3):
IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to
guest
IOMMU: change iommu_op->translate's is_write to flags, add support to
NO_FAIL flag mode
IOMMU: enable intel_iommu map and unmap notifiers

 exec.c                         |   3 +-
 hw/i386/amd_iommu.c            |   4 +-
 hw/i386/intel_iommu.c          | 175 +++++++++++++++++++++++++++++++++--------
 hw/i386/intel_iommu_internal.h |   3 +
 include/exec/memory.h          |   6 +-
 include/hw/i386/intel_iommu.h  |  11 +++
 memory.c                       |   3 +-
 7 files changed, 168 insertions(+), 37 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH RESEND v4 1/3 IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to guest

From: "Aviv Ben-David" <[email protected]>

This capability asks the guest to invalidate cache before each map operation. We can use this invalidation to trap map operations in the hypervisor.

---
 hw/i386/intel_iommu.c          | 5 +++++
 hw/i386/intel_iommu_internal.h | 1 +
 include/hw/i386/intel_iommu.h  | 2 ++
 3 files changed, 8 insertions(+)
hw/i386/intel_iommu.c +5 -0
index 2efd69b..69730cb 100644
@@ -2012,6 +2012,7 @@ static const MemoryRegionOps vtd_mem_ops = {
 
 static Property vtd_properties[] = {
     DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
+    DEFINE_PROP_BOOL("cache-mode", IntelIOMMUState, cache_mode_enabled, FALSE),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -2385,6 +2386,10 @@ static void vtd_init(IntelIOMMUState *s)
         s->ecap |= VTD_ECAP_IR | VTD_ECAP_EIM | VTD_ECAP_MHMV;
     }
 
+    if (s->cache_mode_enabled) {
+        s->cap |= VTD_CAP_CM;
+    }
+
     vtd_reset_context_cache(s);
     vtd_reset_iotlb(s);
 
hw/i386/intel_iommu_internal.h +1 -0
index 0829a50..35d9f3a 100644
@@ -201,6 +201,7 @@
 #define VTD_CAP_MAMV                (VTD_MAMV << 48)
 #define VTD_CAP_PSI                 (1ULL << 39)
 #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
+#define VTD_CAP_CM                  (1ULL << 7)
 
 /* Supported Adjusted Guest Address Widths */
 #define VTD_CAP_SAGAW_SHIFT         8
include/hw/i386/intel_iommu.h +2 -0
index a42dbd7..7a94f16 100644
@@ -258,6 +258,8 @@ struct IntelIOMMUState {
     uint8_t womask[DMAR_REG_SIZE];  /* WO (write only - read returns 0) */
     uint32_t version;
 
+    bool cache_mode_enabled;        /* RO - is cap CM enabled? */
+
     dma_addr_t root;                /* Current root table pointer */
     bool root_extended;             /* Type of root table (extended or not) */
     bool dmar_enabled;              /* Set if DMA remapping is enabled */
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 1/3] IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to guest

Quoted 11 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

This capability asks the guest to invalidate cache before each map operation. We can use this invalidation to trap map operations in the hypervisor.

---
  hw/i386/intel_iommu.c          | 5 +++++
  hw/i386/intel_iommu_internal.h | 1 +
  include/hw/i386/intel_iommu.h  | 2 ++
  3 files changed, 8 insertions(+)

As I asked in previous version, this may not be sufficient.

CM requires to cache fault translations which is not implemented in this patch. Guest can easily notice this kind of spec violation.

Thanks

Quoted 49 lines
hw/i386/intel_iommu.c +5 -0
index 2efd69b..69730cb 100644
@@ -2012,6 +2012,7 @@ static const MemoryRegionOps vtd_mem_ops = {
 
  static Property vtd_properties[] = {
      DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
+    DEFINE_PROP_BOOL("cache-mode", IntelIOMMUState, cache_mode_enabled, FALSE),
      DEFINE_PROP_END_OF_LIST(),
  };
 
@@ -2385,6 +2386,10 @@ static void vtd_init(IntelIOMMUState *s)
          s->ecap |= VTD_ECAP_IR | VTD_ECAP_EIM | VTD_ECAP_MHMV;
      }
 
+    if (s->cache_mode_enabled) {
+        s->cap |= VTD_CAP_CM;
+    }
+
      vtd_reset_context_cache(s);
      vtd_reset_iotlb(s);
 
hw/i386/intel_iommu_internal.h +1 -0
index 0829a50..35d9f3a 100644
@@ -201,6 +201,7 @@
  #define VTD_CAP_MAMV                (VTD_MAMV << 48)
  #define VTD_CAP_PSI                 (1ULL << 39)
  #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
+#define VTD_CAP_CM                  (1ULL << 7)
 
  /* Supported Adjusted Guest Address Widths */
  #define VTD_CAP_SAGAW_SHIFT         8
include/hw/i386/intel_iommu.h +2 -0
index a42dbd7..7a94f16 100644
@@ -258,6 +258,8 @@ struct IntelIOMMUState {
      uint8_t womask[DMAR_REG_SIZE];  /* WO (write only - read returns 0) */
      uint32_t version;
 
+    bool cache_mode_enabled;        /* RO - is cap CM enabled? */
+
      dma_addr_t root;                /* Current root table pointer */
      bool root_extended;             /* Type of root table (extended or not) */
      bool dmar_enabled;              /* Set if DMA remapping is enabled */
#3 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 1/3] IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to guest

Quoted 15 lines · Jason Wang
Quoted 11 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

This capability asks the guest to invalidate cache before each map operation. We can use this invalidation to trap map operations in the hypervisor.

---
  hw/i386/intel_iommu.c          | 5 +++++
  hw/i386/intel_iommu_internal.h | 1 +
  include/hw/i386/intel_iommu.h  | 2 ++
  3 files changed, 8 insertions(+)
Quoted 4 lines

As I asked in previous version, this may not be sufficient.

CM requires to cache fault translations which is not implemented in this patch.

I'm not sure why would there be a requirement to cache fault information. Cache can always be invalidated for any reason, in particular an empty cache is always OK.

Quoted 1 line

Guest can easily notice this kind of spec violation.

How?

Quoted 45 lines

Thanks

Quoted 43 lines
hw/i386/intel_iommu.c +5 -0
index 2efd69b..69730cb 100644
@@ -2012,6 +2012,7 @@ static const MemoryRegionOps vtd_mem_ops = {
  static Property vtd_properties[] = {
      DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
+    DEFINE_PROP_BOOL("cache-mode", IntelIOMMUState, cache_mode_enabled, FALSE),
      DEFINE_PROP_END_OF_LIST(),
  };
@@ -2385,6 +2386,10 @@ static void vtd_init(IntelIOMMUState *s)
          s->ecap |= VTD_ECAP_IR | VTD_ECAP_EIM | VTD_ECAP_MHMV;
      }
+    if (s->cache_mode_enabled) {
+        s->cap |= VTD_CAP_CM;
+    }
+
      vtd_reset_context_cache(s);
      vtd_reset_iotlb(s);
hw/i386/intel_iommu_internal.h +1 -0
index 0829a50..35d9f3a 100644
@@ -201,6 +201,7 @@
  #define VTD_CAP_MAMV                (VTD_MAMV << 48)
  #define VTD_CAP_PSI                 (1ULL << 39)
  #define VTD_CAP_SLLPS               ((1ULL << 34) | (1ULL << 35))
+#define VTD_CAP_CM                  (1ULL << 7)
  /* Supported Adjusted Guest Address Widths */
  #define VTD_CAP_SAGAW_SHIFT         8
include/hw/i386/intel_iommu.h +2 -0
index a42dbd7..7a94f16 100644
@@ -258,6 +258,8 @@ struct IntelIOMMUState {
      uint8_t womask[DMAR_REG_SIZE];  /* WO (write only - read returns 0) */
      uint32_t version;
+    bool cache_mode_enabled;        /* RO - is cap CM enabled? */
+
      dma_addr_t root;                /* Current root table pointer */
      bool root_extended;             /* Type of root table (extended or not) */
      bool dmar_enabled;              /* Set if DMA remapping is enabled */
#4 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 1/3] IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to guest

Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Jason Wang
Quoted 15 lines · Aviv B.D
Quoted 11 lines
Quoted 11 lines

From: "Aviv Ben-David"<[email protected]>

This capability asks the guest to invalidate cache before each map operation. We can use this invalidation to trap map operations in the hypervisor.

---
  hw/i386/intel_iommu.c          | 5 +++++
  hw/i386/intel_iommu_internal.h | 1 +
  include/hw/i386/intel_iommu.h  | 2 ++
  3 files changed, 8 insertions(+)
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

As I asked in previous version, this may not be sufficient.

CM requires to cache fault translations which is not implemented in this patch.

I'm not sure why would there be a requirement to cache fault information. Cache can always be invalidated for any reason, in particular an empty cache is always OK.

s/requires/may/. But what did here is "don't". Isn't this an obvious violation?

Empty cache only work if we don't implement an real IOTLB but traverse the IO page tables each time.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

Guest can easily notice this kind of spec violation.

How?

I guess this may do the detection:

1) map iova A to be non-present.
2) invalidate iova A
3) access iova A
4) map iova A to addr B
5) access iova A

A correct implemented CM may meet fault in step 5, but with this patch, we don't.

author
#5 lore raw

PATCH RESEND v4 2/3 IOMMU: change iommu_op->translate's is_write to flags, add support to NO_FAIL flag mode

In reply to Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Supports translation trials without reporting error to guest on translation failure.

---
 exec.c                |  3 ++-
 hw/i386/amd_iommu.c   |  4 +--
 hw/i386/intel_iommu.c | 70 +++++++++++++++++++++++++++++++++------------------
 include/exec/memory.h |  6 +++--
 memory.c              |  3 ++-
 5 files changed, 55 insertions(+), 31 deletions(-)
exec.c +2 -1
index 374c364..266fa01 100644
@@ -432,7 +432,8 @@ MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
             break;
         }
 
-        iotlb = mr->iommu_ops->translate(mr, addr, is_write);
+        iotlb = mr->iommu_ops->translate(mr, addr,
+                                         is_write ? IOMMU_WO : IOMMU_RO);
         addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
                 | (addr & iotlb.addr_mask));
         *plen = MIN(*plen, (addr | iotlb.addr_mask) - addr + 1);
hw/i386/amd_iommu.c +2 -2
index 47b79d9..1f0d76b 100644
@@ -988,7 +988,7 @@ static inline bool amdvi_is_interrupt_addr(hwaddr addr)
 }
 
 static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,
-                                     bool is_write)
+                                     IOMMUAccessFlags flags)
 {
     AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
     AMDVIState *s = as->iommu_state;
@@ -1017,7 +1017,7 @@ static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,
         return ret;
     }
 
-    amdvi_do_translate(as, addr, is_write, &ret);
+    amdvi_do_translate(as, addr, flags & IOMMU_WO, &ret);
     trace_amdvi_translation_result(as->bus_num, PCI_SLOT(as->devfn),
             PCI_FUNC(as->devfn), addr, ret.translated_addr);
     return ret;
hw/i386/intel_iommu.c +45 -25

168 lines not on this page · raw message

include/exec/memory.h +4 -2
index 10d7eac..0d4acb9 100644
@@ -57,6 +57,7 @@ typedef enum {
     IOMMU_RO   = 1,
     IOMMU_WO   = 2,
     IOMMU_RW   = 3,
+    IOMMU_NO_FAIL  = 4, /* may not be present, don't repport error to guest */
 } IOMMUAccessFlags;
 
 struct IOMMUTLBEntry {
@@ -168,10 +169,11 @@ struct MemoryRegionOps {
 };
 
 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
-
 struct MemoryRegionIOMMUOps {
     /* Return a TLB entry that contains a given address. */
-    IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
+    IOMMUTLBEntry (*translate)(MemoryRegion *iommu,
+                               hwaddr addr,
+                               IOMMUAccessFlags flags);
     /* Returns minimum supported page size */
     uint64_t (*get_min_page_size)(MemoryRegion *iommu);
     /* Called when IOMMU Notifier flag changed */
memory.c +2 -1
index 58f9269..dfbb9a0 100644
@@ -1563,7 +1563,8 @@ void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,
     granularity = memory_region_iommu_get_min_page_size(mr);
 
     for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
-        iotlb = mr->iommu_ops->translate(mr, addr, is_write);
+        iotlb = mr->iommu_ops->translate(mr, addr,
+                                         is_write ? IOMMU_WO : IOMMU_RO);
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);
         }
Signature · 1 line
1.9.1
#6 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 2/3] IOMMU: change iommu_op->translate's is_write to flags, add support to NO_FAIL flag mode

Quoted 33 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Supports translation trials without reporting error to guest on translation failure.

---
 exec.c                |  3 ++-
 hw/i386/amd_iommu.c   |  4 +--
 hw/i386/intel_iommu.c | 70 +++++++++++++++++++++++++++++++++------------------
 include/exec/memory.h |  6 +++--
 memory.c              |  3 ++-
 5 files changed, 55 insertions(+), 31 deletions(-)
exec.c +2 -1
index 374c364..266fa01 100644
@@ -432,7 +432,8 @@ MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
             break;
         }
 
-        iotlb = mr->iommu_ops->translate(mr, addr, is_write);
+        iotlb = mr->iommu_ops->translate(mr, addr,
+                                         is_write ? IOMMU_WO : IOMMU_RO);
         addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
                 | (addr & iotlb.addr_mask));
         *plen = MIN(*plen, (addr | iotlb.addr_mask) - addr + 1);
hw/i386/amd_iommu.c +0 -0
index 47b79d9..1f0d76b 100644

hw/i386/amd_iommu.c static inline bool amdvi_is_interrupt_addr(hwaddr addr) · line 991

Quoted 5 lines
hw/i386/amd_iommu.c +1 -1
@@ -988,7 +988,7 @@ static inline bool amdvi_is_interrupt_addr(hwaddr addr)
 }
 
 static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,
-                                     bool is_write)
+                                     IOMMUAccessFlags flags)

You've also updated the intel viommu implementation for the new notifier flags semantics, but none of the others (AMD and sPAPR). That needs to be done as well.

Quoted 77 lines
hw/i386/amd_iommu.c +1 -1
 {
     AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
     AMDVIState *s = as->iommu_state;
@@ -1017,7 +1017,7 @@ static IOMMUTLBEntry amdvi_translate(MemoryRegion *iommu, hwaddr addr,
         return ret;
     }
 
-    amdvi_do_translate(as, addr, is_write, &ret);
+    amdvi_do_translate(as, addr, flags & IOMMU_WO, &ret);
     trace_amdvi_translation_result(as->bus_num, PCI_SLOT(as->devfn),
             PCI_FUNC(as->devfn), addr, ret.translated_addr);
     return ret;
hw/i386/intel_iommu.c +8 -7
index 69730cb..dcf45f0 100644
@@ -364,7 +364,7 @@ static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
 /* Must not update F field now, should be done later */
 static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
                             uint16_t source_id, hwaddr addr,
-                            VTDFaultReason fault, bool is_write)
+                            VTDFaultReason fault, IOMMUAccessFlags flags)
 {
     uint64_t hi = 0, lo;
     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
@@ -373,7 +373,7 @@ static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
 
     lo = VTD_FRCD_FI(addr);
     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
-    if (!is_write) {
+    if (!(flags == IOMMU_WO || flags == IOMMU_RW)) {
         hi |= VTD_FRCD_T;
     }
     vtd_set_quad_raw(s, frcd_reg_addr, lo);
@@ -404,7 +404,7 @@ static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
 /* Log and report an DMAR (address translation) fault to software */
 static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
                                   hwaddr addr, VTDFaultReason fault,
-                                  bool is_write)
+                                  IOMMUAccessFlags flags)
 {
     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
 
@@ -415,7 +415,7 @@ static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
         return;
     }
     VTD_DPRINTF(FLOG, "sid 0x%"PRIx16 ", fault %d, addr 0x%"PRIx64
-                ", is_write %d", source_id, fault, addr, is_write);
+                ", flags %d", source_id, fault, addr, flags);
     if (fsts_reg & VTD_FSTS_PFO) {
         VTD_DPRINTF(FLOG, "new fault is not recorded due to "
                     "Primary Fault Overflow");
@@ -433,7 +433,7 @@ static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
         return;
     }
 
-    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
+    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, flags);
 
     if (fsts_reg & VTD_FSTS_PPF) {
         VTD_DPRINTF(FLOG, "there are pending faults already, "
@@ -629,7 +629,8 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
 /* Given the @gpa, get relevant @slptep. @slpte_level will be the last level
  * of the translation, can be used for deciding the size of large page.
  */
-static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, bool is_write,
+static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,
+                            IOMMUAccessFlags flags,
                             uint64_t *slptep, uint32_t *slpte_level,
                             bool *reads, bool *writes)
 {
@@ -649,7 +650,20 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, bool is_write,
     }
 
     /* FIXME: what is the Atomics request here? */
-    access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
+    switch (flags) {

hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, bool is_write, · line 663

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    case IOMMU_WO:
+        access_right_check = VTD_SL_W;
+        break;
+    case IOMMU_RO:
+        access_right_check = VTD_SL_R;
+        break;
+    case IOMMU_RW: /* passthrow */
+    case IOMMU_NO_FAIL:
+        access_right_check = VTD_SL_R | VTD_SL_W;
+        break;

I'm very confused by the semantics of IOMMU_NO_FAIL. At first I thought it was an additional flag that would be ORed with the access mode to trigger extra behaviour. But here it seems that you'd use it _instead_ of a normal access mode. I don't really see how that makes sense.

Quoted 52 lines
hw/i386/intel_iommu.c +11 -6
+    default:
+        assert(0);
+    }
 
     while (true) {
         offset = vtd_gpa_level_offset(gpa, level);
@@ -671,8 +685,9 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, bool is_write,
         if (!(slpte & access_right_check)) {
             VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
                         "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
-                        (is_write ? "write" : "read"), gpa, slpte);
-            return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
+                        (flags == IOMMU_WO ? "write" : "read"), gpa, slpte);
+            return (flags == IOMMU_RW || flags == IOMMU_WO) ?
+                   -VTD_FR_WRITE : -VTD_FR_READ;
         }
         if (vtd_slpte_nonzero_rsvd(slpte, level)) {
             VTD_DPRINTF(GENERAL, "error: non-zero reserved field in second "
@@ -789,11 +804,12 @@ static inline bool vtd_is_interrupt_addr(hwaddr addr)
  *
  * @bus_num: The bus number
  * @devfn: The devfn, which is the  combined of device and function number
- * @is_write: The access is a write operation
+ * @flags: The access is a write operation
  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
  */
 static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
-                                   uint8_t devfn, hwaddr addr, bool is_write,
+                                   uint8_t devfn, hwaddr addr,
+                                   IOMMUAccessFlags flags,
                                    IOMMUTLBEntry *entry)
 {
     IntelIOMMUState *s = vtd_as->iommu_state;
@@ -811,7 +827,7 @@ static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
 
     /* Check if the request is in interrupt address range */
     if (vtd_is_interrupt_addr(addr)) {
-        if (is_write) {
+        if (flags == IOMMU_WO || flags == IOMMU_RW) {
             /* FIXME: since we don't know the length of the access here, we
              * treat Non-DWORD length write requests without PASID as
              * interrupt requests, too. Withoud interrupt remapping support,
@@ -827,7 +843,7 @@ static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         } else {
             VTD_DPRINTF(GENERAL, "error: read request from interrupt address "
                         "gpa 0x%"PRIx64, addr);
-            vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ, is_write);
+            vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ, flags);
             return;
         }
     }

hw/i386/intel_iommu.c static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, · line 879

Quoted 10 lines
hw/i386/intel_iommu.c +3 -2
@@ -856,12 +872,14 @@ static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
         if (ret_fr) {
             ret_fr = -ret_fr;
-            if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
-                VTD_DPRINTF(FLOG, "fault processing is disabled for DMA "
+            if (flags != IOMMU_NO_FAIL) {
+                if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
+                    VTD_DPRINTF(FLOG, "fault processing is disabled for DMA "
                             "requests through this context-entry "
                             "(with FPD Set)");

Um.. the descriptions seem to imply that NO_FAIL prevents reporting failures to the guest. But here it seems to be the opposite - you only transmit the fault if the flags are != IOMMU_NO_FAIL.

Quoted 81 lines
hw/i386/intel_iommu.c +13 -10
-            } else {
-                vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
+                } else {
+                    vtd_report_dmar_fault(s, source_id, addr, ret_fr, flags);
+                }
             }
             return;
         }
@@ -874,15 +892,17 @@ static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         cc_entry->context_cache_gen = s->context_cache_gen;
     }
 
-    ret_fr = vtd_gpa_to_slpte(&ce, addr, is_write, &slpte, &level,
+    ret_fr = vtd_gpa_to_slpte(&ce, addr, flags, &slpte, &level,
                               &reads, &writes);
     if (ret_fr) {
         ret_fr = -ret_fr;
-        if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
-            VTD_DPRINTF(FLOG, "fault processing is disabled for DMA requests "
-                        "through this context-entry (with FPD Set)");
-        } else {
-            vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
+        if (flags != IOMMU_NO_FAIL) {
+            if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
+                VTD_DPRINTF(FLOG, "fault processing is disabled for DMA "
+                        "requests through this context-entry (with FPD Set)");
+            } else {
+                vtd_report_dmar_fault(s, source_id, addr, ret_fr, flags);
+            }
         }
         return;
     }
@@ -1944,7 +1964,7 @@ static void vtd_mem_write(void *opaque, hwaddr addr,
 }
 
 static IOMMUTLBEntry vtd_iommu_translate(MemoryRegion *iommu, hwaddr addr,
-                                         bool is_write)
+                                         IOMMUAccessFlags flags)
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
     IntelIOMMUState *s = vtd_as->iommu_state;
@@ -1966,7 +1986,7 @@ static IOMMUTLBEntry vtd_iommu_translate(MemoryRegion *iommu, hwaddr addr,
     }
 
     vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn, addr,
-                           is_write, &ret);
+                           flags, &ret);
     VTD_DPRINTF(MMU,
                 "bus %"PRIu8 " slot %"PRIu8 " func %"PRIu8 " devfn %"PRIu8
                 " gpa 0x%"PRIx64 " hpa 0x%"PRIx64, pci_bus_num(vtd_as->bus),
include/exec/memory.h +4 -2
index 10d7eac..0d4acb9 100644
@@ -57,6 +57,7 @@ typedef enum {
     IOMMU_RO   = 1,
     IOMMU_WO   = 2,
     IOMMU_RW   = 3,
+    IOMMU_NO_FAIL  = 4, /* may not be present, don't repport error to guest */
 } IOMMUAccessFlags;
 
 struct IOMMUTLBEntry {
@@ -168,10 +169,11 @@ struct MemoryRegionOps {
 };
 
 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
-
 struct MemoryRegionIOMMUOps {
     /* Return a TLB entry that contains a given address. */
-    IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
+    IOMMUTLBEntry (*translate)(MemoryRegion *iommu,
+                               hwaddr addr,
+                               IOMMUAccessFlags flags);
     /* Returns minimum supported page size */
     uint64_t (*get_min_page_size)(MemoryRegion *iommu);
     /* Called when IOMMU Notifier flag changed */
memory.c +0 -0
index 58f9269..dfbb9a0 100644

memory.c void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n, · line 1567

Quoted 6 lines
memory.c +2 -1
@@ -1563,7 +1563,8 @@ void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,
     granularity = memory_region_iommu_get_min_page_size(mr);
 
     for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
-        iotlb = mr->iommu_ops->translate(mr, addr, is_write);
+        iotlb = mr->iommu_ops->translate(mr, addr,
+                                         is_write ? IOMMU_WO : IOMMU_RO);

It probably makes more sense to pust the IOMMUAccessFlags parameter into memory_region_iommu_replay().

Quoted 3 lines
memory.c +0 -0
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);
         }
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#7 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 2/3] IOMMU: change iommu_op->translate's is_write to flags, add support to NO_FAIL flag mode

In reply to Aviv B.D

On Mon, Oct 17, 2016 at 06:44:23PM +0300, Aviv B.D wrote:

[...]

hw/i386/intel_iommu.c static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index) · line 367

Quoted 6 lines
hw/i386/intel_iommu.c +1 -1
@@ -364,7 +364,7 @@ static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
 /* Must not update F field now, should be done later */
 static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
                             uint16_t source_id, hwaddr addr,
-                            VTDFaultReason fault, bool is_write)
+                            VTDFaultReason fault, IOMMUAccessFlags flags)

I think we don't need to change this is_write into flags. We can just make sure we translate the flags into is_write when calling vtd_record_frcd. After all, NO_FAIL makes no sense in this function.

Quoted 13 lines
hw/i386/intel_iommu.c +1 -1
 {
     uint64_t hi = 0, lo;
     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
@@ -373,7 +373,7 @@ static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
 
     lo = VTD_FRCD_FI(addr);
     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
-    if (!is_write) {
+    if (!(flags == IOMMU_WO || flags == IOMMU_RW)) {
         hi |= VTD_FRCD_T;
     }
     vtd_set_quad_raw(s, frcd_reg_addr, lo);

hw/i386/intel_iommu.c static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id) · line 407

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
@@ -404,7 +404,7 @@ static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
 /* Log and report an DMAR (address translation) fault to software */
 static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
                                   hwaddr addr, VTDFaultReason fault,
-                                  bool is_write)
+                                  IOMMUAccessFlags flags)

Here as well. IMHO we can just keep the old is_write, and tune the callers.

Quoted 13 lines
hw/i386/intel_iommu.c +1 -1
 {
     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
 
@@ -415,7 +415,7 @@ static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
         return;
     }
     VTD_DPRINTF(FLOG, "sid 0x%"PRIx16 ", fault %d, addr 0x%"PRIx64
-                ", is_write %d", source_id, fault, addr, is_write);
+                ", flags %d", source_id, fault, addr, flags);
     if (fsts_reg & VTD_FSTS_PFO) {
         VTD_DPRINTF(FLOG, "new fault is not recorded due to "
                     "Primary Fault Overflow");

hw/i386/intel_iommu.c static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id, · line 436

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
@@ -433,7 +433,7 @@ static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
         return;
     }
 
-    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
+    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, flags);

... so if you agree on my previous comments, here we can use:

       vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault,
                       flags & IOMMU_WO);

and keep the vtd_record_frcd() untouched.

[...]

hw/i386/intel_iommu.c static inline bool vtd_is_interrupt_addr(hwaddr addr) · line 807

Quoted 6 lines
hw/i386/intel_iommu.c +1 -1
@@ -789,11 +804,12 @@ static inline bool vtd_is_interrupt_addr(hwaddr addr)
  *
  * @bus_num: The bus number
  * @devfn: The devfn, which is the  combined of device and function number
- * @is_write: The access is a write operation
+ * @flags: The access is a write operation

Need to fix comment to suite the new flag.

Quoted 10 lines
hw/i386/intel_iommu.c +2 -1
  * @entry: IOMMUTLBEntry that contain the addr to be translated and result
  */
 static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
-                                   uint8_t devfn, hwaddr addr, bool is_write,
+                                   uint8_t devfn, hwaddr addr,
+                                   IOMMUAccessFlags flags,
                                    IOMMUTLBEntry *entry)
 {
     IntelIOMMUState *s = vtd_as->iommu_state;

hw/i386/intel_iommu.c static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, · line 830

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
@@ -811,7 +827,7 @@ static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
 
     /* Check if the request is in interrupt address range */
     if (vtd_is_interrupt_addr(addr)) {
-        if (is_write) {
+        if (flags == IOMMU_WO || flags == IOMMU_RW) {

I suggest we directly use (flags & IOMMU_WO) in all similar places.

hw/i386/intel_iommu.c static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, · line 846

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
             /* FIXME: since we don't know the length of the access here, we
              * treat Non-DWORD length write requests without PASID as
              * interrupt requests, too. Withoud interrupt remapping support,
@@ -827,7 +843,7 @@ static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         } else {
             VTD_DPRINTF(GENERAL, "error: read request from interrupt address "
                         "gpa 0x%"PRIx64, addr);
-            vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ, is_write);
+            vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ, flags);

Again, we can avoid touching vtd_report_dmar_fault() interface, and use the old is_write. Looks like NO_FAIL makes no sense for it as well.

Thanks,

-- peterx

author
#8 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 2/3] IOMMU: change iommu_op->translate's is_write to flags, add support to NO_FAIL flag mode

Hi,
You are right, and I will revert those functions you mentioned.

Thanks,
Aviv.

Quoted 130 lines · Peter Xu

On Mon, Oct 17, 2016 at 06:44:23PM +0300, Aviv B.D wrote:

[...]

Review static void vtd_set_frcd_and_update_ppf(IntelIOMMUState

Quoted 1 line
(quoted diff) +0 -0
@@ -364,7 +364,7 @@ static void vtd_set_frcd_and_update_ppf(IntelIOMMUState

*s, uint16_t index)

hw/i386/intel_iommu.c static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
 /* Must not update F field now, should be done later */
 static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
                             uint16_t source_id, hwaddr addr,
-                            VTDFaultReason fault, bool is_write)
+                            VTDFaultReason fault, IOMMUAccessFlags

flags)

I think we don't need to change this is_write into flags. We can just make sure we translate the flags into is_write when calling vtd_record_frcd. After all, NO_FAIL makes no sense in this function.

hw/i386/intel_iommu.c static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
 {
     uint64_t hi = 0, lo;
     hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) <<

4);

Review static void vtd_record_frcd(IntelIOMMUState *s,

Quoted 1 line
(quoted diff) +0 -0
@@ -373,7 +373,7 @@ static void vtd_record_frcd(IntelIOMMUState *s,

uint16_t index,

hw/i386/intel_iommu.c static bool vtd_try_collapse_fault(IntelIOMMUState

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
     lo = VTD_FRCD_FI(addr);
     hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
-    if (!is_write) {
+    if (!(flags == IOMMU_WO || flags == IOMMU_RW)) {
         hi |= VTD_FRCD_T;
     }
     vtd_set_quad_raw(s, frcd_reg_addr, lo);
@@ -404,7 +404,7 @@ static bool vtd_try_collapse_fault(IntelIOMMUState

*s, uint16_t source_id)

Review static bool vtd_try_collapse_fault(IntelIOMMUState · line 405

Quoted 2 lines
(quoted diff) +0 -0
 /* Log and report an DMAR (address translation) fault to software */
 static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t

source_id,

hw/i386/intel_iommu.c static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id) · line 407

Quoted 3 lines
hw/i386/intel_iommu.c +1 -1
                                   hwaddr addr, VTDFaultReason fault,
-                                  bool is_write)
+                                  IOMMUAccessFlags flags)

Here as well. IMHO we can just keep the old is_write, and tune the callers.

hw/i386/intel_iommu.c static void vtd_report_dmar_fault(IntelIOMMUState

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
 {
     uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
 
@@ -415,7 +415,7 @@ static void vtd_report_dmar_fault(IntelIOMMUState

*s, uint16_t source_id,

hw/i386/intel_iommu.c static void vtd_report_dmar_fault(IntelIOMMUState

Quoted 9 lines
hw/i386/intel_iommu.c +1 -1
         return;
     }
     VTD_DPRINTF(FLOG, "sid 0x%"PRIx16 ", fault %d, addr 0x%"PRIx64
-                ", is_write %d", source_id, fault, addr, is_write);
+                ", flags %d", source_id, fault, addr, flags);
     if (fsts_reg & VTD_FSTS_PFO) {
         VTD_DPRINTF(FLOG, "new fault is not recorded due to "
                     "Primary Fault Overflow");
@@ -433,7 +433,7 @@ static void vtd_report_dmar_fault(IntelIOMMUState

*s, uint16_t source_id,

Review static void vtd_report_dmar_fault(IntelIOMMUState · line 436

Quoted 4 lines
(quoted diff) +0 -1
         return;
     }
 
-    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault,

is_write);

hw/i386/intel_iommu.c static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id, · line 436

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, flags);

... so if you agree on my previous comments, here we can use:

       vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault,
                       flags & IOMMU_WO);

and keep the vtd_record_frcd() untouched.

[...]

Review static inline bool vtd_is_interrupt_addr(hwaddr

Quoted 1 line
(quoted diff) +0 -0
@@ -789,11 +804,12 @@ static inline bool vtd_is_interrupt_addr(hwaddr

addr)

hw/i386/intel_iommu.c static inline bool vtd_is_interrupt_addr(hwaddr addr)

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
  *
  * @bus_num: The bus number
  * @devfn: The devfn, which is the  combined of device and function

number

hw/i386/intel_iommu.c static inline bool vtd_is_interrupt_addr(hwaddr addr) · line 807

Quoted 2 lines
hw/i386/intel_iommu.c +1 -1
- * @is_write: The access is a write operation
+ * @flags: The access is a write operation

Need to fix comment to suite the new flag.

Review static inline bool vtd_is_interrupt_addr(hwaddr · line 808

Quoted 1 line
(quoted diff) +0 -0
  * @entry: IOMMUTLBEntry that contain the addr to be translated and

result

Review static inline bool vtd_is_interrupt_addr(hwaddr · line 810

Quoted 2 lines
(quoted diff) +0 -0
  */
 static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus

*bus,

Review static inline bool vtd_is_interrupt_addr(hwaddr · line 796

Quoted 1 line
(quoted diff) +0 -1
-                                   uint8_t devfn, hwaddr addr, bool

is_write,

hw/i386/intel_iommu.c static void vtd_do_iommu_translate(VTDAddressSpace

Quoted 6 lines
hw/i386/intel_iommu.c +2 -0
+                                   uint8_t devfn, hwaddr addr,
+                                   IOMMUAccessFlags flags,
                                    IOMMUTLBEntry *entry)
 {
     IntelIOMMUState *s = vtd_as->iommu_state;
@@ -811,7 +827,7 @@ static void vtd_do_iommu_translate(VTDAddressSpace

*vtd_as, PCIBus *bus,

hw/i386/intel_iommu.c static void vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus, · line 830

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
     /* Check if the request is in interrupt address range */
     if (vtd_is_interrupt_addr(addr)) {
-        if (is_write) {
+        if (flags == IOMMU_WO || flags == IOMMU_RW) {

I suggest we directly use (flags & IOMMU_WO) in all similar places.

Review static void vtd_do_iommu_translate(VTDAddressSpace · line 830

Quoted 1 line
(quoted diff) +0 -0
             /* FIXME: since we don't know the length of the access

here, we

Review static void vtd_do_iommu_translate(VTDAddressSpace · line 832

Quoted 2 lines
(quoted diff) +0 -0
              * treat Non-DWORD length write requests without PASID as
              * interrupt requests, too. Withoud interrupt remapping

support,

Review static void vtd_do_iommu_translate(VTDAddressSpace

Quoted 1 line
(quoted diff) +0 -0
@@ -827,7 +843,7 @@ static void vtd_do_iommu_translate(VTDAddressSpace

*vtd_as, PCIBus *bus,

Review static void vtd_do_iommu_translate(VTDAddressSpace · line 844

Quoted 2 lines
(quoted diff) +0 -0
         } else {
             VTD_DPRINTF(GENERAL, "error: read request from interrupt

address "

Review static void vtd_do_iommu_translate(VTDAddressSpace · line 830

Quoted 2 lines
(quoted diff) +0 -1
                         "gpa 0x%"PRIx64, addr);
-            vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ,

is_write);

Review static void vtd_do_iommu_translate(VTDAddressSpace · line 846

Quoted 1 line
(quoted diff) +1 -0
+            vtd_report_dmar_fault(s, source_id, addr, VTD_FR_READ,

flags);

Again, we can avoid touching vtd_report_dmar_fault() interface, and use the old is_write. Looks like NO_FAIL makes no sense for it as well.

Thanks,

-- peterx

author
#9 lore raw

PATCH RESEND v4 3/3 IOMMU: enable intel_iommu map and unmap notifiers

In reply to Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.

---
 hw/i386/intel_iommu.c          | 102 ++++++++++++++++++++++++++++++++++++++---
 hw/i386/intel_iommu_internal.h |   2 +
 include/hw/i386/intel_iommu.h  |   9 ++++
 3 files changed, 106 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +95 -7
index dcf45f0..34fc1e8 100644
@@ -51,6 +51,9 @@ static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);
 #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
 #endif
 
+static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
+                                    uint8_t devfn, VTDContextEntry *ce);
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -142,6 +145,23 @@ static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
     return new_val;
 }
 
+static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num, uint8_t devfn,
+                           uint16_t *domain_id)
+{
+    VTDContextEntry ce;
+    int ret_fr;
+
+    assert(domain_id);
+
+    ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+    if (ret_fr) {
+        return -1;
+    }
+
+    *domain_id =  VTD_CONTEXT_ENTRY_DID(ce.hi);
+    return 0;
+}
+
 /* GHashTable functions */
 static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
 {
@@ -683,9 +703,6 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,
         *reads = (*reads) && (slpte & VTD_SL_R);
         *writes = (*writes) && (slpte & VTD_SL_W);
         if (!(slpte & access_right_check)) {
-            VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
-                        "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
-                        (flags == IOMMU_WO ? "write" : "read"), gpa, slpte);
             return (flags == IOMMU_RW || flags == IOMMU_WO) ?
                    -VTD_FR_WRITE : -VTD_FR_READ;
         }
@@ -734,9 +751,6 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
     }
 
     if (!vtd_context_entry_present(ce)) {
-        VTD_DPRINTF(GENERAL,
-                    "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") "
-                    "is not present", devfn, bus_num);
         return -VTD_FR_CONTEXT_ENTRY_P;
     } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
                (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {
@@ -1065,6 +1079,55 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
                                 &domain_id);
 }
 
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id, hwaddr addr,
+                                           uint8_t am)
+{
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus), vtd_as->devfn,
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {
+                VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask %d",
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K + am);
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu, entry);
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d", addr, am);
+                while (addr < original_addr + (1 << am) * VTD_PAGE_SIZE) {
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+                                                         &node->vtd_as->iommu,
+                                                         addr,
+                                                         IOMMU_NO_FAIL);
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&node->vtd_as->iommu, entry);
+                    } else {
+                        addr += VTD_PAGE_SIZE;
+                    }
+                }
+            }
+        }
+    }
+}
+
+
 static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                       hwaddr addr, uint8_t am)
 {
@@ -1075,6 +1138,8 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
     info.addr = addr;
     info.mask = ~((1 << am) - 1);
     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
+
+    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
 }
 
 /* Flush IOTLB
@@ -2000,8 +2065,10 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
                                           IOMMUNotifierFlag new)
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    IntelIOMMUNotifierNode *node = NULL;
 
-    if (new & IOMMU_NOTIFIER_MAP) {
+    if (!s->cache_mode_enabled && new & IOMMU_NOTIFIER_MAP) {
         error_report("Device at bus %s addr %02x.%d requires iommu "
                      "notifier which is currently not supported by "
                      "intel-iommu emulation",
@@ -2009,6 +2076,27 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
                      PCI_FUNC(vtd_as->devfn));
         exit(1);
     }
+
+    /* Add new ndoe if no mapping was exising before this call */
+    if (old == IOMMU_NOTIFIER_NONE) {
+        node = g_malloc0(sizeof(*node));
+        node->vtd_as = vtd_as;
+        node->notifier_flag = new;
+        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
+        return;
+    }
+
+    /* update notifier node with new flags */
+    QLIST_FOREACH(node, &s->notifiers_list, next) {
+        if (node->vtd_as == vtd_as) {
+            if (new == IOMMU_NOTIFIER_NONE) {
+                QLIST_REMOVE(node, next);
+            } else {
+                node->notifier_flag = new;
+            }
+            return;
+        }
+    }
 }
 
 static const VMStateDescription vtd_vmstate = {
hw/i386/intel_iommu_internal.h +2 -0
index 35d9f3a..96df4ae 100644
@@ -381,6 +381,8 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
 #define VTD_PAGE_SHIFT_1G           30
 #define VTD_PAGE_MASK_1G            (~((1ULL << VTD_PAGE_SHIFT_1G) - 1))
 
+#define VTD_PAGE_MASK(shift)        (~((1ULL << (shift)) - 1))
+
 struct VTDRootEntry {
     uint64_t val;
     uint64_t rsvd;
include/hw/i386/intel_iommu.h +9 -0
index 7a94f16..c160706 100644
@@ -63,6 +63,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDIrq VTDIrq;
 typedef struct VTD_MSIMessage VTD_MSIMessage;
+typedef struct IntelIOMMUNotifierNode IntelIOMMUNotifierNode;
 
 /* Context-Entry */
 struct VTDContextEntry {
@@ -248,6 +249,12 @@ struct VTD_MSIMessage {
 /* When IR is enabled, all MSI/MSI-X data bits should be zero */
 #define VTD_IR_MSI_DATA          (0)
 
+struct IntelIOMMUNotifierNode {
+    VTDAddressSpace *vtd_as;
+    IOMMUNotifierFlag notifier_flag;
+    QLIST_ENTRY(IntelIOMMUNotifierNode) next;
+};
+
 /* The iommu (DMAR) device state struct */
 struct IntelIOMMUState {
     X86IOMMUState x86_iommu;
@@ -285,6 +292,8 @@ struct IntelIOMMUState {
     MemoryRegionIOMMUOps iommu_ops;
     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
     VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed by bus number */
+    /* list of registered notifiers */
+    QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list;
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
Signature · 1 line
1.9.1
#10 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

Quoted 71 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.

---
 hw/i386/intel_iommu.c          | 102 ++++++++++++++++++++++++++++++++++++++---
 hw/i386/intel_iommu_internal.h |   2 +
 include/hw/i386/intel_iommu.h  |   9 ++++
 3 files changed, 106 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +20 -6
index dcf45f0..34fc1e8 100644
@@ -51,6 +51,9 @@ static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);
 #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
 #endif
 
+static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
+                                    uint8_t devfn, VTDContextEntry *ce);
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -142,6 +145,23 @@ static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
     return new_val;
 }
 
+static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num, uint8_t devfn,
+                           uint16_t *domain_id)
+{
+    VTDContextEntry ce;
+    int ret_fr;
+
+    assert(domain_id);
+
+    ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+    if (ret_fr) {
+        return -1;
+    }
+
+    *domain_id =  VTD_CONTEXT_ENTRY_DID(ce.hi);
+    return 0;
+}
+
 /* GHashTable functions */
 static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
 {
@@ -683,9 +703,6 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,
         *reads = (*reads) && (slpte & VTD_SL_R);
         *writes = (*writes) && (slpte & VTD_SL_W);
         if (!(slpte & access_right_check)) {
-            VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
-                        "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
-                        (flags == IOMMU_WO ? "write" : "read"), gpa, slpte);
             return (flags == IOMMU_RW || flags == IOMMU_WO) ?
                    -VTD_FR_WRITE : -VTD_FR_READ;
         }
@@ -734,9 +751,6 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
     }
 
     if (!vtd_context_entry_present(ce)) {
-        VTD_DPRINTF(GENERAL,
-                    "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") "
-                    "is not present", devfn, bus_num);
         return -VTD_FR_CONTEXT_ENTRY_P;
     } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
                (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1088

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -1065,6 +1079,55 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
                                 &domain_id);
 }
 
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id, hwaddr addr,
+                                           uint8_t am)
+{
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {

It's not really obvious to me why you need this additional list of IntelIOMMUNotifierNode structures, rather than just the notifier list already built into each MemoryRegion.

Quoted 141 lines
hw/i386/intel_iommu.c +68 -1
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus), vtd_as->devfn,
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {
+                VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask %d",
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K + am);
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu, entry);
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d", addr, am);
+                while (addr < original_addr + (1 << am) * VTD_PAGE_SIZE) {
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+                                                         &node->vtd_as->iommu,
+                                                         addr,
+                                                         IOMMU_NO_FAIL);
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&node->vtd_as->iommu, entry);
+                    } else {
+                        addr += VTD_PAGE_SIZE;
+                    }
+                }
+            }
+        }
+    }
+}
+
+
 static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                       hwaddr addr, uint8_t am)
 {
@@ -1075,6 +1138,8 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
     info.addr = addr;
     info.mask = ~((1 << am) - 1);
     g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
+
+    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
 }
 
 /* Flush IOTLB
@@ -2000,8 +2065,10 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
                                           IOMMUNotifierFlag new)
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    IntelIOMMUNotifierNode *node = NULL;
 
-    if (new & IOMMU_NOTIFIER_MAP) {
+    if (!s->cache_mode_enabled && new & IOMMU_NOTIFIER_MAP) {
         error_report("Device at bus %s addr %02x.%d requires iommu "
                      "notifier which is currently not supported by "
                      "intel-iommu emulation",
@@ -2009,6 +2076,27 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
                      PCI_FUNC(vtd_as->devfn));
         exit(1);
     }
+
+    /* Add new ndoe if no mapping was exising before this call */
+    if (old == IOMMU_NOTIFIER_NONE) {
+        node = g_malloc0(sizeof(*node));
+        node->vtd_as = vtd_as;
+        node->notifier_flag = new;
+        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
+        return;
+    }
+
+    /* update notifier node with new flags */
+    QLIST_FOREACH(node, &s->notifiers_list, next) {
+        if (node->vtd_as == vtd_as) {
+            if (new == IOMMU_NOTIFIER_NONE) {
+                QLIST_REMOVE(node, next);
+            } else {
+                node->notifier_flag = new;
+            }
+            return;
+        }
+    }
 }
 
 static const VMStateDescription vtd_vmstate = {
hw/i386/intel_iommu_internal.h +2 -0
index 35d9f3a..96df4ae 100644
@@ -381,6 +381,8 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
 #define VTD_PAGE_SHIFT_1G           30
 #define VTD_PAGE_MASK_1G            (~((1ULL << VTD_PAGE_SHIFT_1G) - 1))
 
+#define VTD_PAGE_MASK(shift)        (~((1ULL << (shift)) - 1))
+
 struct VTDRootEntry {
     uint64_t val;
     uint64_t rsvd;
include/hw/i386/intel_iommu.h +9 -0
index 7a94f16..c160706 100644
@@ -63,6 +63,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDIrq VTDIrq;
 typedef struct VTD_MSIMessage VTD_MSIMessage;
+typedef struct IntelIOMMUNotifierNode IntelIOMMUNotifierNode;
 
 /* Context-Entry */
 struct VTDContextEntry {
@@ -248,6 +249,12 @@ struct VTD_MSIMessage {
 /* When IR is enabled, all MSI/MSI-X data bits should be zero */
 #define VTD_IR_MSI_DATA          (0)
 
+struct IntelIOMMUNotifierNode {
+    VTDAddressSpace *vtd_as;
+    IOMMUNotifierFlag notifier_flag;
+    QLIST_ENTRY(IntelIOMMUNotifierNode) next;
+};
+
 /* The iommu (DMAR) device state struct */
 struct IntelIOMMUState {
     X86IOMMUState x86_iommu;
@@ -285,6 +292,8 @@ struct IntelIOMMUState {
     MemoryRegionIOMMUOps iommu_ops;
     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
     VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed by bus number */
+    /* list of registered notifiers */
+    QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list;
 
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#11 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

In reply to Aviv B.D
Quoted 34 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.

---
 hw/i386/intel_iommu.c          | 102 ++++++++++++++++++++++++++++++++++++++---
 hw/i386/intel_iommu_internal.h |   2 +
 include/hw/i386/intel_iommu.h  |   9 ++++
 3 files changed, 106 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +7 -0
index dcf45f0..34fc1e8 100644
@@ -51,6 +51,9 @@ static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);
 #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
 #endif
 
+static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
+                                    uint8_t devfn, VTDContextEntry *ce);
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -142,6 +145,23 @@ static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
     return new_val;
 }
 
+static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num, uint8_t devfn,
+                           uint16_t *domain_id)
+{
+    VTDContextEntry ce;

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, · line 161

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    int ret_fr;
+
+    assert(domain_id);
+
+    ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+    if (ret_fr) {
+        return -1;
+    }
+
+    *domain_id =  VTD_CONTEXT_ENTRY_DID(ce.hi);

^ one more space

hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, · line 688

Quoted 13 lines
hw/i386/intel_iommu.c +3 -3
+    return 0;
+}
+
 /* GHashTable functions */
 static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
 {
@@ -683,9 +703,6 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,
         *reads = (*reads) && (slpte & VTD_SL_R);
         *writes = (*writes) && (slpte & VTD_SL_W);
         if (!(slpte & access_right_check)) {
-            VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
-                        "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
-                        (flags == IOMMU_WO ? "write" : "read"), gpa, slpte);

Could I ask why we are removing these lines? It can be useful if we have permission issues.

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 739

Quoted 10 lines
hw/i386/intel_iommu.c +0 -3
             return (flags == IOMMU_RW || flags == IOMMU_WO) ?
                    -VTD_FR_WRITE : -VTD_FR_READ;
         }
@@ -734,9 +751,6 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
     }
 
     if (!vtd_context_entry_present(ce)) {
-        VTD_DPRINTF(GENERAL,
-                    "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") "
-                    "is not present", devfn, bus_num);

Here as well. Any reason to remove it?

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1085

Quoted 11 lines
hw/i386/intel_iommu.c +4 -0
         return -VTD_FR_CONTEXT_ENTRY_P;
     } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
                (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {
@@ -1065,6 +1079,55 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
                                 &domain_id);
 }
 
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id, hwaddr addr,
+                                           uint8_t am)
+{

The logic of this function looks strange to me.

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1097

Quoted 12 lines
hw/i386/intel_iommu.c +12 -0
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus), vtd_as->devfn,
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {

First of all, if we are talking about VFIO, notifier_flag should always be MAP|UNMAP. So in that case, for newly mapped entries, looks like we will first send an UNMAP, then a MAP?

Quoted 15 lines
hw/i386/intel_iommu.c +15 -0
+                VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask %d",
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K + am);
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu, entry);
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d", addr, am);
+                while (addr < original_addr + (1 << am) * VTD_PAGE_SIZE) {

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1122

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+                                                         &node->vtd_as->iommu,
+                                                         addr,
+                                                         IOMMU_NO_FAIL);
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&node->vtd_as->iommu, entry);
+                    } else {
+                        addr += VTD_PAGE_SIZE;

IIUC, here is the point that we found "the page is gone" (so this is an UNMAP invalidation), and we should do memory_region_iommu_notify() for the whole area with IOMMU_NONE. Then we just quit the loop since continuous translate()s should fail as well if the first page is missing.

Please correct if I am wrong.

Thanks,

-- peterx

author
#12 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

Quoted 67 lines · Peter Xu
Quoted 8 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.

---
 hw/i386/intel_iommu.c          | 102 ++++++++++++++++++++++++++++++

++++++++---

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) |

Quoted 9 lines
 hw/i386/intel_iommu_internal.h |   2 +
 include/hw/i386/intel_iommu.h  |   9 ++++
 3 files changed, 106 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +0 -0
index dcf45f0..34fc1e8 100644
@@ -51,6 +51,9 @@ static int vtd_dbgflags = VTD_DBGBIT(GENERAL) |

VTD_DBGBIT(CSR);

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
 #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
 #endif
 
+static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t

bus_num,

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);

Quoted 3 lines
hw/i386/intel_iommu.c +2 -0
+                                    uint8_t devfn, VTDContextEntry *ce);
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t

val,

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -142,6 +145,23 @@ static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState

*s, hwaddr addr,

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return new_val;
 }
 
+static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num,

uint8_t devfn,

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, · line 161

Quoted 13 lines
hw/i386/intel_iommu.c +13 -0
+                           uint16_t *domain_id)
+{
+    VTDContextEntry ce;
+    int ret_fr;
+
+    assert(domain_id);
+
+    ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+    if (ret_fr) {
+        return -1;
+    }
+
+    *domain_id =  VTD_CONTEXT_ENTRY_DID(ce.hi);

^ one more space

hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce,

Quoted 7 lines
hw/i386/intel_iommu.c +3 -0
+    return 0;
+}
+
 /* GHashTable functions */
 static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
 {
@@ -683,9 +703,6 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce,

uint64_t gpa,

hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,

Quoted 6 lines
hw/i386/intel_iommu.c +0 -3
         *reads = (*reads) && (slpte & VTD_SL_R);
         *writes = (*writes) && (slpte & VTD_SL_W);
         if (!(slpte & access_right_check)) {
-            VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
-                        "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
-                        (flags == IOMMU_WO ? "write" : "read"), gpa,
Quoted 5 lines

slpte);

Could I ask why we are removing these lines? It can be useful if we have permission issues.

I will return Those lines if flags & NO_FAIL == 0

Quoted 6 lines

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
             return (flags == IOMMU_RW || flags == IOMMU_WO) ?
                    -VTD_FR_WRITE : -VTD_FR_READ;
         }
@@ -734,9 +751,6 @@ static int vtd_dev_to_context_entry(IntelIOMMUState

*s, uint8_t bus_num,

Quoted 10 lines

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 739

Quoted 6 lines
hw/i386/intel_iommu.c +0 -3
     }
 
     if (!vtd_context_entry_present(ce)) {
-        VTD_DPRINTF(GENERAL,
-                    "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") "
-                    "is not present", devfn, bus_num);

Here as well. Any reason to remove it?

Here as well...

Quoted 30 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
         return -VTD_FR_CONTEXT_ENTRY_P;
     } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
                (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {
@@ -1065,6 +1079,55 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState

*s, uint16_t domain_id)

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
                                 &domain_id);
 }
 
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id, hwaddr

addr,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1085

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                                           uint8_t am)
+{

The logic of this function looks strange to me.

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus),

vtd_as->devfn,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1097

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {
Quoted 4 lines

First of all, if we are talking about VFIO, notifier_flag should always be MAP|UNMAP. So in that case, for newly mapped entries, looks like we will first send an UNMAP, then a MAP?

You are correct, there is no valid reason to have notifier_flag other than MAP|UNMAP
at least for VFIO.
I'm not sure if in the feature there won't be good reason to do otherwise, so my
code support this scenario...

Quoted 32 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+                VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask %d",
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K +

am);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState · line 1105

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d",

addr, am);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState · line 1112

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                while (addr < original_addr + (1 << am) *

VTD_PAGE_SIZE) {

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1129

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+

&node->vtd_as->iommu,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                                                         addr,
+                                                         IOMMU_NO_FAIL);
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1122

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                    } else {
+                        addr += VTD_PAGE_SIZE;
Quoted 8 lines

IIUC, here is the point that we found "the page is gone" (so this is an UNMAP invalidation), and we should do memory_region_iommu_notify() for the whole area with IOMMU_NONE. Then we just quit the loop since continuous translate()s should fail as well if the first page is missing.

Please correct if I am wrong.

If I remember correctly I encounter a few cases where there was hole of unmaped
memory in the middle of otherwise mapped pages. If I remember correctly it was
with linux kernel 4.4, but I'm not sure.

Quoted 4 lines

Thanks,

-- peterx

author
#13 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

I will fix all those issues.
Thanks,
Aviv.

Quoted 198 lines · Aviv B.D.
Quoted 68 lines · Peter Xu
Quoted 8 lines · Aviv B.D

From: "Aviv Ben-David" <[email protected]>

Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain.

---
 hw/i386/intel_iommu.c          | 102 ++++++++++++++++++++++++++++++

++++++++---

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) |

Quoted 9 lines
 hw/i386/intel_iommu_internal.h |   2 +
 include/hw/i386/intel_iommu.h  |   9 ++++
 3 files changed, 106 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +0 -0
index dcf45f0..34fc1e8 100644
@@ -51,6 +51,9 @@ static int vtd_dbgflags = VTD_DBGBIT(GENERAL) |

VTD_DBGBIT(CSR);

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR);

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
 #define VTD_DPRINTF(what, fmt, ...) do {} while (0)
 #endif
 
+static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t

bus_num,

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | · line 55

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                                    uint8_t devfn, VTDContextEntry

*ce);

hw/i386/intel_iommu.c static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | · line 57

Quoted 2 lines
hw/i386/intel_iommu.c +1 -0
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t

val,

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -142,6 +145,23 @@ static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState

*s, hwaddr addr,

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return new_val;
 }
 
+static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num,

uint8_t devfn,

hw/i386/intel_iommu.c static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, · line 161

Quoted 13 lines
hw/i386/intel_iommu.c +13 -0
+                           uint16_t *domain_id)
+{
+    VTDContextEntry ce;
+    int ret_fr;
+
+    assert(domain_id);
+
+    ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+    if (ret_fr) {
+        return -1;
+    }
+
+    *domain_id =  VTD_CONTEXT_ENTRY_DID(ce.hi);

^ one more space

hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce,

Quoted 7 lines
hw/i386/intel_iommu.c +3 -0
+    return 0;
+}
+
 /* GHashTable functions */
 static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
 {
@@ -683,9 +703,6 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce,

uint64_t gpa,

hw/i386/intel_iommu.c static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa,

Quoted 6 lines
hw/i386/intel_iommu.c +0 -3
         *reads = (*reads) && (slpte & VTD_SL_R);
         *writes = (*writes) && (slpte & VTD_SL_W);
         if (!(slpte & access_right_check)) {
-            VTD_DPRINTF(GENERAL, "error: lack of %s permission for "
-                        "gpa 0x%"PRIx64 " slpte 0x%"PRIx64,
-                        (flags == IOMMU_WO ? "write" : "read"), gpa,
Quoted 5 lines

slpte);

Could I ask why we are removing these lines? It can be useful if we have permission issues.

I will return Those lines if flags & NO_FAIL == 0

Quoted 6 lines

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
             return (flags == IOMMU_RW || flags == IOMMU_WO) ?
                    -VTD_FR_WRITE : -VTD_FR_READ;
         }
@@ -734,9 +751,6 @@ static int vtd_dev_to_context_entry(IntelIOMMUState

*s, uint8_t bus_num,

Quoted 10 lines

hw/i386/intel_iommu.c static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, · line 739

Quoted 6 lines
hw/i386/intel_iommu.c +0 -3
     }
 
     if (!vtd_context_entry_present(ce)) {
-        VTD_DPRINTF(GENERAL,
-                    "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") "
-                    "is not present", devfn, bus_num);

Here as well. Any reason to remove it?

Here as well...

Quoted 30 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
         return -VTD_FR_CONTEXT_ENTRY_P;
     } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
                (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) {
@@ -1065,6 +1079,55 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState

*s, uint16_t domain_id)

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 5 lines
hw/i386/intel_iommu.c +2 -0
                                 &domain_id);
 }
 
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id, hwaddr

addr,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1085

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                                           uint8_t am)
+{

The logic of this function looks strange to me.

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus),

vtd_as->devfn,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1097

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {
Quoted 4 lines

First of all, if we are talking about VFIO, notifier_flag should always be MAP|UNMAP. So in that case, for newly mapped entries, looks like we will first send an UNMAP, then a MAP?

You are correct, there is no valid reason to have notifier_flag other than MAP|UNMAP
at least for VFIO.
I'm not sure if in the feature there won't be good reason to do otherwise, so my
code support this scenario...

Quoted 34 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState · line 1098

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask

%d",

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K +

am);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState · line 1105

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d",

addr, am);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState · line 1112

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+                while (addr < original_addr + (1 << am) *

VTD_PAGE_SIZE) {

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1129

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+

&node->vtd_as->iommu,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState · line 1117

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                                                         addr,
+

IOMMU_NO_FAIL);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1122

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                    } else {
+                        addr += VTD_PAGE_SIZE;
Quoted 8 lines

IIUC, here is the point that we found "the page is gone" (so this is an UNMAP invalidation), and we should do memory_region_iommu_notify() for the whole area with IOMMU_NONE. Then we just quit the loop since continuous translate()s should fail as well if the first page is missing.

Please correct if I am wrong.

If I remember correctly I encounter a few cases where there was hole of unmaped
memory in the middle of otherwise mapped pages. If I remember correctly it was
with linux kernel 4.4, but I'm not sure.

Quoted 4 lines

Thanks,

-- peterx

#14 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

In reply to Aviv B.D.

On Thu, Oct 20, 2016 at 10:11:15PM +0300, Aviv B.D. wrote:

[...]

Quoted 22 lines
Quoted 22 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id, hwaddr

addr,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1085

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                                           uint8_t am)
+{

The logic of this function looks strange to me.

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus),

vtd_as->devfn,

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {
Quoted 8 lines
Quoted 4 lines

First of all, if we are talking about VFIO, notifier_flag should always be MAP|UNMAP. So in that case, for newly mapped entries, looks like we will first send an UNMAP, then a MAP?

You are correct, there is no valid reason to have notifier_flag other than MAP|UNMAP
at least for VFIO.

Not sure whether you know about upstream vhost work, vhost is going to support Intel IOMMU, in that case, it will only register to UNMAP notifier, not MAP.

Quoted 3 lines

I'm not sure if in the feature there won't be good reason to do otherwise, so my
code support this scenario...

My point is, I think you should not send this notify unconditionally. IMO the logic should be simpler here, like:

    foreach (page in invalidation range) {
        IOTLBEntry entry = m->iommu_ops.translate();
        if (entry.perm != IOMMU_NONE && notifier_flag & NOTIIFER_MAP) {
            /* this is map, user requested MAP flag */
            memory_region_iommu_notify(entry);
        } else if (entry.perm == IOMMU_NONE && notifier_flag &
                NOTIFIER_UNMAP) {
            /* this is unmap, user requested UNMAP */
            entry = ... /* setup the entry */
            memory_region_iommu_notify(entry);
        }
    }

This is to follow your logic. I don't know whether this is efficient enough, maybe good for the first version. The problem is, when you call translate(), you will need to go over the page every time from root dir. A faster way may be: provide a function to walk specific address range. If you are going to implement the replay logic that Alex/David has mentioned, maybe that will help too (walk over the whole 64bit range).

Quoted 40 lines
Quoted 40 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 8 lines
hw/i386/intel_iommu.c +6 -0
+                VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask %d",
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K +

am);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d",

addr, am);

Quoted 1 line

+ while (addr < original_addr + (1 << am) *

VTD_PAGE_SIZE) {

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1129

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+

&node->vtd_as->iommu,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                                                         addr,
+                                                         IOMMU_NO_FAIL);
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1122

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                    } else {
+                        addr += VTD_PAGE_SIZE;

IIUC, here is the point that we found "the page is gone" (so this is an UNMAP invalidation), and we should do memory_region_iommu_notify() for the whole area with IOMMU_NONE. Then we just quit the loop since continuous translate()s should fail as well if the first page is missing.

Quoted 8 lines
Quoted 2 lines

Please correct if I am wrong.

If I remember correctly I encounter a few cases where there was hole of unmaped
memory in the middle of otherwise mapped pages. If I remember correctly it was
with linux kernel 4.4, but I'm not sure.

I see. Thanks.

-- peterx

author
#15 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

Quoted 49 lines · Peter Xu

On Thu, Oct 20, 2016 at 10:11:15PM +0300, Aviv B.D. wrote:

[...]

Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
+                                           uint16_t domain_id,

hwaddr

Quoted 20 lines
Quoted 20 lines

addr,

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1085

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                                           uint8_t am)
+{

The logic of this function looks strange to me.

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+    IntelIOMMUNotifierNode *node;
+
+    QLIST_FOREACH(node, &(s->notifiers_list), next) {
+        VTDAddressSpace *vtd_as = node->vtd_as;
+        uint16_t vfio_domain_id;
+        int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus),

vtd_as->devfn,

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+                                  &vfio_domain_id);
+        if (!ret && domain_id == vfio_domain_id) {
+            IOMMUTLBEntry entry;
+
+            /* notify unmap */
+            if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP) {
Quoted 6 lines
Quoted 4 lines

First of all, if we are talking about VFIO, notifier_flag should always be MAP|UNMAP. So in that case, for newly mapped entries, looks like we will first send an UNMAP, then a MAP?

You are correct, there is no valid reason to have notifier_flag other

than

Quoted 2 lines

MAP|UNMAP
at least for VFIO.

Not sure whether you know about upstream vhost work, vhost is going to support Intel IOMMU, in that case, it will only register to UNMAP notifier, not MAP.

Quoted 1 line

I'm not sure if in the feature there won't be good reason to do

otherwise,

Quoted 2 lines

so my
code support this scenario...

My point is, I think you should not send this notify unconditionally. IMO the logic should be simpler here, like:

Quoted 13 lines
    foreach (page in invalidation range) {
        IOTLBEntry entry = m->iommu_ops.translate();
        if (entry.perm != IOMMU_NONE && notifier_flag & NOTIIFER_MAP) {
            /* this is map, user requested MAP flag */
            memory_region_iommu_notify(entry);
        } else if (entry.perm == IOMMU_NONE && notifier_flag &
                NOTIFIER_UNMAP) {
            /* this is unmap, user requested UNMAP */
            entry = ... /* setup the entry */
            memory_region_iommu_notify(entry);
        }
    }

This was my first algorithm, but VFIO do not support remapping of mapped page.
Before each MAP operation in VFIO one must do unmap, and therefore I'm sending
the unmap notifications blindly before.
I can rearrange my code closer to your suggestion.

Quoted 10 lines

This is to follow your logic. I don't know whether this is efficient enough, maybe good for the first version. The problem is, when you call translate(), you will need to go over the page every time from root dir. A faster way may be: provide a function to walk specific address range. If you are going to implement the replay logic that Alex/David has mentioned, maybe that will help too (walk over the whole 64bit range).

Interesting idea, but I prefer to add it in separate patch set after this

one committed, if it's OK.

Quoted 51 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+ VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask

%d",

Quoted 5 lines
Quoted 5 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                            addr, am);
+                entry.target_as = &address_space_memory;
+                entry.iova = addr & VTD_PAGE_MASK_4K;
+                entry.translated_addr = 0;
+                entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT_4K

+

Quoted 10 lines
Quoted 10 lines

am);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                entry.perm = IOMMU_NONE;
+                memory_region_notify_iommu(&node->vtd_as->iommu,

entry);

Quoted 6 lines
hw/i386/intel_iommu.c +6 -0
+            }
+
+            /* notify map */
+            if (node->notifier_flag & IOMMU_NOTIFIER_MAP) {
+                hwaddr original_addr = addr;
+                VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask

%d",

Quoted 9 lines
Quoted 9 lines

addr, am);

Quoted 1 line

+ while (addr < original_addr + (1 << am) *

VTD_PAGE_SIZE) {

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1129

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+                    /* call to vtd_iommu_translate */
+                    IOMMUTLBEntry entry = s->iommu_ops.translate(
+

&node->vtd_as->iommu,

Quoted 2 lines
(quoted diff) +2 -0
+                                                         addr,
+

IOMMU_NO_FAIL);

Quoted 3 lines
Quoted 3 lines

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+                    if (entry.perm != IOMMU_NONE) {
+                        addr += entry.addr_mask + 1;
+                        memory_region_notify_iommu(&

node->vtd_as->iommu,

Quoted 10 lines
Quoted 10 lines

entry);

hw/i386/intel_iommu.c static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) · line 1122

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+                    } else {
+                        addr += VTD_PAGE_SIZE;

IIUC, here is the point that we found "the page is gone" (so this is an UNMAP invalidation), and we should do memory_region_iommu_notify() for the whole area with IOMMU_NONE. Then we just quit the loop since continuous translate()s should fail as well if the first page is missing.

Quoted 6 lines
Quoted 2 lines

Please correct if I am wrong.

If I remember correctly I encounter a few cases where there was hole of unmaped
memory in the middle of otherwise mapped pages. If I remember correctly

Quoted 8 lines

it

Quoted 2 lines

was
with linux kernel 4.4, but I'm not sure.

I see. Thanks.

-- peterx

Best regards,
Aviv.

#16 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

On Mon, Oct 24, 2016 at 10:53:01AM +0300, Aviv B.D. wrote:

[...]

Quoted 6 lines

This was my first algorithm, but VFIO do not support remapping of mapped page.
Before each MAP operation in VFIO one must do unmap, and therefore I'm sending
the unmap notifications blindly before.
I can rearrange my code closer to your suggestion.

If so, I would suggest we solve the real problem first: we should not notify VFIO twice on map(), but only once. IMO either Alex's or David's suggestion (in the other mail) is a good start.

Quoted 11 lines
Quoted 11 lines

This is to follow your logic. I don't know whether this is efficient enough, maybe good for the first version. The problem is, when you call translate(), you will need to go over the page every time from root dir. A faster way may be: provide a function to walk specific address range. If you are going to implement the replay logic that Alex/David has mentioned, maybe that will help too (walk over the whole 64bit range).

Quoted 2 lines
Quoted 1 line

Interesting idea, but I prefer to add it in separate patch set after this

one committed, if it's OK.

Sure.

-- peterx

author
#17 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

Quoted 11 lines · Peter Xu

On Mon, Oct 24, 2016 at 10:53:01AM +0300, Aviv B.D. wrote:

[...]

Quoted 6 lines

This was my first algorithm, but VFIO do not support remapping of mapped page.
Before each MAP operation in VFIO one must do unmap, and therefore I'm sending
the unmap notifications blindly before.
I can rearrange my code closer to your suggestion.

Quoted 5 lines

If so, I would suggest we solve the real problem first: we should not notify VFIO twice on map(), but only once. IMO either Alex's or David's suggestion (in the other mail) is a good start.

OK. I will publish a new patch set that notify only once per page.

I prefer David's suggestion - adding the range information to the notifier struct
and check it from inside the VFIO notification function.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

This is to follow your logic. I don't know whether this is efficient enough, maybe good for the first version. The problem is, when you call translate(), you will need to go over the page every time from root dir. A faster way may be: provide a function to walk specific address range. If you are going to implement the replay logic that Alex/David has mentioned, maybe that will help too (walk over the whole 64bit range).

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Interesting idea, but I prefer to add it in separate patch set after

this

Quoted 1 line

one committed, if it's OK.

Sure.

-- peterx

Thanks,
Aviv.

#18 lore raw

Re: [Qemu-devel] [PATCH v4 RESEND 3/3] IOMMU: enable intel_iommu map and unmap notifiers

In reply to Aviv B.D

On Mon, Oct 17, 2016 at 06:44:24PM +0300, Aviv B.D wrote:

[...]

hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2074

Quoted 12 lines
hw/i386/intel_iommu.c +3 -1
@@ -2000,8 +2065,10 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
                                           IOMMUNotifierFlag new)
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
+    IntelIOMMUState *s = vtd_as->iommu_state;
+    IntelIOMMUNotifierNode *node = NULL;
 
-    if (new & IOMMU_NOTIFIER_MAP) {
+    if (!s->cache_mode_enabled && new & IOMMU_NOTIFIER_MAP) {
         error_report("Device at bus %s addr %02x.%d requires iommu "
                      "notifier which is currently not supported by "
                      "intel-iommu emulation",

Here after the patch works, we can modify the warning message into something like:

"We need to set cache_mode=1 for intel-iommu to enable device assignment with IOMMU protection."

Quoted 6 lines
hw/i386/intel_iommu.c +2 -0
@@ -2009,6 +2076,27 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu,
                      PCI_FUNC(vtd_as->devfn));
         exit(1);
     }
+
+    /* Add new ndoe if no mapping was exising before this call */

hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2090

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    if (old == IOMMU_NOTIFIER_NONE) {
+        node = g_malloc0(sizeof(*node));
+        node->vtd_as = vtd_as;
+        node->notifier_flag = new;
+        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
+        return;
+    }
+
+    /* update notifier node with new flags */
+    QLIST_FOREACH(node, &s->notifiers_list, next) {

Though in this case it is safe, I would still suggest we use QLIST_FOREACH_SAFE here.

hw/i386/intel_iommu.c static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, · line 2093

Quoted 3 lines
hw/i386/intel_iommu.c +3 -0
+        if (node->vtd_as == vtd_as) {
+            if (new == IOMMU_NOTIFIER_NONE) {
+                QLIST_REMOVE(node, next);

Memory leak here?

Thanks,

-- peterx

#19 lore raw
In reply to Aviv B.D
Quoted 6 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".

Quoted 7 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.

Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

Quoted 3 lines

* memory_region_iommu_replay hangs QEMU on start up while it itterate over 64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

This has been discussed previously, it would be incorrect for vfio not to call the replay function. The solution is to add an iommu driver callback to efficiently walk the mappings within a MemoryRegion. Thanks,

Alex

Quoted 34 lines

Changes from v1 to v2:
* remove assumption that the cache do not clears
* fix lockup on high load.

Changes from v2 to v3:
* remove debug leftovers
* split to sepearate commits
* change is_write to flags in vtd_do_iommu_translate, add IOMMU_NO_FAIL to suppress error propagating to guest.

Changes from v3 to v4:
* Add property to intel_iommu device to control the CM capability, default to False.
* Use s->iommu_ops.notify_flag_changed to register notifiers.

Changes from v4 to v4 RESEND:
* Fix codding style pointed by checkpatch.pl script.

Aviv Ben-David (3):
IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to
guest
IOMMU: change iommu_op->translate's is_write to flags, add support to
NO_FAIL flag mode
IOMMU: enable intel_iommu map and unmap notifiers

 exec.c                         |   3 +-
 hw/i386/amd_iommu.c            |   4 +-
 hw/i386/intel_iommu.c          | 175 +++++++++++++++++++++++++++++++++--------
 hw/i386/intel_iommu_internal.h |   3 +
 include/exec/memory.h          |   6 +-
 include/hw/i386/intel_iommu.h  |  11 +++
 memory.c                       |   3 +-
 7 files changed, 168 insertions(+), 37 deletions(-)
#20 lore raw
Quoted 19 lines · Alex Williamson
Quoted 6 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".

Quoted 7 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.

Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

Quoted 7 lines
Quoted 3 lines

* memory_region_iommu_replay hangs QEMU on start up while it itterate over 64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

This has been discussed previously, it would be incorrect for vfio not to call the replay function. The solution is to add an iommu driver callback to efficiently walk the mappings within a MemoryRegion.

Right, replay is a bit of a hack. There are a couple of other approaches that might be adequate without a new callback:
- Make the VFIOGuestIOMMU aware of the guest address range mapped by the vIOMMU. Intel currently advertises that as a full 64-bit address space, but I bet that's not actually true in practice.
- Have the IOMMU MR advertise a (minimum) page size for vIOMMU mappings. That may let you stpe through the range with greater strides

Quoted 40 lines

Thanks,

Alex

Quoted 34 lines

Changes from v1 to v2:
* remove assumption that the cache do not clears
* fix lockup on high load.

Changes from v2 to v3:
* remove debug leftovers
* split to sepearate commits
* change is_write to flags in vtd_do_iommu_translate, add IOMMU_NO_FAIL to suppress error propagating to guest.

Changes from v3 to v4:
* Add property to intel_iommu device to control the CM capability, default to False.
* Use s->iommu_ops.notify_flag_changed to register notifiers.

Changes from v4 to v4 RESEND:
* Fix codding style pointed by checkpatch.pl script.

Aviv Ben-David (3):
IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to
guest
IOMMU: change iommu_op->translate's is_write to flags, add support to
NO_FAIL flag mode
IOMMU: enable intel_iommu map and unmap notifiers

 exec.c                         |   3 +-
 hw/i386/amd_iommu.c            |   4 +-
 hw/i386/intel_iommu.c          | 175 +++++++++++++++++++++++++++++++++--------
 hw/i386/intel_iommu_internal.h |   3 +
 include/exec/memory.h          |   6 +-
 include/hw/i386/intel_iommu.h  |  11 +++
 memory.c                       |   3 +-
 7 files changed, 168 insertions(+), 37 deletions(-)
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#21 lore raw
Quoted 28 lines · On Tue, 18 Oct 2016 15:06:55 +1100 David Gibson <[email protected]>…
Quoted 19 lines · Alex Williamson
Quoted 6 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".

Quoted 7 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.

Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

Quoted 7 lines
Quoted 3 lines

* memory_region_iommu_replay hangs QEMU on start up while it itterate over 64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

This has been discussed previously, it would be incorrect for vfio not to call the replay function. The solution is to add an iommu driver callback to efficiently walk the mappings within a MemoryRegion.

Quoted 8 lines

Right, replay is a bit of a hack. There are a couple of other approaches that might be adequate without a new callback:
- Make the VFIOGuestIOMMU aware of the guest address range mapped by the vIOMMU. Intel currently advertises that as a full 64-bit address space, but I bet that's not actually true in practice.
- Have the IOMMU MR advertise a (minimum) page size for vIOMMU mappings. That may let you stpe through the range with greater strides

Hmm, VT-d supports at least a 39-bit address width and always supports a minimum 4k page size, so yes that does reduce us from 2^52 steps down to 2^27, but it's still absurd to walk through the raw address space. It does however seem correct to create the MemoryRegion with a width that actually matches the IOMMU capability, but I don't think that's a sufficient fix by itself. Thanks,

Alex

#22 lore raw
Quoted 40 lines · Alex Williamson
Quoted 28 lines · On Tue, 18 Oct 2016 15:06:55 +1100 David Gibson <[email protected]>…
Quoted 19 lines · Alex Williamson
Quoted 6 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".

Quoted 7 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.

Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

Quoted 7 lines
Quoted 3 lines

* memory_region_iommu_replay hangs QEMU on start up while it itterate over 64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

This has been discussed previously, it would be incorrect for vfio not to call the replay function. The solution is to add an iommu driver callback to efficiently walk the mappings within a MemoryRegion.

Quoted 8 lines

Right, replay is a bit of a hack. There are a couple of other approaches that might be adequate without a new callback:
- Make the VFIOGuestIOMMU aware of the guest address range mapped by the vIOMMU. Intel currently advertises that as a full 64-bit address space, but I bet that's not actually true in practice.
- Have the IOMMU MR advertise a (minimum) page size for vIOMMU mappings. That may let you stpe through the range with greater strides

Quoted 3 lines

Hmm, VT-d supports at least a 39-bit address width and always supports a minimum 4k page size, so yes that does reduce us from 2^52 steps down to 2^27,

Right, which is probably doable, if not ideal

Quoted 1 line

but it's still absurd to walk through the raw address space.

Well.. it depends on the internal structure of the IOMMU. For Power, it's traditionally just a 1-level page table, so we can't actually do any better than stepping through each IOMMU page.

Quoted 3 lines

It does however seem correct to create the MemoryRegion with a width that actually matches the IOMMU capability, but I don't think that's a sufficient fix by itself. Thanks,

I suspect it would actually make it workable in the short term.

But I don't disagree that a "traverse" or "replay" callback of some sort in the iommu_ops is a better idea long term. Having a fallback to the current replay implementation if the callback isn't supplied seems pretty reasonable though.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#23 lore raw
Quoted 45 lines · On Tue, 18 Oct 2016 16:52:04 +1100 David Gibson <[email protected]>…
Quoted 40 lines · Alex Williamson
Quoted 28 lines · On Tue, 18 Oct 2016 15:06:55 +1100 David Gibson <[email protected]>…
Quoted 19 lines · Alex Williamson
Quoted 6 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register.
This capability is controlled by "cache-mode" property of intel-iommu device. To enable this option call QEMU with "-device intel-iommu,cache-mode=true".

Quoted 7 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong to registered notifier, and notify accordingly.

Currently this patch still doesn't enabling VFIO devices support with vIOMMU present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to specific VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

Quoted 7 lines
Quoted 3 lines

* memory_region_iommu_replay hangs QEMU on start up while it itterate over 64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

This has been discussed previously, it would be incorrect for vfio not to call the replay function. The solution is to add an iommu driver callback to efficiently walk the mappings within a MemoryRegion.

Quoted 8 lines

Right, replay is a bit of a hack. There are a couple of other approaches that might be adequate without a new callback:
- Make the VFIOGuestIOMMU aware of the guest address range mapped by the vIOMMU. Intel currently advertises that as a full 64-bit address space, but I bet that's not actually true in practice.
- Have the IOMMU MR advertise a (minimum) page size for vIOMMU mappings. That may let you stpe through the range with greater strides

Quoted 3 lines

Hmm, VT-d supports at least a 39-bit address width and always supports a minimum 4k page size, so yes that does reduce us from 2^52 steps down to 2^27,

Quoted 7 lines

Right, which is probably doable, if not ideal

Quoted 1 line

but it's still absurd to walk through the raw address space.

Well.. it depends on the internal structure of the IOMMU. For Power, it's traditionally just a 1-level page table, so we can't actually do any better than stepping through each IOMMU page.

Intel always has a least a 3-level page table AIUI.

Quoted 10 lines
Quoted 3 lines

It does however seem correct to create the MemoryRegion with a width that actually matches the IOMMU capability, but I don't think that's a sufficient fix by itself. Thanks,

I suspect it would actually make it workable in the short term.

But I don't disagree that a "traverse" or "replay" callback of some sort in the iommu_ops is a better idea long term. Having a fallback to the current replay implementation if the callback isn't supplied seems pretty reasonable though.

Exactly, the callback could be optional where IOMMUs that supply a relatively small IOVA window could fallback to the code we have today. Thanks,

Alex

author
#24 lore raw
In reply to Alex Williamson

On Mon, Oct 17, 2016 at 7:07 PM, Alex Williamson <[email protected]

Quoted 17 lines

wrote:

Quoted 4 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register. This capability is controlled by "cache-mode" property of intel-iommu

device.

Quoted 1 line

To enable this option call QEMU with "-device

intel-iommu,cache-mode=true".

Quoted 2 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong

to

Quoted 2 lines

registered notifier, and notify accordingly.

Quoted 9 lines
Quoted 1 line

Currently this patch still doesn't enabling VFIO devices support with

vIOMMU

Quoted 2 lines

present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to

specific

Quoted 1 line

VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

In my setup the VFIO registered two memory areas with one page of unregistered memory
between them.

When I'm calling memory_region_notify_iommu it calls the notifier function of VFIO twice
when the second time is failing with warning to console as the new mapping is already present.

The notifier function of VFIO should ignore IOMMUTLBEntry that is not in the correct
range.

Quoted 11 lines
Quoted 2 lines

* memory_region_iommu_replay hangs QEMU on start up while it itterate

over

Quoted 2 lines

64bit address space. Commenting out the call to this function enables workable VFIO device while vIOMMU present.

This has been discussed previously, it would be incorrect for vfio not to call the replay function. The solution is to add an iommu driver callback to efficiently walk the mappings within a MemoryRegion. Thanks,

I'm aware about those discussion, I put this info here as helpful reminder if someone wants to
test my code.

Quoted 27 lines

Alex

Quoted 25 lines

Changes from v1 to v2:
* remove assumption that the cache do not clears
* fix lockup on high load.

Changes from v2 to v3:
* remove debug leftovers
* split to sepearate commits
* change is_write to flags in vtd_do_iommu_translate, add IOMMU_NO_FAIL to suppress error propagating to guest.

Changes from v3 to v4:
* Add property to intel_iommu device to control the CM capability, default to False.
* Use s->iommu_ops.notify_flag_changed to register notifiers.

Changes from v4 to v4 RESEND:
* Fix codding style pointed by checkpatch.pl script.

Aviv Ben-David (3):
IOMMU: add option to enable VTD_CAP_CM to vIOMMU capility exposoed to
guest
IOMMU: change iommu_op->translate's is_write to flags, add support to
NO_FAIL flag mode
IOMMU: enable intel_iommu map and unmap notifiers

Quoted 12 lines
Quoted 3 lines
 exec.c                         |   3 +-
 hw/i386/amd_iommu.c            |   4 +-
 hw/i386/intel_iommu.c          | 175 ++++++++++++++++++++++++++++++

+++--------

Quoted 6 lines
 hw/i386/intel_iommu_internal.h |   3 +
 include/exec/memory.h          |   6 +-
 include/hw/i386/intel_iommu.h  |  11 +++
 memory.c                       |   3 +-
 7 files changed, 168 insertions(+), 37 deletions(-)

Aviv.

#25 lore raw

[cc +david]

Quoted 32 lines · On Thu, 20 Oct 2016 22:17:18 +0300 "Aviv B.D." <[email protected]> wrote:

On Mon, Oct 17, 2016 at 7:07 PM, Alex Williamson <[email protected]

Quoted 17 lines

wrote:

Quoted 4 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register. This capability is controlled by "cache-mode" property of intel-iommu

device.

Quoted 1 line

To enable this option call QEMU with "-device

intel-iommu,cache-mode=true".

Quoted 2 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong

to

Quoted 2 lines

registered notifier, and notify accordingly.

Quoted 9 lines
Quoted 1 line

Currently this patch still doesn't enabling VFIO devices support with

vIOMMU

Quoted 2 lines

present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to

specific

Quoted 1 line

VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

In my setup the VFIO registered two memory areas with one page of unregistered memory
between them.

Quoted 8 lines

When I'm calling memory_region_notify_iommu it calls the notifier function of VFIO twice
when the second time is failing with warning to console as the new mapping is already present.

The notifier function of VFIO should ignore IOMMUTLBEntry that is not in the correct
range.

Hmm, right vfio_listener_region_add() is called for a MemoryRegionSection, but then we add an iommu notifier to the MemoryRegion, so we end up with a notifier per MemoryRegionSection regardless of whether they're backed by the same MemoryRegion. Seems like we need a MemoryRegion based list of VFIOGuestIOMMUs so we only register once per MemoryRegion and then sort though the list of VFIOGuestIOMMUs for a given MemoryRegion to find the one affected. David, does that sound right to you?

I am curious why you get two regions separated by one page, can you give an example of the ranges for each? Thanks,

Alex

#26 lore raw
Quoted 46 lines · Alex Williamson

[cc +david]

Quoted 32 lines · On Thu, 20 Oct 2016 22:17:18 +0300 "Aviv B.D." <[email protected]> wrote:

On Mon, Oct 17, 2016 at 7:07 PM, Alex Williamson <[email protected]

Quoted 17 lines

wrote:

Quoted 4 lines · On Mon, 17 Oct 2016 18:44:21 +0300 "Aviv B.D" <[email protected]> wrote:

From: "Aviv Ben-David" <[email protected]>

* Advertize Cache Mode capability in iommu cap register. This capability is controlled by "cache-mode" property of intel-iommu

device.

Quoted 1 line

To enable this option call QEMU with "-device

intel-iommu,cache-mode=true".

Quoted 2 lines

* On page cache invalidation in intel vIOMMU, check if the domain belong

to

Quoted 2 lines

registered notifier, and notify accordingly.

Quoted 9 lines
Quoted 1 line

Currently this patch still doesn't enabling VFIO devices support with

vIOMMU

Quoted 2 lines

present. Current problems:
* vfio_iommu_map_notify is not aware about memory range belong to

specific

Quoted 1 line

VFIOGuestIOMMU.

Could you elaborate on why this is an issue?

In my setup the VFIO registered two memory areas with one page of unregistered memory
between them.

Quoted 8 lines

When I'm calling memory_region_notify_iommu it calls the notifier function of VFIO twice
when the second time is failing with warning to console as the new mapping is already present.

The notifier function of VFIO should ignore IOMMUTLBEntry that is not in the correct
range.

Quoted 8 lines

Hmm, right vfio_listener_region_add() is called for a MemoryRegionSection, but then we add an iommu notifier to the MemoryRegion, so we end up with a notifier per MemoryRegionSection regardless of whether they're backed by the same MemoryRegion. Seems like we need a MemoryRegion based list of VFIOGuestIOMMUs so we only register once per MemoryRegion and then sort though the list of VFIOGuestIOMMUs for a given MemoryRegion to find the one affected. David, does that sound right to you?

Well, I think that would work. But I think it would be better to fix it from the other side:

We add the range to be notified into the IOMMUNotifier structure and filter based on that range in memory_region_notify_iommu.

It means a little more list searching and filtering on notify, but it avoids having to have another list and search on the VFIO side. I think it will also better deal with cases where part of an IOMMU mapped region is inaccessible due to an intermediate bridge.

Quoted 5 lines

I am curious why you get two regions separated by one page, can you give an example of the ranges for each? Thanks,

Alex

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#27 lore raw

On Fri, Oct 21, 2016 at 11:50:05AM +1100, David Gibson wrote:

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

In my setup the VFIO registered two memory areas with one page of unregistered memory
between them.

When I'm calling memory_region_notify_iommu it calls the notifier function of VFIO twice
when the second time is failing with warning to console as the new mapping is already present.

The notifier function of VFIO should ignore IOMMUTLBEntry that is not in the correct
range.

Quoted 8 lines
Quoted 8 lines

Hmm, right vfio_listener_region_add() is called for a MemoryRegionSection, but then we add an iommu notifier to the MemoryRegion, so we end up with a notifier per MemoryRegionSection regardless of whether they're backed by the same MemoryRegion. Seems like we need a MemoryRegion based list of VFIOGuestIOMMUs so we only register once per MemoryRegion and then sort though the list of VFIOGuestIOMMUs for a given MemoryRegion to find the one affected. David, does that sound right to you?

I think we already have such a list (VFIOContainer.giommu_list)? Can we use that to do it? When we try to add a new IOMMU notifier for specific MemoryRegion, we can first traverse VFIOContainer.giommu_list and see whether there are existing MemoryRegion registered, and we only register if it's the first one.

Quoted 11 lines

Well, I think that would work. But I think it would be better to fix it from the other side:

We add the range to be notified into the IOMMUNotifier structure and filter based on that range in memory_region_notify_iommu.

It means a little more list searching and filtering on notify, but it avoids having to have another list and search on the VFIO side. I think it will also better deal with cases where part of an IOMMU mapped region is inaccessible due to an intermediate bridge.

IIUC, this will still need to keep several VFIOGuestIOMMUs which contains exactly the same content?

Thanks,

-- peterx

#28 lore raw
Quoted 26 lines · On Fri, 21 Oct 2016 13:17:08 +0800 Peter Xu <[email protected]> wrote:

On Fri, Oct 21, 2016 at 11:50:05AM +1100, David Gibson wrote:

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

In my setup the VFIO registered two memory areas with one page of unregistered memory
between them.

When I'm calling memory_region_notify_iommu it calls the notifier function of VFIO twice
when the second time is failing with warning to console as the new mapping is already present.

The notifier function of VFIO should ignore IOMMUTLBEntry that is not in the correct
range.

Quoted 8 lines
Quoted 8 lines

Hmm, right vfio_listener_region_add() is called for a MemoryRegionSection, but then we add an iommu notifier to the MemoryRegion, so we end up with a notifier per MemoryRegionSection regardless of whether they're backed by the same MemoryRegion. Seems like we need a MemoryRegion based list of VFIOGuestIOMMUs so we only register once per MemoryRegion and then sort though the list of VFIOGuestIOMMUs for a given MemoryRegion to find the one affected. David, does that sound right to you?

Quoted 5 lines

I think we already have such a list (VFIOContainer.giommu_list)? Can we use that to do it? When we try to add a new IOMMU notifier for specific MemoryRegion, we can first traverse VFIOContainer.giommu_list and see whether there are existing MemoryRegion registered, and we only register if it's the first one.

That's effectively what I'm suggesting except I was trying to add some efficiency by tracking both the MemoryRegion and the MemoryRegionSection separately so we don't need to traverse a list to see if a MemoryRegion is already registered.

Quoted 7 lines
Quoted 7 lines

Well, I think that would work. But I think it would be better to fix it from the other side:

We add the range to be notified into the IOMMUNotifier structure and filter based on that range in memory_region_notify_iommu.

Quoted 7 lines
Quoted 4 lines

It means a little more list searching and filtering on notify, but it avoids having to have another list and search on the VFIO side. I think it will also better deal with cases where part of an IOMMU mapped region is inaccessible due to an intermediate bridge.

IIUC, this will still need to keep several VFIOGuestIOMMUs which contains exactly the same content?

The contain different offsets since this is based on the section rather than the MemoryRegion. Thanks,

Alex

#29 lore raw

On Fri, Oct 21, 2016 at 08:43:05AM -0600, Alex Williamson wrote:

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

Well, I think that would work. But I think it would be better to fix it from the other side:

We add the range to be notified into the IOMMUNotifier structure and filter based on that range in memory_region_notify_iommu.

It means a little more list searching and filtering on notify, but it avoids having to have another list and search on the VFIO side. I think it will also better deal with cases where part of an IOMMU mapped region is inaccessible due to an intermediate bridge.

Quoted 5 lines
Quoted 2 lines

IIUC, this will still need to keep several VFIOGuestIOMMUs which contains exactly the same content?

The contain different offsets since this is based on the section rather than the MemoryRegion. Thanks,

Though vfio_listener_region_add() is per memory region section, VFIOGuestIOMMU looks like to be for memory region? See in vfio_listener_region_add():

        giommu->iommu_offset = section->offset_within_address_space -
                               section->offset_within_region;

Here VFIOGuestIOMMU.iommu_offset should be the offset of memory region (rather than memory region section offset), right?

Thanks,

-- peterx

#30 lore raw
In reply to Peter Xu
Quoted 26 lines · Peter Xu

On Fri, Oct 21, 2016 at 11:50:05AM +1100, David Gibson wrote:

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

In my setup the VFIO registered two memory areas with one page of unregistered memory
between them.

When I'm calling memory_region_notify_iommu it calls the notifier function of VFIO twice
when the second time is failing with warning to console as the new mapping is already present.

The notifier function of VFIO should ignore IOMMUTLBEntry that is not in the correct
range.

Quoted 8 lines
Quoted 8 lines

Hmm, right vfio_listener_region_add() is called for a MemoryRegionSection, but then we add an iommu notifier to the MemoryRegion, so we end up with a notifier per MemoryRegionSection regardless of whether they're backed by the same MemoryRegion. Seems like we need a MemoryRegion based list of VFIOGuestIOMMUs so we only register once per MemoryRegion and then sort though the list of VFIOGuestIOMMUs for a given MemoryRegion to find the one affected. David, does that sound right to you?

Quoted 5 lines

I think we already have such a list (VFIOContainer.giommu_list)? Can we use that to do it? When we try to add a new IOMMU notifier for specific MemoryRegion, we can first traverse VFIOContainer.giommu_list and see whether there are existing MemoryRegion registered, and we only register if it's the first one.

Yes, I think that would work, but I still prefer the alternate approach I describe below.

Quoted 6 lines
Quoted 6 lines

Well, I think that would work. But I think it would be better to fix it from the other side:

We add the range to be notified into the IOMMUNotifier structure and filter based on that range in memory_region_notify_iommu.

Quoted 7 lines
Quoted 4 lines

It means a little more list searching and filtering on notify, but it avoids having to have another list and search on the VFIO side. I think it will also better deal with cases where part of an IOMMU mapped region is inaccessible due to an intermediate bridge.

IIUC, this will still need to keep several VFIOGuestIOMMUs which contains exactly the same content?

Well, it would no longer be exactly the same content since the different ranges would be stored within the IOMMUNotifier substructure. But, yes, it would be very similar.

I think it's still worth it for improved clarity, and the possibility to handle in future cases where part of the IOMMU region simply isn't accessible at all in the CPU address space.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson