Blame SOURCES/kvm-acpi-pcihp-pcie-set-power-on-cap-on-parent-slot.patch

495e37
From a3a4fd1733e71d029e38136366e73ace8e78298b Mon Sep 17 00:00:00 2001
495e37
From: Igor Mammedov <imammedo@redhat.com>
495e37
Date: Tue, 1 Mar 2022 10:11:59 -0500
495e37
Subject: [PATCH 02/14] acpi: pcihp: pcie: set power on cap on parent slot
495e37
495e37
RH-Author: Igor Mammedov <imammedo@redhat.com>
495e37
RH-MergeRequest: 124: RHEL-9.0 Fix broken PCIe device after migration
495e37
RH-Commit: [2/2] 135602421e5fed803f7d71121380306633d490d8
495e37
RH-Bugzilla: 2053584
495e37
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
495e37
RH-Acked-by: MST <None>
495e37
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
495e37
495e37
on creation a PCIDevice has power turned on at the end of pci_qdev_realize()
495e37
however later on if PCIe slot isn't populated with any children
495e37
it's power is turned off. It's fine if native hotplug is used
495e37
as plug callback will power slot on among other things.
495e37
However when ACPI hotplug is enabled it replaces native PCIe plug
495e37
callbacks with ACPI specific ones (acpi_pcihp_device_*plug_cb) and
495e37
as result slot stays powered off. It works fine as ACPI hotplug
495e37
on guest side takes care of enumerating/initializing hotplugged
495e37
device. But when later guest is migrated, call chain introduced by]
495e37
commit d5daff7d312 (pcie: implement slot power control for pcie root ports)
495e37
495e37
   pcie_cap_slot_post_load()
495e37
       -> pcie_cap_update_power()
495e37
           -> pcie_set_power_device()
495e37
               -> pci_set_power()
495e37
                   -> pci_update_mappings()
495e37
495e37
will disable earlier initialized BARs for the hotplugged device
495e37
in powered off slot due to commit 23786d13441 (pci: implement power state)
495e37
which disables BARs if power is off.
495e37
495e37
Fix it by setting PCI_EXP_SLTCTL_PCC to PCI_EXP_SLTCTL_PWR_ON
495e37
on slot (root port/downstream port) at the time a device
495e37
hotplugged into it. As result PCI_EXP_SLTCTL_PWR_ON is migrated
495e37
to target and above call chain keeps device plugged into it
495e37
powered on.
495e37
495e37
Fixes: d5daff7d312 ("pcie: implement slot power control for pcie root ports")
495e37
Fixes: 23786d13441 ("pci: implement power state")
495e37
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2053584
495e37
Suggested-by: "Michael S. Tsirkin" <mst@redhat.com>
495e37
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
495e37
Message-Id: <20220301151200.3507298-3-imammedo@redhat.com>
495e37
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
495e37
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
495e37
(cherry picked from commit 6b0969f1ec825984cd74619f0730be421b0c46fb)
495e37
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
495e37
---
495e37
 hw/acpi/pcihp.c       | 12 +++++++++++-
495e37
 hw/pci/pcie.c         | 11 +++++++++++
495e37
 include/hw/pci/pcie.h |  1 +
495e37
 3 files changed, 23 insertions(+), 1 deletion(-)
495e37
495e37
diff --git a/hw/acpi/pcihp.c b/hw/acpi/pcihp.c
495e37
index 30405b5113..3d5610e02d 100644
495e37
--- a/hw/acpi/pcihp.c
495e37
+++ b/hw/acpi/pcihp.c
495e37
@@ -32,6 +32,7 @@
495e37
 #include "hw/pci/pci_bridge.h"
495e37
 #include "hw/pci/pci_host.h"
495e37
 #include "hw/pci/pcie_port.h"
495e37
+#include "hw/pci-bridge/xio3130_downstream.h"
495e37
 #include "hw/i386/acpi-build.h"
495e37
 #include "hw/acpi/acpi.h"
495e37
 #include "hw/pci/pci_bus.h"
495e37
@@ -341,6 +342,8 @@ void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
495e37
 {
495e37
     PCIDevice *pdev = PCI_DEVICE(dev);
495e37
     int slot = PCI_SLOT(pdev->devfn);
495e37
+    PCIDevice *bridge;
495e37
+    PCIBus *bus;
495e37
     int bsel;
495e37
 
495e37
     /* Don't send event when device is enabled during qemu machine creation:
495e37
@@ -370,7 +373,14 @@ void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
495e37
         return;
495e37
     }
495e37
 
495e37
-    bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
495e37
+    bus = pci_get_bus(pdev);
495e37
+    bridge = pci_bridge_get_device(bus);
495e37
+    if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) ||
495e37
+        object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) {
495e37
+        pcie_cap_slot_enable_power(bridge);
495e37
+    }
495e37
+
495e37
+    bsel = acpi_pcihp_get_bsel(bus);
495e37
     g_assert(bsel >= 0);
495e37
     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
495e37
     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
495e37
diff --git a/hw/pci/pcie.c b/hw/pci/pcie.c
495e37
index d7d73a31e4..996f0e24fe 100644
495e37
--- a/hw/pci/pcie.c
495e37
+++ b/hw/pci/pcie.c
495e37
@@ -366,6 +366,17 @@ static void hotplug_event_clear(PCIDevice *dev)
495e37
     }
495e37
 }
495e37
 
495e37
+void pcie_cap_slot_enable_power(PCIDevice *dev)
495e37
+{
495e37
+    uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
495e37
+    uint32_t sltcap = pci_get_long(exp_cap + PCI_EXP_SLTCAP);
495e37
+
495e37
+    if (sltcap & PCI_EXP_SLTCAP_PCP) {
495e37
+        pci_set_word_by_mask(exp_cap + PCI_EXP_SLTCTL,
495e37
+                             PCI_EXP_SLTCTL_PCC, PCI_EXP_SLTCTL_PWR_ON);
495e37
+    }
495e37
+}
495e37
+
495e37
 static void pcie_set_power_device(PCIBus *bus, PCIDevice *dev, void *opaque)
495e37
 {
495e37
     bool *power = opaque;
495e37
diff --git a/include/hw/pci/pcie.h b/include/hw/pci/pcie.h
495e37
index 6063bee0ec..c27368d077 100644
495e37
--- a/include/hw/pci/pcie.h
495e37
+++ b/include/hw/pci/pcie.h
495e37
@@ -112,6 +112,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
495e37
                                 uint32_t addr, uint32_t val, int len);
495e37
 int pcie_cap_slot_post_load(void *opaque, int version_id);
495e37
 void pcie_cap_slot_push_attention_button(PCIDevice *dev);
495e37
+void pcie_cap_slot_enable_power(PCIDevice *dev);
495e37
 
495e37
 void pcie_cap_root_init(PCIDevice *dev);
495e37
 void pcie_cap_root_reset(PCIDevice *dev);
495e37
-- 
495e37
2.31.1
495e37