Skip to contentSkip to conversation

s390x/pci: relax I/O address translation requirement

PATCH Superseded Matthew Rosato 4 participants 10 messages Jan 24, 2025 – Jan 28, 2025

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover s390x/pci: relax I/O address translation requirement

This series introduces the concept of the relaxed translation requirement for s390x guests in order to allow bypass of the guest IOMMU for more efficient PCI passthrough.

With this series, QEMU can indicate to the guest that an IOMMU is not strictly required for a zPCI device. This would subsequently allow a guest linux to use iommu.passthrough=1 and bypass their guest IOMMU for PCI devices.

When this occurs, QEMU will note the behavior via an intercepted MPCIFC instruction and will fill the host iommu with mappings of the entire guest address space in response.

There is a kernel series that adds the relevant behavior needed to exploit this new feature from within a s390x linux guest. Most recent version of that is at [1].

[1]: https://lore.kernel.org/linux-s390/[email protected]/

Changes for v3:
- use s390_get_memory_limit
- advertise full aperture for relaxed-translation-capable devices

Changes for v2:
- Add relax-translation property, fence for older machines
- Add a new MPCIFC failure case when direct-mapping requested but the relax-translation property is set to off.
- For direct mapping, use a memory alias to handle the SMDA offset and then just let vfio handle the pinning of memory.

Matthew Rosato (2):
s390x/pci: add support for guests that request direct mapping s390x/pci: indicate QEMU supports relaxed translation for passthrough

 hw/s390x/s390-pci-bus.c         | 36 +++++++++++++++++++++++++++++++--
 hw/s390x/s390-pci-inst.c        | 13 ++++++++++--
 hw/s390x/s390-pci-vfio.c        | 20 +++++++++++++-----
 hw/s390x/s390-virtio-ccw.c      |  5 +++++
 include/hw/s390x/s390-pci-bus.h |  4 ++++
 include/hw/s390x/s390-pci-clp.h |  1 +
 6 files changed, 70 insertions(+), 9 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/2 s390x/pci: add support for guests that request direct mapping

When receiving a guest mpcifc(4) or mpcifc(6) instruction without the T bit set, treat this as a request to perform direct mapping instead of address translation. In order to facilitate this, pin the entirety of guest memory into the host iommu.

Pinning for the direct mapping case is handled via vfio and its memory listener. Additionally, ram discard settings are inherited from vfio:
coordinated discards (e.g. virtio-mem) are allowed while uncoordinated discards (e.g. virtio-balloon) are disabled.

Subsequent guest DMA operations are all expected to be of the format guest_phys+sdma, allowing them to be used as lookup into the host iommu table.

---
 hw/s390x/s390-pci-bus.c         | 36 +++++++++++++++++++++++++++++++--
 hw/s390x/s390-pci-inst.c        | 13 ++++++++++--
 hw/s390x/s390-pci-vfio.c        | 15 ++++++++++----
 hw/s390x/s390-virtio-ccw.c      |  5 +++++
 include/hw/s390x/s390-pci-bus.h |  4 ++++
 5 files changed, 65 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +34 -2
index eead269cc2..8cf5aec1a2 100644
@@ -18,6 +18,8 @@
 #include "hw/s390x/s390-pci-inst.h"
 #include "hw/s390x/s390-pci-kvm.h"
 #include "hw/s390x/s390-pci-vfio.h"
+#include "hw/s390x/s390-virtio-ccw.h"
+#include "hw/boards.h"
 #include "hw/pci/pci_bus.h"
 #include "hw/qdev-properties.h"
 #include "hw/pci/pci_bridge.h"
@@ -720,16 +722,44 @@ void s390_pci_iommu_enable(S390PCIIOMMU *iommu)
                              TYPE_S390_IOMMU_MEMORY_REGION, OBJECT(&iommu->mr),
                              name, iommu->pal + 1);
     iommu->enabled = true;
+    iommu->direct_map = false;
     memory_region_add_subregion(&iommu->mr, 0, MEMORY_REGION(&iommu->iommu_mr));
     g_free(name);
 }
 
