| 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 | #include <errno.h>
|
|---|
| 30 | #include <assert.h>
|
|---|
| 31 | #include <ddi.h>
|
|---|
| 32 | #include <as.h>
|
|---|
| 33 | #include <align.h>
|
|---|
| 34 | #include <usb/debug.h>
|
|---|
| 35 | #include <usb/host/utils/malloc32.h>
|
|---|
| 36 | #include "hw_struct/trb.h"
|
|---|
| 37 | #include "trb_ring.h"
|
|---|
| 38 |
|
|---|
| 39 | #define SEGMENT_HEADER_SIZE (sizeof(link_t) + sizeof(uintptr_t))
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Number of TRBs in a segment (with our header).
|
|---|
| 43 | */
|
|---|
| 44 | #define SEGMENT_TRB_COUNT ((PAGE_SIZE - SEGMENT_HEADER_SIZE) / sizeof(xhci_trb_t))
|
|---|
| 45 |
|
|---|
| 46 | struct trb_segment {
|
|---|
| 47 | xhci_trb_t trb_storage [SEGMENT_TRB_COUNT];
|
|---|
| 48 |
|
|---|
| 49 | link_t segments_link;
|
|---|
| 50 | uintptr_t phys;
|
|---|
| 51 | } __attribute__((aligned(PAGE_SIZE)));
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | static inline xhci_trb_t *segment_begin(trb_segment_t *segment)
|
|---|
| 55 | {
|
|---|
| 56 | return segment->trb_storage;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | static inline xhci_trb_t *segment_end(trb_segment_t *segment)
|
|---|
| 60 | {
|
|---|
| 61 | return segment_begin(segment) + SEGMENT_TRB_COUNT;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Allocate and initialize new segment.
|
|---|
| 66 | *
|
|---|
| 67 | * TODO: When the HC supports 64-bit addressing, there's no need to restrict
|
|---|
| 68 | * to DMAMEM_4GiB.
|
|---|
| 69 | */
|
|---|
| 70 | static int trb_segment_allocate(trb_segment_t **segment)
|
|---|
| 71 | {
|
|---|
| 72 | uintptr_t phys;
|
|---|
| 73 | int err;
|
|---|
| 74 |
|
|---|
| 75 | *segment = AS_AREA_ANY;
|
|---|
| 76 | err = dmamem_map_anonymous(PAGE_SIZE,
|
|---|
| 77 | DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0, &phys,
|
|---|
| 78 | (void *) segment);
|
|---|
| 79 |
|
|---|
| 80 | if (err == EOK) {
|
|---|
| 81 | memset(*segment, 0, PAGE_SIZE);
|
|---|
| 82 | (*segment)->phys = phys;
|
|---|
| 83 |
|
|---|
| 84 | usb_log_debug2("Allocated new ring segment.");
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | return err;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Initializes the ring with one segment.
|
|---|
| 92 | * Event when it fails, the structure needs to be finalized.
|
|---|
| 93 | */
|
|---|
| 94 | int xhci_trb_ring_init(xhci_trb_ring_t *ring, xhci_hc_t *hc)
|
|---|
| 95 | {
|
|---|
| 96 | struct trb_segment *segment;
|
|---|
| 97 | int err;
|
|---|
| 98 |
|
|---|
| 99 | list_initialize(&ring->segments);
|
|---|
| 100 |
|
|---|
| 101 | if ((err = trb_segment_allocate(&segment)) != EOK)
|
|---|
| 102 | return err;
|
|---|
| 103 |
|
|---|
| 104 | list_append(&segment->segments_link, &ring->segments);
|
|---|
| 105 | ring->segment_count = 1;
|
|---|
| 106 |
|
|---|
| 107 | xhci_trb_t *last = segment_end(segment) - 1;
|
|---|
| 108 | xhci_trb_link_fill(last, segment->phys);
|
|---|
| 109 | xhci_trb_set_cycle(last, true);
|
|---|
| 110 |
|
|---|
| 111 | ring->enqueue_segment = segment;
|
|---|
| 112 | ring->enqueue_trb = segment_begin(segment);
|
|---|
| 113 | ring->dequeue = segment->phys;
|
|---|
| 114 | ring->pcs = 1;
|
|---|
| 115 |
|
|---|
| 116 | usb_log_debug("Initialized new TRB ring.");
|
|---|
| 117 |
|
|---|
| 118 | return EOK;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | int xhci_trb_ring_fini(xhci_trb_ring_t *ring)
|
|---|
| 122 | {
|
|---|
| 123 | if (!ring)
|
|---|
| 124 | return EOK;
|
|---|
| 125 |
|
|---|
| 126 | list_foreach(ring->segments, segments_link, trb_segment_t, segment)
|
|---|
| 127 | dmamem_unmap_anonymous(segment);
|
|---|
| 128 | return EOK;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * When the enqueue pointer targets a Link TRB, resolve it.
|
|---|
| 133 | *
|
|---|
| 134 | * Relies on segments being in the segment list in linked order.
|
|---|
| 135 | *
|
|---|
| 136 | * According to section 4.9.2.2, figure 16, the link TRBs cannot be chained, so
|
|---|
| 137 | * it shall not be called in cycle, nor have an inner cycle.
|
|---|
| 138 | */
|
|---|
| 139 | static void trb_ring_resolve_link(xhci_trb_ring_t *ring)
|
|---|
| 140 | {
|
|---|
| 141 | link_t *next_segment = list_next(&ring->enqueue_segment->segments_link, &ring->segments);
|
|---|
| 142 | if (!next_segment)
|
|---|
| 143 | next_segment = list_first(&ring->segments);
|
|---|
| 144 |
|
|---|
| 145 | ring->enqueue_segment = list_get_instance(next_segment, trb_segment_t, segments_link);
|
|---|
| 146 | ring->enqueue_trb = segment_begin(ring->enqueue_segment);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | static uintptr_t trb_ring_enqueue_phys(xhci_trb_ring_t *ring)
|
|---|
| 150 | {
|
|---|
| 151 | uintptr_t trb_id = ring->enqueue_trb - segment_begin(ring->enqueue_segment);
|
|---|
| 152 | return ring->enqueue_segment->phys + trb_id * sizeof(xhci_trb_t);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Enqueue a TD composed of TRBs.
|
|---|
| 157 | *
|
|---|
| 158 | * This will copy all TRBs chained together into the ring. The cycle flag in
|
|---|
| 159 | * TRBs may be changed.
|
|---|
| 160 | *
|
|---|
| 161 | * The chained TRBs must be contiguous in memory, and must not contain Link TRBs.
|
|---|
| 162 | *
|
|---|
| 163 | * We cannot avoid the copying, because the TRB in ring should be updated atomically.
|
|---|
| 164 | *
|
|---|
| 165 | * @param td the first TRB of TD
|
|---|
| 166 | * @param phys returns address of the first TRB enqueued
|
|---|
| 167 | * @return EOK on success,
|
|---|
| 168 | * EAGAIN when the ring is too full to fit all TRBs (temporary)
|
|---|
| 169 | */
|
|---|
| 170 | int xhci_trb_ring_enqueue(xhci_trb_ring_t *ring, xhci_trb_t *td, uintptr_t *phys)
|
|---|
| 171 | {
|
|---|
| 172 | xhci_trb_t * const saved_enqueue_trb = ring->enqueue_trb;
|
|---|
| 173 | trb_segment_t * const saved_enqueue_segment = ring->enqueue_segment;
|
|---|
| 174 | if (phys)
|
|---|
| 175 | *phys = (uintptr_t)NULL;
|
|---|
| 176 |
|
|---|
| 177 | /*
|
|---|
| 178 | * First, dry run and advance the enqueue pointer to see if the ring would
|
|---|
| 179 | * be full anytime during the transaction.
|
|---|
| 180 | */
|
|---|
| 181 | xhci_trb_t *trb = td;
|
|---|
| 182 | do {
|
|---|
| 183 | ring->enqueue_trb++;
|
|---|
| 184 |
|
|---|
| 185 | if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK)
|
|---|
| 186 | trb_ring_resolve_link(ring);
|
|---|
| 187 |
|
|---|
| 188 | if (trb_ring_enqueue_phys(ring) == ring->dequeue)
|
|---|
| 189 | goto err_again;
|
|---|
| 190 | } while (xhci_trb_is_chained(trb++));
|
|---|
| 191 |
|
|---|
| 192 | ring->enqueue_segment = saved_enqueue_segment;
|
|---|
| 193 | ring->enqueue_trb = saved_enqueue_trb;
|
|---|
| 194 | if (phys)
|
|---|
| 195 | *phys = trb_ring_enqueue_phys(ring);
|
|---|
| 196 |
|
|---|
| 197 | /*
|
|---|
| 198 | * Now, copy the TRBs without further checking.
|
|---|
| 199 | */
|
|---|
| 200 | trb = td;
|
|---|
| 201 | do {
|
|---|
| 202 | xhci_trb_set_cycle(trb, ring->pcs);
|
|---|
| 203 | xhci_trb_copy(ring->enqueue_trb, trb);
|
|---|
| 204 |
|
|---|
| 205 | usb_log_debug2("TRB ring(%p): Enqueued TRB %p", ring, trb);
|
|---|
| 206 | ring->enqueue_trb++;
|
|---|
| 207 |
|
|---|
| 208 | if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK) {
|
|---|
| 209 | // XXX: Check, whether the order here is correct (ambiguous instructions in 4.11.5.1)
|
|---|
| 210 | xhci_trb_set_cycle(ring->enqueue_trb, ring->pcs);
|
|---|
| 211 |
|
|---|
| 212 | if (TRB_LINK_TC(*ring->enqueue_trb)) {
|
|---|
| 213 | ring->pcs = !ring->pcs;
|
|---|
| 214 | usb_log_debug2("TRB ring(%p): PCS toggled", ring);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | trb_ring_resolve_link(ring);
|
|---|
| 218 | }
|
|---|
| 219 | } while (xhci_trb_is_chained(trb++));
|
|---|
| 220 |
|
|---|
| 221 | return EOK;
|
|---|
| 222 |
|
|---|
| 223 | err_again:
|
|---|
| 224 | ring->enqueue_segment = saved_enqueue_segment;
|
|---|
| 225 | ring->enqueue_trb = saved_enqueue_trb;
|
|---|
| 226 | return EAGAIN;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /**
|
|---|
| 230 | * Initializes an event ring.
|
|---|
| 231 | * Even when it fails, the structure needs to be finalized.
|
|---|
| 232 | */
|
|---|
| 233 | int xhci_event_ring_init(xhci_event_ring_t *ring, xhci_hc_t *hc)
|
|---|
| 234 | {
|
|---|
| 235 | struct trb_segment *segment;
|
|---|
| 236 | int err;
|
|---|
| 237 |
|
|---|
| 238 | list_initialize(&ring->segments);
|
|---|
| 239 |
|
|---|
| 240 | if ((err = trb_segment_allocate(&segment)) != EOK)
|
|---|
| 241 | return err;
|
|---|
| 242 |
|
|---|
| 243 | list_append(&segment->segments_link, &ring->segments);
|
|---|
| 244 | ring->segment_count = 1;
|
|---|
| 245 |
|
|---|
| 246 | ring->dequeue_segment = segment;
|
|---|
| 247 | ring->dequeue_trb = segment_begin(segment);
|
|---|
| 248 | ring->dequeue_ptr = segment->phys;
|
|---|
| 249 |
|
|---|
| 250 | ring->erst = malloc32(PAGE_SIZE);
|
|---|
| 251 | if (ring->erst == NULL)
|
|---|
| 252 | return ENOMEM;
|
|---|
| 253 | memset(ring->erst, 0, PAGE_SIZE);
|
|---|
| 254 |
|
|---|
| 255 | xhci_fill_erst_entry(&ring->erst[0], segment->phys, SEGMENT_TRB_COUNT);
|
|---|
| 256 |
|
|---|
| 257 | ring->ccs = 1;
|
|---|
| 258 |
|
|---|
| 259 | usb_log_debug("Initialized event ring.");
|
|---|
| 260 |
|
|---|
| 261 | return EOK;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | int xhci_event_ring_fini(xhci_event_ring_t *ring)
|
|---|
| 265 | {
|
|---|
| 266 | list_foreach(ring->segments, segments_link, trb_segment_t, segment)
|
|---|
| 267 | dmamem_unmap_anonymous(segment);
|
|---|
| 268 |
|
|---|
| 269 | if (ring->erst)
|
|---|
| 270 | free32(ring->erst);
|
|---|
| 271 |
|
|---|
| 272 | return EOK;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | static uintptr_t event_ring_dequeue_phys(xhci_event_ring_t *ring)
|
|---|
| 276 | {
|
|---|
| 277 | uintptr_t trb_id = ring->dequeue_trb - segment_begin(ring->dequeue_segment);
|
|---|
| 278 | return ring->dequeue_segment->phys + trb_id * sizeof(xhci_trb_t);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /**
|
|---|
| 282 | * Fill the event with next valid event from the ring.
|
|---|
| 283 | *
|
|---|
| 284 | * @param event pointer to event to be overwritten
|
|---|
| 285 | * @return EOK on success,
|
|---|
| 286 | * ENOENT when the ring is empty
|
|---|
| 287 | */
|
|---|
| 288 | int xhci_event_ring_dequeue(xhci_event_ring_t *ring, xhci_trb_t *event)
|
|---|
| 289 | {
|
|---|
| 290 | /**
|
|---|
| 291 | * The ERDP reported to the HC is a half-phase off the one we need to
|
|---|
| 292 | * maintain. Therefore, we keep it extra.
|
|---|
| 293 | */
|
|---|
| 294 | ring->dequeue_ptr = event_ring_dequeue_phys(ring);
|
|---|
| 295 |
|
|---|
| 296 | if (TRB_CYCLE(*ring->dequeue_trb) != ring->ccs)
|
|---|
| 297 | return ENOENT; /* The ring is empty. */
|
|---|
| 298 |
|
|---|
| 299 | memcpy(event, ring->dequeue_trb, sizeof(xhci_trb_t));
|
|---|
| 300 |
|
|---|
| 301 | ring->dequeue_trb++;
|
|---|
| 302 | const unsigned index = ring->dequeue_trb - segment_begin(ring->dequeue_segment);
|
|---|
| 303 |
|
|---|
| 304 | /* Wrapping around segment boundary */
|
|---|
| 305 | if (index >= SEGMENT_TRB_COUNT) {
|
|---|
| 306 | link_t *next_segment = list_next(&ring->dequeue_segment->segments_link, &ring->segments);
|
|---|
| 307 |
|
|---|
| 308 | /* Wrapping around table boundary */
|
|---|
| 309 | if (!next_segment) {
|
|---|
| 310 | next_segment = list_first(&ring->segments);
|
|---|
| 311 | ring->ccs = !ring->ccs;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | ring->dequeue_segment = list_get_instance(next_segment, trb_segment_t, segments_link);
|
|---|
| 315 | ring->dequeue_trb = segment_begin(ring->dequeue_segment);
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | return EOK;
|
|---|
| 319 | }
|
|---|