Skip to contentSkip to conversation

hw/i386/amd_iommu: Cleanups and fixes

PATCH Superseded Sairaj Kodilkar 2 participants 13 messages Jul 24, 2025 – Jul 28, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/i386/amd_iommu: Cleanups and fixes

This series provides few cleanups and fixes for the amd iommu

Changes since v1:
- Dropped top two patches which depend on the Alejandro's changes and rebased remaining patches on top of v10.1.0-rc0 [Phil].
- Added a patch to fix amdvi_write*() [Ethon].
- Reset event log head and tail when guest writes to event log base register [Ethon].
- Considered "evtlog_intr" flag while generating event log interrupt [Ethon].
- Fixed comment [Ethon].

Base commit: 9e601684dc24a521bb1d23215a63e5c6e79ea0bb (v10.1.0-rc0)

Sairaj Kodilkar (6):
hw/i386/amd_iommu: Fix MMIO register write tracing hw/i386/amd_iommu: Remove unused and wrongly set ats_enabled field hw/i386/amd_iommu: Move IOAPIC memory region initialization to the end hw/i386/amd_iommu: Fix amdvi_write*()
hw/i386/amd_iommu: Support MMIO writes to the status register hw/i386/amd_iommu: Fix event log generation

 hw/i386/amd_iommu.c | 89 +++++++++++++++++++++++++++++++++------------
 hw/i386/amd_iommu.h |  2 +-
 2 files changed, 67 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/6 hw/i386/amd_iommu: Fix MMIO register write tracing

Define separate functions to trace MMIO write accesses instead of using trace_amdvi_mmio_read() for both read and write.

---
 hw/i386/amd_iommu.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
hw/i386/amd_iommu.c +18 -5
index 5a24c17548d4..7fb0bb68f008 100644
@@ -592,18 +592,31 @@ static void amdvi_cmdbuf_run(AMDVIState *s)
     }
 }
 
-static void amdvi_mmio_trace(hwaddr addr, unsigned size)
+static inline uint8_t amdvi_mmio_get_index(hwaddr addr)
 {
     uint8_t index = (addr & ~0x2000) / 8;
 
     if ((addr & 0x2000)) {
         /* high table */
         index = index >= AMDVI_MMIO_REGS_HIGH ? AMDVI_MMIO_REGS_HIGH : index;
-        trace_amdvi_mmio_read(amdvi_mmio_high[index], addr, size, addr & ~0x07);
     } else {
         index = index >= AMDVI_MMIO_REGS_LOW ? AMDVI_MMIO_REGS_LOW : index;
-        trace_amdvi_mmio_read(amdvi_mmio_low[index], addr, size, addr & ~0x07);
     }
+
+    return index;
+}
+
+static void amdvi_mmio_trace_read(hwaddr addr, unsigned size)
+{
+    uint8_t index = amdvi_mmio_get_index(addr);
+    trace_amdvi_mmio_read(amdvi_mmio_low[index], addr, size, addr & ~0x07);
+}
+
+static void amdvi_mmio_trace_write(hwaddr addr, unsigned size, uint64_t val)
+{
+    uint8_t index = amdvi_mmio_get_index(addr);
+    trace_amdvi_mmio_write(amdvi_mmio_low[index], addr, size, val,
+                           addr & ~0x07);
 }
 
 static uint64_t amdvi_mmio_read(void *opaque, hwaddr addr, unsigned size)
@@ -623,7 +636,7 @@ static uint64_t amdvi_mmio_read(void *opaque, hwaddr addr, unsigned size)
     } else if (size == 8) {
         val = amdvi_readq(s, addr);
     }
-    amdvi_mmio_trace(addr, size);
+    amdvi_mmio_trace_read(addr, size);
 
     return val;
 }
@@ -770,7 +783,7 @@ static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val,
         return;
     }
 
