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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5cbccd4 was f4eb6c93, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

xhci: hardware data structures and trb ring management

  • Property mode set to 100644
File size: 5.1 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 "trb_ring.h"
35
36#define SEGMENT_HEADER_SIZE (sizeof(link_t) + sizeof(uintptr_t))
37
38/**
39 * Number of TRBs in a segment (with our header).
40 */
41#define SEGMENT_TRB_COUNT ((PAGE_SIZE - SEGMENT_HEADER_SIZE) / sizeof(xhci_trb_t))
42
43struct trb_segment {
44 xhci_trb_t trb_storage [SEGMENT_TRB_COUNT] __attribute__((packed));
45
46 link_t segments_link;
47 uintptr_t phys;
48} __attribute__((aligned(PAGE_SIZE)));
49
50
51static inline xhci_trb_t *segment_begin(trb_segment_t *segment)
52{
53 return segment->trb_storage;
54}
55
56static inline xhci_trb_t *segment_end(trb_segment_t *segment)
57{
58 return segment_begin(segment) + SEGMENT_TRB_COUNT;
59}
60
61static int trb_segment_allocate(trb_segment_t **segment)
62{
63 uintptr_t phys;
64 int err;
65
66 *segment = AS_AREA_ANY;
67 err = dmamem_map_anonymous(PAGE_SIZE,
68 DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0, &phys,
69 (void *) segment);
70
71 if (err == EOK) {
72 memset(*segment, 0, PAGE_SIZE);
73 (*segment)->phys = phys;
74 }
75
76 return err;
77}
78
79int trb_ring_init(xhci_trb_ring_t *ring)
80{
81 struct trb_segment *segment;
82 int err;
83
84 if ((err = trb_segment_allocate(&segment)) != EOK)
85 return err;
86
87 list_initialize(&ring->segments);
88 list_append(&segment->segments_link, &ring->segments);
89
90 xhci_trb_t *last = segment_end(segment) - 1;
91 xhci_trb_link_fill(last, segment->phys);
92 xhci_trb_set_cycle(last, true);
93
94 ring->enqueue_segment = segment;
95 ring->enqueue_trb = segment_begin(segment);
96 ring->dequeue = segment->phys;
97 ring->pcs = 1;
98
99 return EOK;
100}
101
102int trb_ring_fini(xhci_trb_ring_t *ring)
103{
104 return EOK;
105}
106
107/**
108 * When the enqueue pointer targets a Link TRB, resolve it.
109 *
110 * Relies on segments being in the segment list in linked order.
111 *
112 * According to section 4.9.2.2, figure 16, the link TRBs cannot be chained, so
113 * it shall not be called in cycle, nor have an inner cycle.
114 */
115static void trb_ring_resolve_link(xhci_trb_ring_t *ring)
116{
117 link_t *next_segment = list_next(&ring->enqueue_segment->segments_link, &ring->segments);
118 if (!next_segment)
119 next_segment = list_first(&ring->segments);
120
121 ring->enqueue_segment = list_get_instance(next_segment, trb_segment_t, segments_link);
122 ring->enqueue_trb = segment_begin(ring->enqueue_segment);
123}
124
125static uintptr_t xhci_trb_ring_enqueue_phys(xhci_trb_ring_t *ring)
126{
127 uintptr_t trb_id = ring->enqueue_trb - segment_begin(ring->enqueue_segment);
128 return ring->enqueue_segment->phys + trb_id * sizeof(xhci_trb_t);
129}
130
131int trb_ring_enqueue(xhci_trb_ring_t *ring, xhci_trb_t *td)
132{
133 xhci_trb_t * const saved_enqueue_trb = ring->enqueue_trb;
134 trb_segment_t * const saved_enqueue_segment = ring->enqueue_segment;
135
136 /*
137 * First, dry run and advance the enqueue pointer to see if the ring would
138 * be full anytime during the transaction.
139 */
140 xhci_trb_t *trb = td;
141 do {
142 ring->enqueue_trb++;
143
144 if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK)
145 trb_ring_resolve_link(ring);
146
147 if (xhci_trb_ring_enqueue_phys(ring) == ring->dequeue)
148 goto err_again;
149 } while (xhci_trb_is_chained(trb++));
150
151 ring->enqueue_segment = saved_enqueue_segment;
152 ring->enqueue_trb = saved_enqueue_trb;
153
154 /*
155 * Now, copy the TRBs without further checking.
156 */
157 trb = td;
158 do {
159 xhci_trb_set_cycle(trb, ring->pcs);
160 xhci_trb_copy(ring->enqueue_trb, trb);
161
162 ring->enqueue_trb++;
163
164 if (TRB_TYPE(*ring->enqueue_trb) == XHCI_TRB_TYPE_LINK) {
165 // XXX: Check, whether the order here is correct (ambiguous instructions in 4.11.5.1)
166 xhci_trb_set_cycle(ring->enqueue_trb, ring->pcs);
167
168 if (TRB_LINK_TC(*ring->enqueue_trb))
169 ring->pcs = !ring->pcs;
170
171 trb_ring_resolve_link(ring);
172 }
173 } while (xhci_trb_is_chained(trb++));
174
175 return EOK;
176
177err_again:
178 ring->enqueue_segment = saved_enqueue_segment;
179 ring->enqueue_trb = saved_enqueue_trb;
180 return EAGAIN;
181}
Note: See TracBrowser for help on using the repository browser.