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