-    amdvi_mmio_trace(addr, size);
+    amdvi_mmio_trace_write(addr, size, val);
     switch (addr & ~0x07) {
     case AMDVI_MMIO_CONTROL:
         amdvi_mmio_reg_write(s, size, val, addr);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/6 hw/i386/amd_iommu: Remove unused and wrongly set ats_enabled field

In reply to Sairaj Kodilkar

The ats_enabled field is set using HTTUNEN, which is wrong. Fix this by removing the field as it is never used.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")
---
 hw/i386/amd_iommu.c | 3 ---
 hw/i386/amd_iommu.h | 1 -
 2 files changed, 4 deletions(-)
hw/i386/amd_iommu.c +0 -3
index 7fb0bb68f008..39bb04fd9b18 100644
@@ -646,7 +646,6 @@ static void amdvi_handle_control_write(AMDVIState *s)
     unsigned long control = amdvi_readq(s, AMDVI_MMIO_CONTROL);
     s->enabled = !!(control & AMDVI_MMIO_CONTROL_AMDVIEN);
 
-    s->ats_enabled = !!(control & AMDVI_MMIO_CONTROL_HTTUNEN);
     s->evtlog_enabled = s->enabled && !!(control &
                         AMDVI_MMIO_CONTROL_EVENTLOGEN);
 
@@ -1555,7 +1554,6 @@ static void amdvi_init(AMDVIState *s)
     s->excl_allow = false;
     s->mmio_enabled = false;
     s->enabled = false;
-    s->ats_enabled = false;
     s->cmdbuf_enabled = false;
 
     /* reset MMIO */
@@ -1626,7 +1624,6 @@ static const VMStateDescription vmstate_amdvi_sysbus_migratable = {
       /* Updated in  amdvi_handle_control_write() */
       VMSTATE_BOOL(enabled, AMDVIState),
       VMSTATE_BOOL(ga_enabled, AMDVIState),
-      VMSTATE_BOOL(ats_enabled, AMDVIState),
       VMSTATE_BOOL(cmdbuf_enabled, AMDVIState),
       VMSTATE_BOOL(completion_wait_intr, AMDVIState),
       VMSTATE_BOOL(evtlog_enabled, AMDVIState),
hw/i386/amd_iommu.h +0 -1
index 8b42913ed8da..67078c6f1e22 100644
@@ -322,7 +322,6 @@ struct AMDVIState {
     uint64_t mmio_addr;
 
     bool enabled;                /* IOMMU enabled                */
-    bool ats_enabled;            /* address translation enabled  */
     bool cmdbuf_enabled;         /* command buffer enabled       */
     bool evtlog_enabled;         /* event log enabled            */
     bool excl_enabled;
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v2 2/6] hw/i386/amd_iommu: Remove unused and wrongly set ats_enabled field

Hi,

Quoted 14 lines · Sairaj Kodilkar

The ats_enabled field is set using HTTUNEN, which is wrong. Fix this by removing the field as it is never used.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")
---
  hw/i386/amd_iommu.c | 3 ---
  hw/i386/amd_iommu.h | 1 -
  2 files changed, 4 deletions(-)

hw/i386/amd_iommu.c static const VMStateDescription vmstate_amdvi_sysbus_migratable = { · line 1629

Quoted 4 lines
hw/i386/amd_iommu.c +0 -1
@@ -1626,7 +1624,6 @@ static const VMStateDescription vmstate_amdvi_sysbus_migratable = {
        /* Updated in  amdvi_handle_control_write() */
        VMSTATE_BOOL(enabled, AMDVIState),
        VMSTATE_BOOL(ga_enabled, AMDVIState),
-      VMSTATE_BOOL(ats_enabled, AMDVIState),

Please have a look at the migration documentation:
https://www.qemu.org/docs/master/devel/migration/main.html#not-sending-existing-elements

Quoted 15 lines
hw/i386/amd_iommu.c +0 -0
        VMSTATE_BOOL(cmdbuf_enabled, AMDVIState),
        VMSTATE_BOOL(completion_wait_intr, AMDVIState),
        VMSTATE_BOOL(evtlog_enabled, AMDVIState),
hw/i386/amd_iommu.h +0 -1
index 8b42913ed8da..67078c6f1e22 100644
@@ -322,7 +322,6 @@ struct AMDVIState {
      uint64_t mmio_addr;
 
      bool enabled;                /* IOMMU enabled                */
-    bool ats_enabled;            /* address translation enabled  */
      bool cmdbuf_enabled;         /* command buffer enabled       */
      bool evtlog_enabled;         /* event log enabled            */
      bool excl_enabled;
#4 lore raw

Re: [PATCH v2 2/6] hw/i386/amd_iommu: Remove unused and wrongly set ats_enabled field

Quoted 16 lines · Philippe Mathieu-Daudé

Hi,

Quoted 11 lines · Sairaj Kodilkar

The ats_enabled field is set using HTTUNEN, which is wrong. Fix this by removing the field as it is never used.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")

---
  hw/i386/amd_iommu.c | 3 ---
  hw/i386/amd_iommu.h | 1 -

  2 files changed, 4 deletions(-)
Quoted 10 lines

hw/i386/amd_iommu.c static const VMStateDescription · line 1630

Quoted 6 lines
hw/i386/amd_iommu.c +0 -1
@@ -1626,7 +1624,6 @@ static const VMStateDescription 
 vmstate_amdvi_sysbus_migratable = {
         /* Updated in  amdvi_handle_control_write() */
         VMSTATE_BOOL(enabled, AMDVIState),
         VMSTATE_BOOL(ga_enabled, AMDVIState),
-      VMSTATE_BOOL(ats_enabled, AMDVIState),

Please have a look at the migration documentation:
https://www.qemu.org/docs/master/devel/migration/main.html#not-sending- existing-elements

Thanks for sharing. I'll need to add a VMSTATE_UNUSED() here

#5 lore raw

PATCH v2 3/6 hw/i386/amd_iommu: Move IOAPIC memory region initialization to the end

In reply to Sairaj Kodilkar

Setting up IOAPIC memory region requires mr_sys and mr_ir. Currently these two memory regions are setup after the initializing the IOAPIC memory region, which cause amdvi_host_dma_iommu() to use unitialized mr_sys and mr_ir.

Move the IOAPIC memory region initialization to the end in order to use the mr_sys and mr_ir regions after they are fully initialized.

Fixes 577c470f4326 ("x86_iommu/amd: Prepare for interrupt remap support")
---
 hw/i386/amd_iommu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/i386/amd_iommu.c +3 -3
index 39bb04fd9b18..2183510d227c 100644
@@ -1696,9 +1696,6 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
     s->iotlb = g_hash_table_new_full(amdvi_uint64_hash,
                                      amdvi_uint64_equal, g_free, g_free);
 
-    /* Pseudo address space under root PCI bus. */
-    x86ms->ioapic_as = amdvi_host_dma_iommu(bus, s, AMDVI_IOAPIC_SB_DEVID);
-
     /* set up MMIO */
     memory_region_init_io(&s->mr_mmio, OBJECT(s), &mmio_mem_ops, s,
                           "amdvi-mmio", AMDVI_MMIO_SIZE);
@@ -1721,6 +1718,9 @@ static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
     memory_region_add_subregion_overlap(&s->mr_sys, AMDVI_INT_ADDR_FIRST,
                                         &s->mr_ir, 1);
 
+    /* Pseudo address space under root PCI bus. */
+    x86ms->ioapic_as = amdvi_host_dma_iommu(bus, s, AMDVI_IOAPIC_SB_DEVID);
+
     if (kvm_enabled() && x86ms->apic_id_limit > 255 && !s->xtsup) {
         error_report("AMD IOMMU with x2APIC configuration requires xtsup=on");
         exit(EXIT_FAILURE);
Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 4/6 hw/i386/amd_iommu: Fix amdvi_write*()

In reply to Sairaj Kodilkar

amdvi_write*() function do not preserve the older values of W1C bits in the MMIO register. This results in all W1C bits set to 0, when guest tries to reset a single bit by writing 1 to it. Fix this by preserving W1C bits in the old value of the MMIO register.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")
---
 hw/i386/amd_iommu.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
hw/i386/amd_iommu.c +6 -3
index 2183510d227c..bbffd07b4e48 100644
@@ -124,7 +124,8 @@ static void amdvi_writew(AMDVIState *s, hwaddr addr, uint16_t val)
     uint16_t w1cmask = lduw_le_p(&s->w1cmask[addr]);
     uint16_t oldval = lduw_le_p(&s->mmior[addr]);
     stw_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
 }
 
 static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
@@ -133,7 +134,8 @@ static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
     uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
     uint32_t oldval = ldl_le_p(&s->mmior[addr]);
     stl_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
 }
 
 static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
@@ -142,7 +144,8 @@ static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
     uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
     uint64_t oldval = ldq_le_p(&s->mmior[addr]);
     stq_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
 }
 
 /* OR a 64-bit register with a 64-bit value */
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 4/6] hw/i386/amd_iommu: Fix amdvi_write*()

Quoted 17 lines · Sairaj Kodilkar

amdvi_write*() function do not preserve the older values of W1C bits in the MMIO register. This results in all W1C bits set to 0, when guest tries to reset a single bit by writing 1 to it. Fix this by preserving W1C bits in the old value of the MMIO register.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")
---
  hw/i386/amd_iommu.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
hw/i386/amd_iommu.c +0 -0
index 2183510d227c..bbffd07b4e48 100644

hw/i386/amd_iommu.c static void amdvi_writew(AMDVIState *s, hwaddr addr, uint16_t val) · line 128

Quoted 6 lines
hw/i386/amd_iommu.c +2 -1
@@ -124,7 +124,8 @@ static void amdvi_writew(AMDVIState *s, hwaddr addr, uint16_t val)
      uint16_t w1cmask = lduw_le_p(&s->w1cmask[addr]);
      uint16_t oldval = lduw_le_p(&s->mmior[addr]);
      stw_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));

High code complexity, hard to review. Do you mind introducing intermediate well-named variables for clarity?

Quoted 23 lines
hw/i386/amd_iommu.c +4 -2
  }
 
  static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
