1f1ae32a1SGerd Hoffmann /* 2f1ae32a1SGerd Hoffmann * USB UHCI controller emulation 3f1ae32a1SGerd Hoffmann * 4f1ae32a1SGerd Hoffmann * Copyright (c) 2005 Fabrice Bellard 5f1ae32a1SGerd Hoffmann * 6f1ae32a1SGerd Hoffmann * Copyright (c) 2008 Max Krasnyansky 7f1ae32a1SGerd Hoffmann * Magor rewrite of the UHCI data structures parser and frame processor 8f1ae32a1SGerd Hoffmann * Support for fully async operation and multiple outstanding transactions 9f1ae32a1SGerd Hoffmann * 10f1ae32a1SGerd Hoffmann * Permission is hereby granted, free of charge, to any person obtaining a copy 11f1ae32a1SGerd Hoffmann * of this software and associated documentation files (the "Software"), to deal 12f1ae32a1SGerd Hoffmann * in the Software without restriction, including without limitation the rights 13f1ae32a1SGerd Hoffmann * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14f1ae32a1SGerd Hoffmann * copies of the Software, and to permit persons to whom the Software is 15f1ae32a1SGerd Hoffmann * furnished to do so, subject to the following conditions: 16f1ae32a1SGerd Hoffmann * 17f1ae32a1SGerd Hoffmann * The above copyright notice and this permission notice shall be included in 18f1ae32a1SGerd Hoffmann * all copies or substantial portions of the Software. 19f1ae32a1SGerd Hoffmann * 20f1ae32a1SGerd Hoffmann * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21f1ae32a1SGerd Hoffmann * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22f1ae32a1SGerd Hoffmann * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23f1ae32a1SGerd Hoffmann * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24f1ae32a1SGerd Hoffmann * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25f1ae32a1SGerd Hoffmann * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26f1ae32a1SGerd Hoffmann * THE SOFTWARE. 27f1ae32a1SGerd Hoffmann */ 280b8fa32fSMarkus Armbruster 29e532b2e0SPeter Maydell #include "qemu/osdep.h" 30f1ae32a1SGerd Hoffmann #include "hw/usb.h" 319a1d111eSGerd Hoffmann #include "hw/usb/uhci-regs.h" 32d6454270SMarkus Armbruster #include "migration/vmstate.h" 33a2cb15b0SMichael S. Tsirkin #include "hw/pci/pci.h" 34*e4f5b939SBALATON Zoltan #include "hw/irq.h" 35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 36da34e65cSMarkus Armbruster #include "qapi/error.h" 371de7afc9SPaolo Bonzini #include "qemu/timer.h" 381de7afc9SPaolo Bonzini #include "qemu/iov.h" 399c17d615SPaolo Bonzini #include "sysemu/dma.h" 4050dcc0f8SGerd Hoffmann #include "trace.h" 416a1751b7SAlex Bligh #include "qemu/main-loop.h" 420b8fa32fSMarkus Armbruster #include "qemu/module.h" 43db1015e9SEduardo Habkost #include "qom/object.h" 449a4e12a6SPhilippe Mathieu-Daudé #include "hcd-uhci.h" 45f1ae32a1SGerd Hoffmann 46f1ae32a1SGerd Hoffmann #define FRAME_TIMER_FREQ 1000 47f1ae32a1SGerd Hoffmann 48f1ae32a1SGerd Hoffmann #define FRAME_MAX_LOOPS 256 49f1ae32a1SGerd Hoffmann 50475443cfSHans de Goede /* Must be large enough to handle 10 frame delay for initial isoc requests */ 51475443cfSHans de Goede #define QH_VALID 32 52475443cfSHans de Goede 53f8f48b69SHans de Goede #define MAX_FRAMES_PER_TICK (QH_VALID / 2) 54f8f48b69SHans de Goede 5560e1b2a6SGerd Hoffmann enum { 560cd178caSGerd Hoffmann TD_RESULT_STOP_FRAME = 10, 570cd178caSGerd Hoffmann TD_RESULT_COMPLETE, 580cd178caSGerd Hoffmann TD_RESULT_NEXT_QH, 594efe4ef3SGerd Hoffmann TD_RESULT_ASYNC_START, 604efe4ef3SGerd Hoffmann TD_RESULT_ASYNC_CONT, 6160e1b2a6SGerd Hoffmann }; 6260e1b2a6SGerd Hoffmann 63f1ae32a1SGerd Hoffmann typedef struct UHCIState UHCIState; 64f1ae32a1SGerd Hoffmann typedef struct UHCIAsync UHCIAsync; 658f3f90b0SGerd Hoffmann typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass; 662c2e8525SGerd Hoffmann 678f3f90b0SGerd Hoffmann struct UHCIPCIDeviceClass { 688f3f90b0SGerd Hoffmann PCIDeviceClass parent_class; 698f3f90b0SGerd Hoffmann UHCIInfo info; 708f3f90b0SGerd Hoffmann }; 718f3f90b0SGerd Hoffmann 72f1ae32a1SGerd Hoffmann /* 73f1ae32a1SGerd Hoffmann * Pending async transaction. 74f1ae32a1SGerd Hoffmann * 'packet' must be the first field because completion 75f1ae32a1SGerd Hoffmann * handler does "(UHCIAsync *) pkt" cast. 76f1ae32a1SGerd Hoffmann */ 77f1ae32a1SGerd Hoffmann 78f1ae32a1SGerd Hoffmann struct UHCIAsync { 79f1ae32a1SGerd Hoffmann USBPacket packet; 809822261cSHans de Goede uint8_t static_buf[64]; /* 64 bytes is enough, except for isoc packets */ 819822261cSHans de Goede uint8_t *buf; 82f1ae32a1SGerd Hoffmann UHCIQueue *queue; 83f1ae32a1SGerd Hoffmann QTAILQ_ENTRY(UHCIAsync) next; 841f250cc7SHans de Goede uint32_t td_addr; 85f1ae32a1SGerd Hoffmann uint8_t done; 86f1ae32a1SGerd Hoffmann }; 87f1ae32a1SGerd Hoffmann 88f1ae32a1SGerd Hoffmann struct UHCIQueue { 8966a08cbeSHans de Goede uint32_t qh_addr; 90f1ae32a1SGerd Hoffmann uint32_t token; 91f1ae32a1SGerd Hoffmann UHCIState *uhci; 9211d15e40SHans de Goede USBEndpoint *ep; 93f1ae32a1SGerd Hoffmann QTAILQ_ENTRY(UHCIQueue) next; 94eae3eb3eSPaolo Bonzini QTAILQ_HEAD(, UHCIAsync) asyncs; 95f1ae32a1SGerd Hoffmann int8_t valid; 96f1ae32a1SGerd Hoffmann }; 97f1ae32a1SGerd Hoffmann 98f1ae32a1SGerd Hoffmann typedef struct UHCI_TD { 99f1ae32a1SGerd Hoffmann uint32_t link; 100f1ae32a1SGerd Hoffmann uint32_t ctrl; /* see TD_CTRL_xxx */ 101f1ae32a1SGerd Hoffmann uint32_t token; 102f1ae32a1SGerd Hoffmann uint32_t buffer; 103f1ae32a1SGerd Hoffmann } UHCI_TD; 104f1ae32a1SGerd Hoffmann 105f1ae32a1SGerd Hoffmann typedef struct UHCI_QH { 106f1ae32a1SGerd Hoffmann uint32_t link; 107f1ae32a1SGerd Hoffmann uint32_t el_link; 108f1ae32a1SGerd Hoffmann } UHCI_QH; 109f1ae32a1SGerd Hoffmann 11040507377SHans de Goede static void uhci_async_cancel(UHCIAsync *async); 11111d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td); 1129f0f1a0cSGerd Hoffmann static void uhci_resume(void *opaque); 11340507377SHans de Goede 114f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td) 115f1ae32a1SGerd Hoffmann { 1166fe30910SHans de Goede if ((td->token & (0xf << 15)) == 0) { 1176fe30910SHans de Goede /* ctrl ep, cover ep and dev, not pid! */ 1186fe30910SHans de Goede return td->token & 0x7ff00; 1196fe30910SHans de Goede } else { 120f1ae32a1SGerd Hoffmann /* covers ep, dev, pid -> identifies the endpoint */ 121f1ae32a1SGerd Hoffmann return td->token & 0x7ffff; 122f1ae32a1SGerd Hoffmann } 1236fe30910SHans de Goede } 124f1ae32a1SGerd Hoffmann 12566a08cbeSHans de Goede static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td, 12666a08cbeSHans de Goede USBEndpoint *ep) 127f1ae32a1SGerd Hoffmann { 128f1ae32a1SGerd Hoffmann UHCIQueue *queue; 129f1ae32a1SGerd Hoffmann 130f1ae32a1SGerd Hoffmann queue = g_new0(UHCIQueue, 1); 131f1ae32a1SGerd Hoffmann queue->uhci = s; 13266a08cbeSHans de Goede queue->qh_addr = qh_addr; 13366a08cbeSHans de Goede queue->token = uhci_queue_token(td); 13411d15e40SHans de Goede queue->ep = ep; 135f1ae32a1SGerd Hoffmann QTAILQ_INIT(&queue->asyncs); 136f1ae32a1SGerd Hoffmann QTAILQ_INSERT_HEAD(&s->queues, queue, next); 137475443cfSHans de Goede queue->valid = QH_VALID; 13850dcc0f8SGerd Hoffmann trace_usb_uhci_queue_add(queue->token); 139f1ae32a1SGerd Hoffmann return queue; 140f1ae32a1SGerd Hoffmann } 141f1ae32a1SGerd Hoffmann 14266a08cbeSHans de Goede static void uhci_queue_free(UHCIQueue *queue, const char *reason) 143f1ae32a1SGerd Hoffmann { 144f1ae32a1SGerd Hoffmann UHCIState *s = queue->uhci; 14540507377SHans de Goede UHCIAsync *async; 14640507377SHans de Goede 14740507377SHans de Goede while (!QTAILQ_EMPTY(&queue->asyncs)) { 14840507377SHans de Goede async = QTAILQ_FIRST(&queue->asyncs); 14940507377SHans de Goede uhci_async_cancel(async); 15040507377SHans de Goede } 151f79738b0SHans de Goede usb_device_ep_stopped(queue->ep->dev, queue->ep); 152f1ae32a1SGerd Hoffmann 15366a08cbeSHans de Goede trace_usb_uhci_queue_del(queue->token, reason); 154f1ae32a1SGerd Hoffmann QTAILQ_REMOVE(&s->queues, queue, next); 155f1ae32a1SGerd Hoffmann g_free(queue); 156f1ae32a1SGerd Hoffmann } 157f1ae32a1SGerd Hoffmann 15866a08cbeSHans de Goede static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td) 15966a08cbeSHans de Goede { 16066a08cbeSHans de Goede uint32_t token = uhci_queue_token(td); 16166a08cbeSHans de Goede UHCIQueue *queue; 16266a08cbeSHans de Goede 16366a08cbeSHans de Goede QTAILQ_FOREACH(queue, &s->queues, next) { 16466a08cbeSHans de Goede if (queue->token == token) { 16566a08cbeSHans de Goede return queue; 16666a08cbeSHans de Goede } 16766a08cbeSHans de Goede } 16866a08cbeSHans de Goede return NULL; 16966a08cbeSHans de Goede } 17066a08cbeSHans de Goede 17166a08cbeSHans de Goede static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td, 17266a08cbeSHans de Goede uint32_t td_addr, bool queuing) 17366a08cbeSHans de Goede { 17466a08cbeSHans de Goede UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs); 175c348e481SGerd Hoffmann uint32_t queue_token_addr = (queue->token >> 8) & 0x7f; 17666a08cbeSHans de Goede 17766a08cbeSHans de Goede return queue->qh_addr == qh_addr && 17866a08cbeSHans de Goede queue->token == uhci_queue_token(td) && 179c348e481SGerd Hoffmann queue_token_addr == queue->ep->dev->addr && 18066a08cbeSHans de Goede (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL || 18166a08cbeSHans de Goede first->td_addr == td_addr); 18266a08cbeSHans de Goede } 18366a08cbeSHans de Goede 1841f250cc7SHans de Goede static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr) 185f1ae32a1SGerd Hoffmann { 186f1ae32a1SGerd Hoffmann UHCIAsync *async = g_new0(UHCIAsync, 1); 187f1ae32a1SGerd Hoffmann 188f1ae32a1SGerd Hoffmann async->queue = queue; 1891f250cc7SHans de Goede async->td_addr = td_addr; 190f1ae32a1SGerd Hoffmann usb_packet_init(&async->packet); 1911f250cc7SHans de Goede trace_usb_uhci_packet_add(async->queue->token, async->td_addr); 192f1ae32a1SGerd Hoffmann 193f1ae32a1SGerd Hoffmann return async; 194f1ae32a1SGerd Hoffmann } 195f1ae32a1SGerd Hoffmann 196f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async) 197f1ae32a1SGerd Hoffmann { 1981f250cc7SHans de Goede trace_usb_uhci_packet_del(async->queue->token, async->td_addr); 199f1ae32a1SGerd Hoffmann usb_packet_cleanup(&async->packet); 2009822261cSHans de Goede if (async->buf != async->static_buf) { 2019822261cSHans de Goede g_free(async->buf); 2029822261cSHans de Goede } 203f1ae32a1SGerd Hoffmann g_free(async); 204f1ae32a1SGerd Hoffmann } 205f1ae32a1SGerd Hoffmann 206f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async) 207f1ae32a1SGerd Hoffmann { 208f1ae32a1SGerd Hoffmann UHCIQueue *queue = async->queue; 209f1ae32a1SGerd Hoffmann QTAILQ_INSERT_TAIL(&queue->asyncs, async, next); 2101f250cc7SHans de Goede trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr); 211f1ae32a1SGerd Hoffmann } 212f1ae32a1SGerd Hoffmann 213f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async) 214f1ae32a1SGerd Hoffmann { 215f1ae32a1SGerd Hoffmann UHCIQueue *queue = async->queue; 216f1ae32a1SGerd Hoffmann QTAILQ_REMOVE(&queue->asyncs, async, next); 2171f250cc7SHans de Goede trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr); 218f1ae32a1SGerd Hoffmann } 219f1ae32a1SGerd Hoffmann 220f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async) 221f1ae32a1SGerd Hoffmann { 2222f2ee268SHans de Goede uhci_async_unlink(async); 2231f250cc7SHans de Goede trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr, 2241f250cc7SHans de Goede async->done); 225f1ae32a1SGerd Hoffmann if (!async->done) 226f1ae32a1SGerd Hoffmann usb_cancel_packet(&async->packet); 227f1ae32a1SGerd Hoffmann uhci_async_free(async); 228f1ae32a1SGerd Hoffmann } 229f1ae32a1SGerd Hoffmann 230f1ae32a1SGerd Hoffmann /* 231f1ae32a1SGerd Hoffmann * Mark all outstanding async packets as invalid. 232f1ae32a1SGerd Hoffmann * This is used for canceling them when TDs are removed by the HCD. 233f1ae32a1SGerd Hoffmann */ 234f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s) 235f1ae32a1SGerd Hoffmann { 236f1ae32a1SGerd Hoffmann UHCIQueue *queue; 237f1ae32a1SGerd Hoffmann 238f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(queue, &s->queues, next) { 239f1ae32a1SGerd Hoffmann queue->valid--; 240f1ae32a1SGerd Hoffmann } 241f1ae32a1SGerd Hoffmann } 242f1ae32a1SGerd Hoffmann 243f1ae32a1SGerd Hoffmann /* 244f1ae32a1SGerd Hoffmann * Cancel async packets that are no longer valid 245f1ae32a1SGerd Hoffmann */ 246f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s) 247f1ae32a1SGerd Hoffmann { 248f1ae32a1SGerd Hoffmann UHCIQueue *queue, *n; 249f1ae32a1SGerd Hoffmann 250f1ae32a1SGerd Hoffmann QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) { 25140507377SHans de Goede if (!queue->valid) { 25266a08cbeSHans de Goede uhci_queue_free(queue, "validate-end"); 253f1ae32a1SGerd Hoffmann } 254f1ae32a1SGerd Hoffmann } 25540507377SHans de Goede } 256f1ae32a1SGerd Hoffmann 257f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev) 258f1ae32a1SGerd Hoffmann { 2595ad23e87SHans de Goede UHCIQueue *queue, *n; 260f1ae32a1SGerd Hoffmann 2615ad23e87SHans de Goede QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) { 2625ad23e87SHans de Goede if (queue->ep->dev == dev) { 2635ad23e87SHans de Goede uhci_queue_free(queue, "cancel-device"); 264f1ae32a1SGerd Hoffmann } 265f1ae32a1SGerd Hoffmann } 266f1ae32a1SGerd Hoffmann } 267f1ae32a1SGerd Hoffmann 268f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s) 269f1ae32a1SGerd Hoffmann { 27077fa9aeeSGerd Hoffmann UHCIQueue *queue, *nq; 271f1ae32a1SGerd Hoffmann 27277fa9aeeSGerd Hoffmann QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) { 27366a08cbeSHans de Goede uhci_queue_free(queue, "cancel-all"); 274f1ae32a1SGerd Hoffmann } 275f1ae32a1SGerd Hoffmann } 276f1ae32a1SGerd Hoffmann 2778c75a899SHans de Goede static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr) 278f1ae32a1SGerd Hoffmann { 279f1ae32a1SGerd Hoffmann UHCIQueue *queue; 280f1ae32a1SGerd Hoffmann UHCIAsync *async; 281f1ae32a1SGerd Hoffmann 282f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(queue, &s->queues, next) { 283f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(async, &queue->asyncs, next) { 2841f250cc7SHans de Goede if (async->td_addr == td_addr) { 285f1ae32a1SGerd Hoffmann return async; 286f1ae32a1SGerd Hoffmann } 287f1ae32a1SGerd Hoffmann } 2888c75a899SHans de Goede } 289f1ae32a1SGerd Hoffmann return NULL; 290f1ae32a1SGerd Hoffmann } 291f1ae32a1SGerd Hoffmann 292f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s) 293f1ae32a1SGerd Hoffmann { 294d3647ef1SBALATON Zoltan int level = 0; 295f1ae32a1SGerd Hoffmann if (((s->status2 & 1) && (s->intr & (1 << 2))) || 296f1ae32a1SGerd Hoffmann ((s->status2 & 2) && (s->intr & (1 << 3))) || 297f1ae32a1SGerd Hoffmann ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) || 298f1ae32a1SGerd Hoffmann ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) || 299f1ae32a1SGerd Hoffmann (s->status & UHCI_STS_HSERR) || 300f1ae32a1SGerd Hoffmann (s->status & UHCI_STS_HCPERR)) { 301f1ae32a1SGerd Hoffmann level = 1; 302f1ae32a1SGerd Hoffmann } 303*e4f5b939SBALATON Zoltan qemu_set_irq(s->irq, level); 304f1ae32a1SGerd Hoffmann } 305f1ae32a1SGerd Hoffmann 306537e572aSGonglei static void uhci_reset(DeviceState *dev) 307f1ae32a1SGerd Hoffmann { 308537e572aSGonglei PCIDevice *d = PCI_DEVICE(dev); 30949184b62SGonglei UHCIState *s = UHCI(d); 310f1ae32a1SGerd Hoffmann uint8_t *pci_conf; 311f1ae32a1SGerd Hoffmann int i; 312f1ae32a1SGerd Hoffmann UHCIPort *port; 313f1ae32a1SGerd Hoffmann 31450dcc0f8SGerd Hoffmann trace_usb_uhci_reset(); 315f1ae32a1SGerd Hoffmann 316f1ae32a1SGerd Hoffmann pci_conf = s->dev.config; 317f1ae32a1SGerd Hoffmann 318f1ae32a1SGerd Hoffmann pci_conf[0x6a] = 0x01; /* usb clock */ 319f1ae32a1SGerd Hoffmann pci_conf[0x6b] = 0x00; 320f1ae32a1SGerd Hoffmann s->cmd = 0; 321ca5a21c4SGerd Hoffmann s->status = UHCI_STS_HCHALTED; 322f1ae32a1SGerd Hoffmann s->status2 = 0; 323f1ae32a1SGerd Hoffmann s->intr = 0; 324f1ae32a1SGerd Hoffmann s->fl_base_addr = 0; 325f1ae32a1SGerd Hoffmann s->sof_timing = 64; 326f1ae32a1SGerd Hoffmann 327f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 328f1ae32a1SGerd Hoffmann port = &s->ports[i]; 329f1ae32a1SGerd Hoffmann port->ctrl = 0x0080; 330f1ae32a1SGerd Hoffmann if (port->port.dev && port->port.dev->attached) { 331f1ae32a1SGerd Hoffmann usb_port_reset(&port->port); 332f1ae32a1SGerd Hoffmann } 333f1ae32a1SGerd Hoffmann } 334f1ae32a1SGerd Hoffmann 335f1ae32a1SGerd Hoffmann uhci_async_cancel_all(s); 3369a16c595SGerd Hoffmann qemu_bh_cancel(s->bh); 337aba1f242SGerd Hoffmann uhci_update_irq(s); 338f1ae32a1SGerd Hoffmann } 339f1ae32a1SGerd Hoffmann 340f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = { 341f1ae32a1SGerd Hoffmann .name = "uhci port", 342f1ae32a1SGerd Hoffmann .version_id = 1, 343f1ae32a1SGerd Hoffmann .minimum_version_id = 1, 344f1ae32a1SGerd Hoffmann .fields = (VMStateField[]) { 345f1ae32a1SGerd Hoffmann VMSTATE_UINT16(ctrl, UHCIPort), 346f1ae32a1SGerd Hoffmann VMSTATE_END_OF_LIST() 347f1ae32a1SGerd Hoffmann } 348f1ae32a1SGerd Hoffmann }; 349f1ae32a1SGerd Hoffmann 35075f151cdSGerd Hoffmann static int uhci_post_load(void *opaque, int version_id) 35175f151cdSGerd Hoffmann { 35275f151cdSGerd Hoffmann UHCIState *s = opaque; 35375f151cdSGerd Hoffmann 35475f151cdSGerd Hoffmann if (version_id < 2) { 355bc72ad67SAlex Bligh s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 35673bcb24dSRutuja Shah (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ); 35775f151cdSGerd Hoffmann } 35875f151cdSGerd Hoffmann return 0; 35975f151cdSGerd Hoffmann } 36075f151cdSGerd Hoffmann 361f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = { 362f1ae32a1SGerd Hoffmann .name = "uhci", 363ecfdc15fSHans de Goede .version_id = 3, 364f1ae32a1SGerd Hoffmann .minimum_version_id = 1, 36575f151cdSGerd Hoffmann .post_load = uhci_post_load, 366f1ae32a1SGerd Hoffmann .fields = (VMStateField[]) { 367f1ae32a1SGerd Hoffmann VMSTATE_PCI_DEVICE(dev, UHCIState), 368d2164ad3SHalil Pasic VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState, NULL), 369f1ae32a1SGerd Hoffmann VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1, 370f1ae32a1SGerd Hoffmann vmstate_uhci_port, UHCIPort), 371f1ae32a1SGerd Hoffmann VMSTATE_UINT16(cmd, UHCIState), 372f1ae32a1SGerd Hoffmann VMSTATE_UINT16(status, UHCIState), 373f1ae32a1SGerd Hoffmann VMSTATE_UINT16(intr, UHCIState), 374f1ae32a1SGerd Hoffmann VMSTATE_UINT16(frnum, UHCIState), 375f1ae32a1SGerd Hoffmann VMSTATE_UINT32(fl_base_addr, UHCIState), 376f1ae32a1SGerd Hoffmann VMSTATE_UINT8(sof_timing, UHCIState), 377f1ae32a1SGerd Hoffmann VMSTATE_UINT8(status2, UHCIState), 378e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(frame_timer, UHCIState), 379f1ae32a1SGerd Hoffmann VMSTATE_INT64_V(expire_time, UHCIState, 2), 380ecfdc15fSHans de Goede VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3), 381f1ae32a1SGerd Hoffmann VMSTATE_END_OF_LIST() 382f1ae32a1SGerd Hoffmann } 383f1ae32a1SGerd Hoffmann }; 384f1ae32a1SGerd Hoffmann 38589eb147cSGerd Hoffmann static void uhci_port_write(void *opaque, hwaddr addr, 38689eb147cSGerd Hoffmann uint64_t val, unsigned size) 387f1ae32a1SGerd Hoffmann { 388f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 389f1ae32a1SGerd Hoffmann 39050dcc0f8SGerd Hoffmann trace_usb_uhci_mmio_writew(addr, val); 391f1ae32a1SGerd Hoffmann 392f1ae32a1SGerd Hoffmann switch(addr) { 393f1ae32a1SGerd Hoffmann case 0x00: 394f1ae32a1SGerd Hoffmann if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) { 395f1ae32a1SGerd Hoffmann /* start frame processing */ 39650dcc0f8SGerd Hoffmann trace_usb_uhci_schedule_start(); 397bc72ad67SAlex Bligh s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 39873bcb24dSRutuja Shah (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ); 399bc72ad67SAlex Bligh timer_mod(s->frame_timer, s->expire_time); 400f1ae32a1SGerd Hoffmann s->status &= ~UHCI_STS_HCHALTED; 401f1ae32a1SGerd Hoffmann } else if (!(val & UHCI_CMD_RS)) { 402f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCHALTED; 403f1ae32a1SGerd Hoffmann } 404f1ae32a1SGerd Hoffmann if (val & UHCI_CMD_GRESET) { 405f1ae32a1SGerd Hoffmann UHCIPort *port; 406f1ae32a1SGerd Hoffmann int i; 407f1ae32a1SGerd Hoffmann 408f1ae32a1SGerd Hoffmann /* send reset on the USB bus */ 409f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 410f1ae32a1SGerd Hoffmann port = &s->ports[i]; 411f1ae32a1SGerd Hoffmann usb_device_reset(port->port.dev); 412f1ae32a1SGerd Hoffmann } 413537e572aSGonglei uhci_reset(DEVICE(s)); 414f1ae32a1SGerd Hoffmann return; 415f1ae32a1SGerd Hoffmann } 416f1ae32a1SGerd Hoffmann if (val & UHCI_CMD_HCRESET) { 417537e572aSGonglei uhci_reset(DEVICE(s)); 418f1ae32a1SGerd Hoffmann return; 419f1ae32a1SGerd Hoffmann } 420f1ae32a1SGerd Hoffmann s->cmd = val; 4219f0f1a0cSGerd Hoffmann if (val & UHCI_CMD_EGSM) { 4229f0f1a0cSGerd Hoffmann if ((s->ports[0].ctrl & UHCI_PORT_RD) || 4239f0f1a0cSGerd Hoffmann (s->ports[1].ctrl & UHCI_PORT_RD)) { 4249f0f1a0cSGerd Hoffmann uhci_resume(s); 4259f0f1a0cSGerd Hoffmann } 4269f0f1a0cSGerd Hoffmann } 427f1ae32a1SGerd Hoffmann break; 428f1ae32a1SGerd Hoffmann case 0x02: 429f1ae32a1SGerd Hoffmann s->status &= ~val; 430f1ae32a1SGerd Hoffmann /* XXX: the chip spec is not coherent, so we add a hidden 431f1ae32a1SGerd Hoffmann register to distinguish between IOC and SPD */ 432f1ae32a1SGerd Hoffmann if (val & UHCI_STS_USBINT) 433f1ae32a1SGerd Hoffmann s->status2 = 0; 434f1ae32a1SGerd Hoffmann uhci_update_irq(s); 435f1ae32a1SGerd Hoffmann break; 436f1ae32a1SGerd Hoffmann case 0x04: 437f1ae32a1SGerd Hoffmann s->intr = val; 438f1ae32a1SGerd Hoffmann uhci_update_irq(s); 439f1ae32a1SGerd Hoffmann break; 440f1ae32a1SGerd Hoffmann case 0x06: 441f1ae32a1SGerd Hoffmann if (s->status & UHCI_STS_HCHALTED) 442f1ae32a1SGerd Hoffmann s->frnum = val & 0x7ff; 443f1ae32a1SGerd Hoffmann break; 44489eb147cSGerd Hoffmann case 0x08: 44589eb147cSGerd Hoffmann s->fl_base_addr &= 0xffff0000; 44689eb147cSGerd Hoffmann s->fl_base_addr |= val & ~0xfff; 44789eb147cSGerd Hoffmann break; 44889eb147cSGerd Hoffmann case 0x0a: 44989eb147cSGerd Hoffmann s->fl_base_addr &= 0x0000ffff; 45089eb147cSGerd Hoffmann s->fl_base_addr |= (val << 16); 45189eb147cSGerd Hoffmann break; 45289eb147cSGerd Hoffmann case 0x0c: 45389eb147cSGerd Hoffmann s->sof_timing = val & 0xff; 45489eb147cSGerd Hoffmann break; 455f1ae32a1SGerd Hoffmann case 0x10 ... 0x1f: 456f1ae32a1SGerd Hoffmann { 457f1ae32a1SGerd Hoffmann UHCIPort *port; 458f1ae32a1SGerd Hoffmann USBDevice *dev; 459f1ae32a1SGerd Hoffmann int n; 460f1ae32a1SGerd Hoffmann 461f1ae32a1SGerd Hoffmann n = (addr >> 1) & 7; 462f1ae32a1SGerd Hoffmann if (n >= NB_PORTS) 463f1ae32a1SGerd Hoffmann return; 464f1ae32a1SGerd Hoffmann port = &s->ports[n]; 465f1ae32a1SGerd Hoffmann dev = port->port.dev; 466f1ae32a1SGerd Hoffmann if (dev && dev->attached) { 467f1ae32a1SGerd Hoffmann /* port reset */ 468f1ae32a1SGerd Hoffmann if ( (val & UHCI_PORT_RESET) && 469f1ae32a1SGerd Hoffmann !(port->ctrl & UHCI_PORT_RESET) ) { 470f1ae32a1SGerd Hoffmann usb_device_reset(dev); 471f1ae32a1SGerd Hoffmann } 472f1ae32a1SGerd Hoffmann } 473f1ae32a1SGerd Hoffmann port->ctrl &= UHCI_PORT_READ_ONLY; 4741cbdde90SHans de Goede /* enabled may only be set if a device is connected */ 4751cbdde90SHans de Goede if (!(port->ctrl & UHCI_PORT_CCS)) { 4761cbdde90SHans de Goede val &= ~UHCI_PORT_EN; 4771cbdde90SHans de Goede } 478f1ae32a1SGerd Hoffmann port->ctrl |= (val & ~UHCI_PORT_READ_ONLY); 479f1ae32a1SGerd Hoffmann /* some bits are reset when a '1' is written to them */ 480f1ae32a1SGerd Hoffmann port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR); 481f1ae32a1SGerd Hoffmann } 482f1ae32a1SGerd Hoffmann break; 483f1ae32a1SGerd Hoffmann } 484f1ae32a1SGerd Hoffmann } 485f1ae32a1SGerd Hoffmann 48689eb147cSGerd Hoffmann static uint64_t uhci_port_read(void *opaque, hwaddr addr, unsigned size) 487f1ae32a1SGerd Hoffmann { 488f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 489f1ae32a1SGerd Hoffmann uint32_t val; 490f1ae32a1SGerd Hoffmann 491f1ae32a1SGerd Hoffmann switch(addr) { 492f1ae32a1SGerd Hoffmann case 0x00: 493f1ae32a1SGerd Hoffmann val = s->cmd; 494f1ae32a1SGerd Hoffmann break; 495f1ae32a1SGerd Hoffmann case 0x02: 496f1ae32a1SGerd Hoffmann val = s->status; 497f1ae32a1SGerd Hoffmann break; 498f1ae32a1SGerd Hoffmann case 0x04: 499f1ae32a1SGerd Hoffmann val = s->intr; 500f1ae32a1SGerd Hoffmann break; 501f1ae32a1SGerd Hoffmann case 0x06: 502f1ae32a1SGerd Hoffmann val = s->frnum; 503f1ae32a1SGerd Hoffmann break; 50489eb147cSGerd Hoffmann case 0x08: 50589eb147cSGerd Hoffmann val = s->fl_base_addr & 0xffff; 50689eb147cSGerd Hoffmann break; 50789eb147cSGerd Hoffmann case 0x0a: 50889eb147cSGerd Hoffmann val = (s->fl_base_addr >> 16) & 0xffff; 50989eb147cSGerd Hoffmann break; 51089eb147cSGerd Hoffmann case 0x0c: 51189eb147cSGerd Hoffmann val = s->sof_timing; 51289eb147cSGerd Hoffmann break; 513f1ae32a1SGerd Hoffmann case 0x10 ... 0x1f: 514f1ae32a1SGerd Hoffmann { 515f1ae32a1SGerd Hoffmann UHCIPort *port; 516f1ae32a1SGerd Hoffmann int n; 517f1ae32a1SGerd Hoffmann n = (addr >> 1) & 7; 518f1ae32a1SGerd Hoffmann if (n >= NB_PORTS) 519f1ae32a1SGerd Hoffmann goto read_default; 520f1ae32a1SGerd Hoffmann port = &s->ports[n]; 521f1ae32a1SGerd Hoffmann val = port->ctrl; 522f1ae32a1SGerd Hoffmann } 523f1ae32a1SGerd Hoffmann break; 524f1ae32a1SGerd Hoffmann default: 525f1ae32a1SGerd Hoffmann read_default: 526f1ae32a1SGerd Hoffmann val = 0xff7f; /* disabled port */ 527f1ae32a1SGerd Hoffmann break; 528f1ae32a1SGerd Hoffmann } 529f1ae32a1SGerd Hoffmann 53050dcc0f8SGerd Hoffmann trace_usb_uhci_mmio_readw(addr, val); 531f1ae32a1SGerd Hoffmann 532f1ae32a1SGerd Hoffmann return val; 533f1ae32a1SGerd Hoffmann } 534f1ae32a1SGerd Hoffmann 535f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */ 536f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque) 537f1ae32a1SGerd Hoffmann { 538f1ae32a1SGerd Hoffmann UHCIState *s = (UHCIState *)opaque; 539f1ae32a1SGerd Hoffmann 540f1ae32a1SGerd Hoffmann if (!s) 541f1ae32a1SGerd Hoffmann return; 542f1ae32a1SGerd Hoffmann 543f1ae32a1SGerd Hoffmann if (s->cmd & UHCI_CMD_EGSM) { 544f1ae32a1SGerd Hoffmann s->cmd |= UHCI_CMD_FGR; 545f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_RD; 546f1ae32a1SGerd Hoffmann uhci_update_irq(s); 547f1ae32a1SGerd Hoffmann } 548f1ae32a1SGerd Hoffmann } 549f1ae32a1SGerd Hoffmann 550f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1) 551f1ae32a1SGerd Hoffmann { 552f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 553f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 554f1ae32a1SGerd Hoffmann 555f1ae32a1SGerd Hoffmann /* set connect status */ 556f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC; 557f1ae32a1SGerd Hoffmann 558f1ae32a1SGerd Hoffmann /* update speed */ 559f1ae32a1SGerd Hoffmann if (port->port.dev->speed == USB_SPEED_LOW) { 560f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_LSDA; 561f1ae32a1SGerd Hoffmann } else { 562f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_LSDA; 563f1ae32a1SGerd Hoffmann } 564f1ae32a1SGerd Hoffmann 565f1ae32a1SGerd Hoffmann uhci_resume(s); 566f1ae32a1SGerd Hoffmann } 567f1ae32a1SGerd Hoffmann 568f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1) 569f1ae32a1SGerd Hoffmann { 570f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 571f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 572f1ae32a1SGerd Hoffmann 573f1ae32a1SGerd Hoffmann uhci_async_cancel_device(s, port1->dev); 574f1ae32a1SGerd Hoffmann 575f1ae32a1SGerd Hoffmann /* set connect status */ 576f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_CCS) { 577f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_CCS; 578f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_CSC; 579f1ae32a1SGerd Hoffmann } 580f1ae32a1SGerd Hoffmann /* disable port */ 581f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_EN) { 582f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_EN; 583f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_ENC; 584f1ae32a1SGerd Hoffmann } 585f1ae32a1SGerd Hoffmann 586f1ae32a1SGerd Hoffmann uhci_resume(s); 587f1ae32a1SGerd Hoffmann } 588f1ae32a1SGerd Hoffmann 589f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child) 590f1ae32a1SGerd Hoffmann { 591f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 592f1ae32a1SGerd Hoffmann 593f1ae32a1SGerd Hoffmann uhci_async_cancel_device(s, child); 594f1ae32a1SGerd Hoffmann } 595f1ae32a1SGerd Hoffmann 596f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1) 597f1ae32a1SGerd Hoffmann { 598f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 599f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 600f1ae32a1SGerd Hoffmann 601f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) { 602f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_RD; 603f1ae32a1SGerd Hoffmann uhci_resume(s); 604f1ae32a1SGerd Hoffmann } 605f1ae32a1SGerd Hoffmann } 606f1ae32a1SGerd Hoffmann 607f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr) 608f1ae32a1SGerd Hoffmann { 609f1ae32a1SGerd Hoffmann USBDevice *dev; 610f1ae32a1SGerd Hoffmann int i; 611f1ae32a1SGerd Hoffmann 612f1ae32a1SGerd Hoffmann for (i = 0; i < NB_PORTS; i++) { 613f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[i]; 614f1ae32a1SGerd Hoffmann if (!(port->ctrl & UHCI_PORT_EN)) { 615f1ae32a1SGerd Hoffmann continue; 616f1ae32a1SGerd Hoffmann } 617f1ae32a1SGerd Hoffmann dev = usb_find_device(&port->port, addr); 618f1ae32a1SGerd Hoffmann if (dev != NULL) { 619f1ae32a1SGerd Hoffmann return dev; 620f1ae32a1SGerd Hoffmann } 621f1ae32a1SGerd Hoffmann } 622f1ae32a1SGerd Hoffmann return NULL; 623f1ae32a1SGerd Hoffmann } 624f1ae32a1SGerd Hoffmann 625963a68b5SHans de Goede static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link) 626963a68b5SHans de Goede { 627963a68b5SHans de Goede pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td)); 628963a68b5SHans de Goede le32_to_cpus(&td->link); 629963a68b5SHans de Goede le32_to_cpus(&td->ctrl); 630963a68b5SHans de Goede le32_to_cpus(&td->token); 631963a68b5SHans de Goede le32_to_cpus(&td->buffer); 632963a68b5SHans de Goede } 633963a68b5SHans de Goede 634faccca00SHans de Goede static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr, 635faccca00SHans de Goede int status, uint32_t *int_mask) 636faccca00SHans de Goede { 637faccca00SHans de Goede uint32_t queue_token = uhci_queue_token(td); 638faccca00SHans de Goede int ret; 639faccca00SHans de Goede 640faccca00SHans de Goede switch (status) { 641faccca00SHans de Goede case USB_RET_NAK: 642faccca00SHans de Goede td->ctrl |= TD_CTRL_NAK; 643faccca00SHans de Goede return TD_RESULT_NEXT_QH; 644faccca00SHans de Goede 645faccca00SHans de Goede case USB_RET_STALL: 646faccca00SHans de Goede td->ctrl |= TD_CTRL_STALL; 647faccca00SHans de Goede trace_usb_uhci_packet_complete_stall(queue_token, td_addr); 648faccca00SHans de Goede ret = TD_RESULT_NEXT_QH; 649faccca00SHans de Goede break; 650faccca00SHans de Goede 651faccca00SHans de Goede case USB_RET_BABBLE: 652faccca00SHans de Goede td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL; 653faccca00SHans de Goede /* frame interrupted */ 654faccca00SHans de Goede trace_usb_uhci_packet_complete_babble(queue_token, td_addr); 655faccca00SHans de Goede ret = TD_RESULT_STOP_FRAME; 656faccca00SHans de Goede break; 657faccca00SHans de Goede 658faccca00SHans de Goede case USB_RET_IOERROR: 659faccca00SHans de Goede case USB_RET_NODEV: 660faccca00SHans de Goede default: 661faccca00SHans de Goede td->ctrl |= TD_CTRL_TIMEOUT; 662faccca00SHans de Goede td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT); 663faccca00SHans de Goede trace_usb_uhci_packet_complete_error(queue_token, td_addr); 664faccca00SHans de Goede ret = TD_RESULT_NEXT_QH; 665faccca00SHans de Goede break; 666faccca00SHans de Goede } 667faccca00SHans de Goede 668faccca00SHans de Goede td->ctrl &= ~TD_CTRL_ACTIVE; 669faccca00SHans de Goede s->status |= UHCI_STS_USBERR; 670faccca00SHans de Goede if (td->ctrl & TD_CTRL_IOC) { 671faccca00SHans de Goede *int_mask |= 0x01; 672faccca00SHans de Goede } 673faccca00SHans de Goede uhci_update_irq(s); 674faccca00SHans de Goede return ret; 675faccca00SHans de Goede } 676faccca00SHans de Goede 677f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask) 678f1ae32a1SGerd Hoffmann { 6799a77a0f5SHans de Goede int len = 0, max_len; 680f1ae32a1SGerd Hoffmann uint8_t pid; 681f1ae32a1SGerd Hoffmann 682f1ae32a1SGerd Hoffmann max_len = ((td->token >> 21) + 1) & 0x7ff; 683f1ae32a1SGerd Hoffmann pid = td->token & 0xff; 684f1ae32a1SGerd Hoffmann 685f1ae32a1SGerd Hoffmann if (td->ctrl & TD_CTRL_IOS) 686f1ae32a1SGerd Hoffmann td->ctrl &= ~TD_CTRL_ACTIVE; 687f1ae32a1SGerd Hoffmann 6889a77a0f5SHans de Goede if (async->packet.status != USB_RET_SUCCESS) { 6899a77a0f5SHans de Goede return uhci_handle_td_error(s, td, async->td_addr, 6909a77a0f5SHans de Goede async->packet.status, int_mask); 691faccca00SHans de Goede } 692f1ae32a1SGerd Hoffmann 6939a77a0f5SHans de Goede len = async->packet.actual_length; 694f1ae32a1SGerd Hoffmann td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff); 695f1ae32a1SGerd Hoffmann 696f1ae32a1SGerd Hoffmann /* The NAK bit may have been set by a previous frame, so clear it 697f1ae32a1SGerd Hoffmann here. The docs are somewhat unclear, but win2k relies on this 698f1ae32a1SGerd Hoffmann behavior. */ 699f1ae32a1SGerd Hoffmann td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK); 700f1ae32a1SGerd Hoffmann if (td->ctrl & TD_CTRL_IOC) 701f1ae32a1SGerd Hoffmann *int_mask |= 0x01; 702f1ae32a1SGerd Hoffmann 703f1ae32a1SGerd Hoffmann if (pid == USB_TOKEN_IN) { 7049822261cSHans de Goede pci_dma_write(&s->dev, td->buffer, async->buf, len); 705f1ae32a1SGerd Hoffmann if ((td->ctrl & TD_CTRL_SPD) && len < max_len) { 706f1ae32a1SGerd Hoffmann *int_mask |= 0x02; 707f1ae32a1SGerd Hoffmann /* short packet: do not update QH */ 70850dcc0f8SGerd Hoffmann trace_usb_uhci_packet_complete_shortxfer(async->queue->token, 7091f250cc7SHans de Goede async->td_addr); 71060e1b2a6SGerd Hoffmann return TD_RESULT_NEXT_QH; 711f1ae32a1SGerd Hoffmann } 712f1ae32a1SGerd Hoffmann } 713f1ae32a1SGerd Hoffmann 714f1ae32a1SGerd Hoffmann /* success */ 7151f250cc7SHans de Goede trace_usb_uhci_packet_complete_success(async->queue->token, 7161f250cc7SHans de Goede async->td_addr); 71760e1b2a6SGerd Hoffmann return TD_RESULT_COMPLETE; 718f1ae32a1SGerd Hoffmann } 719f1ae32a1SGerd Hoffmann 72066a08cbeSHans de Goede static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr, 721a4f30cd7SHans de Goede UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask) 722f1ae32a1SGerd Hoffmann { 7239a77a0f5SHans de Goede int ret, max_len; 7246ba43f1fSHans de Goede bool spd; 725a4f30cd7SHans de Goede bool queuing = (q != NULL); 72611d15e40SHans de Goede uint8_t pid = td->token & 0xff; 7275f77e06bSGonglei UHCIAsync *async; 7288c75a899SHans de Goede 7295f77e06bSGonglei async = uhci_async_find_td(s, td_addr); 7308c75a899SHans de Goede if (async) { 7318c75a899SHans de Goede if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) { 7328c75a899SHans de Goede assert(q == NULL || q == async->queue); 7338c75a899SHans de Goede q = async->queue; 7348c75a899SHans de Goede } else { 7358c75a899SHans de Goede uhci_queue_free(async->queue, "guest re-used pending td"); 7368c75a899SHans de Goede async = NULL; 7378c75a899SHans de Goede } 7388c75a899SHans de Goede } 739f1ae32a1SGerd Hoffmann 74066a08cbeSHans de Goede if (q == NULL) { 74166a08cbeSHans de Goede q = uhci_queue_find(s, td); 74266a08cbeSHans de Goede if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) { 74366a08cbeSHans de Goede uhci_queue_free(q, "guest re-used qh"); 74466a08cbeSHans de Goede q = NULL; 74566a08cbeSHans de Goede } 74666a08cbeSHans de Goede } 74766a08cbeSHans de Goede 7483905097eSHans de Goede if (q) { 749475443cfSHans de Goede q->valid = QH_VALID; 7503905097eSHans de Goede } 7513905097eSHans de Goede 752f1ae32a1SGerd Hoffmann /* Is active ? */ 753883bca77SHans de Goede if (!(td->ctrl & TD_CTRL_ACTIVE)) { 754420ca987SHans de Goede if (async) { 755420ca987SHans de Goede /* Guest marked a pending td non-active, cancel the queue */ 756420ca987SHans de Goede uhci_queue_free(async->queue, "pending td non-active"); 757420ca987SHans de Goede } 758883bca77SHans de Goede /* 759883bca77SHans de Goede * ehci11d spec page 22: "Even if the Active bit in the TD is already 760883bca77SHans de Goede * cleared when the TD is fetched ... an IOC interrupt is generated" 761883bca77SHans de Goede */ 762883bca77SHans de Goede if (td->ctrl & TD_CTRL_IOC) { 763883bca77SHans de Goede *int_mask |= 0x01; 764883bca77SHans de Goede } 76560e1b2a6SGerd Hoffmann return TD_RESULT_NEXT_QH; 766883bca77SHans de Goede } 767f1ae32a1SGerd Hoffmann 768f419a626SGerd Hoffmann switch (pid) { 769f419a626SGerd Hoffmann case USB_TOKEN_OUT: 770f419a626SGerd Hoffmann case USB_TOKEN_SETUP: 771f419a626SGerd Hoffmann case USB_TOKEN_IN: 772f419a626SGerd Hoffmann break; 773f419a626SGerd Hoffmann default: 774f419a626SGerd Hoffmann /* invalid pid : frame interrupted */ 775f419a626SGerd Hoffmann s->status |= UHCI_STS_HCPERR; 776f419a626SGerd Hoffmann s->cmd &= ~UHCI_CMD_RS; 777f419a626SGerd Hoffmann uhci_update_irq(s); 778f419a626SGerd Hoffmann return TD_RESULT_STOP_FRAME; 779f419a626SGerd Hoffmann } 780f419a626SGerd Hoffmann 781f1ae32a1SGerd Hoffmann if (async) { 782ee008ba6SGerd Hoffmann if (queuing) { 783ee008ba6SGerd Hoffmann /* we are busy filling the queue, we are not prepared 784ee008ba6SGerd Hoffmann to consume completed packages then, just leave them 785ee008ba6SGerd Hoffmann in async state */ 786ee008ba6SGerd Hoffmann return TD_RESULT_ASYNC_CONT; 787ee008ba6SGerd Hoffmann } 7888928c9c4SHans de Goede if (!async->done) { 7898928c9c4SHans de Goede UHCI_TD last_td; 790eae3eb3eSPaolo Bonzini UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs); 7918928c9c4SHans de Goede /* 7928928c9c4SHans de Goede * While we are waiting for the current td to complete, the guest 7938928c9c4SHans de Goede * may have added more tds to the queue. Note we re-read the td 7948928c9c4SHans de Goede * rather then caching it, as we want to see guest made changes! 7958928c9c4SHans de Goede */ 7968928c9c4SHans de Goede uhci_read_td(s, &last_td, last->td_addr); 7978928c9c4SHans de Goede uhci_queue_fill(async->queue, &last_td); 798f1ae32a1SGerd Hoffmann 7998928c9c4SHans de Goede return TD_RESULT_ASYNC_CONT; 8008928c9c4SHans de Goede } 801f1ae32a1SGerd Hoffmann uhci_async_unlink(async); 802f1ae32a1SGerd Hoffmann goto done; 803f1ae32a1SGerd Hoffmann } 804f1ae32a1SGerd Hoffmann 80588793816SHans de Goede if (s->completions_only) { 80688793816SHans de Goede return TD_RESULT_ASYNC_CONT; 80788793816SHans de Goede } 80888793816SHans de Goede 809f1ae32a1SGerd Hoffmann /* Allocate new packet */ 810a4f30cd7SHans de Goede if (q == NULL) { 811ff668537SLiam Merwick USBDevice *dev; 812ff668537SLiam Merwick USBEndpoint *ep; 8137f102ebeSHans de Goede 814ff668537SLiam Merwick dev = uhci_find_device(s, (td->token >> 8) & 0x7f); 815ff668537SLiam Merwick if (dev == NULL) { 8167f102ebeSHans de Goede return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV, 8177f102ebeSHans de Goede int_mask); 8187f102ebeSHans de Goede } 819ff668537SLiam Merwick ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf); 82066a08cbeSHans de Goede q = uhci_queue_new(s, qh_addr, td, ep); 821a4f30cd7SHans de Goede } 822a4f30cd7SHans de Goede async = uhci_async_alloc(q, td_addr); 823f1ae32a1SGerd Hoffmann 824f1ae32a1SGerd Hoffmann max_len = ((td->token >> 21) + 1) & 0x7ff; 8256ba43f1fSHans de Goede spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0); 8268550a02dSGerd Hoffmann usb_packet_setup(&async->packet, pid, q->ep, 0, td_addr, spd, 827a6fb2ddbSHans de Goede (td->ctrl & TD_CTRL_IOC) != 0); 8289822261cSHans de Goede if (max_len <= sizeof(async->static_buf)) { 8299822261cSHans de Goede async->buf = async->static_buf; 8309822261cSHans de Goede } else { 8319822261cSHans de Goede async->buf = g_malloc(max_len); 8329822261cSHans de Goede } 8339822261cSHans de Goede usb_packet_addbuf(&async->packet, async->buf, max_len); 834f1ae32a1SGerd Hoffmann 835f1ae32a1SGerd Hoffmann switch(pid) { 836f1ae32a1SGerd Hoffmann case USB_TOKEN_OUT: 837f1ae32a1SGerd Hoffmann case USB_TOKEN_SETUP: 8389822261cSHans de Goede pci_dma_read(&s->dev, td->buffer, async->buf, max_len); 8399a77a0f5SHans de Goede usb_handle_packet(q->ep->dev, &async->packet); 8409a77a0f5SHans de Goede if (async->packet.status == USB_RET_SUCCESS) { 8419a77a0f5SHans de Goede async->packet.actual_length = max_len; 8429a77a0f5SHans de Goede } 843f1ae32a1SGerd Hoffmann break; 844f1ae32a1SGerd Hoffmann 845f1ae32a1SGerd Hoffmann case USB_TOKEN_IN: 8469a77a0f5SHans de Goede usb_handle_packet(q->ep->dev, &async->packet); 847f1ae32a1SGerd Hoffmann break; 848f1ae32a1SGerd Hoffmann 849f1ae32a1SGerd Hoffmann default: 8505f77e06bSGonglei abort(); /* Never to execute */ 851f1ae32a1SGerd Hoffmann } 852f1ae32a1SGerd Hoffmann 8539a77a0f5SHans de Goede if (async->packet.status == USB_RET_ASYNC) { 854f1ae32a1SGerd Hoffmann uhci_async_link(async); 855a4f30cd7SHans de Goede if (!queuing) { 85611d15e40SHans de Goede uhci_queue_fill(q, td); 857a4f30cd7SHans de Goede } 8584efe4ef3SGerd Hoffmann return TD_RESULT_ASYNC_START; 859f1ae32a1SGerd Hoffmann } 860f1ae32a1SGerd Hoffmann 861f1ae32a1SGerd Hoffmann done: 8629a77a0f5SHans de Goede ret = uhci_complete_td(s, td, async, int_mask); 863f1ae32a1SGerd Hoffmann uhci_async_free(async); 8649a77a0f5SHans de Goede return ret; 865f1ae32a1SGerd Hoffmann } 866f1ae32a1SGerd Hoffmann 867f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet) 868f1ae32a1SGerd Hoffmann { 869f1ae32a1SGerd Hoffmann UHCIAsync *async = container_of(packet, UHCIAsync, packet); 870f1ae32a1SGerd Hoffmann UHCIState *s = async->queue->uhci; 871f1ae32a1SGerd Hoffmann 8729a77a0f5SHans de Goede if (packet->status == USB_RET_REMOVE_FROM_QUEUE) { 8730cae7b1aSHans de Goede uhci_async_cancel(async); 8740cae7b1aSHans de Goede return; 8750cae7b1aSHans de Goede } 8760cae7b1aSHans de Goede 877f1ae32a1SGerd Hoffmann async->done = 1; 87888793816SHans de Goede /* Force processing of this packet *now*, needed for migration */ 87988793816SHans de Goede s->completions_only = true; 8809a16c595SGerd Hoffmann qemu_bh_schedule(s->bh); 8819a16c595SGerd Hoffmann } 882f1ae32a1SGerd Hoffmann 883f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link) 884f1ae32a1SGerd Hoffmann { 885f1ae32a1SGerd Hoffmann return (link & 1) == 0; 886f1ae32a1SGerd Hoffmann } 887f1ae32a1SGerd Hoffmann 888f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link) 889f1ae32a1SGerd Hoffmann { 890f1ae32a1SGerd Hoffmann return (link & 2) != 0; 891f1ae32a1SGerd Hoffmann } 892f1ae32a1SGerd Hoffmann 893f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link) 894f1ae32a1SGerd Hoffmann { 895f1ae32a1SGerd Hoffmann return (link & 4) != 0; 896f1ae32a1SGerd Hoffmann } 897f1ae32a1SGerd Hoffmann 898f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */ 899f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128 900f1ae32a1SGerd Hoffmann typedef struct { 901f1ae32a1SGerd Hoffmann uint32_t addr[UHCI_MAX_QUEUES]; 902f1ae32a1SGerd Hoffmann int count; 903f1ae32a1SGerd Hoffmann } QhDb; 904f1ae32a1SGerd Hoffmann 905f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db) 906f1ae32a1SGerd Hoffmann { 907f1ae32a1SGerd Hoffmann db->count = 0; 908f1ae32a1SGerd Hoffmann } 909f1ae32a1SGerd Hoffmann 910f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */ 911f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr) 912f1ae32a1SGerd Hoffmann { 913f1ae32a1SGerd Hoffmann int i; 914f1ae32a1SGerd Hoffmann for (i = 0; i < db->count; i++) 915f1ae32a1SGerd Hoffmann if (db->addr[i] == addr) 916f1ae32a1SGerd Hoffmann return 1; 917f1ae32a1SGerd Hoffmann 918f1ae32a1SGerd Hoffmann if (db->count >= UHCI_MAX_QUEUES) 919f1ae32a1SGerd Hoffmann return 1; 920f1ae32a1SGerd Hoffmann 921f1ae32a1SGerd Hoffmann db->addr[db->count++] = addr; 922f1ae32a1SGerd Hoffmann return 0; 923f1ae32a1SGerd Hoffmann } 924f1ae32a1SGerd Hoffmann 92511d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td) 926f1ae32a1SGerd Hoffmann { 927f1ae32a1SGerd Hoffmann uint32_t int_mask = 0; 928f1ae32a1SGerd Hoffmann uint32_t plink = td->link; 929f1ae32a1SGerd Hoffmann UHCI_TD ptd; 930f1ae32a1SGerd Hoffmann int ret; 931f1ae32a1SGerd Hoffmann 9326ba43f1fSHans de Goede while (is_valid(plink)) { 933a4f30cd7SHans de Goede uhci_read_td(q->uhci, &ptd, plink); 934f1ae32a1SGerd Hoffmann if (!(ptd.ctrl & TD_CTRL_ACTIVE)) { 935f1ae32a1SGerd Hoffmann break; 936f1ae32a1SGerd Hoffmann } 937a4f30cd7SHans de Goede if (uhci_queue_token(&ptd) != q->token) { 938f1ae32a1SGerd Hoffmann break; 939f1ae32a1SGerd Hoffmann } 94050dcc0f8SGerd Hoffmann trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token); 94166a08cbeSHans de Goede ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask); 94252b0fecdSGerd Hoffmann if (ret == TD_RESULT_ASYNC_CONT) { 94352b0fecdSGerd Hoffmann break; 94452b0fecdSGerd Hoffmann } 9454efe4ef3SGerd Hoffmann assert(ret == TD_RESULT_ASYNC_START); 946f1ae32a1SGerd Hoffmann assert(int_mask == 0); 947f1ae32a1SGerd Hoffmann plink = ptd.link; 948f1ae32a1SGerd Hoffmann } 94911d15e40SHans de Goede usb_device_flush_ep_queue(q->ep->dev, q->ep); 950f1ae32a1SGerd Hoffmann } 951f1ae32a1SGerd Hoffmann 952f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s) 953f1ae32a1SGerd Hoffmann { 954f1ae32a1SGerd Hoffmann uint32_t frame_addr, link, old_td_ctrl, val, int_mask; 9554aed20e2SGerd Hoffmann uint32_t curr_qh, td_count = 0; 956f1ae32a1SGerd Hoffmann int cnt, ret; 957f1ae32a1SGerd Hoffmann UHCI_TD td; 958f1ae32a1SGerd Hoffmann UHCI_QH qh; 959f1ae32a1SGerd Hoffmann QhDb qhdb; 960f1ae32a1SGerd Hoffmann 961f1ae32a1SGerd Hoffmann frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2); 962f1ae32a1SGerd Hoffmann 963f1ae32a1SGerd Hoffmann pci_dma_read(&s->dev, frame_addr, &link, 4); 964f1ae32a1SGerd Hoffmann le32_to_cpus(&link); 965f1ae32a1SGerd Hoffmann 966f1ae32a1SGerd Hoffmann int_mask = 0; 967f1ae32a1SGerd Hoffmann curr_qh = 0; 968f1ae32a1SGerd Hoffmann 969f1ae32a1SGerd Hoffmann qhdb_reset(&qhdb); 970f1ae32a1SGerd Hoffmann 971f1ae32a1SGerd Hoffmann for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) { 97288793816SHans de Goede if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) { 9734aed20e2SGerd Hoffmann /* We've reached the usb 1.1 bandwidth, which is 9744aed20e2SGerd Hoffmann 1280 bytes/frame, stop processing */ 9754aed20e2SGerd Hoffmann trace_usb_uhci_frame_stop_bandwidth(); 9764aed20e2SGerd Hoffmann break; 9774aed20e2SGerd Hoffmann } 978f1ae32a1SGerd Hoffmann if (is_qh(link)) { 979f1ae32a1SGerd Hoffmann /* QH */ 98050dcc0f8SGerd Hoffmann trace_usb_uhci_qh_load(link & ~0xf); 981f1ae32a1SGerd Hoffmann 982f1ae32a1SGerd Hoffmann if (qhdb_insert(&qhdb, link)) { 983f1ae32a1SGerd Hoffmann /* 984f1ae32a1SGerd Hoffmann * We're going in circles. Which is not a bug because 985f1ae32a1SGerd Hoffmann * HCD is allowed to do that as part of the BW management. 986f1ae32a1SGerd Hoffmann * 9874aed20e2SGerd Hoffmann * Stop processing here if no transaction has been done 9884aed20e2SGerd Hoffmann * since we've been here last time. 989f1ae32a1SGerd Hoffmann */ 990f1ae32a1SGerd Hoffmann if (td_count == 0) { 99150dcc0f8SGerd Hoffmann trace_usb_uhci_frame_loop_stop_idle(); 992f1ae32a1SGerd Hoffmann break; 993f1ae32a1SGerd Hoffmann } else { 99450dcc0f8SGerd Hoffmann trace_usb_uhci_frame_loop_continue(); 995f1ae32a1SGerd Hoffmann td_count = 0; 996f1ae32a1SGerd Hoffmann qhdb_reset(&qhdb); 997f1ae32a1SGerd Hoffmann qhdb_insert(&qhdb, link); 998f1ae32a1SGerd Hoffmann } 999f1ae32a1SGerd Hoffmann } 1000f1ae32a1SGerd Hoffmann 1001f1ae32a1SGerd Hoffmann pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh)); 1002f1ae32a1SGerd Hoffmann le32_to_cpus(&qh.link); 1003f1ae32a1SGerd Hoffmann le32_to_cpus(&qh.el_link); 1004f1ae32a1SGerd Hoffmann 1005f1ae32a1SGerd Hoffmann if (!is_valid(qh.el_link)) { 1006f1ae32a1SGerd Hoffmann /* QH w/o elements */ 1007f1ae32a1SGerd Hoffmann curr_qh = 0; 1008f1ae32a1SGerd Hoffmann link = qh.link; 1009f1ae32a1SGerd Hoffmann } else { 1010f1ae32a1SGerd Hoffmann /* QH with elements */ 1011f1ae32a1SGerd Hoffmann curr_qh = link; 1012f1ae32a1SGerd Hoffmann link = qh.el_link; 1013f1ae32a1SGerd Hoffmann } 1014f1ae32a1SGerd Hoffmann continue; 1015f1ae32a1SGerd Hoffmann } 1016f1ae32a1SGerd Hoffmann 1017f1ae32a1SGerd Hoffmann /* TD */ 1018963a68b5SHans de Goede uhci_read_td(s, &td, link); 101950dcc0f8SGerd Hoffmann trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token); 1020f1ae32a1SGerd Hoffmann 1021f1ae32a1SGerd Hoffmann old_td_ctrl = td.ctrl; 102266a08cbeSHans de Goede ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask); 1023f1ae32a1SGerd Hoffmann if (old_td_ctrl != td.ctrl) { 1024f1ae32a1SGerd Hoffmann /* update the status bits of the TD */ 1025f1ae32a1SGerd Hoffmann val = cpu_to_le32(td.ctrl); 1026f1ae32a1SGerd Hoffmann pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val)); 1027f1ae32a1SGerd Hoffmann } 1028f1ae32a1SGerd Hoffmann 1029f1ae32a1SGerd Hoffmann switch (ret) { 103060e1b2a6SGerd Hoffmann case TD_RESULT_STOP_FRAME: /* interrupted frame */ 1031f1ae32a1SGerd Hoffmann goto out; 1032f1ae32a1SGerd Hoffmann 103360e1b2a6SGerd Hoffmann case TD_RESULT_NEXT_QH: 10344efe4ef3SGerd Hoffmann case TD_RESULT_ASYNC_CONT: 103550dcc0f8SGerd Hoffmann trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf); 1036f1ae32a1SGerd Hoffmann link = curr_qh ? qh.link : td.link; 1037f1ae32a1SGerd Hoffmann continue; 1038f1ae32a1SGerd Hoffmann 10394efe4ef3SGerd Hoffmann case TD_RESULT_ASYNC_START: 104050dcc0f8SGerd Hoffmann trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf); 1041f1ae32a1SGerd Hoffmann link = curr_qh ? qh.link : td.link; 1042f1ae32a1SGerd Hoffmann continue; 1043f1ae32a1SGerd Hoffmann 104460e1b2a6SGerd Hoffmann case TD_RESULT_COMPLETE: 104550dcc0f8SGerd Hoffmann trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf); 1046f1ae32a1SGerd Hoffmann link = td.link; 1047f1ae32a1SGerd Hoffmann td_count++; 10484aed20e2SGerd Hoffmann s->frame_bytes += (td.ctrl & 0x7ff) + 1; 1049f1ae32a1SGerd Hoffmann 1050f1ae32a1SGerd Hoffmann if (curr_qh) { 1051f1ae32a1SGerd Hoffmann /* update QH element link */ 1052f1ae32a1SGerd Hoffmann qh.el_link = link; 1053f1ae32a1SGerd Hoffmann val = cpu_to_le32(qh.el_link); 1054f1ae32a1SGerd Hoffmann pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val)); 1055f1ae32a1SGerd Hoffmann 1056f1ae32a1SGerd Hoffmann if (!depth_first(link)) { 1057f1ae32a1SGerd Hoffmann /* done with this QH */ 1058f1ae32a1SGerd Hoffmann curr_qh = 0; 1059f1ae32a1SGerd Hoffmann link = qh.link; 1060f1ae32a1SGerd Hoffmann } 1061f1ae32a1SGerd Hoffmann } 1062f1ae32a1SGerd Hoffmann break; 1063f1ae32a1SGerd Hoffmann 1064f1ae32a1SGerd Hoffmann default: 1065f1ae32a1SGerd Hoffmann assert(!"unknown return code"); 1066f1ae32a1SGerd Hoffmann } 1067f1ae32a1SGerd Hoffmann 1068f1ae32a1SGerd Hoffmann /* go to the next entry */ 1069f1ae32a1SGerd Hoffmann } 1070f1ae32a1SGerd Hoffmann 1071f1ae32a1SGerd Hoffmann out: 1072f1ae32a1SGerd Hoffmann s->pending_int_mask |= int_mask; 1073f1ae32a1SGerd Hoffmann } 1074f1ae32a1SGerd Hoffmann 10759a16c595SGerd Hoffmann static void uhci_bh(void *opaque) 10769a16c595SGerd Hoffmann { 10779a16c595SGerd Hoffmann UHCIState *s = opaque; 10789a16c595SGerd Hoffmann uhci_process_frame(s); 10799a16c595SGerd Hoffmann } 10809a16c595SGerd Hoffmann 1081f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque) 1082f1ae32a1SGerd Hoffmann { 1083f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 1084f8f48b69SHans de Goede uint64_t t_now, t_last_run; 1085f8f48b69SHans de Goede int i, frames; 108673bcb24dSRutuja Shah const uint64_t frame_t = NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ; 1087f1ae32a1SGerd Hoffmann 108888793816SHans de Goede s->completions_only = false; 10899a16c595SGerd Hoffmann qemu_bh_cancel(s->bh); 1090f1ae32a1SGerd Hoffmann 1091f1ae32a1SGerd Hoffmann if (!(s->cmd & UHCI_CMD_RS)) { 1092f1ae32a1SGerd Hoffmann /* Full stop */ 109350dcc0f8SGerd Hoffmann trace_usb_uhci_schedule_stop(); 1094bc72ad67SAlex Bligh timer_del(s->frame_timer); 1095d9a528dbSGerd Hoffmann uhci_async_cancel_all(s); 1096f1ae32a1SGerd Hoffmann /* set hchalted bit in status - UHCI11D 2.1.2 */ 1097f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCHALTED; 1098f1ae32a1SGerd Hoffmann return; 1099f1ae32a1SGerd Hoffmann } 1100f1ae32a1SGerd Hoffmann 1101f8f48b69SHans de Goede /* We still store expire_time in our state, for migration */ 1102f8f48b69SHans de Goede t_last_run = s->expire_time - frame_t; 1103bc72ad67SAlex Bligh t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1104f8f48b69SHans de Goede 1105f8f48b69SHans de Goede /* Process up to MAX_FRAMES_PER_TICK frames */ 1106f8f48b69SHans de Goede frames = (t_now - t_last_run) / frame_t; 11079fdf7027SHans de Goede if (frames > s->maxframes) { 11089fdf7027SHans de Goede int skipped = frames - s->maxframes; 11099fdf7027SHans de Goede s->expire_time += skipped * frame_t; 11109fdf7027SHans de Goede s->frnum = (s->frnum + skipped) & 0x7ff; 11119fdf7027SHans de Goede frames -= skipped; 11129fdf7027SHans de Goede } 1113f8f48b69SHans de Goede if (frames > MAX_FRAMES_PER_TICK) { 1114f8f48b69SHans de Goede frames = MAX_FRAMES_PER_TICK; 1115f8f48b69SHans de Goede } 1116f8f48b69SHans de Goede 1117f8f48b69SHans de Goede for (i = 0; i < frames; i++) { 1118f8f48b69SHans de Goede s->frame_bytes = 0; 111950dcc0f8SGerd Hoffmann trace_usb_uhci_frame_start(s->frnum); 1120f1ae32a1SGerd Hoffmann uhci_async_validate_begin(s); 1121f1ae32a1SGerd Hoffmann uhci_process_frame(s); 1122f1ae32a1SGerd Hoffmann uhci_async_validate_end(s); 1123f8f48b69SHans de Goede /* The spec says frnum is the frame currently being processed, and 1124f8f48b69SHans de Goede * the guest must look at frnum - 1 on interrupt, so inc frnum now */ 1125719c130dSHans de Goede s->frnum = (s->frnum + 1) & 0x7ff; 1126f8f48b69SHans de Goede s->expire_time += frame_t; 1127f8f48b69SHans de Goede } 1128719c130dSHans de Goede 1129f8f48b69SHans de Goede /* Complete the previous frame(s) */ 1130719c130dSHans de Goede if (s->pending_int_mask) { 1131719c130dSHans de Goede s->status2 |= s->pending_int_mask; 1132719c130dSHans de Goede s->status |= UHCI_STS_USBINT; 1133719c130dSHans de Goede uhci_update_irq(s); 1134719c130dSHans de Goede } 1135719c130dSHans de Goede s->pending_int_mask = 0; 1136719c130dSHans de Goede 1137bc72ad67SAlex Bligh timer_mod(s->frame_timer, t_now + frame_t); 1138f1ae32a1SGerd Hoffmann } 1139f1ae32a1SGerd Hoffmann 1140f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = { 114189eb147cSGerd Hoffmann .read = uhci_port_read, 114289eb147cSGerd Hoffmann .write = uhci_port_write, 114389eb147cSGerd Hoffmann .valid.min_access_size = 1, 114489eb147cSGerd Hoffmann .valid.max_access_size = 4, 114589eb147cSGerd Hoffmann .impl.min_access_size = 2, 114689eb147cSGerd Hoffmann .impl.max_access_size = 2, 114789eb147cSGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN, 1148f1ae32a1SGerd Hoffmann }; 1149f1ae32a1SGerd Hoffmann 1150f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = { 1151f1ae32a1SGerd Hoffmann .attach = uhci_attach, 1152f1ae32a1SGerd Hoffmann .detach = uhci_detach, 1153f1ae32a1SGerd Hoffmann .child_detach = uhci_child_detach, 1154f1ae32a1SGerd Hoffmann .wakeup = uhci_wakeup, 1155f1ae32a1SGerd Hoffmann .complete = uhci_async_complete, 1156f1ae32a1SGerd Hoffmann }; 1157f1ae32a1SGerd Hoffmann 1158f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = { 1159f1ae32a1SGerd Hoffmann }; 1160f1ae32a1SGerd Hoffmann 11619a4e12a6SPhilippe Mathieu-Daudé void usb_uhci_common_realize(PCIDevice *dev, Error **errp) 1162f1ae32a1SGerd Hoffmann { 1163f4bbaaf5SMarkus Armbruster Error *err = NULL; 1164973002c1SGerd Hoffmann PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 11658f3f90b0SGerd Hoffmann UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class); 116649184b62SGonglei UHCIState *s = UHCI(dev); 1167f1ae32a1SGerd Hoffmann uint8_t *pci_conf = s->dev.config; 1168f1ae32a1SGerd Hoffmann int i; 1169f1ae32a1SGerd Hoffmann 1170f1ae32a1SGerd Hoffmann pci_conf[PCI_CLASS_PROG] = 0x00; 1171f1ae32a1SGerd Hoffmann /* TODO: reset value should be 0. */ 1172d3647ef1SBALATON Zoltan pci_conf[USB_SBRN] = USB_RELEASE_1; /* release number */ 11739e64f8a3SMarcel Apfelbaum pci_config_set_interrupt_pin(pci_conf, u->info.irq_pin + 1); 1174*e4f5b939SBALATON Zoltan s->irq = pci_allocate_irq(dev); 1175973002c1SGerd Hoffmann 1176f1ae32a1SGerd Hoffmann if (s->masterbus) { 1177f1ae32a1SGerd Hoffmann USBPort *ports[NB_PORTS]; 1178f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 1179f1ae32a1SGerd Hoffmann ports[i] = &s->ports[i].port; 1180f1ae32a1SGerd Hoffmann } 1181f4bbaaf5SMarkus Armbruster usb_register_companion(s->masterbus, ports, NB_PORTS, 1182f1ae32a1SGerd Hoffmann s->firstport, s, &uhci_port_ops, 1183f4bbaaf5SMarkus Armbruster USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL, 1184f4bbaaf5SMarkus Armbruster &err); 1185f4bbaaf5SMarkus Armbruster if (err) { 118663216dc7SMarkus Armbruster error_propagate(errp, err); 118763216dc7SMarkus Armbruster return; 1188f1ae32a1SGerd Hoffmann } 1189f1ae32a1SGerd Hoffmann } else { 1190c889b3a5SAndreas Färber usb_bus_new(&s->bus, sizeof(s->bus), &uhci_bus_ops, DEVICE(dev)); 1191f1ae32a1SGerd Hoffmann for (i = 0; i < NB_PORTS; i++) { 1192f1ae32a1SGerd Hoffmann usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops, 1193f1ae32a1SGerd Hoffmann USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); 1194f1ae32a1SGerd Hoffmann } 1195f1ae32a1SGerd Hoffmann } 11969a16c595SGerd Hoffmann s->bh = qemu_bh_new(uhci_bh, s); 1197bc72ad67SAlex Bligh s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, uhci_frame_timer, s); 1198f1ae32a1SGerd Hoffmann s->num_ports_vmstate = NB_PORTS; 1199f1ae32a1SGerd Hoffmann QTAILQ_INIT(&s->queues); 1200f1ae32a1SGerd Hoffmann 120122fc860bSPaolo Bonzini memory_region_init_io(&s->io_bar, OBJECT(s), &uhci_ioport_ops, s, 120222fc860bSPaolo Bonzini "uhci", 0x20); 120322fc860bSPaolo Bonzini 1204f1ae32a1SGerd Hoffmann /* Use region 4 for consistency with real hardware. BSD guests seem 1205f1ae32a1SGerd Hoffmann to rely on this. */ 1206f1ae32a1SGerd Hoffmann pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar); 1207f1ae32a1SGerd Hoffmann } 1208f1ae32a1SGerd Hoffmann 12093a3464b0SGonglei static void usb_uhci_exit(PCIDevice *dev) 12103a3464b0SGonglei { 121149184b62SGonglei UHCIState *s = UHCI(dev); 12123a3464b0SGonglei 1213d733f74cSGonglei trace_usb_uhci_exit(); 1214d733f74cSGonglei 12153a3464b0SGonglei if (s->frame_timer) { 12163a3464b0SGonglei timer_free(s->frame_timer); 12173a3464b0SGonglei s->frame_timer = NULL; 12183a3464b0SGonglei } 12193a3464b0SGonglei 12203a3464b0SGonglei if (s->bh) { 12213a3464b0SGonglei qemu_bh_delete(s->bh); 12223a3464b0SGonglei } 12233a3464b0SGonglei 12243a3464b0SGonglei uhci_async_cancel_all(s); 12253a3464b0SGonglei 12263a3464b0SGonglei if (!s->masterbus) { 12273a3464b0SGonglei usb_bus_release(&s->bus); 12283a3464b0SGonglei } 12293a3464b0SGonglei } 12303a3464b0SGonglei 1231638ca939SGerd Hoffmann static Property uhci_properties_companion[] = { 1232f1ae32a1SGerd Hoffmann DEFINE_PROP_STRING("masterbus", UHCIState, masterbus), 1233f1ae32a1SGerd Hoffmann DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0), 123440141d12SGerd Hoffmann DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280), 12359fdf7027SHans de Goede DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128), 1236f1ae32a1SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 1237f1ae32a1SGerd Hoffmann }; 1238638ca939SGerd Hoffmann static Property uhci_properties_standalone[] = { 1239638ca939SGerd Hoffmann DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280), 1240638ca939SGerd Hoffmann DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128), 1241638ca939SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 1242638ca939SGerd Hoffmann }; 1243f1ae32a1SGerd Hoffmann 12442c2e8525SGerd Hoffmann static void uhci_class_init(ObjectClass *klass, void *data) 1245f1ae32a1SGerd Hoffmann { 1246f1ae32a1SGerd Hoffmann DeviceClass *dc = DEVICE_CLASS(klass); 1247f1ae32a1SGerd Hoffmann PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 124849184b62SGonglei 124949184b62SGonglei k->class_id = PCI_CLASS_SERIAL_USB; 125049184b62SGonglei dc->vmsd = &vmstate_uhci; 125149184b62SGonglei dc->reset = uhci_reset; 125249184b62SGonglei set_bit(DEVICE_CATEGORY_USB, dc->categories); 125349184b62SGonglei } 125449184b62SGonglei 125549184b62SGonglei static const TypeInfo uhci_pci_type_info = { 125649184b62SGonglei .name = TYPE_UHCI, 125749184b62SGonglei .parent = TYPE_PCI_DEVICE, 125849184b62SGonglei .instance_size = sizeof(UHCIState), 125949184b62SGonglei .class_size = sizeof(UHCIPCIDeviceClass), 126049184b62SGonglei .abstract = true, 126149184b62SGonglei .class_init = uhci_class_init, 1262fd3b02c8SEduardo Habkost .interfaces = (InterfaceInfo[]) { 1263fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 1264fd3b02c8SEduardo Habkost { }, 1265fd3b02c8SEduardo Habkost }, 126649184b62SGonglei }; 126749184b62SGonglei 12689a4e12a6SPhilippe Mathieu-Daudé void uhci_data_class_init(ObjectClass *klass, void *data) 126949184b62SGonglei { 127049184b62SGonglei PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 127149184b62SGonglei DeviceClass *dc = DEVICE_CLASS(klass); 12728f3f90b0SGerd Hoffmann UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class); 12732c2e8525SGerd Hoffmann UHCIInfo *info = data; 1274f1ae32a1SGerd Hoffmann 127563216dc7SMarkus Armbruster k->realize = info->realize ? info->realize : usb_uhci_common_realize; 12763a3464b0SGonglei k->exit = info->unplug ? usb_uhci_exit : NULL; 12772c2e8525SGerd Hoffmann k->vendor_id = info->vendor_id; 12782c2e8525SGerd Hoffmann k->device_id = info->device_id; 12792c2e8525SGerd Hoffmann k->revision = info->revision; 1280638ca939SGerd Hoffmann if (!info->unplug) { 1281638ca939SGerd Hoffmann /* uhci controllers in companion setups can't be hotplugged */ 1282638ca939SGerd Hoffmann dc->hotpluggable = false; 12834f67d30bSMarc-André Lureau device_class_set_props(dc, uhci_properties_companion); 1284638ca939SGerd Hoffmann } else { 12854f67d30bSMarc-André Lureau device_class_set_props(dc, uhci_properties_standalone); 1286638ca939SGerd Hoffmann } 1287ece29df3SBALATON Zoltan if (info->notuser) { 1288ece29df3SBALATON Zoltan dc->user_creatable = false; 1289ece29df3SBALATON Zoltan } 12908f3f90b0SGerd Hoffmann u->info = *info; 1291f1ae32a1SGerd Hoffmann } 1292f1ae32a1SGerd Hoffmann 12932c2e8525SGerd Hoffmann static UHCIInfo uhci_info[] = { 12942c2e8525SGerd Hoffmann { 1295f1ae32a1SGerd Hoffmann .name = "piix3-usb-uhci", 12962c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 12972c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82371SB_2, 12982c2e8525SGerd Hoffmann .revision = 0x01, 12998f3f90b0SGerd Hoffmann .irq_pin = 3, 13002c2e8525SGerd Hoffmann .unplug = true, 13012c2e8525SGerd Hoffmann },{ 1302f1ae32a1SGerd Hoffmann .name = "piix4-usb-uhci", 13032c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13042c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82371AB_2, 13052c2e8525SGerd Hoffmann .revision = 0x01, 13068f3f90b0SGerd Hoffmann .irq_pin = 3, 13072c2e8525SGerd Hoffmann .unplug = true, 13082c2e8525SGerd Hoffmann },{ 130974625ea2SGerd Hoffmann .name = "ich9-usb-uhci1", /* 00:1d.0 */ 13102c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13112c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1, 13122c2e8525SGerd Hoffmann .revision = 0x03, 13138f3f90b0SGerd Hoffmann .irq_pin = 0, 13142c2e8525SGerd Hoffmann .unplug = false, 13152c2e8525SGerd Hoffmann },{ 131674625ea2SGerd Hoffmann .name = "ich9-usb-uhci2", /* 00:1d.1 */ 13172c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13182c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2, 13192c2e8525SGerd Hoffmann .revision = 0x03, 13208f3f90b0SGerd Hoffmann .irq_pin = 1, 13212c2e8525SGerd Hoffmann .unplug = false, 13222c2e8525SGerd Hoffmann },{ 132374625ea2SGerd Hoffmann .name = "ich9-usb-uhci3", /* 00:1d.2 */ 13242c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13252c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3, 13262c2e8525SGerd Hoffmann .revision = 0x03, 13278f3f90b0SGerd Hoffmann .irq_pin = 2, 13282c2e8525SGerd Hoffmann .unplug = false, 132974625ea2SGerd Hoffmann },{ 133074625ea2SGerd Hoffmann .name = "ich9-usb-uhci4", /* 00:1a.0 */ 133174625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 133274625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4, 133374625ea2SGerd Hoffmann .revision = 0x03, 133474625ea2SGerd Hoffmann .irq_pin = 0, 133574625ea2SGerd Hoffmann .unplug = false, 133674625ea2SGerd Hoffmann },{ 133774625ea2SGerd Hoffmann .name = "ich9-usb-uhci5", /* 00:1a.1 */ 133874625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 133974625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5, 134074625ea2SGerd Hoffmann .revision = 0x03, 134174625ea2SGerd Hoffmann .irq_pin = 1, 134274625ea2SGerd Hoffmann .unplug = false, 134374625ea2SGerd Hoffmann },{ 134474625ea2SGerd Hoffmann .name = "ich9-usb-uhci6", /* 00:1a.2 */ 134574625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 134674625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6, 134774625ea2SGerd Hoffmann .revision = 0x03, 134874625ea2SGerd Hoffmann .irq_pin = 2, 134974625ea2SGerd Hoffmann .unplug = false, 13502c2e8525SGerd Hoffmann } 1351f1ae32a1SGerd Hoffmann }; 1352f1ae32a1SGerd Hoffmann 1353f1ae32a1SGerd Hoffmann static void uhci_register_types(void) 1354f1ae32a1SGerd Hoffmann { 13552c2e8525SGerd Hoffmann TypeInfo uhci_type_info = { 135649184b62SGonglei .parent = TYPE_UHCI, 135749184b62SGonglei .class_init = uhci_data_class_init, 13582c2e8525SGerd Hoffmann }; 13592c2e8525SGerd Hoffmann int i; 13602c2e8525SGerd Hoffmann 136149184b62SGonglei type_register_static(&uhci_pci_type_info); 136249184b62SGonglei 13632c2e8525SGerd Hoffmann for (i = 0; i < ARRAY_SIZE(uhci_info); i++) { 13642c2e8525SGerd Hoffmann uhci_type_info.name = uhci_info[i].name; 13652c2e8525SGerd Hoffmann uhci_type_info.class_data = uhci_info + i; 13662c2e8525SGerd Hoffmann type_register(&uhci_type_info); 13672c2e8525SGerd Hoffmann } 1368f1ae32a1SGerd Hoffmann } 1369f1ae32a1SGerd Hoffmann 1370f1ae32a1SGerd Hoffmann type_init(uhci_register_types) 1371