f375e62
From 26c73adc7b291c90f5975f7148092e4fc207f3dc Mon Sep 17 00:00:00 2001
c8dfc65
From: Gerd Hoffmann <kraxel@redhat.com>
c8dfc65
Date: Fri, 24 Aug 2012 14:13:08 +0200
5544c1b
Subject: [PATCH] xhci: iso xfer support
c8dfc65
c8dfc65
Add support for iso transfers.
c8dfc65
c8dfc65
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
c8dfc65
---
c8dfc65
 hw/usb/hcd-xhci.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++--------
c8dfc65
 1 file changed, 101 insertions(+), 16 deletions(-)
c8dfc65
c8dfc65
diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c
f375e62
index 88f71fc..f3f1d61 100644
c8dfc65
--- a/hw/usb/hcd-xhci.c
c8dfc65
+++ b/hw/usb/hcd-xhci.c
c8dfc65
@@ -325,9 +325,15 @@ typedef struct XHCITransfer {
c8dfc65
     unsigned int pkts;
c8dfc65
     unsigned int pktsize;
c8dfc65
     unsigned int cur_pkt;
c8dfc65
+
c8dfc65
+    uint64_t mfindex_kick;
c8dfc65
 } XHCITransfer;
c8dfc65
 
c8dfc65
 typedef struct XHCIEPContext {
c8dfc65
+    XHCIState *xhci;
c8dfc65
+    unsigned int slotid;
c8dfc65
+    unsigned int epid;
c8dfc65
+
c8dfc65
     XHCIRing ring;
c8dfc65
     unsigned int next_xfer;
c8dfc65
     unsigned int comp_xfer;
c8dfc65
@@ -337,6 +343,11 @@ typedef struct XHCIEPContext {
c8dfc65
     dma_addr_t pctx;
c8dfc65
     unsigned int max_psize;
c8dfc65
     uint32_t state;
c8dfc65
+
c8dfc65
+    /* iso xfer scheduling */
c8dfc65
+    unsigned int interval;
c8dfc65
+    int64_t mfindex_last;
c8dfc65
+    QEMUTimer *kick_timer;
c8dfc65
 } XHCIEPContext;
c8dfc65
 
c8dfc65
 typedef struct XHCISlot {
c8dfc65
@@ -856,6 +867,12 @@ static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx,
c8dfc65
     epctx->state = state;
c8dfc65
 }
c8dfc65
 
c8dfc65
+static void xhci_ep_kick_timer(void *opaque)
c8dfc65
+{
c8dfc65
+    XHCIEPContext *epctx = opaque;
c8dfc65
+    xhci_kick_ep(epctx->xhci, epctx->slotid, epctx->epid);
c8dfc65
+}
c8dfc65
+
c8dfc65
 static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid,
c8dfc65
                                unsigned int epid, dma_addr_t pctx,
c8dfc65
                                uint32_t *ctx)
c8dfc65
@@ -877,6 +894,9 @@ static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid,
c8dfc65
 
c8dfc65
     epctx = g_malloc(sizeof(XHCIEPContext));
c8dfc65
     memset(epctx, 0, sizeof(XHCIEPContext));
c8dfc65
+    epctx->xhci = xhci;
c8dfc65
+    epctx->slotid = slotid;
c8dfc65
+    epctx->epid = epid;
c8dfc65
 
c8dfc65
     slot->eps[epid-1] = epctx;
c8dfc65
 
c8dfc65
@@ -895,6 +915,10 @@ static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid,
c8dfc65
         usb_packet_init(&epctx->transfers[i].packet);
c8dfc65
     }
c8dfc65
 
c8dfc65
+    epctx->interval = 1 << (ctx[0] >> 16) & 0xff;
c8dfc65
+    epctx->mfindex_last = 0;
c8dfc65
+    epctx->kick_timer = qemu_new_timer_ns(vm_clock, xhci_ep_kick_timer, epctx);
c8dfc65
+
c8dfc65
     epctx->state = EP_RUNNING;
c8dfc65
     ctx[0] &= ~EP_STATE_MASK;
c8dfc65
     ctx[0] |= EP_RUNNING;
