Skip to contentSkip to conversation

hw/usb: Add ASPEED USB Device Controller (UDC)

PATCH Superseded Jamin Lin 2 participants 9 messages Sep 1

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jamin Lin

author
#0 lore raw

v4 cover hw/usb: Add ASPEED USB Device Controller (UDC)

This series adds a QEMU model for the ASPEED USB Device Controller (UDC), driven by the Linux "aspeed_udc" gadget driver.

It is the first step of a larger plan to model USB device-side support on ASPEED BMC/BIC SoCs, which has three goals:

1. Model the ASPEED UDC (AST2600 / AST1030). The AST2600 also has USB host (EHCI) controllers, so this series targets the AST2600 UDC: its gadget can be attached to the SoC's own EHCI bus, letting the guest enumerate its own gadget and exercise the UDC end-to-end. [this series]

2. AST1030 UDC. The AST1030 has no USB host controller, so testing its UDC needs a second QEMU instance. The plan is to redirect the UDC gadget out of the guest using libusbredir and attach it to another QEMU that runs a USB host (a VMM, or an AST2600 / AST2700 guest). [on-going]

3. ASPEED vHub, as a longer-term goal towards BMC KVM / Virtual Media support in QEMU. [future]

This series implements goal 1 only.

Design
------
The UDC is modelled as two QOM objects, because a single object cannot be both a SysBusDevice and a USBDevice:

- "aspeed.udc": the sysbus device (MMIO register map, IRQ and DMA engine) that the guest gadget driver programs.

- "aspeed.udc-gadget": a user-creatable USB device presented on a USB host controller's bus. It links back to its controller through the "udc" property.

The SoC creates the controller; the gadget is added on the command line, e.g.

-device aspeed.udc-gadget,udc=/machine/soc/udc

Test result:
-----------
The default ASPEED SDK prebuilt image does not enable the UDC driver, so build a kernel with CONFIG_USB_ASPEED_UDC=y first.

Start QEMU with the gadget attached to the on-SoC EHCI:

  qemu-system-arm -machine ast2600-evb -drive file=<image>,if=mtd,format=raw \
      -device aspeed.udc-gadget,udc=/machine/soc/udc -nographic

In the guest, bring up a mass-storage gadget and verify enumeration and I/O:

1. Before: only the host controllers are present

root@ast2600-default:~# lsusb
unable to initialize usb specBus 001 Device 001: ID 1d6b:0002 Linux 6.18.20-dirty-32e49fb4a22b-g32e49fb4a22b-dirty ehci_hcd EHCI Host Controller
Bus 002 Device 001: ID 1d6b:0001 Linux 6.18.20-dirty-32e49fb4a22b-g32e49fb4a22b-dirty uhci_hcd Generic UHCI Host Controller

2. Enable the gadget

root@ast2600-default:~# ./usb-storage.sh
Using UDC: 1e6a2000.usb
[  598.582205] Mass Storage Function, version: 2009/09/11
[  598.582635] LUN: removable file: (no medium)
USB Mass Storage gadget is enabled.
Backing file: /home/root/jamin
root@ast2600-default:~# [  598.876395] usb 1-1: new high-speed USB device number 2 using ehci-platform
[  599.070035] usb 1-1: New USB device found, idVendor=1d6b, idProduct=0104, bcdDevice= 1.00
[  599.070821] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  599.071448] usb 1-1: Product: ASPEED USB Storage
[  599.072687] usb 1-1: Manufacturer: ASPEED
[  599.073384] usb 1-1: SerialNumber: 1234567890
[  599.103625] usb-storage 1-1:1.0: USB Mass Storage device detected
[  599.136326] scsi host0: usb-storage 1-1:1.0
[  600.230921] scsi 0:0:0:0: Direct-Access     Linux    File-Stor Gadget 0618 PQ: 0 ANSI: 2
[  600.243758] sd 0:0:0:0: Attached scsi generic sg0 type 0
[  600.263452] sd 0:0:0:0: Power-on or device reset occurred
[  600.289381] sd 0:0:0:0: [sda] 2048 512-byte logical blocks: (1.05 MB/1.00 MiB)
[  600.297896] sd 0:0:0:0: [sda] Write Protect is off
[  600.306379] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  600.424608]  sda:
[  600.425526] sd 0:0:0:0: [sda] Attached SCSI removable disk

3. After: the gadget enumerates on the EHCI bus

root@ast2600-default:~# lsusb
unable to initialize usb specBus 001 Device 001: ID 1d6b:0002 Linux 6.18.20-dirty-32e49fb4a22b-g32e49fb4a22b-dirty ehci_hcd EHCI Host Controller
Bus 001 Device 002: ID 1d6b:0104 ASPEED ASPEED USB Storage
Bus 002 Device 001: ID 1d6b:0001 Linux 6.18.20-dirty-32e49fb4a22b-g32e49fb4a22b-dirty uhci_hcd Generic UHCI Host Controller

4. Mount and verify data

root@ast2600-default:~# mount /dev/sda /mnt/
root@ast2600-default:~# sha256sum /mnt/testfile
3308890a1289f6a327c014537523e8ebd0833301af3d25a7c5893dc7050d2c70  /mnt/testfile
root@ast2600-default:~# umount /mnt

5. Stop the gadget -> clean disconnect

root@ast2600-default:~# ./usb-storage.sh stop
Stopping USB gadget...
[  669.866439] usb 1-1: USB disconnect, device number 2
Stopped.
root@ast2600-default:~# [  669.968156] sd 0:0:0:0: [sda] Synchronizing SCSI cache
[  669.969307] sd 0:0:0:0: [sda] Synchronize Cache(10) failed: Result: hostbyte=0x01 driverbyte=DRIVER_OK
root@ast2600-default:~# lsusb
unable to initialize usb specBus 001 Device 001: ID 1d6b:0002 Linux 6.18.20-dirty-32e49fb4a22b-g32e49fb4a22b-dirty ehci_hcd EHCI Host Controller
Bus 002 Device 001: ID 1d6b:0001 Linux 6.18.20-dirty-32e49fb4a22b-g32e49fb4a22b-dirty uhci_hcd Generic UHCI Host Controller

The contents of this scripts
---------------------------

root@ast2600-default:~# cat  usb-storage.sh
#!/bin/sh
set -e

G=/sys/kernel/config/usb_gadget/g1
IMG=/home/root/jamin
SIZE_MB=64

mount_configfs()

{
    mountpoint -q /sys/kernel/config || mount -t configfs none /sys/kernel/config
}

get_udc()
{
ls /sys/class/udc | head -n 1
}

stop_gadget()

{
    if [ ! -d "$G" ]; then
        return
    fi

    echo "Stopping USB gadget..."

    # Unbind UDC first
    if [ -f "$G/UDC" ]; then
        echo "" > "$G/UDC" 2>/dev/null || true
    fi

    # Clear backing file to release image
    if [ -f "$G/functions/mass_storage.0/lun.0/file" ]; then
        echo "" > "$G/functions/mass_storage.0/lun.0/file" 2>/dev/null || true
    fi

    # Remove function link from config
    rm -f "$G/configs/c.1/mass_storage.0" 2>/dev/null || true

    echo "Stopped."
}

start_gadget()

{
    mount_configfs

    UDC="$(get_udc)"
    if [ -z "$UDC" ]; then
        echo "ERROR: No UDC found in /sys/class/udc"
        exit 1
    fi

    stop_gadget

    echo "Using UDC: $UDC"

    modprobe libcomposite 2>/dev/null || true

    mkdir -p "$G"
    cd "$G"

    echo 0x1d6b > idVendor
    echo 0x0104 > idProduct
    echo 0x0200 > bcdUSB
    echo 0x0100 > bcdDevice

    mkdir -p strings/0x409
    echo "1234567890" > strings/0x409/serialnumber
    echo "ASPEED" > strings/0x409/manufacturer
    echo "ASPEED USB Storage" > strings/0x409/product

    mkdir -p configs/c.1/strings/0x409
    echo "Mass Storage" > configs/c.1/strings/0x409/configuration
    echo 120 > configs/c.1/MaxPower

    if [ ! -f "$IMG" ]; then
        echo "Creating backing image: $IMG"
        dd if=/dev/zero of="$IMG" bs=1M count="$SIZE_MB"
        mkfs.vfat "$IMG"
    fi

    mkdir -p functions/mass_storage.0

    # Make sure old LUN is detached before changing attributes
    echo "" > functions/mass_storage.0/lun.0/file 2>/dev/null || true

    echo 0 > functions/mass_storage.0/stall
    echo 0 > functions/mass_storage.0/lun.0/cdrom
    echo 0 > functions/mass_storage.0/lun.0/ro
    echo 1 > functions/mass_storage.0/lun.0/removable
    echo "$IMG" > functions/mass_storage.0/lun.0/file

    ln -sf functions/mass_storage.0 configs/c.1/mass_storage.0

    echo "$UDC" > UDC

    echo "USB Mass Storage gadget is enabled."
    echo "Backing file: $IMG"
}

case "$1" in

    start|"")
        start_gadget
        ;;
    stop)
        stop_gadget
        ;;
    restart)
        stop_gadget
        sleep 1
        start_gadget
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;

esac

v1:
1. Add ASPEED UDC device controller
2. Add ASPEED UDC gadget USB device

v2:
1. Replace dynamic memory allocation with fixed-size arrays.
2. Use an OR gate to route the interrupt signals from the UDC and EHCI2.
3. Split the "Add ASPEED UDC gadget USB device" patch into two patches:
a. Add ASPEED UDC gadget USB device
Implement EP0 control transfers.
b. Add programmable endpoint DMA transfers
Implement DMA transfers for programmable endpoints.
4. Remove unused macros.

v3:
1. Remove unused macros.
2. Bind the gadget to the controller via a QOM link property ("usbgadget", weak) instead of writing the back-pointer directly

v4:
1. Move the "select USB" dependency to the appropriate patch.

Jamin Lin (4):
hw/usb/aspeed-udc: Add ASPEED UDC device controller hw/usb/aspeed-udc: Add ASPEED UDC gadget USB device hw/usb/aspeed-udc: Add programmable endpoint DMA transfers hw/arm/aspeed_ast2600: Wire up the UDC

 include/hw/arm/aspeed_soc.h |    4 +
 include/hw/usb/aspeed-udc.h |   85 +++
 hw/arm/aspeed_ast2600.c     |   37 +-
 hw/usb/aspeed-udc.c         | 1059 +++++++++++++++++++++++++++++++++++
 hw/arm/Kconfig              |    1 +
 hw/usb/Kconfig              |    4 +
 hw/usb/meson.build          |    1 +
 hw/usb/trace-events         |   16 +
 8 files changed, 1204 insertions(+), 3 deletions(-)
 create mode 100644 include/hw/usb/aspeed-udc.h
 create mode 100644 hw/usb/aspeed-udc.c
Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v4 1/4 hw/usb/aspeed-udc: Add ASPEED UDC device controller

The AST2600 has a USB 2.0 Device Controller (UDC) at 0x1e6a2000 with one control endpoint and four programmable endpoints.

Add the controller as a sysbus (system) device: the MMIO register map described with the registerfields macros, the interrupt line and the soft reset. This is only the register/system side.

Note: this "device controller" is the system-bus device (TYPE_ASPEED_UDC). It is not the gadget USB device (TYPE_ASPEED_UDC_GADGET) that a host controller enumerates, which is added in the next patch.

---
 include/hw/usb/aspeed-udc.h |  49 ++++++++
 hw/usb/aspeed-udc.c         | 236 ++++++++++++++++++++++++++++++++++++
 hw/arm/Kconfig              |   1 +
 hw/usb/Kconfig              |   3 +
 hw/usb/meson.build          |   1 +
 hw/usb/trace-events         |   7 ++
 6 files changed, 297 insertions(+)
 create mode 100644 include/hw/usb/aspeed-udc.h
 create mode 100644 hw/usb/aspeed-udc.c
include/hw/usb/aspeed-udc.h new file +49 -0
new file mode 100644
index 0000000000..58fed5f9a2
@@ -0,0 +1,49 @@
+/*
+ * ASPEED USB Device Controller (UDC)
+ *
+ * Copyright (c) 2026 ASPEED Technology Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_USB_ASPEED_UDC_H
+#define HW_USB_ASPEED_UDC_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_ASPEED_UDC "aspeed.udc"
+OBJECT_DECLARE_SIMPLE_TYPE(AspeedUDCState, ASPEED_UDC)
+
+/*
+ * Register map: root/global block at 0x000 - 0x087, then one 0x10 byte bank
+ * per programmable endpoint from 0x200.
+ */
+#define ASPEED_UDC_MEM_SIZE     0x300
+#define ASPEED_UDC_ROOT_NR_REGS (0x88 >> 2)
+#define ASPEED_UDC_EP_REG_BASE  0x200
+#define ASPEED_UDC_EP_NR_REGS   (0x10 >> 2)
+
+/*
+ * EP0 (control) is served through the root registers (UDC_EP0_*), so only
+ * the 4 programmable endpoints get their own register bank / ep[] entry.
+ */
+#define ASPEED_UDC_NUM_EP   4
+
+typedef struct AspeedUDCEP {
+    MemoryRegion mr;
+    uint32_t regs[ASPEED_UDC_EP_NR_REGS];
+    int index;
+} AspeedUDCEP;
+
+struct AspeedUDCState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion udc_container;
+    MemoryRegion root_mr;
+    uint32_t regs[ASPEED_UDC_ROOT_NR_REGS];
+    AspeedUDCEP ep[ASPEED_UDC_NUM_EP];
+    qemu_irq irq;
+};
+
+#endif /* HW_USB_ASPEED_UDC_H */
hw/usb/aspeed-udc.c new file +236 -0

