thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
7f1c5b
From 1ed1f8fc20a4883bc0bc1f58d299b0278abc5442 Mon Sep 17 00:00:00 2001
7f1c5b
From: Matthew Rosato <mjrosato@linux.ibm.com>
7f1c5b
Date: Fri, 9 Dec 2022 14:57:00 -0500
7f1c5b
Subject: [PATCH 8/9] s390x/pci: reset ISM passthrough devices on shutdown and
7f1c5b
 system reset
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: [3/3] c531352b9d57f51ba938d4c46ee19a5706ade697 (clegoate/qemu-kvm-c9s)
7f1c5b
7f1c5b
ISM device firmware stores unique state information that can
7f1c5b
can cause a wholesale unmap of the associated IOMMU (e.g. when
7f1c5b
we get a termination signal for QEMU) to trigger firmware errors
7f1c5b
because firmware believes we are attempting to invalidate entries
7f1c5b
that are still in-use by the guest OS (when in fact that guest is
7f1c5b
in the process of being terminated or rebooted).
7f1c5b
To alleviate this, register both a shutdown notifier (for unexpected
7f1c5b
termination cases e.g. virsh destroy) as well as a reset callback
7f1c5b
(for cases like guest OS reboot).  For each of these scenarios, trigger
7f1c5b
PCI device reset; this is enough to indicate to firmware that the IOMMU
7f1c5b
is no longer in-use by the guest OS, making it safe to invalidate any
7f1c5b
associated IOMMU entries.
7f1c5b
7f1c5b
Fixes: 15d0e7942d3b ("s390x/pci: don't fence interpreted devices without MSI-X")
7f1c5b
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
7f1c5b
Message-Id: <20221209195700.263824-1-mjrosato@linux.ibm.com>
7f1c5b
Reviewed-by: Eric Farman <farman@linux.ibm.com>
7f1c5b
[thuth: Adjusted the hunk in s390-pci-vfio.c due to different context]
7f1c5b
Signed-off-by: Thomas Huth <thuth@redhat.com>
7f1c5b
(cherry picked from commit 03451953c79e6b31f7860ee0c35b28e181d573c1)
7f1c5b
Signed-off-by: Cédric Le Goater <clg@redhat.com>
7f1c5b
---
7f1c5b
 hw/s390x/s390-pci-bus.c         | 28 ++++++++++++++++++++++++++++
7f1c5b
 hw/s390x/s390-pci-vfio.c        |  2 ++
7f1c5b
 include/hw/s390x/s390-pci-bus.h |  5 +++++
7f1c5b
 3 files changed, 35 insertions(+)
7f1c5b
7f1c5b
diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
7f1c5b
index 977e7daa15..02751f3597 100644
7f1c5b
--- a/hw/s390x/s390-pci-bus.c
7f1c5b
+++ b/hw/s390x/s390-pci-bus.c
7f1c5b
@@ -24,6 +24,8 @@
7f1c5b
 #include "hw/pci/msi.h"
7f1c5b
 #include "qemu/error-report.h"
7f1c5b
 #include "qemu/module.h"
7f1c5b
+#include "sysemu/reset.h"
7f1c5b
+#include "sysemu/runstate.h"
7f1c5b
 
7f1c5b
 #ifndef DEBUG_S390PCI_BUS
7f1c5b
 #define DEBUG_S390PCI_BUS  0
7f1c5b
@@ -150,10 +152,30 @@ out:
7f1c5b
     psccb->header.response_code = cpu_to_be16(rc);
7f1c5b
 }
7f1c5b
 
7f1c5b
+static void s390_pci_shutdown_notifier(Notifier *n, void *opaque)
7f1c5b
+{
7f1c5b
+    S390PCIBusDevice *pbdev = container_of(n, S390PCIBusDevice,
7f1c5b
+                                           shutdown_notifier);
7f1c5b
+
7f1c5b
+    pci_device_reset(pbdev->pdev);
7f1c5b
+}
7f1c5b
+
7f1c5b
+static void s390_pci_reset_cb(void *opaque)
7f1c5b
+{
7f1c5b
+    S390PCIBusDevice *pbdev = opaque;
7f1c5b
+
7f1c5b
+    pci_device_reset(pbdev->pdev);
7f1c5b
+}
7f1c5b
+
7f1c5b
 static void s390_pci_perform_unplug(S390PCIBusDevice *pbdev)
