Skip to contentSkip to conversation

hw/pcie: better hotplug/hotunplug support

PATCH Superseded Marcel Apfelbaum 3 participants 16 messages Jun 19, 2014 – Jun 22, 2014

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover hw/pcie: better hotplug/hotunplug support

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

patch 1/3: trivial debug message fix
patch 2/3: enable 'power controller' to receive power events from guests patch 3/3: the actual hotplug/hotunplug implementation.

Tested with Linux and Windows guests and with an e1000 with "PCIe" capability. Notes:
Windows requires devices to be pci express in order to enable hotplug functionality, so we need to think about converting virtio devices to pci express.
Linux outputs a "Surprise Removal/Addition" info message because we trigger 2 events in the same time, however this warning can be disregarded or a kernel patch submitted for our scenario.

Marcel Apfelbaum (3):
hw/pcie: corrected a debug message hw/pcie: implement power controller functionality hw/pcie: better hotplug/hotunplug support

 hw/pci/pcie.c              | 37 ++++++++++++++++++++++++++++++-------
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 32 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 hw/pcie: corrected a debug message

Trivial issue, discovered while debugging.

---
 hw/pci/pcie.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/pci/pcie.c +1 -1
index 02cde6f..ae92f00 100644
@@ -224,7 +224,7 @@ static void pcie_cap_slot_hotplug_common(PCIDevice *hotplug_dev,
     *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
     uint16_t sltsta = pci_get_word(*exp_cap + PCI_EXP_SLTSTA);
 
-    PCIE_DEV_PRINTF(PCI_DEVICE(dev), "hotplug state: %d\n", state);
+    PCIE_DEV_PRINTF(PCI_DEVICE(dev), "hotplug state: 0x%x\n", sltsta);
     if (sltsta & PCI_EXP_SLTSTA_EIS) {
         /* the slot is electromechanically locked.
          * This error is propagated up to qdev and then to HMP/QMP.
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 hw/pcie: implement power controller functionality

In reply to Marcel Apfelbaum

It is needed by hot-unplug in order to get an indication from the OS when the device can be physically detached.

---
 hw/pci/pcie.c              | 15 ++++++++++++++-
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/pci/pcie.c +14 -1
index ae92f00..f8bf515 100644
@@ -292,16 +292,19 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
                                PCI_EXP_SLTCAP_HPC |
                                PCI_EXP_SLTCAP_PIP |
                                PCI_EXP_SLTCAP_AIP |
+                               PCI_EXP_SLTCAP_PCP |
                                PCI_EXP_SLTCAP_ABP);
 
     pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC);
     pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC_OFF |
                                PCI_EXP_SLTCTL_AIC_OFF);
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |
+                               PCI_EXP_SLTCTL_PCC |
                                PCI_EXP_SLTCTL_AIC |
                                PCI_EXP_SLTCTL_HPIE |
                                PCI_EXP_SLTCTL_CCIE |
@@ -327,21 +330,31 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
 void pcie_cap_slot_reset(PCIDevice *dev)
 {
     uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
+    PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+    int pic;
+
+    pic = slot_dev ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
 
     PCIE_DEV_PRINTF(dev, "reset\n");
 
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_EIC |
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC |
                                  PCI_EXP_SLTCTL_HPIE |
                                  PCI_EXP_SLTCTL_CCIE |
                                  PCI_EXP_SLTCTL_PDCE |
                                  PCI_EXP_SLTCTL_ABPE);
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
-                               PCI_EXP_SLTCTL_PIC_OFF |
+                               pic |
                                PCI_EXP_SLTCTL_AIC_OFF);
 
+    if (!slot_dev) {
+        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
+                                   PCI_EXP_SLTCTL_PCC);
+    }
+
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                  PCI_EXP_SLTSTA_EIS |/* on reset,
                                                         the lock is released */
include/hw/pci/pcie_regs.h +2 -0
index 4d123d9..652d9fc 100644
@@ -57,6 +57,8 @@
 #define PCI_EXP_SLTCTL_PIC_SHIFT        (ffs(PCI_EXP_SLTCTL_PIC) - 1)
 #define PCI_EXP_SLTCTL_PIC_OFF                          \
     (PCI_EXP_SLTCTL_IND_OFF << PCI_EXP_SLTCTL_PIC_SHIFT)
+#define PCI_EXP_SLTCTL_PIC_ON                          \
+    (PCI_EXP_SLTCTL_IND_ON << PCI_EXP_SLTCTL_PIC_SHIFT)
 
 #define PCI_EXP_SLTCTL_SUPPORTED        \
             (PCI_EXP_SLTCTL_ABPE |      \
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [Qemu-devel] [PATCH 2/3] hw/pcie: implement power controller functionality

Quoted 23 lines · Marcel Apfelbaum

It is needed by hot-unplug in order to get an indication from the OS when the device can be physically detached.

---
 hw/pci/pcie.c              | 15 ++++++++++++++-
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/pci/pcie.c +2 -0
index ae92f00..f8bf515 100644
@@ -292,16 +292,19 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
                                PCI_EXP_SLTCAP_HPC |
                                PCI_EXP_SLTCAP_PIP |
                                PCI_EXP_SLTCAP_AIP |
+                               PCI_EXP_SLTCAP_PCP |
                                PCI_EXP_SLTCAP_ABP);
 
     pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 310

Quoted 10 lines
hw/pci/pcie.c +1 -0
                                  PCI_EXP_SLTCTL_AIC);
     pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC_OFF |
                                PCI_EXP_SLTCTL_AIC_OFF);
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |
+                               PCI_EXP_SLTCTL_PCC |
                                PCI_EXP_SLTCTL_AIC |
                                PCI_EXP_SLTCTL_HPIE |
                                PCI_EXP_SLTCTL_CCIE |

