1 /* 2 * USB xHCI controller emulation 3 * 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 7 * 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. 12 * 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. 17 * 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/>. 20 */ 21 #include "hw/hw.h" 22 #include "qemu-timer.h" 23 #include "hw/usb.h" 24 #include "hw/pci.h" 25 #include "hw/msi.h" 26 #include "hw/msix.h" 27 #include "trace.h" 28 29 //#define DEBUG_XHCI 30 //#define DEBUG_DATA 31 32 #ifdef DEBUG_XHCI 33 #define DPRINTF(...) fprintf(stderr, __VA_ARGS__) 34 #else 35 #define DPRINTF(...) do {} while (0) 36 #endif 37 #define FIXME() do { fprintf(stderr, "FIXME %s:%d\n", \ 38 __func__, __LINE__); abort(); } while (0) 39 40 #define MAXPORTS_2 15 41 #define MAXPORTS_3 15 42 43 #define MAXPORTS (MAXPORTS_2+MAXPORTS_3) 44 #define MAXSLOTS 64 45 #define MAXINTRS 16 46 47 #define TD_QUEUE 24 48 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 */ 53 #define ER_FULL_HACK 54 55 #define LEN_CAP 0x40 56 #define LEN_OPER (0x400 + 0x10 * MAXPORTS) 57 #define LEN_RUNTIME ((MAXINTRS + 1) * 0x20) 58 #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20) 59 60 #define OFF_OPER LEN_CAP 61 #define OFF_RUNTIME 0x1000 62 #define OFF_DOORBELL 0x2000 63 #define OFF_MSIX_TABLE 0x3000 64 #define OFF_MSIX_PBA 0x3800 65 /* must be power of 2 */ 66 #define LEN_REGS 0x4000 67 68 #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME 69 #error Increase OFF_RUNTIME 70 #endif 71 #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL 72 #error Increase OFF_DOORBELL 73 #endif 74 #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS 75 # error Increase LEN_REGS 76 #endif 77 78 /* bit definitions */ 79 #define USBCMD_RS (1<<0) 80 #define USBCMD_HCRST (1<<1) 81 #define USBCMD_INTE (1<<2) 82 #define USBCMD_HSEE (1<<3) 83 #define USBCMD_LHCRST (1<<7) 84 #define USBCMD_CSS (1<<8) 85 #define USBCMD_CRS (1<<9) 86 #define USBCMD_EWE (1<<10) 87 #define USBCMD_EU3S (1<<11) 88 89 #define USBSTS_HCH (1<<0) 90 #define USBSTS_HSE (1<<2) 91 #define USBSTS_EINT (1<<3) 92 #define USBSTS_PCD (1<<4) 93 #define USBSTS_SSS (1<<8) 94 #define USBSTS_RSS (1<<9) 95 #define USBSTS_SRE (1<<10) 96 #define USBSTS_CNR (1<<11) 97 #define USBSTS_HCE (1<<12) 98 99 100 #define PORTSC_CCS (1<<0) 101 #define PORTSC_PED (1<<1) 102 #define PORTSC_OCA (1<<3) 103 #define PORTSC_PR (1<<4) 104 #define PORTSC_PLS_SHIFT 5 105 #define PORTSC_PLS_MASK 0xf 106 #define PORTSC_PP (1<<9) 107 #define PORTSC_SPEED_SHIFT 10 108 #define PORTSC_SPEED_MASK 0xf 109 #define PORTSC_SPEED_FULL (1<<10) 110 #define PORTSC_SPEED_LOW (2<<10) 111 #define PORTSC_SPEED_HIGH (3<<10) 112 #define PORTSC_SPEED_SUPER (4<<10) 113 #define PORTSC_PIC_SHIFT 14 114 #define PORTSC_PIC_MASK 0x3 115 #define PORTSC_LWS (1<<16) 116 #define PORTSC_CSC (1<<17) 117 #define PORTSC_PEC (1<<18) 118 #define PORTSC_WRC (1<<19) 119 #define PORTSC_OCC (1<<20) 120 #define PORTSC_PRC (1<<21) 121 #define PORTSC_PLC (1<<22) 122 #define PORTSC_CEC (1<<23) 123 #define PORTSC_CAS (1<<24) 124 #define PORTSC_WCE (1<<25) 125 #define PORTSC_WDE (1<<26) 126 #define PORTSC_WOE (1<<27) 127 #define PORTSC_DR (1<<30) 128 #define PORTSC_WPR (1<<31) 129 130 #define CRCR_RCS (1<<0) 131 #define CRCR_CS (1<<1) 132 #define CRCR_CA (1<<2) 133 #define CRCR_CRR (1<<3) 134 135 #define IMAN_IP (1<<0) 136 #define IMAN_IE (1<<1) 137 138 #define ERDP_EHB (1<<3) 139 140 #define TRB_SIZE 16 141 typedef struct XHCITRB { 142 uint64_t parameter; 143 uint32_t status; 144 uint32_t control; 145 dma_addr_t addr; 146 bool ccs; 147 } XHCITRB; 148 149 150 typedef enum TRBType { 151 TRB_RESERVED = 0, 152 TR_NORMAL, 153 TR_SETUP, 154 TR_DATA, 155 TR_STATUS, 156 TR_ISOCH, 157 TR_LINK, 158 TR_EVDATA, 159 TR_NOOP, 160 CR_ENABLE_SLOT, 161 CR_DISABLE_SLOT, 162 CR_ADDRESS_DEVICE, 163 CR_CONFIGURE_ENDPOINT, 164 CR_EVALUATE_CONTEXT, 165 CR_RESET_ENDPOINT, 166 CR_STOP_ENDPOINT, 167 CR_SET_TR_DEQUEUE, 168 CR_RESET_DEVICE, 169 CR_FORCE_EVENT, 170 CR_NEGOTIATE_BW, 171 CR_SET_LATENCY_TOLERANCE, 172 CR_GET_PORT_BANDWIDTH, 173 CR_FORCE_HEADER, 174 CR_NOOP, 175 ER_TRANSFER = 32, 176 ER_COMMAND_COMPLETE, 177 ER_PORT_STATUS_CHANGE, 178 ER_BANDWIDTH_REQUEST, 179 ER_DOORBELL, 180 ER_HOST_CONTROLLER, 181 ER_DEVICE_NOTIFICATION, 182 ER_MFINDEX_WRAP, 183 /* vendor specific bits */ 184 CR_VENDOR_VIA_CHALLENGE_RESPONSE = 48, 185 CR_VENDOR_NEC_FIRMWARE_REVISION = 49, 186 CR_VENDOR_NEC_CHALLENGE_RESPONSE = 50, 187 } TRBType; 188 189 #define CR_LINK TR_LINK 190 191 typedef enum TRBCCode { 192 CC_INVALID = 0, 193 CC_SUCCESS, 194 CC_DATA_BUFFER_ERROR, 195 CC_BABBLE_DETECTED, 196 CC_USB_TRANSACTION_ERROR, 197 CC_TRB_ERROR, 198 CC_STALL_ERROR, 199 CC_RESOURCE_ERROR, 200 CC_BANDWIDTH_ERROR, 201 CC_NO_SLOTS_ERROR, 202 CC_INVALID_STREAM_TYPE_ERROR, 203 CC_SLOT_NOT_ENABLED_ERROR, 204 CC_EP_NOT_ENABLED_ERROR, 205 CC_SHORT_PACKET, 206 CC_RING_UNDERRUN, 207 CC_RING_OVERRUN, 208 CC_VF_ER_FULL, 209 CC_PARAMETER_ERROR, 210 CC_BANDWIDTH_OVERRUN, 211 CC_CONTEXT_STATE_ERROR, 212 CC_NO_PING_RESPONSE_ERROR, 213 CC_EVENT_RING_FULL_ERROR, 214 CC_INCOMPATIBLE_DEVICE_ERROR, 215 CC_MISSED_SERVICE_ERROR, 216 CC_COMMAND_RING_STOPPED, 217 CC_COMMAND_ABORTED, 218 CC_STOPPED, 219 CC_STOPPED_LENGTH_INVALID, 220 CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29, 221 CC_ISOCH_BUFFER_OVERRUN = 31, 222 CC_EVENT_LOST_ERROR, 223 CC_UNDEFINED_ERROR, 224 CC_INVALID_STREAM_ID_ERROR, 225 CC_SECONDARY_BANDWIDTH_ERROR, 226 CC_SPLIT_TRANSACTION_ERROR 227 } TRBCCode; 228 229 #define TRB_C (1<<0) 230 #define TRB_TYPE_SHIFT 10 231 #define TRB_TYPE_MASK 0x3f 232 #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK) 233 234 #define TRB_EV_ED (1<<2) 235 236 #define TRB_TR_ENT (1<<1) 237 #define TRB_TR_ISP (1<<2) 238 #define TRB_TR_NS (1<<3) 239 #define TRB_TR_CH (1<<4) 240 #define TRB_TR_IOC (1<<5) 241 #define TRB_TR_IDT (1<<6) 242 #define TRB_TR_TBC_SHIFT 7 243 #define TRB_TR_TBC_MASK 0x3 244 #define TRB_TR_BEI (1<<9) 245 #define TRB_TR_TLBPC_SHIFT 16 246 #define TRB_TR_TLBPC_MASK 0xf 247 #define TRB_TR_FRAMEID_SHIFT 20 248 #define TRB_TR_FRAMEID_MASK 0x7ff 249 #define TRB_TR_SIA (1<<31) 250 251 #define TRB_TR_DIR (1<<16) 252 253 #define TRB_CR_SLOTID_SHIFT 24 254 #define TRB_CR_SLOTID_MASK 0xff 255 #define TRB_CR_EPID_SHIFT 16 256 #define TRB_CR_EPID_MASK 0x1f 257 258 #define TRB_CR_BSR (1<<9) 259 #define TRB_CR_DC (1<<9) 260 261 #define TRB_LK_TC (1<<1) 262 263 #define TRB_INTR_SHIFT 22 264 #define TRB_INTR_MASK 0x3ff 265 #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK) 266 267 #define EP_TYPE_MASK 0x7 268 #define EP_TYPE_SHIFT 3 269 270 #define EP_STATE_MASK 0x7 271 #define EP_DISABLED (0<<0) 272 #define EP_RUNNING (1<<0) 273 #define EP_HALTED (2<<0) 274 #define EP_STOPPED (3<<0) 275 #define EP_ERROR (4<<0) 276 277 #define SLOT_STATE_MASK 0x1f 278 #define SLOT_STATE_SHIFT 27 279 #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK) 280 #define SLOT_ENABLED 0 281 #define SLOT_DEFAULT 1 282 #define SLOT_ADDRESSED 2 283 #define SLOT_CONFIGURED 3 284 285 #define SLOT_CONTEXT_ENTRIES_MASK 0x1f 286 #define SLOT_CONTEXT_ENTRIES_SHIFT 27 287 288 typedef struct XHCIState XHCIState; 289 290 typedef enum EPType { 291 ET_INVALID = 0, 292 ET_ISO_OUT, 293 ET_BULK_OUT, 294 ET_INTR_OUT, 295 ET_CONTROL, 296 ET_ISO_IN, 297 ET_BULK_IN, 298 ET_INTR_IN, 299 } EPType; 300 301 typedef struct XHCIRing { 302 dma_addr_t base; 303 dma_addr_t dequeue; 304 bool ccs; 305 } XHCIRing; 306 307 typedef struct XHCIPort { 308 XHCIState *xhci; 309 uint32_t portsc; 310 uint32_t portnr; 311 USBPort *uport; 312 uint32_t speedmask; 313 char name[16]; 314 MemoryRegion mem; 315 } XHCIPort; 316 317 typedef struct XHCITransfer { 318 XHCIState *xhci; 319 USBPacket packet; 320 QEMUSGList sgl; 321 bool running_async; 322 bool running_retry; 323 bool cancelled; 324 bool complete; 325 bool int_req; 326 unsigned int iso_pkts; 327 unsigned int slotid; 328 unsigned int epid; 329 bool in_xfer; 330 bool iso_xfer; 331 332 unsigned int trb_count; 333 unsigned int trb_alloced; 334 XHCITRB *trbs; 335 336 TRBCCode status; 337 338 unsigned int pkts; 339 unsigned int pktsize; 340 unsigned int cur_pkt; 341 342 uint64_t mfindex_kick; 343 } XHCITransfer; 344 345 typedef struct XHCIEPContext { 346 XHCIState *xhci; 347 unsigned int slotid; 348 unsigned int epid; 349 350 XHCIRing ring; 351 unsigned int next_xfer; 352 unsigned int comp_xfer; 353 XHCITransfer transfers[TD_QUEUE]; 354 XHCITransfer *retry; 355 EPType type; 356 dma_addr_t pctx; 357 unsigned int max_psize; 358 uint32_t state; 359 360 /* iso xfer scheduling */ 361 unsigned int interval; 362 int64_t mfindex_last; 363 QEMUTimer *kick_timer; 364 } XHCIEPContext; 365 366 typedef struct XHCISlot { 367 bool enabled; 368 dma_addr_t ctx; 369 USBPort *uport; 370 unsigned int devaddr; 371 XHCIEPContext * eps[31]; 372 } XHCISlot; 373 374 typedef struct XHCIEvent { 375 TRBType type; 376 TRBCCode ccode; 377 uint64_t ptr; 378 uint32_t length; 379 uint32_t flags; 380 uint8_t slotid; 381 uint8_t epid; 382 } XHCIEvent; 383 384 typedef struct XHCIInterrupter { 385 uint32_t iman; 386 uint32_t imod; 387 uint32_t erstsz; 388 uint32_t erstba_low; 389 uint32_t erstba_high; 390 uint32_t erdp_low; 391 uint32_t erdp_high; 392 393 bool msix_used, er_pcs, er_full; 394 395 dma_addr_t er_start; 396 uint32_t er_size; 397 unsigned int er_ep_idx; 398 399 XHCIEvent ev_buffer[EV_QUEUE]; 400 unsigned int ev_buffer_put; 401 unsigned int ev_buffer_get; 402 403 } XHCIInterrupter; 404 405 struct XHCIState { 406 PCIDevice pci_dev; 407 USBBus bus; 408 qemu_irq irq; 409 MemoryRegion mem; 410 MemoryRegion mem_cap; 411 MemoryRegion mem_oper; 412 MemoryRegion mem_runtime; 413 MemoryRegion mem_doorbell; 414 const char *name; 415 unsigned int devaddr; 416 417 /* properties */ 418 uint32_t numports_2; 419 uint32_t numports_3; 420 uint32_t flags; 421 422 /* Operational Registers */ 423 uint32_t usbcmd; 424 uint32_t usbsts; 425 uint32_t dnctrl; 426 uint32_t crcr_low; 427 uint32_t crcr_high; 428 uint32_t dcbaap_low; 429 uint32_t dcbaap_high; 430 uint32_t config; 431 432 USBPort uports[MAX(MAXPORTS_2, MAXPORTS_3)]; 433 XHCIPort ports[MAXPORTS]; 434 XHCISlot slots[MAXSLOTS]; 435 uint32_t numports; 436 437 /* Runtime Registers */ 438 int64_t mfindex_start; 439 QEMUTimer *mfwrap_timer; 440 XHCIInterrupter intr[MAXINTRS]; 441 442 XHCIRing cmd_ring; 443 }; 444 445 typedef struct XHCIEvRingSeg { 446 uint32_t addr_low; 447 uint32_t addr_high; 448 uint32_t size; 449 uint32_t rsvd; 450 } XHCIEvRingSeg; 451 452 enum xhci_flags { 453 XHCI_FLAG_USE_MSI = 1, 454 XHCI_FLAG_USE_MSI_X, 455 }; 456 457 static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, 458 unsigned int epid); 459 static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v); 460 static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v); 461 462 static const char *TRBType_names[] = { 463 [TRB_RESERVED] = "TRB_RESERVED", 464 [TR_NORMAL] = "TR_NORMAL", 465 [TR_SETUP] = "TR_SETUP", 466 [TR_DATA] = "TR_DATA", 467 [TR_STATUS] = "TR_STATUS", 468 [TR_ISOCH] = "TR_ISOCH", 469 [TR_LINK] = "TR_LINK", 470 [TR_EVDATA] = "TR_EVDATA", 471 [TR_NOOP] = "TR_NOOP", 472 [CR_ENABLE_SLOT] = "CR_ENABLE_SLOT", 473 [CR_DISABLE_SLOT] = "CR_DISABLE_SLOT", 474 [CR_ADDRESS_DEVICE] = "CR_ADDRESS_DEVICE", 475 [CR_CONFIGURE_ENDPOINT] = "CR_CONFIGURE_ENDPOINT", 476 [CR_EVALUATE_CONTEXT] = "CR_EVALUATE_CONTEXT", 477 [CR_RESET_ENDPOINT] = "CR_RESET_ENDPOINT", 478 [CR_STOP_ENDPOINT] = "CR_STOP_ENDPOINT", 479 [CR_SET_TR_DEQUEUE] = "CR_SET_TR_DEQUEUE", 480 [CR_RESET_DEVICE] = "CR_RESET_DEVICE", 481 [CR_FORCE_EVENT] = "CR_FORCE_EVENT", 482 [CR_NEGOTIATE_BW] = "CR_NEGOTIATE_BW", 483 [CR_SET_LATENCY_TOLERANCE] = "CR_SET_LATENCY_TOLERANCE", 484 [CR_GET_PORT_BANDWIDTH] = "CR_GET_PORT_BANDWIDTH", 485 [CR_FORCE_HEADER] = "CR_FORCE_HEADER", 486 [CR_NOOP] = "CR_NOOP", 487 [ER_TRANSFER] = "ER_TRANSFER", 488 [ER_COMMAND_COMPLETE] = "ER_COMMAND_COMPLETE", 489 [ER_PORT_STATUS_CHANGE] = "ER_PORT_STATUS_CHANGE", 490 [ER_BANDWIDTH_REQUEST] = "ER_BANDWIDTH_REQUEST", 491 [ER_DOORBELL] = "ER_DOORBELL", 492 [ER_HOST_CONTROLLER] = "ER_HOST_CONTROLLER", 493 [ER_DEVICE_NOTIFICATION] = "ER_DEVICE_NOTIFICATION", 494 [ER_MFINDEX_WRAP] = "ER_MFINDEX_WRAP", 495 [CR_VENDOR_VIA_CHALLENGE_RESPONSE] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE", 496 [CR_VENDOR_NEC_FIRMWARE_REVISION] = "CR_VENDOR_NEC_FIRMWARE_REVISION", 497 [CR_VENDOR_NEC_CHALLENGE_RESPONSE] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE", 498 }; 499 500 static const char *TRBCCode_names[] = { 501 [CC_INVALID] = "CC_INVALID", 502 [CC_SUCCESS] = "CC_SUCCESS", 503 [CC_DATA_BUFFER_ERROR] = "CC_DATA_BUFFER_ERROR", 504 [CC_BABBLE_DETECTED] = "CC_BABBLE_DETECTED", 505 [CC_USB_TRANSACTION_ERROR] = "CC_USB_TRANSACTION_ERROR", 506 [CC_TRB_ERROR] = "CC_TRB_ERROR", 507 [CC_STALL_ERROR] = "CC_STALL_ERROR", 508 [CC_RESOURCE_ERROR] = "CC_RESOURCE_ERROR", 509 [CC_BANDWIDTH_ERROR] = "CC_BANDWIDTH_ERROR", 510 [CC_NO_SLOTS_ERROR] = "CC_NO_SLOTS_ERROR", 511 [CC_INVALID_STREAM_TYPE_ERROR] = "CC_INVALID_STREAM_TYPE_ERROR", 512 [CC_SLOT_NOT_ENABLED_ERROR] = "CC_SLOT_NOT_ENABLED_ERROR", 513 [CC_EP_NOT_ENABLED_ERROR] = "CC_EP_NOT_ENABLED_ERROR", 514 [CC_SHORT_PACKET] = "CC_SHORT_PACKET", 515 [CC_RING_UNDERRUN] = "CC_RING_UNDERRUN", 516 [CC_RING_OVERRUN] = "CC_RING_OVERRUN", 517 [CC_VF_ER_FULL] = "CC_VF_ER_FULL", 518 [CC_PARAMETER_ERROR] = "CC_PARAMETER_ERROR", 519 [CC_BANDWIDTH_OVERRUN] = "CC_BANDWIDTH_OVERRUN", 520 [CC_CONTEXT_STATE_ERROR] = "CC_CONTEXT_STATE_ERROR", 521 [CC_NO_PING_RESPONSE_ERROR] = "CC_NO_PING_RESPONSE_ERROR", 522 [CC_EVENT_RING_FULL_ERROR] = "CC_EVENT_RING_FULL_ERROR", 523 [CC_INCOMPATIBLE_DEVICE_ERROR] = "CC_INCOMPATIBLE_DEVICE_ERROR", 524 [CC_MISSED_SERVICE_ERROR] = "CC_MISSED_SERVICE_ERROR", 525 [CC_COMMAND_RING_STOPPED] = "CC_COMMAND_RING_STOPPED", 526 [CC_COMMAND_ABORTED] = "CC_COMMAND_ABORTED", 527 [CC_STOPPED] = "CC_STOPPED", 528 [CC_STOPPED_LENGTH_INVALID] = "CC_STOPPED_LENGTH_INVALID", 529 [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR] 530 = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR", 531 [CC_ISOCH_BUFFER_OVERRUN] = "CC_ISOCH_BUFFER_OVERRUN", 532 [CC_EVENT_LOST_ERROR] = "CC_EVENT_LOST_ERROR", 533 [CC_UNDEFINED_ERROR] = "CC_UNDEFINED_ERROR", 534 [CC_INVALID_STREAM_ID_ERROR] = "CC_INVALID_STREAM_ID_ERROR", 535 [CC_SECONDARY_BANDWIDTH_ERROR] = "CC_SECONDARY_BANDWIDTH_ERROR", 536 [CC_SPLIT_TRANSACTION_ERROR] = "CC_SPLIT_TRANSACTION_ERROR", 537 }; 538 539 static const char *lookup_name(uint32_t index, const char **list, uint32_t llen) 540 { 541 if (index >= llen || list[index] == NULL) { 542 return "???"; 543 } 544 return list[index]; 545 } 546 547 static const char *trb_name(XHCITRB *trb) 548 { 549 return lookup_name(TRB_TYPE(*trb), TRBType_names, 550 ARRAY_SIZE(TRBType_names)); 551 } 552 553 static const char *event_name(XHCIEvent *event) 554 { 555 return lookup_name(event->ccode, TRBCCode_names, 556 ARRAY_SIZE(TRBCCode_names)); 557 } 558 559 static uint64_t xhci_mfindex_get(XHCIState *xhci) 560 { 561 int64_t now = qemu_get_clock_ns(vm_clock); 562 return (now - xhci->mfindex_start) / 125000; 563 } 564 565 static void xhci_mfwrap_update(XHCIState *xhci) 566 { 567 const uint32_t bits = USBCMD_RS | USBCMD_EWE; 568 uint32_t mfindex, left; 569 int64_t now; 570 571 if ((xhci->usbcmd & bits) == bits) { 572 now = qemu_get_clock_ns(vm_clock); 573 mfindex = ((now - xhci->mfindex_start) / 125000) & 0x3fff; 574 left = 0x4000 - mfindex; 575 qemu_mod_timer(xhci->mfwrap_timer, now + left * 125000); 576 } else { 577 qemu_del_timer(xhci->mfwrap_timer); 578 } 579 } 580 581 static void xhci_mfwrap_timer(void *opaque) 582 { 583 XHCIState *xhci = opaque; 584 XHCIEvent wrap = { ER_MFINDEX_WRAP, CC_SUCCESS }; 585 586 xhci_event(xhci, &wrap, 0); 587 xhci_mfwrap_update(xhci); 588 } 589 590 static inline dma_addr_t xhci_addr64(uint32_t low, uint32_t high) 591 { 592 if (sizeof(dma_addr_t) == 4) { 593 return low; 594 } else { 595 return low | (((dma_addr_t)high << 16) << 16); 596 } 597 } 598 599 static inline dma_addr_t xhci_mask64(uint64_t addr) 600 { 601 if (sizeof(dma_addr_t) == 4) { 602 return addr & 0xffffffff; 603 } else { 604 return addr; 605 } 606 } 607 608 static XHCIPort *xhci_lookup_port(XHCIState *xhci, struct USBPort *uport) 609 { 610 int index; 611 612 if (!uport->dev) { 613 return NULL; 614 } 615 switch (uport->dev->speed) { 616 case USB_SPEED_LOW: 617 case USB_SPEED_FULL: 618 case USB_SPEED_HIGH: 619 index = uport->index; 620 break; 621 case USB_SPEED_SUPER: 622 index = uport->index + xhci->numports_2; 623 break; 624 default: 625 return NULL; 626 } 627 return &xhci->ports[index]; 628 } 629 630 static void xhci_intx_update(XHCIState *xhci) 631 { 632 int level = 0; 633 634 if (msix_enabled(&xhci->pci_dev) || 635 msi_enabled(&xhci->pci_dev)) { 636 return; 637 } 638 639 if (xhci->intr[0].iman & IMAN_IP && 640 xhci->intr[0].iman & IMAN_IE && 641 xhci->usbcmd & USBCMD_INTE) { 642 level = 1; 643 } 644 645 trace_usb_xhci_irq_intx(level); 646 qemu_set_irq(xhci->irq, level); 647 } 648 649 static void xhci_msix_update(XHCIState *xhci, int v) 650 { 651 bool enabled; 652 653 if (!msix_enabled(&xhci->pci_dev)) { 654 return; 655 } 656 657 enabled = xhci->intr[v].iman & IMAN_IE; 658 if (enabled == xhci->intr[v].msix_used) { 659 return; 660 } 661 662 if (enabled) { 663 trace_usb_xhci_irq_msix_use(v); 664 msix_vector_use(&xhci->pci_dev, v); 665 xhci->intr[v].msix_used = true; 666 } else { 667 trace_usb_xhci_irq_msix_unuse(v); 668 msix_vector_unuse(&xhci->pci_dev, v); 669 xhci->intr[v].msix_used = false; 670 } 671 } 672 673 static void xhci_intr_raise(XHCIState *xhci, int v) 674 { 675 xhci->intr[v].erdp_low |= ERDP_EHB; 676 xhci->intr[v].iman |= IMAN_IP; 677 xhci->usbsts |= USBSTS_EINT; 678 679 if (!(xhci->intr[v].iman & IMAN_IE)) { 680 return; 681 } 682 683 if (!(xhci->usbcmd & USBCMD_INTE)) { 684 return; 685 } 686 687 if (msix_enabled(&xhci->pci_dev)) { 688 trace_usb_xhci_irq_msix(v); 689 msix_notify(&xhci->pci_dev, v); 690 return; 691 } 692 693 if (msi_enabled(&xhci->pci_dev)) { 694 trace_usb_xhci_irq_msi(v); 695 msi_notify(&xhci->pci_dev, v); 696 return; 697 } 698 699 if (v == 0) { 700 trace_usb_xhci_irq_intx(1); 701 qemu_set_irq(xhci->irq, 1); 702 } 703 } 704 705 static inline int xhci_running(XHCIState *xhci) 706 { 707 return !(xhci->usbsts & USBSTS_HCH) && !xhci->intr[0].er_full; 708 } 709 710 static void xhci_die(XHCIState *xhci) 711 { 712 xhci->usbsts |= USBSTS_HCE; 713 fprintf(stderr, "xhci: asserted controller error\n"); 714 } 715 716 static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v) 717 { 718 XHCIInterrupter *intr = &xhci->intr[v]; 719 XHCITRB ev_trb; 720 dma_addr_t addr; 721 722 ev_trb.parameter = cpu_to_le64(event->ptr); 723 ev_trb.status = cpu_to_le32(event->length | (event->ccode << 24)); 724 ev_trb.control = (event->slotid << 24) | (event->epid << 16) | 725 event->flags | (event->type << TRB_TYPE_SHIFT); 726 if (intr->er_pcs) { 727 ev_trb.control |= TRB_C; 728 } 729 ev_trb.control = cpu_to_le32(ev_trb.control); 730 731 trace_usb_xhci_queue_event(v, intr->er_ep_idx, trb_name(&ev_trb), 732 event_name(event), ev_trb.parameter, 733 ev_trb.status, ev_trb.control); 734 735 addr = intr->er_start + TRB_SIZE*intr->er_ep_idx; 736 pci_dma_write(&xhci->pci_dev, addr, &ev_trb, TRB_SIZE); 737 738 intr->er_ep_idx++; 739 if (intr->er_ep_idx >= intr->er_size) { 740 intr->er_ep_idx = 0; 741 intr->er_pcs = !intr->er_pcs; 742 } 743 } 744 745 static void xhci_events_update(XHCIState *xhci, int v) 746 { 747 XHCIInterrupter *intr = &xhci->intr[v]; 748 dma_addr_t erdp; 749 unsigned int dp_idx; 750 bool do_irq = 0; 751 752 if (xhci->usbsts & USBSTS_HCH) { 753 return; 754 } 755 756 erdp = xhci_addr64(intr->erdp_low, intr->erdp_high); 757 if (erdp < intr->er_start || 758 erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) { 759 fprintf(stderr, "xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp); 760 fprintf(stderr, "xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n", 761 v, intr->er_start, intr->er_size); 762 xhci_die(xhci); 763 return; 764 } 765 dp_idx = (erdp - intr->er_start) / TRB_SIZE; 766 assert(dp_idx < intr->er_size); 767 768 /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus 769 * deadlocks when the ER is full. Hack it by holding off events until 770 * the driver decides to free at least half of the ring */ 771 if (intr->er_full) { 772 int er_free = dp_idx - intr->er_ep_idx; 773 if (er_free <= 0) { 774 er_free += intr->er_size; 775 } 776 if (er_free < (intr->er_size/2)) { 777 DPRINTF("xhci_events_update(): event ring still " 778 "more than half full (hack)\n"); 779 return; 780 } 781 } 782 783 while (intr->ev_buffer_put != intr->ev_buffer_get) { 784 assert(intr->er_full); 785 if (((intr->er_ep_idx+1) % intr->er_size) == dp_idx) { 786 DPRINTF("xhci_events_update(): event ring full again\n"); 787 #ifndef ER_FULL_HACK 788 XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR}; 789 xhci_write_event(xhci, &full, v); 790 #endif 791 do_irq = 1; 792 break; 793 } 794 XHCIEvent *event = &intr->ev_buffer[intr->ev_buffer_get]; 795 xhci_write_event(xhci, event, v); 796 intr->ev_buffer_get++; 797 do_irq = 1; 798 if (intr->ev_buffer_get == EV_QUEUE) { 799 intr->ev_buffer_get = 0; 800 } 801 } 802 803 if (do_irq) { 804 xhci_intr_raise(xhci, v); 805 } 806 807 if (intr->er_full && intr->ev_buffer_put == intr->ev_buffer_get) { 808 DPRINTF("xhci_events_update(): event ring no longer full\n"); 809 intr->er_full = 0; 810 } 811 } 812 813 static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v) 814 { 815 XHCIInterrupter *intr; 816 dma_addr_t erdp; 817 unsigned int dp_idx; 818 819 if (v >= MAXINTRS) { 820 DPRINTF("intr nr out of range (%d >= %d)\n", v, MAXINTRS); 821 return; 822 } 823 intr = &xhci->intr[v]; 824 825 if (intr->er_full) { 826 DPRINTF("xhci_event(): ER full, queueing\n"); 827 if (((intr->ev_buffer_put+1) % EV_QUEUE) == intr->ev_buffer_get) { 828 fprintf(stderr, "xhci: event queue full, dropping event!\n"); 829 return; 830 } 831 intr->ev_buffer[intr->ev_buffer_put++] = *event; 832 if (intr->ev_buffer_put == EV_QUEUE) { 833 intr->ev_buffer_put = 0; 834 } 835 return; 836 } 837 838 erdp = xhci_addr64(intr->erdp_low, intr->erdp_high); 839 if (erdp < intr->er_start || 840 erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) { 841 fprintf(stderr, "xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp); 842 fprintf(stderr, "xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n", 843 v, intr->er_start, intr->er_size); 844 xhci_die(xhci); 845 return; 846 } 847 848 dp_idx = (erdp - intr->er_start) / TRB_SIZE; 849 assert(dp_idx < intr->er_size); 850 851 if ((intr->er_ep_idx+1) % intr->er_size == dp_idx) { 852 DPRINTF("xhci_event(): ER full, queueing\n"); 853 #ifndef ER_FULL_HACK 854 XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR}; 855 xhci_write_event(xhci, &full); 856 #endif 857 intr->er_full = 1; 858 if (((intr->ev_buffer_put+1) % EV_QUEUE) == intr->ev_buffer_get) { 859 fprintf(stderr, "xhci: event queue full, dropping event!\n"); 860 return; 861 } 862 intr->ev_buffer[intr->ev_buffer_put++] = *event; 863 if (intr->ev_buffer_put == EV_QUEUE) { 864 intr->ev_buffer_put = 0; 865 } 866 } else { 867 xhci_write_event(xhci, event, v); 868 } 869 870 xhci_intr_raise(xhci, v); 871 } 872 873 static void xhci_ring_init(XHCIState *xhci, XHCIRing *ring, 874 dma_addr_t base) 875 { 876 ring->base = base; 877 ring->dequeue = base; 878 ring->ccs = 1; 879 } 880 881 static TRBType xhci_ring_fetch(XHCIState *xhci, XHCIRing *ring, XHCITRB *trb, 882 dma_addr_t *addr) 883 { 884 while (1) { 885 TRBType type; 886 pci_dma_read(&xhci->pci_dev, ring->dequeue, trb, TRB_SIZE); 887 trb->addr = ring->dequeue; 888 trb->ccs = ring->ccs; 889 le64_to_cpus(&trb->parameter); 890 le32_to_cpus(&trb->status); 891 le32_to_cpus(&trb->control); 892 893 trace_usb_xhci_fetch_trb(ring->dequeue, trb_name(trb), 894 trb->parameter, trb->status, trb->control); 895 896 if ((trb->control & TRB_C) != ring->ccs) { 897 return 0; 898 } 899 900 type = TRB_TYPE(*trb); 901 902 if (type != TR_LINK) { 903 if (addr) { 904 *addr = ring->dequeue; 905 } 906 ring->dequeue += TRB_SIZE; 907 return type; 908 } else { 909 ring->dequeue = xhci_mask64(trb->parameter); 910 if (trb->control & TRB_LK_TC) { 911 ring->ccs = !ring->ccs; 912 } 913 } 914 } 915 } 916 917 static int xhci_ring_chain_length(XHCIState *xhci, const XHCIRing *ring) 918 { 919 XHCITRB trb; 920 int length = 0; 921 dma_addr_t dequeue = ring->dequeue; 922 bool ccs = ring->ccs; 923 /* hack to bundle together the two/three TDs that make a setup transfer */ 924 bool control_td_set = 0; 925 926 while (1) { 927 TRBType type; 928 pci_dma_read(&xhci->pci_dev, dequeue, &trb, TRB_SIZE); 929 le64_to_cpus(&trb.parameter); 930 le32_to_cpus(&trb.status); 931 le32_to_cpus(&trb.control); 932 933 if ((trb.control & TRB_C) != ccs) { 934 return -length; 935 } 936 937 type = TRB_TYPE(trb); 938 939 if (type == TR_LINK) { 940 dequeue = xhci_mask64(trb.parameter); 941 if (trb.control & TRB_LK_TC) { 942 ccs = !ccs; 943 } 944 continue; 945 } 946 947 length += 1; 948 dequeue += TRB_SIZE; 949 950 if (type == TR_SETUP) { 951 control_td_set = 1; 952 } else if (type == TR_STATUS) { 953 control_td_set = 0; 954 } 955 956 if (!control_td_set && !(trb.control & TRB_TR_CH)) { 957 return length; 958 } 959 } 960 } 961 962 static void xhci_er_reset(XHCIState *xhci, int v) 963 { 964 XHCIInterrupter *intr = &xhci->intr[v]; 965 XHCIEvRingSeg seg; 966 967 /* cache the (sole) event ring segment location */ 968 if (intr->erstsz != 1) { 969 fprintf(stderr, "xhci: invalid value for ERSTSZ: %d\n", intr->erstsz); 970 xhci_die(xhci); 971 return; 972 } 973 dma_addr_t erstba = xhci_addr64(intr->erstba_low, intr->erstba_high); 974 pci_dma_read(&xhci->pci_dev, erstba, &seg, sizeof(seg)); 975 le32_to_cpus(&seg.addr_low); 976 le32_to_cpus(&seg.addr_high); 977 le32_to_cpus(&seg.size); 978 if (seg.size < 16 || seg.size > 4096) { 979 fprintf(stderr, "xhci: invalid value for segment size: %d\n", seg.size); 980 xhci_die(xhci); 981 return; 982 } 983 intr->er_start = xhci_addr64(seg.addr_low, seg.addr_high); 984 intr->er_size = seg.size; 985 986 intr->er_ep_idx = 0; 987 intr->er_pcs = 1; 988 intr->er_full = 0; 989 990 DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT " [%d]\n", 991 v, intr->er_start, intr->er_size); 992 } 993 994 static void xhci_run(XHCIState *xhci) 995 { 996 trace_usb_xhci_run(); 997 xhci->usbsts &= ~USBSTS_HCH; 998 xhci->mfindex_start = qemu_get_clock_ns(vm_clock); 999 } 1000 1001 static void xhci_stop(XHCIState *xhci) 1002 { 1003 trace_usb_xhci_stop(); 1004 xhci->usbsts |= USBSTS_HCH; 1005 xhci->crcr_low &= ~CRCR_CRR; 1006 } 1007 1008 static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx, 1009 uint32_t state) 1010 { 1011 uint32_t ctx[5]; 1012 1013 pci_dma_read(&xhci->pci_dev, epctx->pctx, ctx, sizeof(ctx)); 1014 ctx[0] &= ~EP_STATE_MASK; 1015 ctx[0] |= state; 1016 ctx[2] = epctx->ring.dequeue | epctx->ring.ccs; 1017 ctx[3] = (epctx->ring.dequeue >> 16) >> 16; 1018 DPRINTF("xhci: set epctx: " DMA_ADDR_FMT " state=%d dequeue=%08x%08x\n", 1019 epctx->pctx, state, ctx[3], ctx[2]); 1020 pci_dma_write(&xhci->pci_dev, epctx->pctx, ctx, sizeof(ctx)); 1021 epctx->state = state; 1022 } 1023 1024 static void xhci_ep_kick_timer(void *opaque) 1025 { 1026 XHCIEPContext *epctx = opaque; 1027 xhci_kick_ep(epctx->xhci, epctx->slotid, epctx->epid); 1028 } 1029 1030 static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid, 1031 unsigned int epid, dma_addr_t pctx, 1032 uint32_t *ctx) 1033 { 1034 XHCISlot *slot; 1035 XHCIEPContext *epctx; 1036 dma_addr_t dequeue; 1037 int i; 1038 1039 trace_usb_xhci_ep_enable(slotid, epid); 1040 assert(slotid >= 1 && slotid <= MAXSLOTS); 1041 assert(epid >= 1 && epid <= 31); 1042 1043 slot = &xhci->slots[slotid-1]; 1044 if (slot->eps[epid-1]) { 1045 fprintf(stderr, "xhci: slot %d ep %d already enabled!\n", slotid, epid); 1046 return CC_TRB_ERROR; 1047 } 1048 1049 epctx = g_malloc(sizeof(XHCIEPContext)); 1050 memset(epctx, 0, sizeof(XHCIEPContext)); 1051 epctx->xhci = xhci; 1052 epctx->slotid = slotid; 1053 epctx->epid = epid; 1054 1055 slot->eps[epid-1] = epctx; 1056 1057 dequeue = xhci_addr64(ctx[2] & ~0xf, ctx[3]); 1058 xhci_ring_init(xhci, &epctx->ring, dequeue); 1059 epctx->ring.ccs = ctx[2] & 1; 1060 1061 epctx->type = (ctx[1] >> EP_TYPE_SHIFT) & EP_TYPE_MASK; 1062 DPRINTF("xhci: endpoint %d.%d type is %d\n", epid/2, epid%2, epctx->type); 1063 epctx->pctx = pctx; 1064 epctx->max_psize = ctx[1]>>16; 1065 epctx->max_psize *= 1+((ctx[1]>>8)&0xff); 1066 DPRINTF("xhci: endpoint %d.%d max transaction (burst) size is %d\n", 1067 epid/2, epid%2, epctx->max_psize); 1068 for (i = 0; i < ARRAY_SIZE(epctx->transfers); i++) { 1069 usb_packet_init(&epctx->transfers[i].packet); 1070 } 1071 1072 epctx->interval = 1 << (ctx[0] >> 16) & 0xff; 1073 epctx->mfindex_last = 0; 1074 epctx->kick_timer = qemu_new_timer_ns(vm_clock, xhci_ep_kick_timer, epctx); 1075 1076 epctx->state = EP_RUNNING; 1077 ctx[0] &= ~EP_STATE_MASK; 1078 ctx[0] |= EP_RUNNING; 1079 1080 return CC_SUCCESS; 1081 } 1082 1083 static int xhci_ep_nuke_one_xfer(XHCITransfer *t) 1084 { 1085 int killed = 0; 1086 1087 if (t->running_async) { 1088 usb_cancel_packet(&t->packet); 1089 t->running_async = 0; 1090 t->cancelled = 1; 1091 DPRINTF("xhci: cancelling transfer, waiting for it to complete\n"); 1092 killed = 1; 1093 } 1094 if (t->running_retry) { 1095 XHCIEPContext *epctx = t->xhci->slots[t->slotid-1].eps[t->epid-1]; 1096 if (epctx) { 1097 epctx->retry = NULL; 1098 qemu_del_timer(epctx->kick_timer); 1099 } 1100 t->running_retry = 0; 1101 } 1102 if (t->trbs) { 1103 g_free(t->trbs); 1104 } 1105 1106 t->trbs = NULL; 1107 t->trb_count = t->trb_alloced = 0; 1108 1109 return killed; 1110 } 1111 1112 static int xhci_ep_nuke_xfers(XHCIState *xhci, unsigned int slotid, 1113 unsigned int epid) 1114 { 1115 XHCISlot *slot; 1116 XHCIEPContext *epctx; 1117 int i, xferi, killed = 0; 1118 assert(slotid >= 1 && slotid <= MAXSLOTS); 1119 assert(epid >= 1 && epid <= 31); 1120 1121 DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid, epid); 1122 1123 slot = &xhci->slots[slotid-1]; 1124 1125 if (!slot->eps[epid-1]) { 1126 return 0; 1127 } 1128 1129 epctx = slot->eps[epid-1]; 1130 1131 xferi = epctx->next_xfer; 1132 for (i = 0; i < TD_QUEUE; i++) { 1133 killed += xhci_ep_nuke_one_xfer(&epctx->transfers[xferi]); 1134 xferi = (xferi + 1) % TD_QUEUE; 1135 } 1136 return killed; 1137 } 1138 1139 static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid, 1140 unsigned int epid) 1141 { 1142 XHCISlot *slot; 1143 XHCIEPContext *epctx; 1144 1145 trace_usb_xhci_ep_disable(slotid, epid); 1146 assert(slotid >= 1 && slotid <= MAXSLOTS); 1147 assert(epid >= 1 && epid <= 31); 1148 1149 slot = &xhci->slots[slotid-1]; 1150 1151 if (!slot->eps[epid-1]) { 1152 DPRINTF("xhci: slot %d ep %d already disabled\n", slotid, epid); 1153 return CC_SUCCESS; 1154 } 1155 1156 xhci_ep_nuke_xfers(xhci, slotid, epid); 1157 1158 epctx = slot->eps[epid-1]; 1159 1160 xhci_set_ep_state(xhci, epctx, EP_DISABLED); 1161 1162 qemu_free_timer(epctx->kick_timer); 1163 g_free(epctx); 1164 slot->eps[epid-1] = NULL; 1165 1166 return CC_SUCCESS; 1167 } 1168 1169 static TRBCCode xhci_stop_ep(XHCIState *xhci, unsigned int slotid, 1170 unsigned int epid) 1171 { 1172 XHCISlot *slot; 1173 XHCIEPContext *epctx; 1174 1175 trace_usb_xhci_ep_stop(slotid, epid); 1176 assert(slotid >= 1 && slotid <= MAXSLOTS); 1177 1178 if (epid < 1 || epid > 31) { 1179 fprintf(stderr, "xhci: bad ep %d\n", epid); 1180 return CC_TRB_ERROR; 1181 } 1182 1183 slot = &xhci->slots[slotid-1]; 1184 1185 if (!slot->eps[epid-1]) { 1186 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid); 1187 return CC_EP_NOT_ENABLED_ERROR; 1188 } 1189 1190 if (xhci_ep_nuke_xfers(xhci, slotid, epid) > 0) { 1191 fprintf(stderr, "xhci: FIXME: endpoint stopped w/ xfers running, " 1192 "data might be lost\n"); 1193 } 1194 1195 epctx = slot->eps[epid-1]; 1196 1197 xhci_set_ep_state(xhci, epctx, EP_STOPPED); 1198 1199 return CC_SUCCESS; 1200 } 1201 1202 static TRBCCode xhci_reset_ep(XHCIState *xhci, unsigned int slotid, 1203 unsigned int epid) 1204 { 1205 XHCISlot *slot; 1206 XHCIEPContext *epctx; 1207 USBDevice *dev; 1208 1209 trace_usb_xhci_ep_reset(slotid, epid); 1210 assert(slotid >= 1 && slotid <= MAXSLOTS); 1211 1212 if (epid < 1 || epid > 31) { 1213 fprintf(stderr, "xhci: bad ep %d\n", epid); 1214 return CC_TRB_ERROR; 1215 } 1216 1217 slot = &xhci->slots[slotid-1]; 1218 1219 if (!slot->eps[epid-1]) { 1220 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid); 1221 return CC_EP_NOT_ENABLED_ERROR; 1222 } 1223 1224 epctx = slot->eps[epid-1]; 1225 1226 if (epctx->state != EP_HALTED) { 1227 fprintf(stderr, "xhci: reset EP while EP %d not halted (%d)\n", 1228 epid, epctx->state); 1229 return CC_CONTEXT_STATE_ERROR; 1230 } 1231 1232 if (xhci_ep_nuke_xfers(xhci, slotid, epid) > 0) { 1233 fprintf(stderr, "xhci: FIXME: endpoint reset w/ xfers running, " 1234 "data might be lost\n"); 1235 } 1236 1237 uint8_t ep = epid>>1; 1238 1239 if (epid & 1) { 1240 ep |= 0x80; 1241 } 1242 1243 dev = xhci->slots[slotid-1].uport->dev; 1244 if (!dev) { 1245 return CC_USB_TRANSACTION_ERROR; 1246 } 1247 1248 xhci_set_ep_state(xhci, epctx, EP_STOPPED); 1249 1250 return CC_SUCCESS; 1251 } 1252 1253 static TRBCCode xhci_set_ep_dequeue(XHCIState *xhci, unsigned int slotid, 1254 unsigned int epid, uint64_t pdequeue) 1255 { 1256 XHCISlot *slot; 1257 XHCIEPContext *epctx; 1258 dma_addr_t dequeue; 1259 1260 assert(slotid >= 1 && slotid <= MAXSLOTS); 1261 1262 if (epid < 1 || epid > 31) { 1263 fprintf(stderr, "xhci: bad ep %d\n", epid); 1264 return CC_TRB_ERROR; 1265 } 1266 1267 trace_usb_xhci_ep_set_dequeue(slotid, epid, pdequeue); 1268 dequeue = xhci_mask64(pdequeue); 1269 1270 slot = &xhci->slots[slotid-1]; 1271 1272 if (!slot->eps[epid-1]) { 1273 DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid); 1274 return CC_EP_NOT_ENABLED_ERROR; 1275 } 1276 1277 epctx = slot->eps[epid-1]; 1278 1279 1280 if (epctx->state != EP_STOPPED) { 1281 fprintf(stderr, "xhci: set EP dequeue pointer while EP %d not stopped\n", epid); 1282 return CC_CONTEXT_STATE_ERROR; 1283 } 1284 1285 xhci_ring_init(xhci, &epctx->ring, dequeue & ~0xF); 1286 epctx->ring.ccs = dequeue & 1; 1287 1288 xhci_set_ep_state(xhci, epctx, EP_STOPPED); 1289 1290 return CC_SUCCESS; 1291 } 1292 1293 static int xhci_xfer_create_sgl(XHCITransfer *xfer, int in_xfer) 1294 { 1295 XHCIState *xhci = xfer->xhci; 1296 int i; 1297 1298 xfer->int_req = false; 1299 pci_dma_sglist_init(&xfer->sgl, &xhci->pci_dev, xfer->trb_count); 1300 for (i = 0; i < xfer->trb_count; i++) { 1301 XHCITRB *trb = &xfer->trbs[i]; 1302 dma_addr_t addr; 1303 unsigned int chunk = 0; 1304 1305 if (trb->control & TRB_TR_IOC) { 1306 xfer->int_req = true; 1307 } 1308 1309 switch (TRB_TYPE(*trb)) { 1310 case TR_DATA: 1311 if ((!(trb->control & TRB_TR_DIR)) != (!in_xfer)) { 1312 fprintf(stderr, "xhci: data direction mismatch for TR_DATA\n"); 1313 goto err; 1314 } 1315 /* fallthrough */ 1316 case TR_NORMAL: 1317 case TR_ISOCH: 1318 addr = xhci_mask64(trb->parameter); 1319 chunk = trb->status & 0x1ffff; 1320 if (trb->control & TRB_TR_IDT) { 1321 if (chunk > 8 || in_xfer) { 1322 fprintf(stderr, "xhci: invalid immediate data TRB\n"); 1323 goto err; 1324 } 1325 qemu_sglist_add(&xfer->sgl, trb->addr, chunk); 1326 } else { 1327 qemu_sglist_add(&xfer->sgl, addr, chunk); 1328 } 1329 break; 1330 } 1331 } 1332 1333 return 0; 1334 1335 err: 1336 qemu_sglist_destroy(&xfer->sgl); 1337 xhci_die(xhci); 1338 return -1; 1339 } 1340 1341 static void xhci_xfer_unmap(XHCITransfer *xfer) 1342 { 1343 usb_packet_unmap(&xfer->packet, &xfer->sgl); 1344 qemu_sglist_destroy(&xfer->sgl); 1345 } 1346 1347 static void xhci_xfer_report(XHCITransfer *xfer) 1348 { 1349 uint32_t edtla = 0; 1350 unsigned int left; 1351 bool reported = 0; 1352 bool shortpkt = 0; 1353 XHCIEvent event = {ER_TRANSFER, CC_SUCCESS}; 1354 XHCIState *xhci = xfer->xhci; 1355 int i; 1356 1357 left = xfer->packet.result < 0 ? 0 : xfer->packet.result; 1358 1359 for (i = 0; i < xfer->trb_count; i++) { 1360 XHCITRB *trb = &xfer->trbs[i]; 1361 unsigned int chunk = 0; 1362 1363 switch (TRB_TYPE(*trb)) { 1364 case TR_DATA: 1365 case TR_NORMAL: 1366 case TR_ISOCH: 1367 chunk = trb->status & 0x1ffff; 1368 if (chunk > left) { 1369 chunk = left; 1370 if (xfer->status == CC_SUCCESS) { 1371 shortpkt = 1; 1372 } 1373 } 1374 left -= chunk; 1375 edtla += chunk; 1376 break; 1377 case TR_STATUS: 1378 reported = 0; 1379 shortpkt = 0; 1380 break; 1381 } 1382 1383 if (!reported && ((trb->control & TRB_TR_IOC) || 1384 (shortpkt && (trb->control & TRB_TR_ISP)) || 1385 (xfer->status != CC_SUCCESS))) { 1386 event.slotid = xfer->slotid; 1387 event.epid = xfer->epid; 1388 event.length = (trb->status & 0x1ffff) - chunk; 1389 event.flags = 0; 1390 event.ptr = trb->addr; 1391 if (xfer->status == CC_SUCCESS) { 1392 event.ccode = shortpkt ? CC_SHORT_PACKET : CC_SUCCESS; 1393 } else { 1394 event.ccode = xfer->status; 1395 } 1396 if (TRB_TYPE(*trb) == TR_EVDATA) { 1397 event.ptr = trb->parameter; 1398 event.flags |= TRB_EV_ED; 1399 event.length = edtla & 0xffffff; 1400 DPRINTF("xhci_xfer_data: EDTLA=%d\n", event.length); 1401 edtla = 0; 1402 } 1403 xhci_event(xhci, &event, TRB_INTR(*trb)); 1404 reported = 1; 1405 if (xfer->status != CC_SUCCESS) { 1406 return; 1407 } 1408 } 1409 } 1410 } 1411 1412 static void xhci_stall_ep(XHCITransfer *xfer) 1413 { 1414 XHCIState *xhci = xfer->xhci; 1415 XHCISlot *slot = &xhci->slots[xfer->slotid-1]; 1416 XHCIEPContext *epctx = slot->eps[xfer->epid-1]; 1417 1418 epctx->ring.dequeue = xfer->trbs[0].addr; 1419 epctx->ring.ccs = xfer->trbs[0].ccs; 1420 xhci_set_ep_state(xhci, epctx, EP_HALTED); 1421 DPRINTF("xhci: stalled slot %d ep %d\n", xfer->slotid, xfer->epid); 1422 DPRINTF("xhci: will continue at "DMA_ADDR_FMT"\n", epctx->ring.dequeue); 1423 } 1424 1425 static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, 1426 XHCIEPContext *epctx); 1427 1428 static int xhci_setup_packet(XHCITransfer *xfer) 1429 { 1430 XHCIState *xhci = xfer->xhci; 1431 USBDevice *dev; 1432 USBEndpoint *ep; 1433 int dir; 1434 1435 dir = xfer->in_xfer ? USB_TOKEN_IN : USB_TOKEN_OUT; 1436 1437 if (xfer->packet.ep) { 1438 ep = xfer->packet.ep; 1439 dev = ep->dev; 1440 } else { 1441 if (!xhci->slots[xfer->slotid-1].uport) { 1442 fprintf(stderr, "xhci: slot %d has no device\n", 1443 xfer->slotid); 1444 return -1; 1445 } 1446 dev = xhci->slots[xfer->slotid-1].uport->dev; 1447 ep = usb_ep_get(dev, dir, xfer->epid >> 1); 1448 } 1449 1450 xhci_xfer_create_sgl(xfer, dir == USB_TOKEN_IN); /* Also sets int_req */ 1451 usb_packet_setup(&xfer->packet, dir, ep, xfer->trbs[0].addr, false, 1452 xfer->int_req); 1453 usb_packet_map(&xfer->packet, &xfer->sgl); 1454 DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n", 1455 xfer->packet.pid, dev->addr, ep->nr); 1456 return 0; 1457 } 1458 1459 static int xhci_complete_packet(XHCITransfer *xfer, int ret) 1460 { 1461 if (ret == USB_RET_ASYNC) { 1462 trace_usb_xhci_xfer_async(xfer); 1463 xfer->running_async = 1; 1464 xfer->running_retry = 0; 1465 xfer->complete = 0; 1466 xfer->cancelled = 0; 1467 return 0; 1468 } else if (ret == USB_RET_NAK) { 1469 trace_usb_xhci_xfer_nak(xfer); 1470 xfer->running_async = 0; 1471 xfer->running_retry = 1; 1472 xfer->complete = 0; 1473 xfer->cancelled = 0; 1474 return 0; 1475 } else { 1476 xfer->running_async = 0; 1477 xfer->running_retry = 0; 1478 xfer->complete = 1; 1479 xhci_xfer_unmap(xfer); 1480 } 1481 1482 if (ret >= 0) { 1483 trace_usb_xhci_xfer_success(xfer, ret); 1484 xfer->status = CC_SUCCESS; 1485 xhci_xfer_report(xfer); 1486 return 0; 1487 } 1488 1489 /* error */ 1490 trace_usb_xhci_xfer_error(xfer, ret); 1491 switch (ret) { 1492 case USB_RET_NODEV: 1493 xfer->status = CC_USB_TRANSACTION_ERROR; 1494 xhci_xfer_report(xfer); 1495 xhci_stall_ep(xfer); 1496 break; 1497 case USB_RET_STALL: 1498 xfer->status = CC_STALL_ERROR; 1499 xhci_xfer_report(xfer); 1500 xhci_stall_ep(xfer); 1501 break; 1502 default: 1503 fprintf(stderr, "%s: FIXME: ret = %d\n", __FUNCTION__, ret); 1504 FIXME(); 1505 } 1506 return 0; 1507 } 1508 1509 static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer) 1510 { 1511 XHCITRB *trb_setup, *trb_status; 1512 uint8_t bmRequestType; 1513 int ret; 1514 1515 trb_setup = &xfer->trbs[0]; 1516 trb_status = &xfer->trbs[xfer->trb_count-1]; 1517 1518 trace_usb_xhci_xfer_start(xfer, xfer->slotid, xfer->epid); 1519 1520 /* at most one Event Data TRB allowed after STATUS */ 1521 if (TRB_TYPE(*trb_status) == TR_EVDATA && xfer->trb_count > 2) { 1522 trb_status--; 1523 } 1524 1525 /* do some sanity checks */ 1526 if (TRB_TYPE(*trb_setup) != TR_SETUP) { 1527 fprintf(stderr, "xhci: ep0 first TD not SETUP: %d\n", 1528 TRB_TYPE(*trb_setup)); 1529 return -1; 1530 } 1531 if (TRB_TYPE(*trb_status) != TR_STATUS) { 1532 fprintf(stderr, "xhci: ep0 last TD not STATUS: %d\n", 1533 TRB_TYPE(*trb_status)); 1534 return -1; 1535 } 1536 if (!(trb_setup->control & TRB_TR_IDT)) { 1537 fprintf(stderr, "xhci: Setup TRB doesn't have IDT set\n"); 1538 return -1; 1539 } 1540 if ((trb_setup->status & 0x1ffff) != 8) { 1541 fprintf(stderr, "xhci: Setup TRB has bad length (%d)\n", 1542 (trb_setup->status & 0x1ffff)); 1543 return -1; 1544 } 1545 1546 bmRequestType = trb_setup->parameter; 1547 1548 xfer->in_xfer = bmRequestType & USB_DIR_IN; 1549 xfer->iso_xfer = false; 1550 1551 if (xhci_setup_packet(xfer) < 0) { 1552 return -1; 1553 } 1554 xfer->packet.parameter = trb_setup->parameter; 1555 1556 ret = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); 1557 1558 xhci_complete_packet(xfer, ret); 1559 if (!xfer->running_async && !xfer->running_retry) { 1560 xhci_kick_ep(xhci, xfer->slotid, xfer->epid); 1561 } 1562 return 0; 1563 } 1564 1565 static void xhci_calc_iso_kick(XHCIState *xhci, XHCITransfer *xfer, 1566 XHCIEPContext *epctx, uint64_t mfindex) 1567 { 1568 if (xfer->trbs[0].control & TRB_TR_SIA) { 1569 uint64_t asap = ((mfindex + epctx->interval - 1) & 1570 ~(epctx->interval-1)); 1571 if (asap >= epctx->mfindex_last && 1572 asap <= epctx->mfindex_last + epctx->interval * 4) { 1573 xfer->mfindex_kick = epctx->mfindex_last + epctx->interval; 1574 } else { 1575 xfer->mfindex_kick = asap; 1576 } 1577 } else { 1578 xfer->mfindex_kick = (xfer->trbs[0].control >> TRB_TR_FRAMEID_SHIFT) 1579 & TRB_TR_FRAMEID_MASK; 1580 xfer->mfindex_kick |= mfindex & ~0x3fff; 1581 if (xfer->mfindex_kick < mfindex) { 1582 xfer->mfindex_kick += 0x4000; 1583 } 1584 } 1585 } 1586 1587 static void xhci_check_iso_kick(XHCIState *xhci, XHCITransfer *xfer, 1588 XHCIEPContext *epctx, uint64_t mfindex) 1589 { 1590 if (xfer->mfindex_kick > mfindex) { 1591 qemu_mod_timer(epctx->kick_timer, qemu_get_clock_ns(vm_clock) + 1592 (xfer->mfindex_kick - mfindex) * 125000); 1593 xfer->running_retry = 1; 1594 } else { 1595 epctx->mfindex_last = xfer->mfindex_kick; 1596 qemu_del_timer(epctx->kick_timer); 1597 xfer->running_retry = 0; 1598 } 1599 } 1600 1601 1602 static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx) 1603 { 1604 uint64_t mfindex; 1605 int ret; 1606 1607 DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer->slotid, xfer->epid); 1608 1609 xfer->in_xfer = epctx->type>>2; 1610 1611 switch(epctx->type) { 1612 case ET_INTR_OUT: 1613 case ET_INTR_IN: 1614 case ET_BULK_OUT: 1615 case ET_BULK_IN: 1616 xfer->pkts = 0; 1617 xfer->iso_xfer = false; 1618 break; 1619 case ET_ISO_OUT: 1620 case ET_ISO_IN: 1621 xfer->pkts = 1; 1622 xfer->iso_xfer = true; 1623 mfindex = xhci_mfindex_get(xhci); 1624 xhci_calc_iso_kick(xhci, xfer, epctx, mfindex); 1625 xhci_check_iso_kick(xhci, xfer, epctx, mfindex); 1626 if (xfer->running_retry) { 1627 return -1; 1628 } 1629 break; 1630 default: 1631 fprintf(stderr, "xhci: unknown or unhandled EP " 1632 "(type %d, in %d, ep %02x)\n", 1633 epctx->type, xfer->in_xfer, xfer->epid); 1634 return -1; 1635 } 1636 1637 if (xhci_setup_packet(xfer) < 0) { 1638 return -1; 1639 } 1640 ret = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); 1641 1642 xhci_complete_packet(xfer, ret); 1643 if (!xfer->running_async && !xfer->running_retry) { 1644 xhci_kick_ep(xhci, xfer->slotid, xfer->epid); 1645 } 1646 return 0; 1647 } 1648 1649 static int xhci_fire_transfer(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx) 1650 { 1651 trace_usb_xhci_xfer_start(xfer, xfer->slotid, xfer->epid); 1652 return xhci_submit(xhci, xfer, epctx); 1653 } 1654 1655 static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, unsigned int epid) 1656 { 1657 XHCIEPContext *epctx; 1658 USBEndpoint *ep = NULL; 1659 uint64_t mfindex; 1660 int length; 1661 int i; 1662 1663 trace_usb_xhci_ep_kick(slotid, epid); 1664 assert(slotid >= 1 && slotid <= MAXSLOTS); 1665 assert(epid >= 1 && epid <= 31); 1666 1667 if (!xhci->slots[slotid-1].enabled) { 1668 fprintf(stderr, "xhci: xhci_kick_ep for disabled slot %d\n", slotid); 1669 return; 1670 } 1671 epctx = xhci->slots[slotid-1].eps[epid-1]; 1672 if (!epctx) { 1673 fprintf(stderr, "xhci: xhci_kick_ep for disabled endpoint %d,%d\n", 1674 epid, slotid); 1675 return; 1676 } 1677 1678 if (epctx->retry) { 1679 XHCITransfer *xfer = epctx->retry; 1680 int result; 1681 1682 trace_usb_xhci_xfer_retry(xfer); 1683 assert(xfer->running_retry); 1684 if (xfer->iso_xfer) { 1685 /* retry delayed iso transfer */ 1686 mfindex = xhci_mfindex_get(xhci); 1687 xhci_check_iso_kick(xhci, xfer, epctx, mfindex); 1688 if (xfer->running_retry) { 1689 return; 1690 } 1691 if (xhci_setup_packet(xfer) < 0) { 1692 return; 1693 } 1694 result = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); 1695 assert(result != USB_RET_NAK); 1696 xhci_complete_packet(xfer, result); 1697 } else { 1698 /* retry nak'ed transfer */ 1699 if (xhci_setup_packet(xfer) < 0) { 1700 return; 1701 } 1702 result = usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); 1703 if (result == USB_RET_NAK) { 1704 return; 1705 } 1706 xhci_complete_packet(xfer, result); 1707 } 1708 assert(!xfer->running_retry); 1709 epctx->retry = NULL; 1710 } 1711 1712 if (epctx->state == EP_HALTED) { 1713 DPRINTF("xhci: ep halted, not running schedule\n"); 1714 return; 1715 } 1716 1717 xhci_set_ep_state(xhci, epctx, EP_RUNNING); 1718 1719 while (1) { 1720 XHCITransfer *xfer = &epctx->transfers[epctx->next_xfer]; 1721 if (xfer->running_async || xfer->running_retry) { 1722 break; 1723 } 1724 length = xhci_ring_chain_length(xhci, &epctx->ring); 1725 if (length < 0) { 1726 break; 1727 } else if (length == 0) { 1728 break; 1729 } 1730 if (xfer->trbs && xfer->trb_alloced < length) { 1731 xfer->trb_count = 0; 1732 xfer->trb_alloced = 0; 1733 g_free(xfer->trbs); 1734 xfer->trbs = NULL; 1735 } 1736 if (!xfer->trbs) { 1737 xfer->trbs = g_malloc(sizeof(XHCITRB) * length); 1738 xfer->trb_alloced = length; 1739 } 1740 xfer->trb_count = length; 1741 1742 for (i = 0; i < length; i++) { 1743 assert(xhci_ring_fetch(xhci, &epctx->ring, &xfer->trbs[i], NULL)); 1744 } 1745 xfer->xhci = xhci; 1746 xfer->epid = epid; 1747 xfer->slotid = slotid; 1748 1749 if (epid == 1) { 1750 if (xhci_fire_ctl_transfer(xhci, xfer) >= 0) { 1751 epctx->next_xfer = (epctx->next_xfer + 1) % TD_QUEUE; 1752 ep = xfer->packet.ep; 1753 } else { 1754 fprintf(stderr, "xhci: error firing CTL transfer\n"); 1755 } 1756 } else { 1757 if (xhci_fire_transfer(xhci, xfer, epctx) >= 0) { 1758 epctx->next_xfer = (epctx->next_xfer + 1) % TD_QUEUE; 1759 ep = xfer->packet.ep; 1760 } else { 1761 if (!xfer->iso_xfer) { 1762 fprintf(stderr, "xhci: error firing data transfer\n"); 1763 } 1764 } 1765 } 1766 1767 if (epctx->state == EP_HALTED) { 1768 break; 1769 } 1770 if (xfer->running_retry) { 1771 DPRINTF("xhci: xfer nacked, stopping schedule\n"); 1772 epctx->retry = xfer; 1773 break; 1774 } 1775 } 1776 if (ep) { 1777 usb_device_flush_ep_queue(ep->dev, ep); 1778 } 1779 } 1780 1781 static TRBCCode xhci_enable_slot(XHCIState *xhci, unsigned int slotid) 1782 { 1783 trace_usb_xhci_slot_enable(slotid); 1784 assert(slotid >= 1 && slotid <= MAXSLOTS); 1785 xhci->slots[slotid-1].enabled = 1; 1786 xhci->slots[slotid-1].uport = NULL; 1787 memset(xhci->slots[slotid-1].eps, 0, sizeof(XHCIEPContext*)*31); 1788 1789 return CC_SUCCESS; 1790 } 1791 1792 static TRBCCode xhci_disable_slot(XHCIState *xhci, unsigned int slotid) 1793 { 1794 int i; 1795 1796 trace_usb_xhci_slot_disable(slotid); 1797 assert(slotid >= 1 && slotid <= MAXSLOTS); 1798 1799 for (i = 1; i <= 31; i++) { 1800 if (xhci->slots[slotid-1].eps[i-1]) { 1801 xhci_disable_ep(xhci, slotid, i); 1802 } 1803 } 1804 1805 xhci->slots[slotid-1].enabled = 0; 1806 return CC_SUCCESS; 1807 } 1808 1809 static USBPort *xhci_lookup_uport(XHCIState *xhci, uint32_t *slot_ctx) 1810 { 1811 USBPort *uport; 1812 char path[32]; 1813 int i, pos, port; 1814 1815 port = (slot_ctx[1]>>16) & 0xFF; 1816 port = xhci->ports[port-1].uport->index+1; 1817 pos = snprintf(path, sizeof(path), "%d", port); 1818 for (i = 0; i < 5; i++) { 1819 port = (slot_ctx[0] >> 4*i) & 0x0f; 1820 if (!port) { 1821 break; 1822 } 1823 pos += snprintf(path + pos, sizeof(path) - pos, ".%d", port); 1824 } 1825 1826 QTAILQ_FOREACH(uport, &xhci->bus.used, next) { 1827 if (strcmp(uport->path, path) == 0) { 1828 return uport; 1829 } 1830 } 1831 return NULL; 1832 } 1833 1834 static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid, 1835 uint64_t pictx, bool bsr) 1836 { 1837 XHCISlot *slot; 1838 USBPort *uport; 1839 USBDevice *dev; 1840 dma_addr_t ictx, octx, dcbaap; 1841 uint64_t poctx; 1842 uint32_t ictl_ctx[2]; 1843 uint32_t slot_ctx[4]; 1844 uint32_t ep0_ctx[5]; 1845 int i; 1846 TRBCCode res; 1847 1848 trace_usb_xhci_slot_address(slotid); 1849 assert(slotid >= 1 && slotid <= MAXSLOTS); 1850 1851 dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high); 1852 pci_dma_read(&xhci->pci_dev, dcbaap + 8*slotid, &poctx, sizeof(poctx)); 1853 ictx = xhci_mask64(pictx); 1854 octx = xhci_mask64(le64_to_cpu(poctx)); 1855 1856 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); 1857 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); 1858 1859 pci_dma_read(&xhci->pci_dev, ictx, ictl_ctx, sizeof(ictl_ctx)); 1860 1861 if (ictl_ctx[0] != 0x0 || ictl_ctx[1] != 0x3) { 1862 fprintf(stderr, "xhci: invalid input context control %08x %08x\n", 1863 ictl_ctx[0], ictl_ctx[1]); 1864 return CC_TRB_ERROR; 1865 } 1866 1867 pci_dma_read(&xhci->pci_dev, ictx+32, slot_ctx, sizeof(slot_ctx)); 1868 pci_dma_read(&xhci->pci_dev, ictx+64, ep0_ctx, sizeof(ep0_ctx)); 1869 1870 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n", 1871 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); 1872 1873 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n", 1874 ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]); 1875 1876 uport = xhci_lookup_uport(xhci, slot_ctx); 1877 if (uport == NULL) { 1878 fprintf(stderr, "xhci: port not found\n"); 1879 return CC_TRB_ERROR; 1880 } 1881 1882 dev = uport->dev; 1883 if (!dev) { 1884 fprintf(stderr, "xhci: port %s not connected\n", uport->path); 1885 return CC_USB_TRANSACTION_ERROR; 1886 } 1887 1888 for (i = 0; i < MAXSLOTS; i++) { 1889 if (xhci->slots[i].uport == uport) { 1890 fprintf(stderr, "xhci: port %s already assigned to slot %d\n", 1891 uport->path, i+1); 1892 return CC_TRB_ERROR; 1893 } 1894 } 1895 1896 slot = &xhci->slots[slotid-1]; 1897 slot->uport = uport; 1898 slot->ctx = octx; 1899 1900 if (bsr) { 1901 slot_ctx[3] = SLOT_DEFAULT << SLOT_STATE_SHIFT; 1902 } else { 1903 slot->devaddr = xhci->devaddr++; 1904 slot_ctx[3] = (SLOT_ADDRESSED << SLOT_STATE_SHIFT) | slot->devaddr; 1905 DPRINTF("xhci: device address is %d\n", slot->devaddr); 1906 usb_device_handle_control(dev, NULL, 1907 DeviceOutRequest | USB_REQ_SET_ADDRESS, 1908 slot->devaddr, 0, 0, NULL); 1909 } 1910 1911 res = xhci_enable_ep(xhci, slotid, 1, octx+32, ep0_ctx); 1912 1913 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", 1914 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); 1915 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n", 1916 ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]); 1917 1918 pci_dma_write(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 1919 pci_dma_write(&xhci->pci_dev, octx+32, ep0_ctx, sizeof(ep0_ctx)); 1920 1921 return res; 1922 } 1923 1924 1925 static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid, 1926 uint64_t pictx, bool dc) 1927 { 1928 dma_addr_t ictx, octx; 1929 uint32_t ictl_ctx[2]; 1930 uint32_t slot_ctx[4]; 1931 uint32_t islot_ctx[4]; 1932 uint32_t ep_ctx[5]; 1933 int i; 1934 TRBCCode res; 1935 1936 trace_usb_xhci_slot_configure(slotid); 1937 assert(slotid >= 1 && slotid <= MAXSLOTS); 1938 1939 ictx = xhci_mask64(pictx); 1940 octx = xhci->slots[slotid-1].ctx; 1941 1942 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); 1943 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); 1944 1945 if (dc) { 1946 for (i = 2; i <= 31; i++) { 1947 if (xhci->slots[slotid-1].eps[i-1]) { 1948 xhci_disable_ep(xhci, slotid, i); 1949 } 1950 } 1951 1952 pci_dma_read(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 1953 slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT); 1954 slot_ctx[3] |= SLOT_ADDRESSED << SLOT_STATE_SHIFT; 1955 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", 1956 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); 1957 pci_dma_write(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 1958 1959 return CC_SUCCESS; 1960 } 1961 1962 pci_dma_read(&xhci->pci_dev, ictx, ictl_ctx, sizeof(ictl_ctx)); 1963 1964 if ((ictl_ctx[0] & 0x3) != 0x0 || (ictl_ctx[1] & 0x3) != 0x1) { 1965 fprintf(stderr, "xhci: invalid input context control %08x %08x\n", 1966 ictl_ctx[0], ictl_ctx[1]); 1967 return CC_TRB_ERROR; 1968 } 1969 1970 pci_dma_read(&xhci->pci_dev, ictx+32, islot_ctx, sizeof(islot_ctx)); 1971 pci_dma_read(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 1972 1973 if (SLOT_STATE(slot_ctx[3]) < SLOT_ADDRESSED) { 1974 fprintf(stderr, "xhci: invalid slot state %08x\n", slot_ctx[3]); 1975 return CC_CONTEXT_STATE_ERROR; 1976 } 1977 1978 for (i = 2; i <= 31; i++) { 1979 if (ictl_ctx[0] & (1<<i)) { 1980 xhci_disable_ep(xhci, slotid, i); 1981 } 1982 if (ictl_ctx[1] & (1<<i)) { 1983 pci_dma_read(&xhci->pci_dev, ictx+32+(32*i), ep_ctx, 1984 sizeof(ep_ctx)); 1985 DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n", 1986 i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2], 1987 ep_ctx[3], ep_ctx[4]); 1988 xhci_disable_ep(xhci, slotid, i); 1989 res = xhci_enable_ep(xhci, slotid, i, octx+(32*i), ep_ctx); 1990 if (res != CC_SUCCESS) { 1991 return res; 1992 } 1993 DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n", 1994 i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2], 1995 ep_ctx[3], ep_ctx[4]); 1996 pci_dma_write(&xhci->pci_dev, octx+(32*i), ep_ctx, sizeof(ep_ctx)); 1997 } 1998 } 1999 2000 slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT); 2001 slot_ctx[3] |= SLOT_CONFIGURED << SLOT_STATE_SHIFT; 2002 slot_ctx[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK << SLOT_CONTEXT_ENTRIES_SHIFT); 2003 slot_ctx[0] |= islot_ctx[0] & (SLOT_CONTEXT_ENTRIES_MASK << 2004 SLOT_CONTEXT_ENTRIES_SHIFT); 2005 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", 2006 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); 2007 2008 pci_dma_write(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 2009 2010 return CC_SUCCESS; 2011 } 2012 2013 2014 static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid, 2015 uint64_t pictx) 2016 { 2017 dma_addr_t ictx, octx; 2018 uint32_t ictl_ctx[2]; 2019 uint32_t iep0_ctx[5]; 2020 uint32_t ep0_ctx[5]; 2021 uint32_t islot_ctx[4]; 2022 uint32_t slot_ctx[4]; 2023 2024 trace_usb_xhci_slot_evaluate(slotid); 2025 assert(slotid >= 1 && slotid <= MAXSLOTS); 2026 2027 ictx = xhci_mask64(pictx); 2028 octx = xhci->slots[slotid-1].ctx; 2029 2030 DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); 2031 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); 2032 2033 pci_dma_read(&xhci->pci_dev, ictx, ictl_ctx, sizeof(ictl_ctx)); 2034 2035 if (ictl_ctx[0] != 0x0 || ictl_ctx[1] & ~0x3) { 2036 fprintf(stderr, "xhci: invalid input context control %08x %08x\n", 2037 ictl_ctx[0], ictl_ctx[1]); 2038 return CC_TRB_ERROR; 2039 } 2040 2041 if (ictl_ctx[1] & 0x1) { 2042 pci_dma_read(&xhci->pci_dev, ictx+32, islot_ctx, sizeof(islot_ctx)); 2043 2044 DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n", 2045 islot_ctx[0], islot_ctx[1], islot_ctx[2], islot_ctx[3]); 2046 2047 pci_dma_read(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 2048 2049 slot_ctx[1] &= ~0xFFFF; /* max exit latency */ 2050 slot_ctx[1] |= islot_ctx[1] & 0xFFFF; 2051 slot_ctx[2] &= ~0xFF00000; /* interrupter target */ 2052 slot_ctx[2] |= islot_ctx[2] & 0xFF000000; 2053 2054 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", 2055 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); 2056 2057 pci_dma_write(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 2058 } 2059 2060 if (ictl_ctx[1] & 0x2) { 2061 pci_dma_read(&xhci->pci_dev, ictx+64, iep0_ctx, sizeof(iep0_ctx)); 2062 2063 DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n", 2064 iep0_ctx[0], iep0_ctx[1], iep0_ctx[2], 2065 iep0_ctx[3], iep0_ctx[4]); 2066 2067 pci_dma_read(&xhci->pci_dev, octx+32, ep0_ctx, sizeof(ep0_ctx)); 2068 2069 ep0_ctx[1] &= ~0xFFFF0000; /* max packet size*/ 2070 ep0_ctx[1] |= iep0_ctx[1] & 0xFFFF0000; 2071 2072 DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n", 2073 ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]); 2074 2075 pci_dma_write(&xhci->pci_dev, octx+32, ep0_ctx, sizeof(ep0_ctx)); 2076 } 2077 2078 return CC_SUCCESS; 2079 } 2080 2081 static TRBCCode xhci_reset_slot(XHCIState *xhci, unsigned int slotid) 2082 { 2083 uint32_t slot_ctx[4]; 2084 dma_addr_t octx; 2085 int i; 2086 2087 trace_usb_xhci_slot_reset(slotid); 2088 assert(slotid >= 1 && slotid <= MAXSLOTS); 2089 2090 octx = xhci->slots[slotid-1].ctx; 2091 2092 DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); 2093 2094 for (i = 2; i <= 31; i++) { 2095 if (xhci->slots[slotid-1].eps[i-1]) { 2096 xhci_disable_ep(xhci, slotid, i); 2097 } 2098 } 2099 2100 pci_dma_read(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 2101 slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT); 2102 slot_ctx[3] |= SLOT_DEFAULT << SLOT_STATE_SHIFT; 2103 DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", 2104 slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); 2105 pci_dma_write(&xhci->pci_dev, octx, slot_ctx, sizeof(slot_ctx)); 2106 2107 return CC_SUCCESS; 2108 } 2109 2110 static unsigned int xhci_get_slot(XHCIState *xhci, XHCIEvent *event, XHCITRB *trb) 2111 { 2112 unsigned int slotid; 2113 slotid = (trb->control >> TRB_CR_SLOTID_SHIFT) & TRB_CR_SLOTID_MASK; 2114 if (slotid < 1 || slotid > MAXSLOTS) { 2115 fprintf(stderr, "xhci: bad slot id %d\n", slotid); 2116 event->ccode = CC_TRB_ERROR; 2117 return 0; 2118 } else if (!xhci->slots[slotid-1].enabled) { 2119 fprintf(stderr, "xhci: slot id %d not enabled\n", slotid); 2120 event->ccode = CC_SLOT_NOT_ENABLED_ERROR; 2121 return 0; 2122 } 2123 return slotid; 2124 } 2125 2126 static TRBCCode xhci_get_port_bandwidth(XHCIState *xhci, uint64_t pctx) 2127 { 2128 dma_addr_t ctx; 2129 uint8_t bw_ctx[xhci->numports+1]; 2130 2131 DPRINTF("xhci_get_port_bandwidth()\n"); 2132 2133 ctx = xhci_mask64(pctx); 2134 2135 DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT"\n", ctx); 2136 2137 /* TODO: actually implement real values here */ 2138 bw_ctx[0] = 0; 2139 memset(&bw_ctx[1], 80, xhci->numports); /* 80% */ 2140 pci_dma_write(&xhci->pci_dev, ctx, bw_ctx, sizeof(bw_ctx)); 2141 2142 return CC_SUCCESS; 2143 } 2144 2145 static uint32_t rotl(uint32_t v, unsigned count) 2146 { 2147 count &= 31; 2148 return (v << count) | (v >> (32 - count)); 2149 } 2150 2151 2152 static uint32_t xhci_nec_challenge(uint32_t hi, uint32_t lo) 2153 { 2154 uint32_t val; 2155 val = rotl(lo - 0x49434878, 32 - ((hi>>8) & 0x1F)); 2156 val += rotl(lo + 0x49434878, hi & 0x1F); 2157 val -= rotl(hi ^ 0x49434878, (lo >> 16) & 0x1F); 2158 return ~val; 2159 } 2160 2161 static void xhci_via_challenge(XHCIState *xhci, uint64_t addr) 2162 { 2163 uint32_t buf[8]; 2164 uint32_t obuf[8]; 2165 dma_addr_t paddr = xhci_mask64(addr); 2166 2167 pci_dma_read(&xhci->pci_dev, paddr, &buf, 32); 2168 2169 memcpy(obuf, buf, sizeof(obuf)); 2170 2171 if ((buf[0] & 0xff) == 2) { 2172 obuf[0] = 0x49932000 + 0x54dc200 * buf[2] + 0x7429b578 * buf[3]; 2173 obuf[0] |= (buf[2] * buf[3]) & 0xff; 2174 obuf[1] = 0x0132bb37 + 0xe89 * buf[2] + 0xf09 * buf[3]; 2175 obuf[2] = 0x0066c2e9 + 0x2091 * buf[2] + 0x19bd * buf[3]; 2176 obuf[3] = 0xd5281342 + 0x2cc9691 * buf[2] + 0x2367662 * buf[3]; 2177 obuf[4] = 0x0123c75c + 0x1595 * buf[2] + 0x19ec * buf[3]; 2178 obuf[5] = 0x00f695de + 0x26fd * buf[2] + 0x3e9 * buf[3]; 2179 obuf[6] = obuf[2] ^ obuf[3] ^ 0x29472956; 2180 obuf[7] = obuf[2] ^ obuf[3] ^ 0x65866593; 2181 } 2182 2183 pci_dma_write(&xhci->pci_dev, paddr, &obuf, 32); 2184 } 2185 2186 static void xhci_process_commands(XHCIState *xhci) 2187 { 2188 XHCITRB trb; 2189 TRBType type; 2190 XHCIEvent event = {ER_COMMAND_COMPLETE, CC_SUCCESS}; 2191 dma_addr_t addr; 2192 unsigned int i, slotid = 0; 2193 2194 DPRINTF("xhci_process_commands()\n"); 2195 if (!xhci_running(xhci)) { 2196 DPRINTF("xhci_process_commands() called while xHC stopped or paused\n"); 2197 return; 2198 } 2199 2200 xhci->crcr_low |= CRCR_CRR; 2201 2202 while ((type = xhci_ring_fetch(xhci, &xhci->cmd_ring, &trb, &addr))) { 2203 event.ptr = addr; 2204 switch (type) { 2205 case CR_ENABLE_SLOT: 2206 for (i = 0; i < MAXSLOTS; i++) { 2207 if (!xhci->slots[i].enabled) { 2208 break; 2209 } 2210 } 2211 if (i >= MAXSLOTS) { 2212 fprintf(stderr, "xhci: no device slots available\n"); 2213 event.ccode = CC_NO_SLOTS_ERROR; 2214 } else { 2215 slotid = i+1; 2216 event.ccode = xhci_enable_slot(xhci, slotid); 2217 } 2218 break; 2219 case CR_DISABLE_SLOT: 2220 slotid = xhci_get_slot(xhci, &event, &trb); 2221 if (slotid) { 2222 event.ccode = xhci_disable_slot(xhci, slotid); 2223 } 2224 break; 2225 case CR_ADDRESS_DEVICE: 2226 slotid = xhci_get_slot(xhci, &event, &trb); 2227 if (slotid) { 2228 event.ccode = xhci_address_slot(xhci, slotid, trb.parameter, 2229 trb.control & TRB_CR_BSR); 2230 } 2231 break; 2232 case CR_CONFIGURE_ENDPOINT: 2233 slotid = xhci_get_slot(xhci, &event, &trb); 2234 if (slotid) { 2235 event.ccode = xhci_configure_slot(xhci, slotid, trb.parameter, 2236 trb.control & TRB_CR_DC); 2237 } 2238 break; 2239 case CR_EVALUATE_CONTEXT: 2240 slotid = xhci_get_slot(xhci, &event, &trb); 2241 if (slotid) { 2242 event.ccode = xhci_evaluate_slot(xhci, slotid, trb.parameter); 2243 } 2244 break; 2245 case CR_STOP_ENDPOINT: 2246 slotid = xhci_get_slot(xhci, &event, &trb); 2247 if (slotid) { 2248 unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT) 2249 & TRB_CR_EPID_MASK; 2250 event.ccode = xhci_stop_ep(xhci, slotid, epid); 2251 } 2252 break; 2253 case CR_RESET_ENDPOINT: 2254 slotid = xhci_get_slot(xhci, &event, &trb); 2255 if (slotid) { 2256 unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT) 2257 & TRB_CR_EPID_MASK; 2258 event.ccode = xhci_reset_ep(xhci, slotid, epid); 2259 } 2260 break; 2261 case CR_SET_TR_DEQUEUE: 2262 slotid = xhci_get_slot(xhci, &event, &trb); 2263 if (slotid) { 2264 unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT) 2265 & TRB_CR_EPID_MASK; 2266 event.ccode = xhci_set_ep_dequeue(xhci, slotid, epid, 2267 trb.parameter); 2268 } 2269 break; 2270 case CR_RESET_DEVICE: 2271 slotid = xhci_get_slot(xhci, &event, &trb); 2272 if (slotid) { 2273 event.ccode = xhci_reset_slot(xhci, slotid); 2274 } 2275 break; 2276 case CR_GET_PORT_BANDWIDTH: 2277 event.ccode = xhci_get_port_bandwidth(xhci, trb.parameter); 2278 break; 2279 case CR_VENDOR_VIA_CHALLENGE_RESPONSE: 2280 xhci_via_challenge(xhci, trb.parameter); 2281 break; 2282 case CR_VENDOR_NEC_FIRMWARE_REVISION: 2283 event.type = 48; /* NEC reply */ 2284 event.length = 0x3025; 2285 break; 2286 case CR_VENDOR_NEC_CHALLENGE_RESPONSE: 2287 { 2288 uint32_t chi = trb.parameter >> 32; 2289 uint32_t clo = trb.parameter; 2290 uint32_t val = xhci_nec_challenge(chi, clo); 2291 event.length = val & 0xFFFF; 2292 event.epid = val >> 16; 2293 slotid = val >> 24; 2294 event.type = 48; /* NEC reply */ 2295 } 2296 break; 2297 default: 2298 fprintf(stderr, "xhci: unimplemented command %d\n", type); 2299 event.ccode = CC_TRB_ERROR; 2300 break; 2301 } 2302 event.slotid = slotid; 2303 xhci_event(xhci, &event, 0); 2304 } 2305 } 2306 2307 static void xhci_update_port(XHCIState *xhci, XHCIPort *port, int is_detach) 2308 { 2309 port->portsc = PORTSC_PP; 2310 if (port->uport->dev && port->uport->dev->attached && !is_detach && 2311 (1 << port->uport->dev->speed) & port->speedmask) { 2312 port->portsc |= PORTSC_CCS; 2313 switch (port->uport->dev->speed) { 2314 case USB_SPEED_LOW: 2315 port->portsc |= PORTSC_SPEED_LOW; 2316 break; 2317 case USB_SPEED_FULL: 2318 port->portsc |= PORTSC_SPEED_FULL; 2319 break; 2320 case USB_SPEED_HIGH: 2321 port->portsc |= PORTSC_SPEED_HIGH; 2322 break; 2323 case USB_SPEED_SUPER: 2324 port->portsc |= PORTSC_SPEED_SUPER; 2325 break; 2326 } 2327 } 2328 2329 if (xhci_running(xhci)) { 2330 port->portsc |= PORTSC_CSC; 2331 XHCIEvent ev = { ER_PORT_STATUS_CHANGE, CC_SUCCESS, 2332 port->portnr << 24}; 2333 xhci_event(xhci, &ev, 0); 2334 DPRINTF("xhci: port change event for port %d\n", port->portnr); 2335 } 2336 } 2337 2338 static void xhci_reset(DeviceState *dev) 2339 { 2340 XHCIState *xhci = DO_UPCAST(XHCIState, pci_dev.qdev, dev); 2341 int i; 2342 2343 trace_usb_xhci_reset(); 2344 if (!(xhci->usbsts & USBSTS_HCH)) { 2345 fprintf(stderr, "xhci: reset while running!\n"); 2346 } 2347 2348 xhci->usbcmd = 0; 2349 xhci->usbsts = USBSTS_HCH; 2350 xhci->dnctrl = 0; 2351 xhci->crcr_low = 0; 2352 xhci->crcr_high = 0; 2353 xhci->dcbaap_low = 0; 2354 xhci->dcbaap_high = 0; 2355 xhci->config = 0; 2356 xhci->devaddr = 2; 2357 2358 for (i = 0; i < MAXSLOTS; i++) { 2359 xhci_disable_slot(xhci, i+1); 2360 } 2361 2362 for (i = 0; i < xhci->numports; i++) { 2363 xhci_update_port(xhci, xhci->ports + i, 0); 2364 } 2365 2366 for (i = 0; i < MAXINTRS; i++) { 2367 xhci->intr[i].iman = 0; 2368 xhci->intr[i].imod = 0; 2369 xhci->intr[i].erstsz = 0; 2370 xhci->intr[i].erstba_low = 0; 2371 xhci->intr[i].erstba_high = 0; 2372 xhci->intr[i].erdp_low = 0; 2373 xhci->intr[i].erdp_high = 0; 2374 xhci->intr[i].msix_used = 0; 2375 2376 xhci->intr[i].er_ep_idx = 0; 2377 xhci->intr[i].er_pcs = 1; 2378 xhci->intr[i].er_full = 0; 2379 xhci->intr[i].ev_buffer_put = 0; 2380 xhci->intr[i].ev_buffer_get = 0; 2381 } 2382 2383 xhci->mfindex_start = qemu_get_clock_ns(vm_clock); 2384 xhci_mfwrap_update(xhci); 2385 } 2386 2387 static uint64_t xhci_cap_read(void *ptr, hwaddr reg, unsigned size) 2388 { 2389 XHCIState *xhci = ptr; 2390 uint32_t ret; 2391 2392 switch (reg) { 2393 case 0x00: /* HCIVERSION, CAPLENGTH */ 2394 ret = 0x01000000 | LEN_CAP; 2395 break; 2396 case 0x04: /* HCSPARAMS 1 */ 2397 ret = ((xhci->numports_2+xhci->numports_3)<<24) 2398 | (MAXINTRS<<8) | MAXSLOTS; 2399 break; 2400 case 0x08: /* HCSPARAMS 2 */ 2401 ret = 0x0000000f; 2402 break; 2403 case 0x0c: /* HCSPARAMS 3 */ 2404 ret = 0x00000000; 2405 break; 2406 case 0x10: /* HCCPARAMS */ 2407 if (sizeof(dma_addr_t) == 4) { 2408 ret = 0x00081000; 2409 } else { 2410 ret = 0x00081001; 2411 } 2412 break; 2413 case 0x14: /* DBOFF */ 2414 ret = OFF_DOORBELL; 2415 break; 2416 case 0x18: /* RTSOFF */ 2417 ret = OFF_RUNTIME; 2418 break; 2419 2420 /* extended capabilities */ 2421 case 0x20: /* Supported Protocol:00 */ 2422 ret = 0x02000402; /* USB 2.0 */ 2423 break; 2424 case 0x24: /* Supported Protocol:04 */ 2425 ret = 0x20425455; /* "USB " */ 2426 break; 2427 case 0x28: /* Supported Protocol:08 */ 2428 ret = 0x00000001 | (xhci->numports_2<<8); 2429 break; 2430 case 0x2c: /* Supported Protocol:0c */ 2431 ret = 0x00000000; /* reserved */ 2432 break; 2433 case 0x30: /* Supported Protocol:00 */ 2434 ret = 0x03000002; /* USB 3.0 */ 2435 break; 2436 case 0x34: /* Supported Protocol:04 */ 2437 ret = 0x20425455; /* "USB " */ 2438 break; 2439 case 0x38: /* Supported Protocol:08 */ 2440 ret = 0x00000000 | (xhci->numports_2+1) | (xhci->numports_3<<8); 2441 break; 2442 case 0x3c: /* Supported Protocol:0c */ 2443 ret = 0x00000000; /* reserved */ 2444 break; 2445 default: 2446 fprintf(stderr, "xhci_cap_read: reg %d unimplemented\n", (int)reg); 2447 ret = 0; 2448 } 2449 2450 trace_usb_xhci_cap_read(reg, ret); 2451 return ret; 2452 } 2453 2454 static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size) 2455 { 2456 XHCIPort *port = ptr; 2457 uint32_t ret; 2458 2459 switch (reg) { 2460 case 0x00: /* PORTSC */ 2461 ret = port->portsc; 2462 break; 2463 case 0x04: /* PORTPMSC */ 2464 case 0x08: /* PORTLI */ 2465 ret = 0; 2466 break; 2467 case 0x0c: /* reserved */ 2468 default: 2469 fprintf(stderr, "xhci_port_read (port %d): reg 0x%x unimplemented\n", 2470 port->portnr, (uint32_t)reg); 2471 ret = 0; 2472 } 2473 2474 trace_usb_xhci_port_read(port->portnr, reg, ret); 2475 return ret; 2476 } 2477 2478 static void xhci_port_write(void *ptr, hwaddr reg, 2479 uint64_t val, unsigned size) 2480 { 2481 XHCIPort *port = ptr; 2482 uint32_t portsc; 2483 2484 trace_usb_xhci_port_write(port->portnr, reg, val); 2485 2486 switch (reg) { 2487 case 0x00: /* PORTSC */ 2488 portsc = port->portsc; 2489 /* write-1-to-clear bits*/ 2490 portsc &= ~(val & (PORTSC_CSC|PORTSC_PEC|PORTSC_WRC|PORTSC_OCC| 2491 PORTSC_PRC|PORTSC_PLC|PORTSC_CEC)); 2492 if (val & PORTSC_LWS) { 2493 /* overwrite PLS only when LWS=1 */ 2494 portsc &= ~(PORTSC_PLS_MASK << PORTSC_PLS_SHIFT); 2495 portsc |= val & (PORTSC_PLS_MASK << PORTSC_PLS_SHIFT); 2496 } 2497 /* read/write bits */ 2498 portsc &= ~(PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE); 2499 portsc |= (val & (PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE)); 2500 /* write-1-to-start bits */ 2501 if (val & PORTSC_PR) { 2502 DPRINTF("xhci: port %d reset\n", port); 2503 usb_device_reset(port->uport->dev); 2504 portsc |= PORTSC_PRC | PORTSC_PED; 2505 } 2506 port->portsc = portsc; 2507 break; 2508 case 0x04: /* PORTPMSC */ 2509 case 0x08: /* PORTLI */ 2510 default: 2511 fprintf(stderr, "xhci_port_write (port %d): reg 0x%x unimplemented\n", 2512 port->portnr, (uint32_t)reg); 2513 } 2514 } 2515 2516 static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size) 2517 { 2518 XHCIState *xhci = ptr; 2519 uint32_t ret; 2520 2521 switch (reg) { 2522 case 0x00: /* USBCMD */ 2523 ret = xhci->usbcmd; 2524 break; 2525 case 0x04: /* USBSTS */ 2526 ret = xhci->usbsts; 2527 break; 2528 case 0x08: /* PAGESIZE */ 2529 ret = 1; /* 4KiB */ 2530 break; 2531 case 0x14: /* DNCTRL */ 2532 ret = xhci->dnctrl; 2533 break; 2534 case 0x18: /* CRCR low */ 2535 ret = xhci->crcr_low & ~0xe; 2536 break; 2537 case 0x1c: /* CRCR high */ 2538 ret = xhci->crcr_high; 2539 break; 2540 case 0x30: /* DCBAAP low */ 2541 ret = xhci->dcbaap_low; 2542 break; 2543 case 0x34: /* DCBAAP high */ 2544 ret = xhci->dcbaap_high; 2545 break; 2546 case 0x38: /* CONFIG */ 2547 ret = xhci->config; 2548 break; 2549 default: 2550 fprintf(stderr, "xhci_oper_read: reg 0x%x unimplemented\n", (int)reg); 2551 ret = 0; 2552 } 2553 2554 trace_usb_xhci_oper_read(reg, ret); 2555 return ret; 2556 } 2557 2558 static void xhci_oper_write(void *ptr, hwaddr reg, 2559 uint64_t val, unsigned size) 2560 { 2561 XHCIState *xhci = ptr; 2562 2563 trace_usb_xhci_oper_write(reg, val); 2564 2565 switch (reg) { 2566 case 0x00: /* USBCMD */ 2567 if ((val & USBCMD_RS) && !(xhci->usbcmd & USBCMD_RS)) { 2568 xhci_run(xhci); 2569 } else if (!(val & USBCMD_RS) && (xhci->usbcmd & USBCMD_RS)) { 2570 xhci_stop(xhci); 2571 } 2572 xhci->usbcmd = val & 0xc0f; 2573 xhci_mfwrap_update(xhci); 2574 if (val & USBCMD_HCRST) { 2575 xhci_reset(&xhci->pci_dev.qdev); 2576 } 2577 xhci_intx_update(xhci); 2578 break; 2579 2580 case 0x04: /* USBSTS */ 2581 /* these bits are write-1-to-clear */ 2582 xhci->usbsts &= ~(val & (USBSTS_HSE|USBSTS_EINT|USBSTS_PCD|USBSTS_SRE)); 2583 xhci_intx_update(xhci); 2584 break; 2585 2586 case 0x14: /* DNCTRL */ 2587 xhci->dnctrl = val & 0xffff; 2588 break; 2589 case 0x18: /* CRCR low */ 2590 xhci->crcr_low = (val & 0xffffffcf) | (xhci->crcr_low & CRCR_CRR); 2591 break; 2592 case 0x1c: /* CRCR high */ 2593 xhci->crcr_high = val; 2594 if (xhci->crcr_low & (CRCR_CA|CRCR_CS) && (xhci->crcr_low & CRCR_CRR)) { 2595 XHCIEvent event = {ER_COMMAND_COMPLETE, CC_COMMAND_RING_STOPPED}; 2596 xhci->crcr_low &= ~CRCR_CRR; 2597 xhci_event(xhci, &event, 0); 2598 DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci->crcr_low); 2599 } else { 2600 dma_addr_t base = xhci_addr64(xhci->crcr_low & ~0x3f, val); 2601 xhci_ring_init(xhci, &xhci->cmd_ring, base); 2602 } 2603 xhci->crcr_low &= ~(CRCR_CA | CRCR_CS); 2604 break; 2605 case 0x30: /* DCBAAP low */ 2606 xhci->dcbaap_low = val & 0xffffffc0; 2607 break; 2608 case 0x34: /* DCBAAP high */ 2609 xhci->dcbaap_high = val; 2610 break; 2611 case 0x38: /* CONFIG */ 2612 xhci->config = val & 0xff; 2613 break; 2614 default: 2615 fprintf(stderr, "xhci_oper_write: reg 0x%x unimplemented\n", (int)reg); 2616 } 2617 } 2618 2619 static uint64_t xhci_runtime_read(void *ptr, hwaddr reg, 2620 unsigned size) 2621 { 2622 XHCIState *xhci = ptr; 2623 uint32_t ret = 0; 2624 2625 if (reg < 0x20) { 2626 switch (reg) { 2627 case 0x00: /* MFINDEX */ 2628 ret = xhci_mfindex_get(xhci) & 0x3fff; 2629 break; 2630 default: 2631 fprintf(stderr, "xhci_runtime_read: reg 0x%x unimplemented\n", 2632 (int)reg); 2633 break; 2634 } 2635 } else { 2636 int v = (reg - 0x20) / 0x20; 2637 XHCIInterrupter *intr = &xhci->intr[v]; 2638 switch (reg & 0x1f) { 2639 case 0x00: /* IMAN */ 2640 ret = intr->iman; 2641 break; 2642 case 0x04: /* IMOD */ 2643 ret = intr->imod; 2644 break; 2645 case 0x08: /* ERSTSZ */ 2646 ret = intr->erstsz; 2647 break; 2648 case 0x10: /* ERSTBA low */ 2649 ret = intr->erstba_low; 2650 break; 2651 case 0x14: /* ERSTBA high */ 2652 ret = intr->erstba_high; 2653 break; 2654 case 0x18: /* ERDP low */ 2655 ret = intr->erdp_low; 2656 break; 2657 case 0x1c: /* ERDP high */ 2658 ret = intr->erdp_high; 2659 break; 2660 } 2661 } 2662 2663 trace_usb_xhci_runtime_read(reg, ret); 2664 return ret; 2665 } 2666 2667 static void xhci_runtime_write(void *ptr, hwaddr reg, 2668 uint64_t val, unsigned size) 2669 { 2670 XHCIState *xhci = ptr; 2671 int v = (reg - 0x20) / 0x20; 2672 XHCIInterrupter *intr = &xhci->intr[v]; 2673 trace_usb_xhci_runtime_write(reg, val); 2674 2675 if (reg < 0x20) { 2676 fprintf(stderr, "%s: reg 0x%x unimplemented\n", __func__, (int)reg); 2677 return; 2678 } 2679 2680 switch (reg & 0x1f) { 2681 case 0x00: /* IMAN */ 2682 if (val & IMAN_IP) { 2683 intr->iman &= ~IMAN_IP; 2684 } 2685 intr->iman &= ~IMAN_IE; 2686 intr->iman |= val & IMAN_IE; 2687 if (v == 0) { 2688 xhci_intx_update(xhci); 2689 } 2690 xhci_msix_update(xhci, v); 2691 break; 2692 case 0x04: /* IMOD */ 2693 intr->imod = val; 2694 break; 2695 case 0x08: /* ERSTSZ */ 2696 intr->erstsz = val & 0xffff; 2697 break; 2698 case 0x10: /* ERSTBA low */ 2699 /* XXX NEC driver bug: it doesn't align this to 64 bytes 2700 intr->erstba_low = val & 0xffffffc0; */ 2701 intr->erstba_low = val & 0xfffffff0; 2702 break; 2703 case 0x14: /* ERSTBA high */ 2704 intr->erstba_high = val; 2705 xhci_er_reset(xhci, v); 2706 break; 2707 case 0x18: /* ERDP low */ 2708 if (val & ERDP_EHB) { 2709 intr->erdp_low &= ~ERDP_EHB; 2710 } 2711 intr->erdp_low = (val & ~ERDP_EHB) | (intr->erdp_low & ERDP_EHB); 2712 break; 2713 case 0x1c: /* ERDP high */ 2714 intr->erdp_high = val; 2715 xhci_events_update(xhci, v); 2716 break; 2717 default: 2718 fprintf(stderr, "xhci_oper_write: reg 0x%x unimplemented\n", 2719 (int)reg); 2720 } 2721 } 2722 2723 static uint64_t xhci_doorbell_read(void *ptr, hwaddr reg, 2724 unsigned size) 2725 { 2726 /* doorbells always read as 0 */ 2727 trace_usb_xhci_doorbell_read(reg, 0); 2728 return 0; 2729 } 2730 2731 static void xhci_doorbell_write(void *ptr, hwaddr reg, 2732 uint64_t val, unsigned size) 2733 { 2734 XHCIState *xhci = ptr; 2735 2736 trace_usb_xhci_doorbell_write(reg, val); 2737 2738 if (!xhci_running(xhci)) { 2739 fprintf(stderr, "xhci: wrote doorbell while xHC stopped or paused\n"); 2740 return; 2741 } 2742 2743 reg >>= 2; 2744 2745 if (reg == 0) { 2746 if (val == 0) { 2747 xhci_process_commands(xhci); 2748 } else { 2749 fprintf(stderr, "xhci: bad doorbell 0 write: 0x%x\n", 2750 (uint32_t)val); 2751 } 2752 } else { 2753 if (reg > MAXSLOTS) { 2754 fprintf(stderr, "xhci: bad doorbell %d\n", (int)reg); 2755 } else if (val > 31) { 2756 fprintf(stderr, "xhci: bad doorbell %d write: 0x%x\n", 2757 (int)reg, (uint32_t)val); 2758 } else { 2759 xhci_kick_ep(xhci, reg, val); 2760 } 2761 } 2762 } 2763 2764 static const MemoryRegionOps xhci_cap_ops = { 2765 .read = xhci_cap_read, 2766 .valid.min_access_size = 1, 2767 .valid.max_access_size = 4, 2768 .impl.min_access_size = 4, 2769 .impl.max_access_size = 4, 2770 .endianness = DEVICE_LITTLE_ENDIAN, 2771 }; 2772 2773 static const MemoryRegionOps xhci_oper_ops = { 2774 .read = xhci_oper_read, 2775 .write = xhci_oper_write, 2776 .valid.min_access_size = 4, 2777 .valid.max_access_size = 4, 2778 .endianness = DEVICE_LITTLE_ENDIAN, 2779 }; 2780 2781 static const MemoryRegionOps xhci_port_ops = { 2782 .read = xhci_port_read, 2783 .write = xhci_port_write, 2784 .valid.min_access_size = 4, 2785 .valid.max_access_size = 4, 2786 .endianness = DEVICE_LITTLE_ENDIAN, 2787 }; 2788 2789 static const MemoryRegionOps xhci_runtime_ops = { 2790 .read = xhci_runtime_read, 2791 .write = xhci_runtime_write, 2792 .valid.min_access_size = 4, 2793 .valid.max_access_size = 4, 2794 .endianness = DEVICE_LITTLE_ENDIAN, 2795 }; 2796 2797 static const MemoryRegionOps xhci_doorbell_ops = { 2798 .read = xhci_doorbell_read, 2799 .write = xhci_doorbell_write, 2800 .valid.min_access_size = 4, 2801 .valid.max_access_size = 4, 2802 .endianness = DEVICE_LITTLE_ENDIAN, 2803 }; 2804 2805 static void xhci_attach(USBPort *usbport) 2806 { 2807 XHCIState *xhci = usbport->opaque; 2808 XHCIPort *port = xhci_lookup_port(xhci, usbport); 2809 2810 xhci_update_port(xhci, port, 0); 2811 } 2812 2813 static void xhci_detach(USBPort *usbport) 2814 { 2815 XHCIState *xhci = usbport->opaque; 2816 XHCIPort *port = xhci_lookup_port(xhci, usbport); 2817 2818 xhci_update_port(xhci, port, 1); 2819 } 2820 2821 static void xhci_wakeup(USBPort *usbport) 2822 { 2823 XHCIState *xhci = usbport->opaque; 2824 XHCIPort *port = xhci_lookup_port(xhci, usbport); 2825 XHCIEvent ev = { ER_PORT_STATUS_CHANGE, CC_SUCCESS, 2826 port->portnr << 24}; 2827 uint32_t pls; 2828 2829 pls = (port->portsc >> PORTSC_PLS_SHIFT) & PORTSC_PLS_MASK; 2830 if (pls != 3) { 2831 return; 2832 } 2833 port->portsc |= 0xf << PORTSC_PLS_SHIFT; 2834 if (port->portsc & PORTSC_PLC) { 2835 return; 2836 } 2837 port->portsc |= PORTSC_PLC; 2838 xhci_event(xhci, &ev, 0); 2839 } 2840 2841 static void xhci_complete(USBPort *port, USBPacket *packet) 2842 { 2843 XHCITransfer *xfer = container_of(packet, XHCITransfer, packet); 2844 2845 if (packet->result == USB_RET_REMOVE_FROM_QUEUE) { 2846 xhci_ep_nuke_one_xfer(xfer); 2847 return; 2848 } 2849 xhci_complete_packet(xfer, packet->result); 2850 xhci_kick_ep(xfer->xhci, xfer->slotid, xfer->epid); 2851 } 2852 2853 static void xhci_child_detach(USBPort *uport, USBDevice *child) 2854 { 2855 USBBus *bus = usb_bus_from_device(child); 2856 XHCIState *xhci = container_of(bus, XHCIState, bus); 2857 int i; 2858 2859 for (i = 0; i < MAXSLOTS; i++) { 2860 if (xhci->slots[i].uport == uport) { 2861 xhci->slots[i].uport = NULL; 2862 } 2863 } 2864 } 2865 2866 static USBPortOps xhci_uport_ops = { 2867 .attach = xhci_attach, 2868 .detach = xhci_detach, 2869 .wakeup = xhci_wakeup, 2870 .complete = xhci_complete, 2871 .child_detach = xhci_child_detach, 2872 }; 2873 2874 static int xhci_find_slotid(XHCIState *xhci, USBDevice *dev) 2875 { 2876 XHCISlot *slot; 2877 int slotid; 2878 2879 for (slotid = 1; slotid <= MAXSLOTS; slotid++) { 2880 slot = &xhci->slots[slotid-1]; 2881 if (slot->devaddr == dev->addr) { 2882 return slotid; 2883 } 2884 } 2885 return 0; 2886 } 2887 2888 static int xhci_find_epid(USBEndpoint *ep) 2889 { 2890 if (ep->nr == 0) { 2891 return 1; 2892 } 2893 if (ep->pid == USB_TOKEN_IN) { 2894 return ep->nr * 2 + 1; 2895 } else { 2896 return ep->nr * 2; 2897 } 2898 } 2899 2900 static void xhci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep) 2901 { 2902 XHCIState *xhci = container_of(bus, XHCIState, bus); 2903 int slotid; 2904 2905 DPRINTF("%s\n", __func__); 2906 slotid = xhci_find_slotid(xhci, ep->dev); 2907 if (slotid == 0 || !xhci->slots[slotid-1].enabled) { 2908 DPRINTF("%s: oops, no slot for dev %d\n", __func__, ep->dev->addr); 2909 return; 2910 } 2911 xhci_kick_ep(xhci, slotid, xhci_find_epid(ep)); 2912 } 2913 2914 static USBBusOps xhci_bus_ops = { 2915 .wakeup_endpoint = xhci_wakeup_endpoint, 2916 }; 2917 2918 static void usb_xhci_init(XHCIState *xhci, DeviceState *dev) 2919 { 2920 XHCIPort *port; 2921 int i, usbports, speedmask; 2922 2923 xhci->usbsts = USBSTS_HCH; 2924 2925 if (xhci->numports_2 > MAXPORTS_2) { 2926 xhci->numports_2 = MAXPORTS_2; 2927 } 2928 if (xhci->numports_3 > MAXPORTS_3) { 2929 xhci->numports_3 = MAXPORTS_3; 2930 } 2931 usbports = MAX(xhci->numports_2, xhci->numports_3); 2932 xhci->numports = xhci->numports_2 + xhci->numports_3; 2933 2934 usb_bus_new(&xhci->bus, &xhci_bus_ops, &xhci->pci_dev.qdev); 2935 2936 for (i = 0; i < usbports; i++) { 2937 speedmask = 0; 2938 if (i < xhci->numports_2) { 2939 port = &xhci->ports[i]; 2940 port->portnr = i + 1; 2941 port->uport = &xhci->uports[i]; 2942 port->speedmask = 2943 USB_SPEED_MASK_LOW | 2944 USB_SPEED_MASK_FULL | 2945 USB_SPEED_MASK_HIGH; 2946 snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1); 2947 speedmask |= port->speedmask; 2948 } 2949 if (i < xhci->numports_3) { 2950 port = &xhci->ports[i + xhci->numports_2]; 2951 port->portnr = i + 1 + xhci->numports_2; 2952 port->uport = &xhci->uports[i]; 2953 port->speedmask = USB_SPEED_MASK_SUPER; 2954 snprintf(port->name, sizeof(port->name), "usb3 port #%d", i+1); 2955 speedmask |= port->speedmask; 2956 } 2957 usb_register_port(&xhci->bus, &xhci->uports[i], xhci, i, 2958 &xhci_uport_ops, speedmask); 2959 } 2960 } 2961 2962 static int usb_xhci_initfn(struct PCIDevice *dev) 2963 { 2964 int i, ret; 2965 2966 XHCIState *xhci = DO_UPCAST(XHCIState, pci_dev, dev); 2967 2968 xhci->pci_dev.config[PCI_CLASS_PROG] = 0x30; /* xHCI */ 2969 xhci->pci_dev.config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin 1 */ 2970 xhci->pci_dev.config[PCI_CACHE_LINE_SIZE] = 0x10; 2971 xhci->pci_dev.config[0x60] = 0x30; /* release number */ 2972 2973 usb_xhci_init(xhci, &dev->qdev); 2974 2975 xhci->mfwrap_timer = qemu_new_timer_ns(vm_clock, xhci_mfwrap_timer, xhci); 2976 2977 xhci->irq = xhci->pci_dev.irq[0]; 2978 2979 memory_region_init(&xhci->mem, "xhci", LEN_REGS); 2980 memory_region_init_io(&xhci->mem_cap, &xhci_cap_ops, xhci, 2981 "capabilities", LEN_CAP); 2982 memory_region_init_io(&xhci->mem_oper, &xhci_oper_ops, xhci, 2983 "operational", 0x400); 2984 memory_region_init_io(&xhci->mem_runtime, &xhci_runtime_ops, xhci, 2985 "runtime", LEN_RUNTIME); 2986 memory_region_init_io(&xhci->mem_doorbell, &xhci_doorbell_ops, xhci, 2987 "doorbell", LEN_DOORBELL); 2988 2989 memory_region_add_subregion(&xhci->mem, 0, &xhci->mem_cap); 2990 memory_region_add_subregion(&xhci->mem, OFF_OPER, &xhci->mem_oper); 2991 memory_region_add_subregion(&xhci->mem, OFF_RUNTIME, &xhci->mem_runtime); 2992 memory_region_add_subregion(&xhci->mem, OFF_DOORBELL, &xhci->mem_doorbell); 2993 2994 for (i = 0; i < xhci->numports; i++) { 2995 XHCIPort *port = &xhci->ports[i]; 2996 uint32_t offset = OFF_OPER + 0x400 + 0x10 * i; 2997 port->xhci = xhci; 2998 memory_region_init_io(&port->mem, &xhci_port_ops, port, 2999 port->name, 0x10); 3000 memory_region_add_subregion(&xhci->mem, offset, &port->mem); 3001 } 3002 3003 pci_register_bar(&xhci->pci_dev, 0, 3004 PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64, 3005 &xhci->mem); 3006 3007 ret = pcie_cap_init(&xhci->pci_dev, 0xa0, PCI_EXP_TYPE_ENDPOINT, 0); 3008 assert(ret >= 0); 3009 3010 if (xhci->flags & (1 << XHCI_FLAG_USE_MSI)) { 3011 msi_init(&xhci->pci_dev, 0x70, MAXINTRS, true, false); 3012 } 3013 if (xhci->flags & (1 << XHCI_FLAG_USE_MSI_X)) { 3014 msix_init(&xhci->pci_dev, MAXINTRS, 3015 &xhci->mem, 0, OFF_MSIX_TABLE, 3016 &xhci->mem, 0, OFF_MSIX_PBA, 3017 0x90); 3018 } 3019 3020 return 0; 3021 } 3022 3023 static const VMStateDescription vmstate_xhci = { 3024 .name = "xhci", 3025 .unmigratable = 1, 3026 }; 3027 3028 static Property xhci_properties[] = { 3029 DEFINE_PROP_BIT("msi", XHCIState, flags, XHCI_FLAG_USE_MSI, true), 3030 DEFINE_PROP_BIT("msix", XHCIState, flags, XHCI_FLAG_USE_MSI_X, true), 3031 DEFINE_PROP_UINT32("p2", XHCIState, numports_2, 4), 3032 DEFINE_PROP_UINT32("p3", XHCIState, numports_3, 4), 3033 DEFINE_PROP_END_OF_LIST(), 3034 }; 3035 3036 static void xhci_class_init(ObjectClass *klass, void *data) 3037 { 3038 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 3039 DeviceClass *dc = DEVICE_CLASS(klass); 3040 3041 dc->vmsd = &vmstate_xhci; 3042 dc->props = xhci_properties; 3043 dc->reset = xhci_reset; 3044 k->init = usb_xhci_initfn; 3045 k->vendor_id = PCI_VENDOR_ID_NEC; 3046 k->device_id = PCI_DEVICE_ID_NEC_UPD720200; 3047 k->class_id = PCI_CLASS_SERIAL_USB; 3048 k->revision = 0x03; 3049 k->is_express = 1; 3050 } 3051 3052 static TypeInfo xhci_info = { 3053 .name = "nec-usb-xhci", 3054 .parent = TYPE_PCI_DEVICE, 3055 .instance_size = sizeof(XHCIState), 3056 .class_init = xhci_class_init, 3057 }; 3058 3059 static void xhci_register_types(void) 3060 { 3061 type_register_static(&xhci_info); 3062 } 3063 3064 type_init(xhci_register_types) 3065