Skip to contentSkip to conversation

usb/xhci: TR NOOP, TI HCD device, more qtests

PATCH Superseded Nicholas Piggin 1 participant 9 messages Jan 18, 2025 Arm/Amlogic Meson SoC support

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover usb/xhci: TR NOOP, TI HCD device, more qtests

Since last posting, I accounted for feedback from Phil and others.

- Improved the names of the XHCI register constants when moving them to the header file.
- Leaving the symbolic cases for unimplemented registers in the handlers.
- Fixed a bug introduced in a register field calculation when moving code.
- Moved more constants and added more missing ones to the header file.
- Added qtests for the qemu-xhci device.

The qtests for the TI device added in patch 8 still have a small dependency on libqos pci code bug fix which I submitted separately, so that may crash if you try to test it.

Thanks,
Nick

Nicholas Piggin (8):
hw/usb/xhci: Move HCD constants to a header and add register constants hw/usb/xhci: Rename and move HCD register region constants to header tests/qtest/xhci: Add controller and device setup and ring tests hw/usb/xhci: Support TR NOOP commands
tests/qtest/xhci: add a test for TR NOOP commands tests/qtest/xhci: test the qemu-xhci device hw/usb/hcd-xhci-pci: Make PCI device more configurable hw/usb/hcd-xhci-pci: Add TI TUSB73X0 XHCI controller model

 hw/usb/hcd-xhci-pci.h           |   9 +
 hw/usb/hcd-xhci.h               | 237 +++++++++++++
 include/hw/pci/pci_ids.h        |   1 +
 include/hw/usb/xhci.h           |   1 +
 hw/usb/hcd-xhci-pci.c           |  87 ++++-
 hw/usb/hcd-xhci-ti.c            |  88 +++++
 hw/usb/hcd-xhci.c               | 527 ++++++++++------------------
 tests/qtest/usb-hcd-xhci-test.c | 600 +++++++++++++++++++++++++++++++-
 hw/usb/Kconfig                  |   5 +
 hw/usb/meson.build              |   1 +
 10 files changed, 1196 insertions(+), 360 deletions(-)
 create mode 100644 hw/usb/hcd-xhci-ti.c
Signature · 1 line
2.45.2
#1 lore raw

PATCH v2 1/8 hw/usb/xhci: Move HCD constants to a header and add register constants

Prepare to use some of these constants in xhci qtest code.

---
 hw/usb/hcd-xhci.h | 214 ++++++++++++++++++++++
 hw/usb/hcd-xhci.c | 450 +++++++++++++++-------------------------------
 2 files changed, 360 insertions(+), 304 deletions(-)
hw/usb/hcd-xhci.h +214 -0
index 9609b835141..bd1d8e8e81a 100644
@@ -115,6 +115,220 @@ typedef enum TRBCCode {
     CC_SPLIT_TRANSACTION_ERROR
 } TRBCCode;
 