Need to disable for compat types?

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 333

Quoted 5 lines
hw/pci/pcie.c +1 -0
@@ -327,21 +330,31 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
 void pcie_cap_slot_reset(PCIDevice *dev)
 {
     uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
+    PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

What does this mean?
Downstream port?
A switch can have multiple downstream ports at any slot #.

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 334

Quoted 1 line
hw/pci/pcie.c +1 -0
+    int pic;

uint16_t please.

Quoted 12 lines
hw/pci/pcie.c +3 -0
+
+    pic = slot_dev ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
 
     PCIE_DEV_PRINTF(dev, "reset\n");
 
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_EIC |
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC |
                                  PCI_EXP_SLTCTL_HPIE |
                                  PCI_EXP_SLTCTL_CCIE |

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 355

Quoted 10 lines
hw/pci/pcie.c +4 -1
                                  PCI_EXP_SLTCTL_PDCE |
                                  PCI_EXP_SLTCTL_ABPE);
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
-                               PCI_EXP_SLTCTL_PIC_OFF |
+                               pic |
                                PCI_EXP_SLTCTL_AIC_OFF);
 
+    if (!slot_dev) {
+        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
+                                   PCI_EXP_SLTCTL_PCC);

I dislike it when we clear bits then set them back. Please just add else here.

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 357

Quoted 2 lines
hw/pci/pcie.c +2 -0
+    }
+

Need to disable for compat types?

Quoted 18 lines
hw/pci/pcie.c +0 -0
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                  PCI_EXP_SLTSTA_EIS |/* on reset,
                                                         the lock is released */
include/hw/pci/pcie_regs.h +2 -0
index 4d123d9..652d9fc 100644
@@ -57,6 +57,8 @@
 #define PCI_EXP_SLTCTL_PIC_SHIFT        (ffs(PCI_EXP_SLTCTL_PIC) - 1)
 #define PCI_EXP_SLTCTL_PIC_OFF                          \
     (PCI_EXP_SLTCTL_IND_OFF << PCI_EXP_SLTCTL_PIC_SHIFT)
+#define PCI_EXP_SLTCTL_PIC_ON                          \
+    (PCI_EXP_SLTCTL_IND_ON << PCI_EXP_SLTCTL_PIC_SHIFT)
 
 #define PCI_EXP_SLTCTL_SUPPORTED        \
             (PCI_EXP_SLTCTL_ABPE |      \
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] hw/pcie: implement power controller functionality

Il 19/06/2014 16:39, Michael S. Tsirkin ha scritto:

Quoted 8 lines
Quoted 6 lines
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |

+ PCI_EXP_SLTCTL_PCC |
PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |

Need to disable for compat types?

Do we support PCI-to-PCIe bridges? Otherwise, AHCI breaks migration and the only PCIe machine always has an AHCI controller, so I'd say no.

Paolo

#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] hw/pcie: implement power controller functionality

In reply to Michael S. Tsirkin
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Marcel Apfelbaum

It is needed by hot-unplug in order to get an indication from the OS when the device can be physically detached.

---
 hw/pci/pcie.c              | 15 ++++++++++++++-
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/pci/pcie.c +1 -0
index ae92f00..f8bf515 100644
@@ -292,16 +292,19 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
                                PCI_EXP_SLTCAP_HPC |
                                PCI_EXP_SLTCAP_PIP |
                                PCI_EXP_SLTCAP_AIP |
