]>
Commit | Line | Data |
---|---|---|
94527ead GH |
1 | /* |
2 | * QEMU USB EHCI Emulation | |
3 | * | |
4 | * Copyright(c) 2008 Emutex Ltd. (address@hidden) | |
5 | * | |
6 | * EHCI project was started by Mark Burkley, with contributions by | |
7 | * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf, | |
8 | * Jan Kiszka and Vincent Palatin contributed bugfixes. | |
9 | * | |
10 | * | |
11 | * This library is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU Lesser General Public | |
13 | * License as published by the Free Software Foundation; either | |
14 | * version 2 of the License, or(at your option) any later version. | |
15 | * | |
16 | * This library is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * Lesser General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
94527ead GH |
23 | */ |
24 | ||
f1ae32a1 | 25 | #include "hw/hw.h" |
94527ead | 26 | #include "qemu-timer.h" |
f1ae32a1 GH |
27 | #include "hw/usb.h" |
28 | #include "hw/pci.h" | |
94527ead | 29 | #include "monitor.h" |
439a97cc | 30 | #include "trace.h" |
0ce668bc | 31 | #include "dma.h" |
94527ead GH |
32 | |
33 | #define EHCI_DEBUG 0 | |
94527ead | 34 | |
26d53979 | 35 | #if EHCI_DEBUG |
94527ead GH |
36 | #define DPRINTF printf |
37 | #else | |
38 | #define DPRINTF(...) | |
39 | #endif | |
40 | ||
94527ead GH |
41 | /* internal processing - reset HC to try and recover */ |
42 | #define USB_RET_PROCERR (-99) | |
43 | ||
44 | #define MMIO_SIZE 0x1000 | |
45 | ||
46 | /* Capability Registers Base Address - section 2.2 */ | |
47 | #define CAPREGBASE 0x0000 | |
48 | #define CAPLENGTH CAPREGBASE + 0x0000 // 1-byte, 0x0001 reserved | |
49 | #define HCIVERSION CAPREGBASE + 0x0002 // 2-bytes, i/f version # | |
50 | #define HCSPARAMS CAPREGBASE + 0x0004 // 4-bytes, structural params | |
51 | #define HCCPARAMS CAPREGBASE + 0x0008 // 4-bytes, capability params | |
52 | #define EECP HCCPARAMS + 1 | |
53 | #define HCSPPORTROUTE1 CAPREGBASE + 0x000c | |
54 | #define HCSPPORTROUTE2 CAPREGBASE + 0x0010 | |
55 | ||
56 | #define OPREGBASE 0x0020 // Operational Registers Base Address | |
57 | ||
58 | #define USBCMD OPREGBASE + 0x0000 | |
59 | #define USBCMD_RUNSTOP (1 << 0) // run / Stop | |
60 | #define USBCMD_HCRESET (1 << 1) // HC Reset | |
61 | #define USBCMD_FLS (3 << 2) // Frame List Size | |
62 | #define USBCMD_FLS_SH 2 // Frame List Size Shift | |
63 | #define USBCMD_PSE (1 << 4) // Periodic Schedule Enable | |
64 | #define USBCMD_ASE (1 << 5) // Asynch Schedule Enable | |
65 | #define USBCMD_IAAD (1 << 6) // Int Asynch Advance Doorbell | |
66 | #define USBCMD_LHCR (1 << 7) // Light Host Controller Reset | |
67 | #define USBCMD_ASPMC (3 << 8) // Async Sched Park Mode Count | |
68 | #define USBCMD_ASPME (1 << 11) // Async Sched Park Mode Enable | |
69 | #define USBCMD_ITC (0x7f << 16) // Int Threshold Control | |
70 | #define USBCMD_ITC_SH 16 // Int Threshold Control Shift | |
71 | ||
72 | #define USBSTS OPREGBASE + 0x0004 | |
73 | #define USBSTS_RO_MASK 0x0000003f | |
74 | #define USBSTS_INT (1 << 0) // USB Interrupt | |
75 | #define USBSTS_ERRINT (1 << 1) // Error Interrupt | |
76 | #define USBSTS_PCD (1 << 2) // Port Change Detect | |
77 | #define USBSTS_FLR (1 << 3) // Frame List Rollover | |
78 | #define USBSTS_HSE (1 << 4) // Host System Error | |
79 | #define USBSTS_IAA (1 << 5) // Interrupt on Async Advance | |
80 | #define USBSTS_HALT (1 << 12) // HC Halted | |
81 | #define USBSTS_REC (1 << 13) // Reclamation | |
82 | #define USBSTS_PSS (1 << 14) // Periodic Schedule Status | |
83 | #define USBSTS_ASS (1 << 15) // Asynchronous Schedule Status | |
84 | ||
85 | /* | |
86 | * Interrupt enable bits correspond to the interrupt active bits in USBSTS | |
87 | * so no need to redefine here. | |
88 | */ | |
89 | #define USBINTR OPREGBASE + 0x0008 | |
90 | #define USBINTR_MASK 0x0000003f | |
91 | ||
92 | #define FRINDEX OPREGBASE + 0x000c | |
93 | #define CTRLDSSEGMENT OPREGBASE + 0x0010 | |
94 | #define PERIODICLISTBASE OPREGBASE + 0x0014 | |
95 | #define ASYNCLISTADDR OPREGBASE + 0x0018 | |
96 | #define ASYNCLISTADDR_MASK 0xffffffe0 | |
97 | ||
98 | #define CONFIGFLAG OPREGBASE + 0x0040 | |
99 | ||
100 | #define PORTSC (OPREGBASE + 0x0044) | |
101 | #define PORTSC_BEGIN PORTSC | |
102 | #define PORTSC_END (PORTSC + 4 * NB_PORTS) | |
103 | /* | |
c44fd61c | 104 | * Bits that are reserved or are read-only are masked out of values |
94527ead GH |
105 | * written to us by software |
106 | */ | |
a0a3167a | 107 | #define PORTSC_RO_MASK 0x007001c0 |
94527ead GH |
108 | #define PORTSC_RWC_MASK 0x0000002a |
109 | #define PORTSC_WKOC_E (1 << 22) // Wake on Over Current Enable | |
110 | #define PORTSC_WKDS_E (1 << 21) // Wake on Disconnect Enable | |
111 | #define PORTSC_WKCN_E (1 << 20) // Wake on Connect Enable | |
112 | #define PORTSC_PTC (15 << 16) // Port Test Control | |
113 | #define PORTSC_PTC_SH 16 // Port Test Control shift | |
114 | #define PORTSC_PIC (3 << 14) // Port Indicator Control | |
115 | #define PORTSC_PIC_SH 14 // Port Indicator Control Shift | |
116 | #define PORTSC_POWNER (1 << 13) // Port Owner | |
117 | #define PORTSC_PPOWER (1 << 12) // Port Power | |
118 | #define PORTSC_LINESTAT (3 << 10) // Port Line Status | |
119 | #define PORTSC_LINESTAT_SH 10 // Port Line Status Shift | |
120 | #define PORTSC_PRESET (1 << 8) // Port Reset | |
121 | #define PORTSC_SUSPEND (1 << 7) // Port Suspend | |
122 | #define PORTSC_FPRES (1 << 6) // Force Port Resume | |
123 | #define PORTSC_OCC (1 << 5) // Over Current Change | |
124 | #define PORTSC_OCA (1 << 4) // Over Current Active | |
125 | #define PORTSC_PEDC (1 << 3) // Port Enable/Disable Change | |
126 | #define PORTSC_PED (1 << 2) // Port Enable/Disable | |
127 | #define PORTSC_CSC (1 << 1) // Connect Status Change | |
128 | #define PORTSC_CONNECT (1 << 0) // Current Connect Status | |
129 | ||
130 | #define FRAME_TIMER_FREQ 1000 | |
adddecb1 | 131 | #define FRAME_TIMER_NS (1000000000 / FRAME_TIMER_FREQ) |
94527ead GH |
132 | |
133 | #define NB_MAXINTRATE 8 // Max rate at which controller issues ints | |
5cc194ca | 134 | #define NB_PORTS 6 // Number of downstream ports |
94527ead | 135 | #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction |
94527ead GH |
136 | #define MAX_QH 100 // Max allowable queue heads in a chain |
137 | ||
138 | /* Internal periodic / asynchronous schedule state machine states | |
139 | */ | |
140 | typedef enum { | |
141 | EST_INACTIVE = 1000, | |
142 | EST_ACTIVE, | |
143 | EST_EXECUTING, | |
144 | EST_SLEEPING, | |
145 | /* The following states are internal to the state machine function | |
146 | */ | |
147 | EST_WAITLISTHEAD, | |
148 | EST_FETCHENTRY, | |
149 | EST_FETCHQH, | |
150 | EST_FETCHITD, | |
2fe80192 | 151 | EST_FETCHSITD, |
94527ead GH |
152 | EST_ADVANCEQUEUE, |
153 | EST_FETCHQTD, | |
154 | EST_EXECUTE, | |
155 | EST_WRITEBACK, | |
156 | EST_HORIZONTALQH | |
157 | } EHCI_STATES; | |
158 | ||
159 | /* macros for accessing fields within next link pointer entry */ | |
160 | #define NLPTR_GET(x) ((x) & 0xffffffe0) | |
161 | #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3) | |
162 | #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid | |
163 | ||
164 | /* link pointer types */ | |
165 | #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor | |
166 | #define NLPTR_TYPE_QH 1 // queue head | |
167 | #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor | |
168 | #define NLPTR_TYPE_FSTN 3 // frame span traversal node | |
169 | ||
170 | ||
171 | /* EHCI spec version 1.0 Section 3.3 | |
172 | */ | |
173 | typedef struct EHCIitd { | |
174 | uint32_t next; | |
175 | ||
176 | uint32_t transact[8]; | |
177 | #define ITD_XACT_ACTIVE (1 << 31) | |
178 | #define ITD_XACT_DBERROR (1 << 30) | |
179 | #define ITD_XACT_BABBLE (1 << 29) | |
180 | #define ITD_XACT_XACTERR (1 << 28) | |
181 | #define ITD_XACT_LENGTH_MASK 0x0fff0000 | |
182 | #define ITD_XACT_LENGTH_SH 16 | |
183 | #define ITD_XACT_IOC (1 << 15) | |
184 | #define ITD_XACT_PGSEL_MASK 0x00007000 | |
185 | #define ITD_XACT_PGSEL_SH 12 | |
186 | #define ITD_XACT_OFFSET_MASK 0x00000fff | |
187 | ||
188 | uint32_t bufptr[7]; | |
189 | #define ITD_BUFPTR_MASK 0xfffff000 | |
190 | #define ITD_BUFPTR_SH 12 | |
191 | #define ITD_BUFPTR_EP_MASK 0x00000f00 | |
192 | #define ITD_BUFPTR_EP_SH 8 | |
193 | #define ITD_BUFPTR_DEVADDR_MASK 0x0000007f | |
194 | #define ITD_BUFPTR_DEVADDR_SH 0 | |
195 | #define ITD_BUFPTR_DIRECTION (1 << 11) | |
196 | #define ITD_BUFPTR_MAXPKT_MASK 0x000007ff | |
197 | #define ITD_BUFPTR_MAXPKT_SH 0 | |
198 | #define ITD_BUFPTR_MULT_MASK 0x00000003 | |
e654887f | 199 | #define ITD_BUFPTR_MULT_SH 0 |
94527ead GH |
200 | } EHCIitd; |
201 | ||
202 | /* EHCI spec version 1.0 Section 3.4 | |
203 | */ | |
204 | typedef struct EHCIsitd { | |
205 | uint32_t next; // Standard next link pointer | |
206 | uint32_t epchar; | |
207 | #define SITD_EPCHAR_IO (1 << 31) | |
208 | #define SITD_EPCHAR_PORTNUM_MASK 0x7f000000 | |
209 | #define SITD_EPCHAR_PORTNUM_SH 24 | |
210 | #define SITD_EPCHAR_HUBADD_MASK 0x007f0000 | |
211 | #define SITD_EPCHAR_HUBADDR_SH 16 | |
212 | #define SITD_EPCHAR_EPNUM_MASK 0x00000f00 | |
213 | #define SITD_EPCHAR_EPNUM_SH 8 | |
214 | #define SITD_EPCHAR_DEVADDR_MASK 0x0000007f | |
215 | ||
216 | uint32_t uframe; | |
217 | #define SITD_UFRAME_CMASK_MASK 0x0000ff00 | |
218 | #define SITD_UFRAME_CMASK_SH 8 | |
219 | #define SITD_UFRAME_SMASK_MASK 0x000000ff | |
220 | ||
221 | uint32_t results; | |
222 | #define SITD_RESULTS_IOC (1 << 31) | |
223 | #define SITD_RESULTS_PGSEL (1 << 30) | |
224 | #define SITD_RESULTS_TBYTES_MASK 0x03ff0000 | |
225 | #define SITD_RESULTS_TYBYTES_SH 16 | |
226 | #define SITD_RESULTS_CPROGMASK_MASK 0x0000ff00 | |
227 | #define SITD_RESULTS_CPROGMASK_SH 8 | |
228 | #define SITD_RESULTS_ACTIVE (1 << 7) | |
229 | #define SITD_RESULTS_ERR (1 << 6) | |
230 | #define SITD_RESULTS_DBERR (1 << 5) | |
231 | #define SITD_RESULTS_BABBLE (1 << 4) | |
232 | #define SITD_RESULTS_XACTERR (1 << 3) | |
233 | #define SITD_RESULTS_MISSEDUF (1 << 2) | |
234 | #define SITD_RESULTS_SPLITXSTATE (1 << 1) | |
235 | ||
236 | uint32_t bufptr[2]; | |
237 | #define SITD_BUFPTR_MASK 0xfffff000 | |
238 | #define SITD_BUFPTR_CURROFF_MASK 0x00000fff | |
239 | #define SITD_BUFPTR_TPOS_MASK 0x00000018 | |
240 | #define SITD_BUFPTR_TPOS_SH 3 | |
241 | #define SITD_BUFPTR_TCNT_MASK 0x00000007 | |
242 | ||
243 | uint32_t backptr; // Standard next link pointer | |
244 | } EHCIsitd; | |
245 | ||
246 | /* EHCI spec version 1.0 Section 3.5 | |
247 | */ | |
248 | typedef struct EHCIqtd { | |
249 | uint32_t next; // Standard next link pointer | |
250 | uint32_t altnext; // Standard next link pointer | |
251 | uint32_t token; | |
252 | #define QTD_TOKEN_DTOGGLE (1 << 31) | |
253 | #define QTD_TOKEN_TBYTES_MASK 0x7fff0000 | |
254 | #define QTD_TOKEN_TBYTES_SH 16 | |
255 | #define QTD_TOKEN_IOC (1 << 15) | |
256 | #define QTD_TOKEN_CPAGE_MASK 0x00007000 | |
257 | #define QTD_TOKEN_CPAGE_SH 12 | |
258 | #define QTD_TOKEN_CERR_MASK 0x00000c00 | |
259 | #define QTD_TOKEN_CERR_SH 10 | |
260 | #define QTD_TOKEN_PID_MASK 0x00000300 | |
261 | #define QTD_TOKEN_PID_SH 8 | |
262 | #define QTD_TOKEN_ACTIVE (1 << 7) | |
263 | #define QTD_TOKEN_HALT (1 << 6) | |
264 | #define QTD_TOKEN_DBERR (1 << 5) | |
265 | #define QTD_TOKEN_BABBLE (1 << 4) | |
266 | #define QTD_TOKEN_XACTERR (1 << 3) | |
267 | #define QTD_TOKEN_MISSEDUF (1 << 2) | |
268 | #define QTD_TOKEN_SPLITXSTATE (1 << 1) | |
269 | #define QTD_TOKEN_PING (1 << 0) | |
270 | ||
271 | uint32_t bufptr[5]; // Standard buffer pointer | |
272 | #define QTD_BUFPTR_MASK 0xfffff000 | |
0ce668bc | 273 | #define QTD_BUFPTR_SH 12 |
94527ead GH |
274 | } EHCIqtd; |
275 | ||
276 | /* EHCI spec version 1.0 Section 3.6 | |
277 | */ | |
278 | typedef struct EHCIqh { | |
279 | uint32_t next; // Standard next link pointer | |
280 | ||
281 | /* endpoint characteristics */ | |
282 | uint32_t epchar; | |
283 | #define QH_EPCHAR_RL_MASK 0xf0000000 | |
284 | #define QH_EPCHAR_RL_SH 28 | |
285 | #define QH_EPCHAR_C (1 << 27) | |
286 | #define QH_EPCHAR_MPLEN_MASK 0x07FF0000 | |
287 | #define QH_EPCHAR_MPLEN_SH 16 | |
288 | #define QH_EPCHAR_H (1 << 15) | |
289 | #define QH_EPCHAR_DTC (1 << 14) | |
290 | #define QH_EPCHAR_EPS_MASK 0x00003000 | |
291 | #define QH_EPCHAR_EPS_SH 12 | |
292 | #define EHCI_QH_EPS_FULL 0 | |
293 | #define EHCI_QH_EPS_LOW 1 | |
294 | #define EHCI_QH_EPS_HIGH 2 | |
295 | #define EHCI_QH_EPS_RESERVED 3 | |
296 | ||
297 | #define QH_EPCHAR_EP_MASK 0x00000f00 | |
298 | #define QH_EPCHAR_EP_SH 8 | |
299 | #define QH_EPCHAR_I (1 << 7) | |
300 | #define QH_EPCHAR_DEVADDR_MASK 0x0000007f | |
301 | #define QH_EPCHAR_DEVADDR_SH 0 | |
302 | ||
303 | /* endpoint capabilities */ | |
304 | uint32_t epcap; | |
305 | #define QH_EPCAP_MULT_MASK 0xc0000000 | |
306 | #define QH_EPCAP_MULT_SH 30 | |
307 | #define QH_EPCAP_PORTNUM_MASK 0x3f800000 | |
308 | #define QH_EPCAP_PORTNUM_SH 23 | |
309 | #define QH_EPCAP_HUBADDR_MASK 0x007f0000 | |
310 | #define QH_EPCAP_HUBADDR_SH 16 | |
311 | #define QH_EPCAP_CMASK_MASK 0x0000ff00 | |
312 | #define QH_EPCAP_CMASK_SH 8 | |
313 | #define QH_EPCAP_SMASK_MASK 0x000000ff | |
314 | #define QH_EPCAP_SMASK_SH 0 | |
315 | ||
316 | uint32_t current_qtd; // Standard next link pointer | |
317 | uint32_t next_qtd; // Standard next link pointer | |
318 | uint32_t altnext_qtd; | |
319 | #define QH_ALTNEXT_NAKCNT_MASK 0x0000001e | |
320 | #define QH_ALTNEXT_NAKCNT_SH 1 | |
321 | ||
322 | uint32_t token; // Same as QTD token | |
323 | uint32_t bufptr[5]; // Standard buffer pointer | |
324 | #define BUFPTR_CPROGMASK_MASK 0x000000ff | |
325 | #define BUFPTR_FRAMETAG_MASK 0x0000001f | |
326 | #define BUFPTR_SBYTES_MASK 0x00000fe0 | |
327 | #define BUFPTR_SBYTES_SH 5 | |
328 | } EHCIqh; | |
329 | ||
330 | /* EHCI spec version 1.0 Section 3.7 | |
331 | */ | |
332 | typedef struct EHCIfstn { | |
333 | uint32_t next; // Standard next link pointer | |
334 | uint32_t backptr; // Standard next link pointer | |
335 | } EHCIfstn; | |
336 | ||
eb36a88e | 337 | typedef struct EHCIPacket EHCIPacket; |
0122f472 GH |
338 | typedef struct EHCIQueue EHCIQueue; |
339 | typedef struct EHCIState EHCIState; | |
340 | ||
341 | enum async_state { | |
342 | EHCI_ASYNC_NONE = 0, | |
343 | EHCI_ASYNC_INFLIGHT, | |
344 | EHCI_ASYNC_FINISHED, | |
345 | }; | |
346 | ||
eb36a88e GH |
347 | struct EHCIPacket { |
348 | EHCIQueue *queue; | |
349 | QTAILQ_ENTRY(EHCIPacket) next; | |
350 | ||
351 | EHCIqtd qtd; /* copy of current QTD (being worked on) */ | |
352 | uint32_t qtdaddr; /* address QTD read from */ | |
353 | ||
354 | USBPacket packet; | |
355 | QEMUSGList sgl; | |
356 | int pid; | |
357 | uint32_t tbytes; | |
358 | enum async_state async; | |
359 | int usb_status; | |
360 | }; | |
361 | ||
0122f472 GH |
362 | struct EHCIQueue { |
363 | EHCIState *ehci; | |
8ac6d699 | 364 | QTAILQ_ENTRY(EHCIQueue) next; |
adddecb1 GH |
365 | uint32_t seen; |
366 | uint64_t ts; | |
ae0138a8 | 367 | int async; |
0122f472 GH |
368 | |
369 | /* cached data from guest - needs to be flushed | |
370 | * when guest removes an entry (doorbell, handshake sequence) | |
371 | */ | |
eb36a88e GH |
372 | EHCIqh qh; /* copy of current QH (being worked on) */ |
373 | uint32_t qhaddr; /* address QH read from */ | |
374 | uint32_t qtdaddr; /* address QTD read from */ | |
e59928b3 | 375 | USBDevice *dev; |
eb36a88e | 376 | QTAILQ_HEAD(, EHCIPacket) packets; |
0122f472 GH |
377 | }; |
378 | ||
df5d5c5c HG |
379 | typedef QTAILQ_HEAD(EHCIQueueHead, EHCIQueue) EHCIQueueHead; |
380 | ||
0122f472 | 381 | struct EHCIState { |
94527ead | 382 | PCIDevice dev; |
0122f472 | 383 | USBBus bus; |
94527ead | 384 | qemu_irq irq; |
e57964f5 | 385 | MemoryRegion mem; |
a0a3167a | 386 | int companion_count; |
16a2dee6 GH |
387 | |
388 | /* properties */ | |
16a2dee6 GH |
389 | uint32_t maxframes; |
390 | ||
94527ead GH |
391 | /* |
392 | * EHCI spec version 1.0 Section 2.3 | |
393 | * Host Controller Operational Registers | |
394 | */ | |
395 | union { | |
396 | uint8_t mmio[MMIO_SIZE]; | |
397 | struct { | |
398 | uint8_t cap[OPREGBASE]; | |
399 | uint32_t usbcmd; | |
400 | uint32_t usbsts; | |
401 | uint32_t usbintr; | |
402 | uint32_t frindex; | |
403 | uint32_t ctrldssegment; | |
404 | uint32_t periodiclistbase; | |
405 | uint32_t asynclistaddr; | |
406 | uint32_t notused[9]; | |
407 | uint32_t configflag; | |
408 | uint32_t portsc[NB_PORTS]; | |
409 | }; | |
410 | }; | |
0122f472 | 411 | |
94527ead GH |
412 | /* |
413 | * Internal states, shadow registers, etc | |
414 | */ | |
94527ead | 415 | QEMUTimer *frame_timer; |
0fb3e299 | 416 | QEMUBH *async_bh; |
9a773408 GH |
417 | uint32_t astate; /* Current state in asynchronous schedule */ |
418 | uint32_t pstate; /* Current state in periodic schedule */ | |
94527ead | 419 | USBPort ports[NB_PORTS]; |
a0a3167a | 420 | USBPort *companion_ports[NB_PORTS]; |
94527ead | 421 | uint32_t usbsts_pending; |
df5d5c5c HG |
422 | EHCIQueueHead aqueues; |
423 | EHCIQueueHead pqueues; | |
94527ead | 424 | |
9a773408 GH |
425 | /* which address to look at next */ |
426 | uint32_t a_fetch_addr; | |
427 | uint32_t p_fetch_addr; | |
94527ead | 428 | |
0122f472 | 429 | USBPacket ipacket; |
0ce668bc | 430 | QEMUSGList isgl; |
0122f472 | 431 | |
adddecb1 | 432 | uint64_t last_run_ns; |
3a215326 | 433 | uint32_t async_stepdown; |
0122f472 | 434 | }; |
94527ead GH |
435 | |
436 | #define SET_LAST_RUN_CLOCK(s) \ | |
adddecb1 | 437 | (s)->last_run_ns = qemu_get_clock_ns(vm_clock); |
94527ead GH |
438 | |
439 | /* nifty macros from Arnon's EHCI version */ | |
440 | #define get_field(data, field) \ | |
441 | (((data) & field##_MASK) >> field##_SH) | |
442 | ||
443 | #define set_field(data, newval, field) do { \ | |
444 | uint32_t val = *data; \ | |
445 | val &= ~ field##_MASK; \ | |
446 | val |= ((newval) << field##_SH) & field##_MASK; \ | |
447 | *data = val; \ | |
448 | } while(0) | |
449 | ||
26d53979 | 450 | static const char *ehci_state_names[] = { |
aac882e7 GH |
451 | [EST_INACTIVE] = "INACTIVE", |
452 | [EST_ACTIVE] = "ACTIVE", | |
453 | [EST_EXECUTING] = "EXECUTING", | |
454 | [EST_SLEEPING] = "SLEEPING", | |
455 | [EST_WAITLISTHEAD] = "WAITLISTHEAD", | |
456 | [EST_FETCHENTRY] = "FETCH ENTRY", | |
457 | [EST_FETCHQH] = "FETCH QH", | |
458 | [EST_FETCHITD] = "FETCH ITD", | |
459 | [EST_ADVANCEQUEUE] = "ADVANCEQUEUE", | |
460 | [EST_FETCHQTD] = "FETCH QTD", | |
461 | [EST_EXECUTE] = "EXECUTE", | |
462 | [EST_WRITEBACK] = "WRITEBACK", | |
463 | [EST_HORIZONTALQH] = "HORIZONTALQH", | |
26d53979 GH |
464 | }; |
465 | ||
466 | static const char *ehci_mmio_names[] = { | |
aac882e7 GH |
467 | [CAPLENGTH] = "CAPLENGTH", |
468 | [HCIVERSION] = "HCIVERSION", | |
469 | [HCSPARAMS] = "HCSPARAMS", | |
470 | [HCCPARAMS] = "HCCPARAMS", | |
471 | [USBCMD] = "USBCMD", | |
472 | [USBSTS] = "USBSTS", | |
473 | [USBINTR] = "USBINTR", | |
474 | [FRINDEX] = "FRINDEX", | |
475 | [PERIODICLISTBASE] = "P-LIST BASE", | |
476 | [ASYNCLISTADDR] = "A-LIST ADDR", | |
477 | [PORTSC_BEGIN] = "PORTSC #0", | |
478 | [PORTSC_BEGIN + 4] = "PORTSC #1", | |
479 | [PORTSC_BEGIN + 8] = "PORTSC #2", | |
480 | [PORTSC_BEGIN + 12] = "PORTSC #3", | |
335b8d20 GH |
481 | [PORTSC_BEGIN + 16] = "PORTSC #4", |
482 | [PORTSC_BEGIN + 20] = "PORTSC #5", | |
aac882e7 | 483 | [CONFIGFLAG] = "CONFIGFLAG", |
26d53979 | 484 | }; |
94527ead | 485 | |
26d53979 | 486 | static const char *nr2str(const char **n, size_t len, uint32_t nr) |
94527ead | 487 | { |
26d53979 GH |
488 | if (nr < len && n[nr] != NULL) { |
489 | return n[nr]; | |
94527ead | 490 | } else { |
26d53979 | 491 | return "unknown"; |
94527ead GH |
492 | } |
493 | } | |
94527ead | 494 | |
26d53979 GH |
495 | static const char *state2str(uint32_t state) |
496 | { | |
497 | return nr2str(ehci_state_names, ARRAY_SIZE(ehci_state_names), state); | |
498 | } | |
499 | ||
500 | static const char *addr2str(target_phys_addr_t addr) | |
501 | { | |
502 | return nr2str(ehci_mmio_names, ARRAY_SIZE(ehci_mmio_names), addr); | |
503 | } | |
504 | ||
439a97cc GH |
505 | static void ehci_trace_usbsts(uint32_t mask, int state) |
506 | { | |
507 | /* interrupts */ | |
508 | if (mask & USBSTS_INT) { | |
509 | trace_usb_ehci_usbsts("INT", state); | |
510 | } | |
511 | if (mask & USBSTS_ERRINT) { | |
512 | trace_usb_ehci_usbsts("ERRINT", state); | |
513 | } | |
514 | if (mask & USBSTS_PCD) { | |
515 | trace_usb_ehci_usbsts("PCD", state); | |
516 | } | |
517 | if (mask & USBSTS_FLR) { | |
518 | trace_usb_ehci_usbsts("FLR", state); | |
519 | } | |
520 | if (mask & USBSTS_HSE) { | |
521 | trace_usb_ehci_usbsts("HSE", state); | |
522 | } | |
523 | if (mask & USBSTS_IAA) { | |
524 | trace_usb_ehci_usbsts("IAA", state); | |
525 | } | |
526 | ||
527 | /* status */ | |
528 | if (mask & USBSTS_HALT) { | |
529 | trace_usb_ehci_usbsts("HALT", state); | |
530 | } | |
531 | if (mask & USBSTS_REC) { | |
532 | trace_usb_ehci_usbsts("REC", state); | |
533 | } | |
534 | if (mask & USBSTS_PSS) { | |
535 | trace_usb_ehci_usbsts("PSS", state); | |
536 | } | |
537 | if (mask & USBSTS_ASS) { | |
538 | trace_usb_ehci_usbsts("ASS", state); | |
539 | } | |
540 | } | |
541 | ||
542 | static inline void ehci_set_usbsts(EHCIState *s, int mask) | |
543 | { | |
544 | if ((s->usbsts & mask) == mask) { | |
545 | return; | |
546 | } | |
547 | ehci_trace_usbsts(mask, 1); | |
548 | s->usbsts |= mask; | |
549 | } | |
550 | ||
551 | static inline void ehci_clear_usbsts(EHCIState *s, int mask) | |
552 | { | |
553 | if ((s->usbsts & mask) == 0) { | |
554 | return; | |
555 | } | |
556 | ehci_trace_usbsts(mask, 0); | |
557 | s->usbsts &= ~mask; | |
558 | } | |
94527ead GH |
559 | |
560 | static inline void ehci_set_interrupt(EHCIState *s, int intr) | |
561 | { | |
562 | int level = 0; | |
563 | ||
564 | // TODO honour interrupt threshold requests | |
565 | ||
439a97cc | 566 | ehci_set_usbsts(s, intr); |
94527ead GH |
567 | |
568 | if ((s->usbsts & USBINTR_MASK) & s->usbintr) { | |
569 | level = 1; | |
570 | } | |
571 | ||
572 | qemu_set_irq(s->irq, level); | |
573 | } | |
574 | ||
575 | static inline void ehci_record_interrupt(EHCIState *s, int intr) | |
576 | { | |
577 | s->usbsts_pending |= intr; | |
578 | } | |
579 | ||
580 | static inline void ehci_commit_interrupt(EHCIState *s) | |
581 | { | |
582 | if (!s->usbsts_pending) { | |
583 | return; | |
584 | } | |
585 | ehci_set_interrupt(s, s->usbsts_pending); | |
586 | s->usbsts_pending = 0; | |
587 | } | |
588 | ||
daf25307 GH |
589 | static void ehci_update_halt(EHCIState *s) |
590 | { | |
591 | if (s->usbcmd & USBCMD_RUNSTOP) { | |
592 | ehci_clear_usbsts(s, USBSTS_HALT); | |
593 | } else { | |
594 | if (s->astate == EST_INACTIVE && s->pstate == EST_INACTIVE) { | |
595 | ehci_set_usbsts(s, USBSTS_HALT); | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
26d53979 GH |
600 | static void ehci_set_state(EHCIState *s, int async, int state) |
601 | { | |
602 | if (async) { | |
603 | trace_usb_ehci_state("async", state2str(state)); | |
604 | s->astate = state; | |
b53f685d GH |
605 | if (s->astate == EST_INACTIVE) { |
606 | ehci_clear_usbsts(s, USBSTS_ASS); | |
daf25307 | 607 | ehci_update_halt(s); |
b53f685d GH |
608 | } else { |
609 | ehci_set_usbsts(s, USBSTS_ASS); | |
610 | } | |
26d53979 GH |
611 | } else { |
612 | trace_usb_ehci_state("periodic", state2str(state)); | |
613 | s->pstate = state; | |
b53f685d GH |
614 | if (s->pstate == EST_INACTIVE) { |
615 | ehci_clear_usbsts(s, USBSTS_PSS); | |
daf25307 | 616 | ehci_update_halt(s); |
b53f685d GH |
617 | } else { |
618 | ehci_set_usbsts(s, USBSTS_PSS); | |
619 | } | |
26d53979 GH |
620 | } |
621 | } | |
622 | ||
623 | static int ehci_get_state(EHCIState *s, int async) | |
624 | { | |
625 | return async ? s->astate : s->pstate; | |
626 | } | |
627 | ||
0122f472 GH |
628 | static void ehci_set_fetch_addr(EHCIState *s, int async, uint32_t addr) |
629 | { | |
630 | if (async) { | |
631 | s->a_fetch_addr = addr; | |
632 | } else { | |
633 | s->p_fetch_addr = addr; | |
634 | } | |
635 | } | |
636 | ||
637 | static int ehci_get_fetch_addr(EHCIState *s, int async) | |
638 | { | |
639 | return async ? s->a_fetch_addr : s->p_fetch_addr; | |
640 | } | |
641 | ||
8ac6d699 | 642 | static void ehci_trace_qh(EHCIQueue *q, target_phys_addr_t addr, EHCIqh *qh) |
26d53979 | 643 | { |
025b168c GH |
644 | /* need three here due to argument count limits */ |
645 | trace_usb_ehci_qh_ptrs(q, addr, qh->next, | |
646 | qh->current_qtd, qh->next_qtd, qh->altnext_qtd); | |
647 | trace_usb_ehci_qh_fields(addr, | |
648 | get_field(qh->epchar, QH_EPCHAR_RL), | |
649 | get_field(qh->epchar, QH_EPCHAR_MPLEN), | |
650 | get_field(qh->epchar, QH_EPCHAR_EPS), | |
651 | get_field(qh->epchar, QH_EPCHAR_EP), | |
652 | get_field(qh->epchar, QH_EPCHAR_DEVADDR)); | |
653 | trace_usb_ehci_qh_bits(addr, | |
654 | (bool)(qh->epchar & QH_EPCHAR_C), | |
655 | (bool)(qh->epchar & QH_EPCHAR_H), | |
656 | (bool)(qh->epchar & QH_EPCHAR_DTC), | |
657 | (bool)(qh->epchar & QH_EPCHAR_I)); | |
26d53979 GH |
658 | } |
659 | ||
8ac6d699 | 660 | static void ehci_trace_qtd(EHCIQueue *q, target_phys_addr_t addr, EHCIqtd *qtd) |
26d53979 | 661 | { |
025b168c GH |
662 | /* need three here due to argument count limits */ |
663 | trace_usb_ehci_qtd_ptrs(q, addr, qtd->next, qtd->altnext); | |
664 | trace_usb_ehci_qtd_fields(addr, | |
665 | get_field(qtd->token, QTD_TOKEN_TBYTES), | |
666 | get_field(qtd->token, QTD_TOKEN_CPAGE), | |
667 | get_field(qtd->token, QTD_TOKEN_CERR), | |
668 | get_field(qtd->token, QTD_TOKEN_PID)); | |
669 | trace_usb_ehci_qtd_bits(addr, | |
670 | (bool)(qtd->token & QTD_TOKEN_IOC), | |
671 | (bool)(qtd->token & QTD_TOKEN_ACTIVE), | |
672 | (bool)(qtd->token & QTD_TOKEN_HALT), | |
673 | (bool)(qtd->token & QTD_TOKEN_BABBLE), | |
674 | (bool)(qtd->token & QTD_TOKEN_XACTERR)); | |
26d53979 GH |
675 | } |
676 | ||
677 | static void ehci_trace_itd(EHCIState *s, target_phys_addr_t addr, EHCIitd *itd) | |
678 | { | |
e654887f GH |
679 | trace_usb_ehci_itd(addr, itd->next, |
680 | get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT), | |
681 | get_field(itd->bufptr[2], ITD_BUFPTR_MULT), | |
682 | get_field(itd->bufptr[0], ITD_BUFPTR_EP), | |
683 | get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR)); | |
26d53979 GH |
684 | } |
685 | ||
2fe80192 GH |
686 | static void ehci_trace_sitd(EHCIState *s, target_phys_addr_t addr, |
687 | EHCIsitd *sitd) | |
688 | { | |
689 | trace_usb_ehci_sitd(addr, sitd->next, | |
690 | (bool)(sitd->results & SITD_RESULTS_ACTIVE)); | |
691 | } | |
692 | ||
ec807d12 GH |
693 | static inline bool ehci_enabled(EHCIState *s) |
694 | { | |
695 | return s->usbcmd & USBCMD_RUNSTOP; | |
696 | } | |
697 | ||
698 | static inline bool ehci_async_enabled(EHCIState *s) | |
699 | { | |
700 | return ehci_enabled(s) && (s->usbcmd & USBCMD_ASE); | |
701 | } | |
702 | ||
703 | static inline bool ehci_periodic_enabled(EHCIState *s) | |
704 | { | |
705 | return ehci_enabled(s) && (s->usbcmd & USBCMD_PSE); | |
706 | } | |
707 | ||
eb36a88e GH |
708 | /* packet management */ |
709 | ||
710 | static EHCIPacket *ehci_alloc_packet(EHCIQueue *q) | |
711 | { | |
712 | EHCIPacket *p; | |
713 | ||
eb36a88e GH |
714 | p = g_new0(EHCIPacket, 1); |
715 | p->queue = q; | |
716 | usb_packet_init(&p->packet); | |
717 | QTAILQ_INSERT_TAIL(&q->packets, p, next); | |
718 | trace_usb_ehci_packet_action(p->queue, p, "alloc"); | |
719 | return p; | |
720 | } | |
721 | ||
722 | static void ehci_free_packet(EHCIPacket *p) | |
723 | { | |
724 | trace_usb_ehci_packet_action(p->queue, p, "free"); | |
725 | if (p->async == EHCI_ASYNC_INFLIGHT) { | |
726 | usb_cancel_packet(&p->packet); | |
727 | } | |
728 | QTAILQ_REMOVE(&p->queue->packets, p, next); | |
729 | usb_packet_cleanup(&p->packet); | |
730 | g_free(p); | |
731 | } | |
732 | ||
8ac6d699 GH |
733 | /* queue management */ |
734 | ||
8f6d5e26 | 735 | static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, uint32_t addr, int async) |
8ac6d699 | 736 | { |
df5d5c5c | 737 | EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; |
8ac6d699 GH |
738 | EHCIQueue *q; |
739 | ||
7267c094 | 740 | q = g_malloc0(sizeof(*q)); |
8ac6d699 | 741 | q->ehci = ehci; |
8f6d5e26 | 742 | q->qhaddr = addr; |
ae0138a8 | 743 | q->async = async; |
eb36a88e | 744 | QTAILQ_INIT(&q->packets); |
df5d5c5c | 745 | QTAILQ_INSERT_HEAD(head, q, next); |
8ac6d699 GH |
746 | trace_usb_ehci_queue_action(q, "alloc"); |
747 | return q; | |
748 | } | |
749 | ||
ae0138a8 | 750 | static void ehci_free_queue(EHCIQueue *q) |
8ac6d699 | 751 | { |
ae0138a8 | 752 | EHCIQueueHead *head = q->async ? &q->ehci->aqueues : &q->ehci->pqueues; |
eb36a88e GH |
753 | EHCIPacket *p; |
754 | ||
8ac6d699 | 755 | trace_usb_ehci_queue_action(q, "free"); |
eb36a88e GH |
756 | while ((p = QTAILQ_FIRST(&q->packets)) != NULL) { |
757 | ehci_free_packet(p); | |
8ac6d699 | 758 | } |
df5d5c5c | 759 | QTAILQ_REMOVE(head, q, next); |
7267c094 | 760 | g_free(q); |
8ac6d699 GH |
761 | } |
762 | ||
df5d5c5c HG |
763 | static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr, |
764 | int async) | |
8ac6d699 | 765 | { |
df5d5c5c | 766 | EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; |
8ac6d699 GH |
767 | EHCIQueue *q; |
768 | ||
df5d5c5c | 769 | QTAILQ_FOREACH(q, head, next) { |
8ac6d699 GH |
770 | if (addr == q->qhaddr) { |
771 | return q; | |
772 | } | |
773 | } | |
774 | return NULL; | |
775 | } | |
776 | ||
4be23939 | 777 | static void ehci_queues_rip_unused(EHCIState *ehci, int async, int flush) |
8ac6d699 | 778 | { |
df5d5c5c | 779 | EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; |
3a215326 | 780 | uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4; |
8ac6d699 GH |
781 | EHCIQueue *q, *tmp; |
782 | ||
df5d5c5c | 783 | QTAILQ_FOREACH_SAFE(q, head, next, tmp) { |
8ac6d699 GH |
784 | if (q->seen) { |
785 | q->seen = 0; | |
adddecb1 | 786 | q->ts = ehci->last_run_ns; |
8ac6d699 GH |
787 | continue; |
788 | } | |
3a215326 | 789 | if (!flush && ehci->last_run_ns < q->ts + maxage) { |
8ac6d699 GH |
790 | continue; |
791 | } | |
ae0138a8 | 792 | ehci_free_queue(q); |
8ac6d699 GH |
793 | } |
794 | } | |
795 | ||
df5d5c5c | 796 | static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async) |
07771f6f | 797 | { |
df5d5c5c | 798 | EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; |
07771f6f GH |
799 | EHCIQueue *q, *tmp; |
800 | ||
df5d5c5c | 801 | QTAILQ_FOREACH_SAFE(q, head, next, tmp) { |
e59928b3 | 802 | if (q->dev != dev) { |
07771f6f GH |
803 | continue; |
804 | } | |
ae0138a8 | 805 | ehci_free_queue(q); |
07771f6f GH |
806 | } |
807 | } | |
808 | ||
df5d5c5c | 809 | static void ehci_queues_rip_all(EHCIState *ehci, int async) |
8ac6d699 | 810 | { |
df5d5c5c | 811 | EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; |
8ac6d699 GH |
812 | EHCIQueue *q, *tmp; |
813 | ||
df5d5c5c | 814 | QTAILQ_FOREACH_SAFE(q, head, next, tmp) { |
ae0138a8 | 815 | ehci_free_queue(q); |
8ac6d699 GH |
816 | } |
817 | } | |
818 | ||
94527ead GH |
819 | /* Attach or detach a device on root hub */ |
820 | ||
821 | static void ehci_attach(USBPort *port) | |
822 | { | |
823 | EHCIState *s = port->opaque; | |
824 | uint32_t *portsc = &s->portsc[port->index]; | |
825 | ||
dcbd0b5c | 826 | trace_usb_ehci_port_attach(port->index, port->dev->product_desc); |
94527ead | 827 | |
a0a3167a HG |
828 | if (*portsc & PORTSC_POWNER) { |
829 | USBPort *companion = s->companion_ports[port->index]; | |
830 | companion->dev = port->dev; | |
831 | companion->ops->attach(companion); | |
832 | return; | |
833 | } | |
834 | ||
94527ead GH |
835 | *portsc |= PORTSC_CONNECT; |
836 | *portsc |= PORTSC_CSC; | |
837 | ||
a0a3167a | 838 | ehci_set_interrupt(s, USBSTS_PCD); |
94527ead GH |
839 | } |
840 | ||
841 | static void ehci_detach(USBPort *port) | |
842 | { | |
843 | EHCIState *s = port->opaque; | |
844 | uint32_t *portsc = &s->portsc[port->index]; | |
845 | ||
dcbd0b5c | 846 | trace_usb_ehci_port_detach(port->index); |
94527ead | 847 | |
a0a3167a HG |
848 | if (*portsc & PORTSC_POWNER) { |
849 | USBPort *companion = s->companion_ports[port->index]; | |
850 | companion->ops->detach(companion); | |
851 | companion->dev = NULL; | |
f76e1d81 HG |
852 | /* |
853 | * EHCI spec 4.2.2: "When a disconnect occurs... On the event, | |
854 | * the port ownership is returned immediately to the EHCI controller." | |
855 | */ | |
856 | *portsc &= ~PORTSC_POWNER; | |
a0a3167a HG |
857 | return; |
858 | } | |
859 | ||
df5d5c5c HG |
860 | ehci_queues_rip_device(s, port->dev, 0); |
861 | ehci_queues_rip_device(s, port->dev, 1); | |
4706ab6c | 862 | |
fbd97532 | 863 | *portsc &= ~(PORTSC_CONNECT|PORTSC_PED); |
94527ead GH |
864 | *portsc |= PORTSC_CSC; |
865 | ||
a0a3167a | 866 | ehci_set_interrupt(s, USBSTS_PCD); |
94527ead GH |
867 | } |
868 | ||
4706ab6c HG |
869 | static void ehci_child_detach(USBPort *port, USBDevice *child) |
870 | { | |
871 | EHCIState *s = port->opaque; | |
a0a3167a HG |
872 | uint32_t portsc = s->portsc[port->index]; |
873 | ||
874 | if (portsc & PORTSC_POWNER) { | |
875 | USBPort *companion = s->companion_ports[port->index]; | |
876 | companion->ops->child_detach(companion, child); | |
a0a3167a HG |
877 | return; |
878 | } | |
4706ab6c | 879 | |
df5d5c5c HG |
880 | ehci_queues_rip_device(s, child, 0); |
881 | ehci_queues_rip_device(s, child, 1); | |
4706ab6c HG |
882 | } |
883 | ||
a0a3167a HG |
884 | static void ehci_wakeup(USBPort *port) |
885 | { | |
886 | EHCIState *s = port->opaque; | |
887 | uint32_t portsc = s->portsc[port->index]; | |
888 | ||
889 | if (portsc & PORTSC_POWNER) { | |
890 | USBPort *companion = s->companion_ports[port->index]; | |
891 | if (companion->ops->wakeup) { | |
892 | companion->ops->wakeup(companion); | |
0f588df8 GH |
893 | } else { |
894 | qemu_bh_schedule(s->async_bh); | |
a0a3167a HG |
895 | } |
896 | } | |
897 | } | |
898 | ||
899 | static int ehci_register_companion(USBBus *bus, USBPort *ports[], | |
900 | uint32_t portcount, uint32_t firstport) | |
901 | { | |
902 | EHCIState *s = container_of(bus, EHCIState, bus); | |
903 | uint32_t i; | |
904 | ||
905 | if (firstport + portcount > NB_PORTS) { | |
906 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "firstport", | |
907 | "firstport on masterbus"); | |
908 | error_printf_unless_qmp( | |
909 | "firstport value of %u makes companion take ports %u - %u, which " | |
910 | "is outside of the valid range of 0 - %u\n", firstport, firstport, | |
911 | firstport + portcount - 1, NB_PORTS - 1); | |
912 | return -1; | |
913 | } | |
914 | ||
915 | for (i = 0; i < portcount; i++) { | |
916 | if (s->companion_ports[firstport + i]) { | |
917 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus", | |
918 | "an USB masterbus"); | |
919 | error_printf_unless_qmp( | |
920 | "port %u on masterbus %s already has a companion assigned\n", | |
921 | firstport + i, bus->qbus.name); | |
922 | return -1; | |
923 | } | |
924 | } | |
925 | ||
926 | for (i = 0; i < portcount; i++) { | |
927 | s->companion_ports[firstport + i] = ports[i]; | |
928 | s->ports[firstport + i].speedmask |= | |
929 | USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL; | |
930 | /* Ensure devs attached before the initial reset go to the companion */ | |
931 | s->portsc[firstport + i] = PORTSC_POWNER; | |
932 | } | |
933 | ||
934 | s->companion_count++; | |
935 | s->mmio[0x05] = (s->companion_count << 4) | portcount; | |
936 | ||
937 | return 0; | |
938 | } | |
939 | ||
828143c6 GH |
940 | static USBDevice *ehci_find_device(EHCIState *ehci, uint8_t addr) |
941 | { | |
942 | USBDevice *dev; | |
943 | USBPort *port; | |
944 | int i; | |
945 | ||
946 | for (i = 0; i < NB_PORTS; i++) { | |
947 | port = &ehci->ports[i]; | |
948 | if (!(ehci->portsc[i] & PORTSC_PED)) { | |
949 | DPRINTF("Port %d not enabled\n", i); | |
950 | continue; | |
951 | } | |
952 | dev = usb_find_device(port, addr); | |
953 | if (dev != NULL) { | |
954 | return dev; | |
955 | } | |
956 | } | |
957 | return NULL; | |
958 | } | |
959 | ||
94527ead GH |
960 | /* 4.1 host controller initialization */ |
961 | static void ehci_reset(void *opaque) | |
962 | { | |
963 | EHCIState *s = opaque; | |
94527ead | 964 | int i; |
a0a3167a | 965 | USBDevice *devs[NB_PORTS]; |
94527ead | 966 | |
439a97cc | 967 | trace_usb_ehci_reset(); |
94527ead | 968 | |
a0a3167a HG |
969 | /* |
970 | * Do the detach before touching portsc, so that it correctly gets send to | |
971 | * us or to our companion based on PORTSC_POWNER before the reset. | |
972 | */ | |
973 | for(i = 0; i < NB_PORTS; i++) { | |
974 | devs[i] = s->ports[i].dev; | |
891fb2cd GH |
975 | if (devs[i] && devs[i]->attached) { |
976 | usb_detach(&s->ports[i]); | |
a0a3167a HG |
977 | } |
978 | } | |
979 | ||
94527ead GH |
980 | memset(&s->mmio[OPREGBASE], 0x00, MMIO_SIZE - OPREGBASE); |
981 | ||
982 | s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH; | |
983 | s->usbsts = USBSTS_HALT; | |
984 | ||
985 | s->astate = EST_INACTIVE; | |
986 | s->pstate = EST_INACTIVE; | |
94527ead GH |
987 | |
988 | for(i = 0; i < NB_PORTS; i++) { | |
a0a3167a HG |
989 | if (s->companion_ports[i]) { |
990 | s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER; | |
991 | } else { | |
992 | s->portsc[i] = PORTSC_PPOWER; | |
993 | } | |
891fb2cd GH |
994 | if (devs[i] && devs[i]->attached) { |
995 | usb_attach(&s->ports[i]); | |
d28f4e2d | 996 | usb_device_reset(devs[i]); |
94527ead GH |
997 | } |
998 | } | |
df5d5c5c HG |
999 | ehci_queues_rip_all(s, 0); |
1000 | ehci_queues_rip_all(s, 1); | |
81d37739 | 1001 | qemu_del_timer(s->frame_timer); |
0fb3e299 | 1002 | qemu_bh_cancel(s->async_bh); |
94527ead GH |
1003 | } |
1004 | ||
1005 | static uint32_t ehci_mem_readb(void *ptr, target_phys_addr_t addr) | |
1006 | { | |
1007 | EHCIState *s = ptr; | |
1008 | uint32_t val; | |
1009 | ||
1010 | val = s->mmio[addr]; | |
1011 | ||
1012 | return val; | |
1013 | } | |
1014 | ||
1015 | static uint32_t ehci_mem_readw(void *ptr, target_phys_addr_t addr) | |
1016 | { | |
1017 | EHCIState *s = ptr; | |
1018 | uint32_t val; | |
1019 | ||
1020 | val = s->mmio[addr] | (s->mmio[addr+1] << 8); | |
1021 | ||
1022 | return val; | |
1023 | } | |
1024 | ||
1025 | static uint32_t ehci_mem_readl(void *ptr, target_phys_addr_t addr) | |
1026 | { | |
1027 | EHCIState *s = ptr; | |
1028 | uint32_t val; | |
1029 | ||
1030 | val = s->mmio[addr] | (s->mmio[addr+1] << 8) | | |
1031 | (s->mmio[addr+2] << 16) | (s->mmio[addr+3] << 24); | |
1032 | ||
439a97cc | 1033 | trace_usb_ehci_mmio_readl(addr, addr2str(addr), val); |
94527ead GH |
1034 | return val; |
1035 | } | |
1036 | ||
1037 | static void ehci_mem_writeb(void *ptr, target_phys_addr_t addr, uint32_t val) | |
1038 | { | |
1039 | fprintf(stderr, "EHCI doesn't handle byte writes to MMIO\n"); | |
1040 | exit(1); | |
1041 | } | |
1042 | ||
1043 | static void ehci_mem_writew(void *ptr, target_phys_addr_t addr, uint32_t val) | |
1044 | { | |
1045 | fprintf(stderr, "EHCI doesn't handle 16-bit writes to MMIO\n"); | |
1046 | exit(1); | |
1047 | } | |
1048 | ||
a0a3167a HG |
1049 | static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner) |
1050 | { | |
1051 | USBDevice *dev = s->ports[port].dev; | |
1052 | uint32_t *portsc = &s->portsc[port]; | |
1053 | uint32_t orig; | |
1054 | ||
1055 | if (s->companion_ports[port] == NULL) | |
1056 | return; | |
1057 | ||
1058 | owner = owner & PORTSC_POWNER; | |
1059 | orig = *portsc & PORTSC_POWNER; | |
1060 | ||
1061 | if (!(owner ^ orig)) { | |
1062 | return; | |
1063 | } | |
1064 | ||
891fb2cd GH |
1065 | if (dev && dev->attached) { |
1066 | usb_detach(&s->ports[port]); | |
a0a3167a HG |
1067 | } |
1068 | ||
1069 | *portsc &= ~PORTSC_POWNER; | |
1070 | *portsc |= owner; | |
1071 | ||
891fb2cd GH |
1072 | if (dev && dev->attached) { |
1073 | usb_attach(&s->ports[port]); | |
a0a3167a HG |
1074 | } |
1075 | } | |
1076 | ||
94527ead GH |
1077 | static void handle_port_status_write(EHCIState *s, int port, uint32_t val) |
1078 | { | |
1079 | uint32_t *portsc = &s->portsc[port]; | |
94527ead GH |
1080 | USBDevice *dev = s->ports[port].dev; |
1081 | ||
fbd97532 HG |
1082 | /* Clear rwc bits */ |
1083 | *portsc &= ~(val & PORTSC_RWC_MASK); | |
1084 | /* The guest may clear, but not set the PED bit */ | |
1085 | *portsc &= val | ~PORTSC_PED; | |
a0a3167a HG |
1086 | /* POWNER is masked out by RO_MASK as it is RO when we've no companion */ |
1087 | handle_port_owner_write(s, port, val); | |
1088 | /* And finally apply RO_MASK */ | |
94527ead GH |
1089 | val &= PORTSC_RO_MASK; |
1090 | ||
94527ead | 1091 | if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) { |
dcbd0b5c | 1092 | trace_usb_ehci_port_reset(port, 1); |
94527ead GH |
1093 | } |
1094 | ||
1095 | if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) { | |
dcbd0b5c | 1096 | trace_usb_ehci_port_reset(port, 0); |
891fb2cd | 1097 | if (dev && dev->attached) { |
d28f4e2d | 1098 | usb_port_reset(&s->ports[port]); |
94527ead GH |
1099 | *portsc &= ~PORTSC_CSC; |
1100 | } | |
1101 | ||
fbd97532 HG |
1102 | /* |
1103 | * Table 2.16 Set the enable bit(and enable bit change) to indicate | |
94527ead | 1104 | * to SW that this port has a high speed device attached |
94527ead | 1105 | */ |
891fb2cd | 1106 | if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) { |
fbd97532 HG |
1107 | val |= PORTSC_PED; |
1108 | } | |
94527ead GH |
1109 | } |
1110 | ||
1111 | *portsc &= ~PORTSC_RO_MASK; | |
1112 | *portsc |= val; | |
94527ead GH |
1113 | } |
1114 | ||
1115 | static void ehci_mem_writel(void *ptr, target_phys_addr_t addr, uint32_t val) | |
1116 | { | |
1117 | EHCIState *s = ptr; | |
c4f8e211 GH |
1118 | uint32_t *mmio = (uint32_t *)(&s->mmio[addr]); |
1119 | uint32_t old = *mmio; | |
94527ead | 1120 | int i; |
439a97cc | 1121 | |
c4f8e211 | 1122 | trace_usb_ehci_mmio_writel(addr, addr2str(addr), val); |
94527ead GH |
1123 | |
1124 | /* Only aligned reads are allowed on OHCI */ | |
1125 | if (addr & 3) { | |
1126 | fprintf(stderr, "usb-ehci: Mis-aligned write to addr 0x" | |
1127 | TARGET_FMT_plx "\n", addr); | |
1128 | return; | |
1129 | } | |
1130 | ||
1131 | if (addr >= PORTSC && addr < PORTSC + 4 * NB_PORTS) { | |
1132 | handle_port_status_write(s, (addr-PORTSC)/4, val); | |
c4f8e211 | 1133 | trace_usb_ehci_mmio_change(addr, addr2str(addr), *mmio, old); |
94527ead GH |
1134 | return; |
1135 | } | |
1136 | ||
1137 | if (addr < OPREGBASE) { | |
1138 | fprintf(stderr, "usb-ehci: write attempt to read-only register" | |
1139 | TARGET_FMT_plx "\n", addr); | |
1140 | return; | |
1141 | } | |
1142 | ||
1143 | ||
1144 | /* Do any register specific pre-write processing here. */ | |
94527ead GH |
1145 | switch(addr) { |
1146 | case USBCMD: | |
7046530c GH |
1147 | if (val & USBCMD_HCRESET) { |
1148 | ehci_reset(s); | |
1149 | val = s->usbcmd; | |
1150 | break; | |
1151 | } | |
1152 | ||
daf25307 GH |
1153 | if (((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & val) != |
1154 | ((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & s->usbcmd)) { | |
3a215326 | 1155 | if (s->pstate == EST_INACTIVE) { |
daf25307 GH |
1156 | SET_LAST_RUN_CLOCK(s); |
1157 | } | |
1158 | ehci_update_halt(s); | |
3a215326 GH |
1159 | s->async_stepdown = 0; |
1160 | qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock)); | |
94527ead GH |
1161 | } |
1162 | ||
94527ead GH |
1163 | /* not supporting dynamic frame list size at the moment */ |
1164 | if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) { | |
1165 | fprintf(stderr, "attempt to set frame list size -- value %d\n", | |
1166 | val & USBCMD_FLS); | |
1167 | val &= ~USBCMD_FLS; | |
1168 | } | |
94527ead GH |
1169 | break; |
1170 | ||
94527ead | 1171 | case USBSTS: |
a31f0531 JM |
1172 | val &= USBSTS_RO_MASK; // bits 6 through 31 are RO |
1173 | ehci_clear_usbsts(s, val); // bits 0 through 5 are R/WC | |
439a97cc | 1174 | val = s->usbsts; |
94527ead GH |
1175 | ehci_set_interrupt(s, 0); |
1176 | break; | |
1177 | ||
94527ead GH |
1178 | case USBINTR: |
1179 | val &= USBINTR_MASK; | |
94527ead GH |
1180 | break; |
1181 | ||
8a771f77 HG |
1182 | case FRINDEX: |
1183 | val &= 0x00003ff8; /* frindex is 14bits and always a multiple of 8 */ | |
1184 | break; | |
1185 | ||
94527ead | 1186 | case CONFIGFLAG: |
94527ead GH |
1187 | val &= 0x1; |
1188 | if (val) { | |
1189 | for(i = 0; i < NB_PORTS; i++) | |
a0a3167a | 1190 | handle_port_owner_write(s, i, 0); |
94527ead GH |
1191 | } |
1192 | break; | |
1193 | ||
1194 | case PERIODICLISTBASE: | |
ec807d12 | 1195 | if (ehci_periodic_enabled(s)) { |
94527ead GH |
1196 | fprintf(stderr, |
1197 | "ehci: PERIODIC list base register set while periodic schedule\n" | |
1198 | " is enabled and HC is enabled\n"); | |
1199 | } | |
94527ead GH |
1200 | break; |
1201 | ||
1202 | case ASYNCLISTADDR: | |
ec807d12 | 1203 | if (ehci_async_enabled(s)) { |
94527ead GH |
1204 | fprintf(stderr, |
1205 | "ehci: ASYNC list address register set while async schedule\n" | |
1206 | " is enabled and HC is enabled\n"); | |
1207 | } | |
94527ead GH |
1208 | break; |
1209 | } | |
1210 | ||
c4f8e211 GH |
1211 | *mmio = val; |
1212 | trace_usb_ehci_mmio_change(addr, addr2str(addr), *mmio, old); | |
94527ead GH |
1213 | } |
1214 | ||
1215 | ||
1216 | // TODO : Put in common header file, duplication from usb-ohci.c | |
1217 | ||
1218 | /* Get an array of dwords from main memory */ | |
68d55358 DG |
1219 | static inline int get_dwords(EHCIState *ehci, uint32_t addr, |
1220 | uint32_t *buf, int num) | |
94527ead GH |
1221 | { |
1222 | int i; | |
1223 | ||
1224 | for(i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { | |
4bf80119 | 1225 | pci_dma_read(&ehci->dev, addr, buf, sizeof(*buf)); |
94527ead GH |
1226 | *buf = le32_to_cpu(*buf); |
1227 | } | |
1228 | ||
1229 | return 1; | |
1230 | } | |
1231 | ||
1232 | /* Put an array of dwords in to main memory */ | |
68d55358 DG |
1233 | static inline int put_dwords(EHCIState *ehci, uint32_t addr, |
1234 | uint32_t *buf, int num) | |
94527ead GH |
1235 | { |
1236 | int i; | |
1237 | ||
1238 | for(i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { | |
1239 | uint32_t tmp = cpu_to_le32(*buf); | |
4bf80119 | 1240 | pci_dma_write(&ehci->dev, addr, &tmp, sizeof(tmp)); |
94527ead GH |
1241 | } |
1242 | ||
1243 | return 1; | |
1244 | } | |
1245 | ||
1246 | // 4.10.2 | |
1247 | ||
0122f472 | 1248 | static int ehci_qh_do_overlay(EHCIQueue *q) |
94527ead | 1249 | { |
eb36a88e | 1250 | EHCIPacket *p = QTAILQ_FIRST(&q->packets); |
94527ead GH |
1251 | int i; |
1252 | int dtoggle; | |
1253 | int ping; | |
1254 | int eps; | |
1255 | int reload; | |
1256 | ||
eb36a88e GH |
1257 | assert(p != NULL); |
1258 | assert(p->qtdaddr == q->qtdaddr); | |
1259 | ||
94527ead GH |
1260 | // remember values in fields to preserve in qh after overlay |
1261 | ||
0122f472 GH |
1262 | dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE; |
1263 | ping = q->qh.token & QTD_TOKEN_PING; | |
94527ead | 1264 | |
eb36a88e GH |
1265 | q->qh.current_qtd = p->qtdaddr; |
1266 | q->qh.next_qtd = p->qtd.next; | |
1267 | q->qh.altnext_qtd = p->qtd.altnext; | |
1268 | q->qh.token = p->qtd.token; | |
94527ead GH |
1269 | |
1270 | ||
0122f472 | 1271 | eps = get_field(q->qh.epchar, QH_EPCHAR_EPS); |
94527ead | 1272 | if (eps == EHCI_QH_EPS_HIGH) { |
0122f472 GH |
1273 | q->qh.token &= ~QTD_TOKEN_PING; |
1274 | q->qh.token |= ping; | |
94527ead GH |
1275 | } |
1276 | ||
0122f472 GH |
1277 | reload = get_field(q->qh.epchar, QH_EPCHAR_RL); |
1278 | set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT); | |
94527ead GH |
1279 | |
1280 | for (i = 0; i < 5; i++) { | |
eb36a88e | 1281 | q->qh.bufptr[i] = p->qtd.bufptr[i]; |
94527ead GH |
1282 | } |
1283 | ||
0122f472 | 1284 | if (!(q->qh.epchar & QH_EPCHAR_DTC)) { |
94527ead | 1285 | // preserve QH DT bit |
0122f472 GH |
1286 | q->qh.token &= ~QTD_TOKEN_DTOGGLE; |
1287 | q->qh.token |= dtoggle; | |
94527ead GH |
1288 | } |
1289 | ||
0122f472 GH |
1290 | q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK; |
1291 | q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK; | |
94527ead | 1292 | |
68d55358 DG |
1293 | put_dwords(q->ehci, NLPTR_GET(q->qhaddr), (uint32_t *) &q->qh, |
1294 | sizeof(EHCIqh) >> 2); | |
94527ead GH |
1295 | |
1296 | return 0; | |
1297 | } | |
1298 | ||
eb36a88e | 1299 | static int ehci_init_transfer(EHCIPacket *p) |
94527ead | 1300 | { |
0ce668bc | 1301 | uint32_t cpage, offset, bytes, plen; |
68d55358 | 1302 | dma_addr_t page; |
94527ead | 1303 | |
eb36a88e GH |
1304 | cpage = get_field(p->qtd.token, QTD_TOKEN_CPAGE); |
1305 | bytes = get_field(p->qtd.token, QTD_TOKEN_TBYTES); | |
1306 | offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK; | |
1307 | pci_dma_sglist_init(&p->sgl, &p->queue->ehci->dev, 5); | |
94527ead | 1308 | |
0ce668bc GH |
1309 | while (bytes > 0) { |
1310 | if (cpage > 4) { | |
1311 | fprintf(stderr, "cpage out of range (%d)\n", cpage); | |
1312 | return USB_RET_PROCERR; | |
1313 | } | |
94527ead | 1314 | |
eb36a88e | 1315 | page = p->qtd.bufptr[cpage] & QTD_BUFPTR_MASK; |
0ce668bc GH |
1316 | page += offset; |
1317 | plen = bytes; | |
1318 | if (plen > 4096 - offset) { | |
1319 | plen = 4096 - offset; | |
1320 | offset = 0; | |
1321 | cpage++; | |
94527ead GH |
1322 | } |
1323 | ||
eb36a88e | 1324 | qemu_sglist_add(&p->sgl, page, plen); |
0ce668bc GH |
1325 | bytes -= plen; |
1326 | } | |
1327 | return 0; | |
1328 | } | |
94527ead | 1329 | |
0ce668bc GH |
1330 | static void ehci_finish_transfer(EHCIQueue *q, int status) |
1331 | { | |
1332 | uint32_t cpage, offset; | |
94527ead | 1333 | |
0ce668bc GH |
1334 | if (status > 0) { |
1335 | /* update cpage & offset */ | |
1336 | cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE); | |
1337 | offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK; | |
94527ead | 1338 | |
0ce668bc GH |
1339 | offset += status; |
1340 | cpage += offset >> QTD_BUFPTR_SH; | |
1341 | offset &= ~QTD_BUFPTR_MASK; | |
94527ead | 1342 | |
0ce668bc GH |
1343 | set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE); |
1344 | q->qh.bufptr[0] &= QTD_BUFPTR_MASK; | |
1345 | q->qh.bufptr[0] |= offset; | |
1346 | } | |
94527ead GH |
1347 | } |
1348 | ||
d47e59b8 | 1349 | static void ehci_async_complete_packet(USBPort *port, USBPacket *packet) |
94527ead | 1350 | { |
eb36a88e | 1351 | EHCIPacket *p; |
a0a3167a HG |
1352 | EHCIState *s = port->opaque; |
1353 | uint32_t portsc = s->portsc[port->index]; | |
1354 | ||
1355 | if (portsc & PORTSC_POWNER) { | |
1356 | USBPort *companion = s->companion_ports[port->index]; | |
1357 | companion->ops->complete(companion, packet); | |
1358 | return; | |
1359 | } | |
94527ead | 1360 | |
eb36a88e GH |
1361 | p = container_of(packet, EHCIPacket, packet); |
1362 | trace_usb_ehci_packet_action(p->queue, p, "wakeup"); | |
1363 | assert(p->async == EHCI_ASYNC_INFLIGHT); | |
1364 | p->async = EHCI_ASYNC_FINISHED; | |
1365 | p->usb_status = packet->result; | |
ae710b99 GH |
1366 | |
1367 | if (p->queue->async) { | |
1368 | qemu_bh_schedule(p->queue->ehci->async_bh); | |
1369 | } | |
94527ead GH |
1370 | } |
1371 | ||
0122f472 | 1372 | static void ehci_execute_complete(EHCIQueue *q) |
94527ead | 1373 | { |
eb36a88e GH |
1374 | EHCIPacket *p = QTAILQ_FIRST(&q->packets); |
1375 | ||
1376 | assert(p != NULL); | |
1377 | assert(p->qtdaddr == q->qtdaddr); | |
1378 | assert(p->async != EHCI_ASYNC_INFLIGHT); | |
1379 | p->async = EHCI_ASYNC_NONE; | |
94527ead GH |
1380 | |
1381 | DPRINTF("execute_complete: qhaddr 0x%x, next %x, qtdaddr 0x%x, status %d\n", | |
0122f472 | 1382 | q->qhaddr, q->qh.next, q->qtdaddr, q->usb_status); |
94527ead | 1383 | |
eb36a88e GH |
1384 | if (p->usb_status < 0) { |
1385 | switch (p->usb_status) { | |
d61000a8 | 1386 | case USB_RET_IOERROR: |
94527ead | 1387 | case USB_RET_NODEV: |
d2bd525f | 1388 | q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR); |
dd54cfe0 | 1389 | set_field(&q->qh.token, 0, QTD_TOKEN_CERR); |
d2bd525f | 1390 | ehci_record_interrupt(q->ehci, USBSTS_ERRINT); |
94527ead GH |
1391 | break; |
1392 | case USB_RET_STALL: | |
0122f472 GH |
1393 | q->qh.token |= QTD_TOKEN_HALT; |
1394 | ehci_record_interrupt(q->ehci, USBSTS_ERRINT); | |
94527ead GH |
1395 | break; |
1396 | case USB_RET_NAK: | |
553a6a59 HG |
1397 | set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT); |
1398 | return; /* We're not done yet with this transaction */ | |
94527ead | 1399 | case USB_RET_BABBLE: |
d2bd525f | 1400 | q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE); |
0122f472 | 1401 | ehci_record_interrupt(q->ehci, USBSTS_ERRINT); |
94527ead GH |
1402 | break; |
1403 | default: | |
0122f472 | 1404 | /* should not be triggerable */ |
eb36a88e | 1405 | fprintf(stderr, "USB invalid response %d\n", p->usb_status); |
0122f472 | 1406 | assert(0); |
94527ead GH |
1407 | break; |
1408 | } | |
eb36a88e GH |
1409 | } else if ((p->usb_status > p->tbytes) && (p->pid == USB_TOKEN_IN)) { |
1410 | p->usb_status = USB_RET_BABBLE; | |
dd54cfe0 HG |
1411 | q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE); |
1412 | ehci_record_interrupt(q->ehci, USBSTS_ERRINT); | |
94527ead | 1413 | } else { |
94527ead GH |
1414 | // TODO check 4.12 for splits |
1415 | ||
eb36a88e GH |
1416 | if (p->tbytes && p->pid == USB_TOKEN_IN) { |
1417 | p->tbytes -= p->usb_status; | |
94527ead | 1418 | } else { |
eb36a88e | 1419 | p->tbytes = 0; |
94527ead GH |
1420 | } |
1421 | ||
eb36a88e GH |
1422 | DPRINTF("updating tbytes to %d\n", p->tbytes); |
1423 | set_field(&q->qh.token, p->tbytes, QTD_TOKEN_TBYTES); | |
94527ead | 1424 | } |
eb36a88e GH |
1425 | ehci_finish_transfer(q, p->usb_status); |
1426 | qemu_sglist_destroy(&p->sgl); | |
1427 | usb_packet_unmap(&p->packet); | |
94527ead | 1428 | |
0122f472 GH |
1429 | q->qh.token ^= QTD_TOKEN_DTOGGLE; |
1430 | q->qh.token &= ~QTD_TOKEN_ACTIVE; | |
94527ead | 1431 | |
553a6a59 | 1432 | if (q->qh.token & QTD_TOKEN_IOC) { |
0122f472 | 1433 | ehci_record_interrupt(q->ehci, USBSTS_INT); |
94527ead | 1434 | } |
94527ead GH |
1435 | } |
1436 | ||
1437 | // 4.10.3 | |
1438 | ||
773dc9cd | 1439 | static int ehci_execute(EHCIPacket *p, const char *action) |
94527ead | 1440 | { |
079d0b7f | 1441 | USBEndpoint *ep; |
94527ead | 1442 | int ret; |
94527ead | 1443 | int endp; |
94527ead | 1444 | |
4224558f GH |
1445 | if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) { |
1446 | fprintf(stderr, "Attempting to execute inactive qtd\n"); | |
94527ead GH |
1447 | return USB_RET_PROCERR; |
1448 | } | |
1449 | ||
4224558f | 1450 | p->tbytes = (p->qtd.token & QTD_TOKEN_TBYTES_MASK) >> QTD_TOKEN_TBYTES_SH; |
eb36a88e | 1451 | if (p->tbytes > BUFF_SIZE) { |
94527ead GH |
1452 | fprintf(stderr, "Request for more bytes than allowed\n"); |
1453 | return USB_RET_PROCERR; | |
1454 | } | |
1455 | ||
4224558f | 1456 | p->pid = (p->qtd.token & QTD_TOKEN_PID_MASK) >> QTD_TOKEN_PID_SH; |
eb36a88e GH |
1457 | switch (p->pid) { |
1458 | case 0: | |
1459 | p->pid = USB_TOKEN_OUT; | |
1460 | break; | |
1461 | case 1: | |
1462 | p->pid = USB_TOKEN_IN; | |
1463 | break; | |
1464 | case 2: | |
1465 | p->pid = USB_TOKEN_SETUP; | |
1466 | break; | |
1467 | default: | |
1468 | fprintf(stderr, "bad token\n"); | |
1469 | break; | |
94527ead GH |
1470 | } |
1471 | ||
eb36a88e | 1472 | if (ehci_init_transfer(p) != 0) { |
94527ead GH |
1473 | return USB_RET_PROCERR; |
1474 | } | |
1475 | ||
4224558f | 1476 | endp = get_field(p->queue->qh.epchar, QH_EPCHAR_EP); |
e59928b3 | 1477 | ep = usb_ep_get(p->queue->dev, p->pid, endp); |
94527ead | 1478 | |
eb36a88e GH |
1479 | usb_packet_setup(&p->packet, p->pid, ep); |
1480 | usb_packet_map(&p->packet, &p->sgl); | |
0ce668bc | 1481 | |
773dc9cd | 1482 | trace_usb_ehci_packet_action(p->queue, p, action); |
e59928b3 | 1483 | ret = usb_handle_packet(p->queue->dev, &p->packet); |
828143c6 GH |
1484 | DPRINTF("submit: qh %x next %x qtd %x pid %x len %zd " |
1485 | "(total %d) endp %x ret %d\n", | |
1486 | q->qhaddr, q->qh.next, q->qtdaddr, q->pid, | |
1487 | q->packet.iov.size, q->tbytes, endp, ret); | |
94527ead GH |
1488 | |
1489 | if (ret > BUFF_SIZE) { | |
1490 | fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n"); | |
1491 | return USB_RET_PROCERR; | |
1492 | } | |
1493 | ||
94527ead GH |
1494 | return ret; |
1495 | } | |
1496 | ||
1497 | /* 4.7.2 | |
1498 | */ | |
1499 | ||
1500 | static int ehci_process_itd(EHCIState *ehci, | |
1501 | EHCIitd *itd) | |
1502 | { | |
94527ead | 1503 | USBDevice *dev; |
079d0b7f | 1504 | USBEndpoint *ep; |
94527ead | 1505 | int ret; |
828143c6 | 1506 | uint32_t i, len, pid, dir, devaddr, endp; |
e654887f | 1507 | uint32_t pg, off, ptr1, ptr2, max, mult; |
94527ead GH |
1508 | |
1509 | dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION); | |
e654887f | 1510 | devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR); |
94527ead | 1511 | endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP); |
e654887f GH |
1512 | max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT); |
1513 | mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT); | |
94527ead GH |
1514 | |
1515 | for(i = 0; i < 8; i++) { | |
1516 | if (itd->transact[i] & ITD_XACT_ACTIVE) { | |
e654887f GH |
1517 | pg = get_field(itd->transact[i], ITD_XACT_PGSEL); |
1518 | off = itd->transact[i] & ITD_XACT_OFFSET_MASK; | |
1519 | ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK); | |
1520 | ptr2 = (itd->bufptr[pg+1] & ITD_BUFPTR_MASK); | |
1521 | len = get_field(itd->transact[i], ITD_XACT_LENGTH); | |
1522 | ||
1523 | if (len > max * mult) { | |
1524 | len = max * mult; | |
1525 | } | |
94527ead GH |
1526 | |
1527 | if (len > BUFF_SIZE) { | |
1528 | return USB_RET_PROCERR; | |
1529 | } | |
1530 | ||
68d55358 | 1531 | pci_dma_sglist_init(&ehci->isgl, &ehci->dev, 2); |
e654887f GH |
1532 | if (off + len > 4096) { |
1533 | /* transfer crosses page border */ | |
0ce668bc GH |
1534 | uint32_t len2 = off + len - 4096; |
1535 | uint32_t len1 = len - len2; | |
1536 | qemu_sglist_add(&ehci->isgl, ptr1 + off, len1); | |
1537 | qemu_sglist_add(&ehci->isgl, ptr2, len2); | |
e654887f | 1538 | } else { |
0ce668bc | 1539 | qemu_sglist_add(&ehci->isgl, ptr1 + off, len); |
e654887f | 1540 | } |
94527ead | 1541 | |
0ce668bc | 1542 | pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT; |
94527ead | 1543 | |
079d0b7f GH |
1544 | dev = ehci_find_device(ehci, devaddr); |
1545 | ep = usb_ep_get(dev, pid, endp); | |
aa0568ff GH |
1546 | if (ep->type == USB_ENDPOINT_XFER_ISOC) { |
1547 | usb_packet_setup(&ehci->ipacket, pid, ep); | |
1548 | usb_packet_map(&ehci->ipacket, &ehci->isgl); | |
1549 | ret = usb_handle_packet(dev, &ehci->ipacket); | |
1550 | assert(ret != USB_RET_ASYNC); | |
1551 | usb_packet_unmap(&ehci->ipacket); | |
1552 | } else { | |
1553 | DPRINTF("ISOCH: attempt to addess non-iso endpoint\n"); | |
1554 | ret = USB_RET_NAK; | |
1555 | } | |
0ce668bc GH |
1556 | qemu_sglist_destroy(&ehci->isgl); |
1557 | ||
5eafd438 | 1558 | if (ret < 0) { |
df787185 HG |
1559 | switch (ret) { |
1560 | default: | |
1561 | fprintf(stderr, "Unexpected iso usb result: %d\n", ret); | |
1562 | /* Fall through */ | |
d61000a8 | 1563 | case USB_RET_IOERROR: |
df787185 HG |
1564 | case USB_RET_NODEV: |
1565 | /* 3.3.2: XACTERR is only allowed on IN transactions */ | |
1566 | if (dir) { | |
1567 | itd->transact[i] |= ITD_XACT_XACTERR; | |
1568 | ehci_record_interrupt(ehci, USBSTS_ERRINT); | |
1569 | } | |
1570 | break; | |
1571 | case USB_RET_BABBLE: | |
1572 | itd->transact[i] |= ITD_XACT_BABBLE; | |
1573 | ehci_record_interrupt(ehci, USBSTS_ERRINT); | |
1574 | break; | |
5eafd438 HG |
1575 | case USB_RET_NAK: |
1576 | /* no data for us, so do a zero-length transfer */ | |
1577 | ret = 0; | |
1578 | break; | |
1579 | } | |
1580 | } | |
1581 | if (ret >= 0) { | |
1582 | if (!dir) { | |
1583 | /* OUT */ | |
1584 | set_field(&itd->transact[i], len - ret, ITD_XACT_LENGTH); | |
1585 | } else { | |
1586 | /* IN */ | |
1587 | set_field(&itd->transact[i], ret, ITD_XACT_LENGTH); | |
94527ead GH |
1588 | } |
1589 | } | |
df787185 HG |
1590 | if (itd->transact[i] & ITD_XACT_IOC) { |
1591 | ehci_record_interrupt(ehci, USBSTS_INT); | |
1592 | } | |
e654887f | 1593 | itd->transact[i] &= ~ITD_XACT_ACTIVE; |
94527ead GH |
1594 | } |
1595 | } | |
1596 | return 0; | |
1597 | } | |
1598 | ||
cd665715 GH |
1599 | |
1600 | /* | |
1601 | * Write the qh back to guest physical memory. This step isn't | |
1602 | * in the EHCI spec but we need to do it since we don't share | |
1603 | * physical memory with our guest VM. | |
1604 | * | |
1605 | * The first three dwords are read-only for the EHCI, so skip them | |
1606 | * when writing back the qh. | |
1607 | */ | |
1608 | static void ehci_flush_qh(EHCIQueue *q) | |
1609 | { | |
1610 | uint32_t *qh = (uint32_t *) &q->qh; | |
1611 | uint32_t dwords = sizeof(EHCIqh) >> 2; | |
1612 | uint32_t addr = NLPTR_GET(q->qhaddr); | |
1613 | ||
1614 | put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3); | |
1615 | } | |
1616 | ||
94527ead GH |
1617 | /* This state is the entry point for asynchronous schedule |
1618 | * processing. Entry here consitutes a EHCI start event state (4.8.5) | |
1619 | */ | |
26d53979 | 1620 | static int ehci_state_waitlisthead(EHCIState *ehci, int async) |
94527ead | 1621 | { |
0122f472 | 1622 | EHCIqh qh; |
94527ead GH |
1623 | int i = 0; |
1624 | int again = 0; | |
1625 | uint32_t entry = ehci->asynclistaddr; | |
1626 | ||
1627 | /* set reclamation flag at start event (4.8.6) */ | |
1628 | if (async) { | |
439a97cc | 1629 | ehci_set_usbsts(ehci, USBSTS_REC); |
94527ead GH |
1630 | } |
1631 | ||
4be23939 | 1632 | ehci_queues_rip_unused(ehci, async, 0); |
8ac6d699 | 1633 | |
94527ead GH |
1634 | /* Find the head of the list (4.9.1.1) */ |
1635 | for(i = 0; i < MAX_QH; i++) { | |
68d55358 DG |
1636 | get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh, |
1637 | sizeof(EHCIqh) >> 2); | |
8ac6d699 | 1638 | ehci_trace_qh(NULL, NLPTR_GET(entry), &qh); |
94527ead | 1639 | |
0122f472 | 1640 | if (qh.epchar & QH_EPCHAR_H) { |
94527ead GH |
1641 | if (async) { |
1642 | entry |= (NLPTR_TYPE_QH << 1); | |
1643 | } | |
1644 | ||
0122f472 | 1645 | ehci_set_fetch_addr(ehci, async, entry); |
26d53979 | 1646 | ehci_set_state(ehci, async, EST_FETCHENTRY); |
94527ead GH |
1647 | again = 1; |
1648 | goto out; | |
1649 | } | |
1650 | ||
0122f472 | 1651 | entry = qh.next; |
94527ead | 1652 | if (entry == ehci->asynclistaddr) { |
94527ead GH |
1653 | break; |
1654 | } | |
1655 | } | |
1656 | ||
1657 | /* no head found for list. */ | |
1658 | ||
26d53979 | 1659 | ehci_set_state(ehci, async, EST_ACTIVE); |
94527ead GH |
1660 | |
1661 | out: | |
1662 | return again; | |
1663 | } | |
1664 | ||
1665 | ||
1666 | /* This state is the entry point for periodic schedule processing as | |
1667 | * well as being a continuation state for async processing. | |
1668 | */ | |
26d53979 | 1669 | static int ehci_state_fetchentry(EHCIState *ehci, int async) |
94527ead GH |
1670 | { |
1671 | int again = 0; | |
0122f472 | 1672 | uint32_t entry = ehci_get_fetch_addr(ehci, async); |
94527ead | 1673 | |
2a5ff735 | 1674 | if (NLPTR_TBIT(entry)) { |
26d53979 | 1675 | ehci_set_state(ehci, async, EST_ACTIVE); |
94527ead GH |
1676 | goto out; |
1677 | } | |
1678 | ||
1679 | /* section 4.8, only QH in async schedule */ | |
1680 | if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) { | |
1681 | fprintf(stderr, "non queue head request in async schedule\n"); | |
1682 | return -1; | |
1683 | } | |
1684 | ||
1685 | switch (NLPTR_TYPE_GET(entry)) { | |
1686 | case NLPTR_TYPE_QH: | |
26d53979 | 1687 | ehci_set_state(ehci, async, EST_FETCHQH); |
94527ead GH |
1688 | again = 1; |
1689 | break; | |
1690 | ||
1691 | case NLPTR_TYPE_ITD: | |
26d53979 | 1692 | ehci_set_state(ehci, async, EST_FETCHITD); |
94527ead GH |
1693 | again = 1; |
1694 | break; | |
1695 | ||
2fe80192 GH |
1696 | case NLPTR_TYPE_STITD: |
1697 | ehci_set_state(ehci, async, EST_FETCHSITD); | |
1698 | again = 1; | |
1699 | break; | |
1700 | ||
94527ead | 1701 | default: |
2fe80192 | 1702 | /* TODO: handle FSTN type */ |
94527ead GH |
1703 | fprintf(stderr, "FETCHENTRY: entry at %X is of type %d " |
1704 | "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry)); | |
1705 | return -1; | |
1706 | } | |
1707 | ||
1708 | out: | |
1709 | return again; | |
1710 | } | |
1711 | ||
0122f472 | 1712 | static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async) |
94527ead | 1713 | { |
eb36a88e | 1714 | EHCIPacket *p; |
e59928b3 | 1715 | uint32_t entry, devaddr; |
0122f472 | 1716 | EHCIQueue *q; |
94527ead | 1717 | |
0122f472 | 1718 | entry = ehci_get_fetch_addr(ehci, async); |
df5d5c5c | 1719 | q = ehci_find_queue_by_qh(ehci, entry, async); |
8ac6d699 | 1720 | if (NULL == q) { |
8f6d5e26 | 1721 | q = ehci_alloc_queue(ehci, entry, async); |
8ac6d699 | 1722 | } |
eb36a88e | 1723 | p = QTAILQ_FIRST(&q->packets); |
8ac6d699 | 1724 | |
8f6d5e26 | 1725 | q->seen++; |
8ac6d699 GH |
1726 | if (q->seen > 1) { |
1727 | /* we are going in circles -- stop processing */ | |
1728 | ehci_set_state(ehci, async, EST_ACTIVE); | |
1729 | q = NULL; | |
1730 | goto out; | |
1731 | } | |
94527ead | 1732 | |
68d55358 DG |
1733 | get_dwords(ehci, NLPTR_GET(q->qhaddr), |
1734 | (uint32_t *) &q->qh, sizeof(EHCIqh) >> 2); | |
8ac6d699 GH |
1735 | ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &q->qh); |
1736 | ||
e59928b3 GH |
1737 | devaddr = get_field(q->qh.epchar, QH_EPCHAR_DEVADDR); |
1738 | if (q->dev != NULL && q->dev->addr != devaddr) { | |
1739 | if (!QTAILQ_EMPTY(&q->packets)) { | |
1740 | /* should not happen (guest bug) */ | |
1741 | while ((p = QTAILQ_FIRST(&q->packets)) != NULL) { | |
1742 | ehci_free_packet(p); | |
1743 | } | |
1744 | } | |
1745 | q->dev = NULL; | |
1746 | } | |
1747 | if (q->dev == NULL) { | |
1748 | q->dev = ehci_find_device(q->ehci, devaddr); | |
1749 | } | |
1750 | ||
eb36a88e | 1751 | if (p && p->async == EHCI_ASYNC_INFLIGHT) { |
8ac6d699 GH |
1752 | /* I/O still in progress -- skip queue */ |
1753 | ehci_set_state(ehci, async, EST_HORIZONTALQH); | |
1754 | goto out; | |
1755 | } | |
eb36a88e | 1756 | if (p && p->async == EHCI_ASYNC_FINISHED) { |
8ac6d699 | 1757 | /* I/O finished -- continue processing queue */ |
773dc9cd | 1758 | trace_usb_ehci_packet_action(p->queue, p, "complete"); |
8ac6d699 GH |
1759 | ehci_set_state(ehci, async, EST_EXECUTING); |
1760 | goto out; | |
1761 | } | |
0122f472 GH |
1762 | |
1763 | if (async && (q->qh.epchar & QH_EPCHAR_H)) { | |
94527ead GH |
1764 | |
1765 | /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */ | |
1766 | if (ehci->usbsts & USBSTS_REC) { | |
439a97cc | 1767 | ehci_clear_usbsts(ehci, USBSTS_REC); |
94527ead GH |
1768 | } else { |
1769 | DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset" | |
0122f472 | 1770 | " - done processing\n", q->qhaddr); |
26d53979 | 1771 | ehci_set_state(ehci, async, EST_ACTIVE); |
0122f472 | 1772 | q = NULL; |
94527ead GH |
1773 | goto out; |
1774 | } | |
1775 | } | |
1776 | ||
1777 | #if EHCI_DEBUG | |
0122f472 | 1778 | if (q->qhaddr != q->qh.next) { |
94527ead | 1779 | DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n", |
0122f472 GH |
1780 | q->qhaddr, |
1781 | q->qh.epchar & QH_EPCHAR_H, | |
1782 | q->qh.token & QTD_TOKEN_HALT, | |
1783 | q->qh.token & QTD_TOKEN_ACTIVE, | |
1784 | q->qh.next); | |
94527ead GH |
1785 | } |
1786 | #endif | |
1787 | ||
0122f472 | 1788 | if (q->qh.token & QTD_TOKEN_HALT) { |
26d53979 | 1789 | ehci_set_state(ehci, async, EST_HORIZONTALQH); |
94527ead | 1790 | |
2a5ff735 HG |
1791 | } else if ((q->qh.token & QTD_TOKEN_ACTIVE) && |
1792 | (NLPTR_TBIT(q->qh.current_qtd) == 0)) { | |
0122f472 | 1793 | q->qtdaddr = q->qh.current_qtd; |
26d53979 | 1794 | ehci_set_state(ehci, async, EST_FETCHQTD); |
94527ead GH |
1795 | |
1796 | } else { | |
1797 | /* EHCI spec version 1.0 Section 4.10.2 */ | |
26d53979 | 1798 | ehci_set_state(ehci, async, EST_ADVANCEQUEUE); |
94527ead GH |
1799 | } |
1800 | ||
1801 | out: | |
0122f472 | 1802 | return q; |
94527ead GH |
1803 | } |
1804 | ||
26d53979 | 1805 | static int ehci_state_fetchitd(EHCIState *ehci, int async) |
94527ead | 1806 | { |
0122f472 | 1807 | uint32_t entry; |
94527ead GH |
1808 | EHCIitd itd; |
1809 | ||
0122f472 GH |
1810 | assert(!async); |
1811 | entry = ehci_get_fetch_addr(ehci, async); | |
1812 | ||
68d55358 | 1813 | get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd, |
94527ead | 1814 | sizeof(EHCIitd) >> 2); |
0122f472 | 1815 | ehci_trace_itd(ehci, entry, &itd); |
94527ead GH |
1816 | |
1817 | if (ehci_process_itd(ehci, &itd) != 0) { | |
1818 | return -1; | |
1819 | } | |
1820 | ||
68d55358 DG |
1821 | put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd, |
1822 | sizeof(EHCIitd) >> 2); | |
0122f472 | 1823 | ehci_set_fetch_addr(ehci, async, itd.next); |
26d53979 | 1824 | ehci_set_state(ehci, async, EST_FETCHENTRY); |
94527ead GH |
1825 | |
1826 | return 1; | |
1827 | } | |
1828 | ||
2fe80192 GH |
1829 | static int ehci_state_fetchsitd(EHCIState *ehci, int async) |
1830 | { | |
1831 | uint32_t entry; | |
1832 | EHCIsitd sitd; | |
1833 | ||
1834 | assert(!async); | |
1835 | entry = ehci_get_fetch_addr(ehci, async); | |
1836 | ||
68d55358 | 1837 | get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd, |
2fe80192 GH |
1838 | sizeof(EHCIsitd) >> 2); |
1839 | ehci_trace_sitd(ehci, entry, &sitd); | |
1840 | ||
1841 | if (!(sitd.results & SITD_RESULTS_ACTIVE)) { | |
1842 | /* siTD is not active, nothing to do */; | |
1843 | } else { | |
1844 | /* TODO: split transfers are not implemented */ | |
1845 | fprintf(stderr, "WARNING: Skipping active siTD\n"); | |
1846 | } | |
1847 | ||
1848 | ehci_set_fetch_addr(ehci, async, sitd.next); | |
1849 | ehci_set_state(ehci, async, EST_FETCHENTRY); | |
1850 | return 1; | |
1851 | } | |
1852 | ||
94527ead | 1853 | /* Section 4.10.2 - paragraph 3 */ |
ae0138a8 | 1854 | static int ehci_state_advqueue(EHCIQueue *q) |
94527ead GH |
1855 | { |
1856 | #if 0 | |
1857 | /* TO-DO: 4.10.2 - paragraph 2 | |
1858 | * if I-bit is set to 1 and QH is not active | |
1859 | * go to horizontal QH | |
1860 | */ | |
1861 | if (I-bit set) { | |
26d53979 | 1862 | ehci_set_state(ehci, async, EST_HORIZONTALQH); |
94527ead GH |
1863 | goto out; |
1864 | } | |
1865 | #endif | |
1866 | ||
1867 | /* | |
1868 | * want data and alt-next qTD is valid | |
1869 | */ | |
0122f472 | 1870 | if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) && |
0122f472 GH |
1871 | (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) { |
1872 | q->qtdaddr = q->qh.altnext_qtd; | |
ae0138a8 | 1873 | ehci_set_state(q->ehci, q->async, EST_FETCHQTD); |
94527ead GH |
1874 | |
1875 | /* | |
1876 | * next qTD is valid | |
1877 | */ | |
2a5ff735 | 1878 | } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) { |
0122f472 | 1879 | q->qtdaddr = q->qh.next_qtd; |
ae0138a8 | 1880 | ehci_set_state(q->ehci, q->async, EST_FETCHQTD); |
94527ead GH |
1881 | |
1882 | /* | |
1883 | * no valid qTD, try next QH | |
1884 | */ | |
1885 | } else { | |
ae0138a8 | 1886 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
94527ead GH |
1887 | } |
1888 | ||
1889 | return 1; | |
1890 | } | |
1891 | ||
1892 | /* Section 4.10.2 - paragraph 4 */ | |
ae0138a8 | 1893 | static int ehci_state_fetchqtd(EHCIQueue *q) |
94527ead | 1894 | { |
eb36a88e GH |
1895 | EHCIqtd qtd; |
1896 | EHCIPacket *p; | |
94527ead GH |
1897 | int again = 0; |
1898 | ||
eb36a88e | 1899 | get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &qtd, |
68d55358 | 1900 | sizeof(EHCIqtd) >> 2); |
eb36a88e | 1901 | ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd); |
94527ead | 1902 | |
773dc9cd GH |
1903 | p = QTAILQ_FIRST(&q->packets); |
1904 | while (p != NULL && p->qtdaddr != q->qtdaddr) { | |
1905 | /* should not happen (guest bug) */ | |
1906 | ehci_free_packet(p); | |
1907 | p = QTAILQ_FIRST(&q->packets); | |
1908 | } | |
1909 | if (p != NULL) { | |
1910 | ehci_qh_do_overlay(q); | |
1911 | ehci_flush_qh(q); | |
1912 | if (p->async == EHCI_ASYNC_INFLIGHT) { | |
ae0138a8 | 1913 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
773dc9cd | 1914 | } else { |
ae0138a8 | 1915 | ehci_set_state(q->ehci, q->async, EST_EXECUTING); |
773dc9cd GH |
1916 | } |
1917 | again = 1; | |
1918 | } else if (qtd.token & QTD_TOKEN_ACTIVE) { | |
eb36a88e GH |
1919 | p = ehci_alloc_packet(q); |
1920 | p->qtdaddr = q->qtdaddr; | |
1921 | p->qtd = qtd; | |
ae0138a8 | 1922 | ehci_set_state(q->ehci, q->async, EST_EXECUTE); |
94527ead GH |
1923 | again = 1; |
1924 | } else { | |
ae0138a8 | 1925 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
94527ead GH |
1926 | again = 1; |
1927 | } | |
1928 | ||
1929 | return again; | |
1930 | } | |
1931 | ||
ae0138a8 | 1932 | static int ehci_state_horizqh(EHCIQueue *q) |
94527ead GH |
1933 | { |
1934 | int again = 0; | |
1935 | ||
ae0138a8 GH |
1936 | if (ehci_get_fetch_addr(q->ehci, q->async) != q->qh.next) { |
1937 | ehci_set_fetch_addr(q->ehci, q->async, q->qh.next); | |
1938 | ehci_set_state(q->ehci, q->async, EST_FETCHENTRY); | |
94527ead GH |
1939 | again = 1; |
1940 | } else { | |
ae0138a8 | 1941 | ehci_set_state(q->ehci, q->async, EST_ACTIVE); |
94527ead GH |
1942 | } |
1943 | ||
1944 | return again; | |
1945 | } | |
1946 | ||
ae0138a8 | 1947 | static void ehci_fill_queue(EHCIPacket *p) |
773dc9cd GH |
1948 | { |
1949 | EHCIQueue *q = p->queue; | |
1950 | EHCIqtd qtd = p->qtd; | |
1951 | uint32_t qtdaddr; | |
1952 | ||
1953 | for (;;) { | |
1954 | if (NLPTR_TBIT(qtd.altnext) == 0) { | |
1955 | break; | |
1956 | } | |
1957 | if (NLPTR_TBIT(qtd.next) != 0) { | |
1958 | break; | |
1959 | } | |
1960 | qtdaddr = qtd.next; | |
1961 | get_dwords(q->ehci, NLPTR_GET(qtdaddr), | |
1962 | (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2); | |
1963 | ehci_trace_qtd(q, NLPTR_GET(qtdaddr), &qtd); | |
1964 | if (!(qtd.token & QTD_TOKEN_ACTIVE)) { | |
1965 | break; | |
1966 | } | |
1967 | p = ehci_alloc_packet(q); | |
1968 | p->qtdaddr = qtdaddr; | |
1969 | p->qtd = qtd; | |
1970 | p->usb_status = ehci_execute(p, "queue"); | |
1971 | assert(p->usb_status = USB_RET_ASYNC); | |
1972 | p->async = EHCI_ASYNC_INFLIGHT; | |
1973 | } | |
1974 | } | |
1975 | ||
ae0138a8 | 1976 | static int ehci_state_execute(EHCIQueue *q) |
94527ead | 1977 | { |
eb36a88e | 1978 | EHCIPacket *p = QTAILQ_FIRST(&q->packets); |
94527ead | 1979 | int again = 0; |
94527ead | 1980 | |
eb36a88e GH |
1981 | assert(p != NULL); |
1982 | assert(p->qtdaddr == q->qtdaddr); | |
1983 | ||
0122f472 | 1984 | if (ehci_qh_do_overlay(q) != 0) { |
94527ead GH |
1985 | return -1; |
1986 | } | |
1987 | ||
94527ead GH |
1988 | // TODO verify enough time remains in the uframe as in 4.4.1.1 |
1989 | // TODO write back ptr to async list when done or out of time | |
1990 | // TODO Windows does not seem to ever set the MULT field | |
1991 | ||
ae0138a8 | 1992 | if (!q->async) { |
0122f472 | 1993 | int transactCtr = get_field(q->qh.epcap, QH_EPCAP_MULT); |
94527ead | 1994 | if (!transactCtr) { |
ae0138a8 | 1995 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
94527ead GH |
1996 | again = 1; |
1997 | goto out; | |
1998 | } | |
1999 | } | |
2000 | ||
ae0138a8 | 2001 | if (q->async) { |
0122f472 | 2002 | ehci_set_usbsts(q->ehci, USBSTS_REC); |
94527ead GH |
2003 | } |
2004 | ||
773dc9cd | 2005 | p->usb_status = ehci_execute(p, "process"); |
eb36a88e | 2006 | if (p->usb_status == USB_RET_PROCERR) { |
94527ead GH |
2007 | again = -1; |
2008 | goto out; | |
2009 | } | |
eb36a88e | 2010 | if (p->usb_status == USB_RET_ASYNC) { |
8ac6d699 | 2011 | ehci_flush_qh(q); |
773dc9cd | 2012 | trace_usb_ehci_packet_action(p->queue, p, "async"); |
eb36a88e | 2013 | p->async = EHCI_ASYNC_INFLIGHT; |
ae0138a8 | 2014 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
94527ead | 2015 | again = 1; |
ae0138a8 | 2016 | ehci_fill_queue(p); |
8ac6d699 | 2017 | goto out; |
94527ead GH |
2018 | } |
2019 | ||
ae0138a8 | 2020 | ehci_set_state(q->ehci, q->async, EST_EXECUTING); |
8ac6d699 GH |
2021 | again = 1; |
2022 | ||
94527ead GH |
2023 | out: |
2024 | return again; | |
2025 | } | |
2026 | ||
ae0138a8 | 2027 | static int ehci_state_executing(EHCIQueue *q) |
94527ead | 2028 | { |
eb36a88e | 2029 | EHCIPacket *p = QTAILQ_FIRST(&q->packets); |
94527ead | 2030 | int again = 0; |
94527ead | 2031 | |
eb36a88e GH |
2032 | assert(p != NULL); |
2033 | assert(p->qtdaddr == q->qtdaddr); | |
2034 | ||
0122f472 | 2035 | ehci_execute_complete(q); |
eb36a88e | 2036 | if (p->usb_status == USB_RET_ASYNC) { |
94527ead GH |
2037 | goto out; |
2038 | } | |
eb36a88e | 2039 | if (p->usb_status == USB_RET_PROCERR) { |
94527ead GH |
2040 | again = -1; |
2041 | goto out; | |
2042 | } | |
2043 | ||
2044 | // 4.10.3 | |
ae0138a8 | 2045 | if (!q->async) { |
0122f472 | 2046 | int transactCtr = get_field(q->qh.epcap, QH_EPCAP_MULT); |
94527ead | 2047 | transactCtr--; |
0122f472 | 2048 | set_field(&q->qh.epcap, transactCtr, QH_EPCAP_MULT); |
94527ead GH |
2049 | // 4.10.3, bottom of page 82, should exit this state when transaction |
2050 | // counter decrements to 0 | |
2051 | } | |
2052 | ||
94527ead | 2053 | /* 4.10.5 */ |
eb36a88e | 2054 | if (p->usb_status == USB_RET_NAK) { |
ae0138a8 | 2055 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
94527ead | 2056 | } else { |
ae0138a8 | 2057 | ehci_set_state(q->ehci, q->async, EST_WRITEBACK); |
94527ead GH |
2058 | } |
2059 | ||
2060 | again = 1; | |
2061 | ||
2062 | out: | |
8ac6d699 | 2063 | ehci_flush_qh(q); |
94527ead GH |
2064 | return again; |
2065 | } | |
2066 | ||
2067 | ||
ae0138a8 | 2068 | static int ehci_state_writeback(EHCIQueue *q) |
94527ead | 2069 | { |
eb36a88e | 2070 | EHCIPacket *p = QTAILQ_FIRST(&q->packets); |
94527ead GH |
2071 | int again = 0; |
2072 | ||
2073 | /* Write back the QTD from the QH area */ | |
eb36a88e GH |
2074 | assert(p != NULL); |
2075 | assert(p->qtdaddr == q->qtdaddr); | |
2076 | ||
2077 | ehci_trace_qtd(q, NLPTR_GET(p->qtdaddr), (EHCIqtd *) &q->qh.next_qtd); | |
2078 | put_dwords(q->ehci, NLPTR_GET(p->qtdaddr), (uint32_t *) &q->qh.next_qtd, | |
68d55358 | 2079 | sizeof(EHCIqtd) >> 2); |
eb36a88e | 2080 | ehci_free_packet(p); |
94527ead | 2081 | |
d2bd525f GH |
2082 | /* |
2083 | * EHCI specs say go horizontal here. | |
2084 | * | |
2085 | * We can also advance the queue here for performance reasons. We | |
2086 | * need to take care to only take that shortcut in case we've | |
2087 | * processed the qtd just written back without errors, i.e. halt | |
2088 | * bit is clear. | |
94527ead | 2089 | */ |
d2bd525f | 2090 | if (q->qh.token & QTD_TOKEN_HALT) { |
ae0138a8 | 2091 | ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); |
d2bd525f GH |
2092 | again = 1; |
2093 | } else { | |
ae0138a8 | 2094 | ehci_set_state(q->ehci, q->async, EST_ADVANCEQUEUE); |
94527ead | 2095 | again = 1; |
d2bd525f | 2096 | } |
94527ead GH |
2097 | return again; |
2098 | } | |
2099 | ||
2100 | /* | |
2101 | * This is the state machine that is common to both async and periodic | |
2102 | */ | |
2103 | ||
ae0138a8 | 2104 | static void ehci_advance_state(EHCIState *ehci, int async) |
94527ead | 2105 | { |
0122f472 | 2106 | EHCIQueue *q = NULL; |
94527ead | 2107 | int again; |
94527ead GH |
2108 | |
2109 | do { | |
26d53979 | 2110 | switch(ehci_get_state(ehci, async)) { |
94527ead | 2111 | case EST_WAITLISTHEAD: |
26d53979 | 2112 | again = ehci_state_waitlisthead(ehci, async); |
94527ead GH |
2113 | break; |
2114 | ||
2115 | case EST_FETCHENTRY: | |
26d53979 | 2116 | again = ehci_state_fetchentry(ehci, async); |
94527ead GH |
2117 | break; |
2118 | ||
2119 | case EST_FETCHQH: | |
0122f472 | 2120 | q = ehci_state_fetchqh(ehci, async); |
ae0138a8 GH |
2121 | if (q != NULL) { |
2122 | assert(q->async == async); | |
2123 | again = 1; | |
2124 | } else { | |
2125 | again = 0; | |
2126 | } | |
94527ead GH |
2127 | break; |
2128 | ||
2129 | case EST_FETCHITD: | |
26d53979 | 2130 | again = ehci_state_fetchitd(ehci, async); |
94527ead GH |
2131 | break; |
2132 | ||
2fe80192 GH |
2133 | case EST_FETCHSITD: |
2134 | again = ehci_state_fetchsitd(ehci, async); | |
2135 | break; | |
2136 | ||
94527ead | 2137 | case EST_ADVANCEQUEUE: |
ae0138a8 | 2138 | again = ehci_state_advqueue(q); |
94527ead GH |
2139 | break; |
2140 | ||
2141 | case EST_FETCHQTD: | |
ae0138a8 | 2142 | again = ehci_state_fetchqtd(q); |
94527ead GH |
2143 | break; |
2144 | ||
2145 | case EST_HORIZONTALQH: | |
ae0138a8 | 2146 | again = ehci_state_horizqh(q); |
94527ead GH |
2147 | break; |
2148 | ||
2149 | case EST_EXECUTE: | |
ae0138a8 | 2150 | again = ehci_state_execute(q); |
3a215326 GH |
2151 | if (async) { |
2152 | ehci->async_stepdown = 0; | |
2153 | } | |
94527ead GH |
2154 | break; |
2155 | ||
2156 | case EST_EXECUTING: | |
8ac6d699 | 2157 | assert(q != NULL); |
3a215326 GH |
2158 | if (async) { |
2159 | ehci->async_stepdown = 0; | |
2160 | } | |
ae0138a8 | 2161 | again = ehci_state_executing(q); |
94527ead GH |
2162 | break; |
2163 | ||
2164 | case EST_WRITEBACK: | |
b2467216 | 2165 | assert(q != NULL); |
ae0138a8 | 2166 | again = ehci_state_writeback(q); |
94527ead GH |
2167 | break; |
2168 | ||
2169 | default: | |
2170 | fprintf(stderr, "Bad state!\n"); | |
2171 | again = -1; | |
8ac6d699 | 2172 | assert(0); |
94527ead GH |
2173 | break; |
2174 | } | |
2175 | ||
2176 | if (again < 0) { | |
2177 | fprintf(stderr, "processing error - resetting ehci HC\n"); | |
2178 | ehci_reset(ehci); | |
2179 | again = 0; | |
2180 | } | |
2181 | } | |
2182 | while (again); | |
2183 | ||
2184 | ehci_commit_interrupt(ehci); | |
94527ead GH |
2185 | } |
2186 | ||
2187 | static void ehci_advance_async_state(EHCIState *ehci) | |
2188 | { | |
df5d5c5c | 2189 | const int async = 1; |
94527ead | 2190 | |
26d53979 | 2191 | switch(ehci_get_state(ehci, async)) { |
94527ead | 2192 | case EST_INACTIVE: |
ec807d12 | 2193 | if (!ehci_async_enabled(ehci)) { |
94527ead GH |
2194 | break; |
2195 | } | |
26d53979 | 2196 | ehci_set_state(ehci, async, EST_ACTIVE); |
94527ead GH |
2197 | // No break, fall through to ACTIVE |
2198 | ||
2199 | case EST_ACTIVE: | |
ec807d12 | 2200 | if (!ehci_async_enabled(ehci)) { |
e850c2b4 | 2201 | ehci_queues_rip_all(ehci, async); |
26d53979 | 2202 | ehci_set_state(ehci, async, EST_INACTIVE); |
94527ead GH |
2203 | break; |
2204 | } | |
2205 | ||
4be23939 | 2206 | /* make sure guest has acknowledged the doorbell interrupt */ |
94527ead GH |
2207 | /* TO-DO: is this really needed? */ |
2208 | if (ehci->usbsts & USBSTS_IAA) { | |
2209 | DPRINTF("IAA status bit still set.\n"); | |
2210 | break; | |
2211 | } | |
2212 | ||
94527ead GH |
2213 | /* check that address register has been set */ |
2214 | if (ehci->asynclistaddr == 0) { | |
2215 | break; | |
2216 | } | |
2217 | ||
26d53979 | 2218 | ehci_set_state(ehci, async, EST_WAITLISTHEAD); |
26d53979 | 2219 | ehci_advance_state(ehci, async); |
4be23939 HG |
2220 | |
2221 | /* If the doorbell is set, the guest wants to make a change to the | |
2222 | * schedule. The host controller needs to release cached data. | |
2223 | * (section 4.8.2) | |
2224 | */ | |
2225 | if (ehci->usbcmd & USBCMD_IAAD) { | |
2226 | /* Remove all unseen qhs from the async qhs queue */ | |
2227 | ehci_queues_rip_unused(ehci, async, 1); | |
2228 | DPRINTF("ASYNC: doorbell request acknowledged\n"); | |
2229 | ehci->usbcmd &= ~USBCMD_IAAD; | |
2230 | ehci_set_interrupt(ehci, USBSTS_IAA); | |
2231 | } | |
94527ead GH |
2232 | break; |
2233 | ||
2234 | default: | |
2235 | /* this should only be due to a developer mistake */ | |
2236 | fprintf(stderr, "ehci: Bad asynchronous state %d. " | |
2237 | "Resetting to active\n", ehci->astate); | |
0122f472 | 2238 | assert(0); |
94527ead GH |
2239 | } |
2240 | } | |
2241 | ||
2242 | static void ehci_advance_periodic_state(EHCIState *ehci) | |
2243 | { | |
2244 | uint32_t entry; | |
2245 | uint32_t list; | |
df5d5c5c | 2246 | const int async = 0; |
94527ead GH |
2247 | |
2248 | // 4.6 | |
2249 | ||
26d53979 | 2250 | switch(ehci_get_state(ehci, async)) { |
94527ead | 2251 | case EST_INACTIVE: |
ec807d12 | 2252 | if (!(ehci->frindex & 7) && ehci_periodic_enabled(ehci)) { |
26d53979 | 2253 | ehci_set_state(ehci, async, EST_ACTIVE); |
94527ead GH |
2254 | // No break, fall through to ACTIVE |
2255 | } else | |
2256 | break; | |
2257 | ||
2258 | case EST_ACTIVE: | |
ec807d12 | 2259 | if (!(ehci->frindex & 7) && !ehci_periodic_enabled(ehci)) { |
e850c2b4 | 2260 | ehci_queues_rip_all(ehci, async); |
26d53979 | 2261 | ehci_set_state(ehci, async, EST_INACTIVE); |
94527ead GH |
2262 | break; |
2263 | } | |
2264 | ||
2265 | list = ehci->periodiclistbase & 0xfffff000; | |
2266 | /* check that register has been set */ | |
2267 | if (list == 0) { | |
2268 | break; | |
2269 | } | |
2270 | list |= ((ehci->frindex & 0x1ff8) >> 1); | |
2271 | ||
4bf80119 | 2272 | pci_dma_read(&ehci->dev, list, &entry, sizeof entry); |
94527ead GH |
2273 | entry = le32_to_cpu(entry); |
2274 | ||
2275 | DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n", | |
2276 | ehci->frindex / 8, list, entry); | |
0122f472 | 2277 | ehci_set_fetch_addr(ehci, async,entry); |
26d53979 GH |
2278 | ehci_set_state(ehci, async, EST_FETCHENTRY); |
2279 | ehci_advance_state(ehci, async); | |
4be23939 | 2280 | ehci_queues_rip_unused(ehci, async, 0); |
94527ead GH |
2281 | break; |
2282 | ||
94527ead GH |
2283 | default: |
2284 | /* this should only be due to a developer mistake */ | |
2285 | fprintf(stderr, "ehci: Bad periodic state %d. " | |
2286 | "Resetting to active\n", ehci->pstate); | |
0122f472 | 2287 | assert(0); |
94527ead GH |
2288 | } |
2289 | } | |
2290 | ||
6ceced0b GH |
2291 | static void ehci_update_frindex(EHCIState *ehci, int frames) |
2292 | { | |
2293 | int i; | |
2294 | ||
2295 | if (!ehci_enabled(ehci)) { | |
2296 | return; | |
2297 | } | |
2298 | ||
2299 | for (i = 0; i < frames; i++) { | |
2300 | ehci->frindex += 8; | |
2301 | ||
2302 | if (ehci->frindex == 0x00002000) { | |
2303 | ehci_set_interrupt(ehci, USBSTS_FLR); | |
2304 | } | |
2305 | ||
2306 | if (ehci->frindex == 0x00004000) { | |
2307 | ehci_set_interrupt(ehci, USBSTS_FLR); | |
2308 | ehci->frindex = 0; | |
2309 | } | |
2310 | } | |
2311 | } | |
2312 | ||
94527ead GH |
2313 | static void ehci_frame_timer(void *opaque) |
2314 | { | |
2315 | EHCIState *ehci = opaque; | |
3a215326 | 2316 | int schedules = 0; |
94527ead | 2317 | int64_t expire_time, t_now; |
adddecb1 | 2318 | uint64_t ns_elapsed; |
f020ed36 | 2319 | int frames, skipped_frames; |
94527ead | 2320 | int i; |
94527ead | 2321 | |
94527ead | 2322 | t_now = qemu_get_clock_ns(vm_clock); |
adddecb1 GH |
2323 | ns_elapsed = t_now - ehci->last_run_ns; |
2324 | frames = ns_elapsed / FRAME_TIMER_NS; | |
94527ead | 2325 | |
3a215326 GH |
2326 | if (ehci_periodic_enabled(ehci) || ehci->pstate != EST_INACTIVE) { |
2327 | schedules++; | |
2328 | expire_time = t_now + (get_ticks_per_sec() / FRAME_TIMER_FREQ); | |
94527ead | 2329 | |
f020ed36 GH |
2330 | if (frames > ehci->maxframes) { |
2331 | skipped_frames = frames - ehci->maxframes; | |
2332 | ehci_update_frindex(ehci, skipped_frames); | |
2333 | ehci->last_run_ns += FRAME_TIMER_NS * skipped_frames; | |
2334 | frames -= skipped_frames; | |
2335 | DPRINTF("WARNING - EHCI skipped %d frames\n", skipped_frames); | |
2336 | } | |
2337 | ||
3a215326 GH |
2338 | for (i = 0; i < frames; i++) { |
2339 | ehci_update_frindex(ehci, 1); | |
f020ed36 | 2340 | ehci_advance_periodic_state(ehci); |
3a215326 GH |
2341 | ehci->last_run_ns += FRAME_TIMER_NS; |
2342 | } | |
2343 | } else { | |
2344 | if (ehci->async_stepdown < ehci->maxframes / 2) { | |
2345 | ehci->async_stepdown++; | |
2346 | } | |
2347 | expire_time = t_now + (get_ticks_per_sec() | |
2348 | * ehci->async_stepdown / FRAME_TIMER_FREQ); | |
2349 | ehci_update_frindex(ehci, frames); | |
2350 | ehci->last_run_ns += FRAME_TIMER_NS * frames; | |
94527ead GH |
2351 | } |
2352 | ||
94527ead GH |
2353 | /* Async is not inside loop since it executes everything it can once |
2354 | * called | |
2355 | */ | |
3a215326 GH |
2356 | if (ehci_async_enabled(ehci) || ehci->astate != EST_INACTIVE) { |
2357 | schedules++; | |
2358 | qemu_bh_schedule(ehci->async_bh); | |
2359 | } | |
94527ead | 2360 | |
3a215326 | 2361 | if (schedules) { |
daf25307 GH |
2362 | qemu_mod_timer(ehci->frame_timer, expire_time); |
2363 | } | |
94527ead GH |
2364 | } |
2365 | ||
0fb3e299 GH |
2366 | static void ehci_async_bh(void *opaque) |
2367 | { | |
2368 | EHCIState *ehci = opaque; | |
2369 | ehci_advance_async_state(ehci); | |
2370 | } | |
94527ead | 2371 | |
e57964f5 AK |
2372 | static const MemoryRegionOps ehci_mem_ops = { |
2373 | .old_mmio = { | |
2374 | .read = { ehci_mem_readb, ehci_mem_readw, ehci_mem_readl }, | |
2375 | .write = { ehci_mem_writeb, ehci_mem_writew, ehci_mem_writel }, | |
2376 | }, | |
2377 | .endianness = DEVICE_LITTLE_ENDIAN, | |
94527ead GH |
2378 | }; |
2379 | ||
94527ead GH |
2380 | static int usb_ehci_initfn(PCIDevice *dev); |
2381 | ||
2382 | static USBPortOps ehci_port_ops = { | |
2383 | .attach = ehci_attach, | |
2384 | .detach = ehci_detach, | |
4706ab6c | 2385 | .child_detach = ehci_child_detach, |
a0a3167a | 2386 | .wakeup = ehci_wakeup, |
94527ead GH |
2387 | .complete = ehci_async_complete_packet, |
2388 | }; | |
2389 | ||
07771f6f | 2390 | static USBBusOps ehci_bus_ops = { |
a0a3167a | 2391 | .register_companion = ehci_register_companion, |
07771f6f GH |
2392 | }; |
2393 | ||
9a773408 GH |
2394 | static int usb_ehci_post_load(void *opaque, int version_id) |
2395 | { | |
2396 | EHCIState *s = opaque; | |
2397 | int i; | |
2398 | ||
2399 | for (i = 0; i < NB_PORTS; i++) { | |
2400 | USBPort *companion = s->companion_ports[i]; | |
2401 | if (companion == NULL) { | |
2402 | continue; | |
2403 | } | |
2404 | if (s->portsc[i] & PORTSC_POWNER) { | |
2405 | companion->dev = s->ports[i].dev; | |
2406 | } else { | |
2407 | companion->dev = NULL; | |
2408 | } | |
2409 | } | |
2410 | ||
2411 | return 0; | |
2412 | } | |
2413 | ||
9490fb06 | 2414 | static const VMStateDescription vmstate_ehci = { |
9a773408 GH |
2415 | .name = "ehci", |
2416 | .version_id = 1, | |
2417 | .post_load = usb_ehci_post_load, | |
2418 | .fields = (VMStateField[]) { | |
2419 | VMSTATE_PCI_DEVICE(dev, EHCIState), | |
2420 | /* mmio registers */ | |
2421 | VMSTATE_UINT32(usbcmd, EHCIState), | |
2422 | VMSTATE_UINT32(usbsts, EHCIState), | |
2423 | VMSTATE_UINT32(usbintr, EHCIState), | |
2424 | VMSTATE_UINT32(frindex, EHCIState), | |
2425 | VMSTATE_UINT32(ctrldssegment, EHCIState), | |
2426 | VMSTATE_UINT32(periodiclistbase, EHCIState), | |
2427 | VMSTATE_UINT32(asynclistaddr, EHCIState), | |
2428 | VMSTATE_UINT32(configflag, EHCIState), | |
2429 | VMSTATE_UINT32(portsc[0], EHCIState), | |
2430 | VMSTATE_UINT32(portsc[1], EHCIState), | |
2431 | VMSTATE_UINT32(portsc[2], EHCIState), | |
2432 | VMSTATE_UINT32(portsc[3], EHCIState), | |
2433 | VMSTATE_UINT32(portsc[4], EHCIState), | |
2434 | VMSTATE_UINT32(portsc[5], EHCIState), | |
2435 | /* frame timer */ | |
2436 | VMSTATE_TIMER(frame_timer, EHCIState), | |
2437 | VMSTATE_UINT64(last_run_ns, EHCIState), | |
2438 | VMSTATE_UINT32(async_stepdown, EHCIState), | |
2439 | /* schedule state */ | |
2440 | VMSTATE_UINT32(astate, EHCIState), | |
2441 | VMSTATE_UINT32(pstate, EHCIState), | |
2442 | VMSTATE_UINT32(a_fetch_addr, EHCIState), | |
2443 | VMSTATE_UINT32(p_fetch_addr, EHCIState), | |
2444 | VMSTATE_END_OF_LIST() | |
2445 | } | |
9490fb06 GH |
2446 | }; |
2447 | ||
3028376e | 2448 | static Property ehci_properties[] = { |
3028376e GH |
2449 | DEFINE_PROP_UINT32("maxframes", EHCIState, maxframes, 128), |
2450 | DEFINE_PROP_END_OF_LIST(), | |
2451 | }; | |
2452 | ||
40021f08 AL |
2453 | static void ehci_class_init(ObjectClass *klass, void *data) |
2454 | { | |
39bffca2 | 2455 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
2456 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
2457 | ||
2458 | k->init = usb_ehci_initfn; | |
2459 | k->vendor_id = PCI_VENDOR_ID_INTEL; | |
2460 | k->device_id = PCI_DEVICE_ID_INTEL_82801D; /* ich4 */ | |
2461 | k->revision = 0x10; | |
2462 | k->class_id = PCI_CLASS_SERIAL_USB; | |
39bffca2 AL |
2463 | dc->vmsd = &vmstate_ehci; |
2464 | dc->props = ehci_properties; | |
40021f08 AL |
2465 | } |
2466 | ||
39bffca2 AL |
2467 | static TypeInfo ehci_info = { |
2468 | .name = "usb-ehci", | |
2469 | .parent = TYPE_PCI_DEVICE, | |
2470 | .instance_size = sizeof(EHCIState), | |
2471 | .class_init = ehci_class_init, | |
e855761c AL |
2472 | }; |
2473 | ||
40021f08 AL |
2474 | static void ich9_ehci_class_init(ObjectClass *klass, void *data) |
2475 | { | |
39bffca2 | 2476 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
2477 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
2478 | ||
2479 | k->init = usb_ehci_initfn; | |
2480 | k->vendor_id = PCI_VENDOR_ID_INTEL; | |
2481 | k->device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1; | |
2482 | k->revision = 0x03; | |
2483 | k->class_id = PCI_CLASS_SERIAL_USB; | |
39bffca2 AL |
2484 | dc->vmsd = &vmstate_ehci; |
2485 | dc->props = ehci_properties; | |
40021f08 AL |
2486 | } |
2487 | ||
39bffca2 AL |
2488 | static TypeInfo ich9_ehci_info = { |
2489 | .name = "ich9-usb-ehci1", | |
2490 | .parent = TYPE_PCI_DEVICE, | |
2491 | .instance_size = sizeof(EHCIState), | |
2492 | .class_init = ich9_ehci_class_init, | |
94527ead GH |
2493 | }; |
2494 | ||
2495 | static int usb_ehci_initfn(PCIDevice *dev) | |
2496 | { | |
2497 | EHCIState *s = DO_UPCAST(EHCIState, dev, dev); | |
2498 | uint8_t *pci_conf = s->dev.config; | |
2499 | int i; | |
2500 | ||
94527ead | 2501 | pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20); |
94527ead GH |
2502 | |
2503 | /* capabilities pointer */ | |
2504 | pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00); | |
2505 | //pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50); | |
2506 | ||
817e0b6f | 2507 | pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */ |
94527ead GH |
2508 | pci_set_byte(&pci_conf[PCI_MIN_GNT], 0); |
2509 | pci_set_byte(&pci_conf[PCI_MAX_LAT], 0); | |
2510 | ||
2511 | // pci_conf[0x50] = 0x01; // power management caps | |
2512 | ||
4001f22f | 2513 | pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); // release number (2.1.4) |
94527ead GH |
2514 | pci_set_byte(&pci_conf[0x61], 0x20); // frame length adjustment (2.1.5) |
2515 | pci_set_word(&pci_conf[0x62], 0x00); // port wake up capability (2.1.6) | |
2516 | ||
2517 | pci_conf[0x64] = 0x00; | |
2518 | pci_conf[0x65] = 0x00; | |
2519 | pci_conf[0x66] = 0x00; | |
2520 | pci_conf[0x67] = 0x00; | |
2521 | pci_conf[0x68] = 0x01; | |
2522 | pci_conf[0x69] = 0x00; | |
2523 | pci_conf[0x6a] = 0x00; | |
2524 | pci_conf[0x6b] = 0x00; // USBLEGSUP | |
2525 | pci_conf[0x6c] = 0x00; | |
2526 | pci_conf[0x6d] = 0x00; | |
2527 | pci_conf[0x6e] = 0x00; | |
2528 | pci_conf[0x6f] = 0xc0; // USBLEFCTLSTS | |
2529 | ||
2530 | // 2.2 host controller interface version | |
2531 | s->mmio[0x00] = (uint8_t) OPREGBASE; | |
2532 | s->mmio[0x01] = 0x00; | |
2533 | s->mmio[0x02] = 0x00; | |
2534 | s->mmio[0x03] = 0x01; // HC version | |
2535 | s->mmio[0x04] = NB_PORTS; // Number of downstream ports | |
2536 | s->mmio[0x05] = 0x00; // No companion ports at present | |
2537 | s->mmio[0x06] = 0x00; | |
2538 | s->mmio[0x07] = 0x00; | |
2539 | s->mmio[0x08] = 0x80; // We can cache whole frame, not 64-bit capable | |
2540 | s->mmio[0x09] = 0x68; // EECP | |
2541 | s->mmio[0x0a] = 0x00; | |
2542 | s->mmio[0x0b] = 0x00; | |
2543 | ||
2544 | s->irq = s->dev.irq[3]; | |
2545 | ||
07771f6f | 2546 | usb_bus_new(&s->bus, &ehci_bus_ops, &s->dev.qdev); |
94527ead GH |
2547 | for(i = 0; i < NB_PORTS; i++) { |
2548 | usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops, | |
2549 | USB_SPEED_MASK_HIGH); | |
94527ead GH |
2550 | s->ports[i].dev = 0; |
2551 | } | |
2552 | ||
2553 | s->frame_timer = qemu_new_timer_ns(vm_clock, ehci_frame_timer, s); | |
0fb3e299 | 2554 | s->async_bh = qemu_bh_new(ehci_async_bh, s); |
df5d5c5c HG |
2555 | QTAILQ_INIT(&s->aqueues); |
2556 | QTAILQ_INIT(&s->pqueues); | |
94527ead GH |
2557 | |
2558 | qemu_register_reset(ehci_reset, s); | |
2559 | ||
e57964f5 | 2560 | memory_region_init_io(&s->mem, &ehci_mem_ops, s, "ehci", MMIO_SIZE); |
e824b2cc | 2561 | pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem); |
94527ead | 2562 | |
94527ead GH |
2563 | return 0; |
2564 | } | |
2565 | ||
83f7d43a | 2566 | static void ehci_register_types(void) |
94527ead | 2567 | { |
39bffca2 AL |
2568 | type_register_static(&ehci_info); |
2569 | type_register_static(&ich9_ehci_info); | |
94527ead | 2570 | } |
83f7d43a AF |
2571 | |
2572 | type_init(ehci_register_types) | |
94527ead GH |
2573 | |
2574 | /* | |
2575 | * vim: expandtab ts=4 | |
2576 | */ |