1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "nouveau_drv.h"
28#include "nouveau_dma.h"
29#include "nouveau_vmm.h"
30
31#include <nvif/user.h>
32
33
34
35
36
37
38
39
40static inline int
41READ_GET(struct nouveau_channel *chan, uint64_t *prev_get, int *timeout)
42{
43 uint64_t val;
44
45 val = nvif_rd32(&chan->user, chan->user_get);
46 if (chan->user_get_hi)
47 val |= (uint64_t)nvif_rd32(&chan->user, chan->user_get_hi) << 32;
48
49
50
51
52
53 if (val != *prev_get) {
54 *prev_get = val;
55 *timeout = 0;
56 }
57
58 if ((++*timeout & 0xff) == 0) {
59 udelay(1);
60 if (*timeout > 100000)
61 return -EBUSY;
62 }
63
64 if (val < chan->push.addr ||
65 val > chan->push.addr + (chan->dma.max << 2))
66 return -EINVAL;
67
68 return (val - chan->push.addr) >> 2;
69}
70
71void
72nv50_dma_push(struct nouveau_channel *chan, u64 offset, int length)
73{
74 struct nvif_user *user = &chan->drm->client.device.user;
75 struct nouveau_bo *pb = chan->push.buffer;
76 int ip = (chan->dma.ib_put * 2) + chan->dma.ib_base;
77
78 BUG_ON(chan->dma.ib_free < 1);
79
80 nouveau_bo_wr32(pb, ip++, lower_32_bits(offset));
81 nouveau_bo_wr32(pb, ip++, upper_32_bits(offset) | length << 8);
82
83 chan->dma.ib_put = (chan->dma.ib_put + 1) & chan->dma.ib_max;
84
85 mb();
86
87 nouveau_bo_rd32(pb, 0);
88
89 nvif_wr32(&chan->user, 0x8c, chan->dma.ib_put);
90 if (user->func && user->func->doorbell)
91 user->func->doorbell(user, chan->token);
92 chan->dma.ib_free--;
93}
94
95static int
96nv50_dma_push_wait(struct nouveau_channel *chan, int count)
97{
98 uint32_t cnt = 0, prev_get = 0;
99
100 while (chan->dma.ib_free < count) {
101 uint32_t get = nvif_rd32(&chan->user, 0x88);
102 if (get != prev_get) {
103 prev_get = get;
104 cnt = 0;
105 }
106
107 if ((++cnt & 0xff) == 0) {
108 udelay(1);
109 if (cnt > 100000)
110 return -EBUSY;
111 }
112
113 chan->dma.ib_free = get - chan->dma.ib_put;
114 if (chan->dma.ib_free <= 0)
115 chan->dma.ib_free += chan->dma.ib_max;
116 }
117
118 return 0;
119}
120
121static int
122nv50_dma_wait(struct nouveau_channel *chan, int slots, int count)
123{
124 uint64_t prev_get = 0;
125 int ret, cnt = 0;
126
127 ret = nv50_dma_push_wait(chan, slots + 1);
128 if (unlikely(ret))
129 return ret;
130
131 while (chan->dma.free < count) {
132 int get = READ_GET(chan, &prev_get, &cnt);
133 if (unlikely(get < 0)) {
134 if (get == -EINVAL)
135 continue;
136
137 return get;
138 }
139
140 if (get <= chan->dma.cur) {
141 chan->dma.free = chan->dma.max - chan->dma.cur;
142 if (chan->dma.free >= count)
143 break;
144
145 FIRE_RING(chan);
146 do {
147 get = READ_GET(chan, &prev_get, &cnt);
148 if (unlikely(get < 0)) {
149 if (get == -EINVAL)
150 continue;
151 return get;
152 }
153 } while (get == 0);
154 chan->dma.cur = 0;
155 chan->dma.put = 0;
156 }
157
158 chan->dma.free = get - chan->dma.cur - 1;
159 }
160
161 return 0;
162}
163
164int
165nouveau_dma_wait(struct nouveau_channel *chan, int slots, int size)
166{
167 uint64_t prev_get = 0;
168 int cnt = 0, get;
169
170 if (chan->dma.ib_max)
171 return nv50_dma_wait(chan, slots, size);
172
173 while (chan->dma.free < size) {
174 get = READ_GET(chan, &prev_get, &cnt);
175 if (unlikely(get == -EBUSY))
176 return -EBUSY;
177
178
179
180
181
182
183
184
185
186
187 if (unlikely(get == -EINVAL) || get < NOUVEAU_DMA_SKIPS)
188 continue;
189
190 if (get <= chan->dma.cur) {
191
192
193
194
195
196
197
198
199
200
201
202
203
204 chan->dma.free = chan->dma.max - chan->dma.cur;
205 if (chan->dma.free >= size)
206 break;
207
208
209
210
211
212 OUT_RING(chan, chan->push.addr | 0x20000000);
213
214
215
216
217
218
219 do {
220 get = READ_GET(chan, &prev_get, &cnt);
221 if (unlikely(get == -EBUSY))
222 return -EBUSY;
223 if (unlikely(get == -EINVAL))
224 continue;
225 } while (get <= NOUVEAU_DMA_SKIPS);
226 WRITE_PUT(NOUVEAU_DMA_SKIPS);
227
228
229
230
231 chan->dma.cur =
232 chan->dma.put = NOUVEAU_DMA_SKIPS;
233 }
234
235
236
237
238
239
240
241 chan->dma.free = get - chan->dma.cur - 1;
242 }
243
244 return 0;
245}
246
247