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/>.
22 #include "qemu/timer.h"
24 #include "hw/pci/pci.h"
25 #include "hw/pci/msi.h"
26 #include "hw/pci/msix.h"
33 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
35 #define DPRINTF(...) do {} while (0)
37 #define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \
38 __func__, __LINE__, _msg); abort(); } while (0)
43 #define MAXPORTS (MAXPORTS_2+MAXPORTS_3)
49 /* Very pessimistic, let's hope it's enough for all cases */
50 #define EV_QUEUE (((3*TD_QUEUE)+16)*MAXSLOTS)
51 /* Do not deliver ER Full events. NEC's driver does some things not bound
52 * to the specs when it gets them */
56 #define LEN_OPER (0x400 + 0x10 * MAXPORTS)
57 #define LEN_RUNTIME ((MAXINTRS + 1) * 0x20)
58 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
60 #define OFF_OPER LEN_CAP
61 #define OFF_RUNTIME 0x1000
62 #define OFF_DOORBELL 0x2000
63 #define OFF_MSIX_TABLE 0x3000
64 #define OFF_MSIX_PBA 0x3800
65 /* must be power of 2 */
66 #define LEN_REGS 0x4000
68 #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME
69 #error Increase OFF_RUNTIME
71 #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL
72 #error Increase OFF_DOORBELL
74 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
75 # error Increase LEN_REGS
79 #define USBCMD_RS (1<<0)
80 #define USBCMD_HCRST (1<<1)
81 #define USBCMD_INTE (1<<2)
82 #define USBCMD_HSEE (1<<3)
83 #define USBCMD_LHCRST (1<<7)
84 #define USBCMD_CSS (1<<8)
85 #define USBCMD_CRS (1<<9)
86 #define USBCMD_EWE (1<<10)
87 #define USBCMD_EU3S (1<<11)
89 #define USBSTS_HCH (1<<0)
90 #define USBSTS_HSE (1<<2)
91 #define USBSTS_EINT (1<<3)
92 #define USBSTS_PCD (1<<4)
93 #define USBSTS_SSS (1<<8)
94 #define USBSTS_RSS (1<<9)
95 #define USBSTS_SRE (1<<10)
96 #define USBSTS_CNR (1<<11)
97 #define USBSTS_HCE (1<<12)
100 #define PORTSC_CCS (1<<0)
101 #define PORTSC_PED (1<<1)
102 #define PORTSC_OCA (1<<3)
103 #define PORTSC_PR (1<<4)
104 #define PORTSC_PLS_SHIFT 5
105 #define PORTSC_PLS_MASK 0xf
106 #define PORTSC_PP (1<<9)
107 #define PORTSC_SPEED_SHIFT 10
108 #define PORTSC_SPEED_MASK 0xf
109 #define PORTSC_SPEED_FULL (1<<10)
110 #define PORTSC_SPEED_LOW (2<<10)
111 #define PORTSC_SPEED_HIGH (3<<10)
112 #define PORTSC_SPEED_SUPER (4<<10)
113 #define PORTSC_PIC_SHIFT 14
114 #define PORTSC_PIC_MASK 0x3
115 #define PORTSC_LWS (1<<16)
116 #define PORTSC_CSC (1<<17)
117 #define PORTSC_PEC (1<<18)
118 #define PORTSC_WRC (1<<19)
119 #define PORTSC_OCC (1<<20)
120 #define PORTSC_PRC (1<<21)
121 #define PORTSC_PLC (1<<22)
122 #define PORTSC_CEC (1<<23)
123 #define PORTSC_CAS (1<<24)
124 #define PORTSC_WCE (1<<25)
125 #define PORTSC_WDE (1<<26)
126 #define PORTSC_WOE (1<<27)
127 #define PORTSC_DR (1<<30)
128 #define PORTSC_WPR (1<<31)
130 #define CRCR_RCS (1<<0)
131 #define CRCR_CS (1<<1)
132 #define CRCR_CA (1<<2)
133 #define CRCR_CRR (1<<3)
135 #define IMAN_IP (1<<0)
136 #define IMAN_IE (1<<1)
138 #define ERDP_EHB (1<<3)
141 typedef struct XHCITRB
{
160 PLS_COMPILANCE_MODE
= 10,
165 typedef enum TRBType
{
178 CR_CONFIGURE_ENDPOINT
,
186 CR_SET_LATENCY_TOLERANCE
,
187 CR_GET_PORT_BANDWIDTH
,
192 ER_PORT_STATUS_CHANGE
,
193 ER_BANDWIDTH_REQUEST
,
196 ER_DEVICE_NOTIFICATION
,
198 /* vendor specific bits */
199 CR_VENDOR_VIA_CHALLENGE_RESPONSE
= 48,
200 CR_VENDOR_NEC_FIRMWARE_REVISION
= 49,
201 CR_VENDOR_NEC_CHALLENGE_RESPONSE
= 50,
204 #define CR_LINK TR_LINK
206 typedef enum TRBCCode
{
209 CC_DATA_BUFFER_ERROR
,
211 CC_USB_TRANSACTION_ERROR
,
217 CC_INVALID_STREAM_TYPE_ERROR
,
218 CC_SLOT_NOT_ENABLED_ERROR
,
219 CC_EP_NOT_ENABLED_ERROR
,
225 CC_BANDWIDTH_OVERRUN
,
226 CC_CONTEXT_STATE_ERROR
,
227 CC_NO_PING_RESPONSE_ERROR
,
228 CC_EVENT_RING_FULL_ERROR
,
229 CC_INCOMPATIBLE_DEVICE_ERROR
,
230 CC_MISSED_SERVICE_ERROR
,
231 CC_COMMAND_RING_STOPPED
,
234 CC_STOPPED_LENGTH_INVALID
,
235 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
= 29,
236 CC_ISOCH_BUFFER_OVERRUN
= 31,
239 CC_INVALID_STREAM_ID_ERROR
,
240 CC_SECONDARY_BANDWIDTH_ERROR
,
241 CC_SPLIT_TRANSACTION_ERROR
245 #define TRB_TYPE_SHIFT 10
246 #define TRB_TYPE_MASK 0x3f
247 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
249 #define TRB_EV_ED (1<<2)
251 #define TRB_TR_ENT (1<<1)
252 #define TRB_TR_ISP (1<<2)
253 #define TRB_TR_NS (1<<3)
254 #define TRB_TR_CH (1<<4)
255 #define TRB_TR_IOC (1<<5)
256 #define TRB_TR_IDT (1<<6)
257 #define TRB_TR_TBC_SHIFT 7
258 #define TRB_TR_TBC_MASK 0x3
259 #define TRB_TR_BEI (1<<9)
260 #define TRB_TR_TLBPC_SHIFT 16
261 #define TRB_TR_TLBPC_MASK 0xf
262 #define TRB_TR_FRAMEID_SHIFT 20
263 #define TRB_TR_FRAMEID_MASK 0x7ff
264 #define TRB_TR_SIA (1<<31)
266 #define TRB_TR_DIR (1<<16)
268 #define TRB_CR_SLOTID_SHIFT 24
269 #define TRB_CR_SLOTID_MASK 0xff
270 #define TRB_CR_EPID_SHIFT 16
271 #define TRB_CR_EPID_MASK 0x1f
273 #define TRB_CR_BSR (1<<9)
274 #define TRB_CR_DC (1<<9)
276 #define TRB_LK_TC (1<<1)
278 #define TRB_INTR_SHIFT 22
279 #define TRB_INTR_MASK 0x3ff
280 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK)
282 #define EP_TYPE_MASK 0x7
283 #define EP_TYPE_SHIFT 3
285 #define EP_STATE_MASK 0x7
286 #define EP_DISABLED (0<<0)
287 #define EP_RUNNING (1<<0)
288 #define EP_HALTED (2<<0)
289 #define EP_STOPPED (3<<0)
290 #define EP_ERROR (4<<0)
292 #define SLOT_STATE_MASK 0x1f
293 #define SLOT_STATE_SHIFT 27
294 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
295 #define SLOT_ENABLED 0
296 #define SLOT_DEFAULT 1
297 #define SLOT_ADDRESSED 2
298 #define SLOT_CONFIGURED 3
300 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
301 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
303 typedef struct XHCIState XHCIState
;
304 typedef struct XHCIStreamContext XHCIStreamContext
;
305 typedef struct XHCIEPContext XHCIEPContext
;
307 #define get_field(data, field) \
308 (((data) >> field##_SHIFT) & field##_MASK)
310 #define set_field(data, newval, field) do { \
311 uint32_t val = *data; \
312 val &= ~(field##_MASK << field##_SHIFT); \
313 val |= ((newval) & field##_MASK) << field##_SHIFT; \
317 typedef enum EPType
{
328 typedef struct XHCIRing
{
333 typedef struct XHCIPort
{
343 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
{
377 XHCIStreamContext
*sstreams
;
380 struct XHCIEPContext
{
386 unsigned int next_xfer
;
387 unsigned int comp_xfer
;
388 XHCITransfer transfers
[TD_QUEUE
];
392 unsigned int max_psize
;
396 unsigned int max_pstreams
;
398 unsigned int nr_pstreams
;
399 XHCIStreamContext
*pstreams
;
401 /* iso xfer scheduling */
402 unsigned int interval
;
403 int64_t mfindex_last
;
404 QEMUTimer
*kick_timer
;
407 typedef struct XHCISlot
{
412 XHCIEPContext
* eps
[31];
415 typedef struct XHCIEvent
{
425 typedef struct XHCIInterrupter
{
430 uint32_t erstba_high
;
434 bool msix_used
, er_pcs
, er_full
;
438 unsigned int er_ep_idx
;
440 XHCIEvent ev_buffer
[EV_QUEUE
];
441 unsigned int ev_buffer_put
;
442 unsigned int ev_buffer_get
;
448 PCIDevice parent_obj
;
453 MemoryRegion mem_cap
;
454 MemoryRegion mem_oper
;
455 MemoryRegion mem_runtime
;
456 MemoryRegion mem_doorbell
;
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,
505 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
506 unsigned int epid
, unsigned int streamid
);
507 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
509 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
510 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
);
512 static const char *TRBType_names
[] = {
513 [TRB_RESERVED
] = "TRB_RESERVED",
514 [TR_NORMAL
] = "TR_NORMAL",
515 [TR_SETUP
] = "TR_SETUP",
516 [TR_DATA
] = "TR_DATA",
517 [TR_STATUS
] = "TR_STATUS",
518 [TR_ISOCH
] = "TR_ISOCH",
519 [TR_LINK
] = "TR_LINK",
520 [TR_EVDATA
] = "TR_EVDATA",
521 [TR_NOOP
] = "TR_NOOP",
522 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
523 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
524 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
525 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
526 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
527 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
528 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
529 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
530 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
531 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
532 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
533 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
534 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
535 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
536 [CR_NOOP
] = "CR_NOOP",
537 [ER_TRANSFER
] = "ER_TRANSFER",
538 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
539 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
540 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
541 [ER_DOORBELL
] = "ER_DOORBELL",
542 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
543 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
544 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
545 [CR_VENDOR_VIA_CHALLENGE_RESPONSE
] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE",
546 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
547 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
550 static const char *TRBCCode_names
[] = {
551 [CC_INVALID
] = "CC_INVALID",
552 [CC_SUCCESS
] = "CC_SUCCESS",
553 [CC_DATA_BUFFER_ERROR
] = "CC_DATA_BUFFER_ERROR",
554 [CC_BABBLE_DETECTED
] = "CC_BABBLE_DETECTED",
555 [CC_USB_TRANSACTION_ERROR
] = "CC_USB_TRANSACTION_ERROR",
556 [CC_TRB_ERROR
] = "CC_TRB_ERROR",
557 [CC_STALL_ERROR
] = "CC_STALL_ERROR",
558 [CC_RESOURCE_ERROR
] = "CC_RESOURCE_ERROR",
559 [CC_BANDWIDTH_ERROR
] = "CC_BANDWIDTH_ERROR",
560 [CC_NO_SLOTS_ERROR
] = "CC_NO_SLOTS_ERROR",
561 [CC_INVALID_STREAM_TYPE_ERROR
] = "CC_INVALID_STREAM_TYPE_ERROR",
562 [CC_SLOT_NOT_ENABLED_ERROR
] = "CC_SLOT_NOT_ENABLED_ERROR",
563 [CC_EP_NOT_ENABLED_ERROR
] = "CC_EP_NOT_ENABLED_ERROR",
564 [CC_SHORT_PACKET
] = "CC_SHORT_PACKET",
565 [CC_RING_UNDERRUN
] = "CC_RING_UNDERRUN",
566 [CC_RING_OVERRUN
] = "CC_RING_OVERRUN",
567 [CC_VF_ER_FULL
] = "CC_VF_ER_FULL",
568 [CC_PARAMETER_ERROR
] = "CC_PARAMETER_ERROR",
569 [CC_BANDWIDTH_OVERRUN
] = "CC_BANDWIDTH_OVERRUN",
570 [CC_CONTEXT_STATE_ERROR
] = "CC_CONTEXT_STATE_ERROR",
571 [CC_NO_PING_RESPONSE_ERROR
] = "CC_NO_PING_RESPONSE_ERROR",
572 [CC_EVENT_RING_FULL_ERROR
] = "CC_EVENT_RING_FULL_ERROR",
573 [CC_INCOMPATIBLE_DEVICE_ERROR
] = "CC_INCOMPATIBLE_DEVICE_ERROR",
574 [CC_MISSED_SERVICE_ERROR
] = "CC_MISSED_SERVICE_ERROR",
575 [CC_COMMAND_RING_STOPPED
] = "CC_COMMAND_RING_STOPPED",
576 [CC_COMMAND_ABORTED
] = "CC_COMMAND_ABORTED",
577 [CC_STOPPED
] = "CC_STOPPED",
578 [CC_STOPPED_LENGTH_INVALID
] = "CC_STOPPED_LENGTH_INVALID",
579 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
]
580 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR",
581 [CC_ISOCH_BUFFER_OVERRUN
] = "CC_ISOCH_BUFFER_OVERRUN",
582 [CC_EVENT_LOST_ERROR
] = "CC_EVENT_LOST_ERROR",
583 [CC_UNDEFINED_ERROR
] = "CC_UNDEFINED_ERROR",
584 [CC_INVALID_STREAM_ID_ERROR
] = "CC_INVALID_STREAM_ID_ERROR",
585 [CC_SECONDARY_BANDWIDTH_ERROR
] = "CC_SECONDARY_BANDWIDTH_ERROR",
586 [CC_SPLIT_TRANSACTION_ERROR
] = "CC_SPLIT_TRANSACTION_ERROR",
589 static const char *ep_state_names
[] = {
590 [EP_DISABLED
] = "disabled",
591 [EP_RUNNING
] = "running",
592 [EP_HALTED
] = "halted",
593 [EP_STOPPED
] = "stopped",
594 [EP_ERROR
] = "error",
597 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
599 if (index
>= llen
|| list
[index
] == NULL
) {
605 static const char *trb_name(XHCITRB
*trb
)
607 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
608 ARRAY_SIZE(TRBType_names
));
611 static const char *event_name(XHCIEvent
*event
)
613 return lookup_name(event
->ccode
, TRBCCode_names
,
614 ARRAY_SIZE(TRBCCode_names
));
617 static const char *ep_state_name(uint32_t state
)
619 return lookup_name(state
, ep_state_names
,
620 ARRAY_SIZE(ep_state_names
));
623 static uint64_t xhci_mfindex_get(XHCIState
*xhci
)
625 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
626 return (now
- xhci
->mfindex_start
) / 125000;
629 static void xhci_mfwrap_update(XHCIState
*xhci
)
631 const uint32_t bits
= USBCMD_RS
| USBCMD_EWE
;
632 uint32_t mfindex
, left
;
635 if ((xhci
->usbcmd
& bits
) == bits
) {
636 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
637 mfindex
= ((now
- xhci
->mfindex_start
) / 125000) & 0x3fff;
638 left
= 0x4000 - mfindex
;
639 timer_mod(xhci
->mfwrap_timer
, now
+ left
* 125000);
641 timer_del(xhci
->mfwrap_timer
);
645 static void xhci_mfwrap_timer(void *opaque
)
647 XHCIState
*xhci
= opaque
;
648 XHCIEvent wrap
= { ER_MFINDEX_WRAP
, CC_SUCCESS
};
650 xhci_event(xhci
, &wrap
, 0);
651 xhci_mfwrap_update(xhci
);
654 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
656 if (sizeof(dma_addr_t
) == 4) {
659 return low
| (((dma_addr_t
)high
<< 16) << 16);
663 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
665 if (sizeof(dma_addr_t
) == 4) {
666 return addr
& 0xffffffff;
672 static inline void xhci_dma_read_u32s(XHCIState
*xhci
, dma_addr_t addr
,
673 uint32_t *buf
, size_t len
)
677 assert((len
% sizeof(uint32_t)) == 0);
679 pci_dma_read(PCI_DEVICE(xhci
), addr
, buf
, len
);
681 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
682 buf
[i
] = le32_to_cpu(buf
[i
]);
686 static inline void xhci_dma_write_u32s(XHCIState
*xhci
, dma_addr_t addr
,
687 uint32_t *buf
, size_t len
)
690 uint32_t tmp
[len
/ sizeof(uint32_t)];
692 assert((len
% sizeof(uint32_t)) == 0);
694 for (i
= 0; i
< (len
/ sizeof(uint32_t)); i
++) {
695 tmp
[i
] = cpu_to_le32(buf
[i
]);
697 pci_dma_write(PCI_DEVICE(xhci
), addr
, tmp
, len
);
700 static XHCIPort
*xhci_lookup_port(XHCIState
*xhci
, struct USBPort
*uport
)
707 switch (uport
->dev
->speed
) {
711 index
= uport
->index
;
713 case USB_SPEED_SUPER
:
714 index
= uport
->index
+ xhci
->numports_2
;
719 return &xhci
->ports
[index
];
722 static void xhci_intx_update(XHCIState
*xhci
)
724 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
727 if (msix_enabled(pci_dev
) ||
728 msi_enabled(pci_dev
)) {
732 if (xhci
->intr
[0].iman
& IMAN_IP
&&
733 xhci
->intr
[0].iman
& IMAN_IE
&&
734 xhci
->usbcmd
& USBCMD_INTE
) {
738 trace_usb_xhci_irq_intx(level
);
739 pci_set_irq(pci_dev
, level
);
742 static void xhci_msix_update(XHCIState
*xhci
, int v
)
744 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
747 if (!msix_enabled(pci_dev
)) {
751 enabled
= xhci
->intr
[v
].iman
& IMAN_IE
;
752 if (enabled
== xhci
->intr
[v
].msix_used
) {
757 trace_usb_xhci_irq_msix_use(v
);
758 msix_vector_use(pci_dev
, v
);
759 xhci
->intr
[v
].msix_used
= true;
761 trace_usb_xhci_irq_msix_unuse(v
);
762 msix_vector_unuse(pci_dev
, v
);
763 xhci
->intr
[v
].msix_used
= false;
767 static void xhci_intr_raise(XHCIState
*xhci
, int v
)
769 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
771 xhci
->intr
[v
].erdp_low
|= ERDP_EHB
;
772 xhci
->intr
[v
].iman
|= IMAN_IP
;
773 xhci
->usbsts
|= USBSTS_EINT
;
775 if (!(xhci
->intr
[v
].iman
& IMAN_IE
)) {
779 if (!(xhci
->usbcmd
& USBCMD_INTE
)) {
783 if (msix_enabled(pci_dev
)) {
784 trace_usb_xhci_irq_msix(v
);
785 msix_notify(pci_dev
, v
);
789 if (msi_enabled(pci_dev
)) {
790 trace_usb_xhci_irq_msi(v
);
791 msi_notify(pci_dev
, v
);
796 trace_usb_xhci_irq_intx(1);
797 pci_irq_assert(pci_dev
);
801 static inline int xhci_running(XHCIState
*xhci
)
803 return !(xhci
->usbsts
& USBSTS_HCH
) && !xhci
->intr
[0].er_full
;
806 static void xhci_die(XHCIState
*xhci
)
808 xhci
->usbsts
|= USBSTS_HCE
;
809 fprintf(stderr
, "xhci: asserted controller error\n");
812 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
814 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
815 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
819 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
820 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
821 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
822 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
824 ev_trb
.control
|= TRB_C
;
826 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
828 trace_usb_xhci_queue_event(v
, intr
->er_ep_idx
, trb_name(&ev_trb
),
829 event_name(event
), ev_trb
.parameter
,
830 ev_trb
.status
, ev_trb
.control
);
832 addr
= intr
->er_start
+ TRB_SIZE
*intr
->er_ep_idx
;
833 pci_dma_write(pci_dev
, addr
, &ev_trb
, TRB_SIZE
);
836 if (intr
->er_ep_idx
>= intr
->er_size
) {
838 intr
->er_pcs
= !intr
->er_pcs
;
842 static void xhci_events_update(XHCIState
*xhci
, int v
)
844 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
849 if (xhci
->usbsts
& USBSTS_HCH
) {
853 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
854 if (erdp
< intr
->er_start
||
855 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
856 fprintf(stderr
, "xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
857 fprintf(stderr
, "xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
858 v
, intr
->er_start
, intr
->er_size
);
862 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
863 assert(dp_idx
< intr
->er_size
);
865 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus
866 * deadlocks when the ER is full. Hack it by holding off events until
867 * the driver decides to free at least half of the ring */
869 int er_free
= dp_idx
- intr
->er_ep_idx
;
871 er_free
+= intr
->er_size
;
873 if (er_free
< (intr
->er_size
/2)) {
874 DPRINTF("xhci_events_update(): event ring still "
875 "more than half full (hack)\n");
880 while (intr
->ev_buffer_put
!= intr
->ev_buffer_get
) {
881 assert(intr
->er_full
);
882 if (((intr
->er_ep_idx
+1) % intr
->er_size
) == dp_idx
) {
883 DPRINTF("xhci_events_update(): event ring full again\n");
885 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
886 xhci_write_event(xhci
, &full
, v
);
891 XHCIEvent
*event
= &intr
->ev_buffer
[intr
->ev_buffer_get
];
892 xhci_write_event(xhci
, event
, v
);
893 intr
->ev_buffer_get
++;
895 if (intr
->ev_buffer_get
== EV_QUEUE
) {
896 intr
->ev_buffer_get
= 0;
901 xhci_intr_raise(xhci
, v
);
904 if (intr
->er_full
&& intr
->ev_buffer_put
== intr
->ev_buffer_get
) {
905 DPRINTF("xhci_events_update(): event ring no longer full\n");
910 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
, int v
)
912 XHCIInterrupter
*intr
;
916 if (v
>= xhci
->numintrs
) {
917 DPRINTF("intr nr out of range (%d >= %d)\n", v
, xhci
->numintrs
);
920 intr
= &xhci
->intr
[v
];
923 DPRINTF("xhci_event(): ER full, queueing\n");
924 if (((intr
->ev_buffer_put
+1) % EV_QUEUE
) == intr
->ev_buffer_get
) {
925 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
928 intr
->ev_buffer
[intr
->ev_buffer_put
++] = *event
;
929 if (intr
->ev_buffer_put
== EV_QUEUE
) {
930 intr
->ev_buffer_put
= 0;
935 erdp
= xhci_addr64(intr
->erdp_low
, intr
->erdp_high
);
936 if (erdp
< intr
->er_start
||
937 erdp
>= (intr
->er_start
+ TRB_SIZE
*intr
->er_size
)) {
938 fprintf(stderr
, "xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
939 fprintf(stderr
, "xhci: ER[%d] at "DMA_ADDR_FMT
" len %d\n",
940 v
, intr
->er_start
, intr
->er_size
);
945 dp_idx
= (erdp
- intr
->er_start
) / TRB_SIZE
;
946 assert(dp_idx
< intr
->er_size
);
948 if ((intr
->er_ep_idx
+1) % intr
->er_size
== dp_idx
) {
949 DPRINTF("xhci_event(): ER full, queueing\n");
951 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
952 xhci_write_event(xhci
, &full
);
955 if (((intr
->ev_buffer_put
+1) % EV_QUEUE
) == intr
->ev_buffer_get
) {
956 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
959 intr
->ev_buffer
[intr
->ev_buffer_put
++] = *event
;
960 if (intr
->ev_buffer_put
== EV_QUEUE
) {
961 intr
->ev_buffer_put
= 0;
964 xhci_write_event(xhci
, event
, v
);
967 xhci_intr_raise(xhci
, v
);
970 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
973 ring
->dequeue
= base
;
977 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
980 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
984 pci_dma_read(pci_dev
, ring
->dequeue
, trb
, TRB_SIZE
);
985 trb
->addr
= ring
->dequeue
;
986 trb
->ccs
= ring
->ccs
;
987 le64_to_cpus(&trb
->parameter
);
988 le32_to_cpus(&trb
->status
);
989 le32_to_cpus(&trb
->control
);
991 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
992 trb
->parameter
, trb
->status
, trb
->control
);
994 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
998 type
= TRB_TYPE(*trb
);
1000 if (type
!= TR_LINK
) {
1002 *addr
= ring
->dequeue
;
1004 ring
->dequeue
+= TRB_SIZE
;
1007 ring
->dequeue
= xhci_mask64(trb
->parameter
);
1008 if (trb
->control
& TRB_LK_TC
) {
1009 ring
->ccs
= !ring
->ccs
;
1015 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
1017 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
1020 dma_addr_t dequeue
= ring
->dequeue
;
1021 bool ccs
= ring
->ccs
;
1022 /* hack to bundle together the two/three TDs that make a setup transfer */
1023 bool control_td_set
= 0;
1027 pci_dma_read(pci_dev
, dequeue
, &trb
, TRB_SIZE
);
1028 le64_to_cpus(&trb
.parameter
);
1029 le32_to_cpus(&trb
.status
);
1030 le32_to_cpus(&trb
.control
);
1032 if ((trb
.control
& TRB_C
) != ccs
) {
1036 type
= TRB_TYPE(trb
);
1038 if (type
== TR_LINK
) {
1039 dequeue
= xhci_mask64(trb
.parameter
);
1040 if (trb
.control
& TRB_LK_TC
) {
1047 dequeue
+= TRB_SIZE
;
1049 if (type
== TR_SETUP
) {
1051 } else if (type
== TR_STATUS
) {
1055 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
1061 static void xhci_er_reset(XHCIState
*xhci
, int v
)
1063 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
1066 if (intr
->erstsz
== 0) {
1072 /* cache the (sole) event ring segment location */
1073 if (intr
->erstsz
!= 1) {
1074 fprintf(stderr
, "xhci: invalid value for ERSTSZ: %d\n", intr
->erstsz
);
1078 dma_addr_t erstba
= xhci_addr64(intr
->erstba_low
, intr
->erstba_high
);
1079 pci_dma_read(PCI_DEVICE(xhci
), erstba
, &seg
, sizeof(seg
));
1080 le32_to_cpus(&seg
.addr_low
);
1081 le32_to_cpus(&seg
.addr_high
);
1082 le32_to_cpus(&seg
.size
);
1083 if (seg
.size
< 16 || seg
.size
> 4096) {
1084 fprintf(stderr
, "xhci: invalid value for segment size: %d\n", seg
.size
);
1088 intr
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
1089 intr
->er_size
= seg
.size
;
1091 intr
->er_ep_idx
= 0;
1095 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT
" [%d]\n",
1096 v
, intr
->er_start
, intr
->er_size
);
1099 static void xhci_run(XHCIState
*xhci
)
1101 trace_usb_xhci_run();
1102 xhci
->usbsts
&= ~USBSTS_HCH
;
1103 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1106 static void xhci_stop(XHCIState
*xhci
)
1108 trace_usb_xhci_stop();
1109 xhci
->usbsts
|= USBSTS_HCH
;
1110 xhci
->crcr_low
&= ~CRCR_CRR
;
1113 static XHCIStreamContext
*xhci_alloc_stream_contexts(unsigned count
,
1116 XHCIStreamContext
*stctx
;
1119 stctx
= g_new0(XHCIStreamContext
, count
);
1120 for (i
= 0; i
< count
; i
++) {
1121 stctx
[i
].pctx
= base
+ i
* 16;
1127 static void xhci_reset_streams(XHCIEPContext
*epctx
)
1131 for (i
= 0; i
< epctx
->nr_pstreams
; i
++) {
1132 epctx
->pstreams
[i
].sct
= -1;
1133 g_free(epctx
->pstreams
[i
].sstreams
);
1137 static void xhci_alloc_streams(XHCIEPContext
*epctx
, dma_addr_t base
)
1139 assert(epctx
->pstreams
== NULL
);
1140 epctx
->nr_pstreams
= 2 << epctx
->max_pstreams
;
1141 epctx
->pstreams
= xhci_alloc_stream_contexts(epctx
->nr_pstreams
, base
);
1144 static void xhci_free_streams(XHCIEPContext
*epctx
)
1148 assert(epctx
->pstreams
!= NULL
);
1151 for (i
= 0; i
< epctx
->nr_pstreams
; i
++) {
1152 g_free(epctx
->pstreams
[i
].sstreams
);
1155 g_free(epctx
->pstreams
);
1156 epctx
->pstreams
= NULL
;
1157 epctx
->nr_pstreams
= 0;
1160 static XHCIStreamContext
*xhci_find_stream(XHCIEPContext
*epctx
,
1161 unsigned int streamid
,
1164 XHCIStreamContext
*sctx
;
1166 uint32_t ctx
[2], sct
;
1168 assert(streamid
!= 0);
1170 if (streamid
>= epctx
->nr_pstreams
) {
1171 *cc_error
= CC_INVALID_STREAM_ID_ERROR
;
1174 sctx
= epctx
->pstreams
+ streamid
;
1176 FIXME("secondary streams not implemented yet");
1179 if (sctx
->sct
== -1) {
1180 xhci_dma_read_u32s(epctx
->xhci
, sctx
->pctx
, ctx
, sizeof(ctx
));
1181 sct
= (ctx
[0] >> 1) & 0x07;
1182 if (epctx
->lsa
&& sct
!= 1) {
1183 *cc_error
= CC_INVALID_STREAM_TYPE_ERROR
;
1187 base
= xhci_addr64(ctx
[0] & ~0xf, ctx
[1]);
1188 xhci_ring_init(epctx
->xhci
, &sctx
->ring
, base
);
1193 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
1194 XHCIStreamContext
*sctx
, uint32_t state
)
1199 xhci_dma_read_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1200 ctx
[0] &= ~EP_STATE_MASK
;
1203 /* update ring dequeue ptr */
1204 if (epctx
->nr_pstreams
) {
1206 xhci_dma_read_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1208 ctx2
[0] |= sctx
->ring
.dequeue
| sctx
->ring
.ccs
;
1209 ctx2
[1] = (sctx
->ring
.dequeue
>> 16) >> 16;
1210 xhci_dma_write_u32s(xhci
, sctx
->pctx
, ctx2
, sizeof(ctx2
));
1213 ctx
[2] = epctx
->ring
.dequeue
| epctx
->ring
.ccs
;
1214 ctx
[3] = (epctx
->ring
.dequeue
>> 16) >> 16;
1215 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
1216 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
1219 xhci_dma_write_u32s(xhci
, epctx
->pctx
, ctx
, sizeof(ctx
));
1220 if (epctx
->state
!= state
) {
1221 trace_usb_xhci_ep_state(epctx
->slotid
, epctx
->epid
,
1222 ep_state_name(epctx
->state
),
1223 ep_state_name(state
));
1225 epctx
->state
= state
;
1228 static void xhci_ep_kick_timer(void *opaque
)
1230 XHCIEPContext
*epctx
= opaque
;
1231 xhci_kick_ep(epctx
->xhci
, epctx
->slotid
, epctx
->epid
, 0);
1234 static XHCIEPContext
*xhci_alloc_epctx(XHCIState
*xhci
,
1235 unsigned int slotid
,
1238 XHCIEPContext
*epctx
;
1241 epctx
= g_new0(XHCIEPContext
, 1);
1243 epctx
->slotid
= slotid
;
1246 for (i
= 0; i
< ARRAY_SIZE(epctx
->transfers
); i
++) {
1247 usb_packet_init(&epctx
->transfers
[i
].packet
);
1249 epctx
->kick_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_ep_kick_timer
, epctx
);
1254 static void xhci_init_epctx(XHCIEPContext
*epctx
,
1255 dma_addr_t pctx
, uint32_t *ctx
)
1259 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
1261 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
1262 DPRINTF("xhci: endpoint %d.%d type is %d\n", epid
/2, epid
%2, epctx
->type
);
1264 epctx
->max_psize
= ctx
[1]>>16;
1265 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
1266 epctx
->max_pstreams
= (ctx
[0] >> 10) & 0xf;
1267 epctx
->lsa
= (ctx
[0] >> 15) & 1;
1268 DPRINTF("xhci: endpoint %d.%d max transaction (burst) size is %d\n",
1269 epid
/2, epid
%2, epctx
->max_psize
);
1270 if (epctx
->max_pstreams
) {
1271 xhci_alloc_streams(epctx
, dequeue
);
1273 xhci_ring_init(epctx
->xhci
, &epctx
->ring
, dequeue
);
1274 epctx
->ring
.ccs
= ctx
[2] & 1;
1277 epctx
->interval
= 1 << ((ctx
[0] >> 16) & 0xff);
1280 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
1281 unsigned int epid
, dma_addr_t pctx
,
1285 XHCIEPContext
*epctx
;
1287 trace_usb_xhci_ep_enable(slotid
, epid
);
1288 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1289 assert(epid
>= 1 && epid
<= 31);
1291 slot
= &xhci
->slots
[slotid
-1];
1292 if (slot
->eps
[epid
-1]) {
1293 xhci_disable_ep(xhci
, slotid
, epid
);
1296 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
1297 slot
->eps
[epid
-1] = epctx
;
1298 xhci_init_epctx(epctx
, pctx
, ctx
);
1300 epctx
->mfindex_last
= 0;
1302 epctx
->state
= EP_RUNNING
;
1303 ctx
[0] &= ~EP_STATE_MASK
;
1304 ctx
[0] |= EP_RUNNING
;
1309 static int xhci_ep_nuke_one_xfer(XHCITransfer
*t
)
1313 if (t
->running_async
) {
1314 usb_cancel_packet(&t
->packet
);
1315 t
->running_async
= 0;
1317 DPRINTF("xhci: cancelling transfer, waiting for it to complete\n");
1320 if (t
->running_retry
) {
1321 XHCIEPContext
*epctx
= t
->xhci
->slots
[t
->slotid
-1].eps
[t
->epid
-1];
1323 epctx
->retry
= NULL
;
1324 timer_del(epctx
->kick_timer
);
1326 t
->running_retry
= 0;
1333 t
->trb_count
= t
->trb_alloced
= 0;
1338 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
1342 XHCIEPContext
*epctx
;
1343 int i
, xferi
, killed
= 0;
1344 USBEndpoint
*ep
= NULL
;
1345 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1346 assert(epid
>= 1 && epid
<= 31);
1348 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
1350 slot
= &xhci
->slots
[slotid
-1];
1352 if (!slot
->eps
[epid
-1]) {
1356 epctx
= slot
->eps
[epid
-1];
1358 xferi
= epctx
->next_xfer
;
1359 for (i
= 0; i
< TD_QUEUE
; i
++) {
1360 if (epctx
->transfers
[xferi
].packet
.ep
) {
1361 ep
= epctx
->transfers
[xferi
].packet
.ep
;
1363 killed
+= xhci_ep_nuke_one_xfer(&epctx
->transfers
[xferi
]);
1364 epctx
->transfers
[xferi
].packet
.ep
= NULL
;
1365 xferi
= (xferi
+ 1) % TD_QUEUE
;
1368 usb_device_ep_stopped(ep
->dev
, ep
);
1373 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
1377 XHCIEPContext
*epctx
;
1379 trace_usb_xhci_ep_disable(slotid
, epid
);
1380 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1381 assert(epid
>= 1 && epid
<= 31);
1383 slot
= &xhci
->slots
[slotid
-1];
1385 if (!slot
->eps
[epid
-1]) {
1386 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
1390 xhci_ep_nuke_xfers(xhci
, slotid
, epid
);
1392 epctx
= slot
->eps
[epid
-1];
1394 if (epctx
->nr_pstreams
) {
1395 xhci_free_streams(epctx
);
1398 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_DISABLED
);
1400 timer_free(epctx
->kick_timer
);
1402 slot
->eps
[epid
-1] = NULL
;
1407 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
1411 XHCIEPContext
*epctx
;
1413 trace_usb_xhci_ep_stop(slotid
, epid
);
1414 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1416 if (epid
< 1 || epid
> 31) {
1417 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1418 return CC_TRB_ERROR
;
1421 slot
= &xhci
->slots
[slotid
-1];
1423 if (!slot
->eps
[epid
-1]) {
1424 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1425 return CC_EP_NOT_ENABLED_ERROR
;
1428 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
1429 fprintf(stderr
, "xhci: FIXME: endpoint stopped w/ xfers running, "
1430 "data might be lost\n");
1433 epctx
= slot
->eps
[epid
-1];
1435 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1437 if (epctx
->nr_pstreams
) {
1438 xhci_reset_streams(epctx
);
1444 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
1448 XHCIEPContext
*epctx
;
1450 trace_usb_xhci_ep_reset(slotid
, epid
);
1451 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1453 if (epid
< 1 || epid
> 31) {
1454 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1455 return CC_TRB_ERROR
;
1458 slot
= &xhci
->slots
[slotid
-1];
1460 if (!slot
->eps
[epid
-1]) {
1461 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1462 return CC_EP_NOT_ENABLED_ERROR
;
1465 epctx
= slot
->eps
[epid
-1];
1467 if (epctx
->state
!= EP_HALTED
) {
1468 fprintf(stderr
, "xhci: reset EP while EP %d not halted (%d)\n",
1469 epid
, epctx
->state
);
1470 return CC_CONTEXT_STATE_ERROR
;
1473 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
1474 fprintf(stderr
, "xhci: FIXME: endpoint reset w/ xfers running, "
1475 "data might be lost\n");
1478 uint8_t ep
= epid
>>1;
1484 if (!xhci
->slots
[slotid
-1].uport
||
1485 !xhci
->slots
[slotid
-1].uport
->dev
) {
1486 return CC_USB_TRANSACTION_ERROR
;
1489 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_STOPPED
);
1491 if (epctx
->nr_pstreams
) {
1492 xhci_reset_streams(epctx
);
1498 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1499 unsigned int epid
, unsigned int streamid
,
1503 XHCIEPContext
*epctx
;
1504 XHCIStreamContext
*sctx
;
1507 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1509 if (epid
< 1 || epid
> 31) {
1510 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1511 return CC_TRB_ERROR
;
1514 trace_usb_xhci_ep_set_dequeue(slotid
, epid
, streamid
, pdequeue
);
1515 dequeue
= xhci_mask64(pdequeue
);
1517 slot
= &xhci
->slots
[slotid
-1];
1519 if (!slot
->eps
[epid
-1]) {
1520 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1521 return CC_EP_NOT_ENABLED_ERROR
;
1524 epctx
= slot
->eps
[epid
-1];
1526 if (epctx
->state
!= EP_STOPPED
) {
1527 fprintf(stderr
, "xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1528 return CC_CONTEXT_STATE_ERROR
;
1531 if (epctx
->nr_pstreams
) {
1533 sctx
= xhci_find_stream(epctx
, streamid
, &err
);
1537 xhci_ring_init(xhci
, &sctx
->ring
, dequeue
& ~0xf);
1538 sctx
->ring
.ccs
= dequeue
& 1;
1541 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1542 epctx
->ring
.ccs
= dequeue
& 1;
1545 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_STOPPED
);
1550 static int xhci_xfer_create_sgl(XHCITransfer
*xfer
, int in_xfer
)
1552 XHCIState
*xhci
= xfer
->xhci
;
1555 xfer
->int_req
= false;
1556 pci_dma_sglist_init(&xfer
->sgl
, PCI_DEVICE(xhci
), xfer
->trb_count
);
1557 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1558 XHCITRB
*trb
= &xfer
->trbs
[i
];
1560 unsigned int chunk
= 0;
1562 if (trb
->control
& TRB_TR_IOC
) {
1563 xfer
->int_req
= true;
1566 switch (TRB_TYPE(*trb
)) {
1568 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1569 fprintf(stderr
, "xhci: data direction mismatch for TR_DATA\n");
1575 addr
= xhci_mask64(trb
->parameter
);
1576 chunk
= trb
->status
& 0x1ffff;
1577 if (trb
->control
& TRB_TR_IDT
) {
1578 if (chunk
> 8 || in_xfer
) {
1579 fprintf(stderr
, "xhci: invalid immediate data TRB\n");
1582 qemu_sglist_add(&xfer
->sgl
, trb
->addr
, chunk
);
1584 qemu_sglist_add(&xfer
->sgl
, addr
, chunk
);
1593 qemu_sglist_destroy(&xfer
->sgl
);
1598 static void xhci_xfer_unmap(XHCITransfer
*xfer
)
1600 usb_packet_unmap(&xfer
->packet
, &xfer
->sgl
);
1601 qemu_sglist_destroy(&xfer
->sgl
);
1604 static void xhci_xfer_report(XHCITransfer
*xfer
)
1610 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1611 XHCIState
*xhci
= xfer
->xhci
;
1614 left
= xfer
->packet
.actual_length
;
1616 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1617 XHCITRB
*trb
= &xfer
->trbs
[i
];
1618 unsigned int chunk
= 0;
1620 switch (TRB_TYPE(*trb
)) {
1624 chunk
= trb
->status
& 0x1ffff;
1627 if (xfer
->status
== CC_SUCCESS
) {
1640 if (!reported
&& ((trb
->control
& TRB_TR_IOC
) ||
1641 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)) ||
1642 (xfer
->status
!= CC_SUCCESS
&& left
== 0))) {
1643 event
.slotid
= xfer
->slotid
;
1644 event
.epid
= xfer
->epid
;
1645 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1647 event
.ptr
= trb
->addr
;
1648 if (xfer
->status
== CC_SUCCESS
) {
1649 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1651 event
.ccode
= xfer
->status
;
1653 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1654 event
.ptr
= trb
->parameter
;
1655 event
.flags
|= TRB_EV_ED
;
1656 event
.length
= edtla
& 0xffffff;
1657 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1660 xhci_event(xhci
, &event
, TRB_INTR(*trb
));
1662 if (xfer
->status
!= CC_SUCCESS
) {
1669 static void xhci_stall_ep(XHCITransfer
*xfer
)
1671 XHCIState
*xhci
= xfer
->xhci
;
1672 XHCISlot
*slot
= &xhci
->slots
[xfer
->slotid
-1];
1673 XHCIEPContext
*epctx
= slot
->eps
[xfer
->epid
-1];
1675 XHCIStreamContext
*sctx
;
1677 if (epctx
->nr_pstreams
) {
1678 sctx
= xhci_find_stream(epctx
, xfer
->streamid
, &err
);
1682 sctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1683 sctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1684 xhci_set_ep_state(xhci
, epctx
, sctx
, EP_HALTED
);
1686 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1687 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1688 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_HALTED
);
1692 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
,
1693 XHCIEPContext
*epctx
);
1695 static int xhci_setup_packet(XHCITransfer
*xfer
)
1697 XHCIState
*xhci
= xfer
->xhci
;
1702 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1704 if (xfer
->packet
.ep
) {
1705 ep
= xfer
->packet
.ep
;
1708 if (!xhci
->slots
[xfer
->slotid
-1].uport
) {
1709 fprintf(stderr
, "xhci: slot %d has no device\n",
1713 dev
= xhci
->slots
[xfer
->slotid
-1].uport
->dev
;
1714 ep
= usb_ep_get(dev
, dir
, xfer
->epid
>> 1);
1717 xhci_xfer_create_sgl(xfer
, dir
== USB_TOKEN_IN
); /* Also sets int_req */
1718 usb_packet_setup(&xfer
->packet
, dir
, ep
, xfer
->streamid
,
1719 xfer
->trbs
[0].addr
, false, xfer
->int_req
);
1720 usb_packet_map(&xfer
->packet
, &xfer
->sgl
);
1721 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1722 xfer
->packet
.pid
, dev
->addr
, ep
->nr
);
1726 static int xhci_complete_packet(XHCITransfer
*xfer
)
1728 if (xfer
->packet
.status
== USB_RET_ASYNC
) {
1729 trace_usb_xhci_xfer_async(xfer
);
1730 xfer
->running_async
= 1;
1731 xfer
->running_retry
= 0;
1733 xfer
->cancelled
= 0;
1735 } else if (xfer
->packet
.status
== USB_RET_NAK
) {
1736 trace_usb_xhci_xfer_nak(xfer
);
1737 xfer
->running_async
= 0;
1738 xfer
->running_retry
= 1;
1740 xfer
->cancelled
= 0;
1743 xfer
->running_async
= 0;
1744 xfer
->running_retry
= 0;
1746 xhci_xfer_unmap(xfer
);
1749 if (xfer
->packet
.status
== USB_RET_SUCCESS
) {
1750 trace_usb_xhci_xfer_success(xfer
, xfer
->packet
.actual_length
);
1751 xfer
->status
= CC_SUCCESS
;
1752 xhci_xfer_report(xfer
);
1757 trace_usb_xhci_xfer_error(xfer
, xfer
->packet
.status
);
1758 switch (xfer
->packet
.status
) {
1760 case USB_RET_IOERROR
:
1761 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1762 xhci_xfer_report(xfer
);
1763 xhci_stall_ep(xfer
);
1766 xfer
->status
= CC_STALL_ERROR
;
1767 xhci_xfer_report(xfer
);
1768 xhci_stall_ep(xfer
);
1770 case USB_RET_BABBLE
:
1771 xfer
->status
= CC_BABBLE_DETECTED
;
1772 xhci_xfer_report(xfer
);
1773 xhci_stall_ep(xfer
);
1776 fprintf(stderr
, "%s: FIXME: status = %d\n", __func__
,
1777 xfer
->packet
.status
);
1778 FIXME("unhandled USB_RET_*");
1783 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1785 XHCITRB
*trb_setup
, *trb_status
;
1786 uint8_t bmRequestType
;
1788 trb_setup
= &xfer
->trbs
[0];
1789 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1791 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
1793 /* at most one Event Data TRB allowed after STATUS */
1794 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1798 /* do some sanity checks */
1799 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1800 fprintf(stderr
, "xhci: ep0 first TD not SETUP: %d\n",
1801 TRB_TYPE(*trb_setup
));
1804 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1805 fprintf(stderr
, "xhci: ep0 last TD not STATUS: %d\n",
1806 TRB_TYPE(*trb_status
));
1809 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1810 fprintf(stderr
, "xhci: Setup TRB doesn't have IDT set\n");
1813 if ((trb_setup
->status
& 0x1ffff) != 8) {
1814 fprintf(stderr
, "xhci: Setup TRB has bad length (%d)\n",
1815 (trb_setup
->status
& 0x1ffff));
1819 bmRequestType
= trb_setup
->parameter
;
1821 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1822 xfer
->iso_xfer
= false;
1823 xfer
->timed_xfer
= false;
1825 if (xhci_setup_packet(xfer
) < 0) {
1828 xfer
->packet
.parameter
= trb_setup
->parameter
;
1830 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1832 xhci_complete_packet(xfer
);
1833 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1834 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
, 0);
1839 static void xhci_calc_intr_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1840 XHCIEPContext
*epctx
, uint64_t mfindex
)
1842 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1843 ~(epctx
->interval
-1));
1844 uint64_t kick
= epctx
->mfindex_last
+ epctx
->interval
;
1846 assert(epctx
->interval
!= 0);
1847 xfer
->mfindex_kick
= MAX(asap
, kick
);
1850 static void xhci_calc_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1851 XHCIEPContext
*epctx
, uint64_t mfindex
)
1853 if (xfer
->trbs
[0].control
& TRB_TR_SIA
) {
1854 uint64_t asap
= ((mfindex
+ epctx
->interval
- 1) &
1855 ~(epctx
->interval
-1));
1856 if (asap
>= epctx
->mfindex_last
&&
1857 asap
<= epctx
->mfindex_last
+ epctx
->interval
* 4) {
1858 xfer
->mfindex_kick
= epctx
->mfindex_last
+ epctx
->interval
;
1860 xfer
->mfindex_kick
= asap
;
1863 xfer
->mfindex_kick
= (xfer
->trbs
[0].control
>> TRB_TR_FRAMEID_SHIFT
)
1864 & TRB_TR_FRAMEID_MASK
;
1865 xfer
->mfindex_kick
|= mfindex
& ~0x3fff;
1866 if (xfer
->mfindex_kick
< mfindex
) {
1867 xfer
->mfindex_kick
+= 0x4000;
1872 static void xhci_check_intr_iso_kick(XHCIState
*xhci
, XHCITransfer
*xfer
,
1873 XHCIEPContext
*epctx
, uint64_t mfindex
)
1875 if (xfer
->mfindex_kick
> mfindex
) {
1876 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
1877 (xfer
->mfindex_kick
- mfindex
) * 125000);
1878 xfer
->running_retry
= 1;
1880 epctx
->mfindex_last
= xfer
->mfindex_kick
;
1881 timer_del(epctx
->kick_timer
);
1882 xfer
->running_retry
= 0;
1887 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1891 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer
->slotid
, xfer
->epid
);
1893 xfer
->in_xfer
= epctx
->type
>>2;
1895 switch(epctx
->type
) {
1899 xfer
->iso_xfer
= false;
1900 xfer
->timed_xfer
= true;
1901 mfindex
= xhci_mfindex_get(xhci
);
1902 xhci_calc_intr_kick(xhci
, xfer
, epctx
, mfindex
);
1903 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1904 if (xfer
->running_retry
) {
1911 xfer
->iso_xfer
= false;
1912 xfer
->timed_xfer
= false;
1917 xfer
->iso_xfer
= true;
1918 xfer
->timed_xfer
= true;
1919 mfindex
= xhci_mfindex_get(xhci
);
1920 xhci_calc_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1921 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1922 if (xfer
->running_retry
) {
1927 fprintf(stderr
, "xhci: unknown or unhandled EP "
1928 "(type %d, in %d, ep %02x)\n",
1929 epctx
->type
, xfer
->in_xfer
, xfer
->epid
);
1933 if (xhci_setup_packet(xfer
) < 0) {
1936 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1938 xhci_complete_packet(xfer
);
1939 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1940 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
1945 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1947 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
1948 return xhci_submit(xhci
, xfer
, epctx
);
1951 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
1952 unsigned int epid
, unsigned int streamid
)
1954 XHCIStreamContext
*stctx
;
1955 XHCIEPContext
*epctx
;
1957 USBEndpoint
*ep
= NULL
;
1962 trace_usb_xhci_ep_kick(slotid
, epid
, streamid
);
1963 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
1964 assert(epid
>= 1 && epid
<= 31);
1966 if (!xhci
->slots
[slotid
-1].enabled
) {
1967 fprintf(stderr
, "xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1970 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1972 fprintf(stderr
, "xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1978 XHCITransfer
*xfer
= epctx
->retry
;
1980 trace_usb_xhci_xfer_retry(xfer
);
1981 assert(xfer
->running_retry
);
1982 if (xfer
->timed_xfer
) {
1983 /* time to kick the transfer? */
1984 mfindex
= xhci_mfindex_get(xhci
);
1985 xhci_check_intr_iso_kick(xhci
, xfer
, epctx
, mfindex
);
1986 if (xfer
->running_retry
) {
1989 xfer
->timed_xfer
= 0;
1990 xfer
->running_retry
= 1;
1992 if (xfer
->iso_xfer
) {
1993 /* retry iso transfer */
1994 if (xhci_setup_packet(xfer
) < 0) {
1997 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1998 assert(xfer
->packet
.status
!= USB_RET_NAK
);
1999 xhci_complete_packet(xfer
);
2001 /* retry nak'ed transfer */
2002 if (xhci_setup_packet(xfer
) < 0) {
2005 usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
2006 if (xfer
->packet
.status
== USB_RET_NAK
) {
2009 xhci_complete_packet(xfer
);
2011 assert(!xfer
->running_retry
);
2012 epctx
->retry
= NULL
;
2015 if (epctx
->state
== EP_HALTED
) {
2016 DPRINTF("xhci: ep halted, not running schedule\n");
2021 if (epctx
->nr_pstreams
) {
2023 stctx
= xhci_find_stream(epctx
, streamid
, &err
);
2024 if (stctx
== NULL
) {
2027 ring
= &stctx
->ring
;
2028 xhci_set_ep_state(xhci
, epctx
, stctx
, EP_RUNNING
);
2030 ring
= &epctx
->ring
;
2032 xhci_set_ep_state(xhci
, epctx
, NULL
, EP_RUNNING
);
2034 assert(ring
->dequeue
!= 0);
2037 XHCITransfer
*xfer
= &epctx
->transfers
[epctx
->next_xfer
];
2038 if (xfer
->running_async
|| xfer
->running_retry
) {
2041 length
= xhci_ring_chain_length(xhci
, ring
);
2044 } else if (length
== 0) {
2047 if (xfer
->trbs
&& xfer
->trb_alloced
< length
) {
2048 xfer
->trb_count
= 0;
2049 xfer
->trb_alloced
= 0;
2054 xfer
->trbs
= g_malloc(sizeof(XHCITRB
) * length
);
2055 xfer
->trb_alloced
= length
;
2057 xfer
->trb_count
= length
;
2059 for (i
= 0; i
< length
; i
++) {
2060 assert(xhci_ring_fetch(xhci
, ring
, &xfer
->trbs
[i
], NULL
));
2064 xfer
->slotid
= slotid
;
2065 xfer
->streamid
= streamid
;
2068 if (xhci_fire_ctl_transfer(xhci
, xfer
) >= 0) {
2069 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
2070 ep
= xfer
->packet
.ep
;
2072 fprintf(stderr
, "xhci: error firing CTL transfer\n");
2075 if (xhci_fire_transfer(xhci
, xfer
, epctx
) >= 0) {
2076 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
2077 ep
= xfer
->packet
.ep
;
2079 if (!xfer
->timed_xfer
) {
2080 fprintf(stderr
, "xhci: error firing data transfer\n");
2085 if (epctx
->state
== EP_HALTED
) {
2088 if (xfer
->running_retry
) {
2089 DPRINTF("xhci: xfer nacked, stopping schedule\n");
2090 epctx
->retry
= xfer
;
2095 usb_device_flush_ep_queue(ep
->dev
, ep
);
2099 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
2101 trace_usb_xhci_slot_enable(slotid
);
2102 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2103 xhci
->slots
[slotid
-1].enabled
= 1;
2104 xhci
->slots
[slotid
-1].uport
= NULL
;
2105 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
2110 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
2114 trace_usb_xhci_slot_disable(slotid
);
2115 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2117 for (i
= 1; i
<= 31; i
++) {
2118 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2119 xhci_disable_ep(xhci
, slotid
, i
);
2123 xhci
->slots
[slotid
-1].enabled
= 0;
2124 xhci
->slots
[slotid
-1].addressed
= 0;
2125 xhci
->slots
[slotid
-1].uport
= NULL
;
2129 static USBPort
*xhci_lookup_uport(XHCIState
*xhci
, uint32_t *slot_ctx
)
2135 port
= (slot_ctx
[1]>>16) & 0xFF;
2136 port
= xhci
->ports
[port
-1].uport
->index
+1;
2137 pos
= snprintf(path
, sizeof(path
), "%d", port
);
2138 for (i
= 0; i
< 5; i
++) {
2139 port
= (slot_ctx
[0] >> 4*i
) & 0x0f;
2143 pos
+= snprintf(path
+ pos
, sizeof(path
) - pos
, ".%d", port
);
2146 QTAILQ_FOREACH(uport
, &xhci
->bus
.used
, next
) {
2147 if (strcmp(uport
->path
, path
) == 0) {
2154 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
2155 uint64_t pictx
, bool bsr
)
2160 dma_addr_t ictx
, octx
, dcbaap
;
2162 uint32_t ictl_ctx
[2];
2163 uint32_t slot_ctx
[4];
2164 uint32_t ep0_ctx
[5];
2168 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2170 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
2171 poctx
= ldq_le_pci_dma(PCI_DEVICE(xhci
), dcbaap
+ 8 * slotid
);
2172 ictx
= xhci_mask64(pictx
);
2173 octx
= xhci_mask64(poctx
);
2175 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2176 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2178 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2180 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
2181 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
2182 ictl_ctx
[0], ictl_ctx
[1]);
2183 return CC_TRB_ERROR
;
2186 xhci_dma_read_u32s(xhci
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
2187 xhci_dma_read_u32s(xhci
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
2189 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2190 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2192 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2193 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2195 uport
= xhci_lookup_uport(xhci
, slot_ctx
);
2196 if (uport
== NULL
) {
2197 fprintf(stderr
, "xhci: port not found\n");
2198 return CC_TRB_ERROR
;
2200 trace_usb_xhci_slot_address(slotid
, uport
->path
);
2204 fprintf(stderr
, "xhci: port %s not connected\n", uport
->path
);
2205 return CC_USB_TRANSACTION_ERROR
;
2208 for (i
= 0; i
< xhci
->numslots
; i
++) {
2209 if (i
== slotid
-1) {
2212 if (xhci
->slots
[i
].uport
== uport
) {
2213 fprintf(stderr
, "xhci: port %s already assigned to slot %d\n",
2215 return CC_TRB_ERROR
;
2219 slot
= &xhci
->slots
[slotid
-1];
2220 slot
->uport
= uport
;
2224 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2229 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slotid
;
2230 usb_device_reset(dev
);
2231 memset(&p
, 0, sizeof(p
));
2232 usb_packet_addbuf(&p
, buf
, sizeof(buf
));
2233 usb_packet_setup(&p
, USB_TOKEN_OUT
,
2234 usb_ep_get(dev
, USB_TOKEN_OUT
, 0), 0,
2236 usb_device_handle_control(dev
, &p
,
2237 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
2238 slotid
, 0, 0, NULL
);
2239 assert(p
.status
!= USB_RET_ASYNC
);
2242 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
2244 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2245 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2246 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2247 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2249 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2250 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2252 xhci
->slots
[slotid
-1].addressed
= 1;
2257 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
2258 uint64_t pictx
, bool dc
)
2260 dma_addr_t ictx
, octx
;
2261 uint32_t ictl_ctx
[2];
2262 uint32_t slot_ctx
[4];
2263 uint32_t islot_ctx
[4];
2268 trace_usb_xhci_slot_configure(slotid
);
2269 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2271 ictx
= xhci_mask64(pictx
);
2272 octx
= xhci
->slots
[slotid
-1].ctx
;
2274 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2275 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2278 for (i
= 2; i
<= 31; i
++) {
2279 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2280 xhci_disable_ep(xhci
, slotid
, i
);
2284 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2285 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2286 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
2287 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2288 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2289 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2294 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2296 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
2297 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
2298 ictl_ctx
[0], ictl_ctx
[1]);
2299 return CC_TRB_ERROR
;
2302 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2303 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2305 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
2306 fprintf(stderr
, "xhci: invalid slot state %08x\n", slot_ctx
[3]);
2307 return CC_CONTEXT_STATE_ERROR
;
2310 for (i
= 2; i
<= 31; i
++) {
2311 if (ictl_ctx
[0] & (1<<i
)) {
2312 xhci_disable_ep(xhci
, slotid
, i
);
2314 if (ictl_ctx
[1] & (1<<i
)) {
2315 xhci_dma_read_u32s(xhci
, ictx
+32+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2316 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
2317 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2318 ep_ctx
[3], ep_ctx
[4]);
2319 xhci_disable_ep(xhci
, slotid
, i
);
2320 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
2321 if (res
!= CC_SUCCESS
) {
2324 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
2325 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
2326 ep_ctx
[3], ep_ctx
[4]);
2327 xhci_dma_write_u32s(xhci
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
2331 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2332 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
2333 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
2334 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
2335 SLOT_CONTEXT_ENTRIES_SHIFT
);
2336 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2337 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2339 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2345 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
2348 dma_addr_t ictx
, octx
;
2349 uint32_t ictl_ctx
[2];
2350 uint32_t iep0_ctx
[5];
2351 uint32_t ep0_ctx
[5];
2352 uint32_t islot_ctx
[4];
2353 uint32_t slot_ctx
[4];
2355 trace_usb_xhci_slot_evaluate(slotid
);
2356 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2358 ictx
= xhci_mask64(pictx
);
2359 octx
= xhci
->slots
[slotid
-1].ctx
;
2361 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
2362 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2364 xhci_dma_read_u32s(xhci
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
2366 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
2367 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
2368 ictl_ctx
[0], ictl_ctx
[1]);
2369 return CC_TRB_ERROR
;
2372 if (ictl_ctx
[1] & 0x1) {
2373 xhci_dma_read_u32s(xhci
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
2375 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
2376 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
2378 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2380 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
2381 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
2382 slot_ctx
[2] &= ~0xFF00000; /* interrupter target */
2383 slot_ctx
[2] |= islot_ctx
[2] & 0xFF000000;
2385 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2386 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2388 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2391 if (ictl_ctx
[1] & 0x2) {
2392 xhci_dma_read_u32s(xhci
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
2394 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
2395 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
2396 iep0_ctx
[3], iep0_ctx
[4]);
2398 xhci_dma_read_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2400 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
2401 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
2403 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
2404 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
2406 xhci_dma_write_u32s(xhci
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
2412 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
2414 uint32_t slot_ctx
[4];
2418 trace_usb_xhci_slot_reset(slotid
);
2419 assert(slotid
>= 1 && slotid
<= xhci
->numslots
);
2421 octx
= xhci
->slots
[slotid
-1].ctx
;
2423 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
2425 for (i
= 2; i
<= 31; i
++) {
2426 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
2427 xhci_disable_ep(xhci
, slotid
, i
);
2431 xhci_dma_read_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2432 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
2433 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
2434 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
2435 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
2436 xhci_dma_write_u32s(xhci
, octx
, slot_ctx
, sizeof(slot_ctx
));
2441 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
2443 unsigned int slotid
;
2444 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
2445 if (slotid
< 1 || slotid
> xhci
->numslots
) {
2446 fprintf(stderr
, "xhci: bad slot id %d\n", slotid
);
2447 event
->ccode
= CC_TRB_ERROR
;
2449 } else if (!xhci
->slots
[slotid
-1].enabled
) {
2450 fprintf(stderr
, "xhci: slot id %d not enabled\n", slotid
);
2451 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
2457 /* cleanup slot state on usb device detach */
2458 static void xhci_detach_slot(XHCIState
*xhci
, USBPort
*uport
)
2462 for (slot
= 0; slot
< xhci
->numslots
; slot
++) {
2463 if (xhci
->slots
[slot
].uport
== uport
) {
2467 if (slot
== xhci
->numslots
) {
2471 for (ep
= 0; ep
< 31; ep
++) {
2472 if (xhci
->slots
[slot
].eps
[ep
]) {
2473 xhci_ep_nuke_xfers(xhci
, slot
+1, ep
+1);
2476 xhci
->slots
[slot
].uport
= NULL
;
2479 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
2482 uint8_t bw_ctx
[xhci
->numports
+1];
2484 DPRINTF("xhci_get_port_bandwidth()\n");
2486 ctx
= xhci_mask64(pctx
);
2488 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
2490 /* TODO: actually implement real values here */
2492 memset(&bw_ctx
[1], 80, xhci
->numports
); /* 80% */
2493 pci_dma_write(PCI_DEVICE(xhci
), ctx
, bw_ctx
, sizeof(bw_ctx
));
2498 static uint32_t rotl(uint32_t v
, unsigned count
)
2501 return (v
<< count
) | (v
>> (32 - count
));
2505 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2508 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2509 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2510 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2514 static void xhci_via_challenge(XHCIState
*xhci
, uint64_t addr
)
2516 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
2519 dma_addr_t paddr
= xhci_mask64(addr
);
2521 pci_dma_read(pci_dev
, paddr
, &buf
, 32);
2523 memcpy(obuf
, buf
, sizeof(obuf
));
2525 if ((buf
[0] & 0xff) == 2) {
2526 obuf
[0] = 0x49932000 + 0x54dc200 * buf
[2] + 0x7429b578 * buf
[3];
2527 obuf
[0] |= (buf
[2] * buf
[3]) & 0xff;
2528 obuf
[1] = 0x0132bb37 + 0xe89 * buf
[2] + 0xf09 * buf
[3];
2529 obuf
[2] = 0x0066c2e9 + 0x2091 * buf
[2] + 0x19bd * buf
[3];
2530 obuf
[3] = 0xd5281342 + 0x2cc9691 * buf
[2] + 0x2367662 * buf
[3];
2531 obuf
[4] = 0x0123c75c + 0x1595 * buf
[2] + 0x19ec * buf
[3];
2532 obuf
[5] = 0x00f695de + 0x26fd * buf
[2] + 0x3e9 * buf
[3];
2533 obuf
[6] = obuf
[2] ^ obuf
[3] ^ 0x29472956;
2534 obuf
[7] = obuf
[2] ^ obuf
[3] ^ 0x65866593;
2537 pci_dma_write(pci_dev
, paddr
, &obuf
, 32);
2540 static void xhci_process_commands(XHCIState
*xhci
)
2544 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2546 unsigned int i
, slotid
= 0;
2548 DPRINTF("xhci_process_commands()\n");
2549 if (!xhci_running(xhci
)) {
2550 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2554 xhci
->crcr_low
|= CRCR_CRR
;
2556 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2559 case CR_ENABLE_SLOT
:
2560 for (i
= 0; i
< xhci
->numslots
; i
++) {
2561 if (!xhci
->slots
[i
].enabled
) {
2565 if (i
>= xhci
->numslots
) {
2566 fprintf(stderr
, "xhci: no device slots available\n");
2567 event
.ccode
= CC_NO_SLOTS_ERROR
;
2570 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2573 case CR_DISABLE_SLOT
:
2574 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2576 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2579 case CR_ADDRESS_DEVICE
:
2580 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2582 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2583 trb
.control
& TRB_CR_BSR
);
2586 case CR_CONFIGURE_ENDPOINT
:
2587 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2589 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2590 trb
.control
& TRB_CR_DC
);
2593 case CR_EVALUATE_CONTEXT
:
2594 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2596 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2599 case CR_STOP_ENDPOINT
:
2600 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2602 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2604 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2607 case CR_RESET_ENDPOINT
:
2608 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2610 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2612 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2615 case CR_SET_TR_DEQUEUE
:
2616 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2618 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2620 unsigned int streamid
= (trb
.status
>> 16) & 0xffff;
2621 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
,
2626 case CR_RESET_DEVICE
:
2627 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2629 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2632 case CR_GET_PORT_BANDWIDTH
:
2633 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2635 case CR_VENDOR_VIA_CHALLENGE_RESPONSE
:
2636 xhci_via_challenge(xhci
, trb
.parameter
);
2638 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2639 event
.type
= 48; /* NEC reply */
2640 event
.length
= 0x3025;
2642 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2644 uint32_t chi
= trb
.parameter
>> 32;
2645 uint32_t clo
= trb
.parameter
;
2646 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2647 event
.length
= val
& 0xFFFF;
2648 event
.epid
= val
>> 16;
2650 event
.type
= 48; /* NEC reply */
2654 trace_usb_xhci_unimplemented("command", type
);
2655 event
.ccode
= CC_TRB_ERROR
;
2658 event
.slotid
= slotid
;
2659 xhci_event(xhci
, &event
, 0);
2663 static bool xhci_port_have_device(XHCIPort
*port
)
2665 if (!port
->uport
->dev
|| !port
->uport
->dev
->attached
) {
2666 return false; /* no device present */
2668 if (!((1 << port
->uport
->dev
->speed
) & port
->speedmask
)) {
2669 return false; /* speed mismatch */
2674 static void xhci_port_notify(XHCIPort
*port
, uint32_t bits
)
2676 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
,
2677 port
->portnr
<< 24 };
2679 if ((port
->portsc
& bits
) == bits
) {
2682 trace_usb_xhci_port_notify(port
->portnr
, bits
);
2683 port
->portsc
|= bits
;
2684 if (!xhci_running(port
->xhci
)) {
2687 xhci_event(port
->xhci
, &ev
, 0);
2690 static void xhci_port_update(XHCIPort
*port
, int is_detach
)
2692 uint32_t pls
= PLS_RX_DETECT
;
2694 port
->portsc
= PORTSC_PP
;
2695 if (!is_detach
&& xhci_port_have_device(port
)) {
2696 port
->portsc
|= PORTSC_CCS
;
2697 switch (port
->uport
->dev
->speed
) {
2699 port
->portsc
|= PORTSC_SPEED_LOW
;
2702 case USB_SPEED_FULL
:
2703 port
->portsc
|= PORTSC_SPEED_FULL
;
2706 case USB_SPEED_HIGH
:
2707 port
->portsc
|= PORTSC_SPEED_HIGH
;
2710 case USB_SPEED_SUPER
:
2711 port
->portsc
|= PORTSC_SPEED_SUPER
;
2712 port
->portsc
|= PORTSC_PED
;
2717 set_field(&port
->portsc
, pls
, PORTSC_PLS
);
2718 trace_usb_xhci_port_link(port
->portnr
, pls
);
2719 xhci_port_notify(port
, PORTSC_CSC
);
2722 static void xhci_port_reset(XHCIPort
*port
, bool warm_reset
)
2724 trace_usb_xhci_port_reset(port
->portnr
);
2726 if (!xhci_port_have_device(port
)) {
2730 usb_device_reset(port
->uport
->dev
);
2732 switch (port
->uport
->dev
->speed
) {
2733 case USB_SPEED_SUPER
:
2735 port
->portsc
|= PORTSC_WRC
;
2739 case USB_SPEED_FULL
:
2740 case USB_SPEED_HIGH
:
2741 set_field(&port
->portsc
, PLS_U0
, PORTSC_PLS
);
2742 trace_usb_xhci_port_link(port
->portnr
, PLS_U0
);
2743 port
->portsc
|= PORTSC_PED
;
2747 port
->portsc
&= ~PORTSC_PR
;
2748 xhci_port_notify(port
, PORTSC_PRC
);
2751 static void xhci_reset(DeviceState
*dev
)
2753 XHCIState
*xhci
= XHCI(dev
);
2756 trace_usb_xhci_reset();
2757 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2758 fprintf(stderr
, "xhci: reset while running!\n");
2762 xhci
->usbsts
= USBSTS_HCH
;
2765 xhci
->crcr_high
= 0;
2766 xhci
->dcbaap_low
= 0;
2767 xhci
->dcbaap_high
= 0;
2770 for (i
= 0; i
< xhci
->numslots
; i
++) {
2771 xhci_disable_slot(xhci
, i
+1);
2774 for (i
= 0; i
< xhci
->numports
; i
++) {
2775 xhci_port_update(xhci
->ports
+ i
, 0);
2778 for (i
= 0; i
< xhci
->numintrs
; i
++) {
2779 xhci
->intr
[i
].iman
= 0;
2780 xhci
->intr
[i
].imod
= 0;
2781 xhci
->intr
[i
].erstsz
= 0;
2782 xhci
->intr
[i
].erstba_low
= 0;
2783 xhci
->intr
[i
].erstba_high
= 0;
2784 xhci
->intr
[i
].erdp_low
= 0;
2785 xhci
->intr
[i
].erdp_high
= 0;
2786 xhci
->intr
[i
].msix_used
= 0;
2788 xhci
->intr
[i
].er_ep_idx
= 0;
2789 xhci
->intr
[i
].er_pcs
= 1;
2790 xhci
->intr
[i
].er_full
= 0;
2791 xhci
->intr
[i
].ev_buffer_put
= 0;
2792 xhci
->intr
[i
].ev_buffer_get
= 0;
2795 xhci
->mfindex_start
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
2796 xhci_mfwrap_update(xhci
);
2799 static uint64_t xhci_cap_read(void *ptr
, hwaddr reg
, unsigned size
)
2801 XHCIState
*xhci
= ptr
;
2805 case 0x00: /* HCIVERSION, CAPLENGTH */
2806 ret
= 0x01000000 | LEN_CAP
;
2808 case 0x04: /* HCSPARAMS 1 */
2809 ret
= ((xhci
->numports_2
+xhci
->numports_3
)<<24)
2810 | (xhci
->numintrs
<<8) | xhci
->numslots
;
2812 case 0x08: /* HCSPARAMS 2 */
2815 case 0x0c: /* HCSPARAMS 3 */
2818 case 0x10: /* HCCPARAMS */
2819 if (sizeof(dma_addr_t
) == 4) {
2825 case 0x14: /* DBOFF */
2828 case 0x18: /* RTSOFF */
2832 /* extended capabilities */
2833 case 0x20: /* Supported Protocol:00 */
2834 ret
= 0x02000402; /* USB 2.0 */
2836 case 0x24: /* Supported Protocol:04 */
2837 ret
= 0x20425355; /* "USB " */
2839 case 0x28: /* Supported Protocol:08 */
2840 ret
= 0x00000001 | (xhci
->numports_2
<<8);
2842 case 0x2c: /* Supported Protocol:0c */
2843 ret
= 0x00000000; /* reserved */
2845 case 0x30: /* Supported Protocol:00 */
2846 ret
= 0x03000002; /* USB 3.0 */
2848 case 0x34: /* Supported Protocol:04 */
2849 ret
= 0x20425355; /* "USB " */
2851 case 0x38: /* Supported Protocol:08 */
2852 ret
= 0x00000000 | (xhci
->numports_2
+1) | (xhci
->numports_3
<<8);
2854 case 0x3c: /* Supported Protocol:0c */
2855 ret
= 0x00000000; /* reserved */
2858 trace_usb_xhci_unimplemented("cap read", reg
);
2862 trace_usb_xhci_cap_read(reg
, ret
);
2866 static uint64_t xhci_port_read(void *ptr
, hwaddr reg
, unsigned size
)
2868 XHCIPort
*port
= ptr
;
2872 case 0x00: /* PORTSC */
2875 case 0x04: /* PORTPMSC */
2876 case 0x08: /* PORTLI */
2879 case 0x0c: /* reserved */
2881 trace_usb_xhci_unimplemented("port read", reg
);
2885 trace_usb_xhci_port_read(port
->portnr
, reg
, ret
);
2889 static void xhci_port_write(void *ptr
, hwaddr reg
,
2890 uint64_t val
, unsigned size
)
2892 XHCIPort
*port
= ptr
;
2893 uint32_t portsc
, notify
;
2895 trace_usb_xhci_port_write(port
->portnr
, reg
, val
);
2898 case 0x00: /* PORTSC */
2899 /* write-1-to-start bits */
2900 if (val
& PORTSC_WPR
) {
2901 xhci_port_reset(port
, true);
2904 if (val
& PORTSC_PR
) {
2905 xhci_port_reset(port
, false);
2909 portsc
= port
->portsc
;
2911 /* write-1-to-clear bits*/
2912 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2913 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2914 if (val
& PORTSC_LWS
) {
2915 /* overwrite PLS only when LWS=1 */
2916 uint32_t old_pls
= get_field(port
->portsc
, PORTSC_PLS
);
2917 uint32_t new_pls
= get_field(val
, PORTSC_PLS
);
2920 if (old_pls
!= PLS_U0
) {
2921 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2922 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2923 notify
= PORTSC_PLC
;
2927 if (old_pls
< PLS_U3
) {
2928 set_field(&portsc
, new_pls
, PORTSC_PLS
);
2929 trace_usb_xhci_port_link(port
->portnr
, new_pls
);
2933 /* windows does this for some reason, don't spam stderr */
2936 fprintf(stderr
, "%s: ignore pls write (old %d, new %d)\n",
2937 __func__
, old_pls
, new_pls
);
2941 /* read/write bits */
2942 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2943 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2944 port
->portsc
= portsc
;
2946 xhci_port_notify(port
, notify
);
2949 case 0x04: /* PORTPMSC */
2950 case 0x08: /* PORTLI */
2952 trace_usb_xhci_unimplemented("port write", reg
);
2956 static uint64_t xhci_oper_read(void *ptr
, hwaddr reg
, unsigned size
)
2958 XHCIState
*xhci
= ptr
;
2962 case 0x00: /* USBCMD */
2965 case 0x04: /* USBSTS */
2968 case 0x08: /* PAGESIZE */
2971 case 0x14: /* DNCTRL */
2974 case 0x18: /* CRCR low */
2975 ret
= xhci
->crcr_low
& ~0xe;
2977 case 0x1c: /* CRCR high */
2978 ret
= xhci
->crcr_high
;
2980 case 0x30: /* DCBAAP low */
2981 ret
= xhci
->dcbaap_low
;
2983 case 0x34: /* DCBAAP high */
2984 ret
= xhci
->dcbaap_high
;
2986 case 0x38: /* CONFIG */
2990 trace_usb_xhci_unimplemented("oper read", reg
);
2994 trace_usb_xhci_oper_read(reg
, ret
);
2998 static void xhci_oper_write(void *ptr
, hwaddr reg
,
2999 uint64_t val
, unsigned size
)
3001 XHCIState
*xhci
= ptr
;
3002 DeviceState
*d
= DEVICE(ptr
);
3004 trace_usb_xhci_oper_write(reg
, val
);
3007 case 0x00: /* USBCMD */
3008 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
3010 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
3013 xhci
->usbcmd
= val
& 0xc0f;
3014 xhci_mfwrap_update(xhci
);
3015 if (val
& USBCMD_HCRST
) {
3018 xhci_intx_update(xhci
);
3021 case 0x04: /* USBSTS */
3022 /* these bits are write-1-to-clear */
3023 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
3024 xhci_intx_update(xhci
);
3027 case 0x14: /* DNCTRL */
3028 xhci
->dnctrl
= val
& 0xffff;
3030 case 0x18: /* CRCR low */
3031 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
3033 case 0x1c: /* CRCR high */
3034 xhci
->crcr_high
= val
;
3035 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
3036 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
3037 xhci
->crcr_low
&= ~CRCR_CRR
;
3038 xhci_event(xhci
, &event
, 0);
3039 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
3041 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
3042 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
3044 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
3046 case 0x30: /* DCBAAP low */
3047 xhci
->dcbaap_low
= val
& 0xffffffc0;
3049 case 0x34: /* DCBAAP high */
3050 xhci
->dcbaap_high
= val
;
3052 case 0x38: /* CONFIG */
3053 xhci
->config
= val
& 0xff;
3056 trace_usb_xhci_unimplemented("oper write", reg
);
3060 static uint64_t xhci_runtime_read(void *ptr
, hwaddr reg
,
3063 XHCIState
*xhci
= ptr
;
3068 case 0x00: /* MFINDEX */
3069 ret
= xhci_mfindex_get(xhci
) & 0x3fff;
3072 trace_usb_xhci_unimplemented("runtime read", reg
);
3076 int v
= (reg
- 0x20) / 0x20;
3077 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
3078 switch (reg
& 0x1f) {
3079 case 0x00: /* IMAN */
3082 case 0x04: /* IMOD */
3085 case 0x08: /* ERSTSZ */
3088 case 0x10: /* ERSTBA low */
3089 ret
= intr
->erstba_low
;
3091 case 0x14: /* ERSTBA high */
3092 ret
= intr
->erstba_high
;
3094 case 0x18: /* ERDP low */
3095 ret
= intr
->erdp_low
;
3097 case 0x1c: /* ERDP high */
3098 ret
= intr
->erdp_high
;
3103 trace_usb_xhci_runtime_read(reg
, ret
);
3107 static void xhci_runtime_write(void *ptr
, hwaddr reg
,
3108 uint64_t val
, unsigned size
)
3110 XHCIState
*xhci
= ptr
;
3111 int v
= (reg
- 0x20) / 0x20;
3112 XHCIInterrupter
*intr
= &xhci
->intr
[v
];
3113 trace_usb_xhci_runtime_write(reg
, val
);
3116 trace_usb_xhci_unimplemented("runtime write", reg
);
3120 switch (reg
& 0x1f) {
3121 case 0x00: /* IMAN */
3122 if (val
& IMAN_IP
) {
3123 intr
->iman
&= ~IMAN_IP
;
3125 intr
->iman
&= ~IMAN_IE
;
3126 intr
->iman
|= val
& IMAN_IE
;
3128 xhci_intx_update(xhci
);
3130 xhci_msix_update(xhci
, v
);
3132 case 0x04: /* IMOD */
3135 case 0x08: /* ERSTSZ */
3136 intr
->erstsz
= val
& 0xffff;
3138 case 0x10: /* ERSTBA low */
3139 /* XXX NEC driver bug: it doesn't align this to 64 bytes
3140 intr->erstba_low = val & 0xffffffc0; */
3141 intr
->erstba_low
= val
& 0xfffffff0;
3143 case 0x14: /* ERSTBA high */
3144 intr
->erstba_high
= val
;
3145 xhci_er_reset(xhci
, v
);
3147 case 0x18: /* ERDP low */
3148 if (val
& ERDP_EHB
) {
3149 intr
->erdp_low
&= ~ERDP_EHB
;
3151 intr
->erdp_low
= (val
& ~ERDP_EHB
) | (intr
->erdp_low
& ERDP_EHB
);
3153 case 0x1c: /* ERDP high */
3154 intr
->erdp_high
= val
;
3155 xhci_events_update(xhci
, v
);
3158 trace_usb_xhci_unimplemented("oper write", reg
);
3162 static uint64_t xhci_doorbell_read(void *ptr
, hwaddr reg
,
3165 /* doorbells always read as 0 */
3166 trace_usb_xhci_doorbell_read(reg
, 0);
3170 static void xhci_doorbell_write(void *ptr
, hwaddr reg
,
3171 uint64_t val
, unsigned size
)
3173 XHCIState
*xhci
= ptr
;
3174 unsigned int epid
, streamid
;
3176 trace_usb_xhci_doorbell_write(reg
, val
);
3178 if (!xhci_running(xhci
)) {
3179 fprintf(stderr
, "xhci: wrote doorbell while xHC stopped or paused\n");
3187 xhci_process_commands(xhci
);
3189 fprintf(stderr
, "xhci: bad doorbell 0 write: 0x%x\n",
3194 streamid
= (val
>> 16) & 0xffff;
3195 if (reg
> xhci
->numslots
) {
3196 fprintf(stderr
, "xhci: bad doorbell %d\n", (int)reg
);
3197 } else if (epid
> 31) {
3198 fprintf(stderr
, "xhci: bad doorbell %d write: 0x%x\n",
3199 (int)reg
, (uint32_t)val
);
3201 xhci_kick_ep(xhci
, reg
, epid
, streamid
);
3206 static void xhci_cap_write(void *opaque
, hwaddr addr
, uint64_t val
,
3212 static const MemoryRegionOps xhci_cap_ops
= {
3213 .read
= xhci_cap_read
,
3214 .write
= xhci_cap_write
,
3215 .valid
.min_access_size
= 1,
3216 .valid
.max_access_size
= 4,
3217 .impl
.min_access_size
= 4,
3218 .impl
.max_access_size
= 4,
3219 .endianness
= DEVICE_LITTLE_ENDIAN
,
3222 static const MemoryRegionOps xhci_oper_ops
= {
3223 .read
= xhci_oper_read
,
3224 .write
= xhci_oper_write
,
3225 .valid
.min_access_size
= 4,
3226 .valid
.max_access_size
= 4,
3227 .endianness
= DEVICE_LITTLE_ENDIAN
,
3230 static const MemoryRegionOps xhci_port_ops
= {
3231 .read
= xhci_port_read
,
3232 .write
= xhci_port_write
,
3233 .valid
.min_access_size
= 4,
3234 .valid
.max_access_size
= 4,
3235 .endianness
= DEVICE_LITTLE_ENDIAN
,
3238 static const MemoryRegionOps xhci_runtime_ops
= {
3239 .read
= xhci_runtime_read
,
3240 .write
= xhci_runtime_write
,
3241 .valid
.min_access_size
= 4,
3242 .valid
.max_access_size
= 4,
3243 .endianness
= DEVICE_LITTLE_ENDIAN
,
3246 static const MemoryRegionOps xhci_doorbell_ops
= {
3247 .read
= xhci_doorbell_read
,
3248 .write
= xhci_doorbell_write
,
3249 .valid
.min_access_size
= 4,
3250 .valid
.max_access_size
= 4,
3251 .endianness
= DEVICE_LITTLE_ENDIAN
,
3254 static void xhci_attach(USBPort
*usbport
)
3256 XHCIState
*xhci
= usbport
->opaque
;
3257 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3259 xhci_port_update(port
, 0);
3262 static void xhci_detach(USBPort
*usbport
)
3264 XHCIState
*xhci
= usbport
->opaque
;
3265 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3267 xhci_detach_slot(xhci
, usbport
);
3268 xhci_port_update(port
, 1);
3271 static void xhci_wakeup(USBPort
*usbport
)
3273 XHCIState
*xhci
= usbport
->opaque
;
3274 XHCIPort
*port
= xhci_lookup_port(xhci
, usbport
);
3276 if (get_field(port
->portsc
, PORTSC_PLS
) != PLS_U3
) {
3279 set_field(&port
->portsc
, PLS_RESUME
, PORTSC_PLS
);
3280 xhci_port_notify(port
, PORTSC_PLC
);
3283 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
3285 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
3287 if (packet
->status
== USB_RET_REMOVE_FROM_QUEUE
) {
3288 xhci_ep_nuke_one_xfer(xfer
);
3291 xhci_complete_packet(xfer
);
3292 xhci_kick_ep(xfer
->xhci
, xfer
->slotid
, xfer
->epid
, xfer
->streamid
);
3295 static void xhci_child_detach(USBPort
*uport
, USBDevice
*child
)
3297 USBBus
*bus
= usb_bus_from_device(child
);
3298 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3300 xhci_detach_slot(xhci
, uport
);
3303 static USBPortOps xhci_uport_ops
= {
3304 .attach
= xhci_attach
,
3305 .detach
= xhci_detach
,
3306 .wakeup
= xhci_wakeup
,
3307 .complete
= xhci_complete
,
3308 .child_detach
= xhci_child_detach
,
3311 static int xhci_find_epid(USBEndpoint
*ep
)
3316 if (ep
->pid
== USB_TOKEN_IN
) {
3317 return ep
->nr
* 2 + 1;
3323 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
,
3324 unsigned int stream
)
3326 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
3329 DPRINTF("%s\n", __func__
);
3330 slotid
= ep
->dev
->addr
;
3331 if (slotid
== 0 || !xhci
->slots
[slotid
-1].enabled
) {
3332 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
3335 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
), stream
);
3338 static USBBusOps xhci_bus_ops
= {
3339 .wakeup_endpoint
= xhci_wakeup_endpoint
,
3342 static void usb_xhci_init(XHCIState
*xhci
)
3344 DeviceState
*dev
= DEVICE(xhci
);
3346 int i
, usbports
, speedmask
;
3348 xhci
->usbsts
= USBSTS_HCH
;
3350 if (xhci
->numports_2
> MAXPORTS_2
) {
3351 xhci
->numports_2
= MAXPORTS_2
;
3353 if (xhci
->numports_3
> MAXPORTS_3
) {
3354 xhci
->numports_3
= MAXPORTS_3
;
3356 usbports
= MAX(xhci
->numports_2
, xhci
->numports_3
);
3357 xhci
->numports
= xhci
->numports_2
+ xhci
->numports_3
;
3359 usb_bus_new(&xhci
->bus
, sizeof(xhci
->bus
), &xhci_bus_ops
, dev
);
3361 for (i
= 0; i
< usbports
; i
++) {
3363 if (i
< xhci
->numports_2
) {
3364 port
= &xhci
->ports
[i
];
3365 port
->portnr
= i
+ 1;
3366 port
->uport
= &xhci
->uports
[i
];
3368 USB_SPEED_MASK_LOW
|
3369 USB_SPEED_MASK_FULL
|
3370 USB_SPEED_MASK_HIGH
;
3371 snprintf(port
->name
, sizeof(port
->name
), "usb2 port #%d", i
+1);
3372 speedmask
|= port
->speedmask
;
3374 if (i
< xhci
->numports_3
) {
3375 port
= &xhci
->ports
[i
+ xhci
->numports_2
];
3376 port
->portnr
= i
+ 1 + xhci
->numports_2
;
3377 port
->uport
= &xhci
->uports
[i
];
3378 port
->speedmask
= USB_SPEED_MASK_SUPER
;
3379 snprintf(port
->name
, sizeof(port
->name
), "usb3 port #%d", i
+1);
3380 speedmask
|= port
->speedmask
;
3382 usb_register_port(&xhci
->bus
, &xhci
->uports
[i
], xhci
, i
,
3383 &xhci_uport_ops
, speedmask
);
3387 static int usb_xhci_initfn(struct PCIDevice
*dev
)
3391 XHCIState
*xhci
= XHCI(dev
);
3393 dev
->config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
3394 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
3395 dev
->config
[PCI_CACHE_LINE_SIZE
] = 0x10;
3396 dev
->config
[0x60] = 0x30; /* release number */
3398 usb_xhci_init(xhci
);
3400 if (xhci
->numintrs
> MAXINTRS
) {
3401 xhci
->numintrs
= MAXINTRS
;
3403 while (xhci
->numintrs
& (xhci
->numintrs
- 1)) { /* ! power of 2 */
3406 if (xhci
->numintrs
< 1) {
3409 if (xhci
->numslots
> MAXSLOTS
) {
3410 xhci
->numslots
= MAXSLOTS
;
3412 if (xhci
->numslots
< 1) {
3416 xhci
->mfwrap_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, xhci_mfwrap_timer
, xhci
);
3418 memory_region_init(&xhci
->mem
, OBJECT(xhci
), "xhci", LEN_REGS
);
3419 memory_region_init_io(&xhci
->mem_cap
, OBJECT(xhci
), &xhci_cap_ops
, xhci
,
3420 "capabilities", LEN_CAP
);
3421 memory_region_init_io(&xhci
->mem_oper
, OBJECT(xhci
), &xhci_oper_ops
, xhci
,
3422 "operational", 0x400);
3423 memory_region_init_io(&xhci
->mem_runtime
, OBJECT(xhci
), &xhci_runtime_ops
, xhci
,
3424 "runtime", LEN_RUNTIME
);
3425 memory_region_init_io(&xhci
->mem_doorbell
, OBJECT(xhci
), &xhci_doorbell_ops
, xhci
,
3426 "doorbell", LEN_DOORBELL
);
3428 memory_region_add_subregion(&xhci
->mem
, 0, &xhci
->mem_cap
);
3429 memory_region_add_subregion(&xhci
->mem
, OFF_OPER
, &xhci
->mem_oper
);
3430 memory_region_add_subregion(&xhci
->mem
, OFF_RUNTIME
, &xhci
->mem_runtime
);
3431 memory_region_add_subregion(&xhci
->mem
, OFF_DOORBELL
, &xhci
->mem_doorbell
);
3433 for (i
= 0; i
< xhci
->numports
; i
++) {
3434 XHCIPort
*port
= &xhci
->ports
[i
];
3435 uint32_t offset
= OFF_OPER
+ 0x400 + 0x10 * i
;
3437 memory_region_init_io(&port
->mem
, OBJECT(xhci
), &xhci_port_ops
, port
,
3439 memory_region_add_subregion(&xhci
->mem
, offset
, &port
->mem
);
3442 pci_register_bar(dev
, 0,
3443 PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
,
3446 ret
= pcie_endpoint_cap_init(dev
, 0xa0);
3449 if (xhci
->flags
& (1 << XHCI_FLAG_USE_MSI
)) {
3450 msi_init(dev
, 0x70, xhci
->numintrs
, true, false);
3452 if (xhci
->flags
& (1 << XHCI_FLAG_USE_MSI_X
)) {
3453 msix_init(dev
, xhci
->numintrs
,
3454 &xhci
->mem
, 0, OFF_MSIX_TABLE
,
3455 &xhci
->mem
, 0, OFF_MSIX_PBA
,
3462 static int usb_xhci_post_load(void *opaque
, int version_id
)
3464 XHCIState
*xhci
= opaque
;
3465 PCIDevice
*pci_dev
= PCI_DEVICE(xhci
);
3467 XHCIEPContext
*epctx
;
3468 dma_addr_t dcbaap
, pctx
;
3469 uint32_t slot_ctx
[4];
3471 int slotid
, epid
, state
, intr
;
3473 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
3475 for (slotid
= 1; slotid
<= xhci
->numslots
; slotid
++) {
3476 slot
= &xhci
->slots
[slotid
-1];
3477 if (!slot
->addressed
) {
3481 xhci_mask64(ldq_le_pci_dma(pci_dev
, dcbaap
+ 8 * slotid
));
3482 xhci_dma_read_u32s(xhci
, slot
->ctx
, slot_ctx
, sizeof(slot_ctx
));
3483 slot
->uport
= xhci_lookup_uport(xhci
, slot_ctx
);
3484 assert(slot
->uport
&& slot
->uport
->dev
);
3486 for (epid
= 1; epid
<= 32; epid
++) {
3487 pctx
= slot
->ctx
+ 32 * epid
;
3488 xhci_dma_read_u32s(xhci
, pctx
, ep_ctx
, sizeof(ep_ctx
));
3489 state
= ep_ctx
[0] & EP_STATE_MASK
;
3490 if (state
== EP_DISABLED
) {
3493 epctx
= xhci_alloc_epctx(xhci
, slotid
, epid
);
3494 slot
->eps
[epid
-1] = epctx
;
3495 xhci_init_epctx(epctx
, pctx
, ep_ctx
);
3496 epctx
->state
= state
;
3497 if (state
== EP_RUNNING
) {
3498 /* kick endpoint after vmload is finished */
3499 timer_mod(epctx
->kick_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
3504 for (intr
= 0; intr
< xhci
->numintrs
; intr
++) {
3505 if (xhci
->intr
[intr
].msix_used
) {
3506 msix_vector_use(pci_dev
, intr
);
3508 msix_vector_unuse(pci_dev
, intr
);
3515 static const VMStateDescription vmstate_xhci_ring
= {
3516 .name
= "xhci-ring",
3518 .fields
= (VMStateField
[]) {
3519 VMSTATE_UINT64(dequeue
, XHCIRing
),
3520 VMSTATE_BOOL(ccs
, XHCIRing
),
3521 VMSTATE_END_OF_LIST()
3525 static const VMStateDescription vmstate_xhci_port
= {
3526 .name
= "xhci-port",
3528 .fields
= (VMStateField
[]) {
3529 VMSTATE_UINT32(portsc
, XHCIPort
),
3530 VMSTATE_END_OF_LIST()
3534 static const VMStateDescription vmstate_xhci_slot
= {
3535 .name
= "xhci-slot",
3537 .fields
= (VMStateField
[]) {
3538 VMSTATE_BOOL(enabled
, XHCISlot
),
3539 VMSTATE_BOOL(addressed
, XHCISlot
),
3540 VMSTATE_END_OF_LIST()
3544 static const VMStateDescription vmstate_xhci_event
= {
3545 .name
= "xhci-event",
3547 .fields
= (VMStateField
[]) {
3548 VMSTATE_UINT32(type
, XHCIEvent
),
3549 VMSTATE_UINT32(ccode
, XHCIEvent
),
3550 VMSTATE_UINT64(ptr
, XHCIEvent
),
3551 VMSTATE_UINT32(length
, XHCIEvent
),
3552 VMSTATE_UINT32(flags
, XHCIEvent
),
3553 VMSTATE_UINT8(slotid
, XHCIEvent
),
3554 VMSTATE_UINT8(epid
, XHCIEvent
),
3558 static bool xhci_er_full(void *opaque
, int version_id
)
3560 struct XHCIInterrupter
*intr
= opaque
;
3561 return intr
->er_full
;
3564 static const VMStateDescription vmstate_xhci_intr
= {
3565 .name
= "xhci-intr",
3567 .fields
= (VMStateField
[]) {
3569 VMSTATE_UINT32(iman
, XHCIInterrupter
),
3570 VMSTATE_UINT32(imod
, XHCIInterrupter
),
3571 VMSTATE_UINT32(erstsz
, XHCIInterrupter
),
3572 VMSTATE_UINT32(erstba_low
, XHCIInterrupter
),
3573 VMSTATE_UINT32(erstba_high
, XHCIInterrupter
),
3574 VMSTATE_UINT32(erdp_low
, XHCIInterrupter
),
3575 VMSTATE_UINT32(erdp_high
, XHCIInterrupter
),
3578 VMSTATE_BOOL(msix_used
, XHCIInterrupter
),
3579 VMSTATE_BOOL(er_pcs
, XHCIInterrupter
),
3580 VMSTATE_UINT64(er_start
, XHCIInterrupter
),
3581 VMSTATE_UINT32(er_size
, XHCIInterrupter
),
3582 VMSTATE_UINT32(er_ep_idx
, XHCIInterrupter
),
3584 /* event queue (used if ring is full) */
3585 VMSTATE_BOOL(er_full
, XHCIInterrupter
),
3586 VMSTATE_UINT32_TEST(ev_buffer_put
, XHCIInterrupter
, xhci_er_full
),
3587 VMSTATE_UINT32_TEST(ev_buffer_get
, XHCIInterrupter
, xhci_er_full
),
3588 VMSTATE_STRUCT_ARRAY_TEST(ev_buffer
, XHCIInterrupter
, EV_QUEUE
,
3590 vmstate_xhci_event
, XHCIEvent
),
3592 VMSTATE_END_OF_LIST()
3596 static const VMStateDescription vmstate_xhci
= {
3599 .post_load
= usb_xhci_post_load
,
3600 .fields
= (VMStateField
[]) {
3601 VMSTATE_PCIE_DEVICE(parent_obj
, XHCIState
),
3602 VMSTATE_MSIX(parent_obj
, XHCIState
),
3604 VMSTATE_STRUCT_VARRAY_UINT32(ports
, XHCIState
, numports
, 1,
3605 vmstate_xhci_port
, XHCIPort
),
3606 VMSTATE_STRUCT_VARRAY_UINT32(slots
, XHCIState
, numslots
, 1,
3607 vmstate_xhci_slot
, XHCISlot
),
3608 VMSTATE_STRUCT_VARRAY_UINT32(intr
, XHCIState
, numintrs
, 1,
3609 vmstate_xhci_intr
, XHCIInterrupter
),
3611 /* Operational Registers */
3612 VMSTATE_UINT32(usbcmd
, XHCIState
),
3613 VMSTATE_UINT32(usbsts
, XHCIState
),
3614 VMSTATE_UINT32(dnctrl
, XHCIState
),
3615 VMSTATE_UINT32(crcr_low
, XHCIState
),
3616 VMSTATE_UINT32(crcr_high
, XHCIState
),
3617 VMSTATE_UINT32(dcbaap_low
, XHCIState
),
3618 VMSTATE_UINT32(dcbaap_high
, XHCIState
),
3619 VMSTATE_UINT32(config
, XHCIState
),
3621 /* Runtime Registers & state */
3622 VMSTATE_INT64(mfindex_start
, XHCIState
),
3623 VMSTATE_TIMER(mfwrap_timer
, XHCIState
),
3624 VMSTATE_STRUCT(cmd_ring
, XHCIState
, 1, vmstate_xhci_ring
, XHCIRing
),
3626 VMSTATE_END_OF_LIST()
3630 static Property xhci_properties
[] = {
3631 DEFINE_PROP_BIT("msi", XHCIState
, flags
, XHCI_FLAG_USE_MSI
, true),
3632 DEFINE_PROP_BIT("msix", XHCIState
, flags
, XHCI_FLAG_USE_MSI_X
, true),
3633 DEFINE_PROP_UINT32("intrs", XHCIState
, numintrs
, MAXINTRS
),
3634 DEFINE_PROP_UINT32("slots", XHCIState
, numslots
, MAXSLOTS
),
3635 DEFINE_PROP_UINT32("p2", XHCIState
, numports_2
, 4),
3636 DEFINE_PROP_UINT32("p3", XHCIState
, numports_3
, 4),
3637 DEFINE_PROP_END_OF_LIST(),
3640 static void xhci_class_init(ObjectClass
*klass
, void *data
)
3642 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
3643 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3645 dc
->vmsd
= &vmstate_xhci
;
3646 dc
->props
= xhci_properties
;
3647 dc
->reset
= xhci_reset
;
3648 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
3649 k
->init
= usb_xhci_initfn
;
3650 k
->vendor_id
= PCI_VENDOR_ID_NEC
;
3651 k
->device_id
= PCI_DEVICE_ID_NEC_UPD720200
;
3652 k
->class_id
= PCI_CLASS_SERIAL_USB
;
3658 static const TypeInfo xhci_info
= {
3660 .parent
= TYPE_PCI_DEVICE
,
3661 .instance_size
= sizeof(XHCIState
),
3662 .class_init
= xhci_class_init
,
3665 static void xhci_register_types(void)
3667 type_register_static(&xhci_info
);
3670 type_init(xhci_register_types
)