236 lines not on this page · raw message

hw/arm/Kconfig +1 -0
index 260d2f0751..146ed1a0be 100644
@@ -536,6 +536,7 @@ config ASPEED_SOC
     imply GENERIC_LOADER
     imply PCI_DEVICES
     imply E1000E_PCI_EXPRESS
+    select ASPEED_UDC
     select DS1338
     select FTGMAC100
     select I2C
hw/usb/Kconfig +3 -0
index de95686720..e4df15fe7c 100644
@@ -146,3 +146,6 @@ config XLNX_USB_SUBSYS
 config USB_CHIPIDEA
     bool
     select USB_EHCI_SYSBUS
+
+config ASPEED_UDC
+    bool
hw/usb/meson.build +1 -0
index ba55c28ef6..d4ba60a91c 100644
@@ -27,6 +27,7 @@ system_ss.add(when: 'CONFIG_USB_XHCI_NEC', if_true: files('hcd-xhci-nec.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'))
 system_ss.add(when: 'CONFIG_USB_CHIPIDEA', if_true: files('chipidea.c'))
+system_ss.add(when: 'CONFIG_ASPEED_UDC', if_true: files('aspeed-udc.c'))
 
 system_ss.add(when: 'CONFIG_IMX_USBPHY', if_true: files('imx-usb-phy.c'))
 system_ss.add(when: 'CONFIG_VT82C686', if_true: files('vt82c686-uhci-pci.c'))
hw/usb/trace-events +7 -0
index 67249d69c2..ed05304520 100644
@@ -377,3 +377,10 @@ canokey_handle_data_out(uint8_t ep_out, uint32_t out_len) "ep %d len %d"
 canokey_handle_data_in(uint8_t ep_in, uint32_t in_len) "ep %d len %d"
 canokey_realize(void)
 canokey_unrealize(void)
