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