]>
Commit | Line | Data |
---|---|---|
62c6ae04 HM |
1 | /* |
2 | * USB xHCI controller emulation | |
3 | * | |
4 | * Copyright (c) 2011 Securiforest | |
5 | * Date: 2011-05-11 ; Author: Hector Martin <[email protected]> | |
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 | */ | |
e532b2e0 | 21 | #include "qemu/osdep.h" |
f1ae32a1 | 22 | #include "hw/hw.h" |
1de7afc9 | 23 | #include "qemu/timer.h" |
94b037f2 | 24 | #include "qemu/queue.h" |
f1ae32a1 | 25 | #include "hw/usb.h" |
a2cb15b0 MT |
26 | #include "hw/pci/pci.h" |
27 | #include "hw/pci/msi.h" | |
28 | #include "hw/pci/msix.h" | |
2d754a10 | 29 | #include "trace.h" |
1108b2f8 | 30 | #include "qapi/error.h" |
62c6ae04 HM |
31 | |
32 | //#define DEBUG_XHCI | |
33 | //#define DEBUG_DATA | |
34 | ||
35 | #ifdef DEBUG_XHCI | |
36 | #define DPRINTF(...) fprintf(stderr, __VA_ARGS__) | |
37 | #else | |
38 | #define DPRINTF(...) do {} while (0) | |
39 | #endif | |
024426ac GH |
40 | #define FIXME(_msg) do { fprintf(stderr, "FIXME %s:%d %s\n", \ |
41 | __func__, __LINE__, _msg); abort(); } while (0) | |
62c6ae04 | 42 | |
d95e74ea GH |
43 | #define MAXPORTS_2 15 |
44 | #define MAXPORTS_3 15 | |
62c6ae04 | 45 | |
0846e635 | 46 | #define MAXPORTS (MAXPORTS_2+MAXPORTS_3) |
d95e74ea GH |
47 | #define MAXSLOTS 64 |
48 | #define MAXINTRS 16 | |
62c6ae04 | 49 | |
62c6ae04 | 50 | /* Very pessimistic, let's hope it's enough for all cases */ |
1fe163fe | 51 | #define EV_QUEUE (((3 * 24) + 16) * MAXSLOTS) |
62c6ae04 HM |
52 | /* Do not deliver ER Full events. NEC's driver does some things not bound |
53 | * to the specs when it gets them */ | |
54 | #define ER_FULL_HACK | |
55 | ||
05f43d44 GH |
56 | #define TRB_LINK_LIMIT 4 |
57 | ||
62c6ae04 | 58 | #define LEN_CAP 0x40 |
62c6ae04 | 59 | #define LEN_OPER (0x400 + 0x10 * MAXPORTS) |
106b214c | 60 | #define LEN_RUNTIME ((MAXINTRS + 1) * 0x20) |
62c6ae04 HM |
61 | #define LEN_DOORBELL ((MAXSLOTS + 1) * 0x20) |
62 | ||
106b214c GH |
63 | #define OFF_OPER LEN_CAP |
64 | #define OFF_RUNTIME 0x1000 | |
65 | #define OFF_DOORBELL 0x2000 | |
4c47f800 GH |
66 | #define OFF_MSIX_TABLE 0x3000 |
67 | #define OFF_MSIX_PBA 0x3800 | |
62c6ae04 | 68 | /* must be power of 2 */ |
106b214c | 69 | #define LEN_REGS 0x4000 |
62c6ae04 | 70 | |
106b214c GH |
71 | #if (OFF_OPER + LEN_OPER) > OFF_RUNTIME |
72 | #error Increase OFF_RUNTIME | |
73 | #endif | |
74 | #if (OFF_RUNTIME + LEN_RUNTIME) > OFF_DOORBELL | |
75 | #error Increase OFF_DOORBELL | |
76 | #endif | |
62c6ae04 HM |
77 | #if (OFF_DOORBELL + LEN_DOORBELL) > LEN_REGS |
78 | # error Increase LEN_REGS | |
79 | #endif | |
80 | ||
62c6ae04 HM |
81 | /* bit definitions */ |
82 | #define USBCMD_RS (1<<0) | |
83 | #define USBCMD_HCRST (1<<1) | |
84 | #define USBCMD_INTE (1<<2) | |
85 | #define USBCMD_HSEE (1<<3) | |
86 | #define USBCMD_LHCRST (1<<7) | |
87 | #define USBCMD_CSS (1<<8) | |
88 | #define USBCMD_CRS (1<<9) | |
89 | #define USBCMD_EWE (1<<10) | |
90 | #define USBCMD_EU3S (1<<11) | |
91 | ||
92 | #define USBSTS_HCH (1<<0) | |
93 | #define USBSTS_HSE (1<<2) | |
94 | #define USBSTS_EINT (1<<3) | |
95 | #define USBSTS_PCD (1<<4) | |
96 | #define USBSTS_SSS (1<<8) | |
97 | #define USBSTS_RSS (1<<9) | |
98 | #define USBSTS_SRE (1<<10) | |
99 | #define USBSTS_CNR (1<<11) | |
100 | #define USBSTS_HCE (1<<12) | |
101 | ||
102 | ||
103 | #define PORTSC_CCS (1<<0) | |
104 | #define PORTSC_PED (1<<1) | |
105 | #define PORTSC_OCA (1<<3) | |
106 | #define PORTSC_PR (1<<4) | |
107 | #define PORTSC_PLS_SHIFT 5 | |
108 | #define PORTSC_PLS_MASK 0xf | |
109 | #define PORTSC_PP (1<<9) | |
110 | #define PORTSC_SPEED_SHIFT 10 | |
111 | #define PORTSC_SPEED_MASK 0xf | |
112 | #define PORTSC_SPEED_FULL (1<<10) | |
113 | #define PORTSC_SPEED_LOW (2<<10) | |
114 | #define PORTSC_SPEED_HIGH (3<<10) | |
115 | #define PORTSC_SPEED_SUPER (4<<10) | |
116 | #define PORTSC_PIC_SHIFT 14 | |
117 | #define PORTSC_PIC_MASK 0x3 | |
118 | #define PORTSC_LWS (1<<16) | |
119 | #define PORTSC_CSC (1<<17) | |
120 | #define PORTSC_PEC (1<<18) | |
121 | #define PORTSC_WRC (1<<19) | |
122 | #define PORTSC_OCC (1<<20) | |
123 | #define PORTSC_PRC (1<<21) | |
124 | #define PORTSC_PLC (1<<22) | |
125 | #define PORTSC_CEC (1<<23) | |
126 | #define PORTSC_CAS (1<<24) | |
127 | #define PORTSC_WCE (1<<25) | |
128 | #define PORTSC_WDE (1<<26) | |
129 | #define PORTSC_WOE (1<<27) | |
130 | #define PORTSC_DR (1<<30) | |
131 | #define PORTSC_WPR (1<<31) | |
132 | ||
133 | #define CRCR_RCS (1<<0) | |
134 | #define CRCR_CS (1<<1) | |
135 | #define CRCR_CA (1<<2) | |
136 | #define CRCR_CRR (1<<3) | |
137 | ||
138 | #define IMAN_IP (1<<0) | |
139 | #define IMAN_IE (1<<1) | |
140 | ||
141 | #define ERDP_EHB (1<<3) | |
142 | ||
143 | #define TRB_SIZE 16 | |
144 | typedef struct XHCITRB { | |
145 | uint64_t parameter; | |
146 | uint32_t status; | |
147 | uint32_t control; | |
59a70ccd | 148 | dma_addr_t addr; |
62c6ae04 HM |
149 | bool ccs; |
150 | } XHCITRB; | |
151 | ||
85e05d82 GH |
152 | enum { |
153 | PLS_U0 = 0, | |
154 | PLS_U1 = 1, | |
155 | PLS_U2 = 2, | |
156 | PLS_U3 = 3, | |
157 | PLS_DISABLED = 4, | |
158 | PLS_RX_DETECT = 5, | |
159 | PLS_INACTIVE = 6, | |
160 | PLS_POLLING = 7, | |
161 | PLS_RECOVERY = 8, | |
162 | PLS_HOT_RESET = 9, | |
163 | PLS_COMPILANCE_MODE = 10, | |
164 | PLS_TEST_MODE = 11, | |
165 | PLS_RESUME = 15, | |
166 | }; | |
62c6ae04 HM |
167 | |
168 | typedef enum TRBType { | |
169 | TRB_RESERVED = 0, | |
170 | TR_NORMAL, | |
171 | TR_SETUP, | |
172 | TR_DATA, | |
173 | TR_STATUS, | |
174 | TR_ISOCH, | |
175 | TR_LINK, | |
176 | TR_EVDATA, | |
177 | TR_NOOP, | |
178 | CR_ENABLE_SLOT, | |
179 | CR_DISABLE_SLOT, | |
180 | CR_ADDRESS_DEVICE, | |
181 | CR_CONFIGURE_ENDPOINT, | |
182 | CR_EVALUATE_CONTEXT, | |
183 | CR_RESET_ENDPOINT, | |
184 | CR_STOP_ENDPOINT, | |
185 | CR_SET_TR_DEQUEUE, | |
186 | CR_RESET_DEVICE, | |
187 | CR_FORCE_EVENT, | |
188 | CR_NEGOTIATE_BW, | |
189 | CR_SET_LATENCY_TOLERANCE, | |
190 | CR_GET_PORT_BANDWIDTH, | |
191 | CR_FORCE_HEADER, | |
192 | CR_NOOP, | |
193 | ER_TRANSFER = 32, | |
194 | ER_COMMAND_COMPLETE, | |
195 | ER_PORT_STATUS_CHANGE, | |
196 | ER_BANDWIDTH_REQUEST, | |
197 | ER_DOORBELL, | |
198 | ER_HOST_CONTROLLER, | |
199 | ER_DEVICE_NOTIFICATION, | |
200 | ER_MFINDEX_WRAP, | |
201 | /* vendor specific bits */ | |
202 | CR_VENDOR_VIA_CHALLENGE_RESPONSE = 48, | |
203 | CR_VENDOR_NEC_FIRMWARE_REVISION = 49, | |
204 | CR_VENDOR_NEC_CHALLENGE_RESPONSE = 50, | |
205 | } TRBType; | |
206 | ||
207 | #define CR_LINK TR_LINK | |
208 | ||
209 | typedef enum TRBCCode { | |
210 | CC_INVALID = 0, | |
211 | CC_SUCCESS, | |
212 | CC_DATA_BUFFER_ERROR, | |
213 | CC_BABBLE_DETECTED, | |
214 | CC_USB_TRANSACTION_ERROR, | |
215 | CC_TRB_ERROR, | |
216 | CC_STALL_ERROR, | |
217 | CC_RESOURCE_ERROR, | |
218 | CC_BANDWIDTH_ERROR, | |
219 | CC_NO_SLOTS_ERROR, | |
220 | CC_INVALID_STREAM_TYPE_ERROR, | |
221 | CC_SLOT_NOT_ENABLED_ERROR, | |
222 | CC_EP_NOT_ENABLED_ERROR, | |
223 | CC_SHORT_PACKET, | |
224 | CC_RING_UNDERRUN, | |
225 | CC_RING_OVERRUN, | |
226 | CC_VF_ER_FULL, | |
227 | CC_PARAMETER_ERROR, | |
228 | CC_BANDWIDTH_OVERRUN, | |
229 | CC_CONTEXT_STATE_ERROR, | |
230 | CC_NO_PING_RESPONSE_ERROR, | |
231 | CC_EVENT_RING_FULL_ERROR, | |
232 | CC_INCOMPATIBLE_DEVICE_ERROR, | |
233 | CC_MISSED_SERVICE_ERROR, | |
234 | CC_COMMAND_RING_STOPPED, | |
235 | CC_COMMAND_ABORTED, | |
236 | CC_STOPPED, | |
237 | CC_STOPPED_LENGTH_INVALID, | |
238 | CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR = 29, | |
239 | CC_ISOCH_BUFFER_OVERRUN = 31, | |
240 | CC_EVENT_LOST_ERROR, | |
241 | CC_UNDEFINED_ERROR, | |
242 | CC_INVALID_STREAM_ID_ERROR, | |
243 | CC_SECONDARY_BANDWIDTH_ERROR, | |
244 | CC_SPLIT_TRANSACTION_ERROR | |
245 | } TRBCCode; | |
246 | ||
247 | #define TRB_C (1<<0) | |
248 | #define TRB_TYPE_SHIFT 10 | |
249 | #define TRB_TYPE_MASK 0x3f | |
250 | #define TRB_TYPE(t) (((t).control >> TRB_TYPE_SHIFT) & TRB_TYPE_MASK) | |
251 | ||
252 | #define TRB_EV_ED (1<<2) | |
253 | ||
254 | #define TRB_TR_ENT (1<<1) | |
255 | #define TRB_TR_ISP (1<<2) | |
256 | #define TRB_TR_NS (1<<3) | |
257 | #define TRB_TR_CH (1<<4) | |
258 | #define TRB_TR_IOC (1<<5) | |
259 | #define TRB_TR_IDT (1<<6) | |
260 | #define TRB_TR_TBC_SHIFT 7 | |
261 | #define TRB_TR_TBC_MASK 0x3 | |
262 | #define TRB_TR_BEI (1<<9) | |
263 | #define TRB_TR_TLBPC_SHIFT 16 | |
264 | #define TRB_TR_TLBPC_MASK 0xf | |
265 | #define TRB_TR_FRAMEID_SHIFT 20 | |
266 | #define TRB_TR_FRAMEID_MASK 0x7ff | |
267 | #define TRB_TR_SIA (1<<31) | |
268 | ||
269 | #define TRB_TR_DIR (1<<16) | |
270 | ||
271 | #define TRB_CR_SLOTID_SHIFT 24 | |
272 | #define TRB_CR_SLOTID_MASK 0xff | |
273 | #define TRB_CR_EPID_SHIFT 16 | |
274 | #define TRB_CR_EPID_MASK 0x1f | |
275 | ||
276 | #define TRB_CR_BSR (1<<9) | |
277 | #define TRB_CR_DC (1<<9) | |
278 | ||
279 | #define TRB_LK_TC (1<<1) | |
280 | ||
2d1de850 GH |
281 | #define TRB_INTR_SHIFT 22 |
282 | #define TRB_INTR_MASK 0x3ff | |
283 | #define TRB_INTR(t) (((t).status >> TRB_INTR_SHIFT) & TRB_INTR_MASK) | |
284 | ||
62c6ae04 HM |
285 | #define EP_TYPE_MASK 0x7 |
286 | #define EP_TYPE_SHIFT 3 | |
287 | ||
288 | #define EP_STATE_MASK 0x7 | |
289 | #define EP_DISABLED (0<<0) | |
290 | #define EP_RUNNING (1<<0) | |
291 | #define EP_HALTED (2<<0) | |
292 | #define EP_STOPPED (3<<0) | |
293 | #define EP_ERROR (4<<0) | |
294 | ||
295 | #define SLOT_STATE_MASK 0x1f | |
296 | #define SLOT_STATE_SHIFT 27 | |
297 | #define SLOT_STATE(s) (((s)>>SLOT_STATE_SHIFT)&SLOT_STATE_MASK) | |
298 | #define SLOT_ENABLED 0 | |
299 | #define SLOT_DEFAULT 1 | |
300 | #define SLOT_ADDRESSED 2 | |
301 | #define SLOT_CONFIGURED 3 | |
302 | ||
303 | #define SLOT_CONTEXT_ENTRIES_MASK 0x1f | |
304 | #define SLOT_CONTEXT_ENTRIES_SHIFT 27 | |
305 | ||
1d8a4e69 | 306 | typedef struct XHCIState XHCIState; |
024426ac GH |
307 | typedef struct XHCIStreamContext XHCIStreamContext; |
308 | typedef struct XHCIEPContext XHCIEPContext; | |
1d8a4e69 | 309 | |
85e05d82 GH |
310 | #define get_field(data, field) \ |
311 | (((data) >> field##_SHIFT) & field##_MASK) | |
312 | ||
313 | #define set_field(data, newval, field) do { \ | |
314 | uint32_t val = *data; \ | |
315 | val &= ~(field##_MASK << field##_SHIFT); \ | |
316 | val |= ((newval) & field##_MASK) << field##_SHIFT; \ | |
317 | *data = val; \ | |
318 | } while (0) | |
319 | ||
62c6ae04 HM |
320 | typedef enum EPType { |
321 | ET_INVALID = 0, | |
322 | ET_ISO_OUT, | |
323 | ET_BULK_OUT, | |
324 | ET_INTR_OUT, | |
325 | ET_CONTROL, | |
326 | ET_ISO_IN, | |
327 | ET_BULK_IN, | |
328 | ET_INTR_IN, | |
329 | } EPType; | |
330 | ||
331 | typedef struct XHCIRing { | |
59a70ccd | 332 | dma_addr_t dequeue; |
62c6ae04 HM |
333 | bool ccs; |
334 | } XHCIRing; | |
335 | ||
336 | typedef struct XHCIPort { | |
1d8a4e69 | 337 | XHCIState *xhci; |
62c6ae04 | 338 | uint32_t portsc; |
0846e635 GH |
339 | uint32_t portnr; |
340 | USBPort *uport; | |
341 | uint32_t speedmask; | |
1d8a4e69 GH |
342 | char name[16]; |
343 | MemoryRegion mem; | |
62c6ae04 HM |
344 | } XHCIPort; |
345 | ||
62c6ae04 | 346 | typedef struct XHCITransfer { |
94b037f2 | 347 | XHCIEPContext *epctx; |
62c6ae04 | 348 | USBPacket packet; |
d5a15814 | 349 | QEMUSGList sgl; |
7c605a23 GH |
350 | bool running_async; |
351 | bool running_retry; | |
62c6ae04 | 352 | bool complete; |
a6fb2ddb | 353 | bool int_req; |
62c6ae04 | 354 | unsigned int iso_pkts; |
024426ac | 355 | unsigned int streamid; |
62c6ae04 HM |
356 | bool in_xfer; |
357 | bool iso_xfer; | |
4d7a81c0 | 358 | bool timed_xfer; |
62c6ae04 HM |
359 | |
360 | unsigned int trb_count; | |
62c6ae04 HM |
361 | XHCITRB *trbs; |
362 | ||
62c6ae04 HM |
363 | TRBCCode status; |
364 | ||
365 | unsigned int pkts; | |
366 | unsigned int pktsize; | |
367 | unsigned int cur_pkt; | |
3d139684 GH |
368 | |
369 | uint64_t mfindex_kick; | |
94b037f2 GH |
370 | |
371 | QTAILQ_ENTRY(XHCITransfer) next; | |
62c6ae04 HM |
372 | } XHCITransfer; |
373 | ||
024426ac GH |
374 | struct XHCIStreamContext { |
375 | dma_addr_t pctx; | |
376 | unsigned int sct; | |
377 | XHCIRing ring; | |
024426ac GH |
378 | }; |
379 | ||
380 | struct XHCIEPContext { | |
3d139684 GH |
381 | XHCIState *xhci; |
382 | unsigned int slotid; | |
383 | unsigned int epid; | |
384 | ||
62c6ae04 | 385 | XHCIRing ring; |
94b037f2 GH |
386 | uint32_t xfer_count; |
387 | QTAILQ_HEAD(, XHCITransfer) transfers; | |
7c605a23 | 388 | XHCITransfer *retry; |
62c6ae04 | 389 | EPType type; |
59a70ccd | 390 | dma_addr_t pctx; |
62c6ae04 | 391 | unsigned int max_psize; |
62c6ae04 | 392 | uint32_t state; |
3d139684 | 393 | |
024426ac GH |
394 | /* streams */ |
395 | unsigned int max_pstreams; | |
396 | bool lsa; | |
397 | unsigned int nr_pstreams; | |
398 | XHCIStreamContext *pstreams; | |
399 | ||
3d139684 GH |
400 | /* iso xfer scheduling */ |
401 | unsigned int interval; | |
402 | int64_t mfindex_last; | |
403 | QEMUTimer *kick_timer; | |
024426ac | 404 | }; |
62c6ae04 HM |
405 | |
406 | typedef struct XHCISlot { | |
407 | bool enabled; | |
4034e693 | 408 | bool addressed; |
59a70ccd | 409 | dma_addr_t ctx; |
ccaf87a0 | 410 | USBPort *uport; |
62c6ae04 HM |
411 | XHCIEPContext * eps[31]; |
412 | } XHCISlot; | |
413 | ||
414 | typedef struct XHCIEvent { | |
415 | TRBType type; | |
416 | TRBCCode ccode; | |
417 | uint64_t ptr; | |
418 | uint32_t length; | |
419 | uint32_t flags; | |
420 | uint8_t slotid; | |
421 | uint8_t epid; | |
422 | } XHCIEvent; | |
423 | ||
962d11e1 GH |
424 | typedef struct XHCIInterrupter { |
425 | uint32_t iman; | |
426 | uint32_t imod; | |
427 | uint32_t erstsz; | |
428 | uint32_t erstba_low; | |
429 | uint32_t erstba_high; | |
430 | uint32_t erdp_low; | |
431 | uint32_t erdp_high; | |
432 | ||
433 | bool msix_used, er_pcs, er_full; | |
434 | ||
435 | dma_addr_t er_start; | |
436 | uint32_t er_size; | |
437 | unsigned int er_ep_idx; | |
438 | ||
439 | XHCIEvent ev_buffer[EV_QUEUE]; | |
440 | unsigned int ev_buffer_put; | |
441 | unsigned int ev_buffer_get; | |
442 | ||
443 | } XHCIInterrupter; | |
444 | ||
62c6ae04 | 445 | struct XHCIState { |
9b7d3334 AF |
446 | /*< private >*/ |
447 | PCIDevice parent_obj; | |
448 | /*< public >*/ | |
449 | ||
62c6ae04 | 450 | USBBus bus; |
62c6ae04 | 451 | MemoryRegion mem; |
1b067564 GH |
452 | MemoryRegion mem_cap; |
453 | MemoryRegion mem_oper; | |
454 | MemoryRegion mem_runtime; | |
455 | MemoryRegion mem_doorbell; | |
62c6ae04 | 456 | |
0846e635 GH |
457 | /* properties */ |
458 | uint32_t numports_2; | |
459 | uint32_t numports_3; | |
91062ae0 GH |
460 | uint32_t numintrs; |
461 | uint32_t numslots; | |
c5e9b02d | 462 | uint32_t flags; |
2aa6bfcb | 463 | uint32_t max_pstreams_mask; |
290fd20d C |
464 | OnOffAuto msi; |
465 | OnOffAuto msix; | |
0846e635 | 466 | |
62c6ae04 HM |
467 | /* Operational Registers */ |
468 | uint32_t usbcmd; | |
469 | uint32_t usbsts; | |
470 | uint32_t dnctrl; | |
471 | uint32_t crcr_low; | |
472 | uint32_t crcr_high; | |
473 | uint32_t dcbaap_low; | |
474 | uint32_t dcbaap_high; | |
475 | uint32_t config; | |
476 | ||
0846e635 | 477 | USBPort uports[MAX(MAXPORTS_2, MAXPORTS_3)]; |
62c6ae04 HM |
478 | XHCIPort ports[MAXPORTS]; |
479 | XHCISlot slots[MAXSLOTS]; | |
0846e635 | 480 | uint32_t numports; |
62c6ae04 HM |
481 | |
482 | /* Runtime Registers */ | |
01546fa6 GH |
483 | int64_t mfindex_start; |
484 | QEMUTimer *mfwrap_timer; | |
962d11e1 | 485 | XHCIInterrupter intr[MAXINTRS]; |
62c6ae04 HM |
486 | |
487 | XHCIRing cmd_ring; | |
488 | }; | |
489 | ||
37034575 PC |
490 | #define TYPE_XHCI "nec-usb-xhci" |
491 | ||
492 | #define XHCI(obj) \ | |
493 | OBJECT_CHECK(XHCIState, (obj), TYPE_XHCI) | |
494 | ||
62c6ae04 HM |
495 | typedef struct XHCIEvRingSeg { |
496 | uint32_t addr_low; | |
497 | uint32_t addr_high; | |
498 | uint32_t size; | |
499 | uint32_t rsvd; | |
500 | } XHCIEvRingSeg; | |
501 | ||
c5e9b02d | 502 | enum xhci_flags { |
290fd20d | 503 | XHCI_FLAG_SS_FIRST = 1, |
e6043e92 | 504 | XHCI_FLAG_FORCE_PCIE_ENDCAP, |
2aa6bfcb | 505 | XHCI_FLAG_ENABLE_STREAMS, |
c5e9b02d GH |
506 | }; |
507 | ||
01546fa6 | 508 | static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, |
024426ac | 509 | unsigned int epid, unsigned int streamid); |
3a533ee8 | 510 | static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid); |
0bc85da6 GH |
511 | static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid, |
512 | unsigned int epid); | |
582d6f4a | 513 | static void xhci_xfer_report(XHCITransfer *xfer); |
962d11e1 GH |
514 | static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v); |
515 | static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v); | |
070eeef9 | 516 | static USBEndpoint *xhci_epid_to_usbep(XHCIEPContext *epctx); |
01546fa6 | 517 | |
f10de44e GH |
518 | static const char *TRBType_names[] = { |
519 | [TRB_RESERVED] = "TRB_RESERVED", | |
520 | [TR_NORMAL] = "TR_NORMAL", | |
521 | [TR_SETUP] = "TR_SETUP", | |
522 | [TR_DATA] = "TR_DATA", | |
523 | [TR_STATUS] = "TR_STATUS", | |
524 | [TR_ISOCH] = "TR_ISOCH", | |
525 | [TR_LINK] = "TR_LINK", | |
526 | [TR_EVDATA] = "TR_EVDATA", | |
527 | [TR_NOOP] = "TR_NOOP", | |
528 | [CR_ENABLE_SLOT] = "CR_ENABLE_SLOT", | |
529 | [CR_DISABLE_SLOT] = "CR_DISABLE_SLOT", | |
530 | [CR_ADDRESS_DEVICE] = "CR_ADDRESS_DEVICE", | |
531 | [CR_CONFIGURE_ENDPOINT] = "CR_CONFIGURE_ENDPOINT", | |
532 | [CR_EVALUATE_CONTEXT] = "CR_EVALUATE_CONTEXT", | |
533 | [CR_RESET_ENDPOINT] = "CR_RESET_ENDPOINT", | |
534 | [CR_STOP_ENDPOINT] = "CR_STOP_ENDPOINT", | |
535 | [CR_SET_TR_DEQUEUE] = "CR_SET_TR_DEQUEUE", | |
536 | [CR_RESET_DEVICE] = "CR_RESET_DEVICE", | |
537 | [CR_FORCE_EVENT] = "CR_FORCE_EVENT", | |
538 | [CR_NEGOTIATE_BW] = "CR_NEGOTIATE_BW", | |
539 | [CR_SET_LATENCY_TOLERANCE] = "CR_SET_LATENCY_TOLERANCE", | |
540 | [CR_GET_PORT_BANDWIDTH] = "CR_GET_PORT_BANDWIDTH", | |
541 | [CR_FORCE_HEADER] = "CR_FORCE_HEADER", | |
542 | [CR_NOOP] = "CR_NOOP", | |
543 | [ER_TRANSFER] = "ER_TRANSFER", | |
544 | [ER_COMMAND_COMPLETE] = "ER_COMMAND_COMPLETE", | |
545 | [ER_PORT_STATUS_CHANGE] = "ER_PORT_STATUS_CHANGE", | |
546 | [ER_BANDWIDTH_REQUEST] = "ER_BANDWIDTH_REQUEST", | |
547 | [ER_DOORBELL] = "ER_DOORBELL", | |
548 | [ER_HOST_CONTROLLER] = "ER_HOST_CONTROLLER", | |
549 | [ER_DEVICE_NOTIFICATION] = "ER_DEVICE_NOTIFICATION", | |
550 | [ER_MFINDEX_WRAP] = "ER_MFINDEX_WRAP", | |
551 | [CR_VENDOR_VIA_CHALLENGE_RESPONSE] = "CR_VENDOR_VIA_CHALLENGE_RESPONSE", | |
552 | [CR_VENDOR_NEC_FIRMWARE_REVISION] = "CR_VENDOR_NEC_FIRMWARE_REVISION", | |
553 | [CR_VENDOR_NEC_CHALLENGE_RESPONSE] = "CR_VENDOR_NEC_CHALLENGE_RESPONSE", | |
554 | }; | |
555 | ||
873123fe GH |
556 | static const char *TRBCCode_names[] = { |
557 | [CC_INVALID] = "CC_INVALID", | |
558 | [CC_SUCCESS] = "CC_SUCCESS", | |
559 | [CC_DATA_BUFFER_ERROR] = "CC_DATA_BUFFER_ERROR", | |
560 | [CC_BABBLE_DETECTED] = "CC_BABBLE_DETECTED", | |
561 | [CC_USB_TRANSACTION_ERROR] = "CC_USB_TRANSACTION_ERROR", | |
562 | [CC_TRB_ERROR] = "CC_TRB_ERROR", | |
563 | [CC_STALL_ERROR] = "CC_STALL_ERROR", | |
564 | [CC_RESOURCE_ERROR] = "CC_RESOURCE_ERROR", | |
565 | [CC_BANDWIDTH_ERROR] = "CC_BANDWIDTH_ERROR", | |
566 | [CC_NO_SLOTS_ERROR] = "CC_NO_SLOTS_ERROR", | |
567 | [CC_INVALID_STREAM_TYPE_ERROR] = "CC_INVALID_STREAM_TYPE_ERROR", | |
568 | [CC_SLOT_NOT_ENABLED_ERROR] = "CC_SLOT_NOT_ENABLED_ERROR", | |
569 | [CC_EP_NOT_ENABLED_ERROR] = "CC_EP_NOT_ENABLED_ERROR", | |
570 | [CC_SHORT_PACKET] = "CC_SHORT_PACKET", | |
571 | [CC_RING_UNDERRUN] = "CC_RING_UNDERRUN", | |
572 | [CC_RING_OVERRUN] = "CC_RING_OVERRUN", | |
573 | [CC_VF_ER_FULL] = "CC_VF_ER_FULL", | |
574 | [CC_PARAMETER_ERROR] = "CC_PARAMETER_ERROR", | |
575 | [CC_BANDWIDTH_OVERRUN] = "CC_BANDWIDTH_OVERRUN", | |
576 | [CC_CONTEXT_STATE_ERROR] = "CC_CONTEXT_STATE_ERROR", | |
577 | [CC_NO_PING_RESPONSE_ERROR] = "CC_NO_PING_RESPONSE_ERROR", | |
578 | [CC_EVENT_RING_FULL_ERROR] = "CC_EVENT_RING_FULL_ERROR", | |
579 | [CC_INCOMPATIBLE_DEVICE_ERROR] = "CC_INCOMPATIBLE_DEVICE_ERROR", | |
580 | [CC_MISSED_SERVICE_ERROR] = "CC_MISSED_SERVICE_ERROR", | |
581 | [CC_COMMAND_RING_STOPPED] = "CC_COMMAND_RING_STOPPED", | |
582 | [CC_COMMAND_ABORTED] = "CC_COMMAND_ABORTED", | |
583 | [CC_STOPPED] = "CC_STOPPED", | |
584 | [CC_STOPPED_LENGTH_INVALID] = "CC_STOPPED_LENGTH_INVALID", | |
585 | [CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR] | |
586 | = "CC_MAX_EXIT_LATENCY_TOO_LARGE_ERROR", | |
587 | [CC_ISOCH_BUFFER_OVERRUN] = "CC_ISOCH_BUFFER_OVERRUN", | |
588 | [CC_EVENT_LOST_ERROR] = "CC_EVENT_LOST_ERROR", | |
589 | [CC_UNDEFINED_ERROR] = "CC_UNDEFINED_ERROR", | |
590 | [CC_INVALID_STREAM_ID_ERROR] = "CC_INVALID_STREAM_ID_ERROR", | |
591 | [CC_SECONDARY_BANDWIDTH_ERROR] = "CC_SECONDARY_BANDWIDTH_ERROR", | |
592 | [CC_SPLIT_TRANSACTION_ERROR] = "CC_SPLIT_TRANSACTION_ERROR", | |
593 | }; | |
594 | ||
1c82392a GH |
595 | static const char *ep_state_names[] = { |
596 | [EP_DISABLED] = "disabled", | |
597 | [EP_RUNNING] = "running", | |
598 | [EP_HALTED] = "halted", | |
599 | [EP_STOPPED] = "stopped", | |
600 | [EP_ERROR] = "error", | |
601 | }; | |
602 | ||
f10de44e GH |
603 | static const char *lookup_name(uint32_t index, const char **list, uint32_t llen) |
604 | { | |
605 | if (index >= llen || list[index] == NULL) { | |
606 | return "???"; | |
607 | } | |
608 | return list[index]; | |
609 | } | |
610 | ||
611 | static const char *trb_name(XHCITRB *trb) | |
612 | { | |
613 | return lookup_name(TRB_TYPE(*trb), TRBType_names, | |
614 | ARRAY_SIZE(TRBType_names)); | |
615 | } | |
f10de44e | 616 | |
873123fe GH |
617 | static const char *event_name(XHCIEvent *event) |
618 | { | |
619 | return lookup_name(event->ccode, TRBCCode_names, | |
620 | ARRAY_SIZE(TRBCCode_names)); | |
621 | } | |
622 | ||
1c82392a GH |
623 | static const char *ep_state_name(uint32_t state) |
624 | { | |
625 | return lookup_name(state, ep_state_names, | |
626 | ARRAY_SIZE(ep_state_names)); | |
627 | } | |
628 | ||
f9955235 GH |
629 | static bool xhci_get_flag(XHCIState *xhci, enum xhci_flags bit) |
630 | { | |
631 | return xhci->flags & (1 << bit); | |
632 | } | |
633 | ||
01546fa6 GH |
634 | static uint64_t xhci_mfindex_get(XHCIState *xhci) |
635 | { | |
bc72ad67 | 636 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
01546fa6 GH |
637 | return (now - xhci->mfindex_start) / 125000; |
638 | } | |
639 | ||
640 | static void xhci_mfwrap_update(XHCIState *xhci) | |
641 | { | |
642 | const uint32_t bits = USBCMD_RS | USBCMD_EWE; | |
643 | uint32_t mfindex, left; | |
644 | int64_t now; | |
645 | ||
646 | if ((xhci->usbcmd & bits) == bits) { | |
bc72ad67 | 647 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
01546fa6 GH |
648 | mfindex = ((now - xhci->mfindex_start) / 125000) & 0x3fff; |
649 | left = 0x4000 - mfindex; | |
bc72ad67 | 650 | timer_mod(xhci->mfwrap_timer, now + left * 125000); |
01546fa6 | 651 | } else { |
bc72ad67 | 652 | timer_del(xhci->mfwrap_timer); |
01546fa6 GH |
653 | } |
654 | } | |
655 | ||
656 | static void xhci_mfwrap_timer(void *opaque) | |
657 | { | |
658 | XHCIState *xhci = opaque; | |
659 | XHCIEvent wrap = { ER_MFINDEX_WRAP, CC_SUCCESS }; | |
660 | ||
962d11e1 | 661 | xhci_event(xhci, &wrap, 0); |
01546fa6 GH |
662 | xhci_mfwrap_update(xhci); |
663 | } | |
62c6ae04 | 664 | |
59a70ccd | 665 | static inline dma_addr_t xhci_addr64(uint32_t low, uint32_t high) |
62c6ae04 | 666 | { |
59a70ccd DG |
667 | if (sizeof(dma_addr_t) == 4) { |
668 | return low; | |
669 | } else { | |
670 | return low | (((dma_addr_t)high << 16) << 16); | |
671 | } | |
62c6ae04 HM |
672 | } |
673 | ||
59a70ccd | 674 | static inline dma_addr_t xhci_mask64(uint64_t addr) |
62c6ae04 | 675 | { |
59a70ccd DG |
676 | if (sizeof(dma_addr_t) == 4) { |
677 | return addr & 0xffffffff; | |
678 | } else { | |
679 | return addr; | |
680 | } | |
62c6ae04 HM |
681 | } |
682 | ||
616b5d53 DG |
683 | static inline void xhci_dma_read_u32s(XHCIState *xhci, dma_addr_t addr, |
684 | uint32_t *buf, size_t len) | |
685 | { | |
686 | int i; | |
687 | ||
688 | assert((len % sizeof(uint32_t)) == 0); | |
689 | ||
9b7d3334 | 690 | pci_dma_read(PCI_DEVICE(xhci), addr, buf, len); |
616b5d53 DG |
691 | |
692 | for (i = 0; i < (len / sizeof(uint32_t)); i++) { | |
693 | buf[i] = le32_to_cpu(buf[i]); | |
694 | } | |
695 | } | |
696 | ||
697 | static inline void xhci_dma_write_u32s(XHCIState *xhci, dma_addr_t addr, | |
698 | uint32_t *buf, size_t len) | |
699 | { | |
700 | int i; | |
182b391e PX |
701 | uint32_t tmp[5]; |
702 | uint32_t n = len / sizeof(uint32_t); | |
616b5d53 DG |
703 | |
704 | assert((len % sizeof(uint32_t)) == 0); | |
182b391e | 705 | assert(n <= ARRAY_SIZE(tmp)); |
616b5d53 | 706 | |
182b391e | 707 | for (i = 0; i < n; i++) { |
616b5d53 DG |
708 | tmp[i] = cpu_to_le32(buf[i]); |
709 | } | |
9b7d3334 | 710 | pci_dma_write(PCI_DEVICE(xhci), addr, tmp, len); |
616b5d53 DG |
711 | } |
712 | ||
0846e635 GH |
713 | static XHCIPort *xhci_lookup_port(XHCIState *xhci, struct USBPort *uport) |
714 | { | |
715 | int index; | |
716 | ||
717 | if (!uport->dev) { | |
718 | return NULL; | |
719 | } | |
720 | switch (uport->dev->speed) { | |
721 | case USB_SPEED_LOW: | |
722 | case USB_SPEED_FULL: | |
723 | case USB_SPEED_HIGH: | |
7bafd888 GH |
724 | if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) { |
725 | index = uport->index + xhci->numports_3; | |
726 | } else { | |
727 | index = uport->index; | |
728 | } | |
0846e635 GH |
729 | break; |
730 | case USB_SPEED_SUPER: | |
7bafd888 GH |
731 | if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) { |
732 | index = uport->index; | |
733 | } else { | |
734 | index = uport->index + xhci->numports_2; | |
735 | } | |
0846e635 GH |
736 | break; |
737 | default: | |
738 | return NULL; | |
739 | } | |
740 | return &xhci->ports[index]; | |
741 | } | |
742 | ||
4c4abe7c | 743 | static void xhci_intx_update(XHCIState *xhci) |
62c6ae04 | 744 | { |
9b7d3334 | 745 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
62c6ae04 HM |
746 | int level = 0; |
747 | ||
9b7d3334 AF |
748 | if (msix_enabled(pci_dev) || |
749 | msi_enabled(pci_dev)) { | |
4c4abe7c GH |
750 | return; |
751 | } | |
752 | ||
962d11e1 GH |
753 | if (xhci->intr[0].iman & IMAN_IP && |
754 | xhci->intr[0].iman & IMAN_IE && | |
215bff17 | 755 | xhci->usbcmd & USBCMD_INTE) { |
62c6ae04 HM |
756 | level = 1; |
757 | } | |
758 | ||
4c4abe7c | 759 | trace_usb_xhci_irq_intx(level); |
9e64f8a3 | 760 | pci_set_irq(pci_dev, level); |
4c4abe7c GH |
761 | } |
762 | ||
962d11e1 | 763 | static void xhci_msix_update(XHCIState *xhci, int v) |
4c47f800 | 764 | { |
9b7d3334 | 765 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
4c47f800 GH |
766 | bool enabled; |
767 | ||
9b7d3334 | 768 | if (!msix_enabled(pci_dev)) { |
4c47f800 GH |
769 | return; |
770 | } | |
771 | ||
962d11e1 GH |
772 | enabled = xhci->intr[v].iman & IMAN_IE; |
773 | if (enabled == xhci->intr[v].msix_used) { | |
4c47f800 GH |
774 | return; |
775 | } | |
776 | ||
777 | if (enabled) { | |
962d11e1 | 778 | trace_usb_xhci_irq_msix_use(v); |
9b7d3334 | 779 | msix_vector_use(pci_dev, v); |
962d11e1 | 780 | xhci->intr[v].msix_used = true; |
4c47f800 | 781 | } else { |
962d11e1 | 782 | trace_usb_xhci_irq_msix_unuse(v); |
9b7d3334 | 783 | msix_vector_unuse(pci_dev, v); |
962d11e1 | 784 | xhci->intr[v].msix_used = false; |
4c47f800 GH |
785 | } |
786 | } | |
787 | ||
962d11e1 | 788 | static void xhci_intr_raise(XHCIState *xhci, int v) |
4c4abe7c | 789 | { |
9b7d3334 AF |
790 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
791 | ||
962d11e1 GH |
792 | xhci->intr[v].erdp_low |= ERDP_EHB; |
793 | xhci->intr[v].iman |= IMAN_IP; | |
2cae4119 GH |
794 | xhci->usbsts |= USBSTS_EINT; |
795 | ||
962d11e1 | 796 | if (!(xhci->intr[v].iman & IMAN_IE)) { |
4c4abe7c GH |
797 | return; |
798 | } | |
799 | ||
800 | if (!(xhci->usbcmd & USBCMD_INTE)) { | |
801 | return; | |
802 | } | |
803 | ||
9b7d3334 | 804 | if (msix_enabled(pci_dev)) { |
962d11e1 | 805 | trace_usb_xhci_irq_msix(v); |
9b7d3334 | 806 | msix_notify(pci_dev, v); |
4c47f800 GH |
807 | return; |
808 | } | |
809 | ||
9b7d3334 | 810 | if (msi_enabled(pci_dev)) { |
962d11e1 | 811 | trace_usb_xhci_irq_msi(v); |
9b7d3334 | 812 | msi_notify(pci_dev, v); |
4c4abe7c | 813 | return; |
62c6ae04 | 814 | } |
4c4abe7c | 815 | |
962d11e1 GH |
816 | if (v == 0) { |
817 | trace_usb_xhci_irq_intx(1); | |
9e64f8a3 | 818 | pci_irq_assert(pci_dev); |
962d11e1 | 819 | } |
62c6ae04 HM |
820 | } |
821 | ||
822 | static inline int xhci_running(XHCIState *xhci) | |
823 | { | |
962d11e1 | 824 | return !(xhci->usbsts & USBSTS_HCH) && !xhci->intr[0].er_full; |
62c6ae04 HM |
825 | } |
826 | ||
827 | static void xhci_die(XHCIState *xhci) | |
828 | { | |
829 | xhci->usbsts |= USBSTS_HCE; | |
d6bb65fc | 830 | DPRINTF("xhci: asserted controller error\n"); |
62c6ae04 HM |
831 | } |
832 | ||
962d11e1 | 833 | static void xhci_write_event(XHCIState *xhci, XHCIEvent *event, int v) |
62c6ae04 | 834 | { |
9b7d3334 | 835 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
962d11e1 | 836 | XHCIInterrupter *intr = &xhci->intr[v]; |
62c6ae04 | 837 | XHCITRB ev_trb; |
59a70ccd | 838 | dma_addr_t addr; |
62c6ae04 HM |
839 | |
840 | ev_trb.parameter = cpu_to_le64(event->ptr); | |
841 | ev_trb.status = cpu_to_le32(event->length | (event->ccode << 24)); | |
842 | ev_trb.control = (event->slotid << 24) | (event->epid << 16) | | |
843 | event->flags | (event->type << TRB_TYPE_SHIFT); | |
962d11e1 | 844 | if (intr->er_pcs) { |
62c6ae04 HM |
845 | ev_trb.control |= TRB_C; |
846 | } | |
847 | ev_trb.control = cpu_to_le32(ev_trb.control); | |
848 | ||
962d11e1 | 849 | trace_usb_xhci_queue_event(v, intr->er_ep_idx, trb_name(&ev_trb), |
873123fe GH |
850 | event_name(event), ev_trb.parameter, |
851 | ev_trb.status, ev_trb.control); | |
62c6ae04 | 852 | |
962d11e1 | 853 | addr = intr->er_start + TRB_SIZE*intr->er_ep_idx; |
9b7d3334 | 854 | pci_dma_write(pci_dev, addr, &ev_trb, TRB_SIZE); |
62c6ae04 | 855 | |
962d11e1 GH |
856 | intr->er_ep_idx++; |
857 | if (intr->er_ep_idx >= intr->er_size) { | |
858 | intr->er_ep_idx = 0; | |
859 | intr->er_pcs = !intr->er_pcs; | |
62c6ae04 HM |
860 | } |
861 | } | |
862 | ||
962d11e1 | 863 | static void xhci_events_update(XHCIState *xhci, int v) |
62c6ae04 | 864 | { |
962d11e1 | 865 | XHCIInterrupter *intr = &xhci->intr[v]; |
59a70ccd | 866 | dma_addr_t erdp; |
62c6ae04 HM |
867 | unsigned int dp_idx; |
868 | bool do_irq = 0; | |
869 | ||
870 | if (xhci->usbsts & USBSTS_HCH) { | |
871 | return; | |
872 | } | |
873 | ||
962d11e1 GH |
874 | erdp = xhci_addr64(intr->erdp_low, intr->erdp_high); |
875 | if (erdp < intr->er_start || | |
876 | erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) { | |
d6bb65fc GH |
877 | DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp); |
878 | DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n", | |
962d11e1 | 879 | v, intr->er_start, intr->er_size); |
62c6ae04 HM |
880 | xhci_die(xhci); |
881 | return; | |
882 | } | |
962d11e1 GH |
883 | dp_idx = (erdp - intr->er_start) / TRB_SIZE; |
884 | assert(dp_idx < intr->er_size); | |
62c6ae04 HM |
885 | |
886 | /* NEC didn't read section 4.9.4 of the spec (v1.0 p139 top Note) and thus | |
887 | * deadlocks when the ER is full. Hack it by holding off events until | |
888 | * the driver decides to free at least half of the ring */ | |
962d11e1 GH |
889 | if (intr->er_full) { |
890 | int er_free = dp_idx - intr->er_ep_idx; | |
62c6ae04 | 891 | if (er_free <= 0) { |
962d11e1 | 892 | er_free += intr->er_size; |
62c6ae04 | 893 | } |
962d11e1 | 894 | if (er_free < (intr->er_size/2)) { |
62c6ae04 HM |
895 | DPRINTF("xhci_events_update(): event ring still " |
896 | "more than half full (hack)\n"); | |
897 | return; | |
898 | } | |
899 | } | |
900 | ||
962d11e1 GH |
901 | while (intr->ev_buffer_put != intr->ev_buffer_get) { |
902 | assert(intr->er_full); | |
903 | if (((intr->er_ep_idx+1) % intr->er_size) == dp_idx) { | |
62c6ae04 HM |
904 | DPRINTF("xhci_events_update(): event ring full again\n"); |
905 | #ifndef ER_FULL_HACK | |
906 | XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR}; | |
962d11e1 | 907 | xhci_write_event(xhci, &full, v); |
62c6ae04 HM |
908 | #endif |
909 | do_irq = 1; | |
910 | break; | |
911 | } | |
962d11e1 GH |
912 | XHCIEvent *event = &intr->ev_buffer[intr->ev_buffer_get]; |
913 | xhci_write_event(xhci, event, v); | |
914 | intr->ev_buffer_get++; | |
62c6ae04 | 915 | do_irq = 1; |
962d11e1 GH |
916 | if (intr->ev_buffer_get == EV_QUEUE) { |
917 | intr->ev_buffer_get = 0; | |
62c6ae04 HM |
918 | } |
919 | } | |
920 | ||
921 | if (do_irq) { | |
962d11e1 | 922 | xhci_intr_raise(xhci, v); |
62c6ae04 HM |
923 | } |
924 | ||
962d11e1 | 925 | if (intr->er_full && intr->ev_buffer_put == intr->ev_buffer_get) { |
62c6ae04 | 926 | DPRINTF("xhci_events_update(): event ring no longer full\n"); |
962d11e1 | 927 | intr->er_full = 0; |
62c6ae04 | 928 | } |
62c6ae04 HM |
929 | } |
930 | ||
962d11e1 | 931 | static void xhci_event(XHCIState *xhci, XHCIEvent *event, int v) |
62c6ae04 | 932 | { |
2d1de850 | 933 | XHCIInterrupter *intr; |
59a70ccd | 934 | dma_addr_t erdp; |
62c6ae04 HM |
935 | unsigned int dp_idx; |
936 | ||
91062ae0 GH |
937 | if (v >= xhci->numintrs) { |
938 | DPRINTF("intr nr out of range (%d >= %d)\n", v, xhci->numintrs); | |
2d1de850 GH |
939 | return; |
940 | } | |
941 | intr = &xhci->intr[v]; | |
942 | ||
962d11e1 | 943 | if (intr->er_full) { |
62c6ae04 | 944 | DPRINTF("xhci_event(): ER full, queueing\n"); |
962d11e1 | 945 | if (((intr->ev_buffer_put+1) % EV_QUEUE) == intr->ev_buffer_get) { |
d6bb65fc | 946 | DPRINTF("xhci: event queue full, dropping event!\n"); |
62c6ae04 HM |
947 | return; |
948 | } | |
962d11e1 GH |
949 | intr->ev_buffer[intr->ev_buffer_put++] = *event; |
950 | if (intr->ev_buffer_put == EV_QUEUE) { | |
951 | intr->ev_buffer_put = 0; | |
62c6ae04 HM |
952 | } |
953 | return; | |
954 | } | |
955 | ||
962d11e1 GH |
956 | erdp = xhci_addr64(intr->erdp_low, intr->erdp_high); |
957 | if (erdp < intr->er_start || | |
958 | erdp >= (intr->er_start + TRB_SIZE*intr->er_size)) { | |
d6bb65fc GH |
959 | DPRINTF("xhci: ERDP out of bounds: "DMA_ADDR_FMT"\n", erdp); |
960 | DPRINTF("xhci: ER[%d] at "DMA_ADDR_FMT" len %d\n", | |
962d11e1 | 961 | v, intr->er_start, intr->er_size); |
62c6ae04 HM |
962 | xhci_die(xhci); |
963 | return; | |
964 | } | |
965 | ||
962d11e1 GH |
966 | dp_idx = (erdp - intr->er_start) / TRB_SIZE; |
967 | assert(dp_idx < intr->er_size); | |
62c6ae04 | 968 | |
962d11e1 | 969 | if ((intr->er_ep_idx+1) % intr->er_size == dp_idx) { |
62c6ae04 HM |
970 | DPRINTF("xhci_event(): ER full, queueing\n"); |
971 | #ifndef ER_FULL_HACK | |
972 | XHCIEvent full = {ER_HOST_CONTROLLER, CC_EVENT_RING_FULL_ERROR}; | |
973 | xhci_write_event(xhci, &full); | |
974 | #endif | |
962d11e1 GH |
975 | intr->er_full = 1; |
976 | if (((intr->ev_buffer_put+1) % EV_QUEUE) == intr->ev_buffer_get) { | |
d6bb65fc | 977 | DPRINTF("xhci: event queue full, dropping event!\n"); |
62c6ae04 HM |
978 | return; |
979 | } | |
962d11e1 GH |
980 | intr->ev_buffer[intr->ev_buffer_put++] = *event; |
981 | if (intr->ev_buffer_put == EV_QUEUE) { | |
982 | intr->ev_buffer_put = 0; | |
62c6ae04 HM |
983 | } |
984 | } else { | |
962d11e1 | 985 | xhci_write_event(xhci, event, v); |
62c6ae04 HM |
986 | } |
987 | ||
962d11e1 | 988 | xhci_intr_raise(xhci, v); |
62c6ae04 HM |
989 | } |
990 | ||
991 | static void xhci_ring_init(XHCIState *xhci, XHCIRing *ring, | |
59a70ccd | 992 | dma_addr_t base) |
62c6ae04 | 993 | { |
62c6ae04 HM |
994 | ring->dequeue = base; |
995 | ring->ccs = 1; | |
996 | } | |
997 | ||
998 | static TRBType xhci_ring_fetch(XHCIState *xhci, XHCIRing *ring, XHCITRB *trb, | |
59a70ccd | 999 | dma_addr_t *addr) |
62c6ae04 | 1000 | { |
9b7d3334 | 1001 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
05f43d44 | 1002 | uint32_t link_cnt = 0; |
9b7d3334 | 1003 | |
62c6ae04 HM |
1004 | while (1) { |
1005 | TRBType type; | |
9b7d3334 | 1006 | pci_dma_read(pci_dev, ring->dequeue, trb, TRB_SIZE); |
62c6ae04 HM |
1007 | trb->addr = ring->dequeue; |
1008 | trb->ccs = ring->ccs; | |
1009 | le64_to_cpus(&trb->parameter); | |
1010 | le32_to_cpus(&trb->status); | |
1011 | le32_to_cpus(&trb->control); | |
1012 | ||
0703a4a7 GH |
1013 | trace_usb_xhci_fetch_trb(ring->dequeue, trb_name(trb), |
1014 | trb->parameter, trb->status, trb->control); | |
62c6ae04 HM |
1015 | |
1016 | if ((trb->control & TRB_C) != ring->ccs) { | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | type = TRB_TYPE(*trb); | |
1021 | ||
1022 | if (type != TR_LINK) { | |
1023 | if (addr) { | |
1024 | *addr = ring->dequeue; | |
1025 | } | |
1026 | ring->dequeue += TRB_SIZE; | |
1027 | return type; | |
1028 | } else { | |
05f43d44 GH |
1029 | if (++link_cnt > TRB_LINK_LIMIT) { |
1030 | return 0; | |
1031 | } | |
62c6ae04 HM |
1032 | ring->dequeue = xhci_mask64(trb->parameter); |
1033 | if (trb->control & TRB_LK_TC) { | |
1034 | ring->ccs = !ring->ccs; | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | static int xhci_ring_chain_length(XHCIState *xhci, const XHCIRing *ring) | |
1041 | { | |
9b7d3334 | 1042 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
62c6ae04 HM |
1043 | XHCITRB trb; |
1044 | int length = 0; | |
59a70ccd | 1045 | dma_addr_t dequeue = ring->dequeue; |
62c6ae04 HM |
1046 | bool ccs = ring->ccs; |
1047 | /* hack to bundle together the two/three TDs that make a setup transfer */ | |
1048 | bool control_td_set = 0; | |
05f43d44 | 1049 | uint32_t link_cnt = 0; |
62c6ae04 HM |
1050 | |
1051 | while (1) { | |
1052 | TRBType type; | |
9b7d3334 | 1053 | pci_dma_read(pci_dev, dequeue, &trb, TRB_SIZE); |
62c6ae04 HM |
1054 | le64_to_cpus(&trb.parameter); |
1055 | le32_to_cpus(&trb.status); | |
1056 | le32_to_cpus(&trb.control); | |
1057 | ||
62c6ae04 HM |
1058 | if ((trb.control & TRB_C) != ccs) { |
1059 | return -length; | |
1060 | } | |
1061 | ||
1062 | type = TRB_TYPE(trb); | |
1063 | ||
1064 | if (type == TR_LINK) { | |
05f43d44 GH |
1065 | if (++link_cnt > TRB_LINK_LIMIT) { |
1066 | return -length; | |
1067 | } | |
62c6ae04 HM |
1068 | dequeue = xhci_mask64(trb.parameter); |
1069 | if (trb.control & TRB_LK_TC) { | |
1070 | ccs = !ccs; | |
1071 | } | |
1072 | continue; | |
1073 | } | |
1074 | ||
1075 | length += 1; | |
1076 | dequeue += TRB_SIZE; | |
1077 | ||
1078 | if (type == TR_SETUP) { | |
1079 | control_td_set = 1; | |
1080 | } else if (type == TR_STATUS) { | |
1081 | control_td_set = 0; | |
1082 | } | |
1083 | ||
1084 | if (!control_td_set && !(trb.control & TRB_TR_CH)) { | |
1085 | return length; | |
1086 | } | |
1087 | } | |
1088 | } | |
1089 | ||
962d11e1 | 1090 | static void xhci_er_reset(XHCIState *xhci, int v) |
62c6ae04 | 1091 | { |
962d11e1 | 1092 | XHCIInterrupter *intr = &xhci->intr[v]; |
62c6ae04 HM |
1093 | XHCIEvRingSeg seg; |
1094 | ||
e099ad4b GH |
1095 | if (intr->erstsz == 0) { |
1096 | /* disabled */ | |
1097 | intr->er_start = 0; | |
1098 | intr->er_size = 0; | |
1099 | return; | |
1100 | } | |
62c6ae04 | 1101 | /* cache the (sole) event ring segment location */ |
962d11e1 | 1102 | if (intr->erstsz != 1) { |
d6bb65fc | 1103 | DPRINTF("xhci: invalid value for ERSTSZ: %d\n", intr->erstsz); |
62c6ae04 HM |
1104 | xhci_die(xhci); |
1105 | return; | |
1106 | } | |
962d11e1 | 1107 | dma_addr_t erstba = xhci_addr64(intr->erstba_low, intr->erstba_high); |
9b7d3334 | 1108 | pci_dma_read(PCI_DEVICE(xhci), erstba, &seg, sizeof(seg)); |
62c6ae04 HM |
1109 | le32_to_cpus(&seg.addr_low); |
1110 | le32_to_cpus(&seg.addr_high); | |
1111 | le32_to_cpus(&seg.size); | |
1112 | if (seg.size < 16 || seg.size > 4096) { | |
d6bb65fc | 1113 | DPRINTF("xhci: invalid value for segment size: %d\n", seg.size); |
62c6ae04 HM |
1114 | xhci_die(xhci); |
1115 | return; | |
1116 | } | |
962d11e1 GH |
1117 | intr->er_start = xhci_addr64(seg.addr_low, seg.addr_high); |
1118 | intr->er_size = seg.size; | |
62c6ae04 | 1119 | |
962d11e1 GH |
1120 | intr->er_ep_idx = 0; |
1121 | intr->er_pcs = 1; | |
1122 | intr->er_full = 0; | |
62c6ae04 | 1123 | |
962d11e1 GH |
1124 | DPRINTF("xhci: event ring[%d]:" DMA_ADDR_FMT " [%d]\n", |
1125 | v, intr->er_start, intr->er_size); | |
62c6ae04 HM |
1126 | } |
1127 | ||
1128 | static void xhci_run(XHCIState *xhci) | |
1129 | { | |
fc0ddaca | 1130 | trace_usb_xhci_run(); |
62c6ae04 | 1131 | xhci->usbsts &= ~USBSTS_HCH; |
bc72ad67 | 1132 | xhci->mfindex_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
62c6ae04 HM |
1133 | } |
1134 | ||
1135 | static void xhci_stop(XHCIState *xhci) | |
1136 | { | |
fc0ddaca | 1137 | trace_usb_xhci_stop(); |
62c6ae04 HM |
1138 | xhci->usbsts |= USBSTS_HCH; |
1139 | xhci->crcr_low &= ~CRCR_CRR; | |
1140 | } | |
1141 | ||
024426ac GH |
1142 | static XHCIStreamContext *xhci_alloc_stream_contexts(unsigned count, |
1143 | dma_addr_t base) | |
1144 | { | |
1145 | XHCIStreamContext *stctx; | |
1146 | unsigned int i; | |
1147 | ||
1148 | stctx = g_new0(XHCIStreamContext, count); | |
1149 | for (i = 0; i < count; i++) { | |
1150 | stctx[i].pctx = base + i * 16; | |
1151 | stctx[i].sct = -1; | |
1152 | } | |
1153 | return stctx; | |
1154 | } | |
1155 | ||
1156 | static void xhci_reset_streams(XHCIEPContext *epctx) | |
1157 | { | |
1158 | unsigned int i; | |
1159 | ||
1160 | for (i = 0; i < epctx->nr_pstreams; i++) { | |
1161 | epctx->pstreams[i].sct = -1; | |
024426ac GH |
1162 | } |
1163 | } | |
1164 | ||
1165 | static void xhci_alloc_streams(XHCIEPContext *epctx, dma_addr_t base) | |
1166 | { | |
1167 | assert(epctx->pstreams == NULL); | |
f90e160b | 1168 | epctx->nr_pstreams = 2 << epctx->max_pstreams; |
024426ac GH |
1169 | epctx->pstreams = xhci_alloc_stream_contexts(epctx->nr_pstreams, base); |
1170 | } | |
1171 | ||
1172 | static void xhci_free_streams(XHCIEPContext *epctx) | |
1173 | { | |
024426ac GH |
1174 | assert(epctx->pstreams != NULL); |
1175 | ||
024426ac GH |
1176 | g_free(epctx->pstreams); |
1177 | epctx->pstreams = NULL; | |
1178 | epctx->nr_pstreams = 0; | |
1179 | } | |
1180 | ||
72391da5 HG |
1181 | static int xhci_epmask_to_eps_with_streams(XHCIState *xhci, |
1182 | unsigned int slotid, | |
1183 | uint32_t epmask, | |
1184 | XHCIEPContext **epctxs, | |
1185 | USBEndpoint **eps) | |
1186 | { | |
1187 | XHCISlot *slot; | |
1188 | XHCIEPContext *epctx; | |
1189 | USBEndpoint *ep; | |
1190 | int i, j; | |
1191 | ||
1192 | assert(slotid >= 1 && slotid <= xhci->numslots); | |
1193 | ||
1194 | slot = &xhci->slots[slotid - 1]; | |
1195 | ||
1196 | for (i = 2, j = 0; i <= 31; i++) { | |
3d80365b | 1197 | if (!(epmask & (1u << i))) { |
72391da5 HG |
1198 | continue; |
1199 | } | |
1200 | ||
1201 | epctx = slot->eps[i - 1]; | |
070eeef9 | 1202 | ep = xhci_epid_to_usbep(epctx); |
72391da5 HG |
1203 | if (!epctx || !epctx->nr_pstreams || !ep) { |
1204 | continue; | |
1205 | } | |
1206 | ||
1207 | if (epctxs) { | |
1208 | epctxs[j] = epctx; | |
1209 | } | |
1210 | eps[j++] = ep; | |
1211 | } | |
1212 | return j; | |
1213 | } | |
1214 | ||
1215 | static void xhci_free_device_streams(XHCIState *xhci, unsigned int slotid, | |
1216 | uint32_t epmask) | |
1217 | { | |
1218 | USBEndpoint *eps[30]; | |
1219 | int nr_eps; | |
1220 | ||
1221 | nr_eps = xhci_epmask_to_eps_with_streams(xhci, slotid, epmask, NULL, eps); | |
1222 | if (nr_eps) { | |
1223 | usb_device_free_streams(eps[0]->dev, eps, nr_eps); | |
1224 | } | |
1225 | } | |
1226 | ||
1227 | static TRBCCode xhci_alloc_device_streams(XHCIState *xhci, unsigned int slotid, | |
1228 | uint32_t epmask) | |
1229 | { | |
1230 | XHCIEPContext *epctxs[30]; | |
1231 | USBEndpoint *eps[30]; | |
1232 | int i, r, nr_eps, req_nr_streams, dev_max_streams; | |
1233 | ||
1234 | nr_eps = xhci_epmask_to_eps_with_streams(xhci, slotid, epmask, epctxs, | |
1235 | eps); | |
1236 | if (nr_eps == 0) { | |
1237 | return CC_SUCCESS; | |
1238 | } | |
1239 | ||
1240 | req_nr_streams = epctxs[0]->nr_pstreams; | |
1241 | dev_max_streams = eps[0]->max_streams; | |
1242 | ||
1243 | for (i = 1; i < nr_eps; i++) { | |
1244 | /* | |
1245 | * HdG: I don't expect these to ever trigger, but if they do we need | |
1246 | * to come up with another solution, ie group identical endpoints | |
1247 | * together and make an usb_device_alloc_streams call per group. | |
1248 | */ | |
1249 | if (epctxs[i]->nr_pstreams != req_nr_streams) { | |
1250 | FIXME("guest streams config not identical for all eps"); | |
1251 | return CC_RESOURCE_ERROR; | |
1252 | } | |
1253 | if (eps[i]->max_streams != dev_max_streams) { | |
1254 | FIXME("device streams config not identical for all eps"); | |
1255 | return CC_RESOURCE_ERROR; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | /* | |
1260 | * max-streams in both the device descriptor and in the controller is a | |
1261 | * power of 2. But stream id 0 is reserved, so if a device can do up to 4 | |
1262 | * streams the guest will ask for 5 rounded up to the next power of 2 which | |
1263 | * becomes 8. For emulated devices usb_device_alloc_streams is a nop. | |
1264 | * | |
1265 | * For redirected devices however this is an issue, as there we must ask | |
1266 | * the real xhci controller to alloc streams, and the host driver for the | |
1267 | * real xhci controller will likely disallow allocating more streams then | |
1268 | * the device can handle. | |
1269 | * | |
1270 | * So we limit the requested nr_streams to the maximum number the device | |
1271 | * can handle. | |
1272 | */ | |
1273 | if (req_nr_streams > dev_max_streams) { | |
1274 | req_nr_streams = dev_max_streams; | |
1275 | } | |
1276 | ||
1277 | r = usb_device_alloc_streams(eps[0]->dev, eps, nr_eps, req_nr_streams); | |
1278 | if (r != 0) { | |
d6bb65fc | 1279 | DPRINTF("xhci: alloc streams failed\n"); |
72391da5 HG |
1280 | return CC_RESOURCE_ERROR; |
1281 | } | |
1282 | ||
1283 | return CC_SUCCESS; | |
1284 | } | |
1285 | ||
024426ac GH |
1286 | static XHCIStreamContext *xhci_find_stream(XHCIEPContext *epctx, |
1287 | unsigned int streamid, | |
1288 | uint32_t *cc_error) | |
1289 | { | |
1290 | XHCIStreamContext *sctx; | |
1291 | dma_addr_t base; | |
1292 | uint32_t ctx[2], sct; | |
1293 | ||
1294 | assert(streamid != 0); | |
1295 | if (epctx->lsa) { | |
1296 | if (streamid >= epctx->nr_pstreams) { | |
1297 | *cc_error = CC_INVALID_STREAM_ID_ERROR; | |
1298 | return NULL; | |
1299 | } | |
1300 | sctx = epctx->pstreams + streamid; | |
1301 | } else { | |
1302 | FIXME("secondary streams not implemented yet"); | |
1303 | } | |
1304 | ||
1305 | if (sctx->sct == -1) { | |
1306 | xhci_dma_read_u32s(epctx->xhci, sctx->pctx, ctx, sizeof(ctx)); | |
024426ac GH |
1307 | sct = (ctx[0] >> 1) & 0x07; |
1308 | if (epctx->lsa && sct != 1) { | |
1309 | *cc_error = CC_INVALID_STREAM_TYPE_ERROR; | |
1310 | return NULL; | |
1311 | } | |
1312 | sctx->sct = sct; | |
1313 | base = xhci_addr64(ctx[0] & ~0xf, ctx[1]); | |
1314 | xhci_ring_init(epctx->xhci, &sctx->ring, base); | |
1315 | } | |
1316 | return sctx; | |
1317 | } | |
1318 | ||
62c6ae04 | 1319 | static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx, |
024426ac | 1320 | XHCIStreamContext *sctx, uint32_t state) |
62c6ae04 | 1321 | { |
c90daa1c | 1322 | XHCIRing *ring = NULL; |
62c6ae04 | 1323 | uint32_t ctx[5]; |
024426ac | 1324 | uint32_t ctx2[2]; |
62c6ae04 | 1325 | |
616b5d53 | 1326 | xhci_dma_read_u32s(xhci, epctx->pctx, ctx, sizeof(ctx)); |
62c6ae04 HM |
1327 | ctx[0] &= ~EP_STATE_MASK; |
1328 | ctx[0] |= state; | |
024426ac GH |
1329 | |
1330 | /* update ring dequeue ptr */ | |
1331 | if (epctx->nr_pstreams) { | |
1332 | if (sctx != NULL) { | |
c90daa1c | 1333 | ring = &sctx->ring; |
024426ac GH |
1334 | xhci_dma_read_u32s(xhci, sctx->pctx, ctx2, sizeof(ctx2)); |
1335 | ctx2[0] &= 0xe; | |
1336 | ctx2[0] |= sctx->ring.dequeue | sctx->ring.ccs; | |
1337 | ctx2[1] = (sctx->ring.dequeue >> 16) >> 16; | |
1338 | xhci_dma_write_u32s(xhci, sctx->pctx, ctx2, sizeof(ctx2)); | |
1339 | } | |
1340 | } else { | |
c90daa1c HG |
1341 | ring = &epctx->ring; |
1342 | } | |
1343 | if (ring) { | |
1344 | ctx[2] = ring->dequeue | ring->ccs; | |
1345 | ctx[3] = (ring->dequeue >> 16) >> 16; | |
1346 | ||
024426ac GH |
1347 | DPRINTF("xhci: set epctx: " DMA_ADDR_FMT " state=%d dequeue=%08x%08x\n", |
1348 | epctx->pctx, state, ctx[3], ctx[2]); | |
1349 | } | |
1350 | ||
616b5d53 | 1351 | xhci_dma_write_u32s(xhci, epctx->pctx, ctx, sizeof(ctx)); |
1c82392a GH |
1352 | if (epctx->state != state) { |
1353 | trace_usb_xhci_ep_state(epctx->slotid, epctx->epid, | |
1354 | ep_state_name(epctx->state), | |
1355 | ep_state_name(state)); | |
1356 | } | |
62c6ae04 HM |
1357 | epctx->state = state; |
1358 | } | |
1359 | ||
3d139684 GH |
1360 | static void xhci_ep_kick_timer(void *opaque) |
1361 | { | |
1362 | XHCIEPContext *epctx = opaque; | |
3a533ee8 | 1363 | xhci_kick_epctx(epctx, 0); |
3d139684 GH |
1364 | } |
1365 | ||
492b21f6 GH |
1366 | static XHCIEPContext *xhci_alloc_epctx(XHCIState *xhci, |
1367 | unsigned int slotid, | |
1368 | unsigned int epid) | |
1369 | { | |
1370 | XHCIEPContext *epctx; | |
492b21f6 GH |
1371 | |
1372 | epctx = g_new0(XHCIEPContext, 1); | |
1373 | epctx->xhci = xhci; | |
1374 | epctx->slotid = slotid; | |
1375 | epctx->epid = epid; | |
1376 | ||
94b037f2 | 1377 | QTAILQ_INIT(&epctx->transfers); |
bc72ad67 | 1378 | epctx->kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_ep_kick_timer, epctx); |
492b21f6 GH |
1379 | |
1380 | return epctx; | |
1381 | } | |
1382 | ||
003e15a1 GH |
1383 | static void xhci_init_epctx(XHCIEPContext *epctx, |
1384 | dma_addr_t pctx, uint32_t *ctx) | |
62c6ae04 | 1385 | { |
59a70ccd | 1386 | dma_addr_t dequeue; |
62c6ae04 | 1387 | |
62c6ae04 | 1388 | dequeue = xhci_addr64(ctx[2] & ~0xf, ctx[3]); |
62c6ae04 HM |
1389 | |
1390 | epctx->type = (ctx[1] >> EP_TYPE_SHIFT) & EP_TYPE_MASK; | |
62c6ae04 HM |
1391 | epctx->pctx = pctx; |
1392 | epctx->max_psize = ctx[1]>>16; | |
1393 | epctx->max_psize *= 1+((ctx[1]>>8)&0xff); | |
2aa6bfcb | 1394 | epctx->max_pstreams = (ctx[0] >> 10) & epctx->xhci->max_pstreams_mask; |
024426ac | 1395 | epctx->lsa = (ctx[0] >> 15) & 1; |
024426ac GH |
1396 | if (epctx->max_pstreams) { |
1397 | xhci_alloc_streams(epctx, dequeue); | |
1398 | } else { | |
003e15a1 | 1399 | xhci_ring_init(epctx->xhci, &epctx->ring, dequeue); |
024426ac GH |
1400 | epctx->ring.ccs = ctx[2] & 1; |
1401 | } | |
62c6ae04 | 1402 | |
ca716278 | 1403 | epctx->interval = 1 << ((ctx[0] >> 16) & 0xff); |
003e15a1 GH |
1404 | } |
1405 | ||
1406 | static TRBCCode xhci_enable_ep(XHCIState *xhci, unsigned int slotid, | |
1407 | unsigned int epid, dma_addr_t pctx, | |
1408 | uint32_t *ctx) | |
1409 | { | |
1410 | XHCISlot *slot; | |
1411 | XHCIEPContext *epctx; | |
1412 | ||
1413 | trace_usb_xhci_ep_enable(slotid, epid); | |
1414 | assert(slotid >= 1 && slotid <= xhci->numslots); | |
1415 | assert(epid >= 1 && epid <= 31); | |
1416 | ||
1417 | slot = &xhci->slots[slotid-1]; | |
1418 | if (slot->eps[epid-1]) { | |
1419 | xhci_disable_ep(xhci, slotid, epid); | |
1420 | } | |
1421 | ||
1422 | epctx = xhci_alloc_epctx(xhci, slotid, epid); | |
1423 | slot->eps[epid-1] = epctx; | |
1424 | xhci_init_epctx(epctx, pctx, ctx); | |
1425 | ||
8c244210 GA |
1426 | DPRINTF("xhci: endpoint %d.%d type is %d, max transaction (burst) " |
1427 | "size is %d\n", epid/2, epid%2, epctx->type, epctx->max_psize); | |
1428 | ||
3d139684 | 1429 | epctx->mfindex_last = 0; |
3d139684 | 1430 | |
62c6ae04 HM |
1431 | epctx->state = EP_RUNNING; |
1432 | ctx[0] &= ~EP_STATE_MASK; | |
1433 | ctx[0] |= EP_RUNNING; | |
1434 | ||
1435 | return CC_SUCCESS; | |
1436 | } | |
1437 | ||
94b037f2 GH |
1438 | static XHCITransfer *xhci_ep_alloc_xfer(XHCIEPContext *epctx, |
1439 | uint32_t length) | |
1440 | { | |
1441 | uint32_t limit = epctx->nr_pstreams + 16; | |
1442 | XHCITransfer *xfer; | |
1443 | ||
1444 | if (epctx->xfer_count >= limit) { | |
1445 | return NULL; | |
1446 | } | |
1447 | ||
1448 | xfer = g_new0(XHCITransfer, 1); | |
94b037f2 | 1449 | xfer->epctx = epctx; |
94b037f2 GH |
1450 | xfer->trbs = g_new(XHCITRB, length); |
1451 | xfer->trb_count = length; | |
1452 | usb_packet_init(&xfer->packet); | |
1453 | ||
1454 | QTAILQ_INSERT_TAIL(&epctx->transfers, xfer, next); | |
1455 | epctx->xfer_count++; | |
1456 | ||
1457 | return xfer; | |
1458 | } | |
1459 | ||
1460 | static void xhci_ep_free_xfer(XHCITransfer *xfer) | |
1461 | { | |
1462 | QTAILQ_REMOVE(&xfer->epctx->transfers, xfer, next); | |
1463 | xfer->epctx->xfer_count--; | |
1464 | ||
1465 | usb_packet_cleanup(&xfer->packet); | |
1466 | g_free(xfer->trbs); | |
1467 | g_free(xfer); | |
1468 | } | |
1469 | ||
582d6f4a | 1470 | static int xhci_ep_nuke_one_xfer(XHCITransfer *t, TRBCCode report) |
3151f209 HG |
1471 | { |
1472 | int killed = 0; | |
1473 | ||
582d6f4a HG |
1474 | if (report && (t->running_async || t->running_retry)) { |
1475 | t->status = report; | |
1476 | xhci_xfer_report(t); | |
1477 | } | |
1478 | ||
3151f209 HG |
1479 | if (t->running_async) { |
1480 | usb_cancel_packet(&t->packet); | |
1481 | t->running_async = 0; | |
3151f209 HG |
1482 | killed = 1; |
1483 | } | |
1484 | if (t->running_retry) { | |
5612564e GH |
1485 | if (t->epctx) { |
1486 | t->epctx->retry = NULL; | |
1487 | timer_del(t->epctx->kick_timer); | |
3151f209 HG |
1488 | } |
1489 | t->running_retry = 0; | |
582d6f4a | 1490 | killed = 1; |
3151f209 | 1491 | } |
ef1e1e07 | 1492 | g_free(t->trbs); |
3151f209 HG |
1493 | |
1494 | t->trbs = NULL; | |
94b037f2 | 1495 | t->trb_count = 0; |
3151f209 HG |
1496 | |
1497 | return killed; | |
1498 | } | |
1499 | ||
62c6ae04 | 1500 | static int xhci_ep_nuke_xfers(XHCIState *xhci, unsigned int slotid, |
582d6f4a | 1501 | unsigned int epid, TRBCCode report) |
62c6ae04 HM |
1502 | { |
1503 | XHCISlot *slot; | |
1504 | XHCIEPContext *epctx; | |
94b037f2 GH |
1505 | XHCITransfer *xfer; |
1506 | int killed = 0; | |
f79738b0 | 1507 | USBEndpoint *ep = NULL; |
91062ae0 | 1508 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
1509 | assert(epid >= 1 && epid <= 31); |
1510 | ||
1511 | DPRINTF("xhci_ep_nuke_xfers(%d, %d)\n", slotid, epid); | |
1512 | ||
1513 | slot = &xhci->slots[slotid-1]; | |
1514 | ||
1515 | if (!slot->eps[epid-1]) { | |
1516 | return 0; | |
1517 | } | |
1518 | ||
1519 | epctx = slot->eps[epid-1]; | |
1520 | ||
94b037f2 GH |
1521 | for (;;) { |
1522 | xfer = QTAILQ_FIRST(&epctx->transfers); | |
1523 | if (xfer == NULL) { | |
1524 | break; | |
1525 | } | |
1526 | killed += xhci_ep_nuke_one_xfer(xfer, report); | |
582d6f4a HG |
1527 | if (killed) { |
1528 | report = 0; /* Only report once */ | |
1529 | } | |
94b037f2 | 1530 | xhci_ep_free_xfer(xfer); |
62c6ae04 | 1531 | } |
518ad5f2 | 1532 | |
070eeef9 | 1533 | ep = xhci_epid_to_usbep(epctx); |
f79738b0 HG |
1534 | if (ep) { |
1535 | usb_device_ep_stopped(ep->dev, ep); | |
1536 | } | |
62c6ae04 HM |
1537 | return killed; |
1538 | } | |
1539 | ||
1540 | static TRBCCode xhci_disable_ep(XHCIState *xhci, unsigned int slotid, | |
1541 | unsigned int epid) | |
1542 | { | |
1543 | XHCISlot *slot; | |
1544 | XHCIEPContext *epctx; | |
1545 | ||
c1f6b493 | 1546 | trace_usb_xhci_ep_disable(slotid, epid); |
91062ae0 | 1547 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
1548 | assert(epid >= 1 && epid <= 31); |
1549 | ||
62c6ae04 HM |
1550 | slot = &xhci->slots[slotid-1]; |
1551 | ||
1552 | if (!slot->eps[epid-1]) { | |
1553 | DPRINTF("xhci: slot %d ep %d already disabled\n", slotid, epid); | |
1554 | return CC_SUCCESS; | |
1555 | } | |
1556 | ||
582d6f4a | 1557 | xhci_ep_nuke_xfers(xhci, slotid, epid, 0); |
62c6ae04 HM |
1558 | |
1559 | epctx = slot->eps[epid-1]; | |
1560 | ||
024426ac GH |
1561 | if (epctx->nr_pstreams) { |
1562 | xhci_free_streams(epctx); | |
1563 | } | |
1564 | ||
491d68d9 RK |
1565 | /* only touch guest RAM if we're not resetting the HC */ |
1566 | if (xhci->dcbaap_low || xhci->dcbaap_high) { | |
1567 | xhci_set_ep_state(xhci, epctx, NULL, EP_DISABLED); | |
1568 | } | |
62c6ae04 | 1569 | |
bc72ad67 | 1570 | timer_free(epctx->kick_timer); |
62c6ae04 HM |
1571 | g_free(epctx); |
1572 | slot->eps[epid-1] = NULL; | |
1573 | ||
1574 | return CC_SUCCESS; | |
1575 | } | |
1576 | ||
1577 | static TRBCCode xhci_stop_ep(XHCIState *xhci, unsigned int slotid, | |
1578 | unsigned int epid) | |
1579 | { | |
1580 | XHCISlot *slot; | |
1581 | XHCIEPContext *epctx; | |
1582 | ||
c1f6b493 | 1583 | trace_usb_xhci_ep_stop(slotid, epid); |
91062ae0 | 1584 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
1585 | |
1586 | if (epid < 1 || epid > 31) { | |
d6bb65fc | 1587 | DPRINTF("xhci: bad ep %d\n", epid); |
62c6ae04 HM |
1588 | return CC_TRB_ERROR; |
1589 | } | |
1590 | ||
1591 | slot = &xhci->slots[slotid-1]; | |
1592 | ||
1593 | if (!slot->eps[epid-1]) { | |
1594 | DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid); | |
1595 | return CC_EP_NOT_ENABLED_ERROR; | |
1596 | } | |
1597 | ||
582d6f4a | 1598 | if (xhci_ep_nuke_xfers(xhci, slotid, epid, CC_STOPPED) > 0) { |
d6bb65fc | 1599 | DPRINTF("xhci: FIXME: endpoint stopped w/ xfers running, " |
62c6ae04 HM |
1600 | "data might be lost\n"); |
1601 | } | |
1602 | ||
1603 | epctx = slot->eps[epid-1]; | |
1604 | ||
024426ac GH |
1605 | xhci_set_ep_state(xhci, epctx, NULL, EP_STOPPED); |
1606 | ||
1607 | if (epctx->nr_pstreams) { | |
1608 | xhci_reset_streams(epctx); | |
1609 | } | |
62c6ae04 HM |
1610 | |
1611 | return CC_SUCCESS; | |
1612 | } | |
1613 | ||
1614 | static TRBCCode xhci_reset_ep(XHCIState *xhci, unsigned int slotid, | |
1615 | unsigned int epid) | |
1616 | { | |
1617 | XHCISlot *slot; | |
1618 | XHCIEPContext *epctx; | |
62c6ae04 | 1619 | |
c1f6b493 | 1620 | trace_usb_xhci_ep_reset(slotid, epid); |
91062ae0 | 1621 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 | 1622 | |
62c6ae04 | 1623 | if (epid < 1 || epid > 31) { |
d6bb65fc | 1624 | DPRINTF("xhci: bad ep %d\n", epid); |
62c6ae04 HM |
1625 | return CC_TRB_ERROR; |
1626 | } | |
1627 | ||
1628 | slot = &xhci->slots[slotid-1]; | |
1629 | ||
1630 | if (!slot->eps[epid-1]) { | |
1631 | DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid); | |
1632 | return CC_EP_NOT_ENABLED_ERROR; | |
1633 | } | |
1634 | ||
1635 | epctx = slot->eps[epid-1]; | |
1636 | ||
1637 | if (epctx->state != EP_HALTED) { | |
d6bb65fc | 1638 | DPRINTF("xhci: reset EP while EP %d not halted (%d)\n", |
62c6ae04 HM |
1639 | epid, epctx->state); |
1640 | return CC_CONTEXT_STATE_ERROR; | |
1641 | } | |
1642 | ||
582d6f4a | 1643 | if (xhci_ep_nuke_xfers(xhci, slotid, epid, 0) > 0) { |
d6bb65fc | 1644 | DPRINTF("xhci: FIXME: endpoint reset w/ xfers running, " |
62c6ae04 HM |
1645 | "data might be lost\n"); |
1646 | } | |
1647 | ||
75cc1c1f | 1648 | if (!xhci->slots[slotid-1].uport || |
de9de157 HG |
1649 | !xhci->slots[slotid-1].uport->dev || |
1650 | !xhci->slots[slotid-1].uport->dev->attached) { | |
62c6ae04 HM |
1651 | return CC_USB_TRANSACTION_ERROR; |
1652 | } | |
1653 | ||
024426ac GH |
1654 | xhci_set_ep_state(xhci, epctx, NULL, EP_STOPPED); |
1655 | ||
1656 | if (epctx->nr_pstreams) { | |
1657 | xhci_reset_streams(epctx); | |
1658 | } | |
62c6ae04 HM |
1659 | |
1660 | return CC_SUCCESS; | |
1661 | } | |
1662 | ||
1663 | static TRBCCode xhci_set_ep_dequeue(XHCIState *xhci, unsigned int slotid, | |
024426ac GH |
1664 | unsigned int epid, unsigned int streamid, |
1665 | uint64_t pdequeue) | |
62c6ae04 HM |
1666 | { |
1667 | XHCISlot *slot; | |
1668 | XHCIEPContext *epctx; | |
024426ac | 1669 | XHCIStreamContext *sctx; |
59a70ccd | 1670 | dma_addr_t dequeue; |
62c6ae04 | 1671 | |
91062ae0 | 1672 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
1673 | |
1674 | if (epid < 1 || epid > 31) { | |
d6bb65fc | 1675 | DPRINTF("xhci: bad ep %d\n", epid); |
62c6ae04 HM |
1676 | return CC_TRB_ERROR; |
1677 | } | |
1678 | ||
024426ac | 1679 | trace_usb_xhci_ep_set_dequeue(slotid, epid, streamid, pdequeue); |
62c6ae04 HM |
1680 | dequeue = xhci_mask64(pdequeue); |
1681 | ||
1682 | slot = &xhci->slots[slotid-1]; | |
1683 | ||
1684 | if (!slot->eps[epid-1]) { | |
1685 | DPRINTF("xhci: slot %d ep %d not enabled\n", slotid, epid); | |
1686 | return CC_EP_NOT_ENABLED_ERROR; | |
1687 | } | |
1688 | ||
1689 | epctx = slot->eps[epid-1]; | |
1690 | ||
62c6ae04 | 1691 | if (epctx->state != EP_STOPPED) { |
d6bb65fc | 1692 | DPRINTF("xhci: set EP dequeue pointer while EP %d not stopped\n", epid); |
62c6ae04 HM |
1693 | return CC_CONTEXT_STATE_ERROR; |
1694 | } | |
1695 | ||
024426ac GH |
1696 | if (epctx->nr_pstreams) { |
1697 | uint32_t err; | |
1698 | sctx = xhci_find_stream(epctx, streamid, &err); | |
1699 | if (sctx == NULL) { | |
1700 | return err; | |
1701 | } | |
1702 | xhci_ring_init(xhci, &sctx->ring, dequeue & ~0xf); | |
1703 | sctx->ring.ccs = dequeue & 1; | |
1704 | } else { | |
1705 | sctx = NULL; | |
1706 | xhci_ring_init(xhci, &epctx->ring, dequeue & ~0xF); | |
1707 | epctx->ring.ccs = dequeue & 1; | |
1708 | } | |
62c6ae04 | 1709 | |
024426ac | 1710 | xhci_set_ep_state(xhci, epctx, sctx, EP_STOPPED); |
62c6ae04 HM |
1711 | |
1712 | return CC_SUCCESS; | |
1713 | } | |
1714 | ||
a6fb2ddb | 1715 | static int xhci_xfer_create_sgl(XHCITransfer *xfer, int in_xfer) |
62c6ae04 | 1716 | { |
5612564e | 1717 | XHCIState *xhci = xfer->epctx->xhci; |
d5a15814 | 1718 | int i; |
62c6ae04 | 1719 | |
a6fb2ddb | 1720 | xfer->int_req = false; |
9b7d3334 | 1721 | pci_dma_sglist_init(&xfer->sgl, PCI_DEVICE(xhci), xfer->trb_count); |
62c6ae04 HM |
1722 | for (i = 0; i < xfer->trb_count; i++) { |
1723 | XHCITRB *trb = &xfer->trbs[i]; | |
59a70ccd | 1724 | dma_addr_t addr; |
62c6ae04 HM |
1725 | unsigned int chunk = 0; |
1726 | ||
a6fb2ddb HG |
1727 | if (trb->control & TRB_TR_IOC) { |
1728 | xfer->int_req = true; | |
1729 | } | |
1730 | ||
62c6ae04 HM |
1731 | switch (TRB_TYPE(*trb)) { |
1732 | case TR_DATA: | |
1733 | if ((!(trb->control & TRB_TR_DIR)) != (!in_xfer)) { | |
d6bb65fc | 1734 | DPRINTF("xhci: data direction mismatch for TR_DATA\n"); |
d5a15814 | 1735 | goto err; |
62c6ae04 HM |
1736 | } |
1737 | /* fallthrough */ | |
1738 | case TR_NORMAL: | |
1739 | case TR_ISOCH: | |
1740 | addr = xhci_mask64(trb->parameter); | |
d5a15814 GH |
1741 | chunk = trb->status & 0x1ffff; |
1742 | if (trb->control & TRB_TR_IDT) { | |
1743 | if (chunk > 8 || in_xfer) { | |
d6bb65fc | 1744 | DPRINTF("xhci: invalid immediate data TRB\n"); |
d5a15814 GH |
1745 | goto err; |
1746 | } | |
1747 | qemu_sglist_add(&xfer->sgl, trb->addr, chunk); | |
1748 | } else { | |
1749 | qemu_sglist_add(&xfer->sgl, addr, chunk); | |
1750 | } | |
1751 | break; | |
1752 | } | |
1753 | } | |
1754 | ||
d5a15814 GH |
1755 | return 0; |
1756 | ||
1757 | err: | |
1758 | qemu_sglist_destroy(&xfer->sgl); | |
1759 | xhci_die(xhci); | |
1760 | return -1; | |
1761 | } | |
1762 | ||
1763 | static void xhci_xfer_unmap(XHCITransfer *xfer) | |
1764 | { | |
1765 | usb_packet_unmap(&xfer->packet, &xfer->sgl); | |
1766 | qemu_sglist_destroy(&xfer->sgl); | |
1767 | } | |
1768 | ||
1769 | static void xhci_xfer_report(XHCITransfer *xfer) | |
1770 | { | |
1771 | uint32_t edtla = 0; | |
1772 | unsigned int left; | |
1773 | bool reported = 0; | |
1774 | bool shortpkt = 0; | |
1775 | XHCIEvent event = {ER_TRANSFER, CC_SUCCESS}; | |
5612564e | 1776 | XHCIState *xhci = xfer->epctx->xhci; |
d5a15814 GH |
1777 | int i; |
1778 | ||
9b8251c5 | 1779 | left = xfer->packet.actual_length; |
d5a15814 GH |
1780 | |
1781 | for (i = 0; i < xfer->trb_count; i++) { | |
1782 | XHCITRB *trb = &xfer->trbs[i]; | |
1783 | unsigned int chunk = 0; | |
1784 | ||
1785 | switch (TRB_TYPE(*trb)) { | |
b66ad1f1 HPS |
1786 | case TR_SETUP: |
1787 | chunk = trb->status & 0x1ffff; | |
1788 | if (chunk > 8) { | |
1789 | chunk = 8; | |
1790 | } | |
1791 | break; | |
d5a15814 GH |
1792 | case TR_DATA: |
1793 | case TR_NORMAL: | |
1794 | case TR_ISOCH: | |
62c6ae04 HM |
1795 | chunk = trb->status & 0x1ffff; |
1796 | if (chunk > left) { | |
1797 | chunk = left; | |
d5a15814 GH |
1798 | if (xfer->status == CC_SUCCESS) { |
1799 | shortpkt = 1; | |
62c6ae04 HM |
1800 | } |
1801 | } | |
1802 | left -= chunk; | |
62c6ae04 | 1803 | edtla += chunk; |
62c6ae04 HM |
1804 | break; |
1805 | case TR_STATUS: | |
1806 | reported = 0; | |
1807 | shortpkt = 0; | |
1808 | break; | |
1809 | } | |
1810 | ||
88dbed3f GH |
1811 | if (!reported && ((trb->control & TRB_TR_IOC) || |
1812 | (shortpkt && (trb->control & TRB_TR_ISP)) || | |
1813 | (xfer->status != CC_SUCCESS && left == 0))) { | |
d6fcb293 GH |
1814 | event.slotid = xfer->epctx->slotid; |
1815 | event.epid = xfer->epctx->epid; | |
62c6ae04 HM |
1816 | event.length = (trb->status & 0x1ffff) - chunk; |
1817 | event.flags = 0; | |
1818 | event.ptr = trb->addr; | |
1819 | if (xfer->status == CC_SUCCESS) { | |
1820 | event.ccode = shortpkt ? CC_SHORT_PACKET : CC_SUCCESS; | |
1821 | } else { | |
1822 | event.ccode = xfer->status; | |
1823 | } | |
1824 | if (TRB_TYPE(*trb) == TR_EVDATA) { | |
1825 | event.ptr = trb->parameter; | |
1826 | event.flags |= TRB_EV_ED; | |
1827 | event.length = edtla & 0xffffff; | |
1828 | DPRINTF("xhci_xfer_data: EDTLA=%d\n", event.length); | |
1829 | edtla = 0; | |
1830 | } | |
2d1de850 | 1831 | xhci_event(xhci, &event, TRB_INTR(*trb)); |
62c6ae04 | 1832 | reported = 1; |
d5a15814 GH |
1833 | if (xfer->status != CC_SUCCESS) { |
1834 | return; | |
1835 | } | |
62c6ae04 | 1836 | } |
df0f1692 GH |
1837 | |
1838 | switch (TRB_TYPE(*trb)) { | |
1839 | case TR_SETUP: | |
1840 | reported = 0; | |
1841 | shortpkt = 0; | |
1842 | break; | |
1843 | } | |
1844 | ||
62c6ae04 | 1845 | } |
62c6ae04 HM |
1846 | } |
1847 | ||
1848 | static void xhci_stall_ep(XHCITransfer *xfer) | |
1849 | { | |
5612564e GH |
1850 | XHCIEPContext *epctx = xfer->epctx; |
1851 | XHCIState *xhci = epctx->xhci; | |
024426ac GH |
1852 | uint32_t err; |
1853 | XHCIStreamContext *sctx; | |
62c6ae04 | 1854 | |
024426ac GH |
1855 | if (epctx->nr_pstreams) { |
1856 | sctx = xhci_find_stream(epctx, xfer->streamid, &err); | |
1857 | if (sctx == NULL) { | |
1858 | return; | |
1859 | } | |
1860 | sctx->ring.dequeue = xfer->trbs[0].addr; | |
1861 | sctx->ring.ccs = xfer->trbs[0].ccs; | |
1862 | xhci_set_ep_state(xhci, epctx, sctx, EP_HALTED); | |
1863 | } else { | |
1864 | epctx->ring.dequeue = xfer->trbs[0].addr; | |
1865 | epctx->ring.ccs = xfer->trbs[0].ccs; | |
1866 | xhci_set_ep_state(xhci, epctx, NULL, EP_HALTED); | |
1867 | } | |
62c6ae04 HM |
1868 | } |
1869 | ||
1870 | static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, | |
1871 | XHCIEPContext *epctx); | |
1872 | ||
5c08106f GH |
1873 | static int xhci_setup_packet(XHCITransfer *xfer) |
1874 | { | |
079d0b7f GH |
1875 | USBEndpoint *ep; |
1876 | int dir; | |
1877 | ||
1878 | dir = xfer->in_xfer ? USB_TOKEN_IN : USB_TOKEN_OUT; | |
5c08106f GH |
1879 | |
1880 | if (xfer->packet.ep) { | |
1881 | ep = xfer->packet.ep; | |
5c08106f | 1882 | } else { |
070eeef9 | 1883 | ep = xhci_epid_to_usbep(xfer->epctx); |
518ad5f2 | 1884 | if (!ep) { |
d6bb65fc | 1885 | DPRINTF("xhci: slot %d has no device\n", |
ccaf87a0 | 1886 | xfer->slotid); |
5c08106f GH |
1887 | return -1; |
1888 | } | |
5c08106f GH |
1889 | } |
1890 | ||
a6fb2ddb | 1891 | xhci_xfer_create_sgl(xfer, dir == USB_TOKEN_IN); /* Also sets int_req */ |
024426ac | 1892 | usb_packet_setup(&xfer->packet, dir, ep, xfer->streamid, |
8550a02d | 1893 | xfer->trbs[0].addr, false, xfer->int_req); |
a6fb2ddb | 1894 | usb_packet_map(&xfer->packet, &xfer->sgl); |
62c6ae04 | 1895 | DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n", |
518ad5f2 | 1896 | xfer->packet.pid, ep->dev->addr, ep->nr); |
62c6ae04 HM |
1897 | return 0; |
1898 | } | |
1899 | ||
9a77a0f5 | 1900 | static int xhci_complete_packet(XHCITransfer *xfer) |
62c6ae04 | 1901 | { |
9a77a0f5 | 1902 | if (xfer->packet.status == USB_RET_ASYNC) { |
97df650b | 1903 | trace_usb_xhci_xfer_async(xfer); |
7c605a23 GH |
1904 | xfer->running_async = 1; |
1905 | xfer->running_retry = 0; | |
1906 | xfer->complete = 0; | |
7c605a23 | 1907 | return 0; |
9a77a0f5 | 1908 | } else if (xfer->packet.status == USB_RET_NAK) { |
97df650b | 1909 | trace_usb_xhci_xfer_nak(xfer); |
7c605a23 GH |
1910 | xfer->running_async = 0; |
1911 | xfer->running_retry = 1; | |
62c6ae04 | 1912 | xfer->complete = 0; |
62c6ae04 HM |
1913 | return 0; |
1914 | } else { | |
7c605a23 GH |
1915 | xfer->running_async = 0; |
1916 | xfer->running_retry = 0; | |
62c6ae04 | 1917 | xfer->complete = 1; |
d5a15814 | 1918 | xhci_xfer_unmap(xfer); |
62c6ae04 HM |
1919 | } |
1920 | ||
9a77a0f5 HG |
1921 | if (xfer->packet.status == USB_RET_SUCCESS) { |
1922 | trace_usb_xhci_xfer_success(xfer, xfer->packet.actual_length); | |
d5a15814 GH |
1923 | xfer->status = CC_SUCCESS; |
1924 | xhci_xfer_report(xfer); | |
62c6ae04 HM |
1925 | return 0; |
1926 | } | |
1927 | ||
1928 | /* error */ | |
9a77a0f5 HG |
1929 | trace_usb_xhci_xfer_error(xfer, xfer->packet.status); |
1930 | switch (xfer->packet.status) { | |
62c6ae04 | 1931 | case USB_RET_NODEV: |
ed60ff02 | 1932 | case USB_RET_IOERROR: |
62c6ae04 | 1933 | xfer->status = CC_USB_TRANSACTION_ERROR; |
d5a15814 | 1934 | xhci_xfer_report(xfer); |
62c6ae04 HM |
1935 | xhci_stall_ep(xfer); |
1936 | break; | |
1937 | case USB_RET_STALL: | |
1938 | xfer->status = CC_STALL_ERROR; | |
d5a15814 | 1939 | xhci_xfer_report(xfer); |
62c6ae04 HM |
1940 | xhci_stall_ep(xfer); |
1941 | break; | |
4e906d56 GH |
1942 | case USB_RET_BABBLE: |
1943 | xfer->status = CC_BABBLE_DETECTED; | |
1944 | xhci_xfer_report(xfer); | |
1945 | xhci_stall_ep(xfer); | |
1946 | break; | |
62c6ae04 | 1947 | default: |
d6bb65fc | 1948 | DPRINTF("%s: FIXME: status = %d\n", __func__, |
9a77a0f5 | 1949 | xfer->packet.status); |
024426ac | 1950 | FIXME("unhandled USB_RET_*"); |
62c6ae04 HM |
1951 | } |
1952 | return 0; | |
1953 | } | |
1954 | ||
1955 | static int xhci_fire_ctl_transfer(XHCIState *xhci, XHCITransfer *xfer) | |
1956 | { | |
1957 | XHCITRB *trb_setup, *trb_status; | |
2850ca9e | 1958 | uint8_t bmRequestType; |
62c6ae04 | 1959 | |
62c6ae04 HM |
1960 | trb_setup = &xfer->trbs[0]; |
1961 | trb_status = &xfer->trbs[xfer->trb_count-1]; | |
1962 | ||
d6fcb293 GH |
1963 | trace_usb_xhci_xfer_start(xfer, xfer->epctx->slotid, |
1964 | xfer->epctx->epid, xfer->streamid); | |
97df650b | 1965 | |
62c6ae04 HM |
1966 | /* at most one Event Data TRB allowed after STATUS */ |
1967 | if (TRB_TYPE(*trb_status) == TR_EVDATA && xfer->trb_count > 2) { | |
1968 | trb_status--; | |
1969 | } | |
1970 | ||
1971 | /* do some sanity checks */ | |
1972 | if (TRB_TYPE(*trb_setup) != TR_SETUP) { | |
d6bb65fc | 1973 | DPRINTF("xhci: ep0 first TD not SETUP: %d\n", |
62c6ae04 HM |
1974 | TRB_TYPE(*trb_setup)); |
1975 | return -1; | |
1976 | } | |
1977 | if (TRB_TYPE(*trb_status) != TR_STATUS) { | |
d6bb65fc | 1978 | DPRINTF("xhci: ep0 last TD not STATUS: %d\n", |
62c6ae04 HM |
1979 | TRB_TYPE(*trb_status)); |
1980 | return -1; | |
1981 | } | |
1982 | if (!(trb_setup->control & TRB_TR_IDT)) { | |
d6bb65fc | 1983 | DPRINTF("xhci: Setup TRB doesn't have IDT set\n"); |
62c6ae04 HM |
1984 | return -1; |
1985 | } | |
1986 | if ((trb_setup->status & 0x1ffff) != 8) { | |
d6bb65fc | 1987 | DPRINTF("xhci: Setup TRB has bad length (%d)\n", |
62c6ae04 HM |
1988 | (trb_setup->status & 0x1ffff)); |
1989 | return -1; | |
1990 | } | |
1991 | ||
1992 | bmRequestType = trb_setup->parameter; | |
62c6ae04 | 1993 | |
62c6ae04 HM |
1994 | xfer->in_xfer = bmRequestType & USB_DIR_IN; |
1995 | xfer->iso_xfer = false; | |
4d7a81c0 | 1996 | xfer->timed_xfer = false; |
62c6ae04 | 1997 | |
5c08106f GH |
1998 | if (xhci_setup_packet(xfer) < 0) { |
1999 | return -1; | |
2000 | } | |
2850ca9e | 2001 | xfer->packet.parameter = trb_setup->parameter; |
2850ca9e | 2002 | |
9a77a0f5 | 2003 | usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); |
62c6ae04 | 2004 | |
9a77a0f5 | 2005 | xhci_complete_packet(xfer); |
7c605a23 | 2006 | if (!xfer->running_async && !xfer->running_retry) { |
3a533ee8 | 2007 | xhci_kick_epctx(xfer->epctx, 0); |
62c6ae04 HM |
2008 | } |
2009 | return 0; | |
2010 | } | |
2011 | ||
4d7a81c0 GH |
2012 | static void xhci_calc_intr_kick(XHCIState *xhci, XHCITransfer *xfer, |
2013 | XHCIEPContext *epctx, uint64_t mfindex) | |
2014 | { | |
2015 | uint64_t asap = ((mfindex + epctx->interval - 1) & | |
2016 | ~(epctx->interval-1)); | |
2017 | uint64_t kick = epctx->mfindex_last + epctx->interval; | |
2018 | ||
2019 | assert(epctx->interval != 0); | |
2020 | xfer->mfindex_kick = MAX(asap, kick); | |
2021 | } | |
2022 | ||
3d139684 GH |
2023 | static void xhci_calc_iso_kick(XHCIState *xhci, XHCITransfer *xfer, |
2024 | XHCIEPContext *epctx, uint64_t mfindex) | |
2025 | { | |
2026 | if (xfer->trbs[0].control & TRB_TR_SIA) { | |
2027 | uint64_t asap = ((mfindex + epctx->interval - 1) & | |
2028 | ~(epctx->interval-1)); | |
2029 | if (asap >= epctx->mfindex_last && | |
2030 | asap <= epctx->mfindex_last + epctx->interval * 4) { | |
2031 | xfer->mfindex_kick = epctx->mfindex_last + epctx->interval; | |
2032 | } else { | |
2033 | xfer->mfindex_kick = asap; | |
2034 | } | |
2035 | } else { | |
786ad214 GH |
2036 | xfer->mfindex_kick = ((xfer->trbs[0].control >> TRB_TR_FRAMEID_SHIFT) |
2037 | & TRB_TR_FRAMEID_MASK) << 3; | |
3d139684 | 2038 | xfer->mfindex_kick |= mfindex & ~0x3fff; |
cc03ff9d | 2039 | if (xfer->mfindex_kick + 0x100 < mfindex) { |
3d139684 GH |
2040 | xfer->mfindex_kick += 0x4000; |
2041 | } | |
2042 | } | |
2043 | } | |
2044 | ||
4d7a81c0 GH |
2045 | static void xhci_check_intr_iso_kick(XHCIState *xhci, XHCITransfer *xfer, |
2046 | XHCIEPContext *epctx, uint64_t mfindex) | |
3d139684 GH |
2047 | { |
2048 | if (xfer->mfindex_kick > mfindex) { | |
bc72ad67 | 2049 | timer_mod(epctx->kick_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
3d139684 GH |
2050 | (xfer->mfindex_kick - mfindex) * 125000); |
2051 | xfer->running_retry = 1; | |
2052 | } else { | |
2053 | epctx->mfindex_last = xfer->mfindex_kick; | |
bc72ad67 | 2054 | timer_del(epctx->kick_timer); |
3d139684 GH |
2055 | xfer->running_retry = 0; |
2056 | } | |
2057 | } | |
2058 | ||
2059 | ||
62c6ae04 HM |
2060 | static int xhci_submit(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx) |
2061 | { | |
3d139684 | 2062 | uint64_t mfindex; |
62c6ae04 HM |
2063 | |
2064 | DPRINTF("xhci_submit(slotid=%d,epid=%d)\n", xfer->slotid, xfer->epid); | |
62c6ae04 HM |
2065 | |
2066 | xfer->in_xfer = epctx->type>>2; | |
62c6ae04 | 2067 | |
62c6ae04 HM |
2068 | switch(epctx->type) { |
2069 | case ET_INTR_OUT: | |
2070 | case ET_INTR_IN: | |
4d7a81c0 GH |
2071 | xfer->pkts = 0; |
2072 | xfer->iso_xfer = false; | |
2073 | xfer->timed_xfer = true; | |
2074 | mfindex = xhci_mfindex_get(xhci); | |
2075 | xhci_calc_intr_kick(xhci, xfer, epctx, mfindex); | |
2076 | xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex); | |
2077 | if (xfer->running_retry) { | |
2078 | return -1; | |
2079 | } | |
2080 | break; | |
62c6ae04 HM |
2081 | case ET_BULK_OUT: |
2082 | case ET_BULK_IN: | |
3d139684 GH |
2083 | xfer->pkts = 0; |
2084 | xfer->iso_xfer = false; | |
4d7a81c0 | 2085 | xfer->timed_xfer = false; |
62c6ae04 HM |
2086 | break; |
2087 | case ET_ISO_OUT: | |
2088 | case ET_ISO_IN: | |
3d139684 GH |
2089 | xfer->pkts = 1; |
2090 | xfer->iso_xfer = true; | |
4d7a81c0 | 2091 | xfer->timed_xfer = true; |
3d139684 GH |
2092 | mfindex = xhci_mfindex_get(xhci); |
2093 | xhci_calc_iso_kick(xhci, xfer, epctx, mfindex); | |
4d7a81c0 | 2094 | xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex); |
3d139684 GH |
2095 | if (xfer->running_retry) { |
2096 | return -1; | |
2097 | } | |
62c6ae04 HM |
2098 | break; |
2099 | default: | |
4f9cc734 | 2100 | trace_usb_xhci_unimplemented("endpoint type", epctx->type); |
62c6ae04 HM |
2101 | return -1; |
2102 | } | |
2103 | ||
5c08106f GH |
2104 | if (xhci_setup_packet(xfer) < 0) { |
2105 | return -1; | |
2106 | } | |
9a77a0f5 | 2107 | usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); |
62c6ae04 | 2108 | |
9a77a0f5 | 2109 | xhci_complete_packet(xfer); |
7c605a23 | 2110 | if (!xfer->running_async && !xfer->running_retry) { |
3a533ee8 | 2111 | xhci_kick_epctx(xfer->epctx, xfer->streamid); |
62c6ae04 HM |
2112 | } |
2113 | return 0; | |
2114 | } | |
2115 | ||
2116 | static int xhci_fire_transfer(XHCIState *xhci, XHCITransfer *xfer, XHCIEPContext *epctx) | |
2117 | { | |
d6fcb293 GH |
2118 | trace_usb_xhci_xfer_start(xfer, xfer->epctx->slotid, |
2119 | xfer->epctx->epid, xfer->streamid); | |
331e9406 | 2120 | return xhci_submit(xhci, xfer, epctx); |
62c6ae04 HM |
2121 | } |
2122 | ||
024426ac GH |
2123 | static void xhci_kick_ep(XHCIState *xhci, unsigned int slotid, |
2124 | unsigned int epid, unsigned int streamid) | |
62c6ae04 HM |
2125 | { |
2126 | XHCIEPContext *epctx; | |
62c6ae04 | 2127 | |
91062ae0 | 2128 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 | 2129 | assert(epid >= 1 && epid <= 31); |
62c6ae04 HM |
2130 | |
2131 | if (!xhci->slots[slotid-1].enabled) { | |
d6bb65fc | 2132 | DPRINTF("xhci: xhci_kick_ep for disabled slot %d\n", slotid); |
62c6ae04 HM |
2133 | return; |
2134 | } | |
2135 | epctx = xhci->slots[slotid-1].eps[epid-1]; | |
2136 | if (!epctx) { | |
d6bb65fc | 2137 | DPRINTF("xhci: xhci_kick_ep for disabled endpoint %d,%d\n", |
62c6ae04 HM |
2138 | epid, slotid); |
2139 | return; | |
2140 | } | |
2141 | ||
3a533ee8 GH |
2142 | xhci_kick_epctx(epctx, streamid); |
2143 | } | |
2144 | ||
2145 | static void xhci_kick_epctx(XHCIEPContext *epctx, unsigned int streamid) | |
2146 | { | |
2147 | XHCIState *xhci = epctx->xhci; | |
2148 | XHCIStreamContext *stctx; | |
2149 | XHCITransfer *xfer; | |
2150 | XHCIRing *ring; | |
2151 | USBEndpoint *ep = NULL; | |
2152 | uint64_t mfindex; | |
2153 | int length; | |
2154 | int i; | |
2155 | ||
2156 | trace_usb_xhci_ep_kick(epctx->slotid, epctx->epid, streamid); | |
2157 | ||
de9de157 HG |
2158 | /* If the device has been detached, but the guest has not noticed this |
2159 | yet the 2 above checks will succeed, but we must NOT continue */ | |
3a533ee8 GH |
2160 | if (!xhci->slots[epctx->slotid - 1].uport || |
2161 | !xhci->slots[epctx->slotid - 1].uport->dev || | |
2162 | !xhci->slots[epctx->slotid - 1].uport->dev->attached) { | |
de9de157 HG |
2163 | return; |
2164 | } | |
2165 | ||
7c605a23 | 2166 | if (epctx->retry) { |
7c605a23 | 2167 | XHCITransfer *xfer = epctx->retry; |
7c605a23 | 2168 | |
97df650b | 2169 | trace_usb_xhci_xfer_retry(xfer); |
7c605a23 | 2170 | assert(xfer->running_retry); |
4d7a81c0 GH |
2171 | if (xfer->timed_xfer) { |
2172 | /* time to kick the transfer? */ | |
3d139684 | 2173 | mfindex = xhci_mfindex_get(xhci); |
4d7a81c0 | 2174 | xhci_check_intr_iso_kick(xhci, xfer, epctx, mfindex); |
3d139684 GH |
2175 | if (xfer->running_retry) { |
2176 | return; | |
2177 | } | |
4d7a81c0 GH |
2178 | xfer->timed_xfer = 0; |
2179 | xfer->running_retry = 1; | |
2180 | } | |
2181 | if (xfer->iso_xfer) { | |
2182 | /* retry iso transfer */ | |
3d139684 GH |
2183 | if (xhci_setup_packet(xfer) < 0) { |
2184 | return; | |
2185 | } | |
9a77a0f5 HG |
2186 | usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); |
2187 | assert(xfer->packet.status != USB_RET_NAK); | |
2188 | xhci_complete_packet(xfer); | |
3d139684 GH |
2189 | } else { |
2190 | /* retry nak'ed transfer */ | |
2191 | if (xhci_setup_packet(xfer) < 0) { | |
2192 | return; | |
2193 | } | |
9a77a0f5 HG |
2194 | usb_handle_packet(xfer->packet.ep->dev, &xfer->packet); |
2195 | if (xfer->packet.status == USB_RET_NAK) { | |
3d139684 GH |
2196 | return; |
2197 | } | |
9a77a0f5 | 2198 | xhci_complete_packet(xfer); |
7c605a23 | 2199 | } |
7c605a23 | 2200 | assert(!xfer->running_retry); |
94b037f2 | 2201 | xhci_ep_free_xfer(epctx->retry); |
7c605a23 GH |
2202 | epctx->retry = NULL; |
2203 | } | |
2204 | ||
62c6ae04 HM |
2205 | if (epctx->state == EP_HALTED) { |
2206 | DPRINTF("xhci: ep halted, not running schedule\n"); | |
2207 | return; | |
2208 | } | |
2209 | ||
024426ac GH |
2210 | |
2211 | if (epctx->nr_pstreams) { | |
2212 | uint32_t err; | |
2213 | stctx = xhci_find_stream(epctx, streamid, &err); | |
2214 | if (stctx == NULL) { | |
2215 | return; | |
2216 | } | |
2217 | ring = &stctx->ring; | |
2218 | xhci_set_ep_state(xhci, epctx, stctx, EP_RUNNING); | |
2219 | } else { | |
2220 | ring = &epctx->ring; | |
2221 | streamid = 0; | |
2222 | xhci_set_ep_state(xhci, epctx, NULL, EP_RUNNING); | |
2223 | } | |
7d04c2b7 | 2224 | assert(ring->dequeue != 0); |
62c6ae04 HM |
2225 | |
2226 | while (1) { | |
024426ac | 2227 | length = xhci_ring_chain_length(xhci, ring); |
94b037f2 | 2228 | if (length <= 0) { |
62c6ae04 | 2229 | break; |
62c6ae04 | 2230 | } |
94b037f2 GH |
2231 | xfer = xhci_ep_alloc_xfer(epctx, length); |
2232 | if (xfer == NULL) { | |
2233 | break; | |
62c6ae04 | 2234 | } |
62c6ae04 HM |
2235 | |
2236 | for (i = 0; i < length; i++) { | |
f81bb347 AK |
2237 | TRBType type; |
2238 | type = xhci_ring_fetch(xhci, ring, &xfer->trbs[i], NULL); | |
2239 | assert(type); | |
62c6ae04 | 2240 | } |
024426ac | 2241 | xfer->streamid = streamid; |
62c6ae04 | 2242 | |
3a533ee8 | 2243 | if (epctx->epid == 1) { |
94b037f2 | 2244 | xhci_fire_ctl_transfer(xhci, xfer); |
62c6ae04 | 2245 | } else { |
94b037f2 GH |
2246 | xhci_fire_transfer(xhci, xfer, epctx); |
2247 | } | |
2248 | if (xfer->complete) { | |
2249 | xhci_ep_free_xfer(xfer); | |
2250 | xfer = NULL; | |
62c6ae04 HM |
2251 | } |
2252 | ||
3c4866e0 | 2253 | if (epctx->state == EP_HALTED) { |
3c4866e0 GH |
2254 | break; |
2255 | } | |
94b037f2 | 2256 | if (xfer != NULL && xfer->running_retry) { |
7c605a23 GH |
2257 | DPRINTF("xhci: xfer nacked, stopping schedule\n"); |
2258 | epctx->retry = xfer; | |
2259 | break; | |
2260 | } | |
62c6ae04 | 2261 | } |
518ad5f2 | 2262 | |
070eeef9 | 2263 | ep = xhci_epid_to_usbep(epctx); |
36dfe324 HG |
2264 | if (ep) { |
2265 | usb_device_flush_ep_queue(ep->dev, ep); | |
2266 | } | |
62c6ae04 HM |
2267 | } |
2268 | ||
2269 | static TRBCCode xhci_enable_slot(XHCIState *xhci, unsigned int slotid) | |
2270 | { | |
348f1037 | 2271 | trace_usb_xhci_slot_enable(slotid); |
91062ae0 | 2272 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 | 2273 | xhci->slots[slotid-1].enabled = 1; |
ccaf87a0 | 2274 | xhci->slots[slotid-1].uport = NULL; |
62c6ae04 HM |
2275 | memset(xhci->slots[slotid-1].eps, 0, sizeof(XHCIEPContext*)*31); |
2276 | ||
2277 | return CC_SUCCESS; | |
2278 | } | |
2279 | ||
2280 | static TRBCCode xhci_disable_slot(XHCIState *xhci, unsigned int slotid) | |
2281 | { | |
2282 | int i; | |
2283 | ||
348f1037 | 2284 | trace_usb_xhci_slot_disable(slotid); |
91062ae0 | 2285 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
2286 | |
2287 | for (i = 1; i <= 31; i++) { | |
2288 | if (xhci->slots[slotid-1].eps[i-1]) { | |
2289 | xhci_disable_ep(xhci, slotid, i); | |
2290 | } | |
2291 | } | |
2292 | ||
2293 | xhci->slots[slotid-1].enabled = 0; | |
4034e693 | 2294 | xhci->slots[slotid-1].addressed = 0; |
5c67dd7b | 2295 | xhci->slots[slotid-1].uport = NULL; |
62c6ae04 HM |
2296 | return CC_SUCCESS; |
2297 | } | |
2298 | ||
ccaf87a0 GH |
2299 | static USBPort *xhci_lookup_uport(XHCIState *xhci, uint32_t *slot_ctx) |
2300 | { | |
2301 | USBPort *uport; | |
2302 | char path[32]; | |
2303 | int i, pos, port; | |
2304 | ||
2305 | port = (slot_ctx[1]>>16) & 0xFF; | |
f2ad97ff GH |
2306 | if (port < 1 || port > xhci->numports) { |
2307 | return NULL; | |
2308 | } | |
ccaf87a0 GH |
2309 | port = xhci->ports[port-1].uport->index+1; |
2310 | pos = snprintf(path, sizeof(path), "%d", port); | |
2311 | for (i = 0; i < 5; i++) { | |
2312 | port = (slot_ctx[0] >> 4*i) & 0x0f; | |
2313 | if (!port) { | |
2314 | break; | |
2315 | } | |
2316 | pos += snprintf(path + pos, sizeof(path) - pos, ".%d", port); | |
2317 | } | |
2318 | ||
2319 | QTAILQ_FOREACH(uport, &xhci->bus.used, next) { | |
2320 | if (strcmp(uport->path, path) == 0) { | |
2321 | return uport; | |
2322 | } | |
2323 | } | |
2324 | return NULL; | |
2325 | } | |
2326 | ||
62c6ae04 HM |
2327 | static TRBCCode xhci_address_slot(XHCIState *xhci, unsigned int slotid, |
2328 | uint64_t pictx, bool bsr) | |
2329 | { | |
2330 | XHCISlot *slot; | |
ccaf87a0 | 2331 | USBPort *uport; |
62c6ae04 | 2332 | USBDevice *dev; |
59a70ccd | 2333 | dma_addr_t ictx, octx, dcbaap; |
62c6ae04 HM |
2334 | uint64_t poctx; |
2335 | uint32_t ictl_ctx[2]; | |
2336 | uint32_t slot_ctx[4]; | |
2337 | uint32_t ep0_ctx[5]; | |
62c6ae04 HM |
2338 | int i; |
2339 | TRBCCode res; | |
2340 | ||
91062ae0 | 2341 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
2342 | |
2343 | dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high); | |
9b7d3334 | 2344 | poctx = ldq_le_pci_dma(PCI_DEVICE(xhci), dcbaap + 8 * slotid); |
62c6ae04 | 2345 | ictx = xhci_mask64(pictx); |
616b5d53 | 2346 | octx = xhci_mask64(poctx); |
62c6ae04 | 2347 | |
59a70ccd DG |
2348 | DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); |
2349 | DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); | |
62c6ae04 | 2350 | |
616b5d53 | 2351 | xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx)); |
62c6ae04 HM |
2352 | |
2353 | if (ictl_ctx[0] != 0x0 || ictl_ctx[1] != 0x3) { | |
d6bb65fc | 2354 | DPRINTF("xhci: invalid input context control %08x %08x\n", |
62c6ae04 HM |
2355 | ictl_ctx[0], ictl_ctx[1]); |
2356 | return CC_TRB_ERROR; | |
2357 | } | |
2358 | ||
616b5d53 DG |
2359 | xhci_dma_read_u32s(xhci, ictx+32, slot_ctx, sizeof(slot_ctx)); |
2360 | xhci_dma_read_u32s(xhci, ictx+64, ep0_ctx, sizeof(ep0_ctx)); | |
62c6ae04 HM |
2361 | |
2362 | DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n", | |
2363 | slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); | |
2364 | ||
2365 | DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n", | |
2366 | ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]); | |
2367 | ||
ccaf87a0 GH |
2368 | uport = xhci_lookup_uport(xhci, slot_ctx); |
2369 | if (uport == NULL) { | |
d6bb65fc | 2370 | DPRINTF("xhci: port not found\n"); |
62c6ae04 | 2371 | return CC_TRB_ERROR; |
ccaf87a0 | 2372 | } |
65d81ed4 | 2373 | trace_usb_xhci_slot_address(slotid, uport->path); |
ccaf87a0 GH |
2374 | |
2375 | dev = uport->dev; | |
de9de157 | 2376 | if (!dev || !dev->attached) { |
d6bb65fc | 2377 | DPRINTF("xhci: port %s not connected\n", uport->path); |
62c6ae04 HM |
2378 | return CC_USB_TRANSACTION_ERROR; |
2379 | } | |
2380 | ||
91062ae0 | 2381 | for (i = 0; i < xhci->numslots; i++) { |
0bc85da6 GH |
2382 | if (i == slotid-1) { |
2383 | continue; | |
2384 | } | |
ccaf87a0 | 2385 | if (xhci->slots[i].uport == uport) { |
d6bb65fc | 2386 | DPRINTF("xhci: port %s already assigned to slot %d\n", |
ccaf87a0 | 2387 | uport->path, i+1); |
62c6ae04 HM |
2388 | return CC_TRB_ERROR; |
2389 | } | |
2390 | } | |
2391 | ||
2392 | slot = &xhci->slots[slotid-1]; | |
ccaf87a0 | 2393 | slot->uport = uport; |
62c6ae04 HM |
2394 | slot->ctx = octx; |
2395 | ||
a4055d85 ZS |
2396 | /* Make sure device is in USB_STATE_DEFAULT state */ |
2397 | usb_device_reset(dev); | |
62c6ae04 HM |
2398 | if (bsr) { |
2399 | slot_ctx[3] = SLOT_DEFAULT << SLOT_STATE_SHIFT; | |
2400 | } else { | |
a820b575 | 2401 | USBPacket p; |
a6718874 GH |
2402 | uint8_t buf[1]; |
2403 | ||
af203be3 | 2404 | slot_ctx[3] = (SLOT_ADDRESSED << SLOT_STATE_SHIFT) | slotid; |
a6718874 GH |
2405 | memset(&p, 0, sizeof(p)); |
2406 | usb_packet_addbuf(&p, buf, sizeof(buf)); | |
a820b575 | 2407 | usb_packet_setup(&p, USB_TOKEN_OUT, |
8550a02d | 2408 | usb_ep_get(dev, USB_TOKEN_OUT, 0), 0, |
a820b575 GH |
2409 | 0, false, false); |
2410 | usb_device_handle_control(dev, &p, | |
62c6ae04 | 2411 | DeviceOutRequest | USB_REQ_SET_ADDRESS, |
af203be3 | 2412 | slotid, 0, 0, NULL); |
a820b575 | 2413 | assert(p.status != USB_RET_ASYNC); |
62c6ae04 HM |
2414 | } |
2415 | ||
2416 | res = xhci_enable_ep(xhci, slotid, 1, octx+32, ep0_ctx); | |
2417 | ||
2418 | DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", | |
2419 | slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); | |
2420 | DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n", | |
2421 | ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]); | |
2422 | ||
616b5d53 DG |
2423 | xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
2424 | xhci_dma_write_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx)); | |
62c6ae04 | 2425 | |
4034e693 | 2426 | xhci->slots[slotid-1].addressed = 1; |
62c6ae04 HM |
2427 | return res; |
2428 | } | |
2429 | ||
2430 | ||
2431 | static TRBCCode xhci_configure_slot(XHCIState *xhci, unsigned int slotid, | |
2432 | uint64_t pictx, bool dc) | |
2433 | { | |
59a70ccd | 2434 | dma_addr_t ictx, octx; |
62c6ae04 HM |
2435 | uint32_t ictl_ctx[2]; |
2436 | uint32_t slot_ctx[4]; | |
2437 | uint32_t islot_ctx[4]; | |
2438 | uint32_t ep_ctx[5]; | |
2439 | int i; | |
2440 | TRBCCode res; | |
2441 | ||
348f1037 | 2442 | trace_usb_xhci_slot_configure(slotid); |
91062ae0 | 2443 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
2444 | |
2445 | ictx = xhci_mask64(pictx); | |
2446 | octx = xhci->slots[slotid-1].ctx; | |
2447 | ||
59a70ccd DG |
2448 | DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); |
2449 | DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); | |
62c6ae04 HM |
2450 | |
2451 | if (dc) { | |
2452 | for (i = 2; i <= 31; i++) { | |
2453 | if (xhci->slots[slotid-1].eps[i-1]) { | |
2454 | xhci_disable_ep(xhci, slotid, i); | |
2455 | } | |
2456 | } | |
2457 | ||
616b5d53 | 2458 | xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2459 | slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT); |
2460 | slot_ctx[3] |= SLOT_ADDRESSED << SLOT_STATE_SHIFT; | |
2461 | DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", | |
2462 | slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); | |
616b5d53 | 2463 | xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2464 | |
2465 | return CC_SUCCESS; | |
2466 | } | |
2467 | ||
616b5d53 | 2468 | xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx)); |
62c6ae04 HM |
2469 | |
2470 | if ((ictl_ctx[0] & 0x3) != 0x0 || (ictl_ctx[1] & 0x3) != 0x1) { | |
d6bb65fc | 2471 | DPRINTF("xhci: invalid input context control %08x %08x\n", |
62c6ae04 HM |
2472 | ictl_ctx[0], ictl_ctx[1]); |
2473 | return CC_TRB_ERROR; | |
2474 | } | |
2475 | ||
616b5d53 DG |
2476 | xhci_dma_read_u32s(xhci, ictx+32, islot_ctx, sizeof(islot_ctx)); |
2477 | xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); | |
62c6ae04 HM |
2478 | |
2479 | if (SLOT_STATE(slot_ctx[3]) < SLOT_ADDRESSED) { | |
d6bb65fc | 2480 | DPRINTF("xhci: invalid slot state %08x\n", slot_ctx[3]); |
62c6ae04 HM |
2481 | return CC_CONTEXT_STATE_ERROR; |
2482 | } | |
2483 | ||
72391da5 HG |
2484 | xhci_free_device_streams(xhci, slotid, ictl_ctx[0] | ictl_ctx[1]); |
2485 | ||
62c6ae04 HM |
2486 | for (i = 2; i <= 31; i++) { |
2487 | if (ictl_ctx[0] & (1<<i)) { | |
2488 | xhci_disable_ep(xhci, slotid, i); | |
2489 | } | |
2490 | if (ictl_ctx[1] & (1<<i)) { | |
616b5d53 | 2491 | xhci_dma_read_u32s(xhci, ictx+32+(32*i), ep_ctx, sizeof(ep_ctx)); |
62c6ae04 HM |
2492 | DPRINTF("xhci: input ep%d.%d context: %08x %08x %08x %08x %08x\n", |
2493 | i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2], | |
2494 | ep_ctx[3], ep_ctx[4]); | |
2495 | xhci_disable_ep(xhci, slotid, i); | |
2496 | res = xhci_enable_ep(xhci, slotid, i, octx+(32*i), ep_ctx); | |
2497 | if (res != CC_SUCCESS) { | |
2498 | return res; | |
2499 | } | |
2500 | DPRINTF("xhci: output ep%d.%d context: %08x %08x %08x %08x %08x\n", | |
2501 | i/2, i%2, ep_ctx[0], ep_ctx[1], ep_ctx[2], | |
2502 | ep_ctx[3], ep_ctx[4]); | |
616b5d53 | 2503 | xhci_dma_write_u32s(xhci, octx+(32*i), ep_ctx, sizeof(ep_ctx)); |
62c6ae04 HM |
2504 | } |
2505 | } | |
2506 | ||
72391da5 HG |
2507 | res = xhci_alloc_device_streams(xhci, slotid, ictl_ctx[1]); |
2508 | if (res != CC_SUCCESS) { | |
2509 | for (i = 2; i <= 31; i++) { | |
3d80365b | 2510 | if (ictl_ctx[1] & (1u << i)) { |
72391da5 HG |
2511 | xhci_disable_ep(xhci, slotid, i); |
2512 | } | |
2513 | } | |
2514 | return res; | |
2515 | } | |
2516 | ||
62c6ae04 HM |
2517 | slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT); |
2518 | slot_ctx[3] |= SLOT_CONFIGURED << SLOT_STATE_SHIFT; | |
2519 | slot_ctx[0] &= ~(SLOT_CONTEXT_ENTRIES_MASK << SLOT_CONTEXT_ENTRIES_SHIFT); | |
2520 | slot_ctx[0] |= islot_ctx[0] & (SLOT_CONTEXT_ENTRIES_MASK << | |
2521 | SLOT_CONTEXT_ENTRIES_SHIFT); | |
2522 | DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", | |
2523 | slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); | |
2524 | ||
616b5d53 | 2525 | xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2526 | |
2527 | return CC_SUCCESS; | |
2528 | } | |
2529 | ||
2530 | ||
2531 | static TRBCCode xhci_evaluate_slot(XHCIState *xhci, unsigned int slotid, | |
2532 | uint64_t pictx) | |
2533 | { | |
59a70ccd | 2534 | dma_addr_t ictx, octx; |
62c6ae04 HM |
2535 | uint32_t ictl_ctx[2]; |
2536 | uint32_t iep0_ctx[5]; | |
2537 | uint32_t ep0_ctx[5]; | |
2538 | uint32_t islot_ctx[4]; | |
2539 | uint32_t slot_ctx[4]; | |
2540 | ||
348f1037 | 2541 | trace_usb_xhci_slot_evaluate(slotid); |
91062ae0 | 2542 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
2543 | |
2544 | ictx = xhci_mask64(pictx); | |
2545 | octx = xhci->slots[slotid-1].ctx; | |
2546 | ||
59a70ccd DG |
2547 | DPRINTF("xhci: input context at "DMA_ADDR_FMT"\n", ictx); |
2548 | DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); | |
62c6ae04 | 2549 | |
616b5d53 | 2550 | xhci_dma_read_u32s(xhci, ictx, ictl_ctx, sizeof(ictl_ctx)); |
62c6ae04 HM |
2551 | |
2552 | if (ictl_ctx[0] != 0x0 || ictl_ctx[1] & ~0x3) { | |
d6bb65fc | 2553 | DPRINTF("xhci: invalid input context control %08x %08x\n", |
62c6ae04 HM |
2554 | ictl_ctx[0], ictl_ctx[1]); |
2555 | return CC_TRB_ERROR; | |
2556 | } | |
2557 | ||
2558 | if (ictl_ctx[1] & 0x1) { | |
616b5d53 | 2559 | xhci_dma_read_u32s(xhci, ictx+32, islot_ctx, sizeof(islot_ctx)); |
62c6ae04 HM |
2560 | |
2561 | DPRINTF("xhci: input slot context: %08x %08x %08x %08x\n", | |
2562 | islot_ctx[0], islot_ctx[1], islot_ctx[2], islot_ctx[3]); | |
2563 | ||
616b5d53 | 2564 | xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2565 | |
2566 | slot_ctx[1] &= ~0xFFFF; /* max exit latency */ | |
2567 | slot_ctx[1] |= islot_ctx[1] & 0xFFFF; | |
2568 | slot_ctx[2] &= ~0xFF00000; /* interrupter target */ | |
2569 | slot_ctx[2] |= islot_ctx[2] & 0xFF000000; | |
2570 | ||
2571 | DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", | |
2572 | slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); | |
2573 | ||
616b5d53 | 2574 | xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2575 | } |
2576 | ||
2577 | if (ictl_ctx[1] & 0x2) { | |
616b5d53 | 2578 | xhci_dma_read_u32s(xhci, ictx+64, iep0_ctx, sizeof(iep0_ctx)); |
62c6ae04 HM |
2579 | |
2580 | DPRINTF("xhci: input ep0 context: %08x %08x %08x %08x %08x\n", | |
2581 | iep0_ctx[0], iep0_ctx[1], iep0_ctx[2], | |
2582 | iep0_ctx[3], iep0_ctx[4]); | |
2583 | ||
616b5d53 | 2584 | xhci_dma_read_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx)); |
62c6ae04 HM |
2585 | |
2586 | ep0_ctx[1] &= ~0xFFFF0000; /* max packet size*/ | |
2587 | ep0_ctx[1] |= iep0_ctx[1] & 0xFFFF0000; | |
2588 | ||
2589 | DPRINTF("xhci: output ep0 context: %08x %08x %08x %08x %08x\n", | |
2590 | ep0_ctx[0], ep0_ctx[1], ep0_ctx[2], ep0_ctx[3], ep0_ctx[4]); | |
2591 | ||
616b5d53 | 2592 | xhci_dma_write_u32s(xhci, octx+32, ep0_ctx, sizeof(ep0_ctx)); |
62c6ae04 HM |
2593 | } |
2594 | ||
2595 | return CC_SUCCESS; | |
2596 | } | |
2597 | ||
2598 | static TRBCCode xhci_reset_slot(XHCIState *xhci, unsigned int slotid) | |
2599 | { | |
2600 | uint32_t slot_ctx[4]; | |
59a70ccd | 2601 | dma_addr_t octx; |
62c6ae04 HM |
2602 | int i; |
2603 | ||
348f1037 | 2604 | trace_usb_xhci_slot_reset(slotid); |
91062ae0 | 2605 | assert(slotid >= 1 && slotid <= xhci->numslots); |
62c6ae04 HM |
2606 | |
2607 | octx = xhci->slots[slotid-1].ctx; | |
2608 | ||
59a70ccd | 2609 | DPRINTF("xhci: output context at "DMA_ADDR_FMT"\n", octx); |
62c6ae04 HM |
2610 | |
2611 | for (i = 2; i <= 31; i++) { | |
2612 | if (xhci->slots[slotid-1].eps[i-1]) { | |
2613 | xhci_disable_ep(xhci, slotid, i); | |
2614 | } | |
2615 | } | |
2616 | ||
616b5d53 | 2617 | xhci_dma_read_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2618 | slot_ctx[3] &= ~(SLOT_STATE_MASK << SLOT_STATE_SHIFT); |
2619 | slot_ctx[3] |= SLOT_DEFAULT << SLOT_STATE_SHIFT; | |
2620 | DPRINTF("xhci: output slot context: %08x %08x %08x %08x\n", | |
2621 | slot_ctx[0], slot_ctx[1], slot_ctx[2], slot_ctx[3]); | |
616b5d53 | 2622 | xhci_dma_write_u32s(xhci, octx, slot_ctx, sizeof(slot_ctx)); |
62c6ae04 HM |
2623 | |
2624 | return CC_SUCCESS; | |
2625 | } | |
2626 | ||
2627 | static unsigned int xhci_get_slot(XHCIState *xhci, XHCIEvent *event, XHCITRB *trb) | |
2628 | { | |
2629 | unsigned int slotid; | |
2630 | slotid = (trb->control >> TRB_CR_SLOTID_SHIFT) & TRB_CR_SLOTID_MASK; | |
91062ae0 | 2631 | if (slotid < 1 || slotid > xhci->numslots) { |
d6bb65fc | 2632 | DPRINTF("xhci: bad slot id %d\n", slotid); |
62c6ae04 HM |
2633 | event->ccode = CC_TRB_ERROR; |
2634 | return 0; | |
2635 | } else if (!xhci->slots[slotid-1].enabled) { | |
d6bb65fc | 2636 | DPRINTF("xhci: slot id %d not enabled\n", slotid); |
62c6ae04 HM |
2637 | event->ccode = CC_SLOT_NOT_ENABLED_ERROR; |
2638 | return 0; | |
2639 | } | |
2640 | return slotid; | |
2641 | } | |
2642 | ||
81251841 GH |
2643 | /* cleanup slot state on usb device detach */ |
2644 | static void xhci_detach_slot(XHCIState *xhci, USBPort *uport) | |
2645 | { | |
0cb41e2c | 2646 | int slot, ep; |
81251841 GH |
2647 | |
2648 | for (slot = 0; slot < xhci->numslots; slot++) { | |
2649 | if (xhci->slots[slot].uport == uport) { | |
2650 | break; | |
2651 | } | |
2652 | } | |
2653 | if (slot == xhci->numslots) { | |
2654 | return; | |
2655 | } | |
2656 | ||
0cb41e2c GH |
2657 | for (ep = 0; ep < 31; ep++) { |
2658 | if (xhci->slots[slot].eps[ep]) { | |
582d6f4a | 2659 | xhci_ep_nuke_xfers(xhci, slot + 1, ep + 1, 0); |
0cb41e2c GH |
2660 | } |
2661 | } | |
81251841 GH |
2662 | xhci->slots[slot].uport = NULL; |
2663 | } | |
2664 | ||
62c6ae04 HM |
2665 | static TRBCCode xhci_get_port_bandwidth(XHCIState *xhci, uint64_t pctx) |
2666 | { | |
59a70ccd | 2667 | dma_addr_t ctx; |
0846e635 | 2668 | uint8_t bw_ctx[xhci->numports+1]; |
62c6ae04 HM |
2669 | |
2670 | DPRINTF("xhci_get_port_bandwidth()\n"); | |
2671 | ||
2672 | ctx = xhci_mask64(pctx); | |
2673 | ||
59a70ccd | 2674 | DPRINTF("xhci: bandwidth context at "DMA_ADDR_FMT"\n", ctx); |
62c6ae04 HM |
2675 | |
2676 | /* TODO: actually implement real values here */ | |
2677 | bw_ctx[0] = 0; | |
0846e635 | 2678 | memset(&bw_ctx[1], 80, xhci->numports); /* 80% */ |
9b7d3334 | 2679 | pci_dma_write(PCI_DEVICE(xhci), ctx, bw_ctx, sizeof(bw_ctx)); |
62c6ae04 HM |
2680 | |
2681 | return CC_SUCCESS; | |
2682 | } | |
2683 | ||
2684 | static uint32_t rotl(uint32_t v, unsigned count) | |
2685 | { | |
2686 | count &= 31; | |
2687 | return (v << count) | (v >> (32 - count)); | |
2688 | } | |
2689 | ||
2690 | ||
2691 | static uint32_t xhci_nec_challenge(uint32_t hi, uint32_t lo) | |
2692 | { | |
2693 | uint32_t val; | |
2694 | val = rotl(lo - 0x49434878, 32 - ((hi>>8) & 0x1F)); | |
2695 | val += rotl(lo + 0x49434878, hi & 0x1F); | |
2696 | val -= rotl(hi ^ 0x49434878, (lo >> 16) & 0x1F); | |
2697 | return ~val; | |
2698 | } | |
2699 | ||
59a70ccd | 2700 | static void xhci_via_challenge(XHCIState *xhci, uint64_t addr) |
62c6ae04 | 2701 | { |
9b7d3334 | 2702 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
62c6ae04 HM |
2703 | uint32_t buf[8]; |
2704 | uint32_t obuf[8]; | |
59a70ccd | 2705 | dma_addr_t paddr = xhci_mask64(addr); |
62c6ae04 | 2706 | |
9b7d3334 | 2707 | pci_dma_read(pci_dev, paddr, &buf, 32); |
62c6ae04 HM |
2708 | |
2709 | memcpy(obuf, buf, sizeof(obuf)); | |
2710 | ||
2711 | if ((buf[0] & 0xff) == 2) { | |
2712 | obuf[0] = 0x49932000 + 0x54dc200 * buf[2] + 0x7429b578 * buf[3]; | |
2713 | obuf[0] |= (buf[2] * buf[3]) & 0xff; | |
2714 | obuf[1] = 0x0132bb37 + 0xe89 * buf[2] + 0xf09 * buf[3]; | |
2715 | obuf[2] = 0x0066c2e9 + 0x2091 * buf[2] + 0x19bd * buf[3]; | |
2716 | obuf[3] = 0xd5281342 + 0x2cc9691 * buf[2] + 0x2367662 * buf[3]; | |
2717 | obuf[4] = 0x0123c75c + 0x1595 * buf[2] + 0x19ec * buf[3]; | |
2718 | obuf[5] = 0x00f695de + 0x26fd * buf[2] + 0x3e9 * buf[3]; | |
2719 | obuf[6] = obuf[2] ^ obuf[3] ^ 0x29472956; | |
2720 | obuf[7] = obuf[2] ^ obuf[3] ^ 0x65866593; | |
2721 | } | |
2722 | ||
9b7d3334 | 2723 | pci_dma_write(pci_dev, paddr, &obuf, 32); |
62c6ae04 HM |
2724 | } |
2725 | ||
2726 | static void xhci_process_commands(XHCIState *xhci) | |
2727 | { | |
2728 | XHCITRB trb; | |
2729 | TRBType type; | |
2730 | XHCIEvent event = {ER_COMMAND_COMPLETE, CC_SUCCESS}; | |
59a70ccd | 2731 | dma_addr_t addr; |
62c6ae04 HM |
2732 | unsigned int i, slotid = 0; |
2733 | ||
2734 | DPRINTF("xhci_process_commands()\n"); | |
2735 | if (!xhci_running(xhci)) { | |
2736 | DPRINTF("xhci_process_commands() called while xHC stopped or paused\n"); | |
2737 | return; | |
2738 | } | |
2739 | ||
2740 | xhci->crcr_low |= CRCR_CRR; | |
2741 | ||
2742 | while ((type = xhci_ring_fetch(xhci, &xhci->cmd_ring, &trb, &addr))) { | |
2743 | event.ptr = addr; | |
2744 | switch (type) { | |
2745 | case CR_ENABLE_SLOT: | |
91062ae0 | 2746 | for (i = 0; i < xhci->numslots; i++) { |
62c6ae04 HM |
2747 | if (!xhci->slots[i].enabled) { |
2748 | break; | |
2749 | } | |
2750 | } | |
91062ae0 | 2751 | if (i >= xhci->numslots) { |
d6bb65fc | 2752 | DPRINTF("xhci: no device slots available\n"); |
62c6ae04 HM |
2753 | event.ccode = CC_NO_SLOTS_ERROR; |
2754 | } else { | |
2755 | slotid = i+1; | |
2756 | event.ccode = xhci_enable_slot(xhci, slotid); | |
2757 | } | |
2758 | break; | |
2759 | case CR_DISABLE_SLOT: | |
2760 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2761 | if (slotid) { | |
2762 | event.ccode = xhci_disable_slot(xhci, slotid); | |
2763 | } | |
2764 | break; | |
2765 | case CR_ADDRESS_DEVICE: | |
2766 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2767 | if (slotid) { | |
2768 | event.ccode = xhci_address_slot(xhci, slotid, trb.parameter, | |
2769 | trb.control & TRB_CR_BSR); | |
2770 | } | |
2771 | break; | |
2772 | case CR_CONFIGURE_ENDPOINT: | |
2773 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2774 | if (slotid) { | |
2775 | event.ccode = xhci_configure_slot(xhci, slotid, trb.parameter, | |
2776 | trb.control & TRB_CR_DC); | |
2777 | } | |
2778 | break; | |
2779 | case CR_EVALUATE_CONTEXT: | |
2780 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2781 | if (slotid) { | |
2782 | event.ccode = xhci_evaluate_slot(xhci, slotid, trb.parameter); | |
2783 | } | |
2784 | break; | |
2785 | case CR_STOP_ENDPOINT: | |
2786 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2787 | if (slotid) { | |
2788 | unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT) | |
2789 | & TRB_CR_EPID_MASK; | |
2790 | event.ccode = xhci_stop_ep(xhci, slotid, epid); | |
2791 | } | |
2792 | break; | |
2793 | case CR_RESET_ENDPOINT: | |
2794 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2795 | if (slotid) { | |
2796 | unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT) | |
2797 | & TRB_CR_EPID_MASK; | |
2798 | event.ccode = xhci_reset_ep(xhci, slotid, epid); | |
2799 | } | |
2800 | break; | |
2801 | case CR_SET_TR_DEQUEUE: | |
2802 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2803 | if (slotid) { | |
2804 | unsigned int epid = (trb.control >> TRB_CR_EPID_SHIFT) | |
2805 | & TRB_CR_EPID_MASK; | |
024426ac GH |
2806 | unsigned int streamid = (trb.status >> 16) & 0xffff; |
2807 | event.ccode = xhci_set_ep_dequeue(xhci, slotid, | |
2808 | epid, streamid, | |
62c6ae04 HM |
2809 | trb.parameter); |
2810 | } | |
2811 | break; | |
2812 | case CR_RESET_DEVICE: | |
2813 | slotid = xhci_get_slot(xhci, &event, &trb); | |
2814 | if (slotid) { | |
2815 | event.ccode = xhci_reset_slot(xhci, slotid); | |
2816 | } | |
2817 | break; | |
2818 | case CR_GET_PORT_BANDWIDTH: | |
2819 | event.ccode = xhci_get_port_bandwidth(xhci, trb.parameter); | |
2820 | break; | |
2821 | case CR_VENDOR_VIA_CHALLENGE_RESPONSE: | |
59a70ccd | 2822 | xhci_via_challenge(xhci, trb.parameter); |
62c6ae04 HM |
2823 | break; |
2824 | case CR_VENDOR_NEC_FIRMWARE_REVISION: | |
2825 | event.type = 48; /* NEC reply */ | |
2826 | event.length = 0x3025; | |
2827 | break; | |
2828 | case CR_VENDOR_NEC_CHALLENGE_RESPONSE: | |
2829 | { | |
2830 | uint32_t chi = trb.parameter >> 32; | |
2831 | uint32_t clo = trb.parameter; | |
2832 | uint32_t val = xhci_nec_challenge(chi, clo); | |
2833 | event.length = val & 0xFFFF; | |
2834 | event.epid = val >> 16; | |
2835 | slotid = val >> 24; | |
2836 | event.type = 48; /* NEC reply */ | |
2837 | } | |
2838 | break; | |
2839 | default: | |
0ab966cf | 2840 | trace_usb_xhci_unimplemented("command", type); |
62c6ae04 HM |
2841 | event.ccode = CC_TRB_ERROR; |
2842 | break; | |
2843 | } | |
2844 | event.slotid = slotid; | |
2d1de850 | 2845 | xhci_event(xhci, &event, 0); |
62c6ae04 HM |
2846 | } |
2847 | } | |
2848 | ||
6a32f80f GH |
2849 | static bool xhci_port_have_device(XHCIPort *port) |
2850 | { | |
2851 | if (!port->uport->dev || !port->uport->dev->attached) { | |
2852 | return false; /* no device present */ | |
2853 | } | |
2854 | if (!((1 << port->uport->dev->speed) & port->speedmask)) { | |
2855 | return false; /* speed mismatch */ | |
2856 | } | |
2857 | return true; | |
2858 | } | |
2859 | ||
f705a362 GH |
2860 | static void xhci_port_notify(XHCIPort *port, uint32_t bits) |
2861 | { | |
2862 | XHCIEvent ev = { ER_PORT_STATUS_CHANGE, CC_SUCCESS, | |
2863 | port->portnr << 24 }; | |
2864 | ||
2865 | if ((port->portsc & bits) == bits) { | |
2866 | return; | |
2867 | } | |
bdfce20d | 2868 | trace_usb_xhci_port_notify(port->portnr, bits); |
f705a362 GH |
2869 | port->portsc |= bits; |
2870 | if (!xhci_running(port->xhci)) { | |
2871 | return; | |
2872 | } | |
2873 | xhci_event(port->xhci, &ev, 0); | |
2874 | } | |
2875 | ||
f3214027 | 2876 | static void xhci_port_update(XHCIPort *port, int is_detach) |
62c6ae04 | 2877 | { |
b62b0828 GH |
2878 | uint32_t pls = PLS_RX_DETECT; |
2879 | ||
62c6ae04 | 2880 | port->portsc = PORTSC_PP; |
6a32f80f | 2881 | if (!is_detach && xhci_port_have_device(port)) { |
62c6ae04 | 2882 | port->portsc |= PORTSC_CCS; |
0846e635 | 2883 | switch (port->uport->dev->speed) { |
62c6ae04 HM |
2884 | case USB_SPEED_LOW: |
2885 | port->portsc |= PORTSC_SPEED_LOW; | |
b62b0828 | 2886 | pls = PLS_POLLING; |
62c6ae04 HM |
2887 | break; |
2888 | case USB_SPEED_FULL: | |
2889 | port->portsc |= PORTSC_SPEED_FULL; | |
b62b0828 | 2890 | pls = PLS_POLLING; |
62c6ae04 HM |
2891 | break; |
2892 | case USB_SPEED_HIGH: | |
2893 | port->portsc |= PORTSC_SPEED_HIGH; | |
b62b0828 | 2894 | pls = PLS_POLLING; |
62c6ae04 | 2895 | break; |
0846e635 GH |
2896 | case USB_SPEED_SUPER: |
2897 | port->portsc |= PORTSC_SPEED_SUPER; | |
b62b0828 GH |
2898 | port->portsc |= PORTSC_PED; |
2899 | pls = PLS_U0; | |
0846e635 | 2900 | break; |
62c6ae04 HM |
2901 | } |
2902 | } | |
b62b0828 | 2903 | set_field(&port->portsc, pls, PORTSC_PLS); |
4f47f0f8 | 2904 | trace_usb_xhci_port_link(port->portnr, pls); |
f705a362 | 2905 | xhci_port_notify(port, PORTSC_CSC); |
62c6ae04 HM |
2906 | } |
2907 | ||
dad5b9ea | 2908 | static void xhci_port_reset(XHCIPort *port, bool warm_reset) |
40030130 | 2909 | { |
7bd3055f | 2910 | trace_usb_xhci_port_reset(port->portnr, warm_reset); |
4f47f0f8 | 2911 | |
b62b0828 GH |
2912 | if (!xhci_port_have_device(port)) { |
2913 | return; | |
2914 | } | |
2915 | ||
40030130 | 2916 | usb_device_reset(port->uport->dev); |
b62b0828 GH |
2917 | |
2918 | switch (port->uport->dev->speed) { | |
dad5b9ea GH |
2919 | case USB_SPEED_SUPER: |
2920 | if (warm_reset) { | |
2921 | port->portsc |= PORTSC_WRC; | |
2922 | } | |
2923 | /* fall through */ | |
b62b0828 GH |
2924 | case USB_SPEED_LOW: |
2925 | case USB_SPEED_FULL: | |
2926 | case USB_SPEED_HIGH: | |
2927 | set_field(&port->portsc, PLS_U0, PORTSC_PLS); | |
4f47f0f8 | 2928 | trace_usb_xhci_port_link(port->portnr, PLS_U0); |
b62b0828 GH |
2929 | port->portsc |= PORTSC_PED; |
2930 | break; | |
2931 | } | |
2932 | ||
2933 | port->portsc &= ~PORTSC_PR; | |
2934 | xhci_port_notify(port, PORTSC_PRC); | |
40030130 GH |
2935 | } |
2936 | ||
64619739 | 2937 | static void xhci_reset(DeviceState *dev) |
62c6ae04 | 2938 | { |
37034575 | 2939 | XHCIState *xhci = XHCI(dev); |
62c6ae04 HM |
2940 | int i; |
2941 | ||
2d754a10 | 2942 | trace_usb_xhci_reset(); |
62c6ae04 | 2943 | if (!(xhci->usbsts & USBSTS_HCH)) { |
d6bb65fc | 2944 | DPRINTF("xhci: reset while running!\n"); |
62c6ae04 HM |
2945 | } |
2946 | ||
2947 | xhci->usbcmd = 0; | |
2948 | xhci->usbsts = USBSTS_HCH; | |
2949 | xhci->dnctrl = 0; | |
2950 | xhci->crcr_low = 0; | |
2951 | xhci->crcr_high = 0; | |
2952 | xhci->dcbaap_low = 0; | |
2953 | xhci->dcbaap_high = 0; | |
2954 | xhci->config = 0; | |
62c6ae04 | 2955 | |
91062ae0 | 2956 | for (i = 0; i < xhci->numslots; i++) { |
62c6ae04 HM |
2957 | xhci_disable_slot(xhci, i+1); |
2958 | } | |
2959 | ||
0846e635 | 2960 | for (i = 0; i < xhci->numports; i++) { |
f3214027 | 2961 | xhci_port_update(xhci->ports + i, 0); |
62c6ae04 HM |
2962 | } |
2963 | ||
91062ae0 | 2964 | for (i = 0; i < xhci->numintrs; i++) { |
962d11e1 GH |
2965 | xhci->intr[i].iman = 0; |
2966 | xhci->intr[i].imod = 0; | |
2967 | xhci->intr[i].erstsz = 0; | |
2968 | xhci->intr[i].erstba_low = 0; | |
2969 | xhci->intr[i].erstba_high = 0; | |
2970 | xhci->intr[i].erdp_low = 0; | |
2971 | xhci->intr[i].erdp_high = 0; | |
2972 | xhci->intr[i].msix_used = 0; | |
62c6ae04 | 2973 | |
962d11e1 GH |
2974 | xhci->intr[i].er_ep_idx = 0; |
2975 | xhci->intr[i].er_pcs = 1; | |
2976 | xhci->intr[i].er_full = 0; | |
2977 | xhci->intr[i].ev_buffer_put = 0; | |
2978 | xhci->intr[i].ev_buffer_get = 0; | |
2979 | } | |
01546fa6 | 2980 | |
bc72ad67 | 2981 | xhci->mfindex_start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
01546fa6 | 2982 | xhci_mfwrap_update(xhci); |
62c6ae04 HM |
2983 | } |
2984 | ||
a8170e5e | 2985 | static uint64_t xhci_cap_read(void *ptr, hwaddr reg, unsigned size) |
62c6ae04 | 2986 | { |
1b067564 | 2987 | XHCIState *xhci = ptr; |
2d754a10 | 2988 | uint32_t ret; |
62c6ae04 HM |
2989 | |
2990 | switch (reg) { | |
2991 | case 0x00: /* HCIVERSION, CAPLENGTH */ | |
2d754a10 GH |
2992 | ret = 0x01000000 | LEN_CAP; |
2993 | break; | |
62c6ae04 | 2994 | case 0x04: /* HCSPARAMS 1 */ |
0846e635 | 2995 | ret = ((xhci->numports_2+xhci->numports_3)<<24) |
91062ae0 | 2996 | | (xhci->numintrs<<8) | xhci->numslots; |
2d754a10 | 2997 | break; |
62c6ae04 | 2998 | case 0x08: /* HCSPARAMS 2 */ |
2d754a10 GH |
2999 | ret = 0x0000000f; |
3000 | break; | |
62c6ae04 | 3001 | case 0x0c: /* HCSPARAMS 3 */ |
2d754a10 GH |
3002 | ret = 0x00000000; |
3003 | break; | |
62c6ae04 | 3004 | case 0x10: /* HCCPARAMS */ |
2d754a10 | 3005 | if (sizeof(dma_addr_t) == 4) { |
2aa6bfcb | 3006 | ret = 0x00080000 | (xhci->max_pstreams_mask << 12); |
2d754a10 | 3007 | } else { |
2aa6bfcb | 3008 | ret = 0x00080001 | (xhci->max_pstreams_mask << 12); |
2d754a10 GH |
3009 | } |
3010 | break; | |
62c6ae04 | 3011 | case 0x14: /* DBOFF */ |
2d754a10 GH |
3012 | ret = OFF_DOORBELL; |
3013 | break; | |
62c6ae04 | 3014 | case 0x18: /* RTSOFF */ |
2d754a10 GH |
3015 | ret = OFF_RUNTIME; |
3016 | break; | |
62c6ae04 HM |
3017 | |
3018 | /* extended capabilities */ | |
3019 | case 0x20: /* Supported Protocol:00 */ | |
2d754a10 GH |
3020 | ret = 0x02000402; /* USB 2.0 */ |
3021 | break; | |
62c6ae04 | 3022 | case 0x24: /* Supported Protocol:04 */ |
0ebfb144 | 3023 | ret = 0x20425355; /* "USB " */ |
2d754a10 | 3024 | break; |
62c6ae04 | 3025 | case 0x28: /* Supported Protocol:08 */ |
7bafd888 GH |
3026 | if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) { |
3027 | ret = (xhci->numports_2<<8) | (xhci->numports_3+1); | |
3028 | } else { | |
3029 | ret = (xhci->numports_2<<8) | 1; | |
3030 | } | |
2d754a10 | 3031 | break; |
62c6ae04 | 3032 | case 0x2c: /* Supported Protocol:0c */ |
2d754a10 GH |
3033 | ret = 0x00000000; /* reserved */ |
3034 | break; | |
62c6ae04 | 3035 | case 0x30: /* Supported Protocol:00 */ |
2d754a10 GH |
3036 | ret = 0x03000002; /* USB 3.0 */ |
3037 | break; | |
62c6ae04 | 3038 | case 0x34: /* Supported Protocol:04 */ |
0ebfb144 | 3039 | ret = 0x20425355; /* "USB " */ |
2d754a10 | 3040 | break; |
62c6ae04 | 3041 | case 0x38: /* Supported Protocol:08 */ |
7bafd888 GH |
3042 | if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) { |
3043 | ret = (xhci->numports_3<<8) | 1; | |
3044 | } else { | |
3045 | ret = (xhci->numports_3<<8) | (xhci->numports_2+1); | |
3046 | } | |
2d754a10 | 3047 | break; |
62c6ae04 | 3048 | case 0x3c: /* Supported Protocol:0c */ |
2d754a10 GH |
3049 | ret = 0x00000000; /* reserved */ |
3050 | break; | |
62c6ae04 | 3051 | default: |
0ab966cf | 3052 | trace_usb_xhci_unimplemented("cap read", reg); |
2d754a10 | 3053 | ret = 0; |
62c6ae04 | 3054 | } |
2d754a10 GH |
3055 | |
3056 | trace_usb_xhci_cap_read(reg, ret); | |
3057 | return ret; | |
62c6ae04 HM |
3058 | } |
3059 | ||
a8170e5e | 3060 | static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size) |
62c6ae04 | 3061 | { |
1d8a4e69 | 3062 | XHCIPort *port = ptr; |
2d754a10 GH |
3063 | uint32_t ret; |
3064 | ||
1d8a4e69 | 3065 | switch (reg) { |
62c6ae04 | 3066 | case 0x00: /* PORTSC */ |
1d8a4e69 | 3067 | ret = port->portsc; |
2d754a10 | 3068 | break; |
62c6ae04 HM |
3069 | case 0x04: /* PORTPMSC */ |
3070 | case 0x08: /* PORTLI */ | |
2d754a10 GH |
3071 | ret = 0; |
3072 | break; | |
62c6ae04 HM |
3073 | case 0x0c: /* reserved */ |
3074 | default: | |
0ab966cf | 3075 | trace_usb_xhci_unimplemented("port read", reg); |
2d754a10 | 3076 | ret = 0; |
62c6ae04 | 3077 | } |
2d754a10 | 3078 | |
1d8a4e69 | 3079 | trace_usb_xhci_port_read(port->portnr, reg, ret); |
2d754a10 | 3080 | return ret; |
62c6ae04 HM |
3081 | } |
3082 | ||
a8170e5e | 3083 | static void xhci_port_write(void *ptr, hwaddr reg, |
1d8a4e69 | 3084 | uint64_t val, unsigned size) |
62c6ae04 | 3085 | { |
1d8a4e69 | 3086 | XHCIPort *port = ptr; |
bdfce20d | 3087 | uint32_t portsc, notify; |
62c6ae04 | 3088 | |
1d8a4e69 | 3089 | trace_usb_xhci_port_write(port->portnr, reg, val); |
2d754a10 | 3090 | |
1d8a4e69 | 3091 | switch (reg) { |
62c6ae04 | 3092 | case 0x00: /* PORTSC */ |
bdfce20d | 3093 | /* write-1-to-start bits */ |
dad5b9ea GH |
3094 | if (val & PORTSC_WPR) { |
3095 | xhci_port_reset(port, true); | |
3096 | break; | |
3097 | } | |
bdfce20d | 3098 | if (val & PORTSC_PR) { |
dad5b9ea | 3099 | xhci_port_reset(port, false); |
bdfce20d GH |
3100 | break; |
3101 | } | |
3102 | ||
1d8a4e69 | 3103 | portsc = port->portsc; |
bdfce20d | 3104 | notify = 0; |
62c6ae04 HM |
3105 | /* write-1-to-clear bits*/ |
3106 | portsc &= ~(val & (PORTSC_CSC|PORTSC_PEC|PORTSC_WRC|PORTSC_OCC| | |
3107 | PORTSC_PRC|PORTSC_PLC|PORTSC_CEC)); | |
3108 | if (val & PORTSC_LWS) { | |
3109 | /* overwrite PLS only when LWS=1 */ | |
bdfce20d GH |
3110 | uint32_t old_pls = get_field(port->portsc, PORTSC_PLS); |
3111 | uint32_t new_pls = get_field(val, PORTSC_PLS); | |
3112 | switch (new_pls) { | |
3113 | case PLS_U0: | |
3114 | if (old_pls != PLS_U0) { | |
3115 | set_field(&portsc, new_pls, PORTSC_PLS); | |
3116 | trace_usb_xhci_port_link(port->portnr, new_pls); | |
3117 | notify = PORTSC_PLC; | |
3118 | } | |
3119 | break; | |
3120 | case PLS_U3: | |
3121 | if (old_pls < PLS_U3) { | |
3122 | set_field(&portsc, new_pls, PORTSC_PLS); | |
3123 | trace_usb_xhci_port_link(port->portnr, new_pls); | |
3124 | } | |
3125 | break; | |
3126 | case PLS_RESUME: | |
3127 | /* windows does this for some reason, don't spam stderr */ | |
3128 | break; | |
3129 | default: | |
d6bb65fc | 3130 | DPRINTF("%s: ignore pls write (old %d, new %d)\n", |
bdfce20d GH |
3131 | __func__, old_pls, new_pls); |
3132 | break; | |
3133 | } | |
62c6ae04 HM |
3134 | } |
3135 | /* read/write bits */ | |
3136 | portsc &= ~(PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE); | |
3137 | portsc |= (val & (PORTSC_PP|PORTSC_WCE|PORTSC_WDE|PORTSC_WOE)); | |
40030130 | 3138 | port->portsc = portsc; |
bdfce20d GH |
3139 | if (notify) { |
3140 | xhci_port_notify(port, notify); | |
62c6ae04 | 3141 | } |
62c6ae04 HM |
3142 | break; |
3143 | case 0x04: /* PORTPMSC */ | |
3144 | case 0x08: /* PORTLI */ | |
3145 | default: | |
0ab966cf | 3146 | trace_usb_xhci_unimplemented("port write", reg); |
62c6ae04 HM |
3147 | } |
3148 | } | |
3149 | ||
a8170e5e | 3150 | static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size) |
62c6ae04 | 3151 | { |
1b067564 | 3152 | XHCIState *xhci = ptr; |
2d754a10 | 3153 | uint32_t ret; |
62c6ae04 | 3154 | |
62c6ae04 HM |
3155 | switch (reg) { |
3156 | case 0x00: /* USBCMD */ | |
2d754a10 GH |
3157 | ret = xhci->usbcmd; |
3158 | break; | |
62c6ae04 | 3159 | case 0x04: /* USBSTS */ |
2d754a10 GH |
3160 | ret = xhci->usbsts; |
3161 | break; | |
62c6ae04 | 3162 | case 0x08: /* PAGESIZE */ |
2d754a10 GH |
3163 | ret = 1; /* 4KiB */ |
3164 | break; | |
62c6ae04 | 3165 | case 0x14: /* DNCTRL */ |
2d754a10 GH |
3166 | ret = xhci->dnctrl; |
3167 | break; | |
62c6ae04 | 3168 | case 0x18: /* CRCR low */ |
2d754a10 GH |
3169 | ret = xhci->crcr_low & ~0xe; |
3170 | break; | |
62c6ae04 | 3171 | case 0x1c: /* CRCR high */ |
2d754a10 GH |
3172 | ret = xhci->crcr_high; |
3173 | break; | |
62c6ae04 | 3174 | case 0x30: /* DCBAAP low */ |
2d754a10 GH |
3175 | ret = xhci->dcbaap_low; |
3176 | break; | |
62c6ae04 | 3177 | case 0x34: /* DCBAAP high */ |
2d754a10 GH |
3178 | ret = xhci->dcbaap_high; |
3179 | break; | |
62c6ae04 | 3180 | case 0x38: /* CONFIG */ |
2d754a10 GH |
3181 | ret = xhci->config; |
3182 | break; | |
62c6ae04 | 3183 | default: |
0ab966cf | 3184 | trace_usb_xhci_unimplemented("oper read", reg); |
2d754a10 | 3185 | ret = 0; |
62c6ae04 | 3186 | } |
2d754a10 GH |
3187 | |
3188 | trace_usb_xhci_oper_read(reg, ret); | |
3189 | return ret; | |
62c6ae04 HM |
3190 | } |
3191 | ||
a8170e5e | 3192 | static void xhci_oper_write(void *ptr, hwaddr reg, |
1b067564 | 3193 | uint64_t val, unsigned size) |
62c6ae04 | 3194 | { |
1b067564 | 3195 | XHCIState *xhci = ptr; |
37034575 | 3196 | DeviceState *d = DEVICE(ptr); |
1b067564 | 3197 | |
2d754a10 GH |
3198 | trace_usb_xhci_oper_write(reg, val); |
3199 | ||
62c6ae04 HM |
3200 | switch (reg) { |
3201 | case 0x00: /* USBCMD */ | |
3202 | if ((val & USBCMD_RS) && !(xhci->usbcmd & USBCMD_RS)) { | |
3203 | xhci_run(xhci); | |
3204 | } else if (!(val & USBCMD_RS) && (xhci->usbcmd & USBCMD_RS)) { | |
3205 | xhci_stop(xhci); | |
3206 | } | |
f1f8bc21 GH |
3207 | if (val & USBCMD_CSS) { |
3208 | /* save state */ | |
3209 | xhci->usbsts &= ~USBSTS_SRE; | |
3210 | } | |
3211 | if (val & USBCMD_CRS) { | |
3212 | /* restore state */ | |
3213 | xhci->usbsts |= USBSTS_SRE; | |
3214 | } | |
62c6ae04 | 3215 | xhci->usbcmd = val & 0xc0f; |
01546fa6 | 3216 | xhci_mfwrap_update(xhci); |
62c6ae04 | 3217 | if (val & USBCMD_HCRST) { |
37034575 | 3218 | xhci_reset(d); |
62c6ae04 | 3219 | } |
4c4abe7c | 3220 | xhci_intx_update(xhci); |
62c6ae04 HM |
3221 | break; |
3222 | ||
3223 | case 0x04: /* USBSTS */ | |
3224 | /* these bits are write-1-to-clear */ | |
3225 | xhci->usbsts &= ~(val & (USBSTS_HSE|USBSTS_EINT|USBSTS_PCD|USBSTS_SRE)); | |
4c4abe7c | 3226 | xhci_intx_update(xhci); |
62c6ae04 HM |
3227 | break; |
3228 | ||
3229 | case 0x14: /* DNCTRL */ | |
3230 | xhci->dnctrl = val & 0xffff; | |
3231 | break; | |
3232 | case 0x18: /* CRCR low */ | |
3233 | xhci->crcr_low = (val & 0xffffffcf) | (xhci->crcr_low & CRCR_CRR); | |
3234 | break; | |
3235 | case 0x1c: /* CRCR high */ | |
3236 | xhci->crcr_high = val; | |
3237 | if (xhci->crcr_low & (CRCR_CA|CRCR_CS) && (xhci->crcr_low & CRCR_CRR)) { | |
3238 | XHCIEvent event = {ER_COMMAND_COMPLETE, CC_COMMAND_RING_STOPPED}; | |
3239 | xhci->crcr_low &= ~CRCR_CRR; | |
2d1de850 | 3240 | xhci_event(xhci, &event, 0); |
62c6ae04 HM |
3241 | DPRINTF("xhci: command ring stopped (CRCR=%08x)\n", xhci->crcr_low); |
3242 | } else { | |
59a70ccd | 3243 | dma_addr_t base = xhci_addr64(xhci->crcr_low & ~0x3f, val); |
62c6ae04 HM |
3244 | xhci_ring_init(xhci, &xhci->cmd_ring, base); |
3245 | } | |
3246 | xhci->crcr_low &= ~(CRCR_CA | CRCR_CS); | |
3247 | break; | |
3248 | case 0x30: /* DCBAAP low */ | |
3249 | xhci->dcbaap_low = val & 0xffffffc0; | |
3250 | break; | |
3251 | case 0x34: /* DCBAAP high */ | |
3252 | xhci->dcbaap_high = val; | |
3253 | break; | |
3254 | case 0x38: /* CONFIG */ | |
3255 | xhci->config = val & 0xff; | |
3256 | break; | |
3257 | default: | |
0ab966cf | 3258 | trace_usb_xhci_unimplemented("oper write", reg); |
62c6ae04 HM |
3259 | } |
3260 | } | |
3261 | ||
a8170e5e | 3262 | static uint64_t xhci_runtime_read(void *ptr, hwaddr reg, |
1b067564 | 3263 | unsigned size) |
62c6ae04 | 3264 | { |
1b067564 | 3265 | XHCIState *xhci = ptr; |
43d9d604 | 3266 | uint32_t ret = 0; |
62c6ae04 | 3267 | |
43d9d604 GH |
3268 | if (reg < 0x20) { |
3269 | switch (reg) { | |
3270 | case 0x00: /* MFINDEX */ | |
3271 | ret = xhci_mfindex_get(xhci) & 0x3fff; | |
3272 | break; | |
3273 | default: | |
0ab966cf | 3274 | trace_usb_xhci_unimplemented("runtime read", reg); |
43d9d604 GH |
3275 | break; |
3276 | } | |
3277 | } else { | |
3278 | int v = (reg - 0x20) / 0x20; | |
3279 | XHCIInterrupter *intr = &xhci->intr[v]; | |
3280 | switch (reg & 0x1f) { | |
3281 | case 0x00: /* IMAN */ | |
3282 | ret = intr->iman; | |
3283 | break; | |
3284 | case 0x04: /* IMOD */ | |
3285 | ret = intr->imod; | |
3286 | break; | |
3287 | case 0x08: /* ERSTSZ */ | |
3288 | ret = intr->erstsz; | |
3289 | break; | |
3290 | case 0x10: /* ERSTBA low */ | |
3291 | ret = intr->erstba_low; | |
3292 | break; | |
3293 | case 0x14: /* ERSTBA high */ | |
3294 | ret = intr->erstba_high; | |
3295 | break; | |
3296 | case 0x18: /* ERDP low */ | |
3297 | ret = intr->erdp_low; | |
3298 | break; | |
3299 | case 0x1c: /* ERDP high */ | |
3300 | ret = intr->erdp_high; | |
3301 | break; | |
3302 | } | |
62c6ae04 | 3303 | } |
2d754a10 GH |
3304 | |
3305 | trace_usb_xhci_runtime_read(reg, ret); | |
3306 | return ret; | |
62c6ae04 HM |
3307 | } |
3308 | ||
a8170e5e | 3309 | static void xhci_runtime_write(void *ptr, hwaddr reg, |
1b067564 | 3310 | uint64_t val, unsigned size) |
62c6ae04 | 3311 | { |
1b067564 | 3312 | XHCIState *xhci = ptr; |
43d9d604 GH |
3313 | int v = (reg - 0x20) / 0x20; |
3314 | XHCIInterrupter *intr = &xhci->intr[v]; | |
8e9f18b6 | 3315 | trace_usb_xhci_runtime_write(reg, val); |
62c6ae04 | 3316 | |
43d9d604 | 3317 | if (reg < 0x20) { |
0ab966cf | 3318 | trace_usb_xhci_unimplemented("runtime write", reg); |
43d9d604 GH |
3319 | return; |
3320 | } | |
3321 | ||
3322 | switch (reg & 0x1f) { | |
3323 | case 0x00: /* IMAN */ | |
62c6ae04 | 3324 | if (val & IMAN_IP) { |
962d11e1 | 3325 | intr->iman &= ~IMAN_IP; |
62c6ae04 | 3326 | } |
962d11e1 GH |
3327 | intr->iman &= ~IMAN_IE; |
3328 | intr->iman |= val & IMAN_IE; | |
43d9d604 GH |
3329 | if (v == 0) { |
3330 | xhci_intx_update(xhci); | |
3331 | } | |
3332 | xhci_msix_update(xhci, v); | |
62c6ae04 | 3333 | break; |
43d9d604 | 3334 | case 0x04: /* IMOD */ |
962d11e1 | 3335 | intr->imod = val; |
62c6ae04 | 3336 | break; |
43d9d604 | 3337 | case 0x08: /* ERSTSZ */ |
962d11e1 | 3338 | intr->erstsz = val & 0xffff; |
62c6ae04 | 3339 | break; |
43d9d604 | 3340 | case 0x10: /* ERSTBA low */ |
62c6ae04 | 3341 | /* XXX NEC driver bug: it doesn't align this to 64 bytes |
962d11e1 GH |
3342 | intr->erstba_low = val & 0xffffffc0; */ |
3343 | intr->erstba_low = val & 0xfffffff0; | |
62c6ae04 | 3344 | break; |
43d9d604 | 3345 | case 0x14: /* ERSTBA high */ |
962d11e1 | 3346 | intr->erstba_high = val; |
43d9d604 | 3347 | xhci_er_reset(xhci, v); |
62c6ae04 | 3348 | break; |
43d9d604 | 3349 | case 0x18: /* ERDP low */ |
62c6ae04 | 3350 | if (val & ERDP_EHB) { |
962d11e1 | 3351 | intr->erdp_low &= ~ERDP_EHB; |
62c6ae04 | 3352 | } |
962d11e1 | 3353 | intr->erdp_low = (val & ~ERDP_EHB) | (intr->erdp_low & ERDP_EHB); |
62c6ae04 | 3354 | break; |
43d9d604 | 3355 | case 0x1c: /* ERDP high */ |
962d11e1 | 3356 | intr->erdp_high = val; |
43d9d604 | 3357 | xhci_events_update(xhci, v); |
62c6ae04 HM |
3358 | break; |
3359 | default: | |
0ab966cf | 3360 | trace_usb_xhci_unimplemented("oper write", reg); |
62c6ae04 HM |
3361 | } |
3362 | } | |
3363 | ||
a8170e5e | 3364 | static uint64_t xhci_doorbell_read(void *ptr, hwaddr reg, |
1b067564 | 3365 | unsigned size) |
62c6ae04 | 3366 | { |
62c6ae04 | 3367 | /* doorbells always read as 0 */ |
2d754a10 | 3368 | trace_usb_xhci_doorbell_read(reg, 0); |
62c6ae04 HM |
3369 | return 0; |
3370 | } | |
3371 | ||
a8170e5e | 3372 | static void xhci_doorbell_write(void *ptr, hwaddr reg, |
1b067564 | 3373 | uint64_t val, unsigned size) |
62c6ae04 | 3374 | { |
1b067564 | 3375 | XHCIState *xhci = ptr; |
024426ac | 3376 | unsigned int epid, streamid; |
1b067564 | 3377 | |
2d754a10 | 3378 | trace_usb_xhci_doorbell_write(reg, val); |
62c6ae04 HM |
3379 | |
3380 | if (!xhci_running(xhci)) { | |
d6bb65fc | 3381 | DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n"); |
62c6ae04 HM |
3382 | return; |
3383 | } | |
3384 | ||
3385 | reg >>= 2; | |
3386 | ||
3387 | if (reg == 0) { | |
3388 | if (val == 0) { | |
3389 | xhci_process_commands(xhci); | |
3390 | } else { | |
d6bb65fc | 3391 | DPRINTF("xhci: bad doorbell 0 write: 0x%x\n", |
1b067564 | 3392 | (uint32_t)val); |
62c6ae04 HM |
3393 | } |
3394 | } else { | |
024426ac GH |
3395 | epid = val & 0xff; |
3396 | streamid = (val >> 16) & 0xffff; | |
91062ae0 | 3397 | if (reg > xhci->numslots) { |
d6bb65fc | 3398 | DPRINTF("xhci: bad doorbell %d\n", (int)reg); |
024426ac | 3399 | } else if (epid > 31) { |
d6bb65fc | 3400 | DPRINTF("xhci: bad doorbell %d write: 0x%x\n", |
1b067564 | 3401 | (int)reg, (uint32_t)val); |
62c6ae04 | 3402 | } else { |
024426ac | 3403 | xhci_kick_ep(xhci, reg, epid, streamid); |
62c6ae04 HM |
3404 | } |
3405 | } | |
3406 | } | |
3407 | ||
6d3bc22e GH |
3408 | static void xhci_cap_write(void *opaque, hwaddr addr, uint64_t val, |
3409 | unsigned width) | |
3410 | { | |
3411 | /* nothing */ | |
3412 | } | |
3413 | ||
1b067564 GH |
3414 | static const MemoryRegionOps xhci_cap_ops = { |
3415 | .read = xhci_cap_read, | |
6d3bc22e | 3416 | .write = xhci_cap_write, |
6ee021d4 | 3417 | .valid.min_access_size = 1, |
1b067564 | 3418 | .valid.max_access_size = 4, |
6ee021d4 GH |
3419 | .impl.min_access_size = 4, |
3420 | .impl.max_access_size = 4, | |
1b067564 GH |
3421 | .endianness = DEVICE_LITTLE_ENDIAN, |
3422 | }; | |
62c6ae04 | 3423 | |
1b067564 GH |
3424 | static const MemoryRegionOps xhci_oper_ops = { |
3425 | .read = xhci_oper_read, | |
3426 | .write = xhci_oper_write, | |
3427 | .valid.min_access_size = 4, | |
3428 | .valid.max_access_size = 4, | |
3429 | .endianness = DEVICE_LITTLE_ENDIAN, | |
3430 | }; | |
62c6ae04 | 3431 | |
1d8a4e69 GH |
3432 | static const MemoryRegionOps xhci_port_ops = { |
3433 | .read = xhci_port_read, | |
3434 | .write = xhci_port_write, | |
3435 | .valid.min_access_size = 4, | |
3436 | .valid.max_access_size = 4, | |
3437 | .endianness = DEVICE_LITTLE_ENDIAN, | |
3438 | }; | |
3439 | ||
1b067564 GH |
3440 | static const MemoryRegionOps xhci_runtime_ops = { |
3441 | .read = xhci_runtime_read, | |
3442 | .write = xhci_runtime_write, | |
3443 | .valid.min_access_size = 4, | |
3444 | .valid.max_access_size = 4, | |
3445 | .endianness = DEVICE_LITTLE_ENDIAN, | |
3446 | }; | |
62c6ae04 | 3447 | |
1b067564 GH |
3448 | static const MemoryRegionOps xhci_doorbell_ops = { |
3449 | .read = xhci_doorbell_read, | |
3450 | .write = xhci_doorbell_write, | |
62c6ae04 HM |
3451 | .valid.min_access_size = 4, |
3452 | .valid.max_access_size = 4, | |
3453 | .endianness = DEVICE_LITTLE_ENDIAN, | |
3454 | }; | |
3455 | ||
3456 | static void xhci_attach(USBPort *usbport) | |
3457 | { | |
3458 | XHCIState *xhci = usbport->opaque; | |
0846e635 | 3459 | XHCIPort *port = xhci_lookup_port(xhci, usbport); |
62c6ae04 | 3460 | |
f3214027 | 3461 | xhci_port_update(port, 0); |
62c6ae04 HM |
3462 | } |
3463 | ||
3464 | static void xhci_detach(USBPort *usbport) | |
3465 | { | |
3466 | XHCIState *xhci = usbport->opaque; | |
0846e635 | 3467 | XHCIPort *port = xhci_lookup_port(xhci, usbport); |
62c6ae04 | 3468 | |
f3dcf638 | 3469 | xhci_detach_slot(xhci, usbport); |
f3214027 | 3470 | xhci_port_update(port, 1); |
62c6ae04 HM |
3471 | } |
3472 | ||
8c735e43 GH |
3473 | static void xhci_wakeup(USBPort *usbport) |
3474 | { | |
3475 | XHCIState *xhci = usbport->opaque; | |
0846e635 | 3476 | XHCIPort *port = xhci_lookup_port(xhci, usbport); |
8c735e43 | 3477 | |
85e05d82 | 3478 | if (get_field(port->portsc, PORTSC_PLS) != PLS_U3) { |
8c735e43 GH |
3479 | return; |
3480 | } | |
85e05d82 | 3481 | set_field(&port->portsc, PLS_RESUME, PORTSC_PLS); |
f705a362 | 3482 | xhci_port_notify(port, PORTSC_PLC); |
8c735e43 GH |
3483 | } |
3484 | ||
62c6ae04 HM |
3485 | static void xhci_complete(USBPort *port, USBPacket *packet) |
3486 | { | |
3487 | XHCITransfer *xfer = container_of(packet, XHCITransfer, packet); | |
3488 | ||
9a77a0f5 | 3489 | if (packet->status == USB_RET_REMOVE_FROM_QUEUE) { |
582d6f4a | 3490 | xhci_ep_nuke_one_xfer(xfer, 0); |
0cae7b1a HG |
3491 | return; |
3492 | } | |
9a77a0f5 | 3493 | xhci_complete_packet(xfer); |
3a533ee8 | 3494 | xhci_kick_epctx(xfer->epctx, xfer->streamid); |
94b037f2 GH |
3495 | if (xfer->complete) { |
3496 | xhci_ep_free_xfer(xfer); | |
3497 | } | |
62c6ae04 HM |
3498 | } |
3499 | ||
ccaf87a0 | 3500 | static void xhci_child_detach(USBPort *uport, USBDevice *child) |
62c6ae04 | 3501 | { |
ccaf87a0 GH |
3502 | USBBus *bus = usb_bus_from_device(child); |
3503 | XHCIState *xhci = container_of(bus, XHCIState, bus); | |
ccaf87a0 | 3504 | |
463c534d | 3505 | xhci_detach_slot(xhci, child->port); |
62c6ae04 HM |
3506 | } |
3507 | ||
1d8a4e69 | 3508 | static USBPortOps xhci_uport_ops = { |
62c6ae04 HM |
3509 | .attach = xhci_attach, |
3510 | .detach = xhci_detach, | |
8c735e43 | 3511 | .wakeup = xhci_wakeup, |
62c6ae04 HM |
3512 | .complete = xhci_complete, |
3513 | .child_detach = xhci_child_detach, | |
3514 | }; | |
3515 | ||
7c605a23 GH |
3516 | static int xhci_find_epid(USBEndpoint *ep) |
3517 | { | |
3518 | if (ep->nr == 0) { | |
3519 | return 1; | |
3520 | } | |
3521 | if (ep->pid == USB_TOKEN_IN) { | |
3522 | return ep->nr * 2 + 1; | |
3523 | } else { | |
3524 | return ep->nr * 2; | |
3525 | } | |
3526 | } | |
3527 | ||
070eeef9 | 3528 | static USBEndpoint *xhci_epid_to_usbep(XHCIEPContext *epctx) |
518ad5f2 | 3529 | { |
070eeef9 GH |
3530 | USBPort *uport; |
3531 | uint32_t token; | |
518ad5f2 | 3532 | |
070eeef9 | 3533 | if (!epctx) { |
518ad5f2 HG |
3534 | return NULL; |
3535 | } | |
070eeef9 GH |
3536 | uport = epctx->xhci->slots[epctx->slotid - 1].uport; |
3537 | token = (epctx->epid & 1) ? USB_TOKEN_IN : USB_TOKEN_OUT; | |
3538 | if (!uport) { | |
3539 | return NULL; | |
3540 | } | |
3541 | return usb_ep_get(uport->dev, token, epctx->epid >> 1); | |
518ad5f2 HG |
3542 | } |
3543 | ||
8550a02d GH |
3544 | static void xhci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep, |
3545 | unsigned int stream) | |
7c605a23 GH |
3546 | { |
3547 | XHCIState *xhci = container_of(bus, XHCIState, bus); | |
3548 | int slotid; | |
3549 | ||
3550 | DPRINTF("%s\n", __func__); | |
af203be3 | 3551 | slotid = ep->dev->addr; |
7c605a23 GH |
3552 | if (slotid == 0 || !xhci->slots[slotid-1].enabled) { |
3553 | DPRINTF("%s: oops, no slot for dev %d\n", __func__, ep->dev->addr); | |
3554 | return; | |
3555 | } | |
024426ac | 3556 | xhci_kick_ep(xhci, slotid, xhci_find_epid(ep), stream); |
7c605a23 GH |
3557 | } |
3558 | ||
62c6ae04 | 3559 | static USBBusOps xhci_bus_ops = { |
7c605a23 | 3560 | .wakeup_endpoint = xhci_wakeup_endpoint, |
62c6ae04 HM |
3561 | }; |
3562 | ||
37034575 | 3563 | static void usb_xhci_init(XHCIState *xhci) |
62c6ae04 | 3564 | { |
37034575 | 3565 | DeviceState *dev = DEVICE(xhci); |
0846e635 GH |
3566 | XHCIPort *port; |
3567 | int i, usbports, speedmask; | |
62c6ae04 HM |
3568 | |
3569 | xhci->usbsts = USBSTS_HCH; | |
3570 | ||
0846e635 GH |
3571 | if (xhci->numports_2 > MAXPORTS_2) { |
3572 | xhci->numports_2 = MAXPORTS_2; | |
3573 | } | |
3574 | if (xhci->numports_3 > MAXPORTS_3) { | |
3575 | xhci->numports_3 = MAXPORTS_3; | |
3576 | } | |
3577 | usbports = MAX(xhci->numports_2, xhci->numports_3); | |
3578 | xhci->numports = xhci->numports_2 + xhci->numports_3; | |
3579 | ||
c889b3a5 | 3580 | usb_bus_new(&xhci->bus, sizeof(xhci->bus), &xhci_bus_ops, dev); |
62c6ae04 | 3581 | |
0846e635 GH |
3582 | for (i = 0; i < usbports; i++) { |
3583 | speedmask = 0; | |
3584 | if (i < xhci->numports_2) { | |
7bafd888 GH |
3585 | if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) { |
3586 | port = &xhci->ports[i + xhci->numports_3]; | |
3587 | port->portnr = i + 1 + xhci->numports_3; | |
3588 | } else { | |
3589 | port = &xhci->ports[i]; | |
3590 | port->portnr = i + 1; | |
3591 | } | |
0846e635 GH |
3592 | port->uport = &xhci->uports[i]; |
3593 | port->speedmask = | |
3594 | USB_SPEED_MASK_LOW | | |
3595 | USB_SPEED_MASK_FULL | | |
3596 | USB_SPEED_MASK_HIGH; | |
1d8a4e69 | 3597 | snprintf(port->name, sizeof(port->name), "usb2 port #%d", i+1); |
0846e635 GH |
3598 | speedmask |= port->speedmask; |
3599 | } | |
3600 | if (i < xhci->numports_3) { | |
7bafd888 GH |
3601 | if (xhci_get_flag(xhci, XHCI_FLAG_SS_FIRST)) { |
3602 | port = &xhci->ports[i]; | |
3603 | port->portnr = i + 1; | |
3604 | } else { | |
3605 | port = &xhci->ports[i + xhci->numports_2]; | |
3606 | port->portnr = i + 1 + xhci->numports_2; | |
3607 | } | |
0846e635 GH |
3608 | port->uport = &xhci->uports[i]; |
3609 | port->speedmask = USB_SPEED_MASK_SUPER; | |
1d8a4e69 | 3610 | snprintf(port->name, sizeof(port->name), "usb3 port #%d", i+1); |
0846e635 GH |
3611 | speedmask |= port->speedmask; |
3612 | } | |
3613 | usb_register_port(&xhci->bus, &xhci->uports[i], xhci, i, | |
1d8a4e69 | 3614 | &xhci_uport_ops, speedmask); |
62c6ae04 | 3615 | } |
62c6ae04 HM |
3616 | } |
3617 | ||
9af21dbe | 3618 | static void usb_xhci_realize(struct PCIDevice *dev, Error **errp) |
62c6ae04 | 3619 | { |
1d8a4e69 | 3620 | int i, ret; |
1108b2f8 | 3621 | Error *err = NULL; |
62c6ae04 | 3622 | |
37034575 | 3623 | XHCIState *xhci = XHCI(dev); |
62c6ae04 | 3624 | |
9b7d3334 AF |
3625 | dev->config[PCI_CLASS_PROG] = 0x30; /* xHCI */ |
3626 | dev->config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin 1 */ | |
3627 | dev->config[PCI_CACHE_LINE_SIZE] = 0x10; | |
3628 | dev->config[0x60] = 0x30; /* release number */ | |
62c6ae04 | 3629 | |
91062ae0 GH |
3630 | if (xhci->numintrs > MAXINTRS) { |
3631 | xhci->numintrs = MAXINTRS; | |
3632 | } | |
c94a7c69 GH |
3633 | while (xhci->numintrs & (xhci->numintrs - 1)) { /* ! power of 2 */ |
3634 | xhci->numintrs++; | |
3635 | } | |
91062ae0 GH |
3636 | if (xhci->numintrs < 1) { |
3637 | xhci->numintrs = 1; | |
3638 | } | |
3639 | if (xhci->numslots > MAXSLOTS) { | |
3640 | xhci->numslots = MAXSLOTS; | |
3641 | } | |
3642 | if (xhci->numslots < 1) { | |
3643 | xhci->numslots = 1; | |
3644 | } | |
2aa6bfcb GH |
3645 | if (xhci_get_flag(xhci, XHCI_FLAG_ENABLE_STREAMS)) { |
3646 | xhci->max_pstreams_mask = 7; /* == 256 primary streams */ | |
3647 | } else { | |
3648 | xhci->max_pstreams_mask = 0; | |
3649 | } | |
91062ae0 | 3650 | |
20729dbd C |
3651 | if (xhci->msi != ON_OFF_AUTO_OFF) { |
3652 | ret = msi_init(dev, 0x70, xhci->numintrs, true, false, &err); | |
3653 | /* Any error other than -ENOTSUP(board's MSI support is broken) | |
3654 | * is a programming error */ | |
3655 | assert(!ret || ret == -ENOTSUP); | |
3656 | if (ret && xhci->msi == ON_OFF_AUTO_ON) { | |
3657 | /* Can't satisfy user's explicit msi=on request, fail */ | |
3658 | error_append_hint(&err, "You have to use msi=auto (default) or " | |
3659 | "msi=off with this machine type.\n"); | |
3660 | error_propagate(errp, err); | |
3661 | return; | |
3662 | } | |
3663 | assert(!err || xhci->msi == ON_OFF_AUTO_AUTO); | |
3664 | /* With msi=auto, we fall back to MSI off silently */ | |
3665 | error_free(err); | |
3666 | } | |
3667 | ||
3668 | usb_xhci_init(xhci); | |
bc72ad67 | 3669 | xhci->mfwrap_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_mfwrap_timer, xhci); |
01546fa6 | 3670 | |
22fc860b PB |
3671 | memory_region_init(&xhci->mem, OBJECT(xhci), "xhci", LEN_REGS); |
3672 | memory_region_init_io(&xhci->mem_cap, OBJECT(xhci), &xhci_cap_ops, xhci, | |
1b067564 | 3673 | "capabilities", LEN_CAP); |
22fc860b | 3674 | memory_region_init_io(&xhci->mem_oper, OBJECT(xhci), &xhci_oper_ops, xhci, |
1d8a4e69 | 3675 | "operational", 0x400); |
22fc860b | 3676 | memory_region_init_io(&xhci->mem_runtime, OBJECT(xhci), &xhci_runtime_ops, xhci, |
1b067564 | 3677 | "runtime", LEN_RUNTIME); |
22fc860b | 3678 | memory_region_init_io(&xhci->mem_doorbell, OBJECT(xhci), &xhci_doorbell_ops, xhci, |
1b067564 GH |
3679 | "doorbell", LEN_DOORBELL); |
3680 | ||
3681 | memory_region_add_subregion(&xhci->mem, 0, &xhci->mem_cap); | |
3682 | memory_region_add_subregion(&xhci->mem, OFF_OPER, &xhci->mem_oper); | |
3683 | memory_region_add_subregion(&xhci->mem, OFF_RUNTIME, &xhci->mem_runtime); | |
3684 | memory_region_add_subregion(&xhci->mem, OFF_DOORBELL, &xhci->mem_doorbell); | |
3685 | ||
1d8a4e69 GH |
3686 | for (i = 0; i < xhci->numports; i++) { |
3687 | XHCIPort *port = &xhci->ports[i]; | |
3688 | uint32_t offset = OFF_OPER + 0x400 + 0x10 * i; | |
3689 | port->xhci = xhci; | |
22fc860b | 3690 | memory_region_init_io(&port->mem, OBJECT(xhci), &xhci_port_ops, port, |
1d8a4e69 GH |
3691 | port->name, 0x10); |
3692 | memory_region_add_subregion(&xhci->mem, offset, &port->mem); | |
3693 | } | |
3694 | ||
9b7d3334 | 3695 | pci_register_bar(dev, 0, |
62c6ae04 HM |
3696 | PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64, |
3697 | &xhci->mem); | |
3698 | ||
e6043e92 DDAG |
3699 | if (pci_bus_is_express(dev->bus) || |
3700 | xhci_get_flag(xhci, XHCI_FLAG_FORCE_PCIE_ENDCAP)) { | |
058fdcf5 GH |
3701 | ret = pcie_endpoint_cap_init(dev, 0xa0); |
3702 | assert(ret >= 0); | |
3703 | } | |
62c6ae04 | 3704 | |
290fd20d | 3705 | if (xhci->msix != ON_OFF_AUTO_OFF) { |
ee640c62 | 3706 | /* TODO check for errors, and should fail when msix=on */ |
9b7d3334 | 3707 | msix_init(dev, xhci->numintrs, |
4c47f800 GH |
3708 | &xhci->mem, 0, OFF_MSIX_TABLE, |
3709 | &xhci->mem, 0, OFF_MSIX_PBA, | |
ee640c62 | 3710 | 0x90, NULL); |
4c47f800 | 3711 | } |
62c6ae04 HM |
3712 | } |
3713 | ||
53c30545 GA |
3714 | static void usb_xhci_exit(PCIDevice *dev) |
3715 | { | |
3716 | int i; | |
3717 | XHCIState *xhci = XHCI(dev); | |
3718 | ||
d733f74c GA |
3719 | trace_usb_xhci_exit(); |
3720 | ||
53c30545 GA |
3721 | for (i = 0; i < xhci->numslots; i++) { |
3722 | xhci_disable_slot(xhci, i + 1); | |
3723 | } | |
3724 | ||
3725 | if (xhci->mfwrap_timer) { | |
3726 | timer_del(xhci->mfwrap_timer); | |
3727 | timer_free(xhci->mfwrap_timer); | |
3728 | xhci->mfwrap_timer = NULL; | |
3729 | } | |
3730 | ||
3731 | memory_region_del_subregion(&xhci->mem, &xhci->mem_cap); | |
3732 | memory_region_del_subregion(&xhci->mem, &xhci->mem_oper); | |
3733 | memory_region_del_subregion(&xhci->mem, &xhci->mem_runtime); | |
3734 | memory_region_del_subregion(&xhci->mem, &xhci->mem_doorbell); | |
3735 | ||
3736 | for (i = 0; i < xhci->numports; i++) { | |
3737 | XHCIPort *port = &xhci->ports[i]; | |
3738 | memory_region_del_subregion(&xhci->mem, &port->mem); | |
3739 | } | |
3740 | ||
3741 | /* destroy msix memory region */ | |
3742 | if (dev->msix_table && dev->msix_pba | |
3743 | && dev->msix_entry_used) { | |
b53dd449 | 3744 | msix_uninit(dev, &xhci->mem, &xhci->mem); |
53c30545 GA |
3745 | } |
3746 | ||
3747 | usb_bus_release(&xhci->bus); | |
3748 | } | |
3749 | ||
37352df3 GH |
3750 | static int usb_xhci_post_load(void *opaque, int version_id) |
3751 | { | |
3752 | XHCIState *xhci = opaque; | |
9b7d3334 | 3753 | PCIDevice *pci_dev = PCI_DEVICE(xhci); |
37352df3 GH |
3754 | XHCISlot *slot; |
3755 | XHCIEPContext *epctx; | |
3756 | dma_addr_t dcbaap, pctx; | |
3757 | uint32_t slot_ctx[4]; | |
3758 | uint32_t ep_ctx[5]; | |
3759 | int slotid, epid, state, intr; | |
3760 | ||
3761 | dcbaap = xhci_addr64(xhci->dcbaap_low, xhci->dcbaap_high); | |
3762 | ||
3763 | for (slotid = 1; slotid <= xhci->numslots; slotid++) { | |
3764 | slot = &xhci->slots[slotid-1]; | |
3765 | if (!slot->addressed) { | |
3766 | continue; | |
3767 | } | |
3768 | slot->ctx = | |
9b7d3334 | 3769 | xhci_mask64(ldq_le_pci_dma(pci_dev, dcbaap + 8 * slotid)); |
37352df3 GH |
3770 | xhci_dma_read_u32s(xhci, slot->ctx, slot_ctx, sizeof(slot_ctx)); |
3771 | slot->uport = xhci_lookup_uport(xhci, slot_ctx); | |
f2ad97ff GH |
3772 | if (!slot->uport) { |
3773 | /* should not happen, but may trigger on guest bugs */ | |
3774 | slot->enabled = 0; | |
3775 | slot->addressed = 0; | |
3776 | continue; | |
3777 | } | |
37352df3 GH |
3778 | assert(slot->uport && slot->uport->dev); |
3779 | ||
f6969b9f | 3780 | for (epid = 1; epid <= 31; epid++) { |
37352df3 GH |
3781 | pctx = slot->ctx + 32 * epid; |
3782 | xhci_dma_read_u32s(xhci, pctx, ep_ctx, sizeof(ep_ctx)); | |
3783 | state = ep_ctx[0] & EP_STATE_MASK; | |
3784 | if (state == EP_DISABLED) { | |
3785 | continue; | |
3786 | } | |
3787 | epctx = xhci_alloc_epctx(xhci, slotid, epid); | |
3788 | slot->eps[epid-1] = epctx; | |
3789 | xhci_init_epctx(epctx, pctx, ep_ctx); | |
3790 | epctx->state = state; | |
3791 | if (state == EP_RUNNING) { | |
3792 | /* kick endpoint after vmload is finished */ | |
bc72ad67 | 3793 | timer_mod(epctx->kick_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); |
37352df3 GH |
3794 | } |
3795 | } | |
3796 | } | |
3797 | ||
3798 | for (intr = 0; intr < xhci->numintrs; intr++) { | |
3799 | if (xhci->intr[intr].msix_used) { | |
9b7d3334 | 3800 | msix_vector_use(pci_dev, intr); |
37352df3 | 3801 | } else { |
9b7d3334 | 3802 | msix_vector_unuse(pci_dev, intr); |
37352df3 GH |
3803 | } |
3804 | } | |
3805 | ||
3806 | return 0; | |
3807 | } | |
3808 | ||
3809 | static const VMStateDescription vmstate_xhci_ring = { | |
3810 | .name = "xhci-ring", | |
3811 | .version_id = 1, | |
3812 | .fields = (VMStateField[]) { | |
3813 | VMSTATE_UINT64(dequeue, XHCIRing), | |
3814 | VMSTATE_BOOL(ccs, XHCIRing), | |
3815 | VMSTATE_END_OF_LIST() | |
3816 | } | |
3817 | }; | |
3818 | ||
3819 | static const VMStateDescription vmstate_xhci_port = { | |
3820 | .name = "xhci-port", | |
3821 | .version_id = 1, | |
3822 | .fields = (VMStateField[]) { | |
3823 | VMSTATE_UINT32(portsc, XHCIPort), | |
3824 | VMSTATE_END_OF_LIST() | |
3825 | } | |
3826 | }; | |
3827 | ||
3828 | static const VMStateDescription vmstate_xhci_slot = { | |
3829 | .name = "xhci-slot", | |
3830 | .version_id = 1, | |
3831 | .fields = (VMStateField[]) { | |
3832 | VMSTATE_BOOL(enabled, XHCISlot), | |
3833 | VMSTATE_BOOL(addressed, XHCISlot), | |
3834 | VMSTATE_END_OF_LIST() | |
3835 | } | |
3836 | }; | |
3837 | ||
3838 | static const VMStateDescription vmstate_xhci_event = { | |
3839 | .name = "xhci-event", | |
3840 | .version_id = 1, | |
3841 | .fields = (VMStateField[]) { | |
3842 | VMSTATE_UINT32(type, XHCIEvent), | |
3843 | VMSTATE_UINT32(ccode, XHCIEvent), | |
3844 | VMSTATE_UINT64(ptr, XHCIEvent), | |
3845 | VMSTATE_UINT32(length, XHCIEvent), | |
3846 | VMSTATE_UINT32(flags, XHCIEvent), | |
3847 | VMSTATE_UINT8(slotid, XHCIEvent), | |
3848 | VMSTATE_UINT8(epid, XHCIEvent), | |
3afca1d6 | 3849 | VMSTATE_END_OF_LIST() |
37352df3 GH |
3850 | } |
3851 | }; | |
3852 | ||
3853 | static bool xhci_er_full(void *opaque, int version_id) | |
3854 | { | |
3855 | struct XHCIInterrupter *intr = opaque; | |
3856 | return intr->er_full; | |
3857 | } | |
3858 | ||
3859 | static const VMStateDescription vmstate_xhci_intr = { | |
3860 | .name = "xhci-intr", | |
3861 | .version_id = 1, | |
3862 | .fields = (VMStateField[]) { | |
3863 | /* registers */ | |
3864 | VMSTATE_UINT32(iman, XHCIInterrupter), | |
3865 | VMSTATE_UINT32(imod, XHCIInterrupter), | |
3866 | VMSTATE_UINT32(erstsz, XHCIInterrupter), | |
3867 | VMSTATE_UINT32(erstba_low, XHCIInterrupter), | |
3868 | VMSTATE_UINT32(erstba_high, XHCIInterrupter), | |
3869 | VMSTATE_UINT32(erdp_low, XHCIInterrupter), | |
3870 | VMSTATE_UINT32(erdp_high, XHCIInterrupter), | |
3871 | ||
3872 | /* state */ | |
3873 | VMSTATE_BOOL(msix_used, XHCIInterrupter), | |
3874 | VMSTATE_BOOL(er_pcs, XHCIInterrupter), | |
3875 | VMSTATE_UINT64(er_start, XHCIInterrupter), | |
3876 | VMSTATE_UINT32(er_size, XHCIInterrupter), | |
3877 | VMSTATE_UINT32(er_ep_idx, XHCIInterrupter), | |
3878 | ||
3879 | /* event queue (used if ring is full) */ | |
3880 | VMSTATE_BOOL(er_full, XHCIInterrupter), | |
3881 | VMSTATE_UINT32_TEST(ev_buffer_put, XHCIInterrupter, xhci_er_full), | |
3882 | VMSTATE_UINT32_TEST(ev_buffer_get, XHCIInterrupter, xhci_er_full), | |
3883 | VMSTATE_STRUCT_ARRAY_TEST(ev_buffer, XHCIInterrupter, EV_QUEUE, | |
3884 | xhci_er_full, 1, | |
3885 | vmstate_xhci_event, XHCIEvent), | |
3886 | ||
3887 | VMSTATE_END_OF_LIST() | |
3888 | } | |
3889 | }; | |
3890 | ||
62c6ae04 HM |
3891 | static const VMStateDescription vmstate_xhci = { |
3892 | .name = "xhci", | |
37352df3 GH |
3893 | .version_id = 1, |
3894 | .post_load = usb_xhci_post_load, | |
3895 | .fields = (VMStateField[]) { | |
20daa90a | 3896 | VMSTATE_PCI_DEVICE(parent_obj, XHCIState), |
9b7d3334 | 3897 | VMSTATE_MSIX(parent_obj, XHCIState), |
37352df3 GH |
3898 | |
3899 | VMSTATE_STRUCT_VARRAY_UINT32(ports, XHCIState, numports, 1, | |
3900 | vmstate_xhci_port, XHCIPort), | |
3901 | VMSTATE_STRUCT_VARRAY_UINT32(slots, XHCIState, numslots, 1, | |
3902 | vmstate_xhci_slot, XHCISlot), | |
3903 | VMSTATE_STRUCT_VARRAY_UINT32(intr, XHCIState, numintrs, 1, | |
3904 | vmstate_xhci_intr, XHCIInterrupter), | |
3905 | ||
3906 | /* Operational Registers */ | |
3907 | VMSTATE_UINT32(usbcmd, XHCIState), | |
3908 | VMSTATE_UINT32(usbsts, XHCIState), | |
3909 | VMSTATE_UINT32(dnctrl, XHCIState), | |
3910 | VMSTATE_UINT32(crcr_low, XHCIState), | |
3911 | VMSTATE_UINT32(crcr_high, XHCIState), | |
3912 | VMSTATE_UINT32(dcbaap_low, XHCIState), | |
3913 | VMSTATE_UINT32(dcbaap_high, XHCIState), | |
3914 | VMSTATE_UINT32(config, XHCIState), | |
3915 | ||
3916 | /* Runtime Registers & state */ | |
3917 | VMSTATE_INT64(mfindex_start, XHCIState), | |
e720677e | 3918 | VMSTATE_TIMER_PTR(mfwrap_timer, XHCIState), |
37352df3 GH |
3919 | VMSTATE_STRUCT(cmd_ring, XHCIState, 1, vmstate_xhci_ring, XHCIRing), |
3920 | ||
3921 | VMSTATE_END_OF_LIST() | |
3922 | } | |
62c6ae04 HM |
3923 | }; |
3924 | ||
39bffca2 | 3925 | static Property xhci_properties[] = { |
290fd20d C |
3926 | DEFINE_PROP_ON_OFF_AUTO("msi", XHCIState, msi, ON_OFF_AUTO_AUTO), |
3927 | DEFINE_PROP_ON_OFF_AUTO("msix", XHCIState, msix, ON_OFF_AUTO_AUTO), | |
7bafd888 GH |
3928 | DEFINE_PROP_BIT("superspeed-ports-first", |
3929 | XHCIState, flags, XHCI_FLAG_SS_FIRST, true), | |
e6043e92 DDAG |
3930 | DEFINE_PROP_BIT("force-pcie-endcap", XHCIState, flags, |
3931 | XHCI_FLAG_FORCE_PCIE_ENDCAP, false), | |
2aa6bfcb GH |
3932 | DEFINE_PROP_BIT("streams", XHCIState, flags, |
3933 | XHCI_FLAG_ENABLE_STREAMS, true), | |
91062ae0 GH |
3934 | DEFINE_PROP_UINT32("intrs", XHCIState, numintrs, MAXINTRS), |
3935 | DEFINE_PROP_UINT32("slots", XHCIState, numslots, MAXSLOTS), | |
3936 | DEFINE_PROP_UINT32("p2", XHCIState, numports_2, 4), | |
3937 | DEFINE_PROP_UINT32("p3", XHCIState, numports_3, 4), | |
39bffca2 AL |
3938 | DEFINE_PROP_END_OF_LIST(), |
3939 | }; | |
3940 | ||
40021f08 AL |
3941 | static void xhci_class_init(ObjectClass *klass, void *data) |
3942 | { | |
3943 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
39bffca2 | 3944 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 | 3945 | |
39bffca2 AL |
3946 | dc->vmsd = &vmstate_xhci; |
3947 | dc->props = xhci_properties; | |
64619739 | 3948 | dc->reset = xhci_reset; |
125ee0ed | 3949 | set_bit(DEVICE_CATEGORY_USB, dc->categories); |
9af21dbe | 3950 | k->realize = usb_xhci_realize; |
53c30545 | 3951 | k->exit = usb_xhci_exit; |
40021f08 AL |
3952 | k->vendor_id = PCI_VENDOR_ID_NEC; |
3953 | k->device_id = PCI_DEVICE_ID_NEC_UPD720200; | |
3954 | k->class_id = PCI_CLASS_SERIAL_USB; | |
3955 | k->revision = 0x03; | |
3956 | k->is_express = 1; | |
40021f08 AL |
3957 | } |
3958 | ||
8c43a6f0 | 3959 | static const TypeInfo xhci_info = { |
37034575 | 3960 | .name = TYPE_XHCI, |
39bffca2 AL |
3961 | .parent = TYPE_PCI_DEVICE, |
3962 | .instance_size = sizeof(XHCIState), | |
3963 | .class_init = xhci_class_init, | |
62c6ae04 HM |
3964 | }; |
3965 | ||
83f7d43a | 3966 | static void xhci_register_types(void) |
62c6ae04 | 3967 | { |
39bffca2 | 3968 | type_register_static(&xhci_info); |
62c6ae04 | 3969 | } |
83f7d43a AF |
3970 | |
3971 | type_init(xhci_register_types) |