2 * USB xHCI controller emulation
4 * Copyright (c) 2011 Securiforest
5 * Date: 2011-05-11 ; Author: Hector Martin <hector@marcansoft.com>
6 * Based on usb-ohci.c, emulates Renesas NEC USB 3.0
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu/timer.h"
25 #include "qemu/module.h"
26 #include "qemu/queue.h"
27 #include "migration/vmstate.h"
28 #include "hw/qdev-properties.h"
30 #include "qapi/error.h"
38 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
40 #define DPRINTF(...) do {} while (0)
42 #define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \
43 __func__, __LINE__, _msg); abort(); } while (0)
45 #define TRB_LINK_LIMIT 32
46 #define COMMAND_LIMIT 256
47 #define TRANSFER_LIMIT 256
50 #define LEN_OPER (0x400 + 0x10 * XHCI_MAXPORTS)
51 #define LEN_RUNTIME ((XHCI_MAXINTRS + 1) * 0x20)
52 #define LEN_DOORBELL ((XHCI_MAXSLOTS + 1) * 0x20)
54 #define OFF_OPER LEN_CAP
55 #define OFF_RUNTIME 0x1000
56 #define OFF_DOORBELL 0x2000
58 #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
59 #error Increase OFF_RUNTIME
61 #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
62 #error Increase OFF_DOORBELL
64 #if (OFF_DOORBELL + LEN_DOORBELL) > XHCI_LEN_REGS
65 # error Increase XHCI_LEN_REGS
69 #define USBCMD_RS (1<<0)
70 #define USBCMD_HCRST (1<<1)
71 #define USBCMD_INTE (1<<2)
72 #define USBCMD_HSEE (1<<3)
73 #define USBCMD_LHCRST (1<<7)
74 #define USBCMD_CSS (1<<8)
75 #define USBCMD_CRS (1<<9)
76 #define USBCMD_EWE (1<<10)
77 #define USBCMD_EU3S (1<<11)
79 #define USBSTS_HCH (1<<0)
80 #define USBSTS_HSE (1<<2)
81 #define USBSTS_EINT (1<<3)
82 #define USBSTS_PCD (1<<4)
83 #define USBSTS_SSS (1<<8)
84 #define USBSTS_RSS (1<<9)
85 #define USBSTS_SRE (1<<10)
86 #define USBSTS_CNR (1<<11)
87 #define USBSTS_HCE (1<<12)
90 #define PORTSC_CCS (1<<0)
91 #define PORTSC_PED (1<<1)
92 #define PORTSC_OCA (1<<3)
93 #define PORTSC_PR (1<<4)
94 #define PORTSC_PLS_SHIFT 5
95 #define PORTSC_PLS_MASK 0xf
96 #define PORTSC_PP (1<<9)
97 #define PORTSC_SPEED_SHIFT 10
98 #define PORTSC_SPEED_MASK 0xf
99 #define PORTSC_SPEED_FULL (1<<10)
100 #define PORTSC_SPEED_LOW (2<<10)
101 #define PORTSC_SPEED_HIGH (3<<10)
102 #define PORTSC_SPEED_SUPER (4<<10)
103 #define PORTSC_PIC_SHIFT 14
104 #define PORTSC_PIC_MASK 0x3
105 #define PORTSC_LWS (1<<16)
106 #define PORTSC_CSC (1<<17)
107 #define PORTSC_PEC (1<<18)
108 #define PORTSC_WRC (1<<19)
109 #define PORTSC_OCC (1<<20)
110 #define PORTSC_PRC (1<<21)
111 #define PORTSC_PLC (1<<22)
112 #define PORTSC_CEC (1<<23)
113 #define PORTSC_CAS (1<<24)
114 #define PORTSC_WCE (1<<25)
115 #define PORTSC_WDE (1<<26)
116 #define PORTSC_WOE (1<<27)
117 #define PORTSC_DR (1<<30)
118 #define PORTSC_WPR (1<<31)
120 #define CRCR_RCS (1<<0)
121 #define CRCR_CS (1<<1)
122 #define CRCR_CA (1<<2)
123 #define CRCR_CRR (1<<3)
125 #define IMAN_IP (1<<0)
126 #define IMAN_IE (1<<1)
128 #define ERDP_EHB (1<<3)
131 typedef struct XHCITRB
{
150 PLS_COMPILANCE_MODE
= 10,
155 #define CR_LINK TR_LINK
158 #define TRB_TYPE_SHIFT 10
159 #define TRB_TYPE_MASK 0x3f
160 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
162 #define TRB_EV_ED (1<<2)
164 #define TRB_TR_ENT (1<<1)
165 #define TRB_TR_ISP (1<<2)
166 #define TRB_TR_NS (1<<3)
167 #define TRB_TR_CH (1<<4)
168 #define TRB_TR_IOC (1<<5)
169 #define TRB_TR_IDT (1<<6)
170 #define TRB_TR_TBC_SHIFT 7
171 #define TRB_TR_TBC_MASK 0x3
172 #define TRB_TR_BEI (1<<9)
173 #define TRB_TR_TLBPC_SHIFT 16
174 #define TRB_TR_TLBPC_MASK 0xf
175 #define TRB_TR_FRAMEID_SHIFT 20
176 #define TRB_TR_FRAMEID_MASK 0x7ff
177 #define TRB_TR_SIA (1<<31)
179 #define TRB_TR_DIR (1<<16)
181 #define TRB_CR_SLOTID_SHIFT 24
182 #define TRB_CR_SLOTID_MASK 0xff
183 #define TRB_CR_EPID_SHIFT 16
184 #define TRB_CR_EPID_MASK 0x1f
186 #define TRB_CR_BSR (1<<9)
187 #define TRB_CR_DC (1<<9)
189 #define TRB_LK_TC (1<<1)
191 #define TRB_INTR_SHIFT 22
192 #define TRB_INTR_MASK 0x3ff
193 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
195 #define EP_TYPE_MASK 0x7
196 #define EP_TYPE_SHIFT 3
198 #define EP_STATE_MASK 0x7
199 #define EP_DISABLED (0<<0)
200 #define EP_RUNNING (1<<0)
201 #define EP_HALTED (2<<0)
202 #define EP_STOPPED (3<<0)
203 #define EP_ERROR (4<<0)
205 #define SLOT_STATE_MASK 0x1f
206 #define SLOT_STATE_SHIFT 27
207 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
208 #define SLOT_ENABLED 0
209 #define SLOT_DEFAULT 1
210 #define SLOT_ADDRESSED 2
211 #define SLOT_CONFIGURED 3
213 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
214 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
216 #define get_field(data, field) \
217 (((data) >> field##_SHIFT) & field##_MASK)
219 #define set_field(data, newval, field) do { \
220 uint32_t val_ = *data; \
221 val_ &= ~(field##_MASK << field##_SHIFT); \
222 val_ |= ((newval) & field##_MASK) << field##_SHIFT; \
226 typedef enum EPType
{
237 typedef struct XHCITransfer
{
238 XHCIEPContext
*epctx
;
245 unsigned int iso_pkts
;
246 unsigned int streamid
;
251 unsigned int trb_count
;
257 unsigned int pktsize
;
258 unsigned int cur_pkt
;
260 uint64_t mfindex_kick
;
262 QTAILQ_ENTRY(XHCITransfer
) next
;
265 struct XHCIStreamContext
{
271 struct XHCIEPContext
{
278 QTAILQ_HEAD(, XHCITransfer
) transfers
;
282 unsigned int max_psize
;
284 uint32_t kick_active
;
287 unsigned int max_pstreams
;
289 unsigned int nr_pstreams
;
290 XHCIStreamContext
*pstreams
;
292 /* iso xfer scheduling */
293 unsigned int interval
;
294 int64_t mfindex_last
;
295 QEMUTimer
*kick_timer
;
298 typedef struct XHCIEvRingSeg
{
305 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
306 unsigned int epid
, unsigned int streamid
);
307 static void xhci_kick_epctx(XHCIEPContext
*epctx
, unsigned int streamid
);
308 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
310 static void xhci_xfer_report(XHCITransfer
*xfer
);
311 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
312 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
313 static USBEndpoint
*xhci_epid_to_usbep(XHCIEPContext
*epctx
);
315 static const char *TRBType_names
[] = {
316 [TRB_RESERVED
] = "TRB_RESERVED",
317 [TR_NORMAL
] = "TR_NORMAL",
318 [TR_SETUP
] = "TR_SETUP",
319 [TR_DATA
] = "TR_DATA",
320 [TR_STATUS
] = "TR_STATUS",
321 [TR_ISOCH
] = "TR_ISOCH",
322 [TR_LINK
] = "TR_LINK",
323 [TR_EVDATA
] = "TR_EVDATA",
324 [TR_NOOP
] = "TR_NOOP",
325 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
326 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
327 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
328 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
329 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
330 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
331 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
332 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
333 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
334 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
335 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
336 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
337 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
338 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
339 [CR_NOOP
] = "CR_NOOP",
340 [ER_TRANSFER
] = "ER_TRANSFER",
341 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
342 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
343 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
344 [ER_DOORBELL
] = "ER_DOORBELL",
345 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
346 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
347 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
348 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
349 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
352 static const char *TRBCCode_names
[] = {
353 [CC_INVALID
] = "CC_INVALID",
354 [CC_SUCCESS
] = "CC_SUCCESS",
355 [CC_DATA_BUFFER_ERROR
] = "CC_DATA_BUFFER_ERROR",
356 [CC_BABBLE_DETECTED
] = "CC_BABBLE_DETECTED",
357 [CC_USB_TRANSACTION_ERROR
] = "CC_USB_TRANSACTION_ERROR",
358 [CC_TRB_ERROR
] = "CC_TRB_ERROR",
359 [CC_STALL_ERROR
] = "CC_STALL_ERROR",
360 [CC_RESOURCE_ERROR
] = "CC_RESOURCE_ERROR",
361 [CC_BANDWIDTH_ERROR
] = "CC_BANDWIDTH_ERROR",
362 [CC_NO_SLOTS_ERROR
] = "CC_NO_SLOTS_ERROR",
363 [CC_INVALID_STREAM_TYPE_ERROR
] = "CC_INVALID_STREAM_TYPE_ERROR",
364 [CC_SLOT_NOT_ENABLED_ERROR
] = "CC_SLOT_NOT_ENABLED_ERROR",
365 [CC_EP_NOT_ENABLED_ERROR
] = "CC_EP_NOT_ENABLED_ERROR",
366 [CC_SHORT_PACKET
] = "CC_SHORT_PACKET",
367 [CC_RING_UNDERRUN
] = "CC_RING_UNDERRUN",
368 [CC_RING_OVERRUN
] = "CC_RING_OVERRUN",
369 [CC_VF_ER_FULL
] = "CC_VF_ER_FULL",
370 [CC_PARAMETER_ERROR
] = "CC_PARAMETER_ERROR",
371 [CC_BANDWIDTH_OVERRUN
] = "CC_BANDWIDTH_OVERRUN",
372 [CC_CONTEXT_STATE_ERROR
] = "CC_CONTEXT_STATE_ERROR",
373 [CC_NO_PING_RESPONSE_ERROR
] = "CC_NO_PING_RESPONSE_ERROR",
374 [CC_EVENT_RING_FULL_ERROR
] = "CC_EVENT_RING_FULL_ERROR",
375 [CC_INCOMPATIBLE_DEVICE_ERROR
] = "CC_INCOMPATIBLE_DEVICE_ERROR",
376 [CC_MISSED_SERVICE_ERROR
] = "CC_MISSED_SERVICE_ERROR",
377 [CC_COMMAND_RING_STOPPED
] = "CC_COMMAND_RING_STOPPED",
378 [CC_COMMAND_ABORTED
] = "CC_COMMAND_ABORTED",
379 [CC_STOPPED
] = "CC_STOPPED",
380 [CC_STOPPED_LENGTH_INVALID
] = "CC_STOPPED_LENGTH_INVALID",
381 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
]
382 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
383 [CC_ISOCH_BUFFER_OVERRUN
] = "CC_ISOCH_BUFFER_OVERRUN",
384 [CC_EVENT_LOST_ERROR
] = "CC_EVENT_LOST_ERROR",
385 [CC_UNDEFINED_ERROR
] = "CC_UNDEFINED_ERROR",
386 [CC_INVALID_STREAM_ID_ERROR
] = "CC_INVALID_STREAM_ID_ERROR",
387 [CC_SECONDARY_BANDWIDTH_ERROR
] = "CC_SECONDARY_BANDWIDTH_ERROR",
388 [CC_SPLIT_TRANSACTION_ERROR
] = "CC_SPLIT_TRANSACTION_ERROR",
391 static const char *ep_state_names
[] = {
392 [EP_DISABLED
] = "disabled",
393 [EP_RUNNING
] = "running",
394 [EP_HALTED
] = "halted",
395 [EP_STOPPED
] = "stopped",
396 [EP_ERROR
] = "error",
399 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
401 if (index
>= llen
|| list
[index
] == NULL
) {
407 static const char *trb_name(XHCITRB
*trb
)
409 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
410 ARRAY_SIZE(TRBType_names
));
413 static const char *event_name(XHCIEvent
*event
)
415 return lookup_name(event
->ccode
, TRBCCode_names
,
416 ARRAY_SIZE(TRBCCode_names
));
419 static const char *ep_state_name(uint32_t state
)
421 return lookup_name(state
, ep_state_names
,
422 ARRAY_SIZE(ep_state_names
));
425 bool xhci_get_flag(XHCIState
*xhci
, enum xhci_flags bit
)
427 return xhci
->flags
& (1 << bit
);
430 void xhci_set_flag(XHCIState
*xhci
, enum xhci_flags bit
)
432 xhci
->flags
|= (1 << bit
);
435 static uint64_t xhci_mfindex_get(XHCIState
*xhci
)
437 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
438 return (now
- xhci
->mfindex_start
) / 125000;
441 static void xhci_mfwrap_update(XHCIState
*xhci
)
443 const uint32_t bits
= USBCMD_RS
| USBCMD_EWE
;
444 uint32_t mfindex
, left
;
447 if ((xhci
->usbcmd
& bits
) == bits
) {
448 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
449 mfindex
= ((now
- xhci
->mfindex_start
) / 125000) & 0x3fff;
450 left
= 0x4000 - mfindex
;
451 timer_mod(xhci
->mfwrap_timer
, now
+ left
* 125000);
453 timer_del(xhci
->mfwrap_timer
);
457 static void xhci_mfwrap_timer(void *opaque
)
459 XHCIState
*xhci
= opaque
;
460 XHCIEvent wrap
= { ER_MFINDEX_WRAP
, CC_SUCCESS
};
462 xhci_event(xhci
, &wrap
, 0);
463 xhci_mfwrap_update(xhci
);
466 static void xhci_die(XHCIState
*xhci
)
468 xhci
->usbsts
|= USBSTS_HCE
;
469 DPRINTF("xhci: asserted controller error\n");
472 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
474 if (sizeof(dma_addr_t
) == 4) {
477 return low
| (((dma_addr_t
)high
<< 16) << 16);
481 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
483 if (sizeof(dma_addr_t
) == 4) {
484 return addr
& 0xffffffff;
490 static inline void xhci_dma_read_u32s(XHCIState
*xhci
, dma_addr_t addr
,
491 uint32_t *buf
, size_t len
)
495 assert((len
% sizeof(uint32_t)) == 0);
497 if (dma_memory_read(xhci
->as
, addr
, buf
, len
,
498 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
499 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory access failed!\n",
501 memset(buf
, 0xff, len
);
506 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
507 buf
[i
] = le32_to_cpu(buf
[i
]);
511 static inline void xhci_dma_write_u32s(XHCIState
*xhci
, dma_addr_t addr
,
512 const uint32_t *buf
, size_t len
)
516 uint32_t n
= len
/ sizeof(uint32_t);
518 assert((len
% sizeof(uint32_t)) == 0);
519 assert(n
<= ARRAY_SIZE(tmp
));
521 for (i
= 0; i
< n
; i
++) {
522 tmp
[i
] = cpu_to_le32(buf
[i
]);
524 if (dma_memory_write(xhci
->as
, addr
, tmp
, len
,
525 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
526 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory access failed!\n",
533 static XHCIPort
*xhci_lookup_port(XHCIState
*xhci
, struct USBPort
*uport
)
540 switch (uport
->dev
->speed
) {
544 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
545 index
= uport
->index
+ xhci
->numports_3
;
547 index
= uport
->index
;
550 case USB_SPEED_SUPER
:
551 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
552 index
= uport
->index
;
554 index
= uport
->index
+ xhci
->numports_2
;
560 return &xhci
->ports
[index
];
563 static void xhci_intr_update(XHCIState
*xhci
, int v
)
568 if (xhci
->intr
[0].iman
& IMAN_IP
&&
569 xhci
->intr
[0].iman
& IMAN_IE
&&
570 xhci
->usbcmd
& USBCMD_INTE
) {
573 if (xhci
->intr_raise
) {
574 if (xhci
->intr_raise(xhci
, 0, level
)) {
575 xhci
->intr
[0].iman
&= ~IMAN_IP
;
579 if (xhci
->intr_update
) {
580 xhci
->intr_update(xhci
, v
,
581 xhci
->intr
[v
].iman
& IMAN_IE
);
585 static void xhci_intr_raise(XHCIState
*xhci
, int v
)
587 bool pending
= (xhci
->intr
[v
].erdp_low
& ERDP_EHB
);
589 xhci
->intr
[v
].erdp_low
|= ERDP_EHB
;
590 xhci
->intr
[v
].iman
|= IMAN_IP
;
591 xhci
->usbsts
|= USBSTS_EINT
;
596 if (!(xhci
->intr
[v
].iman
& IMAN_IE
)) {
600 if (!(xhci
->usbcmd
& USBCMD_INTE
)) {
603 if (xhci
->intr_raise
) {
604 if (xhci
->intr_raise(xhci
, v
, true)) {
605 xhci
->intr
[v
].iman
&= ~IMAN_IP
;
610 static inline int xhci_running(XHCIState
*xhci
)
612 return !(xhci
->usbsts
& USBSTS_HCH
);
615 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
617 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
621 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
622 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
623 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
624 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
626 ev_trb
.control
|= TRB_C
;
628 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
630 trace_usb_xhci_queue_event(v
, intr
->er_ep_idx
, trb_name(&ev_trb
),
631 event_name(event
), ev_trb
.parameter
,
632 ev_trb
.status
, ev_trb
.control
);
634 addr
= intr
->er_start
+ TRB_SIZE
*intr
->er_ep_idx
;
635 if (dma_memory_write(xhci
->as
, addr
, &ev_trb
, TRB_SIZE
,
636 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
637 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory access failed!\n",
643 if (intr
->er_ep_idx
>= intr
->er_size
) {
645 intr
->er_pcs
= !intr
->er_pcs
;
649 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
651 XHCIInterrupter
*intr
;
655 if (v
>= xhci
->numintrs
) {
656 DPRINTF("intr nr out of range (%d >= %d)\n", v
, xhci
->numintrs
);
659 intr
= &xhci
->intr
[v
];
661 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
662 if (erdp
< intr
->er_start
||
663 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
664 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
665 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
666 v
, intr
->er_start
, intr
->er_size
);
671 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
672 assert(dp_idx
< intr
->er_size
);
674 if ((intr
->er_ep_idx
+ 2) % intr
->er_size
== dp_idx
) {
675 DPRINTF("xhci: ER %d full, send ring full error\n", v
);
676 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
677 xhci_write_event(xhci
, &full
, v
);
678 } else if ((intr
->er_ep_idx
+ 1) % intr
->er_size
== dp_idx
) {
679 DPRINTF("xhci: ER %d full, drop event\n", v
);
681 xhci_write_event(xhci
, event
, v
);
684 xhci_intr_raise(xhci
, v
);
687 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
690 ring
->dequeue
= base
;
694 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
697 uint32_t link_cnt
= 0;
701 if (dma_memory_read(xhci
->as
, ring
->dequeue
, trb
, TRB_SIZE
,
702 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
703 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory access failed!\n",
707 trb
->addr
= ring
->dequeue
;
708 trb
->ccs
= ring
->ccs
;
709 le64_to_cpus(&trb
->parameter
);
710 le32_to_cpus(&trb
->status
);
711 le32_to_cpus(&trb
->control
);
713 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
714 trb
->parameter
, trb
->status
, trb
->control
);
716 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
720 type
= TRB_TYPE(*trb
);
722 if (type
!= TR_LINK
) {
724 *addr
= ring
->dequeue
;
726 ring
->dequeue
+= TRB_SIZE
;
729 if (++link_cnt
> TRB_LINK_LIMIT
) {
730 trace_usb_xhci_enforced_limit("trb-link");
733 ring
->dequeue
= xhci_mask64(trb
->parameter
);
734 if (trb
->control
& TRB_LK_TC
) {
735 ring
->ccs
= !ring
->ccs
;
741 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
745 dma_addr_t dequeue
= ring
->dequeue
;
746 bool ccs
= ring
->ccs
;
747 /* hack to bundle together the two/three TDs that make a setup transfer */
748 bool control_td_set
= 0;
749 uint32_t link_cnt
= 0;
753 if (dma_memory_read(xhci
->as
, dequeue
, &trb
, TRB_SIZE
,
754 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
755 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory access failed!\n",
759 le64_to_cpus(&trb
.parameter
);
760 le32_to_cpus(&trb
.status
);
761 le32_to_cpus(&trb
.control
);
763 if ((trb
.control
& TRB_C
) != ccs
) {
767 type
= TRB_TYPE(trb
);
769 if (type
== TR_LINK
) {
770 if (++link_cnt
> TRB_LINK_LIMIT
) {
773 dequeue
= xhci_mask64(trb
.parameter
);
774 if (trb
.control
& TRB_LK_TC
) {
783 if (type
== TR_SETUP
) {
785 } else if (type
== TR_STATUS
) {
789 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
794 * According to the xHCI spec, Transfer Ring segments should have
795 * a maximum size of 64 kB (see chapter "6 Data Structures")
797 } while (length
< TRB_LINK_LIMIT
* 65536 / TRB_SIZE
);
799 qemu_log_mask(LOG_GUEST_ERROR
, "%s: exceeded maximum transfer ring size!\n",
805 static void xhci_er_reset(XHCIState
*xhci
, int v
)
807 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
809 dma_addr_t erstba
= xhci_addr64(intr
->erstba_low
, intr
->erstba_high
);
811 if (intr
->erstsz
== 0 || erstba
== 0) {
817 /* cache the (sole) event ring segment location */
818 if (intr
->erstsz
!= 1) {
819 DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr
->erstsz
);
823 if (dma_memory_read(xhci
->as
, erstba
, &seg
, sizeof(seg
),
824 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
825 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory access failed!\n",
831 le32_to_cpus(&seg
.addr_low
);
832 le32_to_cpus(&seg
.addr_high
);
833 le32_to_cpus(&seg
.size
);
834 if (seg
.size
< 16 || seg
.size
> 4096) {
835 DPRINTF("xhci: invalid value for segment size: %d\n", seg
.size
);
839 intr
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
840 intr
->er_size
= seg
.size
;
845 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT
" [%d]\n",
846 v
, intr
->er_start
, intr
->er_size
);
849 static void xhci_run(XHCIState
*xhci
)
851 trace_usb_xhci_run();
852 xhci
->usbsts
&= ~USBSTS_HCH
;
853 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
856 static void xhci_stop(XHCIState
*xhci
)
858 trace_usb_xhci_stop();
859 xhci
->usbsts
|= USBSTS_HCH
;
860 xhci
->crcr_low
&= ~CRCR_CRR
;
863 static XHCIStreamContext
*xhci_alloc_stream_contexts(unsigned count
,
866 XHCIStreamContext
*stctx
;
869 stctx
= g_new0(XHCIStreamContext
, count
);
870 for (i
= 0; i
< count
; i
++) {
871 stctx
[i
].pctx
= base
+ i
* 16;
877 static void xhci_reset_streams(XHCIEPContext
*epctx
)
881 for (i
= 0; i
< epctx
->nr_pstreams
; i
++) {
882 epctx
->pstreams
[i
].sct
= -1;
886 static void xhci_alloc_streams(XHCIEPContext
*epctx
, dma_addr_t base
)
888 assert(epctx
->pstreams
== NULL
);
889 epctx
->nr_pstreams
= 2 << epctx
->max_pstreams
;
890 epctx
->pstreams
= xhci_alloc_stream_contexts(epctx
->nr_pstreams
, base
);
893 static void xhci_free_streams(XHCIEPContext
*epctx
)
895 assert(epctx
->pstreams
!= NULL
);
897 g_free(epctx
->pstreams
);
898 epctx
->pstreams
= NULL
;
899 epctx
->nr_pstreams
= 0;
902 static int xhci_epmask_to_eps_with_streams(XHCIState
*xhci
,
905 XHCIEPContext
**epctxs
,
909 XHCIEPContext
*epctx
;
913 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
915 slot
= &xhci
->slots
[slotid
- 1];
917 for (i
= 2, j
= 0; i
<= 31; i
++) {
918 if (!(epmask
& (1u << i
))) {
922 epctx
= slot
->eps
[i
- 1];
923 ep
= xhci_epid_to_usbep(epctx
);
924 if (!epctx
|| !epctx
->nr_pstreams
|| !ep
) {
936 static void xhci_free_device_streams(XHCIState
*xhci
, unsigned int slotid
,
939 USBEndpoint
*eps
[30];
942 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, NULL
, eps
);
944 usb_device_free_streams(eps
[0]->dev
, eps
, nr_eps
);
948 static TRBCCode
xhci_alloc_device_streams(XHCIState
*xhci
, unsigned int slotid
,
951 XHCIEPContext
*epctxs
[30];
952 USBEndpoint
*eps
[30];
953 int i
, r
, nr_eps
, req_nr_streams
, dev_max_streams
;
955 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, epctxs
,
961 req_nr_streams
= epctxs
[0]->nr_pstreams
;
962 dev_max_streams
= eps
[0]->max_streams
;
964 for (i
= 1; i
< nr_eps
; i
++) {
966 * HdG: I don't expect these to ever trigger, but if they do we need
967 * to come up with another solution, ie group identical endpoints
968 * together and make an usb_device_alloc_streams call per group.
970 if (epctxs
[i
]->nr_pstreams
!= req_nr_streams
) {
971 FIXME("guest streams config not identical for all eps");
972 return CC_RESOURCE_ERROR
;
974 if (eps
[i
]->max_streams
!= dev_max_streams
) {
975 FIXME("device streams config not identical for all eps");
976 return CC_RESOURCE_ERROR
;
981 * max-streams in both the device descriptor and in the controller is a
982 * power of 2. But stream id 0 is reserved, so if a device can do up to 4
983 * streams the guest will ask for 5 rounded up to the next power of 2 which
984 * becomes 8. For emulated devices usb_device_alloc_streams is a nop.
986 * For redirected devices however this is an issue, as there we must ask
987 * the real xhci controller to alloc streams, and the host driver for the
988 * real xhci controller will likely disallow allocating more streams then
989 * the device can handle.
991 * So we limit the requested nr_streams to the maximum number the device
994 if (req_nr_streams
> dev_max_streams
) {
995 req_nr_streams
= dev_max_streams
;
998 r
= usb_device_alloc_streams(eps
[0]->dev
, eps
, nr_eps
, req_nr_streams
);
1000 DPRINTF("xhci: alloc streams failed\n");
1001 return CC_RESOURCE_ERROR
;
1007 static XHCIStreamContext
*xhci_find_stream(XHCIEPContext
*epctx
,
1008 unsigned int streamid
,
1011 XHCIStreamContext
*sctx
;
1013 uint32_t ctx
[2], sct
;
1015 assert(streamid
!= 0);
1017 if (streamid
>= epctx
->nr_pstreams
) {
1018 *cc_error
= CC_INVALID_STREAM_ID_ERROR
;
1021 sctx
= epctx
->pstreams
+ streamid
;
1023 fprintf(stderr
, "xhci: FIXME: secondary streams not implemented yet");
1024 *cc_error
= CC_INVALID_STREAM_TYPE_ERROR
;
1028 if (sctx
->sct
== -1) {
1029 xhci_dma_read_u32s(epctx
->xhci
, sctx
->pctx
, ctx
, sizeof(ctx
));
1030 sct
= (ctx
[0] >> 1) & 0x07;
1031 if (epctx
->lsa
&& sct
!= 1) {
1032 *cc_error
= CC_INVALID_STREAM_TYPE_ERROR
;
1036 base
= xhci_addr64(ctx
[0] & ~0xf, ctx
[1]);
1037 xhci_ring_init(epctx
->xhci
, &sctx
->ring
, base
);
1042 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
1043 XHCIStreamContext
*sctx
, uint32_t state
)
1045 XHCIRing
*ring
= NULL
;
1049 xhci_dma_read_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1050 ctx
[0] &= ~EP_STATE_MASK
;
1053 /* update ring dequeue ptr */
1054 if (epctx
->nr_pstreams
) {
1057 xhci_dma_read_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1059 ctx2
[0] |= sctx
->ring
.dequeue
| sctx
->ring
.ccs
;
1060 ctx2
[1] = (sctx
->ring
.dequeue
>> 16) >> 16;
1061 xhci_dma_write_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1064 ring
= &epctx
->ring
;
1067 ctx
[2] = ring
->dequeue
| ring
->ccs
;
1068 ctx
[3] = (ring
->dequeue
>> 16) >> 16;
1070 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
1071 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
1074 xhci_dma_write_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1075 if (epctx
->state
!= state
) {
1076 trace_usb_xhci_ep_state(epctx
->slotid
, epctx
->epid
,
1077 ep_state_name(epctx
->state
),
1078 ep_state_name(state
));
1080 epctx
->state
= state
;
1083 static void xhci_ep_kick_timer(void *opaque
)
1085 XHCIEPContext
*epctx
= opaque
;
1086 xhci_kick_epctx(epctx
, 0);
1089 static XHCIEPContext
*xhci_alloc_epctx(XHCIState
*xhci
,
1090 unsigned int slotid
,
1093 XHCIEPContext
*epctx
;
1095 epctx
= g_new0(XHCIEPContext
, 1);
1097 epctx
->slotid
= slotid
;
1100 QTAILQ_INIT(&epctx
->transfers
);
1101 epctx
->kick_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_ep_kick_timer
, epctx
);
1106 static void xhci_init_epctx(XHCIEPContext
*epctx
,
1107 dma_addr_t pctx
, uint32_t *ctx
)
1111 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
1113 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
1115 epctx
->max_psize
= ctx
[1]>>16;
1116 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
1117 epctx
->max_pstreams
= (ctx
[0] >> 10) & epctx
->xhci
->max_pstreams_mask
;
1118 epctx
->lsa
= (ctx
[0] >> 15) & 1;
1119 if (epctx
->max_pstreams
) {
1120 xhci_alloc_streams(epctx
, dequeue
);
1122 xhci_ring_init(epctx
->xhci
, &epctx
->ring
, dequeue
);
1123 epctx
->ring
.ccs
= ctx
[2] & 1;
1126 epctx
->interval
= 1 << ((ctx
[0] >> 16) & 0xff);
1129 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
1130 unsigned int epid
, dma_addr_t pctx
,
1134 XHCIEPContext
*epctx
;
1136 trace_usb_xhci_ep_enable(slotid
, epid
);
1137 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1138 assert(epid
>= 1 && epid
<= 31);
1140 slot
= &xhci
->slots
[slotid
-1];
1141 if (slot
->eps
[epid
-1]) {
1142 xhci_disable_ep(xhci
, slotid
, epid
);
1145 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
1146 slot
->eps
[epid
-1] = epctx
;
1147 xhci_init_epctx(epctx
, pctx
, ctx
);
1149 DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) "
1150 "size is %d\n", epid
/2, epid
%2, epctx
->type
, epctx
->max_psize
);
1152 epctx
->mfindex_last
= 0;
1154 epctx
->state
= EP_RUNNING
;
1155 ctx
[0] &= ~EP_STATE_MASK
;
1156 ctx
[0] |= EP_RUNNING
;
1161 static XHCITransfer
*xhci_ep_alloc_xfer(XHCIEPContext
*epctx
,
1164 uint32_t limit
= epctx
->nr_pstreams
+ 16;
1167 if (epctx
->xfer_count
>= limit
) {
1171 xfer
= g_new0(XHCITransfer
, 1);
1172 xfer
->epctx
= epctx
;
1173 xfer
->trbs
= g_new(XHCITRB
, length
);
1174 xfer
->trb_count
= length
;
1175 usb_packet_init(&xfer
->packet
);
1177 QTAILQ_INSERT_TAIL(&epctx
->transfers
, xfer
, next
);
1178 epctx
->xfer_count
++;
1183 static void xhci_ep_free_xfer(XHCITransfer
*xfer
)
1185 QTAILQ_REMOVE(&xfer
->epctx
->transfers
, xfer
, next
);
1186 xfer
->epctx
->xfer_count
--;
1188 usb_packet_cleanup(&xfer
->packet
);
1193 static int xhci_ep_nuke_one_xfer(XHCITransfer
*t
, TRBCCode report
)
1197 if (report
&& (t
->running_async
|| t
->running_retry
)) {
1199 xhci_xfer_report(t
);
1202 if (t
->running_async
) {
1203 usb_cancel_packet(&t
->packet
);
1204 t
->running_async
= 0;
1207 if (t
->running_retry
) {
1209 t
->epctx
->retry
= NULL
;
1210 timer_del(t
->epctx
->kick_timer
);
1212 t
->running_retry
= 0;
1223 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
1224 unsigned int epid
, TRBCCode report
)
1227 XHCIEPContext
*epctx
;
1230 USBEndpoint
*ep
= NULL
;
1231 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1232 assert(epid
>= 1 && epid
<= 31);
1234 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
1236 slot
= &xhci
->slots
[slotid
-1];
1238 if (!slot
->eps
[epid
-1]) {
1242 epctx
= slot
->eps
[epid
-1];
1245 xfer
= QTAILQ_FIRST(&epctx
->transfers
);
1249 killed
+= xhci_ep_nuke_one_xfer(xfer
, report
);
1251 report
= 0; /* Only report once */
1253 xhci_ep_free_xfer(xfer
);
1256 ep
= xhci_epid_to_usbep(epctx
);
1258 usb_device_ep_stopped(ep
->dev
, ep
);
1263 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
1267 XHCIEPContext
*epctx
;
1269 trace_usb_xhci_ep_disable(slotid
, epid
);
1270 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1271 assert(epid
>= 1 && epid
<= 31);
1273 slot
= &xhci
->slots
[slotid
-1];
1275 if (!slot
->eps
[epid
-1]) {
1276 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
1280 xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0);
1282 epctx
= slot
->eps
[epid
-1];
1284 if (epctx
->nr_pstreams
) {
1285 xhci_free_streams(epctx
);
1288 /* only touch guest RAM if we're not resetting the HC */
1289 if (xhci
->dcbaap_low
|| xhci
->dcbaap_high
) {
1290 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_DISABLED
);
1293 timer_free(epctx
->kick_timer
);
1295 slot
->eps
[epid
-1] = NULL
;
1300 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
1304 XHCIEPContext
*epctx
;
1306 trace_usb_xhci_ep_stop(slotid
, epid
);
1307 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1309 if (epid
< 1 || epid
> 31) {
1310 DPRINTF("xhci: bad ep %d\n", epid
);
1311 return CC_TRB_ERROR
;
1314 slot
= &xhci
->slots
[slotid
-1];
1316 if (!slot
->eps
[epid
-1]) {
1317 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1318 return CC_EP_NOT_ENABLED_ERROR
;
1321 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, CC_STOPPED
) > 0) {
1322 DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, "
1323 "data might be lost\n");
1326 epctx
= slot
->eps
[epid
-1];
1328 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1330 if (epctx
->nr_pstreams
) {
1331 xhci_reset_streams(epctx
);
1337 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
1341 XHCIEPContext
*epctx
;
1343 trace_usb_xhci_ep_reset(slotid
, epid
);
1344 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1346 if (epid
< 1 || epid
> 31) {
1347 DPRINTF("xhci: bad ep %d\n", epid
);
1348 return CC_TRB_ERROR
;
1351 slot
= &xhci
->slots
[slotid
-1];
1353 if (!slot
->eps
[epid
-1]) {
1354 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1355 return CC_EP_NOT_ENABLED_ERROR
;
1358 epctx
= slot
->eps
[epid
-1];
1360 if (epctx
->state
!= EP_HALTED
) {
1361 DPRINTF("xhci: reset EP while EP %d not halted (%d)\n",
1362 epid
, epctx
->state
);
1363 return CC_CONTEXT_STATE_ERROR
;
1366 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0) > 0) {
1367 DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, "
1368 "data might be lost\n");
1371 if (!xhci
->slots
[slotid
-1].uport
||
1372 !xhci
->slots
[slotid
-1].uport
->dev
||
1373 !xhci
->slots
[slotid
-1].uport
->dev
->attached
) {
1374 return CC_USB_TRANSACTION_ERROR
;
1377 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1379 if (epctx
->nr_pstreams
) {
1380 xhci_reset_streams(epctx
);
1386 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1387 unsigned int epid
, unsigned int streamid
,
1391 XHCIEPContext
*epctx
;
1392 XHCIStreamContext
*sctx
;
1395 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1397 if (epid
< 1 || epid
> 31) {
1398 DPRINTF("xhci: bad ep %d\n", epid
);
1399 return CC_TRB_ERROR
;
1402 trace_usb_xhci_ep_set_dequeue(slotid
, epid
, streamid
, pdequeue
);
1403 dequeue
= xhci_mask64(pdequeue
);
1405 slot
= &xhci
->slots
[slotid
-1];
1407 if (!slot
->eps
[epid
-1]) {
1408 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1409 return CC_EP_NOT_ENABLED_ERROR
;
1412 epctx
= slot
->eps
[epid
-1];
1414 if (epctx
->state
!= EP_STOPPED
) {
1415 DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1416 return CC_CONTEXT_STATE_ERROR
;
1419 if (epctx
->nr_pstreams
) {
1421 sctx
= xhci_find_stream(epctx
, streamid
, &err
);
1425 xhci_ring_init(xhci
, &sctx
->ring
, dequeue
& ~0xf);
1426 sctx
->ring
.ccs
= dequeue
& 1;
1429 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1430 epctx
->ring
.ccs
= dequeue
& 1;
1433 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_STOPPED
);
1438 static int xhci_xfer_create_sgl(XHCITransfer
*xfer
, int in_xfer
)
1440 XHCIState
*xhci
= xfer
->epctx
->xhci
;
1443 xfer
->int_req
= false;
1444 qemu_sglist_init(&xfer
->sgl
, DEVICE(xhci
), xfer
->trb_count
, xhci
->as
);
1445 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1446 XHCITRB
*trb
= &xfer
->trbs
[i
];
1448 unsigned int chunk
= 0;
1450 if (trb
->control
& TRB_TR_IOC
) {
1451 xfer
->int_req
= true;
1454 switch (TRB_TYPE(*trb
)) {
1456 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1457 DPRINTF("xhci: data direction mismatch for TR_DATA\n");
1463 addr
= xhci_mask64(trb
->parameter
);
1464 chunk
= trb
->status
& 0x1ffff;
1465 if (trb
->control
& TRB_TR_IDT
) {
1466 if (chunk
> 8 || in_xfer
) {
1467 DPRINTF("xhci: invalid immediate data TRB\n");
1470 qemu_sglist_add(&xfer
->sgl
, trb
->addr
, chunk
);
1472 qemu_sglist_add(&xfer
->sgl
, addr
, chunk
);
1481 qemu_sglist_destroy(&xfer
->sgl
);
1486 static void xhci_xfer_unmap(XHCITransfer
*xfer
)
1488 usb_packet_unmap(&xfer
->packet
, &xfer
->sgl
);
1489 qemu_sglist_destroy(&xfer
->sgl
);
1492 static void xhci_xfer_report(XHCITransfer
*xfer
)
1498 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1499 XHCIState
*xhci
= xfer
->epctx
->xhci
;
1502 left
= xfer
->packet
.actual_length
;
1504 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1505 XHCITRB
*trb
= &xfer
->trbs
[i
];
1506 unsigned int chunk
= 0;
1508 switch (TRB_TYPE(*trb
)) {
1510 chunk
= trb
->status
& 0x1ffff;
1518 chunk
= trb
->status
& 0x1ffff;
1521 if (xfer
->status
== CC_SUCCESS
) {
1534 if (!reported
&& ((trb
->control
& TRB_TR_IOC
) ||
1535 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)) ||
1536 (xfer
->status
!= CC_SUCCESS
&& left
== 0))) {
1537 event
.slotid
= xfer
->epctx
->slotid
;
1538 event
.epid
= xfer
->epctx
->epid
;
1539 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1541 event
.ptr
= trb
->addr
;
1542 if (xfer
->status
== CC_SUCCESS
) {
1543 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1545 event
.ccode
= xfer
->status
;
1547 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1548 event
.ptr
= trb
->parameter
;
1549 event
.flags
|= TRB_EV_ED
;
1550 event
.length
= edtla
& 0xffffff;
1551 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1554 xhci_event(xhci
, &event
, TRB_INTR(*trb
));
1556 if (xfer
->status
!= CC_SUCCESS
) {
1561 switch (TRB_TYPE(*trb
)) {
1571 static void xhci_stall_ep(XHCITransfer
*xfer
)
1573 XHCIEPContext
*epctx
= xfer
->epctx
;
1574 XHCIState
*xhci
= epctx
->xhci
;
1576 XHCIStreamContext
*sctx
;
1578 if (epctx
->type
== ET_ISO_IN
|| epctx
->type
== ET_ISO_OUT
) {
1579 /* never halt isoch endpoints, 4.10.2 */
1583 if (epctx
->nr_pstreams
) {
1584 sctx
= xhci_find_stream(epctx
, xfer
->streamid
, &err
);
1588 sctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1589 sctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1590 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_HALTED
);
1592 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1593 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1594 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_HALTED
);
1598 static int xhci_setup_packet(XHCITransfer
*xfer
)
1603 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1605 if (xfer
->packet
.ep
) {
1606 ep
= xfer
->packet
.ep
;
1608 ep
= xhci_epid_to_usbep(xfer
->epctx
);
1610 DPRINTF("xhci: slot %d has no device\n",
1611 xfer
->epctx
->slotid
);
1616 xhci_xfer_create_sgl(xfer
, dir
== USB_TOKEN_IN
); /* Also sets int_req */
1617 usb_packet_setup(&xfer
->packet
, dir
, ep
, xfer
->streamid
,
1618 xfer
->trbs
[0].addr
, false, xfer
->int_req
);
1619 if (usb_packet_map(&xfer
->packet
, &xfer
->sgl
)) {
1620 qemu_sglist_destroy(&xfer
->sgl
);
1623 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1624 xfer
->packet
.pid
, ep
->dev
->addr
, ep
->nr
);
1628 static int xhci_try_complete_packet(XHCITransfer
*xfer
)
1630 if (xfer
->packet
.status
== USB_RET_ASYNC
) {
1631 trace_usb_xhci_xfer_async(xfer
);
1632 xfer
->running_async
= 1;
1633 xfer
->running_retry
= 0;
1636 } else if (xfer
->packet
.status
== USB_RET_NAK
) {
1637 trace_usb_xhci_xfer_nak(xfer
);
1638 xfer
->running_async
= 0;
1639 xfer
->running_retry
= 1;
1643 xfer
->running_async
= 0;
1644 xfer
->running_retry
= 0;
1646 xhci_xfer_unmap(xfer
);
1649 if (xfer
->packet
.status
== USB_RET_SUCCESS
) {
1650 trace_usb_xhci_xfer_success(xfer
, xfer
->packet
.actual_length
);
1651 xfer
->status
= CC_SUCCESS
;
1652 xhci_xfer_report(xfer
);
1657 trace_usb_xhci_xfer_error(xfer
, xfer
->packet
.status
);
1658 switch (xfer
->packet
.status
) {
1660 case USB_RET_IOERROR
:
1661 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1662 xhci_xfer_report(xfer
);
1663 xhci_stall_ep(xfer
);
1666 xfer
->status
= CC_STALL_ERROR
;
1667 xhci_xfer_report(xfer
);
1668 xhci_stall_ep(xfer
);
1670 case USB_RET_BABBLE
:
1671 xfer
->status
= CC_BABBLE_DETECTED
;
1672 xhci_xfer_report(xfer
);
1673 xhci_stall_ep(xfer
);
1676 DPRINTF("%s: FIXME: status = %d\n", __func__
,
1677 xfer
->packet
.status
);
1678 FIXME("unhandled USB_RET_*");
1683 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1685 XHCITRB
*trb_setup
, *trb_status
;
1686 uint8_t bmRequestType
;
1688 trb_setup
= &xfer
->trbs
[0];
1689 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1691 trace_usb_xhci_xfer_start(xfer
, xfer
->epctx
->slotid
,
1692 xfer
->epctx
->epid
, xfer
->streamid
);
1694 /* at most one Event Data TRB allowed after STATUS */
1695 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1699 /* do some sanity checks */
1700 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1701 DPRINTF("xhci: ep0 first TD not SETUP: %d\n",
1702 TRB_TYPE(*trb_setup
));
1705 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1706 DPRINTF("xhci: ep0 last TD not STATUS: %d\n",
1707 TRB_TYPE(*trb_status
));
1710 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1711 DPRINTF("xhci: Setup TRB doesn't have IDT set\n");
1714 if ((trb_setup
->status
& 0x1ffff) != 8) {
1715 DPRINTF("xhci: Setup TRB has bad length (%d)\n",
1716 (trb_setup
->status
& 0x1ffff));
1720 bmRequestType
= trb_setup
->parameter
;
1722 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1723 xfer
->iso_xfer
= false;
1724 xfer
->timed_xfer
= false;
1726 if (xhci_setup_packet(xfer
) < 0) {
1729 xfer
->packet
.parameter
= trb_setup
->parameter
;
1731 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1732 xhci_try_complete_packet(xfer
);
1736 static void xhci_calc_intr_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1737 XHCIEPContext
*epctx
, uint64_t mfindex
)
1739 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1740 ~(epctx
->interval
-1));
1741 uint64_t kick
= epctx
->mfindex_last
+ epctx
->interval
;
1743 assert(epctx
->interval
!= 0);
1744 xfer
->mfindex_kick
= MAX(asap
, kick
);
1747 static void xhci_calc_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1748 XHCIEPContext
*epctx
, uint64_t mfindex
)
1750 if (xfer
->trbs
[0].control
& TRB_TR_SIA
) {
1751 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1752 ~(epctx
->interval
-1));
1753 if (asap
>= epctx
->mfindex_last
&&
1754 asap
<= epctx
->mfindex_last
+ epctx
->interval
* 4) {
1755 xfer
->mfindex_kick
= epctx
->mfindex_last
+ epctx
->interval
;
1757 xfer
->mfindex_kick
= asap
;
1760 xfer
->mfindex_kick
= ((xfer
->trbs
[0].control
>> TRB_TR_FRAMEID_SHIFT
)
1761 & TRB_TR_FRAMEID_MASK
) << 3;
1762 xfer
->mfindex_kick
|= mfindex
& ~0x3fff;
1763 if (xfer
->mfindex_kick
+ 0x100 < mfindex
) {
1764 xfer
->mfindex_kick
+= 0x4000;
1769 static void xhci_check_intr_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1770 XHCIEPContext
*epctx
, uint64_t mfindex
)
1772 if (xfer
->mfindex_kick
> mfindex
) {
1773 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
1774 (xfer
->mfindex_kick
- mfindex
) * 125000);
1775 xfer
->running_retry
= 1;
1777 epctx
->mfindex_last
= xfer
->mfindex_kick
;
1778 timer_del(epctx
->kick_timer
);
1779 xfer
->running_retry
= 0;
1784 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1788 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", epctx
->slotid
, epctx
->epid
);
1790 xfer
->in_xfer
= epctx
->type
>>2;
1792 switch(epctx
->type
) {
1796 xfer
->iso_xfer
= false;
1797 xfer
->timed_xfer
= true;
1798 mfindex
= xhci_mfindex_get(xhci
);
1799 xhci_calc_intr_kick(xhci
, xfer
, epctx
, mfindex
);
1800 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1801 if (xfer
->running_retry
) {
1808 xfer
->iso_xfer
= false;
1809 xfer
->timed_xfer
= false;
1814 xfer
->iso_xfer
= true;
1815 xfer
->timed_xfer
= true;
1816 mfindex
= xhci_mfindex_get(xhci
);
1817 xhci_calc_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1818 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1819 if (xfer
->running_retry
) {
1824 trace_usb_xhci_unimplemented("endpoint type", epctx
->type
);
1828 if (xhci_setup_packet(xfer
) < 0) {
1831 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1832 xhci_try_complete_packet(xfer
);
1836 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1838 trace_usb_xhci_xfer_start(xfer
, xfer
->epctx
->slotid
,
1839 xfer
->epctx
->epid
, xfer
->streamid
);
1840 return xhci_submit(xhci
, xfer
, epctx
);
1843 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
1844 unsigned int epid
, unsigned int streamid
)
1846 XHCIEPContext
*epctx
;
1848 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1849 assert(epid
>= 1 && epid
<= 31);
1851 if (!xhci
->slots
[slotid
-1].enabled
) {
1852 DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1855 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1857 DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1862 if (epctx
->kick_active
) {
1865 xhci_kick_epctx(epctx
, streamid
);
1868 static bool xhci_slot_ok(XHCIState
*xhci
, int slotid
)
1870 return (xhci
->slots
[slotid
- 1].uport
&&
1871 xhci
->slots
[slotid
- 1].uport
->dev
&&
1872 xhci
->slots
[slotid
- 1].uport
->dev
->attached
);
1875 static void xhci_kick_epctx(XHCIEPContext
*epctx
, unsigned int streamid
)
1877 XHCIState
*xhci
= epctx
->xhci
;
1878 XHCIStreamContext
*stctx
= NULL
;
1881 USBEndpoint
*ep
= NULL
;
1883 unsigned int count
= 0;
1887 trace_usb_xhci_ep_kick(epctx
->slotid
, epctx
->epid
, streamid
);
1888 assert(!epctx
->kick_active
);
1890 /* If the device has been detached, but the guest has not noticed this
1891 yet the 2 above checks will succeed, but we must NOT continue */
1892 if (!xhci_slot_ok(xhci
, epctx
->slotid
)) {
1897 xfer
= epctx
->retry
;
1899 trace_usb_xhci_xfer_retry(xfer
);
1900 assert(xfer
->running_retry
);
1901 if (xfer
->timed_xfer
) {
1902 /* time to kick the transfer? */
1903 mfindex
= xhci_mfindex_get(xhci
);
1904 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1905 if (xfer
->running_retry
) {
1908 xfer
->timed_xfer
= 0;
1909 xfer
->running_retry
= 1;
1911 if (xfer
->iso_xfer
) {
1912 /* retry iso transfer */
1913 if (xhci_setup_packet(xfer
) < 0) {
1916 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1917 assert(xfer
->packet
.status
!= USB_RET_NAK
);
1918 xhci_try_complete_packet(xfer
);
1920 /* retry nak'ed transfer */
1921 if (xhci_setup_packet(xfer
) < 0) {
1924 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1925 if (xfer
->packet
.status
== USB_RET_NAK
) {
1926 xhci_xfer_unmap(xfer
);
1929 xhci_try_complete_packet(xfer
);
1931 assert(!xfer
->running_retry
);
1932 if (xfer
->complete
) {
1933 /* update ring dequeue ptr */
1934 xhci_set_ep_state(xhci
, epctx
, stctx
, epctx
->state
);
1935 xhci_ep_free_xfer(epctx
->retry
);
1937 epctx
->retry
= NULL
;
1940 if (epctx
->state
== EP_HALTED
) {
1941 DPRINTF("xhci: ep halted, not running schedule\n");
1946 if (epctx
->nr_pstreams
) {
1948 stctx
= xhci_find_stream(epctx
, streamid
, &err
);
1949 if (stctx
== NULL
) {
1952 ring
= &stctx
->ring
;
1953 xhci_set_ep_state(xhci
, epctx
, stctx
, EP_RUNNING
);
1955 ring
= &epctx
->ring
;
1957 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_RUNNING
);
1959 if (!ring
->dequeue
) {
1963 epctx
->kick_active
++;
1965 length
= xhci_ring_chain_length(xhci
, ring
);
1967 if (epctx
->type
== ET_ISO_OUT
|| epctx
->type
== ET_ISO_IN
) {
1969 XHCIEvent ev
= { ER_TRANSFER
};
1970 ev
.ccode
= epctx
->type
== ET_ISO_IN
?
1971 CC_RING_OVERRUN
: CC_RING_UNDERRUN
;
1972 ev
.slotid
= epctx
->slotid
;
1973 ev
.epid
= epctx
->epid
;
1974 ev
.ptr
= epctx
->ring
.dequeue
;
1975 xhci_event(xhci
, &ev
, xhci
->slots
[epctx
->slotid
-1].intr
);
1979 xfer
= xhci_ep_alloc_xfer(epctx
, length
);
1984 for (i
= 0; i
< length
; i
++) {
1986 type
= xhci_ring_fetch(xhci
, ring
, &xfer
->trbs
[i
], NULL
);
1989 xhci_ep_free_xfer(xfer
);
1990 epctx
->kick_active
--;
1994 xfer
->streamid
= streamid
;
1996 if (epctx
->epid
== 1) {
1997 xhci_fire_ctl_transfer(xhci
, xfer
);
1999 xhci_fire_transfer(xhci
, xfer
, epctx
);
2001 if (!xhci_slot_ok(xhci
, epctx
->slotid
)) {
2002 /* surprise removal -> stop processing */
2005 if (xfer
->complete
) {
2006 /* update ring dequeue ptr */
2007 xhci_set_ep_state(xhci
, epctx
, stctx
, epctx
->state
);
2008 xhci_ep_free_xfer(xfer
);
2012 if (epctx
->state
== EP_HALTED
) {
2015 if (xfer
!= NULL
&& xfer
->running_retry
) {
2016 DPRINTF("xhci: xfer nacked, stopping schedule\n");
2017 epctx
->retry
= xfer
;
2018 xhci_xfer_unmap(xfer
);
2021 if (count
++ > TRANSFER_LIMIT
) {
2022 trace_usb_xhci_enforced_limit("transfers");
2026 epctx
->kick_active
--;
2028 ep
= xhci_epid_to_usbep(epctx
);
2030 usb_device_flush_ep_queue(ep
->dev
, ep
);
2034 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
2036 trace_usb_xhci_slot_enable(slotid
);
2037 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2038 xhci
->slots
[slotid
-1].enabled
= 1;
2039 xhci
->slots
[slotid
-1].uport
= NULL
;
2040 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
2045 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
2049 trace_usb_xhci_slot_disable(slotid
);
2050 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2052 for (i
= 1; i
<= 31; i
++) {
2053 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2054 xhci_disable_ep(xhci
, slotid
, i
);
2058 xhci
->slots
[slotid
-1].enabled
= 0;
2059 xhci
->slots
[slotid
-1].addressed
= 0;
2060 xhci
->slots
[slotid
-1].uport
= NULL
;
2061 xhci
->slots
[slotid
-1].intr
= 0;
2065 static USBPort
*xhci_lookup_uport(XHCIState
*xhci
, uint32_t *slot_ctx
)
2071 port
= (slot_ctx
[1]>>16) & 0xFF;
2072 if (port
< 1 || port
> xhci
->numports
) {
2075 port
= xhci
->ports
[port
-1].uport
->index
+1;
2076 pos
= snprintf(path
, sizeof(path
), "%d", port
);
2077 for (i
= 0; i
< 5; i
++) {
2078 port
= (slot_ctx
[0] >> 4*i
) & 0x0f;
2082 pos
+= snprintf(path
+ pos
, sizeof(path
) - pos
, ".%d", port
);
2085 QTAILQ_FOREACH(uport
, &xhci
->bus
.used
, next
) {
2086 if (strcmp(uport
->path
, path
) == 0) {
2093 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
2094 uint64_t pictx
, bool bsr
)
2099 dma_addr_t ictx
, octx
, dcbaap
;
2101 uint32_t ictl_ctx
[2];
2102 uint32_t slot_ctx
[4];
2103 uint32_t ep0_ctx
[5];
2107 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2109 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
2110 ldq_le_dma(xhci
->as
, dcbaap
+ 8 * slotid
, &poctx
, MEMTXATTRS_UNSPECIFIED
);
2111 ictx
= xhci_mask64(pictx
);
2112 octx
= xhci_mask64(poctx
);
2114 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2115 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2117 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2119 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
2120 DPRINTF("xhci: invalid input context control %08x %08x\n",
2121 ictl_ctx
[0], ictl_ctx
[1]);
2122 return CC_TRB_ERROR
;
2125 xhci_dma_read_u32s(xhci
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
2126 xhci_dma_read_u32s(xhci
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
2128 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2129 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2131 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2132 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2134 uport
= xhci_lookup_uport(xhci
, slot_ctx
);
2135 if (uport
== NULL
) {
2136 DPRINTF("xhci: port not found\n");
2137 return CC_TRB_ERROR
;
2139 trace_usb_xhci_slot_address(slotid
, uport
->path
);
2142 if (!dev
|| !dev
->attached
) {
2143 DPRINTF("xhci: port %s not connected\n", uport
->path
);
2144 return CC_USB_TRANSACTION_ERROR
;
2147 for (i
= 0; i
< xhci
->numslots
; i
++) {
2148 if (i
== slotid
-1) {
2151 if (xhci
->slots
[i
].uport
== uport
) {
2152 DPRINTF("xhci: port %s already assigned to slot %d\n",
2154 return CC_TRB_ERROR
;
2158 slot
= &xhci
->slots
[slotid
-1];
2159 slot
->uport
= uport
;
2161 slot
->intr
= get_field(slot_ctx
[2], TRB_INTR
);
2163 /* Make sure device is in USB_STATE_DEFAULT state */
2164 usb_device_reset(dev
);
2166 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2171 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slotid
;
2172 memset(&p
, 0, sizeof(p
));
2173 usb_packet_addbuf(&p
, buf
, sizeof(buf
));
2174 usb_packet_setup(&p
, USB_TOKEN_OUT
,
2175 usb_ep_get(dev
, USB_TOKEN_OUT
, 0), 0,
2177 usb_device_handle_control(dev
, &p
,
2178 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
2179 slotid
, 0, 0, NULL
);
2180 assert(p
.status
!= USB_RET_ASYNC
);
2181 usb_packet_cleanup(&p
);
2184 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
2186 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2187 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2188 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2189 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2191 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2192 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2194 xhci
->slots
[slotid
-1].addressed
= 1;
2199 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
2200 uint64_t pictx
, bool dc
)
2202 dma_addr_t ictx
, octx
;
2203 uint32_t ictl_ctx
[2];
2204 uint32_t slot_ctx
[4];
2205 uint32_t islot_ctx
[4];
2210 trace_usb_xhci_slot_configure(slotid
);
2211 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2213 ictx
= xhci_mask64(pictx
);
2214 octx
= xhci
->slots
[slotid
-1].ctx
;
2216 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2217 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2220 for (i
= 2; i
<= 31; i
++) {
2221 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2222 xhci_disable_ep(xhci
, slotid
, i
);
2226 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2227 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2228 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
2229 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2230 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2231 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2236 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2238 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
2239 DPRINTF("xhci: invalid input context control %08x %08x\n",
2240 ictl_ctx
[0], ictl_ctx
[1]);
2241 return CC_TRB_ERROR
;
2244 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2245 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2247 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
2248 DPRINTF("xhci: invalid slot state %08x\n", slot_ctx
[3]);
2249 return CC_CONTEXT_STATE_ERROR
;
2252 xhci_free_device_streams(xhci
, slotid
, ictl_ctx
[0] | ictl_ctx
[1]);
2254 for (i
= 2; i
<= 31; i
++) {
2255 if (ictl_ctx
[0] & (1<<i
)) {
2256 xhci_disable_ep(xhci
, slotid
, i
);
2258 if (ictl_ctx
[1] & (1<<i
)) {
2259 xhci_dma_read_u32s(xhci
, ictx
+32+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2260 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
2261 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2262 ep_ctx
[3], ep_ctx
[4]);
2263 xhci_disable_ep(xhci
, slotid
, i
);
2264 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
2265 if (res
!= CC_SUCCESS
) {
2268 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
2269 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2270 ep_ctx
[3], ep_ctx
[4]);
2271 xhci_dma_write_u32s(xhci
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2275 res
= xhci_alloc_device_streams(xhci
, slotid
, ictl_ctx
[1]);
2276 if (res
!= CC_SUCCESS
) {
2277 for (i
= 2; i
<= 31; i
++) {
2278 if (ictl_ctx
[1] & (1u << i
)) {
2279 xhci_disable_ep(xhci
, slotid
, i
);
2285 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2286 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
2287 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
2288 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
2289 SLOT_CONTEXT_ENTRIES_SHIFT
);
2290 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2291 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2293 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2299 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
2302 dma_addr_t ictx
, octx
;
2303 uint32_t ictl_ctx
[2];
2304 uint32_t iep0_ctx
[5];
2305 uint32_t ep0_ctx
[5];
2306 uint32_t islot_ctx
[4];
2307 uint32_t slot_ctx
[4];
2309 trace_usb_xhci_slot_evaluate(slotid
);
2310 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2312 ictx
= xhci_mask64(pictx
);
2313 octx
= xhci
->slots
[slotid
-1].ctx
;
2315 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2316 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2318 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2320 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
2321 DPRINTF("xhci: invalid input context control %08x %08x\n",
2322 ictl_ctx
[0], ictl_ctx
[1]);
2323 return CC_TRB_ERROR
;
2326 if (ictl_ctx
[1] & 0x1) {
2327 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2329 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2330 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
2332 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2334 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
2335 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
2336 /* update interrupter target field */
2337 xhci
->slots
[slotid
-1].intr
= get_field(islot_ctx
[2], TRB_INTR
);
2338 set_field(&slot_ctx
[2], xhci
->slots
[slotid
-1].intr
, TRB_INTR
);
2340 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2341 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2343 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2346 if (ictl_ctx
[1] & 0x2) {
2347 xhci_dma_read_u32s(xhci
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
2349 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2350 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
2351 iep0_ctx
[3], iep0_ctx
[4]);
2353 xhci_dma_read_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2355 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
2356 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
2358 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2359 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2361 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2367 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
2369 uint32_t slot_ctx
[4];
2373 trace_usb_xhci_slot_reset(slotid
);
2374 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2376 octx
= xhci
->slots
[slotid
-1].ctx
;
2378 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2380 for (i
= 2; i
<= 31; i
++) {
2381 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2382 xhci_disable_ep(xhci
, slotid
, i
);
2386 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2387 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2388 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2389 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2390 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2391 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2396 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
2398 unsigned int slotid
;
2399 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
2400 if (slotid
< 1 || slotid
> xhci
->numslots
) {
2401 DPRINTF("xhci: bad slot id %d\n", slotid
);
2402 event
->ccode
= CC_TRB_ERROR
;
2404 } else if (!xhci
->slots
[slotid
-1].enabled
) {
2405 DPRINTF("xhci: slot id %d not enabled\n", slotid
);
2406 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
2412 /* cleanup slot state on usb device detach */
2413 static void xhci_detach_slot(XHCIState
*xhci
, USBPort
*uport
)
2417 for (slot
= 0; slot
< xhci
->numslots
; slot
++) {
2418 if (xhci
->slots
[slot
].uport
== uport
) {
2422 if (slot
== xhci
->numslots
) {
2426 for (ep
= 0; ep
< 31; ep
++) {
2427 if (xhci
->slots
[slot
].eps
[ep
]) {
2428 xhci_ep_nuke_xfers(xhci
, slot
+ 1, ep
+ 1, 0);
2431 xhci
->slots
[slot
].uport
= NULL
;
2434 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
2438 DPRINTF("xhci_get_port_bandwidth()\n");
2440 ctx
= xhci_mask64(pctx
);
2442 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
2444 /* TODO: actually implement real values here. This is 80% for all ports. */
2445 if (stb_dma(xhci
->as
, ctx
, 0, MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
||
2446 dma_memory_set(xhci
->as
, ctx
+ 1, 80, xhci
->numports
,
2447 MEMTXATTRS_UNSPECIFIED
) != MEMTX_OK
) {
2448 qemu_log_mask(LOG_GUEST_ERROR
, "%s: DMA memory write failed!\n",
2450 return CC_TRB_ERROR
;
2456 static uint32_t rotl(uint32_t v
, unsigned count
)
2459 return (v
<< count
) | (v
>> (32 - count
));
2463 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2466 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2467 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2468 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2472 static void xhci_process_commands(XHCIState
*xhci
)
2476 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2478 unsigned int i
, slotid
= 0, count
= 0;
2480 DPRINTF("xhci_process_commands()\n");
2481 if (!xhci_running(xhci
)) {
2482 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2486 xhci
->crcr_low
|= CRCR_CRR
;
2488 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2491 case CR_ENABLE_SLOT
:
2492 for (i
= 0; i
< xhci
->numslots
; i
++) {
2493 if (!xhci
->slots
[i
].enabled
) {
2497 if (i
>= xhci
->numslots
) {
2498 DPRINTF("xhci: no device slots available\n");
2499 event
.ccode
= CC_NO_SLOTS_ERROR
;
2502 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2505 case CR_DISABLE_SLOT
:
2506 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2508 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2511 case CR_ADDRESS_DEVICE
:
2512 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2514 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2515 trb
.control
& TRB_CR_BSR
);
2518 case CR_CONFIGURE_ENDPOINT
:
2519 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2521 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2522 trb
.control
& TRB_CR_DC
);
2525 case CR_EVALUATE_CONTEXT
:
2526 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2528 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2531 case CR_STOP_ENDPOINT
:
2532 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2534 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2536 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2539 case CR_RESET_ENDPOINT
:
2540 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2542 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2544 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2547 case CR_SET_TR_DEQUEUE
:
2548 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2550 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2552 unsigned int streamid
= (trb
.status
>> 16) & 0xffff;
2553 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
,
2558 case CR_RESET_DEVICE
:
2559 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2561 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2564 case CR_GET_PORT_BANDWIDTH
:
2565 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2568 event
.ccode
= CC_SUCCESS
;
2570 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2571 if (xhci
->nec_quirks
) {
2572 event
.type
= 48; /* NEC reply */
2573 event
.length
= 0x3034;
2575 event
.ccode
= CC_TRB_ERROR
;
2578 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2579 if (xhci
->nec_quirks
) {
2580 uint32_t chi
= trb
.parameter
>> 32;
2581 uint32_t clo
= trb
.parameter
;
2582 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2583 event
.length
= val
& 0xFFFF;
2584 event
.epid
= val
>> 16;
2586 event
.type
= 48; /* NEC reply */
2588 event
.ccode
= CC_TRB_ERROR
;
2592 trace_usb_xhci_unimplemented("command", type
);
2593 event
.ccode
= CC_TRB_ERROR
;
2596 event
.slotid
= slotid
;
2597 xhci_event(xhci
, &event
, 0);
2599 if (count
++ > COMMAND_LIMIT
) {
2600 trace_usb_xhci_enforced_limit("commands");
2606 static bool xhci_port_have_device(XHCIPort
*port
)
2608 if (!port
->uport
->dev
|| !port
->uport
->dev
->attached
) {
2609 return false; /* no device present */
2611 if (!((1 << port
->uport
->dev
->speed
) & port
->speedmask
)) {
2612 return false; /* speed mismatch */
2617 static void xhci_port_notify(XHCIPort
*port
, uint32_t bits
)
2619 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
,
2620 port
->portnr
<< 24 };
2622 if ((port
->portsc
& bits
) == bits
) {
2625 trace_usb_xhci_port_notify(port
->portnr
, bits
);
2626 port
->portsc
|= bits
;
2627 if (!xhci_running(port
->xhci
)) {
2630 xhci_event(port
->xhci
, &ev
, 0);
2633 static void xhci_port_update(XHCIPort
*port
, int is_detach
)
2635 uint32_t pls
= PLS_RX_DETECT
;
2638 port
->portsc
= PORTSC_PP
;
2639 if (!is_detach
&& xhci_port_have_device(port
)) {
2640 port
->portsc
|= PORTSC_CCS
;
2641 switch (port
->uport
->dev
->speed
) {
2643 port
->portsc
|= PORTSC_SPEED_LOW
;
2646 case USB_SPEED_FULL
:
2647 port
->portsc
|= PORTSC_SPEED_FULL
;
2650 case USB_SPEED_HIGH
:
2651 port
->portsc
|= PORTSC_SPEED_HIGH
;
2654 case USB_SPEED_SUPER
:
2655 port
->portsc
|= PORTSC_SPEED_SUPER
;
2656 port
->portsc
|= PORTSC_PED
;
2661 set_field(&port
->portsc
, pls
, PORTSC_PLS
);
2662 trace_usb_xhci_port_link(port
->portnr
, pls
);
2663 xhci_port_notify(port
, PORTSC_CSC
);
2666 static void xhci_port_reset(XHCIPort
*port
, bool warm_reset
)
2668 trace_usb_xhci_port_reset(port
->portnr
, warm_reset
);
2670 if (!xhci_port_have_device(port
)) {
2674 usb_device_reset(port
->uport
->dev
);
2676 switch (port
->uport
->dev
->speed
) {
2677 case USB_SPEED_SUPER
:
2679 port
->portsc
|= PORTSC_WRC
;
2683 case USB_SPEED_FULL
:
2684 case USB_SPEED_HIGH
:
2685 set_field(&port
->portsc
, PLS_U0
, PORTSC_PLS
);
2686 trace_usb_xhci_port_link(port
->portnr
, PLS_U0
);
2687 port
->portsc
|= PORTSC_PED
;
2691 port
->portsc
&= ~PORTSC_PR
;
2692 xhci_port_notify(port
, PORTSC_PRC
);
2695 static void xhci_reset(DeviceState
*dev
)
2697 XHCIState
*xhci
= XHCI(dev
);
2700 trace_usb_xhci_reset();
2701 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2702 DPRINTF("xhci: reset while running!\n");
2706 xhci
->usbsts
= USBSTS_HCH
;
2709 xhci
->crcr_high
= 0;
2710 xhci
->dcbaap_low
= 0;
2711 xhci
->dcbaap_high
= 0;
2714 for (i
= 0; i
< xhci
->numslots
; i
++) {
2715 xhci_disable_slot(xhci
, i
+1);
2718 for (i
= 0; i
< xhci
->numports
; i
++) {
2719 xhci_port_update(xhci
->ports
+ i
, 0);
2722 for (i
= 0; i
< xhci
->numintrs
; i
++) {
2723 xhci
->intr
[i
].iman
= 0;
2724 xhci
->intr
[i
].imod
= 0;
2725 xhci
->intr
[i
].erstsz
= 0;
2726 xhci
->intr
[i
].erstba_low
= 0;
2727 xhci
->intr
[i
].erstba_high
= 0;
2728 xhci
->intr
[i
].erdp_low
= 0;
2729 xhci
->intr
[i
].erdp_high
= 0;
2731 xhci
->intr
[i
].er_ep_idx
= 0;
2732 xhci
->intr
[i
].er_pcs
= 1;
2733 xhci
->intr
[i
].ev_buffer_put
= 0;
2734 xhci
->intr
[i
].ev_buffer_get
= 0;
2737 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2738 xhci_mfwrap_update(xhci
);
2741 static uint64_t xhci_cap_read(void *ptr
, hwaddr reg
, unsigned size
)
2743 XHCIState
*xhci
= ptr
;
2747 case 0x00: /* HCIVERSION, CAPLENGTH */
2748 ret
= 0x01000000 | LEN_CAP
;
2750 case 0x04: /* HCSPARAMS 1 */
2751 ret
= ((xhci
->numports_2
+xhci
->numports_3
)<<24)
2752 | (xhci
->numintrs
<<8) | xhci
->numslots
;
2754 case 0x08: /* HCSPARAMS 2 */
2757 case 0x0c: /* HCSPARAMS 3 */
2760 case 0x10: /* HCCPARAMS */
2761 if (sizeof(dma_addr_t
) == 4) {
2762 ret
= 0x00080000 | (xhci
->max_pstreams_mask
<< 12);
2764 ret
= 0x00080001 | (xhci
->max_pstreams_mask
<< 12);
2767 case 0x14: /* DBOFF */
2770 case 0x18: /* RTSOFF */
2774 /* extended capabilities */
2775 case 0x20: /* Supported Protocol:00 */
2776 ret
= 0x02000402; /* USB 2.0 */
2778 case 0x24: /* Supported Protocol:04 */
2779 ret
= 0x20425355; /* "USB " */
2781 case 0x28: /* Supported Protocol:08 */
2782 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2783 ret
= (xhci
->numports_2
<<8) | (xhci
->numports_3
+1);
2785 ret
= (xhci
->numports_2
<<8) | 1;
2788 case 0x2c: /* Supported Protocol:0c */
2789 ret
= 0x00000000; /* reserved */
2791 case 0x30: /* Supported Protocol:00 */
2792 ret
= 0x03000002; /* USB 3.0 */
2794 case 0x34: /* Supported Protocol:04 */
2795 ret
= 0x20425355; /* "USB " */
2797 case 0x38: /* Supported Protocol:08 */
2798 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2799 ret
= (xhci
->numports_3
<<8) | 1;
2801 ret
= (xhci
->numports_3
<<8) | (xhci
->numports_2
+1);
2804 case 0x3c: /* Supported Protocol:0c */
2805 ret
= 0x00000000; /* reserved */
2808 trace_usb_xhci_unimplemented("cap read", reg
);
2812 trace_usb_xhci_cap_read(reg
, ret
);
2816 static uint64_t xhci_port_read(void *ptr
, hwaddr reg
, unsigned size
)
2818 XHCIPort
*port
= ptr
;
2822 case 0x00: /* PORTSC */
2825 case 0x04: /* PORTPMSC */
2826 case 0x08: /* PORTLI */
2829 case 0x0c: /* reserved */
2831 trace_usb_xhci_unimplemented("port read", reg
);
2835 trace_usb_xhci_port_read(port
->portnr
, reg
, ret
);
2839 static void xhci_port_write(void *ptr
, hwaddr reg
,
2840 uint64_t val
, unsigned size
)
2842 XHCIPort
*port
= ptr
;
2843 uint32_t portsc
, notify
;
2845 trace_usb_xhci_port_write(port
->portnr
, reg
, val
);
2848 case 0x00: /* PORTSC */
2849 /* write-1-to-start bits */
2850 if (val
& PORTSC_WPR
) {
2851 xhci_port_reset(port
, true);
2854 if (val
& PORTSC_PR
) {
2855 xhci_port_reset(port
, false);
2859 portsc
= port
->portsc
;
2861 /* write-1-to-clear bits*/
2862 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2863 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2864 if (val
& PORTSC_LWS
) {
2865 /* overwrite PLS only when LWS=1 */
2866 uint32_t old_pls
= get_field(port
->portsc
, PORTSC_PLS
);
2867 uint32_t new_pls
= get_field(val
, PORTSC_PLS
);
2870 if (old_pls
!= PLS_U0
) {
2871 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2872 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2873 notify
= PORTSC_PLC
;
2877 if (old_pls
< PLS_U3
) {
2878 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2879 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2883 /* windows does this for some reason, don't spam stderr */
2886 DPRINTF("%s: ignore pls write (old %d, new %d)\n",
2887 __func__
, old_pls
, new_pls
);
2891 /* read/write bits */
2892 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2893 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2894 port
->portsc
= portsc
;
2896 xhci_port_notify(port
, notify
);
2899 case 0x04: /* PORTPMSC */
2900 case 0x08: /* PORTLI */
2902 trace_usb_xhci_unimplemented("port write", reg
);
2906 static uint64_t xhci_oper_read(void *ptr
, hwaddr reg
, unsigned size
)
2908 XHCIState
*xhci
= ptr
;
2912 case 0x00: /* USBCMD */
2915 case 0x04: /* USBSTS */
2918 case 0x08: /* PAGESIZE */
2921 case 0x14: /* DNCTRL */
2924 case 0x18: /* CRCR low */
2925 ret
= xhci
->crcr_low
& ~0xe;
2927 case 0x1c: /* CRCR high */
2928 ret
= xhci
->crcr_high
;
2930 case 0x30: /* DCBAAP low */
2931 ret
= xhci
->dcbaap_low
;
2933 case 0x34: /* DCBAAP high */
2934 ret
= xhci
->dcbaap_high
;
2936 case 0x38: /* CONFIG */
2940 trace_usb_xhci_unimplemented("oper read", reg
);
2944 trace_usb_xhci_oper_read(reg
, ret
);
2948 static void xhci_oper_write(void *ptr
, hwaddr reg
,
2949 uint64_t val
, unsigned size
)
2951 XHCIState
*xhci
= XHCI(ptr
);
2953 trace_usb_xhci_oper_write(reg
, val
);
2956 case 0x00: /* USBCMD */
2957 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
2959 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
2962 if (val
& USBCMD_CSS
) {
2964 xhci
->usbsts
&= ~USBSTS_SRE
;
2966 if (val
& USBCMD_CRS
) {
2968 xhci
->usbsts
|= USBSTS_SRE
;
2970 xhci
->usbcmd
= val
& 0xc0f;
2971 xhci_mfwrap_update(xhci
);
2972 if (val
& USBCMD_HCRST
) {
2973 xhci_reset(DEVICE(xhci
));
2975 xhci_intr_update(xhci
, 0);
2978 case 0x04: /* USBSTS */
2979 /* these bits are write-1-to-clear */
2980 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
2981 xhci_intr_update(xhci
, 0);
2984 case 0x14: /* DNCTRL */
2985 xhci
->dnctrl
= val
& 0xffff;
2987 case 0x18: /* CRCR low */
2988 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
2990 case 0x1c: /* CRCR high */
2991 xhci
->crcr_high
= val
;
2992 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
2993 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
2994 xhci
->crcr_low
&= ~CRCR_CRR
;
2995 xhci_event(xhci
, &event
, 0);
2996 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
2998 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
2999 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
3001 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
3003 case 0x30: /* DCBAAP low */
3004 xhci
->dcbaap_low
= val
& 0xffffffc0;
3006 case 0x34: /* DCBAAP high */
3007 xhci
->dcbaap_high
= val
;
3009 case 0x38: /* CONFIG */
3010 xhci
->config
= val
& 0xff;
3013 trace_usb_xhci_unimplemented("oper write", reg
);
3017 static uint64_t xhci_runtime_read(void *ptr
, hwaddr reg
,
3020 XHCIState
*xhci
= ptr
;
3025 case 0x00: /* MFINDEX */
3026 ret
= xhci_mfindex_get(xhci
) & 0x3fff;
3029 trace_usb_xhci_unimplemented("runtime read", reg
);
3033 int v
= (reg
- 0x20) / 0x20;
3034 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
3035 switch (reg
& 0x1f) {
3036 case 0x00: /* IMAN */
3039 case 0x04: /* IMOD */
3042 case 0x08: /* ERSTSZ */
3045 case 0x10: /* ERSTBA low */
3046 ret
= intr
->erstba_low
;
3048 case 0x14: /* ERSTBA high */
3049 ret
= intr
->erstba_high
;
3051 case 0x18: /* ERDP low */
3052 ret
= intr
->erdp_low
;
3054 case 0x1c: /* ERDP high */
3055 ret
= intr
->erdp_high
;
3060 trace_usb_xhci_runtime_read(reg
, ret
);
3064 static void xhci_runtime_write(void *ptr
, hwaddr reg
,
3065 uint64_t val
, unsigned size
)
3067 XHCIState
*xhci
= ptr
;
3068 XHCIInterrupter
*intr
;
3071 trace_usb_xhci_runtime_write(reg
, val
);
3074 trace_usb_xhci_unimplemented("runtime write", reg
);
3077 v
= (reg
- 0x20) / 0x20;
3078 intr
= &xhci
->intr
[v
];
3080 switch (reg
& 0x1f) {
3081 case 0x00: /* IMAN */
3082 if (val
& IMAN_IP
) {
3083 intr
->iman
&= ~IMAN_IP
;
3085 intr
->iman
&= ~IMAN_IE
;
3086 intr
->iman
|= val
& IMAN_IE
;
3087 xhci_intr_update(xhci
, v
);
3089 case 0x04: /* IMOD */
3092 case 0x08: /* ERSTSZ */
3093 intr
->erstsz
= val
& 0xffff;
3095 case 0x10: /* ERSTBA low */
3096 if (xhci
->nec_quirks
) {
3097 /* NEC driver bug: it doesn't align this to 64 bytes */
3098 intr
->erstba_low
= val
& 0xfffffff0;
3100 intr
->erstba_low
= val
& 0xffffffc0;
3103 case 0x14: /* ERSTBA high */
3104 intr
->erstba_high
= val
;
3105 xhci_er_reset(xhci
, v
);
3107 case 0x18: /* ERDP low */
3108 if (val
& ERDP_EHB
) {
3109 intr
->erdp_low
&= ~ERDP_EHB
;
3111 intr
->erdp_low
= (val
& ~ERDP_EHB
) | (intr
->erdp_low
& ERDP_EHB
);
3112 if (val
& ERDP_EHB
) {
3113 dma_addr_t erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
3114 unsigned int dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
3115 if (erdp
>= intr
->er_start
&&
3116 erdp
< (intr
->er_start
+ TRB_SIZE
* intr
->er_size
) &&
3117 dp_idx
!= intr
->er_ep_idx
) {
3118 xhci_intr_raise(xhci
, v
);
3122 case 0x1c: /* ERDP high */
3123 intr
->erdp_high
= val
;
3126 trace_usb_xhci_unimplemented("oper write", reg
);
3130 static uint64_t xhci_doorbell_read(void *ptr
, hwaddr reg
,
3133 /* doorbells always read as 0 */
3134 trace_usb_xhci_doorbell_read(reg
, 0);
3138 static void xhci_doorbell_write(void *ptr
, hwaddr reg
,
3139 uint64_t val
, unsigned size
)
3141 XHCIState
*xhci
= ptr
;
3142 unsigned int epid
, streamid
;
3144 trace_usb_xhci_doorbell_write(reg
, val
);
3146 if (!xhci_running(xhci
)) {
3147 DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
3155 xhci_process_commands(xhci
);
3157 DPRINTF("xhci: bad doorbell 0 write: 0x%x\n",
3162 streamid
= (val
>> 16) & 0xffff;
3163 if (reg
> xhci
->numslots
) {
3164 DPRINTF("xhci: bad doorbell %d\n", (int)reg
);
3165 } else if (epid
== 0 || epid
> 31) {
3166 DPRINTF("xhci: bad doorbell %d write: 0x%x\n",
3167 (int)reg
, (uint32_t)val
);
3169 xhci_kick_ep(xhci
, reg
, epid
, streamid
);
3174 static void xhci_cap_write(void *opaque
, hwaddr addr
, uint64_t val
,
3180 static const MemoryRegionOps xhci_cap_ops
= {
3181 .read
= xhci_cap_read
,
3182 .write
= xhci_cap_write
,
3183 .valid
.min_access_size
= 1,
3184 .valid
.max_access_size
= 4,
3185 .impl
.min_access_size
= 4,
3186 .impl
.max_access_size
= 4,
3187 .endianness
= DEVICE_LITTLE_ENDIAN
,
3190 static const MemoryRegionOps xhci_oper_ops
= {
3191 .read
= xhci_oper_read
,
3192 .write
= xhci_oper_write
,
3193 .valid
.min_access_size
= 4,
3194 .valid
.max_access_size
= sizeof(dma_addr_t
),
3195 .endianness
= DEVICE_LITTLE_ENDIAN
,
3198 static const MemoryRegionOps xhci_port_ops
= {
3199 .read
= xhci_port_read
,
3200 .write
= xhci_port_write
,
3201 .valid
.min_access_size
= 4,
3202 .valid
.max_access_size
= 4,
3203 .endianness
= DEVICE_LITTLE_ENDIAN
,
3206 static const MemoryRegionOps xhci_runtime_ops
= {
3207 .read
= xhci_runtime_read
,
3208 .write
= xhci_runtime_write
,
3209 .valid
.min_access_size
= 4,
3210 .valid
.max_access_size
= sizeof(dma_addr_t
),
3211 .endianness
= DEVICE_LITTLE_ENDIAN
,
3214 static const MemoryRegionOps xhci_doorbell_ops
= {
3215 .read
= xhci_doorbell_read
,
3216 .write
= xhci_doorbell_write
,
3217 .valid
.min_access_size
= 4,
3218 .valid
.max_access_size
= 4,
3219 .endianness
= DEVICE_LITTLE_ENDIAN
,
3222 static void xhci_attach(USBPort
*usbport
)
3224 XHCIState
*xhci
= usbport
->opaque
;
3225 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3227 xhci_port_update(port
, 0);
3230 static void xhci_detach(USBPort
*usbport
)
3232 XHCIState
*xhci
= usbport
->opaque
;
3233 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3235 xhci_detach_slot(xhci
, usbport
);
3236 xhci_port_update(port
, 1);
3239 static void xhci_wakeup(USBPort
*usbport
)
3241 XHCIState
*xhci
= usbport
->opaque
;
3242 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3245 if (get_field(port
->portsc
, PORTSC_PLS
) != PLS_U3
) {
3248 set_field(&port
->portsc
, PLS_RESUME
, PORTSC_PLS
);
3249 xhci_port_notify(port
, PORTSC_PLC
);
3252 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
3254 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
3256 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
3257 xhci_ep_nuke_one_xfer(xfer
, 0);
3260 xhci_try_complete_packet(xfer
);
3261 xhci_kick_epctx(xfer
->epctx
, xfer
->streamid
);
3262 if (xfer
->complete
) {
3263 xhci_ep_free_xfer(xfer
);
3267 static void xhci_child_detach(USBPort
*uport
, USBDevice
*child
)
3269 USBBus
*bus
= usb_bus_from_device(child
);
3270 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3272 xhci_detach_slot(xhci
, child
->port
);
3275 static USBPortOps xhci_uport_ops
= {
3276 .attach
= xhci_attach
,
3277 .detach
= xhci_detach
,
3278 .wakeup
= xhci_wakeup
,
3279 .complete
= xhci_complete
,
3280 .child_detach
= xhci_child_detach
,
3283 static int xhci_find_epid(USBEndpoint
*ep
)
3288 if (ep
->pid
== USB_TOKEN_IN
) {
3289 return ep
->nr
* 2 + 1;
3295 static USBEndpoint
*xhci_epid_to_usbep(XHCIEPContext
*epctx
)
3303 uport
= epctx
->xhci
->slots
[epctx
->slotid
- 1].uport
;
3304 if (!uport
|| !uport
->dev
) {
3307 token
= (epctx
->epid
& 1) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
3308 return usb_ep_get(uport
->dev
, token
, epctx
->epid
>> 1);
3311 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
3312 unsigned int stream
)
3314 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3317 DPRINTF("%s\n", __func__
);
3318 slotid
= ep
->dev
->addr
;
3319 if (slotid
== 0 || slotid
> xhci
->numslots
||
3320 !xhci
->slots
[slotid
- 1].enabled
) {
3321 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
3324 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
), stream
);
3327 static USBBusOps xhci_bus_ops
= {
3328 .wakeup_endpoint
= xhci_wakeup_endpoint
,
3331 static void usb_xhci_init(XHCIState
*xhci
)
3334 unsigned int i
, usbports
, speedmask
;
3336 xhci
->usbsts
= USBSTS_HCH
;
3338 if (xhci
->numports_2
> XHCI_MAXPORTS_2
) {
3339 xhci
->numports_2
= XHCI_MAXPORTS_2
;
3341 if (xhci
->numports_3
> XHCI_MAXPORTS_3
) {
3342 xhci
->numports_3
= XHCI_MAXPORTS_3
;
3344 usbports
= MAX(xhci
->numports_2
, xhci
->numports_3
);
3345 xhci
->numports
= xhci
->numports_2
+ xhci
->numports_3
;
3347 usb_bus_new(&xhci
->bus
, sizeof(xhci
->bus
), &xhci_bus_ops
, xhci
->hostOpaque
);
3349 for (i
= 0; i
< usbports
; i
++) {
3351 if (i
< xhci
->numports_2
) {
3352 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3353 port
= &xhci
->ports
[i
+ xhci
->numports_3
];
3354 port
->portnr
= i
+ 1 + xhci
->numports_3
;
3356 port
= &xhci
->ports
[i
];
3357 port
->portnr
= i
+ 1;
3359 port
->uport
= &xhci
->uports
[i
];
3361 USB_SPEED_MASK_LOW
|
3362 USB_SPEED_MASK_FULL
|
3363 USB_SPEED_MASK_HIGH
;
3364 assert(i
< XHCI_MAXPORTS
);
3365 snprintf(port
->name
, sizeof(port
->name
), "usb2 port #%d", i
+1);
3366 speedmask
|= port
->speedmask
;
3368 if (i
< xhci
->numports_3
) {
3369 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3370 port
= &xhci
->ports
[i
];
3371 port
->portnr
= i
+ 1;
3373 port
= &xhci
->ports
[i
+ xhci
->numports_2
];
3374 port
->portnr
= i
+ 1 + xhci
->numports_2
;
3376 port
->uport
= &xhci
->uports
[i
];
3377 port
->speedmask
= USB_SPEED_MASK_SUPER
;
3378 assert(i
< XHCI_MAXPORTS
);
3379 snprintf(port
->name
, sizeof(port
->name
), "usb3 port #%d", i
+1);
3380 speedmask
|= port
->speedmask
;
3382 usb_register_port(&xhci
->bus
, &xhci
->uports
[i
], xhci
, i
,
3383 &xhci_uport_ops
, speedmask
);
3387 static void usb_xhci_realize(DeviceState
*dev
, Error
**errp
)
3391 XHCIState
*xhci
= XHCI(dev
);
3393 if (xhci
->numintrs
> XHCI_MAXINTRS
) {
3394 xhci
->numintrs
= XHCI_MAXINTRS
;
3396 while (xhci
->numintrs
& (xhci
->numintrs
- 1)) { /* ! power of 2 */
3399 if (xhci
->numintrs
< 1) {
3402 if (xhci
->numslots
> XHCI_MAXSLOTS
) {
3403 xhci
->numslots
= XHCI_MAXSLOTS
;
3405 if (xhci
->numslots
< 1) {
3408 if (xhci_get_flag(xhci
, XHCI_FLAG_ENABLE_STREAMS
)) {
3409 xhci
->max_pstreams_mask
= 7; /* == 256 primary streams */
3411 xhci
->max_pstreams_mask
= 0;
3414 usb_xhci_init(xhci
);
3415 xhci
->mfwrap_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_mfwrap_timer
, xhci
);
3417 memory_region_init(&xhci
->mem
, OBJECT(dev
), "xhci", XHCI_LEN_REGS
);
3418 memory_region_init_io(&xhci
->mem_cap
, OBJECT(dev
), &xhci_cap_ops
, xhci
,
3419 "capabilities", LEN_CAP
);
3420 memory_region_init_io(&xhci
->mem_oper
, OBJECT(dev
), &xhci_oper_ops
, xhci
,
3421 "operational", 0x400);
3422 memory_region_init_io(&xhci
->mem_runtime
, OBJECT(dev
), &xhci_runtime_ops
,
3423 xhci
, "runtime", LEN_RUNTIME
);
3424 memory_region_init_io(&xhci
->mem_doorbell
, OBJECT(dev
), &xhci_doorbell_ops
,
3425 xhci
, "doorbell", LEN_DOORBELL
);
3427 memory_region_add_subregion(&xhci
->mem
, 0, &xhci
->mem_cap
);
3428 memory_region_add_subregion(&xhci
->mem
, OFF_OPER
, &xhci
->mem_oper
);
3429 memory_region_add_subregion(&xhci
->mem
, OFF_RUNTIME
, &xhci
->mem_runtime
);
3430 memory_region_add_subregion(&xhci
->mem
, OFF_DOORBELL
, &xhci
->mem_doorbell
);
3432 for (i
= 0; i
< xhci
->numports
; i
++) {
3433 XHCIPort
*port
= &xhci
->ports
[i
];
3434 uint32_t offset
= OFF_OPER
+ 0x400 + 0x10 * i
;
3436 memory_region_init_io(&port
->mem
, OBJECT(dev
), &xhci_port_ops
, port
,
3438 memory_region_add_subregion(&xhci
->mem
, offset
, &port
->mem
);
3442 static void usb_xhci_unrealize(DeviceState
*dev
)
3445 XHCIState
*xhci
= XHCI(dev
);
3447 trace_usb_xhci_exit();
3449 for (i
= 0; i
< xhci
->numslots
; i
++) {
3450 xhci_disable_slot(xhci
, i
+ 1);
3453 if (xhci
->mfwrap_timer
) {
3454 timer_free(xhci
->mfwrap_timer
);
3455 xhci
->mfwrap_timer
= NULL
;
3458 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_cap
);
3459 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_oper
);
3460 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_runtime
);
3461 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_doorbell
);
3463 for (i
= 0; i
< xhci
->numports
; i
++) {
3464 XHCIPort
*port
= &xhci
->ports
[i
];
3465 memory_region_del_subregion(&xhci
->mem
, &port
->mem
);
3468 usb_bus_release(&xhci
->bus
);
3471 static int usb_xhci_post_load(void *opaque
, int version_id
)
3473 XHCIState
*xhci
= opaque
;
3475 XHCIEPContext
*epctx
;
3476 dma_addr_t dcbaap
, pctx
;
3477 uint32_t slot_ctx
[4];
3479 int slotid
, epid
, state
;
3482 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
3484 for (slotid
= 1; slotid
<= xhci
->numslots
; slotid
++) {
3485 slot
= &xhci
->slots
[slotid
-1];
3486 if (!slot
->addressed
) {
3489 ldq_le_dma(xhci
->as
, dcbaap
+ 8 * slotid
, &addr
, MEMTXATTRS_UNSPECIFIED
);
3490 slot
->ctx
= xhci_mask64(addr
);
3492 xhci_dma_read_u32s(xhci
, slot
->ctx
, slot_ctx
, sizeof(slot_ctx
));
3493 slot
->uport
= xhci_lookup_uport(xhci
, slot_ctx
);
3495 /* should not happen, but may trigger on guest bugs */
3497 slot
->addressed
= 0;
3500 assert(slot
->uport
&& slot
->uport
->dev
);
3502 for (epid
= 1; epid
<= 31; epid
++) {
3503 pctx
= slot
->ctx
+ 32 * epid
;
3504 xhci_dma_read_u32s(xhci
, pctx
, ep_ctx
, sizeof(ep_ctx
));
3505 state
= ep_ctx
[0] & EP_STATE_MASK
;
3506 if (state
== EP_DISABLED
) {
3509 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
3510 slot
->eps
[epid
-1] = epctx
;
3511 xhci_init_epctx(epctx
, pctx
, ep_ctx
);
3512 epctx
->state
= state
;
3513 if (state
== EP_RUNNING
) {
3514 /* kick endpoint after vmload is finished */
3515 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
3522 static const VMStateDescription vmstate_xhci_ring
= {
3523 .name
= "xhci-ring",
3525 .fields
= (const VMStateField
[]) {
3526 VMSTATE_UINT64(dequeue
, XHCIRing
),
3527 VMSTATE_BOOL(ccs
, XHCIRing
),
3528 VMSTATE_END_OF_LIST()
3532 static const VMStateDescription vmstate_xhci_port
= {
3533 .name
= "xhci-port",
3535 .fields
= (const VMStateField
[]) {
3536 VMSTATE_UINT32(portsc
, XHCIPort
),
3537 VMSTATE_END_OF_LIST()
3541 static const VMStateDescription vmstate_xhci_slot
= {
3542 .name
= "xhci-slot",
3544 .fields
= (const VMStateField
[]) {
3545 VMSTATE_BOOL(enabled
, XHCISlot
),
3546 VMSTATE_BOOL(addressed
, XHCISlot
),
3547 VMSTATE_END_OF_LIST()
3551 static const VMStateDescription vmstate_xhci_event
= {
3552 .name
= "xhci-event",
3554 .fields
= (const VMStateField
[]) {
3555 VMSTATE_UINT32(type
, XHCIEvent
),
3556 VMSTATE_UINT32(ccode
, XHCIEvent
),
3557 VMSTATE_UINT64(ptr
, XHCIEvent
),
3558 VMSTATE_UINT32(length
, XHCIEvent
),
3559 VMSTATE_UINT32(flags
, XHCIEvent
),
3560 VMSTATE_UINT8(slotid
, XHCIEvent
),
3561 VMSTATE_UINT8(epid
, XHCIEvent
),
3562 VMSTATE_END_OF_LIST()
3566 static bool xhci_er_full(void *opaque
, int version_id
)
3571 static const VMStateDescription vmstate_xhci_intr
= {
3572 .name
= "xhci-intr",
3574 .fields
= (const VMStateField
[]) {
3576 VMSTATE_UINT32(iman
, XHCIInterrupter
),
3577 VMSTATE_UINT32(imod
, XHCIInterrupter
),
3578 VMSTATE_UINT32(erstsz
, XHCIInterrupter
),
3579 VMSTATE_UINT32(erstba_low
, XHCIInterrupter
),
3580 VMSTATE_UINT32(erstba_high
, XHCIInterrupter
),
3581 VMSTATE_UINT32(erdp_low
, XHCIInterrupter
),
3582 VMSTATE_UINT32(erdp_high
, XHCIInterrupter
),
3585 VMSTATE_BOOL(msix_used
, XHCIInterrupter
),
3586 VMSTATE_BOOL(er_pcs
, XHCIInterrupter
),
3587 VMSTATE_UINT64(er_start
, XHCIInterrupter
),
3588 VMSTATE_UINT32(er_size
, XHCIInterrupter
),
3589 VMSTATE_UINT32(er_ep_idx
, XHCIInterrupter
),
3591 /* event queue (used if ring is full) */
3592 VMSTATE_BOOL(er_full_unused
, XHCIInterrupter
),
3593 VMSTATE_UINT32_TEST(ev_buffer_put
, XHCIInterrupter
, xhci_er_full
),
3594 VMSTATE_UINT32_TEST(ev_buffer_get
, XHCIInterrupter
, xhci_er_full
),
3595 VMSTATE_STRUCT_ARRAY_TEST(ev_buffer
, XHCIInterrupter
, EV_QUEUE
,
3597 vmstate_xhci_event
, XHCIEvent
),
3599 VMSTATE_END_OF_LIST()
3603 const VMStateDescription vmstate_xhci
= {
3604 .name
= "xhci-core",
3606 .post_load
= usb_xhci_post_load
,
3607 .fields
= (const VMStateField
[]) {
3608 VMSTATE_STRUCT_VARRAY_UINT32(ports
, XHCIState
, numports
, 1,
3609 vmstate_xhci_port
, XHCIPort
),
3610 VMSTATE_STRUCT_VARRAY_UINT32(slots
, XHCIState
, numslots
, 1,
3611 vmstate_xhci_slot
, XHCISlot
),
3612 VMSTATE_STRUCT_VARRAY_UINT32(intr
, XHCIState
, numintrs
, 1,
3613 vmstate_xhci_intr
, XHCIInterrupter
),
3615 /* Operational Registers */
3616 VMSTATE_UINT32(usbcmd
, XHCIState
),
3617 VMSTATE_UINT32(usbsts
, XHCIState
),
3618 VMSTATE_UINT32(dnctrl
, XHCIState
),
3619 VMSTATE_UINT32(crcr_low
, XHCIState
),
3620 VMSTATE_UINT32(crcr_high
, XHCIState
),
3621 VMSTATE_UINT32(dcbaap_low
, XHCIState
),
3622 VMSTATE_UINT32(dcbaap_high
, XHCIState
),
3623 VMSTATE_UINT32(config
, XHCIState
),
3625 /* Runtime Registers & state */
3626 VMSTATE_INT64(mfindex_start
, XHCIState
),
3627 VMSTATE_TIMER_PTR(mfwrap_timer
, XHCIState
),
3628 VMSTATE_STRUCT(cmd_ring
, XHCIState
, 1, vmstate_xhci_ring
, XHCIRing
),
3630 VMSTATE_END_OF_LIST()
3634 static Property xhci_properties
[] = {
3635 DEFINE_PROP_BIT("streams", XHCIState
, flags
,
3636 XHCI_FLAG_ENABLE_STREAMS
, true),
3637 DEFINE_PROP_UINT32("p2", XHCIState
, numports_2
, 4),
3638 DEFINE_PROP_UINT32("p3", XHCIState
, numports_3
, 4),
3639 DEFINE_PROP_LINK("host", XHCIState
, hostOpaque
, TYPE_DEVICE
,
3641 DEFINE_PROP_END_OF_LIST(),
3644 static void xhci_class_init(ObjectClass
*klass
, void *data
)
3646 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3648 dc
->realize
= usb_xhci_realize
;
3649 dc
->unrealize
= usb_xhci_unrealize
;
3650 dc
->reset
= xhci_reset
;
3651 device_class_set_props(dc
, xhci_properties
);
3652 dc
->user_creatable
= false;
3655 static const TypeInfo xhci_info
= {
3657 .parent
= TYPE_DEVICE
,
3658 .instance_size
= sizeof(XHCIState
),
3659 .class_init
= xhci_class_init
,
3662 static void xhci_register_types(void)
3664 type_register_static(&xhci_info
);
3667 type_init(xhci_register_types
)