2 * QEMU USB EHCI Emulation
4 * Copyright(c) 2008 Emutex Ltd. (address@hidden)
5 * Copyright(c) 2011-2012 Red Hat, Inc.
8 * Gerd Hoffmann <kraxel@redhat.com>
9 * Hans de Goede <hdegoede@redhat.com>
11 * EHCI project was started by Mark Burkley, with contributions by
12 * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf,
13 * Jan Kiszka and Vincent Palatin contributed bugfixes.
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include "qemu/osdep.h"
30 #include "qapi/error.h"
32 #include "hw/usb/ehci-regs.h"
33 #include "hw/usb/hcd-ehci.h"
34 #include "migration/vmstate.h"
36 #include "qemu/error-report.h"
37 #include "qemu/main-loop.h"
38 #include "sysemu/runstate.h"
40 #define FRAME_TIMER_FREQ 1000
41 #define FRAME_TIMER_NS (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ)
42 #define UFRAME_TIMER_NS (FRAME_TIMER_NS / 8)
44 #define NB_MAXINTRATE 8 // Max rate at which controller issues ints
45 #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction
46 #define MAX_QH 100 // Max allowable queue heads in a chain
47 #define MIN_UFR_PER_TICK 24 /* Min frames to process when catching up */
48 #define PERIODIC_ACTIVE 512 /* Micro-frames */
50 /* Internal periodic / asynchronous schedule state machine states
57 /* The following states are internal to the state machine function
71 /* macros for accessing fields within next link pointer entry */
72 #define NLPTR_GET(x) ((x) & 0xffffffe0)
73 #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3)
74 #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid
76 /* link pointer types */
77 #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor
78 #define NLPTR_TYPE_QH 1 // queue head
79 #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor
80 #define NLPTR_TYPE_FSTN 3 // frame span traversal node
82 #define SET_LAST_RUN_CLOCK(s) \
83 (s)->last_run_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
85 /* nifty macros from Arnon's EHCI version */
86 #define get_field(data, field) \
87 (((data) & field##_MASK) >> field##_SH)
89 #define set_field(data, newval, field) do { \
90 uint32_t val = *data; \
91 val &= ~ field##_MASK; \
92 val |= ((newval) << field##_SH) & field##_MASK; \
96 static const char *ehci_state_names
[] = {
97 [EST_INACTIVE
] = "INACTIVE",
98 [EST_ACTIVE
] = "ACTIVE",
99 [EST_EXECUTING
] = "EXECUTING",
100 [EST_SLEEPING
] = "SLEEPING",
101 [EST_WAITLISTHEAD
] = "WAITLISTHEAD",
102 [EST_FETCHENTRY
] = "FETCH ENTRY",
103 [EST_FETCHQH
] = "FETCH QH",
104 [EST_FETCHITD
] = "FETCH ITD",
105 [EST_ADVANCEQUEUE
] = "ADVANCEQUEUE",
106 [EST_FETCHQTD
] = "FETCH QTD",
107 [EST_EXECUTE
] = "EXECUTE",
108 [EST_WRITEBACK
] = "WRITEBACK",
109 [EST_HORIZONTALQH
] = "HORIZONTALQH",
112 static const char *ehci_mmio_names
[] = {
115 [USBINTR
] = "USBINTR",
116 [FRINDEX
] = "FRINDEX",
117 [PERIODICLISTBASE
] = "P-LIST BASE",
118 [ASYNCLISTADDR
] = "A-LIST ADDR",
119 [CONFIGFLAG
] = "CONFIGFLAG",
122 static int ehci_state_executing(EHCIQueue
*q
);
123 static int ehci_state_writeback(EHCIQueue
*q
);
124 static int ehci_state_advqueue(EHCIQueue
*q
);
125 static int ehci_fill_queue(EHCIPacket
*p
);
126 static void ehci_free_packet(EHCIPacket
*p
);
128 static const char *nr2str(const char **n
, size_t len
, uint32_t nr
)
130 if (nr
< len
&& n
[nr
] != NULL
) {
137 static const char *state2str(uint32_t state
)
139 return nr2str(ehci_state_names
, ARRAY_SIZE(ehci_state_names
), state
);
142 static const char *addr2str(hwaddr addr
)
144 return nr2str(ehci_mmio_names
, ARRAY_SIZE(ehci_mmio_names
), addr
);
147 static void ehci_trace_usbsts(uint32_t mask
, int state
)
150 if (mask
& USBSTS_INT
) {
151 trace_usb_ehci_usbsts("INT", state
);
153 if (mask
& USBSTS_ERRINT
) {
154 trace_usb_ehci_usbsts("ERRINT", state
);
156 if (mask
& USBSTS_PCD
) {
157 trace_usb_ehci_usbsts("PCD", state
);
159 if (mask
& USBSTS_FLR
) {
160 trace_usb_ehci_usbsts("FLR", state
);
162 if (mask
& USBSTS_HSE
) {
163 trace_usb_ehci_usbsts("HSE", state
);
165 if (mask
& USBSTS_IAA
) {
166 trace_usb_ehci_usbsts("IAA", state
);
170 if (mask
& USBSTS_HALT
) {
171 trace_usb_ehci_usbsts("HALT", state
);
173 if (mask
& USBSTS_REC
) {
174 trace_usb_ehci_usbsts("REC", state
);
176 if (mask
& USBSTS_PSS
) {
177 trace_usb_ehci_usbsts("PSS", state
);
179 if (mask
& USBSTS_ASS
) {
180 trace_usb_ehci_usbsts("ASS", state
);
184 static inline void ehci_set_usbsts(EHCIState
*s
, int mask
)
186 if ((s
->usbsts
& mask
) == mask
) {
189 ehci_trace_usbsts(mask
, 1);
193 static inline void ehci_clear_usbsts(EHCIState
*s
, int mask
)
195 if ((s
->usbsts
& mask
) == 0) {
198 ehci_trace_usbsts(mask
, 0);
202 /* update irq line */
203 static inline void ehci_update_irq(EHCIState
*s
)
207 if ((s
->usbsts
& USBINTR_MASK
) & s
->usbintr
) {
211 trace_usb_ehci_irq(level
, s
->frindex
, s
->usbsts
, s
->usbintr
);
212 qemu_set_irq(s
->irq
, level
);
215 /* flag interrupt condition */
216 static inline void ehci_raise_irq(EHCIState
*s
, int intr
)
218 if (intr
& (USBSTS_PCD
| USBSTS_FLR
| USBSTS_HSE
)) {
222 s
->usbsts_pending
|= intr
;
227 * Commit pending interrupts (added via ehci_raise_irq),
228 * at the rate allowed by "Interrupt Threshold Control".
230 static inline void ehci_commit_irq(EHCIState
*s
)
234 if (!s
->usbsts_pending
) {
237 if (s
->usbsts_frindex
> s
->frindex
) {
241 itc
= (s
->usbcmd
>> 16) & 0xff;
242 s
->usbsts
|= s
->usbsts_pending
;
243 s
->usbsts_pending
= 0;
244 s
->usbsts_frindex
= s
->frindex
+ itc
;
248 static void ehci_update_halt(EHCIState
*s
)
250 if (s
->usbcmd
& USBCMD_RUNSTOP
) {
251 ehci_clear_usbsts(s
, USBSTS_HALT
);
253 if (s
->astate
== EST_INACTIVE
&& s
->pstate
== EST_INACTIVE
) {
254 ehci_set_usbsts(s
, USBSTS_HALT
);
259 static void ehci_set_state(EHCIState
*s
, int async
, int state
)
262 trace_usb_ehci_state("async", state2str(state
));
264 if (s
->astate
== EST_INACTIVE
) {
265 ehci_clear_usbsts(s
, USBSTS_ASS
);
268 ehci_set_usbsts(s
, USBSTS_ASS
);
271 trace_usb_ehci_state("periodic", state2str(state
));
273 if (s
->pstate
== EST_INACTIVE
) {
274 ehci_clear_usbsts(s
, USBSTS_PSS
);
277 ehci_set_usbsts(s
, USBSTS_PSS
);
282 static int ehci_get_state(EHCIState
*s
, int async
)
284 return async
? s
->astate
: s
->pstate
;
287 static void ehci_set_fetch_addr(EHCIState
*s
, int async
, uint32_t addr
)
290 s
->a_fetch_addr
= addr
;
292 s
->p_fetch_addr
= addr
;
296 static int ehci_get_fetch_addr(EHCIState
*s
, int async
)
298 return async
? s
->a_fetch_addr
: s
->p_fetch_addr
;
301 static void ehci_trace_qh(EHCIQueue
*q
, hwaddr addr
, EHCIqh
*qh
)
303 /* need three here due to argument count limits */
304 trace_usb_ehci_qh_ptrs(q
, addr
, qh
->next
,
305 qh
->current_qtd
, qh
->next_qtd
, qh
->altnext_qtd
);
306 trace_usb_ehci_qh_fields(addr
,
307 get_field(qh
->epchar
, QH_EPCHAR_RL
),
308 get_field(qh
->epchar
, QH_EPCHAR_MPLEN
),
309 get_field(qh
->epchar
, QH_EPCHAR_EPS
),
310 get_field(qh
->epchar
, QH_EPCHAR_EP
),
311 get_field(qh
->epchar
, QH_EPCHAR_DEVADDR
));
312 trace_usb_ehci_qh_bits(addr
,
313 (bool)(qh
->epchar
& QH_EPCHAR_C
),
314 (bool)(qh
->epchar
& QH_EPCHAR_H
),
315 (bool)(qh
->epchar
& QH_EPCHAR_DTC
),
316 (bool)(qh
->epchar
& QH_EPCHAR_I
));
319 static void ehci_trace_qtd(EHCIQueue
*q
, hwaddr addr
, EHCIqtd
*qtd
)
321 /* need three here due to argument count limits */
322 trace_usb_ehci_qtd_ptrs(q
, addr
, qtd
->next
, qtd
->altnext
);
323 trace_usb_ehci_qtd_fields(addr
,
324 get_field(qtd
->token
, QTD_TOKEN_TBYTES
),
325 get_field(qtd
->token
, QTD_TOKEN_CPAGE
),
326 get_field(qtd
->token
, QTD_TOKEN_CERR
),
327 get_field(qtd
->token
, QTD_TOKEN_PID
));
328 trace_usb_ehci_qtd_bits(addr
,
329 (bool)(qtd
->token
& QTD_TOKEN_IOC
),
330 (bool)(qtd
->token
& QTD_TOKEN_ACTIVE
),
331 (bool)(qtd
->token
& QTD_TOKEN_HALT
),
332 (bool)(qtd
->token
& QTD_TOKEN_BABBLE
),
333 (bool)(qtd
->token
& QTD_TOKEN_XACTERR
));
336 static void ehci_trace_itd(EHCIState
*s
, hwaddr addr
, EHCIitd
*itd
)
338 trace_usb_ehci_itd(addr
, itd
->next
,
339 get_field(itd
->bufptr
[1], ITD_BUFPTR_MAXPKT
),
340 get_field(itd
->bufptr
[2], ITD_BUFPTR_MULT
),
341 get_field(itd
->bufptr
[0], ITD_BUFPTR_EP
),
342 get_field(itd
->bufptr
[0], ITD_BUFPTR_DEVADDR
));
345 static void ehci_trace_sitd(EHCIState
*s
, hwaddr addr
,
348 trace_usb_ehci_sitd(addr
, sitd
->next
,
349 (bool)(sitd
->results
& SITD_RESULTS_ACTIVE
));
352 static void ehci_trace_guest_bug(EHCIState
*s
, const char *message
)
354 trace_usb_ehci_guest_bug(message
);
355 warn_report("%s", message
);
358 static inline bool ehci_enabled(EHCIState
*s
)
360 return s
->usbcmd
& USBCMD_RUNSTOP
;
363 static inline bool ehci_async_enabled(EHCIState
*s
)
365 return ehci_enabled(s
) && (s
->usbcmd
& USBCMD_ASE
);
368 static inline bool ehci_periodic_enabled(EHCIState
*s
)
370 return ehci_enabled(s
) && (s
->usbcmd
& USBCMD_PSE
);
373 /* Get an array of dwords from main memory */
374 static inline int get_dwords(EHCIState
*ehci
, uint32_t addr
,
375 uint32_t *buf
, int num
)
380 ehci_raise_irq(ehci
, USBSTS_HSE
);
381 ehci
->usbcmd
&= ~USBCMD_RUNSTOP
;
382 trace_usb_ehci_dma_error();
386 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
387 dma_memory_read(ehci
->as
, addr
, buf
, sizeof(*buf
));
388 *buf
= le32_to_cpu(*buf
);
394 /* Put an array of dwords in to main memory */
395 static inline int put_dwords(EHCIState
*ehci
, uint32_t addr
,
396 uint32_t *buf
, int num
)
401 ehci_raise_irq(ehci
, USBSTS_HSE
);
402 ehci
->usbcmd
&= ~USBCMD_RUNSTOP
;
403 trace_usb_ehci_dma_error();
407 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
408 uint32_t tmp
= cpu_to_le32(*buf
);
409 dma_memory_write(ehci
->as
, addr
, &tmp
, sizeof(tmp
));
415 static int ehci_get_pid(EHCIqtd
*qtd
)
417 switch (get_field(qtd
->token
, QTD_TOKEN_PID
)) {
419 return USB_TOKEN_OUT
;
423 return USB_TOKEN_SETUP
;
425 fprintf(stderr
, "bad token\n");
430 static bool ehci_verify_qh(EHCIQueue
*q
, EHCIqh
*qh
)
432 uint32_t devaddr
= get_field(qh
->epchar
, QH_EPCHAR_DEVADDR
);
433 uint32_t endp
= get_field(qh
->epchar
, QH_EPCHAR_EP
);
434 if ((devaddr
!= get_field(q
->qh
.epchar
, QH_EPCHAR_DEVADDR
)) ||
435 (endp
!= get_field(q
->qh
.epchar
, QH_EPCHAR_EP
)) ||
436 (qh
->current_qtd
!= q
->qh
.current_qtd
) ||
437 (q
->async
&& qh
->next_qtd
!= q
->qh
.next_qtd
) ||
438 (memcmp(&qh
->altnext_qtd
, &q
->qh
.altnext_qtd
,
439 7 * sizeof(uint32_t)) != 0) ||
440 (q
->dev
!= NULL
&& q
->dev
->addr
!= devaddr
)) {
447 static bool ehci_verify_qtd(EHCIPacket
*p
, EHCIqtd
*qtd
)
449 if (p
->qtdaddr
!= p
->queue
->qtdaddr
||
450 (p
->queue
->async
&& !NLPTR_TBIT(p
->qtd
.next
) &&
451 (p
->qtd
.next
!= qtd
->next
)) ||
452 (!NLPTR_TBIT(p
->qtd
.altnext
) && (p
->qtd
.altnext
!= qtd
->altnext
)) ||
453 p
->qtd
.token
!= qtd
->token
||
454 p
->qtd
.bufptr
[0] != qtd
->bufptr
[0]) {
461 static bool ehci_verify_pid(EHCIQueue
*q
, EHCIqtd
*qtd
)
463 int ep
= get_field(q
->qh
.epchar
, QH_EPCHAR_EP
);
464 int pid
= ehci_get_pid(qtd
);
466 /* Note the pid changing is normal for ep 0 (the control ep) */
467 if (q
->last_pid
&& ep
!= 0 && pid
!= q
->last_pid
) {
474 /* Finish executing and writeback a packet outside of the regular
475 fetchqh -> fetchqtd -> execute -> writeback cycle */
476 static void ehci_writeback_async_complete_packet(EHCIPacket
*p
)
478 EHCIQueue
*q
= p
->queue
;
483 /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */
484 get_dwords(q
->ehci
, NLPTR_GET(q
->qhaddr
),
485 (uint32_t *) &qh
, sizeof(EHCIqh
) >> 2);
486 get_dwords(q
->ehci
, NLPTR_GET(q
->qtdaddr
),
487 (uint32_t *) &qtd
, sizeof(EHCIqtd
) >> 2);
488 if (!ehci_verify_qh(q
, &qh
) || !ehci_verify_qtd(p
, &qtd
)) {
489 p
->async
= EHCI_ASYNC_INITIALIZED
;
494 state
= ehci_get_state(q
->ehci
, q
->async
);
495 ehci_state_executing(q
);
496 ehci_state_writeback(q
); /* Frees the packet! */
497 if (!(q
->qh
.token
& QTD_TOKEN_HALT
)) {
498 ehci_state_advqueue(q
);
500 ehci_set_state(q
->ehci
, q
->async
, state
);
503 /* packet management */
505 static EHCIPacket
*ehci_alloc_packet(EHCIQueue
*q
)
509 p
= g_new0(EHCIPacket
, 1);
511 usb_packet_init(&p
->packet
);
512 QTAILQ_INSERT_TAIL(&q
->packets
, p
, next
);
513 trace_usb_ehci_packet_action(p
->queue
, p
, "alloc");
517 static void ehci_free_packet(EHCIPacket
*p
)
519 if (p
->async
== EHCI_ASYNC_FINISHED
&&
520 !(p
->queue
->qh
.token
& QTD_TOKEN_HALT
)) {
521 ehci_writeback_async_complete_packet(p
);
524 trace_usb_ehci_packet_action(p
->queue
, p
, "free");
525 if (p
->async
== EHCI_ASYNC_INFLIGHT
) {
526 usb_cancel_packet(&p
->packet
);
528 if (p
->async
== EHCI_ASYNC_FINISHED
&&
529 p
->packet
.status
== USB_RET_SUCCESS
) {
531 "EHCI: Dropping completed packet from halted %s ep %02X\n",
532 (p
->pid
== USB_TOKEN_IN
) ? "in" : "out",
533 get_field(p
->queue
->qh
.epchar
, QH_EPCHAR_EP
));
535 if (p
->async
!= EHCI_ASYNC_NONE
) {
536 usb_packet_unmap(&p
->packet
, &p
->sgl
);
537 qemu_sglist_destroy(&p
->sgl
);
539 QTAILQ_REMOVE(&p
->queue
->packets
, p
, next
);
540 usb_packet_cleanup(&p
->packet
);
544 /* queue management */
546 static EHCIQueue
*ehci_alloc_queue(EHCIState
*ehci
, uint32_t addr
, int async
)
548 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
551 q
= g_malloc0(sizeof(*q
));
555 QTAILQ_INIT(&q
->packets
);
556 QTAILQ_INSERT_HEAD(head
, q
, next
);
557 trace_usb_ehci_queue_action(q
, "alloc");
561 static void ehci_queue_stopped(EHCIQueue
*q
)
563 int endp
= get_field(q
->qh
.epchar
, QH_EPCHAR_EP
);
565 if (!q
->last_pid
|| !q
->dev
) {
569 usb_device_ep_stopped(q
->dev
, usb_ep_get(q
->dev
, q
->last_pid
, endp
));
572 static int ehci_cancel_queue(EHCIQueue
*q
)
577 p
= QTAILQ_FIRST(&q
->packets
);
582 trace_usb_ehci_queue_action(q
, "cancel");
586 } while ((p
= QTAILQ_FIRST(&q
->packets
)) != NULL
);
589 ehci_queue_stopped(q
);
593 static int ehci_reset_queue(EHCIQueue
*q
)
597 trace_usb_ehci_queue_action(q
, "reset");
598 packets
= ehci_cancel_queue(q
);
605 static void ehci_free_queue(EHCIQueue
*q
, const char *warn
)
607 EHCIQueueHead
*head
= q
->async
? &q
->ehci
->aqueues
: &q
->ehci
->pqueues
;
610 trace_usb_ehci_queue_action(q
, "free");
611 cancelled
= ehci_cancel_queue(q
);
612 if (warn
&& cancelled
> 0) {
613 ehci_trace_guest_bug(q
->ehci
, warn
);
615 QTAILQ_REMOVE(head
, q
, next
);
619 static EHCIQueue
*ehci_find_queue_by_qh(EHCIState
*ehci
, uint32_t addr
,
622 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
625 QTAILQ_FOREACH(q
, head
, next
) {
626 if (addr
== q
->qhaddr
) {
633 static void ehci_queues_rip_unused(EHCIState
*ehci
, int async
)
635 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
636 const char *warn
= async
? "guest unlinked busy QH" : NULL
;
637 uint64_t maxage
= FRAME_TIMER_NS
* ehci
->maxframes
* 4;
640 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
643 q
->ts
= ehci
->last_run_ns
;
646 if (ehci
->last_run_ns
< q
->ts
+ maxage
) {
649 ehci_free_queue(q
, warn
);
653 static void ehci_queues_rip_unseen(EHCIState
*ehci
, int async
)
655 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
658 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
660 ehci_free_queue(q
, NULL
);
665 static void ehci_queues_rip_device(EHCIState
*ehci
, USBDevice
*dev
, int async
)
667 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
670 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
674 ehci_free_queue(q
, NULL
);
678 static void ehci_queues_rip_all(EHCIState
*ehci
, int async
)
680 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
681 const char *warn
= async
? "guest stopped busy async schedule" : NULL
;
684 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
685 ehci_free_queue(q
, warn
);
689 /* Attach or detach a device on root hub */
691 static void ehci_attach(USBPort
*port
)
693 EHCIState
*s
= port
->opaque
;
694 uint32_t *portsc
= &s
->portsc
[port
->index
];
695 const char *owner
= (*portsc
& PORTSC_POWNER
) ? "comp" : "ehci";
697 trace_usb_ehci_port_attach(port
->index
, owner
, port
->dev
->product_desc
);
699 if (*portsc
& PORTSC_POWNER
) {
700 USBPort
*companion
= s
->companion_ports
[port
->index
];
701 companion
->dev
= port
->dev
;
702 companion
->ops
->attach(companion
);
706 *portsc
|= PORTSC_CONNECT
;
707 *portsc
|= PORTSC_CSC
;
709 ehci_raise_irq(s
, USBSTS_PCD
);
712 static void ehci_detach(USBPort
*port
)
714 EHCIState
*s
= port
->opaque
;
715 uint32_t *portsc
= &s
->portsc
[port
->index
];
716 const char *owner
= (*portsc
& PORTSC_POWNER
) ? "comp" : "ehci";
718 trace_usb_ehci_port_detach(port
->index
, owner
);
720 if (*portsc
& PORTSC_POWNER
) {
721 USBPort
*companion
= s
->companion_ports
[port
->index
];
722 companion
->ops
->detach(companion
);
723 companion
->dev
= NULL
;
725 * EHCI spec 4.2.2: "When a disconnect occurs... On the event,
726 * the port ownership is returned immediately to the EHCI controller."
728 *portsc
&= ~PORTSC_POWNER
;
732 ehci_queues_rip_device(s
, port
->dev
, 0);
733 ehci_queues_rip_device(s
, port
->dev
, 1);
735 *portsc
&= ~(PORTSC_CONNECT
|PORTSC_PED
|PORTSC_SUSPEND
);
736 *portsc
|= PORTSC_CSC
;
738 ehci_raise_irq(s
, USBSTS_PCD
);
741 static void ehci_child_detach(USBPort
*port
, USBDevice
*child
)
743 EHCIState
*s
= port
->opaque
;
744 uint32_t portsc
= s
->portsc
[port
->index
];
746 if (portsc
& PORTSC_POWNER
) {
747 USBPort
*companion
= s
->companion_ports
[port
->index
];
748 companion
->ops
->child_detach(companion
, child
);
752 ehci_queues_rip_device(s
, child
, 0);
753 ehci_queues_rip_device(s
, child
, 1);
756 static void ehci_wakeup(USBPort
*port
)
758 EHCIState
*s
= port
->opaque
;
759 uint32_t *portsc
= &s
->portsc
[port
->index
];
761 if (*portsc
& PORTSC_POWNER
) {
762 USBPort
*companion
= s
->companion_ports
[port
->index
];
763 if (companion
->ops
->wakeup
) {
764 companion
->ops
->wakeup(companion
);
769 if (*portsc
& PORTSC_SUSPEND
) {
770 trace_usb_ehci_port_wakeup(port
->index
);
771 *portsc
|= PORTSC_FPRES
;
772 ehci_raise_irq(s
, USBSTS_PCD
);
775 qemu_bh_schedule(s
->async_bh
);
778 static void ehci_register_companion(USBBus
*bus
, USBPort
*ports
[],
779 uint32_t portcount
, uint32_t firstport
,
782 EHCIState
*s
= container_of(bus
, EHCIState
, bus
);
785 if (firstport
+ portcount
> NB_PORTS
) {
786 error_setg(errp
, "firstport must be between 0 and %u",
787 NB_PORTS
- portcount
);
791 for (i
= 0; i
< portcount
; i
++) {
792 if (s
->companion_ports
[firstport
+ i
]) {
793 error_setg(errp
, "firstport %u asks for ports %u-%u,"
794 " but port %u has a companion assigned already",
795 firstport
, firstport
, firstport
+ portcount
- 1,
801 for (i
= 0; i
< portcount
; i
++) {
802 s
->companion_ports
[firstport
+ i
] = ports
[i
];
803 s
->ports
[firstport
+ i
].speedmask
|=
804 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
;
805 /* Ensure devs attached before the initial reset go to the companion */
806 s
->portsc
[firstport
+ i
] = PORTSC_POWNER
;
809 s
->companion_count
++;
810 s
->caps
[0x05] = (s
->companion_count
<< 4) | portcount
;
813 static void ehci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
816 EHCIState
*s
= container_of(bus
, EHCIState
, bus
);
817 uint32_t portsc
= s
->portsc
[ep
->dev
->port
->index
];
819 if (portsc
& PORTSC_POWNER
) {
823 s
->periodic_sched_active
= PERIODIC_ACTIVE
;
824 qemu_bh_schedule(s
->async_bh
);
827 static USBDevice
*ehci_find_device(EHCIState
*ehci
, uint8_t addr
)
833 for (i
= 0; i
< NB_PORTS
; i
++) {
834 port
= &ehci
->ports
[i
];
835 if (!(ehci
->portsc
[i
] & PORTSC_PED
)) {
836 DPRINTF("Port %d not enabled\n", i
);
839 dev
= usb_find_device(port
, addr
);
847 /* 4.1 host controller initialization */
848 void ehci_reset(void *opaque
)
850 EHCIState
*s
= opaque
;
852 USBDevice
*devs
[NB_PORTS
];
854 trace_usb_ehci_reset();
857 * Do the detach before touching portsc, so that it correctly gets send to
858 * us or to our companion based on PORTSC_POWNER before the reset.
860 for(i
= 0; i
< NB_PORTS
; i
++) {
861 devs
[i
] = s
->ports
[i
].dev
;
862 if (devs
[i
] && devs
[i
]->attached
) {
863 usb_detach(&s
->ports
[i
]);
867 memset(&s
->opreg
, 0x00, sizeof(s
->opreg
));
868 memset(&s
->portsc
, 0x00, sizeof(s
->portsc
));
870 s
->usbcmd
= NB_MAXINTRATE
<< USBCMD_ITC_SH
;
871 s
->usbsts
= USBSTS_HALT
;
872 s
->usbsts_pending
= 0;
873 s
->usbsts_frindex
= 0;
876 s
->astate
= EST_INACTIVE
;
877 s
->pstate
= EST_INACTIVE
;
879 for(i
= 0; i
< NB_PORTS
; i
++) {
880 if (s
->companion_ports
[i
]) {
881 s
->portsc
[i
] = PORTSC_POWNER
| PORTSC_PPOWER
;
883 s
->portsc
[i
] = PORTSC_PPOWER
;
885 if (devs
[i
] && devs
[i
]->attached
) {
886 usb_attach(&s
->ports
[i
]);
887 usb_device_reset(devs
[i
]);
890 ehci_queues_rip_all(s
, 0);
891 ehci_queues_rip_all(s
, 1);
892 timer_del(s
->frame_timer
);
893 qemu_bh_cancel(s
->async_bh
);
896 static uint64_t ehci_caps_read(void *ptr
, hwaddr addr
,
900 return s
->caps
[addr
];
903 static void ehci_caps_write(void *ptr
, hwaddr addr
,
904 uint64_t val
, unsigned size
)
908 static uint64_t ehci_opreg_read(void *ptr
, hwaddr addr
,
916 /* Round down to mult of 8, else it can go backwards on migration */
917 val
= s
->frindex
& ~7;
920 val
= s
->opreg
[addr
>> 2];
923 trace_usb_ehci_opreg_read(addr
+ s
->opregbase
, addr2str(addr
), val
);
927 static uint64_t ehci_port_read(void *ptr
, hwaddr addr
,
933 val
= s
->portsc
[addr
>> 2];
934 trace_usb_ehci_portsc_read(addr
+ s
->portscbase
, addr
>> 2, val
);
938 static void handle_port_owner_write(EHCIState
*s
, int port
, uint32_t owner
)
940 USBDevice
*dev
= s
->ports
[port
].dev
;
941 uint32_t *portsc
= &s
->portsc
[port
];
944 if (s
->companion_ports
[port
] == NULL
)
947 owner
= owner
& PORTSC_POWNER
;
948 orig
= *portsc
& PORTSC_POWNER
;
950 if (!(owner
^ orig
)) {
954 if (dev
&& dev
->attached
) {
955 usb_detach(&s
->ports
[port
]);
958 *portsc
&= ~PORTSC_POWNER
;
961 if (dev
&& dev
->attached
) {
962 usb_attach(&s
->ports
[port
]);
966 static void ehci_port_write(void *ptr
, hwaddr addr
,
967 uint64_t val
, unsigned size
)
970 int port
= addr
>> 2;
971 uint32_t *portsc
= &s
->portsc
[port
];
972 uint32_t old
= *portsc
;
973 USBDevice
*dev
= s
->ports
[port
].dev
;
975 trace_usb_ehci_portsc_write(addr
+ s
->portscbase
, addr
>> 2, val
);
978 *portsc
&= ~(val
& PORTSC_RWC_MASK
);
979 /* The guest may clear, but not set the PED bit */
980 *portsc
&= val
| ~PORTSC_PED
;
981 /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
982 handle_port_owner_write(s
, port
, val
);
983 /* And finally apply RO_MASK */
984 val
&= PORTSC_RO_MASK
;
986 if ((val
& PORTSC_PRESET
) && !(*portsc
& PORTSC_PRESET
)) {
987 trace_usb_ehci_port_reset(port
, 1);
990 if (!(val
& PORTSC_PRESET
) &&(*portsc
& PORTSC_PRESET
)) {
991 trace_usb_ehci_port_reset(port
, 0);
992 if (dev
&& dev
->attached
) {
993 usb_port_reset(&s
->ports
[port
]);
994 *portsc
&= ~PORTSC_CSC
;
998 * Table 2.16 Set the enable bit(and enable bit change) to indicate
999 * to SW that this port has a high speed device attached
1001 if (dev
&& dev
->attached
&& (dev
->speedmask
& USB_SPEED_MASK_HIGH
)) {
1006 if ((val
& PORTSC_SUSPEND
) && !(*portsc
& PORTSC_SUSPEND
)) {
1007 trace_usb_ehci_port_suspend(port
);
1009 if (!(val
& PORTSC_FPRES
) && (*portsc
& PORTSC_FPRES
)) {
1010 trace_usb_ehci_port_resume(port
);
1011 val
&= ~PORTSC_SUSPEND
;
1014 *portsc
&= ~PORTSC_RO_MASK
;
1016 trace_usb_ehci_portsc_change(addr
+ s
->portscbase
, addr
>> 2, *portsc
, old
);
1019 static void ehci_opreg_write(void *ptr
, hwaddr addr
,
1020 uint64_t val
, unsigned size
)
1023 uint32_t *mmio
= s
->opreg
+ (addr
>> 2);
1024 uint32_t old
= *mmio
;
1027 trace_usb_ehci_opreg_write(addr
+ s
->opregbase
, addr2str(addr
), val
);
1031 if (val
& USBCMD_HCRESET
) {
1037 /* not supporting dynamic frame list size at the moment */
1038 if ((val
& USBCMD_FLS
) && !(s
->usbcmd
& USBCMD_FLS
)) {
1039 fprintf(stderr
, "attempt to set frame list size -- value %d\n",
1040 (int)val
& USBCMD_FLS
);
1044 if (val
& USBCMD_IAAD
) {
1046 * Process IAAD immediately, otherwise the Linux IAAD watchdog may
1047 * trigger and re-use a qh without us seeing the unlink.
1049 s
->async_stepdown
= 0;
1050 qemu_bh_schedule(s
->async_bh
);
1051 trace_usb_ehci_doorbell_ring();
1054 if (((USBCMD_RUNSTOP
| USBCMD_PSE
| USBCMD_ASE
) & val
) !=
1055 ((USBCMD_RUNSTOP
| USBCMD_PSE
| USBCMD_ASE
) & s
->usbcmd
)) {
1056 if (s
->pstate
== EST_INACTIVE
) {
1057 SET_LAST_RUN_CLOCK(s
);
1059 s
->usbcmd
= val
; /* Set usbcmd for ehci_update_halt() */
1060 ehci_update_halt(s
);
1061 s
->async_stepdown
= 0;
1062 qemu_bh_schedule(s
->async_bh
);
1067 val
&= USBSTS_RO_MASK
; // bits 6 through 31 are RO
1068 ehci_clear_usbsts(s
, val
); // bits 0 through 5 are R/WC
1074 val
&= USBINTR_MASK
;
1075 if (ehci_enabled(s
) && (USBSTS_FLR
& val
)) {
1076 qemu_bh_schedule(s
->async_bh
);
1081 val
&= 0x00003fff; /* frindex is 14bits */
1082 s
->usbsts_frindex
= val
;
1088 for(i
= 0; i
< NB_PORTS
; i
++)
1089 handle_port_owner_write(s
, i
, 0);
1093 case PERIODICLISTBASE
:
1094 if (ehci_periodic_enabled(s
)) {
1096 "ehci: PERIODIC list base register set while periodic schedule\n"
1097 " is enabled and HC is enabled\n");
1102 if (ehci_async_enabled(s
)) {
1104 "ehci: ASYNC list address register set while async schedule\n"
1105 " is enabled and HC is enabled\n");
1111 trace_usb_ehci_opreg_change(addr
+ s
->opregbase
, addr2str(addr
),
1116 * Write the qh back to guest physical memory. This step isn't
1117 * in the EHCI spec but we need to do it since we don't share
1118 * physical memory with our guest VM.
1120 * The first three dwords are read-only for the EHCI, so skip them
1121 * when writing back the qh.
1123 static void ehci_flush_qh(EHCIQueue
*q
)
1125 uint32_t *qh
= (uint32_t *) &q
->qh
;
1126 uint32_t dwords
= sizeof(EHCIqh
) >> 2;
1127 uint32_t addr
= NLPTR_GET(q
->qhaddr
);
1129 put_dwords(q
->ehci
, addr
+ 3 * sizeof(uint32_t), qh
+ 3, dwords
- 3);
1134 static int ehci_qh_do_overlay(EHCIQueue
*q
)
1136 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1144 assert(p
->qtdaddr
== q
->qtdaddr
);
1146 // remember values in fields to preserve in qh after overlay
1148 dtoggle
= q
->qh
.token
& QTD_TOKEN_DTOGGLE
;
1149 ping
= q
->qh
.token
& QTD_TOKEN_PING
;
1151 q
->qh
.current_qtd
= p
->qtdaddr
;
1152 q
->qh
.next_qtd
= p
->qtd
.next
;
1153 q
->qh
.altnext_qtd
= p
->qtd
.altnext
;
1154 q
->qh
.token
= p
->qtd
.token
;
1157 eps
= get_field(q
->qh
.epchar
, QH_EPCHAR_EPS
);
1158 if (eps
== EHCI_QH_EPS_HIGH
) {
1159 q
->qh
.token
&= ~QTD_TOKEN_PING
;
1160 q
->qh
.token
|= ping
;
1163 reload
= get_field(q
->qh
.epchar
, QH_EPCHAR_RL
);
1164 set_field(&q
->qh
.altnext_qtd
, reload
, QH_ALTNEXT_NAKCNT
);
1166 for (i
= 0; i
< 5; i
++) {
1167 q
->qh
.bufptr
[i
] = p
->qtd
.bufptr
[i
];
1170 if (!(q
->qh
.epchar
& QH_EPCHAR_DTC
)) {
1171 // preserve QH DT bit
1172 q
->qh
.token
&= ~QTD_TOKEN_DTOGGLE
;
1173 q
->qh
.token
|= dtoggle
;
1176 q
->qh
.bufptr
[1] &= ~BUFPTR_CPROGMASK_MASK
;
1177 q
->qh
.bufptr
[2] &= ~BUFPTR_FRAMETAG_MASK
;
1184 static int ehci_init_transfer(EHCIPacket
*p
)
1186 uint32_t cpage
, offset
, bytes
, plen
;
1189 cpage
= get_field(p
->qtd
.token
, QTD_TOKEN_CPAGE
);
1190 bytes
= get_field(p
->qtd
.token
, QTD_TOKEN_TBYTES
);
1191 offset
= p
->qtd
.bufptr
[0] & ~QTD_BUFPTR_MASK
;
1192 qemu_sglist_init(&p
->sgl
, p
->queue
->ehci
->device
, 5, p
->queue
->ehci
->as
);
1196 fprintf(stderr
, "cpage out of range (%d)\n", cpage
);
1197 qemu_sglist_destroy(&p
->sgl
);
1201 page
= p
->qtd
.bufptr
[cpage
] & QTD_BUFPTR_MASK
;
1204 if (plen
> 4096 - offset
) {
1205 plen
= 4096 - offset
;
1210 qemu_sglist_add(&p
->sgl
, page
, plen
);
1216 static void ehci_finish_transfer(EHCIQueue
*q
, int len
)
1218 uint32_t cpage
, offset
;
1221 /* update cpage & offset */
1222 cpage
= get_field(q
->qh
.token
, QTD_TOKEN_CPAGE
);
1223 offset
= q
->qh
.bufptr
[0] & ~QTD_BUFPTR_MASK
;
1226 cpage
+= offset
>> QTD_BUFPTR_SH
;
1227 offset
&= ~QTD_BUFPTR_MASK
;
1229 set_field(&q
->qh
.token
, cpage
, QTD_TOKEN_CPAGE
);
1230 q
->qh
.bufptr
[0] &= QTD_BUFPTR_MASK
;
1231 q
->qh
.bufptr
[0] |= offset
;
1235 static void ehci_async_complete_packet(USBPort
*port
, USBPacket
*packet
)
1238 EHCIState
*s
= port
->opaque
;
1239 uint32_t portsc
= s
->portsc
[port
->index
];
1241 if (portsc
& PORTSC_POWNER
) {
1242 USBPort
*companion
= s
->companion_ports
[port
->index
];
1243 companion
->ops
->complete(companion
, packet
);
1247 p
= container_of(packet
, EHCIPacket
, packet
);
1248 assert(p
->async
== EHCI_ASYNC_INFLIGHT
);
1250 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
1251 trace_usb_ehci_packet_action(p
->queue
, p
, "remove");
1252 ehci_free_packet(p
);
1256 trace_usb_ehci_packet_action(p
->queue
, p
, "wakeup");
1257 p
->async
= EHCI_ASYNC_FINISHED
;
1259 if (!p
->queue
->async
) {
1260 s
->periodic_sched_active
= PERIODIC_ACTIVE
;
1262 qemu_bh_schedule(s
->async_bh
);
1265 static void ehci_execute_complete(EHCIQueue
*q
)
1267 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1271 assert(p
->qtdaddr
== q
->qtdaddr
);
1272 assert(p
->async
== EHCI_ASYNC_INITIALIZED
||
1273 p
->async
== EHCI_ASYNC_FINISHED
);
1275 DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, "
1276 "status %d, actual_length %d\n",
1277 q
->qhaddr
, q
->qh
.next
, q
->qtdaddr
,
1278 p
->packet
.status
, p
->packet
.actual_length
);
1280 switch (p
->packet
.status
) {
1281 case USB_RET_SUCCESS
:
1283 case USB_RET_IOERROR
:
1285 q
->qh
.token
|= (QTD_TOKEN_HALT
| QTD_TOKEN_XACTERR
);
1286 set_field(&q
->qh
.token
, 0, QTD_TOKEN_CERR
);
1287 ehci_raise_irq(q
->ehci
, USBSTS_ERRINT
);
1290 q
->qh
.token
|= QTD_TOKEN_HALT
;
1291 ehci_raise_irq(q
->ehci
, USBSTS_ERRINT
);
1294 set_field(&q
->qh
.altnext_qtd
, 0, QH_ALTNEXT_NAKCNT
);
1295 return; /* We're not done yet with this transaction */
1296 case USB_RET_BABBLE
:
1297 q
->qh
.token
|= (QTD_TOKEN_HALT
| QTD_TOKEN_BABBLE
);
1298 ehci_raise_irq(q
->ehci
, USBSTS_ERRINT
);
1301 /* should not be triggerable */
1302 fprintf(stderr
, "USB invalid response %d\n", p
->packet
.status
);
1303 g_assert_not_reached();
1307 /* TODO check 4.12 for splits */
1308 tbytes
= get_field(q
->qh
.token
, QTD_TOKEN_TBYTES
);
1309 if (tbytes
&& p
->pid
== USB_TOKEN_IN
) {
1310 tbytes
-= p
->packet
.actual_length
;
1312 /* 4.15.1.2 must raise int on a short input packet */
1313 ehci_raise_irq(q
->ehci
, USBSTS_INT
);
1315 q
->ehci
->int_req_by_async
= true;
1321 DPRINTF("updating tbytes to %d\n", tbytes
);
1322 set_field(&q
->qh
.token
, tbytes
, QTD_TOKEN_TBYTES
);
1324 ehci_finish_transfer(q
, p
->packet
.actual_length
);
1325 usb_packet_unmap(&p
->packet
, &p
->sgl
);
1326 qemu_sglist_destroy(&p
->sgl
);
1327 p
->async
= EHCI_ASYNC_NONE
;
1329 q
->qh
.token
^= QTD_TOKEN_DTOGGLE
;
1330 q
->qh
.token
&= ~QTD_TOKEN_ACTIVE
;
1332 if (q
->qh
.token
& QTD_TOKEN_IOC
) {
1333 ehci_raise_irq(q
->ehci
, USBSTS_INT
);
1335 q
->ehci
->int_req_by_async
= true;
1340 /* 4.10.3 returns "again" */
1341 static int ehci_execute(EHCIPacket
*p
, const char *action
)
1347 assert(p
->async
== EHCI_ASYNC_NONE
||
1348 p
->async
== EHCI_ASYNC_INITIALIZED
);
1350 if (!(p
->qtd
.token
& QTD_TOKEN_ACTIVE
)) {
1351 fprintf(stderr
, "Attempting to execute inactive qtd\n");
1355 if (get_field(p
->qtd
.token
, QTD_TOKEN_TBYTES
) > BUFF_SIZE
) {
1356 ehci_trace_guest_bug(p
->queue
->ehci
,
1357 "guest requested more bytes than allowed");
1361 if (!ehci_verify_pid(p
->queue
, &p
->qtd
)) {
1362 ehci_queue_stopped(p
->queue
); /* Mark the ep in the prev dir stopped */
1364 p
->pid
= ehci_get_pid(&p
->qtd
);
1365 p
->queue
->last_pid
= p
->pid
;
1366 endp
= get_field(p
->queue
->qh
.epchar
, QH_EPCHAR_EP
);
1367 ep
= usb_ep_get(p
->queue
->dev
, p
->pid
, endp
);
1369 if (p
->async
== EHCI_ASYNC_NONE
) {
1370 if (ehci_init_transfer(p
) != 0) {
1374 spd
= (p
->pid
== USB_TOKEN_IN
&& NLPTR_TBIT(p
->qtd
.altnext
) == 0);
1375 usb_packet_setup(&p
->packet
, p
->pid
, ep
, 0, p
->qtdaddr
, spd
,
1376 (p
->qtd
.token
& QTD_TOKEN_IOC
) != 0);
1377 usb_packet_map(&p
->packet
, &p
->sgl
);
1378 p
->async
= EHCI_ASYNC_INITIALIZED
;
1381 trace_usb_ehci_packet_action(p
->queue
, p
, action
);
1382 usb_handle_packet(p
->queue
->dev
, &p
->packet
);
1383 DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x "
1384 "status %d actual_length %d\n", p
->queue
->qhaddr
, p
->qtd
.next
,
1385 p
->qtdaddr
, p
->pid
, p
->packet
.iov
.size
, endp
, p
->packet
.status
,
1386 p
->packet
.actual_length
);
1388 if (p
->packet
.actual_length
> BUFF_SIZE
) {
1389 fprintf(stderr
, "ret from usb_handle_packet > BUFF_SIZE\n");
1399 static int ehci_process_itd(EHCIState
*ehci
,
1405 uint32_t i
, len
, pid
, dir
, devaddr
, endp
;
1406 uint32_t pg
, off
, ptr1
, ptr2
, max
, mult
;
1408 ehci
->periodic_sched_active
= PERIODIC_ACTIVE
;
1410 dir
=(itd
->bufptr
[1] & ITD_BUFPTR_DIRECTION
);
1411 devaddr
= get_field(itd
->bufptr
[0], ITD_BUFPTR_DEVADDR
);
1412 endp
= get_field(itd
->bufptr
[0], ITD_BUFPTR_EP
);
1413 max
= get_field(itd
->bufptr
[1], ITD_BUFPTR_MAXPKT
);
1414 mult
= get_field(itd
->bufptr
[2], ITD_BUFPTR_MULT
);
1416 for(i
= 0; i
< 8; i
++) {
1417 if (itd
->transact
[i
] & ITD_XACT_ACTIVE
) {
1418 pg
= get_field(itd
->transact
[i
], ITD_XACT_PGSEL
);
1419 off
= itd
->transact
[i
] & ITD_XACT_OFFSET_MASK
;
1420 len
= get_field(itd
->transact
[i
], ITD_XACT_LENGTH
);
1422 if (len
> max
* mult
) {
1425 if (len
> BUFF_SIZE
|| pg
> 6) {
1429 ptr1
= (itd
->bufptr
[pg
] & ITD_BUFPTR_MASK
);
1430 qemu_sglist_init(&ehci
->isgl
, ehci
->device
, 2, ehci
->as
);
1431 if (off
+ len
> 4096) {
1432 /* transfer crosses page border */
1434 qemu_sglist_destroy(&ehci
->isgl
);
1435 return -1; /* avoid page pg + 1 */
1437 ptr2
= (itd
->bufptr
[pg
+ 1] & ITD_BUFPTR_MASK
);
1438 uint32_t len2
= off
+ len
- 4096;
1439 uint32_t len1
= len
- len2
;
1440 qemu_sglist_add(&ehci
->isgl
, ptr1
+ off
, len1
);
1441 qemu_sglist_add(&ehci
->isgl
, ptr2
, len2
);
1443 qemu_sglist_add(&ehci
->isgl
, ptr1
+ off
, len
);
1446 dev
= ehci_find_device(ehci
, devaddr
);
1448 ehci_trace_guest_bug(ehci
, "no device found");
1451 pid
= dir
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1452 ep
= usb_ep_get(dev
, pid
, endp
);
1453 if (ep
&& ep
->type
== USB_ENDPOINT_XFER_ISOC
) {
1454 usb_packet_setup(&ehci
->ipacket
, pid
, ep
, 0, addr
, false,
1455 (itd
->transact
[i
] & ITD_XACT_IOC
) != 0);
1456 usb_packet_map(&ehci
->ipacket
, &ehci
->isgl
);
1457 usb_handle_packet(dev
, &ehci
->ipacket
);
1458 usb_packet_unmap(&ehci
->ipacket
, &ehci
->isgl
);
1460 DPRINTF("ISOCH: attempt to addess non-iso endpoint\n");
1461 ehci
->ipacket
.status
= USB_RET_NAK
;
1462 ehci
->ipacket
.actual_length
= 0;
1464 qemu_sglist_destroy(&ehci
->isgl
);
1466 switch (ehci
->ipacket
.status
) {
1467 case USB_RET_SUCCESS
:
1470 fprintf(stderr
, "Unexpected iso usb result: %d\n",
1471 ehci
->ipacket
.status
);
1473 case USB_RET_IOERROR
:
1475 /* 3.3.2: XACTERR is only allowed on IN transactions */
1477 itd
->transact
[i
] |= ITD_XACT_XACTERR
;
1478 ehci_raise_irq(ehci
, USBSTS_ERRINT
);
1481 case USB_RET_BABBLE
:
1482 itd
->transact
[i
] |= ITD_XACT_BABBLE
;
1483 ehci_raise_irq(ehci
, USBSTS_ERRINT
);
1486 /* no data for us, so do a zero-length transfer */
1487 ehci
->ipacket
.actual_length
= 0;
1491 set_field(&itd
->transact
[i
], len
- ehci
->ipacket
.actual_length
,
1492 ITD_XACT_LENGTH
); /* OUT */
1494 set_field(&itd
->transact
[i
], ehci
->ipacket
.actual_length
,
1495 ITD_XACT_LENGTH
); /* IN */
1497 if (itd
->transact
[i
] & ITD_XACT_IOC
) {
1498 ehci_raise_irq(ehci
, USBSTS_INT
);
1500 itd
->transact
[i
] &= ~ITD_XACT_ACTIVE
;
1507 /* This state is the entry point for asynchronous schedule
1508 * processing. Entry here consitutes a EHCI start event state (4.8.5)
1510 static int ehci_state_waitlisthead(EHCIState
*ehci
, int async
)
1515 uint32_t entry
= ehci
->asynclistaddr
;
1517 /* set reclamation flag at start event (4.8.6) */
1519 ehci_set_usbsts(ehci
, USBSTS_REC
);
1522 ehci_queues_rip_unused(ehci
, async
);
1524 /* Find the head of the list (4.9.1.1) */
1525 for(i
= 0; i
< MAX_QH
; i
++) {
1526 if (get_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *) &qh
,
1527 sizeof(EHCIqh
) >> 2) < 0) {
1530 ehci_trace_qh(NULL
, NLPTR_GET(entry
), &qh
);
1532 if (qh
.epchar
& QH_EPCHAR_H
) {
1534 entry
|= (NLPTR_TYPE_QH
<< 1);
1537 ehci_set_fetch_addr(ehci
, async
, entry
);
1538 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
1544 if (entry
== ehci
->asynclistaddr
) {
1549 /* no head found for list. */
1551 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1558 /* This state is the entry point for periodic schedule processing as
1559 * well as being a continuation state for async processing.
1561 static int ehci_state_fetchentry(EHCIState
*ehci
, int async
)
1564 uint32_t entry
= ehci_get_fetch_addr(ehci
, async
);
1566 if (NLPTR_TBIT(entry
)) {
1567 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1571 /* section 4.8, only QH in async schedule */
1572 if (async
&& (NLPTR_TYPE_GET(entry
) != NLPTR_TYPE_QH
)) {
1573 fprintf(stderr
, "non queue head request in async schedule\n");
1577 switch (NLPTR_TYPE_GET(entry
)) {
1579 ehci_set_state(ehci
, async
, EST_FETCHQH
);
1583 case NLPTR_TYPE_ITD
:
1584 ehci_set_state(ehci
, async
, EST_FETCHITD
);
1588 case NLPTR_TYPE_STITD
:
1589 ehci_set_state(ehci
, async
, EST_FETCHSITD
);
1594 /* TODO: handle FSTN type */
1595 fprintf(stderr
, "FETCHENTRY: entry at %X is of type %d "
1596 "which is not supported yet\n", entry
, NLPTR_TYPE_GET(entry
));
1604 static EHCIQueue
*ehci_state_fetchqh(EHCIState
*ehci
, int async
)
1610 entry
= ehci_get_fetch_addr(ehci
, async
);
1611 q
= ehci_find_queue_by_qh(ehci
, entry
, async
);
1613 q
= ehci_alloc_queue(ehci
, entry
, async
);
1618 /* we are going in circles -- stop processing */
1619 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1624 if (get_dwords(ehci
, NLPTR_GET(q
->qhaddr
),
1625 (uint32_t *) &qh
, sizeof(EHCIqh
) >> 2) < 0) {
1629 ehci_trace_qh(q
, NLPTR_GET(q
->qhaddr
), &qh
);
1632 * The overlay area of the qh should never be changed by the guest,
1633 * except when idle, in which case the reset is a nop.
1635 if (!ehci_verify_qh(q
, &qh
)) {
1636 if (ehci_reset_queue(q
) > 0) {
1637 ehci_trace_guest_bug(ehci
, "guest updated active QH");
1642 q
->transact_ctr
= get_field(q
->qh
.epcap
, QH_EPCAP_MULT
);
1643 if (q
->transact_ctr
== 0) { /* Guest bug in some versions of windows */
1644 q
->transact_ctr
= 4;
1647 if (q
->dev
== NULL
) {
1648 q
->dev
= ehci_find_device(q
->ehci
,
1649 get_field(q
->qh
.epchar
, QH_EPCHAR_DEVADDR
));
1652 if (async
&& (q
->qh
.epchar
& QH_EPCHAR_H
)) {
1654 /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
1655 if (ehci
->usbsts
& USBSTS_REC
) {
1656 ehci_clear_usbsts(ehci
, USBSTS_REC
);
1658 DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
1659 " - done processing\n", q
->qhaddr
);
1660 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1667 if (q
->qhaddr
!= q
->qh
.next
) {
1668 DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
1670 q
->qh
.epchar
& QH_EPCHAR_H
,
1671 q
->qh
.token
& QTD_TOKEN_HALT
,
1672 q
->qh
.token
& QTD_TOKEN_ACTIVE
,
1677 if (q
->qh
.token
& QTD_TOKEN_HALT
) {
1678 ehci_set_state(ehci
, async
, EST_HORIZONTALQH
);
1680 } else if ((q
->qh
.token
& QTD_TOKEN_ACTIVE
) &&
1681 (NLPTR_TBIT(q
->qh
.current_qtd
) == 0) &&
1682 (q
->qh
.current_qtd
!= 0)) {
1683 q
->qtdaddr
= q
->qh
.current_qtd
;
1684 ehci_set_state(ehci
, async
, EST_FETCHQTD
);
1687 /* EHCI spec version 1.0 Section 4.10.2 */
1688 ehci_set_state(ehci
, async
, EST_ADVANCEQUEUE
);
1695 static int ehci_state_fetchitd(EHCIState
*ehci
, int async
)
1701 entry
= ehci_get_fetch_addr(ehci
, async
);
1703 if (get_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *) &itd
,
1704 sizeof(EHCIitd
) >> 2) < 0) {
1707 ehci_trace_itd(ehci
, entry
, &itd
);
1709 if (ehci_process_itd(ehci
, &itd
, entry
) != 0) {
1713 put_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *) &itd
,
1714 sizeof(EHCIitd
) >> 2);
1715 ehci_set_fetch_addr(ehci
, async
, itd
.next
);
1716 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
1721 static int ehci_state_fetchsitd(EHCIState
*ehci
, int async
)
1727 entry
= ehci_get_fetch_addr(ehci
, async
);
1729 if (get_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *)&sitd
,
1730 sizeof(EHCIsitd
) >> 2) < 0) {
1733 ehci_trace_sitd(ehci
, entry
, &sitd
);
1735 if (!(sitd
.results
& SITD_RESULTS_ACTIVE
)) {
1736 /* siTD is not active, nothing to do */;
1738 /* TODO: split transfers are not implemented */
1739 warn_report("Skipping active siTD");
1742 ehci_set_fetch_addr(ehci
, async
, sitd
.next
);
1743 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
1747 /* Section 4.10.2 - paragraph 3 */
1748 static int ehci_state_advqueue(EHCIQueue
*q
)
1751 /* TO-DO: 4.10.2 - paragraph 2
1752 * if I-bit is set to 1 and QH is not active
1753 * go to horizontal QH
1756 ehci_set_state(ehci
, async
, EST_HORIZONTALQH
);
1762 * want data and alt-next qTD is valid
1764 if (((q
->qh
.token
& QTD_TOKEN_TBYTES_MASK
) != 0) &&
1765 (NLPTR_TBIT(q
->qh
.altnext_qtd
) == 0)) {
1766 q
->qtdaddr
= q
->qh
.altnext_qtd
;
1767 ehci_set_state(q
->ehci
, q
->async
, EST_FETCHQTD
);
1772 } else if (NLPTR_TBIT(q
->qh
.next_qtd
) == 0) {
1773 q
->qtdaddr
= q
->qh
.next_qtd
;
1774 ehci_set_state(q
->ehci
, q
->async
, EST_FETCHQTD
);
1777 * no valid qTD, try next QH
1780 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1786 /* Section 4.10.2 - paragraph 4 */
1787 static int ehci_state_fetchqtd(EHCIQueue
*q
)
1794 addr
= NLPTR_GET(q
->qtdaddr
);
1795 if (get_dwords(q
->ehci
, addr
+ 8, &qtd
.token
, 1) < 0) {
1799 if (get_dwords(q
->ehci
, addr
+ 0, &qtd
.next
, 1) < 0 ||
1800 get_dwords(q
->ehci
, addr
+ 4, &qtd
.altnext
, 1) < 0 ||
1801 get_dwords(q
->ehci
, addr
+ 12, qtd
.bufptr
,
1802 ARRAY_SIZE(qtd
.bufptr
)) < 0) {
1805 ehci_trace_qtd(q
, NLPTR_GET(q
->qtdaddr
), &qtd
);
1807 p
= QTAILQ_FIRST(&q
->packets
);
1809 if (!ehci_verify_qtd(p
, &qtd
)) {
1810 ehci_cancel_queue(q
);
1811 if (qtd
.token
& QTD_TOKEN_ACTIVE
) {
1812 ehci_trace_guest_bug(q
->ehci
, "guest updated active qTD");
1817 ehci_qh_do_overlay(q
);
1821 if (!(qtd
.token
& QTD_TOKEN_ACTIVE
)) {
1822 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1823 } else if (p
!= NULL
) {
1825 case EHCI_ASYNC_NONE
:
1826 case EHCI_ASYNC_INITIALIZED
:
1827 /* Not yet executed (MULT), or previously nacked (int) packet */
1828 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTE
);
1830 case EHCI_ASYNC_INFLIGHT
:
1831 /* Check if the guest has added new tds to the queue */
1832 again
= ehci_fill_queue(QTAILQ_LAST(&q
->packets
));
1833 /* Unfinished async handled packet, go horizontal */
1834 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1836 case EHCI_ASYNC_FINISHED
:
1837 /* Complete executing of the packet */
1838 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTING
);
1841 } else if (q
->dev
== NULL
) {
1842 ehci_trace_guest_bug(q
->ehci
, "no device attached to queue");
1843 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1845 p
= ehci_alloc_packet(q
);
1846 p
->qtdaddr
= q
->qtdaddr
;
1848 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTE
);
1854 static int ehci_state_horizqh(EHCIQueue
*q
)
1858 if (ehci_get_fetch_addr(q
->ehci
, q
->async
) != q
->qh
.next
) {
1859 ehci_set_fetch_addr(q
->ehci
, q
->async
, q
->qh
.next
);
1860 ehci_set_state(q
->ehci
, q
->async
, EST_FETCHENTRY
);
1863 ehci_set_state(q
->ehci
, q
->async
, EST_ACTIVE
);
1869 /* Returns "again" */
1870 static int ehci_fill_queue(EHCIPacket
*p
)
1872 USBEndpoint
*ep
= p
->packet
.ep
;
1873 EHCIQueue
*q
= p
->queue
;
1874 EHCIqtd qtd
= p
->qtd
;
1878 if (NLPTR_TBIT(qtd
.next
) != 0) {
1883 * Detect circular td lists, Windows creates these, counting on the
1884 * active bit going low after execution to make the queue stop.
1886 QTAILQ_FOREACH(p
, &q
->packets
, next
) {
1887 if (p
->qtdaddr
== qtdaddr
) {
1891 if (get_dwords(q
->ehci
, NLPTR_GET(qtdaddr
),
1892 (uint32_t *) &qtd
, sizeof(EHCIqtd
) >> 2) < 0) {
1895 ehci_trace_qtd(q
, NLPTR_GET(qtdaddr
), &qtd
);
1896 if (!(qtd
.token
& QTD_TOKEN_ACTIVE
)) {
1899 if (!ehci_verify_pid(q
, &qtd
)) {
1900 ehci_trace_guest_bug(q
->ehci
, "guest queued token with wrong pid");
1903 p
= ehci_alloc_packet(q
);
1904 p
->qtdaddr
= qtdaddr
;
1906 if (ehci_execute(p
, "queue") == -1) {
1909 assert(p
->packet
.status
== USB_RET_ASYNC
);
1910 p
->async
= EHCI_ASYNC_INFLIGHT
;
1913 usb_device_flush_ep_queue(ep
->dev
, ep
);
1917 static int ehci_state_execute(EHCIQueue
*q
)
1919 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1923 assert(p
->qtdaddr
== q
->qtdaddr
);
1925 if (ehci_qh_do_overlay(q
) != 0) {
1929 // TODO verify enough time remains in the uframe as in 4.4.1.1
1930 // TODO write back ptr to async list when done or out of time
1932 /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */
1933 if (!q
->async
&& q
->transact_ctr
== 0) {
1934 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1940 ehci_set_usbsts(q
->ehci
, USBSTS_REC
);
1943 again
= ehci_execute(p
, "process");
1947 if (p
->packet
.status
== USB_RET_ASYNC
) {
1949 trace_usb_ehci_packet_action(p
->queue
, p
, "async");
1950 p
->async
= EHCI_ASYNC_INFLIGHT
;
1951 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1953 again
= ehci_fill_queue(p
);
1960 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTING
);
1967 static int ehci_state_executing(EHCIQueue
*q
)
1969 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1972 assert(p
->qtdaddr
== q
->qtdaddr
);
1974 ehci_execute_complete(q
);
1977 if (!q
->async
&& q
->transact_ctr
> 0) {
1982 if (p
->packet
.status
== USB_RET_NAK
) {
1983 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1985 ehci_set_state(q
->ehci
, q
->async
, EST_WRITEBACK
);
1993 static int ehci_state_writeback(EHCIQueue
*q
)
1995 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1996 uint32_t *qtd
, addr
;
1999 /* Write back the QTD from the QH area */
2001 assert(p
->qtdaddr
== q
->qtdaddr
);
2003 ehci_trace_qtd(q
, NLPTR_GET(p
->qtdaddr
), (EHCIqtd
*) &q
->qh
.next_qtd
);
2004 qtd
= (uint32_t *) &q
->qh
.next_qtd
;
2005 addr
= NLPTR_GET(p
->qtdaddr
);
2006 put_dwords(q
->ehci
, addr
+ 2 * sizeof(uint32_t), qtd
+ 2, 2);
2007 ehci_free_packet(p
);
2010 * EHCI specs say go horizontal here.
2012 * We can also advance the queue here for performance reasons. We
2013 * need to take care to only take that shortcut in case we've
2014 * processed the qtd just written back without errors, i.e. halt
2017 if (q
->qh
.token
& QTD_TOKEN_HALT
) {
2018 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
2021 ehci_set_state(q
->ehci
, q
->async
, EST_ADVANCEQUEUE
);
2028 * This is the state machine that is common to both async and periodic
2031 static void ehci_advance_state(EHCIState
*ehci
, int async
)
2033 EHCIQueue
*q
= NULL
;
2038 switch(ehci_get_state(ehci
, async
)) {
2039 case EST_WAITLISTHEAD
:
2040 again
= ehci_state_waitlisthead(ehci
, async
);
2043 case EST_FETCHENTRY
:
2044 again
= ehci_state_fetchentry(ehci
, async
);
2048 q
= ehci_state_fetchqh(ehci
, async
);
2050 assert(q
->async
== async
);
2058 again
= ehci_state_fetchitd(ehci
, async
);
2063 again
= ehci_state_fetchsitd(ehci
, async
);
2067 case EST_ADVANCEQUEUE
:
2069 again
= ehci_state_advqueue(q
);
2074 again
= ehci_state_fetchqtd(q
);
2077 case EST_HORIZONTALQH
:
2079 again
= ehci_state_horizqh(q
);
2084 again
= ehci_state_execute(q
);
2086 ehci
->async_stepdown
= 0;
2093 ehci
->async_stepdown
= 0;
2095 again
= ehci_state_executing(q
);
2100 again
= ehci_state_writeback(q
);
2102 ehci
->periodic_sched_active
= PERIODIC_ACTIVE
;
2107 fprintf(stderr
, "Bad state!\n");
2109 g_assert_not_reached();
2113 if (again
< 0 || itd_count
> 16) {
2114 /* TODO: notify guest (raise HSE irq?) */
2115 fprintf(stderr
, "processing error - resetting ehci HC\n");
2123 static void ehci_advance_async_state(EHCIState
*ehci
)
2125 const int async
= 1;
2127 switch(ehci_get_state(ehci
, async
)) {
2129 if (!ehci_async_enabled(ehci
)) {
2132 ehci_set_state(ehci
, async
, EST_ACTIVE
);
2133 // No break, fall through to ACTIVE
2136 if (!ehci_async_enabled(ehci
)) {
2137 ehci_queues_rip_all(ehci
, async
);
2138 ehci_set_state(ehci
, async
, EST_INACTIVE
);
2142 /* make sure guest has acknowledged the doorbell interrupt */
2143 /* TO-DO: is this really needed? */
2144 if (ehci
->usbsts
& USBSTS_IAA
) {
2145 DPRINTF("IAA status bit still set.\n");
2149 /* check that address register has been set */
2150 if (ehci
->asynclistaddr
== 0) {
2154 ehci_set_state(ehci
, async
, EST_WAITLISTHEAD
);
2155 ehci_advance_state(ehci
, async
);
2157 /* If the doorbell is set, the guest wants to make a change to the
2158 * schedule. The host controller needs to release cached data.
2161 if (ehci
->usbcmd
& USBCMD_IAAD
) {
2162 /* Remove all unseen qhs from the async qhs queue */
2163 ehci_queues_rip_unseen(ehci
, async
);
2164 trace_usb_ehci_doorbell_ack();
2165 ehci
->usbcmd
&= ~USBCMD_IAAD
;
2166 ehci_raise_irq(ehci
, USBSTS_IAA
);
2171 /* this should only be due to a developer mistake */
2172 fprintf(stderr
, "ehci: Bad asynchronous state %d. "
2173 "Resetting to active\n", ehci
->astate
);
2174 g_assert_not_reached();
2178 static void ehci_advance_periodic_state(EHCIState
*ehci
)
2182 const int async
= 0;
2186 switch(ehci_get_state(ehci
, async
)) {
2188 if (!(ehci
->frindex
& 7) && ehci_periodic_enabled(ehci
)) {
2189 ehci_set_state(ehci
, async
, EST_ACTIVE
);
2190 // No break, fall through to ACTIVE
2195 if (!(ehci
->frindex
& 7) && !ehci_periodic_enabled(ehci
)) {
2196 ehci_queues_rip_all(ehci
, async
);
2197 ehci_set_state(ehci
, async
, EST_INACTIVE
);
2201 list
= ehci
->periodiclistbase
& 0xfffff000;
2202 /* check that register has been set */
2206 list
|= ((ehci
->frindex
& 0x1ff8) >> 1);
2208 if (get_dwords(ehci
, list
, &entry
, 1) < 0) {
2212 DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
2213 ehci
->frindex
/ 8, list
, entry
);
2214 ehci_set_fetch_addr(ehci
, async
,entry
);
2215 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
2216 ehci_advance_state(ehci
, async
);
2217 ehci_queues_rip_unused(ehci
, async
);
2221 /* this should only be due to a developer mistake */
2222 fprintf(stderr
, "ehci: Bad periodic state %d. "
2223 "Resetting to active\n", ehci
->pstate
);
2224 g_assert_not_reached();
2228 static void ehci_update_frindex(EHCIState
*ehci
, int uframes
)
2230 if (!ehci_enabled(ehci
) && ehci
->pstate
== EST_INACTIVE
) {
2234 /* Generate FLR interrupt if frame index rolls over 0x2000 */
2235 if ((ehci
->frindex
% 0x2000) + uframes
>= 0x2000) {
2236 ehci_raise_irq(ehci
, USBSTS_FLR
);
2239 /* How many times will frindex roll over 0x4000 with this frame count?
2240 * usbsts_frindex is decremented by 0x4000 on rollover until it reaches 0
2242 int rollovers
= (ehci
->frindex
+ uframes
) / 0x4000;
2243 if (rollovers
> 0) {
2244 if (ehci
->usbsts_frindex
>= (rollovers
* 0x4000)) {
2245 ehci
->usbsts_frindex
-= 0x4000 * rollovers
;
2247 ehci
->usbsts_frindex
= 0;
2251 ehci
->frindex
= (ehci
->frindex
+ uframes
) % 0x4000;
2254 static void ehci_work_bh(void *opaque
)
2256 EHCIState
*ehci
= opaque
;
2258 int64_t expire_time
, t_now
;
2259 uint64_t ns_elapsed
;
2260 uint64_t uframes
, skipped_uframes
;
2263 if (ehci
->working
) {
2266 ehci
->working
= true;
2268 t_now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2269 ns_elapsed
= t_now
- ehci
->last_run_ns
;
2270 uframes
= ns_elapsed
/ UFRAME_TIMER_NS
;
2272 if (ehci_periodic_enabled(ehci
) || ehci
->pstate
!= EST_INACTIVE
) {
2275 if (uframes
> (ehci
->maxframes
* 8)) {
2276 skipped_uframes
= uframes
- (ehci
->maxframes
* 8);
2277 ehci_update_frindex(ehci
, skipped_uframes
);
2278 ehci
->last_run_ns
+= UFRAME_TIMER_NS
* skipped_uframes
;
2279 uframes
-= skipped_uframes
;
2280 DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes
);
2283 for (i
= 0; i
< uframes
; i
++) {
2285 * If we're running behind schedule, we should not catch up
2286 * too fast, as that will make some guests unhappy:
2287 * 1) We must process a minimum of MIN_UFR_PER_TICK frames,
2288 * otherwise we will never catch up
2289 * 2) Process frames until the guest has requested an irq (IOC)
2291 if (i
>= MIN_UFR_PER_TICK
) {
2292 ehci_commit_irq(ehci
);
2293 if ((ehci
->usbsts
& USBINTR_MASK
) & ehci
->usbintr
) {
2297 if (ehci
->periodic_sched_active
) {
2298 ehci
->periodic_sched_active
--;
2300 ehci_update_frindex(ehci
, 1);
2301 if ((ehci
->frindex
& 7) == 0) {
2302 ehci_advance_periodic_state(ehci
);
2304 ehci
->last_run_ns
+= UFRAME_TIMER_NS
;
2307 ehci
->periodic_sched_active
= 0;
2308 ehci_update_frindex(ehci
, uframes
);
2309 ehci
->last_run_ns
+= UFRAME_TIMER_NS
* uframes
;
2312 if (ehci
->periodic_sched_active
) {
2313 ehci
->async_stepdown
= 0;
2314 } else if (ehci
->async_stepdown
< ehci
->maxframes
/ 2) {
2315 ehci
->async_stepdown
++;
2318 /* Async is not inside loop since it executes everything it can once
2321 if (ehci_async_enabled(ehci
) || ehci
->astate
!= EST_INACTIVE
) {
2323 ehci_advance_async_state(ehci
);
2326 ehci_commit_irq(ehci
);
2327 if (ehci
->usbsts_pending
) {
2329 ehci
->async_stepdown
= 0;
2332 if (ehci_enabled(ehci
) && (ehci
->usbintr
& USBSTS_FLR
)) {
2337 /* If we've raised int, we speed up the timer, so that we quickly
2338 * notice any new packets queued up in response */
2339 if (ehci
->int_req_by_async
&& (ehci
->usbsts
& USBSTS_INT
)) {
2340 expire_time
= t_now
+
2341 NANOSECONDS_PER_SECOND
/ (FRAME_TIMER_FREQ
* 4);
2342 ehci
->int_req_by_async
= false;
2344 expire_time
= t_now
+ (NANOSECONDS_PER_SECOND
2345 * (ehci
->async_stepdown
+1) / FRAME_TIMER_FREQ
);
2347 timer_mod(ehci
->frame_timer
, expire_time
);
2350 ehci
->working
= false;
2353 static void ehci_work_timer(void *opaque
)
2355 EHCIState
*ehci
= opaque
;
2357 qemu_bh_schedule(ehci
->async_bh
);
2360 static const MemoryRegionOps ehci_mmio_caps_ops
= {
2361 .read
= ehci_caps_read
,
2362 .write
= ehci_caps_write
,
2363 .valid
.min_access_size
= 1,
2364 .valid
.max_access_size
= 4,
2365 .impl
.min_access_size
= 1,
2366 .impl
.max_access_size
= 1,
2367 .endianness
= DEVICE_LITTLE_ENDIAN
,
2370 static const MemoryRegionOps ehci_mmio_opreg_ops
= {
2371 .read
= ehci_opreg_read
,
2372 .write
= ehci_opreg_write
,
2373 .valid
.min_access_size
= 4,
2374 .valid
.max_access_size
= 4,
2375 .endianness
= DEVICE_LITTLE_ENDIAN
,
2378 static const MemoryRegionOps ehci_mmio_port_ops
= {
2379 .read
= ehci_port_read
,
2380 .write
= ehci_port_write
,
2381 .valid
.min_access_size
= 4,
2382 .valid
.max_access_size
= 4,
2383 .endianness
= DEVICE_LITTLE_ENDIAN
,
2386 static USBPortOps ehci_port_ops
= {
2387 .attach
= ehci_attach
,
2388 .detach
= ehci_detach
,
2389 .child_detach
= ehci_child_detach
,
2390 .wakeup
= ehci_wakeup
,
2391 .complete
= ehci_async_complete_packet
,
2394 static USBBusOps ehci_bus_ops_companion
= {
2395 .register_companion
= ehci_register_companion
,
2396 .wakeup_endpoint
= ehci_wakeup_endpoint
,
2398 static USBBusOps ehci_bus_ops_standalone
= {
2399 .wakeup_endpoint
= ehci_wakeup_endpoint
,
2402 static int usb_ehci_pre_save(void *opaque
)
2404 EHCIState
*ehci
= opaque
;
2405 uint32_t new_frindex
;
2407 /* Round down frindex to a multiple of 8 for migration compatibility */
2408 new_frindex
= ehci
->frindex
& ~7;
2409 ehci
->last_run_ns
-= (ehci
->frindex
- new_frindex
) * UFRAME_TIMER_NS
;
2410 ehci
->frindex
= new_frindex
;
2415 static int usb_ehci_post_load(void *opaque
, int version_id
)
2417 EHCIState
*s
= opaque
;
2420 for (i
= 0; i
< NB_PORTS
; i
++) {
2421 USBPort
*companion
= s
->companion_ports
[i
];
2422 if (companion
== NULL
) {
2425 if (s
->portsc
[i
] & PORTSC_POWNER
) {
2426 companion
->dev
= s
->ports
[i
].dev
;
2428 companion
->dev
= NULL
;
2435 static void usb_ehci_vm_state_change(void *opaque
, int running
, RunState state
)
2437 EHCIState
*ehci
= opaque
;
2440 * We don't migrate the EHCIQueue-s, instead we rebuild them for the
2441 * schedule in guest memory. We must do the rebuilt ASAP, so that
2442 * USB-devices which have async handled packages have a packet in the
2443 * ep queue to match the completion with.
2445 if (state
== RUN_STATE_RUNNING
) {
2446 ehci_advance_async_state(ehci
);
2450 * The schedule rebuilt from guest memory could cause the migration dest
2451 * to miss a QH unlink, and fail to cancel packets, since the unlinked QH
2452 * will never have existed on the destination. Therefor we must flush the
2453 * async schedule on savevm to catch any not yet noticed unlinks.
2455 if (state
== RUN_STATE_SAVE_VM
) {
2456 ehci_advance_async_state(ehci
);
2457 ehci_queues_rip_unseen(ehci
, 1);
2461 const VMStateDescription vmstate_ehci
= {
2462 .name
= "ehci-core",
2464 .minimum_version_id
= 1,
2465 .pre_save
= usb_ehci_pre_save
,
2466 .post_load
= usb_ehci_post_load
,
2467 .fields
= (VMStateField
[]) {
2468 /* mmio registers */
2469 VMSTATE_UINT32(usbcmd
, EHCIState
),
2470 VMSTATE_UINT32(usbsts
, EHCIState
),
2471 VMSTATE_UINT32_V(usbsts_pending
, EHCIState
, 2),
2472 VMSTATE_UINT32_V(usbsts_frindex
, EHCIState
, 2),
2473 VMSTATE_UINT32(usbintr
, EHCIState
),
2474 VMSTATE_UINT32(frindex
, EHCIState
),
2475 VMSTATE_UINT32(ctrldssegment
, EHCIState
),
2476 VMSTATE_UINT32(periodiclistbase
, EHCIState
),
2477 VMSTATE_UINT32(asynclistaddr
, EHCIState
),
2478 VMSTATE_UINT32(configflag
, EHCIState
),
2479 VMSTATE_UINT32(portsc
[0], EHCIState
),
2480 VMSTATE_UINT32(portsc
[1], EHCIState
),
2481 VMSTATE_UINT32(portsc
[2], EHCIState
),
2482 VMSTATE_UINT32(portsc
[3], EHCIState
),
2483 VMSTATE_UINT32(portsc
[4], EHCIState
),
2484 VMSTATE_UINT32(portsc
[5], EHCIState
),
2486 VMSTATE_TIMER_PTR(frame_timer
, EHCIState
),
2487 VMSTATE_UINT64(last_run_ns
, EHCIState
),
2488 VMSTATE_UINT32(async_stepdown
, EHCIState
),
2489 /* schedule state */
2490 VMSTATE_UINT32(astate
, EHCIState
),
2491 VMSTATE_UINT32(pstate
, EHCIState
),
2492 VMSTATE_UINT32(a_fetch_addr
, EHCIState
),
2493 VMSTATE_UINT32(p_fetch_addr
, EHCIState
),
2494 VMSTATE_END_OF_LIST()
2498 void usb_ehci_realize(EHCIState
*s
, DeviceState
*dev
, Error
**errp
)
2502 if (s
->portnr
> NB_PORTS
) {
2503 error_setg(errp
, "Too many ports! Max. port number is %d.",
2507 if (s
->maxframes
< 8 || s
->maxframes
> 512) {
2508 error_setg(errp
, "maxframes %d out if range (8 .. 512)",
2513 usb_bus_new(&s
->bus
, sizeof(s
->bus
), s
->companion_enable
?
2514 &ehci_bus_ops_companion
: &ehci_bus_ops_standalone
, dev
);
2515 for (i
= 0; i
< s
->portnr
; i
++) {
2516 usb_register_port(&s
->bus
, &s
->ports
[i
], s
, i
, &ehci_port_ops
,
2517 USB_SPEED_MASK_HIGH
);
2518 s
->ports
[i
].dev
= 0;
2521 s
->frame_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ehci_work_timer
, s
);
2522 s
->async_bh
= qemu_bh_new(ehci_work_bh
, s
);
2525 s
->vmstate
= qemu_add_vm_change_state_handler(usb_ehci_vm_state_change
, s
);
2528 void usb_ehci_unrealize(EHCIState
*s
, DeviceState
*dev
, Error
**errp
)
2530 trace_usb_ehci_unrealize();
2532 if (s
->frame_timer
) {
2533 timer_del(s
->frame_timer
);
2534 timer_free(s
->frame_timer
);
2535 s
->frame_timer
= NULL
;
2538 qemu_bh_delete(s
->async_bh
);
2541 ehci_queues_rip_all(s
, 0);
2542 ehci_queues_rip_all(s
, 1);
2544 memory_region_del_subregion(&s
->mem
, &s
->mem_caps
);
2545 memory_region_del_subregion(&s
->mem
, &s
->mem_opreg
);
2546 memory_region_del_subregion(&s
->mem
, &s
->mem_ports
);
2548 usb_bus_release(&s
->bus
);
2551 qemu_del_vm_change_state_handler(s
->vmstate
);
2555 void usb_ehci_init(EHCIState
*s
, DeviceState
*dev
)
2557 /* 2.2 host controller interface version */
2558 s
->caps
[0x00] = (uint8_t)(s
->opregbase
- s
->capsbase
);
2559 s
->caps
[0x01] = 0x00;
2560 s
->caps
[0x02] = 0x00;
2561 s
->caps
[0x03] = 0x01; /* HC version */
2562 s
->caps
[0x04] = s
->portnr
; /* Number of downstream ports */
2563 s
->caps
[0x05] = 0x00; /* No companion ports at present */
2564 s
->caps
[0x06] = 0x00;
2565 s
->caps
[0x07] = 0x00;
2566 s
->caps
[0x08] = 0x80; /* We can cache whole frame, no 64-bit */
2567 s
->caps
[0x0a] = 0x00;
2568 s
->caps
[0x0b] = 0x00;
2570 QTAILQ_INIT(&s
->aqueues
);
2571 QTAILQ_INIT(&s
->pqueues
);
2572 usb_packet_init(&s
->ipacket
);
2574 memory_region_init(&s
->mem
, OBJECT(dev
), "ehci", MMIO_SIZE
);
2575 memory_region_init_io(&s
->mem_caps
, OBJECT(dev
), &ehci_mmio_caps_ops
, s
,
2576 "capabilities", CAPA_SIZE
);
2577 memory_region_init_io(&s
->mem_opreg
, OBJECT(dev
), &ehci_mmio_opreg_ops
, s
,
2578 "operational", s
->portscbase
);
2579 memory_region_init_io(&s
->mem_ports
, OBJECT(dev
), &ehci_mmio_port_ops
, s
,
2580 "ports", 4 * s
->portnr
);
2582 memory_region_add_subregion(&s
->mem
, s
->capsbase
, &s
->mem_caps
);
2583 memory_region_add_subregion(&s
->mem
, s
->opregbase
, &s
->mem_opreg
);
2584 memory_region_add_subregion(&s
->mem
, s
->opregbase
+ s
->portscbase
,
2588 void usb_ehci_finalize(EHCIState
*s
)
2590 usb_packet_cleanup(&s
->ipacket
);
2594 * vim: expandtab ts=4