+                               PCI_EXP_SLTCAP_PCP |
                                PCI_EXP_SLTCAP_ABP);
 
Quoted 15 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 310

Quoted 13 lines
hw/pci/pcie.c +2 -0
     pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC);
     pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC_OFF |
                                PCI_EXP_SLTCTL_AIC_OFF);
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |
+                               PCI_EXP_SLTCTL_PCC |
                                PCI_EXP_SLTCTL_AIC |
                                PCI_EXP_SLTCTL_HPIE |
                                PCI_EXP_SLTCTL_CCIE |

Need to disable for compat types?

Does Paolo's explanation answer your question?

Quoted 10 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 333

Quoted 7 lines
hw/pci/pcie.c +1 -0
@@ -327,21 +330,31 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
 void pcie_cap_slot_reset(PCIDevice *dev)
 {
     uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
+    PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

What does this mean?
Downstream port?

Yes

Quoted 1 line

A switch can have multiple downstream ports at any slot #.

It doesn't matter how many devices are under this slot, we need only one to power up the slot. The question here was "Do we have at least one device attahc to slot? If yes, power up the slot."

Quoted 4 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 334

Quoted 2 lines
hw/pci/pcie.c +1 -0
+    int pic;

uint16_t please.

Sure,

Quoted 20 lines
Quoted 20 lines
hw/pci/pcie.c +4 -1
+
+    pic = slot_dev ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
 
     PCIE_DEV_PRINTF(dev, "reset\n");
 
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_EIC |
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC |
                                  PCI_EXP_SLTCTL_HPIE |
                                  PCI_EXP_SLTCTL_CCIE |
                                  PCI_EXP_SLTCTL_PDCE |
                                  PCI_EXP_SLTCTL_ABPE);
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
-                               PCI_EXP_SLTCTL_PIC_OFF |
+                               pic |
                                PCI_EXP_SLTCTL_AIC_OFF);
 
Quoted 6 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 355

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    if (!slot_dev) {
+        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
+                                   PCI_EXP_SLTCTL_PCC);

I dislike it when we clear bits then set them back. Please just add else here.

Sure,

Quoted 5 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 357

Quoted 3 lines
hw/pci/pcie.c +2 -0
+    }
+

Need to disable for compat types?

As above,

Thanks for the review,
Marcel

Quoted 19 lines
Quoted 19 lines
hw/pci/pcie.c +0 -0
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                  PCI_EXP_SLTSTA_EIS |/* on reset,
                                                         the lock is released */
include/hw/pci/pcie_regs.h +2 -0
index 4d123d9..652d9fc 100644
@@ -57,6 +57,8 @@
 #define PCI_EXP_SLTCTL_PIC_SHIFT        (ffs(PCI_EXP_SLTCTL_PIC) - 1)
 #define PCI_EXP_SLTCTL_PIC_OFF                          \
     (PCI_EXP_SLTCTL_IND_OFF << PCI_EXP_SLTCTL_PIC_SHIFT)
+#define PCI_EXP_SLTCTL_PIC_ON                          \
+    (PCI_EXP_SLTCTL_IND_ON << PCI_EXP_SLTCTL_PIC_SHIFT)
 
 #define PCI_EXP_SLTCTL_SUPPORTED        \
             (PCI_EXP_SLTCTL_ABPE |      \
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] hw/pcie: implement power controller functionality

Quoted 37 lines · Marcel Apfelbaum
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Marcel Apfelbaum

It is needed by hot-unplug in order to get an indication from the OS when the device can be physically detached.

---
 hw/pci/pcie.c              | 15 ++++++++++++++-
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/pci/pcie.c +1 -0
index ae92f00..f8bf515 100644
@@ -292,16 +292,19 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
                                PCI_EXP_SLTCAP_HPC |
                                PCI_EXP_SLTCAP_PIP |
                                PCI_EXP_SLTCAP_AIP |
+                               PCI_EXP_SLTCAP_PCP |
                                PCI_EXP_SLTCAP_ABP);
 
Quoted 15 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 310

Quoted 13 lines
hw/pci/pcie.c +2 -0
     pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC);
     pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC_OFF |
                                PCI_EXP_SLTCTL_AIC_OFF);
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |
+                               PCI_EXP_SLTCTL_PCC |
                                PCI_EXP_SLTCTL_AIC |
                                PCI_EXP_SLTCTL_HPIE |
                                PCI_EXP_SLTCTL_CCIE |

Need to disable for compat types?

Quoted 1 line