+
+# aspeed-udc.c
+aspeed_udc_read(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_write(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_ep_read(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_ep_write(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_irq(uint32_t isr, uint32_t ier, int level) "isr 0x%x, ier 0x%x, level %d"
Signature · 1 line
2.53.0
author
#3 lore raw

PATCH v4 2/4 hw/usb/aspeed-udc: Add ASPEED UDC gadget USB device

In reply to Jamin Lin

Present the UDC gadget side to a USB host controller as a USB device (TYPE_ASPEED_UDC_GADGET). This is a normal QEMU USB device, so it can be attached to any USB host controller bus, not only the BMC's own EHCI. It links back to its controller through the "udc" property.

This patch implements the control endpoint (EP0), which is enough for the host to enumerate the gadget. Host control transfers are handled
asynchronously: the SETUP packet is mirrored into the SETUP data buffer, the EP0 interrupt is raised and the host packet is parked (USB_RET_ASYNC). The guest gadget driver then drives the data and status stages by writing UDC_EP0_CTRL; that moves data to/from the driver's DMA buffer and completes the parked packet back to the host.

SET_ADDRESS is the exception: it is applied synchronously, because the host controller keeps the transfer bound to address 0 until it completes.

The gadget connects to / disconnects from the host bus when the driver sets or clears the upstream-enable (pull-up) bit, and is detached on reset.

---
 include/hw/usb/aspeed-udc.h |  28 +++
 hw/usb/aspeed-udc.c         | 382 +++++++++++++++++++++++++++++++++++-
 hw/usb/Kconfig              |   1 +
 hw/usb/trace-events         |   5 +
 4 files changed, 413 insertions(+), 3 deletions(-)
include/hw/usb/aspeed-udc.h +28 -0
index 58fed5f9a2..ab9d016c61 100644
@@ -10,11 +10,19 @@
 #define HW_USB_ASPEED_UDC_H
 
 #include "hw/core/sysbus.h"
+#include "hw/usb/usb.h"
 #include "qom/object.h"
 
 #define TYPE_ASPEED_UDC "aspeed.udc"
 OBJECT_DECLARE_SIMPLE_TYPE(AspeedUDCState, ASPEED_UDC)
 
+/*
+ * The gadget side of the controller is presented to a USB host controller's
+ * bus as a single USB device that delegates back to the AspeedUDCState.
+ */
+#define TYPE_ASPEED_UDC_GADGET "aspeed.udc-gadget"
+OBJECT_DECLARE_SIMPLE_TYPE(AspeedUDCGadget, ASPEED_UDC_GADGET)
+
 /*
  * Register map: root/global block at 0x000 - 0x087, then one 0x10 byte bank
  * per programmable endpoint from 0x200.
@@ -36,14 +44,34 @@ typedef struct AspeedUDCEP {
     int index;
 } AspeedUDCEP;
 
+struct AspeedUDCGadget {
+    USBDevice parent_obj;
+    AspeedUDCState *udc;
+};
+
 struct AspeedUDCState {
     SysBusDevice parent_obj;
 
     MemoryRegion udc_container;
     MemoryRegion root_mr;
+    MemoryRegion *dram_mr;
+    AddressSpace dram_as;
     uint32_t regs[ASPEED_UDC_ROOT_NR_REGS];
     AspeedUDCEP ep[ASPEED_UDC_NUM_EP];
     qemu_irq irq;
+
+    /* gadget USB device bound to this controller (set at its realize) */
+    AspeedUDCGadget *usbgadget;
+
+    /*
+     * In-flight EP0 control transfer (host side), deferred until the guest
+     * gadget driver responds via MMIO.
+     */
+    USBPacket *ep0_packet;
+    uint32_t ep0_setup_len;
+    uint32_t ep0_offset;
+    uint8_t *ep0_data;
+    bool ep0_dir_in;
 };
 
 #endif /* HW_USB_ASPEED_UDC_H */
hw/usb/aspeed-udc.c +379 -3

463 lines not on this page · raw message

hw/usb/Kconfig +1 -0
index e4df15fe7c..e8c00f813a 100644
@@ -149,3 +149,4 @@ config USB_CHIPIDEA
 
 config ASPEED_UDC
     bool
+    select USB
hw/usb/trace-events +5 -0
index ed05304520..098c3d6179 100644
@@ -383,4 +383,9 @@ aspeed_udc_read(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%
 aspeed_udc_write(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
 aspeed_udc_ep_read(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
 aspeed_udc_ep_write(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_pullup(int on, int attached) "on %d, attached %d"
 aspeed_udc_irq(uint32_t isr, uint32_t ier, int level) "isr 0x%x, ier 0x%x, level %d"
+aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x"
+aspeed_udc_ep0_setup(uint8_t type, uint8_t req, uint16_t value, uint16_t index, uint16_t length, int dir_in, int addr) "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength %d, dir_in %d, addr %d"
+aspeed_udc_ep0_ctrl_write(uint32_t val, int dir_in, uint32_t offset) "val 0x%x, dir_in %d, off %u"
+aspeed_udc_ep0_complete(int dir_in, int actual) "dir_in %d, actual %d"
Signature · 1 line
2.53.0
author
#7 lore raw

PATCH v4 4/4 hw/arm/aspeed_ast2600: Wire up the UDC

In reply to Jamin Lin

Create the USB Device Controller (UDC) at 0x1e6a2000 on the AST2600 SoC and map its registers.

The UDC and the second EHCI host controller (EHCI2) share one SoC interrupt line. Route both through an OR gate whose output drives that GIC input, so either device can raise the interrupt without the other clobbering the line.

The gadget USB device is not created by the SoC. It is a separate, user-creatable "aspeed.udc-gadget" USB device that the user plugs onto a USB host controller's bus; it finds its controller through the "udc" link property, e.g.

-device aspeed.udc-gadget,udc=/machine/soc/udc

---
 include/hw/arm/aspeed_soc.h |  4 ++++
 hw/arm/aspeed_ast2600.c     | 37 ++++++++++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 3 deletions(-)
include/hw/arm/aspeed_soc.h +4 -0
index cd68c7f1ca..fe7b31e42c 100644
@@ -36,6 +36,8 @@
 #include "hw/gpio/aspeed_sgpio.h"
 #include "hw/sd/aspeed_sdhci.h"
 #include "hw/usb/hcd-ehci.h"
+#include "hw/usb/aspeed-udc.h"
+#include "hw/core/or-irq.h"
 #include "qom/object.h"
 #include "hw/misc/aspeed_lpc.h"
 #include "hw/misc/unimp.h"
@@ -138,6 +140,8 @@ struct Aspeed2600SoCState {
 
     A15MPPrivState a7mpcore;
     ARMCPU cpu[ASPEED_CPUS_NUM]; /* XXX belong to a7mpcore */
+    AspeedUDCState udc;
+    OrIRQState ehci2_udc_orgate;
 };
 
 #define TYPE_ASPEED2600_SOC "aspeed2600-soc"
hw/arm/aspeed_ast2600.c +34 -3
index d1f18e471a..21db95f5ae 100644
@@ -32,6 +32,7 @@ static const hwaddr aspeed_soc_ast2600_memmap[] = {
     [ASPEED_DEV_SPI1]      = 0x1E630000,
     [ASPEED_DEV_SPI2]      = 0x1E631000,
     [ASPEED_DEV_EHCI1]     = 0x1E6A1000,
+    [ASPEED_DEV_UDC]       = 0x1E6A2000,
     [ASPEED_DEV_EHCI2]     = 0x1E6A3000,
     [ASPEED_DEV_MII1]      = 0x1E650000,
     [ASPEED_DEV_MII2]      = 0x1E650008,
@@ -214,6 +215,10 @@ static void aspeed_soc_ast2600_init(Object *obj)
                                 TYPE_PLATFORM_EHCI);
     }
 
+    object_initialize_child(obj, "udc", &a->udc, TYPE_ASPEED_UDC);
+    object_initialize_child(obj, "ehci2-udc-orgate", &a->ehci2_udc_orgate,
+                            TYPE_OR_IRQ);
+
     snprintf(typename, sizeof(typename), "aspeed.sdmc-%s", socname);
     object_initialize_child(obj, "sdmc", &s->sdmc, typename);
     object_property_add_alias(obj, "ram-size", OBJECT(&s->sdmc),
@@ -561,6 +566,16 @@ static void aspeed_soc_ast2600_realize(DeviceState *dev, Error **errp)
                            aspeed_soc_ast2600_get_irq(s, ASPEED_DEV_SPI1 + i));
     }
 
+    /*
+     * EHCI2 and the UDC share one SoC interrupt line, so OR their outputs
+     * together and drive that GIC input from the OR gate.
+     */
+    object_property_set_int(OBJECT(&a->ehci2_udc_orgate), "num-lines", 2,
+                            &error_abort);
+    qdev_realize(DEVICE(&a->ehci2_udc_orgate), NULL, &error_abort);
+    qdev_connect_gpio_out(DEVICE(&a->ehci2_udc_orgate), 0,
+                          aspeed_soc_ast2600_get_irq(s, ASPEED_DEV_EHCI2));
+
     /* EHCI */
     for (i = 0; i < sc->ehcis_num; i++) {
         if (!sysbus_realize(SYS_BUS_DEVICE(&s->ehci[i]), errp)) {
@@ -568,10 +583,26 @@ static void aspeed_soc_ast2600_realize(DeviceState *dev, Error **errp)
         }
         aspeed_mmio_map(s->memory, SYS_BUS_DEVICE(&s->ehci[i]), 0,
                         sc->memmap[ASPEED_DEV_EHCI1 + i]);
-        sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[i]), 0,
-                           aspeed_soc_ast2600_get_irq(s,
-                                                      ASPEED_DEV_EHCI1 + i));
     }
+    /*
+     * EHCI1 has its own IRQ; EHCI2 shares the UDC's IRQ, so route it through
+     * the OR gate.
+     */
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[0]), 0,
+                       aspeed_soc_ast2600_get_irq(s, ASPEED_DEV_EHCI1));
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[1]), 0,
+                       qdev_get_gpio_in(DEVICE(&a->ehci2_udc_orgate), 0));
+
+    /* UDC - USB 2.0 Device Controller */
+    object_property_set_link(OBJECT(&a->udc), "dram", OBJECT(s->dram_mr),
+                             &error_abort);
+    if (!sysbus_realize(SYS_BUS_DEVICE(&a->udc), errp)) {
+        return;
+    }
+    aspeed_mmio_map(s->memory, SYS_BUS_DEVICE(&a->udc), 0,
+                    sc->memmap[ASPEED_DEV_UDC]);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&a->udc), 0,
+                       qdev_get_gpio_in(DEVICE(&a->ehci2_udc_orgate), 1));
 
     /* SDMC - SDRAM Memory Controller */
     if (!sysbus_realize(SYS_BUS_DEVICE(&s->sdmc), errp)) {
Signature · 1 line
2.53.0
author
#4 lore raw

PATCH v4 3/4 hw/usb/aspeed-udc: Add programmable endpoint DMA transfers

In reply to Jamin Lin

Add the bulk data plane for the four programmable endpoints. The gadget driver queues IN data through the descriptor-list DMA ring and arms OUT buffers through the single-stage DMA registers; host bulk transactions are served from / delivered into those.

The DMA mode is taken from EP_DMA_CTRL.DESC_OP_EN: IN endpoints use the descriptor-list ring, OUT endpoints use single-stage buffers.

A transfer larger than one host packet is served across several polls, with the host packet parked (USB_RET_ASYNC) until the gadget queues (IN) or arms (OUT) more data, then completed from the matching DMA kick.

With this the gadget data endpoints work, e.g. a mass-storage gadget can be enumerated and read/written end to end.

---
 include/hw/usb/aspeed-udc.h |   8 +
 hw/usb/aspeed-udc.c         | 457 +++++++++++++++++++++++++++++++++++-
 hw/usb/trace-events         |   4 +
 3 files changed, 464 insertions(+), 5 deletions(-)
include/hw/usb/aspeed-udc.h +8 -0
index ab9d016c61..7701c1aa34 100644
@@ -42,6 +42,14 @@ typedef struct AspeedUDCEP {
     MemoryRegion mr;
     uint32_t regs[ASPEED_UDC_EP_NR_REGS];
     int index;
+
+    /*
+     * host packet parked until the guest gadget driver queues (IN) or
+     * arms (OUT) data
+     */
+    USBPacket *pkt;
+    /* bytes of the current IN descriptor already served */
+    uint32_t desc_off;
 } AspeedUDCEP;
 
 struct AspeedUDCGadget {
hw/usb/aspeed-udc.c +452 -5

515 lines not on this page · raw message

hw/usb/trace-events +4 -0
index 098c3d6179..80ead23358 100644
@@ -389,3 +389,7 @@ aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x"
 aspeed_udc_ep0_setup(uint8_t type, uint8_t req, uint16_t value, uint16_t index, uint16_t length, int dir_in, int addr) "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength %d, dir_in %d, addr %d"
 aspeed_udc_ep0_ctrl_write(uint32_t val, int dir_in, uint32_t offset) "val 0x%x, dir_in %d, off %u"
 aspeed_udc_ep0_complete(int dir_in, int actual) "dir_in %d, actual %d"
+aspeed_udc_handle_data(int ep_nr, const char *dir, uint32_t iov, int ep_idx) "ep_nr %d, %s, iov %u, ep_idx %d"
+aspeed_udc_ep_data_in(unsigned ep, uint32_t rptr, uint32_t wptr, uint32_t iov) "ep %u, rptr %u, wptr %u, iov %u"
+aspeed_udc_ep_data_out(unsigned ep, uint32_t wptr, uint32_t avail, uint32_t iov) "ep %u, wptr %u, avail %u, iov %u"
+aspeed_udc_ep_ack(unsigned ep) "ep %u"
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH v4 3/4] hw/usb/aspeed-udc: Add programmable endpoint DMA transfers

Quoted 257 lines · Jamin Lin

Add the bulk data plane for the four programmable endpoints. The gadget driver queues IN data through the descriptor-list DMA ring and arms OUT buffers through the single-stage DMA registers; host bulk transactions are served from / delivered into those.

The DMA mode is taken from EP_DMA_CTRL.DESC_OP_EN: IN endpoints use the descriptor-list ring, OUT endpoints use single-stage buffers.

A transfer larger than one host packet is served across several polls, with the host packet parked (USB_RET_ASYNC) until the gadget queues (IN) or arms (OUT) more data, then completed from the matching DMA kick.

With this the gadget data endpoints work, e.g. a mass-storage gadget can be enumerated and read/written end to end.

---
  include/hw/usb/aspeed-udc.h |   8 +
  hw/usb/aspeed-udc.c         | 457 +++++++++++++++++++++++++++++++++++-
  hw/usb/trace-events         |   4 +
  3 files changed, 464 insertions(+), 5 deletions(-)
include/hw/usb/aspeed-udc.h +8 -0
index ab9d016c61..7701c1aa34 100644
@@ -42,6 +42,14 @@ typedef struct AspeedUDCEP {
      MemoryRegion mr;
      uint32_t regs[ASPEED_UDC_EP_NR_REGS];
      int index;
+
+    /*
+     * host packet parked until the guest gadget driver queues (IN) or
+     * arms (OUT) data
+     */
+    USBPacket *pkt;
+    /* bytes of the current IN descriptor already served */
+    uint32_t desc_off;
  } AspeedUDCEP;
 
  struct AspeedUDCGadget {
hw/usb/aspeed-udc.c +188 -1
index 70a2296062..a6eaf6f97b 100644
@@ -72,12 +72,37 @@ REG32(EP_DMA_CTRL, 0x04)
      FIELD(EP_DMA_CTRL, PROC_STS,            4, 4)
      FIELD(EP_DMA_CTRL, DESC_OP_EN,          0, 1)
  REG32(EP_DMA_BUFF, 0x08)
+    FIELD(EP_DMA_BUFF, BASE_ADDR,           0, 31)
  REG32(EP_DMA_STS, 0x0C)
      FIELD(EP_DMA_STS, PKT_SIZE,            16, 11)
      FIELD(EP_DMA_STS, RPTR,                 8, 8)
      FIELD(EP_DMA_STS, WPTR,                 0, 8)
 
-#define ASPEED_UDC_EP0_MAXPKT      64
+#define ASPEED_UDC_EP0_MAXPKT   64
+#define ASPEED_UDC_EP_MAXPKT    1024
+
+/* DMA descriptor ring (256-stage mode) and descriptor data limits */
+#define ASPEED_UDC_DESCS_COUNT  256
+#define ASPEED_UDC_DESC_MAX_LEN 4096
+
+/* DMA processing-status idle codes */
+#define EP_DMA_CTRL_STS_RX_IDLE 0x0
+#define EP_DMA_CTRL_STS_TX_IDLE 0x8
+
+/* DMA descriptor (DES1) fields, in guest memory */
+#define ASPEED_EP_DESC1_IN_LEN(ctrl)   ((ctrl) & 0x1fff)
+/* interrupt-on-completion */
+#define ASPEED_EP_DESC1_INTR           BIT(31)
+
+/* Result of moving a host data packet through an endpoint's DMA */
+typedef enum {
+    /* whole packet transferred */
+    ASPEED_UDC_XFER_DONE,
+    /* not finished, keep parked */
+    ASPEED_UDC_XFER_MORE,
+    /* DMA failed */
+    ASPEED_UDC_XFER_ERROR,
+} AspeedUDCXferResult;
 
  static void aspeed_udc_update_irq(AspeedUDCState *s)
  {
@@ -97,6 +122,14 @@ static void aspeed_udc_raise_isr(AspeedUDCState *s, uint32_t mask)
      aspeed_udc_update_irq(s);
  }
 
+static void aspeed_udc_raise_ep_ack(AspeedUDCState *s, int ep)
+{
+    trace_aspeed_udc_ep_ack(ep);
+    s->regs[R_UDC_EP_ACK_ISR] |= BIT(ep);
+    s->regs[R_UDC_ISR] |= R_UDC_ISR_EP_POOL_ACK_MASK;
+    aspeed_udc_update_irq(s);
+}
+
  /*
   * System bus device: MMIO register interface (guest gadget-driver facing)
   */
@@ -330,6 +363,288 @@ static const MemoryRegionOps aspeed_udc_ops = {
      },
  };
 
+/*
+ * Copy len bytes from guest memory at addr into the IN packet, going through
+ * a bounce buffer one buf-full at a time. Returns false on DMA failure.
+ */
+static bool aspeed_udc_ep_copy_to_pkt(AspeedUDCState *s, int ep, uint32_t addr,
+                                      uint32_t len, USBPacket *p)
+{
+    uint8_t buf[ASPEED_UDC_EP_MAXPKT];
+    uint32_t copied = 0;
+    uint32_t seg;
+
+    while (copied < len) {
+        seg = MIN(len - copied, sizeof(buf));
+        if (address_space_read(&s->dram_as, addr + copied,
+                               MEMTXATTRS_UNSPECIFIED, buf, seg) != MEMTX_OK) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: ep %d IN data DMA read failed\n", __func__,
+                          ep);
+            return false;
+        }
+        usb_packet_copy(p, buf, seg);
+        copied += seg;
+    }
+
+    return true;
+}
+
+/*
+ * IN transfer: send data to the host by filling its IN packet from the
+ * buffers the guest gadget driver queued in the descriptor ring (from the
+ * read pointer to the write pointer).
+ *
+ * One host packet can be bigger than one descriptor's buffer, so we copy from
+ * several descriptors in a row until the packet is full or the ring is empty.
+ * If a descriptor is too big for the space left in the packet, we copy only
+ * part of it now and copy the rest on the next call; desc_off remembers how
+ * far we got. We move the read pointer to the next descriptor only after a
+ * descriptor is fully copied, so the guest gadget driver can read the pointer
+ * and see how much was sent.
+ *
+ * This function raises the endpoint ACK by itself when the ring becomes empty
+ * or when a descriptor asks for an interrupt.
+ */
+static AspeedUDCXferResult aspeed_udc_ep_xfer_in(AspeedUDCState *s, int ep,
+                                                 USBPacket *p)
+{
+    QEMUIOVector *pktiov = p->combined ? &p->combined->iov : &p->iov;
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t mps = FIELD_EX32(e->regs[R_EP_CONFIG], EP_CONFIG, MAX_PKT);
+    uint32_t wptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR);
+    uint32_t rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
+    uint32_t desc_base = e->regs[R_EP_DMA_BUFF];
+    uint32_t desc_addr;
+    uint32_t remaining;
+    uint32_t desc_ctrl;
+    uint32_t pkt_space;
+    /* des_0: data buffer base address, des_1: control/status */
+    uint32_t desc[2];
+    uint32_t offset;
+    uint32_t chunk;
+    uint32_t dlen;
+    bool done = false;
+    bool ack = false;
+
+    if (mps == 0) {
+        /* a MAX_PKT field of 0 means the maximum packet size */
+        mps = ASPEED_UDC_EP_MAXPKT;
+    }
+
+    trace_aspeed_udc_ep_data_in(ep, rptr, wptr, pktiov->size);
+
+    /* walk the queued descriptors, filling the packet */
+    while (rptr != wptr) {
+        if (address_space_read(&s->dram_as, desc_base + rptr * sizeof(desc),
+                               MEMTXATTRS_UNSPECIFIED, desc,
+                               sizeof(desc)) != MEMTX_OK) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: ep %d descriptor DMA read failed\n",
+                          __func__, ep);
+            return ASPEED_UDC_XFER_ERROR;
+        }
+        desc_addr = le32_to_cpu(desc[0]) & R_EP_DMA_BUFF_BASE_ADDR_MASK;
+        desc_ctrl = le32_to_cpu(desc[1]);
+        dlen = ASPEED_EP_DESC1_IN_LEN(desc_ctrl);
+        offset = e->desc_off;
+        /* how much to copy: min(descriptor bytes left, packet space left) */
+        remaining = dlen > offset ? dlen - offset : 0;
+        pkt_space = pktiov->size > (uint32_t)p->actual_length ?
+                    pktiov->size - (uint32_t)p->actual_length : 0;
+        chunk = MIN(remaining, pkt_space);
+
+        if (!aspeed_udc_ep_copy_to_pkt(s, ep, desc_addr + offset, chunk, p)) {
+            return ASPEED_UDC_XFER_ERROR;
+        }
+        e->desc_off += chunk;
+
+        if (e->desc_off < dlen) {
+            /*
+             * The packet ran out of space in the middle of this descriptor,
+             * so only part of it was copied. Stop here, and leave the read
+             * pointer on this descriptor: the next call resumes copying the
+             * rest (desc_off remembers how far we got).
+             */
+            done = true;
+            break;
+        }
+
+        /*
+         * This descriptor was copied in full. Advance the read pointer to the
+         * next descriptor and reset desc_off so it starts from the beginning.
+         */
+        rptr = (rptr + 1) % ASPEED_UDC_DESCS_COUNT;
+        e->desc_off = 0;
+        if (desc_ctrl & ASPEED_EP_DESC1_INTR) {
+            ack = true;
+        }
+        /*
+         * This descriptor is shorter than the max packet size, i.e. a short
+         * (or zero-length) packet. In USB that marks the end of the transfer,
+         * so stop here.
+         */
+        if (dlen < mps) {
+            done = true;
+            break;
+        }
+        /*
+         * The packet is now completely full, so the host has received all the
+         * data it asked for. Stop here.
+         */
+        if ((uint32_t)p->actual_length >= pktiov->size) {
+            done = true;
+            break;
+        }
+    }
+
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS], EP_DMA_STS,
+                                       RPTR, rptr);
+    e->regs[R_EP_DMA_CTRL] = FIELD_DP32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL,
+                                        PROC_STS, EP_DMA_CTRL_STS_TX_IDLE);
+    /* The guest gadget driver completes its request when the ring drains */
+    if (rptr == wptr) {
+        ack = true;
+    }
+    if (ack) {
+        aspeed_udc_raise_ep_ack(s, ep);
+    }
+
+    return done ? ASPEED_UDC_XFER_DONE : ASPEED_UDC_XFER_MORE;
+}
+
+/*
+ * OUT transfer: receive data from the host by copying its OUT packet into the
+ * buffer the guest gadget driver set up (single-stage mode).
+ *

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = { · line 529

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+ * A host packet can be bigger than one buffer, so we copy at most PKT_SIZE
+ * bytes per call, continuing from where the last call stopped
+ * (p->actual_length). The caller keeps the packet parked until it is fully
+ * copied.
+ */
+static AspeedUDCXferResult aspeed_udc_ep_xfer_out(AspeedUDCState *s, int ep,
+                                                  USBPacket *p)
+{
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t chunk = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, PKT_SIZE);

