thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

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

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