Skip to contentSkip to conversation

hw/usb: hcd-xhci-pci: Raise MSI/MSI-X interrupts only when told to

PATCH In review Bin Meng 2 participants 7 messages May 21, 2021 – May 27, 2021

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/2 hw/usb: hcd-xhci-pci: Raise MSI/MSI-X interrupts only when told to

From: Ruimei Yan <[email protected]>

At present MSI / MSI-X interrupts are triggered regardless of the irq level. We should have checked the level to determine whether the interrupt needs to be delivered.

The level check logic was present in early versions of the xhci model, but got dropped later by a rework of interrupt handling under commit 4c4abe7cc903 ("xhci: rework interrupt handling").

Fixes 4c4abe7cc903 ("xhci: rework interrupt handling")

---

 hw/usb/hcd-xhci-pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/usb/hcd-xhci-pci.c +3 -2
index 9421734d0f..b6acd1790c 100644
@@ -67,12 +67,13 @@ static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
          msi_enabled(pci_dev))) {
         pci_set_irq(pci_dev, level);
     }
-    if (msix_enabled(pci_dev)) {
+
+    if (msix_enabled(pci_dev) && level) {
         msix_notify(pci_dev, n);
         return;
     }
 
-    if (msi_enabled(pci_dev)) {
+    if (msi_enabled(pci_dev) && level) {
         msi_notify(pci_dev, n);
         return;
     }
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 hw/usb: hcd-xhci-pci: Fix spec violation of IP flag for MSI/MSI-X

From: Ruimei Yan <[email protected]>

Per xHCI spec v1.2 chapter 4.17.5 page 296:

If MSI or MSI-X interrupts are enabled, Interrupt Pending (IP) shall be cleared automatically when the PCI dword write generated by the interrupt assertion is complete.

Currently QEMU does not clear the IP flag in the MSI / MSI-X mode. This causes subsequent spurious interrupt to be delivered to guests. To solve this, we change the xhci intr_raise() hook routine to have a bool return value that is passed to its caller (the xhci core), with true indicating that IP should be self-cleared.

Fixes 62c6ae04cf43 ("xhci: Initial xHCI implementation")
Fixes 4c47f800631a ("xhci: add msix support")
[bmeng: move IP clear codes from xhci pci to xhci core]

---

 hw/usb/hcd-xhci.h        | 2 +-
 hw/usb/hcd-xhci-pci.c    | 8 +++++---
 hw/usb/hcd-xhci-sysbus.c | 4 +++-
 hw/usb/hcd-xhci.c        | 8 ++++++--
 4 files changed, 15 insertions(+), 7 deletions(-)
hw/usb/hcd-xhci.h +1 -1
index 7bba361f3b..98f598382a 100644
@@ -194,7 +194,7 @@ typedef struct XHCIState {
     uint32_t flags;
     uint32_t max_pstreams_mask;
     void (*intr_update)(XHCIState *s, int n, bool enable);
-    void (*intr_raise)(XHCIState *s, int n, bool level);
+    bool (*intr_raise)(XHCIState *s, int n, bool level);
     DeviceState *hostOpaque;
 
     /* Operational Registers */
hw/usb/hcd-xhci-pci.c +5 -3
index b6acd1790c..e934b1a5b1 100644
@@ -57,7 +57,7 @@ static void xhci_pci_intr_update(XHCIState *xhci, int n, bool enable)
     }
 }
 
-static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
+static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 {
     XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
     PCIDevice *pci_dev = PCI_DEVICE(s);
@@ -70,13 +70,15 @@ static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 
     if (msix_enabled(pci_dev) && level) {
         msix_notify(pci_dev, n);
-        return;
+        return true;
     }
 
     if (msi_enabled(pci_dev) && level) {
         msi_notify(pci_dev, n);
-        return;
+        return true;
     }
+
+    return false;
 }
 
 static void xhci_pci_reset(DeviceState *dev)
hw/usb/hcd-xhci-sysbus.c +3 -1
index 42e2574c82..a14e438196 100644
@@ -16,11 +16,13 @@
 #include "hw/acpi/aml-build.h"
 #include "hw/irq.h"
 
-static void xhci_sysbus_intr_raise(XHCIState *xhci, int n, bool level)
+static bool xhci_sysbus_intr_raise(XHCIState *xhci, int n, bool level)
 {
     XHCISysbusState *s = container_of(xhci, XHCISysbusState, xhci);
 
     qemu_set_irq(s->irq[n], level);
+
+    return false;
 }
 
 void xhci_sysbus_reset(DeviceState *dev)
hw/usb/hcd-xhci.c +6 -2
index 46212b1e69..e01700039b 100644
@@ -551,7 +551,9 @@ static void xhci_intr_update(XHCIState *xhci, int v)
             level = 1;
         }
         if (xhci->intr_raise) {
-            xhci->intr_raise(xhci, 0, level);
+            if (xhci->intr_raise(xhci, 0, level)) {
+                xhci->intr[0].iman &= ~IMAN_IP;
+            }
         }
     }
     if (xhci->intr_update) {
@@ -579,7 +581,9 @@ static void xhci_intr_raise(XHCIState *xhci, int v)
         return;
     }
     if (xhci->intr_raise) {
-        xhci->intr_raise(xhci, v, true);
+        if (xhci->intr_raise(xhci, v, true)) {
+            xhci->intr[v].iman &= ~IMAN_IP;
+        }
     }
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] hw/usb: hcd-xhci-pci: Fix spec violation of IP flag for MSI/MSI-X