PKT_SIZE is 2K -1, in practice 512 for USB 2.0 bulk.

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = { · line 533

Quoted 4 lines
hw/usb/aspeed-udc.c +4 -0
+    uint32_t remaining = p->iov.size - (uint32_t)p->actual_length;
+    uint32_t data_buf_addr = e->regs[R_EP_DMA_BUFF];
+    uint32_t len = MIN(remaining, chunk);
+    uint8_t buf[ASPEED_UDC_DESC_MAX_LEN];

This is 4k on the stack. we could just use :

g_autofree uint8_t *buf = g_malloc(len)

and drop the MIN() below.

Quoted 125 lines
hw/usb/aspeed-udc.c +116 -0
+
+    if (data_buf_addr && len) {
+        len = MIN(len, sizeof(buf));
+        usb_packet_copy(p, buf, len);
+        if (address_space_write(&s->dram_as, data_buf_addr,
+                                MEMTXATTRS_UNSPECIFIED, buf,
+                                len) != MEMTX_OK) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: ep %d OUT data DMA write failed\n",
+                          __func__, ep);
+            return ASPEED_UDC_XFER_ERROR;
+        }
+    }
+
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS],
+                                       EP_DMA_STS, PKT_SIZE, len);
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS],
+                                       EP_DMA_STS, WPTR, 0);
+    e->regs[R_EP_DMA_CTRL] = FIELD_DP32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL,
+                                        PROC_STS, EP_DMA_CTRL_STS_RX_IDLE);
+    aspeed_udc_raise_ep_ack(s, ep);
+
+    if ((uint32_t)p->actual_length >= p->iov.size) {
+        return ASPEED_UDC_XFER_DONE;
+    }
+
+    return ASPEED_UDC_XFER_MORE;
+}
+
+/*
+ * IN kick: the guest gadget driver wrote EP_DMA_STS to tell us it queued more
+ * IN data to send to the host. If a host IN request is already waiting
+ * (parked because there was no data before), send the data now and finish it.
+ * If the request needs more data than was queued, keep it parked and wait for
+ * the next kick.
+ */
+static void aspeed_udc_ep_in_kick(AspeedUDCState *s, int ep, uint32_t val)
+{
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t cur_rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
+    uint32_t new_rptr = FIELD_EX32(val, EP_DMA_STS, RPTR);
+    uint32_t new_wptr = FIELD_EX32(val, EP_DMA_STS, WPTR);
+    USBPacket *p = e->pkt;
+
+    /*
+     * A normal kick only sets the write pointer and leaves the read-pointer
+     * field 0 (the read pointer is ours to advance). The guest resets the ring
+     * by writing a read pointer that is non-zero and equal to the write
+     * pointer.
+     *
+     * We check non-zero as well as equal: on a normal kick whose write pointer
+     * just wrapped back to 0, both fields would be 0, so an "equal" test alone
+     * would look like a reset by mistake.
+     */
+    if (new_rptr != 0 && new_rptr == new_wptr) {
+        cur_rptr = new_rptr;
+        e->desc_off = 0;
+    }
+    /* store the guest's write, but keep our own read pointer */
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(val, EP_DMA_STS, RPTR, cur_rptr);
+
+    /* nothing to do unless an IN packet is waiting and the ring has data */
+    if (!p || cur_rptr == new_wptr) {
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_in(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        e->pkt = NULL;
+        p->status = USB_RET_SUCCESS;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        e->pkt = NULL;
+        p->status = USB_RET_IOERROR;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        break;
+    }
+}
+
+/*
+ * OUT kick: the guest gadget driver wrote EP_DMA_STS to give us a buffer for
+ * OUT data. If an OUT packet is already waiting (parked because there was no
+ * buffer before), copy its data into the buffer now and finish it. If the
+ * packet has more data than fits, keep it parked and wait for the next buffer.
+ */
+static void aspeed_udc_ep_out_kick(AspeedUDCState *s, int ep)
+{
+    AspeedUDCEP *e = &s->ep[ep];
+    USBPacket *p = e->pkt;
+
+    /* nothing to do unless an OUT packet is waiting and a buffer is ready */
+    if (!p || !FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR)) {
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_out(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        e->pkt = NULL;
+        p->status = USB_RET_SUCCESS;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        e->pkt = NULL;
+        p->status = USB_RET_IOERROR;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        break;
+    }
+}
+
  static uint64_t aspeed_udc_ep_read(void *opaque, hwaddr offset, unsigned size)
  {
      AspeedUDCEP *e = opaque;
@@ -346,10 +661,31 @@ static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,
                                  unsigned size)
  {
      AspeedUDCEP *e = opaque;
+    AspeedUDCState *s = container_of(e - e->index, AspeedUDCState, ep[0]);
      uint32_t reg = offset >> 2;
+    uint32_t val = data;
 

hw/usb/aspeed-udc.c static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data, · line 675

Quoted 10 lines
hw/usb/aspeed-udc.c +8 -2
-    trace_aspeed_udc_ep_write(e->index, offset, data);
-    e->regs[reg] = data;
+    trace_aspeed_udc_ep_write(e->index, offset, val);
+
+    switch (reg) {
+    case R_EP_DMA_BUFF:
+        e->regs[reg] = val & R_EP_DMA_BUFF_BASE_ADDR_MASK;
+        break;
+    case R_EP_DMA_STS:
+        val &= 0x77ffffff;

0x07ffffff ?

Quoted 173 lines
hw/usb/aspeed-udc.c +126 -2
+        if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL, DESC_OP_EN)) {
+            /* IN, descriptor-list mode */
+            aspeed_udc_ep_in_kick(s, e->index, val);
+        } else {
+            /* OUT, single-stage mode */
+            e->regs[reg] = val;
+            aspeed_udc_ep_out_kick(s, e->index);
+        }
+        break;
+    default:
+        e->regs[reg] = val;
+        break;
+    }
  }
 
  static const MemoryRegionOps aspeed_udc_ep_ops = {
@@ -375,6 +711,8 @@ static void aspeed_udc_reset_hold(Object *obj, ResetType type)
      memset(s->regs, 0, sizeof(s->regs));
      for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
          memset(s->ep[i].regs, 0, sizeof(s->ep[i].regs));
+        s->ep[i].pkt = NULL;
+        s->ep[i].desc_off = 0;
      }
 
      /* Device-reset default: root, DMA and EP-pool soft-reset bits set */
@@ -468,6 +806,95 @@ static void aspeed_udc_class_init(ObjectClass *klass, const void *data)
   * through the MMIO register interface above.
   */
 
+static int aspeed_udc_find_ep(AspeedUDCState *s, int ep_nr, bool is_out)
+{
+    uint32_t cfg;
+    int i;
+
+    for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
+        cfg = s->ep[i].regs[R_EP_CONFIG];
+
+        if (!FIELD_EX32(cfg, EP_CONFIG, ENABLE) ||
+            FIELD_EX32(cfg, EP_CONFIG, EP_NUM) != ep_nr) {
+            continue;
+        }
+        if (FIELD_EX32(cfg, EP_CONFIG, DIR_OUT) == is_out) {
+            return i;
+        }
+    }
+
+    return -1;
+}
+
+static void aspeed_udc_ep_data_in(AspeedUDCState *s, int ep, USBPacket *p)
+{
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, RPTR);
+    uint32_t wptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR);
+
+    if (rptr == wptr) {
+        /*
+         * No IN data is queued yet. Save the packet and return ASYNC
+         * instead of NAK. A NAK would make the host retry slowly.
+         * aspeed_udc_ep_in_kick() serves and completes this packet later,
+         * once the guest gadget driver queues descriptors.
+         */
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_in(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        p->status = USB_RET_SUCCESS;
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        /* not fully sent yet: save the packet, wait for more descriptors */
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        p->status = USB_RET_IOERROR;
+        break;
+    }
+}
+
+static void aspeed_udc_ep_data_out(AspeedUDCState *s, int ep, USBPacket *p)
+{
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t sts = e->regs[R_EP_DMA_STS];
+
+    trace_aspeed_udc_ep_data_out(ep, FIELD_EX32(sts, EP_DMA_STS, WPTR),
+                                 FIELD_EX32(sts, EP_DMA_STS, PKT_SIZE),
+                                 p->iov.size);
+    if (!FIELD_EX32(sts, EP_DMA_STS, WPTR)) {
+        /*
+         * No OUT buffer is ready yet. Save the packet and return ASYNC
+         * instead of NAK. Writing now could use an old buffer address and
+         * lose the data (for example a mass-storage CBW). A NAK would make
+         * the host retry slowly. aspeed_udc_ep_out_kick() delivers this
+         * packet later, once the guest gadget driver sets up a buffer.
+         */
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_out(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        p->status = USB_RET_SUCCESS;
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        /* not fully received yet: save the packet, wait for the next buffer */
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        p->status = USB_RET_IOERROR;
+        break;
+    }
+}
+
  static void aspeed_udc_gadget_handle_reset(USBDevice *udev)
  {
      AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc;
@@ -531,17 +958,37 @@ static void aspeed_udc_gadget_handle_control(USBDevice *udev, USBPacket *p,
 
  static void aspeed_udc_gadget_handle_data(USBDevice *udev, USBPacket *p)
  {
-    /* Programmable endpoint (bulk) transfers are added in a later patch. */
-    p->status = USB_RET_STALL;
+    AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc;
+    bool is_out = (p->pid == USB_TOKEN_OUT);
+    int ep = aspeed_udc_find_ep(s, p->ep->nr, is_out);
+
+    trace_aspeed_udc_handle_data(p->ep->nr, is_out ? "OUT" : "IN",
+                                 p->iov.size, ep);
+    if (ep < 0) {
+        p->status = USB_RET_STALL;
+        return;
+    }
+
+    if (is_out) {
+        aspeed_udc_ep_data_out(s, ep, p);
+    } else {
+        aspeed_udc_ep_data_in(s, ep, p);
+    }
  }
 
  static void aspeed_udc_gadget_cancel_packet(USBDevice *udev, USBPacket *p)
  {
      AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc;
+    int i;
 
      if (s->ep0_packet == p) {
          s->ep0_packet = NULL;
      }
+    for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
+        if (s->ep[i].pkt == p) {
+            s->ep[i].pkt = NULL;
+        }
+    }
  }
 
  static void aspeed_udc_gadget_realize(USBDevice *udev, Error **errp)
hw/usb/trace-events +4 -0
index 098c3d6179..80ead23358 100644
@@ -389,3 +389,7 @@ aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x"
  aspeed_udc_ep0_setup(uint8_t type, uint8_t req, uint16_t value, uint16_t index, uint16_t length, int dir_in, int addr) "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength %d, dir_in %d, addr %d"
  aspeed_udc_ep0_ctrl_write(uint32_t val, int dir_in, uint32_t offset) "val 0x%x, dir_in %d, off %u"
  aspeed_udc_ep0_complete(int dir_in, int actual) "dir_in %d, actual %d"
+aspeed_udc_handle_data(int ep_nr, const char *dir, uint32_t iov, int ep_idx) "ep_nr %d, %s, iov %u, ep_idx %d"
+aspeed_udc_ep_data_in(unsigned ep, uint32_t rptr, uint32_t wptr, uint32_t iov) "ep %u, rptr %u, wptr %u, iov %u"
+aspeed_udc_ep_data_out(unsigned ep, uint32_t wptr, uint32_t avail, uint32_t iov) "ep %u, wptr %u, avail %u, iov %u"
+aspeed_udc_ep_ack(unsigned ep) "ep %u"
Reviewed-by on v4 4/4
#8 lore raw

Re: [PATCH v4 4/4] hw/arm/aspeed_ast2600: Wire up the UDC

In reply to Jamin Lin
Quoted 104 lines · Jamin Lin

Create the USB Device Controller (UDC) at 0x1e6a2000 on the AST2600 SoC and map its registers.

The UDC and the second EHCI host controller (EHCI2) share one SoC interrupt line. Route both through an OR gate whose output drives that GIC input, so either device can raise the interrupt without the other clobbering the line.

The gadget USB device is not created by the SoC. It is a separate, user-creatable "aspeed.udc-gadget" USB device that the user plugs onto a USB host controller's bus; it finds its controller through the "udc" link property, e.g.

-device aspeed.udc-gadget,udc=/machine/soc/udc

---
  include/hw/arm/aspeed_soc.h |  4 ++++
  hw/arm/aspeed_ast2600.c     | 37 ++++++++++++++++++++++++++++++++++---
  2 files changed, 38 insertions(+), 3 deletions(-)
include/hw/arm/aspeed_soc.h +4 -0
index cd68c7f1ca..fe7b31e42c 100644
@@ -36,6 +36,8 @@
  #include "hw/gpio/aspeed_sgpio.h"
  #include "hw/sd/aspeed_sdhci.h"
  #include "hw/usb/hcd-ehci.h"
+#include "hw/usb/aspeed-udc.h"
+#include "hw/core/or-irq.h"
  #include "qom/object.h"
  #include "hw/misc/aspeed_lpc.h"
  #include "hw/misc/unimp.h"
@@ -138,6 +140,8 @@ struct Aspeed2600SoCState {
 
      A15MPPrivState a7mpcore;
      ARMCPU cpu[ASPEED_CPUS_NUM]; /* XXX belong to a7mpcore */
+    AspeedUDCState udc;
+    OrIRQState ehci2_udc_orgate;
  };
 
  #define TYPE_ASPEED2600_SOC "aspeed2600-soc"
hw/arm/aspeed_ast2600.c +27 -3
index d1f18e471a..21db95f5ae 100644
@@ -32,6 +32,7 @@ static const hwaddr aspeed_soc_ast2600_memmap[] = {
      [ASPEED_DEV_SPI1]      = 0x1E630000,
      [ASPEED_DEV_SPI2]      = 0x1E631000,
      [ASPEED_DEV_EHCI1]     = 0x1E6A1000,
+    [ASPEED_DEV_UDC]       = 0x1E6A2000,
      [ASPEED_DEV_EHCI2]     = 0x1E6A3000,
      [ASPEED_DEV_MII1]      = 0x1E650000,
      [ASPEED_DEV_MII2]      = 0x1E650008,
@@ -214,6 +215,10 @@ static void aspeed_soc_ast2600_init(Object *obj)
                                  TYPE_PLATFORM_EHCI);
      }
 
+    object_initialize_child(obj, "udc", &a->udc, TYPE_ASPEED_UDC);
+    object_initialize_child(obj, "ehci2-udc-orgate", &a->ehci2_udc_orgate,
+                            TYPE_OR_IRQ);
+
      snprintf(typename, sizeof(typename), "aspeed.sdmc-%s", socname);
      object_initialize_child(obj, "sdmc", &s->sdmc, typename);
      object_property_add_alias(obj, "ram-size", OBJECT(&s->sdmc),
@@ -561,6 +566,16 @@ static void aspeed_soc_ast2600_realize(DeviceState *dev, Error **errp)
                             aspeed_soc_ast2600_get_irq(s, ASPEED_DEV_SPI1 + i));
      }
 