Does Paolo's explanation answer your question?

Kind of hacky - we do have compat work for
q35. So I'd prefer consistency.

Quoted 11 lines
Quoted 10 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 333

Quoted 7 lines
hw/pci/pcie.c +1 -0
@@ -327,21 +330,31 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
 void pcie_cap_slot_reset(PCIDevice *dev)
 {
     uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
+    PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

What does this mean?
Downstream port?

Yes

well it's not really clear, needs a comment.

Quoted 4 lines
Quoted 1 line

A switch can have multiple downstream ports at any slot #.

It doesn't matter how many devices are under this slot, we need only one to power up the slot. The question here was "Do we have at least one device attahc to slot? If yes, power up the slot."

You need a loop for that I think. There's no guarantee the device is at devfn=0.

Quoted 65 lines
Quoted 4 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 334

Quoted 2 lines
hw/pci/pcie.c +1 -0
+    int pic;

uint16_t please.

Sure,

Quoted 20 lines
Quoted 20 lines
hw/pci/pcie.c +4 -1
+
+    pic = slot_dev ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
 
     PCIE_DEV_PRINTF(dev, "reset\n");
 
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_EIC |
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC |
                                  PCI_EXP_SLTCTL_HPIE |
                                  PCI_EXP_SLTCTL_CCIE |
                                  PCI_EXP_SLTCTL_PDCE |
                                  PCI_EXP_SLTCTL_ABPE);
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
-                               PCI_EXP_SLTCTL_PIC_OFF |
+                               pic |
                                PCI_EXP_SLTCTL_AIC_OFF);
 
Quoted 6 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 355

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    if (!slot_dev) {
+        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
+                                   PCI_EXP_SLTCTL_PCC);

I dislike it when we clear bits then set them back. Please just add else here.

Sure,

Quoted 5 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 357

Quoted 3 lines
hw/pci/pcie.c +2 -0
+    }
+

Need to disable for compat types?

As above,

Thanks for the review,
Marcel

Quoted 19 lines
Quoted 19 lines
hw/pci/pcie.c +0 -0
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                  PCI_EXP_SLTSTA_EIS |/* on reset,
                                                         the lock is released */
include/hw/pci/pcie_regs.h +2 -0
index 4d123d9..652d9fc 100644
@@ -57,6 +57,8 @@
 #define PCI_EXP_SLTCTL_PIC_SHIFT        (ffs(PCI_EXP_SLTCTL_PIC) - 1)
 #define PCI_EXP_SLTCTL_PIC_OFF                          \
     (PCI_EXP_SLTCTL_IND_OFF << PCI_EXP_SLTCTL_PIC_SHIFT)
+#define PCI_EXP_SLTCTL_PIC_ON                          \
+    (PCI_EXP_SLTCTL_IND_ON << PCI_EXP_SLTCTL_PIC_SHIFT)
 
 #define PCI_EXP_SLTCTL_SUPPORTED        \
             (PCI_EXP_SLTCTL_ABPE |      \
Signature · 1 line
1.8.3.1
#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] hw/pcie: implement power controller functionality

Quoted 57 lines · Michael S. Tsirkin
Quoted 37 lines · Marcel Apfelbaum
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Marcel Apfelbaum

It is needed by hot-unplug in order to get an indication from the OS when the device can be physically detached.

---
 hw/pci/pcie.c              | 15 ++++++++++++++-
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/pci/pcie.c +1 -0
index ae92f00..f8bf515 100644
@@ -292,16 +292,19 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
                                PCI_EXP_SLTCAP_HPC |
                                PCI_EXP_SLTCAP_PIP |
                                PCI_EXP_SLTCAP_AIP |
+                               PCI_EXP_SLTCAP_PCP |
                                PCI_EXP_SLTCAP_ABP);
 
Quoted 15 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 310

Quoted 13 lines
hw/pci/pcie.c +2 -0
     pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC);
     pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC_OFF |
                                PCI_EXP_SLTCTL_AIC_OFF);
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |
+                               PCI_EXP_SLTCTL_PCC |
                                PCI_EXP_SLTCTL_AIC |
                                PCI_EXP_SLTCTL_HPIE |
                                PCI_EXP_SLTCTL_CCIE |

Need to disable for compat types?

Quoted 1 line

Does Paolo's explanation answer your question?

Kind of hacky - we do have compat work for
q35. So I'd prefer consistency.

Quoted 11 lines
Quoted 10 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 333

