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