+void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu)
+{
+    MachineState *ms = MACHINE(qdev_get_machine());
+    S390CcwMachineState *s390ms = S390_CCW_MACHINE(ms);
+
+    /*
+     * For direct-mapping we must map the entire guest address space.  Rather
+     * than using an iommu, create a memory region alias that maps GPA X to
+     * iova X + SDMA.  VFIO will handle pinning via its memory listener.
+     */
+    g_autofree char *name = g_strdup_printf("iommu-dm-s390-%04x",
+                                            iommu->pbdev->uid);
+    memory_region_init_alias(&iommu->dm_mr, OBJECT(&iommu->mr), name, ms->ram,
+                             0, s390_get_memory_limit(s390ms));
+    iommu->enabled = true;
+    iommu->direct_map = true;
+    memory_region_add_subregion(&iommu->mr, iommu->pbdev->zpci_fn.sdma,
+                                &iommu->dm_mr);
+}
+
 void s390_pci_iommu_disable(S390PCIIOMMU *iommu)
 {
     iommu->enabled = false;
     g_hash_table_remove_all(iommu->iotlb);
-    memory_region_del_subregion(&iommu->mr, MEMORY_REGION(&iommu->iommu_mr));
-    object_unparent(OBJECT(&iommu->iommu_mr));
+    if (iommu->direct_map) {
+        memory_region_del_subregion(&iommu->mr, &iommu->dm_mr);
+        iommu->direct_map = false;
+        object_unparent(OBJECT(&iommu->dm_mr));
+    } else {
+        memory_region_del_subregion(&iommu->mr,
+                                    MEMORY_REGION(&iommu->iommu_mr));
+        object_unparent(OBJECT(&iommu->iommu_mr));
+    }
 }
 
 static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)
@@ -1488,6 +1518,8 @@ static const Property s390_pci_device_properties[] = {
     DEFINE_PROP_BOOL("interpret", S390PCIBusDevice, interp, true),
     DEFINE_PROP_BOOL("forwarding-assist", S390PCIBusDevice, forwarding_assist,
                      true),
+    DEFINE_PROP_BOOL("relaxed-translation", S390PCIBusDevice, rtr_allowed,
+                     true),
 };
 
 static const VMStateDescription s390_pci_device_vmstate = {
hw/s390x/s390-pci-inst.c +11 -2
index e386d75d58..4c108fa8c4 100644
@@ -16,6 +16,7 @@
 #include "exec/memory.h"
 #include "qemu/error-report.h"
 #include "system/hw_accel.h"
+#include "hw/boards.h"
 #include "hw/pci/pci_device.h"
 #include "hw/s390x/s390-pci-inst.h"
 #include "hw/s390x/s390-pci-bus.h"
@@ -1008,17 +1009,25 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
     }
 
     /* currently we only support designation type 1 with translation */
-    if (!(dt == ZPCI_IOTA_RTTO && t)) {
+    if (t && !(dt == ZPCI_IOTA_RTTO)) {
         error_report("unsupported ioat dt %d t %d", dt, t);
         s390_program_interrupt(env, PGM_OPERAND, ra);
         return -EINVAL;
+    } else if (!t && !pbdev->rtr_allowed) {
+        error_report("relaxed translation not allowed");
+        s390_program_interrupt(env, PGM_OPERAND, ra);
+        return -EINVAL;
     }
 
     iommu->pba = pba;
     iommu->pal = pal;
     iommu->g_iota = g_iota;
 
-    s390_pci_iommu_enable(iommu);
+    if (t) {
+        s390_pci_iommu_enable(iommu);
+    } else {
+        s390_pci_iommu_dm_enable(iommu);
+    }
 
     return 0;
 }
hw/s390x/s390-pci-vfio.c +11 -4
index 7dbbc76823..dad525c81c 100644
@@ -133,11 +133,18 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
 
     /*
      * If appropriate, reduce the size of the supported DMA aperture reported
-     * to the guest based upon the vfio DMA limit.
+     * to the guest based upon the vfio DMA limit.  This is applicable for
+     * devices that are guaranteed to not use relaxed translation.  If the
+     * device is capable of relaxed translation then we must advertise the
+     * full aperture.  In this case, if translation is used then we will
+     * rely on the vfio DMA limit counting and use RPCIT CC1 / status 16
+     * to request the guest free DMA mappings when necessary.
      */
-    vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS;
-    if (vfio_size > 0 && vfio_size < cap->end_dma - cap->start_dma + 1) {
-        pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1;
+    if (!pbdev->rtr_allowed) {
+        vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS;
+        if (vfio_size > 0 && vfio_size < cap->end_dma - cap->start_dma + 1) {
+            pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1;
+        }
     }
 }
 
hw/s390x/s390-virtio-ccw.c +5 -0
index 3af613d4e9..c96cd0d4bb 100644
@@ -907,8 +907,13 @@ static void ccw_machine_9_2_instance_options(MachineState *machine)
 
 static void ccw_machine_9_2_class_options(MachineClass *mc)
 {
+    static GlobalProperty compat[] = {
+        { TYPE_S390_PCI_DEVICE, "relaxed-translation", "off", },
+    };
+
     ccw_machine_10_0_class_options(mc);
     compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len);
+    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
 }
 DEFINE_CCW_MACHINE(9, 2);
 
