1
// Code for handling XHCI "Super speed" USB controllers.
3
// Copyright (C) 2013 Gerd Hoffmann <kraxel@redhat.com>
4
// Copyright (C) 2014 Kevin O'Connor <kevin@koconnor.net>
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
8
#include "biosvar.h" // GET_LOWFLAT
1
9
#include "config.h" // CONFIG_*
10
#include "malloc.h" // memalign_low
11
#include "memmap.h" // PAGE_SIZE
2
12
#include "output.h" // dprintf
3
#include "string.h" // memcpy_fl
4
#include "util.h" // timer_calc
5
#include "x86.h" // readl
6
#include "malloc.h" // memalign_low
7
13
#include "pci.h" // pci_bdf_to_bus
14
#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_XHCI
8
15
#include "pci_regs.h" // PCI_BASE_ADDRESS_0
16
#include "string.h" // memcpy_fl
9
17
#include "usb.h" // struct usb_s
10
18
#include "usb-xhci.h" // struct ehci_qh
11
#include "biosvar.h" // GET_LOWFLAT
19
#include "util.h" // timer_calc
20
#include "x86.h" // readl
13
22
// --------------------------------------------------------------
457
447
static void xhci_xfer_kick(struct xhci_pipe *pipe)
459
struct xhci_device *dev = GET_LOWFLAT(pipe->dev);
460
struct usb_xhci_s *xhci = GET_LOWFLAT(dev->xhci);
461
u32 slotid = GET_LOWFLAT(dev->slotid);
449
struct usb_xhci_s *xhci = container_of(
450
GET_LOWFLAT(pipe->pipe.cntl), struct usb_xhci_s, usb);
451
u32 slotid = GET_LOWFLAT(pipe->slotid);
462
452
u32 epid = GET_LOWFLAT(pipe->epid);
464
454
dprintf(5, "%s: ring %p, slotid %d, epid %d\n",
529
519
return (xhci->cmds->evt.control >> 24) & 0xff;
532
static int xhci_cmd_disable_slot(struct xhci_device *dev)
523
static int xhci_cmd_disable_slot(struct usb_xhci_s *xhci, u32 slotid)
535
526
struct xhci_trb cmd = {
539
.control = (dev->slotid << 24) | (CR_DISABLE_SLOT << 10)
541
dprintf(3, "%s: slotid %d\n", __func__, dev->slotid);
542
return xhci_cmd_submit(dev->xhci, &cmd);
545
static int xhci_cmd_address_device(struct xhci_device *dev)
548
struct xhci_trb cmd = {
549
.ptr_low = (u32)&dev->inctx,
552
.control = (dev->slotid << 24) | (CR_ADDRESS_DEVICE << 10)
554
dprintf(3, "%s: slotid %d\n", __func__, dev->slotid);
555
return xhci_cmd_submit(dev->xhci, &cmd);
558
static int xhci_cmd_configure_endpoint(struct xhci_device *dev)
561
struct xhci_trb cmd = {
562
.ptr_low = (u32)&dev->inctx,
565
.control = (dev->slotid << 24) | (CR_CONFIGURE_ENDPOINT << 10)
567
dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
568
dev->slotid, dev->inctx.add, dev->inctx.del);
569
return xhci_cmd_submit(dev->xhci, &cmd);
572
static int xhci_cmd_evaluate_context(struct xhci_device *dev)
575
struct xhci_trb cmd = {
576
.ptr_low = (u32)&dev->inctx,
579
.control = (dev->slotid << 24) | (CR_EVALUATE_CONTEXT << 10)
581
dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
582
dev->slotid, dev->inctx.add, dev->inctx.del);
583
return xhci_cmd_submit(dev->xhci, &cmd);
530
.control = (slotid << 24) | (CR_DISABLE_SLOT << 10)
532
dprintf(3, "%s: slotid %d\n", __func__, slotid);
533
return xhci_cmd_submit(xhci, &cmd);
537
static int xhci_cmd_address_device(struct usb_xhci_s *xhci, u32 slotid
538
, struct xhci_inctx *inctx)
541
struct xhci_trb cmd = {
542
.ptr_low = (u32)inctx,
545
.control = (slotid << 24) | (CR_ADDRESS_DEVICE << 10)
547
dprintf(3, "%s: slotid %d\n", __func__, slotid);
548
return xhci_cmd_submit(xhci, &cmd);
551
static int xhci_cmd_configure_endpoint(struct usb_xhci_s *xhci, u32 slotid
552
, struct xhci_inctx *inctx)
555
struct xhci_trb cmd = {
556
.ptr_low = (u32)inctx,
559
.control = (slotid << 24) | (CR_CONFIGURE_ENDPOINT << 10)
561
dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
562
slotid, inctx->add, inctx->del);
563
return xhci_cmd_submit(xhci, &cmd);
566
static int xhci_cmd_evaluate_context(struct usb_xhci_s *xhci, u32 slotid
567
, struct xhci_inctx *inctx)
570
struct xhci_trb cmd = {
571
.ptr_low = (u32)inctx,
574
.control = (slotid << 24) | (CR_EVALUATE_CONTEXT << 10)
576
dprintf(3, "%s: slotid %d, add 0x%x, del 0x%x\n", __func__,
577
slotid, inctx->add, inctx->del);
578
return xhci_cmd_submit(xhci, &cmd);
586
581
static void xhci_xfer_setup(struct xhci_pipe *pipe,
627
622
memset(&trb, 0, sizeof(trb));
628
623
trb.control |= (TR_STATUS << 10); // trb type
629
624
trb.control |= TRB_TR_IOC;
625
if (!datalen || !dir)
631
626
trb.control |= (1 << 16);
633
628
xhci_xfer_queue(pipe, &trb);
634
629
xhci_xfer_kick(pipe);
637
static struct xhci_device *xhci_find_alloc_device(struct usb_xhci_s *xhci,
638
struct usbdevice_s *usbdev)
641
struct xhci_device *dev;
643
hlist_for_each_entry(dev, &xhci->list, next) {
644
if (dev->usbdev == usbdev) {
649
dev = memalign_low(64, sizeof(*dev));
654
memset(dev, 0, sizeof(*dev));
655
dev->usbdev = usbdev;
657
hlist_add_head(&dev->next, &xhci->list);
662
633
configure_xhci(void *data)
710
681
writel(&xhci->ir->erstba_high, 0);
711
682
xhci->evts->cs = 1;
684
reg = readl(&xhci->caps->hcsparams2);
687
dprintf(3, "%s: setup %d scratch pad buffers\n", __func__, spb);
688
u64 *spba = memalign_high(64, sizeof(*spba) * spb);
689
void *pad = memalign_high(PAGE_SIZE, PAGE_SIZE * spb);
697
for (i = 0; i < spb; i++)
698
spba[i] = (u32)pad + (i * PAGE_SIZE);
699
xhci->devs[0].ptr_low = (u32)spba;
700
xhci->devs[0].ptr_high = 0;
713
703
reg = readl(&xhci->op->usbcmd);
714
704
reg |= XHCI_CMD_RS;
715
705
writel(&xhci->op->usbcmd, reg);
717
707
// FIXME: try find a more elegant way than a fixed delay
720
710
usb_enumerate(&xhci->hub);
711
// XXX - should walk list of pipes and free unused pipes.
721
712
if (xhci->hub.devcount)
829
809
// --------------------------------------------------------------
830
810
// external interface
813
static struct xhci_inctx *
814
xhci_alloc_inctx(struct usbdevice_s *usbdev, int maxepid)
816
struct usb_xhci_s *xhci = container_of(
817
usbdev->hub->cntl, struct usb_xhci_s, usb);
818
int size = (sizeof(struct xhci_inctx) * 33) << xhci->context64;
819
struct xhci_inctx *in = memalign_tmphigh(2048 << xhci->context64, size);
826
struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
827
slot->ctx[0] |= maxepid << 27; // context entries
828
slot->ctx[0] |= speed_to_xhci[usbdev->speed] << 20;
830
// Set high-speed hub flags.
831
struct usbdevice_s *hubdev = usbdev->hub->usbdev;
833
if (usbdev->speed == USB_LOWSPEED || usbdev->speed == USB_FULLSPEED) {
834
struct xhci_pipe *hpipe = container_of(
835
hubdev->defpipe, struct xhci_pipe, pipe);
836
if (hubdev->speed == USB_HIGHSPEED) {
837
slot->ctx[2] |= hpipe->slotid;
838
slot->ctx[2] |= (usbdev->port+1) << 8;
840
struct xhci_slotctx *hslot = (void*)xhci->devs[hpipe->slotid].ptr_low;
841
slot->ctx[2] = hslot->ctx[2];
845
while (usbdev->hub->usbdev) {
847
route |= (usbdev->port+1) & 0xf;
848
usbdev = usbdev->hub->usbdev;
850
slot->ctx[0] |= route;
853
slot->ctx[1] |= (usbdev->port+1) << 16;
858
static int xhci_config_hub(struct usbhub_s *hub)
860
struct usb_xhci_s *xhci = container_of(
861
hub->cntl, struct usb_xhci_s, usb);
862
struct xhci_pipe *pipe = container_of(
863
hub->usbdev->defpipe, struct xhci_pipe, pipe);
864
struct xhci_slotctx *hdslot = (void*)xhci->devs[pipe->slotid].ptr_low;
865
if ((hdslot->ctx[3] >> 27) == 3)
866
// Already configured
868
struct xhci_inctx *in = xhci_alloc_inctx(hub->usbdev, 1);
872
struct xhci_slotctx *slot = (void*)&in[1 << xhci->context64];
873
slot->ctx[0] |= 1 << 26;
874
slot->ctx[1] |= hub->portcount << 24;
876
int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
878
if (cc != CC_SUCCESS) {
879
dprintf(1, "%s: configure hub: failed (cc %d)\n", __func__, cc);
832
885
struct usb_pipe *
833
886
xhci_alloc_pipe(struct usbdevice_s *usbdev
834
887
, struct usb_endpoint_descriptor *epdesc)
860
913
memset(pipe, 0, sizeof(*pipe));
862
915
usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
863
pipe->dev = xhci_find_alloc_device(xhci, usbdev);
868
916
pipe->epid = epid;
869
917
pipe->reqs.cs = 1;
870
918
if (eptype == USB_ENDPOINT_XFER_INT)
871
919
pipe->buf = malloc_low(pipe->pipe.maxpacket);
921
// Allocate input context and initialize endpoint info.
922
struct xhci_inctx *in = xhci_alloc_inctx(usbdev, epid);
925
in->add = 0x01 | (1 << epid);
926
struct xhci_epctx *ep = (void*)&in[(pipe->epid+1) << xhci->context64];
927
if (eptype == USB_ENDPOINT_XFER_INT)
928
ep->ctx[0] = (usb_getFrameExp(usbdev, epdesc) + 3) << 16;
929
ep->ctx[1] |= eptype << 3;
930
if (epdesc->bEndpointAddress & USB_DIR_IN
931
|| eptype == USB_ENDPOINT_XFER_CONTROL)
932
ep->ctx[1] |= 1 << 5;
933
ep->ctx[1] |= pipe->pipe.maxpacket << 16;
934
ep->deq_low = (u32)&pipe->reqs.ring[0];
935
ep->deq_low |= 1; // dcs
936
ep->length = pipe->pipe.maxpacket;
873
938
dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
874
usbdev, &pipe->reqs, pipe->dev->slotid, pipe->epid);
875
if (pipe->epid > 1 && pipe->dev->slotid) {
876
struct xhci_inctx *in = &pipe->dev->inctx;
877
in->add = (1 << pipe->epid) | 1;
880
in->slot.ctx[0] |= (31 << 27); // context entries
882
int e = pipe->epid-1;
883
in->ep[e].ctx[1] |= (eptype << 3);
884
if (epdesc->bEndpointAddress & USB_DIR_IN)
885
in->ep[e].ctx[1] |= (1 << 5);
886
in->ep[e].ctx[1] |= (pipe->pipe.maxpacket << 16);
887
in->ep[e].deq_low = (u32)&pipe->reqs.ring[0];
888
in->ep[e].deq_low |= 1; // dcs
889
in->ep[e].deq_high = 0;
890
in->ep[e].length = pipe->pipe.maxpacket;
892
int cc = xhci_cmd_configure_endpoint(pipe->dev);
939
usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
940
if (pipe->epid == 1) {
941
if (usbdev->hub->usbdev) {
942
// Make sure parent hub is configured.
943
int ret = xhci_config_hub(usbdev->hub);
948
u32 size = (sizeof(struct xhci_slotctx) * 32) << xhci->context64;
949
struct xhci_slotctx *dev = memalign_high(1024 << xhci->context64, size);
954
int slotid = xhci_cmd_enable_slot(xhci);
956
dprintf(1, "%s: enable slot: failed\n", __func__);
960
dprintf(3, "%s: enable slot: got slotid %d\n", __func__, slotid);
961
memset(dev, 0, size);
962
pipe->slotid = usbdev->slotid = slotid;
963
xhci->devs[slotid].ptr_low = (u32)dev;
964
xhci->devs[slotid].ptr_high = 0;
966
// Send set_address command.
967
int cc = xhci_cmd_address_device(xhci, slotid, in);
968
if (cc != CC_SUCCESS) {
969
dprintf(1, "%s: address device: failed (cc %d)\n", __func__, cc);
973
pipe->slotid = usbdev->slotid;
974
// Send configure command.
975
int cc = xhci_cmd_configure_endpoint(xhci, pipe->slotid, in);
893
976
if (cc != CC_SUCCESS) {
894
977
dprintf(1, "%s: configure endpoint: failed (cc %d)\n", __func__, cc);
900
982
return &pipe->pipe;
903
990
struct usb_pipe *
910
997
u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
911
998
struct xhci_pipe *pipe = container_of(upipe, struct xhci_pipe, pipe);
999
struct usb_xhci_s *xhci = container_of(
1000
pipe->pipe.cntl, struct usb_xhci_s, usb);
912
1001
dprintf(3, "%s: usbdev %p, ring %p, slotid %d, epid %d\n", __func__,
913
usbdev, &pipe->reqs, pipe->dev->slotid, pipe->epid);
1002
usbdev, &pipe->reqs, pipe->slotid, pipe->epid);
914
1003
if (eptype == USB_ENDPOINT_XFER_CONTROL &&
915
pipe->pipe.maxpacket != epdesc->wMaxPacketSize) {
1004
pipe->pipe.maxpacket != epdesc->wMaxPacketSize) {
916
1005
dprintf(1, "%s: reconf ctl endpoint pkt size: %d -> %d\n",
917
1006
__func__, pipe->pipe.maxpacket, epdesc->wMaxPacketSize);
918
1007
pipe->pipe.maxpacket = epdesc->wMaxPacketSize;
919
struct xhci_inctx *in = &pipe->dev->inctx;
1008
struct xhci_inctx *in = xhci_alloc_inctx(usbdev, 1);
920
1011
in->add = (1 << 1);
922
in->ep[0].ctx[1] &= 0xffff;
923
in->ep[0].ctx[1] |= (pipe->pipe.maxpacket << 16);
924
int cc = xhci_cmd_evaluate_context(pipe->dev);
1012
struct xhci_epctx *ep = (void*)&in[2 << xhci->context64];
1013
ep->ctx[1] |= (pipe->pipe.maxpacket << 16);
1014
int cc = xhci_cmd_evaluate_context(xhci, pipe->slotid, in);
925
1015
if (cc != CC_SUCCESS) {
926
1016
dprintf(1, "%s: reconf ctl endpoint: failed (cc %d)\n",
940
1031
const struct usb_ctrlrequest *req = cmd;
941
1032
struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
942
struct usb_xhci_s *xhci = pipe->dev->xhci;
945
if (req->bRequest == USB_REQ_SET_ADDRESS) {
946
int slotid = xhci_cmd_enable_slot(xhci);
948
dprintf(1, "%s: enable slot: failed\n", __func__);
951
dprintf(3, "%s: enable slot: got slotid %d\n", __func__, slotid);
952
pipe->dev->slotid = slotid;
953
xhci->devs[slotid].ptr_low = (u32)&pipe->dev->devctx;
954
xhci->devs[slotid].ptr_high = 0;
956
struct usbdevice_s *usbdev = pipe->dev->usbdev;
958
while (usbdev->hub->usbdev) {
960
route |= (usbdev->port+1) & 0xf;
961
usbdev = usbdev->hub->usbdev;
963
dprintf(3, "%s: root port %d, route 0x%x\n",
964
__func__, usbdev->port+1, route);
966
struct xhci_inctx *in = &pipe->dev->inctx;
968
in->slot.ctx[0] |= (1 << 27); // context entries
969
in->slot.ctx[0] |= speed_to_xhci[pipe->dev->usbdev->speed] << 20;
970
in->slot.ctx[0] |= route;
971
in->slot.ctx[1] |= (usbdev->port+1) << 16;
972
/* TODO ctx0: hub bit */
973
/* TODO ctx1: hub ports */
975
in->ep[0].ctx[0] |= (3 << 16); // interval: 1ms
976
in->ep[0].ctx[1] |= (4 << 3); // control pipe
977
in->ep[0].ctx[1] |= (speed_to_ctlsize[pipe->dev->usbdev->speed] << 16);
979
in->ep[0].deq_low = (u32)&pipe->reqs.ring[0];
980
in->ep[0].deq_low |= 1; // dcs
981
in->ep[0].deq_high = 0;
982
in->ep[0].length = 8;
984
cc = xhci_cmd_address_device(pipe->dev);
985
if (cc != CC_SUCCESS) {
986
dprintf(1, "%s: address device: failed (cc %d)\n", __func__, cc);
1033
struct usb_xhci_s *xhci = container_of(
1034
pipe->pipe.cntl, struct usb_xhci_s, usb);
1036
if (req->bRequest == USB_REQ_SET_ADDRESS)
1037
// Set address command sent during xhci_alloc_pipe.
992
1040
xhci_xfer_setup(pipe, req, dir, datalen);
994
1042
xhci_xfer_data(pipe, dir, data, datalen);
995
xhci_xfer_status(pipe, dir);
1043
xhci_xfer_status(pipe, dir, datalen);
997
cc = xhci_event_wait(xhci, &pipe->reqs, 1000);
1045
int cc = xhci_event_wait(xhci, &pipe->reqs, 1000);
998
1046
if (cc != CC_SUCCESS) {
999
1047
dprintf(1, "%s: control xfer failed (cc %d)\n", __func__, cc);
1007
1055
xhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datalen)
1009
1057
if (!CONFIG_USB_XHCI)
1012
1060
struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1013
struct xhci_device *dev = GET_LOWFLAT(pipe->dev);
1014
struct usb_xhci_s *xhci = GET_LOWFLAT(dev->xhci);
1061
struct usb_xhci_s *xhci = container_of(
1062
GET_LOWFLAT(pipe->pipe.cntl), struct usb_xhci_s, usb);
1016
1064
xhci_xfer_normal(pipe, data, datalen);
1017
1065
int cc = xhci_event_wait(xhci, &pipe->reqs, 1000);
1026
1074
xhci_poll_intr(struct usb_pipe *p, void *data)
1028
1076
if (!CONFIG_USB_XHCI)
1031
1079
struct xhci_pipe *pipe = container_of(p, struct xhci_pipe, pipe);
1032
struct xhci_device *dev = GET_LOWFLAT(pipe->dev);
1033
struct usb_xhci_s *xhci = GET_LOWFLAT(dev->xhci);
1080
struct usb_xhci_s *xhci = container_of(
1081
GET_LOWFLAT(pipe->pipe.cntl), struct usb_xhci_s, usb);
1034
1082
u32 len = GET_LOWFLAT(pipe->pipe.maxpacket);
1035
1083
void *buf = GET_LOWFLAT(pipe->buf);
1036
1084
int bufused = GET_LOWFLAT(pipe->bufused);
1081
1126
xhci->ports = (hcs1 >> 24) & 0xff;
1082
1127
xhci->slots = hcs1 & 0xff;
1083
1128
xhci->xcap = ((hcc >> 16) & 0xffff) << 2;
1129
xhci->context64 = (hcc & 0x04) ? 1 : 0;
1085
xhci->usb.busid = busid;
1086
1131
xhci->usb.pci = pci;
1087
1132
xhci->usb.type = USB_TYPE_XHCI;
1088
1133
xhci->hub.cntl = &xhci->usb;
1089
1134
xhci->hub.portcount = xhci->ports;
1090
1135
xhci->hub.op = &xhci_hub_ops;
1092
dprintf(1, "XHCI init on dev %02x:%02x.%x: regs @ %p, %d ports, %d slots\n"
1137
dprintf(1, "XHCI init on dev %02x:%02x.%x: regs @ %p, %d ports, %d slots"
1138
", %d byte contexts\n"
1093
1139
, pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf)
1094
1140
, pci_bdf_to_fn(pci->bdf), xhci->caps
1095
, xhci->ports, xhci->slots);
1141
, xhci->ports, xhci->slots, xhci->context64 ? 64 : 32);
1097
1143
if (xhci->xcap) {
1098
1144
u32 off, addr = xhci->baseaddr + xhci->xcap;