c8dfc65
@@ -934,6 +958,7 @@ static int xhci_ep_nuke_xfers(XHCIState *xhci, unsigned int slotid,
c8dfc65
         if (t->running_retry) {
c8dfc65
             t->running_retry = 0;
c8dfc65
             epctx->retry = NULL;
c8dfc65
+            qemu_del_timer(epctx->kick_timer);
c8dfc65
         }
c8dfc65
         if (t->trbs) {
c8dfc65
             g_free(t->trbs);
c8dfc65
@@ -969,6 +994,7 @@ static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid,
c8dfc65
 
c8dfc65
     xhci_set_ep_state(xhci, epctx, EP_DISABLED);
c8dfc65
 
c8dfc65
+    qemu_free_timer(epctx->kick_timer);
c8dfc65
     g_free(epctx);
c8dfc65
     slot->eps[epid-1] = NULL;
c8dfc65
 
5544c1b
@@ -1376,29 +1402,70 @@ static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer)
c8dfc65
     return 0;
c8dfc65
 }
c8dfc65
 
c8dfc65
+static void xhci_calc_iso_kick(XHCIState *xhci, XHCITransfer *xfer,
c8dfc65
+                               XHCIEPContext *epctx, uint64_t mfindex)
c8dfc65
+{
c8dfc65
+    if (xfer->trbs[0].control & TRB_TR_SIA) {
c8dfc65
+        uint64_t asap = ((mfindex + epctx->interval - 1) &
c8dfc65
+                         ~(epctx->interval-1));
c8dfc65
+        if (asap >= epctx->mfindex_last &&
c8dfc65
+            asap <= epctx->mfindex_last + epctx->interval * 4) {
c8dfc65
+            xfer->mfindex_kick = epctx->mfindex_last + epctx->interval;
c8dfc65
+        } else {
c8dfc65
+            xfer->mfindex_kick = asap;
c8dfc65
+        }
c8dfc65
+    } else {
c8dfc65
+        xfer->mfindex_kick = (xfer->trbs[0].control >> TRB_TR_FRAMEID_SHIFT)
c8dfc65
+            & TRB_TR_FRAMEID_MASK;
c8dfc65
+        xfer->mfindex_kick |= mfindex & ~0x3fff;
c8dfc65
+        if (xfer->mfindex_kick < mfindex) {
c8dfc65
+            xfer->mfindex_kick += 0x4000;
c8dfc65
+        }
c8dfc65
+    }
c8dfc65
+}
c8dfc65
+
c8dfc65
+static void xhci_check_iso_kick(XHCIState *xhci, XHCITransfer *xfer,
c8dfc65
+                                XHCIEPContext *epctx, uint64_t mfindex)
c8dfc65
+{
c8dfc65
+    if (xfer->mfindex_kick > mfindex) {
c8dfc65
+        qemu_mod_timer(epctx->kick_timer, qemu_get_clock_ns(vm_clock) +
c8dfc65
+                       (xfer->mfindex_kick - mfindex) * 125000);
c8dfc65
+        xfer->running_retry = 1;
c8dfc65
+    } else {
c8dfc65
+        epctx->mfindex_last = xfer->mfindex_kick;
c8dfc65
+        qemu_del_timer(epctx->kick_timer);
c8dfc65
+        xfer->running_retry = 0;
c8dfc65
+    }
c8dfc65
+}
c8dfc65
+
c8dfc65
+
c8dfc65
 static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx)
c8dfc65
 {
c8dfc65
+    uint64_t mfindex;
c8dfc65
     int ret;
c8dfc65
 
c8dfc65
     DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer->slotid, xfer->epid);
c8dfc65
 
c8dfc65
     xfer->in_xfer = epctx->type>>2;
c8dfc65
 