include/hw/s390x/s390-pci-bus.h +4 -0
index 2c43ea123f..27732247cf 100644
@@ -277,7 +277,9 @@ struct S390PCIIOMMU {
     AddressSpace as;
     MemoryRegion mr;
     IOMMUMemoryRegion iommu_mr;
+    MemoryRegion dm_mr;
     bool enabled;
+    bool direct_map;
     uint64_t g_iota;
     uint64_t pba;
     uint64_t pal;
@@ -362,6 +364,7 @@ struct S390PCIBusDevice {
     bool interp;
     bool forwarding_assist;
     bool aif;
+    bool rtr_allowed;
     QTAILQ_ENTRY(S390PCIBusDevice) link;
 };
 
@@ -389,6 +392,7 @@ int pci_chsc_sei_nt2_have_event(void);
 void s390_pci_sclp_configure(SCCB *sccb);
 void s390_pci_sclp_deconfigure(SCCB *sccb);
 void s390_pci_iommu_enable(S390PCIIOMMU *iommu);
+void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu);
 void s390_pci_iommu_disable(S390PCIIOMMU *iommu);
 void s390_pci_generate_error_event(uint16_t pec, uint32_t fh, uint32_t fid,
                                    uint64_t faddr, uint32_t e);
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

Quoted 49 lines · Matthew Rosato

When receiving a guest mpcifc(4) or mpcifc(6) instruction without the T bit set, treat this as a request to perform direct mapping instead of address translation. In order to facilitate this, pin the entirety of guest memory into the host iommu.

Pinning for the direct mapping case is handled via vfio and its memory listener. Additionally, ram discard settings are inherited from vfio:
coordinated discards (e.g. virtio-mem) are allowed while uncoordinated discards (e.g. virtio-balloon) are disabled.

Subsequent guest DMA operations are all expected to be of the format guest_phys+sdma, allowing them to be used as lookup into the host iommu table.

---
  hw/s390x/s390-pci-bus.c         | 36 +++++++++++++++++++++++++++++++--
  hw/s390x/s390-pci-inst.c        | 13 ++++++++++--
  hw/s390x/s390-pci-vfio.c        | 15 ++++++++++----
  hw/s390x/s390-virtio-ccw.c      |  5 +++++
  include/hw/s390x/s390-pci-bus.h |  4 ++++
  5 files changed, 65 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +7 -0
index eead269cc2..8cf5aec1a2 100644
@@ -18,6 +18,8 @@
  #include "hw/s390x/s390-pci-inst.h"
  #include "hw/s390x/s390-pci-kvm.h"
  #include "hw/s390x/s390-pci-vfio.h"
+#include "hw/s390x/s390-virtio-ccw.h"
+#include "hw/boards.h"
  #include "hw/pci/pci_bus.h"
  #include "hw/qdev-properties.h"
  #include "hw/pci/pci_bridge.h"
@@ -720,16 +722,44 @@ void s390_pci_iommu_enable(S390PCIIOMMU *iommu)
                               TYPE_S390_IOMMU_MEMORY_REGION, OBJECT(&iommu->mr),
                               name, iommu->pal + 1);
      iommu->enabled = true;
+    iommu->direct_map = false;
      memory_region_add_subregion(&iommu->mr, 0, MEMORY_REGION(&iommu->iommu_mr));
      g_free(name);
  }
 
+void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu)
+{
+    MachineState *ms = MACHINE(qdev_get_machine());
+    S390CcwMachineState *s390ms = S390_CCW_MACHINE(ms);

hw/s390x/s390-pci-bus.c void s390_pci_iommu_enable(S390PCIIOMMU *iommu) · line 743

Quoted 10 lines
hw/s390x/s390-pci-bus.c +10 -0
+
+    /*
+     * For direct-mapping we must map the entire guest address space.  Rather
+     * than using an iommu, create a memory region alias that maps GPA X to
+     * iova X + SDMA.  VFIO will handle pinning via its memory listener.
+     */
+    g_autofree char *name = g_strdup_printf("iommu-dm-s390-%04x",
+                                            iommu->pbdev->uid);
+    memory_region_init_alias(&iommu->dm_mr, OBJECT(&iommu->mr), name, ms->ram,
+                             0, s390_get_memory_limit(s390ms));

Hm, the memory limit can exceed ms->ram.

Would it be possible to use get_system_memory() here, such that whatever is mapped into physical address space (including virtio-mem devices etc) would simply be aliased with an offset?

Or does that blow up elsewhere?

target/i386/kvm/kvm.c seems to do that:

memory_region_init_alias(&smram_as_mem, OBJECT(kvm_state), "mem-smram",

			 get_system_memory(), 0, ~0ull);

and target/i386/tcg/system/tcg-cpu.c

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

Quoted 18 lines
Quoted 5 lines

  +void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu)

