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 <libarch/barrier.h>
|
---|
35 | #include <usb/debug.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 | /**
|
---|
55 | * Get the first TRB of a segment.
|
---|
56 | */
|
---|
57 | static inline xhci_trb_t *segment_begin(trb_segment_t *segment)
|
---|
58 | {
|
---|
59 | return segment->trb_storage;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Get the one-past-end TRB of a segment.
|
---|
64 | */
|
---|
65 | static inline xhci_trb_t *segment_end(trb_segment_t *segment)
|
---|
66 | {
|
---|
67 | return segment_begin(segment) + SEGMENT_TRB_COUNT;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Allocate and initialize new segment.
|
---|
72 | *
|
---|
73 | * TODO: When the HC supports 64-bit addressing, there's no need to restrict
|
---|
74 | * to DMAMEM_4GiB.
|
---|
75 | */
|
---|
76 | static int trb_segment_alloc(trb_segment_t **segment)
|
---|
77 | {
|
---|
78 | dma_buffer_t dbuf;
|
---|
79 |
|
---|
80 | const int err = dma_buffer_alloc(&dbuf, PAGE_SIZE);
|
---|
81 | if (err)
|
---|
82 | return err;
|
---|
83 |
|
---|
84 | *segment = dbuf.virt;
|
---|
85 | memset(*segment, 0, PAGE_SIZE);
|
---|
86 | (*segment)->phys = dbuf.phys;
|
---|
87 | usb_log_debug2("Allocated new ring segment.");
|
---|
88 | return EOK;
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void trb_segment_free(trb_segment_t *segment)
|
---|
92 | {
|
---|
93 | dma_buffer_t dbuf = { .virt = segment, .phys = segment->phys };
|
---|
94 | dma_buffer_free(&dbuf);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Initializes the ring with one segment.
|
---|
99 | */
|
---|
100 | int xhci_trb_ring_init(xhci_trb_ring_t *ring)
|
---|
101 | {
|
---|
102 | struct trb_segment *segment;
|
---|
103 | int err;
|
---|
104 |
|
---|
105 | list_initialize(&ring->segments);
|
---|
106 |
|
---|
107 | if ((err = trb_segment_alloc(&segment)) != EOK)
|
---|
108 | return err;
|
---|
109 |
|
---|
110 | list_append(&segment->segments_link, &ring->segments);
|
---|
111 | ring->segment_count = 1;
|
---|
112 |
|
---|
113 | xhci_trb_t *last = segment_end(segment) - 1;
|
---|
114 | xhci_trb_link_fill(last, segment->phys);
|
---|
115 | TRB_LINK_SET_TC(*last, true);
|
---|
116 |
|
---|
117 | ring->enqueue_segment = segment;
|
---|
118 | ring->enqueue_trb = segment_begin(segment);
|
---|
119 | ring->dequeue = segment->phys;
|
---|
120 | ring->pcs = 1;
|
---|
121 |
|
---|
122 | fibril_mutex_initialize(&ring->guard);
|
---|
123 |
|
---|
124 | return EOK;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Free all segments inside the ring.
|
---|
129 | */
|
---|
130 | void xhci_trb_ring_fini(xhci_trb_ring_t *ring)
|
---|
131 | {
|
---|
132 | assert(ring);
|
---|
133 |
|
---|
134 | list_foreach_safe(ring->segments, cur, next) {
|
---|
135 | trb_segment_t *segment = list_get_instance(cur, trb_segment_t, segments_link);
|
---|
136 | trb_segment_free(segment);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * When the enqueue pointer targets a Link TRB, resolve it.
|
---|
142 | *
|
---|
143 | * Relies on segments being in the segment list in linked order.
|
---|
144 | *
|
---|
145 | * According to section 4.9.2.2, figure 16, the link TRBs cannot be chained, so
|
---|
146 | * it shall not be called in cycle, nor have an inner cycle.
|
---|
147 | */
|
---|
148 | static void trb_ring_resolve_link(xhci_trb_ring_t *ring)
|
---|
149 | {
|
---|
150 | link_t *next_segment = list_next(&ring->enqueue_segment->segments_link, &ring->segments);
|
---|
151 | if (!next_segment)
|
---|
152 | next_segment = list_first(&ring->segments);
|
---|
153 | assert(next_segment);
|
---|
154 |
|
---|
155 | ring->enqueue_segment = list_get_instance(next_segment, trb_segment_t, segments_link);
|
---|
156 | ring->enqueue_trb = segment_begin(ring->enqueue_segment);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Get the physical address of the enqueue pointer.
|
---|
161 | */
|
---|
162 | static uintptr_t trb_ring_enqueue_phys(xhci_trb_ring_t *ring)
|
---|
163 | {
|
---|
164 | uintptr_t trb_id = ring->enqueue_trb - segment_begin(ring->enqueue_segment);
|
---|
165 | return ring->enqueue_segment->phys + trb_id * sizeof(xhci_trb_t);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Decides whether the TRB will trigger an interrupt after being processed.
|
---|
170 | */
|
---|
171 | static bool trb_generates_interrupt(xhci_trb_t *trb)
|
---|
172 | {
|
---|
173 | return TRB_TYPE(*trb) >= XHCI_TRB_TYPE_ENABLE_SLOT_CMD
|
---|
174 | || TRB_IOC(*trb);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Enqueue TD composed of TRBs.
|
---|
179 | *
|
---|
180 | * This will copy specified number of TRBs chained together into the ring. The
|
---|
181 | * cycle flag in TRBs may be changed.
|
---|
182 | *
|
---|
183 | * The copied TRBs must be contiguous in memory, and must not contain Link TRBs.
|
---|
184 | *
|
---|
185 | * We cannot avoid the copying, because the TRB in ring should be updated atomically.
|
---|
186 | *
|
---|
187 | * @param first_trb the first TRB
|
---|
188 | * @param trbs number of TRBS to enqueue
|
---|
189 | * @param phys returns address of the last TRB enqueued
|
---|
190 | * @return EOK on success,
|
---|
191 | * EAGAIN when the ring is too full to fit all TRBs (temporary)
|
---|
192 | */
|
---|
193 | int xhci_trb_ring_enqueue_multiple(xhci_trb_ring_t *ring, xhci_trb_t *first_trb,
|
---|
194 | size_t trbs, uintptr_t *phys)
|
---|
195 | {
|
---|
196 | assert(trbs > 0);
|
---|
197 | fibril_mutex_lock(&ring->guard);
|
---|
198 |
|
---|
199 | xhci_trb_t * const saved_enqueue_trb = ring->enqueue_trb;
|
---|
200 | trb_segment_t * const saved_enqueue_segment = ring->enqueue_segment;
|
---|
201 | if (phys)
|
---|
202 | *phys = (uintptr_t)NULL;
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * First, dry run and advance the enqueue pointer to see if the ring would
|
---|
206 | * be full anytime during the transaction.
|
---|
207 | */
|
---|
208 | xhci_trb_t *trb = first_trb;
|
---|
209 | for (size_t i = 0; i < trbs; ++i, ++trb) {
|
---|
210 | if (phys && trb_generates_interrupt(trb)) {
|
---|
211 | if (*phys)
|
---|
212 | return ENOTSUP;
|
---|
213 | *phys = trb_ring_enqueue_phys(ring);
|
---|
214 | }
|
---|
215 |
|
---|
216 | ring->enqueue_trb++;
|
---|
217 |
|
---|
218 | if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK)
|
---|
219 | trb_ring_resolve_link(ring);
|
---|
220 |
|
---|
221 | if (trb_ring_enqueue_phys(ring) == ring->dequeue)
|
---|
222 | goto err_again;
|
---|
223 | }
|
---|
224 |
|
---|
225 | ring->enqueue_segment = saved_enqueue_segment;
|
---|
226 | ring->enqueue_trb = saved_enqueue_trb;
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Now, copy the TRBs without further checking.
|
---|
230 | */
|
---|
231 | trb = first_trb;
|
---|
232 | for (size_t i = 0; i < trbs; ++i, ++trb) {
|
---|
233 | TRB_SET_CYCLE(*trb, ring->pcs);
|
---|
234 | xhci_trb_copy_to_pio(ring->enqueue_trb, trb);
|
---|
235 |
|
---|
236 | usb_log_debug2("TRB ring(%p): Enqueued TRB %p", ring, trb);
|
---|
237 | ring->enqueue_trb++;
|
---|
238 |
|
---|
239 | if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK) {
|
---|
240 | TRB_SET_CYCLE(*ring->enqueue_trb, ring->pcs);
|
---|
241 |
|
---|
242 | if (TRB_LINK_TC(*ring->enqueue_trb)) {
|
---|
243 | ring->pcs = !ring->pcs;
|
---|
244 | usb_log_debug2("TRB ring(%p): PCS toggled", ring);
|
---|
245 | }
|
---|
246 |
|
---|
247 | trb_ring_resolve_link(ring);
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | fibril_mutex_unlock(&ring->guard);
|
---|
252 | return EOK;
|
---|
253 |
|
---|
254 | err_again:
|
---|
255 | ring->enqueue_segment = saved_enqueue_segment;
|
---|
256 | ring->enqueue_trb = saved_enqueue_trb;
|
---|
257 | fibril_mutex_unlock(&ring->guard);
|
---|
258 | return EAGAIN;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * Enqueue TD composed of a single TRB. See: `xhci_trb_ring_enqueue_multiple`
|
---|
263 | */
|
---|
264 | int xhci_trb_ring_enqueue(xhci_trb_ring_t *ring, xhci_trb_t *td, uintptr_t *phys)
|
---|
265 | {
|
---|
266 | return xhci_trb_ring_enqueue_multiple(ring, td, 1, phys);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Initializes an event ring.
|
---|
271 | */
|
---|
272 | int xhci_event_ring_init(xhci_event_ring_t *ring)
|
---|
273 | {
|
---|
274 | struct trb_segment *segment;
|
---|
275 | int err;
|
---|
276 |
|
---|
277 | list_initialize(&ring->segments);
|
---|
278 |
|
---|
279 | if ((err = trb_segment_alloc(&segment)) != EOK)
|
---|
280 | return err;
|
---|
281 |
|
---|
282 | list_append(&segment->segments_link, &ring->segments);
|
---|
283 | ring->segment_count = 1;
|
---|
284 |
|
---|
285 | ring->dequeue_segment = segment;
|
---|
286 | ring->dequeue_trb = segment_begin(segment);
|
---|
287 | ring->dequeue_ptr = segment->phys;
|
---|
288 |
|
---|
289 | if (dma_buffer_alloc(&ring->erst, PAGE_SIZE)) {
|
---|
290 | xhci_event_ring_fini(ring);
|
---|
291 | return ENOMEM;
|
---|
292 | }
|
---|
293 | xhci_erst_entry_t *erst = ring->erst.virt;
|
---|
294 |
|
---|
295 | memset(erst, 0, PAGE_SIZE);
|
---|
296 | xhci_fill_erst_entry(&erst[0], segment->phys, SEGMENT_TRB_COUNT);
|
---|
297 |
|
---|
298 | ring->ccs = 1;
|
---|
299 |
|
---|
300 | fibril_mutex_initialize(&ring->guard);
|
---|
301 |
|
---|
302 | usb_log_debug("Initialized event ring.");
|
---|
303 |
|
---|
304 | return EOK;
|
---|
305 | }
|
---|
306 |
|
---|
307 | void xhci_event_ring_fini(xhci_event_ring_t *ring)
|
---|
308 | {
|
---|
309 | list_foreach_safe(ring->segments, cur, next) {
|
---|
310 | trb_segment_t *segment = list_get_instance(cur, trb_segment_t, segments_link);
|
---|
311 | dmamem_unmap_anonymous(segment);
|
---|
312 | }
|
---|
313 |
|
---|
314 | dma_buffer_free(&ring->erst);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Get the physical address of the dequeue pointer.
|
---|
319 | */
|
---|
320 | static uintptr_t event_ring_dequeue_phys(xhci_event_ring_t *ring)
|
---|
321 | {
|
---|
322 | uintptr_t trb_id = ring->dequeue_trb - segment_begin(ring->dequeue_segment);
|
---|
323 | return ring->dequeue_segment->phys + trb_id * sizeof(xhci_trb_t);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Fill the event with next valid event from the ring.
|
---|
328 | *
|
---|
329 | * @param event pointer to event to be overwritten
|
---|
330 | * @return EOK on success,
|
---|
331 | * ENOENT when the ring is empty
|
---|
332 | */
|
---|
333 | int xhci_event_ring_dequeue(xhci_event_ring_t *ring, xhci_trb_t *event)
|
---|
334 | {
|
---|
335 | fibril_mutex_lock(&ring->guard);
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * The ERDP reported to the HC is a half-phase off the one we need to
|
---|
339 | * maintain. Therefore, we keep it extra.
|
---|
340 | */
|
---|
341 | ring->dequeue_ptr = event_ring_dequeue_phys(ring);
|
---|
342 |
|
---|
343 | if (TRB_CYCLE(*ring->dequeue_trb) != ring->ccs) {
|
---|
344 | fibril_mutex_unlock(&ring->guard);
|
---|
345 | return ENOENT; /* The ring is empty. */
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* Do not reorder the Cycle bit reading with memcpy */
|
---|
349 | read_barrier();
|
---|
350 |
|
---|
351 | memcpy(event, ring->dequeue_trb, sizeof(xhci_trb_t));
|
---|
352 |
|
---|
353 | ring->dequeue_trb++;
|
---|
354 | const unsigned index = ring->dequeue_trb - segment_begin(ring->dequeue_segment);
|
---|
355 |
|
---|
356 | /* Wrapping around segment boundary */
|
---|
357 | if (index >= SEGMENT_TRB_COUNT) {
|
---|
358 | link_t *next_segment = list_next(&ring->dequeue_segment->segments_link, &ring->segments);
|
---|
359 |
|
---|
360 | /* Wrapping around table boundary */
|
---|
361 | if (!next_segment) {
|
---|
362 | next_segment = list_first(&ring->segments);
|
---|
363 | ring->ccs = !ring->ccs;
|
---|
364 | }
|
---|
365 |
|
---|
366 | ring->dequeue_segment = list_get_instance(next_segment, trb_segment_t, segments_link);
|
---|
367 | ring->dequeue_trb = segment_begin(ring->dequeue_segment);
|
---|
368 | }
|
---|
369 |
|
---|
370 | fibril_mutex_unlock(&ring->guard);
|
---|
371 | return EOK;
|
---|
372 | }
|
---|