+/* Register definitions */
+#define XHCI_HCCAP_REG_CAPLENGTH            0x00
+#define XHCI_HCCAP_REG_HCIVERSION           0x02
+#define XHCI_HCCAP_REG_HCSPARAMS1           0x04
+#define XHCI_HCCAP_REG_HCSPARAMS2           0x08
+#define XHCI_HCCAP_REG_HCSPARAMS3           0x0C
+#define XHCI_HCCAP_REG_HCCPARAMS1           0x10
+#define   XHCI_HCCPARAMS1_AC64              0x00000001
+#define   XHCI_HCCPARAMS1_XECP_SHIFT        16
+#define   XHCI_HCCPARAMS1_MAXPSASIZE_SHIFT  12
+#define XHCI_HCCAP_REG_DBOFF                0x14
+#define XHCI_HCCAP_REG_RTSOFF               0x18
+#define XHCI_HCCAP_REG_HCCPARAMS2           0x1C
+#define XHCI_HCCAP_EXTCAP_START             0x20 /* SW-defined */
+
+#define XHCI_PORT_PR_SZ                     0x10
+#define XHCI_PORT_REG_PORTSC                0x00
+#define   XHCI_PORTSC_CCS                   (1 << 0)
+#define   XHCI_PORTSC_PED                   (1 << 1)
+#define   XHCI_PORTSC_OCA                   (1 << 3)
+#define   XHCI_PORTSC_PR                    (1 << 4)
+#define   XHCI_PORTSC_PLS_SHIFT             5
+#define   XHCI_PORTSC_PLS_MASK              0xf
+#define   XHCI_PORTSC_PP                    (1 << 9)
+#define   XHCI_PORTSC_SPEED_SHIFT           10
+#define   XHCI_PORTSC_SPEED_MASK            0xf
+#define   XHCI_PORTSC_SPEED_FULL            (1 << 10)
+#define   XHCI_PORTSC_SPEED_LOW             (2 << 10)
+#define   XHCI_PORTSC_SPEED_HIGH            (3 << 10)
+#define   XHCI_PORTSC_SPEED_SUPER           (4 << 10)
+#define   XHCI_PORTSC_PIC_SHIFT             14
+#define   XHCI_PORTSC_PIC_MASK              0x3
+#define   XHCI_PORTSC_LWS                   (1 << 16)
+#define   XHCI_PORTSC_CSC                   (1 << 17)
+#define   XHCI_PORTSC_PEC                   (1 << 18)
+#define   XHCI_PORTSC_WRC                   (1 << 19)
+#define   XHCI_PORTSC_OCC                   (1 << 20)
+#define   XHCI_PORTSC_PRC                   (1 << 21)
+#define   XHCI_PORTSC_PLC                   (1 << 22)
+#define   XHCI_PORTSC_CEC                   (1 << 23)
+#define   XHCI_PORTSC_CAS                   (1 << 24)
+#define   XHCI_PORTSC_WCE                   (1 << 25)
+#define   XHCI_PORTSC_WDE                   (1 << 26)
+#define   XHCI_PORTSC_WOE                   (1 << 27)
+#define   XHCI_PORTSC_DR                    (1 << 30)
+#define   XHCI_PORTSC_WPR                   (1 << 31)
+/* read/write bits */
+#define   XHCI_PORTSC_RW_MASK               (XHCI_PORTSC_PP |    \
+                                             XHCI_PORTSC_WCE |   \
+                                             XHCI_PORTSC_WDE |   \
+                                             XHCI_PORTSC_WOE)
+/* write-1-to-clear bits*/
+#define   XHCI_PORTSC_W1C_MASK              (XHCI_PORTSC_CSC |   \
+                                             XHCI_PORTSC_PEC |   \
+                                             XHCI_PORTSC_WRC |   \
+                                             XHCI_PORTSC_OCC |   \
+                                             XHCI_PORTSC_PRC |   \
+                                             XHCI_PORTSC_PLC |   \
+                                             XHCI_PORTSC_CEC)
+#define XHCI_PORT_REG_PORTPMSC              0x04
+#define XHCI_PORT_REG_PORTLI                0x08
+#define XHCI_PORT_REG_PORTHLPMC             0x0C
+
+#define XHCI_OPER_REG_USBCMD                0x00
+#define   XHCI_USBCMD_RS                    (1 << 0)
+#define   XHCI_USBCMD_HCRST                 (1 << 1)
+#define   XHCI_USBCMD_INTE                  (1 << 2)
+#define   XHCI_USBCMD_HSEE                  (1 << 3)
+#define   XHCI_USBCMD_LHCRST                (1 << 7)
+#define   XHCI_USBCMD_CSS                   (1 << 8)
+#define   XHCI_USBCMD_CRS                   (1 << 9)
+#define   XHCI_USBCMD_EWE                   (1 << 10)
+#define   XHCI_USBCMD_EU3S                  (1 << 11)
+#define XHCI_OPER_REG_USBSTS                0x04
+#define   XHCI_USBSTS_HCH                   (1 << 0)
+#define   XHCI_USBSTS_HSE                   (1 << 2)
+#define   XHCI_USBSTS_EINT                  (1 << 3)
+#define   XHCI_USBSTS_PCD                   (1 << 4)
+#define   XHCI_USBSTS_SSS                   (1 << 8)
+#define   XHCI_USBSTS_RSS                   (1 << 9)
+#define   XHCI_USBSTS_SRE                   (1 << 10)
+#define   XHCI_USBSTS_CNR                   (1 << 11)
+#define   XHCI_USBSTS_HCE                   (1 << 12)
+/* these bits are write-1-to-clear */
+#define   XHCI_USBSTS_W1C_MASK              (XHCI_USBSTS_HSE |    \
+                                             XHCI_USBSTS_EINT |   \
+                                             XHCI_USBSTS_PCD |    \
+                                             XHCI_USBSTS_SRE)
+#define XHCI_OPER_REG_PAGESIZE              0x08
+#define XHCI_OPER_REG_DNCTRL                0x14
+#define XHCI_OPER_REG_CRCR_LO               0x18
+#define   XHCI_CRCR_RCS                     (1 << 0)
+#define   XHCI_CRCR_CS                      (1 << 1)
+#define   XHCI_CRCR_CA                      (1 << 2)
+#define   XHCI_CRCR_CRR                     (1 << 3)
+#define XHCI_OPER_REG_CRCR_HI               0x1C
+#define XHCI_OPER_REG_DCBAAP_LO             0x30
+#define XHCI_OPER_REG_DCBAAP_HI             0x34
+#define XHCI_OPER_REG_CONFIG                0x38
+
+#define XHCI_DBELL_DB_SZ                    0x4
+
+#define XHCI_INTR_REG_MFINDEX               0x00
+#define XHCI_INTR_REG_IR0                   0x20
+#define XHCI_INTR_IR_SZ                     0x20
+
+#define XHCI_INTR_REG_IMAN                  0x00
+#define   XHCI_IMAN_IP                      (1 << 0)
+#define   XHCI_IMAN_IE                      (1 << 1)
+#define XHCI_INTR_REG_IMOD                  0x04
+#define XHCI_INTR_REG_ERSTSZ                0x08
+#define XHCI_INTR_REG_ERSTBA_LO             0x10
+#define XHCI_INTR_REG_ERSTBA_HI             0x14
+#define XHCI_INTR_REG_ERDP_LO               0x18
+#define   XHCI_ERDP_EHB                     (1 << 3)
+#define XHCI_INTR_REG_ERDP_HI               0x1C
+
+#define TRB_SIZE 16
+typedef struct XHCITRB {
+    uint64_t parameter;
+    uint32_t status;
+    uint32_t control;
+    dma_addr_t addr;
+    bool ccs;
+} XHCITRB;
+
+enum {
+    PLS_U0              =  0,
+    PLS_U1              =  1,
+    PLS_U2              =  2,
+    PLS_U3              =  3,
+    PLS_DISABLED        =  4,
+    PLS_RX_DETECT       =  5,
+    PLS_INACTIVE        =  6,
+    PLS_POLLING         =  7,
+    PLS_RECOVERY        =  8,
+    PLS_HOT_RESET       =  9,
+    PLS_COMPILANCE_MODE = 10,
+    PLS_TEST_MODE       = 11,
+    PLS_RESUME          = 15,
+};
+
+#define CR_LINK TR_LINK
+
+#define TRB_C               (1 << 0)
+#define TRB_TYPE_SHIFT      10
+#define TRB_TYPE_MASK       0x3f
+#define TRB_TYPE(t)         (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
+
+#define TRB_EV_ED           (1 << 2)
+
+#define TRB_TR_ENT          (1 << 1)
+#define TRB_TR_ISP          (1 << 2)
+#define TRB_TR_NS           (1 << 3)
+#define TRB_TR_CH           (1 << 4)
+#define TRB_TR_IOC          (1 << 5)
+#define TRB_TR_IDT          (1 << 6)
+#define TRB_TR_TBC_SHIFT    7
+#define TRB_TR_TBC_MASK     0x3
+#define TRB_TR_BEI          (1 << 9)
+#define TRB_TR_TLBPC_SHIFT  16
+#define TRB_TR_TLBPC_MASK   0xf
+#define TRB_TR_FRAMEID_SHIFT    20
+#define TRB_TR_FRAMEID_MASK 0x7ff
+#define TRB_TR_SIA          (1 << 31)
+
+#define TRB_TR_DIR          (1 << 16)
+
+#define TRB_CR_SLOTID_SHIFT 24
+#define TRB_CR_SLOTID_MASK  0xff
+#define TRB_CR_EPID_SHIFT   16
+#define TRB_CR_EPID_MASK    0x1f
+
+#define TRB_CR_BSR          (1 << 9)
+#define TRB_CR_DC           (1 << 9)
+
+#define TRB_LK_TC           (1 << 1)
+
+#define TRB_INTR_SHIFT      22
+#define TRB_INTR_MASK       0x3ff
+#define TRB_INTR(t)         (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
+
+#define EP_TYPE_MASK        0x7
+#define EP_TYPE_SHIFT       3
+
+#define EP_STATE_MASK       0x7
+#define EP_DISABLED         (0 << 0)
+#define EP_RUNNING          (1 << 0)
+#define EP_HALTED           (2 << 0)
+#define EP_STOPPED          (3 << 0)
+#define EP_ERROR            (4 << 0)
+
+#define SLOT_STATE_MASK     0x1f
+#define SLOT_STATE_SHIFT    27
+#define SLOT_STATE(s)       (((s) >> SLOT_STATE_SHIFT) & SLOT_STATE_MASK)
+#define SLOT_ENABLED        0
+#define SLOT_DEFAULT        1
+#define SLOT_ADDRESSED      2
+#define SLOT_CONFIGURED     3
+
+#define SLOT_CONTEXT_ENTRIES_MASK 0x1f
+#define SLOT_CONTEXT_ENTRIES_SHIFT 27
+
+typedef enum EPType {
+    ET_INVALID = 0,
+    ET_ISO_OUT,
+    ET_BULK_OUT,
+    ET_INTR_OUT,
+    ET_CONTROL,
+    ET_ISO_IN,
+    ET_BULK_IN,
+    ET_INTR_IN,
+} EPType;
+
 typedef struct XHCIRing {
     dma_addr_t dequeue;
     bool ccs;
hw/usb/hcd-xhci.c +146 -304

821 lines not on this page · raw message

Signature · 1 line
2.45.2
#2 lore raw

PATCH v2 2/8 hw/usb/xhci: Rename and move HCD register region constants to header

In reply to Nicholas Piggin

This also adds some missing constants rather than open-coding offsets and sizes.

---
 hw/usb/hcd-xhci.h | 16 ++++++++++++++++
 hw/usb/hcd-xhci.c | 48 ++++++++++++++++++++++-------------------------
 2 files changed, 38 insertions(+), 26 deletions(-)
hw/usb/hcd-xhci.h +16 -0
index bd1d8e8e81a..8557f59dafe 100644
@@ -115,6 +115,22 @@ typedef enum TRBCCode {
     CC_SPLIT_TRANSACTION_ERROR
 } TRBCCode;
 
+/* Register regions */
+#define XHCI_REGS_LENGTH_CAP         0x40
+#define XHCI_REGS_LENGTH_OPER        0x400
+#define XHCI_REGS_LENGTH_PORT        (XHCI_PORT_PR_SZ * XHCI_MAXPORTS)
+#define XHCI_REGS_LENGTH_RUNTIME     ((XHCI_MAXINTRS + 1) * XHCI_INTR_IR_SZ)
+/* XXX: Should doorbell length be *4 rather than *32? */
+#define XHCI_REGS_LENGTH_DOORBELL    ((XHCI_MAXSLOTS + 1) * 0x20)
+
+#define XHCI_REGS_OFFSET_CAP         0
+#define XHCI_REGS_OFFSET_OPER        (XHCI_REGS_OFFSET_CAP +   \
+                                      XHCI_REGS_LENGTH_CAP)
+#define XHCI_REGS_OFFSET_PORT        (XHCI_REGS_OFFSET_OPER +  \
+                                      XHCI_REGS_LENGTH_OPER)
+#define XHCI_REGS_OFFSET_RUNTIME     0x1000
+#define XHCI_REGS_OFFSET_DOORBELL    0x2000
+
 /* Register definitions */
 #define XHCI_HCCAP_REG_CAPLENGTH            0x00
 #define XHCI_HCCAP_REG_HCIVERSION           0x02