hw/s390x/s390-pci-bus.c +3 -0
+{
+    MachineState *ms = MACHINE(qdev_get_machine());
+    S390CcwMachineState *s390ms = S390_CCW_MACHINE(ms);

hw/s390x/s390-pci-bus.c void s390_pci_iommu_enable(S390PCIIOMMU *iommu) · line 743

Quoted 10 lines
hw/s390x/s390-pci-bus.c +10 -0
+
+    /*
+     * For direct-mapping we must map the entire guest address space.  Rather
+     * than using an iommu, create a memory region alias that maps GPA X to
+     * iova X + SDMA.  VFIO will handle pinning via its memory listener.
+     */
+    g_autofree char *name = g_strdup_printf("iommu-dm-s390-%04x",
+                                            iommu->pbdev->uid);
+    memory_region_init_alias(&iommu->dm_mr, OBJECT(&iommu->mr), name, ms->ram,
+                             0, s390_get_memory_limit(s390ms));

Hm, the memory limit can exceed  ms->ram.

Quoted 3 lines

Would it be possible to use get_system_memory() here, such that whatever is mapped into physical address space (including virtio-mem devices etc) would simply be aliased with an offset?

Or does that blow up elsewhere?

Testing with

    memory_region_init_alias(&iommu->dm_mr, OBJECT(&iommu->mr), name,
                             get_system_memory(), 0,
                             s390_get_memory_limit(s390ms));

Looks good so far, will change for next version

Quoted 8 lines

target/i386/kvm/kvm.c seems to do that:

memory_region_init_alias(&smram_as_mem, OBJECT(kvm_state), "mem-smram", get_system_memory(), 0, ~0ull);

and target/i386/tcg/system/tcg-cpu.c

#4 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

In reply to Matthew Rosato
Quoted 16 lines · Matthew Rosato

When receiving a guest mpcifc(4) or mpcifc(6) instruction without the T bit set, treat this as a request to perform direct mapping instead of address translation. In order to facilitate this, pin the entirety of guest memory into the host iommu.

Pinning for the direct mapping case is handled via vfio and its memory listener. Additionally, ram discard settings are inherited from vfio:
coordinated discards (e.g. virtio-mem) are allowed while uncoordinated discards (e.g. virtio-balloon) are disabled.

Subsequent guest DMA operations are all expected to be of the format guest_phys+sdma, allowing them to be used as lookup into the host iommu table.

---

...

Quoted 13 lines
hw/s390x/s390-pci-inst.c +1 -0
index e386d75d58..4c108fa8c4 100644
@@ -16,6 +16,7 @@
  #include "exec/memory.h"
  #include "qemu/error-report.h"
  #include "system/hw_accel.h"
+#include "hw/boards.h"
  #include "hw/pci/pci_device.h"
  #include "hw/s390x/s390-pci-inst.h"
  #include "hw/s390x/s390-pci-bus.h"

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1012

Quoted 5 lines
hw/s390x/s390-pci-inst.c +1 -1
@@ -1008,17 +1009,25 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
      }
 
      /* currently we only support designation type 1 with translation */
-    if (!(dt == ZPCI_IOTA_RTTO && t)) {
+    if (t && !(dt == ZPCI_IOTA_RTTO)) {

While you're at it, you could change that "!(dt == ZPCI_IOTA_RTTO)" into "dt != ZPCI_IOTA_RTTO".

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1017

Quoted 5 lines
hw/s390x/s390-pci-inst.c +2 -0
          error_report("unsupported ioat dt %d t %d", dt, t);
          s390_program_interrupt(env, PGM_OPERAND, ra);
          return -EINVAL;
+    } else if (!t && !pbdev->rtr_allowed) {
+        error_report("relaxed translation not allowed");

Not sure, but maybe better use qemu_log_mask(LOG_GUEST_ERROR, ...) instead?

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1020

Quoted 3 lines
hw/s390x/s390-pci-inst.c +2 -0
+        s390_program_interrupt(env, PGM_OPERAND, ra);
+        return -EINVAL;
      }

Thomas

#5 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

Quoted 11 lines

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1011

Quoted 7 lines

  #include "hw/s390x/s390-pci-bus.h"

hw/s390x/s390-pci-inst.c +1 -1
@@ -1008,17 +1009,25 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
       }
         /* currently we only support designation type 1 with translation */
