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>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 | #include <usb/host/utils/malloc32.h>
|
---|
40 | #include "debug.h"
|
---|
41 | #include "hc.h"
|
---|
42 | #include "rh.h"
|
---|
43 | #include "hw_struct/trb.h"
|
---|
44 | #include "commands.h"
|
---|
45 | #include "transfers.h"
|
---|
46 | #include "trb_ring.h"
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Default USB Speed ID mapping: Table 157
|
---|
50 | */
|
---|
51 | #define PSI_TO_BPS(psie, psim) (((uint64_t) psim) << (10 * psie))
|
---|
52 | #define PORT_SPEED(mjr, psie, psim) { \
|
---|
53 | .name = "USB ", \
|
---|
54 | .major = mjr, \
|
---|
55 | .minor = 0, \
|
---|
56 | .rx_bps = PSI_TO_BPS(psie, psim), \
|
---|
57 | .tx_bps = PSI_TO_BPS(psie, psim) \
|
---|
58 | }
|
---|
59 | static const xhci_port_speed_t ps_default_full = PORT_SPEED(2, 2, 12);
|
---|
60 | static const xhci_port_speed_t ps_default_low = PORT_SPEED(2, 1, 1500);
|
---|
61 | static const xhci_port_speed_t ps_default_high = PORT_SPEED(2, 2, 480);
|
---|
62 | static const xhci_port_speed_t ps_default_super = PORT_SPEED(3, 3, 5);
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Walk the list of extended capabilities.
|
---|
66 | */
|
---|
67 | static int hc_parse_ec(xhci_hc_t *hc)
|
---|
68 | {
|
---|
69 | unsigned psic, major, minor;
|
---|
70 | xhci_sp_name_t name;
|
---|
71 |
|
---|
72 | xhci_port_speed_t *speeds = hc->rh.speeds;
|
---|
73 |
|
---|
74 | for (xhci_extcap_t *ec = hc->xecp; ec; ec = xhci_extcap_next(ec)) {
|
---|
75 | xhci_dump_extcap(ec);
|
---|
76 | switch (XHCI_REG_RD(ec, XHCI_EC_CAP_ID)) {
|
---|
77 | case XHCI_EC_USB_LEGACY:
|
---|
78 | assert(hc->legsup == NULL);
|
---|
79 | hc->legsup = (xhci_legsup_t *) ec;
|
---|
80 | break;
|
---|
81 | case XHCI_EC_SUPPORTED_PROTOCOL:
|
---|
82 | psic = XHCI_REG_RD(ec, XHCI_EC_SP_PSIC);
|
---|
83 | major = XHCI_REG_RD(ec, XHCI_EC_SP_MAJOR);
|
---|
84 | minor = XHCI_REG_RD(ec, XHCI_EC_SP_MINOR);
|
---|
85 | name.packed = host2uint32_t_le(XHCI_REG_RD(ec, XHCI_EC_SP_NAME));
|
---|
86 |
|
---|
87 | if (name.packed != xhci_name_usb.packed) {
|
---|
88 | /**
|
---|
89 | * The detection of such protocol would work,
|
---|
90 | * but the rest of the implementation is made
|
---|
91 | * for the USB protocol only.
|
---|
92 | */
|
---|
93 | usb_log_error("Unknown protocol %.4s.", name.str);
|
---|
94 | return ENOTSUP;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // "Implied" speed
|
---|
98 | if (psic == 0) {
|
---|
99 | assert(minor == 0);
|
---|
100 |
|
---|
101 | if (major == 2) {
|
---|
102 | speeds[1] = ps_default_full;
|
---|
103 | speeds[2] = ps_default_low;
|
---|
104 | speeds[3] = ps_default_high;
|
---|
105 | } else if (major == 3) {
|
---|
106 | speeds[4] = ps_default_super;
|
---|
107 | } else {
|
---|
108 | return EINVAL;
|
---|
109 | }
|
---|
110 |
|
---|
111 | usb_log_debug2("Implied speed of USB %u.0 set up.", major);
|
---|
112 | } else {
|
---|
113 | for (unsigned i = 0; i < psic; i++) {
|
---|
114 | xhci_psi_t *psi = xhci_extcap_psi(ec, i);
|
---|
115 | unsigned sim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
|
---|
116 | unsigned psiv = XHCI_REG_RD(psi, XHCI_PSI_PSIV);
|
---|
117 | unsigned psie = XHCI_REG_RD(psi, XHCI_PSI_PSIE);
|
---|
118 | unsigned psim = XHCI_REG_RD(psi, XHCI_PSI_PSIM);
|
---|
119 |
|
---|
120 | speeds[psiv].major = major;
|
---|
121 | speeds[psiv].minor = minor;
|
---|
122 | str_ncpy(speeds[psiv].name, 4, name.str, 4);
|
---|
123 |
|
---|
124 | uint64_t bps = PSI_TO_BPS(psie, psim);
|
---|
125 |
|
---|
126 | if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_RX)
|
---|
127 | speeds[psiv].rx_bps = bps;
|
---|
128 | if (sim == XHCI_PSI_PLT_SYMM || sim == XHCI_PSI_PLT_TX) {
|
---|
129 | speeds[psiv].tx_bps = bps;
|
---|
130 | usb_log_debug2("Speed %u set up for bps %" PRIu64 " / %" PRIu64 ".", psiv, speeds[psiv].rx_bps, speeds[psiv].tx_bps);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | return EOK;
|
---|
137 | }
|
---|
138 |
|
---|
139 | int hc_init_mmio(xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
|
---|
140 | {
|
---|
141 | int err;
|
---|
142 |
|
---|
143 | if (hw_res->mem_ranges.count != 1) {
|
---|
144 | usb_log_error("Unexpected MMIO area, bailing out.");
|
---|
145 | return EINVAL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | hc->mmio_range = hw_res->mem_ranges.ranges[0];
|
---|
149 |
|
---|
150 | usb_log_debug("MMIO area at %p (size %zu), IRQ %d.\n",
|
---|
151 | RNGABSPTR(hc->mmio_range), RNGSZ(hc->mmio_range), hw_res->irqs.irqs[0]);
|
---|
152 |
|
---|
153 | if (RNGSZ(hc->mmio_range) < sizeof(xhci_cap_regs_t))
|
---|
154 | return EOVERFLOW;
|
---|
155 |
|
---|
156 | void *base;
|
---|
157 | if ((err = pio_enable_range(&hc->mmio_range, &base)))
|
---|
158 | return err;
|
---|
159 |
|
---|
160 | hc->base = base;
|
---|
161 | hc->cap_regs = (xhci_cap_regs_t *) base;
|
---|
162 | hc->op_regs = (xhci_op_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH));
|
---|
163 | hc->rt_regs = (xhci_rt_regs_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF));
|
---|
164 | hc->db_arry = (xhci_doorbell_t *) (base + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_DBOFF));
|
---|
165 |
|
---|
166 | uintptr_t xec_offset = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_XECP) * sizeof(xhci_dword_t);
|
---|
167 | if (xec_offset > 0)
|
---|
168 | hc->xecp = (xhci_extcap_t *) (base + xec_offset);
|
---|
169 |
|
---|
170 | usb_log_debug2("Initialized MMIO reg areas:");
|
---|
171 | usb_log_debug2("\tCapability regs: %p", hc->cap_regs);
|
---|
172 | usb_log_debug2("\tOperational regs: %p", hc->op_regs);
|
---|
173 | usb_log_debug2("\tRuntime regs: %p", hc->rt_regs);
|
---|
174 | usb_log_debug2("\tDoorbell array base: %p", hc->db_arry);
|
---|
175 |
|
---|
176 | xhci_dump_cap_regs(hc->cap_regs);
|
---|
177 |
|
---|
178 | hc->ac64 = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_AC64);
|
---|
179 | hc->max_slots = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SLOTS);
|
---|
180 |
|
---|
181 | if ((err = hc_parse_ec(hc))) {
|
---|
182 | pio_disable(hc->base, RNGSZ(hc->mmio_range));
|
---|
183 | return err;
|
---|
184 | }
|
---|
185 |
|
---|
186 | return EOK;
|
---|
187 | }
|
---|
188 |
|
---|
189 | int hc_init_memory(xhci_hc_t *hc)
|
---|
190 | {
|
---|
191 | int err;
|
---|
192 |
|
---|
193 | hc->dcbaa = malloc32((1 + hc->max_slots) * sizeof(uint64_t));
|
---|
194 | if (!hc->dcbaa)
|
---|
195 | return ENOMEM;
|
---|
196 |
|
---|
197 | hc->dcbaa_virt = malloc((1 + hc->max_slots) * sizeof(xhci_virt_device_ctx_t));
|
---|
198 | if (!hc->dcbaa_virt) {
|
---|
199 | err = ENOMEM;
|
---|
200 | goto err_dcbaa;
|
---|
201 | }
|
---|
202 |
|
---|
203 | if ((err = xhci_trb_ring_init(&hc->command_ring, hc)))
|
---|
204 | goto err_dcbaa_virt;
|
---|
205 |
|
---|
206 | if ((err = xhci_event_ring_init(&hc->event_ring, hc)))
|
---|
207 | goto err_cmd_ring;
|
---|
208 |
|
---|
209 | if ((err = xhci_scratchpad_alloc(hc)))
|
---|
210 | goto err_event_ring;
|
---|
211 |
|
---|
212 | if ((err = xhci_init_commands(hc)))
|
---|
213 | goto err_scratch;
|
---|
214 |
|
---|
215 | if ((err = xhci_rh_init(&hc->rh, hc)))
|
---|
216 | goto err_cmd;
|
---|
217 |
|
---|
218 | if ((err = xhci_bus_init(&hc->bus)))
|
---|
219 | goto err_rh;
|
---|
220 |
|
---|
221 |
|
---|
222 | return EOK;
|
---|
223 |
|
---|
224 | err_rh:
|
---|
225 | xhci_rh_fini(&hc->rh);
|
---|
226 | err_cmd:
|
---|
227 | xhci_fini_commands(hc);
|
---|
228 | err_scratch:
|
---|
229 | xhci_scratchpad_free(hc);
|
---|
230 | err_event_ring:
|
---|
231 | xhci_event_ring_fini(&hc->event_ring);
|
---|
232 | err_cmd_ring:
|
---|
233 | xhci_trb_ring_fini(&hc->command_ring);
|
---|
234 | err_dcbaa_virt:
|
---|
235 | free32(hc->dcbaa_virt);
|
---|
236 | err_dcbaa:
|
---|
237 | free32(hc->dcbaa);
|
---|
238 | return err;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * Pseudocode:
|
---|
243 | * ip = read(intr[0].iman)
|
---|
244 | * if (ip) {
|
---|
245 | * status = read(usbsts)
|
---|
246 | * assert status
|
---|
247 | * assert ip
|
---|
248 | * accept (passing status)
|
---|
249 | * }
|
---|
250 | * decline
|
---|
251 | */
|
---|
252 | static const irq_cmd_t irq_commands[] = {
|
---|
253 | {
|
---|
254 | .cmd = CMD_PIO_READ_32,
|
---|
255 | .dstarg = 3,
|
---|
256 | .addr = NULL /* intr[0].iman */
|
---|
257 | },
|
---|
258 | {
|
---|
259 | .cmd = CMD_AND,
|
---|
260 | .srcarg = 3,
|
---|
261 | .dstarg = 4,
|
---|
262 | .value = 0 /* host2xhci(32, 1) */
|
---|
263 | },
|
---|
264 | {
|
---|
265 | .cmd = CMD_PREDICATE,
|
---|
266 | .srcarg = 4,
|
---|
267 | .value = 5
|
---|
268 | },
|
---|
269 | {
|
---|
270 | .cmd = CMD_PIO_READ_32,
|
---|
271 | .dstarg = 1,
|
---|
272 | .addr = NULL /* usbsts */
|
---|
273 | },
|
---|
274 | {
|
---|
275 | .cmd = CMD_AND,
|
---|
276 | .srcarg = 1,
|
---|
277 | .dstarg = 2,
|
---|
278 | .value = 0 /* host2xhci(32, XHCI_STATUS_ACK_MASK) */
|
---|
279 | },
|
---|
280 | {
|
---|
281 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
282 | .srcarg = 2,
|
---|
283 | .addr = NULL /* usbsts */
|
---|
284 | },
|
---|
285 | {
|
---|
286 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
287 | .srcarg = 3,
|
---|
288 | .addr = NULL /* intr[0].iman */
|
---|
289 | },
|
---|
290 | {
|
---|
291 | .cmd = CMD_ACCEPT
|
---|
292 | },
|
---|
293 | {
|
---|
294 | .cmd = CMD_DECLINE
|
---|
295 | }
|
---|
296 | };
|
---|
297 |
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Generates code to accept interrupts. The xHCI is designed primarily for
|
---|
301 | * MSI/MSI-X, but we use PCI Interrupt Pin. In this mode, all the Interrupters
|
---|
302 | * (except 0) are disabled.
|
---|
303 | */
|
---|
304 | int hc_irq_code_gen(irq_code_t *code, xhci_hc_t *hc, const hw_res_list_parsed_t *hw_res)
|
---|
305 | {
|
---|
306 | assert(code);
|
---|
307 | assert(hw_res);
|
---|
308 |
|
---|
309 | if (hw_res->irqs.count != 1) {
|
---|
310 | usb_log_info("Unexpected HW resources to enable interrupts.");
|
---|
311 | return EINVAL;
|
---|
312 | }
|
---|
313 |
|
---|
314 | code->ranges = malloc(sizeof(irq_pio_range_t));
|
---|
315 | if (code->ranges == NULL)
|
---|
316 | return ENOMEM;
|
---|
317 |
|
---|
318 | code->cmds = malloc(sizeof(irq_commands));
|
---|
319 | if (code->cmds == NULL) {
|
---|
320 | free(code->ranges);
|
---|
321 | return ENOMEM;
|
---|
322 | }
|
---|
323 |
|
---|
324 | code->rangecount = 1;
|
---|
325 | code->ranges[0] = (irq_pio_range_t) {
|
---|
326 | .base = RNGABS(hc->mmio_range),
|
---|
327 | .size = RNGSZ(hc->mmio_range),
|
---|
328 | };
|
---|
329 |
|
---|
330 | code->cmdcount = ARRAY_SIZE(irq_commands);
|
---|
331 | memcpy(code->cmds, irq_commands, sizeof(irq_commands));
|
---|
332 |
|
---|
333 | void *intr0_iman = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_RTSOFF) + offsetof(xhci_rt_regs_t, ir[0]);
|
---|
334 | void *usbsts = RNGABSPTR(hc->mmio_range) + XHCI_REG_RD(hc->cap_regs, XHCI_CAP_LENGTH) + offsetof(xhci_op_regs_t, usbsts);
|
---|
335 | code->cmds[0].addr = intr0_iman;
|
---|
336 | code->cmds[1].value = host2xhci(32, 1);
|
---|
337 | code->cmds[3].addr = usbsts;
|
---|
338 | code->cmds[4].value = host2xhci(32, XHCI_STATUS_ACK_MASK);
|
---|
339 | code->cmds[5].addr = usbsts;
|
---|
340 | code->cmds[6].addr = intr0_iman;
|
---|
341 |
|
---|
342 | return hw_res->irqs.irqs[0];
|
---|
343 | }
|
---|
344 |
|
---|
345 | int hc_claim(xhci_hc_t *hc, ddf_dev_t *dev)
|
---|
346 | {
|
---|
347 | /* No legacy support capability, the controller is solely for us */
|
---|
348 | if (!hc->legsup)
|
---|
349 | return EOK;
|
---|
350 |
|
---|
351 | /* Section 4.22.1 */
|
---|
352 | /* TODO: Test this with USB3-aware BIOS */
|
---|
353 | usb_log_debug2("LEGSUP: bios: %x, os: %x", hc->legsup->sem_bios, hc->legsup->sem_os);
|
---|
354 | XHCI_REG_WR(hc->legsup, XHCI_LEGSUP_SEM_OS, 1);
|
---|
355 | for (int i = 0; i <= (XHCI_LEGSUP_BIOS_TIMEOUT_US / XHCI_LEGSUP_POLLING_DELAY_1MS); i++) {
|
---|
356 | usb_log_debug2("LEGSUP: elapsed: %i ms, bios: %x, os: %x", i,
|
---|
357 | XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS),
|
---|
358 | XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS));
|
---|
359 | if (XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_BIOS) == 0) {
|
---|
360 | assert(XHCI_REG_RD(hc->legsup, XHCI_LEGSUP_SEM_OS) == 1);
|
---|
361 | return EOK;
|
---|
362 | }
|
---|
363 | async_usleep(XHCI_LEGSUP_POLLING_DELAY_1MS);
|
---|
364 | }
|
---|
365 | usb_log_error("BIOS did not release XHCI legacy hold!\n");
|
---|
366 |
|
---|
367 | return ENOTSUP;
|
---|
368 | }
|
---|
369 |
|
---|
370 | static int hc_reset(xhci_hc_t *hc)
|
---|
371 | {
|
---|
372 | /* Stop the HC: set R/S to 0 */
|
---|
373 | XHCI_REG_CLR(hc->op_regs, XHCI_OP_RS, 1);
|
---|
374 |
|
---|
375 | /* Wait 16 ms until the HC is halted */
|
---|
376 | async_usleep(16000);
|
---|
377 | assert(XHCI_REG_RD(hc->op_regs, XHCI_OP_HCH));
|
---|
378 |
|
---|
379 | /* Reset */
|
---|
380 | XHCI_REG_SET(hc->op_regs, XHCI_OP_HCRST, 1);
|
---|
381 |
|
---|
382 | /* Wait until the reset is complete */
|
---|
383 | while (XHCI_REG_RD(hc->op_regs, XHCI_OP_HCRST))
|
---|
384 | async_usleep(1000);
|
---|
385 |
|
---|
386 | return EOK;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Initialize the HC: section 4.2
|
---|
391 | */
|
---|
392 | int hc_start(xhci_hc_t *hc, bool irq)
|
---|
393 | {
|
---|
394 | int err;
|
---|
395 |
|
---|
396 | if ((err = hc_reset(hc)))
|
---|
397 | return err;
|
---|
398 |
|
---|
399 | while (XHCI_REG_RD(hc->op_regs, XHCI_OP_CNR))
|
---|
400 | async_usleep(1000);
|
---|
401 |
|
---|
402 | uint64_t dcbaaptr = addr_to_phys(hc->dcbaa);
|
---|
403 | XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_LO, LOWER32(dcbaaptr));
|
---|
404 | XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_HI, UPPER32(dcbaaptr));
|
---|
405 | XHCI_REG_WR(hc->op_regs, XHCI_OP_MAX_SLOTS_EN, 0);
|
---|
406 |
|
---|
407 | uint64_t crptr = xhci_trb_ring_get_dequeue_ptr(&hc->command_ring);
|
---|
408 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_LO, LOWER32(crptr) >> 6);
|
---|
409 | XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, UPPER32(crptr));
|
---|
410 |
|
---|
411 | uint64_t erstptr = addr_to_phys(hc->event_ring.erst);
|
---|
412 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
413 | xhci_interrupter_regs_t *intr0 = &hc->rt_regs->ir[0];
|
---|
414 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTSZ, hc->event_ring.segment_count);
|
---|
415 | XHCI_REG_WR(intr0, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
416 | XHCI_REG_WR(intr0, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
417 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_LO, LOWER32(erstptr));
|
---|
418 | XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_HI, UPPER32(erstptr));
|
---|
419 |
|
---|
420 | if (irq) {
|
---|
421 | XHCI_REG_SET(intr0, XHCI_INTR_IE, 1);
|
---|
422 | XHCI_REG_SET(hc->op_regs, XHCI_OP_INTE, 1);
|
---|
423 | }
|
---|
424 |
|
---|
425 | XHCI_REG_SET(hc->op_regs, XHCI_OP_RS, 1);
|
---|
426 |
|
---|
427 | /* The reset changed status of all ports, and SW originated reason does
|
---|
428 | * not cause an interrupt.
|
---|
429 | */
|
---|
430 | xhci_rh_handle_port_change(&hc->rh);
|
---|
431 |
|
---|
432 | return EOK;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Used only when polling. Shall supplement the irq_commands.
|
---|
437 | */
|
---|
438 | int hc_status(xhci_hc_t *hc, uint32_t *status)
|
---|
439 | {
|
---|
440 | int ip = XHCI_REG_RD(hc->rt_regs->ir, XHCI_INTR_IP);
|
---|
441 | if (ip) {
|
---|
442 | *status = XHCI_REG_RD(hc->op_regs, XHCI_OP_STATUS);
|
---|
443 | XHCI_REG_WR(hc->op_regs, XHCI_OP_STATUS, *status & XHCI_STATUS_ACK_MASK);
|
---|
444 | XHCI_REG_WR(hc->rt_regs->ir, XHCI_INTR_IP, 1);
|
---|
445 |
|
---|
446 | /* interrupt handler expects status from irq_commands, which is
|
---|
447 | * in xhci order. */
|
---|
448 | *status = host2xhci(32, *status);
|
---|
449 | }
|
---|
450 |
|
---|
451 | usb_log_debug2("HC(%p): Polled status: %x", hc, *status);
|
---|
452 | return EOK;
|
---|
453 | }
|
---|
454 |
|
---|
455 | int hc_schedule(xhci_hc_t *hc, usb_transfer_batch_t *batch)
|
---|
456 | {
|
---|
457 | assert(batch);
|
---|
458 |
|
---|
459 | usb_log_debug2("EP(%d:%d) started %s transfer of size %lu.",
|
---|
460 | batch->ep->target.address, batch->ep->target.endpoint,
|
---|
461 | usb_str_transfer_type(batch->ep->transfer_type),
|
---|
462 | batch->buffer_size);
|
---|
463 |
|
---|
464 | if (!batch->ep->target.address) {
|
---|
465 | usb_log_error("Attempted to schedule transfer to address 0.");
|
---|
466 | return EINVAL;
|
---|
467 | }
|
---|
468 |
|
---|
469 | switch (batch->ep->transfer_type) {
|
---|
470 | case USB_TRANSFER_CONTROL:
|
---|
471 | return xhci_schedule_control_transfer(hc, batch);
|
---|
472 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
473 | /* TODO: Implement me. */
|
---|
474 | break;
|
---|
475 | case USB_TRANSFER_BULK:
|
---|
476 | return xhci_schedule_bulk_transfer(hc, batch);
|
---|
477 | case USB_TRANSFER_INTERRUPT:
|
---|
478 | /* TODO: Implement me. */
|
---|
479 | break;
|
---|
480 | }
|
---|
481 |
|
---|
482 | return EOK;
|
---|
483 | }
|
---|
484 |
|
---|
485 | typedef int (*event_handler) (xhci_hc_t *, xhci_trb_t *trb);
|
---|
486 |
|
---|
487 | static event_handler event_handlers [] = {
|
---|
488 | [XHCI_TRB_TYPE_COMMAND_COMPLETION_EVENT] = &xhci_handle_command_completion,
|
---|
489 | [XHCI_TRB_TYPE_PORT_STATUS_CHANGE_EVENT] = &xhci_rh_handle_port_status_change_event,
|
---|
490 | [XHCI_TRB_TYPE_TRANSFER_EVENT] = &xhci_handle_transfer_event,
|
---|
491 | };
|
---|
492 |
|
---|
493 | static int hc_handle_event(xhci_hc_t *hc, xhci_trb_t *trb, xhci_interrupter_regs_t *intr)
|
---|
494 | {
|
---|
495 | unsigned type = TRB_TYPE(*trb);
|
---|
496 | if (type >= ARRAY_SIZE(event_handlers) || !event_handlers[type])
|
---|
497 | return ENOTSUP;
|
---|
498 |
|
---|
499 | return event_handlers[type](hc, trb);
|
---|
500 | }
|
---|
501 |
|
---|
502 | static void hc_run_event_ring(xhci_hc_t *hc, xhci_event_ring_t *event_ring, xhci_interrupter_regs_t *intr)
|
---|
503 | {
|
---|
504 | int err;
|
---|
505 | ssize_t size = 16;
|
---|
506 | xhci_trb_t *queue = malloc(sizeof(xhci_trb_t) * size);
|
---|
507 | if (!queue) {
|
---|
508 | usb_log_error("Not enough memory to run the event ring.");
|
---|
509 | return;
|
---|
510 | }
|
---|
511 |
|
---|
512 | xhci_trb_t *head = queue;
|
---|
513 |
|
---|
514 | while ((err = xhci_event_ring_dequeue(event_ring, head)) != ENOENT) {
|
---|
515 | if (err != EOK) {
|
---|
516 | usb_log_warning("Error while accessing event ring: %s", str_error(err));
|
---|
517 | break;
|
---|
518 | }
|
---|
519 |
|
---|
520 | usb_log_debug2("Dequeued trb from event ring: %s", xhci_trb_str_type(TRB_TYPE(*head)));
|
---|
521 | head++;
|
---|
522 |
|
---|
523 | /* Expand the array if needed. */
|
---|
524 | if (head - queue >= size) {
|
---|
525 | size *= 2;
|
---|
526 | xhci_trb_t *new_queue = realloc(queue, size);
|
---|
527 | if (new_queue == NULL)
|
---|
528 | break; /* Will process only those TRBs we have memory for. */
|
---|
529 |
|
---|
530 | head = new_queue + (head - queue);
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | /* Update the ERDP to make room in the ring. */
|
---|
535 | usb_log_debug2("Copying from ring finished, updating ERDP.");
|
---|
536 | hc->event_ring.dequeue_ptr = host2xhci(64, addr_to_phys(hc->event_ring.dequeue_trb));
|
---|
537 | uint64_t erdp = hc->event_ring.dequeue_ptr;
|
---|
538 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_LO, LOWER32(erdp));
|
---|
539 | XHCI_REG_WR(intr, XHCI_INTR_ERDP_HI, UPPER32(erdp));
|
---|
540 | XHCI_REG_SET(intr, XHCI_INTR_ERDP_EHB, 1);
|
---|
541 |
|
---|
542 | /* Handle all of the collected events if possible. */
|
---|
543 | if (head == queue)
|
---|
544 | usb_log_warning("No events to be handled!");
|
---|
545 |
|
---|
546 | for (xhci_trb_t *tail = queue; tail != head; tail++) {
|
---|
547 | if ((err = hc_handle_event(hc, tail, intr)) != EOK) {
|
---|
548 | usb_log_error("Failed to handle event: %s", str_error(err));
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | free(queue);
|
---|
553 | usb_log_debug2("Event ring run finished.");
|
---|
554 | }
|
---|
555 |
|
---|
556 | void hc_interrupt(xhci_hc_t *hc, uint32_t status)
|
---|
557 | {
|
---|
558 | status = xhci2host(32, status);
|
---|
559 |
|
---|
560 | if (status & XHCI_REG_MASK(XHCI_OP_PCD)) {
|
---|
561 | usb_log_debug2("Root hub interrupt.");
|
---|
562 | xhci_rh_handle_port_change(&hc->rh);
|
---|
563 | status &= ~XHCI_REG_MASK(XHCI_OP_PCD);
|
---|
564 | }
|
---|
565 |
|
---|
566 | if (status & XHCI_REG_MASK(XHCI_OP_HSE)) {
|
---|
567 | usb_log_error("Host controller error occured. Bad things gonna happen...");
|
---|
568 | status &= ~XHCI_REG_MASK(XHCI_OP_HSE);
|
---|
569 | }
|
---|
570 |
|
---|
571 | if (status & XHCI_REG_MASK(XHCI_OP_EINT)) {
|
---|
572 | usb_log_debug2("Event interrupt, running the event ring.");
|
---|
573 | hc_run_event_ring(hc, &hc->event_ring, &hc->rt_regs->ir[0]);
|
---|
574 | status &= ~XHCI_REG_MASK(XHCI_OP_EINT);
|
---|
575 | }
|
---|
576 |
|
---|
577 | if (status & XHCI_REG_MASK(XHCI_OP_SRE)) {
|
---|
578 | usb_log_error("Save/Restore error occured. WTF, S/R mechanism not implemented!");
|
---|
579 | status &= ~XHCI_REG_MASK(XHCI_OP_SRE);
|
---|
580 | }
|
---|
581 |
|
---|
582 | if (status) {
|
---|
583 | usb_log_error("Non-zero status after interrupt handling (%08x) - missing something?", status);
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | static void hc_dcbaa_fini(xhci_hc_t *hc)
|
---|
588 | {
|
---|
589 | xhci_trb_ring_t* trb_ring;
|
---|
590 | xhci_scratchpad_free(hc);
|
---|
591 |
|
---|
592 | /* Idx 0 already deallocated by xhci_scratchpad_free. */
|
---|
593 | for (unsigned i = 1; i < hc->max_slots + 1; ++i) {
|
---|
594 | if (hc->dcbaa_virt[i].dev_ctx) {
|
---|
595 | free32(hc->dcbaa_virt[i].dev_ctx);
|
---|
596 | hc->dcbaa_virt[i].dev_ctx = NULL;
|
---|
597 | }
|
---|
598 |
|
---|
599 | for (unsigned i = 0; i < XHCI_EP_COUNT; ++i) {
|
---|
600 | trb_ring = hc->dcbaa_virt[i].trs[i];
|
---|
601 | if (trb_ring) {
|
---|
602 | hc->dcbaa_virt[i].trs[i] = NULL;
|
---|
603 | xhci_trb_ring_fini(trb_ring);
|
---|
604 | free32(trb_ring);
|
---|
605 | }
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | free32(hc->dcbaa);
|
---|
610 | free32(hc->dcbaa_virt);
|
---|
611 | }
|
---|
612 |
|
---|
613 | void hc_fini(xhci_hc_t *hc)
|
---|
614 | {
|
---|
615 | xhci_bus_fini(&hc->bus);
|
---|
616 | xhci_trb_ring_fini(&hc->command_ring);
|
---|
617 | xhci_event_ring_fini(&hc->event_ring);
|
---|
618 | hc_dcbaa_fini(hc);
|
---|
619 | xhci_fini_commands(hc);
|
---|
620 | xhci_rh_fini(&hc->rh);
|
---|
621 | pio_disable(hc->base, RNGSZ(hc->mmio_range));
|
---|
622 | usb_log_info("HC(%p): Finalized.", hc);
|
---|
623 | }
|
---|
624 |
|
---|
625 | int hc_ring_doorbell(xhci_hc_t *hc, unsigned doorbell, unsigned target)
|
---|
626 | {
|
---|
627 | assert(hc);
|
---|
628 | uint32_t v = host2xhci(32, target & BIT_RRANGE(uint32_t, 7));
|
---|
629 | pio_write_32(&hc->db_arry[doorbell], v);
|
---|
630 | return EOK;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * @}
|
---|
635 | */
|
---|