Quoted 7 lines
hw/pci/pcie.c +1 -0
@@ -327,21 +330,31 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
 void pcie_cap_slot_reset(PCIDevice *dev)
 {
     uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
+    PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

What does this mean?
Downstream port?

Yes

well it's not really clear, needs a comment.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

A switch can have multiple downstream ports at any slot #.

It doesn't matter how many devices are under this slot, we need only one to power up the slot. The question here was "Do we have at least one device attahc to slot? If yes, power up the slot."

You need a loop for that I think. There's no guarantee the device is at devfn=0.

Unless something guarantees this is a downstream port, if yes maybe add an assert?
slot_dev is also not a very good variable name. How about

	bool populated = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

?
This is all you care about ...
And maybe add a comment
/* Downstream ports enforce device number 0. */

Quoted 65 lines
Quoted 65 lines
Quoted 4 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 334

Quoted 2 lines
hw/pci/pcie.c +1 -0
+    int pic;

uint16_t please.

Sure,

Quoted 20 lines
Quoted 20 lines
hw/pci/pcie.c +4 -1
+
+    pic = slot_dev ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
 
     PCIE_DEV_PRINTF(dev, "reset\n");
 
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_EIC |
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC |
                                  PCI_EXP_SLTCTL_HPIE |
                                  PCI_EXP_SLTCTL_CCIE |
                                  PCI_EXP_SLTCTL_PDCE |
                                  PCI_EXP_SLTCTL_ABPE);
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
-                               PCI_EXP_SLTCTL_PIC_OFF |
+                               pic |
                                PCI_EXP_SLTCTL_AIC_OFF);
 
Quoted 6 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 355

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    if (!slot_dev) {
+        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
+                                   PCI_EXP_SLTCTL_PCC);

I dislike it when we clear bits then set them back. Please just add else here.

Sure,

Quoted 5 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 357

Quoted 3 lines
hw/pci/pcie.c +2 -0
+    }
+

Need to disable for compat types?

As above,

Thanks for the review,
Marcel

Quoted 19 lines
Quoted 19 lines
hw/pci/pcie.c +0 -0
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                  PCI_EXP_SLTSTA_EIS |/* on reset,
                                                         the lock is released */
include/hw/pci/pcie_regs.h +2 -0
index 4d123d9..652d9fc 100644
@@ -57,6 +57,8 @@
 #define PCI_EXP_SLTCTL_PIC_SHIFT        (ffs(PCI_EXP_SLTCTL_PIC) - 1)
 #define PCI_EXP_SLTCTL_PIC_OFF                          \
     (PCI_EXP_SLTCTL_IND_OFF << PCI_EXP_SLTCTL_PIC_SHIFT)
+#define PCI_EXP_SLTCTL_PIC_ON                          \
+    (PCI_EXP_SLTCTL_IND_ON << PCI_EXP_SLTCTL_PIC_SHIFT)
 
 #define PCI_EXP_SLTCTL_SUPPORTED        \
             (PCI_EXP_SLTCTL_ABPE |      \
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [Qemu-devel] [PATCH 2/3] hw/pcie: implement power controller functionality

Quoted 70 lines · Michael S. Tsirkin
Quoted 57 lines · Michael S. Tsirkin
Quoted 37 lines · Marcel Apfelbaum
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Marcel Apfelbaum

It is needed by hot-unplug in order to get an indication from the OS when the device can be physically detached.

---
 hw/pci/pcie.c              | 15 ++++++++++++++-
 include/hw/pci/pcie_regs.h |  2 ++
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/pci/pcie.c +1 -0
index ae92f00..f8bf515 100644
@@ -292,16 +292,19 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
                                PCI_EXP_SLTCAP_HPC |
                                PCI_EXP_SLTCAP_PIP |
                                PCI_EXP_SLTCAP_AIP |
+                               PCI_EXP_SLTCAP_PCP |
                                PCI_EXP_SLTCAP_ABP);
 
Quoted 15 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 310

Quoted 13 lines
hw/pci/pcie.c +2 -0
     pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC);
     pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC_OFF |
                                PCI_EXP_SLTCTL_AIC_OFF);
     pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
                                PCI_EXP_SLTCTL_PIC |
+                               PCI_EXP_SLTCTL_PCC |
                                PCI_EXP_SLTCTL_AIC |
                                PCI_EXP_SLTCTL_HPIE |
                                PCI_EXP_SLTCTL_CCIE |

Need to disable for compat types?

Quoted 1 line

Does Paolo's explanation answer your question?

Kind of hacky - we do have compat work for
q35. So I'd prefer consistency.

Quoted 11 lines
Quoted 10 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 333