Quoted 59 lines · Bin Meng

From: Ruimei Yan <[email protected]>

Per xHCI spec v1.2 chapter 4.17.5 page 296:

If MSI or MSI-X interrupts are enabled, Interrupt Pending (IP) shall be cleared automatically when the PCI dword write generated by the interrupt assertion is complete.

Currently QEMU does not clear the IP flag in the MSI / MSI-X mode. This causes subsequent spurious interrupt to be delivered to guests. To solve this, we change the xhci intr_raise() hook routine to have a bool return value that is passed to its caller (the xhci core), with true indicating that IP should be self-cleared.

Fixes 62c6ae04cf43 ("xhci: Initial xHCI implementation")
Fixes 4c47f800631a ("xhci: add msix support")
[bmeng: move IP clear codes from xhci pci to xhci core]

---

 hw/usb/hcd-xhci.h        | 2 +-
 hw/usb/hcd-xhci-pci.c    | 8 +++++---
 hw/usb/hcd-xhci-sysbus.c | 4 +++-
 hw/usb/hcd-xhci.c        | 8 ++++++--
 4 files changed, 15 insertions(+), 7 deletions(-)
hw/usb/hcd-xhci.h +1 -1
index 7bba361f3b..98f598382a 100644
@@ -194,7 +194,7 @@ typedef struct XHCIState {
     uint32_t flags;
     uint32_t max_pstreams_mask;
     void (*intr_update)(XHCIState *s, int n, bool enable);
-    void (*intr_raise)(XHCIState *s, int n, bool level);
+    bool (*intr_raise)(XHCIState *s, int n, bool level);
     DeviceState *hostOpaque;
 
     /* Operational Registers */
hw/usb/hcd-xhci-pci.c +2 -2
index b6acd1790c..e934b1a5b1 100644
@@ -57,7 +57,7 @@ static void xhci_pci_intr_update(XHCIState *xhci, int n, bool enable)
     }
 }
 
-static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
+static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 {
     XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
     PCIDevice *pci_dev = PCI_DEVICE(s);
@@ -70,13 +70,15 @@ static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 
     if (msix_enabled(pci_dev) && level) {
         msix_notify(pci_dev, n);
-        return;
+        return true;

hw/usb/hcd-xhci-pci.c static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level) · line 82

Quoted 10 lines
hw/usb/hcd-xhci-pci.c +3 -1
     }
 
     if (msi_enabled(pci_dev) && level) {
         msi_notify(pci_dev, n);
-        return;
+        return true;
     }
+
+    return false;
 }

Could be simplified as:

  if (!level) {
    return false;
  }
  if (msix_enabled(pci_dev)) {
    msix_notify(pci_dev, n);
  }
  if (msi_enabled(pci_dev)) {
    msi_notify(pci_dev, n);
  }
  return true;

Otherwise,

author
#3 lore raw

Re: [PATCH 2/2] hw/usb: hcd-xhci-pci: Fix spec violation of IP flag for MSI/MSI-X

