ed5979
From ee69c8c57fe62fc200f749c4ce3927c88803644d Mon Sep 17 00:00:00 2001
ed5979
From: Matthew Rosato <mjrosato@linux.ibm.com>
ed5979
Date: Fri, 28 Oct 2022 15:47:58 -0400
ed5979
Subject: [PATCH 7/9] s390x/pci: shrink DMA aperture to be bound by vfio DMA
ed5979
 limit
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Cédric Le Goater <clg@redhat.com>
ed5979
RH-MergeRequest: 141: s390x/pci: reset ISM passthrough devices on shutdown and system reset
ed5979
RH-Bugzilla: 2163701
ed5979
RH-Acked-by: Thomas Huth <thuth@redhat.com>
ed5979
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [2/3] 0956bbb4773dd0085f6aed59d6284c704b4fed3b (clegoate/qemu-kvm-c9s)
ed5979
ed5979
Currently, s390x-pci performs accounting against the vfio DMA
ed5979
limit and triggers the guest to clean up mappings when the limit
ed5979
is reached. Let's go a step further and also limit the size of
ed5979
the supported DMA aperture reported to the guest based upon the
ed5979
initial vfio DMA limit reported for the container (if less than
ed5979
than the size reported by the firmware/host zPCI layer).  This
ed5979
avoids processing sections of the guest DMA table during global
ed5979
refresh that, for common use cases, will never be used anway, and
ed5979
makes exhausting the vfio DMA limit due to mismatch between guest
ed5979
aperture size and host limit far less likely and more indicitive
ed5979
of an error.
ed5979
ed5979
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
ed5979
Message-Id: <20221028194758.204007-4-mjrosato@linux.ibm.com>
ed5979
Reviewed-by: Eric Farman <farman@linux.ibm.com>
ed5979
Signed-off-by: Thomas Huth <thuth@redhat.com>
ed5979
(cherry picked from commit df202e3ff3fccb49868e08f20d0bda86cb953fbe)
ed5979
Signed-off-by: Cédric Le Goater <clg@redhat.com>
ed5979
---
ed5979
 hw/s390x/s390-pci-vfio.c        | 11 +++++++++++
ed5979
 include/hw/s390x/s390-pci-bus.h |  1 +
ed5979
 2 files changed, 12 insertions(+)
ed5979
ed5979
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
ed5979
index 5f0adb0b4a..f7bf36cec8 100644
ed5979
--- a/hw/s390x/s390-pci-vfio.c
ed5979
+++ b/hw/s390x/s390-pci-vfio.c
ed5979
@@ -84,6 +84,7 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,
ed5979
     cnt->users = 1;
ed5979
     cnt->avail = avail;
ed5979
     QTAILQ_INSERT_TAIL(&s->zpci_dma_limit, cnt, link);
ed5979
+    pbdev->iommu->max_dma_limit = avail;
ed5979
     return cnt;
ed5979
 }
ed5979
 
ed5979
@@ -103,6 +104,7 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
ed5979
     struct vfio_info_cap_header *hdr;
ed5979
     struct vfio_device_info_cap_zpci_base *cap;
ed5979
     VFIOPCIDevice *vpci =  container_of(pbdev->pdev, VFIOPCIDevice, pdev);
ed5979
+    uint64_t vfio_size;
ed5979
 
ed5979
     hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE);
ed5979
 
ed5979
@@ -122,6 +124,15 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
ed5979
     /* The following values remain 0 until we support other FMB formats */
ed5979
     pbdev->zpci_fn.fmbl = 0;
ed5979
     pbdev->zpci_fn.pft = 0;
ed5979
+
ed5979
+    /*
ed5979
+     * If appropriate, reduce the size of the supported DMA aperture reported
ed5979
+     * to the guest based upon the vfio DMA limit.
ed5979
+     */
ed5979
+    vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS;
ed5979
+    if (vfio_size < (cap->end_dma - cap->start_dma + 1)) {
ed5979
+        pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1;
ed5979
+    }
ed5979
 }
ed5979
 
ed5979
 static bool get_host_fh(S390PCIBusDevice *pbdev, struct vfio_device_info *info,
ed5979
diff --git a/include/hw/s390x/s390-pci-bus.h b/include/hw/s390x/s390-pci-bus.h
ed5979
index 0605fcea24..1c46e3a269 100644
ed5979
--- a/include/hw/s390x/s390-pci-bus.h
ed5979
+++ b/include/hw/s390x/s390-pci-bus.h
ed5979
@@ -278,6 +278,7 @@ struct S390PCIIOMMU {
ed5979
     uint64_t g_iota;
ed5979
     uint64_t pba;
ed5979
     uint64_t pal;
ed5979
+    uint64_t max_dma_limit;
ed5979
     GHashTable *iotlb;
ed5979
     S390PCIDMACount *dma_limit;
ed5979
 };
ed5979
-- 
ed5979
2.31.1
ed5979