Skip to contentSkip to conversation

Add the AST2700 PCIe EHCI

PATCH New Jamin Lin 1 participant 4 messages Nov 25

Subsystems (2)
lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

cover Add the AST2700 PCIe EHCI

v1
1. fixes lost INTx interrupts in the ASPEED PCIe RC
2. converts hcd-ehci-pci.c to DEFINE_TYPES.
3. adds the AST2700 PCIe EHCI.

Jamin Lin (3):
hw/pci-host/aspeed_pcie: Fix lost INTx interrupts hw/usb/hcd-ehci-pci: Use DEFINE_TYPES hw/usb/hcd-ehci-pci-aspeed: Add the AST2700 PCIe EHCI

 hw/pci-host/aspeed_pcie.c    |  41 +++++++++---
 hw/usb/hcd-ehci-pci-aspeed.c | 118 +++++++++++++++++++++++++++++++++++
 hw/usb/hcd-ehci-pci.c        |  99 ++++++++++++++---------------
 hw/pci-host/trace-events     |   6 +-
 hw/usb/meson.build           |   3 +-
 5 files changed, 204 insertions(+), 63 deletions(-)
 create mode 100644 hw/usb/hcd-ehci-pci-aspeed.c
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH 2/3 hw/usb/hcd-ehci-pci: Use DEFINE_TYPES

Register the EHCI PCI types with one TypeInfo array and DEFINE_TYPES(). This is the current QEMU style for registering types.

No functional change.

---
 hw/usb/hcd-ehci-pci.c | 99 ++++++++++++++++++++++---------------------
 1 file changed, 50 insertions(+), 49 deletions(-)
hw/usb/hcd-ehci-pci.c +50 -49
index fd35d25340..c35529eece 100644
@@ -23,7 +23,6 @@
 #include "qemu/range.h"
 
 typedef struct EHCIPCIInfo {
-    const char *name;
     uint16_t vendor_id;
     uint16_t device_id;
     uint8_t  revision;
@@ -164,20 +163,6 @@ static void ehci_class_init(ObjectClass *klass, const void *data)
     device_class_set_legacy_reset(dc, usb_ehci_pci_reset);
 }
 
-static const TypeInfo ehci_pci_type_info = {
-    .name = TYPE_PCI_EHCI,
-    .parent = TYPE_PCI_DEVICE,
-    .instance_size = sizeof(EHCIPCIState),
-    .instance_init = usb_ehci_pci_init,
-    .instance_finalize = usb_ehci_pci_finalize,
-    .abstract = true,
-    .class_init = ehci_class_init,
-    .interfaces = (const InterfaceInfo[]) {
-        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
-        { },
-    },
-};
-
 static void ehci_data_class_init(ObjectClass *klass, const void *data)
 {
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
@@ -193,42 +178,58 @@ static void ehci_data_class_init(ObjectClass *klass, const void *data)
     }
 }
 