7f1c5b
 {
7f1c5b
     HotplugHandler *hotplug_ctrl;
7f1c5b
 
7f1c5b
+    if (pbdev->pft == ZPCI_PFT_ISM) {
7f1c5b
+        notifier_remove(&pbdev->shutdown_notifier);
7f1c5b
+        qemu_unregister_reset(s390_pci_reset_cb, pbdev);
7f1c5b
+    }
7f1c5b
+
7f1c5b
     /* Unplug the PCI device */
7f1c5b
     if (pbdev->pdev) {
7f1c5b
         DeviceState *pdev = DEVICE(pbdev->pdev);
7f1c5b
@@ -1111,6 +1133,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
7f1c5b
                 pbdev->fh |= FH_SHM_VFIO;
7f1c5b
                 pbdev->forwarding_assist = false;
7f1c5b
             }
7f1c5b
+            /* Register shutdown notifier and reset callback for ISM devices */
7f1c5b
+            if (pbdev->pft == ZPCI_PFT_ISM) {
7f1c5b
+                pbdev->shutdown_notifier.notify = s390_pci_shutdown_notifier;
7f1c5b
+                qemu_register_shutdown_notifier(&pbdev->shutdown_notifier);
7f1c5b
+                qemu_register_reset(s390_pci_reset_cb, pbdev);
7f1c5b
+            }
7f1c5b
         } else {
7f1c5b
             pbdev->fh |= FH_SHM_EMUL;
7f1c5b
             /* Always intercept emulated devices */
7f1c5b
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
7f1c5b
index f7bf36cec8..f51190d466 100644
7f1c5b
--- a/hw/s390x/s390-pci-vfio.c
7f1c5b
+++ b/hw/s390x/s390-pci-vfio.c
7f1c5b
@@ -124,6 +124,8 @@ 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
+    /* Store function type separately for type-specific behavior */
7f1c5b
+    pbdev->pft = cap->pft;
7f1c5b
 
7f1c5b
     /*
7f1c5b
      * If appropriate, reduce the size of the supported DMA aperture reported
7f1c5b
diff --git a/include/hw/s390x/s390-pci-bus.h b/include/hw/s390x/s390-pci-bus.h
7f1c5b
index 1c46e3a269..e0a9f9385b 100644
7f1c5b
--- a/include/hw/s390x/s390-pci-bus.h
7f1c5b
+++ b/include/hw/s390x/s390-pci-bus.h
7f1c5b
@@ -39,6 +39,9 @@
7f1c5b
 #define UID_CHECKING_ENABLED 0x01
7f1c5b
 #define ZPCI_DTSM 0x40
7f1c5b
 
7f1c5b
+/* zPCI Function Types */
7f1c5b
+#define ZPCI_PFT_ISM 5
7f1c5b
+
7f1c5b
 OBJECT_DECLARE_SIMPLE_TYPE(S390pciState, S390_PCI_HOST_BRIDGE)
7f1c5b
 OBJECT_DECLARE_SIMPLE_TYPE(S390PCIBus, S390_PCI_BUS)
7f1c5b
 OBJECT_DECLARE_SIMPLE_TYPE(S390PCIBusDevice, S390_PCI_DEVICE)
7f1c5b
@@ -344,6 +347,7 @@ struct S390PCIBusDevice {
7f1c5b
     uint16_t noi;
7f1c5b
     uint16_t maxstbl;
7f1c5b
     uint8_t sum;
7f1c5b
+    uint8_t pft;
7f1c5b
     S390PCIGroup *pci_group;
7f1c5b
     ClpRspQueryPci zpci_fn;
7f1c5b
     S390MsixInfo msix;
7f1c5b
@@ -352,6 +356,7 @@ struct S390PCIBusDevice {
7f1c5b
     MemoryRegion msix_notify_mr;
7f1c5b
     IndAddr *summary_ind;
7f1c5b
     IndAddr *indicator;
7f1c5b
+    Notifier shutdown_notifier;
7f1c5b
     bool pci_unplug_request_processed;
7f1c5b
     bool unplug_requested;
7f1c5b
     bool interp;
7f1c5b
-- 
7f1c5b
2.31.1
7f1c5b