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 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/>.
21 #include "qemu/osdep.h"
23 #include "qemu/timer.h"
24 #include "qemu/queue.h"
26 #include "hw/pci/pci.h"
27 #include "hw/pci/msi.h"
28 #include "hw/pci/msix.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 * MAXPORTS)
51 #define LEN_RUNTIME ((MAXINTRS + 1) * 0x20)
52 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
54 #define OFF_OPER LEN_CAP
55 #define OFF_RUNTIME 0x1000
56 #define OFF_DOORBELL 0x2000
57 #define OFF_MSIX_TABLE 0x3000
58 #define OFF_MSIX_PBA 0x3800
59 /* must be power of 2 */
60 #define LEN_REGS 0x4000
62 #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
63 #error Increase OFF_RUNTIME
65 #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
66 #error Increase OFF_DOORBELL
68 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
69 # error Increase LEN_REGS
73 #define USBCMD_RS (1<<0)
74 #define USBCMD_HCRST (1<<1)
75 #define USBCMD_INTE (1<<2)
76 #define USBCMD_HSEE (1<<3)
77 #define USBCMD_LHCRST (1<<7)
78 #define USBCMD_CSS (1<<8)
79 #define USBCMD_CRS (1<<9)
80 #define USBCMD_EWE (1<<10)
81 #define USBCMD_EU3S (1<<11)
83 #define USBSTS_HCH (1<<0)
84 #define USBSTS_HSE (1<<2)
85 #define USBSTS_EINT (1<<3)
86 #define USBSTS_PCD (1<<4)
87 #define USBSTS_SSS (1<<8)
88 #define USBSTS_RSS (1<<9)
89 #define USBSTS_SRE (1<<10)
90 #define USBSTS_CNR (1<<11)
91 #define USBSTS_HCE (1<<12)
94 #define PORTSC_CCS (1<<0)
95 #define PORTSC_PED (1<<1)
96 #define PORTSC_OCA (1<<3)
97 #define PORTSC_PR (1<<4)
98 #define PORTSC_PLS_SHIFT 5
99 #define PORTSC_PLS_MASK 0xf
100 #define PORTSC_PP (1<<9)
101 #define PORTSC_SPEED_SHIFT 10
102 #define PORTSC_SPEED_MASK 0xf
103 #define PORTSC_SPEED_FULL (1<<10)
104 #define PORTSC_SPEED_LOW (2<<10)
105 #define PORTSC_SPEED_HIGH (3<<10)
106 #define PORTSC_SPEED_SUPER (4<<10)
107 #define PORTSC_PIC_SHIFT 14
108 #define PORTSC_PIC_MASK 0x3
109 #define PORTSC_LWS (1<<16)
110 #define PORTSC_CSC (1<<17)
111 #define PORTSC_PEC (1<<18)
112 #define PORTSC_WRC (1<<19)
113 #define PORTSC_OCC (1<<20)
114 #define PORTSC_PRC (1<<21)
115 #define PORTSC_PLC (1<<22)
116 #define PORTSC_CEC (1<<23)
117 #define PORTSC_CAS (1<<24)
118 #define PORTSC_WCE (1<<25)
119 #define PORTSC_WDE (1<<26)
120 #define PORTSC_WOE (1<<27)
121 #define PORTSC_DR (1<<30)
122 #define PORTSC_WPR (1<<31)
124 #define CRCR_RCS (1<<0)
125 #define CRCR_CS (1<<1)
126 #define CRCR_CA (1<<2)
127 #define CRCR_CRR (1<<3)
129 #define IMAN_IP (1<<0)
130 #define IMAN_IE (1<<1)
132 #define ERDP_EHB (1<<3)
135 typedef struct XHCITRB
{
154 PLS_COMPILANCE_MODE
= 10,
159 #define CR_LINK TR_LINK
162 #define TRB_TYPE_SHIFT 10
163 #define TRB_TYPE_MASK 0x3f
164 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
166 #define TRB_EV_ED (1<<2)
168 #define TRB_TR_ENT (1<<1)
169 #define TRB_TR_ISP (1<<2)
170 #define TRB_TR_NS (1<<3)
171 #define TRB_TR_CH (1<<4)
172 #define TRB_TR_IOC (1<<5)
173 #define TRB_TR_IDT (1<<6)
174 #define TRB_TR_TBC_SHIFT 7
175 #define TRB_TR_TBC_MASK 0x3
176 #define TRB_TR_BEI (1<<9)
177 #define TRB_TR_TLBPC_SHIFT 16
178 #define TRB_TR_TLBPC_MASK 0xf
179 #define TRB_TR_FRAMEID_SHIFT 20
180 #define TRB_TR_FRAMEID_MASK 0x7ff
181 #define TRB_TR_SIA (1<<31)
183 #define TRB_TR_DIR (1<<16)
185 #define TRB_CR_SLOTID_SHIFT 24
186 #define TRB_CR_SLOTID_MASK 0xff
187 #define TRB_CR_EPID_SHIFT 16
188 #define TRB_CR_EPID_MASK 0x1f
190 #define TRB_CR_BSR (1<<9)
191 #define TRB_CR_DC (1<<9)
193 #define TRB_LK_TC (1<<1)
195 #define TRB_INTR_SHIFT 22
196 #define TRB_INTR_MASK 0x3ff
197 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
199 #define EP_TYPE_MASK 0x7
200 #define EP_TYPE_SHIFT 3
202 #define EP_STATE_MASK 0x7
203 #define EP_DISABLED (0<<0)
204 #define EP_RUNNING (1<<0)
205 #define EP_HALTED (2<<0)
206 #define EP_STOPPED (3<<0)
207 #define EP_ERROR (4<<0)
209 #define SLOT_STATE_MASK 0x1f
210 #define SLOT_STATE_SHIFT 27
211 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
212 #define SLOT_ENABLED 0
213 #define SLOT_DEFAULT 1
214 #define SLOT_ADDRESSED 2
215 #define SLOT_CONFIGURED 3
217 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
218 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
220 #define get_field(data, field) \
221 (((data) >> field##_SHIFT) & field##_MASK)
223 #define set_field(data, newval, field) do { \
224 uint32_t val = *data; \
225 val &= ~(field##_MASK << field##_SHIFT); \
226 val |= ((newval) & field##_MASK) << field##_SHIFT; \
230 typedef enum EPType
{
241 typedef struct XHCITransfer
{
242 XHCIEPContext
*epctx
;
249 unsigned int iso_pkts
;
250 unsigned int streamid
;
255 unsigned int trb_count
;
261 unsigned int pktsize
;
262 unsigned int cur_pkt
;
264 uint64_t mfindex_kick
;
266 QTAILQ_ENTRY(XHCITransfer
) next
;
269 struct XHCIStreamContext
{
275 struct XHCIEPContext
{
282 QTAILQ_HEAD(, XHCITransfer
) transfers
;
286 unsigned int max_psize
;
288 uint32_t kick_active
;
291 unsigned int max_pstreams
;
293 unsigned int nr_pstreams
;
294 XHCIStreamContext
*pstreams
;
296 /* iso xfer scheduling */
297 unsigned int interval
;
298 int64_t mfindex_last
;
299 QEMUTimer
*kick_timer
;
302 typedef struct XHCIEvRingSeg
{
309 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
310 unsigned int epid
, unsigned int streamid
);
311 static void xhci_kick_epctx(XHCIEPContext
*epctx
, unsigned int streamid
);
312 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
314 static void xhci_xfer_report(XHCITransfer
*xfer
);
315 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
316 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
317 static USBEndpoint
*xhci_epid_to_usbep(XHCIEPContext
*epctx
);
319 static const char *TRBType_names
[] = {
320 [TRB_RESERVED
] = "TRB_RESERVED",
321 [TR_NORMAL
] = "TR_NORMAL",
322 [TR_SETUP
] = "TR_SETUP",
323 [TR_DATA
] = "TR_DATA",
324 [TR_STATUS
] = "TR_STATUS",
325 [TR_ISOCH
] = "TR_ISOCH",
326 [TR_LINK
] = "TR_LINK",
327 [TR_EVDATA
] = "TR_EVDATA",
328 [TR_NOOP
] = "TR_NOOP",
329 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
330 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
331 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
332 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
333 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
334 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
335 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
336 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
337 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
338 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
339 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
340 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
341 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
342 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
343 [CR_NOOP
] = "CR_NOOP",
344 [ER_TRANSFER
] = "ER_TRANSFER",
345 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
346 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
347 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
348 [ER_DOORBELL
] = "ER_DOORBELL",
349 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
350 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
351 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
352 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
353 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
356 static const char *TRBCCode_names
[] = {
357 [CC_INVALID
] = "CC_INVALID",
358 [CC_SUCCESS
] = "CC_SUCCESS",
359 [CC_DATA_BUFFER_ERROR
] = "CC_DATA_BUFFER_ERROR",
360 [CC_BABBLE_DETECTED
] = "CC_BABBLE_DETECTED",
361 [CC_USB_TRANSACTION_ERROR
] = "CC_USB_TRANSACTION_ERROR",
362 [CC_TRB_ERROR
] = "CC_TRB_ERROR",
363 [CC_STALL_ERROR
] = "CC_STALL_ERROR",
364 [CC_RESOURCE_ERROR
] = "CC_RESOURCE_ERROR",
365 [CC_BANDWIDTH_ERROR
] = "CC_BANDWIDTH_ERROR",
366 [CC_NO_SLOTS_ERROR
] = "CC_NO_SLOTS_ERROR",
367 [CC_INVALID_STREAM_TYPE_ERROR
] = "CC_INVALID_STREAM_TYPE_ERROR",
368 [CC_SLOT_NOT_ENABLED_ERROR
] = "CC_SLOT_NOT_ENABLED_ERROR",
369 [CC_EP_NOT_ENABLED_ERROR
] = "CC_EP_NOT_ENABLED_ERROR",
370 [CC_SHORT_PACKET
] = "CC_SHORT_PACKET",
371 [CC_RING_UNDERRUN
] = "CC_RING_UNDERRUN",
372 [CC_RING_OVERRUN
] = "CC_RING_OVERRUN",
373 [CC_VF_ER_FULL
] = "CC_VF_ER_FULL",
374 [CC_PARAMETER_ERROR
] = "CC_PARAMETER_ERROR",
375 [CC_BANDWIDTH_OVERRUN
] = "CC_BANDWIDTH_OVERRUN",
376 [CC_CONTEXT_STATE_ERROR
] = "CC_CONTEXT_STATE_ERROR",
377 [CC_NO_PING_RESPONSE_ERROR
] = "CC_NO_PING_RESPONSE_ERROR",
378 [CC_EVENT_RING_FULL_ERROR
] = "CC_EVENT_RING_FULL_ERROR",
379 [CC_INCOMPATIBLE_DEVICE_ERROR
] = "CC_INCOMPATIBLE_DEVICE_ERROR",
380 [CC_MISSED_SERVICE_ERROR
] = "CC_MISSED_SERVICE_ERROR",
381 [CC_COMMAND_RING_STOPPED
] = "CC_COMMAND_RING_STOPPED",
382 [CC_COMMAND_ABORTED
] = "CC_COMMAND_ABORTED",
383 [CC_STOPPED
] = "CC_STOPPED",
384 [CC_STOPPED_LENGTH_INVALID
] = "CC_STOPPED_LENGTH_INVALID",
385 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
]
386 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
387 [CC_ISOCH_BUFFER_OVERRUN
] = "CC_ISOCH_BUFFER_OVERRUN",
388 [CC_EVENT_LOST_ERROR
] = "CC_EVENT_LOST_ERROR",
389 [CC_UNDEFINED_ERROR
] = "CC_UNDEFINED_ERROR",
390 [CC_INVALID_STREAM_ID_ERROR
] = "CC_INVALID_STREAM_ID_ERROR",
391 [CC_SECONDARY_BANDWIDTH_ERROR
] = "CC_SECONDARY_BANDWIDTH_ERROR",
392 [CC_SPLIT_TRANSACTION_ERROR
] = "CC_SPLIT_TRANSACTION_ERROR",
395 static const char *ep_state_names
[] = {
396 [EP_DISABLED
] = "disabled",
397 [EP_RUNNING
] = "running",
398 [EP_HALTED
] = "halted",
399 [EP_STOPPED
] = "stopped",
400 [EP_ERROR
] = "error",
403 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
405 if (index
>= llen
|| list
[index
] == NULL
) {
411 static const char *trb_name(XHCITRB
*trb
)
413 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
414 ARRAY_SIZE(TRBType_names
));
417 static const char *event_name(XHCIEvent
*event
)
419 return lookup_name(event
->ccode
, TRBCCode_names
,
420 ARRAY_SIZE(TRBCCode_names
));
423 static const char *ep_state_name(uint32_t state
)
425 return lookup_name(state
, ep_state_names
,
426 ARRAY_SIZE(ep_state_names
));
429 static bool xhci_get_flag(XHCIState
*xhci
, enum xhci_flags bit
)
431 return xhci
->flags
& (1 << bit
);
434 static void xhci_set_flag(XHCIState
*xhci
, enum xhci_flags bit
)
436 xhci
->flags
|= (1 << bit
);
439 static uint64_t xhci_mfindex_get(XHCIState
*xhci
)
441 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
442 return (now
- xhci
->mfindex_start
) / 125000;
445 static void xhci_mfwrap_update(XHCIState
*xhci
)
447 const uint32_t bits
= USBCMD_RS
| USBCMD_EWE
;
448 uint32_t mfindex
, left
;
451 if ((xhci
->usbcmd
& bits
) == bits
) {
452 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
453 mfindex
= ((now
- xhci
->mfindex_start
) / 125000) & 0x3fff;
454 left
= 0x4000 - mfindex
;
455 timer_mod(xhci
->mfwrap_timer
, now
+ left
* 125000);
457 timer_del(xhci
->mfwrap_timer
);
461 static void xhci_mfwrap_timer(void *opaque
)
463 XHCIState
*xhci
= opaque
;
464 XHCIEvent wrap
= { ER_MFINDEX_WRAP
, CC_SUCCESS
};
466 xhci_event(xhci
, &wrap
, 0);
467 xhci_mfwrap_update(xhci
);
470 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
472 if (sizeof(dma_addr_t
) == 4) {
475 return low
| (((dma_addr_t
)high
<< 16) << 16);
479 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
481 if (sizeof(dma_addr_t
) == 4) {
482 return addr
& 0xffffffff;
488 static inline void xhci_dma_read_u32s(XHCIState
*xhci
, dma_addr_t addr
,
489 uint32_t *buf
, size_t len
)
493 assert((len
% sizeof(uint32_t)) == 0);
495 pci_dma_read(PCI_DEVICE(xhci
), addr
, buf
, len
);
497 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
498 buf
[i
] = le32_to_cpu(buf
[i
]);
502 static inline void xhci_dma_write_u32s(XHCIState
*xhci
, dma_addr_t addr
,
503 uint32_t *buf
, size_t len
)
507 uint32_t n
= len
/ sizeof(uint32_t);
509 assert((len
% sizeof(uint32_t)) == 0);
510 assert(n
<= ARRAY_SIZE(tmp
));
512 for (i
= 0; i
< n
; i
++) {
513 tmp
[i
] = cpu_to_le32(buf
[i
]);
515 pci_dma_write(PCI_DEVICE(xhci
), addr
, tmp
, len
);
518 static XHCIPort
*xhci_lookup_port(XHCIState
*xhci
, struct USBPort
*uport
)
525 switch (uport
->dev
->speed
) {
529 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
530 index
= uport
->index
+ xhci
->numports_3
;
532 index
= uport
->index
;
535 case USB_SPEED_SUPER
:
536 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
537 index
= uport
->index
;
539 index
= uport
->index
+ xhci
->numports_2
;
545 return &xhci
->ports
[index
];
548 static void xhci_intx_update(XHCIState
*xhci
)
550 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
553 if (msix_enabled(pci_dev
) ||
554 msi_enabled(pci_dev
)) {
558 if (xhci
->intr
[0].iman
& IMAN_IP
&&
559 xhci
->intr
[0].iman
& IMAN_IE
&&
560 xhci
->usbcmd
& USBCMD_INTE
) {
564 trace_usb_xhci_irq_intx(level
);
565 pci_set_irq(pci_dev
, level
);
568 static void xhci_msix_update(XHCIState
*xhci
, int v
)
570 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
573 if (!msix_enabled(pci_dev
)) {
577 enabled
= xhci
->intr
[v
].iman
& IMAN_IE
;
578 if (enabled
== xhci
->intr
[v
].msix_used
) {
583 trace_usb_xhci_irq_msix_use(v
);
584 msix_vector_use(pci_dev
, v
);
585 xhci
->intr
[v
].msix_used
= true;
587 trace_usb_xhci_irq_msix_unuse(v
);
588 msix_vector_unuse(pci_dev
, v
);
589 xhci
->intr
[v
].msix_used
= false;
593 static void xhci_intr_raise(XHCIState
*xhci
, int v
)
595 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
596 bool pending
= (xhci
->intr
[v
].erdp_low
& ERDP_EHB
);
598 xhci
->intr
[v
].erdp_low
|= ERDP_EHB
;
599 xhci
->intr
[v
].iman
|= IMAN_IP
;
600 xhci
->usbsts
|= USBSTS_EINT
;
605 if (!(xhci
->intr
[v
].iman
& IMAN_IE
)) {
609 if (!(xhci
->usbcmd
& USBCMD_INTE
)) {
613 if (msix_enabled(pci_dev
)) {
614 trace_usb_xhci_irq_msix(v
);
615 msix_notify(pci_dev
, v
);
619 if (msi_enabled(pci_dev
)) {
620 trace_usb_xhci_irq_msi(v
);
621 msi_notify(pci_dev
, v
);
626 trace_usb_xhci_irq_intx(1);
627 pci_irq_assert(pci_dev
);
631 static inline int xhci_running(XHCIState
*xhci
)
633 return !(xhci
->usbsts
& USBSTS_HCH
);
636 static void xhci_die(XHCIState
*xhci
)
638 xhci
->usbsts
|= USBSTS_HCE
;
639 DPRINTF("xhci: asserted controller error\n");
642 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
644 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
645 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
649 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
650 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
651 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
652 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
654 ev_trb
.control
|= TRB_C
;
656 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
658 trace_usb_xhci_queue_event(v
, intr
->er_ep_idx
, trb_name(&ev_trb
),
659 event_name(event
), ev_trb
.parameter
,
660 ev_trb
.status
, ev_trb
.control
);
662 addr
= intr
->er_start
+ TRB_SIZE
*intr
->er_ep_idx
;
663 pci_dma_write(pci_dev
, addr
, &ev_trb
, TRB_SIZE
);
666 if (intr
->er_ep_idx
>= intr
->er_size
) {
668 intr
->er_pcs
= !intr
->er_pcs
;
672 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
674 XHCIInterrupter
*intr
;
678 if (v
>= xhci
->numintrs
) {
679 DPRINTF("intr nr out of range (%d >= %d)\n", v
, xhci
->numintrs
);
682 intr
= &xhci
->intr
[v
];
684 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
685 if (erdp
< intr
->er_start
||
686 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
687 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
688 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
689 v
, intr
->er_start
, intr
->er_size
);
694 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
695 assert(dp_idx
< intr
->er_size
);
697 if ((intr
->er_ep_idx
+ 2) % intr
->er_size
== dp_idx
) {
698 DPRINTF("xhci: ER %d full, send ring full error\n", v
);
699 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
700 xhci_write_event(xhci
, &full
, v
);
701 } else if ((intr
->er_ep_idx
+ 1) % intr
->er_size
== dp_idx
) {
702 DPRINTF("xhci: ER %d full, drop event\n", v
);
704 xhci_write_event(xhci
, event
, v
);
707 xhci_intr_raise(xhci
, v
);
710 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
713 ring
->dequeue
= base
;
717 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
720 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
721 uint32_t link_cnt
= 0;
725 pci_dma_read(pci_dev
, ring
->dequeue
, trb
, TRB_SIZE
);
726 trb
->addr
= ring
->dequeue
;
727 trb
->ccs
= ring
->ccs
;
728 le64_to_cpus(&trb
->parameter
);
729 le32_to_cpus(&trb
->status
);
730 le32_to_cpus(&trb
->control
);
732 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
733 trb
->parameter
, trb
->status
, trb
->control
);
735 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
739 type
= TRB_TYPE(*trb
);
741 if (type
!= TR_LINK
) {
743 *addr
= ring
->dequeue
;
745 ring
->dequeue
+= TRB_SIZE
;
748 if (++link_cnt
> TRB_LINK_LIMIT
) {
749 trace_usb_xhci_enforced_limit("trb-link");
752 ring
->dequeue
= xhci_mask64(trb
->parameter
);
753 if (trb
->control
& TRB_LK_TC
) {
754 ring
->ccs
= !ring
->ccs
;
760 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
762 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
765 dma_addr_t dequeue
= ring
->dequeue
;
766 bool ccs
= ring
->ccs
;
767 /* hack to bundle together the two/three TDs that make a setup transfer */
768 bool control_td_set
= 0;
769 uint32_t link_cnt
= 0;
773 pci_dma_read(pci_dev
, dequeue
, &trb
, TRB_SIZE
);
774 le64_to_cpus(&trb
.parameter
);
775 le32_to_cpus(&trb
.status
);
776 le32_to_cpus(&trb
.control
);
778 if ((trb
.control
& TRB_C
) != ccs
) {
782 type
= TRB_TYPE(trb
);
784 if (type
== TR_LINK
) {
785 if (++link_cnt
> TRB_LINK_LIMIT
) {
788 dequeue
= xhci_mask64(trb
.parameter
);
789 if (trb
.control
& TRB_LK_TC
) {
798 if (type
== TR_SETUP
) {
800 } else if (type
== TR_STATUS
) {
804 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
810 static void xhci_er_reset(XHCIState
*xhci
, int v
)
812 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
814 dma_addr_t erstba
= xhci_addr64(intr
->erstba_low
, intr
->erstba_high
);
816 if (intr
->erstsz
== 0 || erstba
== 0) {
822 /* cache the (sole) event ring segment location */
823 if (intr
->erstsz
!= 1) {
824 DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr
->erstsz
);
828 pci_dma_read(PCI_DEVICE(xhci
), erstba
, &seg
, sizeof(seg
));
829 le32_to_cpus(&seg
.addr_low
);
830 le32_to_cpus(&seg
.addr_high
);
831 le32_to_cpus(&seg
.size
);
832 if (seg
.size
< 16 || seg
.size
> 4096) {
833 DPRINTF("xhci: invalid value for segment size: %d\n", seg
.size
);
837 intr
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
838 intr
->er_size
= seg
.size
;
843 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT
" [%d]\n",
844 v
, intr
->er_start
, intr
->er_size
);
847 static void xhci_run(XHCIState
*xhci
)
849 trace_usb_xhci_run();
850 xhci
->usbsts
&= ~USBSTS_HCH
;
851 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
854 static void xhci_stop(XHCIState
*xhci
)
856 trace_usb_xhci_stop();
857 xhci
->usbsts
|= USBSTS_HCH
;
858 xhci
->crcr_low
&= ~CRCR_CRR
;
861 static XHCIStreamContext
*xhci_alloc_stream_contexts(unsigned count
,
864 XHCIStreamContext
*stctx
;
867 stctx
= g_new0(XHCIStreamContext
, count
);
868 for (i
= 0; i
< count
; i
++) {
869 stctx
[i
].pctx
= base
+ i
* 16;
875 static void xhci_reset_streams(XHCIEPContext
*epctx
)
879 for (i
= 0; i
< epctx
->nr_pstreams
; i
++) {
880 epctx
->pstreams
[i
].sct
= -1;
884 static void xhci_alloc_streams(XHCIEPContext
*epctx
, dma_addr_t base
)
886 assert(epctx
->pstreams
== NULL
);
887 epctx
->nr_pstreams
= 2 << epctx
->max_pstreams
;
888 epctx
->pstreams
= xhci_alloc_stream_contexts(epctx
->nr_pstreams
, base
);
891 static void xhci_free_streams(XHCIEPContext
*epctx
)
893 assert(epctx
->pstreams
!= NULL
);
895 g_free(epctx
->pstreams
);
896 epctx
->pstreams
= NULL
;
897 epctx
->nr_pstreams
= 0;
900 static int xhci_epmask_to_eps_with_streams(XHCIState
*xhci
,
903 XHCIEPContext
**epctxs
,
907 XHCIEPContext
*epctx
;
911 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
913 slot
= &xhci
->slots
[slotid
- 1];
915 for (i
= 2, j
= 0; i
<= 31; i
++) {
916 if (!(epmask
& (1u << i
))) {
920 epctx
= slot
->eps
[i
- 1];
921 ep
= xhci_epid_to_usbep(epctx
);
922 if (!epctx
|| !epctx
->nr_pstreams
|| !ep
) {
934 static void xhci_free_device_streams(XHCIState
*xhci
, unsigned int slotid
,
937 USBEndpoint
*eps
[30];
940 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, NULL
, eps
);
942 usb_device_free_streams(eps
[0]->dev
, eps
, nr_eps
);
946 static TRBCCode
xhci_alloc_device_streams(XHCIState
*xhci
, unsigned int slotid
,
949 XHCIEPContext
*epctxs
[30];
950 USBEndpoint
*eps
[30];
951 int i
, r
, nr_eps
, req_nr_streams
, dev_max_streams
;
953 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, epctxs
,
959 req_nr_streams
= epctxs
[0]->nr_pstreams
;
960 dev_max_streams
= eps
[0]->max_streams
;
962 for (i
= 1; i
< nr_eps
; i
++) {
964 * HdG: I don't expect these to ever trigger, but if they do we need
965 * to come up with another solution, ie group identical endpoints
966 * together and make an usb_device_alloc_streams call per group.
968 if (epctxs
[i
]->nr_pstreams
!= req_nr_streams
) {
969 FIXME("guest streams config not identical for all eps");
970 return CC_RESOURCE_ERROR
;
972 if (eps
[i
]->max_streams
!= dev_max_streams
) {
973 FIXME("device streams config not identical for all eps");
974 return CC_RESOURCE_ERROR
;
979 * max-streams in both the device descriptor and in the controller is a
980 * power of 2. But stream id 0 is reserved, so if a device can do up to 4
981 * streams the guest will ask for 5 rounded up to the next power of 2 which
982 * becomes 8. For emulated devices usb_device_alloc_streams is a nop.
984 * For redirected devices however this is an issue, as there we must ask
985 * the real xhci controller to alloc streams, and the host driver for the
986 * real xhci controller will likely disallow allocating more streams then
987 * the device can handle.
989 * So we limit the requested nr_streams to the maximum number the device
992 if (req_nr_streams
> dev_max_streams
) {
993 req_nr_streams
= dev_max_streams
;
996 r
= usb_device_alloc_streams(eps
[0]->dev
, eps
, nr_eps
, req_nr_streams
);
998 DPRINTF("xhci: alloc streams failed\n");
999 return CC_RESOURCE_ERROR
;
1005 static XHCIStreamContext
*xhci_find_stream(XHCIEPContext
*epctx
,
1006 unsigned int streamid
,
1009 XHCIStreamContext
*sctx
;
1011 uint32_t ctx
[2], sct
;
1013 assert(streamid
!= 0);
1015 if (streamid
>= epctx
->nr_pstreams
) {
1016 *cc_error
= CC_INVALID_STREAM_ID_ERROR
;
1019 sctx
= epctx
->pstreams
+ streamid
;
1021 FIXME("secondary streams not implemented yet");
1024 if (sctx
->sct
== -1) {
1025 xhci_dma_read_u32s(epctx
->xhci
, sctx
->pctx
, ctx
, sizeof(ctx
));
1026 sct
= (ctx
[0] >> 1) & 0x07;
1027 if (epctx
->lsa
&& sct
!= 1) {
1028 *cc_error
= CC_INVALID_STREAM_TYPE_ERROR
;
1032 base
= xhci_addr64(ctx
[0] & ~0xf, ctx
[1]);
1033 xhci_ring_init(epctx
->xhci
, &sctx
->ring
, base
);
1038 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
1039 XHCIStreamContext
*sctx
, uint32_t state
)
1041 XHCIRing
*ring
= NULL
;
1045 xhci_dma_read_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1046 ctx
[0] &= ~EP_STATE_MASK
;
1049 /* update ring dequeue ptr */
1050 if (epctx
->nr_pstreams
) {
1053 xhci_dma_read_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1055 ctx2
[0] |= sctx
->ring
.dequeue
| sctx
->ring
.ccs
;
1056 ctx2
[1] = (sctx
->ring
.dequeue
>> 16) >> 16;
1057 xhci_dma_write_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1060 ring
= &epctx
->ring
;
1063 ctx
[2] = ring
->dequeue
| ring
->ccs
;
1064 ctx
[3] = (ring
->dequeue
>> 16) >> 16;
1066 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
1067 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
1070 xhci_dma_write_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1071 if (epctx
->state
!= state
) {
1072 trace_usb_xhci_ep_state(epctx
->slotid
, epctx
->epid
,
1073 ep_state_name(epctx
->state
),
1074 ep_state_name(state
));
1076 epctx
->state
= state
;
1079 static void xhci_ep_kick_timer(void *opaque
)
1081 XHCIEPContext
*epctx
= opaque
;
1082 xhci_kick_epctx(epctx
, 0);
1085 static XHCIEPContext
*xhci_alloc_epctx(XHCIState
*xhci
,
1086 unsigned int slotid
,
1089 XHCIEPContext
*epctx
;
1091 epctx
= g_new0(XHCIEPContext
, 1);
1093 epctx
->slotid
= slotid
;
1096 QTAILQ_INIT(&epctx
->transfers
);
1097 epctx
->kick_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_ep_kick_timer
, epctx
);
1102 static void xhci_init_epctx(XHCIEPContext
*epctx
,
1103 dma_addr_t pctx
, uint32_t *ctx
)
1107 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
1109 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
1111 epctx
->max_psize
= ctx
[1]>>16;
1112 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
1113 epctx
->max_pstreams
= (ctx
[0] >> 10) & epctx
->xhci
->max_pstreams_mask
;
1114 epctx
->lsa
= (ctx
[0] >> 15) & 1;
1115 if (epctx
->max_pstreams
) {
1116 xhci_alloc_streams(epctx
, dequeue
);
1118 xhci_ring_init(epctx
->xhci
, &epctx
->ring
, dequeue
);
1119 epctx
->ring
.ccs
= ctx
[2] & 1;
1122 epctx
->interval
= 1 << ((ctx
[0] >> 16) & 0xff);
1125 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
1126 unsigned int epid
, dma_addr_t pctx
,
1130 XHCIEPContext
*epctx
;
1132 trace_usb_xhci_ep_enable(slotid
, epid
);
1133 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1134 assert(epid
>= 1 && epid
<= 31);
1136 slot
= &xhci
->slots
[slotid
-1];
1137 if (slot
->eps
[epid
-1]) {
1138 xhci_disable_ep(xhci
, slotid
, epid
);
1141 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
1142 slot
->eps
[epid
-1] = epctx
;
1143 xhci_init_epctx(epctx
, pctx
, ctx
);
1145 DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) "
1146 "size is %d\n", epid
/2, epid
%2, epctx
->type
, epctx
->max_psize
);
1148 epctx
->mfindex_last
= 0;
1150 epctx
->state
= EP_RUNNING
;
1151 ctx
[0] &= ~EP_STATE_MASK
;
1152 ctx
[0] |= EP_RUNNING
;
1157 static XHCITransfer
*xhci_ep_alloc_xfer(XHCIEPContext
*epctx
,
1160 uint32_t limit
= epctx
->nr_pstreams
+ 16;
1163 if (epctx
->xfer_count
>= limit
) {
1167 xfer
= g_new0(XHCITransfer
, 1);
1168 xfer
->epctx
= epctx
;
1169 xfer
->trbs
= g_new(XHCITRB
, length
);
1170 xfer
->trb_count
= length
;
1171 usb_packet_init(&xfer
->packet
);
1173 QTAILQ_INSERT_TAIL(&epctx
->transfers
, xfer
, next
);
1174 epctx
->xfer_count
++;
1179 static void xhci_ep_free_xfer(XHCITransfer
*xfer
)
1181 QTAILQ_REMOVE(&xfer
->epctx
->transfers
, xfer
, next
);
1182 xfer
->epctx
->xfer_count
--;
1184 usb_packet_cleanup(&xfer
->packet
);
1189 static int xhci_ep_nuke_one_xfer(XHCITransfer
*t
, TRBCCode report
)
1193 if (report
&& (t
->running_async
|| t
->running_retry
)) {
1195 xhci_xfer_report(t
);
1198 if (t
->running_async
) {
1199 usb_cancel_packet(&t
->packet
);
1200 t
->running_async
= 0;
1203 if (t
->running_retry
) {
1205 t
->epctx
->retry
= NULL
;
1206 timer_del(t
->epctx
->kick_timer
);
1208 t
->running_retry
= 0;
1219 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
1220 unsigned int epid
, TRBCCode report
)
1223 XHCIEPContext
*epctx
;
1226 USBEndpoint
*ep
= NULL
;
1227 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1228 assert(epid
>= 1 && epid
<= 31);
1230 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
1232 slot
= &xhci
->slots
[slotid
-1];
1234 if (!slot
->eps
[epid
-1]) {
1238 epctx
= slot
->eps
[epid
-1];
1241 xfer
= QTAILQ_FIRST(&epctx
->transfers
);
1245 killed
+= xhci_ep_nuke_one_xfer(xfer
, report
);
1247 report
= 0; /* Only report once */
1249 xhci_ep_free_xfer(xfer
);
1252 ep
= xhci_epid_to_usbep(epctx
);
1254 usb_device_ep_stopped(ep
->dev
, ep
);
1259 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
1263 XHCIEPContext
*epctx
;
1265 trace_usb_xhci_ep_disable(slotid
, epid
);
1266 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1267 assert(epid
>= 1 && epid
<= 31);
1269 slot
= &xhci
->slots
[slotid
-1];
1271 if (!slot
->eps
[epid
-1]) {
1272 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
1276 xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0);
1278 epctx
= slot
->eps
[epid
-1];
1280 if (epctx
->nr_pstreams
) {
1281 xhci_free_streams(epctx
);
1284 /* only touch guest RAM if we're not resetting the HC */
1285 if (xhci
->dcbaap_low
|| xhci
->dcbaap_high
) {
1286 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_DISABLED
);
1289 timer_free(epctx
->kick_timer
);
1291 slot
->eps
[epid
-1] = NULL
;
1296 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
1300 XHCIEPContext
*epctx
;
1302 trace_usb_xhci_ep_stop(slotid
, epid
);
1303 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1305 if (epid
< 1 || epid
> 31) {
1306 DPRINTF("xhci: bad ep %d\n", epid
);
1307 return CC_TRB_ERROR
;
1310 slot
= &xhci
->slots
[slotid
-1];
1312 if (!slot
->eps
[epid
-1]) {
1313 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1314 return CC_EP_NOT_ENABLED_ERROR
;
1317 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, CC_STOPPED
) > 0) {
1318 DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, "
1319 "data might be lost\n");
1322 epctx
= slot
->eps
[epid
-1];
1324 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1326 if (epctx
->nr_pstreams
) {
1327 xhci_reset_streams(epctx
);
1333 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
1337 XHCIEPContext
*epctx
;
1339 trace_usb_xhci_ep_reset(slotid
, epid
);
1340 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1342 if (epid
< 1 || epid
> 31) {
1343 DPRINTF("xhci: bad ep %d\n", epid
);
1344 return CC_TRB_ERROR
;
1347 slot
= &xhci
->slots
[slotid
-1];
1349 if (!slot
->eps
[epid
-1]) {
1350 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1351 return CC_EP_NOT_ENABLED_ERROR
;
1354 epctx
= slot
->eps
[epid
-1];
1356 if (epctx
->state
!= EP_HALTED
) {
1357 DPRINTF("xhci: reset EP while EP %d not halted (%d)\n",
1358 epid
, epctx
->state
);
1359 return CC_CONTEXT_STATE_ERROR
;
1362 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0) > 0) {
1363 DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, "
1364 "data might be lost\n");
1367 if (!xhci
->slots
[slotid
-1].uport
||
1368 !xhci
->slots
[slotid
-1].uport
->dev
||
1369 !xhci
->slots
[slotid
-1].uport
->dev
->attached
) {
1370 return CC_USB_TRANSACTION_ERROR
;
1373 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1375 if (epctx
->nr_pstreams
) {
1376 xhci_reset_streams(epctx
);
1382 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1383 unsigned int epid
, unsigned int streamid
,
1387 XHCIEPContext
*epctx
;
1388 XHCIStreamContext
*sctx
;
1391 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1393 if (epid
< 1 || epid
> 31) {
1394 DPRINTF("xhci: bad ep %d\n", epid
);
1395 return CC_TRB_ERROR
;
1398 trace_usb_xhci_ep_set_dequeue(slotid
, epid
, streamid
, pdequeue
);
1399 dequeue
= xhci_mask64(pdequeue
);
1401 slot
= &xhci
->slots
[slotid
-1];
1403 if (!slot
->eps
[epid
-1]) {
1404 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1405 return CC_EP_NOT_ENABLED_ERROR
;
1408 epctx
= slot
->eps
[epid
-1];
1410 if (epctx
->state
!= EP_STOPPED
) {
1411 DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1412 return CC_CONTEXT_STATE_ERROR
;
1415 if (epctx
->nr_pstreams
) {
1417 sctx
= xhci_find_stream(epctx
, streamid
, &err
);
1421 xhci_ring_init(xhci
, &sctx
->ring
, dequeue
& ~0xf);
1422 sctx
->ring
.ccs
= dequeue
& 1;
1425 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1426 epctx
->ring
.ccs
= dequeue
& 1;
1429 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_STOPPED
);
1434 static int xhci_xfer_create_sgl(XHCITransfer
*xfer
, int in_xfer
)
1436 XHCIState
*xhci
= xfer
->epctx
->xhci
;
1439 xfer
->int_req
= false;
1440 pci_dma_sglist_init(&xfer
->sgl
, PCI_DEVICE(xhci
), xfer
->trb_count
);
1441 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1442 XHCITRB
*trb
= &xfer
->trbs
[i
];
1444 unsigned int chunk
= 0;
1446 if (trb
->control
& TRB_TR_IOC
) {
1447 xfer
->int_req
= true;
1450 switch (TRB_TYPE(*trb
)) {
1452 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1453 DPRINTF("xhci: data direction mismatch for TR_DATA\n");
1459 addr
= xhci_mask64(trb
->parameter
);
1460 chunk
= trb
->status
& 0x1ffff;
1461 if (trb
->control
& TRB_TR_IDT
) {
1462 if (chunk
> 8 || in_xfer
) {
1463 DPRINTF("xhci: invalid immediate data TRB\n");
1466 qemu_sglist_add(&xfer
->sgl
, trb
->addr
, chunk
);
1468 qemu_sglist_add(&xfer
->sgl
, addr
, chunk
);
1477 qemu_sglist_destroy(&xfer
->sgl
);
1482 static void xhci_xfer_unmap(XHCITransfer
*xfer
)
1484 usb_packet_unmap(&xfer
->packet
, &xfer
->sgl
);
1485 qemu_sglist_destroy(&xfer
->sgl
);
1488 static void xhci_xfer_report(XHCITransfer
*xfer
)
1494 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1495 XHCIState
*xhci
= xfer
->epctx
->xhci
;
1498 left
= xfer
->packet
.actual_length
;
1500 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1501 XHCITRB
*trb
= &xfer
->trbs
[i
];
1502 unsigned int chunk
= 0;
1504 switch (TRB_TYPE(*trb
)) {
1506 chunk
= trb
->status
& 0x1ffff;
1514 chunk
= trb
->status
& 0x1ffff;
1517 if (xfer
->status
== CC_SUCCESS
) {
1530 if (!reported
&& ((trb
->control
& TRB_TR_IOC
) ||
1531 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)) ||
1532 (xfer
->status
!= CC_SUCCESS
&& left
== 0))) {
1533 event
.slotid
= xfer
->epctx
->slotid
;
1534 event
.epid
= xfer
->epctx
->epid
;
1535 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1537 event
.ptr
= trb
->addr
;
1538 if (xfer
->status
== CC_SUCCESS
) {
1539 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1541 event
.ccode
= xfer
->status
;
1543 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1544 event
.ptr
= trb
->parameter
;
1545 event
.flags
|= TRB_EV_ED
;
1546 event
.length
= edtla
& 0xffffff;
1547 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1550 xhci_event(xhci
, &event
, TRB_INTR(*trb
));
1552 if (xfer
->status
!= CC_SUCCESS
) {
1557 switch (TRB_TYPE(*trb
)) {
1567 static void xhci_stall_ep(XHCITransfer
*xfer
)
1569 XHCIEPContext
*epctx
= xfer
->epctx
;
1570 XHCIState
*xhci
= epctx
->xhci
;
1572 XHCIStreamContext
*sctx
;
1574 if (epctx
->nr_pstreams
) {
1575 sctx
= xhci_find_stream(epctx
, xfer
->streamid
, &err
);
1579 sctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1580 sctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1581 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_HALTED
);
1583 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1584 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1585 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_HALTED
);
1589 static int xhci_setup_packet(XHCITransfer
*xfer
)
1594 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1596 if (xfer
->packet
.ep
) {
1597 ep
= xfer
->packet
.ep
;
1599 ep
= xhci_epid_to_usbep(xfer
->epctx
);
1601 DPRINTF("xhci: slot %d has no device\n",
1602 xfer
->epctx
->slotid
);
1607 xhci_xfer_create_sgl(xfer
, dir
== USB_TOKEN_IN
); /* Also sets int_req */
1608 usb_packet_setup(&xfer
->packet
, dir
, ep
, xfer
->streamid
,
1609 xfer
->trbs
[0].addr
, false, xfer
->int_req
);
1610 usb_packet_map(&xfer
->packet
, &xfer
->sgl
);
1611 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1612 xfer
->packet
.pid
, ep
->dev
->addr
, ep
->nr
);
1616 static int xhci_try_complete_packet(XHCITransfer
*xfer
)
1618 if (xfer
->packet
.status
== USB_RET_ASYNC
) {
1619 trace_usb_xhci_xfer_async(xfer
);
1620 xfer
->running_async
= 1;
1621 xfer
->running_retry
= 0;
1624 } else if (xfer
->packet
.status
== USB_RET_NAK
) {
1625 trace_usb_xhci_xfer_nak(xfer
);
1626 xfer
->running_async
= 0;
1627 xfer
->running_retry
= 1;
1631 xfer
->running_async
= 0;
1632 xfer
->running_retry
= 0;
1634 xhci_xfer_unmap(xfer
);
1637 if (xfer
->packet
.status
== USB_RET_SUCCESS
) {
1638 trace_usb_xhci_xfer_success(xfer
, xfer
->packet
.actual_length
);
1639 xfer
->status
= CC_SUCCESS
;
1640 xhci_xfer_report(xfer
);
1645 trace_usb_xhci_xfer_error(xfer
, xfer
->packet
.status
);
1646 switch (xfer
->packet
.status
) {
1648 case USB_RET_IOERROR
:
1649 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1650 xhci_xfer_report(xfer
);
1651 xhci_stall_ep(xfer
);
1654 xfer
->status
= CC_STALL_ERROR
;
1655 xhci_xfer_report(xfer
);
1656 xhci_stall_ep(xfer
);
1658 case USB_RET_BABBLE
:
1659 xfer
->status
= CC_BABBLE_DETECTED
;
1660 xhci_xfer_report(xfer
);
1661 xhci_stall_ep(xfer
);
1664 DPRINTF("%s: FIXME: status = %d\n", __func__
,
1665 xfer
->packet
.status
);
1666 FIXME("unhandled USB_RET_*");
1671 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1673 XHCITRB
*trb_setup
, *trb_status
;
1674 uint8_t bmRequestType
;
1676 trb_setup
= &xfer
->trbs
[0];
1677 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1679 trace_usb_xhci_xfer_start(xfer
, xfer
->epctx
->slotid
,
1680 xfer
->epctx
->epid
, xfer
->streamid
);
1682 /* at most one Event Data TRB allowed after STATUS */
1683 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1687 /* do some sanity checks */
1688 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1689 DPRINTF("xhci: ep0 first TD not SETUP: %d\n",
1690 TRB_TYPE(*trb_setup
));
1693 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1694 DPRINTF("xhci: ep0 last TD not STATUS: %d\n",
1695 TRB_TYPE(*trb_status
));
1698 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1699 DPRINTF("xhci: Setup TRB doesn't have IDT set\n");
1702 if ((trb_setup
->status
& 0x1ffff) != 8) {
1703 DPRINTF("xhci: Setup TRB has bad length (%d)\n",
1704 (trb_setup
->status
& 0x1ffff));
1708 bmRequestType
= trb_setup
->parameter
;
1710 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1711 xfer
->iso_xfer
= false;
1712 xfer
->timed_xfer
= false;
1714 if (xhci_setup_packet(xfer
) < 0) {
1717 xfer
->packet
.parameter
= trb_setup
->parameter
;
1719 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1720 xhci_try_complete_packet(xfer
);
1724 static void xhci_calc_intr_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1725 XHCIEPContext
*epctx
, uint64_t mfindex
)
1727 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1728 ~(epctx
->interval
-1));
1729 uint64_t kick
= epctx
->mfindex_last
+ epctx
->interval
;
1731 assert(epctx
->interval
!= 0);
1732 xfer
->mfindex_kick
= MAX(asap
, kick
);
1735 static void xhci_calc_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1736 XHCIEPContext
*epctx
, uint64_t mfindex
)
1738 if (xfer
->trbs
[0].control
& TRB_TR_SIA
) {
1739 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1740 ~(epctx
->interval
-1));
1741 if (asap
>= epctx
->mfindex_last
&&
1742 asap
<= epctx
->mfindex_last
+ epctx
->interval
* 4) {
1743 xfer
->mfindex_kick
= epctx
->mfindex_last
+ epctx
->interval
;
1745 xfer
->mfindex_kick
= asap
;
1748 xfer
->mfindex_kick
= ((xfer
->trbs
[0].control
>> TRB_TR_FRAMEID_SHIFT
)
1749 & TRB_TR_FRAMEID_MASK
) << 3;
1750 xfer
->mfindex_kick
|= mfindex
& ~0x3fff;
1751 if (xfer
->mfindex_kick
+ 0x100 < mfindex
) {
1752 xfer
->mfindex_kick
+= 0x4000;
1757 static void xhci_check_intr_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1758 XHCIEPContext
*epctx
, uint64_t mfindex
)
1760 if (xfer
->mfindex_kick
> mfindex
) {
1761 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
1762 (xfer
->mfindex_kick
- mfindex
) * 125000);
1763 xfer
->running_retry
= 1;
1765 epctx
->mfindex_last
= xfer
->mfindex_kick
;
1766 timer_del(epctx
->kick_timer
);
1767 xfer
->running_retry
= 0;
1772 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1776 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", epctx
->slotid
, epctx
->epid
);
1778 xfer
->in_xfer
= epctx
->type
>>2;
1780 switch(epctx
->type
) {
1784 xfer
->iso_xfer
= false;
1785 xfer
->timed_xfer
= true;
1786 mfindex
= xhci_mfindex_get(xhci
);
1787 xhci_calc_intr_kick(xhci
, xfer
, epctx
, mfindex
);
1788 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1789 if (xfer
->running_retry
) {
1796 xfer
->iso_xfer
= false;
1797 xfer
->timed_xfer
= false;
1802 xfer
->iso_xfer
= true;
1803 xfer
->timed_xfer
= true;
1804 mfindex
= xhci_mfindex_get(xhci
);
1805 xhci_calc_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1806 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1807 if (xfer
->running_retry
) {
1812 trace_usb_xhci_unimplemented("endpoint type", epctx
->type
);
1816 if (xhci_setup_packet(xfer
) < 0) {
1819 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1820 xhci_try_complete_packet(xfer
);
1824 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1826 trace_usb_xhci_xfer_start(xfer
, xfer
->epctx
->slotid
,
1827 xfer
->epctx
->epid
, xfer
->streamid
);
1828 return xhci_submit(xhci
, xfer
, epctx
);
1831 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
1832 unsigned int epid
, unsigned int streamid
)
1834 XHCIEPContext
*epctx
;
1836 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1837 assert(epid
>= 1 && epid
<= 31);
1839 if (!xhci
->slots
[slotid
-1].enabled
) {
1840 DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1843 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1845 DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1850 if (epctx
->kick_active
) {
1853 xhci_kick_epctx(epctx
, streamid
);
1856 static void xhci_kick_epctx(XHCIEPContext
*epctx
, unsigned int streamid
)
1858 XHCIState
*xhci
= epctx
->xhci
;
1859 XHCIStreamContext
*stctx
= NULL
;
1862 USBEndpoint
*ep
= NULL
;
1864 unsigned int count
= 0;
1868 trace_usb_xhci_ep_kick(epctx
->slotid
, epctx
->epid
, streamid
);
1869 assert(!epctx
->kick_active
);
1871 /* If the device has been detached, but the guest has not noticed this
1872 yet the 2 above checks will succeed, but we must NOT continue */
1873 if (!xhci
->slots
[epctx
->slotid
- 1].uport
||
1874 !xhci
->slots
[epctx
->slotid
- 1].uport
->dev
||
1875 !xhci
->slots
[epctx
->slotid
- 1].uport
->dev
->attached
) {
1880 XHCITransfer
*xfer
= epctx
->retry
;
1882 trace_usb_xhci_xfer_retry(xfer
);
1883 assert(xfer
->running_retry
);
1884 if (xfer
->timed_xfer
) {
1885 /* time to kick the transfer? */
1886 mfindex
= xhci_mfindex_get(xhci
);
1887 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1888 if (xfer
->running_retry
) {
1891 xfer
->timed_xfer
= 0;
1892 xfer
->running_retry
= 1;
1894 if (xfer
->iso_xfer
) {
1895 /* retry iso transfer */
1896 if (xhci_setup_packet(xfer
) < 0) {
1899 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1900 assert(xfer
->packet
.status
!= USB_RET_NAK
);
1901 xhci_try_complete_packet(xfer
);
1903 /* retry nak'ed transfer */
1904 if (xhci_setup_packet(xfer
) < 0) {
1907 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1908 if (xfer
->packet
.status
== USB_RET_NAK
) {
1911 xhci_try_complete_packet(xfer
);
1913 assert(!xfer
->running_retry
);
1914 if (xfer
->complete
) {
1915 /* update ring dequeue ptr */
1916 xhci_set_ep_state(xhci
, epctx
, stctx
, epctx
->state
);
1917 xhci_ep_free_xfer(epctx
->retry
);
1919 epctx
->retry
= NULL
;
1922 if (epctx
->state
== EP_HALTED
) {
1923 DPRINTF("xhci: ep halted, not running schedule\n");
1928 if (epctx
->nr_pstreams
) {
1930 stctx
= xhci_find_stream(epctx
, streamid
, &err
);
1931 if (stctx
== NULL
) {
1934 ring
= &stctx
->ring
;
1935 xhci_set_ep_state(xhci
, epctx
, stctx
, EP_RUNNING
);
1937 ring
= &epctx
->ring
;
1939 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_RUNNING
);
1941 assert(ring
->dequeue
!= 0);
1943 epctx
->kick_active
++;
1945 length
= xhci_ring_chain_length(xhci
, ring
);
1949 xfer
= xhci_ep_alloc_xfer(epctx
, length
);
1954 for (i
= 0; i
< length
; i
++) {
1956 type
= xhci_ring_fetch(xhci
, ring
, &xfer
->trbs
[i
], NULL
);
1959 xhci_ep_free_xfer(xfer
);
1960 epctx
->kick_active
--;
1964 xfer
->streamid
= streamid
;
1966 if (epctx
->epid
== 1) {
1967 xhci_fire_ctl_transfer(xhci
, xfer
);
1969 xhci_fire_transfer(xhci
, xfer
, epctx
);
1971 if (xfer
->complete
) {
1972 /* update ring dequeue ptr */
1973 xhci_set_ep_state(xhci
, epctx
, stctx
, epctx
->state
);
1974 xhci_ep_free_xfer(xfer
);
1978 if (epctx
->state
== EP_HALTED
) {
1981 if (xfer
!= NULL
&& xfer
->running_retry
) {
1982 DPRINTF("xhci: xfer nacked, stopping schedule\n");
1983 epctx
->retry
= xfer
;
1986 if (count
++ > TRANSFER_LIMIT
) {
1987 trace_usb_xhci_enforced_limit("transfers");
1991 epctx
->kick_active
--;
1993 ep
= xhci_epid_to_usbep(epctx
);
1995 usb_device_flush_ep_queue(ep
->dev
, ep
);
1999 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
2001 trace_usb_xhci_slot_enable(slotid
);
2002 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2003 xhci
->slots
[slotid
-1].enabled
= 1;
2004 xhci
->slots
[slotid
-1].uport
= NULL
;
2005 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
2010 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
2014 trace_usb_xhci_slot_disable(slotid
);
2015 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2017 for (i
= 1; i
<= 31; i
++) {
2018 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2019 xhci_disable_ep(xhci
, slotid
, i
);
2023 xhci
->slots
[slotid
-1].enabled
= 0;
2024 xhci
->slots
[slotid
-1].addressed
= 0;
2025 xhci
->slots
[slotid
-1].uport
= NULL
;
2029 static USBPort
*xhci_lookup_uport(XHCIState
*xhci
, uint32_t *slot_ctx
)
2035 port
= (slot_ctx
[1]>>16) & 0xFF;
2036 if (port
< 1 || port
> xhci
->numports
) {
2039 port
= xhci
->ports
[port
-1].uport
->index
+1;
2040 pos
= snprintf(path
, sizeof(path
), "%d", port
);
2041 for (i
= 0; i
< 5; i
++) {
2042 port
= (slot_ctx
[0] >> 4*i
) & 0x0f;
2046 pos
+= snprintf(path
+ pos
, sizeof(path
) - pos
, ".%d", port
);
2049 QTAILQ_FOREACH(uport
, &xhci
->bus
.used
, next
) {
2050 if (strcmp(uport
->path
, path
) == 0) {
2057 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
2058 uint64_t pictx
, bool bsr
)
2063 dma_addr_t ictx
, octx
, dcbaap
;
2065 uint32_t ictl_ctx
[2];
2066 uint32_t slot_ctx
[4];
2067 uint32_t ep0_ctx
[5];
2071 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2073 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
2074 poctx
= ldq_le_pci_dma(PCI_DEVICE(xhci
), dcbaap
+ 8 * slotid
);
2075 ictx
= xhci_mask64(pictx
);
2076 octx
= xhci_mask64(poctx
);
2078 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2079 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2081 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2083 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
2084 DPRINTF("xhci: invalid input context control %08x %08x\n",
2085 ictl_ctx
[0], ictl_ctx
[1]);
2086 return CC_TRB_ERROR
;
2089 xhci_dma_read_u32s(xhci
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
2090 xhci_dma_read_u32s(xhci
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
2092 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2093 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2095 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2096 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2098 uport
= xhci_lookup_uport(xhci
, slot_ctx
);
2099 if (uport
== NULL
) {
2100 DPRINTF("xhci: port not found\n");
2101 return CC_TRB_ERROR
;
2103 trace_usb_xhci_slot_address(slotid
, uport
->path
);
2106 if (!dev
|| !dev
->attached
) {
2107 DPRINTF("xhci: port %s not connected\n", uport
->path
);
2108 return CC_USB_TRANSACTION_ERROR
;
2111 for (i
= 0; i
< xhci
->numslots
; i
++) {
2112 if (i
== slotid
-1) {
2115 if (xhci
->slots
[i
].uport
== uport
) {
2116 DPRINTF("xhci: port %s already assigned to slot %d\n",
2118 return CC_TRB_ERROR
;
2122 slot
= &xhci
->slots
[slotid
-1];
2123 slot
->uport
= uport
;
2126 /* Make sure device is in USB_STATE_DEFAULT state */
2127 usb_device_reset(dev
);
2129 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2134 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slotid
;
2135 memset(&p
, 0, sizeof(p
));
2136 usb_packet_addbuf(&p
, buf
, sizeof(buf
));
2137 usb_packet_setup(&p
, USB_TOKEN_OUT
,
2138 usb_ep_get(dev
, USB_TOKEN_OUT
, 0), 0,
2140 usb_device_handle_control(dev
, &p
,
2141 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
2142 slotid
, 0, 0, NULL
);
2143 assert(p
.status
!= USB_RET_ASYNC
);
2146 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
2148 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2149 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2150 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2151 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2153 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2154 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2156 xhci
->slots
[slotid
-1].addressed
= 1;
2161 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
2162 uint64_t pictx
, bool dc
)
2164 dma_addr_t ictx
, octx
;
2165 uint32_t ictl_ctx
[2];
2166 uint32_t slot_ctx
[4];
2167 uint32_t islot_ctx
[4];
2172 trace_usb_xhci_slot_configure(slotid
);
2173 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2175 ictx
= xhci_mask64(pictx
);
2176 octx
= xhci
->slots
[slotid
-1].ctx
;
2178 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2179 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2182 for (i
= 2; i
<= 31; i
++) {
2183 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2184 xhci_disable_ep(xhci
, slotid
, i
);
2188 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2189 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2190 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
2191 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2192 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2193 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2198 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2200 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
2201 DPRINTF("xhci: invalid input context control %08x %08x\n",
2202 ictl_ctx
[0], ictl_ctx
[1]);
2203 return CC_TRB_ERROR
;
2206 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2207 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2209 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
2210 DPRINTF("xhci: invalid slot state %08x\n", slot_ctx
[3]);
2211 return CC_CONTEXT_STATE_ERROR
;
2214 xhci_free_device_streams(xhci
, slotid
, ictl_ctx
[0] | ictl_ctx
[1]);
2216 for (i
= 2; i
<= 31; i
++) {
2217 if (ictl_ctx
[0] & (1<<i
)) {
2218 xhci_disable_ep(xhci
, slotid
, i
);
2220 if (ictl_ctx
[1] & (1<<i
)) {
2221 xhci_dma_read_u32s(xhci
, ictx
+32+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2222 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
2223 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2224 ep_ctx
[3], ep_ctx
[4]);
2225 xhci_disable_ep(xhci
, slotid
, i
);
2226 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
2227 if (res
!= CC_SUCCESS
) {
2230 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
2231 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2232 ep_ctx
[3], ep_ctx
[4]);
2233 xhci_dma_write_u32s(xhci
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2237 res
= xhci_alloc_device_streams(xhci
, slotid
, ictl_ctx
[1]);
2238 if (res
!= CC_SUCCESS
) {
2239 for (i
= 2; i
<= 31; i
++) {
2240 if (ictl_ctx
[1] & (1u << i
)) {
2241 xhci_disable_ep(xhci
, slotid
, i
);
2247 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2248 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
2249 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
2250 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
2251 SLOT_CONTEXT_ENTRIES_SHIFT
);
2252 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2253 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2255 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2261 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
2264 dma_addr_t ictx
, octx
;
2265 uint32_t ictl_ctx
[2];
2266 uint32_t iep0_ctx
[5];
2267 uint32_t ep0_ctx
[5];
2268 uint32_t islot_ctx
[4];
2269 uint32_t slot_ctx
[4];
2271 trace_usb_xhci_slot_evaluate(slotid
);
2272 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2274 ictx
= xhci_mask64(pictx
);
2275 octx
= xhci
->slots
[slotid
-1].ctx
;
2277 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2278 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2280 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2282 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
2283 DPRINTF("xhci: invalid input context control %08x %08x\n",
2284 ictl_ctx
[0], ictl_ctx
[1]);
2285 return CC_TRB_ERROR
;
2288 if (ictl_ctx
[1] & 0x1) {
2289 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2291 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2292 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
2294 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2296 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
2297 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
2298 slot_ctx
[2] &= ~0xFF00000; /* interrupter target */
2299 slot_ctx
[2] |= islot_ctx
[2] & 0xFF000000;
2301 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2302 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2304 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2307 if (ictl_ctx
[1] & 0x2) {
2308 xhci_dma_read_u32s(xhci
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
2310 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2311 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
2312 iep0_ctx
[3], iep0_ctx
[4]);
2314 xhci_dma_read_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2316 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
2317 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
2319 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2320 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2322 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2328 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
2330 uint32_t slot_ctx
[4];
2334 trace_usb_xhci_slot_reset(slotid
);
2335 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2337 octx
= xhci
->slots
[slotid
-1].ctx
;
2339 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2341 for (i
= 2; i
<= 31; i
++) {
2342 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2343 xhci_disable_ep(xhci
, slotid
, i
);
2347 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2348 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2349 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2350 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2351 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2352 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2357 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
2359 unsigned int slotid
;
2360 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
2361 if (slotid
< 1 || slotid
> xhci
->numslots
) {
2362 DPRINTF("xhci: bad slot id %d\n", slotid
);
2363 event
->ccode
= CC_TRB_ERROR
;
2365 } else if (!xhci
->slots
[slotid
-1].enabled
) {
2366 DPRINTF("xhci: slot id %d not enabled\n", slotid
);
2367 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
2373 /* cleanup slot state on usb device detach */
2374 static void xhci_detach_slot(XHCIState
*xhci
, USBPort
*uport
)
2378 for (slot
= 0; slot
< xhci
->numslots
; slot
++) {
2379 if (xhci
->slots
[slot
].uport
== uport
) {
2383 if (slot
== xhci
->numslots
) {
2387 for (ep
= 0; ep
< 31; ep
++) {
2388 if (xhci
->slots
[slot
].eps
[ep
]) {
2389 xhci_ep_nuke_xfers(xhci
, slot
+ 1, ep
+ 1, 0);
2392 xhci
->slots
[slot
].uport
= NULL
;
2395 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
2398 uint8_t bw_ctx
[xhci
->numports
+1];
2400 DPRINTF("xhci_get_port_bandwidth()\n");
2402 ctx
= xhci_mask64(pctx
);
2404 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
2406 /* TODO: actually implement real values here */
2408 memset(&bw_ctx
[1], 80, xhci
->numports
); /* 80% */
2409 pci_dma_write(PCI_DEVICE(xhci
), ctx
, bw_ctx
, sizeof(bw_ctx
));
2414 static uint32_t rotl(uint32_t v
, unsigned count
)
2417 return (v
<< count
) | (v
>> (32 - count
));
2421 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2424 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2425 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2426 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2430 static void xhci_process_commands(XHCIState
*xhci
)
2434 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2436 unsigned int i
, slotid
= 0, count
= 0;
2438 DPRINTF("xhci_process_commands()\n");
2439 if (!xhci_running(xhci
)) {
2440 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2444 xhci
->crcr_low
|= CRCR_CRR
;
2446 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2449 case CR_ENABLE_SLOT
:
2450 for (i
= 0; i
< xhci
->numslots
; i
++) {
2451 if (!xhci
->slots
[i
].enabled
) {
2455 if (i
>= xhci
->numslots
) {
2456 DPRINTF("xhci: no device slots available\n");
2457 event
.ccode
= CC_NO_SLOTS_ERROR
;
2460 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2463 case CR_DISABLE_SLOT
:
2464 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2466 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2469 case CR_ADDRESS_DEVICE
:
2470 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2472 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2473 trb
.control
& TRB_CR_BSR
);
2476 case CR_CONFIGURE_ENDPOINT
:
2477 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2479 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2480 trb
.control
& TRB_CR_DC
);
2483 case CR_EVALUATE_CONTEXT
:
2484 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2486 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2489 case CR_STOP_ENDPOINT
:
2490 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2492 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2494 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2497 case CR_RESET_ENDPOINT
:
2498 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2500 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2502 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2505 case CR_SET_TR_DEQUEUE
:
2506 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2508 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2510 unsigned int streamid
= (trb
.status
>> 16) & 0xffff;
2511 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
,
2516 case CR_RESET_DEVICE
:
2517 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2519 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2522 case CR_GET_PORT_BANDWIDTH
:
2523 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2525 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2526 if (xhci
->nec_quirks
) {
2527 event
.type
= 48; /* NEC reply */
2528 event
.length
= 0x3025;
2530 event
.ccode
= CC_TRB_ERROR
;
2533 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2534 if (xhci
->nec_quirks
) {
2535 uint32_t chi
= trb
.parameter
>> 32;
2536 uint32_t clo
= trb
.parameter
;
2537 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2538 event
.length
= val
& 0xFFFF;
2539 event
.epid
= val
>> 16;
2541 event
.type
= 48; /* NEC reply */
2543 event
.ccode
= CC_TRB_ERROR
;
2547 trace_usb_xhci_unimplemented("command", type
);
2548 event
.ccode
= CC_TRB_ERROR
;
2551 event
.slotid
= slotid
;
2552 xhci_event(xhci
, &event
, 0);
2554 if (count
++ > COMMAND_LIMIT
) {
2555 trace_usb_xhci_enforced_limit("commands");
2561 static bool xhci_port_have_device(XHCIPort
*port
)
2563 if (!port
->uport
->dev
|| !port
->uport
->dev
->attached
) {
2564 return false; /* no device present */
2566 if (!((1 << port
->uport
->dev
->speed
) & port
->speedmask
)) {
2567 return false; /* speed mismatch */
2572 static void xhci_port_notify(XHCIPort
*port
, uint32_t bits
)
2574 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
,
2575 port
->portnr
<< 24 };
2577 if ((port
->portsc
& bits
) == bits
) {
2580 trace_usb_xhci_port_notify(port
->portnr
, bits
);
2581 port
->portsc
|= bits
;
2582 if (!xhci_running(port
->xhci
)) {
2585 xhci_event(port
->xhci
, &ev
, 0);
2588 static void xhci_port_update(XHCIPort
*port
, int is_detach
)
2590 uint32_t pls
= PLS_RX_DETECT
;
2592 port
->portsc
= PORTSC_PP
;
2593 if (!is_detach
&& xhci_port_have_device(port
)) {
2594 port
->portsc
|= PORTSC_CCS
;
2595 switch (port
->uport
->dev
->speed
) {
2597 port
->portsc
|= PORTSC_SPEED_LOW
;
2600 case USB_SPEED_FULL
:
2601 port
->portsc
|= PORTSC_SPEED_FULL
;
2604 case USB_SPEED_HIGH
:
2605 port
->portsc
|= PORTSC_SPEED_HIGH
;
2608 case USB_SPEED_SUPER
:
2609 port
->portsc
|= PORTSC_SPEED_SUPER
;
2610 port
->portsc
|= PORTSC_PED
;
2615 set_field(&port
->portsc
, pls
, PORTSC_PLS
);
2616 trace_usb_xhci_port_link(port
->portnr
, pls
);
2617 xhci_port_notify(port
, PORTSC_CSC
);
2620 static void xhci_port_reset(XHCIPort
*port
, bool warm_reset
)
2622 trace_usb_xhci_port_reset(port
->portnr
, warm_reset
);
2624 if (!xhci_port_have_device(port
)) {
2628 usb_device_reset(port
->uport
->dev
);
2630 switch (port
->uport
->dev
->speed
) {
2631 case USB_SPEED_SUPER
:
2633 port
->portsc
|= PORTSC_WRC
;
2637 case USB_SPEED_FULL
:
2638 case USB_SPEED_HIGH
:
2639 set_field(&port
->portsc
, PLS_U0
, PORTSC_PLS
);
2640 trace_usb_xhci_port_link(port
->portnr
, PLS_U0
);
2641 port
->portsc
|= PORTSC_PED
;
2645 port
->portsc
&= ~PORTSC_PR
;
2646 xhci_port_notify(port
, PORTSC_PRC
);
2649 static void xhci_reset(DeviceState
*dev
)
2651 XHCIState
*xhci
= XHCI(dev
);
2654 trace_usb_xhci_reset();
2655 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2656 DPRINTF("xhci: reset while running!\n");
2660 xhci
->usbsts
= USBSTS_HCH
;
2663 xhci
->crcr_high
= 0;
2664 xhci
->dcbaap_low
= 0;
2665 xhci
->dcbaap_high
= 0;
2668 for (i
= 0; i
< xhci
->numslots
; i
++) {
2669 xhci_disable_slot(xhci
, i
+1);
2672 for (i
= 0; i
< xhci
->numports
; i
++) {
2673 xhci_port_update(xhci
->ports
+ i
, 0);
2676 for (i
= 0; i
< xhci
->numintrs
; i
++) {
2677 xhci
->intr
[i
].iman
= 0;
2678 xhci
->intr
[i
].imod
= 0;
2679 xhci
->intr
[i
].erstsz
= 0;
2680 xhci
->intr
[i
].erstba_low
= 0;
2681 xhci
->intr
[i
].erstba_high
= 0;
2682 xhci
->intr
[i
].erdp_low
= 0;
2683 xhci
->intr
[i
].erdp_high
= 0;
2684 xhci
->intr
[i
].msix_used
= 0;
2686 xhci
->intr
[i
].er_ep_idx
= 0;
2687 xhci
->intr
[i
].er_pcs
= 1;
2688 xhci
->intr
[i
].ev_buffer_put
= 0;
2689 xhci
->intr
[i
].ev_buffer_get
= 0;
2692 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2693 xhci_mfwrap_update(xhci
);
2696 static uint64_t xhci_cap_read(void *ptr
, hwaddr reg
, unsigned size
)
2698 XHCIState
*xhci
= ptr
;
2702 case 0x00: /* HCIVERSION, CAPLENGTH */
2703 ret
= 0x01000000 | LEN_CAP
;
2705 case 0x04: /* HCSPARAMS 1 */
2706 ret
= ((xhci
->numports_2
+xhci
->numports_3
)<<24)
2707 | (xhci
->numintrs
<<8) | xhci
->numslots
;
2709 case 0x08: /* HCSPARAMS 2 */
2712 case 0x0c: /* HCSPARAMS 3 */
2715 case 0x10: /* HCCPARAMS */
2716 if (sizeof(dma_addr_t
) == 4) {
2717 ret
= 0x00080000 | (xhci
->max_pstreams_mask
<< 12);
2719 ret
= 0x00080001 | (xhci
->max_pstreams_mask
<< 12);
2722 case 0x14: /* DBOFF */
2725 case 0x18: /* RTSOFF */
2729 /* extended capabilities */
2730 case 0x20: /* Supported Protocol:00 */
2731 ret
= 0x02000402; /* USB 2.0 */
2733 case 0x24: /* Supported Protocol:04 */
2734 ret
= 0x20425355; /* "USB " */
2736 case 0x28: /* Supported Protocol:08 */
2737 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2738 ret
= (xhci
->numports_2
<<8) | (xhci
->numports_3
+1);
2740 ret
= (xhci
->numports_2
<<8) | 1;
2743 case 0x2c: /* Supported Protocol:0c */
2744 ret
= 0x00000000; /* reserved */
2746 case 0x30: /* Supported Protocol:00 */
2747 ret
= 0x03000002; /* USB 3.0 */
2749 case 0x34: /* Supported Protocol:04 */
2750 ret
= 0x20425355; /* "USB " */
2752 case 0x38: /* Supported Protocol:08 */
2753 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2754 ret
= (xhci
->numports_3
<<8) | 1;
2756 ret
= (xhci
->numports_3
<<8) | (xhci
->numports_2
+1);
2759 case 0x3c: /* Supported Protocol:0c */
2760 ret
= 0x00000000; /* reserved */
2763 trace_usb_xhci_unimplemented("cap read", reg
);
2767 trace_usb_xhci_cap_read(reg
, ret
);
2771 static uint64_t xhci_port_read(void *ptr
, hwaddr reg
, unsigned size
)
2773 XHCIPort
*port
= ptr
;
2777 case 0x00: /* PORTSC */
2780 case 0x04: /* PORTPMSC */
2781 case 0x08: /* PORTLI */
2784 case 0x0c: /* reserved */
2786 trace_usb_xhci_unimplemented("port read", reg
);
2790 trace_usb_xhci_port_read(port
->portnr
, reg
, ret
);
2794 static void xhci_port_write(void *ptr
, hwaddr reg
,
2795 uint64_t val
, unsigned size
)
2797 XHCIPort
*port
= ptr
;
2798 uint32_t portsc
, notify
;
2800 trace_usb_xhci_port_write(port
->portnr
, reg
, val
);
2803 case 0x00: /* PORTSC */
2804 /* write-1-to-start bits */
2805 if (val
& PORTSC_WPR
) {
2806 xhci_port_reset(port
, true);
2809 if (val
& PORTSC_PR
) {
2810 xhci_port_reset(port
, false);
2814 portsc
= port
->portsc
;
2816 /* write-1-to-clear bits*/
2817 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2818 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2819 if (val
& PORTSC_LWS
) {
2820 /* overwrite PLS only when LWS=1 */
2821 uint32_t old_pls
= get_field(port
->portsc
, PORTSC_PLS
);
2822 uint32_t new_pls
= get_field(val
, PORTSC_PLS
);
2825 if (old_pls
!= PLS_U0
) {
2826 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2827 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2828 notify
= PORTSC_PLC
;
2832 if (old_pls
< PLS_U3
) {
2833 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2834 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2838 /* windows does this for some reason, don't spam stderr */
2841 DPRINTF("%s: ignore pls write (old %d, new %d)\n",
2842 __func__
, old_pls
, new_pls
);
2846 /* read/write bits */
2847 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2848 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2849 port
->portsc
= portsc
;
2851 xhci_port_notify(port
, notify
);
2854 case 0x04: /* PORTPMSC */
2855 case 0x08: /* PORTLI */
2857 trace_usb_xhci_unimplemented("port write", reg
);
2861 static uint64_t xhci_oper_read(void *ptr
, hwaddr reg
, unsigned size
)
2863 XHCIState
*xhci
= ptr
;
2867 case 0x00: /* USBCMD */
2870 case 0x04: /* USBSTS */
2873 case 0x08: /* PAGESIZE */
2876 case 0x14: /* DNCTRL */
2879 case 0x18: /* CRCR low */
2880 ret
= xhci
->crcr_low
& ~0xe;
2882 case 0x1c: /* CRCR high */
2883 ret
= xhci
->crcr_high
;
2885 case 0x30: /* DCBAAP low */
2886 ret
= xhci
->dcbaap_low
;
2888 case 0x34: /* DCBAAP high */
2889 ret
= xhci
->dcbaap_high
;
2891 case 0x38: /* CONFIG */
2895 trace_usb_xhci_unimplemented("oper read", reg
);
2899 trace_usb_xhci_oper_read(reg
, ret
);
2903 static void xhci_oper_write(void *ptr
, hwaddr reg
,
2904 uint64_t val
, unsigned size
)
2906 XHCIState
*xhci
= ptr
;
2907 DeviceState
*d
= DEVICE(ptr
);
2909 trace_usb_xhci_oper_write(reg
, val
);
2912 case 0x00: /* USBCMD */
2913 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
2915 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
2918 if (val
& USBCMD_CSS
) {
2920 xhci
->usbsts
&= ~USBSTS_SRE
;
2922 if (val
& USBCMD_CRS
) {
2924 xhci
->usbsts
|= USBSTS_SRE
;
2926 xhci
->usbcmd
= val
& 0xc0f;
2927 xhci_mfwrap_update(xhci
);
2928 if (val
& USBCMD_HCRST
) {
2931 xhci_intx_update(xhci
);
2934 case 0x04: /* USBSTS */
2935 /* these bits are write-1-to-clear */
2936 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
2937 xhci_intx_update(xhci
);
2940 case 0x14: /* DNCTRL */
2941 xhci
->dnctrl
= val
& 0xffff;
2943 case 0x18: /* CRCR low */
2944 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
2946 case 0x1c: /* CRCR high */
2947 xhci
->crcr_high
= val
;
2948 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
2949 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
2950 xhci
->crcr_low
&= ~CRCR_CRR
;
2951 xhci_event(xhci
, &event
, 0);
2952 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
2954 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
2955 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
2957 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
2959 case 0x30: /* DCBAAP low */
2960 xhci
->dcbaap_low
= val
& 0xffffffc0;
2962 case 0x34: /* DCBAAP high */
2963 xhci
->dcbaap_high
= val
;
2965 case 0x38: /* CONFIG */
2966 xhci
->config
= val
& 0xff;
2969 trace_usb_xhci_unimplemented("oper write", reg
);
2973 static uint64_t xhci_runtime_read(void *ptr
, hwaddr reg
,
2976 XHCIState
*xhci
= ptr
;
2981 case 0x00: /* MFINDEX */
2982 ret
= xhci_mfindex_get(xhci
) & 0x3fff;
2985 trace_usb_xhci_unimplemented("runtime read", reg
);
2989 int v
= (reg
- 0x20) / 0x20;
2990 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
2991 switch (reg
& 0x1f) {
2992 case 0x00: /* IMAN */
2995 case 0x04: /* IMOD */
2998 case 0x08: /* ERSTSZ */
3001 case 0x10: /* ERSTBA low */
3002 ret
= intr
->erstba_low
;
3004 case 0x14: /* ERSTBA high */
3005 ret
= intr
->erstba_high
;
3007 case 0x18: /* ERDP low */
3008 ret
= intr
->erdp_low
;
3010 case 0x1c: /* ERDP high */
3011 ret
= intr
->erdp_high
;
3016 trace_usb_xhci_runtime_read(reg
, ret
);
3020 static void xhci_runtime_write(void *ptr
, hwaddr reg
,
3021 uint64_t val
, unsigned size
)
3023 XHCIState
*xhci
= ptr
;
3024 int v
= (reg
- 0x20) / 0x20;
3025 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
3026 trace_usb_xhci_runtime_write(reg
, val
);
3029 trace_usb_xhci_unimplemented("runtime write", reg
);
3033 switch (reg
& 0x1f) {
3034 case 0x00: /* IMAN */
3035 if (val
& IMAN_IP
) {
3036 intr
->iman
&= ~IMAN_IP
;
3038 intr
->iman
&= ~IMAN_IE
;
3039 intr
->iman
|= val
& IMAN_IE
;
3041 xhci_intx_update(xhci
);
3043 xhci_msix_update(xhci
, v
);
3045 case 0x04: /* IMOD */
3048 case 0x08: /* ERSTSZ */
3049 intr
->erstsz
= val
& 0xffff;
3051 case 0x10: /* ERSTBA low */
3052 if (xhci
->nec_quirks
) {
3053 /* NEC driver bug: it doesn't align this to 64 bytes */
3054 intr
->erstba_low
= val
& 0xfffffff0;
3056 intr
->erstba_low
= val
& 0xffffffc0;
3059 case 0x14: /* ERSTBA high */
3060 intr
->erstba_high
= val
;
3061 xhci_er_reset(xhci
, v
);
3063 case 0x18: /* ERDP low */
3064 if (val
& ERDP_EHB
) {
3065 intr
->erdp_low
&= ~ERDP_EHB
;
3067 intr
->erdp_low
= (val
& ~ERDP_EHB
) | (intr
->erdp_low
& ERDP_EHB
);
3068 if (val
& ERDP_EHB
) {
3069 dma_addr_t erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
3070 unsigned int dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
3071 if (erdp
>= intr
->er_start
&&
3072 erdp
< (intr
->er_start
+ TRB_SIZE
* intr
->er_size
) &&
3073 dp_idx
!= intr
->er_ep_idx
) {
3074 xhci_intr_raise(xhci
, v
);
3078 case 0x1c: /* ERDP high */
3079 intr
->erdp_high
= val
;
3082 trace_usb_xhci_unimplemented("oper write", reg
);
3086 static uint64_t xhci_doorbell_read(void *ptr
, hwaddr reg
,
3089 /* doorbells always read as 0 */
3090 trace_usb_xhci_doorbell_read(reg
, 0);
3094 static void xhci_doorbell_write(void *ptr
, hwaddr reg
,
3095 uint64_t val
, unsigned size
)
3097 XHCIState
*xhci
= ptr
;
3098 unsigned int epid
, streamid
;
3100 trace_usb_xhci_doorbell_write(reg
, val
);
3102 if (!xhci_running(xhci
)) {
3103 DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
3111 xhci_process_commands(xhci
);
3113 DPRINTF("xhci: bad doorbell 0 write: 0x%x\n",
3118 streamid
= (val
>> 16) & 0xffff;
3119 if (reg
> xhci
->numslots
) {
3120 DPRINTF("xhci: bad doorbell %d\n", (int)reg
);
3121 } else if (epid
> 31) {
3122 DPRINTF("xhci: bad doorbell %d write: 0x%x\n",
3123 (int)reg
, (uint32_t)val
);
3125 xhci_kick_ep(xhci
, reg
, epid
, streamid
);
3130 static void xhci_cap_write(void *opaque
, hwaddr addr
, uint64_t val
,
3136 static const MemoryRegionOps xhci_cap_ops
= {
3137 .read
= xhci_cap_read
,
3138 .write
= xhci_cap_write
,
3139 .valid
.min_access_size
= 1,
3140 .valid
.max_access_size
= 4,
3141 .impl
.min_access_size
= 4,
3142 .impl
.max_access_size
= 4,
3143 .endianness
= DEVICE_LITTLE_ENDIAN
,
3146 static const MemoryRegionOps xhci_oper_ops
= {
3147 .read
= xhci_oper_read
,
3148 .write
= xhci_oper_write
,
3149 .valid
.min_access_size
= 4,
3150 .valid
.max_access_size
= 4,
3151 .endianness
= DEVICE_LITTLE_ENDIAN
,
3154 static const MemoryRegionOps xhci_port_ops
= {
3155 .read
= xhci_port_read
,
3156 .write
= xhci_port_write
,
3157 .valid
.min_access_size
= 4,
3158 .valid
.max_access_size
= 4,
3159 .endianness
= DEVICE_LITTLE_ENDIAN
,
3162 static const MemoryRegionOps xhci_runtime_ops
= {
3163 .read
= xhci_runtime_read
,
3164 .write
= xhci_runtime_write
,
3165 .valid
.min_access_size
= 4,
3166 .valid
.max_access_size
= 4,
3167 .endianness
= DEVICE_LITTLE_ENDIAN
,
3170 static const MemoryRegionOps xhci_doorbell_ops
= {
3171 .read
= xhci_doorbell_read
,
3172 .write
= xhci_doorbell_write
,
3173 .valid
.min_access_size
= 4,
3174 .valid
.max_access_size
= 4,
3175 .endianness
= DEVICE_LITTLE_ENDIAN
,
3178 static void xhci_attach(USBPort
*usbport
)
3180 XHCIState
*xhci
= usbport
->opaque
;
3181 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3183 xhci_port_update(port
, 0);
3186 static void xhci_detach(USBPort
*usbport
)
3188 XHCIState
*xhci
= usbport
->opaque
;
3189 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3191 xhci_detach_slot(xhci
, usbport
);
3192 xhci_port_update(port
, 1);
3195 static void xhci_wakeup(USBPort
*usbport
)
3197 XHCIState
*xhci
= usbport
->opaque
;
3198 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3200 if (get_field(port
->portsc
, PORTSC_PLS
) != PLS_U3
) {
3203 set_field(&port
->portsc
, PLS_RESUME
, PORTSC_PLS
);
3204 xhci_port_notify(port
, PORTSC_PLC
);
3207 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
3209 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
3211 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
3212 xhci_ep_nuke_one_xfer(xfer
, 0);
3215 xhci_try_complete_packet(xfer
);
3216 xhci_kick_epctx(xfer
->epctx
, xfer
->streamid
);
3217 if (xfer
->complete
) {
3218 xhci_ep_free_xfer(xfer
);
3222 static void xhci_child_detach(USBPort
*uport
, USBDevice
*child
)
3224 USBBus
*bus
= usb_bus_from_device(child
);
3225 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3227 xhci_detach_slot(xhci
, child
->port
);
3230 static USBPortOps xhci_uport_ops
= {
3231 .attach
= xhci_attach
,
3232 .detach
= xhci_detach
,
3233 .wakeup
= xhci_wakeup
,
3234 .complete
= xhci_complete
,
3235 .child_detach
= xhci_child_detach
,
3238 static int xhci_find_epid(USBEndpoint
*ep
)
3243 if (ep
->pid
== USB_TOKEN_IN
) {
3244 return ep
->nr
* 2 + 1;
3250 static USBEndpoint
*xhci_epid_to_usbep(XHCIEPContext
*epctx
)
3258 uport
= epctx
->xhci
->slots
[epctx
->slotid
- 1].uport
;
3259 token
= (epctx
->epid
& 1) ? USB_TOKEN_IN
: USB_TOKEN_OUT
;
3263 return usb_ep_get(uport
->dev
, token
, epctx
->epid
>> 1);
3266 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
3267 unsigned int stream
)
3269 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3272 DPRINTF("%s\n", __func__
);
3273 slotid
= ep
->dev
->addr
;
3274 if (slotid
== 0 || !xhci
->slots
[slotid
-1].enabled
) {
3275 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
3278 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
), stream
);
3281 static USBBusOps xhci_bus_ops
= {
3282 .wakeup_endpoint
= xhci_wakeup_endpoint
,
3285 static void usb_xhci_init(XHCIState
*xhci
)
3287 DeviceState
*dev
= DEVICE(xhci
);
3289 int i
, usbports
, speedmask
;
3291 xhci
->usbsts
= USBSTS_HCH
;
3293 if (xhci
->numports_2
> MAXPORTS_2
) {
3294 xhci
->numports_2
= MAXPORTS_2
;
3296 if (xhci
->numports_3
> MAXPORTS_3
) {
3297 xhci
->numports_3
= MAXPORTS_3
;
3299 usbports
= MAX(xhci
->numports_2
, xhci
->numports_3
);
3300 xhci
->numports
= xhci
->numports_2
+ xhci
->numports_3
;
3302 usb_bus_new(&xhci
->bus
, sizeof(xhci
->bus
), &xhci_bus_ops
, dev
);
3304 for (i
= 0; i
< usbports
; i
++) {
3306 if (i
< xhci
->numports_2
) {
3307 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3308 port
= &xhci
->ports
[i
+ xhci
->numports_3
];
3309 port
->portnr
= i
+ 1 + xhci
->numports_3
;
3311 port
= &xhci
->ports
[i
];
3312 port
->portnr
= i
+ 1;
3314 port
->uport
= &xhci
->uports
[i
];
3316 USB_SPEED_MASK_LOW
|
3317 USB_SPEED_MASK_FULL
|
3318 USB_SPEED_MASK_HIGH
;
3319 snprintf(port
->name
, sizeof(port
->name
), "usb2 port #%d", i
+1);
3320 speedmask
|= port
->speedmask
;
3322 if (i
< xhci
->numports_3
) {
3323 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3324 port
= &xhci
->ports
[i
];
3325 port
->portnr
= i
+ 1;
3327 port
= &xhci
->ports
[i
+ xhci
->numports_2
];
3328 port
->portnr
= i
+ 1 + xhci
->numports_2
;
3330 port
->uport
= &xhci
->uports
[i
];
3331 port
->speedmask
= USB_SPEED_MASK_SUPER
;
3332 snprintf(port
->name
, sizeof(port
->name
), "usb3 port #%d", i
+1);
3333 speedmask
|= port
->speedmask
;
3335 usb_register_port(&xhci
->bus
, &xhci
->uports
[i
], xhci
, i
,
3336 &xhci_uport_ops
, speedmask
);
3340 static void usb_xhci_realize(struct PCIDevice
*dev
, Error
**errp
)
3345 XHCIState
*xhci
= XHCI(dev
);
3347 dev
->config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
3348 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
3349 dev
->config
[PCI_CACHE_LINE_SIZE
] = 0x10;
3350 dev
->config
[0x60] = 0x30; /* release number */
3352 if (strcmp(object_get_typename(OBJECT(dev
)), TYPE_NEC_XHCI
) == 0) {
3353 xhci
->nec_quirks
= true;
3355 if (xhci
->numintrs
> MAXINTRS
) {
3356 xhci
->numintrs
= MAXINTRS
;
3358 while (xhci
->numintrs
& (xhci
->numintrs
- 1)) { /* ! power of 2 */
3361 if (xhci
->numintrs
< 1) {
3364 if (xhci
->numslots
> MAXSLOTS
) {
3365 xhci
->numslots
= MAXSLOTS
;
3367 if (xhci
->numslots
< 1) {
3370 if (xhci_get_flag(xhci
, XHCI_FLAG_ENABLE_STREAMS
)) {
3371 xhci
->max_pstreams_mask
= 7; /* == 256 primary streams */
3373 xhci
->max_pstreams_mask
= 0;
3376 if (xhci
->msi
!= ON_OFF_AUTO_OFF
) {
3377 ret
= msi_init(dev
, 0x70, xhci
->numintrs
, true, false, &err
);
3378 /* Any error other than -ENOTSUP(board's MSI support is broken)
3379 * is a programming error */
3380 assert(!ret
|| ret
== -ENOTSUP
);
3381 if (ret
&& xhci
->msi
== ON_OFF_AUTO_ON
) {
3382 /* Can't satisfy user's explicit msi=on request, fail */
3383 error_append_hint(&err
, "You have to use msi=auto (default) or "
3384 "msi=off with this machine type.\n");
3385 error_propagate(errp
, err
);
3388 assert(!err
|| xhci
->msi
== ON_OFF_AUTO_AUTO
);
3389 /* With msi=auto, we fall back to MSI off silently */
3393 usb_xhci_init(xhci
);
3394 xhci
->mfwrap_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_mfwrap_timer
, xhci
);
3396 memory_region_init(&xhci
->mem
, OBJECT(xhci
), "xhci", LEN_REGS
);
3397 memory_region_init_io(&xhci
->mem_cap
, OBJECT(xhci
), &xhci_cap_ops
, xhci
,
3398 "capabilities", LEN_CAP
);
3399 memory_region_init_io(&xhci
->mem_oper
, OBJECT(xhci
), &xhci_oper_ops
, xhci
,
3400 "operational", 0x400);
3401 memory_region_init_io(&xhci
->mem_runtime
, OBJECT(xhci
), &xhci_runtime_ops
, xhci
,
3402 "runtime", LEN_RUNTIME
);
3403 memory_region_init_io(&xhci
->mem_doorbell
, OBJECT(xhci
), &xhci_doorbell_ops
, xhci
,
3404 "doorbell", LEN_DOORBELL
);
3406 memory_region_add_subregion(&xhci
->mem
, 0, &xhci
->mem_cap
);
3407 memory_region_add_subregion(&xhci
->mem
, OFF_OPER
, &xhci
->mem_oper
);
3408 memory_region_add_subregion(&xhci
->mem
, OFF_RUNTIME
, &xhci
->mem_runtime
);
3409 memory_region_add_subregion(&xhci
->mem
, OFF_DOORBELL
, &xhci
->mem_doorbell
);
3411 for (i
= 0; i
< xhci
->numports
; i
++) {
3412 XHCIPort
*port
= &xhci
->ports
[i
];
3413 uint32_t offset
= OFF_OPER
+ 0x400 + 0x10 * i
;
3415 memory_region_init_io(&port
->mem
, OBJECT(xhci
), &xhci_port_ops
, port
,
3417 memory_region_add_subregion(&xhci
->mem
, offset
, &port
->mem
);
3420 pci_register_bar(dev
, 0,
3421 PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
,
3424 if (pci_bus_is_express(pci_get_bus(dev
)) ||
3425 xhci_get_flag(xhci
, XHCI_FLAG_FORCE_PCIE_ENDCAP
)) {
3426 ret
= pcie_endpoint_cap_init(dev
, 0xa0);
3430 if (xhci
->msix
!= ON_OFF_AUTO_OFF
) {
3431 /* TODO check for errors, and should fail when msix=on */
3432 msix_init(dev
, xhci
->numintrs
,
3433 &xhci
->mem
, 0, OFF_MSIX_TABLE
,
3434 &xhci
->mem
, 0, OFF_MSIX_PBA
,
3439 static void usb_xhci_exit(PCIDevice
*dev
)
3442 XHCIState
*xhci
= XHCI(dev
);
3444 trace_usb_xhci_exit();
3446 for (i
= 0; i
< xhci
->numslots
; i
++) {
3447 xhci_disable_slot(xhci
, i
+ 1);
3450 if (xhci
->mfwrap_timer
) {
3451 timer_del(xhci
->mfwrap_timer
);
3452 timer_free(xhci
->mfwrap_timer
);
3453 xhci
->mfwrap_timer
= NULL
;
3456 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_cap
);
3457 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_oper
);
3458 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_runtime
);
3459 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_doorbell
);
3461 for (i
= 0; i
< xhci
->numports
; i
++) {
3462 XHCIPort
*port
= &xhci
->ports
[i
];
3463 memory_region_del_subregion(&xhci
->mem
, &port
->mem
);
3466 /* destroy msix memory region */
3467 if (dev
->msix_table
&& dev
->msix_pba
3468 && dev
->msix_entry_used
) {
3469 msix_uninit(dev
, &xhci
->mem
, &xhci
->mem
);
3472 usb_bus_release(&xhci
->bus
);
3475 static int usb_xhci_post_load(void *opaque
, int version_id
)
3477 XHCIState
*xhci
= opaque
;
3478 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
3480 XHCIEPContext
*epctx
;
3481 dma_addr_t dcbaap
, pctx
;
3482 uint32_t slot_ctx
[4];
3484 int slotid
, epid
, state
, intr
;
3486 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
3488 for (slotid
= 1; slotid
<= xhci
->numslots
; slotid
++) {
3489 slot
= &xhci
->slots
[slotid
-1];
3490 if (!slot
->addressed
) {
3494 xhci_mask64(ldq_le_pci_dma(pci_dev
, dcbaap
+ 8 * slotid
));
3495 xhci_dma_read_u32s(xhci
, slot
->ctx
, slot_ctx
, sizeof(slot_ctx
));
3496 slot
->uport
= xhci_lookup_uport(xhci
, slot_ctx
);
3498 /* should not happen, but may trigger on guest bugs */
3500 slot
->addressed
= 0;
3503 assert(slot
->uport
&& slot
->uport
->dev
);
3505 for (epid
= 1; epid
<= 31; epid
++) {
3506 pctx
= slot
->ctx
+ 32 * epid
;
3507 xhci_dma_read_u32s(xhci
, pctx
, ep_ctx
, sizeof(ep_ctx
));
3508 state
= ep_ctx
[0] & EP_STATE_MASK
;
3509 if (state
== EP_DISABLED
) {
3512 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
3513 slot
->eps
[epid
-1] = epctx
;
3514 xhci_init_epctx(epctx
, pctx
, ep_ctx
);
3515 epctx
->state
= state
;
3516 if (state
== EP_RUNNING
) {
3517 /* kick endpoint after vmload is finished */
3518 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
3523 for (intr
= 0; intr
< xhci
->numintrs
; intr
++) {
3524 if (xhci
->intr
[intr
].msix_used
) {
3525 msix_vector_use(pci_dev
, intr
);
3527 msix_vector_unuse(pci_dev
, intr
);
3534 static const VMStateDescription vmstate_xhci_ring
= {
3535 .name
= "xhci-ring",
3537 .fields
= (VMStateField
[]) {
3538 VMSTATE_UINT64(dequeue
, XHCIRing
),
3539 VMSTATE_BOOL(ccs
, XHCIRing
),
3540 VMSTATE_END_OF_LIST()
3544 static const VMStateDescription vmstate_xhci_port
= {
3545 .name
= "xhci-port",
3547 .fields
= (VMStateField
[]) {
3548 VMSTATE_UINT32(portsc
, XHCIPort
),
3549 VMSTATE_END_OF_LIST()
3553 static const VMStateDescription vmstate_xhci_slot
= {
3554 .name
= "xhci-slot",
3556 .fields
= (VMStateField
[]) {
3557 VMSTATE_BOOL(enabled
, XHCISlot
),
3558 VMSTATE_BOOL(addressed
, XHCISlot
),
3559 VMSTATE_END_OF_LIST()
3563 static const VMStateDescription vmstate_xhci_event
= {
3564 .name
= "xhci-event",
3566 .fields
= (VMStateField
[]) {
3567 VMSTATE_UINT32(type
, XHCIEvent
),
3568 VMSTATE_UINT32(ccode
, XHCIEvent
),
3569 VMSTATE_UINT64(ptr
, XHCIEvent
),
3570 VMSTATE_UINT32(length
, XHCIEvent
),
3571 VMSTATE_UINT32(flags
, XHCIEvent
),
3572 VMSTATE_UINT8(slotid
, XHCIEvent
),
3573 VMSTATE_UINT8(epid
, XHCIEvent
),
3574 VMSTATE_END_OF_LIST()
3578 static bool xhci_er_full(void *opaque
, int version_id
)
3583 static const VMStateDescription vmstate_xhci_intr
= {
3584 .name
= "xhci-intr",
3586 .fields
= (VMStateField
[]) {
3588 VMSTATE_UINT32(iman
, XHCIInterrupter
),
3589 VMSTATE_UINT32(imod
, XHCIInterrupter
),
3590 VMSTATE_UINT32(erstsz
, XHCIInterrupter
),
3591 VMSTATE_UINT32(erstba_low
, XHCIInterrupter
),
3592 VMSTATE_UINT32(erstba_high
, XHCIInterrupter
),
3593 VMSTATE_UINT32(erdp_low
, XHCIInterrupter
),
3594 VMSTATE_UINT32(erdp_high
, XHCIInterrupter
),
3597 VMSTATE_BOOL(msix_used
, XHCIInterrupter
),
3598 VMSTATE_BOOL(er_pcs
, XHCIInterrupter
),
3599 VMSTATE_UINT64(er_start
, XHCIInterrupter
),
3600 VMSTATE_UINT32(er_size
, XHCIInterrupter
),
3601 VMSTATE_UINT32(er_ep_idx
, XHCIInterrupter
),
3603 /* event queue (used if ring is full) */
3604 VMSTATE_BOOL(er_full_unused
, XHCIInterrupter
),
3605 VMSTATE_UINT32_TEST(ev_buffer_put
, XHCIInterrupter
, xhci_er_full
),
3606 VMSTATE_UINT32_TEST(ev_buffer_get
, XHCIInterrupter
, xhci_er_full
),
3607 VMSTATE_STRUCT_ARRAY_TEST(ev_buffer
, XHCIInterrupter
, EV_QUEUE
,
3609 vmstate_xhci_event
, XHCIEvent
),
3611 VMSTATE_END_OF_LIST()
3615 static const VMStateDescription vmstate_xhci
= {
3618 .post_load
= usb_xhci_post_load
,
3619 .fields
= (VMStateField
[]) {
3620 VMSTATE_PCI_DEVICE(parent_obj
, XHCIState
),
3621 VMSTATE_MSIX(parent_obj
, XHCIState
),
3623 VMSTATE_STRUCT_VARRAY_UINT32(ports
, XHCIState
, numports
, 1,
3624 vmstate_xhci_port
, XHCIPort
),
3625 VMSTATE_STRUCT_VARRAY_UINT32(slots
, XHCIState
, numslots
, 1,
3626 vmstate_xhci_slot
, XHCISlot
),
3627 VMSTATE_STRUCT_VARRAY_UINT32(intr
, XHCIState
, numintrs
, 1,
3628 vmstate_xhci_intr
, XHCIInterrupter
),
3630 /* Operational Registers */
3631 VMSTATE_UINT32(usbcmd
, XHCIState
),
3632 VMSTATE_UINT32(usbsts
, XHCIState
),
3633 VMSTATE_UINT32(dnctrl
, XHCIState
),
3634 VMSTATE_UINT32(crcr_low
, XHCIState
),
3635 VMSTATE_UINT32(crcr_high
, XHCIState
),
3636 VMSTATE_UINT32(dcbaap_low
, XHCIState
),
3637 VMSTATE_UINT32(dcbaap_high
, XHCIState
),
3638 VMSTATE_UINT32(config
, XHCIState
),
3640 /* Runtime Registers & state */
3641 VMSTATE_INT64(mfindex_start
, XHCIState
),
3642 VMSTATE_TIMER_PTR(mfwrap_timer
, XHCIState
),
3643 VMSTATE_STRUCT(cmd_ring
, XHCIState
, 1, vmstate_xhci_ring
, XHCIRing
),
3645 VMSTATE_END_OF_LIST()
3649 static Property xhci_properties
[] = {
3650 DEFINE_PROP_BIT("streams", XHCIState
, flags
,
3651 XHCI_FLAG_ENABLE_STREAMS
, true),
3652 DEFINE_PROP_UINT32("p2", XHCIState
, numports_2
, 4),
3653 DEFINE_PROP_UINT32("p3", XHCIState
, numports_3
, 4),
3654 DEFINE_PROP_END_OF_LIST(),
3657 static void xhci_instance_init(Object
*obj
)
3659 /* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
3660 * line, therefore, no need to wait to realize like other devices */
3661 PCI_DEVICE(obj
)->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
3664 static void xhci_class_init(ObjectClass
*klass
, void *data
)
3666 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
3667 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3669 dc
->vmsd
= &vmstate_xhci
;
3670 dc
->props
= xhci_properties
;
3671 dc
->reset
= xhci_reset
;
3672 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
3673 k
->realize
= usb_xhci_realize
;
3674 k
->exit
= usb_xhci_exit
;
3675 k
->class_id
= PCI_CLASS_SERIAL_USB
;
3678 static const TypeInfo xhci_info
= {
3680 .parent
= TYPE_PCI_DEVICE
,
3681 .instance_size
= sizeof(XHCIState
),
3682 .class_init
= xhci_class_init
,
3683 .instance_init
= xhci_instance_init
,
3685 .interfaces
= (InterfaceInfo
[]) {
3686 { INTERFACE_PCIE_DEVICE
},
3687 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
3692 static void qemu_xhci_class_init(ObjectClass
*klass
, void *data
)
3694 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
3696 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
3697 k
->device_id
= PCI_DEVICE_ID_REDHAT_XHCI
;
3701 static void qemu_xhci_instance_init(Object
*obj
)
3703 XHCIState
*xhci
= XHCI(obj
);
3705 xhci
->msi
= ON_OFF_AUTO_OFF
;
3706 xhci
->msix
= ON_OFF_AUTO_AUTO
;
3707 xhci
->numintrs
= MAXINTRS
;
3708 xhci
->numslots
= MAXSLOTS
;
3709 xhci_set_flag(xhci
, XHCI_FLAG_SS_FIRST
);
3712 static const TypeInfo qemu_xhci_info
= {
3713 .name
= TYPE_QEMU_XHCI
,
3714 .parent
= TYPE_XHCI
,
3715 .class_init
= qemu_xhci_class_init
,
3716 .instance_init
= qemu_xhci_instance_init
,
3719 static void xhci_register_types(void)
3721 type_register_static(&xhci_info
);
3722 type_register_static(&qemu_xhci_info
);
3725 type_init(xhci_register_types
)