hw/usb/hcd-xhci.c +22 -26

83 lines not on this page · raw message

Signature · 1 line
2.45.2
#3 lore raw

PATCH v2 3/8 tests/qtest/xhci: Add controller and device setup and ring tests

In reply to Nicholas Piggin

Add tests which init the host controller registers to the point where command and event rings, irqs are operational. Enumerate ports and set up an attached device context that enables device transfer ring to be set up and tested.

This test does a bunch of things at once and is not yet well librified, but it allows testing basic mechanisms and gives a starting point for further work.

---
 hw/usb/hcd-xhci.h               |   7 +
 hw/usb/hcd-xhci.c               |   7 -
 tests/qtest/usb-hcd-xhci-test.c | 516 +++++++++++++++++++++++++++++++-
 3 files changed, 517 insertions(+), 13 deletions(-)
hw/usb/hcd-xhci.h +7 -0
index 8557f59dafe..d149ef59c11 100644
@@ -350,6 +350,13 @@ typedef struct XHCIRing {
     bool ccs;
 } XHCIRing;
 
+typedef struct XHCIEvRingSeg {
+    uint32_t addr_low;
+    uint32_t addr_high;
+    uint32_t size;
+    uint32_t rsvd;
+} XHCIEvRingSeg;
+
 typedef struct XHCIPort {
     XHCIState *xhci;
     uint32_t portsc;
hw/usb/hcd-xhci.c +0 -7
index 76eb473c150..c68cf268365 100644
@@ -128,13 +128,6 @@ struct XHCIEPContext {
     QEMUTimer *kick_timer;
 };
 
-typedef struct XHCIEvRingSeg {
-    uint32_t addr_low;
-    uint32_t addr_high;
-    uint32_t size;
-    uint32_t rsvd;
-} XHCIEvRingSeg;
-
 static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid,
                          unsigned int epid, unsigned int streamid);
 static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid);
