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"
32 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__)
34 #define DPRINTF(...) do {} while (0)
36 #define FIXME() do { fprintf(stderr, "FIXME %s:%d\n", \
37 __func__, __LINE__); abort(); } while (0)
45 #define MAXPORTS (USB2_PORTS+USB3_PORTS)
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 OFF_OPER LEN_CAP
57 #define LEN_OPER (0x400 + 0x10 * MAXPORTS)
58 #define OFF_RUNTIME ((OFF_OPER + LEN_OPER + 0x20) & ~0x1f)
59 #define LEN_RUNTIME (0x20 + MAXINTRS * 0x20)
60 #define OFF_DOORBELL (OFF_RUNTIME + LEN_RUNTIME)
61 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
63 /* must be power of 2 */
64 #define LEN_REGS 0x2000
66 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
67 # error Increase LEN_REGS
71 # error TODO: only one interrupter supported
75 #define USBCMD_RS (1<<0)
76 #define USBCMD_HCRST (1<<1)
77 #define USBCMD_INTE (1<<2)
78 #define USBCMD_HSEE (1<<3)
79 #define USBCMD_LHCRST (1<<7)
80 #define USBCMD_CSS (1<<8)
81 #define USBCMD_CRS (1<<9)
82 #define USBCMD_EWE (1<<10)
83 #define USBCMD_EU3S (1<<11)
85 #define USBSTS_HCH (1<<0)
86 #define USBSTS_HSE (1<<2)
87 #define USBSTS_EINT (1<<3)
88 #define USBSTS_PCD (1<<4)
89 #define USBSTS_SSS (1<<8)
90 #define USBSTS_RSS (1<<9)
91 #define USBSTS_SRE (1<<10)
92 #define USBSTS_CNR (1<<11)
93 #define USBSTS_HCE (1<<12)
96 #define PORTSC_CCS (1<<0)
97 #define PORTSC_PED (1<<1)
98 #define PORTSC_OCA (1<<3)
99 #define PORTSC_PR (1<<4)
100 #define PORTSC_PLS_SHIFT 5
101 #define PORTSC_PLS_MASK 0xf
102 #define PORTSC_PP (1<<9)
103 #define PORTSC_SPEED_SHIFT 10
104 #define PORTSC_SPEED_MASK 0xf
105 #define PORTSC_SPEED_FULL (1<<10)
106 #define PORTSC_SPEED_LOW (2<<10)
107 #define PORTSC_SPEED_HIGH (3<<10)
108 #define PORTSC_SPEED_SUPER (4<<10)
109 #define PORTSC_PIC_SHIFT 14
110 #define PORTSC_PIC_MASK 0x3
111 #define PORTSC_LWS (1<<16)
112 #define PORTSC_CSC (1<<17)
113 #define PORTSC_PEC (1<<18)
114 #define PORTSC_WRC (1<<19)
115 #define PORTSC_OCC (1<<20)
116 #define PORTSC_PRC (1<<21)
117 #define PORTSC_PLC (1<<22)
118 #define PORTSC_CEC (1<<23)
119 #define PORTSC_CAS (1<<24)
120 #define PORTSC_WCE (1<<25)
121 #define PORTSC_WDE (1<<26)
122 #define PORTSC_WOE (1<<27)
123 #define PORTSC_DR (1<<30)
124 #define PORTSC_WPR (1<<31)
126 #define CRCR_RCS (1<<0)
127 #define CRCR_CS (1<<1)
128 #define CRCR_CA (1<<2)
129 #define CRCR_CRR (1<<3)
131 #define IMAN_IP (1<<0)
132 #define IMAN_IE (1<<1)
134 #define ERDP_EHB (1<<3)
137 typedef struct XHCITRB
{
146 typedef enum TRBType
{
159 CR_CONFIGURE_ENDPOINT
,
167 CR_SET_LATENCY_TOLERANCE
,
168 CR_GET_PORT_BANDWIDTH
,
173 ER_PORT_STATUS_CHANGE
,
174 ER_BANDWIDTH_REQUEST
,
177 ER_DEVICE_NOTIFICATION
,
179 /* vendor specific bits */
180 CR_VENDOR_VIA_CHALLENGE_RESPONSE
= 48,
181 CR_VENDOR_NEC_FIRMWARE_REVISION
= 49,
182 CR_VENDOR_NEC_CHALLENGE_RESPONSE
= 50,
185 #define CR_LINK TR_LINK
187 typedef enum TRBCCode
{
190 CC_DATA_BUFFER_ERROR
,
192 CC_USB_TRANSACTION_ERROR
,
198 CC_INVALID_STREAM_TYPE_ERROR
,
199 CC_SLOT_NOT_ENABLED_ERROR
,
200 CC_EP_NOT_ENABLED_ERROR
,
206 CC_BANDWIDTH_OVERRUN
,
207 CC_CONTEXT_STATE_ERROR
,
208 CC_NO_PING_RESPONSE_ERROR
,
209 CC_EVENT_RING_FULL_ERROR
,
210 CC_INCOMPATIBLE_DEVICE_ERROR
,
211 CC_MISSED_SERVICE_ERROR
,
212 CC_COMMAND_RING_STOPPED
,
215 CC_STOPPED_LENGTH_INVALID
,
216 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
= 29,
217 CC_ISOCH_BUFFER_OVERRUN
= 31,
220 CC_INVALID_STREAM_ID_ERROR
,
221 CC_SECONDARY_BANDWIDTH_ERROR
,
222 CC_SPLIT_TRANSACTION_ERROR
226 #define TRB_TYPE_SHIFT 10
227 #define TRB_TYPE_MASK 0x3f
228 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
230 #define TRB_EV_ED (1<<2)
232 #define TRB_TR_ENT (1<<1)
233 #define TRB_TR_ISP (1<<2)
234 #define TRB_TR_NS (1<<3)
235 #define TRB_TR_CH (1<<4)
236 #define TRB_TR_IOC (1<<5)
237 #define TRB_TR_IDT (1<<6)
238 #define TRB_TR_TBC_SHIFT 7
239 #define TRB_TR_TBC_MASK 0x3
240 #define TRB_TR_BEI (1<<9)
241 #define TRB_TR_TLBPC_SHIFT 16
242 #define TRB_TR_TLBPC_MASK 0xf
243 #define TRB_TR_FRAMEID_SHIFT 20
244 #define TRB_TR_FRAMEID_MASK 0x7ff
245 #define TRB_TR_SIA (1<<31)
247 #define TRB_TR_DIR (1<<16)
249 #define TRB_CR_SLOTID_SHIFT 24
250 #define TRB_CR_SLOTID_MASK 0xff
251 #define TRB_CR_EPID_SHIFT 16
252 #define TRB_CR_EPID_MASK 0x1f
254 #define TRB_CR_BSR (1<<9)
255 #define TRB_CR_DC (1<<9)
257 #define TRB_LK_TC (1<<1)
259 #define EP_TYPE_MASK 0x7
260 #define EP_TYPE_SHIFT 3
262 #define EP_STATE_MASK 0x7
263 #define EP_DISABLED (0<<0)
264 #define EP_RUNNING (1<<0)
265 #define EP_HALTED (2<<0)
266 #define EP_STOPPED (3<<0)
267 #define EP_ERROR (4<<0)
269 #define SLOT_STATE_MASK 0x1f
270 #define SLOT_STATE_SHIFT 27
271 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
272 #define SLOT_ENABLED 0
273 #define SLOT_DEFAULT 1
274 #define SLOT_ADDRESSED 2
275 #define SLOT_CONFIGURED 3
277 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
278 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
280 typedef enum EPType
{
291 typedef struct XHCIRing
{
297 typedef struct XHCIPort
{
303 typedef struct XHCIState XHCIState
;
305 typedef struct XHCITransfer
{
312 unsigned int iso_pkts
;
318 unsigned int trb_count
;
319 unsigned int trb_alloced
;
322 unsigned int data_length
;
323 unsigned int data_alloced
;
329 unsigned int pktsize
;
330 unsigned int cur_pkt
;
333 typedef struct XHCIEPContext
{
335 unsigned int next_xfer
;
336 unsigned int comp_xfer
;
337 XHCITransfer transfers
[TD_QUEUE
];
341 unsigned int max_psize
;
345 typedef struct XHCISlot
{
349 unsigned int devaddr
;
350 XHCIEPContext
* eps
[31];
353 typedef struct XHCIEvent
{
370 unsigned int devaddr
;
372 /* Operational Registers */
379 uint32_t dcbaap_high
;
382 XHCIPort ports
[MAXPORTS
];
383 XHCISlot slots
[MAXSLOTS
];
385 /* Runtime Registers */
387 /* note: we only support one interrupter */
392 uint32_t erstba_high
;
399 unsigned int er_ep_idx
;
402 XHCIEvent ev_buffer
[EV_QUEUE
];
403 unsigned int ev_buffer_put
;
404 unsigned int ev_buffer_get
;
409 typedef struct XHCIEvRingSeg
{
416 static const char *TRBType_names
[] = {
417 [TRB_RESERVED
] = "TRB_RESERVED",
418 [TR_NORMAL
] = "TR_NORMAL",
419 [TR_SETUP
] = "TR_SETUP",
420 [TR_DATA
] = "TR_DATA",
421 [TR_STATUS
] = "TR_STATUS",
422 [TR_ISOCH
] = "TR_ISOCH",
423 [TR_LINK
] = "TR_LINK",
424 [TR_EVDATA
] = "TR_EVDATA",
425 [TR_NOOP
] = "TR_NOOP",
426 [CR_ENABLE_SLOT
] = "CR_ENABLE_SLOT",
427 [CR_DISABLE_SLOT
] = "CR_DISABLE_SLOT",
428 [CR_ADDRESS_DEVICE
] = "CR_ADDRESS_DEVICE",
429 [CR_CONFIGURE_ENDPOINT
] = "CR_CONFIGURE_ENDPOINT",
430 [CR_EVALUATE_CONTEXT
] = "CR_EVALUATE_CONTEXT",
431 [CR_RESET_ENDPOINT
] = "CR_RESET_ENDPOINT",
432 [CR_STOP_ENDPOINT
] = "CR_STOP_ENDPOINT",
433 [CR_SET_TR_DEQUEUE
] = "CR_SET_TR_DEQUEUE",
434 [CR_RESET_DEVICE
] = "CR_RESET_DEVICE",
435 [CR_FORCE_EVENT
] = "CR_FORCE_EVENT",
436 [CR_NEGOTIATE_BW
] = "CR_NEGOTIATE_BW",
437 [CR_SET_LATENCY_TOLERANCE
] = "CR_SET_LATENCY_TOLERANCE",
438 [CR_GET_PORT_BANDWIDTH
] = "CR_GET_PORT_BANDWIDTH",
439 [CR_FORCE_HEADER
] = "CR_FORCE_HEADER",
440 [CR_NOOP
] = "CR_NOOP",
441 [ER_TRANSFER
] = "ER_TRANSFER",
442 [ER_COMMAND_COMPLETE
] = "ER_COMMAND_COMPLETE",
443 [ER_PORT_STATUS_CHANGE
] = "ER_PORT_STATUS_CHANGE",
444 [ER_BANDWIDTH_REQUEST
] = "ER_BANDWIDTH_REQUEST",
445 [ER_DOORBELL
] = "ER_DOORBELL",
446 [ER_HOST_CONTROLLER
] = "ER_HOST_CONTROLLER",
447 [ER_DEVICE_NOTIFICATION
] = "ER_DEVICE_NOTIFICATION",
448 [ER_MFINDEX_WRAP
] = "ER_MFINDEX_WRAP",
449 [CR_VENDOR_VIA_CHALLENGE_RESPONSE
] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE",
450 [CR_VENDOR_NEC_FIRMWARE_REVISION
] = "CR_VENDOR_NEC_FIRMWARE_REVISION",
451 [CR_VENDOR_NEC_CHALLENGE_RESPONSE
] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE",
454 static const char *lookup_name(uint32_t index
, const char **list
, uint32_t llen
)
456 if (index
>= llen
|| list
[index
] == NULL
) {
462 static const char *trb_name(XHCITRB
*trb
)
464 return lookup_name(TRB_TYPE(*trb
), TRBType_names
,
465 ARRAY_SIZE(TRBType_names
));
468 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
471 static inline dma_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
473 if (sizeof(dma_addr_t
) == 4) {
476 return low
| (((dma_addr_t
)high
<< 16) << 16);
480 static inline dma_addr_t
xhci_mask64(uint64_t addr
)
482 if (sizeof(dma_addr_t
) == 4) {
483 return addr
& 0xffffffff;
489 static void xhci_irq_update(XHCIState
*xhci
)
493 if (xhci
->iman
& IMAN_IP
&& xhci
->iman
& IMAN_IE
&&
494 xhci
->usbcmd
& USBCMD_INTE
) {
498 if (xhci
->msi
&& msi_enabled(&xhci
->pci_dev
)) {
500 trace_usb_xhci_irq_msi(0);
501 msi_notify(&xhci
->pci_dev
, 0);
504 trace_usb_xhci_irq_intx(level
);
505 qemu_set_irq(xhci
->irq
, level
);
509 static inline int xhci_running(XHCIState
*xhci
)
511 return !(xhci
->usbsts
& USBSTS_HCH
) && !xhci
->er_full
;
514 static void xhci_die(XHCIState
*xhci
)
516 xhci
->usbsts
|= USBSTS_HCE
;
517 fprintf(stderr
, "xhci: asserted controller error\n");
520 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
)
525 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
526 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
527 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
528 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
530 ev_trb
.control
|= TRB_C
;
532 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
534 trace_usb_xhci_queue_event(xhci
->er_ep_idx
, trb_name(&ev_trb
),
535 ev_trb
.parameter
, ev_trb
.status
, ev_trb
.control
);
537 addr
= xhci
->er_start
+ TRB_SIZE
*xhci
->er_ep_idx
;
538 pci_dma_write(&xhci
->pci_dev
, addr
, &ev_trb
, TRB_SIZE
);
541 if (xhci
->er_ep_idx
>= xhci
->er_size
) {
543 xhci
->er_pcs
= !xhci
->er_pcs
;
547 static void xhci_events_update(XHCIState
*xhci
)
553 if (xhci
->usbsts
& USBSTS_HCH
) {
557 erdp
= xhci_addr64(xhci
->erdp_low
, xhci
->erdp_high
);
558 if (erdp
< xhci
->er_start
||
559 erdp
>= (xhci
->er_start
+ TRB_SIZE
*xhci
->er_size
)) {
560 fprintf(stderr
, "xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
561 fprintf(stderr
, "xhci: ER at "DMA_ADDR_FMT
" len %d\n",
562 xhci
->er_start
, xhci
->er_size
);
566 dp_idx
= (erdp
- xhci
->er_start
) / TRB_SIZE
;
567 assert(dp_idx
< xhci
->er_size
);
569 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus
570 * deadlocks when the ER is full. Hack it by holding off events until
571 * the driver decides to free at least half of the ring */
573 int er_free
= dp_idx
- xhci
->er_ep_idx
;
575 er_free
+= xhci
->er_size
;
577 if (er_free
< (xhci
->er_size
/2)) {
578 DPRINTF("xhci_events_update(): event ring still "
579 "more than half full (hack)\n");
584 while (xhci
->ev_buffer_put
!= xhci
->ev_buffer_get
) {
585 assert(xhci
->er_full
);
586 if (((xhci
->er_ep_idx
+1) % xhci
->er_size
) == dp_idx
) {
587 DPRINTF("xhci_events_update(): event ring full again\n");
589 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
590 xhci_write_event(xhci
, &full
);
595 XHCIEvent
*event
= &xhci
->ev_buffer
[xhci
->ev_buffer_get
];
596 xhci_write_event(xhci
, event
);
597 xhci
->ev_buffer_get
++;
599 if (xhci
->ev_buffer_get
== EV_QUEUE
) {
600 xhci
->ev_buffer_get
= 0;
605 xhci
->erdp_low
|= ERDP_EHB
;
606 xhci
->iman
|= IMAN_IP
;
607 xhci
->usbsts
|= USBSTS_EINT
;
608 xhci_irq_update(xhci
);
611 if (xhci
->er_full
&& xhci
->ev_buffer_put
== xhci
->ev_buffer_get
) {
612 DPRINTF("xhci_events_update(): event ring no longer full\n");
618 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
)
624 DPRINTF("xhci_event(): ER full, queueing\n");
625 if (((xhci
->ev_buffer_put
+1) % EV_QUEUE
) == xhci
->ev_buffer_get
) {
626 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
629 xhci
->ev_buffer
[xhci
->ev_buffer_put
++] = *event
;
630 if (xhci
->ev_buffer_put
== EV_QUEUE
) {
631 xhci
->ev_buffer_put
= 0;
636 erdp
= xhci_addr64(xhci
->erdp_low
, xhci
->erdp_high
);
637 if (erdp
< xhci
->er_start
||
638 erdp
>= (xhci
->er_start
+ TRB_SIZE
*xhci
->er_size
)) {
639 fprintf(stderr
, "xhci: ERDP out of bounds: "DMA_ADDR_FMT
"\n", erdp
);
640 fprintf(stderr
, "xhci: ER at "DMA_ADDR_FMT
" len %d\n",
641 xhci
->er_start
, xhci
->er_size
);
646 dp_idx
= (erdp
- xhci
->er_start
) / TRB_SIZE
;
647 assert(dp_idx
< xhci
->er_size
);
649 if ((xhci
->er_ep_idx
+1) % xhci
->er_size
== dp_idx
) {
650 DPRINTF("xhci_event(): ER full, queueing\n");
652 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
653 xhci_write_event(xhci
, &full
);
656 if (((xhci
->ev_buffer_put
+1) % EV_QUEUE
) == xhci
->ev_buffer_get
) {
657 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
660 xhci
->ev_buffer
[xhci
->ev_buffer_put
++] = *event
;
661 if (xhci
->ev_buffer_put
== EV_QUEUE
) {
662 xhci
->ev_buffer_put
= 0;
665 xhci_write_event(xhci
, event
);
668 xhci
->erdp_low
|= ERDP_EHB
;
669 xhci
->iman
|= IMAN_IP
;
670 xhci
->usbsts
|= USBSTS_EINT
;
672 xhci_irq_update(xhci
);
675 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
679 ring
->dequeue
= base
;
683 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
688 pci_dma_read(&xhci
->pci_dev
, ring
->dequeue
, trb
, TRB_SIZE
);
689 trb
->addr
= ring
->dequeue
;
690 trb
->ccs
= ring
->ccs
;
691 le64_to_cpus(&trb
->parameter
);
692 le32_to_cpus(&trb
->status
);
693 le32_to_cpus(&trb
->control
);
695 trace_usb_xhci_fetch_trb(ring
->dequeue
, trb_name(trb
),
696 trb
->parameter
, trb
->status
, trb
->control
);
698 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
702 type
= TRB_TYPE(*trb
);
704 if (type
!= TR_LINK
) {
706 *addr
= ring
->dequeue
;
708 ring
->dequeue
+= TRB_SIZE
;
711 ring
->dequeue
= xhci_mask64(trb
->parameter
);
712 if (trb
->control
& TRB_LK_TC
) {
713 ring
->ccs
= !ring
->ccs
;
719 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
723 dma_addr_t dequeue
= ring
->dequeue
;
724 bool ccs
= ring
->ccs
;
725 /* hack to bundle together the two/three TDs that make a setup transfer */
726 bool control_td_set
= 0;
730 pci_dma_read(&xhci
->pci_dev
, dequeue
, &trb
, TRB_SIZE
);
731 le64_to_cpus(&trb
.parameter
);
732 le32_to_cpus(&trb
.status
);
733 le32_to_cpus(&trb
.control
);
735 if ((trb
.control
& TRB_C
) != ccs
) {
739 type
= TRB_TYPE(trb
);
741 if (type
== TR_LINK
) {
742 dequeue
= xhci_mask64(trb
.parameter
);
743 if (trb
.control
& TRB_LK_TC
) {
752 if (type
== TR_SETUP
) {
754 } else if (type
== TR_STATUS
) {
758 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
764 static void xhci_er_reset(XHCIState
*xhci
)
768 /* cache the (sole) event ring segment location */
769 if (xhci
->erstsz
!= 1) {
770 fprintf(stderr
, "xhci: invalid value for ERSTSZ: %d\n", xhci
->erstsz
);
774 dma_addr_t erstba
= xhci_addr64(xhci
->erstba_low
, xhci
->erstba_high
);
775 pci_dma_read(&xhci
->pci_dev
, erstba
, &seg
, sizeof(seg
));
776 le32_to_cpus(&seg
.addr_low
);
777 le32_to_cpus(&seg
.addr_high
);
778 le32_to_cpus(&seg
.size
);
779 if (seg
.size
< 16 || seg
.size
> 4096) {
780 fprintf(stderr
, "xhci: invalid value for segment size: %d\n", seg
.size
);
784 xhci
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
785 xhci
->er_size
= seg
.size
;
791 DPRINTF("xhci: event ring:" DMA_ADDR_FMT
" [%d]\n",
792 xhci
->er_start
, xhci
->er_size
);
795 static void xhci_run(XHCIState
*xhci
)
797 trace_usb_xhci_run();
798 xhci
->usbsts
&= ~USBSTS_HCH
;
801 static void xhci_stop(XHCIState
*xhci
)
803 trace_usb_xhci_stop();
804 xhci
->usbsts
|= USBSTS_HCH
;
805 xhci
->crcr_low
&= ~CRCR_CRR
;
808 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
812 if (epctx
->state
== state
) {
816 pci_dma_read(&xhci
->pci_dev
, epctx
->pctx
, ctx
, sizeof(ctx
));
817 ctx
[0] &= ~EP_STATE_MASK
;
819 ctx
[2] = epctx
->ring
.dequeue
| epctx
->ring
.ccs
;
820 ctx
[3] = (epctx
->ring
.dequeue
>> 16) >> 16;
821 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT
" state=%d dequeue=%08x%08x\n",
822 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
823 pci_dma_write(&xhci
->pci_dev
, epctx
->pctx
, ctx
, sizeof(ctx
));
824 epctx
->state
= state
;
827 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
828 unsigned int epid
, dma_addr_t pctx
,
832 XHCIEPContext
*epctx
;
836 trace_usb_xhci_ep_enable(slotid
, epid
);
837 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
838 assert(epid
>= 1 && epid
<= 31);
840 slot
= &xhci
->slots
[slotid
-1];
841 if (slot
->eps
[epid
-1]) {
842 fprintf(stderr
, "xhci: slot %d ep %d already enabled!\n", slotid
, epid
);
846 epctx
= g_malloc(sizeof(XHCIEPContext
));
847 memset(epctx
, 0, sizeof(XHCIEPContext
));
849 slot
->eps
[epid
-1] = epctx
;
851 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
852 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
);
853 epctx
->ring
.ccs
= ctx
[2] & 1;
855 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
856 DPRINTF("xhci: endpoint %d.%d type is %d\n", epid
/2, epid
%2, epctx
->type
);
858 epctx
->max_psize
= ctx
[1]>>16;
859 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
860 DPRINTF("xhci: endpoint %d.%d max transaction (burst) size is %d\n",
861 epid
/2, epid
%2, epctx
->max_psize
);
862 for (i
= 0; i
< ARRAY_SIZE(epctx
->transfers
); i
++) {
863 usb_packet_init(&epctx
->transfers
[i
].packet
);
866 epctx
->state
= EP_RUNNING
;
867 ctx
[0] &= ~EP_STATE_MASK
;
868 ctx
[0] |= EP_RUNNING
;
873 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
877 XHCIEPContext
*epctx
;
878 int i
, xferi
, killed
= 0;
879 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
880 assert(epid
>= 1 && epid
<= 31);
882 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
884 slot
= &xhci
->slots
[slotid
-1];
886 if (!slot
->eps
[epid
-1]) {
890 epctx
= slot
->eps
[epid
-1];
892 xferi
= epctx
->next_xfer
;
893 for (i
= 0; i
< TD_QUEUE
; i
++) {
894 XHCITransfer
*t
= &epctx
->transfers
[xferi
];
895 if (t
->running_async
) {
896 usb_cancel_packet(&t
->packet
);
897 t
->running_async
= 0;
899 DPRINTF("xhci: cancelling transfer %d, waiting for it to complete...\n", i
);
902 if (t
->running_retry
) {
903 t
->running_retry
= 0;
915 t
->trb_count
= t
->trb_alloced
= 0;
916 t
->data_length
= t
->data_alloced
= 0;
917 xferi
= (xferi
+ 1) % TD_QUEUE
;
922 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
926 XHCIEPContext
*epctx
;
928 trace_usb_xhci_ep_disable(slotid
, epid
);
929 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
930 assert(epid
>= 1 && epid
<= 31);
932 slot
= &xhci
->slots
[slotid
-1];
934 if (!slot
->eps
[epid
-1]) {
935 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
939 xhci_ep_nuke_xfers(xhci
, slotid
, epid
);
941 epctx
= slot
->eps
[epid
-1];
943 xhci_set_ep_state(xhci
, epctx
, EP_DISABLED
);
946 slot
->eps
[epid
-1] = NULL
;
951 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
955 XHCIEPContext
*epctx
;
957 trace_usb_xhci_ep_stop(slotid
, epid
);
958 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
960 if (epid
< 1 || epid
> 31) {
961 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
965 slot
= &xhci
->slots
[slotid
-1];
967 if (!slot
->eps
[epid
-1]) {
968 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
969 return CC_EP_NOT_ENABLED_ERROR
;
972 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
973 fprintf(stderr
, "xhci: FIXME: endpoint stopped w/ xfers running, "
974 "data might be lost\n");
977 epctx
= slot
->eps
[epid
-1];
979 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
984 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
988 XHCIEPContext
*epctx
;
991 trace_usb_xhci_ep_reset(slotid
, epid
);
992 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
994 if (epid
< 1 || epid
> 31) {
995 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
999 slot
= &xhci
->slots
[slotid
-1];
1001 if (!slot
->eps
[epid
-1]) {
1002 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1003 return CC_EP_NOT_ENABLED_ERROR
;
1006 epctx
= slot
->eps
[epid
-1];
1008 if (epctx
->state
!= EP_HALTED
) {
1009 fprintf(stderr
, "xhci: reset EP while EP %d not halted (%d)\n",
1010 epid
, epctx
->state
);
1011 return CC_CONTEXT_STATE_ERROR
;
1014 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
1015 fprintf(stderr
, "xhci: FIXME: endpoint reset w/ xfers running, "
1016 "data might be lost\n");
1019 uint8_t ep
= epid
>>1;
1025 dev
= xhci
->ports
[xhci
->slots
[slotid
-1].port
-1].port
.dev
;
1027 return CC_USB_TRANSACTION_ERROR
;
1030 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1035 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1036 unsigned int epid
, uint64_t pdequeue
)
1039 XHCIEPContext
*epctx
;
1042 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1044 if (epid
< 1 || epid
> 31) {
1045 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1046 return CC_TRB_ERROR
;
1049 DPRINTF("xhci_set_ep_dequeue(%d, %d, %016"PRIx64
")\n", slotid
, epid
, pdequeue
);
1050 dequeue
= xhci_mask64(pdequeue
);
1052 slot
= &xhci
->slots
[slotid
-1];
1054 if (!slot
->eps
[epid
-1]) {
1055 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1056 return CC_EP_NOT_ENABLED_ERROR
;
1059 epctx
= slot
->eps
[epid
-1];
1062 if (epctx
->state
!= EP_STOPPED
) {
1063 fprintf(stderr
, "xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1064 return CC_CONTEXT_STATE_ERROR
;
1067 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1068 epctx
->ring
.ccs
= dequeue
& 1;
1070 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1075 static int xhci_xfer_data(XHCITransfer
*xfer
, uint8_t *data
,
1076 unsigned int length
, bool in_xfer
, bool out_xfer
,
1081 unsigned int transferred
= 0;
1082 unsigned int left
= length
;
1085 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1086 XHCIState
*xhci
= xfer
->xhci
;
1088 DPRINTF("xhci_xfer_data(len=%d, in_xfer=%d, out_xfer=%d, report=%d)\n",
1089 length
, in_xfer
, out_xfer
, report
);
1091 assert(!(in_xfer
&& out_xfer
));
1093 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1094 XHCITRB
*trb
= &xfer
->trbs
[i
];
1096 unsigned int chunk
= 0;
1098 switch (TRB_TYPE(*trb
)) {
1100 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1101 fprintf(stderr
, "xhci: data direction mismatch for TR_DATA\n");
1108 addr
= xhci_mask64(trb
->parameter
);
1109 chunk
= trb
->status
& 0x1ffff;
1114 if (in_xfer
|| out_xfer
) {
1115 if (trb
->control
& TRB_TR_IDT
) {
1117 if (chunk
> 8 || in_xfer
) {
1118 fprintf(stderr
, "xhci: invalid immediate data TRB\n");
1122 idata
= le64_to_cpu(trb
->parameter
);
1123 memcpy(data
, &idata
, chunk
);
1125 DPRINTF("xhci_xfer_data: r/w(%d) %d bytes at "
1126 DMA_ADDR_FMT
"\n", in_xfer
, chunk
, addr
);
1128 pci_dma_write(&xhci
->pci_dev
, addr
, data
, chunk
);
1130 pci_dma_read(&xhci
->pci_dev
, addr
, data
, chunk
);
1133 unsigned int count
= chunk
;
1139 for (i
= 0; i
< count
; i
++) {
1140 DPRINTF(" %02x", data
[i
]);
1149 transferred
+= chunk
;
1157 if (report
&& !reported
&& (trb
->control
& TRB_TR_IOC
||
1158 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)))) {
1159 event
.slotid
= xfer
->slotid
;
1160 event
.epid
= xfer
->epid
;
1161 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1163 event
.ptr
= trb
->addr
;
1164 if (xfer
->status
== CC_SUCCESS
) {
1165 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1167 event
.ccode
= xfer
->status
;
1169 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1170 event
.ptr
= trb
->parameter
;
1171 event
.flags
|= TRB_EV_ED
;
1172 event
.length
= edtla
& 0xffffff;
1173 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1176 xhci_event(xhci
, &event
);
1183 static void xhci_stall_ep(XHCITransfer
*xfer
)
1185 XHCIState
*xhci
= xfer
->xhci
;
1186 XHCISlot
*slot
= &xhci
->slots
[xfer
->slotid
-1];
1187 XHCIEPContext
*epctx
= slot
->eps
[xfer
->epid
-1];
1189 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1190 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1191 xhci_set_ep_state(xhci
, epctx
, EP_HALTED
);
1192 DPRINTF("xhci: stalled slot %d ep %d\n", xfer
->slotid
, xfer
->epid
);
1193 DPRINTF("xhci: will continue at "DMA_ADDR_FMT
"\n", epctx
->ring
.dequeue
);
1196 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
,
1197 XHCIEPContext
*epctx
);
1199 static int xhci_setup_packet(XHCITransfer
*xfer
, USBDevice
*dev
)
1204 dir
= xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
;
1205 ep
= usb_ep_get(dev
, dir
, xfer
->epid
>> 1);
1206 usb_packet_setup(&xfer
->packet
, dir
, ep
, xfer
->trbs
[0].addr
);
1207 usb_packet_addbuf(&xfer
->packet
, xfer
->data
, xfer
->data_length
);
1208 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1209 xfer
->packet
.pid
, dev
->addr
, ep
->nr
);
1213 static int xhci_complete_packet(XHCITransfer
*xfer
, int ret
)
1215 if (ret
== USB_RET_ASYNC
) {
1216 trace_usb_xhci_xfer_async(xfer
);
1217 xfer
->running_async
= 1;
1218 xfer
->running_retry
= 0;
1220 xfer
->cancelled
= 0;
1222 } else if (ret
== USB_RET_NAK
) {
1223 trace_usb_xhci_xfer_nak(xfer
);
1224 xfer
->running_async
= 0;
1225 xfer
->running_retry
= 1;
1227 xfer
->cancelled
= 0;
1230 xfer
->running_async
= 0;
1231 xfer
->running_retry
= 0;
1236 xfer
->status
= CC_SUCCESS
;
1237 xhci_xfer_data(xfer
, xfer
->data
, ret
, xfer
->in_xfer
, 0, 1);
1238 trace_usb_xhci_xfer_success(xfer
, ret
);
1243 trace_usb_xhci_xfer_error(xfer
, ret
);
1246 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1247 xhci_xfer_data(xfer
, xfer
->data
, 0, xfer
->in_xfer
, 0, 1);
1248 xhci_stall_ep(xfer
);
1251 xfer
->status
= CC_STALL_ERROR
;
1252 xhci_xfer_data(xfer
, xfer
->data
, 0, xfer
->in_xfer
, 0, 1);
1253 xhci_stall_ep(xfer
);
1256 fprintf(stderr
, "%s: FIXME: ret = %d\n", __FUNCTION__
, ret
);
1262 static USBDevice
*xhci_find_device(XHCIPort
*port
, uint8_t addr
)
1264 if (!(port
->portsc
& PORTSC_PED
)) {
1267 return usb_find_device(&port
->port
, addr
);
1270 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1272 XHCITRB
*trb_setup
, *trb_status
;
1273 uint8_t bmRequestType
;
1279 trb_setup
= &xfer
->trbs
[0];
1280 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1282 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
,
1283 trb_setup
->parameter
>> 48);
1285 /* at most one Event Data TRB allowed after STATUS */
1286 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1290 /* do some sanity checks */
1291 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1292 fprintf(stderr
, "xhci: ep0 first TD not SETUP: %d\n",
1293 TRB_TYPE(*trb_setup
));
1296 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1297 fprintf(stderr
, "xhci: ep0 last TD not STATUS: %d\n",
1298 TRB_TYPE(*trb_status
));
1301 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1302 fprintf(stderr
, "xhci: Setup TRB doesn't have IDT set\n");
1305 if ((trb_setup
->status
& 0x1ffff) != 8) {
1306 fprintf(stderr
, "xhci: Setup TRB has bad length (%d)\n",
1307 (trb_setup
->status
& 0x1ffff));
1311 bmRequestType
= trb_setup
->parameter
;
1312 wLength
= trb_setup
->parameter
>> 48;
1314 if (xfer
->data
&& xfer
->data_alloced
< wLength
) {
1315 xfer
->data_alloced
= 0;
1320 DPRINTF("xhci: alloc %d bytes data\n", wLength
);
1321 xfer
->data
= g_malloc(wLength
+1);
1322 xfer
->data_alloced
= wLength
;
1324 xfer
->data_length
= wLength
;
1326 port
= &xhci
->ports
[xhci
->slots
[xfer
->slotid
-1].port
-1];
1327 dev
= xhci_find_device(port
, xhci
->slots
[xfer
->slotid
-1].devaddr
);
1329 fprintf(stderr
, "xhci: slot %d port %d has no device\n", xfer
->slotid
,
1330 xhci
->slots
[xfer
->slotid
-1].port
);
1334 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1335 xfer
->iso_xfer
= false;
1337 xhci_setup_packet(xfer
, dev
);
1338 xfer
->packet
.parameter
= trb_setup
->parameter
;
1339 if (!xfer
->in_xfer
) {
1340 xhci_xfer_data(xfer
, xfer
->data
, wLength
, 0, 1, 0);
1343 ret
= usb_handle_packet(dev
, &xfer
->packet
);
1345 xhci_complete_packet(xfer
, ret
);
1346 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1347 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1352 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1358 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer
->slotid
, xfer
->epid
);
1360 xfer
->in_xfer
= epctx
->type
>>2;
1362 if (xfer
->data
&& xfer
->data_alloced
< xfer
->data_length
) {
1363 xfer
->data_alloced
= 0;
1367 if (!xfer
->data
&& xfer
->data_length
) {
1368 DPRINTF("xhci: alloc %d bytes data\n", xfer
->data_length
);
1369 xfer
->data
= g_malloc(xfer
->data_length
);
1370 xfer
->data_alloced
= xfer
->data_length
;
1372 if (epctx
->type
== ET_ISO_IN
|| epctx
->type
== ET_ISO_OUT
) {
1378 port
= &xhci
->ports
[xhci
->slots
[xfer
->slotid
-1].port
-1];
1379 dev
= xhci_find_device(port
, xhci
->slots
[xfer
->slotid
-1].devaddr
);
1381 fprintf(stderr
, "xhci: slot %d port %d has no device\n", xfer
->slotid
,
1382 xhci
->slots
[xfer
->slotid
-1].port
);
1386 xhci_setup_packet(xfer
, dev
);
1388 switch(epctx
->type
) {
1399 fprintf(stderr
, "xhci: unknown or unhandled EP "
1400 "(type %d, in %d, ep %02x)\n",
1401 epctx
->type
, xfer
->in_xfer
, xfer
->epid
);
1405 if (!xfer
->in_xfer
) {
1406 xhci_xfer_data(xfer
, xfer
->data
, xfer
->data_length
, 0, 1, 0);
1408 ret
= usb_handle_packet(dev
, &xfer
->packet
);
1410 xhci_complete_packet(xfer
, ret
);
1411 if (!xfer
->running_async
&& !xfer
->running_retry
) {
1412 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1417 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1420 unsigned int length
= 0;
1423 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1424 trb
= &xfer
->trbs
[i
];
1425 if (TRB_TYPE(*trb
) == TR_NORMAL
|| TRB_TYPE(*trb
) == TR_ISOCH
) {
1426 length
+= trb
->status
& 0x1ffff;
1430 trace_usb_xhci_xfer_start(xfer
, xfer
->slotid
, xfer
->epid
, length
);
1432 xfer
->data_length
= length
;
1433 return xhci_submit(xhci
, xfer
, epctx
);
1436 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
, unsigned int epid
)
1438 XHCIEPContext
*epctx
;
1442 trace_usb_xhci_ep_kick(slotid
, epid
);
1443 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1444 assert(epid
>= 1 && epid
<= 31);
1446 if (!xhci
->slots
[slotid
-1].enabled
) {
1447 fprintf(stderr
, "xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1450 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1452 fprintf(stderr
, "xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1458 /* retry nak'ed transfer */
1459 XHCITransfer
*xfer
= epctx
->retry
;
1462 trace_usb_xhci_xfer_retry(xfer
);
1463 assert(xfer
->running_retry
);
1464 xhci_setup_packet(xfer
, xfer
->packet
.ep
->dev
);
1465 result
= usb_handle_packet(xfer
->packet
.ep
->dev
, &xfer
->packet
);
1466 if (result
== USB_RET_NAK
) {
1469 xhci_complete_packet(xfer
, result
);
1470 assert(!xfer
->running_retry
);
1471 epctx
->retry
= NULL
;
1474 if (epctx
->state
== EP_HALTED
) {
1475 DPRINTF("xhci: ep halted, not running schedule\n");
1479 xhci_set_ep_state(xhci
, epctx
, EP_RUNNING
);
1482 XHCITransfer
*xfer
= &epctx
->transfers
[epctx
->next_xfer
];
1483 if (xfer
->running_async
|| xfer
->running_retry
) {
1486 length
= xhci_ring_chain_length(xhci
, &epctx
->ring
);
1489 } else if (length
== 0) {
1492 if (xfer
->trbs
&& xfer
->trb_alloced
< length
) {
1493 xfer
->trb_count
= 0;
1494 xfer
->trb_alloced
= 0;
1499 xfer
->trbs
= g_malloc(sizeof(XHCITRB
) * length
);
1500 xfer
->trb_alloced
= length
;
1502 xfer
->trb_count
= length
;
1504 for (i
= 0; i
< length
; i
++) {
1505 assert(xhci_ring_fetch(xhci
, &epctx
->ring
, &xfer
->trbs
[i
], NULL
));
1509 xfer
->slotid
= slotid
;
1512 if (xhci_fire_ctl_transfer(xhci
, xfer
) >= 0) {
1513 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
1515 fprintf(stderr
, "xhci: error firing CTL transfer\n");
1518 if (xhci_fire_transfer(xhci
, xfer
, epctx
) >= 0) {
1519 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
1521 fprintf(stderr
, "xhci: error firing data transfer\n");
1525 if (epctx
->state
== EP_HALTED
) {
1528 if (xfer
->running_retry
) {
1529 DPRINTF("xhci: xfer nacked, stopping schedule\n");
1530 epctx
->retry
= xfer
;
1536 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
1538 trace_usb_xhci_slot_enable(slotid
);
1539 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1540 xhci
->slots
[slotid
-1].enabled
= 1;
1541 xhci
->slots
[slotid
-1].port
= 0;
1542 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
1547 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
1551 trace_usb_xhci_slot_disable(slotid
);
1552 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1554 for (i
= 1; i
<= 31; i
++) {
1555 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1556 xhci_disable_ep(xhci
, slotid
, i
);
1560 xhci
->slots
[slotid
-1].enabled
= 0;
1564 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
1565 uint64_t pictx
, bool bsr
)
1569 dma_addr_t ictx
, octx
, dcbaap
;
1571 uint32_t ictl_ctx
[2];
1572 uint32_t slot_ctx
[4];
1573 uint32_t ep0_ctx
[5];
1578 trace_usb_xhci_slot_address(slotid
);
1579 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1581 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
1582 pci_dma_read(&xhci
->pci_dev
, dcbaap
+ 8*slotid
, &poctx
, sizeof(poctx
));
1583 ictx
= xhci_mask64(pictx
);
1584 octx
= xhci_mask64(le64_to_cpu(poctx
));
1586 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
1587 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1589 pci_dma_read(&xhci
->pci_dev
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
1591 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
1592 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1593 ictl_ctx
[0], ictl_ctx
[1]);
1594 return CC_TRB_ERROR
;
1597 pci_dma_read(&xhci
->pci_dev
, ictx
+32, slot_ctx
, sizeof(slot_ctx
));
1598 pci_dma_read(&xhci
->pci_dev
, ictx
+64, ep0_ctx
, sizeof(ep0_ctx
));
1600 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
1601 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1603 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
1604 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1606 port
= (slot_ctx
[1]>>16) & 0xFF;
1607 dev
= xhci
->ports
[port
-1].port
.dev
;
1609 if (port
< 1 || port
> MAXPORTS
) {
1610 fprintf(stderr
, "xhci: bad port %d\n", port
);
1611 return CC_TRB_ERROR
;
1613 fprintf(stderr
, "xhci: port %d not connected\n", port
);
1614 return CC_USB_TRANSACTION_ERROR
;
1617 for (i
= 0; i
< MAXSLOTS
; i
++) {
1618 if (xhci
->slots
[i
].port
== port
) {
1619 fprintf(stderr
, "xhci: port %d already assigned to slot %d\n",
1621 return CC_TRB_ERROR
;
1625 slot
= &xhci
->slots
[slotid
-1];
1630 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
1632 slot
->devaddr
= xhci
->devaddr
++;
1633 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slot
->devaddr
;
1634 DPRINTF("xhci: device address is %d\n", slot
->devaddr
);
1635 usb_device_handle_control(dev
, NULL
,
1636 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
1637 slot
->devaddr
, 0, 0, NULL
);
1640 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
1642 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1643 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1644 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
1645 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1647 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1648 pci_dma_write(&xhci
->pci_dev
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
1654 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
1655 uint64_t pictx
, bool dc
)
1657 dma_addr_t ictx
, octx
;
1658 uint32_t ictl_ctx
[2];
1659 uint32_t slot_ctx
[4];
1660 uint32_t islot_ctx
[4];
1665 trace_usb_xhci_slot_configure(slotid
);
1666 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1668 ictx
= xhci_mask64(pictx
);
1669 octx
= xhci
->slots
[slotid
-1].ctx
;
1671 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
1672 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1675 for (i
= 2; i
<= 31; i
++) {
1676 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1677 xhci_disable_ep(xhci
, slotid
, i
);
1681 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1682 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1683 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
1684 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1685 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1686 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1691 pci_dma_read(&xhci
->pci_dev
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
1693 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
1694 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1695 ictl_ctx
[0], ictl_ctx
[1]);
1696 return CC_TRB_ERROR
;
1699 pci_dma_read(&xhci
->pci_dev
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
1700 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1702 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
1703 fprintf(stderr
, "xhci: invalid slot state %08x\n", slot_ctx
[3]);
1704 return CC_CONTEXT_STATE_ERROR
;
1707 for (i
= 2; i
<= 31; i
++) {
1708 if (ictl_ctx
[0] & (1<<i
)) {
1709 xhci_disable_ep(xhci
, slotid
, i
);
1711 if (ictl_ctx
[1] & (1<<i
)) {
1712 pci_dma_read(&xhci
->pci_dev
, ictx
+32+(32*i
), ep_ctx
,
1714 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
1715 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
1716 ep_ctx
[3], ep_ctx
[4]);
1717 xhci_disable_ep(xhci
, slotid
, i
);
1718 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
1719 if (res
!= CC_SUCCESS
) {
1722 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
1723 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
1724 ep_ctx
[3], ep_ctx
[4]);
1725 pci_dma_write(&xhci
->pci_dev
, octx
+(32*i
), ep_ctx
, sizeof(ep_ctx
));
1729 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1730 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
1731 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
1732 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
1733 SLOT_CONTEXT_ENTRIES_SHIFT
);
1734 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1735 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1737 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1743 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
1746 dma_addr_t ictx
, octx
;
1747 uint32_t ictl_ctx
[2];
1748 uint32_t iep0_ctx
[5];
1749 uint32_t ep0_ctx
[5];
1750 uint32_t islot_ctx
[4];
1751 uint32_t slot_ctx
[4];
1753 trace_usb_xhci_slot_evaluate(slotid
);
1754 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1756 ictx
= xhci_mask64(pictx
);
1757 octx
= xhci
->slots
[slotid
-1].ctx
;
1759 DPRINTF("xhci: input context at "DMA_ADDR_FMT
"\n", ictx
);
1760 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1762 pci_dma_read(&xhci
->pci_dev
, ictx
, ictl_ctx
, sizeof(ictl_ctx
));
1764 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
1765 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1766 ictl_ctx
[0], ictl_ctx
[1]);
1767 return CC_TRB_ERROR
;
1770 if (ictl_ctx
[1] & 0x1) {
1771 pci_dma_read(&xhci
->pci_dev
, ictx
+32, islot_ctx
, sizeof(islot_ctx
));
1773 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
1774 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
1776 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1778 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
1779 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
1780 slot_ctx
[2] &= ~0xFF00000; /* interrupter target */
1781 slot_ctx
[2] |= islot_ctx
[2] & 0xFF000000;
1783 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1784 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1786 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1789 if (ictl_ctx
[1] & 0x2) {
1790 pci_dma_read(&xhci
->pci_dev
, ictx
+64, iep0_ctx
, sizeof(iep0_ctx
));
1792 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
1793 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
1794 iep0_ctx
[3], iep0_ctx
[4]);
1796 pci_dma_read(&xhci
->pci_dev
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
1798 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
1799 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
1801 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
1802 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1804 pci_dma_write(&xhci
->pci_dev
, octx
+32, ep0_ctx
, sizeof(ep0_ctx
));
1810 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
1812 uint32_t slot_ctx
[4];
1816 trace_usb_xhci_slot_reset(slotid
);
1817 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1819 octx
= xhci
->slots
[slotid
-1].ctx
;
1821 DPRINTF("xhci: output context at "DMA_ADDR_FMT
"\n", octx
);
1823 for (i
= 2; i
<= 31; i
++) {
1824 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1825 xhci_disable_ep(xhci
, slotid
, i
);
1829 pci_dma_read(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1830 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1831 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
1832 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1833 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1834 pci_dma_write(&xhci
->pci_dev
, octx
, slot_ctx
, sizeof(slot_ctx
));
1839 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
1841 unsigned int slotid
;
1842 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
1843 if (slotid
< 1 || slotid
> MAXSLOTS
) {
1844 fprintf(stderr
, "xhci: bad slot id %d\n", slotid
);
1845 event
->ccode
= CC_TRB_ERROR
;
1847 } else if (!xhci
->slots
[slotid
-1].enabled
) {
1848 fprintf(stderr
, "xhci: slot id %d not enabled\n", slotid
);
1849 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
1855 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
1858 uint8_t bw_ctx
[MAXPORTS
+1];
1860 DPRINTF("xhci_get_port_bandwidth()\n");
1862 ctx
= xhci_mask64(pctx
);
1864 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT
"\n", ctx
);
1866 /* TODO: actually implement real values here */
1868 memset(&bw_ctx
[1], 80, MAXPORTS
); /* 80% */
1869 pci_dma_write(&xhci
->pci_dev
, ctx
, bw_ctx
, sizeof(bw_ctx
));
1874 static uint32_t rotl(uint32_t v
, unsigned count
)
1877 return (v
<< count
) | (v
>> (32 - count
));
1881 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
1884 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
1885 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
1886 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
1890 static void xhci_via_challenge(XHCIState
*xhci
, uint64_t addr
)
1894 dma_addr_t paddr
= xhci_mask64(addr
);
1896 pci_dma_read(&xhci
->pci_dev
, paddr
, &buf
, 32);
1898 memcpy(obuf
, buf
, sizeof(obuf
));
1900 if ((buf
[0] & 0xff) == 2) {
1901 obuf
[0] = 0x49932000 + 0x54dc200 * buf
[2] + 0x7429b578 * buf
[3];
1902 obuf
[0] |= (buf
[2] * buf
[3]) & 0xff;
1903 obuf
[1] = 0x0132bb37 + 0xe89 * buf
[2] + 0xf09 * buf
[3];
1904 obuf
[2] = 0x0066c2e9 + 0x2091 * buf
[2] + 0x19bd * buf
[3];
1905 obuf
[3] = 0xd5281342 + 0x2cc9691 * buf
[2] + 0x2367662 * buf
[3];
1906 obuf
[4] = 0x0123c75c + 0x1595 * buf
[2] + 0x19ec * buf
[3];
1907 obuf
[5] = 0x00f695de + 0x26fd * buf
[2] + 0x3e9 * buf
[3];
1908 obuf
[6] = obuf
[2] ^ obuf
[3] ^ 0x29472956;
1909 obuf
[7] = obuf
[2] ^ obuf
[3] ^ 0x65866593;
1912 pci_dma_write(&xhci
->pci_dev
, paddr
, &obuf
, 32);
1915 static void xhci_process_commands(XHCIState
*xhci
)
1919 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
1921 unsigned int i
, slotid
= 0;
1923 DPRINTF("xhci_process_commands()\n");
1924 if (!xhci_running(xhci
)) {
1925 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
1929 xhci
->crcr_low
|= CRCR_CRR
;
1931 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
1934 case CR_ENABLE_SLOT
:
1935 for (i
= 0; i
< MAXSLOTS
; i
++) {
1936 if (!xhci
->slots
[i
].enabled
) {
1940 if (i
>= MAXSLOTS
) {
1941 fprintf(stderr
, "xhci: no device slots available\n");
1942 event
.ccode
= CC_NO_SLOTS_ERROR
;
1945 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
1948 case CR_DISABLE_SLOT
:
1949 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1951 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
1954 case CR_ADDRESS_DEVICE
:
1955 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1957 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
1958 trb
.control
& TRB_CR_BSR
);
1961 case CR_CONFIGURE_ENDPOINT
:
1962 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1964 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
1965 trb
.control
& TRB_CR_DC
);
1968 case CR_EVALUATE_CONTEXT
:
1969 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1971 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
1974 case CR_STOP_ENDPOINT
:
1975 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1977 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
1979 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
1982 case CR_RESET_ENDPOINT
:
1983 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1985 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
1987 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
1990 case CR_SET_TR_DEQUEUE
:
1991 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
1993 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
1995 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
, epid
,
1999 case CR_RESET_DEVICE
:
2000 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2002 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2005 case CR_GET_PORT_BANDWIDTH
:
2006 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2008 case CR_VENDOR_VIA_CHALLENGE_RESPONSE
:
2009 xhci_via_challenge(xhci
, trb
.parameter
);
2011 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2012 event
.type
= 48; /* NEC reply */
2013 event
.length
= 0x3025;
2015 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2017 uint32_t chi
= trb
.parameter
>> 32;
2018 uint32_t clo
= trb
.parameter
;
2019 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2020 event
.length
= val
& 0xFFFF;
2021 event
.epid
= val
>> 16;
2023 event
.type
= 48; /* NEC reply */
2027 fprintf(stderr
, "xhci: unimplemented command %d\n", type
);
2028 event
.ccode
= CC_TRB_ERROR
;
2031 event
.slotid
= slotid
;
2032 xhci_event(xhci
, &event
);
2036 static void xhci_update_port(XHCIState
*xhci
, XHCIPort
*port
, int is_detach
)
2038 int nr
= port
->port
.index
+ 1;
2040 port
->portsc
= PORTSC_PP
;
2041 if (port
->port
.dev
&& port
->port
.dev
->attached
&& !is_detach
) {
2042 port
->portsc
|= PORTSC_CCS
;
2043 switch (port
->port
.dev
->speed
) {
2045 port
->portsc
|= PORTSC_SPEED_LOW
;
2047 case USB_SPEED_FULL
:
2048 port
->portsc
|= PORTSC_SPEED_FULL
;
2050 case USB_SPEED_HIGH
:
2051 port
->portsc
|= PORTSC_SPEED_HIGH
;
2056 if (xhci_running(xhci
)) {
2057 port
->portsc
|= PORTSC_CSC
;
2058 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
, nr
<< 24};
2059 xhci_event(xhci
, &ev
);
2060 DPRINTF("xhci: port change event for port %d\n", nr
);
2064 static void xhci_reset(DeviceState
*dev
)
2066 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
.qdev
, dev
);
2069 trace_usb_xhci_reset();
2070 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2071 fprintf(stderr
, "xhci: reset while running!\n");
2075 xhci
->usbsts
= USBSTS_HCH
;
2078 xhci
->crcr_high
= 0;
2079 xhci
->dcbaap_low
= 0;
2080 xhci
->dcbaap_high
= 0;
2084 for (i
= 0; i
< MAXSLOTS
; i
++) {
2085 xhci_disable_slot(xhci
, i
+1);
2088 for (i
= 0; i
< MAXPORTS
; i
++) {
2089 xhci_update_port(xhci
, xhci
->ports
+ i
, 0);
2096 xhci
->erstba_low
= 0;
2097 xhci
->erstba_high
= 0;
2099 xhci
->erdp_high
= 0;
2101 xhci
->er_ep_idx
= 0;
2104 xhci
->ev_buffer_put
= 0;
2105 xhci
->ev_buffer_get
= 0;
2108 static uint32_t xhci_cap_read(XHCIState
*xhci
, uint32_t reg
)
2113 case 0x00: /* HCIVERSION, CAPLENGTH */
2114 ret
= 0x01000000 | LEN_CAP
;
2116 case 0x04: /* HCSPARAMS 1 */
2117 ret
= (MAXPORTS
<<24) | (MAXINTRS
<<8) | MAXSLOTS
;
2119 case 0x08: /* HCSPARAMS 2 */
2122 case 0x0c: /* HCSPARAMS 3 */
2125 case 0x10: /* HCCPARAMS */
2126 if (sizeof(dma_addr_t
) == 4) {
2132 case 0x14: /* DBOFF */
2135 case 0x18: /* RTSOFF */
2139 /* extended capabilities */
2140 case 0x20: /* Supported Protocol:00 */
2141 ret
= 0x02000402; /* USB 2.0 */
2143 case 0x24: /* Supported Protocol:04 */
2144 ret
= 0x20425455; /* "USB " */
2146 case 0x28: /* Supported Protocol:08 */
2147 ret
= 0x00000001 | (USB2_PORTS
<<8);
2149 case 0x2c: /* Supported Protocol:0c */
2150 ret
= 0x00000000; /* reserved */
2152 case 0x30: /* Supported Protocol:00 */
2153 ret
= 0x03000002; /* USB 3.0 */
2155 case 0x34: /* Supported Protocol:04 */
2156 ret
= 0x20425455; /* "USB " */
2158 case 0x38: /* Supported Protocol:08 */
2159 ret
= 0x00000000 | (USB2_PORTS
+1) | (USB3_PORTS
<<8);
2161 case 0x3c: /* Supported Protocol:0c */
2162 ret
= 0x00000000; /* reserved */
2165 fprintf(stderr
, "xhci_cap_read: reg %d unimplemented\n", reg
);
2169 trace_usb_xhci_cap_read(reg
, ret
);
2173 static uint32_t xhci_port_read(XHCIState
*xhci
, uint32_t reg
)
2175 uint32_t port
= reg
>> 4;
2178 if (port
>= MAXPORTS
) {
2179 fprintf(stderr
, "xhci_port_read: port %d out of bounds\n", port
);
2184 switch (reg
& 0xf) {
2185 case 0x00: /* PORTSC */
2186 ret
= xhci
->ports
[port
].portsc
;
2188 case 0x04: /* PORTPMSC */
2189 case 0x08: /* PORTLI */
2192 case 0x0c: /* reserved */
2194 fprintf(stderr
, "xhci_port_read (port %d): reg 0x%x unimplemented\n",
2200 trace_usb_xhci_port_read(port
, reg
& 0x0f, ret
);
2204 static void xhci_port_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2206 uint32_t port
= reg
>> 4;
2209 trace_usb_xhci_port_write(port
, reg
& 0x0f, val
);
2211 if (port
>= MAXPORTS
) {
2212 fprintf(stderr
, "xhci_port_read: port %d out of bounds\n", port
);
2216 switch (reg
& 0xf) {
2217 case 0x00: /* PORTSC */
2218 portsc
= xhci
->ports
[port
].portsc
;
2219 /* write-1-to-clear bits*/
2220 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2221 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2222 if (val
& PORTSC_LWS
) {
2223 /* overwrite PLS only when LWS=1 */
2224 portsc
&= ~(PORTSC_PLS_MASK
<< PORTSC_PLS_SHIFT
);
2225 portsc
|= val
& (PORTSC_PLS_MASK
<< PORTSC_PLS_SHIFT
);
2227 /* read/write bits */
2228 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2229 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2230 /* write-1-to-start bits */
2231 if (val
& PORTSC_PR
) {
2232 DPRINTF("xhci: port %d reset\n", port
);
2233 usb_device_reset(xhci
->ports
[port
].port
.dev
);
2234 portsc
|= PORTSC_PRC
| PORTSC_PED
;
2236 xhci
->ports
[port
].portsc
= portsc
;
2238 case 0x04: /* PORTPMSC */
2239 case 0x08: /* PORTLI */
2241 fprintf(stderr
, "xhci_port_write (port %d): reg 0x%x unimplemented\n",
2246 static uint32_t xhci_oper_read(XHCIState
*xhci
, uint32_t reg
)
2251 return xhci_port_read(xhci
, reg
- 0x400);
2255 case 0x00: /* USBCMD */
2258 case 0x04: /* USBSTS */
2261 case 0x08: /* PAGESIZE */
2264 case 0x14: /* DNCTRL */
2267 case 0x18: /* CRCR low */
2268 ret
= xhci
->crcr_low
& ~0xe;
2270 case 0x1c: /* CRCR high */
2271 ret
= xhci
->crcr_high
;
2273 case 0x30: /* DCBAAP low */
2274 ret
= xhci
->dcbaap_low
;
2276 case 0x34: /* DCBAAP high */
2277 ret
= xhci
->dcbaap_high
;
2279 case 0x38: /* CONFIG */
2283 fprintf(stderr
, "xhci_oper_read: reg 0x%x unimplemented\n", reg
);
2287 trace_usb_xhci_oper_read(reg
, ret
);
2291 static void xhci_oper_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2294 xhci_port_write(xhci
, reg
- 0x400, val
);
2298 trace_usb_xhci_oper_write(reg
, val
);
2301 case 0x00: /* USBCMD */
2302 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
2304 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
2307 xhci
->usbcmd
= val
& 0xc0f;
2308 if (val
& USBCMD_HCRST
) {
2309 xhci_reset(&xhci
->pci_dev
.qdev
);
2311 xhci_irq_update(xhci
);
2314 case 0x04: /* USBSTS */
2315 /* these bits are write-1-to-clear */
2316 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
2317 xhci_irq_update(xhci
);
2320 case 0x14: /* DNCTRL */
2321 xhci
->dnctrl
= val
& 0xffff;
2323 case 0x18: /* CRCR low */
2324 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
2326 case 0x1c: /* CRCR high */
2327 xhci
->crcr_high
= val
;
2328 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
2329 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
2330 xhci
->crcr_low
&= ~CRCR_CRR
;
2331 xhci_event(xhci
, &event
);
2332 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
2334 dma_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
2335 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
2337 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
2339 case 0x30: /* DCBAAP low */
2340 xhci
->dcbaap_low
= val
& 0xffffffc0;
2342 case 0x34: /* DCBAAP high */
2343 xhci
->dcbaap_high
= val
;
2345 case 0x38: /* CONFIG */
2346 xhci
->config
= val
& 0xff;
2349 fprintf(stderr
, "xhci_oper_write: reg 0x%x unimplemented\n", reg
);
2353 static uint32_t xhci_runtime_read(XHCIState
*xhci
, uint32_t reg
)
2358 case 0x00: /* MFINDEX */
2359 fprintf(stderr
, "xhci_runtime_read: MFINDEX not yet implemented\n");
2360 ret
= xhci
->mfindex
;
2362 case 0x20: /* IMAN */
2365 case 0x24: /* IMOD */
2368 case 0x28: /* ERSTSZ */
2371 case 0x30: /* ERSTBA low */
2372 ret
= xhci
->erstba_low
;
2374 case 0x34: /* ERSTBA high */
2375 ret
= xhci
->erstba_high
;
2377 case 0x38: /* ERDP low */
2378 ret
= xhci
->erdp_low
;
2380 case 0x3c: /* ERDP high */
2381 ret
= xhci
->erdp_high
;
2384 fprintf(stderr
, "xhci_runtime_read: reg 0x%x unimplemented\n", reg
);
2388 trace_usb_xhci_runtime_read(reg
, ret
);
2392 static void xhci_runtime_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2394 trace_usb_xhci_runtime_read(reg
, val
);
2397 case 0x20: /* IMAN */
2398 if (val
& IMAN_IP
) {
2399 xhci
->iman
&= ~IMAN_IP
;
2401 xhci
->iman
&= ~IMAN_IE
;
2402 xhci
->iman
|= val
& IMAN_IE
;
2403 xhci_irq_update(xhci
);
2405 case 0x24: /* IMOD */
2408 case 0x28: /* ERSTSZ */
2409 xhci
->erstsz
= val
& 0xffff;
2411 case 0x30: /* ERSTBA low */
2412 /* XXX NEC driver bug: it doesn't align this to 64 bytes
2413 xhci->erstba_low = val & 0xffffffc0; */
2414 xhci
->erstba_low
= val
& 0xfffffff0;
2416 case 0x34: /* ERSTBA high */
2417 xhci
->erstba_high
= val
;
2418 xhci_er_reset(xhci
);
2420 case 0x38: /* ERDP low */
2421 if (val
& ERDP_EHB
) {
2422 xhci
->erdp_low
&= ~ERDP_EHB
;
2424 xhci
->erdp_low
= (val
& ~ERDP_EHB
) | (xhci
->erdp_low
& ERDP_EHB
);
2426 case 0x3c: /* ERDP high */
2427 xhci
->erdp_high
= val
;
2428 xhci_events_update(xhci
);
2431 fprintf(stderr
, "xhci_oper_write: reg 0x%x unimplemented\n", reg
);
2435 static uint32_t xhci_doorbell_read(XHCIState
*xhci
, uint32_t reg
)
2437 /* doorbells always read as 0 */
2438 trace_usb_xhci_doorbell_read(reg
, 0);
2442 static void xhci_doorbell_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2444 trace_usb_xhci_doorbell_write(reg
, val
);
2446 if (!xhci_running(xhci
)) {
2447 fprintf(stderr
, "xhci: wrote doorbell while xHC stopped or paused\n");
2455 xhci_process_commands(xhci
);
2457 fprintf(stderr
, "xhci: bad doorbell 0 write: 0x%x\n", val
);
2460 if (reg
> MAXSLOTS
) {
2461 fprintf(stderr
, "xhci: bad doorbell %d\n", reg
);
2462 } else if (val
> 31) {
2463 fprintf(stderr
, "xhci: bad doorbell %d write: 0x%x\n", reg
, val
);
2465 xhci_kick_ep(xhci
, reg
, val
);
2470 static uint64_t xhci_mem_read(void *ptr
, target_phys_addr_t addr
,
2473 XHCIState
*xhci
= ptr
;
2475 /* Only aligned reads are allowed on xHCI */
2477 fprintf(stderr
, "xhci_mem_read: Mis-aligned read\n");
2481 if (addr
< LEN_CAP
) {
2482 return xhci_cap_read(xhci
, addr
);
2483 } else if (addr
>= OFF_OPER
&& addr
< (OFF_OPER
+ LEN_OPER
)) {
2484 return xhci_oper_read(xhci
, addr
- OFF_OPER
);
2485 } else if (addr
>= OFF_RUNTIME
&& addr
< (OFF_RUNTIME
+ LEN_RUNTIME
)) {
2486 return xhci_runtime_read(xhci
, addr
- OFF_RUNTIME
);
2487 } else if (addr
>= OFF_DOORBELL
&& addr
< (OFF_DOORBELL
+ LEN_DOORBELL
)) {
2488 return xhci_doorbell_read(xhci
, addr
- OFF_DOORBELL
);
2490 fprintf(stderr
, "xhci_mem_read: Bad offset %x\n", (int)addr
);
2495 static void xhci_mem_write(void *ptr
, target_phys_addr_t addr
,
2496 uint64_t val
, unsigned size
)
2498 XHCIState
*xhci
= ptr
;
2500 /* Only aligned writes are allowed on xHCI */
2502 fprintf(stderr
, "xhci_mem_write: Mis-aligned write\n");
2506 if (addr
>= OFF_OPER
&& addr
< (OFF_OPER
+ LEN_OPER
)) {
2507 xhci_oper_write(xhci
, addr
- OFF_OPER
, val
);
2508 } else if (addr
>= OFF_RUNTIME
&& addr
< (OFF_RUNTIME
+ LEN_RUNTIME
)) {
2509 xhci_runtime_write(xhci
, addr
- OFF_RUNTIME
, val
);
2510 } else if (addr
>= OFF_DOORBELL
&& addr
< (OFF_DOORBELL
+ LEN_DOORBELL
)) {
2511 xhci_doorbell_write(xhci
, addr
- OFF_DOORBELL
, val
);
2513 fprintf(stderr
, "xhci_mem_write: Bad offset %x\n", (int)addr
);
2517 static const MemoryRegionOps xhci_mem_ops
= {
2518 .read
= xhci_mem_read
,
2519 .write
= xhci_mem_write
,
2520 .valid
.min_access_size
= 4,
2521 .valid
.max_access_size
= 4,
2522 .endianness
= DEVICE_LITTLE_ENDIAN
,
2525 static void xhci_attach(USBPort
*usbport
)
2527 XHCIState
*xhci
= usbport
->opaque
;
2528 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2530 xhci_update_port(xhci
, port
, 0);
2533 static void xhci_detach(USBPort
*usbport
)
2535 XHCIState
*xhci
= usbport
->opaque
;
2536 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2538 xhci_update_port(xhci
, port
, 1);
2541 static void xhci_wakeup(USBPort
*usbport
)
2543 XHCIState
*xhci
= usbport
->opaque
;
2544 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2545 int nr
= port
->port
.index
+ 1;
2546 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
, nr
<< 24};
2549 pls
= (port
->portsc
>> PORTSC_PLS_SHIFT
) & PORTSC_PLS_MASK
;
2553 port
->portsc
|= 0xf << PORTSC_PLS_SHIFT
;
2554 if (port
->portsc
& PORTSC_PLC
) {
2557 port
->portsc
|= PORTSC_PLC
;
2558 xhci_event(xhci
, &ev
);
2561 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
2563 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
2565 xhci_complete_packet(xfer
, packet
->result
);
2566 xhci_kick_ep(xfer
->xhci
, xfer
->slotid
, xfer
->epid
);
2569 static void xhci_child_detach(USBPort
*port
, USBDevice
*child
)
2574 static USBPortOps xhci_port_ops
= {
2575 .attach
= xhci_attach
,
2576 .detach
= xhci_detach
,
2577 .wakeup
= xhci_wakeup
,
2578 .complete
= xhci_complete
,
2579 .child_detach
= xhci_child_detach
,
2582 static int xhci_find_slotid(XHCIState
*xhci
, USBDevice
*dev
)
2587 for (slotid
= 1; slotid
<= MAXSLOTS
; slotid
++) {
2588 slot
= &xhci
->slots
[slotid
-1];
2589 if (slot
->devaddr
== dev
->addr
) {
2596 static int xhci_find_epid(USBEndpoint
*ep
)
2601 if (ep
->pid
== USB_TOKEN_IN
) {
2602 return ep
->nr
* 2 + 1;
2608 static void xhci_wakeup_endpoint(USBBus
*bus
, USBEndpoint
*ep
)
2610 XHCIState
*xhci
= container_of(bus
, XHCIState
, bus
);
2613 DPRINTF("%s\n", __func__
);
2614 slotid
= xhci_find_slotid(xhci
, ep
->dev
);
2615 if (slotid
== 0 || !xhci
->slots
[slotid
-1].enabled
) {
2616 DPRINTF("%s: oops, no slot for dev %d\n", __func__
, ep
->dev
->addr
);
2619 xhci_kick_ep(xhci
, slotid
, xhci_find_epid(ep
));
2622 static USBBusOps xhci_bus_ops
= {
2623 .wakeup_endpoint
= xhci_wakeup_endpoint
,
2626 static void usb_xhci_init(XHCIState
*xhci
, DeviceState
*dev
)
2630 xhci
->usbsts
= USBSTS_HCH
;
2632 usb_bus_new(&xhci
->bus
, &xhci_bus_ops
, &xhci
->pci_dev
.qdev
);
2634 for (i
= 0; i
< MAXPORTS
; i
++) {
2635 memset(&xhci
->ports
[i
], 0, sizeof(xhci
->ports
[i
]));
2636 usb_register_port(&xhci
->bus
, &xhci
->ports
[i
].port
, xhci
, i
,
2638 USB_SPEED_MASK_LOW
|
2639 USB_SPEED_MASK_FULL
|
2640 USB_SPEED_MASK_HIGH
);
2642 for (i
= 0; i
< MAXSLOTS
; i
++) {
2643 xhci
->slots
[i
].enabled
= 0;
2647 static int usb_xhci_initfn(struct PCIDevice
*dev
)
2651 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
, dev
);
2653 xhci
->pci_dev
.config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
2654 xhci
->pci_dev
.config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
2655 xhci
->pci_dev
.config
[PCI_CACHE_LINE_SIZE
] = 0x10;
2656 xhci
->pci_dev
.config
[0x60] = 0x30; /* release number */
2658 usb_xhci_init(xhci
, &dev
->qdev
);
2660 xhci
->irq
= xhci
->pci_dev
.irq
[0];
2662 memory_region_init_io(&xhci
->mem
, &xhci_mem_ops
, xhci
,
2664 pci_register_bar(&xhci
->pci_dev
, 0,
2665 PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
,
2668 ret
= pcie_cap_init(&xhci
->pci_dev
, 0xa0, PCI_EXP_TYPE_ENDPOINT
, 0);
2672 ret
= msi_init(&xhci
->pci_dev
, 0x70, 1, true, false);
2679 static void xhci_write_config(PCIDevice
*dev
, uint32_t addr
, uint32_t val
,
2682 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
, dev
);
2684 pci_default_write_config(dev
, addr
, val
, len
);
2686 msi_write_config(dev
, addr
, val
, len
);
2690 static const VMStateDescription vmstate_xhci
= {
2695 static Property xhci_properties
[] = {
2696 DEFINE_PROP_UINT32("msi", XHCIState
, msi
, 0),
2697 DEFINE_PROP_END_OF_LIST(),
2700 static void xhci_class_init(ObjectClass
*klass
, void *data
)
2702 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2703 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2705 dc
->vmsd
= &vmstate_xhci
;
2706 dc
->props
= xhci_properties
;
2707 dc
->reset
= xhci_reset
;
2708 k
->init
= usb_xhci_initfn
;
2709 k
->vendor_id
= PCI_VENDOR_ID_NEC
;
2710 k
->device_id
= PCI_DEVICE_ID_NEC_UPD720200
;
2711 k
->class_id
= PCI_CLASS_SERIAL_USB
;
2714 k
->config_write
= xhci_write_config
;
2717 static TypeInfo xhci_info
= {
2718 .name
= "nec-usb-xhci",
2719 .parent
= TYPE_PCI_DEVICE
,
2720 .instance_size
= sizeof(XHCIState
),
2721 .class_init
= xhci_class_init
,
2724 static void xhci_register_types(void)
2726 type_register_static(&xhci_info
);
2729 type_init(xhci_register_types
)