Skip to contentSkip to conversation

hw/usb/hcd-xhci: Fixes, improvements, and macOS workaround

PATCH Superseded Phil Dennis-Jordan 2 participants 13 messages Dec 27, 2024 – Jan 9, 2025

v3 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v3 cover hw/usb/hcd-xhci: Fixes, improvements, and macOS workaround

For a while now, I've been chasing the problem of macOS's XHCI guest driver not working properly with QEMU's PCI XHCI controller when MSI-X is unavailable. I've finally figured out the cause, and I think an acceptable solution. I've explained the problem and quoted the relevant sections of the XHCI spec in more detail in the linked GitLab issue:

https://gitlab.com/qemu-project/qemu/-/issues/2705

Essentially, the macOS driver attempts to use XHCI event rings 1 and 2 even when there is only a single pin-based interrupt available. The interrupts for rings 1 and 2 are dropped, and so events are only handled after a timeout. The driver appears to expect the device to act as if interrupter mapping was not supported - the spec only mentions that interrupter mapping should be disabled if only one **interrupter** is enabled, not one **interrupt**, although there is some ambiguity in the spec's wording around enabling and disabling interrupters.

After feedback to my initial RFC submission and discovering some more unhandled edge cases, I've now split this up into 6 different patches.

Ultimately, for macOS to be able to drive the XHCI controller with MSI(-X) disabled, we need to disable interrupter mapping (in the sense of XHCI spec section 4.17.1) when using a pin-based interrupt.

1. Fixes an assertion failure crash when XHCI attempts to raise an interrupt on a MSI vector higher than the maximum allocated. It turns out the spec says the MSI vector needs to be computed from the interrupter number by modulo arithmetic, so this patch implements that behaviour.
2. Moves the msi/msix toggles from the NEC XHCI controller to the generic hci-xhci-pci superclass for consistency. This makes testing with MSI-X and/or MSI disabled easier when using the qemu-xhci device variant.
3. Implements interrupter mapping disabling as per XHCI spec, when numintrs==1.
4. Switches from tracing to LOG_UNIMP and LOG_GUEST_ERROR for certain cases of unhandled MMIO. (This is not comprehensive, but came up in review of one of Nicholas Piggin's XHCI patch sets.)
5. Add a new boolean property to hcd-xhci-pci, "conditional-intr-mapping", which defaults to false, retaining existing behaviour. When set to true, additional constraints for enabling interrupter mapping are enabled, so it is disabled in pin-based mode. This works around the macOS guest driver quirks.
6. Enables the "conditional-intr-mapping" property in the VMApple machine type, which does not support MSI-X or MSI and has previously suffered from the macOS guest driver quirk.

Of course, patch 6 can only be applied once the VMApple patch set is also
applied: https://patchew.org/QEMU/[email protected]/

Patches 2 and 4 are optional for the purposes of fixing the issue I set out to fix, but seem sensible enough to include in this series.

You can reproduce the problems being fixed as follows:

* Assertion failure crash fixed in patch 1: Use a x86-64 VM with macOS guest and machine type Q35. For USB, use: -device nec-usb-xhci,msix=off QEMU will crash with a failed assertion, "vector < nr_vectors" on guest boot.
* macOS guest not driving the XHCI controller correctly with pin-based
interrupts: Either as above but with -device nec-usb-xhci,msix=off,msi=off or by following the instructions to boot aarch64 macOS 12 on the VMApple machine type.

History:

RFC -> v1:

* Gated conditional interrupter mapping support behind a property, enabled that property in the VMApple machine type.
* Added patch to fix the MSI vector assertion failure.
* Moved msi and msix properties from NEC XHCI controller to generic xhci-pci superclass as that also seems useful.
* Broke the workaround up into 2 patches, one for mapping disabling required by the standard, and one for the conditional disabling workaround.

v1 -> v2:

* 1/6: Switch to modulo arithmetic for MSI vector number, as per spec.
* 6/6: Set the "conditional-intr-mapping" property via compat_props.
* Commit message tweaks

v2 -> v3:

* 2/6: In line with recent upstream changes, the property table is now const and no longer carries an end-of-list marker.
* The indentation fix (previously 5/6) has already been merged, so is no longer included.
* Added patch fixing up logging of certain unhandled MMIO cases. (4/6)
* 6/6: Moved the compat global property table into vmapple patch set -v16; we now just add the conditional-intr-mapping property to it in this patch. We also set the property on any device implementing the abstract TYPE_XHCI_PCI rather than only the TYPE_QEMU_XHCI device specifically.

Phil Dennis-Jordan (6):
hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec hw/usb/hcd-xhci-pci: Move msi/msix properties from NEC to superclass hw/usb/hcd-xhci-pci: Use event ring 0 if mapping unsupported hw/usb/hcd-xhci: Unimplemented/guest error logging for port MMIO hw/usb/hcd-xhci-pci: Adds property for disabling mapping in IRQ mode hw/vmapple: XHCI controller's interrupt mapping workaround for macOS

 hw/usb/hcd-xhci-nec.c |  2 --
 hw/usb/hcd-xhci-pci.c | 31 +++++++++++++++++++++++++++++++
 hw/usb/hcd-xhci-pci.h |  1 +
 hw/usb/hcd-xhci.c     | 30 +++++++++++++++++++++++++++---
 hw/usb/hcd-xhci.h     |  5 +++++
 hw/vmapple/vmapple.c  |  8 +++++++-
 6 files changed, 71 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH v3 1/6 hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec

QEMU would crash with a failed assertion if the XHCI controller attempted to raise the interrupt on a higher vector than the highest configured for the device by the guest driver.

It turns out the XHCI spec (Implementation Note in section 4.17, "Interrupters") requires that the host controller signal the MSI vector with the number computed by taking the interrupter number modulo the number of enabled MSI vectors.

This change introduces that modulo calculation, fixing the failed assertion and making the device work correctly in MSI mode with macOS's XHCI driver.

---

v2:

* Switch to modulo arithmetic for MSI vector number rather than dropping, as per spec.

 hw/usb/hcd-xhci-pci.c | 1 +
 1 file changed, 1 insertion(+)
hw/usb/hcd-xhci-pci.c +1 -0
index e110840c7a..e5e7330387 100644
@@ -74,6 +74,7 @@ static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
     }
 
     if (msi_enabled(pci_dev) && level) {
+        n %= msi_nr_vectors_allocated(pci_dev);
         msi_notify(pci_dev, n);
         return true;
     }
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