-    if (!(dt == ZPCI_IOTA_RTTO && t)) {
+    if (t && !(dt == ZPCI_IOTA_RTTO)) {

While you're at it, you could change that "!(dt == ZPCI_IOTA_RTTO)" into "dt != ZPCI_IOTA_RTTO".

ACK

Quoted 8 lines

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1017

Quoted 5 lines
hw/s390x/s390-pci-inst.c +2 -0
           error_report("unsupported ioat dt %d t %d", dt, t);
           s390_program_interrupt(env, PGM_OPERAND, ra);
           return -EINVAL;
+    } else if (!t && !pbdev->rtr_allowed) {
+        error_report("relaxed translation not allowed");

Not sure, but maybe better use qemu_log_mask(LOG_GUEST_ERROR, ...) instead?

Hrm, this one I'm not so sure. If I force this path and we give back the operand exception to the guest, the guest kernel is going to panic as a result. We are very much in a "that wasn't ever supposed to happen" path, just like the unsupported dt above, because we already reported what we do (not) support via CLP.

I just tried to force the path with default settings + the above change, and I don't see anything in the qemu log from qemu_log_mask(LOG_GUEST_ERROR, ...) but I do with error_report. Given the unlikely scenario that we reach this path and the potential ramifications, I think I'd rather leave it as error_report unless you've got a strong opinion on it!

Thanks,
Matt

#6 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

Quoted 20 lines · Matthew Rosato
Quoted 11 lines

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1011

Quoted 7 lines

  #include "hw/s390x/s390-pci-bus.h"

hw/s390x/s390-pci-inst.c +1 -1
@@ -1008,17 +1009,25 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
       }
         /* currently we only support designation type 1 with translation */
-    if (!(dt == ZPCI_IOTA_RTTO && t)) {
+    if (t && !(dt == ZPCI_IOTA_RTTO)) {

While you're at it, you could change that "!(dt == ZPCI_IOTA_RTTO)" into "dt != ZPCI_IOTA_RTTO".

ACK

Quoted 6 lines
Quoted 5 lines
hw/s390x/s390-pci-inst.c +2 -0
           error_report("unsupported ioat dt %d t %d", dt, t);
           s390_program_interrupt(env, PGM_OPERAND, ra);
           return -EINVAL;
+    } else if (!t && !pbdev->rtr_allowed) {
+        error_report("relaxed translation not allowed");
Quoted 6 lines
Quoted 2 lines

Not sure, but maybe better use qemu_log_mask(LOG_GUEST_ERROR, ...) instead?

Hrm, this one I'm not so sure. If I force this path and we give back the operand exception to the guest, the guest kernel is going to panic as a result. We are very much in a "that wasn't ever supposed to happen" path, just like the unsupported dt above, because we already reported what we do (not) support via CLP.

I just tried to force the path with default settings + the above change, and I don't see anything in the qemu log from qemu_log_mask(LOG_GUEST_ERROR, ...) but I do with error_report.

The qemu_log_mask() stuff has to be enabled on the command line with "-d guest_errors".

Quoted 1 line

Given the unlikely scenario that we reach this path and the potential ramifications, I think I'd rather leave it as error_report unless you've got a strong opinion on it!

Ok, fine for me, too.

Thomas

#7 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

In reply to Matthew Rosato
Quoted 44 lines · Matthew Rosato

When receiving a guest mpcifc(4) or mpcifc(6) instruction without the T bit set, treat this as a request to perform direct mapping instead of address translation. In order to facilitate this, pin the entirety of guest memory into the host iommu.

Pinning for the direct mapping case is handled via vfio and its memory listener. Additionally, ram discard settings are inherited from vfio:
coordinated discards (e.g. virtio-mem) are allowed while uncoordinated discards (e.g. virtio-balloon) are disabled.

Subsequent guest DMA operations are all expected to be of the format guest_phys+sdma, allowing them to be used as lookup into the host iommu table.

---
 hw/s390x/s390-pci-bus.c         | 36 +++++++++++++++++++++++++++++++--
 hw/s390x/s390-pci-inst.c        | 13 ++++++++++--
 hw/s390x/s390-pci-vfio.c        | 15 ++++++++++----
 hw/s390x/s390-virtio-ccw.c      |  5 +++++
 include/hw/s390x/s390-pci-bus.h |  4 ++++
 5 files changed, 65 insertions(+), 8 deletions(-)
hw/s390x/s390-pci-bus.c +3 -0
index eead269cc2..8cf5aec1a2 100644
@@ -18,6 +18,8 @@
 #include "hw/s390x/s390-pci-inst.h"
 #include "hw/s390x/s390-pci-kvm.h"
 #include "hw/s390x/s390-pci-vfio.h"
+#include "hw/s390x/s390-virtio-ccw.h"
+#include "hw/boards.h"
 #include "hw/pci/pci_bus.h"
 #include "hw/qdev-properties.h"
 #include "hw/pci/pci_bridge.h"
@@ -720,16 +722,44 @@ void s390_pci_iommu_enable(S390PCIIOMMU *iommu)
                              TYPE_S390_IOMMU_MEMORY_REGION, OBJECT(&iommu->mr),
                              name, iommu->pal + 1);
     iommu->enabled = true;
+    iommu->direct_map = false;
     memory_region_add_subregion(&iommu->mr, 0, MEMORY_REGION(&iommu->iommu_mr));
     g_free(name);
 }

hw/s390x/s390-pci-bus.c void s390_pci_iommu_enable(S390PCIIOMMU *iommu) · line 738

Quoted 10 lines
hw/s390x/s390-pci-bus.c +9 -0
 
+void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu)
+{
+    MachineState *ms = MACHINE(qdev_get_machine());
+    S390CcwMachineState *s390ms = S390_CCW_MACHINE(ms);
+
+    /*
+     * For direct-mapping we must map the entire guest address space.  Rather
+     * than using an iommu, create a memory region alias that maps GPA X to
+     * iova X + SDMA.  VFIO will handle pinning via its memory listener.

Nit: I'd capitalize IOVA to match GPA.

Quoted 30 lines
hw/s390x/s390-pci-bus.c +20 -2
+     */
+    g_autofree char *name = g_strdup_printf("iommu-dm-s390-%04x",
+                                            iommu->pbdev->uid);
+    memory_region_init_alias(&iommu->dm_mr, OBJECT(&iommu->mr), name, ms->ram,
+                             0, s390_get_memory_limit(s390ms));
+    iommu->enabled = true;
+    iommu->direct_map = true;
+    memory_region_add_subregion(&iommu->mr, iommu->pbdev->zpci_fn.sdma,
+                                &iommu->dm_mr);
+}
+
 void s390_pci_iommu_disable(S390PCIIOMMU *iommu)
 {
     iommu->enabled = false;
     g_hash_table_remove_all(iommu->iotlb);
-    memory_region_del_subregion(&iommu->mr, MEMORY_REGION(&iommu->iommu_mr));
-    object_unparent(OBJECT(&iommu->iommu_mr));
+    if (iommu->direct_map) {
+        memory_region_del_subregion(&iommu->mr, &iommu->dm_mr);
+        iommu->direct_map = false;
+        object_unparent(OBJECT(&iommu->dm_mr));
+    } else {
+        memory_region_del_subregion(&iommu->mr,
+                                    MEMORY_REGION(&iommu->iommu_mr));
+        object_unparent(OBJECT(&iommu->iommu_mr));
+    }
 }
 
 static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)

