source: mainline/uspace/drv/bus/usb/xhci/trb_ring.c@ ba2fc2c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ba2fc2c was b80c1ab, checked in by Aearsis <Hlavaty.Ondrej@…>, 8 years ago

xhci: use dma_buffers instead of malloc32 util

A bit of refactoring was needed to adapt scratchpad buffers.

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