tests/qtest/usb-hcd-xhci-test.c +510 -6

563 lines not on this page · raw message

Signature · 1 line
2.45.2
#4 lore raw

PATCH v2 4/8 hw/usb/xhci: Support TR NOOP commands

In reply to Nicholas Piggin

Implement XHCI TR NOOP commands by setting up then immediately completing the packet.

The IBM AIX XHCI HCD driver uses NOOP commands to check driver and hardware health, which works after this change.

---
 hw/usb/hcd-xhci.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
hw/usb/hcd-xhci.c +27 -1
index c68cf268365..256bee6c72f 100644
@@ -1662,6 +1662,20 @@ static int xhci_fire_transfer(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext
     return xhci_submit(xhci, xfer, epctx);
 }
 
+static int xhci_noop_transfer(XHCIState *xhci, XHCITransfer *xfer)
+{
+    /*
+     * TR NOOP conceptually probably better not call into USB subsystem
+     * (usb_packet_setup() via xhci_setup_packet()). In practice it
+     * works and avoids code duplication.
+     */
+    if (xhci_setup_packet(xfer) < 0) {
+        return -1;
+    }
+    xhci_try_complete_packet(xfer);
+    return 0;
+}
+
 static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid,
                          unsigned int epid, unsigned int streamid)
 {
@@ -1784,6 +1798,8 @@ static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid)
 
     epctx->kick_active++;
     while (1) {
+        bool noop = false;
+
         length = xhci_ring_chain_length(xhci, ring);
         if (length <= 0) {
             if (epctx->type == ET_ISO_OUT || epctx->type == ET_ISO_IN) {
@@ -1812,10 +1828,20 @@ static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid)
                 epctx->kick_active--;
                 return;
             }
+            if (type == TR_NOOP) {
+                noop = true;
+            }
         }
         xfer->streamid = streamid;
 
