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"
24 #include "qemu/module.h"
25 #include "qemu/queue.h"
26 #include "migration/vmstate.h"
27 #include "hw/qdev-properties.h"
29 #include "qapi/error.h"
37 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
39 #define DPRINTF(...) do {} while (0)
41 #define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \
42 __func__, __LINE__, _msg); abort(); } while (0)
44 #define TRB_LINK_LIMIT 32
45 #define COMMAND_LIMIT 256
46 #define TRANSFER_LIMIT 256
49 #define LEN_OPER (0x400 + 0x10 * XHCI_MAXPORTS)
50 #define LEN_RUNTIME ((XHCI_MAXINTRS + 1) * 0x20)
51 #define LEN_DOORBELL ((XHCI_MAXSLOTS + 1) * 0x20)
53 #define OFF_OPER LEN_CAP
54 #define OFF_RUNTIME 0x1000
55 #define OFF_DOORBELL 0x2000
57 #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
58 #error Increase OFF_RUNTIME
60 #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
61 #error Increase OFF_DOORBELL
63 #if (OFF_DOORBELL + LEN_DOORBELL) > XHCI_LEN_REGS
64 # error Increase XHCI_LEN_REGS
68 #define USBCMD_RS (1<<0)
69 #define USBCMD_HCRST (1<<1)
70 #define USBCMD_INTE (1<<2)
71 #define USBCMD_HSEE (1<<3)
72 #define USBCMD_LHCRST (1<<7)
73 #define USBCMD_CSS (1<<8)
74 #define USBCMD_CRS (1<<9)
75 #define USBCMD_EWE (1<<10)
76 #define USBCMD_EU3S (1<<11)
78 #define USBSTS_HCH (1<<0)
79 #define USBSTS_HSE (1<<2)
80 #define USBSTS_EINT (1<<3)
81 #define USBSTS_PCD (1<<4)
82 #define USBSTS_SSS (1<<8)
83 #define USBSTS_RSS (1<<9)
84 #define USBSTS_SRE (1<<10)
85 #define USBSTS_CNR (1<<11)
86 #define USBSTS_HCE (1<<12)
89 #define PORTSC_CCS (1<<0)
90 #define PORTSC_PED (1<<1)
91 #define PORTSC_OCA (1<<3)
92 #define PORTSC_PR (1<<4)
93 #define PORTSC_PLS_SHIFT 5
94 #define PORTSC_PLS_MASK 0xf
95 #define PORTSC_PP (1<<9)
96 #define PORTSC_SPEED_SHIFT 10
97 #define PORTSC_SPEED_MASK 0xf
98 #define PORTSC_SPEED_FULL (1<<10)
99 #define PORTSC_SPEED_LOW (2<<10)
100 #define PORTSC_SPEED_HIGH (3<<10)
101 #define PORTSC_SPEED_SUPER (4<<10)
102 #define PORTSC_PIC_SHIFT 14
103 #define PORTSC_PIC_MASK 0x3
104 #define PORTSC_LWS (1<<16)
105 #define PORTSC_CSC (1<<17)
106 #define PORTSC_PEC (1<<18)
107 #define PORTSC_WRC (1<<19)
108 #define PORTSC_OCC (1<<20)
109 #define PORTSC_PRC (1<<21)
110 #define PORTSC_PLC (1<<22)
111 #define PORTSC_CEC (1<<23)
112 #define PORTSC_CAS (1<<24)
113 #define PORTSC_WCE (1<<25)
114 #define PORTSC_WDE (1<<26)
115 #define PORTSC_WOE (1<<27)
116 #define PORTSC_DR (1<<30)
117 #define PORTSC_WPR (1<<31)
119 #define CRCR_RCS (1<<0)
120 #define CRCR_CS (1<<1)
121 #define CRCR_CA (1<<2)
122 #define CRCR_CRR (1<<3)
124 #define IMAN_IP (1<<0)
125 #define IMAN_IE (1<<1)
127 #define ERDP_EHB (1<<3)
130 typedef struct XHCITRB
{
149 PLS_COMPILANCE_MODE
= 10,
154 #define CR_LINK TR_LINK
157 #define TRB_TYPE_SHIFT 10
158 #define TRB_TYPE_MASK 0x3f
159 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
161 #define TRB_EV_ED (1<<2)
163 #define TRB_TR_ENT (1<<1)
164 #define TRB_TR_ISP (1<<2)
165 #define TRB_TR_NS (1<<3)
166 #define TRB_TR_CH (1<<4)
167 #define TRB_TR_IOC (1<<5)
168 #define TRB_TR_IDT (1<<6)
169 #define TRB_TR_TBC_SHIFT 7
170 #define TRB_TR_TBC_MASK 0x3
171 #define TRB_TR_BEI (1<<9)
172 #define TRB_TR_TLBPC_SHIFT 16
173 #define TRB_TR_TLBPC_MASK 0xf
174 #define TRB_TR_FRAMEID_SHIFT 20
175 #define TRB_TR_FRAMEID_MASK 0x7ff
176 #define TRB_TR_SIA (1<<31)
178 #define TRB_TR_DIR (1<<16)
180 #define TRB_CR_SLOTID_SHIFT 24
181 #define TRB_CR_SLOTID_MASK 0xff
182 #define TRB_CR_EPID_SHIFT 16
183 #define TRB_CR_EPID_MASK 0x1f
185 #define TRB_CR_BSR (1<<9)
186 #define TRB_CR_DC (1<<9)
188 #define TRB_LK_TC (1<<1)
190 #define TRB_INTR_SHIFT 22
191 #define TRB_INTR_MASK 0x3ff
192 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
194 #define EP_TYPE_MASK 0x7
195 #define EP_TYPE_SHIFT 3
197 #define EP_STATE_MASK 0x7
198 #define EP_DISABLED (0<<0)
199 #define EP_RUNNING (1<<0)
200 #define EP_HALTED (2<<0)
201 #define EP_STOPPED (3<<0)
202 #define EP_ERROR (4<<0)
204 #define SLOT_STATE_MASK 0x1f
205 #define SLOT_STATE_SHIFT 27
206 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
207 #define SLOT_ENABLED 0
208 #define SLOT_DEFAULT 1
209 #define SLOT_ADDRESSED 2
210 #define SLOT_CONFIGURED 3
212 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
213 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
215 #define get_field(data, field) \
216 (((data) >> field##_SHIFT) & field##_MASK)
218 #define set_field(data, newval, field) do { \
219 uint32_t val = *data; \
220 val &= ~(field##_MASK << field##_SHIFT); \
221 val |= ((newval) & field##_MASK) << field##_SHIFT; \
225 typedef enum EPType
{
236 typedef struct XHCITransfer
{
237 XHCIEPContext
*epctx
;
244 unsigned int iso_pkts
;
245 unsigned int streamid
;
250 unsigned int trb_count
;
256 unsigned int pktsize
;
257 unsigned int cur_pkt
;
259 uint64_t mfindex_kick
;
261 QTAILQ_ENTRY(XHCITransfer
) next
;
264 struct XHCIStreamContext
{
270 struct XHCIEPContext
{
277 QTAILQ_HEAD(, XHCITransfer
) transfers
;
281 unsigned int max_psize
;
283 uint32_t kick_active
;
286 unsigned int max_pstreams
;
288 unsigned int nr_pstreams
;
289 XHCIStreamContext
*pstreams
;
291 /* iso xfer scheduling */
292 unsigned int interval
;
293 int64_t mfindex_last
;
294 QEMUTimer
*kick_timer
;
297 typedef struct XHCIEvRingSeg
{
304 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
305 unsigned int epid
, unsigned int streamid
);
306 static void xhci_kick_epctx(XHCIEPContext
*epctx
, unsigned int streamid
);
307 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
309 static void xhci_xfer_report(XHCITransfer
*xfer
);
310 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
311 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
312 static USBEndpoint
*xhci_epid_to_usbep(XHCIEPContext
*epctx
);
314 static const char *TRBType_names
[] = {
315 [TRB_RESERVED
] = "TRB_RESERVED",
316 [TR_NORMAL
] = "TR_NORMAL",
317 [TR_SETUP
] = "TR_SETUP",
318 [TR_DATA
] = "TR_DATA",
319 [TR_STATUS
] = "TR_STATUS",
320 [TR_ISOCH
] = "TR_ISOCH",
321 [TR_LINK
] = "TR_LINK",
322 [TR_EVDATA
] = "TR_EVDATA",
323 [TR_NOOP
] = "TR_NOOP",
324 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
325 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
326 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
327 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
328 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
329 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
330 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
331 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
332 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
333 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
334 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
335 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
336 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
337 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
338 [CR_NOOP
] = "CR_NOOP",
339 [ER_TRANSFER
] = "ER_TRANSFER",
340 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
341 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
342 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
343 [ER_DOORBELL
] = "ER_DOORBELL",
344 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
345 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
346 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
347 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
348 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
351 static const char *TRBCCode_names
[] = {
352 [CC_INVALID
] = "CC_INVALID",
353 [CC_SUCCESS
] = "CC_SUCCESS",
354 [CC_DATA_BUFFER_ERROR
] = "CC_DATA_BUFFER_ERROR",
355 [CC_BABBLE_DETECTED
] = "CC_BABBLE_DETECTED",
356 [CC_USB_TRANSACTION_ERROR
] = "CC_USB_TRANSACTION_ERROR",
357 [CC_TRB_ERROR
] = "CC_TRB_ERROR",
358 [CC_STALL_ERROR
] = "CC_STALL_ERROR",
359 [CC_RESOURCE_ERROR
] = "CC_RESOURCE_ERROR",
360 [CC_BANDWIDTH_ERROR
] = "CC_BANDWIDTH_ERROR",
361 [CC_NO_SLOTS_ERROR
] = "CC_NO_SLOTS_ERROR",
362 [CC_INVALID_STREAM_TYPE_ERROR
] = "CC_INVALID_STREAM_TYPE_ERROR",
363 [CC_SLOT_NOT_ENABLED_ERROR
] = "CC_SLOT_NOT_ENABLED_ERROR",
364 [CC_EP_NOT_ENABLED_ERROR
] = "CC_EP_NOT_ENABLED_ERROR",
365 [CC_SHORT_PACKET
] = "CC_SHORT_PACKET",
366 [CC_RING_UNDERRUN
] = "CC_RING_UNDERRUN",
367 [CC_RING_OVERRUN
] = "CC_RING_OVERRUN",
368 [CC_VF_ER_FULL
] = "CC_VF_ER_FULL",
369 [CC_PARAMETER_ERROR
] = "CC_PARAMETER_ERROR",
370 [CC_BANDWIDTH_OVERRUN
] = "CC_BANDWIDTH_OVERRUN",
371 [CC_CONTEXT_STATE_ERROR
] = "CC_CONTEXT_STATE_ERROR",
372 [CC_NO_PING_RESPONSE_ERROR
] = "CC_NO_PING_RESPONSE_ERROR",
373 [CC_EVENT_RING_FULL_ERROR
] = "CC_EVENT_RING_FULL_ERROR",
374 [CC_INCOMPATIBLE_DEVICE_ERROR
] = "CC_INCOMPATIBLE_DEVICE_ERROR",
375 [CC_MISSED_SERVICE_ERROR
] = "CC_MISSED_SERVICE_ERROR",
376 [CC_COMMAND_RING_STOPPED
] = "CC_COMMAND_RING_STOPPED",
377 [CC_COMMAND_ABORTED
] = "CC_COMMAND_ABORTED",
378 [CC_STOPPED
] = "CC_STOPPED",
379 [CC_STOPPED_LENGTH_INVALID
] = "CC_STOPPED_LENGTH_INVALID",
380 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
]
381 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
382 [CC_ISOCH_BUFFER_OVERRUN
] = "CC_ISOCH_BUFFER_OVERRUN",
383 [CC_EVENT_LOST_ERROR
] = "CC_EVENT_LOST_ERROR",
384 [CC_UNDEFINED_ERROR
] = "CC_UNDEFINED_ERROR",
385 [CC_INVALID_STREAM_ID_ERROR
] = "CC_INVALID_STREAM_ID_ERROR",
386 [CC_SECONDARY_BANDWIDTH_ERROR
] = "CC_SECONDARY_BANDWIDTH_ERROR",
387 [CC_SPLIT_TRANSACTION_ERROR
] = "CC_SPLIT_TRANSACTION_ERROR",
390 static const char *ep_state_names
[] = {
391 [EP_DISABLED
] = "disabled",
392 [EP_RUNNING
] = "running",
393 [EP_HALTED
] = "halted",
394 [EP_STOPPED
] = "stopped",
395 [EP_ERROR
] = "error",
398 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
400 if (index
>= llen
|| list
[index
] == NULL
) {
406 static const char *trb_name(XHCITRB
*trb
)
408 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
409 ARRAY_SIZE(TRBType_names
));
412 static const char *event_name(XHCIEvent
*event
)
414 return lookup_name(event
->ccode
, TRBCCode_names
,
415 ARRAY_SIZE(TRBCCode_names
));
418 static const char *ep_state_name(uint32_t state
)
420 return lookup_name(state
, ep_state_names
,
421 ARRAY_SIZE(ep_state_names
));
424 bool xhci_get_flag(XHCIState
*xhci
, enum xhci_flags bit
)
426 return xhci
->flags
& (1 << bit
);
429 void xhci_set_flag(XHCIState
*xhci
, enum xhci_flags bit
)
431 xhci
->flags
|= (1 << bit
);
434 static uint64_t xhci_mfindex_get(XHCIState
*xhci
)
436 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
437 return (now
- xhci
->mfindex_start
) / 125000;
440 static void xhci_mfwrap_update(XHCIState
*xhci
)
442 const uint32_t bits
= USBCMD_RS
| USBCMD_EWE
;
443 uint32_t mfindex
, left
;
446 if ((xhci
->usbcmd
& bits
) == bits
) {
447 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
448 mfindex
= ((now
- xhci
->mfindex_start
) / 125000) & 0x3fff;
449 left
= 0x4000 - mfindex
;
450 timer_mod(xhci
->mfwrap_timer
, now
+ left
* 125000);
452 timer_del(xhci
->mfwrap_timer
);
456 static void xhci_mfwrap_timer(void *opaque
)
458 XHCIState
*xhci
= opaque
;
459 XHCIEvent wrap
= { ER_MFINDEX_WRAP
, CC_SUCCESS
};
461 xhci_event(xhci
, &wrap
, 0);
462 xhci_mfwrap_update(xhci
);
465 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
467 if (sizeof(dma_addr_t
) == 4) {
470 return low
| (((dma_addr_t
)high
<< 16) << 16);
474 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
476 if (sizeof(dma_addr_t
) == 4) {
477 return addr
& 0xffffffff;
483 static inline void xhci_dma_read_u32s(XHCIState
*xhci
, dma_addr_t addr
,
484 uint32_t *buf
, size_t len
)
488 assert((len
% sizeof(uint32_t)) == 0);
490 dma_memory_read(xhci
->as
, addr
, buf
, len
);
492 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
493 buf
[i
] = le32_to_cpu(buf
[i
]);
497 static inline void xhci_dma_write_u32s(XHCIState
*xhci
, dma_addr_t addr
,
498 uint32_t *buf
, size_t len
)
502 uint32_t n
= len
/ sizeof(uint32_t);
504 assert((len
% sizeof(uint32_t)) == 0);
505 assert(n
<= ARRAY_SIZE(tmp
));
507 for (i
= 0; i
< n
; i
++) {
508 tmp
[i
] = cpu_to_le32(buf
[i
]);
510 dma_memory_write(xhci
->as
, addr
, tmp
, len
);
513 static XHCIPort
*xhci_lookup_port(XHCIState
*xhci
, struct USBPort
*uport
)
520 switch (uport
->dev
->speed
) {
524 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
525 index
= uport
->index
+ xhci
->numports_3
;
527 index
= uport
->index
;
530 case USB_SPEED_SUPER
:
531 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
532 index
= uport
->index
;
534 index
= uport
->index
+ xhci
->numports_2
;
540 return &xhci
->ports
[index
];
543 static void xhci_intr_update(XHCIState
*xhci
, int v
)
548 if (xhci
->intr
[0].iman
& IMAN_IP
&&
549 xhci
->intr
[0].iman
& IMAN_IE
&&
550 xhci
->usbcmd
& USBCMD_INTE
) {
553 if (xhci
->intr_raise
) {
554 if (xhci
->intr_raise(xhci
, 0, level
)) {
555 xhci
->intr
[0].iman
&= ~IMAN_IP
;
559 if (xhci
->intr_update
) {
560 xhci
->intr_update(xhci
, v
,
561 xhci
->intr
[v
].iman
& IMAN_IE
);
565 static void xhci_intr_raise(XHCIState
*xhci
, int v
)
567 bool pending
= (xhci
->intr
[v
].erdp_low
& ERDP_EHB
);
569 xhci
->intr
[v
].erdp_low
|= ERDP_EHB
;
570 xhci
->intr
[v
].iman
|= IMAN_IP
;
571 xhci
->usbsts
|= USBSTS_EINT
;
576 if (!(xhci
->intr
[v
].iman
& IMAN_IE
)) {
580 if (!(xhci
->usbcmd
& USBCMD_INTE
)) {
583 if (xhci
->intr_raise
) {
584 if (xhci
->intr_raise(xhci
, v
, true)) {
585 xhci
->intr
[v
].iman
&= ~IMAN_IP
;
590 static inline int xhci_running(XHCIState
*xhci
)
592 return !(xhci
->usbsts
& USBSTS_HCH
);
595 static void xhci_die(XHCIState
*xhci
)
597 xhci
->usbsts
|= USBSTS_HCE
;
598 DPRINTF("xhci: asserted controller error\n");
601 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
603 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
607 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
608 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
609 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
610 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
612 ev_trb
.control
|= TRB_C
;
614 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
616 trace_usb_xhci_queue_event(v
, intr
->er_ep_idx
, trb_name(&ev_trb
),
617 event_name(event
), ev_trb
.parameter
,
618 ev_trb
.status
, ev_trb
.control
);
620 addr
= intr
->er_start
+ TRB_SIZE
*intr
->er_ep_idx
;
621 dma_memory_write(xhci
->as
, addr
, &ev_trb
, TRB_SIZE
);
624 if (intr
->er_ep_idx
>= intr
->er_size
) {
626 intr
->er_pcs
= !intr
->er_pcs
;
630 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
632 XHCIInterrupter
*intr
;
636 if (v
>= xhci
->numintrs
) {
637 DPRINTF("intr nr out of range (%d >= %d)\n", v
, xhci
->numintrs
);
640 intr
= &xhci
->intr
[v
];
642 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
643 if (erdp
< intr
->er_start
||
644 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
645 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
646 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
647 v
, intr
->er_start
, intr
->er_size
);
652 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
653 assert(dp_idx
< intr
->er_size
);
655 if ((intr
->er_ep_idx
+ 2) % intr
->er_size
== dp_idx
) {
656 DPRINTF("xhci: ER %d full, send ring full error\n", v
);
657 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
658 xhci_write_event(xhci
, &full
, v
);
659 } else if ((intr
->er_ep_idx
+ 1) % intr
->er_size
== dp_idx
) {
660 DPRINTF("xhci: ER %d full, drop event\n", v
);
662 xhci_write_event(xhci
, event
, v
);
665 xhci_intr_raise(xhci
, v
);
668 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
671 ring
->dequeue
= base
;
675 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
678 uint32_t link_cnt
= 0;
682 dma_memory_read(xhci
->as
, ring
->dequeue
, trb
, TRB_SIZE
);
683 trb
->addr
= ring
->dequeue
;
684 trb
->ccs
= ring
->ccs
;
685 le64_to_cpus(&trb
->parameter
);
686 le32_to_cpus(&trb
->status
);
687 le32_to_cpus(&trb
->control
);
689 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
690 trb
->parameter
, trb
->status
, trb
->control
);
692 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
696 type
= TRB_TYPE(*trb
);
698 if (type
!= TR_LINK
) {
700 *addr
= ring
->dequeue
;
702 ring
->dequeue
+= TRB_SIZE
;
705 if (++link_cnt
> TRB_LINK_LIMIT
) {
706 trace_usb_xhci_enforced_limit("trb-link");
709 ring
->dequeue
= xhci_mask64(trb
->parameter
);
710 if (trb
->control
& TRB_LK_TC
) {
711 ring
->ccs
= !ring
->ccs
;
717 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
721 dma_addr_t dequeue
= ring
->dequeue
;
722 bool ccs
= ring
->ccs
;
723 /* hack to bundle together the two/three TDs that make a setup transfer */
724 bool control_td_set
= 0;
725 uint32_t link_cnt
= 0;
729 dma_memory_read(xhci
->as
, dequeue
, &trb
, TRB_SIZE
);
730 le64_to_cpus(&trb
.parameter
);
731 le32_to_cpus(&trb
.status
);
732 le32_to_cpus(&trb
.control
);
734 if ((trb
.control
& TRB_C
) != ccs
) {
738 type
= TRB_TYPE(trb
);
740 if (type
== TR_LINK
) {
741 if (++link_cnt
> TRB_LINK_LIMIT
) {
744 dequeue
= xhci_mask64(trb
.parameter
);
745 if (trb
.control
& TRB_LK_TC
) {
754 if (type
== TR_SETUP
) {
756 } else if (type
== TR_STATUS
) {
760 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
766 static void xhci_er_reset(XHCIState
*xhci
, int v
)
768 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
770 dma_addr_t erstba
= xhci_addr64(intr
->erstba_low
, intr
->erstba_high
);
772 if (intr
->erstsz
== 0 || erstba
== 0) {
778 /* cache the (sole) event ring segment location */
779 if (intr
->erstsz
!= 1) {
780 DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr
->erstsz
);
784 dma_memory_read(xhci
->as
, erstba
, &seg
, sizeof(seg
));
785 le32_to_cpus(&seg
.addr_low
);
786 le32_to_cpus(&seg
.addr_high
);
787 le32_to_cpus(&seg
.size
);
788 if (seg
.size
< 16 || seg
.size
> 4096) {
789 DPRINTF("xhci: invalid value for segment size: %d\n", seg
.size
);
793 intr
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
794 intr
->er_size
= seg
.size
;
799 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT
" [%d]\n",
800 v
, intr
->er_start
, intr
->er_size
);
803 static void xhci_run(XHCIState
*xhci
)
805 trace_usb_xhci_run();
806 xhci
->usbsts
&= ~USBSTS_HCH
;
807 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
810 static void xhci_stop(XHCIState
*xhci
)
812 trace_usb_xhci_stop();
813 xhci
->usbsts
|= USBSTS_HCH
;
814 xhci
->crcr_low
&= ~CRCR_CRR
;
817 static XHCIStreamContext
*xhci_alloc_stream_contexts(unsigned count
,
820 XHCIStreamContext
*stctx
;
823 stctx
= g_new0(XHCIStreamContext
, count
);
824 for (i
= 0; i
< count
; i
++) {
825 stctx
[i
].pctx
= base
+ i
* 16;
831 static void xhci_reset_streams(XHCIEPContext
*epctx
)
835 for (i
= 0; i
< epctx
->nr_pstreams
; i
++) {
836 epctx
->pstreams
[i
].sct
= -1;
840 static void xhci_alloc_streams(XHCIEPContext
*epctx
, dma_addr_t base
)
842 assert(epctx
->pstreams
== NULL
);
843 epctx
->nr_pstreams
= 2 << epctx
->max_pstreams
;
844 epctx
->pstreams
= xhci_alloc_stream_contexts(epctx
->nr_pstreams
, base
);
847 static void xhci_free_streams(XHCIEPContext
*epctx
)
849 assert(epctx
->pstreams
!= NULL
);
851 g_free(epctx
->pstreams
);
852 epctx
->pstreams
= NULL
;
853 epctx
->nr_pstreams
= 0;
856 static int xhci_epmask_to_eps_with_streams(XHCIState
*xhci
,
859 XHCIEPContext
**epctxs
,
863 XHCIEPContext
*epctx
;
867 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
869 slot
= &xhci
->slots
[slotid
- 1];
871 for (i
= 2, j
= 0; i
<= 31; i
++) {
872 if (!(epmask
& (1u << i
))) {
876 epctx
= slot
->eps
[i
- 1];
877 ep
= xhci_epid_to_usbep(epctx
);
878 if (!epctx
|| !epctx
->nr_pstreams
|| !ep
) {
890 static void xhci_free_device_streams(XHCIState
*xhci
, unsigned int slotid
,
893 USBEndpoint
*eps
[30];
896 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, NULL
, eps
);
898 usb_device_free_streams(eps
[0]->dev
, eps
, nr_eps
);
902 static TRBCCode
xhci_alloc_device_streams(XHCIState
*xhci
, unsigned int slotid
,
905 XHCIEPContext
*epctxs
[30];
906 USBEndpoint
*eps
[30];
907 int i
, r
, nr_eps
, req_nr_streams
, dev_max_streams
;
909 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, epctxs
,
915 req_nr_streams
= epctxs
[0]->nr_pstreams
;
916 dev_max_streams
= eps
[0]->max_streams
;
918 for (i
= 1; i
< nr_eps
; i
++) {
920 * HdG: I don't expect these to ever trigger, but if they do we need
921 * to come up with another solution, ie group identical endpoints
922 * together and make an usb_device_alloc_streams call per group.
924 if (epctxs
[i
]->nr_pstreams
!= req_nr_streams
) {
925 FIXME("guest streams config not identical for all eps");
926 return CC_RESOURCE_ERROR
;
928 if (eps
[i
]->max_streams
!= dev_max_streams
) {
929 FIXME("device streams config not identical for all eps");
930 return CC_RESOURCE_ERROR
;
935 * max-streams in both the device descriptor and in the controller is a
936 * power of 2. But stream id 0 is reserved, so if a device can do up to 4
937 * streams the guest will ask for 5 rounded up to the next power of 2 which
938 * becomes 8. For emulated devices usb_device_alloc_streams is a nop.
940 * For redirected devices however this is an issue, as there we must ask
941 * the real xhci controller to alloc streams, and the host driver for the
942 * real xhci controller will likely disallow allocating more streams then
943 * the device can handle.
945 * So we limit the requested nr_streams to the maximum number the device
948 if (req_nr_streams
> dev_max_streams
) {
949 req_nr_streams
= dev_max_streams
;
952 r
= usb_device_alloc_streams(eps
[0]->dev
, eps
, nr_eps
, req_nr_streams
);
954 DPRINTF("xhci: alloc streams failed\n");
955 return CC_RESOURCE_ERROR
;
961 static XHCIStreamContext
*xhci_find_stream(XHCIEPContext
*epctx
,
962 unsigned int streamid
,
965 XHCIStreamContext
*sctx
;
967 uint32_t ctx
[2], sct
;
969 assert(streamid
!= 0);
971 if (streamid
>= epctx
->nr_pstreams
) {
972 *cc_error
= CC_INVALID_STREAM_ID_ERROR
;
975 sctx
= epctx
->pstreams
+ streamid
;
977 FIXME("secondary streams not implemented yet");
980 if (sctx
->sct
== -1) {
981 xhci_dma_read_u32s(epctx
->xhci
, sctx
->pctx
, ctx
, sizeof(ctx
));
982 sct
= (ctx
[0] >> 1) & 0x07;
983 if (epctx
->lsa
&& sct
!= 1) {
984 *cc_error
= CC_INVALID_STREAM_TYPE_ERROR
;
988 base
= xhci_addr64(ctx
[0] & ~0xf, ctx
[1]);
989 xhci_ring_init(epctx
->xhci
, &sctx
->ring
, base
);
994 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
995 XHCIStreamContext
*sctx
, uint32_t state
)
997 XHCIRing
*ring
= NULL
;
1001 xhci_dma_read_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1002 ctx
[0] &= ~EP_STATE_MASK
;
1005 /* update ring dequeue ptr */
1006 if (epctx
->nr_pstreams
) {
1009 xhci_dma_read_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1011 ctx2
[0] |= sctx
->ring
.dequeue
| sctx
->ring
.ccs
;
1012 ctx2
[1] = (sctx
->ring
.dequeue
>> 16) >> 16;
1013 xhci_dma_write_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1016 ring
= &epctx
->ring
;
1019 ctx
[2] = ring
->dequeue
| ring
->ccs
;
1020 ctx
[3] = (ring
->dequeue
>> 16) >> 16;
1022 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
1023 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
1026 xhci_dma_write_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1027 if (epctx
->state
!= state
) {
1028 trace_usb_xhci_ep_state(epctx
->slotid
, epctx
->epid
,
1029 ep_state_name(epctx
->state
),
1030 ep_state_name(state
));
1032 epctx
->state
= state
;
1035 static void xhci_ep_kick_timer(void *opaque
)
1037 XHCIEPContext
*epctx
= opaque
;
1038 xhci_kick_epctx(epctx
, 0);
1041 static XHCIEPContext
*xhci_alloc_epctx(XHCIState
*xhci
,
1042 unsigned int slotid
,
1045 XHCIEPContext
*epctx
;
1047 epctx
= g_new0(XHCIEPContext
, 1);
1049 epctx
->slotid
= slotid
;
1052 QTAILQ_INIT(&epctx
->transfers
);
1053 epctx
->kick_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_ep_kick_timer
, epctx
);
1058 static void xhci_init_epctx(XHCIEPContext
*epctx
,
1059 dma_addr_t pctx
, uint32_t *ctx
)
1063 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
1065 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
1067 epctx
->max_psize
= ctx
[1]>>16;
1068 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
1069 epctx
->max_pstreams
= (ctx
[0] >> 10) & epctx
->xhci
->max_pstreams_mask
;
1070 epctx
->lsa
= (ctx
[0] >> 15) & 1;
1071 if (epctx
->max_pstreams
) {
1072 xhci_alloc_streams(epctx
, dequeue
);
1074 xhci_ring_init(epctx
->xhci
, &epctx
->ring
, dequeue
);
1075 epctx
->ring
.ccs
= ctx
[2] & 1;
1078 epctx
->interval
= 1 << ((ctx
[0] >> 16) & 0xff);
1081 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
1082 unsigned int epid
, dma_addr_t pctx
,
1086 XHCIEPContext
*epctx
;
1088 trace_usb_xhci_ep_enable(slotid
, epid
);
1089 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1090 assert(epid
>= 1 && epid
<= 31);
1092 slot
= &xhci
->slots
[slotid
-1];
1093 if (slot
->eps
[epid
-1]) {
1094 xhci_disable_ep(xhci
, slotid
, epid
);
1097 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
1098 slot
->eps
[epid
-1] = epctx
;
1099 xhci_init_epctx(epctx
, pctx
, ctx
);
1101 DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) "
1102 "size is %d\n", epid
/2, epid
%2, epctx
->type
, epctx
->max_psize
);
1104 epctx
->mfindex_last
= 0;
1106 epctx
->state
= EP_RUNNING
;
1107 ctx
[0] &= ~EP_STATE_MASK
;
1108 ctx
[0] |= EP_RUNNING
;
1113 static XHCITransfer
*xhci_ep_alloc_xfer(XHCIEPContext
*epctx
,
1116 uint32_t limit
= epctx
->nr_pstreams
+ 16;
1119 if (epctx
->xfer_count
>= limit
) {
1123 xfer
= g_new0(XHCITransfer
, 1);
1124 xfer
->epctx
= epctx
;
1125 xfer
->trbs
= g_new(XHCITRB
, length
);
1126 xfer
->trb_count
= length
;
1127 usb_packet_init(&xfer
->packet
);
1129 QTAILQ_INSERT_TAIL(&epctx
->transfers
, xfer
, next
);
1130 epctx
->xfer_count
++;
1135 static void xhci_ep_free_xfer(XHCITransfer
*xfer
)
1137 QTAILQ_REMOVE(&xfer
->epctx
->transfers
, xfer
, next
);
1138 xfer
->epctx
->xfer_count
--;
1140 usb_packet_cleanup(&xfer
->packet
);
1145 static int xhci_ep_nuke_one_xfer(XHCITransfer
*t
, TRBCCode report
)
1149 if (report
&& (t
->running_async
|| t
->running_retry
)) {
1151 xhci_xfer_report(t
);
1154 if (t
->running_async
) {
1155 usb_cancel_packet(&t
->packet
);
1156 t
->running_async
= 0;
1159 if (t
->running_retry
) {
1161 t
->epctx
->retry
= NULL
;
1162 timer_del(t
->epctx
->kick_timer
);
1164 t
->running_retry
= 0;
1175 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
1176 unsigned int epid
, TRBCCode report
)
1179 XHCIEPContext
*epctx
;
1182 USBEndpoint
*ep
= NULL
;
1183 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1184 assert(epid
>= 1 && epid
<= 31);
1186 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
1188 slot
= &xhci
->slots
[slotid
-1];
1190 if (!slot
->eps
[epid
-1]) {
1194 epctx
= slot
->eps
[epid
-1];
1197 xfer
= QTAILQ_FIRST(&epctx
->transfers
);
1201 killed
+= xhci_ep_nuke_one_xfer(xfer
, report
);
1203 report
= 0; /* Only report once */
1205 xhci_ep_free_xfer(xfer
);
1208 ep
= xhci_epid_to_usbep(epctx
);
1210 usb_device_ep_stopped(ep
->dev
, ep
);
1215 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
1219 XHCIEPContext
*epctx
;
1221 trace_usb_xhci_ep_disable(slotid
, epid
);
1222 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1223 assert(epid
>= 1 && epid
<= 31);
1225 slot
= &xhci
->slots
[slotid
-1];
1227 if (!slot
->eps
[epid
-1]) {
1228 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
1232 xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0);
1234 epctx
= slot
->eps
[epid
-1];
1236 if (epctx
->nr_pstreams
) {
1237 xhci_free_streams(epctx
);
1240 /* only touch guest RAM if we're not resetting the HC */
1241 if (xhci
->dcbaap_low
|| xhci
->dcbaap_high
) {
1242 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_DISABLED
);
1245 timer_free(epctx
->kick_timer
);
1247 slot
->eps
[epid
-1] = NULL
;
1252 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
1256 XHCIEPContext
*epctx
;
1258 trace_usb_xhci_ep_stop(slotid
, epid
);
1259 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1261 if (epid
< 1 || epid
> 31) {
1262 DPRINTF("xhci: bad ep %d\n", epid
);
1263 return CC_TRB_ERROR
;
1266 slot
= &xhci
->slots
[slotid
-1];
1268 if (!slot
->eps
[epid
-1]) {
1269 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1270 return CC_EP_NOT_ENABLED_ERROR
;
1273 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, CC_STOPPED
) > 0) {
1274 DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, "
1275 "data might be lost\n");
1278 epctx
= slot
->eps
[epid
-1];
1280 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1282 if (epctx
->nr_pstreams
) {
1283 xhci_reset_streams(epctx
);
1289 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
1293 XHCIEPContext
*epctx
;
1295 trace_usb_xhci_ep_reset(slotid
, epid
);
1296 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1298 if (epid
< 1 || epid
> 31) {
1299 DPRINTF("xhci: bad ep %d\n", epid
);
1300 return CC_TRB_ERROR
;
1303 slot
= &xhci
->slots
[slotid
-1];
1305 if (!slot
->eps
[epid
-1]) {
1306 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1307 return CC_EP_NOT_ENABLED_ERROR
;
1310 epctx
= slot
->eps
[epid
-1];
1312 if (epctx
->state
!= EP_HALTED
) {
1313 DPRINTF("xhci: reset EP while EP %d not halted (%d)\n",
1314 epid
, epctx
->state
);
1315 return CC_CONTEXT_STATE_ERROR
;
1318 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0) > 0) {
1319 DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, "
1320 "data might be lost\n");
1323 if (!xhci
->slots
[slotid
-1].uport
||
1324 !xhci
->slots
[slotid
-1].uport
->dev
||
1325 !xhci
->slots
[slotid
-1].uport
->dev
->attached
) {
1326 return CC_USB_TRANSACTION_ERROR
;
1329 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1331 if (epctx
->nr_pstreams
) {
1332 xhci_reset_streams(epctx
);
1338 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1339 unsigned int epid
, unsigned int streamid
,
1343 XHCIEPContext
*epctx
;
1344 XHCIStreamContext
*sctx
;
1347 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1349 if (epid
< 1 || epid
> 31) {
1350 DPRINTF("xhci: bad ep %d\n", epid
);
1351 return CC_TRB_ERROR
;
1354 trace_usb_xhci_ep_set_dequeue(slotid
, epid
, streamid
, pdequeue
);
1355 dequeue
= xhci_mask64(pdequeue
);
1357 slot
= &xhci
->slots
[slotid
-1];
1359 if (!slot
->eps
[epid
-1]) {
1360 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1361 return CC_EP_NOT_ENABLED_ERROR
;
1364 epctx
= slot
->eps
[epid
-1];
1366 if (epctx
->state
!= EP_STOPPED
) {
1367 DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1368 return CC_CONTEXT_STATE_ERROR
;
1371 if (epctx
->nr_pstreams
) {
1373 sctx
= xhci_find_stream(epctx
, streamid
, &err
);
1377 xhci_ring_init(xhci
, &sctx
->ring
, dequeue
& ~0xf);
1378 sctx
->ring
.ccs
= dequeue
& 1;
1381 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1382 epctx
->ring
.ccs
= dequeue
& 1;
1385 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_STOPPED
);
1390 static int xhci_xfer_create_sgl(XHCITransfer
*xfer
, int in_xfer
)
1392 XHCIState
*xhci
= xfer
->epctx
->xhci
;
1395 xfer
->int_req
= false;
1396 qemu_sglist_init(&xfer
->sgl
, DEVICE(xhci
), xfer
->trb_count
, xhci
->as
);
1397 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1398 XHCITRB
*trb
= &xfer
->trbs
[i
];
1400 unsigned int chunk
= 0;
1402 if (trb
->control
& TRB_TR_IOC
) {
1403 xfer
->int_req
= true;
1406 switch (TRB_TYPE(*trb
)) {
1408 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1409 DPRINTF("xhci: data direction mismatch for TR_DATA\n");
1415 addr
= xhci_mask64(trb
->parameter
);
1416 chunk
= trb
->status
& 0x1ffff;
1417 if (trb
->control
& TRB_TR_IDT
) {
1418 if (chunk
> 8 || in_xfer
) {
1419 DPRINTF("xhci: invalid immediate data TRB\n");
1422 qemu_sglist_add(&xfer
->sgl
, trb
->addr
, chunk
);
1424 qemu_sglist_add(&xfer
->sgl
, addr
, chunk
);
1433 qemu_sglist_destroy(&xfer
->sgl
);
1438 static void xhci_xfer_unmap(XHCITransfer
*xfer
)
1440 usb_packet_unmap(&xfer
->packet
, &xfer
->sgl
);
1441 qemu_sglist_destroy(&xfer
->sgl
);
1444 static void xhci_xfer_report(XHCITransfer
*xfer
)
1450 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1451 XHCIState
*xhci
= xfer
->epctx
->xhci
;
1454 left
= xfer
->packet
.actual_length
;
1456 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1457 XHCITRB
*trb
= &xfer
->trbs
[i
];
1458 unsigned int chunk
= 0;
1460 switch (TRB_TYPE(*trb
)) {
1462 chunk
= trb
->status
& 0x1ffff;
1470 chunk
= trb
->status
& 0x1ffff;
1473 if (xfer
->status
== CC_SUCCESS
) {
1486 if (!reported
&& ((trb
->control
& TRB_TR_IOC
) ||
1487 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)) ||
1488 (xfer
->status
!= CC_SUCCESS
&& left
== 0))) {
1489 event
.slotid
= xfer
->epctx
->slotid
;
1490 event
.epid
= xfer
->epctx
->epid
;
1491 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1493 event
.ptr
= trb
->addr
;
1494 if (xfer
->status
== CC_SUCCESS
) {
1495 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1497 event
.ccode
= xfer
->status
;
1499 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1500 event
.ptr
= trb
->parameter
;
1501 event
.flags
|= TRB_EV_ED
;
1502 event
.length
= edtla
& 0xffffff;
1503 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1506 xhci_event(xhci
, &event
, TRB_INTR(*trb
));
1508 if (xfer
->status
!= CC_SUCCESS
) {
1513 switch (TRB_TYPE(*trb
)) {
1523 static void xhci_stall_ep(XHCITransfer
*xfer
)
1525 XHCIEPContext
*epctx
= xfer
->epctx
;
1526 XHCIState
*xhci
= epctx
->xhci
;
1528 XHCIStreamContext
*sctx
;
1530 if (epctx
->type
== ET_ISO_IN
|| epctx
->type
== ET_ISO_OUT
) {
1531 /* never halt isoch endpoints, 4.10.2 */
1535 if (epctx
->nr_pstreams
) {
1536 sctx
= xhci_find_stream(epctx
, xfer
->streamid
, &err
);
1540 sctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1541 sctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1542 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_HALTED
);
1544 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1545 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1546 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_HALTED
);
1550 static int xhci_setup_packet(XHCITransfer
*xfer
)
1555 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1557 if (xfer
->packet
.ep
) {
1558 ep
= xfer
->packet
.ep
;
1560 ep
= xhci_epid_to_usbep(xfer
->epctx
);
1562 DPRINTF("xhci: slot %d has no device\n",
1563 xfer
->epctx
->slotid
);
1568 xhci_xfer_create_sgl(xfer
, dir
== USB_TOKEN_IN
); /* Also sets int_req */
1569 usb_packet_setup(&xfer
->packet
, dir
, ep
, xfer
->streamid
,
1570 xfer
->trbs
[0].addr
, false, xfer
->int_req
);
1571 if (usb_packet_map(&xfer
->packet
, &xfer
->sgl
)) {
1572 qemu_sglist_destroy(&xfer
->sgl
);
1575 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1576 xfer
->packet
.pid
, ep
->dev
->addr
, ep
->nr
);
1580 static int xhci_try_complete_packet(XHCITransfer
*xfer
)
1582 if (xfer
->packet
.status
== USB_RET_ASYNC
) {
1583 trace_usb_xhci_xfer_async(xfer
);
1584 xfer
->running_async
= 1;
1585 xfer
->running_retry
= 0;
1588 } else if (xfer
->packet
.status
== USB_RET_NAK
) {
1589 trace_usb_xhci_xfer_nak(xfer
);
1590 xfer
->running_async
= 0;
1591 xfer
->running_retry
= 1;
1595 xfer
->running_async
= 0;
1596 xfer
->running_retry
= 0;
1598 xhci_xfer_unmap(xfer
);
1601 if (xfer
->packet
.status
== USB_RET_SUCCESS
) {
1602 trace_usb_xhci_xfer_success(xfer
, xfer
->packet
.actual_length
);
1603 xfer
->status
= CC_SUCCESS
;
1604 xhci_xfer_report(xfer
);
1609 trace_usb_xhci_xfer_error(xfer
, xfer
->packet
.status
);
1610 switch (xfer
->packet
.status
) {
1612 case USB_RET_IOERROR
:
1613 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1614 xhci_xfer_report(xfer
);
1615 xhci_stall_ep(xfer
);
1618 xfer
->status
= CC_STALL_ERROR
;
1619 xhci_xfer_report(xfer
);
1620 xhci_stall_ep(xfer
);
1622 case USB_RET_BABBLE
:
1623 xfer
->status
= CC_BABBLE_DETECTED
;
1624 xhci_xfer_report(xfer
);
1625 xhci_stall_ep(xfer
);
1628 DPRINTF("%s: FIXME: status = %d\n", __func__
,
1629 xfer
->packet
.status
);
1630 FIXME("unhandled USB_RET_*");
1635 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1637 XHCITRB
*trb_setup
, *trb_status
;
1638 uint8_t bmRequestType
;
1640 trb_setup
= &xfer
->trbs
[0];
1641 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1643 trace_usb_xhci_xfer_start(xfer
, xfer
->epctx
->slotid
,
1644 xfer
->epctx
->epid
, xfer
->streamid
);
1646 /* at most one Event Data TRB allowed after STATUS */
1647 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1651 /* do some sanity checks */
1652 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1653 DPRINTF("xhci: ep0 first TD not SETUP: %d\n",
1654 TRB_TYPE(*trb_setup
));
1657 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1658 DPRINTF("xhci: ep0 last TD not STATUS: %d\n",
1659 TRB_TYPE(*trb_status
));
1662 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1663 DPRINTF("xhci: Setup TRB doesn't have IDT set\n");
1666 if ((trb_setup
->status
& 0x1ffff) != 8) {
1667 DPRINTF("xhci: Setup TRB has bad length (%d)\n",
1668 (trb_setup
->status
& 0x1ffff));
1672 bmRequestType
= trb_setup
->parameter
;
1674 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1675 xfer
->iso_xfer
= false;
1676 xfer
->timed_xfer
= false;
1678 if (xhci_setup_packet(xfer
) < 0) {
1681 xfer
->packet
.parameter
= trb_setup
->parameter
;
1683 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1684 xhci_try_complete_packet(xfer
);
1688 static void xhci_calc_intr_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1689 XHCIEPContext
*epctx
, uint64_t mfindex
)
1691 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1692 ~(epctx
->interval
-1));
1693 uint64_t kick
= epctx
->mfindex_last
+ epctx
->interval
;
1695 assert(epctx
->interval
!= 0);
1696 xfer
->mfindex_kick
= MAX(asap
, kick
);
1699 static void xhci_calc_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1700 XHCIEPContext
*epctx
, uint64_t mfindex
)
1702 if (xfer
->trbs
[0].control
& TRB_TR_SIA
) {
1703 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1704 ~(epctx
->interval
-1));
1705 if (asap
>= epctx
->mfindex_last
&&
1706 asap
<= epctx
->mfindex_last
+ epctx
->interval
* 4) {
1707 xfer
->mfindex_kick
= epctx
->mfindex_last
+ epctx
->interval
;
1709 xfer
->mfindex_kick
= asap
;
1712 xfer
->mfindex_kick
= ((xfer
->trbs
[0].control
>> TRB_TR_FRAMEID_SHIFT
)
1713 & TRB_TR_FRAMEID_MASK
) << 3;
1714 xfer
->mfindex_kick
|= mfindex
& ~0x3fff;
1715 if (xfer
->mfindex_kick
+ 0x100 < mfindex
) {
1716 xfer
->mfindex_kick
+= 0x4000;
1721 static void xhci_check_intr_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1722 XHCIEPContext
*epctx
, uint64_t mfindex
)
1724 if (xfer
->mfindex_kick
> mfindex
) {
1725 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
1726 (xfer
->mfindex_kick
- mfindex
) * 125000);
1727 xfer
->running_retry
= 1;
1729 epctx
->mfindex_last
= xfer
->mfindex_kick
;
1730 timer_del(epctx
->kick_timer
);
1731 xfer
->running_retry
= 0;
1736 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1740 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", epctx
->slotid
, epctx
->epid
);
1742 xfer
->in_xfer
= epctx
->type
>>2;
1744 switch(epctx
->type
) {
1748 xfer
->iso_xfer
= false;
1749 xfer
->timed_xfer
= true;
1750 mfindex
= xhci_mfindex_get(xhci
);
1751 xhci_calc_intr_kick(xhci
, xfer
, epctx
, mfindex
);
1752 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1753 if (xfer
->running_retry
) {
1760 xfer
->iso_xfer
= false;
1761 xfer
->timed_xfer
= false;
1766 xfer
->iso_xfer
= true;
1767 xfer
->timed_xfer
= true;
1768 mfindex
= xhci_mfindex_get(xhci
);
1769 xhci_calc_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1770 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1771 if (xfer
->running_retry
) {
1776 trace_usb_xhci_unimplemented("endpoint type", epctx
->type
);
1780 if (xhci_setup_packet(xfer
) < 0) {
1783 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1784 xhci_try_complete_packet(xfer
);
1788 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1790 trace_usb_xhci_xfer_start(xfer
, xfer
->epctx
->slotid
,
1791 xfer
->epctx
->epid
, xfer
->streamid
);
1792 return xhci_submit(xhci
, xfer
, epctx
);
1795 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
1796 unsigned int epid
, unsigned int streamid
)
1798 XHCIEPContext
*epctx
;
1800 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1801 assert(epid
>= 1 && epid
<= 31);
1803 if (!xhci
->slots
[slotid
-1].enabled
) {
1804 DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1807 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1809 DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1814 if (epctx
->kick_active
) {
1817 xhci_kick_epctx(epctx
, streamid
);
1820 static bool xhci_slot_ok(XHCIState
*xhci
, int slotid
)
1822 return (xhci
->slots
[slotid
- 1].uport
&&
1823 xhci
->slots
[slotid
- 1].uport
->dev
&&
1824 xhci
->slots
[slotid
- 1].uport
->dev
->attached
);
1827 static void xhci_kick_epctx(XHCIEPContext
*epctx
, unsigned int streamid
)
1829 XHCIState
*xhci
= epctx
->xhci
;
1830 XHCIStreamContext
*stctx
= NULL
;
1833 USBEndpoint
*ep
= NULL
;
1835 unsigned int count
= 0;
1839 trace_usb_xhci_ep_kick(epctx
->slotid
, epctx
->epid
, streamid
);
1840 assert(!epctx
->kick_active
);
1842 /* If the device has been detached, but the guest has not noticed this
1843 yet the 2 above checks will succeed, but we must NOT continue */
1844 if (!xhci_slot_ok(xhci
, epctx
->slotid
)) {
1849 XHCITransfer
*xfer
= epctx
->retry
;
1851 trace_usb_xhci_xfer_retry(xfer
);
1852 assert(xfer
->running_retry
);
1853 if (xfer
->timed_xfer
) {
1854 /* time to kick the transfer? */
1855 mfindex
= xhci_mfindex_get(xhci
);
1856 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1857 if (xfer
->running_retry
) {
1860 xfer
->timed_xfer
= 0;
1861 xfer
->running_retry
= 1;
1863 if (xfer
->iso_xfer
) {
1864 /* retry iso transfer */
1865 if (xhci_setup_packet(xfer
) < 0) {
1868 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1869 assert(xfer
->packet
.status
!= USB_RET_NAK
);
1870 xhci_try_complete_packet(xfer
);
1872 /* retry nak'ed transfer */
1873 if (xhci_setup_packet(xfer
) < 0) {
1876 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1877 if (xfer
->packet
.status
== USB_RET_NAK
) {
1878 xhci_xfer_unmap(xfer
);
1881 xhci_try_complete_packet(xfer
);
1883 assert(!xfer
->running_retry
);
1884 if (xfer
->complete
) {
1885 /* update ring dequeue ptr */
1886 xhci_set_ep_state(xhci
, epctx
, stctx
, epctx
->state
);
1887 xhci_ep_free_xfer(epctx
->retry
);
1889 epctx
->retry
= NULL
;
1892 if (epctx
->state
== EP_HALTED
) {
1893 DPRINTF("xhci: ep halted, not running schedule\n");
1898 if (epctx
->nr_pstreams
) {
1900 stctx
= xhci_find_stream(epctx
, streamid
, &err
);
1901 if (stctx
== NULL
) {
1904 ring
= &stctx
->ring
;
1905 xhci_set_ep_state(xhci
, epctx
, stctx
, EP_RUNNING
);
1907 ring
= &epctx
->ring
;
1909 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_RUNNING
);
1911 if (!ring
->dequeue
) {
1915 epctx
->kick_active
++;
1917 length
= xhci_ring_chain_length(xhci
, ring
);
1919 if (epctx
->type
== ET_ISO_OUT
|| epctx
->type
== ET_ISO_IN
) {
1921 XHCIEvent ev
= { ER_TRANSFER
};
1922 ev
.ccode
= epctx
->type
== ET_ISO_IN
?
1923 CC_RING_OVERRUN
: CC_RING_UNDERRUN
;
1924 ev
.slotid
= epctx
->slotid
;
1925 ev
.epid
= epctx
->epid
;
1926 ev
.ptr
= epctx
->ring
.dequeue
;
1927 xhci_event(xhci
, &ev
, xhci
->slots
[epctx
->slotid
-1].intr
);
1931 xfer
= xhci_ep_alloc_xfer(epctx
, length
);
1936 for (i
= 0; i
< length
; i
++) {
1938 type
= xhci_ring_fetch(xhci
, ring
, &xfer
->trbs
[i
], NULL
);
1941 xhci_ep_free_xfer(xfer
);
1942 epctx
->kick_active
--;
1946 xfer
->streamid
= streamid
;
1948 if (epctx
->epid
== 1) {
1949 xhci_fire_ctl_transfer(xhci
, xfer
);
1951 xhci_fire_transfer(xhci
, xfer
, epctx
);
1953 if (!xhci_slot_ok(xhci
, epctx
->slotid
)) {
1954 /* surprise removal -> stop processing */
1957 if (xfer
->complete
) {
1958 /* update ring dequeue ptr */
1959 xhci_set_ep_state(xhci
, epctx
, stctx
, epctx
->state
);
1960 xhci_ep_free_xfer(xfer
);
1964 if (epctx
->state
== EP_HALTED
) {
1967 if (xfer
!= NULL
&& xfer
->running_retry
) {
1968 DPRINTF("xhci: xfer nacked, stopping schedule\n");
1969 epctx
->retry
= xfer
;
1970 xhci_xfer_unmap(xfer
);
1973 if (count
++ > TRANSFER_LIMIT
) {
1974 trace_usb_xhci_enforced_limit("transfers");
1978 epctx
->kick_active
--;
1980 ep
= xhci_epid_to_usbep(epctx
);
1982 usb_device_flush_ep_queue(ep
->dev
, ep
);
1986 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
1988 trace_usb_xhci_slot_enable(slotid
);
1989 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1990 xhci
->slots
[slotid
-1].enabled
= 1;
1991 xhci
->slots
[slotid
-1].uport
= NULL
;
1992 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
1997 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
2001 trace_usb_xhci_slot_disable(slotid
);
2002 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2004 for (i
= 1; i
<= 31; i
++) {
2005 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2006 xhci_disable_ep(xhci
, slotid
, i
);
2010 xhci
->slots
[slotid
-1].enabled
= 0;
2011 xhci
->slots
[slotid
-1].addressed
= 0;
2012 xhci
->slots
[slotid
-1].uport
= NULL
;
2013 xhci
->slots
[slotid
-1].intr
= 0;
2017 static USBPort
*xhci_lookup_uport(XHCIState
*xhci
, uint32_t *slot_ctx
)
2023 port
= (slot_ctx
[1]>>16) & 0xFF;
2024 if (port
< 1 || port
> xhci
->numports
) {
2027 port
= xhci
->ports
[port
-1].uport
->index
+1;
2028 pos
= snprintf(path
, sizeof(path
), "%d", port
);
2029 for (i
= 0; i
< 5; i
++) {
2030 port
= (slot_ctx
[0] >> 4*i
) & 0x0f;
2034 pos
+= snprintf(path
+ pos
, sizeof(path
) - pos
, ".%d", port
);
2037 QTAILQ_FOREACH(uport
, &xhci
->bus
.used
, next
) {
2038 if (strcmp(uport
->path
, path
) == 0) {
2045 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
2046 uint64_t pictx
, bool bsr
)
2051 dma_addr_t ictx
, octx
, dcbaap
;
2053 uint32_t ictl_ctx
[2];
2054 uint32_t slot_ctx
[4];
2055 uint32_t ep0_ctx
[5];
2059 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2061 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
2062 poctx
= ldq_le_dma(xhci
->as
, dcbaap
+ 8 * slotid
);
2063 ictx
= xhci_mask64(pictx
);
2064 octx
= xhci_mask64(poctx
);
2066 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2067 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2069 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2071 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
2072 DPRINTF("xhci: invalid input context control %08x %08x\n",
2073 ictl_ctx
[0], ictl_ctx
[1]);
2074 return CC_TRB_ERROR
;
2077 xhci_dma_read_u32s(xhci
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
2078 xhci_dma_read_u32s(xhci
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
2080 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2081 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2083 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2084 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2086 uport
= xhci_lookup_uport(xhci
, slot_ctx
);
2087 if (uport
== NULL
) {
2088 DPRINTF("xhci: port not found\n");
2089 return CC_TRB_ERROR
;
2091 trace_usb_xhci_slot_address(slotid
, uport
->path
);
2094 if (!dev
|| !dev
->attached
) {
2095 DPRINTF("xhci: port %s not connected\n", uport
->path
);
2096 return CC_USB_TRANSACTION_ERROR
;
2099 for (i
= 0; i
< xhci
->numslots
; i
++) {
2100 if (i
== slotid
-1) {
2103 if (xhci
->slots
[i
].uport
== uport
) {
2104 DPRINTF("xhci: port %s already assigned to slot %d\n",
2106 return CC_TRB_ERROR
;
2110 slot
= &xhci
->slots
[slotid
-1];
2111 slot
->uport
= uport
;
2113 slot
->intr
= get_field(slot_ctx
[2], TRB_INTR
);
2115 /* Make sure device is in USB_STATE_DEFAULT state */
2116 usb_device_reset(dev
);
2118 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2123 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slotid
;
2124 memset(&p
, 0, sizeof(p
));
2125 usb_packet_addbuf(&p
, buf
, sizeof(buf
));
2126 usb_packet_setup(&p
, USB_TOKEN_OUT
,
2127 usb_ep_get(dev
, USB_TOKEN_OUT
, 0), 0,
2129 usb_device_handle_control(dev
, &p
,
2130 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
2131 slotid
, 0, 0, NULL
);
2132 assert(p
.status
!= USB_RET_ASYNC
);
2133 usb_packet_cleanup(&p
);
2136 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
2138 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2139 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2140 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2141 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2143 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2144 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2146 xhci
->slots
[slotid
-1].addressed
= 1;
2151 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
2152 uint64_t pictx
, bool dc
)
2154 dma_addr_t ictx
, octx
;
2155 uint32_t ictl_ctx
[2];
2156 uint32_t slot_ctx
[4];
2157 uint32_t islot_ctx
[4];
2162 trace_usb_xhci_slot_configure(slotid
);
2163 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2165 ictx
= xhci_mask64(pictx
);
2166 octx
= xhci
->slots
[slotid
-1].ctx
;
2168 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2169 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2172 for (i
= 2; i
<= 31; i
++) {
2173 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2174 xhci_disable_ep(xhci
, slotid
, i
);
2178 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2179 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2180 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
2181 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2182 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2183 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2188 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2190 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
2191 DPRINTF("xhci: invalid input context control %08x %08x\n",
2192 ictl_ctx
[0], ictl_ctx
[1]);
2193 return CC_TRB_ERROR
;
2196 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2197 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2199 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
2200 DPRINTF("xhci: invalid slot state %08x\n", slot_ctx
[3]);
2201 return CC_CONTEXT_STATE_ERROR
;
2204 xhci_free_device_streams(xhci
, slotid
, ictl_ctx
[0] | ictl_ctx
[1]);
2206 for (i
= 2; i
<= 31; i
++) {
2207 if (ictl_ctx
[0] & (1<<i
)) {
2208 xhci_disable_ep(xhci
, slotid
, i
);
2210 if (ictl_ctx
[1] & (1<<i
)) {
2211 xhci_dma_read_u32s(xhci
, ictx
+32+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2212 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
2213 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2214 ep_ctx
[3], ep_ctx
[4]);
2215 xhci_disable_ep(xhci
, slotid
, i
);
2216 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
2217 if (res
!= CC_SUCCESS
) {
2220 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
2221 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2222 ep_ctx
[3], ep_ctx
[4]);
2223 xhci_dma_write_u32s(xhci
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2227 res
= xhci_alloc_device_streams(xhci
, slotid
, ictl_ctx
[1]);
2228 if (res
!= CC_SUCCESS
) {
2229 for (i
= 2; i
<= 31; i
++) {
2230 if (ictl_ctx
[1] & (1u << i
)) {
2231 xhci_disable_ep(xhci
, slotid
, i
);
2237 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2238 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
2239 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
2240 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
2241 SLOT_CONTEXT_ENTRIES_SHIFT
);
2242 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2243 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2245 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2251 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
2254 dma_addr_t ictx
, octx
;
2255 uint32_t ictl_ctx
[2];
2256 uint32_t iep0_ctx
[5];
2257 uint32_t ep0_ctx
[5];
2258 uint32_t islot_ctx
[4];
2259 uint32_t slot_ctx
[4];
2261 trace_usb_xhci_slot_evaluate(slotid
);
2262 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2264 ictx
= xhci_mask64(pictx
);
2265 octx
= xhci
->slots
[slotid
-1].ctx
;
2267 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2268 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2270 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2272 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
2273 DPRINTF("xhci: invalid input context control %08x %08x\n",
2274 ictl_ctx
[0], ictl_ctx
[1]);
2275 return CC_TRB_ERROR
;
2278 if (ictl_ctx
[1] & 0x1) {
2279 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2281 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2282 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
2284 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2286 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
2287 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
2288 /* update interrupter target field */
2289 xhci
->slots
[slotid
-1].intr
= get_field(islot_ctx
[2], TRB_INTR
);
2290 set_field(&slot_ctx
[2], xhci
->slots
[slotid
-1].intr
, TRB_INTR
);
2292 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2293 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2295 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2298 if (ictl_ctx
[1] & 0x2) {
2299 xhci_dma_read_u32s(xhci
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
2301 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2302 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
2303 iep0_ctx
[3], iep0_ctx
[4]);
2305 xhci_dma_read_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2307 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
2308 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
2310 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2311 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2313 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2319 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
2321 uint32_t slot_ctx
[4];
2325 trace_usb_xhci_slot_reset(slotid
);
2326 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2328 octx
= xhci
->slots
[slotid
-1].ctx
;
2330 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2332 for (i
= 2; i
<= 31; i
++) {
2333 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2334 xhci_disable_ep(xhci
, slotid
, i
);
2338 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2339 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2340 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2341 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2342 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2343 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2348 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
2350 unsigned int slotid
;
2351 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
2352 if (slotid
< 1 || slotid
> xhci
->numslots
) {
2353 DPRINTF("xhci: bad slot id %d\n", slotid
);
2354 event
->ccode
= CC_TRB_ERROR
;
2356 } else if (!xhci
->slots
[slotid
-1].enabled
) {
2357 DPRINTF("xhci: slot id %d not enabled\n", slotid
);
2358 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
2364 /* cleanup slot state on usb device detach */
2365 static void xhci_detach_slot(XHCIState
*xhci
, USBPort
*uport
)
2369 for (slot
= 0; slot
< xhci
->numslots
; slot
++) {
2370 if (xhci
->slots
[slot
].uport
== uport
) {
2374 if (slot
== xhci
->numslots
) {
2378 for (ep
= 0; ep
< 31; ep
++) {
2379 if (xhci
->slots
[slot
].eps
[ep
]) {
2380 xhci_ep_nuke_xfers(xhci
, slot
+ 1, ep
+ 1, 0);
2383 xhci
->slots
[slot
].uport
= NULL
;
2386 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
2389 uint8_t bw_ctx
[xhci
->numports
+1];
2391 DPRINTF("xhci_get_port_bandwidth()\n");
2393 ctx
= xhci_mask64(pctx
);
2395 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
2397 /* TODO: actually implement real values here */
2399 memset(&bw_ctx
[1], 80, xhci
->numports
); /* 80% */
2400 dma_memory_write(xhci
->as
, ctx
, bw_ctx
, sizeof(bw_ctx
));
2405 static uint32_t rotl(uint32_t v
, unsigned count
)
2408 return (v
<< count
) | (v
>> (32 - count
));
2412 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2415 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2416 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2417 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2421 static void xhci_process_commands(XHCIState
*xhci
)
2425 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2427 unsigned int i
, slotid
= 0, count
= 0;
2429 DPRINTF("xhci_process_commands()\n");
2430 if (!xhci_running(xhci
)) {
2431 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2435 xhci
->crcr_low
|= CRCR_CRR
;
2437 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2440 case CR_ENABLE_SLOT
:
2441 for (i
= 0; i
< xhci
->numslots
; i
++) {
2442 if (!xhci
->slots
[i
].enabled
) {
2446 if (i
>= xhci
->numslots
) {
2447 DPRINTF("xhci: no device slots available\n");
2448 event
.ccode
= CC_NO_SLOTS_ERROR
;
2451 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2454 case CR_DISABLE_SLOT
:
2455 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2457 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2460 case CR_ADDRESS_DEVICE
:
2461 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2463 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2464 trb
.control
& TRB_CR_BSR
);
2467 case CR_CONFIGURE_ENDPOINT
:
2468 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2470 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2471 trb
.control
& TRB_CR_DC
);
2474 case CR_EVALUATE_CONTEXT
:
2475 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2477 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2480 case CR_STOP_ENDPOINT
:
2481 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2483 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2485 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2488 case CR_RESET_ENDPOINT
:
2489 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2491 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2493 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2496 case CR_SET_TR_DEQUEUE
:
2497 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2499 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2501 unsigned int streamid
= (trb
.status
>> 16) & 0xffff;
2502 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
,
2507 case CR_RESET_DEVICE
:
2508 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2510 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2513 case CR_GET_PORT_BANDWIDTH
:
2514 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2517 event
.ccode
= CC_SUCCESS
;
2519 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2520 if (xhci
->nec_quirks
) {
2521 event
.type
= 48; /* NEC reply */
2522 event
.length
= 0x3025;
2524 event
.ccode
= CC_TRB_ERROR
;
2527 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2528 if (xhci
->nec_quirks
) {
2529 uint32_t chi
= trb
.parameter
>> 32;
2530 uint32_t clo
= trb
.parameter
;
2531 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2532 event
.length
= val
& 0xFFFF;
2533 event
.epid
= val
>> 16;
2535 event
.type
= 48; /* NEC reply */
2537 event
.ccode
= CC_TRB_ERROR
;
2541 trace_usb_xhci_unimplemented("command", type
);
2542 event
.ccode
= CC_TRB_ERROR
;
2545 event
.slotid
= slotid
;
2546 xhci_event(xhci
, &event
, 0);
2548 if (count
++ > COMMAND_LIMIT
) {
2549 trace_usb_xhci_enforced_limit("commands");
2555 static bool xhci_port_have_device(XHCIPort
*port
)
2557 if (!port
->uport
->dev
|| !port
->uport
->dev
->attached
) {
2558 return false; /* no device present */
2560 if (!((1 << port
->uport
->dev
->speed
) & port
->speedmask
)) {
2561 return false; /* speed mismatch */
2566 static void xhci_port_notify(XHCIPort
*port
, uint32_t bits
)
2568 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
,
2569 port
->portnr
<< 24 };
2571 if ((port
->portsc
& bits
) == bits
) {
2574 trace_usb_xhci_port_notify(port
->portnr
, bits
);
2575 port
->portsc
|= bits
;
2576 if (!xhci_running(port
->xhci
)) {
2579 xhci_event(port
->xhci
, &ev
, 0);
2582 static void xhci_port_update(XHCIPort
*port
, int is_detach
)
2584 uint32_t pls
= PLS_RX_DETECT
;
2587 port
->portsc
= PORTSC_PP
;
2588 if (!is_detach
&& xhci_port_have_device(port
)) {
2589 port
->portsc
|= PORTSC_CCS
;
2590 switch (port
->uport
->dev
->speed
) {
2592 port
->portsc
|= PORTSC_SPEED_LOW
;
2595 case USB_SPEED_FULL
:
2596 port
->portsc
|= PORTSC_SPEED_FULL
;
2599 case USB_SPEED_HIGH
:
2600 port
->portsc
|= PORTSC_SPEED_HIGH
;
2603 case USB_SPEED_SUPER
:
2604 port
->portsc
|= PORTSC_SPEED_SUPER
;
2605 port
->portsc
|= PORTSC_PED
;
2610 set_field(&port
->portsc
, pls
, PORTSC_PLS
);
2611 trace_usb_xhci_port_link(port
->portnr
, pls
);
2612 xhci_port_notify(port
, PORTSC_CSC
);
2615 static void xhci_port_reset(XHCIPort
*port
, bool warm_reset
)
2617 trace_usb_xhci_port_reset(port
->portnr
, warm_reset
);
2619 if (!xhci_port_have_device(port
)) {
2623 usb_device_reset(port
->uport
->dev
);
2625 switch (port
->uport
->dev
->speed
) {
2626 case USB_SPEED_SUPER
:
2628 port
->portsc
|= PORTSC_WRC
;
2632 case USB_SPEED_FULL
:
2633 case USB_SPEED_HIGH
:
2634 set_field(&port
->portsc
, PLS_U0
, PORTSC_PLS
);
2635 trace_usb_xhci_port_link(port
->portnr
, PLS_U0
);
2636 port
->portsc
|= PORTSC_PED
;
2640 port
->portsc
&= ~PORTSC_PR
;
2641 xhci_port_notify(port
, PORTSC_PRC
);
2644 static void xhci_reset(DeviceState
*dev
)
2646 XHCIState
*xhci
= XHCI(dev
);
2649 trace_usb_xhci_reset();
2650 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2651 DPRINTF("xhci: reset while running!\n");
2655 xhci
->usbsts
= USBSTS_HCH
;
2658 xhci
->crcr_high
= 0;
2659 xhci
->dcbaap_low
= 0;
2660 xhci
->dcbaap_high
= 0;
2663 for (i
= 0; i
< xhci
->numslots
; i
++) {
2664 xhci_disable_slot(xhci
, i
+1);
2667 for (i
= 0; i
< xhci
->numports
; i
++) {
2668 xhci_port_update(xhci
->ports
+ i
, 0);
2671 for (i
= 0; i
< xhci
->numintrs
; i
++) {
2672 xhci
->intr
[i
].iman
= 0;
2673 xhci
->intr
[i
].imod
= 0;
2674 xhci
->intr
[i
].erstsz
= 0;
2675 xhci
->intr
[i
].erstba_low
= 0;
2676 xhci
->intr
[i
].erstba_high
= 0;
2677 xhci
->intr
[i
].erdp_low
= 0;
2678 xhci
->intr
[i
].erdp_high
= 0;
2680 xhci
->intr
[i
].er_ep_idx
= 0;
2681 xhci
->intr
[i
].er_pcs
= 1;
2682 xhci
->intr
[i
].ev_buffer_put
= 0;
2683 xhci
->intr
[i
].ev_buffer_get
= 0;
2686 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2687 xhci_mfwrap_update(xhci
);
2690 static uint64_t xhci_cap_read(void *ptr
, hwaddr reg
, unsigned size
)
2692 XHCIState
*xhci
= ptr
;
2696 case 0x00: /* HCIVERSION, CAPLENGTH */
2697 ret
= 0x01000000 | LEN_CAP
;
2699 case 0x04: /* HCSPARAMS 1 */
2700 ret
= ((xhci
->numports_2
+xhci
->numports_3
)<<24)
2701 | (xhci
->numintrs
<<8) | xhci
->numslots
;
2703 case 0x08: /* HCSPARAMS 2 */
2706 case 0x0c: /* HCSPARAMS 3 */
2709 case 0x10: /* HCCPARAMS */
2710 if (sizeof(dma_addr_t
) == 4) {
2711 ret
= 0x00080000 | (xhci
->max_pstreams_mask
<< 12);
2713 ret
= 0x00080001 | (xhci
->max_pstreams_mask
<< 12);
2716 case 0x14: /* DBOFF */
2719 case 0x18: /* RTSOFF */
2723 /* extended capabilities */
2724 case 0x20: /* Supported Protocol:00 */
2725 ret
= 0x02000402; /* USB 2.0 */
2727 case 0x24: /* Supported Protocol:04 */
2728 ret
= 0x20425355; /* "USB " */
2730 case 0x28: /* Supported Protocol:08 */
2731 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2732 ret
= (xhci
->numports_2
<<8) | (xhci
->numports_3
+1);
2734 ret
= (xhci
->numports_2
<<8) | 1;
2737 case 0x2c: /* Supported Protocol:0c */
2738 ret
= 0x00000000; /* reserved */
2740 case 0x30: /* Supported Protocol:00 */
2741 ret
= 0x03000002; /* USB 3.0 */
2743 case 0x34: /* Supported Protocol:04 */
2744 ret
= 0x20425355; /* "USB " */
2746 case 0x38: /* Supported Protocol:08 */
2747 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2748 ret
= (xhci
->numports_3
<<8) | 1;
2750 ret
= (xhci
->numports_3
<<8) | (xhci
->numports_2
+1);
2753 case 0x3c: /* Supported Protocol:0c */
2754 ret
= 0x00000000; /* reserved */
2757 trace_usb_xhci_unimplemented("cap read", reg
);
2761 trace_usb_xhci_cap_read(reg
, ret
);
2765 static uint64_t xhci_port_read(void *ptr
, hwaddr reg
, unsigned size
)
2767 XHCIPort
*port
= ptr
;
2771 case 0x00: /* PORTSC */
2774 case 0x04: /* PORTPMSC */
2775 case 0x08: /* PORTLI */
2778 case 0x0c: /* reserved */
2780 trace_usb_xhci_unimplemented("port read", reg
);
2784 trace_usb_xhci_port_read(port
->portnr
, reg
, ret
);
2788 static void xhci_port_write(void *ptr
, hwaddr reg
,
2789 uint64_t val
, unsigned size
)
2791 XHCIPort
*port
= ptr
;
2792 uint32_t portsc
, notify
;
2794 trace_usb_xhci_port_write(port
->portnr
, reg
, val
);
2797 case 0x00: /* PORTSC */
2798 /* write-1-to-start bits */
2799 if (val
& PORTSC_WPR
) {
2800 xhci_port_reset(port
, true);
2803 if (val
& PORTSC_PR
) {
2804 xhci_port_reset(port
, false);
2808 portsc
= port
->portsc
;
2810 /* write-1-to-clear bits*/
2811 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2812 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2813 if (val
& PORTSC_LWS
) {
2814 /* overwrite PLS only when LWS=1 */
2815 uint32_t old_pls
= get_field(port
->portsc
, PORTSC_PLS
);
2816 uint32_t new_pls
= get_field(val
, PORTSC_PLS
);
2819 if (old_pls
!= PLS_U0
) {
2820 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2821 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2822 notify
= PORTSC_PLC
;
2826 if (old_pls
< PLS_U3
) {
2827 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2828 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2832 /* windows does this for some reason, don't spam stderr */
2835 DPRINTF("%s: ignore pls write (old %d, new %d)\n",
2836 __func__
, old_pls
, new_pls
);
2840 /* read/write bits */
2841 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2842 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2843 port
->portsc
= portsc
;
2845 xhci_port_notify(port
, notify
);
2848 case 0x04: /* PORTPMSC */
2849 case 0x08: /* PORTLI */
2851 trace_usb_xhci_unimplemented("port write", reg
);
2855 static uint64_t xhci_oper_read(void *ptr
, hwaddr reg
, unsigned size
)
2857 XHCIState
*xhci
= ptr
;
2861 case 0x00: /* USBCMD */
2864 case 0x04: /* USBSTS */
2867 case 0x08: /* PAGESIZE */
2870 case 0x14: /* DNCTRL */
2873 case 0x18: /* CRCR low */
2874 ret
= xhci
->crcr_low
& ~0xe;
2876 case 0x1c: /* CRCR high */
2877 ret
= xhci
->crcr_high
;
2879 case 0x30: /* DCBAAP low */
2880 ret
= xhci
->dcbaap_low
;
2882 case 0x34: /* DCBAAP high */
2883 ret
= xhci
->dcbaap_high
;
2885 case 0x38: /* CONFIG */
2889 trace_usb_xhci_unimplemented("oper read", reg
);
2893 trace_usb_xhci_oper_read(reg
, ret
);
2897 static void xhci_oper_write(void *ptr
, hwaddr reg
,
2898 uint64_t val
, unsigned size
)
2900 XHCIState
*xhci
= XHCI(ptr
);
2902 trace_usb_xhci_oper_write(reg
, val
);
2905 case 0x00: /* USBCMD */
2906 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
2908 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
2911 if (val
& USBCMD_CSS
) {
2913 xhci
->usbsts
&= ~USBSTS_SRE
;
2915 if (val
& USBCMD_CRS
) {
2917 xhci
->usbsts
|= USBSTS_SRE
;
2919 xhci
->usbcmd
= val
& 0xc0f;
2920 xhci_mfwrap_update(xhci
);
2921 if (val
& USBCMD_HCRST
) {
2922 xhci_reset(DEVICE(xhci
));
2924 xhci_intr_update(xhci
, 0);
2927 case 0x04: /* USBSTS */
2928 /* these bits are write-1-to-clear */
2929 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
2930 xhci_intr_update(xhci
, 0);
2933 case 0x14: /* DNCTRL */
2934 xhci
->dnctrl
= val
& 0xffff;
2936 case 0x18: /* CRCR low */
2937 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
2939 case 0x1c: /* CRCR high */
2940 xhci
->crcr_high
= val
;
2941 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
2942 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
2943 xhci
->crcr_low
&= ~CRCR_CRR
;
2944 xhci_event(xhci
, &event
, 0);
2945 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
2947 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
2948 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
2950 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
2952 case 0x30: /* DCBAAP low */
2953 xhci
->dcbaap_low
= val
& 0xffffffc0;
2955 case 0x34: /* DCBAAP high */
2956 xhci
->dcbaap_high
= val
;
2958 case 0x38: /* CONFIG */
2959 xhci
->config
= val
& 0xff;
2962 trace_usb_xhci_unimplemented("oper write", reg
);
2966 static uint64_t xhci_runtime_read(void *ptr
, hwaddr reg
,
2969 XHCIState
*xhci
= ptr
;
2974 case 0x00: /* MFINDEX */
2975 ret
= xhci_mfindex_get(xhci
) & 0x3fff;
2978 trace_usb_xhci_unimplemented("runtime read", reg
);
2982 int v
= (reg
- 0x20) / 0x20;
2983 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
2984 switch (reg
& 0x1f) {
2985 case 0x00: /* IMAN */
2988 case 0x04: /* IMOD */
2991 case 0x08: /* ERSTSZ */
2994 case 0x10: /* ERSTBA low */
2995 ret
= intr
->erstba_low
;
2997 case 0x14: /* ERSTBA high */
2998 ret
= intr
->erstba_high
;
3000 case 0x18: /* ERDP low */
3001 ret
= intr
->erdp_low
;
3003 case 0x1c: /* ERDP high */
3004 ret
= intr
->erdp_high
;
3009 trace_usb_xhci_runtime_read(reg
, ret
);
3013 static void xhci_runtime_write(void *ptr
, hwaddr reg
,
3014 uint64_t val
, unsigned size
)
3016 XHCIState
*xhci
= ptr
;
3017 XHCIInterrupter
*intr
;
3020 trace_usb_xhci_runtime_write(reg
, val
);
3023 trace_usb_xhci_unimplemented("runtime write", reg
);
3026 v
= (reg
- 0x20) / 0x20;
3027 intr
= &xhci
->intr
[v
];
3029 switch (reg
& 0x1f) {
3030 case 0x00: /* IMAN */
3031 if (val
& IMAN_IP
) {
3032 intr
->iman
&= ~IMAN_IP
;
3034 intr
->iman
&= ~IMAN_IE
;
3035 intr
->iman
|= val
& IMAN_IE
;
3036 xhci_intr_update(xhci
, v
);
3038 case 0x04: /* IMOD */
3041 case 0x08: /* ERSTSZ */
3042 intr
->erstsz
= val
& 0xffff;
3044 case 0x10: /* ERSTBA low */
3045 if (xhci
->nec_quirks
) {
3046 /* NEC driver bug: it doesn't align this to 64 bytes */
3047 intr
->erstba_low
= val
& 0xfffffff0;
3049 intr
->erstba_low
= val
& 0xffffffc0;
3052 case 0x14: /* ERSTBA high */
3053 intr
->erstba_high
= val
;
3054 xhci_er_reset(xhci
, v
);
3056 case 0x18: /* ERDP low */
3057 if (val
& ERDP_EHB
) {
3058 intr
->erdp_low
&= ~ERDP_EHB
;
3060 intr
->erdp_low
= (val
& ~ERDP_EHB
) | (intr
->erdp_low
& ERDP_EHB
);
3061 if (val
& ERDP_EHB
) {
3062 dma_addr_t erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
3063 unsigned int dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
3064 if (erdp
>= intr
->er_start
&&
3065 erdp
< (intr
->er_start
+ TRB_SIZE
* intr
->er_size
) &&
3066 dp_idx
!= intr
->er_ep_idx
) {
3067 xhci_intr_raise(xhci
, v
);
3071 case 0x1c: /* ERDP high */
3072 intr
->erdp_high
= val
;
3075 trace_usb_xhci_unimplemented("oper write", reg
);
3079 static uint64_t xhci_doorbell_read(void *ptr
, hwaddr reg
,
3082 /* doorbells always read as 0 */
3083 trace_usb_xhci_doorbell_read(reg
, 0);
3087 static void xhci_doorbell_write(void *ptr
, hwaddr reg
,
3088 uint64_t val
, unsigned size
)
3090 XHCIState
*xhci
= ptr
;
3091 unsigned int epid
, streamid
;
3093 trace_usb_xhci_doorbell_write(reg
, val
);
3095 if (!xhci_running(xhci
)) {
3096 DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
3104 xhci_process_commands(xhci
);
3106 DPRINTF("xhci: bad doorbell 0 write: 0x%x\n",
3111 streamid
= (val
>> 16) & 0xffff;
3112 if (reg
> xhci
->numslots
) {
3113 DPRINTF("xhci: bad doorbell %d\n", (int)reg
);
3114 } else if (epid
== 0 || epid
> 31) {
3115 DPRINTF("xhci: bad doorbell %d write: 0x%x\n",
3116 (int)reg
, (uint32_t)val
);
3118 xhci_kick_ep(xhci
, reg
, epid
, streamid
);
3123 static void xhci_cap_write(void *opaque
, hwaddr addr
, uint64_t val
,
3129 static const MemoryRegionOps xhci_cap_ops
= {
3130 .read
= xhci_cap_read
,
3131 .write
= xhci_cap_write
,
3132 .valid
.min_access_size
= 1,
3133 .valid
.max_access_size
= 4,
3134 .impl
.min_access_size
= 4,
3135 .impl
.max_access_size
= 4,
3136 .endianness
= DEVICE_LITTLE_ENDIAN
,
3139 static const MemoryRegionOps xhci_oper_ops
= {
3140 .read
= xhci_oper_read
,
3141 .write
= xhci_oper_write
,
3142 .valid
.min_access_size
= 4,
3143 .valid
.max_access_size
= sizeof(dma_addr_t
),
3144 .endianness
= DEVICE_LITTLE_ENDIAN
,
3147 static const MemoryRegionOps xhci_port_ops
= {
3148 .read
= xhci_port_read
,
3149 .write
= xhci_port_write
,
3150 .valid
.min_access_size
= 4,
3151 .valid
.max_access_size
= 4,
3152 .endianness
= DEVICE_LITTLE_ENDIAN
,
3155 static const MemoryRegionOps xhci_runtime_ops
= {
3156 .read
= xhci_runtime_read
,
3157 .write
= xhci_runtime_write
,
3158 .valid
.min_access_size
= 4,
3159 .valid
.max_access_size
= sizeof(dma_addr_t
),
3160 .endianness
= DEVICE_LITTLE_ENDIAN
,
3163 static const MemoryRegionOps xhci_doorbell_ops
= {
3164 .read
= xhci_doorbell_read
,
3165 .write
= xhci_doorbell_write
,
3166 .valid
.min_access_size
= 4,
3167 .valid
.max_access_size
= 4,
3168 .endianness
= DEVICE_LITTLE_ENDIAN
,
3171 static void xhci_attach(USBPort
*usbport
)
3173 XHCIState
*xhci
= usbport
->opaque
;
3174 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3176 xhci_port_update(port
, 0);
3179 static void xhci_detach(USBPort
*usbport
)
3181 XHCIState
*xhci
= usbport
->opaque
;
3182 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3184 xhci_detach_slot(xhci
, usbport
);
3185 xhci_port_update(port
, 1);
3188 static void xhci_wakeup(USBPort
*usbport
)
3190 XHCIState
*xhci
= usbport
->opaque
;
3191 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3194 if (get_field(port
->portsc
, PORTSC_PLS
) != PLS_U3
) {
3197 set_field(&port
->portsc
, PLS_RESUME
, PORTSC_PLS
);
3198 xhci_port_notify(port
, PORTSC_PLC
);
3201 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
3203 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
3205 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
3206 xhci_ep_nuke_one_xfer(xfer
, 0);
3209 xhci_try_complete_packet(xfer
);
3210 xhci_kick_epctx(xfer
->epctx
, xfer
->streamid
);
3211 if (xfer
->complete
) {
3212 xhci_ep_free_xfer(xfer
);
3216 static void xhci_child_detach(USBPort
*uport
, USBDevice
*child
)
3218 USBBus
*bus
= usb_bus_from_device(child
);
3219 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3221 xhci_detach_slot(xhci
, child
->port
);
3224 static USBPortOps xhci_uport_ops
= {
3225 .attach
= xhci_attach
,
3226 .detach
= xhci_detach
,
3227 .wakeup
= xhci_wakeup
,
3228 .complete
= xhci_complete
,
3229 .child_detach
= xhci_child_detach
,
3232 static int xhci_find_epid(USBEndpoint
*ep
)
3237 if (ep
->pid
== USB_TOKEN_IN
) {
3238 return ep
->nr
* 2 + 1;
3244 static USBEndpoint
*xhci_epid_to_usbep(XHCIEPContext
*epctx
)
3252 uport
= epctx
->xhci
->slots
[epctx
->slotid
- 1].uport
;
3253 if (!uport
|| !uport
->dev
) {
3256 token
= (epctx
->epid
& 1) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
3257 return usb_ep_get(uport
->dev
, token
, epctx
->epid
>> 1);
3260 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
3261 unsigned int stream
)
3263 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3266 DPRINTF("%s\n", __func__
);
3267 slotid
= ep
->dev
->addr
;
3268 if (slotid
== 0 || !xhci
->slots
[slotid
-1].enabled
) {
3269 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
3272 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
), stream
);
3275 static USBBusOps xhci_bus_ops
= {
3276 .wakeup_endpoint
= xhci_wakeup_endpoint
,
3279 static void usb_xhci_init(XHCIState
*xhci
)
3282 unsigned int i
, usbports
, speedmask
;
3284 xhci
->usbsts
= USBSTS_HCH
;
3286 if (xhci
->numports_2
> XHCI_MAXPORTS_2
) {
3287 xhci
->numports_2
= XHCI_MAXPORTS_2
;
3289 if (xhci
->numports_3
> XHCI_MAXPORTS_3
) {
3290 xhci
->numports_3
= XHCI_MAXPORTS_3
;
3292 usbports
= MAX(xhci
->numports_2
, xhci
->numports_3
);
3293 xhci
->numports
= xhci
->numports_2
+ xhci
->numports_3
;
3295 usb_bus_new(&xhci
->bus
, sizeof(xhci
->bus
), &xhci_bus_ops
, xhci
->hostOpaque
);
3297 for (i
= 0; i
< usbports
; i
++) {
3299 if (i
< xhci
->numports_2
) {
3300 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3301 port
= &xhci
->ports
[i
+ xhci
->numports_3
];
3302 port
->portnr
= i
+ 1 + xhci
->numports_3
;
3304 port
= &xhci
->ports
[i
];
3305 port
->portnr
= i
+ 1;
3307 port
->uport
= &xhci
->uports
[i
];
3309 USB_SPEED_MASK_LOW
|
3310 USB_SPEED_MASK_FULL
|
3311 USB_SPEED_MASK_HIGH
;
3312 assert(i
< XHCI_MAXPORTS
);
3313 snprintf(port
->name
, sizeof(port
->name
), "usb2 port #%d", i
+1);
3314 speedmask
|= port
->speedmask
;
3316 if (i
< xhci
->numports_3
) {
3317 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3318 port
= &xhci
->ports
[i
];
3319 port
->portnr
= i
+ 1;
3321 port
= &xhci
->ports
[i
+ xhci
->numports_2
];
3322 port
->portnr
= i
+ 1 + xhci
->numports_2
;
3324 port
->uport
= &xhci
->uports
[i
];
3325 port
->speedmask
= USB_SPEED_MASK_SUPER
;
3326 assert(i
< XHCI_MAXPORTS
);
3327 snprintf(port
->name
, sizeof(port
->name
), "usb3 port #%d", i
+1);
3328 speedmask
|= port
->speedmask
;
3330 usb_register_port(&xhci
->bus
, &xhci
->uports
[i
], xhci
, i
,
3331 &xhci_uport_ops
, speedmask
);
3335 static void usb_xhci_realize(DeviceState
*dev
, Error
**errp
)
3339 XHCIState
*xhci
= XHCI(dev
);
3341 if (xhci
->numintrs
> XHCI_MAXINTRS
) {
3342 xhci
->numintrs
= XHCI_MAXINTRS
;
3344 while (xhci
->numintrs
& (xhci
->numintrs
- 1)) { /* ! power of 2 */
3347 if (xhci
->numintrs
< 1) {
3350 if (xhci
->numslots
> XHCI_MAXSLOTS
) {
3351 xhci
->numslots
= XHCI_MAXSLOTS
;
3353 if (xhci
->numslots
< 1) {
3356 if (xhci_get_flag(xhci
, XHCI_FLAG_ENABLE_STREAMS
)) {
3357 xhci
->max_pstreams_mask
= 7; /* == 256 primary streams */
3359 xhci
->max_pstreams_mask
= 0;
3362 usb_xhci_init(xhci
);
3363 xhci
->mfwrap_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_mfwrap_timer
, xhci
);
3365 memory_region_init(&xhci
->mem
, OBJECT(dev
), "xhci", XHCI_LEN_REGS
);
3366 memory_region_init_io(&xhci
->mem_cap
, OBJECT(dev
), &xhci_cap_ops
, xhci
,
3367 "capabilities", LEN_CAP
);
3368 memory_region_init_io(&xhci
->mem_oper
, OBJECT(dev
), &xhci_oper_ops
, xhci
,
3369 "operational", 0x400);
3370 memory_region_init_io(&xhci
->mem_runtime
, OBJECT(dev
), &xhci_runtime_ops
,
3371 xhci
, "runtime", LEN_RUNTIME
);
3372 memory_region_init_io(&xhci
->mem_doorbell
, OBJECT(dev
), &xhci_doorbell_ops
,
3373 xhci
, "doorbell", LEN_DOORBELL
);
3375 memory_region_add_subregion(&xhci
->mem
, 0, &xhci
->mem_cap
);
3376 memory_region_add_subregion(&xhci
->mem
, OFF_OPER
, &xhci
->mem_oper
);
3377 memory_region_add_subregion(&xhci
->mem
, OFF_RUNTIME
, &xhci
->mem_runtime
);
3378 memory_region_add_subregion(&xhci
->mem
, OFF_DOORBELL
, &xhci
->mem_doorbell
);
3380 for (i
= 0; i
< xhci
->numports
; i
++) {
3381 XHCIPort
*port
= &xhci
->ports
[i
];
3382 uint32_t offset
= OFF_OPER
+ 0x400 + 0x10 * i
;
3384 memory_region_init_io(&port
->mem
, OBJECT(dev
), &xhci_port_ops
, port
,
3386 memory_region_add_subregion(&xhci
->mem
, offset
, &port
->mem
);
3390 static void usb_xhci_unrealize(DeviceState
*dev
)
3393 XHCIState
*xhci
= XHCI(dev
);
3395 trace_usb_xhci_exit();
3397 for (i
= 0; i
< xhci
->numslots
; i
++) {
3398 xhci_disable_slot(xhci
, i
+ 1);
3401 if (xhci
->mfwrap_timer
) {
3402 timer_free(xhci
->mfwrap_timer
);
3403 xhci
->mfwrap_timer
= NULL
;
3406 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_cap
);
3407 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_oper
);
3408 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_runtime
);
3409 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_doorbell
);
3411 for (i
= 0; i
< xhci
->numports
; i
++) {
3412 XHCIPort
*port
= &xhci
->ports
[i
];
3413 memory_region_del_subregion(&xhci
->mem
, &port
->mem
);
3416 usb_bus_release(&xhci
->bus
);
3419 static int usb_xhci_post_load(void *opaque
, int version_id
)
3421 XHCIState
*xhci
= opaque
;
3423 XHCIEPContext
*epctx
;
3424 dma_addr_t dcbaap
, pctx
;
3425 uint32_t slot_ctx
[4];
3427 int slotid
, epid
, state
;
3429 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
3431 for (slotid
= 1; slotid
<= xhci
->numslots
; slotid
++) {
3432 slot
= &xhci
->slots
[slotid
-1];
3433 if (!slot
->addressed
) {
3437 xhci_mask64(ldq_le_dma(xhci
->as
, dcbaap
+ 8 * slotid
));
3438 xhci_dma_read_u32s(xhci
, slot
->ctx
, slot_ctx
, sizeof(slot_ctx
));
3439 slot
->uport
= xhci_lookup_uport(xhci
, slot_ctx
);
3441 /* should not happen, but may trigger on guest bugs */
3443 slot
->addressed
= 0;
3446 assert(slot
->uport
&& slot
->uport
->dev
);
3448 for (epid
= 1; epid
<= 31; epid
++) {
3449 pctx
= slot
->ctx
+ 32 * epid
;
3450 xhci_dma_read_u32s(xhci
, pctx
, ep_ctx
, sizeof(ep_ctx
));
3451 state
= ep_ctx
[0] & EP_STATE_MASK
;
3452 if (state
== EP_DISABLED
) {
3455 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
3456 slot
->eps
[epid
-1] = epctx
;
3457 xhci_init_epctx(epctx
, pctx
, ep_ctx
);
3458 epctx
->state
= state
;
3459 if (state
== EP_RUNNING
) {
3460 /* kick endpoint after vmload is finished */
3461 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
3468 static const VMStateDescription vmstate_xhci_ring
= {
3469 .name
= "xhci-ring",
3471 .fields
= (VMStateField
[]) {
3472 VMSTATE_UINT64(dequeue
, XHCIRing
),
3473 VMSTATE_BOOL(ccs
, XHCIRing
),
3474 VMSTATE_END_OF_LIST()
3478 static const VMStateDescription vmstate_xhci_port
= {
3479 .name
= "xhci-port",
3481 .fields
= (VMStateField
[]) {
3482 VMSTATE_UINT32(portsc
, XHCIPort
),
3483 VMSTATE_END_OF_LIST()
3487 static const VMStateDescription vmstate_xhci_slot
= {
3488 .name
= "xhci-slot",
3490 .fields
= (VMStateField
[]) {
3491 VMSTATE_BOOL(enabled
, XHCISlot
),
3492 VMSTATE_BOOL(addressed
, XHCISlot
),
3493 VMSTATE_END_OF_LIST()
3497 static const VMStateDescription vmstate_xhci_event
= {
3498 .name
= "xhci-event",
3500 .fields
= (VMStateField
[]) {
3501 VMSTATE_UINT32(type
, XHCIEvent
),
3502 VMSTATE_UINT32(ccode
, XHCIEvent
),
3503 VMSTATE_UINT64(ptr
, XHCIEvent
),
3504 VMSTATE_UINT32(length
, XHCIEvent
),
3505 VMSTATE_UINT32(flags
, XHCIEvent
),
3506 VMSTATE_UINT8(slotid
, XHCIEvent
),
3507 VMSTATE_UINT8(epid
, XHCIEvent
),
3508 VMSTATE_END_OF_LIST()
3512 static bool xhci_er_full(void *opaque
, int version_id
)
3517 static const VMStateDescription vmstate_xhci_intr
= {
3518 .name
= "xhci-intr",
3520 .fields
= (VMStateField
[]) {
3522 VMSTATE_UINT32(iman
, XHCIInterrupter
),
3523 VMSTATE_UINT32(imod
, XHCIInterrupter
),
3524 VMSTATE_UINT32(erstsz
, XHCIInterrupter
),
3525 VMSTATE_UINT32(erstba_low
, XHCIInterrupter
),
3526 VMSTATE_UINT32(erstba_high
, XHCIInterrupter
),
3527 VMSTATE_UINT32(erdp_low
, XHCIInterrupter
),
3528 VMSTATE_UINT32(erdp_high
, XHCIInterrupter
),
3531 VMSTATE_BOOL(msix_used
, XHCIInterrupter
),
3532 VMSTATE_BOOL(er_pcs
, XHCIInterrupter
),
3533 VMSTATE_UINT64(er_start
, XHCIInterrupter
),
3534 VMSTATE_UINT32(er_size
, XHCIInterrupter
),
3535 VMSTATE_UINT32(er_ep_idx
, XHCIInterrupter
),
3537 /* event queue (used if ring is full) */
3538 VMSTATE_BOOL(er_full_unused
, XHCIInterrupter
),
3539 VMSTATE_UINT32_TEST(ev_buffer_put
, XHCIInterrupter
, xhci_er_full
),
3540 VMSTATE_UINT32_TEST(ev_buffer_get
, XHCIInterrupter
, xhci_er_full
),
3541 VMSTATE_STRUCT_ARRAY_TEST(ev_buffer
, XHCIInterrupter
, EV_QUEUE
,
3543 vmstate_xhci_event
, XHCIEvent
),
3545 VMSTATE_END_OF_LIST()
3549 const VMStateDescription vmstate_xhci
= {
3550 .name
= "xhci-core",
3552 .post_load
= usb_xhci_post_load
,
3553 .fields
= (VMStateField
[]) {
3554 VMSTATE_STRUCT_VARRAY_UINT32(ports
, XHCIState
, numports
, 1,
3555 vmstate_xhci_port
, XHCIPort
),
3556 VMSTATE_STRUCT_VARRAY_UINT32(slots
, XHCIState
, numslots
, 1,
3557 vmstate_xhci_slot
, XHCISlot
),
3558 VMSTATE_STRUCT_VARRAY_UINT32(intr
, XHCIState
, numintrs
, 1,
3559 vmstate_xhci_intr
, XHCIInterrupter
),
3561 /* Operational Registers */
3562 VMSTATE_UINT32(usbcmd
, XHCIState
),
3563 VMSTATE_UINT32(usbsts
, XHCIState
),
3564 VMSTATE_UINT32(dnctrl
, XHCIState
),
3565 VMSTATE_UINT32(crcr_low
, XHCIState
),
3566 VMSTATE_UINT32(crcr_high
, XHCIState
),
3567 VMSTATE_UINT32(dcbaap_low
, XHCIState
),
3568 VMSTATE_UINT32(dcbaap_high
, XHCIState
),
3569 VMSTATE_UINT32(config
, XHCIState
),
3571 /* Runtime Registers & state */
3572 VMSTATE_INT64(mfindex_start
, XHCIState
),
3573 VMSTATE_TIMER_PTR(mfwrap_timer
, XHCIState
),
3574 VMSTATE_STRUCT(cmd_ring
, XHCIState
, 1, vmstate_xhci_ring
, XHCIRing
),
3576 VMSTATE_END_OF_LIST()
3580 static Property xhci_properties
[] = {
3581 DEFINE_PROP_BIT("streams", XHCIState
, flags
,
3582 XHCI_FLAG_ENABLE_STREAMS
, true),
3583 DEFINE_PROP_UINT32("p2", XHCIState
, numports_2
, 4),
3584 DEFINE_PROP_UINT32("p3", XHCIState
, numports_3
, 4),
3585 DEFINE_PROP_LINK("host", XHCIState
, hostOpaque
, TYPE_DEVICE
,
3587 DEFINE_PROP_END_OF_LIST(),
3590 static void xhci_class_init(ObjectClass
*klass
, void *data
)
3592 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3594 dc
->realize
= usb_xhci_realize
;
3595 dc
->unrealize
= usb_xhci_unrealize
;
3596 dc
->reset
= xhci_reset
;
3597 device_class_set_props(dc
, xhci_properties
);
3598 dc
->user_creatable
= false;
3601 static const TypeInfo xhci_info
= {
3603 .parent
= TYPE_DEVICE
,
3604 .instance_size
= sizeof(XHCIState
),
3605 .class_init
= xhci_class_init
,
3608 static void xhci_register_types(void)
3610 type_register_static(&xhci_info
);
3613 type_init(xhci_register_types
)