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"
25 #include "qdev-addr.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)
51 /* Very pessimistic, let's hope it's enough for all cases */
52 #define EV_QUEUE (((3*TD_QUEUE)+16)*MAXSLOTS)
53 /* Do not deliver ER Full events. NEC's driver does some things not bound
54 * to the specs when it gets them */
58 #define OFF_OPER LEN_CAP
59 #define LEN_OPER (0x400 + 0x10 * MAXPORTS)
60 #define OFF_RUNTIME ((OFF_OPER + LEN_OPER + 0x20) & ~0x1f)
61 #define LEN_RUNTIME (0x20 + MAXINTRS * 0x20)
62 #define OFF_DOORBELL (OFF_RUNTIME + LEN_RUNTIME)
63 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20)
65 /* must be power of 2 */
66 #define LEN_REGS 0x2000
68 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS
69 # error Increase LEN_REGS
73 # error TODO: only one interrupter supported
77 #define USBCMD_RS (1<<0)
78 #define USBCMD_HCRST (1<<1)
79 #define USBCMD_INTE (1<<2)
80 #define USBCMD_HSEE (1<<3)
81 #define USBCMD_LHCRST (1<<7)
82 #define USBCMD_CSS (1<<8)
83 #define USBCMD_CRS (1<<9)
84 #define USBCMD_EWE (1<<10)
85 #define USBCMD_EU3S (1<<11)
87 #define USBSTS_HCH (1<<0)
88 #define USBSTS_HSE (1<<2)
89 #define USBSTS_EINT (1<<3)
90 #define USBSTS_PCD (1<<4)
91 #define USBSTS_SSS (1<<8)
92 #define USBSTS_RSS (1<<9)
93 #define USBSTS_SRE (1<<10)
94 #define USBSTS_CNR (1<<11)
95 #define USBSTS_HCE (1<<12)
98 #define PORTSC_CCS (1<<0)
99 #define PORTSC_PED (1<<1)
100 #define PORTSC_OCA (1<<3)
101 #define PORTSC_PR (1<<4)
102 #define PORTSC_PLS_SHIFT 5
103 #define PORTSC_PLS_MASK 0xf
104 #define PORTSC_PP (1<<9)
105 #define PORTSC_SPEED_SHIFT 10
106 #define PORTSC_SPEED_MASK 0xf
107 #define PORTSC_SPEED_FULL (1<<10)
108 #define PORTSC_SPEED_LOW (2<<10)
109 #define PORTSC_SPEED_HIGH (3<<10)
110 #define PORTSC_SPEED_SUPER (4<<10)
111 #define PORTSC_PIC_SHIFT 14
112 #define PORTSC_PIC_MASK 0x3
113 #define PORTSC_LWS (1<<16)
114 #define PORTSC_CSC (1<<17)
115 #define PORTSC_PEC (1<<18)
116 #define PORTSC_WRC (1<<19)
117 #define PORTSC_OCC (1<<20)
118 #define PORTSC_PRC (1<<21)
119 #define PORTSC_PLC (1<<22)
120 #define PORTSC_CEC (1<<23)
121 #define PORTSC_CAS (1<<24)
122 #define PORTSC_WCE (1<<25)
123 #define PORTSC_WDE (1<<26)
124 #define PORTSC_WOE (1<<27)
125 #define PORTSC_DR (1<<30)
126 #define PORTSC_WPR (1<<31)
128 #define CRCR_RCS (1<<0)
129 #define CRCR_CS (1<<1)
130 #define CRCR_CA (1<<2)
131 #define CRCR_CRR (1<<3)
133 #define IMAN_IP (1<<0)
134 #define IMAN_IE (1<<1)
136 #define ERDP_EHB (1<<3)
139 typedef struct XHCITRB
{
143 target_phys_addr_t addr
;
148 typedef enum TRBType
{
161 CR_CONFIGURE_ENDPOINT
,
169 CR_SET_LATENCY_TOLERANCE
,
170 CR_GET_PORT_BANDWIDTH
,
175 ER_PORT_STATUS_CHANGE
,
176 ER_BANDWIDTH_REQUEST
,
179 ER_DEVICE_NOTIFICATION
,
181 /* vendor specific bits */
182 CR_VENDOR_VIA_CHALLENGE_RESPONSE
= 48,
183 CR_VENDOR_NEC_FIRMWARE_REVISION
= 49,
184 CR_VENDOR_NEC_CHALLENGE_RESPONSE
= 50,
187 #define CR_LINK TR_LINK
189 typedef enum TRBCCode
{
192 CC_DATA_BUFFER_ERROR
,
194 CC_USB_TRANSACTION_ERROR
,
200 CC_INVALID_STREAM_TYPE_ERROR
,
201 CC_SLOT_NOT_ENABLED_ERROR
,
202 CC_EP_NOT_ENABLED_ERROR
,
208 CC_BANDWIDTH_OVERRUN
,
209 CC_CONTEXT_STATE_ERROR
,
210 CC_NO_PING_RESPONSE_ERROR
,
211 CC_EVENT_RING_FULL_ERROR
,
212 CC_INCOMPATIBLE_DEVICE_ERROR
,
213 CC_MISSED_SERVICE_ERROR
,
214 CC_COMMAND_RING_STOPPED
,
217 CC_STOPPED_LENGTH_INVALID
,
218 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR
= 29,
219 CC_ISOCH_BUFFER_OVERRUN
= 31,
222 CC_INVALID_STREAM_ID_ERROR
,
223 CC_SECONDARY_BANDWIDTH_ERROR
,
224 CC_SPLIT_TRANSACTION_ERROR
228 #define TRB_TYPE_SHIFT 10
229 #define TRB_TYPE_MASK 0x3f
230 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK)
232 #define TRB_EV_ED (1<<2)
234 #define TRB_TR_ENT (1<<1)
235 #define TRB_TR_ISP (1<<2)
236 #define TRB_TR_NS (1<<3)
237 #define TRB_TR_CH (1<<4)
238 #define TRB_TR_IOC (1<<5)
239 #define TRB_TR_IDT (1<<6)
240 #define TRB_TR_TBC_SHIFT 7
241 #define TRB_TR_TBC_MASK 0x3
242 #define TRB_TR_BEI (1<<9)
243 #define TRB_TR_TLBPC_SHIFT 16
244 #define TRB_TR_TLBPC_MASK 0xf
245 #define TRB_TR_FRAMEID_SHIFT 20
246 #define TRB_TR_FRAMEID_MASK 0x7ff
247 #define TRB_TR_SIA (1<<31)
249 #define TRB_TR_DIR (1<<16)
251 #define TRB_CR_SLOTID_SHIFT 24
252 #define TRB_CR_SLOTID_MASK 0xff
253 #define TRB_CR_EPID_SHIFT 16
254 #define TRB_CR_EPID_MASK 0x1f
256 #define TRB_CR_BSR (1<<9)
257 #define TRB_CR_DC (1<<9)
259 #define TRB_LK_TC (1<<1)
261 #define EP_TYPE_MASK 0x7
262 #define EP_TYPE_SHIFT 3
264 #define EP_STATE_MASK 0x7
265 #define EP_DISABLED (0<<0)
266 #define EP_RUNNING (1<<0)
267 #define EP_HALTED (2<<0)
268 #define EP_STOPPED (3<<0)
269 #define EP_ERROR (4<<0)
271 #define SLOT_STATE_MASK 0x1f
272 #define SLOT_STATE_SHIFT 27
273 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK)
274 #define SLOT_ENABLED 0
275 #define SLOT_DEFAULT 1
276 #define SLOT_ADDRESSED 2
277 #define SLOT_CONFIGURED 3
279 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f
280 #define SLOT_CONTEXT_ENTRIES_SHIFT 27
282 typedef enum EPType
{
293 typedef struct XHCIRing
{
294 target_phys_addr_t base
;
295 target_phys_addr_t dequeue
;
299 typedef struct XHCIPort
{
305 typedef struct XHCIState XHCIState
;
307 typedef struct XHCITransfer
{
314 unsigned int iso_pkts
;
321 unsigned int trb_count
;
322 unsigned int trb_alloced
;
325 unsigned int data_length
;
326 unsigned int data_alloced
;
332 unsigned int pktsize
;
333 unsigned int cur_pkt
;
336 typedef struct XHCIEPContext
{
338 unsigned int next_xfer
;
339 unsigned int comp_xfer
;
340 XHCITransfer transfers
[TD_QUEUE
];
343 unsigned int next_bg
;
344 XHCITransfer bg_transfers
[BG_XFERS
];
346 target_phys_addr_t pctx
;
347 unsigned int max_psize
;
352 typedef struct XHCISlot
{
354 target_phys_addr_t ctx
;
356 unsigned int devaddr
;
357 XHCIEPContext
* eps
[31];
360 typedef struct XHCIEvent
{
377 unsigned int devaddr
;
379 /* Operational Registers */
386 uint32_t dcbaap_high
;
389 XHCIPort ports
[MAXPORTS
];
390 XHCISlot slots
[MAXSLOTS
];
392 /* Runtime Registers */
394 /* note: we only support one interrupter */
399 uint32_t erstba_high
;
403 target_phys_addr_t er_start
;
406 unsigned int er_ep_idx
;
409 XHCIEvent ev_buffer
[EV_QUEUE
];
410 unsigned int ev_buffer_put
;
411 unsigned int ev_buffer_get
;
416 typedef struct XHCIEvRingSeg
{
423 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
,
426 static inline target_phys_addr_t
xhci_addr64(uint32_t low
, uint32_t high
)
428 #if TARGET_PHYS_ADDR_BITS > 32
429 return low
| ((target_phys_addr_t
)high
<< 32);
435 static inline target_phys_addr_t
xhci_mask64(uint64_t addr
)
437 #if TARGET_PHYS_ADDR_BITS > 32
440 return addr
& 0xffffffff;
444 static void xhci_irq_update(XHCIState
*xhci
)
448 if (xhci
->iman
& IMAN_IP
&& xhci
->iman
& IMAN_IE
&&
449 xhci
->usbcmd
&& USBCMD_INTE
) {
453 DPRINTF("xhci_irq_update(): %d\n", level
);
455 if (xhci
->msi
&& msi_enabled(&xhci
->pci_dev
)) {
457 DPRINTF("xhci_irq_update(): MSI signal\n");
458 msi_notify(&xhci
->pci_dev
, 0);
461 qemu_set_irq(xhci
->irq
, level
);
465 static inline int xhci_running(XHCIState
*xhci
)
467 return !(xhci
->usbsts
& USBSTS_HCH
) && !xhci
->er_full
;
470 static void xhci_die(XHCIState
*xhci
)
472 xhci
->usbsts
|= USBSTS_HCE
;
473 fprintf(stderr
, "xhci: asserted controller error\n");
476 static void xhci_write_event(XHCIState
*xhci
, XHCIEvent
*event
)
479 target_phys_addr_t addr
;
481 ev_trb
.parameter
= cpu_to_le64(event
->ptr
);
482 ev_trb
.status
= cpu_to_le32(event
->length
| (event
->ccode
<< 24));
483 ev_trb
.control
= (event
->slotid
<< 24) | (event
->epid
<< 16) |
484 event
->flags
| (event
->type
<< TRB_TYPE_SHIFT
);
486 ev_trb
.control
|= TRB_C
;
488 ev_trb
.control
= cpu_to_le32(ev_trb
.control
);
490 DPRINTF("xhci_write_event(): [%d] %016"PRIx64
" %08x %08x\n",
491 xhci
->er_ep_idx
, ev_trb
.parameter
, ev_trb
.status
, ev_trb
.control
);
493 addr
= xhci
->er_start
+ TRB_SIZE
*xhci
->er_ep_idx
;
494 cpu_physical_memory_write(addr
, (uint8_t *) &ev_trb
, TRB_SIZE
);
497 if (xhci
->er_ep_idx
>= xhci
->er_size
) {
499 xhci
->er_pcs
= !xhci
->er_pcs
;
503 static void xhci_events_update(XHCIState
*xhci
)
505 target_phys_addr_t erdp
;
509 if (xhci
->usbsts
& USBSTS_HCH
) {
513 erdp
= xhci_addr64(xhci
->erdp_low
, xhci
->erdp_high
);
514 if (erdp
< xhci
->er_start
||
515 erdp
>= (xhci
->er_start
+ TRB_SIZE
*xhci
->er_size
)) {
516 fprintf(stderr
, "xhci: ERDP out of bounds: "TARGET_FMT_plx
"\n", erdp
);
517 fprintf(stderr
, "xhci: ER at "TARGET_FMT_plx
" len %d\n",
518 xhci
->er_start
, xhci
->er_size
);
522 dp_idx
= (erdp
- xhci
->er_start
) / TRB_SIZE
;
523 assert(dp_idx
< xhci
->er_size
);
525 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus
526 * deadlocks when the ER is full. Hack it by holding off events until
527 * the driver decides to free at least half of the ring */
529 int er_free
= dp_idx
- xhci
->er_ep_idx
;
531 er_free
+= xhci
->er_size
;
533 if (er_free
< (xhci
->er_size
/2)) {
534 DPRINTF("xhci_events_update(): event ring still "
535 "more than half full (hack)\n");
540 while (xhci
->ev_buffer_put
!= xhci
->ev_buffer_get
) {
541 assert(xhci
->er_full
);
542 if (((xhci
->er_ep_idx
+1) % xhci
->er_size
) == dp_idx
) {
543 DPRINTF("xhci_events_update(): event ring full again\n");
545 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
546 xhci_write_event(xhci
, &full
);
551 XHCIEvent
*event
= &xhci
->ev_buffer
[xhci
->ev_buffer_get
];
552 xhci_write_event(xhci
, event
);
553 xhci
->ev_buffer_get
++;
555 if (xhci
->ev_buffer_get
== EV_QUEUE
) {
556 xhci
->ev_buffer_get
= 0;
561 xhci
->erdp_low
|= ERDP_EHB
;
562 xhci
->iman
|= IMAN_IP
;
563 xhci
->usbsts
|= USBSTS_EINT
;
564 xhci_irq_update(xhci
);
567 if (xhci
->er_full
&& xhci
->ev_buffer_put
== xhci
->ev_buffer_get
) {
568 DPRINTF("xhci_events_update(): event ring no longer full\n");
574 static void xhci_event(XHCIState
*xhci
, XHCIEvent
*event
)
576 target_phys_addr_t erdp
;
580 DPRINTF("xhci_event(): ER full, queueing\n");
581 if (((xhci
->ev_buffer_put
+1) % EV_QUEUE
) == xhci
->ev_buffer_get
) {
582 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
585 xhci
->ev_buffer
[xhci
->ev_buffer_put
++] = *event
;
586 if (xhci
->ev_buffer_put
== EV_QUEUE
) {
587 xhci
->ev_buffer_put
= 0;
592 erdp
= xhci_addr64(xhci
->erdp_low
, xhci
->erdp_high
);
593 if (erdp
< xhci
->er_start
||
594 erdp
>= (xhci
->er_start
+ TRB_SIZE
*xhci
->er_size
)) {
595 fprintf(stderr
, "xhci: ERDP out of bounds: "TARGET_FMT_plx
"\n", erdp
);
596 fprintf(stderr
, "xhci: ER at "TARGET_FMT_plx
" len %d\n",
597 xhci
->er_start
, xhci
->er_size
);
602 dp_idx
= (erdp
- xhci
->er_start
) / TRB_SIZE
;
603 assert(dp_idx
< xhci
->er_size
);
605 if ((xhci
->er_ep_idx
+1) % xhci
->er_size
== dp_idx
) {
606 DPRINTF("xhci_event(): ER full, queueing\n");
608 XHCIEvent full
= {ER_HOST_CONTROLLER
, CC_EVENT_RING_FULL_ERROR
};
609 xhci_write_event(xhci
, &full
);
612 if (((xhci
->ev_buffer_put
+1) % EV_QUEUE
) == xhci
->ev_buffer_get
) {
613 fprintf(stderr
, "xhci: event queue full, dropping event!\n");
616 xhci
->ev_buffer
[xhci
->ev_buffer_put
++] = *event
;
617 if (xhci
->ev_buffer_put
== EV_QUEUE
) {
618 xhci
->ev_buffer_put
= 0;
621 xhci_write_event(xhci
, event
);
624 xhci
->erdp_low
|= ERDP_EHB
;
625 xhci
->iman
|= IMAN_IP
;
626 xhci
->usbsts
|= USBSTS_EINT
;
628 xhci_irq_update(xhci
);
631 static void xhci_ring_init(XHCIState
*xhci
, XHCIRing
*ring
,
632 target_phys_addr_t base
)
635 ring
->dequeue
= base
;
639 static TRBType
xhci_ring_fetch(XHCIState
*xhci
, XHCIRing
*ring
, XHCITRB
*trb
,
640 target_phys_addr_t
*addr
)
644 cpu_physical_memory_read(ring
->dequeue
, (uint8_t *) trb
, TRB_SIZE
);
645 trb
->addr
= ring
->dequeue
;
646 trb
->ccs
= ring
->ccs
;
647 le64_to_cpus(&trb
->parameter
);
648 le32_to_cpus(&trb
->status
);
649 le32_to_cpus(&trb
->control
);
651 DPRINTF("xhci: TRB fetched [" TARGET_FMT_plx
"]: "
652 "%016" PRIx64
" %08x %08x\n",
653 ring
->dequeue
, trb
->parameter
, trb
->status
, trb
->control
);
655 if ((trb
->control
& TRB_C
) != ring
->ccs
) {
659 type
= TRB_TYPE(*trb
);
661 if (type
!= TR_LINK
) {
663 *addr
= ring
->dequeue
;
665 ring
->dequeue
+= TRB_SIZE
;
668 ring
->dequeue
= xhci_mask64(trb
->parameter
);
669 if (trb
->control
& TRB_LK_TC
) {
670 ring
->ccs
= !ring
->ccs
;
676 static int xhci_ring_chain_length(XHCIState
*xhci
, const XHCIRing
*ring
)
680 target_phys_addr_t dequeue
= ring
->dequeue
;
681 bool ccs
= ring
->ccs
;
682 /* hack to bundle together the two/three TDs that make a setup transfer */
683 bool control_td_set
= 0;
687 cpu_physical_memory_read(dequeue
, (uint8_t *) &trb
, TRB_SIZE
);
688 le64_to_cpus(&trb
.parameter
);
689 le32_to_cpus(&trb
.status
);
690 le32_to_cpus(&trb
.control
);
692 DPRINTF("xhci: TRB peeked [" TARGET_FMT_plx
"]: "
693 "%016" PRIx64
" %08x %08x\n",
694 dequeue
, trb
.parameter
, trb
.status
, trb
.control
);
696 if ((trb
.control
& TRB_C
) != ccs
) {
700 type
= TRB_TYPE(trb
);
702 if (type
== TR_LINK
) {
703 dequeue
= xhci_mask64(trb
.parameter
);
704 if (trb
.control
& TRB_LK_TC
) {
713 if (type
== TR_SETUP
) {
715 } else if (type
== TR_STATUS
) {
719 if (!control_td_set
&& !(trb
.control
& TRB_TR_CH
)) {
725 static void xhci_er_reset(XHCIState
*xhci
)
729 /* cache the (sole) event ring segment location */
730 if (xhci
->erstsz
!= 1) {
731 fprintf(stderr
, "xhci: invalid value for ERSTSZ: %d\n", xhci
->erstsz
);
735 target_phys_addr_t erstba
= xhci_addr64(xhci
->erstba_low
, xhci
->erstba_high
);
736 cpu_physical_memory_read(erstba
, (uint8_t *) &seg
, sizeof(seg
));
737 le32_to_cpus(&seg
.addr_low
);
738 le32_to_cpus(&seg
.addr_high
);
739 le32_to_cpus(&seg
.size
);
740 if (seg
.size
< 16 || seg
.size
> 4096) {
741 fprintf(stderr
, "xhci: invalid value for segment size: %d\n", seg
.size
);
745 xhci
->er_start
= xhci_addr64(seg
.addr_low
, seg
.addr_high
);
746 xhci
->er_size
= seg
.size
;
752 DPRINTF("xhci: event ring:" TARGET_FMT_plx
" [%d]\n",
753 xhci
->er_start
, xhci
->er_size
);
756 static void xhci_run(XHCIState
*xhci
)
758 DPRINTF("xhci_run()\n");
760 xhci
->usbsts
&= ~USBSTS_HCH
;
763 static void xhci_stop(XHCIState
*xhci
)
765 DPRINTF("xhci_stop()\n");
766 xhci
->usbsts
|= USBSTS_HCH
;
767 xhci
->crcr_low
&= ~CRCR_CRR
;
770 static void xhci_set_ep_state(XHCIState
*xhci
, XHCIEPContext
*epctx
,
774 if (epctx
->state
== state
) {
778 cpu_physical_memory_read(epctx
->pctx
, (uint8_t *) ctx
, sizeof(ctx
));
779 ctx
[0] &= ~EP_STATE_MASK
;
781 ctx
[2] = epctx
->ring
.dequeue
| epctx
->ring
.ccs
;
782 ctx
[3] = (epctx
->ring
.dequeue
>> 16) >> 16;
783 DPRINTF("xhci: set epctx: " TARGET_FMT_plx
" state=%d dequeue=%08x%08x\n",
784 epctx
->pctx
, state
, ctx
[3], ctx
[2]);
785 cpu_physical_memory_write(epctx
->pctx
, (uint8_t *) ctx
, sizeof(ctx
));
786 epctx
->state
= state
;
789 static TRBCCode
xhci_enable_ep(XHCIState
*xhci
, unsigned int slotid
,
790 unsigned int epid
, target_phys_addr_t pctx
,
794 XHCIEPContext
*epctx
;
795 target_phys_addr_t dequeue
;
798 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
799 assert(epid
>= 1 && epid
<= 31);
801 DPRINTF("xhci_enable_ep(%d, %d)\n", slotid
, epid
);
803 slot
= &xhci
->slots
[slotid
-1];
804 if (slot
->eps
[epid
-1]) {
805 fprintf(stderr
, "xhci: slot %d ep %d already enabled!\n", slotid
, epid
);
809 epctx
= g_malloc(sizeof(XHCIEPContext
));
810 memset(epctx
, 0, sizeof(XHCIEPContext
));
812 slot
->eps
[epid
-1] = epctx
;
814 dequeue
= xhci_addr64(ctx
[2] & ~0xf, ctx
[3]);
815 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
);
816 epctx
->ring
.ccs
= ctx
[2] & 1;
818 epctx
->type
= (ctx
[1] >> EP_TYPE_SHIFT
) & EP_TYPE_MASK
;
819 DPRINTF("xhci: endpoint %d.%d type is %d\n", epid
/2, epid
%2, epctx
->type
);
821 epctx
->max_psize
= ctx
[1]>>16;
822 epctx
->max_psize
*= 1+((ctx
[1]>>8)&0xff);
823 epctx
->has_bg
= false;
824 if (epctx
->type
== ET_ISO_IN
) {
825 epctx
->has_bg
= true;
827 DPRINTF("xhci: endpoint %d.%d max transaction (burst) size is %d\n",
828 epid
/2, epid
%2, epctx
->max_psize
);
829 for (i
= 0; i
< ARRAY_SIZE(epctx
->transfers
); i
++) {
830 usb_packet_init(&epctx
->transfers
[i
].packet
);
833 epctx
->state
= EP_RUNNING
;
834 ctx
[0] &= ~EP_STATE_MASK
;
835 ctx
[0] |= EP_RUNNING
;
840 static int xhci_ep_nuke_xfers(XHCIState
*xhci
, unsigned int slotid
,
844 XHCIEPContext
*epctx
;
845 int i
, xferi
, killed
= 0;
846 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
847 assert(epid
>= 1 && epid
<= 31);
849 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid
, epid
);
851 slot
= &xhci
->slots
[slotid
-1];
853 if (!slot
->eps
[epid
-1]) {
857 epctx
= slot
->eps
[epid
-1];
859 xferi
= epctx
->next_xfer
;
860 for (i
= 0; i
< TD_QUEUE
; i
++) {
861 XHCITransfer
*t
= &epctx
->transfers
[xferi
];
864 /* libusb_cancel_transfer(t->usbxfer) */
865 DPRINTF("xhci: cancelling transfer %d, waiting for it to complete...\n", i
);
868 if (t
->backgrounded
) {
880 t
->trb_count
= t
->trb_alloced
= 0;
881 t
->data_length
= t
->data_alloced
= 0;
882 xferi
= (xferi
+ 1) % TD_QUEUE
;
885 xferi
= epctx
->next_bg
;
886 for (i
= 0; i
< BG_XFERS
; i
++) {
887 XHCITransfer
*t
= &epctx
->bg_transfers
[xferi
];
890 /* libusb_cancel_transfer(t->usbxfer); */
891 DPRINTF("xhci: cancelling bg transfer %d, waiting for it to complete...\n", i
);
899 xferi
= (xferi
+ 1) % BG_XFERS
;
905 static TRBCCode
xhci_disable_ep(XHCIState
*xhci
, unsigned int slotid
,
909 XHCIEPContext
*epctx
;
911 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
912 assert(epid
>= 1 && epid
<= 31);
914 DPRINTF("xhci_disable_ep(%d, %d)\n", slotid
, epid
);
916 slot
= &xhci
->slots
[slotid
-1];
918 if (!slot
->eps
[epid
-1]) {
919 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid
, epid
);
923 xhci_ep_nuke_xfers(xhci
, slotid
, epid
);
925 epctx
= slot
->eps
[epid
-1];
927 xhci_set_ep_state(xhci
, epctx
, EP_DISABLED
);
930 slot
->eps
[epid
-1] = NULL
;
935 static TRBCCode
xhci_stop_ep(XHCIState
*xhci
, unsigned int slotid
,
939 XHCIEPContext
*epctx
;
941 DPRINTF("xhci_stop_ep(%d, %d)\n", slotid
, epid
);
943 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
945 if (epid
< 1 || epid
> 31) {
946 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
950 slot
= &xhci
->slots
[slotid
-1];
952 if (!slot
->eps
[epid
-1]) {
953 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
954 return CC_EP_NOT_ENABLED_ERROR
;
957 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
958 fprintf(stderr
, "xhci: FIXME: endpoint stopped w/ xfers running, "
959 "data might be lost\n");
962 epctx
= slot
->eps
[epid
-1];
964 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
969 static TRBCCode
xhci_reset_ep(XHCIState
*xhci
, unsigned int slotid
,
973 XHCIEPContext
*epctx
;
976 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
978 DPRINTF("xhci_reset_ep(%d, %d)\n", slotid
, epid
);
980 if (epid
< 1 || epid
> 31) {
981 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
985 slot
= &xhci
->slots
[slotid
-1];
987 if (!slot
->eps
[epid
-1]) {
988 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
989 return CC_EP_NOT_ENABLED_ERROR
;
992 epctx
= slot
->eps
[epid
-1];
994 if (epctx
->state
!= EP_HALTED
) {
995 fprintf(stderr
, "xhci: reset EP while EP %d not halted (%d)\n",
997 return CC_CONTEXT_STATE_ERROR
;
1000 if (xhci_ep_nuke_xfers(xhci
, slotid
, epid
) > 0) {
1001 fprintf(stderr
, "xhci: FIXME: endpoint reset w/ xfers running, "
1002 "data might be lost\n");
1005 uint8_t ep
= epid
>>1;
1011 dev
= xhci
->ports
[xhci
->slots
[slotid
-1].port
-1].port
.dev
;
1013 return CC_USB_TRANSACTION_ERROR
;
1016 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1021 static TRBCCode
xhci_set_ep_dequeue(XHCIState
*xhci
, unsigned int slotid
,
1022 unsigned int epid
, uint64_t pdequeue
)
1025 XHCIEPContext
*epctx
;
1026 target_phys_addr_t dequeue
;
1028 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1030 if (epid
< 1 || epid
> 31) {
1031 fprintf(stderr
, "xhci: bad ep %d\n", epid
);
1032 return CC_TRB_ERROR
;
1035 DPRINTF("xhci_set_ep_dequeue(%d, %d, %016"PRIx64
")\n", slotid
, epid
, pdequeue
);
1036 dequeue
= xhci_mask64(pdequeue
);
1038 slot
= &xhci
->slots
[slotid
-1];
1040 if (!slot
->eps
[epid
-1]) {
1041 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid
, epid
);
1042 return CC_EP_NOT_ENABLED_ERROR
;
1045 epctx
= slot
->eps
[epid
-1];
1048 if (epctx
->state
!= EP_STOPPED
) {
1049 fprintf(stderr
, "xhci: set EP dequeue pointer while EP %d not stopped\n", epid
);
1050 return CC_CONTEXT_STATE_ERROR
;
1053 xhci_ring_init(xhci
, &epctx
->ring
, dequeue
& ~0xF);
1054 epctx
->ring
.ccs
= dequeue
& 1;
1056 xhci_set_ep_state(xhci
, epctx
, EP_STOPPED
);
1061 static int xhci_xfer_data(XHCITransfer
*xfer
, uint8_t *data
,
1062 unsigned int length
, bool in_xfer
, bool out_xfer
,
1067 unsigned int transferred
= 0;
1068 unsigned int left
= length
;
1071 XHCIEvent event
= {ER_TRANSFER
, CC_SUCCESS
};
1072 XHCIState
*xhci
= xfer
->xhci
;
1074 DPRINTF("xhci_xfer_data(len=%d, in_xfer=%d, out_xfer=%d, report=%d)\n",
1075 length
, in_xfer
, out_xfer
, report
);
1077 assert(!(in_xfer
&& out_xfer
));
1079 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1080 XHCITRB
*trb
= &xfer
->trbs
[i
];
1081 target_phys_addr_t addr
;
1082 unsigned int chunk
= 0;
1084 switch (TRB_TYPE(*trb
)) {
1086 if ((!(trb
->control
& TRB_TR_DIR
)) != (!in_xfer
)) {
1087 fprintf(stderr
, "xhci: data direction mismatch for TR_DATA\n");
1094 addr
= xhci_mask64(trb
->parameter
);
1095 chunk
= trb
->status
& 0x1ffff;
1100 if (in_xfer
|| out_xfer
) {
1101 if (trb
->control
& TRB_TR_IDT
) {
1103 if (chunk
> 8 || in_xfer
) {
1104 fprintf(stderr
, "xhci: invalid immediate data TRB\n");
1108 idata
= le64_to_cpu(trb
->parameter
);
1109 memcpy(data
, &idata
, chunk
);
1111 DPRINTF("xhci_xfer_data: r/w(%d) %d bytes at "
1112 TARGET_FMT_plx
"\n", in_xfer
, chunk
, addr
);
1114 cpu_physical_memory_write(addr
, data
, chunk
);
1116 cpu_physical_memory_read(addr
, data
, chunk
);
1119 unsigned int count
= chunk
;
1125 for (i
= 0; i
< count
; i
++) {
1126 DPRINTF(" %02x", data
[i
]);
1135 transferred
+= chunk
;
1143 if (report
&& !reported
&& (trb
->control
& TRB_TR_IOC
||
1144 (shortpkt
&& (trb
->control
& TRB_TR_ISP
)))) {
1145 event
.slotid
= xfer
->slotid
;
1146 event
.epid
= xfer
->epid
;
1147 event
.length
= (trb
->status
& 0x1ffff) - chunk
;
1149 event
.ptr
= trb
->addr
;
1150 if (xfer
->status
== CC_SUCCESS
) {
1151 event
.ccode
= shortpkt
? CC_SHORT_PACKET
: CC_SUCCESS
;
1153 event
.ccode
= xfer
->status
;
1155 if (TRB_TYPE(*trb
) == TR_EVDATA
) {
1156 event
.ptr
= trb
->parameter
;
1157 event
.flags
|= TRB_EV_ED
;
1158 event
.length
= edtla
& 0xffffff;
1159 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event
.length
);
1162 xhci_event(xhci
, &event
);
1169 static void xhci_stall_ep(XHCITransfer
*xfer
)
1171 XHCIState
*xhci
= xfer
->xhci
;
1172 XHCISlot
*slot
= &xhci
->slots
[xfer
->slotid
-1];
1173 XHCIEPContext
*epctx
= slot
->eps
[xfer
->epid
-1];
1175 epctx
->ring
.dequeue
= xfer
->trbs
[0].addr
;
1176 epctx
->ring
.ccs
= xfer
->trbs
[0].ccs
;
1177 xhci_set_ep_state(xhci
, epctx
, EP_HALTED
);
1178 DPRINTF("xhci: stalled slot %d ep %d\n", xfer
->slotid
, xfer
->epid
);
1179 DPRINTF("xhci: will continue at "TARGET_FMT_plx
"\n", epctx
->ring
.dequeue
);
1182 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
,
1183 XHCIEPContext
*epctx
);
1185 static void xhci_bg_update(XHCIState
*xhci
, XHCIEPContext
*epctx
)
1187 if (epctx
->bg_updating
) {
1190 DPRINTF("xhci_bg_update(%p, %p)\n", xhci
, epctx
);
1191 assert(epctx
->has_bg
);
1192 DPRINTF("xhci: fg=%d bg=%d\n", epctx
->comp_xfer
, epctx
->next_bg
);
1193 epctx
->bg_updating
= 1;
1194 while (epctx
->transfers
[epctx
->comp_xfer
].backgrounded
&&
1195 epctx
->bg_transfers
[epctx
->next_bg
].complete
) {
1196 XHCITransfer
*fg
= &epctx
->transfers
[epctx
->comp_xfer
];
1197 XHCITransfer
*bg
= &epctx
->bg_transfers
[epctx
->next_bg
];
1199 DPRINTF("xhci: completing fg %d from bg %d.%d (stat: %d)\n",
1200 epctx
->comp_xfer
, epctx
->next_bg
, bg
->cur_pkt
,
1201 bg
->usbxfer
->iso_packet_desc
[bg
->cur_pkt
].status
1204 assert(epctx
->type
== ET_ISO_IN
);
1205 assert(bg
->iso_xfer
);
1206 assert(bg
->in_xfer
);
1207 uint8_t *p
= bg
->data
+ bg
->cur_pkt
* bg
->pktsize
;
1209 int len
= bg
->usbxfer
->iso_packet_desc
[bg
->cur_pkt
].actual_length
;
1210 fg
->status
= libusb_to_ccode(bg
->usbxfer
->iso_packet_desc
[bg
->cur_pkt
].status
);
1216 fg
->backgrounded
= 0;
1218 if (fg
->status
== CC_STALL_ERROR
) {
1222 xhci_xfer_data(fg
, p
, len
, 1, 0, 1);
1225 if (epctx
->comp_xfer
== TD_QUEUE
) {
1226 epctx
->comp_xfer
= 0;
1228 DPRINTF("next fg xfer: %d\n", epctx
->comp_xfer
);
1230 if (bg
->cur_pkt
== bg
->pkts
) {
1232 if (xhci_submit(xhci
, bg
, epctx
) < 0) {
1233 fprintf(stderr
, "xhci: bg resubmit failed\n");
1236 if (epctx
->next_bg
== BG_XFERS
) {
1239 DPRINTF("next bg xfer: %d\n", epctx
->next_bg
);
1241 xhci_kick_ep(xhci
, fg
->slotid
, fg
->epid
);
1244 epctx
->bg_updating
= 0;
1248 static void xhci_xfer_cb(struct libusb_transfer
*transfer
)
1253 xfer
= (XHCITransfer
*)transfer
->user_data
;
1256 DPRINTF("xhci_xfer_cb(slot=%d, ep=%d, status=%d)\n", xfer
->slotid
,
1257 xfer
->epid
, transfer
->status
);
1259 assert(xfer
->slotid
>= 1 && xfer
->slotid
<= MAXSLOTS
);
1260 assert(xfer
->epid
>= 1 && xfer
->epid
<= 31);
1262 if (xfer
->cancelled
) {
1263 DPRINTF("xhci: transfer cancelled, not reporting anything\n");
1268 XHCIEPContext
*epctx
;
1270 slot
= &xhci
->slots
[xfer
->slotid
-1];
1271 assert(slot
->eps
[xfer
->epid
-1]);
1272 epctx
= slot
->eps
[xfer
->epid
-1];
1274 if (xfer
->bg_xfer
) {
1275 DPRINTF("xhci: background transfer, updating\n");
1278 xhci_bg_update(xhci
, epctx
);
1282 if (xfer
->iso_xfer
) {
1283 transfer
->status
= transfer
->iso_packet_desc
[0].status
;
1284 transfer
->actual_length
= transfer
->iso_packet_desc
[0].actual_length
;
1287 xfer
->status
= libusb_to_ccode(transfer
->status
);
1292 if (transfer
->status
== LIBUSB_TRANSFER_STALL
)
1293 xhci_stall_ep(xhci
, epctx
, xfer
);
1295 DPRINTF("xhci: transfer actual length = %d\n", transfer
->actual_length
);
1297 if (xfer
->in_xfer
) {
1298 if (xfer
->epid
== 1) {
1299 xhci_xfer_data(xhci
, xfer
, xfer
->data
+ 8,
1300 transfer
->actual_length
, 1, 0, 1);
1302 xhci_xfer_data(xhci
, xfer
, xfer
->data
,
1303 transfer
->actual_length
, 1, 0, 1);
1306 xhci_xfer_data(xhci
, xfer
, NULL
, transfer
->actual_length
, 0, 0, 1);
1309 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1312 static int xhci_hle_control(XHCIState
*xhci
, XHCITransfer
*xfer
,
1313 uint8_t bmRequestType
, uint8_t bRequest
,
1314 uint16_t wValue
, uint16_t wIndex
, uint16_t wLength
)
1316 uint16_t type_req
= (bmRequestType
<< 8) | bRequest
;
1319 case 0x0000 | USB_REQ_SET_CONFIGURATION
:
1320 DPRINTF("xhci: HLE switch configuration\n");
1321 return xhci_switch_config(xhci
, xfer
->slotid
, wValue
) == 0;
1322 case 0x0100 | USB_REQ_SET_INTERFACE
:
1323 DPRINTF("xhci: HLE set interface altsetting\n");
1324 return xhci_set_iface_alt(xhci
, xfer
->slotid
, wIndex
, wValue
) == 0;
1325 case 0x0200 | USB_REQ_CLEAR_FEATURE
:
1326 if (wValue
== 0) { // endpoint halt
1327 DPRINTF("xhci: HLE clear halt\n");
1328 return xhci_clear_halt(xhci
, xfer
->slotid
, wIndex
);
1330 case 0x0000 | USB_REQ_SET_ADDRESS
:
1331 fprintf(stderr
, "xhci: warn: illegal SET_ADDRESS request\n");
1339 static int xhci_setup_packet(XHCITransfer
*xfer
, XHCIPort
*port
, int ep
)
1341 usb_packet_setup(&xfer
->packet
,
1342 xfer
->in_xfer
? USB_TOKEN_IN
: USB_TOKEN_OUT
,
1343 xfer
->xhci
->slots
[xfer
->slotid
-1].devaddr
,
1345 usb_packet_addbuf(&xfer
->packet
, xfer
->data
, xfer
->data_length
);
1346 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
1347 xfer
->packet
.pid
, xfer
->packet
.devaddr
, xfer
->packet
.devep
);
1351 static int xhci_complete_packet(XHCITransfer
*xfer
, int ret
)
1353 if (ret
== USB_RET_ASYNC
) {
1356 xfer
->cancelled
= 0;
1364 xfer
->status
= CC_SUCCESS
;
1365 xhci_xfer_data(xfer
, xfer
->data
, ret
, xfer
->in_xfer
, 0, 1);
1372 xfer
->status
= CC_USB_TRANSACTION_ERROR
;
1373 xhci_xfer_data(xfer
, xfer
->data
, 0, xfer
->in_xfer
, 0, 1);
1374 xhci_stall_ep(xfer
);
1377 xfer
->status
= CC_STALL_ERROR
;
1378 xhci_xfer_data(xfer
, xfer
->data
, 0, xfer
->in_xfer
, 0, 1);
1379 xhci_stall_ep(xfer
);
1382 fprintf(stderr
, "%s: FIXME: ret = %d\n", __FUNCTION__
, ret
);
1388 static int xhci_fire_ctl_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
)
1390 XHCITRB
*trb_setup
, *trb_status
;
1391 uint8_t bmRequestType
, bRequest
;
1392 uint16_t wValue
, wLength
, wIndex
;
1397 DPRINTF("xhci_fire_ctl_transfer(slot=%d)\n", xfer
->slotid
);
1399 trb_setup
= &xfer
->trbs
[0];
1400 trb_status
= &xfer
->trbs
[xfer
->trb_count
-1];
1402 /* at most one Event Data TRB allowed after STATUS */
1403 if (TRB_TYPE(*trb_status
) == TR_EVDATA
&& xfer
->trb_count
> 2) {
1407 /* do some sanity checks */
1408 if (TRB_TYPE(*trb_setup
) != TR_SETUP
) {
1409 fprintf(stderr
, "xhci: ep0 first TD not SETUP: %d\n",
1410 TRB_TYPE(*trb_setup
));
1413 if (TRB_TYPE(*trb_status
) != TR_STATUS
) {
1414 fprintf(stderr
, "xhci: ep0 last TD not STATUS: %d\n",
1415 TRB_TYPE(*trb_status
));
1418 if (!(trb_setup
->control
& TRB_TR_IDT
)) {
1419 fprintf(stderr
, "xhci: Setup TRB doesn't have IDT set\n");
1422 if ((trb_setup
->status
& 0x1ffff) != 8) {
1423 fprintf(stderr
, "xhci: Setup TRB has bad length (%d)\n",
1424 (trb_setup
->status
& 0x1ffff));
1428 bmRequestType
= trb_setup
->parameter
;
1429 bRequest
= trb_setup
->parameter
>> 8;
1430 wValue
= trb_setup
->parameter
>> 16;
1431 wIndex
= trb_setup
->parameter
>> 32;
1432 wLength
= trb_setup
->parameter
>> 48;
1434 if (xfer
->data
&& xfer
->data_alloced
< wLength
) {
1435 xfer
->data_alloced
= 0;
1440 DPRINTF("xhci: alloc %d bytes data\n", wLength
);
1441 xfer
->data
= g_malloc(wLength
+1);
1442 xfer
->data_alloced
= wLength
;
1444 xfer
->data_length
= wLength
;
1446 port
= &xhci
->ports
[xhci
->slots
[xfer
->slotid
-1].port
-1];
1447 dev
= port
->port
.dev
;
1449 fprintf(stderr
, "xhci: slot %d port %d has no device\n", xfer
->slotid
,
1450 xhci
->slots
[xfer
->slotid
-1].port
);
1454 xfer
->in_xfer
= bmRequestType
& USB_DIR_IN
;
1455 xfer
->iso_xfer
= false;
1457 xhci_setup_packet(xfer
, port
, 0);
1458 if (!xfer
->in_xfer
) {
1459 xhci_xfer_data(xfer
, xfer
->data
, wLength
, 0, 1, 0);
1461 ret
= usb_device_handle_control(dev
, &xfer
->packet
,
1462 (bmRequestType
<< 8) | bRequest
,
1463 wValue
, wIndex
, wLength
, xfer
->data
);
1465 xhci_complete_packet(xfer
, ret
);
1466 if (!xfer
->running
) {
1467 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1472 static int xhci_submit(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1478 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer
->slotid
, xfer
->epid
);
1479 uint8_t ep
= xfer
->epid
>>1;
1481 xfer
->in_xfer
= epctx
->type
>>2;
1482 if (xfer
->in_xfer
) {
1486 if (xfer
->data
&& xfer
->data_alloced
< xfer
->data_length
) {
1487 xfer
->data_alloced
= 0;
1491 if (!xfer
->data
&& xfer
->data_length
) {
1492 DPRINTF("xhci: alloc %d bytes data\n", xfer
->data_length
);
1493 xfer
->data
= g_malloc(xfer
->data_length
);
1494 xfer
->data_alloced
= xfer
->data_length
;
1496 if (epctx
->type
== ET_ISO_IN
|| epctx
->type
== ET_ISO_OUT
) {
1497 if (!xfer
->bg_xfer
) {
1504 port
= &xhci
->ports
[xhci
->slots
[xfer
->slotid
-1].port
-1];
1505 dev
= port
->port
.dev
;
1507 fprintf(stderr
, "xhci: slot %d port %d has no device\n", xfer
->slotid
,
1508 xhci
->slots
[xfer
->slotid
-1].port
);
1512 xhci_setup_packet(xfer
, port
, ep
);
1514 switch(epctx
->type
) {
1525 fprintf(stderr
, "xhci: unknown or unhandled EP type %d (ep %02x)\n",
1530 if (!xfer
->in_xfer
) {
1531 xhci_xfer_data(xfer
, xfer
->data
, xfer
->data_length
, 0, 1, 0);
1533 ret
= usb_handle_packet(dev
, &xfer
->packet
);
1535 xhci_complete_packet(xfer
, ret
);
1536 if (!xfer
->running
) {
1537 xhci_kick_ep(xhci
, xfer
->slotid
, xfer
->epid
);
1542 static int xhci_fire_transfer(XHCIState
*xhci
, XHCITransfer
*xfer
, XHCIEPContext
*epctx
)
1545 unsigned int length
= 0;
1548 DPRINTF("xhci_fire_transfer(slotid=%d,epid=%d)\n", xfer
->slotid
, xfer
->epid
);
1550 for (i
= 0; i
< xfer
->trb_count
; i
++) {
1551 trb
= &xfer
->trbs
[i
];
1552 if (TRB_TYPE(*trb
) == TR_NORMAL
|| TRB_TYPE(*trb
) == TR_ISOCH
) {
1553 length
+= trb
->status
& 0x1ffff;
1556 DPRINTF("xhci: total TD length=%d\n", length
);
1558 if (!epctx
->has_bg
) {
1559 xfer
->data_length
= length
;
1560 xfer
->backgrounded
= 0;
1561 return xhci_submit(xhci
, xfer
, epctx
);
1563 if (!epctx
->bg_running
) {
1564 for (i
= 0; i
< BG_XFERS
; i
++) {
1565 XHCITransfer
*t
= &epctx
->bg_transfers
[i
];
1567 t
->epid
= xfer
->epid
;
1568 t
->slotid
= xfer
->slotid
;
1570 t
->pktsize
= epctx
->max_psize
;
1571 t
->data_length
= t
->pkts
* t
->pktsize
;
1573 if (xhci_submit(xhci
, t
, epctx
) < 0) {
1574 fprintf(stderr
, "xhci: bg submit failed\n");
1578 epctx
->bg_running
= 1;
1580 xfer
->backgrounded
= 1;
1581 xhci_bg_update(xhci
, epctx
);
1586 static void xhci_kick_ep(XHCIState
*xhci
, unsigned int slotid
, unsigned int epid
)
1588 XHCIEPContext
*epctx
;
1592 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1593 assert(epid
>= 1 && epid
<= 31);
1594 DPRINTF("xhci_kick_ep(%d, %d)\n", slotid
, epid
);
1596 if (!xhci
->slots
[slotid
-1].enabled
) {
1597 fprintf(stderr
, "xhci: xhci_kick_ep for disabled slot %d\n", slotid
);
1600 epctx
= xhci
->slots
[slotid
-1].eps
[epid
-1];
1602 fprintf(stderr
, "xhci: xhci_kick_ep for disabled endpoint %d,%d\n",
1607 if (epctx
->state
== EP_HALTED
) {
1608 DPRINTF("xhci: ep halted, not running schedule\n");
1612 xhci_set_ep_state(xhci
, epctx
, EP_RUNNING
);
1615 XHCITransfer
*xfer
= &epctx
->transfers
[epctx
->next_xfer
];
1616 if (xfer
->running
|| xfer
->backgrounded
) {
1617 DPRINTF("xhci: ep is busy\n");
1620 length
= xhci_ring_chain_length(xhci
, &epctx
->ring
);
1622 DPRINTF("xhci: incomplete TD (%d TRBs)\n", -length
);
1624 } else if (length
== 0) {
1627 DPRINTF("xhci: fetching %d-TRB TD\n", length
);
1628 if (xfer
->trbs
&& xfer
->trb_alloced
< length
) {
1629 xfer
->trb_count
= 0;
1630 xfer
->trb_alloced
= 0;
1635 xfer
->trbs
= g_malloc(sizeof(XHCITRB
) * length
);
1636 xfer
->trb_alloced
= length
;
1638 xfer
->trb_count
= length
;
1640 for (i
= 0; i
< length
; i
++) {
1641 assert(xhci_ring_fetch(xhci
, &epctx
->ring
, &xfer
->trbs
[i
], NULL
));
1645 xfer
->slotid
= slotid
;
1648 if (xhci_fire_ctl_transfer(xhci
, xfer
) >= 0) {
1649 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
1651 fprintf(stderr
, "xhci: error firing CTL transfer\n");
1654 if (xhci_fire_transfer(xhci
, xfer
, epctx
) >= 0) {
1655 epctx
->next_xfer
= (epctx
->next_xfer
+ 1) % TD_QUEUE
;
1657 fprintf(stderr
, "xhci: error firing data transfer\n");
1662 * Qemu usb can't handle multiple in-flight xfers.
1663 * Also xfers might be finished here already,
1664 * possibly with an error. Stop here for now.
1670 static TRBCCode
xhci_enable_slot(XHCIState
*xhci
, unsigned int slotid
)
1672 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1673 DPRINTF("xhci_enable_slot(%d)\n", slotid
);
1674 xhci
->slots
[slotid
-1].enabled
= 1;
1675 xhci
->slots
[slotid
-1].port
= 0;
1676 memset(xhci
->slots
[slotid
-1].eps
, 0, sizeof(XHCIEPContext
*)*31);
1681 static TRBCCode
xhci_disable_slot(XHCIState
*xhci
, unsigned int slotid
)
1685 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1686 DPRINTF("xhci_disable_slot(%d)\n", slotid
);
1688 for (i
= 1; i
<= 31; i
++) {
1689 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1690 xhci_disable_ep(xhci
, slotid
, i
);
1694 xhci
->slots
[slotid
-1].enabled
= 0;
1698 static TRBCCode
xhci_address_slot(XHCIState
*xhci
, unsigned int slotid
,
1699 uint64_t pictx
, bool bsr
)
1703 target_phys_addr_t ictx
, octx
, dcbaap
;
1705 uint32_t ictl_ctx
[2];
1706 uint32_t slot_ctx
[4];
1707 uint32_t ep0_ctx
[5];
1712 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1713 DPRINTF("xhci_address_slot(%d)\n", slotid
);
1715 dcbaap
= xhci_addr64(xhci
->dcbaap_low
, xhci
->dcbaap_high
);
1716 cpu_physical_memory_read(dcbaap
+ 8*slotid
,
1717 (uint8_t *) &poctx
, sizeof(poctx
));
1718 ictx
= xhci_mask64(pictx
);
1719 octx
= xhci_mask64(le64_to_cpu(poctx
));
1721 DPRINTF("xhci: input context at "TARGET_FMT_plx
"\n", ictx
);
1722 DPRINTF("xhci: output context at "TARGET_FMT_plx
"\n", octx
);
1724 cpu_physical_memory_read(ictx
, (uint8_t *) ictl_ctx
, sizeof(ictl_ctx
));
1726 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] != 0x3) {
1727 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1728 ictl_ctx
[0], ictl_ctx
[1]);
1729 return CC_TRB_ERROR
;
1732 cpu_physical_memory_read(ictx
+32, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1733 cpu_physical_memory_read(ictx
+64, (uint8_t *) ep0_ctx
, sizeof(ep0_ctx
));
1735 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
1736 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1738 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
1739 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1741 port
= (slot_ctx
[1]>>16) & 0xFF;
1742 dev
= xhci
->ports
[port
-1].port
.dev
;
1744 if (port
< 1 || port
> MAXPORTS
) {
1745 fprintf(stderr
, "xhci: bad port %d\n", port
);
1746 return CC_TRB_ERROR
;
1748 fprintf(stderr
, "xhci: port %d not connected\n", port
);
1749 return CC_USB_TRANSACTION_ERROR
;
1752 for (i
= 0; i
< MAXSLOTS
; i
++) {
1753 if (xhci
->slots
[i
].port
== port
) {
1754 fprintf(stderr
, "xhci: port %d already assigned to slot %d\n",
1756 return CC_TRB_ERROR
;
1760 slot
= &xhci
->slots
[slotid
-1];
1765 slot_ctx
[3] = SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
1767 slot
->devaddr
= xhci
->devaddr
++;
1768 slot_ctx
[3] = (SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
) | slot
->devaddr
;
1769 DPRINTF("xhci: device address is %d\n", slot
->devaddr
);
1770 usb_device_handle_control(dev
, NULL
,
1771 DeviceOutRequest
| USB_REQ_SET_ADDRESS
,
1772 slot
->devaddr
, 0, 0, NULL
);
1775 res
= xhci_enable_ep(xhci
, slotid
, 1, octx
+32, ep0_ctx
);
1777 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1778 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1779 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
1780 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1782 cpu_physical_memory_write(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1783 cpu_physical_memory_write(octx
+32, (uint8_t *) ep0_ctx
, sizeof(ep0_ctx
));
1789 static TRBCCode
xhci_configure_slot(XHCIState
*xhci
, unsigned int slotid
,
1790 uint64_t pictx
, bool dc
)
1792 target_phys_addr_t ictx
, octx
;
1793 uint32_t ictl_ctx
[2];
1794 uint32_t slot_ctx
[4];
1795 uint32_t islot_ctx
[4];
1800 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1801 DPRINTF("xhci_configure_slot(%d)\n", slotid
);
1803 ictx
= xhci_mask64(pictx
);
1804 octx
= xhci
->slots
[slotid
-1].ctx
;
1806 DPRINTF("xhci: input context at "TARGET_FMT_plx
"\n", ictx
);
1807 DPRINTF("xhci: output context at "TARGET_FMT_plx
"\n", octx
);
1810 for (i
= 2; i
<= 31; i
++) {
1811 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1812 xhci_disable_ep(xhci
, slotid
, i
);
1816 cpu_physical_memory_read(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1817 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1818 slot_ctx
[3] |= SLOT_ADDRESSED
<< SLOT_STATE_SHIFT
;
1819 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1820 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1821 cpu_physical_memory_write(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1826 cpu_physical_memory_read(ictx
, (uint8_t *) ictl_ctx
, sizeof(ictl_ctx
));
1828 if ((ictl_ctx
[0] & 0x3) != 0x0 || (ictl_ctx
[1] & 0x3) != 0x1) {
1829 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1830 ictl_ctx
[0], ictl_ctx
[1]);
1831 return CC_TRB_ERROR
;
1834 cpu_physical_memory_read(ictx
+32, (uint8_t *) islot_ctx
, sizeof(islot_ctx
));
1835 cpu_physical_memory_read(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1837 if (SLOT_STATE(slot_ctx
[3]) < SLOT_ADDRESSED
) {
1838 fprintf(stderr
, "xhci: invalid slot state %08x\n", slot_ctx
[3]);
1839 return CC_CONTEXT_STATE_ERROR
;
1842 for (i
= 2; i
<= 31; i
++) {
1843 if (ictl_ctx
[0] & (1<<i
)) {
1844 xhci_disable_ep(xhci
, slotid
, i
);
1846 if (ictl_ctx
[1] & (1<<i
)) {
1847 cpu_physical_memory_read(ictx
+32+(32*i
),
1848 (uint8_t *) ep_ctx
, sizeof(ep_ctx
));
1849 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n",
1850 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
1851 ep_ctx
[3], ep_ctx
[4]);
1852 xhci_disable_ep(xhci
, slotid
, i
);
1853 res
= xhci_enable_ep(xhci
, slotid
, i
, octx
+(32*i
), ep_ctx
);
1854 if (res
!= CC_SUCCESS
) {
1857 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n",
1858 i
/2, i
%2, ep_ctx
[0], ep_ctx
[1], ep_ctx
[2],
1859 ep_ctx
[3], ep_ctx
[4]);
1860 cpu_physical_memory_write(octx
+(32*i
),
1861 (uint8_t *) ep_ctx
, sizeof(ep_ctx
));
1865 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1866 slot_ctx
[3] |= SLOT_CONFIGURED
<< SLOT_STATE_SHIFT
;
1867 slot_ctx
[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK
<< SLOT_CONTEXT_ENTRIES_SHIFT
);
1868 slot_ctx
[0] |= islot_ctx
[0] & (SLOT_CONTEXT_ENTRIES_MASK
<<
1869 SLOT_CONTEXT_ENTRIES_SHIFT
);
1870 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1871 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1873 cpu_physical_memory_write(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1879 static TRBCCode
xhci_evaluate_slot(XHCIState
*xhci
, unsigned int slotid
,
1882 target_phys_addr_t ictx
, octx
;
1883 uint32_t ictl_ctx
[2];
1884 uint32_t iep0_ctx
[5];
1885 uint32_t ep0_ctx
[5];
1886 uint32_t islot_ctx
[4];
1887 uint32_t slot_ctx
[4];
1889 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1890 DPRINTF("xhci_evaluate_slot(%d)\n", slotid
);
1892 ictx
= xhci_mask64(pictx
);
1893 octx
= xhci
->slots
[slotid
-1].ctx
;
1895 DPRINTF("xhci: input context at "TARGET_FMT_plx
"\n", ictx
);
1896 DPRINTF("xhci: output context at "TARGET_FMT_plx
"\n", octx
);
1898 cpu_physical_memory_read(ictx
, (uint8_t *) ictl_ctx
, sizeof(ictl_ctx
));
1900 if (ictl_ctx
[0] != 0x0 || ictl_ctx
[1] & ~0x3) {
1901 fprintf(stderr
, "xhci: invalid input context control %08x %08x\n",
1902 ictl_ctx
[0], ictl_ctx
[1]);
1903 return CC_TRB_ERROR
;
1906 if (ictl_ctx
[1] & 0x1) {
1907 cpu_physical_memory_read(ictx
+32,
1908 (uint8_t *) islot_ctx
, sizeof(islot_ctx
));
1910 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n",
1911 islot_ctx
[0], islot_ctx
[1], islot_ctx
[2], islot_ctx
[3]);
1913 cpu_physical_memory_read(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1915 slot_ctx
[1] &= ~0xFFFF; /* max exit latency */
1916 slot_ctx
[1] |= islot_ctx
[1] & 0xFFFF;
1917 slot_ctx
[2] &= ~0xFF00000; /* interrupter target */
1918 slot_ctx
[2] |= islot_ctx
[2] & 0xFF000000;
1920 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1921 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1923 cpu_physical_memory_write(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1926 if (ictl_ctx
[1] & 0x2) {
1927 cpu_physical_memory_read(ictx
+64,
1928 (uint8_t *) iep0_ctx
, sizeof(iep0_ctx
));
1930 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n",
1931 iep0_ctx
[0], iep0_ctx
[1], iep0_ctx
[2],
1932 iep0_ctx
[3], iep0_ctx
[4]);
1934 cpu_physical_memory_read(octx
+32, (uint8_t *) ep0_ctx
, sizeof(ep0_ctx
));
1936 ep0_ctx
[1] &= ~0xFFFF0000; /* max packet size*/
1937 ep0_ctx
[1] |= iep0_ctx
[1] & 0xFFFF0000;
1939 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n",
1940 ep0_ctx
[0], ep0_ctx
[1], ep0_ctx
[2], ep0_ctx
[3], ep0_ctx
[4]);
1942 cpu_physical_memory_write(octx
+32,
1943 (uint8_t *) ep0_ctx
, sizeof(ep0_ctx
));
1949 static TRBCCode
xhci_reset_slot(XHCIState
*xhci
, unsigned int slotid
)
1951 uint32_t slot_ctx
[4];
1952 target_phys_addr_t octx
;
1955 assert(slotid
>= 1 && slotid
<= MAXSLOTS
);
1956 DPRINTF("xhci_reset_slot(%d)\n", slotid
);
1958 octx
= xhci
->slots
[slotid
-1].ctx
;
1960 DPRINTF("xhci: output context at "TARGET_FMT_plx
"\n", octx
);
1962 for (i
= 2; i
<= 31; i
++) {
1963 if (xhci
->slots
[slotid
-1].eps
[i
-1]) {
1964 xhci_disable_ep(xhci
, slotid
, i
);
1968 cpu_physical_memory_read(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1969 slot_ctx
[3] &= ~(SLOT_STATE_MASK
<< SLOT_STATE_SHIFT
);
1970 slot_ctx
[3] |= SLOT_DEFAULT
<< SLOT_STATE_SHIFT
;
1971 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n",
1972 slot_ctx
[0], slot_ctx
[1], slot_ctx
[2], slot_ctx
[3]);
1973 cpu_physical_memory_write(octx
, (uint8_t *) slot_ctx
, sizeof(slot_ctx
));
1978 static unsigned int xhci_get_slot(XHCIState
*xhci
, XHCIEvent
*event
, XHCITRB
*trb
)
1980 unsigned int slotid
;
1981 slotid
= (trb
->control
>> TRB_CR_SLOTID_SHIFT
) & TRB_CR_SLOTID_MASK
;
1982 if (slotid
< 1 || slotid
> MAXSLOTS
) {
1983 fprintf(stderr
, "xhci: bad slot id %d\n", slotid
);
1984 event
->ccode
= CC_TRB_ERROR
;
1986 } else if (!xhci
->slots
[slotid
-1].enabled
) {
1987 fprintf(stderr
, "xhci: slot id %d not enabled\n", slotid
);
1988 event
->ccode
= CC_SLOT_NOT_ENABLED_ERROR
;
1994 static TRBCCode
xhci_get_port_bandwidth(XHCIState
*xhci
, uint64_t pctx
)
1996 target_phys_addr_t ctx
;
1997 uint8_t bw_ctx
[MAXPORTS
+1];
1999 DPRINTF("xhci_get_port_bandwidth()\n");
2001 ctx
= xhci_mask64(pctx
);
2003 DPRINTF("xhci: bandwidth context at "TARGET_FMT_plx
"\n", ctx
);
2005 /* TODO: actually implement real values here */
2007 memset(&bw_ctx
[1], 80, MAXPORTS
); /* 80% */
2008 cpu_physical_memory_write(ctx
, bw_ctx
, sizeof(bw_ctx
));
2013 static uint32_t rotl(uint32_t v
, unsigned count
)
2016 return (v
<< count
) | (v
>> (32 - count
));
2020 static uint32_t xhci_nec_challenge(uint32_t hi
, uint32_t lo
)
2023 val
= rotl(lo
- 0x49434878, 32 - ((hi
>>8) & 0x1F));
2024 val
+= rotl(lo
+ 0x49434878, hi
& 0x1F);
2025 val
-= rotl(hi
^ 0x49434878, (lo
>> 16) & 0x1F);
2029 static void xhci_via_challenge(uint64_t addr
)
2033 target_phys_addr_t paddr
= xhci_mask64(addr
);
2035 cpu_physical_memory_read(paddr
, (uint8_t *) &buf
, 32);
2037 memcpy(obuf
, buf
, sizeof(obuf
));
2039 if ((buf
[0] & 0xff) == 2) {
2040 obuf
[0] = 0x49932000 + 0x54dc200 * buf
[2] + 0x7429b578 * buf
[3];
2041 obuf
[0] |= (buf
[2] * buf
[3]) & 0xff;
2042 obuf
[1] = 0x0132bb37 + 0xe89 * buf
[2] + 0xf09 * buf
[3];
2043 obuf
[2] = 0x0066c2e9 + 0x2091 * buf
[2] + 0x19bd * buf
[3];
2044 obuf
[3] = 0xd5281342 + 0x2cc9691 * buf
[2] + 0x2367662 * buf
[3];
2045 obuf
[4] = 0x0123c75c + 0x1595 * buf
[2] + 0x19ec * buf
[3];
2046 obuf
[5] = 0x00f695de + 0x26fd * buf
[2] + 0x3e9 * buf
[3];
2047 obuf
[6] = obuf
[2] ^ obuf
[3] ^ 0x29472956;
2048 obuf
[7] = obuf
[2] ^ obuf
[3] ^ 0x65866593;
2051 cpu_physical_memory_write(paddr
, (uint8_t *) &obuf
, 32);
2054 static void xhci_process_commands(XHCIState
*xhci
)
2058 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_SUCCESS
};
2059 target_phys_addr_t addr
;
2060 unsigned int i
, slotid
= 0;
2062 DPRINTF("xhci_process_commands()\n");
2063 if (!xhci_running(xhci
)) {
2064 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n");
2068 xhci
->crcr_low
|= CRCR_CRR
;
2070 while ((type
= xhci_ring_fetch(xhci
, &xhci
->cmd_ring
, &trb
, &addr
))) {
2073 case CR_ENABLE_SLOT
:
2074 for (i
= 0; i
< MAXSLOTS
; i
++) {
2075 if (!xhci
->slots
[i
].enabled
) {
2079 if (i
>= MAXSLOTS
) {
2080 fprintf(stderr
, "xhci: no device slots available\n");
2081 event
.ccode
= CC_NO_SLOTS_ERROR
;
2084 event
.ccode
= xhci_enable_slot(xhci
, slotid
);
2087 case CR_DISABLE_SLOT
:
2088 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2090 event
.ccode
= xhci_disable_slot(xhci
, slotid
);
2093 case CR_ADDRESS_DEVICE
:
2094 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2096 event
.ccode
= xhci_address_slot(xhci
, slotid
, trb
.parameter
,
2097 trb
.control
& TRB_CR_BSR
);
2100 case CR_CONFIGURE_ENDPOINT
:
2101 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2103 event
.ccode
= xhci_configure_slot(xhci
, slotid
, trb
.parameter
,
2104 trb
.control
& TRB_CR_DC
);
2107 case CR_EVALUATE_CONTEXT
:
2108 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2110 event
.ccode
= xhci_evaluate_slot(xhci
, slotid
, trb
.parameter
);
2113 case CR_STOP_ENDPOINT
:
2114 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2116 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2118 event
.ccode
= xhci_stop_ep(xhci
, slotid
, epid
);
2121 case CR_RESET_ENDPOINT
:
2122 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2124 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2126 event
.ccode
= xhci_reset_ep(xhci
, slotid
, epid
);
2129 case CR_SET_TR_DEQUEUE
:
2130 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2132 unsigned int epid
= (trb
.control
>> TRB_CR_EPID_SHIFT
)
2134 event
.ccode
= xhci_set_ep_dequeue(xhci
, slotid
, epid
,
2138 case CR_RESET_DEVICE
:
2139 slotid
= xhci_get_slot(xhci
, &event
, &trb
);
2141 event
.ccode
= xhci_reset_slot(xhci
, slotid
);
2144 case CR_GET_PORT_BANDWIDTH
:
2145 event
.ccode
= xhci_get_port_bandwidth(xhci
, trb
.parameter
);
2147 case CR_VENDOR_VIA_CHALLENGE_RESPONSE
:
2148 xhci_via_challenge(trb
.parameter
);
2150 case CR_VENDOR_NEC_FIRMWARE_REVISION
:
2151 event
.type
= 48; /* NEC reply */
2152 event
.length
= 0x3025;
2154 case CR_VENDOR_NEC_CHALLENGE_RESPONSE
:
2156 uint32_t chi
= trb
.parameter
>> 32;
2157 uint32_t clo
= trb
.parameter
;
2158 uint32_t val
= xhci_nec_challenge(chi
, clo
);
2159 event
.length
= val
& 0xFFFF;
2160 event
.epid
= val
>> 16;
2162 event
.type
= 48; /* NEC reply */
2166 fprintf(stderr
, "xhci: unimplemented command %d\n", type
);
2167 event
.ccode
= CC_TRB_ERROR
;
2170 event
.slotid
= slotid
;
2171 xhci_event(xhci
, &event
);
2175 static void xhci_update_port(XHCIState
*xhci
, XHCIPort
*port
, int is_detach
)
2177 int nr
= port
->port
.index
+ 1;
2179 port
->portsc
= PORTSC_PP
;
2180 if (port
->port
.dev
&& !is_detach
) {
2181 port
->portsc
|= PORTSC_CCS
;
2182 switch (port
->port
.dev
->speed
) {
2184 port
->portsc
|= PORTSC_SPEED_LOW
;
2186 case USB_SPEED_FULL
:
2187 port
->portsc
|= PORTSC_SPEED_FULL
;
2189 case USB_SPEED_HIGH
:
2190 port
->portsc
|= PORTSC_SPEED_HIGH
;
2195 if (xhci_running(xhci
)) {
2196 port
->portsc
|= PORTSC_CSC
;
2197 XHCIEvent ev
= { ER_PORT_STATUS_CHANGE
, CC_SUCCESS
, nr
<< 24};
2198 xhci_event(xhci
, &ev
);
2199 DPRINTF("xhci: port change event for port %d\n", nr
);
2203 static void xhci_reset(void *opaque
)
2205 XHCIState
*xhci
= opaque
;
2208 DPRINTF("xhci: full reset\n");
2209 if (!(xhci
->usbsts
& USBSTS_HCH
)) {
2210 fprintf(stderr
, "xhci: reset while running!\n");
2214 xhci
->usbsts
= USBSTS_HCH
;
2217 xhci
->crcr_high
= 0;
2218 xhci
->dcbaap_low
= 0;
2219 xhci
->dcbaap_high
= 0;
2223 for (i
= 0; i
< MAXSLOTS
; i
++) {
2224 xhci_disable_slot(xhci
, i
+1);
2227 for (i
= 0; i
< MAXPORTS
; i
++) {
2228 xhci_update_port(xhci
, xhci
->ports
+ i
, 0);
2235 xhci
->erstba_low
= 0;
2236 xhci
->erstba_high
= 0;
2238 xhci
->erdp_high
= 0;
2240 xhci
->er_ep_idx
= 0;
2243 xhci
->ev_buffer_put
= 0;
2244 xhci
->ev_buffer_get
= 0;
2247 static uint32_t xhci_cap_read(XHCIState
*xhci
, uint32_t reg
)
2249 DPRINTF("xhci_cap_read(0x%x)\n", reg
);
2252 case 0x00: /* HCIVERSION, CAPLENGTH */
2253 return 0x01000000 | LEN_CAP
;
2254 case 0x04: /* HCSPARAMS 1 */
2255 return (MAXPORTS
<<24) | (MAXINTRS
<<8) | MAXSLOTS
;
2256 case 0x08: /* HCSPARAMS 2 */
2258 case 0x0c: /* HCSPARAMS 3 */
2260 case 0x10: /* HCCPARAMS */
2261 #if TARGET_PHYS_ADDR_BITS > 32
2266 case 0x14: /* DBOFF */
2267 return OFF_DOORBELL
;
2268 case 0x18: /* RTSOFF */
2271 /* extended capabilities */
2272 case 0x20: /* Supported Protocol:00 */
2274 return 0x02000402; /* USB 2.0 */
2276 return 0x02000002; /* USB 2.0 */
2278 case 0x24: /* Supported Protocol:04 */
2279 return 0x20425455; /* "USB " */
2280 case 0x28: /* Supported Protocol:08 */
2281 return 0x00000001 | (USB2_PORTS
<<8);
2282 case 0x2c: /* Supported Protocol:0c */
2283 return 0x00000000; /* reserved */
2285 case 0x30: /* Supported Protocol:00 */
2286 return 0x03000002; /* USB 3.0 */
2287 case 0x34: /* Supported Protocol:04 */
2288 return 0x20425455; /* "USB " */
2289 case 0x38: /* Supported Protocol:08 */
2290 return 0x00000000 | (USB2_PORTS
+1) | (USB3_PORTS
<<8);
2291 case 0x3c: /* Supported Protocol:0c */
2292 return 0x00000000; /* reserved */
2295 fprintf(stderr
, "xhci_cap_read: reg %d unimplemented\n", reg
);
2300 static uint32_t xhci_port_read(XHCIState
*xhci
, uint32_t reg
)
2302 uint32_t port
= reg
>> 4;
2303 if (port
>= MAXPORTS
) {
2304 fprintf(stderr
, "xhci_port_read: port %d out of bounds\n", port
);
2308 switch (reg
& 0xf) {
2309 case 0x00: /* PORTSC */
2310 return xhci
->ports
[port
].portsc
;
2311 case 0x04: /* PORTPMSC */
2312 case 0x08: /* PORTLI */
2314 case 0x0c: /* reserved */
2316 fprintf(stderr
, "xhci_port_read (port %d): reg 0x%x unimplemented\n",
2322 static void xhci_port_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2324 uint32_t port
= reg
>> 4;
2327 if (port
>= MAXPORTS
) {
2328 fprintf(stderr
, "xhci_port_read: port %d out of bounds\n", port
);
2332 switch (reg
& 0xf) {
2333 case 0x00: /* PORTSC */
2334 portsc
= xhci
->ports
[port
].portsc
;
2335 /* write-1-to-clear bits*/
2336 portsc
&= ~(val
& (PORTSC_CSC
|PORTSC_PEC
|PORTSC_WRC
|PORTSC_OCC
|
2337 PORTSC_PRC
|PORTSC_PLC
|PORTSC_CEC
));
2338 if (val
& PORTSC_LWS
) {
2339 /* overwrite PLS only when LWS=1 */
2340 portsc
&= ~(PORTSC_PLS_MASK
<< PORTSC_PLS_SHIFT
);
2341 portsc
|= val
& (PORTSC_PLS_MASK
<< PORTSC_PLS_SHIFT
);
2343 /* read/write bits */
2344 portsc
&= ~(PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
);
2345 portsc
|= (val
& (PORTSC_PP
|PORTSC_WCE
|PORTSC_WDE
|PORTSC_WOE
));
2346 /* write-1-to-start bits */
2347 if (val
& PORTSC_PR
) {
2348 DPRINTF("xhci: port %d reset\n", port
);
2349 if (xhci
->ports
[port
].port
.dev
) {
2350 usb_send_msg(xhci
->ports
[port
].port
.dev
, USB_MSG_RESET
);
2352 portsc
|= PORTSC_PRC
| PORTSC_PED
;
2354 xhci
->ports
[port
].portsc
= portsc
;
2356 case 0x04: /* PORTPMSC */
2357 case 0x08: /* PORTLI */
2359 fprintf(stderr
, "xhci_port_write (port %d): reg 0x%x unimplemented\n",
2364 static uint32_t xhci_oper_read(XHCIState
*xhci
, uint32_t reg
)
2366 DPRINTF("xhci_oper_read(0x%x)\n", reg
);
2369 return xhci_port_read(xhci
, reg
- 0x400);
2373 case 0x00: /* USBCMD */
2374 return xhci
->usbcmd
;
2375 case 0x04: /* USBSTS */
2376 return xhci
->usbsts
;
2377 case 0x08: /* PAGESIZE */
2378 return 1; /* 4KiB */
2379 case 0x14: /* DNCTRL */
2380 return xhci
->dnctrl
;
2381 case 0x18: /* CRCR low */
2382 return xhci
->crcr_low
& ~0xe;
2383 case 0x1c: /* CRCR high */
2384 return xhci
->crcr_high
;
2385 case 0x30: /* DCBAAP low */
2386 return xhci
->dcbaap_low
;
2387 case 0x34: /* DCBAAP high */
2388 return xhci
->dcbaap_high
;
2389 case 0x38: /* CONFIG */
2390 return xhci
->config
;
2392 fprintf(stderr
, "xhci_oper_read: reg 0x%x unimplemented\n", reg
);
2397 static void xhci_oper_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2399 DPRINTF("xhci_oper_write(0x%x, 0x%08x)\n", reg
, val
);
2402 xhci_port_write(xhci
, reg
- 0x400, val
);
2407 case 0x00: /* USBCMD */
2408 if ((val
& USBCMD_RS
) && !(xhci
->usbcmd
& USBCMD_RS
)) {
2410 } else if (!(val
& USBCMD_RS
) && (xhci
->usbcmd
& USBCMD_RS
)) {
2413 xhci
->usbcmd
= val
& 0xc0f;
2414 if (val
& USBCMD_HCRST
) {
2417 xhci_irq_update(xhci
);
2420 case 0x04: /* USBSTS */
2421 /* these bits are write-1-to-clear */
2422 xhci
->usbsts
&= ~(val
& (USBSTS_HSE
|USBSTS_EINT
|USBSTS_PCD
|USBSTS_SRE
));
2423 xhci_irq_update(xhci
);
2426 case 0x14: /* DNCTRL */
2427 xhci
->dnctrl
= val
& 0xffff;
2429 case 0x18: /* CRCR low */
2430 xhci
->crcr_low
= (val
& 0xffffffcf) | (xhci
->crcr_low
& CRCR_CRR
);
2432 case 0x1c: /* CRCR high */
2433 xhci
->crcr_high
= val
;
2434 if (xhci
->crcr_low
& (CRCR_CA
|CRCR_CS
) && (xhci
->crcr_low
& CRCR_CRR
)) {
2435 XHCIEvent event
= {ER_COMMAND_COMPLETE
, CC_COMMAND_RING_STOPPED
};
2436 xhci
->crcr_low
&= ~CRCR_CRR
;
2437 xhci_event(xhci
, &event
);
2438 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci
->crcr_low
);
2440 target_phys_addr_t base
= xhci_addr64(xhci
->crcr_low
& ~0x3f, val
);
2441 xhci_ring_init(xhci
, &xhci
->cmd_ring
, base
);
2443 xhci
->crcr_low
&= ~(CRCR_CA
| CRCR_CS
);
2445 case 0x30: /* DCBAAP low */
2446 xhci
->dcbaap_low
= val
& 0xffffffc0;
2448 case 0x34: /* DCBAAP high */
2449 xhci
->dcbaap_high
= val
;
2451 case 0x38: /* CONFIG */
2452 xhci
->config
= val
& 0xff;
2455 fprintf(stderr
, "xhci_oper_write: reg 0x%x unimplemented\n", reg
);
2459 static uint32_t xhci_runtime_read(XHCIState
*xhci
, uint32_t reg
)
2461 DPRINTF("xhci_runtime_read(0x%x)\n", reg
);
2464 case 0x00: /* MFINDEX */
2465 fprintf(stderr
, "xhci_runtime_read: MFINDEX not yet implemented\n");
2466 return xhci
->mfindex
;
2467 case 0x20: /* IMAN */
2469 case 0x24: /* IMOD */
2471 case 0x28: /* ERSTSZ */
2472 return xhci
->erstsz
;
2473 case 0x30: /* ERSTBA low */
2474 return xhci
->erstba_low
;
2475 case 0x34: /* ERSTBA high */
2476 return xhci
->erstba_high
;
2477 case 0x38: /* ERDP low */
2478 return xhci
->erdp_low
;
2479 case 0x3c: /* ERDP high */
2480 return xhci
->erdp_high
;
2482 fprintf(stderr
, "xhci_runtime_read: reg 0x%x unimplemented\n", reg
);
2487 static void xhci_runtime_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2489 DPRINTF("xhci_runtime_write(0x%x, 0x%08x)\n", reg
, val
);
2492 case 0x20: /* IMAN */
2493 if (val
& IMAN_IP
) {
2494 xhci
->iman
&= ~IMAN_IP
;
2496 xhci
->iman
&= ~IMAN_IE
;
2497 xhci
->iman
|= val
& IMAN_IE
;
2498 xhci_irq_update(xhci
);
2500 case 0x24: /* IMOD */
2503 case 0x28: /* ERSTSZ */
2504 xhci
->erstsz
= val
& 0xffff;
2506 case 0x30: /* ERSTBA low */
2507 /* XXX NEC driver bug: it doesn't align this to 64 bytes
2508 xhci->erstba_low = val & 0xffffffc0; */
2509 xhci
->erstba_low
= val
& 0xfffffff0;
2511 case 0x34: /* ERSTBA high */
2512 xhci
->erstba_high
= val
;
2513 xhci_er_reset(xhci
);
2515 case 0x38: /* ERDP low */
2516 if (val
& ERDP_EHB
) {
2517 xhci
->erdp_low
&= ~ERDP_EHB
;
2519 xhci
->erdp_low
= (val
& ~ERDP_EHB
) | (xhci
->erdp_low
& ERDP_EHB
);
2521 case 0x3c: /* ERDP high */
2522 xhci
->erdp_high
= val
;
2523 xhci_events_update(xhci
);
2526 fprintf(stderr
, "xhci_oper_write: reg 0x%x unimplemented\n", reg
);
2530 static uint32_t xhci_doorbell_read(XHCIState
*xhci
, uint32_t reg
)
2532 DPRINTF("xhci_doorbell_read(0x%x)\n", reg
);
2533 /* doorbells always read as 0 */
2537 static void xhci_doorbell_write(XHCIState
*xhci
, uint32_t reg
, uint32_t val
)
2539 DPRINTF("xhci_doorbell_write(0x%x, 0x%08x)\n", reg
, val
);
2541 if (!xhci_running(xhci
)) {
2542 fprintf(stderr
, "xhci: wrote doorbell while xHC stopped or paused\n");
2550 xhci_process_commands(xhci
);
2552 fprintf(stderr
, "xhci: bad doorbell 0 write: 0x%x\n", val
);
2555 if (reg
> MAXSLOTS
) {
2556 fprintf(stderr
, "xhci: bad doorbell %d\n", reg
);
2557 } else if (val
> 31) {
2558 fprintf(stderr
, "xhci: bad doorbell %d write: 0x%x\n", reg
, val
);
2560 xhci_kick_ep(xhci
, reg
, val
);
2565 static uint64_t xhci_mem_read(void *ptr
, target_phys_addr_t addr
,
2568 XHCIState
*xhci
= ptr
;
2570 /* Only aligned reads are allowed on xHCI */
2572 fprintf(stderr
, "xhci_mem_read: Mis-aligned read\n");
2576 if (addr
< LEN_CAP
) {
2577 return xhci_cap_read(xhci
, addr
);
2578 } else if (addr
>= OFF_OPER
&& addr
< (OFF_OPER
+ LEN_OPER
)) {
2579 return xhci_oper_read(xhci
, addr
- OFF_OPER
);
2580 } else if (addr
>= OFF_RUNTIME
&& addr
< (OFF_RUNTIME
+ LEN_RUNTIME
)) {
2581 return xhci_runtime_read(xhci
, addr
- OFF_RUNTIME
);
2582 } else if (addr
>= OFF_DOORBELL
&& addr
< (OFF_DOORBELL
+ LEN_DOORBELL
)) {
2583 return xhci_doorbell_read(xhci
, addr
- OFF_DOORBELL
);
2585 fprintf(stderr
, "xhci_mem_read: Bad offset %x\n", (int)addr
);
2590 static void xhci_mem_write(void *ptr
, target_phys_addr_t addr
,
2591 uint64_t val
, unsigned size
)
2593 XHCIState
*xhci
= ptr
;
2595 /* Only aligned writes are allowed on xHCI */
2597 fprintf(stderr
, "xhci_mem_write: Mis-aligned write\n");
2601 if (addr
>= OFF_OPER
&& addr
< (OFF_OPER
+ LEN_OPER
)) {
2602 xhci_oper_write(xhci
, addr
- OFF_OPER
, val
);
2603 } else if (addr
>= OFF_RUNTIME
&& addr
< (OFF_RUNTIME
+ LEN_RUNTIME
)) {
2604 xhci_runtime_write(xhci
, addr
- OFF_RUNTIME
, val
);
2605 } else if (addr
>= OFF_DOORBELL
&& addr
< (OFF_DOORBELL
+ LEN_DOORBELL
)) {
2606 xhci_doorbell_write(xhci
, addr
- OFF_DOORBELL
, val
);
2608 fprintf(stderr
, "xhci_mem_write: Bad offset %x\n", (int)addr
);
2612 static const MemoryRegionOps xhci_mem_ops
= {
2613 .read
= xhci_mem_read
,
2614 .write
= xhci_mem_write
,
2615 .valid
.min_access_size
= 4,
2616 .valid
.max_access_size
= 4,
2617 .endianness
= DEVICE_LITTLE_ENDIAN
,
2620 static void xhci_attach(USBPort
*usbport
)
2622 XHCIState
*xhci
= usbport
->opaque
;
2623 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2625 xhci_update_port(xhci
, port
, 0);
2628 static void xhci_detach(USBPort
*usbport
)
2630 XHCIState
*xhci
= usbport
->opaque
;
2631 XHCIPort
*port
= &xhci
->ports
[usbport
->index
];
2633 xhci_update_port(xhci
, port
, 1);
2636 static void xhci_complete(USBPort
*port
, USBPacket
*packet
)
2638 XHCITransfer
*xfer
= container_of(packet
, XHCITransfer
, packet
);
2640 xhci_complete_packet(xfer
, packet
->result
);
2641 xhci_kick_ep(xfer
->xhci
, xfer
->slotid
, xfer
->epid
);
2644 static void xhci_child_detach(USBPort
*port
, USBDevice
*child
)
2649 static USBPortOps xhci_port_ops
= {
2650 .attach
= xhci_attach
,
2651 .detach
= xhci_detach
,
2652 .complete
= xhci_complete
,
2653 .child_detach
= xhci_child_detach
,
2656 static USBBusOps xhci_bus_ops
= {
2659 static void usb_xhci_init(XHCIState
*xhci
, DeviceState
*dev
)
2663 xhci
->usbsts
= USBSTS_HCH
;
2665 usb_bus_new(&xhci
->bus
, &xhci_bus_ops
, &xhci
->pci_dev
.qdev
);
2667 for (i
= 0; i
< MAXPORTS
; i
++) {
2668 memset(&xhci
->ports
[i
], 0, sizeof(xhci
->ports
[i
]));
2669 usb_register_port(&xhci
->bus
, &xhci
->ports
[i
].port
, xhci
, i
,
2670 &xhci_port_ops
, USB_SPEED_MASK_HIGH
);
2672 for (i
= 0; i
< MAXSLOTS
; i
++) {
2673 xhci
->slots
[i
].enabled
= 0;
2676 qemu_register_reset(xhci_reset
, xhci
);
2679 static int usb_xhci_initfn(struct PCIDevice
*dev
)
2683 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
, dev
);
2685 xhci
->pci_dev
.config
[PCI_CLASS_PROG
] = 0x30; /* xHCI */
2686 xhci
->pci_dev
.config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin 1 */
2687 xhci
->pci_dev
.config
[PCI_CACHE_LINE_SIZE
] = 0x10;
2688 xhci
->pci_dev
.config
[0x60] = 0x30; /* release number */
2690 usb_xhci_init(xhci
, &dev
->qdev
);
2692 xhci
->irq
= xhci
->pci_dev
.irq
[0];
2694 memory_region_init_io(&xhci
->mem
, &xhci_mem_ops
, xhci
,
2696 pci_register_bar(&xhci
->pci_dev
, 0,
2697 PCI_BASE_ADDRESS_SPACE_MEMORY
|PCI_BASE_ADDRESS_MEM_TYPE_64
,
2700 ret
= pcie_cap_init(&xhci
->pci_dev
, 0xa0, PCI_EXP_TYPE_ENDPOINT
, 0);
2704 ret
= msi_init(&xhci
->pci_dev
, 0x70, 1, true, false);
2711 static void xhci_write_config(PCIDevice
*dev
, uint32_t addr
, uint32_t val
,
2714 XHCIState
*xhci
= DO_UPCAST(XHCIState
, pci_dev
, dev
);
2716 pci_default_write_config(dev
, addr
, val
, len
);
2718 msi_write_config(dev
, addr
, val
, len
);
2722 static const VMStateDescription vmstate_xhci
= {
2727 static Property xhci_properties
[] = {
2728 DEFINE_PROP_UINT32("msi", XHCIState
, msi
, 0),
2729 DEFINE_PROP_END_OF_LIST(),
2732 static void xhci_class_init(ObjectClass
*klass
, void *data
)
2734 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2735 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2737 dc
->vmsd
= &vmstate_xhci
;
2738 dc
->props
= xhci_properties
;
2739 k
->init
= usb_xhci_initfn
;
2740 k
->vendor_id
= PCI_VENDOR_ID_NEC
;
2741 k
->device_id
= PCI_DEVICE_ID_NEC_UPD720200
;
2742 k
->class_id
= PCI_CLASS_SERIAL_USB
;
2745 k
->config_write
= xhci_write_config
;
2748 static TypeInfo xhci_info
= {
2749 .name
= "nec-usb-xhci",
2750 .parent
= TYPE_PCI_DEVICE
,
2751 .instance_size
= sizeof(XHCIState
),
2752 .class_init
= xhci_class_init
,
2755 static void xhci_register(void)
2757 type_register_static(&xhci_info
);
2759 device_init(xhci_register
);