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"
35 #include "qemu/main-loop.h"
38 //#define DEBUG_DUMP_DATA
40 #define UHCI_CMD_FGR (1 << 4)
41 #define UHCI_CMD_EGSM (1 << 3)
42 #define UHCI_CMD_GRESET (1 << 2)
43 #define UHCI_CMD_HCRESET (1 << 1)
44 #define UHCI_CMD_RS (1 << 0)
46 #define UHCI_STS_HCHALTED (1 << 5)
47 #define UHCI_STS_HCPERR (1 << 4)
48 #define UHCI_STS_HSERR (1 << 3)
49 #define UHCI_STS_RD (1 << 2)
50 #define UHCI_STS_USBERR (1 << 1)
51 #define UHCI_STS_USBINT (1 << 0)
53 #define TD_CTRL_SPD (1 << 29)
54 #define TD_CTRL_ERROR_SHIFT 27
55 #define TD_CTRL_IOS (1 << 25)
56 #define TD_CTRL_IOC (1 << 24)
57 #define TD_CTRL_ACTIVE (1 << 23)
58 #define TD_CTRL_STALL (1 << 22)
59 #define TD_CTRL_BABBLE (1 << 20)
60 #define TD_CTRL_NAK (1 << 19)
61 #define TD_CTRL_TIMEOUT (1 << 18)
63 #define UHCI_PORT_SUSPEND (1 << 12)
64 #define UHCI_PORT_RESET (1 << 9)
65 #define UHCI_PORT_LSDA (1 << 8)
66 #define UHCI_PORT_RD (1 << 6)
67 #define UHCI_PORT_ENC (1 << 3)
68 #define UHCI_PORT_EN (1 << 2)
69 #define UHCI_PORT_CSC (1 << 1)
70 #define UHCI_PORT_CCS (1 << 0)
72 #define UHCI_PORT_READ_ONLY (0x1bb)
73 #define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC)
75 #define FRAME_TIMER_FREQ 1000
77 #define FRAME_MAX_LOOPS 256
79 /* Must be large enough to handle 10 frame delay for initial isoc requests */
82 #define MAX_FRAMES_PER_TICK (QH_VALID / 2)
87 TD_RESULT_STOP_FRAME
= 10,
90 TD_RESULT_ASYNC_START
,
94 typedef struct UHCIState UHCIState
;
95 typedef struct UHCIAsync UHCIAsync
;
96 typedef struct UHCIQueue UHCIQueue
;
97 typedef struct UHCIInfo UHCIInfo
;
98 typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass
;
106 int (*initfn
)(PCIDevice
*dev
);
110 struct UHCIPCIDeviceClass
{
111 PCIDeviceClass parent_class
;
116 * Pending async transaction.
117 * 'packet' must be the first field because completion
118 * handler does "(UHCIAsync *) pkt" cast.
123 uint8_t static_buf
[64]; /* 64 bytes is enough, except for isoc packets */
126 QTAILQ_ENTRY(UHCIAsync
) next
;
136 QTAILQ_ENTRY(UHCIQueue
) next
;
137 QTAILQ_HEAD(asyncs_head
, UHCIAsync
) asyncs
;
141 typedef struct UHCIPort
{
149 USBBus bus
; /* Note unused when we're a companion controller */
150 uint16_t cmd
; /* cmd register */
152 uint16_t intr
; /* interrupt enable register */
153 uint16_t frnum
; /* frame number */
154 uint32_t fl_base_addr
; /* frame list base address */
156 uint8_t status2
; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
158 QEMUTimer
*frame_timer
;
160 uint32_t frame_bytes
;
161 uint32_t frame_bandwidth
;
162 bool completions_only
;
163 UHCIPort ports
[NB_PORTS
];
165 /* Interrupts that should be raised at the end of the current frame. */
166 uint32_t pending_int_mask
;
169 QTAILQ_HEAD(, UHCIQueue
) queues
;
170 uint8_t num_ports_vmstate
;
178 typedef struct UHCI_TD
{
180 uint32_t ctrl
; /* see TD_CTRL_xxx */
185 typedef struct UHCI_QH
{
190 static void uhci_async_cancel(UHCIAsync
*async
);
191 static void uhci_queue_fill(UHCIQueue
*q
, UHCI_TD
*td
);
192 static void uhci_resume(void *opaque
);
194 static inline int32_t uhci_queue_token(UHCI_TD
*td
)
196 if ((td
->token
& (0xf << 15)) == 0) {
197 /* ctrl ep, cover ep and dev, not pid! */
198 return td
->token
& 0x7ff00;
200 /* covers ep, dev, pid -> identifies the endpoint */
201 return td
->token
& 0x7ffff;
205 static UHCIQueue
*uhci_queue_new(UHCIState
*s
, uint32_t qh_addr
, UHCI_TD
*td
,
210 queue
= g_new0(UHCIQueue
, 1);
212 queue
->qh_addr
= qh_addr
;
213 queue
->token
= uhci_queue_token(td
);
215 QTAILQ_INIT(&queue
->asyncs
);
216 QTAILQ_INSERT_HEAD(&s
->queues
, queue
, next
);
217 queue
->valid
= QH_VALID
;
218 trace_usb_uhci_queue_add(queue
->token
);
222 static void uhci_queue_free(UHCIQueue
*queue
, const char *reason
)
224 UHCIState
*s
= queue
->uhci
;
227 while (!QTAILQ_EMPTY(&queue
->asyncs
)) {
228 async
= QTAILQ_FIRST(&queue
->asyncs
);
229 uhci_async_cancel(async
);
231 usb_device_ep_stopped(queue
->ep
->dev
, queue
->ep
);
233 trace_usb_uhci_queue_del(queue
->token
, reason
);
234 QTAILQ_REMOVE(&s
->queues
, queue
, next
);
238 static UHCIQueue
*uhci_queue_find(UHCIState
*s
, UHCI_TD
*td
)
240 uint32_t token
= uhci_queue_token(td
);
243 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
244 if (queue
->token
== token
) {
251 static bool uhci_queue_verify(UHCIQueue
*queue
, uint32_t qh_addr
, UHCI_TD
*td
,
252 uint32_t td_addr
, bool queuing
)
254 UHCIAsync
*first
= QTAILQ_FIRST(&queue
->asyncs
);
256 return queue
->qh_addr
== qh_addr
&&
257 queue
->token
== uhci_queue_token(td
) &&
258 (queuing
|| !(td
->ctrl
& TD_CTRL_ACTIVE
) || first
== NULL
||
259 first
->td_addr
== td_addr
);
262 static UHCIAsync
*uhci_async_alloc(UHCIQueue
*queue
, uint32_t td_addr
)
264 UHCIAsync
*async
= g_new0(UHCIAsync
, 1);
266 async
->queue
= queue
;
267 async
->td_addr
= td_addr
;
268 usb_packet_init(&async
->packet
);
269 trace_usb_uhci_packet_add(async
->queue
->token
, async
->td_addr
);
274 static void uhci_async_free(UHCIAsync
*async
)
276 trace_usb_uhci_packet_del(async
->queue
->token
, async
->td_addr
);
277 usb_packet_cleanup(&async
->packet
);
278 if (async
->buf
!= async
->static_buf
) {
284 static void uhci_async_link(UHCIAsync
*async
)
286 UHCIQueue
*queue
= async
->queue
;
287 QTAILQ_INSERT_TAIL(&queue
->asyncs
, async
, next
);
288 trace_usb_uhci_packet_link_async(async
->queue
->token
, async
->td_addr
);
291 static void uhci_async_unlink(UHCIAsync
*async
)
293 UHCIQueue
*queue
= async
->queue
;
294 QTAILQ_REMOVE(&queue
->asyncs
, async
, next
);
295 trace_usb_uhci_packet_unlink_async(async
->queue
->token
, async
->td_addr
);
298 static void uhci_async_cancel(UHCIAsync
*async
)
300 uhci_async_unlink(async
);
301 trace_usb_uhci_packet_cancel(async
->queue
->token
, async
->td_addr
,
304 usb_cancel_packet(&async
->packet
);
305 uhci_async_free(async
);
309 * Mark all outstanding async packets as invalid.
310 * This is used for canceling them when TDs are removed by the HCD.
312 static void uhci_async_validate_begin(UHCIState
*s
)
316 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
322 * Cancel async packets that are no longer valid
324 static void uhci_async_validate_end(UHCIState
*s
)
326 UHCIQueue
*queue
, *n
;
328 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, n
) {
330 uhci_queue_free(queue
, "validate-end");
335 static void uhci_async_cancel_device(UHCIState
*s
, USBDevice
*dev
)
337 UHCIQueue
*queue
, *n
;
339 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, n
) {
340 if (queue
->ep
->dev
== dev
) {
341 uhci_queue_free(queue
, "cancel-device");
346 static void uhci_async_cancel_all(UHCIState
*s
)
348 UHCIQueue
*queue
, *nq
;
350 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, nq
) {
351 uhci_queue_free(queue
, "cancel-all");
355 static UHCIAsync
*uhci_async_find_td(UHCIState
*s
, uint32_t td_addr
)
360 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
361 QTAILQ_FOREACH(async
, &queue
->asyncs
, next
) {
362 if (async
->td_addr
== td_addr
) {
370 static void uhci_update_irq(UHCIState
*s
)
373 if (((s
->status2
& 1) && (s
->intr
& (1 << 2))) ||
374 ((s
->status2
& 2) && (s
->intr
& (1 << 3))) ||
375 ((s
->status
& UHCI_STS_USBERR
) && (s
->intr
& (1 << 0))) ||
376 ((s
->status
& UHCI_STS_RD
) && (s
->intr
& (1 << 1))) ||
377 (s
->status
& UHCI_STS_HSERR
) ||
378 (s
->status
& UHCI_STS_HCPERR
)) {
383 pci_set_irq(&s
->dev
, level
);
386 static void uhci_reset(void *opaque
)
388 UHCIState
*s
= opaque
;
393 trace_usb_uhci_reset();
395 pci_conf
= s
->dev
.config
;
397 pci_conf
[0x6a] = 0x01; /* usb clock */
398 pci_conf
[0x6b] = 0x00;
406 for(i
= 0; i
< NB_PORTS
; i
++) {
409 if (port
->port
.dev
&& port
->port
.dev
->attached
) {
410 usb_port_reset(&port
->port
);
414 uhci_async_cancel_all(s
);
415 qemu_bh_cancel(s
->bh
);
419 static const VMStateDescription vmstate_uhci_port
= {
422 .minimum_version_id
= 1,
423 .minimum_version_id_old
= 1,
424 .fields
= (VMStateField
[]) {
425 VMSTATE_UINT16(ctrl
, UHCIPort
),
426 VMSTATE_END_OF_LIST()
430 static int uhci_post_load(void *opaque
, int version_id
)
432 UHCIState
*s
= opaque
;
434 if (version_id
< 2) {
435 s
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
436 (get_ticks_per_sec() / FRAME_TIMER_FREQ
);
441 static const VMStateDescription vmstate_uhci
= {
444 .minimum_version_id
= 1,
445 .minimum_version_id_old
= 1,
446 .post_load
= uhci_post_load
,
447 .fields
= (VMStateField
[]) {
448 VMSTATE_PCI_DEVICE(dev
, UHCIState
),
449 VMSTATE_UINT8_EQUAL(num_ports_vmstate
, UHCIState
),
450 VMSTATE_STRUCT_ARRAY(ports
, UHCIState
, NB_PORTS
, 1,
451 vmstate_uhci_port
, UHCIPort
),
452 VMSTATE_UINT16(cmd
, UHCIState
),
453 VMSTATE_UINT16(status
, UHCIState
),
454 VMSTATE_UINT16(intr
, UHCIState
),
455 VMSTATE_UINT16(frnum
, UHCIState
),
456 VMSTATE_UINT32(fl_base_addr
, UHCIState
),
457 VMSTATE_UINT8(sof_timing
, UHCIState
),
458 VMSTATE_UINT8(status2
, UHCIState
),
459 VMSTATE_TIMER(frame_timer
, UHCIState
),
460 VMSTATE_INT64_V(expire_time
, UHCIState
, 2),
461 VMSTATE_UINT32_V(pending_int_mask
, UHCIState
, 3),
462 VMSTATE_END_OF_LIST()
466 static void uhci_port_write(void *opaque
, hwaddr addr
,
467 uint64_t val
, unsigned size
)
469 UHCIState
*s
= opaque
;
471 trace_usb_uhci_mmio_writew(addr
, val
);
475 if ((val
& UHCI_CMD_RS
) && !(s
->cmd
& UHCI_CMD_RS
)) {
476 /* start frame processing */
477 trace_usb_uhci_schedule_start();
478 s
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
479 (get_ticks_per_sec() / FRAME_TIMER_FREQ
);
480 timer_mod(s
->frame_timer
, s
->expire_time
);
481 s
->status
&= ~UHCI_STS_HCHALTED
;
482 } else if (!(val
& UHCI_CMD_RS
)) {
483 s
->status
|= UHCI_STS_HCHALTED
;
485 if (val
& UHCI_CMD_GRESET
) {
489 /* send reset on the USB bus */
490 for(i
= 0; i
< NB_PORTS
; i
++) {
492 usb_device_reset(port
->port
.dev
);
497 if (val
& UHCI_CMD_HCRESET
) {
502 if (val
& UHCI_CMD_EGSM
) {
503 if ((s
->ports
[0].ctrl
& UHCI_PORT_RD
) ||
504 (s
->ports
[1].ctrl
& UHCI_PORT_RD
)) {
511 /* XXX: the chip spec is not coherent, so we add a hidden
512 register to distinguish between IOC and SPD */
513 if (val
& UHCI_STS_USBINT
)
522 if (s
->status
& UHCI_STS_HCHALTED
)
523 s
->frnum
= val
& 0x7ff;
526 s
->fl_base_addr
&= 0xffff0000;
527 s
->fl_base_addr
|= val
& ~0xfff;
530 s
->fl_base_addr
&= 0x0000ffff;
531 s
->fl_base_addr
|= (val
<< 16);
534 s
->sof_timing
= val
& 0xff;
546 dev
= port
->port
.dev
;
547 if (dev
&& dev
->attached
) {
549 if ( (val
& UHCI_PORT_RESET
) &&
550 !(port
->ctrl
& UHCI_PORT_RESET
) ) {
551 usb_device_reset(dev
);
554 port
->ctrl
&= UHCI_PORT_READ_ONLY
;
555 /* enabled may only be set if a device is connected */
556 if (!(port
->ctrl
& UHCI_PORT_CCS
)) {
557 val
&= ~UHCI_PORT_EN
;
559 port
->ctrl
|= (val
& ~UHCI_PORT_READ_ONLY
);
560 /* some bits are reset when a '1' is written to them */
561 port
->ctrl
&= ~(val
& UHCI_PORT_WRITE_CLEAR
);
567 static uint64_t uhci_port_read(void *opaque
, hwaddr addr
, unsigned size
)
569 UHCIState
*s
= opaque
;
586 val
= s
->fl_base_addr
& 0xffff;
589 val
= (s
->fl_base_addr
>> 16) & 0xffff;
607 val
= 0xff7f; /* disabled port */
611 trace_usb_uhci_mmio_readw(addr
, val
);
616 /* signal resume if controller suspended */
617 static void uhci_resume (void *opaque
)
619 UHCIState
*s
= (UHCIState
*)opaque
;
624 if (s
->cmd
& UHCI_CMD_EGSM
) {
625 s
->cmd
|= UHCI_CMD_FGR
;
626 s
->status
|= UHCI_STS_RD
;
631 static void uhci_attach(USBPort
*port1
)
633 UHCIState
*s
= port1
->opaque
;
634 UHCIPort
*port
= &s
->ports
[port1
->index
];
636 /* set connect status */
637 port
->ctrl
|= UHCI_PORT_CCS
| UHCI_PORT_CSC
;
640 if (port
->port
.dev
->speed
== USB_SPEED_LOW
) {
641 port
->ctrl
|= UHCI_PORT_LSDA
;
643 port
->ctrl
&= ~UHCI_PORT_LSDA
;
649 static void uhci_detach(USBPort
*port1
)
651 UHCIState
*s
= port1
->opaque
;
652 UHCIPort
*port
= &s
->ports
[port1
->index
];
654 uhci_async_cancel_device(s
, port1
->dev
);
656 /* set connect status */
657 if (port
->ctrl
& UHCI_PORT_CCS
) {
658 port
->ctrl
&= ~UHCI_PORT_CCS
;
659 port
->ctrl
|= UHCI_PORT_CSC
;
662 if (port
->ctrl
& UHCI_PORT_EN
) {
663 port
->ctrl
&= ~UHCI_PORT_EN
;
664 port
->ctrl
|= UHCI_PORT_ENC
;
670 static void uhci_child_detach(USBPort
*port1
, USBDevice
*child
)
672 UHCIState
*s
= port1
->opaque
;
674 uhci_async_cancel_device(s
, child
);
677 static void uhci_wakeup(USBPort
*port1
)
679 UHCIState
*s
= port1
->opaque
;
680 UHCIPort
*port
= &s
->ports
[port1
->index
];
682 if (port
->ctrl
& UHCI_PORT_SUSPEND
&& !(port
->ctrl
& UHCI_PORT_RD
)) {
683 port
->ctrl
|= UHCI_PORT_RD
;
688 static USBDevice
*uhci_find_device(UHCIState
*s
, uint8_t addr
)
693 for (i
= 0; i
< NB_PORTS
; i
++) {
694 UHCIPort
*port
= &s
->ports
[i
];
695 if (!(port
->ctrl
& UHCI_PORT_EN
)) {
698 dev
= usb_find_device(&port
->port
, addr
);
706 static void uhci_read_td(UHCIState
*s
, UHCI_TD
*td
, uint32_t link
)
708 pci_dma_read(&s
->dev
, link
& ~0xf, td
, sizeof(*td
));
709 le32_to_cpus(&td
->link
);
710 le32_to_cpus(&td
->ctrl
);
711 le32_to_cpus(&td
->token
);
712 le32_to_cpus(&td
->buffer
);
715 static int uhci_handle_td_error(UHCIState
*s
, UHCI_TD
*td
, uint32_t td_addr
,
716 int status
, uint32_t *int_mask
)
718 uint32_t queue_token
= uhci_queue_token(td
);
723 td
->ctrl
|= TD_CTRL_NAK
;
724 return TD_RESULT_NEXT_QH
;
727 td
->ctrl
|= TD_CTRL_STALL
;
728 trace_usb_uhci_packet_complete_stall(queue_token
, td_addr
);
729 ret
= TD_RESULT_NEXT_QH
;
733 td
->ctrl
|= TD_CTRL_BABBLE
| TD_CTRL_STALL
;
734 /* frame interrupted */
735 trace_usb_uhci_packet_complete_babble(queue_token
, td_addr
);
736 ret
= TD_RESULT_STOP_FRAME
;
739 case USB_RET_IOERROR
:
742 td
->ctrl
|= TD_CTRL_TIMEOUT
;
743 td
->ctrl
&= ~(3 << TD_CTRL_ERROR_SHIFT
);
744 trace_usb_uhci_packet_complete_error(queue_token
, td_addr
);
745 ret
= TD_RESULT_NEXT_QH
;
749 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
750 s
->status
|= UHCI_STS_USBERR
;
751 if (td
->ctrl
& TD_CTRL_IOC
) {
758 static int uhci_complete_td(UHCIState
*s
, UHCI_TD
*td
, UHCIAsync
*async
, uint32_t *int_mask
)
760 int len
= 0, max_len
;
763 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
764 pid
= td
->token
& 0xff;
766 if (td
->ctrl
& TD_CTRL_IOS
)
767 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
769 if (async
->packet
.status
!= USB_RET_SUCCESS
) {
770 return uhci_handle_td_error(s
, td
, async
->td_addr
,
771 async
->packet
.status
, int_mask
);
774 len
= async
->packet
.actual_length
;
775 td
->ctrl
= (td
->ctrl
& ~0x7ff) | ((len
- 1) & 0x7ff);
777 /* The NAK bit may have been set by a previous frame, so clear it
778 here. The docs are somewhat unclear, but win2k relies on this
780 td
->ctrl
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_NAK
);
781 if (td
->ctrl
& TD_CTRL_IOC
)
784 if (pid
== USB_TOKEN_IN
) {
785 pci_dma_write(&s
->dev
, td
->buffer
, async
->buf
, len
);
786 if ((td
->ctrl
& TD_CTRL_SPD
) && len
< max_len
) {
788 /* short packet: do not update QH */
789 trace_usb_uhci_packet_complete_shortxfer(async
->queue
->token
,
791 return TD_RESULT_NEXT_QH
;
796 trace_usb_uhci_packet_complete_success(async
->queue
->token
,
798 return TD_RESULT_COMPLETE
;
801 static int uhci_handle_td(UHCIState
*s
, UHCIQueue
*q
, uint32_t qh_addr
,
802 UHCI_TD
*td
, uint32_t td_addr
, uint32_t *int_mask
)
806 bool queuing
= (q
!= NULL
);
807 uint8_t pid
= td
->token
& 0xff;
808 UHCIAsync
*async
= uhci_async_find_td(s
, td_addr
);
811 if (uhci_queue_verify(async
->queue
, qh_addr
, td
, td_addr
, queuing
)) {
812 assert(q
== NULL
|| q
== async
->queue
);
815 uhci_queue_free(async
->queue
, "guest re-used pending td");
821 q
= uhci_queue_find(s
, td
);
822 if (q
&& !uhci_queue_verify(q
, qh_addr
, td
, td_addr
, queuing
)) {
823 uhci_queue_free(q
, "guest re-used qh");
833 if (!(td
->ctrl
& TD_CTRL_ACTIVE
)) {
835 /* Guest marked a pending td non-active, cancel the queue */
836 uhci_queue_free(async
->queue
, "pending td non-active");
839 * ehci11d spec page 22: "Even if the Active bit in the TD is already
840 * cleared when the TD is fetched ... an IOC interrupt is generated"
842 if (td
->ctrl
& TD_CTRL_IOC
) {
845 return TD_RESULT_NEXT_QH
;
850 /* we are busy filling the queue, we are not prepared
851 to consume completed packages then, just leave them
853 return TD_RESULT_ASYNC_CONT
;
857 UHCIAsync
*last
= QTAILQ_LAST(&async
->queue
->asyncs
, asyncs_head
);
859 * While we are waiting for the current td to complete, the guest
860 * may have added more tds to the queue. Note we re-read the td
861 * rather then caching it, as we want to see guest made changes!
863 uhci_read_td(s
, &last_td
, last
->td_addr
);
864 uhci_queue_fill(async
->queue
, &last_td
);
866 return TD_RESULT_ASYNC_CONT
;
868 uhci_async_unlink(async
);
872 if (s
->completions_only
) {
873 return TD_RESULT_ASYNC_CONT
;
876 /* Allocate new packet */
878 USBDevice
*dev
= uhci_find_device(s
, (td
->token
>> 8) & 0x7f);
879 USBEndpoint
*ep
= usb_ep_get(dev
, pid
, (td
->token
>> 15) & 0xf);
882 return uhci_handle_td_error(s
, td
, td_addr
, USB_RET_NODEV
,
885 q
= uhci_queue_new(s
, qh_addr
, td
, ep
);
887 async
= uhci_async_alloc(q
, td_addr
);
889 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
890 spd
= (pid
== USB_TOKEN_IN
&& (td
->ctrl
& TD_CTRL_SPD
) != 0);
891 usb_packet_setup(&async
->packet
, pid
, q
->ep
, 0, td_addr
, spd
,
892 (td
->ctrl
& TD_CTRL_IOC
) != 0);
893 if (max_len
<= sizeof(async
->static_buf
)) {
894 async
->buf
= async
->static_buf
;
896 async
->buf
= g_malloc(max_len
);
898 usb_packet_addbuf(&async
->packet
, async
->buf
, max_len
);
902 case USB_TOKEN_SETUP
:
903 pci_dma_read(&s
->dev
, td
->buffer
, async
->buf
, max_len
);
904 usb_handle_packet(q
->ep
->dev
, &async
->packet
);
905 if (async
->packet
.status
== USB_RET_SUCCESS
) {
906 async
->packet
.actual_length
= max_len
;
911 usb_handle_packet(q
->ep
->dev
, &async
->packet
);
915 /* invalid pid : frame interrupted */
916 uhci_async_free(async
);
917 s
->status
|= UHCI_STS_HCPERR
;
919 return TD_RESULT_STOP_FRAME
;
922 if (async
->packet
.status
== USB_RET_ASYNC
) {
923 uhci_async_link(async
);
925 uhci_queue_fill(q
, td
);
927 return TD_RESULT_ASYNC_START
;
931 ret
= uhci_complete_td(s
, td
, async
, int_mask
);
932 uhci_async_free(async
);
936 static void uhci_async_complete(USBPort
*port
, USBPacket
*packet
)
938 UHCIAsync
*async
= container_of(packet
, UHCIAsync
, packet
);
939 UHCIState
*s
= async
->queue
->uhci
;
941 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
942 uhci_async_cancel(async
);
947 /* Force processing of this packet *now*, needed for migration */
948 s
->completions_only
= true;
949 qemu_bh_schedule(s
->bh
);
952 static int is_valid(uint32_t link
)
954 return (link
& 1) == 0;
957 static int is_qh(uint32_t link
)
959 return (link
& 2) != 0;
962 static int depth_first(uint32_t link
)
964 return (link
& 4) != 0;
967 /* QH DB used for detecting QH loops */
968 #define UHCI_MAX_QUEUES 128
970 uint32_t addr
[UHCI_MAX_QUEUES
];
974 static void qhdb_reset(QhDb
*db
)
979 /* Add QH to DB. Returns 1 if already present or DB is full. */
980 static int qhdb_insert(QhDb
*db
, uint32_t addr
)
983 for (i
= 0; i
< db
->count
; i
++)
984 if (db
->addr
[i
] == addr
)
987 if (db
->count
>= UHCI_MAX_QUEUES
)
990 db
->addr
[db
->count
++] = addr
;
994 static void uhci_queue_fill(UHCIQueue
*q
, UHCI_TD
*td
)
996 uint32_t int_mask
= 0;
997 uint32_t plink
= td
->link
;
1001 while (is_valid(plink
)) {
1002 uhci_read_td(q
->uhci
, &ptd
, plink
);
1003 if (!(ptd
.ctrl
& TD_CTRL_ACTIVE
)) {
1006 if (uhci_queue_token(&ptd
) != q
->token
) {
1009 trace_usb_uhci_td_queue(plink
& ~0xf, ptd
.ctrl
, ptd
.token
);
1010 ret
= uhci_handle_td(q
->uhci
, q
, q
->qh_addr
, &ptd
, plink
, &int_mask
);
1011 if (ret
== TD_RESULT_ASYNC_CONT
) {
1014 assert(ret
== TD_RESULT_ASYNC_START
);
1015 assert(int_mask
== 0);
1018 usb_device_flush_ep_queue(q
->ep
->dev
, q
->ep
);
1021 static void uhci_process_frame(UHCIState
*s
)
1023 uint32_t frame_addr
, link
, old_td_ctrl
, val
, int_mask
;
1024 uint32_t curr_qh
, td_count
= 0;
1030 frame_addr
= s
->fl_base_addr
+ ((s
->frnum
& 0x3ff) << 2);
1032 pci_dma_read(&s
->dev
, frame_addr
, &link
, 4);
1033 le32_to_cpus(&link
);
1040 for (cnt
= FRAME_MAX_LOOPS
; is_valid(link
) && cnt
; cnt
--) {
1041 if (!s
->completions_only
&& s
->frame_bytes
>= s
->frame_bandwidth
) {
1042 /* We've reached the usb 1.1 bandwidth, which is
1043 1280 bytes/frame, stop processing */
1044 trace_usb_uhci_frame_stop_bandwidth();
1049 trace_usb_uhci_qh_load(link
& ~0xf);
1051 if (qhdb_insert(&qhdb
, link
)) {
1053 * We're going in circles. Which is not a bug because
1054 * HCD is allowed to do that as part of the BW management.
1056 * Stop processing here if no transaction has been done
1057 * since we've been here last time.
1059 if (td_count
== 0) {
1060 trace_usb_uhci_frame_loop_stop_idle();
1063 trace_usb_uhci_frame_loop_continue();
1066 qhdb_insert(&qhdb
, link
);
1070 pci_dma_read(&s
->dev
, link
& ~0xf, &qh
, sizeof(qh
));
1071 le32_to_cpus(&qh
.link
);
1072 le32_to_cpus(&qh
.el_link
);
1074 if (!is_valid(qh
.el_link
)) {
1075 /* QH w/o elements */
1079 /* QH with elements */
1087 uhci_read_td(s
, &td
, link
);
1088 trace_usb_uhci_td_load(curr_qh
& ~0xf, link
& ~0xf, td
.ctrl
, td
.token
);
1090 old_td_ctrl
= td
.ctrl
;
1091 ret
= uhci_handle_td(s
, NULL
, curr_qh
, &td
, link
, &int_mask
);
1092 if (old_td_ctrl
!= td
.ctrl
) {
1093 /* update the status bits of the TD */
1094 val
= cpu_to_le32(td
.ctrl
);
1095 pci_dma_write(&s
->dev
, (link
& ~0xf) + 4, &val
, sizeof(val
));
1099 case TD_RESULT_STOP_FRAME
: /* interrupted frame */
1102 case TD_RESULT_NEXT_QH
:
1103 case TD_RESULT_ASYNC_CONT
:
1104 trace_usb_uhci_td_nextqh(curr_qh
& ~0xf, link
& ~0xf);
1105 link
= curr_qh
? qh
.link
: td
.link
;
1108 case TD_RESULT_ASYNC_START
:
1109 trace_usb_uhci_td_async(curr_qh
& ~0xf, link
& ~0xf);
1110 link
= curr_qh
? qh
.link
: td
.link
;
1113 case TD_RESULT_COMPLETE
:
1114 trace_usb_uhci_td_complete(curr_qh
& ~0xf, link
& ~0xf);
1117 s
->frame_bytes
+= (td
.ctrl
& 0x7ff) + 1;
1120 /* update QH element link */
1122 val
= cpu_to_le32(qh
.el_link
);
1123 pci_dma_write(&s
->dev
, (curr_qh
& ~0xf) + 4, &val
, sizeof(val
));
1125 if (!depth_first(link
)) {
1126 /* done with this QH */
1134 assert(!"unknown return code");
1137 /* go to the next entry */
1141 s
->pending_int_mask
|= int_mask
;
1144 static void uhci_bh(void *opaque
)
1146 UHCIState
*s
= opaque
;
1147 uhci_process_frame(s
);
1150 static void uhci_frame_timer(void *opaque
)
1152 UHCIState
*s
= opaque
;
1153 uint64_t t_now
, t_last_run
;
1155 const uint64_t frame_t
= get_ticks_per_sec() / FRAME_TIMER_FREQ
;
1157 s
->completions_only
= false;
1158 qemu_bh_cancel(s
->bh
);
1160 if (!(s
->cmd
& UHCI_CMD_RS
)) {
1162 trace_usb_uhci_schedule_stop();
1163 timer_del(s
->frame_timer
);
1164 uhci_async_cancel_all(s
);
1165 /* set hchalted bit in status - UHCI11D 2.1.2 */
1166 s
->status
|= UHCI_STS_HCHALTED
;
1170 /* We still store expire_time in our state, for migration */
1171 t_last_run
= s
->expire_time
- frame_t
;
1172 t_now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1174 /* Process up to MAX_FRAMES_PER_TICK frames */
1175 frames
= (t_now
- t_last_run
) / frame_t
;
1176 if (frames
> s
->maxframes
) {
1177 int skipped
= frames
- s
->maxframes
;
1178 s
->expire_time
+= skipped
* frame_t
;
1179 s
->frnum
= (s
->frnum
+ skipped
) & 0x7ff;
1182 if (frames
> MAX_FRAMES_PER_TICK
) {
1183 frames
= MAX_FRAMES_PER_TICK
;
1186 for (i
= 0; i
< frames
; i
++) {
1188 trace_usb_uhci_frame_start(s
->frnum
);
1189 uhci_async_validate_begin(s
);
1190 uhci_process_frame(s
);
1191 uhci_async_validate_end(s
);
1192 /* The spec says frnum is the frame currently being processed, and
1193 * the guest must look at frnum - 1 on interrupt, so inc frnum now */
1194 s
->frnum
= (s
->frnum
+ 1) & 0x7ff;
1195 s
->expire_time
+= frame_t
;
1198 /* Complete the previous frame(s) */
1199 if (s
->pending_int_mask
) {
1200 s
->status2
|= s
->pending_int_mask
;
1201 s
->status
|= UHCI_STS_USBINT
;
1204 s
->pending_int_mask
= 0;
1206 timer_mod(s
->frame_timer
, t_now
+ frame_t
);
1209 static const MemoryRegionOps uhci_ioport_ops
= {
1210 .read
= uhci_port_read
,
1211 .write
= uhci_port_write
,
1212 .valid
.min_access_size
= 1,
1213 .valid
.max_access_size
= 4,
1214 .impl
.min_access_size
= 2,
1215 .impl
.max_access_size
= 2,
1216 .endianness
= DEVICE_LITTLE_ENDIAN
,
1219 static USBPortOps uhci_port_ops
= {
1220 .attach
= uhci_attach
,
1221 .detach
= uhci_detach
,
1222 .child_detach
= uhci_child_detach
,
1223 .wakeup
= uhci_wakeup
,
1224 .complete
= uhci_async_complete
,
1227 static USBBusOps uhci_bus_ops
= {
1230 static int usb_uhci_common_initfn(PCIDevice
*dev
)
1232 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(dev
);
1233 UHCIPCIDeviceClass
*u
= container_of(pc
, UHCIPCIDeviceClass
, parent_class
);
1234 UHCIState
*s
= DO_UPCAST(UHCIState
, dev
, dev
);
1235 uint8_t *pci_conf
= s
->dev
.config
;
1238 pci_conf
[PCI_CLASS_PROG
] = 0x00;
1239 /* TODO: reset value should be 0. */
1240 pci_conf
[USB_SBRN
] = USB_RELEASE_1
; // release number
1242 pci_config_set_interrupt_pin(pci_conf
, u
->info
.irq_pin
+ 1);
1245 USBPort
*ports
[NB_PORTS
];
1246 for(i
= 0; i
< NB_PORTS
; i
++) {
1247 ports
[i
] = &s
->ports
[i
].port
;
1249 if (usb_register_companion(s
->masterbus
, ports
, NB_PORTS
,
1250 s
->firstport
, s
, &uhci_port_ops
,
1251 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
) != 0) {
1255 usb_bus_new(&s
->bus
, sizeof(s
->bus
), &uhci_bus_ops
, DEVICE(dev
));
1256 for (i
= 0; i
< NB_PORTS
; i
++) {
1257 usb_register_port(&s
->bus
, &s
->ports
[i
].port
, s
, i
, &uhci_port_ops
,
1258 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
1261 s
->bh
= qemu_bh_new(uhci_bh
, s
);
1262 s
->frame_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, uhci_frame_timer
, s
);
1263 s
->num_ports_vmstate
= NB_PORTS
;
1264 QTAILQ_INIT(&s
->queues
);
1266 qemu_register_reset(uhci_reset
, s
);
1268 memory_region_init_io(&s
->io_bar
, OBJECT(s
), &uhci_ioport_ops
, s
,
1271 /* Use region 4 for consistency with real hardware. BSD guests seem
1273 pci_register_bar(&s
->dev
, 4, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_bar
);
1278 static int usb_uhci_vt82c686b_initfn(PCIDevice
*dev
)
1280 UHCIState
*s
= DO_UPCAST(UHCIState
, dev
, dev
);
1281 uint8_t *pci_conf
= s
->dev
.config
;
1283 /* USB misc control 1/2 */
1284 pci_set_long(pci_conf
+ 0x40,0x00001000);
1286 pci_set_long(pci_conf
+ 0x80,0x00020001);
1287 /* USB legacy support */
1288 pci_set_long(pci_conf
+ 0xc0,0x00002000);
1290 return usb_uhci_common_initfn(dev
);
1293 static void usb_uhci_exit(PCIDevice
*dev
)
1295 UHCIState
*s
= DO_UPCAST(UHCIState
, dev
, dev
);
1297 memory_region_destroy(&s
->io_bar
);
1300 static Property uhci_properties
[] = {
1301 DEFINE_PROP_STRING("masterbus", UHCIState
, masterbus
),
1302 DEFINE_PROP_UINT32("firstport", UHCIState
, firstport
, 0),
1303 DEFINE_PROP_UINT32("bandwidth", UHCIState
, frame_bandwidth
, 1280),
1304 DEFINE_PROP_UINT32("maxframes", UHCIState
, maxframes
, 128),
1305 DEFINE_PROP_END_OF_LIST(),
1308 static void uhci_class_init(ObjectClass
*klass
, void *data
)
1310 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1311 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1312 UHCIPCIDeviceClass
*u
= container_of(k
, UHCIPCIDeviceClass
, parent_class
);
1313 UHCIInfo
*info
= data
;
1315 k
->init
= info
->initfn
? info
->initfn
: usb_uhci_common_initfn
;
1316 k
->exit
= info
->unplug
? usb_uhci_exit
: NULL
;
1317 k
->vendor_id
= info
->vendor_id
;
1318 k
->device_id
= info
->device_id
;
1319 k
->revision
= info
->revision
;
1320 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1322 dc
->vmsd
= &vmstate_uhci
;
1323 dc
->props
= uhci_properties
;
1324 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
1328 static UHCIInfo uhci_info
[] = {
1330 .name
= "piix3-usb-uhci",
1331 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1332 .device_id
= PCI_DEVICE_ID_INTEL_82371SB_2
,
1337 .name
= "piix4-usb-uhci",
1338 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1339 .device_id
= PCI_DEVICE_ID_INTEL_82371AB_2
,
1344 .name
= "vt82c686b-usb-uhci",
1345 .vendor_id
= PCI_VENDOR_ID_VIA
,
1346 .device_id
= PCI_DEVICE_ID_VIA_UHCI
,
1349 .initfn
= usb_uhci_vt82c686b_initfn
,
1352 .name
= "ich9-usb-uhci1", /* 00:1d.0 */
1353 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1354 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI1
,
1359 .name
= "ich9-usb-uhci2", /* 00:1d.1 */
1360 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1361 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI2
,
1366 .name
= "ich9-usb-uhci3", /* 00:1d.2 */
1367 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1368 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI3
,
1373 .name
= "ich9-usb-uhci4", /* 00:1a.0 */
1374 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1375 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI4
,
1380 .name
= "ich9-usb-uhci5", /* 00:1a.1 */
1381 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1382 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI5
,
1387 .name
= "ich9-usb-uhci6", /* 00:1a.2 */
1388 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1389 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI6
,
1396 static void uhci_register_types(void)
1398 TypeInfo uhci_type_info
= {
1399 .parent
= TYPE_PCI_DEVICE
,
1400 .instance_size
= sizeof(UHCIState
),
1401 .class_size
= sizeof(UHCIPCIDeviceClass
),
1402 .class_init
= uhci_class_init
,
1406 for (i
= 0; i
< ARRAY_SIZE(uhci_info
); i
++) {
1407 uhci_type_info
.name
= uhci_info
[i
].name
;
1408 uhci_type_info
.class_data
= uhci_info
+ i
;
1409 type_register(&uhci_type_info
);
1413 type_init(uhci_register_types
)