@@ -133,7 +134,8 @@ static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
      uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
      uint32_t oldval = ldl_le_p(&s->mmior[addr]);
      stl_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
  }
 
  static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
@@ -142,7 +144,8 @@ static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
      uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
      uint64_t oldval = ldq_le_p(&s->mmior[addr]);
      stq_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
  }
 
  /* OR a 64-bit register with a 64-bit value */
#8 lore raw

Re: [PATCH v2 4/6] hw/i386/amd_iommu: Fix amdvi_write*()

Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Sairaj Kodilkar

amdvi_write*() function do not preserve the older values of W1C bits in the MMIO register. This results in all W1C bits set to 0, when guest tries to reset a single bit by writing 1 to it. Fix this by preserving W1C bits in the old value of the MMIO register.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")

---
  hw/i386/amd_iommu.c | 9 ++++++---

  1 file changed, 6 insertions(+), 3 deletions(-)
Quoted 16 lines

hw/i386/amd_iommu.c static void amdvi_writew(AMDVIState *s, hwaddr · line 129

Quoted 12 lines
hw/i386/amd_iommu.c +2 -1
index 2183510d227c..bbffd07b4e48 100644
@@ -124,7 +124,8 @@ static void amdvi_writew(AMDVIState *s, hwaddr 
 addr, uint16_t val)
       uint16_t w1cmask = lduw_le_p(&s->w1cmask[addr]);
       uint16_t oldval = lduw_le_p(&s->mmior[addr]);
       stw_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));