-        if (epctx->epid == 1) {
+        if (noop) {
+            if (length != 1) {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: NOOP TR TRB within TRB chain!\n", __func__);
+                /* Undefined behavior, we no-op the entire chain */
+            }
+            xhci_noop_transfer(xhci, xfer);
+        } else if (epctx->epid == 1) {
             xhci_fire_ctl_transfer(xhci, xfer);
         } else {
             xhci_fire_transfer(xhci, xfer, epctx);
Signature · 1 line
2.45.2
#5 lore raw

PATCH v2 5/8 tests/qtest/xhci: add a test for TR NOOP commands

In reply to Nicholas Piggin

Run some TR NOOP commands through the transfer ring.

---
 tests/qtest/usb-hcd-xhci-test.c | 36 ++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
tests/qtest/usb-hcd-xhci-test.c +35 -1
index b9fb2356d26..63359fb70b9 100644
@@ -361,9 +361,33 @@ static void submit_cr_trb(XHCIQState *s, XHCITRB *trb)
     xhci_db_writel(s, 0, 0); /* doorbell 0 */
 }
 
+static void submit_tr_trb(XHCIQState *s, int slot, XHCITRB *trb)
+{
+    XHCIQSlotState *sl = &s->slots[slot];
+    uint64_t tr_addr = sl->transfer_ring + sl->tr_trb_idx * TRB_SIZE;
+    XHCITRB t;
+
+    trb->control |= sl->tr_trb_c; /* C */
+
+    t.parameter = cpu_to_le64(trb->parameter);
+    t.status = cpu_to_le32(trb->status);
+    t.control = cpu_to_le32(trb->control);
+
+    qtest_memwrite(s->parent->qts, tr_addr, &t, TRB_SIZE);
+    sl->tr_trb_idx++;
+    /* Last entry contains the link, so wrap back */
+    if (sl->tr_trb_idx == sl->tr_trb_entries - 1) {
+        set_link_trb(s, sl->transfer_ring, sl->tr_trb_c, sl->tr_trb_entries);
+        sl->tr_trb_idx = 0;
+        sl->tr_trb_c ^= 1;
+    }
+    xhci_db_writel(s, slot, 1); /* doorbell slot, EP0 target */
+}
+
 /*
  * This test brings up an endpoint and runs some noops through its command
- * ring and gets responses back on the event ring.
+ * ring and gets responses back on the event ring, then brings up a device
+ * context and runs some noops through its transfer ring.
  *
  * This could be librified in future (like AHCI0 to have a way to bring up
  * an endpoint to test device protocols.
@@ -519,6 +543,16 @@ static void pci_xhci_stress_rings(void)
 
     /* XXX: Could check EP state is running */
 
+    /* Wrap the transfer ring a few times */
+    for (i = 0; i < 100; i++) {
+        /* Issue a transfer ring slot 0 noop */
+        memset(&trb, 0, TRB_SIZE);
+        trb.control |= TR_NOOP << TRB_TYPE_SHIFT;
+        trb.control |= TRB_TR_IOC;
+        submit_tr_trb(s, slotid, &trb);
+        wait_event_trb(s, &trb);
+    }
+
     /* Shut it down */
     qpci_msix_disable(s->dev);
 
Signature · 1 line
2.45.2
#6 lore raw

PATCH v2 6/8 tests/qtest/xhci: test the qemu-xhci device

In reply to Nicholas Piggin

Add support in the test code for running multiple drivers, and add tests for the qemu-xhci device.

---
 tests/qtest/usb-hcd-xhci-test.c | 96 +++++++++++++++++++++------------
 1 file changed, 63 insertions(+), 33 deletions(-)
tests/qtest/usb-hcd-xhci-test.c +63 -33
index 63359fb70b9..4efe7b69d4f 100644
@@ -13,10 +13,15 @@
 #include "libqos/libqos-pc.h"
 #include "libqtest-single.h"
 #include "libqos/usb.h"
+#include "hw/pci/pci.h"
 #include "hw/pci/pci_ids.h"
 #include "hw/pci/pci_regs.h"
 #include "hw/usb/hcd-xhci.h"
 
+typedef struct TestData {
+    const char *device;
+} TestData;
+
 /*** Test Setup & Teardown ***/
 typedef struct XHCIQSlotState {
     /* In-memory arrays */
@@ -56,6 +61,8 @@ typedef struct XHCIQState {
     XHCIQSlotState slots[32];
 } XHCIQState;
 
+#define XHCI_QEMU_ID (PCI_DEVICE_ID_REDHAT_XHCI << 16 | \
+                      PCI_VENDOR_ID_REDHAT)
 #define XHCI_NEC_ID (PCI_DEVICE_ID_NEC_UPD720200 << 16 | \
                      PCI_VENDOR_ID_NEC)
 