hw/s390x/s390-pci-bus.c static const Property s390_pci_device_properties[] = { · line 1522

Quoted 5 lines
hw/s390x/s390-pci-bus.c +2 -0
@@ -1488,6 +1518,8 @@ static const Property s390_pci_device_properties[] = {
     DEFINE_PROP_BOOL("interpret", S390PCIBusDevice, interp, true),
     DEFINE_PROP_BOOL("forwarding-assist", S390PCIBusDevice, forwarding_assist,
                      true),
+    DEFINE_PROP_BOOL("relaxed-translation", S390PCIBusDevice, rtr_allowed,
+                     true),

Question: Do we maybe want to default rtr_allowed to false for ISM devices? Performance wise it doesn't matter much since they keep their mappings fairly static and it would help us catch bugs in the handling of rtr_allowed == false devices, the ISM driver and increase security.

Quoted 16 lines
hw/s390x/s390-pci-bus.c +0 -0
 };
 
 static const VMStateDescription s390_pci_device_vmstate = {
hw/s390x/s390-pci-inst.c +1 -0
index e386d75d58..4c108fa8c4 100644
@@ -16,6 +16,7 @@
 #include "exec/memory.h"
 #include "qemu/error-report.h"
 #include "system/hw_accel.h"
+#include "hw/boards.h"
 #include "hw/pci/pci_device.h"
 #include "hw/s390x/s390-pci-inst.h"
 #include "hw/s390x/s390-pci-bus.h"

hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1012

Quoted 5 lines
hw/s390x/s390-pci-inst.c +1 -1
@@ -1008,17 +1009,25 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
     }
 
     /* currently we only support designation type 1 with translation */
-    if (!(dt == ZPCI_IOTA_RTTO && t)) {
+    if (t && !(dt == ZPCI_IOTA_RTTO)) {

What Thomas said ;-)

Quoted 27 lines
hw/s390x/s390-pci-inst.c +9 -1
         error_report("unsupported ioat dt %d t %d", dt, t);
         s390_program_interrupt(env, PGM_OPERAND, ra);
         return -EINVAL;
+    } else if (!t && !pbdev->rtr_allowed) {
+        error_report("relaxed translation not allowed");
+        s390_program_interrupt(env, PGM_OPERAND, ra);
+        return -EINVAL;
     }
 
     iommu->pba = pba;
     iommu->pal = pal;
     iommu->g_iota = g_iota;
 
-    s390_pci_iommu_enable(iommu);
+    if (t) {
+        s390_pci_iommu_enable(iommu);
+    } else {
+        s390_pci_iommu_dm_enable(iommu);
+    }
 
     return 0;
 }
hw/s390x/s390-pci-vfio.c +0 -0
index 7dbbc76823..dad525c81c 100644

