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
28 #include "qemu/osdep.h"
31 #include "hw/usb/uhci-regs.h"
32 #include "hw/pci/pci.h"
33 #include "qemu/timer.h"
35 #include "sysemu/dma.h"
37 #include "qemu/main-loop.h"
39 #define FRAME_TIMER_FREQ 1000
41 #define FRAME_MAX_LOOPS 256
43 /* Must be large enough to handle 10 frame delay for initial isoc requests */
46 #define MAX_FRAMES_PER_TICK (QH_VALID / 2)
51 TD_RESULT_STOP_FRAME
= 10,
54 TD_RESULT_ASYNC_START
,
58 typedef struct UHCIState UHCIState
;
59 typedef struct UHCIAsync UHCIAsync
;
60 typedef struct UHCIQueue UHCIQueue
;
61 typedef struct UHCIInfo UHCIInfo
;
62 typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass
;
70 void (*realize
)(PCIDevice
*dev
, Error
**errp
);
74 struct UHCIPCIDeviceClass
{
75 PCIDeviceClass parent_class
;
80 * Pending async transaction.
81 * 'packet' must be the first field because completion
82 * handler does "(UHCIAsync *) pkt" cast.
87 uint8_t static_buf
[64]; /* 64 bytes is enough, except for isoc packets */
90 QTAILQ_ENTRY(UHCIAsync
) next
;
100 QTAILQ_ENTRY(UHCIQueue
) next
;
101 QTAILQ_HEAD(asyncs_head
, UHCIAsync
) asyncs
;
105 typedef struct UHCIPort
{
113 USBBus bus
; /* Note unused when we're a companion controller */
114 uint16_t cmd
; /* cmd register */
116 uint16_t intr
; /* interrupt enable register */
117 uint16_t frnum
; /* frame number */
118 uint32_t fl_base_addr
; /* frame list base address */
120 uint8_t status2
; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
122 QEMUTimer
*frame_timer
;
124 uint32_t frame_bytes
;
125 uint32_t frame_bandwidth
;
126 bool completions_only
;
127 UHCIPort ports
[NB_PORTS
];
129 /* Interrupts that should be raised at the end of the current frame. */
130 uint32_t pending_int_mask
;
133 QTAILQ_HEAD(, UHCIQueue
) queues
;
134 uint8_t num_ports_vmstate
;
142 typedef struct UHCI_TD
{
144 uint32_t ctrl
; /* see TD_CTRL_xxx */
149 typedef struct UHCI_QH
{
154 static void uhci_async_cancel(UHCIAsync
*async
);
155 static void uhci_queue_fill(UHCIQueue
*q
, UHCI_TD
*td
);
156 static void uhci_resume(void *opaque
);
158 #define TYPE_UHCI "pci-uhci-usb"
159 #define UHCI(obj) OBJECT_CHECK(UHCIState, (obj), TYPE_UHCI)
161 static inline int32_t uhci_queue_token(UHCI_TD
*td
)
163 if ((td
->token
& (0xf << 15)) == 0) {
164 /* ctrl ep, cover ep and dev, not pid! */
165 return td
->token
& 0x7ff00;
167 /* covers ep, dev, pid -> identifies the endpoint */
168 return td
->token
& 0x7ffff;
172 static UHCIQueue
*uhci_queue_new(UHCIState
*s
, uint32_t qh_addr
, UHCI_TD
*td
,
177 queue
= g_new0(UHCIQueue
, 1);
179 queue
->qh_addr
= qh_addr
;
180 queue
->token
= uhci_queue_token(td
);
182 QTAILQ_INIT(&queue
->asyncs
);
183 QTAILQ_INSERT_HEAD(&s
->queues
, queue
, next
);
184 queue
->valid
= QH_VALID
;
185 trace_usb_uhci_queue_add(queue
->token
);
189 static void uhci_queue_free(UHCIQueue
*queue
, const char *reason
)
191 UHCIState
*s
= queue
->uhci
;
194 while (!QTAILQ_EMPTY(&queue
->asyncs
)) {
195 async
= QTAILQ_FIRST(&queue
->asyncs
);
196 uhci_async_cancel(async
);
198 usb_device_ep_stopped(queue
->ep
->dev
, queue
->ep
);
200 trace_usb_uhci_queue_del(queue
->token
, reason
);
201 QTAILQ_REMOVE(&s
->queues
, queue
, next
);
205 static UHCIQueue
*uhci_queue_find(UHCIState
*s
, UHCI_TD
*td
)
207 uint32_t token
= uhci_queue_token(td
);
210 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
211 if (queue
->token
== token
) {
218 static bool uhci_queue_verify(UHCIQueue
*queue
, uint32_t qh_addr
, UHCI_TD
*td
,
219 uint32_t td_addr
, bool queuing
)
221 UHCIAsync
*first
= QTAILQ_FIRST(&queue
->asyncs
);
222 uint32_t queue_token_addr
= (queue
->token
>> 8) & 0x7f;
224 return queue
->qh_addr
== qh_addr
&&
225 queue
->token
== uhci_queue_token(td
) &&
226 queue_token_addr
== queue
->ep
->dev
->addr
&&
227 (queuing
|| !(td
->ctrl
& TD_CTRL_ACTIVE
) || first
== NULL
||
228 first
->td_addr
== td_addr
);
231 static UHCIAsync
*uhci_async_alloc(UHCIQueue
*queue
, uint32_t td_addr
)
233 UHCIAsync
*async
= g_new0(UHCIAsync
, 1);
235 async
->queue
= queue
;
236 async
->td_addr
= td_addr
;
237 usb_packet_init(&async
->packet
);
238 trace_usb_uhci_packet_add(async
->queue
->token
, async
->td_addr
);
243 static void uhci_async_free(UHCIAsync
*async
)
245 trace_usb_uhci_packet_del(async
->queue
->token
, async
->td_addr
);
246 usb_packet_cleanup(&async
->packet
);
247 if (async
->buf
!= async
->static_buf
) {
253 static void uhci_async_link(UHCIAsync
*async
)
255 UHCIQueue
*queue
= async
->queue
;
256 QTAILQ_INSERT_TAIL(&queue
->asyncs
, async
, next
);
257 trace_usb_uhci_packet_link_async(async
->queue
->token
, async
->td_addr
);
260 static void uhci_async_unlink(UHCIAsync
*async
)
262 UHCIQueue
*queue
= async
->queue
;
263 QTAILQ_REMOVE(&queue
->asyncs
, async
, next
);
264 trace_usb_uhci_packet_unlink_async(async
->queue
->token
, async
->td_addr
);
267 static void uhci_async_cancel(UHCIAsync
*async
)
269 uhci_async_unlink(async
);
270 trace_usb_uhci_packet_cancel(async
->queue
->token
, async
->td_addr
,
273 usb_cancel_packet(&async
->packet
);
274 uhci_async_free(async
);
278 * Mark all outstanding async packets as invalid.
279 * This is used for canceling them when TDs are removed by the HCD.
281 static void uhci_async_validate_begin(UHCIState
*s
)
285 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
291 * Cancel async packets that are no longer valid
293 static void uhci_async_validate_end(UHCIState
*s
)
295 UHCIQueue
*queue
, *n
;
297 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, n
) {
299 uhci_queue_free(queue
, "validate-end");
304 static void uhci_async_cancel_device(UHCIState
*s
, USBDevice
*dev
)
306 UHCIQueue
*queue
, *n
;
308 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, n
) {
309 if (queue
->ep
->dev
== dev
) {
310 uhci_queue_free(queue
, "cancel-device");
315 static void uhci_async_cancel_all(UHCIState
*s
)
317 UHCIQueue
*queue
, *nq
;
319 QTAILQ_FOREACH_SAFE(queue
, &s
->queues
, next
, nq
) {
320 uhci_queue_free(queue
, "cancel-all");
324 static UHCIAsync
*uhci_async_find_td(UHCIState
*s
, uint32_t td_addr
)
329 QTAILQ_FOREACH(queue
, &s
->queues
, next
) {
330 QTAILQ_FOREACH(async
, &queue
->asyncs
, next
) {
331 if (async
->td_addr
== td_addr
) {
339 static void uhci_update_irq(UHCIState
*s
)
342 if (((s
->status2
& 1) && (s
->intr
& (1 << 2))) ||
343 ((s
->status2
& 2) && (s
->intr
& (1 << 3))) ||
344 ((s
->status
& UHCI_STS_USBERR
) && (s
->intr
& (1 << 0))) ||
345 ((s
->status
& UHCI_STS_RD
) && (s
->intr
& (1 << 1))) ||
346 (s
->status
& UHCI_STS_HSERR
) ||
347 (s
->status
& UHCI_STS_HCPERR
)) {
352 pci_set_irq(&s
->dev
, level
);
355 static void uhci_reset(DeviceState
*dev
)
357 PCIDevice
*d
= PCI_DEVICE(dev
);
358 UHCIState
*s
= UHCI(d
);
363 trace_usb_uhci_reset();
365 pci_conf
= s
->dev
.config
;
367 pci_conf
[0x6a] = 0x01; /* usb clock */
368 pci_conf
[0x6b] = 0x00;
370 s
->status
= UHCI_STS_HCHALTED
;
376 for(i
= 0; i
< NB_PORTS
; i
++) {
379 if (port
->port
.dev
&& port
->port
.dev
->attached
) {
380 usb_port_reset(&port
->port
);
384 uhci_async_cancel_all(s
);
385 qemu_bh_cancel(s
->bh
);
389 static const VMStateDescription vmstate_uhci_port
= {
392 .minimum_version_id
= 1,
393 .fields
= (VMStateField
[]) {
394 VMSTATE_UINT16(ctrl
, UHCIPort
),
395 VMSTATE_END_OF_LIST()
399 static int uhci_post_load(void *opaque
, int version_id
)
401 UHCIState
*s
= opaque
;
403 if (version_id
< 2) {
404 s
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
405 (get_ticks_per_sec() / FRAME_TIMER_FREQ
);
410 static const VMStateDescription vmstate_uhci
= {
413 .minimum_version_id
= 1,
414 .post_load
= uhci_post_load
,
415 .fields
= (VMStateField
[]) {
416 VMSTATE_PCI_DEVICE(dev
, UHCIState
),
417 VMSTATE_UINT8_EQUAL(num_ports_vmstate
, UHCIState
),
418 VMSTATE_STRUCT_ARRAY(ports
, UHCIState
, NB_PORTS
, 1,
419 vmstate_uhci_port
, UHCIPort
),
420 VMSTATE_UINT16(cmd
, UHCIState
),
421 VMSTATE_UINT16(status
, UHCIState
),
422 VMSTATE_UINT16(intr
, UHCIState
),
423 VMSTATE_UINT16(frnum
, UHCIState
),
424 VMSTATE_UINT32(fl_base_addr
, UHCIState
),
425 VMSTATE_UINT8(sof_timing
, UHCIState
),
426 VMSTATE_UINT8(status2
, UHCIState
),
427 VMSTATE_TIMER_PTR(frame_timer
, UHCIState
),
428 VMSTATE_INT64_V(expire_time
, UHCIState
, 2),
429 VMSTATE_UINT32_V(pending_int_mask
, UHCIState
, 3),
430 VMSTATE_END_OF_LIST()
434 static void uhci_port_write(void *opaque
, hwaddr addr
,
435 uint64_t val
, unsigned size
)
437 UHCIState
*s
= opaque
;
439 trace_usb_uhci_mmio_writew(addr
, val
);
443 if ((val
& UHCI_CMD_RS
) && !(s
->cmd
& UHCI_CMD_RS
)) {
444 /* start frame processing */
445 trace_usb_uhci_schedule_start();
446 s
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
447 (get_ticks_per_sec() / FRAME_TIMER_FREQ
);
448 timer_mod(s
->frame_timer
, s
->expire_time
);
449 s
->status
&= ~UHCI_STS_HCHALTED
;
450 } else if (!(val
& UHCI_CMD_RS
)) {
451 s
->status
|= UHCI_STS_HCHALTED
;
453 if (val
& UHCI_CMD_GRESET
) {
457 /* send reset on the USB bus */
458 for(i
= 0; i
< NB_PORTS
; i
++) {
460 usb_device_reset(port
->port
.dev
);
462 uhci_reset(DEVICE(s
));
465 if (val
& UHCI_CMD_HCRESET
) {
466 uhci_reset(DEVICE(s
));
470 if (val
& UHCI_CMD_EGSM
) {
471 if ((s
->ports
[0].ctrl
& UHCI_PORT_RD
) ||
472 (s
->ports
[1].ctrl
& UHCI_PORT_RD
)) {
479 /* XXX: the chip spec is not coherent, so we add a hidden
480 register to distinguish between IOC and SPD */
481 if (val
& UHCI_STS_USBINT
)
490 if (s
->status
& UHCI_STS_HCHALTED
)
491 s
->frnum
= val
& 0x7ff;
494 s
->fl_base_addr
&= 0xffff0000;
495 s
->fl_base_addr
|= val
& ~0xfff;
498 s
->fl_base_addr
&= 0x0000ffff;
499 s
->fl_base_addr
|= (val
<< 16);
502 s
->sof_timing
= val
& 0xff;
514 dev
= port
->port
.dev
;
515 if (dev
&& dev
->attached
) {
517 if ( (val
& UHCI_PORT_RESET
) &&
518 !(port
->ctrl
& UHCI_PORT_RESET
) ) {
519 usb_device_reset(dev
);
522 port
->ctrl
&= UHCI_PORT_READ_ONLY
;
523 /* enabled may only be set if a device is connected */
524 if (!(port
->ctrl
& UHCI_PORT_CCS
)) {
525 val
&= ~UHCI_PORT_EN
;
527 port
->ctrl
|= (val
& ~UHCI_PORT_READ_ONLY
);
528 /* some bits are reset when a '1' is written to them */
529 port
->ctrl
&= ~(val
& UHCI_PORT_WRITE_CLEAR
);
535 static uint64_t uhci_port_read(void *opaque
, hwaddr addr
, unsigned size
)
537 UHCIState
*s
= opaque
;
554 val
= s
->fl_base_addr
& 0xffff;
557 val
= (s
->fl_base_addr
>> 16) & 0xffff;
575 val
= 0xff7f; /* disabled port */
579 trace_usb_uhci_mmio_readw(addr
, val
);
584 /* signal resume if controller suspended */
585 static void uhci_resume (void *opaque
)
587 UHCIState
*s
= (UHCIState
*)opaque
;
592 if (s
->cmd
& UHCI_CMD_EGSM
) {
593 s
->cmd
|= UHCI_CMD_FGR
;
594 s
->status
|= UHCI_STS_RD
;
599 static void uhci_attach(USBPort
*port1
)
601 UHCIState
*s
= port1
->opaque
;
602 UHCIPort
*port
= &s
->ports
[port1
->index
];
604 /* set connect status */
605 port
->ctrl
|= UHCI_PORT_CCS
| UHCI_PORT_CSC
;
608 if (port
->port
.dev
->speed
== USB_SPEED_LOW
) {
609 port
->ctrl
|= UHCI_PORT_LSDA
;
611 port
->ctrl
&= ~UHCI_PORT_LSDA
;
617 static void uhci_detach(USBPort
*port1
)
619 UHCIState
*s
= port1
->opaque
;
620 UHCIPort
*port
= &s
->ports
[port1
->index
];
622 uhci_async_cancel_device(s
, port1
->dev
);
624 /* set connect status */
625 if (port
->ctrl
& UHCI_PORT_CCS
) {
626 port
->ctrl
&= ~UHCI_PORT_CCS
;
627 port
->ctrl
|= UHCI_PORT_CSC
;
630 if (port
->ctrl
& UHCI_PORT_EN
) {
631 port
->ctrl
&= ~UHCI_PORT_EN
;
632 port
->ctrl
|= UHCI_PORT_ENC
;
638 static void uhci_child_detach(USBPort
*port1
, USBDevice
*child
)
640 UHCIState
*s
= port1
->opaque
;
642 uhci_async_cancel_device(s
, child
);
645 static void uhci_wakeup(USBPort
*port1
)
647 UHCIState
*s
= port1
->opaque
;
648 UHCIPort
*port
= &s
->ports
[port1
->index
];
650 if (port
->ctrl
& UHCI_PORT_SUSPEND
&& !(port
->ctrl
& UHCI_PORT_RD
)) {
651 port
->ctrl
|= UHCI_PORT_RD
;
656 static USBDevice
*uhci_find_device(UHCIState
*s
, uint8_t addr
)
661 for (i
= 0; i
< NB_PORTS
; i
++) {
662 UHCIPort
*port
= &s
->ports
[i
];
663 if (!(port
->ctrl
& UHCI_PORT_EN
)) {
666 dev
= usb_find_device(&port
->port
, addr
);
674 static void uhci_read_td(UHCIState
*s
, UHCI_TD
*td
, uint32_t link
)
676 pci_dma_read(&s
->dev
, link
& ~0xf, td
, sizeof(*td
));
677 le32_to_cpus(&td
->link
);
678 le32_to_cpus(&td
->ctrl
);
679 le32_to_cpus(&td
->token
);
680 le32_to_cpus(&td
->buffer
);
683 static int uhci_handle_td_error(UHCIState
*s
, UHCI_TD
*td
, uint32_t td_addr
,
684 int status
, uint32_t *int_mask
)
686 uint32_t queue_token
= uhci_queue_token(td
);
691 td
->ctrl
|= TD_CTRL_NAK
;
692 return TD_RESULT_NEXT_QH
;
695 td
->ctrl
|= TD_CTRL_STALL
;
696 trace_usb_uhci_packet_complete_stall(queue_token
, td_addr
);
697 ret
= TD_RESULT_NEXT_QH
;
701 td
->ctrl
|= TD_CTRL_BABBLE
| TD_CTRL_STALL
;
702 /* frame interrupted */
703 trace_usb_uhci_packet_complete_babble(queue_token
, td_addr
);
704 ret
= TD_RESULT_STOP_FRAME
;
707 case USB_RET_IOERROR
:
710 td
->ctrl
|= TD_CTRL_TIMEOUT
;
711 td
->ctrl
&= ~(3 << TD_CTRL_ERROR_SHIFT
);
712 trace_usb_uhci_packet_complete_error(queue_token
, td_addr
);
713 ret
= TD_RESULT_NEXT_QH
;
717 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
718 s
->status
|= UHCI_STS_USBERR
;
719 if (td
->ctrl
& TD_CTRL_IOC
) {
726 static int uhci_complete_td(UHCIState
*s
, UHCI_TD
*td
, UHCIAsync
*async
, uint32_t *int_mask
)
728 int len
= 0, max_len
;
731 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
732 pid
= td
->token
& 0xff;
734 if (td
->ctrl
& TD_CTRL_IOS
)
735 td
->ctrl
&= ~TD_CTRL_ACTIVE
;
737 if (async
->packet
.status
!= USB_RET_SUCCESS
) {
738 return uhci_handle_td_error(s
, td
, async
->td_addr
,
739 async
->packet
.status
, int_mask
);
742 len
= async
->packet
.actual_length
;
743 td
->ctrl
= (td
->ctrl
& ~0x7ff) | ((len
- 1) & 0x7ff);
745 /* The NAK bit may have been set by a previous frame, so clear it
746 here. The docs are somewhat unclear, but win2k relies on this
748 td
->ctrl
&= ~(TD_CTRL_ACTIVE
| TD_CTRL_NAK
);
749 if (td
->ctrl
& TD_CTRL_IOC
)
752 if (pid
== USB_TOKEN_IN
) {
753 pci_dma_write(&s
->dev
, td
->buffer
, async
->buf
, len
);
754 if ((td
->ctrl
& TD_CTRL_SPD
) && len
< max_len
) {
756 /* short packet: do not update QH */
757 trace_usb_uhci_packet_complete_shortxfer(async
->queue
->token
,
759 return TD_RESULT_NEXT_QH
;
764 trace_usb_uhci_packet_complete_success(async
->queue
->token
,
766 return TD_RESULT_COMPLETE
;
769 static int uhci_handle_td(UHCIState
*s
, UHCIQueue
*q
, uint32_t qh_addr
,
770 UHCI_TD
*td
, uint32_t td_addr
, uint32_t *int_mask
)
774 bool queuing
= (q
!= NULL
);
775 uint8_t pid
= td
->token
& 0xff;
776 UHCIAsync
*async
= uhci_async_find_td(s
, td_addr
);
779 if (uhci_queue_verify(async
->queue
, qh_addr
, td
, td_addr
, queuing
)) {
780 assert(q
== NULL
|| q
== async
->queue
);
783 uhci_queue_free(async
->queue
, "guest re-used pending td");
789 q
= uhci_queue_find(s
, td
);
790 if (q
&& !uhci_queue_verify(q
, qh_addr
, td
, td_addr
, queuing
)) {
791 uhci_queue_free(q
, "guest re-used qh");
801 if (!(td
->ctrl
& TD_CTRL_ACTIVE
)) {
803 /* Guest marked a pending td non-active, cancel the queue */
804 uhci_queue_free(async
->queue
, "pending td non-active");
807 * ehci11d spec page 22: "Even if the Active bit in the TD is already
808 * cleared when the TD is fetched ... an IOC interrupt is generated"
810 if (td
->ctrl
& TD_CTRL_IOC
) {
813 return TD_RESULT_NEXT_QH
;
818 /* we are busy filling the queue, we are not prepared
819 to consume completed packages then, just leave them
821 return TD_RESULT_ASYNC_CONT
;
825 UHCIAsync
*last
= QTAILQ_LAST(&async
->queue
->asyncs
, asyncs_head
);
827 * While we are waiting for the current td to complete, the guest
828 * may have added more tds to the queue. Note we re-read the td
829 * rather then caching it, as we want to see guest made changes!
831 uhci_read_td(s
, &last_td
, last
->td_addr
);
832 uhci_queue_fill(async
->queue
, &last_td
);
834 return TD_RESULT_ASYNC_CONT
;
836 uhci_async_unlink(async
);
840 if (s
->completions_only
) {
841 return TD_RESULT_ASYNC_CONT
;
844 /* Allocate new packet */
846 USBDevice
*dev
= uhci_find_device(s
, (td
->token
>> 8) & 0x7f);
847 USBEndpoint
*ep
= usb_ep_get(dev
, pid
, (td
->token
>> 15) & 0xf);
850 return uhci_handle_td_error(s
, td
, td_addr
, USB_RET_NODEV
,
853 q
= uhci_queue_new(s
, qh_addr
, td
, ep
);
855 async
= uhci_async_alloc(q
, td_addr
);
857 max_len
= ((td
->token
>> 21) + 1) & 0x7ff;
858 spd
= (pid
== USB_TOKEN_IN
&& (td
->ctrl
& TD_CTRL_SPD
) != 0);
859 usb_packet_setup(&async
->packet
, pid
, q
->ep
, 0, td_addr
, spd
,
860 (td
->ctrl
& TD_CTRL_IOC
) != 0);
861 if (max_len
<= sizeof(async
->static_buf
)) {
862 async
->buf
= async
->static_buf
;
864 async
->buf
= g_malloc(max_len
);
866 usb_packet_addbuf(&async
->packet
, async
->buf
, max_len
);
870 case USB_TOKEN_SETUP
:
871 pci_dma_read(&s
->dev
, td
->buffer
, async
->buf
, max_len
);
872 usb_handle_packet(q
->ep
->dev
, &async
->packet
);
873 if (async
->packet
.status
== USB_RET_SUCCESS
) {
874 async
->packet
.actual_length
= max_len
;
879 usb_handle_packet(q
->ep
->dev
, &async
->packet
);
883 /* invalid pid : frame interrupted */
884 uhci_async_free(async
);
885 s
->status
|= UHCI_STS_HCPERR
;
887 return TD_RESULT_STOP_FRAME
;
890 if (async
->packet
.status
== USB_RET_ASYNC
) {
891 uhci_async_link(async
);
893 uhci_queue_fill(q
, td
);
895 return TD_RESULT_ASYNC_START
;
899 ret
= uhci_complete_td(s
, td
, async
, int_mask
);
900 uhci_async_free(async
);
904 static void uhci_async_complete(USBPort
*port
, USBPacket
*packet
)
906 UHCIAsync
*async
= container_of(packet
, UHCIAsync
, packet
);
907 UHCIState
*s
= async
->queue
->uhci
;
909 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
910 uhci_async_cancel(async
);
915 /* Force processing of this packet *now*, needed for migration */
916 s
->completions_only
= true;
917 qemu_bh_schedule(s
->bh
);
920 static int is_valid(uint32_t link
)
922 return (link
& 1) == 0;
925 static int is_qh(uint32_t link
)
927 return (link
& 2) != 0;
930 static int depth_first(uint32_t link
)
932 return (link
& 4) != 0;
935 /* QH DB used for detecting QH loops */
936 #define UHCI_MAX_QUEUES 128
938 uint32_t addr
[UHCI_MAX_QUEUES
];
942 static void qhdb_reset(QhDb
*db
)
947 /* Add QH to DB. Returns 1 if already present or DB is full. */
948 static int qhdb_insert(QhDb
*db
, uint32_t addr
)
951 for (i
= 0; i
< db
->count
; i
++)
952 if (db
->addr
[i
] == addr
)
955 if (db
->count
>= UHCI_MAX_QUEUES
)
958 db
->addr
[db
->count
++] = addr
;
962 static void uhci_queue_fill(UHCIQueue
*q
, UHCI_TD
*td
)
964 uint32_t int_mask
= 0;
965 uint32_t plink
= td
->link
;
969 while (is_valid(plink
)) {
970 uhci_read_td(q
->uhci
, &ptd
, plink
);
971 if (!(ptd
.ctrl
& TD_CTRL_ACTIVE
)) {
974 if (uhci_queue_token(&ptd
) != q
->token
) {
977 trace_usb_uhci_td_queue(plink
& ~0xf, ptd
.ctrl
, ptd
.token
);
978 ret
= uhci_handle_td(q
->uhci
, q
, q
->qh_addr
, &ptd
, plink
, &int_mask
);
979 if (ret
== TD_RESULT_ASYNC_CONT
) {
982 assert(ret
== TD_RESULT_ASYNC_START
);
983 assert(int_mask
== 0);
986 usb_device_flush_ep_queue(q
->ep
->dev
, q
->ep
);
989 static void uhci_process_frame(UHCIState
*s
)
991 uint32_t frame_addr
, link
, old_td_ctrl
, val
, int_mask
;
992 uint32_t curr_qh
, td_count
= 0;
998 frame_addr
= s
->fl_base_addr
+ ((s
->frnum
& 0x3ff) << 2);
1000 pci_dma_read(&s
->dev
, frame_addr
, &link
, 4);
1001 le32_to_cpus(&link
);
1008 for (cnt
= FRAME_MAX_LOOPS
; is_valid(link
) && cnt
; cnt
--) {
1009 if (!s
->completions_only
&& s
->frame_bytes
>= s
->frame_bandwidth
) {
1010 /* We've reached the usb 1.1 bandwidth, which is
1011 1280 bytes/frame, stop processing */
1012 trace_usb_uhci_frame_stop_bandwidth();
1017 trace_usb_uhci_qh_load(link
& ~0xf);
1019 if (qhdb_insert(&qhdb
, link
)) {
1021 * We're going in circles. Which is not a bug because
1022 * HCD is allowed to do that as part of the BW management.
1024 * Stop processing here if no transaction has been done
1025 * since we've been here last time.
1027 if (td_count
== 0) {
1028 trace_usb_uhci_frame_loop_stop_idle();
1031 trace_usb_uhci_frame_loop_continue();
1034 qhdb_insert(&qhdb
, link
);
1038 pci_dma_read(&s
->dev
, link
& ~0xf, &qh
, sizeof(qh
));
1039 le32_to_cpus(&qh
.link
);
1040 le32_to_cpus(&qh
.el_link
);
1042 if (!is_valid(qh
.el_link
)) {
1043 /* QH w/o elements */
1047 /* QH with elements */
1055 uhci_read_td(s
, &td
, link
);
1056 trace_usb_uhci_td_load(curr_qh
& ~0xf, link
& ~0xf, td
.ctrl
, td
.token
);
1058 old_td_ctrl
= td
.ctrl
;
1059 ret
= uhci_handle_td(s
, NULL
, curr_qh
, &td
, link
, &int_mask
);
1060 if (old_td_ctrl
!= td
.ctrl
) {
1061 /* update the status bits of the TD */
1062 val
= cpu_to_le32(td
.ctrl
);
1063 pci_dma_write(&s
->dev
, (link
& ~0xf) + 4, &val
, sizeof(val
));
1067 case TD_RESULT_STOP_FRAME
: /* interrupted frame */
1070 case TD_RESULT_NEXT_QH
:
1071 case TD_RESULT_ASYNC_CONT
:
1072 trace_usb_uhci_td_nextqh(curr_qh
& ~0xf, link
& ~0xf);
1073 link
= curr_qh
? qh
.link
: td
.link
;
1076 case TD_RESULT_ASYNC_START
:
1077 trace_usb_uhci_td_async(curr_qh
& ~0xf, link
& ~0xf);
1078 link
= curr_qh
? qh
.link
: td
.link
;
1081 case TD_RESULT_COMPLETE
:
1082 trace_usb_uhci_td_complete(curr_qh
& ~0xf, link
& ~0xf);
1085 s
->frame_bytes
+= (td
.ctrl
& 0x7ff) + 1;
1088 /* update QH element link */
1090 val
= cpu_to_le32(qh
.el_link
);
1091 pci_dma_write(&s
->dev
, (curr_qh
& ~0xf) + 4, &val
, sizeof(val
));
1093 if (!depth_first(link
)) {
1094 /* done with this QH */
1102 assert(!"unknown return code");
1105 /* go to the next entry */
1109 s
->pending_int_mask
|= int_mask
;
1112 static void uhci_bh(void *opaque
)
1114 UHCIState
*s
= opaque
;
1115 uhci_process_frame(s
);
1118 static void uhci_frame_timer(void *opaque
)
1120 UHCIState
*s
= opaque
;
1121 uint64_t t_now
, t_last_run
;
1123 const uint64_t frame_t
= get_ticks_per_sec() / FRAME_TIMER_FREQ
;
1125 s
->completions_only
= false;
1126 qemu_bh_cancel(s
->bh
);
1128 if (!(s
->cmd
& UHCI_CMD_RS
)) {
1130 trace_usb_uhci_schedule_stop();
1131 timer_del(s
->frame_timer
);
1132 uhci_async_cancel_all(s
);
1133 /* set hchalted bit in status - UHCI11D 2.1.2 */
1134 s
->status
|= UHCI_STS_HCHALTED
;
1138 /* We still store expire_time in our state, for migration */
1139 t_last_run
= s
->expire_time
- frame_t
;
1140 t_now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1142 /* Process up to MAX_FRAMES_PER_TICK frames */
1143 frames
= (t_now
- t_last_run
) / frame_t
;
1144 if (frames
> s
->maxframes
) {
1145 int skipped
= frames
- s
->maxframes
;
1146 s
->expire_time
+= skipped
* frame_t
;
1147 s
->frnum
= (s
->frnum
+ skipped
) & 0x7ff;
1150 if (frames
> MAX_FRAMES_PER_TICK
) {
1151 frames
= MAX_FRAMES_PER_TICK
;
1154 for (i
= 0; i
< frames
; i
++) {
1156 trace_usb_uhci_frame_start(s
->frnum
);
1157 uhci_async_validate_begin(s
);
1158 uhci_process_frame(s
);
1159 uhci_async_validate_end(s
);
1160 /* The spec says frnum is the frame currently being processed, and
1161 * the guest must look at frnum - 1 on interrupt, so inc frnum now */
1162 s
->frnum
= (s
->frnum
+ 1) & 0x7ff;
1163 s
->expire_time
+= frame_t
;
1166 /* Complete the previous frame(s) */
1167 if (s
->pending_int_mask
) {
1168 s
->status2
|= s
->pending_int_mask
;
1169 s
->status
|= UHCI_STS_USBINT
;
1172 s
->pending_int_mask
= 0;
1174 timer_mod(s
->frame_timer
, t_now
+ frame_t
);
1177 static const MemoryRegionOps uhci_ioport_ops
= {
1178 .read
= uhci_port_read
,
1179 .write
= uhci_port_write
,
1180 .valid
.min_access_size
= 1,
1181 .valid
.max_access_size
= 4,
1182 .impl
.min_access_size
= 2,
1183 .impl
.max_access_size
= 2,
1184 .endianness
= DEVICE_LITTLE_ENDIAN
,
1187 static USBPortOps uhci_port_ops
= {
1188 .attach
= uhci_attach
,
1189 .detach
= uhci_detach
,
1190 .child_detach
= uhci_child_detach
,
1191 .wakeup
= uhci_wakeup
,
1192 .complete
= uhci_async_complete
,
1195 static USBBusOps uhci_bus_ops
= {
1198 static void usb_uhci_common_realize(PCIDevice
*dev
, Error
**errp
)
1201 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(dev
);
1202 UHCIPCIDeviceClass
*u
= container_of(pc
, UHCIPCIDeviceClass
, parent_class
);
1203 UHCIState
*s
= UHCI(dev
);
1204 uint8_t *pci_conf
= s
->dev
.config
;
1207 pci_conf
[PCI_CLASS_PROG
] = 0x00;
1208 /* TODO: reset value should be 0. */
1209 pci_conf
[USB_SBRN
] = USB_RELEASE_1
; // release number
1211 pci_config_set_interrupt_pin(pci_conf
, u
->info
.irq_pin
+ 1);
1214 USBPort
*ports
[NB_PORTS
];
1215 for(i
= 0; i
< NB_PORTS
; i
++) {
1216 ports
[i
] = &s
->ports
[i
].port
;
1218 usb_register_companion(s
->masterbus
, ports
, NB_PORTS
,
1219 s
->firstport
, s
, &uhci_port_ops
,
1220 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
,
1223 error_propagate(errp
, err
);
1227 usb_bus_new(&s
->bus
, sizeof(s
->bus
), &uhci_bus_ops
, DEVICE(dev
));
1228 for (i
= 0; i
< NB_PORTS
; i
++) {
1229 usb_register_port(&s
->bus
, &s
->ports
[i
].port
, s
, i
, &uhci_port_ops
,
1230 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
1233 s
->bh
= qemu_bh_new(uhci_bh
, s
);
1234 s
->frame_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, uhci_frame_timer
, s
);
1235 s
->num_ports_vmstate
= NB_PORTS
;
1236 QTAILQ_INIT(&s
->queues
);
1238 memory_region_init_io(&s
->io_bar
, OBJECT(s
), &uhci_ioport_ops
, s
,
1241 /* Use region 4 for consistency with real hardware. BSD guests seem
1243 pci_register_bar(&s
->dev
, 4, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io_bar
);
1246 static void usb_uhci_vt82c686b_realize(PCIDevice
*dev
, Error
**errp
)
1248 UHCIState
*s
= UHCI(dev
);
1249 uint8_t *pci_conf
= s
->dev
.config
;
1251 /* USB misc control 1/2 */
1252 pci_set_long(pci_conf
+ 0x40,0x00001000);
1254 pci_set_long(pci_conf
+ 0x80,0x00020001);
1255 /* USB legacy support */
1256 pci_set_long(pci_conf
+ 0xc0,0x00002000);
1258 usb_uhci_common_realize(dev
, errp
);
1261 static void usb_uhci_exit(PCIDevice
*dev
)
1263 UHCIState
*s
= UHCI(dev
);
1265 trace_usb_uhci_exit();
1267 if (s
->frame_timer
) {
1268 timer_del(s
->frame_timer
);
1269 timer_free(s
->frame_timer
);
1270 s
->frame_timer
= NULL
;
1274 qemu_bh_delete(s
->bh
);
1277 uhci_async_cancel_all(s
);
1279 if (!s
->masterbus
) {
1280 usb_bus_release(&s
->bus
);
1284 static Property uhci_properties_companion
[] = {
1285 DEFINE_PROP_STRING("masterbus", UHCIState
, masterbus
),
1286 DEFINE_PROP_UINT32("firstport", UHCIState
, firstport
, 0),
1287 DEFINE_PROP_UINT32("bandwidth", UHCIState
, frame_bandwidth
, 1280),
1288 DEFINE_PROP_UINT32("maxframes", UHCIState
, maxframes
, 128),
1289 DEFINE_PROP_END_OF_LIST(),
1291 static Property uhci_properties_standalone
[] = {
1292 DEFINE_PROP_UINT32("bandwidth", UHCIState
, frame_bandwidth
, 1280),
1293 DEFINE_PROP_UINT32("maxframes", UHCIState
, maxframes
, 128),
1294 DEFINE_PROP_END_OF_LIST(),
1297 static void uhci_class_init(ObjectClass
*klass
, void *data
)
1299 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1300 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1302 k
->class_id
= PCI_CLASS_SERIAL_USB
;
1303 dc
->vmsd
= &vmstate_uhci
;
1304 dc
->reset
= uhci_reset
;
1305 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
1308 static const TypeInfo uhci_pci_type_info
= {
1310 .parent
= TYPE_PCI_DEVICE
,
1311 .instance_size
= sizeof(UHCIState
),
1312 .class_size
= sizeof(UHCIPCIDeviceClass
),
1314 .class_init
= uhci_class_init
,
1317 static void uhci_data_class_init(ObjectClass
*klass
, void *data
)
1319 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1320 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1321 UHCIPCIDeviceClass
*u
= container_of(k
, UHCIPCIDeviceClass
, parent_class
);
1322 UHCIInfo
*info
= data
;
1324 k
->realize
= info
->realize
? info
->realize
: usb_uhci_common_realize
;
1325 k
->exit
= info
->unplug
? usb_uhci_exit
: NULL
;
1326 k
->vendor_id
= info
->vendor_id
;
1327 k
->device_id
= info
->device_id
;
1328 k
->revision
= info
->revision
;
1329 if (!info
->unplug
) {
1330 /* uhci controllers in companion setups can't be hotplugged */
1331 dc
->hotpluggable
= false;
1332 dc
->props
= uhci_properties_companion
;
1334 dc
->props
= uhci_properties_standalone
;
1339 static UHCIInfo uhci_info
[] = {
1341 .name
= "piix3-usb-uhci",
1342 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1343 .device_id
= PCI_DEVICE_ID_INTEL_82371SB_2
,
1348 .name
= "piix4-usb-uhci",
1349 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1350 .device_id
= PCI_DEVICE_ID_INTEL_82371AB_2
,
1355 .name
= "vt82c686b-usb-uhci",
1356 .vendor_id
= PCI_VENDOR_ID_VIA
,
1357 .device_id
= PCI_DEVICE_ID_VIA_UHCI
,
1360 .realize
= usb_uhci_vt82c686b_realize
,
1363 .name
= "ich9-usb-uhci1", /* 00:1d.0 */
1364 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1365 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI1
,
1370 .name
= "ich9-usb-uhci2", /* 00:1d.1 */
1371 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1372 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI2
,
1377 .name
= "ich9-usb-uhci3", /* 00:1d.2 */
1378 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1379 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI3
,
1384 .name
= "ich9-usb-uhci4", /* 00:1a.0 */
1385 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1386 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI4
,
1391 .name
= "ich9-usb-uhci5", /* 00:1a.1 */
1392 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1393 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI5
,
1398 .name
= "ich9-usb-uhci6", /* 00:1a.2 */
1399 .vendor_id
= PCI_VENDOR_ID_INTEL
,
1400 .device_id
= PCI_DEVICE_ID_INTEL_82801I_UHCI6
,
1407 static void uhci_register_types(void)
1409 TypeInfo uhci_type_info
= {
1410 .parent
= TYPE_UHCI
,
1411 .class_init
= uhci_data_class_init
,
1415 type_register_static(&uhci_pci_type_info
);
1417 for (i
= 0; i
< ARRAY_SIZE(uhci_info
); i
++) {
1418 uhci_type_info
.name
= uhci_info
[i
].name
;
1419 uhci_type_info
.class_data
= uhci_info
+ i
;
1420 type_register(&uhci_type_info
);
1424 type_init(uhci_register_types
)