High code complexity, hard to review. Do you mind introducing intermediate well-named variables for clarity?

Sure !

Quoted 23 lines
Quoted 22 lines
hw/i386/amd_iommu.c +4 -2
   }
   static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
@@ -133,7 +134,8 @@ static void amdvi_writel(AMDVIState *s, hwaddr 
 addr, uint32_t val)
       uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
       uint32_t oldval = ldl_le_p(&s->mmior[addr]);
       stl_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
   }
   static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
@@ -142,7 +144,8 @@ static void amdvi_writeq(AMDVIState *s, hwaddr 
 addr, uint64_t val)
       uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
       uint64_t oldval = ldq_le_p(&s->mmior[addr]);
       stq_le_p(&s->mmior[addr],
-            ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
+            ((oldval & (romask | w1cmask)) |
+             (val & ~romask)) & ~(val & w1cmask));
   }
   /* OR a 64-bit register with a 64-bit value */
#9 lore raw

PATCH v2 5/6 hw/i386/amd_iommu: Support MMIO writes to the status register

In reply to Sairaj Kodilkar

Support the writes to the status register so that guest can reset the EventOverflow, EventLogInt, ComWaitIntr, etc bits after servicing the respective interrupt.

---
 hw/i386/amd_iommu.c | 3 +++
 1 file changed, 3 insertions(+)
hw/i386/amd_iommu.c +3 -0
index bbffd07b4e48..7c2fa80d14ff 100644
@@ -850,6 +850,9 @@ static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val,
         amdvi_mmio_reg_write(s, size, val, addr);
         amdvi_handle_pprtail_write(s);
         break;
