nouveau_dma.c revision 1.3 1 /* $NetBSD: nouveau_dma.c,v 1.3 2018/08/27 04:58:24 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2007 Ben Skeggs.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nouveau_dma.c,v 1.3 2018/08/27 04:58:24 riastradh Exp $");
31
32 #include "nouveau_drm.h"
33 #include "nouveau_dma.h"
34
35 #ifdef __NetBSD__
36 # define __iomem
37 # define __force
38 #endif
39
40 void
41 OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned nr_dwords)
42 {
43 bool is_iomem;
44 u32 *mem = ttm_kmap_obj_virtual(&chan->push.buffer->kmap, &is_iomem);
45 mem = &mem[chan->dma.cur];
46 if (is_iomem)
47 memcpy_toio((void __force __iomem *)mem, data, nr_dwords * 4);
48 else
49 memcpy(mem, data, nr_dwords * 4);
50 chan->dma.cur += nr_dwords;
51 }
52
53 #ifdef __NetBSD__
54 # undef __force
55 # undef __iomem
56 #endif
57
58 /* Fetch and adjust GPU GET pointer
59 *
60 * Returns:
61 * value >= 0, the adjusted GET pointer
62 * -EINVAL if GET pointer currently outside main push buffer
63 * -EBUSY if timeout exceeded
64 */
65 static inline int
66 READ_GET(struct nouveau_channel *chan, uint64_t *prev_get, int *timeout)
67 {
68 uint64_t val;
69
70 val = nvif_rd32(&chan->user, chan->user_get);
71 if (chan->user_get_hi)
72 val |= (uint64_t)nvif_rd32(&chan->user, chan->user_get_hi) << 32;
73
74 /* reset counter as long as GET is still advancing, this is
75 * to avoid misdetecting a GPU lockup if the GPU happens to
76 * just be processing an operation that takes a long time
77 */
78 if (val != *prev_get) {
79 *prev_get = val;
80 *timeout = 0;
81 }
82
83 if ((++*timeout & 0xff) == 0) {
84 udelay(1);
85 if (*timeout > 100000)
86 return -EBUSY;
87 }
88
89 if (val < chan->push.vma.offset ||
90 val > chan->push.vma.offset + (chan->dma.max << 2))
91 return -EINVAL;
92
93 return (val - chan->push.vma.offset) >> 2;
94 }
95
96 void
97 nv50_dma_push(struct nouveau_channel *chan, struct nouveau_bo *bo,
98 int delta, int length)
99 {
100 struct nouveau_cli *cli = (void *)chan->user.client;
101 struct nouveau_bo *pb = chan->push.buffer;
102 struct nvkm_vma *vma;
103 int ip = (chan->dma.ib_put * 2) + chan->dma.ib_base;
104 u64 offset;
105
106 vma = nouveau_bo_vma_find(bo, cli->vm);
107 BUG_ON(!vma);
108 offset = vma->offset + delta;
109
110 BUG_ON(chan->dma.ib_free < 1);
111
112 nouveau_bo_wr32(pb, ip++, lower_32_bits(offset));
113 nouveau_bo_wr32(pb, ip++, upper_32_bits(offset) | length << 8);
114
115 chan->dma.ib_put = (chan->dma.ib_put + 1) & chan->dma.ib_max;
116
117 mb();
118 /* Flush writes. */
119 nouveau_bo_rd32(pb, 0);
120
121 nvif_wr32(&chan->user, 0x8c, chan->dma.ib_put);
122 chan->dma.ib_free--;
123 }
124
125 static int
126 nv50_dma_push_wait(struct nouveau_channel *chan, int count)
127 {
128 uint32_t cnt = 0, prev_get = 0;
129
130 while (chan->dma.ib_free < count) {
131 uint32_t get = nvif_rd32(&chan->user, 0x88);
132 if (get != prev_get) {
133 prev_get = get;
134 cnt = 0;
135 }
136
137 if ((++cnt & 0xff) == 0) {
138 DRM_UDELAY(1);
139 if (cnt > 100000)
140 return -EBUSY;
141 }
142
143 chan->dma.ib_free = get - chan->dma.ib_put;
144 if (chan->dma.ib_free <= 0)
145 chan->dma.ib_free += chan->dma.ib_max;
146 }
147
148 return 0;
149 }
150
151 static int
152 nv50_dma_wait(struct nouveau_channel *chan, int slots, int count)
153 {
154 uint64_t prev_get = 0;
155 int ret, cnt = 0;
156
157 ret = nv50_dma_push_wait(chan, slots + 1);
158 if (unlikely(ret))
159 return ret;
160
161 while (chan->dma.free < count) {
162 int get = READ_GET(chan, &prev_get, &cnt);
163 if (unlikely(get < 0)) {
164 if (get == -EINVAL)
165 continue;
166
167 return get;
168 }
169
170 if (get <= chan->dma.cur) {
171 chan->dma.free = chan->dma.max - chan->dma.cur;
172 if (chan->dma.free >= count)
173 break;
174
175 FIRE_RING(chan);
176 do {
177 get = READ_GET(chan, &prev_get, &cnt);
178 if (unlikely(get < 0)) {
179 if (get == -EINVAL)
180 continue;
181 return get;
182 }
183 } while (get == 0);
184 chan->dma.cur = 0;
185 chan->dma.put = 0;
186 }
187
188 chan->dma.free = get - chan->dma.cur - 1;
189 }
190
191 return 0;
192 }
193
194 int
195 nouveau_dma_wait(struct nouveau_channel *chan, int slots, int size)
196 {
197 uint64_t prev_get = 0;
198 int cnt = 0, get;
199
200 if (chan->dma.ib_max)
201 return nv50_dma_wait(chan, slots, size);
202
203 while (chan->dma.free < size) {
204 get = READ_GET(chan, &prev_get, &cnt);
205 if (unlikely(get == -EBUSY))
206 return -EBUSY;
207
208 /* loop until we have a usable GET pointer. the value
209 * we read from the GPU may be outside the main ring if
210 * PFIFO is processing a buffer called from the main ring,
211 * discard these values until something sensible is seen.
212 *
213 * the other case we discard GET is while the GPU is fetching
214 * from the SKIPS area, so the code below doesn't have to deal
215 * with some fun corner cases.
216 */
217 if (unlikely(get == -EINVAL) || get < NOUVEAU_DMA_SKIPS)
218 continue;
219
220 if (get <= chan->dma.cur) {
221 /* engine is fetching behind us, or is completely
222 * idle (GET == PUT) so we have free space up until
223 * the end of the push buffer
224 *
225 * we can only hit that path once per call due to
226 * looping back to the beginning of the push buffer,
227 * we'll hit the fetching-ahead-of-us path from that
228 * point on.
229 *
230 * the *one* exception to that rule is if we read
231 * GET==PUT, in which case the below conditional will
232 * always succeed and break us out of the wait loop.
233 */
234 chan->dma.free = chan->dma.max - chan->dma.cur;
235 if (chan->dma.free >= size)
236 break;
237
238 /* not enough space left at the end of the push buffer,
239 * instruct the GPU to jump back to the start right
240 * after processing the currently pending commands.
241 */
242 OUT_RING(chan, chan->push.vma.offset | 0x20000000);
243
244 /* wait for GET to depart from the skips area.
245 * prevents writing GET==PUT and causing a race
246 * condition that causes us to think the GPU is
247 * idle when it's not.
248 */
249 do {
250 get = READ_GET(chan, &prev_get, &cnt);
251 if (unlikely(get == -EBUSY))
252 return -EBUSY;
253 if (unlikely(get == -EINVAL))
254 continue;
255 } while (get <= NOUVEAU_DMA_SKIPS);
256 WRITE_PUT(NOUVEAU_DMA_SKIPS);
257
258 /* we're now submitting commands at the start of
259 * the push buffer.
260 */
261 chan->dma.cur =
262 chan->dma.put = NOUVEAU_DMA_SKIPS;
263 }
264
265 /* engine fetching ahead of us, we have space up until the
266 * current GET pointer. the "- 1" is to ensure there's
267 * space left to emit a jump back to the beginning of the
268 * push buffer if we require it. we can never get GET == PUT
269 * here, so this is safe.
270 */
271 chan->dma.free = get - chan->dma.cur - 1;
272 }
273
274 return 0;
275 }
276
277