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"
35 #include "qemu/error-report.h"
37 #define FRAME_TIMER_FREQ 1000
38 #define FRAME_TIMER_NS (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ)
39 #define UFRAME_TIMER_NS (FRAME_TIMER_NS / 8)
41 #define NB_MAXINTRATE 8 // Max rate at which controller issues ints
42 #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction
43 #define MAX_QH 100 // Max allowable queue heads in a chain
44 #define MIN_UFR_PER_TICK 24 /* Min frames to process when catching up */
45 #define PERIODIC_ACTIVE 512 /* Micro-frames */
47 /* Internal periodic / asynchronous schedule state machine states
54 /* The following states are internal to the state machine function
68 /* macros for accessing fields within next link pointer entry */
69 #define NLPTR_GET(x) ((x) & 0xffffffe0)
70 #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3)
71 #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid
73 /* link pointer types */
74 #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor
75 #define NLPTR_TYPE_QH 1 // queue head
76 #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor
77 #define NLPTR_TYPE_FSTN 3 // frame span traversal node
79 #define SET_LAST_RUN_CLOCK(s) \
80 (s)->last_run_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
82 /* nifty macros from Arnon's EHCI version */
83 #define get_field(data, field) \
84 (((data) & field##_MASK) >> field##_SH)
86 #define set_field(data, newval, field) do { \
87 uint32_t val = *data; \
88 val &= ~ field##_MASK; \
89 val |= ((newval) << field##_SH) & field##_MASK; \
93 static const char *ehci_state_names
[] = {
94 [EST_INACTIVE
] = "INACTIVE",
95 [EST_ACTIVE
] = "ACTIVE",
96 [EST_EXECUTING
] = "EXECUTING",
97 [EST_SLEEPING
] = "SLEEPING",
98 [EST_WAITLISTHEAD
] = "WAITLISTHEAD",
99 [EST_FETCHENTRY
] = "FETCH ENTRY",
100 [EST_FETCHQH
] = "FETCH QH",
101 [EST_FETCHITD
] = "FETCH ITD",
102 [EST_ADVANCEQUEUE
] = "ADVANCEQUEUE",
103 [EST_FETCHQTD
] = "FETCH QTD",
104 [EST_EXECUTE
] = "EXECUTE",
105 [EST_WRITEBACK
] = "WRITEBACK",
106 [EST_HORIZONTALQH
] = "HORIZONTALQH",
109 static const char *ehci_mmio_names
[] = {
112 [USBINTR
] = "USBINTR",
113 [FRINDEX
] = "FRINDEX",
114 [PERIODICLISTBASE
] = "P-LIST BASE",
115 [ASYNCLISTADDR
] = "A-LIST ADDR",
116 [CONFIGFLAG
] = "CONFIGFLAG",
119 static int ehci_state_executing(EHCIQueue
*q
);
120 static int ehci_state_writeback(EHCIQueue
*q
);
121 static int ehci_state_advqueue(EHCIQueue
*q
);
122 static int ehci_fill_queue(EHCIPacket
*p
);
123 static void ehci_free_packet(EHCIPacket
*p
);
125 static const char *nr2str(const char **n
, size_t len
, uint32_t nr
)
127 if (nr
< len
&& n
[nr
] != NULL
) {
134 static const char *state2str(uint32_t state
)
136 return nr2str(ehci_state_names
, ARRAY_SIZE(ehci_state_names
), state
);
139 static const char *addr2str(hwaddr addr
)
141 return nr2str(ehci_mmio_names
, ARRAY_SIZE(ehci_mmio_names
), addr
);
144 static void ehci_trace_usbsts(uint32_t mask
, int state
)
147 if (mask
& USBSTS_INT
) {
148 trace_usb_ehci_usbsts("INT", state
);
150 if (mask
& USBSTS_ERRINT
) {
151 trace_usb_ehci_usbsts("ERRINT", state
);
153 if (mask
& USBSTS_PCD
) {
154 trace_usb_ehci_usbsts("PCD", state
);
156 if (mask
& USBSTS_FLR
) {
157 trace_usb_ehci_usbsts("FLR", state
);
159 if (mask
& USBSTS_HSE
) {
160 trace_usb_ehci_usbsts("HSE", state
);
162 if (mask
& USBSTS_IAA
) {
163 trace_usb_ehci_usbsts("IAA", state
);
167 if (mask
& USBSTS_HALT
) {
168 trace_usb_ehci_usbsts("HALT", state
);
170 if (mask
& USBSTS_REC
) {
171 trace_usb_ehci_usbsts("REC", state
);
173 if (mask
& USBSTS_PSS
) {
174 trace_usb_ehci_usbsts("PSS", state
);
176 if (mask
& USBSTS_ASS
) {
177 trace_usb_ehci_usbsts("ASS", state
);
181 static inline void ehci_set_usbsts(EHCIState
*s
, int mask
)
183 if ((s
->usbsts
& mask
) == mask
) {
186 ehci_trace_usbsts(mask
, 1);
190 static inline void ehci_clear_usbsts(EHCIState
*s
, int mask
)
192 if ((s
->usbsts
& mask
) == 0) {
195 ehci_trace_usbsts(mask
, 0);
199 /* update irq line */
200 static inline void ehci_update_irq(EHCIState
*s
)
204 if ((s
->usbsts
& USBINTR_MASK
) & s
->usbintr
) {
208 trace_usb_ehci_irq(level
, s
->frindex
, s
->usbsts
, s
->usbintr
);
209 qemu_set_irq(s
->irq
, level
);
212 /* flag interrupt condition */
213 static inline void ehci_raise_irq(EHCIState
*s
, int intr
)
215 if (intr
& (USBSTS_PCD
| USBSTS_FLR
| USBSTS_HSE
)) {
219 s
->usbsts_pending
|= intr
;
224 * Commit pending interrupts (added via ehci_raise_irq),
225 * at the rate allowed by "Interrupt Threshold Control".
227 static inline void ehci_commit_irq(EHCIState
*s
)
231 if (!s
->usbsts_pending
) {
234 if (s
->usbsts_frindex
> s
->frindex
) {
238 itc
= (s
->usbcmd
>> 16) & 0xff;
239 s
->usbsts
|= s
->usbsts_pending
;
240 s
->usbsts_pending
= 0;
241 s
->usbsts_frindex
= s
->frindex
+ itc
;
245 static void ehci_update_halt(EHCIState
*s
)
247 if (s
->usbcmd
& USBCMD_RUNSTOP
) {
248 ehci_clear_usbsts(s
, USBSTS_HALT
);
250 if (s
->astate
== EST_INACTIVE
&& s
->pstate
== EST_INACTIVE
) {
251 ehci_set_usbsts(s
, USBSTS_HALT
);
256 static void ehci_set_state(EHCIState
*s
, int async
, int state
)
259 trace_usb_ehci_state("async", state2str(state
));
261 if (s
->astate
== EST_INACTIVE
) {
262 ehci_clear_usbsts(s
, USBSTS_ASS
);
265 ehci_set_usbsts(s
, USBSTS_ASS
);
268 trace_usb_ehci_state("periodic", state2str(state
));
270 if (s
->pstate
== EST_INACTIVE
) {
271 ehci_clear_usbsts(s
, USBSTS_PSS
);
274 ehci_set_usbsts(s
, USBSTS_PSS
);
279 static int ehci_get_state(EHCIState
*s
, int async
)
281 return async
? s
->astate
: s
->pstate
;
284 static void ehci_set_fetch_addr(EHCIState
*s
, int async
, uint32_t addr
)
287 s
->a_fetch_addr
= addr
;
289 s
->p_fetch_addr
= addr
;
293 static int ehci_get_fetch_addr(EHCIState
*s
, int async
)
295 return async
? s
->a_fetch_addr
: s
->p_fetch_addr
;
298 static void ehci_trace_qh(EHCIQueue
*q
, hwaddr addr
, EHCIqh
*qh
)
300 /* need three here due to argument count limits */
301 trace_usb_ehci_qh_ptrs(q
, addr
, qh
->next
,
302 qh
->current_qtd
, qh
->next_qtd
, qh
->altnext_qtd
);
303 trace_usb_ehci_qh_fields(addr
,
304 get_field(qh
->epchar
, QH_EPCHAR_RL
),
305 get_field(qh
->epchar
, QH_EPCHAR_MPLEN
),
306 get_field(qh
->epchar
, QH_EPCHAR_EPS
),
307 get_field(qh
->epchar
, QH_EPCHAR_EP
),
308 get_field(qh
->epchar
, QH_EPCHAR_DEVADDR
));
309 trace_usb_ehci_qh_bits(addr
,
310 (bool)(qh
->epchar
& QH_EPCHAR_C
),
311 (bool)(qh
->epchar
& QH_EPCHAR_H
),
312 (bool)(qh
->epchar
& QH_EPCHAR_DTC
),
313 (bool)(qh
->epchar
& QH_EPCHAR_I
));
316 static void ehci_trace_qtd(EHCIQueue
*q
, hwaddr addr
, EHCIqtd
*qtd
)
318 /* need three here due to argument count limits */
319 trace_usb_ehci_qtd_ptrs(q
, addr
, qtd
->next
, qtd
->altnext
);
320 trace_usb_ehci_qtd_fields(addr
,
321 get_field(qtd
->token
, QTD_TOKEN_TBYTES
),
322 get_field(qtd
->token
, QTD_TOKEN_CPAGE
),
323 get_field(qtd
->token
, QTD_TOKEN_CERR
),
324 get_field(qtd
->token
, QTD_TOKEN_PID
));
325 trace_usb_ehci_qtd_bits(addr
,
326 (bool)(qtd
->token
& QTD_TOKEN_IOC
),
327 (bool)(qtd
->token
& QTD_TOKEN_ACTIVE
),
328 (bool)(qtd
->token
& QTD_TOKEN_HALT
),
329 (bool)(qtd
->token
& QTD_TOKEN_BABBLE
),
330 (bool)(qtd
->token
& QTD_TOKEN_XACTERR
));
333 static void ehci_trace_itd(EHCIState
*s
, hwaddr addr
, EHCIitd
*itd
)
335 trace_usb_ehci_itd(addr
, itd
->next
,
336 get_field(itd
->bufptr
[1], ITD_BUFPTR_MAXPKT
),
337 get_field(itd
->bufptr
[2], ITD_BUFPTR_MULT
),
338 get_field(itd
->bufptr
[0], ITD_BUFPTR_EP
),
339 get_field(itd
->bufptr
[0], ITD_BUFPTR_DEVADDR
));
342 static void ehci_trace_sitd(EHCIState
*s
, hwaddr addr
,
345 trace_usb_ehci_sitd(addr
, sitd
->next
,
346 (bool)(sitd
->results
& SITD_RESULTS_ACTIVE
));
349 static void ehci_trace_guest_bug(EHCIState
*s
, const char *message
)
351 trace_usb_ehci_guest_bug(message
);
352 warn_report("%s", message
);
355 static inline bool ehci_enabled(EHCIState
*s
)
357 return s
->usbcmd
& USBCMD_RUNSTOP
;
360 static inline bool ehci_async_enabled(EHCIState
*s
)
362 return ehci_enabled(s
) && (s
->usbcmd
& USBCMD_ASE
);
365 static inline bool ehci_periodic_enabled(EHCIState
*s
)
367 return ehci_enabled(s
) && (s
->usbcmd
& USBCMD_PSE
);
370 /* Get an array of dwords from main memory */
371 static inline int get_dwords(EHCIState
*ehci
, uint32_t addr
,
372 uint32_t *buf
, int num
)
377 ehci_raise_irq(ehci
, USBSTS_HSE
);
378 ehci
->usbcmd
&= ~USBCMD_RUNSTOP
;
379 trace_usb_ehci_dma_error();
383 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
384 dma_memory_read(ehci
->as
, addr
, buf
, sizeof(*buf
));
385 *buf
= le32_to_cpu(*buf
);
391 /* Put an array of dwords in to main memory */
392 static inline int put_dwords(EHCIState
*ehci
, uint32_t addr
,
393 uint32_t *buf
, int num
)
398 ehci_raise_irq(ehci
, USBSTS_HSE
);
399 ehci
->usbcmd
&= ~USBCMD_RUNSTOP
;
400 trace_usb_ehci_dma_error();
404 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
405 uint32_t tmp
= cpu_to_le32(*buf
);
406 dma_memory_write(ehci
->as
, addr
, &tmp
, sizeof(tmp
));
412 static int ehci_get_pid(EHCIqtd
*qtd
)
414 switch (get_field(qtd
->token
, QTD_TOKEN_PID
)) {
416 return USB_TOKEN_OUT
;
420 return USB_TOKEN_SETUP
;
422 fprintf(stderr
, "bad token\n");
427 static bool ehci_verify_qh(EHCIQueue
*q
, EHCIqh
*qh
)
429 uint32_t devaddr
= get_field(qh
->epchar
, QH_EPCHAR_DEVADDR
);
430 uint32_t endp
= get_field(qh
->epchar
, QH_EPCHAR_EP
);
431 if ((devaddr
!= get_field(q
->qh
.epchar
, QH_EPCHAR_DEVADDR
)) ||
432 (endp
!= get_field(q
->qh
.epchar
, QH_EPCHAR_EP
)) ||
433 (qh
->current_qtd
!= q
->qh
.current_qtd
) ||
434 (q
->async
&& qh
->next_qtd
!= q
->qh
.next_qtd
) ||
435 (memcmp(&qh
->altnext_qtd
, &q
->qh
.altnext_qtd
,
436 7 * sizeof(uint32_t)) != 0) ||
437 (q
->dev
!= NULL
&& q
->dev
->addr
!= devaddr
)) {
444 static bool ehci_verify_qtd(EHCIPacket
*p
, EHCIqtd
*qtd
)
446 if (p
->qtdaddr
!= p
->queue
->qtdaddr
||
447 (p
->queue
->async
&& !NLPTR_TBIT(p
->qtd
.next
) &&
448 (p
->qtd
.next
!= qtd
->next
)) ||
449 (!NLPTR_TBIT(p
->qtd
.altnext
) && (p
->qtd
.altnext
!= qtd
->altnext
)) ||
450 p
->qtd
.token
!= qtd
->token
||
451 p
->qtd
.bufptr
[0] != qtd
->bufptr
[0]) {
458 static bool ehci_verify_pid(EHCIQueue
*q
, EHCIqtd
*qtd
)
460 int ep
= get_field(q
->qh
.epchar
, QH_EPCHAR_EP
);
461 int pid
= ehci_get_pid(qtd
);
463 /* Note the pid changing is normal for ep 0 (the control ep) */
464 if (q
->last_pid
&& ep
!= 0 && pid
!= q
->last_pid
) {
471 /* Finish executing and writeback a packet outside of the regular
472 fetchqh -> fetchqtd -> execute -> writeback cycle */
473 static void ehci_writeback_async_complete_packet(EHCIPacket
*p
)
475 EHCIQueue
*q
= p
->queue
;
480 /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */
481 get_dwords(q
->ehci
, NLPTR_GET(q
->qhaddr
),
482 (uint32_t *) &qh
, sizeof(EHCIqh
) >> 2);
483 get_dwords(q
->ehci
, NLPTR_GET(q
->qtdaddr
),
484 (uint32_t *) &qtd
, sizeof(EHCIqtd
) >> 2);
485 if (!ehci_verify_qh(q
, &qh
) || !ehci_verify_qtd(p
, &qtd
)) {
486 p
->async
= EHCI_ASYNC_INITIALIZED
;
491 state
= ehci_get_state(q
->ehci
, q
->async
);
492 ehci_state_executing(q
);
493 ehci_state_writeback(q
); /* Frees the packet! */
494 if (!(q
->qh
.token
& QTD_TOKEN_HALT
)) {
495 ehci_state_advqueue(q
);
497 ehci_set_state(q
->ehci
, q
->async
, state
);
500 /* packet management */
502 static EHCIPacket
*ehci_alloc_packet(EHCIQueue
*q
)
506 p
= g_new0(EHCIPacket
, 1);
508 usb_packet_init(&p
->packet
);
509 QTAILQ_INSERT_TAIL(&q
->packets
, p
, next
);
510 trace_usb_ehci_packet_action(p
->queue
, p
, "alloc");
514 static void ehci_free_packet(EHCIPacket
*p
)
516 if (p
->async
== EHCI_ASYNC_FINISHED
&&
517 !(p
->queue
->qh
.token
& QTD_TOKEN_HALT
)) {
518 ehci_writeback_async_complete_packet(p
);
521 trace_usb_ehci_packet_action(p
->queue
, p
, "free");
522 if (p
->async
== EHCI_ASYNC_INFLIGHT
) {
523 usb_cancel_packet(&p
->packet
);
525 if (p
->async
== EHCI_ASYNC_FINISHED
&&
526 p
->packet
.status
== USB_RET_SUCCESS
) {
528 "EHCI: Dropping completed packet from halted %s ep %02X\n",
529 (p
->pid
== USB_TOKEN_IN
) ? "in" : "out",
530 get_field(p
->queue
->qh
.epchar
, QH_EPCHAR_EP
));
532 if (p
->async
!= EHCI_ASYNC_NONE
) {
533 usb_packet_unmap(&p
->packet
, &p
->sgl
);
534 qemu_sglist_destroy(&p
->sgl
);
536 QTAILQ_REMOVE(&p
->queue
->packets
, p
, next
);
537 usb_packet_cleanup(&p
->packet
);
541 /* queue management */
543 static EHCIQueue
*ehci_alloc_queue(EHCIState
*ehci
, uint32_t addr
, int async
)
545 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
548 q
= g_malloc0(sizeof(*q
));
552 QTAILQ_INIT(&q
->packets
);
553 QTAILQ_INSERT_HEAD(head
, q
, next
);
554 trace_usb_ehci_queue_action(q
, "alloc");
558 static void ehci_queue_stopped(EHCIQueue
*q
)
560 int endp
= get_field(q
->qh
.epchar
, QH_EPCHAR_EP
);
562 if (!q
->last_pid
|| !q
->dev
) {
566 usb_device_ep_stopped(q
->dev
, usb_ep_get(q
->dev
, q
->last_pid
, endp
));
569 static int ehci_cancel_queue(EHCIQueue
*q
)
574 p
= QTAILQ_FIRST(&q
->packets
);
579 trace_usb_ehci_queue_action(q
, "cancel");
583 } while ((p
= QTAILQ_FIRST(&q
->packets
)) != NULL
);
586 ehci_queue_stopped(q
);
590 static int ehci_reset_queue(EHCIQueue
*q
)
594 trace_usb_ehci_queue_action(q
, "reset");
595 packets
= ehci_cancel_queue(q
);
602 static void ehci_free_queue(EHCIQueue
*q
, const char *warn
)
604 EHCIQueueHead
*head
= q
->async
? &q
->ehci
->aqueues
: &q
->ehci
->pqueues
;
607 trace_usb_ehci_queue_action(q
, "free");
608 cancelled
= ehci_cancel_queue(q
);
609 if (warn
&& cancelled
> 0) {
610 ehci_trace_guest_bug(q
->ehci
, warn
);
612 QTAILQ_REMOVE(head
, q
, next
);
616 static EHCIQueue
*ehci_find_queue_by_qh(EHCIState
*ehci
, uint32_t addr
,
619 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
622 QTAILQ_FOREACH(q
, head
, next
) {
623 if (addr
== q
->qhaddr
) {
630 static void ehci_queues_rip_unused(EHCIState
*ehci
, int async
)
632 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
633 const char *warn
= async
? "guest unlinked busy QH" : NULL
;
634 uint64_t maxage
= FRAME_TIMER_NS
* ehci
->maxframes
* 4;
637 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
640 q
->ts
= ehci
->last_run_ns
;
643 if (ehci
->last_run_ns
< q
->ts
+ maxage
) {
646 ehci_free_queue(q
, warn
);
650 static void ehci_queues_rip_unseen(EHCIState
*ehci
, int async
)
652 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
655 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
657 ehci_free_queue(q
, NULL
);
662 static void ehci_queues_rip_device(EHCIState
*ehci
, USBDevice
*dev
, int async
)
664 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
667 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
671 ehci_free_queue(q
, NULL
);
675 static void ehci_queues_rip_all(EHCIState
*ehci
, int async
)
677 EHCIQueueHead
*head
= async
? &ehci
->aqueues
: &ehci
->pqueues
;
678 const char *warn
= async
? "guest stopped busy async schedule" : NULL
;
681 QTAILQ_FOREACH_SAFE(q
, head
, next
, tmp
) {
682 ehci_free_queue(q
, warn
);
686 /* Attach or detach a device on root hub */
688 static void ehci_attach(USBPort
*port
)
690 EHCIState
*s
= port
->opaque
;
691 uint32_t *portsc
= &s
->portsc
[port
->index
];
692 const char *owner
= (*portsc
& PORTSC_POWNER
) ? "comp" : "ehci";
694 trace_usb_ehci_port_attach(port
->index
, owner
, port
->dev
->product_desc
);
696 if (*portsc
& PORTSC_POWNER
) {
697 USBPort
*companion
= s
->companion_ports
[port
->index
];
698 companion
->dev
= port
->dev
;
699 companion
->ops
->attach(companion
);
703 *portsc
|= PORTSC_CONNECT
;
704 *portsc
|= PORTSC_CSC
;
706 ehci_raise_irq(s
, USBSTS_PCD
);
709 static void ehci_detach(USBPort
*port
)
711 EHCIState
*s
= port
->opaque
;
712 uint32_t *portsc
= &s
->portsc
[port
->index
];
713 const char *owner
= (*portsc
& PORTSC_POWNER
) ? "comp" : "ehci";
715 trace_usb_ehci_port_detach(port
->index
, owner
);
717 if (*portsc
& PORTSC_POWNER
) {
718 USBPort
*companion
= s
->companion_ports
[port
->index
];
719 companion
->ops
->detach(companion
);
720 companion
->dev
= NULL
;
722 * EHCI spec 4.2.2: "When a disconnect occurs... On the event,
723 * the port ownership is returned immediately to the EHCI controller."
725 *portsc
&= ~PORTSC_POWNER
;
729 ehci_queues_rip_device(s
, port
->dev
, 0);
730 ehci_queues_rip_device(s
, port
->dev
, 1);
732 *portsc
&= ~(PORTSC_CONNECT
|PORTSC_PED
|PORTSC_SUSPEND
);
733 *portsc
|= PORTSC_CSC
;
735 ehci_raise_irq(s
, USBSTS_PCD
);
738 static void ehci_child_detach(USBPort
*port
, USBDevice
*child
)
740 EHCIState
*s
= port
->opaque
;
741 uint32_t portsc
= s
->portsc
[port
->index
];
743 if (portsc
& PORTSC_POWNER
) {
744 USBPort
*companion
= s
->companion_ports
[port
->index
];
745 companion
->ops
->child_detach(companion
, child
);
749 ehci_queues_rip_device(s
, child
, 0);
750 ehci_queues_rip_device(s
, child
, 1);
753 static void ehci_wakeup(USBPort
*port
)
755 EHCIState
*s
= port
->opaque
;
756 uint32_t *portsc
= &s
->portsc
[port
->index
];
758 if (*portsc
& PORTSC_POWNER
) {
759 USBPort
*companion
= s
->companion_ports
[port
->index
];
760 if (companion
->ops
->wakeup
) {
761 companion
->ops
->wakeup(companion
);
766 if (*portsc
& PORTSC_SUSPEND
) {
767 trace_usb_ehci_port_wakeup(port
->index
);
768 *portsc
|= PORTSC_FPRES
;
769 ehci_raise_irq(s
, USBSTS_PCD
);
772 qemu_bh_schedule(s
->async_bh
);
775 static void ehci_register_companion(USBBus
*bus
, USBPort
*ports
[],
776 uint32_t portcount
, uint32_t firstport
,
779 EHCIState
*s
= container_of(bus
, EHCIState
, bus
);
782 if (firstport
+ portcount
> NB_PORTS
) {
783 error_setg(errp
, "firstport must be between 0 and %u",
784 NB_PORTS
- portcount
);
788 for (i
= 0; i
< portcount
; i
++) {
789 if (s
->companion_ports
[firstport
+ i
]) {
790 error_setg(errp
, "firstport %u asks for ports %u-%u,"
791 " but port %u has a companion assigned already",
792 firstport
, firstport
, firstport
+ portcount
- 1,
798 for (i
= 0; i
< portcount
; i
++) {
799 s
->companion_ports
[firstport
+ i
] = ports
[i
];
800 s
->ports
[firstport
+ i
].speedmask
|=
801 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
;
802 /* Ensure devs attached before the initial reset go to the companion */
803 s
->portsc
[firstport
+ i
] = PORTSC_POWNER
;
806 s
->companion_count
++;
807 s
->caps
[0x05] = (s
->companion_count
<< 4) | portcount
;
810 static void ehci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
813 EHCIState
*s
= container_of(bus
, EHCIState
, bus
);
814 uint32_t portsc
= s
->portsc
[ep
->dev
->port
->index
];
816 if (portsc
& PORTSC_POWNER
) {
820 s
->periodic_sched_active
= PERIODIC_ACTIVE
;
821 qemu_bh_schedule(s
->async_bh
);
824 static USBDevice
*ehci_find_device(EHCIState
*ehci
, uint8_t addr
)
830 for (i
= 0; i
< NB_PORTS
; i
++) {
831 port
= &ehci
->ports
[i
];
832 if (!(ehci
->portsc
[i
] & PORTSC_PED
)) {
833 DPRINTF("Port %d not enabled\n", i
);
836 dev
= usb_find_device(port
, addr
);
844 /* 4.1 host controller initialization */
845 void ehci_reset(void *opaque
)
847 EHCIState
*s
= opaque
;
849 USBDevice
*devs
[NB_PORTS
];
851 trace_usb_ehci_reset();
854 * Do the detach before touching portsc, so that it correctly gets send to
855 * us or to our companion based on PORTSC_POWNER before the reset.
857 for(i
= 0; i
< NB_PORTS
; i
++) {
858 devs
[i
] = s
->ports
[i
].dev
;
859 if (devs
[i
] && devs
[i
]->attached
) {
860 usb_detach(&s
->ports
[i
]);
864 memset(&s
->opreg
, 0x00, sizeof(s
->opreg
));
865 memset(&s
->portsc
, 0x00, sizeof(s
->portsc
));
867 s
->usbcmd
= NB_MAXINTRATE
<< USBCMD_ITC_SH
;
868 s
->usbsts
= USBSTS_HALT
;
869 s
->usbsts_pending
= 0;
870 s
->usbsts_frindex
= 0;
873 s
->astate
= EST_INACTIVE
;
874 s
->pstate
= EST_INACTIVE
;
876 for(i
= 0; i
< NB_PORTS
; i
++) {
877 if (s
->companion_ports
[i
]) {
878 s
->portsc
[i
] = PORTSC_POWNER
| PORTSC_PPOWER
;
880 s
->portsc
[i
] = PORTSC_PPOWER
;
882 if (devs
[i
] && devs
[i
]->attached
) {
883 usb_attach(&s
->ports
[i
]);
884 usb_device_reset(devs
[i
]);
887 ehci_queues_rip_all(s
, 0);
888 ehci_queues_rip_all(s
, 1);
889 timer_del(s
->frame_timer
);
890 qemu_bh_cancel(s
->async_bh
);
893 static uint64_t ehci_caps_read(void *ptr
, hwaddr addr
,
897 return s
->caps
[addr
];
900 static void ehci_caps_write(void *ptr
, hwaddr addr
,
901 uint64_t val
, unsigned size
)
905 static uint64_t ehci_opreg_read(void *ptr
, hwaddr addr
,
913 /* Round down to mult of 8, else it can go backwards on migration */
914 val
= s
->frindex
& ~7;
917 val
= s
->opreg
[addr
>> 2];
920 trace_usb_ehci_opreg_read(addr
+ s
->opregbase
, addr2str(addr
), val
);
924 static uint64_t ehci_port_read(void *ptr
, hwaddr addr
,
930 val
= s
->portsc
[addr
>> 2];
931 trace_usb_ehci_portsc_read(addr
+ s
->portscbase
, addr
>> 2, val
);
935 static void handle_port_owner_write(EHCIState
*s
, int port
, uint32_t owner
)
937 USBDevice
*dev
= s
->ports
[port
].dev
;
938 uint32_t *portsc
= &s
->portsc
[port
];
941 if (s
->companion_ports
[port
] == NULL
)
944 owner
= owner
& PORTSC_POWNER
;
945 orig
= *portsc
& PORTSC_POWNER
;
947 if (!(owner
^ orig
)) {
951 if (dev
&& dev
->attached
) {
952 usb_detach(&s
->ports
[port
]);
955 *portsc
&= ~PORTSC_POWNER
;
958 if (dev
&& dev
->attached
) {
959 usb_attach(&s
->ports
[port
]);
963 static void ehci_port_write(void *ptr
, hwaddr addr
,
964 uint64_t val
, unsigned size
)
967 int port
= addr
>> 2;
968 uint32_t *portsc
= &s
->portsc
[port
];
969 uint32_t old
= *portsc
;
970 USBDevice
*dev
= s
->ports
[port
].dev
;
972 trace_usb_ehci_portsc_write(addr
+ s
->portscbase
, addr
>> 2, val
);
975 *portsc
&= ~(val
& PORTSC_RWC_MASK
);
976 /* The guest may clear, but not set the PED bit */
977 *portsc
&= val
| ~PORTSC_PED
;
978 /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
979 handle_port_owner_write(s
, port
, val
);
980 /* And finally apply RO_MASK */
981 val
&= PORTSC_RO_MASK
;
983 if ((val
& PORTSC_PRESET
) && !(*portsc
& PORTSC_PRESET
)) {
984 trace_usb_ehci_port_reset(port
, 1);
987 if (!(val
& PORTSC_PRESET
) &&(*portsc
& PORTSC_PRESET
)) {
988 trace_usb_ehci_port_reset(port
, 0);
989 if (dev
&& dev
->attached
) {
990 usb_port_reset(&s
->ports
[port
]);
991 *portsc
&= ~PORTSC_CSC
;
995 * Table 2.16 Set the enable bit(and enable bit change) to indicate
996 * to SW that this port has a high speed device attached
998 if (dev
&& dev
->attached
&& (dev
->speedmask
& USB_SPEED_MASK_HIGH
)) {
1003 if ((val
& PORTSC_SUSPEND
) && !(*portsc
& PORTSC_SUSPEND
)) {
1004 trace_usb_ehci_port_suspend(port
);
1006 if (!(val
& PORTSC_FPRES
) && (*portsc
& PORTSC_FPRES
)) {
1007 trace_usb_ehci_port_resume(port
);
1008 val
&= ~PORTSC_SUSPEND
;
1011 *portsc
&= ~PORTSC_RO_MASK
;
1013 trace_usb_ehci_portsc_change(addr
+ s
->portscbase
, addr
>> 2, *portsc
, old
);
1016 static void ehci_opreg_write(void *ptr
, hwaddr addr
,
1017 uint64_t val
, unsigned size
)
1020 uint32_t *mmio
= s
->opreg
+ (addr
>> 2);
1021 uint32_t old
= *mmio
;
1024 trace_usb_ehci_opreg_write(addr
+ s
->opregbase
, addr2str(addr
), val
);
1028 if (val
& USBCMD_HCRESET
) {
1034 /* not supporting dynamic frame list size at the moment */
1035 if ((val
& USBCMD_FLS
) && !(s
->usbcmd
& USBCMD_FLS
)) {
1036 fprintf(stderr
, "attempt to set frame list size -- value %d\n",
1037 (int)val
& USBCMD_FLS
);
1041 if (val
& USBCMD_IAAD
) {
1043 * Process IAAD immediately, otherwise the Linux IAAD watchdog may
1044 * trigger and re-use a qh without us seeing the unlink.
1046 s
->async_stepdown
= 0;
1047 qemu_bh_schedule(s
->async_bh
);
1048 trace_usb_ehci_doorbell_ring();
1051 if (((USBCMD_RUNSTOP
| USBCMD_PSE
| USBCMD_ASE
) & val
) !=
1052 ((USBCMD_RUNSTOP
| USBCMD_PSE
| USBCMD_ASE
) & s
->usbcmd
)) {
1053 if (s
->pstate
== EST_INACTIVE
) {
1054 SET_LAST_RUN_CLOCK(s
);
1056 s
->usbcmd
= val
; /* Set usbcmd for ehci_update_halt() */
1057 ehci_update_halt(s
);
1058 s
->async_stepdown
= 0;
1059 qemu_bh_schedule(s
->async_bh
);
1064 val
&= USBSTS_RO_MASK
; // bits 6 through 31 are RO
1065 ehci_clear_usbsts(s
, val
); // bits 0 through 5 are R/WC
1071 val
&= USBINTR_MASK
;
1072 if (ehci_enabled(s
) && (USBSTS_FLR
& val
)) {
1073 qemu_bh_schedule(s
->async_bh
);
1078 val
&= 0x00003fff; /* frindex is 14bits */
1079 s
->usbsts_frindex
= val
;
1085 for(i
= 0; i
< NB_PORTS
; i
++)
1086 handle_port_owner_write(s
, i
, 0);
1090 case PERIODICLISTBASE
:
1091 if (ehci_periodic_enabled(s
)) {
1093 "ehci: PERIODIC list base register set while periodic schedule\n"
1094 " is enabled and HC is enabled\n");
1099 if (ehci_async_enabled(s
)) {
1101 "ehci: ASYNC list address register set while async schedule\n"
1102 " is enabled and HC is enabled\n");
1108 trace_usb_ehci_opreg_change(addr
+ s
->opregbase
, addr2str(addr
),
1113 * Write the qh back to guest physical memory. This step isn't
1114 * in the EHCI spec but we need to do it since we don't share
1115 * physical memory with our guest VM.
1117 * The first three dwords are read-only for the EHCI, so skip them
1118 * when writing back the qh.
1120 static void ehci_flush_qh(EHCIQueue
*q
)
1122 uint32_t *qh
= (uint32_t *) &q
->qh
;
1123 uint32_t dwords
= sizeof(EHCIqh
) >> 2;
1124 uint32_t addr
= NLPTR_GET(q
->qhaddr
);
1126 put_dwords(q
->ehci
, addr
+ 3 * sizeof(uint32_t), qh
+ 3, dwords
- 3);
1131 static int ehci_qh_do_overlay(EHCIQueue
*q
)
1133 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1141 assert(p
->qtdaddr
== q
->qtdaddr
);
1143 // remember values in fields to preserve in qh after overlay
1145 dtoggle
= q
->qh
.token
& QTD_TOKEN_DTOGGLE
;
1146 ping
= q
->qh
.token
& QTD_TOKEN_PING
;
1148 q
->qh
.current_qtd
= p
->qtdaddr
;
1149 q
->qh
.next_qtd
= p
->qtd
.next
;
1150 q
->qh
.altnext_qtd
= p
->qtd
.altnext
;
1151 q
->qh
.token
= p
->qtd
.token
;
1154 eps
= get_field(q
->qh
.epchar
, QH_EPCHAR_EPS
);
1155 if (eps
== EHCI_QH_EPS_HIGH
) {
1156 q
->qh
.token
&= ~QTD_TOKEN_PING
;
1157 q
->qh
.token
|= ping
;
1160 reload
= get_field(q
->qh
.epchar
, QH_EPCHAR_RL
);
1161 set_field(&q
->qh
.altnext_qtd
, reload
, QH_ALTNEXT_NAKCNT
);
1163 for (i
= 0; i
< 5; i
++) {
1164 q
->qh
.bufptr
[i
] = p
->qtd
.bufptr
[i
];
1167 if (!(q
->qh
.epchar
& QH_EPCHAR_DTC
)) {
1168 // preserve QH DT bit
1169 q
->qh
.token
&= ~QTD_TOKEN_DTOGGLE
;
1170 q
->qh
.token
|= dtoggle
;
1173 q
->qh
.bufptr
[1] &= ~BUFPTR_CPROGMASK_MASK
;
1174 q
->qh
.bufptr
[2] &= ~BUFPTR_FRAMETAG_MASK
;
1181 static int ehci_init_transfer(EHCIPacket
*p
)
1183 uint32_t cpage
, offset
, bytes
, plen
;
1186 cpage
= get_field(p
->qtd
.token
, QTD_TOKEN_CPAGE
);
1187 bytes
= get_field(p
->qtd
.token
, QTD_TOKEN_TBYTES
);
1188 offset
= p
->qtd
.bufptr
[0] & ~QTD_BUFPTR_MASK
;
1189 qemu_sglist_init(&p
->sgl
, p
->queue
->ehci
->device
, 5, p
->queue
->ehci
->as
);
1193 fprintf(stderr
, "cpage out of range (%d)\n", cpage
);
1194 qemu_sglist_destroy(&p
->sgl
);
1198 page
= p
->qtd
.bufptr
[cpage
] & QTD_BUFPTR_MASK
;
1201 if (plen
> 4096 - offset
) {
1202 plen
= 4096 - offset
;
1207 qemu_sglist_add(&p
->sgl
, page
, plen
);
1213 static void ehci_finish_transfer(EHCIQueue
*q
, int len
)
1215 uint32_t cpage
, offset
;
1218 /* update cpage & offset */
1219 cpage
= get_field(q
->qh
.token
, QTD_TOKEN_CPAGE
);
1220 offset
= q
->qh
.bufptr
[0] & ~QTD_BUFPTR_MASK
;
1223 cpage
+= offset
>> QTD_BUFPTR_SH
;
1224 offset
&= ~QTD_BUFPTR_MASK
;
1226 set_field(&q
->qh
.token
, cpage
, QTD_TOKEN_CPAGE
);
1227 q
->qh
.bufptr
[0] &= QTD_BUFPTR_MASK
;
1228 q
->qh
.bufptr
[0] |= offset
;
1232 static void ehci_async_complete_packet(USBPort
*port
, USBPacket
*packet
)
1235 EHCIState
*s
= port
->opaque
;
1236 uint32_t portsc
= s
->portsc
[port
->index
];
1238 if (portsc
& PORTSC_POWNER
) {
1239 USBPort
*companion
= s
->companion_ports
[port
->index
];
1240 companion
->ops
->complete(companion
, packet
);
1244 p
= container_of(packet
, EHCIPacket
, packet
);
1245 assert(p
->async
== EHCI_ASYNC_INFLIGHT
);
1247 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
1248 trace_usb_ehci_packet_action(p
->queue
, p
, "remove");
1249 ehci_free_packet(p
);
1253 trace_usb_ehci_packet_action(p
->queue
, p
, "wakeup");
1254 p
->async
= EHCI_ASYNC_FINISHED
;
1256 if (!p
->queue
->async
) {
1257 s
->periodic_sched_active
= PERIODIC_ACTIVE
;
1259 qemu_bh_schedule(s
->async_bh
);
1262 static void ehci_execute_complete(EHCIQueue
*q
)
1264 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1268 assert(p
->qtdaddr
== q
->qtdaddr
);
1269 assert(p
->async
== EHCI_ASYNC_INITIALIZED
||
1270 p
->async
== EHCI_ASYNC_FINISHED
);
1272 DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, "
1273 "status %d, actual_length %d\n",
1274 q
->qhaddr
, q
->qh
.next
, q
->qtdaddr
,
1275 p
->packet
.status
, p
->packet
.actual_length
);
1277 switch (p
->packet
.status
) {
1278 case USB_RET_SUCCESS
:
1280 case USB_RET_IOERROR
:
1282 q
->qh
.token
|= (QTD_TOKEN_HALT
| QTD_TOKEN_XACTERR
);
1283 set_field(&q
->qh
.token
, 0, QTD_TOKEN_CERR
);
1284 ehci_raise_irq(q
->ehci
, USBSTS_ERRINT
);
1287 q
->qh
.token
|= QTD_TOKEN_HALT
;
1288 ehci_raise_irq(q
->ehci
, USBSTS_ERRINT
);
1291 set_field(&q
->qh
.altnext_qtd
, 0, QH_ALTNEXT_NAKCNT
);
1292 return; /* We're not done yet with this transaction */
1293 case USB_RET_BABBLE
:
1294 q
->qh
.token
|= (QTD_TOKEN_HALT
| QTD_TOKEN_BABBLE
);
1295 ehci_raise_irq(q
->ehci
, USBSTS_ERRINT
);
1298 /* should not be triggerable */
1299 fprintf(stderr
, "USB invalid response %d\n", p
->packet
.status
);
1300 g_assert_not_reached();
1304 /* TODO check 4.12 for splits */
1305 tbytes
= get_field(q
->qh
.token
, QTD_TOKEN_TBYTES
);
1306 if (tbytes
&& p
->pid
== USB_TOKEN_IN
) {
1307 tbytes
-= p
->packet
.actual_length
;
1309 /* 4.15.1.2 must raise int on a short input packet */
1310 ehci_raise_irq(q
->ehci
, USBSTS_INT
);
1312 q
->ehci
->int_req_by_async
= true;
1318 DPRINTF("updating tbytes to %d\n", tbytes
);
1319 set_field(&q
->qh
.token
, tbytes
, QTD_TOKEN_TBYTES
);
1321 ehci_finish_transfer(q
, p
->packet
.actual_length
);
1322 usb_packet_unmap(&p
->packet
, &p
->sgl
);
1323 qemu_sglist_destroy(&p
->sgl
);
1324 p
->async
= EHCI_ASYNC_NONE
;
1326 q
->qh
.token
^= QTD_TOKEN_DTOGGLE
;
1327 q
->qh
.token
&= ~QTD_TOKEN_ACTIVE
;
1329 if (q
->qh
.token
& QTD_TOKEN_IOC
) {
1330 ehci_raise_irq(q
->ehci
, USBSTS_INT
);
1332 q
->ehci
->int_req_by_async
= true;
1337 /* 4.10.3 returns "again" */
1338 static int ehci_execute(EHCIPacket
*p
, const char *action
)
1344 assert(p
->async
== EHCI_ASYNC_NONE
||
1345 p
->async
== EHCI_ASYNC_INITIALIZED
);
1347 if (!(p
->qtd
.token
& QTD_TOKEN_ACTIVE
)) {
1348 fprintf(stderr
, "Attempting to execute inactive qtd\n");
1352 if (get_field(p
->qtd
.token
, QTD_TOKEN_TBYTES
) > BUFF_SIZE
) {
1353 ehci_trace_guest_bug(p
->queue
->ehci
,
1354 "guest requested more bytes than allowed");
1358 if (!ehci_verify_pid(p
->queue
, &p
->qtd
)) {
1359 ehci_queue_stopped(p
->queue
); /* Mark the ep in the prev dir stopped */
1361 p
->pid
= ehci_get_pid(&p
->qtd
);
1362 p
->queue
->last_pid
= p
->pid
;
1363 endp
= get_field(p
->queue
->qh
.epchar
, QH_EPCHAR_EP
);
1364 ep
= usb_ep_get(p
->queue
->dev
, p
->pid
, endp
);
1366 if (p
->async
== EHCI_ASYNC_NONE
) {
1367 if (ehci_init_transfer(p
) != 0) {
1371 spd
= (p
->pid
== USB_TOKEN_IN
&& NLPTR_TBIT(p
->qtd
.altnext
) == 0);
1372 usb_packet_setup(&p
->packet
, p
->pid
, ep
, 0, p
->qtdaddr
, spd
,
1373 (p
->qtd
.token
& QTD_TOKEN_IOC
) != 0);
1374 usb_packet_map(&p
->packet
, &p
->sgl
);
1375 p
->async
= EHCI_ASYNC_INITIALIZED
;
1378 trace_usb_ehci_packet_action(p
->queue
, p
, action
);
1379 usb_handle_packet(p
->queue
->dev
, &p
->packet
);
1380 DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x "
1381 "status %d actual_length %d\n", p
->queue
->qhaddr
, p
->qtd
.next
,
1382 p
->qtdaddr
, p
->pid
, p
->packet
.iov
.size
, endp
, p
->packet
.status
,
1383 p
->packet
.actual_length
);
1385 if (p
->packet
.actual_length
> BUFF_SIZE
) {
1386 fprintf(stderr
, "ret from usb_handle_packet > BUFF_SIZE\n");
1396 static int ehci_process_itd(EHCIState
*ehci
,
1402 uint32_t i
, len
, pid
, dir
, devaddr
, endp
;
1403 uint32_t pg
, off
, ptr1
, ptr2
, max
, mult
;
1405 ehci
->periodic_sched_active
= PERIODIC_ACTIVE
;
1407 dir
=(itd
->bufptr
[1] & ITD_BUFPTR_DIRECTION
);
1408 devaddr
= get_field(itd
->bufptr
[0], ITD_BUFPTR_DEVADDR
);
1409 endp
= get_field(itd
->bufptr
[0], ITD_BUFPTR_EP
);
1410 max
= get_field(itd
->bufptr
[1], ITD_BUFPTR_MAXPKT
);
1411 mult
= get_field(itd
->bufptr
[2], ITD_BUFPTR_MULT
);
1413 for(i
= 0; i
< 8; i
++) {
1414 if (itd
->transact
[i
] & ITD_XACT_ACTIVE
) {
1415 pg
= get_field(itd
->transact
[i
], ITD_XACT_PGSEL
);
1416 off
= itd
->transact
[i
] & ITD_XACT_OFFSET_MASK
;
1417 len
= get_field(itd
->transact
[i
], ITD_XACT_LENGTH
);
1419 if (len
> max
* mult
) {
1422 if (len
> BUFF_SIZE
|| pg
> 6) {
1426 ptr1
= (itd
->bufptr
[pg
] & ITD_BUFPTR_MASK
);
1427 qemu_sglist_init(&ehci
->isgl
, ehci
->device
, 2, ehci
->as
);
1428 if (off
+ len
> 4096) {
1429 /* transfer crosses page border */
1431 qemu_sglist_destroy(&ehci
->isgl
);
1432 return -1; /* avoid page pg + 1 */
1434 ptr2
= (itd
->bufptr
[pg
+ 1] & ITD_BUFPTR_MASK
);
1435 uint32_t len2
= off
+ len
- 4096;
1436 uint32_t len1
= len
- len2
;
1437 qemu_sglist_add(&ehci
->isgl
, ptr1
+ off
, len1
);
1438 qemu_sglist_add(&ehci
->isgl
, ptr2
, len2
);
1440 qemu_sglist_add(&ehci
->isgl
, ptr1
+ off
, len
);
1443 dev
= ehci_find_device(ehci
, devaddr
);
1445 ehci_trace_guest_bug(ehci
, "no device found");
1448 pid
= dir
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1449 ep
= usb_ep_get(dev
, pid
, endp
);
1450 if (ep
&& ep
->type
== USB_ENDPOINT_XFER_ISOC
) {
1451 usb_packet_setup(&ehci
->ipacket
, pid
, ep
, 0, addr
, false,
1452 (itd
->transact
[i
] & ITD_XACT_IOC
) != 0);
1453 usb_packet_map(&ehci
->ipacket
, &ehci
->isgl
);
1454 usb_handle_packet(dev
, &ehci
->ipacket
);
1455 usb_packet_unmap(&ehci
->ipacket
, &ehci
->isgl
);
1457 DPRINTF("ISOCH: attempt to addess non-iso endpoint\n");
1458 ehci
->ipacket
.status
= USB_RET_NAK
;
1459 ehci
->ipacket
.actual_length
= 0;
1461 qemu_sglist_destroy(&ehci
->isgl
);
1463 switch (ehci
->ipacket
.status
) {
1464 case USB_RET_SUCCESS
:
1467 fprintf(stderr
, "Unexpected iso usb result: %d\n",
1468 ehci
->ipacket
.status
);
1470 case USB_RET_IOERROR
:
1472 /* 3.3.2: XACTERR is only allowed on IN transactions */
1474 itd
->transact
[i
] |= ITD_XACT_XACTERR
;
1475 ehci_raise_irq(ehci
, USBSTS_ERRINT
);
1478 case USB_RET_BABBLE
:
1479 itd
->transact
[i
] |= ITD_XACT_BABBLE
;
1480 ehci_raise_irq(ehci
, USBSTS_ERRINT
);
1483 /* no data for us, so do a zero-length transfer */
1484 ehci
->ipacket
.actual_length
= 0;
1488 set_field(&itd
->transact
[i
], len
- ehci
->ipacket
.actual_length
,
1489 ITD_XACT_LENGTH
); /* OUT */
1491 set_field(&itd
->transact
[i
], ehci
->ipacket
.actual_length
,
1492 ITD_XACT_LENGTH
); /* IN */
1494 if (itd
->transact
[i
] & ITD_XACT_IOC
) {
1495 ehci_raise_irq(ehci
, USBSTS_INT
);
1497 itd
->transact
[i
] &= ~ITD_XACT_ACTIVE
;
1504 /* This state is the entry point for asynchronous schedule
1505 * processing. Entry here consitutes a EHCI start event state (4.8.5)
1507 static int ehci_state_waitlisthead(EHCIState
*ehci
, int async
)
1512 uint32_t entry
= ehci
->asynclistaddr
;
1514 /* set reclamation flag at start event (4.8.6) */
1516 ehci_set_usbsts(ehci
, USBSTS_REC
);
1519 ehci_queues_rip_unused(ehci
, async
);
1521 /* Find the head of the list (4.9.1.1) */
1522 for(i
= 0; i
< MAX_QH
; i
++) {
1523 if (get_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *) &qh
,
1524 sizeof(EHCIqh
) >> 2) < 0) {
1527 ehci_trace_qh(NULL
, NLPTR_GET(entry
), &qh
);
1529 if (qh
.epchar
& QH_EPCHAR_H
) {
1531 entry
|= (NLPTR_TYPE_QH
<< 1);
1534 ehci_set_fetch_addr(ehci
, async
, entry
);
1535 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
1541 if (entry
== ehci
->asynclistaddr
) {
1546 /* no head found for list. */
1548 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1555 /* This state is the entry point for periodic schedule processing as
1556 * well as being a continuation state for async processing.
1558 static int ehci_state_fetchentry(EHCIState
*ehci
, int async
)
1561 uint32_t entry
= ehci_get_fetch_addr(ehci
, async
);
1563 if (NLPTR_TBIT(entry
)) {
1564 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1568 /* section 4.8, only QH in async schedule */
1569 if (async
&& (NLPTR_TYPE_GET(entry
) != NLPTR_TYPE_QH
)) {
1570 fprintf(stderr
, "non queue head request in async schedule\n");
1574 switch (NLPTR_TYPE_GET(entry
)) {
1576 ehci_set_state(ehci
, async
, EST_FETCHQH
);
1580 case NLPTR_TYPE_ITD
:
1581 ehci_set_state(ehci
, async
, EST_FETCHITD
);
1585 case NLPTR_TYPE_STITD
:
1586 ehci_set_state(ehci
, async
, EST_FETCHSITD
);
1591 /* TODO: handle FSTN type */
1592 fprintf(stderr
, "FETCHENTRY: entry at %X is of type %d "
1593 "which is not supported yet\n", entry
, NLPTR_TYPE_GET(entry
));
1601 static EHCIQueue
*ehci_state_fetchqh(EHCIState
*ehci
, int async
)
1607 entry
= ehci_get_fetch_addr(ehci
, async
);
1608 q
= ehci_find_queue_by_qh(ehci
, entry
, async
);
1610 q
= ehci_alloc_queue(ehci
, entry
, async
);
1615 /* we are going in circles -- stop processing */
1616 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1621 if (get_dwords(ehci
, NLPTR_GET(q
->qhaddr
),
1622 (uint32_t *) &qh
, sizeof(EHCIqh
) >> 2) < 0) {
1626 ehci_trace_qh(q
, NLPTR_GET(q
->qhaddr
), &qh
);
1629 * The overlay area of the qh should never be changed by the guest,
1630 * except when idle, in which case the reset is a nop.
1632 if (!ehci_verify_qh(q
, &qh
)) {
1633 if (ehci_reset_queue(q
) > 0) {
1634 ehci_trace_guest_bug(ehci
, "guest updated active QH");
1639 q
->transact_ctr
= get_field(q
->qh
.epcap
, QH_EPCAP_MULT
);
1640 if (q
->transact_ctr
== 0) { /* Guest bug in some versions of windows */
1641 q
->transact_ctr
= 4;
1644 if (q
->dev
== NULL
) {
1645 q
->dev
= ehci_find_device(q
->ehci
,
1646 get_field(q
->qh
.epchar
, QH_EPCHAR_DEVADDR
));
1649 if (async
&& (q
->qh
.epchar
& QH_EPCHAR_H
)) {
1651 /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
1652 if (ehci
->usbsts
& USBSTS_REC
) {
1653 ehci_clear_usbsts(ehci
, USBSTS_REC
);
1655 DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
1656 " - done processing\n", q
->qhaddr
);
1657 ehci_set_state(ehci
, async
, EST_ACTIVE
);
1664 if (q
->qhaddr
!= q
->qh
.next
) {
1665 DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
1667 q
->qh
.epchar
& QH_EPCHAR_H
,
1668 q
->qh
.token
& QTD_TOKEN_HALT
,
1669 q
->qh
.token
& QTD_TOKEN_ACTIVE
,
1674 if (q
->qh
.token
& QTD_TOKEN_HALT
) {
1675 ehci_set_state(ehci
, async
, EST_HORIZONTALQH
);
1677 } else if ((q
->qh
.token
& QTD_TOKEN_ACTIVE
) &&
1678 (NLPTR_TBIT(q
->qh
.current_qtd
) == 0) &&
1679 (q
->qh
.current_qtd
!= 0)) {
1680 q
->qtdaddr
= q
->qh
.current_qtd
;
1681 ehci_set_state(ehci
, async
, EST_FETCHQTD
);
1684 /* EHCI spec version 1.0 Section 4.10.2 */
1685 ehci_set_state(ehci
, async
, EST_ADVANCEQUEUE
);
1692 static int ehci_state_fetchitd(EHCIState
*ehci
, int async
)
1698 entry
= ehci_get_fetch_addr(ehci
, async
);
1700 if (get_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *) &itd
,
1701 sizeof(EHCIitd
) >> 2) < 0) {
1704 ehci_trace_itd(ehci
, entry
, &itd
);
1706 if (ehci_process_itd(ehci
, &itd
, entry
) != 0) {
1710 put_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *) &itd
,
1711 sizeof(EHCIitd
) >> 2);
1712 ehci_set_fetch_addr(ehci
, async
, itd
.next
);
1713 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
1718 static int ehci_state_fetchsitd(EHCIState
*ehci
, int async
)
1724 entry
= ehci_get_fetch_addr(ehci
, async
);
1726 if (get_dwords(ehci
, NLPTR_GET(entry
), (uint32_t *)&sitd
,
1727 sizeof(EHCIsitd
) >> 2) < 0) {
1730 ehci_trace_sitd(ehci
, entry
, &sitd
);
1732 if (!(sitd
.results
& SITD_RESULTS_ACTIVE
)) {
1733 /* siTD is not active, nothing to do */;
1735 /* TODO: split transfers are not implemented */
1736 warn_report("Skipping active siTD");
1739 ehci_set_fetch_addr(ehci
, async
, sitd
.next
);
1740 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
1744 /* Section 4.10.2 - paragraph 3 */
1745 static int ehci_state_advqueue(EHCIQueue
*q
)
1748 /* TO-DO: 4.10.2 - paragraph 2
1749 * if I-bit is set to 1 and QH is not active
1750 * go to horizontal QH
1753 ehci_set_state(ehci
, async
, EST_HORIZONTALQH
);
1759 * want data and alt-next qTD is valid
1761 if (((q
->qh
.token
& QTD_TOKEN_TBYTES_MASK
) != 0) &&
1762 (NLPTR_TBIT(q
->qh
.altnext_qtd
) == 0)) {
1763 q
->qtdaddr
= q
->qh
.altnext_qtd
;
1764 ehci_set_state(q
->ehci
, q
->async
, EST_FETCHQTD
);
1769 } else if (NLPTR_TBIT(q
->qh
.next_qtd
) == 0) {
1770 q
->qtdaddr
= q
->qh
.next_qtd
;
1771 ehci_set_state(q
->ehci
, q
->async
, EST_FETCHQTD
);
1774 * no valid qTD, try next QH
1777 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1783 /* Section 4.10.2 - paragraph 4 */
1784 static int ehci_state_fetchqtd(EHCIQueue
*q
)
1791 addr
= NLPTR_GET(q
->qtdaddr
);
1792 if (get_dwords(q
->ehci
, addr
+ 8, &qtd
.token
, 1) < 0) {
1796 if (get_dwords(q
->ehci
, addr
+ 0, &qtd
.next
, 1) < 0 ||
1797 get_dwords(q
->ehci
, addr
+ 4, &qtd
.altnext
, 1) < 0 ||
1798 get_dwords(q
->ehci
, addr
+ 12, qtd
.bufptr
,
1799 ARRAY_SIZE(qtd
.bufptr
)) < 0) {
1802 ehci_trace_qtd(q
, NLPTR_GET(q
->qtdaddr
), &qtd
);
1804 p
= QTAILQ_FIRST(&q
->packets
);
1806 if (!ehci_verify_qtd(p
, &qtd
)) {
1807 ehci_cancel_queue(q
);
1808 if (qtd
.token
& QTD_TOKEN_ACTIVE
) {
1809 ehci_trace_guest_bug(q
->ehci
, "guest updated active qTD");
1814 ehci_qh_do_overlay(q
);
1818 if (!(qtd
.token
& QTD_TOKEN_ACTIVE
)) {
1819 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1820 } else if (p
!= NULL
) {
1822 case EHCI_ASYNC_NONE
:
1823 case EHCI_ASYNC_INITIALIZED
:
1824 /* Not yet executed (MULT), or previously nacked (int) packet */
1825 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTE
);
1827 case EHCI_ASYNC_INFLIGHT
:
1828 /* Check if the guest has added new tds to the queue */
1829 again
= ehci_fill_queue(QTAILQ_LAST(&q
->packets
));
1830 /* Unfinished async handled packet, go horizontal */
1831 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1833 case EHCI_ASYNC_FINISHED
:
1834 /* Complete executing of the packet */
1835 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTING
);
1839 p
= ehci_alloc_packet(q
);
1840 p
->qtdaddr
= q
->qtdaddr
;
1842 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTE
);
1848 static int ehci_state_horizqh(EHCIQueue
*q
)
1852 if (ehci_get_fetch_addr(q
->ehci
, q
->async
) != q
->qh
.next
) {
1853 ehci_set_fetch_addr(q
->ehci
, q
->async
, q
->qh
.next
);
1854 ehci_set_state(q
->ehci
, q
->async
, EST_FETCHENTRY
);
1857 ehci_set_state(q
->ehci
, q
->async
, EST_ACTIVE
);
1863 /* Returns "again" */
1864 static int ehci_fill_queue(EHCIPacket
*p
)
1866 USBEndpoint
*ep
= p
->packet
.ep
;
1867 EHCIQueue
*q
= p
->queue
;
1868 EHCIqtd qtd
= p
->qtd
;
1872 if (NLPTR_TBIT(qtd
.next
) != 0) {
1877 * Detect circular td lists, Windows creates these, counting on the
1878 * active bit going low after execution to make the queue stop.
1880 QTAILQ_FOREACH(p
, &q
->packets
, next
) {
1881 if (p
->qtdaddr
== qtdaddr
) {
1885 if (get_dwords(q
->ehci
, NLPTR_GET(qtdaddr
),
1886 (uint32_t *) &qtd
, sizeof(EHCIqtd
) >> 2) < 0) {
1889 ehci_trace_qtd(q
, NLPTR_GET(qtdaddr
), &qtd
);
1890 if (!(qtd
.token
& QTD_TOKEN_ACTIVE
)) {
1893 if (!ehci_verify_pid(q
, &qtd
)) {
1894 ehci_trace_guest_bug(q
->ehci
, "guest queued token with wrong pid");
1897 p
= ehci_alloc_packet(q
);
1898 p
->qtdaddr
= qtdaddr
;
1900 if (ehci_execute(p
, "queue") == -1) {
1903 assert(p
->packet
.status
== USB_RET_ASYNC
);
1904 p
->async
= EHCI_ASYNC_INFLIGHT
;
1907 usb_device_flush_ep_queue(ep
->dev
, ep
);
1911 static int ehci_state_execute(EHCIQueue
*q
)
1913 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1917 assert(p
->qtdaddr
== q
->qtdaddr
);
1919 if (ehci_qh_do_overlay(q
) != 0) {
1923 // TODO verify enough time remains in the uframe as in 4.4.1.1
1924 // TODO write back ptr to async list when done or out of time
1926 /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */
1927 if (!q
->async
&& q
->transact_ctr
== 0) {
1928 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1934 ehci_set_usbsts(q
->ehci
, USBSTS_REC
);
1937 again
= ehci_execute(p
, "process");
1941 if (p
->packet
.status
== USB_RET_ASYNC
) {
1943 trace_usb_ehci_packet_action(p
->queue
, p
, "async");
1944 p
->async
= EHCI_ASYNC_INFLIGHT
;
1945 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1947 again
= ehci_fill_queue(p
);
1954 ehci_set_state(q
->ehci
, q
->async
, EST_EXECUTING
);
1961 static int ehci_state_executing(EHCIQueue
*q
)
1963 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1966 assert(p
->qtdaddr
== q
->qtdaddr
);
1968 ehci_execute_complete(q
);
1971 if (!q
->async
&& q
->transact_ctr
> 0) {
1976 if (p
->packet
.status
== USB_RET_NAK
) {
1977 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
1979 ehci_set_state(q
->ehci
, q
->async
, EST_WRITEBACK
);
1987 static int ehci_state_writeback(EHCIQueue
*q
)
1989 EHCIPacket
*p
= QTAILQ_FIRST(&q
->packets
);
1990 uint32_t *qtd
, addr
;
1993 /* Write back the QTD from the QH area */
1995 assert(p
->qtdaddr
== q
->qtdaddr
);
1997 ehci_trace_qtd(q
, NLPTR_GET(p
->qtdaddr
), (EHCIqtd
*) &q
->qh
.next_qtd
);
1998 qtd
= (uint32_t *) &q
->qh
.next_qtd
;
1999 addr
= NLPTR_GET(p
->qtdaddr
);
2000 put_dwords(q
->ehci
, addr
+ 2 * sizeof(uint32_t), qtd
+ 2, 2);
2001 ehci_free_packet(p
);
2004 * EHCI specs say go horizontal here.
2006 * We can also advance the queue here for performance reasons. We
2007 * need to take care to only take that shortcut in case we've
2008 * processed the qtd just written back without errors, i.e. halt
2011 if (q
->qh
.token
& QTD_TOKEN_HALT
) {
2012 ehci_set_state(q
->ehci
, q
->async
, EST_HORIZONTALQH
);
2015 ehci_set_state(q
->ehci
, q
->async
, EST_ADVANCEQUEUE
);
2022 * This is the state machine that is common to both async and periodic
2025 static void ehci_advance_state(EHCIState
*ehci
, int async
)
2027 EHCIQueue
*q
= NULL
;
2032 switch(ehci_get_state(ehci
, async
)) {
2033 case EST_WAITLISTHEAD
:
2034 again
= ehci_state_waitlisthead(ehci
, async
);
2037 case EST_FETCHENTRY
:
2038 again
= ehci_state_fetchentry(ehci
, async
);
2042 q
= ehci_state_fetchqh(ehci
, async
);
2044 assert(q
->async
== async
);
2052 again
= ehci_state_fetchitd(ehci
, async
);
2057 again
= ehci_state_fetchsitd(ehci
, async
);
2061 case EST_ADVANCEQUEUE
:
2063 again
= ehci_state_advqueue(q
);
2068 again
= ehci_state_fetchqtd(q
);
2071 case EST_HORIZONTALQH
:
2073 again
= ehci_state_horizqh(q
);
2078 again
= ehci_state_execute(q
);
2080 ehci
->async_stepdown
= 0;
2087 ehci
->async_stepdown
= 0;
2089 again
= ehci_state_executing(q
);
2094 again
= ehci_state_writeback(q
);
2096 ehci
->periodic_sched_active
= PERIODIC_ACTIVE
;
2101 fprintf(stderr
, "Bad state!\n");
2103 g_assert_not_reached();
2107 if (again
< 0 || itd_count
> 16) {
2108 /* TODO: notify guest (raise HSE irq?) */
2109 fprintf(stderr
, "processing error - resetting ehci HC\n");
2117 static void ehci_advance_async_state(EHCIState
*ehci
)
2119 const int async
= 1;
2121 switch(ehci_get_state(ehci
, async
)) {
2123 if (!ehci_async_enabled(ehci
)) {
2126 ehci_set_state(ehci
, async
, EST_ACTIVE
);
2127 // No break, fall through to ACTIVE
2130 if (!ehci_async_enabled(ehci
)) {
2131 ehci_queues_rip_all(ehci
, async
);
2132 ehci_set_state(ehci
, async
, EST_INACTIVE
);
2136 /* make sure guest has acknowledged the doorbell interrupt */
2137 /* TO-DO: is this really needed? */
2138 if (ehci
->usbsts
& USBSTS_IAA
) {
2139 DPRINTF("IAA status bit still set.\n");
2143 /* check that address register has been set */
2144 if (ehci
->asynclistaddr
== 0) {
2148 ehci_set_state(ehci
, async
, EST_WAITLISTHEAD
);
2149 ehci_advance_state(ehci
, async
);
2151 /* If the doorbell is set, the guest wants to make a change to the
2152 * schedule. The host controller needs to release cached data.
2155 if (ehci
->usbcmd
& USBCMD_IAAD
) {
2156 /* Remove all unseen qhs from the async qhs queue */
2157 ehci_queues_rip_unseen(ehci
, async
);
2158 trace_usb_ehci_doorbell_ack();
2159 ehci
->usbcmd
&= ~USBCMD_IAAD
;
2160 ehci_raise_irq(ehci
, USBSTS_IAA
);
2165 /* this should only be due to a developer mistake */
2166 fprintf(stderr
, "ehci: Bad asynchronous state %d. "
2167 "Resetting to active\n", ehci
->astate
);
2168 g_assert_not_reached();
2172 static void ehci_advance_periodic_state(EHCIState
*ehci
)
2176 const int async
= 0;
2180 switch(ehci_get_state(ehci
, async
)) {
2182 if (!(ehci
->frindex
& 7) && ehci_periodic_enabled(ehci
)) {
2183 ehci_set_state(ehci
, async
, EST_ACTIVE
);
2184 // No break, fall through to ACTIVE
2189 if (!(ehci
->frindex
& 7) && !ehci_periodic_enabled(ehci
)) {
2190 ehci_queues_rip_all(ehci
, async
);
2191 ehci_set_state(ehci
, async
, EST_INACTIVE
);
2195 list
= ehci
->periodiclistbase
& 0xfffff000;
2196 /* check that register has been set */
2200 list
|= ((ehci
->frindex
& 0x1ff8) >> 1);
2202 if (get_dwords(ehci
, list
, &entry
, 1) < 0) {
2206 DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
2207 ehci
->frindex
/ 8, list
, entry
);
2208 ehci_set_fetch_addr(ehci
, async
,entry
);
2209 ehci_set_state(ehci
, async
, EST_FETCHENTRY
);
2210 ehci_advance_state(ehci
, async
);
2211 ehci_queues_rip_unused(ehci
, async
);
2215 /* this should only be due to a developer mistake */
2216 fprintf(stderr
, "ehci: Bad periodic state %d. "
2217 "Resetting to active\n", ehci
->pstate
);
2218 g_assert_not_reached();
2222 static void ehci_update_frindex(EHCIState
*ehci
, int uframes
)
2224 if (!ehci_enabled(ehci
) && ehci
->pstate
== EST_INACTIVE
) {
2228 /* Generate FLR interrupt if frame index rolls over 0x2000 */
2229 if ((ehci
->frindex
% 0x2000) + uframes
>= 0x2000) {
2230 ehci_raise_irq(ehci
, USBSTS_FLR
);
2233 /* How many times will frindex roll over 0x4000 with this frame count?
2234 * usbsts_frindex is decremented by 0x4000 on rollover until it reaches 0
2236 int rollovers
= (ehci
->frindex
+ uframes
) / 0x4000;
2237 if (rollovers
> 0) {
2238 if (ehci
->usbsts_frindex
>= (rollovers
* 0x4000)) {
2239 ehci
->usbsts_frindex
-= 0x4000 * rollovers
;
2241 ehci
->usbsts_frindex
= 0;
2245 ehci
->frindex
= (ehci
->frindex
+ uframes
) % 0x4000;
2248 static void ehci_work_bh(void *opaque
)
2250 EHCIState
*ehci
= opaque
;
2252 int64_t expire_time
, t_now
;
2253 uint64_t ns_elapsed
;
2254 uint64_t uframes
, skipped_uframes
;
2257 if (ehci
->working
) {
2260 ehci
->working
= true;
2262 t_now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2263 ns_elapsed
= t_now
- ehci
->last_run_ns
;
2264 uframes
= ns_elapsed
/ UFRAME_TIMER_NS
;
2266 if (ehci_periodic_enabled(ehci
) || ehci
->pstate
!= EST_INACTIVE
) {
2269 if (uframes
> (ehci
->maxframes
* 8)) {
2270 skipped_uframes
= uframes
- (ehci
->maxframes
* 8);
2271 ehci_update_frindex(ehci
, skipped_uframes
);
2272 ehci
->last_run_ns
+= UFRAME_TIMER_NS
* skipped_uframes
;
2273 uframes
-= skipped_uframes
;
2274 DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes
);
2277 for (i
= 0; i
< uframes
; i
++) {
2279 * If we're running behind schedule, we should not catch up
2280 * too fast, as that will make some guests unhappy:
2281 * 1) We must process a minimum of MIN_UFR_PER_TICK frames,
2282 * otherwise we will never catch up
2283 * 2) Process frames until the guest has requested an irq (IOC)
2285 if (i
>= MIN_UFR_PER_TICK
) {
2286 ehci_commit_irq(ehci
);
2287 if ((ehci
->usbsts
& USBINTR_MASK
) & ehci
->usbintr
) {
2291 if (ehci
->periodic_sched_active
) {
2292 ehci
->periodic_sched_active
--;
2294 ehci_update_frindex(ehci
, 1);
2295 if ((ehci
->frindex
& 7) == 0) {
2296 ehci_advance_periodic_state(ehci
);
2298 ehci
->last_run_ns
+= UFRAME_TIMER_NS
;
2301 ehci
->periodic_sched_active
= 0;
2302 ehci_update_frindex(ehci
, uframes
);
2303 ehci
->last_run_ns
+= UFRAME_TIMER_NS
* uframes
;
2306 if (ehci
->periodic_sched_active
) {
2307 ehci
->async_stepdown
= 0;
2308 } else if (ehci
->async_stepdown
< ehci
->maxframes
/ 2) {
2309 ehci
->async_stepdown
++;
2312 /* Async is not inside loop since it executes everything it can once
2315 if (ehci_async_enabled(ehci
) || ehci
->astate
!= EST_INACTIVE
) {
2317 ehci_advance_async_state(ehci
);
2320 ehci_commit_irq(ehci
);
2321 if (ehci
->usbsts_pending
) {
2323 ehci
->async_stepdown
= 0;
2326 if (ehci_enabled(ehci
) && (ehci
->usbintr
& USBSTS_FLR
)) {
2331 /* If we've raised int, we speed up the timer, so that we quickly
2332 * notice any new packets queued up in response */
2333 if (ehci
->int_req_by_async
&& (ehci
->usbsts
& USBSTS_INT
)) {
2334 expire_time
= t_now
+
2335 NANOSECONDS_PER_SECOND
/ (FRAME_TIMER_FREQ
* 4);
2336 ehci
->int_req_by_async
= false;
2338 expire_time
= t_now
+ (NANOSECONDS_PER_SECOND
2339 * (ehci
->async_stepdown
+1) / FRAME_TIMER_FREQ
);
2341 timer_mod(ehci
->frame_timer
, expire_time
);
2344 ehci
->working
= false;
2347 static void ehci_work_timer(void *opaque
)
2349 EHCIState
*ehci
= opaque
;
2351 qemu_bh_schedule(ehci
->async_bh
);
2354 static const MemoryRegionOps ehci_mmio_caps_ops
= {
2355 .read
= ehci_caps_read
,
2356 .write
= ehci_caps_write
,
2357 .valid
.min_access_size
= 1,
2358 .valid
.max_access_size
= 4,
2359 .impl
.min_access_size
= 1,
2360 .impl
.max_access_size
= 1,
2361 .endianness
= DEVICE_LITTLE_ENDIAN
,
2364 static const MemoryRegionOps ehci_mmio_opreg_ops
= {
2365 .read
= ehci_opreg_read
,
2366 .write
= ehci_opreg_write
,
2367 .valid
.min_access_size
= 4,
2368 .valid
.max_access_size
= 4,
2369 .endianness
= DEVICE_LITTLE_ENDIAN
,
2372 static const MemoryRegionOps ehci_mmio_port_ops
= {
2373 .read
= ehci_port_read
,
2374 .write
= ehci_port_write
,
2375 .valid
.min_access_size
= 4,
2376 .valid
.max_access_size
= 4,
2377 .endianness
= DEVICE_LITTLE_ENDIAN
,
2380 static USBPortOps ehci_port_ops
= {
2381 .attach
= ehci_attach
,
2382 .detach
= ehci_detach
,
2383 .child_detach
= ehci_child_detach
,
2384 .wakeup
= ehci_wakeup
,
2385 .complete
= ehci_async_complete_packet
,
2388 static USBBusOps ehci_bus_ops_companion
= {
2389 .register_companion
= ehci_register_companion
,
2390 .wakeup_endpoint
= ehci_wakeup_endpoint
,
2392 static USBBusOps ehci_bus_ops_standalone
= {
2393 .wakeup_endpoint
= ehci_wakeup_endpoint
,
2396 static int usb_ehci_pre_save(void *opaque
)
2398 EHCIState
*ehci
= opaque
;
2399 uint32_t new_frindex
;
2401 /* Round down frindex to a multiple of 8 for migration compatibility */
2402 new_frindex
= ehci
->frindex
& ~7;
2403 ehci
->last_run_ns
-= (ehci
->frindex
- new_frindex
) * UFRAME_TIMER_NS
;
2404 ehci
->frindex
= new_frindex
;
2409 static int usb_ehci_post_load(void *opaque
, int version_id
)
2411 EHCIState
*s
= opaque
;
2414 for (i
= 0; i
< NB_PORTS
; i
++) {
2415 USBPort
*companion
= s
->companion_ports
[i
];
2416 if (companion
== NULL
) {
2419 if (s
->portsc
[i
] & PORTSC_POWNER
) {
2420 companion
->dev
= s
->ports
[i
].dev
;
2422 companion
->dev
= NULL
;
2429 static void usb_ehci_vm_state_change(void *opaque
, int running
, RunState state
)
2431 EHCIState
*ehci
= opaque
;
2434 * We don't migrate the EHCIQueue-s, instead we rebuild them for the
2435 * schedule in guest memory. We must do the rebuilt ASAP, so that
2436 * USB-devices which have async handled packages have a packet in the
2437 * ep queue to match the completion with.
2439 if (state
== RUN_STATE_RUNNING
) {
2440 ehci_advance_async_state(ehci
);
2444 * The schedule rebuilt from guest memory could cause the migration dest
2445 * to miss a QH unlink, and fail to cancel packets, since the unlinked QH
2446 * will never have existed on the destination. Therefor we must flush the
2447 * async schedule on savevm to catch any not yet noticed unlinks.
2449 if (state
== RUN_STATE_SAVE_VM
) {
2450 ehci_advance_async_state(ehci
);
2451 ehci_queues_rip_unseen(ehci
, 1);
2455 const VMStateDescription vmstate_ehci
= {
2456 .name
= "ehci-core",
2458 .minimum_version_id
= 1,
2459 .pre_save
= usb_ehci_pre_save
,
2460 .post_load
= usb_ehci_post_load
,
2461 .fields
= (VMStateField
[]) {
2462 /* mmio registers */
2463 VMSTATE_UINT32(usbcmd
, EHCIState
),
2464 VMSTATE_UINT32(usbsts
, EHCIState
),
2465 VMSTATE_UINT32_V(usbsts_pending
, EHCIState
, 2),
2466 VMSTATE_UINT32_V(usbsts_frindex
, EHCIState
, 2),
2467 VMSTATE_UINT32(usbintr
, EHCIState
),
2468 VMSTATE_UINT32(frindex
, EHCIState
),
2469 VMSTATE_UINT32(ctrldssegment
, EHCIState
),
2470 VMSTATE_UINT32(periodiclistbase
, EHCIState
),
2471 VMSTATE_UINT32(asynclistaddr
, EHCIState
),
2472 VMSTATE_UINT32(configflag
, EHCIState
),
2473 VMSTATE_UINT32(portsc
[0], EHCIState
),
2474 VMSTATE_UINT32(portsc
[1], EHCIState
),
2475 VMSTATE_UINT32(portsc
[2], EHCIState
),
2476 VMSTATE_UINT32(portsc
[3], EHCIState
),
2477 VMSTATE_UINT32(portsc
[4], EHCIState
),
2478 VMSTATE_UINT32(portsc
[5], EHCIState
),
2480 VMSTATE_TIMER_PTR(frame_timer
, EHCIState
),
2481 VMSTATE_UINT64(last_run_ns
, EHCIState
),
2482 VMSTATE_UINT32(async_stepdown
, EHCIState
),
2483 /* schedule state */
2484 VMSTATE_UINT32(astate
, EHCIState
),
2485 VMSTATE_UINT32(pstate
, EHCIState
),
2486 VMSTATE_UINT32(a_fetch_addr
, EHCIState
),
2487 VMSTATE_UINT32(p_fetch_addr
, EHCIState
),
2488 VMSTATE_END_OF_LIST()
2492 void usb_ehci_realize(EHCIState
*s
, DeviceState
*dev
, Error
**errp
)
2496 if (s
->portnr
> NB_PORTS
) {
2497 error_setg(errp
, "Too many ports! Max. port number is %d.",
2501 if (s
->maxframes
< 8 || s
->maxframes
> 512) {
2502 error_setg(errp
, "maxframes %d out if range (8 .. 512)",
2507 usb_bus_new(&s
->bus
, sizeof(s
->bus
), s
->companion_enable
?
2508 &ehci_bus_ops_companion
: &ehci_bus_ops_standalone
, dev
);
2509 for (i
= 0; i
< s
->portnr
; i
++) {
2510 usb_register_port(&s
->bus
, &s
->ports
[i
], s
, i
, &ehci_port_ops
,
2511 USB_SPEED_MASK_HIGH
);
2512 s
->ports
[i
].dev
= 0;
2515 s
->frame_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ehci_work_timer
, s
);
2516 s
->async_bh
= qemu_bh_new(ehci_work_bh
, s
);
2519 s
->vmstate
= qemu_add_vm_change_state_handler(usb_ehci_vm_state_change
, s
);
2522 void usb_ehci_unrealize(EHCIState
*s
, DeviceState
*dev
, Error
**errp
)
2524 trace_usb_ehci_unrealize();
2526 if (s
->frame_timer
) {
2527 timer_del(s
->frame_timer
);
2528 timer_free(s
->frame_timer
);
2529 s
->frame_timer
= NULL
;
2532 qemu_bh_delete(s
->async_bh
);
2535 ehci_queues_rip_all(s
, 0);
2536 ehci_queues_rip_all(s
, 1);
2538 memory_region_del_subregion(&s
->mem
, &s
->mem_caps
);
2539 memory_region_del_subregion(&s
->mem
, &s
->mem_opreg
);
2540 memory_region_del_subregion(&s
->mem
, &s
->mem_ports
);
2542 usb_bus_release(&s
->bus
);
2545 qemu_del_vm_change_state_handler(s
->vmstate
);
2549 void usb_ehci_init(EHCIState
*s
, DeviceState
*dev
)
2551 /* 2.2 host controller interface version */
2552 s
->caps
[0x00] = (uint8_t)(s
->opregbase
- s
->capsbase
);
2553 s
->caps
[0x01] = 0x00;
2554 s
->caps
[0x02] = 0x00;
2555 s
->caps
[0x03] = 0x01; /* HC version */
2556 s
->caps
[0x04] = s
->portnr
; /* Number of downstream ports */
2557 s
->caps
[0x05] = 0x00; /* No companion ports at present */
2558 s
->caps
[0x06] = 0x00;
2559 s
->caps
[0x07] = 0x00;
2560 s
->caps
[0x08] = 0x80; /* We can cache whole frame, no 64-bit */
2561 s
->caps
[0x0a] = 0x00;
2562 s
->caps
[0x0b] = 0x00;
2564 QTAILQ_INIT(&s
->aqueues
);
2565 QTAILQ_INIT(&s
->pqueues
);
2566 usb_packet_init(&s
->ipacket
);
2568 memory_region_init(&s
->mem
, OBJECT(dev
), "ehci", MMIO_SIZE
);
2569 memory_region_init_io(&s
->mem_caps
, OBJECT(dev
), &ehci_mmio_caps_ops
, s
,
2570 "capabilities", CAPA_SIZE
);
2571 memory_region_init_io(&s
->mem_opreg
, OBJECT(dev
), &ehci_mmio_opreg_ops
, s
,
2572 "operational", s
->portscbase
);
2573 memory_region_init_io(&s
->mem_ports
, OBJECT(dev
), &ehci_mmio_port_ops
, s
,
2574 "ports", 4 * s
->portnr
);
2576 memory_region_add_subregion(&s
->mem
, s
->capsbase
, &s
->mem_caps
);
2577 memory_region_add_subregion(&s
->mem
, s
->opregbase
, &s
->mem_opreg
);
2578 memory_region_add_subregion(&s
->mem
, s
->opregbase
+ s
->portscbase
,
2582 void usb_ehci_finalize(EHCIState
*s
)
2584 usb_packet_cleanup(&s
->ipacket
);
2588 * vim: expandtab ts=4