-static struct EHCIPCIInfo ehci_pci_info[] = {
-    {
-        .name      = "usb-ehci",
-        .vendor_id = PCI_VENDOR_ID_INTEL,
-        .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
-        .revision  = 0x10,
-    },{
-        .name      = "ich9-usb-ehci1", /* 00:1d.7 */
-        .vendor_id = PCI_VENDOR_ID_INTEL,
-        .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
-        .revision  = 0x03,
-        .companion = true,
-    },{
-        .name      = "ich9-usb-ehci2", /* 00:1a.7 */
-        .vendor_id = PCI_VENDOR_ID_INTEL,
-        .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2,
-        .revision  = 0x03,
-        .companion = true,
-    }
+static const EHCIPCIInfo usb_ehci_info = {
+    .vendor_id = PCI_VENDOR_ID_INTEL,
+    .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
+    .revision  = 0x10,
 };
 
-static void ehci_pci_register_types(void)
-{
-    TypeInfo ehci_type_info = {
-        .parent        = TYPE_PCI_EHCI,
-        .class_init    = ehci_data_class_init,
-    };
-    int i;
+static const EHCIPCIInfo ich9_usb_ehci1_info = {
+    .vendor_id = PCI_VENDOR_ID_INTEL,
+    .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
+    .revision  = 0x03,
+    .companion = true,
+};
 
-    type_register_static(&ehci_pci_type_info);
+static const EHCIPCIInfo ich9_usb_ehci2_info = {
+    .vendor_id = PCI_VENDOR_ID_INTEL,
+    .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI2,
+    .revision  = 0x03,
+    .companion = true,
+};
 
-    for (i = 0; i < ARRAY_SIZE(ehci_pci_info); i++) {
-        ehci_type_info.name = ehci_pci_info[i].name;
-        ehci_type_info.class_data = ehci_pci_info + i;
-        type_register_static(&ehci_type_info);
-    }
-}
+static const TypeInfo ehci_pci_types[] = {
+    {
+        .name          = TYPE_PCI_EHCI,
+        .parent        = TYPE_PCI_DEVICE,
+        .instance_size = sizeof(EHCIPCIState),
+        .instance_init = usb_ehci_pci_init,
+        .instance_finalize = usb_ehci_pci_finalize,
+        .abstract      = true,
+        .class_init    = ehci_class_init,
+        .interfaces    = (const InterfaceInfo[]) {
+            { INTERFACE_CONVENTIONAL_PCI_DEVICE },
+            { },
+        },
+    },
+    {
+        .name          = "usb-ehci",
+        .parent        = TYPE_PCI_EHCI,
+        .class_init    = ehci_data_class_init,
+        .class_data    = &usb_ehci_info,
+    },
+    {
+        .name          = "ich9-usb-ehci1", /* 00:1d.7 */
+        .parent        = TYPE_PCI_EHCI,
+        .class_init    = ehci_data_class_init,
+        .class_data    = &ich9_usb_ehci1_info,
+    },
+    {
+        .name          = "ich9-usb-ehci2", /* 00:1a.7 */
+        .parent        = TYPE_PCI_EHCI,
+        .class_init    = ehci_data_class_init,
+        .class_data    = &ich9_usb_ehci2_info,
+    },
+};
 
-type_init(ehci_pci_register_types)
+DEFINE_TYPES(ehci_pci_types)
Signature · 1 line
2.53.0
author
#3 lore raw

PATCH 3/3 hw/usb/hcd-ehci-pci-aspeed: Add the AST2700 PCIe EHCI

In reply to Jamin Lin

The AST2700 PCIe EHCI (1a03:2603, rev 0x72, subsystem 1a03:2603) has an MSI capability at 0x50 and uses INTA. It supports 64-bit addressing.

Add it as a new type, "aspeed-usb-ehci", based on the EHCI PCI device. The new type sets the IDs and the interrupt pin, adds the MSI capability, and turns on caps-64bit-addr by default.

The guest decides whether to use MSI. If MSI is on, the device sends an MSI each time the EHCI raises an interrupt. If MSI is off, the device uses INTA. If the board has no MSI support, the device uses INTA only.

---
 hw/usb/hcd-ehci-pci-aspeed.c | 118 +++++++++++++++++++++++++++++++++++
 hw/usb/meson.build           |   3 +-
 2 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 hw/usb/hcd-ehci-pci-aspeed.c
hw/usb/hcd-ehci-pci-aspeed.c new file +118 -0
new file mode 100644
index 0000000000..e18fc047b5
@@ -0,0 +1,118 @@
+/*
+ * ASPEED PCIe EHCI
+ *
+ * Copyright (c) 2026 ASPEED Technology Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/core/irq.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/pci/msi.h"
+#include "hw/usb/hcd-ehci.h"
+
+#define TYPE_ASPEED_EHCI_PCI "aspeed-usb-ehci"
+OBJECT_DECLARE_TYPE(AspeedEHCIPCIState, AspeedEHCIPCIClass, ASPEED_EHCI_PCI)
+
+struct AspeedEHCIPCIState {
+    EHCIPCIState parent_obj;
+};
+
+struct AspeedEHCIPCIClass {
+    PCIDeviceClass parent_class;
+
+    void (*parent_realize)(PCIDevice *dev, Error **errp);
+    PCIUnregisterFunc *parent_exit;
+};
+
+#define ASPEED_EHCI_PCI_MSI_CAP 0x50
+
+/* The EHCI core calls this function when its interrupt changes. */
+static void aspeed_ehci_pci_set_irq(void *opaque, int n, int level)
+{
+    PCIDevice *dev = opaque;
+
+    /* MSI */
+    if (msi_enabled(dev)) {
+        if (level) {
+            msi_notify(dev, 0);
+        }
+        return;
+    }
+
+    /* INTx */
+    pci_set_irq(dev, level);
+}
+
+static void aspeed_ehci_pci_init(Object *obj)
+{
+    qdev_prop_set_bit(DEVICE(obj), "caps-64bit-addr", true);
+}
+
+static void aspeed_ehci_pci_realize(PCIDevice *dev, Error **errp)
+{
+    AspeedEHCIPCIClass *ac = ASPEED_EHCI_PCI_GET_CLASS(dev);
+    AspeedEHCIPCIState *s = ASPEED_EHCI_PCI(dev);
+    EHCIState *ehci = &s->parent_obj.ehci;
+    Error *err = NULL;
+    int ret;
+
+    ac->parent_realize(dev, &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+
+    /*
+     * INTx uses INTA. Send the EHCI interrupt to
+     * aspeed_ehci_pci_set_irq(), which picks MSI or INTx.
+     */
+    pci_set_byte(&dev->config[PCI_INTERRUPT_PIN], 1);
+    qemu_free_irq(ehci->irq);
+    ehci->irq = qemu_allocate_irq(aspeed_ehci_pci_set_irq, dev, 0);
+
+    /* If the board has no MSI, use INTx only. */
+    ret = msi_init(dev, ASPEED_EHCI_PCI_MSI_CAP, 1, true, false, NULL);
+    assert(!ret || ret == -ENOTSUP);
+}
+
+static void aspeed_ehci_pci_exit(PCIDevice *dev)
+{
+    AspeedEHCIPCIClass *ac = ASPEED_EHCI_PCI_GET_CLASS(dev);
+
+    msi_uninit(dev);
+    ac->parent_exit(dev);
+}
+
+static void aspeed_ehci_pci_class_init(ObjectClass *klass, const void *data)
+{
+    AspeedEHCIPCIClass *ac = ASPEED_EHCI_PCI_CLASS(klass);
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    ac->parent_realize = k->realize;
+    ac->parent_exit = k->exit;
+    k->realize = aspeed_ehci_pci_realize;
+    k->exit = aspeed_ehci_pci_exit;
+    k->vendor_id = PCI_VENDOR_ID_ASPEED;
+    k->device_id = 0x2603;
+    k->revision = 0x72;
+    k->subsystem_vendor_id = PCI_VENDOR_ID_ASPEED;
+    k->subsystem_id = 0x2603;
+    set_bit(DEVICE_CATEGORY_USB, dc->categories);
+}
+
+static const TypeInfo aspeed_ehci_pci_types[] = {
+    {
+        .name          = TYPE_ASPEED_EHCI_PCI,
+        .parent        = TYPE_PCI_EHCI,
+        .instance_size = sizeof(AspeedEHCIPCIState),
+        .instance_init = aspeed_ehci_pci_init,
+        .class_size    = sizeof(AspeedEHCIPCIClass),
+        .class_init    = aspeed_ehci_pci_class_init,
+    },
+};
+
+DEFINE_TYPES(aspeed_ehci_pci_types)
hw/usb/meson.build +2 -1
index 240c5344b7..9e09310d1a 100644
@@ -18,7 +18,8 @@ system_ss.add(when: 'CONFIG_USB_OHCI', if_true: files('hcd-ohci.c'))
 system_ss.add(when: 'CONFIG_USB_OHCI_PCI', if_true: files('hcd-ohci-pci.c'))
 system_ss.add(when: 'CONFIG_USB_OHCI_SYSBUS', if_true: files('hcd-ohci-sysbus.c'))
 system_ss.add(when: 'CONFIG_USB_EHCI', if_true: files('hcd-ehci.c'))
-system_ss.add(when: 'CONFIG_USB_EHCI_PCI', if_true: files('hcd-ehci-pci.c'))
+system_ss.add(when: 'CONFIG_USB_EHCI_PCI', if_true: files('hcd-ehci-pci.c',
+                                                          'hcd-ehci-pci-aspeed.c'))
 system_ss.add(when: 'CONFIG_USB_EHCI_SYSBUS', if_true: files('hcd-ehci-sysbus.c'))
 system_ss.add(when: 'CONFIG_USB_XHCI', if_true: files('hcd-xhci.c'))
 system_ss.add(when: 'CONFIG_USB_XHCI_PCI', if_true: files('hcd-xhci-pci.c'))
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/3 hw/pci-host/aspeed_pcie: Fix lost INTx interrupts

In reply to Jamin Lin

The RC has one interrupt line for both INTx and MSI. It should be high when (INT_STS & INT_EN) != 0 or an MSI is pending.

The model only updates the line when a device raises or lowers its INTx. A write to INT_EN does not update it. The guest firmware masks an INTx while the handler runs, so an INTx that comes in during the handler is lost.

For example, with INTD (bit 3, 0x8):

1. The device raises INTD.
2. The guest firmware masks INTD.
3. The device lowers INTD.
4. The device raises INTD again.
5. The guest firmware unmasks INTD.

  step  INT_EN  INT_STS  line
  1     0x8     0x8      1
  2     0x0     0x8      1
  3     0x0     0x0      0
  4     0x0     0x8      0
  5     0x8     0x8      0    (should be 1)

The guest never sees the interrupt from step 4.

INTx and MSI also overwrite each other on the line. If an MSI is pending (MSI_STS0 = 0x1) and a device lowers INTD, the line drops to 0. If INTD is pending (INT_STS = 0x8) and the guest clears the last MSI, the line drops to 0 as well.

Compute the line in one place, aspeed_pcie_rc_update_irq(), and call it on INTx changes, MSI changes and INT_EN writes.

Fixes f002aa35f320 ("hw/pci-host/aspeed: Add AST2600 PCIe config space and host bridge")
---
 hw/pci-host/aspeed_pcie.c | 41 +++++++++++++++++++++++++++++----------
 hw/pci-host/trace-events  |  6 +++---
 2 files changed, 34 insertions(+), 13 deletions(-)
hw/pci-host/aspeed_pcie.c +31 -10
index ceb010f213..2362409ab7 100644
@@ -89,12 +89,29 @@ static void aspeed_pcie_root_port_class_init(ObjectClass *klass,
 
 #define ASPEED_PCIE_CFG_RC_MAX_MSI 64
 
+/*
+ * INTx and MSI share one RC interrupt line. Compute it from both, so one
+ * does not lower the line while the other is still pending.
+ */
+static void aspeed_pcie_rc_update_irq(AspeedPCIECfgState *cfg)
+{
+    uint32_t msi_sts0 = cfg->regs[cfg->rc_regs->msi_sts0_reg];
+    uint32_t msi_sts1 = cfg->regs[cfg->rc_regs->msi_sts1_reg];
+    uint32_t int_sts = cfg->regs[cfg->rc_regs->int_sts_reg];
+    uint32_t int_en = cfg->regs[cfg->rc_regs->int_en_reg];
+    bool level;
+
+    level = (int_sts & int_en) || msi_sts0 || msi_sts1;
+    trace_aspeed_pcie_rc_update_irq(cfg->id, int_sts, int_en, msi_sts0,
+                                    msi_sts1, level);
+    qemu_set_irq(cfg->rc.irq, level);
+}
+
 static void aspeed_pcie_rc_set_irq(void *opaque, int irq, int level)
 {
     AspeedPCIERcState *rc = (AspeedPCIERcState *) opaque;
     AspeedPCIECfgState *cfg =
         container_of(rc, AspeedPCIECfgState, rc);
-    bool intx;
 
     assert(irq < PCI_NUM_PINS);
 
@@ -104,10 +121,8 @@ static void aspeed_pcie_rc_set_irq(void *opaque, int irq, int level)
         cfg->regs[cfg->rc_regs->int_sts_reg] &= ~BIT(irq);
     }
 
-    intx = !!(cfg->regs[cfg->rc_regs->int_sts_reg] &
-              cfg->regs[cfg->rc_regs->int_en_reg]);
-    trace_aspeed_pcie_rc_intx_set_irq(cfg->id, irq, intx);
-    qemu_set_irq(rc->irq, intx);
+    trace_aspeed_pcie_rc_intx_set_irq(cfg->id, irq, level);
+    aspeed_pcie_rc_update_irq(cfg);
 }
 
 static int aspeed_pcie_rc_map_irq(PCIDevice *pci_dev, int irq_num)
@@ -129,7 +144,7 @@ static void aspeed_pcie_rc_msi_notify(AspeedPCIERcState *rc, uint64_t data)
     cfg->regs[reg] |= BIT(data % 32);
 
     trace_aspeed_pcie_rc_msi_set_irq(cfg->id, data, 1);
-    qemu_set_irq(rc->irq, 1);
+    aspeed_pcie_rc_update_irq(cfg);
 }
 
 static void aspeed_pcie_rc_msi_write(void *opaque, hwaddr addr, uint64_t data,
@@ -541,6 +556,10 @@ static void aspeed_pcie_cfg_write(void *opaque, hwaddr addr, uint64_t data,
             s->regs[R_H2X_TX_STS] |= R_H2X_TX_STS_IDLE_MASK;
         }
         break;
+    case R_H2X_RC_H_INT_EN:
+        s->regs[reg] = data;
+        aspeed_pcie_rc_update_irq(s);
+        break;
     /* preserve INTx status */
     case R_H2X_RC_H_INT_STS:
         if (data & H2X_RC_INT_INTDONE_MASK) {
@@ -567,8 +586,7 @@ static void aspeed_pcie_cfg_write(void *opaque, hwaddr addr, uint64_t data,
 
         if (!s->regs[R_H2X_RC_H_MSI_STS0] &&
             !s->regs[R_H2X_RC_H_MSI_STS1]) {
-            trace_aspeed_pcie_rc_msi_clear_irq(s->id, 0);
-            qemu_set_irq(s->rc.irq, 0);
+            aspeed_pcie_rc_update_irq(s);
         }
         break;
     default:
@@ -718,6 +736,10 @@ static void aspeed_2700_pcie_cfg_write(void *opaque, hwaddr addr,
         }
         break;
 
+    case R_H2X_INT_EN:
+        s->regs[reg] = data;
+        aspeed_pcie_rc_update_irq(s);
+        break;
     case R_H2X_INT_STS:
         s->regs[reg] &= ~data | R_H2X_INT_STS_INTX_MASK;
         break;
@@ -740,8 +762,7 @@ static void aspeed_2700_pcie_cfg_write(void *opaque, hwaddr addr,
 
         if (!s->regs[R_H2X_MSI_STS0] &&
             !s->regs[R_H2X_MSI_STS1]) {
-            trace_aspeed_pcie_rc_msi_clear_irq(s->id, 0);
-            qemu_set_irq(s->rc.irq, 0);
+            aspeed_pcie_rc_update_irq(s);
         }
         break;
     default:
hw/pci-host/trace-events +3 -3
index a6fd88c2c4..26730beafc 100644
@@ -1,10 +1,10 @@
 # See docs/devel/tracing.rst for syntax documentation.
 
 # aspeed_pcie.c
-aspeed_pcie_rc_intx_set_irq(uint32_t id, int num, int level) "%d: num %d set IRQ leve %d"
+aspeed_pcie_rc_update_irq(uint32_t id, uint32_t int_sts, uint32_t int_en, uint32_t msi_sts0, uint32_t msi_sts1, int level) "%d: INT_STS 0x%x INT_EN 0x%x MSI_STS0 0x%x MSI_STS1 0x%x level %d"
+aspeed_pcie_rc_intx_set_irq(uint32_t id, int num, int level) "%d: INTx %d level %d"
 aspeed_pcie_rc_msi_notify(uint32_t id, uint64_t addr, uint64_t data) "%d: 0x%" PRIx64 " data 0x%" PRIx64
-aspeed_pcie_rc_msi_set_irq(uint32_t id, uint64_t unm, int level) "%d: num 0x%" PRIx64 " set IRQ level %d"
-aspeed_pcie_rc_msi_clear_irq(uint32_t id, int level) "%d: clear IRQ level %d"
+aspeed_pcie_rc_msi_set_irq(uint32_t id, uint64_t num, int level) "%d: num 0x%" PRIx64 " set IRQ level %d"
 aspeed_pcie_cfg_read(uint32_t id, uint64_t addr, uint32_t value) "%d: addr 0x%" PRIx64 " value 0x%" PRIx32
 aspeed_pcie_cfg_write(uint32_t id, uint64_t addr, uint32_t value) "%d: addr 0x%" PRIx64 " value 0x%" PRIx32
 aspeed_pcie_cfg_rw(uint32_t id, const char *dir, uint8_t bus, uint8_t devfn, uint64_t addr, uint64_t data) "%d: %s bus:0x%x devfn:0x%x addr 0x%" PRIx64 " data 0x%" PRIx64
Signature · 1 line
2.53.0