+    /*
+     * EHCI2 and the UDC share one SoC interrupt line, so OR their outputs
+     * together and drive that GIC input from the OR gate.
+     */
+    object_property_set_int(OBJECT(&a->ehci2_udc_orgate), "num-lines", 2,
+                            &error_abort);
+    qdev_realize(DEVICE(&a->ehci2_udc_orgate), NULL, &error_abort);
+    qdev_connect_gpio_out(DEVICE(&a->ehci2_udc_orgate), 0,
+                          aspeed_soc_ast2600_get_irq(s, ASPEED_DEV_EHCI2));
+
      /* EHCI */
      for (i = 0; i < sc->ehcis_num; i++) {
          if (!sysbus_realize(SYS_BUS_DEVICE(&s->ehci[i]), errp)) {
@@ -568,10 +583,26 @@ static void aspeed_soc_ast2600_realize(DeviceState *dev, Error **errp)
          }
          aspeed_mmio_map(s->memory, SYS_BUS_DEVICE(&s->ehci[i]), 0,
                          sc->memmap[ASPEED_DEV_EHCI1 + i]);
-        sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[i]), 0,
-                           aspeed_soc_ast2600_get_irq(s,
-                                                      ASPEED_DEV_EHCI1 + i));
      }
+    /*
+     * EHCI1 has its own IRQ; EHCI2 shares the UDC's IRQ, so route it through
+     * the OR gate.
+     */
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[0]), 0,
+                       aspeed_soc_ast2600_get_irq(s, ASPEED_DEV_EHCI1));
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->ehci[1]), 0,
+                       qdev_get_gpio_in(DEVICE(&a->ehci2_udc_orgate), 0));
+
+    /* UDC - USB 2.0 Device Controller */
+    object_property_set_link(OBJECT(&a->udc), "dram", OBJECT(s->dram_mr),
+                             &error_abort);

hw/arm/aspeed_ast2600.c static void aspeed_soc_ast2600_realize(DeviceState *dev, Error **errp) · line 608

Quoted 10 lines
hw/arm/aspeed_ast2600.c +7 -0
+    if (!sysbus_realize(SYS_BUS_DEVICE(&a->udc), errp)) {
+        return;
+    }
+    aspeed_mmio_map(s->memory, SYS_BUS_DEVICE(&a->udc), 0,
+                    sc->memmap[ASPEED_DEV_UDC]);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&a->udc), 0,
+                       qdev_get_gpio_in(DEVICE(&a->ehci2_udc_orgate), 1));
 
      /* SDMC - SDRAM Memory Controller */
      if (!sysbus_realize(SYS_BUS_DEVICE(&s->sdmc), errp)) {

Thanks,

C.

Reviewed-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] hw/usb/aspeed-udc: Add ASPEED UDC device controller

In reply to Jamin Lin
Quoted 361 lines · Jamin Lin

The AST2600 has a USB 2.0 Device Controller (UDC) at 0x1e6a2000 with one control endpoint and four programmable endpoints.

Add the controller as a sysbus (system) device: the MMIO register map described with the registerfields macros, the interrupt line and the soft reset. This is only the register/system side.

Note: this "device controller" is the system-bus device (TYPE_ASPEED_UDC). It is not the gadget USB device (TYPE_ASPEED_UDC_GADGET) that a host controller enumerates, which is added in the next patch.

---
  include/hw/usb/aspeed-udc.h |  49 ++++++++
  hw/usb/aspeed-udc.c         | 236 ++++++++++++++++++++++++++++++++++++
  hw/arm/Kconfig              |   1 +
  hw/usb/Kconfig              |   3 +
  hw/usb/meson.build          |   1 +
  hw/usb/trace-events         |   7 ++
  6 files changed, 297 insertions(+)

create mode 100644 include/hw/usb/aspeed-udc.h
create mode 100644 hw/usb/aspeed-udc.c

include/hw/usb/aspeed-udc.h new file +49 -0
new file mode 100644
index 0000000000..58fed5f9a2
@@ -0,0 +1,49 @@
+/*
+ * ASPEED USB Device Controller (UDC)
+ *
+ * Copyright (c) 2026 ASPEED Technology Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_USB_ASPEED_UDC_H
+#define HW_USB_ASPEED_UDC_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_ASPEED_UDC "aspeed.udc"
+OBJECT_DECLARE_SIMPLE_TYPE(AspeedUDCState, ASPEED_UDC)
+
+/*
+ * Register map: root/global block at 0x000 - 0x087, then one 0x10 byte bank
+ * per programmable endpoint from 0x200.
+ */
+#define ASPEED_UDC_MEM_SIZE     0x300
+#define ASPEED_UDC_ROOT_NR_REGS (0x88 >> 2)
+#define ASPEED_UDC_EP_REG_BASE  0x200
+#define ASPEED_UDC_EP_NR_REGS   (0x10 >> 2)
+
+/*
+ * EP0 (control) is served through the root registers (UDC_EP0_*), so only
+ * the 4 programmable endpoints get their own register bank / ep[] entry.
+ */
+#define ASPEED_UDC_NUM_EP   4
+
+typedef struct AspeedUDCEP {
+    MemoryRegion mr;
+    uint32_t regs[ASPEED_UDC_EP_NR_REGS];
+    int index;
+} AspeedUDCEP;
+
+struct AspeedUDCState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion udc_container;
+    MemoryRegion root_mr;
+    uint32_t regs[ASPEED_UDC_ROOT_NR_REGS];
+    AspeedUDCEP ep[ASPEED_UDC_NUM_EP];
+    qemu_irq irq;
+};
+
+#endif /* HW_USB_ASPEED_UDC_H */
hw/usb/aspeed-udc.c new file +236 -0
new file mode 100644
index 0000000000..cd773e979b
@@ -0,0 +1,236 @@
+/*
+ * ASPEED USB Device Controller (UDC)
+ *
+ * Copyright (c) 2026 ASPEED Technology Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Models the ASPEED USB Device Controller (UDC). It implements one control
+ * endpoint (EP0) and 4 programmable endpoints.
+ *
+ * This file is the system-bus side of the controller: the MMIO register map,
+ * the interrupt and the soft reset. The gadget USB device presented to a host
+ * controller (and the endpoint data path) is added on top of this.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/core/irq.h"
+#include "hw/core/registerfields.h"
+#include "hw/usb/aspeed-udc.h"
+#include "qemu/module.h"
+#include "trace.h"
+
+/* Root / Global registers (offset from the controller base) */
+REG32(UDC_FUNC_CTRL, 0x00)
+    FIELD(UDC_FUNC_CTRL, UPSTREAM_EN,       0, 1)
+REG32(UDC_IER, 0x08)
+REG32(UDC_ISR, 0x0C)
+    FIELD(UDC_ISR, EP_POOL_ACK,            16, 1)
+    FIELD(UDC_ISR, BUS_RESET,               6, 1)
+    FIELD(UDC_ISR, EP0_IN_ACK,              3, 1)
+    FIELD(UDC_ISR, EP0_OUT_ACK,             1, 1)
+    FIELD(UDC_ISR, EP0_SETUP,               0, 1)
+REG32(UDC_EP_ACK_IER, 0x10)
+REG32(UDC_EP_NAK_IER, 0x14)
+REG32(UDC_EP_ACK_ISR, 0x18)
+REG32(UDC_EP_NAK_ISR, 0x1C)
+REG32(UDC_DEV_RESET, 0x20)
+    FIELD(UDC_DEV_RESET, EP_POOL,           9, 1)
+    FIELD(UDC_DEV_RESET, DMA,               8, 1)
+    FIELD(UDC_DEV_RESET, ROOT,              0, 1)
+REG32(UDC_STS, 0x24)
+    FIELD(UDC_STS, HIGHSPEED,              27, 1)
+REG32(UDC_EP0_CTRL, 0x30)
+    FIELD(UDC_EP0_CTRL, RX_LEN,            16, 7)
+    FIELD(UDC_EP0_CTRL, TX_LEN,             8, 7)
+    FIELD(UDC_EP0_CTRL, RX_RDY,             2, 1)
+    FIELD(UDC_EP0_CTRL, TX_RDY,             1, 1)
+    FIELD(UDC_EP0_CTRL, STALL,              0, 1)
+REG32(UDC_EP0_DATA_BUFF, 0x34)
+/* EP0 SETUP packet buffer: SETUP0 = bytes 0...3, SETUP1 = bytes 4...7 */
+REG32(UDC_SETUP0, 0x80)
+REG32(UDC_SETUP1, 0x84)
+
+/* Per programmable-endpoint registers (offset from the EP register base) */
+REG32(EP_CONFIG, 0x00)
+    FIELD(EP_CONFIG, MAX_PKT,              16, 10)
+    FIELD(EP_CONFIG, EP_NUM,                8, 4)
+    FIELD(EP_CONFIG, DIR_OUT,               4, 1)
+    FIELD(EP_CONFIG, ENABLE,                0, 1)
+REG32(EP_DMA_CTRL, 0x04)
+    FIELD(EP_DMA_CTRL, PROC_STS,            4, 4)
+    FIELD(EP_DMA_CTRL, DESC_OP_EN,          0, 1)
+REG32(EP_DMA_BUFF, 0x08)
+REG32(EP_DMA_STS, 0x0C)
+    FIELD(EP_DMA_STS, PKT_SIZE,            16, 11)
+    FIELD(EP_DMA_STS, RPTR,                 8, 8)
+    FIELD(EP_DMA_STS, WPTR,                 0, 8)
+
+static void aspeed_udc_update_irq(AspeedUDCState *s)
+{
+    bool level;
+
+    level = (s->regs[R_UDC_ISR] & s->regs[R_UDC_IER]) ||
+            (s->regs[R_UDC_EP_ACK_ISR] & s->regs[R_UDC_EP_ACK_IER]) ||
+            (s->regs[R_UDC_EP_NAK_ISR] & s->regs[R_UDC_EP_NAK_IER]);
+
+    trace_aspeed_udc_irq(s->regs[R_UDC_ISR], s->regs[R_UDC_IER], level);
+    qemu_set_irq(s->irq, level);
+}
+
+static uint64_t aspeed_udc_read(void *opaque, hwaddr offset, unsigned size)
+{
+    AspeedUDCState *s = ASPEED_UDC(opaque);
+    uint32_t reg = offset >> 2;
+    uint32_t val;
+
+    val = s->regs[reg];
+    trace_aspeed_udc_read(offset, val);
+
+    return val;
+}
+
+static void aspeed_udc_write(void *opaque, hwaddr offset, uint64_t data,
+                             unsigned size)
+{
+    AspeedUDCState *s = ASPEED_UDC(opaque);
+    uint32_t reg = offset >> 2;
+    uint32_t val = data;
+
+    trace_aspeed_udc_write(offset, val);
+
+    switch (reg) {
+    case R_UDC_IER:
+    case R_UDC_EP_ACK_IER:
+    case R_UDC_EP_NAK_IER:
+        s->regs[reg] = val;
+        aspeed_udc_update_irq(s);
+        break;
+    case R_UDC_ISR:
+    case R_UDC_EP_ACK_ISR:
+    case R_UDC_EP_NAK_ISR:
+        s->regs[reg] &= ~val;
+        aspeed_udc_update_irq(s);
+        break;
+    default:
+        s->regs[reg] = val;
+        break;
+    }
+}
+
+static const MemoryRegionOps aspeed_udc_ops = {
+    .read = aspeed_udc_read,
+    .write = aspeed_udc_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+    },
+};
+
+static uint64_t aspeed_udc_ep_read(void *opaque, hwaddr offset, unsigned size)
+{
+    AspeedUDCEP *e = opaque;
+    uint32_t reg = offset >> 2;
+    uint32_t val;
+
+    val = e->regs[reg];
+    trace_aspeed_udc_ep_read(e->index, offset, val);
+
+    return val;
+}
+
+static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,
+                                unsigned size)
+{
+    AspeedUDCEP *e = opaque;
+    uint32_t reg = offset >> 2;
+
+    trace_aspeed_udc_ep_write(e->index, offset, data);
+    e->regs[reg] = data;
+}
+
+static const MemoryRegionOps aspeed_udc_ep_ops = {
+    .read = aspeed_udc_ep_read,
+    .write = aspeed_udc_ep_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+    },
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+    },
+};
+
+static void aspeed_udc_reset_hold(Object *obj, ResetType type)
+{
+    AspeedUDCState *s = ASPEED_UDC(obj);
+    int i;
+
+    memset(s->regs, 0, sizeof(s->regs));
+    for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
+        memset(s->ep[i].regs, 0, sizeof(s->ep[i].regs));
+    }
+
+    /* Device-reset default: root, DMA and EP-pool soft-reset bits set */
+    s->regs[R_UDC_DEV_RESET] = (R_UDC_DEV_RESET_ROOT_MASK |
+                                R_UDC_DEV_RESET_DMA_MASK |
+                                R_UDC_DEV_RESET_EP_POOL_MASK);
+}
+
+static void aspeed_udc_realize(DeviceState *dev, Error **errp)
+{
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+    AspeedUDCState *s = ASPEED_UDC(dev);
+    int i;
+
+    memory_region_init(&s->udc_container, OBJECT(s), TYPE_ASPEED_UDC,
+                       ASPEED_UDC_MEM_SIZE);
+    memory_region_init_io(&s->root_mr, OBJECT(s), &aspeed_udc_ops, s,
+                          TYPE_ASPEED_UDC ".root",
+                          ASPEED_UDC_ROOT_NR_REGS << 2);
+    memory_region_add_subregion(&s->udc_container, 0, &s->root_mr);
+
+    /* Each programmable endpoint has its own register bank */
+    for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
+        g_autofree char *name = g_strdup_printf(TYPE_ASPEED_UDC ".ep%d", i);
+
+        s->ep[i].index = i;
+        memory_region_init_io(&s->ep[i].mr, OBJECT(s), &aspeed_udc_ep_ops,
+                              &s->ep[i], name, ASPEED_UDC_EP_NR_REGS << 2);
+        memory_region_add_subregion(&s->udc_container,
+                                    ASPEED_UDC_EP_REG_BASE +
+                                    i * (ASPEED_UDC_EP_NR_REGS << 2),
+                                    &s->ep[i].mr);
+    }
+
+    sysbus_init_mmio(sbd, &s->udc_container);
+    sysbus_init_irq(sbd, &s->irq);
+}
+
+static void aspeed_udc_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+
+    dc->desc = "ASPEED USB Device Controller";
+    dc->realize = aspeed_udc_realize;
+    rc->phases.hold = aspeed_udc_reset_hold;
+}
+
+static const TypeInfo aspeed_udc_types[] = {
+    {
+        .name          = TYPE_ASPEED_UDC,
+        .parent        = TYPE_SYS_BUS_DEVICE,
+        .instance_size = sizeof(AspeedUDCState),
+        .class_init    = aspeed_udc_class_init,
+    },
+};
+
+DEFINE_TYPES(aspeed_udc_types)
hw/arm/Kconfig +1 -0
index 260d2f0751..146ed1a0be 100644
@@ -536,6 +536,7 @@ config ASPEED_SOC
      imply GENERIC_LOADER
      imply PCI_DEVICES
      imply E1000E_PCI_EXPRESS