@@ -76,6 +83,7 @@ static QPCIDevice *get_xhci_device(QTestState *qts, uint32_t *fingerprint)
 
     xhci_fingerprint = qpci_config_readl(xhci, PCI_VENDOR_ID);
     switch (xhci_fingerprint) {
+    case XHCI_QEMU_ID:
     case XHCI_NEC_ID:
         break;
     default:
@@ -128,20 +136,21 @@ static XHCIQState *xhci_boot(const char *cli, ...)
     XHCIQState *s;
     va_list ap;
 
-    if (cli) {
-        va_start(ap, cli);
-        s = xhci_vboot(cli, ap);
-        va_end(ap);
-    } else {
-        s = xhci_boot("-M q35 "
-                      "-device nec-usb-xhci,id=xhci,bus=pcie.0,addr=1d.0 "
-                      "-drive id=drive0,if=none,file=null-co://,"
-                          "file.read-zeroes=on,format=raw");
-    }
+    va_start(ap, cli);
+    s = xhci_vboot(cli, ap);
+    va_end(ap);
 
     return s;
 }
 
+static XHCIQState *xhci_boot_dev(const char *device)
+{
+    return xhci_boot("-M q35 "
+                    "-device %s,id=xhci,bus=pcie.0,addr=1d.0 "
+                    "-drive id=drive0,if=none,file=null-co://,"
+                        "file.read-zeroes=on,format=raw", device);
+}
+
 /**
  * Clean up the PCI device, then terminate the QEMU instance.
  */
@@ -156,12 +165,13 @@ static void xhci_shutdown(XHCIQState *xhci)
 
 /*** tests ***/
 
-static void test_xhci_hotplug(void)
+static void test_xhci_hotplug(const void *arg)
 {
+    const TestData *td = arg;
     XHCIQState *s;
     QTestState *qts;
 
-    s = xhci_boot(NULL);
+    s = xhci_boot_dev(td->device);
     qts = s->parent->qts;
 
     usb_test_hotplug(qts, "xhci", "1", NULL);
@@ -169,12 +179,13 @@ static void test_xhci_hotplug(void)
     xhci_shutdown(s);
 }
 
-static void test_usb_uas_hotplug(void)
+static void test_usb_uas_hotplug(const void *arg)
 {
+    const TestData *td = arg;
     XHCIQState *s;
     QTestState *qts;
 
-    s = xhci_boot(NULL);
+    s = xhci_boot_dev(td->device);
     qts = s->parent->qts;
 
     qtest_qmp_device_add(qts, "usb-uas", "uas", "{}");
@@ -191,12 +202,13 @@ static void test_usb_uas_hotplug(void)
     xhci_shutdown(s);
 }
 