+    case AMDVI_MMIO_STATUS:
+        amdvi_mmio_reg_write(s, size, val, addr);
+        break;
     }
 }
 
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH v2 5/6] hw/i386/amd_iommu: Support MMIO writes to the status register

Quoted 15 lines · Sairaj Kodilkar

Support the writes to the status register so that guest can reset the EventOverflow, EventLogInt, ComWaitIntr, etc bits after servicing the respective interrupt.

---
  hw/i386/amd_iommu.c | 3 +++
  1 file changed, 3 insertions(+)
hw/i386/amd_iommu.c +0 -0
index bbffd07b4e48..7c2fa80d14ff 100644

hw/i386/amd_iommu.c static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val, · line 855

Quoted 6 lines
hw/i386/amd_iommu.c +3 -0
@@ -850,6 +850,9 @@ static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val,
          amdvi_mmio_reg_write(s, size, val, addr);
          amdvi_handle_pprtail_write(s);
          break;
+    case AMDVI_MMIO_STATUS:
+        amdvi_mmio_reg_write(s, size, val, addr);
+        break;

Should we add:

        default:
            g_assert_not_reached();

to catch unimplemented cases?

Quoted 3 lines
hw/i386/amd_iommu.c +0 -0
      }
  }
 
#11 lore raw

Re: [PATCH v2 5/6] hw/i386/amd_iommu: Support MMIO writes to the status register

Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 15 lines · Sairaj Kodilkar

Support the writes to the status register so that guest can reset the EventOverflow, EventLogInt, ComWaitIntr, etc bits after servicing the respective interrupt.

---
  hw/i386/amd_iommu.c | 3 +++

  1 file changed, 3 insertions(+)
hw/i386/amd_iommu.c +0 -0
index bbffd07b4e48..7c2fa80d14ff 100644

hw/i386/amd_iommu.c static void amdvi_mmio_write(void *opaque, hwaddr · line 856

Quoted 7 lines
hw/i386/amd_iommu.c +3 -0
@@ -850,6 +850,9 @@ static void amdvi_mmio_write(void *opaque, hwaddr 
 addr, uint64_t val,
           amdvi_mmio_reg_write(s, size, val, addr);
           amdvi_handle_pprtail_write(s);
           break;
+    case AMDVI_MMIO_STATUS:
+        amdvi_mmio_reg_write(s, size, val, addr);
+        break;

Should we add:

Quoted 2 lines

        default:
            g_assert_not_reached();

I am against adding a failure here. Because when xtsup=on, linux tries to write all the XT registers ( which we dont support yet, because of which event logging does not work in this mode). Although future cleanup patches will add this support, I dont want to break things now.

I think amdvi_mmio_trace_write() is sufficient for debugging purpose, which prints "UNHANDLED" for unsupported registers.

Thanks
Sairaj

Quoted 6 lines

to catch unimplemented cases?

Quoted 2 lines
hw/i386/amd_iommu.c +0 -0
       }
   }
#12 lore raw

PATCH v2 6/6 hw/i386/amd_iommu: Fix event log generation

In reply to Sairaj Kodilkar

Current event logging code is broken, because of following issues

1. The code uses '|' instead of '&' to test the bit field, which causes vIOMMU to generate overflow interrupt for every log entry.
2. Code does not update the eventlog tail MMIO register after adding an entry to the buffer, because of which guest cannot process new entries (as head == tail means buffer is empty).
3. Compares eventlog tail (which is byte offset in the buffer) to eventlog length (which is number of maximum entries in the buffer). This causes vIOMMU to generate only fix number of event logs, after which it keeps on generating overflow interrupts, without actually resetting the log buffer.
4. Updates ComWaitInt instead of EventLogInt bitfield in Status register. Guest checks this field to see if there are new event log entries in the buffer.
5. Does not reset event log head and tail pointers when guest writes to eventlog base register.

Fix above issues, so that guest can process event log entries.

Fixes d29a09ca68428 ("hw/i386: Introduce AMD IOMMU")
---
 hw/i386/amd_iommu.c | 44 +++++++++++++++++++++++++++++++++++---------
 hw/i386/amd_iommu.h |  1 +
 2 files changed, 36 insertions(+), 9 deletions(-)
hw/i386/amd_iommu.c +35 -9
index 7c2fa80d14ff..df564bc90fa9 100644
@@ -148,10 +148,10 @@ static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
              (val & ~romask)) & ~(val & w1cmask));
 }
 
