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

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