-static void test_usb_ccid_hotplug(void)
+static void test_usb_ccid_hotplug(const void *arg)
 {
+    const TestData *td = arg;
     XHCIQState *s;
     QTestState *qts;
 
-    s = xhci_boot(NULL);
+    s = xhci_boot_dev(td->device);
     qts = s->parent->qts;
 
     qtest_qmp_device_add(qts, "usb-ccid", "ccid", "{}");
@@ -392,8 +404,9 @@ static void submit_tr_trb(XHCIQState *s, int slot, XHCITRB *trb)
  * This could be librified in future (like AHCI0 to have a way to bring up
  * an endpoint to test device protocols.
  */
-static void pci_xhci_stress_rings(void)
+static void test_xhci_stress_rings(const void *arg)
 {
+    const TestData *td = arg;
     XHCIQState *s;
     uint32_t value;
     uint64_t input_context;
@@ -405,11 +418,11 @@ static void pci_xhci_stress_rings(void)
     int i;
 
     s = xhci_boot("-M q35 "
-            "-device nec-usb-xhci,id=xhci,bus=pcie.0,addr=1d.0 "
+            "-device %s,id=xhci,bus=pcie.0,addr=1d.0 "
             "-device usb-storage,bus=xhci.0,drive=drive0 "
             "-drive id=drive0,if=none,file=null-co://,"
-                "file.read-zeroes=on,format=raw "
-            );
+                "file.read-zeroes=on,format=raw ",
+            td->device);
 
     hcsparams1 = xhci_cap_readl(s, XHCI_HCCAP_REG_HCSPARAMS1);
     s->maxports = (hcsparams1 >> 24) & 0xff;
@@ -567,11 +580,37 @@ static void pci_xhci_stress_rings(void)
     xhci_shutdown(s);
 }
 
+static void add_test(const char *name, TestData *td, void (*fn)(const void *))
+{
+    g_autofree char *full_name = g_strdup_printf(
+            "/xhci/pci/%s/%s", td->device, name);
+    qtest_add_data_func(full_name, td, fn);
+}
+
+static void add_tests(TestData *td)
+{
+    add_test("hotplug", td, test_xhci_hotplug);
+    if (qtest_has_device("usb-uas")) {
+        add_test("usb-uas", td, test_usb_uas_hotplug);
+    }
+    if (qtest_has_device("usb-ccid")) {
+        add_test("usb-ccid", td, test_usb_ccid_hotplug);
+    }
+    if (qtest_has_device("usb-storage")) {
+        add_test("xhci-stress-rings", td, test_xhci_stress_rings);
+    }
+}
+
 /* tests */
 int main(int argc, char **argv)
 {
     int ret;
     const char *arch;
+    int i;
+    TestData td[] = {
+        { .device = "qemu-xhci", },
+        { .device = "nec-usb-xhci", },
+    };
 
     g_test_init(&argc, &argv, NULL);
 
@@ -582,19 +621,10 @@ int main(int argc, char **argv)
         return 0;
     }
 
-    if (!qtest_has_device("nec-usb-xhci")) {
-        return 0;
-    }
-
-    qtest_add_func("/xhci/pci/hotplug", test_xhci_hotplug);
-    if (qtest_has_device("usb-uas")) {
-        qtest_add_func("/xhci/pci/hotplug/usb-uas", test_usb_uas_hotplug);
-    }
-    if (qtest_has_device("usb-ccid")) {
-        qtest_add_func("/xhci/pci/hotplug/usb-ccid", test_usb_ccid_hotplug);
-    }
-    if (qtest_has_device("usb-storage")) {
-        qtest_add_func("/xhci/pci/xhci-stress-rings", pci_xhci_stress_rings);
+    for (i = 0; i < ARRAY_SIZE(td); i++) {
+        if (qtest_has_device(td[i].device)) {
+            add_tests(&td[i]);
+        }
     }
 
     ret = g_test_run();
Signature · 1 line
2.45.2
#7 lore raw

PATCH v2 7/8 hw/usb/hcd-xhci-pci: Make PCI device more configurable

In reply to Nicholas Piggin

To prepare to support another USB PCI Host Controller, make some PCI configuration dynamic.

---
 hw/usb/hcd-xhci-pci.h |  9 +++++
 hw/usb/hcd-xhci-pci.c | 87 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 85 insertions(+), 11 deletions(-)
hw/usb/hcd-xhci-pci.h +9 -0
index 08f70ce97cc..213076aabf6 100644
@@ -40,6 +40,15 @@ typedef struct XHCIPciState {
     XHCIState xhci;
     OnOffAuto msi;
     OnOffAuto msix;
+    uint8_t cache_line_size;
+    uint8_t pm_cap_off;
+    uint8_t pcie_cap_off;
+    uint8_t msi_cap_off;
+    uint8_t msix_cap_off;
+    int msix_bar_nr;
+    uint64_t msix_bar_size;
+    uint32_t msix_table_off;
+    uint32_t msix_pba_off;
 } XHCIPciState;
 
 #endif
hw/usb/hcd-xhci-pci.c +76 -11

134 lines not on this page · raw message

Signature · 1 line
2.45.2
#8 lore raw

PATCH v2 8/8 hw/usb/hcd-xhci-pci: Add TI TUSB73X0 XHCI controller model

In reply to Nicholas Piggin

The TI TUSB73X0 controller has some interesting differences from NEC, notably a separate BAR for MSIX, and PM capabilities. The spec is freely available without sign-up.

This controller is accepted by IBM Power proprietary firmware and software (when the subsystem IDs are set to Power servers, which is not done here). IBM code is picky about device support, so the NEC device can not be used.

xhci qtests are added for this device.

---
 include/hw/pci/pci_ids.h        |  1 +
 include/hw/usb/xhci.h           |  1 +
 hw/usb/hcd-xhci-ti.c            | 88 +++++++++++++++++++++++++++++++++
 tests/qtest/usb-hcd-xhci-test.c |  4 ++
 hw/usb/Kconfig                  |  5 ++
 hw/usb/meson.build              |  1 +
 6 files changed, 100 insertions(+)
 create mode 100644 hw/usb/hcd-xhci-ti.c
include/hw/pci/pci_ids.h +1 -0
index f1a53fea8d6..fdb692db513 100644
@@ -182,6 +182,7 @@
 #define PCI_VENDOR_ID_HP                 0x103c
 
 #define PCI_VENDOR_ID_TI                 0x104c
+#define PCI_DEVICE_ID_TI_TUSB73X0        0x8241
 
 #define PCI_VENDOR_ID_MOTOROLA           0x1057
 #define PCI_DEVICE_ID_MOTOROLA_MPC106    0x0002
include/hw/usb/xhci.h +1 -0
index 5c90e1373e5..203ec1fca32 100644
@@ -3,6 +3,7 @@
 
 #define TYPE_XHCI "base-xhci"
 #define TYPE_NEC_XHCI "nec-usb-xhci"
+#define TYPE_TI_XHCI "ti-usb-xhci"
 #define TYPE_QEMU_XHCI "qemu-xhci"
 #define TYPE_XHCI_SYSBUS "sysbus-xhci"
 
hw/usb/hcd-xhci-ti.c new file +88 -0

88 lines not on this page · raw message

tests/qtest/usb-hcd-xhci-test.c +4 -0
index 4efe7b69d4f..dc438cf35c7 100644
@@ -65,6 +65,8 @@ typedef struct XHCIQState {
                       PCI_VENDOR_ID_REDHAT)
 #define XHCI_NEC_ID (PCI_DEVICE_ID_NEC_UPD720200 << 16 | \
                      PCI_VENDOR_ID_NEC)
+#define XHCI_TI_ID  (PCI_DEVICE_ID_TI_TUSB73X0 << 16 | \
+                     PCI_VENDOR_ID_TI)
 
 /**
  * Locate, verify, and return a handle to the XHCI device.
@@ -85,6 +87,7 @@ static QPCIDevice *get_xhci_device(QTestState *qts, uint32_t *fingerprint)
     switch (xhci_fingerprint) {
     case XHCI_QEMU_ID:
     case XHCI_NEC_ID:
+    case XHCI_TI_ID:
         break;
     default:
         /* Unknown device. */