Quoted 7 lines
hw/pci/pcie.c +1 -0
@@ -327,21 +330,31 @@ void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
 void pcie_cap_slot_reset(PCIDevice *dev)
 {
     uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
+    PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

What does this mean?
Downstream port?

Yes

well it's not really clear, needs a comment.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

A switch can have multiple downstream ports at any slot #.

It doesn't matter how many devices are under this slot, we need only one to power up the slot. The question here was "Do we have at least one device attahc to slot? If yes, power up the slot."

You need a loop for that I think. There's no guarantee the device is at devfn=0.

Unless something guarantees this is a downstream port, if yes maybe add an assert?
slot_dev is also not a very good variable name. How about

Quoted 5 lines
	bool populated = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];

?
This is all you care about ...
And maybe add a comment
/* Downstream ports enforce device number 0. */

I'll use that, thanks!
Marcel

Quoted 66 lines
Quoted 66 lines
Quoted 66 lines
Quoted 5 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 334

Quoted 3 lines
hw/pci/pcie.c +1 -0
+    int pic;

uint16_t please.

Sure,

Quoted 20 lines
Quoted 20 lines
hw/pci/pcie.c +4 -1
+
+    pic = slot_dev ? PCI_EXP_SLTCTL_PIC_ON : PCI_EXP_SLTCTL_PIC_OFF;
 
     PCIE_DEV_PRINTF(dev, "reset\n");
 
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
                                  PCI_EXP_SLTCTL_EIC |
                                  PCI_EXP_SLTCTL_PIC |
+                                 PCI_EXP_SLTCTL_PCC |
                                  PCI_EXP_SLTCTL_AIC |
                                  PCI_EXP_SLTCTL_HPIE |
                                  PCI_EXP_SLTCTL_CCIE |
                                  PCI_EXP_SLTCTL_PDCE |
                                  PCI_EXP_SLTCTL_ABPE);
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
-                               PCI_EXP_SLTCTL_PIC_OFF |
+                               pic |
                                PCI_EXP_SLTCTL_AIC_OFF);
 
Quoted 6 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 355

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    if (!slot_dev) {
+        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
+                                   PCI_EXP_SLTCTL_PCC);

I dislike it when we clear bits then set them back. Please just add else here.

Sure,

Quoted 5 lines

hw/pci/pcie.c void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot) · line 357

Quoted 3 lines
hw/pci/pcie.c +2 -0
+    }
+

Need to disable for compat types?

As above,

Thanks for the review,
Marcel

Quoted 19 lines
Quoted 19 lines
hw/pci/pcie.c +0 -0
     pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
                                  PCI_EXP_SLTSTA_EIS |/* on reset,
                                                         the lock is released */
include/hw/pci/pcie_regs.h +2 -0
index 4d123d9..652d9fc 100644
@@ -57,6 +57,8 @@
 #define PCI_EXP_SLTCTL_PIC_SHIFT        (ffs(PCI_EXP_SLTCTL_PIC) - 1)
 #define PCI_EXP_SLTCTL_PIC_OFF                          \
     (PCI_EXP_SLTCTL_IND_OFF << PCI_EXP_SLTCTL_PIC_SHIFT)
