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"
25 #include "hw/pci/pci.h"
26 #include "hw/pci/msi.h"
27 #include "hw/pci/msix.h"
34 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
36 #define DPRINTF(...) do {} while (0)
38 #define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \
39 __func__, __LINE__, _msg); abort(); } while (0)
44 #define MAXPORTS (MAXPORTS_2+MAXPORTS_3)
50 /* Very pessimistic, let's hope it's enough for all cases */
51 #define EV_QUEUE (((3*TD_QUEUE)+16)*MAXSLOTS)
52 /* Do not deliver ER Full events. NEC's driver does some things not bound
53 * to the specs when it gets them */
57 #define LEN_OPER (0x400 + 0x10 * MAXPORTS)
58 #define LEN_RUNTIME ((MAXINTRS + 1) * 0x20)
59 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
61 #define OFF_OPER LEN_CAP
62 #define OFF_RUNTIME 0x1000
63 #define OFF_DOORBELL 0x2000
64 #define OFF_MSIX_TABLE 0x3000
65 #define OFF_MSIX_PBA 0x3800
66 /* must be power of 2 */
67 #define LEN_REGS 0x4000
69 #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
70 #error Increase OFF_RUNTIME
72 #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
73 #error Increase OFF_DOORBELL
75 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
76 # error Increase LEN_REGS
80 #define USBCMD_RS (1<<0)
81 #define USBCMD_HCRST (1<<1)
82 #define USBCMD_INTE (1<<2)
83 #define USBCMD_HSEE (1<<3)
84 #define USBCMD_LHCRST (1<<7)
85 #define USBCMD_CSS (1<<8)
86 #define USBCMD_CRS (1<<9)
87 #define USBCMD_EWE (1<<10)
88 #define USBCMD_EU3S (1<<11)
90 #define USBSTS_HCH (1<<0)
91 #define USBSTS_HSE (1<<2)
92 #define USBSTS_EINT (1<<3)
93 #define USBSTS_PCD (1<<4)
94 #define USBSTS_SSS (1<<8)
95 #define USBSTS_RSS (1<<9)
96 #define USBSTS_SRE (1<<10)
97 #define USBSTS_CNR (1<<11)
98 #define USBSTS_HCE (1<<12)
101 #define PORTSC_CCS (1<<0)
102 #define PORTSC_PED (1<<1)
103 #define PORTSC_OCA (1<<3)
104 #define PORTSC_PR (1<<4)
105 #define PORTSC_PLS_SHIFT 5
106 #define PORTSC_PLS_MASK 0xf
107 #define PORTSC_PP (1<<9)
108 #define PORTSC_SPEED_SHIFT 10
109 #define PORTSC_SPEED_MASK 0xf
110 #define PORTSC_SPEED_FULL (1<<10)
111 #define PORTSC_SPEED_LOW (2<<10)
112 #define PORTSC_SPEED_HIGH (3<<10)
113 #define PORTSC_SPEED_SUPER (4<<10)
114 #define PORTSC_PIC_SHIFT 14
115 #define PORTSC_PIC_MASK 0x3
116 #define PORTSC_LWS (1<<16)
117 #define PORTSC_CSC (1<<17)
118 #define PORTSC_PEC (1<<18)
119 #define PORTSC_WRC (1<<19)
120 #define PORTSC_OCC (1<<20)
121 #define PORTSC_PRC (1<<21)
122 #define PORTSC_PLC (1<<22)
123 #define PORTSC_CEC (1<<23)
124 #define PORTSC_CAS (1<<24)
125 #define PORTSC_WCE (1<<25)
126 #define PORTSC_WDE (1<<26)
127 #define PORTSC_WOE (1<<27)
128 #define PORTSC_DR (1<<30)
129 #define PORTSC_WPR (1<<31)
131 #define CRCR_RCS (1<<0)
132 #define CRCR_CS (1<<1)
133 #define CRCR_CA (1<<2)
134 #define CRCR_CRR (1<<3)
136 #define IMAN_IP (1<<0)
137 #define IMAN_IE (1<<1)
139 #define ERDP_EHB (1<<3)
142 typedef struct XHCITRB
{
161 PLS_COMPILANCE_MODE
= 10,
166 typedef enum TRBType
{
179 CR_CONFIGURE_ENDPOINT
,
187 CR_SET_LATENCY_TOLERANCE
,
188 CR_GET_PORT_BANDWIDTH
,
193 ER_PORT_STATUS_CHANGE
,
194 ER_BANDWIDTH_REQUEST
,
197 ER_DEVICE_NOTIFICATION
,
199 /* vendor specific bits */
200 CR_VENDOR_VIA_CHALLENGE_RESPONSE
= 48,
201 CR_VENDOR_NEC_FIRMWARE_REVISION
= 49,
202 CR_VENDOR_NEC_CHALLENGE_RESPONSE
= 50,
205 #define CR_LINK TR_LINK
207 typedef enum TRBCCode
{
210 CC_DATA_BUFFER_ERROR
,
212 CC_USB_TRANSACTION_ERROR
,
218 CC_INVALID_STREAM_TYPE_ERROR
,
219 CC_SLOT_NOT_ENABLED_ERROR
,
220 CC_EP_NOT_ENABLED_ERROR
,
226 CC_BANDWIDTH_OVERRUN
,
227 CC_CONTEXT_STATE_ERROR
,
228 CC_NO_PING_RESPONSE_ERROR
,
229 CC_EVENT_RING_FULL_ERROR
,
230 CC_INCOMPATIBLE_DEVICE_ERROR
,
231 CC_MISSED_SERVICE_ERROR
,
232 CC_COMMAND_RING_STOPPED
,
235 CC_STOPPED_LENGTH_INVALID
,
236 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
= 29,
237 CC_ISOCH_BUFFER_OVERRUN
= 31,
240 CC_INVALID_STREAM_ID_ERROR
,
241 CC_SECONDARY_BANDWIDTH_ERROR
,
242 CC_SPLIT_TRANSACTION_ERROR
246 #define TRB_TYPE_SHIFT 10
247 #define TRB_TYPE_MASK 0x3f
248 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
250 #define TRB_EV_ED (1<<2)
252 #define TRB_TR_ENT (1<<1)
253 #define TRB_TR_ISP (1<<2)
254 #define TRB_TR_NS (1<<3)
255 #define TRB_TR_CH (1<<4)
256 #define TRB_TR_IOC (1<<5)
257 #define TRB_TR_IDT (1<<6)
258 #define TRB_TR_TBC_SHIFT 7
259 #define TRB_TR_TBC_MASK 0x3
260 #define TRB_TR_BEI (1<<9)
261 #define TRB_TR_TLBPC_SHIFT 16
262 #define TRB_TR_TLBPC_MASK 0xf
263 #define TRB_TR_FRAMEID_SHIFT 20
264 #define TRB_TR_FRAMEID_MASK 0x7ff
265 #define TRB_TR_SIA (1<<31)
267 #define TRB_TR_DIR (1<<16)
269 #define TRB_CR_SLOTID_SHIFT 24
270 #define TRB_CR_SLOTID_MASK 0xff
271 #define TRB_CR_EPID_SHIFT 16
272 #define TRB_CR_EPID_MASK 0x1f
274 #define TRB_CR_BSR (1<<9)
275 #define TRB_CR_DC (1<<9)
277 #define TRB_LK_TC (1<<1)
279 #define TRB_INTR_SHIFT 22
280 #define TRB_INTR_MASK 0x3ff
281 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
283 #define EP_TYPE_MASK 0x7
284 #define EP_TYPE_SHIFT 3
286 #define EP_STATE_MASK 0x7
287 #define EP_DISABLED (0<<0)
288 #define EP_RUNNING (1<<0)
289 #define EP_HALTED (2<<0)
290 #define EP_STOPPED (3<<0)
291 #define EP_ERROR (4<<0)
293 #define SLOT_STATE_MASK 0x1f
294 #define SLOT_STATE_SHIFT 27
295 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
296 #define SLOT_ENABLED 0
297 #define SLOT_DEFAULT 1
298 #define SLOT_ADDRESSED 2
299 #define SLOT_CONFIGURED 3
301 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
302 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
304 typedef struct XHCIState XHCIState
;
305 typedef struct XHCIStreamContext XHCIStreamContext
;
306 typedef struct XHCIEPContext XHCIEPContext
;
308 #define get_field(data, field) \
309 (((data) >> field##_SHIFT) & field##_MASK)
311 #define set_field(data, newval, field) do { \
312 uint32_t val = *data; \
313 val &= ~(field##_MASK << field##_SHIFT); \
314 val |= ((newval) & field##_MASK) << field##_SHIFT; \
318 typedef enum EPType
{
329 typedef struct XHCIRing
{
334 typedef struct XHCIPort
{
344 typedef struct XHCITransfer
{
352 unsigned int iso_pkts
;
355 unsigned int streamid
;
360 unsigned int trb_count
;
361 unsigned int trb_alloced
;
367 unsigned int pktsize
;
368 unsigned int cur_pkt
;
370 uint64_t mfindex_kick
;
373 struct XHCIStreamContext
{
379 struct XHCIEPContext
{
385 unsigned int next_xfer
;
386 unsigned int comp_xfer
;
387 XHCITransfer transfers
[TD_QUEUE
];
391 unsigned int max_psize
;
395 unsigned int max_pstreams
;
397 unsigned int nr_pstreams
;
398 XHCIStreamContext
*pstreams
;
400 /* iso xfer scheduling */
401 unsigned int interval
;
402 int64_t mfindex_last
;
403 QEMUTimer
*kick_timer
;
406 typedef struct XHCISlot
{
411 XHCIEPContext
* eps
[31];
414 typedef struct XHCIEvent
{
424 typedef struct XHCIInterrupter
{
429 uint32_t erstba_high
;
433 bool msix_used
, er_pcs
, er_full
;
437 unsigned int er_ep_idx
;
439 XHCIEvent ev_buffer
[EV_QUEUE
];
440 unsigned int ev_buffer_put
;
441 unsigned int ev_buffer_get
;
447 PCIDevice parent_obj
;
452 MemoryRegion mem_cap
;
453 MemoryRegion mem_oper
;
454 MemoryRegion mem_runtime
;
455 MemoryRegion mem_doorbell
;
463 uint32_t max_pstreams_mask
;
465 /* Operational Registers */
472 uint32_t dcbaap_high
;
475 USBPort uports
[MAX(MAXPORTS_2
, MAXPORTS_3
)];
476 XHCIPort ports
[MAXPORTS
];
477 XHCISlot slots
[MAXSLOTS
];
480 /* Runtime Registers */
481 int64_t mfindex_start
;
482 QEMUTimer
*mfwrap_timer
;
483 XHCIInterrupter intr
[MAXINTRS
];
488 #define TYPE_XHCI "nec-usb-xhci"
491 OBJECT_CHECK(XHCIState, (obj), TYPE_XHCI)
493 typedef struct XHCIEvRingSeg
{
501 XHCI_FLAG_USE_MSI
= 1,
504 XHCI_FLAG_FORCE_PCIE_ENDCAP
,
505 XHCI_FLAG_ENABLE_STREAMS
,
508 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
509 unsigned int epid
, unsigned int streamid
);
510 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
512 static void xhci_xfer_report(XHCITransfer
*xfer
);
513 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
514 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
515 static USBEndpoint
*xhci_epid_to_usbep(XHCIState
*xhci
,
516 unsigned int slotid
, unsigned int epid
);
518 static const char *TRBType_names
[] = {
519 [TRB_RESERVED
] = "TRB_RESERVED",
520 [TR_NORMAL
] = "TR_NORMAL",
521 [TR_SETUP
] = "TR_SETUP",
522 [TR_DATA
] = "TR_DATA",
523 [TR_STATUS
] = "TR_STATUS",
524 [TR_ISOCH
] = "TR_ISOCH",
525 [TR_LINK
] = "TR_LINK",
526 [TR_EVDATA
] = "TR_EVDATA",
527 [TR_NOOP
] = "TR_NOOP",
528 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
529 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
530 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
531 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
532 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
533 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
534 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
535 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
536 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
537 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
538 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
539 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
540 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
541 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
542 [CR_NOOP
] = "CR_NOOP",
543 [ER_TRANSFER
] = "ER_TRANSFER",
544 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
545 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
546 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
547 [ER_DOORBELL
] = "ER_DOORBELL",
548 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
549 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
550 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
551 [CR_VENDOR_VIA_CHALLENGE_RESPONSE
] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE",
552 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
553 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
556 static const char *TRBCCode_names
[] = {
557 [CC_INVALID
] = "CC_INVALID",
558 [CC_SUCCESS
] = "CC_SUCCESS",
559 [CC_DATA_BUFFER_ERROR
] = "CC_DATA_BUFFER_ERROR",
560 [CC_BABBLE_DETECTED
] = "CC_BABBLE_DETECTED",
561 [CC_USB_TRANSACTION_ERROR
] = "CC_USB_TRANSACTION_ERROR",
562 [CC_TRB_ERROR
] = "CC_TRB_ERROR",
563 [CC_STALL_ERROR
] = "CC_STALL_ERROR",
564 [CC_RESOURCE_ERROR
] = "CC_RESOURCE_ERROR",
565 [CC_BANDWIDTH_ERROR
] = "CC_BANDWIDTH_ERROR",
566 [CC_NO_SLOTS_ERROR
] = "CC_NO_SLOTS_ERROR",
567 [CC_INVALID_STREAM_TYPE_ERROR
] = "CC_INVALID_STREAM_TYPE_ERROR",
568 [CC_SLOT_NOT_ENABLED_ERROR
] = "CC_SLOT_NOT_ENABLED_ERROR",
569 [CC_EP_NOT_ENABLED_ERROR
] = "CC_EP_NOT_ENABLED_ERROR",
570 [CC_SHORT_PACKET
] = "CC_SHORT_PACKET",
571 [CC_RING_UNDERRUN
] = "CC_RING_UNDERRUN",
572 [CC_RING_OVERRUN
] = "CC_RING_OVERRUN",
573 [CC_VF_ER_FULL
] = "CC_VF_ER_FULL",
574 [CC_PARAMETER_ERROR
] = "CC_PARAMETER_ERROR",
575 [CC_BANDWIDTH_OVERRUN
] = "CC_BANDWIDTH_OVERRUN",
576 [CC_CONTEXT_STATE_ERROR
] = "CC_CONTEXT_STATE_ERROR",
577 [CC_NO_PING_RESPONSE_ERROR
] = "CC_NO_PING_RESPONSE_ERROR",
578 [CC_EVENT_RING_FULL_ERROR
] = "CC_EVENT_RING_FULL_ERROR",
579 [CC_INCOMPATIBLE_DEVICE_ERROR
] = "CC_INCOMPATIBLE_DEVICE_ERROR",
580 [CC_MISSED_SERVICE_ERROR
] = "CC_MISSED_SERVICE_ERROR",
581 [CC_COMMAND_RING_STOPPED
] = "CC_COMMAND_RING_STOPPED",
582 [CC_COMMAND_ABORTED
] = "CC_COMMAND_ABORTED",
583 [CC_STOPPED
] = "CC_STOPPED",
584 [CC_STOPPED_LENGTH_INVALID
] = "CC_STOPPED_LENGTH_INVALID",
585 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
]
586 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
587 [CC_ISOCH_BUFFER_OVERRUN
] = "CC_ISOCH_BUFFER_OVERRUN",
588 [CC_EVENT_LOST_ERROR
] = "CC_EVENT_LOST_ERROR",
589 [CC_UNDEFINED_ERROR
] = "CC_UNDEFINED_ERROR",
590 [CC_INVALID_STREAM_ID_ERROR
] = "CC_INVALID_STREAM_ID_ERROR",
591 [CC_SECONDARY_BANDWIDTH_ERROR
] = "CC_SECONDARY_BANDWIDTH_ERROR",
592 [CC_SPLIT_TRANSACTION_ERROR
] = "CC_SPLIT_TRANSACTION_ERROR",
595 static const char *ep_state_names
[] = {
596 [EP_DISABLED
] = "disabled",
597 [EP_RUNNING
] = "running",
598 [EP_HALTED
] = "halted",
599 [EP_STOPPED
] = "stopped",
600 [EP_ERROR
] = "error",
603 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
605 if (index
>= llen
|| list
[index
] == NULL
) {
611 static const char *trb_name(XHCITRB
*trb
)
613 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
614 ARRAY_SIZE(TRBType_names
));
617 static const char *event_name(XHCIEvent
*event
)
619 return lookup_name(event
->ccode
, TRBCCode_names
,
620 ARRAY_SIZE(TRBCCode_names
));
623 static const char *ep_state_name(uint32_t state
)
625 return lookup_name(state
, ep_state_names
,
626 ARRAY_SIZE(ep_state_names
));
629 static bool xhci_get_flag(XHCIState
*xhci
, enum xhci_flags bit
)
631 return xhci
->flags
& (1 << bit
);
634 static uint64_t xhci_mfindex_get(XHCIState
*xhci
)
636 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
637 return (now
- xhci
->mfindex_start
) / 125000;
640 static void xhci_mfwrap_update(XHCIState
*xhci
)
642 const uint32_t bits
= USBCMD_RS
| USBCMD_EWE
;
643 uint32_t mfindex
, left
;
646 if ((xhci
->usbcmd
& bits
) == bits
) {
647 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
648 mfindex
= ((now
- xhci
->mfindex_start
) / 125000) & 0x3fff;
649 left
= 0x4000 - mfindex
;
650 timer_mod(xhci
->mfwrap_timer
, now
+ left
* 125000);
652 timer_del(xhci
->mfwrap_timer
);
656 static void xhci_mfwrap_timer(void *opaque
)
658 XHCIState
*xhci
= opaque
;
659 XHCIEvent wrap
= { ER_MFINDEX_WRAP
, CC_SUCCESS
};
661 xhci_event(xhci
, &wrap
, 0);
662 xhci_mfwrap_update(xhci
);
665 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
667 if (sizeof(dma_addr_t
) == 4) {
670 return low
| (((dma_addr_t
)high
<< 16) << 16);
674 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
676 if (sizeof(dma_addr_t
) == 4) {
677 return addr
& 0xffffffff;
683 static inline void xhci_dma_read_u32s(XHCIState
*xhci
, dma_addr_t addr
,
684 uint32_t *buf
, size_t len
)
688 assert((len
% sizeof(uint32_t)) == 0);
690 pci_dma_read(PCI_DEVICE(xhci
), addr
, buf
, len
);
692 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
693 buf
[i
] = le32_to_cpu(buf
[i
]);
697 static inline void xhci_dma_write_u32s(XHCIState
*xhci
, dma_addr_t addr
,
698 uint32_t *buf
, size_t len
)
702 uint32_t n
= len
/ sizeof(uint32_t);
704 assert((len
% sizeof(uint32_t)) == 0);
705 assert(n
<= ARRAY_SIZE(tmp
));
707 for (i
= 0; i
< n
; i
++) {
708 tmp
[i
] = cpu_to_le32(buf
[i
]);
710 pci_dma_write(PCI_DEVICE(xhci
), addr
, tmp
, len
);
713 static XHCIPort
*xhci_lookup_port(XHCIState
*xhci
, struct USBPort
*uport
)
720 switch (uport
->dev
->speed
) {
724 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
725 index
= uport
->index
+ xhci
->numports_3
;
727 index
= uport
->index
;
730 case USB_SPEED_SUPER
:
731 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
732 index
= uport
->index
;
734 index
= uport
->index
+ xhci
->numports_2
;
740 return &xhci
->ports
[index
];
743 static void xhci_intx_update(XHCIState
*xhci
)
745 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
748 if (msix_enabled(pci_dev
) ||
749 msi_enabled(pci_dev
)) {
753 if (xhci
->intr
[0].iman
& IMAN_IP
&&
754 xhci
->intr
[0].iman
& IMAN_IE
&&
755 xhci
->usbcmd
& USBCMD_INTE
) {
759 trace_usb_xhci_irq_intx(level
);
760 pci_set_irq(pci_dev
, level
);
763 static void xhci_msix_update(XHCIState
*xhci
, int v
)
765 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
768 if (!msix_enabled(pci_dev
)) {
772 enabled
= xhci
->intr
[v
].iman
& IMAN_IE
;
773 if (enabled
== xhci
->intr
[v
].msix_used
) {
778 trace_usb_xhci_irq_msix_use(v
);
779 msix_vector_use(pci_dev
, v
);
780 xhci
->intr
[v
].msix_used
= true;
782 trace_usb_xhci_irq_msix_unuse(v
);
783 msix_vector_unuse(pci_dev
, v
);
784 xhci
->intr
[v
].msix_used
= false;
788 static void xhci_intr_raise(XHCIState
*xhci
, int v
)
790 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
792 xhci
->intr
[v
].erdp_low
|= ERDP_EHB
;
793 xhci
->intr
[v
].iman
|= IMAN_IP
;
794 xhci
->usbsts
|= USBSTS_EINT
;
796 if (!(xhci
->intr
[v
].iman
& IMAN_IE
)) {
800 if (!(xhci
->usbcmd
& USBCMD_INTE
)) {
804 if (msix_enabled(pci_dev
)) {
805 trace_usb_xhci_irq_msix(v
);
806 msix_notify(pci_dev
, v
);
810 if (msi_enabled(pci_dev
)) {
811 trace_usb_xhci_irq_msi(v
);
812 msi_notify(pci_dev
, v
);
817 trace_usb_xhci_irq_intx(1);
818 pci_irq_assert(pci_dev
);
822 static inline int xhci_running(XHCIState
*xhci
)
824 return !(xhci
->usbsts
& USBSTS_HCH
) && !xhci
->intr
[0].er_full
;
827 static void xhci_die(XHCIState
*xhci
)
829 xhci
->usbsts
|= USBSTS_HCE
;
830 DPRINTF("xhci: asserted controller error\n");
833 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
835 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
836 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
840 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
841 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
842 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
843 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
845 ev_trb
.control
|= TRB_C
;
847 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
849 trace_usb_xhci_queue_event(v
, intr
->er_ep_idx
, trb_name(&ev_trb
),
850 event_name(event
), ev_trb
.parameter
,
851 ev_trb
.status
, ev_trb
.control
);
853 addr
= intr
->er_start
+ TRB_SIZE
*intr
->er_ep_idx
;
854 pci_dma_write(pci_dev
, addr
, &ev_trb
, TRB_SIZE
);
857 if (intr
->er_ep_idx
>= intr
->er_size
) {
859 intr
->er_pcs
= !intr
->er_pcs
;
863 static void xhci_events_update(XHCIState
*xhci
, int v
)
865 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
870 if (xhci
->usbsts
& USBSTS_HCH
) {
874 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
875 if (erdp
< intr
->er_start
||
876 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
877 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
878 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
879 v
, intr
->er_start
, intr
->er_size
);
883 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
884 assert(dp_idx
< intr
->er_size
);
886 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus
887 * deadlocks when the ER is full. Hack it by holding off events until
888 * the driver decides to free at least half of the ring */
890 int er_free
= dp_idx
- intr
->er_ep_idx
;
892 er_free
+= intr
->er_size
;
894 if (er_free
< (intr
->er_size
/2)) {
895 DPRINTF("xhci_events_update(): event ring still "
896 "more than half full (hack)\n");
901 while (intr
->ev_buffer_put
!= intr
->ev_buffer_get
) {
902 assert(intr
->er_full
);
903 if (((intr
->er_ep_idx
+1) % intr
->er_size
) == dp_idx
) {
904 DPRINTF("xhci_events_update(): event ring full again\n");
906 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
907 xhci_write_event(xhci
, &full
, v
);
912 XHCIEvent
*event
= &intr
->ev_buffer
[intr
->ev_buffer_get
];
913 xhci_write_event(xhci
, event
, v
);
914 intr
->ev_buffer_get
++;
916 if (intr
->ev_buffer_get
== EV_QUEUE
) {
917 intr
->ev_buffer_get
= 0;
922 xhci_intr_raise(xhci
, v
);
925 if (intr
->er_full
&& intr
->ev_buffer_put
== intr
->ev_buffer_get
) {
926 DPRINTF("xhci_events_update(): event ring no longer full\n");
931 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
933 XHCIInterrupter
*intr
;
937 if (v
>= xhci
->numintrs
) {
938 DPRINTF("intr nr out of range (%d >= %d)\n", v
, xhci
->numintrs
);
941 intr
= &xhci
->intr
[v
];
944 DPRINTF("xhci_event(): ER full, queueing\n");
945 if (((intr
->ev_buffer_put
+1) % EV_QUEUE
) == intr
->ev_buffer_get
) {
946 DPRINTF("xhci: event queue full, dropping event!\n");
949 intr
->ev_buffer
[intr
->ev_buffer_put
++] = *event
;
950 if (intr
->ev_buffer_put
== EV_QUEUE
) {
951 intr
->ev_buffer_put
= 0;
956 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
957 if (erdp
< intr
->er_start
||
958 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
959 DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
960 DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
961 v
, intr
->er_start
, intr
->er_size
);
966 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
967 assert(dp_idx
< intr
->er_size
);
969 if ((intr
->er_ep_idx
+1) % intr
->er_size
== dp_idx
) {
970 DPRINTF("xhci_event(): ER full, queueing\n");
972 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
973 xhci_write_event(xhci
, &full
);
976 if (((intr
->ev_buffer_put
+1) % EV_QUEUE
) == intr
->ev_buffer_get
) {
977 DPRINTF("xhci: event queue full, dropping event!\n");
980 intr
->ev_buffer
[intr
->ev_buffer_put
++] = *event
;
981 if (intr
->ev_buffer_put
== EV_QUEUE
) {
982 intr
->ev_buffer_put
= 0;
985 xhci_write_event(xhci
, event
, v
);
988 xhci_intr_raise(xhci
, v
);
991 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
994 ring
->dequeue
= base
;
998 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
1001 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
1005 pci_dma_read(pci_dev
, ring
->dequeue
, trb
, TRB_SIZE
);
1006 trb
->addr
= ring
->dequeue
;
1007 trb
->ccs
= ring
->ccs
;
1008 le64_to_cpus(&trb
->parameter
);
1009 le32_to_cpus(&trb
->status
);
1010 le32_to_cpus(&trb
->control
);
1012 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
1013 trb
->parameter
, trb
->status
, trb
->control
);
1015 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
1019 type
= TRB_TYPE(*trb
);
1021 if (type
!= TR_LINK
) {
1023 *addr
= ring
->dequeue
;
1025 ring
->dequeue
+= TRB_SIZE
;
1028 ring
->dequeue
= xhci_mask64(trb
->parameter
);
1029 if (trb
->control
& TRB_LK_TC
) {
1030 ring
->ccs
= !ring
->ccs
;
1036 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
1038 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
1041 dma_addr_t dequeue
= ring
->dequeue
;
1042 bool ccs
= ring
->ccs
;
1043 /* hack to bundle together the two/three TDs that make a setup transfer */
1044 bool control_td_set
= 0;
1048 pci_dma_read(pci_dev
, dequeue
, &trb
, TRB_SIZE
);
1049 le64_to_cpus(&trb
.parameter
);
1050 le32_to_cpus(&trb
.status
);
1051 le32_to_cpus(&trb
.control
);
1053 if ((trb
.control
& TRB_C
) != ccs
) {
1057 type
= TRB_TYPE(trb
);
1059 if (type
== TR_LINK
) {
1060 dequeue
= xhci_mask64(trb
.parameter
);
1061 if (trb
.control
& TRB_LK_TC
) {
1068 dequeue
+= TRB_SIZE
;
1070 if (type
== TR_SETUP
) {
1072 } else if (type
== TR_STATUS
) {
1076 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
1082 static void xhci_er_reset(XHCIState
*xhci
, int v
)
1084 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
1087 if (intr
->erstsz
== 0) {
1093 /* cache the (sole) event ring segment location */
1094 if (intr
->erstsz
!= 1) {
1095 DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr
->erstsz
);
1099 dma_addr_t erstba
= xhci_addr64(intr
->erstba_low
, intr
->erstba_high
);
1100 pci_dma_read(PCI_DEVICE(xhci
), erstba
, &seg
, sizeof(seg
));
1101 le32_to_cpus(&seg
.addr_low
);
1102 le32_to_cpus(&seg
.addr_high
);
1103 le32_to_cpus(&seg
.size
);
1104 if (seg
.size
< 16 || seg
.size
> 4096) {
1105 DPRINTF("xhci: invalid value for segment size: %d\n", seg
.size
);
1109 intr
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
1110 intr
->er_size
= seg
.size
;
1112 intr
->er_ep_idx
= 0;
1116 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT
" [%d]\n",
1117 v
, intr
->er_start
, intr
->er_size
);
1120 static void xhci_run(XHCIState
*xhci
)
1122 trace_usb_xhci_run();
1123 xhci
->usbsts
&= ~USBSTS_HCH
;
1124 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1127 static void xhci_stop(XHCIState
*xhci
)
1129 trace_usb_xhci_stop();
1130 xhci
->usbsts
|= USBSTS_HCH
;
1131 xhci
->crcr_low
&= ~CRCR_CRR
;
1134 static XHCIStreamContext
*xhci_alloc_stream_contexts(unsigned count
,
1137 XHCIStreamContext
*stctx
;
1140 stctx
= g_new0(XHCIStreamContext
, count
);
1141 for (i
= 0; i
< count
; i
++) {
1142 stctx
[i
].pctx
= base
+ i
* 16;
1148 static void xhci_reset_streams(XHCIEPContext
*epctx
)
1152 for (i
= 0; i
< epctx
->nr_pstreams
; i
++) {
1153 epctx
->pstreams
[i
].sct
= -1;
1157 static void xhci_alloc_streams(XHCIEPContext
*epctx
, dma_addr_t base
)
1159 assert(epctx
->pstreams
== NULL
);
1160 epctx
->nr_pstreams
= 2 << epctx
->max_pstreams
;
1161 epctx
->pstreams
= xhci_alloc_stream_contexts(epctx
->nr_pstreams
, base
);
1164 static void xhci_free_streams(XHCIEPContext
*epctx
)
1166 assert(epctx
->pstreams
!= NULL
);
1168 g_free(epctx
->pstreams
);
1169 epctx
->pstreams
= NULL
;
1170 epctx
->nr_pstreams
= 0;
1173 static int xhci_epmask_to_eps_with_streams(XHCIState
*xhci
,
1174 unsigned int slotid
,
1176 XHCIEPContext
**epctxs
,
1180 XHCIEPContext
*epctx
;
1184 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1186 slot
= &xhci
->slots
[slotid
- 1];
1188 for (i
= 2, j
= 0; i
<= 31; i
++) {
1189 if (!(epmask
& (1u << i
))) {
1193 epctx
= slot
->eps
[i
- 1];
1194 ep
= xhci_epid_to_usbep(xhci
, slotid
, i
);
1195 if (!epctx
|| !epctx
->nr_pstreams
|| !ep
) {
1207 static void xhci_free_device_streams(XHCIState
*xhci
, unsigned int slotid
,
1210 USBEndpoint
*eps
[30];
1213 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, NULL
, eps
);
1215 usb_device_free_streams(eps
[0]->dev
, eps
, nr_eps
);
1219 static TRBCCode
xhci_alloc_device_streams(XHCIState
*xhci
, unsigned int slotid
,
1222 XHCIEPContext
*epctxs
[30];
1223 USBEndpoint
*eps
[30];
1224 int i
, r
, nr_eps
, req_nr_streams
, dev_max_streams
;
1226 nr_eps
= xhci_epmask_to_eps_with_streams(xhci
, slotid
, epmask
, epctxs
,
1232 req_nr_streams
= epctxs
[0]->nr_pstreams
;
1233 dev_max_streams
= eps
[0]->max_streams
;
1235 for (i
= 1; i
< nr_eps
; i
++) {
1237 * HdG: I don't expect these to ever trigger, but if they do we need
1238 * to come up with another solution, ie group identical endpoints
1239 * together and make an usb_device_alloc_streams call per group.
1241 if (epctxs
[i
]->nr_pstreams
!= req_nr_streams
) {
1242 FIXME("guest streams config not identical for all eps");
1243 return CC_RESOURCE_ERROR
;
1245 if (eps
[i
]->max_streams
!= dev_max_streams
) {
1246 FIXME("device streams config not identical for all eps");
1247 return CC_RESOURCE_ERROR
;
1252 * max-streams in both the device descriptor and in the controller is a
1253 * power of 2. But stream id 0 is reserved, so if a device can do up to 4
1254 * streams the guest will ask for 5 rounded up to the next power of 2 which
1255 * becomes 8. For emulated devices usb_device_alloc_streams is a nop.
1257 * For redirected devices however this is an issue, as there we must ask
1258 * the real xhci controller to alloc streams, and the host driver for the
1259 * real xhci controller will likely disallow allocating more streams then
1260 * the device can handle.
1262 * So we limit the requested nr_streams to the maximum number the device
1265 if (req_nr_streams
> dev_max_streams
) {
1266 req_nr_streams
= dev_max_streams
;
1269 r
= usb_device_alloc_streams(eps
[0]->dev
, eps
, nr_eps
, req_nr_streams
);
1271 DPRINTF("xhci: alloc streams failed\n");
1272 return CC_RESOURCE_ERROR
;
1278 static XHCIStreamContext
*xhci_find_stream(XHCIEPContext
*epctx
,
1279 unsigned int streamid
,
1282 XHCIStreamContext
*sctx
;
1284 uint32_t ctx
[2], sct
;
1286 assert(streamid
!= 0);
1288 if (streamid
>= epctx
->nr_pstreams
) {
1289 *cc_error
= CC_INVALID_STREAM_ID_ERROR
;
1292 sctx
= epctx
->pstreams
+ streamid
;
1294 FIXME("secondary streams not implemented yet");
1297 if (sctx
->sct
== -1) {
1298 xhci_dma_read_u32s(epctx
->xhci
, sctx
->pctx
, ctx
, sizeof(ctx
));
1299 sct
= (ctx
[0] >> 1) & 0x07;
1300 if (epctx
->lsa
&& sct
!= 1) {
1301 *cc_error
= CC_INVALID_STREAM_TYPE_ERROR
;
1305 base
= xhci_addr64(ctx
[0] & ~0xf, ctx
[1]);
1306 xhci_ring_init(epctx
->xhci
, &sctx
->ring
, base
);
1311 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
1312 XHCIStreamContext
*sctx
, uint32_t state
)
1314 XHCIRing
*ring
= NULL
;
1318 xhci_dma_read_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1319 ctx
[0] &= ~EP_STATE_MASK
;
1322 /* update ring dequeue ptr */
1323 if (epctx
->nr_pstreams
) {
1326 xhci_dma_read_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1328 ctx2
[0] |= sctx
->ring
.dequeue
| sctx
->ring
.ccs
;
1329 ctx2
[1] = (sctx
->ring
.dequeue
>> 16) >> 16;
1330 xhci_dma_write_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1333 ring
= &epctx
->ring
;
1336 ctx
[2] = ring
->dequeue
| ring
->ccs
;
1337 ctx
[3] = (ring
->dequeue
>> 16) >> 16;
1339 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
1340 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
1343 xhci_dma_write_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1344 if (epctx
->state
!= state
) {
1345 trace_usb_xhci_ep_state(epctx
->slotid
, epctx
->epid
,
1346 ep_state_name(epctx
->state
),
1347 ep_state_name(state
));
1349 epctx
->state
= state
;
1352 static void xhci_ep_kick_timer(void *opaque
)
1354 XHCIEPContext
*epctx
= opaque
;
1355 xhci_kick_ep(epctx
->xhci
, epctx
->slotid
, epctx
->epid
, 0);
1358 static XHCIEPContext
*xhci_alloc_epctx(XHCIState
*xhci
,
1359 unsigned int slotid
,
1362 XHCIEPContext
*epctx
;
1365 epctx
= g_new0(XHCIEPContext
, 1);
1367 epctx
->slotid
= slotid
;
1370 for (i
= 0; i
< ARRAY_SIZE(epctx
->transfers
); i
++) {
1371 epctx
->transfers
[i
].xhci
= xhci
;
1372 epctx
->transfers
[i
].slotid
= slotid
;
1373 epctx
->transfers
[i
].epid
= epid
;
1374 usb_packet_init(&epctx
->transfers
[i
].packet
);
1376 epctx
->kick_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_ep_kick_timer
, epctx
);
1381 static void xhci_init_epctx(XHCIEPContext
*epctx
,
1382 dma_addr_t pctx
, uint32_t *ctx
)
1386 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
1388 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
1390 epctx
->max_psize
= ctx
[1]>>16;
1391 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
1392 epctx
->max_pstreams
= (ctx
[0] >> 10) & epctx
->xhci
->max_pstreams_mask
;
1393 epctx
->lsa
= (ctx
[0] >> 15) & 1;
1394 if (epctx
->max_pstreams
) {
1395 xhci_alloc_streams(epctx
, dequeue
);
1397 xhci_ring_init(epctx
->xhci
, &epctx
->ring
, dequeue
);
1398 epctx
->ring
.ccs
= ctx
[2] & 1;
1401 epctx
->interval
= 1 << ((ctx
[0] >> 16) & 0xff);
1404 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
1405 unsigned int epid
, dma_addr_t pctx
,
1409 XHCIEPContext
*epctx
;
1411 trace_usb_xhci_ep_enable(slotid
, epid
);
1412 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1413 assert(epid
>= 1 && epid
<= 31);
1415 slot
= &xhci
->slots
[slotid
-1];
1416 if (slot
->eps
[epid
-1]) {
1417 xhci_disable_ep(xhci
, slotid
, epid
);
1420 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
1421 slot
->eps
[epid
-1] = epctx
;
1422 xhci_init_epctx(epctx
, pctx
, ctx
);
1424 DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) "
1425 "size is %d\n", epid
/2, epid
%2, epctx
->type
, epctx
->max_psize
);
1427 epctx
->mfindex_last
= 0;
1429 epctx
->state
= EP_RUNNING
;
1430 ctx
[0] &= ~EP_STATE_MASK
;
1431 ctx
[0] |= EP_RUNNING
;
1436 static int xhci_ep_nuke_one_xfer(XHCITransfer
*t
, TRBCCode report
)
1440 if (report
&& (t
->running_async
|| t
->running_retry
)) {
1442 xhci_xfer_report(t
);
1445 if (t
->running_async
) {
1446 usb_cancel_packet(&t
->packet
);
1447 t
->running_async
= 0;
1450 if (t
->running_retry
) {
1451 XHCIEPContext
*epctx
= t
->xhci
->slots
[t
->slotid
-1].eps
[t
->epid
-1];
1453 epctx
->retry
= NULL
;
1454 timer_del(epctx
->kick_timer
);
1456 t
->running_retry
= 0;
1462 t
->trb_count
= t
->trb_alloced
= 0;
1467 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
1468 unsigned int epid
, TRBCCode report
)
1471 XHCIEPContext
*epctx
;
1472 int i
, xferi
, killed
= 0;
1473 USBEndpoint
*ep
= NULL
;
1474 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1475 assert(epid
>= 1 && epid
<= 31);
1477 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
1479 slot
= &xhci
->slots
[slotid
-1];
1481 if (!slot
->eps
[epid
-1]) {
1485 epctx
= slot
->eps
[epid
-1];
1487 xferi
= epctx
->next_xfer
;
1488 for (i
= 0; i
< TD_QUEUE
; i
++) {
1489 killed
+= xhci_ep_nuke_one_xfer(&epctx
->transfers
[xferi
], report
);
1491 report
= 0; /* Only report once */
1493 epctx
->transfers
[xferi
].packet
.ep
= NULL
;
1494 xferi
= (xferi
+ 1) % TD_QUEUE
;
1497 ep
= xhci_epid_to_usbep(xhci
, slotid
, epid
);
1499 usb_device_ep_stopped(ep
->dev
, ep
);
1504 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
1508 XHCIEPContext
*epctx
;
1511 trace_usb_xhci_ep_disable(slotid
, epid
);
1512 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1513 assert(epid
>= 1 && epid
<= 31);
1515 slot
= &xhci
->slots
[slotid
-1];
1517 if (!slot
->eps
[epid
-1]) {
1518 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
1522 xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0);
1524 epctx
= slot
->eps
[epid
-1];
1526 if (epctx
->nr_pstreams
) {
1527 xhci_free_streams(epctx
);
1530 for (i
= 0; i
< ARRAY_SIZE(epctx
->transfers
); i
++) {
1531 usb_packet_cleanup(&epctx
->transfers
[i
].packet
);
1534 /* only touch guest RAM if we're not resetting the HC */
1535 if (xhci
->dcbaap_low
|| xhci
->dcbaap_high
) {
1536 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_DISABLED
);
1539 timer_free(epctx
->kick_timer
);
1541 slot
->eps
[epid
-1] = NULL
;
1546 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
1550 XHCIEPContext
*epctx
;
1552 trace_usb_xhci_ep_stop(slotid
, epid
);
1553 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1555 if (epid
< 1 || epid
> 31) {
1556 DPRINTF("xhci: bad ep %d\n", epid
);
1557 return CC_TRB_ERROR
;
1560 slot
= &xhci
->slots
[slotid
-1];
1562 if (!slot
->eps
[epid
-1]) {
1563 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1564 return CC_EP_NOT_ENABLED_ERROR
;
1567 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, CC_STOPPED
) > 0) {
1568 DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, "
1569 "data might be lost\n");
1572 epctx
= slot
->eps
[epid
-1];
1574 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1576 if (epctx
->nr_pstreams
) {
1577 xhci_reset_streams(epctx
);
1583 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
1587 XHCIEPContext
*epctx
;
1589 trace_usb_xhci_ep_reset(slotid
, epid
);
1590 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1592 if (epid
< 1 || epid
> 31) {
1593 DPRINTF("xhci: bad ep %d\n", epid
);
1594 return CC_TRB_ERROR
;
1597 slot
= &xhci
->slots
[slotid
-1];
1599 if (!slot
->eps
[epid
-1]) {
1600 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1601 return CC_EP_NOT_ENABLED_ERROR
;
1604 epctx
= slot
->eps
[epid
-1];
1606 if (epctx
->state
!= EP_HALTED
) {
1607 DPRINTF("xhci: reset EP while EP %d not halted (%d)\n",
1608 epid
, epctx
->state
);
1609 return CC_CONTEXT_STATE_ERROR
;
1612 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
, 0) > 0) {
1613 DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, "
1614 "data might be lost\n");
1617 if (!xhci
->slots
[slotid
-1].uport
||
1618 !xhci
->slots
[slotid
-1].uport
->dev
||
1619 !xhci
->slots
[slotid
-1].uport
->dev
->attached
) {
1620 return CC_USB_TRANSACTION_ERROR
;
1623 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1625 if (epctx
->nr_pstreams
) {
1626 xhci_reset_streams(epctx
);
1632 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1633 unsigned int epid
, unsigned int streamid
,
1637 XHCIEPContext
*epctx
;
1638 XHCIStreamContext
*sctx
;
1641 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1643 if (epid
< 1 || epid
> 31) {
1644 DPRINTF("xhci: bad ep %d\n", epid
);
1645 return CC_TRB_ERROR
;
1648 trace_usb_xhci_ep_set_dequeue(slotid
, epid
, streamid
, pdequeue
);
1649 dequeue
= xhci_mask64(pdequeue
);
1651 slot
= &xhci
->slots
[slotid
-1];
1653 if (!slot
->eps
[epid
-1]) {
1654 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1655 return CC_EP_NOT_ENABLED_ERROR
;
1658 epctx
= slot
->eps
[epid
-1];
1660 if (epctx
->state
!= EP_STOPPED
) {
1661 DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1662 return CC_CONTEXT_STATE_ERROR
;
1665 if (epctx
->nr_pstreams
) {
1667 sctx
= xhci_find_stream(epctx
, streamid
, &err
);
1671 xhci_ring_init(xhci
, &sctx
->ring
, dequeue
& ~0xf);
1672 sctx
->ring
.ccs
= dequeue
& 1;
1675 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1676 epctx
->ring
.ccs
= dequeue
& 1;
1679 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_STOPPED
);
1684 static int xhci_xfer_create_sgl(XHCITransfer
*xfer
, int in_xfer
)
1686 XHCIState
*xhci
= xfer
->xhci
;
1689 xfer
->int_req
= false;
1690 pci_dma_sglist_init(&xfer
->sgl
, PCI_DEVICE(xhci
), xfer
->trb_count
);
1691 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1692 XHCITRB
*trb
= &xfer
->trbs
[i
];
1694 unsigned int chunk
= 0;
1696 if (trb
->control
& TRB_TR_IOC
) {
1697 xfer
->int_req
= true;
1700 switch (TRB_TYPE(*trb
)) {
1702 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1703 DPRINTF("xhci: data direction mismatch for TR_DATA\n");
1709 addr
= xhci_mask64(trb
->parameter
);
1710 chunk
= trb
->status
& 0x1ffff;
1711 if (trb
->control
& TRB_TR_IDT
) {
1712 if (chunk
> 8 || in_xfer
) {
1713 DPRINTF("xhci: invalid immediate data TRB\n");
1716 qemu_sglist_add(&xfer
->sgl
, trb
->addr
, chunk
);
1718 qemu_sglist_add(&xfer
->sgl
, addr
, chunk
);
1727 qemu_sglist_destroy(&xfer
->sgl
);
1732 static void xhci_xfer_unmap(XHCITransfer
*xfer
)
1734 usb_packet_unmap(&xfer
->packet
, &xfer
->sgl
);
1735 qemu_sglist_destroy(&xfer
->sgl
);
1738 static void xhci_xfer_report(XHCITransfer
*xfer
)
1744 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1745 XHCIState
*xhci
= xfer
->xhci
;
1748 left
= xfer
->packet
.actual_length
;
1750 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1751 XHCITRB
*trb
= &xfer
->trbs
[i
];
1752 unsigned int chunk
= 0;
1754 switch (TRB_TYPE(*trb
)) {
1758 chunk
= trb
->status
& 0x1ffff;
1761 if (xfer
->status
== CC_SUCCESS
) {
1774 if (!reported
&& ((trb
->control
& TRB_TR_IOC
) ||
1775 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)) ||
1776 (xfer
->status
!= CC_SUCCESS
&& left
== 0))) {
1777 event
.slotid
= xfer
->slotid
;
1778 event
.epid
= xfer
->epid
;
1779 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1781 event
.ptr
= trb
->addr
;
1782 if (xfer
->status
== CC_SUCCESS
) {
1783 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1785 event
.ccode
= xfer
->status
;
1787 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1788 event
.ptr
= trb
->parameter
;
1789 event
.flags
|= TRB_EV_ED
;
1790 event
.length
= edtla
& 0xffffff;
1791 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1794 xhci_event(xhci
, &event
, TRB_INTR(*trb
));
1796 if (xfer
->status
!= CC_SUCCESS
) {
1801 switch (TRB_TYPE(*trb
)) {
1811 static void xhci_stall_ep(XHCITransfer
*xfer
)
1813 XHCIState
*xhci
= xfer
->xhci
;
1814 XHCISlot
*slot
= &xhci
->slots
[xfer
->slotid
-1];
1815 XHCIEPContext
*epctx
= slot
->eps
[xfer
->epid
-1];
1817 XHCIStreamContext
*sctx
;
1819 if (epctx
->nr_pstreams
) {
1820 sctx
= xhci_find_stream(epctx
, xfer
->streamid
, &err
);
1824 sctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1825 sctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1826 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_HALTED
);
1828 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1829 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1830 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_HALTED
);
1834 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
,
1835 XHCIEPContext
*epctx
);
1837 static int xhci_setup_packet(XHCITransfer
*xfer
)
1839 XHCIState
*xhci
= xfer
->xhci
;
1843 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1845 if (xfer
->packet
.ep
) {
1846 ep
= xfer
->packet
.ep
;
1848 ep
= xhci_epid_to_usbep(xhci
, xfer
->slotid
, xfer
->epid
);
1850 DPRINTF("xhci: slot %d has no device\n",
1856 xhci_xfer_create_sgl(xfer
, dir
== USB_TOKEN_IN
); /* Also sets int_req */
1857 usb_packet_setup(&xfer
->packet
, dir
, ep
, xfer
->streamid
,
1858 xfer
->trbs
[0].addr
, false, xfer
->int_req
);
1859 usb_packet_map(&xfer
->packet
, &xfer
->sgl
);
1860 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1861 xfer
->packet
.pid
, ep
->dev
->addr
, ep
->nr
);
1865 static int xhci_complete_packet(XHCITransfer
*xfer
)
1867 if (xfer
->packet
.status
== USB_RET_ASYNC
) {
1868 trace_usb_xhci_xfer_async(xfer
);
1869 xfer
->running_async
= 1;
1870 xfer
->running_retry
= 0;
1873 } else if (xfer
->packet
.status
== USB_RET_NAK
) {
1874 trace_usb_xhci_xfer_nak(xfer
);
1875 xfer
->running_async
= 0;
1876 xfer
->running_retry
= 1;
1880 xfer
->running_async
= 0;
1881 xfer
->running_retry
= 0;
1883 xhci_xfer_unmap(xfer
);
1886 if (xfer
->packet
.status
== USB_RET_SUCCESS
) {
1887 trace_usb_xhci_xfer_success(xfer
, xfer
->packet
.actual_length
);
1888 xfer
->status
= CC_SUCCESS
;
1889 xhci_xfer_report(xfer
);
1894 trace_usb_xhci_xfer_error(xfer
, xfer
->packet
.status
);
1895 switch (xfer
->packet
.status
) {
1897 case USB_RET_IOERROR
:
1898 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1899 xhci_xfer_report(xfer
);
1900 xhci_stall_ep(xfer
);
1903 xfer
->status
= CC_STALL_ERROR
;
1904 xhci_xfer_report(xfer
);
1905 xhci_stall_ep(xfer
);
1907 case USB_RET_BABBLE
:
1908 xfer
->status
= CC_BABBLE_DETECTED
;
1909 xhci_xfer_report(xfer
);
1910 xhci_stall_ep(xfer
);
1913 DPRINTF("%s: FIXME: status = %d\n", __func__
,
1914 xfer
->packet
.status
);
1915 FIXME("unhandled USB_RET_*");
1920 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1922 XHCITRB
*trb_setup
, *trb_status
;
1923 uint8_t bmRequestType
;
1925 trb_setup
= &xfer
->trbs
[0];
1926 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1928 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
1930 /* at most one Event Data TRB allowed after STATUS */
1931 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1935 /* do some sanity checks */
1936 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1937 DPRINTF("xhci: ep0 first TD not SETUP: %d\n",
1938 TRB_TYPE(*trb_setup
));
1941 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1942 DPRINTF("xhci: ep0 last TD not STATUS: %d\n",
1943 TRB_TYPE(*trb_status
));
1946 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1947 DPRINTF("xhci: Setup TRB doesn't have IDT set\n");
1950 if ((trb_setup
->status
& 0x1ffff) != 8) {
1951 DPRINTF("xhci: Setup TRB has bad length (%d)\n",
1952 (trb_setup
->status
& 0x1ffff));
1956 bmRequestType
= trb_setup
->parameter
;
1958 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1959 xfer
->iso_xfer
= false;
1960 xfer
->timed_xfer
= false;
1962 if (xhci_setup_packet(xfer
) < 0) {
1965 xfer
->packet
.parameter
= trb_setup
->parameter
;
1967 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1969 xhci_complete_packet(xfer
);
1970 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1971 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
, 0);
1976 static void xhci_calc_intr_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1977 XHCIEPContext
*epctx
, uint64_t mfindex
)
1979 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1980 ~(epctx
->interval
-1));
1981 uint64_t kick
= epctx
->mfindex_last
+ epctx
->interval
;
1983 assert(epctx
->interval
!= 0);
1984 xfer
->mfindex_kick
= MAX(asap
, kick
);
1987 static void xhci_calc_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1988 XHCIEPContext
*epctx
, uint64_t mfindex
)
1990 if (xfer
->trbs
[0].control
& TRB_TR_SIA
) {
1991 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1992 ~(epctx
->interval
-1));
1993 if (asap
>= epctx
->mfindex_last
&&
1994 asap
<= epctx
->mfindex_last
+ epctx
->interval
* 4) {
1995 xfer
->mfindex_kick
= epctx
->mfindex_last
+ epctx
->interval
;
1997 xfer
->mfindex_kick
= asap
;
2000 xfer
->mfindex_kick
= ((xfer
->trbs
[0].control
>> TRB_TR_FRAMEID_SHIFT
)
2001 & TRB_TR_FRAMEID_MASK
) << 3;
2002 xfer
->mfindex_kick
|= mfindex
& ~0x3fff;
2003 if (xfer
->mfindex_kick
+ 0x100 < mfindex
) {
2004 xfer
->mfindex_kick
+= 0x4000;
2009 static void xhci_check_intr_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
2010 XHCIEPContext
*epctx
, uint64_t mfindex
)
2012 if (xfer
->mfindex_kick
> mfindex
) {
2013 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
2014 (xfer
->mfindex_kick
- mfindex
) * 125000);
2015 xfer
->running_retry
= 1;
2017 epctx
->mfindex_last
= xfer
->mfindex_kick
;
2018 timer_del(epctx
->kick_timer
);
2019 xfer
->running_retry
= 0;
2024 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
2028 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer
->slotid
, xfer
->epid
);
2030 xfer
->in_xfer
= epctx
->type
>>2;
2032 switch(epctx
->type
) {
2036 xfer
->iso_xfer
= false;
2037 xfer
->timed_xfer
= true;
2038 mfindex
= xhci_mfindex_get(xhci
);
2039 xhci_calc_intr_kick(xhci
, xfer
, epctx
, mfindex
);
2040 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
2041 if (xfer
->running_retry
) {
2048 xfer
->iso_xfer
= false;
2049 xfer
->timed_xfer
= false;
2054 xfer
->iso_xfer
= true;
2055 xfer
->timed_xfer
= true;
2056 mfindex
= xhci_mfindex_get(xhci
);
2057 xhci_calc_iso_kick(xhci
, xfer
, epctx
, mfindex
);
2058 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
2059 if (xfer
->running_retry
) {
2064 trace_usb_xhci_unimplemented("endpoint type", epctx
->type
);
2068 if (xhci_setup_packet(xfer
) < 0) {
2071 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
2073 xhci_complete_packet(xfer
);
2074 if (!xfer
->running_async
&& !xfer
->running_retry
) {
2075 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
2080 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
2082 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
2083 return xhci_submit(xhci
, xfer
, epctx
);
2086 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
2087 unsigned int epid
, unsigned int streamid
)
2089 XHCIStreamContext
*stctx
;
2090 XHCIEPContext
*epctx
;
2092 USBEndpoint
*ep
= NULL
;
2097 trace_usb_xhci_ep_kick(slotid
, epid
, streamid
);
2098 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2099 assert(epid
>= 1 && epid
<= 31);
2101 if (!xhci
->slots
[slotid
-1].enabled
) {
2102 DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
2105 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
2107 DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
2112 /* If the device has been detached, but the guest has not noticed this
2113 yet the 2 above checks will succeed, but we must NOT continue */
2114 if (!xhci
->slots
[slotid
- 1].uport
||
2115 !xhci
->slots
[slotid
- 1].uport
->dev
||
2116 !xhci
->slots
[slotid
- 1].uport
->dev
->attached
) {
2121 XHCITransfer
*xfer
= epctx
->retry
;
2123 trace_usb_xhci_xfer_retry(xfer
);
2124 assert(xfer
->running_retry
);
2125 if (xfer
->timed_xfer
) {
2126 /* time to kick the transfer? */
2127 mfindex
= xhci_mfindex_get(xhci
);
2128 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
2129 if (xfer
->running_retry
) {
2132 xfer
->timed_xfer
= 0;
2133 xfer
->running_retry
= 1;
2135 if (xfer
->iso_xfer
) {
2136 /* retry iso transfer */
2137 if (xhci_setup_packet(xfer
) < 0) {
2140 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
2141 assert(xfer
->packet
.status
!= USB_RET_NAK
);
2142 xhci_complete_packet(xfer
);
2144 /* retry nak'ed transfer */
2145 if (xhci_setup_packet(xfer
) < 0) {
2148 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
2149 if (xfer
->packet
.status
== USB_RET_NAK
) {
2152 xhci_complete_packet(xfer
);
2154 assert(!xfer
->running_retry
);
2155 epctx
->retry
= NULL
;
2158 if (epctx
->state
== EP_HALTED
) {
2159 DPRINTF("xhci: ep halted, not running schedule\n");
2164 if (epctx
->nr_pstreams
) {
2166 stctx
= xhci_find_stream(epctx
, streamid
, &err
);
2167 if (stctx
== NULL
) {
2170 ring
= &stctx
->ring
;
2171 xhci_set_ep_state(xhci
, epctx
, stctx
, EP_RUNNING
);
2173 ring
= &epctx
->ring
;
2175 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_RUNNING
);
2177 assert(ring
->dequeue
!= 0);
2180 XHCITransfer
*xfer
= &epctx
->transfers
[epctx
->next_xfer
];
2181 if (xfer
->running_async
|| xfer
->running_retry
) {
2184 length
= xhci_ring_chain_length(xhci
, ring
);
2187 } else if (length
== 0) {
2190 if (xfer
->trbs
&& xfer
->trb_alloced
< length
) {
2191 xfer
->trb_count
= 0;
2192 xfer
->trb_alloced
= 0;
2197 xfer
->trbs
= g_new(XHCITRB
, length
);
2198 xfer
->trb_alloced
= length
;
2200 xfer
->trb_count
= length
;
2202 for (i
= 0; i
< length
; i
++) {
2203 assert(xhci_ring_fetch(xhci
, ring
, &xfer
->trbs
[i
], NULL
));
2205 xfer
->streamid
= streamid
;
2208 if (xhci_fire_ctl_transfer(xhci
, xfer
) >= 0) {
2209 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
2211 DPRINTF("xhci: error firing CTL transfer\n");
2214 if (xhci_fire_transfer(xhci
, xfer
, epctx
) >= 0) {
2215 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
2217 if (!xfer
->timed_xfer
) {
2218 DPRINTF("xhci: error firing data transfer\n");
2223 if (epctx
->state
== EP_HALTED
) {
2226 if (xfer
->running_retry
) {
2227 DPRINTF("xhci: xfer nacked, stopping schedule\n");
2228 epctx
->retry
= xfer
;
2233 ep
= xhci_epid_to_usbep(xhci
, slotid
, epid
);
2235 usb_device_flush_ep_queue(ep
->dev
, ep
);
2239 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
2241 trace_usb_xhci_slot_enable(slotid
);
2242 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2243 xhci
->slots
[slotid
-1].enabled
= 1;
2244 xhci
->slots
[slotid
-1].uport
= NULL
;
2245 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
2250 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
2254 trace_usb_xhci_slot_disable(slotid
);
2255 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2257 for (i
= 1; i
<= 31; i
++) {
2258 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2259 xhci_disable_ep(xhci
, slotid
, i
);
2263 xhci
->slots
[slotid
-1].enabled
= 0;
2264 xhci
->slots
[slotid
-1].addressed
= 0;
2265 xhci
->slots
[slotid
-1].uport
= NULL
;
2269 static USBPort
*xhci_lookup_uport(XHCIState
*xhci
, uint32_t *slot_ctx
)
2275 port
= (slot_ctx
[1]>>16) & 0xFF;
2276 if (port
< 1 || port
> xhci
->numports
) {
2279 port
= xhci
->ports
[port
-1].uport
->index
+1;
2280 pos
= snprintf(path
, sizeof(path
), "%d", port
);
2281 for (i
= 0; i
< 5; i
++) {
2282 port
= (slot_ctx
[0] >> 4*i
) & 0x0f;
2286 pos
+= snprintf(path
+ pos
, sizeof(path
) - pos
, ".%d", port
);
2289 QTAILQ_FOREACH(uport
, &xhci
->bus
.used
, next
) {
2290 if (strcmp(uport
->path
, path
) == 0) {
2297 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
2298 uint64_t pictx
, bool bsr
)
2303 dma_addr_t ictx
, octx
, dcbaap
;
2305 uint32_t ictl_ctx
[2];
2306 uint32_t slot_ctx
[4];
2307 uint32_t ep0_ctx
[5];
2311 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2313 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
2314 poctx
= ldq_le_pci_dma(PCI_DEVICE(xhci
), dcbaap
+ 8 * slotid
);
2315 ictx
= xhci_mask64(pictx
);
2316 octx
= xhci_mask64(poctx
);
2318 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2319 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2321 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2323 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
2324 DPRINTF("xhci: invalid input context control %08x %08x\n",
2325 ictl_ctx
[0], ictl_ctx
[1]);
2326 return CC_TRB_ERROR
;
2329 xhci_dma_read_u32s(xhci
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
2330 xhci_dma_read_u32s(xhci
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
2332 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2333 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2335 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2336 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2338 uport
= xhci_lookup_uport(xhci
, slot_ctx
);
2339 if (uport
== NULL
) {
2340 DPRINTF("xhci: port not found\n");
2341 return CC_TRB_ERROR
;
2343 trace_usb_xhci_slot_address(slotid
, uport
->path
);
2346 if (!dev
|| !dev
->attached
) {
2347 DPRINTF("xhci: port %s not connected\n", uport
->path
);
2348 return CC_USB_TRANSACTION_ERROR
;
2351 for (i
= 0; i
< xhci
->numslots
; i
++) {
2352 if (i
== slotid
-1) {
2355 if (xhci
->slots
[i
].uport
== uport
) {
2356 DPRINTF("xhci: port %s already assigned to slot %d\n",
2358 return CC_TRB_ERROR
;
2362 slot
= &xhci
->slots
[slotid
-1];
2363 slot
->uport
= uport
;
2367 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2372 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slotid
;
2373 usb_device_reset(dev
);
2374 memset(&p
, 0, sizeof(p
));
2375 usb_packet_addbuf(&p
, buf
, sizeof(buf
));
2376 usb_packet_setup(&p
, USB_TOKEN_OUT
,
2377 usb_ep_get(dev
, USB_TOKEN_OUT
, 0), 0,
2379 usb_device_handle_control(dev
, &p
,
2380 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
2381 slotid
, 0, 0, NULL
);
2382 assert(p
.status
!= USB_RET_ASYNC
);
2385 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
2387 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2388 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2389 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2390 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2392 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2393 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2395 xhci
->slots
[slotid
-1].addressed
= 1;
2400 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
2401 uint64_t pictx
, bool dc
)
2403 dma_addr_t ictx
, octx
;
2404 uint32_t ictl_ctx
[2];
2405 uint32_t slot_ctx
[4];
2406 uint32_t islot_ctx
[4];
2411 trace_usb_xhci_slot_configure(slotid
);
2412 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2414 ictx
= xhci_mask64(pictx
);
2415 octx
= xhci
->slots
[slotid
-1].ctx
;
2417 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2418 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2421 for (i
= 2; i
<= 31; i
++) {
2422 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2423 xhci_disable_ep(xhci
, slotid
, i
);
2427 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2428 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2429 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
2430 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2431 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2432 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2437 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2439 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
2440 DPRINTF("xhci: invalid input context control %08x %08x\n",
2441 ictl_ctx
[0], ictl_ctx
[1]);
2442 return CC_TRB_ERROR
;
2445 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2446 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2448 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
2449 DPRINTF("xhci: invalid slot state %08x\n", slot_ctx
[3]);
2450 return CC_CONTEXT_STATE_ERROR
;
2453 xhci_free_device_streams(xhci
, slotid
, ictl_ctx
[0] | ictl_ctx
[1]);
2455 for (i
= 2; i
<= 31; i
++) {
2456 if (ictl_ctx
[0] & (1<<i
)) {
2457 xhci_disable_ep(xhci
, slotid
, i
);
2459 if (ictl_ctx
[1] & (1<<i
)) {
2460 xhci_dma_read_u32s(xhci
, ictx
+32+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2461 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
2462 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2463 ep_ctx
[3], ep_ctx
[4]);
2464 xhci_disable_ep(xhci
, slotid
, i
);
2465 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
2466 if (res
!= CC_SUCCESS
) {
2469 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
2470 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2471 ep_ctx
[3], ep_ctx
[4]);
2472 xhci_dma_write_u32s(xhci
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2476 res
= xhci_alloc_device_streams(xhci
, slotid
, ictl_ctx
[1]);
2477 if (res
!= CC_SUCCESS
) {
2478 for (i
= 2; i
<= 31; i
++) {
2479 if (ictl_ctx
[1] & (1u << i
)) {
2480 xhci_disable_ep(xhci
, slotid
, i
);
2486 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2487 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
2488 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
2489 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
2490 SLOT_CONTEXT_ENTRIES_SHIFT
);
2491 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2492 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2494 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2500 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
2503 dma_addr_t ictx
, octx
;
2504 uint32_t ictl_ctx
[2];
2505 uint32_t iep0_ctx
[5];
2506 uint32_t ep0_ctx
[5];
2507 uint32_t islot_ctx
[4];
2508 uint32_t slot_ctx
[4];
2510 trace_usb_xhci_slot_evaluate(slotid
);
2511 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2513 ictx
= xhci_mask64(pictx
);
2514 octx
= xhci
->slots
[slotid
-1].ctx
;
2516 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2517 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2519 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2521 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
2522 DPRINTF("xhci: invalid input context control %08x %08x\n",
2523 ictl_ctx
[0], ictl_ctx
[1]);
2524 return CC_TRB_ERROR
;
2527 if (ictl_ctx
[1] & 0x1) {
2528 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2530 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2531 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
2533 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2535 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
2536 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
2537 slot_ctx
[2] &= ~0xFF00000; /* interrupter target */
2538 slot_ctx
[2] |= islot_ctx
[2] & 0xFF000000;
2540 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2541 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2543 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2546 if (ictl_ctx
[1] & 0x2) {
2547 xhci_dma_read_u32s(xhci
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
2549 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2550 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
2551 iep0_ctx
[3], iep0_ctx
[4]);
2553 xhci_dma_read_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2555 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
2556 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
2558 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2559 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2561 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2567 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
2569 uint32_t slot_ctx
[4];
2573 trace_usb_xhci_slot_reset(slotid
);
2574 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2576 octx
= xhci
->slots
[slotid
-1].ctx
;
2578 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2580 for (i
= 2; i
<= 31; i
++) {
2581 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2582 xhci_disable_ep(xhci
, slotid
, i
);
2586 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2587 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2588 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2589 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2590 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2591 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2596 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
2598 unsigned int slotid
;
2599 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
2600 if (slotid
< 1 || slotid
> xhci
->numslots
) {
2601 DPRINTF("xhci: bad slot id %d\n", slotid
);
2602 event
->ccode
= CC_TRB_ERROR
;
2604 } else if (!xhci
->slots
[slotid
-1].enabled
) {
2605 DPRINTF("xhci: slot id %d not enabled\n", slotid
);
2606 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
2612 /* cleanup slot state on usb device detach */
2613 static void xhci_detach_slot(XHCIState
*xhci
, USBPort
*uport
)
2617 for (slot
= 0; slot
< xhci
->numslots
; slot
++) {
2618 if (xhci
->slots
[slot
].uport
== uport
) {
2622 if (slot
== xhci
->numslots
) {
2626 for (ep
= 0; ep
< 31; ep
++) {
2627 if (xhci
->slots
[slot
].eps
[ep
]) {
2628 xhci_ep_nuke_xfers(xhci
, slot
+ 1, ep
+ 1, 0);
2631 xhci
->slots
[slot
].uport
= NULL
;
2634 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
2637 uint8_t bw_ctx
[xhci
->numports
+1];
2639 DPRINTF("xhci_get_port_bandwidth()\n");
2641 ctx
= xhci_mask64(pctx
);
2643 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
2645 /* TODO: actually implement real values here */
2647 memset(&bw_ctx
[1], 80, xhci
->numports
); /* 80% */
2648 pci_dma_write(PCI_DEVICE(xhci
), ctx
, bw_ctx
, sizeof(bw_ctx
));
2653 static uint32_t rotl(uint32_t v
, unsigned count
)
2656 return (v
<< count
) | (v
>> (32 - count
));
2660 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2663 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2664 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2665 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2669 static void xhci_via_challenge(XHCIState
*xhci
, uint64_t addr
)
2671 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
2674 dma_addr_t paddr
= xhci_mask64(addr
);
2676 pci_dma_read(pci_dev
, paddr
, &buf
, 32);
2678 memcpy(obuf
, buf
, sizeof(obuf
));
2680 if ((buf
[0] & 0xff) == 2) {
2681 obuf
[0] = 0x49932000 + 0x54dc200 * buf
[2] + 0x7429b578 * buf
[3];
2682 obuf
[0] |= (buf
[2] * buf
[3]) & 0xff;
2683 obuf
[1] = 0x0132bb37 + 0xe89 * buf
[2] + 0xf09 * buf
[3];
2684 obuf
[2] = 0x0066c2e9 + 0x2091 * buf
[2] + 0x19bd * buf
[3];
2685 obuf
[3] = 0xd5281342 + 0x2cc9691 * buf
[2] + 0x2367662 * buf
[3];
2686 obuf
[4] = 0x0123c75c + 0x1595 * buf
[2] + 0x19ec * buf
[3];
2687 obuf
[5] = 0x00f695de + 0x26fd * buf
[2] + 0x3e9 * buf
[3];
2688 obuf
[6] = obuf
[2] ^ obuf
[3] ^ 0x29472956;
2689 obuf
[7] = obuf
[2] ^ obuf
[3] ^ 0x65866593;
2692 pci_dma_write(pci_dev
, paddr
, &obuf
, 32);
2695 static void xhci_process_commands(XHCIState
*xhci
)
2699 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2701 unsigned int i
, slotid
= 0;
2703 DPRINTF("xhci_process_commands()\n");
2704 if (!xhci_running(xhci
)) {
2705 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2709 xhci
->crcr_low
|= CRCR_CRR
;
2711 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2714 case CR_ENABLE_SLOT
:
2715 for (i
= 0; i
< xhci
->numslots
; i
++) {
2716 if (!xhci
->slots
[i
].enabled
) {
2720 if (i
>= xhci
->numslots
) {
2721 DPRINTF("xhci: no device slots available\n");
2722 event
.ccode
= CC_NO_SLOTS_ERROR
;
2725 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2728 case CR_DISABLE_SLOT
:
2729 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2731 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2734 case CR_ADDRESS_DEVICE
:
2735 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2737 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2738 trb
.control
& TRB_CR_BSR
);
2741 case CR_CONFIGURE_ENDPOINT
:
2742 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2744 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2745 trb
.control
& TRB_CR_DC
);
2748 case CR_EVALUATE_CONTEXT
:
2749 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2751 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2754 case CR_STOP_ENDPOINT
:
2755 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2757 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2759 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2762 case CR_RESET_ENDPOINT
:
2763 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2765 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2767 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2770 case CR_SET_TR_DEQUEUE
:
2771 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2773 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2775 unsigned int streamid
= (trb
.status
>> 16) & 0xffff;
2776 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
,
2781 case CR_RESET_DEVICE
:
2782 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2784 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2787 case CR_GET_PORT_BANDWIDTH
:
2788 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2790 case CR_VENDOR_VIA_CHALLENGE_RESPONSE
:
2791 xhci_via_challenge(xhci
, trb
.parameter
);
2793 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2794 event
.type
= 48; /* NEC reply */
2795 event
.length
= 0x3025;
2797 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2799 uint32_t chi
= trb
.parameter
>> 32;
2800 uint32_t clo
= trb
.parameter
;
2801 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2802 event
.length
= val
& 0xFFFF;
2803 event
.epid
= val
>> 16;
2805 event
.type
= 48; /* NEC reply */
2809 trace_usb_xhci_unimplemented("command", type
);
2810 event
.ccode
= CC_TRB_ERROR
;
2813 event
.slotid
= slotid
;
2814 xhci_event(xhci
, &event
, 0);
2818 static bool xhci_port_have_device(XHCIPort
*port
)
2820 if (!port
->uport
->dev
|| !port
->uport
->dev
->attached
) {
2821 return false; /* no device present */
2823 if (!((1 << port
->uport
->dev
->speed
) & port
->speedmask
)) {
2824 return false; /* speed mismatch */
2829 static void xhci_port_notify(XHCIPort
*port
, uint32_t bits
)
2831 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
,
2832 port
->portnr
<< 24 };
2834 if ((port
->portsc
& bits
) == bits
) {
2837 trace_usb_xhci_port_notify(port
->portnr
, bits
);
2838 port
->portsc
|= bits
;
2839 if (!xhci_running(port
->xhci
)) {
2842 xhci_event(port
->xhci
, &ev
, 0);
2845 static void xhci_port_update(XHCIPort
*port
, int is_detach
)
2847 uint32_t pls
= PLS_RX_DETECT
;
2849 port
->portsc
= PORTSC_PP
;
2850 if (!is_detach
&& xhci_port_have_device(port
)) {
2851 port
->portsc
|= PORTSC_CCS
;
2852 switch (port
->uport
->dev
->speed
) {
2854 port
->portsc
|= PORTSC_SPEED_LOW
;
2857 case USB_SPEED_FULL
:
2858 port
->portsc
|= PORTSC_SPEED_FULL
;
2861 case USB_SPEED_HIGH
:
2862 port
->portsc
|= PORTSC_SPEED_HIGH
;
2865 case USB_SPEED_SUPER
:
2866 port
->portsc
|= PORTSC_SPEED_SUPER
;
2867 port
->portsc
|= PORTSC_PED
;
2872 set_field(&port
->portsc
, pls
, PORTSC_PLS
);
2873 trace_usb_xhci_port_link(port
->portnr
, pls
);
2874 xhci_port_notify(port
, PORTSC_CSC
);
2877 static void xhci_port_reset(XHCIPort
*port
, bool warm_reset
)
2879 trace_usb_xhci_port_reset(port
->portnr
, warm_reset
);
2881 if (!xhci_port_have_device(port
)) {
2885 usb_device_reset(port
->uport
->dev
);
2887 switch (port
->uport
->dev
->speed
) {
2888 case USB_SPEED_SUPER
:
2890 port
->portsc
|= PORTSC_WRC
;
2894 case USB_SPEED_FULL
:
2895 case USB_SPEED_HIGH
:
2896 set_field(&port
->portsc
, PLS_U0
, PORTSC_PLS
);
2897 trace_usb_xhci_port_link(port
->portnr
, PLS_U0
);
2898 port
->portsc
|= PORTSC_PED
;
2902 port
->portsc
&= ~PORTSC_PR
;
2903 xhci_port_notify(port
, PORTSC_PRC
);
2906 static void xhci_reset(DeviceState
*dev
)
2908 XHCIState
*xhci
= XHCI(dev
);
2911 trace_usb_xhci_reset();
2912 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2913 DPRINTF("xhci: reset while running!\n");
2917 xhci
->usbsts
= USBSTS_HCH
;
2920 xhci
->crcr_high
= 0;
2921 xhci
->dcbaap_low
= 0;
2922 xhci
->dcbaap_high
= 0;
2925 for (i
= 0; i
< xhci
->numslots
; i
++) {
2926 xhci_disable_slot(xhci
, i
+1);
2929 for (i
= 0; i
< xhci
->numports
; i
++) {
2930 xhci_port_update(xhci
->ports
+ i
, 0);
2933 for (i
= 0; i
< xhci
->numintrs
; i
++) {
2934 xhci
->intr
[i
].iman
= 0;
2935 xhci
->intr
[i
].imod
= 0;
2936 xhci
->intr
[i
].erstsz
= 0;
2937 xhci
->intr
[i
].erstba_low
= 0;
2938 xhci
->intr
[i
].erstba_high
= 0;
2939 xhci
->intr
[i
].erdp_low
= 0;
2940 xhci
->intr
[i
].erdp_high
= 0;
2941 xhci
->intr
[i
].msix_used
= 0;
2943 xhci
->intr
[i
].er_ep_idx
= 0;
2944 xhci
->intr
[i
].er_pcs
= 1;
2945 xhci
->intr
[i
].er_full
= 0;
2946 xhci
->intr
[i
].ev_buffer_put
= 0;
2947 xhci
->intr
[i
].ev_buffer_get
= 0;
2950 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2951 xhci_mfwrap_update(xhci
);
2954 static uint64_t xhci_cap_read(void *ptr
, hwaddr reg
, unsigned size
)
2956 XHCIState
*xhci
= ptr
;
2960 case 0x00: /* HCIVERSION, CAPLENGTH */
2961 ret
= 0x01000000 | LEN_CAP
;
2963 case 0x04: /* HCSPARAMS 1 */
2964 ret
= ((xhci
->numports_2
+xhci
->numports_3
)<<24)
2965 | (xhci
->numintrs
<<8) | xhci
->numslots
;
2967 case 0x08: /* HCSPARAMS 2 */
2970 case 0x0c: /* HCSPARAMS 3 */
2973 case 0x10: /* HCCPARAMS */
2974 if (sizeof(dma_addr_t
) == 4) {
2975 ret
= 0x00080000 | (xhci
->max_pstreams_mask
<< 12);
2977 ret
= 0x00080001 | (xhci
->max_pstreams_mask
<< 12);
2980 case 0x14: /* DBOFF */
2983 case 0x18: /* RTSOFF */
2987 /* extended capabilities */
2988 case 0x20: /* Supported Protocol:00 */
2989 ret
= 0x02000402; /* USB 2.0 */
2991 case 0x24: /* Supported Protocol:04 */
2992 ret
= 0x20425355; /* "USB " */
2994 case 0x28: /* Supported Protocol:08 */
2995 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
2996 ret
= (xhci
->numports_2
<<8) | (xhci
->numports_3
+1);
2998 ret
= (xhci
->numports_2
<<8) | 1;
3001 case 0x2c: /* Supported Protocol:0c */
3002 ret
= 0x00000000; /* reserved */
3004 case 0x30: /* Supported Protocol:00 */
3005 ret
= 0x03000002; /* USB 3.0 */
3007 case 0x34: /* Supported Protocol:04 */
3008 ret
= 0x20425355; /* "USB " */
3010 case 0x38: /* Supported Protocol:08 */
3011 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3012 ret
= (xhci
->numports_3
<<8) | 1;
3014 ret
= (xhci
->numports_3
<<8) | (xhci
->numports_2
+1);
3017 case 0x3c: /* Supported Protocol:0c */
3018 ret
= 0x00000000; /* reserved */
3021 trace_usb_xhci_unimplemented("cap read", reg
);
3025 trace_usb_xhci_cap_read(reg
, ret
);
3029 static uint64_t xhci_port_read(void *ptr
, hwaddr reg
, unsigned size
)
3031 XHCIPort
*port
= ptr
;
3035 case 0x00: /* PORTSC */
3038 case 0x04: /* PORTPMSC */
3039 case 0x08: /* PORTLI */
3042 case 0x0c: /* reserved */
3044 trace_usb_xhci_unimplemented("port read", reg
);
3048 trace_usb_xhci_port_read(port
->portnr
, reg
, ret
);
3052 static void xhci_port_write(void *ptr
, hwaddr reg
,
3053 uint64_t val
, unsigned size
)
3055 XHCIPort
*port
= ptr
;
3056 uint32_t portsc
, notify
;
3058 trace_usb_xhci_port_write(port
->portnr
, reg
, val
);
3061 case 0x00: /* PORTSC */
3062 /* write-1-to-start bits */
3063 if (val
& PORTSC_WPR
) {
3064 xhci_port_reset(port
, true);
3067 if (val
& PORTSC_PR
) {
3068 xhci_port_reset(port
, false);
3072 portsc
= port
->portsc
;
3074 /* write-1-to-clear bits*/
3075 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
3076 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
3077 if (val
& PORTSC_LWS
) {
3078 /* overwrite PLS only when LWS=1 */
3079 uint32_t old_pls
= get_field(port
->portsc
, PORTSC_PLS
);
3080 uint32_t new_pls
= get_field(val
, PORTSC_PLS
);
3083 if (old_pls
!= PLS_U0
) {
3084 set_field(&portsc
, new_pls
, PORTSC_PLS
);
3085 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
3086 notify
= PORTSC_PLC
;
3090 if (old_pls
< PLS_U3
) {
3091 set_field(&portsc
, new_pls
, PORTSC_PLS
);
3092 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
3096 /* windows does this for some reason, don't spam stderr */
3099 DPRINTF("%s: ignore pls write (old %d, new %d)\n",
3100 __func__
, old_pls
, new_pls
);
3104 /* read/write bits */
3105 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
3106 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
3107 port
->portsc
= portsc
;
3109 xhci_port_notify(port
, notify
);
3112 case 0x04: /* PORTPMSC */
3113 case 0x08: /* PORTLI */
3115 trace_usb_xhci_unimplemented("port write", reg
);
3119 static uint64_t xhci_oper_read(void *ptr
, hwaddr reg
, unsigned size
)
3121 XHCIState
*xhci
= ptr
;
3125 case 0x00: /* USBCMD */
3128 case 0x04: /* USBSTS */
3131 case 0x08: /* PAGESIZE */
3134 case 0x14: /* DNCTRL */
3137 case 0x18: /* CRCR low */
3138 ret
= xhci
->crcr_low
& ~0xe;
3140 case 0x1c: /* CRCR high */
3141 ret
= xhci
->crcr_high
;
3143 case 0x30: /* DCBAAP low */
3144 ret
= xhci
->dcbaap_low
;
3146 case 0x34: /* DCBAAP high */
3147 ret
= xhci
->dcbaap_high
;
3149 case 0x38: /* CONFIG */
3153 trace_usb_xhci_unimplemented("oper read", reg
);
3157 trace_usb_xhci_oper_read(reg
, ret
);
3161 static void xhci_oper_write(void *ptr
, hwaddr reg
,
3162 uint64_t val
, unsigned size
)
3164 XHCIState
*xhci
= ptr
;
3165 DeviceState
*d
= DEVICE(ptr
);
3167 trace_usb_xhci_oper_write(reg
, val
);
3170 case 0x00: /* USBCMD */
3171 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
3173 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
3176 if (val
& USBCMD_CSS
) {
3178 xhci
->usbsts
&= ~USBSTS_SRE
;
3180 if (val
& USBCMD_CRS
) {
3182 xhci
->usbsts
|= USBSTS_SRE
;
3184 xhci
->usbcmd
= val
& 0xc0f;
3185 xhci_mfwrap_update(xhci
);
3186 if (val
& USBCMD_HCRST
) {
3189 xhci_intx_update(xhci
);
3192 case 0x04: /* USBSTS */
3193 /* these bits are write-1-to-clear */
3194 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
3195 xhci_intx_update(xhci
);
3198 case 0x14: /* DNCTRL */
3199 xhci
->dnctrl
= val
& 0xffff;
3201 case 0x18: /* CRCR low */
3202 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
3204 case 0x1c: /* CRCR high */
3205 xhci
->crcr_high
= val
;
3206 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
3207 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
3208 xhci
->crcr_low
&= ~CRCR_CRR
;
3209 xhci_event(xhci
, &event
, 0);
3210 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
3212 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
3213 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
3215 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
3217 case 0x30: /* DCBAAP low */
3218 xhci
->dcbaap_low
= val
& 0xffffffc0;
3220 case 0x34: /* DCBAAP high */
3221 xhci
->dcbaap_high
= val
;
3223 case 0x38: /* CONFIG */
3224 xhci
->config
= val
& 0xff;
3227 trace_usb_xhci_unimplemented("oper write", reg
);
3231 static uint64_t xhci_runtime_read(void *ptr
, hwaddr reg
,
3234 XHCIState
*xhci
= ptr
;
3239 case 0x00: /* MFINDEX */
3240 ret
= xhci_mfindex_get(xhci
) & 0x3fff;
3243 trace_usb_xhci_unimplemented("runtime read", reg
);
3247 int v
= (reg
- 0x20) / 0x20;
3248 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
3249 switch (reg
& 0x1f) {
3250 case 0x00: /* IMAN */
3253 case 0x04: /* IMOD */
3256 case 0x08: /* ERSTSZ */
3259 case 0x10: /* ERSTBA low */
3260 ret
= intr
->erstba_low
;
3262 case 0x14: /* ERSTBA high */
3263 ret
= intr
->erstba_high
;
3265 case 0x18: /* ERDP low */
3266 ret
= intr
->erdp_low
;
3268 case 0x1c: /* ERDP high */
3269 ret
= intr
->erdp_high
;
3274 trace_usb_xhci_runtime_read(reg
, ret
);
3278 static void xhci_runtime_write(void *ptr
, hwaddr reg
,
3279 uint64_t val
, unsigned size
)
3281 XHCIState
*xhci
= ptr
;
3282 int v
= (reg
- 0x20) / 0x20;
3283 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
3284 trace_usb_xhci_runtime_write(reg
, val
);
3287 trace_usb_xhci_unimplemented("runtime write", reg
);
3291 switch (reg
& 0x1f) {
3292 case 0x00: /* IMAN */
3293 if (val
& IMAN_IP
) {
3294 intr
->iman
&= ~IMAN_IP
;
3296 intr
->iman
&= ~IMAN_IE
;
3297 intr
->iman
|= val
& IMAN_IE
;
3299 xhci_intx_update(xhci
);
3301 xhci_msix_update(xhci
, v
);
3303 case 0x04: /* IMOD */
3306 case 0x08: /* ERSTSZ */
3307 intr
->erstsz
= val
& 0xffff;
3309 case 0x10: /* ERSTBA low */
3310 /* XXX NEC driver bug: it doesn't align this to 64 bytes
3311 intr->erstba_low = val & 0xffffffc0; */
3312 intr
->erstba_low
= val
& 0xfffffff0;
3314 case 0x14: /* ERSTBA high */
3315 intr
->erstba_high
= val
;
3316 xhci_er_reset(xhci
, v
);
3318 case 0x18: /* ERDP low */
3319 if (val
& ERDP_EHB
) {
3320 intr
->erdp_low
&= ~ERDP_EHB
;
3322 intr
->erdp_low
= (val
& ~ERDP_EHB
) | (intr
->erdp_low
& ERDP_EHB
);
3324 case 0x1c: /* ERDP high */
3325 intr
->erdp_high
= val
;
3326 xhci_events_update(xhci
, v
);
3329 trace_usb_xhci_unimplemented("oper write", reg
);
3333 static uint64_t xhci_doorbell_read(void *ptr
, hwaddr reg
,
3336 /* doorbells always read as 0 */
3337 trace_usb_xhci_doorbell_read(reg
, 0);
3341 static void xhci_doorbell_write(void *ptr
, hwaddr reg
,
3342 uint64_t val
, unsigned size
)
3344 XHCIState
*xhci
= ptr
;
3345 unsigned int epid
, streamid
;
3347 trace_usb_xhci_doorbell_write(reg
, val
);
3349 if (!xhci_running(xhci
)) {
3350 DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
3358 xhci_process_commands(xhci
);
3360 DPRINTF("xhci: bad doorbell 0 write: 0x%x\n",
3365 streamid
= (val
>> 16) & 0xffff;
3366 if (reg
> xhci
->numslots
) {
3367 DPRINTF("xhci: bad doorbell %d\n", (int)reg
);
3368 } else if (epid
> 31) {
3369 DPRINTF("xhci: bad doorbell %d write: 0x%x\n",
3370 (int)reg
, (uint32_t)val
);
3372 xhci_kick_ep(xhci
, reg
, epid
, streamid
);
3377 static void xhci_cap_write(void *opaque
, hwaddr addr
, uint64_t val
,
3383 static const MemoryRegionOps xhci_cap_ops
= {
3384 .read
= xhci_cap_read
,
3385 .write
= xhci_cap_write
,
3386 .valid
.min_access_size
= 1,
3387 .valid
.max_access_size
= 4,
3388 .impl
.min_access_size
= 4,
3389 .impl
.max_access_size
= 4,
3390 .endianness
= DEVICE_LITTLE_ENDIAN
,
3393 static const MemoryRegionOps xhci_oper_ops
= {
3394 .read
= xhci_oper_read
,
3395 .write
= xhci_oper_write
,
3396 .valid
.min_access_size
= 4,
3397 .valid
.max_access_size
= 4,
3398 .endianness
= DEVICE_LITTLE_ENDIAN
,
3401 static const MemoryRegionOps xhci_port_ops
= {
3402 .read
= xhci_port_read
,
3403 .write
= xhci_port_write
,
3404 .valid
.min_access_size
= 4,
3405 .valid
.max_access_size
= 4,
3406 .endianness
= DEVICE_LITTLE_ENDIAN
,
3409 static const MemoryRegionOps xhci_runtime_ops
= {
3410 .read
= xhci_runtime_read
,
3411 .write
= xhci_runtime_write
,
3412 .valid
.min_access_size
= 4,
3413 .valid
.max_access_size
= 4,
3414 .endianness
= DEVICE_LITTLE_ENDIAN
,
3417 static const MemoryRegionOps xhci_doorbell_ops
= {
3418 .read
= xhci_doorbell_read
,
3419 .write
= xhci_doorbell_write
,
3420 .valid
.min_access_size
= 4,
3421 .valid
.max_access_size
= 4,
3422 .endianness
= DEVICE_LITTLE_ENDIAN
,
3425 static void xhci_attach(USBPort
*usbport
)
3427 XHCIState
*xhci
= usbport
->opaque
;
3428 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3430 xhci_port_update(port
, 0);
3433 static void xhci_detach(USBPort
*usbport
)
3435 XHCIState
*xhci
= usbport
->opaque
;
3436 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3438 xhci_detach_slot(xhci
, usbport
);
3439 xhci_port_update(port
, 1);
3442 static void xhci_wakeup(USBPort
*usbport
)
3444 XHCIState
*xhci
= usbport
->opaque
;
3445 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3447 if (get_field(port
->portsc
, PORTSC_PLS
) != PLS_U3
) {
3450 set_field(&port
->portsc
, PLS_RESUME
, PORTSC_PLS
);
3451 xhci_port_notify(port
, PORTSC_PLC
);
3454 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
3456 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
3458 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
3459 xhci_ep_nuke_one_xfer(xfer
, 0);
3462 xhci_complete_packet(xfer
);
3463 xhci_kick_ep(xfer
->xhci
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
3466 static void xhci_child_detach(USBPort
*uport
, USBDevice
*child
)
3468 USBBus
*bus
= usb_bus_from_device(child
);
3469 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3471 xhci_detach_slot(xhci
, child
->port
);
3474 static USBPortOps xhci_uport_ops
= {
3475 .attach
= xhci_attach
,
3476 .detach
= xhci_detach
,
3477 .wakeup
= xhci_wakeup
,
3478 .complete
= xhci_complete
,
3479 .child_detach
= xhci_child_detach
,
3482 static int xhci_find_epid(USBEndpoint
*ep
)
3487 if (ep
->pid
== USB_TOKEN_IN
) {
3488 return ep
->nr
* 2 + 1;
3494 static USBEndpoint
*xhci_epid_to_usbep(XHCIState
*xhci
,
3495 unsigned int slotid
, unsigned int epid
)
3497 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
3499 if (!xhci
->slots
[slotid
- 1].uport
) {
3503 return usb_ep_get(xhci
->slots
[slotid
- 1].uport
->dev
,
3504 (epid
& 1) ? USB_TOKEN_IN
: USB_TOKEN_OUT
, epid
>> 1);
3507 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
3508 unsigned int stream
)
3510 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3513 DPRINTF("%s\n", __func__
);
3514 slotid
= ep
->dev
->addr
;
3515 if (slotid
== 0 || !xhci
->slots
[slotid
-1].enabled
) {
3516 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
3519 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
), stream
);
3522 static USBBusOps xhci_bus_ops
= {
3523 .wakeup_endpoint
= xhci_wakeup_endpoint
,
3526 static void usb_xhci_init(XHCIState
*xhci
)
3528 DeviceState
*dev
= DEVICE(xhci
);
3530 int i
, usbports
, speedmask
;
3532 xhci
->usbsts
= USBSTS_HCH
;
3534 if (xhci
->numports_2
> MAXPORTS_2
) {
3535 xhci
->numports_2
= MAXPORTS_2
;
3537 if (xhci
->numports_3
> MAXPORTS_3
) {
3538 xhci
->numports_3
= MAXPORTS_3
;
3540 usbports
= MAX(xhci
->numports_2
, xhci
->numports_3
);
3541 xhci
->numports
= xhci
->numports_2
+ xhci
->numports_3
;
3543 usb_bus_new(&xhci
->bus
, sizeof(xhci
->bus
), &xhci_bus_ops
, dev
);
3545 for (i
= 0; i
< usbports
; i
++) {
3547 if (i
< xhci
->numports_2
) {
3548 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3549 port
= &xhci
->ports
[i
+ xhci
->numports_3
];
3550 port
->portnr
= i
+ 1 + xhci
->numports_3
;
3552 port
= &xhci
->ports
[i
];
3553 port
->portnr
= i
+ 1;
3555 port
->uport
= &xhci
->uports
[i
];
3557 USB_SPEED_MASK_LOW
|
3558 USB_SPEED_MASK_FULL
|
3559 USB_SPEED_MASK_HIGH
;
3560 snprintf(port
->name
, sizeof(port
->name
), "usb2 port #%d", i
+1);
3561 speedmask
|= port
->speedmask
;
3563 if (i
< xhci
->numports_3
) {
3564 if (xhci_get_flag(xhci
, XHCI_FLAG_SS_FIRST
)) {
3565 port
= &xhci
->ports
[i
];
3566 port
->portnr
= i
+ 1;
3568 port
= &xhci
->ports
[i
+ xhci
->numports_2
];
3569 port
->portnr
= i
+ 1 + xhci
->numports_2
;
3571 port
->uport
= &xhci
->uports
[i
];
3572 port
->speedmask
= USB_SPEED_MASK_SUPER
;
3573 snprintf(port
->name
, sizeof(port
->name
), "usb3 port #%d", i
+1);
3574 speedmask
|= port
->speedmask
;
3576 usb_register_port(&xhci
->bus
, &xhci
->uports
[i
], xhci
, i
,
3577 &xhci_uport_ops
, speedmask
);
3581 static void usb_xhci_realize(struct PCIDevice
*dev
, Error
**errp
)
3585 XHCIState
*xhci
= XHCI(dev
);
3587 dev
->config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
3588 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
3589 dev
->config
[PCI_CACHE_LINE_SIZE
] = 0x10;
3590 dev
->config
[0x60] = 0x30; /* release number */
3592 usb_xhci_init(xhci
);
3594 if (xhci
->numintrs
> MAXINTRS
) {
3595 xhci
->numintrs
= MAXINTRS
;
3597 while (xhci
->numintrs
& (xhci
->numintrs
- 1)) { /* ! power of 2 */
3600 if (xhci
->numintrs
< 1) {
3603 if (xhci
->numslots
> MAXSLOTS
) {
3604 xhci
->numslots
= MAXSLOTS
;
3606 if (xhci
->numslots
< 1) {
3609 if (xhci_get_flag(xhci
, XHCI_FLAG_ENABLE_STREAMS
)) {
3610 xhci
->max_pstreams_mask
= 7; /* == 256 primary streams */
3612 xhci
->max_pstreams_mask
= 0;
3615 xhci
->mfwrap_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_mfwrap_timer
, xhci
);
3617 memory_region_init(&xhci
->mem
, OBJECT(xhci
), "xhci", LEN_REGS
);
3618 memory_region_init_io(&xhci
->mem_cap
, OBJECT(xhci
), &xhci_cap_ops
, xhci
,
3619 "capabilities", LEN_CAP
);
3620 memory_region_init_io(&xhci
->mem_oper
, OBJECT(xhci
), &xhci_oper_ops
, xhci
,
3621 "operational", 0x400);
3622 memory_region_init_io(&xhci
->mem_runtime
, OBJECT(xhci
), &xhci_runtime_ops
, xhci
,
3623 "runtime", LEN_RUNTIME
);
3624 memory_region_init_io(&xhci
->mem_doorbell
, OBJECT(xhci
), &xhci_doorbell_ops
, xhci
,
3625 "doorbell", LEN_DOORBELL
);
3627 memory_region_add_subregion(&xhci
->mem
, 0, &xhci
->mem_cap
);
3628 memory_region_add_subregion(&xhci
->mem
, OFF_OPER
, &xhci
->mem_oper
);
3629 memory_region_add_subregion(&xhci
->mem
, OFF_RUNTIME
, &xhci
->mem_runtime
);
3630 memory_region_add_subregion(&xhci
->mem
, OFF_DOORBELL
, &xhci
->mem_doorbell
);
3632 for (i
= 0; i
< xhci
->numports
; i
++) {
3633 XHCIPort
*port
= &xhci
->ports
[i
];
3634 uint32_t offset
= OFF_OPER
+ 0x400 + 0x10 * i
;
3636 memory_region_init_io(&port
->mem
, OBJECT(xhci
), &xhci_port_ops
, port
,
3638 memory_region_add_subregion(&xhci
->mem
, offset
, &port
->mem
);
3641 pci_register_bar(dev
, 0,
3642 PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
,
3645 if (pci_bus_is_express(dev
->bus
) ||
3646 xhci_get_flag(xhci
, XHCI_FLAG_FORCE_PCIE_ENDCAP
)) {
3647 ret
= pcie_endpoint_cap_init(dev
, 0xa0);
3651 if (xhci_get_flag(xhci
, XHCI_FLAG_USE_MSI
)) {
3652 msi_init(dev
, 0x70, xhci
->numintrs
, true, false);
3654 if (xhci_get_flag(xhci
, XHCI_FLAG_USE_MSI_X
)) {
3655 msix_init(dev
, xhci
->numintrs
,
3656 &xhci
->mem
, 0, OFF_MSIX_TABLE
,
3657 &xhci
->mem
, 0, OFF_MSIX_PBA
,
3662 static void usb_xhci_exit(PCIDevice
*dev
)
3665 XHCIState
*xhci
= XHCI(dev
);
3667 trace_usb_xhci_exit();
3669 for (i
= 0; i
< xhci
->numslots
; i
++) {
3670 xhci_disable_slot(xhci
, i
+ 1);
3673 if (xhci
->mfwrap_timer
) {
3674 timer_del(xhci
->mfwrap_timer
);
3675 timer_free(xhci
->mfwrap_timer
);
3676 xhci
->mfwrap_timer
= NULL
;
3679 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_cap
);
3680 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_oper
);
3681 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_runtime
);
3682 memory_region_del_subregion(&xhci
->mem
, &xhci
->mem_doorbell
);
3684 for (i
= 0; i
< xhci
->numports
; i
++) {
3685 XHCIPort
*port
= &xhci
->ports
[i
];
3686 memory_region_del_subregion(&xhci
->mem
, &port
->mem
);
3689 /* destroy msix memory region */
3690 if (dev
->msix_table
&& dev
->msix_pba
3691 && dev
->msix_entry_used
) {
3692 memory_region_del_subregion(&xhci
->mem
, &dev
->msix_table_mmio
);
3693 memory_region_del_subregion(&xhci
->mem
, &dev
->msix_pba_mmio
);
3696 usb_bus_release(&xhci
->bus
);
3699 static int usb_xhci_post_load(void *opaque
, int version_id
)
3701 XHCIState
*xhci
= opaque
;
3702 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
3704 XHCIEPContext
*epctx
;
3705 dma_addr_t dcbaap
, pctx
;
3706 uint32_t slot_ctx
[4];
3708 int slotid
, epid
, state
, intr
;
3710 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
3712 for (slotid
= 1; slotid
<= xhci
->numslots
; slotid
++) {
3713 slot
= &xhci
->slots
[slotid
-1];
3714 if (!slot
->addressed
) {
3718 xhci_mask64(ldq_le_pci_dma(pci_dev
, dcbaap
+ 8 * slotid
));
3719 xhci_dma_read_u32s(xhci
, slot
->ctx
, slot_ctx
, sizeof(slot_ctx
));
3720 slot
->uport
= xhci_lookup_uport(xhci
, slot_ctx
);
3722 /* should not happen, but may trigger on guest bugs */
3724 slot
->addressed
= 0;
3727 assert(slot
->uport
&& slot
->uport
->dev
);
3729 for (epid
= 1; epid
<= 31; epid
++) {
3730 pctx
= slot
->ctx
+ 32 * epid
;
3731 xhci_dma_read_u32s(xhci
, pctx
, ep_ctx
, sizeof(ep_ctx
));
3732 state
= ep_ctx
[0] & EP_STATE_MASK
;
3733 if (state
== EP_DISABLED
) {
3736 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
3737 slot
->eps
[epid
-1] = epctx
;
3738 xhci_init_epctx(epctx
, pctx
, ep_ctx
);
3739 epctx
->state
= state
;
3740 if (state
== EP_RUNNING
) {
3741 /* kick endpoint after vmload is finished */
3742 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
3747 for (intr
= 0; intr
< xhci
->numintrs
; intr
++) {
3748 if (xhci
->intr
[intr
].msix_used
) {
3749 msix_vector_use(pci_dev
, intr
);
3751 msix_vector_unuse(pci_dev
, intr
);
3758 static const VMStateDescription vmstate_xhci_ring
= {
3759 .name
= "xhci-ring",
3761 .fields
= (VMStateField
[]) {
3762 VMSTATE_UINT64(dequeue
, XHCIRing
),
3763 VMSTATE_BOOL(ccs
, XHCIRing
),
3764 VMSTATE_END_OF_LIST()
3768 static const VMStateDescription vmstate_xhci_port
= {
3769 .name
= "xhci-port",
3771 .fields
= (VMStateField
[]) {
3772 VMSTATE_UINT32(portsc
, XHCIPort
),
3773 VMSTATE_END_OF_LIST()
3777 static const VMStateDescription vmstate_xhci_slot
= {
3778 .name
= "xhci-slot",
3780 .fields
= (VMStateField
[]) {
3781 VMSTATE_BOOL(enabled
, XHCISlot
),
3782 VMSTATE_BOOL(addressed
, XHCISlot
),
3783 VMSTATE_END_OF_LIST()
3787 static const VMStateDescription vmstate_xhci_event
= {
3788 .name
= "xhci-event",
3790 .fields
= (VMStateField
[]) {
3791 VMSTATE_UINT32(type
, XHCIEvent
),
3792 VMSTATE_UINT32(ccode
, XHCIEvent
),
3793 VMSTATE_UINT64(ptr
, XHCIEvent
),
3794 VMSTATE_UINT32(length
, XHCIEvent
),
3795 VMSTATE_UINT32(flags
, XHCIEvent
),
3796 VMSTATE_UINT8(slotid
, XHCIEvent
),
3797 VMSTATE_UINT8(epid
, XHCIEvent
),
3798 VMSTATE_END_OF_LIST()
3802 static bool xhci_er_full(void *opaque
, int version_id
)
3804 struct XHCIInterrupter
*intr
= opaque
;
3805 return intr
->er_full
;
3808 static const VMStateDescription vmstate_xhci_intr
= {
3809 .name
= "xhci-intr",
3811 .fields
= (VMStateField
[]) {
3813 VMSTATE_UINT32(iman
, XHCIInterrupter
),
3814 VMSTATE_UINT32(imod
, XHCIInterrupter
),
3815 VMSTATE_UINT32(erstsz
, XHCIInterrupter
),
3816 VMSTATE_UINT32(erstba_low
, XHCIInterrupter
),
3817 VMSTATE_UINT32(erstba_high
, XHCIInterrupter
),
3818 VMSTATE_UINT32(erdp_low
, XHCIInterrupter
),
3819 VMSTATE_UINT32(erdp_high
, XHCIInterrupter
),
3822 VMSTATE_BOOL(msix_used
, XHCIInterrupter
),
3823 VMSTATE_BOOL(er_pcs
, XHCIInterrupter
),
3824 VMSTATE_UINT64(er_start
, XHCIInterrupter
),
3825 VMSTATE_UINT32(er_size
, XHCIInterrupter
),
3826 VMSTATE_UINT32(er_ep_idx
, XHCIInterrupter
),
3828 /* event queue (used if ring is full) */
3829 VMSTATE_BOOL(er_full
, XHCIInterrupter
),
3830 VMSTATE_UINT32_TEST(ev_buffer_put
, XHCIInterrupter
, xhci_er_full
),
3831 VMSTATE_UINT32_TEST(ev_buffer_get
, XHCIInterrupter
, xhci_er_full
),
3832 VMSTATE_STRUCT_ARRAY_TEST(ev_buffer
, XHCIInterrupter
, EV_QUEUE
,
3834 vmstate_xhci_event
, XHCIEvent
),
3836 VMSTATE_END_OF_LIST()
3840 static const VMStateDescription vmstate_xhci
= {
3843 .post_load
= usb_xhci_post_load
,
3844 .fields
= (VMStateField
[]) {
3845 VMSTATE_PCIE_DEVICE(parent_obj
, XHCIState
),
3846 VMSTATE_MSIX(parent_obj
, XHCIState
),
3848 VMSTATE_STRUCT_VARRAY_UINT32(ports
, XHCIState
, numports
, 1,
3849 vmstate_xhci_port
, XHCIPort
),
3850 VMSTATE_STRUCT_VARRAY_UINT32(slots
, XHCIState
, numslots
, 1,
3851 vmstate_xhci_slot
, XHCISlot
),
3852 VMSTATE_STRUCT_VARRAY_UINT32(intr
, XHCIState
, numintrs
, 1,
3853 vmstate_xhci_intr
, XHCIInterrupter
),
3855 /* Operational Registers */
3856 VMSTATE_UINT32(usbcmd
, XHCIState
),
3857 VMSTATE_UINT32(usbsts
, XHCIState
),
3858 VMSTATE_UINT32(dnctrl
, XHCIState
),
3859 VMSTATE_UINT32(crcr_low
, XHCIState
),
3860 VMSTATE_UINT32(crcr_high
, XHCIState
),
3861 VMSTATE_UINT32(dcbaap_low
, XHCIState
),
3862 VMSTATE_UINT32(dcbaap_high
, XHCIState
),
3863 VMSTATE_UINT32(config
, XHCIState
),
3865 /* Runtime Registers & state */
3866 VMSTATE_INT64(mfindex_start
, XHCIState
),
3867 VMSTATE_TIMER_PTR(mfwrap_timer
, XHCIState
),
3868 VMSTATE_STRUCT(cmd_ring
, XHCIState
, 1, vmstate_xhci_ring
, XHCIRing
),
3870 VMSTATE_END_OF_LIST()
3874 static Property xhci_properties
[] = {
3875 DEFINE_PROP_BIT("msi", XHCIState
, flags
, XHCI_FLAG_USE_MSI
, true),
3876 DEFINE_PROP_BIT("msix", XHCIState
, flags
, XHCI_FLAG_USE_MSI_X
, true),
3877 DEFINE_PROP_BIT("superspeed-ports-first",
3878 XHCIState
, flags
, XHCI_FLAG_SS_FIRST
, true),
3879 DEFINE_PROP_BIT("force-pcie-endcap", XHCIState
, flags
,
3880 XHCI_FLAG_FORCE_PCIE_ENDCAP
, false),
3881 DEFINE_PROP_BIT("streams", XHCIState
, flags
,
3882 XHCI_FLAG_ENABLE_STREAMS
, true),
3883 DEFINE_PROP_UINT32("intrs", XHCIState
, numintrs
, MAXINTRS
),
3884 DEFINE_PROP_UINT32("slots", XHCIState
, numslots
, MAXSLOTS
),
3885 DEFINE_PROP_UINT32("p2", XHCIState
, numports_2
, 4),
3886 DEFINE_PROP_UINT32("p3", XHCIState
, numports_3
, 4),
3887 DEFINE_PROP_END_OF_LIST(),
3890 static void xhci_class_init(ObjectClass
*klass
, void *data
)
3892 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
3893 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3895 dc
->vmsd
= &vmstate_xhci
;
3896 dc
->props
= xhci_properties
;
3897 dc
->reset
= xhci_reset
;
3898 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
3899 k
->realize
= usb_xhci_realize
;
3900 k
->exit
= usb_xhci_exit
;
3901 k
->vendor_id
= PCI_VENDOR_ID_NEC
;
3902 k
->device_id
= PCI_DEVICE_ID_NEC_UPD720200
;
3903 k
->class_id
= PCI_CLASS_SERIAL_USB
;
3908 static const TypeInfo xhci_info
= {
3910 .parent
= TYPE_PCI_DEVICE
,
3911 .instance_size
= sizeof(XHCIState
),
3912 .class_init
= xhci_class_init
,
3915 static void xhci_register_types(void)
3917 type_register_static(&xhci_info
);
3920 type_init(xhci_register_types
)