218e99
From e07526dd14669081a569bb6b1e3d72e1ee59ebad Mon Sep 17 00:00:00 2001
218e99
From: Gerd Hoffmann <kraxel@redhat.com>
218e99
Date: Wed, 7 Aug 2013 09:22:48 +0200
218e99
Subject: [PATCH 17/28] xhci: add live migration support
218e99
218e99
RH-Author: Gerd Hoffmann <kraxel@redhat.com>
218e99
Message-id: <1375867368-18979-6-git-send-email-kraxel@redhat.com>
218e99
Patchwork-id: 53041
218e99
O-Subject: [RHEL-7 qemu-kvm PATCH 5/5] xhci: add live migration support
218e99
Bugzilla: 838170
218e99
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
RH-Acked-by: Orit Wasserman <owasserm@redhat.com>
218e99
218e99
With all preparing pieces in place we can finally drop in
218e99
the vmstate structs and the postload function.
218e99
218e99
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
218e99
(cherry picked from commit 37352df30fbc38d1de464db8927536d5e36cf52a)
218e99
---
218e99
 hw/usb/hcd-xhci.c |  164 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
218e99
 1 file changed, 163 insertions(+), 1 deletion(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 hw/usb/hcd-xhci.c |  164 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
218e99
 1 files changed, 163 insertions(+), 1 deletions(-)
218e99
218e99
diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c
218e99
index 8224465..3d3d6c3 100644
218e99
--- a/hw/usb/hcd-xhci.c
218e99
+++ b/hw/usb/hcd-xhci.c
218e99
@@ -3386,9 +3386,171 @@ static int usb_xhci_initfn(struct PCIDevice *dev)
218e99
     return 0;
218e99
 }
218e99
 