-/* OR a 64-bit register with a 64-bit value */
+/* AND a 64-bit register with a 64-bit value */
 static bool amdvi_test_mask(AMDVIState *s, hwaddr addr, uint64_t val)
 {
-    return amdvi_readq(s, addr) | val;
+    return amdvi_readq(s, addr) & val;
 }
 
 /* OR a 64-bit register with a 64-bit value storing result in the register */
@@ -180,19 +180,31 @@ static void amdvi_generate_msi_interrupt(AMDVIState *s)
     }
 }
 
+static uint32_t get_next_eventlog_entry(AMDVIState *s)
+{
+    uint32_t evtlog_size = s->evtlog_len * AMDVI_EVENT_LEN;
+    return (s->evtlog_tail + AMDVI_EVENT_LEN) % evtlog_size;
+}
+
 static void amdvi_log_event(AMDVIState *s, uint64_t *evt)
 {
+    uint32_t evtlog_tail_next;
+
     /* event logging not enabled */
     if (!s->evtlog_enabled || amdvi_test_mask(s, AMDVI_MMIO_STATUS,
         AMDVI_MMIO_STATUS_EVT_OVF)) {
         return;
     }
 
+    evtlog_tail_next = get_next_eventlog_entry(s);
+
     /* event log buffer full */
-    if (s->evtlog_tail >= s->evtlog_len) {
-        amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_OVF);
-        /* generate interrupt */
-        amdvi_generate_msi_interrupt(s);
+    if (evtlog_tail_next == s->evtlog_head) {
+        /* generate overflow interrupt */
+        if (s->evtlog_intr) {
+            amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_OVF);
+            amdvi_generate_msi_interrupt(s);
+        }
         return;
     }
 
@@ -201,9 +213,13 @@ static void amdvi_log_event(AMDVIState *s, uint64_t *evt)
         trace_amdvi_evntlog_fail(s->evtlog, s->evtlog_tail);
     }
 
-    s->evtlog_tail += AMDVI_EVENT_LEN;
-    amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_COMP_INT);
-    amdvi_generate_msi_interrupt(s);
+    s->evtlog_tail = evtlog_tail_next;
+    amdvi_writeq_raw(s, AMDVI_MMIO_EVENT_TAIL, s->evtlog_tail);
+
+    if (s->evtlog_intr) {
+        amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVENT_INT);
+        amdvi_generate_msi_interrupt(s);
+    }
 }
 
 static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int start,
@@ -719,9 +735,19 @@ static inline void amdvi_handle_excllim_write(AMDVIState *s)
 static inline void amdvi_handle_evtbase_write(AMDVIState *s)
 {
     uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_BASE);
+
+    if (amdvi_readq(s, AMDVI_MMIO_STATUS) & AMDVI_MMIO_STATUS_EVENT_INT)
+        /* Do not reset if eventlog interrupt bit is set*/
+        return;
+
     s->evtlog = val & AMDVI_MMIO_EVTLOG_BASE_MASK;
     s->evtlog_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_EVTLOG_SIZE_BYTE)
                     & AMDVI_MMIO_EVTLOG_SIZE_MASK);
+
+    /* clear tail and head pointer to 0 when event base is updated */
+    s->evtlog_tail = s->evtlog_head = 0;
+    amdvi_writeq_raw(s, AMDVI_MMIO_EVENT_HEAD, s->evtlog_head);
+    amdvi_writeq_raw(s, AMDVI_MMIO_EVENT_TAIL, s->evtlog_tail);
 }
 
 static inline void amdvi_handle_evttail_write(AMDVIState *s)
hw/i386/amd_iommu.h +1 -0
index 67078c6f1e22..2476296c4902 100644
@@ -111,6 +111,7 @@
 #define AMDVI_MMIO_STATUS_CMDBUF_RUN  (1 << 4)
 #define AMDVI_MMIO_STATUS_EVT_RUN     (1 << 3)
 #define AMDVI_MMIO_STATUS_COMP_INT    (1 << 2)
+#define AMDVI_MMIO_STATUS_EVENT_INT   (1 << 1)
 #define AMDVI_MMIO_STATUS_EVT_OVF     (1 << 0)
 
 #define AMDVI_CMDBUF_ID_BYTE              0x07
Signature · 1 line
2.34.1