+    select ASPEED_UDC
      select DS1338
      select FTGMAC100
      select I2C
hw/usb/Kconfig +3 -0
index de95686720..e4df15fe7c 100644
@@ -146,3 +146,6 @@ config XLNX_USB_SUBSYS
  config USB_CHIPIDEA
      bool
      select USB_EHCI_SYSBUS
+
+config ASPEED_UDC
+    bool
hw/usb/meson.build +1 -0
index ba55c28ef6..d4ba60a91c 100644
@@ -27,6 +27,7 @@ system_ss.add(when: 'CONFIG_USB_XHCI_NEC', if_true: files('hcd-xhci-nec.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'))
  system_ss.add(when: 'CONFIG_USB_CHIPIDEA', if_true: files('chipidea.c'))
+system_ss.add(when: 'CONFIG_ASPEED_UDC', if_true: files('aspeed-udc.c'))
 
  system_ss.add(when: 'CONFIG_IMX_USBPHY', if_true: files('imx-usb-phy.c'))
  system_ss.add(when: 'CONFIG_VT82C686', if_true: files('vt82c686-uhci-pci.c'))
hw/usb/trace-events +0 -0
index 67249d69c2..ed05304520 100644

hw/usb/trace-events canokey_handle_data_out(uint8_t ep_out, uint32_t out_len) "ep %d len %d" · line 386

Quoted 10 lines
hw/usb/trace-events +7 -0
@@ -377,3 +377,10 @@ canokey_handle_data_out(uint8_t ep_out, uint32_t out_len) "ep %d len %d"
  canokey_handle_data_in(uint8_t ep_in, uint32_t in_len) "ep %d len %d"
  canokey_realize(void)
  canokey_unrealize(void)
+
+# aspeed-udc.c
+aspeed_udc_read(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_write(uint64_t offset, uint32_t value) "offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_ep_read(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_ep_write(int ep, uint64_t offset, uint32_t value) "ep %d, offset 0x%" PRIx64 " value 0x%x"
+aspeed_udc_irq(uint32_t isr, uint32_t ier, int level) "isr 0x%x, ier 0x%x, level %d"

Thanks,

C.

author
#6 lore raw

RE: [PATCH v4 3/4] hw/usb/aspeed-udc: Add programmable endpoint DMA transfers

In reply to Cédric Le Goater

Hi Cédric

Quoted 329 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, September 1, 2026 2:17 PM
To: Jamin Lin <[email protected]>; Paolo Bonzini <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Kane Chen <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ARM TCG CPUs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [PATCH v4 3/4] hw/usb/aspeed-udc: Add programmable endpoint DMA transfers

Quoted 12 lines · Jamin Lin

Add the bulk data plane for the four programmable endpoints. The gadget driver queues IN data through the descriptor-list DMA ring and arms OUT buffers through the single-stage DMA registers; host bulk transactions are served from / delivered into those.

The DMA mode is taken from EP_DMA_CTRL.DESC_OP_EN: IN endpoints use the descriptor-list ring, OUT endpoints use single-stage buffers.

A transfer larger than one host packet is served across several polls, with the host packet parked (USB_RET_ASYNC) until the gadget queues (IN) or arms (OUT) more data, then completed from the matching DMA kick.

Quoted 7 lines

With this the gadget data endpoints work, e.g. a mass-storage gadget can be enumerated and read/written end to end.

---
  include/hw/usb/aspeed-udc.h |   8 +
  hw/usb/aspeed-udc.c         | 457

+++++++++++++++++++++++++++++++++++-

Quoted 32 lines
  hw/usb/trace-events         |   4 +
  3 files changed, 464 insertions(+), 5 deletions(-)
include/hw/usb/aspeed-udc.h +8 -0
index ab9d016c61..7701c1aa34 100644
@@ -42,6 +42,14 @@ typedef struct AspeedUDCEP {
      MemoryRegion mr;
      uint32_t regs[ASPEED_UDC_EP_NR_REGS];
      int index;
+
+    /*
+     * host packet parked until the guest gadget driver queues (IN) or
+     * arms (OUT) data
+     */
+    USBPacket *pkt;
+    /* bytes of the current IN descriptor already served */
+    uint32_t desc_off;
  } AspeedUDCEP;
 
  struct AspeedUDCGadget {
hw/usb/aspeed-udc.c index renamed from hw/usb/aspeed-udc.c +0 -0

70a2296062..a6eaf6f97b 100644

hw/usb/aspeed-udc.c +1 -0
@@ -72,12 +72,37 @@ REG32(EP_DMA_CTRL, 0x04)
      FIELD(EP_DMA_CTRL, PROC_STS,            4, 4)
      FIELD(EP_DMA_CTRL, DESC_OP_EN,          0, 1)
  REG32(EP_DMA_BUFF, 0x08)
+    FIELD(EP_DMA_BUFF, BASE_ADDR,           0, 31)
  REG32(EP_DMA_STS, 0x0C)

hw/usb/aspeed-udc.c REG32(EP_DMA_CTRL, 0x04) · line 85

Quoted 10 lines
hw/usb/aspeed-udc.c +5 -1
      FIELD(EP_DMA_STS, PKT_SIZE,            16, 11)
      FIELD(EP_DMA_STS, RPTR,                 8, 8)
      FIELD(EP_DMA_STS, WPTR,                 0, 8)
 
-#define ASPEED_UDC_EP0_MAXPKT      64
+#define ASPEED_UDC_EP0_MAXPKT   64
+#define ASPEED_UDC_EP_MAXPKT    1024
+
+/* DMA descriptor ring (256-stage mode) and descriptor data limits */
+#define ASPEED_UDC_DESCS_COUNT  256 #define

ASPEED_UDC_DESC_MAX_LEN

hw/usb/aspeed-udc.c static void aspeed_udc_raise_isr(AspeedUDCState *s,

Quoted 23 lines
hw/usb/aspeed-udc.c +19 -0
+4096
+
+/* DMA processing-status idle codes */ #define
+EP_DMA_CTRL_STS_RX_IDLE 0x0 #define EP_DMA_CTRL_STS_TX_IDLE 0x8
+
+/* DMA descriptor (DES1) fields, in guest memory */
+#define ASPEED_EP_DESC1_IN_LEN(ctrl)   ((ctrl) & 0x1fff)
+/* interrupt-on-completion */
+#define ASPEED_EP_DESC1_INTR           BIT(31)
+
+/* Result of moving a host data packet through an endpoint's DMA */
+typedef enum {
+    /* whole packet transferred */
+    ASPEED_UDC_XFER_DONE,
+    /* not finished, keep parked */
+    ASPEED_UDC_XFER_MORE,
+    /* DMA failed */
+    ASPEED_UDC_XFER_ERROR,
+} AspeedUDCXferResult;
 
  static void aspeed_udc_update_irq(AspeedUDCState *s)
  {
@@ -97,6 +122,14 @@ static void aspeed_udc_raise_isr(AspeedUDCState *s,

uint32_t mask)

hw/usb/aspeed-udc.c static void aspeed_udc_raise_isr(AspeedUDCState *s, uint32_t mask)

Quoted 12 lines
hw/usb/aspeed-udc.c +7 -0
      aspeed_udc_update_irq(s);
  }
 
+static void aspeed_udc_raise_ep_ack(AspeedUDCState *s, int ep) {
+    trace_aspeed_udc_ep_ack(ep);
+    s->regs[R_UDC_EP_ACK_ISR] |= BIT(ep);
+    s->regs[R_UDC_ISR] |= R_UDC_ISR_EP_POOL_ACK_MASK;
+    aspeed_udc_update_irq(s);
+}
+
  /*
   * System bus device: MMIO register interface (guest gadget-driver

facing)

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops =

Quoted 2 lines
hw/usb/aspeed-udc.c +0 -0
   */
@@ -330,6 +363,288 @@ static const MemoryRegionOps aspeed_udc_ops =

{

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 9 lines
hw/usb/aspeed-udc.c +6 -0
      },
  };
 
+/*
+ * Copy len bytes from guest memory at addr into the IN packet, going
+through
+ * a bounce buffer one buf-full at a time. Returns false on DMA failure.
+ */
+static bool aspeed_udc_ep_copy_to_pkt(AspeedUDCState *s, int ep,

uint32_t addr,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 9 lines
hw/usb/aspeed-udc.c +9 -0
+                                      uint32_t len, USBPacket *p) {
+    uint8_t buf[ASPEED_UDC_EP_MAXPKT];
+    uint32_t copied = 0;
+    uint32_t seg;
+
+    while (copied < len) {
+        seg = MIN(len - copied, sizeof(buf));
+        if (address_space_read(&s->dram_as, addr + copied,
+                               MEMTXATTRS_UNSPECIFIED, buf,

seg) != MEMTX_OK) {

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 382

Quoted 2 lines
hw/usb/aspeed-udc.c +2 -0
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: ep %d IN data DMA read failed\n",

__func__,

Quoted 25 lines
hw/usb/aspeed-udc.c +25 -0
+                          ep);
+            return false;
+        }
+        usb_packet_copy(p, buf, seg);
+        copied += seg;
+    }
+
+    return true;
+}
+
+/*
+ * IN transfer: send data to the host by filling its IN packet from
+the
+ * buffers the guest gadget driver queued in the descriptor ring
+(from the
+ * read pointer to the write pointer).
+ *
+ * One host packet can be bigger than one descriptor's buffer, so we
+copy from
+ * several descriptors in a row until the packet is full or the ring is empty.
+ * If a descriptor is too big for the space left in the packet, we
+copy only
+ * part of it now and copy the rest on the next call; desc_off
+remembers how
+ * far we got. We move the read pointer to the next descriptor only

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+after a
+ * descriptor is fully copied, so the guest gadget driver can read
+the pointer
+ * and see how much was sent.
+ *
+ * This function raises the endpoint ACK by itself when the ring
+becomes empty
+ * or when a descriptor asks for an interrupt.
+ */
+static AspeedUDCXferResult aspeed_udc_ep_xfer_in(AspeedUDCState *s,

int ep,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 418

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+                                                 USBPacket *p)

{

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 3 lines
hw/usb/aspeed-udc.c +3 -0
+    QEMUIOVector *pktiov = p->combined ? &p->combined->iov : &p->iov;
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t mps = FIELD_EX32(e->regs[R_EP_CONFIG], EP_CONFIG,

MAX_PKT);

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 422

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+    uint32_t wptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS,

WPTR);

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 423

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+    uint32_t rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS,

RPTR);

