2 * USB UHCI controller emulation
4 * Copyright (c) 2005 Fabrice Bellard
6 * Copyright (c) 2008 Max Krasnyansky
7 * Magor rewrite of the UHCI data structures parser and frame processor
8 * Support for fully async operation and multiple outstanding transactions
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #include "hw/pci/pci.h"
31 #include "qemu/timer.h"
33 #include "sysemu/dma.h"
37 //#define DEBUG_DUMP_DATA
39 #define UHCI_CMD_FGR (1 << 4)
40 #define UHCI_CMD_EGSM (1 << 3)
41 #define UHCI_CMD_GRESET (1 << 2)
42 #define UHCI_CMD_HCRESET (1 << 1)
43 #define UHCI_CMD_RS (1 << 0)
45 #define UHCI_STS_HCHALTED (1 << 5)
46 #define UHCI_STS_HCPERR (1 << 4)
47 #define UHCI_STS_HSERR (1 << 3)
48 #define UHCI_STS_RD (1 << 2)
49 #define UHCI_STS_USBERR (1 << 1)
50 #define UHCI_STS_USBINT (1 << 0)
52 #define TD_CTRL_SPD (1 << 29)
53 #define TD_CTRL_ERROR_SHIFT 27
54 #define TD_CTRL_IOS (1 << 25)
55 #define TD_CTRL_IOC (1 << 24)
56 #define TD_CTRL_ACTIVE (1 << 23)
57 #define TD_CTRL_STALL (1 << 22)
58 #define TD_CTRL_BABBLE (1 << 20)
59 #define TD_CTRL_NAK (1 << 19)
60 #define TD_CTRL_TIMEOUT (1 << 18)
62 #define UHCI_PORT_SUSPEND (1 << 12)
63 #define UHCI_PORT_RESET (1 << 9)
64 #define UHCI_PORT_LSDA (1 << 8)
65 #define UHCI_PORT_RD (1 << 6)
66 #define UHCI_PORT_ENC (1 << 3)
67 #define UHCI_PORT_EN (1 << 2)
68 #define UHCI_PORT_CSC (1 << 1)
69 #define UHCI_PORT_CCS (1 << 0)
71 #define UHCI_PORT_READ_ONLY (0x1bb)
72 #define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC)
74 #define FRAME_TIMER_FREQ 1000
76 #define FRAME_MAX_LOOPS 256
78 /* Must be large enough to handle 10 frame delay for initial isoc requests */
81 #define MAX_FRAMES_PER_TICK (QH_VALID / 2)
86 TD_RESULT_STOP_FRAME = 10,
89 TD_RESULT_ASYNC_START,
93 typedef struct UHCIState UHCIState;
94 typedef struct UHCIAsync UHCIAsync;
95 typedef struct UHCIQueue UHCIQueue;
96 typedef struct UHCIInfo UHCIInfo;
97 typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass;
105 int (*initfn)(PCIDevice *dev);
109 struct UHCIPCIDeviceClass {
110 PCIDeviceClass parent_class;
115 * Pending async transaction.
116 * 'packet' must be the first field because completion
117 * handler does "(UHCIAsync *) pkt" cast.
124 QTAILQ_ENTRY(UHCIAsync) next;
134 QTAILQ_ENTRY(UHCIQueue) next;
135 QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs;
139 typedef struct UHCIPort {
147 USBBus bus; /* Note unused when we're a companion controller */
148 uint16_t cmd; /* cmd register */
150 uint16_t intr; /* interrupt enable register */
151 uint16_t frnum; /* frame number */
152 uint32_t fl_base_addr; /* frame list base address */
154 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
156 QEMUTimer *frame_timer;
158 uint32_t frame_bytes;
159 uint32_t frame_bandwidth;
160 bool completions_only;
161 UHCIPort ports[NB_PORTS];
163 /* Interrupts that should be raised at the end of the current frame. */
164 uint32_t pending_int_mask;
168 QTAILQ_HEAD(, UHCIQueue) queues;
169 uint8_t num_ports_vmstate;
176 typedef struct UHCI_TD {
178 uint32_t ctrl; /* see TD_CTRL_xxx */
183 typedef struct UHCI_QH {
188 static void uhci_async_cancel(UHCIAsync *async);
189 static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
191 static inline int32_t uhci_queue_token(UHCI_TD *td)
193 if ((td->token & (0xf << 15)) == 0) {
194 /* ctrl ep, cover ep and dev, not pid! */
195 return td->token & 0x7ff00;
197 /* covers ep, dev, pid -> identifies the endpoint */
198 return td->token & 0x7ffff;
202 static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
207 queue = g_new0(UHCIQueue, 1);
209 queue->qh_addr = qh_addr;
210 queue->token = uhci_queue_token(td);
212 QTAILQ_INIT(&queue->asyncs);
213 QTAILQ_INSERT_HEAD(&s->queues, queue, next);
214 queue->valid = QH_VALID;
215 trace_usb_uhci_queue_add(queue->token);
219 static void uhci_queue_free(UHCIQueue *queue, const char *reason)
221 UHCIState *s = queue->uhci;
224 while (!QTAILQ_EMPTY(&queue->asyncs)) {
225 async = QTAILQ_FIRST(&queue->asyncs);
226 uhci_async_cancel(async);
229 trace_usb_uhci_queue_del(queue->token, reason);
230 QTAILQ_REMOVE(&s->queues, queue, next);
234 static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
236 uint32_t token = uhci_queue_token(td);
239 QTAILQ_FOREACH(queue, &s->queues, next) {
240 if (queue->token == token) {
247 static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
248 uint32_t td_addr, bool queuing)
250 UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
252 return queue->qh_addr == qh_addr &&
253 queue->token == uhci_queue_token(td) &&
254 (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
255 first->td_addr == td_addr);
258 static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
260 UHCIAsync *async = g_new0(UHCIAsync, 1);
262 async->queue = queue;
263 async->td_addr = td_addr;
264 usb_packet_init(&async->packet);
265 pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
266 trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
271 static void uhci_async_free(UHCIAsync *async)
273 trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
274 usb_packet_cleanup(&async->packet);
275 qemu_sglist_destroy(&async->sgl);
279 static void uhci_async_link(UHCIAsync *async)
281 UHCIQueue *queue = async->queue;
282 QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
283 trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
286 static void uhci_async_unlink(UHCIAsync *async)
288 UHCIQueue *queue = async->queue;
289 QTAILQ_REMOVE(&queue->asyncs, async, next);
290 trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
293 static void uhci_async_cancel(UHCIAsync *async)
295 uhci_async_unlink(async);
296 trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
299 usb_cancel_packet(&async->packet);
300 usb_packet_unmap(&async->packet, &async->sgl);
301 uhci_async_free(async);
305 * Mark all outstanding async packets as invalid.
306 * This is used for canceling them when TDs are removed by the HCD.
308 static void uhci_async_validate_begin(UHCIState *s)
312 QTAILQ_FOREACH(queue, &s->queues, next) {
318 * Cancel async packets that are no longer valid
320 static void uhci_async_validate_end(UHCIState *s)
322 UHCIQueue *queue, *n;
324 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
326 uhci_queue_free(queue, "validate-end");
331 static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
333 UHCIQueue *queue, *n;
335 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
336 if (queue->ep->dev == dev) {
337 uhci_queue_free(queue, "cancel-device");
342 static void uhci_async_cancel_all(UHCIState *s)
344 UHCIQueue *queue, *nq;
346 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
347 uhci_queue_free(queue, "cancel-all");
351 static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
356 QTAILQ_FOREACH(queue, &s->queues, next) {
357 QTAILQ_FOREACH(async, &queue->asyncs, next) {
358 if (async->td_addr == td_addr) {
366 static void uhci_update_irq(UHCIState *s)
369 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
370 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
371 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
372 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
373 (s->status & UHCI_STS_HSERR) ||
374 (s->status & UHCI_STS_HCPERR)) {
379 qemu_set_irq(s->dev.irq[s->irq_pin], level);
382 static void uhci_reset(void *opaque)
384 UHCIState *s = opaque;
389 trace_usb_uhci_reset();
391 pci_conf = s->dev.config;
393 pci_conf[0x6a] = 0x01; /* usb clock */
394 pci_conf[0x6b] = 0x00;
402 for(i = 0; i < NB_PORTS; i++) {
405 if (port->port.dev && port->port.dev->attached) {
406 usb_port_reset(&port->port);
410 uhci_async_cancel_all(s);
411 qemu_bh_cancel(s->bh);
415 static const VMStateDescription vmstate_uhci_port = {
418 .minimum_version_id = 1,
419 .minimum_version_id_old = 1,
420 .fields = (VMStateField []) {
421 VMSTATE_UINT16(ctrl, UHCIPort),
422 VMSTATE_END_OF_LIST()
426 static int uhci_post_load(void *opaque, int version_id)
428 UHCIState *s = opaque;
430 if (version_id < 2) {
431 s->expire_time = qemu_get_clock_ns(vm_clock) +
432 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
437 static const VMStateDescription vmstate_uhci = {
440 .minimum_version_id = 1,
441 .minimum_version_id_old = 1,
442 .post_load = uhci_post_load,
443 .fields = (VMStateField []) {
444 VMSTATE_PCI_DEVICE(dev, UHCIState),
445 VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
446 VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
447 vmstate_uhci_port, UHCIPort),
448 VMSTATE_UINT16(cmd, UHCIState),
449 VMSTATE_UINT16(status, UHCIState),
450 VMSTATE_UINT16(intr, UHCIState),
451 VMSTATE_UINT16(frnum, UHCIState),
452 VMSTATE_UINT32(fl_base_addr, UHCIState),
453 VMSTATE_UINT8(sof_timing, UHCIState),
454 VMSTATE_UINT8(status2, UHCIState),
455 VMSTATE_TIMER(frame_timer, UHCIState),
456 VMSTATE_INT64_V(expire_time, UHCIState, 2),
457 VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3),
458 VMSTATE_END_OF_LIST()
462 static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
464 UHCIState *s = opaque;
474 static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
476 UHCIState *s = opaque;
491 static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
493 UHCIState *s = opaque;
496 trace_usb_uhci_mmio_writew(addr, val);
500 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
501 /* start frame processing */
502 trace_usb_uhci_schedule_start();
503 s->expire_time = qemu_get_clock_ns(vm_clock) +
504 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
505 qemu_mod_timer(s->frame_timer, s->expire_time);
506 s->status &= ~UHCI_STS_HCHALTED;
507 } else if (!(val & UHCI_CMD_RS)) {
508 s->status |= UHCI_STS_HCHALTED;
510 if (val & UHCI_CMD_GRESET) {
514 /* send reset on the USB bus */
515 for(i = 0; i < NB_PORTS; i++) {
517 usb_device_reset(port->port.dev);
522 if (val & UHCI_CMD_HCRESET) {
530 /* XXX: the chip spec is not coherent, so we add a hidden
531 register to distinguish between IOC and SPD */
532 if (val & UHCI_STS_USBINT)
541 if (s->status & UHCI_STS_HCHALTED)
542 s->frnum = val & 0x7ff;
554 dev = port->port.dev;
555 if (dev && dev->attached) {
557 if ( (val & UHCI_PORT_RESET) &&
558 !(port->ctrl & UHCI_PORT_RESET) ) {
559 usb_device_reset(dev);
562 port->ctrl &= UHCI_PORT_READ_ONLY;
563 /* enabled may only be set if a device is connected */
564 if (!(port->ctrl & UHCI_PORT_CCS)) {
565 val &= ~UHCI_PORT_EN;
567 port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
568 /* some bits are reset when a '1' is written to them */
569 port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
575 static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
577 UHCIState *s = opaque;
607 val = 0xff7f; /* disabled port */
611 trace_usb_uhci_mmio_readw(addr, val);
616 static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
618 UHCIState *s = opaque;
621 trace_usb_uhci_mmio_writel(addr, val);
625 s->fl_base_addr = val & ~0xfff;
630 static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
632 UHCIState *s = opaque;
638 val = s->fl_base_addr;
644 trace_usb_uhci_mmio_readl(addr, val);
648 /* signal resume if controller suspended */
649 static void uhci_resume (void *opaque)
651 UHCIState *s = (UHCIState *)opaque;
656 if (s->cmd & UHCI_CMD_EGSM) {
657 s->cmd |= UHCI_CMD_FGR;
658 s->status |= UHCI_STS_RD;
663 static void uhci_attach(USBPort *port1)
665 UHCIState *s = port1->opaque;
666 UHCIPort *port = &s->ports[port1->index];
668 /* set connect status */
669 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
672 if (port->port.dev->speed == USB_SPEED_LOW) {
673 port->ctrl |= UHCI_PORT_LSDA;
675 port->ctrl &= ~UHCI_PORT_LSDA;
681 static void uhci_detach(USBPort *port1)
683 UHCIState *s = port1->opaque;
684 UHCIPort *port = &s->ports[port1->index];
686 uhci_async_cancel_device(s, port1->dev);
688 /* set connect status */
689 if (port->ctrl & UHCI_PORT_CCS) {
690 port->ctrl &= ~UHCI_PORT_CCS;
691 port->ctrl |= UHCI_PORT_CSC;
694 if (port->ctrl & UHCI_PORT_EN) {
695 port->ctrl &= ~UHCI_PORT_EN;
696 port->ctrl |= UHCI_PORT_ENC;
702 static void uhci_child_detach(USBPort *port1, USBDevice *child)
704 UHCIState *s = port1->opaque;
706 uhci_async_cancel_device(s, child);
709 static void uhci_wakeup(USBPort *port1)
711 UHCIState *s = port1->opaque;
712 UHCIPort *port = &s->ports[port1->index];
714 if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
715 port->ctrl |= UHCI_PORT_RD;
720 static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
725 for (i = 0; i < NB_PORTS; i++) {
726 UHCIPort *port = &s->ports[i];
727 if (!(port->ctrl & UHCI_PORT_EN)) {
730 dev = usb_find_device(&port->port, addr);
738 static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
740 pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
741 le32_to_cpus(&td->link);
742 le32_to_cpus(&td->ctrl);
743 le32_to_cpus(&td->token);
744 le32_to_cpus(&td->buffer);
747 static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr,
748 int status, uint32_t *int_mask)
750 uint32_t queue_token = uhci_queue_token(td);
755 td->ctrl |= TD_CTRL_NAK;
756 return TD_RESULT_NEXT_QH;
759 td->ctrl |= TD_CTRL_STALL;
760 trace_usb_uhci_packet_complete_stall(queue_token, td_addr);
761 ret = TD_RESULT_NEXT_QH;
765 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
766 /* frame interrupted */
767 trace_usb_uhci_packet_complete_babble(queue_token, td_addr);
768 ret = TD_RESULT_STOP_FRAME;
771 case USB_RET_IOERROR:
774 td->ctrl |= TD_CTRL_TIMEOUT;
775 td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
776 trace_usb_uhci_packet_complete_error(queue_token, td_addr);
777 ret = TD_RESULT_NEXT_QH;
781 td->ctrl &= ~TD_CTRL_ACTIVE;
782 s->status |= UHCI_STS_USBERR;
783 if (td->ctrl & TD_CTRL_IOC) {
790 static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
792 int len = 0, max_len;
795 max_len = ((td->token >> 21) + 1) & 0x7ff;
796 pid = td->token & 0xff;
798 if (td->ctrl & TD_CTRL_IOS)
799 td->ctrl &= ~TD_CTRL_ACTIVE;
801 if (async->packet.status != USB_RET_SUCCESS) {
802 return uhci_handle_td_error(s, td, async->td_addr,
803 async->packet.status, int_mask);
806 len = async->packet.actual_length;
807 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
809 /* The NAK bit may have been set by a previous frame, so clear it
810 here. The docs are somewhat unclear, but win2k relies on this
812 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
813 if (td->ctrl & TD_CTRL_IOC)
816 if (pid == USB_TOKEN_IN) {
817 if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
819 /* short packet: do not update QH */
820 trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
822 return TD_RESULT_NEXT_QH;
827 trace_usb_uhci_packet_complete_success(async->queue->token,
829 return TD_RESULT_COMPLETE;
832 static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
833 UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
837 bool queuing = (q != NULL);
838 uint8_t pid = td->token & 0xff;
839 UHCIAsync *async = uhci_async_find_td(s, td_addr);
842 if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
843 assert(q == NULL || q == async->queue);
846 uhci_queue_free(async->queue, "guest re-used pending td");
852 q = uhci_queue_find(s, td);
853 if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
854 uhci_queue_free(q, "guest re-used qh");
864 if (!(td->ctrl & TD_CTRL_ACTIVE)) {
866 /* Guest marked a pending td non-active, cancel the queue */
867 uhci_queue_free(async->queue, "pending td non-active");
870 * ehci11d spec page 22: "Even if the Active bit in the TD is already
871 * cleared when the TD is fetched ... an IOC interrupt is generated"
873 if (td->ctrl & TD_CTRL_IOC) {
876 return TD_RESULT_NEXT_QH;
881 /* we are busy filling the queue, we are not prepared
882 to consume completed packages then, just leave them
884 return TD_RESULT_ASYNC_CONT;
888 UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head);
890 * While we are waiting for the current td to complete, the guest
891 * may have added more tds to the queue. Note we re-read the td
892 * rather then caching it, as we want to see guest made changes!
894 uhci_read_td(s, &last_td, last->td_addr);
895 uhci_queue_fill(async->queue, &last_td);
897 return TD_RESULT_ASYNC_CONT;
899 uhci_async_unlink(async);
903 if (s->completions_only) {
904 return TD_RESULT_ASYNC_CONT;
907 /* Allocate new packet */
909 USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
910 USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
913 return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV,
916 q = uhci_queue_new(s, qh_addr, td, ep);
918 async = uhci_async_alloc(q, td_addr);
920 max_len = ((td->token >> 21) + 1) & 0x7ff;
921 spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
922 usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
923 (td->ctrl & TD_CTRL_IOC) != 0);
924 qemu_sglist_add(&async->sgl, td->buffer, max_len);
925 usb_packet_map(&async->packet, &async->sgl);
929 case USB_TOKEN_SETUP:
930 usb_handle_packet(q->ep->dev, &async->packet);
931 if (async->packet.status == USB_RET_SUCCESS) {
932 async->packet.actual_length = max_len;
937 usb_handle_packet(q->ep->dev, &async->packet);
941 /* invalid pid : frame interrupted */
942 usb_packet_unmap(&async->packet, &async->sgl);
943 uhci_async_free(async);
944 s->status |= UHCI_STS_HCPERR;
946 return TD_RESULT_STOP_FRAME;
949 if (async->packet.status == USB_RET_ASYNC) {
950 uhci_async_link(async);
952 uhci_queue_fill(q, td);
954 return TD_RESULT_ASYNC_START;
958 ret = uhci_complete_td(s, td, async, int_mask);
959 usb_packet_unmap(&async->packet, &async->sgl);
960 uhci_async_free(async);
964 static void uhci_async_complete(USBPort *port, USBPacket *packet)
966 UHCIAsync *async = container_of(packet, UHCIAsync, packet);
967 UHCIState *s = async->queue->uhci;
969 if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
970 uhci_async_cancel(async);
975 /* Force processing of this packet *now*, needed for migration */
976 s->completions_only = true;
977 qemu_bh_schedule(s->bh);
980 static int is_valid(uint32_t link)
982 return (link & 1) == 0;
985 static int is_qh(uint32_t link)
987 return (link & 2) != 0;
990 static int depth_first(uint32_t link)
992 return (link & 4) != 0;
995 /* QH DB used for detecting QH loops */
996 #define UHCI_MAX_QUEUES 128
998 uint32_t addr[UHCI_MAX_QUEUES];
1002 static void qhdb_reset(QhDb *db)
1007 /* Add QH to DB. Returns 1 if already present or DB is full. */
1008 static int qhdb_insert(QhDb *db, uint32_t addr)
1011 for (i = 0; i < db->count; i++)
1012 if (db->addr[i] == addr)
1015 if (db->count >= UHCI_MAX_QUEUES)
1018 db->addr[db->count++] = addr;
1022 static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
1024 uint32_t int_mask = 0;
1025 uint32_t plink = td->link;
1029 while (is_valid(plink)) {
1030 uhci_read_td(q->uhci, &ptd, plink);
1031 if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
1034 if (uhci_queue_token(&ptd) != q->token) {
1037 trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
1038 ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
1039 if (ret == TD_RESULT_ASYNC_CONT) {
1042 assert(ret == TD_RESULT_ASYNC_START);
1043 assert(int_mask == 0);
1046 usb_device_flush_ep_queue(q->ep->dev, q->ep);
1049 static void uhci_process_frame(UHCIState *s)
1051 uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
1052 uint32_t curr_qh, td_count = 0;
1058 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
1060 pci_dma_read(&s->dev, frame_addr, &link, 4);
1061 le32_to_cpus(&link);
1068 for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
1069 if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) {
1070 /* We've reached the usb 1.1 bandwidth, which is
1071 1280 bytes/frame, stop processing */
1072 trace_usb_uhci_frame_stop_bandwidth();
1077 trace_usb_uhci_qh_load(link & ~0xf);
1079 if (qhdb_insert(&qhdb, link)) {
1081 * We're going in circles. Which is not a bug because
1082 * HCD is allowed to do that as part of the BW management.
1084 * Stop processing here if no transaction has been done
1085 * since we've been here last time.
1087 if (td_count == 0) {
1088 trace_usb_uhci_frame_loop_stop_idle();
1091 trace_usb_uhci_frame_loop_continue();
1094 qhdb_insert(&qhdb, link);
1098 pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1099 le32_to_cpus(&qh.link);
1100 le32_to_cpus(&qh.el_link);
1102 if (!is_valid(qh.el_link)) {
1103 /* QH w/o elements */
1107 /* QH with elements */
1115 uhci_read_td(s, &td, link);
1116 trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
1118 old_td_ctrl = td.ctrl;
1119 ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
1120 if (old_td_ctrl != td.ctrl) {
1121 /* update the status bits of the TD */
1122 val = cpu_to_le32(td.ctrl);
1123 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1127 case TD_RESULT_STOP_FRAME: /* interrupted frame */
1130 case TD_RESULT_NEXT_QH:
1131 case TD_RESULT_ASYNC_CONT:
1132 trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
1133 link = curr_qh ? qh.link : td.link;
1136 case TD_RESULT_ASYNC_START:
1137 trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
1138 link = curr_qh ? qh.link : td.link;
1141 case TD_RESULT_COMPLETE:
1142 trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
1145 s->frame_bytes += (td.ctrl & 0x7ff) + 1;
1148 /* update QH element link */
1150 val = cpu_to_le32(qh.el_link);
1151 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1153 if (!depth_first(link)) {
1154 /* done with this QH */
1162 assert(!"unknown return code");
1165 /* go to the next entry */
1169 s->pending_int_mask |= int_mask;
1172 static void uhci_bh(void *opaque)
1174 UHCIState *s = opaque;
1175 uhci_process_frame(s);
1178 static void uhci_frame_timer(void *opaque)
1180 UHCIState *s = opaque;
1181 uint64_t t_now, t_last_run;
1183 const uint64_t frame_t = get_ticks_per_sec() / FRAME_TIMER_FREQ;
1185 s->completions_only = false;
1186 qemu_bh_cancel(s->bh);
1188 if (!(s->cmd & UHCI_CMD_RS)) {
1190 trace_usb_uhci_schedule_stop();
1191 qemu_del_timer(s->frame_timer);
1192 uhci_async_cancel_all(s);
1193 /* set hchalted bit in status - UHCI11D 2.1.2 */
1194 s->status |= UHCI_STS_HCHALTED;
1198 /* We still store expire_time in our state, for migration */
1199 t_last_run = s->expire_time - frame_t;
1200 t_now = qemu_get_clock_ns(vm_clock);
1202 /* Process up to MAX_FRAMES_PER_TICK frames */
1203 frames = (t_now - t_last_run) / frame_t;
1204 if (frames > MAX_FRAMES_PER_TICK) {
1205 frames = MAX_FRAMES_PER_TICK;
1208 for (i = 0; i < frames; i++) {
1210 trace_usb_uhci_frame_start(s->frnum);
1211 uhci_async_validate_begin(s);
1212 uhci_process_frame(s);
1213 uhci_async_validate_end(s);
1214 /* The spec says frnum is the frame currently being processed, and
1215 * the guest must look at frnum - 1 on interrupt, so inc frnum now */
1216 s->frnum = (s->frnum + 1) & 0x7ff;
1217 s->expire_time += frame_t;
1220 /* Complete the previous frame(s) */
1221 if (s->pending_int_mask) {
1222 s->status2 |= s->pending_int_mask;
1223 s->status |= UHCI_STS_USBINT;
1226 s->pending_int_mask = 0;
1228 qemu_mod_timer(s->frame_timer, t_now + frame_t);
1231 static const MemoryRegionPortio uhci_portio[] = {
1232 { 0, 32, 2, .write = uhci_ioport_writew, },
1233 { 0, 32, 2, .read = uhci_ioport_readw, },
1234 { 0, 32, 4, .write = uhci_ioport_writel, },
1235 { 0, 32, 4, .read = uhci_ioport_readl, },
1236 { 0, 32, 1, .write = uhci_ioport_writeb, },
1237 { 0, 32, 1, .read = uhci_ioport_readb, },
1238 PORTIO_END_OF_LIST()
1241 static const MemoryRegionOps uhci_ioport_ops = {
1242 .old_portio = uhci_portio,
1245 static USBPortOps uhci_port_ops = {
1246 .attach = uhci_attach,
1247 .detach = uhci_detach,
1248 .child_detach = uhci_child_detach,
1249 .wakeup = uhci_wakeup,
1250 .complete = uhci_async_complete,
1253 static USBBusOps uhci_bus_ops = {
1256 static int usb_uhci_common_initfn(PCIDevice *dev)
1258 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1259 UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class);
1260 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1261 uint8_t *pci_conf = s->dev.config;
1264 pci_conf[PCI_CLASS_PROG] = 0x00;
1265 /* TODO: reset value should be 0. */
1266 pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1268 s->irq_pin = u->info.irq_pin;
1269 pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
1272 USBPort *ports[NB_PORTS];
1273 for(i = 0; i < NB_PORTS; i++) {
1274 ports[i] = &s->ports[i].port;
1276 if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1277 s->firstport, s, &uhci_port_ops,
1278 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1282 usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1283 for (i = 0; i < NB_PORTS; i++) {
1284 usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1285 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1288 s->bh = qemu_bh_new(uhci_bh, s);
1289 s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1290 s->num_ports_vmstate = NB_PORTS;
1291 QTAILQ_INIT(&s->queues);
1293 qemu_register_reset(uhci_reset, s);
1295 memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1296 /* Use region 4 for consistency with real hardware. BSD guests seem
1298 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1303 static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1305 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1306 uint8_t *pci_conf = s->dev.config;
1308 /* USB misc control 1/2 */
1309 pci_set_long(pci_conf + 0x40,0x00001000);
1311 pci_set_long(pci_conf + 0x80,0x00020001);
1312 /* USB legacy support */
1313 pci_set_long(pci_conf + 0xc0,0x00002000);
1315 return usb_uhci_common_initfn(dev);
1318 static void usb_uhci_exit(PCIDevice *dev)
1320 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1322 memory_region_destroy(&s->io_bar);
1325 static Property uhci_properties[] = {
1326 DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1327 DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
1328 DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1329 DEFINE_PROP_END_OF_LIST(),
1332 static void uhci_class_init(ObjectClass *klass, void *data)
1334 DeviceClass *dc = DEVICE_CLASS(klass);
1335 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1336 UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class);
1337 UHCIInfo *info = data;
1339 k->init = info->initfn ? info->initfn : usb_uhci_common_initfn;
1340 k->exit = info->unplug ? usb_uhci_exit : NULL;
1341 k->vendor_id = info->vendor_id;
1342 k->device_id = info->device_id;
1343 k->revision = info->revision;
1344 k->class_id = PCI_CLASS_SERIAL_USB;
1346 dc->vmsd = &vmstate_uhci;
1347 dc->props = uhci_properties;
1351 static UHCIInfo uhci_info[] = {
1353 .name = "piix3-usb-uhci",
1354 .vendor_id = PCI_VENDOR_ID_INTEL,
1355 .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
1360 .name = "piix4-usb-uhci",
1361 .vendor_id = PCI_VENDOR_ID_INTEL,
1362 .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
1367 .name = "vt82c686b-usb-uhci",
1368 .vendor_id = PCI_VENDOR_ID_VIA,
1369 .device_id = PCI_DEVICE_ID_VIA_UHCI,
1372 .initfn = usb_uhci_vt82c686b_initfn,
1375 .name = "ich9-usb-uhci1", /* 00:1d.0 */
1376 .vendor_id = PCI_VENDOR_ID_INTEL,
1377 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
1382 .name = "ich9-usb-uhci2", /* 00:1d.1 */
1383 .vendor_id = PCI_VENDOR_ID_INTEL,
1384 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
1389 .name = "ich9-usb-uhci3", /* 00:1d.2 */
1390 .vendor_id = PCI_VENDOR_ID_INTEL,
1391 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
1396 .name = "ich9-usb-uhci4", /* 00:1a.0 */
1397 .vendor_id = PCI_VENDOR_ID_INTEL,
1398 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4,
1403 .name = "ich9-usb-uhci5", /* 00:1a.1 */
1404 .vendor_id = PCI_VENDOR_ID_INTEL,
1405 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5,
1410 .name = "ich9-usb-uhci6", /* 00:1a.2 */
1411 .vendor_id = PCI_VENDOR_ID_INTEL,
1412 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6,
1419 static void uhci_register_types(void)
1421 TypeInfo uhci_type_info = {
1422 .parent = TYPE_PCI_DEVICE,
1423 .instance_size = sizeof(UHCIState),
1424 .class_size = sizeof(UHCIPCIDeviceClass),
1425 .class_init = uhci_class_init,
1429 for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
1430 uhci_type_info.name = uhci_info[i].name;
1431 uhci_type_info.class_data = uhci_info + i;
1432 type_register(&uhci_type_info);
1436 type_init(uhci_register_types)