Quoted 74 lines · Philippe Mathieu-Daudé
Quoted 59 lines · Bin Meng

From: Ruimei Yan <[email protected]>

Per xHCI spec v1.2 chapter 4.17.5 page 296:

If MSI or MSI-X interrupts are enabled, Interrupt Pending (IP) shall be cleared automatically when the PCI dword write generated by the interrupt assertion is complete.

Currently QEMU does not clear the IP flag in the MSI / MSI-X mode. This causes subsequent spurious interrupt to be delivered to guests. To solve this, we change the xhci intr_raise() hook routine to have a bool return value that is passed to its caller (the xhci core), with true indicating that IP should be self-cleared.

Fixes 62c6ae04cf43 ("xhci: Initial xHCI implementation")
Fixes 4c47f800631a ("xhci: add msix support")
[bmeng: move IP clear codes from xhci pci to xhci core]

---

 hw/usb/hcd-xhci.h        | 2 +-
 hw/usb/hcd-xhci-pci.c    | 8 +++++---
 hw/usb/hcd-xhci-sysbus.c | 4 +++-
 hw/usb/hcd-xhci.c        | 8 ++++++--
 4 files changed, 15 insertions(+), 7 deletions(-)
hw/usb/hcd-xhci.h +1 -1
index 7bba361f3b..98f598382a 100644
@@ -194,7 +194,7 @@ typedef struct XHCIState {
     uint32_t flags;
     uint32_t max_pstreams_mask;
     void (*intr_update)(XHCIState *s, int n, bool enable);
-    void (*intr_raise)(XHCIState *s, int n, bool level);
+    bool (*intr_raise)(XHCIState *s, int n, bool level);
     DeviceState *hostOpaque;
 
     /* Operational Registers */
hw/usb/hcd-xhci-pci.c +2 -2
index b6acd1790c..e934b1a5b1 100644
@@ -57,7 +57,7 @@ static void xhci_pci_intr_update(XHCIState *xhci, int n, bool enable)
     }
 }
 
-static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
+static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 {
     XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
     PCIDevice *pci_dev = PCI_DEVICE(s);
@@ -70,13 +70,15 @@ static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 
     if (msix_enabled(pci_dev) && level) {
         msix_notify(pci_dev, n);
-        return;
+        return true;

hw/usb/hcd-xhci-pci.c static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level) · line 82

Quoted 10 lines
hw/usb/hcd-xhci-pci.c +3 -1
     }
 
     if (msi_enabled(pci_dev) && level) {
         msi_notify(pci_dev, n);
-        return;
+        return true;
     }
+
+    return false;
 }

Could be simplified as:

Quoted 10 lines
  if (!level) {
    return false;
  }
  if (msix_enabled(pci_dev)) {
    msix_notify(pci_dev, n);
  }
  if (msi_enabled(pci_dev)) {
    msi_notify(pci_dev, n);
  }
  return true;

The simplified logic will deliver both interrupts if both msix and msi are enabled. The previous logic prevents such from happening.

Quoted 4 lines

Otherwise,

Regards,
Bin

#4 lore raw

Re: [PATCH 2/2] hw/usb: hcd-xhci-pci: Fix spec violation of IP flag for MSI/MSI-X

Quoted 87 lines · Bin Meng
Quoted 74 lines · Philippe Mathieu-Daudé
Quoted 59 lines · Bin Meng

From: Ruimei Yan <[email protected]>

Per xHCI spec v1.2 chapter 4.17.5 page 296:

If MSI or MSI-X interrupts are enabled, Interrupt Pending (IP) shall be cleared automatically when the PCI dword write generated by the interrupt assertion is complete.

Currently QEMU does not clear the IP flag in the MSI / MSI-X mode. This causes subsequent spurious interrupt to be delivered to guests. To solve this, we change the xhci intr_raise() hook routine to have a bool return value that is passed to its caller (the xhci core), with true indicating that IP should be self-cleared.

Fixes 62c6ae04cf43 ("xhci: Initial xHCI implementation")
Fixes 4c47f800631a ("xhci: add msix support")
[bmeng: move IP clear codes from xhci pci to xhci core]