Quoted 13 lines
hw/usb/aspeed-udc.c +13 -0
+    uint32_t desc_base = e->regs[R_EP_DMA_BUFF];
+    uint32_t desc_addr;
+    uint32_t remaining;
+    uint32_t desc_ctrl;
+    uint32_t pkt_space;
+    /* des_0: data buffer base address, des_1: control/status */
+    uint32_t desc[2];
+    uint32_t offset;
+    uint32_t chunk;
+    uint32_t dlen;
+    bool done = false;
+    bool ack = false;
+

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+    if (mps == 0) {
+        /* a MAX_PKT field of 0 means the maximum packet size */
+        mps = ASPEED_UDC_EP_MAXPKT;
+    }
+
+    trace_aspeed_udc_ep_data_in(ep, rptr, wptr, pktiov->size);
+
+    /* walk the queued descriptors, filling the packet */
+    while (rptr != wptr) {
+        if (address_space_read(&s->dram_as, desc_base + rptr *

sizeof(desc),

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 8 lines
hw/usb/aspeed-udc.c +8 -0
+                               MEMTXATTRS_UNSPECIFIED, desc,
+                               sizeof(desc)) != MEMTX_OK) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: ep %d descriptor DMA read failed\n",
+                          __func__, ep);
+            return ASPEED_UDC_XFER_ERROR;
+        }
+        desc_addr = le32_to_cpu(desc[0]) &

R_EP_DMA_BUFF_BASE_ADDR_MASK;

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 4 lines
hw/usb/aspeed-udc.c +4 -0
+        desc_ctrl = le32_to_cpu(desc[1]);
+        dlen = ASPEED_EP_DESC1_IN_LEN(desc_ctrl);
+        offset = e->desc_off;
+        /* how much to copy: min(descriptor bytes left, packet space left)

*/

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 6 lines
hw/usb/aspeed-udc.c +6 -0
+        remaining = dlen > offset ? dlen - offset : 0;
+        pkt_space = pktiov->size > (uint32_t)p->actual_length ?
+                    pktiov->size - (uint32_t)p->actual_length : 0;
+        chunk = MIN(remaining, pkt_space);
+
+        if (!aspeed_udc_ep_copy_to_pkt(s, ep, desc_addr + offset, chunk,

p)) {

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 7 lines
hw/usb/aspeed-udc.c +7 -0
+            return ASPEED_UDC_XFER_ERROR;
+        }
+        e->desc_off += chunk;
+
+        if (e->desc_off < dlen) {
+            /*
+             * The packet ran out of space in the middle of this

descriptor,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 472

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+             * so only part of it was copied. Stop here, and leave the

read

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 473

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+             * pointer on this descriptor: the next call resumes copying

the

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 8 lines
hw/usb/aspeed-udc.c +8 -0
+             * rest (desc_off remembers how far we got).
+             */
+            done = true;
+            break;
+        }
+
+        /*
+         * This descriptor was copied in full. Advance the read pointer to

the

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 482

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+         * next descriptor and reset desc_off so it starts from the

beginning.

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 9 lines
hw/usb/aspeed-udc.c +9 -0
+         */
+        rptr = (rptr + 1) % ASPEED_UDC_DESCS_COUNT;
+        e->desc_off = 0;
+        if (desc_ctrl & ASPEED_EP_DESC1_INTR) {
+            ack = true;
+        }
+        /*
+         * This descriptor is shorter than the max packet size, i.e. a short
+         * (or zero-length) packet. In USB that marks the end of the

transfer,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 8 lines
hw/usb/aspeed-udc.c +8 -0
+         * so stop here.
+         */
+        if (dlen < mps) {
+            done = true;
+            break;
+        }
+        /*
+         * The packet is now completely full, so the host has received all

the

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = { · line 548

Quoted 9 lines
hw/usb/aspeed-udc.c +9 -0
+         * data it asked for. Stop here.
+         */
+        if ((uint32_t)p->actual_length >= pktiov->size) {
+            done = true;
+            break;
+        }
+    }
+
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS],

EP_DMA_STS,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 510

Quoted 2 lines
hw/usb/aspeed-udc.c +2 -0
+                                       RPTR, rptr);
+    e->regs[R_EP_DMA_CTRL] = FIELD_DP32(e->regs[R_EP_DMA_CTRL],

EP_DMA_CTRL,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 511

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+                                        PROC_STS,

EP_DMA_CTRL_STS_TX_IDLE);

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 512

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+    /* The guest gadget driver completes its request when the ring drains

*/

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 8 lines
hw/usb/aspeed-udc.c +8 -0
+    if (rptr == wptr) {
+        ack = true;
+    }
+    if (ack) {
+        aspeed_udc_raise_ep_ack(s, ep);
+    }
+
+    return done ? ASPEED_UDC_XFER_DONE :

ASPEED_UDC_XFER_MORE; }

Quoted 4 lines
hw/usb/aspeed-udc.c +4 -0
+
+/*
+ * OUT transfer: receive data from the host by copying its OUT packet
+into the

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+ * buffer the guest gadget driver set up (single-stage mode).
+ *
+ * A host packet can be bigger than one buffer, so we copy at most
+PKT_SIZE
+ * bytes per call, continuing from where the last call stopped
+ * (p->actual_length). The caller keeps the packet parked until it is
+fully
+ * copied.
+ */
+static AspeedUDCXferResult aspeed_udc_ep_xfer_out(AspeedUDCState *s,

int ep,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 535

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+                                                  USBPacket *p)

{

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 538

Quoted 3 lines
hw/usb/aspeed-udc.c +3 -0
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t chunk = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS,
+PKT_SIZE);

PKT_SIZE is 2K -1, in practice 512 for USB 2.0 bulk.

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = { · line 533

Quoted 4 lines
hw/usb/aspeed-udc.c +4 -0
+    uint32_t remaining = p->iov.size - (uint32_t)p->actual_length;
+    uint32_t data_buf_addr = e->regs[R_EP_DMA_BUFF];
+    uint32_t len = MIN(remaining, chunk);
+    uint8_t buf[ASPEED_UDC_DESC_MAX_LEN];
Quoted 6 lines

This is 4k on the stack. we could just use :

g_autofree uint8_t *buf = g_malloc(len)

and drop the MIN() below.

Thanks for the review and suggestion.
Will do.

Quoted 157 lines
Quoted 6 lines
hw/usb/aspeed-udc.c +6 -0
+
+    if (data_buf_addr && len) {
+        len = MIN(len, sizeof(buf));
+        usb_packet_copy(p, buf, len);
+        if (address_space_write(&s->dram_as, data_buf_addr,
+                                MEMTXATTRS_UNSPECIFIED, buf,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+                                len) != MEMTX_OK) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: ep %d OUT data DMA write failed\n",
+                          __func__, ep);
+            return ASPEED_UDC_XFER_ERROR;
+        }
+    }
+
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS],
+                                       EP_DMA_STS, PKT_SIZE,

len);

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 3 lines
hw/usb/aspeed-udc.c +3 -0
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(e->regs[R_EP_DMA_STS],
+                                       EP_DMA_STS, WPTR, 0);
+    e->regs[R_EP_DMA_CTRL] = FIELD_DP32(e->regs[R_EP_DMA_CTRL],

EP_DMA_CTRL,

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 562

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+                                        PROC_STS,

EP_DMA_CTRL_STS_RX_IDLE);

