[5cbccd4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Ondrej Hlavaty
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup drvusbxhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief The host controller data bookkeeping.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
[cb89430] | 37 | #include <str_error.h>
|
---|
[5cbccd4] | 38 | #include <usb/debug.h>
|
---|
[5fd9c30] | 39 | #include <usb/host/endpoint.h>
|
---|
[5cbccd4] | 40 | #include "debug.h"
|
---|
| 41 | #include "hc.h"
|
---|
[7bd99bf] | 42 | #include "rh.h"
|
---|
[cb89430] | 43 | #include "hw_struct/trb.h"
|
---|
[0206d35] | 44 | #include "hw_struct/context.h"
|
---|
| 45 | #include "endpoint.h"
|
---|
[e9e24f2] | 46 | #include "transfers.h"
|
---|
| 47 | #include "trb_ring.h"
|
---|
[5cbccd4] | 48 |
|
---|
[91ca111] | 49 | /**
|
---|
| 50 | * Default USB Speed ID mapping: Table 157
|
---|
| 51 | */
|
---|
| 52 | #define PSI_TO_BPS(psie, psim) (((uint64_t) psim) << (10 * psie))
|
---|
[f668d60] | 53 | #define PORT_SPEED(usb, mjr, psie, psim) { \
|
---|
[816335c] | 54 | .name = "USB ", \
|
---|
| 55 | .major = mjr, \
|
---|
| 56 | .minor = 0, \
|
---|
[f668d60] | 57 | .usb_speed = USB_SPEED_##usb, \
|
---|
[91ca111] | 58 | .rx_bps = PSI_TO_BPS(psie, psim), \
|
---|
| 59 | .tx_bps = PSI_TO_BPS(psie, psim) \
|
---|
| 60 | }
|
---|
[a75f9cbc] | 61 |
|
---|
| 62 | static const xhci_port_speed_t default_psiv_to_port_speed [] = {
|
---|
| 63 | [1] = PORT_SPEED(FULL, 2, 2, 12),
|
---|
| 64 | [2] = PORT_SPEED(LOW, 2, 1, 1500),
|
---|
| 65 | [3] = PORT_SPEED(HIGH, 2, 2, 480),
|
---|
| 66 | [4] = PORT_SPEED(SUPER, 3, 3, 5),
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | static const unsigned usb_speed_to_psiv [] = {
|
---|
| 70 | [USB_SPEED_FULL] = 1,
|
---|
| 71 | [USB_SPEED_LOW] = 2,
|
---|
| 72 | [USB_SPEED_HIGH] = 3,
|
---|
| 73 | [USB_SPEED_SUPER] = 4,
|
---|
| 74 | };
|
---|
[91ca111] | 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Walk the list of extended capabilities.
|
---|
[eb928c4] | 78 | *
|
---|
| 79 | * The most interesting thing hidden in extended capabilities is the mapping of
|
---|
| 80 | * ports to protocol versions and speeds.
|
---|
[91ca111] | 81 | */
|
---|
| 82 | static int hc_parse_ec(xhci_hc_t *hc)
|
---|
| 83 | {
|
---|
[816335c] | 84 | unsigned psic, major, minor;
|
---|
| 85 | xhci_sp_name_t name;
|
---|
| 86 |
|
---|
[f668d60] | 87 | xhci_port_speed_t *speeds = hc->speeds;
|
---|
[91ca111] | 88 |
|
---|
| 89 | for (xhci_extcap_t *ec = hc->xecp; ec; ec = xhci_extcap_next(ec)) {
|
---|
| 90 | xhci_dump_extcap(ec);
|
---|
| 91 | switch (XHCI_REG_RD(ec, XHCI_EC_CAP_ID)) {
|
---|
| 92 | case XHCI_EC_USB_LEGACY:
|
---|
| 93 | assert(hc->legsup == NULL);
|
---|
| 94 | hc->legsup = (xhci_legsup_t *) ec;
|
---|
| 95 | break;
|
---|
| 96 | case XHCI_EC_SUPPORTED_PROTOCOL:
|
---|
| 97 | psic = XHCI_REG_RD(ec, XHCI_EC_SP_PSIC);
|
---|
| 98 | major = XHCI_REG_RD(ec, XHCI_EC_SP_MAJOR);
|
---|
[816335c] | 99 | minor = XHCI_REG_RD(ec, XHCI_EC_SP_MINOR);
|
---|
| 100 | name.packed = host2uint32_t_le(XHCI_REG_RD(ec, XHCI_EC_SP_NAME));
|
---|
| 101 |
|
---|
| 102 | if (name.packed != xhci_name_usb.packed) {
|
---|
| 103 | /**
|
---|
| 104 | * The detection of such protocol would work,
|
---|
| 105 | * but the rest of the implementation is made
|
---|
| 106 | * for the USB protocol only.
|
---|
| 107 | */
|
---|
| 108 | usb_log_error("Unknown protocol %.4s.", name.str);
|
---|
| 109 | return ENOTSUP;
|
---|
| 110 | }
|
---|
[91ca111] | 111 |
|
---|
[a9fcd73] | 112 | unsigned offset = XHCI_REG_RD(ec, XHCI_EC_SP_CP_OFF);
|
---|
| 113 | unsigned count = XHCI_REG_RD(ec, XHCI_EC_SP_CP_COUNT);
|
---|
| 114 | xhci_rh_set_ports_protocol(&hc->rh, offset, count, major);
|
---|
| 115 |
|
---|
[91ca111] | 116 | // "Implied" speed
|
---|
| 117 | if (psic == 0) {
|
---|
[816335c] | 118 | assert(minor == 0);
|
---|
[370a1c8] | 119 |
|
---|
[91ca111] | 120 | if (major == 2) {
|
---|
[a75f9cbc] | 121 | speeds[1] = default_psiv_to_port_speed[1];
|
---|
| 122 | speeds[2] = default_psiv_to_port_speed[2];
|
---|
| 123 | speeds[3] = default_psiv_to_port_speed[3];
|
---|
[91ca111] | 124 | } else if (major == 3) {
|
---|
[a75f9cbc] | 125 | speeds[4] = default_psiv_to_port_speed[4];
|
---|
[91ca111] | 126 | } else {
|
---|
| 127 | return EINVAL;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[816335c] | 130 | usb_log_debug2("Implied speed of USB %u.0 set up.", major);
|
---|
[91ca111] | 131 | } else {
|
---|
| 132 | for (unsigned i = 0; i < psic; i++) {
|
---|
| 133 | xhci_psi_t *psi = xhci_extcap_psi(ec, i);
|
---|
| 134 | unsigned sim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
|
---|
| 135 | unsigned psiv = XHCI_REG_RD(psi, XHCI_PSI_PSIV);
|
---|
| 136 | unsigned psie = XHCI_REG_RD(psi, XHCI_PSI_PSIE);
|
---|
| 137 | unsigned psim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
|
---|
[a75f9cbc] | 138 | uint64_t bps = PSI_TO_BPS(psie, psim);
|
---|
| 139 |
|
---|
| 140 | /*
|
---|
| 141 | * Speed is not implied, but using one of default PSIV. This is
|
---|
| 142 | * not clearly stated in xHCI spec. There is a clear intention
|
---|
| 143 | * to allow xHCI to specify its own speed parameters, but
|
---|
| 144 | * throughout the document, they used fixed values for e.g.
|
---|
| 145 | * High-speed (3), without stating the controller shall have
|
---|
| 146 | * implied default speeds - and for instance Intel controllers
|
---|
| 147 | * do not. So let's check if the values match and if so, accept
|
---|
| 148 | * the implied USB speed too.
|
---|
| 149 | *
|
---|
| 150 | * The main reason we need this is the usb_speed to have
|
---|
| 151 | * mapping also for devices connected to hubs.
|
---|
| 152 | */
|
---|
| 153 | if (psiv < ARRAY_SIZE(default_psiv_to_port_speed)
|
---|
| 154 | && default_psiv_to_port_speed[psiv].major == major
|
---|
| 155 | && default_psiv_to_port_speed[psiv].minor == minor
|
---|
| 156 | && default_psiv_to_port_speed[psiv].rx_bps == bps
|
---|
| 157 | && default_psiv_to_port_speed[psiv].tx_bps == bps) {
|
---|
| 158 | speeds[psiv] = default_psiv_to_port_speed[psiv];
|
---|
| 159 | usb_log_debug2("Assumed default %s speed of USB %u.", usb_str_speed(speeds[psiv].usb_speed), major);
|
---|
| 160 | continue;
|
---|
| 161 | }
|
---|
[91ca111] | 162 |
|
---|
[a75f9cbc] | 163 | // Custom speed
|
---|
[816335c] | 164 | speeds[psiv].major = major;
|
---|
| 165 | speeds[psiv].minor = minor;
|
---|
| 166 | str_ncpy(speeds[psiv].name, 4, name.str, 4);
|
---|
[f668d60] | 167 | speeds[psiv].usb_speed = USB_SPEED_MAX;
|
---|
[816335c] | 168 |
|
---|
[91ca111] | 169 | if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_RX)
|
---|
[816335c] | 170 | speeds[psiv].rx_bps = bps;
|
---|
[91ca111] | 171 | if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_TX) {
|
---|
[816335c] | 172 | speeds[psiv].tx_bps = bps;
|
---|
| 173 | usb_log_debug2("Speed %u set up for bps %" PRIu64 " / %" PRIu64 ".", psiv, speeds[psiv].rx_bps, speeds[psiv].tx_bps);
|
---|
[91ca111] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[eb928c4] | 182 | /**
|
---|
| 183 | * Initialize MMIO spaces of xHC.
|
---|
| 184 | */
|
---|
[e4d7363] | 185 | int hc_init_mmio(xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
|
---|
| 186 | {
|
---|
| 187 | int err;
|
---|
| 188 |
|
---|
| 189 | if (hw_res->mem_ranges.count != 1) {
|
---|
| 190 | usb_log_error("Unexpected MMIO area, bailing out.");
|
---|
| 191 | return EINVAL;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | hc->mmio_range = hw_res->mem_ranges.ranges[0];
|
---|
| 195 |
|
---|
[a1732929] | 196 | usb_log_debug("MMIO area at %p (size %zu), IRQ %d.",
|
---|
[e4d7363] | 197 | RNGABSPTR(hc->mmio_range), RNGSZ(hc->mmio_range), hw_res->irqs.irqs[0]);
|
---|
| 198 |
|
---|
| 199 | if (RNGSZ(hc->mmio_range) < sizeof(xhci_cap_regs_t))
|
---|
| 200 | return EOVERFLOW;
|
---|
| 201 |
|
---|
| 202 | void *base;
|
---|
| 203 | if ((err = pio_enable_range(&hc->mmio_range, &base)))
|
---|
| 204 | return err;
|
---|
| 205 |
|
---|
[20eaa82] | 206 | hc->reg_base = base;
|
---|
[e4d7363] | 207 | hc->cap_regs = (xhci_cap_regs_t *) base;
|
---|
| 208 | hc->op_regs = (xhci_op_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH));
|
---|
| 209 | hc->rt_regs = (xhci_rt_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF));
|
---|
| 210 | hc->db_arry = (xhci_doorbell_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_DBOFF));
|
---|
| 211 |
|
---|
[91ca111] | 212 | uintptr_t xec_offset = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_XECP) * sizeof(xhci_dword_t);
|
---|
| 213 | if (xec_offset > 0)
|
---|
| 214 | hc->xecp = (xhci_extcap_t *) (base + xec_offset);
|
---|
| 215 |
|
---|
[e4d7363] | 216 | usb_log_debug2("Initialized MMIO reg areas:");
|
---|
| 217 | usb_log_debug2("\tCapability regs: %p", hc->cap_regs);
|
---|
| 218 | usb_log_debug2("\tOperational regs: %p", hc->op_regs);
|
---|
| 219 | usb_log_debug2("\tRuntime regs: %p", hc->rt_regs);
|
---|
| 220 | usb_log_debug2("\tDoorbell array base: %p", hc->db_arry);
|
---|
| 221 |
|
---|
| 222 | xhci_dump_cap_regs(hc->cap_regs);
|
---|
| 223 |
|
---|
| 224 | hc->ac64 = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_AC64);
|
---|
| 225 | hc->max_slots = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SLOTS);
|
---|
[94e9c29] | 226 |
|
---|
| 227 | struct timeval tv;
|
---|
| 228 | getuptime(&tv);
|
---|
| 229 | hc->wrap_time = tv.tv_sec * 1000000 + tv.tv_usec;
|
---|
[665368c] | 230 | hc->wrap_count = 0;
|
---|
[94e9c29] | 231 |
|
---|
[708d8fcd] | 232 | unsigned ist = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_IST);
|
---|
| 233 | hc->ist = (ist & 0x10 >> 1) * (ist & 0xf);
|
---|
[e4d7363] | 234 |
|
---|
[a9fcd73] | 235 | if ((err = xhci_rh_init(&hc->rh, hc)))
|
---|
| 236 | goto err_pio;
|
---|
| 237 |
|
---|
| 238 | if ((err = hc_parse_ec(hc)))
|
---|
| 239 | goto err_rh;
|
---|
[91ca111] | 240 |
|
---|
[e4d7363] | 241 | return EOK;
|
---|
[a9fcd73] | 242 |
|
---|
| 243 | err_rh:
|
---|
| 244 | xhci_rh_fini(&hc->rh);
|
---|
| 245 | err_pio:
|
---|
| 246 | pio_disable(hc->reg_base, RNGSZ(hc->mmio_range));
|
---|
| 247 | return err;
|
---|
[e4d7363] | 248 | }
|
---|
| 249 |
|
---|
[2c0564c] | 250 | static int event_worker(void *arg);
|
---|
| 251 |
|
---|
[eb928c4] | 252 | /**
|
---|
| 253 | * Initialize structures kept in allocated memory.
|
---|
| 254 | */
|
---|
[0f6b50f] | 255 | int hc_init_memory(xhci_hc_t *hc, ddf_dev_t *device)
|
---|
[e4d7363] | 256 | {
|
---|
| 257 | int err;
|
---|
| 258 |
|
---|
[b80c1ab] | 259 | if (dma_buffer_alloc(&hc->dcbaa_dma, (1 + hc->max_slots) * sizeof(uint64_t)))
|
---|
[e4d7363] | 260 | return ENOMEM;
|
---|
[b80c1ab] | 261 | hc->dcbaa = hc->dcbaa_dma.virt;
|
---|
[e4d7363] | 262 |
|
---|
[9b2f69e] | 263 | if ((err = xhci_event_ring_init(&hc->event_ring)))
|
---|
[889146e] | 264 | goto err_dcbaa;
|
---|
[e4d7363] | 265 |
|
---|
[b19131c5] | 266 | if ((err = xhci_scratchpad_alloc(hc)))
|
---|
[5a9ae994] | 267 | goto err_event_ring;
|
---|
[e4d7363] | 268 |
|
---|
[aee352c] | 269 | if ((err = xhci_init_commands(hc)))
|
---|
[ee28ae66] | 270 | goto err_scratch;
|
---|
[aee352c] | 271 |
|
---|
[2b61945] | 272 | if ((err = xhci_bus_init(&hc->bus, hc)))
|
---|
[6832245] | 273 | goto err_cmd;
|
---|
[e6b9182] | 274 |
|
---|
[2c0564c] | 275 | fid_t fid = fibril_create(&event_worker, hc);
|
---|
| 276 | if (!fid)
|
---|
| 277 | goto err_bus;
|
---|
| 278 |
|
---|
| 279 | // TODO: completion_reset
|
---|
| 280 | hc->event_fibril_completion.active = true;
|
---|
| 281 | fibril_mutex_initialize(&hc->event_fibril_completion.guard);
|
---|
| 282 | fibril_condvar_initialize(&hc->event_fibril_completion.cv);
|
---|
| 283 |
|
---|
| 284 | xhci_sw_ring_init(&hc->sw_ring, PAGE_SIZE / sizeof(xhci_trb_t));
|
---|
| 285 |
|
---|
| 286 | fibril_add_ready(fid);
|
---|
| 287 |
|
---|
[e4d7363] | 288 | return EOK;
|
---|
| 289 |
|
---|
[2c0564c] | 290 | err_bus:
|
---|
| 291 | xhci_bus_fini(&hc->bus);
|
---|
[ee28ae66] | 292 | err_cmd:
|
---|
[d271f78] | 293 | xhci_fini_commands(hc);
|
---|
[ee28ae66] | 294 | err_scratch:
|
---|
| 295 | xhci_scratchpad_free(hc);
|
---|
[5a9ae994] | 296 | err_event_ring:
|
---|
[e4d7363] | 297 | xhci_event_ring_fini(&hc->event_ring);
|
---|
| 298 | err_dcbaa:
|
---|
[b80c1ab] | 299 | hc->dcbaa = NULL;
|
---|
| 300 | dma_buffer_free(&hc->dcbaa_dma);
|
---|
[e4d7363] | 301 | return err;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[ab5a0830] | 304 | /*
|
---|
| 305 | * Pseudocode:
|
---|
| 306 | * ip = read(intr[0].iman)
|
---|
| 307 | * if (ip) {
|
---|
| 308 | * status = read(usbsts)
|
---|
| 309 | * assert status
|
---|
| 310 | * assert ip
|
---|
| 311 | * accept (passing status)
|
---|
| 312 | * }
|
---|
| 313 | * decline
|
---|
| 314 | */
|
---|
| 315 | static const irq_cmd_t irq_commands[] = {
|
---|
| 316 | {
|
---|
| 317 | .cmd = CMD_PIO_READ_32,
|
---|
| 318 | .dstarg = 3,
|
---|
| 319 | .addr = NULL /* intr[0].iman */
|
---|
| 320 | },
|
---|
| 321 | {
|
---|
| 322 | .cmd = CMD_AND,
|
---|
| 323 | .srcarg = 3,
|
---|
| 324 | .dstarg = 4,
|
---|
| 325 | .value = 0 /* host2xhci(32, 1) */
|
---|
| 326 | },
|
---|
| 327 | {
|
---|
| 328 | .cmd = CMD_PREDICATE,
|
---|
| 329 | .srcarg = 4,
|
---|
| 330 | .value = 5
|
---|
| 331 | },
|
---|
| 332 | {
|
---|
| 333 | .cmd = CMD_PIO_READ_32,
|
---|
| 334 | .dstarg = 1,
|
---|
| 335 | .addr = NULL /* usbsts */
|
---|
| 336 | },
|
---|
| 337 | {
|
---|
| 338 | .cmd = CMD_AND,
|
---|
| 339 | .srcarg = 1,
|
---|
| 340 | .dstarg = 2,
|
---|
| 341 | .value = 0 /* host2xhci(32, XHCI_STATUS_ACK_MASK) */
|
---|
| 342 | },
|
---|
| 343 | {
|
---|
| 344 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
| 345 | .srcarg = 2,
|
---|
| 346 | .addr = NULL /* usbsts */
|
---|
| 347 | },
|
---|
| 348 | {
|
---|
| 349 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
[efe9463] | 350 | .srcarg = 3,
|
---|
[ab5a0830] | 351 | .addr = NULL /* intr[0].iman */
|
---|
| 352 | },
|
---|
| 353 | {
|
---|
| 354 | .cmd = CMD_ACCEPT
|
---|
| 355 | },
|
---|
| 356 | {
|
---|
| 357 | .cmd = CMD_DECLINE
|
---|
| 358 | }
|
---|
| 359 | };
|
---|
| 360 |
|
---|
[e4d7363] | 361 |
|
---|
[cb89430] | 362 | /**
|
---|
| 363 | * Generates code to accept interrupts. The xHCI is designed primarily for
|
---|
| 364 | * MSI/MSI-X, but we use PCI Interrupt Pin. In this mode, all the Interrupters
|
---|
| 365 | * (except 0) are disabled.
|
---|
| 366 | */
|
---|
[e4d7363] | 367 | int hc_irq_code_gen(irq_code_t *code, xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
|
---|
[cb89430] | 368 | {
|
---|
| 369 | assert(code);
|
---|
| 370 | assert(hw_res);
|
---|
| 371 |
|
---|
[e4d7363] | 372 | if (hw_res->irqs.count != 1) {
|
---|
[cb89430] | 373 | usb_log_info("Unexpected HW resources to enable interrupts.");
|
---|
| 374 | return EINVAL;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | code->ranges = malloc(sizeof(irq_pio_range_t));
|
---|
| 378 | if (code->ranges == NULL)
|
---|
| 379 | return ENOMEM;
|
---|
| 380 |
|
---|
| 381 | code->cmds = malloc(sizeof(irq_commands));
|
---|
| 382 | if (code->cmds == NULL) {
|
---|
| 383 | free(code->ranges);
|
---|
| 384 | return ENOMEM;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | code->rangecount = 1;
|
---|
| 388 | code->ranges[0] = (irq_pio_range_t) {
|
---|
[91ca111] | 389 | .base = RNGABS(hc->mmio_range),
|
---|
| 390 | .size = RNGSZ(hc->mmio_range),
|
---|
[cb89430] | 391 | };
|
---|
| 392 |
|
---|
| 393 | code->cmdcount = ARRAY_SIZE(irq_commands);
|
---|
| 394 | memcpy(code->cmds, irq_commands, sizeof(irq_commands));
|
---|
| 395 |
|
---|
[91ca111] | 396 | void *intr0_iman = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF) + offsetof(xhci_rt_regs_t, ir[0]);
|
---|
[ab5a0830] | 397 | void *usbsts = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH) + offsetof(xhci_op_regs_t, usbsts);
|
---|
[cb89430] | 398 | code->cmds[0].addr = intr0_iman;
|
---|
| 399 | code->cmds[1].value = host2xhci(32, 1);
|
---|
[ab5a0830] | 400 | code->cmds[3].addr = usbsts;
|
---|
| 401 | code->cmds[4].value = host2xhci(32, XHCI_STATUS_ACK_MASK);
|
---|
| 402 | code->cmds[5].addr = usbsts;
|
---|
| 403 | code->cmds[6].addr = intr0_iman;
|
---|
[cb89430] | 404 |
|
---|
| 405 | return hw_res->irqs.irqs[0];
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[eb928c4] | 408 | /**
|
---|
| 409 | * Claim xHC from BIOS. Implements handoff as per Section 4.22.1 of xHCI spec.
|
---|
| 410 | */
|
---|
[e4d7363] | 411 | int hc_claim(xhci_hc_t *hc, ddf_dev_t *dev)
|
---|
[cb89430] | 412 | {
|
---|
[91ca111] | 413 | /* No legacy support capability, the controller is solely for us */
|
---|
| 414 | if (!hc->legsup)
|
---|
| 415 | return EOK;
|
---|
| 416 |
|
---|
[0e7380f] | 417 | if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_CNR), 0))
|
---|
| 418 | return ETIMEOUT;
|
---|
| 419 |
|
---|
[e6b0dba] | 420 | usb_log_debug2("LEGSUP: bios: %x, os: %x", hc->legsup->sem_bios, hc->legsup->sem_os);
|
---|
[0e7380f] | 421 | XHCI_REG_SET(hc->legsup, XHCI_LEGSUP_SEM_OS, 1);
|
---|
[4d28d86] | 422 | for (int i = 0; i <= (XHCI_LEGSUP_BIOS_TIMEOUT_US / XHCI_LEGSUP_POLLING_DELAY_1MS); i++) {
|
---|
[e6b0dba] | 423 | usb_log_debug2("LEGSUP: elapsed: %i ms, bios: %x, os: %x", i,
|
---|
| 424 | XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS),
|
---|
| 425 | XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS));
|
---|
| 426 | if (XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS) == 0) {
|
---|
[0e7380f] | 427 | return XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS) == 1 ? EOK : EIO;
|
---|
[e6b0dba] | 428 | }
|
---|
[c9d905f] | 429 | async_usleep(XHCI_LEGSUP_POLLING_DELAY_1MS);
|
---|
[e6b0dba] | 430 | }
|
---|
[a1732929] | 431 | usb_log_error("BIOS did not release XHCI legacy hold!");
|
---|
[e6b0dba] | 432 |
|
---|
[91ca111] | 433 | return ENOTSUP;
|
---|
[cb89430] | 434 | }
|
---|
| 435 |
|
---|
[eb928c4] | 436 | /**
|
---|
[665368c] | 437 | * Ask the xHC to reset its state. Implements sequence
|
---|
[eb928c4] | 438 | */
|
---|
[cb89430] | 439 | static int hc_reset(xhci_hc_t *hc)
|
---|
| 440 | {
|
---|
[0e7380f] | 441 | if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_CNR), 0))
|
---|
| 442 | return ETIMEOUT;
|
---|
| 443 |
|
---|
[cb89430] | 444 | /* Stop the HC: set R/S to 0 */
|
---|
| 445 | XHCI_REG_CLR(hc->op_regs, XHCI_OP_RS, 1);
|
---|
| 446 |
|
---|
[0e7380f] | 447 | /* Wait until the HC is halted - it shall take at most 16 ms */
|
---|
| 448 | if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_HCH), XHCI_REG_MASK(XHCI_OP_HCH)))
|
---|
| 449 | return ETIMEOUT;
|
---|
[cb89430] | 450 |
|
---|
| 451 | /* Reset */
|
---|
| 452 | XHCI_REG_SET(hc->op_regs, XHCI_OP_HCRST, 1);
|
---|
| 453 |
|
---|
| 454 | /* Wait until the reset is complete */
|
---|
[0e7380f] | 455 | if (xhci_reg_wait(&hc->op_regs->usbcmd, XHCI_REG_MASK(XHCI_OP_HCRST), 0))
|
---|
| 456 | return ETIMEOUT;
|
---|
[cb89430] | 457 |
|
---|
| 458 | return EOK;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | /**
|
---|
| 462 | * Initialize the HC: section 4.2
|
---|
| 463 | */
|
---|
[e4d7363] | 464 | int hc_start(xhci_hc_t *hc, bool irq)
|
---|
[cb89430] | 465 | {
|
---|
| 466 | int err;
|
---|
| 467 |
|
---|
| 468 | if ((err = hc_reset(hc)))
|
---|
| 469 | return err;
|
---|
| 470 |
|
---|
[0e7380f] | 471 | if (xhci_reg_wait(&hc->op_regs->usbsts, XHCI_REG_MASK(XHCI_OP_CNR), 0))
|
---|
| 472 | return ETIMEOUT;
|
---|
[cb89430] | 473 |
|
---|
[b80c1ab] | 474 | uint64_t dcbaaptr = hc->dcbaa_dma.phys;
|
---|
[cb89430] | 475 | XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_LO, LOWER32(dcbaaptr));
|
---|
| 476 | XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_HI, UPPER32(dcbaaptr));
|
---|
[15f8079] | 477 | XHCI_REG_WR(hc->op_regs, XHCI_OP_MAX_SLOTS_EN, hc->max_slots);
|
---|
[cb89430] | 478 |
|
---|
[fb28cde] | 479 | uintptr_t crcr;
|
---|
| 480 | xhci_trb_ring_reset_dequeue_state(&hc->cr.trb_ring, &crcr);
|
---|
[4abb134] | 481 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_LO, LOWER32(crcr));
|
---|
| 482 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, UPPER32(crcr));
|
---|
[cb89430] | 483 |
|
---|
[665368c] | 484 | XHCI_REG_SET(hc->op_regs, XHCI_OP_EWE, 1);
|
---|
| 485 |
|
---|
[cb89430] | 486 | xhci_interrupter_regs_t *intr0 = &hc->rt_regs->ir[0];
|
---|
| 487 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTSZ, hc->event_ring.segment_count);
|
---|
[b80c1ab] | 488 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
[12fba858] | 489 | XHCI_REG_WR(intr0, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
| 490 | XHCI_REG_WR(intr0, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
[b80c1ab] | 491 | uint64_t erstptr = hc->event_ring.erst.phys;
|
---|
[cb89430] | 492 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_LO, LOWER32(erstptr));
|
---|
| 493 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_HI, UPPER32(erstptr));
|
---|
| 494 |
|
---|
[665368c] | 495 |
|
---|
[cb89430] | 496 | if (irq) {
|
---|
| 497 | XHCI_REG_SET(intr0, XHCI_INTR_IE, 1);
|
---|
| 498 | XHCI_REG_SET(hc->op_regs, XHCI_OP_INTE, 1);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
[503086d8] | 501 | XHCI_REG_SET(hc->op_regs, XHCI_OP_HSEE, 1);
|
---|
| 502 |
|
---|
[cb89430] | 503 | XHCI_REG_SET(hc->op_regs, XHCI_OP_RS, 1);
|
---|
| 504 |
|
---|
[05770666] | 505 | xhci_rh_startup(&hc->rh);
|
---|
[dcf0597] | 506 |
|
---|
[cb89430] | 507 | return EOK;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
[ab5a0830] | 510 | /**
|
---|
| 511 | * Used only when polling. Shall supplement the irq_commands.
|
---|
| 512 | */
|
---|
[32fb6bce] | 513 | int hc_status(bus_t *bus, uint32_t *status)
|
---|
[5cbccd4] | 514 | {
|
---|
[32fb6bce] | 515 | xhci_hc_t *hc = bus_to_hc(bus);
|
---|
[ab5a0830] | 516 | int ip = XHCI_REG_RD(hc->rt_regs->ir, XHCI_INTR_IP);
|
---|
| 517 | if (ip) {
|
---|
| 518 | *status = XHCI_REG_RD(hc->op_regs, XHCI_OP_STATUS);
|
---|
| 519 | XHCI_REG_WR(hc->op_regs, XHCI_OP_STATUS, *status & XHCI_STATUS_ACK_MASK);
|
---|
| 520 | XHCI_REG_WR(hc->rt_regs->ir, XHCI_INTR_IP, 1);
|
---|
| 521 |
|
---|
| 522 | /* interrupt handler expects status from irq_commands, which is
|
---|
| 523 | * in xhci order. */
|
---|
| 524 | *status = host2xhci(32, *status);
|
---|
| 525 | }
|
---|
[62ba2cbe] | 526 |
|
---|
[598733c9] | 527 | usb_log_debug2("Polled status: %x", *status);
|
---|
[cb89430] | 528 | return EOK;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
[665368c] | 531 | static int xhci_handle_mfindex_wrap_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
| 532 | {
|
---|
[94e9c29] | 533 | struct timeval tv;
|
---|
| 534 | getuptime(&tv);
|
---|
[598733c9] | 535 | usb_log_debug2("Microframe index wrapped (@%lu.%li, %"PRIu64" total).", tv.tv_sec, tv.tv_usec, hc->wrap_count);
|
---|
[94e9c29] | 536 | hc->wrap_time = ((uint64_t) tv.tv_sec) * 1000000 + ((uint64_t) tv.tv_usec);
|
---|
[665368c] | 537 | ++hc->wrap_count;
|
---|
| 538 | return EOK;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[fb154e13] | 541 | static int handle_port_status_change_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
| 542 | {
|
---|
| 543 | uint8_t port_id = XHCI_QWORD_EXTRACT(trb->parameter, 31, 24);
|
---|
| 544 | usb_log_debug("Port status change event detected for port %u.", port_id);
|
---|
| 545 | xhci_rh_handle_port_change(&hc->rh, port_id);
|
---|
| 546 | return EOK;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
[472235a] | 549 | typedef int (*event_handler) (xhci_hc_t *, xhci_trb_t *trb);
|
---|
| 550 |
|
---|
[2c0564c] | 551 | /**
|
---|
| 552 | * These events are handled by separate event handling fibril.
|
---|
| 553 | */
|
---|
[472235a] | 554 | static event_handler event_handlers [] = {
|
---|
[fb154e13] | 555 | [XHCI_TRB_TYPE_PORT_STATUS_CHANGE_EVENT] = &handle_port_status_change_event,
|
---|
[e9e24f2] | 556 | [XHCI_TRB_TYPE_TRANSFER_EVENT] = &xhci_handle_transfer_event,
|
---|
[2c0564c] | 557 | };
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
| 560 | * These events are handled directly in the interrupt handler, thus they must
|
---|
| 561 | * not block waiting for another interrupt.
|
---|
| 562 | */
|
---|
| 563 | static event_handler event_handlers_fast [] = {
|
---|
| 564 | [XHCI_TRB_TYPE_COMMAND_COMPLETION_EVENT] = &xhci_handle_command_completion,
|
---|
[665368c] | 565 | [XHCI_TRB_TYPE_MFINDEX_WRAP_EVENT] = &xhci_handle_mfindex_wrap_event,
|
---|
[472235a] | 566 | };
|
---|
| 567 |
|
---|
[2c0564c] | 568 | static int hc_handle_event(xhci_hc_t *hc, xhci_trb_t *trb)
|
---|
| 569 | {
|
---|
| 570 | const unsigned type = TRB_TYPE(*trb);
|
---|
| 571 |
|
---|
| 572 | if (type <= ARRAY_SIZE(event_handlers_fast) && event_handlers_fast[type])
|
---|
| 573 | return event_handlers_fast[type](hc, trb);
|
---|
| 574 |
|
---|
| 575 | if (type <= ARRAY_SIZE(event_handlers) && event_handlers[type])
|
---|
| 576 | return xhci_sw_ring_enqueue(&hc->sw_ring, trb);
|
---|
| 577 |
|
---|
| 578 | return ENOTSUP;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | static int event_worker(void *arg)
|
---|
[7ee5408] | 582 | {
|
---|
[2c0564c] | 583 | int err;
|
---|
| 584 | xhci_trb_t trb;
|
---|
| 585 | xhci_hc_t * const hc = arg;
|
---|
| 586 | assert(hc);
|
---|
| 587 |
|
---|
| 588 | while (xhci_sw_ring_dequeue(&hc->sw_ring, &trb) != EINTR) {
|
---|
| 589 | const unsigned type = TRB_TYPE(trb);
|
---|
[472235a] | 590 |
|
---|
[2c0564c] | 591 | if ((err = event_handlers[type](hc, &trb)))
|
---|
| 592 | usb_log_error("Failed to handle event: %s", str_error(err));
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | // TODO: completion_complete
|
---|
| 596 | fibril_mutex_lock(&hc->event_fibril_completion.guard);
|
---|
| 597 | hc->event_fibril_completion.active = false;
|
---|
| 598 | fibril_condvar_wait(&hc->event_fibril_completion.cv, &hc->event_fibril_completion.guard);
|
---|
| 599 | fibril_mutex_unlock(&hc->event_fibril_completion.guard);
|
---|
| 600 |
|
---|
| 601 | return EOK;
|
---|
[7ee5408] | 602 | }
|
---|
| 603 |
|
---|
[eb928c4] | 604 | /**
|
---|
| 605 | * Dequeue from event ring and handle dequeued events.
|
---|
| 606 | *
|
---|
| 607 | * As there can be events, that blocks on waiting for subsequent events,
|
---|
| 608 | * we solve this problem by first copying the event TRBs from the event ring,
|
---|
| 609 | * then asserting EHB and only after, handling the events.
|
---|
| 610 | *
|
---|
| 611 | * Whenever the event handling blocks, it switches fibril, and incoming
|
---|
| 612 | * IPC notification will create new event handling fibril for us.
|
---|
| 613 | */
|
---|
[cb89430] | 614 | static void hc_run_event_ring(xhci_hc_t *hc, xhci_event_ring_t *event_ring, xhci_interrupter_regs_t *intr)
|
---|
[62ba2cbe] | 615 | {
|
---|
[cb89430] | 616 | int err;
|
---|
[472235a] | 617 |
|
---|
[f3baab1] | 618 | xhci_trb_t trb;
|
---|
| 619 | hc->event_handler = fibril_get_id();
|
---|
[e50bdd92] | 620 |
|
---|
[f3baab1] | 621 | while ((err = xhci_event_ring_dequeue(event_ring, &trb)) != ENOENT) {
|
---|
[2c0564c] | 622 | if ((err = hc_handle_event(hc, &trb)) != EOK) {
|
---|
| 623 | usb_log_error("Failed to handle event in interrupt: %s", str_error(err));
|
---|
[adb4e683] | 624 | }
|
---|
[f543804] | 625 |
|
---|
| 626 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
| 627 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
| 628 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
[cb89430] | 629 | }
|
---|
| 630 |
|
---|
[f3baab1] | 631 | hc->event_handler = 0;
|
---|
| 632 |
|
---|
[adb4e683] | 633 | /* Update the ERDP to make room in the ring. */
|
---|
[12fba858] | 634 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
[f543804] | 635 | erdp |= XHCI_REG_MASK(XHCI_INTR_ERDP_EHB);
|
---|
[12fba858] | 636 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
| 637 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
[adb4e683] | 638 |
|
---|
[472235a] | 639 | usb_log_debug2("Event ring run finished.");
|
---|
[cb89430] | 640 | }
|
---|
| 641 |
|
---|
[eb928c4] | 642 | /**
|
---|
| 643 | * Handle an interrupt request from xHC. Resolve all situations that trigger an
|
---|
| 644 | * interrupt separately.
|
---|
| 645 | *
|
---|
| 646 | * Note that all RW1C bits in USBSTS register are cleared at the time of
|
---|
| 647 | * handling the interrupt in irq_code. This method is the top-half.
|
---|
| 648 | *
|
---|
| 649 | * @param status contents of USBSTS register at the time of the interrupt.
|
---|
| 650 | */
|
---|
[32fb6bce] | 651 | void hc_interrupt(bus_t *bus, uint32_t status)
|
---|
[cb89430] | 652 | {
|
---|
[32fb6bce] | 653 | xhci_hc_t *hc = bus_to_hc(bus);
|
---|
[ab5a0830] | 654 | status = xhci2host(32, status);
|
---|
[aee352c] | 655 |
|
---|
[cb89430] | 656 | if (status & XHCI_REG_MASK(XHCI_OP_HSE)) {
|
---|
| 657 | usb_log_error("Host controller error occured. Bad things gonna happen...");
|
---|
[ab5a0830] | 658 | status &= ~XHCI_REG_MASK(XHCI_OP_HSE);
|
---|
[cb89430] | 659 | }
|
---|
| 660 |
|
---|
| 661 | if (status & XHCI_REG_MASK(XHCI_OP_EINT)) {
|
---|
[472235a] | 662 | usb_log_debug2("Event interrupt, running the event ring.");
|
---|
[ab5a0830] | 663 | hc_run_event_ring(hc, &hc->event_ring, &hc->rt_regs->ir[0]);
|
---|
| 664 | status &= ~XHCI_REG_MASK(XHCI_OP_EINT);
|
---|
[cb89430] | 665 | }
|
---|
[275f529] | 666 |
|
---|
[cb89430] | 667 | if (status & XHCI_REG_MASK(XHCI_OP_SRE)) {
|
---|
| 668 | usb_log_error("Save/Restore error occured. WTF, S/R mechanism not implemented!");
|
---|
[ab5a0830] | 669 | status &= ~XHCI_REG_MASK(XHCI_OP_SRE);
|
---|
| 670 | }
|
---|
| 671 |
|
---|
[fb154e13] | 672 | /* According to Note on p. 302, we may safely ignore the PCD bit. */
|
---|
| 673 | status &= ~XHCI_REG_MASK(XHCI_OP_PCD);
|
---|
| 674 |
|
---|
[ab5a0830] | 675 | if (status) {
|
---|
| 676 | usb_log_error("Non-zero status after interrupt handling (%08x) - missing something?", status);
|
---|
[cb89430] | 677 | }
|
---|
| 678 | }
|
---|
| 679 |
|
---|
[eb928c4] | 680 | /**
|
---|
| 681 | * Tear down all in-memory structures.
|
---|
| 682 | */
|
---|
[e4d7363] | 683 | void hc_fini(xhci_hc_t *hc)
|
---|
[cb89430] | 684 | {
|
---|
[2c0564c] | 685 | xhci_sw_ring_stop(&hc->sw_ring);
|
---|
| 686 |
|
---|
| 687 | // TODO: completion_wait
|
---|
| 688 | fibril_mutex_lock(&hc->event_fibril_completion.guard);
|
---|
| 689 | while (hc->event_fibril_completion.active)
|
---|
| 690 | fibril_condvar_wait(&hc->event_fibril_completion.cv, &hc->event_fibril_completion.guard);
|
---|
| 691 | fibril_mutex_unlock(&hc->event_fibril_completion.guard);
|
---|
| 692 | xhci_sw_ring_fini(&hc->sw_ring);
|
---|
| 693 |
|
---|
[e6b9182] | 694 | xhci_bus_fini(&hc->bus);
|
---|
[cb89430] | 695 | xhci_event_ring_fini(&hc->event_ring);
|
---|
[b60944b] | 696 | xhci_scratchpad_free(hc);
|
---|
| 697 | dma_buffer_free(&hc->dcbaa_dma);
|
---|
[c46c356] | 698 | xhci_fini_commands(hc);
|
---|
[d32d51d] | 699 | xhci_rh_fini(&hc->rh);
|
---|
[20eaa82] | 700 | pio_disable(hc->reg_base, RNGSZ(hc->mmio_range));
|
---|
[837581fd] | 701 | usb_log_info("Finalized.");
|
---|
[62ba2cbe] | 702 | }
|
---|
| 703 |
|
---|
[eb928c4] | 704 | /**
|
---|
| 705 | * Ring a xHC Doorbell. Implements section 4.7.
|
---|
| 706 | */
|
---|
[708d8fcd] | 707 | void hc_ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
|
---|
[a0be5d0] | 708 | {
|
---|
| 709 | assert(hc);
|
---|
| 710 | uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
|
---|
| 711 | pio_write_32(&hc->db_arry[doorbell], v);
|
---|
[2896ff6] | 712 | usb_log_debug2("Ringing doorbell %d (target: %d)", doorbell, target);
|
---|
[a0be5d0] | 713 | }
|
---|
[5cbccd4] | 714 |
|
---|
[eb928c4] | 715 | /**
|
---|
[7e5a12b] | 716 | * Issue an Enable Slot command. Allocate memory for the slot and fill the
|
---|
| 717 | * DCBAA with the newly created slot.
|
---|
[eb928c4] | 718 | */
|
---|
[7e5a12b] | 719 | int hc_enable_slot(xhci_device_t *dev)
|
---|
[8ea7459] | 720 | {
|
---|
| 721 | int err;
|
---|
[7e5a12b] | 722 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 723 |
|
---|
| 724 | /* Prepare memory for the context */
|
---|
| 725 | if ((err = dma_buffer_alloc(&dev->dev_ctx, sizeof(xhci_device_ctx_t))))
|
---|
| 726 | return err;
|
---|
| 727 | memset(dev->dev_ctx.virt, 0, sizeof(xhci_device_ctx_t));
|
---|
| 728 |
|
---|
| 729 | /* Get the slot number */
|
---|
[8ea7459] | 730 | xhci_cmd_t cmd;
|
---|
[c3d926f3] | 731 | xhci_cmd_init(&cmd, XHCI_CMD_ENABLE_SLOT);
|
---|
[8ea7459] | 732 |
|
---|
[7e5a12b] | 733 | err = xhci_cmd_sync(hc, &cmd);
|
---|
[8ea7459] | 734 |
|
---|
[7e5a12b] | 735 | /* Link them together */
|
---|
| 736 | if (err == EOK) {
|
---|
| 737 | dev->slot_id = cmd.slot_id;
|
---|
| 738 | hc->dcbaa[dev->slot_id] = host2xhci(64, dev->dev_ctx.phys);
|
---|
[c3d926f3] | 739 | }
|
---|
[8ea7459] | 740 |
|
---|
| 741 | xhci_cmd_fini(&cmd);
|
---|
[c3d926f3] | 742 | return err;
|
---|
[8ea7459] | 743 | }
|
---|
| 744 |
|
---|
[eb928c4] | 745 | /**
|
---|
| 746 | * Issue a Disable Slot command for a slot occupied by device.
|
---|
[7e5a12b] | 747 | * Frees the device context.
|
---|
[eb928c4] | 748 | */
|
---|
[7e5a12b] | 749 | int hc_disable_slot(xhci_device_t *dev)
|
---|
[f270ecb] | 750 | {
|
---|
[9620a54] | 751 | int err;
|
---|
[7e5a12b] | 752 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
[9620a54] | 753 |
|
---|
| 754 | if ((err = xhci_cmd_sync_inline(hc, DISABLE_SLOT, .slot_id = dev->slot_id))) {
|
---|
| 755 | return err;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | /* Free the device context. */
|
---|
| 759 | hc->dcbaa[dev->slot_id] = 0;
|
---|
[b80c1ab] | 760 | dma_buffer_free(&dev->dev_ctx);
|
---|
[9620a54] | 761 |
|
---|
| 762 | /* Mark the slot as invalid. */
|
---|
| 763 | dev->slot_id = 0;
|
---|
| 764 |
|
---|
| 765 | return EOK;
|
---|
[f270ecb] | 766 | }
|
---|
| 767 |
|
---|
[a4e7e6e1] | 768 | /**
|
---|
| 769 | * Fill a slot context that is part of an Input Context with appropriate
|
---|
| 770 | * values.
|
---|
| 771 | *
|
---|
| 772 | * @param ctx Slot context, zeroed out.
|
---|
| 773 | */
|
---|
| 774 | static void xhci_setup_slot_context(xhci_device_t *dev, xhci_slot_ctx_t *ctx)
|
---|
| 775 | {
|
---|
| 776 | /* Initialize slot_ctx according to section 4.3.3 point 3. */
|
---|
| 777 | XHCI_SLOT_ROOT_HUB_PORT_SET(*ctx, dev->rh_port);
|
---|
| 778 | XHCI_SLOT_ROUTE_STRING_SET(*ctx, dev->route_str);
|
---|
| 779 | XHCI_SLOT_SPEED_SET(*ctx, usb_speed_to_psiv[dev->base.speed]);
|
---|
| 780 |
|
---|
[2833bb4] | 781 | /*
|
---|
| 782 | * Note: This function is used even before this flag can be set, to
|
---|
| 783 | * issue the address device command. It is OK, because these
|
---|
| 784 | * flags are not required to be valid for that command.
|
---|
| 785 | */
|
---|
| 786 | if (dev->is_hub) {
|
---|
| 787 | XHCI_SLOT_HUB_SET(*ctx, 1);
|
---|
| 788 | XHCI_SLOT_NUM_PORTS_SET(*ctx, dev->num_ports);
|
---|
| 789 | XHCI_SLOT_TT_THINK_TIME_SET(*ctx, dev->tt_think_time);
|
---|
| 790 | XHCI_SLOT_MTT_SET(*ctx, 0); // MTT not supported yet
|
---|
| 791 | }
|
---|
| 792 |
|
---|
[a4e7e6e1] | 793 | /* Setup Transaction Translation. TODO: Test this with HS hub. */
|
---|
| 794 | if (dev->base.tt.dev != NULL) {
|
---|
| 795 | xhci_device_t *hub = xhci_device_get(dev->base.tt.dev);
|
---|
| 796 | XHCI_SLOT_TT_HUB_SLOT_ID_SET(*ctx, hub->slot_id);
|
---|
| 797 | XHCI_SLOT_TT_HUB_PORT_SET(*ctx, dev->base.tt.port);
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | // As we always allocate space for whole input context, we can set this to maximum
|
---|
| 801 | XHCI_SLOT_CTX_ENTRIES_SET(*ctx, 31);
|
---|
| 802 | }
|
---|
| 803 |
|
---|
[eb928c4] | 804 | /**
|
---|
| 805 | * Prepare an empty Endpoint Input Context inside a dma buffer.
|
---|
| 806 | */
|
---|
[a4e7e6e1] | 807 | static int create_configure_ep_input_ctx(xhci_device_t *dev, dma_buffer_t *dma_buf)
|
---|
[b724494] | 808 | {
|
---|
[b80c1ab] | 809 | const int err = dma_buffer_alloc(dma_buf, sizeof(xhci_input_ctx_t));
|
---|
| 810 | if (err)
|
---|
| 811 | return err;
|
---|
[b724494] | 812 |
|
---|
[b80c1ab] | 813 | xhci_input_ctx_t *ictx = dma_buf->virt;
|
---|
[b724494] | 814 | memset(ictx, 0, sizeof(xhci_input_ctx_t));
|
---|
| 815 |
|
---|
[e76c0ea] | 816 | // Quoting sec. 4.6.5 and 4.6.6: A1, D0, D1 are down (already zeroed), A0 is up.
|
---|
[b724494] | 817 | XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
|
---|
[a4e7e6e1] | 818 | xhci_setup_slot_context(dev, &ictx->slot_ctx);
|
---|
[001778c] | 819 |
|
---|
[b724494] | 820 | return EOK;
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[eb928c4] | 823 | /**
|
---|
| 824 | * Initialize a device, assigning it an address. Implements section 4.3.4.
|
---|
| 825 | *
|
---|
| 826 | * @param dev Device to assing an address (unconfigured yet)
|
---|
| 827 | * @param ep0 EP0 of device TODO remove, can be fetched from dev
|
---|
| 828 | */
|
---|
[a4e7e6e1] | 829 | int hc_address_device(xhci_device_t *dev, xhci_endpoint_t *ep0)
|
---|
[b724494] | 830 | {
|
---|
[0206d35] | 831 | int err = ENOMEM;
|
---|
[a4e7e6e1] | 832 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
[0206d35] | 833 |
|
---|
[2cf28b9] | 834 | /* Although we have the precise PSIV value on devices of tier 1,
|
---|
| 835 | * we have to rely on reverse mapping on others. */
|
---|
[a75f9cbc] | 836 | if (!usb_speed_to_psiv[dev->base.speed]) {
|
---|
| 837 | usb_log_error("Device reported an USB speed (%s) that cannot be mapped to HC port speed.", usb_str_speed(dev->base.speed));
|
---|
[2cf28b9] | 838 | return EINVAL;
|
---|
| 839 | }
|
---|
| 840 |
|
---|
[b724494] | 841 | /* Issue configure endpoint command (sec 4.3.5). */
|
---|
[b80c1ab] | 842 | dma_buffer_t ictx_dma_buf;
|
---|
[7e5a12b] | 843 | if ((err = create_configure_ep_input_ctx(dev, &ictx_dma_buf)))
|
---|
| 844 | return err;
|
---|
[b80c1ab] | 845 | xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
|
---|
[b724494] | 846 |
|
---|
| 847 | /* Copy endpoint 0 context and set A1 flag. */
|
---|
| 848 | XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
|
---|
[0206d35] | 849 | xhci_setup_endpoint_context(ep0, &ictx->endpoint_ctx[0]);
|
---|
| 850 |
|
---|
[69b2dfee] | 851 | /* Address device needs Ctx entries set to 1 only */
|
---|
| 852 | xhci_slot_ctx_t *slot_ctx = &ictx->slot_ctx;
|
---|
| 853 | XHCI_SLOT_CTX_ENTRIES_SET(*slot_ctx, 1);
|
---|
| 854 |
|
---|
[c3d926f3] | 855 | /* Issue Address Device command. */
|
---|
[7e5a12b] | 856 | if ((err = xhci_cmd_sync_inline(hc, ADDRESS_DEVICE, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf)))
|
---|
| 857 | return err;
|
---|
[b724494] | 858 |
|
---|
[b80c1ab] | 859 | xhci_device_ctx_t *dev_ctx = dev->dev_ctx.virt;
|
---|
| 860 | dev->base.address = XHCI_SLOT_DEVICE_ADDRESS(dev_ctx->slot_ctx);
|
---|
[a1732929] | 861 | usb_log_debug2("Obtained USB address: %d.", dev->base.address);
|
---|
[0206d35] | 862 |
|
---|
[b724494] | 863 | return EOK;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
[eb928c4] | 866 | /**
|
---|
| 867 | * Issue a Configure Device command for a device in slot.
|
---|
| 868 | *
|
---|
| 869 | * @param slot_id Slot ID assigned to the device.
|
---|
| 870 | */
|
---|
[a4e7e6e1] | 871 | int hc_configure_device(xhci_device_t *dev)
|
---|
[b724494] | 872 | {
|
---|
[a4e7e6e1] | 873 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 874 |
|
---|
[b724494] | 875 | /* Issue configure endpoint command (sec 4.3.5). */
|
---|
[b80c1ab] | 876 | dma_buffer_t ictx_dma_buf;
|
---|
[a4e7e6e1] | 877 | const int err = create_configure_ep_input_ctx(dev, &ictx_dma_buf);
|
---|
[928afc8d] | 878 | if (err)
|
---|
[c3d926f3] | 879 | return err;
|
---|
[b724494] | 880 |
|
---|
[a4e7e6e1] | 881 | return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
|
---|
[b724494] | 882 | }
|
---|
| 883 |
|
---|
[eb928c4] | 884 | /**
|
---|
| 885 | * Issue a Deconfigure Device command for a device in slot.
|
---|
| 886 | *
|
---|
[a4e7e6e1] | 887 | * @param dev The owner of the device
|
---|
[eb928c4] | 888 | */
|
---|
[a4e7e6e1] | 889 | int hc_deconfigure_device(xhci_device_t *dev)
|
---|
[b724494] | 890 | {
|
---|
[a4e7e6e1] | 891 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 892 |
|
---|
[b724494] | 893 | /* Issue configure endpoint command (sec 4.3.5) with the DC flag. */
|
---|
[a4e7e6e1] | 894 | return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .deconfigure = true);
|
---|
[b724494] | 895 | }
|
---|
| 896 |
|
---|
[eb928c4] | 897 | /**
|
---|
| 898 | * Instruct xHC to add an endpoint with supplied endpoint context.
|
---|
| 899 | *
|
---|
[a4e7e6e1] | 900 | * @param dev The owner of the device
|
---|
| 901 | * @param ep_idx Endpoint DCI in question
|
---|
[eb928c4] | 902 | * @param ep_ctx Endpoint context of the endpoint
|
---|
| 903 | */
|
---|
[a4e7e6e1] | 904 | int hc_add_endpoint(xhci_device_t *dev, uint8_t ep_idx, xhci_ep_ctx_t *ep_ctx)
|
---|
[b724494] | 905 | {
|
---|
| 906 | /* Issue configure endpoint command (sec 4.3.5). */
|
---|
[b80c1ab] | 907 | dma_buffer_t ictx_dma_buf;
|
---|
[a4e7e6e1] | 908 | const int err = create_configure_ep_input_ctx(dev, &ictx_dma_buf);
|
---|
[928afc8d] | 909 | if (err)
|
---|
[c3d926f3] | 910 | return err;
|
---|
[b724494] | 911 |
|
---|
[b80c1ab] | 912 | xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
|
---|
[b724494] | 913 | XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
|
---|
| 914 | memcpy(&ictx->endpoint_ctx[ep_idx], ep_ctx, sizeof(xhci_ep_ctx_t));
|
---|
[001778c] | 915 |
|
---|
[a4e7e6e1] | 916 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 917 | return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
|
---|
[b724494] | 918 | }
|
---|
| 919 |
|
---|
[eb928c4] | 920 | /**
|
---|
| 921 | * Instruct xHC to drop an endpoint.
|
---|
| 922 | *
|
---|
[a4e7e6e1] | 923 | * @param dev The owner of the endpoint
|
---|
| 924 | * @param ep_idx Endpoint DCI in question
|
---|
[eb928c4] | 925 | */
|
---|
[a4e7e6e1] | 926 | int hc_drop_endpoint(xhci_device_t *dev, uint8_t ep_idx)
|
---|
[b724494] | 927 | {
|
---|
| 928 | /* Issue configure endpoint command (sec 4.3.5). */
|
---|
[b80c1ab] | 929 | dma_buffer_t ictx_dma_buf;
|
---|
[a4e7e6e1] | 930 | const int err = create_configure_ep_input_ctx(dev, &ictx_dma_buf);
|
---|
[928afc8d] | 931 | if (err)
|
---|
[c3d926f3] | 932 | return err;
|
---|
[b724494] | 933 |
|
---|
[b80c1ab] | 934 | xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
|
---|
[b724494] | 935 | XHCI_INPUT_CTRL_CTX_DROP_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
|
---|
| 936 |
|
---|
[a4e7e6e1] | 937 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 938 | return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
|
---|
[b724494] | 939 | }
|
---|
| 940 |
|
---|
[eb928c4] | 941 | /**
|
---|
| 942 | * Instruct xHC to update information about an endpoint, using supplied
|
---|
| 943 | * endpoint context.
|
---|
| 944 | *
|
---|
[a4e7e6e1] | 945 | * @param dev The owner of the endpoint
|
---|
| 946 | * @param ep_idx Endpoint DCI in question
|
---|
[eb928c4] | 947 | * @param ep_ctx Endpoint context of the endpoint
|
---|
| 948 | */
|
---|
[a4e7e6e1] | 949 | int hc_update_endpoint(xhci_device_t *dev, uint8_t ep_idx, xhci_ep_ctx_t *ep_ctx)
|
---|
[306a36d] | 950 | {
|
---|
| 951 | dma_buffer_t ictx_dma_buf;
|
---|
| 952 | const int err = dma_buffer_alloc(&ictx_dma_buf, sizeof(xhci_input_ctx_t));
|
---|
| 953 | if (err)
|
---|
| 954 | return err;
|
---|
| 955 |
|
---|
| 956 | xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
|
---|
| 957 | memset(ictx, 0, sizeof(xhci_input_ctx_t));
|
---|
| 958 |
|
---|
| 959 | XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, ep_idx + 1);
|
---|
| 960 | memcpy(&ictx->endpoint_ctx[ep_idx], ep_ctx, sizeof(xhci_ep_ctx_t));
|
---|
| 961 |
|
---|
[a4e7e6e1] | 962 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 963 | return xhci_cmd_sync_inline(hc, EVALUATE_CONTEXT, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf);
|
---|
[306a36d] | 964 | }
|
---|
| 965 |
|
---|
[30fc56f] | 966 | /**
|
---|
| 967 | * Instruct xHC to stop running a transfer ring on an endpoint.
|
---|
| 968 | *
|
---|
[a4e7e6e1] | 969 | * @param dev The owner of the endpoint
|
---|
| 970 | * @param ep_idx Endpoint DCI in question
|
---|
[30fc56f] | 971 | */
|
---|
[a4e7e6e1] | 972 | int hc_stop_endpoint(xhci_device_t *dev, uint8_t ep_idx)
|
---|
[30fc56f] | 973 | {
|
---|
[a4e7e6e1] | 974 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 975 | return xhci_cmd_sync_inline(hc, STOP_ENDPOINT, .slot_id = dev->slot_id, .endpoint_id = ep_idx);
|
---|
[30fc56f] | 976 | }
|
---|
| 977 |
|
---|
[feabe163] | 978 | /**
|
---|
| 979 | * Instruct xHC to reset halted endpoint.
|
---|
| 980 | *
|
---|
[a4e7e6e1] | 981 | * @param dev The owner of the endpoint
|
---|
| 982 | * @param ep_idx Endpoint DCI in question
|
---|
[feabe163] | 983 | */
|
---|
[a4e7e6e1] | 984 | int hc_reset_endpoint(xhci_device_t *dev, uint8_t ep_idx)
|
---|
[feabe163] | 985 | {
|
---|
[a4e7e6e1] | 986 | xhci_hc_t * const hc = bus_to_hc(dev->base.bus);
|
---|
| 987 | return xhci_cmd_sync_inline(hc, RESET_ENDPOINT, .slot_id = dev->slot_id, .endpoint_id = ep_idx);
|
---|
[feabe163] | 988 | }
|
---|
| 989 |
|
---|
[5cbccd4] | 990 | /**
|
---|
| 991 | * @}
|
---|
| 992 | */
|
---|