---

 hw/usb/hcd-xhci.h        | 2 +-
 hw/usb/hcd-xhci-pci.c    | 8 +++++---
 hw/usb/hcd-xhci-sysbus.c | 4 +++-
 hw/usb/hcd-xhci.c        | 8 ++++++--
 4 files changed, 15 insertions(+), 7 deletions(-)
hw/usb/hcd-xhci.h +1 -1
index 7bba361f3b..98f598382a 100644
@@ -194,7 +194,7 @@ typedef struct XHCIState {
     uint32_t flags;
     uint32_t max_pstreams_mask;
     void (*intr_update)(XHCIState *s, int n, bool enable);
-    void (*intr_raise)(XHCIState *s, int n, bool level);
+    bool (*intr_raise)(XHCIState *s, int n, bool level);
     DeviceState *hostOpaque;
 
     /* Operational Registers */
hw/usb/hcd-xhci-pci.c +2 -2
index b6acd1790c..e934b1a5b1 100644
@@ -57,7 +57,7 @@ static void xhci_pci_intr_update(XHCIState *xhci, int n, bool enable)
     }
 }
 
-static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
+static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 {
     XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
     PCIDevice *pci_dev = PCI_DEVICE(s);
@@ -70,13 +70,15 @@ static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
 
     if (msix_enabled(pci_dev) && level) {
         msix_notify(pci_dev, n);
-        return;
+        return true;

hw/usb/hcd-xhci-pci.c static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level) · line 82

Quoted 10 lines
hw/usb/hcd-xhci-pci.c +3 -1
     }
 
     if (msi_enabled(pci_dev) && level) {
         msi_notify(pci_dev, n);
-        return;
+        return true;
     }
+
+    return false;
 }

Could be simplified as:

Quoted 10 lines
  if (!level) {
    return false;
  }
  if (msix_enabled(pci_dev)) {
    msix_notify(pci_dev, n);
  }
  if (msi_enabled(pci_dev)) {
    msi_notify(pci_dev, n);
  }
  return true;
Quoted 2 lines

The simplified logic will deliver both interrupts if both msix and msi are enabled. The previous logic prevents such from happening.

Oops you are right :)

Quoted 2 lines
Quoted 2 lines

Otherwise,

This stands.

Reviewed-by on v1 1/2
#5 lore raw
In reply to Bin Meng
Quoted 23 lines · Bin Meng

From: Ruimei Yan <[email protected]>

At present MSI / MSI-X interrupts are triggered regardless of the irq level. We should have checked the level to determine whether the interrupt needs to be delivered.

The level check logic was present in early versions of the xhci model, but got dropped later by a rework of interrupt handling under commit 4c4abe7cc903 ("xhci: rework interrupt handling").

Fixes 4c4abe7cc903 ("xhci: rework interrupt handling")

---

 hw/usb/hcd-xhci-pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/usb/hcd-xhci-pci.c +0 -0
index 9421734d0f..b6acd1790c 100644

hw/usb/hcd-xhci-pci.c static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level) · line 69

Quoted 3 lines
hw/usb/hcd-xhci-pci.c +0 -0
@@ -67,12 +67,13 @@ static void xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
          msi_enabled(pci_dev))) {
         pci_set_irq(pci_dev, level);
     }

I read the next patch before this one :)

So please consider:

if (!level) {
return;
}

Otherwise:

Quoted 13 lines
hw/usb/hcd-xhci-pci.c +3 -2
-    if (msix_enabled(pci_dev)) {
+
+    if (msix_enabled(pci_dev) && level) {
         msix_notify(pci_dev, n);
         return;
     }
 
-    if (msi_enabled(pci_dev)) {
+    if (msi_enabled(pci_dev) && level) {
         msi_notify(pci_dev, n);
         return;
     }
author
#6 lore raw
In reply to Bin Meng
Quoted 11 lines · Bin Meng

From: Ruimei Yan <[email protected]>

At present MSI / MSI-X interrupts are triggered regardless of the irq level. We should have checked the level to determine whether the interrupt needs to be delivered.

The level check logic was present in early versions of the xhci model, but got dropped later by a rework of interrupt handling under commit 4c4abe7cc903 ("xhci: rework interrupt handling").

Quoted 8 lines
Fixes 4c4abe7cc903 ("xhci: rework interrupt handling")

---

 hw/usb/hcd-xhci-pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Ping?