+#define PCI_EXP_SLTCTL_PIC_ON                          \
+    (PCI_EXP_SLTCTL_IND_ON << PCI_EXP_SLTCTL_PIC_SHIFT)
 
 #define PCI_EXP_SLTCTL_SUPPORTED        \
             (PCI_EXP_SLTCTL_ABPE |      \
Signature · 1 line
1.8.3.1
#9 lore raw

PATCH 3/3 hw/pcie: better hotplug/hotunplug support

In reply to Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +15 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
 /* pci express slot for pci express root/downstream port
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1
#10 lore raw
Quoted 7 lines · Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

Pls not here that current code is broken: it does surprise removal which crashes guests.

Quoted 32 lines
---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +3 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
 /* pci express slot for pci express root/downstream port

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 389

Quoted 3 lines
hw/pci/pcie.c +0 -0
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 

pls add code comments explaining the logic here.

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 393

Quoted 4 lines
hw/pci/pcie.c +4 -0
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {

Here you want to remove all devices behind the bridge? You need to do this for all functions, not just function 0.

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 398

Quoted 5 lines
hw/pci/pcie.c +5 -0
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);

These bits need to be cleared in any case?

Quoted 8 lines
hw/pci/pcie.c +3 -0
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1
#11 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

Pls not here that current code is broken: it does surprise removal which crashes guests.

I'll add a note, sure.

Quoted 31 lines
Quoted 31 lines
---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +3 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 389

Quoted 5 lines
hw/pci/pcie.c +0 -0
 /* pci express slot for pci express root/downstream port
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 

pls add code comments explaining the logic here.

I thought is clear :(
Basically: If the device is present, power indicator off and power controller off, it is safe to detach the device.

Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 393

Quoted 5 lines
hw/pci/pcie.c +4 -0
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {

Here you want to remove all devices behind the bridge?

Yes, but since it is PCIe we only have one device, but we may have a multi-function device...

Quoted 1 line

You need to do this for all functions, not just function 0.

So bus devices are actually functions... OK, I'll run a loop here.

Quoted 8 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 398

Quoted 6 lines
hw/pci/pcie.c +5 -0
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);

These bits need to be cleared in any case?

No, "PDS on" means the devices is present, so we clear it only after the OS powers it off.

Quoted 9 lines
Quoted 9 lines
hw/pci/pcie.c +3 -0
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1
#12 lore raw
Quoted 52 lines · Marcel Apfelbaum
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

Pls not here that current code is broken: it does surprise removal which crashes guests.

I'll add a note, sure.

Quoted 31 lines
Quoted 31 lines
---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +3 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 389

Quoted 5 lines
hw/pci/pcie.c +0 -0
 /* pci express slot for pci express root/downstream port
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 

pls add code comments explaining the logic here.

Quoted 3 lines

I thought is clear :(
Basically: If the device is present, power indicator off and power controller off, it is safe to detach the device.

Sure, put this in the comment.

Quoted 9 lines
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 393

Quoted 5 lines
hw/pci/pcie.c +4 -0
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {

Here you want to remove all devices behind the bridge?

Yes, but since it is PCIe we only have one device, but we may have a multi-function device...

Exactly. Up to 256 functions.

Quoted 10 lines
Quoted 1 line

You need to do this for all functions, not just function 0.

So bus devices are actually functions... OK, I'll run a loop here.

Quoted 7 lines
Quoted 6 lines
hw/pci/pcie.c +5 -0
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);
Quoted 3 lines
Quoted 1 line

These bits need to be cleared in any case?

No, "PDS on" means the devices is present, so we clear it only after the OS powers it off.

So why not clear PDS unconditionally?

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
hw/pci/pcie.c +3 -0
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1
#13 lore raw
Quoted 81 lines · Michael S. Tsirkin
Quoted 52 lines · Marcel Apfelbaum
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

Pls not here that current code is broken: it does surprise removal which crashes guests.

I'll add a note, sure.

Quoted 31 lines
Quoted 31 lines
---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +3 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 389

Quoted 5 lines
hw/pci/pcie.c +0 -0
 /* pci express slot for pci express root/downstream port
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 

pls add code comments explaining the logic here.

Quoted 3 lines

I thought is clear :(
Basically: If the device is present, power indicator off and power controller off, it is safe to detach the device.

Sure, put this in the comment.

Quoted 9 lines
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 393

Quoted 5 lines
hw/pci/pcie.c +4 -0
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {

Here you want to remove all devices behind the bridge?

Yes, but since it is PCIe we only have one device, but we may have a multi-function device...

Exactly. Up to 256 functions.

Quoted 10 lines
Quoted 1 line

You need to do this for all functions, not just function 0.

So bus devices are actually functions... OK, I'll run a loop here.

Quoted 7 lines
Quoted 6 lines
hw/pci/pcie.c +5 -0
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

These bits need to be cleared in any case?

No, "PDS on" means the devices is present, so we clear it only after the OS powers it off.

So why not clear PDS unconditionally?

Because we are 'allowed' to remove the device only when the OS tells us the power is off. Before that, it can result in data loss, since the device driver is not yet unloaded.

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
hw/pci/pcie.c +3 -0
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1
#14 lore raw
Quoted 82 lines · Marcel Apfelbaum
Quoted 81 lines · Michael S. Tsirkin
Quoted 52 lines · Marcel Apfelbaum
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

Pls not here that current code is broken: it does surprise removal which crashes guests.

I'll add a note, sure.

Quoted 31 lines
Quoted 31 lines
---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +3 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 389

Quoted 5 lines
hw/pci/pcie.c +0 -0
 /* pci express slot for pci express root/downstream port
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 

pls add code comments explaining the logic here.

Quoted 3 lines

I thought is clear :(
Basically: If the device is present, power indicator off and power controller off, it is safe to detach the device.

Sure, put this in the comment.

Quoted 9 lines
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 393

Quoted 5 lines
hw/pci/pcie.c +4 -0
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {

Here you want to remove all devices behind the bridge?

Yes, but since it is PCIe we only have one device, but we may have a multi-function device...

Exactly. Up to 256 functions.

Quoted 10 lines
Quoted 1 line

You need to do this for all functions, not just function 0.

So bus devices are actually functions... OK, I'll run a loop here.

Quoted 7 lines
Quoted 6 lines
hw/pci/pcie.c +5 -0
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

These bits need to be cleared in any case?

No, "PDS on" means the devices is present, so we clear it only after the OS powers it off.

So why not clear PDS unconditionally?

Because we are 'allowed' to remove the device only when the OS tells us the power is off. Before that, it can result in data loss, since the device driver is not yet unloaded.

Yes. But if (slot_dev) seems not to be needed:
if slot is empty we can clear PDC.

Quoted 14 lines
Quoted 12 lines
Quoted 12 lines
Quoted 10 lines
Quoted 10 lines
hw/pci/pcie.c +3 -0
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1
#15 lore raw
Quoted 87 lines · Michael S. Tsirkin
Quoted 86 lines · Marcel Apfelbaum
Quoted 85 lines · Michael S. Tsirkin
Quoted 52 lines · Marcel Apfelbaum
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Marcel Apfelbaum

Hotplug triggers both 'present detect change' and
'attention button pressed'.

Hotunplug starts by triggering 'attention button pressed', then waits for the OS to power off the device and only then detaches it.

Pls not here that current code is broken: it does surprise removal which crashes guests.

I'll add a note, sure.

Quoted 31 lines
Quoted 31 lines
---
 hw/pci/pcie.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/pci/pcie.c +3 -5
index f8bf515..9cfd93d 100644
@@ -258,7 +258,8 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev),
+                        PCI_EXP_HP_EV_PDC | PCI_EXP_HP_EV_ABP);
 }
 
 void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
@@ -268,10 +269,7 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
 
     pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
 
-    object_unparent(OBJECT(dev));
-    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
-                                 PCI_EXP_SLTSTA_PDS);
-    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
+    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
 }
 
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 389

Quoted 5 lines
hw/pci/pcie.c +0 -0
 /* pci express slot for pci express root/downstream port
@@ -389,6 +387,18 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
                         sltsta);
     }
 

pls add code comments explaining the logic here.

Quoted 3 lines

I thought is clear :(
Basically: If the device is present, power indicator off and power controller off, it is safe to detach the device.

Sure, put this in the comment.

Quoted 9 lines
Quoted 7 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 393

Quoted 5 lines
hw/pci/pcie.c +4 -0
+    if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
+        ((val & PCI_EXP_SLTCTL_PIC_OFF) == PCI_EXP_SLTCTL_PIC_OFF)) {
+        PCIDevice *slot_dev = pci_bridge_get_sec_bus(PCI_BRIDGE(dev))->devices[0];
+        if (slot_dev) {

Here you want to remove all devices behind the bridge?

Yes, but since it is PCIe we only have one device, but we may have a multi-function device...

Exactly. Up to 256 functions.

Quoted 13 lines
Quoted 1 line

You need to do this for all functions, not just function 0.

So bus devices are actually functions... OK, I'll run a loop here.

Quoted 8 lines

hw/pci/pcie.c void pcie_cap_slot_write_config(PCIDevice *dev, · line 398

Quoted 6 lines
hw/pci/pcie.c +5 -0
+            object_unparent(OBJECT(slot_dev));
+            pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
+                                         PCI_EXP_SLTSTA_PDS);
+            pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
+                                       PCI_EXP_SLTSTA_PDC);

These bits need to be cleared in any case?

No, "PDS on" means the devices is present, so we clear it only after the OS powers it off.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

So why not clear PDS unconditionally?

Because we are 'allowed' to remove the device only when the OS tells us the power is off. Before that, it can result in data loss, since the device driver is not yet unloaded.

Yes. But if (slot_dev) seems not to be needed:
if slot is empty we can clear PDC.

You mean PDS, PDC is the event we send to the OS.
The main reason I added this "if clause" is to protect against multiple OS events of 'power controller off' and 'power indicator off', but now I see that indeed I don't need that because I check "sltsta & PCI_EXP_SLTSTA_PDS". I will remove that if.

Thanks,
Marcel

Quoted 17 lines
Quoted 17 lines
Quoted 15 lines
Quoted 15 lines
Quoted 13 lines
Quoted 13 lines
hw/pci/pcie.c +3 -0
+        }
+    }
+
     hotplug_event_notify(dev);
 
     /* 
Signature · 1 line
1.8.3.1