[f4eb6c93] | 1 | /*
|
---|
[e0a5d4c] | 2 | * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek, Jaroslav Jindrak, Jan Hrach
|
---|
[f4eb6c93] | 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 | #include <errno.h>
|
---|
| 30 | #include <assert.h>
|
---|
| 31 | #include <ddi.h>
|
---|
| 32 | #include <as.h>
|
---|
| 33 | #include <align.h>
|
---|
[05882233] | 34 | #include <barrier.h>
|
---|
[cb89430] | 35 | #include <usb/debug.h>
|
---|
| 36 | #include "hw_struct/trb.h"
|
---|
[f4eb6c93] | 37 | #include "trb_ring.h"
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
[998773d] | 40 | * A structure representing a segment of a TRB ring.
|
---|
[f4eb6c93] | 41 | */
|
---|
[998773d] | 42 |
|
---|
| 43 | #define SEGMENT_FOOTER_SIZE (sizeof(link_t) + sizeof(uintptr_t))
|
---|
| 44 |
|
---|
| 45 | #define SEGMENT_TRB_COUNT ((PAGE_SIZE - SEGMENT_FOOTER_SIZE) / sizeof(xhci_trb_t))
|
---|
| 46 | #define SEGMENT_TRB_USEFUL_COUNT (SEGMENT_TRB_COUNT - 1)
|
---|
[f4eb6c93] | 47 |
|
---|
| 48 | struct trb_segment {
|
---|
[cb89430] | 49 | xhci_trb_t trb_storage [SEGMENT_TRB_COUNT];
|
---|
[f4eb6c93] | 50 |
|
---|
| 51 | link_t segments_link;
|
---|
| 52 | uintptr_t phys;
|
---|
| 53 | } __attribute__((aligned(PAGE_SIZE)));
|
---|
| 54 |
|
---|
[998773d] | 55 | static_assert(sizeof(trb_segment_t) == PAGE_SIZE);
|
---|
| 56 |
|
---|
[f4eb6c93] | 57 |
|
---|
[eb928c4] | 58 | /**
|
---|
| 59 | * Get the first TRB of a segment.
|
---|
| 60 | */
|
---|
[f4eb6c93] | 61 | static inline xhci_trb_t *segment_begin(trb_segment_t *segment)
|
---|
| 62 | {
|
---|
| 63 | return segment->trb_storage;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[eb928c4] | 66 | /**
|
---|
| 67 | * Get the one-past-end TRB of a segment.
|
---|
| 68 | */
|
---|
[f4eb6c93] | 69 | static inline xhci_trb_t *segment_end(trb_segment_t *segment)
|
---|
| 70 | {
|
---|
| 71 | return segment_begin(segment) + SEGMENT_TRB_COUNT;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[998773d] | 74 | /**
|
---|
| 75 | * Return a first segment of a list of segments.
|
---|
| 76 | */
|
---|
| 77 | static inline trb_segment_t *get_first_segment(list_t *segments)
|
---|
| 78 | {
|
---|
| 79 | return list_get_instance(list_first(segments), trb_segment_t, segments_link);
|
---|
| 80 |
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[cb89430] | 83 | /**
|
---|
| 84 | * Allocate and initialize new segment.
|
---|
| 85 | *
|
---|
| 86 | * TODO: When the HC supports 64-bit addressing, there's no need to restrict
|
---|
| 87 | * to DMAMEM_4GiB.
|
---|
| 88 | */
|
---|
[45457265] | 89 | static errno_t trb_segment_alloc(trb_segment_t **segment)
|
---|
[f4eb6c93] | 90 | {
|
---|
[1d758fc] | 91 | *segment = AS_AREA_ANY;
|
---|
| 92 | uintptr_t phys;
|
---|
[f4eb6c93] | 93 |
|
---|
[1d758fc] | 94 | const int err = dmamem_map_anonymous(PAGE_SIZE,
|
---|
| 95 | DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0,
|
---|
| 96 | &phys, (void **) segment);
|
---|
[b80c1ab] | 97 | if (err)
|
---|
| 98 | return err;
|
---|
[cb89430] | 99 |
|
---|
[b80c1ab] | 100 | memset(*segment, 0, PAGE_SIZE);
|
---|
[1d758fc] | 101 | (*segment)->phys = phys;
|
---|
[defaab2] | 102 | usb_log_debug("Allocated new ring segment.");
|
---|
[b80c1ab] | 103 | return EOK;
|
---|
| 104 | }
|
---|
[f4eb6c93] | 105 |
|
---|
[b80c1ab] | 106 | static void trb_segment_free(trb_segment_t *segment)
|
---|
| 107 | {
|
---|
[1d758fc] | 108 | dmamem_unmap_anonymous(segment);
|
---|
[f4eb6c93] | 109 | }
|
---|
| 110 |
|
---|
[cb89430] | 111 | /**
|
---|
| 112 | * Initializes the ring with one segment.
|
---|
[998773d] | 113 | *
|
---|
| 114 | * @param[in] initial_size A number of free slots on the ring, 0 leaves the
|
---|
| 115 | * choice on a reasonable default (one page-sized segment).
|
---|
[cb89430] | 116 | */
|
---|
[45457265] | 117 | errno_t xhci_trb_ring_init(xhci_trb_ring_t *ring, size_t initial_size)
|
---|
[f4eb6c93] | 118 | {
|
---|
[45457265] | 119 | errno_t err;
|
---|
[998773d] | 120 | if (initial_size == 0)
|
---|
| 121 | initial_size = SEGMENT_TRB_USEFUL_COUNT;
|
---|
[f4eb6c93] | 122 |
|
---|
[cb89430] | 123 | list_initialize(&ring->segments);
|
---|
[3bacee1] | 124 | size_t segment_count = (initial_size + SEGMENT_TRB_USEFUL_COUNT - 1) /
|
---|
| 125 | SEGMENT_TRB_USEFUL_COUNT;
|
---|
[cb89430] | 126 |
|
---|
[998773d] | 127 | for (size_t i = 0; i < segment_count; ++i) {
|
---|
| 128 | struct trb_segment *segment;
|
---|
| 129 | if ((err = trb_segment_alloc(&segment)) != EOK)
|
---|
| 130 | return err;
|
---|
[f4eb6c93] | 131 |
|
---|
[998773d] | 132 | list_append(&segment->segments_link, &ring->segments);
|
---|
| 133 | ring->segment_count = i + 1;
|
---|
| 134 | }
|
---|
[f4eb6c93] | 135 |
|
---|
[3bacee1] | 136 | trb_segment_t *const segment = get_first_segment(&ring->segments);
|
---|
[f4eb6c93] | 137 | xhci_trb_t *last = segment_end(segment) - 1;
|
---|
| 138 | xhci_trb_link_fill(last, segment->phys);
|
---|
[a501aaba] | 139 | TRB_LINK_SET_TC(*last, true);
|
---|
[f4eb6c93] | 140 |
|
---|
| 141 | ring->enqueue_segment = segment;
|
---|
| 142 | ring->enqueue_trb = segment_begin(segment);
|
---|
| 143 | ring->dequeue = segment->phys;
|
---|
| 144 | ring->pcs = 1;
|
---|
| 145 |
|
---|
[a2b0ba3] | 146 | fibril_mutex_initialize(&ring->guard);
|
---|
| 147 |
|
---|
[f4eb6c93] | 148 | return EOK;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[eb928c4] | 151 | /**
|
---|
| 152 | * Free all segments inside the ring.
|
---|
| 153 | */
|
---|
[9620a54] | 154 | void xhci_trb_ring_fini(xhci_trb_ring_t *ring)
|
---|
[f4eb6c93] | 155 | {
|
---|
[47ab89e] | 156 | assert(ring);
|
---|
[3d8a3bd] | 157 |
|
---|
[47ab89e] | 158 | list_foreach_safe(ring->segments, cur, next) {
|
---|
[8033f89] | 159 | trb_segment_t *segment =
|
---|
| 160 | list_get_instance(cur, trb_segment_t, segments_link);
|
---|
[b80c1ab] | 161 | trb_segment_free(segment);
|
---|
[47ab89e] | 162 | }
|
---|
[f4eb6c93] | 163 | }
|
---|
| 164 |
|
---|
| 165 | /**
|
---|
| 166 | * When the enqueue pointer targets a Link TRB, resolve it.
|
---|
| 167 | *
|
---|
| 168 | * Relies on segments being in the segment list in linked order.
|
---|
| 169 | *
|
---|
| 170 | * According to section 4.9.2.2, figure 16, the link TRBs cannot be chained, so
|
---|
| 171 | * it shall not be called in cycle, nor have an inner cycle.
|
---|
| 172 | */
|
---|
| 173 | static void trb_ring_resolve_link(xhci_trb_ring_t *ring)
|
---|
| 174 | {
|
---|
[8033f89] | 175 | link_t *next_segment =
|
---|
| 176 | list_next(&ring->enqueue_segment->segments_link, &ring->segments);
|
---|
[f4eb6c93] | 177 | if (!next_segment)
|
---|
| 178 | next_segment = list_first(&ring->segments);
|
---|
[4a00bc9] | 179 | assert(next_segment);
|
---|
[f4eb6c93] | 180 |
|
---|
[8033f89] | 181 | ring->enqueue_segment =
|
---|
| 182 | list_get_instance(next_segment, trb_segment_t, segments_link);
|
---|
[f4eb6c93] | 183 | ring->enqueue_trb = segment_begin(ring->enqueue_segment);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[eb928c4] | 186 | /**
|
---|
| 187 | * Get the physical address of the enqueue pointer.
|
---|
| 188 | */
|
---|
[cb89430] | 189 | static uintptr_t trb_ring_enqueue_phys(xhci_trb_ring_t *ring)
|
---|
[f4eb6c93] | 190 | {
|
---|
[19f0048] | 191 | size_t trb_id = ring->enqueue_trb - segment_begin(ring->enqueue_segment);
|
---|
[f4eb6c93] | 192 | return ring->enqueue_segment->phys + trb_id * sizeof(xhci_trb_t);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[eb928c4] | 195 | /**
|
---|
| 196 | * Decides whether the TRB will trigger an interrupt after being processed.
|
---|
| 197 | */
|
---|
[2b61945] | 198 | static bool trb_generates_interrupt(xhci_trb_t *trb)
|
---|
| 199 | {
|
---|
[3bacee1] | 200 | return TRB_TYPE(*trb) >= XHCI_TRB_TYPE_ENABLE_SLOT_CMD ||
|
---|
| 201 | TRB_IOC(*trb);
|
---|
[2b61945] | 202 | }
|
---|
| 203 |
|
---|
[cb89430] | 204 | /**
|
---|
[eb928c4] | 205 | * Enqueue TD composed of TRBs.
|
---|
[cb89430] | 206 | *
|
---|
[d1d7a92] | 207 | * This will copy specified number of TRBs chained together into the ring. The
|
---|
| 208 | * cycle flag in TRBs may be changed.
|
---|
[cb89430] | 209 | *
|
---|
[d1d7a92] | 210 | * The copied TRBs must be contiguous in memory, and must not contain Link TRBs.
|
---|
[cb89430] | 211 | *
|
---|
[8033f89] | 212 | * We cannot avoid the copying, because the TRB in ring should be updated
|
---|
| 213 | * atomically.
|
---|
[cb89430] | 214 | *
|
---|
[d1d7a92] | 215 | * @param first_trb the first TRB
|
---|
| 216 | * @param trbs number of TRBS to enqueue
|
---|
| 217 | * @param phys returns address of the last TRB enqueued
|
---|
[cb89430] | 218 | * @return EOK on success,
|
---|
| 219 | * EAGAIN when the ring is too full to fit all TRBs (temporary)
|
---|
| 220 | */
|
---|
[45457265] | 221 | errno_t xhci_trb_ring_enqueue_multiple(xhci_trb_ring_t *ring, xhci_trb_t *first_trb,
|
---|
[3bacee1] | 222 | size_t trbs, uintptr_t *phys)
|
---|
[f4eb6c93] | 223 | {
|
---|
[45457265] | 224 | errno_t err;
|
---|
[d1d7a92] | 225 | assert(trbs > 0);
|
---|
[af16ebe] | 226 |
|
---|
| 227 | if (trbs > xhci_trb_ring_size(ring))
|
---|
| 228 | return ELIMIT;
|
---|
| 229 |
|
---|
[a2b0ba3] | 230 | fibril_mutex_lock(&ring->guard);
|
---|
| 231 |
|
---|
[3bacee1] | 232 | xhci_trb_t *const saved_enqueue_trb = ring->enqueue_trb;
|
---|
| 233 | trb_segment_t *const saved_enqueue_segment = ring->enqueue_segment;
|
---|
[548c123] | 234 | if (phys)
|
---|
[cc9ac7c] | 235 | *phys = (uintptr_t)NULL;
|
---|
[f4eb6c93] | 236 |
|
---|
| 237 | /*
|
---|
| 238 | * First, dry run and advance the enqueue pointer to see if the ring would
|
---|
| 239 | * be full anytime during the transaction.
|
---|
| 240 | */
|
---|
[d1d7a92] | 241 | xhci_trb_t *trb = first_trb;
|
---|
| 242 | for (size_t i = 0; i < trbs; ++i, ++trb) {
|
---|
[4a00bc9] | 243 | if (phys && trb_generates_interrupt(trb)) {
|
---|
[9ff99e8] | 244 | if (*phys) {
|
---|
| 245 | err = ENOTSUP;
|
---|
| 246 | goto err;
|
---|
| 247 | }
|
---|
[2b61945] | 248 | *phys = trb_ring_enqueue_phys(ring);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[f4eb6c93] | 251 | ring->enqueue_trb++;
|
---|
| 252 |
|
---|
| 253 | if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK)
|
---|
| 254 | trb_ring_resolve_link(ring);
|
---|
| 255 |
|
---|
[9ff99e8] | 256 | if (trb_ring_enqueue_phys(ring) == ring->dequeue) {
|
---|
| 257 | err = EAGAIN;
|
---|
| 258 | goto err;
|
---|
| 259 | }
|
---|
[d1d7a92] | 260 | }
|
---|
[f4eb6c93] | 261 |
|
---|
| 262 | ring->enqueue_segment = saved_enqueue_segment;
|
---|
| 263 | ring->enqueue_trb = saved_enqueue_trb;
|
---|
| 264 |
|
---|
| 265 | /*
|
---|
| 266 | * Now, copy the TRBs without further checking.
|
---|
| 267 | */
|
---|
[d1d7a92] | 268 | trb = first_trb;
|
---|
| 269 | for (size_t i = 0; i < trbs; ++i, ++trb) {
|
---|
[a501aaba] | 270 | TRB_SET_CYCLE(*trb, ring->pcs);
|
---|
[887c9de] | 271 | xhci_trb_copy_to_pio(ring->enqueue_trb, trb);
|
---|
[f4eb6c93] | 272 |
|
---|
[cb89430] | 273 | usb_log_debug2("TRB ring(%p): Enqueued TRB %p", ring, trb);
|
---|
[f4eb6c93] | 274 | ring->enqueue_trb++;
|
---|
| 275 |
|
---|
| 276 | if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK) {
|
---|
[a501aaba] | 277 | TRB_SET_CYCLE(*ring->enqueue_trb, ring->pcs);
|
---|
[f4eb6c93] | 278 |
|
---|
[cb89430] | 279 | if (TRB_LINK_TC(*ring->enqueue_trb)) {
|
---|
[f4eb6c93] | 280 | ring->pcs = !ring->pcs;
|
---|
[defaab2] | 281 | usb_log_debug("TRB ring(%p): PCS toggled", ring);
|
---|
[cb89430] | 282 | }
|
---|
[f4eb6c93] | 283 |
|
---|
| 284 | trb_ring_resolve_link(ring);
|
---|
| 285 | }
|
---|
[d1d7a92] | 286 | }
|
---|
[f4eb6c93] | 287 |
|
---|
[a2b0ba3] | 288 | fibril_mutex_unlock(&ring->guard);
|
---|
[f4eb6c93] | 289 | return EOK;
|
---|
| 290 |
|
---|
[9ff99e8] | 291 | err:
|
---|
[f4eb6c93] | 292 | ring->enqueue_segment = saved_enqueue_segment;
|
---|
| 293 | ring->enqueue_trb = saved_enqueue_trb;
|
---|
[a2b0ba3] | 294 | fibril_mutex_unlock(&ring->guard);
|
---|
[9ff99e8] | 295 | return err;
|
---|
[f4eb6c93] | 296 | }
|
---|
[cb89430] | 297 |
|
---|
[d1d7a92] | 298 | /**
|
---|
| 299 | * Enqueue TD composed of a single TRB. See: `xhci_trb_ring_enqueue_multiple`
|
---|
| 300 | */
|
---|
[45457265] | 301 | errno_t xhci_trb_ring_enqueue(xhci_trb_ring_t *ring, xhci_trb_t *td, uintptr_t *phys)
|
---|
[d1d7a92] | 302 | {
|
---|
| 303 | return xhci_trb_ring_enqueue_multiple(ring, td, 1, phys);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[fb28cde] | 306 | void xhci_trb_ring_reset_dequeue_state(xhci_trb_ring_t *ring, uintptr_t *addr)
|
---|
| 307 | {
|
---|
| 308 | assert(ring);
|
---|
| 309 |
|
---|
| 310 | ring->dequeue = trb_ring_enqueue_phys(ring);
|
---|
| 311 |
|
---|
| 312 | if (addr)
|
---|
| 313 | *addr = ring->dequeue | ring->pcs;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[af16ebe] | 316 | size_t xhci_trb_ring_size(xhci_trb_ring_t *ring)
|
---|
| 317 | {
|
---|
| 318 | return ring->segment_count * SEGMENT_TRB_USEFUL_COUNT;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[cb89430] | 321 | /**
|
---|
| 322 | * Initializes an event ring.
|
---|
[998773d] | 323 | *
|
---|
| 324 | * @param[in] initial_size A number of free slots on the ring, 0 leaves the
|
---|
| 325 | * choice on a reasonable default (one page-sized segment).
|
---|
[cb89430] | 326 | */
|
---|
[45457265] | 327 | errno_t xhci_event_ring_init(xhci_event_ring_t *ring, size_t initial_size)
|
---|
[cb89430] | 328 | {
|
---|
[45457265] | 329 | errno_t err;
|
---|
[998773d] | 330 | if (initial_size == 0)
|
---|
| 331 | initial_size = SEGMENT_TRB_COUNT;
|
---|
[cb89430] | 332 |
|
---|
| 333 | list_initialize(&ring->segments);
|
---|
| 334 |
|
---|
[998773d] | 335 | size_t segment_count = (initial_size + SEGMENT_TRB_COUNT - 1) / SEGMENT_TRB_COUNT;
|
---|
| 336 | size_t erst_size = segment_count * sizeof(xhci_erst_entry_t);
|
---|
[cb89430] | 337 |
|
---|
[998773d] | 338 | if (dma_buffer_alloc(&ring->erst, erst_size)) {
|
---|
[eb928c4] | 339 | xhci_event_ring_fini(ring);
|
---|
[cb89430] | 340 | return ENOMEM;
|
---|
[eb928c4] | 341 | }
|
---|
[998773d] | 342 |
|
---|
[b80c1ab] | 343 | xhci_erst_entry_t *erst = ring->erst.virt;
|
---|
[998773d] | 344 | memset(erst, 0, erst_size);
|
---|
| 345 |
|
---|
| 346 | for (size_t i = 0; i < segment_count; i++) {
|
---|
| 347 | trb_segment_t *segment;
|
---|
| 348 | if ((err = trb_segment_alloc(&segment)) != EOK) {
|
---|
| 349 | xhci_event_ring_fini(ring);
|
---|
| 350 | return err;
|
---|
| 351 | }
|
---|
[cb89430] | 352 |
|
---|
[998773d] | 353 | list_append(&segment->segments_link, &ring->segments);
|
---|
| 354 | ring->segment_count = i + 1;
|
---|
| 355 | xhci_fill_erst_entry(&erst[i], segment->phys, SEGMENT_TRB_COUNT);
|
---|
| 356 | }
|
---|
[cb89430] | 357 |
|
---|
[19f0048] | 358 | fibril_mutex_initialize(&ring->guard);
|
---|
| 359 |
|
---|
| 360 | usb_log_debug("Initialized event ring.");
|
---|
| 361 | return EOK;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | void xhci_event_ring_reset(xhci_event_ring_t *ring)
|
---|
| 365 | {
|
---|
| 366 | list_foreach(ring->segments, segments_link, trb_segment_t, segment)
|
---|
[774aa332] | 367 | memset(segment->trb_storage, 0, sizeof(segment->trb_storage));
|
---|
[19f0048] | 368 |
|
---|
[3bacee1] | 369 | trb_segment_t *const segment = get_first_segment(&ring->segments);
|
---|
[998773d] | 370 | ring->dequeue_segment = segment;
|
---|
| 371 | ring->dequeue_trb = segment_begin(segment);
|
---|
| 372 | ring->dequeue_ptr = segment->phys;
|
---|
[cb89430] | 373 | ring->ccs = 1;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[9620a54] | 376 | void xhci_event_ring_fini(xhci_event_ring_t *ring)
|
---|
[cb89430] | 377 | {
|
---|
[47ab89e] | 378 | list_foreach_safe(ring->segments, cur, next) {
|
---|
| 379 | trb_segment_t *segment = list_get_instance(cur, trb_segment_t, segments_link);
|
---|
[998773d] | 380 | trb_segment_free(segment);
|
---|
[47ab89e] | 381 | }
|
---|
[cb89430] | 382 |
|
---|
[b80c1ab] | 383 | dma_buffer_free(&ring->erst);
|
---|
[cb89430] | 384 | }
|
---|
| 385 |
|
---|
[eb928c4] | 386 | /**
|
---|
| 387 | * Get the physical address of the dequeue pointer.
|
---|
| 388 | */
|
---|
[cb89430] | 389 | static uintptr_t event_ring_dequeue_phys(xhci_event_ring_t *ring)
|
---|
| 390 | {
|
---|
| 391 | uintptr_t trb_id = ring->dequeue_trb - segment_begin(ring->dequeue_segment);
|
---|
| 392 | return ring->dequeue_segment->phys + trb_id * sizeof(xhci_trb_t);
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /**
|
---|
| 396 | * Fill the event with next valid event from the ring.
|
---|
| 397 | *
|
---|
| 398 | * @param event pointer to event to be overwritten
|
---|
| 399 | * @return EOK on success,
|
---|
| 400 | * ENOENT when the ring is empty
|
---|
| 401 | */
|
---|
[45457265] | 402 | errno_t xhci_event_ring_dequeue(xhci_event_ring_t *ring, xhci_trb_t *event)
|
---|
[cb89430] | 403 | {
|
---|
[a1eb7c67] | 404 | fibril_mutex_lock(&ring->guard);
|
---|
| 405 |
|
---|
[cb89430] | 406 | /**
|
---|
| 407 | * The ERDP reported to the HC is a half-phase off the one we need to
|
---|
| 408 | * maintain. Therefore, we keep it extra.
|
---|
| 409 | */
|
---|
| 410 | ring->dequeue_ptr = event_ring_dequeue_phys(ring);
|
---|
| 411 |
|
---|
[a1eb7c67] | 412 | if (TRB_CYCLE(*ring->dequeue_trb) != ring->ccs) {
|
---|
| 413 | fibril_mutex_unlock(&ring->guard);
|
---|
[cb89430] | 414 | return ENOENT; /* The ring is empty. */
|
---|
[a1eb7c67] | 415 | }
|
---|
[cb89430] | 416 |
|
---|
[74c0de0] | 417 | /* Do not reorder the Cycle bit reading with memcpy */
|
---|
| 418 | read_barrier();
|
---|
| 419 |
|
---|
[cb89430] | 420 | memcpy(event, ring->dequeue_trb, sizeof(xhci_trb_t));
|
---|
| 421 |
|
---|
| 422 | ring->dequeue_trb++;
|
---|
| 423 | const unsigned index = ring->dequeue_trb - segment_begin(ring->dequeue_segment);
|
---|
| 424 |
|
---|
| 425 | /* Wrapping around segment boundary */
|
---|
| 426 | if (index >= SEGMENT_TRB_COUNT) {
|
---|
[8033f89] | 427 | link_t *next_segment =
|
---|
| 428 | list_next(&ring->dequeue_segment->segments_link, &ring->segments);
|
---|
[cb89430] | 429 |
|
---|
| 430 | /* Wrapping around table boundary */
|
---|
| 431 | if (!next_segment) {
|
---|
| 432 | next_segment = list_first(&ring->segments);
|
---|
| 433 | ring->ccs = !ring->ccs;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
[8033f89] | 436 | ring->dequeue_segment =
|
---|
| 437 | list_get_instance(next_segment, trb_segment_t, segments_link);
|
---|
[cb89430] | 438 | ring->dequeue_trb = segment_begin(ring->dequeue_segment);
|
---|
| 439 | }
|
---|
| 440 |
|
---|
[a1eb7c67] | 441 | fibril_mutex_unlock(&ring->guard);
|
---|
[cb89430] | 442 | return EOK;
|
---|
| 443 | }
|
---|
[2bff2cc2] | 444 |
|
---|
| 445 | void xhci_sw_ring_init(xhci_sw_ring_t *ring, size_t size)
|
---|
| 446 | {
|
---|
| 447 | ring->begin = calloc(size, sizeof(xhci_trb_t));
|
---|
| 448 | ring->end = ring->begin + size;
|
---|
| 449 |
|
---|
| 450 | fibril_mutex_initialize(&ring->guard);
|
---|
| 451 | fibril_condvar_initialize(&ring->enqueued_cv);
|
---|
| 452 | fibril_condvar_initialize(&ring->dequeued_cv);
|
---|
| 453 |
|
---|
[19f0048] | 454 | xhci_sw_ring_restart(ring);
|
---|
[2bff2cc2] | 455 | }
|
---|
| 456 |
|
---|
[45457265] | 457 | errno_t xhci_sw_ring_enqueue(xhci_sw_ring_t *ring, xhci_trb_t *trb)
|
---|
[2bff2cc2] | 458 | {
|
---|
| 459 | assert(ring);
|
---|
| 460 | assert(trb);
|
---|
| 461 |
|
---|
| 462 | fibril_mutex_lock(&ring->guard);
|
---|
| 463 | while (ring->running && TRB_CYCLE(*ring->enqueue))
|
---|
| 464 | fibril_condvar_wait(&ring->dequeued_cv, &ring->guard);
|
---|
| 465 |
|
---|
| 466 | *ring->enqueue = *trb;
|
---|
| 467 | TRB_SET_CYCLE(*ring->enqueue, 1);
|
---|
| 468 | if (++ring->enqueue == ring->end)
|
---|
| 469 | ring->enqueue = ring->begin;
|
---|
| 470 | fibril_condvar_signal(&ring->enqueued_cv);
|
---|
| 471 | fibril_mutex_unlock(&ring->guard);
|
---|
| 472 |
|
---|
| 473 | return ring->running ? EOK : EINTR;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[45457265] | 476 | errno_t xhci_sw_ring_dequeue(xhci_sw_ring_t *ring, xhci_trb_t *trb)
|
---|
[2bff2cc2] | 477 | {
|
---|
| 478 | assert(ring);
|
---|
| 479 | assert(trb);
|
---|
| 480 |
|
---|
| 481 | fibril_mutex_lock(&ring->guard);
|
---|
| 482 | while (ring->running && !TRB_CYCLE(*ring->dequeue))
|
---|
| 483 | fibril_condvar_wait(&ring->enqueued_cv, &ring->guard);
|
---|
| 484 |
|
---|
| 485 | *trb = *ring->dequeue;
|
---|
| 486 | TRB_SET_CYCLE(*ring->dequeue, 0);
|
---|
| 487 | if (++ring->dequeue == ring->end)
|
---|
| 488 | ring->dequeue = ring->begin;
|
---|
| 489 | fibril_condvar_signal(&ring->dequeued_cv);
|
---|
| 490 | fibril_mutex_unlock(&ring->guard);
|
---|
| 491 |
|
---|
| 492 | return ring->running ? EOK : EINTR;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | void xhci_sw_ring_stop(xhci_sw_ring_t *ring)
|
---|
| 496 | {
|
---|
| 497 | ring->running = false;
|
---|
| 498 | fibril_condvar_broadcast(&ring->enqueued_cv);
|
---|
| 499 | fibril_condvar_broadcast(&ring->dequeued_cv);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[19f0048] | 502 | void xhci_sw_ring_restart(xhci_sw_ring_t *ring)
|
---|
| 503 | {
|
---|
| 504 | ring->enqueue = ring->dequeue = ring->begin;
|
---|
| 505 | memset(ring->begin, 0, sizeof(xhci_trb_t) * (ring->end - ring->begin));
|
---|
| 506 | ring->running = true;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[2bff2cc2] | 509 | void xhci_sw_ring_fini(xhci_sw_ring_t *ring)
|
---|
| 510 | {
|
---|
| 511 | free(ring->begin);
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 | /**
|
---|
| 515 | * @}
|
---|
| 516 | */
|
---|