@@ -610,6 +613,7 @@ int main(int argc, char **argv)
     TestData td[] = {
         { .device = "qemu-xhci", },
         { .device = "nec-usb-xhci", },
+        { .device = "ti-usb-xhci", },
     };
 
     g_test_init(&argc, &argv, NULL);
hw/usb/Kconfig +5 -0
index 5fbecd2f43b..8e5c4747af9 100644
@@ -49,6 +49,11 @@ config USB_XHCI_NEC
     default y if PCI_DEVICES
     select USB_XHCI_PCI
 
+config USB_XHCI_TI
+    bool
+    default y if PCI_DEVICES
+    select USB_XHCI_PCI
+
 config USB_XHCI_SYSBUS
     bool
     select USB_XHCI
hw/usb/meson.build +1 -0
index 1b4d1507e41..b874a93f16e 100644
@@ -23,6 +23,7 @@ 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'))
 system_ss.add(when: 'CONFIG_USB_XHCI_SYSBUS', if_true: files('hcd-xhci-sysbus.c'))
 system_ss.add(when: 'CONFIG_USB_XHCI_NEC', if_true: files('hcd-xhci-nec.c'))
+system_ss.add(when: 'CONFIG_USB_XHCI_TI', if_true: files('hcd-xhci-ti.c'))
 system_ss.add(when: 'CONFIG_USB_DWC2', if_true: files('hcd-dwc2.c'))
 system_ss.add(when: 'CONFIG_USB_DWC3', if_true: files('hcd-dwc3.c'))
 
Signature · 1 line
2.45.2