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