hw/s390x/s390-pci-vfio.c static void s390_pci_read_base(S390PCIBusDevice *pbdev, · line 141

Quoted 10 lines
hw/s390x/s390-pci-vfio.c +6 -1
@@ -133,11 +133,18 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
 
     /*
      * If appropriate, reduce the size of the supported DMA aperture reported
-     * to the guest based upon the vfio DMA limit.
+     * to the guest based upon the vfio DMA limit.  This is applicable for
+     * devices that are guaranteed to not use relaxed translation.  If the
+     * device is capable of relaxed translation then we must advertise the
+     * full aperture.  In this case, if translation is used then we will
+     * rely on the vfio DMA limit counting and use RPCIT CC1 / status 16
+     * to request the guest free DMA mappings when necessary.

Not a native speaker but I think there is a "to" missing in the last sentence and I'd have used "as necessary".

Quoted 45 lines
hw/s390x/s390-pci-vfio.c +5 -3
      */
-    vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS;
-    if (vfio_size > 0 && vfio_size < cap->end_dma - cap->start_dma + 1) {
-        pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1;
+    if (!pbdev->rtr_allowed) {
+        vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS;
+        if (vfio_size > 0 && vfio_size < cap->end_dma - cap->start_dma + 1) {
+            pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1;
+        }
     }
 }
 
hw/s390x/s390-virtio-ccw.c +5 -0
index 3af613d4e9..c96cd0d4bb 100644
@@ -907,8 +907,13 @@ static void ccw_machine_9_2_instance_options(MachineState *machine)
 
 static void ccw_machine_9_2_class_options(MachineClass *mc)
 {
+    static GlobalProperty compat[] = {
+        { TYPE_S390_PCI_DEVICE, "relaxed-translation", "off", },
+    };
+
     ccw_machine_10_0_class_options(mc);
     compat_props_add(mc->compat_props, hw_compat_9_2, hw_compat_9_2_len);
+    compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
 }
 DEFINE_CCW_MACHINE(9, 2);
 
include/hw/s390x/s390-pci-bus.h +2 -0
index 2c43ea123f..27732247cf 100644
@@ -277,7 +277,9 @@ struct S390PCIIOMMU {
     AddressSpace as;
     MemoryRegion mr;
     IOMMUMemoryRegion iommu_mr;
+    MemoryRegion dm_mr;
     bool enabled;
+    bool direct_map;
     uint64_t g_iota;
     uint64_t pba;
     uint64_t pal;

include/hw/s390x/s390-pci-bus.h struct S390PCIBusDevice { · line 367

Quoted 4 lines
include/hw/s390x/s390-pci-bus.h +1 -0
@@ -362,6 +364,7 @@ struct S390PCIBusDevice {
     bool interp;
     bool forwarding_assist;
     bool aif;
+    bool rtr_allowed;

Nit: In the kernel in struct zpci_dev you used rtr_avail but "allowed" in the comment, just for gerppability I'd prefer the names to match.

include/hw/s390x/s390-pci-bus.h int pci_chsc_sei_nt2_have_event(void); · line 395

Quoted 8 lines
include/hw/s390x/s390-pci-bus.h +1 -0
     QTAILQ_ENTRY(S390PCIBusDevice) link;
 };
 
@@ -389,6 +392,7 @@ int pci_chsc_sei_nt2_have_event(void);
 void s390_pci_sclp_configure(SCCB *sccb);
 void s390_pci_sclp_deconfigure(SCCB *sccb);
 void s390_pci_iommu_enable(S390PCIIOMMU *iommu);
+void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu);

Nit: I find "_dm_" a bit hard to map to "direct map". If you want two letters I'd go for "_pt_" for "_iommu_pass_through_" or maybe "_direct_map_".

Quoted 3 lines
include/hw/s390x/s390-pci-bus.h +0 -0
 void s390_pci_iommu_disable(S390PCIIOMMU *iommu);
 void s390_pci_generate_error_event(uint16_t pec, uint32_t fh, uint32_t fid,
                                    uint64_t faddr, uint32_t e);
#8 lore raw

Re: [PATCH v3 1/2] s390x/pci: add support for guests that request direct mapping

Quoted 15 lines

hw/s390x/s390-pci-bus.c static const Property s390_pci_device_properties[] = { · line 1522

Quoted 9 lines

static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)

hw/s390x/s390-pci-bus.c +2 -0
@@ -1488,6 +1518,8 @@ static const Property s390_pci_device_properties[] = {
     DEFINE_PROP_BOOL("interpret", S390PCIBusDevice, interp, true),
     DEFINE_PROP_BOOL("forwarding-assist", S390PCIBusDevice, forwarding_assist,
                      true),
+    DEFINE_PROP_BOOL("relaxed-translation", S390PCIBusDevice, rtr_allowed,
+                     true),

Question: Do we maybe want to default rtr_allowed to false for ISM devices? Performance wise it doesn't matter much since they keep their mappings fairly static and it would help us catch bugs in the handling of rtr_allowed == false devices, the ISM driver and increase security.

I've been asking myself the same. Believe we've discussed it in the past (off-list) too.

I'd be fine with that. I think it doesn't change the line above, but I can add a check against pft in s390_pcihost_plug() after clp info is read in, along with a small comment, that changes the setting to false for ISM devices.

Note that there is one side effect I can think of based on the kernel implementation - if a guest has, say, NVMe and ISM passed through with iommu.passthrough=1 specified then they would always see the "Falling back to IOMMU_DOMAIN_DMA" message for the ISM device(s) because of rtr_allowed == false.

Quoted 10 lines

hw/s390x/s390-pci-vfio.c static void s390_pci_read_base(S390PCIBusDevice *pbdev, · line 141

Quoted 9 lines
hw/s390x/s390-pci-vfio.c +6 -1
     /*
      * If appropriate, reduce the size of the supported DMA aperture reported
-     * to the guest based upon the vfio DMA limit.
+     * to the guest based upon the vfio DMA limit.  This is applicable for
+     * devices that are guaranteed to not use relaxed translation.  If the
+     * device is capable of relaxed translation then we must advertise the
+     * full aperture.  In this case, if translation is used then we will
+     * rely on the vfio DMA limit counting and use RPCIT CC1 / status 16
+     * to request the guest free DMA mappings when necessary.
Quoted 2 lines

Not a native speaker but I think there is a "to" missing in the last sentence and I'd have used "as necessary".

'request that the' would probably work too... But yeah, something is missing, I'll re-word.

Quoted 8 lines

include/hw/s390x/s390-pci-bus.h struct S390PCIBusDevice { · line 367

Quoted 5 lines
include/hw/s390x/s390-pci-bus.h +1 -0
@@ -362,6 +364,7 @@ struct S390PCIBusDevice {
     bool interp;
     bool forwarding_assist;
     bool aif;
+    bool rtr_allowed;

Nit: In the kernel in struct zpci_dev you used rtr_avail but "allowed" in the comment, just for gerppability I'd prefer the names to match.

I guess in my head, QEMU is the one allowing it and the guest kernel is checking whether or not it's available.

But I have no strong opinion on the name; can rename the QEMU variable to rtr_avail

Quoted 4 lines
Quoted 4 lines
include/hw/s390x/s390-pci-bus.h +0 -0
     QTAILQ_ENTRY(S390PCIBusDevice) link;
 };
 
Quoted 9 lines

include/hw/s390x/s390-pci-bus.h int pci_chsc_sei_nt2_have_event(void); · line 395

Quoted 5 lines
include/hw/s390x/s390-pci-bus.h +1 -0
@@ -389,6 +392,7 @@ int pci_chsc_sei_nt2_have_event(void);
 void s390_pci_sclp_configure(SCCB *sccb);
 void s390_pci_sclp_deconfigure(SCCB *sccb);
 void s390_pci_iommu_enable(S390PCIIOMMU *iommu);
+void s390_pci_iommu_dm_enable(S390PCIIOMMU *iommu);

Nit: I find "_dm_" a bit hard to map to "direct map". If you want two letters I'd go for "_pt_" for "_iommu_pass_through_" or maybe "_direct_map_".

OK

#9 lore raw

PATCH v3 2/2 s390x/pci: indicate QEMU supports relaxed translation for passthrough

In reply to Matthew Rosato

Specifying this bit in the guest CLP response indicates that the guest can optionally choose to skip translation and instead use identity-mapped operations.

---
 hw/s390x/s390-pci-vfio.c        | 5 ++++-
 include/hw/s390x/s390-pci-clp.h | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-vfio.c +4 -1
index dad525c81c..69543d1ca8 100644
@@ -230,8 +230,11 @@ static void s390_pci_read_group(S390PCIBusDevice *pbdev,
         pbdev->pci_group = s390_group_create(pbdev->zpci_fn.pfgid, start_gid);
 
         resgrp = &pbdev->pci_group->zpci_group;
+        if (pbdev->rtr_allowed) {
+            resgrp->fr |= CLP_RSP_QPCIG_MASK_RTR;
+        }
         if (cap->flags & VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH) {
-            resgrp->fr = 1;
+            resgrp->fr |= CLP_RSP_QPCIG_MASK_REFRESH;
         }
         resgrp->dasm = cap->dasm;
         resgrp->msia = cap->msi_addr;
include/hw/s390x/s390-pci-clp.h +1 -0
index 03b7f9ba5f..6a635d693b 100644
@@ -158,6 +158,7 @@ typedef struct ClpRspQueryPciGrp {
 #define CLP_RSP_QPCIG_MASK_NOI 0xfff
     uint16_t i;
     uint8_t version;
+#define CLP_RSP_QPCIG_MASK_RTR     0x20
 #define CLP_RSP_QPCIG_MASK_FRAME   0x2
 #define CLP_RSP_QPCIG_MASK_REFRESH 0x1
     uint8_t fr;
Signature · 1 line
2.48.1