Quoted 5 lines
hw/usb/aspeed-udc.c +5 -0
+    aspeed_udc_raise_ep_ack(s, ep);
+
+    if ((uint32_t)p->actual_length >= p->iov.size) {
+        return ASPEED_UDC_XFER_DONE;
+    }

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+
+    return ASPEED_UDC_XFER_MORE;
+}
+
+/*
+ * IN kick: the guest gadget driver wrote EP_DMA_STS to tell us it
+queued more
+ * IN data to send to the host. If a host IN request is already
+waiting
+ * (parked because there was no data before), send the data now and finish

it.

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 585

Quoted 8 lines
hw/usb/aspeed-udc.c +8 -0
+ * If the request needs more data than was queued, keep it parked and
+wait for
+ * the next kick.
+ */
+static void aspeed_udc_ep_in_kick(AspeedUDCState *s, int ep, uint32_t
+val) {
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t cur_rptr = FIELD_EX32(e->regs[R_EP_DMA_STS],

EP_DMA_STS, RPTR);

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 6 lines
hw/usb/aspeed-udc.c +6 -0
+    uint32_t new_rptr = FIELD_EX32(val, EP_DMA_STS, RPTR);
+    uint32_t new_wptr = FIELD_EX32(val, EP_DMA_STS, WPTR);
+    USBPacket *p = e->pkt;
+
+    /*
+     * A normal kick only sets the write pointer and leaves the

read-pointer

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 592

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+     * field 0 (the read pointer is ours to advance). The guest resets the

ring

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 4 lines
hw/usb/aspeed-udc.c +4 -0
+     * by writing a read pointer that is non-zero and equal to the write
+     * pointer.
+     *
+     * We check non-zero as well as equal: on a normal kick whose write

pointer

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 597

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+     * just wrapped back to 0, both fields would be 0, so an "equal" test

alone

Quoted 29 lines
hw/usb/aspeed-udc.c +29 -0
+     * would look like a reset by mistake.
+     */
+    if (new_rptr != 0 && new_rptr == new_wptr) {
+        cur_rptr = new_rptr;
+        e->desc_off = 0;
+    }
+    /* store the guest's write, but keep our own read pointer */
+    e->regs[R_EP_DMA_STS] = FIELD_DP32(val, EP_DMA_STS, RPTR,
+ cur_rptr);
+
+    /* nothing to do unless an IN packet is waiting and the ring has data */
+    if (!p || cur_rptr == new_wptr) {
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_in(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        e->pkt = NULL;
+        p->status = USB_RET_SUCCESS;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        e->pkt = NULL;
+        p->status = USB_RET_IOERROR;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        break;
+    }

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+}
+
+/*
+ * OUT kick: the guest gadget driver wrote EP_DMA_STS to give us a
+buffer for
+ * OUT data. If an OUT packet is already waiting (parked because
+there was no
+ * buffer before), copy its data into the buffer now and finish it.
+If the
+ * packet has more data than fits, keep it parked and wait for the next

buffer.

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 6 lines
hw/usb/aspeed-udc.c +6 -0
+ */
+static void aspeed_udc_ep_out_kick(AspeedUDCState *s, int ep) {
+    AspeedUDCEP *e = &s->ep[ep];
+    USBPacket *p = e->pkt;
+
+    /* nothing to do unless an OUT packet is waiting and a buffer is ready

*/

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = · line 643

Quoted 1 line
hw/usb/aspeed-udc.c +1 -0
+    if (!p || !FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS, WPTR))

{

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_out(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        e->pkt = NULL;
+        p->status = USB_RET_SUCCESS;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_ERROR:

hw/usb/aspeed-udc.c static const MemoryRegionOps aspeed_udc_ops = {

Quoted 10 lines
hw/usb/aspeed-udc.c +9 -0
+        e->pkt = NULL;
+        p->status = USB_RET_IOERROR;
+        usb_packet_complete(USB_DEVICE(s->usbgadget), p);
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        break;
+    }
+}
+
  static uint64_t aspeed_udc_ep_read(void *opaque, hwaddr offset,

unsigned size)

hw/usb/aspeed-udc.c static void aspeed_udc_ep_write(void *opaque,

Quoted 3 lines
  {
      AspeedUDCEP *e = opaque;
hw/usb/aspeed-udc.c +0 -0
@@ -346,10 +661,31 @@ static void aspeed_udc_ep_write(void *opaque,

hwaddr offset, uint64_t data,

Quoted 8 lines
hw/usb/aspeed-udc.c +3 -0
                                  unsigned size)
  {
      AspeedUDCEP *e = opaque;
+    AspeedUDCState *s = container_of(e - e->index, AspeedUDCState,
+ ep[0]);
      uint32_t reg = offset >> 2;
+    uint32_t val = data;
 

hw/usb/aspeed-udc.c static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data, · line 675

Quoted 10 lines
hw/usb/aspeed-udc.c +8 -2
-    trace_aspeed_udc_ep_write(e->index, offset, data);
-    e->regs[reg] = data;
+    trace_aspeed_udc_ep_write(e->index, offset, val);
+
+    switch (reg) {
+    case R_EP_DMA_BUFF:
+        e->regs[reg] = val & R_EP_DMA_BUFF_BASE_ADDR_MASK;
+        break;
+    case R_EP_DMA_STS:
+        val &= 0x77ffffff;
Quoted 1 line

0x07ffffff ?

0x77ffffff is intentional. Per the "EPP0C" register definition in the AST2600 A3 datasheet, only two bits are read-only; all the others are RW, so the write mask must keep them:

  bit 31    RO  Descriptor List Empty Flag   -> masked (0)
  bit 30    RW  Long idle interrupt status   -> kept
  bit 29:28 RW  Endpoint Data Toggle value   -> kept
  bit 27    RO  Reserved                     -> masked (0)
  bit 26:16 RW  Packet Size                  -> kept
  bit 15:8  RW  Descriptor List Read Pointer -> kept
  bit 7:0   RW  Descriptor List Write Pointer-> kept

Putting RW=1 / RO=0 together, from bit 31 down:

0111 0111 1111 1111 1111 1111 1111 1111 = 0x77ffffff

So only bit 31 (empty flag) and bit 27 (reserved) are cleared.

Thanks,
Jamin

Quoted 197 lines

hw/usb/aspeed-udc.c static void aspeed_udc_ep_write(void *opaque, · line 677

Quoted 3 lines
hw/usb/aspeed-udc.c +1 -0
+        if (FIELD_EX32(e->regs[R_EP_DMA_CTRL], EP_DMA_CTRL,

DESC_OP_EN)) {

Quoted 5 lines
hw/usb/aspeed-udc.c +5 -0
+            /* IN, descriptor-list mode */
+            aspeed_udc_ep_in_kick(s, e->index, val);
+        } else {
+            /* OUT, single-stage mode */
+            e->regs[reg] = val;

hw/usb/aspeed-udc.c static void aspeed_udc_ep_write(void *opaque, hwaddr offset, uint64_t data,

Quoted 10 lines
hw/usb/aspeed-udc.c +7 -0
+            aspeed_udc_ep_out_kick(s, e->index);
+        }
+        break;
+    default:
+        e->regs[reg] = val;
+        break;
+    }
  }
 
  static const MemoryRegionOps aspeed_udc_ep_ops = { @@ -375,6

+711,8

Quoted 10 lines

@@ static void aspeed_udc_reset_hold(Object *obj, ResetType type)

      memset(s->regs, 0, sizeof(s->regs));
      for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
          memset(s->ep[i].regs, 0, sizeof(s->ep[i].regs));
hw/usb/aspeed-udc.c +2 -0
+        s->ep[i].pkt = NULL;
+        s->ep[i].desc_off = 0;
      }
 
      /* Device-reset default: root, DMA and EP-pool soft-reset bits

set */ @@ -468,6 +806,95 @@ static void

aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 17 lines

* through the MMIO register interface above.
*/

hw/usb/aspeed-udc.c +14 -0
+static int aspeed_udc_find_ep(AspeedUDCState *s, int ep_nr, bool
+is_out) {
+    uint32_t cfg;
+    int i;
+
+    for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
+        cfg = s->ep[i].regs[R_EP_CONFIG];
+
+        if (!FIELD_EX32(cfg, EP_CONFIG, ENABLE) ||
+            FIELD_EX32(cfg, EP_CONFIG, EP_NUM) != ep_nr) {
+            continue;
+        }
+        if (FIELD_EX32(cfg, EP_CONFIG, DIR_OUT) == is_out) {
+            return i;

hw/usb/aspeed-udc.c static void aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+        }
+    }
+
+    return -1;
+}
+
+static void aspeed_udc_ep_data_in(AspeedUDCState *s, int ep,
+USBPacket *p) {
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t rptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS,

RPTR);

hw/usb/aspeed-udc.c static void aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 8 lines
hw/usb/aspeed-udc.c +8 -0
+    uint32_t wptr = FIELD_EX32(e->regs[R_EP_DMA_STS], EP_DMA_STS,
+WPTR);
+
+    if (rptr == wptr) {
+        /*
+         * No IN data is queued yet. Save the packet and return ASYNC
+         * instead of NAK. A NAK would make the host retry slowly.
+         * aspeed_udc_ep_in_kick() serves and completes this packet

later,

Quoted 18 lines
hw/usb/aspeed-udc.c +18 -0
+         * once the guest gadget driver queues descriptors.
+         */
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_in(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        p->status = USB_RET_SUCCESS;
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        /* not fully sent yet: save the packet, wait for more descriptors */
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        p->status = USB_RET_IOERROR;

hw/usb/aspeed-udc.c static void aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+        break;
+    }
+}
+
+static void aspeed_udc_ep_data_out(AspeedUDCState *s, int ep,
+USBPacket *p) {
+    AspeedUDCEP *e = &s->ep[ep];
+    uint32_t sts = e->regs[R_EP_DMA_STS];
+
+    trace_aspeed_udc_ep_data_out(ep, FIELD_EX32(sts, EP_DMA_STS,

WPTR),

Quoted 1 line

+ FIELD_EX32(sts, EP_DMA_STS,

PKT_SIZE),

hw/usb/aspeed-udc.c static void aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 5 lines
hw/usb/aspeed-udc.c +5 -0
+                                 p->iov.size);
+    if (!FIELD_EX32(sts, EP_DMA_STS, WPTR)) {
+        /*
+         * No OUT buffer is ready yet. Save the packet and return ASYNC
+         * instead of NAK. Writing now could use an old buffer address

and

Quoted 1 line

+ * lose the data (for example a mass-storage CBW). A NAK would

make

Quoted 4 lines
hw/usb/aspeed-udc.c +4 -0
+         * the host retry slowly. aspeed_udc_ep_out_kick() delivers this
+         * packet later, once the guest gadget driver sets up a buffer.
+         */
+        e->pkt = p;

hw/usb/aspeed-udc.c static void aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 10 lines
hw/usb/aspeed-udc.c +10 -0
+        p->status = USB_RET_ASYNC;
+        return;
+    }
+
+    switch (aspeed_udc_ep_xfer_out(s, ep, p)) {
+    case ASPEED_UDC_XFER_DONE:
+        p->status = USB_RET_SUCCESS;
+        break;
+    case ASPEED_UDC_XFER_MORE:
+        /* not fully received yet: save the packet, wait for the next buffer

*/

hw/usb/aspeed-udc.c static void aspeed_udc_class_init(ObjectClass *klass, const void *data)

Quoted 12 lines
hw/usb/aspeed-udc.c +9 -0
+        e->pkt = p;
+        p->status = USB_RET_ASYNC;
+        break;
+    case ASPEED_UDC_XFER_ERROR:
+        p->status = USB_RET_IOERROR;
+        break;
+    }
+}
+
  static void aspeed_udc_gadget_handle_reset(USBDevice *udev)
  {
      AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc; @@

-531,17

Quoted 4 lines

+958,37 @@ static void aspeed_udc_gadget_handle_control(USBDevice *udev, USBPacket *p,

static void aspeed_udc_gadget_handle_data(USBDevice *udev, USBPacket

*p)

Quoted 2 lines

{
- /* Programmable endpoint (bulk) transfers are added in a later patch.

*/

Quoted 10 lines
hw/usb/aspeed-udc.c +9 -1
-    p->status = USB_RET_STALL;
+    AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc;
+    bool is_out = (p->pid == USB_TOKEN_OUT);
+    int ep = aspeed_udc_find_ep(s, p->ep->nr, is_out);
+
+    trace_aspeed_udc_handle_data(p->ep->nr, is_out ? "OUT" : "IN",
+                                 p->iov.size, ep);
+    if (ep < 0) {
+        p->status = USB_RET_STALL;
+        return;

hw/usb/aspeed-udc.c static void aspeed_udc_gadget_handle_control(USBDevice *udev, USBPacket *p,

Quoted 10 lines
hw/usb/aspeed-udc.c +7 -0
+    }
+
+    if (is_out) {
+        aspeed_udc_ep_data_out(s, ep, p);
+    } else {
+        aspeed_udc_ep_data_in(s, ep, p);
+    }
  }
 
  static void aspeed_udc_gadget_cancel_packet(USBDevice *udev,

USBPacket *p)

Quoted 20 lines
  {
      AspeedUDCState *s = ASPEED_UDC_GADGET(udev)->udc;
hw/usb/aspeed-udc.c +6 -0
+    int i;
 
      if (s->ep0_packet == p) {
          s->ep0_packet = NULL;
      }
+    for (i = 0; i < ASPEED_UDC_NUM_EP; i++) {
+        if (s->ep[i].pkt == p) {
+            s->ep[i].pkt = NULL;
+        }
+    }
  }
 
  static void aspeed_udc_gadget_realize(USBDevice *udev, Error **errp)
hw/usb/trace-events index renamed from hw/usb/trace-events +0 -0

098c3d6179..80ead23358 100644

hw/usb/trace-events aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x" · line 389

Quoted 1 line
hw/usb/trace-events +0 -0
@@ -389,3 +389,7 @@ aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x"
  aspeed_udc_ep0_setup(uint8_t type, uint8_t req, uint16_t value, uint16_t

index, uint16_t length, int dir_in, int addr) "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength %d, dir_in %d, addr %d"

hw/usb/trace-events aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x" · line 390

Quoted 1 line
hw/usb/trace-events +0 -0
  aspeed_udc_ep0_ctrl_write(uint32_t val, int dir_in, uint32_t offset) "val

0x%x, dir_in %d, off %u"

hw/usb/trace-events aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x" · line 392

Quoted 2 lines
hw/usb/trace-events +1 -0
  aspeed_udc_ep0_complete(int dir_in, int actual) "dir_in %d, actual %d"
+aspeed_udc_handle_data(int ep_nr, const char *dir, uint32_t iov, int ep_idx)

"ep_nr %d, %s, iov %u, ep_idx %d"

hw/usb/trace-events aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x" · line 393

Quoted 1 line
hw/usb/trace-events +1 -0
+aspeed_udc_ep_data_in(unsigned ep, uint32_t rptr, uint32_t wptr, uint32_t

iov) "ep %u, rptr %u, wptr %u, iov %u"

hw/usb/trace-events aspeed_udc_reset(uint32_t ier) "bus reset, ier 0x%x" · line 394

Quoted 1 line
hw/usb/trace-events +1 -0
+aspeed_udc_ep_data_out(unsigned ep, uint32_t wptr, uint32_t avail,

uint32_t iov) "ep %u, wptr %u, avail %u, iov %u"

Quoted 1 line
hw/usb/trace-events +1 -0
+aspeed_udc_ep_ack(unsigned ep) "ep %u"