218e99
+static int usb_xhci_post_load(void *opaque, int version_id)
218e99
+{
218e99
+    XHCIState *xhci = opaque;
218e99
+    XHCISlot *slot;
218e99
+    XHCIEPContext *epctx;
218e99
+    dma_addr_t dcbaap, pctx;
218e99
+    uint32_t slot_ctx[4];
218e99
+    uint32_t ep_ctx[5];
218e99
+    int slotid, epid, state, intr;
218e99
+
218e99
+    dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high);
218e99
+
218e99
+    for (slotid = 1; slotid <= xhci->numslots; slotid++) {
218e99
+        slot = &xhci->slots[slotid-1];
218e99
+        if (!slot->addressed) {
218e99
+            continue;
218e99
+        }
218e99
+        slot->ctx =
218e99
+            xhci_mask64(ldq_le_pci_dma(&xhci->pci_dev, dcbaap + 8*slotid));
218e99
+        xhci_dma_read_u32s(xhci, slot->ctx, slot_ctx, sizeof(slot_ctx));
218e99
+        slot->uport = xhci_lookup_uport(xhci, slot_ctx);
218e99
+        assert(slot->uport && slot->uport->dev);
218e99
+
218e99
+        for (epid = 1; epid <= 32; epid++) {
218e99
+            pctx = slot->ctx + 32 * epid;
218e99
+            xhci_dma_read_u32s(xhci, pctx, ep_ctx, sizeof(ep_ctx));
218e99
+            state = ep_ctx[0] & EP_STATE_MASK;
218e99
+            if (state == EP_DISABLED) {
218e99
+                continue;
218e99
+            }
218e99
+            epctx = xhci_alloc_epctx(xhci, slotid, epid);
218e99
+            slot->eps[epid-1] = epctx;
218e99
+            xhci_init_epctx(epctx, pctx, ep_ctx);
218e99
+            epctx->state = state;
218e99
+            if (state == EP_RUNNING) {
218e99
+                /* kick endpoint after vmload is finished */
218e99
+                qemu_mod_timer(epctx->kick_timer, qemu_get_clock_ns(vm_clock));
218e99
+            }
218e99
+        }
218e99
+    }
218e99
+
218e99
+    for (intr = 0; intr < xhci->numintrs; intr++) {
218e99
+        if (xhci->intr[intr].msix_used) {
218e99
+            msix_vector_use(&xhci->pci_dev, intr);
218e99
+        } else {
218e99
+            msix_vector_unuse(&xhci->pci_dev, intr);
218e99
+        }
218e99
+    }
218e99
+
218e99
+    return 0;
218e99
+}
218e99
+
218e99
+static const VMStateDescription vmstate_xhci_ring = {
218e99
+    .name = "xhci-ring",
218e99
+    .version_id = 1,
218e99
+    .fields = (VMStateField[]) {
218e99
+        VMSTATE_UINT64(dequeue, XHCIRing),
218e99
+        VMSTATE_BOOL(ccs, XHCIRing),
218e99
+        VMSTATE_END_OF_LIST()
218e99
+    }
218e99
+};
218e99
+
218e99
+static const VMStateDescription vmstate_xhci_port = {
218e99
+    .name = "xhci-port",
218e99
+    .version_id = 1,
218e99
+    .fields = (VMStateField[]) {
218e99
+        VMSTATE_UINT32(portsc, XHCIPort),
218e99
+        VMSTATE_END_OF_LIST()
218e99
+    }
218e99
+};
218e99
+
218e99
+static const VMStateDescription vmstate_xhci_slot = {
218e99
+    .name = "xhci-slot",
218e99
+    .version_id = 1,
218e99
+    .fields = (VMStateField[]) {
218e99
+        VMSTATE_BOOL(enabled,   XHCISlot),
218e99
+        VMSTATE_BOOL(addressed, XHCISlot),
218e99
+        VMSTATE_END_OF_LIST()
218e99
+    }
218e99
+};
218e99
+
218e99
+static const VMStateDescription vmstate_xhci_event = {
218e99
+    .name = "xhci-event",
218e99
+    .version_id = 1,
218e99
+    .fields = (VMStateField[]) {
218e99
+        VMSTATE_UINT32(type,   XHCIEvent),
218e99
+        VMSTATE_UINT32(ccode,  XHCIEvent),
218e99
+        VMSTATE_UINT64(ptr,    XHCIEvent),
218e99
+        VMSTATE_UINT32(length, XHCIEvent),
218e99
+        VMSTATE_UINT32(flags,  XHCIEvent),
218e99
+        VMSTATE_UINT8(slotid,  XHCIEvent),
218e99
+        VMSTATE_UINT8(epid,    XHCIEvent),
218e99
+    }
218e99
+};
218e99
+
218e99
+static bool xhci_er_full(void *opaque, int version_id)
218e99
+{
218e99
+    struct XHCIInterrupter *intr = opaque;
218e99
+    return intr->er_full;
218e99
+}
218e99
+
218e99
+static const VMStateDescription vmstate_xhci_intr = {
218e99
+    .name = "xhci-intr",
218e99
+    .version_id = 1,
218e99
+    .fields = (VMStateField[]) {
218e99
+        /* registers */
218e99
+        VMSTATE_UINT32(iman,          XHCIInterrupter),
218e99
+        VMSTATE_UINT32(imod,          XHCIInterrupter),
218e99
+        VMSTATE_UINT32(erstsz,        XHCIInterrupter),
218e99
+        VMSTATE_UINT32(erstba_low,    XHCIInterrupter),
218e99
+        VMSTATE_UINT32(erstba_high,   XHCIInterrupter),
218e99
+        VMSTATE_UINT32(erdp_low,      XHCIInterrupter),
218e99
+        VMSTATE_UINT32(erdp_high,     XHCIInterrupter),
218e99
+
218e99
+        /* state */
218e99
+        VMSTATE_BOOL(msix_used,       XHCIInterrupter),
218e99
+        VMSTATE_BOOL(er_pcs,          XHCIInterrupter),
218e99
+        VMSTATE_UINT64(er_start,      XHCIInterrupter),
218e99
+        VMSTATE_UINT32(er_size,       XHCIInterrupter),
218e99
+        VMSTATE_UINT32(er_ep_idx,     XHCIInterrupter),
218e99
+
218e99
+        /* event queue (used if ring is full) */
218e99
+        VMSTATE_BOOL(er_full,         XHCIInterrupter),
218e99
+        VMSTATE_UINT32_TEST(ev_buffer_put, XHCIInterrupter, xhci_er_full),
218e99
+        VMSTATE_UINT32_TEST(ev_buffer_get, XHCIInterrupter, xhci_er_full),
218e99
+        VMSTATE_STRUCT_ARRAY_TEST(ev_buffer, XHCIInterrupter, EV_QUEUE,
218e99
+                                  xhci_er_full, 1,
218e99
+                                  vmstate_xhci_event, XHCIEvent),
218e99
+
218e99
+        VMSTATE_END_OF_LIST()
218e99
+    }
218e99
+};
218e99
+
218e99
 static const VMStateDescription vmstate_xhci = {
218e99
     .name = "xhci",
218e99
-    .unmigratable = 1,
218e99
+    .version_id = 1,
218e99
+    .post_load = usb_xhci_post_load,
218e99
+    .fields = (VMStateField[]) {
218e99
+        VMSTATE_PCIE_DEVICE(pci_dev, XHCIState),
218e99
+        VMSTATE_MSIX(pci_dev, XHCIState),
218e99
+
218e99
+        VMSTATE_STRUCT_VARRAY_UINT32(ports, XHCIState, numports, 1,
218e99
+                                     vmstate_xhci_port, XHCIPort),
218e99
+        VMSTATE_STRUCT_VARRAY_UINT32(slots, XHCIState, numslots, 1,
218e99
+                                     vmstate_xhci_slot, XHCISlot),
218e99
+        VMSTATE_STRUCT_VARRAY_UINT32(intr, XHCIState, numintrs, 1,
218e99
+                                     vmstate_xhci_intr, XHCIInterrupter),
218e99
+
218e99
+        /* Operational Registers */
218e99
+        VMSTATE_UINT32(usbcmd,        XHCIState),
218e99
+        VMSTATE_UINT32(usbsts,        XHCIState),
218e99
+        VMSTATE_UINT32(dnctrl,        XHCIState),
218e99
+        VMSTATE_UINT32(crcr_low,      XHCIState),
218e99
+        VMSTATE_UINT32(crcr_high,     XHCIState),
218e99
+        VMSTATE_UINT32(dcbaap_low,    XHCIState),
218e99
+        VMSTATE_UINT32(dcbaap_high,   XHCIState),
218e99
+        VMSTATE_UINT32(config,        XHCIState),
218e99
+
218e99
+        /* Runtime Registers & state */
218e99
+        VMSTATE_INT64(mfindex_start,  XHCIState),
218e99
+        VMSTATE_TIMER(mfwrap_timer,   XHCIState),
218e99
+        VMSTATE_STRUCT(cmd_ring, XHCIState, 1, vmstate_xhci_ring, XHCIRing),
218e99
+
218e99
+        VMSTATE_END_OF_LIST()
218e99
+    }
218e99
 };
218e99
 
218e99
 static Property xhci_properties[] = {
218e99
-- 
218e99
1.7.1
218e99