Re: [PATCH v3 1/6] hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec

Quoted 29 lines · Phil Dennis-Jordan

QEMU would crash with a failed assertion if the XHCI controller attempted to raise the interrupt on a higher vector than the highest configured for the device by the guest driver.

It turns out the XHCI spec (Implementation Note in section 4.17, "Interrupters") requires that the host controller signal the MSI vector with the number computed by taking the interrupter number modulo the number of enabled MSI vectors.

This change introduces that modulo calculation, fixing the failed assertion and making the device work correctly in MSI mode with macOS's XHCI driver.

---

v2:

* Switch to modulo arithmetic for MSI vector number rather than dropping, as per spec.

  hw/usb/hcd-xhci-pci.c | 1 +
  1 file changed, 1 insertion(+)
hw/usb/hcd-xhci-pci.c +0 -0
index e110840c7a..e5e7330387 100644

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

Quoted 5 lines
hw/usb/hcd-xhci-pci.c +1 -0
@@ -74,6 +74,7 @@ static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
      }
 
      if (msi_enabled(pci_dev) && level) {
+        n %= msi_nr_vectors_allocated(pci_dev);
          msi_notify(pci_dev, n);

Should this be done at the MSI layer in the callee? (I haven't checked the MSI spec).

(Cc'ing hw/pci/msi.c maintainers)

Quoted 2 lines
hw/usb/hcd-xhci-pci.c +0 -0
          return true;
      }
#3 lore raw

Re: [PATCH v3 1/6] hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec

Quoted 34 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Phil Dennis-Jordan

QEMU would crash with a failed assertion if the XHCI controller attempted to raise the interrupt on a higher vector than the highest configured for the device by the guest driver.

It turns out the XHCI spec (Implementation Note in section 4.17, "Interrupters") requires that the host controller signal the MSI vector with the number computed by taking the interrupter number modulo the number of enabled MSI vectors.

This change introduces that modulo calculation, fixing the failed assertion and making the device work correctly in MSI mode with macOS's XHCI driver.

Quoted 6 lines

---

v2:

* Switch to modulo arithmetic for MSI vector number rather than

dropping,

hw/usb/hcd-xhci-pci.c static bool xhci_pci_intr_raise(XHCIState *xhci, int

Quoted 10 lines

as per spec.

  hw/usb/hcd-xhci-pci.c | 1 +
  1 file changed, 1 insertion(+)
hw/usb/hcd-xhci-pci.c +0 -0
index e110840c7a..e5e7330387 100644
@@ -74,6 +74,7 @@ static bool xhci_pci_intr_raise(XHCIState *xhci, int

n, bool level)

Quoted 2 lines
hw/usb/hcd-xhci-pci.c +0 -0
      }
 
Quoted 9 lines

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

Quoted 3 lines
hw/usb/hcd-xhci-pci.c +1 -0
      if (msi_enabled(pci_dev) && level) {
+        n %= msi_nr_vectors_allocated(pci_dev);
          msi_notify(pci_dev, n);

Should this be done at the MSI layer in the callee? (I haven't checked the MSI spec).

(Cc'ing hw/pci/msi.c maintainers)

MSI-X has specified aliasing behaviour. As far as I can tell, MSI does not
- this does not seem especially ambiguous either. From the PCI base spec 3.0:

6.8.3.4. Sending Messages
[…]
“If the Multiple Message Enable field is “000”, the function is not permitted to modify the message data.”
[…]
“How a function uses multiple vectors (when allocated) is device dependent. A function must handle being allocated fewer vectors than requested.”

I understand that to mean that MSI vector aliasing is entirely device-specific, and the assertion in msi_notify() is correct. The XHCI specification statement that the vector should be determined via the modulus of the interrupter index and the number of allocated MSI vectors does indeed seem to be XHCI-specific.

(NB: reading the PCI spec, I’m struck that it seems very vague on HOW the low bits of the message data should be “modified” to encode the vector number. But perhaps I’ve just not found that section yet.)

Quoted 4 lines
Quoted 2 lines
hw/usb/hcd-xhci-pci.c +0 -0
          return true;
      }
#4 lore raw

Re: [PATCH v3 1/6] hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec

Quoted 51 lines · Phil Dennis-Jordan

On Fri 27. Dec 2024 at 18:45, Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

    On 27/12/24 13:13, Phil Dennis-Jordan wrote:
     > QEMU would crash with a failed assertion if the XHCI controller
     > attempted to raise the interrupt on a higher vector than the
     > highest configured for the device by the guest driver.
     >
     > It turns out the XHCI spec (Implementation Note in section 4.17,
     > "Interrupters") requires that the host controller signal the MSI
     > vector with the number computed by taking the interrupter number
     > modulo the number of enabled MSI vectors.
     >
     > This change introduces that modulo calculation, fixing the
     > failed assertion and making the device work correctly in MSI mode
     > with macOS's XHCI driver.
     >
     > Signed-off-by: Phil Dennis-Jordan <[email protected]
    <mailto:[email protected]>>
     > ---
     >
     > v2:
     >
     >   * Switch to modulo arithmetic for MSI vector number rather than
    dropping,
     >     as per spec.
     >
     >   hw/usb/hcd-xhci-pci.c | 1 +
     >   1 file changed, 1 insertion(+)
     >
     > diff --git a/hw/usb/hcd-xhci-pci.c b/hw/usb/hcd-xhci-pci.c
     > index e110840c7a..e5e7330387 100644
     > --- a/hw/usb/hcd-xhci-pci.c
     > +++ b/hw/usb/hcd-xhci-pci.c
     > @@ -74,6 +74,7 @@ static bool xhci_pci_intr_raise(XHCIState
    *xhci, int n, bool level)
     >       }
     >
     >       if (msi_enabled(pci_dev) && level) {
     > +        n %= msi_nr_vectors_allocated(pci_dev);
     >           msi_notify(pci_dev, n);

    Should this be done at the MSI layer in the callee?
    (I haven't checked the MSI spec).

    (Cc'ing hw/pci/msi.c maintainers)

MSI-X has specified aliasing behaviour. As far as I can tell, MSI does not - this does not seem especially ambiguous either. From the PCI base spec 3.0:

Quoted 8 lines

6.8.3.4. Sending Messages
[…]
“If the Multiple Message Enable field is “000”, the function is not permitted to modify the message data.”
[…]
“How a function uses multiple vectors (when allocated) is device dependent. A function must handle being allocated fewer vectors than requested.”

I understand that to mean that MSI vector aliasing is entirely device- specific, and the assertion in msi_notify() is correct. The XHCI specification statement that the vector should be determined via the modulus of the interrupter index and the number of allocated MSI vectors does indeed seem to be XHCI-specific.

OK, thanks for checking! Should we add this new information to the patch description?

Otherwise,

Quoted 1 line

(NB: reading the PCI spec, I’m struck that it seems very vague on HOW the low bits of the message data should be “modified” to encode the vector number. But perhaps I’ve just not found that section yet.)

Maybe MST/Marcel know.

#5 lore raw

Re: [PATCH v3 1/6] hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec

Quoted 65 lines · Philippe Mathieu-Daudé
Quoted 64 lines · Phil Dennis-Jordan

On Fri 27. Dec 2024 at 18:45, Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> wrote:

    On 27/12/24 13:13, Phil Dennis-Jordan wrote:
     > QEMU would crash with a failed assertion if the XHCI controller
     > attempted to raise the interrupt on a higher vector than the
     > highest configured for the device by the guest driver.
     >
     > It turns out the XHCI spec (Implementation Note in section 4.17,
     > "Interrupters") requires that the host controller signal the MSI
     > vector with the number computed by taking the interrupter number
     > modulo the number of enabled MSI vectors.
     >
     > This change introduces that modulo calculation, fixing the
     > failed assertion and making the device work correctly in MSI mode
     > with macOS's XHCI driver.
     >
     > Signed-off-by: Phil Dennis-Jordan <[email protected]
    <mailto:[email protected]>>
     > ---
     >
     > v2:
     >
     >   * Switch to modulo arithmetic for MSI vector number rather than
    dropping,
     >     as per spec.
     >
     >   hw/usb/hcd-xhci-pci.c | 1 +
     >   1 file changed, 1 insertion(+)
     >
     > diff --git a/hw/usb/hcd-xhci-pci.c b/hw/usb/hcd-xhci-pci.c
     > index e110840c7a..e5e7330387 100644
     > --- a/hw/usb/hcd-xhci-pci.c
     > +++ b/hw/usb/hcd-xhci-pci.c
     > @@ -74,6 +74,7 @@ static bool xhci_pci_intr_raise(XHCIState
    *xhci, int n, bool level)
     >       }
     >
     >       if (msi_enabled(pci_dev) && level) {
     > +        n %= msi_nr_vectors_allocated(pci_dev);
     >           msi_notify(pci_dev, n);

    Should this be done at the MSI layer in the callee?
    (I haven't checked the MSI spec).

    (Cc'ing hw/pci/msi.c maintainers)

MSI-X has specified aliasing behaviour. As far as I can tell, MSI does not - this does not seem especially ambiguous either. From the PCI base spec 3.0:

6.8.3.4. Sending Messages
[…]
“If the Multiple Message Enable field is “000”, the function is not permitted to modify the message data.”
[…]
“How a function uses multiple vectors (when allocated) is device dependent. A function must handle being allocated fewer vectors than requested.”

Quoted 9 lines
Quoted 5 lines

I understand that to mean that MSI vector aliasing is entirely device- specific, and the assertion in msi_notify() is correct. The XHCI specification statement that the vector should be determined via the modulus of the interrupter index and the number of allocated MSI vectors does indeed seem to be XHCI-specific.

OK, thanks for checking! Should we add this new information to the patch description?

How about this for the new commit message (I've also slightly clarified the final paragraph about macOS guests):

hw/usb/hcd-xhci-pci: Use modulo to select MSI vector as per spec

QEMU would crash with a failed assertion if the XHCI controller attempted to raise the interrupt on an interrupter corresponding to a MSI vector with a higher index than the highest configured for the device by the guest driver.

This behaviour is correct on the MSI/PCI side: per PCI 3.0 spec, devices must ensure they do not send MSI notifications for vectors beyond the range of those allocated by the system/driver software. Unlike MSI-X, there is no generic way for handling aliasing in the case of fewer allocated vectors than requested, so the specifics are up to device implementors. (Section 6.8.3.4. "Sending Messages")

It turns out the XHCI spec (Implementation Note in section 4.17, "Interrupters") requires that the host controller signal the MSI vector with the number computed by taking the interrupter number modulo the number of enabled MSI vectors.

This change introduces that modulo calculation, fixing the failed assertion. This makes the device work correctly in MSI mode with macOS's XHCI driver, which only allocates a single vector.

Otherwise,

Quoted 8 lines
Quoted 3 lines

(NB: reading the PCI spec, I’m struck that it seems very vague on HOW the low bits of the message data should be “modified” to encode the vector number. But perhaps I’ve just not found that section yet.)

Maybe MST/Marcel know.

msi_prepare_message() implements it by masking off the bits and bitwise or'ing the vector number, which must clearly be the correct method of doing it or guest OSes would not be happy. I was just struck by the lack of clarity in the spec. :-) Much like the handwaving in the XHCI spec on the point of pin-based interrupts. [Note to self: if I ever co-author a standard specification, make sure someone with no prior involvement implements it before ratification.]

#6 lore raw

PATCH v3 2/6 hw/usb/hcd-xhci-pci: Move msi/msix properties from NEC to superclass

In reply to Phil Dennis-Jordan

The NEC XHCI controller exposes the underlying PCI device's msi and msix properties, but the superclass and thus the qemu-xhci device do not. There does not seem to be any obvious reason for this limitation. This change moves these properties to the superclass so they are exposed by both PCI XHCI device variants.

---

v3:

* In line with recent upstream changes, the property table is now const and no longer carries an end-of-list marker.

 hw/usb/hcd-xhci-nec.c | 2 --
 hw/usb/hcd-xhci-pci.c | 6 ++++++
 2 files changed, 6 insertions(+), 2 deletions(-)
hw/usb/hcd-xhci-nec.c +0 -2
index b1df95b52a..1df518baf5 100644
@@ -37,8 +37,6 @@ struct XHCINecState {
 };
 
 static const Property nec_xhci_properties[] = {
-    DEFINE_PROP_ON_OFF_AUTO("msi", XHCIPciState, msi, ON_OFF_AUTO_AUTO),
-    DEFINE_PROP_ON_OFF_AUTO("msix", XHCIPciState, msix, ON_OFF_AUTO_AUTO),
     DEFINE_PROP_UINT32("intrs", XHCINecState, intrs, XHCI_MAXINTRS),
     DEFINE_PROP_UINT32("slots", XHCINecState, slots, XHCI_MAXSLOTS),
 };
hw/usb/hcd-xhci-pci.c +6 -0
index e5e7330387..49642aab58 100644
@@ -198,6 +198,11 @@ static void xhci_instance_init(Object *obj)
     qdev_alias_all_properties(DEVICE(&s->xhci), obj);
 }
 
+static const Property xhci_pci_properties[] = {
+    DEFINE_PROP_ON_OFF_AUTO("msi", XHCIPciState, msi, ON_OFF_AUTO_AUTO),
+    DEFINE_PROP_ON_OFF_AUTO("msix", XHCIPciState, msix, ON_OFF_AUTO_AUTO),
+};
+
 static void xhci_class_init(ObjectClass *klass, void *data)
 {
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
@@ -209,6 +214,7 @@ static void xhci_class_init(ObjectClass *klass, void *data)
     k->realize      = usb_xhci_pci_realize;
     k->exit         = usb_xhci_pci_exit;
     k->class_id     = PCI_CLASS_SERIAL_USB;
+    device_class_set_props(dc, xhci_pci_properties);
 }
 
 static const TypeInfo xhci_pci_info = {
Signature · 1 line
2.39.5 (Apple Git-154)
#7 lore raw

PATCH v3 3/6 hw/usb/hcd-xhci-pci: Use event ring 0 if mapping unsupported

In reply to Phil Dennis-Jordan

The XHCI specification, section 4.17.1 specifies that "If the Number of Interrupters (MaxIntrs) field is greater than 1, then Interrupter Mapping shall be supported." and "If Interrupter Mapping is not supported, the Interrupter Target field shall be ignored by the xHC and all Events targeted at Interrupter 0."

QEMU's XHCI device has so far not specially addressed this case, so we add a check to xhci_event() to redirect to event ring and interrupt 0 if mapping is disabled.

---
 hw/usb/hcd-xhci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/usb/hcd-xhci.c +4 -0
index 3719c0f190..011c445443 100644
@@ -644,6 +644,10 @@ static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v)
     dma_addr_t erdp;
     unsigned int dp_idx;
 
+    if (xhci->numintrs == 1) {
+        v = 0;
+    }
+
     if (v >= xhci->numintrs) {
         DPRINTF("intr nr out of range (%d >= %d)\n", v, xhci->numintrs);
         return;
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v3 3/6
#8 lore raw

Re: [PATCH v3 3/6] hw/usb/hcd-xhci-pci: Use event ring 0 if mapping unsupported

Quoted 6 lines · Phil Dennis-Jordan

The XHCI specification, section 4.17.1 specifies that "If the Number of Interrupters (MaxIntrs) field is greater than 1, then Interrupter Mapping shall be supported." and "If Interrupter Mapping is not supported, the Interrupter Target field shall be ignored by the xHC and all Events targeted at Interrupter 0."

Quoted 8 lines

QEMU's XHCI device has so far not specially addressed this case, so we add a check to xhci_event() to redirect to event ring and interrupt 0 if mapping is disabled.

---
  hw/usb/hcd-xhci.c | 4 ++++
  1 file changed, 4 insertions(+)
#9 lore raw

PATCH v3 4/6 hw/usb/hcd-xhci: Unimplemented/guest error logging for port MMIO

In reply to Phil Dennis-Jordan

The XHCI device code uses tracing rather than logging on various code paths that are so far unimplemented. In some cases, these code paths actually indicate faulty guest software. This patch switches instances in the read and write handlers for the port MMIO region to use qemu_log_mask() with LOG_UNIMP or LOG_GUEST_ERROR, as appropriate in each case.

---
 hw/usb/hcd-xhci.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
hw/usb/hcd-xhci.c +22 -3
index 011c445443..00d5bc3779 100644
@@ -2814,9 +2814,15 @@ static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size)
     case 0x08: /* PORTLI */
         ret = 0;
         break;
-    case 0x0c: /* reserved */
+    case 0x0c: /* PORTHLPMC */
+        ret = 0;
+        qemu_log_mask(LOG_UNIMP, "%s: read from port register PORTHLPMC",
+                      __func__);
+        break;
     default:
-        trace_usb_xhci_unimplemented("port read", reg);
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: read from port offset 0x%" HWADDR_PRIx,
+                      __func__, reg);
         ret = 0;
     }
 
@@ -2885,9 +2891,22 @@ static void xhci_port_write(void *ptr, hwaddr reg,
         }
         break;
     case 0x04: /* PORTPMSC */
+    case 0x0c: /* PORTHLPMC */
+        qemu_log_mask(LOG_UNIMP,
+                      "%s: write 0x%" PRIx64
+                      " (%u bytes) to port register at offset 0x%" HWADDR_PRIx,
+                      __func__, val, size, reg);
+        break;
     case 0x08: /* PORTLI */
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Write to read-only PORTLI register",
+                      __func__);
+        break;
     default:
-        trace_usb_xhci_unimplemented("port write", reg);
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: write 0x%" PRIx64 " (%u bytes) to unknown port "
+                      "register at offset 0x%" HWADDR_PRIx,
+                      __func__, val, size, reg);
+        break;
     }
 }
 
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v3 4/6
#10 lore raw

Re: [PATCH v3 4/6] hw/usb/hcd-xhci: Unimplemented/guest error logging for port MMIO

Quoted 11 lines · Phil Dennis-Jordan

The XHCI device code uses tracing rather than logging on various code paths that are so far unimplemented. In some cases, these code paths actually indicate faulty guest software. This patch switches instances in the read and write handlers for the port MMIO region to use qemu_log_mask() with LOG_UNIMP or LOG_GUEST_ERROR, as appropriate in each case.

---
  hw/usb/hcd-xhci.c | 25 ++++++++++++++++++++++---
  1 file changed, 22 insertions(+), 3 deletions(-)
#11 lore raw

PATCH v3 5/6 hw/usb/hcd-xhci-pci: Adds property for disabling mapping in IRQ mode

In reply to Phil Dennis-Jordan

This change addresses an edge case that trips up macOS guest drivers for PCI based XHCI controllers. The guest driver would attempt to schedule events to XHCI event rings 1 and 2 even when using PCI pin-based interrupts. Interrupts would therefore be dropped, and events only handled on timeout.

So, in addition to disabling interrupter mapping if numintrs is 1, a callback is added to xhci to check whether interrupter mapping should be enabled. The PCI XHCI device type now provides an implementation of this callback if the new "conditional-intr-mapping" property is enabled. (default: disabled) When enabled, interrupter mapping is only enabled when MSI-X or MSI is active.

This means that when using pin-based interrupts, events are only submitted to interrupter 0 regardless of selected target. This allows the macOS guest drivers to work with the device in those configurations.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2705

---
 hw/usb/hcd-xhci-pci.c | 24 ++++++++++++++++++++++++
 hw/usb/hcd-xhci-pci.h |  1 +
 hw/usb/hcd-xhci.c     |  3 ++-
 hw/usb/hcd-xhci.h     |  5 +++++
 4 files changed, 32 insertions(+), 1 deletion(-)
hw/usb/hcd-xhci-pci.c +24 -0
index 49642aab58..d908eb787d 100644
@@ -82,6 +82,21 @@ static bool xhci_pci_intr_raise(XHCIState *xhci, int n, bool level)
     return false;
 }
 
+static bool xhci_pci_intr_mapping_conditional(XHCIState *xhci)
+{
+    XHCIPciState *s = container_of(xhci, XHCIPciState, xhci);
+    PCIDevice *pci_dev = PCI_DEVICE(s);
+
+    /*
+     * Implementation of the "conditional-intr-mapping" property, which only
+     * enables interrupter mapping if MSI or MSI-X is available and active.
+     * Forces all events onto interrupter/event ring 0 in pin-based IRQ mode.
+     * Provides compatibility with macOS guests on machine types where MSI(-X)
+     * is not available.
+     */
+    return msix_enabled(pci_dev) || msi_enabled(pci_dev);
+}
+
 static void xhci_pci_reset(DeviceState *dev)
 {
     XHCIPciState *s = XHCI_PCI(dev);
@@ -119,6 +134,9 @@ static void usb_xhci_pci_realize(struct PCIDevice *dev, Error **errp)
     object_property_set_link(OBJECT(&s->xhci), "host", OBJECT(s), NULL);
     s->xhci.intr_update = xhci_pci_intr_update;
     s->xhci.intr_raise = xhci_pci_intr_raise;
+    if (s->conditional_intr_mapping) {
+        s->xhci.intr_mapping_supported = xhci_pci_intr_mapping_conditional;
+    }
     if (!qdev_realize(DEVICE(&s->xhci), NULL, errp)) {
         return;
     }
@@ -201,6 +219,8 @@ static void xhci_instance_init(Object *obj)
 static const Property xhci_pci_properties[] = {
     DEFINE_PROP_ON_OFF_AUTO("msi", XHCIPciState, msi, ON_OFF_AUTO_AUTO),
     DEFINE_PROP_ON_OFF_AUTO("msix", XHCIPciState, msix, ON_OFF_AUTO_AUTO),
+    DEFINE_PROP_BOOL("conditional-intr-mapping", XHCIPciState,
+                     conditional_intr_mapping, false),
 };
 
 static void xhci_class_init(ObjectClass *klass, void *data)
@@ -215,6 +235,10 @@ static void xhci_class_init(ObjectClass *klass, void *data)
     k->exit         = usb_xhci_pci_exit;
     k->class_id     = PCI_CLASS_SERIAL_USB;
     device_class_set_props(dc, xhci_pci_properties);
+    object_class_property_set_description(klass, "conditional-intr-mapping",
+        "When true, disables interrupter mapping for pin-based IRQ mode. "
+        "Intended to be used with guest drivers with questionable behaviour, "
+        "such as macOS's.");
 }
 
 static const TypeInfo xhci_pci_info = {
hw/usb/hcd-xhci-pci.h +1 -0
index 08f70ce97c..5b61ae8455 100644
@@ -40,6 +40,7 @@ typedef struct XHCIPciState {
     XHCIState xhci;
     OnOffAuto msi;
     OnOffAuto msix;
+    bool conditional_intr_mapping;
 } XHCIPciState;
 
 #endif
hw/usb/hcd-xhci.c +2 -1
index 00d5bc3779..64c3a23b9b 100644
@@ -644,7 +644,8 @@ static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v)
     dma_addr_t erdp;
     unsigned int dp_idx;
 
-    if (xhci->numintrs == 1) {
+    if (xhci->numintrs == 1 ||
+        (xhci->intr_mapping_supported && !xhci->intr_mapping_supported(xhci))) {
         v = 0;
     }
 
hw/usb/hcd-xhci.h +5 -0
index 9609b83514..9c3974f148 100644
@@ -193,6 +193,11 @@ typedef struct XHCIState {
     uint32_t max_pstreams_mask;
     void (*intr_update)(XHCIState *s, int n, bool enable);
     bool (*intr_raise)(XHCIState *s, int n, bool level);
+    /*
+     * Callback for special-casing interrupter mapping support. NULL for most
+     * implementations, for defaulting to enabled mapping unless numintrs == 1.
+     */
+    bool (*intr_mapping_supported)(XHCIState *s);
     DeviceState *hostOpaque;
 
     /* Operational Registers */
Signature · 1 line
2.39.5 (Apple Git-154)
#12 lore raw

PATCH v3 6/6 hw/vmapple: XHCI controller's interrupt mapping workaround for macOS

In reply to Phil Dennis-Jordan

This change enables the new conditional interrupt mapping support property on the vmapple machine type's integrated XHCI controller using compat_props.

The macOS guest driver attempts to use event rings 1 and 2 on the XHCI controller, despite there being only one (PCI pin) interrupt channel available. With conditional interrupt mapping enabled, the XHCI controller will only schedule events on interrupter 0 in PCI pin mode.

---

v2:

* Set the "conditional-intr-mapping" property via compat_props, not directly on the created XHCI controller object.

v3:

* Moved the compat global property table into vmapple patch set -v16, now just add the conditional-intr-mapping property to it.
* Set the property on any device implementing the abstract TYPE_XHCI_PCI rather than only the TYPE_QEMU_XHCI device specifically. (So the NEC controller works with the vmapple machine type too.)

 hw/vmapple/vmapple.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/vmapple/vmapple.c +7 -1
index 41e3d7d189..ec0896dd32 100644
@@ -38,7 +38,7 @@
 #include "hw/intc/arm_gicv3_common.h"
 #include "hw/misc/pvpanic.h"
 #include "hw/pci-host/gpex.h"
-#include "hw/usb/xhci.h"
+#include "hw/usb/hcd-xhci-pci.h"
 #include "hw/virtio/virtio-pci.h"
 #include "hw/vmapple/vmapple.h"
 #include "net/net.h"
@@ -562,6 +562,12 @@ static const CPUArchIdList *vmapple_possible_cpu_arch_ids(MachineState *ms)
 
 static GlobalProperty vmapple_compat_defaults[] = {
     { TYPE_VIRTIO_PCI, "disable-legacy", "on" },
+    /*
+     * macOS XHCI driver attempts to schedule events onto even rings 1 & 2
+     * even when (as here) there is no MSI(-X) support. Disabling interrupter
+     * mapping in the XHCI controller works around the problem.
+     */
+    { TYPE_XHCI_PCI, "conditional-intr-mapping", "on" },
 };
 
 static void vmapple_machine_class_init(ObjectClass *oc, void *data)
Signature · 1 line
2.39.5 (Apple Git-154)