]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/hcd-uhci.c
sysbus: Convert to sysbus_realize() etc. with Coccinelle
[mirror_qemu.git] / hw / usb / hcd-uhci.c
CommitLineData
bb36d470
FB
1/*
2 * USB UHCI controller emulation
5fafdf24 3 *
bb36d470 4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
54f254f9
AL
6 * Copyright (c) 2008 Max Krasnyansky
7 * Magor rewrite of the UHCI data structures parser and frame processor
8 * Support for fully async operation and multiple outstanding transactions
9 *
bb36d470
FB
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
0b8fa32f 28
e532b2e0 29#include "qemu/osdep.h"
f1ae32a1 30#include "hw/usb.h"
9a1d111e 31#include "hw/usb/uhci-regs.h"
d6454270 32#include "migration/vmstate.h"
a2cb15b0 33#include "hw/pci/pci.h"
a27bd6c7 34#include "hw/qdev-properties.h"
da34e65c 35#include "qapi/error.h"
1de7afc9
PB
36#include "qemu/timer.h"
37#include "qemu/iov.h"
9c17d615 38#include "sysemu/dma.h"
50dcc0f8 39#include "trace.h"
6a1751b7 40#include "qemu/main-loop.h"
0b8fa32f 41#include "qemu/module.h"
bb36d470 42
bb36d470
FB
43#define FRAME_TIMER_FREQ 1000
44
3200d108 45#define FRAME_MAX_LOOPS 256
bb36d470 46
475443cf
HG
47/* Must be large enough to handle 10 frame delay for initial isoc requests */
48#define QH_VALID 32
49
f8f48b69
HG
50#define MAX_FRAMES_PER_TICK (QH_VALID / 2)
51
bb36d470
FB
52#define NB_PORTS 2
53
60e1b2a6 54enum {
0cd178ca
GH
55 TD_RESULT_STOP_FRAME = 10,
56 TD_RESULT_COMPLETE,
57 TD_RESULT_NEXT_QH,
4efe4ef3
GH
58 TD_RESULT_ASYNC_START,
59 TD_RESULT_ASYNC_CONT,
60e1b2a6
GH
60};
61
7b5a44c5 62typedef struct UHCIState UHCIState;
f8af1e88
GH
63typedef struct UHCIAsync UHCIAsync;
64typedef struct UHCIQueue UHCIQueue;
2c2e8525 65typedef struct UHCIInfo UHCIInfo;
8f3f90b0 66typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass;
2c2e8525
GH
67
68struct UHCIInfo {
69 const char *name;
70 uint16_t vendor_id;
71 uint16_t device_id;
72 uint8_t revision;
8f3f90b0 73 uint8_t irq_pin;
63216dc7 74 void (*realize)(PCIDevice *dev, Error **errp);
2c2e8525
GH
75 bool unplug;
76};
7b5a44c5 77
8f3f90b0
GH
78struct UHCIPCIDeviceClass {
79 PCIDeviceClass parent_class;
80 UHCIInfo info;
81};
82
54f254f9
AL
83/*
84 * Pending async transaction.
85 * 'packet' must be the first field because completion
86 * handler does "(UHCIAsync *) pkt" cast.
87 */
f8af1e88
GH
88
89struct UHCIAsync {
54f254f9 90 USBPacket packet;
9822261c
HG
91 uint8_t static_buf[64]; /* 64 bytes is enough, except for isoc packets */
92 uint8_t *buf;
f8af1e88 93 UHCIQueue *queue;
ddf6583f 94 QTAILQ_ENTRY(UHCIAsync) next;
1f250cc7 95 uint32_t td_addr;
54f254f9 96 uint8_t done;
f8af1e88
GH
97};
98
99struct UHCIQueue {
66a08cbe 100 uint32_t qh_addr;
f8af1e88
GH
101 uint32_t token;
102 UHCIState *uhci;
11d15e40 103 USBEndpoint *ep;
f8af1e88 104 QTAILQ_ENTRY(UHCIQueue) next;
eae3eb3e 105 QTAILQ_HEAD(, UHCIAsync) asyncs;
f8af1e88
GH
106 int8_t valid;
107};
54f254f9 108
bb36d470
FB
109typedef struct UHCIPort {
110 USBPort port;
111 uint16_t ctrl;
bb36d470
FB
112} UHCIPort;
113
7b5a44c5 114struct UHCIState {
bb36d470 115 PCIDevice dev;
a03f66e4 116 MemoryRegion io_bar;
35e4977f 117 USBBus bus; /* Note unused when we're a companion controller */
bb36d470
FB
118 uint16_t cmd; /* cmd register */
119 uint16_t status;
120 uint16_t intr; /* interrupt enable register */
121 uint16_t frnum; /* frame number */
122 uint32_t fl_base_addr; /* frame list base address */
123 uint8_t sof_timing;
124 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
8e65b7c0 125 int64_t expire_time;
bb36d470 126 QEMUTimer *frame_timer;
9a16c595 127 QEMUBH *bh;
4aed20e2 128 uint32_t frame_bytes;
40141d12 129 uint32_t frame_bandwidth;
88793816 130 bool completions_only;
bb36d470 131 UHCIPort ports[NB_PORTS];
4d611c9a
PB
132
133 /* Interrupts that should be raised at the end of the current frame. */
134 uint32_t pending_int_mask;
54f254f9
AL
135
136 /* Active packets */
f8af1e88 137 QTAILQ_HEAD(, UHCIQueue) queues;
64e58fe5 138 uint8_t num_ports_vmstate;
35e4977f
HG
139
140 /* Properties */
141 char *masterbus;
142 uint32_t firstport;
9fdf7027 143 uint32_t maxframes;
7b5a44c5 144};
bb36d470
FB
145
146typedef struct UHCI_TD {
147 uint32_t link;
148 uint32_t ctrl; /* see TD_CTRL_xxx */
149 uint32_t token;
150 uint32_t buffer;
151} UHCI_TD;
152
153typedef struct UHCI_QH {
154 uint32_t link;
155 uint32_t el_link;
156} UHCI_QH;
157
40507377 158static void uhci_async_cancel(UHCIAsync *async);
11d15e40 159static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
9f0f1a0c 160static void uhci_resume(void *opaque);
40507377 161
49184b62
GA
162#define TYPE_UHCI "pci-uhci-usb"
163#define UHCI(obj) OBJECT_CHECK(UHCIState, (obj), TYPE_UHCI)
164
f8af1e88
GH
165static inline int32_t uhci_queue_token(UHCI_TD *td)
166{
6fe30910
HG
167 if ((td->token & (0xf << 15)) == 0) {
168 /* ctrl ep, cover ep and dev, not pid! */
169 return td->token & 0x7ff00;
170 } else {
171 /* covers ep, dev, pid -> identifies the endpoint */
172 return td->token & 0x7ffff;
173 }
f8af1e88
GH
174}
175
66a08cbe
HG
176static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
177 USBEndpoint *ep)
f8af1e88 178{
f8af1e88
GH
179 UHCIQueue *queue;
180
f8af1e88
GH
181 queue = g_new0(UHCIQueue, 1);
182 queue->uhci = s;
66a08cbe
HG
183 queue->qh_addr = qh_addr;
184 queue->token = uhci_queue_token(td);
11d15e40 185 queue->ep = ep;
f8af1e88
GH
186 QTAILQ_INIT(&queue->asyncs);
187 QTAILQ_INSERT_HEAD(&s->queues, queue, next);
475443cf 188 queue->valid = QH_VALID;
50dcc0f8 189 trace_usb_uhci_queue_add(queue->token);
f8af1e88
GH
190 return queue;
191}
192
66a08cbe 193static void uhci_queue_free(UHCIQueue *queue, const char *reason)
f8af1e88
GH
194{
195 UHCIState *s = queue->uhci;
40507377
HG
196 UHCIAsync *async;
197
198 while (!QTAILQ_EMPTY(&queue->asyncs)) {
199 async = QTAILQ_FIRST(&queue->asyncs);
200 uhci_async_cancel(async);
201 }
f79738b0 202 usb_device_ep_stopped(queue->ep->dev, queue->ep);
f8af1e88 203
66a08cbe 204 trace_usb_uhci_queue_del(queue->token, reason);
f8af1e88
GH
205 QTAILQ_REMOVE(&s->queues, queue, next);
206 g_free(queue);
207}
208
66a08cbe
HG
209static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
210{
211 uint32_t token = uhci_queue_token(td);
212 UHCIQueue *queue;
213
214 QTAILQ_FOREACH(queue, &s->queues, next) {
215 if (queue->token == token) {
216 return queue;
217 }
218 }
219 return NULL;
220}
221
222static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
223 uint32_t td_addr, bool queuing)
224{
225 UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
c348e481 226 uint32_t queue_token_addr = (queue->token >> 8) & 0x7f;
66a08cbe
HG
227
228 return queue->qh_addr == qh_addr &&
229 queue->token == uhci_queue_token(td) &&
c348e481 230 queue_token_addr == queue->ep->dev->addr &&
66a08cbe
HG
231 (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
232 first->td_addr == td_addr);
233}
234
1f250cc7 235static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
54f254f9 236{
326700e3 237 UHCIAsync *async = g_new0(UHCIAsync, 1);
487414f1 238
f8af1e88 239 async->queue = queue;
1f250cc7 240 async->td_addr = td_addr;
4f4321c1 241 usb_packet_init(&async->packet);
1f250cc7 242 trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
54f254f9
AL
243
244 return async;
245}
246
f8af1e88 247static void uhci_async_free(UHCIAsync *async)
54f254f9 248{
1f250cc7 249 trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
4f4321c1 250 usb_packet_cleanup(&async->packet);
9822261c
HG
251 if (async->buf != async->static_buf) {
252 g_free(async->buf);
253 }
7267c094 254 g_free(async);
54f254f9
AL
255}
256
f8af1e88 257static void uhci_async_link(UHCIAsync *async)
54f254f9 258{
f8af1e88
GH
259 UHCIQueue *queue = async->queue;
260 QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
1f250cc7 261 trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
54f254f9
AL
262}
263
f8af1e88 264static void uhci_async_unlink(UHCIAsync *async)
54f254f9 265{
f8af1e88
GH
266 UHCIQueue *queue = async->queue;
267 QTAILQ_REMOVE(&queue->asyncs, async, next);
1f250cc7 268 trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
54f254f9
AL
269}
270
f8af1e88 271static void uhci_async_cancel(UHCIAsync *async)
54f254f9 272{
2f2ee268 273 uhci_async_unlink(async);
1f250cc7
HG
274 trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
275 async->done);
54f254f9
AL
276 if (!async->done)
277 usb_cancel_packet(&async->packet);
f8af1e88 278 uhci_async_free(async);
54f254f9
AL
279}
280
281/*
282 * Mark all outstanding async packets as invalid.
283 * This is used for canceling them when TDs are removed by the HCD.
284 */
f8af1e88 285static void uhci_async_validate_begin(UHCIState *s)
54f254f9 286{
f8af1e88 287 UHCIQueue *queue;
54f254f9 288
f8af1e88
GH
289 QTAILQ_FOREACH(queue, &s->queues, next) {
290 queue->valid--;
54f254f9 291 }
54f254f9
AL
292}
293
294/*
295 * Cancel async packets that are no longer valid
296 */
297static void uhci_async_validate_end(UHCIState *s)
298{
f8af1e88 299 UHCIQueue *queue, *n;
54f254f9 300
f8af1e88 301 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
40507377 302 if (!queue->valid) {
66a08cbe 303 uhci_queue_free(queue, "validate-end");
f8af1e88 304 }
54f254f9
AL
305 }
306}
307
07771f6f
GH
308static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
309{
5ad23e87 310 UHCIQueue *queue, *n;
07771f6f 311
5ad23e87
HG
312 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
313 if (queue->ep->dev == dev) {
314 uhci_queue_free(queue, "cancel-device");
07771f6f 315 }
07771f6f
GH
316 }
317}
318
54f254f9
AL
319static void uhci_async_cancel_all(UHCIState *s)
320{
77fa9aee 321 UHCIQueue *queue, *nq;
54f254f9 322
77fa9aee 323 QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
66a08cbe 324 uhci_queue_free(queue, "cancel-all");
54f254f9 325 }
54f254f9
AL
326}
327
8c75a899 328static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
54f254f9 329{
f8af1e88 330 UHCIQueue *queue;
ddf6583f 331 UHCIAsync *async;
e8ee3c72 332
f8af1e88 333 QTAILQ_FOREACH(queue, &s->queues, next) {
8c75a899
HG
334 QTAILQ_FOREACH(async, &queue->asyncs, next) {
335 if (async->td_addr == td_addr) {
336 return async;
337 }
f8af1e88
GH
338 }
339 }
f8af1e88 340 return NULL;
54f254f9
AL
341}
342
bb36d470
FB
343static void uhci_update_irq(UHCIState *s)
344{
345 int level;
346 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
347 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
348 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
349 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
350 (s->status & UHCI_STS_HSERR) ||
351 (s->status & UHCI_STS_HCPERR)) {
352 level = 1;
353 } else {
354 level = 0;
355 }
9e64f8a3 356 pci_set_irq(&s->dev, level);
bb36d470
FB
357}
358
537e572a 359static void uhci_reset(DeviceState *dev)
bb36d470 360{
537e572a 361 PCIDevice *d = PCI_DEVICE(dev);
49184b62 362 UHCIState *s = UHCI(d);
bb36d470
FB
363 uint8_t *pci_conf;
364 int i;
365 UHCIPort *port;
366
50dcc0f8 367 trace_usb_uhci_reset();
6f382b5e 368
bb36d470
FB
369 pci_conf = s->dev.config;
370
371 pci_conf[0x6a] = 0x01; /* usb clock */
372 pci_conf[0x6b] = 0x00;
373 s->cmd = 0;
ca5a21c4 374 s->status = UHCI_STS_HCHALTED;
bb36d470
FB
375 s->status2 = 0;
376 s->intr = 0;
377 s->fl_base_addr = 0;
378 s->sof_timing = 64;
54f254f9 379
bb36d470
FB
380 for(i = 0; i < NB_PORTS; i++) {
381 port = &s->ports[i];
382 port->ctrl = 0x0080;
891fb2cd 383 if (port->port.dev && port->port.dev->attached) {
d28f4e2d 384 usb_port_reset(&port->port);
618c169b 385 }
bb36d470 386 }
54f254f9
AL
387
388 uhci_async_cancel_all(s);
9a16c595 389 qemu_bh_cancel(s->bh);
aba1f242 390 uhci_update_irq(s);
bb36d470
FB
391}
392
817afc61
JQ
393static const VMStateDescription vmstate_uhci_port = {
394 .name = "uhci port",
395 .version_id = 1,
396 .minimum_version_id = 1,
6e3d652a 397 .fields = (VMStateField[]) {
817afc61
JQ
398 VMSTATE_UINT16(ctrl, UHCIPort),
399 VMSTATE_END_OF_LIST()
400 }
401};
402
75f151cd
GH
403static int uhci_post_load(void *opaque, int version_id)
404{
405 UHCIState *s = opaque;
406
407 if (version_id < 2) {
bc72ad67 408 s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
73bcb24d 409 (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ);
75f151cd
GH
410 }
411 return 0;
412}
413
817afc61
JQ
414static const VMStateDescription vmstate_uhci = {
415 .name = "uhci",
ecfdc15f 416 .version_id = 3,
817afc61 417 .minimum_version_id = 1,
75f151cd 418 .post_load = uhci_post_load,
6e3d652a 419 .fields = (VMStateField[]) {
817afc61 420 VMSTATE_PCI_DEVICE(dev, UHCIState),
d2164ad3 421 VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState, NULL),
817afc61
JQ
422 VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
423 vmstate_uhci_port, UHCIPort),
424 VMSTATE_UINT16(cmd, UHCIState),
425 VMSTATE_UINT16(status, UHCIState),
426 VMSTATE_UINT16(intr, UHCIState),
427 VMSTATE_UINT16(frnum, UHCIState),
428 VMSTATE_UINT32(fl_base_addr, UHCIState),
429 VMSTATE_UINT8(sof_timing, UHCIState),
430 VMSTATE_UINT8(status2, UHCIState),
e720677e 431 VMSTATE_TIMER_PTR(frame_timer, UHCIState),
6881dd5f 432 VMSTATE_INT64_V(expire_time, UHCIState, 2),
ecfdc15f 433 VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3),
817afc61
JQ
434 VMSTATE_END_OF_LIST()
435 }
436};
b9dc033c 437
89eb147c
GH
438static void uhci_port_write(void *opaque, hwaddr addr,
439 uint64_t val, unsigned size)
bb36d470
FB
440{
441 UHCIState *s = opaque;
3b46e624 442
50dcc0f8 443 trace_usb_uhci_mmio_writew(addr, val);
54f254f9 444
bb36d470
FB
445 switch(addr) {
446 case 0x00:
447 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
448 /* start frame processing */
50dcc0f8 449 trace_usb_uhci_schedule_start();
bc72ad67 450 s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
73bcb24d 451 (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ);
bc72ad67 452 timer_mod(s->frame_timer, s->expire_time);
52328140 453 s->status &= ~UHCI_STS_HCHALTED;
467d409f 454 } else if (!(val & UHCI_CMD_RS)) {
52328140 455 s->status |= UHCI_STS_HCHALTED;
bb36d470
FB
456 }
457 if (val & UHCI_CMD_GRESET) {
458 UHCIPort *port;
bb36d470
FB
459 int i;
460
461 /* send reset on the USB bus */
462 for(i = 0; i < NB_PORTS; i++) {
463 port = &s->ports[i];
d28f4e2d 464 usb_device_reset(port->port.dev);
bb36d470 465 }
537e572a 466 uhci_reset(DEVICE(s));
bb36d470
FB
467 return;
468 }
5e9ab4c4 469 if (val & UHCI_CMD_HCRESET) {
537e572a 470 uhci_reset(DEVICE(s));
bb36d470
FB
471 return;
472 }
473 s->cmd = val;
9f0f1a0c
GH
474 if (val & UHCI_CMD_EGSM) {
475 if ((s->ports[0].ctrl & UHCI_PORT_RD) ||
476 (s->ports[1].ctrl & UHCI_PORT_RD)) {
477 uhci_resume(s);
478 }
479 }
bb36d470
FB
480 break;
481 case 0x02:
482 s->status &= ~val;
483 /* XXX: the chip spec is not coherent, so we add a hidden
484 register to distinguish between IOC and SPD */
485 if (val & UHCI_STS_USBINT)
486 s->status2 = 0;
487 uhci_update_irq(s);
488 break;
489 case 0x04:
490 s->intr = val;
491 uhci_update_irq(s);
492 break;
493 case 0x06:
494 if (s->status & UHCI_STS_HCHALTED)
495 s->frnum = val & 0x7ff;
496 break;
89eb147c
GH
497 case 0x08:
498 s->fl_base_addr &= 0xffff0000;
499 s->fl_base_addr |= val & ~0xfff;
500 break;
501 case 0x0a:
502 s->fl_base_addr &= 0x0000ffff;
503 s->fl_base_addr |= (val << 16);
504 break;
505 case 0x0c:
506 s->sof_timing = val & 0xff;
507 break;
bb36d470
FB
508 case 0x10 ... 0x1f:
509 {
510 UHCIPort *port;
511 USBDevice *dev;
512 int n;
513
514 n = (addr >> 1) & 7;
515 if (n >= NB_PORTS)
516 return;
517 port = &s->ports[n];
a594cfbf 518 dev = port->port.dev;
891fb2cd 519 if (dev && dev->attached) {
bb36d470 520 /* port reset */
5fafdf24 521 if ( (val & UHCI_PORT_RESET) &&
bb36d470 522 !(port->ctrl & UHCI_PORT_RESET) ) {
d28f4e2d 523 usb_device_reset(dev);
bb36d470
FB
524 }
525 }
9159f679 526 port->ctrl &= UHCI_PORT_READ_ONLY;
1cbdde90
HG
527 /* enabled may only be set if a device is connected */
528 if (!(port->ctrl & UHCI_PORT_CCS)) {
529 val &= ~UHCI_PORT_EN;
530 }
9159f679 531 port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
bb36d470 532 /* some bits are reset when a '1' is written to them */
9159f679 533 port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
bb36d470
FB
534 }
535 break;
536 }
537}
538
89eb147c 539static uint64_t uhci_port_read(void *opaque, hwaddr addr, unsigned size)
bb36d470
FB
540{
541 UHCIState *s = opaque;
542 uint32_t val;
543
bb36d470
FB
544 switch(addr) {
545 case 0x00:
546 val = s->cmd;
547 break;
548 case 0x02:
549 val = s->status;
550 break;
551 case 0x04:
552 val = s->intr;
553 break;
554 case 0x06:
555 val = s->frnum;
556 break;
89eb147c
GH
557 case 0x08:
558 val = s->fl_base_addr & 0xffff;
559 break;
560 case 0x0a:
561 val = (s->fl_base_addr >> 16) & 0xffff;
562 break;
563 case 0x0c:
564 val = s->sof_timing;
565 break;
bb36d470
FB
566 case 0x10 ... 0x1f:
567 {
568 UHCIPort *port;
569 int n;
570 n = (addr >> 1) & 7;
5fafdf24 571 if (n >= NB_PORTS)
bb36d470
FB
572 goto read_default;
573 port = &s->ports[n];
574 val = port->ctrl;
575 }
576 break;
577 default:
578 read_default:
579 val = 0xff7f; /* disabled port */
580 break;
581 }
54f254f9 582
50dcc0f8 583 trace_usb_uhci_mmio_readw(addr, val);
54f254f9 584
bb36d470
FB
585 return val;
586}
587
96217e31
TS
588/* signal resume if controller suspended */
589static void uhci_resume (void *opaque)
590{
591 UHCIState *s = (UHCIState *)opaque;
592
593 if (!s)
594 return;
595
596 if (s->cmd & UHCI_CMD_EGSM) {
597 s->cmd |= UHCI_CMD_FGR;
598 s->status |= UHCI_STS_RD;
599 uhci_update_irq(s);
600 }
601}
602
618c169b 603static void uhci_attach(USBPort *port1)
bb36d470
FB
604{
605 UHCIState *s = port1->opaque;
606 UHCIPort *port = &s->ports[port1->index];
607
618c169b
GH
608 /* set connect status */
609 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
61064870 610
618c169b
GH
611 /* update speed */
612 if (port->port.dev->speed == USB_SPEED_LOW) {
613 port->ctrl |= UHCI_PORT_LSDA;
bb36d470 614 } else {
618c169b
GH
615 port->ctrl &= ~UHCI_PORT_LSDA;
616 }
96217e31 617
618c169b
GH
618 uhci_resume(s);
619}
96217e31 620
618c169b
GH
621static void uhci_detach(USBPort *port1)
622{
623 UHCIState *s = port1->opaque;
624 UHCIPort *port = &s->ports[port1->index];
625
4706ab6c
HG
626 uhci_async_cancel_device(s, port1->dev);
627
618c169b
GH
628 /* set connect status */
629 if (port->ctrl & UHCI_PORT_CCS) {
630 port->ctrl &= ~UHCI_PORT_CCS;
631 port->ctrl |= UHCI_PORT_CSC;
bb36d470 632 }
618c169b
GH
633 /* disable port */
634 if (port->ctrl & UHCI_PORT_EN) {
635 port->ctrl &= ~UHCI_PORT_EN;
636 port->ctrl |= UHCI_PORT_ENC;
637 }
638
639 uhci_resume(s);
bb36d470
FB
640}
641
4706ab6c
HG
642static void uhci_child_detach(USBPort *port1, USBDevice *child)
643{
644 UHCIState *s = port1->opaque;
645
646 uhci_async_cancel_device(s, child);
647}
648
d47e59b8 649static void uhci_wakeup(USBPort *port1)
9159f679 650{
d47e59b8
HG
651 UHCIState *s = port1->opaque;
652 UHCIPort *port = &s->ports[port1->index];
9159f679
GH
653
654 if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
655 port->ctrl |= UHCI_PORT_RD;
656 uhci_resume(s);
657 }
658}
659
461700c1 660static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
bb36d470 661{
461700c1
GH
662 USBDevice *dev;
663 int i;
54f254f9 664
461700c1 665 for (i = 0; i < NB_PORTS; i++) {
54f254f9 666 UHCIPort *port = &s->ports[i];
461700c1
GH
667 if (!(port->ctrl & UHCI_PORT_EN)) {
668 continue;
669 }
670 dev = usb_find_device(&port->port, addr);
671 if (dev != NULL) {
672 return dev;
891fb2cd 673 }
bb36d470 674 }
461700c1 675 return NULL;
bb36d470
FB
676}
677
963a68b5
HG
678static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
679{
680 pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
681 le32_to_cpus(&td->link);
682 le32_to_cpus(&td->ctrl);
683 le32_to_cpus(&td->token);
684 le32_to_cpus(&td->buffer);
685}
686
faccca00
HG
687static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr,
688 int status, uint32_t *int_mask)
689{
690 uint32_t queue_token = uhci_queue_token(td);
691 int ret;
692
693 switch (status) {
694 case USB_RET_NAK:
695 td->ctrl |= TD_CTRL_NAK;
696 return TD_RESULT_NEXT_QH;
697
698 case USB_RET_STALL:
699 td->ctrl |= TD_CTRL_STALL;
700 trace_usb_uhci_packet_complete_stall(queue_token, td_addr);
701 ret = TD_RESULT_NEXT_QH;
702 break;
703
704 case USB_RET_BABBLE:
705 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
706 /* frame interrupted */
707 trace_usb_uhci_packet_complete_babble(queue_token, td_addr);
708 ret = TD_RESULT_STOP_FRAME;
709 break;
710
711 case USB_RET_IOERROR:
712 case USB_RET_NODEV:
713 default:
714 td->ctrl |= TD_CTRL_TIMEOUT;
715 td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
716 trace_usb_uhci_packet_complete_error(queue_token, td_addr);
717 ret = TD_RESULT_NEXT_QH;
718 break;
719 }
720
721 td->ctrl &= ~TD_CTRL_ACTIVE;
722 s->status |= UHCI_STS_USBERR;
723 if (td->ctrl & TD_CTRL_IOC) {
724 *int_mask |= 0x01;
725 }
726 uhci_update_irq(s);
727 return ret;
728}
729
54f254f9 730static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
bb36d470 731{
9a77a0f5 732 int len = 0, max_len;
bb36d470 733 uint8_t pid;
bb36d470 734
54f254f9
AL
735 max_len = ((td->token >> 21) + 1) & 0x7ff;
736 pid = td->token & 0xff;
737
54f254f9
AL
738 if (td->ctrl & TD_CTRL_IOS)
739 td->ctrl &= ~TD_CTRL_ACTIVE;
bb36d470 740
9a77a0f5
HG
741 if (async->packet.status != USB_RET_SUCCESS) {
742 return uhci_handle_td_error(s, td, async->td_addr,
743 async->packet.status, int_mask);
faccca00 744 }
b9dc033c 745
9a77a0f5 746 len = async->packet.actual_length;
54f254f9
AL
747 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
748
749 /* The NAK bit may have been set by a previous frame, so clear it
750 here. The docs are somewhat unclear, but win2k relies on this
751 behavior. */
752 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
5bd2c0d7
PB
753 if (td->ctrl & TD_CTRL_IOC)
754 *int_mask |= 0x01;
54f254f9
AL
755
756 if (pid == USB_TOKEN_IN) {
9822261c 757 pci_dma_write(&s->dev, td->buffer, async->buf, len);
54f254f9 758 if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
bb36d470
FB
759 *int_mask |= 0x02;
760 /* short packet: do not update QH */
50dcc0f8 761 trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
1f250cc7 762 async->td_addr);
60e1b2a6 763 return TD_RESULT_NEXT_QH;
bb36d470 764 }
54f254f9
AL
765 }
766
767 /* success */
1f250cc7
HG
768 trace_usb_uhci_packet_complete_success(async->queue->token,
769 async->td_addr);
60e1b2a6 770 return TD_RESULT_COMPLETE;
bb36d470
FB
771}
772
66a08cbe 773static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
a4f30cd7 774 UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
54f254f9 775{
9a77a0f5 776 int ret, max_len;
6ba43f1f 777 bool spd;
a4f30cd7 778 bool queuing = (q != NULL);
11d15e40 779 uint8_t pid = td->token & 0xff;
5f77e06b
GA
780 UHCIAsync *async;
781
5f77e06b 782 async = uhci_async_find_td(s, td_addr);
8c75a899
HG
783 if (async) {
784 if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
785 assert(q == NULL || q == async->queue);
786 q = async->queue;
787 } else {
788 uhci_queue_free(async->queue, "guest re-used pending td");
789 async = NULL;
790 }
791 }
54f254f9 792
66a08cbe
HG
793 if (q == NULL) {
794 q = uhci_queue_find(s, td);
795 if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
796 uhci_queue_free(q, "guest re-used qh");
797 q = NULL;
798 }
799 }
800
3905097e 801 if (q) {
475443cf 802 q->valid = QH_VALID;
3905097e
HG
803 }
804
54f254f9 805 /* Is active ? */
883bca77 806 if (!(td->ctrl & TD_CTRL_ACTIVE)) {
420ca987
HG
807 if (async) {
808 /* Guest marked a pending td non-active, cancel the queue */
809 uhci_queue_free(async->queue, "pending td non-active");
810 }
883bca77
HG
811 /*
812 * ehci11d spec page 22: "Even if the Active bit in the TD is already
813 * cleared when the TD is fetched ... an IOC interrupt is generated"
814 */
815 if (td->ctrl & TD_CTRL_IOC) {
816 *int_mask |= 0x01;
817 }
60e1b2a6 818 return TD_RESULT_NEXT_QH;
883bca77 819 }
54f254f9 820
f419a626
GH
821 switch (pid) {
822 case USB_TOKEN_OUT:
823 case USB_TOKEN_SETUP:
824 case USB_TOKEN_IN:
825 break;
826 default:
827 /* invalid pid : frame interrupted */
828 s->status |= UHCI_STS_HCPERR;
829 s->cmd &= ~UHCI_CMD_RS;
830 uhci_update_irq(s);
831 return TD_RESULT_STOP_FRAME;
832 }
833
54f254f9 834 if (async) {
ee008ba6
GH
835 if (queuing) {
836 /* we are busy filling the queue, we are not prepared
837 to consume completed packages then, just leave them
838 in async state */
839 return TD_RESULT_ASYNC_CONT;
840 }
8928c9c4
HG
841 if (!async->done) {
842 UHCI_TD last_td;
eae3eb3e 843 UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs);
8928c9c4
HG
844 /*
845 * While we are waiting for the current td to complete, the guest
846 * may have added more tds to the queue. Note we re-read the td
847 * rather then caching it, as we want to see guest made changes!
848 */
849 uhci_read_td(s, &last_td, last->td_addr);
850 uhci_queue_fill(async->queue, &last_td);
54f254f9 851
8928c9c4
HG
852 return TD_RESULT_ASYNC_CONT;
853 }
f8af1e88 854 uhci_async_unlink(async);
54f254f9
AL
855 goto done;
856 }
857
88793816
HG
858 if (s->completions_only) {
859 return TD_RESULT_ASYNC_CONT;
860 }
861
54f254f9 862 /* Allocate new packet */
a4f30cd7 863 if (q == NULL) {
ff668537
LM
864 USBDevice *dev;
865 USBEndpoint *ep;
7f102ebe 866
ff668537
LM
867 dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
868 if (dev == NULL) {
7f102ebe
HG
869 return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV,
870 int_mask);
871 }
ff668537 872 ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
66a08cbe 873 q = uhci_queue_new(s, qh_addr, td, ep);
a4f30cd7
HG
874 }
875 async = uhci_async_alloc(q, td_addr);
54f254f9 876
54f254f9 877 max_len = ((td->token >> 21) + 1) & 0x7ff;
6ba43f1f 878 spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
8550a02d 879 usb_packet_setup(&async->packet, pid, q->ep, 0, td_addr, spd,
a6fb2ddb 880 (td->ctrl & TD_CTRL_IOC) != 0);
9822261c
HG
881 if (max_len <= sizeof(async->static_buf)) {
882 async->buf = async->static_buf;
883 } else {
884 async->buf = g_malloc(max_len);
885 }
886 usb_packet_addbuf(&async->packet, async->buf, max_len);
54f254f9
AL
887
888 switch(pid) {
889 case USB_TOKEN_OUT:
890 case USB_TOKEN_SETUP:
9822261c 891 pci_dma_read(&s->dev, td->buffer, async->buf, max_len);
9a77a0f5
HG
892 usb_handle_packet(q->ep->dev, &async->packet);
893 if (async->packet.status == USB_RET_SUCCESS) {
894 async->packet.actual_length = max_len;
895 }
54f254f9
AL
896 break;
897
898 case USB_TOKEN_IN:
9a77a0f5 899 usb_handle_packet(q->ep->dev, &async->packet);
54f254f9
AL
900 break;
901
902 default:
5f77e06b 903 abort(); /* Never to execute */
54f254f9 904 }
9a77a0f5
HG
905
906 if (async->packet.status == USB_RET_ASYNC) {
f8af1e88 907 uhci_async_link(async);
a4f30cd7 908 if (!queuing) {
11d15e40 909 uhci_queue_fill(q, td);
a4f30cd7 910 }
4efe4ef3 911 return TD_RESULT_ASYNC_START;
54f254f9
AL
912 }
913
54f254f9 914done:
9a77a0f5 915 ret = uhci_complete_td(s, td, async, int_mask);
f8af1e88 916 uhci_async_free(async);
9a77a0f5 917 return ret;
54f254f9
AL
918}
919
d47e59b8 920static void uhci_async_complete(USBPort *port, USBPacket *packet)
4d611c9a 921{
7b5a44c5 922 UHCIAsync *async = container_of(packet, UHCIAsync, packet);
f8af1e88 923 UHCIState *s = async->queue->uhci;
54f254f9 924
9a77a0f5 925 if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
0cae7b1a
HG
926 uhci_async_cancel(async);
927 return;
928 }
929
5b352ed5 930 async->done = 1;
88793816
HG
931 /* Force processing of this packet *now*, needed for migration */
932 s->completions_only = true;
933 qemu_bh_schedule(s->bh);
54f254f9
AL
934}
935
936static int is_valid(uint32_t link)
937{
938 return (link & 1) == 0;
939}
940
941static int is_qh(uint32_t link)
942{
943 return (link & 2) != 0;
944}
945
946static int depth_first(uint32_t link)
947{
948 return (link & 4) != 0;
949}
950
951/* QH DB used for detecting QH loops */
952#define UHCI_MAX_QUEUES 128
953typedef struct {
954 uint32_t addr[UHCI_MAX_QUEUES];
955 int count;
956} QhDb;
957
958static void qhdb_reset(QhDb *db)
959{
960 db->count = 0;
961}
962
963/* Add QH to DB. Returns 1 if already present or DB is full. */
964static int qhdb_insert(QhDb *db, uint32_t addr)
965{
966 int i;
967 for (i = 0; i < db->count; i++)
968 if (db->addr[i] == addr)
969 return 1;
970
971 if (db->count >= UHCI_MAX_QUEUES)
972 return 1;
973
974 db->addr[db->count++] = addr;
975 return 0;
976}
977
11d15e40 978static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
5a248289
GH
979{
980 uint32_t int_mask = 0;
981 uint32_t plink = td->link;
5a248289
GH
982 UHCI_TD ptd;
983 int ret;
984
6ba43f1f 985 while (is_valid(plink)) {
a4f30cd7 986 uhci_read_td(q->uhci, &ptd, plink);
5a248289
GH
987 if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
988 break;
989 }
a4f30cd7 990 if (uhci_queue_token(&ptd) != q->token) {
5a248289
GH
991 break;
992 }
50dcc0f8 993 trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
66a08cbe 994 ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
52b0fecd
GH
995 if (ret == TD_RESULT_ASYNC_CONT) {
996 break;
997 }
4efe4ef3 998 assert(ret == TD_RESULT_ASYNC_START);
5a248289
GH
999 assert(int_mask == 0);
1000 plink = ptd.link;
1001 }
11d15e40 1002 usb_device_flush_ep_queue(q->ep->dev, q->ep);
5a248289
GH
1003}
1004
54f254f9
AL
1005static void uhci_process_frame(UHCIState *s)
1006{
1007 uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
4aed20e2 1008 uint32_t curr_qh, td_count = 0;
54f254f9 1009 int cnt, ret;
4d611c9a 1010 UHCI_TD td;
54f254f9
AL
1011 UHCI_QH qh;
1012 QhDb qhdb;
4d611c9a 1013
54f254f9
AL
1014 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
1015
9fe2fd67 1016 pci_dma_read(&s->dev, frame_addr, &link, 4);
54f254f9 1017 le32_to_cpus(&link);
b9dc033c 1018
54f254f9
AL
1019 int_mask = 0;
1020 curr_qh = 0;
1021
1022 qhdb_reset(&qhdb);
1023
1024 for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
88793816 1025 if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) {
4aed20e2
GH
1026 /* We've reached the usb 1.1 bandwidth, which is
1027 1280 bytes/frame, stop processing */
1028 trace_usb_uhci_frame_stop_bandwidth();
1029 break;
1030 }
54f254f9
AL
1031 if (is_qh(link)) {
1032 /* QH */
50dcc0f8 1033 trace_usb_uhci_qh_load(link & ~0xf);
54f254f9
AL
1034
1035 if (qhdb_insert(&qhdb, link)) {
1036 /*
1037 * We're going in circles. Which is not a bug because
3200d108
GH
1038 * HCD is allowed to do that as part of the BW management.
1039 *
4aed20e2
GH
1040 * Stop processing here if no transaction has been done
1041 * since we've been here last time.
54f254f9 1042 */
3200d108 1043 if (td_count == 0) {
50dcc0f8 1044 trace_usb_uhci_frame_loop_stop_idle();
3200d108 1045 break;
3200d108 1046 } else {
50dcc0f8 1047 trace_usb_uhci_frame_loop_continue();
3200d108
GH
1048 td_count = 0;
1049 qhdb_reset(&qhdb);
1050 qhdb_insert(&qhdb, link);
1051 }
54f254f9
AL
1052 }
1053
9fe2fd67 1054 pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
54f254f9
AL
1055 le32_to_cpus(&qh.link);
1056 le32_to_cpus(&qh.el_link);
1057
54f254f9
AL
1058 if (!is_valid(qh.el_link)) {
1059 /* QH w/o elements */
1060 curr_qh = 0;
1061 link = qh.link;
1062 } else {
1063 /* QH with elements */
72e21db7
PB
1064 curr_qh = link;
1065 link = qh.el_link;
54f254f9
AL
1066 }
1067 continue;
1068 }
1069
1070 /* TD */
963a68b5 1071 uhci_read_td(s, &td, link);
50dcc0f8 1072 trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
54f254f9
AL
1073
1074 old_td_ctrl = td.ctrl;
66a08cbe 1075 ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
b9dc033c 1076 if (old_td_ctrl != td.ctrl) {
54f254f9 1077 /* update the status bits of the TD */
b9dc033c 1078 val = cpu_to_le32(td.ctrl);
9fe2fd67 1079 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
b9dc033c 1080 }
54f254f9 1081
971a5a40 1082 switch (ret) {
60e1b2a6 1083 case TD_RESULT_STOP_FRAME: /* interrupted frame */
971a5a40 1084 goto out;
b9dc033c 1085
60e1b2a6 1086 case TD_RESULT_NEXT_QH:
4efe4ef3 1087 case TD_RESULT_ASYNC_CONT:
50dcc0f8 1088 trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
54f254f9
AL
1089 link = curr_qh ? qh.link : td.link;
1090 continue;
54f254f9 1091
4efe4ef3 1092 case TD_RESULT_ASYNC_START:
50dcc0f8 1093 trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
971a5a40
GH
1094 link = curr_qh ? qh.link : td.link;
1095 continue;
54f254f9 1096
60e1b2a6 1097 case TD_RESULT_COMPLETE:
50dcc0f8 1098 trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
971a5a40
GH
1099 link = td.link;
1100 td_count++;
4aed20e2 1101 s->frame_bytes += (td.ctrl & 0x7ff) + 1;
54f254f9 1102
971a5a40
GH
1103 if (curr_qh) {
1104 /* update QH element link */
1105 qh.el_link = link;
1106 val = cpu_to_le32(qh.el_link);
1107 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
54f254f9 1108
971a5a40
GH
1109 if (!depth_first(link)) {
1110 /* done with this QH */
971a5a40
GH
1111 curr_qh = 0;
1112 link = qh.link;
1113 }
54f254f9 1114 }
971a5a40
GH
1115 break;
1116
1117 default:
1118 assert(!"unknown return code");
4d611c9a 1119 }
54f254f9
AL
1120
1121 /* go to the next entry */
4d611c9a 1122 }
54f254f9 1123
971a5a40 1124out:
8e65b7c0 1125 s->pending_int_mask |= int_mask;
4d611c9a
PB
1126}
1127
9a16c595
GH
1128static void uhci_bh(void *opaque)
1129{
1130 UHCIState *s = opaque;
1131 uhci_process_frame(s);
1132}
1133
bb36d470
FB
1134static void uhci_frame_timer(void *opaque)
1135{
1136 UHCIState *s = opaque;
f8f48b69
HG
1137 uint64_t t_now, t_last_run;
1138 int i, frames;
73bcb24d 1139 const uint64_t frame_t = NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ;
8e65b7c0 1140
88793816 1141 s->completions_only = false;
9a16c595 1142 qemu_bh_cancel(s->bh);
bb36d470
FB
1143
1144 if (!(s->cmd & UHCI_CMD_RS)) {
54f254f9 1145 /* Full stop */
50dcc0f8 1146 trace_usb_uhci_schedule_stop();
bc72ad67 1147 timer_del(s->frame_timer);
d9a528db 1148 uhci_async_cancel_all(s);
52328140
FB
1149 /* set hchalted bit in status - UHCI11D 2.1.2 */
1150 s->status |= UHCI_STS_HCHALTED;
bb36d470
FB
1151 return;
1152 }
54f254f9 1153
f8f48b69
HG
1154 /* We still store expire_time in our state, for migration */
1155 t_last_run = s->expire_time - frame_t;
bc72ad67 1156 t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
54f254f9 1157
f8f48b69
HG
1158 /* Process up to MAX_FRAMES_PER_TICK frames */
1159 frames = (t_now - t_last_run) / frame_t;
9fdf7027
HG
1160 if (frames > s->maxframes) {
1161 int skipped = frames - s->maxframes;
1162 s->expire_time += skipped * frame_t;
1163 s->frnum = (s->frnum + skipped) & 0x7ff;
1164 frames -= skipped;
1165 }
f8f48b69
HG
1166 if (frames > MAX_FRAMES_PER_TICK) {
1167 frames = MAX_FRAMES_PER_TICK;
1168 }
b9dc033c 1169
f8f48b69
HG
1170 for (i = 0; i < frames; i++) {
1171 s->frame_bytes = 0;
1172 trace_usb_uhci_frame_start(s->frnum);
1173 uhci_async_validate_begin(s);
1174 uhci_process_frame(s);
1175 uhci_async_validate_end(s);
1176 /* The spec says frnum is the frame currently being processed, and
1177 * the guest must look at frnum - 1 on interrupt, so inc frnum now */
1178 s->frnum = (s->frnum + 1) & 0x7ff;
1179 s->expire_time += frame_t;
1180 }
719c130d 1181
f8f48b69 1182 /* Complete the previous frame(s) */
719c130d
HG
1183 if (s->pending_int_mask) {
1184 s->status2 |= s->pending_int_mask;
1185 s->status |= UHCI_STS_USBINT;
1186 uhci_update_irq(s);
1187 }
1188 s->pending_int_mask = 0;
1189
bc72ad67 1190 timer_mod(s->frame_timer, t_now + frame_t);
bb36d470
FB
1191}
1192
a03f66e4 1193static const MemoryRegionOps uhci_ioport_ops = {
89eb147c
GH
1194 .read = uhci_port_read,
1195 .write = uhci_port_write,
1196 .valid.min_access_size = 1,
1197 .valid.max_access_size = 4,
1198 .impl.min_access_size = 2,
1199 .impl.max_access_size = 2,
1200 .endianness = DEVICE_LITTLE_ENDIAN,
a03f66e4 1201};
bb36d470 1202
0d86d2be
GH
1203static USBPortOps uhci_port_ops = {
1204 .attach = uhci_attach,
618c169b 1205 .detach = uhci_detach,
4706ab6c 1206 .child_detach = uhci_child_detach,
9159f679 1207 .wakeup = uhci_wakeup,
13a9a0d3 1208 .complete = uhci_async_complete,
0d86d2be
GH
1209};
1210
07771f6f 1211static USBBusOps uhci_bus_ops = {
07771f6f
GH
1212};
1213
63216dc7 1214static void usb_uhci_common_realize(PCIDevice *dev, Error **errp)
bb36d470 1215{
f4bbaaf5 1216 Error *err = NULL;
973002c1 1217 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
8f3f90b0 1218 UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class);
49184b62 1219 UHCIState *s = UHCI(dev);
6cf9b6f1 1220 uint8_t *pci_conf = s->dev.config;
bb36d470
FB
1221 int i;
1222
db579e9e 1223 pci_conf[PCI_CLASS_PROG] = 0x00;
db579e9e 1224 /* TODO: reset value should be 0. */
e59d33a7 1225 pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
3b46e624 1226
9e64f8a3 1227 pci_config_set_interrupt_pin(pci_conf, u->info.irq_pin + 1);
973002c1 1228
35e4977f
HG
1229 if (s->masterbus) {
1230 USBPort *ports[NB_PORTS];
1231 for(i = 0; i < NB_PORTS; i++) {
1232 ports[i] = &s->ports[i].port;
1233 }
f4bbaaf5
MA
1234 usb_register_companion(s->masterbus, ports, NB_PORTS,
1235 s->firstport, s, &uhci_port_ops,
1236 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL,
1237 &err);
1238 if (err) {
63216dc7
MA
1239 error_propagate(errp, err);
1240 return;
35e4977f
HG
1241 }
1242 } else {
c889b3a5 1243 usb_bus_new(&s->bus, sizeof(s->bus), &uhci_bus_ops, DEVICE(dev));
35e4977f
HG
1244 for (i = 0; i < NB_PORTS; i++) {
1245 usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1246 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1247 }
bb36d470 1248 }
9a16c595 1249 s->bh = qemu_bh_new(uhci_bh, s);
bc72ad67 1250 s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, uhci_frame_timer, s);
64e58fe5 1251 s->num_ports_vmstate = NB_PORTS;
f8af1e88 1252 QTAILQ_INIT(&s->queues);
bb36d470 1253
22fc860b
PB
1254 memory_region_init_io(&s->io_bar, OBJECT(s), &uhci_ioport_ops, s,
1255 "uhci", 0x20);
1256
38ca0f6d
PB
1257 /* Use region 4 for consistency with real hardware. BSD guests seem
1258 to rely on this. */
e824b2cc 1259 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
bb36d470 1260}
afcc3cdf 1261
63216dc7 1262static void usb_uhci_vt82c686b_realize(PCIDevice *dev, Error **errp)
30235a54 1263{
49184b62 1264 UHCIState *s = UHCI(dev);
30235a54
HC
1265 uint8_t *pci_conf = s->dev.config;
1266
30235a54
HC
1267 /* USB misc control 1/2 */
1268 pci_set_long(pci_conf + 0x40,0x00001000);
1269 /* PM capability */
1270 pci_set_long(pci_conf + 0x80,0x00020001);
1271 /* USB legacy support */
1272 pci_set_long(pci_conf + 0xc0,0x00002000);
1273
63216dc7 1274 usb_uhci_common_realize(dev, errp);
30235a54
HC
1275}
1276
3a3464b0
GA
1277static void usb_uhci_exit(PCIDevice *dev)
1278{
49184b62 1279 UHCIState *s = UHCI(dev);
3a3464b0 1280
d733f74c
GA
1281 trace_usb_uhci_exit();
1282
3a3464b0
GA
1283 if (s->frame_timer) {
1284 timer_del(s->frame_timer);
1285 timer_free(s->frame_timer);
1286 s->frame_timer = NULL;
1287 }
1288
1289 if (s->bh) {
1290 qemu_bh_delete(s->bh);
1291 }
1292
1293 uhci_async_cancel_all(s);
1294
1295 if (!s->masterbus) {
1296 usb_bus_release(&s->bus);
1297 }
1298}
1299
638ca939 1300static Property uhci_properties_companion[] = {
1b5a7570
GH
1301 DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1302 DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
40141d12 1303 DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
9fdf7027 1304 DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128),
1b5a7570
GH
1305 DEFINE_PROP_END_OF_LIST(),
1306};
638ca939
GH
1307static Property uhci_properties_standalone[] = {
1308 DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1309 DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128),
1310 DEFINE_PROP_END_OF_LIST(),
1311};
1b5a7570 1312
2c2e8525 1313static void uhci_class_init(ObjectClass *klass, void *data)
40021f08 1314{
39bffca2 1315 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08 1316 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
49184b62
GA
1317
1318 k->class_id = PCI_CLASS_SERIAL_USB;
1319 dc->vmsd = &vmstate_uhci;
1320 dc->reset = uhci_reset;
1321 set_bit(DEVICE_CATEGORY_USB, dc->categories);
1322}
1323
1324static const TypeInfo uhci_pci_type_info = {
1325 .name = TYPE_UHCI,
1326 .parent = TYPE_PCI_DEVICE,
1327 .instance_size = sizeof(UHCIState),
1328 .class_size = sizeof(UHCIPCIDeviceClass),
1329 .abstract = true,
1330 .class_init = uhci_class_init,
fd3b02c8
EH
1331 .interfaces = (InterfaceInfo[]) {
1332 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1333 { },
1334 },
49184b62
GA
1335};
1336
1337static void uhci_data_class_init(ObjectClass *klass, void *data)
1338{
1339 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1340 DeviceClass *dc = DEVICE_CLASS(klass);
8f3f90b0 1341 UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class);
2c2e8525
GH
1342 UHCIInfo *info = data;
1343
63216dc7 1344 k->realize = info->realize ? info->realize : usb_uhci_common_realize;
3a3464b0 1345 k->exit = info->unplug ? usb_uhci_exit : NULL;
2c2e8525
GH
1346 k->vendor_id = info->vendor_id;
1347 k->device_id = info->device_id;
1348 k->revision = info->revision;
638ca939
GH
1349 if (!info->unplug) {
1350 /* uhci controllers in companion setups can't be hotplugged */
1351 dc->hotpluggable = false;
4f67d30b 1352 device_class_set_props(dc, uhci_properties_companion);
638ca939 1353 } else {
4f67d30b 1354 device_class_set_props(dc, uhci_properties_standalone);
638ca939 1355 }
8f3f90b0 1356 u->info = *info;
40021f08
AL
1357}
1358
2c2e8525
GH
1359static UHCIInfo uhci_info[] = {
1360 {
1361 .name = "piix3-usb-uhci",
1362 .vendor_id = PCI_VENDOR_ID_INTEL,
1363 .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
1364 .revision = 0x01,
8f3f90b0 1365 .irq_pin = 3,
2c2e8525
GH
1366 .unplug = true,
1367 },{
1368 .name = "piix4-usb-uhci",
1369 .vendor_id = PCI_VENDOR_ID_INTEL,
1370 .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
1371 .revision = 0x01,
8f3f90b0 1372 .irq_pin = 3,
2c2e8525
GH
1373 .unplug = true,
1374 },{
1375 .name = "vt82c686b-usb-uhci",
1376 .vendor_id = PCI_VENDOR_ID_VIA,
1377 .device_id = PCI_DEVICE_ID_VIA_UHCI,
1378 .revision = 0x01,
8f3f90b0 1379 .irq_pin = 3,
63216dc7 1380 .realize = usb_uhci_vt82c686b_realize,
2c2e8525
GH
1381 .unplug = true,
1382 },{
74625ea2 1383 .name = "ich9-usb-uhci1", /* 00:1d.0 */
2c2e8525
GH
1384 .vendor_id = PCI_VENDOR_ID_INTEL,
1385 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
1386 .revision = 0x03,
8f3f90b0 1387 .irq_pin = 0,
2c2e8525
GH
1388 .unplug = false,
1389 },{
74625ea2 1390 .name = "ich9-usb-uhci2", /* 00:1d.1 */
2c2e8525
GH
1391 .vendor_id = PCI_VENDOR_ID_INTEL,
1392 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
1393 .revision = 0x03,
8f3f90b0 1394 .irq_pin = 1,
2c2e8525
GH
1395 .unplug = false,
1396 },{
74625ea2 1397 .name = "ich9-usb-uhci3", /* 00:1d.2 */
2c2e8525
GH
1398 .vendor_id = PCI_VENDOR_ID_INTEL,
1399 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
1400 .revision = 0x03,
8f3f90b0 1401 .irq_pin = 2,
2c2e8525 1402 .unplug = false,
74625ea2
GH
1403 },{
1404 .name = "ich9-usb-uhci4", /* 00:1a.0 */
1405 .vendor_id = PCI_VENDOR_ID_INTEL,
1406 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4,
1407 .revision = 0x03,
1408 .irq_pin = 0,
1409 .unplug = false,
1410 },{
1411 .name = "ich9-usb-uhci5", /* 00:1a.1 */
1412 .vendor_id = PCI_VENDOR_ID_INTEL,
1413 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5,
1414 .revision = 0x03,
1415 .irq_pin = 1,
1416 .unplug = false,
1417 },{
1418 .name = "ich9-usb-uhci6", /* 00:1a.2 */
1419 .vendor_id = PCI_VENDOR_ID_INTEL,
1420 .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6,
1421 .revision = 0x03,
1422 .irq_pin = 2,
1423 .unplug = false,
2c2e8525 1424 }
6cf9b6f1 1425};
afcc3cdf 1426
83f7d43a 1427static void uhci_register_types(void)
6cf9b6f1 1428{
2c2e8525 1429 TypeInfo uhci_type_info = {
49184b62
GA
1430 .parent = TYPE_UHCI,
1431 .class_init = uhci_data_class_init,
2c2e8525
GH
1432 };
1433 int i;
1434
49184b62
GA
1435 type_register_static(&uhci_pci_type_info);
1436
2c2e8525
GH
1437 for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
1438 uhci_type_info.name = uhci_info[i].name;
1439 uhci_type_info.class_data = uhci_info + i;
1440 type_register(&uhci_type_info);
1441 }
6cf9b6f1 1442}
83f7d43a
AF
1443
1444type_init(uhci_register_types)