c8dfc65
-    if (epctx->type == ET_ISO_IN || epctx->type == ET_ISO_OUT) {
c8dfc65
-        xfer->pkts = 1;
c8dfc65
-    } else {
c8dfc65
-        xfer->pkts = 0;
c8dfc65
-    }
c8dfc65
-
c8dfc65
     switch(epctx->type) {
c8dfc65
     case ET_INTR_OUT:
c8dfc65
     case ET_INTR_IN:
c8dfc65
     case ET_BULK_OUT:
c8dfc65
     case ET_BULK_IN:
c8dfc65
+        xfer->pkts = 0;
c8dfc65
+        xfer->iso_xfer = false;
c8dfc65
         break;
c8dfc65
     case ET_ISO_OUT:
c8dfc65
     case ET_ISO_IN:
c8dfc65
-        FIXME();
c8dfc65
+        xfer->pkts = 1;
c8dfc65
+        xfer->iso_xfer = true;
c8dfc65
+        mfindex = xhci_mfindex_get(xhci);
c8dfc65
+        xhci_calc_iso_kick(xhci, xfer, epctx, mfindex);
c8dfc65
+        xhci_check_iso_kick(xhci, xfer, epctx, mfindex);
c8dfc65
+        if (xfer->running_retry) {
c8dfc65
+            return -1;
c8dfc65
+        }
c8dfc65
         break;
c8dfc65
     default:
c8dfc65
         fprintf(stderr, "xhci: unknown or unhandled EP "
5544c1b
@@ -1428,6 +1495,7 @@ static int xhci_fire_transfer(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext
c8dfc65
 static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, unsigned int epid)
c8dfc65
 {
c8dfc65
     XHCIEPContext *epctx;
c8dfc65
+    uint64_t mfindex;
c8dfc65
     int length;
c8dfc65
     int i;
c8dfc65
 
5544c1b
@@ -1447,20 +1515,35 @@ static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, unsigned int epid
c8dfc65
     }
c8dfc65
 
c8dfc65
     if (epctx->retry) {
c8dfc65
-        /* retry nak'ed transfer */
c8dfc65
         XHCITransfer *xfer = epctx->retry;
c8dfc65
         int result;
c8dfc65
 
c8dfc65
         trace_usb_xhci_xfer_retry(xfer);
c8dfc65
         assert(xfer->running_retry);
c8dfc65
-        if (xhci_setup_packet(xfer) < 0) {
c8dfc65
-            return;
c8dfc65
-        }
c8dfc65
-        result = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
c8dfc65
-        if (result == USB_RET_NAK) {
c8dfc65
-            return;
c8dfc65
+        if (xfer->iso_xfer) {
c8dfc65
+            /* retry delayed iso transfer */
c8dfc65
+            mfindex = xhci_mfindex_get(xhci);
c8dfc65
+            xhci_check_iso_kick(xhci, xfer, epctx, mfindex);
c8dfc65
+            if (xfer->running_retry) {
c8dfc65
+                return;
c8dfc65
+            }
c8dfc65
+            if (xhci_setup_packet(xfer) < 0) {
c8dfc65
+                return;
c8dfc65
+            }
c8dfc65
+            result = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
c8dfc65
+            assert(result != USB_RET_NAK);
c8dfc65
+            xhci_complete_packet(xfer, result);
c8dfc65
+        } else {
c8dfc65
+            /* retry nak'ed transfer */
c8dfc65
+            if (xhci_setup_packet(xfer) < 0) {
c8dfc65
+                return;
c8dfc65
+            }
c8dfc65
+            result = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet);
c8dfc65
+            if (result == USB_RET_NAK) {
c8dfc65
+                return;
c8dfc65
+            }
c8dfc65
+            xhci_complete_packet(xfer, result);
c8dfc65
         }
c8dfc65
-        xhci_complete_packet(xfer, result);
c8dfc65
         assert(!xfer->running_retry);
c8dfc65
         epctx->retry = NULL;
c8dfc65
     }
5544c1b
@@ -1512,7 +1595,9 @@ static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, unsigned int epid
c8dfc65
             if (xhci_fire_transfer(xhci, xfer, epctx) >= 0) {
c8dfc65
                 epctx->next_xfer = (epctx->next_xfer + 1) % TD_QUEUE;
c8dfc65
             } else {
c8dfc65
-                fprintf(stderr, "xhci: error firing data transfer\n");
c8dfc65
+                if (!xfer->iso_xfer) {
c8dfc65
+                    fprintf(stderr, "xhci: error firing data transfer\n");
c8dfc65
+                }
c8dfc65
             }
c8dfc65
         }
c8dfc65
 
c8dfc65
-- 
f375e62
1.8.0.2
c8dfc65