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
29 #include "qemu/osdep.h"
31 #include "hw/usb/uhci-regs.h"
32 #include "migration/vmstate.h"
33 #include "hw/pci/pci.h"
35 #include "hw/qdev-properties.h"
36 #include "qapi/error.h"
37 #include "qemu/timer.h"
39 #include "sysemu/dma.h"
41 #include "qemu/main-loop.h"
42 #include "qemu/module.h"
43 #include "qom/object.h"
46 #define FRAME_TIMER_FREQ 1000
48 #define FRAME_MAX_LOOPS 256
50 /* Must be large enough to handle 10 frame delay for initial isoc requests */
53 #define MAX_FRAMES_PER_TICK (QH_VALID / 2)
56 TD_RESULT_STOP_FRAME
= 10,
59 TD_RESULT_ASYNC_START
,
63 typedef struct UHCIAsync UHCIAsync
;
65 struct UHCIPCIDeviceClass
{
66 PCIDeviceClass parent_class
;
71 * Pending async transaction.
72 * 'packet' must be the first field because completion
73 * handler does "(UHCIAsync *) pkt" cast.
78 uint8_t static_buf
[64]; /* 64 bytes is enough, except for isoc packets */
81 QTAILQ_ENTRY(UHCIAsync
) next
;
91 QTAILQ_ENTRY(UHCIQueue
) next
;
92 QTAILQ_HEAD(, UHCIAsync
) asyncs
;
96 typedef struct UHCI_TD
{
98 uint32_t ctrl
; /* see TD_CTRL_xxx */
103 typedef struct UHCI_QH
{
108 static void uhci_async_cancel(UHCIAsync
*async
);
109 static void uhci_queue_fill(UHCIQueue
*q
, UHCI_TD
*td
);
110 static void uhci_resume(void *opaque
);
112 static inline int32_t uhci_queue_token(UHCI_TD
*td
)
114 if ((td
->token
& (0xf << 15)) == 0) {
115 /* ctrl ep, cover ep and dev, not pid! */
116 return td
->token
& 0x7ff00;
118 /* covers ep, dev, pid -> identifies the endpoint */
119 return td
->token
& 0x7ffff;
123 static UHCIQueue
*uhci_queue_new(UHCIState
*s
, uint32_t qh_addr
, UHCI_TD
*td
,
128 queue
= g_new0(UHCIQueue
, 1);
130 queue
->qh_addr
= qh_addr
;
131 queue
->token
= uhci_queue_token(td
);
133 QTAILQ_INIT(&queue
->asyncs
);
134 QTAILQ_INSERT_HEAD(&s
->queues
, queue
, next
);
135 queue
->valid
= QH_VALID
;
136 trace_usb_uhci_queue_add(queue
->token
);
140 static void uhci_queue_free(UHCIQueue
*queue
, const char *reason
)
142 UHCIState
*s
= queue
->uhci
;
145 while (!QTAILQ_EMPTY(&queue
->asyncs
)) {
146 async
= QTAILQ_FIRST(&queue
->asyncs
);
147 uhci_async_cancel(async
);
149 usb_device_ep_stopped(queue
->ep
->dev
, queue
->ep
);
151 trace_usb_uhci_queue_del(queue
->token
, reason
);
152 QTAILQ_REMOVE(&s
->queues
, queue
, next
);
156 static UHCIQueue
*uhci_queue_find(UHCIState
*s
, UHCI_TD
*td
)
158 uint32_t token
= uhci_queue_token(td
);
161 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
162 if (queue
->token
== token
) {
169 static bool uhci_queue_verify(UHCIQueue
*queue
, uint32_t qh_addr
, UHCI_TD
*td
,
170 uint32_t td_addr
, bool queuing
)
172 UHCIAsync
*first
= QTAILQ_FIRST(&queue
->asyncs
);
173 uint32_t queue_token_addr
= (queue
->token
>> 8) & 0x7f;
175 return queue
->qh_addr
== qh_addr
&&
176 queue
->token
== uhci_queue_token(td
) &&
177 queue_token_addr
== queue
->ep
->dev
->addr
&&
178 (queuing
|| !(td
->ctrl
& TD_CTRL_ACTIVE
) || first
== NULL
||
179 first
->td_addr
== td_addr
);
182 static UHCIAsync
*uhci_async_alloc(UHCIQueue
*queue
, uint32_t td_addr
)
184 UHCIAsync
*async
= g_new0(UHCIAsync
, 1);
186 async
->queue
= queue
;
187 async
->td_addr
= td_addr
;
188 usb_packet_init(&async
->packet
);
189 trace_usb_uhci_packet_add(async
->queue
->token
, async
->td_addr
);
194 static void uhci_async_free(UHCIAsync
*async
)
196 trace_usb_uhci_packet_del(async
->queue
->token
, async
->td_addr
);
197 usb_packet_cleanup(&async
->packet
);
198 if (async
->buf
!= async
->static_buf
) {
204 static void uhci_async_link(UHCIAsync
*async
)
206 UHCIQueue
*queue
= async
->queue
;
207 QTAILQ_INSERT_TAIL(&queue
->asyncs
, async
, next
);
208 trace_usb_uhci_packet_link_async(async
->queue
->token
, async
->td_addr
);
211 static void uhci_async_unlink(UHCIAsync
*async
)
213 UHCIQueue
*queue
= async
->queue
;
214 QTAILQ_REMOVE(&queue
->asyncs
, async
, next
);
215 trace_usb_uhci_packet_unlink_async(async
->queue
->token
, async
->td_addr
);
218 static void uhci_async_cancel(UHCIAsync
*async
)
220 uhci_async_unlink(async
);
221 trace_usb_uhci_packet_cancel(async
->queue
->token
, async
->td_addr
,
224 usb_cancel_packet(&async
->packet
);
225 uhci_async_free(async
);
229 * Mark all outstanding async packets as invalid.
230 * This is used for canceling them when TDs are removed by the HCD.
232 static void uhci_async_validate_begin(UHCIState
*s
)
236 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
242 * Cancel async packets that are no longer valid
244 static void uhci_async_validate_end(UHCIState
*s
)
246 UHCIQueue
*queue
, *n
;
248 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, n
) {
250 uhci_queue_free(queue
, "validate-end");
255 static void uhci_async_cancel_device(UHCIState
*s
, USBDevice
*dev
)
257 UHCIQueue
*queue
, *n
;
259 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, n
) {
260 if (queue
->ep
->dev
== dev
) {
261 uhci_queue_free(queue
, "cancel-device");
266 static void uhci_async_cancel_all(UHCIState
*s
)
268 UHCIQueue
*queue
, *nq
;
270 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, nq
) {
271 uhci_queue_free(queue
, "cancel-all");
275 static UHCIAsync
*uhci_async_find_td(UHCIState
*s
, uint32_t td_addr
)
280 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
281 QTAILQ_FOREACH(async
, &queue
->asyncs
, next
) {
282 if (async
->td_addr
== td_addr
) {
290 static void uhci_update_irq(UHCIState
*s
)
293 if (((s
->status2
& 1) && (s
->intr
& (1 << 2))) ||
294 ((s
->status2
& 2) && (s
->intr
& (1 << 3))) ||
295 ((s
->status
& UHCI_STS_USBERR
) && (s
->intr
& (1 << 0))) ||
296 ((s
->status
& UHCI_STS_RD
) && (s
->intr
& (1 << 1))) ||
297 (s
->status
& UHCI_STS_HSERR
) ||
298 (s
->status
& UHCI_STS_HCPERR
)) {
301 qemu_set_irq(s
->irq
, level
);
304 static void uhci_reset(DeviceState
*dev
)
306 PCIDevice
*d
= PCI_DEVICE(dev
);
307 UHCIState
*s
= UHCI(d
);
312 trace_usb_uhci_reset();
314 pci_conf
= s
->dev
.config
;
316 pci_conf
[0x6a] = 0x01; /* usb clock */
317 pci_conf
[0x6b] = 0x00;
319 s
->status
= UHCI_STS_HCHALTED
;
325 for(i
= 0; i
< NB_PORTS
; i
++) {
328 if (port
->port
.dev
&& port
->port
.dev
->attached
) {
329 usb_port_reset(&port
->port
);
333 uhci_async_cancel_all(s
);
334 qemu_bh_cancel(s
->bh
);
338 static const VMStateDescription vmstate_uhci_port
= {
341 .minimum_version_id
= 1,
342 .fields
= (VMStateField
[]) {
343 VMSTATE_UINT16(ctrl
, UHCIPort
),
344 VMSTATE_END_OF_LIST()
348 static int uhci_post_load(void *opaque
, int version_id
)
350 UHCIState
*s
= opaque
;
352 if (version_id
< 2) {
353 s
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
354 (NANOSECONDS_PER_SECOND
/ FRAME_TIMER_FREQ
);
359 static const VMStateDescription vmstate_uhci
= {
362 .minimum_version_id
= 1,
363 .post_load
= uhci_post_load
,
364 .fields
= (VMStateField
[]) {
365 VMSTATE_PCI_DEVICE(dev
, UHCIState
),
366 VMSTATE_UINT8_EQUAL(num_ports_vmstate
, UHCIState
, NULL
),
367 VMSTATE_STRUCT_ARRAY(ports
, UHCIState
, NB_PORTS
, 1,
368 vmstate_uhci_port
, UHCIPort
),
369 VMSTATE_UINT16(cmd
, UHCIState
),
370 VMSTATE_UINT16(status
, UHCIState
),
371 VMSTATE_UINT16(intr
, UHCIState
),
372 VMSTATE_UINT16(frnum
, UHCIState
),
373 VMSTATE_UINT32(fl_base_addr
, UHCIState
),
374 VMSTATE_UINT8(sof_timing
, UHCIState
),
375 VMSTATE_UINT8(status2
, UHCIState
),
376 VMSTATE_TIMER_PTR(frame_timer
, UHCIState
),
377 VMSTATE_INT64_V(expire_time
, UHCIState
, 2),
378 VMSTATE_UINT32_V(pending_int_mask
, UHCIState
, 3),
379 VMSTATE_END_OF_LIST()
383 static void uhci_port_write(void *opaque
, hwaddr addr
,
384 uint64_t val
, unsigned size
)
386 UHCIState
*s
= opaque
;
388 trace_usb_uhci_mmio_writew(addr
, val
);
392 if ((val
& UHCI_CMD_RS
) && !(s
->cmd
& UHCI_CMD_RS
)) {
393 /* start frame processing */
394 trace_usb_uhci_schedule_start();
395 s
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
396 (NANOSECONDS_PER_SECOND
/ FRAME_TIMER_FREQ
);
397 timer_mod(s
->frame_timer
, s
->expire_time
);
398 s
->status
&= ~UHCI_STS_HCHALTED
;
399 } else if (!(val
& UHCI_CMD_RS
)) {
400 s
->status
|= UHCI_STS_HCHALTED
;
402 if (val
& UHCI_CMD_GRESET
) {
406 /* send reset on the USB bus */
407 for(i
= 0; i
< NB_PORTS
; i
++) {
409 usb_device_reset(port
->port
.dev
);
411 uhci_reset(DEVICE(s
));
414 if (val
& UHCI_CMD_HCRESET
) {
415 uhci_reset(DEVICE(s
));
419 if (val
& UHCI_CMD_EGSM
) {
420 if ((s
->ports
[0].ctrl
& UHCI_PORT_RD
) ||
421 (s
->ports
[1].ctrl
& UHCI_PORT_RD
)) {
428 /* XXX: the chip spec is not coherent, so we add a hidden
429 register to distinguish between IOC and SPD */
430 if (val
& UHCI_STS_USBINT
)
439 if (s
->status
& UHCI_STS_HCHALTED
)
440 s
->frnum
= val
& 0x7ff;
443 s
->fl_base_addr
&= 0xffff0000;
444 s
->fl_base_addr
|= val
& ~0xfff;
447 s
->fl_base_addr
&= 0x0000ffff;
448 s
->fl_base_addr
|= (val
<< 16);
451 s
->sof_timing
= val
& 0xff;
463 dev
= port
->port
.dev
;
464 if (dev
&& dev
->attached
) {
466 if ( (val
& UHCI_PORT_RESET
) &&
467 !(port
->ctrl
& UHCI_PORT_RESET
) ) {
468 usb_device_reset(dev
);
471 port
->ctrl
&= UHCI_PORT_READ_ONLY
;
472 /* enabled may only be set if a device is connected */
473 if (!(port
->ctrl
& UHCI_PORT_CCS
)) {
474 val
&= ~UHCI_PORT_EN
;
476 port
->ctrl
|= (val
& ~UHCI_PORT_READ_ONLY
);
477 /* some bits are reset when a '1' is written to them */
478 port
->ctrl
&= ~(val
& UHCI_PORT_WRITE_CLEAR
);
484 static uint64_t uhci_port_read(void *opaque
, hwaddr addr
, unsigned size
)
486 UHCIState
*s
= opaque
;
503 val
= s
->fl_base_addr
& 0xffff;
506 val
= (s
->fl_base_addr
>> 16) & 0xffff;
524 val
= 0xff7f; /* disabled port */
528 trace_usb_uhci_mmio_readw(addr
, val
);
533 /* signal resume if controller suspended */
534 static void uhci_resume (void *opaque
)
536 UHCIState
*s
= (UHCIState
*)opaque
;
541 if (s
->cmd
& UHCI_CMD_EGSM
) {
542 s
->cmd
|= UHCI_CMD_FGR
;
543 s
->status
|= UHCI_STS_RD
;
548 static void uhci_attach(USBPort
*port1
)
550 UHCIState
*s
= port1
->opaque
;
551 UHCIPort
*port
= &s
->ports
[port1
->index
];
553 /* set connect status */
554 port
->ctrl
|= UHCI_PORT_CCS
| UHCI_PORT_CSC
;
557 if (port
->port
.dev
->speed
== USB_SPEED_LOW
) {
558 port
->ctrl
|= UHCI_PORT_LSDA
;
560 port
->ctrl
&= ~UHCI_PORT_LSDA
;
566 static void uhci_detach(USBPort
*port1
)
568 UHCIState
*s
= port1
->opaque
;
569 UHCIPort
*port
= &s
->ports
[port1
->index
];
571 uhci_async_cancel_device(s
, port1
->dev
);
573 /* set connect status */
574 if (port
->ctrl
& UHCI_PORT_CCS
) {
575 port
->ctrl
&= ~UHCI_PORT_CCS
;
576 port
->ctrl
|= UHCI_PORT_CSC
;
579 if (port
->ctrl
& UHCI_PORT_EN
) {
580 port
->ctrl
&= ~UHCI_PORT_EN
;
581 port
->ctrl
|= UHCI_PORT_ENC
;
587 static void uhci_child_detach(USBPort
*port1
, USBDevice
*child
)
589 UHCIState
*s
= port1
->opaque
;
591 uhci_async_cancel_device(s
, child
);
594 static void uhci_wakeup(USBPort
*port1
)
596 UHCIState
*s
= port1
->opaque
;
597 UHCIPort
*port
= &s
->ports
[port1
->index
];
599 if (port
->ctrl
& UHCI_PORT_SUSPEND
&& !(port
->ctrl
& UHCI_PORT_RD
)) {
600 port
->ctrl
|= UHCI_PORT_RD
;
605 static USBDevice
*uhci_find_device(UHCIState
*s
, uint8_t addr
)
610 for (i
= 0; i
< NB_PORTS
; i
++) {
611 UHCIPort
*port
= &s
->ports
[i
];
612 if (!(port
->ctrl
& UHCI_PORT_EN
)) {
615 dev
= usb_find_device(&port
->port
, addr
);
623 static void uhci_read_td(UHCIState
*s
, UHCI_TD
*td
, uint32_t link
)
625 pci_dma_read(&s
->dev
, link
& ~0xf, td
, sizeof(*td
));
626 le32_to_cpus(&td
->link
);
627 le32_to_cpus(&td
->ctrl
);
628 le32_to_cpus(&td
->token
);
629 le32_to_cpus(&td
->buffer
);
632 static int uhci_handle_td_error(UHCIState
*s
, UHCI_TD
*td
, uint32_t td_addr
,
633 int status
, uint32_t *int_mask
)
635 uint32_t queue_token
= uhci_queue_token(td
);
640 td
->ctrl
|= TD_CTRL_NAK
;
641 return TD_RESULT_NEXT_QH
;
644 td
->ctrl
|= TD_CTRL_STALL
;
645 trace_usb_uhci_packet_complete_stall(queue_token
, td_addr
);
646 ret
= TD_RESULT_NEXT_QH
;
650 td
->ctrl
|= TD_CTRL_BABBLE
| TD_CTRL_STALL
;
651 /* frame interrupted */
652 trace_usb_uhci_packet_complete_babble(queue_token
, td_addr
);
653 ret
= TD_RESULT_STOP_FRAME
;
656 case USB_RET_IOERROR
:
659 td
->ctrl
|= TD_CTRL_TIMEOUT
;
660 td
->ctrl
&= ~(3 << TD_CTRL_ERROR_SHIFT
);
661 trace_usb_uhci_packet_complete_error(queue_token
, td_addr
);
662 ret
= TD_RESULT_NEXT_QH
;
666 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
667 s
->status
|= UHCI_STS_USBERR
;
668 if (td
->ctrl
& TD_CTRL_IOC
) {
675 static int uhci_complete_td(UHCIState
*s
, UHCI_TD
*td
, UHCIAsync
*async
, uint32_t *int_mask
)
677 int len
= 0, max_len
;
680 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
681 pid
= td
->token
& 0xff;
683 if (td
->ctrl
& TD_CTRL_IOS
)
684 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
686 if (async
->packet
.status
!= USB_RET_SUCCESS
) {
687 return uhci_handle_td_error(s
, td
, async
->td_addr
,
688 async
->packet
.status
, int_mask
);
691 len
= async
->packet
.actual_length
;
692 td
->ctrl
= (td
->ctrl
& ~0x7ff) | ((len
- 1) & 0x7ff);
694 /* The NAK bit may have been set by a previous frame, so clear it
695 here. The docs are somewhat unclear, but win2k relies on this
697 td
->ctrl
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_NAK
);
698 if (td
->ctrl
& TD_CTRL_IOC
)
701 if (pid
== USB_TOKEN_IN
) {
702 pci_dma_write(&s
->dev
, td
->buffer
, async
->buf
, len
);
703 if ((td
->ctrl
& TD_CTRL_SPD
) && len
< max_len
) {
705 /* short packet: do not update QH */
706 trace_usb_uhci_packet_complete_shortxfer(async
->queue
->token
,
708 return TD_RESULT_NEXT_QH
;
713 trace_usb_uhci_packet_complete_success(async
->queue
->token
,
715 return TD_RESULT_COMPLETE
;
718 static int uhci_handle_td(UHCIState
*s
, UHCIQueue
*q
, uint32_t qh_addr
,
719 UHCI_TD
*td
, uint32_t td_addr
, uint32_t *int_mask
)
723 bool queuing
= (q
!= NULL
);
724 uint8_t pid
= td
->token
& 0xff;
727 async
= uhci_async_find_td(s
, td_addr
);
729 if (uhci_queue_verify(async
->queue
, qh_addr
, td
, td_addr
, queuing
)) {
730 assert(q
== NULL
|| q
== async
->queue
);
733 uhci_queue_free(async
->queue
, "guest re-used pending td");
739 q
= uhci_queue_find(s
, td
);
740 if (q
&& !uhci_queue_verify(q
, qh_addr
, td
, td_addr
, queuing
)) {
741 uhci_queue_free(q
, "guest re-used qh");
751 if (!(td
->ctrl
& TD_CTRL_ACTIVE
)) {
753 /* Guest marked a pending td non-active, cancel the queue */
754 uhci_queue_free(async
->queue
, "pending td non-active");
757 * ehci11d spec page 22: "Even if the Active bit in the TD is already
758 * cleared when the TD is fetched ... an IOC interrupt is generated"
760 if (td
->ctrl
& TD_CTRL_IOC
) {
763 return TD_RESULT_NEXT_QH
;
768 case USB_TOKEN_SETUP
:
772 /* invalid pid : frame interrupted */
773 s
->status
|= UHCI_STS_HCPERR
;
774 s
->cmd
&= ~UHCI_CMD_RS
;
776 return TD_RESULT_STOP_FRAME
;
781 /* we are busy filling the queue, we are not prepared
782 to consume completed packages then, just leave them
784 return TD_RESULT_ASYNC_CONT
;
788 UHCIAsync
*last
= QTAILQ_LAST(&async
->queue
->asyncs
);
790 * While we are waiting for the current td to complete, the guest
791 * may have added more tds to the queue. Note we re-read the td
792 * rather then caching it, as we want to see guest made changes!
794 uhci_read_td(s
, &last_td
, last
->td_addr
);
795 uhci_queue_fill(async
->queue
, &last_td
);
797 return TD_RESULT_ASYNC_CONT
;
799 uhci_async_unlink(async
);
803 if (s
->completions_only
) {
804 return TD_RESULT_ASYNC_CONT
;
807 /* Allocate new packet */
812 dev
= uhci_find_device(s
, (td
->token
>> 8) & 0x7f);
814 return uhci_handle_td_error(s
, td
, td_addr
, USB_RET_NODEV
,
817 ep
= usb_ep_get(dev
, pid
, (td
->token
>> 15) & 0xf);
818 q
= uhci_queue_new(s
, qh_addr
, td
, ep
);
820 async
= uhci_async_alloc(q
, td_addr
);
822 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
823 spd
= (pid
== USB_TOKEN_IN
&& (td
->ctrl
& TD_CTRL_SPD
) != 0);
824 usb_packet_setup(&async
->packet
, pid
, q
->ep
, 0, td_addr
, spd
,
825 (td
->ctrl
& TD_CTRL_IOC
) != 0);
826 if (max_len
<= sizeof(async
->static_buf
)) {
827 async
->buf
= async
->static_buf
;
829 async
->buf
= g_malloc(max_len
);
831 usb_packet_addbuf(&async
->packet
, async
->buf
, max_len
);
835 case USB_TOKEN_SETUP
:
836 pci_dma_read(&s
->dev
, td
->buffer
, async
->buf
, max_len
);
837 usb_handle_packet(q
->ep
->dev
, &async
->packet
);
838 if (async
->packet
.status
== USB_RET_SUCCESS
) {
839 async
->packet
.actual_length
= max_len
;
844 usb_handle_packet(q
->ep
->dev
, &async
->packet
);
848 abort(); /* Never to execute */
851 if (async
->packet
.status
== USB_RET_ASYNC
) {
852 uhci_async_link(async
);
854 uhci_queue_fill(q
, td
);
856 return TD_RESULT_ASYNC_START
;
860 ret
= uhci_complete_td(s
, td
, async
, int_mask
);
861 uhci_async_free(async
);
865 static void uhci_async_complete(USBPort
*port
, USBPacket
*packet
)
867 UHCIAsync
*async
= container_of(packet
, UHCIAsync
, packet
);
868 UHCIState
*s
= async
->queue
->uhci
;
870 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
871 uhci_async_cancel(async
);
876 /* Force processing of this packet *now*, needed for migration */
877 s
->completions_only
= true;
878 qemu_bh_schedule(s
->bh
);
881 static int is_valid(uint32_t link
)
883 return (link
& 1) == 0;
886 static int is_qh(uint32_t link
)
888 return (link
& 2) != 0;
891 static int depth_first(uint32_t link
)
893 return (link
& 4) != 0;
896 /* QH DB used for detecting QH loops */
897 #define UHCI_MAX_QUEUES 128
899 uint32_t addr
[UHCI_MAX_QUEUES
];
903 static void qhdb_reset(QhDb
*db
)
908 /* Add QH to DB. Returns 1 if already present or DB is full. */
909 static int qhdb_insert(QhDb
*db
, uint32_t addr
)
912 for (i
= 0; i
< db
->count
; i
++)
913 if (db
->addr
[i
] == addr
)
916 if (db
->count
>= UHCI_MAX_QUEUES
)
919 db
->addr
[db
->count
++] = addr
;
923 static void uhci_queue_fill(UHCIQueue
*q
, UHCI_TD
*td
)
925 uint32_t int_mask
= 0;
926 uint32_t plink
= td
->link
;
930 while (is_valid(plink
)) {
931 uhci_read_td(q
->uhci
, &ptd
, plink
);
932 if (!(ptd
.ctrl
& TD_CTRL_ACTIVE
)) {
935 if (uhci_queue_token(&ptd
) != q
->token
) {
938 trace_usb_uhci_td_queue(plink
& ~0xf, ptd
.ctrl
, ptd
.token
);
939 ret
= uhci_handle_td(q
->uhci
, q
, q
->qh_addr
, &ptd
, plink
, &int_mask
);
940 if (ret
== TD_RESULT_ASYNC_CONT
) {
943 assert(ret
== TD_RESULT_ASYNC_START
);
944 assert(int_mask
== 0);
947 usb_device_flush_ep_queue(q
->ep
->dev
, q
->ep
);
950 static void uhci_process_frame(UHCIState
*s
)
952 uint32_t frame_addr
, link
, old_td_ctrl
, val
, int_mask
;
953 uint32_t curr_qh
, td_count
= 0;
959 frame_addr
= s
->fl_base_addr
+ ((s
->frnum
& 0x3ff) << 2);
961 pci_dma_read(&s
->dev
, frame_addr
, &link
, 4);
969 for (cnt
= FRAME_MAX_LOOPS
; is_valid(link
) && cnt
; cnt
--) {
970 if (!s
->completions_only
&& s
->frame_bytes
>= s
->frame_bandwidth
) {
971 /* We've reached the usb 1.1 bandwidth, which is
972 1280 bytes/frame, stop processing */
973 trace_usb_uhci_frame_stop_bandwidth();
978 trace_usb_uhci_qh_load(link
& ~0xf);
980 if (qhdb_insert(&qhdb
, link
)) {
982 * We're going in circles. Which is not a bug because
983 * HCD is allowed to do that as part of the BW management.
985 * Stop processing here if no transaction has been done
986 * since we've been here last time.
989 trace_usb_uhci_frame_loop_stop_idle();
992 trace_usb_uhci_frame_loop_continue();
995 qhdb_insert(&qhdb
, link
);
999 pci_dma_read(&s
->dev
, link
& ~0xf, &qh
, sizeof(qh
));
1000 le32_to_cpus(&qh
.link
);
1001 le32_to_cpus(&qh
.el_link
);
1003 if (!is_valid(qh
.el_link
)) {
1004 /* QH w/o elements */
1008 /* QH with elements */
1016 uhci_read_td(s
, &td
, link
);
1017 trace_usb_uhci_td_load(curr_qh
& ~0xf, link
& ~0xf, td
.ctrl
, td
.token
);
1019 old_td_ctrl
= td
.ctrl
;
1020 ret
= uhci_handle_td(s
, NULL
, curr_qh
, &td
, link
, &int_mask
);
1021 if (old_td_ctrl
!= td
.ctrl
) {
1022 /* update the status bits of the TD */
1023 val
= cpu_to_le32(td
.ctrl
);
1024 pci_dma_write(&s
->dev
, (link
& ~0xf) + 4, &val
, sizeof(val
));
1028 case TD_RESULT_STOP_FRAME
: /* interrupted frame */
1031 case TD_RESULT_NEXT_QH
:
1032 case TD_RESULT_ASYNC_CONT
:
1033 trace_usb_uhci_td_nextqh(curr_qh
& ~0xf, link
& ~0xf);
1034 link
= curr_qh
? qh
.link
: td
.link
;
1037 case TD_RESULT_ASYNC_START
:
1038 trace_usb_uhci_td_async(curr_qh
& ~0xf, link
& ~0xf);
1039 link
= curr_qh
? qh
.link
: td
.link
;
1042 case TD_RESULT_COMPLETE
:
1043 trace_usb_uhci_td_complete(curr_qh
& ~0xf, link
& ~0xf);
1046 s
->frame_bytes
+= (td
.ctrl
& 0x7ff) + 1;
1049 /* update QH element link */
1051 val
= cpu_to_le32(qh
.el_link
);
1052 pci_dma_write(&s
->dev
, (curr_qh
& ~0xf) + 4, &val
, sizeof(val
));
1054 if (!depth_first(link
)) {
1055 /* done with this QH */
1063 assert(!"unknown return code");
1066 /* go to the next entry */
1070 s
->pending_int_mask
|= int_mask
;
1073 static void uhci_bh(void *opaque
)
1075 UHCIState
*s
= opaque
;
1076 uhci_process_frame(s
);
1079 static void uhci_frame_timer(void *opaque
)
1081 UHCIState
*s
= opaque
;
1082 uint64_t t_now
, t_last_run
;
1084 const uint64_t frame_t
= NANOSECONDS_PER_SECOND
/ FRAME_TIMER_FREQ
;
1086 s
->completions_only
= false;
1087 qemu_bh_cancel(s
->bh
);
1089 if (!(s
->cmd
& UHCI_CMD_RS
)) {
1091 trace_usb_uhci_schedule_stop();
1092 timer_del(s
->frame_timer
);
1093 uhci_async_cancel_all(s
);
1094 /* set hchalted bit in status - UHCI11D 2.1.2 */
1095 s
->status
|= UHCI_STS_HCHALTED
;
1099 /* We still store expire_time in our state, for migration */
1100 t_last_run
= s
->expire_time
- frame_t
;
1101 t_now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1103 /* Process up to MAX_FRAMES_PER_TICK frames */
1104 frames
= (t_now
- t_last_run
) / frame_t
;
1105 if (frames
> s
->maxframes
) {
1106 int skipped
= frames
- s
->maxframes
;
1107 s
->expire_time
+= skipped
* frame_t
;
1108 s
->frnum
= (s
->frnum
+ skipped
) & 0x7ff;
1111 if (frames
> MAX_FRAMES_PER_TICK
) {
1112 frames
= MAX_FRAMES_PER_TICK
;
1115 for (i
= 0; i
< frames
; i
++) {
1117 trace_usb_uhci_frame_start(s
->frnum
);
1118 uhci_async_validate_begin(s
);
1119 uhci_process_frame(s
);
1120 uhci_async_validate_end(s
);
1121 /* The spec says frnum is the frame currently being processed, and
1122 * the guest must look at frnum - 1 on interrupt, so inc frnum now */
1123 s
->frnum
= (s
->frnum
+ 1) & 0x7ff;
1124 s
->expire_time
+= frame_t
;
1127 /* Complete the previous frame(s) */
1128 if (s
->pending_int_mask
) {
1129 s
->status2
|= s
->pending_int_mask
;
1130 s
->status
|= UHCI_STS_USBINT
;
1133 s
->pending_int_mask
= 0;
1135 timer_mod(s
->frame_timer
, t_now
+ frame_t
);
1138 static const MemoryRegionOps uhci_ioport_ops
= {
1139 .read
= uhci_port_read
,
1140 .write
= uhci_port_write
,
1141 .valid
.min_access_size
= 1,
1142 .valid
.max_access_size
= 4,
1143 .impl
.min_access_size
= 2,
1144 .impl
.max_access_size
= 2,
1145 .endianness
= DEVICE_LITTLE_ENDIAN
,
1148 static USBPortOps uhci_port_ops
= {
1149 .attach
= uhci_attach
,
1150 .detach
= uhci_detach
,
1151 .child_detach
= uhci_child_detach
,
1152 .wakeup
= uhci_wakeup
,
1153 .complete
= uhci_async_complete
,
1156 static USBBusOps uhci_bus_ops
= {
1159 void usb_uhci_common_realize(PCIDevice
*dev
, Error
**errp
)
1162 UHCIPCIDeviceClass
*u
= UHCI_GET_CLASS(dev
);
1163 UHCIState
*s
= UHCI(dev
);
1164 uint8_t *pci_conf
= s
->dev
.config
;
1167 pci_conf
[PCI_CLASS_PROG
] = 0x00;
1168 /* TODO: reset value should be 0. */
1169 pci_conf
[USB_SBRN
] = USB_RELEASE_1
; /* release number */
1170 pci_config_set_interrupt_pin(pci_conf
, u
->info
.irq_pin
+ 1);
1171 s
->irq
= pci_allocate_irq(dev
);
1174 USBPort
*ports
[NB_PORTS
];
1175 for(i
= 0; i
< NB_PORTS
; i
++) {
1176 ports
[i
] = &s
->ports
[i
].port
;
1178 usb_register_companion(s
->masterbus
, ports
, NB_PORTS
,
1179 s
->firstport
, s
, &uhci_port_ops
,
1180 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
,
1183 error_propagate(errp
, err
);
1187 usb_bus_new(&s
->bus
, sizeof(s
->bus
), &uhci_bus_ops
, DEVICE(dev
));
1188 for (i
= 0; i
< NB_PORTS
; i
++) {
1189 usb_register_port(&s
->bus
, &s
->ports
[i
].port
, s
, i
, &uhci_port_ops
,
1190 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
1193 s
->bh
= qemu_bh_new_guarded(uhci_bh
, s
, &DEVICE(dev
)->mem_reentrancy_guard
);
1194 s
->frame_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, uhci_frame_timer
, s
);
1195 s
->num_ports_vmstate
= NB_PORTS
;
1196 QTAILQ_INIT(&s
->queues
);
1198 memory_region_init_io(&s
->io_bar
, OBJECT(s
), &uhci_ioport_ops
, s
,
1201 /* Use region 4 for consistency with real hardware. BSD guests seem
1203 pci_register_bar(&s
->dev
, 4, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_bar
);
1206 static void usb_uhci_exit(PCIDevice
*dev
)
1208 UHCIState
*s
= UHCI(dev
);
1210 trace_usb_uhci_exit();
1212 if (s
->frame_timer
) {
1213 timer_free(s
->frame_timer
);
1214 s
->frame_timer
= NULL
;
1218 qemu_bh_delete(s
->bh
);
1221 uhci_async_cancel_all(s
);
1223 if (!s
->masterbus
) {
1224 usb_bus_release(&s
->bus
);
1228 static Property uhci_properties_companion
[] = {
1229 DEFINE_PROP_STRING("masterbus", UHCIState
, masterbus
),
1230 DEFINE_PROP_UINT32("firstport", UHCIState
, firstport
, 0),
1231 DEFINE_PROP_UINT32("bandwidth", UHCIState
, frame_bandwidth
, 1280),
1232 DEFINE_PROP_UINT32("maxframes", UHCIState
, maxframes
, 128),
1233 DEFINE_PROP_END_OF_LIST(),
1235 static Property uhci_properties_standalone
[] = {
1236 DEFINE_PROP_UINT32("bandwidth", UHCIState
, frame_bandwidth
, 1280),
1237 DEFINE_PROP_UINT32("maxframes", UHCIState
, maxframes
, 128),
1238 DEFINE_PROP_END_OF_LIST(),
1241 static void uhci_class_init(ObjectClass
*klass
, void *data
)
1243 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1244 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1246 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1247 dc
->vmsd
= &vmstate_uhci
;
1248 dc
->reset
= uhci_reset
;
1249 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
1252 static const TypeInfo uhci_pci_type_info
= {
1254 .parent
= TYPE_PCI_DEVICE
,
1255 .instance_size
= sizeof(UHCIState
),
1256 .class_size
= sizeof(UHCIPCIDeviceClass
),
1258 .class_init
= uhci_class_init
,
1259 .interfaces
= (InterfaceInfo
[]) {
1260 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1265 void uhci_data_class_init(ObjectClass
*klass
, void *data
)
1267 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1268 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1269 UHCIPCIDeviceClass
*u
= UHCI_CLASS(klass
);
1270 UHCIInfo
*info
= data
;
1272 k
->realize
= info
->realize
? info
->realize
: usb_uhci_common_realize
;
1273 k
->exit
= info
->unplug
? usb_uhci_exit
: NULL
;
1274 k
->vendor_id
= info
->vendor_id
;
1275 k
->device_id
= info
->device_id
;
1276 k
->revision
= info
->revision
;
1277 if (!info
->unplug
) {
1278 /* uhci controllers in companion setups can't be hotplugged */
1279 dc
->hotpluggable
= false;
1280 device_class_set_props(dc
, uhci_properties_companion
);
1282 device_class_set_props(dc
, uhci_properties_standalone
);
1284 if (info
->notuser
) {
1285 dc
->user_creatable
= false;
1290 static UHCIInfo uhci_info
[] = {
1292 .name
= TYPE_PIIX3_USB_UHCI
,
1293 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1294 .device_id
= PCI_DEVICE_ID_INTEL_82371SB_2
,
1299 .name
= TYPE_PIIX4_USB_UHCI
,
1300 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1301 .device_id
= PCI_DEVICE_ID_INTEL_82371AB_2
,
1306 .name
= TYPE_ICH9_USB_UHCI(1), /* 00:1d.0 */
1307 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1308 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI1
,
1313 .name
= TYPE_ICH9_USB_UHCI(2), /* 00:1d.1 */
1314 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1315 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI2
,
1320 .name
= TYPE_ICH9_USB_UHCI(3), /* 00:1d.2 */
1321 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1322 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI3
,
1327 .name
= TYPE_ICH9_USB_UHCI(4), /* 00:1a.0 */
1328 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1329 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI4
,
1334 .name
= TYPE_ICH9_USB_UHCI(5), /* 00:1a.1 */
1335 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1336 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI5
,
1341 .name
= TYPE_ICH9_USB_UHCI(6), /* 00:1a.2 */
1342 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1343 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI6
,
1350 static void uhci_register_types(void)
1352 TypeInfo uhci_type_info
= {
1353 .parent
= TYPE_UHCI
,
1354 .class_init
= uhci_data_class_init
,
1358 type_register_static(&uhci_pci_type_info
);
1360 for (i
= 0; i
< ARRAY_SIZE(uhci_info
); i
++) {
1361 uhci_type_info
.name
= uhci_info
[i
].name
;
1362 uhci_type_info
.class_data
= uhci_info
+ i
;
1363 type_register(&uhci_type_info
);
1367 type_init(uhci_register_types
)